[4.1] fb06777 Implementation of VSB auto-indent facility

Poul-Henning Kamp phk at FreeBSD.org
Fri Sep 4 15:54:51 CEST 2015


commit fb06777292fe29f5571a2a406dc374ee450355c2
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Fri Jul 31 08:08:27 2015 +0000

    Implementation of VSB auto-indent facility

diff --git a/lib/libvarnish/vsb.c b/lib/libvarnish/vsb.c
index 1332844..be147c2 100644
--- a/lib/libvarnish/vsb.c
+++ b/lib/libvarnish/vsb.c
@@ -114,7 +114,6 @@ CTASSERT(powerof2(VSB_MAXEXTENDSIZE));
 CTASSERT(powerof2(VSB_MAXEXTENDINCR));
 #endif
 
-
 static int
 VSB_extendsize(int size)
 {
@@ -156,6 +155,21 @@ VSB_extend(struct vsb *s, int addlen)
 	return (0);
 }
 
+static void
+_vsb_indent(struct vsb *s)
+{
+	if (s->s_indent == 0 || s->s_error != 0 ||
+	    s->s_buf[s->s_len - 1] != '\n')
+		return;
+	if (VSB_FREESPACE(s) <= s->s_indent &&
+	    VSB_extend(s, s->s_indent) < 0) {
+		s->s_error = ENOMEM;
+		return;
+	}
+	memset(s->s_buf + s->s_len, ' ', s->s_indent);
+	s->s_len += s->s_indent;
+}
+
 /*
  * Initialize the internals of an vsb.
  * If buf is non-NULL, it points to a static or already-allocated string
@@ -231,6 +245,7 @@ VSB_clear(struct vsb *s)
 	VSB_CLEARFLAG(s, VSB_FINISHED);
 	s->s_error = 0;
 	s->s_len = 0;
+	s->s_indent = 0;
 }
 
 /*
@@ -247,6 +262,7 @@ VSB_put_byte(struct vsb *s, int c)
 
 	if (s->s_error != 0)
 		return;
+	_vsb_indent(s);
 	if (VSB_FREESPACE(s) <= 0) {
 		if (VSB_extend(s, 1) < 0)
 			s->s_error = ENOMEM;
@@ -270,6 +286,7 @@ VSB_bcat(struct vsb *s, const void *buf, size_t len)
 
 	if (s->s_error != 0)
 		return (-1);
+	_vsb_indent(s);
 	for (; str < end; str++) {
 		VSB_put_byte(s, *str);
 		if (s->s_error != 0)
@@ -316,6 +333,7 @@ VSB_vprintf(struct vsb *s, const char *fmt, va_list ap)
 
 	if (s->s_error != 0)
 		return (-1);
+	_vsb_indent(s);
 
 	/*
 	 * For the moment, there is no way to get vsnprintf(3) to hand
@@ -515,3 +533,18 @@ VSB_quote(struct vsb *s, const char *p, int len, int how)
 	}
 	(void)VSB_putc(s, '"');
 }
+
+/*
+ * Indentation
+ */
+
+void
+VSB_indent(struct vsb * s, int i)
+{
+
+	assert_VSB_integrity(s);
+	if (s->s_indent + i < 0)
+		s->s_error = EINVAL;
+	else
+		s->s_indent += i;
+}



More information about the varnish-commit mailing list