File: vav.c

package info (click to toggle)
varnish 7.7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,256 kB
  • sloc: ansic: 104,222; python: 2,679; makefile: 1,303; sh: 1,077; awk: 114; perl: 105; ruby: 41
file content (498 lines) | stat: -rw-r--r-- 10,958 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*-
 * Copyright (c) 2006 Verdens Gang AS
 * Copyright (c) 2006-2011 Varnish Software AS
 * All rights reserved.
 *
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * const char **VAV_Parse(const char *s, int *argc, int flag)
 *	Parse a command like line into an argv[]
 *	Index zero contains NULL or an error message
 *	(The error message is a static const char* and does not
 *	need saving or copying.)
 *	"double quotes" and backslash substitution is handled.
 *
 * void VAV_Free(const char **argv)
 *	Free the result of VAV_Parse()
 *
 */

#include "config.h"

#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "vdef.h"

#include "vas.h"
#include "vav.h"
#include "vnum.h"

static int
vav_backslash_txt(const char *s, const char *e, char *res)
{
	int r, l, i;
	const char *p;
	char c;

	AN(s);
	if (e == NULL)
		e = strchr(s, '\0');

	l = pdiff(s, e);
	if (l < 2)
		return (0);

	assert(*s == '\\');
	r = c = 0;
	switch (s[1]) {
	case 'n':
		c = '\n';
		r = 2;
		break;
	case 'r':
		c = '\r';
		r = 2;
		break;
	case 't':
		c = '\t';
		r = 2;
		break;
	case '"':
		c = '"';
		r = 2;
		break;
	case '\\':
		c = '\\';
		r = 2;
		break;
	case '0': case '1': case '2': case '3':
	case '4': case '5': case '6': case '7':
		for (r = 1; r < 4 && r < l; r++) {
			if (!isdigit(s[r]))
				break;
			if (s[r] - '0' > 7)
				break;
			c <<= 3;	/*lint !e701 signed left shift */
			c |= s[r] - '0';
		}
		break;
	case 'x':
		if (l >= 4 && (i = VNUM_hex(s + 2, s + 4, &p)) >= 0 &&
		    p == s + 4) {
			AZ(i & ~0xff);
			c = i;	/*lint !e734 loss of precision */
			r = 4;
		}
		break;
	default:
		break;
	}
	if (res != NULL)
		*res = c;
	return (r);
}

int
VAV_BackSlash(const char *s, char *res)
{

	return (vav_backslash_txt(s, NULL, res));
}

char *
VAV_BackSlashDecode(const char *s, const char *e)
{
	const char *q;
	char *p, *r;
	int i;

	if (e == NULL)
		e = strchr(s, '\0');
	assert(e != NULL);
	p = calloc(1, (e - s) + 1L);
	if (p == NULL)
		return (p);
	for (r = p, q = s; q < e; ) {
		if (*q != '\\') {
			*r++ = *q++;
			continue;
		}
		i = vav_backslash_txt(q, e, r);
		if (i == 0) {
			free(p);
			errno = EINVAL;
			return (NULL);
		}
		q += i;
		r++;
	}
	*r = '\0';
	return (p);
}

static char err_invalid_backslash[] = "Invalid backslash sequence";
static char err_invalid_quote[] = "Invalid '\"'";
static char err_missing_quote[] = "Missing '\"'";
static char err_missing_separator[] = "Missing separator between arguments";

