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