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