[master] a08370f Centralize the code to handle a named command line argument.
Poul-Henning Kamp
phk at FreeBSD.org
Sat Aug 12 15:08:11 CEST 2017
commit a08370f3db98e0c8db0e93297b156f9ba50f4eb1
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date: Sat Aug 12 13:04:13 2017 +0000
Centralize the code to handle a named command line argument.
diff --git a/bin/varnishd/mgt/mgt.h b/bin/varnishd/mgt/mgt.h
index 07b8c61..2a230db 100644
--- a/bin/varnishd/mgt/mgt.h
+++ b/bin/varnishd/mgt/mgt.h
@@ -180,6 +180,8 @@ void mgt_DumpRstVsl(void);
struct vsb *mgt_BuildVident(void);
void MGT_Complain(const char *, const char *, ...) __v_printflike(2, 3);
const void *MGT_Pick(const struct choice *, const char *, const char *);
+char **MGT_NamedArg(const char *arg, const char **name, const char *what);
+
/* stevedore_mgt.c */
void STV_Config(const char *spec);
diff --git a/bin/varnishd/mgt/mgt_util.c b/bin/varnishd/mgt/mgt_util.c
index 15bee9f..9263af8 100644
--- a/bin/varnishd/mgt/mgt_util.c
+++ b/bin/varnishd/mgt/mgt_util.c
@@ -43,6 +43,9 @@
#include "common/heritage.h"
+#include "vav.h"
+#include "vct.h"
+
/*--------------------------------------------------------------------*/
char *
@@ -184,3 +187,33 @@ MGT_Pick(const struct choice *cp, const char *which, const char *kind)
}
ARGV_ERR("Unknown %s method \"%s\"\n", kind, which);
}
+
+/*--------------------------------------------------------------------*/
+
+char **
+MGT_NamedArg(const char *spec, const char **name, const char *what)
+{
+ const char *p, *q;
+ char **av;
+
+ ASSERT_MGT();
+ p = strchr(spec, '=');
+ q = strchr(spec, ',');
+ if (p == NULL || (q != NULL && q < p)) {
+ av = VAV_Parse(spec, NULL, ARGV_COMMA);
+ p = NULL;
+ } else if (VCT_invalid_name(spec, p) != NULL) {
+ ARGV_ERR("invalid %s name \"%.*s\"=[...]\n",
+ what, (int)(p - spec), spec);
+ } else if (p[1] == '\0') {
+ ARGV_ERR("Empty named %s argument \"%s\"\n", what, spec);
+ } else {
+ av = VAV_Parse(p + 1, NULL, ARGV_COMMA);
+ }
+ AN(av);
+
+ if (av[0] != NULL)
+ ARGV_ERR("%s\n", av[0]);
+ *name = p;
+ return (av);
+}
diff --git a/bin/varnishd/storage/mgt_stevedore.c b/bin/varnishd/storage/mgt_stevedore.c
index 0b0dac2..60791a7 100644
--- a/bin/varnishd/storage/mgt_stevedore.c
+++ b/bin/varnishd/storage/mgt_stevedore.c
@@ -41,8 +41,6 @@
#include "vcli_serve.h"
#include "storage/storage.h"
-#include "vav.h"
-#include "vct.h"
static VTAILQ_HEAD(, stevedore) stevedores =
VTAILQ_HEAD_INITIALIZER(stevedores);
@@ -146,26 +144,15 @@ void
STV_Config(const char *spec)
{
char **av;
- const char *p, *q;
+ const char *name;
struct stevedore *stv;
const struct stevedore *stv2;
int ac, l;
static unsigned seq = 0;
- ASSERT_MGT();
- p = strchr(spec, '=');
- q = strchr(spec, ',');
- if (p != NULL && (q == NULL || q > p)) {
- av = VAV_Parse(p + 1, NULL, ARGV_COMMA);
- } else {
- av = VAV_Parse(spec, NULL, ARGV_COMMA);
- p = NULL;
- }
+ av = MGT_NamedArg(spec, &name, "-s");
AN(av);
- if (av[0] != NULL)
- ARGV_ERR("%s\n", av[0]);
-
if (av[1] == NULL)
ARGV_ERR("-s argument lacks strategy {malloc, file, ...}\n");
@@ -187,13 +174,11 @@ STV_Config(const char *spec)
*stv = *stv2;
AN(stv->name);
- if (p == NULL)
+ if (name == NULL)
bprintf(stv->ident, "s%u", seq++);
else {
- if (VCT_invalid_name(spec, p) != NULL)
- ARGV_ERR("invalid storage name (-s %s)\n", spec);
/* XXX: no need for truncation once VSM ident becomes dynamic */
- l = p - spec;
+ l = name - spec;
if (l > sizeof stv->ident - 1)
l = sizeof stv->ident - 1;
bprintf(stv->ident, "%.*s", l, spec);
diff --git a/bin/varnishtest/tests/r02325.vtc b/bin/varnishtest/tests/r02325.vtc
index 0c6ad4d..7b357e5 100644
--- a/bin/varnishtest/tests/r02325.vtc
+++ b/bin/varnishtest/tests/r02325.vtc
@@ -1,5 +1,8 @@
varnishtest "validate storage identifiers"
-shell -err -expect "Error: invalid storage name (-s ...=malloc)" {
- varnishd -a :0 -n ${tmpdir} -F -f '' -s ...=malloc
+shell -err -expect {Error: invalid -s name "///"=[...]} {
+ varnishd -a :0 -n ${tmpdir} -F -f '' -s ///=malloc
+}
+shell -err -expect {Error: Empty named -s argument "foo="} {
+ varnishd -a :0 -n ${tmpdir} -F -f '' -s foo=
}
More information about the varnish-commit
mailing list