[master] 80240d7 Move query input to be an option instead of cmd line args

Martin Blix Grydeland martin at varnish-cache.org
Tue Oct 1 14:48:19 CEST 2013


commit 80240d7746a5912d18ef5134e02f6c1f3ddfaa62
Author: Martin Blix Grydeland <martin at varnish-software.com>
Date:   Mon Sep 30 12:36:47 2013 +0200

    Move query input to be an option instead of cmd line args
    
    Having the query input to the utils be cmd line args leads users to
    believe that the args are interpreted as tokens themselves. The parser
    doesn't see each argument as a token, but instead makes one string
    from all the arguments. This would cause quoting issues.
    
    By having the query expression be an option instead, it becomes
    clearer that the input is one string only, and any quoting when
    constructing an expression from some other input is left to the user.

diff --git a/bin/varnishlog/varnishlog.c b/bin/varnishlog/varnishlog.c
index d3d9e29..a10c595 100644
--- a/bin/varnishlog/varnishlog.c
+++ b/bin/varnishlog/varnishlog.c
@@ -54,7 +54,7 @@ static void
 usage(void)
 {
 	const char **opt;
-	fprintf(stderr, "Usage: varnishlog <options> [query expression]\n\n");
+	fprintf(stderr, "Usage: varnishlog <options>\n\n");
 	fprintf(stderr, "Options:\n");
 	for (opt = vopt_usage; *opt != NULL; opt += 2)
 		fprintf(stderr, "  %-25s %s\n", *opt, *(opt + 1));
@@ -76,8 +76,8 @@ main(int argc, char * const *argv)
 		}
 	}
 
-	if (optind < argc)
-		VUT.query = argv[optind];
+	if (optind != argc)
+		usage();
 
 	VUT_Setup();
 	VUT_Main(NULL, NULL);
diff --git a/bin/varnishlog/varnishlog_options.h b/bin/varnishlog/varnishlog_options.h
index 5c09c58..53519b7 100644
--- a/bin/varnishlog/varnishlog_options.h
+++ b/bin/varnishlog/varnishlog_options.h
@@ -39,6 +39,7 @@ VSL_OPT_I
 VSM_OPT_n
 VSM_OPT_N
 VUT_OPT_P
+VUT_OPT_q
 VSL_OPT_r
 VSL_OPT_u
 VSL_OPT_v
diff --git a/include/vut.h b/include/vut.h
index 0fdf8de..2ba148c 100644
--- a/include/vut.h
+++ b/include/vut.h
@@ -38,10 +38,10 @@ struct VUT {
 	int		D_opt;
 	int		g_arg;
 	char		*P_arg;
+	char		*q_arg;
 	char		*r_arg;
 	int		u_opt;
 	char		*w_arg;
-	const char	*query;
 
 	/* State */
 	struct VSL_data	*vsl;
diff --git a/include/vut_options.h b/include/vut_options.h
index 3034b41..7328b91 100644
--- a/include/vut_options.h
+++ b/include/vut_options.h
@@ -38,3 +38,8 @@
 	VOPT("P:", "[-P file]", "PID file",				\
 		"Write the process' PID to the specified file."		\
 	)
+
+#define VUT_OPT_q							\
+	VOPT("q:", "[-q query]", "VSL query",				\
+		"Specifies the VSL query to use."			\
+	)
diff --git a/lib/libvarnishapi/vxp_test.c b/lib/libvarnishapi/vxp_test.c
index d4a8b4c..1104039 100644
--- a/lib/libvarnishapi/vxp_test.c
+++ b/lib/libvarnishapi/vxp_test.c
@@ -2,43 +2,56 @@
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
+#include <unistd.h>
 
 #include "vxp.h"
 #include "vas.h"
 #include "vsb.h"
+#include "miniobj.h"
+
+static void
+usage(void)
+{
+	fprintf(stderr, "Usage: vxp_test -q <query-expression>\n");
+	exit(1);
+}
 
 int
 main(int argc, char **argv)
 {
-	int i;
-	unsigned l;
-	char *s;
 	struct vsb *vsb;
 	struct vex *vex;
+	char *q_arg = NULL;
+	char opt;
 
-	l = 0;
-	for (i = 1; i < argc; i++)
-		l += strlen(argv[i]) + 1;
-	s = calloc(l + 1, sizeof (char));
-	for (i = 1; i < argc; strcat(s, " "), i++)
-		strcat(s, argv[i]);
+	while ((opt = getopt(argc, argv, "q:")) != -1) {
+		switch (opt) {
+		case 'q':
+			REPLACE(q_arg, optarg);
+			break;
+		default:
+			usage();
+		}
+	}
+	if (q_arg == NULL || optind != argc)
+		usage();
 
 	vsb = VSB_new_auto();
 	AN(vsb);
-	vex = vex_New(s, vsb);
+	vex = vex_New(q_arg, vsb);
 
 	if (vex == NULL) {
 		VSB_finish(vsb);
 		fprintf(stderr, "Error:\n%s", VSB_data(vsb));
 		VSB_delete(vsb);
-		free(s);
+		free(q_arg);
 		exit(1);
 	}
 	VSB_delete(vsb);
 
 	vex_Free(&vex);
 	AZ(vex);
-	free(s);
+	free(q_arg);
 
 	return (0);
 }
diff --git a/lib/libvarnishtools/vut.c b/lib/libvarnishtools/vut.c
index d3e5abd..4cf5c71 100644
--- a/lib/libvarnishtools/vut.c
+++ b/lib/libvarnishtools/vut.c
@@ -136,6 +136,10 @@ VUT_Arg(int opt, const char *arg)
 		/* PID file */
 		REPLACE(VUT.P_arg, arg);
 		return (1);
+	case 'q':
+		/* Query to use */
+		REPLACE(VUT.q_arg, arg);
+		return (1);
 	case 'r':
 		/* Binary file input */
 		REPLACE(VUT.r_arg, arg);
@@ -201,7 +205,7 @@ VUT_Setup(void)
 	}
 
 	/* Create query */
-	VUT.vslq = VSLQ_New(VUT.vsl, &c, VUT.g_arg, VUT.query);
+	VUT.vslq = VSLQ_New(VUT.vsl, &c, VUT.g_arg, VUT.q_arg);
 	if (VUT.vslq == NULL)
 		VUT_Error(1, "Query parse error (%s)", VSL_Error(VUT.vsl));
 	AZ(c);
@@ -289,7 +293,7 @@ VUT_Main(VSLQ_dispatch_f *func, void *priv)
 				VSL_ResetError(VUT.vsl);
 				continue;
 			}
-			VUT.vslq = VSLQ_New(VUT.vsl, &c, VUT.g_arg, VUT.query);
+			VUT.vslq = VSLQ_New(VUT.vsl, &c, VUT.g_arg, VUT.q_arg);
 			AN(VUT.vslq);
 			AZ(c);
 		}
@@ -339,7 +343,7 @@ VUT_Main(VSLQ_dispatch_f *func, void *priv)
 				VSL_ResetError(VUT.vsl);
 				continue;
 			}
-			VUT.vslq = VSLQ_New(VUT.vsl, &c, VUT.g_arg, VUT.query);
+			VUT.vslq = VSLQ_New(VUT.vsl, &c, VUT.g_arg, VUT.q_arg);
 			AN(VUT.vslq);
 			AZ(c);
 		}



More information about the varnish-commit mailing list