varnish-cache/lib/libvcc/vcc_parse.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
 * 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
 *    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
 * 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
 * SUCH DAMAGE.
29
 */
30
31
#include "config.h"
32
33
#include <string.h>
34
35
#include "vcc_compile.h"
36
37
/*--------------------------------------------------------------------*/
38
39
static void vcc_Compound(struct vcc *tl);
40
41
/*--------------------------------------------------------------------*/
42
43
#define L(tl, foo)      do {    \
44
        tl->indent += INDENT;   \
45
        foo;                    \
46
        tl->indent -= INDENT;   \
47
} while (0)
48
49
#define C(tl, sep)      do {                                            \
50
        Fb(tl, 1, "VPI_count(ctx, VGC_NREFS, %u)%s\n", ++tl->cnt, sep); \
51
        Fb(tl, 1, "if (UNLIKELY(ctx->vpi->trace)) VPI_trace(ctx, %u)%s\n", \
52
           tl->cnt, sep);                                               \
53
        tl->t->cnt = tl->cnt;                                           \
54
} while (0)
55
56
/*--------------------------------------------------------------------
57
 * SYNTAX:
58
 *    Conditional:
59
 *      '(' Cond_0 ')'
60
 */
61
62
static void
63 461148
vcc_Conditional(struct vcc *tl)
64
{
65
66 461148
        SkipToken(tl, '(');
67 461148
        Fb(tl, 0, "(\n");
68 461148
        L(tl, vcc_Expr(tl, BOOL));
69 461148
        ERRCHK(tl);
70 460764
        Fb(tl, 1, ")\n");
71 460764
        SkipToken(tl, ')');
72 461148
}
73
74
/*--------------------------------------------------------------------
75
 * SYNTAX:
76
 *    IfStmt:
77
 *      'if' Conditional  Compound Branch1* Branch2
78
 *    Branch1:
79
 *      'elseif' Conditional Compound
80
 *    Branch2:
81
 *      'else' Compound
82
 *      null
83
 */
84
85
void v_matchproto_(sym_act_f)
86 459576
vcc_Act_If(struct vcc *tl, struct token *t, struct symbol *sym)
87
{
88
89 459576
        (void)t;
90 459576
        (void)sym;
91 459576
        Fb(tl, 1, "if ");
92 459576
        vcc_Conditional(tl);
93 459576
        ERRCHK(tl);
94 459180
        L(tl, vcc_Compound(tl));
95 459180
        ERRCHK(tl);
96 460752
        while (tl->t->tok == ID) {
97 166968
                if (vcc_IdIs(tl->t, "else")) {
98 33948
                        vcc_NextToken(tl);
99 33948
                        if (tl->t->tok == '{') {
100 33048
                                Fb(tl, 1, "else\n");
101 33048
                                L(tl, vcc_Compound(tl));
102 33048
                                ERRCHK(tl);
103 33048
                                return;
104
                        }
105 900
                        if (tl->t->tok != ID || !vcc_IdIs(tl->t, "if")) {
106 0
                                VSB_printf(tl->sb,
107
                                    "'else' must be followed by 'if' or '{'\n");
108 0
                                vcc_ErrWhere(tl, tl->t);
109 0
                                return;
110
                        }
111 900
                        Fb(tl, 1, "else if ");
112 900
                        vcc_NextToken(tl);
113 900
                        vcc_Conditional(tl);
114 900
                        ERRCHK(tl);
115 900
                        L(tl, vcc_Compound(tl));
116 900
                        ERRCHK(tl);
117 266304
                } else if (vcc_IdIs(tl->t, "elseif") ||
118 132996
                     vcc_IdIs(tl->t, "elsif") ||
119 132384
                     vcc_IdIs(tl->t, "elif")) {
120 672
                        Fb(tl, 1, "else if ");
121 672
                        vcc_NextToken(tl);
122 672
                        vcc_Conditional(tl);
123 672
                        ERRCHK(tl);
124 672
                        L(tl, vcc_Compound(tl));
125 672
                        ERRCHK(tl);
126 672
                } else {
127 132348
                        break;
128
                }
129
        }
130 426132
        C(tl, ";");
131 459576
}
132
133
/*--------------------------------------------------------------------
134
 * SYNTAX:
135
 *    Compound:
136
 *      '{' Stmt* '}'
137
 *
138
 *    Stmt:
139
 *      Compound
140
 *      IfStmt
141
 *      CSRC
142
 *      Id(Action) (XXX)
143
 */
