Whamcloud - gitweb
LU-16723 libcfs: refactor parser to be simpler 83/50583/19
authorTimothy Day <timday@amazon.com>
Sat, 8 Apr 2023 00:44:30 +0000 (00:44 +0000)
committerOleg Drokin <green@whamcloud.com>
Wed, 31 May 2023 19:12:55 +0000 (19:12 +0000)
The parser code has a lot of unused complexity that can be
streamlined. Refactor the parser makes the interface simpler
from a development perspective and can provide a consistent
user experience for all of the lustre utilities.

All functions which are not used outside of the parser have
been made static. Functions which don't appear to be used at
all have been removed.

The file headers have been standardized and the SDX text
added. The function names have been changed to be more standard.

Test-Parameters: testlist=sanity env=ONLY=60a,ONLY_REPEAT=10
Signed-off-by: Timothy Day <timday@amazon.com>
Change-Id: I3354f213de424f51aef94c840083a4cb781d7aae
Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/50583
Tested-by: jenkins <devops@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
Reviewed-by: Arshad Hussain <arshad.hussain@aeoncomputing.com>
Reviewed-by: Oleg Drokin <green@whamcloud.com>
libcfs/include/libcfs/util/parser.h
libcfs/include/libcfs/util/string.h
libcfs/libcfs/util/parser.c
lnet/utils/lnetctl.c
lnet/utils/lst.c
lustre/utils/lctl.c
lustre/utils/lfs.c
lustre/utils/llsom_sync.c
lustre/utils/obd.c

index 7919071..62eb5f3 100644 (file)
@@ -1,30 +1,13 @@
-/*
- * GPL HEADER START
- *
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 only,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License version 2 for more details (a copy is included
- * in the LICENSE file that accompanied this code).
- *
- * You should have received a copy of the GNU General Public License
- * version 2 along with this program; If not, see
- * http://www.gnu.org/licenses/gpl-2.0.html
- *
- * GPL HEADER END
- */
+// SPDX-License-Identifier: GPL-2.0
+
 /*
  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
  * Copyright (c) 2014, 2017, Intel Corporation.
+ *
  */
+
 /*
  * This file is part of Lustre, http://www.lustre.org/
  *
@@ -37,7 +20,7 @@
 #ifndef _PARSER_H_
 #define _PARSER_H_
 
-#define HISTORY        100             /* Don't let history grow unbounded    */
+#define HISTORY        100
 #define MAXARGS 512
 
 #define CMD_COMPLETE   0
@@ -65,48 +48,6 @@ typedef struct network {
        int     port;
 } network_t;
 
-int Parser_quit(int argc, char **argv);
-int Parser_version(int argc, char **argv);
-void Parser_init(char *, command_t *); /* Set prompt and load command list */
-int Parser_commands(void);                     /* Start the command parser */
-void Parser_qhelp(int, char **);       /* Quick help routine */
-int Parser_help(int, char **);         /* Detailed help routine */
-void Parser_ignore_errors(int ignore); /* Set the ignore errors flag */
-void Parser_printhelp(char *);         /* Detailed help routine */
-void Parser_exit(int, char **);                /* Shuts down command parser */
-int Parser_execarg(int argc, char **argv, command_t cmds[]);
-int Parser_list_commands(const command_t *cmdlist, int line_len,
-                        int col_num);
-
-/* Converts a string to an integer */
-int Parser_int(char *, int *);
-
-/* Prompts for a string, with default values and a maximum length */
-char *Parser_getstr(const char *prompt, const char *deft, char *res, 
-                   size_t len);
-
-/* Prompts for an integer, with minimum, maximum and default values and base */
-int Parser_getint(const char *prompt, long min, long max, long deft,
-                 int base);
-
-/* Prompts for a yes/no, with default */
-int Parser_getbool(const char *prompt, int deft);
-
-/* Extracts an integer from a string, or prompts if it cannot get one */
-long Parser_intarg(const char *inp, const char *prompt, int deft,
-                  int min, int max, int base);
-
-/* Extracts a word from the input, or propmts if it cannot get one */
-char *Parser_strarg(char *inp, const char *prompt, const char *deft,
-                   char *answer, int len);
-
-/* Extracts an integer from a string  with a base */
-int Parser_arg2int(const char *inp, long *result, int base);
-
-/* Convert human readable size string to and int; "1k" -> 1000 */
-int Parser_size(unsigned long *sizep, char *str);
-
-/* Convert a string boolean to an int; "enable" -> 1 */
-int Parser_bool(int *b, char *str);
+int cfs_parser(int argc, char **argv, command_t cmds[]);
 
 #endif
