[6.0] 8700e538c varnishtest: Replace macro_get() with macro_cat()

Dridi Boukelmoune dridi.boukelmoune at gmail.com
Thu Apr 4 14:33:07 UTC 2024


commit 8700e538cfe23c7ea61ca93cb4beea80f4acdf06
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.
    
    Conflicts:
            bin/varnishtest/vtc_http.c

diff --git a/bin/varnishtest/vtc.c b/bin/varnishtest/vtc.c
index c970543ec..e9e178ccc 100644
--- a/bin/varnishtest/vtc.c
+++ b/bin/varnishtest/vtc.c
@@ -187,8 +187,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;
@@ -205,7 +205,9 @@ macro_get(const char *b, const char *e)
 		retval = malloc(64);
 		AN(retval);
 		VTIM_format(t, retval);
-		return (retval);
+		VSB_cat(vsb, retval);
+		free(retval);
+		return;
 	}
 
 	AZ(pthread_mutex_lock(&macro_mtx));
@@ -215,9 +217,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 *
@@ -242,7 +254,6 @@ macro_expand(struct vtclog *vl, const char *text)
 {
 	struct vsb *vsb;
 	const char *p, *q;
-	char *m;
 
 	vsb = VSB_new_auto();
 	AN(vsb);
@@ -262,19 +273,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));
@@ -509,7 +508,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);
 
@@ -529,12 +527,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 c091afe40..7c705bf29 100644
--- a/bin/varnishtest/vtc.h
+++ b/bin/varnishtest/vtc.h
@@ -116,7 +116,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 9cd701f8f..828492b6b 100644
--- a/bin/varnishtest/vtc_http.c
+++ b/bin/varnishtest/vtc_http.c
@@ -849,11 +849,9 @@ http_tx_parse_args(char * const *av, struct vtclog *vl, struct http *hp,
 	int bodylen = 0;
 	char *b, *c;
 	char *nullbody;
-	char *m;
 	int nolen = 0;
 	int l;
 
-	(void)vl;
 	nullbody = body;
 
 	for (; *av != NULL; av++) {
@@ -926,10 +924,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: %d%s", bodylen, nl);


More information about the varnish-commit mailing list