144
145
static void
146 1640808
vcc_Compound(struct vcc *tl)
147
{
148
        struct symbol *sym;
149
        struct token *t;
150
151 1640808
        SkipToken(tl, '{');
152 1640808
        Fb(tl, 1, "{\n");
153 1640808
        tl->indent += INDENT;
154 1640808
        C(tl, ";");
155 1640808
        Fb(tl, 1, "END_;\n");
156 4055028
        while (1) {
157 4055028
                ERRCHK(tl);
158 4053660
                t = tl->t;
159 4053660
                switch (tl->t->tok) {
160
                case '{':
161 24
                        vcc_Compound(tl);
162 24
                        break;
163
                case '}':
164 1639380
                        vcc_NextToken(tl);
165 1639380
                        tl->indent -= INDENT;
166 1639380
                        Fb(tl, 1, "}\n");
167 1639380
                        return;
168
                case CSRC:
169 180
                        if (tl->allow_inline_c) {
170 336
                                Fb(tl, 1, "%.*s\n",
171 168
                                    (int) (tl->t->e - (tl->t->b + 2)),
172 168
                                    tl->t->b + 1);
173 168
                                vcc_NextToken(tl);
174 168
                        } else {
175 12
                                VSB_printf(tl->sb,
176
                                    "Inline-C not allowed\n");
177 12
                                vcc_ErrWhere(tl, tl->t);
178
                        }
179 180
                        break;
180
                case EOI:
181 0
                        VSB_printf(tl->sb,
182
                            "End of input while in compound statement\n");
183 0
                        tl->err = 1;
184 0
                        return;
185
                case ID:
186 2414076
                        sym = VCC_SymbolGet(tl, SYM_MAIN, SYM_NONE,
187
                            SYMTAB_PARTIAL, XREF_NONE);
188 2414076
                        if (sym == NULL) {
189 48
                                VSB_printf(tl->sb, "Symbol not found.\n");
190 48
                                vcc_ErrWhere(tl, tl->t);
191 48
                                return;
192
                        }
193 2414028
                        if (sym->action == NULL) {
194 12
                                VSB_printf(tl->sb,
195
                                    "Symbol cannot be used here.\n");
196 12
                                vcc_ErrWhere(tl, tl->t);
197 12
                                return;
198
                        }
199 2414016
                        if (sym->action_mask != 0)
200 99348
                                vcc_AddUses(tl, t, NULL, sym, XREF_ACTION);
201 2414016
                        sym->action(tl, t, sym);
202 2414016
                        break;
203
                default:
204
                        /* We deliberately do not mention inline C */
205 0
                        VSB_printf(tl->sb,
206
                            "Expected an action, 'if', '{' or '}'\n");
207 0
                        vcc_ErrWhere(tl, tl->t);
208 0
                        return;
209
                }
210 2414220
                Fb(tl, 1, "END_;\n");
211
        }
212 1640808
}
213
214
/*--------------------------------------------------------------------
215
 * SYNTAX:
216
 *    Function:
217
 *      'sub' ID(name) Compound
218
 */
