varnish-cache/lib/libvarnish/vav.c
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2011 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
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
 * const char **VAV_Parse(const char *s, int *argc, int flag)
31
 *      Parse a command like line into an argv[]
32
 *      Index zero contains NULL or an error message
33
 *      (The error message is a static const char* and does not
34
 *      need saving or copying.)
35
 *      "double quotes" and backslash substitution is handled.
36
 *
37
 * void VAV_Free(const char **argv)
38
 *      Free the result of VAV_Parse()
39
 *
40
 */
41
42
#include "config.h"
43
44
#include <ctype.h>
45
#include <stdint.h>
46
#include <stdio.h>
47
#include <stdlib.h>
48
#include <string.h>
49
50
#include "vdef.h"
51
52
#include "vas.h"
53
#include "vav.h"
54
#include "vnum.h"
55
56
static int
57 2974
vav_backslash_txt(const char *s, const char *e, char *res)
58
{
59
        int r, l, i;
60
        const char *p;
61
        char c;
62
63 2974
        AN(s);
64 2974
        if (e == NULL)
65 2888
                e = strchr(s, '\0');
66
67 2974
        l = pdiff(s, e);
68 2974
        if (l < 2)
69 1
                return (0);
70
71 2973
        assert(*s == '\\');
72 2973
        r = c = 0;
73 2973
        switch (s[1]) {
74
        case 'n':
75 1424
                c = '\n';
76 1424
                r = 2;
77 1424
                break;
78
        case 'r':
79 1080
                c = '\r';
80 1080
                r = 2;
81 1080
                break;
82
        case 't':
83 23
                c = '\t';
84 23
                r = 2;
85 23
                break;
86
        case '"':
87 144
                c = '"';
88 144
                r = 2;
89 144
                break;
90
        case '\\':
91 41
                c = '\\';
92 41
                r = 2;
93 41
                break;
94
        case '0': case '1': case '2': case '3':
95
        case '4': case '5': case '6': case '7':
96 18
                for (r = 1; r < 4 && r < l; r++) {
97 14
                        if (!isdigit(s[r]))
98 1
                                break;
99 13
                        if (s[r] - '0' > 7)
100 0
                                break;
101 13
                        c <<= 3;        /*lint !e701 signed left shift */
102 13
                        c |= s[r] - '0';
103 13
                }
104 5
                break;
105
        case 'x':
106 240
                if (l >= 4 && (i = VNUM_hex(s + 2, s + 4, &p)) >= 0 &&
107 238
                    p == s + 4) {
108 237
                        AZ(i & ~0xff);
109 237
                        c = i;  /*lint !e734 loss of precision */
110 237
                        r = 4;
111 237
                }
112 240
                break;
113
        default:
114 16
                break;
115
        }
116 2973
        if (res != NULL)
117 2929
                *res = c;
118 2973
        return (r);
119 2974
}
120
121
int
122 2888
VAV_BackSlash(const char *s, char *res)
123
{
124
125 2888
        return (vav_backslash_txt(s, NULL, res));
126
}
127
128
char *
129 503530
VAV_BackSlashDecode(const char *s, const char *e)
130
{
131
        const char *q;
132
        char *p, *r;
133
        int i;
134
135 503530
        if (e == NULL)
136 0
                e = strchr(s, '\0');
137 503530
        assert(e != NULL);
138 503530
        p = calloc(1, (e - s) + 1L);
139 503530
        if (p == NULL)
140 0
                return (p);
141 4573040
        for (r = p, q = s; q < e; ) {
142 4069510
                if (*q != '\\') {
143 4069469
                        *r++ = *q++;
144 4069469
                        continue;
145
                }
146 41
                i = vav_backslash_txt(q, e, r);
147 41
                if (i == 0) {
148 0
                        free(p);
149 0
                        errno = EINVAL;
150 0
                        return (NULL);
151
                }
152 41
                q += i;
153 41
                r++;
154
        }
155 503530
        *r = '\0';
156 503530
        return (p);
157 503530
}
158
159
static char err_invalid_backslash[] = "Invalid backslash sequence";
160
static char err_invalid_quote[] = "Invalid '\"'";
161
static char err_missing_quote[] = "Missing '\"'";
162
static char err_missing_separator[] = "Missing separator between arguments";
163
164
char **
165 131859
VAV_ParseTxt(const char *b, const char *e, int *argc, int flag)
166
{
167
        char **argv;
168
        const char *p, *sep;
169
        int nargv, largv;
170
        int i, quote;
171
172 131859
        AN(b);
173 131859
        if (e == NULL)
174 119875
                e = strchr(b, '\0');
175 131859
        sep = NULL;
176 131859
        quote = 0;
177 131859
        nargv = 1;
178 131859
        largv = 16;
179 131859
        argv = calloc(largv, sizeof *argv);
180 131859
        if (argv == NULL)
181 0
                return (NULL);
182
183 722750
        while (b < e) {
184 590904
                if (isspace(*b)) {
185 87213
                        b++;
186 87213
                        continue;
187
                }
188 503692
                if (sep != NULL && isspace(*sep) &&
189 342696
                    *b == ',' && (flag & ARGV_COMMA)) {
190 1
                        sep = NULL;
191 1
                        b++;
192 1
                        continue;
193
                }
194 503691
                if (sep != NULL && *sep == '"' &&
195 991
                    *b == ',' && (flag & ARGV_COMMA)) {
196 1
                        sep = NULL;
197 1
                        b++;
198 1
                        continue;
199
                }
200 503689
                if (sep != NULL && *sep == '"' && *b == '"' && (b - sep) < 2) {
201 1
                        argv[0] = err_missing_separator;
202 1
                        return (argv);
203
                }
204 503688
                sep = NULL;
205 503688
                if ((flag & ARGV_COMMENT) && *b == '#')
206 1
                        break;
207 503687
                if (*b == '"' && !(flag & ARGV_NOESC)) {
208 1954
                        p = ++b;
209 1954
                        quote = 1;
210 1954
                } else {
211 501733
                        p = b;
212 501733
                        quote = 0;
213
                }
214 4573972
                while (b < e) {
215 4453918
                        if (*b == '\\' && !(flag & ARGV_NOESC)) {
216 45
                                i = vav_backslash_txt(b, e, NULL);
217 45
                                if (i == 0) {
218 4
                                        argv[0] = err_invalid_backslash;
219 4
                                        return (argv);
220
                                }
221 41
                                b += i;
222 41
                                continue;
223
                        }
224 4453873
                        if (!quote) {
225 4443453
                                if (isspace(*b)) {
226 353530
                                        sep = b;
227 353530
                                        break;
228
                                }
229 4089923
                                if ((flag & ARGV_COMMA) && *b == ',') {
230 28148
                                        sep = b;
231 28148
                                        break;
232
                                }
233 4061775
                                if (!(flag & ARGV_NOESC) && *b == '"') {
234 2
                                        argv[0] = err_invalid_quote;
235 2
                                        return (argv);
236
                                }
237 4061773
                                b++;
238 4061773
                                continue;
239
                        }
240 10420
                        if (*b == '"' && !(flag & ARGV_NOESC)) {
241 1949
                                sep = b;
242 1949
                                quote = 0;
243 1949
                                break;
244
                        }
245 8471
                        b++;
246
                }
247 503681
                if (sep == NULL && quote) {
248 5
                        argv[0] = err_missing_quote;
249 5
                        return (argv);
250
                }
251
                /*
252
                 * Ensure slots for 1 new arg plus 1 trailing arg,
253
                 * vcli_serve.c depends on this extra slot.
254
                 */
255 503676
                if (nargv + 2 >= largv) {
256 1042
                        argv = realloc(argv, sizeof (*argv) * (largv += largv));
257 1042
                        assert(argv != NULL);
258 1042
                }
259 503676
                if (flag & ARGV_NOESC) {
260 146
                        argv[nargv] = malloc(1L + (b - p));
261 146
                        assert(argv[nargv] != NULL);
262 146
                        memcpy(argv[nargv], p, b - p);
263 146
                        argv[nargv][b - p] = '\0';
264 146
                } else {
265 503530
                        argv[nargv] = VAV_BackSlashDecode(p, b);
266 503530
                        assert(argv[nargv] != NULL);
267
                }
268 503676
                nargv++;
269 503676
                if (b < e)
270 383627
                        b++;
271
        }
272 131847
        if (sep != NULL && *sep == ',') {
273 1
                argv[nargv] = strdup("");
274 1
                assert(argv[nargv] != NULL);
275 1
                nargv++;
276 1
        }
277 131847
        argv[nargv] = NULL;
278 131847
        if (argc != NULL)
279 124736
                *argc = nargv;
280 131847
        return (argv);
281 131859
}
282
283
char **
284 119875
VAV_Parse(const char *s, int *argc, int flag)
285
{
286
287 119875
        return (VAV_ParseTxt(s, NULL, argc, flag));
288
}
289
290
void
291 127991
VAV_Free(char **argv)
292
{
293
        int i;
294
295 620193
        for (i = 1; argv[i] != NULL; i++)
296 492202
                free(argv[i]);
297 127991
        free(argv);
298 127991
}
299
300
#ifdef TESTPROG
301
302
#include <printf.h>
303
304
static void
305
VAV_Print(char **argv)
306
{
307
        int i;
308
309
        printf("---- %p\n", argv);
310
        if (argv[0] != NULL)
311
                printf("err %V\n", argv[0]);
312
        for (i = 1; argv[i] != NULL; i++)
313
                printf("%3d %V\n", i, argv[i]);
314
}
315
316
static void
317
Test(const char *str)
318
{
319
        char **av;
320
321
        printf("Test: <%V>\n", str);
322
        av = VAV_Parse(str, NULL, 0);
323
        VAV_Print(av);
324
}
325
326
#if defined __linux__
327
int
328
printf_v(FILE *stream, const struct printf_info *info,
329
    const void *const *args)
