varnish-cache/lib/libvarnishapi/vxp_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: Martin Blix Grydeland <martin@varnish-software.com>
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
32
#include "config.h"
33
34
#include <ctype.h>
35
#include <math.h>
36
#include <stdio.h>
37
38
#include "vdef.h"
39
#include "vas.h"
40
#include "miniobj.h"
41
42
#include "vbm.h"
43
#include "vqueue.h"
44
#include "vre.h"
45
#include "vsb.h"
46
47
#include "vapi/vsl.h"
48
49
#include "vsl_api.h"
50
#include "vxp.h"
51
52
static void vxp_expr_or(struct vxp *vxp, struct vex **pvex);
53
54
static struct vex *
55 2912
vex_alloc(const struct vxp *vxp)
56
{
57
        struct vex *vex;
58
59 2912
        ALLOC_OBJ(vex, VEX_MAGIC);
60 2912
        AN(vex);
61 2912
        vex->options = vxp->vex_options;
62 2912
        return (vex);
63
}
64
65
static void
66 2626
vxp_expr_lhs(struct vxp *vxp, struct vex_lhs **plhs)
67
{
68
        char *p;
69
        int i;
70
71 2626
        AN(plhs);
72 2626
        AZ(*plhs);
73 2626
        ALLOC_OBJ(*plhs, VEX_LHS_MAGIC);
74 2626
        AN(*plhs);
75 2626
        (*plhs)->tags = vbit_new(SLT__MAX);
76 2626
        (*plhs)->level = -1;
77
78 2626
        if (vxp->t->tok == '{') {
79
                /* Transaction level limits */
80 91
                vxp_NextToken(vxp);
81 91
                if (vxp->t->tok != VAL) {
82 26
                        VSB_printf(vxp->sb, "Expected integer got '%.*s' ",
83 13
                            PF(vxp->t));
84 13
                        vxp_ErrWhere(vxp, vxp->t, -1);
85 13
                        return;
86
                }
87 78
                (*plhs)->level = (int)strtol(vxp->t->dec, &p, 0);
88 78
                if ((*plhs)->level < 0) {
89 13
                        VSB_cat(vxp->sb, "Expected positive integer ");
90 13
                        vxp_ErrWhere(vxp, vxp->t, -1);
91 13
                        return;
92
                }
93 65
                if (*p == '-') {
94 13
                        (*plhs)->level_pm = -1;
95 13
                        p++;
96 65
                } else if (*p == '+') {
97 13
                        (*plhs)->level_pm = 1;
98 13
                        p++;
99 13
                }
100 65
                if (*p) {
101 13
                        VSB_cat(vxp->sb, "Syntax error in level limit ");
102 13
                        vxp_ErrWhere(vxp, vxp->t, -1);
103 13
                        return;
104
                }
105 52
                vxp_NextToken(vxp);
106 52
                ExpectErr(vxp, '}');
107 52
                vxp_NextToken(vxp);
108 52
        }
109
110 2639
        while (1) {
111
                /* The tags this expression applies to */
112 2639
                if (vxp->t->tok == VXID) {
113 390
                        (*plhs)->vxid++;
114 390
                        i = 0;
115 2639
                } else if (vxp->t->tok != VAL) {
116 26
                        VSB_printf(vxp->sb, "Expected VSL tag name got '%.*s' ",
117 13
                            PF(vxp->t));
118 13
                        vxp_ErrWhere(vxp, vxp->t, -1);
119 13
                        return;
120
                } else {
121 2236
                        (*plhs)->taglist++;
122 4472
                        i = VSL_Glob2Tags(vxp->t->dec, -1, vsl_vbm_bitset,
123 2236
                            (*plhs)->tags);
124
                }
125 2626
                if (i == -1) {
126 26
                        VSB_cat(vxp->sb, "Tag name matches zero tags ");
127 26
                        vxp_ErrWhere(vxp, vxp->t, -1);
128 26
                        return;
129
                }
130 2600
                if (i == -2) {
131 13
                        VSB_cat(vxp->sb, "Tag name is ambiguous ");
132 13
                        vxp_ErrWhere(vxp, vxp->t, -1);
133 13
                        return;
134
                }
135 2587
                if (i == -3) {
136 13
                        VSB_cat(vxp->sb, "Syntax error in tag name ");
137 13
                        vxp_ErrWhere(vxp, vxp->t, -1);
138 13
                        return;
139
                }
140 2574
                assert(i > 0 || vxp->t->tok == VXID);
141 2574
                vxp_NextToken(vxp);
142 2574
                if (vxp->t->tok != ',')
143 2522
                        break;
144 52
                vxp_NextToken(vxp);
145
        }
146
147 2522
        if (vxp->t->tok == ':') {
148
                /* Record prefix */
149 286
                vxp_NextToken(vxp);
150 286
                if (vxp->t->tok != VAL) {
151 26
                        VSB_printf(vxp->sb, "Expected string got '%.*s' ",
152 13
                            PF(vxp->t));
153 13
                        vxp_ErrWhere(vxp, vxp->t, -1);
154 13
                        return;
155
                }
156 273
                AN(vxp->t->dec);
157 273
                (*plhs)->prefix = strdup(vxp->t->dec);
158 273
                AN((*plhs)->prefix);
159 273
                (*plhs)->prefixlen = strlen((*plhs)->prefix);
160 273
                vxp_NextToken(vxp);
161 273
        }
162
163 2509
        if (vxp->t->tok == '[') {
164
                /* LHS field [] */
165 156
                vxp_NextToken(vxp);
166 156
                if (vxp->t->tok != VAL) {
167 26
                        VSB_printf(vxp->sb, "Expected integer got '%.*s' ",
168 13
                            PF(vxp->t));
169 13
                        vxp_ErrWhere(vxp, vxp->t, -1);
170 13
                        return;
171
                }
172 143
                (*plhs)->field = (int)strtol(vxp->t->dec, &p, 0);
173 143
                if (*p || (*plhs)->field <= 0) {
174 13
                        VSB_cat(vxp->sb, "Expected positive integer ");
175 13
                        vxp_ErrWhere(vxp, vxp->t, -1);
176 13
                        return;
177
                }
178 130
                vxp_NextToken(vxp);
179 130
                ExpectErr(vxp, ']');
180 130
                vxp_NextToken(vxp);
181 130
        }
182
183 2483
        if ((*plhs)->vxid == 0)
184 2106
                return;
185
186 715
        if ((*plhs)->vxid > 1 || (*plhs)->level >= 0 ||
187 351
            (*plhs)->field > 0 || (*plhs)->prefixlen > 0 ||
188 338
            (*plhs)->taglist > 0) {
189 52
                VSB_cat(vxp->sb, "Unexpected taglist selection for vxid ");
190 52
                vxp_ErrWhere(vxp, vxp->t, -1);
191 52
        }
192 2626
}
193
194
static void
195 1001
vxp_expr_num(struct vxp *vxp, struct vex_rhs **prhs, unsigned vxid)
196
{
197
        char *endptr;
198
199 1001
        AN(prhs);
200 1001
        AZ(*prhs);
201 1001
        if (vxp->t->tok != VAL) {
202 13
                VSB_printf(vxp->sb, "Expected number got '%.*s' ", PF(vxp->t));
203 13
                vxp_ErrWhere(vxp, vxp->t, -1);
204 13
                return;
205
        }
206 988
        AN(vxp->t->dec);
207 988
        ALLOC_OBJ(*prhs, VEX_RHS_MAGIC);
208 988
        AN(*prhs);
209 988
        endptr = NULL;
210 988
        if (strchr(vxp->t->dec, '.')) {
211 156
                (*prhs)->type = VEX_FLOAT;
212 156
                (*prhs)->val_float = strtod(vxp->t->dec, &endptr);
213 156
        } else {
214 832
                (*prhs)->type = VEX_INT;
215 832
                (*prhs)->val_int = strtoll(vxp->t->dec, &endptr, 0);
216
        }
217 988
        while (isspace(*endptr))
218 0
                endptr++;
219 988
        if (*endptr != '\0') {
220 52
                VSB_printf(vxp->sb, "%s parse error ",
221 26
                    (*prhs)->type == VEX_FLOAT ? "Floating point" : "Integer");
222 26
                vxp_ErrWhere(vxp, vxp->t, -1);
223 26
                return;
224
        }
225 962
        if (vxid && (*prhs)->type != VEX_INT) {
226 26
                VSB_printf(vxp->sb, "Expected integer got '%.*s' ",
227 13
                    PF(vxp->t));
228 13
                vxp_ErrWhere(vxp, vxp->t, 0);
229 13
                return;
230
        }
231 949
        vxp_NextToken(vxp);
232 1001
}
233
234
static void
235 182
vxp_expr_str(struct vxp *vxp, struct vex_rhs **prhs)
236
{
237
238 182
        AN(prhs);
239 182
        AZ(*prhs);
240 182
        if (vxp->t->tok != VAL) {
241 13
                VSB_printf(vxp->sb, "Expected string got '%.*s' ", PF(vxp->t));
242 13
                vxp_ErrWhere(vxp, vxp->t, -1);
243 13
                return;
244
        }
245 169
        AN(vxp->t->dec);
246 169
        ALLOC_OBJ(*prhs, VEX_RHS_MAGIC);
247 169
        AN(*prhs);
248 169
        (*prhs)->type = VEX_STRING;
249 169
        (*prhs)->val_string = strdup(vxp->t->dec);
250 169
        AN((*prhs)->val_string);
251 169
        (*prhs)->val_stringlen = strlen((*prhs)->val_string);
252 169
        vxp_NextToken(vxp);
253 182
}
254
255
static void
256 741
vxp_expr_regex(struct vxp *vxp, struct vex_rhs **prhs)
257
{
258
        int err, erroff;
259
260
        /* XXX: Caseless option */
261
262 741
        AN(prhs);
263 741
        AZ(*prhs);
264 741
        if (vxp->t->tok != VAL) {
265 26
                VSB_printf(vxp->sb, "Expected regular expression got '%.*s' ",
266 13
                    PF(vxp->t));
267 13
                vxp_ErrWhere(vxp, vxp->t, -1);
268 13
                return;
269
        }
270 728
        AN(vxp->t->dec);
271 728
        ALLOC_OBJ(*prhs, VEX_RHS_MAGIC);
272 728
        AN(*prhs);
273 728
        (*prhs)->type = VEX_REGEX;
274 728
        (*prhs)->val_string = strdup(vxp->t->dec);
275 728
        (*prhs)->val_regex = VRE_compile(vxp->t->dec, vxp->vre_options,
276
            &err, &erroff, 1);
277 728
        if ((*prhs)->val_regex == NULL) {
278 13
                VSB_cat(vxp->sb, "Regular expression error: ");
279 13
                AZ(VRE_error(vxp->sb, err));
280 13
                VSB_putc(vxp->sb, ' ');
281 13
                vxp_ErrWhere(vxp, vxp->t, erroff);
282 13
                return;
283
        }
284 715
        vxp_NextToken(vxp);
285 741
}
286
287
static void
288 325
vxp_vxid_cmp(struct vxp *vxp)
289
{
290
291 325
        switch (vxp->t->tok) {
292
        /* Valid operators */
293
        case T_EQ:              /* == */
294
        case '<':               /* < */
295
        case '>':               /* > */
296
        case T_GEQ:             /* >= */
297
        case T_LEQ:             /* <= */
298
        case T_NEQ:             /* != */
299 273
                break;
300
301
        /* Error */
302
        default:
303 104
                VSB_printf(vxp->sb, "Expected vxid operator got '%.*s' ",
304 52
                    PF(vxp->t));
305 52
                vxp_ErrWhere(vxp, vxp->t, -1);
306 52
        }
307 325
}
308
309
/*
310
 * SYNTAX:
311
 *   expr_cmp:
312
 *     lhs
313
 *     lhs <operator> num|str|regex
314
 */
