Home | History | Annotate | Line # | Download | only in libedit
tokenizer.c revision 1.12
      1 /*	$NetBSD: tokenizer.c,v 1.12 2003/08/07 16:44:34 agc Exp $	*/
      2 
      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.
     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.
     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 
     35 #include "config.h"
     36 #if !defined(lint) && !defined(SCCSID)
     37 #if 0
     38 static char sccsid[] = "@(#)tokenizer.c	8.1 (Berkeley) 6/4/93";
     39 #else
     40 __RCSID("$NetBSD: tokenizer.c,v 1.12 2003/08/07 16:44:34 agc Exp $");
     41 #endif
     42 #endif /* not lint && not SCCSID */
     43 
     44 /*
     45  * tokenize.c: Bourne shell like tokenizer
     46  */
     47 #include <string.h>
     48 #include <stdlib.h>
     49 #include "tokenizer.h"
     50 
     51 typedef enum {
     52 	Q_none, Q_single, Q_double, Q_one, Q_doubleone
     53 } quote_t;
     54 
     55 #define	IFS		"\t \n"
     56 
     57 #define	TOK_KEEP	1
     58 #define	TOK_EAT		2
     59 
     60 #define	WINCR		20
     61 #define	AINCR		10
     62 
     63 #define	tok_malloc(a)		malloc(a)
     64 #define	tok_free(a)		free(a)
     65 #define	tok_realloc(a, b)	realloc(a, b)
     66 
     67 
     68 struct tokenizer {
     69 	char	*ifs;		/* In field separator			 */
     70 	int	 argc, amax;	/* Current and maximum number of args	 */
     71 	char   **argv;		/* Argument list			 */
     72 	char	*wptr, *wmax;	/* Space and limit on the word buffer	 */
     73 	char	*wstart;	/* Beginning of next word		 */
     74 	char	*wspace;	/* Space of word buffer			 */
     75 	quote_t	 quote;		/* Quoting state			 */
     76 	int	 flags;		/* flags;				 */
     77 };
     78 
     79 
     80 private void tok_finish(Tokenizer *);
     81 
     82 
     83 /* tok_finish():
     84  *	Finish a word in the tokenizer.
     85  */
     86 private void
     87 tok_finish(Tokenizer *tok)
     88 {
     89 
     90 	*tok->wptr = '\0';
     91 	if ((tok->flags & TOK_KEEP) || tok->wptr != tok->wstart) {
     92 		tok->argv[tok->argc++] = tok->wstart;
     93 		tok->argv[tok->argc] = NULL;
     94 		tok->wstart = ++tok->wptr;
     95 	}
     96 	tok->flags &= ~TOK_KEEP;
     97 }
     98 
     99 
    100 /* tok_init():
    101  *	Initialize the tokenizer
    102  */
    103 public Tokenizer *
    104 tok_init(const char *ifs)
    105 {
    106 	Tokenizer *tok = (Tokenizer *) tok_malloc(sizeof(Tokenizer));
    107 
    108 	if (tok == NULL)
    109 		return NULL;
    110 	tok->ifs = strdup(ifs ? ifs : IFS);
    111 	if (tok->ifs == NULL) {
    112 		tok_free((ptr_t)tok);
    113 		return NULL;
    114 	}
    115 	tok->argc = 0;
    116 	tok->amax = AINCR;
    117 	tok->argv = (char **) tok_malloc(sizeof(char *) * tok->amax);
    118 	if (tok->argv == NULL) {
    119 		tok_free((ptr_t)tok->ifs);
    120 		tok_free((ptr_t)tok);
    121 		return NULL;
    122 	}
    123 	tok->argv[0] = NULL;
    124 	tok->wspace = (char *) tok_malloc(WINCR);
    125 	if (tok->wspace == NULL) {
    126 		tok_free((ptr_t)tok->argv);
    127 		tok_free((ptr_t)tok->ifs);
    128 		tok_free((ptr_t)tok);
    129 		return NULL;
    130 	}
    131 	tok->wmax = tok->wspace + WINCR;
    132 	tok->wstart = tok->wspace;
    133 	tok->wptr = tok->wspace;
    134 	tok->flags = 0;
    135 	tok->quote = Q_none;
    136 
    137 	return (tok);
    138 }
    139 
    140 
    141 /* tok_reset():
    142  *	Reset the tokenizer
    143  */
    144 public void
    145 tok_reset(Tokenizer *tok)
    146 {
    147 
    148 	tok->argc = 0;
    149 	tok->wstart = tok->wspace;
    150 	tok->wptr = tok->wspace;
    151 	tok->flags = 0;
    152 	tok->quote = Q_none;
    153 }
    154 
    155 
    156 /* tok_end():
    157  *	Clean up
    158  */
    159 public void
    160 tok_end(Tokenizer *tok)
    161 {
    162 
    163 	tok_free((ptr_t) tok->ifs);
    164 	tok_free((ptr_t) tok->wspace);
    165 	tok_free((ptr_t) tok->argv);
    166 	tok_free((ptr_t) tok);
    167 }
    168 
    169 
    170 
    171 /* tok_line():
    172  *	Bourne shell like tokenizing
    173  *	Return:
    174  *		-1: Internal error
    175  *		 3: Quoted return
    176  *		 2: Unmatched double quote
    177  *		 1: Unmatched single quote
    178  *		 0: Ok
    179  */
    180 public int
    181 tok_line(Tokenizer *tok, const char *line, int *argc, const char ***argv)
    182 {
    183 	const char *ptr;
    184 
    185 	for (;;) {
    186 		switch (*(ptr = line++)) {
    187 		case '\'':
    188 			tok->flags |= TOK_KEEP;
    189 			tok->flags &= ~TOK_EAT;
    190 			switch (tok->quote) {
    191 			case Q_none:
    192 				tok->quote = Q_single;	/* Enter single quote
    193 							 * mode */
    194 				break;
    195 
    196 			case Q_single:	/* Exit single quote mode */
    197 				tok->quote = Q_none;
    198 				break;
    199 
    200 			case Q_one:	/* Quote this ' */
    201 				tok->quote = Q_none;
    202 				*tok->wptr++ = *ptr;
    203 				break;
    204 
    205 			case Q_double:	/* Stay in double quote mode */
    206 				*tok->wptr++ = *ptr;
    207 				break;
    208 
    209 			case Q_doubleone:	/* Quote this ' */
    210 				tok->quote = Q_double;
    211 				*tok->wptr++ = *ptr;
    212 				break;
    213 
    214 			default:
    215 				return (-1);
    216 			}
    217 			break;
    218 
    219 		case '"':
    220 			tok->flags &= ~TOK_EAT;
    221 			tok->flags |= TOK_KEEP;
    222 			switch (tok->quote) {
    223 			case Q_none:	/* Enter double quote mode */
    224 				tok->quote = Q_double;
    225 				break;
    226 
    227 			case Q_double:	/* Exit double quote mode */
    228 				tok->quote = Q_none;
    229 				break;
    230 
    231 			case Q_one:	/* Quote this " */
    232 				tok->quote = Q_none;
    233 				*tok->wptr++ = *ptr;
    234 				break;
    235 
    236 			case Q_single:	/* Stay in single quote mode */
    237 				*tok->wptr++ = *ptr;
    238 				break;
    239 
    240 			case Q_doubleone:	/* Quote this " */
    241 				tok->quote = Q_double;
    242 				*tok->wptr++ = *ptr;
    243 				break;
    244 
    245 			default:
    246 				return (-1);
    247 			}
    248 			break;
    249 
    250 		case '\\':
    251 			tok->flags |= TOK_KEEP;
    252 			tok->flags &= ~TOK_EAT;
    253 			switch (tok->quote) {
    254 			case Q_none:	/* Quote next character */
    255 				tok->quote = Q_one;
    256 				break;
    257 
    258 			case Q_double:	/* Quote next character */
    259 				tok->quote = Q_doubleone;
    260 				break;
    261 
    262 			case Q_one:	/* Quote this, restore state */
    263 				*tok->wptr++ = *ptr;
    264 				tok->quote = Q_none;
    265 				break;
    266 
    267 			case Q_single:	/* Stay in single quote mode */
    268 				*tok->wptr++ = *ptr;
    269 				break;
    270 
    271 			case Q_doubleone:	/* Quote this \ */
    272 				tok->quote = Q_double;
    273 				*tok->wptr++ = *ptr;
    274 				break;
    275 
    276 			default:
    277 				return (-1);
    278 			}
    279 			break;
    280 
    281 		case '\n':
    282 			tok->flags &= ~TOK_EAT;
    283 			switch (tok->quote) {
    284 			case Q_none:
    285 				tok_finish(tok);
    286 				*argv = (const char **)tok->argv;
    287 				*argc = tok->argc;
    288 				return (0);
    289 
    290 			case Q_single:
    291 			case Q_double:
    292 				*tok->wptr++ = *ptr;	/* Add the return */
    293 				break;
    294 
    295 			case Q_doubleone:   /* Back to double, eat the '\n' */
    296 				tok->flags |= TOK_EAT;
    297 				tok->quote = Q_double;
    298 				break;
    299 
    300 			case Q_one:	/* No quote, more eat the '\n' */
    301 				tok->flags |= TOK_EAT;
    302 				tok->quote = Q_none;
    303 				break;
    304 
    305 			default:
    306 				return (0);
    307 			}
    308 			break;
    309 
    310 		case '\0':
    311 			switch (tok->quote) {
    312 			case Q_none:
    313 				/* Finish word and return */
    314 				if (tok->flags & TOK_EAT) {
    315 					tok->flags &= ~TOK_EAT;
    316 					return (3);
    317 				}
    318 				tok_finish(tok);
    319 				*argv = (const char **)tok->argv;
    320 				*argc = tok->argc;
    321 				return (0);
    322 
    323 			case Q_single:
    324 				return (1);
    325 
    326 			case Q_double:
    327 				return (2);
    328 
    329 			case Q_doubleone:
    330 				tok->quote = Q_double;
    331 				*tok->wptr++ = *ptr;
    332 				break;
    333 
    334 			case Q_one:
    335 				tok->quote = Q_none;
    336 				*tok->wptr++ = *ptr;
    337 				break;
    338 
    339 			default:
    340 				return (-1);
    341 			}
    342 			break;
    343 
    344 		default:
    345 			tok->flags &= ~TOK_EAT;
    346 			switch (tok->quote) {
    347 			case Q_none:
    348 				if (strchr(tok->ifs, *ptr) != NULL)
    349 					tok_finish(tok);
    350 				else
    351 					*tok->wptr++ = *ptr;
    352 				break;
    353 
    354 			case Q_single:
    355 			case Q_double:
    356 				*tok->wptr++ = *ptr;
    357 				break;
    358 
    359 
    360 			case Q_doubleone:
    361 				*tok->wptr++ = '\\';
    362 				tok->quote = Q_double;
    363 				*tok->wptr++ = *ptr;
    364 				break;
    365 
    366 			case Q_one:
    367 				tok->quote = Q_none;
    368 				*tok->wptr++ = *ptr;
    369 				break;
    370 
    371 			default:
    372 				return (-1);
    373 
    374 			}
    375 			break;
    376 		}
    377 
    378 		if (tok->wptr >= tok->wmax - 4) {
    379 			size_t size = tok->wmax - tok->wspace + WINCR;
    380 			char *s = (char *) tok_realloc(tok->wspace, size);
    381 			if (s == NULL)
    382 				return (-1);
    383 
    384 			if (s != tok->wspace) {
    385 				int i;
    386 				for (i = 0; i < tok->argc; i++) {
    387 				    tok->argv[i] =
    388 					(tok->argv[i] - tok->wspace) + s;
    389 				}
    390 				tok->wptr = (tok->wptr - tok->wspace) + s;
    391 				tok->wstart = (tok->wstart - tok->wspace) + s;
    392 				tok->wspace = s;
    393 			}
    394 			tok->wmax = s + size;
    395 		}
    396 		if (tok->argc >= tok->amax - 4) {
    397 			char **p;
    398 			tok->amax += AINCR;
    399 			p = (char **) tok_realloc(tok->argv,
    400 			    tok->amax * sizeof(char *));
    401 			if (p == NULL)
    402 				return (-1);
    403 			tok->argv = p;
    404 		}
    405 	}
    406 }
    407