summaryrefslogtreecommitdiff
path: root/usr/src/lib/libast/common/string/tokscan.c
blob: ba4a455769fa2047c0ecd18506d0e51910896ece (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
348
349
350
351
352
353
354
355
356
357
358
359
360
/***********************************************************************
*                                                                      *
*               This software is part of the ast package               *
*          Copyright (c) 1985-2009 AT&T Intellectual Property          *
*                      and is licensed under the                       *
*                  Common Public License, Version 1.0                  *
*                    by AT&T Intellectual Property                     *
*                                                                      *
*                A copy of the License is available at                 *
*            http://www.opensource.org/licenses/cpl1.0.txt             *
*         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
*                                                                      *
*              Information and Software Systems Research               *
*                            AT&T Research                             *
*                           Florham Park NJ                            *
*                                                                      *
*                 Glenn Fowler <gsf@research.att.com>                  *
*                  David Korn <dgk@research.att.com>                   *
*                   Phong Vo <kpv@research.att.com>                    *
*                                                                      *
***********************************************************************/
#pragma prototyped
/*
 * Glenn Fowler
 * AT&T Research
 *
 * scan s for tokens in fmt
 * s modified in place and not restored
 * if nxt!=0 then it will point to the first unread char in s
 * the number of scanned tokens is returned
 * -1 returned if s was not empty and fmt failed to match
 *
 * ' ' in fmt matches 0 or more {space,tab}
 * '\n' in fmt eats remainder of current line
 * "..." and '...' quotes interpreted
 * newline is equivalent to end of buf except when quoted
 * \\ quotes following char
 *
 * message support for %s and %v data
 *
 *	(5:12345)		fixed length strings, ) may be \t
 *	(null)			NiL
 *
 * "..." and '...' may span \n, and \\n is the line splice
 * quoted '\r' translated to '\n'
 * otherwise tokenizing is unconditionally terminated by '\n'
 *
 * a null arg pointer skips that arg
 *
 *	%c		char
 *	%[hl]d		[short|int|long] base 10
 *	%f		double
 *	%g		double
 *	%[hl]n		[short|int|long] C-style base
 *	%[hl]o		[short|int|long] base 8
 *	%s		string
 *	%[hl]u		same as %[hl]n
 *	%v		argv, elements
 *	%[hl]x		[short|int|long] base 16
 *
 * unmatched char args are set to "", int args to 0
 */

#include <ast.h>
#include <tok.h>

static char	empty[1];

/*
 * get one string token into p
 */

static char*
lextok(register char* s, register int c, char** p, int* n)
{
	register char*	t;
	register int	q;
	char*		b;
	char*		u;

	if (*s == '(' && (!c || c == ' ' || c == '\n'))
	{
		q = strtol(s + 1, &b, 10);
		if (*b == ':')
		{
			if (*(t = ++b + q) == ')' || *t == '\t')
			{
				s = t;
				*s++ = 0;
				goto end;
			}
		}
		else if (strneq(b, "null)", 5))
		{
			s = b + 5;
			b = 0;
			goto end;
		}
	}
	b = s;
	q = 0;
	t = 0;
	for (;;)
	{
		if (!*s || !q && *s == '\n')
		{
			if (!q)
			{
				if (!c || c == ' ' || c == '\n') (*n)++;
				else
				{
					s = b;
					b = empty;
					break;
				}
			}
			if (t) *t = 0;
			break;
		}
		else if (*s == '\\')
		{
			u = s;
			if (!*++s || *s == '\n' && (!*++s || *s == '\n')) continue;
			if (p)
			{
				if (b == u) b = s;
				else if (!t) t = u;
			}
		}
		else if (q)
		{
			if (*s == q)
			{
				q = 0;
				if (!t) t = s;
				s++;
				continue;
			}
			else if (*s == '\r') *s = '\n';
		}
		else if (*s == '"' || *s == '\'')
		{
			q = *s++;
			if (p)
			{
				if (b == (s - 1)) b = s;
				else if (!t) t = s - 1;
			}
			continue;
		}
		else if (*s == c || c == ' ' && *s == '\t')
		{
			*s++ = 0;
			if (t) *t = 0;
		end:
			if (c == ' ') while (*s == ' ' || *s == '\t') s++;
			(*n)++;
			break;
		}
		if (t) *t++ = *s;
		s++;
	}
	if (p) *p = b;
	return(s);
}

/*
 * scan entry
 */

int
tokscan(register char* s, char** nxt, const char* fmt, ...)
{
	register int	c;
	register char*	f;
	int		num = 0;
	char*		skip = 0;
	int		q;
	int		onum;
	long		val;
	double		dval;
	va_list		ap;
	char*		p_char;
	double*		p_double;
	int*		p_int;
	long*		p_long;
	short*		p_short;
	char**		p_string;
	char*		prv_f = 0;
	va_list		prv_ap;

	va_start(ap, fmt);
	if (!*s || *s == '\n')
	{
		skip = s;
		s = empty;
	}
	f = (char*)fmt;
	for (;;) switch (c = *f++)
	{
	case 0:
		if (f = prv_f)
		{
			prv_f = 0;
			/* prv_ap value is guarded by prv_f */
			va_copy(ap, prv_ap);
			continue;
		}
		goto done;
	case ' ':
		while (*s == ' ' || *s == '\t') s++;
		break;
	case '%':
		onum = num;
		switch (c = *f++)
		{
		case 'h':
		case 'l':
			q = c;
			c = *f++;
			break;
		default:
			q = 0;
			break;
		}
		switch (c)
		{
		case 0:
		case '%':
			f--;
			continue;
		case ':':
			prv_f = f;
			f = va_arg(ap, char*);
			va_copy(prv_ap, ap);
			va_copy(ap, va_listval(va_arg(ap, va_listarg)));
			continue;
		case 'c':
			p_char = va_arg(ap, char*);
			if (!(c = *s) || c == '\n')
			{
				if (p_char) *p_char = 0;
			}
			else
			{
				if (p_char) *p_char = c;
				s++;
				num++;
			}
			break;
		case 'd':
		case 'n':
		case 'o':
		case 'u':
		case 'x':
			switch (c)
			{
			case 'd':
				c = 10;
				break;
			case 'n':
			case 'u':
				c = 0;
				break;
			case 'o':
				c = 8;
				break;
			case 'x':
				c = 16;
				break;
			}
			if (!*s || *s == '\n')
			{
				val = 0;
				p_char = s;
			}
			else val = strtol(s, &p_char, c);
			switch (q)
			{
			case 'h':
				if (p_short = va_arg(ap, short*)) *p_short = (short)val;
				break;
			case 'l':
				if (p_long = va_arg(ap, long*)) *p_long = val;
				break;
			default:
				if (p_int = va_arg(ap, int*)) *p_int = (int)val;
				break;
			}
			if (s != p_char)
			{
				s = p_char;
				num++;
			}
			break;
		case 'f':
		case 'g':
			if (!*s || *s == '\n')
			{
				dval = 0;
				p_char = s;
			}
			else dval = strtod(s, &p_char);
			if (p_double = va_arg(ap, double*)) *p_double = dval;
			if (s != p_char)
			{
				s = p_char;
				num++;
			}
			break;
		case 's':
			p_string = va_arg(ap, char**);
			if (q = *f) f++;
			if (!*s || *s == '\n')
			{
				if (p_string) *p_string = s;
			}
			else s = lextok(s, q, p_string, &num);
			break;
		case 'v':
			p_string = va_arg(ap, char**);
			c = va_arg(ap, int);
			if (q = *f) f++;
			if ((!*s || *s == '\n') && p_string)
			{
				*p_string = 0;
				p_string = 0;
			}
			while (*s && *s != '\n' && --c > 0)
			{
				s = lextok(s, q, p_string, &num);
				if (p_string) p_string++;
			}
			if (p_string) *p_string = 0;
			break;
		}
		if (skip) num = onum;
		else if (num == onum)
		{
			if (!num) num = -1;
			skip = s;
			s = empty;
		}
		break;
	case '\n':
		goto done;
	default:
		if ((*s++ != c) && !skip)
		{
			skip = s - 1;
			s = empty;
		}
		break;
	}
 done:
	va_end(ap);
	if (*s == '\n') *s++ = 0;
	if (nxt) *nxt = skip ? skip : s;
	return(num);
}