r4655 - trunk/varnish-cache/lib/libvcl

phk at varnish-cache.org phk at varnish-cache.org
Tue Apr 13 09:41:09 CEST 2010


Author: phk
Date: 2010-04-13 09:41:08 +0200 (Tue, 13 Apr 2010)
New Revision: 4655

Modified:
   trunk/varnish-cache/lib/libvcl/vcc_action.c
   trunk/varnish-cache/lib/libvcl/vcc_backend.c
   trunk/varnish-cache/lib/libvcl/vcc_compile.h
   trunk/varnish-cache/lib/libvcl/vcc_parse.c
   trunk/varnish-cache/lib/libvcl/vcc_var.c
Log:
Polish the variable/type system a little bit, to make life easier for
Kristian, and to eliminate some code duplication.



Modified: trunk/varnish-cache/lib/libvcl/vcc_action.c
===================================================================
--- trunk/varnish-cache/lib/libvcl/vcc_action.c	2010-04-12 07:38:05 UTC (rev 4654)
+++ trunk/varnish-cache/lib/libvcl/vcc_action.c	2010-04-13 07:41:08 UTC (rev 4655)
@@ -156,23 +156,8 @@
 		case T_INCR:
 		case T_DECR:
 		case '=':
-			if (vp->fmt == TIME)
-				vcc_TimeVal(tl);
-			else if (vp->fmt == RTIME)
-				vcc_RTimeVal(tl);
-			else if (vp->fmt == SIZE)
-				vcc_SizeVal(tl);
-			else if (vp->fmt == FLOAT)
-				Fb(tl, 0, "%g", vcc_DoubleVal(tl));
-			else if (vp->fmt == INT) {
-				Fb(tl, 0, "%u", vcc_UintVal(tl));
-				vcc_NextToken(tl);
-			} else {
-				vsb_printf(tl->sb,
-				    "Cannot assign this variable type.\n");
-				vcc_ErrWhere(tl, vt);
-				return;
-			}
+			vcc_VarVal(tl, vp, vt);
+			ERRCHK(tl);
 			break;
 		default:
 			vsb_printf(tl->sb, "Invalid assignment operator.\n");

Modified: trunk/varnish-cache/lib/libvcl/vcc_backend.c
===================================================================
--- trunk/varnish-cache/lib/libvcl/vcc_backend.c	2010-04-12 07:38:05 UTC (rev 4654)
+++ trunk/varnish-cache/lib/libvcl/vcc_backend.c	2010-04-13 07:41:08 UTC (rev 4655)
@@ -247,6 +247,7 @@
 	struct token *t_did = NULL, *t_window = NULL, *t_threshold = NULL;
 	struct token *t_initial = NULL;
 	unsigned window, threshold, initial, status;
