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 return (cmd->pc_func)(argc, argv);
127 printf("Try interactive use without arguments or use one of:\n");
128 for (cmd = cmds; cmd->pc_name; cmd++)
129 printf("\"%s\"\n", cmd->pc_name);
130 printf("as argument.\n");
135 /* returns the command_t * (NULL if not found) corresponding to a
136 _partial_ match with the first token in name. It sets *next to
137 point to the following token. Does not modify *name. */
138 static command_t * find_cmd(char * name, command_t cmds[], char ** next)
145 /* This sets name to point to the first non-white space character,
146 and next to the first whitespace after name, len to the length: do
148 name = skipwhitespace(name);
149 *next = skiptowhitespace(name);
154 for (i = 0; cmds[i].pc_name; i++) {
155 if (strncasecmp(name, cmds[i].pc_name, len) == 0) {
156 *next = skipwhitespace(*next);
163 /* Recursively process a command line string s and find the command
164 corresponding to it. This can be ambiguous, full, incomplete,
166 static int process(char *s, char ** next, command_t *lookup,
167 command_t **result, char **prev)
169 *result = find_cmd(s, lookup, next);
176 /* found entry: is it ambigous, i.e. not exact command name and
177 more than one command in the list matches. Note that find_cmd
178 points to the first ambiguous entry */
179 if ( strncasecmp(s, (*result)->pc_name, strlen((*result)->pc_name)) &&
180 find_cmd(s, (*result) + 1, next))
183 /* found a unique command: component or full? */
184 if ( (*result)->pc_func ) {
187 if ( *next == '\0' ) {
188 return CMD_INCOMPLETE;
190 return process(*next, next, (*result)->pc_sub_cmd, result, prev);
195 #ifdef HAVE_LIBREADLINE
196 static command_t * match_tbl; /* Command completion against this table */
197 static char * command_generator(const char * text, int state)
203 /* Do we have a match table? */
207 /* If this is the first time called on this word, state is 0 */
210 len = (int)strlen(text);
213 /* Return next name in the command list that paritally matches test */
214 while ( (name = (match_tbl + index)->pc_name) ) {
217 if (strncasecmp(name, text, len) == 0) {
218 return(strdup(name));
222 /* No more matches */
226 /* probably called by readline */
227 static char **command_completion(char * text, int start, int end)
232 match_tbl = top_level;
233 for (table = find_cmd(rl_line_buffer, match_tbl, &pos);
235 table = find_cmd(pos, match_tbl, &pos)) {
237 if (*(pos - 1) == ' ') match_tbl = table->pc_sub_cmd;
240 return(completion_matches(text, command_generator));
244 /* take a string and execute the function or print help */
245 int execute_line(char * line)
247 command_t *cmd, *ambig;
254 switch( process(line, &next, top_level, &cmd, &prev) ) {
256 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
257 while( (ambig = find_cmd(prev, cmd, &tmp)) ) {
258 fprintf(stderr, "%s ", ambig->pc_name);
261 fprintf(stderr, "\n");
264 fprintf(stderr, "No such command, type help\n");
268 "'%s' incomplete command. Use '%s x' where x is one of:\n",
270 fprintf(stderr, "\t");
271 for (i = 0; cmd->pc_sub_cmd[i].pc_name; i++) {
272 fprintf(stderr, "%s ", cmd->pc_sub_cmd[i].pc_name);
274 fprintf(stderr, "\n");
277 i = line2args(line, argv, MAXARGS);
278 rc = (cmd->pc_func)(i, argv);
281 fprintf(stderr, "%s\n", cmd->pc_help);
295 /* just in case you're ever in an airplane and discover you
296 forgot to install readline-dev. :) */
299 int interactive = isatty (fileno (stdin));
301 #ifdef HAVE_LIBREADLINE
303 stifle_history(HISTORY);
307 rl_prep_term_function = (rl_vintfunc_t *)noop_fn;
308 rl_deprep_term_function = (rl_voidfunc_t *)noop_fn;
311 rl_attempted_completion_function = (CPPFunction *)command_completion;
312 rl_completion_entry_function = (void *)command_generator;
317 #ifndef HAVE_LIBREADLINE
318 #define add_history(s)
319 char * readline(char * prompt)
324 printf ("%s", prompt);
325 if (fgets(line, sizeof(line), stdin) == NULL)
328 if (n && line[n-1] == '\n')
334 /* this is the command execution machine */
335 int Parser_commands(void)
338 int rc = 0, save_error = 0;
341 interactive = init_input();
344 line = readline(interactive ? parser_prompt : NULL);
348 s = skipwhitespace(line);
352 rc = execute_line(s);
354 /* stop on error if not-interactive */
355 if (rc != 0 && !interactive) {
370 /* sets the parser prompt */
371 void Parser_init(char * prompt, command_t * cmds)
375 if (parser_prompt) free(parser_prompt);
376 parser_prompt = strdup(prompt);
379 /* frees the parser prompt */
380 void Parser_exit(int argc, char *argv[])
384 parser_prompt = NULL;
387 /* convert a string to an integer */
388 int Parser_int(char *s, int *val)
393 ret = sscanf(s, "%d", val);
394 else if (*(s+1) != 'x')
395 ret = sscanf(s, "%o", val);
398 ret = sscanf(++s, "%x", val);
405 void Parser_qhelp(int argc, char *argv[]) {
407 printf("Available commands are:\n");
409 print_commands(NULL, top_level);
410 printf("For more help type: help command-name\n");
413 int Parser_help(int argc, char **argv)
416 char *next, *prev, *tmp;
417 command_t *result, *ambig;
421 Parser_qhelp(argc, argv);
426 for ( i = 1 ; i < argc ; i++ ) {
427 strcat(line, argv[i]);
430 switch ( process(line, &next, top_level, &result, &prev) ) {
432 fprintf(stderr, "%s: %s\n",line, result->pc_help);
435 fprintf(stderr, "%s: Unknown command.\n", line);
439 "'%s' incomplete command. Use '%s x' where x is one of:\n",
441 fprintf(stderr, "\t");
442 for (i = 0; result->pc_sub_cmd[i].pc_name; i++) {
443 fprintf(stderr, "%s ", result->pc_sub_cmd[i].pc_name);
445 fprintf(stderr, "\n");
448 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
449 while( (ambig = find_cmd(prev, result, &tmp)) ) {
450 fprintf(stderr, "%s ", ambig->pc_name);
453 fprintf(stderr, "\n");
460 void Parser_printhelp(char *cmd)
462 char *argv[] = { "help", cmd };
463 Parser_help(2, argv);
467 /*************************************************************************
469 *************************************************************************/
472 static void print_commands(char * str, command_t * table) {
476 for (cmds = table; cmds->pc_name; cmds++) {
478 if (str) printf("\t%s %s\n", str, cmds->pc_name);
479 else printf("\t%s\n", cmds->pc_name);
481 if (cmds->pc_sub_cmd) {
483 sprintf(buf, "%s %s", str, cmds->pc_name);
484 print_commands(buf, cmds->pc_sub_cmd);
486 print_commands(cmds->pc_name, cmds->pc_sub_cmd);
492 char *Parser_getstr(const char *prompt, const char *deft, char *res,
496 int size = strlen(prompt) + strlen(deft) + 8;
498 theprompt = malloc(size);
501 sprintf(theprompt, "%s [%s]: ", prompt, deft);
503 line = readline(theprompt);
506 if ( line == NULL || *line == '\0' ) {
507 strncpy(res, deft, len);
509 strncpy(res, line, len);
520 /* get integer from prompt, loop forever to get it */
521 int Parser_getint(const char *prompt, long min, long max, long deft, int base)
526 int size = strlen(prompt) + 40;
527 char *theprompt = malloc(size);
529 sprintf(theprompt,"%s [%ld, (0x%lx)]: ", prompt, deft, deft);
535 line = readline(theprompt);
537 fprintf(stdout, "Please enter an integer.\n");
541 if ( *line == '\0' ) {
546 rc = Parser_arg2int(line, &result, base);
549 fprintf(stdout, "Invalid string.\n");
551 } else if ( result > max || result < min ) {
552 fprintf(stdout, "Error: response must lie between %ld and %ld.\n",
566 /* get boolean (starting with YyNn; loop forever */
567 int Parser_getbool(const char *prompt, int deft)
571 int size = strlen(prompt) + 8;
572 char *theprompt = malloc(size);
577 if ( deft != 0 && deft != 1 ) {
578 fprintf(stderr, "Error: Parser_getbool given bad default (%d).\n",
582 sprintf(theprompt, "%s [%s]: ", prompt, (deft==0)? "N" : "Y");
586 line = readline(theprompt);
587 if ( line == NULL ) {
591 if ( *line == '\0' ) {
595 if ( *line == 'y' || *line == 'Y' ) {
599 if ( *line == 'n' || *line == 'N' ) {
605 fprintf(stdout, "Invalid string. Must start with yY or nN\n");
616 /* parse int out of a string or prompt for it */
617 long Parser_intarg(const char *inp, const char *prompt, int deft,
618 int min, int max, int base)
623 rc = Parser_arg2int(inp, &result, base);
628 return Parser_getint(prompt, deft, min, max, base);
632 /* parse int out of a string or prompt for it */
633 char *Parser_strarg(char *inp, const char *prompt, const char *deft,
634 char *answer, int len)
636 if ( inp == NULL || *inp == '\0' ) {
637 return Parser_getstr(prompt, deft, answer, len);
642 /* change a string into a number: return 0 on success. No invalid characters
643 allowed. The processing of base and validity follows strtol(3)*/
644 int Parser_arg2int(const char *inp, long *result, int base)
648 if ( (base !=0) && (base < 2 || base > 36) )
651 *result = strtol(inp, &endptr, base);
653 if ( *inp != '\0' && *endptr == '\0' )
659 /* Convert human readable size string to and int; "1k" -> 1000 */
660 int Parser_size (int *sizep, char *str) {
664 switch (sscanf (str, "%d%1[gGmMkK]", &size, mod)) {
696 /* Convert a string boolean to an int; "enable" -> 1 */
697 int Parser_bool (int *b, char *str) {
698 if (!strcasecmp (str, "no") ||
699 !strcasecmp (str, "n") ||
700 !strcasecmp (str, "off") ||
701 !strcasecmp (str, "disable"))
707 if (!strcasecmp (str, "yes") ||
708 !strcasecmp (str, "y") ||
709 !strcasecmp (str, "on") ||
710 !strcasecmp (str, "enable"))
719 int Parser_quit(int argc, char **argv)