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