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

Split the parameters for the VCC compiler into a separate structure

for more clarity, before we start hacking VCC further.
parent 17190673
...@@ -64,7 +64,7 @@ unsigned mgt_vcc_err_unref; ...@@ -64,7 +64,7 @@ unsigned mgt_vcc_err_unref;
unsigned mgt_vcc_allow_inline_c; unsigned mgt_vcc_allow_inline_c;
unsigned mgt_vcc_unsafe_path; unsigned mgt_vcc_unsafe_path;
static struct vcc *vcc; static struct vcp *vcp;
#define VGC_SRC "vgc.c" #define VGC_SRC "vgc.c"
#define VGC_LIB "vgc.so" #define VGC_LIB "vgc.so"
...@@ -94,12 +94,12 @@ run_vcc(void *priv) ...@@ -94,12 +94,12 @@ run_vcc(void *priv)
sb = VSB_new_auto(); sb = VSB_new_auto();
XXXAN(sb); XXXAN(sb);
VCC_VCL_dir(vcc, mgt_vcl_dir); VCP_VCL_dir(vcp, mgt_vcl_dir);
VCC_VMOD_dir(vcc, mgt_vmod_dir); VCP_VMOD_dir(vcp, mgt_vmod_dir);
VCC_Err_Unref(vcc, mgt_vcc_err_unref); VCP_Err_Unref(vcp, mgt_vcc_err_unref);
VCC_Allow_InlineC(vcc, mgt_vcc_allow_inline_c); VCP_Allow_InlineC(vcp, mgt_vcc_allow_inline_c);
VCC_Unsafe_Path(vcc, mgt_vcc_unsafe_path); VCP_Unsafe_Path(vcp, mgt_vcc_unsafe_path);
csrc = VCC_Compile(vcc, sb, vp->src); csrc = VCC_Compile(vcp, sb, vp->src);
AZ(VSB_finish(sb)); AZ(VSB_finish(sb));
if (VSB_len(sb)) if (VSB_len(sb))
printf("%s", VSB_data(sb)); printf("%s", VSB_data(sb));
...@@ -318,7 +318,7 @@ void ...@@ -318,7 +318,7 @@ void
mgt_vcc_init(void) mgt_vcc_init(void)
{ {
vcc = VCC_New(); vcp = VCP_New();
AN(vcc); AN(vcp);
VCC_Builtin_VCL(vcc, builtin_vcl); VCP_Builtin_VCL(vcp, builtin_vcl);
} }
...@@ -30,12 +30,12 @@ ...@@ -30,12 +30,12 @@
struct vcc; struct vcc;
struct vcc *VCC_New(void); struct vcp *VCP_New(void);
void VCC_Builtin_VCL(struct vcc *, const char *str); void VCP_Builtin_VCL(struct vcp *, const char *str);
void VCC_VCL_dir(struct vcc *, const char *str); void VCP_VCL_dir(struct vcp *, const char *str);
void VCC_VMOD_dir(struct vcc *, const char *str); void VCP_VMOD_dir(struct vcp *, const char *str);
void VCC_Err_Unref(struct vcc *tl, unsigned u); void VCP_Err_Unref(struct vcp *tl, unsigned u);
void VCC_Allow_InlineC(struct vcc *tl, unsigned u); void VCP_Allow_InlineC(struct vcp *tl, unsigned u);
void VCC_Unsafe_Path(struct vcc *tl, unsigned u); void VCP_Unsafe_Path(struct vcp *tl, unsigned u);
char *VCC_Compile(const struct vcc *, struct vsb *sb, const char *b); char *VCC_Compile(const struct vcp *, struct vsb *sb, const char *b);
...@@ -364,7 +364,7 @@ vcc_acl_emit(struct vcc *tl, const char *acln, int anon) ...@@ -364,7 +364,7 @@ vcc_acl_emit(struct vcc *tl, const char *acln, int anon)
Fh(tl, 0, "\t\tVRT_acl_log(ctx, \"NO_FAM %s\");\n", acln); Fh(tl, 0, "\t\tVRT_acl_log(ctx, \"NO_FAM %s\");\n", acln);
Fh(tl, 0, "\t\treturn(0);\n"); Fh(tl, 0, "\t\treturn(0);\n");
Fh(tl, 0, "\t}\n\n"); Fh(tl, 0, "\t}\n\n");
if (!tl->err_unref && !anon ) { if (!tl->param->err_unref && !anon ) {
ifp = New_IniFin(tl); ifp = New_IniFin(tl);
VSB_printf(ifp->ini, VSB_printf(ifp->ini,
"\tif (0) match_acl_named_%s(0, 0);\n", acln); "\tif (0) match_acl_named_%s(0, 0);\n", acln);
......
...@@ -450,16 +450,16 @@ vcc_destroy_source(struct source *sp) ...@@ -450,16 +450,16 @@ vcc_destroy_source(struct source *sp)
/*--------------------------------------------------------------------*/ /*--------------------------------------------------------------------*/
static struct source * static struct source *
vcc_file_source(const struct vcc *tl, struct vsb *sb, const char *fn) vcc_file_source(const struct vcp * const vcp, struct vsb *sb, const char *fn)
{ {
char *f; char *f;
struct source *sp; struct source *sp;
if (!tl->unsafe_path && strchr(fn, '/') != NULL) { if (!vcp->unsafe_path && strchr(fn, '/') != NULL) {
VSB_printf(sb, "Include path is unsafe '%s'\n", fn); VSB_printf(sb, "Include path is unsafe '%s'\n", fn);
return (NULL); return (NULL);
} }
f = VFIL_readfile(tl->vcl_dir, fn, NULL); f = VFIL_readfile(vcp->vcl_dir, fn, NULL);
if (f == NULL) { if (f == NULL) {
VSB_printf(sb, "Cannot read file '%s': %s\n", VSB_printf(sb, "Cannot read file '%s': %s\n",
fn, strerror(errno)); fn, strerror(errno));
...@@ -500,7 +500,7 @@ vcc_resolve_includes(struct vcc *tl) ...@@ -500,7 +500,7 @@ vcc_resolve_includes(struct vcc *tl)
return; return;
} }
sp = vcc_file_source(tl, tl->sb, t1->dec); sp = vcc_file_source(tl->param, tl->sb, t1->dec);
if (sp == NULL) { if (sp == NULL) {
vcc_ErrWhere(tl, t1); vcc_ErrWhere(tl, t1);
return; return;
...@@ -522,24 +522,16 @@ vcc_resolve_includes(struct vcc *tl) ...@@ -522,24 +522,16 @@ vcc_resolve_includes(struct vcc *tl)
/*--------------------------------------------------------------------*/ /*--------------------------------------------------------------------*/
static struct vcc * static struct vcc *
vcc_NewVcc(const struct vcc *tl0) vcc_NewVcc(const struct vcp *vcp)
{ {
struct vcc *tl; struct vcc *tl;
int i; int i;
CHECK_OBJ_NOTNULL(vcp, VCP_MAGIC);
ALLOC_OBJ(tl, VCC_MAGIC); ALLOC_OBJ(tl, VCC_MAGIC);
AN(tl); AN(tl);
if (tl0 != NULL) { tl->param = vcp;
REPLACE(tl->builtin_vcl, tl0->builtin_vcl); tl->vars = vcc_vars;
REPLACE(tl->vcl_dir, tl0->vcl_dir);
REPLACE(tl->vmod_dir, tl0->vmod_dir);
tl->vars = tl0->vars;
tl->err_unref = tl0->err_unref;
tl->allow_inline_c = tl0->allow_inline_c;
tl->unsafe_path = tl0->unsafe_path;
} else {
tl->err_unref = 1;
}
VTAILQ_INIT(&tl->symbols); VTAILQ_INIT(&tl->symbols);
VTAILQ_INIT(&tl->inifin); VTAILQ_INIT(&tl->inifin);
VTAILQ_INIT(&tl->membits); VTAILQ_INIT(&tl->membits);
...@@ -606,7 +598,8 @@ vcc_DestroyTokenList(struct vcc *tl, char *ret) ...@@ -606,7 +598,8 @@ vcc_DestroyTokenList(struct vcc *tl, char *ret)
*/ */
static char * static char *
vcc_CompileSource(const struct vcc *tl0, struct vsb *sb, struct source *sp) vcc_CompileSource(const struct vcp * const vcp, struct vsb *sb,
struct source *sp)
{ {
struct vcc *tl; struct vcc *tl;
struct symbol *sym; struct symbol *sym;
...@@ -616,7 +609,7 @@ vcc_CompileSource(const struct vcc *tl0, struct vsb *sb, struct source *sp) ...@@ -616,7 +609,7 @@ vcc_CompileSource(const struct vcc *tl0, struct vsb *sb, struct source *sp)
char *of; char *of;
int i; int i;
tl = vcc_NewVcc(tl0); tl = vcc_NewVcc(vcp);
tl->sb = sb; tl->sb = sb;
vcc_Expr_Init(tl); vcc_Expr_Init(tl);
...@@ -649,7 +642,7 @@ vcc_CompileSource(const struct vcc *tl0, struct vsb *sb, struct source *sp) ...@@ -649,7 +642,7 @@ vcc_CompileSource(const struct vcc *tl0, struct vsb *sb, struct source *sp)
return (vcc_DestroyTokenList(tl, NULL)); return (vcc_DestroyTokenList(tl, NULL));
/* Register and lex the builtin VCL */ /* Register and lex the builtin VCL */
sp = vcc_new_source(tl->builtin_vcl, NULL, "Builtin"); sp = vcc_new_source(tl->param->builtin_vcl, NULL, "Builtin");
assert(sp != NULL); assert(sp != NULL);
VTAILQ_INSERT_TAIL(&tl->sources, sp, list); VTAILQ_INSERT_TAIL(&tl->sources, sp, list);
sp->idx = tl->nsources++; sp->idx = tl->nsources++;
...@@ -759,7 +752,7 @@ vcc_CompileSource(const struct vcc *tl0, struct vsb *sb, struct source *sp) ...@@ -759,7 +752,7 @@ vcc_CompileSource(const struct vcc *tl0, struct vsb *sb, struct source *sp)
*/ */
char * char *
VCC_Compile(const struct vcc *tl, struct vsb *sb, const char *b) VCC_Compile(const struct vcp *vcp, struct vsb *sb, const char *b)
{ {
struct source *sp; struct source *sp;
char *r; char *r;
...@@ -767,7 +760,7 @@ VCC_Compile(const struct vcc *tl, struct vsb *sb, const char *b) ...@@ -767,7 +760,7 @@ VCC_Compile(const struct vcc *tl, struct vsb *sb, const char *b)
sp = vcc_new_source(b, NULL, "input"); sp = vcc_new_source(b, NULL, "input");
if (sp == NULL) if (sp == NULL)
return (NULL); return (NULL);
r = vcc_CompileSource(tl, sb, sp); r = vcc_CompileSource(vcp, sb, sp);
return (r); return (r);
} }
...@@ -775,16 +768,15 @@ VCC_Compile(const struct vcc *tl, struct vsb *sb, const char *b) ...@@ -775,16 +768,15 @@ VCC_Compile(const struct vcc *tl, struct vsb *sb, const char *b)
* Allocate a compiler instance * Allocate a compiler instance
*/ */
struct vcc * struct vcp *
VCC_New(void) VCP_New(void)
{ {
struct vcc *tl; struct vcp *vcp;
tl = vcc_NewVcc(NULL); ALLOC_OBJ(vcp, VCP_MAGIC);
AN(vcp);
tl->vars = vcc_vars; return (vcp);
return (tl);
} }
/*-------------------------------------------------------------------- /*--------------------------------------------------------------------
...@@ -792,11 +784,11 @@ VCC_New(void) ...@@ -792,11 +784,11 @@ VCC_New(void)
*/ */
void void
VCC_Builtin_VCL(struct vcc *tl, const char *str) VCP_Builtin_VCL(struct vcp *vcp, const char *str)
{ {
CHECK_OBJ_NOTNULL(tl, VCC_MAGIC); CHECK_OBJ_NOTNULL(vcp, VCP_MAGIC);
REPLACE(tl->builtin_vcl, str); REPLACE(vcp->builtin_vcl, str);
} }
/*-------------------------------------------------------------------- /*--------------------------------------------------------------------
...@@ -804,11 +796,11 @@ VCC_Builtin_VCL(struct vcc *tl, const char *str) ...@@ -804,11 +796,11 @@ VCC_Builtin_VCL(struct vcc *tl, const char *str)
*/ */
void void
VCC_VCL_dir(struct vcc *tl, const char *str) VCP_VCL_dir(struct vcp *vcp, const char *str)
{ {
CHECK_OBJ_NOTNULL(tl, VCC_MAGIC); CHECK_OBJ_NOTNULL(vcp, VCP_MAGIC);
REPLACE(tl->vcl_dir, str); REPLACE(vcp->vcl_dir, str);
} }
/*-------------------------------------------------------------------- /*--------------------------------------------------------------------
...@@ -816,11 +808,11 @@ VCC_VCL_dir(struct vcc *tl, const char *str) ...@@ -816,11 +808,11 @@ VCC_VCL_dir(struct vcc *tl, const char *str)
*/ */
void void
VCC_VMOD_dir(struct vcc *tl, const char *str) VCP_VMOD_dir(struct vcp *vcp, const char *str)
{ {
CHECK_OBJ_NOTNULL(tl, VCC_MAGIC); CHECK_OBJ_NOTNULL(vcp, VCP_MAGIC);
REPLACE(tl->vmod_dir, str); REPLACE(vcp->vmod_dir, str);
} }
/*-------------------------------------------------------------------- /*--------------------------------------------------------------------
...@@ -828,25 +820,25 @@ VCC_VMOD_dir(struct vcc *tl, const char *str) ...@@ -828,25 +820,25 @@ VCC_VMOD_dir(struct vcc *tl, const char *str)
*/ */
void void
VCC_Err_Unref(struct vcc *tl, unsigned u) VCP_Err_Unref(struct vcp *vcp, unsigned u)
{ {
CHECK_OBJ_NOTNULL(tl, VCC_MAGIC); CHECK_OBJ_NOTNULL(vcp, VCP_MAGIC);
tl->err_unref = u; vcp->err_unref = u;
} }
void void
VCC_Allow_InlineC(struct vcc *tl, unsigned u) VCP_Allow_InlineC(struct vcp *vcp, unsigned u)
{ {
CHECK_OBJ_NOTNULL(tl, VCC_MAGIC); CHECK_OBJ_NOTNULL(vcp, VCP_MAGIC);
tl->allow_inline_c = u; vcp->allow_inline_c = u;
} }
void void
VCC_Unsafe_Path(struct vcc *tl, unsigned u) VCP_Unsafe_Path(struct vcp *vcp, unsigned u)
{ {
CHECK_OBJ_NOTNULL(tl, VCC_MAGIC); CHECK_OBJ_NOTNULL(vcp, VCP_MAGIC);
tl->unsafe_path = u; vcp->unsafe_path = u;
} }
...@@ -155,14 +155,24 @@ struct inifin { ...@@ -155,14 +155,24 @@ struct inifin {
VTAILQ_HEAD(inifinhead, inifin); VTAILQ_HEAD(inifinhead, inifin);
struct vcc { struct vcp {
unsigned magic; unsigned magic;
#define VCC_MAGIC 0x24ad719d #define VCP_MAGIC 0xd90acfbc
/* Parameter/Template section */
char *builtin_vcl; char *builtin_vcl;
char *vcl_dir; char *vcl_dir;
char *vmod_dir; char *vmod_dir;
unsigned err_unref;
unsigned allow_inline_c;
unsigned unsafe_path;
};
struct vcc {
unsigned magic;
#define VCC_MAGIC 0x24ad719d
/* Parameter/Template section */
const struct vcp *param;
const struct var *vars; const struct var *vars;
VTAILQ_HEAD(, symbol) symbols; VTAILQ_HEAD(, symbol) symbols;
...@@ -202,9 +212,6 @@ struct vcc { ...@@ -202,9 +212,6 @@ struct vcc {
unsigned unique; unsigned unique;
unsigned err_unref;
unsigned allow_inline_c;
unsigned unsafe_path;
}; };
struct var { struct var {
......
...@@ -160,7 +160,7 @@ vcc_Compound(struct vcc *tl) ...@@ -160,7 +160,7 @@ vcc_Compound(struct vcc *tl)
Fb(tl, 1, "}\n"); Fb(tl, 1, "}\n");
return; return;
case CSRC: case CSRC:
if (tl->allow_inline_c) { if (tl->param->allow_inline_c) {
Fb(tl, 1, "%.*s\n", Fb(tl, 1, "%.*s\n",
(int) (tl->t->e - (tl->t->b + 2)), (int) (tl->t->e - (tl->t->b + 2)),
tl->t->b + 1); tl->t->b + 1);
...@@ -357,7 +357,7 @@ vcc_Parse(struct vcc *tl) ...@@ -357,7 +357,7 @@ vcc_Parse(struct vcc *tl)
ERRCHK(tl); ERRCHK(tl);
switch (tl->t->tok) { switch (tl->t->tok) {
case CSRC: case CSRC:
if (tl->allow_inline_c) { if (tl->param->allow_inline_c) {
Fc(tl, 0, "%.*s\n", Fc(tl, 0, "%.*s\n",
(int) (tl->t->e - (tl->t->b + 4)), (int) (tl->t->e - (tl->t->b + 4)),
tl->t->b + 2); tl->t->b + 2);
......
...@@ -85,7 +85,7 @@ vcc_ParseImport(struct vcc *tl) ...@@ -85,7 +85,7 @@ vcc_ParseImport(struct vcc *tl)
sym->def_e = tl->t; sym->def_e = tl->t;
if (tl->t->tok == ID) { if (tl->t->tok == ID) {
if (!tl->unsafe_path) { if (!tl->param->unsafe_path) {
VSB_printf(tl->sb, VSB_printf(tl->sb,
"'import ... from path ...' not allowed.\nAt:"); "'import ... from path ...' not allowed.\nAt:");
vcc_ErrToken(tl, tl->t); vcc_ErrToken(tl, tl->t);
...@@ -102,7 +102,7 @@ vcc_ParseImport(struct vcc *tl) ...@@ -102,7 +102,7 @@ vcc_ParseImport(struct vcc *tl)
bprintf(fn, "%s", tl->t->dec); bprintf(fn, "%s", tl->t->dec);
vcc_NextToken(tl); vcc_NextToken(tl);
} else { } else {
bprintf(fn, "%s/libvmod_%.*s.so", tl->vmod_dir, PF(mod)); bprintf(fn, "%s/libvmod_%.*s.so", tl->param->vmod_dir, PF(mod));
} }
SkipToken(tl, ';'); SkipToken(tl, ';');
......
...@@ -108,7 +108,7 @@ vcc_checkref(struct vcc *tl, const struct symbol *sym) ...@@ -108,7 +108,7 @@ vcc_checkref(struct vcc *tl, const struct symbol *sym)
VSB_printf(tl->sb, "Unused %s %.*s, defined:\n", VSB_printf(tl->sb, "Unused %s %.*s, defined:\n",
VCC_SymKind(tl, sym), PF(sym->def_b)); VCC_SymKind(tl, sym), PF(sym->def_b));
vcc_ErrWhere(tl, sym->def_b); vcc_ErrWhere(tl, sym->def_b);
if (!tl->err_unref) { if (!tl->param->err_unref) {
VSB_printf(tl->sb, "(That was just a warning)\n"); VSB_printf(tl->sb, "(That was just a warning)\n");
tl->err = 0; tl->err = 0;
} }
...@@ -289,7 +289,7 @@ vcc_checkaction2(struct vcc *tl, const struct symbol *sym) ...@@ -289,7 +289,7 @@ vcc_checkaction2(struct vcc *tl, const struct symbol *sym)
return; return;
VSB_printf(tl->sb, "Function unused\n"); VSB_printf(tl->sb, "Function unused\n");
vcc_ErrWhere(tl, p->name); vcc_ErrWhere(tl, p->name);
if (!tl->err_unref) { if (!tl->param->err_unref) {
VSB_printf(tl->sb, "(That was just a warning)\n"); VSB_printf(tl->sb, "(That was just a warning)\n");
tl->err = 0; tl->err = 0;
} }
......
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