varnish-cache/lib/libvarnishapi/vxp.c
1
/*-
2
 * Copyright (c) 2006 Verdens Gang AS
3
 * Copyright (c) 2006-2015 Varnish Software AS
4
 * All rights reserved.
5
 *
6
 * Author: Martin Blix Grydeland <martin@varnish-software.com>
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 *
29
 */
30
31
#include "config.h"
32
33
#include <ctype.h>
34
#include <stdlib.h>
35
#include <string.h>
36
37
#include "vdef.h"
38
#include "vas.h"
39
#include "miniobj.h"
40
41
#include "vqueue.h"
42
#include "vre.h"
43
#include "vsb.h"
44
45
#include "vxp.h"
46
47
static void
48 0
vxp_ErrToken(const struct vxp *vxp, const struct token *t)
49
{
50
51 0
        if (t->tok == EOI)
52 0
                VSB_printf(vxp->sb, "end of input");
53
        else
54 0
                VSB_printf(vxp->sb, "'%.*s'", PF(t));
55 0
}
56
57
static void
58 9
vxp_Pos(const struct vxp *vxp, struct vsb *vsb, const struct token *t,
59
    int tokoff)
60
{
61
        unsigned pos;
62
63 9
        AN(vxp);
64 9
        AN(vsb);
65 9
        AN(t);
66 9
        assert(t->b >= vxp->b);
67 9
        pos = (unsigned)(t->b - vxp->b);
68 9
        if (tokoff > 0)
69 0
                pos += tokoff;
70 9
        VSB_printf(vsb, "(Pos %u)", pos + 1);
71 9
}
72
73
static void
74 9
vxp_quote(const struct vxp *vxp, const char *b, const char *e, int tokoff)
75
{
76
        const char *p;
77
        char c;
78
79 9
        assert(b <= e);
80 9
        assert(b >= vxp->b);
81 9
        assert(e <= vxp->e);
82 132
        for (p = vxp->b; p < vxp->e; p++) {
83 123
                if (isspace(*p))
84 18
                        VSB_putc(vxp->sb, ' ');
85
                else
86 105
                        VSB_putc(vxp->sb, *p);
87
        }
88 9
        VSB_putc(vxp->sb, '\n');
89 132
        for (p = vxp->b; p < vxp->e; p++) {
90 123
                if (p >= b && p < e) {
91 38
                        if (p - b == tokoff)
92 1
                                c = '^';
93
                        else
94 18
                                c = '#';
95
                } else
96 104
                        c = '-';
97 123
                VSB_putc(vxp->sb, c);
98
        }
99 9
        VSB_putc(vxp->sb, '\n');
100 9
}
101
102
void
103 9
vxp_ErrWhere(struct vxp *vxp, const struct token *t, int tokoff)
104
{
105
106 9
        AN(vxp);
107 9
        AN(t);
108 9
        vxp_Pos(vxp, vxp->sb, t, tokoff);
109 9
        VSB_putc(vxp->sb, '\n');
110 9
        vxp_quote(vxp, t->b, t->e, tokoff);
111 9
        VSB_putc(vxp->sb, '\n');
112 9
        vxp->err = 1;
113 9
}
114
115
void
116 196
vxp_NextToken(struct vxp *vxp)
117
{
118
119 196
        AN(vxp->t);
120 196
        vxp->t = VTAILQ_NEXT(vxp->t, list);
121 196
        if (vxp->t == NULL) {
122 0
                VSB_printf(vxp->sb,
123
                    "Ran out of input, something is missing or"
124
                    " maybe unbalanced parenthesis\n");
125 0
                vxp->err = 1;
126
        }
127 196
}
128
129
void
130 63
vxp__Expect(struct vxp *vxp, unsigned tok)
131
{
132
133 63
        if (vxp->t->tok == tok)
134 126
                return;
135 0
        VSB_printf(vxp->sb, "Expected %s got ", vxp_tnames[tok]);
136 0
        vxp_ErrToken(vxp, vxp->t);
137 0
        VSB_putc(vxp->sb, ' ');
138 0
        vxp_ErrWhere(vxp, vxp->t, -1);
139
}
140
141
static void
142 397
vxp_DoFree(struct vxp *vxp, void *p)
143
{
144
        struct membit *mb;
145
146 397
        mb = calloc(1, sizeof *mb);
147 397
        AN(mb);
148 397
        mb->ptr = p;
149 397
        VTAILQ_INSERT_TAIL(&vxp->membits, mb, list);
150 397
}
151
152
void *
153 397
vxp_Alloc(struct vxp *vxp, unsigned len)
154
{
155
        void *p;
156
157 397
        p = calloc(1, len);
158 397
        AN(p);
159 397
        vxp_DoFree(vxp, p);
160 397
        return (p);
161
}
162
163
static struct vxp *
164 63
vxp_New(struct vsb *sb)
165
{
166
        struct vxp *vxp;
167
168 63
        AN(sb);
169
170 63
        ALLOC_OBJ(vxp, VXP_MAGIC);
171 63
        AN(vxp);
172 63
        VTAILQ_INIT(&vxp->membits);
173 63
        VTAILQ_INIT(&vxp->tokens);
174 63
        vxp->sb = sb;
175
176 63
        return (vxp);
177
}
178
179
static void
180 63
vxp_Delete(struct vxp **pvxp)
181
{
182
        struct vxp *vxp;
183
        struct membit *mb;
184
185 63
        TAKE_OBJ_NOTNULL(vxp, pvxp, VXP_MAGIC);
186
187 523
        while (!VTAILQ_EMPTY(&vxp->membits)) {
188 397
                mb = VTAILQ_FIRST(&vxp->membits);
189 397
                VTAILQ_REMOVE(&vxp->membits, mb, list);
190 397
                free(mb->ptr);
191 397
                free(mb);
192
        }
193
194 63
        FREE_OBJ(vxp);
195 63
}
196
197
struct vex *
198 63
vex_New(const char *query, struct vsb *sb, unsigned options)
199
{
200
        struct vxp *vxp;
201
        struct vex *vex;
202
203 63
        AN(query);
204 63
        AN(sb);
205 63
        vxp = vxp_New(sb);
206 63
        vxp->b = query;
207 63
        vxp->e = query + strlen(query);
208 63
        vxp->vex_options = options;
209 63
        if (options & VEX_OPT_CASELESS)
210 0
                vxp->vre_options |= VRE_CASELESS;
211
212 63
        vxp_Lexer(vxp);
213
214
#ifdef VXP_DEBUG
215
        vxp_PrintTokens(vxp);
216
#endif
217
218 63
        if (vxp->err) {
219 0
                vxp_Delete(&vxp);
220 0
                AZ(vxp);
221 0
                return (NULL);
222
        }
223
224 63
        vex = vxp_Parse(vxp);
225
226
#ifdef VXP_DEBUG
227
        if (vex != NULL)
228
                vex_PrintTree(vex);
229
#endif
230
231 63
        vxp_Delete(&vxp);
232 63
        AZ(vxp);
233
234 63
        return (vex);
235
}