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;
126 int Parser_execarg(int argc, char **argv, command_t cmds[])
130 cmd = Parser_findargcmd(argv[0], cmds);
132 int rc = (cmd->pc_func)(argc, argv);
134 fprintf(stderr, "%s\n", cmd->pc_help);
137 printf("Try interactive use without arguments or use one of:\n");
138 for (cmd = cmds; cmd->pc_name; cmd++)
139 printf("\"%s\"\n", cmd->pc_name);
140 printf("as argument.\n");
145 /* returns the command_t * (NULL if not found) corresponding to a
146 _partial_ match with the first token in name. It sets *next to
147 point to the following token. Does not modify *name. */
148 static command_t * find_cmd(char * name, command_t cmds[], char ** next)
155 /* This sets name to point to the first non-white space character,
156 and next to the first whitespace after name, len to the length: do
158 name = skipwhitespace(name);
159 *next = skiptowhitespace(name);
164 for (i = 0; cmds[i].pc_name; i++) {
165 if (strncasecmp(name, cmds[i].pc_name, len) == 0) {
166 *next = skipwhitespace(*next);
173 /* Recursively process a command line string s and find the command
174 corresponding to it. This can be ambiguous, full, incomplete,
176 static int process(char *s, char ** next, command_t *lookup,
177 command_t **result, char **prev)
179 *result = find_cmd(s, lookup, next);
186 /* found entry: is it ambigous, i.e. not exact command name and
187 more than one command in the list matches. Note that find_cmd
188 points to the first ambiguous entry */
189 if (strncasecmp(s, (*result)->pc_name, strlen((*result)->pc_name))) {
191 command_t *another_result = find_cmd(s, (*result) + 1,
193 int found_another = 0;
195 while (another_result) {
196 if (strncasecmp(s, another_result->pc_name,
197 strlen(another_result->pc_name)) == 0){
198 *result = another_result;
199 *next = another_next;
202 another_result = find_cmd(s, another_result + 1,
211 /* found a unique command: component or full? */
212 if ( (*result)->pc_func ) {
215 if ( *next == '\0' ) {
216 return CMD_INCOMPLETE;
218 return process(*next, next, (*result)->pc_sub_cmd,
224 #ifdef HAVE_LIBREADLINE
225 static command_t * match_tbl; /* Command completion against this table */
226 static char * command_generator(const char * text, int state)
232 /* Do we have a match table? */
236 /* If this is the first time called on this word, state is 0 */
239 len = (int)strlen(text);
242 /* Return next name in the command list that paritally matches test */
243 while ( (name = (match_tbl + index)->pc_name) ) {
246 if (strncasecmp(name, text, len) == 0) {
247 return(strdup(name));
251 /* No more matches */
255 /* probably called by readline */
256 static char **command_completion(char * text, int start, int end)
261 match_tbl = top_level;
263 for (table = find_cmd(rl_line_buffer, match_tbl, &pos);
264 table; table = find_cmd(pos, match_tbl, &pos))
267 if (*(pos - 1) == ' ') match_tbl = table->pc_sub_cmd;
270 return completion_matches(text, command_generator);
274 /* take a string and execute the function or print help */
275 int execute_line(char * line)
277 command_t *cmd, *ambig;
284 switch (process(line, &next, top_level, &cmd, &prev)) {
286 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
287 while( (ambig = find_cmd(prev, cmd, &tmp)) ) {
288 fprintf(stderr, "%s ", ambig->pc_name);
291 fprintf(stderr, "\n");
294 fprintf(stderr, "No such command, type help\n");
298 "'%s' incomplete command. Use '%s x' where x is one of:\n",
300 fprintf(stderr, "\t");
301 for (i = 0; cmd->pc_sub_cmd[i].pc_name; i++) {
302 fprintf(stderr, "%s ", cmd->pc_sub_cmd[i].pc_name);
304 fprintf(stderr, "\n");
307 i = line2args(line, argv, MAXARGS);
308 rc = (cmd->pc_func)(i, argv);
311 fprintf(stderr, "%s\n", cmd->pc_help);
325 /* just in case you're ever in an airplane and discover you
326 forgot to install readline-dev. :) */
329 int interactive = isatty (fileno (stdin));
331 #ifdef HAVE_LIBREADLINE
333 stifle_history(HISTORY);
337 rl_prep_term_function = (rl_vintfunc_t *)noop_fn;
338 rl_deprep_term_function = (rl_voidfunc_t *)noop_fn;
341 rl_attempted_completion_function = (CPPFunction *)command_completion;
342 rl_completion_entry_function = (void *)command_generator;
347 #ifndef HAVE_LIBREADLINE
348 #define add_history(s)
349 char * readline(char * prompt)
352 char *line = malloc(size);
360 printf ("%s", prompt);
363 if ((c = fgetc(stdin)) != EOF) {
368 if (ptr - line >= size - 1) {
375 memcpy(tmp, line, ptr - line);
376 ptr = tmp + (ptr - line);
382 if (ferror(stdin) || feof(stdin))
389 if (eof && (strlen(line) == 0)) {
400 /* this is the command execution machine */
401 int Parser_commands(void)
404 int rc = 0, save_error = 0;
407 interactive = init_input();
410 line = readline(interactive ? parser_prompt : NULL);
414 s = skipwhitespace(line);
418 rc = execute_line(s);
420 /* stop on error if not-interactive */
421 if (rc != 0 && !interactive) {
436 /* sets the parser prompt */
437 void Parser_init(char * prompt, command_t * cmds)
441 if (parser_prompt) free(parser_prompt);
442 parser_prompt = strdup(prompt);
445 /* frees the parser prompt */
446 void Parser_exit(int argc, char *argv[])
450 parser_prompt = NULL;
453 /* convert a string to an integer */
454 int Parser_int(char *s, int *val)
459 ret = sscanf(s, "%d", val);
460 else if (*(s+1) != 'x')
461 ret = sscanf(s, "%o", val);
464 ret = sscanf(++s, "%x", val);
471 void Parser_qhelp(int argc, char *argv[]) {
473 printf("Available commands are:\n");
475 print_commands(NULL, top_level);
476 printf("For more help type: help command-name\n");
479 int Parser_help(int argc, char **argv)
482 char *next, *prev, *tmp;
483 command_t *result, *ambig;
487 Parser_qhelp(argc, argv);
492 for ( i = 1 ; i < argc ; i++ ) {
493 strcat(line, argv[i]);
496 switch ( process(line, &next, top_level, &result, &prev) ) {
498 fprintf(stderr, "%s: %s\n",line, result->pc_help);
501 fprintf(stderr, "%s: Unknown command.\n", line);
505 "'%s' incomplete command. Use '%s x' where x is one of:\n",
507 fprintf(stderr, "\t");
508 for (i = 0; result->pc_sub_cmd[i].pc_name; i++) {
509 fprintf(stderr, "%s ", result->pc_sub_cmd[i].pc_name);
511 fprintf(stderr, "\n");
514 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
515 while( (ambig = find_cmd(prev, result, &tmp)) ) {
516 fprintf(stderr, "%s ", ambig->pc_name);
519 fprintf(stderr, "\n");
526 void Parser_printhelp(char *cmd)
528 char *argv[] = { "help", cmd };
529 Parser_help(2, argv);
533 /*************************************************************************
535 *************************************************************************/
536 static void print_commands(char * str, command_t * table) {
540 for (cmds = table; cmds->pc_name; cmds++) {
542 if (str) printf("\t%s %s\n", str, cmds->pc_name);
543 else printf("\t%s\n", cmds->pc_name);
545 if (cmds->pc_sub_cmd) {
547 sprintf(buf, "%s %s", str, cmds->pc_name);
548 print_commands(buf, cmds->pc_sub_cmd);
550 print_commands(cmds->pc_name, cmds->pc_sub_cmd);
556 char *Parser_getstr(const char *prompt, const char *deft, char *res,
560 int size = strlen(prompt) + strlen(deft) + 8;
562 theprompt = malloc(size);
565 sprintf(theprompt, "%s [%s]: ", prompt, deft);
567 line = readline(theprompt);
570 if ( line == NULL || *line == '\0' ) {
571 strncpy(res, deft, len);
573 strncpy(res, line, len);
584 /* get integer from prompt, loop forever to get it */
585 int Parser_getint(const char *prompt, long min, long max, long deft, int base)
590 int size = strlen(prompt) + 40;
591 char *theprompt = malloc(size);
593 sprintf(theprompt,"%s [%ld, (0x%lx)]: ", prompt, deft, deft);
599 line = readline(theprompt);
601 fprintf(stdout, "Please enter an integer.\n");
605 if ( *line == '\0' ) {
610 rc = Parser_arg2int(line, &result, base);
613 fprintf(stdout, "Invalid string.\n");
615 } else if ( result > max || result < min ) {
616 fprintf(stdout, "Error: response must lie between %ld and %ld.\n",
630 /* get boolean (starting with YyNn; loop forever */
631 int Parser_getbool(const char *prompt, int deft)
635 int size = strlen(prompt) + 8;
636 char *theprompt = malloc(size);
641 if ( deft != 0 && deft != 1 ) {
642 fprintf(stderr, "Error: Parser_getbool given bad default %d\n",
646 sprintf(theprompt, "%s [%s]: ", prompt, (deft==0)? "N" : "Y");
650 line = readline(theprompt);
651 if ( line == NULL ) {
655 if ( *line == '\0' ) {
659 if ( *line == 'y' || *line == 'Y' ) {
663 if ( *line == 'n' || *line == 'N' ) {
669 fprintf(stdout, "Invalid string. Must start with yY or nN\n");
680 /* parse int out of a string or prompt for it */
681 long Parser_intarg(const char *inp, const char *prompt, int deft,
682 int min, int max, int base)
687 rc = Parser_arg2int(inp, &result, base);
692 return Parser_getint(prompt, deft, min, max, base);
696 /* parse int out of a string or prompt for it */
697 char *Parser_strarg(char *inp, const char *prompt, const char *deft,
698 char *answer, int len)
700 if ( inp == NULL || *inp == '\0' ) {
701 return Parser_getstr(prompt, deft, answer, len);
706 /* change a string into a number: return 0 on success. No invalid characters
707 allowed. The processing of base and validity follows strtol(3)*/
708 int Parser_arg2int(const char *inp, long *result, int base)
712 if ( (base !=0) && (base < 2 || base > 36) )
715 *result = strtol(inp, &endptr, base);
717 if ( *inp != '\0' && *endptr == '\0' )
723 /* Convert human readable size string to and int; "1k" -> 1000 */
724 int Parser_size (int *sizep, char *str) {
728 switch (sscanf (str, "%d%1[gGmMkK]", &size, mod)) {
760 /* Convert a string boolean to an int; "enable" -> 1 */
761 int Parser_bool (int *b, char *str) {
762 if (!strcasecmp (str, "no") ||
763 !strcasecmp (str, "n") ||
764 !strcasecmp (str, "off") ||
765 !strcasecmp (str, "down") ||
766 !strcasecmp (str, "disable"))
772 if (!strcasecmp (str, "yes") ||
773 !strcasecmp (str, "y") ||
774 !strcasecmp (str, "on") ||
775 !strcasecmp (str, "up") ||
776 !strcasecmp (str, "enable"))
785 int Parser_quit(int argc, char **argv)