From: phk Date: Mon, 25 Jun 2007 08:57:09 +0000 (+0000) Subject: Move variable related stuff to vcc_var.c X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=176e9c85f738832e3ea2991cf306370000994b03;p=varnish Move variable related stuff to vcc_var.c git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@1553 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- diff --git a/varnish-cache/lib/libvcl/Makefile.am b/varnish-cache/lib/libvcl/Makefile.am index ecf71698..9a8a4d7a 100644 --- a/varnish-cache/lib/libvcl/Makefile.am +++ b/varnish-cache/lib/libvcl/Makefile.am @@ -17,6 +17,7 @@ libvcl_la_SOURCES = \ vcc_fixed_token.c \ vcc_obj.c \ vcc_token.c \ + vcc_var.c \ vcc_xref.c libvcl_la_CFLAGS = -include config.h diff --git a/varnish-cache/lib/libvcl/vcc_action.c b/varnish-cache/lib/libvcl/vcc_action.c index 3ef3f74e..2b5db2cc 100644 --- a/varnish-cache/lib/libvcl/vcc_action.c +++ b/varnish-cache/lib/libvcl/vcc_action.c @@ -39,44 +39,6 @@ /*--------------------------------------------------------------------*/ -static void -StringVal(struct tokenlist *tl) -{ - struct var *vp; - struct token *vt; - - if (tl->t->tok == CSTR) { - EncToken(tl->fb, tl->t); - vcc_NextToken(tl); - return; - } - ExpectErr(tl, VAR); - ERRCHK(tl); - vt = tl->t; - vp = FindVar(tl, tl->t, vcc_vars); - ERRCHK(tl); - if (!vp->has_string) { - vsb_printf(tl->sb, - "No string representation of '%s'\n", vp->name); - vcc_ErrWhere(tl, tl->t); - return; - } - 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); -} - -/*--------------------------------------------------------------------*/ - #define VCL_RET_MAC(l,u,b,i) \ static void \ parse_##l(struct tokenlist *tl) \ @@ -140,7 +102,7 @@ parse_set(struct tokenlist *tl) vcc_NextToken(tl); ExpectErr(tl, VAR); vt = tl->t; - vp = FindVar(tl, tl->t, vcc_vars); + vp = vcc_FindVar(tl, tl->t, vcc_vars); ERRCHK(tl); assert(vp != NULL); Fb(tl, 1, "%s", vp->lname); @@ -222,7 +184,7 @@ parse_set(struct tokenlist *tl) case HASH: ExpectErr(tl, T_INCR); vcc_NextToken(tl); - StringVal(tl); + vcc_StringVal(tl); Fb(tl, 0, ");\n"); return; default: diff --git a/varnish-cache/lib/libvcl/vcc_backend.c b/varnish-cache/lib/libvcl/vcc_backend.c index 6b1bb69f..35f7ff09 100644 --- a/varnish-cache/lib/libvcl/vcc_backend.c +++ b/varnish-cache/lib/libvcl/vcc_backend.c @@ -100,7 +100,7 @@ vcc_ParseBackend(struct tokenlist *tl) } vcc_NextToken(tl); ExpectErr(tl, VAR); - vp = FindVar(tl, tl->t, vcc_be_vars); + vp = vcc_FindVar(tl, tl->t, vcc_be_vars); ERRCHK(tl); assert(vp != NULL); vcc_NextToken(tl); diff --git a/varnish-cache/lib/libvcl/vcc_compile.c b/varnish-cache/lib/libvcl/vcc_compile.c index 59e5f76a..14fe8e81 100644 --- a/varnish-cache/lib/libvcl/vcc_compile.c +++ b/varnish-cache/lib/libvcl/vcc_compile.c @@ -232,64 +232,6 @@ EncToken(struct vsb *sb, const struct token *t) EncString(sb, t->dec, NULL, 0); } -/*--------------------------------------------------------------------*/ - -static struct var * -HeaderVar(struct tokenlist *tl, const struct token *t, const struct var *vh) -{ - char *p; - struct var *v; - int i, w; - - (void)tl; - - v = TlAlloc(tl, sizeof *v); - assert(v != NULL); - i = t->e - t->b; - p = TlAlloc(tl, i + 1); - assert(p != NULL); - memcpy(p, t->b, i); - p[i] = '\0'; - v->name = p; - v->fmt = STRING; - v->has_string = vh->has_string; - if (!memcmp(vh->name, "req.", 4)) - w = 1; - else - w = 2; - asprintf(&p, "VRT_GetHdr(sp, %d, \"\\%03o%s:\")", w, - (unsigned)(strlen(v->name + vh->len) + 1), v->name + vh->len); - assert(p != NULL); - v->rname = p; - return (v); -} - -/*--------------------------------------------------------------------*/ - -struct var * -FindVar(struct tokenlist *tl, const struct token *t, struct var *vl) -{ - struct var *v; - - for (v = vl; 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; - vcc_AddUses(tl, v); - if (v->fmt != HEADER) - return (v); - return (HeaderVar(tl, t, v)); - } - vsb_printf(tl->sb, "Unknown variable "); - vcc_ErrToken(tl, t); - vsb_cat(tl->sb, "\nAt: "); - vcc_ErrWhere(tl, t); - return (NULL); -} - /*-------------------------------------------------------------------- * Output the location/profiling table. For each counted token, we * record source+line+charpos for the first character in the token. diff --git a/varnish-cache/lib/libvcl/vcc_compile.h b/varnish-cache/lib/libvcl/vcc_compile.h index de769605..8f89c31d 100644 --- a/varnish-cache/lib/libvcl/vcc_compile.h +++ b/varnish-cache/lib/libvcl/vcc_compile.h @@ -151,7 +151,6 @@ void Fb(const struct tokenlist *tl, int indent, const char *fmt, ...); void Fi(const struct tokenlist *tl, int indent, const char *fmt, ...); void Ff(const struct tokenlist *tl, int indent, const char *fmt, ...); void EncToken(struct vsb *sb, const struct token *t); -struct var *FindVar(struct tokenlist *tl, const struct token *t, struct var *vl); int IsMethod(const struct token *t); void *TlAlloc(struct tokenlist *tl, unsigned len); @@ -179,6 +178,10 @@ void vcc__ErrInternal(struct tokenlist *tl, const char *func, unsigned line); void vcc_AddToken(struct tokenlist *tl, unsigned tok, const char *b, const char *e); 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 */ void vcc_AddDef(struct tokenlist *tl, struct token *t, enum ref_type type); void vcc_AddRef(struct tokenlist *tl, struct token *t, enum ref_type type); diff --git a/varnish-cache/lib/libvcl/vcc_parse.c b/varnish-cache/lib/libvcl/vcc_parse.c index 91fa793a..a0860d0d 100644 --- a/varnish-cache/lib/libvcl/vcc_parse.c +++ b/varnish-cache/lib/libvcl/vcc_parse.c @@ -348,7 +348,7 @@ Cond_2(struct tokenlist *tl) ExpectErr(tl, ')'); vcc_NextToken(tl); } else if (tl->t->tok == VAR) { - vp = FindVar(tl, tl->t, vcc_vars); + vp = vcc_FindVar(tl, tl->t, vcc_vars); ERRCHK(tl); assert(vp != NULL); vcc_NextToken(tl); diff --git a/varnish-cache/lib/libvcl/vcc_var.c b/varnish-cache/lib/libvcl/vcc_var.c new file mode 100644 index 00000000..9e6749c6 --- /dev/null +++ b/varnish-cache/lib/libvcl/vcc_var.c @@ -0,0 +1,136 @@ +/*- + * Copyright (c) 2006 Verdens Gang AS + * Copyright (c) 2006-2007 Linpro AS + * All rights reserved. + * + * Author: Poul-Henning Kamp + * + * 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 +#include + +#include "vsb.h" + +#include "vcc_priv.h" +#include "vcc_compile.h" +#include "libvarnish.h" + +/*--------------------------------------------------------------------*/ + +void +vcc_StringVal(struct tokenlist *tl) +{ + struct var *vp; + struct token *vt; + + if (tl->t->tok == CSTR) { + EncToken(tl->fb, tl->t); + vcc_NextToken(tl); + return; + } + ExpectErr(tl, VAR); + ERRCHK(tl); + vt = tl->t; + vp = vcc_FindVar(tl, tl->t, vcc_vars); + ERRCHK(tl); + if (!vp->has_string) { + vsb_printf(tl->sb, + "No string representation of '%s'\n", vp->name); + vcc_ErrWhere(tl, tl->t); + return; + } + 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) +{ + char *p; + struct var *v; + int i, w; + + (void)tl; + + v = TlAlloc(tl, sizeof *v); + assert(v != NULL); + i = t->e - t->b; + p = TlAlloc(tl, i + 1); + assert(p != NULL); + memcpy(p, t->b, i); + p[i] = '\0'; + v->name = p; + v->fmt = STRING; + v->has_string = vh->has_string; + if (!memcmp(vh->name, "req.", 4)) + w = 1; + else + w = 2; + asprintf(&p, "VRT_GetHdr(sp, %d, \"\\%03o%s:\")", w, + (unsigned)(strlen(v->name + vh->len) + 1), v->name + vh->len); + assert(p != NULL); + v->rname = p; + return (v); +} + +/*--------------------------------------------------------------------*/ + +struct var * +vcc_FindVar(struct tokenlist *tl, const struct token *t, struct var *vl) +{ + struct var *v; + + for (v = vl; 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; + vcc_AddUses(tl, v); + if (v->fmt != HEADER) + return (v); + return (HeaderVar(tl, t, v)); + } + vsb_printf(tl->sb, "Unknown variable "); + vcc_ErrToken(tl, t); + vsb_cat(tl->sb, "\nAt: "); + vcc_ErrWhere(tl, t); + return (NULL); +} +