[master] eb71eae78 varnishtest: Replace macro_get() with macro_cat()

Dridi Boukelmoune dridi.boukelmoune at gmail.com
Mon Jul 5 15:48:05 UTC 2021


commit eb71eae78cf71412913b66517ab644630e4c21ec
Author: Dridi Boukelmoune <dridi.boukelmoune at gmail.com>
Date:   Thu Jul 1 08:37:15 2021 +0200

    varnishtest: Replace macro_get() with macro_cat()
    
    The latter operates on a VSB, which is always what call sites are doing
    anyway. It also takes the responsibility of ignoring unknown macros, in
    preparation for more responsibilities that will also require the ability
    to fail a test case.

diff --git a/bin/varnishtest/vtc.c b/bin/varnishtest/vtc.c
index 7319d4d43..7a9eb6144 100644
--- a/bin/varnishtest/vtc.c
+++ b/bin/varnishtest/vtc.c
@@ -204,8 +204,8 @@ macro_undef(struct vtclog *vl, const char *instance, const char *name)
 	AZ(pthread_mutex_unlock(&macro_mtx));
 }
 
-char *
-macro_get(const char *b, const char *e)
+void
+macro_cat(struct vtclog *vl, struct vsb *vsb, const char *b, const char *e)
 {
 	struct macro *m;
 	int l;
@@ -222,7 +222,9 @@ macro_get(const char *b, const char *e)
 		retval = malloc(VTIM_FORMAT_SIZE);
 		AN(retval);
 		VTIM_format(t, retval);
-		return (retval);
+		VSB_cat(vsb, retval);
+		free(retval);
+		return;
 	}
 
 	AZ(pthread_mutex_lock(&macro_mtx));
@@ -232,9 +234,19 @@ macro_get(const char *b, const char *e)
 			break;
 	}
 	if (m != NULL)
-		retval = strdup(m->val);
+		REPLACE(retval, m->val);
 	AZ(pthread_mutex_unlock(&macro_mtx));
-	return (retval);
+
+	if (retval == NULL) {
+		if (!ign_unknown_macro)
+			vtc_fatal(vl, "Macro ${%.*s} not found",
+			    (int)(e - b), b);
+		VSB_printf(vsb, "${%.*s}", (int)(e - b), b);
+		return;
+	}
+
+	VSB_cat(vsb, retval);
+	free(retval);
 }
 
 struct vsb *
@@ -259,7 +271,6 @@ macro_expand(struct vtclog *vl, const char *text)
 {
 	struct vsb *vsb;
 	const char *p, *q;
-	char *m;
 
 	vsb = VSB_new_auto();
 	AN(vsb);
@@ -279,19 +290,7 @@ macro_expand(struct vtclog *vl, const char *text)
 		assert(p[1] == '{');
 		assert(q[0] == '}');
 		p += 2;
-		m = macro_get(p, q);
-		if (m == NULL) {
-			if (!ign_unknown_macro) {
-				VSB_destroy(&vsb);
-				vtc_fatal(vl, "Macro ${%.*s} not found",
-					  (int)(q - p), p);
-				NEEDLESS(return (NULL));
-			}
-			VSB_printf(vsb, "${%.*s}", (int)(q - p), p);
-		} else {
-			VSB_printf(vsb, "%s", m);
-			free(m);
-		}
+		macro_cat(vl, vsb, p, q);
 		text = q + 1;
 	}
 	AZ(VSB_finish(vsb));
@@ -513,7 +512,6 @@ exec_file(const char *fn, const char *script, const char *tmpdir,
 	FILE *f;
 	struct vsb *vsb;
 	const char *p;
-	char *q;
 
 	(void)signal(SIGPIPE, SIG_IGN);
 
@@ -537,12 +535,8 @@ exec_file(const char *fn, const char *script, const char *tmpdir,
 
 	vsb = VSB_new_auto();
 	AN(vsb);
-	if (*fn != '/') {
-		q = macro_get("pwd", NULL);
-		AN(q);
-		VSB_cat(vsb, q);
-		free(q);
-	}
+	if (*fn != '/')
+		macro_cat(vltop, vsb, "pwd", NULL);
 	p = strrchr(fn, '/');
 	if (p != NULL) {
 		VSB_putc(vsb, '/');
diff --git a/bin/varnishtest/vtc.h b/bin/varnishtest/vtc.h
index 5380f74cd..6e6978018 100644
--- a/bin/varnishtest/vtc.h
+++ b/bin/varnishtest/vtc.h
@@ -134,7 +134,7 @@ void macro_undef(struct vtclog *vl, const char *instance, const char *name);
 void macro_def(struct vtclog *vl, const char *instance, const char *name,
     const char *fmt, ...)
     v_printflike_(4, 5);
-char *macro_get(const char *, const char *);
+void macro_cat(struct vtclog *, struct vsb *, const char *, const char *);
 struct vsb *macro_expand(struct vtclog *vl, const char *text);
 struct vsb *macro_expandf(struct vtclog *vl, const char *, ...)
     v_printflike_(2, 3);
diff --git a/bin/varnishtest/vtc_http.c b/bin/varnishtest/vtc_http.c
index dc286066a..af3f31e43 100644
--- a/bin/varnishtest/vtc_http.c
+++ b/bin/varnishtest/vtc_http.c
@@ -774,12 +774,10 @@ http_tx_parse_args(char * const *av, struct vtclog *vl, struct http *hp,
 	long bodylen = 0;
 	char *b, *c;
 	char *nullbody;
-	char *m;
 	ssize_t len;
 	int nolen = 0;
 	int l;
 
-	(void)vl;
 	nullbody = body;
 
 	for (; *av != NULL; av++) {
@@ -860,10 +858,9 @@ http_tx_parse_args(char * const *av, struct vtclog *vl, struct http *hp,
 			break;
 	}
 	if (!nohost) {
-		m = macro_get("localhost", NULL);
-		AN(m);
-		VSB_printf(hp->vsb, "Host: %s%s", m, nl);
-		free(m);
+		VSB_cat(hp->vsb, "Host: ");
+		macro_cat(vl, hp->vsb, "localhost", NULL);
+		VSB_cat(hp->vsb, nl);
 	}
 	if (body != NULL && !nolen)
 		VSB_printf(hp->vsb, "Content-Length: %ld%s", bodylen, nl);


More information about the varnish-commit mailing list