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