r811 - trunk/varnish-cache/lib/libvcl

phk at projects.linpro.no phk at projects.linpro.no
Fri Aug 11 13:23:12 CEST 2006


Author: phk
Date: 2006-08-11 13:23:12 +0200 (Fri, 11 Aug 2006)
New Revision: 811

Modified:
   trunk/varnish-cache/lib/libvcl/vcc_acl.c
   trunk/varnish-cache/lib/libvcl/vcc_compile.c
   trunk/varnish-cache/lib/libvcl/vcc_compile.h
Log:
Use the already decoded CSTR where applicable and use
EncString() to encode strings for C source usage.


Modified: trunk/varnish-cache/lib/libvcl/vcc_acl.c
===================================================================
--- trunk/varnish-cache/lib/libvcl/vcc_acl.c	2006-08-11 11:22:33 UTC (rev 810)
+++ trunk/varnish-cache/lib/libvcl/vcc_acl.c	2006-08-11 11:23:12 UTC (rev 811)
@@ -53,7 +53,6 @@
 {
 	unsigned mask, para, not;
 	struct token *t, *an;
-	char *p;
 
 	vcc_NextToken(tl);
 
@@ -93,14 +92,16 @@
 			ExpectErr(tl, CNUM);
 			mask = UintVal(tl);
 		} 
-		Fc(tl, 1, "{ %u, %u, %u, %.*s, \"", not, mask, para, PF(t));
+		Fc(tl, 1, "{ %u, %u, %u, ", not, mask, para);
+		EncString(tl->fc, t);
+		Fc(tl, 0, ", \"");
 		if (para)
 			Fc(tl, 0, "(");
 		if (not)
 			Fc(tl, 0, "!");
-		p = EncString(t);
-		Fc(tl, 0, "%s", p);
-		free(p);
+		Fc(tl, 0, "\\\"\" ");
+		EncString(tl->fc, t);
+		Fc(tl, 0, " \"\\\"");
 		if (mask)
 			Fc(tl, 0, "/%u", mask);
 		if (para)

Modified: trunk/varnish-cache/lib/libvcl/vcc_compile.c
===================================================================
--- trunk/varnish-cache/lib/libvcl/vcc_compile.c	2006-08-11 11:22:33 UTC (rev 810)
+++ trunk/varnish-cache/lib/libvcl/vcc_compile.c	2006-08-11 11:23:12 UTC (rev 811)
@@ -142,53 +142,22 @@
 
 /*--------------------------------------------------------------------*/
 
