r75 - trunk/varnish-cache/lib/libvcl

phk at projects.linpro.no phk at projects.linpro.no
Mon Mar 27 13:21:59 CEST 2006


Author: phk
Date: 2006-03-27 13:21:58 +0200 (Mon, 27 Mar 2006)
New Revision: 75

Removed:
   trunk/varnish-cache/lib/libvcl/vcl_lang.h
Modified:
   trunk/varnish-cache/lib/libvcl/vcl_compile.c
   trunk/varnish-cache/lib/libvcl/vcl_fixed_token.c
   trunk/varnish-cache/lib/libvcl/vcl_gen_fixed_token.tcl
   trunk/varnish-cache/lib/libvcl/vcl_priv.h
Log:
Actually generate a shared object


Modified: trunk/varnish-cache/lib/libvcl/vcl_compile.c
===================================================================
--- trunk/varnish-cache/lib/libvcl/vcl_compile.c	2006-03-27 11:21:18 UTC (rev 74)
+++ trunk/varnish-cache/lib/libvcl/vcl_compile.c	2006-03-27 11:21:58 UTC (rev 75)
@@ -42,6 +42,8 @@
 #include <sys/queue.h>
 #include "vcl_priv.h"
 
+#include "libvcl.h"
+
 #define ERRCHK(tl)	do { if ((tl)->err) return; } while (0)
 
 #define INDENT		2
@@ -61,7 +63,7 @@
 	struct token		*t;
 	int			indent;
 	unsigned		cnt;
-	FILE			*fc, *fh;
+	struct sbuf		*fc, *fh;
 	TAILQ_HEAD(, ref)	refs;
 	struct sbuf		*sb;
 	int			err;
@@ -226,7 +228,7 @@
 {
 	if (tl->t->tok == tok)
 		return;
-	sbuf_printf(tl->sb, "Expected %s got ", tnames[tok]);
+	sbuf_printf(tl->sb, "Expected %s got ", vcl_tnames[tok]);
 	ErrToken(tl, tl->t);
 	sbuf_printf(tl->sb, "\n(program line %u), at\n", line);
 	ErrWhere(tl, tl->t);
@@ -236,8 +238,8 @@
 #define ExpectErr(a, b) do { _Expect(a, b, __LINE__); ERRCHK(a);} while (0)
 
 #define I(tl)	do { 		\
-	fprintf(tl->fc, "/* %-11s */ ", __func__); \
-	fprintf(tl->fc, "%*.*s", tl->indent, tl->indent, ""); \
+	sbuf_printf(tl->fc, "/* %-11s */ ", __func__); \
+	sbuf_printf(tl->fc, "%*.*s", tl->indent, tl->indent, ""); \
 } while (0)
 
 #define L(tl, foo)	do {	\
@@ -248,7 +250,7 @@
 
 #define C(tl, sep)	do {				\
 	I(tl);						\
-	fprintf(tl->fc, "VCL_count(%u)%s\n", ++tl->cnt, sep);	\
+	sbuf_printf(tl->fc, "VCL_count(%u)%s\n", ++tl->cnt, sep);	\
 	tl->t->cnt = tl->cnt; 				\
 } while (0)
 	
@@ -506,7 +508,7 @@
 	v = DoubleVal(tl);
 	ExpectErr(tl, ID);
 	sc = TimeUnit(tl);
-	fprintf(tl->fc, "(%g * %g)", v, sc);
+	sbuf_printf(tl->fc, "(%g * %g)", v, sc);
 }
 
 static void
@@ -517,7 +519,7 @@
 	v = DoubleVal(tl);
 	ExpectErr(tl, ID);
 	sc = SizeUnit(tl);
-	fprintf(tl->fc, "(%g * %g)", v, sc);
+	sbuf_printf(tl->fc, "(%g * %g)", v, sc);
 }
 
 static void
@@ -528,7 +530,7 @@
 	v = DoubleVal(tl);
 	ExpectErr(tl, ID);
 	sc = RateUnit(tl);
-	fprintf(tl->fc, "(%g * %g)", v, sc);
+	sbuf_printf(tl->fc, "(%g * %g)", v, sc);
 }
 
 /*--------------------------------------------------------------------*/
@@ -544,7 +546,7 @@
 		ExpectErr(tl, ID);
 		I(tl);
 		AddRef(tl, tl->t, R_ACL);
