xref: /netbsd-src/lib/libedit/tokenizer.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: tokenizer.c,v 1.14 2003/12/05 13:37:48 lukem Exp $	*/
2 
3 /*-
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Christos Zoulas of Cornell University.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include "config.h"
36 #if !defined(lint) && !defined(SCCSID)
37 #if 0
38 static char sccsid[] = "@(#)tokenizer.c	8.1 (Berkeley) 6/4/93";
39 #else
40 __RCSID("$NetBSD: tokenizer.c,v 1.14 2003/12/05 13:37:48 lukem Exp $");
41 #endif
42 #endif /* not lint && not SCCSID */
43 
44 /*
45  * tokenize.c: Bourne shell like tokenizer
46  */
47 #include <string.h>
48 #include <stdlib.h>
49 #include "histedit.h"
50 
51 typedef enum {
52 	Q_none, Q_single, Q_double, Q_one, Q_doubleone
53 } quote_t;
54 
55 #define	IFS		"\t \n"
56 
57 #define	TOK_KEEP	1
58 #define	TOK_EAT		2
59 
60 #define	WINCR		20
61 #define	AINCR		10
62 
63 #define	tok_strdup(a)		strdup(a)
64 #define	tok_malloc(a)		malloc(a)
65 #define	tok_free(a)		free(a)
66 #define	tok_realloc(a, b)	realloc(a, b)
67 
68 
69 struct tokenizer {
70 	char	*ifs;		/* In field separator			 */
71 	int	 argc, amax;	/* Current and maximum number of args	 */
72 	char   **argv;		/* Argument list			 */
73 	char	*wptr, *wmax;	/* Space and limit on the word buffer	 */
74 	char	*wstart;	/* Beginning of next word		 */
75 	char	*wspace;	/* Space of word buffer			 */
76 	quote_t	 quote;		/* Quoting state			 */
77 	int	 flags;		/* flags;				 */
78 };
79 
80 
81 private void tok_finish(Tokenizer *);
82 
83 
84 /* tok_finish():
85  *	Finish a word in the tokenizer.
86  */
87 private void
88 tok_finish(Tokenizer *tok)
89 {
90 
91 	*tok->wptr = '\0';
92 	if ((tok->flags & TOK_KEEP) || tok->wptr != tok->wstart) {
93 		tok->argv[tok->argc++] = tok->wstart;
94 		tok->argv[tok->argc] = NULL;
95 		tok->wstart = ++tok->wptr;
96 	}
97 	tok->flags &= ~TOK_KEEP;
98 }
99 
100 
101 /* tok_init():
102  *	Initialize the tokenizer
103  */
104 public Tokenizer *
105 tok_init(const char *ifs)
106 {
107 	Tokenizer *tok = (Tokenizer *) tok_malloc(sizeof(Tokenizer));
108 
109 	if (tok == NULL)
110 		return NULL;
111 	tok->ifs = tok_strdup(ifs ? ifs : IFS);
112 	if (tok->ifs == NULL) {
113 		tok_free((ptr_t)tok);
114 		return NULL;
115 	}
116 	tok->argc = 0;
117 	tok->amax = AINCR;
118 	tok->argv = (char **) tok_malloc(sizeof(char *) * tok->amax);
119 	if (tok->argv == NULL) {
120 		tok_free((ptr_t)tok->ifs);
121 		tok_free((ptr_t)tok);
122 		return NULL;
123 	}
124 	tok->argv[0] = NULL;
125 	tok->wspace = (char *) tok_malloc(WINCR);
126 	if (tok->wspace == NULL) {
127 		tok_free((ptr_t)tok->argv);
128 		tok_free((ptr_t)tok->ifs);
129 		tok_free((ptr_t)tok);
130 		return NULL;
131 	}
132 	tok->wmax = tok->wspace + WINCR;
133 	tok->wstart = tok->wspace;
134 	tok->wptr = tok->wspace;
135 	tok->flags = 0;
136 	tok->quote = Q_none;
137 
138 	return (tok);
139 }
140 
141 
142 /* tok_reset():
143  *	Reset the tokenizer
144  */
145 public void
146 tok_reset(Tokenizer *tok)
147 {
148 
149 	tok->argc = 0;
150 	tok->wstart = tok->wspace;
151 	tok->wptr = tok->wspace;
152 	tok->flags = 0;
153 	tok->quote = Q_none;
154 }
155 
156 
157 /* tok_end():
158  *	Clean up
159  */
160 public void
161 tok_end(Tokenizer *tok)
162 {
163 
164 	tok_free((ptr_t) tok->ifs);
165 	tok_free((ptr_t) tok->wspace);
166 	tok_free((ptr_t) tok->argv);
167 	tok_free((ptr_t) tok);
168 }
169 
170 
171 
172 /* tok_line():
173  *	Bourne shell (sh(1)) like tokenizing
174  *	Arguments:
175  *		tok	current tokenizer state (setup with tok_init())
176  *		line	line to parse
177  *	Returns:
178  *		-1	Internal error
179  *		 3	Quoted return
180  *		 2	Unmatched double quote
181  *		 1	Unmatched single quote
182  *		 0	Ok
183  *	Modifies (if return value is 0):
184  *		argc	number of arguments
185  *		argv	argument array
186  *		cursorc	if !NULL, argv element containing cursor
187  *		cursorv	if !NULL, offset in argv[cursorc] of cursor
188  */
189 public int
190 tok_line(Tokenizer *tok, const LineInfo *line,
191     int *argc, const char ***argv, int *cursorc, int *cursoro)
192 {
193 	const char *ptr;
194 	int cc, co;
195 
196 	cc = co = -1;
197 	ptr = line->buffer;
198 	for (ptr = line->buffer; ;ptr++) {
199 		if (ptr >= line->lastchar)
200 			ptr = "";
201 		if (ptr == line->cursor) {
202 			cc = tok->argc;
203 			co = tok->wptr - tok->wstart;
204 		}
205 		switch (*ptr) {
206 		case '\'':
207 			tok->flags |= TOK_KEEP;
208 			tok->flags &= ~TOK_EAT;
209 			switch (tok->quote) {
210 			case Q_none:
211 				tok->quote = Q_single;	/* Enter single quote
212 							 * mode */
213 				break;
214 
215 			case Q_single:	/* Exit single quote mode */
216 				tok->quote = Q_none;
217 				break;
218 
219 			case Q_one:	/* Quote this ' */
220 				tok->quote = Q_none;
221 				*tok->wptr++ = *ptr;
222 				break;
223 
224 			case Q_double:	/* Stay in double quote mode */
225 				*tok->wptr++ = *ptr;
226 				break;
227 
228 			case Q_doubleone:	/* Quote this ' */
229 				tok->quote = Q_double;
230 				*tok->wptr++ = *ptr;
231 				break;
232 
233 			default:
234 				return (-1);
235 			}
236 			break;
237 
238 		case '"':
239 			tok->flags &= ~TOK_EAT;
240 			tok->flags |= TOK_KEEP;
241 			switch (tok->quote) {
242 			case Q_none:	/* Enter double quote mode */
243 				tok->quote = Q_double;
244 				break;
245 
246 			case Q_double:	/* Exit double quote mode */
247 				tok->quote = Q_none;
248 				break;
249 
250 			case Q_one:	/* Quote this " */
251 				tok->quote = Q_none;
252 				*tok->wptr++ = *ptr;
253 				break;
254 
255 			case Q_single:	/* Stay in single quote mode */
256 				*tok->wptr++ = *ptr;
257 				break;
258 
259 			case Q_doubleone:	/* Quote this " */
260 				tok->quote = Q_double;
261 				*tok->wptr++ = *ptr;
262 				break;
263 
264 			default:
265 				return (-1);
266 			}
267 			break;
268 
269 		case '\\':
270 			tok->flags |= TOK_KEEP;
271 			tok->flags &= ~TOK_EAT;
272 			switch (tok->quote) {
273 			case Q_none:	/* Quote next character */
274 				tok->quote = Q_one;
275 				break;
276 
277 			case Q_double:	/* Quote next character */
278 				tok->quote = Q_doubleone;
279 				break;
280 
281 			case Q_one:	/* Quote this, restore state */
282 				*tok->wptr++ = *ptr;
283 				tok->quote = Q_none;
284 				break;
285 
286 			case Q_single:	/* Stay in single quote mode */
287 				*tok->wptr++ = *ptr;
288 				break;
289 
290 			case Q_doubleone:	/* Quote this \ */
291 				tok->quote = Q_double;
292 				*tok->wptr++ = *ptr;
293 				break;
294 
295 			default:
296 				return (-1);
297 			}
298 			break;
299 
300 		case '\n':
301 			tok->flags &= ~TOK_EAT;
302 			switch (tok->quote) {
303 			case Q_none:
304 				goto tok_line_outok;
305 
306 			case Q_single:
307 			case Q_double:
308 				*tok->wptr++ = *ptr;	/* Add the return */
309 				break;
310 
311 			case Q_doubleone:   /* Back to double, eat the '\n' */
312 				tok->flags |= TOK_EAT;
313 				tok->quote = Q_double;
314 				break;
315 
316 			case Q_one:	/* No quote, more eat the '\n' */
317 				tok->flags |= TOK_EAT;
318 				tok->quote = Q_none;
319 				break;
320 
321 			default:
322 				return (0);
323 			}
324 			break;
325 
326 		case '\0':
327 			switch (tok->quote) {
328 			case Q_none:
329 				/* Finish word and return */
330 				if (tok->flags & TOK_EAT) {
331 					tok->flags &= ~TOK_EAT;
332 					return (3);
333 				}
334 				goto tok_line_outok;
335 
336 			case Q_single:
337 				return (1);
338 
339 			case Q_double:
340 				return (2);
341 
342 			case Q_doubleone:
343 				tok->quote = Q_double;
344 				*tok->wptr++ = *ptr;
345 				break;
346 
347 			case Q_one:
348 				tok->quote = Q_none;
349 				*tok->wptr++ = *ptr;
350 				break;
351 
352 			default:
353 				return (-1);
354 			}
355 			break;
356 
357 		default:
358 			tok->flags &= ~TOK_EAT;
359 			switch (tok->quote) {
360 			case Q_none:
361 				if (strchr(tok->ifs, *ptr) != NULL)
362 					tok_finish(tok);
363 				else
364 					*tok->wptr++ = *ptr;
365 				break;
366 
367 			case Q_single:
368 			case Q_double:
369 				*tok->wptr++ = *ptr;
370 				break;
371 
372 
373 			case Q_doubleone:
374 				*tok->wptr++ = '\\';
375 				tok->quote = Q_double;
376 				*tok->wptr++ = *ptr;
377 				break;
378 
379 			case Q_one:
380 				tok->quote = Q_none;
381 				*tok->wptr++ = *ptr;
382 				break;
383 
384 			default:
385 				return (-1);
386 
387 			}
388 			break;
389 		}
390 
391 		if (tok->wptr >= tok->wmax - 4) {
392 			size_t size = tok->wmax - tok->wspace + WINCR;
393 			char *s = (char *) tok_realloc(tok->wspace, size);
394 			if (s == NULL)
395 				return (-1);
396 
397 			if (s != tok->wspace) {
398 				int i;
399 				for (i = 0; i < tok->argc; i++) {
400 				    tok->argv[i] =
401 					(tok->argv[i] - tok->wspace) + s;
402 				}
403 				tok->wptr = (tok->wptr - tok->wspace) + s;
404 				tok->wstart = (tok->wstart - tok->wspace) + s;
405 				tok->wspace = s;
406 			}
407 			tok->wmax = s + size;
408 		}
409 		if (tok->argc >= tok->amax - 4) {
410 			char **p;
411 			tok->amax += AINCR;
412 			p = (char **) tok_realloc(tok->argv,
413 			    tok->amax * sizeof(char *));
414 			if (p == NULL)
415 				return (-1);
416 			tok->argv = p;
417 		}
418 	}
419  tok_line_outok:
420 	if (cc == -1 && co == -1) {
421 		cc = tok->argc;
422 		co = tok->wptr - tok->wstart;
423 	}
424 	if (cursorc != NULL)
425 		*cursorc = cc;
426 	if (cursoro != NULL)
427 		*cursoro = co;
428 	tok_finish(tok);
429 	*argv = (const char **)tok->argv;
430 	*argc = tok->argc;
431 	return (0);
432 }
433 
434 /* tok_str():
435  *	Simpler version of tok_line, taking a NUL terminated line
436  *	and splitting into words, ignoring cursor state.
437  */
438 public int
439 tok_str(Tokenizer *tok, const char *line, int *argc, const char ***argv)
440 {
441 	LineInfo li;
442 
443 	memset(&li, 0, sizeof(li));
444 	li.buffer = line;
445 	li.cursor = li.lastchar = strchr(line, '\0');
446 	return (tok_line(tok, &li, argc, argv, NULL, NULL));
447 }
448