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>
34 static command_t * top_level; /* Top level of commands, initialized by
36 static char * parser_prompt = NULL;/* Parser prompt, set by InitParser */
37 static int done; /* Set to 1 if user types exit or quit */
38 static int ignore_errors; /* Normally, the parser will quit when
39 an error occurs in non-interacive
40 mode. Setting this to non-zero will
41 force it to keep buggering on. */
44 /* static functions */
45 static char *skipwhitespace(char *s);
46 static char *skiptowhitespace(char *s);
47 static command_t *find_cmd(char *name, command_t cmds[], char **next);
48 static int process(char *s, char **next, command_t *lookup, command_t **result,
50 static void print_commands(char *str, command_t *table);
52 static char * skipwhitespace(char * s)
58 for (t = s; t <= s + len && isspace(*t); t++);
63 static char * skiptowhitespace(char * s)
67 for (t = s; *t && !isspace(*t); t++);
71 static int line2args(char *line, char **argv, int maxargs)
76 arg = strtok(line, " \t");
83 while( (arg = strtok(NULL, " \t")) && (i <= maxargs)) {
90 /* find a command -- return it if unique otherwise print alternatives */
91 static command_t *Parser_findargcmd(char *name, command_t cmds[])
95 for (cmd = cmds; cmd->pc_name; cmd++) {
96 if (strcmp(name, cmd->pc_name) == 0)
102 void Parser_ignore_errors(int ignore)
104 ignore_errors = ignore;
107 int Parser_execarg(int argc, char **argv, command_t cmds[])
111 cmd = Parser_findargcmd(argv[0], cmds);
113 int rc = (cmd->pc_func)(argc, argv);
115 fprintf(stderr, "%s\n", cmd->pc_help);
118 printf("Try interactive use without arguments or use one of:\n");
119 for (cmd = cmds; cmd->pc_name; cmd++)
120 printf("\"%s\"\n", cmd->pc_name);
121 printf("as argument.\n");
126 /* returns the command_t * (NULL if not found) corresponding to a
127 _partial_ match with the first token in name. It sets *next to
128 point to the following token. Does not modify *name. */
129 static command_t * find_cmd(char * name, command_t cmds[], char ** next)
136 /* This sets name to point to the first non-white space character,
137 and next to the first whitespace after name, len to the length: do
139 name = skipwhitespace(name);
140 *next = skiptowhitespace(name);
145 for (i = 0; cmds[i].pc_name; i++) {
146 if (strncasecmp(name, cmds[i].pc_name, len) == 0) {
147 *next = skipwhitespace(*next);
154 /* Recursively process a command line string s and find the command
155 corresponding to it. This can be ambiguous, full, incomplete,
157 static int process(char *s, char ** next, command_t *lookup,
158 command_t **result, char **prev)
160 *result = find_cmd(s, lookup, next);
167 /* found entry: is it ambigous, i.e. not exact command name and
168 more than one command in the list matches. Note that find_cmd
169 points to the first ambiguous entry */
170 if (strncasecmp(s, (*result)->pc_name, strlen((*result)->pc_name))) {
172 command_t *another_result = find_cmd(s, (*result) + 1,
174 int found_another = 0;
176 while (another_result) {
177 if (strncasecmp(s, another_result->pc_name,
178 strlen(another_result->pc_name)) == 0){
179 *result = another_result;
180 *next = another_next;
183 another_result = find_cmd(s, another_result + 1,
192 /* found a unique command: component or full? */
193 if ( (*result)->pc_func ) {
196 if ( *next == '\0' ) {
197 return CMD_INCOMPLETE;
199 return process(*next, next, (*result)->pc_sub_cmd,
205 #ifdef HAVE_LIBREADLINE
206 static command_t * match_tbl; /* Command completion against this table */
207 static char * command_generator(const char * text, int state)
213 /* Do we have a match table? */
217 /* If this is the first time called on this word, state is 0 */
220 len = (int)strlen(text);
223 /* Return next name in the command list that paritally matches test */
224 while ( (name = (match_tbl + index)->pc_name) ) {
227 if (strncasecmp(name, text, len) == 0) {
228 return(strdup(name));
232 /* No more matches */
236 /* probably called by readline */
237 static char **command_completion(char * text, int start, int end)
242 match_tbl = top_level;
244 for (table = find_cmd(rl_line_buffer, match_tbl, &pos);
245 table; table = find_cmd(pos, match_tbl, &pos))
248 if (*(pos - 1) == ' ') match_tbl = table->pc_sub_cmd;
251 return completion_matches(text, command_generator);
255 /* take a string and execute the function or print help */
256 int execute_line(char * line)
258 command_t *cmd, *ambig;
265 switch (process(line, &next, top_level, &cmd, &prev)) {
267 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
268 while( (ambig = find_cmd(prev, cmd, &tmp)) ) {
269 fprintf(stderr, "%s ", ambig->pc_name);
272 fprintf(stderr, "\n");
275 fprintf(stderr, "No such command, type help\n");
279 "'%s' incomplete command. Use '%s x' where x is one of:\n",
281 fprintf(stderr, "\t");
282 for (i = 0; cmd->pc_sub_cmd[i].pc_name; i++) {
283 fprintf(stderr, "%s ", cmd->pc_sub_cmd[i].pc_name);
285 fprintf(stderr, "\n");
288 i = line2args(line, argv, MAXARGS);
289 rc = (cmd->pc_func)(i, argv);
292 fprintf(stderr, "%s\n", cmd->pc_help);
306 /* just in case you're ever in an airplane and discover you
307 forgot to install readline-dev. :) */
310 int interactive = isatty (fileno (stdin));
312 #ifdef HAVE_LIBREADLINE
314 stifle_history(HISTORY);
318 rl_prep_term_function = (rl_vintfunc_t *)noop_fn;
319 rl_deprep_term_function = (rl_voidfunc_t *)noop_fn;
322 rl_attempted_completion_function = (CPPFunction *)command_completion;
323 rl_completion_entry_function = (void *)command_generator;
328 #ifndef HAVE_LIBREADLINE
329 #define add_history(s)
330 char * readline(char * prompt)
333 char *line = malloc(size);
341 printf ("%s", prompt);
344 if ((c = fgetc(stdin)) != EOF) {
349 if (ptr - line >= size - 1) {
356 memcpy(tmp, line, ptr - line);
357 ptr = tmp + (ptr - line);
370 if (eof && (strlen(line) == 0)) {
381 /* this is the command execution machine */
382 int Parser_commands(void)
385 int rc = 0, save_error = 0;
388 interactive = init_input();
391 line = readline(interactive ? parser_prompt : NULL);
395 s = skipwhitespace(line);
399 rc = execute_line(s);
401 /* stop on error if not-interactive */
402 if (rc != 0 && !interactive) {
417 /* sets the parser prompt */
418 void Parser_init(char * prompt, command_t * cmds)
422 if (parser_prompt) free(parser_prompt);
423 parser_prompt = strdup(prompt);
426 /* frees the parser prompt */
427 void Parser_exit(int argc, char *argv[])
431 parser_prompt = NULL;
434 /* convert a string to an integer */
435 int Parser_int(char *s, int *val)
440 ret = sscanf(s, "%d", val);
441 else if (*(s+1) != 'x')
442 ret = sscanf(s, "%o", val);
445 ret = sscanf(++s, "%x", val);
452 void Parser_qhelp(int argc, char *argv[]) {
454 printf("Available commands are:\n");
456 print_commands(NULL, top_level);
457 printf("For more help type: help command-name\n");
460 int Parser_help(int argc, char **argv)
463 char *next, *prev, *tmp;
464 command_t *result, *ambig;
468 Parser_qhelp(argc, argv);
473 for ( i = 1 ; i < argc ; i++ ) {
474 strcat(line, argv[i]);
477 switch ( process(line, &next, top_level, &result, &prev) ) {
479 fprintf(stderr, "%s: %s\n",line, result->pc_help);
482 fprintf(stderr, "%s: Unknown command.\n", line);
486 "'%s' incomplete command. Use '%s x' where x is one of:\n",
488 fprintf(stderr, "\t");
489 for (i = 0; result->pc_sub_cmd[i].pc_name; i++) {
490 fprintf(stderr, "%s ", result->pc_sub_cmd[i].pc_name);
492 fprintf(stderr, "\n");
495 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
496 while( (ambig = find_cmd(prev, result, &tmp)) ) {
497 fprintf(stderr, "%s ", ambig->pc_name);
500 fprintf(stderr, "\n");
507 void Parser_printhelp(char *cmd)
509 char *argv[] = { "help", cmd };
510 Parser_help(2, argv);
514 /*************************************************************************
516 *************************************************************************/
517 static void print_commands(char * str, command_t * table) {
521 for (cmds = table; cmds->pc_name; cmds++) {
523 if (str) printf("\t%s %s\n", str, cmds->pc_name);
524 else printf("\t%s\n", cmds->pc_name);
526 if (cmds->pc_sub_cmd) {
528 sprintf(buf, "%s %s", str, cmds->pc_name);
529 print_commands(buf, cmds->pc_sub_cmd);
531 print_commands(cmds->pc_name, cmds->pc_sub_cmd);
537 char *Parser_getstr(const char *prompt, const char *deft, char *res,
541 int size = strlen(prompt) + strlen(deft) + 8;
543 theprompt = malloc(size);
546 sprintf(theprompt, "%s [%s]: ", prompt, deft);
548 line = readline(theprompt);
551 if ( line == NULL || *line == '\0' ) {
552 strncpy(res, deft, len);
554 strncpy(res, line, len);
565 /* get integer from prompt, loop forever to get it */
566 int Parser_getint(const char *prompt, long min, long max, long deft, int base)
571 int size = strlen(prompt) + 40;
572 char *theprompt = malloc(size);
574 sprintf(theprompt,"%s [%ld, (0x%lx)]: ", prompt, deft, deft);
580 line = readline(theprompt);
582 fprintf(stdout, "Please enter an integer.\n");
586 if ( *line == '\0' ) {
591 rc = Parser_arg2int(line, &result, base);
594 fprintf(stdout, "Invalid string.\n");
596 } else if ( result > max || result < min ) {
597 fprintf(stdout, "Error: response must lie between %ld and %ld.\n",
611 /* get boolean (starting with YyNn; loop forever */
612 int Parser_getbool(const char *prompt, int deft)
616 int size = strlen(prompt) + 8;
617 char *theprompt = malloc(size);
622 if ( deft != 0 && deft != 1 ) {
623 fprintf(stderr, "Error: Parser_getbool given bad default %d\n",
627 sprintf(theprompt, "%s [%s]: ", prompt, (deft==0)? "N" : "Y");
631 line = readline(theprompt);
632 if ( line == NULL ) {
636 if ( *line == '\0' ) {
640 if ( *line == 'y' || *line == 'Y' ) {
644 if ( *line == 'n' || *line == 'N' ) {
650 fprintf(stdout, "Invalid string. Must start with yY or nN\n");
661 /* parse int out of a string or prompt for it */
662 long Parser_intarg(const char *inp, const char *prompt, int deft,
663 int min, int max, int base)
668 rc = Parser_arg2int(inp, &result, base);
673 return Parser_getint(prompt, deft, min, max, base);
677 /* parse int out of a string or prompt for it */
678 char *Parser_strarg(char *inp, const char *prompt, const char *deft,
679 char *answer, int len)
681 if ( inp == NULL || *inp == '\0' ) {
682 return Parser_getstr(prompt, deft, answer, len);
687 /* change a string into a number: return 0 on success. No invalid characters
688 allowed. The processing of base and validity follows strtol(3)*/
689 int Parser_arg2int(const char *inp, long *result, int base)
693 if ( (base !=0) && (base < 2 || base > 36) )
696 *result = strtol(inp, &endptr, base);
698 if ( *inp != '\0' && *endptr == '\0' )
704 /* Convert human readable size string to and int; "1k" -> 1000 */
705 int Parser_size (int *sizep, char *str) {
709 switch (sscanf (str, "%d%1[gGmMkK]", &size, mod)) {
741 /* Convert a string boolean to an int; "enable" -> 1 */
742 int Parser_bool (int *b, char *str) {
743 if (!strcasecmp (str, "no") ||
744 !strcasecmp (str, "n") ||
745 !strcasecmp (str, "off") ||
746 !strcasecmp (str, "down") ||
747 !strcasecmp (str, "disable"))
753 if (!strcasecmp (str, "yes") ||
754 !strcasecmp (str, "y") ||
755 !strcasecmp (str, "on") ||
756 !strcasecmp (str, "up") ||
757 !strcasecmp (str, "enable"))
766 int Parser_quit(int argc, char **argv)