+	double t;
 
 	fs = vcc_FldSpec(tl,
 	    "?url",
@@ -292,14 +293,14 @@
 			Fb(tl, 0, "\t\t\t\"\\r\\n\",\n");
 		} else if (vcc_IdIs(t_field, "timeout")) {
 			Fb(tl, 0, "\t\t.timeout = ");
-			vcc_TimeVal(tl);
+			vcc_TimeVal(tl, &t);
 			ERRCHK(tl);
-			Fb(tl, 0, ",\n");
+			Fb(tl, 0, "%g,\n", t);
 		} else if (vcc_IdIs(t_field, "interval")) {
 			Fb(tl, 0, "\t\t.interval = ");
-			vcc_TimeVal(tl);
+			vcc_TimeVal(tl, &t);
 			ERRCHK(tl);
-			Fb(tl, 0, ",\n");
+			Fb(tl, 0, "%g,\n", t);
 		} else if (vcc_IdIs(t_field, "window")) {
 			t_window = tl->t;
 			window = vcc_UintVal(tl);
@@ -396,6 +397,7 @@
 	struct fld_spec *fs;
 	struct vsb *vsb;
 	unsigned u;
+	double t;
 
 	Fh(tl, 1, "\n#define VGC_backend_%s %d\n", vgcname, tl->ndirector);
 
@@ -462,21 +464,21 @@
 			SkipToken(tl, ';');
 		} else if (vcc_IdIs(t_field, "connect_timeout")) {
 			Fb(tl, 0, "\t.connect_timeout = ");
-			vcc_TimeVal(tl);
+			vcc_TimeVal(tl, &t);
 			ERRCHK(tl);
-			Fb(tl, 0, ",\n");
+			Fb(tl, 0, "%g,\n", t);
 			SkipToken(tl, ';');
 		} else if (vcc_IdIs(t_field, "first_byte_timeout")) {
 			Fb(tl, 0, "\t.first_byte_timeout = ");
-			vcc_TimeVal(tl);
+			vcc_TimeVal(tl, &t);
 			ERRCHK(tl);
-			Fb(tl, 0, ",\n");
+			Fb(tl, 0, "%g,\n", t);
 			SkipToken(tl, ';');
 		} else if (vcc_IdIs(t_field, "between_bytes_timeout")) {
 			Fb(tl, 0, "\t.between_bytes_timeout = ");
-			vcc_TimeVal(tl);
+			vcc_TimeVal(tl, &t);
 			ERRCHK(tl);
-			Fb(tl, 0, ",\n");
+			Fb(tl, 0, "%g,\n", t);
 			SkipToken(tl, ';');
 		} else if (vcc_IdIs(t_field, "max_connections")) {
 			u = vcc_UintVal(tl);

Modified: trunk/varnish-cache/lib/libvcl/vcc_compile.h
===================================================================
--- trunk/varnish-cache/lib/libvcl/vcc_compile.h	2010-04-12 07:38:05 UTC (rev 4654)
+++ trunk/varnish-cache/lib/libvcl/vcc_compile.h	2010-04-13 07:41:08 UTC (rev 4655)
@@ -196,9 +196,9 @@
 
 /* vcc_parse.c */
 void vcc_Parse(struct tokenlist *tl);
-void vcc_RTimeVal(struct tokenlist *tl);
-void vcc_TimeVal(struct tokenlist *tl);
-void vcc_SizeVal(struct tokenlist *tl);
+void vcc_RTimeVal(struct tokenlist *tl, double *);
+void vcc_TimeVal(struct tokenlist *tl, double *);
+void vcc_SizeVal(struct tokenlist *tl, double *);
 unsigned vcc_UintVal(struct tokenlist *tl);
 double vcc_DoubleVal(struct tokenlist *tl);
 
@@ -226,6 +226,8 @@
 /* vcc_var.c */
 struct var *vcc_FindVar(struct tokenlist *tl, const struct token *t,
     struct var *vl);
+void vcc_VarVal(struct tokenlist *tl, const struct var *vp,
+    const struct token *vt);
 
 /* vcc_xref.c */
 void vcc_AddDef(struct tokenlist *tl, struct token *t, enum ref_type type);

Modified: trunk/varnish-cache/lib/libvcl/vcc_parse.c
===================================================================
--- trunk/varnish-cache/lib/libvcl/vcc_parse.c	2010-04-12 07:38:05 UTC (rev 4654)
+++ trunk/varnish-cache/lib/libvcl/vcc_parse.c	2010-04-13 07:41:08 UTC (rev 4655)
@@ -123,6 +123,7 @@
 
 /*--------------------------------------------------------------------
  * Recognize and convert { CNUM } to unsigned value
+ * The tokenizer made sure we only get digits.
  */
 
 unsigned
@@ -141,6 +142,7 @@
 
 /*--------------------------------------------------------------------
  * Recognize and convert { CNUM [ '.' [ CNUM ] ] } to double value
+ * The tokenizer made sure we only get digits and a '.'
  */
 
 double
@@ -171,7 +173,7 @@
 /*--------------------------------------------------------------------*/
 
 void
-vcc_RTimeVal(struct tokenlist *tl)
+vcc_RTimeVal(struct tokenlist *tl, double *d)
 {
 	double v, sc;
 	int sign = 1;
@@ -184,11 +186,13 @@
 	ERRCHK(tl);
 	ExpectErr(tl, ID);
 	sc = TimeUnit(tl);
-	Fb(tl, 0, "(%d * %g * %g)", sign, v, sc);
+	*d = sign * v * sc;
 }
 
+/*--------------------------------------------------------------------*/
+
 void
-vcc_TimeVal(struct tokenlist *tl)
+vcc_TimeVal(struct tokenlist *tl, double *d)
 {
 	double v, sc;
 
@@ -196,11 +200,13 @@
 	ERRCHK(tl);
 	ExpectErr(tl, ID);
 	sc = TimeUnit(tl);
-	Fb(tl, 0, "(%g * %g)", v, sc);
+	*d = v * sc;
 }
 
+/*--------------------------------------------------------------------*/
+
 void
-vcc_SizeVal(struct tokenlist *tl)
+vcc_SizeVal(struct tokenlist *tl, double *d)
 {
 	double v, sc;
 
@@ -208,7 +214,7 @@
 	ERRCHK(tl);
 	ExpectErr(tl, ID);
 	sc = SizeUnit(tl);
-	Fb(tl, 0, "(%g * %g)", v, sc);
+	*d = v * sc;
 }
 
 /*--------------------------------------------------------------------*/
@@ -261,28 +267,8 @@
 	case '<':
 		Fb(tl, 0, "%.*s ", PF(tl->t));
 		vcc_NextToken(tl);
-		switch(vp->fmt) {
-		case TIME:
-			vcc_TimeVal(tl);
-			break;
-		case RTIME:
-			vcc_RTimeVal(tl);
-			break;
-		case INT:
-			ExpectErr(tl, CNUM);
-			Fb(tl, 0, "%.*s ", PF(tl->t));
-			vcc_NextToken(tl);
-			break;
-		case SIZE:
-			vcc_SizeVal(tl);
-			break;
-		default:
-			vsb_printf(tl->sb,
-			    "No conditions available for variable '%s'\n",
-			    vp->name);
-			vcc_ErrWhere(tl, tl->t);
-			return;
-		}
+		vcc_VarVal(tl, vp, NULL);
+		ERRCHK(tl);
 		Fb(tl, 0, "\n");
 		break;
 	default:

Modified: trunk/varnish-cache/lib/libvcl/vcc_var.c
===================================================================
--- trunk/varnish-cache/lib/libvcl/vcc_var.c	2010-04-12 07:38:05 UTC (rev 4654)
+++ trunk/varnish-cache/lib/libvcl/vcc_var.c	2010-04-13 07:41:08 UTC (rev 4655)
@@ -110,3 +110,35 @@
 	return (NULL);
 }
 
+/*--------------------------------------------------------------------*/
+
+void
+vcc_VarVal(struct tokenlist *tl, const struct var *vp, const struct token *vt)
+{
+	double d;
+
+	if (vp->fmt == TIME) {
+		vcc_TimeVal(tl, &d);
+		ERRCHK(tl);
+		Fb(tl, 0, "%g", d);
+	} else if (vp->fmt == RTIME) {
+		vcc_RTimeVal(tl, &d);
+		ERRCHK(tl);
+		Fb(tl, 0, "%g", d);
+	} else if (vp->fmt == SIZE) {
+		vcc_SizeVal(tl, &d);
+		ERRCHK(tl);
+		Fb(tl, 0, "%g", d);
+	} else if (vp->fmt == FLOAT) {
+		Fb(tl, 0, "%g", vcc_DoubleVal(tl));
+	} else if (vp->fmt == INT) { 
+		Fb(tl, 0, "%u", vcc_UintVal(tl));
+		vcc_NextToken(tl);
+	} else {
+		AN(vt);
+		vsb_printf(tl->sb,
+		    "Variable has incompatible type.\n");
+		vcc_ErrWhere(tl, vt);
+		return;
+	}
+}




More information about the varnish-commit mailing list