2 * Copyright (C) 2001 Cluster File Systems, Inc.
4 * Copyright (c) 2014, Intel Corporation.
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.
23 #include <lustre_ver.h>
24 #include <libcfs/libcfsutil.h>
26 static command_t * top_level; /* Top level of commands, initialized by
28 static char * parser_prompt = NULL;/* Parser prompt, set by InitParser */
29 static int done; /* Set to 1 if user types exit or quit */
30 static int ignore_errors; /* Normally, the parser will quit when
31 an error occurs in non-interacive
32 mode. Setting this to non-zero will
33 force it to keep buggering on. */
36 /* static functions */
37 static char *skipwhitespace(char *s);
38 static char *skiptowhitespace(char *s);
39 static command_t *find_cmd(char *name, command_t cmds[], char **next);
40 static int process(char *s, char **next, command_t *lookup, command_t **result,
42 static void print_commands(char *str, command_t *table);
44 static char * skipwhitespace(char * s)
50 for (t = s; t <= s + len && isspace(*t); t++);
55 static char * skiptowhitespace(char * s)
59 for (t = s; *t && !isspace(*t); t++);
63 static int line2args(char *line, char **argv, int maxargs)
68 arg = strtok(line, " \t");
69 if (arg == NULL || maxargs < 1)
73 while ((arg = strtok(NULL, " \t")) != NULL && i < maxargs)
78 /* find a command -- return it if unique otherwise print alternatives */
79 static command_t *Parser_findargcmd(char *name, command_t cmds[])
83 for (cmd = cmds; cmd->pc_name; cmd++) {
84 if (strcmp(name, cmd->pc_name) == 0)
90 void Parser_ignore_errors(int ignore)
92 ignore_errors = ignore;
95 int Parser_execarg(int argc, char **argv, command_t cmds[])
99 cmd = Parser_findargcmd(argv[0], cmds);
101 int rc = (cmd->pc_func)(argc, argv);
103 fprintf(stderr, "%s\n", cmd->pc_help);
106 printf("Try interactive use without arguments or use one of:\n");
107 for (cmd = cmds; cmd->pc_name; cmd++)
108 printf("\"%s\"\n", cmd->pc_name);
109 printf("as argument.\n");
114 /* returns the command_t * (NULL if not found) corresponding to a
115 _partial_ match with the first token in name. It sets *next to
116 point to the following token. Does not modify *name. */
117 static command_t * find_cmd(char * name, command_t cmds[], char ** next)
124 /* This sets name to point to the first non-white space character,
125 and next to the first whitespace after name, len to the length: do
127 name = skipwhitespace(name);
128 *next = skiptowhitespace(name);
129 len = (int)(*next - name);
133 for (i = 0; cmds[i].pc_name; i++) {
134 if (strncasecmp(name, cmds[i].pc_name, len) == 0) {
135 *next = skipwhitespace(*next);
142 /* Recursively process a command line string s and find the command
143 corresponding to it. This can be ambiguous, full, incomplete,
145 static int process(char *s, char ** next, command_t *lookup,
146 command_t **result, char **prev)
148 *result = find_cmd(s, lookup, next);
155 /* found entry: is it ambigous, i.e. not exact command name and
156 more than one command in the list matches. Note that find_cmd
157 points to the first ambiguous entry */
158 if (strncasecmp(s, (*result)->pc_name, strlen((*result)->pc_name))) {
160 command_t *another_result = find_cmd(s, (*result) + 1,
162 int found_another = 0;
164 while (another_result) {
165 if (strncasecmp(s, another_result->pc_name,
166 strlen(another_result->pc_name)) == 0){
167 *result = another_result;
168 *next = another_next;
171 another_result = find_cmd(s, another_result + 1,
180 /* found a unique command: component or full? */
181 if ( (*result)->pc_func ) {
184 if ( *next == '\0' ) {
185 return CMD_INCOMPLETE;
187 return process(*next, next, (*result)->pc_sub_cmd,
193 #ifdef HAVE_LIBREADLINE
194 static command_t * match_tbl; /* Command completion against this table */
195 static char * command_generator(const char * text, int state)
201 /* Do we have a match table? */
205 /* If this is the first time called on this word, state is 0 */
208 len = (int)strlen(text);
211 /* Return next name in the command list that paritally matches test */
212 while ( (name = (match_tbl + index)->pc_name) ) {
215 if (strncasecmp(name, text, len) == 0) {
216 return(strdup(name));
220 /* No more matches */
224 /* probably called by readline */
225 static char **command_completion(const char *text, int start, int end)
230 match_tbl = top_level;
232 for (table = find_cmd(rl_line_buffer, match_tbl, &pos);
233 table; table = find_cmd(pos, match_tbl, &pos))
236 if (*(pos - 1) == ' ') match_tbl = table->pc_sub_cmd;
239 return completion_matches(text, command_generator);
243 /* take a string and execute the function or print help */
244 int execute_line(char * line)
246 command_t *cmd, *ambig;
253 switch (process(line, &next, top_level, &cmd, &prev)) {
255 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
256 while( (ambig = find_cmd(prev, cmd, &tmp)) ) {
257 fprintf(stderr, "%s ", ambig->pc_name);
260 fprintf(stderr, "\n");
263 fprintf(stderr, "No such command, type help\n");
267 "'%s' incomplete command. Use '%s x' where x is one of:\n",
269 fprintf(stderr, "\t");
270 for (i = 0; cmd->pc_sub_cmd[i].pc_name; i++) {
271 fprintf(stderr, "%s ", cmd->pc_sub_cmd[i].pc_name);
273 fprintf(stderr, "\n");
276 i = line2args(line, argv, MAXARGS);
277 rc = (cmd->pc_func)(i, argv);
280 fprintf(stderr, "%s\n", cmd->pc_help);
288 #ifdef HAVE_LIBREADLINE
289 static void noop_int_fn(int unused) { }
290 static void noop_void_fn(void) { }
293 /* just in case you're ever in an airplane and discover you
294 * forgot to install readline-dev. :) */
295 static int init_input(void)
297 int interactive = isatty(fileno(stdin));
299 #ifdef HAVE_LIBREADLINE
301 stifle_history(HISTORY);
304 rl_prep_term_function = noop_int_fn;
305 rl_deprep_term_function = noop_void_fn;
308 rl_attempted_completion_function = command_completion;
309 rl_completion_entry_function = command_generator;
314 #ifndef HAVE_LIBREADLINE
315 #define add_history(s)
316 char * readline(char * prompt)
319 char *line = malloc(size);
327 printf ("%s", prompt);
330 if ((c = fgetc(stdin)) != EOF) {
335 if (ptr - line >= size - 1) {
342 memcpy(tmp, line, ptr - line);
343 ptr = tmp + (ptr - line);
349 if (ferror(stdin) || feof(stdin))
356 if (eof && (strlen(line) == 0)) {
367 /* this is the command execution machine */
368 int Parser_commands(void)
371 int rc = 0, save_error = 0;
374 interactive = init_input();
377 line = readline(interactive ? parser_prompt : NULL);
381 s = skipwhitespace(line);
385 rc = execute_line(s);
387 /* stop on error if not-interactive */
388 if (rc != 0 && !interactive) {
403 /* sets the parser prompt */
404 void Parser_init(char * prompt, command_t * cmds)
408 if (parser_prompt) free(parser_prompt);
409 parser_prompt = strdup(prompt);
412 /* frees the parser prompt */
413 void Parser_exit(int argc, char *argv[])
417 parser_prompt = NULL;
420 /* convert a string to an integer */
421 int Parser_int(char *s, int *val)
426 ret = sscanf(s, "%d", val);
427 else if (*(s+1) != 'x')
428 ret = sscanf(s, "%o", val);
431 ret = sscanf(++s, "%x", val);
438 void Parser_qhelp(int argc, char *argv[]) {
440 printf("Available commands are:\n");
442 print_commands(NULL, top_level);
443 printf("For more help type: help command-name\n");
446 int Parser_help(int argc, char **argv)
449 char *next, *prev, *tmp;
450 command_t *result, *ambig;
454 Parser_qhelp(argc, argv);
458 /* Joining command line arguments without space is not critical here
459 * because of this string is used for search a help topic and assume
460 * that only one argument will be (the name of topic). For example:
461 * lst > help ping run
462 * pingrun: Unknown command. */
464 for (i = 1; i < argc; i++) {
465 if (strlen(argv[i]) >= sizeof(line) - strlen(line))
467 /* The function strlcat() cannot be used here because of
468 * this function is used in LNet utils that is not linked
470 strncat(line, argv[i], sizeof(line) - strlen(line));
473 switch ( process(line, &next, top_level, &result, &prev) ) {
475 fprintf(stderr, "%s: %s\n",line, result->pc_help);
478 fprintf(stderr, "%s: Unknown command.\n", line);
482 "'%s' incomplete command. Use '%s x' where x is one of:\n",
484 fprintf(stderr, "\t");
485 for (i = 0; result->pc_sub_cmd[i].pc_name; i++) {
486 fprintf(stderr, "%s ", result->pc_sub_cmd[i].pc_name);
488 fprintf(stderr, "\n");
491 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
492 while( (ambig = find_cmd(prev, result, &tmp)) ) {
493 fprintf(stderr, "%s ", ambig->pc_name);
496 fprintf(stderr, "\n");
503 void Parser_printhelp(char *cmd)
505 char *argv[] = { "help", cmd };
506 Parser_help(2, argv);
510 /*************************************************************************
512 *************************************************************************/
513 static void print_commands(char * str, command_t * table) {
517 for (cmds = table; cmds->pc_name; cmds++) {
519 if (str) printf("\t%s %s\n", str, cmds->pc_name);
520 else printf("\t%s\n", cmds->pc_name);
522 if (cmds->pc_sub_cmd) {
524 sprintf(buf, "%s %s", str, cmds->pc_name);
525 print_commands(buf, cmds->pc_sub_cmd);
527 print_commands(cmds->pc_name, cmds->pc_sub_cmd);
533 char *Parser_getstr(const char *prompt, const char *deft, char *res,
537 int size = strlen(prompt) + strlen(deft) + 8;
539 theprompt = malloc(size);
542 sprintf(theprompt, "%s [%s]: ", prompt, deft);
544 line = readline(theprompt);
547 /* The function strlcpy() cannot be used here because of
548 * this function is used in LNet utils that is not linked
550 if (line == NULL || *line == '\0')
551 strncpy(res, deft, len);
553 strncpy(res, line, len);
563 /* get integer from prompt, loop forever to get it */
564 int Parser_getint(const char *prompt, long min, long max, long deft, int base)
569 int size = strlen(prompt) + 40;
570 char *theprompt = malloc(size);
572 sprintf(theprompt,"%s [%ld, (0x%lx)]: ", prompt, deft, deft);
578 line = readline(theprompt);
580 fprintf(stdout, "Please enter an integer.\n");
584 if ( *line == '\0' ) {
589 rc = Parser_arg2int(line, &result, base);
592 fprintf(stdout, "Invalid string.\n");
594 } else if ( result > max || result < min ) {
595 fprintf(stdout, "Error: response must lie between %ld and %ld.\n",
609 /* get boolean (starting with YyNn; loop forever */
610 int Parser_getbool(const char *prompt, int deft)
614 int size = strlen(prompt) + 8;
615 char *theprompt = malloc(size);
620 if ( deft != 0 && deft != 1 ) {
621 fprintf(stderr, "Error: Parser_getbool given bad default %d\n",
625 sprintf(theprompt, "%s [%s]: ", prompt, (deft==0)? "N" : "Y");
629 line = readline(theprompt);
630 if ( line == NULL ) {
634 if ( *line == '\0' ) {
638 if ( *line == 'y' || *line == 'Y' ) {
642 if ( *line == 'n' || *line == 'N' ) {
648 fprintf(stdout, "Invalid string. Must start with yY or nN\n");
659 /* parse int out of a string or prompt for it */
660 long Parser_intarg(const char *inp, const char *prompt, int deft,
661 int min, int max, int base)
666 rc = Parser_arg2int(inp, &result, base);
671 return Parser_getint(prompt, deft, min, max, base);
675 /* parse int out of a string or prompt for it */
676 char *Parser_strarg(char *inp, const char *prompt, const char *deft,
677 char *answer, int len)
679 if ( inp == NULL || *inp == '\0' ) {
680 return Parser_getstr(prompt, deft, answer, len);
685 /* change a string into a number: return 0 on success. No invalid characters
686 allowed. The processing of base and validity follows strtol(3)*/
687 int Parser_arg2int(const char *inp, long *result, int base)
691 if ( (base !=0) && (base < 2 || base > 36) )
694 *result = strtol(inp, &endptr, base);
696 if ( *inp != '\0' && *endptr == '\0' )
702 /* Convert human readable size string to and int; "1k" -> 1000 */
703 int Parser_size (int *sizep, char *str) {
707 switch (sscanf (str, "%d%1[gGmMkK]", &size, mod)) {
739 /* Convert a string boolean to an int; "enable" -> 1 */
740 int Parser_bool (int *b, char *str) {
741 if (!strcasecmp (str, "no") ||
742 !strcasecmp (str, "n") ||
743 !strcasecmp (str, "off") ||
744 !strcasecmp (str, "down") ||
745 !strcasecmp (str, "disable"))
751 if (!strcasecmp (str, "yes") ||
752 !strcasecmp (str, "y") ||
753 !strcasecmp (str, "on") ||
754 !strcasecmp (str, "up") ||
755 !strcasecmp (str, "enable"))
764 int Parser_quit(int argc, char **argv)
772 int Parser_version(int argc, char **argv)
774 fprintf(stdout, "%s %s\n", program_invocation_short_name,
775 LUSTRE_VERSION_STRING);