char **
VAV_ParseTxt(const char *b, const char *e, int *argc, int flag)
{
	char **argv;
	const char *p, *sep;
	int nargv, largv;
	int i, quote;

	AN(b);
	if (e == NULL)
		e = strchr(b, '\0');
	sep = NULL;
	quote = 0;
	nargv = 1;
	largv = 16;
	argv = calloc(largv, sizeof *argv);
	if (argv == NULL)
		return (NULL);

	while (b < e) {
		if (isspace(*b)) {
			b++;
			continue;
		}
		if (sep != NULL && isspace(*sep) &&
		    *b == ',' && (flag & ARGV_COMMA)) {
			sep = NULL;
			b++;
			continue;
		}
		if (sep != NULL && *sep == '"' &&
		    *b == ',' && (flag & ARGV_COMMA)) {
			sep = NULL;
			b++;
			continue;
		}
		if (sep != NULL && *sep == '"' && *b == '"' && (b - sep) < 2) {
			argv[0] = err_missing_separator;
			return (argv);
		}
		sep = NULL;
		if ((flag & ARGV_COMMENT) && *b == '#')
			break;
		if (*b == '"' && !(flag & ARGV_NOESC)) {
			p = ++b;
			quote = 1;
		} else {
			p = b;
			quote = 0;
		}
		while (b < e) {
			if (*b == '\\' && !(flag & ARGV_NOESC)) {
				i = vav_backslash_txt(b, e, NULL);
				if (i == 0) {
					argv[0] = err_invalid_backslash;
					return (argv);
				}
				b += i;
				continue;
			}
			if (!quote) {
				if (isspace(*b)) {
					sep = b;
					break;
				}
				if ((flag & ARGV_COMMA) && *b == ',') {
					sep = b;
					break;
				}
				if (!(flag & ARGV_NOESC) && *b == '"') {
					argv[0] = err_invalid_quote;
					return (argv);
				}
				b++;
				continue;
			}
			if (*b == '"' && !(flag & ARGV_NOESC)) {
				sep = b;
				quote = 0;
				break;
			}
			b++;
		}
		if (sep == NULL && quote) {
			argv[0] = err_missing_quote;
			return (argv);
		}
		/*
		 * Ensure slots for 1 new arg plus 1 trailing arg,
		 * vcli_serve.c depends on this extra slot.
		 */
		if (nargv + 2 >= largv) {
			argv = realloc(argv, sizeof (*argv) * (largv += largv));
			assert(argv != NULL);
		}
		if (flag & ARGV_NOESC) {
			argv[nargv] = malloc(1L + (b - p));
			assert(argv[nargv] != NULL);
			memcpy(argv[nargv], p, b - p);
			argv[nargv][b - p] = '\0';
		} else {
			argv[nargv] = VAV_BackSlashDecode(p, b);
			assert(argv[nargv] != NULL);
		}
		nargv++;
		if (b < e)
			b++;
	}
	if (sep != NULL && *sep == ',') {
		argv[nargv] = strdup("");
		assert(argv[nargv] != NULL);
		nargv++;
	}
	argv[nargv] = NULL;
	if (argc != NULL)
		*argc = nargv;
	return (argv);
}

char **
VAV_Parse(const char *s, int *argc, int flag)
{

	return (VAV_ParseTxt(s, NULL, argc, flag));
}

void
VAV_Free(char **argv)
{
	int i;

	for (i = 1; argv[i] != NULL; i++)
		free(argv[i]);
	free(argv);
}

#ifdef TESTPROG

#include <printf.h>

static void
VAV_Print(char **argv)
{
	int i;

	printf("---- %p\n", argv);
	if (argv[0] != NULL)
		printf("err %V\n", argv[0]);
	for (i = 1; argv[i] != NULL; i++)
		printf("%3d %V\n", i, argv[i]);
}

static void
Test(const char *str)
{
	char **av;

	printf("Test: <%V>\n", str);
	av = VAV_Parse(str, NULL, 0);
	VAV_Print(av);
}

#if defined __linux__
int
printf_v(FILE *stream, const struct printf_info *info,
    const void *const *args)
{
	const char *v = *((char **)args[0]);
	return (fprintf(stream, "%*s",
	    info->left ? -info->width : info->width, v));
}

int
printf_v_info(const struct printf_info *info, size_t n, int *argtypes,
    int *size)
{
	if (n > 0)
		argtypes[0] = PA_STRING;
	return (1);
}
#endif

int
main(int argc, char **argv)
{
	char buf[BUFSIZ];

	(void)argc;
	(void)argv;

#if defined __FreeBSD__
	register_printf_render_std("V");
#elif defined __linux__
	register_printf_specifier('V', printf_v, printf_v_info);
#else
#error Unsupported platform
#endif

	while (fgets(buf, sizeof buf, stdin))
		Test(buf);

	return (0);
}
#endif

#ifdef TEST_DRIVER
#  include <stdio.h>

struct test_case {
	int		flag;
	const char	*str;
	const char	**argv;
};

