varnish-cache/lib/libvcc/vcc_action.c
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2015 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6
 *
7
 * SPDX-License-Identifier: BSD-2-Clause
8
 *
9 1670764
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16 1670764
 *    documentation and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 1670764
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1670764
 * SUCH DAMAGE.
29
 *
30
 * This file parses the real action of the VCL code, the procedure
31
 * statements which do the actual work.
32
 */
33
34 1670764
#include "config.h"
35
36
#include <string.h>
37
38
#include "vcc_compile.h"
39
#include "libvcc.h"
40
41
/*--------------------------------------------------------------------*/
42
43
static void v_matchproto_(sym_act_f)
44 1924151
vcc_act_call(struct vcc *tl, struct token *t, struct symbol *sym)
45
{
46
        struct token *t0;
47
        unsigned u;
48
49 1924151
        (void)t;
50 1924151
        ExpectErr(tl, ID);
51 3594915
        t0 = tl->t;
52 1924151
        sym = VCC_SymbolGet(tl, SYM_MAIN, SYM_NONE, SYMTAB_PARTIAL_NOERR,
53
            XREF_REF);
54 1924151
        if (sym == NULL) {
55 1722168
                sym = VCC_SymbolGet(tl, SYM_MAIN, SYM_SUB, SYMTAB_CREATE,
56
                    XREF_REF);
57 3392932
                if (sym == NULL)
58 0
                        return;
59 1722168
                VCC_GlobalSymbol(sym, SUB);
60 1722168
        }
61 1670764
62 1924151
        if (sym->kind == SYM_SUB) {
63 1924082
                vcc_AddCall(tl, t0, sym);
64
65 3594846
                Fb(tl, 1, "%s(ctx, VSUB_STATIC, NULL);\n", sym->lname);
66 1924082
                SkipToken(tl, ';');
67 1924082
                return;
68
        }
69 1670764
70 69
        tl->t = t0;
71 69
        u = tl->unique++;
72
73 1670833
        Fb(tl, 1, "{\n");
74 69
        tl->indent += INDENT;
75 69
        Fb(tl, 2, "VCL_SUB call_%u =\n", u);
76 69
        tl->indent += INDENT;
77 69
        vcc_Expr(tl, SUB);
78 1670833
        Fb(tl, 2, ";\n\n");
79 69
        SkipToken(tl, ';');
80 46
        tl->indent -= INDENT;
81 46
        Fb(tl, 2, "if (call_%u == NULL) {\n", u);
82 46
        Fb(tl, 2, "  VRT_fail(ctx, \"Tried to call NULL SUB near"
83
           " source %%u line %%u\",\n");
84 46
        Fb(tl, 2, "    VGC_ref[%u].source,\n", tl->cnt);
85 1670810
        Fb(tl, 2, "    VGC_ref[%u].line);\n", tl->cnt);
86 46
        Fb(tl, 2, "  END_;\n");
87 46
        Fb(tl, 2, "}\n");
88 46
        Fb(tl, 2, "call_%u->func(ctx, VSUB_STATIC, NULL);\n", u);
89 46
        tl->indent -= INDENT;
90 1670810
        Fb(tl, 1, "}\n");
91 1924151
}
92
93
/*--------------------------------------------------------------------*/
94 1670764
95
static const struct assign {
96
        vcc_type_t              type;
97
        unsigned                oper;
98
        vcc_type_t              want;
99
        const char              *expr;
100
} assign[] = {
101
        { INT,          T_INCR,         INT, "\v + " },
102
        { INT,          T_DECR,         INT, "\v - " },
103
        { INT,          T_MUL,          INT, "\v * " },
104 1670764
        { INT,          T_DIV,          INT, "\v / " },
105
        { INT,          '=',            INT },
106
        { INT,          0,              INT },
107
        { TIME,         T_INCR,         DURATION, "\v + " },
108
        { TIME,         T_DECR,         DURATION, "\v - " },
109
        { TIME,         T_MUL,          REAL, "\v * " },
110 1670764
        { TIME,         T_DIV,          REAL, "\v / " },
111
        { TIME,         '=',            TIME },
112
        { TIME,         0,              TIME },
113
        { DURATION,     T_INCR,         DURATION, "\v + " },
114
        { DURATION,     T_DECR,         DURATION, "\v - " },
115
        { DURATION,     T_MUL,          REAL, "\v * " },
116
        { DURATION,     T_DIV,          REAL, "\v / " },
117
        { DURATION,     '=',            DURATION },
118
        { DURATION,     0,              DURATION },
119
        { STRING,       T_INCR,         STRANDS, "\v,\n" },
120 1670764
        { STRING,       '=',            STRANDS, "0,\n" },
121
        { HEADER,       T_INCR,         STRANDS, "VRT_GetHdr(ctx, \v),\n" },
122
        { HEADER,       '=',            STRANDS, "0,\n" },
123
        { BODY,         '=',            BODY, "LBODY_SET_" },
124
        { BODY,         T_INCR,         BODY, "LBODY_ADD_" },
125
        { VOID,         '=',            VOID }
126
};
127
128
static void
129 728147
vcc_assign_expr(struct vcc *tl, struct symbol *sym, const struct assign *ap)
130
{
131
        const char *e;
132 728147
        unsigned indent = 1;
133
134 728147
        e = ap->expr;
135 728147
        if (e == NULL)
136 148992
                return;
137
138 3249463
        while (*e != '\0') {
139 2670308
                if (*e == '\v')
140 460
                        Fb(tl, indent, "%s", sym->rname);
141
                else
142 2669848
                        Fb(tl, indent, "%c", *e);
143 2670308
                indent = 0;
144 2670308
                e++;
145
        }
146 728147
}
147
148
/*--------------------------------------------------------------------*/
149
150
static void v_matchproto_(sym_act_f)
151 728423
vcc_act_set(struct vcc *tl, struct token *t, struct symbol *sym)
152
{
153
        const struct assign *ap;
154
        vcc_type_t type;
155
156 728423
        (void)t;
157 728423
        ExpectErr(tl, ID);
158 728423
        t = tl->t;
159 728423
        sym = VCC_SymbolGet(tl, SYM_MAIN, SYM_VAR, SYMTAB_EXISTING, XREF_NONE);
160 728423
        ERRCHK(tl);
161 728239
        AN(sym);
162 728239
        vcc_AddUses(tl, t, tl->t, sym, XREF_WRITE);
163 728239
        ERRCHK(tl);
164 728239
        type = sym->type;
165 15999937
        for (ap = assign; ap->type != VOID; ap++) {
166 15923279
                if (ap->type != type)
167 14536076
                        continue;
168 1387203
                if (ap->oper != tl->t->tok)
169 735622
                        continue;
170 651581
                vcc_NextToken(tl);
171 651581
                type = ap->want;
172 651581
                break;
173
        }
174
175 728239
        if (ap->type == VOID)
176 76658
                SkipToken(tl, ap->oper);
177
178 728147
        Fb(tl, 1, "%s\n", sym->lname);
179 728147
        tl->indent += INDENT;
180 728147
        vcc_assign_expr(tl, sym, ap);
181 728147
        vcc_Expr(tl, type);
182 728147
        ERRCHK(tl);
183 727181
        tl->indent -= INDENT;
184 727181
        Fb(tl, 1, ");\n");
185 727181
        SkipToken(tl, ';');
186 728423
}
187
188
/*--------------------------------------------------------------------*/
189
190
static void v_matchproto_(sym_act_f)
191 134019
vcc_act_unset(struct vcc *tl, struct token *t, struct symbol *sym)
192
{
193
194
        /* XXX: Wrong, should use VCC_Expr(HEADER) */
195 134019
        ExpectErr(tl, ID);
196 134019
        t = tl->t;
197 134019
        sym = VCC_SymbolGet(tl, SYM_MAIN, SYM_VAR, SYMTAB_EXISTING, XREF_NONE);
198 134019
        ERRCHK(tl);
199 134019
        AN(sym);
200 134019
        if (sym->u_methods == 0) {
201 46
                vcc_ErrWhere2(tl, t, tl->t);
202 46
                VSB_cat(tl->sb, "Variable cannot be unset.\n");
203 46
                return;
204
        }
205 133973
        vcc_AddUses(tl, t, tl->t, sym, XREF_UNSET);
206 133973
        Fb(tl, 1, "%s;\n", sym->uname);
207 133973
        SkipToken(tl, ';');
208 134019
}
209
210
/*--------------------------------------------------------------------*/
211
212
static void v_matchproto_(sym_act_f)
213 345
vcc_act_ban(struct vcc *tl, struct token *t, struct symbol *sym)
214
{
215
216 345
        (void)t;
217 345
        (void)sym;
218
219 345
        SkipToken(tl, '(');
220
221 345
        Fb(tl, 1, "(void) VRT_ban_string(ctx, \n");
222 345
        tl->indent += INDENT;
223 345
        vcc_Expr(tl, STRING);
224 345
        tl->indent -= INDENT;
225 345
        ERRCHK(tl);
226 322
        Fb(tl, 1, ");\n");
227
228 322
        SkipToken(tl, ')');
229 322
        SkipToken(tl, ';');
230 345
}
231
232
/*--------------------------------------------------------------------*/
233
234
static void v_matchproto_(sym_act_f)
235 199062
vcc_act_hash_data(struct vcc *tl, struct token *t, struct symbol *sym)
236
{
237
238 199062
        (void)t;
239 199062
        (void)sym;
240 199062
        SkipToken(tl, '(');
241
242 199062
        Fb(tl, 1, "VRT_hashdata(ctx,\n  ");
243 199062
        vcc_Expr(tl, STRANDS);
244 199062
        ERRCHK(tl);
245 199039
        Fb(tl, 1, ");\n");
246 199039
        SkipToken(tl, ')');
247 199039
        SkipToken(tl, ';');
248 199062
}
249
250
/*--------------------------------------------------------------------*/
251
252
static void
253 184
vcc_act_return_pass(struct vcc *tl)
254
{
255
256 184
        SkipToken(tl, '(');
257 184
        Fb(tl, 1, "VRT_hit_for_pass(ctx,\n");
258 184
        tl->indent += INDENT;
259 184
        vcc_Expr(tl, DURATION);
260 184
        ERRCHK(tl);
261 184
        SkipToken(tl, ')');
262 184
        Fb(tl, 1, ");\n");
263 184
        tl->indent -= INDENT;
264 184
}
265
/*--------------------------------------------------------------------*/
266
267
static void
268 1288
vcc_act_return_fail(struct vcc *tl)
269
{
270 1288
        SkipToken(tl, '(');
271 1288
        Fb(tl, 1, "VRT_fail(ctx, \"%%s\",\n");
272 1288
        tl->indent += INDENT;
273 1288
        vcc_Expr(tl, STRING);
274 1288
        tl->indent -= INDENT;
275 1288
        ERRCHK(tl);
276 1288
        SkipToken(tl, ')');
277 1288
        Fb(tl, 1, ");\n");
278 1288
}
279
280
/*--------------------------------------------------------------------*/
281
282
static void
283 402149
vcc_act_return_synth(struct vcc *tl)
284
{
285
286 402149
        SkipToken(tl, '(');
287 402149
        Fb(tl, 1, "VRT_synth(ctx,\n");
288 402149
        tl->indent += INDENT;
289 402149
        vcc_Expr(tl, INT);
290 402149
        ERRCHK(tl);
291 402149
        Fb(tl, 1, ",\n");
292 402149
        if (tl->t->tok == ',') {
293 266060
                vcc_NextToken(tl);
294 266060
                vcc_Expr(tl, STRING);
295 266060
                ERRCHK(tl);
296 266060
        } else {
297 136089
                Fb(tl, 1, "(const char*)0\n");
298
        }
299 402149
        tl->indent -= INDENT;
300 402149
        SkipToken(tl, ')');
301 402149
        Fb(tl, 1, ");\n");
302 402149
}
303
304
/*--------------------------------------------------------------------*/
305
306
static void
307 552
vcc_act_return_vcl(struct vcc *tl)
308
{
309
        struct symbol *sym;
310
        struct inifin *p;
311
        char buf[1024];
312
313 552
        SkipToken(tl, '(');
314 552
        ExpectErr(tl, ID);
315 552
        sym = VCC_SymbolGet(tl, SYM_MAIN, SYM_VCL, SYMTAB_EXISTING, XREF_NONE);
316 552
        ERRCHK(tl);
317 506
        AN(sym);
318 506
        if (sym->eval_priv == NULL) {
319 483
                VSB_cat(tl->symtab, ",\n    {\n");
320 483
                VSB_cat(tl->symtab, "\t\"dir\": \"import\",\n");
321 483
                VSB_cat(tl->symtab, "\t\"type\": \"$VCL\",\n");
322 483
                VSB_printf(tl->symtab, "\t\"name\": \"%s\"\n", sym->name);
323 483
                VSB_cat(tl->symtab, "   }");
324
325 483
                bprintf(buf, "vgc_vcl_%u", tl->unique++);
326 483
                sym->eval_priv = strdup(buf);
327 483
                AN(sym->eval_priv);
328
329 483
                Fh(tl, 0, "static VCL_VCL %s;", buf);
330 483
                Fh(tl, 0, "\t/* VCL %s */\n", sym->name);
331
332 483
                p = New_IniFin(tl);
333 483
                AN(p);
334 966
                VSB_printf(p->ini, "\t%s = VPI_vcl_get(ctx, \"%s\");",
335 483
                    buf, sym->name);
336 966
                VSB_printf(p->fin, "\tVPI_vcl_rel(ctx, %s);",
337 483
                    buf);
338 483
        }
339 1012
        Fb(tl, 1, "VPI_vcl_select(ctx, %s);\t/* %s */\n",
340 506
            (const char*)sym->eval_priv, sym->name);
341 506
        SkipToken(tl, ')');
342 552
}
343
344
/*--------------------------------------------------------------------*/
345
346
static void v_matchproto_(sym_act_f)
347 1670971
vcc_act_return(struct vcc *tl, struct token *t, struct symbol *sym)
348
{
349
        unsigned hand, mask;
350
        const char *h;
351
352 1670971
        (void)t;
353 1670971
        (void)sym;
354 1670971
        AN(tl->curproc);
355 1670971
        if (tl->t->tok == ';' && tl->curproc->method == NULL) {
356 184
                SkipToken(tl, ';');
357 184
                Fb(tl, 1, "return;\n");
358 184
                return;
359
        }
360 1670787
        SkipToken(tl, '(');
361 1670764
        ExpectErr(tl, ID);
362
363 1670764
        hand = VCL_RET_MAX;
364 1670764
        h = NULL;
365 1670764
        mask = 0;
366
#define VCL_RET_MAC(l, U, B)                            \
367
                if (vcc_IdIs(tl->t, #l)) {              \
368
                        hand = VCL_RET_ ## U;           \
369
                        h = #U;                         \
370
                        mask = (B);                     \
371
                }
372
#include "tbl/vcl_returns.h"
373 1670764
        if (h == NULL) {
374 23
                VSB_cat(tl->sb, "Expected return action name.\n");
375 23
                vcc_ErrWhere(tl, tl->t);
376 23
                ERRCHK(tl);
377 0
        }
378 1670741
        assert(hand < VCL_RET_MAX);
379 1670741
        AN(mask);
380
381
        vcc_ProcAction(tl->curproc, hand, mask, tl->t);
382
        vcc_NextToken(tl);
383 1670741
        if (tl->t->tok == '(') {
384 404196
                if (hand == VCL_RET_SYNTH || hand == VCL_RET_ERROR)
385 402149
                        vcc_act_return_synth(tl);
386 2047
                else if (hand == VCL_RET_VCL)
387 552
                        vcc_act_return_vcl(tl);
388 1495
                else if (hand == VCL_RET_PASS)
389 184
                        vcc_act_return_pass(tl);
390 1311
                else if (hand == VCL_RET_FAIL)
391 1288
                        vcc_act_return_fail(tl);
392
                else {
393 23
                        VSB_cat(tl->sb, "Arguments not allowed.\n");
394 23
                        vcc_ErrWhere(tl, tl->t);
395
                }
396 404196
        } else {
397 1266545
                if (hand == VCL_RET_SYNTH || hand == VCL_RET_VCL) {
398 23
                        VSB_cat(tl->sb, "Missing argument.\n");
399 23
                        vcc_ErrWhere(tl, tl->t);
400 23
                }
401
        }
402 1670741
        ERRCHK(tl);
403
        Fb(tl, 1, "END_;\n");
404 1670649
        if (hand == VCL_RET_FAIL)
405 1794
                Fb(tl, 1, "VRT_fail(ctx, \"Failed from VCL\");\n");
406
        else
407 1668855
                Fb(tl, 1, "VRT_handling(ctx, VCL_RET_%s);\n", h);
408 1670649
        SkipToken(tl, ')');
409 1670649
        SkipToken(tl, ';');
410
}
411
412
/*--------------------------------------------------------------------*/
413
414
static void v_matchproto_(sym_act_f)
415 138
vcc_act_synthetic(struct vcc *tl, struct token *t, struct symbol *sym)
416
{
417
418 138
        (void)t;
419 138
        (void)sym;
420 138
        ExpectErr(tl, '(');
421 138
        ERRCHK(tl);
422 138
        vcc_NextToken(tl);
423
424 138
        Fb(tl, 1, "VRT_synth_strands(ctx, ");
425 138
        vcc_Expr(tl, STRANDS);
426 138
        ERRCHK(tl);
427 115
        Fb(tl, 1, ");\n");
428
429 115
        SkipToken(tl, ')');
430 115
        SkipToken(tl, ';');
431 138
}
432
433
/*--------------------------------------------------------------------*/
434
435
// The pp[] trick is to make the length of #name visible to flexelint.
436
#define ACT(name, func, mask)                                           \
437
        do {                                                            \
438
                const char pp[] = #name;                                \
439
                sym = VCC_MkSym(tl, pp, SYM_MAIN, SYM_ACTION, VCL_LOW,  \
440
                    VCL_HIGH);                                          \
441
                AN(sym);                                                \
442
                sym->action = func;                                     \
443
                sym->action_mask = (mask);                              \
444
        } while (0)
445
446
void
447 71735
vcc_Action_Init(struct vcc *tl)
448
{
449
        struct symbol *sym;
450
451 71735
        ACT(ban,        vcc_act_ban,    0);
452 71735
        ACT(call,       vcc_act_call,   0);
453 71735
        ACT(hash_data,  vcc_act_hash_data,
454
                VCL_MET_HASH);
455 71735
        ACT(if,         vcc_Act_If,     0);
456 71735
        ACT(new,        vcc_Act_New,
457
                VCL_MET_INIT);
458 71735
        ACT(return,     vcc_act_return, 0);
459 71735
        ACT(set,        vcc_act_set,    0);
460 71735
        ACT(synthetic,  vcc_act_synthetic,
461
                VCL_MET_SYNTH | VCL_MET_BACKEND_ERROR);
462 71735
        ACT(unset,      vcc_act_unset,  0);
463 71735
}