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 *);
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 */
49 /* static functions */
50 static char *skipwhitespace(char *s);
51 static char *skiptowhitespace(char *s);
52 static command_t *find_cmd(char *name, command_t cmds[], char **next);
53 static int process(char *s, char **next, command_t *lookup, command_t **result,
55 static void print_commands(char *str, command_t *table);
57 static char * skipwhitespace(char * s)
63 for (t = s; t <= s + len && isspace(*t); t++);
68 static char * skiptowhitespace(char * s)
72 for (t = s; *t && !isspace(*t); t++);
76 static int line2args(char *line, char **argv, int maxargs)
81 arg = strtok(line, " \t");
88 while( (arg = strtok(NULL, " \t")) && (i <= maxargs)) {
95 /* find a command -- return it if unique otherwise print alternatives */
96 static command_t *Parser_findargcmd(char *name, command_t cmds[])
100 for (cmd = cmds; cmd->pc_name; cmd++) {
101 if (strcmp(name, cmd->pc_name) == 0)
107 int Parser_execarg(int argc, char **argv, command_t cmds[])
111 cmd = Parser_findargcmd(argv[0], cmds);
113 return (cmd->pc_func)(argc, argv);
115 printf("Try interactive use without arguments or use one of:\n");
116 for (cmd = cmds; cmd->pc_name; cmd++)
117 printf("\"%s\" ", cmd->pc_name);
118 printf("\nas argument.\n");
123 /* returns the command_t * (NULL if not found) corresponding to a
124 _partial_ match with the first token in name. It sets *next to
125 point to the following token. Does not modify *name. */
126 static command_t * find_cmd(char * name, command_t cmds[], char ** next)
133 /* This sets name to point to the first non-white space character,
134 and next to the first whitespace after name, len to the length: do
136 name = skipwhitespace(name);
137 *next = skiptowhitespace(name);
142 for (i = 0; cmds[i].pc_name; i++) {
143 if (strncasecmp(name, cmds[i].pc_name, len) == 0) {
144 *next = skipwhitespace(*next);
151 /* Recursively process a command line string s and find the command
152 corresponding to it. This can be ambiguous, full, incomplete,
154 static int process(char *s, char ** next, command_t *lookup,
155 command_t **result, char **prev)
157 *result = find_cmd(s, lookup, next);
164 /* found entry: is it ambigous, i.e. not exact command name and
165 more than one command in the list matches. Note that find_cmd
166 points to the first ambiguous entry */
167 if ( strncasecmp(s, (*result)->pc_name, strlen((*result)->pc_name)) &&
168 find_cmd(s, (*result) + 1, next))
171 /* found a unique command: component or full? */
172 if ( (*result)->pc_func ) {
175 if ( *next == '\0' ) {
176 return CMD_INCOMPLETE;
178 return process(*next, next, (*result)->pc_sub_cmd, result, prev);
183 #ifdef HAVE_LIBREADLINE
184 static command_t * match_tbl; /* Command completion against this table */
185 static char * command_generator(const char * text, int state)
191 /* Do we have a match table? */
195 /* If this is the first time called on this word, state is 0 */
198 len = (int)strlen(text);
201 /* Return next name in the command list that paritally matches test */
202 while ( (name = (match_tbl + index)->pc_name) ) {
205 if (strncasecmp(name, text, len) == 0) {
206 return(strdup(name));
210 /* No more matches */
214 /* probably called by readline */
215 static char **command_completion(char * text, int start, int end)
220 match_tbl = top_level;
221 for (table = find_cmd(rl_line_buffer, match_tbl, &pos);
223 table = find_cmd(pos, match_tbl, &pos)) {
225 if (*(pos - 1) == ' ') match_tbl = table->pc_sub_cmd;
228 return(completion_matches(text, command_generator));
232 /* take a string and execute the function or print help */
233 int execute_line(char * line)
235 command_t *cmd, *ambig;
242 switch( process(line, &next, top_level, &cmd, &prev) ) {
244 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
245 while( (ambig = find_cmd(prev, cmd, &tmp)) ) {
246 fprintf(stderr, "%s ", ambig->pc_name);
249 fprintf(stderr, "\n");
252 fprintf(stderr, "No such command, type help\n");
256 "'%s' incomplete command. Use '%s x' where x is one of:\n",
258 fprintf(stderr, "\t");
259 for (i = 0; cmd->pc_sub_cmd[i].pc_name; i++) {
260 fprintf(stderr, "%s ", cmd->pc_sub_cmd[i].pc_name);
262 fprintf(stderr, "\n");
265 i = line2args(line, argv, MAXARGS);
266 rc = (cmd->pc_func)(i, argv);
269 fprintf(stderr, "%s\n", cmd->pc_help);
283 /* just in case you're ever in an airplane and discover you
284 forgot to install readline-dev. :) */
287 int interactive = isatty (fileno (stdin));
289 #ifdef HAVE_LIBREADLINE
291 stifle_history(HISTORY);
295 rl_prep_term_function = (rl_vintfunc_t *)noop_fn;
296 rl_deprep_term_function = (rl_voidfunc_t *)noop_fn;
299 rl_attempted_completion_function = (CPPFunction *)command_completion;
300 rl_completion_entry_function = (void *)command_generator;
305 #ifndef HAVE_LIBREADLINE
306 #define add_history(s)
307 char * readline(char * prompt)
312 printf ("%s", prompt);
313 if (fgets(line, sizeof(line), stdin) == NULL)
316 if (n && line[n-1] == '\n')
322 /* this is the command execution machine */
323 int Parser_commands(void)
329 interactive = init_input();
332 line = readline(interactive ? parser_prompt : NULL);
336 s = skipwhitespace(line);
340 rc = execute_line(s);
349 /* sets the parser prompt */
350 void Parser_init(char * prompt, command_t * cmds)
354 if (parser_prompt) free(parser_prompt);
355 parser_prompt = strdup(prompt);
358 /* frees the parser prompt */
359 void Parser_exit(int argc, char *argv[])
363 parser_prompt = NULL;
366 /* convert a string to an integer */
367 int Parser_int(char *s, int *val)
372 ret = sscanf(s, "%d", val);
373 else if (*(s+1) != 'x')
374 ret = sscanf(s, "%o", val);
377 ret = sscanf(++s, "%x", val);
384 void Parser_qhelp(int argc, char *argv[]) {
386 printf("Available commands are:\n");
388 print_commands(NULL, top_level);
389 printf("For more help type: help command-name\n");
392 int Parser_help(int argc, char **argv)
395 char *next, *prev, *tmp;
396 command_t *result, *ambig;
400 Parser_qhelp(argc, argv);
405 for ( i = 1 ; i < argc ; i++ ) {
406 strcat(line, argv[i]);
409 switch ( process(line, &next, top_level, &result, &prev) ) {
411 fprintf(stderr, "%s: %s\n",line, result->pc_help);
414 fprintf(stderr, "%s: Unknown command.\n", line);
418 "'%s' incomplete command. Use '%s x' where x is one of:\n",
420 fprintf(stderr, "\t");
421 for (i = 0; result->pc_sub_cmd[i].pc_name; i++) {
422 fprintf(stderr, "%s ", result->pc_sub_cmd[i].pc_name);
424 fprintf(stderr, "\n");
427 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
428 while( (ambig = find_cmd(prev, result, &tmp)) ) {
429 fprintf(stderr, "%s ", ambig->pc_name);
432 fprintf(stderr, "\n");
439 void Parser_printhelp(char *cmd)
441 char *argv[] = { "help", cmd };
442 Parser_help(2, argv);
445 /*************************************************************************
447 *************************************************************************/
450 static void print_commands(char * str, command_t * table) {
454 for (cmds = table; cmds->pc_name; cmds++) {
456 if (str) printf("\t%s %s\n", str, cmds->pc_name);
457 else printf("\t%s\n", cmds->pc_name);
459 if (cmds->pc_sub_cmd) {
461 sprintf(buf, "%s %s", str, cmds->pc_name);
462 print_commands(buf, cmds->pc_sub_cmd);
464 print_commands(cmds->pc_name, cmds->pc_sub_cmd);
470 char *Parser_getstr(const char *prompt, const char *deft, char *res,
474 int size = strlen(prompt) + strlen(deft) + 8;
476 theprompt = malloc(size);
479 sprintf(theprompt, "%s [%s]: ", prompt, deft);
481 line = readline(theprompt);
484 if ( line == NULL || *line == '\0' ) {
485 strncpy(res, deft, len);
487 strncpy(res, line, len);
498 /* get integer from prompt, loop forever to get it */
499 int Parser_getint(const char *prompt, long min, long max, long deft, int base)
504 int size = strlen(prompt) + 40;
505 char *theprompt = malloc(size);
507 sprintf(theprompt,"%s [%ld, (0x%lx)]: ", prompt, deft, deft);
513 line = readline(theprompt);
515 fprintf(stdout, "Please enter an integer.\n");
519 if ( *line == '\0' ) {
524 rc = Parser_arg2int(line, &result, base);
527 fprintf(stdout, "Invalid string.\n");
529 } else if ( result > max || result < min ) {
530 fprintf(stdout, "Error: response must lie between %ld and %ld.\n",
544 /* get boolean (starting with YyNn; loop forever */
545 int Parser_getbool(const char *prompt, int deft)
549 int size = strlen(prompt) + 8;
550 char *theprompt = malloc(size);
555 if ( deft != 0 && deft != 1 ) {
556 fprintf(stderr, "Error: Parser_getbool given bad default (%d).\n",
560 sprintf(theprompt, "%s [%s]: ", prompt, (deft==0)? "N" : "Y");
564 line = readline(theprompt);
565 if ( line == NULL ) {
569 if ( *line == '\0' ) {
573 if ( *line == 'y' || *line == 'Y' ) {
577 if ( *line == 'n' || *line == 'N' ) {
583 fprintf(stdout, "Invalid string. Must start with yY or nN\n");
594 /* parse int out of a string or prompt for it */
595 long Parser_intarg(const char *inp, const char *prompt, int deft,
596 int min, int max, int base)
601 rc = Parser_arg2int(inp, &result, base);
606 return Parser_getint(prompt, deft, min, max, base);
610 /* parse int out of a string or prompt for it */
611 char *Parser_strarg(char *inp, const char *prompt, const char *deft,
612 char *answer, int len)
614 if ( inp == NULL || *inp == '\0' ) {
615 return Parser_getstr(prompt, deft, answer, len);
620 /* change a string into a number: return 0 on success. No invalid characters
621 allowed. The processing of base and validity follows strtol(3)*/
622 int Parser_arg2int(const char *inp, long *result, int base)
626 if ( (base !=0) && (base < 2 || base > 36) )
629 *result = strtol(inp, &endptr, base);
631 if ( *inp != '\0' && *endptr == '\0' )
637 /* Convert human readable size string to and int; "1k" -> 1000 */
638 int Parser_size (int *sizep, char *str) {
642 switch (sscanf (str, "%d%1[gGmMkK]", &size, mod)) {
674 /* Convert a string boolean to an int; "enable" -> 1 */
675 int Parser_bool (int *b, char *str) {
676 if (!strcasecmp (str, "no") ||
677 !strcasecmp (str, "n") ||
678 !strcasecmp (str, "off") ||
679 !strcasecmp (str, "disable"))
685 if (!strcasecmp (str, "yes") ||
686 !strcasecmp (str, "y") ||
687 !strcasecmp (str, "on") ||
688 !strcasecmp (str, "enable"))
697 int Parser_quit(int argc, char **argv)