static const struct test_case *tests[] = {
#define TEST_PASS(flag, str, ...)					\
	&(const struct test_case){flag, str,				\
	    (const char **)&(const void *[]){NULL, __VA_ARGS__, NULL}}
#define TEST_FAIL(flag, str, err)					\
	&(const struct test_case){flag, str,				\
	    (const char **)&(const void *[]){err_ ## err, NULL}}
#define K ARGV_COMMENT
#define C ARGV_COMMA
#define N ARGV_NOESC
	TEST_PASS(K|C|N, "", NULL),
	TEST_PASS(0    , "foo", "foo"),
	TEST_PASS(0    , "foo bar", "foo", "bar"),
	TEST_PASS(  C  , "foo bar", "foo", "bar"),
	TEST_PASS(  C  , "foo,bar", "foo", "bar"),
	TEST_PASS(0    , "  foo  bar  ", "foo", "bar"),
	TEST_PASS(  C  , "  foo  ,  bar  ", "foo", "bar"),
	TEST_PASS(  C  , "foo bar ", "foo", "bar"),
	TEST_PASS(  C  , "foo,bar,", "foo", "bar", ""),
	TEST_PASS(0    , "foo \"bar baz\"", "foo", "bar baz"),
	TEST_PASS(0    , "foo #bar", "foo", "#bar"),
	TEST_PASS(K    , "foo #bar", "foo"),
	TEST_PASS(0    , "foo#bar", "foo#bar"),
	TEST_PASS(K    , "foo#bar", "foo#bar"),
	TEST_PASS(    N, "\\", "\\"),
	TEST_FAIL(0    , "\\", invalid_backslash),
	TEST_FAIL(0    , "\\x", invalid_backslash),
	TEST_FAIL(0    , "\\x2", invalid_backslash),
	TEST_FAIL(0    , "\\x2O", invalid_backslash),
	TEST_PASS(0    , "\\x20", " "),
	TEST_FAIL(0    , "\"foo", missing_quote),
	TEST_PASS(    N, "foo\"bar", "foo\"bar"),
	TEST_FAIL(0    , "foo\"bar", invalid_quote),
	TEST_FAIL(0    , "foo\"bar", invalid_quote),
	TEST_PASS(0    , "\"foo\" \"bar\"", "foo", "bar"),
	TEST_PASS(  C  , "\"foo\",\"bar\"", "foo", "bar"),
	TEST_PASS(    N, "\"foo\"\"bar\"", "\"foo\"\"bar\""),
	TEST_FAIL(0    , "\"foo\"\"bar\"", missing_separator),
	NULL
#undef N
#undef C
#undef K
#undef TEST_FAIL
#undef TEST_PASS
};

static char **
test_run(const struct test_case *tc, int *ret)
{
	const char *exp, *act;
	char **argv, *tmp;
	int argc, i;

	i = strlen(tc->str);
	if (i == 0) {
		argv = VAV_Parse(tc->str, &argc, tc->flag);
	} else {
		tmp = malloc(i); /* sanitizer-friendly */
		AN(tmp);
		memcpy(tmp, tc->str, i);
		argv = VAV_ParseTxt(tmp, tmp + i, &argc, tc->flag);
		free(tmp);
	}
	AN(argv);

	if (tc->argv[0] != argv[0]) {
		exp = tc->argv[0] != NULL ? tc->argv[0] : "success";
		act = argv[0] != NULL ? argv[0] : "success";
		printf(
		    "ERROR: Parsing string <%s> with flags %x, "
		    "expected <%s> got <%s>.\n",
		    tc->str, tc->flag, exp, act);
		*ret = 1;
		return (argv);
	}

	for (i = 1; i < argc && tc->argv[i] != NULL && argv[i] != NULL; i++) {
		if (!strcmp(tc->argv[i], argv[i]))
			continue;
		printf(
		    "ERROR: Parsing string <%s> with flags %x, "
		    "expected <%s> for argv[%d] got <%s>.\n",
		    tc->str, tc->flag, tc->argv[i], i, argv[i]);
		*ret = 1;
		return (argv);
	}

	if (tc->argv[0] == NULL && (tc->argv[i] != NULL || argv[i] != NULL)) {
		act = i < argc ? "less" : "more";
		printf(
		    "ERROR: Parsing string <%s> with flags %x, "
		    "got %s arguments (%d) than expected.\n",
		    tc->str, tc->flag, act, argc);
		*ret = 1;
		return (argv);
	}

	exp = tc->argv[0] == NULL ? "PASS" : "FAIL";
	printf("%s: <%s> with flags %x as expected.\n", exp, tc->str, tc->flag);
	return (argv);
}

int
main(int argc, char **argv)
{
	const struct test_case **tc;
	int ret = 0, fail = 0;

	(void)argc;
	(void)argv;

	for (tc = tests; *tc != NULL; ret = 0, tc++) {
		argv = test_run(*tc, &ret);
		VAV_Free(argv);
		if (ret)
			fail = 1;
	}
	return (fail);
}
#endif /* TEST_DRIVER */