Commit 177be95c authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Move variable related stuff to vcc_var.c


git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@1553 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent da861267
......@@ -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
......@@ -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:
......
......@@ -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);
......
......@@ -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.
......
......@@ -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);
......
......@@ -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);
......
/*-
* Copyright (c) 2006 Verdens Gang AS
* Copyright (c) 2006-2007 Linpro AS
* All rights reserved.
*
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
*
* 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 <stdio.h>
#include <string.h>
#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);
}
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