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>
30 #include <lnet/api-support.h>
34 static command_t * top_level; /* Top level of commands, initialized by
36 static char * parser_prompt = NULL;/* Parser prompt, set by InitParser */
37 static int done; /* Set to 1 if user types exit or quit */
40 /* static functions */
41 static char *skipwhitespace(char *s);
42 static char *skiptowhitespace(char *s);
43 static command_t *find_cmd(char *name, command_t cmds[], char **next);
44 static int process(char *s, char **next, command_t *lookup, command_t **result,
46 static void print_commands(char *str, command_t *table);
48 static char * skipwhitespace(char * s)
54 for (t = s; t <= s + len && isspace(*t); t++);
59 static char * skiptowhitespace(char * s)
63 for (t = s; *t && !isspace(*t); t++);
67 static int line2args(char *line, char **argv, int maxargs)
72 arg = strtok(line, " \t");
79 while( (arg = strtok(NULL, " \t")) && (i <= maxargs)) {
86 /* find a command -- return it if unique otherwise print alternatives */
87 static command_t *Parser_findargcmd(char *name, command_t cmds[])
91 for (cmd = cmds; cmd->pc_name; cmd++) {
92 if (strcmp(name, cmd->pc_name) == 0)
98 int Parser_execarg(int argc, char **argv, command_t cmds[])
102 cmd = Parser_findargcmd(argv[0], cmds);
104 int rc = (cmd->pc_func)(argc, argv);
106 fprintf(stderr, "%s\n", cmd->pc_help);
109 printf("Try interactive use without arguments or use one of:\n");
110 for (cmd = cmds; cmd->pc_name; cmd++)
111 printf("\"%s\" ", cmd->pc_name);
112 printf("\nas argument.\n");
117 /* returns the command_t * (NULL if not found) corresponding to a
118 _partial_ match with the first token in name. It sets *next to
119 point to the following token. Does not modify *name. */
120 static command_t * find_cmd(char * name, command_t cmds[], char ** next)
127 /* This sets name to point to the first non-white space character,
128 and next to the first whitespace after name, len to the length: do
130 name = skipwhitespace(name);
131 *next = skiptowhitespace(name);
136 for (i = 0; cmds[i].pc_name; i++) {
137 if (strncasecmp(name, cmds[i].pc_name, len) == 0) {
138 *next = skipwhitespace(*next);
145 /* Recursively process a command line string s and find the command
146 corresponding to it. This can be ambiguous, full, incomplete,
148 static int process(char *s, char ** next, command_t *lookup,
149 command_t **result, char **prev)
151 *result = find_cmd(s, lookup, next);
158 /* found entry: is it ambigous, i.e. not exact command name and
159 more than one command in the list matches. Note that find_cmd
160 points to the first ambiguous entry */
161 if ( strncasecmp(s, (*result)->pc_name, strlen((*result)->pc_name)) &&
162 find_cmd(s, (*result) + 1, next))
165 /* found a unique command: component or full? */
166 if ( (*result)->pc_func ) {
169 if ( *next == '\0' ) {
170 return CMD_INCOMPLETE;
172 return process(*next, next, (*result)->pc_sub_cmd, result, prev);
177 #ifdef HAVE_LIBREADLINE
178 static command_t * match_tbl; /* Command completion against this table */
179 static char * command_generator(const char * text, int state)
185 /* Do we have a match table? */
189 /* If this is the first time called on this word, state is 0 */
192 len = (int)strlen(text);
195 /* Return next name in the command list that paritally matches test */
196 while ( (name = (match_tbl + index)->pc_name) ) {
199 if (strncasecmp(name, text, len) == 0) {
200 return(strdup(name));
204 /* No more matches */
208 /* probably called by readline */
209 static char **command_completion(char * text, int start, int end)
214 match_tbl = top_level;
216 for (table = find_cmd(rl_line_buffer, match_tbl, &pos);
217 table; table = find_cmd(pos, match_tbl, &pos))
220 if (*(pos - 1) == ' ') match_tbl = table->pc_sub_cmd;
223 return completion_matches(text, command_generator);
227 /* take a string and execute the function or print help */
228 int execute_line(char * line)
230 command_t *cmd, *ambig;
237 switch( process(line, &next, top_level, &cmd, &prev) ) {
239 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
240 while( (ambig = find_cmd(prev, cmd, &tmp)) ) {
241 fprintf(stderr, "%s ", ambig->pc_name);
244 fprintf(stderr, "\n");
247 fprintf(stderr, "No such command, type help\n");
251 "'%s' incomplete command. Use '%s x' where x is one of:\n",
253 fprintf(stderr, "\t");
254 for (i = 0; cmd->pc_sub_cmd[i].pc_name; i++) {
255 fprintf(stderr, "%s ", cmd->pc_sub_cmd[i].pc_name);
257 fprintf(stderr, "\n");
260 i = line2args(line, argv, MAXARGS);
261 rc = (cmd->pc_func)(i, argv);
264 fprintf(stderr, "%s\n", cmd->pc_help);
278 /* just in case you're ever in an airplane and discover you
279 forgot to install readline-dev. :) */
282 int interactive = isatty (fileno (stdin));
284 #ifdef HAVE_LIBREADLINE
286 stifle_history(HISTORY);
290 rl_prep_term_function = (rl_vintfunc_t *)noop_fn;
291 rl_deprep_term_function = (rl_voidfunc_t *)noop_fn;
294 rl_attempted_completion_function = (CPPFunction *)command_completion;
295 rl_completion_entry_function = (void *)command_generator;
300 #ifndef HAVE_LIBREADLINE
301 #define add_history(s)
302 char * readline(char * prompt)
307 printf ("%s", prompt);
308 if (fgets(line, sizeof(line), stdin) == NULL)
311 if (n && line[n-1] == '\n')
317 /* this is the command execution machine */
318 int Parser_commands(void)
324 interactive = init_input();
327 line = readline(interactive ? parser_prompt : NULL);
331 s = skipwhitespace(line);
335 rc = execute_line(s);
337 /* reset optind to 0 to tell getopt
338 * to reinitialize itself */
348 /* sets the parser prompt */
349 void Parser_init(char * prompt, command_t * cmds)
353 if (parser_prompt) free(parser_prompt);
354 parser_prompt = strdup(prompt);
357 /* frees the parser prompt */
358 void Parser_exit(int argc, char *argv[])
362 parser_prompt = NULL;
365 /* convert a string to an integer */
366 int Parser_int(char *s, int *val)
371 ret = sscanf(s, "%d", val);
372 else if (*(s+1) != 'x')
373 ret = sscanf(s, "%o", val);
376 ret = sscanf(++s, "%x", val);
383 void Parser_qhelp(int argc, char *argv[]) {
385 printf("Available commands are:\n");
387 print_commands(NULL, top_level);
388 printf("For more help type: help command-name\n");
391 int Parser_help(int argc, char **argv)
394 char *next, *prev, *tmp;
395 command_t *result, *ambig;
399 Parser_qhelp(argc, argv);
404 for ( i = 1 ; i < argc ; i++ ) {
405 strcat(line, argv[i]);
408 switch ( process(line, &next, top_level, &result, &prev) ) {
410 fprintf(stderr, "%s: %s\n",line, result->pc_help);
413 fprintf(stderr, "%s: Unknown command.\n", line);
417 "'%s' incomplete command. Use '%s x' where x is one of:\n",
419 fprintf(stderr, "\t");
420 for (i = 0; result->pc_sub_cmd[i].pc_name; i++) {
421 fprintf(stderr, "%s ", result->pc_sub_cmd[i].pc_name);
423 fprintf(stderr, "\n");
426 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
427 while( (ambig = find_cmd(prev, result, &tmp)) ) {
428 fprintf(stderr, "%s ", ambig->pc_name);
431 fprintf(stderr, "\n");
438 void Parser_printhelp(char *cmd)
440 char *argv[] = { "help", cmd };
441 Parser_help(2, argv);
444 /*************************************************************************
446 *************************************************************************/
449 static void print_commands(char * str, command_t * table) {
453 for (cmds = table; cmds->pc_name; cmds++) {
455 if (str) printf("\t%s %s\n", str, cmds->pc_name);
456 else printf("\t%s\n", cmds->pc_name);
458 if (cmds->pc_sub_cmd) {
460 sprintf(buf, "%s %s", str, cmds->pc_name);
461 print_commands(buf, cmds->pc_sub_cmd);
463 print_commands(cmds->pc_name, cmds->pc_sub_cmd);
469 char *Parser_getstr(const char *prompt, const char *deft, char *res,
473 int size = strlen(prompt) + strlen(deft) + 8;
475 theprompt = malloc(size);
478 sprintf(theprompt, "%s [%s]: ", prompt, deft);
480 line = readline(theprompt);
483 if ( line == NULL || *line == '\0' ) {
484 strncpy(res, deft, len);
486 strncpy(res, line, len);
497 /* get integer from prompt, loop forever to get it */
498 int Parser_getint(const char *prompt, long min, long max, long deft, int base)
503 int size = strlen(prompt) + 40;
504 char *theprompt = malloc(size);
506 sprintf(theprompt,"%s [%ld, (0x%lx)]: ", prompt, deft, deft);
512 line = readline(theprompt);
514 fprintf(stdout, "Please enter an integer.\n");
518 if ( *line == '\0' ) {
523 rc = Parser_arg2int(line, &result, base);
526 fprintf(stdout, "Invalid string.\n");
528 } else if ( result > max || result < min ) {
529 fprintf(stdout, "Error: response must lie between %ld and %ld.\n",
543 /* get boolean (starting with YyNn; loop forever */
544 int Parser_getbool(const char *prompt, int deft)
548 int size = strlen(prompt) + 8;
549 char *theprompt = malloc(size);
554 if ( deft != 0 && deft != 1 ) {
555 fprintf(stderr, "Error: Parser_getbool given bad default (%d).\n",
559 sprintf(theprompt, "%s [%s]: ", prompt, (deft==0)? "N" : "Y");
563 line = readline(theprompt);
564 if ( line == NULL ) {
568 if ( *line == '\0' ) {
572 if ( *line == 'y' || *line == 'Y' ) {
576 if ( *line == 'n' || *line == 'N' ) {
582 fprintf(stdout, "Invalid string. Must start with yY or nN\n");
593 /* parse int out of a string or prompt for it */
594 long Parser_intarg(const char *inp, const char *prompt, int deft,
595 int min, int max, int base)
600 rc = Parser_arg2int(inp, &result, base);
605 return Parser_getint(prompt, deft, min, max, base);
609 /* parse int out of a string or prompt for it */
610 char *Parser_strarg(char *inp, const char *prompt, const char *deft,
611 char *answer, int len)
613 if ( inp == NULL || *inp == '\0' ) {
614 return Parser_getstr(prompt, deft, answer, len);
619 /* change a string into a number: return 0 on success. No invalid characters
620 allowed. The processing of base and validity follows strtol(3)*/
621 int Parser_arg2int(const char *inp, long *result, int base)
625 if ( (base !=0) && (base < 2 || base > 36) )
628 *result = strtol(inp, &endptr, base);
630 if ( *inp != '\0' && *endptr == '\0' )
636 int Parser_quit(int argc, char **argv)