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 *);
40 static command_t * top_level; /* Top level of commands, initialized by
42 static command_t * match_tbl; /* Command completion against this table */
43 static char * parser_prompt = NULL;/* Parser prompt, set by InitParser */
44 static int done; /* Set to 1 if user types exit or quit */
47 /* static functions */
48 static char *skipwhitespace(char *s);
49 static char *skiptowhitespace(char *s);
50 static command_t *find_cmd(char *name, command_t cmds[], char **next);
51 static int process(char *s, char **next, command_t *lookup, command_t **result, char **prev);
52 static char *command_generator(const char *text, int state);
53 static char **command_completion(char *text, int start, int end);
54 static void print_commands(char *str, command_t *table);
56 static char * skipwhitespace(char * s)
62 for (t = s; t <= s + len && isspace(*t); t++);
67 static char * skiptowhitespace(char * s)
71 for (t = s; *t && !isspace(*t); t++);
75 static int line2args(char *line, char **argv, int maxargs)
80 arg = strtok(line, " \t");
87 while( (arg = strtok(NULL, " \t")) && (i <= maxargs)) {
94 /* find a command -- return it if unique otherwise print alternatives */
95 static command_t *Parser_findargcmd(char *name, command_t cmds[])
99 for (cmd = cmds; cmd->pc_name; cmd++) {
100 if (strcmp(name, cmd->pc_name) == 0)
106 int Parser_execarg(int argc, char **argv, command_t cmds[])
110 cmd = Parser_findargcmd(argv[0], cmds);
112 return (cmd->pc_func)(argc, argv);
114 printf("Try interactive use without arguments or use one of:\n");
115 for (cmd = cmds; cmd->pc_name; cmd++)
116 printf("\"%s\" ", cmd->pc_name);
117 printf("\nas argument.\n");
122 /* returns the command_t * (NULL if not found) corresponding to a
123 _partial_ match with the first token in name. It sets *next to
124 point to the following token. Does not modify *name. */
125 static command_t * find_cmd(char * name, command_t cmds[], char ** next)
132 /* This sets name to point to the first non-white space character,
133 and next to the first whitespace after name, len to the length: do
135 name = skipwhitespace(name);
136 *next = skiptowhitespace(name);
141 for (i = 0; cmds[i].pc_name; i++) {
142 if (strncasecmp(name, cmds[i].pc_name, len) == 0) {
143 *next = skipwhitespace(*next);
150 /* Recursively process a command line string s and find the command
151 corresponding to it. This can be ambiguous, full, incomplete,
153 static int process(char *s, char ** next, command_t *lookup,
154 command_t **result, char **prev)
156 *result = find_cmd(s, lookup, next);
163 /* found entry: is it ambigous, i.e. not exact command name and
164 more than one command in the list matches. Note that find_cmd
165 points to the first ambiguous entry */
166 if ( strncasecmp(s, (*result)->pc_name, strlen((*result)->pc_name)) &&
167 find_cmd(s, (*result) + 1, next))
170 /* found a unique command: component or full? */
171 if ( (*result)->pc_func ) {
174 if ( *next == '\0' ) {
175 return CMD_INCOMPLETE;
177 return process(*next, next, (*result)->pc_sub_cmd, result, prev);
182 static char * command_generator(const char * text, int state)
188 /* Do we have a match table? */
192 /* If this is the first time called on this word, state is 0 */
195 len = (int)strlen(text);
198 /* Return the next name in the command list that paritally matches test */
199 while ( (name = (match_tbl + index)->pc_name) ) {
202 if (strncasecmp(name, text, len) == 0) {
203 return(strdup(name));
207 /* No more matches */
211 /* probably called by readline */
212 static char **command_completion(char * text, int start, int end)
217 match_tbl = top_level;
218 for (table = find_cmd(rl_line_buffer, match_tbl, &pos);
220 table = find_cmd(pos, match_tbl, &pos)) {
222 if (*(pos - 1) == ' ') match_tbl = table->pc_sub_cmd;
225 return(completion_matches(text, command_generator));
228 /* take a string and execute the function or print help */
229 int execute_line(char * line)
231 command_t *cmd, *ambig;
238 switch( process(line, &next, top_level, &cmd, &prev) ) {
240 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
241 while( (ambig = find_cmd(prev, cmd, &tmp)) ) {
242 fprintf(stderr, "%s ", ambig->pc_name);
245 fprintf(stderr, "\n");
248 fprintf(stderr, "No such command, type help\n");
252 "'%s' incomplete command. Use '%s x' where x is one of:\n",
254 fprintf(stderr, "\t");
255 for (i = 0; cmd->pc_sub_cmd[i].pc_name; i++) {
256 fprintf(stderr, "%s ", cmd->pc_sub_cmd[i].pc_name);
258 fprintf(stderr, "\n");
261 i = line2args(line, argv, MAXARGS);
262 rc = (cmd->pc_func)(i, argv);
265 fprintf(stderr, "%s\n", cmd->pc_help);
273 /* this is the command execution machine */
274 int Parser_commands(void)
280 stifle_history(HISTORY);
282 rl_attempted_completion_function = (CPPFunction *)command_completion;
283 rl_completion_entry_function = (void *)command_generator;
286 line = readline(parser_prompt);
290 s = skipwhitespace(line);
294 rc = execute_line(s);
303 /* sets the parser prompt */
304 void Parser_init(char * prompt, command_t * cmds)
308 if (parser_prompt) free(parser_prompt);
309 parser_prompt = strdup(prompt);
312 /* frees the parser prompt */
313 void Parser_exit(int argc, char *argv[])
317 parser_prompt = NULL;
320 /* convert a string to an integer */
321 int Parser_int(char *s, int *val)
326 ret = sscanf(s, "%d", val);
327 else if (*(s+1) != 'x')
328 ret = sscanf(s, "%o", val);
331 ret = sscanf(++s, "%x", val);
338 void Parser_qhelp(int argc, char *argv[]) {
340 printf("Available commands are:\n");
342 print_commands(NULL, top_level);
343 printf("For more help type: help command-name\n");
346 int Parser_help(int argc, char **argv)
349 char *next, *prev, *tmp;
350 command_t *result, *ambig;
354 Parser_qhelp(argc, argv);
359 for ( i = 1 ; i < argc ; i++ ) {
360 strcat(line, argv[i]);
363 switch ( process(line, &next, top_level, &result, &prev) ) {
365 fprintf(stderr, "%s: %s\n",line, result->pc_help);
368 fprintf(stderr, "%s: Unknown command.\n", line);
372 "'%s' incomplete command. Use '%s x' where x is one of:\n",
374 fprintf(stderr, "\t");
375 for (i = 0; result->pc_sub_cmd[i].pc_name; i++) {
376 fprintf(stderr, "%s ", result->pc_sub_cmd[i].pc_name);
378 fprintf(stderr, "\n");
381 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
382 while( (ambig = find_cmd(prev, result, &tmp)) ) {
383 fprintf(stderr, "%s ", ambig->pc_name);
386 fprintf(stderr, "\n");
393 void Parser_printhelp(char *cmd)
395 char *argv[] = { "help", cmd };
396 Parser_help(2, argv);
400 /*************************************************************************
402 *************************************************************************/
405 static void print_commands(char * str, command_t * table) {
409 for (cmds = table; cmds->pc_name; cmds++) {
411 if (str) printf("\t%s %s\n", str, cmds->pc_name);
412 else printf("\t%s\n", cmds->pc_name);
414 if (cmds->pc_sub_cmd) {
416 sprintf(buf, "%s %s", str, cmds->pc_name);
417 print_commands(buf, cmds->pc_sub_cmd);
419 print_commands(cmds->pc_name, cmds->pc_sub_cmd);
425 char *Parser_getstr(const char *prompt, const char *deft, char *res,
429 int size = strlen(prompt) + strlen(deft) + 8;
431 theprompt = malloc(size);
434 sprintf(theprompt, "%s [%s]: ", prompt, deft);
436 line = readline(theprompt);
439 if ( line == NULL || *line == '\0' ) {
440 strncpy(res, deft, len);
442 strncpy(res, line, len);
453 /* get integer from prompt, loop forever to get it */
454 int Parser_getint(const char *prompt, long min, long max, long deft, int base)
459 int size = strlen(prompt) + 40;
460 char *theprompt = malloc(size);
462 sprintf(theprompt,"%s [%ld, (0x%lx)]: ", prompt, deft, deft);
468 line = readline(theprompt);
470 fprintf(stdout, "Please enter an integer.\n");
474 if ( *line == '\0' ) {
479 rc = Parser_arg2int(line, &result, base);
482 fprintf(stdout, "Invalid string.\n");
484 } else if ( result > max || result < min ) {
485 fprintf(stdout, "Error: response must lie between %ld and %ld.\n",
499 /* get boolean (starting with YyNn; loop forever */
500 int Parser_getbool(const char *prompt, int deft)
504 int size = strlen(prompt) + 8;
505 char *theprompt = malloc(size);
510 if ( deft != 0 && deft != 1 ) {
511 fprintf(stderr, "Error: Parser_getbool given bad default (%d).\n",
515 sprintf(theprompt, "%s [%s]: ", prompt, (deft==0)? "N" : "Y");
519 line = readline(theprompt);
520 if ( line == NULL ) {
524 if ( *line == '\0' ) {
528 if ( *line == 'y' || *line == 'Y' ) {
532 if ( *line == 'n' || *line == 'N' ) {
538 fprintf(stdout, "Invalid string. Must start with yY or nN\n");
549 /* parse int out of a string or prompt for it */
550 long Parser_intarg(const char *inp, const char *prompt, int deft,
551 int min, int max, int base)
556 rc = Parser_arg2int(inp, &result, base);
561 return Parser_getint(prompt, deft, min, max, base);
565 /* parse int out of a string or prompt for it */
566 char *Parser_strarg(char *inp, const char *prompt, const char *deft,
567 char *answer, int len)
569 if ( inp == NULL || *inp == '\0' ) {
570 return Parser_getstr(prompt, deft, answer, len);
575 /* change a string into a number: return 0 on success. No invalid characters
576 allowed. The processing of base and validity follows strtol(3)*/
577 int Parser_arg2int(const char *inp, long *result, int base)
581 if ( (base !=0) && (base < 2 || base > 36) )
584 *result = strtol(inp, &endptr, base);
586 if ( *inp != '\0' && *endptr == '\0' )
592 /* Convert human readable size string to and int; "1k" -> 1000 */
593 int Parser_size (int *sizep, char *str) {
597 switch (sscanf (str, "%d%1[gGmMkK]", &size, mod)) {
629 /* Convert a string boolean to an int; "enable" -> 1 */
630 int Parser_bool (int *b, char *str) {
631 if (strcasecmp (str, "no") ||
632 strcasecmp (str, "n") ||
633 strcasecmp (str, "off") ||
634 strcasecmp (str, "disable"))
640 if (strcasecmp (str, "yes") ||
641 strcasecmp (str, "y") ||
642 strcasecmp (str, "on") ||
643 strcasecmp (str, "enable"))
652 int Parser_quit(int argc, char **argv)