1 /* $NetBSD: tokenizer.c,v 1.19 2011/07/28 20:50:55 christos 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 #endif
41 #endif /* not lint && not SCCSID */
42
43 /* We build this file twice, once as NARROW, once as WIDE. */
44 /*
45 * tokenize.c: Bourne shell like tokenizer
46 */
47 #include <string.h>
48 #include <stdlib.h>
49 #include "histedit.h"
50 #include "chartype.h"
51
52 typedef enum {
53 Q_none, Q_single, Q_double, Q_one, Q_doubleone
54 } quote_t;
55
56 #define TOK_KEEP 1
57 #define TOK_EAT 2
58
59 #define WINCR 20
60 #define AINCR 10
61
62 #define IFS STR("\t \n")
63
64 #define tok_malloc(a) malloc(a)
65 #define tok_free(a) free(a)
66 #define tok_realloc(a, b) realloc(a, b)
67 #define tok_strdup(a) Strdup(a)
68
69
TYPE(tokenizer)70 struct TYPE(tokenizer) {
71 Char *ifs; /* In field separator */
72 int argc, amax; /* Current and maximum number of args */
73 Char **argv; /* Argument list */
74 Char *wptr, *wmax; /* Space and limit on the word buffer */
75 Char *wstart; /* Beginning of next word */
76 Char *wspace; /* Space of word buffer */
77 quote_t quote; /* Quoting state */
78 int flags; /* flags; */
79 };
80
81
82 private void FUN(tok,finish)(TYPE(Tokenizer) *);
83
84
85 /* FUN(tok,finish)():
86 * Finish a word in the tokenizer.
87 */
88 private void
FUN(tok,finish)89 FUN(tok,finish)(TYPE(Tokenizer) *tok)
90 {
91
92 *tok->wptr = '\0';
93 if ((tok->flags & TOK_KEEP) || tok->wptr != tok->wstart) {
94 tok->argv[tok->argc++] = tok->wstart;
95 tok->argv[tok->argc] = NULL;
96 tok->wstart = ++tok->wptr;
97 }
98 tok->flags &= ~TOK_KEEP;
99 }
100
101
102 /* FUN(tok,init)():
103 * Initialize the tokenizer
104 */
TYPE(Tokenizer)105 public TYPE(Tokenizer) *
106 FUN(tok,init)(const Char *ifs)
107 {
108 TYPE(Tokenizer) *tok = tok_malloc(sizeof(*tok));
109
110 if (tok == NULL)
111 return NULL;
112 tok->ifs = tok_strdup(ifs ? ifs : IFS);
113 if (tok->ifs == NULL) {
114 tok_free(tok);
115 return NULL;
116 }
117 tok->argc = 0;
118 tok->amax = AINCR;
119 tok->argv = tok_malloc(sizeof(*tok->argv) * tok->amax);
120 if (tok->argv == NULL) {
121 tok_free(tok->ifs);
122 tok_free(tok);
123 return NULL;
124 }
125 tok->argv[0] = NULL;
126 tok->wspace = tok_malloc(WINCR * sizeof(*tok->wspace));
127 if (tok->wspace == NULL) {
128 tok_free(tok->argv);
129 tok_free(tok->ifs);
130 tok_free(tok);
131 return NULL;
132 }
133 tok->wmax = tok->wspace + WINCR;
134 tok->wstart = tok->wspace;
135 tok->wptr = tok->wspace;
136 tok->flags = 0;
137 tok->quote = Q_none;
138
139 return (tok);
140 }
141
142
143 /* FUN(tok,reset)():
144 * Reset the tokenizer
145 */
146 public void
FUN(tok,reset)147 FUN(tok,reset)(TYPE(Tokenizer) *tok)
148 {
149
150 tok->argc = 0;
151 tok->wstart = tok->wspace;
152 tok->wptr = tok->wspace;
153 tok->flags = 0;
154 tok->quote = Q_none;
155 }
156
157
158 /* FUN(tok,end)():
159 * Clean up
160 */
161 public void
FUN(tok,end)162 FUN(tok,end)(TYPE(Tokenizer) *tok)
163 {
164
165 tok_free(tok->ifs);
166 tok_free(tok->wspace);
167 tok_free(tok->argv);
168 tok_free(tok);
169 }
170
171
172
173 /* FUN(tok,line)():
174 * Bourne shell (sh(1)) like tokenizing
175 * Arguments:
176 * tok current tokenizer state (setup with FUN(tok,init)())
177 * line line to parse
178 * Returns:
179 * -1 Internal error
180 * 3 Quoted return
181 * 2 Unmatched double quote
182 * 1 Unmatched single quote
183 * 0 Ok
184 * Modifies (if return value is 0):
185 * argc number of arguments
186 * argv argument array
187 * cursorc if !NULL, argv element containing cursor
188 * cursorv if !NULL, offset in argv[cursorc] of cursor
189 */
190 public int
FUN(tok,line)191 FUN(tok,line)(TYPE(Tokenizer) *tok, const TYPE(LineInfo) *line,
192 int *argc, const Char ***argv, int *cursorc, int *cursoro)
193 {
194 const Char *ptr;
195 int cc, co;
196
197 cc = co = -1;
198 ptr = line->buffer;
199 for (ptr = line->buffer; ;ptr++) {
200 if (ptr >= line->lastchar)
201 ptr = STR("");
202 if (ptr == line->cursor) {
203 cc = tok->argc;
204 co = (int)(tok->wptr - tok->wstart);
205 }
206 switch (*ptr) {
207 case '\'':
208 tok->flags |= TOK_KEEP;
209 tok->flags &= ~TOK_EAT;
210 switch (tok->quote) {
211 case Q_none:
212 tok->quote = Q_single; /* Enter single quote
213 * mode */
214 break;
215
216 case Q_single: /* Exit single quote mode */
217 tok->quote = Q_none;
218 break;
219
220 case Q_one: /* Quote this ' */
221 tok->quote = Q_none;
222 *tok->wptr++ = *ptr;
223 break;
224
225 case Q_double: /* Stay in double quote mode */
226 *tok->wptr++ = *ptr;
227 break;
228
229 case Q_doubleone: /* Quote this ' */
230 tok->quote = Q_double;
231 *tok->wptr++ = *ptr;
232 break;
233
234 default:
235 return (-1);
236 }
237 break;
238
239 case '"':
240 tok->flags &= ~TOK_EAT;
241 tok->flags |= TOK_KEEP;
242 switch (tok->quote) {
243 case Q_none: /* Enter double quote mode */
244 tok->quote = Q_double;
245 break;
246
247 case Q_double: /* Exit double quote mode */
248 tok->quote = Q_none;
249 break;
250
251 case Q_one: /* Quote this " */
252 tok->quote = Q_none;
253 *tok->wptr++ = *ptr;
254 break;
255
256 case Q_single: /* Stay in single quote mode */
257 *tok->wptr++ = *ptr;
258 break;
259
260 case Q_doubleone: /* Quote this " */
261 tok->quote = Q_double;
262 *tok->wptr++ = *ptr;
263 break;
264
265 default:
266 return (-1);
267 }
268 break;
269
270 case '\\':
271 tok->flags |= TOK_KEEP;
272 tok->flags &= ~TOK_EAT;
273 switch (tok->quote) {
274 case Q_none: /* Quote next character */
275 tok->quote = Q_one;
276 break;
277
278 case Q_double: /* Quote next character */
279 tok->quote = Q_doubleone;
280 break;
281
282 case Q_one: /* Quote this, restore state */
283 *tok->wptr++ = *ptr;
284 tok->quote = Q_none;
285 break;
286
287 case Q_single: /* Stay in single quote mode */
288 *tok->wptr++ = *ptr;
289 break;
290
291 case Q_doubleone: /* Quote this \ */
292 tok->quote = Q_double;
293 *tok->wptr++ = *ptr;
294 break;
295
296 default:
297 return (-1);
298 }
299 break;
300
301 case '\n':
302 tok->flags &= ~TOK_EAT;
303 switch (tok->quote) {
304 case Q_none:
305 goto tok_line_outok;
306
307 case Q_single:
308 case Q_double:
309 *tok->wptr++ = *ptr; /* Add the return */
310 break;
311
312 case Q_doubleone: /* Back to double, eat the '\n' */
313 tok->flags |= TOK_EAT;
314 tok->quote = Q_double;
315 break;
316
317 case Q_one: /* No quote, more eat the '\n' */
318 tok->flags |= TOK_EAT;
319 tok->quote = Q_none;
320 break;
321
322 default:
323 return (0);
324 }
325 break;
326
327 case '\0':
328 switch (tok->quote) {
329 case Q_none:
330 /* Finish word and return */
331 if (tok->flags & TOK_EAT) {
332 tok->flags &= ~TOK_EAT;
333 return (3);
334 }
335 goto tok_line_outok;
336
337 case Q_single:
338 return (1);
339
340 case Q_double:
341 return (2);
342
343 case Q_doubleone:
344 tok->quote = Q_double;
345 *tok->wptr++ = *ptr;
346 break;
347
348 case Q_one:
349 tok->quote = Q_none;
350 *tok->wptr++ = *ptr;
351 break;
352
353 default:
354 return (-1);
355 }
356 break;
357
358 default:
359 tok->flags &= ~TOK_EAT;
360 switch (tok->quote) {
361 case Q_none:
362 if (Strchr(tok->ifs, *ptr) != NULL)
363 FUN(tok,finish)(tok);
364 else
365 *tok->wptr++ = *ptr;
366 break;
367
368 case Q_single:
369 case Q_double:
370 *tok->wptr++ = *ptr;
371 break;
372
373
374 case Q_doubleone:
375 *tok->wptr++ = '\\';
376 tok->quote = Q_double;
377 *tok->wptr++ = *ptr;
378 break;
379
380 case Q_one:
381 tok->quote = Q_none;
382 *tok->wptr++ = *ptr;
383 break;
384
385 default:
386 return (-1);
387
388 }
389 break;
390 }
391
392 if (tok->wptr >= tok->wmax - 4) {
393 size_t size = tok->wmax - tok->wspace + WINCR;
394 Char *s = tok_realloc(tok->wspace,
395 size * sizeof(*s));
396 if (s == NULL)
397 return (-1);
398
399 if (s != tok->wspace) {
400 int i;
401 for (i = 0; i < tok->argc; i++) {
402 tok->argv[i] =
403 (tok->argv[i] - tok->wspace) + s;
404 }
405 tok->wptr = (tok->wptr - tok->wspace) + s;
406 tok->wstart = (tok->wstart - tok->wspace) + s;
407 tok->wspace = s;
408 }
409 tok->wmax = s + size;
410 }
411 if (tok->argc >= tok->amax - 4) {
412 Char **p;
413 tok->amax += AINCR;
414 p = tok_realloc(tok->argv, tok->amax * sizeof(*p));
415 if (p == NULL)
416 return (-1);
417 tok->argv = p;
418 }
419 }
420 tok_line_outok:
421 if (cc == -1 && co == -1) {
422 cc = tok->argc;
423 co = (int)(tok->wptr - tok->wstart);
424 }
425 if (cursorc != NULL)
426 *cursorc = cc;
427 if (cursoro != NULL)
428 *cursoro = co;
429 FUN(tok,finish)(tok);
430 *argv = (const Char **)tok->argv;
431 *argc = tok->argc;
432 return (0);
433 }
434
435 /* FUN(tok,str)():
436 * Simpler version of tok_line, taking a NUL terminated line
437 * and splitting into words, ignoring cursor state.
438 */
439 public int
FUN(tok,str)440 FUN(tok,str)(TYPE(Tokenizer) *tok, const Char *line, int *argc,
441 const Char ***argv)
442 {
443 TYPE(LineInfo) li;
444
445 memset(&li, 0, sizeof(li));
446 li.buffer = line;
447 li.cursor = li.lastchar = Strchr(line, '\0');
448 return (FUN(tok,line)(tok, &li, argc, argv, NULL, NULL));
449 }
450