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 //extern char **completion_matches __P((char *, rl_compentry_func_t *));
36 extern void using_history(void);
37 extern void stifle_history(int);
38 extern void add_history(char *);
43 static command_t * top_level; /* Top level of commands, initialized by
45 static char * parser_prompt = NULL;/* Parser prompt, set by InitParser */
46 static int done; /* Set to 1 if user types exit or quit */
47 static int ignore_errors; /* Normally, the parser will quit when
48 an error occurs in non-interacive
49 mode. Setting this to non-zero will
50 force it to keep buggering on. */
53 /* static functions */
54 static char *skipwhitespace(char *s);
55 static char *skiptowhitespace(char *s);
56 static command_t *find_cmd(char *name, command_t cmds[], char **next);
57 static int process(char *s, char **next, command_t *lookup, command_t **result,
59 static void print_commands(char *str, command_t *table);
61 static char * skipwhitespace(char * s)
67 for (t = s; t <= s + len && isspace(*t); t++);
72 static char * skiptowhitespace(char * s)
76 for (t = s; *t && !isspace(*t); t++);
80 static int line2args(char *line, char **argv, int maxargs)
85 arg = strtok(line, " \t");
92 while( (arg = strtok(NULL, " \t")) && (i <= maxargs)) {
99 /* find a command -- return it if unique otherwise print alternatives */
100 static command_t *Parser_findargcmd(char *name, command_t cmds[])
104 for (cmd = cmds; cmd->pc_name; cmd++) {
105 if (strcmp(name, cmd->pc_name) == 0)
111 void Parser_ignore_errors(int ignore)
113 ignore_errors = ignore;
116 int Parser_execarg(int argc, char **argv, command_t cmds[])
120 cmd = Parser_findargcmd(argv[0], cmds);
122 return (cmd->pc_func)(argc, argv);
124 printf("Try interactive use without arguments or use one of:\n");
125 for (cmd = cmds; cmd->pc_name; cmd++)
126 printf("\"%s\"\n", cmd->pc_name);
127 printf("as argument.\n");
132 /* returns the command_t * (NULL if not found) corresponding to a
133 _partial_ match with the first token in name. It sets *next to
134 point to the following token. Does not modify *name. */
135 static command_t * find_cmd(char * name, command_t cmds[], char ** next)
142 /* This sets name to point to the first non-white space character,
143 and next to the first whitespace after name, len to the length: do
145 name = skipwhitespace(name);
146 *next = skiptowhitespace(name);
151 for (i = 0; cmds[i].pc_name; i++) {
152 if (strncasecmp(name, cmds[i].pc_name, len) == 0) {
153 *next = skipwhitespace(*next);
160 /* Recursively process a command line string s and find the command
161 corresponding to it. This can be ambiguous, full, incomplete,
163 static int process(char *s, char ** next, command_t *lookup,
164 command_t **result, char **prev)
166 *result = find_cmd(s, lookup, next);
173 /* found entry: is it ambigous, i.e. not exact command name and
174 more than one command in the list matches. Note that find_cmd
175 points to the first ambiguous entry */
176 if ( strncasecmp(s, (*result)->pc_name, strlen((*result)->pc_name)) &&
177 find_cmd(s, (*result) + 1, next))
180 /* found a unique command: component or full? */
181 if ( (*result)->pc_func ) {
184 if ( *next == '\0' ) {
185 return CMD_INCOMPLETE;
187 return process(*next, next, (*result)->pc_sub_cmd, result, prev);
192 #ifdef HAVE_LIBREADLINE
193 static command_t * match_tbl; /* Command completion against this table */
194 static char * command_generator(const char * text, int state)
200 /* Do we have a match table? */
204 /* If this is the first time called on this word, state is 0 */
207 len = (int)strlen(text);
210 /* Return next name in the command list that paritally matches test */
211 while ( (name = (match_tbl + index)->pc_name) ) {
214 if (strncasecmp(name, text, len) == 0) {
215 return(strdup(name));
219 /* No more matches */
223 /* probably called by readline */
224 static char **command_completion(char * text, int start, int end)
229 match_tbl = top_level;
230 for (table = find_cmd(rl_line_buffer, match_tbl, &pos);
232 table = find_cmd(pos, match_tbl, &pos)) {
234 if (*(pos - 1) == ' ') match_tbl = table->pc_sub_cmd;
237 return(completion_matches(text, command_generator));
241 /* take a string and execute the function or print help */
242 int execute_line(char * line)
244 command_t *cmd, *ambig;
251 switch( process(line, &next, top_level, &cmd, &prev) ) {
253 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
254 while( (ambig = find_cmd(prev, cmd, &tmp)) ) {
255 fprintf(stderr, "%s ", ambig->pc_name);
258 fprintf(stderr, "\n");
261 fprintf(stderr, "No such command, type help\n");
265 "'%s' incomplete command. Use '%s x' where x is one of:\n",
267 fprintf(stderr, "\t");
268 for (i = 0; cmd->pc_sub_cmd[i].pc_name; i++) {
269 fprintf(stderr, "%s ", cmd->pc_sub_cmd[i].pc_name);
271 fprintf(stderr, "\n");
274 i = line2args(line, argv, MAXARGS);
275 rc = (cmd->pc_func)(i, argv);
278 fprintf(stderr, "%s\n", cmd->pc_help);
292 /* just in case you're ever in an airplane and discover you
293 forgot to install readline-dev. :) */
296 int interactive = isatty (fileno (stdin));
298 #ifdef HAVE_LIBREADLINE
300 stifle_history(HISTORY);
304 rl_prep_term_function = (rl_vintfunc_t *)noop_fn;
305 rl_deprep_term_function = (rl_voidfunc_t *)noop_fn;
308 rl_attempted_completion_function = (CPPFunction *)command_completion;
309 rl_completion_entry_function = (void *)command_generator;
314 #ifndef HAVE_LIBREADLINE
315 #define add_history(s)
316 char * readline(char * prompt)
321 printf ("%s", prompt);
322 if (fgets(line, sizeof(line), stdin) == NULL)
325 if (n && line[n-1] == '\n')
331 /* this is the command execution machine */
332 int Parser_commands(void)
335 int rc = 0, save_error = 0;
338 interactive = init_input();
341 line = readline(interactive ? parser_prompt : NULL);
345 s = skipwhitespace(line);
349 rc = execute_line(s);
351 /* stop on error if not-interactive */
352 if (rc != 0 && !interactive) {
367 /* sets the parser prompt */
368 void Parser_init(char * prompt, command_t * cmds)
372 if (parser_prompt) free(parser_prompt);
373 parser_prompt = strdup(prompt);
376 /* frees the parser prompt */
377 void Parser_exit(int argc, char *argv[])
381 parser_prompt = NULL;
384 /* convert a string to an integer */
385 int Parser_int(char *s, int *val)
390 ret = sscanf(s, "%d", val);
391 else if (*(s+1) != 'x')
392 ret = sscanf(s, "%o", val);
395 ret = sscanf(++s, "%x", val);
402 void Parser_qhelp(int argc, char *argv[]) {
404 printf("Available commands are:\n");
406 print_commands(NULL, top_level);
407 printf("For more help type: help command-name\n");
410 int Parser_help(int argc, char **argv)
413 char *next, *prev, *tmp;
414 command_t *result, *ambig;
418 Parser_qhelp(argc, argv);
423 for ( i = 1 ; i < argc ; i++ ) {
424 strcat(line, argv[i]);
427 switch ( process(line, &next, top_level, &result, &prev) ) {
429 fprintf(stderr, "%s: %s\n",line, result->pc_help);
432 fprintf(stderr, "%s: Unknown command.\n", line);
436 "'%s' incomplete command. Use '%s x' where x is one of:\n",
438 fprintf(stderr, "\t");
439 for (i = 0; result->pc_sub_cmd[i].pc_name; i++) {
440 fprintf(stderr, "%s ", result->pc_sub_cmd[i].pc_name);
442 fprintf(stderr, "\n");
445 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
446 while( (ambig = find_cmd(prev, result, &tmp)) ) {
447 fprintf(stderr, "%s ", ambig->pc_name);
450 fprintf(stderr, "\n");
457 void Parser_printhelp(char *cmd)
459 char *argv[] = { "help", cmd };
460 Parser_help(2, argv);
464 /*************************************************************************
466 *************************************************************************/
469 static void print_commands(char * str, command_t * table) {
473 for (cmds = table; cmds->pc_name; cmds++) {
475 if (str) printf("\t%s %s\n", str, cmds->pc_name);
476 else printf("\t%s\n", cmds->pc_name);
478 if (cmds->pc_sub_cmd) {
480 sprintf(buf, "%s %s", str, cmds->pc_name);
481 print_commands(buf, cmds->pc_sub_cmd);
483 print_commands(cmds->pc_name, cmds->pc_sub_cmd);
489 char *Parser_getstr(const char *prompt, const char *deft, char *res,
493 int size = strlen(prompt) + strlen(deft) + 8;
495 theprompt = malloc(size);
498 sprintf(theprompt, "%s [%s]: ", prompt, deft);
500 line = readline(theprompt);
503 if ( line == NULL || *line == '\0' ) {
504 strncpy(res, deft, len);
506 strncpy(res, line, len);
517 /* get integer from prompt, loop forever to get it */
518 int Parser_getint(const char *prompt, long min, long max, long deft, int base)
523 int size = strlen(prompt) + 40;
524 char *theprompt = malloc(size);
526 sprintf(theprompt,"%s [%ld, (0x%lx)]: ", prompt, deft, deft);
532 line = readline(theprompt);
534 fprintf(stdout, "Please enter an integer.\n");
538 if ( *line == '\0' ) {
543 rc = Parser_arg2int(line, &result, base);
546 fprintf(stdout, "Invalid string.\n");
548 } else if ( result > max || result < min ) {
549 fprintf(stdout, "Error: response must lie between %ld and %ld.\n",
563 /* get boolean (starting with YyNn; loop forever */
564 int Parser_getbool(const char *prompt, int deft)
568 int size = strlen(prompt) + 8;
569 char *theprompt = malloc(size);
574 if ( deft != 0 && deft != 1 ) {
575 fprintf(stderr, "Error: Parser_getbool given bad default (%d).\n",
579 sprintf(theprompt, "%s [%s]: ", prompt, (deft==0)? "N" : "Y");
583 line = readline(theprompt);
584 if ( line == NULL ) {
588 if ( *line == '\0' ) {
592 if ( *line == 'y' || *line == 'Y' ) {
596 if ( *line == 'n' || *line == 'N' ) {
602 fprintf(stdout, "Invalid string. Must start with yY or nN\n");
613 /* parse int out of a string or prompt for it */
614 long Parser_intarg(const char *inp, const char *prompt, int deft,
615 int min, int max, int base)
620 rc = Parser_arg2int(inp, &result, base);
625 return Parser_getint(prompt, deft, min, max, base);
629 /* parse int out of a string or prompt for it */
630 char *Parser_strarg(char *inp, const char *prompt, const char *deft,
631 char *answer, int len)
633 if ( inp == NULL || *inp == '\0' ) {
634 return Parser_getstr(prompt, deft, answer, len);
639 /* change a string into a number: return 0 on success. No invalid characters
640 allowed. The processing of base and validity follows strtol(3)*/
641 int Parser_arg2int(const char *inp, long *result, int base)
645 if ( (base !=0) && (base < 2 || base > 36) )
648 *result = strtol(inp, &endptr, base);
650 if ( *inp != '\0' && *endptr == '\0' )
656 /* Convert human readable size string to and int; "1k" -> 1000 */
657 int Parser_size (int *sizep, char *str) {
661 switch (sscanf (str, "%d%1[gGmMkK]", &size, mod)) {
693 /* Convert a string boolean to an int; "enable" -> 1 */
694 int Parser_bool (int *b, char *str) {
695 if (!strcasecmp (str, "no") ||
696 !strcasecmp (str, "n") ||
697 !strcasecmp (str, "off") ||
698 !strcasecmp (str, "disable"))
704 if (!strcasecmp (str, "yes") ||
705 !strcasecmp (str, "y") ||
706 !strcasecmp (str, "on") ||
707 !strcasecmp (str, "enable"))
716 int Parser_quit(int argc, char **argv)