[sheepdog] [PATCH v2] dog: let usage printing adopt nested subcommands
Hitoshi Mitake
mitake.hitoshi at gmail.com
Thu Nov 14 16:09:20 CET 2013
At Thu, 14 Nov 2013 23:58:23 +0900,
Hitoshi Mitake wrote:
>
> Signed-off-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
> ---
>
> v2: check depth of nested subcommands
>
> dog/common.c | 55 ++++++++++++++++++++++++++++++++++---------------------
> dog/dog.c | 21 ++++++++++++++-------
> dog/dog.h | 7 ++++++-
> 3 files changed, 54 insertions(+), 29 deletions(-)
I found a needless change in this patch. I'll send v3, sorry for annoying...
Thanks,
Hitoshi
>
> diff --git a/dog/common.c b/dog/common.c
> index 8c9b326..e15ccce 100644
> --- a/dog/common.c
> +++ b/dog/common.c
> @@ -224,36 +224,49 @@ int send_light_req(const struct node_id *nid, struct sd_req *hdr)
> return 0;
> }
>
> +int subcmd_depth = -1;
> +struct subcommand *subcmd_stack[MAX_SUBCMD_DEPTH];
> +
> int do_generic_subcommand(struct subcommand *sub, int argc, char **argv)
> {
> int i, ret;
> - static int depth = -1;
>
> - depth++;
> + if (subcmd_depth + 1 == MAX_SUBCMD_DEPTH) {
> + sd_err("Too deep netsted subcommands, "
> + "please expand MAX_SUBCMD_DEPTH");
> + exit(EXIT_USAGE);
> + }
> +
> + subcmd_stack[++subcmd_depth] = sub;
> +
> for (i = 0; sub[i].name; i++) {
> - if (!strcmp(sub[i].name, argv[optind])) {
> - unsigned long flags = sub[i].flags;
> -
> - if (flags & CMD_NEED_NODELIST) {
> - ret = update_node_list(SD_MAX_NODES);
> - if (ret < 0) {
> - sd_err("Failed to get node list");
> - exit(EXIT_SYSFAIL);
> - }
> - }
> + unsigned long flags;
>
> - if (flags & CMD_NEED_ARG && argc < 5 + depth)
> - subcommand_usage(argv[1], argv[2], EXIT_USAGE);
> - optind++;
> - ret = sub[i].fn(argc, argv);
> - if (ret == EXIT_USAGE)
> - subcommand_usage(argv[1], argv[2], EXIT_USAGE);
> - return ret;
> + if (strcmp(sub[i].name, argv[optind]))
> + continue;
> +
> + flags = sub[i].flags;
> +
> + if (flags & CMD_NEED_NODELIST) {
> + ret = update_node_list(SD_MAX_NODES);
> + if (ret < 0) {
> + sd_err("Failed to get node list");
> + exit(EXIT_SYSFAIL);
> + }
> }
> +
> + if (flags & CMD_NEED_ARG && argc < 5 + subcmd_depth)
> + subcommand_usage(argv[1], argv[2], EXIT_USAGE, true);
> + optind++;
> + ret = sub[i].fn(argc, argv);
> + if (ret == EXIT_USAGE)
> + subcommand_usage(argv[1], argv[2], EXIT_USAGE, true);
> + return ret;
> }
>
> - depth--;
> - subcommand_usage(argv[1], argv[2], EXIT_FAILURE);
> + subcommand_usage(argv[1], argv[2], EXIT_FAILURE, true);
> + subcmd_depth--;
> +
> return EXIT_FAILURE;
> }
>
> diff --git a/dog/dog.c b/dog/dog.c
> index 16298ad..3db90f6 100644
> --- a/dog/dog.c
> +++ b/dog/dog.c
> @@ -254,7 +254,7 @@ static void usage(const struct command *commands, int status)
> exit(status);
> }
>
> -void subcommand_usage(char *cmd, char *subcmd, int status)
> +void subcommand_usage(char *cmd, char *subcmd, int status, bool print_stack)
> {
> int i, n, len = strlen(command_opts);
> const struct sd_option *sd_opt;
> @@ -263,9 +263,16 @@ void subcommand_usage(char *cmd, char *subcmd, int status)
>
> printf("Usage: %s %s %s", program_name, cmd, subcmd);
>
> - /* Show subcmd's subcommands if necessary */
> - sub = find_subcmd(cmd, subcmd);
> - subsub = sub->sub;
> + if (print_stack) {
> + for (i = 0; i < subcmd_depth + 1; i++)
> + printf(" %s", subcmd_stack[i]->name);
> +
> + subsub = subcmd_stack[i - 1]->sub;
> + } else {
> + sub = find_subcmd(cmd, subcmd);
> + subsub = sub->sub;
> + }
> +
> if (subsub) {
> n = 0;
> while (subsub[n].name)
> @@ -397,7 +404,7 @@ int main(int argc, char **argv)
> verbose = true;
> break;
> case 'h':
> - subcommand_usage(argv[1], argv[2], EXIT_SUCCESS);
> + subcommand_usage(argv[1], argv[2], EXIT_SUCCESS, false);
> break;
> case '?':
> usage(commands, EXIT_USAGE);
> @@ -423,7 +430,7 @@ int main(int argc, char **argv)
> }
>
> if (flags & CMD_NEED_ARG && argc == optind)
> - subcommand_usage(argv[1], argv[2], EXIT_USAGE);
> + subcommand_usage(argv[1], argv[2], EXIT_USAGE, false);
>
> if (init_event(EPOLL_SIZE) < 0)
> exit(EXIT_SYSFAIL);
> @@ -440,6 +447,6 @@ int main(int argc, char **argv)
>
> ret = command_fn(argc, argv);
> if (ret == EXIT_USAGE)
> - subcommand_usage(argv[1], argv[2], EXIT_USAGE);
> + subcommand_usage(argv[1], argv[2], EXIT_USAGE, false);
> return ret;
> }
> diff --git a/dog/dog.h b/dog/dog.h
> index 28c36a1..9b210ca 100644
> --- a/dog/dog.h
> +++ b/dog/dog.h
> @@ -47,7 +47,12 @@ struct subcommand {
> int (*fn)(int, char **);
> const struct sd_option *options;
> };
> -void subcommand_usage(char *cmd, char *subcmd, int status);
> +void subcommand_usage(char *cmd, char *subcmd, int status, bool print_stack);
> +
> +#define MAX_SUBCMD_DEPTH 8
> +
> +extern int subcmd_depth;
> +extern struct subcommand *subcmd_stack[MAX_SUBCMD_DEPTH];
>
> extern struct node_id sd_nid;
> extern bool highlight;
> --
> 1.8.1.2
>
More information about the sheepdog
mailing list