xref: /minix3/lib/libedit/tokenizer.c (revision 3e1db26a5a6252fcff0898d4cb0c3fa16ccf561d)
1*3e1db26aSLionel Sambuc /*	$NetBSD: tokenizer.c,v 1.21 2011/08/16 16:25:15 christos Exp $	*/
2*3e1db26aSLionel Sambuc 
3*3e1db26aSLionel Sambuc /*-
4*3e1db26aSLionel Sambuc  * Copyright (c) 1992, 1993
5*3e1db26aSLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
6*3e1db26aSLionel Sambuc  *
7*3e1db26aSLionel Sambuc  * This code is derived from software contributed to Berkeley by
8*3e1db26aSLionel Sambuc  * Christos Zoulas of Cornell University.
9*3e1db26aSLionel Sambuc  *
10*3e1db26aSLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11*3e1db26aSLionel Sambuc  * modification, are permitted provided that the following conditions
12*3e1db26aSLionel Sambuc  * are met:
13*3e1db26aSLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
14*3e1db26aSLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
15*3e1db26aSLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16*3e1db26aSLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17*3e1db26aSLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18*3e1db26aSLionel Sambuc  * 3. Neither the name of the University nor the names of its contributors
19*3e1db26aSLionel Sambuc  *    may be used to endorse or promote products derived from this software
20*3e1db26aSLionel Sambuc  *    without specific prior written permission.
21*3e1db26aSLionel Sambuc  *
22*3e1db26aSLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*3e1db26aSLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*3e1db26aSLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*3e1db26aSLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*3e1db26aSLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*3e1db26aSLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*3e1db26aSLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*3e1db26aSLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*3e1db26aSLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*3e1db26aSLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*3e1db26aSLionel Sambuc  * SUCH DAMAGE.
33*3e1db26aSLionel Sambuc  */
34*3e1db26aSLionel Sambuc 
35*3e1db26aSLionel Sambuc #include "config.h"
36*3e1db26aSLionel Sambuc #if !defined(lint) && !defined(SCCSID)
37*3e1db26aSLionel Sambuc #if 0
38*3e1db26aSLionel Sambuc static char sccsid[] = "@(#)tokenizer.c	8.1 (Berkeley) 6/4/93";
39*3e1db26aSLionel Sambuc #else
40*3e1db26aSLionel Sambuc __RCSID("$NetBSD: tokenizer.c,v 1.21 2011/08/16 16:25:15 christos Exp $");
41*3e1db26aSLionel Sambuc #endif
42*3e1db26aSLionel Sambuc #endif /* not lint && not SCCSID */
43*3e1db26aSLionel Sambuc 
44*3e1db26aSLionel Sambuc /* We build this file twice, once as NARROW, once as WIDE. */
45*3e1db26aSLionel Sambuc /*
46*3e1db26aSLionel Sambuc  * tokenize.c: Bourne shell like tokenizer
47*3e1db26aSLionel Sambuc  */
48*3e1db26aSLionel Sambuc #include <string.h>
49*3e1db26aSLionel Sambuc #include <stdlib.h>
50*3e1db26aSLionel Sambuc #include "histedit.h"
51*3e1db26aSLionel Sambuc #include "chartype.h"
52*3e1db26aSLionel Sambuc 
53*3e1db26aSLionel Sambuc typedef enum {
54*3e1db26aSLionel Sambuc 	Q_none, Q_single, Q_double, Q_one, Q_doubleone
55*3e1db26aSLionel Sambuc } quote_t;
56*3e1db26aSLionel Sambuc 
57*3e1db26aSLionel Sambuc #define	TOK_KEEP	1
58*3e1db26aSLionel Sambuc #define	TOK_EAT		2
59*3e1db26aSLionel Sambuc 
60*3e1db26aSLionel Sambuc #define	WINCR		20
61*3e1db26aSLionel Sambuc #define	AINCR		10
62*3e1db26aSLionel Sambuc 
63*3e1db26aSLionel Sambuc #define	IFS		STR("\t \n")
64*3e1db26aSLionel Sambuc 
65*3e1db26aSLionel Sambuc #define	tok_malloc(a)		malloc(a)
66*3e1db26aSLionel Sambuc #define	tok_free(a)		free(a)
67*3e1db26aSLionel Sambuc #define	tok_realloc(a, b)	realloc(a, b)
68*3e1db26aSLionel Sambuc #define	tok_strdup(a)		Strdup(a)
69*3e1db26aSLionel Sambuc 
70*3e1db26aSLionel Sambuc 
TYPE(tokenizer)71*3e1db26aSLionel Sambuc struct TYPE(tokenizer) {
72*3e1db26aSLionel Sambuc 	Char	*ifs;		/* In field separator			 */
73*3e1db26aSLionel Sambuc 	size_t	 argc, amax;	/* Current and maximum number of args	 */
74*3e1db26aSLionel Sambuc 	Char   **argv;		/* Argument list			 */
75*3e1db26aSLionel Sambuc 	Char	*wptr, *wmax;	/* Space and limit on the word buffer	 */
76*3e1db26aSLionel Sambuc 	Char	*wstart;	/* Beginning of next word		 */
77*3e1db26aSLionel Sambuc 	Char	*wspace;	/* Space of word buffer			 */
78*3e1db26aSLionel Sambuc 	quote_t	 quote;		/* Quoting state			 */
79*3e1db26aSLionel Sambuc 	int	 flags;		/* flags;				 */
80*3e1db26aSLionel Sambuc };
81*3e1db26aSLionel Sambuc 
82*3e1db26aSLionel Sambuc 
83*3e1db26aSLionel Sambuc private void FUN(tok,finish)(TYPE(Tokenizer) *);
84*3e1db26aSLionel Sambuc 
85*3e1db26aSLionel Sambuc 
86*3e1db26aSLionel Sambuc /* FUN(tok,finish)():
87*3e1db26aSLionel Sambuc  *	Finish a word in the tokenizer.
88*3e1db26aSLionel Sambuc  */
89*3e1db26aSLionel Sambuc private void
FUN(tok,finish)90*3e1db26aSLionel Sambuc FUN(tok,finish)(TYPE(Tokenizer) *tok)
91*3e1db26aSLionel Sambuc {
92*3e1db26aSLionel Sambuc 
93*3e1db26aSLionel Sambuc 	*tok->wptr = '\0';
94*3e1db26aSLionel Sambuc 	if ((tok->flags & TOK_KEEP) || tok->wptr != tok->wstart) {
95*3e1db26aSLionel Sambuc 		tok->argv[tok->argc++] = tok->wstart;
96*3e1db26aSLionel Sambuc 		tok->argv[tok->argc] = NULL;
97*3e1db26aSLionel Sambuc 		tok->wstart = ++tok->wptr;
98*3e1db26aSLionel Sambuc 	}
99*3e1db26aSLionel Sambuc 	tok->flags &= ~TOK_KEEP;
100*3e1db26aSLionel Sambuc }
101*3e1db26aSLionel Sambuc 
102*3e1db26aSLionel Sambuc 
103*3e1db26aSLionel Sambuc /* FUN(tok,init)():
104*3e1db26aSLionel Sambuc  *	Initialize the tokenizer
105*3e1db26aSLionel Sambuc  */
TYPE(Tokenizer)106*3e1db26aSLionel Sambuc public TYPE(Tokenizer) *
107*3e1db26aSLionel Sambuc FUN(tok,init)(const Char *ifs)
108*3e1db26aSLionel Sambuc {
109*3e1db26aSLionel Sambuc 	TYPE(Tokenizer) *tok = tok_malloc(sizeof(*tok));
110*3e1db26aSLionel Sambuc 
111*3e1db26aSLionel Sambuc 	if (tok == NULL)
112*3e1db26aSLionel Sambuc 		return NULL;
113*3e1db26aSLionel Sambuc 	tok->ifs = tok_strdup(ifs ? ifs : IFS);
114*3e1db26aSLionel Sambuc 	if (tok->ifs == NULL) {
115*3e1db26aSLionel Sambuc 		tok_free(tok);
116*3e1db26aSLionel Sambuc 		return NULL;
117*3e1db26aSLionel Sambuc 	}
118*3e1db26aSLionel Sambuc 	tok->argc = 0;
119*3e1db26aSLionel Sambuc 	tok->amax = AINCR;
120*3e1db26aSLionel Sambuc 	tok->argv = tok_malloc(sizeof(*tok->argv) * tok->amax);
121*3e1db26aSLionel Sambuc 	if (tok->argv == NULL) {
122*3e1db26aSLionel Sambuc 		tok_free(tok->ifs);
123*3e1db26aSLionel Sambuc 		tok_free(tok);
124*3e1db26aSLionel Sambuc 		return NULL;
125*3e1db26aSLionel Sambuc 	}
126*3e1db26aSLionel Sambuc 	tok->argv[0] = NULL;
127*3e1db26aSLionel Sambuc 	tok->wspace = tok_malloc(WINCR * sizeof(*tok->wspace));
128*3e1db26aSLionel Sambuc 	if (tok->wspace == NULL) {
129*3e1db26aSLionel Sambuc 		tok_free(tok->argv);
130*3e1db26aSLionel Sambuc 		tok_free(tok->ifs);
131*3e1db26aSLionel Sambuc 		tok_free(tok);
132*3e1db26aSLionel Sambuc 		return NULL;
133*3e1db26aSLionel Sambuc 	}
134*3e1db26aSLionel Sambuc 	tok->wmax = tok->wspace + WINCR;
135*3e1db26aSLionel Sambuc 	tok->wstart = tok->wspace;
136*3e1db26aSLionel Sambuc 	tok->wptr = tok->wspace;
137*3e1db26aSLionel Sambuc 	tok->flags = 0;
138*3e1db26aSLionel Sambuc 	tok->quote = Q_none;
139*3e1db26aSLionel Sambuc 
140*3e1db26aSLionel Sambuc 	return tok;
141*3e1db26aSLionel Sambuc }
142*3e1db26aSLionel Sambuc 
143*3e1db26aSLionel Sambuc 
144*3e1db26aSLionel Sambuc /* FUN(tok,reset)():
145*3e1db26aSLionel Sambuc  *	Reset the tokenizer
146*3e1db26aSLionel Sambuc  */
147*3e1db26aSLionel Sambuc public void
FUN(tok,reset)148*3e1db26aSLionel Sambuc FUN(tok,reset)(TYPE(Tokenizer) *tok)
149*3e1db26aSLionel Sambuc {
150*3e1db26aSLionel Sambuc 
151*3e1db26aSLionel Sambuc 	tok->argc = 0;
152*3e1db26aSLionel Sambuc 	tok->wstart = tok->wspace;
153*3e1db26aSLionel Sambuc 	tok->wptr = tok->wspace;
154*3e1db26aSLionel Sambuc 	tok->flags = 0;
155*3e1db26aSLionel Sambuc 	tok->quote = Q_none;
156*3e1db26aSLionel Sambuc }
157*3e1db26aSLionel Sambuc 
158*3e1db26aSLionel Sambuc 
159*3e1db26aSLionel Sambuc /* FUN(tok,end)():
160*3e1db26aSLionel Sambuc  *	Clean up
161*3e1db26aSLionel Sambuc  */
162*3e1db26aSLionel Sambuc public void
FUN(tok,end)163*3e1db26aSLionel Sambuc FUN(tok,end)(TYPE(Tokenizer) *tok)
164*3e1db26aSLionel Sambuc {
165*3e1db26aSLionel Sambuc 
166*3e1db26aSLionel Sambuc 	tok_free(tok->ifs);
167*3e1db26aSLionel Sambuc 	tok_free(tok->wspace);
168*3e1db26aSLionel Sambuc 	tok_free(tok->argv);
169*3e1db26aSLionel Sambuc 	tok_free(tok);
170*3e1db26aSLionel Sambuc }
171*3e1db26aSLionel Sambuc 
172*3e1db26aSLionel Sambuc 
173*3e1db26aSLionel Sambuc 
174*3e1db26aSLionel Sambuc /* FUN(tok,line)():
175*3e1db26aSLionel Sambuc  *	Bourne shell (sh(1)) like tokenizing
176*3e1db26aSLionel Sambuc  *	Arguments:
177*3e1db26aSLionel Sambuc  *		tok	current tokenizer state (setup with FUN(tok,init)())
178*3e1db26aSLionel Sambuc  *		line	line to parse
179*3e1db26aSLionel Sambuc  *	Returns:
180*3e1db26aSLionel Sambuc  *		-1	Internal error
181*3e1db26aSLionel Sambuc  *		 3	Quoted return
182*3e1db26aSLionel Sambuc  *		 2	Unmatched double quote
183*3e1db26aSLionel Sambuc  *		 1	Unmatched single quote
184*3e1db26aSLionel Sambuc  *		 0	Ok
185*3e1db26aSLionel Sambuc  *	Modifies (if return value is 0):
186*3e1db26aSLionel Sambuc  *		argc	number of arguments
187*3e1db26aSLionel Sambuc  *		argv	argument array
188*3e1db26aSLionel Sambuc  *		cursorc	if !NULL, argv element containing cursor
189*3e1db26aSLionel Sambuc  *		cursorv	if !NULL, offset in argv[cursorc] of cursor
190*3e1db26aSLionel Sambuc  */
191*3e1db26aSLionel Sambuc public int
FUN(tok,line)192*3e1db26aSLionel Sambuc FUN(tok,line)(TYPE(Tokenizer) *tok, const TYPE(LineInfo) *line,
193*3e1db26aSLionel Sambuc     int *argc, const Char ***argv, int *cursorc, int *cursoro)
194*3e1db26aSLionel Sambuc {
195*3e1db26aSLionel Sambuc 	const Char *ptr;
196*3e1db26aSLionel Sambuc 	int cc, co;
197*3e1db26aSLionel Sambuc 
198*3e1db26aSLionel Sambuc 	cc = co = -1;
199*3e1db26aSLionel Sambuc 	ptr = line->buffer;
200*3e1db26aSLionel Sambuc 	for (ptr = line->buffer; ;ptr++) {
201*3e1db26aSLionel Sambuc 		if (ptr >= line->lastchar)
202*3e1db26aSLionel Sambuc 			ptr = STR("");
203*3e1db26aSLionel Sambuc 		if (ptr == line->cursor) {
204*3e1db26aSLionel Sambuc 			cc = (int)tok->argc;
205*3e1db26aSLionel Sambuc 			co = (int)(tok->wptr - tok->wstart);
206*3e1db26aSLionel Sambuc 		}
207*3e1db26aSLionel Sambuc 		switch (*ptr) {
208*3e1db26aSLionel Sambuc 		case '\'':
209*3e1db26aSLionel Sambuc 			tok->flags |= TOK_KEEP;
210*3e1db26aSLionel Sambuc 			tok->flags &= ~TOK_EAT;
211*3e1db26aSLionel Sambuc 			switch (tok->quote) {
212*3e1db26aSLionel Sambuc 			case Q_none:
213*3e1db26aSLionel Sambuc 				tok->quote = Q_single;	/* Enter single quote
214*3e1db26aSLionel Sambuc 							 * mode */
215*3e1db26aSLionel Sambuc 				break;
216*3e1db26aSLionel Sambuc 
217*3e1db26aSLionel Sambuc 			case Q_single:	/* Exit single quote mode */
218*3e1db26aSLionel Sambuc 				tok->quote = Q_none;
219*3e1db26aSLionel Sambuc 				break;
220*3e1db26aSLionel Sambuc 
221*3e1db26aSLionel Sambuc 			case Q_one:	/* Quote this ' */
222*3e1db26aSLionel Sambuc 				tok->quote = Q_none;
223*3e1db26aSLionel Sambuc 				*tok->wptr++ = *ptr;
224*3e1db26aSLionel Sambuc 				break;
225*3e1db26aSLionel Sambuc 
226*3e1db26aSLionel Sambuc 			case Q_double:	/* Stay in double quote mode */
227*3e1db26aSLionel Sambuc 				*tok->wptr++ = *ptr;
228*3e1db26aSLionel Sambuc 				break;
229*3e1db26aSLionel Sambuc 
230*3e1db26aSLionel Sambuc 			case Q_doubleone:	/* Quote this ' */
231*3e1db26aSLionel Sambuc 				tok->quote = Q_double;
232*3e1db26aSLionel Sambuc 				*tok->wptr++ = *ptr;
233*3e1db26aSLionel Sambuc 				break;
234*3e1db26aSLionel Sambuc 
235*3e1db26aSLionel Sambuc 			default:
236*3e1db26aSLionel Sambuc 				return -1;
237*3e1db26aSLionel Sambuc 			}
238*3e1db26aSLionel Sambuc 			break;
239*3e1db26aSLionel Sambuc 
240*3e1db26aSLionel Sambuc 		case '"':
241*3e1db26aSLionel Sambuc 			tok->flags &= ~TOK_EAT;
242*3e1db26aSLionel Sambuc 			tok->flags |= TOK_KEEP;
243*3e1db26aSLionel Sambuc 			switch (tok->quote) {
244*3e1db26aSLionel Sambuc 			case Q_none:	/* Enter double quote mode */
245*3e1db26aSLionel Sambuc 				tok->quote = Q_double;
246*3e1db26aSLionel Sambuc 				break;
247*3e1db26aSLionel Sambuc 
248*3e1db26aSLionel Sambuc 			case Q_double:	/* Exit double quote mode */
249*3e1db26aSLionel Sambuc 				tok->quote = Q_none;
250*3e1db26aSLionel Sambuc 				break;
251*3e1db26aSLionel Sambuc 
252*3e1db26aSLionel Sambuc 			case Q_one:	/* Quote this " */
253*3e1db26aSLionel Sambuc 				tok->quote = Q_none;
254*3e1db26aSLionel Sambuc 				*tok->wptr++ = *ptr;
255*3e1db26aSLionel Sambuc 				break;
256*3e1db26aSLionel Sambuc 
257*3e1db26aSLionel Sambuc 			case Q_single:	/* Stay in single quote mode */
258*3e1db26aSLionel Sambuc 				*tok->wptr++ = *ptr;
259*3e1db26aSLionel Sambuc 				break;
260*3e1db26aSLionel Sambuc 
261*3e1db26aSLionel Sambuc 			case Q_doubleone:	/* Quote this " */
262*3e1db26aSLionel Sambuc 				tok->quote = Q_double;
263*3e1db26aSLionel Sambuc 				*tok->wptr++ = *ptr;
264*3e1db26aSLionel Sambuc 				break;
265*3e1db26aSLionel Sambuc 
266*3e1db26aSLionel Sambuc 			default:
267*3e1db26aSLionel Sambuc 				return -1;
268*3e1db26aSLionel Sambuc 			}
269*3e1db26aSLionel Sambuc 			break;
270*3e1db26aSLionel Sambuc 
271*3e1db26aSLionel Sambuc 		case '\\':
272*3e1db26aSLionel Sambuc 			tok->flags |= TOK_KEEP;
273*3e1db26aSLionel Sambuc 			tok->flags &= ~TOK_EAT;
274*3e1db26aSLionel Sambuc 			switch (tok->quote) {
275*3e1db26aSLionel Sambuc 			case Q_none:	/* Quote next character */
276*3e1db26aSLionel Sambuc 				tok->quote = Q_one;
277*3e1db26aSLionel Sambuc 				break;
278*3e1db26aSLionel Sambuc 
279*3e1db26aSLionel Sambuc 			case Q_double:	/* Quote next character */
280*3e1db26aSLionel Sambuc 				tok->quote = Q_doubleone;
281*3e1db26aSLionel Sambuc 				break;
282*3e1db26aSLionel Sambuc 
283*3e1db26aSLionel Sambuc 			case Q_one:	/* Quote this, restore state */
284*3e1db26aSLionel Sambuc 				*tok->wptr++ = *ptr;
285*3e1db26aSLionel Sambuc 				tok->quote = Q_none;
286*3e1db26aSLionel Sambuc 				break;
287*3e1db26aSLionel Sambuc 
288*3e1db26aSLionel Sambuc 			case Q_single:	/* Stay in single quote mode */
289*3e1db26aSLionel Sambuc 				*tok->wptr++ = *ptr;
290*3e1db26aSLionel Sambuc 				break;
291*3e1db26aSLionel Sambuc 
292*3e1db26aSLionel Sambuc 			case Q_doubleone:	/* Quote this \ */
293*3e1db26aSLionel Sambuc 				tok->quote = Q_double;
294*3e1db26aSLionel Sambuc 				*tok->wptr++ = *ptr;
295*3e1db26aSLionel Sambuc 				break;
296*3e1db26aSLionel Sambuc 
297*3e1db26aSLionel Sambuc 			default:
298*3e1db26aSLionel Sambuc 				return -1;
299*3e1db26aSLionel Sambuc 			}
300*3e1db26aSLionel Sambuc 			break;
301*3e1db26aSLionel Sambuc 
302*3e1db26aSLionel Sambuc 		case '\n':
303*3e1db26aSLionel Sambuc 			tok->flags &= ~TOK_EAT;
304*3e1db26aSLionel Sambuc 			switch (tok->quote) {
305*3e1db26aSLionel Sambuc 			case Q_none:
306*3e1db26aSLionel Sambuc 				goto tok_line_outok;
307*3e1db26aSLionel Sambuc 
308*3e1db26aSLionel Sambuc 			case Q_single:
309*3e1db26aSLionel Sambuc 			case Q_double:
310*3e1db26aSLionel Sambuc 				*tok->wptr++ = *ptr;	/* Add the return */
311*3e1db26aSLionel Sambuc 				break;
312*3e1db26aSLionel Sambuc 
313*3e1db26aSLionel Sambuc 			case Q_doubleone:   /* Back to double, eat the '\n' */
314*3e1db26aSLionel Sambuc 				tok->flags |= TOK_EAT;
315*3e1db26aSLionel Sambuc 				tok->quote = Q_double;
316*3e1db26aSLionel Sambuc 				break;
317*3e1db26aSLionel Sambuc 
318*3e1db26aSLionel Sambuc 			case Q_one:	/* No quote, more eat the '\n' */
319*3e1db26aSLionel Sambuc 				tok->flags |= TOK_EAT;
320*3e1db26aSLionel Sambuc 				tok->quote = Q_none;
321*3e1db26aSLionel Sambuc 				break;
322*3e1db26aSLionel Sambuc 
323*3e1db26aSLionel Sambuc 			default:
324*3e1db26aSLionel Sambuc 				return 0;
325*3e1db26aSLionel Sambuc 			}
326*3e1db26aSLionel Sambuc 			break;
327*3e1db26aSLionel Sambuc 
328*3e1db26aSLionel Sambuc 		case '\0':
329*3e1db26aSLionel Sambuc 			switch (tok->quote) {
330*3e1db26aSLionel Sambuc 			case Q_none:
331*3e1db26aSLionel Sambuc 				/* Finish word and return */
332*3e1db26aSLionel Sambuc 				if (tok->flags & TOK_EAT) {
333*3e1db26aSLionel Sambuc 					tok->flags &= ~TOK_EAT;
334*3e1db26aSLionel Sambuc 					return 3;
335*3e1db26aSLionel Sambuc 				}
336*3e1db26aSLionel Sambuc 				goto tok_line_outok;
337*3e1db26aSLionel Sambuc 
338*3e1db26aSLionel Sambuc 			case Q_single:
339*3e1db26aSLionel Sambuc 				return 1;
340*3e1db26aSLionel Sambuc 
341*3e1db26aSLionel Sambuc 			case Q_double:
342*3e1db26aSLionel Sambuc 				return 2;
343*3e1db26aSLionel Sambuc 
344*3e1db26aSLionel Sambuc 			case Q_doubleone:
345*3e1db26aSLionel Sambuc 				tok->quote = Q_double;
346*3e1db26aSLionel Sambuc 				*tok->wptr++ = *ptr;
347*3e1db26aSLionel Sambuc 				break;
348*3e1db26aSLionel Sambuc 
349*3e1db26aSLionel Sambuc 			case Q_one:
350*3e1db26aSLionel Sambuc 				tok->quote = Q_none;
351*3e1db26aSLionel Sambuc 				*tok->wptr++ = *ptr;
352*3e1db26aSLionel Sambuc 				break;
353*3e1db26aSLionel Sambuc 
354*3e1db26aSLionel Sambuc 			default:
355*3e1db26aSLionel Sambuc 				return -1;
356*3e1db26aSLionel Sambuc 			}
357*3e1db26aSLionel Sambuc 			break;
358*3e1db26aSLionel Sambuc 
359*3e1db26aSLionel Sambuc 		default:
360*3e1db26aSLionel Sambuc 			tok->flags &= ~TOK_EAT;
361*3e1db26aSLionel Sambuc 			switch (tok->quote) {
362*3e1db26aSLionel Sambuc 			case Q_none:
363*3e1db26aSLionel Sambuc 				if (Strchr(tok->ifs, *ptr) != NULL)
364*3e1db26aSLionel Sambuc 					FUN(tok,finish)(tok);
365*3e1db26aSLionel Sambuc 				else
366*3e1db26aSLionel Sambuc 					*tok->wptr++ = *ptr;
367*3e1db26aSLionel Sambuc 				break;
368*3e1db26aSLionel Sambuc 
369*3e1db26aSLionel Sambuc 			case Q_single:
370*3e1db26aSLionel Sambuc 			case Q_double:
371*3e1db26aSLionel Sambuc 				*tok->wptr++ = *ptr;
372*3e1db26aSLionel Sambuc 				break;
373*3e1db26aSLionel Sambuc 
374*3e1db26aSLionel Sambuc 
375*3e1db26aSLionel Sambuc 			case Q_doubleone:
376*3e1db26aSLionel Sambuc 				*tok->wptr++ = '\\';
377*3e1db26aSLionel Sambuc 				tok->quote = Q_double;
378*3e1db26aSLionel Sambuc 				*tok->wptr++ = *ptr;
379*3e1db26aSLionel Sambuc 				break;
380*3e1db26aSLionel Sambuc 
381*3e1db26aSLionel Sambuc 			case Q_one:
382*3e1db26aSLionel Sambuc 				tok->quote = Q_none;
383*3e1db26aSLionel Sambuc 				*tok->wptr++ = *ptr;
384*3e1db26aSLionel Sambuc 				break;
385*3e1db26aSLionel Sambuc 
386*3e1db26aSLionel Sambuc 			default:
387*3e1db26aSLionel Sambuc 				return -1;
388*3e1db26aSLionel Sambuc 
389*3e1db26aSLionel Sambuc 			}
390*3e1db26aSLionel Sambuc 			break;
391*3e1db26aSLionel Sambuc 		}
392*3e1db26aSLionel Sambuc 
393*3e1db26aSLionel Sambuc 		if (tok->wptr >= tok->wmax - 4) {
394*3e1db26aSLionel Sambuc 			size_t size = (size_t)(tok->wmax - tok->wspace + WINCR);
395*3e1db26aSLionel Sambuc 			Char *s = tok_realloc(tok->wspace,
396*3e1db26aSLionel Sambuc 			    size * sizeof(*s));
397*3e1db26aSLionel Sambuc 			if (s == NULL)
398*3e1db26aSLionel Sambuc 				return -1;
399*3e1db26aSLionel Sambuc 
400*3e1db26aSLionel Sambuc 			if (s != tok->wspace) {
401*3e1db26aSLionel Sambuc 				size_t i;
402*3e1db26aSLionel Sambuc 				for (i = 0; i < tok->argc; i++) {
403*3e1db26aSLionel Sambuc 				    tok->argv[i] =
404*3e1db26aSLionel Sambuc 					(tok->argv[i] - tok->wspace) + s;
405*3e1db26aSLionel Sambuc 				}
406*3e1db26aSLionel Sambuc 				tok->wptr = (tok->wptr - tok->wspace) + s;
407*3e1db26aSLionel Sambuc 				tok->wstart = (tok->wstart - tok->wspace) + s;
408*3e1db26aSLionel Sambuc 				tok->wspace = s;
409*3e1db26aSLionel Sambuc 			}
410*3e1db26aSLionel Sambuc 			tok->wmax = s + size;
411*3e1db26aSLionel Sambuc 		}
412*3e1db26aSLionel Sambuc 		if (tok->argc >= tok->amax - 4) {
413*3e1db26aSLionel Sambuc 			Char **p;
414*3e1db26aSLionel Sambuc 			tok->amax += AINCR;
415*3e1db26aSLionel Sambuc 			p = tok_realloc(tok->argv, tok->amax * sizeof(*p));
416*3e1db26aSLionel Sambuc 			if (p == NULL)
417*3e1db26aSLionel Sambuc 				return -1;
418*3e1db26aSLionel Sambuc 			tok->argv = p;
419*3e1db26aSLionel Sambuc 		}
420*3e1db26aSLionel Sambuc 	}
421*3e1db26aSLionel Sambuc  tok_line_outok:
422*3e1db26aSLionel Sambuc 	if (cc == -1 && co == -1) {
423*3e1db26aSLionel Sambuc 		cc = (int)tok->argc;
424*3e1db26aSLionel Sambuc 		co = (int)(tok->wptr - tok->wstart);
425*3e1db26aSLionel Sambuc 	}
426*3e1db26aSLionel Sambuc 	if (cursorc != NULL)
427*3e1db26aSLionel Sambuc 		*cursorc = cc;
428*3e1db26aSLionel Sambuc 	if (cursoro != NULL)
429*3e1db26aSLionel Sambuc 		*cursoro = co;
430*3e1db26aSLionel Sambuc 	FUN(tok,finish)(tok);
431*3e1db26aSLionel Sambuc 	*argv = (const Char **)tok->argv;
432*3e1db26aSLionel Sambuc 	*argc = (int)tok->argc;
433*3e1db26aSLionel Sambuc 	return 0;
434*3e1db26aSLionel Sambuc }
435*3e1db26aSLionel Sambuc 
436*3e1db26aSLionel Sambuc /* FUN(tok,str)():
437*3e1db26aSLionel Sambuc  *	Simpler version of tok_line, taking a NUL terminated line
438*3e1db26aSLionel Sambuc  *	and splitting into words, ignoring cursor state.
439*3e1db26aSLionel Sambuc  */
440*3e1db26aSLionel Sambuc public int
FUN(tok,str)441*3e1db26aSLionel Sambuc FUN(tok,str)(TYPE(Tokenizer) *tok, const Char *line, int *argc,
442*3e1db26aSLionel Sambuc     const Char ***argv)
443*3e1db26aSLionel Sambuc {
444*3e1db26aSLionel Sambuc 	TYPE(LineInfo) li;
445*3e1db26aSLionel Sambuc 
446*3e1db26aSLionel Sambuc 	memset(&li, 0, sizeof(li));
447*3e1db26aSLionel Sambuc 	li.buffer = line;
448*3e1db26aSLionel Sambuc 	li.cursor = li.lastchar = Strchr(line, '\0');
449*3e1db26aSLionel Sambuc 	return FUN(tok,line(tok, &li, argc, argv, NULL, NULL));
450*3e1db26aSLionel Sambuc }
451