index d44fd2f..3ad5a95 100644 (file)
@@ -126,6 +126,7 @@ int cfs_expr_list_values(struct cfs_expr_list *expr_list, int max, __u32 **valpp
 int cfs_gettok(struct cfs_lstr *next, char delim, struct cfs_lstr *res);
 int cfs_str2num_check(char *str, int nob, unsigned *num,
                      unsigned min, unsigned max);
+int cfs_human_readable2long(unsigned long *sizep, char *str);
 int cfs_expr2str(struct list_head *list, char *str, size_t size);
 int cfs_expr_list_match(__u32 value, struct cfs_expr_list *expr_list);
 int cfs_expr_list_print(char *buffer, int count,
index 44887e9..20398c8 100644 (file)
@@ -1,22 +1,18 @@
+// SPDX-License-Identifier: GPL-2.0
+
 /*
  * Copyright (C) 2001 Cluster File Systems, Inc.
  *
  * Copyright (c) 2014, 2017, Intel Corporation.
  *
- *   This file is part of Lustre, http://www.sf.net/projects/lustre/
- *
- *   Lustre is free software; you can redistribute it and/or
- *   modify it under the terms of version 2 of the GNU General Public
- *   License as published by the Free Software Foundation.
+ */
+
+/*
+ * This file is part of Lustre, http://www.lustre.org/
  *
- *   Lustre is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
+ * libcfs/libcfs/parser.c
  *
- *   You should have received a copy of the GNU General Public License
- *   along with Lustre; if not, write to the Free Software
- *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * A command line parser.
  *
  */
 
 #include <libcfs/util/parser.h>
 #include <linux/lustre/lustre_ver.h>
 
-/* Top level of commands, initialized by InitParser */
+/* Top level of commands */
 static command_t *top_level;
-/* Parser prompt, set by InitParser */
-static char *parser_prompt;
 /* Set to 1 if user types exit or quit */
 static int done;
 /*
@@ -50,12 +44,43 @@ static int done;
  */
 static int ignore_errors;
 
-/* static functions */
 static char *skipwhitespace(char *s);
 static char *skiptowhitespace(char *s);
 static command_t *find_cmd(char *name, command_t cmds[], char **next);
 static int process(char *s, char **next, command_t *lookup, command_t **result,
                   char **prev);
+static int line2args(char *line, char **argv, int maxargs);
+static int cfs_parser_commands(command_t *cmds);
+static int cfs_parser_execarg(int argc, char **argv, command_t cmds[]);
+static int cfs_parser_list_commands(const command_t *cmdlist, int line_len,
+                               int col_num);
+static int cfs_parser_list(int argc, char **argv);
+static int cfs_parser_help(int argc, char **argv);
+static int cfs_parser_quit(int argc, char **argv);
+static int cfs_parser_version(int argc, char **argv);
+static int cfs_parser_ignore_errors(int argc, char **argv);
+
+command_t override_cmdlist[] = {
+       { .pc_name = "quit", .pc_func = cfs_parser_quit, .pc_help = "quit" },
+       { .pc_name = "exit", .pc_func = cfs_parser_quit, .pc_help = "exit" },
+       { .pc_name = "help", .pc_func = cfs_parser_help, .pc_help = "help" },
+       { .pc_name = "--help", .pc_func = cfs_parser_help, .pc_help = "help" },
+       { .pc_name = "version", .pc_func = cfs_parser_version,
+         .pc_help = "version" },
+       { .pc_name = "--version", .pc_func = cfs_parser_version,
+         .pc_help = "version" },
+       { .pc_name = "list-commands", .pc_func = cfs_parser_list,
+         .pc_help = "list" },
+       { .pc_name = "--list-commands", .pc_func = cfs_parser_list,
+         .pc_help = "list" },
+       { .pc_name = "--ignore_errors", .pc_func = cfs_parser_ignore_errors,
+         .pc_help = "ignore errors that occur during script processing\n"
+               "--ignore_errors"},
+       { .pc_name = "ignore_errors", .pc_func = cfs_parser_ignore_errors,
+         .pc_help = "ignore errors that occur during script processing\n"
+               "ignore_errors"},
+       { .pc_name = 0, .pc_func = NULL, .pc_help = 0 }
+};
 
 static char *skipwhitespace(char *s)
 {
@@ -93,7 +118,7 @@ static int line2args(char *line, char **argv, int maxargs)
 }
 
 /* find a command -- return it if unique otherwise print alternatives */
-static command_t *Parser_findargcmd(char *name, command_t cmds[])
+static command_t *cfs_parser_findargcmd(char *name, command_t cmds[])
 {
        command_t *cmd;
 
@@ -104,16 +129,40 @@ static command_t *Parser_findargcmd(char *name, command_t cmds[])
        return NULL;
 }
 
-void Parser_ignore_errors(int ignore)
+static int cfs_parser_ignore_errors(int argc, char **argv)
+{
+       (void) argc;
+       (void) argv;
+
+       ignore_errors = 1;
+
+       return 0;
+}
+
+int cfs_parser(int argc, char **argv, command_t cmds[])
 {
-       ignore_errors = ignore;
+       int rc = 0;
+
+       done = 0;
+       top_level = cmds;
+
+       if (argc > 1)
+               rc = cfs_parser_execarg(argc - 1, argv + 1, cmds);
+       else
+               rc = cfs_parser_commands(cmds);
+
+       return rc;
 }
 
-int Parser_execarg(int argc, char **argv, command_t cmds[])
+static int cfs_parser_execarg(int argc, char **argv, command_t cmds[])
 {
        command_t *cmd;
 
-       cmd = Parser_findargcmd(argv[0], cmds);
+       cmd = cfs_parser_findargcmd(argv[0], override_cmdlist);
+
+       if (!cmd)
+               cmd = cfs_parser_findargcmd(argv[0], cmds);
+
        if (cmd && cmd->pc_func) {
                int rc = cmd->pc_func(argc, argv);
 
@@ -123,17 +172,7 @@ int Parser_execarg(int argc, char **argv, command_t cmds[])
                }
                return rc;
        }
-       /*
-        * handle --help here as a synonym of --list-commands so that it can be
-        * applied to all commands without needing to individually add a --help
-        * option to each command.
-        * This does not apply to lctl which has its own --help option because
-        * `lctl --list-commands` uses a unique format.
-        */
-       if (strcmp(argv[0], "--help") == 0) {
-               Parser_list_commands(cmds, 80, 4);
-               return 0;
-       }
+
        fprintf(stderr,
                "%s: '%s' is not a valid command. See '%s --list-commands'.\n",
                program_invocation_short_name, argv[0],
@@ -165,6 +204,12 @@ static command_t *find_cmd(char *name, command_t cmds[], char **next)
        if (len == 0)
                return NULL;
 
+       for (i = 0; override_cmdlist[i].pc_name; i++) {
+               if (strncasecmp(name, override_cmdlist[i].pc_name, len) == 0) {
+                       *next = skipwhitespace(*next);
+                       return &override_cmdlist[i];
+               }
+       }
        for (i = 0; cmds[i].pc_name; i++) {
                if (strncasecmp(name, cmds[i].pc_name, len) == 0) {
                        *next = skipwhitespace(*next);
@@ -402,7 +447,7 @@ outfree:
 #endif
 
 /* this is the command execution machine */
-int Parser_commands(void)
+static int cfs_parser_commands(command_t *cmds)
 {
        char *line, *s;
        int rc = 0, save_error = 0;
@@ -411,7 +456,7 @@ int Parser_commands(void)
        interactive = init_input();
 
        while (!done) {
-               line = readline(interactive ? parser_prompt : NULL);
+               line = readline(interactive ? "> " : NULL);
 
                if (!line)
                        break;
@@ -438,52 +483,7 @@ int Parser_commands(void)
        return rc;
 }
 
-/* sets the parser prompt */
-void Parser_init(char *prompt, command_t *cmds)
-{
-       done = 0;
-       top_level = cmds;
-       if (parser_prompt)
-               free(parser_prompt);
-       parser_prompt = strdup(prompt);
-}
-
-/* frees the parser prompt */
-void Parser_exit(int argc, char *argv[])
-{
-       done = 1;
-       free(parser_prompt);
-       parser_prompt = NULL;
-}
-
-/* convert a string to an integer */
-int Parser_int(char *s, int *val)
-{
-       int ret;
-
-       if (*s != '0') {
-               ret = sscanf(s, "%d", val);
-       } else if (*(s + 1) != 'x') {
-               ret = sscanf(s, "%o", val);
-       } else {
-               s++;
-               ret = sscanf(++s, "%x", val);
-       }
-
-       return ret;
-}
-
-void Parser_qhelp(int argc, char *argv[])
-{
-       printf("usage: %s [COMMAND] [OPTIONS]... [ARGS]\n",
-              program_invocation_short_name);
-       printf("Without any parameters, interactive mode is invoked\n");
-
-       printf("Try '%s help <COMMAND>', or '%s --list-commands' for a list of commands.\n",
-               program_invocation_short_name, program_invocation_short_name);
-}
-
-int Parser_help(int argc, char **argv)
+static int cfs_parser_help(int argc, char **argv)
 {
        char line[1024];
        char *next, *prev, *tmp;
@@ -491,7 +491,12 @@ int Parser_help(int argc, char **argv)
        int i;
 
        if (argc == 1) {
-               Parser_qhelp(argc, argv);
+               printf("usage: %s [COMMAND] [OPTIONS]... [ARGS]\n",
+                       program_invocation_short_name);
+               printf("Without any parameters, interactive mode is invoked\n");
+               printf("Try '%s help <COMMAND>', or '%s --list-commands' for a list of commands.\n",
+                       program_invocation_short_name,
+                       program_invocation_short_name);
                return 0;
        }
 
@@ -542,17 +547,8 @@ int Parser_help(int argc, char **argv)
        return 0;
 }
 
-void Parser_printhelp(char *cmd)
-{
-       char *argv[] = { "help", cmd };
-
-       Parser_help(2, argv);
-}
-
-/* COMMANDS */
-
 /**
- * Parser_list_commands() - Output a list of the supported commands.
+ * cfs_parser_list_commands() - Output a list of the supported commands.
  * @cmdlist:     Array of structures describing the commands.
  * @line_len:    Length of output line.
  * @col_num:     The number of commands printed in a single row.
@@ -562,8 +558,8 @@ void Parser_printhelp(char *cmd)
  *
  * Return: The number of items that were printed.
  */
-int Parser_list_commands(const command_t *cmdlist, int line_len,
-                        int col_num)
+static int cfs_parser_list_commands(const command_t *cmdlist, int line_len,
+                               int col_num)
 {
        int char_max;
        int count = 0;
@@ -600,243 +596,47 @@ int Parser_list_commands(const command_t *cmdlist, int line_len,
        return count;
 }
 
-char *Parser_getstr(const char *prompt, const char *deft, char *res,
-                   size_t len)
+static int cfs_parser_quit(int argc, char **argv)
 {
-       char *line = NULL;
-       int size = strlen(prompt) + strlen(deft) + 8;
-       char *theprompt;
-
-       theprompt = malloc(size);
-       assert(theprompt);
-
-       snprintf(theprompt, size, "%s [%s]: ", prompt, deft);
-
-       line  = readline(theprompt);
-       free(theprompt);
-
-       /*
-        * The function strlcpy() cannot be used here because of
-        * this function is used in LNet utils that is not linked
-        * with libcfs.a.
-        */
-       if (!line || *line == '\0')
-               strncpy(res, deft, len);
-       else
-               strncpy(res, line, len);
-       res[len - 1] = '\0';
-
-       if (line) {
-               free(line);
-               return res;
-       }
-       return NULL;
-}
-
-/* get integer from prompt, loop forever to get it */
-int Parser_getint(const char *prompt, long min, long max, long deft, int base)
-{
-       int rc;
-       long result;
-       char *line;
-       int size = strlen(prompt) + 40;
-       char *theprompt = malloc(size);
-
-       assert(theprompt);
-       snprintf(theprompt, size, "%s [%ld, (0x%lx)]: ", prompt, deft, deft);
-       fflush(stdout);
-
-       do {
-               line = NULL;
-               line = readline(theprompt);
-               if (!line) {
-                       fprintf(stdout, "Please enter an integer.\n");
-                       fflush(stdout);
-                       continue;
-               }
-               if (*line == '\0') {
-                       free(line);
-                       result =  deft;
-                       break;
-               }
-               rc = Parser_arg2int(line, &result, base);
-               free(line);
-               if (rc != 0) {
-                       fprintf(stdout, "Invalid string.\n");
-                       fflush(stdout);
-               } else if (result > max || result < min) {
-                       fprintf(stdout,
-                               "Error: response must lie between %ld and %ld.\n",
-                               min, max);
-                       fflush(stdout);
-               } else {
-                       break;
-               }
-       } while (1);
-
-       if (theprompt)
-               free(theprompt);
-       return result;
-}
-
-/* get boolean (starting with YyNn; loop forever */
-int Parser_getbool(const char *prompt, int deft)
-{
-       int result = 0;
-       char *line;
-       int size = strlen(prompt) + 8;
-       char *theprompt = malloc(size);
-
-       assert(theprompt);
-       fflush(stdout);
-
-       if (deft != 0 && deft != 1) {
-               fprintf(stderr, "Error: Parser_getbool given bad default %d\n",
-                       deft);
-               assert(0);
-       }
-       snprintf(theprompt, size, "%s [%s]: ", prompt, (deft == 0) ? "N" : "Y");
-
-       do {
-               line = NULL;
-               line = readline(theprompt);
-               if (!line) {
-                       result = deft;
-                       break;
-               }
-               if (*line == '\0') {
-                       result = deft;
-                       break;
-               }
-               if (*line == 'y' || *line == 'Y') {
-                       result = 1;
-                       break;
-               }
-               if (*line == 'n' || *line == 'N') {
-                       result = 0;
-                       break;
-               }
-               if (line)
-                       free(line);
-               fprintf(stdout, "Invalid string. Must start with yY or nN\n");
-               fflush(stdout);
-       } while (1);
+       (void) argc;
+       (void) argv;
 
-       if (line)
-               free(line);
-       if (theprompt)
-               free(theprompt);
+       done = 1;
 
-       return result;
+       return 0;
 }
 
-/* parse int out of a string or prompt for it */
-long Parser_intarg(const char *inp, const char *prompt, int deft,
-                  int min, int max, int base)
+static int cfs_parser_version(int argc, char **argv)
 {
-       long result;
-       int rc;
+       (void) argc;
+       (void) argv;
 
-       rc = Parser_arg2int(inp, &result, base);
-       if (rc == 0)
-               return result;
-       else
-               return Parser_getint(prompt, deft, min, max, base);
-}
+       fprintf(stdout, "%s %s\n", program_invocation_short_name,
+               LUSTRE_VERSION_STRING);
 
-/* parse int out of a string or prompt for it */
-char *Parser_strarg(char *inp, const char *prompt, const char *deft,
-                   char *answer, int len)
-{
-       if (!inp || *inp == '\0')
-               return Parser_getstr(prompt, deft, answer, len);
-       else
-               return inp;
+       return 0;
 }
 
-/*
- * change a string into a number: return 0 on success. No invalid characters
- * allowed. The processing of base and validity follows strtol(3)
- */
-int Parser_arg2int(const char *inp, long *result, int base)
+static int cfs_parser_list(int argc, char **argv)
 {
-       char *endptr;
-
-       if ((base != 0) && (base < 2 || base > 36))
-               return 1;
-
-       *result = strtol(inp, &endptr, base);
+       command_t *cmd;
+       int num_cmds_listed;
 
-       if (*inp != '\0' && *endptr == '\0')
-               return 0;
-       else
-               return 1;
-}
+       (void) argc;
+       (void) argv;
 
-/* Convert human readable size string to and int; "1k" -> 1000 */
-int Parser_size(unsigned long *sizep, char *str)
-{
-       unsigned long size;
-       char mod[32];
-
-       switch (sscanf(str, "%lu%1[gGmMkK]", &size, mod)) {
-       default:
-               return -1;
-       case 1:
-               *sizep = size;
-               return 0;
-       case 2:
-               switch (*mod) {
-               case 'g':
-               case 'G':
-                       *sizep = size << 30;
-                       return 0;
-               case 'm':
-               case 'M':
-                       *sizep = size << 20;
-                       return 0;
-               case 'k':
-               case 'K':
-                       *sizep = size << 10;
-                       return 0;
-               default:
-                       *sizep = size;
-                       return 0;
+       cmd = top_level;
+       while (cmd->pc_name != NULL) {
+               if (!cmd->pc_func) {
+                       /*
+                        * print the command category
+                        */
+                       printf("\n%s\n", cmd->pc_name);
+                       cmd++;
                }
+               num_cmds_listed = cfs_parser_list_commands(cmd, 80, 4);
+               cmd += num_cmds_listed;
        }
-}
 
-/* Convert a string boolean to an int; "enable" -> 1 */
-int Parser_bool(int *b, char *str)
-{
-       if (!strcasecmp(str, "no") || !strcasecmp(str, "n") ||
-           !strcasecmp(str, "off") || !strcasecmp(str, "down") ||
-           !strcasecmp(str, "disable")) {
-               *b = 0;
-               return 0;
-       }
-
-       if (!strcasecmp(str, "yes") || !strcasecmp(str, "y") ||
-           !strcasecmp(str, "on") || !strcasecmp(str, "up") ||
-           !strcasecmp(str, "enable")) {
-               *b = 1;
-               return 0;
-       }
-
-       return -1;
-}
-
-int Parser_quit(int argc, char **argv)
-{
-       (void) argc;
-       (void) argv;
-       done = 1;
-       return 0;
-}
-
-int Parser_version(int argc, char **argv)
-{
-       fprintf(stdout, "%s %s\n", program_invocation_short_name,
-               LUSTRE_VERSION_STRING);
        return 0;
 }
index 18b49d2..f22103a 100644 (file)
@@ -71,7 +71,6 @@ static int jt_list_peer(int argc, char **argv);
 static int jt_add_udsp(int argc, char **argv);
 static int jt_del_udsp(int argc, char **argv);
 /*static int jt_show_peer(int argc, char **argv);*/
-static int lnetctl_list_commands(int argc, char **argv);
 static int jt_import(int argc, char **argv);
 static int jt_export(int argc, char **argv);
 static int jt_ping(int argc, char **argv);
@@ -121,10 +120,6 @@ command_t cmd_list[] = {
        {"cpt-of-nid", jt_calc_cpt_of_nid, 0, "Calculate the CPT associated with NID\n"
         "\t--nid: NID to calculate the CPT of\n"
         "\t--ncpt: Number of CPTs to consider in the calculation\n"},
-       {"help", Parser_help, 0, "help"},
-       {"exit", Parser_quit, 0, "quit"},
-       {"quit", Parser_quit, 0, "quit"},
-       {"--list-commands", lnetctl_list_commands, 0, "list commands"},
        { 0, 0, 0, NULL }
 };
 
@@ -2859,7 +2854,7 @@ static int jt_lnet(int argc, char **argv)
        if (rc)
                return rc;
 
-       return Parser_execarg(argc - 1, &argv[1], lnet_cmds);
+       return cfs_parser(argc, argv, lnet_cmds);
 }
 
 static int jt_route(int argc, char **argv)
@@ -2870,7 +2865,7 @@ static int jt_route(int argc, char **argv)
        if (rc)
                return rc;
 
-       return Parser_execarg(argc - 1, &argv[1], route_cmds);
+       return cfs_parser(argc, argv, route_cmds);
 }
 
 static int jt_net(int argc, char **argv)
@@ -2881,7 +2876,7 @@ static int jt_net(int argc, char **argv)
        if (rc)
                return rc;
 
-       return Parser_execarg(argc - 1, &argv[1], net_cmds);
+       return cfs_parser(argc, argv, net_cmds);
 }
 
 static int jt_routing(int argc, char **argv)
@@ -2892,7 +2887,7 @@ static int jt_routing(int argc, char **argv)
        if (rc)
                return rc;
 
-       return Parser_execarg(argc - 1, &argv[1], routing_cmds);
+       return cfs_parser(argc, argv, routing_cmds);
 }
 
 static int jt_stats(int argc, char **argv)
@@ -2903,7 +2898,7 @@ static int jt_stats(int argc, char **argv)
        if (rc)
                return rc;
 
-       return Parser_execarg(argc - 1, &argv[1], stats_cmds);
+       return cfs_parser(argc, argv, stats_cmds);
 }
 
 static int jt_debug(int argc, char **argv)
@@ -2914,7 +2909,7 @@ static int jt_debug(int argc, char **argv)
        if (rc)
                return rc;
 
-       return Parser_execarg(argc - 1, &argv[1], debug_cmds);
+       return cfs_parser(argc, argv, debug_cmds);
 }
 
 static int jt_global(int argc, char **argv)
@@ -2925,7 +2920,7 @@ static int jt_global(int argc, char **argv)
        if (rc)
                return rc;
 
-       return Parser_execarg(argc - 1, &argv[1], global_cmds);
+       return cfs_parser(argc, argv, global_cmds);
 }
 
 static int jt_peers(int argc, char **argv)
@@ -2936,7 +2931,7 @@ static int jt_peers(int argc, char **argv)
        if (rc)
                return rc;
 
-       return Parser_execarg(argc - 1, &argv[1], peer_cmds);
+       return cfs_parser(argc, argv, peer_cmds);
 }
 
 static int jt_set(int argc, char **argv)
@@ -2947,7 +2942,7 @@ static int jt_set(int argc, char **argv)
        if (rc)
                return rc;
 
-       return Parser_execarg(argc - 1, &argv[1], set_cmds);
+       return cfs_parser(argc, argv, set_cmds);
 }
 
 static int jt_udsp(int argc, char **argv)
