From 8b4e3d8b5b9e3cb65b244fec55d93608d1a1e319 Mon Sep 17 00:00:00 2001 From: Ilya Dryomov Date: Fri, 3 Feb 2012 21:00:17 +0200 Subject: Btrfs-progs: implement new subcommand parser This completely replaces the existing subcommand infrastructure, which is not flexible enough to accomodate our needs. Instead of a global command table we now have per-level tables and command group handlers, which allows command-group-specific handling of options and subcommands. The new parser exports a clear interface and gets out of the way - all control over how matching is done is passed to commands and command group handlers. One side effect of this is that command implementors have to check the number of arguments themselves - patch to fix up all existing commands follows. Signed-off-by: Ilya Dryomov --- commands.h | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 commands.h (limited to 'commands.h') diff --git a/commands.h b/commands.h new file mode 100644 index 00000000..092b3687 --- /dev/null +++ b/commands.h @@ -0,0 +1,80 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License v2 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 for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 021110-1307, USA. + */ + +#define ARGV0_BUF_SIZE 64 + +struct cmd_struct { + const char *token; + int (*fn)(int, char **); + + /* + * Usage strings + * + * A NULL-terminated array of the following format: + * + * usagestr[0] - one-line synopsis (required) + * usagestr[1] - one-line short description (required) + * usagestr[2..m] - a long (possibly multi-line) description + * (optional) + * usagestr[m + 1] - an empty line separator (required if at least one + * option string is given, not needed otherwise) + * usagestr[m + 2..n] - option strings, one option per line + * (optional) + * usagestr[n + 1] - NULL terminator + * + * Options (if present) should always (even if there is no long + * description) be prepended with an empty line. Supplied strings are + * indented but otherwise printed as-is, no automatic wrapping is done. + * + * Grep for cmd_*_usage[] for examples. + */ + const char * const *usagestr; + + /* should be NULL if token is not a subgroup */ + const struct cmd_group *next; + + /* if true don't list this token in help listings */ + int hidden; +}; + +struct cmd_group { + const char *usagestr; + const char *infostr; + + const struct cmd_struct commands[]; +}; + +/* btrfs.c */ +int prefixcmp(const char *str, const char *prefix); + +int check_argc_exact(int nargs, int expected); +int check_argc_min(int nargs, int expected); +int check_argc_max(int nargs, int expected); + +int handle_command_group(const struct cmd_group *grp, int argc, + char **argv); + +/* help.c */ +extern const char * const generic_cmd_help_usage[]; + +void usage(const char * const *usagestr); +void usage_command(const struct cmd_struct *cmd, int full, int err); +void usage_command_group(const struct cmd_group *grp, int all, int err); + +void help_unknown_token(const char *arg, const struct cmd_group *grp); +void help_ambiguous_token(const char *arg, const struct cmd_group *grp); + +void help_command_group(const struct cmd_group *grp, int argc, char **argv); -- cgit v1.2.3