[master] 42e2118 Pack VUT/VOPT's specification into a single structure for forward compatibility and namespace-hygiene.

Poul-Henning Kamp phk at FreeBSD.org
Tue May 10 12:52:06 CEST 2016


commit 42e21180f10858f9b87f30ca12413c1feb34b380
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Tue May 10 10:37:27 2016 +0000

    Pack VUT/VOPT's specification into a single structure for forward
    compatibility and namespace-hygiene.

diff --git a/bin/varnishhist/varnishhist.c b/bin/varnishhist/varnishhist.c
index 31a40af..d8a9905 100644
--- a/bin/varnishhist/varnishhist.c
+++ b/bin/varnishhist/varnishhist.c
@@ -369,7 +369,7 @@ usage(int status)
 
 	fprintf(stderr, "Usage: %s <options>\n\n", progname);
 	fprintf(stderr, "Options:\n");
-	for (opt = vopt_usage; *opt != NULL; opt +=2)
+	for (opt = vopt_spec.vopt_usage; *opt != NULL; opt +=2)
 		fprintf(stderr, " %-25s %s\n", *opt, *(opt + 1));
 	exit(status);
 }
@@ -385,11 +385,11 @@ main(int argc, char **argv)
 	struct profile cli_p = {0};
 	cli_p.name = 0;
 
-	VUT_Init(progname, argc, argv);
+	VUT_Init(progname, argc, argv, &vopt_spec);
 
 	/* only client requests */
 	assert(VUT_Arg('c', NULL));
