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.
27 #include <sys/param.h>
30 #define READLINE_LIBRARY
31 #include <readline/readline.h>
33 //extern char **completion_matches __P((char *, rl_compentry_func_t *));
34 extern void using_history(void);
35 extern void stifle_history(int);
36 extern void add_history(char *);
39 #define CMD_COMPLETE 0
40 #define CMD_INCOMPLETE 1
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 */
100 static command_t *Parser_findargcmd(char *name, command_t cmds[])
105 for (i = 0; cmds[i].pc_name; i++) {
108 if (strlen(name) != strlen(cmd->pc_name))
111 if (strlen(name) == strlen(cmd->pc_name)) {
112 if (strcmp(name, cmd->pc_name) == 0)
122 int Parser_execarg(int argc, char **argv, command_t cmds[])
127 cmd = Parser_findargcmd(argv[0], cmds);
129 return (cmd->pc_func)(argc, argv);
131 printf("Try interactive use without arguments or use one of: ");
132 for (i=0 ; cmds[i].pc_name ; i++) {
134 printf("\"%s\" ", cmd->pc_name);
136 printf("as argument.\n");
141 /* returns the command_t * (NULL if not found) corresponding to a
142 _partial_ match with the first token in name. It sets *next to
143 point to the following token. Does not modify *name. */
144 static command_t * find_cmd(char * name, command_t cmds[], char ** next)
151 /* This sets name to point to the first non-white space character,
152 and next to the first whitespace after name, len to the length: do
154 name = skipwhitespace(name);
155 *next = skiptowhitespace(name);
160 for (i = 0; cmds[i].pc_name; i++) {
161 if (strncasecmp(name, cmds[i].pc_name, len) == 0) {
162 *next = skipwhitespace(*next);
169 /* Recursively process a command line string s and find the command
170 corresponding to it. This can be ambiguous, full, incomplete,
172 static int process(char *s, char ** next, command_t *lookup,
173 command_t **result, char **prev)
175 *result = find_cmd(s, lookup, next);
182 /* found entry: is it ambigous, i.e. not exact command name and
183 more than one command in the list matches. Note that find_cmd
184 points to the first ambiguous entry */
185 if ( strncasecmp(s, (*result)->pc_name, strlen((*result)->pc_name)) &&
186 find_cmd(s, (*result) + 1, next))
189 /* found a unique command: component or full? */
190 if ( (*result)->pc_func ) {
193 if ( *next == '\0' ) {
194 return CMD_INCOMPLETE;
196 return process(*next, next, (*result)->pc_sub_cmd, result, prev);
201 static char * command_generator(const char * text, int state)
207 /* Do we have a match table? */
211 /* If this is the first time called on this word, state is 0 */
214 len = (int)strlen(text);
217 /* Return the next name in the command list that paritally matches test */
218 while ( (name = (match_tbl + index)->pc_name) ) {
221 if (strncasecmp(name, text, len) == 0) {
222 return(strdup(name));
226 /* No more matches */
230 /* probably called by readline */
231 static char **command_completion(char * text, int start, int end)
236 match_tbl = top_level;
237 for (table = find_cmd(rl_line_buffer, match_tbl, &pos);
239 table = find_cmd(pos, match_tbl, &pos)) {
241 if (*(pos - 1) == ' ') match_tbl = table->pc_sub_cmd;
244 return(completion_matches(text, command_generator));
247 /* take a string and execute the function or print help */
248 int execute_line(char * line)
250 command_t *cmd, *ambig;
257 switch( process(line, &next, top_level, &cmd, &prev) ) {
259 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
260 while( (ambig = find_cmd(prev, cmd, &tmp)) ) {
261 fprintf(stderr, "%s ", ambig->pc_name);
264 fprintf(stderr, "\n");
267 fprintf(stderr, "No such command, type help\n");
271 "'%s' incomplete command. Use '%s x' where x is one of:\n",
273 fprintf(stderr, "\t");
274 for (i = 0; cmd->pc_sub_cmd[i].pc_name; i++) {
275 fprintf(stderr, "%s ", cmd->pc_sub_cmd[i].pc_name);
277 fprintf(stderr, "\n");
280 i = line2args(line, argv, MAXARGS);
281 rc = (cmd->pc_func)(i, argv);
288 /* this is the command execution machine */
289 int Parser_commands(void)
295 stifle_history(HISTORY);
297 rl_attempted_completion_function =
298 (CPPFunction *)command_completion;
299 rl_completion_entry_function = (void *)command_generator;
302 line = readline(parser_prompt);
306 s = skipwhitespace(line);
310 rc = execute_line(s);
319 /* sets the parser prompt */
320 void Parser_init(char * prompt, command_t * cmds)
324 if (parser_prompt) free(parser_prompt);
325 parser_prompt = strdup(prompt);
328 /* frees the parser prompt */
329 void Parser_exit(int argc, char *argv[])
333 parser_prompt = NULL;
336 /* convert a string to an integer */
337 int Parser_int(char *s, int *val)
342 ret = sscanf(s, "%d", val);
343 else if (*(s+1) != 'x')
344 ret = sscanf(s, "%o", val);
347 ret = sscanf(++s, "%x", val);
355 void Parser_qhelp(int argc, char *argv[]) {
357 printf("Available commands are:\n");
359 print_commands(NULL, top_level);
360 printf("For more help type: help command-name\n");
363 int Parser_help(int argc, char **argv)
366 char *next, *prev, *tmp;
367 command_t *result, *ambig;
371 Parser_qhelp(argc, argv);
376 for ( i = 1 ; i < argc ; i++ ) {
377 strcat(line, argv[i]);
380 switch ( process(line, &next, top_level, &result, &prev) ) {
382 fprintf(stderr, "%s: %s\n",line, result->pc_help);
385 fprintf(stderr, "%s: Unknown command.\n", line);
389 "'%s' incomplete command. Use '%s x' where x is one of:\n",
391 fprintf(stderr, "\t");
392 for (i = 0; result->pc_sub_cmd[i].pc_name; i++) {
393 fprintf(stderr, "%s ", result->pc_sub_cmd[i].pc_name);
395 fprintf(stderr, "\n");
398 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
399 while( (ambig = find_cmd(prev, result, &tmp)) ) {
400 fprintf(stderr, "%s ", ambig->pc_name);
403 fprintf(stderr, "\n");
409 /*************************************************************************
411 *************************************************************************/
414 static void print_commands(char * str, command_t * table) {
418 for (cmds = table; cmds->pc_name; cmds++) {
420 if (str) printf("\t%s %s\n", str, cmds->pc_name);
421 else printf("\t%s\n", cmds->pc_name);
423 if (cmds->pc_sub_cmd) {
425 sprintf(buf, "%s %s", str, cmds->pc_name);
426 print_commands(buf, cmds->pc_sub_cmd);
428 print_commands(cmds->pc_name, cmds->pc_sub_cmd);
434 char *Parser_getstr(const char *prompt, const char *deft, char *res,
438 int size = strlen(prompt) + strlen(deft) + 8;
440 theprompt = malloc(size);
443 sprintf(theprompt, "%s [%s]: ", prompt, deft);
445 line = readline(theprompt);
448 if ( line == NULL || *line == '\0' ) {
449 strncpy(res, deft, len);
451 strncpy(res, line, len);
462 /* get integer from prompt, loop forever to get it */
463 int Parser_getint(const char *prompt, long min, long max, long deft, int base)
468 int size = strlen(prompt) + 40;
469 char *theprompt = malloc(size);
471 sprintf(theprompt,"%s [%ld, (0x%lx)]: ", prompt, deft, deft);
477 line = readline(theprompt);
479 fprintf(stdout, "Please enter an integer.\n");
483 if ( *line == '\0' ) {
488 rc = Parser_arg2int(line, &result, base);
491 fprintf(stdout, "Invalid string.\n");
493 } else if ( result > max || result < min ) {
494 fprintf(stdout, "Error: response must lie between %ld and %ld.\n",
508 /* get boolean (starting with YyNn; loop forever */
509 int Parser_getbool(const char *prompt, int deft)
513 int size = strlen(prompt) + 8;
514 char *theprompt = malloc(size);
519 if ( deft != 0 && deft != 1 ) {
520 fprintf(stderr, "Error: Parser_getbool given bad default (%d).\n",
524 sprintf(theprompt, "%s [%s]: ", prompt, (deft==0)? "N" : "Y");
528 line = readline(theprompt);
529 if ( line == NULL ) {
533 if ( *line == '\0' ) {
537 if ( *line == 'y' || *line == 'Y' ) {
541 if ( *line == 'n' || *line == 'N' ) {
547 fprintf(stdout, "Invalid string. Must start with yY or nN\n");
558 /* parse int out of a string or prompt for it */
559 long Parser_intarg(const char *inp, const char *prompt, int deft,
560 int min, int max, int base)
565 rc = Parser_arg2int(inp, &result, base);
570 return Parser_getint(prompt, deft, min, max, base);
574 /* parse int out of a string or prompt for it */
575 char *Parser_strarg(char *inp, const char *prompt, const char *deft,
576 char *answer, int len)
579 if ( inp == NULL || *inp == '\0' ) {
580 return Parser_getstr(prompt, deft, answer, len);
585 /* change a string into a number: return 0 on success. No invalid characters
586 allowed. The processing of base and validity follows strtol(3)*/
587 int Parser_arg2int(const char *inp, long *result, int base)
591 if ( (base !=0) && (base < 2 || base > 36) )
594 *result = strtol(inp, &endptr, base);
596 if ( *inp != '\0' && *endptr == '\0' )
602 int Parser_quit(int argc, char **argv)