2 * Copyright (C) 2001 Cluster File Systems, Inc.
4 * This file is part of Lustre, http://www.sf.net/projects/lustre/
6 * Lustre is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
10 * Lustre is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with Lustre; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <libcfs/libcfsutil.h>
23 static command_t * top_level; /* Top level of commands, initialized by
25 static char * parser_prompt = NULL;/* Parser prompt, set by InitParser */
26 static int done; /* Set to 1 if user types exit or quit */
27 static int ignore_errors; /* Normally, the parser will quit when
28 an error occurs in non-interacive
29 mode. Setting this to non-zero will
30 force it to keep buggering on. */
33 /* static functions */
34 static char *skipwhitespace(char *s);
35 static char *skiptowhitespace(char *s);
36 static command_t *find_cmd(char *name, command_t cmds[], char **next);
37 static int process(char *s, char **next, command_t *lookup, command_t **result,
39 static void print_commands(char *str, command_t *table);
41 static char * skipwhitespace(char * s)
47 for (t = s; t <= s + len && isspace(*t); t++);
52 static char * skiptowhitespace(char * s)
56 for (t = s; *t && !isspace(*t); t++);
60 static int line2args(char *line, char **argv, int maxargs)
65 arg = strtok(line, " \t");
66 if (arg == NULL || maxargs < 1)
70 while ((arg = strtok(NULL, " \t")) != NULL && i < maxargs)
75 /* find a command -- return it if unique otherwise print alternatives */
76 static command_t *Parser_findargcmd(char *name, command_t cmds[])
80 for (cmd = cmds; cmd->pc_name; cmd++) {
81 if (strcmp(name, cmd->pc_name) == 0)
87 void Parser_ignore_errors(int ignore)
89 ignore_errors = ignore;
92 int Parser_execarg(int argc, char **argv, command_t cmds[])
96 cmd = Parser_findargcmd(argv[0], cmds);
98 int rc = (cmd->pc_func)(argc, argv);
100 fprintf(stderr, "%s\n", cmd->pc_help);
103 printf("Try interactive use without arguments or use one of:\n");
104 for (cmd = cmds; cmd->pc_name; cmd++)
105 printf("\"%s\"\n", cmd->pc_name);
106 printf("as argument.\n");
111 /* returns the command_t * (NULL if not found) corresponding to a
112 _partial_ match with the first token in name. It sets *next to
113 point to the following token. Does not modify *name. */
114 static command_t * find_cmd(char * name, command_t cmds[], char ** next)
121 /* This sets name to point to the first non-white space character,
122 and next to the first whitespace after name, len to the length: do
124 name = skipwhitespace(name);
125 *next = skiptowhitespace(name);
126 len = (int)(*next - name);
130 for (i = 0; cmds[i].pc_name; i++) {
131 if (strncasecmp(name, cmds[i].pc_name, len) == 0) {
132 *next = skipwhitespace(*next);
139 /* Recursively process a command line string s and find the command
140 corresponding to it. This can be ambiguous, full, incomplete,
142 static int process(char *s, char ** next, command_t *lookup,
143 command_t **result, char **prev)
145 *result = find_cmd(s, lookup, next);
152 /* found entry: is it ambigous, i.e. not exact command name and
153 more than one command in the list matches. Note that find_cmd
154 points to the first ambiguous entry */
155 if (strncasecmp(s, (*result)->pc_name, strlen((*result)->pc_name))) {
157 command_t *another_result = find_cmd(s, (*result) + 1,
159 int found_another = 0;
161 while (another_result) {
162 if (strncasecmp(s, another_result->pc_name,
163 strlen(another_result->pc_name)) == 0){
164 *result = another_result;
165 *next = another_next;
168 another_result = find_cmd(s, another_result + 1,
177 /* found a unique command: component or full? */
178 if ( (*result)->pc_func ) {
181 if ( *next == '\0' ) {
182 return CMD_INCOMPLETE;
184 return process(*next, next, (*result)->pc_sub_cmd,
190 #ifdef HAVE_LIBREADLINE
191 static command_t * match_tbl; /* Command completion against this table */
192 static char * command_generator(const char * text, int state)
198 /* Do we have a match table? */
202 /* If this is the first time called on this word, state is 0 */
205 len = (int)strlen(text);
208 /* Return next name in the command list that paritally matches test */
209 while ( (name = (match_tbl + index)->pc_name) ) {
212 if (strncasecmp(name, text, len) == 0) {
213 return(strdup(name));
217 /* No more matches */
221 /* probably called by readline */
222 static char **command_completion(char * text, int start, int end)
227 match_tbl = top_level;
229 for (table = find_cmd(rl_line_buffer, match_tbl, &pos);
230 table; table = find_cmd(pos, match_tbl, &pos))
233 if (*(pos - 1) == ' ') match_tbl = table->pc_sub_cmd;
236 return completion_matches(text, command_generator);
240 /* take a string and execute the function or print help */
241 int execute_line(char * line)
243 command_t *cmd, *ambig;
250 switch (process(line, &next, top_level, &cmd, &prev)) {
252 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
253 while( (ambig = find_cmd(prev, cmd, &tmp)) ) {
254 fprintf(stderr, "%s ", ambig->pc_name);
257 fprintf(stderr, "\n");
260 fprintf(stderr, "No such command, type help\n");
264 "'%s' incomplete command. Use '%s x' where x is one of:\n",
266 fprintf(stderr, "\t");
267 for (i = 0; cmd->pc_sub_cmd[i].pc_name; i++) {
268 fprintf(stderr, "%s ", cmd->pc_sub_cmd[i].pc_name);
270 fprintf(stderr, "\n");
273 i = line2args(line, argv, MAXARGS);
274 rc = (cmd->pc_func)(i, argv);
277 fprintf(stderr, "%s\n", cmd->pc_help);
291 /* just in case you're ever in an airplane and discover you
292 forgot to install readline-dev. :) */
295 int interactive = isatty (fileno (stdin));
297 #ifdef HAVE_LIBREADLINE
299 stifle_history(HISTORY);
303 rl_prep_term_function = (rl_vintfunc_t *)noop_fn;
304 rl_deprep_term_function = (rl_voidfunc_t *)noop_fn;
307 rl_attempted_completion_function = (CPPFunction *)command_completion;
308 rl_completion_entry_function = (void *)command_generator;
313 #ifndef HAVE_LIBREADLINE
314 #define add_history(s)
315 char * readline(char * prompt)
318 char *line = malloc(size);
326 printf ("%s", prompt);
329 if ((c = fgetc(stdin)) != EOF) {
334 if (ptr - line >= size - 1) {
341 memcpy(tmp, line, ptr - line);
342 ptr = tmp + (ptr - line);
348 if (ferror(stdin) || feof(stdin))
355 if (eof && (strlen(line) == 0)) {
366 /* this is the command execution machine */
367 int Parser_commands(void)
370 int rc = 0, save_error = 0;
373 interactive = init_input();
376 line = readline(interactive ? parser_prompt : NULL);
380 s = skipwhitespace(line);
384 rc = execute_line(s);
386 /* stop on error if not-interactive */
387 if (rc != 0 && !interactive) {
402 /* sets the parser prompt */
403 void Parser_init(char * prompt, command_t * cmds)
407 if (parser_prompt) free(parser_prompt);
408 parser_prompt = strdup(prompt);
411 /* frees the parser prompt */
412 void Parser_exit(int argc, char *argv[])
416 parser_prompt = NULL;
419 /* convert a string to an integer */
420 int Parser_int(char *s, int *val)
425 ret = sscanf(s, "%d", val);
426 else if (*(s+1) != 'x')
427 ret = sscanf(s, "%o", val);
430 ret = sscanf(++s, "%x", val);
437 void Parser_qhelp(int argc, char *argv[]) {
439 printf("Available commands are:\n");
441 print_commands(NULL, top_level);
442 printf("For more help type: help command-name\n");
445 int Parser_help(int argc, char **argv)
448 char *next, *prev, *tmp;
449 command_t *result, *ambig;
453 Parser_qhelp(argc, argv);
458 for ( i = 1 ; i < argc ; i++ ) {
459 if (strlen(argv[i]) > sizeof(line)-strlen(line)-1)
461 strncat(line, argv[i], sizeof(line)-strlen(line)-1);
464 switch ( process(line, &next, top_level, &result, &prev) ) {
466 fprintf(stderr, "%s: %s\n",line, result->pc_help);
469 fprintf(stderr, "%s: Unknown command.\n", line);
473 "'%s' incomplete command. Use '%s x' where x is one of:\n",
475 fprintf(stderr, "\t");
476 for (i = 0; result->pc_sub_cmd[i].pc_name; i++) {
477 fprintf(stderr, "%s ", result->pc_sub_cmd[i].pc_name);
479 fprintf(stderr, "\n");
482 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
483 while( (ambig = find_cmd(prev, result, &tmp)) ) {
484 fprintf(stderr, "%s ", ambig->pc_name);
487 fprintf(stderr, "\n");
494 void Parser_printhelp(char *cmd)
496 char *argv[] = { "help", cmd };
497 Parser_help(2, argv);
501 /*************************************************************************
503 *************************************************************************/
504 static void print_commands(char * str, command_t * table) {
508 for (cmds = table; cmds->pc_name; cmds++) {
510 if (str) printf("\t%s %s\n", str, cmds->pc_name);
511 else printf("\t%s\n", cmds->pc_name);
513 if (cmds->pc_sub_cmd) {
515 sprintf(buf, "%s %s", str, cmds->pc_name);
516 print_commands(buf, cmds->pc_sub_cmd);
518 print_commands(cmds->pc_name, cmds->pc_sub_cmd);
524 char *Parser_getstr(const char *prompt, const char *deft, char *res,
528 int size = strlen(prompt) + strlen(deft) + 8;
530 theprompt = malloc(size);
533 sprintf(theprompt, "%s [%s]: ", prompt, deft);
535 line = readline(theprompt);
538 if ( line == NULL || *line == '\0' ) {
539 strncpy(res, deft, len);
541 strncpy(res, line, len);
552 /* get integer from prompt, loop forever to get it */
553 int Parser_getint(const char *prompt, long min, long max, long deft, int base)
558 int size = strlen(prompt) + 40;
559 char *theprompt = malloc(size);
561 sprintf(theprompt,"%s [%ld, (0x%lx)]: ", prompt, deft, deft);
567 line = readline(theprompt);
569 fprintf(stdout, "Please enter an integer.\n");
573 if ( *line == '\0' ) {
578 rc = Parser_arg2int(line, &result, base);
581 fprintf(stdout, "Invalid string.\n");
583 } else if ( result > max || result < min ) {
584 fprintf(stdout, "Error: response must lie between %ld and %ld.\n",
598 /* get boolean (starting with YyNn; loop forever */
599 int Parser_getbool(const char *prompt, int deft)
603 int size = strlen(prompt) + 8;
604 char *theprompt = malloc(size);
609 if ( deft != 0 && deft != 1 ) {
610 fprintf(stderr, "Error: Parser_getbool given bad default %d\n",
614 sprintf(theprompt, "%s [%s]: ", prompt, (deft==0)? "N" : "Y");
618 line = readline(theprompt);
619 if ( line == NULL ) {
623 if ( *line == '\0' ) {
627 if ( *line == 'y' || *line == 'Y' ) {
631 if ( *line == 'n' || *line == 'N' ) {
637 fprintf(stdout, "Invalid string. Must start with yY or nN\n");
648 /* parse int out of a string or prompt for it */
649 long Parser_intarg(const char *inp, const char *prompt, int deft,
650 int min, int max, int base)
655 rc = Parser_arg2int(inp, &result, base);
660 return Parser_getint(prompt, deft, min, max, base);
664 /* parse int out of a string or prompt for it */
665 char *Parser_strarg(char *inp, const char *prompt, const char *deft,
666 char *answer, int len)
668 if ( inp == NULL || *inp == '\0' ) {
669 return Parser_getstr(prompt, deft, answer, len);
674 /* change a string into a number: return 0 on success. No invalid characters
675 allowed. The processing of base and validity follows strtol(3)*/
676 int Parser_arg2int(const char *inp, long *result, int base)
680 if ( (base !=0) && (base < 2 || base > 36) )
683 *result = strtol(inp, &endptr, base);
685 if ( *inp != '\0' && *endptr == '\0' )
691 /* Convert human readable size string to and int; "1k" -> 1000 */
692 int Parser_size (int *sizep, char *str) {
696 switch (sscanf (str, "%d%1[gGmMkK]", &size, mod)) {
728 /* Convert a string boolean to an int; "enable" -> 1 */
729 int Parser_bool (int *b, char *str) {
730 if (!strcasecmp (str, "no") ||
731 !strcasecmp (str, "n") ||
732 !strcasecmp (str, "off") ||
733 !strcasecmp (str, "down") ||
734 !strcasecmp (str, "disable"))
740 if (!strcasecmp (str, "yes") ||
741 !strcasecmp (str, "y") ||
742 !strcasecmp (str, "on") ||
743 !strcasecmp (str, "up") ||
744 !strcasecmp (str, "enable"))
753 int Parser_quit(int argc, char **argv)