From ef9b60baed6543c9c864eec4a259f105c3cd271c Mon Sep 17 00:00:00 2001 From: phk Date: Tue, 10 Jul 2007 19:46:16 +0000 Subject: [PATCH] Move string stuff to vcc_string.c, there's going to be a fair bit of it. Give vcc_StringVal() a return value to say if it did anything so we can emit better error messages when confused. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@1662 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/lib/libvcl/Makefile.am | 1 + varnish-cache/lib/libvcl/vcc_action.c | 24 +++++-- varnish-cache/lib/libvcl/vcc_compile.h | 5 +- varnish-cache/lib/libvcl/vcc_string.c | 90 ++++++++++++++++++++++++++ varnish-cache/lib/libvcl/vcc_var.c | 34 ---------- 5 files changed, 114 insertions(+), 40 deletions(-) create mode 100644 varnish-cache/lib/libvcl/vcc_string.c diff --git a/varnish-cache/lib/libvcl/Makefile.am b/varnish-cache/lib/libvcl/Makefile.am index 9a8a4d7a..4c0a4257 100644 --- a/varnish-cache/lib/libvcl/Makefile.am +++ b/varnish-cache/lib/libvcl/Makefile.am @@ -16,6 +16,7 @@ libvcl_la_SOURCES = \ vcc_parse.c \ vcc_fixed_token.c \ vcc_obj.c \ + vcc_string.c \ vcc_token.c \ vcc_var.c \ vcc_xref.c diff --git a/varnish-cache/lib/libvcl/vcc_action.c b/varnish-cache/lib/libvcl/vcc_action.c index 9084b30e..5e5d8e90 100644 --- a/varnish-cache/lib/libvcl/vcc_action.c +++ b/varnish-cache/lib/libvcl/vcc_action.c @@ -200,7 +200,11 @@ parse_set(struct tokenlist *tl) case HASH: ExpectErr(tl, T_INCR); vcc_NextToken(tl); - vcc_StringVal(tl); + if (!vcc_StringVal(tl)) { + ERRCHK(tl); + vcc_ExpectedStringval(tl); + return; + } Fb(tl, 0, ");\n"); break; case STRING: @@ -209,12 +213,22 @@ parse_set(struct tokenlist *tl) return; } vcc_NextToken(tl); - vcc_StringVal(tl); - while (tl->t->tok != ';') { + if (!vcc_StringVal(tl)) { + ERRCHK(tl); + vcc_ExpectedStringval(tl); + return; + } + do Fb(tl, 0, ", "); - vcc_StringVal(tl); + while (vcc_StringVal(tl)); + if (tl->t->tok != ';') { + ERRCHK(tl); + vsb_printf(tl->sb, + "Expected variable, string or semicolon\n"); + vcc_ErrWhere(tl, tl->t); + return; } - Fb(tl, 0, ", 0);\n"); + Fb(tl, 0, "0);\n"); break; default: vsb_printf(tl->sb, diff --git a/varnish-cache/lib/libvcl/vcc_compile.h b/varnish-cache/lib/libvcl/vcc_compile.h index 762dd4b6..bd7d6425 100644 --- a/varnish-cache/lib/libvcl/vcc_compile.h +++ b/varnish-cache/lib/libvcl/vcc_compile.h @@ -167,6 +167,10 @@ void vcc_SizeVal(struct tokenlist *tl); unsigned vcc_UintVal(struct tokenlist *tl); double vcc_DoubleVal(struct tokenlist *tl); +/* vcc_string.c */ +int vcc_StringVal(struct tokenlist *tl); +void vcc_ExpectedStringval(struct tokenlist *tl); + /* vcc_token.c */ void vcc_ErrToken(const struct tokenlist *tl, const struct token *t); void vcc_ErrWhere(struct tokenlist *tl, const struct token *t); @@ -180,7 +184,6 @@ void vcc_AddToken(struct tokenlist *tl, unsigned tok, const char *b, const char 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 */ diff --git a/varnish-cache/lib/libvcl/vcc_string.c b/varnish-cache/lib/libvcl/vcc_string.c new file mode 100644 index 00000000..bc891437 --- /dev/null +++ b/varnish-cache/lib/libvcl/vcc_string.c @@ -0,0 +1,90 @@ +/*- + * 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" + +/*-------------------------------------------------------------------- + * Parse a string value and emit something that results in a usable + * "const char *". + * There are three possible outcomes: + * tl->err != 0 means something bad happened and a message is emitted. + * return (0) means "could not use this token" + * return (1) means "done" + */ + +int +vcc_StringVal(struct tokenlist *tl) +{ + struct var *vp; + + if (tl->t->tok == CSTR) { + EncToken(tl->fb, tl->t); + vcc_NextToken(tl); + return (1); + } + if (tl->t->tok == VAR) { + vp = vcc_FindVar(tl, tl->t, vcc_vars); + if (tl->err) + return (0); + assert(vp != NULL); + 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 (0); + } + vcc_NextToken(tl); + return (1); + } + return (0); +} + +void +vcc_ExpectedStringval(struct tokenlist *tl) +{ + + if (!tl->err) { + vsb_printf(tl->sb, "Expected string variable or constant\n"); + vcc_ErrWhere(tl, tl->t); + } +} diff --git a/varnish-cache/lib/libvcl/vcc_var.c b/varnish-cache/lib/libvcl/vcc_var.c index 53a549a4..33cf04e8 100644 --- a/varnish-cache/lib/libvcl/vcc_var.c +++ b/varnish-cache/lib/libvcl/vcc_var.c @@ -40,40 +40,6 @@ /*--------------------------------------------------------------------*/ -void -vcc_StringVal(struct tokenlist *tl) -{ - struct var *vp; - - if (tl->t->tok == CSTR) { - EncToken(tl->fb, tl->t); - vcc_NextToken(tl); - return; - } else if (tl->t->tok != VAR) { - vsb_printf(tl->sb, "Expected string variable or constant\n"); - vcc_ErrWhere(tl, tl->t); - return; - } - ERRCHK(tl); - vp = vcc_FindVar(tl, tl->t, vcc_vars); - ERRCHK(tl); - assert(vp != NULL); - 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) { -- 2.39.5