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;
256 for (table = find_cmd(rl_line_buffer, match_tbl, &pos);
257 table; table = find_cmd(pos, match_tbl, &pos))
260 if (*(pos - 1) == ' ') match_tbl = table->pc_sub_cmd;
263 return completion_matches(text, command_generator);
267 /* take a string and execute the function or print help */
268 int execute_line(char * line)
270 command_t *cmd, *ambig;
277 switch (process(line, &next, top_level, &cmd, &prev)) {
279 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
280 while( (ambig = find_cmd(prev, cmd, &tmp)) ) {
281 fprintf(stderr, "%s ", ambig->pc_name);
284 fprintf(stderr, "\n");
287 fprintf(stderr, "No such command, type help\n");
291 "'%s' incomplete command. Use '%s x' where x is one of:\n",
293 fprintf(stderr, "\t");
294 for (i = 0; cmd->pc_sub_cmd[i].pc_name; i++) {
295 fprintf(stderr, "%s ", cmd->pc_sub_cmd[i].pc_name);
297 fprintf(stderr, "\n");
300 i = line2args(line, argv, MAXARGS);
301 rc = (cmd->pc_func)(i, argv);
304 fprintf(stderr, "%s\n", cmd->pc_help);
318 /* just in case you're ever in an airplane and discover you
319 forgot to install readline-dev. :) */
322 int interactive = isatty (fileno (stdin));
324 #ifdef HAVE_LIBREADLINE
326 stifle_history(HISTORY);
330 rl_prep_term_function = (rl_vintfunc_t *)noop_fn;
331 rl_deprep_term_function = (rl_voidfunc_t *)noop_fn;
334 rl_attempted_completion_function = (CPPFunction *)command_completion;
335 rl_completion_entry_function = (void *)command_generator;
340 #ifndef HAVE_LIBREADLINE
341 #define add_history(s)
342 char * readline(char * prompt)
345 char *line = malloc(size);
352 printf ("%s", prompt);
355 if ((c = fgetc(stdin)) != EOF) {
360 if (ptr - line >= size - 1) {
367 memcpy(tmp, line, ptr - line);
368 ptr = tmp + (ptr - line);
387 /* this is the command execution machine */
388 int Parser_commands(void)
391 int rc = 0, save_error = 0;
394 interactive = init_input();
397 line = readline(interactive ? parser_prompt : NULL);
401 s = skipwhitespace(line);
405 rc = execute_line(s);
407 /* stop on error if not-interactive */
408 if (rc != 0 && !interactive) {
423 /* sets the parser prompt */
424 void Parser_init(char * prompt, command_t * cmds)
428 if (parser_prompt) free(parser_prompt);
429 parser_prompt = strdup(prompt);
432 /* frees the parser prompt */
433 void Parser_exit(int argc, char *argv[])
437 parser_prompt = NULL;
440 /* convert a string to an integer */
441 int Parser_int(char *s, int *val)
446 ret = sscanf(s, "%d", val);
447 else if (*(s+1) != 'x')
448 ret = sscanf(s, "%o", val);
451 ret = sscanf(++s, "%x", val);
458 void Parser_qhelp(int argc, char *argv[]) {
460 printf("Available commands are:\n");
462 print_commands(NULL, top_level);
463 printf("For more help type: help command-name\n");
466 int Parser_help(int argc, char **argv)
469 char *next, *prev, *tmp;
470 command_t *result, *ambig;
474 Parser_qhelp(argc, argv);
479 for ( i = 1 ; i < argc ; i++ ) {
480 strcat(line, argv[i]);
483 switch ( process(line, &next, top_level, &result, &prev) ) {
485 fprintf(stderr, "%s: %s\n",line, result->pc_help);
488 fprintf(stderr, "%s: Unknown command.\n", line);
492 "'%s' incomplete command. Use '%s x' where x is one of:\n",
494 fprintf(stderr, "\t");
495 for (i = 0; result->pc_sub_cmd[i].pc_name; i++) {
496 fprintf(stderr, "%s ", result->pc_sub_cmd[i].pc_name);
498 fprintf(stderr, "\n");
501 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
502 while( (ambig = find_cmd(prev, result, &tmp)) ) {
503 fprintf(stderr, "%s ", ambig->pc_name);
506 fprintf(stderr, "\n");
513 void Parser_printhelp(char *cmd)
515 char *argv[] = { "help", cmd };
516 Parser_help(2, argv);
520 /*************************************************************************
522 *************************************************************************/
523 static void print_commands(char * str, command_t * table) {
527 for (cmds = table; cmds->pc_name; cmds++) {
529 if (str) printf("\t%s %s\n", str, cmds->pc_name);
530 else printf("\t%s\n", cmds->pc_name);
532 if (cmds->pc_sub_cmd) {
534 sprintf(buf, "%s %s", str, cmds->pc_name);
535 print_commands(buf, cmds->pc_sub_cmd);
537 print_commands(cmds->pc_name, cmds->pc_sub_cmd);
543 char *Parser_getstr(const char *prompt, const char *deft, char *res,
547 int size = strlen(prompt) + strlen(deft) + 8;
549 theprompt = malloc(size);
552 sprintf(theprompt, "%s [%s]: ", prompt, deft);
554 line = readline(theprompt);
557 if ( line == NULL || *line == '\0' ) {
558 strncpy(res, deft, len);
560 strncpy(res, line, len);
571 /* get integer from prompt, loop forever to get it */
572 int Parser_getint(const char *prompt, long min, long max, long deft, int base)
577 int size = strlen(prompt) + 40;
578 char *theprompt = malloc(size);
580 sprintf(theprompt,"%s [%ld, (0x%lx)]: ", prompt, deft, deft);
586 line = readline(theprompt);
588 fprintf(stdout, "Please enter an integer.\n");
592 if ( *line == '\0' ) {
597 rc = Parser_arg2int(line, &result, base);
600 fprintf(stdout, "Invalid string.\n");
602 } else if ( result > max || result < min ) {
603 fprintf(stdout, "Error: response must lie between %ld and %ld.\n",
617 /* get boolean (starting with YyNn; loop forever */
618 int Parser_getbool(const char *prompt, int deft)
622 int size = strlen(prompt) + 8;
623 char *theprompt = malloc(size);
628 if ( deft != 0 && deft != 1 ) {
629 fprintf(stderr, "Error: Parser_getbool given bad default %d\n",
633 sprintf(theprompt, "%s [%s]: ", prompt, (deft==0)? "N" : "Y");
637 line = readline(theprompt);
638 if ( line == NULL ) {
642 if ( *line == '\0' ) {
646 if ( *line == 'y' || *line == 'Y' ) {
650 if ( *line == 'n' || *line == 'N' ) {
656 fprintf(stdout, "Invalid string. Must start with yY or nN\n");
667 /* parse int out of a string or prompt for it */
668 long Parser_intarg(const char *inp, const char *prompt, int deft,
669 int min, int max, int base)
674 rc = Parser_arg2int(inp, &result, base);
679 return Parser_getint(prompt, deft, min, max, base);
683 /* parse int out of a string or prompt for it */
684 char *Parser_strarg(char *inp, const char *prompt, const char *deft,
685 char *answer, int len)
687 if ( inp == NULL || *inp == '\0' ) {
688 return Parser_getstr(prompt, deft, answer, len);
693 /* change a string into a number: return 0 on success. No invalid characters
694 allowed. The processing of base and validity follows strtol(3)*/
695 int Parser_arg2int(const char *inp, long *result, int base)
699 if ( (base !=0) && (base < 2 || base > 36) )
702 *result = strtol(inp, &endptr, base);
704 if ( *inp != '\0' && *endptr == '\0' )
710 /* Convert human readable size string to and int; "1k" -> 1000 */
711 int Parser_size (int *sizep, char *str) {
715 switch (sscanf (str, "%d%1[gGmMkK]", &size, mod)) {
747 /* Convert a string boolean to an int; "enable" -> 1 */
748 int Parser_bool (int *b, char *str) {
749 if (!strcasecmp (str, "no") ||
750 !strcasecmp (str, "n") ||
751 !strcasecmp (str, "off") ||
752 !strcasecmp (str, "down") ||
753 !strcasecmp (str, "disable"))
759 if (!strcasecmp (str, "yes") ||
760 !strcasecmp (str, "y") ||
761 !strcasecmp (str, "on") ||
762 !strcasecmp (str, "up") ||
763 !strcasecmp (str, "enable"))
772 int Parser_quit(int argc, char **argv)