315
316
static void
317 2626
vxp_expr_cmp(struct vxp *vxp, struct vex **pvex)
318
{
319
320 2626
        AN(pvex);
321 2626
        AZ(*pvex);
322 2626
        *pvex = vex_alloc(vxp);
323 2626
        AN(*pvex);
324 2626
        vxp_expr_lhs(vxp, &(*pvex)->lhs);
325 2626
        ERRCHK(vxp);
326
327 2431
        if ((*pvex)->lhs->vxid) {
328 325
                vxp_vxid_cmp(vxp);
329 325
                ERRCHK(vxp);
330 273
        }
331
332
        /* Test operator */
333 2379
        switch (vxp->t->tok) {
334
335
        /* Single lhs expressions don't take any more tokens */
336
        case EOI:
337
        case T_AND:
338
        case T_OR:
339
        case ')':
340 442
                (*pvex)->tok = T_TRUE;
341 442
                return;
342
343
        /* Valid operators */
344
        case T_EQ:              /* == */
345
        case '<':               /* < */
346
        case '>':               /* > */
347
        case T_GEQ:             /* >= */
348
        case T_LEQ:             /* <= */
349
        case T_NEQ:             /* != */
350
        case T_SEQ:             /* eq */
351
        case T_SNEQ:            /* ne */
352
        case '~':               /* ~ */
353
        case T_NOMATCH:         /* !~ */
354 1924
                (*pvex)->tok = vxp->t->tok;
355 1924
                break;
356
357
        /* Error */
358
        default:
359 26
                VSB_printf(vxp->sb, "Expected operator got '%.*s' ",
360 13
                    PF(vxp->t));
361 13
                vxp_ErrWhere(vxp, vxp->t, -1);
362 13
                return;
363
        }
364 1924
        vxp_NextToken(vxp);
365 1924
        ERRCHK(vxp);
366
367
        /* Value */
368 1924
        switch ((*pvex)->tok) {
369
        case '\0':
370 0
                WRONG("Missing token");
371 0
                break;
372
        case T_EQ:              /* == */
373
        case '<':               /* < */
374
        case '>':               /* > */
375
        case T_GEQ:             /* >= */
376
        case T_LEQ:             /* <= */
377
        case T_NEQ:             /* != */
378 1001
                vxp_expr_num(vxp, &(*pvex)->rhs, (*pvex)->lhs->vxid);
379 1001
                break;
380
        case T_SEQ:             /* eq */
381
        case T_SNEQ:            /* ne */
382 182
                vxp_expr_str(vxp, &(*pvex)->rhs);
383 182
                break;
384
        case '~':               /* ~ */
385
        case T_NOMATCH:         /* !~ */
386 741
                vxp_expr_regex(vxp, &(*pvex)->rhs);
387 741
                break;
388
        default:
389 0
                INCOMPL();
390 0
        }
391 2626
}
392
393
/*
394
 * SYNTAX:
395
 *   expr_group:
396
 *     '(' expr_or ')'
397
 *     expr_not
398
 */
