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