1 /* $NetBSD: tokenizer.c,v 1.21 2011/08/16 16:25:15 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.21 2011/08/16 16:25:15 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)
68 #define tok_strdup(a) Strdup(a)
71 struct TYPE(tokenizer
) {
72 Char
*ifs
; /* In field separator */
73 size_t 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 FUN(tok
,finish
)(TYPE(Tokenizer
) *);
87 * Finish a word in the tokenizer.
90 FUN(tok
,finish
)(TYPE(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
106 public TYPE(Tokenizer
) *
107 FUN(tok
,init
)(const Char
*ifs
)
109 TYPE(Tokenizer
) *tok
= tok_malloc(sizeof(*tok
));
113 tok
->ifs
= tok_strdup(ifs
? ifs
: IFS
);
114 if (tok
->ifs
== NULL
) {
120 tok
->argv
= tok_malloc(sizeof(*tok
->argv
) * tok
->amax
);
121 if (tok
->argv
== NULL
) {
127 tok
->wspace
= tok_malloc(WINCR
* sizeof(*tok
->wspace
));
128 if (tok
->wspace
== NULL
) {
134 tok
->wmax
= tok
->wspace
+ WINCR
;
135 tok
->wstart
= tok
->wspace
;
136 tok
->wptr
= tok
->wspace
;
145 * Reset the tokenizer
148 FUN(tok
,reset
)(TYPE(Tokenizer
) *tok
)
152 tok
->wstart
= tok
->wspace
;
153 tok
->wptr
= tok
->wspace
;
163 FUN(tok
,end
)(TYPE(Tokenizer
) *tok
)
167 tok_free(tok
->wspace
);
175 * Bourne shell (sh(1)) like tokenizing
177 * tok current tokenizer state (setup with FUN(tok,init)())
182 * 2 Unmatched double quote
183 * 1 Unmatched single quote
185 * Modifies (if return value is 0):
186 * argc number of arguments
187 * argv argument array
188 * cursorc if !NULL, argv element containing cursor
189 * cursorv if !NULL, offset in argv[cursorc] of cursor
192 FUN(tok
,line
)(TYPE(Tokenizer
) *tok
, const TYPE(LineInfo
) *line
,
193 int *argc
, const Char
***argv
, int *cursorc
, int *cursoro
)
200 for (ptr
= line
->buffer
; ;ptr
++) {
201 if (ptr
>= line
->lastchar
)
203 if (ptr
== line
->cursor
) {
205 co
= (int)(tok
->wptr
- tok
->wstart
);
209 tok
->flags
|= TOK_KEEP
;
210 tok
->flags
&= ~TOK_EAT
;
211 switch (tok
->quote
) {
213 tok
->quote
= Q_single
; /* Enter single quote
217 case Q_single
: /* Exit single quote mode */
221 case Q_one
: /* Quote this ' */
226 case Q_double
: /* Stay in double quote mode */
230 case Q_doubleone
: /* Quote this ' */
231 tok
->quote
= Q_double
;
241 tok
->flags
&= ~TOK_EAT
;
242 tok
->flags
|= TOK_KEEP
;
243 switch (tok
->quote
) {
244 case Q_none
: /* Enter double quote mode */
245 tok
->quote
= Q_double
;
248 case Q_double
: /* Exit double quote mode */
252 case Q_one
: /* Quote this " */
257 case Q_single
: /* Stay in single quote mode */
261 case Q_doubleone
: /* Quote this " */
262 tok
->quote
= Q_double
;
272 tok
->flags
|= TOK_KEEP
;
273 tok
->flags
&= ~TOK_EAT
;
274 switch (tok
->quote
) {
275 case Q_none
: /* Quote next character */
279 case Q_double
: /* Quote next character */
280 tok
->quote
= Q_doubleone
;
283 case Q_one
: /* Quote this, restore state */
288 case Q_single
: /* Stay in single quote mode */
292 case Q_doubleone
: /* Quote this \ */
293 tok
->quote
= Q_double
;
303 tok
->flags
&= ~TOK_EAT
;
304 switch (tok
->quote
) {
310 *tok
->wptr
++ = *ptr
; /* Add the return */
313 case Q_doubleone
: /* Back to double, eat the '\n' */
314 tok
->flags
|= TOK_EAT
;
315 tok
->quote
= Q_double
;
318 case Q_one
: /* No quote, more eat the '\n' */
319 tok
->flags
|= TOK_EAT
;
329 switch (tok
->quote
) {
331 /* Finish word and return */
332 if (tok
->flags
& TOK_EAT
) {
333 tok
->flags
&= ~TOK_EAT
;
345 tok
->quote
= Q_double
;
360 tok
->flags
&= ~TOK_EAT
;
361 switch (tok
->quote
) {
363 if (Strchr(tok
->ifs
, *ptr
) != NULL
)
364 FUN(tok
,finish
)(tok
);
377 tok
->quote
= Q_double
;
393 if (tok
->wptr
>= tok
->wmax
- 4) {
394 size_t size
= (size_t)(tok
->wmax
- tok
->wspace
+ WINCR
);
395 Char
*s
= tok_realloc(tok
->wspace
,
400 if (s
!= tok
->wspace
) {
402 for (i
= 0; i
< tok
->argc
; i
++) {
404 (tok
->argv
[i
] - tok
->wspace
) + s
;
406 tok
->wptr
= (tok
->wptr
- tok
->wspace
) + s
;
407 tok
->wstart
= (tok
->wstart
- tok
->wspace
) + s
;
410 tok
->wmax
= s
+ size
;
412 if (tok
->argc
>= tok
->amax
- 4) {
415 p
= tok_realloc(tok
->argv
, tok
->amax
* sizeof(*p
));
422 if (cc
== -1 && co
== -1) {
424 co
= (int)(tok
->wptr
- tok
->wstart
);
430 FUN(tok
,finish
)(tok
);
431 *argv
= (const Char
**)tok
->argv
;
432 *argc
= (int)tok
->argc
;
437 * Simpler version of tok_line, taking a NUL terminated line
438 * and splitting into words, ignoring cursor state.
441 FUN(tok
,str
)(TYPE(Tokenizer
) *tok
, const Char
*line
, int *argc
,
446 memset(&li
, 0, sizeof(li
));
448 li
.cursor
= li
.lastchar
= Strchr(line
, '\0');
449 return FUN(tok
,line(tok
, &li
, argc
, argv
, NULL
, NULL
));