1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
42 #include <sys/param.h>
48 static command_t * top_level; /* Top level of commands, initialized by
50 static char * parser_prompt = NULL;/* Parser prompt, set by InitParser */
51 static int done; /* Set to 1 if user types exit or quit */
52 static int ignore_errors; /* Normally, the parser will quit when
53 an error occurs in non-interacive
54 mode. Setting this to non-zero will
55 force it to keep buggering on. */
58 /* static functions */
59 static char *skipwhitespace(char *s);
60 static char *skiptowhitespace(char *s);
61 static command_t *find_cmd(char *name, command_t cmds[], char **next);
62 static int process(char *s, char **next, command_t *lookup, command_t **result,
64 static void print_commands(char *str, command_t *table);
66 static char * skipwhitespace(char * s)
72 for (t = s; t <= s + len && isspace(*t); t++);
77 static char * skiptowhitespace(char * s)
81 for (t = s; *t && !isspace(*t); t++);
85 static int line2args(char *line, char **argv, int maxargs)
90 arg = strtok(line, " \t");
97 while( (arg = strtok(NULL, " \t")) && (i <= maxargs)) {
104 /* find a command -- return it if unique otherwise print alternatives */
105 static command_t *Parser_findargcmd(char *name, command_t cmds[])
109 for (cmd = cmds; cmd->pc_name; cmd++) {
110 if (strcmp(name, cmd->pc_name) == 0)
116 void Parser_ignore_errors(int ignore)
118 ignore_errors = ignore;
121 int Parser_execarg(int argc, char **argv, command_t cmds[])
125 cmd = Parser_findargcmd(argv[0], cmds);
127 int rc = (cmd->pc_func)(argc, argv);
129 fprintf(stderr, "%s\n", cmd->pc_help);
132 printf("Try interactive use without arguments or use one of:\n");
133 for (cmd = cmds; cmd->pc_name; cmd++)
134 printf("\"%s\"\n", cmd->pc_name);
135 printf("as argument.\n");
140 /* returns the command_t * (NULL if not found) corresponding to a
141 _partial_ match with the first token in name. It sets *next to
142 point to the following token. Does not modify *name. */
143 static command_t * find_cmd(char * name, command_t cmds[], char ** next)
150 /* This sets name to point to the first non-white space character,
151 and next to the first whitespace after name, len to the length: do
153 name = skipwhitespace(name);
154 *next = skiptowhitespace(name);
159 for (i = 0; cmds[i].pc_name; i++) {
160 if (strncasecmp(name, cmds[i].pc_name, len) == 0) {
161 *next = skipwhitespace(*next);
168 /* Recursively process a command line string s and find the command
169 corresponding to it. This can be ambiguous, full, incomplete,
171 static int process(char *s, char ** next, command_t *lookup,
172 command_t **result, char **prev)
174 *result = find_cmd(s, lookup, next);
181 /* found entry: is it ambigous, i.e. not exact command name and
182 more than one command in the list matches. Note that find_cmd
183 points to the first ambiguous entry */
184 if (strncasecmp(s, (*result)->pc_name, strlen((*result)->pc_name))) {
186 command_t *another_result = find_cmd(s, (*result) + 1,
188 int found_another = 0;
190 while (another_result) {
191 if (strncasecmp(s, another_result->pc_name,
192 strlen(another_result->pc_name)) == 0){
193 *result = another_result;
194 *next = another_next;
197 another_result = find_cmd(s, another_result + 1,
206 /* found a unique command: component or full? */
207 if ( (*result)->pc_func ) {
210 if ( *next == '\0' ) {
211 return CMD_INCOMPLETE;
213 return process(*next, next, (*result)->pc_sub_cmd,
219 #ifdef HAVE_LIBREADLINE
220 static command_t * match_tbl; /* Command completion against this table */
221 static char * command_generator(const char * text, int state)
227 /* Do we have a match table? */
231 /* If this is the first time called on this word, state is 0 */
234 len = (int)strlen(text);
237 /* Return next name in the command list that paritally matches test */
238 while ( (name = (match_tbl + index)->pc_name) ) {
241 if (strncasecmp(name, text, len) == 0) {
242 return(strdup(name));
246 /* No more matches */
250 /* probably called by readline */
251 static char **command_completion(char * text, int start, int end)
256 match_tbl = top_level;
258 for (table = find_cmd(rl_line_buffer, match_tbl, &pos);
259 table; table = find_cmd(pos, match_tbl, &pos))
262 if (*(pos - 1) == ' ') match_tbl = table->pc_sub_cmd;
265 return completion_matches(text, command_generator);
269 /* take a string and execute the function or print help */
270 int execute_line(char * line)
272 command_t *cmd, *ambig;
279 switch (process(line, &next, top_level, &cmd, &prev)) {
281 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
282 while( (ambig = find_cmd(prev, cmd, &tmp)) ) {
283 fprintf(stderr, "%s ", ambig->pc_name);
286 fprintf(stderr, "\n");
289 fprintf(stderr, "No such command, type help\n");
293 "'%s' incomplete command. Use '%s x' where x is one of:\n",
295 fprintf(stderr, "\t");
296 for (i = 0; cmd->pc_sub_cmd[i].pc_name; i++) {
297 fprintf(stderr, "%s ", cmd->pc_sub_cmd[i].pc_name);
299 fprintf(stderr, "\n");
302 i = line2args(line, argv, MAXARGS);
303 rc = (cmd->pc_func)(i, argv);
306 fprintf(stderr, "%s\n", cmd->pc_help);
320 /* just in case you're ever in an airplane and discover you
321 forgot to install readline-dev. :) */
324 int interactive = isatty (fileno (stdin));
326 #ifdef HAVE_LIBREADLINE
328 stifle_history(HISTORY);
332 rl_prep_term_function = (rl_vintfunc_t *)noop_fn;
333 rl_deprep_term_function = (rl_voidfunc_t *)noop_fn;
336 rl_attempted_completion_function = (CPPFunction *)command_completion;
337 rl_completion_entry_function = (void *)command_generator;
342 #ifndef HAVE_LIBREADLINE
343 #define add_history(s)
344 char * readline(char * prompt)
347 char *line = malloc(size);
355 printf ("%s", prompt);
358 if ((c = fgetc(stdin)) != EOF) {
363 if (ptr - line >= size - 1) {
370 memcpy(tmp, line, ptr - line);
371 ptr = tmp + (ptr - line);
377 if (ferror(stdin) || feof(stdin))
384 if (eof && (strlen(line) == 0)) {
395 /* this is the command execution machine */
396 int Parser_commands(void)
399 int rc = 0, save_error = 0;
402 interactive = init_input();
405 line = readline(interactive ? parser_prompt : NULL);
409 s = skipwhitespace(line);
413 rc = execute_line(s);
415 /* stop on error if not-interactive */
416 if (rc != 0 && !interactive) {
431 /* sets the parser prompt */
432 void Parser_init(char * prompt, command_t * cmds)
436 if (parser_prompt) free(parser_prompt);
437 parser_prompt = strdup(prompt);
440 /* frees the parser prompt */
441 void Parser_exit(int argc, char *argv[])
445 parser_prompt = NULL;
448 /* convert a string to an integer */
449 int Parser_int(char *s, int *val)
454 ret = sscanf(s, "%d", val);
455 else if (*(s+1) != 'x')
456 ret = sscanf(s, "%o", val);
459 ret = sscanf(++s, "%x", val);
466 void Parser_qhelp(int argc, char *argv[]) {
468 printf("Available commands are:\n");
470 print_commands(NULL, top_level);
471 printf("For more help type: help command-name\n");
474 int Parser_help(int argc, char **argv)
477 char *next, *prev, *tmp;
478 command_t *result, *ambig;
482 Parser_qhelp(argc, argv);
487 for ( i = 1 ; i < argc ; i++ ) {
488 strcat(line, argv[i]);
491 switch ( process(line, &next, top_level, &result, &prev) ) {
493 fprintf(stderr, "%s: %s\n",line, result->pc_help);
496 fprintf(stderr, "%s: Unknown command.\n", line);
500 "'%s' incomplete command. Use '%s x' where x is one of:\n",
502 fprintf(stderr, "\t");
503 for (i = 0; result->pc_sub_cmd[i].pc_name; i++) {
504 fprintf(stderr, "%s ", result->pc_sub_cmd[i].pc_name);
506 fprintf(stderr, "\n");
509 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
510 while( (ambig = find_cmd(prev, result, &tmp)) ) {
511 fprintf(stderr, "%s ", ambig->pc_name);
514 fprintf(stderr, "\n");
521 void Parser_printhelp(char *cmd)
523 char *argv[] = { "help", cmd };
524 Parser_help(2, argv);
528 /*************************************************************************
530 *************************************************************************/
531 static void print_commands(char * str, command_t * table) {
535 for (cmds = table; cmds->pc_name; cmds++) {
537 if (str) printf("\t%s %s\n", str, cmds->pc_name);
538 else printf("\t%s\n", cmds->pc_name);
540 if (cmds->pc_sub_cmd) {
542 sprintf(buf, "%s %s", str, cmds->pc_name);
543 print_commands(buf, cmds->pc_sub_cmd);
545 print_commands(cmds->pc_name, cmds->pc_sub_cmd);
551 char *Parser_getstr(const char *prompt, const char *deft, char *res,
555 int size = strlen(prompt) + strlen(deft) + 8;
557 theprompt = malloc(size);
560 sprintf(theprompt, "%s [%s]: ", prompt, deft);
562 line = readline(theprompt);
565 if ( line == NULL || *line == '\0' ) {
566 strncpy(res, deft, len);
568 strncpy(res, line, len);
579 /* get integer from prompt, loop forever to get it */
580 int Parser_getint(const char *prompt, long min, long max, long deft, int base)
585 int size = strlen(prompt) + 40;
586 char *theprompt = malloc(size);
588 sprintf(theprompt,"%s [%ld, (0x%lx)]: ", prompt, deft, deft);
594 line = readline(theprompt);
596 fprintf(stdout, "Please enter an integer.\n");
600 if ( *line == '\0' ) {
605 rc = Parser_arg2int(line, &result, base);
608 fprintf(stdout, "Invalid string.\n");
610 } else if ( result > max || result < min ) {
611 fprintf(stdout, "Error: response must lie between %ld and %ld.\n",
625 /* get boolean (starting with YyNn; loop forever */
626 int Parser_getbool(const char *prompt, int deft)
630 int size = strlen(prompt) + 8;
631 char *theprompt = malloc(size);
636 if ( deft != 0 && deft != 1 ) {
637 fprintf(stderr, "Error: Parser_getbool given bad default %d\n",
641 sprintf(theprompt, "%s [%s]: ", prompt, (deft==0)? "N" : "Y");
645 line = readline(theprompt);
646 if ( line == NULL ) {
650 if ( *line == '\0' ) {
654 if ( *line == 'y' || *line == 'Y' ) {
658 if ( *line == 'n' || *line == 'N' ) {
664 fprintf(stdout, "Invalid string. Must start with yY or nN\n");
675 /* parse int out of a string or prompt for it */
676 long Parser_intarg(const char *inp, const char *prompt, int deft,
677 int min, int max, int base)
682 rc = Parser_arg2int(inp, &result, base);
687 return Parser_getint(prompt, deft, min, max, base);
691 /* parse int out of a string or prompt for it */
692 char *Parser_strarg(char *inp, const char *prompt, const char *deft,
693 char *answer, int len)
695 if ( inp == NULL || *inp == '\0' ) {
696 return Parser_getstr(prompt, deft, answer, len);
701 /* change a string into a number: return 0 on success. No invalid characters
702 allowed. The processing of base and validity follows strtol(3)*/
703 int Parser_arg2int(const char *inp, long *result, int base)
707 if ( (base !=0) && (base < 2 || base > 36) )
710 *result = strtol(inp, &endptr, base);
712 if ( *inp != '\0' && *endptr == '\0' )
718 /* Convert human readable size string to and int; "1k" -> 1000 */
719 int Parser_size (int *sizep, char *str) {
723 switch (sscanf (str, "%d%1[gGmMkK]", &size, mod)) {
755 /* Convert a string boolean to an int; "enable" -> 1 */
756 int Parser_bool (int *b, char *str) {
757 if (!strcasecmp (str, "no") ||
758 !strcasecmp (str, "n") ||
759 !strcasecmp (str, "off") ||
760 !strcasecmp (str, "down") ||
761 !strcasecmp (str, "disable"))
767 if (!strcasecmp (str, "yes") ||
768 !strcasecmp (str, "y") ||
769 !strcasecmp (str, "on") ||
770 !strcasecmp (str, "up") ||
771 !strcasecmp (str, "enable"))
780 int Parser_quit(int argc, char **argv)