@@ -2958,7 +2953,7 @@ static int jt_udsp(int argc, char **argv)
        if (rc)
                return rc;
 
-       return Parser_execarg(argc - 1, &argv[1], udsp_cmds);
+       return cfs_parser(argc, argv, udsp_cmds);
 }
 
 static int jt_import(int argc, char **argv)
@@ -3588,13 +3583,6 @@ static int jt_del_udsp(int argc, char **argv)
        return rc;
 }
 
-static int lnetctl_list_commands(int argc, char **argv)
-{
-       Parser_list_commands(cmd_list, 80, 4);
-
-       return 0;
-}
-
 int main(int argc, char **argv)
 {
        int rc = 0;
@@ -3608,14 +3596,5 @@ int main(int argc, char **argv)
                return rc;
        }
 
-       Parser_init("lnetctl > ", cmd_list);
-       if (argc > 1) {
-               rc = Parser_execarg(argc - 1, &argv[1], cmd_list);
-               goto errorout;
-       }
-
-       Parser_commands();
-
-errorout:
-       return rc;
+       return cfs_parser(argc, argv, cmd_list);
 }
index 4b66984..436897f 100644 (file)
@@ -55,7 +55,6 @@
 
 struct lst_sid LST_INVALID_SID = { .ses_nid = LNET_NID_ANY, .ses_stamp = -1 };
 static unsigned int session_key;
