[4.0] 47d2153 Sort out options parsing for varnishtop

Martin Blix Grydeland martin at varnish-software.com
Thu Mar 13 10:24:22 CET 2014


commit 47d2153fb0040be763091932fcc051add175b4b9
Author: Martin Blix Grydeland <martin at varnish-software.com>
Date:   Wed Jan 29 13:50:59 2014 +0100

    Sort out options parsing for varnishtop

diff --git a/bin/varnishtop/Makefile.am b/bin/varnishtop/Makefile.am
index 3a78c4e..a38e293 100644
--- a/bin/varnishtop/Makefile.am
+++ b/bin/varnishtop/Makefile.am
@@ -7,6 +7,8 @@ bin_PROGRAMS = varnishtop
 dist_man_MANS = varnishtop.1
 
 varnishtop_SOURCES = varnishtop.c \
+	varnishtop_options.h \
+	varnishtop_options.c \
 	$(top_srcdir)/lib/libvarnishtools/vut.c \
 	$(top_srcdir)/lib/libvarnish/vas.c \
 	$(top_srcdir)/lib/libvarnish/flopen.c \
@@ -21,9 +23,31 @@ varnishtop_LDADD = \
 	$(top_builddir)/lib/libvarnishapi/libvarnishapi.la \
 	@CURSES_LIB@ ${RT_LIBS} ${LIBM} ${PTHREAD_LIBS}
 
-varnishtop.1: $(top_srcdir)/doc/sphinx/reference/varnishtop.rst
+noinst_PROGRAMS = varnishtop_opt2rst
+varnishtop_opt2rst_SOURCES = \
+	varnishtop_options.h \
+	varnishtop_options.c \
+	$(top_srcdir)/lib/libvarnishtools/opt2rst.c
+
+BUILT_SOURCES = varnishtop_options.rst varnishtop_synopsis.rst
+EXTRA_DIST = $(BUILT_SOURCES)
+MAINTAINERCLEANFILES = $(EXTRA_DIST)
+
+varnishtop_options.rst:
+	./varnishtop_opt2rst options > $@
+varnishtop_synopsis.rst:
+	./varnishtop_opt2rst synopsis > $@
+
+if HAVE_RST2MAN
+varnishtop_options.rst varnishtop_synopsis.rst: varnishtop_opt2rst
+endif
+
+varnishtop.1: \
+	$(top_srcdir)/doc/sphinx/reference/varnishtop.rst \
+	varnishtop_options.rst \
+	varnishtop_synopsis.rst
 if HAVE_RST2MAN
-	${RST2MAN} $? $@
+	${RST2MAN} $(top_srcdir)/doc/sphinx/reference/varnishtop.rst $@
 else
 	@echo "========================================"
 	@echo "You need rst2man installed to make dist"
diff --git a/bin/varnishtop/varnishtop.c b/bin/varnishtop/varnishtop.c
index 65aa6a9..7190257 100644
--- a/bin/varnishtop/varnishtop.c
+++ b/bin/varnishtop/varnishtop.c
@@ -45,8 +45,9 @@
 #include <string.h>
 #include <unistd.h>
 
-#include "vapi/vsl.h"
 #include "vapi/vsm.h"
+#include "vapi/vsl.h"
+#include "vapi/voptget.h"
 #include "vas.h"
 #include "vcs.h"
 #include "vtree.h"
@@ -296,11 +297,15 @@ dump(void)
 }
 
 static void
-usage(void)
+usage(int status)
 {
-	fprintf(stderr,
-		"usage: varnishtop [-1fV] [-n varnish_name]\n");
-	exit(1);
+	const char **opt;
+
+	fprintf(stderr, "Usage: %s <options>\n\n", progname);
+	fprintf(stderr, "Options:\n");
+	for (opt = vopt_usage; *opt != NULL; opt +=2)
+		fprintf(stderr, " %-25s %s\n", *opt, *(opt + 1));
+	exit(status);
 }
 
 int
@@ -313,7 +318,7 @@ main(int argc, char **argv)
 	vd = VSM_New();
 	VUT_Init(progname);
 
-	while ((o = getopt(argc, argv, "1fVp:")) != -1) {
+	while ((o = getopt(argc, argv, vopt_optstring)) != -1) {
 		switch (o) {
 		case '1':
 			VUT_Arg('d', NULL);
@@ -322,6 +327,9 @@ main(int argc, char **argv)
 		case 'f':
 			f_flag = 1;
 			break;
+		case 'h':
+			/* Usage help */
+			usage(0);
 		case 'p':
 			errno = 0;
 			period = strtol(optarg, NULL, 0);
@@ -331,16 +339,9 @@ main(int argc, char **argv)
 				exit(1);
 			}
 			break;
-		case 'V':
-			VCS_Message("varnishtop");
-			exit(0);
-		case 'm':
-			fprintf(stderr, "-m is not supported\n");
-			exit(1);
 		default:
 			if (!VUT_Arg(o, optarg))
-				break;
-			usage();
+				usage(1);
 		}
 	}
 
diff --git a/bin/varnishtop/varnishtop_options.c b/bin/varnishtop/varnishtop_options.c
new file mode 100644
index 0000000..58d132e
--- /dev/null
+++ b/bin/varnishtop/varnishtop_options.c
@@ -0,0 +1,34 @@
+/*-
+ * Copyright (c) 2014 Varnish Software AS
+ * All rights reserved.
+ *
+ * Author: Martin Blix Grydeland <martin at varnish-software.com>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * Option definitions for varnishtop
+ */
+
+#include <stdlib.h>
+#define VOPT_DEFINITION
+#define VOPT_INC "varnishtop_options.h"
+#include "vapi/voptget.h"
diff --git a/bin/varnishtop/varnishtop_options.h b/bin/varnishtop/varnishtop_options.h
new file mode 100644
index 0000000..33135a8
--- /dev/null
+++ b/bin/varnishtop/varnishtop_options.h
@@ -0,0 +1,50 @@
+/*-
+ * Copyright (c) 2014 Varnish Software AS
+ * All rights reserved.
+ *
+ * Author: Martin Blix Grydeland <martin at varnish-software.com>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * Option definitions for varnishtop
+ */
+
+#include "vapi/vapi_options.h"
+#include "vut_options.h"
+
+VSL_OPT_b
+VSL_OPT_c
+VSL_OPT_C
+VUT_OPT_d
+VUT_OPT_D
+VUT_OPT_h
+VSL_OPT_i
+VSL_OPT_I
+VSL_OPT_L
+VUT_OPT_n
+VUT_OPT_N
+VUT_OPT_q
+VUT_OPT_r
+VSL_OPT_T
+VSL_OPT_x
+VSL_OPT_X
+VUT_OPT_V



More information about the varnish-commit mailing list