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