1 /* $NetBSD: tokenizer.c,v 1.22 2016/01/30 04:02:51 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.22 2016/01/30 04:02:51 christos Exp $");
42 #endif /* not lint && not SCCSID */
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 /* We build this file twice, once as NARROW, once as WIDE. */
48 * tokenize.c: Bourne shell like tokenizer
56 Q_none, Q_single, Q_double, Q_one, Q_doubleone
65 #define IFS STR("\t \n")
67 #define tok_malloc(a) malloc(a)
68 #define tok_free(a) free(a)
69 #define tok_realloc(a, b) realloc(a, b)
70 #define tok_strdup(a) Strdup(a)
73 struct TYPE(tokenizer) {
74 Char *ifs; /* In field separator */
75 size_t argc, amax; /* Current and maximum number of args */
76 Char **argv; /* Argument list */
77 Char *wptr, *wmax; /* Space and limit on the word buffer */
78 Char *wstart; /* Beginning of next word */
79 Char *wspace; /* Space of word buffer */
80 quote_t quote; /* Quoting state */
81 int flags; /* flags; */
85 private void FUN(tok,finish)(TYPE(Tokenizer) *);
89 * Finish a word in the tokenizer.
92 FUN(tok,finish)(TYPE(Tokenizer) *tok)
96 if ((tok->flags & TOK_KEEP) || tok->wptr != tok->wstart) {
97 tok->argv[tok->argc++] = tok->wstart;
98 tok->argv[tok->argc] = NULL;
99 tok->wstart = ++tok->wptr;
101 tok->flags &= ~TOK_KEEP;
106 * Initialize the tokenizer
108 public TYPE(Tokenizer) *
109 FUN(tok,init)(const Char *ifs)
111 TYPE(Tokenizer) *tok = tok_malloc(sizeof(*tok));
115 tok->ifs = tok_strdup(ifs ? ifs : IFS);
116 if (tok->ifs == NULL) {
122 tok->argv = tok_malloc(sizeof(*tok->argv) * tok->amax);
123 if (tok->argv == NULL) {
129 tok->wspace = tok_malloc(WINCR * sizeof(*tok->wspace));
130 if (tok->wspace == NULL) {
136 tok->wmax = tok->wspace + WINCR;
137 tok->wstart = tok->wspace;
138 tok->wptr = tok->wspace;
147 * Reset the tokenizer
150 FUN(tok,reset)(TYPE(Tokenizer) *tok)
154 tok->wstart = tok->wspace;
155 tok->wptr = tok->wspace;
165 FUN(tok,end)(TYPE(Tokenizer) *tok)
169 tok_free(tok->wspace);
177 * Bourne shell (sh(1)) like tokenizing
179 * tok current tokenizer state (setup with FUN(tok,init)())
184 * 2 Unmatched double quote
185 * 1 Unmatched single quote
187 * Modifies (if return value is 0):
188 * argc number of arguments
189 * argv argument array
190 * cursorc if !NULL, argv element containing cursor
191 * cursorv if !NULL, offset in argv[cursorc] of cursor
194 FUN(tok,line)(TYPE(Tokenizer) *tok, const TYPE(LineInfo) *line,
195 int *argc, const Char ***argv, int *cursorc, int *cursoro)
202 for (ptr = line->buffer; ;ptr++) {
203 if (ptr >= line->lastchar)
205 if (ptr == line->cursor) {
207 co = (int)(tok->wptr - tok->wstart);
211 tok->flags |= TOK_KEEP;
212 tok->flags &= ~TOK_EAT;
213 switch (tok->quote) {
215 tok->quote = Q_single; /* Enter single quote
219 case Q_single: /* Exit single quote mode */
223 case Q_one: /* Quote this ' */
228 case Q_double: /* Stay in double quote mode */
232 case Q_doubleone: /* Quote this ' */
233 tok->quote = Q_double;
243 tok->flags &= ~TOK_EAT;
244 tok->flags |= TOK_KEEP;
245 switch (tok->quote) {
246 case Q_none: /* Enter double quote mode */
247 tok->quote = Q_double;
250 case Q_double: /* Exit double quote mode */
254 case Q_one: /* Quote this " */
259 case Q_single: /* Stay in single quote mode */
263 case Q_doubleone: /* Quote this " */
264 tok->quote = Q_double;
274 tok->flags |= TOK_KEEP;
275 tok->flags &= ~TOK_EAT;
276 switch (tok->quote) {
277 case Q_none: /* Quote next character */
281 case Q_double: /* Quote next character */
282 tok->quote = Q_doubleone;
285 case Q_one: /* Quote this, restore state */
290 case Q_single: /* Stay in single quote mode */
294 case Q_doubleone: /* Quote this \ */
295 tok->quote = Q_double;
305 tok->flags &= ~TOK_EAT;
306 switch (tok->quote) {
312 *tok->wptr++ = *ptr; /* Add the return */
315 case Q_doubleone: /* Back to double, eat the '\n' */
316 tok->flags |= TOK_EAT;
317 tok->quote = Q_double;
320 case Q_one: /* No quote, more eat the '\n' */
321 tok->flags |= TOK_EAT;
331 switch (tok->quote) {
333 /* Finish word and return */
334 if (tok->flags & TOK_EAT) {
335 tok->flags &= ~TOK_EAT;
347 tok->quote = Q_double;
362 tok->flags &= ~TOK_EAT;
363 switch (tok->quote) {
365 if (Strchr(tok->ifs, *ptr) != NULL)
366 FUN(tok,finish)(tok);
379 tok->quote = Q_double;
395 if (tok->wptr >= tok->wmax - 4) {
396 size_t size = (size_t)(tok->wmax - tok->wspace + WINCR);
397 Char *s = tok_realloc(tok->wspace,
402 if (s != tok->wspace) {
404 for (i = 0; i < tok->argc; i++) {
406 (tok->argv[i] - tok->wspace) + s;
408 tok->wptr = (tok->wptr - tok->wspace) + s;
409 tok->wstart = (tok->wstart - tok->wspace) + s;
412 tok->wmax = s + size;
414 if (tok->argc >= tok->amax - 4) {
417 p = tok_realloc(tok->argv, tok->amax * sizeof(*p));
424 if (cc == -1 && co == -1) {
426 co = (int)(tok->wptr - tok->wstart);
432 FUN(tok,finish)(tok);
433 *argv = (const Char **)tok->argv;
434 *argc = (int)tok->argc;
439 * Simpler version of tok_line, taking a NUL terminated line
440 * and splitting into words, ignoring cursor state.
443 FUN(tok,str)(TYPE(Tokenizer) *tok, const Char *line, int *argc,
448 memset(&li, 0, sizeof(li));
450 li.cursor = li.lastchar = Strchr(line, '\0');
451 return FUN(tok,line)(tok, &li, argc, argv, NULL, NULL);