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 command_t * match_tbl; /* Command completion against this table */
47 static char * parser_prompt = NULL;/* Parser prompt, set by InitParser */
48 static int done; /* Set to 1 if user types exit or quit */
51 /* static functions */
52 static char *skipwhitespace(char *s);
53 static char *skiptowhitespace(char *s);
54 static command_t *find_cmd(char *name, command_t cmds[], char **next);
55 static int process(char *s, char **next, command_t *lookup, command_t **result, char **prev);
56 static char *command_generator(const char *text, int state);
57 static char **command_completion(char *text, int start, int end);
58 static void print_commands(char *str, command_t *table);
60 static char * skipwhitespace(char * s)
66 for (t = s; t <= s + len && isspace(*t); t++);
71 static char * skiptowhitespace(char * s)
75 for (t = s; *t && !isspace(*t); t++);
79 static int line2args(char *line, char **argv, int maxargs)
84 arg = strtok(line, " \t");
91 while( (arg = strtok(NULL, " \t")) && (i <= maxargs)) {
98 /* find a command -- return it if unique otherwise print alternatives */
99 static command_t *Parser_findargcmd(char *name, command_t cmds[])
103 for (cmd = cmds; cmd->pc_name; cmd++) {
104 if (strcmp(name, cmd->pc_name) == 0)
110 int Parser_execarg(int argc, char **argv, command_t cmds[])
114 cmd = Parser_findargcmd(argv[0], cmds);
116 return (cmd->pc_func)(argc, argv);
118 printf("Try interactive use without arguments or use one of:\n");
119 for (cmd = cmds; cmd->pc_name; cmd++)
120 printf("\"%s\" ", cmd->pc_name);
121 printf("\nas argument.\n");
126 /* returns the command_t * (NULL if not found) corresponding to a
127 _partial_ match with the first token in name. It sets *next to
128 point to the following token. Does not modify *name. */
129 static command_t * find_cmd(char * name, command_t cmds[], char ** next)
136 /* This sets name to point to the first non-white space character,
137 and next to the first whitespace after name, len to the length: do
139 name = skipwhitespace(name);
140 *next = skiptowhitespace(name);
145 for (i = 0; cmds[i].pc_name; i++) {
146 if (strncasecmp(name, cmds[i].pc_name, len) == 0) {
147 *next = skipwhitespace(*next);
154 /* Recursively process a command line string s and find the command
155 corresponding to it. This can be ambiguous, full, incomplete,
157 static int process(char *s, char ** next, command_t *lookup,
158 command_t **result, char **prev)
160 *result = find_cmd(s, lookup, next);
167 /* found entry: is it ambigous, i.e. not exact command name and
168 more than one command in the list matches. Note that find_cmd
169 points to the first ambiguous entry */
170 if ( strncasecmp(s, (*result)->pc_name, strlen((*result)->pc_name)) &&
171 find_cmd(s, (*result) + 1, next))
174 /* found a unique command: component or full? */
175 if ( (*result)->pc_func ) {
178 if ( *next == '\0' ) {
179 return CMD_INCOMPLETE;
181 return process(*next, next, (*result)->pc_sub_cmd, result, prev);
186 #ifdef HAVE_LIBREADLINE
187 static char * command_generator(const char * text, int state)
193 /* Do we have a match table? */
197 /* If this is the first time called on this word, state is 0 */
200 len = (int)strlen(text);
203 /* Return the next name in the command list that paritally matches test */
204 while ( (name = (match_tbl + index)->pc_name) ) {
207 if (strncasecmp(name, text, len) == 0) {
208 return(strdup(name));
212 /* No more matches */
216 /* probably called by readline */
217 static char **command_completion(char * text, int start, int end)
222 match_tbl = top_level;
223 for (table = find_cmd(rl_line_buffer, match_tbl, &pos);
225 table = find_cmd(pos, match_tbl, &pos)) {
227 if (*(pos - 1) == ' ') match_tbl = table->pc_sub_cmd;
230 return(completion_matches(text, command_generator));
234 /* take a string and execute the function or print help */
235 int execute_line(char * line)
237 command_t *cmd, *ambig;
244 switch( process(line, &next, top_level, &cmd, &prev) ) {
246 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
247 while( (ambig = find_cmd(prev, cmd, &tmp)) ) {
248 fprintf(stderr, "%s ", ambig->pc_name);
251 fprintf(stderr, "\n");
254 fprintf(stderr, "No such command, type help\n");
258 "'%s' incomplete command. Use '%s x' where x is one of:\n",
260 fprintf(stderr, "\t");
261 for (i = 0; cmd->pc_sub_cmd[i].pc_name; i++) {
262 fprintf(stderr, "%s ", cmd->pc_sub_cmd[i].pc_name);
264 fprintf(stderr, "\n");
267 i = line2args(line, argv, MAXARGS);
268 rc = (cmd->pc_func)(i, argv);
271 fprintf(stderr, "%s\n", cmd->pc_help);
285 /* just in case you're ever in an airplane and discover you
286 forgot to install readline-dev. :) */
289 int interactive = isatty (fileno (stdin));
291 #ifdef HAVE_LIBREADLINE
293 stifle_history(HISTORY);
297 rl_prep_term_function = (rl_vintfunc_t *)noop_fn;
298 rl_deprep_term_function = (rl_voidfunc_t *)noop_fn;
301 rl_attempted_completion_function = (CPPFunction *)command_completion;
302 rl_completion_entry_function = (void *)command_generator;
307 #ifndef HAVE_LIBREADLINE
308 #define add_history(s)
309 char * readline(char * prompt)
315 printf ("%s", prompt);
316 fgets(line, sizeof(line), stdin);
318 if (n && line[n-1] == '\n')
320 if (n == 0 && feof(stdin)) {
328 /* this is the command execution machine */
329 int Parser_commands(void)
335 interactive = init_input();
338 line = readline(interactive ? parser_prompt : NULL);
342 s = skipwhitespace(line);
346 rc = execute_line(s);
355 /* sets the parser prompt */
356 void Parser_init(char * prompt, command_t * cmds)
360 if (parser_prompt) free(parser_prompt);
361 parser_prompt = strdup(prompt);
364 /* frees the parser prompt */
365 void Parser_exit(int argc, char *argv[])
369 parser_prompt = NULL;
372 /* convert a string to an integer */
373 int Parser_int(char *s, int *val)
378 ret = sscanf(s, "%d", val);
379 else if (*(s+1) != 'x')
380 ret = sscanf(s, "%o", val);
383 ret = sscanf(++s, "%x", val);
390 void Parser_qhelp(int argc, char *argv[]) {
392 printf("Available commands are:\n");
394 print_commands(NULL, top_level);
395 printf("For more help type: help command-name\n");
398 int Parser_help(int argc, char **argv)
401 char *next, *prev, *tmp;
402 command_t *result, *ambig;
406 Parser_qhelp(argc, argv);
411 for ( i = 1 ; i < argc ; i++ ) {
412 strcat(line, argv[i]);
415 switch ( process(line, &next, top_level, &result, &prev) ) {
417 fprintf(stderr, "%s: %s\n",line, result->pc_help);
420 fprintf(stderr, "%s: Unknown command.\n", line);
424 "'%s' incomplete command. Use '%s x' where x is one of:\n",
426 fprintf(stderr, "\t");
427 for (i = 0; result->pc_sub_cmd[i].pc_name; i++) {
428 fprintf(stderr, "%s ", result->pc_sub_cmd[i].pc_name);
430 fprintf(stderr, "\n");
433 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
434 while( (ambig = find_cmd(prev, result, &tmp)) ) {
435 fprintf(stderr, "%s ", ambig->pc_name);
438 fprintf(stderr, "\n");
445 void Parser_printhelp(char *cmd)
447 char *argv[] = { "help", cmd };
448 Parser_help(2, argv);
452 /*************************************************************************
454 *************************************************************************/
457 static void print_commands(char * str, command_t * table) {
461 for (cmds = table; cmds->pc_name; cmds++) {
463 if (str) printf("\t%s %s\n", str, cmds->pc_name);
464 else printf("\t%s\n", cmds->pc_name);
466 if (cmds->pc_sub_cmd) {
468 sprintf(buf, "%s %s", str, cmds->pc_name);
469 print_commands(buf, cmds->pc_sub_cmd);
471 print_commands(cmds->pc_name, cmds->pc_sub_cmd);
477 char *Parser_getstr(const char *prompt, const char *deft, char *res,
481 int size = strlen(prompt) + strlen(deft) + 8;
483 theprompt = malloc(size);
486 sprintf(theprompt, "%s [%s]: ", prompt, deft);
488 line = readline(theprompt);
491 if ( line == NULL || *line == '\0' ) {
492 strncpy(res, deft, len);
494 strncpy(res, line, len);
505 /* get integer from prompt, loop forever to get it */
506 int Parser_getint(const char *prompt, long min, long max, long deft, int base)
511 int size = strlen(prompt) + 40;
512 char *theprompt = malloc(size);
514 sprintf(theprompt,"%s [%ld, (0x%lx)]: ", prompt, deft, deft);
520 line = readline(theprompt);
522 fprintf(stdout, "Please enter an integer.\n");
526 if ( *line == '\0' ) {
531 rc = Parser_arg2int(line, &result, base);
534 fprintf(stdout, "Invalid string.\n");
536 } else if ( result > max || result < min ) {
537 fprintf(stdout, "Error: response must lie between %ld and %ld.\n",
551 /* get boolean (starting with YyNn; loop forever */
552 int Parser_getbool(const char *prompt, int deft)
556 int size = strlen(prompt) + 8;
557 char *theprompt = malloc(size);
562 if ( deft != 0 && deft != 1 ) {
563 fprintf(stderr, "Error: Parser_getbool given bad default (%d).\n",
567 sprintf(theprompt, "%s [%s]: ", prompt, (deft==0)? "N" : "Y");
571 line = readline(theprompt);
572 if ( line == NULL ) {
576 if ( *line == '\0' ) {
580 if ( *line == 'y' || *line == 'Y' ) {
584 if ( *line == 'n' || *line == 'N' ) {
590 fprintf(stdout, "Invalid string. Must start with yY or nN\n");
601 /* parse int out of a string or prompt for it */
602 long Parser_intarg(const char *inp, const char *prompt, int deft,
603 int min, int max, int base)
608 rc = Parser_arg2int(inp, &result, base);
613 return Parser_getint(prompt, deft, min, max, base);
617 /* parse int out of a string or prompt for it */
618 char *Parser_strarg(char *inp, const char *prompt, const char *deft,
619 char *answer, int len)
621 if ( inp == NULL || *inp == '\0' ) {
622 return Parser_getstr(prompt, deft, answer, len);
627 /* change a string into a number: return 0 on success. No invalid characters
628 allowed. The processing of base and validity follows strtol(3)*/
629 int Parser_arg2int(const char *inp, long *result, int base)
633 if ( (base !=0) && (base < 2 || base > 36) )
636 *result = strtol(inp, &endptr, base);
638 if ( *inp != '\0' && *endptr == '\0' )
644 /* Convert human readable size string to and int; "1k" -> 1000 */
645 int Parser_size (int *sizep, char *str) {
649 switch (sscanf (str, "%d%1[gGmMkK]", &size, mod)) {
681 /* Convert a string boolean to an int; "enable" -> 1 */
682 int Parser_bool (int *b, char *str) {
683 if (!strcasecmp (str, "no") ||
684 !strcasecmp (str, "n") ||
685 !strcasecmp (str, "off") ||
686 !strcasecmp (str, "disable"))
692 if (!strcasecmp (str, "yes") ||
693 !strcasecmp (str, "y") ||
694 !strcasecmp (str, "on") ||
695 !strcasecmp (str, "enable"))
704 int Parser_quit(int argc, char **argv)