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 62
vxp_Pos(const struct vxp *vxp, struct vsb *vsb, const struct token *t,
62
    int tokoff)
63
{
64
        unsigned pos;
65
66 62
        AN(vxp);
67 62
        AN(vsb);
68 62
        AN(t);
69 62
        assert(t->b >= vxp->b);
70 62
        pos = (unsigned)(t->b - vxp->b);
71 62
        if (tokoff > 0)
72 10
                pos += tokoff;
73 62
        VSB_printf(vsb, "(Pos %u)", pos + 1);
74 62
}
75
76
static void
77 62
vxp_quote(const struct vxp *vxp, const char *b, const char *e, int tokoff)
78
{
79
        const char *p;
80
        char c;
81
82 62
        assert(b <= e);
83 62
        assert(b >= vxp->b);
84 62
        assert(e <= vxp->e);
85 1002
        for (p = vxp->b; p < vxp->e; p++) {
86 940
                if (isspace(*p))
87 132
                        VSB_putc(vxp->sb, ' ');
88
                else
89 808
                        VSB_putc(vxp->sb, *p);
90 940
        }
91 62
        VSB_putc(vxp->sb, '\n');
92 1002
        for (p = vxp->b; p < vxp->e; p++) {
93 940
                if (p >= b && p < e) {
94 202
                        if (p - b == tokoff)
95 4
                                c = '^';
96
                        else
97 198
                                c = '#';
98 202
                } else
99 738
                        c = '-';
100 940
                VSB_putc(vxp->sb, c);
101 940
        }
102 62
        VSB_putc(vxp->sb, '\n');
103 62
}
104
105
void
106 62
vxp_ErrWhere(struct vxp *vxp, const struct token *t, int tokoff)
107
{
108
109 62
        AN(vxp);
110 62
        AN(t);
111 62
        vxp_Pos(vxp, vxp->sb, t, tokoff);
112 62
        VSB_putc(vxp->sb, '\n');
113 62
        vxp_quote(vxp, t->b, t->e, tokoff);
114 62
        VSB_putc(vxp->sb, '\n');
115 62
        vxp->err = 1;
116 62
}
117
118
void
119 1214
vxp_NextToken(struct vxp *vxp)
120
{
121
122 1214
        AN(vxp->t);
123 1214
        vxp->t = VTAILQ_NEXT(vxp->t, list);
124 1214
        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 1214
}
131
132
void
133 368
vxp__Expect(struct vxp *vxp, unsigned tok)
134
{
135
136 368
        if (vxp->t->tok == tok)
137 368
                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 368
}
143
144
static void
145 2512
vxp_DoFree(struct vxp *vxp, void *p)
146
{
147
        struct membit *mb;
148
149 2512
        mb = calloc(1, sizeof *mb);
150 2512
        AN(mb);
151 2512
        mb->ptr = p;
152 2512
        VTAILQ_INSERT_TAIL(&vxp->membits, mb, list);
153 2512
}
154
155
void *
156 2512
vxp_Alloc(struct vxp *vxp, unsigned len)
157
{
158
        void *p;
159
160 2512
        p = calloc(1, len);
161 2512
        AN(p);
162 2512
        vxp_DoFree(vxp, p);
163 2512
        return (p);
164
}
165
166
static struct vxp *
167 374
vxp_New(struct vsb *sb)
168
{
169
        struct vxp *vxp;
170
171 374
        AN(sb);
172
173 374
        ALLOC_OBJ(vxp, VXP_MAGIC);
174 374
        AN(vxp);
175 374
        VTAILQ_INIT(&vxp->membits);
176 374
        VTAILQ_INIT(&vxp->tokens);
177 374
        vxp->sb = sb;
178
179 374
        return (vxp);
180
}
181
182
static void
183 374
vxp_Delete(struct vxp **pvxp)
184
{
185
        struct vxp *vxp;
186
        struct membit *mb;
187
188 374
        TAKE_OBJ_NOTNULL(vxp, pvxp, VXP_MAGIC);
189
190 2886
        while (!VTAILQ_EMPTY(&vxp->membits)) {
191 2512
                mb = VTAILQ_FIRST(&vxp->membits);
192 2512
                VTAILQ_REMOVE(&vxp->membits, mb, list);
193 2512
                free(mb->ptr);
194 2512
                free(mb);
195
        }
196
197 374
        FREE_OBJ(vxp);
198 374
}
199
200
struct vex *
201 374
vex_New(const char *query, struct vsb *sb, unsigned options)
202
{
203
        struct vxp *vxp;
204
        struct vex *vex;
205
206 374
        AN(query);
207 374
        AN(sb);
208 374
        vxp = vxp_New(sb);
209 374
        vxp->b = query;
210 374
        vxp->e = query + strlen(query);
211 374
        vxp->vex_options = options;
212 374
        if (options & VEX_OPT_CASELESS)
213 0
                vxp->vre_options |= VRE_CASELESS;
214
215 374
        vxp_Lexer(vxp);
216
217
#ifdef VXP_DEBUG
218 6
        vxp_PrintTokens(vxp);
219
#endif
220
221 374
        if (vxp->err) {
222 8
                vxp_Delete(&vxp);
223 8
                AZ(vxp);
224 8
                return (NULL);
225
        }
226
227 366
        vex = vxp_Parse(vxp);
228
229
#ifdef VXP_DEBUG
230 6
        if (vex != NULL)
231 4
                vex_PrintTree(vex);
232
#endif
233
234 366
        vxp_Delete(&vxp);
235 366
        AZ(vxp);
236
237 366
        return (vex);
238 374
}