1 /* $NetBSD: tokenizer.c,v 1.15 2009/02/15 21:55:23 christos Exp $ */
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Christos Zoulas of Cornell University.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #if !defined(lint) && !defined(SCCSID)
38 static char sccsid[] = "@(#)tokenizer.c 8.1 (Berkeley) 6/4/93";
40 __RCSID("$NetBSD: tokenizer.c,v 1.15 2009/02/15 21:55:23 christos Exp $");
42 #endif /* not lint && not SCCSID */
45 * tokenize.c: Bourne shell like tokenizer
52 Q_none, Q_single, Q_double, Q_one, Q_doubleone
63 #define tok_strdup(a) strdup(a)
64 #define tok_malloc(a) malloc(a)
65 #define tok_free(a) free(a)
66 #define tok_realloc(a, b) realloc(a, b)
70 char *ifs; /* In field separator */
71 int argc, amax; /* Current and maximum number of args */
72 char **argv; /* Argument list */
73 char *wptr, *wmax; /* Space and limit on the word buffer */
74 char *wstart; /* Beginning of next word */
75 char *wspace; /* Space of word buffer */
76 quote_t quote; /* Quoting state */
77 int flags; /* flags; */
81 private void tok_finish(Tokenizer *);
85 * Finish a word in the tokenizer.
88 tok_finish(Tokenizer *tok)
92 if ((tok->flags & TOK_KEEP) || tok->wptr != tok->wstart) {
93 tok->argv[tok->argc++] = tok->wstart;
94 tok->argv[tok->argc] = NULL;
95 tok->wstart = ++tok->wptr;
97 tok->flags &= ~TOK_KEEP;
102 * Initialize the tokenizer
105 tok_init(const char *ifs)
107 Tokenizer *tok = (Tokenizer *) tok_malloc(sizeof(Tokenizer));
111 tok->ifs = tok_strdup(ifs ? ifs : IFS);
112 if (tok->ifs == NULL) {
113 tok_free((ptr_t)tok);
118 tok->argv = (char **) tok_malloc(sizeof(char *) * tok->amax);
119 if (tok->argv == NULL) {
120 tok_free((ptr_t)tok->ifs);
121 tok_free((ptr_t)tok);
125 tok->wspace = (char *) tok_malloc(WINCR);
126 if (tok->wspace == NULL) {
127 tok_free((ptr_t)tok->argv);
128 tok_free((ptr_t)tok->ifs);
129 tok_free((ptr_t)tok);
132 tok->wmax = tok->wspace + WINCR;
133 tok->wstart = tok->wspace;
134 tok->wptr = tok->wspace;
143 * Reset the tokenizer
146 tok_reset(Tokenizer *tok)
150 tok->wstart = tok->wspace;
151 tok->wptr = tok->wspace;
161 tok_end(Tokenizer *tok)
164 tok_free((ptr_t) tok->ifs);
165 tok_free((ptr_t) tok->wspace);
166 tok_free((ptr_t) tok->argv);
167 tok_free((ptr_t) tok);
173 * Bourne shell (sh(1)) like tokenizing
175 * tok current tokenizer state (setup with tok_init())
180 * 2 Unmatched double quote
181 * 1 Unmatched single quote
183 * Modifies (if return value is 0):
184 * argc number of arguments
185 * argv argument array
186 * cursorc if !NULL, argv element containing cursor
187 * cursorv if !NULL, offset in argv[cursorc] of cursor
190 tok_line(Tokenizer *tok, const LineInfo *line,
191 int *argc, const char ***argv, int *cursorc, int *cursoro)
198 for (ptr = line->buffer; ;ptr++) {
199 if (ptr >= line->lastchar)
201 if (ptr == line->cursor) {
203 co = (int)(tok->wptr - tok->wstart);
207 tok->flags |= TOK_KEEP;
208 tok->flags &= ~TOK_EAT;
209 switch (tok->quote) {
211 tok->quote = Q_single; /* Enter single quote
215 case Q_single: /* Exit single quote mode */
219 case Q_one: /* Quote this ' */
224 case Q_double: /* Stay in double quote mode */
228 case Q_doubleone: /* Quote this ' */
229 tok->quote = Q_double;
239 tok->flags &= ~TOK_EAT;
240 tok->flags |= TOK_KEEP;
241 switch (tok->quote) {
242 case Q_none: /* Enter double quote mode */
243 tok->quote = Q_double;
246 case Q_double: /* Exit double quote mode */
250 case Q_one: /* Quote this " */
255 case Q_single: /* Stay in single quote mode */
259 case Q_doubleone: /* Quote this " */
260 tok->quote = Q_double;
270 tok->flags |= TOK_KEEP;
271 tok->flags &= ~TOK_EAT;
272 switch (tok->quote) {
273 case Q_none: /* Quote next character */
277 case Q_double: /* Quote next character */
278 tok->quote = Q_doubleone;
281 case Q_one: /* Quote this, restore state */
286 case Q_single: /* Stay in single quote mode */
290 case Q_doubleone: /* Quote this \ */
291 tok->quote = Q_double;
301 tok->flags &= ~TOK_EAT;
302 switch (tok->quote) {
308 *tok->wptr++ = *ptr; /* Add the return */
311 case Q_doubleone: /* Back to double, eat the '\n' */
312 tok->flags |= TOK_EAT;
313 tok->quote = Q_double;
316 case Q_one: /* No quote, more eat the '\n' */
317 tok->flags |= TOK_EAT;
327 switch (tok->quote) {
329 /* Finish word and return */
330 if (tok->flags & TOK_EAT) {
331 tok->flags &= ~TOK_EAT;
343 tok->quote = Q_double;
358 tok->flags &= ~TOK_EAT;
359 switch (tok->quote) {
361 if (strchr(tok->ifs, *ptr) != NULL)
375 tok->quote = Q_double;
391 if (tok->wptr >= tok->wmax - 4) {
392 size_t size = tok->wmax - tok->wspace + WINCR;
393 char *s = (char *) tok_realloc(tok->wspace, size);
397 if (s != tok->wspace) {
399 for (i = 0; i < tok->argc; i++) {
401 (tok->argv[i] - tok->wspace) + s;
403 tok->wptr = (tok->wptr - tok->wspace) + s;
404 tok->wstart = (tok->wstart - tok->wspace) + s;
407 tok->wmax = s + size;
409 if (tok->argc >= tok->amax - 4) {
412 p = (char **) tok_realloc(tok->argv,
413 tok->amax * sizeof(char *));
420 if (cc == -1 && co == -1) {
422 co = (int)(tok->wptr - tok->wstart);
429 *argv = (const char **)tok->argv;
435 * Simpler version of tok_line, taking a NUL terminated line
436 * and splitting into words, ignoring cursor state.
439 tok_str(Tokenizer *tok, const char *line, int *argc, const char ***argv)
443 memset(&li, 0, sizeof(li));
445 li.cursor = li.lastchar = strchr(line, '\0');
446 return (tok_line(tok, &li, argc, argv, NULL, NULL));