Code Coverage: vinyl-cache/lib/libvinylapi/vxp_lexer.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
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 <stdlib.h>
36
#include <string.h>
37
#include <stdint.h>
38
#include <unistd.h> /* for MUSL */
39
40
#include "vdef.h"
41
#include "vas.h"
42
#include "vqueue.h"
43
44
#include "vre.h"
45
#include "vsb.h"
46
47
#include "vxp.h"
48
49
static void
50
vxp_append_token(struct vxp *vxp, unsigned tok, const char *b, const char *e)
51 1282
{
52 1282
        struct token *t;
53
54 1282
        t = vxp_Alloc(vxp, sizeof *t);
55 1282
        AN(t);
56 1282
        t->tok = tok;
57 1282
        t->b = b;
58 1282
        t->e = e;
59 1282
        VTAILQ_INSERT_TAIL(&vxp->tokens, t, list);
60 1282
        vxp->t = t;
61 1282
}
62
63
/* Unquote and unescape string */
64
static void
65
vxp_decstr(struct vxp *vxp)
66 87
{
67 87
        const char *b, *e, *p;
68 87
        char *s;
69 87
        int esc = 0;
70
71 87
        assert(vxp->t->tok == VAL);
72
73 87
        b = vxp->t->b;
74 87
        e = vxp->t->e;
75 87
        assert(e - b >= 2);
76 87
        b++;
77 87
        e--;
78
79 87
        s = vxp->t->dec = vxp_Alloc(vxp, (e - b) + 1);
80 87
        AN(vxp->t->dec);
81 539
        for (p = b; p < e; p++) {
82 452
                if (!esc && *p == '\\') {
83 1
                        esc = 1;
84 1
                        continue;
85 1
                }
86 451
                esc = 0;
87 451
                *s++ = *p;
88 451
        }
89 87
        *s = '\0';
90 87
}
91
92
/*
93
 * Build a token list
94
 */
95
96
void
97
vxp_Lexer(struct vxp *vxp)
98 262
{
99 262
        const char *p, *q;
100 262
        unsigned u;
101 262
        char quote;
102
103 1839
        for (p = vxp->b; p < vxp->e; ) {
104
105
                /* Skip any space or tab */
106 1581
                if (isblank(*p)) {
107 542
                        p++;
108 542
                        continue;
109 542
                }
110
111 1039
                if (*p == '\\' && p[1] == '\n') {
112 2
                        p += 2;
113 2
                        continue;
114 2
                }
115
116
                /* Skip comments */
117 1037
                if (*p == '#') {
118 182
                        while (p < vxp->e && *p != '\n')
119 169
                                p++;
120 13
                        continue;
121 13
                }
122
123
                /* Match for the fixed tokens */
124 1024
                u = vxp_fixed_token(p, &q);
125 1024
                if (u != 0) {
126 440
                        AN(q);
127 440
                        vxp_append_token(vxp, u, p, q);
128 440
                        p = q;
129 440
                        continue;
130 440
                }
131
132
                /* Match quoted strings */
133 584
                if (*p == '"' || *p == '\'') {
134 90
                        quote = *p;
135 572
                        for (q = p + 1; q < vxp->e; q++) {
136 571
                                if (*q == '\\') {
137 3
                                        q++;
138 3
                                        if (q == vxp->e || *q == '\n')
139 1
                                                break;
140 568
                                } else if (*q == '\n') {
141 1
                                        break;
142 567
                                } else if (*q == quote) {
143 87
                                        q++;
144 87
                                        quote = '\0';
145 87
                                        break;
146 87
                                }
147 571
                        }
148 90
                        vxp_append_token(vxp, VAL, p, q);
149 90
                        if (quote != '\0') {
150 3
                                VSB_cat(vxp->sb, "Unterminated string ");
151 3
                                vxp_ErrWhere(vxp, vxp->t, q - p);
152 3
                                return;
153 3
                        }
154 87
                        vxp_decstr(vxp);
155 87
                        p = q;
156 87
                        continue;
157 90
                }
158
159
                /* Match bareword */
160 494
                if (isword(*p)) {
161 3486
                        for (q = p; q < vxp->e; q++)
162 3334
                                if (!isword(*q))
163 311
                                        break;
164 463
                        vxp_append_token(vxp, VAL, p, q);
165 463
                        vxp->t->dec = vxp_Alloc(vxp, (q - p) + 1);
166 463
                        AN(vxp->t->dec);
167 463
                        vmemcpy(vxp->t->dec, p, q - p);
168 463
                        vxp->t->dec[q - p] = '\0';
169 463
                        p = q;
170 463
                        continue;
171 463
                }
172
173
                /* On to the next query */
174 31
                if (*p == '\n') {
175 30
                        vxp_append_token(vxp, EOI, p, p + 1);
176 30
                        p++;
177 30
                        continue;
178 30
                }
179
180
                /* Error */
181 1
                vxp_append_token(vxp, EOI, p, p + 1);
182 1
                VSB_cat(vxp->sb, "Syntax error ");
183 1
                vxp_ErrWhere(vxp, vxp->t, q - p);
184 1
                return;
185 31
        }
186
187
        /* Finished */
188 258
        vxp_append_token(vxp, EOI, vxp->e, vxp->e);
189 258
}
190
191
#ifdef VXP_DEBUG
192
#include <stdio.h>
193
void
194
vxp_PrintTokens(const struct vxp *vxp)
195 3
{
196 3
        struct token *t;
197
198 3
        fprintf(stderr, "Token list:\n");
199 3
        fprintf(stderr, "  %-5s %-20s %s\n", "TOK", "SUBSTR", "DECODED");
200 36
        VTAILQ_FOREACH(t, &vxp->tokens, list) {
201 36
                fprintf(stderr, "  ");
202 36
                fprintf(stderr, "%-5s", vxp_tnames[t->tok]);
203 36
                fprintf(stderr, " %-20.*s", (unsigned)(t->e - t->b), t->b);
204 36
                if (t->dec)
205 19
                        fprintf(stderr, " '%s'", t->dec);
206 36
                fprintf(stderr, "\n");
207 36
        }
208
        fprintf(stderr, "\n");
209 3
}
210
#endif /* VXP_DEBUG */