399
400
static void
401 2691
vxp_expr_group(struct vxp *vxp, struct vex **pvex)
402
{
403
404 2691
        AN(pvex);
405 2691
        AZ(*pvex);
406
407 2691
        if (vxp->t->tok == '(') {
408 65
                SkipToken(vxp, '(');
409 65
                vxp_expr_or(vxp, pvex);
410 65
                ERRCHK(vxp);
411 65
                SkipToken(vxp, ')');
412 65
                return;
413
        }
414
415 2626
        vxp_expr_cmp(vxp, pvex);
416 2691
}
417
418
/*
419
 * SYNTAX:
420
 *   expr_not:
421
 *     'not' expr_group
422
 *     expr_group
423
 */
424
425
static void
426 2691
vxp_expr_not(struct vxp *vxp, struct vex **pvex)
427
{
428
429 2691
        AN(pvex);
430 2691
        AZ(*pvex);
431
432 2691
        if (vxp->t->tok == T_NOT) {
433 13
                *pvex = vex_alloc(vxp);
434 13
                AN(*pvex);
435 13
                (*pvex)->tok = vxp->t->tok;
436 13
                vxp_NextToken(vxp);
437 13
                vxp_expr_group(vxp, &(*pvex)->a);
438 13
                return;
439
        }
440
441 2678
        vxp_expr_group(vxp, pvex);
442 2691
}
443
444
/*
445
 * SYNTAX:
446
 *   expr_and:
447
 *     expr_not { 'and' expr_not }*
448
 */
