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 775294
 * 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 775294
 *    notice, this list of conditions and the following disclaimer in the
16
 *    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
 * 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 775294
 * 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 775294
 * 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
#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 1614808
vcc_act_call(struct vcc *tl, struct token *t, struct symbol *sym)
45
{
46
        struct token *t0;
47
        unsigned u;
48
49 839514
        (void)t;
50 1614808
        ExpectErr(tl, ID);
51 839514
        t0 = tl->t;
52 839514
        sym = VCC_SymbolGet(tl, SYM_MAIN, SYM_NONE, SYMTAB_PARTIAL_NOERR,
53
            XREF_REF);
54 1614808
        if (sym == NULL) {
55 733044
                sym = VCC_SymbolGet(tl, SYM_MAIN, SYM_SUB, SYMTAB_CREATE,
56
                    XREF_REF);
57 733044
                if (sym == NULL)
58 775294
                        return;
59 733044
                VCC_GlobalSymbol(sym, SUB);
60 733044
        }
61
62 839514
        if (sym->kind == SYM_SUB) {
63 1614769
                vcc_AddCall(tl, t0, sym);
64
65 839475
                Fb(tl, 1, "%s(ctx, VSUB_STATIC, NULL);\n", sym->lname);
66 839475
                SkipToken(tl, ';');
67 839475
                return;
68
        }
69
70 775333
        tl->t = t0;
71 39
        u = tl->unique++;
72
73 39
        Fb(tl, 1, "{\n");
74 39
        tl->indent += INDENT;
75 775333
        Fb(tl, 2, "VCL_SUB call_%u =\n", u);
76 39
        tl->indent += INDENT;
77 39
        vcc_Expr(tl, SUB);
78 39
        Fb(tl, 2, ";\n\n");
79 775333
        SkipToken(tl, ';');
80 26
        tl->indent -= INDENT;
81 26
        Fb(tl, 2, "if (call_%u == NULL) {\n", u);
82 26
        Fb(tl, 2, "  VRT_fail(ctx, \"Tried to call NULL SUB near"
83
           " source %%u line %%u\",\n");
84 26
        Fb(tl, 2, "    VGC_ref[%u].source,\n", tl->cnt);
85 26
        Fb(tl, 2, "    VGC_ref[%u].line);\n", tl->cnt);
86 26
        Fb(tl, 2, "  END_;\n");
87 26
        Fb(tl, 2, "}\n");
88 26
        Fb(tl, 2, "call_%u->func(ctx, VSUB_STATIC, NULL);\n", u);
89 775320
        tl->indent -= INDENT;
90 26
        Fb(tl, 1, "}\n");
91 839514
}
92
93
/*--------------------------------------------------------------------*/
94 775294
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 775294
        { 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
        { 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
        { 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 348673
vcc_assign_expr(struct vcc *tl, struct symbol *sym, const struct assign *ap)
130
{
131
        const char *e;
132 348673
        unsigned indent = 1;
133
134 348673
        e = ap->expr;
135 348673
        if (e == NULL)
136 78286
                return;
137
138 1572740
        while (*e != '\0') {
139 1302353
                if (*e == '\v')
140 182
                        Fb(tl, indent, "%s", sym->rname);
141
                else
142 1302171
                        Fb(tl, indent, "%c", *e);
143 1302353
                indent = 0;
144 1302353
                e++;
145
        }
146 348673
}
147
148
/*--------------------------------------------------------------------*/
149
150
static void v_matchproto_(sym_act_f)
151 348829
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 348829
        (void)t;
157 348829
        ExpectErr(tl, ID);
158 348829
        t = tl->t;
159 348829
        sym = VCC_SymbolGet(tl, SYM_MAIN, SYM_VAR, SYMTAB_EXISTING, XREF_NONE);
160 348829
        ERRCHK(tl);
161 348725
        AN(sym);
162 348725
        vcc_AddUses(tl, t, tl->t, sym, XREF_WRITE);
163 348725
        ERRCHK(tl);
164 348725
        type = sym->type;
165 7664631
        for (ap = assign; ap->type != VOID; ap++) {
166 7623980
                if (ap->type != type)
167 6964880
                        continue;
168 659100
                if (ap->oper != tl->t->tok)
169 351026
                        continue;
170 308074
                vcc_NextToken(tl);
171 308074
                type = ap->want;
172 308074
                break;
173
        }
174
175 348725
        if (ap->type == VOID)
176 40651
                SkipToken(tl, ap->oper);
177
178 348673
        Fb(tl, 1, "%s\n", sym->lname);
179 348673
        tl->indent += INDENT;
180 348673
        vcc_assign_expr(tl, sym, ap);
181 348673
        vcc_Expr(tl, type);
182 348673
        ERRCHK(tl);
183 348127
        tl->indent -= INDENT;
184 348127
        Fb(tl, 1, ");\n");
185 348127
        SkipToken(tl, ';');
186 348829
}
187
188
/*--------------------------------------------------------------------*/
189
190
static void v_matchproto_(sym_act_f)
191 35542
vcc_act_unset(struct vcc *tl, struct token *t, struct symbol *sym)
192
{
193
194
        /* XXX: Wrong, should use VCC_Expr(HEADER) */
195 35542
        ExpectErr(tl, ID);
196 35542
        t = tl->t;
197 35542
        sym = VCC_SymbolGet(tl, SYM_MAIN, SYM_VAR, SYMTAB_EXISTING, XREF_NONE);
198 35542
        ERRCHK(tl);
199 35542
        AN(sym);
200 35542
        if (sym->u_methods == 0) {
201 26
                vcc_ErrWhere2(tl, t, tl->t);
202 26
                VSB_cat(tl->sb, "Variable cannot be unset.\n");
203 26
                return;
204
        }
205 35516
        vcc_AddUses(tl, t, tl->t, sym, XREF_UNSET);
206 35516
        Fb(tl, 1, "%s;\n", sym->uname);
207 35516
        SkipToken(tl, ';');
208 35542
}
209
210
/*--------------------------------------------------------------------*/
211
212
static void v_matchproto_(sym_act_f)
213 195
vcc_act_ban(struct vcc *tl, struct token *t, struct symbol *sym)
214
{
215
216 195
        (void)t;
217 195
        (void)sym;
218
219 195
        SkipToken(tl, '(');
220
221 195
        Fb(tl, 1, "(void) VRT_ban_string(ctx, \n");
222 195
        tl->indent += INDENT;
223 195
        vcc_Expr(tl, STRING);
224 195
        tl->indent -= INDENT;
225 195
        ERRCHK(tl);
226 182
        Fb(tl, 1, ");\n");
227
228 182
        SkipToken(tl, ')');
229 182
        SkipToken(tl, ';');
230 195
}
231
232
/*--------------------------------------------------------------------*/
233
234
static void v_matchproto_(sym_act_f)
235 104858
vcc_act_hash_data(struct vcc *tl, struct token *t, struct symbol *sym)
236
{
237
238 104858
        (void)t;
239 104858
        (void)sym;
240 104858
        SkipToken(tl, '(');
241
242 104858
        Fb(tl, 1, "VRT_hashdata(ctx,\n  ");
243 104858
        vcc_Expr(tl, STRANDS);
244 104858
        ERRCHK(tl);
245 104845
        Fb(tl, 1, ");\n");
246 104845
        SkipToken(tl, ')');
247 104845
        SkipToken(tl, ';');
248 104858
}
249
250
/*--------------------------------------------------------------------*/
251
252
static void
253 91
vcc_act_return_pass(struct vcc *tl)
254
{
255
256 91
        SkipToken(tl, '(');
257 91
        Fb(tl, 1, "VRT_hit_for_pass(ctx,\n");
258 91
        tl->indent += INDENT;
259 91
        vcc_Expr(tl, DURATION);
260 91
        ERRCHK(tl);
261 91
        SkipToken(tl, ')');
262 91
        Fb(tl, 1, ");\n");
263 91
        tl->indent -= INDENT;
264 91
}
265
/*--------------------------------------------------------------------*/
266
267
static void
268 650
vcc_act_return_fail(struct vcc *tl)
269
{
270 650
        SkipToken(tl, '(');
271 650
        Fb(tl, 1, "VRT_fail(ctx,\n");
272 650
        tl->indent += INDENT;
273 650
        vcc_Expr(tl, STRING);
274 650
        tl->indent -= INDENT;
275 650
        ERRCHK(tl);
276 650
        SkipToken(tl, ')');
277 650
        Fb(tl, 1, ");\n");
278 650
}
279
280
/*--------------------------------------------------------------------*/
281
282
static void
283 107107
vcc_act_return_synth(struct vcc *tl)
284
{
285
286 107107
        SkipToken(tl, '(');
287 107107
        Fb(tl, 1, "VRT_synth(ctx,\n");
288 107107
        tl->indent += INDENT;
289 107107
        vcc_Expr(tl, INT);
290 107107
        ERRCHK(tl);
291 107107
        Fb(tl, 1, ",\n");
292 107107
        if (tl->t->tok == ',') {
293 35503
                vcc_NextToken(tl);
294 35503
                vcc_Expr(tl, STRING);
295 35503
                ERRCHK(tl);
296 35503
        } else {
297 71604
                Fb(tl, 1, "(const char*)0\n");
298
        }
299 107107
        tl->indent -= INDENT;
300 107107
        SkipToken(tl, ')');
301 107107
        Fb(tl, 1, ");\n");
302 107107
}
303
304
/*--------------------------------------------------------------------*/
305
306
static void
307 312
vcc_act_return_vcl(struct vcc *tl)
308
{
309
        struct symbol *sym;
310
        struct inifin *p;
311
        char buf[1024];
312
313 312
        SkipToken(tl, '(');
314 312
        ExpectErr(tl, ID);
315 312
        sym = VCC_SymbolGet(tl, SYM_MAIN, SYM_VCL, SYMTAB_EXISTING, XREF_NONE);
316 312
        ERRCHK(tl);
317 286
        AN(sym);
318 286
        if (sym->eval_priv == NULL) {
319 273
                VSB_cat(tl->symtab, ",\n    {\n");
320 273
                VSB_cat(tl->symtab, "\t\"dir\": \"import\",\n");
321 273
                VSB_cat(tl->symtab, "\t\"type\": \"$VCL\",\n");
322 273
                VSB_printf(tl->symtab, "\t\"name\": \"%s\"\n", sym->name);
323 273
                VSB_cat(tl->symtab, "   }");
324
325 273
                bprintf(buf, "vgc_vcl_%u", tl->unique++);
326 273
                sym->eval_priv = strdup(buf);
327 273
                AN(sym->eval_priv);
328
329 273
                Fh(tl, 0, "static VCL_VCL %s;", buf);
330 273
                Fh(tl, 0, "\t/* VCL %s */\n", sym->name);
331
332 273
                p = New_IniFin(tl);
333 273
                AN(p);
334 546
                VSB_printf(p->ini, "\t%s = VPI_vcl_get(ctx, \"%s\");",
335 273
                    buf, sym->name);
336 546
                VSB_printf(p->fin, "\tVPI_vcl_rel(ctx, %s);",
337 273
                    buf);
338 273
        }
339 572
        Fb(tl, 1, "VPI_vcl_select(ctx, %s);\t/* %s */\n",
340 286
            (const char*)sym->eval_priv, sym->name);
341 286
        SkipToken(tl, ')');
342 312
}
343
344
/*--------------------------------------------------------------------*/
345
346
static void v_matchproto_(sym_act_f)
347 775359
vcc_act_return(struct vcc *tl, struct token *t, struct symbol *sym)
348
{
349
        unsigned hand, mask;
350
        const char *h;
351
352 775359
        (void)t;
353 775359
        (void)sym;
354 775359
        AN(tl->curproc);
355 775359
        if (tl->t->tok == ';' && tl->curproc->method == NULL) {
356 52
                SkipToken(tl, ';');
357 52
                Fb(tl, 1, "return;\n");
358 52
                return;
359
        }
360 775307
        SkipToken(tl, '(');
361 775294
        ExpectErr(tl, ID);
362
363 775294
        hand = VCL_RET_MAX;
364 775294
        h = NULL;
365 775294
        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 775294
        if (h == NULL) {
374 13
                VSB_cat(tl->sb, "Expected return action name.\n");
375 13
                vcc_ErrWhere(tl, tl->t);
376 13
                ERRCHK(tl);
377 0
        }
378 775281
        assert(hand < VCL_RET_MAX);
379 775281
        AN(mask);
380
381 775281
        vcc_ProcAction(tl->curproc, hand, mask, tl->t);
382 775281
        vcc_NextToken(tl);
383 775281
        if (tl->t->tok == '(') {
384 108173
                if (hand == VCL_RET_SYNTH || hand == VCL_RET_ERROR)
385 107107
                        vcc_act_return_synth(tl);
386 1066
                else if (hand == VCL_RET_VCL)
387 312
                        vcc_act_return_vcl(tl);
388 754
                else if (hand == VCL_RET_PASS)
389 91
                        vcc_act_return_pass(tl);
390 663
                else if (hand == VCL_RET_FAIL)
391 650
                        vcc_act_return_fail(tl);
392
                else {
393 13
                        VSB_cat(tl->sb, "Arguments not allowed.\n");
394 13
                        vcc_ErrWhere(tl, tl->t);
395
                }
396 108173
        } else {
397 667108
                if (hand == VCL_RET_SYNTH || hand == VCL_RET_VCL) {
398 13
                        VSB_cat(tl->sb, "Missing argument.\n");
399 13
                        vcc_ErrWhere(tl, tl->t);
400 13
                }
401
        }
402 775281
        ERRCHK(tl);
403 775229
        Fb(tl, 1, "END_;\n");
404 775229
        if (hand == VCL_RET_FAIL)
405 923
                Fb(tl, 1, "VRT_fail(ctx, \"Failed from VCL\");\n");
406
        else
407 774306
                Fb(tl, 1, "VRT_handling(ctx, VCL_RET_%s);\n", h);
408 775229
        SkipToken(tl, ')');
409 775229
        SkipToken(tl, ';');
410 775359
}
411
412
/*--------------------------------------------------------------------*/
413
414
static void v_matchproto_(sym_act_f)
415 78
vcc_act_synthetic(struct vcc *tl, struct token *t, struct symbol *sym)
416
{
417
418 78
        (void)t;
419 78
        (void)sym;
420 78
        ExpectErr(tl, '(');
421 78
        ERRCHK(tl);
422 78
        vcc_NextToken(tl);
423
424 78
        Fb(tl, 1, "VRT_synth_strands(ctx, ");
425 78
        vcc_Expr(tl, STRANDS);
426 78
        ERRCHK(tl);
427 65
        Fb(tl, 1, ");\n");
428
429 65
        SkipToken(tl, ')');
430 65
        SkipToken(tl, ';');
431 78
}
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 37921
vcc_Action_Init(struct vcc *tl)
448
{
449
        struct symbol *sym;
450
451 37921
        ACT(ban,        vcc_act_ban,    0);
452 37921
        ACT(call,       vcc_act_call,   0);
453 37921
        ACT(hash_data,  vcc_act_hash_data,
454
                VCL_MET_HASH);
455 37921
        ACT(if,         vcc_Act_If,     0);
456 37921
        ACT(new,        vcc_Act_New,
457
                VCL_MET_INIT);
458 37921
        ACT(return,     vcc_act_return, 0);
459 37921
        ACT(set,        vcc_act_set,    0);
460 37921
        ACT(synthetic,  vcc_act_synthetic,
461
                VCL_MET_SYNTH | VCL_MET_BACKEND_ERROR);
462 37921
        ACT(unset,      vcc_act_unset,  0);
463 37921
}