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 #ifdef HAVE_LIBREADLINE
32 #define READLINE_LIBRARY
33 #include <readline/readline.h>
35 /* completion_matches() is #if 0-ed out in modern glibc */
36 #ifndef completion_matches
37 #define completion_matches rl_completion_matches
39 extern void using_history(void);
40 extern void stifle_history(int);
41 extern void add_history(char *);
46 static command_t * top_level; /* Top level of commands, initialized by
48 static char * parser_prompt = NULL;/* Parser prompt, set by InitParser */
49 static int done; /* Set to 1 if user types exit or quit */
50 static int ignore_errors; /* Normally, the parser will quit when
51 an error occurs in non-interacive
52 mode. Setting this to non-zero will
53 force it to keep buggering on. */
56 /* static functions */
57 static char *skipwhitespace(char *s);
58 static char *skiptowhitespace(char *s);
59 static command_t *find_cmd(char *name, command_t cmds[], char **next);
60 static int process(char *s, char **next, command_t *lookup, command_t **result,
62 static void print_commands(char *str, command_t *table);
64 static char * skipwhitespace(char * s)
70 for (t = s; t <= s + len && isspace(*t); t++);
75 static char * skiptowhitespace(char * s)
79 for (t = s; *t && !isspace(*t); t++);
83 static int line2args(char *line, char **argv, int maxargs)
88 arg = strtok(line, " \t");
95 while( (arg = strtok(NULL, " \t")) && (i <= maxargs)) {
102 /* find a command -- return it if unique otherwise print alternatives */
103 static command_t *Parser_findargcmd(char *name, command_t cmds[])
107 for (cmd = cmds; cmd->pc_name; cmd++) {
108 if (strcmp(name, cmd->pc_name) == 0)
114 void Parser_ignore_errors(int ignore)
116 ignore_errors = ignore;
119 int Parser_execarg(int argc, char **argv, command_t cmds[])
123 cmd = Parser_findargcmd(argv[0], cmds);
125 int rc = (cmd->pc_func)(argc, argv);
127 fprintf(stderr, "%s\n", cmd->pc_help);
130 printf("Try interactive use without arguments or use one of:\n");
131 for (cmd = cmds; cmd->pc_name; cmd++)
132 printf("\"%s\"\n", cmd->pc_name);
133 printf("as argument.\n");
138 /* returns the command_t * (NULL if not found) corresponding to a
139 _partial_ match with the first token in name. It sets *next to
140 point to the following token. Does not modify *name. */
141 static command_t * find_cmd(char * name, command_t cmds[], char ** next)
148 /* This sets name to point to the first non-white space character,
149 and next to the first whitespace after name, len to the length: do
151 name = skipwhitespace(name);
152 *next = skiptowhitespace(name);
157 for (i = 0; cmds[i].pc_name; i++) {
158 if (strncasecmp(name, cmds[i].pc_name, len) == 0) {
159 *next = skipwhitespace(*next);
166 /* Recursively process a command line string s and find the command
167 corresponding to it. This can be ambiguous, full, incomplete,
169 static int process(char *s, char ** next, command_t *lookup,
170 command_t **result, char **prev)
172 *result = find_cmd(s, lookup, next);
179 /* found entry: is it ambigous, i.e. not exact command name and
180 more than one command in the list matches. Note that find_cmd
181 points to the first ambiguous entry */
182 if (strncasecmp(s, (*result)->pc_name, strlen((*result)->pc_name))) {
184 command_t *another_result = find_cmd(s, (*result) + 1,
186 int found_another = 0;
188 while (another_result) {
189 if (strncasecmp(s, another_result->pc_name,
190 strlen(another_result->pc_name)) == 0){
191 *result = another_result;
192 *next = another_next;
195 another_result = find_cmd(s, another_result + 1,
204 /* found a unique command: component or full? */
205 if ( (*result)->pc_func ) {
208 if ( *next == '\0' ) {
209 return CMD_INCOMPLETE;
211 return process(*next, next, (*result)->pc_sub_cmd,
217 #ifdef HAVE_LIBREADLINE
218 static command_t * match_tbl; /* Command completion against this table */
219 static char * command_generator(const char * text, int state)
225 /* Do we have a match table? */
229 /* If this is the first time called on this word, state is 0 */
232 len = (int)strlen(text);
235 /* Return next name in the command list that paritally matches test */
236 while ( (name = (match_tbl + index)->pc_name) ) {
239 if (strncasecmp(name, text, len) == 0) {
240 return(strdup(name));
244 /* No more matches */
248 /* probably called by readline */
249 static char **command_completion(char * text, int start, int end)
254 match_tbl = top_level;
255 for (table = find_cmd(rl_line_buffer, match_tbl, &pos);
257 table = find_cmd(pos, match_tbl, &pos)) {
259 if (*(pos - 1) == ' ') match_tbl = table->pc_sub_cmd;
262 return(completion_matches(text, command_generator));
266 /* take a string and execute the function or print help */
267 int execute_line(char * line)
269 command_t *cmd, *ambig;
276 switch (process(line, &next, top_level, &cmd, &prev)) {
278 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
279 while( (ambig = find_cmd(prev, cmd, &tmp)) ) {
280 fprintf(stderr, "%s ", ambig->pc_name);
283 fprintf(stderr, "\n");
286 fprintf(stderr, "No such command, type help\n");
290 "'%s' incomplete command. Use '%s x' where x is one of:\n",
292 fprintf(stderr, "\t");
293 for (i = 0; cmd->pc_sub_cmd[i].pc_name; i++) {
294 fprintf(stderr, "%s ", cmd->pc_sub_cmd[i].pc_name);
296 fprintf(stderr, "\n");
299 i = line2args(line, argv, MAXARGS);
300 rc = (cmd->pc_func)(i, argv);
303 fprintf(stderr, "%s\n", cmd->pc_help);
317 /* just in case you're ever in an airplane and discover you
318 forgot to install readline-dev. :) */
321 int interactive = isatty (fileno (stdin));
323 #ifdef HAVE_LIBREADLINE
325 stifle_history(HISTORY);
329 rl_prep_term_function = (rl_vintfunc_t *)noop_fn;
330 rl_deprep_term_function = (rl_voidfunc_t *)noop_fn;
333 rl_attempted_completion_function = (CPPFunction *)command_completion;
334 rl_completion_entry_function = (void *)command_generator;
339 #ifndef HAVE_LIBREADLINE
340 #define add_history(s)
341 char * readline(char * prompt)
346 printf ("%s", prompt);
347 if (fgets(line, sizeof(line), stdin) == NULL)
350 if (n && line[n-1] == '\n')
356 /* this is the command execution machine */
357 int Parser_commands(void)
360 int rc = 0, save_error = 0;
363 interactive = init_input();
366 line = readline(interactive ? parser_prompt : NULL);
370 s = skipwhitespace(line);
374 rc = execute_line(s);
376 /* stop on error if not-interactive */
377 if (rc != 0 && !interactive) {
392 /* sets the parser prompt */
393 void Parser_init(char * prompt, command_t * cmds)
397 if (parser_prompt) free(parser_prompt);
398 parser_prompt = strdup(prompt);
401 /* frees the parser prompt */
402 void Parser_exit(int argc, char *argv[])
406 parser_prompt = NULL;
409 /* convert a string to an integer */
410 int Parser_int(char *s, int *val)
415 ret = sscanf(s, "%d", val);
416 else if (*(s+1) != 'x')
417 ret = sscanf(s, "%o", val);
420 ret = sscanf(++s, "%x", val);
427 void Parser_qhelp(int argc, char *argv[]) {
429 printf("Available commands are:\n");
431 print_commands(NULL, top_level);
432 printf("For more help type: help command-name\n");
435 int Parser_help(int argc, char **argv)
438 char *next, *prev, *tmp;
439 command_t *result, *ambig;
443 Parser_qhelp(argc, argv);
448 for ( i = 1 ; i < argc ; i++ ) {
449 strcat(line, argv[i]);
452 switch ( process(line, &next, top_level, &result, &prev) ) {
454 fprintf(stderr, "%s: %s\n",line, result->pc_help);
457 fprintf(stderr, "%s: Unknown command.\n", line);
461 "'%s' incomplete command. Use '%s x' where x is one of:\n",
463 fprintf(stderr, "\t");
464 for (i = 0; result->pc_sub_cmd[i].pc_name; i++) {
465 fprintf(stderr, "%s ", result->pc_sub_cmd[i].pc_name);
467 fprintf(stderr, "\n");
470 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
471 while( (ambig = find_cmd(prev, result, &tmp)) ) {
472 fprintf(stderr, "%s ", ambig->pc_name);
475 fprintf(stderr, "\n");
482 void Parser_printhelp(char *cmd)
484 char *argv[] = { "help", cmd };
485 Parser_help(2, argv);
489 /*************************************************************************
491 *************************************************************************/
492 static void print_commands(char * str, command_t * table) {
496 for (cmds = table; cmds->pc_name; cmds++) {
498 if (str) printf("\t%s %s\n", str, cmds->pc_name);
499 else printf("\t%s\n", cmds->pc_name);
501 if (cmds->pc_sub_cmd) {
503 sprintf(buf, "%s %s", str, cmds->pc_name);
504 print_commands(buf, cmds->pc_sub_cmd);
506 print_commands(cmds->pc_name, cmds->pc_sub_cmd);
512 char *Parser_getstr(const char *prompt, const char *deft, char *res,
516 int size = strlen(prompt) + strlen(deft) + 8;
518 theprompt = malloc(size);
521 sprintf(theprompt, "%s [%s]: ", prompt, deft);
523 line = readline(theprompt);
526 if ( line == NULL || *line == '\0' ) {
527 strncpy(res, deft, len);
529 strncpy(res, line, len);
540 /* get integer from prompt, loop forever to get it */
541 int Parser_getint(const char *prompt, long min, long max, long deft, int base)
546 int size = strlen(prompt) + 40;
547 char *theprompt = malloc(size);
549 sprintf(theprompt,"%s [%ld, (0x%lx)]: ", prompt, deft, deft);
555 line = readline(theprompt);
557 fprintf(stdout, "Please enter an integer.\n");
561 if ( *line == '\0' ) {
566 rc = Parser_arg2int(line, &result, base);
569 fprintf(stdout, "Invalid string.\n");
571 } else if ( result > max || result < min ) {
572 fprintf(stdout, "Error: response must lie between %ld and %ld.\n",
586 /* get boolean (starting with YyNn; loop forever */
587 int Parser_getbool(const char *prompt, int deft)
591 int size = strlen(prompt) + 8;
592 char *theprompt = malloc(size);
597 if ( deft != 0 && deft != 1 ) {
598 fprintf(stderr, "Error: Parser_getbool given bad default %d\n",
602 sprintf(theprompt, "%s [%s]: ", prompt, (deft==0)? "N" : "Y");
606 line = readline(theprompt);
607 if ( line == NULL ) {
611 if ( *line == '\0' ) {
615 if ( *line == 'y' || *line == 'Y' ) {
619 if ( *line == 'n' || *line == 'N' ) {
625 fprintf(stdout, "Invalid string. Must start with yY or nN\n");
636 /* parse int out of a string or prompt for it */
637 long Parser_intarg(const char *inp, const char *prompt, int deft,
638 int min, int max, int base)
643 rc = Parser_arg2int(inp, &result, base);
648 return Parser_getint(prompt, deft, min, max, base);
652 /* parse int out of a string or prompt for it */
653 char *Parser_strarg(char *inp, const char *prompt, const char *deft,
654 char *answer, int len)
656 if ( inp == NULL || *inp == '\0' ) {
657 return Parser_getstr(prompt, deft, answer, len);
662 /* change a string into a number: return 0 on success. No invalid characters
663 allowed. The processing of base and validity follows strtol(3)*/
664 int Parser_arg2int(const char *inp, long *result, int base)
668 if ( (base !=0) && (base < 2 || base > 36) )
671 *result = strtol(inp, &endptr, base);
673 if ( *inp != '\0' && *endptr == '\0' )
679 /* Convert human readable size string to and int; "1k" -> 1000 */
680 int Parser_size (int *sizep, char *str) {
684 switch (sscanf (str, "%d%1[gGmMkK]", &size, mod)) {
716 /* Convert a string boolean to an int; "enable" -> 1 */
717 int Parser_bool (int *b, char *str) {
718 if (!strcasecmp (str, "no") ||
719 !strcasecmp (str, "n") ||
720 !strcasecmp (str, "off") ||
721 !strcasecmp (str, "down") ||
722 !strcasecmp (str, "disable"))
728 if (!strcasecmp (str, "yes") ||
729 !strcasecmp (str, "y") ||
730 !strcasecmp (str, "on") ||
731 !strcasecmp (str, "up") ||
732 !strcasecmp (str, "enable"))
741 int Parser_quit(int argc, char **argv)