449
450
static void
451 2587
vxp_expr_and(struct vxp *vxp, struct vex **pvex)
452
{
453
        struct vex *a;
454
455 2587
        AN(pvex);
456 2587
        AZ(*pvex);
457 2587
        vxp_expr_not(vxp, pvex);
458 2587
        ERRCHK(vxp);
459 2340
        while (vxp->t->tok == T_AND) {
460 104
                a = *pvex;
461 104
                *pvex = vex_alloc(vxp);
462 104
                AN(*pvex);
463 104
                (*pvex)->tok = vxp->t->tok;
464 104
                (*pvex)->a = a;
465 104
                vxp_NextToken(vxp);
466 104
                ERRCHK(vxp);
467 104
                vxp_expr_not(vxp, &(*pvex)->b);
468 104
                ERRCHK(vxp);
469
        }
470 2587
}
471
472
/*
473
 * SYNTAX:
474
 *   expr_or:
475
 *     expr_and { 'or' expr_and }*
476
 */
477
478
static void
479 2496
vxp_expr_or(struct vxp *vxp, struct vex **pvex)
480
{
481
        struct vex *a;
482
483 2496
        AN(pvex);
484 2496
        AZ(*pvex);
485 2496
        vxp_expr_and(vxp, pvex);
486 2496
        ERRCHK(vxp);
487 2236
        while (vxp->t->tok == T_OR) {
488 91
                a = *pvex;
489 91
                *pvex = vex_alloc(vxp);
490 91
                AN(*pvex);
491 91
                (*pvex)->tok = vxp->t->tok;
492 91
                (*pvex)->a = a;
493 91
                vxp_NextToken(vxp);
494 91
                ERRCHK(vxp);
495 91
                vxp_expr_and(vxp, &(*pvex)->b);
496 91
                ERRCHK(vxp);
497
        }
498 2496
}
499
500
/*
501
 * SYNTAX:
502
 *   expr:
503
 *     expr_or EOI { 'or' expr_or EOI }?
504
 */
