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