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 * $NetBSD: tokenizer.c,v 1.6 2000/09/04 22:06:33 lukem Exp $
39 #if !defined(lint) && !defined(SCCSID)
40 static char sccsid[] = "@(#)tokenizer.c 8.1 (Berkeley) 6/4/93";
41 #endif /* not lint && not SCCSID */
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
46 * tokenize.c: Bourne shell like tokenizer
51 #include "tokenizer.h"
54 Q_none, Q_single, Q_double, Q_one, Q_doubleone
65 #define tok_malloc(a) malloc(a)
66 #define tok_free(a) free(a)
67 #define tok_realloc(a, b) realloc(a, b)
68 #define tok_reallocf(a, b) reallocf(a, b)
72 char *ifs; /* In field separator */
73 int argc, amax; /* Current and maximum number of args */
74 char **argv; /* Argument list */
75 char *wptr, *wmax; /* Space and limit on the word buffer */
76 char *wstart; /* Beginning of next word */
77 char *wspace; /* Space of word buffer */
78 quote_t quote; /* Quoting state */
79 int flags; /* flags; */
83 private void tok_finish(Tokenizer *);
87 * Finish a word in the tokenizer.
90 tok_finish(Tokenizer *tok)
94 if ((tok->flags & TOK_KEEP) || tok->wptr != tok->wstart) {
95 tok->argv[tok->argc++] = tok->wstart;
96 tok->argv[tok->argc] = NULL;
97 tok->wstart = ++tok->wptr;
99 tok->flags &= ~TOK_KEEP;
104 * Initialize the tokenizer
107 tok_init(const char *ifs)
109 Tokenizer *tok = (Tokenizer *) tok_malloc(sizeof(Tokenizer));
111 tok->ifs = strdup(ifs ? ifs : IFS);
114 tok->argv = (char **) tok_malloc(sizeof(char *) * tok->amax);
115 if (tok->argv == NULL)
118 tok->wspace = (char *) tok_malloc(WINCR);
119 if (tok->wspace == NULL)
121 tok->wmax = tok->wspace + WINCR;
122 tok->wstart = tok->wspace;
123 tok->wptr = tok->wspace;
132 * Reset the tokenizer
135 tok_reset(Tokenizer *tok)
139 tok->wstart = tok->wspace;
140 tok->wptr = tok->wspace;
150 tok_end(Tokenizer *tok)
153 tok_free((ptr_t) tok->ifs);
154 tok_free((ptr_t) tok->wspace);
155 tok_free((ptr_t) tok->argv);
156 tok_free((ptr_t) tok);
162 * Bourne shell like tokenizing
166 * 2: Unmatched double quote
167 * 1: Unmatched single quote
171 tok_line(Tokenizer *tok, const char *line, int *argc, char ***argv)
176 switch (*(ptr = line++)) {
178 tok->flags |= TOK_KEEP;
179 tok->flags &= ~TOK_EAT;
180 switch (tok->quote) {
182 tok->quote = Q_single; /* Enter single quote
186 case Q_single: /* Exit single quote mode */
190 case Q_one: /* Quote this ' */
195 case Q_double: /* Stay in double quote mode */
199 case Q_doubleone: /* Quote this ' */
200 tok->quote = Q_double;
210 tok->flags &= ~TOK_EAT;
211 tok->flags |= TOK_KEEP;
212 switch (tok->quote) {
213 case Q_none: /* Enter double quote mode */
214 tok->quote = Q_double;
217 case Q_double: /* Exit double quote mode */
221 case Q_one: /* Quote this " */
226 case Q_single: /* Stay in single quote mode */
230 case Q_doubleone: /* Quote this " */
231 tok->quote = Q_double;
241 tok->flags |= TOK_KEEP;
242 tok->flags &= ~TOK_EAT;
243 switch (tok->quote) {
244 case Q_none: /* Quote next character */
248 case Q_double: /* Quote next character */
249 tok->quote = Q_doubleone;
252 case Q_one: /* Quote this, restore state */
257 case Q_single: /* Stay in single quote mode */
261 case Q_doubleone: /* Quote this \ */
262 tok->quote = Q_double;
272 tok->flags &= ~TOK_EAT;
273 switch (tok->quote) {
282 *tok->wptr++ = *ptr; /* Add the return */
285 case Q_doubleone: /* Back to double, eat the '\n' */
286 tok->flags |= TOK_EAT;
287 tok->quote = Q_double;
290 case Q_one: /* No quote, more eat the '\n' */
291 tok->flags |= TOK_EAT;
301 switch (tok->quote) {
303 /* Finish word and return */
304 if (tok->flags & TOK_EAT) {
305 tok->flags &= ~TOK_EAT;
320 tok->quote = Q_double;
335 tok->flags &= ~TOK_EAT;
336 switch (tok->quote) {
338 if (strchr(tok->ifs, *ptr) != NULL)
352 tok->quote = Q_double;
368 if (tok->wptr >= tok->wmax - 4) {
369 size_t size = tok->wmax - tok->wspace + WINCR;
370 char *s = (char *) tok_realloc(tok->wspace, size);
372 int offs = s - tok->wspace;
378 for (i = 0; i < tok->argc; i++)
379 tok->argv[i] = tok->argv[i] + offs;
380 tok->wptr = tok->wptr + offs;
381 tok->wstart = tok->wstart + offs;
382 tok->wmax = s + size;
386 if (tok->argc >= tok->amax - 4) {
389 p = (char **) tok_reallocf(tok->argv,
390 tok->amax * sizeof(char *));