Commit b880df14 authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Add a new type of symbols: Wildcards.

This is a generalization of the trick we have used for things like
req.http.foobar, which can now also be used with other parts of
the namespace.

The plan is to use it for storage.disk1.free and similar.



git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@5650 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent b26dc433
......@@ -38,4 +38,5 @@ VCC_SYMB(ACL, acl, "acl")
VCC_SYMB(SUB, sub, "sub") /* VCL subroutine */
VCC_SYMB(BACKEND, backend, "backend")
VCC_SYMB(PROBE, probe, "probe")
VCC_SYMB(WILDCARD, wildcard, "wildcard")
/*lint -restore */
......@@ -568,13 +568,16 @@ vcc_CompileSource(const struct vcc *tl0, struct vsb *sb, struct source *sp)
vcc_Expr_Init(tl);
for (v = tl->vars; v->name != NULL; v++) {
sym = VCC_AddSymbolStr(tl, v->name, SYM_VAR);
if (v->fmt == HEADER) {
sym = VCC_AddSymbolStr(tl, v->name, SYM_WILDCARD);
sym->wildcard = vcc_Var_Wildcard;
} else {
sym = VCC_AddSymbolStr(tl, v->name, SYM_VAR);
}
sym->var = v;
sym->fmt = v->fmt;
sym->eval = vcc_Eval_Var;
sym->r_methods = v->r_methods;
if (v->fmt == HEADER)
sym->wildcard = 1;
}
vcl_output_lang_h(tl->fh);
......
......@@ -79,6 +79,8 @@ enum symkind {
};
typedef void sym_expr_t(struct vcc *tl, struct expr **, const struct symbol *sym);
typedef struct symbol *sym_wildcard_t(struct vcc *tl, const struct token *t,
const struct symbol *sym);
struct symbol {
unsigned magic;
......@@ -87,7 +89,7 @@ struct symbol {
char *name;
unsigned nlen;
unsigned wildcard;
sym_wildcard_t *wildcard;
enum symkind kind;
const struct token *def_b, *def_e;
......@@ -264,9 +266,10 @@ void vcc_ExpectedStringval(struct vcc *tl);
/* vcc_symb.c */
struct symbol *VCC_AddSymbolStr(struct vcc *tl, const char *name, enum symkind);
struct symbol *VCC_AddSymbolTok(struct vcc *tl, const struct token *t, enum symkind kind);
struct symbol *VCC_GetSymbolTok(struct vcc *tl, const struct token *tok,
enum symkind);
struct symbol *VCC_FindSymbol(const struct vcc *tl,
struct symbol *VCC_FindSymbol(struct vcc *tl,
const struct token *t, enum symkind kind);
const char * VCC_SymKind(struct vcc *tl, const struct symbol *s);
typedef void symwalk_f(struct vcc *tl, const struct symbol *s);
......@@ -291,6 +294,7 @@ void vcc_AddToken(struct vcc *tl, unsigned tok, const char *b,
const char *e);
/* vcc_var.c */
sym_wildcard_t vcc_Var_Wildcard;
const struct var *vcc_FindVar(struct vcc *tl, const struct token *t,
int wr_access, const char *use);
void vcc_VarVal(struct vcc *tl, const struct var *vp,
......
......@@ -82,7 +82,7 @@ vcc_AddSymbol(struct vcc *tl, const char *nb, int l, enum symkind kind)
memcpy(sym->name, nb, l);
sym->name[l] = '\0';
sym->nlen = l;
VTAILQ_INSERT_TAIL(&tl->symbols, sym, list);
VTAILQ_INSERT_HEAD(&tl->symbols, sym, list);
sym->kind = kind;
return (sym);
}
......@@ -94,6 +94,13 @@ VCC_AddSymbolStr(struct vcc *tl, const char *name, enum symkind kind)
return (vcc_AddSymbol(tl, name, strlen(name), kind));
}
struct symbol *
VCC_AddSymbolTok(struct vcc *tl, const struct token *t, enum symkind kind)
{
return (vcc_AddSymbol(tl, t->b, t->e - t->b, kind));
}
struct symbol *
VCC_GetSymbolTok(struct vcc *tl, const struct token *tok, enum symkind kind)
{
......@@ -109,22 +116,21 @@ VCC_GetSymbolTok(struct vcc *tl, const struct token *tok, enum symkind kind)
}
struct symbol *
VCC_FindSymbol(const struct vcc *tl, const struct token *t, enum symkind kind)
VCC_FindSymbol(struct vcc *tl, const struct token *t, enum symkind kind)
{
struct symbol *sym;
assert(t->tok == ID);
VTAILQ_FOREACH(sym, &tl->symbols, list) {
if (kind != SYM_NONE && kind != sym->kind)
continue;
if (!sym->wildcard) {
if (vcc_IdIs(t, sym->name))
return (sym);
continue;
if (sym->kind == SYM_WILDCARD &&
(t->e - t->b > sym->nlen) &&
!memcmp(sym->name, t->b, sym->nlen)) {
AN (sym->wildcard);
return (sym->wildcard(tl, t, sym));
}
if (t->e - t->b <= sym->nlen)
if (kind != SYM_NONE && kind != sym->kind)
continue;
if (!memcmp(sym->name, t->b, sym->nlen))
if (vcc_IdIs(t, sym->name))
return (sym);
}
return (NULL);
......
......@@ -43,18 +43,21 @@ SVNID("$Id$")
/*--------------------------------------------------------------------*/
static struct var *
HeaderVar(struct vcc *tl, const struct token *t, const struct var *vh)
struct symbol *
vcc_Var_Wildcard(struct vcc *tl, const struct token *t, const struct symbol *wc)
{
char *p;
struct symbol *sym;
struct var *v;
const struct var *vh;
int i, l;
char buf[258];
(void)tl;
vh = wc->var;
v = TlAlloc(tl, sizeof *v);
assert(v != NULL);
i = t->e - t->b;
p = TlAlloc(tl, i + 1);
assert(p != NULL);
......@@ -81,7 +84,13 @@ HeaderVar(struct vcc *tl, const struct token *t, const struct var *vh)
memcpy(p, buf, i + 1);
v->lname = p;
return (v);
sym = VCC_AddSymbolTok(tl, t, SYM_VAR);
AN(sym);
sym->var = v;
sym->fmt = v->fmt;
sym->eval = vcc_Eval_Var;
sym->r_methods = v->r_methods;
return (sym);
}
/*--------------------------------------------------------------------*/
......@@ -118,9 +127,8 @@ vcc_FindVar(struct vcc *tl, const struct token *t, int wr_access,
} else {
vcc_AddUses(tl, t, v->r_methods, use);
}
if (v->fmt != HEADER)
return (v);
return (HeaderVar(tl, t, v));
assert(v->fmt != HEADER);
return (v);
}
vsb_printf(tl->sb, "Unknown variable ");
vcc_ErrToken(tl, t);
......
......@@ -29,7 +29,7 @@
#include "config.h"
#include "svnid.h"
SVNID("$Id$");
SVNID("$Id$")
#include <stdio.h>
#include <dlfcn.h>
......
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