[sheepdog] [PATCH stable-0.7 5/5] dog: let usage printing adopt nested subcommands

Hitoshi Mitake mitake.hitoshi at lab.ntt.co.jp
Thu Dec 12 10:46:45 CET 2013


From: Hitoshi Mitake <mitake.hitoshi at gmail.com>

Signed-off-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
Signed-off-by: Liu Yuan <namei.unix at gmail.com>

Conflicts:
	dog/dog.h

Conflicts resolved by Hitoshi Mitake.
Signed-off-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
---
 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 aa99049..006eeaa 100644
--- a/dog/common.c
+++ b/dog/common.c
@@ -215,36 +215,49 @@ int send_light_req(struct sd_req *hdr, const uint8_t *addr, int port)
 	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 cca8e66..ee7125c 100644
--- a/dog/dog.c
+++ b/dog/dog.c
@@ -255,9 +255,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 4cdc6a1..d115e13 100644
--- a/dog/dog.h
+++ b/dog/dog.h
@@ -51,6 +51,11 @@ void subcommand_usage(char *cmd, char *subcmd, int status);
 
 extern uint8_t sdhost[16];
 extern int sdport;
+#define MAX_SUBCMD_DEPTH 8
+
+extern int subcmd_depth;
+extern struct subcommand *subcmd_stack[MAX_SUBCMD_DEPTH];
+
 extern bool highlight;
 extern bool raw_output;
 extern bool verbose;
-- 
1.7.10.4




More information about the sheepdog mailing list