219
220
static void
221 1147032
vcc_ParseFunction(struct vcc *tl)
222
{
223
        struct symbol *sym, *bsym;
224
        struct token *t;
225
        struct proc *p;
226
227 1147032
        vcc_NextToken(tl);
228 1147032
        vcc_ExpectVid(tl, "subroutine");
229 1147032
        ERRCHK(tl);
230
231 1147020
        t = tl->t;
232 1147020
        sym = VCC_SymbolGet(tl, SYM_MAIN, SYM_SUB, SYMTAB_CREATE, XREF_DEF);
233 1147020
        ERRCHK(tl);
234 1147008
        AN(sym);
235
236 1147008
        if (vcc_builtin != NULL) {
237 534228
                vcc_builtin->t = t;
238 534228
                bsym = VCC_SymbolGet(vcc_builtin, SYM_MAIN, SYM_SUB,
239
                    SYMTAB_NOERR, XREF_NONE);
240 534228
                AZ(vcc_builtin->err);
241 534228
        }
242
        else
243 612780
                bsym = NULL;
244
245 1147008
        p = sym->proc;
246 1147008
        if (p == NULL) {
247 677544
                if (vcc_builtin != NULL && bsym == NULL &&
248 924
                    vcc_Has_vcl_prefix(t->b)) {
249 12
                        VSB_printf(tl->sb,"The names 'vcl_*'"
250
                            " are reserved for subroutines.\n");
251 12
                        vcc_ErrWhere(tl, t);
252 12
                        VSB_printf(tl->sb, "Valid vcl_* subroutines are:\n");
253 432
                        VTAILQ_FOREACH(p, &vcc_builtin->procs, list) {
254 420
                                t = p->name;
255 840
                                VSB_printf(tl->sb, "\t%.*s\n",
256 420
                                    (int)pdiff(t->b, t->e), t->b);
257 420
                        }
258 12
                        return;
259
                }
260 677532
                VCC_GlobalSymbol(sym, SUB);
261 677532
                p = vcc_NewProc(tl, sym);
262 677532
                p->name = t;
263 677532
                VSB_printf(p->cname, "%s", sym->lname);
264 1146996
        } else if (p->method == NULL && bsym == NULL) {
265 12
                VSB_printf(tl->sb, "Subroutine '%s' redefined\n", sym->name);
266 12
                vcc_ErrWhere(tl, t);
267 12
                VSB_printf(tl->sb, "Previously defined here:\n");
268 12
                vcc_ErrWhere(tl, p->name);
269 12
                return;
270
        } else {
271
                /* Add to VCL sub */
272 469452
                if (p->name == NULL)
273 452544
                        p->name = t;
274
        }
275 1146984
        CHECK_OBJ_NOTNULL(p, PROC_MAGIC);
276 1146984
        tl->fb = p->body;
277 1146984
        Fb(tl, 1, "  /* ... from ");
278 1146984
        vcc_Coord(tl, p->body, NULL);
279 1146984
        Fb(tl, 0, " */\n");
280 1146984
        tl->curproc = p;
281 1146984
        tl->indent += INDENT;
282 1146984
        Fb(tl, 1, "{\n");
283 1146984
        L(tl, vcc_Compound(tl));
284 1146984
        Fb(tl, 1, "}\n");
285 1146984
        tl->indent -= INDENT;
286 1146984
        tl->fb = NULL;
287 1146984
        tl->curproc = NULL;
288 1147032
}
289
290
/*--------------------------------------------------------------------
291
 */
292
293
static void
294 49380
vcc_ParseVcl(struct vcc *tl)
295
{
296
        struct token *tok0;
297
        int syntax;
298
299 49380
        assert(vcc_IdIs(tl->t, "vcl"));
300 49380
        tok0 = tl->t;
301 49380
        vcc_NextToken(tl);
302
303 49380
        Expect(tl, FNUM);
304 49380
        if (tl->t->e - tl->t->b != 3 || tl->t->b[1] != '.') {
305 36
                VSB_cat(tl->sb,
306
                    "Don't play silly buggers with VCL version numbers\n");
307 36
                vcc_ErrWhere(tl, tl->t);
308 36
                ERRCHK(tl);
309 0
        }
310 49344
        syntax = (tl->t->b[0] - '0') * 10 + (tl->t->b[2] - '0');
311 49344
        vcc_NextToken(tl);
312
313 49344
        if (syntax < VCL_LOW || syntax > VCL_HIGH) {
314 48
                VSB_printf(tl->sb, "VCL version %.1f not supported.\n",
315 24
                    .1 * syntax);
316 24
                vcc_ErrWhere2(tl, tok0, tl->t);
317 24
                ERRCHK(tl);
318 0
        }
319
320 49320
        if (tl->t->tok != ';') {
321
                /* Special handling, because next token might be 'vcl'
322
                 * in the built-in VCL, and that would give a very
323
                 * confusing error message
324
                 */
325 12
                VSB_cat(tl->sb, "Expected 'vcl N.N;' found no semi-colon\n");
326 12
                vcc_ErrWhere2(tl, tok0, tl->t);
327 12
                ERRCHK(tl);
328 0
        }
329 49308
        vcc_NextToken(tl);
330 49308
        if (tl->syntax == 0)
331 34560
                tl->syntax = syntax;
332 49308
        if (syntax > tl->syntax) {
333 0
                VSB_printf(tl->sb,
334
                    "VCL version %.1f higher than"
335
                    " the top level version %.1f\n",
336 0
                    .1 * syntax, .1 * tl->syntax);
337 0
                vcc_ErrWhere2(tl, tok0, tl->t);
338 0
                ERRCHK(tl);
339 0
        }
340 49380
}
341
342
/*--------------------------------------------------------------------
343
 * Top level of parser, recognize:
344
 *      Inline C-code
345
 *      ACL definitions
346
 *      Function definitions
347
 *      Backend definitions
348
 *      VMOD import directives
349
 *      VCL version declarations
350
 *      End of input
351
 */
