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