505
506
static void
507 2509
vxp_expr(struct vxp *vxp, struct vex **pvex)
508
{
509 2509
        struct vex *a = NULL, *or;
510
511 2509
        if (*pvex == NULL) {
512 2431
                vxp_expr_or(vxp, pvex);
513 2431
                ERRCHK(vxp);
514 2080
                ExpectErr(vxp, EOI);
515 2080
                return;
516
        }
517
518 78
        vxp_expr(vxp, &a);
519 78
        ERRCHK(vxp);
520
521 78
        or = vex_alloc(vxp);
522 78
        AN(or);
523 78
        or->tok = T_OR;
524 78
        or->b = *pvex;
525 78
        or->a = a;
526 78
        *pvex = or;
527 2509
}
528
529
/*
530
 * Build a struct vex tree from the token list in vxp
531
 */
532
533
struct vex *
534 2379
vxp_Parse(struct vxp *vxp)
535
{
536 2379
        struct vex *vex = NULL;
537
538 2379
        AZ(vxp->err);
539 2379
        vxp->t = VTAILQ_FIRST(&vxp->tokens);
540
541 4459
        while (vxp->t != NULL) {
542
                /* Ignore empty queries */
543 2834
                while (vxp->t != NULL && vxp->t->tok == EOI)
544 312
                        vxp->t = VTAILQ_NEXT(vxp->t, list);
545
546 2522
                if (vxp->t == NULL)
547 91
                        break;
548
549 2431
                vxp_expr(vxp, &vex);
550
551 2431
                if (vxp->err) {
552 351
                        if (vex)
553 351
                                vex_Free(&vex);
554 351
                        AZ(vex);
555 351
                        return (NULL);
556
                }
557
558 2080
                vxp->t = VTAILQ_NEXT(vxp->t, list);
559
        }
560
561 2028
        return (vex);
562 2379
}
563
564
/*
565
 * Free a struct vex tree
566
 */
