[sheepdog] [PATCH v3] dog: let usage printing adopt nested subcommands

Hitoshi Mitake mitake.hitoshi at gmail.com
Thu Nov 14 16:10:48 CET 2013


Signed-off-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
---

v3: remove a needless new parameter of subcommand_usage()

v2: check depth of nested subcommands

 dog/common.c | 53 +++++++++++++++++++++++++++++++++--------------------
 dog/dog.c    | 13 ++++++++++---
 dog/dog.h    |  5 +++++
 3 files changed, 48 insertions(+), 23 deletions(-)

diff --git a/dog/common.c b/dog/common.c
index 8c9b326..8600bc7 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);
+		optind++;
+		ret = sub[i].fn(argc, argv);
+		if (ret == EXIT_USAGE)
+			subcommand_usage(argv[1], argv[2], EXIT_USAGE);
+		return ret;
 	}
 
-	depth--;
 	subcommand_usage(argv[1], argv[2], EXIT_FAILURE);
+	subcmd_depth--;
+
 	return EXIT_FAILURE;
 }
 
diff --git a/dog/dog.c b/dog/dog.c
index 16298ad..b092e62 100644
--- a/dog/dog.c
+++ b/dog/dog.c
@@ -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 (0 <= subcmd_depth) {
+		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)
diff --git a/dog/dog.h b/dog/dog.h
index 28c36a1..a174e7a 100644
--- a/dog/dog.h
+++ b/dog/dog.h
@@ -49,6 +49,11 @@ struct subcommand {
 };
 void subcommand_usage(char *cmd, char *subcmd, int status);
 
+#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;
 extern bool raw_output;
-- 
1.8.1.2




More information about the sheepdog mailing list