[sheepdog] [PATCH 1/6] collie: move options to lib

morita.kazutaka at gmail.com morita.kazutaka at gmail.com
Mon Nov 5 11:51:58 CET 2012


From: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>

This is preparation for using sd_option for sheep.

Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
 collie/collie.c  |   87 ++++++++++++++++++------------------------------------
 collie/collie.h  |    8 +----
 include/option.h |   30 +++++++++++++++++++
 lib/Makefile.am  |    3 +-
 lib/option.c     |   48 ++++++++++++++++++++++++++++++
 5 files changed, 110 insertions(+), 66 deletions(-)
 create mode 100644 include/option.h
 create mode 100644 lib/option.c

diff --git a/collie/collie.c b/collie/collie.c
index 060e53c..a37e3a2 100644
--- a/collie/collie.c
+++ b/collie/collie.c
@@ -8,7 +8,6 @@
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
-#include <getopt.h>
 #include <inttypes.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -117,27 +116,22 @@ static struct sd_option *command_options;
 
 static const struct sd_option *find_opt(int ch)
 {
-	int i;
-	struct sd_option *opt;
+	const struct sd_option *opt;
 
 	/* search for common options */
-	for (i = 0; i < ARRAY_SIZE(collie_options); i++) {
-		if (collie_options[i].val == ch)
-			return collie_options + i;
+	sd_for_each_option(opt, collie_options) {
+		if (opt->ch == ch)
+			return opt;
 	}
 
 	/* search for self options */
-	if (!command_options)
-		goto out;
-
-	opt = command_options;
-	while (opt->val) {
-		if (opt->val == ch)
-			return opt;
-		opt++;
+	if (command_options) {
+		sd_for_each_option(opt, command_options) {
+			if (opt->ch == ch)
+				return opt;
+		}
 	}
 
-out:
 	fprintf(stderr, "Internal error\n");
 	exit(EXIT_SYSFAIL);
 }
@@ -183,44 +177,6 @@ static const struct subcommand *find_subcmd(const char *cmd, const char *subcmd)
 	return NULL;
 }
 
-static char *build_short_options(const char *opts)
-{
-	static char sopts[256], *p;
-	const struct sd_option *sd_opt;
-	int i, len = strlen(opts);
-
-	p = sopts;
-	for (i = 0; i < len; i++) {
-		sd_opt = find_opt(opts[i]);
-		*p++ = sd_opt->val;
-		if (sd_opt->has_arg)
-			*p++ = ':';
-	}
-	*p = '\0';
-
-	return sopts;
-}
-
-static struct option *build_long_options(const char *opts)
-{
-	static struct option lopts[256], *p;
-	const struct sd_option *sd_opt;
-	int i, len = strlen(opts);
-
-	p = lopts;
-	for (i = 0; i < len; i++) {
-		sd_opt = find_opt(opts[i]);
-		p->name = sd_opt->name;
-		p->has_arg = sd_opt->has_arg;
-		p->flag = NULL;
-		p->val = sd_opt->val;
-		p++;
-	}
-	memset(p, 0, sizeof(struct option));
-
-	return lopts;
-}
-
 static unsigned long setup_commands(const struct command *commands,
 				    char *cmd, char *subcmd)
 {
@@ -321,9 +277,9 @@ void subcommand_usage(char *cmd, char *subcmd, int status)
 	for (i = 0; i < len; i++) {
 		sd_opt = find_opt(command_opts[i]);
 		if (sd_opt->has_arg)
-			printf(" [-%c %s]", sd_opt->val, sd_opt->name);
+			printf(" [-%c %s]", sd_opt->ch, sd_opt->name);
 		else
-			printf(" [-%c]", sd_opt->val);
+			printf(" [-%c]", sd_opt->ch);
 	}
 	if (command_arg)
 		printf(" %s", command_arg);
@@ -339,13 +295,26 @@ void subcommand_usage(char *cmd, char *subcmd, int status)
 	printf("Options:\n");
 	for (i = 0; i < len; i++) {
 		sd_opt = find_opt(command_opts[i]);
-		sprintf(name, "-%c, --%s", sd_opt->val, sd_opt->name);
+		sprintf(name, "-%c, --%s", sd_opt->ch, sd_opt->name);
 		printf("  %-24s%s\n", name, sd_opt->desc);
 	}
 
 	exit(status);
 }
 
+static const struct sd_option *build_sd_options(const char *opts)
+{
+	static struct sd_option sd_opts[256], *p;
+	int i, len = strlen(opts);
+
+	p = sd_opts;
+	for (i = 0; i < len; i++)
+		*p++ = *find_opt(opts[i]);
+	memset(p, 0, sizeof(struct sd_option));
+
+	return sd_opts;
+}
+
 int main(int argc, char **argv)
 {
 	int ch, longindex, ret;
@@ -354,6 +323,7 @@ int main(int argc, char **argv)
 	const struct command *commands;
 	const char *short_options;
 	char *p;
+	const struct sd_option *sd_opts;
 
 	init_commands(&commands);
 
@@ -364,8 +334,9 @@ int main(int argc, char **argv)
 
 	optind = 3;
 
-	long_options = build_long_options(command_opts);
-	short_options = build_short_options(command_opts);
+	sd_opts = build_sd_options(command_opts);
+	long_options = build_long_options(sd_opts);
+	short_options = build_short_options(sd_opts);
 
 	while ((ch = getopt_long(argc, argv, short_options, long_options,
 				&longindex)) >= 0) {
diff --git a/collie/collie.h b/collie/collie.h
index 909a072..1b00e68 100644
--- a/collie/collie.h
+++ b/collie/collie.h
@@ -20,6 +20,7 @@
 #include "sheepdog_proto.h"
 #include "sheep.h"
 #include "exits.h"
+#include "option.h"
 
 #define SUBCMD_FLAG_NEED_NODELIST (1 << 0)
 #define SUBCMD_FLAG_NEED_THIRD_ARG (1 << 1)
@@ -29,13 +30,6 @@
 
 #define UINT64_DECIMAL_SIZE 21
 
-struct sd_option {
-	int val;
-	const char *name;
-	bool has_arg;
-	const char *desc;
-};
-
 struct command {
 	const char *name;
 	struct subcommand *sub;
diff --git a/include/option.h b/include/option.h
new file mode 100644
index 0000000..14c1093
--- /dev/null
+++ b/include/option.h
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2012 Nippon Telegraph and Telephone Corporation.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version
+ * 2 as published by the Free Software Foundation.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef __SD_OPTION_H__
+#define __SD_OPTION_H__
+
+#include <stdbool.h>
+#include <getopt.h>
+
+struct sd_option {
+	int ch;
+	const char *name;
+	bool has_arg;
+	const char *desc;
+};
+
+char *build_short_options(const struct sd_option *opts);
+struct option *build_long_options(const struct sd_option *opts);
+
+#define sd_for_each_option(opt, opts)		\
+	for (opt = (opts); opt->name; opt++)
+
+#endif /* __SD_OPTION_H__ */
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 49a79d2..8ee6cf2 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -4,7 +4,8 @@ INCLUDES                = -I$(top_builddir)/include -I$(top_srcdir)/include
 
 noinst_LIBRARIES	= libsheepdog.a
 
-libsheepdog_a_SOURCES	= event.c logger.c net.c util.c rbtree.c strbuf.c sha1.c
+libsheepdog_a_SOURCES	= event.c logger.c net.c util.c rbtree.c strbuf.c \
+			  sha1.c option.c
 
 # support for GNU Flymake
 check-syntax:
diff --git a/lib/option.c b/lib/option.c
new file mode 100644
index 0000000..461d661
--- /dev/null
+++ b/lib/option.c
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2012 Nippon Telegraph and Telephone Corporation.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version
+ * 2 as published by the Free Software Foundation.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <string.h>
+
+#include "option.h"
+
+char *build_short_options(const struct sd_option *sd_opts)
+{
+	static char sopts[256], *p;
+	const struct sd_option *opt;
+
+	p = sopts;
+	sd_for_each_option(opt, sd_opts) {
+		*p++ = opt->ch;
+		if (opt->has_arg)
+			*p++ = ':';
+	}
+	*p = '\0';
+
+	return sopts;
+}
+
+struct option *build_long_options(const struct sd_option *sd_opts)
+{
+	static struct option lopts[256], *p;
+	const struct sd_option *opt;
+
+	p = lopts;
+	sd_for_each_option(opt, sd_opts) {
+		p->name = opt->name;
+		p->has_arg = opt->has_arg;
+		p->flag = NULL;
+		p->val = opt->ch;
+		p++;
+	}
+	memset(p, 0, sizeof(struct option));
+
+	return lopts;
+}
-- 
1.7.9.5




More information about the sheepdog mailing list