-static int lst_list_commands(int argc, char **argv);
 
 /* All nodes running 2.6.50 or later understand feature LST_FEAT_BULK_LEN */
 static unsigned int session_features = LST_FEATS_MASK;
@@ -361,7 +360,9 @@ lst_test_name2type(char *name)
 void
 lst_print_usage(char *cmd)
 {
-        Parser_printhelp(cmd);
+       char *argv[] = { "help", cmd };
+
+       cfs_parser(2, argv, NULL);
 }
 
 void
@@ -3952,8 +3953,6 @@ static command_t lst_cmdlist[] = {
         {"add_test",            jt_lst_add_test,        NULL,
          "Usage: lst add_test [--batch BATCH] [--loop #] [--concurrency #] "
          " [--distribute #:#] [--from GROUP] [--to GROUP] TEST..."                      },
-        {"help",                Parser_help,            0,     "help"                   },
-       {"--list-commands",     lst_list_commands,      0,     "list commands"          },
         {0,                     0,                      0,      NULL                    }
 };
 
@@ -3987,37 +3986,22 @@ lst_initialize(void)
         return 0;
 }
 
-static int lst_list_commands(int argc, char **argv)
-{
-       Parser_list_commands(lst_cmdlist, 80, 4);
-
-       return 0;
-}
-
-int
-main(int argc, char **argv)
+int main(int argc, char **argv)
 {
-        int rc = 0;
+       int rc = 0;
 
-        setlinebuf(stdout);
+       setlinebuf(stdout);
 
-        rc = lst_initialize();
-        if (rc < 0)
-                goto errorout;
+       rc = lst_initialize();
+       if (rc < 0)
+               goto errorout;
 
        rc = lustre_lnet_config_lib_init();
        if (rc < 0)
                goto errorout;
 
-        Parser_init("lst > ", lst_cmdlist);
-
-        if (argc != 1)  {
-                rc = Parser_execarg(argc - 1, argv + 1, lst_cmdlist);
-                goto errorout;
-        }
-
-        Parser_commands();
+       rc = cfs_parser(argc, argv, lst_cmdlist);
 
 errorout:
-        return rc;
+       return rc;
 }
