varnish-cache/lib/libvarnishapi/vxp.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 <stdint.h>
36
#include <stdlib.h>
37
#include <string.h>
38
#include <unistd.h> /* for MUSL */
39
40
#include "vdef.h"
41
#include "vas.h"
42
#include "miniobj.h"
43
44
#include "vqueue.h"
45
#include "vre.h"
46
#include "vsb.h"
47
48
#include "vxp.h"
49
50
static void
51 0
vxp_ErrToken(const struct vxp *vxp, const struct token *t)
52
{
53
54 0
        if (t->tok == EOI)
55 0
                VSB_cat(vxp->sb, "end of input");
56
        else
57 0
                VSB_printf(vxp->sb, "'%.*s'", PF(t));
58 0
}
59
60
static void
61 775
vxp_Pos(const struct vxp *vxp, struct vsb *vsb, const struct token *t,
62
    int tokoff)
63
{
64
        unsigned pos;
65
66 775
        AN(vxp);
67 775
        AN(vsb);
68 775
        AN(t);
69 775
        assert(t->b >= vxp->b);
70 775
        pos = (unsigned)(t->b - vxp->b);
71 775
        if (tokoff > 0)
72 109
                pos += tokoff;
73 775
        VSB_printf(vsb, "(Pos %u)", pos + 1);
74 775
}
75
76
static void
77 775
vxp_quote(const struct vxp *vxp, const char *b, const char *e, int tokoff)
78
{
79
        const char *p;
80
        char c;
81
82 775
        assert(b <= e);
83 775
        assert(b >= vxp->b);
84 775
        assert(e <= vxp->e);
85 12525
        for (p = vxp->b; p < vxp->e; p++) {
86 11750
                if (isspace(*p))
87 1650
                        VSB_putc(vxp->sb, ' ');
88
                else
89 10100
                        VSB_putc(vxp->sb, *p);
90 11750
        }
91 775
        VSB_putc(vxp->sb, '\n');
92 12525
        for (p = vxp->b; p < vxp->e; p++) {
93 11750
                if (p >= b && p < e) {
94 2525
                        if (p - b == tokoff)
95 50
                                c = '^';
96
                        else
97 2475
                                c = '#';
98 2525
                } else
99 9225
                        c = '-';
100 11750
                VSB_putc(vxp->sb, c);
101 11750
        }
102 775
        VSB_putc(vxp->sb, '\n');
103 775
}
104
105
void
106 775
vxp_ErrWhere(struct vxp *vxp, const struct token *t, int tokoff)
107
{
108
109 775
        AN(vxp);
110 775
        AN(t);
111 775
        vxp_Pos(vxp, vxp->sb, t, tokoff);
112 775
        VSB_putc(vxp->sb, '\n');
113 775
        vxp_quote(vxp, t->b, t->e, tokoff);
114 775
        VSB_putc(vxp->sb, '\n');
115 775
        vxp->err = 1;
116 775
}
117
118
void
119 14225
vxp_NextToken(struct vxp *vxp)
120
{
121
122 14225
        AN(vxp->t);
123 14225
        vxp->t = VTAILQ_NEXT(vxp->t, list);
124 14225
        if (vxp->t == NULL) {
125 0
                VSB_cat(vxp->sb,
126
                    "Ran out of input, something is missing or"
127
                    " maybe unbalanced parenthesis\n");
128 0
                vxp->err = 1;
129 0
        }
130 14225
}
131
132
void
133 4250
vxp__Expect(struct vxp *vxp, unsigned tok)
134
{
135
136 4250
        if (vxp->t->tok == tok)
137 4250
                return;
138 0
        VSB_printf(vxp->sb, "Expected %s got ", vxp_tnames[tok]);
139 0
        vxp_ErrToken(vxp, vxp->t);
140 0
        VSB_putc(vxp->sb, ' ');
141 0
        vxp_ErrWhere(vxp, vxp->t, -1);
142 4250
}
143
144
static void
145 29525
vxp_DoFree(struct vxp *vxp, void *p)
146
{
147
        struct membit *mb;
148
149 29525
        mb = calloc(1, sizeof *mb);
150 29525
        AN(mb);
151 29525
        mb->ptr = p;
152 29525
        VTAILQ_INSERT_TAIL(&vxp->membits, mb, list);
153 29525
}
154
155
void *
156 29525
vxp_Alloc(struct vxp *vxp, unsigned len)
157
{
158
        void *p;
159
160 29525
        p = calloc(1, len);
161 29525
        AN(p);
162 29525
        vxp_DoFree(vxp, p);
163 29525
        return (p);
164
}
165
166
static struct vxp *
167 4325
vxp_New(struct vsb *sb)
168
{
169
        struct vxp *vxp;
170
171 4325
        AN(sb);
172
173 4325
        ALLOC_OBJ(vxp, VXP_MAGIC);
174 4325
        AN(vxp);
175 4325
        VTAILQ_INIT(&vxp->membits);
176 4325
        VTAILQ_INIT(&vxp->tokens);
177 4325
        vxp->sb = sb;
178
179 4325
        return (vxp);
180
}
181
182
static void
183 4325
vxp_Delete(struct vxp **pvxp)
184
{
185
        struct vxp *vxp;
186
        struct membit *mb;
187
188 4325
        TAKE_OBJ_NOTNULL(vxp, pvxp, VXP_MAGIC);
189
190 33850
        while (!VTAILQ_EMPTY(&vxp->membits)) {
191 29525
                mb = VTAILQ_FIRST(&vxp->membits);
192 29525
                VTAILQ_REMOVE(&vxp->membits, mb, list);
193 29525
                free(mb->ptr);
194 29525
                free(mb);
195
        }
196
197 4325
        FREE_OBJ(vxp);
198 4325
}
199
200
struct vex *
201 4325
vex_New(const char *query, struct vsb *sb, unsigned options)
202
{
203
        struct vxp *vxp;
204
        struct vex *vex;
205
206 4325
        AN(query);
207 4325
        AN(sb);
208 4325
        vxp = vxp_New(sb);
209 4325
        vxp->b = query;
210 4325
        vxp->e = query + strlen(query);
211 4325
        vxp->vex_options = options;
212 4325
        if (options & VEX_OPT_CASELESS)
213 0
                vxp->vre_options |= VRE_CASELESS;
214
215 4325
        vxp_Lexer(vxp);
216
217
#ifdef VXP_DEBUG
218 75
        vxp_PrintTokens(vxp);
219
#endif
220
221 4325
        if (vxp->err) {
222 100
                vxp_Delete(&vxp);
223 100
                AZ(vxp);
224 100
                return (NULL);
225
        }
226
227 4225
        vex = vxp_Parse(vxp);
228
229
#ifdef VXP_DEBUG
230 75
        if (vex != NULL)
231 50
                vex_PrintTree(vex);
232
#endif
233
234 4225
        vxp_Delete(&vxp);
235 4225
        AZ(vxp);
236
237 4225
        return (vex);
238 4325
}