r5013 - trunk/varnish-cache/lib/libvcl

phk at varnish-cache.org phk at varnish-cache.org
Mon Jul 5 23:01:42 CEST 2010


Author: phk
Date: 2010-07-05 23:01:42 +0200 (Mon, 05 Jul 2010)
New Revision: 5013

Added:
   trunk/varnish-cache/lib/libvcl/vcc_symb.c
Modified:
   trunk/varnish-cache/lib/libvcl/Makefile.am
   trunk/varnish-cache/lib/libvcl/vcc_compile.c
   trunk/varnish-cache/lib/libvcl/vcc_compile.h
   trunk/varnish-cache/lib/libvcl/vcc_var.c
Log:
Add a very rudimentary symbol table, and use it to find our
variables.



Modified: trunk/varnish-cache/lib/libvcl/Makefile.am
===================================================================
--- trunk/varnish-cache/lib/libvcl/Makefile.am	2010-07-05 20:56:30 UTC (rev 5012)
+++ trunk/varnish-cache/lib/libvcl/Makefile.am	2010-07-05 21:01:42 UTC (rev 5013)
@@ -22,6 +22,7 @@
 	vcc_fixed_token.c \
 	vcc_obj.c \
 	vcc_string.c \
+	vcc_symb.c \
 	vcc_token.c \
 	vcc_var.c \
 	vcc_xref.c

Modified: trunk/varnish-cache/lib/libvcl/vcc_compile.c
===================================================================
--- trunk/varnish-cache/lib/libvcl/vcc_compile.c	2010-07-05 20:56:30 UTC (rev 5012)
+++ trunk/varnish-cache/lib/libvcl/vcc_compile.c	2010-07-05 21:01:42 UTC (rev 5013)
@@ -463,6 +463,7 @@
 		REPLACE(tl->vcl_dir, tl0->vcl_dir);
 		tl->vars = tl0->vars;
 	}
+	VTAILQ_INIT(&tl->symbols);
 	VTAILQ_INIT(&tl->hosts);
 	VTAILQ_INIT(&tl->membits);
 	VTAILQ_INIT(&tl->tokens);
@@ -504,6 +505,7 @@
 {
 	struct membit *mb;
 	struct source *sp;
+	struct symbol *sym;
 	int i;
 
 	while (!VTAILQ_EMPTY(&tl->membits)) {
@@ -518,6 +520,12 @@
 		vcc_destroy_source(sp);
 	}
 
+	while (!VTAILQ_EMPTY(&tl->symbols)) {
+		sym = VTAILQ_FIRST(&tl->symbols);
+		VTAILQ_REMOVE(&tl->symbols, sym, list);
+		FREE_OBJ(sym);
+	}
+
 	vsb_delete(tl->fh);
 	vsb_delete(tl->fc);
 	vsb_delete(tl->fi);
@@ -537,12 +545,21 @@
 vcc_CompileSource(const struct vcc *tl0, struct vsb *sb, struct source *sp)
 {
 	struct vcc *tl;
+	struct symbol *sym;
+	const struct var *v;
 	char *of;
 	int i;
 
 	tl = vcc_NewVcc(tl0);
 	tl->sb = sb;
 
+	for (v = tl->vars; v->name != NULL; v++) {
+		sym = VCC_AddSymbol(tl, v->name);
+		sym->var = v;
+		if (v->fmt == HEADER)
+			sym->wildcard = 1;
+	}
+
 	vcl_output_lang_h(tl->fh);
 	Fh(tl, 0, "\n/* ---===### VCC generated below here ###===---*/\n");
 	Fh(tl, 0, "\nextern const struct VCL_conf VCL_conf;\n");
@@ -685,7 +702,9 @@
 	struct vcc *tl;
 
 	tl = vcc_NewVcc(NULL);
+
 	tl->vars = vcc_vars;
+
 	return (tl);
 }
 

Modified: trunk/varnish-cache/lib/libvcl/vcc_compile.h
===================================================================
--- trunk/varnish-cache/lib/libvcl/vcc_compile.h	2010-07-05 20:56:30 UTC (rev 5012)
+++ trunk/varnish-cache/lib/libvcl/vcc_compile.h	2010-07-05 21:01:42 UTC (rev 5013)
@@ -62,6 +62,16 @@
 	char			*dec;
 };
 