index eee572f..3b4f145 100644 (file)
 #include <linux/lustre/lustre_ver.h>
 #include <lustre/lustreapi.h>
 
-static int lctl_list_commands(int argc, char **argv);
-
-static int jt_opt_ignore_errors(int argc, char **argv)
-{
-       Parser_ignore_errors(1);
-       return 0;
-}
-
-static int jt_pcc_list_commands(int argc, char **argv);
 static int jt_pcc(int argc, char **argv);
 
 /**
@@ -76,11 +67,6 @@ command_t pccdev_cmdlist[] = {
        { .pc_name = "list", .pc_func = jt_pcc_list,
          .pc_help = "List all PCC backends on a client.\n"
                "usage: lctl pcc list <mntpath>\n" },
-       { .pc_name = "--list-commands", .pc_func = jt_pcc_list_commands,
-         .pc_help = "list commands supported by lctl pcc"},
-       { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
-       { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
-       { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
        { .pc_help = NULL }
 };
 
@@ -90,28 +76,12 @@ command_t cmdlist[] = {
        {"--device", jt_opt_device, 0,
         "run <command> after connecting to device <devno>\n"
         "--device <devno> <command [args ...]>"},
-       {"--ignore_errors", jt_opt_ignore_errors, 0,
-        "ignore errors that occur during script processing\n"
-        "--ignore_errors"},
-       {"ignore_errors", jt_opt_ignore_errors, 0,
-        "ignore errors that occur during script processing\n"
-        "ignore_errors"},
 
        /* User interface commands */
        {"======== control =========", NULL, 0, "control commands"},
