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>
32 #ifdef HAVE_LIBREADLINE
33 #define READLINE_LIBRARY
34 #include <readline/readline.h>
36 //extern char **completion_matches __P((char *, rl_compentry_func_t *));
37 extern void using_history(void);
38 extern void stifle_history(int);
39 extern void add_history(char *);
44 static command_t * top_level; /* Top level of commands, initialized by
46 static char * parser_prompt = NULL;/* Parser prompt, set by InitParser */
47 static int done; /* Set to 1 if user types exit or quit */
50 /* static functions */
51 static char *skipwhitespace(char *s);
52 static char *skiptowhitespace(char *s);
53 static command_t *find_cmd(char *name, command_t cmds[], char **next);
54 static int process(char *s, char **next, command_t *lookup, command_t **result,
56 static void print_commands(char *str, command_t *table);
58 static char * skipwhitespace(char * s)
64 for (t = s; t <= s + len && isspace(*t); t++);
69 static char * skiptowhitespace(char * s)
73 for (t = s; *t && !isspace(*t); t++);
77 static int line2args(char *line, char **argv, int maxargs)
82 arg = strtok(line, " \t");
89 while( (arg = strtok(NULL, " \t")) && (i <= maxargs)) {
96 /* find a command -- return it if unique otherwise print alternatives */
97 static command_t *Parser_findargcmd(char *name, command_t cmds[])
101 for (cmd = cmds; cmd->pc_name; cmd++) {
102 if (strcmp(name, cmd->pc_name) == 0)
108 int Parser_execarg(int argc, char **argv, command_t cmds[])
112 cmd = Parser_findargcmd(argv[0], cmds);
114 return (cmd->pc_func)(argc, argv);
116 printf("Try interactive use without arguments or use one of:\n");
117 for (cmd = cmds; cmd->pc_name; cmd++)
118 printf("\"%s\" ", cmd->pc_name);
119 printf("\nas argument.\n");
124 /* returns the command_t * (NULL if not found) corresponding to a
125 _partial_ match with the first token in name. It sets *next to
126 point to the following token. Does not modify *name. */
127 static command_t * find_cmd(char * name, command_t cmds[], char ** next)
134 /* This sets name to point to the first non-white space character,
135 and next to the first whitespace after name, len to the length: do
137 name = skipwhitespace(name);
138 *next = skiptowhitespace(name);
143 for (i = 0; cmds[i].pc_name; i++) {
144 if (strncasecmp(name, cmds[i].pc_name, len) == 0) {
145 *next = skipwhitespace(*next);
152 /* Recursively process a command line string s and find the command
153 corresponding to it. This can be ambiguous, full, incomplete,
155 static int process(char *s, char ** next, command_t *lookup,
156 command_t **result, char **prev)
158 *result = find_cmd(s, lookup, next);
165 /* found entry: is it ambigous, i.e. not exact command name and
166 more than one command in the list matches. Note that find_cmd
167 points to the first ambiguous entry */
168 if ( strncasecmp(s, (*result)->pc_name, strlen((*result)->pc_name)) &&
169 find_cmd(s, (*result) + 1, next))
172 /* found a unique command: component or full? */
173 if ( (*result)->pc_func ) {
176 if ( *next == '\0' ) {
177 return CMD_INCOMPLETE;
179 return process(*next, next, (*result)->pc_sub_cmd, result, prev);
184 #ifdef HAVE_LIBREADLINE
185 static command_t * match_tbl; /* Command completion against this table */
186 static char * command_generator(const char * text, int state)
192 /* Do we have a match table? */
196 /* If this is the first time called on this word, state is 0 */
199 len = (int)strlen(text);
202 /* Return next name in the command list that paritally matches test */
203 while ( (name = (match_tbl + index)->pc_name) ) {
206 if (strncasecmp(name, text, len) == 0) {
207 return(strdup(name));
211 /* No more matches */
215 /* probably called by readline */
216 static char **command_completion(char * text, int start, int end)
221 match_tbl = top_level;
222 for (table = find_cmd(rl_line_buffer, match_tbl, &pos);
224 table = find_cmd(pos, match_tbl, &pos)) {
226 if (*(pos - 1) == ' ') match_tbl = table->pc_sub_cmd;
229 return(completion_matches(text, command_generator));
233 /* take a string and execute the function or print help */
234 int execute_line(char * line)
236 command_t *cmd, *ambig;
243 switch( process(line, &next, top_level, &cmd, &prev) ) {
245 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
246 while( (ambig = find_cmd(prev, cmd, &tmp)) ) {
247 fprintf(stderr, "%s ", ambig->pc_name);
250 fprintf(stderr, "\n");
253 fprintf(stderr, "No such command, type help\n");
257 "'%s' incomplete command. Use '%s x' where x is one of:\n",
259 fprintf(stderr, "\t");
260 for (i = 0; cmd->pc_sub_cmd[i].pc_name; i++) {
261 fprintf(stderr, "%s ", cmd->pc_sub_cmd[i].pc_name);
263 fprintf(stderr, "\n");
266 i = line2args(line, argv, MAXARGS);
267 rc = (cmd->pc_func)(i, argv);
270 fprintf(stderr, "%s\n", cmd->pc_help);
284 /* just in case you're ever in an airplane and discover you
285 forgot to install readline-dev. :) */
288 int interactive = isatty (fileno (stdin));
290 #ifdef HAVE_LIBREADLINE
292 stifle_history(HISTORY);
296 rl_prep_term_function = (rl_vintfunc_t *)noop_fn;
297 rl_deprep_term_function = (rl_voidfunc_t *)noop_fn;
300 rl_attempted_completion_function = (CPPFunction *)command_completion;
301 rl_completion_entry_function = (void *)command_generator;
306 #ifndef HAVE_LIBREADLINE
307 #define add_history(s)
308 char * readline(char * prompt)
313 printf ("%s", prompt);
314 if (fgets(line, sizeof(line), stdin) == NULL)
317 if (n && line[n-1] == '\n')
323 /* this is the command execution machine */
324 int Parser_commands(void)
330 interactive = init_input();
333 line = readline(interactive ? parser_prompt : NULL);
337 s = skipwhitespace(line);
341 rc = execute_line(s);
350 /* sets the parser prompt */
351 void Parser_init(char * prompt, command_t * cmds)
355 if (parser_prompt) free(parser_prompt);
356 parser_prompt = strdup(prompt);
359 /* frees the parser prompt */
360 void Parser_exit(int argc, char *argv[])
364 parser_prompt = NULL;
367 /* convert a string to an integer */
368 int Parser_int(char *s, int *val)
373 ret = sscanf(s, "%d", val);
374 else if (*(s+1) != 'x')
375 ret = sscanf(s, "%o", val);
378 ret = sscanf(++s, "%x", val);
385 void Parser_qhelp(int argc, char *argv[]) {
387 printf("Available commands are:\n");
389 print_commands(NULL, top_level);
390 printf("For more help type: help command-name\n");
393 int Parser_help(int argc, char **argv)
396 char *next, *prev, *tmp;
397 command_t *result, *ambig;
401 Parser_qhelp(argc, argv);
406 for ( i = 1 ; i < argc ; i++ ) {
407 strcat(line, argv[i]);
410 switch ( process(line, &next, top_level, &result, &prev) ) {
412 fprintf(stderr, "%s: %s\n",line, result->pc_help);
415 fprintf(stderr, "%s: Unknown command.\n", line);
419 "'%s' incomplete command. Use '%s x' where x is one of:\n",
421 fprintf(stderr, "\t");
422 for (i = 0; result->pc_sub_cmd[i].pc_name; i++) {
423 fprintf(stderr, "%s ", result->pc_sub_cmd[i].pc_name);
425 fprintf(stderr, "\n");
428 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
429 while( (ambig = find_cmd(prev, result, &tmp)) ) {
430 fprintf(stderr, "%s ", ambig->pc_name);
433 fprintf(stderr, "\n");
440 void Parser_printhelp(char *cmd)
442 char *argv[] = { "help", cmd };
443 Parser_help(2, argv);
447 /*************************************************************************
449 *************************************************************************/
452 static void print_commands(char * str, command_t * table) {
456 for (cmds = table; cmds->pc_name; cmds++) {
458 if (str) printf("\t%s %s\n", str, cmds->pc_name);
459 else printf("\t%s\n", cmds->pc_name);
461 if (cmds->pc_sub_cmd) {
463 sprintf(buf, "%s %s", str, cmds->pc_name);
464 print_commands(buf, cmds->pc_sub_cmd);
466 print_commands(cmds->pc_name, cmds->pc_sub_cmd);
472 char *Parser_getstr(const char *prompt, const char *deft, char *res,
476 int size = strlen(prompt) + strlen(deft) + 8;
478 theprompt = malloc(size);
481 sprintf(theprompt, "%s [%s]: ", prompt, deft);
483 line = readline(theprompt);
486 if ( line == NULL || *line == '\0' ) {
487 strncpy(res, deft, len);
489 strncpy(res, line, len);
500 /* get integer from prompt, loop forever to get it */
501 int Parser_getint(const char *prompt, long min, long max, long deft, int base)
506 int size = strlen(prompt) + 40;
507 char *theprompt = malloc(size);
509 sprintf(theprompt,"%s [%ld, (0x%lx)]: ", prompt, deft, deft);
515 line = readline(theprompt);
517 fprintf(stdout, "Please enter an integer.\n");
521 if ( *line == '\0' ) {
526 rc = Parser_arg2int(line, &result, base);
529 fprintf(stdout, "Invalid string.\n");
531 } else if ( result > max || result < min ) {
532 fprintf(stdout, "Error: response must lie between %ld and %ld.\n",
546 /* get boolean (starting with YyNn; loop forever */
547 int Parser_getbool(const char *prompt, int deft)
551 int size = strlen(prompt) + 8;
552 char *theprompt = malloc(size);
557 if ( deft != 0 && deft != 1 ) {
558 fprintf(stderr, "Error: Parser_getbool given bad default (%d).\n",
562 sprintf(theprompt, "%s [%s]: ", prompt, (deft==0)? "N" : "Y");
566 line = readline(theprompt);
567 if ( line == NULL ) {
571 if ( *line == '\0' ) {
575 if ( *line == 'y' || *line == 'Y' ) {
579 if ( *line == 'n' || *line == 'N' ) {
585 fprintf(stdout, "Invalid string. Must start with yY or nN\n");
596 /* parse int out of a string or prompt for it */
597 long Parser_intarg(const char *inp, const char *prompt, int deft,
598 int min, int max, int base)
603 rc = Parser_arg2int(inp, &result, base);
608 return Parser_getint(prompt, deft, min, max, base);
612 /* parse int out of a string or prompt for it */
613 char *Parser_strarg(char *inp, const char *prompt, const char *deft,
614 char *answer, int len)
616 if ( inp == NULL || *inp == '\0' ) {
617 return Parser_getstr(prompt, deft, answer, len);
622 /* change a string into a number: return 0 on success. No invalid characters
623 allowed. The processing of base and validity follows strtol(3)*/
624 int Parser_arg2int(const char *inp, long *result, int base)
628 if ( (base !=0) && (base < 2 || base > 36) )
631 *result = strtol(inp, &endptr, base);
633 if ( *inp != '\0' && *endptr == '\0' )
639 /* Convert human readable size string to and int; "1k" -> 1000 */
640 int Parser_size (int *sizep, char *str) {
644 switch (sscanf (str, "%d%1[gGmMkK]", &size, mod)) {
676 /* Convert a string boolean to an int; "enable" -> 1 */
677 int Parser_bool (int *b, char *str) {
678 if (!strcasecmp (str, "no") ||
679 !strcasecmp (str, "n") ||
680 !strcasecmp (str, "off") ||
681 !strcasecmp (str, "disable"))
687 if (!strcasecmp (str, "yes") ||
688 !strcasecmp (str, "y") ||
689 !strcasecmp (str, "on") ||
690 !strcasecmp (str, "enable"))
699 int Parser_quit(int argc, char **argv)