1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.sf.net/projects/lustre/
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <sys/param.h>
31 #include <libcfs/libcfsutil.h>
33 static command_t * top_level; /* Top level of commands, initialized by
35 static char * parser_prompt = NULL;/* Parser prompt, set by InitParser */
36 static int done; /* Set to 1 if user types exit or quit */
37 static int ignore_errors; /* Normally, the parser will quit when
38 an error occurs in non-interacive
39 mode. Setting this to non-zero will
40 force it to keep buggering on. */
43 /* static functions */
44 static char *skipwhitespace(char *s);
45 static char *skiptowhitespace(char *s);
46 static command_t *find_cmd(char *name, command_t cmds[], char **next);
47 static int process(char *s, char **next, command_t *lookup, command_t **result,
49 static void print_commands(char *str, command_t *table);
51 static char * skipwhitespace(char * s)
57 for (t = s; t <= s + len && isspace(*t); t++);
62 static char * skiptowhitespace(char * s)
66 for (t = s; *t && !isspace(*t); t++);
70 static int line2args(char *line, char **argv, int maxargs)
75 arg = strtok(line, " \t");
82 while( (arg = strtok(NULL, " \t")) && (i <= maxargs)) {
89 /* find a command -- return it if unique otherwise print alternatives */
90 static command_t *Parser_findargcmd(char *name, command_t cmds[])
94 for (cmd = cmds; cmd->pc_name; cmd++) {
95 if (strcmp(name, cmd->pc_name) == 0)
101 void Parser_ignore_errors(int ignore)
103 ignore_errors = ignore;
106 int Parser_execarg(int argc, char **argv, command_t cmds[])
110 cmd = Parser_findargcmd(argv[0], cmds);
112 int rc = (cmd->pc_func)(argc, argv);
114 fprintf(stderr, "%s\n", cmd->pc_help);
117 printf("Try interactive use without arguments or use one of:\n");
118 for (cmd = cmds; cmd->pc_name; cmd++)
119 printf("\"%s\"\n", cmd->pc_name);
120 printf("as argument.\n");
125 /* returns the command_t * (NULL if not found) corresponding to a
126 _partial_ match with the first token in name. It sets *next to
127 point to the following token. Does not modify *name. */
128 static command_t * find_cmd(char * name, command_t cmds[], char ** next)
135 /* This sets name to point to the first non-white space character,
136 and next to the first whitespace after name, len to the length: do
138 name = skipwhitespace(name);
139 *next = skiptowhitespace(name);
144 for (i = 0; cmds[i].pc_name; i++) {
145 if (strncasecmp(name, cmds[i].pc_name, len) == 0) {
146 *next = skipwhitespace(*next);
153 /* Recursively process a command line string s and find the command
154 corresponding to it. This can be ambiguous, full, incomplete,
156 static int process(char *s, char ** next, command_t *lookup,
157 command_t **result, char **prev)
159 *result = find_cmd(s, lookup, next);
166 /* found entry: is it ambigous, i.e. not exact command name and
167 more than one command in the list matches. Note that find_cmd
168 points to the first ambiguous entry */
169 if (strncasecmp(s, (*result)->pc_name, strlen((*result)->pc_name))) {
171 command_t *another_result = find_cmd(s, (*result) + 1,
173 int found_another = 0;
175 while (another_result) {
176 if (strncasecmp(s, another_result->pc_name,
177 strlen(another_result->pc_name)) == 0){
178 *result = another_result;
179 *next = another_next;
182 another_result = find_cmd(s, another_result + 1,
191 /* found a unique command: component or full? */
192 if ( (*result)->pc_func ) {
195 if ( *next == '\0' ) {
196 return CMD_INCOMPLETE;
198 return process(*next, next, (*result)->pc_sub_cmd,
204 #ifdef HAVE_LIBREADLINE
205 static command_t * match_tbl; /* Command completion against this table */
206 static char * command_generator(const char * text, int state)
212 /* Do we have a match table? */
216 /* If this is the first time called on this word, state is 0 */
219 len = (int)strlen(text);
222 /* Return next name in the command list that paritally matches test */
223 while ( (name = (match_tbl + index)->pc_name) ) {
226 if (strncasecmp(name, text, len) == 0) {
227 return(strdup(name));
231 /* No more matches */
235 /* probably called by readline */
236 static char **command_completion(char * text, int start, int end)
241 match_tbl = top_level;
243 for (table = find_cmd(rl_line_buffer, match_tbl, &pos);
244 table; table = find_cmd(pos, match_tbl, &pos))
247 if (*(pos - 1) == ' ') match_tbl = table->pc_sub_cmd;
250 return completion_matches(text, command_generator);
254 /* take a string and execute the function or print help */
255 int execute_line(char * line)
257 command_t *cmd, *ambig;
264 switch (process(line, &next, top_level, &cmd, &prev)) {
266 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
267 while( (ambig = find_cmd(prev, cmd, &tmp)) ) {
268 fprintf(stderr, "%s ", ambig->pc_name);
271 fprintf(stderr, "\n");
274 fprintf(stderr, "No such command, type help\n");
278 "'%s' incomplete command. Use '%s x' where x is one of:\n",
280 fprintf(stderr, "\t");
281 for (i = 0; cmd->pc_sub_cmd[i].pc_name; i++) {
282 fprintf(stderr, "%s ", cmd->pc_sub_cmd[i].pc_name);
284 fprintf(stderr, "\n");
287 i = line2args(line, argv, MAXARGS);
288 rc = (cmd->pc_func)(i, argv);
291 fprintf(stderr, "%s\n", cmd->pc_help);
305 /* just in case you're ever in an airplane and discover you
306 forgot to install readline-dev. :) */
309 int interactive = isatty (fileno (stdin));
311 #ifdef HAVE_LIBREADLINE
313 stifle_history(HISTORY);
317 rl_prep_term_function = (rl_vintfunc_t *)noop_fn;
318 rl_deprep_term_function = (rl_voidfunc_t *)noop_fn;
321 rl_attempted_completion_function = (CPPFunction *)command_completion;
322 rl_completion_entry_function = (void *)command_generator;
327 #ifndef HAVE_LIBREADLINE
328 #define add_history(s)
329 char * readline(char * prompt)
332 char *line = malloc(size);
340 printf ("%s", prompt);
343 if ((c = fgetc(stdin)) != EOF) {
348 if (ptr - line >= size - 1) {
355 memcpy(tmp, line, ptr - line);
356 ptr = tmp + (ptr - line);
362 if (ferror(stdin) || feof(stdin))
369 if (eof && (strlen(line) == 0)) {
380 /* this is the command execution machine */
381 int Parser_commands(void)
384 int rc = 0, save_error = 0;
387 interactive = init_input();
390 line = readline(interactive ? parser_prompt : NULL);
394 s = skipwhitespace(line);
398 rc = execute_line(s);
400 /* stop on error if not-interactive */
401 if (rc != 0 && !interactive) {
416 /* sets the parser prompt */
417 void Parser_init(char * prompt, command_t * cmds)
421 if (parser_prompt) free(parser_prompt);
422 parser_prompt = strdup(prompt);
425 /* frees the parser prompt */
426 void Parser_exit(int argc, char *argv[])
430 parser_prompt = NULL;
433 /* convert a string to an integer */
434 int Parser_int(char *s, int *val)
439 ret = sscanf(s, "%d", val);
440 else if (*(s+1) != 'x')
441 ret = sscanf(s, "%o", val);
444 ret = sscanf(++s, "%x", val);
451 void Parser_qhelp(int argc, char *argv[]) {
453 printf("Available commands are:\n");
455 print_commands(NULL, top_level);
456 printf("For more help type: help command-name\n");
459 int Parser_help(int argc, char **argv)
462 char *next, *prev, *tmp;
463 command_t *result, *ambig;
467 Parser_qhelp(argc, argv);
472 for ( i = 1 ; i < argc ; i++ ) {
473 strcat(line, argv[i]);
476 switch ( process(line, &next, top_level, &result, &prev) ) {
478 fprintf(stderr, "%s: %s\n",line, result->pc_help);
481 fprintf(stderr, "%s: Unknown command.\n", line);
485 "'%s' incomplete command. Use '%s x' where x is one of:\n",
487 fprintf(stderr, "\t");
488 for (i = 0; result->pc_sub_cmd[i].pc_name; i++) {
489 fprintf(stderr, "%s ", result->pc_sub_cmd[i].pc_name);
491 fprintf(stderr, "\n");
494 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
495 while( (ambig = find_cmd(prev, result, &tmp)) ) {
496 fprintf(stderr, "%s ", ambig->pc_name);
499 fprintf(stderr, "\n");
506 void Parser_printhelp(char *cmd)
508 char *argv[] = { "help", cmd };
509 Parser_help(2, argv);
513 /*************************************************************************
515 *************************************************************************/
516 static void print_commands(char * str, command_t * table) {
520 for (cmds = table; cmds->pc_name; cmds++) {
522 if (str) printf("\t%s %s\n", str, cmds->pc_name);
523 else printf("\t%s\n", cmds->pc_name);
525 if (cmds->pc_sub_cmd) {
527 sprintf(buf, "%s %s", str, cmds->pc_name);
528 print_commands(buf, cmds->pc_sub_cmd);
530 print_commands(cmds->pc_name, cmds->pc_sub_cmd);
536 char *Parser_getstr(const char *prompt, const char *deft, char *res,
540 int size = strlen(prompt) + strlen(deft) + 8;
542 theprompt = malloc(size);
545 sprintf(theprompt, "%s [%s]: ", prompt, deft);
547 line = readline(theprompt);
550 if ( line == NULL || *line == '\0' ) {
551 strncpy(res, deft, len);
553 strncpy(res, line, len);
564 /* get integer from prompt, loop forever to get it */
565 int Parser_getint(const char *prompt, long min, long max, long deft, int base)
570 int size = strlen(prompt) + 40;
571 char *theprompt = malloc(size);
573 sprintf(theprompt,"%s [%ld, (0x%lx)]: ", prompt, deft, deft);
579 line = readline(theprompt);
581 fprintf(stdout, "Please enter an integer.\n");
585 if ( *line == '\0' ) {
590 rc = Parser_arg2int(line, &result, base);
593 fprintf(stdout, "Invalid string.\n");
595 } else if ( result > max || result < min ) {
596 fprintf(stdout, "Error: response must lie between %ld and %ld.\n",
610 /* get boolean (starting with YyNn; loop forever */
611 int Parser_getbool(const char *prompt, int deft)
615 int size = strlen(prompt) + 8;
616 char *theprompt = malloc(size);
621 if ( deft != 0 && deft != 1 ) {
622 fprintf(stderr, "Error: Parser_getbool given bad default %d\n",
626 sprintf(theprompt, "%s [%s]: ", prompt, (deft==0)? "N" : "Y");
630 line = readline(theprompt);
631 if ( line == NULL ) {
635 if ( *line == '\0' ) {
639 if ( *line == 'y' || *line == 'Y' ) {
643 if ( *line == 'n' || *line == 'N' ) {
649 fprintf(stdout, "Invalid string. Must start with yY or nN\n");
660 /* parse int out of a string or prompt for it */
661 long Parser_intarg(const char *inp, const char *prompt, int deft,
662 int min, int max, int base)
667 rc = Parser_arg2int(inp, &result, base);
672 return Parser_getint(prompt, deft, min, max, base);
676 /* parse int out of a string or prompt for it */
677 char *Parser_strarg(char *inp, const char *prompt, const char *deft,
678 char *answer, int len)
680 if ( inp == NULL || *inp == '\0' ) {
681 return Parser_getstr(prompt, deft, answer, len);
686 /* change a string into a number: return 0 on success. No invalid characters
687 allowed. The processing of base and validity follows strtol(3)*/
688 int Parser_arg2int(const char *inp, long *result, int base)
692 if ( (base !=0) && (base < 2 || base > 36) )
695 *result = strtol(inp, &endptr, base);
697 if ( *inp != '\0' && *endptr == '\0' )
703 /* Convert human readable size string to and int; "1k" -> 1000 */
704 int Parser_size (int *sizep, char *str) {
708 switch (sscanf (str, "%d%1[gGmMkK]", &size, mod)) {
740 /* Convert a string boolean to an int; "enable" -> 1 */
741 int Parser_bool (int *b, char *str) {
742 if (!strcasecmp (str, "no") ||
743 !strcasecmp (str, "n") ||
744 !strcasecmp (str, "off") ||
745 !strcasecmp (str, "down") ||
746 !strcasecmp (str, "disable"))
752 if (!strcasecmp (str, "yes") ||
753 !strcasecmp (str, "y") ||
754 !strcasecmp (str, "on") ||
755 !strcasecmp (str, "up") ||
756 !strcasecmp (str, "enable"))
765 int Parser_quit(int argc, char **argv)