summaryrefslogtreecommitdiffstats
path: root/src/util/mystrtok.c
blob: d5f32b7c47dd1d91f72e7516b889377831c05bda (plain)
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
/*++
/* NAME
/*	mystrtok 3
/* SUMMARY
/*	safe tokenizer
/* SYNOPSIS
/*	#include <stringops.h>
/*
/*	char	*mystrtok(bufp, delimiters)
/*	char	**bufp;
/*	const char *delimiters;
/*
/*	char	*mystrtokq(bufp, delimiters, parens)
/*	char	**bufp;
/*	const char *delimiters;
/*	const char *parens;
/*
/*	char	*mystrtokdq(bufp, delimiters)
/*	char	**bufp;
/*	const char *delimiters;
/*
/*	char	*mystrtok_cw(bufp, delimiters, blame)
/*	char	**bufp;
/*	const char *delimiters;
/*	const char *blame;
/*
/*	char	*mystrtokq_cw(bufp, delimiters, parens, blame)
/*	char	**bufp;
/*	const char *delimiters;
/*	const char *parens;
/*	const char *blame;
/*
/*	char	*mystrtokdq_cw(bufp, delimiters, blame)
/*	char	**bufp;
/*	const char *delimiters;
/*	const char *blame;
/* DESCRIPTION
/*	mystrtok() splits a buffer on the specified \fIdelimiters\fR.
/*	Tokens are delimited by runs of delimiters, so this routine
/*	cannot return zero-length tokens.
/*
/*	mystrtokq() is like mystrtok() but will not split text
/*	between balanced parentheses.  \fIparens\fR specifies the
/*	opening and closing parenthesis (one of each).  The set of
/*	\fIparens\fR must be distinct from the set of \fIdelimiters\fR.
/*
/*	mystrtokdq() is like mystrtok() but will not split text
/*	between double quotes. The backslash character may be used
/*	to escape characters. The double quote and backslash
/*	character must not appear in the set of \fIdelimiters\fR.
/*
/*	The \fIbufp\fR argument specifies the start of the search; it
/*	is updated with each call. The input is destroyed.
/*
/*	The result value is the next token, or a null pointer when the
/*	end of the buffer was reached.
/*
/*	mystrtok_cw(), mystrtokq_cw(), and mystrtokdq_cw, log a
/*	warning and return null when the result would look like
/*	comment. The \fBblame\fR argument provides context for
/*	warning messages. Specify a null pointer to disable the
/*	comment check.
/* LICENSE
/* .ad
/* .fi
/*	The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/*	Wietse Venema
/*	IBM T.J. Watson Research
/*	P.O. Box 704
/*	Yorktown Heights, NY 10598, USA
/*
/*	Wietse Venema
/*	Google, Inc.
/*	111 8th Avenue
/*	New York, NY 10011, USA
/*--*/

/* System library. */

#include <sys_defs.h>
#include <string.h>

/* Utility library. */

#include <msg.h>
#include <stringops.h>

/* mystrtok_warn - warn for #comment after other text */

static void mystrtok_warn(const char *start, const char *bufp, const char *blame)
{
    msg_warn("%s: #comment after other text is not allowed: %s %.20s...",
	     blame, start, bufp);
}

/* mystrtok - ABI compatibility wrapper */

#undef mystrtok

char   *mystrtok(char **src, const char *sep)
{
    return (mystrtok_cw(src, sep, (char *) 0));
}

/* mystrtok - safe tokenizer */

char   *mystrtok_cw(char **src, const char *sep, const char *blame)
{
    char   *start = *src;
    char   *end;

    /*
     * Skip over leading delimiters.
     */
    start += strspn(start, sep);
    if (*start == 0) {
	*src = start;
	return (0);
    }

    /*
     * Separate off one token.
     */
    end = start + strcspn(start, sep);
    if (*end != 0)
	*end++ = 0;
    *src = end;

    if (blame && *start == '#') {
	mystrtok_warn(start, *src, blame);
	return (0);
    } else {
	return (start);
    }
}

/* mystrtokq - ABI compatibility wrapper */

#undef mystrtokq

char   *mystrtokq(char **src, const char *sep, const char *parens)
{
    return (mystrtokq_cw(src, sep, parens, (char *) 0));
}

/* mystrtokq_cw - safe tokenizer with quoting support */

char   *mystrtokq_cw(char **src, const char *sep, const char *parens,
		             const char *blame)
{
    char   *start = *src;
    static char *cp;
    int     ch;
    int     level;

    /*
     * Skip over leading delimiters.
     */
    start += strspn(start, sep);
    if (*start == 0) {
	*src = start;
	return (0);
    }

    /*
     * Parse out the next token.
     */
    for (level = 0, cp = start; (ch = *(unsigned char *) cp) != 0; cp++) {
	if (ch == parens[0]) {
	    level++;
	} else if (level > 0 && ch == parens[1]) {
	    level--;
	} else if (level == 0 && strchr(sep, ch) != 0) {
	    *cp++ = 0;
	    break;
	}
    }
    *src = cp;

    if (blame && *start == '#') {
	mystrtok_warn(start, *src, blame);
	return (0);
    } else {
	return (start);
    }
}

/* mystrtokdq - ABI compatibility wrapper */

#undef mystrtokdq

char   *mystrtokdq(char **src, const char *sep)
{
    return (mystrtokdq_cw(src, sep, (char *) 0));
}

/* mystrtokdq_cw - safe tokenizer, double quote and backslash support */