+struct symbol {
+	unsigned			magic;
+#define SYMBOL_MAGIC			0x3368c9fb
+	VTAILQ_ENTRY(symbol)		list;
+	char				*name;
+	unsigned			nlen;
+	unsigned			wildcard;
+	const struct var		*var;
+};
+
 VTAILQ_HEAD(tokenhead, token);
 
 struct vcc {
@@ -74,6 +84,7 @@
 	char			*vmod_dir;
 
 	const struct var	*vars;
+	VTAILQ_HEAD(, symbol)	symbols;
 
 	/* Instance section */
 	struct tokenhead	tokens;
@@ -225,6 +236,11 @@
 int vcc_StringVal(struct vcc *tl);
 void vcc_ExpectedStringval(struct vcc *tl);
 
+/* vcc_symbol */
+struct symbol *VCC_AddSymbol(struct vcc *tl, const char *name);
+const struct symbol *VCC_FindSymbol(const struct vcc *tl,
+    const struct token *t);
+
 /* vcc_token.c */
 void vcc_Coord(const struct vcc *tl, struct vsb *vsb,
     const struct token *t);

Copied: trunk/varnish-cache/lib/libvcl/vcc_symb.c (from rev 5011, trunk/varnish-cache/lib/libvcl/vcc_var.c)
===================================================================
--- trunk/varnish-cache/lib/libvcl/vcc_symb.c	                        (rev 0)
+++ trunk/varnish-cache/lib/libvcl/vcc_symb.c	2010-07-05 21:01:42 UTC (rev 5013)
@@ -0,0 +1,83 @@
+/*-
+ * Copyright (c) 2010 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.
+ */
+
+#include "config.h"
+
+#include "svnid.h"
+SVNID("$Id$")
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "vsb.h"
+
+#include "vcc_priv.h"
+#include "vcc_compile.h"
+#include "libvarnish.h"
+
+/*--------------------------------------------------------------------*/
+
+
+struct symbol *
+VCC_AddSymbol(struct vcc *tl, const char *name)
+{
+	struct symbol *sym;
+
+	VTAILQ_FOREACH(sym, &tl->symbols, list) {
+		if (strcmp(name, sym->name))
+			continue;
+		printf("%s <> %s\n", name, sym->name);
+		WRONG("name collision");
+	}
+	ALLOC_OBJ(sym, SYMBOL_MAGIC);
+	REPLACE(sym->name, name);
+	sym->nlen = strlen(name);
+	VTAILQ_INSERT_TAIL(&tl->symbols, sym, list);
+	return (sym);
+}
+
+const struct symbol *
+VCC_FindSymbol(const struct vcc *tl, const struct token *t)
+{
+	struct symbol *sym;
+
+	assert(t->tok == ID);
+	VTAILQ_FOREACH(sym, &tl->symbols, list) {
+		if (!sym->wildcard) {
+			if (vcc_IdIs(t, sym->name))
+				return (sym);
+			continue;
+		}
+		if (t->e - t->b <= sym->nlen)
+			continue;
+		if (!memcmp(sym->name, t->b, sym->nlen))
+			return (sym);
+	}
+	return (NULL);
+}

Modified: trunk/varnish-cache/lib/libvcl/vcc_var.c
===================================================================
--- trunk/varnish-cache/lib/libvcl/vcc_var.c	2010-07-05 20:56:30 UTC (rev 5012)
+++ trunk/varnish-cache/lib/libvcl/vcc_var.c	2010-07-05 21:01:42 UTC (rev 5013)
@@ -91,15 +91,14 @@
     const char *use)
 {
 	const struct var *v;
+	const struct symbol *sym;
 
 	AN(tl->vars);
-	for (v = tl->vars; v->name != NULL; v++) {
-		if (v->fmt == HEADER  && (t->e - t->b) <= v->len)
-			continue;
-		if (v->fmt != HEADER  && t->e - t->b != v->len)
-			continue;
-		if (memcmp(t->b, v->name, v->len))
-			continue;
+	sym = VCC_FindSymbol(tl, t);
+	if (sym != NULL) {
+		v = sym->var;
+		AN(v);
+
 		if (wr_access && v->l_methods == 0) {
 			vsb_printf(tl->sb, "Variable ");
 			vcc_ErrToken(tl, t);




More information about the varnish-commit mailing list