[master] f146f86 Start remodelling the argument parsing (again) so that special mode arguments (-V, -x and later -C) break free early.

Poul-Henning Kamp phk at FreeBSD.org
Mon Feb 27 23:39:08 CET 2017


commit f146f863e38a07f7b3faac4cda9b9b4a13e6c189
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Mon Feb 27 22:36:58 2017 +0000

    Start remodelling the argument parsing (again) so that
    special mode arguments (-V, -x and later -C) break free early.

diff --git a/bin/varnishd/mgt/mgt_main.c b/bin/varnishd/mgt/mgt_main.c
index 4ce28e0..e0c77ff 100644
--- a/bin/varnishd/mgt/mgt_main.c
+++ b/bin/varnishd/mgt/mgt_main.c
@@ -74,7 +74,7 @@ int optreset;	// Some has it, some doesn't.  Cheaper than auto*
 
 /*--------------------------------------------------------------------*/
 
-static void __attribute__((__noreturn__))
+static void 
 usage(void)
 {
 #define FMT "    %-28s # %s\n"
@@ -449,7 +449,6 @@ main(int argc, char * const *argv)
 	unsigned C_flag = 0;
 	unsigned f_flag = 0;
 	unsigned F_flag = 0;
-	unsigned V_flag = 0;
 	const char *b_arg = NULL;
 	const char *i_arg = NULL;
 	const char *j_arg = NULL;
@@ -460,7 +459,6 @@ main(int argc, char * const *argv)
 	const char *S_arg = NULL;
 	const char *s_arg = "malloc,100m";
 	const char *W_arg = NULL;
-	const char *x_arg = NULL;
 	int s_arg_given = 0;
 	int novcl = 0;
 	const char *T_arg = "localhost:0";
@@ -483,15 +481,34 @@ main(int argc, char * const *argv)
 
 	mgt_initialize(cli);
 
-	/*
-	 * First pass over arguments, to determine what we will be doing
-	 * and what process configuration we will use for it.
-	 */
-	while ((o = getopt(argc, argv, opt_spec)) != -1) {
+	/* Check if first argument is a special flag */
+
+	o = getopt(argc, argv, opt_spec);
+	switch (o) {
+	case '?':
+		usage();
+		exit(1);
+	case 'x':
+		if (argc != 3)
+			ARGV_ERR("Too many arguments for -x\n");
+		mgt_x_arg(optarg);
+		exit(0);
+	case 'V':
+		if (argc != 2)
+			ARGV_ERR("Too many arguments for -V\n");
+		VCS_Message("varnishd");
+		exit(0);
+	default:
+		break;
+	}
+
+	/* First pass over arguments to determine overall configuration */
+
+	do {
 		switch (o) {
 		case '?':
 			usage();
-			break;
+			exit(1);
 		case 'b':
 			b_arg = optarg;
 			break;
@@ -511,32 +528,18 @@ main(int argc, char * const *argv)
 			j_arg = optarg;
 			break;
 		case 'V':
-			V_flag = 1;
-			break;
 		case 'x':
-			x_arg = optarg;
+			ARGV_ERR("-%c must be the first argument\n", o);
 			break;
 		default:
 			break;
 		}
-	}
+		o = getopt(argc, argv, opt_spec);
+	} while (o != -1);
 
 	if (argc != optind)
 		ARGV_ERR("Too many arguments (%s...)\n", argv[optind]);
 
-	if (V_flag) {
-		if (argc != 2)
-			ARGV_ERR("-V is incompatible with everything else\n");
-		VCS_Message("varnishd");
-		exit(0);
-	}
-	if (x_arg != NULL) {
-		if (argc != 3)
-			ARGV_ERR("-x is incompatible with everything else\n");
-		mgt_x_arg(x_arg);
-		exit(0);
-	}
-
 	if (b_arg != NULL && f_flag)
 		ARGV_ERR("Only one of -b or -f can be specified\n");
 
@@ -597,7 +600,6 @@ main(int argc, char * const *argv)
 		case 'd':
 		case 'F':
 		case 'j':
-		case 'V':
 		case 'x':
 			/* Handled in first pass */
 			break;
@@ -674,8 +676,10 @@ main(int argc, char * const *argv)
 			break;
 		case 'p':
 			p = strchr(optarg, '=');
-			if (p == NULL)
+			if (p == NULL) {
 				usage();
+				exit(2);
+			}
 			AN(p);
 			*p++ = '\0';
 			MCF_ParamSet(cli, optarg, p);
@@ -706,7 +710,7 @@ main(int argc, char * const *argv)
 			W_arg = optarg;
 			break;
 		default:
-			usage();
+			WRONG("Error in argument parsing");
 		}
 	}
 	assert(argc == optind);
