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