-		fprintf(tl->fc, "ip_match(%s, acl_%*.*s)\n",
+		sbuf_printf(tl->fc, "ip_match(%s, acl_%*.*s)\n",
 		    vp->cname,
 		    tl->t->e - tl->t->b,
 		    tl->t->e - tl->t->b, tl->t->b);
@@ -553,13 +555,13 @@
 	case T_EQ:
 	case T_NEQ:
 		I(tl);
-		fprintf(tl->fc, "%s %*.*s ",
+		sbuf_printf(tl->fc, "%s %*.*s ",
 		    vp->cname,
 		    tl->t->e - tl->t->b,
 		    tl->t->e - tl->t->b, tl->t->b);
 		NextToken(tl);
 		u = IpVal(tl);
-		fprintf(tl->fc, "%uU /* %u.%u.%u.%u */\n", u,
+		sbuf_printf(tl->fc, "%uU /* %u.%u.%u.%u */\n", u,
 		    (u >> 24) & 0xff, (u >> 16) & 0xff,
 		    (u >> 8) & 0xff, (u) & 0xff);
 		break;
@@ -579,10 +581,10 @@
 
 	switch (tl->t->tok) {
 	case '~':
-		I(tl); fprintf(tl->fc, "string_match(%s, ", vp->cname);
+		I(tl); sbuf_printf(tl->fc, "string_match(%s, ", vp->cname);
 		NextToken(tl);
 		ExpectErr(tl, CSTR);
-		fprintf(tl->fc, "%*.*s)\n",
+		sbuf_printf(tl->fc, "%*.*s)\n",
 			tl->t->e - tl->t->b,
 			tl->t->e - tl->t->b, tl->t->b);
 		NextToken(tl);
@@ -602,7 +604,7 @@
 {
 
 	I(tl);
-	fprintf(tl->fc, "%s ", vp->cname);
+	sbuf_printf(tl->fc, "%s ", vp->cname);
 	switch (tl->t->tok) {
 	case T_EQ:
 	case T_NEQ:
@@ -610,7 +612,7 @@
 	case T_GEQ:
 	case '>':
 	case '<':
-		fprintf(tl->fc, "%*.*s ", 
+		sbuf_printf(tl->fc, "%*.*s ", 
 			tl->t->e - tl->t->b,
 			tl->t->e - tl->t->b, tl->t->b);
 		NextToken(tl);
@@ -620,7 +622,7 @@
 			break;
 		case INT:
 			ExpectErr(tl, CNUM);
-			fprintf(tl->fc, "%*.*s ", 
+			sbuf_printf(tl->fc, "%*.*s ", 
 				tl->t->e - tl->t->b,
 				tl->t->e - tl->t->b, tl->t->b);
 			NextToken(tl);
@@ -635,7 +637,7 @@
 			ErrWhere(tl, tl->t);
 			return;
 		}
-		fprintf(tl->fc, "\n");
+		sbuf_printf(tl->fc, "\n");
 		break;
 	default:
 		sbuf_printf(tl->sb, "Illegal condition ");
@@ -653,7 +655,7 @@
 {
 
 	I(tl);
-	fprintf(tl->fc, "%s\n", vp->cname);
+	sbuf_printf(tl->fc, "%s\n", vp->cname);
 }
 
 static void
@@ -664,10 +666,10 @@
 	C(tl, ",");
 	I(tl);
 	if (tl->t->tok == '!') {
-		fprintf(tl->fc, "!");
+		sbuf_printf(tl->fc, "!");
 		NextToken(tl);
 	}
-	fprintf(tl->fc, "(\n");
+	sbuf_printf(tl->fc, "(\n");
 	if (tl->t->tok == '(') {
 		NextToken(tl);
 		Cond_0(tl);
@@ -704,35 +706,35 @@
 		return;
 	}
 	I(tl);
-	fprintf(tl->fc, ")\n");
+	sbuf_printf(tl->fc, ")\n");
 }
 
 static void
 Cond_1(struct tokenlist *tl)
 {
 
-	I(tl); fprintf(tl->fc, "(\n");
+	I(tl); sbuf_printf(tl->fc, "(\n");
 	L(tl, Cond_2(tl));
 	while (tl->t->tok == T_CAND) {
 		NextToken(tl);
-		I(tl); fprintf(tl->fc, ") && (\n");
+		I(tl); sbuf_printf(tl->fc, ") && (\n");
 		L(tl, Cond_2(tl));
 	}
-	I(tl); fprintf(tl->fc, ")\n");
+	I(tl); sbuf_printf(tl->fc, ")\n");
 }
 
 static void
 Cond_0(struct tokenlist *tl)
 {
 
-	I(tl); fprintf(tl->fc, "(\n");
+	I(tl); sbuf_printf(tl->fc, "(\n");
 	L(tl, Cond_1(tl));
 	while (tl->t->tok == T_COR) {
 		NextToken(tl);
-		I(tl); fprintf(tl->fc, ") || (\n");
+		I(tl); sbuf_printf(tl->fc, ") || (\n");
 		L(tl, Cond_1(tl));
 	}
-	I(tl); fprintf(tl->fc, ")\n");
+	I(tl); sbuf_printf(tl->fc, ")\n");
 }
 
 static void
@@ -741,10 +743,10 @@
 
 	ExpectErr(tl, '(');
 	NextToken(tl);
-	I(tl); fprintf(tl->fc, "(\n");
+	I(tl); sbuf_printf(tl->fc, "(\n");
 	L(tl, Cond_0(tl));
 	ERRCHK(tl);
-	I(tl); fprintf(tl->fc, ")\n");
+	I(tl); sbuf_printf(tl->fc, ")\n");
 	ExpectErr(tl, ')');
 	NextToken(tl);
 }
@@ -756,7 +758,7 @@
 {
 
 	ExpectErr(tl, T_IF);
-	I(tl); fprintf(tl->fc, "if \n");
+	I(tl); sbuf_printf(tl->fc, "if \n");
 	NextToken(tl);
 	L(tl, Conditional(tl));
 	ERRCHK(tl);
@@ -767,7 +769,7 @@
 		case T_ELSE:
 			NextToken(tl);
 			if (tl->t->tok != T_IF) {
-				I(tl); fprintf(tl->fc, "else \n");
+				I(tl); sbuf_printf(tl->fc, "else \n");
 				L(tl, Compound(tl));
 				ERRCHK(tl);
 				return;
@@ -775,7 +777,7 @@
 			/* FALLTHROUGH */
 		case T_ELSEIF:
 		case T_ELSIF:
-			I(tl); fprintf(tl->fc, "else if \n");
+			I(tl); sbuf_printf(tl->fc, "else if \n");
 			NextToken(tl);
 			L(tl, Conditional(tl));
 			ERRCHK(tl);
@@ -802,36 +804,36 @@
 	switch (at->tok) {
 	case T_NO_NEW_CACHE:
 		I(tl);
-		fprintf(tl->fc, "VCL_no_new_cache();\n");
+		sbuf_printf(tl->fc, "VCL_no_new_cache();\n");
 		return;
 	case T_NO_CACHE:
 		I(tl);
-		fprintf(tl->fc, "VCL_no_cache();\n");
+		sbuf_printf(tl->fc, "VCL_no_cache();\n");
 		return;
 	case T_FINISH:
 		I(tl);
-		fprintf(tl->fc, "return;\n");
+		sbuf_printf(tl->fc, "return;\n");
 		return;
 	case T_FETCH:
 		I(tl);
-		fprintf(tl->fc, "VCL_fetch();\n");
+		sbuf_printf(tl->fc, "VCL_fetch();\n");
 		return;
 	case T_ERROR:
 		a = UintVal(tl);
 		I(tl);
-		fprintf(tl->fc, "VCL_error(%u, ", a);
+		sbuf_printf(tl->fc, "VCL_error(%u, ", a);
 		if (tl->t->tok == CSTR) {
-			fprintf(tl->fc, "%*.*s);\n",
+			sbuf_printf(tl->fc, "%*.*s);\n",
 			    tl->t->e - tl->t->b,
 			    tl->t->e - tl->t->b, tl->t->b);
 			NextToken(tl);
 		} else
-			fprintf(tl->fc, "(const char *)0);\n");
+			sbuf_printf(tl->fc, "(const char *)0);\n");
 		return;
 	case T_SWITCH_CONFIG:
 		ExpectErr(tl, ID);
 		I(tl);
-		fprintf(tl->fc, "VCL_switch_config(\"%*.*s\");\n",
+		sbuf_printf(tl->fc, "VCL_switch_config(\"%*.*s\");\n",
 		    tl->t->e - tl->t->b,
 		    tl->t->e - tl->t->b, tl->t->b);
 		NextToken(tl);
@@ -840,7 +842,7 @@
 		ExpectErr(tl, ID);
 		AddRef(tl, tl->t, R_FUNC);
 		I(tl);
-		fprintf(tl->fc, "VCL_function_%*.*s(VCL_PASS_ARGS);\n",
+		sbuf_printf(tl->fc, "VCL_function_%*.*s(VCL_PASS_ARGS);\n",
 		    tl->t->e - tl->t->b,
 		    tl->t->e - tl->t->b, tl->t->b);
 		/* XXX: check if function finished request */
@@ -849,12 +851,12 @@
 	case T_REWRITE:
 		ExpectErr(tl, CSTR);
 		I(tl);
-		fprintf(tl->fc, "VCL_rewrite(%*.*s",
+		sbuf_printf(tl->fc, "VCL_rewrite(%*.*s",
 		    tl->t->e - tl->t->b,
 		    tl->t->e - tl->t->b, tl->t->b);
 		NextToken(tl);
 		ExpectErr(tl, CSTR);
-		fprintf(tl->fc, ", %*.*s);\n",
+		sbuf_printf(tl->fc, ", %*.*s);\n",
 		    tl->t->e - tl->t->b,
 		    tl->t->e - tl->t->b, tl->t->b);
 		NextToken(tl);
@@ -865,7 +867,7 @@
 		ERRCHK(tl);
 		assert(vp != NULL);
 		I(tl);
-		fprintf(tl->fc, "%s ", vp->cname);
+		sbuf_printf(tl->fc, "%s ", vp->cname);
 		NextToken(tl);
 		switch (vp->fmt) {
 		case INT:
@@ -873,13 +875,13 @@
 		case RATE:
 		case TIME:
 		case FLOAT:
-			fprintf(tl->fc, "%*.*s ",
+			sbuf_printf(tl->fc, "%*.*s ",
 			    tl->t->e - tl->t->b,
 			    tl->t->e - tl->t->b, tl->t->b);
 			a = tl->t->tok;
 			NextToken(tl);
 			if (a == T_MUL || a == T_DIV)
-				fprintf(tl->fc, "%g", DoubleVal(tl));
+				sbuf_printf(tl->fc, "%g", DoubleVal(tl));
 			else if (vp->fmt == TIME)
 				TimeVal(tl);
 			else if (vp->fmt == SIZE)
@@ -887,14 +889,14 @@
 			else if (vp->fmt == RATE)
 				RateVal(tl);
 			else 
-				fprintf(tl->fc, "%g", DoubleVal(tl));
-			fprintf(tl->fc, ";\n");
+				sbuf_printf(tl->fc, "%g", DoubleVal(tl));
+			sbuf_printf(tl->fc, ";\n");
 			break;
 		case IP:
 			if (tl->t->tok == '=') {
 				NextToken(tl);
 				u = IpVal(tl);
-				fprintf(tl->fc, "= %uU; /* %u.%u.%u.%u */\n",
+				sbuf_printf(tl->fc, "= %uU; /* %u.%u.%u.%u */\n",
 				    u,
 				    (u >> 24) & 0xff,
 				    (u >> 16) & 0xff,
@@ -911,7 +913,7 @@
 		case BACKEND:
 			if (tl->t->tok == '=') {
 				NextToken(tl);
-				fprintf(tl->fc, "= &VCL_backend_%*.*s;\n",
+				sbuf_printf(tl->fc, "= &VCL_backend_%*.*s;\n",
 				    tl->t->e - tl->t->b,
 				    tl->t->e - tl->t->b, tl->t->b);
 				NextToken(tl);
@@ -948,11 +950,11 @@
 
 	ExpectErr(tl, ID);
 	AddDef(tl, tl->t, R_ACL);
-	fprintf(tl->fh, "static struct vcl_acl acl_%*.*s[];\n",
+	sbuf_printf(tl->fh, "static struct vcl_acl acl_%*.*s[];\n",
 	    tl->t->e - tl->t->b,
 	    tl->t->e - tl->t->b, tl->t->b);
 	I(tl);
-	fprintf(tl->fc, "static struct vcl_acl acl_%*.*s[] = {\n",
+	sbuf_printf(tl->fc, "static struct vcl_acl acl_%*.*s[] = {\n",
 	    tl->t->e - tl->t->b,
 	    tl->t->e - tl->t->b, tl->t->b);
 	NextToken(tl);
@@ -973,19 +975,19 @@
 		ExpectErr(tl, ';');
 		NextToken(tl);
 		I(tl);
-		fprintf(tl->fc, "{ %11uU, %3uU }, /* %u.%u.%u.%u/%u */\n",
+		sbuf_printf(tl->fc, "{ %11uU, %3uU }, /* %u.%u.%u.%u/%u */\n",
 		    u, m,
 		    (u >> 24) & 0xff, (u >> 16) & 0xff,
 		    (u >> 8) & 0xff, (u) & 0xff, m);
 	}
 	ExpectErr(tl, '}');
 	I(tl);
-	fprintf(tl->fc, "{ %11uU, %3uU }\n", 0, 0);
+	sbuf_printf(tl->fc, "{ %11uU, %3uU }\n", 0, 0);
 
 	tl->indent -= INDENT;
 
 	I(tl);
-	fprintf(tl->fc, "};\n\n");
+	sbuf_printf(tl->fc, "};\n\n");
 	NextToken(tl);
 }
 
@@ -996,7 +998,7 @@
 {
 
 	ExpectErr(tl, '{');
-	I(tl); fprintf(tl->fc, "{\n");
+	I(tl); sbuf_printf(tl->fc, "{\n");
 	tl->indent += INDENT;
 	C(tl, ";");
 	NextToken(tl);
@@ -1012,7 +1014,7 @@
 		case '}':
 			NextToken(tl);
 			tl->indent -= INDENT;
-			I(tl); fprintf(tl->fc, "}\n");
+			I(tl); sbuf_printf(tl->fc, "}\n");
 			return;
 		case EOI:
 			sbuf_printf(tl->sb,
@@ -1039,20 +1041,21 @@
 	ExpectErr(tl, ID);
 	AddDef(tl, tl->t, R_BACKEND);
 	I(tl);
-	fprintf(tl->fh, "static struct backend VCL_backend_%*.*s;\n",
+	sbuf_printf(tl->fh, "static struct backend VCL_backend_%*.*s;\n",
 	    tl->t->e - tl->t->b,
 	    tl->t->e - tl->t->b, tl->t->b);
-	fprintf(tl->fc, "static struct backend VCL_backend_%*.*s;\n",
+	sbuf_printf(tl->fc, "static struct backend VCL_backend_%*.*s;\n",
 	    tl->t->e - tl->t->b,
 	    tl->t->e - tl->t->b, tl->t->b);
-	fprintf(tl->fc, "static void\n");
+	sbuf_printf(tl->fc, "static void\n");
 	I(tl);
-	fprintf(tl->fc, "VCL_init_backend_%*.*s (struct backend *backend)\n",
+	sbuf_printf(tl->fc,
+	    "VCL_init_backend_%*.*s (struct backend *backend)\n",
 	    tl->t->e - tl->t->b,
 	    tl->t->e - tl->t->b, tl->t->b);
 	NextToken(tl);
 	L(tl, Compound(tl));
-	fprintf(tl->fc, "\n");
+	sbuf_printf(tl->fc, "\n");
 }
 
 /*--------------------------------------------------------------------*/
@@ -1064,18 +1067,18 @@
 	NextToken(tl);
 	ExpectErr(tl, ID);
 	AddDef(tl, tl->t, R_FUNC);
-	fprintf(tl->fh, "static void VCL_function_%*.*s (VCL_FARGS);\n",
+	sbuf_printf(tl->fh, "static void VCL_function_%*.*s (VCL_FARGS);\n",
 	    tl->t->e - tl->t->b,
 	    tl->t->e - tl->t->b, tl->t->b);
 	I(tl);
-	fprintf(tl->fc, "static void\n");
+	sbuf_printf(tl->fc, "static void\n");
 	I(tl);
-	fprintf(tl->fc, "VCL_function_%*.*s (VCL_FARGS)\n",
+	sbuf_printf(tl->fc, "VCL_function_%*.*s (VCL_FARGS)\n",
 	    tl->t->e - tl->t->b,
 	    tl->t->e - tl->t->b, tl->t->b);
 	NextToken(tl);
 	L(tl, Compound(tl));
-	fprintf(tl->fc, "\n");
+	sbuf_printf(tl->fc, "\n");
 }
 
 /*--------------------------------------------------------------------
@@ -1130,6 +1133,8 @@
 	t->e = e;
 	TAILQ_INSERT_TAIL(&tl->tokens, t, list);
 	tl->t = t;
+	if (0)
+		fprintf(stderr, "+ %s\n", vcl_tnames[tok]);
 }
 
 /*--------------------------------------------------------------------
@@ -1170,7 +1175,7 @@
 		}
 
 		/* Match for the fixed tokens (see token.tcl) */
-		u = fixed_token(p, &q);
+		u = vcl_fixed_token(p, &q);
 		if (u != 0) {
 			AddToken(tl, u, p, q);
 			p = q;
@@ -1278,8 +1283,12 @@
 	unsigned lin, pos;
 	const char *p;
 	
-	fprintf(tl->fh, "static struct vcl_ref VCL_ref[%u];\n", tl->cnt + 1);
-	fprintf(tl->fc, "static struct vcl_ref VCL_ref[%u] = {\n", tl->cnt + 1);
+	sbuf_printf(tl->fh,
+	    "#define VCL_NREFS %u\n", tl->cnt + 1);
+	sbuf_printf(tl->fh,
+	    "static struct vcl_ref VCL_ref[VCL_NREFS];\n");
+	sbuf_printf(tl->fc,
+	    "static struct vcl_ref VCL_ref[VCL_NREFS] = {\n");
 	lin = 1;
 	pos = 0;
 	p = tl->b;
@@ -1297,51 +1306,137 @@
 				pos++;
 		
 		}
-		fprintf(tl->fc,
+		sbuf_printf(tl->fc,
 		    "%*.*s[%3u] = { %4u, %3u, 0, \"%*.*s\" },\n",
 		    INDENT, INDENT, "",
 		    t->cnt, lin, pos + 1,
 		    t->e - t->b,
 		    t->e - t->b, t->b);
 	}
-	fprintf(tl->fc, "};\n");
+	sbuf_printf(tl->fc, "};\n");
 }
 
 
 /*--------------------------------------------------------------------*/
 
 static void
-Compile(struct sbuf *sb, const char *b, const char *e)
+EmitInitFunc(struct tokenlist *tl)
 {
+	struct ref *r;
+
+	sbuf_printf(tl->fc,
+	    "\nstatic void\n"
+	    "VCL_Init(void)\n"
+	    "{\n\n");
+	
+	TAILQ_FOREACH(r, &tl->refs, list) {
+		switch(r->type) {
+		case R_FUNC:
+			break;
+		case R_ACL:
+			break;
+		case R_BACKEND:
+			sbuf_printf(tl->fc,
+			    "\tVCL_init_backend_%*.*s(&VCL_backend_%*.*s);\n",
+			    r->name->e - r->name->b,
+			    r->name->e - r->name->b, r->name->b,
+			    r->name->e - r->name->b,
+			    r->name->e - r->name->b, r->name->b);
+			break;
+		}
+	}
+	sbuf_printf(tl->fc, "}\n");
+}
+
+/*--------------------------------------------------------------------*/
+
+static void
+EmitStruct(struct tokenlist *tl)
+{
+
+	sbuf_printf(tl->fc, "\nstruct VCL_conf VCL_conf = {\n");
+	sbuf_printf(tl->fc,
+	    "\t.magic = VCL_CONF_MAGIC,\n");
+	sbuf_printf(tl->fc,
+	    "\t.init_func = VCL_Init,\n");
+	sbuf_printf(tl->fc,
+	    "\t.default_backend = &VCL_backend_default,\n");
+	sbuf_printf(tl->fc,
+	    "\t.ref = VCL_ref,\n");
+	sbuf_printf(tl->fc,
+	    "\t.nref = VCL_NREFS,\n");
+	sbuf_printf(tl->fc, "};\n");
+}
+
+/*--------------------------------------------------------------------*/
+
+void
+VCL_Compile(struct sbuf *sb, const char *b, const char *e)
+{
 	struct tokenlist	tokens;
+	FILE *fo;
 
 	memset(&tokens, 0, sizeof tokens);
 	TAILQ_INIT(&tokens.tokens);
 	TAILQ_INIT(&tokens.refs);
 	tokens.sb = sb;
 
-	tokens.fc = fopen("_.c", "w");
+	tokens.fc = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
 	assert(tokens.fc != NULL);
 
-	tokens.fh = fopen("_.h", "w");
+	tokens.fh = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
 	assert(tokens.fh != NULL);
 
-	fprintf(tokens.fc, "#include \"vcl_lang.h\"\n");
-	fprintf(tokens.fc, "#include \"_.h\"\n");
 	tokens.b = b;
+	if (e == NULL)
+		e = strchr(b, '\0');
+	assert(e != NULL);
 	tokens.e = e;
 	Lexer(&tokens, b, e);
 	ERRCHK(&tokens);
 	tokens.t = TAILQ_FIRST(&tokens.tokens);
 	Parse(&tokens);
 	ERRCHK(&tokens);
+if (0)
 	CheckRefs(&tokens);
 	ERRCHK(&tokens);
 	LocTable(&tokens);
+
+	EmitInitFunc(&tokens);
+
+	EmitStruct(&tokens);
+
+	fo = popen(
+	    "tee /tmp/_.c |"
+	    "cc -fpic -shared -Wl,-x -o /tmp/_.so.1 -x c - ", "w");
+
+	vcl_output_lang_h(fo);
+
+	fprintf(fo, "/* FH */\n");
+	sbuf_finish(tokens.fh);
+	fputs(sbuf_data(tokens.fh), fo);
+
+	fprintf(fo, "/* FC */\n");
+	sbuf_finish(tokens.fc);
+	fputs(sbuf_data(tokens.fc), fo);
+	pclose(fo);
 }
 
 /*--------------------------------------------------------------------*/
 
+void
+VCL_InitCompile(void)
+{
+	struct var *v;
+
+	vcl_init_tnames();
+	for (v = vars; v->name != NULL; v++)
+		v->len = strlen(v->name);
+}
+
+#if 0
+/*--------------------------------------------------------------------*/
+
 #include <err.h>
 
 #define MYSPACE	(128 * 1024)
@@ -1381,3 +1476,4 @@
 		printf("<%s>\n", sbuf_data(sb));
 	return (0);
 }
+#endif

Modified: trunk/varnish-cache/lib/libvcl/vcl_fixed_token.c
===================================================================
--- trunk/varnish-cache/lib/libvcl/vcl_fixed_token.c	2006-03-27 11:21:18 UTC (rev 74)
+++ trunk/varnish-cache/lib/libvcl/vcl_fixed_token.c	2006-03-27 11:21:58 UTC (rev 75)
@@ -3,10 +3,12 @@
  * instead, edit the Tcl script vcl_gen_fixed_token.tcl and run it by hand
  */
 
+#include <stdio.h>
+#include <ctype.h>
 #include "vcl_priv.h"
 
 unsigned
-fixed_token(const char *p, const char **q)
+vcl_fixed_token(const char *p, const char **q)
 {
 
 	switch (p[0]) {
@@ -305,65 +307,144 @@
 	}
 }
 
-const char *tnames[256];
+const char *vcl_tnames[256];
 
 void
-init_tnames(void)
+vcl_init_tnames(void)
 {
-	tnames['!'] = "'!'";
-	tnames['%'] = "'%'";
-	tnames['&'] = "'&'";
-	tnames['('] = "'('";
-	tnames[')'] = "')'";
-	tnames['*'] = "'*'";
-	tnames['+'] = "'+'";
-	tnames[','] = "','";
-	tnames['-'] = "'-'";
-	tnames['.'] = "'.'";
-	tnames['/'] = "'/'";
-	tnames['<'] = "'<'";
-	tnames['='] = "'='";
-	tnames['>'] = "'>'";
-	tnames['{'] = "'{'";
-	tnames['}'] = "'}'";
-	tnames['|'] = "'|'";
-	tnames['~'] = "'~'";
-	tnames[';'] = "';'";
-	tnames[CNUM] = "CNUM";
-	tnames[CSTR] = "CSTR";
-	tnames[EOI] = "EOI";
-	tnames[ID] = "ID";
-	tnames[T_ACL] = "acl";
-	tnames[T_BACKEND] = "backend";
-	tnames[T_CALL] = "call";
-	tnames[T_CAND] = "&&";
-	tnames[T_COR] = "||";
-	tnames[T_DEC] = "--";
-	tnames[T_DECR] = "/=";
-	tnames[T_DIV] = "/=";
-	tnames[T_ELSE] = "else";
-	tnames[T_ELSEIF] = "elseif";
-	tnames[T_ELSIF] = "elsif";
-	tnames[T_EQ] = "==";
-	tnames[T_ERROR] = "error";
-	tnames[T_FETCH] = "fetch";
-	tnames[T_FINISH] = "finish";
-	tnames[T_FUNC] = "func";
-	tnames[T_GEQ] = ">=";
-	tnames[T_IF] = "if";
-	tnames[T_INC] = "++";
-	tnames[T_INCR] = "+=";
-	tnames[T_LEQ] = "<=";
-	tnames[T_MUL] = "*=";
-	tnames[T_NEQ] = "!=";
-	tnames[T_NO_CACHE] = "no_cache";
-	tnames[T_NO_NEW_CACHE] = "no_new_cache";
-	tnames[T_PROC] = "proc";
-	tnames[T_REWRITE] = "rewrite";
-	tnames[T_SET] = "set";
-	tnames[T_SHL] = "<<";
-	tnames[T_SHR] = ">>";
-	tnames[T_SUB] = "sub";
-	tnames[T_SWITCH_CONFIG] = "switch_config";
-	tnames[VAR] = "VAR";
+	vcl_tnames['!'] = "'!'";
+	vcl_tnames['%'] = "'%'";
+	vcl_tnames['&'] = "'&'";
+	vcl_tnames['('] = "'('";
+	vcl_tnames[')'] = "')'";
+	vcl_tnames['*'] = "'*'";
+	vcl_tnames['+'] = "'+'";
+	vcl_tnames[','] = "','";
+	vcl_tnames['-'] = "'-'";
+	vcl_tnames['.'] = "'.'";
+	vcl_tnames['/'] = "'/'";
+	vcl_tnames['<'] = "'<'";
+	vcl_tnames['='] = "'='";
+	vcl_tnames['>'] = "'>'";
+	vcl_tnames['{'] = "'{'";
+	vcl_tnames['}'] = "'}'";
+	vcl_tnames['|'] = "'|'";
+	vcl_tnames['~'] = "'~'";
+	vcl_tnames[';'] = "';'";
+	vcl_tnames[CNUM] = "CNUM";
+	vcl_tnames[CSTR] = "CSTR";
+	vcl_tnames[EOI] = "EOI";
+	vcl_tnames[ID] = "ID";
+	vcl_tnames[T_ACL] = "acl";
+	vcl_tnames[T_BACKEND] = "backend";
+	vcl_tnames[T_CALL] = "call";
+	vcl_tnames[T_CAND] = "&&";
+	vcl_tnames[T_COR] = "||";
+	vcl_tnames[T_DEC] = "--";
+	vcl_tnames[T_DECR] = "/=";
+	vcl_tnames[T_DIV] = "/=";
+	vcl_tnames[T_ELSE] = "else";
+	vcl_tnames[T_ELSEIF] = "elseif";
+	vcl_tnames[T_ELSIF] = "elsif";
+	vcl_tnames[T_EQ] = "==";
+	vcl_tnames[T_ERROR] = "error";
+	vcl_tnames[T_FETCH] = "fetch";
+	vcl_tnames[T_FINISH] = "finish";
+	vcl_tnames[T_FUNC] = "func";
+	vcl_tnames[T_GEQ] = ">=";
+	vcl_tnames[T_IF] = "if";
+	vcl_tnames[T_INC] = "++";
+	vcl_tnames[T_INCR] = "+=";
+	vcl_tnames[T_LEQ] = "<=";
+	vcl_tnames[T_MUL] = "*=";
+	vcl_tnames[T_NEQ] = "!=";
+	vcl_tnames[T_NO_CACHE] = "no_cache";
+	vcl_tnames[T_NO_NEW_CACHE] = "no_new_cache";
+	vcl_tnames[T_PROC] = "proc";
+	vcl_tnames[T_REWRITE] = "rewrite";
+	vcl_tnames[T_SET] = "set";
+	vcl_tnames[T_SHL] = "<<";
+	vcl_tnames[T_SHR] = ">>";
+	vcl_tnames[T_SUB] = "sub";
+	vcl_tnames[T_SWITCH_CONFIG] = "switch_config";
+	vcl_tnames[VAR] = "VAR";
 }
+
+void
+vcl_output_lang_h(FILE *f)
+{
+	fputs("/*\n", f);
+	fputs(" * Stuff necessary to compile a VCL programs C code\n", f);
+	fputs(" *\n", f);
+	fputs(" * XXX: When this file is changed, lib/libvcl/vcl_gen_fixed_token.tcl\n", f);
+	fputs(" * XXX: *MUST* be rerun.\n", f);
+	fputs(" */\n", f);
+	fputs("\n", f);
+	fputs("\n", f);
+	fputs("struct vcl_ref {\n", f);
+	fputs("	unsigned	line;\n", f);
+	fputs("	unsigned	pos;\n", f);
+	fputs("	unsigned	count;\n", f);
+	fputs("	const char	*token;\n", f);
+	fputs("};\n", f);
+	fputs("\n", f);
+	fputs("struct vcl_acl {\n", f);
+	fputs("	unsigned	ip;\n", f);
+	fputs("	unsigned	mask;\n", f);
+	fputs("};\n", f);
+	fputs("\n", f);
+	fputs("struct client {\n", f);
+	fputs("	unsigned	ip;\n", f);
+	fputs("};\n", f);
+	fputs("\n", f);
+	fputs("struct req {\n", f);
+	fputs("	char		*req;\n", f);
+	fputs("	char		*useragent;\n", f);
+	fputs("	struct {\n", f);
+	fputs("		char		*path;\n", f);
+	fputs("		char		*host;\n", f);
+	fputs("	}		url;\n", f);
+	fputs("	double		ttlfactor;\n", f);
+	fputs("	struct backend	*backend;\n", f);
+	fputs("};\n", f);
+	fputs("\n", f);
+	fputs("struct backend {\n", f);
+	fputs("	unsigned	ip;\n", f);
+	fputs("	double		responsetime;\n", f);
+	fputs("	double		timeout;\n", f);
+	fputs("	double		bandwidth;\n", f);
+	fputs("	int		down;\n", f);
+	fputs("};\n", f);
+	fputs("\n", f);
+	fputs("struct obj {\n", f);
+	fputs("	int		exists;\n", f);
+	fputs("	double		ttl;\n", f);
+	fputs("	unsigned	result;\n", f);
+	fputs("	unsigned	size;\n", f);
+	fputs("	unsigned	usage;\n", f);
+	fputs("};\n", f);
+	fputs("\n", f);
+	fputs("#define VCL_FARGS	struct client *client, struct obj *obj, struct req *req, struct backend *backend\n", f);
+	fputs("#define VCL_PASS_ARGS	client, obj, req, backend\n", f);
+	fputs("\n", f);
+	fputs("void VCL_count(unsigned);\n", f);
+	fputs("void VCL_no_cache();\n", f);
+	fputs("void VCL_no_new_cache();\n", f);
+	fputs("int ip_match(unsigned, struct vcl_acl *);\n", f);
+	fputs("int string_match(const char *, const char *);\n", f);
+	fputs("int VCL_rewrite(const char *, const char *);\n", f);
+	fputs("int VCL_error(unsigned, const char *);\n", f);
+	fputs("int VCL_fetch(void);\n", f);
+	fputs("int VCL_switch_config(const char *);\n", f);
+	fputs("\n", f);
+	fputs("typedef void vcl_init_f(void);\n", f);
+	fputs("\n", f);
+	fputs("struct VCL_conf {\n", f);
+	fputs("	unsigned	magic;\n", f);
+	fputs("#define VCL_CONF_MAGIC	0x7406c509	/* from /dev/random */\n", f);
+	fputs("	vcl_init_f	*init_func;\n", f);
+	fputs("	struct backend	*default_backend;\n", f);
+	fputs("	struct vcl_ref	*ref;\n", f);
+	fputs("	unsigned	nref;\n", f);
+	fputs("};\n", f);
+}

Modified: trunk/varnish-cache/lib/libvcl/vcl_gen_fixed_token.tcl
===================================================================
--- trunk/varnish-cache/lib/libvcl/vcl_gen_fixed_token.tcl	2006-03-27 11:21:18 UTC (rev 74)
+++ trunk/varnish-cache/lib/libvcl/vcl_gen_fixed_token.tcl	2006-03-27 11:21:58 UTC (rev 75)
@@ -59,6 +59,8 @@
  */
 }
 
+puts $fo "#include <stdio.h>"
+puts $fo "#include <ctype.h>"
 puts $fo "#include \"vcl_priv.h\""
 
 set tn 128
@@ -103,7 +105,7 @@
 
 puts $fo {
 unsigned
-fixed_token(const char *p, const char **q)}
+vcl_fixed_token(const char *p, const char **q)}
 puts $fo "{"
 puts $fo ""
 puts $fo "	switch (p\[0\]) {"
@@ -158,17 +160,29 @@
 puts $fo "}"
 
 puts $fo ""
-puts $fo "const char *tnames\[256\];\n"
+puts $fo "const char *vcl_tnames\[256\];\n"
 puts $fo "void"
-puts $fo "init_tnames(void)"
+puts $fo "vcl_init_tnames(void)"
 puts $fo "{"
 foreach i $token2 {
-	puts $fo "\ttnames\[[lindex $i 0]\] = \"[lindex $i 0]\";"
+	puts $fo "\tvcl_tnames\[[lindex $i 0]\] = \"[lindex $i 0]\";"
 }
 foreach i $tokens {
-	puts $fo "\ttnames\[[lindex $i 0]\] = \"[lindex $i 1]\";"
+	puts $fo "\tvcl_tnames\[[lindex $i 0]\] = \"[lindex $i 1]\";"
 }
 puts $fo "}"
 
+set fi [open "../../include/vcl_lang.h"]
+
+puts $fo ""
+puts $fo "void"
+puts $fo "vcl_output_lang_h(FILE *f)"
+puts $fo "{"
+while {[gets $fi a] >= 0} {
+	puts $fo "\tfputs(\"$a\\n\", f);"
+}
+puts $fo "}"
+close $fi
+
 close $foh
 close $fo

Deleted: trunk/varnish-cache/lib/libvcl/vcl_lang.h
===================================================================
--- trunk/varnish-cache/lib/libvcl/vcl_lang.h	2006-03-27 11:21:18 UTC (rev 74)
+++ trunk/varnish-cache/lib/libvcl/vcl_lang.h	2006-03-27 11:21:58 UTC (rev 75)
@@ -1,62 +0,0 @@
-/*
- * Stuff necessary to compile a VCL programs C code
- */
-
-
-struct vcl_ref {
-	unsigned	line;
-	unsigned	pos;
-	unsigned	count;
-	const char	*token;
-};
-
-struct vcl_acl {
-	unsigned	ip;
-	unsigned	mask;
-};
-
-struct client {
-	unsigned	ip;
-};
-
-struct req {
-	char		*req;
-	char		*useragent;
-	struct {
-		char		*path;
-		char		*host;
-	}		url;
-	double		ttlfactor;
-	struct backend	*backend;
-};
-
-struct backend {
-	unsigned	ip;
-	double		responsetime;
-	double		timeout;
-	double		bandwidth;
-	int		down;
-};
-
-struct obj {
-	int		exists;
-	double		ttl;
-	unsigned	result;
-	unsigned	size;
-	unsigned	usage;
-};
-
-#define VCL_FARGS	struct client *client, struct obj *obj, struct req *req, struct backend *backend
-#define VCL_PASS_ARGS	client, obj, req, backend
-
-void VCL_count(unsigned);
-void VCL_no_cache();
-void VCL_no_new_cache();
-int ip_match(unsigned, struct vcl_acl *);
-int string_match(const char *, const char *);
-int VCL_rewrite(const char *, const char *);
-int VCL_error(unsigned, const char *);
-int VCL_fetch(void);
-int VCL_switch_config(const char *);
-
-

Modified: trunk/varnish-cache/lib/libvcl/vcl_priv.h
===================================================================
--- trunk/varnish-cache/lib/libvcl/vcl_priv.h	2006-03-27 11:21:18 UTC (rev 74)
+++ trunk/varnish-cache/lib/libvcl/vcl_priv.h	2006-03-27 11:21:58 UTC (rev 75)
@@ -3,10 +3,11 @@
  */
 
 #include "vcl_token_defs.h"
-#include <ctype.h>
 
 #define isident1(c) (isalpha(c))
 #define isident(c) (isalpha(c) || isdigit(c) || (c) == '_')
 #define isvar(c) (isident(c) || (c) == '.')
-unsigned fixed_token(const char *p, const char **q);
-extern const char *tnames[256];
+unsigned vcl_fixed_token(const char *p, const char **q);
+extern const char *vcl_tnames[256];
+void vcl_init_tnames(void);
+void vcl_output_lang_h(FILE *f);




More information about the varnish-commit mailing list