330
{
331
        const char *v = *((char **)args[0]);
332
        return (fprintf(stream, "%*s",
333
            info->left ? -info->width : info->width, v));
334
}
335
336
int
337
printf_v_info(const struct printf_info *info, size_t n, int *argtypes,
338
    int *size)
339
{
340
        if (n > 0)
341
                argtypes[0] = PA_STRING;
342
        return (1);
343
}
344
#endif
345
346
int
347
main(int argc, char **argv)
348
{
349
        char buf[BUFSIZ];
350
351
        (void)argc;
352
        (void)argv;
353
354
#if defined __FreeBSD__
355
        register_printf_render_std("V");
356
#elif defined __linux__
357
        register_printf_specifier('V', printf_v, printf_v_info);
358
#else
359
#error Unsupported platform
360
#endif
361
362
        while (fgets(buf, sizeof buf, stdin))
363
                Test(buf);
364
365
        return (0);
366
}
367
#endif
368
369
#ifdef TEST_DRIVER
370
#  include <stdio.h>
371
372
struct test_case {
373
        int             flag;
374
        const char      *str;
375
        const char      **argv;
376
};
377
378
static const struct test_case *tests[] = {
379
#define TEST_PASS(flag, str, ...)                                       \
380
        &(const struct test_case){flag, str,                            \
381
            (const char **)&(const void *[]){NULL, __VA_ARGS__, NULL}}
382
#define TEST_FAIL(flag, str, err)                                       \
383
        &(const struct test_case){flag, str,                            \
384
            (const char **)&(const void *[]){err_ ## err, NULL}}
385
#define K ARGV_COMMENT
386
#define C ARGV_COMMA
387
#define N ARGV_NOESC
388
        TEST_PASS(K|C|N, "", NULL),
389
        TEST_PASS(0    , "foo", "foo"),
390
        TEST_PASS(0    , "foo bar", "foo", "bar"),
391
        TEST_PASS(  C  , "foo bar", "foo", "bar"),
392
        TEST_PASS(  C  , "foo,bar", "foo", "bar"),
393
        TEST_PASS(0    , "  foo  bar  ", "foo", "bar"),
394
        TEST_PASS(  C  , "  foo  ,  bar  ", "foo", "bar"),
395
        TEST_PASS(  C  , "foo bar ", "foo", "bar"),
396
        TEST_PASS(  C  , "foo,bar,", "foo", "bar", ""),
397
        TEST_PASS(0    , "foo \"bar baz\"", "foo", "bar baz"),
398
        TEST_PASS(0    , "foo #bar", "foo", "#bar"),
399
        TEST_PASS(K    , "foo #bar", "foo"),
400
        TEST_PASS(0    , "foo#bar", "foo#bar"),
401
        TEST_PASS(K    , "foo#bar", "foo#bar"),
402
        TEST_PASS(    N, "\\", "\\"),
403
        TEST_FAIL(0    , "\\", invalid_backslash),
404
        TEST_FAIL(0    , "\\x", invalid_backslash),
405
        TEST_FAIL(0    , "\\x2", invalid_backslash),
406
        TEST_FAIL(0    , "\\x2O", invalid_backslash),
407
        TEST_PASS(0    , "\\x20", " "),
408
        TEST_FAIL(0    , "\"foo", missing_quote),
409
        TEST_PASS(    N, "foo\"bar", "foo\"bar"),
410
        TEST_FAIL(0    , "foo\"bar", invalid_quote),
411
        TEST_FAIL(0    , "foo\"bar", invalid_quote),
412
        TEST_PASS(0    , "\"foo\" \"bar\"", "foo", "bar"),
413
        TEST_PASS(  C  , "\"foo\",\"bar\"", "foo", "bar"),
414
        TEST_PASS(    N, "\"foo\"\"bar\"", "\"foo\"\"bar\""),
415
        TEST_FAIL(0    , "\"foo\"\"bar\"", missing_separator),
416
        NULL
417
#undef N
418
#undef C
419
#undef K
420
#undef TEST_FAIL
421
#undef TEST_PASS
422
};
423
424
static char **
425 28
test_run(const struct test_case *tc, int *ret)
426
{
427
        const char *exp, *act;
428
        char **argv, *tmp;
429
        int argc, i;
430
431 28
        i = strlen(tc->str);
432 28
        if (i == 0) {
433 1
                argv = VAV_Parse(tc->str, &argc, tc->flag);
434 1
        } else {
435 27
                tmp = malloc(i); /* sanitizer-friendly */
436 27
                AN(tmp);
437 27
                memcpy(tmp, tc->str, i);
438 27
                argv = VAV_ParseTxt(tmp, tmp + i, &argc, tc->flag);
439 27
                free(tmp);
440
        }
441 28
        AN(argv);
442
443 28
        if (tc->argv[0] != argv[0]) {
444 0
                exp = tc->argv[0] != NULL ? tc->argv[0] : "success";
445 0
                act = argv[0] != NULL ? argv[0] : "success";
446 0
                printf(
447
                    "ERROR: Parsing string <%s> with flags %x, "
448
                    "expected <%s> got <%s>.\n",
449 0
                    tc->str, tc->flag, exp, act);
450 0
                *ret = 1;
451 0
                return (argv);
452
        }
453
454 59
        for (i = 1; i < argc && tc->argv[i] != NULL && argv[i] != NULL; i++) {
455 31
                if (!strcmp(tc->argv[i], argv[i]))
456 31
                        continue;
457 0
                printf(
458
                    "ERROR: Parsing string <%s> with flags %x, "
459
                    "expected <%s> for argv[%d] got <%s>.\n",
460 0
                    tc->str, tc->flag, tc->argv[i], i, argv[i]);
461 0
                *ret = 1;
462 0
                return (argv);
463
        }
464
465 28
        if (tc->argv[0] == NULL && (tc->argv[i] != NULL || argv[i] != NULL)) {
466 0
                act = i < argc ? "less" : "more";
467 0
                printf(
468
                    "ERROR: Parsing string <%s> with flags %x, "
469
                    "got %s arguments (%d) than expected.\n",
470 0
                    tc->str, tc->flag, act, argc);
471 0
                *ret = 1;
472 0
                return (argv);
473
        }
474
475 28
        exp = tc->argv[0] == NULL ? "PASS" : "FAIL";
476 28
        printf("%s: <%s> with flags %x as expected.\n", exp, tc->str, tc->flag);
477 28
        return (argv);
478 28
}
479
480
int
481 1
main(int argc, char **argv)
482
{
483
        const struct test_case **tc;
484 1
        int ret = 0, fail = 0;
485
486 1
        (void)argc;
487 1
        (void)argv;
488
489 29
        for (tc = tests; *tc != NULL; ret = 0, tc++) {
490 28
                argv = test_run(*tc, &ret);
491 28
                VAV_Free(argv);
492 28
                if (ret)
493 0
                        fail = 1;
494 28
        }
495 1
        return (fail);
496
}
497
#endif /* TEST_DRIVER */