static void parse_new(struct vcc *tl) { struct symbol *sy1, *sy2, *sy3; struct inifin *ifp; const char *p, *s_obj, *s_init, *s_struct, *s_fini; char buf1[128]; char buf2[128]; vcc_NextToken(tl); ExpectErr(tl, ID); if (!vcc_isCid(tl->t)) { VSB_printf(tl->sb, "Names of VCL objects cannot contain '-'\n"); vcc_ErrWhere(tl, tl->t); return; } sy1 = VCC_FindSymbol(tl, tl->t, SYM_NONE); XXXAZ(sy1); sy1 = VCC_AddSymbolTok(tl, tl->t, SYM_NONE); // XXX: NONE ? XXXAN(sy1); vcc_NextToken(tl); ExpectErr(tl, '='); vcc_NextToken(tl); ExpectErr(tl, ID); sy2 = VCC_FindSymbol(tl, tl->t, SYM_OBJECT); if (sy2 == NULL) { VSB_printf(tl->sb, "Object not found: "); vcc_ErrToken(tl, tl->t); VSB_printf(tl->sb, " at "); vcc_ErrWhere(tl, tl->t); return; } XXXAN(sy2); /*lint -save -e448 */ /* Split the first three args */ p = sy2->args; s_obj = p; p += strlen(p) + 1; s_init = p; while (p[0] != '\0' || p[1] != '\0') p++; p += 2; s_struct = p; p += strlen(p) + 1; s_fini = p + strlen(p) + 1; while (p[0] != '\0' || p[1] != '\0') p++; p += 2; Fh(tl, 0, "static %s *%s;\n\n", s_struct, sy1->name); vcc_NextToken(tl); bprintf(buf1, ", &%s, \"%s\"", sy1->name, sy1->name); vcc_Eval_Func(tl, s_init, buf1, "ASDF", s_init + strlen(s_init) + 1); ifp = New_IniFin(tl); VSB_printf(ifp->fin, "\t%s(&%s);", s_fini, sy1->name); ExpectErr(tl, ';'); bprintf(buf1, ", %s", sy1->name); /* Split the methods from the args */ while (*p != '\0') { p += strlen(s_obj); bprintf(buf2, "%s%s", sy1->name, p); sy3 = VCC_AddSymbolStr(tl, buf2, SYM_FUNC); AN(sy3); sy3->eval = vcc_Eval_SymFunc; p += strlen(p) + 1; sy3->cfunc = p; p += strlen(p) + 1; /* Functions which return VOID are procedures */ if (!memcmp(p, "VOID\0", 5)) sy3->kind = SYM_PROC; sy3->args = p; sy3->extra = TlDup(tl, buf1); while (p[0] != '\0' || p[1] != '\0') { if (!memcmp(p, "ENUM\0", 5)) { /* XXX: Special case for ENUM that has it's own \0\0 end marker. Not exactly elegant, we should consider alternatives here. Maybe runlength encode the entire block? */ p += strlen(p) + 1; while (p[0] != '\0' || p[1] != '\0') p++; } p++; } p += 2; } /*lint -restore */ }
static void parse_return(struct vcc *tl) { int retval = 0; vcc_NextToken(tl); ExpectErr(tl, '('); vcc_NextToken(tl); ExpectErr(tl, ID); /* 'error' gets special handling, to allow optional status/response */ if (vcc_IdIs(tl->t, "error")) { vcc_NextToken(tl); if (tl->t->tok == ')') { VSB_printf(tl->sb, "Syntax has changed, use:\n" "\treturn(error(999));\n" "or\n" "\treturn(error(999, \"Response text\"));\n"); vcc_ErrWhere(tl, tl->t); return; } ExpectErr(tl, '('); vcc_NextToken(tl); Fb(tl, 1, "VRT_error(ctx,\n"); tl->indent += INDENT; vcc_Expr(tl, INT); ERRCHK(tl); Fb(tl, 1, ",\n"); if (tl->t->tok == ',') { vcc_NextToken(tl); vcc_Expr(tl, STRING); ERRCHK(tl); } else { Fb(tl, 1, "(const char*)0\n"); } tl->indent -= INDENT; ExpectErr(tl, ')'); vcc_NextToken(tl); Fb(tl, 1, ");\n"); Fb(tl, 1, "VRT_handling(ctx, VCL_RET_ERROR);\n"); Fb(tl, 1, "return (1);\n"); vcc_ProcAction(tl->curproc, VCL_RET_ERROR, tl->t); ExpectErr(tl, ')'); vcc_NextToken(tl); return; } #define VCL_RET_MAC(l, U, B) \ do { \ if (vcc_IdIs(tl->t, #l)) { \ Fb(tl, 1, "VRT_handling(ctx, VCL_RET_" #U ");\n"); \ Fb(tl, 1, "return (1);\n"); \ vcc_ProcAction(tl->curproc, VCL_RET_##U, tl->t);\ retval = 1; \ } \ } while (0); #include "tbl/vcl_returns.h" #undef VCL_RET_MAC if (!retval) { VSB_printf(tl->sb, "Expected return action name.\n"); vcc_ErrWhere(tl, tl->t); ERRCHK(tl); } vcc_NextToken(tl); ExpectErr(tl, ')'); vcc_NextToken(tl); }
static void vcc_ParseProbeSpec(struct vcc *tl, const struct symbol *sym, char **name) { struct fld_spec *fs; struct token *t_field; struct token *t_did = NULL, *t_window = NULL, *t_threshold = NULL; struct token *t_initial = NULL; struct vsb *vsb; char *retval; unsigned window, threshold, initial, status; double t; fs = vcc_FldSpec(tl, "?url", "?request", "?expected_response", "?timeout", "?interval", "?window", "?threshold", "?initial", NULL); SkipToken(tl, '{'); vsb = VSB_new_auto(); AN(vsb); if (sym != NULL) VSB_cat(vsb, sym->rname); else VSB_printf(vsb, "vgc_probe__%d", tl->nprobe++); AZ(VSB_finish(vsb)); retval = TlDup(tl, VSB_data(vsb)); AN(retval); VSB_destroy(&vsb); if (name != NULL) *name = retval; window = 0; threshold = 0; initial = 0; status = 0; Fh(tl, 0, "static const struct vrt_backend_probe %s[] = {{\n", retval); Fh(tl, 0, "\t.magic = VRT_BACKEND_PROBE_MAGIC,\n"); while (tl->t->tok != '}') { vcc_IsField(tl, &t_field, fs); ERRCHK(tl); if (vcc_IdIs(t_field, "url")) { vcc_Redef(tl, "Probe request", &t_did, t_field); ERRCHK(tl); ExpectErr(tl, CSTR); Fh(tl, 0, "\t.url = "); EncToken(tl->fh, tl->t); Fh(tl, 0, ",\n"); vcc_NextToken(tl); } else if (vcc_IdIs(t_field, "request")) { vcc_Redef(tl, "Probe request", &t_did, t_field); ERRCHK(tl); ExpectErr(tl, CSTR); Fh(tl, 0, "\t.request =\n"); while (tl->t->tok == CSTR) { Fh(tl, 0, "\t\t"); EncToken(tl->fh, tl->t); Fh(tl, 0, " \"\\r\\n\"\n"); vcc_NextToken(tl); } Fh(tl, 0, "\t\t\"\\r\\n\",\n"); } else if (vcc_IdIs(t_field, "timeout")) { Fh(tl, 0, "\t.timeout = "); vcc_Duration(tl, &t); ERRCHK(tl); Fh(tl, 0, "%g,\n", t); } else if (vcc_IdIs(t_field, "interval")) { Fh(tl, 0, "\t.interval = "); vcc_Duration(tl, &t); ERRCHK(tl); Fh(tl, 0, "%g,\n", t); } else if (vcc_IdIs(t_field, "window")) { t_window = tl->t; window = vcc_UintVal(tl); ERRCHK(tl); } else if (vcc_IdIs(t_field, "initial")) { t_initial = tl->t; initial = vcc_UintVal(tl); ERRCHK(tl); } else if (vcc_IdIs(t_field, "expected_response")) { status = vcc_UintVal(tl); if (status < 100 || status > 999) { VSB_printf(tl->sb, "Must specify .expected_response with " "exactly three digits " "(100 <= x <= 999)\n"); vcc_ErrWhere(tl, tl->t); return; } ERRCHK(tl); } else if (vcc_IdIs(t_field, "threshold")) { t_threshold = tl->t; threshold = vcc_UintVal(tl); ERRCHK(tl); } else { vcc_ErrToken(tl, t_field); vcc_ErrWhere(tl, t_field); ErrInternal(tl); return; } SkipToken(tl, ';'); } if (t_threshold != NULL || t_window != NULL) { if (t_threshold == NULL && t_window != NULL) { VSB_printf(tl->sb, "Must specify .threshold with .window\n"); vcc_ErrWhere(tl, t_window); return; } else if (t_threshold != NULL && t_window == NULL) { if (threshold > 64) { VSB_printf(tl->sb, "Threshold must be 64 or less.\n"); vcc_ErrWhere(tl, t_threshold); return; } window = threshold + 1; } else if (window > 64) { AN(t_window); VSB_printf(tl->sb, "Window must be 64 or less.\n"); vcc_ErrWhere(tl, t_window); return; } if (threshold > window ) { VSB_printf(tl->sb, "Threshold can not be greater than window.\n"); AN(t_threshold); vcc_ErrWhere(tl, t_threshold); AN(t_window); vcc_ErrWhere(tl, t_window); } Fh(tl, 0, "\t.window = %u,\n", window); Fh(tl, 0, "\t.threshold = %u,\n", threshold); } if (t_initial != NULL) Fh(tl, 0, "\t.initial = %u,\n", initial); else Fh(tl, 0, "\t.initial = ~0U,\n"); if (status > 0) Fh(tl, 0, "\t.exp_status = %u,\n", status); Fh(tl, 0, "}};\n"); SkipToken(tl, '}'); }
static void vcc_ParseHostDef(struct vcc *tl, const struct token *t_be, const char *vgcname) { struct token *t_field; struct token *t_val; struct token *t_host = NULL; struct token *t_port = NULL; struct token *t_path = NULL; struct token *t_hosthdr = NULL; struct symbol *pb; struct token *t_did = NULL; struct fld_spec *fs; struct inifin *ifp; struct vsb *vsb; char *p; unsigned u; double t; fs = vcc_FldSpec(tl, "?host", "?port", "?path", "?host_header", "?connect_timeout", "?first_byte_timeout", "?between_bytes_timeout", "?probe", "?max_connections", "?proxy_header", NULL); SkipToken(tl, '{'); vsb = VSB_new_auto(); AN(vsb); tl->fb = vsb; Fb(tl, 0, "\nstatic const struct vrt_backend vgc_dir_priv_%s = {\n", vgcname); Fb(tl, 0, "\t.magic = VRT_BACKEND_MAGIC,\n"); Fb(tl, 0, "\t.vcl_name = \"%.*s", PF(t_be)); Fb(tl, 0, "\",\n"); /* Check for old syntax */ if (tl->t->tok == ID && vcc_IdIs(tl->t, "set")) { VSB_printf(tl->sb, "NB: Backend Syntax has changed:\n" "Remove \"set\" and \"backend\" in front" " of backend fields.\n" ); vcc_ErrToken(tl, tl->t); VSB_printf(tl->sb, " at "); vcc_ErrWhere(tl, tl->t); return; } while (tl->t->tok != '}') { vcc_IsField(tl, &t_field, fs); ERRCHK(tl); if (vcc_IdIs(t_field, "host")) { vcc_Redef(tl, "Address", &t_did, t_field); ERRCHK(tl); ExpectErr(tl, CSTR); assert(tl->t->dec != NULL); t_host = tl->t; vcc_NextToken(tl); SkipToken(tl, ';'); } else if (vcc_IdIs(t_field, "port")) { ExpectErr(tl, CSTR); assert(tl->t->dec != NULL); t_port = tl->t; vcc_NextToken(tl); SkipToken(tl, ';'); } else if (vcc_IdIs(t_field, "path")) { if (tl->syntax < VCL_41) { VSB_printf(tl->sb, "Unix socket backends only supported" " in VCL4.1 and higher.\n"); vcc_ErrToken(tl, tl->t); VSB_printf(tl->sb, " at "); vcc_ErrWhere(tl, tl->t); return; } vcc_Redef(tl, "Address", &t_did, t_field); ERRCHK(tl); ExpectErr(tl, CSTR); assert(tl->t->dec != NULL); t_path = tl->t; vcc_NextToken(tl); SkipToken(tl, ';'); } else if (vcc_IdIs(t_field, "host_header")) { ExpectErr(tl, CSTR); assert(tl->t->dec != NULL); t_hosthdr = tl->t; vcc_NextToken(tl); SkipToken(tl, ';'); } else if (vcc_IdIs(t_field, "connect_timeout")) { Fb(tl, 0, "\t.connect_timeout = "); vcc_Duration(tl, &t); ERRCHK(tl); Fb(tl, 0, "%g,\n", t); SkipToken(tl, ';'); } else if (vcc_IdIs(t_field, "first_byte_timeout")) { Fb(tl, 0, "\t.first_byte_timeout = "); vcc_Duration(tl, &t); ERRCHK(tl); Fb(tl, 0, "%g,\n", t); SkipToken(tl, ';'); } else if (vcc_IdIs(t_field, "between_bytes_timeout")) { Fb(tl, 0, "\t.between_bytes_timeout = "); vcc_Duration(tl, &t); ERRCHK(tl); Fb(tl, 0, "%g,\n", t); SkipToken(tl, ';'); } else if (vcc_IdIs(t_field, "max_connections")) { u = vcc_UintVal(tl); ERRCHK(tl); SkipToken(tl, ';'); Fb(tl, 0, "\t.max_connections = %u,\n", u); } else if (vcc_IdIs(t_field, "proxy_header")) { t_val = tl->t; u = vcc_UintVal(tl); ERRCHK(tl); if (u != 1 && u != 2) { VSB_printf(tl->sb, ".proxy_header must be 1 or 2\n"); vcc_ErrWhere(tl, t_val); return; } SkipToken(tl, ';'); Fb(tl, 0, "\t.proxy_header = %u,\n", u); } else if (vcc_IdIs(t_field, "probe") && tl->t->tok == '{') { vcc_ParseProbeSpec(tl, NULL, &p); Fb(tl, 0, "\t.probe = %s,\n", p); ERRCHK(tl); } else if (vcc_IdIs(t_field, "probe") && tl->t->tok == ID) { if (vcc_IdIs(tl->t, "default")) { vcc_NextToken(tl); (void)vcc_default_probe(tl); } else { pb = VCC_SymbolGet(tl, SYM_PROBE, "Probe not found", XREF_REF); ERRCHK(tl); AN(pb); Fb(tl, 0, "\t.probe = %s,\n", pb->rname); } SkipToken(tl, ';'); } else if (vcc_IdIs(t_field, "probe")) { VSB_printf(tl->sb, "Expected '{' or name of probe, got "); vcc_ErrToken(tl, tl->t); VSB_printf(tl->sb, " at\n"); vcc_ErrWhere(tl, tl->t); return; } else { ErrInternal(tl); return; } } vcc_FieldsOk(tl, fs); ERRCHK(tl); if (t_host == NULL && t_path == NULL) { VSB_printf(tl->sb, "Expected .host or .path.\n"); vcc_ErrWhere(tl, t_be); return; } assert(t_host != NULL || t_path != NULL); if (t_host != NULL) /* Check that the hostname makes sense */ Emit_Sockaddr(tl, t_host, t_port); else /* Check that the path can be a legal UDS */ Emit_UDS_Path(tl, t_path, "Backend path"); ERRCHK(tl); ExpectErr(tl, '}'); /* We have parsed it all, emit the ident string */ /* Emit the hosthdr field, fall back to .host if not specified */ /* If .path is specified, set "0.0.0.0". */ Fb(tl, 0, "\t.hosthdr = "); if (t_hosthdr != NULL) EncToken(tl->fb, t_hosthdr); else if (t_host != NULL) EncToken(tl->fb, t_host); else Fb(tl, 0, "\"0.0.0.0\""); Fb(tl, 0, ",\n"); /* Close the struct */ Fb(tl, 0, "};\n"); vcc_NextToken(tl); tl->fb = NULL; AZ(VSB_finish(vsb)); Fh(tl, 0, "%s", VSB_data(vsb)); VSB_destroy(&vsb); ifp = New_IniFin(tl); VSB_printf(ifp->ini, "\t%s =\n\t VRT_new_backend_clustered(ctx, vsc_cluster,\n" "\t\t&vgc_dir_priv_%s);", vgcname, vgcname); VSB_printf(ifp->fin, "\t\tVRT_delete_backend(ctx, &%s);", vgcname); }
void vcc_Parse(struct vcc *tl) { struct toplev *tp; if (tl->t->tok != ID || !vcc_IdIs(tl->t, "vcl")) { VSB_printf(tl->sb, "VCL version declaration missing\n" "Update your VCL to Version 4 syntax, and add\n" "\tvcl 4.0;\n" "on the first line the VCL files.\n" ); vcc_ErrWhere(tl, tl->t); ERRCHK(tl); } vcc_ParseVcl(tl); ERRCHK(tl); while (tl->t->tok != EOI) { ERRCHK(tl); switch (tl->t->tok) { case CSRC: if (tl->param->allow_inline_c) { Fc(tl, 0, "%.*s\n", (int) (tl->t->e - (tl->t->b + 4)), tl->t->b + 2); vcc_NextToken(tl); } else { VSB_printf(tl->sb, "Inline-C not allowed\n"); vcc_ErrWhere(tl, tl->t); } break; case EOI: break; case ID: for (tp = toplev; tp->name != NULL; tp++) { if (!vcc_IdIs(tl->t, tp->name)) continue; tp->func(tl); break; } if (tp->name != NULL) break; /* FALLTHROUGH */ default: /* We deliberately do not mention inline-C */ VSB_printf(tl->sb, "Expected one of\n\t"); for (tp = toplev; tp->name != NULL; tp++) { if (tp[1].name == NULL) VSB_printf(tl->sb, " or "); VSB_printf(tl->sb, "'%s'", tp->name); if (tp[1].name != NULL) VSB_printf(tl->sb, ", "); } VSB_printf(tl->sb, "\nFound: "); vcc_ErrToken(tl, tl->t); VSB_printf(tl->sb, " at\n"); vcc_ErrWhere(tl, tl->t); return; } } }
static void parse_new(struct vcc *tl) { struct symbol *sy1, *sy2, *sy3; const char *p, *s_obj, *s_init, *s_struct, *s_fini; char buf1[128]; char buf2[128]; vcc_NextToken(tl); ExpectErr(tl, ID); sy1 = VCC_FindSymbol(tl, tl->t, SYM_NONE); XXXAZ(sy1); sy1 = VCC_AddSymbolTok(tl, tl->t, SYM_NONE); // XXX: NONE ? XXXAN(sy1); vcc_NextToken(tl); ExpectErr(tl, '='); vcc_NextToken(tl); ExpectErr(tl, ID); sy2 = VCC_FindSymbol(tl, tl->t, SYM_OBJECT); XXXAN(sy2); /*lint -save -e448 */ /* Split the first three args */ p = sy2->args; s_obj = p; p += strlen(p) + 1; s_init = p; while (p[0] != '\0' || p[1] != '\0') p++; p += 2; s_struct = p; p += strlen(p) + 1; s_fini = p + strlen(p) + 1; while (p[0] != '\0' || p[1] != '\0') p++; p += 2; Fh(tl, 0, "static %s *%s;\n\n", s_struct, sy1->name); vcc_NextToken(tl); bprintf(buf1, ", &%s, \"%s\"", sy1->name, sy1->name); vcc_Eval_Func(tl, s_init, buf1, "ASDF", s_init + strlen(s_init) + 1); Fd(tl, 0, "\t%s(&%s);\n", s_fini, sy1->name); ExpectErr(tl, ';'); bprintf(buf1, ", %s", sy1->name); /* Split the methods from the args */ while (*p != '\0') { p += strlen(s_obj); bprintf(buf2, "%s%s", sy1->name, p); sy3 = VCC_AddSymbolStr(tl, buf2, SYM_FUNC); AN(sy3); sy3->eval = vcc_Eval_SymFunc; p += strlen(p) + 1; sy3->cfunc = p; p += strlen(p) + 1; /* Functions which return VOID are procedures */ if (!memcmp(p, "VOID\0", 5)) sy3->kind = SYM_PROC; sy3->args = p; sy3->extra = TlDup(tl, buf1); while (p[0] != '\0' || p[1] != '\0') p++; p += 2; } /*lint -restore */ }
void vcc_ParseImport(struct vcc *tl) { void *hdl; char fn[1024]; struct token *mod, *t1; const char *modname; const char *proto; const char **spec; struct symbol *sym; const struct symbol *osym; const char *p; // int *modlen; t1 = tl->t; SkipToken(tl, ID); /* "import" */ ExpectErr(tl, ID); mod = tl->t; vcc_NextToken(tl); osym = VCC_FindSymbol(tl, mod, SYM_NONE); if (osym != NULL && osym->kind != SYM_VMOD) { vsb_printf(tl->sb, "Module %.*s conflics with other symbol.\n", PF(mod)); vcc_ErrWhere2(tl, t1, tl->t); return; } if (osym != NULL) { vsb_printf(tl->sb, "Module %.*s already imported.\n", PF(mod)); vcc_ErrWhere2(tl, t1, tl->t); vsb_printf(tl->sb, "Previous import was here:\n"); vcc_ErrWhere2(tl, osym->def_b, osym->def_e); return; } bprintf(fn, "%.*s", PF(mod)); sym = VCC_AddSymbolStr(tl, fn, SYM_VMOD); ERRCHK(tl); AN(sym); sym->def_b = t1; sym->def_e = tl->t; if (tl->t->tok == ID) { if (!vcc_IdIs(tl->t, "from")) { vsb_printf(tl->sb, "Expected 'from path...' at "); vcc_ErrToken(tl, tl->t); vcc_ErrWhere(tl, tl->t); return; } vcc_NextToken(tl); ExpectErr(tl, CSTR); bprintf(fn, "%s", tl->t->dec); vcc_NextToken(tl); } else { bprintf(fn, "%s/libvmod_%.*s.so", tl->vmod_dir, PF(mod)); } Fh(tl, 0, "static void *VGC_vmod_%.*s;\n", PF(mod)); Fi(tl, 0, "\tVRT_Vmod_Init(&VGC_vmod_%.*s,\n", PF(mod)); Fi(tl, 0, "\t &Vmod_Func_%.*s,\n", PF(mod)); Fi(tl, 0, "\t sizeof(Vmod_Func_%.*s),\n", PF(mod)); Fi(tl, 0, "\t \"%.*s\",\n", PF(mod)); Fi(tl, 0, "\t "); EncString(tl->fi, fn, NULL, 0); Fi(tl, 0, ");\n"); SkipToken(tl, ';'); hdl = dlopen(fn, RTLD_NOW | RTLD_LOCAL); if (hdl == NULL) { vsb_printf(tl->sb, "Could not load module %.*s\n\t%s\n\t%s\n", PF(mod), fn, dlerror()); vcc_ErrWhere(tl, mod); return; } modname = dlsym(hdl, "Vmod_Name"); if (modname == NULL) { vsb_printf(tl->sb, "Could not load module %.*s\n\t%s\n\t%s\n", PF(mod), fn, "Symbol Vmod_Name not found"); vcc_ErrWhere(tl, mod); return; } if (!vcc_IdIs(mod, modname)) { vsb_printf(tl->sb, "Could not load module %.*s\n\t%s\n", PF(mod), fn); vsb_printf(tl->sb, "\tModule has wrong name: <%s>\n", modname); vcc_ErrWhere(tl, mod); return; } proto = dlsym(hdl, "Vmod_Proto"); if (proto == NULL) { vsb_printf(tl->sb, "Could not load module %.*s\n\t%s\n\t%s\n", PF(mod), fn, "Symbol Vmod_Proto not found"); vcc_ErrWhere(tl, mod); return; } spec = dlsym(hdl, "Vmod_Spec"); if (spec == NULL) { vsb_printf(tl->sb, "Could not load module %.*s\n\t%s\n\t%s\n", PF(mod), fn, "Symbol Vmod_Spec not found"); vcc_ErrWhere(tl, mod); return; } Fh(tl, 0, "static struct vmod_priv vmod_priv_%.*s;\n", PF(mod)); for (; *spec != NULL; spec++) { p = *spec; if (!strcmp(p, "INIT")) { p += strlen(p) + 1; Fi(tl, 0, "\t%s(&vmod_priv_%.*s, &VCL_conf);\n", p, PF(mod)); } else { sym = VCC_AddSymbolStr(tl, p, SYM_FUNC); ERRCHK(tl); AN(sym); sym->eval = vcc_Eval_Func; p += strlen(p) + 1; sym->cfunc = p; p += strlen(p) + 1; sym->args = p; /* Functions which return VOID are procedures */ if (!memcmp(p, "VOID\0", 5)) sym->kind = SYM_PROC; } } Fh(tl, 0, "\n%s\n", proto); /* XXX: zero the function pointer structure ?*/ Ff(tl, 0, "\tvmod_priv_fini(&vmod_priv_%.*s);\n", PF(mod)); Ff(tl, 0, "\tVRT_Vmod_Fini(&VGC_vmod_%.*s);\n", PF(mod)); }
static void vcc_ParseFunction(struct vcc *tl) { int m, i; vcc_NextToken(tl); ExpectErr(tl, ID); if (!vcc_isCid(tl->t)) { VSB_printf(tl->sb, "Names of VCL sub's cannot contain '-'\n"); vcc_ErrWhere(tl, tl->t); return; } m = IsMethod(tl->t); if (m == -2) { VSB_printf(tl->sb, "VCL sub's named 'vcl*' are reserved names.\n"); vcc_ErrWhere(tl, tl->t); VSB_printf(tl->sb, "Valid vcl_* methods are:\n"); for (i = 0; method_tab[i].name != NULL; i++) VSB_printf(tl->sb, "\t%s\n", method_tab[i].name); return; } else if (m != -1) { assert(m < VCL_MET_MAX); tl->fb = tl->fm[m]; if (tl->mprocs[m] == NULL) { (void)vcc_AddDef(tl, tl->t, SYM_SUB); vcc_AddRef(tl, tl->t, SYM_SUB); tl->mprocs[m] = vcc_AddProc(tl, tl->t); } tl->curproc = tl->mprocs[m]; Fb(tl, 1, " /* ... from "); vcc_Coord(tl, tl->fb, NULL); Fb(tl, 0, " */\n"); } else { tl->fb = tl->fc; i = vcc_AddDef(tl, tl->t, SYM_SUB); if (i > 1) { VSB_printf(tl->sb, "Function %.*s redefined\n", PF(tl->t)); vcc_ErrWhere(tl, tl->t); return; } tl->curproc = vcc_AddProc(tl, tl->t); Fh(tl, 0, "int VGC_function_%.*s " "(VRT_CTX);\n", PF(tl->t)); Fc(tl, 1, "\nint __match_proto__(vcl_func_t)\n"); Fc(tl, 1, "VGC_function_%.*s(VRT_CTX)\n", PF(tl->t)); } vcc_NextToken(tl); tl->indent += INDENT; Fb(tl, 1, "{\n"); L(tl, vcc_Compound(tl)); if (m == -1) { /* * non-method subroutines must have an explicit non-action * return in case they just fall through the bottom. */ Fb(tl, 1, " return(0);\n"); } Fb(tl, 1, "}\n"); tl->indent -= INDENT; tl->fb = NULL; tl->curproc = NULL; }
static void vcc_expr_cmp(struct vcc *tl, struct expr **e, enum var_type fmt) { struct expr *e2; const struct cmps *cp; char buf[256]; char *re; const char *not; struct token *tk; *e = NULL; vcc_expr_add(tl, e, fmt); ERRCHK(tl); if ((*e)->fmt == BOOL) return; tk = tl->t; for (cp = vcc_cmps; cp->fmt != VOID; cp++) if ((*e)->fmt == cp->fmt && tl->t->tok == cp->token) break; if (cp->fmt != VOID) { vcc_NextToken(tl); vcc_expr_add(tl, &e2, (*e)->fmt); ERRCHK(tl); if (e2->fmt != (*e)->fmt) { /* XXX */ vsb_printf(tl->sb, "Comparison of different types: "); vsb_printf(tl->sb, "%s ", vcc_Type((*e)->fmt)); vcc_ErrToken(tl, tk); vsb_printf(tl->sb, " %s\n", vcc_Type(e2->fmt)); vcc_ErrWhere(tl, tk); return; } *e = vcc_expr_edit(BOOL, cp->emit, *e, e2); return; } if ((*e)->fmt == STRING && (tl->t->tok == '~' || tl->t->tok == T_NOMATCH)) { not = tl->t->tok == '~' ? "" : "!"; vcc_NextToken(tl); ExpectErr(tl, CSTR); re = vcc_regexp(tl); ERRCHK(tl); vcc_NextToken(tl); bprintf(buf, "%sVRT_re_match(\v1, %s)", not, re); *e = vcc_expr_edit(BOOL, buf, *e, NULL); return; } if ((*e)->fmt == IP && (tl->t->tok == '~' || tl->t->tok == T_NOMATCH)) { not = tl->t->tok == '~' ? "" : "!"; vcc_NextToken(tl); ExpectErr(tl, ID); vcc_AddRef(tl, tl->t, SYM_ACL); ERRCHK(tl); bprintf(buf, "%smatch_acl_named_%.*s(sp, \v1)", not, PF(tl->t)); vcc_NextToken(tl); *e = vcc_expr_edit(BOOL, buf, *e, NULL); return; } if ((*e)->fmt == IP && (tl->t->tok == T_EQ || tl->t->tok == T_NEQ)) { vcc_Acl_Hack(tl, buf); *e = vcc_expr_edit(BOOL, buf, *e, NULL); return; } if ((*e)->fmt == BACKEND && (tl->t->tok == T_EQ || tl->t->tok == T_NEQ)) { vcc_NextToken(tl); ExpectErr(tl, ID); vcc_AddRef(tl, tl->t, SYM_BACKEND); ERRCHK(tl); bprintf(buf, "(\v1 %.*s VGCDIR(_%.*s))", PF(tk), PF(tl->t)); vcc_NextToken(tl); *e = vcc_expr_edit(BOOL, buf, *e, NULL); return; } switch (tl->t->tok) { case T_EQ: case T_NEQ: case '<': case T_LEQ: case '>': case T_GEQ: case '~': case T_NOMATCH: vsb_printf(tl->sb, "Operator %.*s not possible on %s\n", PF(tl->t), vcc_Type((*e)->fmt)); vcc_ErrWhere(tl, tl->t); return; default: break; } if (fmt == BOOL && (*e)->fmt == STRING) { *e = vcc_expr_edit(BOOL, "(\v1 != 0)", *e, NULL); return; } }
static void vcc_expr_add(struct vcc *tl, struct expr **e, enum var_type fmt) { struct expr *e2; enum var_type f2; struct token *tk; *e = NULL; vcc_expr_mul(tl, e, fmt); ERRCHK(tl); f2 = (*e)->fmt; if ((f2 == STRING_LIST || f2 == STRING) && tl->t->tok == '+') { while (tl->t->tok == '+') { vcc_NextToken(tl); vcc_expr_mul(tl, &e2, STRING); ERRCHK(tl); if (e2->fmt != STRING && e2->fmt != STRING_LIST) vcc_expr_tostring(&e2, f2); ERRCHK(tl); assert(e2->fmt == STRING || e2->fmt == STRING_LIST); if ((*e)->constant && e2->constant) { assert((*e)->fmt == STRING); assert(e2->fmt == STRING); *e = vcc_expr_edit(STRING, "\v1\n\v2", *e, e2); } else { *e = vcc_expr_edit(STRING_LIST, "\v1,\n\v2", *e, e2); } } } if (fmt != STRING_LIST && (*e)->fmt == STRING_LIST) *e = vcc_expr_edit(STRING, "\v+VRT_WrkString(sp,\n\v1,\nvrt_magic_string_end)", *e, NULL); if (fmt == STRING_LIST && (*e)->fmt == STRING) (*e)->fmt = STRING_LIST; switch(f2) { case INT: break; case TIME: break; case DURATION: break; case BYTES: break; default: if (tl->t->tok != '+' && tl->t->tok != '-') return; vsb_printf(tl->sb, "Operator %.*s not possible on type %s.\n", PF(tl->t), vcc_Type(f2)); vcc_ErrWhere(tl, tl->t); return; } while (tl->t->tok == '+' || tl->t->tok == '-') { if (f2 == TIME) f2 = DURATION; tk = tl->t; vcc_NextToken(tl); vcc_expr_mul(tl, &e2, f2); ERRCHK(tl); if (tk->tok == '-' && (*e)->fmt == TIME && e2->fmt == TIME) { /* OK */ } else if (tk->tok == '-' && (*e)->fmt == BYTES && e2->fmt == BYTES) { /* OK */ } else if (e2->fmt != f2) { vsb_printf(tl->sb, "%s %.*s %s not possible.\n", vcc_Type((*e)->fmt), PF(tk), vcc_Type(e2->fmt)); vcc_ErrWhere2(tl, tk, tl->t); return; } if (tk->tok == '+') *e = vcc_expr_edit(f2, "(\v1+\v2)", *e, e2); else if (f2 == TIME && e2->fmt == TIME) *e = vcc_expr_edit(DURATION, "(\v1-\v2)", *e, e2); else *e = vcc_expr_edit(f2, "(\v1-\v2)", *e, e2); } }
static void vcc_expr4(struct vcc *tl, struct expr **e, enum var_type fmt) { struct expr *e1, *e2; const struct symbol *sym; double d; *e = NULL; if (tl->t->tok == '(') { SkipToken(tl, '('); vcc_expr0(tl, &e2, fmt); ERRCHK(tl); SkipToken(tl, ')'); *e = vcc_expr_edit(e2->fmt, "(\v1)", e2, NULL); return; } switch(tl->t->tok) { case ID: /* * XXX: what if var and func/proc had same name ? * XXX: look for SYM_VAR first for consistency ? */ sym = VCC_FindSymbol(tl, tl->t, SYM_NONE); if (sym == NULL || sym->eval == NULL) { vsb_printf(tl->sb, "Symbol not found: "); vcc_ErrToken(tl, tl->t); vsb_printf(tl->sb, " (expected type %s):\n", vcc_Type(fmt)); vcc_ErrWhere(tl, tl->t); return; } AN(sym); switch(sym->kind) { case SYM_VAR: case SYM_FUNC: case SYM_BACKEND: AN(sym->eval); AZ(*e); sym->eval(tl, e, sym); return; default: break; } vsb_printf(tl->sb, "Symbol type (%s) can not be used in expression.\n", VCC_SymKind(tl, sym)); vcc_ErrWhere(tl, tl->t); return; case CSTR: assert(fmt != VOID); e1 = vcc_new_expr(); EncToken(e1->vsb, tl->t); e1->fmt = STRING; e1->t1 = tl->t; e1->constant = 1; vcc_NextToken(tl); AZ(vsb_finish(e1->vsb)); *e = e1; break; case CNUM: /* * XXX: %g may not have enough decimals by default * XXX: but %a is ugly, isn't it ? */ assert(fmt != VOID); if (fmt == DURATION) { vcc_RTimeVal(tl, &d); ERRCHK(tl); e1 = vcc_mk_expr(DURATION, "%g", d); } else if (fmt == BYTES) { vcc_ByteVal(tl, &d); ERRCHK(tl); e1 = vcc_mk_expr(BYTES, "%.1f", d); ERRCHK(tl); } else if (fmt == REAL) { e1 = vcc_mk_expr(REAL, "%g", vcc_DoubleVal(tl)); ERRCHK(tl); } else { e1 = vcc_mk_expr(INT, "%.*s", PF(tl->t)); vcc_NextToken(tl); } e1->constant = 1; *e = e1; break; default: vsb_printf(tl->sb, "Unknown token "); vcc_ErrToken(tl, tl->t); vsb_printf(tl->sb, " when looking for %s\n\n", vcc_Type(fmt)); vcc_ErrWhere(tl, tl->t); break; } }
static void vcc_ParseHostDef(struct vcc *tl, const struct token *t_be, const char *vgcname) { struct token *t_field; struct token *t_val; struct token *t_host = NULL; struct token *t_port = NULL; struct token *t_hosthdr = NULL; struct fld_spec *fs; struct inifin *ifp; struct vsb *vsb; char *p; unsigned u; double t; fs = vcc_FldSpec(tl, "!host", "?port", "?host_header", "?connect_timeout", "?first_byte_timeout", "?between_bytes_timeout", "?probe", "?max_connections", "?proxy_header", NULL); SkipToken(tl, '{'); vsb = VSB_new_auto(); AN(vsb); tl->fb = vsb; Fb(tl, 0, "\nstatic const struct vrt_backend vgc_dir_priv_%s = {\n", vgcname); Fb(tl, 0, "\t.magic = VRT_BACKEND_MAGIC,\n"); Fb(tl, 0, "\t.vcl_name = \"%.*s", PF(t_be)); Fb(tl, 0, "\",\n"); /* Check for old syntax */ if (tl->t->tok == ID && vcc_IdIs(tl->t, "set")) { VSB_printf(tl->sb, "NB: Backend Syntax has changed:\n" "Remove \"set\" and \"backend\" in front" " of backend fields.\n" ); vcc_ErrToken(tl, tl->t); VSB_printf(tl->sb, " at "); vcc_ErrWhere(tl, tl->t); return; } while (tl->t->tok != '}') { vcc_IsField(tl, &t_field, fs); ERRCHK(tl); if (vcc_IdIs(t_field, "host")) { ExpectErr(tl, CSTR); assert(tl->t->dec != NULL); t_host = tl->t; vcc_NextToken(tl); SkipToken(tl, ';'); } else if (vcc_IdIs(t_field, "port")) { ExpectErr(tl, CSTR); assert(tl->t->dec != NULL); t_port = tl->t; vcc_NextToken(tl); SkipToken(tl, ';'); } else if (vcc_IdIs(t_field, "host_header")) { ExpectErr(tl, CSTR); assert(tl->t->dec != NULL); t_hosthdr = tl->t; vcc_NextToken(tl); SkipToken(tl, ';'); } else if (vcc_IdIs(t_field, "connect_timeout")) { Fb(tl, 0, "\t.connect_timeout = "); vcc_Duration(tl, &t); ERRCHK(tl); Fb(tl, 0, "%g,\n", t); SkipToken(tl, ';'); } else if (vcc_IdIs(t_field, "first_byte_timeout")) { Fb(tl, 0, "\t.first_byte_timeout = "); vcc_Duration(tl, &t); ERRCHK(tl); Fb(tl, 0, "%g,\n", t); SkipToken(tl, ';'); } else if (vcc_IdIs(t_field, "between_bytes_timeout")) { Fb(tl, 0, "\t.between_bytes_timeout = "); vcc_Duration(tl, &t); ERRCHK(tl); Fb(tl, 0, "%g,\n", t); SkipToken(tl, ';'); } else if (vcc_IdIs(t_field, "max_connections")) { u = vcc_UintVal(tl); ERRCHK(tl); SkipToken(tl, ';'); Fb(tl, 0, "\t.max_connections = %u,\n", u); } else if (vcc_IdIs(t_field, "proxy_header")) { t_val = tl->t; u = vcc_UintVal(tl); ERRCHK(tl); if (u != 1 && u != 2) { VSB_printf(tl->sb, ".proxy_header must be 1 or 2\n"); vcc_ErrWhere(tl, t_val); return; } SkipToken(tl, ';'); Fb(tl, 0, "\t.proxy_header = %u,\n", u); } else if (vcc_IdIs(t_field, "probe") && tl->t->tok == '{') { vcc_ParseProbeSpec(tl, NULL, &p); Fb(tl, 0, "\t.probe = &%s,\n", p); ERRCHK(tl); } else if (vcc_IdIs(t_field, "probe") && tl->t->tok == ID) { if (VCC_FindSymbol(tl, tl->t, SYM_PROBE) == NULL) { VSB_printf(tl->sb, "Probe %.*s not found\n", PF(tl->t)); vcc_ErrWhere(tl, tl->t); return; } Fb(tl, 0, "\t.probe = &vgc_probe_%.*s,\n", PF(tl->t)); vcc_AddRef(tl, tl->t, SYM_PROBE); vcc_NextToken(tl); SkipToken(tl, ';'); } else if (vcc_IdIs(t_field, "probe")) { VSB_printf(tl->sb, "Expected '{' or name of probe, got "); vcc_ErrToken(tl, tl->t); VSB_printf(tl->sb, " at\n"); vcc_ErrWhere(tl, tl->t); return; } else { ErrInternal(tl); return; } } vcc_FieldsOk(tl, fs); ERRCHK(tl); /* Check that the hostname makes sense */ assert(t_host != NULL); Emit_Sockaddr(tl, t_host, t_port); ERRCHK(tl); ExpectErr(tl, '}'); /* We have parsed it all, emit the ident string */ /* Emit the hosthdr field, fall back to .host if not specified */ Fb(tl, 0, "\t.hosthdr = "); if (t_hosthdr != NULL) EncToken(tl->fb, t_hosthdr); else EncToken(tl->fb, t_host); Fb(tl, 0, ",\n"); /* Close the struct */ Fb(tl, 0, "};\n"); vcc_NextToken(tl); tl->fb = NULL; AZ(VSB_finish(vsb)); Fh(tl, 0, "%s", VSB_data(vsb)); VSB_destroy(&vsb); ifp = New_IniFin(tl); VSB_printf(ifp->ini, "\t%s =\n\t VRT_new_backend(ctx, &vgc_dir_priv_%s);", vgcname, vgcname); }
static void vcc_ParseHostDef(struct vcc *tl, int serial, const char *vgcname) { struct token *t_field; struct token *t_host = NULL; struct token *t_port = NULL; struct token *t_hosthdr = NULL; unsigned saint = UINT_MAX; struct fld_spec *fs; struct vsb *vsb; unsigned u; double t; Fh(tl, 1, "\n#define VGC_backend_%s %d\n", vgcname, tl->ndirector); fs = vcc_FldSpec(tl, "!host", "?port", "?host_header", "?connect_timeout", "?first_byte_timeout", "?between_bytes_timeout", "?probe", "?max_connections", "?saintmode_threshold", NULL); SkipToken(tl, '{'); vsb = VSB_new_auto(); AN(vsb); tl->fb = vsb; Fb(tl, 0, "\nstatic const struct vrt_backend vgc_dir_priv_%s = {\n", vgcname); Fb(tl, 0, "\t.vcl_name = \"%.*s", PF(tl->t_dir)); if (serial >= 0) Fb(tl, 0, "[%d]", serial); Fb(tl, 0, "\",\n"); /* Check for old syntax */ if (tl->t->tok == ID && vcc_IdIs(tl->t, "set")) { VSB_printf(tl->sb, "NB: Backend Syntax has changed:\n" "Remove \"set\" and \"backend\" in front" " of backend fields.\n" ); vcc_ErrToken(tl, tl->t); VSB_printf(tl->sb, " at "); vcc_ErrWhere(tl, tl->t); return; } while (tl->t->tok != '}') { vcc_IsField(tl, &t_field, fs); ERRCHK(tl); if (vcc_IdIs(t_field, "host")) { ExpectErr(tl, CSTR); assert(tl->t->dec != NULL); t_host = tl->t; vcc_NextToken(tl); SkipToken(tl, ';'); } else if (vcc_IdIs(t_field, "port")) { ExpectErr(tl, CSTR); assert(tl->t->dec != NULL); t_port = tl->t; vcc_NextToken(tl); SkipToken(tl, ';'); } else if (vcc_IdIs(t_field, "host_header")) { ExpectErr(tl, CSTR); assert(tl->t->dec != NULL); t_hosthdr = tl->t; vcc_NextToken(tl); SkipToken(tl, ';'); } else if (vcc_IdIs(t_field, "connect_timeout")) { Fb(tl, 0, "\t.connect_timeout = "); vcc_Duration(tl, &t); ERRCHK(tl); Fb(tl, 0, "%g,\n", t); SkipToken(tl, ';'); } else if (vcc_IdIs(t_field, "first_byte_timeout")) { Fb(tl, 0, "\t.first_byte_timeout = "); vcc_Duration(tl, &t); ERRCHK(tl); Fb(tl, 0, "%g,\n", t); SkipToken(tl, ';'); } else if (vcc_IdIs(t_field, "between_bytes_timeout")) { Fb(tl, 0, "\t.between_bytes_timeout = "); vcc_Duration(tl, &t); ERRCHK(tl); Fb(tl, 0, "%g,\n", t); SkipToken(tl, ';'); } else if (vcc_IdIs(t_field, "max_connections")) { u = vcc_UintVal(tl); ERRCHK(tl); SkipToken(tl, ';'); Fb(tl, 0, "\t.max_connections = %u,\n", u); } else if (vcc_IdIs(t_field, "saintmode_threshold")) { u = vcc_UintVal(tl); /* UINT_MAX == magic number to mark as unset, so * not allowed here. */ if (u == UINT_MAX) { VSB_printf(tl->sb, "Value outside allowed range: "); vcc_ErrToken(tl, tl->t); VSB_printf(tl->sb, " at\n"); vcc_ErrWhere(tl, tl->t); } ERRCHK(tl); saint = u; SkipToken(tl, ';'); } else if (vcc_IdIs(t_field, "probe") && tl->t->tok == '{') { Fb(tl, 0, "\t.probe = &vgc_probe__%d,\n", tl->nprobe); vcc_ParseProbeSpec(tl); ERRCHK(tl); } else if (vcc_IdIs(t_field, "probe") && tl->t->tok == ID) { Fb(tl, 0, "\t.probe = &vgc_probe_%.*s,\n", PF(tl->t)); vcc_AddRef(tl, tl->t, SYM_PROBE); vcc_NextToken(tl); SkipToken(tl, ';'); } else if (vcc_IdIs(t_field, "probe")) { VSB_printf(tl->sb, "Expected '{' or name of probe."); vcc_ErrToken(tl, tl->t); VSB_printf(tl->sb, " at\n"); vcc_ErrWhere(tl, tl->t); return; } else { ErrInternal(tl); return; } } vcc_FieldsOk(tl, fs); ERRCHK(tl); /* Check that the hostname makes sense */ assert(t_host != NULL); if (t_port != NULL) Emit_Sockaddr(tl, t_host, t_port->dec); else Emit_Sockaddr(tl, t_host, "80"); ERRCHK(tl); ExpectErr(tl, '}'); /* We have parsed it all, emit the ident string */ /* Emit the hosthdr field, fall back to .host if not specified */ Fb(tl, 0, "\t.hosthdr = "); if (t_hosthdr != NULL) EncToken(tl->fb, t_hosthdr); else EncToken(tl->fb, t_host); Fb(tl, 0, ",\n"); Fb(tl, 0, "\t.saintmode_threshold = %d,\n",saint); /* Close the struct */ Fb(tl, 0, "};\n"); vcc_NextToken(tl); tl->fb = NULL; AZ(VSB_finish(vsb)); Fh(tl, 0, "%s", VSB_data(vsb)); VSB_delete(vsb); Fi(tl, 0, "\tVRT_init_dir(cli, VCL_conf.director, \"simple\",\n" "\t VGC_backend_%s, &vgc_dir_priv_%s);\n", vgcname, vgcname); Ff(tl, 0, "\tVRT_fini_dir(cli, VGCDIR(%s));\n", vgcname); tl->ndirector++; }