-	while ((i = getopt(argc, argv, vopt_optstring)) != -1) {
+	while ((i = getopt(argc, argv, vopt_spec.vopt_optstring)) != -1) {
 		switch (i) {
 		case 'P':
 			colon = strchr(optarg, ':');
diff --git a/bin/varnishlog/varnishlog.c b/bin/varnishlog/varnishlog.c
index d8be34f..9b5e909 100644
--- a/bin/varnishlog/varnishlog.c
+++ b/bin/varnishlog/varnishlog.c
@@ -68,7 +68,7 @@ usage(int status)
 	const char **opt;
 	fprintf(stderr, "Usage: %s <options>\n\n", progname);
 	fprintf(stderr, "Options:\n");
-	for (opt = vopt_usage; *opt != NULL; opt += 2)
+	for (opt = vopt_spec.vopt_usage; *opt != NULL; opt += 2)
 		fprintf(stderr, "  %-25s %s\n", *opt, *(opt + 1));
 	exit(status);
 }
@@ -121,10 +121,10 @@ main(int argc, char * const *argv)
 {
 	int opt;
 
-	VUT_Init(progname, argc, argv);
+	VUT_Init(progname, argc, argv, &vopt_spec);
 	memset(&LOG, 0, sizeof LOG);
 
-	while ((opt = getopt(argc, argv, vopt_optstring)) != -1) {
+	while ((opt = getopt(argc, argv, vopt_spec.vopt_optstring)) != -1) {
 		switch (opt) {
 		case 'a':
 			/* Append to file */
diff --git a/bin/varnishncsa/varnishncsa.c b/bin/varnishncsa/varnishncsa.c
index 13e28d9..5b54ece 100644
--- a/bin/varnishncsa/varnishncsa.c
+++ b/bin/varnishncsa/varnishncsa.c
@@ -166,7 +166,7 @@ usage(int status)
 
 	fprintf(stderr, "Usage: %s <options>\n\n", progname);
 	fprintf(stderr, "Options:\n");
-	for (opt = vopt_usage; *opt != NULL; opt += 2)
+	for (opt = vopt_spec.vopt_usage; *opt != NULL; opt += 2)
 		fprintf(stderr, " %-25s %s\n", *opt, *(opt + 1));
 	exit(status);
 }
@@ -1126,7 +1126,7 @@ main(int argc, char * const *argv)
 	signed char opt;
 	char *format = NULL;
 
-	VUT_Init(progname, argc, argv);
+	VUT_Init(progname, argc, argv, &vopt_spec);
 	memset(&CTX, 0, sizeof CTX);
 	VTAILQ_INIT(&CTX.format);
 	VTAILQ_INIT(&CTX.watch_vcl_log);
@@ -1137,7 +1137,7 @@ main(int argc, char * const *argv)
 	AN(CTX.vsb);
 	VB64_init();
 
-	while ((opt = getopt(argc, argv, vopt_optstring)) != -1) {
+	while ((opt = getopt(argc, argv, vopt_spec.vopt_optstring)) != -1) {
 		switch (opt) {
 		case 'a':
 			/* Append to file */
diff --git a/bin/varnishtop/varnishtop.c b/bin/varnishtop/varnishtop.c
index e91f825..93d61b9 100644
--- a/bin/varnishtop/varnishtop.c
+++ b/bin/varnishtop/varnishtop.c
@@ -318,7 +318,7 @@ usage(int status)
 
 	fprintf(stderr, "Usage: %s <options>\n\n", progname);
 	fprintf(stderr, "Options:\n");
-	for (opt = vopt_usage; *opt != NULL; opt +=2)
+	for (opt = vopt_spec.vopt_usage; *opt != NULL; opt +=2)
 		fprintf(stderr, " %-25s %s\n", *opt, *(opt + 1));
 	exit(status);
 }
@@ -329,9 +329,9 @@ main(int argc, char **argv)
 	int o, once = 0;
 	pthread_t thr;
 
-	VUT_Init(progname, argc, argv);
+	VUT_Init(progname, argc, argv, &vopt_spec);
 
-	while ((o = getopt(argc, argv, vopt_optstring)) != -1) {
+	while ((o = getopt(argc, argv, vopt_spec.vopt_optstring)) != -1) {
 		switch (o) {
 		case '1':
 			AN(VUT_Arg('d', NULL));
diff --git a/include/vapi/voptget.h b/include/vapi/voptget.h
index cf39a48..f01ac94 100644
--- a/include/vapi/voptget.h
+++ b/include/vapi/voptget.h
@@ -35,17 +35,22 @@
  *   l: Long description
  */
 
-extern const char vopt_optstring[];
-extern const char vopt_synopsis[];
-extern const char *vopt_usage[];
 struct vopt_list {
 	const char *option;
 	const char *synopsis;
 	const char *desc;
 	const char *ldesc;
 };
-extern const struct vopt_list vopt_list[];
-extern unsigned vopt_list_n;
+
+struct vopt_spec {
+	const struct vopt_list	*vopt_list;
+	unsigned		vopt_list_n;
+	const char		*vopt_optstring;
+	const char		*vopt_synopsis;
+	const char		**vopt_usage;
+};
+
+extern const struct vopt_spec vopt_spec;
 
 #ifdef VOPT_DEFINITION
 
@@ -54,29 +59,36 @@ extern unsigned vopt_list_n;
 #endif
 
 #define VOPT(o,s,d,l) o
-const char vopt_optstring[] =
+static const char vopt_optstring[] =
 #include VOPT_INC
     ;
 #undef VOPT
 
 #define VOPT(o,s,d,l) " " s
-const char vopt_synopsis[] =
+static const char vopt_synopsis[] =
 #include VOPT_INC
     ;
 #undef VOPT
 
 #define VOPT(o,s,d,l) s, d,
-const char *vopt_usage[] = {
+static const char *vopt_usage[] = {
 #include VOPT_INC
 	NULL, NULL,
 };
 #undef VOPT
 
 #define VOPT(o,s,d,l) { o,s,d,l },
-const struct vopt_list vopt_list[] = {
+static const struct vopt_list vopt_list[] = {
 #include VOPT_INC
 };
 #undef VOPT
-unsigned vopt_list_n = sizeof vopt_list / sizeof vopt_list[0];
+
+const struct vopt_spec vopt_spec = {
+	vopt_list,
+	sizeof vopt_list / sizeof vopt_list[0],
+	vopt_optstring,
+	vopt_synopsis,
+	vopt_usage
+};
 
 #endif /* VOPT_DEFINITION */
diff --git a/include/vut.h b/include/vut.h
index fe99095..d1e6ab3 100644
--- a/include/vut.h
+++ b/include/vut.h
@@ -29,6 +29,8 @@
  * Common functions for the utilities
  */
 
+struct vopt_spec;
+
 typedef int VUT_cb_f(void);
 
 struct VUT {
@@ -74,7 +76,8 @@ int VUT_Arg(int opt, const char *arg);
 
 void VUT_Setup(void);
 
-void VUT_Init(const char *progname, int argc, char * const *argv);
+void VUT_Init(const char *progname, int argc, char * const *argv,
+    const struct vopt_spec *);
 
 void VUT_Fini(void);
 
diff --git a/lib/libvarnishtools/vut.c b/lib/libvarnishtools/vut.c
index e62c128..39c27fb 100644
--- a/lib/libvarnishtools/vut.c
+++ b/lib/libvarnishtools/vut.c
@@ -55,10 +55,12 @@
 
 #include "vut.h"
 
+#include "vapi/voptget.h"
+
 struct VUT VUT;
 
-static int vut_synopsis(void);
-static int vut_options(void);
+static int vut_synopsis(const struct vopt_spec *);
+static int vut_options(const struct vopt_spec *);
 
 static void
 vut_vpf_remove(void)
@@ -205,13 +207,14 @@ VUT_Arg(int opt, const char *arg)
 }
 
 void
-VUT_Init(const char *progname, int argc, char * const *argv)
+VUT_Init(const char *progname, int argc, char * const *argv,
+    const struct vopt_spec *voc)
 {
 
 	if (argc == 2 && !strcmp(argv[1], "--synopsis"))
-		exit(vut_synopsis());
+		exit(vut_synopsis(voc));
 	if (argc == 2 && !strcmp(argv[1], "--options"))
-		exit(vut_options());
+		exit(vut_options(voc));
 
 	VUT.progname = progname;
 	REPLACE(VUT.name, "");
@@ -435,8 +438,6 @@ VUT_Main(void)
 /**********************************************************************/
 
 
-#include "vapi/voptget.h"
-
 static void
 print_nobrackets(const char *s)
 {
@@ -482,18 +483,18 @@ print_opt(const struct vopt_list *opt)
 }
 
 static int
-vut_synopsis(void)
+vut_synopsis(const struct vopt_spec *voc)
 {
-	printf(".. |synopsis| replace:: %s\n", vopt_synopsis);
+	printf(".. |synopsis| replace:: %s\n", voc->vopt_synopsis);
 	return (0);
 }
 
 static int
-vut_options(void)
+vut_options(const struct vopt_spec *voc)
 {
 	int i;
 
-	for (i = 0; i < vopt_list_n; i++)
-		print_opt(&vopt_list[i]);
+	for (i = 0; i < voc->vopt_list_n; i++)
+		print_opt(&voc->vopt_list[i]);
 	return (0);
 }



More information about the varnish-commit mailing list