567
568
void
569 2912
vex_Free(struct vex **pvex)
570
{
571
        struct vex *vex;
572
        struct vex_lhs *lhs;
573
        struct vex_rhs *rhs;
574
575 2912
        TAKE_OBJ_NOTNULL(vex, pvex, VEX_MAGIC);
576
577 2912
        if (vex->lhs) {
578 2626
                CAST_OBJ_NOTNULL(lhs, vex->lhs, VEX_LHS_MAGIC);
579 2626
                if (lhs->tags)
580 2626
                        vbit_destroy(lhs->tags);
581 2626
                if (lhs->prefix)
582 273
                        free(lhs->prefix);
583 2626
                FREE_OBJ(lhs);
584 2626
        }
585 2912
        if (vex->rhs) {
586 1885
                CAST_OBJ_NOTNULL(rhs, vex->rhs, VEX_RHS_MAGIC);
587 1885
                if (rhs->val_string)
588 897
                        free(rhs->val_string);
589 1885
                if (rhs->val_regex)
590 715
                        VRE_free(&rhs->val_regex);
591 1885
                FREE_OBJ(rhs);
592 1885
        }
593 2912
        if (vex->a) {
594 286
                vex_Free(&vex->a);
595 286
                AZ(vex->a);
596 286
        }
597 2912
        if (vex->b) {
598 273
                vex_Free(&vex->b);
599 273
                AZ(vex->b);
600 273
        }
601 2912
        FREE_OBJ(vex);
602 2912
}
603
604
#ifdef VXP_DEBUG
605
606
static void
607 39
vex_print_rhs(const struct vex_rhs *rhs)
608
{
609
610 39
        CHECK_OBJ_NOTNULL(rhs, VEX_RHS_MAGIC);
611 39
        fprintf(stderr, "rhs=");
612 39
        switch (rhs->type) {
613
        case VEX_INT:
614 26
                fprintf(stderr, "INT(%jd)", (intmax_t)rhs->val_int);
615 26
                break;
616
        case VEX_FLOAT:
617 0
                fprintf(stderr, "FLOAT(%f)", rhs->val_float);
618 0
                break;
619
        case VEX_STRING:
620 0
                AN(rhs->val_string);
621 0
                fprintf(stderr, "STRING(%s)", rhs->val_string);
622 0
                break;
623
        case VEX_REGEX:
624 13
                AN(rhs->val_string);
625 13
                AN(rhs->val_regex);
626 13
                fprintf(stderr, "REGEX(%s)", rhs->val_string);
627 13
                break;
628
        default:
629 0
                WRONG("rhs type");
630 0
                break;
631
        }
632 39
}
633
634
static void
635 52
vex_print_tags(const struct vbitmap *vbm)
636
{
637
        int i;
638 52
        int first = 1;
639
640 13364
        for (i = 0; i < SLT__MAX; i++) {
641 13312
                if (VSL_tags[i] == NULL)
642 8476
                        continue;
643 4836
                if (!vbit_test(vbm, i))
644 4732
                        continue;
645 104
                if (first)
646 52
                        first = 0;
647
                else
648 52
                        fprintf(stderr, ",");
649 104
                fprintf(stderr, "%s", VSL_tags[i]);
650 104
        }
651 52
}
652
653
static void
654 78
vex_print(const struct vex *vex, int indent)
655
{
656 78
        CHECK_OBJ_NOTNULL(vex, VEX_MAGIC);
657
658 78
        fprintf(stderr, "%*s%s", indent, "", vxp_tnames[vex->tok]);
659 78
        if (vex->lhs != NULL) {
660 52
                CHECK_OBJ(vex->lhs, VEX_LHS_MAGIC);
661 52
                AN(vex->lhs->tags);
662 52
                fprintf(stderr, " lhs=");
663 52
                if (vex->lhs->level >= 0)
664 0
                        fprintf(stderr, "{%d%s}", vex->lhs->level,
665 0
                            vex->lhs->level_pm < 0 ? "-" :
666 0
                            vex->lhs->level_pm > 0 ? "+" : "");
667 52
                fprintf(stderr, "(");
668 52
                vex_print_tags(vex->lhs->tags);
669 52
                fprintf(stderr, ")");
670 52
                if (vex->lhs->prefix) {
671 26
                        assert(vex->lhs->prefixlen == strlen(vex->lhs->prefix));
672 26
                        fprintf(stderr, ":%s", vex->lhs->prefix);
673 26
                }
674 52
                if (vex->lhs->field > 0)
675 13
                        fprintf(stderr, "[%d]", vex->lhs->field);
676 52
        }
677 78
        if (vex->rhs != NULL) {
678 39
                fprintf(stderr, " ");
679 39
                vex_print_rhs(vex->rhs);
680 39
        }
681 78
        fprintf(stderr, "\n");
682 78
        if (vex->a != NULL)
683 26
                vex_print(vex->a, indent + 2);
684 78
        if (vex->b != NULL)
685 26
                vex_print(vex->b, indent + 2);
686 78
}
687
688
void
689 26
vex_PrintTree(const struct vex *vex)
690
{
691
692 26
        CHECK_OBJ_NOTNULL(vex, VEX_MAGIC);
693 26
        fprintf(stderr, "VEX tree:\n");
694 26
        vex_print(vex, 2);
695 26
}
696
697
#endif /* VXP_DEBUG */