char   *mystrtokdq_cw(char **src, const char *sep, const char *blame)
{
    char   *cp = *src;
    char   *start;

    /*
     * Skip leading delimiters.
     */
    cp += strspn(cp, sep);

    /*
     * Skip to next unquoted space or comma.
     */
    if (*cp == 0) {
	start = 0;
    } else {
	int     in_quotes;

	for (in_quotes = 0, start = cp; *cp; cp++) {
	    if (*cp == '\\') {
		if (*++cp == 0)
		    break;
	    } else if (*cp == '"') {
		in_quotes = !in_quotes;
	    } else if (!in_quotes && strchr(sep, *(unsigned char *) cp) != 0) {
		*cp++ = 0;
		break;
	    }
	}
    }
    *src = cp;

    if (blame && start && *start == '#') {
	mystrtok_warn(start, *src, blame);
	return (0);
    } else {
	return (start);
    }
}

#ifdef TEST

 /*
  * Test program.
  */
#include "msg.h"
#include "mymalloc.h"

 /*
  * The following needs to be large enough to include a null terminator in
  * every testcase.expected field.
  */
#define EXPECT_SIZE	5

struct testcase {
    const char *action;
    const char *input;
    const char *expected[EXPECT_SIZE];
};
static const struct testcase testcases[] = {
    {"mystrtok", ""},
    {"mystrtok", "  foo  ", {"foo"}},
    {"mystrtok", "  foo  bar  ", {"foo", "bar"}},
    {"mystrtokq", ""},
    {"mystrtokq", "foo bar", {"foo", "bar"}},
    {"mystrtokq", "{ bar }  ", {"{ bar }"}},
    {"mystrtokq", "foo { bar } baz", {"foo", "{ bar }", "baz"}},
    {"mystrtokq", "foo{ bar } baz", {"foo{ bar }", "baz"}},
    {"mystrtokq", "foo { bar }baz", {"foo", "{ bar }baz"}},
    {"mystrtokdq", ""},
    {"mystrtokdq", "  foo  ", {"foo"}},
    {"mystrtokdq", "  foo  bar  ", {"foo", "bar"}},
    {"mystrtokdq", "  foo\\ bar  ", {"foo\\ bar"}},
    {"mystrtokdq", "  foo \\\" bar", {"foo", "\\\"", "bar"}},
    {"mystrtokdq", "  foo \" bar baz\"  ", {"foo", "\" bar baz\""}},
    {"mystrtok_cw", "#after text"},
    {"mystrtok_cw", "before-text #after text", {"before-text"}},
    {"mystrtokq_cw", "#after text"},
    {"mystrtokq_cw", "{ before text } #after text", "{ before text }"},
    {"mystrtokdq_cw", "#after text"},
    {"mystrtokdq_cw", "\"before text\" #after text", {"\"before text\""}},
};

int     main(void)
{
    const struct testcase *tp;
    char   *actual;
    int     pass;
    int     fail;
    int     match;
    int     n;

#define NUM_TESTS       sizeof(testcases)/sizeof(testcases[0])
#define STR_OR_NULL(s)	((s) ? (s) : "null")

    for (pass = fail = 0, tp = testcases; tp < testcases + NUM_TESTS; tp++) {
	char   *saved_input = mystrdup(tp->input);
	char   *cp = saved_input;

	msg_info("RUN test case %ld %s >%s<",
		 (long) (tp - testcases), tp->action, tp->input);
#if 0
	msg_info("action=%s", tp->action);
	msg_info("input=%s", tp->input);
	for (n = 0; tp->expected[n]; tp++)
	    msg_info("expected[%d]=%s", n, tp->expected[n]);
#endif

	for (n = 0; n < EXPECT_SIZE; n++) {
	    if (strcmp(tp->action, "mystrtok") == 0) {
		actual = mystrtok(&cp, CHARS_SPACE);
	    } else if (strcmp(tp->action, "mystrtokq") == 0) {
		actual = mystrtokq(&cp, CHARS_SPACE, CHARS_BRACE);
	    } else if (strcmp(tp->action, "mystrtokdq") == 0) {
		actual = mystrtokdq(&cp, CHARS_SPACE);
	    } else if (strcmp(tp->action, "mystrtok_cw") == 0) {
		actual = mystrtok_cw(&cp, CHARS_SPACE, "test");
	    } else if (strcmp(tp->action, "mystrtokq_cw") == 0) {
		actual = mystrtokq_cw(&cp, CHARS_SPACE, CHARS_BRACE, "test");
	    } else if (strcmp(tp->action, "mystrtokdq_cw") == 0) {
		actual = mystrtokdq_cw(&cp, CHARS_SPACE, "test");
	    } else {
		msg_panic("invalid command: %s", tp->action);
	    }
	    if ((match = (actual && tp->expected[n]) ?
		 (strcmp(actual, tp->expected[n]) == 0) :
		 (actual == tp->expected[n])) != 0) {
		if (actual == 0) {
		    msg_info("PASS test %ld", (long) (tp - testcases));
		    pass++;
		    break;
		}
	    } else {
		msg_warn("expected: >%s<, got: >%s<",
			 STR_OR_NULL(tp->expected[n]), STR_OR_NULL(actual));
		msg_info("FAIL test %ld", (long) (tp - testcases));
		fail++;
		break;
	    }
	}
	if (n >= EXPECT_SIZE)
	    msg_panic("need to increase EXPECT_SIZE");
	myfree(saved_input);
    }
    return (fail > 0);
}

#endif