r1662 - trunk/varnish-cache/lib/libvcl

phk at projects.linpro.no phk at projects.linpro.no
Tue Jul 10 21:46:17 CEST 2007


Author: phk
Date: 2007-07-10 21:46:16 +0200 (Tue, 10 Jul 2007)
New Revision: 1662

Added:
   trunk/varnish-cache/lib/libvcl/vcc_string.c
Modified:
   trunk/varnish-cache/lib/libvcl/Makefile.am
   trunk/varnish-cache/lib/libvcl/vcc_action.c
   trunk/varnish-cache/lib/libvcl/vcc_compile.h
   trunk/varnish-cache/lib/libvcl/vcc_var.c
Log:
Move string stuff to vcc_string.c, there's going to be a fair bit of it.

Give vcc_StringVal() a return value to say if it did anything so we can
emit better error messages when confused.



Modified: trunk/varnish-cache/lib/libvcl/Makefile.am
===================================================================
--- trunk/varnish-cache/lib/libvcl/Makefile.am	2007-07-09 20:35:20 UTC (rev 1661)
+++ trunk/varnish-cache/lib/libvcl/Makefile.am	2007-07-10 19:46:16 UTC (rev 1662)
@@ -16,6 +16,7 @@
 	vcc_parse.c \
 	vcc_fixed_token.c \
 	vcc_obj.c \
+	vcc_string.c \
 	vcc_token.c \
 	vcc_var.c \
 	vcc_xref.c

Modified: trunk/varnish-cache/lib/libvcl/vcc_action.c
===================================================================
--- trunk/varnish-cache/lib/libvcl/vcc_action.c	2007-07-09 20:35:20 UTC (rev 1661)
+++ trunk/varnish-cache/lib/libvcl/vcc_action.c	2007-07-10 19:46:16 UTC (rev 1662)
@@ -200,7 +200,11 @@
 	case HASH:
 		ExpectErr(tl, T_INCR);
 		vcc_NextToken(tl);
-		vcc_StringVal(tl);
+		if (!vcc_StringVal(tl)) {
+			ERRCHK(tl);
+			vcc_ExpectedStringval(tl);
+			return;
+		}
 		Fb(tl, 0, ");\n");
 		break;
 	case STRING:
@@ -209,12 +213,22 @@
 			return;
 		}
 		vcc_NextToken(tl);
