Commit 02eab5d8 authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Polish the variable/type system a little bit, to make life easier for

Kristian, and to eliminate some code duplication.



git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@4655 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent 693ff6da
......@@ -156,23 +156,8 @@ parse_set(struct tokenlist *tl)
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");
......
......@@ -247,6 +247,7 @@ vcc_ParseProbe(struct tokenlist *tl)
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 @@ vcc_ParseProbe(struct tokenlist *tl)
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 @@ vcc_ParseHostDef(struct tokenlist *tl, int serial, const char *vgcname)
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 @@ vcc_ParseHostDef(struct tokenlist *tl, int serial, const char *vgcname)
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);
......
......@@ -196,9 +196,9 @@ extern struct var vcc_vars[];
/* 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 @@ void vcc_AddToken(struct tokenlist *tl, unsigned tok, const char *b,
/* 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);
......
......@@ -123,6 +123,7 @@ SizeUnit(struct tokenlist *tl)
/*--------------------------------------------------------------------
* Recognize and convert { CNUM } to unsigned value
* The tokenizer made sure we only get digits.
*/
unsigned
......@@ -141,6 +142,7 @@ vcc_UintVal(struct tokenlist *tl)
/*--------------------------------------------------------------------
* Recognize and convert { CNUM [ '.' [ CNUM ] ] } to double value
* The tokenizer made sure we only get digits and a '.'
*/
double
......@@ -171,7 +173,7 @@ vcc_DoubleVal(struct tokenlist *tl)
/*--------------------------------------------------------------------*/
void
vcc_RTimeVal(struct tokenlist *tl)
vcc_RTimeVal(struct tokenlist *tl, double *d)
{
double v, sc;
int sign = 1;
......@@ -184,11 +186,13 @@ vcc_RTimeVal(struct tokenlist *tl)
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 @@ vcc_TimeVal(struct tokenlist *tl)
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 @@ vcc_SizeVal(struct tokenlist *tl)
ERRCHK(tl);
ExpectErr(tl, ID);
sc = SizeUnit(tl);
Fb(tl, 0, "(%g * %g)", v, sc);
*d = v * sc;
}
/*--------------------------------------------------------------------*/
......@@ -261,28 +267,8 @@ Cond_Int(const struct var *vp, struct tokenlist *tl)
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:
......
......@@ -110,3 +110,35 @@ vcc_FindVar(struct tokenlist *tl, const struct token *t, struct var *vl)
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;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment