1 /* $OpenBSD: tokenizer.c,v 1.12 2010/06/30 00:05:35 nicm Exp $ */
2 /* $NetBSD: tokenizer.c,v 1.18 2010/01/03 18:27:10 christos Exp $ */
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Christos Zoulas of Cornell University.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 /* We build this file twice, once as NARROW, once as WIDE. */
40 * tokenize.c: Bourne shell like tokenizer
48 Q_none
, Q_single
, Q_double
, Q_one
, Q_doubleone
57 #define IFS STR("\t \n")
59 #define tok_malloc(a) malloc(a)
60 #define tok_free(a) free(a)
61 #define tok_realloc(a, b) realloc(a, b)
62 #define tok_strdup(a) Strdup(a)
65 struct TYPE(tokenizer
) {
66 Char
*ifs
; /* In field separator */
67 int argc
, amax
; /* Current and maximum number of args */
68 Char
**argv
; /* Argument list */
69 Char
*wptr
, *wmax
; /* Space and limit on the word buffer */
70 Char
*wstart
; /* Beginning of next word */
71 Char
*wspace
; /* Space of word buffer */
72 quote_t quote
; /* Quoting state */
73 int flags
; /* flags; */
77 private void FUN(tok
,finish
)(TYPE(Tokenizer
) *);
81 * Finish a word in the tokenizer.
84 FUN(tok
,finish
)(TYPE(Tokenizer
) *tok
)
88 if ((tok
->flags
& TOK_KEEP
) || tok
->wptr
!= tok
->wstart
) {
89 tok
->argv
[tok
->argc
++] = tok
->wstart
;
90 tok
->argv
[tok
->argc
] = NULL
;
91 tok
->wstart
= ++tok
->wptr
;
93 tok
->flags
&= ~TOK_KEEP
;
98 * Initialize the tokenizer
100 public TYPE(Tokenizer
) *
101 FUN(tok
,init
)(const Char
*ifs
)
103 TYPE(Tokenizer
) *tok
= tok_malloc(sizeof(TYPE(Tokenizer
)));
107 tok
->ifs
= tok_strdup(ifs
? ifs
: IFS
);
108 if (tok
->ifs
== NULL
) {
109 tok_free((ptr_t
)tok
);
114 tok
->argv
= tok_malloc(sizeof(*tok
->argv
) * tok
->amax
);
115 if (tok
->argv
== NULL
) {
116 tok_free((ptr_t
)tok
->ifs
);
117 tok_free((ptr_t
)tok
);
121 tok
->wspace
= tok_malloc(WINCR
* sizeof(*tok
->wspace
));
122 if (tok
->wspace
== NULL
) {
123 tok_free((ptr_t
)tok
->argv
);
124 tok_free((ptr_t
)tok
->ifs
);
125 tok_free((ptr_t
)tok
);
128 tok
->wmax
= tok
->wspace
+ WINCR
;
129 tok
->wstart
= tok
->wspace
;
130 tok
->wptr
= tok
->wspace
;
139 * Reset the tokenizer
142 FUN(tok
,reset
)(TYPE(Tokenizer
) *tok
)
146 tok
->wstart
= tok
->wspace
;
147 tok
->wptr
= tok
->wspace
;
157 FUN(tok
,end
)(TYPE(Tokenizer
) *tok
)
160 tok_free((ptr_t
) tok
->ifs
);
161 tok_free((ptr_t
) tok
->wspace
);
162 tok_free((ptr_t
) tok
->argv
);
163 tok_free((ptr_t
) tok
);
169 * Bourne shell (sh(1)) like tokenizing
171 * tok current tokenizer state (setup with FUN(tok,init)())
176 * 2 Unmatched double quote
177 * 1 Unmatched single quote
179 * Modifies (if return value is 0):
180 * argc number of arguments
181 * argv argument array
182 * cursorc if !NULL, argv element containing cursor
183 * cursorv if !NULL, offset in argv[cursorc] of cursor
186 FUN(tok
,line
)(TYPE(Tokenizer
) *tok
, const TYPE(LineInfo
) *line
,
187 int *argc
, const Char
***argv
, int *cursorc
, int *cursoro
)
194 for (ptr
= line
->buffer
; ;ptr
++) {
195 if (ptr
>= line
->lastchar
)
197 if (ptr
== line
->cursor
) {
199 co
= (int)(tok
->wptr
- tok
->wstart
);
203 tok
->flags
|= TOK_KEEP
;
204 tok
->flags
&= ~TOK_EAT
;
205 switch (tok
->quote
) {
207 tok
->quote
= Q_single
; /* Enter single quote
211 case Q_single
: /* Exit single quote mode */
215 case Q_one
: /* Quote this ' */
220 case Q_double
: /* Stay in double quote mode */
224 case Q_doubleone
: /* Quote this ' */
225 tok
->quote
= Q_double
;
235 tok
->flags
&= ~TOK_EAT
;
236 tok
->flags
|= TOK_KEEP
;
237 switch (tok
->quote
) {
238 case Q_none
: /* Enter double quote mode */
239 tok
->quote
= Q_double
;
242 case Q_double
: /* Exit double quote mode */
246 case Q_one
: /* Quote this " */
251 case Q_single
: /* Stay in single quote mode */
255 case Q_doubleone
: /* Quote this " */
256 tok
->quote
= Q_double
;
266 tok
->flags
|= TOK_KEEP
;
267 tok
->flags
&= ~TOK_EAT
;
268 switch (tok
->quote
) {
269 case Q_none
: /* Quote next character */
273 case Q_double
: /* Quote next character */
274 tok
->quote
= Q_doubleone
;
277 case Q_one
: /* Quote this, restore state */
282 case Q_single
: /* Stay in single quote mode */
286 case Q_doubleone
: /* Quote this \ */
287 tok
->quote
= Q_double
;
297 tok
->flags
&= ~TOK_EAT
;
298 switch (tok
->quote
) {
304 *tok
->wptr
++ = *ptr
; /* Add the return */
307 case Q_doubleone
: /* Back to double, eat the '\n' */
308 tok
->flags
|= TOK_EAT
;
309 tok
->quote
= Q_double
;
312 case Q_one
: /* No quote, more eat the '\n' */
313 tok
->flags
|= TOK_EAT
;
323 switch (tok
->quote
) {
325 /* Finish word and return */
326 if (tok
->flags
& TOK_EAT
) {
327 tok
->flags
&= ~TOK_EAT
;
339 tok
->quote
= Q_double
;
354 tok
->flags
&= ~TOK_EAT
;
355 switch (tok
->quote
) {
357 if (Strchr(tok
->ifs
, *ptr
) != NULL
)
358 FUN(tok
,finish
)(tok
);
371 tok
->quote
= Q_double
;
387 if (tok
->wptr
>= tok
->wmax
- 4) {
388 size_t size
= tok
->wmax
- tok
->wspace
+ WINCR
;
389 Char
*s
= tok_realloc(tok
->wspace
,
394 if (s
!= tok
->wspace
) {
396 for (i
= 0; i
< tok
->argc
; i
++) {
398 (tok
->argv
[i
] - tok
->wspace
) + s
;
400 tok
->wptr
= (tok
->wptr
- tok
->wspace
) + s
;
401 tok
->wstart
= (tok
->wstart
- tok
->wspace
) + s
;
404 tok
->wmax
= s
+ size
;
406 if (tok
->argc
>= tok
->amax
- 4) {
409 p
= tok_realloc(tok
->argv
, tok
->amax
* sizeof(*p
));
416 if (cc
== -1 && co
== -1) {
418 co
= (int)(tok
->wptr
- tok
->wstart
);
424 FUN(tok
,finish
)(tok
);
425 *argv
= (const Char
**)tok
->argv
;
431 * Simpler version of tok_line, taking a NUL terminated line
432 * and splitting into words, ignoring cursor state.
435 FUN(tok
,str
)(TYPE(Tokenizer
) *tok
, const Char
*line
, int *argc
,
440 memset(&li
, 0, sizeof(li
));
442 li
.cursor
= li
.lastchar
= Strchr(line
, '\0');
443 return (FUN(tok
,line
)(tok
, &li
, argc
, argv
, NULL
, NULL
));