-		vcc_StringVal(tl);
-		while (tl->t->tok != ';') {
+		if (!vcc_StringVal(tl)) {
+			ERRCHK(tl);
+			vcc_ExpectedStringval(tl);
+			return;
+		}
+		do 
 			Fb(tl, 0, ", ");
-			vcc_StringVal(tl);
+		while (vcc_StringVal(tl));
+		if (tl->t->tok != ';') {
+			ERRCHK(tl);
+			vsb_printf(tl->sb,
+			    "Expected variable, string or semicolon\n");
+			vcc_ErrWhere(tl, tl->t);
+			return;
 		}
-		Fb(tl, 0, ", 0);\n");
+		Fb(tl, 0, "0);\n");
 		break;
 	default:
 		vsb_printf(tl->sb,

Modified: trunk/varnish-cache/lib/libvcl/vcc_compile.h
===================================================================
--- trunk/varnish-cache/lib/libvcl/vcc_compile.h	2007-07-09 20:35:20 UTC (rev 1661)
+++ trunk/varnish-cache/lib/libvcl/vcc_compile.h	2007-07-10 19:46:16 UTC (rev 1662)
@@ -167,6 +167,10 @@
 unsigned vcc_UintVal(struct tokenlist *tl);
 double vcc_DoubleVal(struct tokenlist *tl);
 
+/* vcc_string.c */
+int vcc_StringVal(struct tokenlist *tl);
+void vcc_ExpectedStringval(struct tokenlist *tl);
+
 /* vcc_token.c */
 void vcc_ErrToken(const struct tokenlist *tl, const struct token *t);
 void vcc_ErrWhere(struct tokenlist *tl, const struct token *t);
@@ -180,7 +184,6 @@
 void vcc_FreeToken(struct token *t);
 
 /* vcc_var.c */
-void vcc_StringVal(struct tokenlist *tl);
 struct var *vcc_FindVar(struct tokenlist *tl, const struct token *t, struct var *vl);
 
 /* vcc_xref.c */

Copied: trunk/varnish-cache/lib/libvcl/vcc_string.c (from rev 1658, trunk/varnish-cache/lib/libvcl/vcc_var.c)
===================================================================
--- trunk/varnish-cache/lib/libvcl/vcc_string.c	                        (rev 0)
+++ trunk/varnish-cache/lib/libvcl/vcc_string.c	2007-07-10 19:46:16 UTC (rev 1662)
@@ -0,0 +1,90 @@
+/*-
+ * Copyright (c) 2006 Verdens Gang AS
+ * Copyright (c) 2006-2007 Linpro AS
+ * All rights reserved.
+ *
+ * Author: Poul-Henning Kamp <phk at phk.freebsd.dk>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $Id$
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include "vsb.h"
+
+#include "vcc_priv.h"
+#include "vcc_compile.h"
+#include "libvarnish.h"
+
+/*--------------------------------------------------------------------
+ * Parse a string value and emit something that results in a usable
+ * "const char *".
+ * There are three possible outcomes:
+ *	tl->err != 0 means something bad happened and a message is emitted.
+ *	return (0) means "could not use this token"
+ *	return (1) means "done"
+ */
+
+int
+vcc_StringVal(struct tokenlist *tl) 
+{
+	struct var *vp;
+
+	if (tl->t->tok == CSTR) {
+		EncToken(tl->fb, tl->t);
+		vcc_NextToken(tl);
+		return (1);
+	}
+	if (tl->t->tok == VAR) {
+		vp = vcc_FindVar(tl, tl->t, vcc_vars);
+		if (tl->err)
+			return (0);
+		assert(vp != NULL);
+		switch (vp->fmt) {
+		case STRING:
+			Fb(tl, 0, "%s", vp->rname);
+			break;
+		default:
+			vsb_printf(tl->sb,
+			    "String representation of '%s' not implemented yet.\n",
+				vp->name);
+			vcc_ErrWhere(tl, tl->t);
+			return (0);
+		}
+		vcc_NextToken(tl);
+		return (1);
+	}
+	return (0);
+}
+
+void
+vcc_ExpectedStringval(struct tokenlist *tl)
+{
+
+	if (!tl->err) {
+		vsb_printf(tl->sb, "Expected string variable or constant\n");
+		vcc_ErrWhere(tl, tl->t);
+	}
+}

Modified: trunk/varnish-cache/lib/libvcl/vcc_var.c
===================================================================
--- trunk/varnish-cache/lib/libvcl/vcc_var.c	2007-07-09 20:35:20 UTC (rev 1661)
+++ trunk/varnish-cache/lib/libvcl/vcc_var.c	2007-07-10 19:46:16 UTC (rev 1662)
@@ -40,40 +40,6 @@
 
 /*--------------------------------------------------------------------*/
 
-void
-vcc_StringVal(struct tokenlist *tl) 
-{
-	struct var *vp;
-
-	if (tl->t->tok == CSTR) {
-		EncToken(tl->fb, tl->t);
-		vcc_NextToken(tl);
-		return;
-	} else if (tl->t->tok != VAR) {
-		vsb_printf(tl->sb, "Expected string variable or constant\n");
-		vcc_ErrWhere(tl, tl->t);
-		return;
-	}
-	ERRCHK(tl);
-	vp = vcc_FindVar(tl, tl->t, vcc_vars);
-	ERRCHK(tl);
-	assert(vp != NULL);
-	switch (vp->fmt) {
-	case STRING:
-		Fb(tl, 0, "%s", vp->rname);
-		break;
-	default:
-		vsb_printf(tl->sb,
-		    "String representation of '%s' not implemented yet.\n",
-			vp->name);
-		vcc_ErrWhere(tl, tl->t);
-		return;
-	}
-	vcc_NextToken(tl);
-}
-
-/*--------------------------------------------------------------------*/
-
 static struct var *
 HeaderVar(struct tokenlist *tl, const struct token *t, const struct var *vh)
 {




More information about the varnish-commit mailing list