[6.0] 0f8e21002 centralize vcl state symbols / strings in the manager

Dridi Boukelmoune dridi.boukelmoune at gmail.com
Wed Feb 6 08:40:13 UTC 2019


commit 0f8e21002c086a485dc978e7e8240940ab835388
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Wed Nov 14 18:37:27 2018 +0100

    centralize vcl state symbols / strings in the manager
    
    The actual benefit of this will be in follow-up commits
    
    Conflicts:
            bin/varnishd/mgt/mgt_vcl.c

diff --git a/bin/varnishd/mgt/mgt_vcl.c b/bin/varnishd/mgt/mgt_vcl.c
index 8c05c5215..0cb29e44c 100644
--- a/bin/varnishd/mgt/mgt_vcl.c
+++ b/bin/varnishd/mgt/mgt_vcl.c
@@ -48,9 +48,10 @@
 #include "vev.h"
 #include "vtim.h"
 
-static const char * const VCL_STATE_COLD = "cold";
-static const char * const VCL_STATE_WARM = "warm";
-static const char * const VCL_STATE_AUTO = "auto";
+#define VCL_STATE(sym, str)					\
+	static const char * const VCL_STATE_ ## sym = str;
+#include "tbl/vcl_states.h"
+
 static const char * const VCL_STATE_LABEL = "label";
 
 struct vclprog;
@@ -106,6 +107,18 @@ static int mgt_vcl_setstate(struct cli *, struct vclprog *, const char *);
 
 /*--------------------------------------------------------------------*/
 
+static const char *
+mcf_vcl_parse_state(const char *s)
+{
+	if (s == NULL || *s == '\0')
+		return (NULL);
+#define VCL_STATE(sym, str)					\
+	if (!strcmp(s, VCL_STATE_ ## sym))			\
+		return (VCL_STATE_ ## sym);
+#include "tbl/vcl_states.h"
+	return (NULL);
+}
+
 static struct vclprog *
 mcf_vcl_byname(const char *name)
 {
@@ -171,7 +184,7 @@ mcf_find_no_vcl(struct cli *cli, const char *name)
 static int
 mcf_is_label(const struct vclprog *vp)
 {
-	return (!strcmp(vp->state, VCL_STATE_LABEL));
+	return (vp->state == VCL_STATE_LABEL);
 }
 
 /*--------------------------------------------------------------------*/
@@ -469,13 +482,10 @@ mgt_new_vcl(struct cli *cli, const char *vclname, const char *vclsrc,
 
 	if (state == NULL)
 		state = VCL_STATE_AUTO;
-	else if (!strcmp(state, VCL_STATE_AUTO))
-		state = VCL_STATE_AUTO;
-	else if (!strcmp(state, VCL_STATE_COLD))
-		state = VCL_STATE_COLD;
-	else if (!strcmp(state, VCL_STATE_WARM))
-		state = VCL_STATE_WARM;
-	else {
+	else
+		state = mcf_vcl_parse_state(state);
+
+	if (state == NULL) {
 		VCLI_Out(cli, "State must be one of auto, cold or warm.");
 		VCLI_SetResult(cli, CLIS_PARAM);
 		return;
@@ -507,7 +517,7 @@ mgt_new_vcl(struct cli *cli, const char *vclname, const char *vclsrc,
 	}
 	free(p);
 
-	if (vp->warm && !strcmp(vp->state, "auto"))
+	if (vp->warm && vp->state == VCL_STATE_AUTO)
 		vp->go_cold = VTIM_mono();
 }
 
@@ -626,6 +636,7 @@ mcf_vcl_load(struct cli *cli, const char * const *av, void *priv)
 static void v_matchproto_(cli_func_t)
 mcf_vcl_state(struct cli *cli, const char * const *av, void *priv)
 {
+	const char *state;
 	struct vclprog *vp;
 
 	(void)priv;
@@ -638,22 +649,30 @@ mcf_vcl_state(struct cli *cli, const char * const *av, void *priv)
 		VCLI_SetResult(cli, CLIS_PARAM);
 		return;
 	}
+
+	state = mcf_vcl_parse_state(av[3]);
+	if (state == NULL) {
+		VCLI_Out(cli, "State must be one of auto, cold or warm.");
+		VCLI_SetResult(cli, CLIS_PARAM);
+		return;
+	}
+
 	if (!VTAILQ_EMPTY(&vp->dto)) {
-		AN(strcmp(vp->state, "cold"));
-		if (!strcmp(av[3], "cold")) {
+		assert(vp->state != VCL_STATE_COLD);
+		if (state == VCL_STATE_COLD) {
 			VCLI_Out(cli, "A labeled VCL cannot be set cold");
 			VCLI_SetResult(cli, CLIS_CANT);
 			return;
 		}
 	}
 
-	if (!strcmp(vp->state, av[3]))
+	if (vp->state == state)
 		return;
 
-	if (!strcmp(av[3], VCL_STATE_AUTO)) {
+	if (state == VCL_STATE_AUTO) {
 		vp->state = VCL_STATE_AUTO;
 		(void)mgt_vcl_setstate(cli, vp, VCL_STATE_AUTO);
-	} else if (!strcmp(av[3], VCL_STATE_COLD)) {
+	} else if (state == VCL_STATE_COLD) {
 		if (vp == active_vcl) {
 			VCLI_Out(cli, "Cannot set the active VCL cold.");
 			VCLI_SetResult(cli, CLIS_CANT);
@@ -661,7 +680,7 @@ mcf_vcl_state(struct cli *cli, const char * const *av, void *priv)
 		}
 		vp->state = VCL_STATE_AUTO;
 		(void)mgt_vcl_setstate(cli, vp, VCL_STATE_COLD);
-	} else if (!strcmp(av[3], VCL_STATE_WARM)) {
+	} else if (state == VCL_STATE_WARM) {
 		if (mgt_vcl_setstate(cli, vp, VCL_STATE_WARM) == 0)
 			vp->state = VCL_STATE_WARM;
 	} else {
diff --git a/include/tbl/vcl_states.h b/include/tbl/vcl_states.h
new file mode 100644
index 000000000..6fe74dad1
--- /dev/null
+++ b/include/tbl/vcl_states.h
@@ -0,0 +1,8 @@
+/*lint -save -e525 -e539 */
+VCL_STATE(COLD, "cold")
+VCL_STATE(WARM, "warm")
+VCL_STATE(AUTO, "auto")
+#undef VCL_STATE
+// LABEL is private to mgt_vcl.c
+
+/*lint -restore */


More information about the varnish-commit mailing list