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.
31 #ifdef HAVE_LIBREADLINE
32 # include <readline/history.h>
33 # include <readline/readline.h>
34 #endif /* HAVE_LIBREADLINE */
38 #include <libcfs/util/parser.h>
39 #include <lustre_ver.h>
41 static command_t * top_level; /* Top level of commands, initialized by
43 static char * parser_prompt = NULL;/* Parser prompt, set by InitParser */
44 static int done; /* Set to 1 if user types exit or quit */
45 static int ignore_errors; /* Normally, the parser will quit when
46 an error occurs in non-interacive
47 mode. Setting this to non-zero will
48 force it to keep buggering on. */
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,
57 static void print_commands(char *str, command_t *table);
59 static char * skipwhitespace(char * s)
65 for (t = s; t <= s + len && isspace(*t); t++);
70 static char * skiptowhitespace(char * s)
74 for (t = s; *t && !isspace(*t); t++);
78 static int line2args(char *line, char **argv, int maxargs)
83 arg = strtok(line, " \t");
84 if (arg == NULL || maxargs < 1)
88 while ((arg = strtok(NULL, " \t")) != NULL && i < maxargs)
93 /* find a command -- return it if unique otherwise print alternatives */
94 static command_t *Parser_findargcmd(char *name, command_t cmds[])
98 for (cmd = cmds; cmd->pc_name; cmd++) {
99 if (strcmp(name, cmd->pc_name) == 0)
105 void Parser_ignore_errors(int ignore)
107 ignore_errors = ignore;
110 int Parser_execarg(int argc, char **argv, command_t cmds[])
114 cmd = Parser_findargcmd(argv[0], cmds);
116 int rc = (cmd->pc_func)(argc, argv);
118 fprintf(stderr, "%s\n", cmd->pc_help);
121 printf("Try interactive use without arguments or use one of:\n");
122 for (cmd = cmds; cmd->pc_name; cmd++)
123 printf("\"%s\"\n", cmd->pc_name);
124 printf("as argument.\n");
129 /* returns the command_t * (NULL if not found) corresponding to a
130 _partial_ match with the first token in name. It sets *next to
131 point to the following token. Does not modify *name. */
132 static command_t * find_cmd(char * name, command_t cmds[], char ** next)
139 /* This sets name to point to the first non-white space character,
140 and next to the first whitespace after name, len to the length: do
142 name = skipwhitespace(name);
143 *next = skiptowhitespace(name);
144 len = (int)(*next - name);
148 for (i = 0; cmds[i].pc_name; i++) {
149 if (strncasecmp(name, cmds[i].pc_name, len) == 0) {
150 *next = skipwhitespace(*next);
157 /* Recursively process a command line string s and find the command
158 corresponding to it. This can be ambiguous, full, incomplete,
160 static int process(char *s, char ** next, command_t *lookup,
161 command_t **result, char **prev)
163 *result = find_cmd(s, lookup, next);
170 /* found entry: is it ambigous, i.e. not exact command name and
171 more than one command in the list matches. Note that find_cmd
172 points to the first ambiguous entry */
173 if (strncasecmp(s, (*result)->pc_name, strlen((*result)->pc_name))) {
175 command_t *another_result = find_cmd(s, (*result) + 1,
177 int found_another = 0;
179 while (another_result) {
180 if (strncasecmp(s, another_result->pc_name,
181 strlen(another_result->pc_name)) == 0){
182 *result = another_result;
183 *next = another_next;
186 another_result = find_cmd(s, another_result + 1,
195 /* found a unique command: component or full? */
196 if ( (*result)->pc_func ) {
199 if ( *next == '\0' ) {
200 return CMD_INCOMPLETE;
202 return process(*next, next, (*result)->pc_sub_cmd,
208 #ifdef HAVE_LIBREADLINE
209 static command_t * match_tbl; /* Command completion against this table */
210 static char * command_generator(const char * text, int state)
216 /* Do we have a match table? */
220 /* If this is the first time called on this word, state is 0 */
223 len = (int)strlen(text);
226 /* Return next name in the command list that paritally matches test */
227 while ( (name = (match_tbl + index)->pc_name) ) {
230 if (strncasecmp(name, text, len) == 0) {
231 return(strdup(name));
235 /* No more matches */
239 /* probably called by readline */
240 static char **command_completion(const char *text, int start, int end)
245 match_tbl = top_level;
247 for (table = find_cmd(rl_line_buffer, match_tbl, &pos);
248 table; table = find_cmd(pos, match_tbl, &pos))
251 if (*(pos - 1) == ' ') match_tbl = table->pc_sub_cmd;
254 return rl_completion_matches(text, command_generator);
258 /* take a string and execute the function or print help */
259 int execute_line(char * line)
261 command_t *cmd, *ambig;
268 switch (process(line, &next, top_level, &cmd, &prev)) {
270 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
271 while ((ambig = find_cmd(prev, cmd, &tmp))) {
272 fprintf(stderr, "%s ", ambig->pc_name);
275 fprintf(stderr, "\n");
278 fprintf(stderr, "No such command, type help\n");
281 fprintf(stderr, "'%s' incomplete command. Use '%s x' where "
282 "x is one of:\n", line, line);
283 fprintf(stderr, "\t");
284 for (i = 0; cmd->pc_sub_cmd[i].pc_name; i++)
285 fprintf(stderr, "%s ", cmd->pc_sub_cmd[i].pc_name);
286 fprintf(stderr, "\n");
290 i = line2args(line, argv, MAXARGS);
291 rc = (cmd->pc_func)(i, argv);
294 fprintf(stderr, "%s\n", cmd->pc_help);
302 #ifdef HAVE_LIBREADLINE
303 static void noop_int_fn(int unused) { }
304 static void noop_void_fn(void) { }
307 /* just in case you're ever in an airplane and discover you
308 * forgot to install readline-dev. :) */
309 static int init_input(void)
311 int interactive = isatty(fileno(stdin));
313 #ifdef HAVE_LIBREADLINE
315 stifle_history(HISTORY);
318 rl_prep_term_function = noop_int_fn;
319 rl_deprep_term_function = noop_void_fn;
322 rl_attempted_completion_function = command_completion;
323 rl_completion_entry_function = command_generator;
328 #ifndef HAVE_LIBREADLINE
329 #define add_history(s)
330 char * readline(char * prompt)
333 char *line = malloc(size);
341 printf ("%s", prompt);
344 if ((c = fgetc(stdin)) != EOF) {
349 if (ptr - line >= size - 1) {
356 memcpy(tmp, line, ptr - line);
357 ptr = tmp + (ptr - line);
363 if (ferror(stdin) || feof(stdin))
370 if (eof && (strlen(line) == 0)) {
381 /* this is the command execution machine */
382 int Parser_commands(void)
385 int rc = 0, save_error = 0;
388 interactive = init_input();
391 line = readline(interactive ? parser_prompt : NULL);
395 s = skipwhitespace(line);
399 rc = execute_line(s);
401 /* stop on error if not-interactive */
402 if (rc != 0 && !interactive) {
417 /* sets the parser prompt */
418 void Parser_init(char * prompt, command_t * cmds)
422 if (parser_prompt) free(parser_prompt);
423 parser_prompt = strdup(prompt);
426 /* frees the parser prompt */
427 void Parser_exit(int argc, char *argv[])
431 parser_prompt = NULL;
434 /* convert a string to an integer */
435 int Parser_int(char *s, int *val)
440 ret = sscanf(s, "%d", val);
441 else if (*(s+1) != 'x')
442 ret = sscanf(s, "%o", val);
445 ret = sscanf(++s, "%x", val);
452 void Parser_qhelp(int argc, char *argv[]) {
454 printf("Available commands are:\n");
456 print_commands(NULL, top_level);
457 printf("For more help type: help command-name\n");
460 int Parser_help(int argc, char **argv)
463 char *next, *prev, *tmp;
464 command_t *result, *ambig;
468 Parser_qhelp(argc, argv);
472 /* Joining command line arguments without space is not critical here
473 * because of this string is used for search a help topic and assume
474 * that only one argument will be (the name of topic). For example:
475 * lst > help ping run
476 * pingrun: Unknown command. */
478 for (i = 1; i < argc; i++) {
479 if (strlen(argv[i]) >= sizeof(line) - strlen(line))
481 /* The function strlcat() cannot be used here because of
482 * this function is used in LNet utils that is not linked
484 strncat(line, argv[i], sizeof(line) - strlen(line));
487 switch ( process(line, &next, top_level, &result, &prev) ) {
489 fprintf(stderr, "%s: %s\n",line, result->pc_help);
492 fprintf(stderr, "%s: Unknown command.\n", line);
496 "'%s' incomplete command. Use '%s x' where x is one of:\n",
498 fprintf(stderr, "\t");
499 for (i = 0; result->pc_sub_cmd[i].pc_name; i++) {
500 fprintf(stderr, "%s ", result->pc_sub_cmd[i].pc_name);
502 fprintf(stderr, "\n");
505 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
506 while( (ambig = find_cmd(prev, result, &tmp)) ) {
507 fprintf(stderr, "%s ", ambig->pc_name);
510 fprintf(stderr, "\n");
517 void Parser_printhelp(char *cmd)
519 char *argv[] = { "help", cmd };
520 Parser_help(2, argv);
524 /*************************************************************************
526 *************************************************************************/
527 static void print_commands(char * str, command_t * table) {
531 for (cmds = table; cmds->pc_name; cmds++) {
533 if (str) printf("\t%s %s\n", str, cmds->pc_name);
534 else printf("\t%s\n", cmds->pc_name);
536 if (cmds->pc_sub_cmd) {
538 sprintf(buf, "%s %s", str, cmds->pc_name);
539 print_commands(buf, cmds->pc_sub_cmd);
541 print_commands(cmds->pc_name, cmds->pc_sub_cmd);
547 char *Parser_getstr(const char *prompt, const char *deft, char *res,
551 int size = strlen(prompt) + strlen(deft) + 8;
553 theprompt = malloc(size);
556 sprintf(theprompt, "%s [%s]: ", prompt, deft);
558 line = readline(theprompt);
561 /* The function strlcpy() cannot be used here because of
562 * this function is used in LNet utils that is not linked
564 if (line == NULL || *line == '\0')
565 strncpy(res, deft, len);
567 strncpy(res, line, len);
577 /* get integer from prompt, loop forever to get it */
578 int Parser_getint(const char *prompt, long min, long max, long deft, int base)
583 int size = strlen(prompt) + 40;
584 char *theprompt = malloc(size);
586 sprintf(theprompt,"%s [%ld, (0x%lx)]: ", prompt, deft, deft);
592 line = readline(theprompt);
594 fprintf(stdout, "Please enter an integer.\n");
598 if ( *line == '\0' ) {
603 rc = Parser_arg2int(line, &result, base);
606 fprintf(stdout, "Invalid string.\n");
608 } else if ( result > max || result < min ) {
609 fprintf(stdout, "Error: response must lie between %ld and %ld.\n",
623 /* get boolean (starting with YyNn; loop forever */
624 int Parser_getbool(const char *prompt, int deft)
628 int size = strlen(prompt) + 8;
629 char *theprompt = malloc(size);
634 if ( deft != 0 && deft != 1 ) {
635 fprintf(stderr, "Error: Parser_getbool given bad default %d\n",
639 sprintf(theprompt, "%s [%s]: ", prompt, (deft==0)? "N" : "Y");
643 line = readline(theprompt);
644 if ( line == NULL ) {
648 if ( *line == '\0' ) {
652 if ( *line == 'y' || *line == 'Y' ) {
656 if ( *line == 'n' || *line == 'N' ) {
662 fprintf(stdout, "Invalid string. Must start with yY or nN\n");
673 /* parse int out of a string or prompt for it */
674 long Parser_intarg(const char *inp, const char *prompt, int deft,
675 int min, int max, int base)
680 rc = Parser_arg2int(inp, &result, base);
685 return Parser_getint(prompt, deft, min, max, base);
689 /* parse int out of a string or prompt for it */
690 char *Parser_strarg(char *inp, const char *prompt, const char *deft,
691 char *answer, int len)
693 if ( inp == NULL || *inp == '\0' ) {
694 return Parser_getstr(prompt, deft, answer, len);
699 /* change a string into a number: return 0 on success. No invalid characters
700 allowed. The processing of base and validity follows strtol(3)*/
701 int Parser_arg2int(const char *inp, long *result, int base)
705 if ( (base !=0) && (base < 2 || base > 36) )
708 *result = strtol(inp, &endptr, base);
710 if ( *inp != '\0' && *endptr == '\0' )
716 /* Convert human readable size string to and int; "1k" -> 1000 */
717 int Parser_size (int *sizep, char *str) {
721 switch (sscanf (str, "%d%1[gGmMkK]", &size, mod)) {
753 /* Convert a string boolean to an int; "enable" -> 1 */
754 int Parser_bool (int *b, char *str) {
755 if (!strcasecmp (str, "no") ||
756 !strcasecmp (str, "n") ||
757 !strcasecmp (str, "off") ||
758 !strcasecmp (str, "down") ||
759 !strcasecmp (str, "disable"))
765 if (!strcasecmp (str, "yes") ||
766 !strcasecmp (str, "y") ||
767 !strcasecmp (str, "on") ||
768 !strcasecmp (str, "up") ||
769 !strcasecmp (str, "enable"))
778 int Parser_quit(int argc, char **argv)
786 int Parser_version(int argc, char **argv)
788 fprintf(stdout, "%s %s\n", program_invocation_short_name,
789 LUSTRE_VERSION_STRING);