-char *
-EncString(struct token *t)
+void
+EncString(struct vsb *sb, struct token *t)
 {
-	char *p, *q;
-	const char *r;
-	unsigned u;
+	const char *p;
 
 	assert(t->tok == CSTR);
-	p = malloc(t->e - t->b);
-	assert(p != NULL);
-	q = p;
-	for (r = t->b + 1; r < t->e - 1; ) {
-		if (*r != '\\') {
-			*q++ = *r++;
-			continue;
-		}
-		switch (r[1]) {
-		case 'n':	*q++ = '\n';	r += 2; break;
-		case 'r':	*q++ = '\r';	r += 2; break;
-		case 'v':	*q++ = '\v';	r += 2; break;
-		case 'f':	*q++ = '\f';	r += 2; break;
-		case 't':	*q++ = '\t';	r += 2; break;
-		case 'b':	*q++ = '\b';	r += 2; break;
-		case '0': case '1': case '2': case '3':
-		case '4': case '5': case '6': case '7':
-			u = r[1] - '0';
-			r += 2;
-			if (isdigit(r[0]) && (r[0] - '0') < 8) {
-				u <<= 3;
-				u |= r[0] - '0';
-				r++;
-				if (isdigit(r[0]) && (r[0] - '0') < 8) {
-					u <<= 3;
-					u |= r[0] - '0';
-					r++;
-				}
-			}
-			*q++ = u;
-			break;
-		default:
-			*q++ = r[1];	
-			r += 2;
-			break;
-		}
+	vsb_cat(sb, "\"");
+	for (p = t->dec; *p != '\0'; p++) {
+		if (*p == '\\' || *p == '"')
+			vsb_printf(sb, "\\%c", *p);
+		else if (isgraph(*p))
+			vsb_printf(sb, "%c", *p);
+		else
+			vsb_printf(sb, "\\%03o", *p);
 	}
-	*q = '\0';
-	return (p);
+	vsb_cat(sb, "\"");
 }
 
 /*--------------------------------------------------------------------*/
@@ -492,19 +461,20 @@
 static void
 vcc_re(struct tokenlist *tl, const char *str, struct token *re)
 {
-	char buf[32], *p;
+	char buf[32];
 
-	p = EncString(re);
-	if (VRT_re_test(tl->sb, p)) {
+	assert(re->tok == CSTR);
+	if (VRT_re_test(tl->sb, re->dec)) {
 		vcc_ErrWhere(tl, re);
 		return;
 	}
-	free(p);
 	sprintf(buf, "VGC_re_%u", tl->recnt++);
 
 	Fc(tl, 1, "VRT_re_match(%s, %s)\n", str, buf);
 	Fh(tl, 0, "void *%s;\n", buf);
-	Fi(tl, 0, "\tVRT_re_init(&%s, %.*s);\n", buf, PF(re));
+	Fi(tl, 0, "\tVRT_re_init(&%s, ",buf);
+	EncString(tl->fi, re);
+	Fi(tl, 0, ");\n");
 	Ff(tl, 0, "\tVRT_re_fini(%s);\n", buf);
 }
 
@@ -528,7 +498,8 @@
 		    tl->t->tok == T_EQ ? "!" : "", vp->rname);
 		vcc_NextToken(tl);
 		ExpectErr(tl, CSTR);
-		Fc(tl, 0, "%.*s)\n", PF(tl->t));
+		EncString(tl->fc, tl->t);
+		Fc(tl, 0, ")\n");
 		vcc_NextToken(tl);
 		break;
 	default:
@@ -931,8 +902,6 @@
 	struct token *t_be = NULL;
 	struct token *t_host = NULL;
 	struct token *t_port = NULL;
-	char *host = NULL;
-	char *port = NULL;
 	const char *ep;
 
 	vcc_NextToken(tl);
@@ -969,13 +938,17 @@
 		case HOSTNAME:
 			ExpectErr(tl, CSTR);
 			t_host = tl->t;
-			Fc(tl, 1, "\t%s %.*s);\n", vp->lname, PF(tl->t));
+			Fc(tl, 1, "\t%s ", vp->lname);
+			EncString(tl->fc, t_host);
+			Fc(tl, 0, ");\n");
 			vcc_NextToken(tl);
 			break;
 		case PORTNAME:
 			ExpectErr(tl, CSTR);
 			t_port = tl->t;
-			Fc(tl, 1, "\t%s %.*s);\n", vp->lname, PF(tl->t));
+			Fc(tl, 1, "\t%s ", vp->lname);
+			EncString(tl->fc, t_port);
+			Fc(tl, 0, ");\n");
 			vcc_NextToken(tl);
 			break;
 		default:
@@ -994,18 +967,17 @@
 		vcc_ErrWhere(tl, tl->t);
 		return;
 	}
-	host = EncString(t_host);
-	ep = CheckHostPort(host, "80");
+	ep = CheckHostPort(t_host->dec, "80");
 	if (ep != NULL) {
 		vsb_printf(tl->sb, "Backend '%.*s': %s\n", PF(t_be), ep);
 		vcc_ErrWhere(tl, t_host);
 		return;
 	}
 	if (t_port != NULL) {
-		port = EncString(t_port);
-		ep = CheckHostPort(host, port);
+		ep = CheckHostPort(t_host->dec, t_port->dec);
 		if (ep != NULL) {
-			vsb_printf(tl->sb, "Backend '%.*s': %s\n", PF(t_be), ep);
+			vsb_printf(tl->sb,
+			    "Backend '%.*s': %s\n", PF(t_be), ep);
 			vcc_ErrWhere(tl, t_port);
 			return;
 		}

Modified: trunk/varnish-cache/lib/libvcl/vcc_compile.h
===================================================================
--- trunk/varnish-cache/lib/libvcl/vcc_compile.h	2006-08-11 11:22:33 UTC (rev 810)
+++ trunk/varnish-cache/lib/libvcl/vcc_compile.h	2006-08-11 11:23:12 UTC (rev 811)
@@ -111,7 +111,7 @@
 unsigned UintVal(struct tokenlist *tl);
 void AddDef(struct tokenlist *tl, struct token *t, enum ref_type type);
 void AddRef(struct tokenlist *tl, struct token *t, enum ref_type type);
-char *EncString(struct token *t);
+void EncString(struct vsb *sb, struct token *t);
 
 
 /* vcc_obj.c */




More information about the varnish-commit mailing list