1 /* $NetBSD: tokenizer.c,v 1.28 2016/04/11 18:56:31 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.28 2016/04/11 18:56:31 christos Exp $");
42 #endif /* not lint && not SCCSID */
44 /* We build this file twice, once as NARROW, once as WIDE. */
46 * tokenize.c: Bourne shell like tokenizer
54 Q_none, Q_single, Q_double, Q_one, Q_doubleone
63 #define IFS STR("\t \n")
65 #define tok_malloc(a) malloc(a)
66 #define tok_free(a) free(a)
67 #define tok_realloc(a, b) realloc(a, b)
71 #define FUN(prefix, rest) prefix ## _ ## rest
72 #define TYPE(type) type
74 #define Strchr(s, c) strchr(s, c)
75 #define tok_strdup(s) strdup(s)
78 #define FUN(prefix, rest) prefix ## _w ## rest
79 #define TYPE(type) type ## W
81 #define Strchr(s, c) wcschr(s, c)
82 #define tok_strdup(s) wcsdup(s)
85 struct TYPE(tokenizer) {
86 Char *ifs; /* In field separator */
87 size_t argc, amax; /* Current and maximum number of args */
88 Char **argv; /* Argument list */
89 Char *wptr, *wmax; /* Space and limit on the word buffer */
90 Char *wstart; /* Beginning of next word */
91 Char *wspace; /* Space of word buffer */
92 quote_t quote; /* Quoting state */
93 int flags; /* flags; */
97 static void FUN(tok,finish)(TYPE(Tokenizer) *);
100 /* FUN(tok,finish)():
101 * Finish a word in the tokenizer.
104 FUN(tok,finish)(TYPE(Tokenizer) *tok)
108 if ((tok->flags & TOK_KEEP) || tok->wptr != tok->wstart) {
109 tok->argv[tok->argc++] = tok->wstart;
110 tok->argv[tok->argc] = NULL;
111 tok->wstart = ++tok->wptr;
113 tok->flags &= ~TOK_KEEP;
118 * Initialize the tokenizer
121 FUN(tok,init)(const Char *ifs)
123 TYPE(Tokenizer) *tok = tok_malloc(sizeof(*tok));
127 tok->ifs = tok_strdup(ifs ? ifs : IFS);
128 if (tok->ifs == NULL) {
134 tok->argv = tok_malloc(sizeof(*tok->argv) * tok->amax);
135 if (tok->argv == NULL) {
141 tok->wspace = tok_malloc(WINCR * sizeof(*tok->wspace));
142 if (tok->wspace == NULL) {
148 tok->wmax = tok->wspace + WINCR;
149 tok->wstart = tok->wspace;
150 tok->wptr = tok->wspace;
159 * Reset the tokenizer
162 FUN(tok,reset)(TYPE(Tokenizer) *tok)
166 tok->wstart = tok->wspace;
167 tok->wptr = tok->wspace;
177 FUN(tok,end)(TYPE(Tokenizer) *tok)
181 tok_free(tok->wspace);
189 * Bourne shell (sh(1)) like tokenizing
191 * tok current tokenizer state (setup with FUN(tok,init)())
196 * 2 Unmatched double quote
197 * 1 Unmatched single quote
199 * Modifies (if return value is 0):
200 * argc number of arguments
201 * argv argument array
202 * cursorc if !NULL, argv element containing cursor
203 * cursorv if !NULL, offset in argv[cursorc] of cursor
206 FUN(tok,line)(TYPE(Tokenizer) *tok, const TYPE(LineInfo) *line,
207 int *argc, const Char ***argv, int *cursorc, int *cursoro)
214 for (ptr = line->buffer; ;ptr++) {
215 if (ptr >= line->lastchar)
217 if (ptr == line->cursor) {
219 co = (int)(tok->wptr - tok->wstart);
223 tok->flags |= TOK_KEEP;
224 tok->flags &= ~TOK_EAT;
225 switch (tok->quote) {
227 tok->quote = Q_single; /* Enter single quote
231 case Q_single: /* Exit single quote mode */
235 case Q_one: /* Quote this ' */
240 case Q_double: /* Stay in double quote mode */
244 case Q_doubleone: /* Quote this ' */
245 tok->quote = Q_double;
255 tok->flags &= ~TOK_EAT;
256 tok->flags |= TOK_KEEP;
257 switch (tok->quote) {
258 case Q_none: /* Enter double quote mode */
259 tok->quote = Q_double;
262 case Q_double: /* Exit double quote mode */
266 case Q_one: /* Quote this " */
271 case Q_single: /* Stay in single quote mode */
275 case Q_doubleone: /* Quote this " */
276 tok->quote = Q_double;
286 tok->flags |= TOK_KEEP;
287 tok->flags &= ~TOK_EAT;
288 switch (tok->quote) {
289 case Q_none: /* Quote next character */
293 case Q_double: /* Quote next character */
294 tok->quote = Q_doubleone;
297 case Q_one: /* Quote this, restore state */
302 case Q_single: /* Stay in single quote mode */
306 case Q_doubleone: /* Quote this \ */
307 tok->quote = Q_double;
317 tok->flags &= ~TOK_EAT;
318 switch (tok->quote) {
324 *tok->wptr++ = *ptr; /* Add the return */
327 case Q_doubleone: /* Back to double, eat the '\n' */
328 tok->flags |= TOK_EAT;
329 tok->quote = Q_double;
332 case Q_one: /* No quote, more eat the '\n' */
333 tok->flags |= TOK_EAT;
343 switch (tok->quote) {
345 /* Finish word and return */
346 if (tok->flags & TOK_EAT) {
347 tok->flags &= ~TOK_EAT;
359 tok->quote = Q_double;
374 tok->flags &= ~TOK_EAT;
375 switch (tok->quote) {
377 if (Strchr(tok->ifs, *ptr) != NULL)
378 FUN(tok,finish)(tok);
391 tok->quote = Q_double;
407 if (tok->wptr >= tok->wmax - 4) {
408 size_t size = (size_t)(tok->wmax - tok->wspace + WINCR);
409 Char *s = tok_realloc(tok->wspace,
414 if (s != tok->wspace) {
416 for (i = 0; i < tok->argc; i++) {
418 (tok->argv[i] - tok->wspace) + s;
420 tok->wptr = (tok->wptr - tok->wspace) + s;
421 tok->wstart = (tok->wstart - tok->wspace) + s;
424 tok->wmax = s + size;
426 if (tok->argc >= tok->amax - 4) {
429 p = tok_realloc(tok->argv, tok->amax * sizeof(*p));
438 if (cc == -1 && co == -1) {
440 co = (int)(tok->wptr - tok->wstart);
446 FUN(tok,finish)(tok);
447 *argv = (const Char **)tok->argv;
448 *argc = (int)tok->argc;
453 * Simpler version of tok_line, taking a NUL terminated line
454 * and splitting into words, ignoring cursor state.
457 FUN(tok,str)(TYPE(Tokenizer) *tok, const Char *line, int *argc,
462 memset(&li, 0, sizeof(li));
464 li.cursor = li.lastchar = Strchr(line, '\0');
465 return FUN(tok,line)(tok, &li, argc, argv, NULL, NULL);