1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see [sun.com URL with a
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
42 #include <sys/param.h>
44 #include <lnet/api-support.h>
48 static command_t * top_level; /* Top level of commands, initialized by
50 static char * parser_prompt = NULL;/* Parser prompt, set by InitParser */
51 static int done; /* Set to 1 if user types exit or quit */
54 /* static functions */
55 static char *skipwhitespace(char *s);
56 static char *skiptowhitespace(char *s);
57 static command_t *find_cmd(char *name, command_t cmds[], char **next);
58 static int process(char *s, char **next, command_t *lookup, command_t **result,
60 static void print_commands(char *str, command_t *table);
62 static char * skipwhitespace(char * s)
68 for (t = s; t <= s + len && isspace(*t); t++);
73 static char * skiptowhitespace(char * s)
77 for (t = s; *t && !isspace(*t); t++);
81 static int line2args(char *line, char **argv, int maxargs)
86 arg = strtok(line, " \t");
93 while( (arg = strtok(NULL, " \t")) && (i <= maxargs)) {
100 /* find a command -- return it if unique otherwise print alternatives */
101 static command_t *Parser_findargcmd(char *name, command_t cmds[])
105 for (cmd = cmds; cmd->pc_name; cmd++) {
106 if (strcmp(name, cmd->pc_name) == 0)
112 int Parser_execarg(int argc, char **argv, command_t cmds[])
116 cmd = Parser_findargcmd(argv[0], cmds);
118 int rc = (cmd->pc_func)(argc, argv);
120 fprintf(stderr, "%s\n", cmd->pc_help);
123 printf("Try interactive use without arguments or use one of:\n");
124 for (cmd = cmds; cmd->pc_name; cmd++)
125 printf("\"%s\" ", cmd->pc_name);
126 printf("\nas argument.\n");
131 /* returns the command_t * (NULL if not found) corresponding to a
132 _partial_ match with the first token in name. It sets *next to
133 point to the following token. Does not modify *name. */
134 static command_t * find_cmd(char * name, command_t cmds[], char ** next)
141 /* This sets name to point to the first non-white space character,
142 and next to the first whitespace after name, len to the length: do
144 name = skipwhitespace(name);
145 *next = skiptowhitespace(name);
150 for (i = 0; cmds[i].pc_name; i++) {
151 if (strncasecmp(name, cmds[i].pc_name, len) == 0) {
152 *next = skipwhitespace(*next);
159 /* Recursively process a command line string s and find the command
160 corresponding to it. This can be ambiguous, full, incomplete,
162 static int process(char *s, char ** next, command_t *lookup,
163 command_t **result, char **prev)
165 *result = find_cmd(s, lookup, next);
172 /* found entry: is it ambigous, i.e. not exact command name and
173 more than one command in the list matches. Note that find_cmd
174 points to the first ambiguous entry */
175 if ( strncasecmp(s, (*result)->pc_name, strlen((*result)->pc_name)) &&
176 find_cmd(s, (*result) + 1, next))
179 /* found a unique command: component or full? */
180 if ( (*result)->pc_func ) {
183 if ( *next == '\0' ) {
184 return CMD_INCOMPLETE;
186 return process(*next, next, (*result)->pc_sub_cmd, result, prev);
191 #ifdef HAVE_LIBREADLINE
192 static command_t * match_tbl; /* Command completion against this table */
193 static char * command_generator(const char * text, int state)
199 /* Do we have a match table? */
203 /* If this is the first time called on this word, state is 0 */
206 len = (int)strlen(text);
209 /* Return next name in the command list that paritally matches test */
210 while ( (name = (match_tbl + index)->pc_name) ) {
213 if (strncasecmp(name, text, len) == 0) {
214 return(strdup(name));
218 /* No more matches */
222 /* probably called by readline */
223 static char **command_completion(char * text, int start, int end)
228 match_tbl = top_level;
230 for (table = find_cmd(rl_line_buffer, match_tbl, &pos);
231 table; table = find_cmd(pos, match_tbl, &pos))
234 if (*(pos - 1) == ' ') match_tbl = table->pc_sub_cmd;
237 return completion_matches(text, command_generator);
241 /* take a string and execute the function or print help */
242 int execute_line(char * line)
244 command_t *cmd, *ambig;
251 switch( process(line, &next, top_level, &cmd, &prev) ) {
253 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
254 while( (ambig = find_cmd(prev, cmd, &tmp)) ) {
255 fprintf(stderr, "%s ", ambig->pc_name);
258 fprintf(stderr, "\n");
261 fprintf(stderr, "No such command, type help\n");
265 "'%s' incomplete command. Use '%s x' where x is one of:\n",
267 fprintf(stderr, "\t");
268 for (i = 0; cmd->pc_sub_cmd[i].pc_name; i++) {
269 fprintf(stderr, "%s ", cmd->pc_sub_cmd[i].pc_name);
271 fprintf(stderr, "\n");
274 i = line2args(line, argv, MAXARGS);
275 rc = (cmd->pc_func)(i, argv);
278 fprintf(stderr, "%s\n", cmd->pc_help);
292 /* just in case you're ever in an airplane and discover you
293 forgot to install readline-dev. :) */
296 int interactive = isatty (fileno (stdin));
298 #ifdef HAVE_LIBREADLINE
300 stifle_history(HISTORY);
304 rl_prep_term_function = (rl_vintfunc_t *)noop_fn;
305 rl_deprep_term_function = (rl_voidfunc_t *)noop_fn;
308 rl_attempted_completion_function = (CPPFunction *)command_completion;
309 rl_completion_entry_function = (void *)command_generator;
314 #ifndef HAVE_LIBREADLINE
315 #define add_history(s)
316 char * readline(char * prompt)
321 printf ("%s", prompt);
322 if (fgets(line, sizeof(line), stdin) == NULL)
325 if (n && line[n-1] == '\n')
331 /* this is the command execution machine */
332 int Parser_commands(void)
338 interactive = init_input();
341 line = readline(interactive ? parser_prompt : NULL);
345 s = skipwhitespace(line);
349 rc = execute_line(s);
351 /* reset optind to 0 to tell getopt
352 * to reinitialize itself */
362 /* sets the parser prompt */
363 void Parser_init(char * prompt, command_t * cmds)
367 if (parser_prompt) free(parser_prompt);
368 parser_prompt = strdup(prompt);
371 /* frees the parser prompt */
372 void Parser_exit(int argc, char *argv[])
376 parser_prompt = NULL;
379 /* convert a string to an integer */
380 int Parser_int(char *s, int *val)
385 ret = sscanf(s, "%d", val);
386 else if (*(s+1) != 'x')
387 ret = sscanf(s, "%o", val);
390 ret = sscanf(++s, "%x", val);
397 void Parser_qhelp(int argc, char *argv[]) {
399 printf("Available commands are:\n");
401 print_commands(NULL, top_level);
402 printf("For more help type: help command-name\n");
405 int Parser_help(int argc, char **argv)
408 char *next, *prev, *tmp;
409 command_t *result, *ambig;
413 Parser_qhelp(argc, argv);
418 for ( i = 1 ; i < argc ; i++ ) {
419 strcat(line, argv[i]);
422 switch ( process(line, &next, top_level, &result, &prev) ) {
424 fprintf(stderr, "%s: %s\n",line, result->pc_help);
427 fprintf(stderr, "%s: Unknown command.\n", line);
431 "'%s' incomplete command. Use '%s x' where x is one of:\n",
433 fprintf(stderr, "\t");
434 for (i = 0; result->pc_sub_cmd[i].pc_name; i++) {
435 fprintf(stderr, "%s ", result->pc_sub_cmd[i].pc_name);
437 fprintf(stderr, "\n");
440 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
441 while( (ambig = find_cmd(prev, result, &tmp)) ) {
442 fprintf(stderr, "%s ", ambig->pc_name);
445 fprintf(stderr, "\n");
452 void Parser_printhelp(char *cmd)
454 char *argv[] = { "help", cmd };
455 Parser_help(2, argv);
458 /*************************************************************************
460 *************************************************************************/
463 static void print_commands(char * str, command_t * table) {
467 for (cmds = table; cmds->pc_name; cmds++) {
469 if (str) printf("\t%s %s\n", str, cmds->pc_name);
470 else printf("\t%s\n", cmds->pc_name);
472 if (cmds->pc_sub_cmd) {
474 sprintf(buf, "%s %s", str, cmds->pc_name);
475 print_commands(buf, cmds->pc_sub_cmd);
477 print_commands(cmds->pc_name, cmds->pc_sub_cmd);
483 char *Parser_getstr(const char *prompt, const char *deft, char *res,
487 int size = strlen(prompt) + strlen(deft) + 8;
489 theprompt = malloc(size);
492 sprintf(theprompt, "%s [%s]: ", prompt, deft);
494 line = readline(theprompt);
497 if ( line == NULL || *line == '\0' ) {
498 strncpy(res, deft, len);
500 strncpy(res, line, len);
511 /* get integer from prompt, loop forever to get it */
512 int Parser_getint(const char *prompt, long min, long max, long deft, int base)
517 int size = strlen(prompt) + 40;
518 char *theprompt = malloc(size);
520 sprintf(theprompt,"%s [%ld, (0x%lx)]: ", prompt, deft, deft);
526 line = readline(theprompt);
528 fprintf(stdout, "Please enter an integer.\n");
532 if ( *line == '\0' ) {
537 rc = Parser_arg2int(line, &result, base);
540 fprintf(stdout, "Invalid string.\n");
542 } else if ( result > max || result < min ) {
543 fprintf(stdout, "Error: response must lie between %ld and %ld.\n",
557 /* get boolean (starting with YyNn; loop forever */
558 int Parser_getbool(const char *prompt, int deft)
562 int size = strlen(prompt) + 8;
563 char *theprompt = malloc(size);
568 if ( deft != 0 && deft != 1 ) {
569 fprintf(stderr, "Error: Parser_getbool given bad default (%d).\n",
573 sprintf(theprompt, "%s [%s]: ", prompt, (deft==0)? "N" : "Y");
577 line = readline(theprompt);
578 if ( line == NULL ) {
582 if ( *line == '\0' ) {
586 if ( *line == 'y' || *line == 'Y' ) {
590 if ( *line == 'n' || *line == 'N' ) {
596 fprintf(stdout, "Invalid string. Must start with yY or nN\n");
607 /* parse int out of a string or prompt for it */
608 long Parser_intarg(const char *inp, const char *prompt, int deft,
609 int min, int max, int base)
614 rc = Parser_arg2int(inp, &result, base);
619 return Parser_getint(prompt, deft, min, max, base);
623 /* parse int out of a string or prompt for it */
624 char *Parser_strarg(char *inp, const char *prompt, const char *deft,
625 char *answer, int len)
627 if ( inp == NULL || *inp == '\0' ) {
628 return Parser_getstr(prompt, deft, answer, len);
633 /* change a string into a number: return 0 on success. No invalid characters
634 allowed. The processing of base and validity follows strtol(3)*/
635 int Parser_arg2int(const char *inp, long *result, int base)
639 if ( (base !=0) && (base < 2 || base > 36) )
642 *result = strtol(inp, &endptr, base);
644 if ( *inp != '\0' && *endptr == '\0' )
650 int Parser_quit(int argc, char **argv)