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
37 #if !defined(lint) && !defined(SCCSID)
38 static char sccsid[] = "@(#)tokenizer.c 8.1 (Berkeley) 6/4/93";
39 #endif /* not lint && not SCCSID */
42 * tokenize.c: Bourne shell like tokenizer
47 #include "tokenizer.h"
49 typedef enum { Q_none, Q_single, Q_double, Q_one, Q_doubleone } quote_t;
59 #define tok_malloc(a) malloc(a)
60 #define tok_free(a) free(a)
61 #define tok_realloc(a, b) realloc(a, b)
62 #define tok_reallocf(a, b) reallocf(a, b)
66 char *ifs; /* In field separator */
67 int argc, amax; /* Current and maximum number of args */
68 char **argv; /* Argument list */
69 char *wptr, *wmax; /* Space and limit on the word buffer */
70 char *wstart; /* Beginning of next word */
71 char *wspace; /* Space of word buffer */
72 quote_t quote; /* Quoting state */
73 int flags; /* flags; */
77 private void tok_finish __P((Tokenizer *));
81 * Finish a word in the tokenizer.
88 if ((tok->flags & TOK_KEEP) || tok->wptr != tok->wstart) {
89 tok->argv[tok->argc++] = tok->wstart;
90 tok->argv[tok->argc] = NULL;
91 tok->wstart = ++tok->wptr;
93 tok->flags &= ~TOK_KEEP;
98 * Initialize the tokenizer
104 Tokenizer* tok = (Tokenizer*) tok_malloc(sizeof(Tokenizer));
106 tok->ifs = strdup(ifs ? ifs : IFS);
109 tok->argv = (char **) tok_malloc(sizeof(char *) * tok->amax);
111 tok->wspace = (char *) tok_malloc(WINCR);
112 tok->wmax = tok->wspace + WINCR;
113 tok->wstart = tok->wspace;
114 tok->wptr = tok->wspace;
123 * Reset the tokenizer
130 tok->wstart = tok->wspace;
131 tok->wptr = tok->wspace;
144 tok_free((ptr_t) tok->ifs);
145 tok_free((ptr_t) tok->wspace);
146 tok_free((ptr_t) tok->argv);
147 tok_free((ptr_t) tok);
153 * Bourne shell like tokenizing
157 * 2: Unmatched double quote
158 * 1: Unmatched single quote
162 tok_line(tok, line, argc, argv)
171 switch (*(ptr = line++)) {
173 tok->flags |= TOK_KEEP;
174 tok->flags &= ~TOK_EAT;
175 switch (tok->quote) {
177 tok->quote = Q_single; /* Enter single quote mode */
180 case Q_single: /* Exit single quote mode */
184 case Q_one: /* Quote this ' */
189 case Q_double: /* Stay in double quote mode */
193 case Q_doubleone: /* Quote this ' */
194 tok->quote = Q_double;
204 tok->flags &= ~TOK_EAT;
205 tok->flags |= TOK_KEEP;
206 switch (tok->quote) {
207 case Q_none: /* Enter double quote mode */
208 tok->quote = Q_double;
212 tok->quote = Q_none; /* Exit double quote mode */
215 case Q_one: /* Quote this " */
220 case Q_single: /* Stay in single quote mode */
224 case Q_doubleone: /* Quote this " */
225 tok->quote = Q_double;
235 tok->flags |= TOK_KEEP;
236 tok->flags &= ~TOK_EAT;
237 switch (tok->quote) {
238 case Q_none: /* Quote next character */
243 tok->quote = Q_doubleone;/* Quote next character */
248 tok->quote = Q_none; /* Quote this, restore state */
251 case Q_single: /* Stay in single quote mode */
255 case Q_doubleone: /* Quote this \ */
256 tok->quote = Q_double;
266 tok->flags &= ~TOK_EAT;
267 switch (tok->quote) {
276 *tok->wptr++ = *ptr; /* Add the return */
280 tok->flags |= TOK_EAT;
281 tok->quote = Q_double; /* Back to double, eat the '\n' */
285 tok->flags |= TOK_EAT;
286 tok->quote = Q_none; /* No quote, more eat the '\n' */
295 switch (tok->quote) {
297 /* Finish word and return */
298 if (tok->flags & TOK_EAT) {
299 tok->flags &= ~TOK_EAT;
314 tok->quote = Q_double;
329 tok->flags &= ~TOK_EAT;
330 switch (tok->quote) {
332 if (strchr(tok->ifs, *ptr) != NULL)
346 tok->quote = Q_double;
362 if (tok->wptr >= tok->wmax - 4) {
363 size_t size = tok->wmax - tok->wspace + WINCR;
364 char *s = (char *) tok_realloc(tok->wspace, size);
366 int offs = s - tok->wspace;
370 for (i = 0; i < tok->argc; i++)
371 tok->argv[i] = tok->argv[i] + offs;
372 tok->wptr = tok->wptr + offs;
373 tok->wstart = tok->wstart + offs;
374 tok->wmax = s + size;
379 if (tok->argc >= tok->amax - 4) {
381 tok->argv = (char **) tok_reallocf(tok->argv,
382 tok->amax * sizeof(char*));