diff --git a/bin/varnishtest/tests/a00009.vtc b/bin/varnishtest/tests/a00009.vtc
index 2281600..12daa47 100644
--- a/bin/varnishtest/tests/a00009.vtc
+++ b/bin/varnishtest/tests/a00009.vtc
@@ -1,10 +1,6 @@
 varnishtest "Code coverage of mgt_main, (VCL compiler and RSTdump etc)"
 
 shell "varnishd -b 127.0.0.1:80 -C 2> ${tmpdir}/_.c"
-shell "varnishd -x dumprstparam > ${tmpdir}/_.param"
-shell "varnishd -x dumprstvsl > ${tmpdir}/_.vsl"
-shell "varnishd -x dumprstcli > ${tmpdir}/_.cli"
-shell "varnishd -x dumpbuiltin > ${tmpdir}/_.builtin"
 shell -err -expect {VCL version declaration missing} {
 	echo 'bad vcl' > ${tmpdir}/bad.vcl
 	varnishd -f ${tmpdir}/bad.vcl -n ${tmpdir}
@@ -15,8 +11,19 @@ shell -err -expect {VCL version declaration missing} {
 }
 shell -err -expect {-spersistent has been deprecated} "varnishd -spersistent -f '' "
 shell -err -expect {Unknown jail method "xyz"} "varnishd -jxyz -f '' "
-shell -err -expect {-x is incompatible with everything else} "varnishd -d -x foo "
+
+shell -err -expect {-x must be the first argument} "varnishd -d -x foo "
+shell -err -expect {Too many arguments for -x} "varnishd -x foo bar"
 shell -err -expect {Invalid -x argument} "varnishd -x foo "
+shell "varnishd -x dumprstparam > ${tmpdir}/_.param"
+shell "varnishd -x dumprstvsl > ${tmpdir}/_.vsl"
+shell "varnishd -x dumprstcli > ${tmpdir}/_.cli"
+shell "varnishd -x dumpbuiltin > ${tmpdir}/_.builtin"
+
+shell -err -expect {-V must be the first argument} "varnishd -d -V foo "
+shell -err -expect {Too many arguments for -V} "varnishd -V -V"
+shell -expect {Copyright (c) 2006} "varnishd -V"
+
 shell -err -expect {Too many arguments} "varnishd foo "
 shell -err -expect {Only one of -d or -F can be specified} "varnishd -d -F "
 shell -err -expect {Only one of -b or -f can be specified} "varnishd -b a -f b "
@@ -27,7 +34,6 @@ shell -err -expect {-F makes no sense with -C} "varnishd -C -b 127.0.0.1:80 -F "
 shell -err -expect {usage: varnishd [options]} "varnishd -? "
 shell -err -expect {Invalid backslash sequence} {varnishd -l 'xyz\kk,xyz\foo' -f '' }
 shell -err -expect {Invalid backslash sequence} {varnishd -l 'ab\8cd' -f '' }
-shell -err -expect {Copyright (c) 2006} "varnishd -V  ; exit 1"
 shell -err -expect {usage: varnishd [options]} "varnishd -A "
 shell -err -expect {Cannot open -S file} {
 	varnishd -S ${tmpdir}/nonexistent -n ${tmpdir}/v0 -f ''



More information about the varnish-commit mailing list