[master] ce26d62 Handle the options like the other tools

Martin Blix Grydeland martin at varnish-software.com
Tue Feb 25 15:58:39 CET 2014


commit ce26d62cf3e695c5339e513d629f8fa694201c96
Author: Guillaume Quintard <guillaume.quintard at gmail.com>
Date:   Tue Feb 4 14:46:45 2014 +0100

    Handle the options like the other tools

diff --git a/bin/varnishhist/varnishhist.c b/bin/varnishhist/varnishhist.c
index bf61060..626eba6 100644
--- a/bin/varnishhist/varnishhist.c
+++ b/bin/varnishhist/varnishhist.c
@@ -318,9 +318,12 @@ do_curses(void *arg)
 static void
 usage(int status)
 {
-	fprintf(stderr, "usage: varnishhist "
-	    "%s [-p profile] [-f field_num] "
-	    "[-R max] [-r min] [-V] [-w delay]\n", "varnishhist");
+	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);
 }
 
@@ -328,12 +331,12 @@ int
 main(int argc, char **argv)
 {
 	int i;
+	char *colon;
 	const char *profile = "responsetime";
 	pthread_t thr;
 	int fnum = -1;
-	hist_low = -1;
-	hist_high = -1;
-	match_tag = -1;
+	struct profile cli_p;
+	cli_p.name = 0;
 
 	VUT_Init(progname);
 	if (0)
@@ -341,30 +344,30 @@ main(int argc, char **argv)
 
 	while ((i = getopt(argc, argv, vopt_optstring)) != -1) {
 		switch (i) {
-		case 'V':
-			VCS_Message("varnishhist");
-			exit(0);
-		case 'i':
-			match_tag = VSL_Name2Tag(optarg, -1);
+		case 'P':
+			colon = strchr(optarg, ':');
+			/* no colon, take the profile as a name*/
+			if (colon == NULL) {
+				profile = optarg;
+				break;
+			}
+			/* else it's a definition, we hope */
+			if (sscanf(colon+1, "%d:%d:%d", 
+					&cli_p.field, &cli_p.hist_low, &cli_p.hist_high) != 3) {
+				fprintf(stderr, "%s is neither a profile name nor definition (SLT_Tag:field:min:max)\n", optarg);
+				exit(1);
+			}
+
+			match_tag = VSL_Name2Tag(optarg, colon - optarg);
 			if (match_tag < 0) {
-				fprintf(stderr, "No such tag %s\n", optarg);
+				fprintf(stderr, "No such tag in %s\n", optarg);
 				exit(1);
 			}
-			break;
-		case 'w':
-			delay = atoi(optarg);
-			break;
-		case 'f':
-			fnum = atoi(optarg);
-			break;
-		case 'R':
-			hist_high = atoi(optarg);
-			break;
-		case 'r':
-			hist_low = atoi(optarg);
-			break;
-		case 'p':
-			profile = optarg;
+			cli_p.name = "custom";
+			cli_p.tag = match_tag;
+			profile = NULL;
+			active_profile = &cli_p;
+
 			break;
 		default:
 			if (!VUT_Arg(i, optarg))
@@ -383,21 +386,10 @@ main(int argc, char **argv)
 		fprintf(stderr, "No such profile %s\n", profile);
 		exit(1);
 	}
-	if (match_tag < 0) {
-		match_tag = active_profile->tag;
-	}
-
-	if (fnum < 0) {
-		fnum = active_profile->field;
-	}
-
-	if (hist_low < 0) {
-		hist_low = active_profile->hist_low;
-	}
-
-	if (hist_high < 0) {
-		hist_high = active_profile->hist_high;
-	}
+	match_tag = active_profile->tag;
+	fnum = active_profile->field;
+	hist_low = active_profile->hist_low;
+	hist_high = active_profile->hist_high;
 
 	hist_range = hist_high - hist_low;
 	hist_buckets = hist_range * HIST_RES;
diff --git a/bin/varnishhist/varnishhist_options.c b/bin/varnishhist/varnishhist_options.c
new file mode 100644
index 0000000..718fd3d
--- /dev/null
+++ b/bin/varnishhist/varnishhist_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 "varnishhist_options.h"
+#include "vapi/voptget.h"
diff --git a/bin/varnishhist/varnishhist_options.h b/bin/varnishhist/varnishhist_options.h
new file mode 100644
index 0000000..e362d32
--- /dev/null
+++ b/bin/varnishhist/varnishhist_options.h
@@ -0,0 +1,68 @@
+/*-
+ * 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"
+
+#define HIS_OPT_p							\
+	VOPT("p:", "[-p period]", "Refresh period",			\
+	    "Specified the number of seconds between screen refreshes"	\
+	    " default is 1 second, and can be changed during the run"	\
+	    " by pressing the [1-9] keys"	\
+	)
+
+#define HIS_OPT_P							\
+	VOPT("P:", "[-P <size|responsetime|tag:field_num:min:max>]", "Profile definition",			\
+		"Either specify \"size\" or \"responstime\" profile or create a new one" \
+	    " Define the tag we'll look for, and the field number of the value"	\
+	    " we are interested in. min and max are the boundaries of the graph"	\
+	    " (these are power of tens)"	\
+	)
+
+VSL_OPT_b
+VSL_OPT_c
+VSL_OPT_C
+VUT_OPT_d
+VUT_OPT_D
+VUT_OPT_g
+VUT_OPT_h
+VSL_OPT_i
+VSL_OPT_I
+VSL_OPT_L
+VUT_OPT_n
+VUT_OPT_N
+HIS_OPT_p
+HIS_OPT_P
+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