2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Christos Zoulas of Cornell University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $NetBSD: tokenizer.c,v 1.14 2003/12/05 13:37:48 lukem Exp $
35 #if !defined(lint) && !defined(SCCSID)
36 static char sccsid[] = "@(#)tokenizer.c 8.1 (Berkeley) 6/4/93";
37 #endif /* not lint && not SCCSID */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * tokenize.c: Bourne shell like tokenizer
50 Q_none, Q_single, Q_double, Q_one, Q_doubleone
61 #define tok_strdup(a) strdup(a)
62 #define tok_malloc(a) malloc(a)
63 #define tok_free(a) free(a)
64 #define tok_realloc(a, b) realloc(a, b)
68 char *ifs; /* In field separator */
69 int argc, amax; /* Current and maximum number of args */
70 char **argv; /* Argument list */
71 char *wptr, *wmax; /* Space and limit on the word buffer */
72 char *wstart; /* Beginning of next word */
73 char *wspace; /* Space of word buffer */
74 quote_t quote; /* Quoting state */
75 int flags; /* flags; */
79 private void tok_finish(Tokenizer *);
83 * Finish a word in the tokenizer.
86 tok_finish(Tokenizer *tok)
90 if ((tok->flags & TOK_KEEP) || tok->wptr != tok->wstart) {
91 tok->argv[tok->argc++] = tok->wstart;
92 tok->argv[tok->argc] = NULL;
93 tok->wstart = ++tok->wptr;
95 tok->flags &= ~TOK_KEEP;
100 * Initialize the tokenizer
103 tok_init(const char *ifs)
105 Tokenizer *tok = (Tokenizer *) tok_malloc(sizeof(Tokenizer));
109 tok->ifs = tok_strdup(ifs ? ifs : IFS);
110 if (tok->ifs == NULL) {
111 tok_free((ptr_t)tok);
116 tok->argv = (char **) tok_malloc(sizeof(char *) * tok->amax);
117 if (tok->argv == NULL) {
118 tok_free((ptr_t)tok->ifs);
119 tok_free((ptr_t)tok);
123 tok->wspace = (char *) tok_malloc(WINCR);
124 if (tok->wspace == NULL) {
125 tok_free((ptr_t)tok->argv);
126 tok_free((ptr_t)tok->ifs);
127 tok_free((ptr_t)tok);
130 tok->wmax = tok->wspace + WINCR;
131 tok->wstart = tok->wspace;
132 tok->wptr = tok->wspace;
141 * Reset the tokenizer
144 tok_reset(Tokenizer *tok)
148 tok->wstart = tok->wspace;
149 tok->wptr = tok->wspace;
159 tok_end(Tokenizer *tok)
162 tok_free((ptr_t) tok->ifs);
163 tok_free((ptr_t) tok->wspace);
164 tok_free((ptr_t) tok->argv);
165 tok_free((ptr_t) tok);
171 * Bourne shell (sh(1)) like tokenizing
173 * tok current tokenizer state (setup with tok_init())
178 * 2 Unmatched double quote
179 * 1 Unmatched single quote
181 * Modifies (if return value is 0):
182 * argc number of arguments
183 * argv argument array
184 * cursorc if !NULL, argv element containing cursor
185 * cursorv if !NULL, offset in argv[cursorc] of cursor
188 tok_line(Tokenizer *tok, const LineInfo *line,
189 int *argc, const char ***argv, int *cursorc, int *cursoro)
196 for (ptr = line->buffer; ;ptr++) {
197 if (ptr >= line->lastchar)
199 if (ptr == line->cursor) {
201 co = tok->wptr - tok->wstart;
205 tok->flags |= TOK_KEEP;
206 tok->flags &= ~TOK_EAT;
207 switch (tok->quote) {
209 tok->quote = Q_single; /* Enter single quote
213 case Q_single: /* Exit single quote mode */
217 case Q_one: /* Quote this ' */
222 case Q_double: /* Stay in double quote mode */
226 case Q_doubleone: /* Quote this ' */
227 tok->quote = Q_double;
237 tok->flags &= ~TOK_EAT;
238 tok->flags |= TOK_KEEP;
239 switch (tok->quote) {
240 case Q_none: /* Enter double quote mode */
241 tok->quote = Q_double;
244 case Q_double: /* Exit double quote mode */
248 case Q_one: /* Quote this " */
253 case Q_single: /* Stay in single quote mode */
257 case Q_doubleone: /* Quote this " */
258 tok->quote = Q_double;
268 tok->flags |= TOK_KEEP;
269 tok->flags &= ~TOK_EAT;
270 switch (tok->quote) {
271 case Q_none: /* Quote next character */
275 case Q_double: /* Quote next character */
276 tok->quote = Q_doubleone;
279 case Q_one: /* Quote this, restore state */
284 case Q_single: /* Stay in single quote mode */
288 case Q_doubleone: /* Quote this \ */
289 tok->quote = Q_double;
299 tok->flags &= ~TOK_EAT;
300 switch (tok->quote) {
306 *tok->wptr++ = *ptr; /* Add the return */
309 case Q_doubleone: /* Back to double, eat the '\n' */
310 tok->flags |= TOK_EAT;
311 tok->quote = Q_double;
314 case Q_one: /* No quote, more eat the '\n' */
315 tok->flags |= TOK_EAT;
325 switch (tok->quote) {
327 /* Finish word and return */
328 if (tok->flags & TOK_EAT) {
329 tok->flags &= ~TOK_EAT;
341 tok->quote = Q_double;
356 tok->flags &= ~TOK_EAT;
357 switch (tok->quote) {
359 if (strchr(tok->ifs, *ptr) != NULL)
373 tok->quote = Q_double;
389 if (tok->wptr >= tok->wmax - 4) {
390 size_t size = tok->wmax - tok->wspace + WINCR;
391 char *s = (char *) tok_realloc(tok->wspace, size);
395 if (s != tok->wspace) {
397 for (i = 0; i < tok->argc; i++) {
399 (tok->argv[i] - tok->wspace) + s;
401 tok->wptr = (tok->wptr - tok->wspace) + s;
402 tok->wstart = (tok->wstart - tok->wspace) + s;
405 tok->wmax = s + size;
407 if (tok->argc >= tok->amax - 4) {
410 p = (char **) tok_realloc(tok->argv,
411 tok->amax * sizeof(char *));
418 if (cc == -1 && co == -1) {
420 co = tok->wptr - tok->wstart;
427 *argv = (const char **)tok->argv;
433 * Simpler version of tok_line, taking a NUL terminated line
434 * and splitting into words, ignoring cursor state.
437 tok_str(Tokenizer *tok, const char *line, int *argc, const char ***argv)
441 memset(&li, 0, sizeof(li));
443 li.cursor = li.lastchar = strchr(line, '\0');
444 return (tok_line(tok, &li, argc, argv, NULL, NULL));