[master] 9c1d4fb6d param: Fold mgt_param_bits.c into mgt_param_tweak.c

Dridi Boukelmoune dridi.boukelmoune at gmail.com
Mon May 30 14:04:06 UTC 2022


commit 9c1d4fb6d62b82417f30b282f56f39bb68d3503a
Author: Dridi Boukelmoune <dridi.boukelmoune at gmail.com>
Date:   Fri Mar 11 18:58:42 2022 +0100

    param: Fold mgt_param_bits.c into mgt_param_tweak.c

diff --git a/bin/varnishd/Makefile.am b/bin/varnishd/Makefile.am
index 79383fbb6..aabfce050 100644
--- a/bin/varnishd/Makefile.am
+++ b/bin/varnishd/Makefile.am
@@ -86,7 +86,6 @@ varnishd_SOURCES = \
 	mgt/mgt_jail_unix.c \
 	mgt/mgt_main.c \
 	mgt/mgt_param.c \
-	mgt/mgt_param_bits.c \
 	mgt/mgt_param_tcp.c \
 	mgt/mgt_param_tweak.c \
 	mgt/mgt_shmem.c \
diff --git a/bin/varnishd/mgt/mgt_param_bits.c b/bin/varnishd/mgt/mgt_param_bits.c
deleted file mode 100644
index 3ec359ffe..000000000
--- a/bin/varnishd/mgt/mgt_param_bits.c
+++ /dev/null
@@ -1,217 +0,0 @@
-/*-
- * Copyright (c) 2006 Verdens Gang AS
- * Copyright (c) 2006-2011 Varnish Software AS
- * All rights reserved.
- *
- * Author: Poul-Henning Kamp <phk at phk.freebsd.dk>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- *
- * 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.
- */
-
-#include "config.h"
-
-#include <string.h>
-
-#include "mgt/mgt.h"
-#include "mgt/mgt_param.h"
-
-#include "vav.h"
-
-#include "vsl_priv.h"
-
-/*--------------------------------------------------------------------
- */
-
-enum bit_do {BSET, BCLR, BTST};
-
-static int
-bit(uint8_t *p, unsigned no, enum bit_do act)
-{
-	uint8_t b;
-
-	p += (no >> 3);
-	b = (0x80 >> (no & 7));
-	if (act == BSET)
-		*p |= b;
-	else if (act == BCLR)
-		*p &= ~b;
-	return (*p & b);
-}
-
-/*--------------------------------------------------------------------
- */
-
-static int
-bit_tweak(struct vsb *vsb, uint8_t *p, unsigned l, const char *arg,
-    const char * const *tags, const char *desc, char sign)
-{
-	int i, n;
-	unsigned j;
-	char **av;
-	const char *s;
-
-	av = VAV_Parse(arg, &n, ARGV_COMMA);
-	if (av[0] != NULL) {
-		VSB_printf(vsb, "Cannot parse: %s\n", av[0]);
-		VAV_Free(av);
-		return (-1);
-	}
-	for (i = 1; av[i] != NULL; i++) {
-		s = av[i];
-		if (*s != '-' && *s != '+') {
-			VSB_printf(vsb, "Missing '+' or '-' (%s)\n", s);
-			VAV_Free(av);
-			return (-1);
-		}
-		for (j = 0; j < l; j++) {
-			if (tags[j] != NULL && !strcasecmp(s + 1, tags[j]))
-				break;
-		}
-		if (tags[j] == NULL) {
-			VSB_printf(vsb, "Unknown %s (%s)\n", desc, s);
-			VAV_Free(av);
-			return (-1);
-		}
-		assert(j < l);
-		if (s[0] == sign)
-			(void)bit(p, j, BSET);
-		else
-			(void)bit(p, j, BCLR);
-	}
-	VAV_Free(av);
-	return (0);
-}
-
-
-/*--------------------------------------------------------------------
- */
-
-static int
-tweak_generic_bits(struct vsb *vsb, const struct parspec *par, const char *arg,
-    uint8_t *p, unsigned l, const char * const *tags, const char *desc,
-    char sign)
-{
-	const char *s;
-	unsigned j;
-
-	if (arg != NULL && !strcmp(arg, "default") &&
-	    strcmp(par->def, "none")) {
-		memset(p, 0, l >> 3);
-		return (tweak_generic_bits(vsb, par, par->def, p, l, tags,
-		    desc, sign));
-	}
-
-	if (arg != NULL && arg != JSON_FMT) {
-		if (sign == '+' && !strcmp(arg, "none"))
-			memset(p, 0, l >> 3);
-		else
-			return (bit_tweak(vsb, p, l, arg, tags, desc, sign));
-	} else {
-		if (arg == JSON_FMT)
-			VSB_putc(vsb, '"');
-		s = "";
-		for (j = 0; j < l; j++) {
-			if (bit(p, j, BTST)) {
-				VSB_printf(vsb, "%s%c%s", s, sign, tags[j]);
-				s = ",";
-			}
-		}
-		if (*s == '\0')
-			VSB_cat(vsb, sign == '+' ? "none" : "(all enabled)");
-		if (arg == JSON_FMT)
-			VSB_putc(vsb, '"');
-	}
-	return (0);
-}
-
-/*--------------------------------------------------------------------
- * The vsl_mask parameter
- */
-
-static const char * const VSL_tags[256] = {
-#  define SLTM(foo,flags,sdesc,ldesc) [SLT_##foo] = #foo,
-#  include "tbl/vsl_tags.h"
-};
-
-int v_matchproto_(tweak_t)
-tweak_vsl_mask(struct vsb *vsb, const struct parspec *par, const char *arg)
-{
-
-	return (tweak_generic_bits(vsb, par, arg, mgt_param.vsl_mask,
-	    SLT__Reserved, VSL_tags, "VSL tag", '-'));
-}
-
-/*--------------------------------------------------------------------
- * The debug parameter
- */
-
-static const char * const debug_tags[] = {
-#  define DEBUG_BIT(U, l, d) [DBG_##U] = #l,
-#  include "tbl/debug_bits.h"
-       NULL
-};
-
-int v_matchproto_(tweak_t)
-tweak_debug(struct vsb *vsb, const struct parspec *par, const char *arg)
-{
-
-	return (tweak_generic_bits(vsb, par, arg, mgt_param.debug_bits,
-	    DBG_Reserved, debug_tags, "debug bit", '+'));
-}
-
-/*--------------------------------------------------------------------
- * The experimental parameter
- */
-
-static const char * const experimental_tags[] = {
-#  define EXPERIMENTAL_BIT(U, l, d) [EXPERIMENT_##U] = #l,
-#  include "tbl/experimental_bits.h"
-       NULL
-};
-
-int v_matchproto_(tweak_t)
-tweak_experimental(struct vsb *vsb, const struct parspec *par, const char *arg)
-{
-
-	return (tweak_generic_bits(vsb, par, arg, mgt_param.experimental_bits,
-	    EXPERIMENT_Reserved, experimental_tags, "experimental bit", '+'));
-}
-
-/*--------------------------------------------------------------------
- * The feature parameter
- */
-
-static const char * const feature_tags[] = {
-#  define FEATURE_BIT(U, l, d) [FEATURE_##U] = #l,
-#  include "tbl/feature_bits.h"
-       NULL
-};
-
-int v_matchproto_(tweak_t)
-tweak_feature(struct vsb *vsb, const struct parspec *par, const char *arg)
-{
-
-	return (tweak_generic_bits(vsb, par, arg, mgt_param.feature_bits,
-	    FEATURE_Reserved, feature_tags, "feature bit", '+'));
-}
diff --git a/bin/varnishd/mgt/mgt_param_tweak.c b/bin/varnishd/mgt/mgt_param_tweak.c
index 4b4d793e8..6f0e26803 100644
--- a/bin/varnishd/mgt/mgt_param_tweak.c
+++ b/bin/varnishd/mgt/mgt_param_tweak.c
@@ -45,6 +45,7 @@
 #include "storage/storage.h"
 #include "vav.h"
 #include "vnum.h"
