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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)tokenizer.c 8.1 (Berkeley) 6/4/93
37 * $DragonFly: src/lib/libedit/tokenizer.c,v 1.4 2003/11/12 20:21:29 eirikn Exp $
41 * tokenize.c: Bourne shell like tokenizer
46 #include "tokenizer.h"
48 typedef enum { Q_none, Q_single, Q_double, Q_one, Q_doubleone } quote_t;
58 #define tok_malloc(a) malloc(a)
59 #define tok_free(a) free(a)
60 #define tok_realloc(a, b) realloc(a, b)
61 #define tok_reallocf(a, b) reallocf(a, b)
65 char *ifs; /* In field separator */
66 int argc, amax; /* Current and maximum number of args */
67 char **argv; /* Argument list */
68 char *wptr, *wmax; /* Space and limit on the word buffer */
69 char *wstart; /* Beginning of next word */
70 char *wspace; /* Space of word buffer */
71 quote_t quote; /* Quoting state */
72 int flags; /* flags; */
76 private void tok_finish (Tokenizer *);
80 * Finish a word in the tokenizer.
87 if ((tok->flags & TOK_KEEP) || tok->wptr != tok->wstart) {
88 tok->argv[tok->argc++] = tok->wstart;
89 tok->argv[tok->argc] = NULL;
90 tok->wstart = ++tok->wptr;
92 tok->flags &= ~TOK_KEEP;
97 * Initialize the tokenizer
103 Tokenizer* tok = (Tokenizer*) tok_malloc(sizeof(Tokenizer));
105 tok->ifs = strdup(ifs ? ifs : IFS);
108 tok->argv = (char **) tok_malloc(sizeof(char *) * tok->amax);
110 tok->wspace = (char *) tok_malloc(WINCR);
111 tok->wmax = tok->wspace + WINCR;
112 tok->wstart = tok->wspace;
113 tok->wptr = tok->wspace;
122 * Reset the tokenizer
129 tok->wstart = tok->wspace;
130 tok->wptr = tok->wspace;
143 tok_free((ptr_t) tok->ifs);
144 tok_free((ptr_t) tok->wspace);
145 tok_free((ptr_t) tok->argv);
146 tok_free((ptr_t) tok);
152 * Bourne shell like tokenizing
156 * 2: Unmatched double quote
157 * 1: Unmatched single quote
161 tok_line(tok, line, argc, argv)
170 switch (*(ptr = line++)) {
172 tok->flags |= TOK_KEEP;
173 tok->flags &= ~TOK_EAT;
174 switch (tok->quote) {
176 tok->quote = Q_single; /* Enter single quote mode */
179 case Q_single: /* Exit single quote mode */
183 case Q_one: /* Quote this ' */
188 case Q_double: /* Stay in double quote mode */
192 case Q_doubleone: /* Quote this ' */
193 tok->quote = Q_double;
203 tok->flags &= ~TOK_EAT;
204 tok->flags |= TOK_KEEP;
205 switch (tok->quote) {
206 case Q_none: /* Enter double quote mode */
207 tok->quote = Q_double;
211 tok->quote = Q_none; /* Exit double quote mode */
214 case Q_one: /* Quote this " */
219 case Q_single: /* Stay in single quote mode */
223 case Q_doubleone: /* Quote this " */
224 tok->quote = Q_double;
234 tok->flags |= TOK_KEEP;
235 tok->flags &= ~TOK_EAT;
236 switch (tok->quote) {
237 case Q_none: /* Quote next character */
242 tok->quote = Q_doubleone;/* Quote next character */
247 tok->quote = Q_none; /* Quote this, restore state */
250 case Q_single: /* Stay in single quote mode */
254 case Q_doubleone: /* Quote this \ */
255 tok->quote = Q_double;
265 tok->flags &= ~TOK_EAT;
266 switch (tok->quote) {
275 *tok->wptr++ = *ptr; /* Add the return */
279 tok->flags |= TOK_EAT;
280 tok->quote = Q_double; /* Back to double, eat the '\n' */
284 tok->flags |= TOK_EAT;
285 tok->quote = Q_none; /* No quote, more eat the '\n' */
294 switch (tok->quote) {
296 /* Finish word and return */
297 if (tok->flags & TOK_EAT) {
298 tok->flags &= ~TOK_EAT;
313 tok->quote = Q_double;
328 tok->flags &= ~TOK_EAT;
329 switch (tok->quote) {
331 if (strchr(tok->ifs, *ptr) != NULL)
345 tok->quote = Q_double;
361 if (tok->wptr >= tok->wmax - 4) {
362 size_t size = tok->wmax - tok->wspace + WINCR;
363 char *s = (char *) tok_realloc(tok->wspace, size);
365 int offs = s - tok->wspace;
369 for (i = 0; i < tok->argc; i++)
370 tok->argv[i] = tok->argv[i] + offs;
371 tok->wptr = tok->wptr + offs;
372 tok->wstart = tok->wstart + offs;
373 tok->wmax = s + size;
378 if (tok->argc >= tok->amax - 4) {
380 tok->argv = (char **) tok_reallocf(tok->argv,
381 tok->amax * sizeof(char*));