-       {"help", Parser_help, 0, "help"},
        {"lustre_build_version", jt_get_version, 0,
         "print version of Lustre modules\n"
         "usage: lustre_build_version"},
-       {"exit", Parser_quit, 0, "quit"},
-       {"quit", Parser_quit, 0, "quit"},
-       {"--version", Parser_version, 0,
-        "print build version of this utility and exit"},
-       {"--list-commands", lctl_list_commands, 0,
-        "list commands supported by this utility and exit"},
-       /* help is a synonym of --list-commands */
-       {"--help", lctl_list_commands, 0,
-        "list commands supported by this utility and exit"},
 
        /* Network configuration commands */
        {"===== network config =====", NULL, 0, "network config"},
@@ -593,22 +563,6 @@ command_t cmdlist[] = {
 };
 
 /**
- * jt_pcc_list_commands() - List lctl pcc commands.
- * @argc: The count of command line arguments.
- * @argv: Array of strings for command line arguments.
- *
- * This function lists lctl pcc commands defined in pccdev_cmdlist[].
- *
- * Return: 0 on success.
- */
-static int jt_pcc_list_commands(int argc, char **argv)
-{
-       Parser_list_commands(pccdev_cmdlist, 80, 4);
-
-       return 0;
-}
-
-/**
  * jt_pcc() - Parse and execute lctl pcc commands.
  * @argc: The count of lctl pcc command line arguments.
  * @argv: Array of strings for lctl pcc command line arguments.
@@ -625,15 +579,10 @@ static int jt_pcc(int argc, char **argv)
 
        setlinebuf(stdout);
 
-       Parser_init("lctl-pcc > ", pccdev_cmdlist);
-
        snprintf(cmd, sizeof(cmd), "%s %s", program_invocation_short_name,
                 argv[0]);
        program_invocation_short_name = cmd;
-       if (argc > 1)
-               rc = Parser_execarg(argc - 1, argv + 1, pccdev_cmdlist);
-       else
-               rc = Parser_commands();
+       rc = cfs_parser(argc, argv, pccdev_cmdlist);
 
        return rc < 0 ? -rc : rc;
 }
@@ -651,35 +600,12 @@ int lctl_main(int argc, char **argv)
        if (dbg_initialize(argc, argv) < 0)
                exit(3);
 
-       Parser_init("lctl > ", cmdlist);
-
-       if (argc > 1) {
-               llapi_set_command_name(argv[1]);
-               rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
-               llapi_clear_command_name();
-       } else {
-               rc = Parser_commands();
-       }
-
+       llapi_set_command_name(argv[1]);
+       rc = cfs_parser(argc, argv, cmdlist);
+       llapi_clear_command_name();
        obd_finalize(argc, argv);
-       Parser_exit(argc, argv);
-       return rc < 0 ? -rc : rc;
-}
 
-static int lctl_list_commands(int argc, char **argv)
-{
-       command_t *cmd;
-       int rc;
-
-       cmd = cmdlist;
-       while (cmd->pc_name != NULL) {
-               printf("\n%s\n", cmd->pc_name); /* Command category */
-               cmd++;
-               rc = Parser_list_commands(cmd, 80, 4);
-               cmd += rc;
-       }
-
-       return 0;
+       return rc < 0 ? -rc : rc;
 }
 
 int main(int argc, char **argv)
