xref: /netbsd-src/lib/libedit/tokenizer.c (revision 5aefcfdc06931dd97e76246d2fe0302f7b3fe094)
1 /*	$NetBSD: tokenizer.c,v 1.6 2000/09/04 22:06:33 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. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 #if !defined(lint) && !defined(SCCSID)
41 #if 0
42 static char sccsid[] = "@(#)tokenizer.c	8.1 (Berkeley) 6/4/93";
43 #else
44 __RCSID("$NetBSD: tokenizer.c,v 1.6 2000/09/04 22:06:33 lukem Exp $");
45 #endif
46 #endif /* not lint && not SCCSID */
47 
48 /*
49  * tokenize.c: Bourne shell like tokenizer
50  */
51 #include "sys.h"
52 #include <string.h>
53 #include <stdlib.h>
54 #include "tokenizer.h"
55 
56 typedef enum {
57 	Q_none, Q_single, Q_double, Q_one, Q_doubleone
58 } quote_t;
59 
60 #define	IFS		"\t \n"
61 
62 #define	TOK_KEEP	1
63 #define	TOK_EAT		2
64 
65 #define	WINCR		20
66 #define	AINCR		10
67 
68 #define	tok_malloc(a)		malloc(a)
69 #define	tok_free(a)		free(a)
70 #define	tok_realloc(a, b)	realloc(a, b)
71 
72 
73 struct tokenizer {
74 	char	*ifs;		/* In field separator			 */
75 	int	 argc, amax;	/* Current and maximum number of args	 */
76 	char   **argv;		/* Argument list			 */
77 	char	*wptr, *wmax;	/* Space and limit on the word buffer	 */
78 	char	*wstart;	/* Beginning of next word		 */
79 	char	*wspace;	/* Space of word buffer			 */
80 	quote_t	 quote;		/* Quoting state			 */
81 	int	 flags;		/* flags;				 */
82 };
83 
84 
85 private void tok_finish(Tokenizer *);
86 
87 
88 /* tok_finish():
89  *	Finish a word in the tokenizer.
90  */
91 private void
92 tok_finish(Tokenizer *tok)
93 {
94 
95 	*tok->wptr = '\0';
96 	if ((tok->flags & TOK_KEEP) || tok->wptr != tok->wstart) {
97 		tok->argv[tok->argc++] = tok->wstart;
98 		tok->argv[tok->argc] = NULL;
99 		tok->wstart = ++tok->wptr;
100 	}
101 	tok->flags &= ~TOK_KEEP;
102 }
103 
104 
105 /* tok_init():
106  *	Initialize the tokenizer
107  */
108 public Tokenizer *
109 tok_init(const char *ifs)
110 {
111 	Tokenizer *tok = (Tokenizer *) tok_malloc(sizeof(Tokenizer));
112 
113 	tok->ifs = strdup(ifs ? ifs : IFS);
114 	tok->argc = 0;
115 	tok->amax = AINCR;
116 	tok->argv = (char **) tok_malloc(sizeof(char *) * tok->amax);
117 	tok->argv[0] = NULL;
118 	tok->wspace = (char *) tok_malloc(WINCR);
119 	tok->wmax = tok->wspace + WINCR;
120 	tok->wstart = tok->wspace;
121 	tok->wptr = tok->wspace;
122 	tok->flags = 0;
123 	tok->quote = Q_none;
124 
125 	return (tok);
126 }
127 
128 
129 /* tok_reset():
130  *	Reset the tokenizer
131  */
132 public void
133 tok_reset(Tokenizer *tok)
134 {
135 
136 	tok->argc = 0;
137 	tok->wstart = tok->wspace;
138 	tok->wptr = tok->wspace;
139 	tok->flags = 0;
140 	tok->quote = Q_none;
141 }
142 
143 
144 /* tok_end():
145  *	Clean up
146  */
147 public void
148 tok_end(Tokenizer *tok)
149 {
150 
151 	tok_free((ptr_t) tok->ifs);
152 	tok_free((ptr_t) tok->wspace);
153 	tok_free((ptr_t) tok->argv);
154 	tok_free((ptr_t) tok);
155 }
156 
157 
158 
159 /* tok_line():
160  *	Bourne shell like tokenizing
161  *	Return:
162  *		-1: Internal error
163  *		 3: Quoted return
164  *		 2: Unmatched double quote
165  *		 1: Unmatched single quote
166  *		 0: Ok
167  */
168 public int
169 tok_line(Tokenizer *tok, const char *line, int *argc, char ***argv)
170 {
171 	const char *ptr;
172 
173 	for (;;) {
174 		switch (*(ptr = line++)) {
175 		case '\'':
176 			tok->flags |= TOK_KEEP;
177 			tok->flags &= ~TOK_EAT;
178 			switch (tok->quote) {
179 			case Q_none:
180 				tok->quote = Q_single;	/* Enter single quote
181 							 * mode */
182 				break;
183 
184 			case Q_single:	/* Exit single quote mode */
185 				tok->quote = Q_none;
186 				break;
187 
188 			case Q_one:	/* Quote this ' */
189 				tok->quote = Q_none;
190 				*tok->wptr++ = *ptr;
191 				break;
192 
193 			case Q_double:	/* Stay in double quote mode */
194 				*tok->wptr++ = *ptr;
195 				break;
196 
197 			case Q_doubleone:	/* Quote this ' */
198 				tok->quote = Q_double;
199 				*tok->wptr++ = *ptr;
200 				break;
201 
202 			default:
203 				return (-1);
204 			}
205 			break;
206 
207 		case '"':
208 			tok->flags &= ~TOK_EAT;
209 			tok->flags |= TOK_KEEP;
210 			switch (tok->quote) {
211 			case Q_none:	/* Enter double quote mode */
212 				tok->quote = Q_double;
213 				break;
214 
215 			case Q_double:	/* Exit double 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_single:	/* Stay in single 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_KEEP;
240 			tok->flags &= ~TOK_EAT;
241 			switch (tok->quote) {
242 			case Q_none:	/* Quote next character */
243 				tok->quote = Q_one;
244 				break;
245 
246 			case Q_double:	/* Quote next character */
247 				tok->quote = Q_doubleone;
248 				break;
249 
250 			case Q_one:	/* Quote this, restore state */
251 				*tok->wptr++ = *ptr;
252 				tok->quote = Q_none;
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 '\n':
270 			tok->flags &= ~TOK_EAT;
271 			switch (tok->quote) {
272 			case Q_none:
273 				tok_finish(tok);
274 				*argv = tok->argv;
275 				*argc = tok->argc;
276 				return (0);
277 
278 			case Q_single:
279 			case Q_double:
280 				*tok->wptr++ = *ptr;	/* Add the return */
281 				break;
282 
283 			case Q_doubleone:   /* Back to double, eat the '\n' */
284 				tok->flags |= TOK_EAT;
285 				tok->quote = Q_double;
286 				break;
287 
288 			case Q_one:	/* No quote, more eat the '\n' */
289 				tok->flags |= TOK_EAT;
290 				tok->quote = Q_none;
291 				break;
292 
293 			default:
294 				return (0);
295 			}
296 			break;
297 
298 		case '\0':
299 			switch (tok->quote) {
300 			case Q_none:
301 				/* Finish word and return */
302 				if (tok->flags & TOK_EAT) {
303 					tok->flags &= ~TOK_EAT;
304 					return (3);
305 				}
306 				tok_finish(tok);
307 				*argv = tok->argv;
308 				*argc = tok->argc;
309 				return (0);
310 
311 			case Q_single:
312 				return (1);
313 
314 			case Q_double:
315 				return (2);
316 
317 			case Q_doubleone:
318 				tok->quote = Q_double;
319 				*tok->wptr++ = *ptr;
320 				break;
321 
322 			case Q_one:
323 				tok->quote = Q_none;
324 				*tok->wptr++ = *ptr;
325 				break;
326 
327 			default:
328 				return (-1);
329 			}
330 			break;
331 
332 		default:
333 			tok->flags &= ~TOK_EAT;
334 			switch (tok->quote) {
335 			case Q_none:
336 				if (strchr(tok->ifs, *ptr) != NULL)
337 					tok_finish(tok);
338 				else
339 					*tok->wptr++ = *ptr;
340 				break;
341 
342 			case Q_single:
343 			case Q_double:
344 				*tok->wptr++ = *ptr;
345 				break;
346 
347 
348 			case Q_doubleone:
349 				*tok->wptr++ = '\\';
350 				tok->quote = Q_double;
351 				*tok->wptr++ = *ptr;
352 				break;
353 
354 			case Q_one:
355 				tok->quote = Q_none;
356 				*tok->wptr++ = *ptr;
357 				break;
358 
359 			default:
360 				return (-1);
361 
362 			}
363 			break;
364 		}
365 
366 		if (tok->wptr >= tok->wmax - 4) {
367 			size_t size = tok->wmax - tok->wspace + WINCR;
368 			char *s = (char *) tok_realloc(tok->wspace, size);
369 			/* SUPPRESS 22 */
370 			int offs = s - tok->wspace;
371 
372 			if (offs != 0) {
373 				int i;
374 				for (i = 0; i < tok->argc; i++)
375 					tok->argv[i] = tok->argv[i] + offs;
376 				tok->wptr = tok->wptr + offs;
377 				tok->wstart = tok->wstart + offs;
378 				tok->wmax = s + size;
379 				tok->wspace = s;
380 			}
381 		}
382 		if (tok->argc >= tok->amax - 4) {
383 			tok->amax += AINCR;
384 			tok->argv = (char **) tok_realloc(tok->argv,
385 			    tok->amax * sizeof(char *));
386 		}
387 	}
388 }
389