+#include "vsl_priv.h"
 
 const char * const JSON_FMT = (const char *)&JSON_FMT;
 
@@ -568,3 +569,180 @@ tweak_alias(struct vsb *vsb, const struct parspec *par, const char *arg)
 	par = TRUST_ME(par->priv);
 	return (par->func(vsb, par, arg));
 }
+
+/*--------------------------------------------------------------------
+ * Tweak bits
+ */
+
+enum bit_do {BSET, BCLR, BTST};
+
+static int
+bit(uint8_t *p, unsigned no, enum bit_do act)
+{
+	uint8_t b;
+
+	p += (no >> 3);
+	b = (0x80 >> (no & 7));
+	if (act == BSET)
+		*p |= b;
+	else if (act == BCLR)
+		*p &= ~b;
+	return (*p & b);
+}
+
+/*--------------------------------------------------------------------
+ */
+
+static int
+bit_tweak(struct vsb *vsb, uint8_t *p, unsigned l, const char *arg,
+    const char * const *tags, const char *desc, char sign)
+{
+	int i, n;
+	unsigned j;
+	char **av;
+	const char *s;
+
+	av = VAV_Parse(arg, &n, ARGV_COMMA);
+	if (av[0] != NULL) {
+		VSB_printf(vsb, "Cannot parse: %s\n", av[0]);
+		VAV_Free(av);
+		return (-1);
+	}
+	for (i = 1; av[i] != NULL; i++) {
+		s = av[i];
+		if (*s != '-' && *s != '+') {
+			VSB_printf(vsb, "Missing '+' or '-' (%s)\n", s);
+			VAV_Free(av);
+			return (-1);
+		}
+		for (j = 0; j < l; j++) {
+			if (tags[j] != NULL && !strcasecmp(s + 1, tags[j]))
+				break;
+		}
+		if (tags[j] == NULL) {
+			VSB_printf(vsb, "Unknown %s (%s)\n", desc, s);
+			VAV_Free(av);
+			return (-1);
+		}
+		assert(j < l);
+		if (s[0] == sign)
+			(void)bit(p, j, BSET);
+		else
+			(void)bit(p, j, BCLR);
+	}
+	VAV_Free(av);
+	return (0);
+}
+
+
+/*--------------------------------------------------------------------
+ */
+
+static int
+tweak_generic_bits(struct vsb *vsb, const struct parspec *par, const char *arg,
+    uint8_t *p, unsigned l, const char * const *tags, const char *desc,
+    char sign)
+{
+	const char *s;
+	unsigned j;
+
+	if (arg != NULL && !strcmp(arg, "default") &&
+	    strcmp(par->def, "none")) {
+		memset(p, 0, l >> 3);
+		return (tweak_generic_bits(vsb, par, par->def, p, l, tags,
+		    desc, sign));
+	}
+
+	if (arg != NULL && arg != JSON_FMT) {
+		if (sign == '+' && !strcmp(arg, "none"))
+			memset(p, 0, l >> 3);
+		else
+			return (bit_tweak(vsb, p, l, arg, tags, desc, sign));
+	} else {
+		if (arg == JSON_FMT)
+			VSB_putc(vsb, '"');
+		s = "";
+		for (j = 0; j < l; j++) {
+			if (bit(p, j, BTST)) {
+				VSB_printf(vsb, "%s%c%s", s, sign, tags[j]);
+				s = ",";
+			}
+		}
+		if (*s == '\0')
+			VSB_cat(vsb, sign == '+' ? "none" : "(all enabled)");
+		if (arg == JSON_FMT)
+			VSB_putc(vsb, '"');
+	}
+	return (0);
+}
+
+/*--------------------------------------------------------------------
+ * The vsl_mask parameter
+ */
+
+static const char * const VSL_tags[256] = {
+#  define SLTM(foo,flags,sdesc,ldesc) [SLT_##foo] = #foo,
+#  include "tbl/vsl_tags.h"
+};
+
+int v_matchproto_(tweak_t)
+tweak_vsl_mask(struct vsb *vsb, const struct parspec *par, const char *arg)
+{
+
+	return (tweak_generic_bits(vsb, par, arg, mgt_param.vsl_mask,
+	    SLT__Reserved, VSL_tags, "VSL tag", '-'));
+}
+
+/*--------------------------------------------------------------------
+ * The debug parameter
+ */
+
+static const char * const debug_tags[] = {
+#  define DEBUG_BIT(U, l, d) [DBG_##U] = #l,
+#  include "tbl/debug_bits.h"
+       NULL
+};
+
+int v_matchproto_(tweak_t)
+tweak_debug(struct vsb *vsb, const struct parspec *par, const char *arg)
+{
+
+	return (tweak_generic_bits(vsb, par, arg, mgt_param.debug_bits,
+	    DBG_Reserved, debug_tags, "debug bit", '+'));
+}
+
+/*--------------------------------------------------------------------
+ * The experimental parameter
+ */
+
+static const char * const experimental_tags[] = {
+#  define EXPERIMENTAL_BIT(U, l, d) [EXPERIMENT_##U] = #l,
+#  include "tbl/experimental_bits.h"
+       NULL
+};
+
+int v_matchproto_(tweak_t)
+tweak_experimental(struct vsb *vsb, const struct parspec *par, const char *arg)
+{
+
+	return (tweak_generic_bits(vsb, par, arg, mgt_param.experimental_bits,
+	    EXPERIMENT_Reserved, experimental_tags, "experimental bit", '+'));
+}
+
+/*--------------------------------------------------------------------
+ * The feature parameter
+ */
+
+static const char * const feature_tags[] = {
+#  define FEATURE_BIT(U, l, d) [FEATURE_##U] = #l,
+#  include "tbl/feature_bits.h"
+       NULL
+};
+
+int v_matchproto_(tweak_t)
+tweak_feature(struct vsb *vsb, const struct parspec *par, const char *arg)
+{
+
+	return (tweak_generic_bits(vsb, par, arg, mgt_param.feature_bits,
+	    FEATURE_Reserved, feature_tags, "feature bit", '+'));
+}


More information about the varnish-commit mailing list