352
353
typedef void parse_f(struct vcc *tl);
354
355
static struct toplev {
356
        const char      *name;
357
        parse_f         *func;
358
        unsigned        vcllo;
359
        unsigned        vclhi;
360
} toplev[] = {
361
        { "acl",                vcc_ParseAcl,           VCL_41, VCL_HIGH },
362
        { "sub",                vcc_ParseFunction,      VCL_41, VCL_HIGH },
363
        { "backend",            vcc_ParseBackend,       VCL_41, VCL_HIGH },
364
        { "probe",              vcc_ParseProbe,         VCL_41, VCL_HIGH },
365
        { "import",             vcc_ParseImport,        VCL_41, VCL_HIGH },
366
        { "vcl",                vcc_ParseVcl,           VCL_41, VCL_HIGH },
367
        { NULL, NULL }
368
};
369
370
void
371 34620
vcc_Parse(struct vcc *tl)
372
{
373
        struct toplev *tp;
374
375 34620
        AZ(tl->indent);
376 34620
        if (tl->t->tok != ID || !vcc_IdIs(tl->t, "vcl")) {
377 12
                VSB_cat(tl->sb,
378
                    "VCL version declaration missing\n"
379
                    "Update your VCL to Version 4 syntax, and add\n"
380
                    "\tvcl 4.1;\n"
381
                    "on the first line of the VCL files.\n"
382
                );
383 12
                vcc_ErrWhere(tl, tl->t);
384 12
                ERRCHK(tl);
385 0
        }
386 34608
        vcc_ParseVcl(tl);
387 34608
        ERRCHK(tl);
388 34560
        AN(tl->syntax);
389 1223112
        while (tl->t->tok != EOI) {
390 1190892
                ERRCHK(tl);
391 1188576
                switch (tl->t->tok) {
392
                case CSRC:
393 108
                        if (tl->allow_inline_c) {
394 192
                                Fc(tl, 0, "%.*s\n",
395 96
                                    (int) (tl->t->e - (tl->t->b + 4)),
396 96
                                    tl->t->b + 2);
397 96
                                vcc_NextToken(tl);
398 96
                        } else {
399 12
                                VSB_cat(tl->sb, "Inline-C not allowed\n");
400 12
                                vcc_ErrWhere(tl, tl->t);
401
                        }
402 108
                        break;
403
                case EOI:
404 0
                        break;
405
                case ID:
406 2475636
                        for (tp = toplev; tp->name != NULL; tp++) {
407 2475624
                                if (tp->func == NULL)
408 0
                                        continue;
409 2475624
                                if (!vcc_IdIs(tl->t, tp->name))
410 1287180
                                        continue;
411 1188444
                                tp->func(tl);
412 1188444
                                break;
413
                        }
414 1188456
                        if (tp->name != NULL)
415 1188444
                                break;
416
                        /* FALLTHROUGH */
417
                default:
418
                        /* We deliberately do not mention inline-C */
419 24
                        VSB_cat(tl->sb, "Expected one of\n\t");
420 168
                        for (tp = toplev; tp->name != NULL; tp++) {
421 144
                                if (tp[1].name == NULL)
422 24
                                        VSB_cat(tl->sb, " or ");
423 144
                                VSB_printf(tl->sb, "'%s'", tp->name);
424 144
                                if (tp[1].name != NULL)
425 120
                                        VSB_cat(tl->sb, ", ");
426 144
                        }
427 24
                        VSB_cat(tl->sb, "\nFound: ");
428 24
                        vcc_ErrToken(tl, tl->t);
429 24
                        VSB_cat(tl->sb, " at\n");
430 24
                        vcc_ErrWhere(tl, tl->t);
431 24
                        return;
432
                }
433
        }
434 32220
        AZ(tl->indent);
435 34620
}
436
437
void
438 35004
vcc_Parse_Init(struct vcc *tl)
439
{
440
        struct toplev *tp;
441
442 245028
        for (tp = toplev; tp->name != NULL; tp++)
443 210024
                AN(VCC_MkSym(tl, tp->name, SYM_MAIN, SYM_RESERVED,
444
                    tp->vcllo, tp->vclhi));
445 35004
}