From: Goffredo Baroncelli Subject: Re: [PATCH 2/3] Btrfs-progs: implement new subcommand parser Date: Sat, 4 Feb 2012 13:54:23 +0100 Message-ID: <201202041354.23686.kreijack@libero.it> References: <1328304240-26100-1-git-send-email-idryomov@gmail.com> <1328304240-26100-3-git-send-email-idryomov@gmail.com> Reply-To: kreijack@libero.it Mime-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-15" To: Ilya Dryomov , linux-btrfs@vger.kernel.org Return-path: In-Reply-To: <1328304240-26100-3-git-send-email-idryomov@gmail.com> List-ID: Hi Ilya On Friday, 03 February, 2012 22:23:59 you wrote: > This completely replaces the existing subcommand infrastructure, which > is not flexible enough to accomodate our needs. Could you explain what you means with "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. The same could be done with the actual system. For example if you want to to handle the "filesystem balance" subcommands family, you could handle every syntax you want with changing the function do_balance; without forcing everybody to develop a "group handlers". Of course a bit of work should be done on the handling the help. > 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. I agree with you that the current systema has somes problems; the first is the help which should be improved. For example, using the syntax of the C we can extend the struct Command with some additional fields: - (*help_handler)(); /* if != NULL -> generic handler for the command help */ - min_args; /* if != -1 -> minimum arguments */ - max_args; /* if != -1 -> maximum arguments */ A command definition could became: { function, 0, "cmd sub_cmd sub_sub_cmd", .help_handler = my_help_function, .min_args = 10, .max_args = 20, } what do you think > > Signed-off-by: Ilya Dryomov > --- > Makefile | 4 +- > btrfs.c | 574 > ++++++++++++++++++++---------------------------------------- commands.h | > 80 +++++++++ > help.c | 207 ++++++++++++++++++++++ > 4 files changed, 479 insertions(+), 386 deletions(-) > create mode 100644 commands.h > create mode 100644 help.c > > diff --git a/Makefile b/Makefile > index 0d47bd5..deafda6 100644 > --- a/Makefile > +++ b/Makefile > @@ -38,8 +38,8 @@ all: version $(progs) manpages > version: > bash version.sh > > -btrfs: $(objects) btrfs.o common.o $(cmds_objects) > - $(CC) $(CFLAGS) -o btrfs btrfs.o common.o $(cmds_objects) \ > +btrfs: $(objects) btrfs.o help.o common.o $(cmds_objects) > + $(CC) $(CFLAGS) -o btrfs btrfs.o help.o common.o $(cmds_objects) \ > $(objects) $(LDFLAGS) $(LIBS) -lpthread > > calc-size: $(objects) calc-size.o > diff --git a/btrfs.c b/btrfs.c > index 1def354..7cff30b 100644 > --- a/btrfs.c > +++ b/btrfs.c > @@ -19,444 +19,250 @@ > #include > #include > > -#include "kerncompat.h" > -#include "btrfs_cmds.h" > +#include "commands.h" > #include "version.h" > > -#define BASIC_HELP 0 > -#define ADVANCED_HELP 1 > - > -typedef int (*CommandFunction)(int argc, char **argv); > - > -struct Command { > - CommandFunction func; /* function which implements the command */ > - int nargs; /* if == 999, any number of arguments > - if >= 0, number of arguments, > - if < 0, _minimum_ number of arguments */ > - char *verb; /* verb */ > - char *help; /* help lines; from the 2nd line onward they > - are automatically indented */ > - char *adv_help; /* advanced help message; from the 2nd > line - onward they are automatically > indented */ - > - /* the following fields are run-time filled by the program */ > - char **cmds; /* array of subcommands */ > - int ncmds; /* number of subcommand */ > -}; > +static const char btrfs_cmd_group_usage[] = > + "btrfs [--help] [--version] [...] []"; > > -static struct Command commands[] = { > +static const char btrfs_cmd_group_info[] = > + "Use --help as an argument for information on a specific group or > command."; > > - /* > - avoid short commands different for the case only > - */ > - { do_clone, -2, > - "subvolume snapshot", "[-r] [/]\n" > - "Create a writable/readonly snapshot of the subvolume with\n" > - "the name in the directory.", > - NULL > - }, > - { do_delete_subvolume, 1, > - "subvolume delete", "\n" > - "Delete the subvolume .", > - NULL > - }, > - { do_create_subvol, 1, > - "subvolume create", "[/]\n" > - "Create a subvolume in (or the current directory if\n" > - "not passed).", > - NULL > - }, > - { do_subvol_list, -1, "subvolume list", "[-p] \n" > - "List the snapshot/subvolume of a filesystem.", > - "[-p] \n" > - "List the snapshot/subvolume of a filesystem.\n" > - "-p print parent ID" > - }, > - { do_set_default_subvol, 2, > - "subvolume set-default", " \n" > - "Set the subvolume of the filesystem which will be mounted\n" > - "as default.", > - NULL > - }, > - { do_find_newer, 2, "subvolume find-new", " \n" > - "List the recently modified files in a filesystem.", > - NULL > - }, > - { do_defrag, -1, > - "filesystem defragment", "[-vf] [-c[zlib,lzo]] [-s start] [-l len] [-t > size] | [|...]\n" - "Defragment a file or a > directory.", > - "[-vcf] [-s start] [-l len] [-t size] | > [|...]\n" - "Defragment file data or directory metadata. \n" > - "-v be verbose\n" > - "-c compress the file while defragmenting\n" > - "-f flush data to disk immediately after defragmenting\n" > - "-s start defragment only from byte onward\n" > - "-l len defragment only up to len bytes\n" > - "-t size minimal size of file to be considered for defragmenting\n" > - }, > - { do_get_default_subvol, 1, "subvolume get-default", "\n" > - "Get the default subvolume of a filesystem." > - }, > - { do_fssync, 1, > - "filesystem sync", "\n" > - "Force a sync on the filesystem .", > - NULL > - }, > - { do_resize, 2, > - "filesystem resize", "[+/-][gkm]|max \n" > - "Resize the file system. If 'max' is passed, the filesystem\n" > - "will occupe all available space on the device.", > - NULL > - }, > - { do_show_filesystem, 999, > - "filesystem show", "[--all-devices][|