index 37d2e36..a632bf8 100644 (file)
@@ -124,8 +124,6 @@ static int lfs_getsom(int argc, char **argv);
 static int lfs_heat_get(int argc, char **argv);
 static int lfs_heat_set(int argc, char **argv);
 static int lfs_mirror(int argc, char **argv);
-static int lfs_mirror_list_commands(int argc, char **argv);
-static int lfs_list_commands(int argc, char **argv);
 static inline int lfs_mirror_resync(int argc, char **argv);
 static inline int lfs_mirror_verify(int argc, char **argv);
 static inline int lfs_mirror_read(int argc, char **argv);
@@ -137,7 +135,6 @@ static int lfs_pcc_detach(int argc, char **argv);
 static int lfs_pcc_detach_fid(int argc, char **argv);
 static int lfs_pcc_state(int argc, char **argv);
 static int lfs_pcc(int argc, char **argv);
-static int lfs_pcc_list_commands(int argc, char **argv);
 
 enum stats_flag {
        STATS_ON,
@@ -288,11 +285,6 @@ command_t mirror_cmdlist[] = {
          .pc_help = "Verify mirrored file(s).\n"
                "usage: lfs mirror verify [--only MIRROR_ID[,...]]\n"
                "\t\t[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n" },
-       { .pc_name = "--list-commands", .pc_func = lfs_mirror_list_commands,
-         .pc_help = "list commands supported by lfs mirror"},
-       { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
-       { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
-       { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
        { .pc_help = NULL }
 };
 
@@ -318,11 +310,6 @@ command_t pcc_cmdlist[] = {
        { .pc_name = "detach_fid", .pc_func = lfs_pcc_detach_fid,
          .pc_help = "Detach given files from PCC by FID(s).\n"
                "usage: lfs pcc detach_fid <mntpath> <fid>...\n" },
-       { .pc_name = "--list-commands", .pc_func = lfs_pcc_list_commands,
-         .pc_help = "list commands supported by lfs pcc"},
-       { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
-       { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
-       { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
        { .pc_help = NULL }
 };
 
@@ -594,13 +581,6 @@ command_t cmdlist[] = {
         "lfs pcc state  - display the PCC state for given files\n"
         "lfs pcc detach - detach given files from Persistent Client Cache\n"
         "lfs pcc detach_fid - detach given files from PCC by FID(s)\n"},
-       {"help", Parser_help, 0, "help"},
-       {"exit", Parser_quit, 0, "quit"},
-       {"quit", Parser_quit, 0, "quit"},
-       {"--version", Parser_version, 0,
-        "output build version of the utility and exit"},
-       {"--list-commands", lfs_list_commands, 0,
-        "list commands supported by the utility and exit"},
        { 0, 0, 0, NULL }
 };
 
@@ -12743,15 +12723,10 @@ static int lfs_mirror(int argc, char **argv)
 
        setlinebuf(stdout);
 
-       Parser_init("lfs-mirror > ", mirror_cmdlist);
-
        snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
        progname = cmd;
        program_invocation_short_name = cmd;
-       if (argc > 1)
-               rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
-       else
-               rc = Parser_commands();
+       rc = cfs_parser(argc, argv, mirror_cmdlist);
 
        return rc < 0 ? -rc : rc;
 }
@@ -12847,22 +12822,6 @@ static int lfs_getsom(int argc, char **argv)
        return 0;
 }
 
-/**
- * lfs_mirror_list_commands() - List lfs mirror commands.
- * @argc: The count of command line arguments.
- * @argv: Array of strings for command line arguments.
- *
- * This function lists lfs mirror commands defined in mirror_cmdlist[].
- *
- * Return: 0 on success.
- */
-static int lfs_mirror_list_commands(int argc, char **argv)
-{
-       Parser_list_commands(mirror_cmdlist, 80, 4);
-
-       return 0;
-}
-
 static int lfs_pcc_attach(int argc, char **argv)
 {
        struct option long_opts[] = {
@@ -13167,22 +13126,6 @@ static int lfs_pcc_state(int argc, char **argv)
 }
 
 /**
- * lfs_pcc_list_commands() - List lfs pcc commands.
- * @argc: The count of command line arguments.
- * @argv: Array of strings for command line arguments.
- *
- * This function lists lfs pcc commands defined in pcc_cmdlist[].
- *
- * Return: 0 on success.
- */
-static int lfs_pcc_list_commands(int argc, char **argv)
-{
-       Parser_list_commands(pcc_cmdlist, 80, 4);
-
-       return 0;
-}
-
-/**
  * lfs_pcc() - Parse and execute lfs pcc commands.
  * @argc: The count of lfs pcc command line arguments.
  * @argv: Array of strings for lfs pcc command line arguments.
@@ -13199,26 +13142,14 @@ static int lfs_pcc(int argc, char **argv)
 
        setlinebuf(stdout);
 
-       Parser_init("lfs-pcc > ", pcc_cmdlist);
-
        snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
        progname = cmd;
        program_invocation_short_name = cmd;
-       if (argc > 1)
-               rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
-       else
-               rc = Parser_commands();
+       rc = cfs_parser(argc, argv, pcc_cmdlist);
 
        return rc < 0 ? -rc : rc;
 }
 
-static int lfs_list_commands(int argc, char **argv)
-{
-       Parser_list_commands(cmdlist, 80, 4);
-
-       return 0;
-}
-
 int main(int argc, char **argv)
 {
        int rc;
@@ -13230,18 +13161,10 @@ int main(int argc, char **argv)
        setlinebuf(stdout);
        opterr = 0;
 
-       Parser_init("lfs > ", cmdlist);
-
        progname = program_invocation_short_name; /* Used in error messages */
-       if (argc > 1) {
-               llapi_set_command_name(argv[1]);
-               rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
-               llapi_clear_command_name();
-       } else {
-               rc = Parser_commands();
-       }
-
-       Parser_exit(argc, argv);
+       llapi_set_command_name(argv[1]);
+       rc = cfs_parser(argc, argv, cmdlist);
+       llapi_clear_command_name();
 
        return rc < 0 ? -rc : rc;
 }
index 7947c2f..b4d03c9 100644 (file)
@@ -49,7 +49,6 @@
 #include <linux/lustre/lustre_fid.h>
 #include <libcfs/util/hash.h>
 #include <libcfs/util/list.h>
-#include <libcfs/util/parser.h>
 
 #define container_of(ptr, type, member) ({                      \
        const typeof(((type *) 0)->member) * __mptr = (ptr);     \
@@ -58,6 +57,7 @@
 #define CHLG_POLL_INTV 60
 #define REC_MIN_AGE    600
 #define DEF_CACHE_SIZE (256 * 1048576) /* 256MB */
+#define ONE_MB 0x100000
 
 struct options {
        const char      *o_chlg_user;
@@ -389,14 +389,15 @@ static unsigned long get_fid_cache_size(int pct)
 
 int main(int argc, char **argv)
 {
-       int                      c;
-       int                      rc;
-       void                    *chglog_hdlr;
-       struct changelog_rec    *rec;
-       bool                     stop = 0;
-       int                      ret = 0;
-       unsigned long            cache_size = DEF_CACHE_SIZE;
-       char                     fsname[MAX_OBD_NAME + 1];
+       int c;
+       int rc;
+       void *chglog_hdlr;
+       struct changelog_rec *rec;
+       bool stop = 0;
+       int ret = 0;
+       unsigned long long cache_size = DEF_CACHE_SIZE;
+       char fsname[MAX_OBD_NAME + 1];
+       unsigned long long unit;
        static struct option options[] = {
                { "mdt", required_argument, NULL, 'm' },
                { "user", required_argument, 0, 'u'},
@@ -456,7 +457,8 @@ int main(int argc, char **argv)
                        }
                        break;
                case 'c':
-                       rc = Parser_size(&cache_size, optarg);
+                       unit = ONE_MB;
+                       rc = llapi_parse_size(optarg, &cache_size, &unit, 0);
                        if (rc < 0) {
                                rc = -EINVAL;
                                llapi_error(LLAPI_MSG_ERROR, rc,
@@ -469,7 +471,7 @@ int main(int argc, char **argv)
                         */
                        if (cache_size < 100)
                                cache_size = get_fid_cache_size(cache_size);
-                       llapi_printf(LLAPI_MSG_INFO, "Cache size: %lu\n",
+                       llapi_printf(LLAPI_MSG_INFO, "Cache size: %llu\n",
                                     cache_size);
                        break;
                case 'v':
index 118c02c..e1e1513 100644 (file)
@@ -596,7 +596,7 @@ int jt_opt_device(int argc, char **argv)
        rc = do_device("device", argv[1]);
 
        if (!rc)
-               rc = Parser_execarg(argc - 2, argv + 2, cmdlist);
+               rc = cfs_parser(argc - 1, argv + 1, cmdlist);
 
        ret = do_disconnect(argv[0], 0);
        if (!rc)
@@ -775,7 +775,7 @@ int jt_opt_net(int argc, char **argv)
        rc = jt_ptl_network(2, arg2);
 
        if (!rc)
-               rc = Parser_execarg(argc - 2, argv + 2, cmdlist);
+               rc = cfs_parser(argc - 1, argv + 1, cmdlist);
 
        return rc;
 }