[master] e6746bd Introduce VSB_quote_pfx() which prefixes each line with a string.

Poul-Henning Kamp phk at FreeBSD.org
Thu Jan 19 13:56:04 CET 2017


commit e6746bd90cb6a50843356c808caa1eae46627082
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Thu Jan 19 12:22:39 2017 +0000

    Introduce VSB_quote_pfx() which prefixes each line with a string.
    
    This *mainly* affects VSB_QUOTE_NONL.
    
    Make sure VSB_QUOTE_NONL always ends with a NL

diff --git a/include/vsb.h b/include/vsb.h
index 35e752b..0f5bfdc 100644
--- a/include/vsb.h
+++ b/include/vsb.h
@@ -80,6 +80,8 @@ void		 VSB_destroy(struct vsb **);
 #define VSB_QUOTE_JSON	2
 #define VSB_QUOTE_HEX	4
 #define VSB_QUOTE_CSTR	8
+void		 VSB_quote_pfx(struct vsb *, const char*, const void *,
+		     int len, int how);
 void		 VSB_quote(struct vsb *, const void *, int len, int how);
 void		 VSB_indent(struct vsb *, int);
 #ifdef __cplusplus
diff --git a/lib/libvarnish/vsb.c b/lib/libvarnish/vsb.c
index b713642..99e6f50 100644
--- a/lib/libvarnish/vsb.c
+++ b/lib/libvarnish/vsb.c
@@ -500,17 +500,26 @@ VSB_destroy(struct vsb **s)
  * Quote a string
  */
 void
-VSB_quote(struct vsb *s, const void *v, int len, int how)
+VSB_quote_pfx(struct vsb *s, const char *pfx, const void *v, int len, int how)
 {
 	const char *p;
 	const char *q;
 	int quote = 0;
+	int nl = 0;
 	const unsigned char *u, *w;
 
 	assert(v != NULL);
 	if (len == -1)
 		len = strlen(v);
 
+	if (len == 0 && (how & VSB_QUOTE_CSTR)) {
+		VSB_printf(s, "%s\"\"", pfx);
+		return;
+	} else if (len == 0)
+		return;
+
+	VSB_cat(s, pfx);
+
 	if (how & VSB_QUOTE_HEX) {
 		u = v;
 		for (w = u; w < u + len; w++)
@@ -537,8 +546,14 @@ VSB_quote(struct vsb *s, const void *v, int len, int how)
 		(void)VSB_bcat(s, p, len);
 		return;
 	}
-	(void)VSB_putc(s, '"');
+
+	if (how & VSB_QUOTE_CSTR)
+		(void)VSB_putc(s, '"');
+
 	for (q = p; q < p + len; q++) {
+		if (nl)
+			VSB_cat(s, pfx);
+		nl = 0;
 		switch (*q) {
 		case '?':
 			if (how & VSB_QUOTE_CSTR)
@@ -554,12 +569,14 @@ VSB_quote(struct vsb *s, const void *v, int len, int how)
 			(void)VSB_putc(s, *q);
 			break;
 		case '\n':
-			if (how & VSB_QUOTE_CSTR)
-				(void)VSB_cat(s, "\\n\"\n\t\"");
-			else if (how & VSB_QUOTE_NONL)
-				(void)VSB_cat(s, "\n");
-			else
-				(void)VSB_cat(s, "\\n");
+			if (how & VSB_QUOTE_CSTR) {
+				(void)VSB_printf(s, "\\n\"\n%s\t\"", pfx);
+			} else if (how & VSB_QUOTE_NONL) {
+				(void)VSB_printf(s, "\n");
+				nl = 1;
+			} else {
+				(void)VSB_printf(s, "\\n");
+			}
 			break;
 		case '\r':
 			(void)VSB_cat(s, "\\r");
@@ -576,7 +593,16 @@ VSB_quote(struct vsb *s, const void *v, int len, int how)
 			break;
 		}
 	}
-	(void)VSB_putc(s, '"');
+	if (how & VSB_QUOTE_CSTR)
+		(void)VSB_putc(s, '"');
+	if ((how & VSB_QUOTE_NONL && !nl))
+		(void)VSB_putc(s, '\n');
+}
+
+void
+VSB_quote(struct vsb *s, const void *v, int len, int how)
+{
+	VSB_quote_pfx(s, "", v, len, how);
 }
 
 /*



More information about the varnish-commit mailing list