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