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 return (NULL); 118 tok->argv[0] = NULL; 119 tok->wspace = (char *) tok_malloc(WINCR); 120 if (tok->wspace == NULL) 121 return (NULL); 122 tok->wmax = tok->wspace + WINCR; 123 tok->wstart = tok->wspace; 124 tok->wptr = tok->wspace; 125 tok->flags = 0; 126 tok->quote = Q_none; 127 128 return (tok); 129 } 130 131 132 /* tok_reset(): 133 * Reset the tokenizer 134 */ 135 public void 136 tok_reset(Tokenizer *tok) 137 { 138 139 tok->argc = 0; 140 tok->wstart = tok->wspace; 141 tok->wptr = tok->wspace; 142 tok->flags = 0; 143 tok->quote = Q_none; 144 } 145 146 147 /* tok_end(): 148 * Clean up 149 */ 150 public void 151 tok_end(Tokenizer *tok) 152 { 153 154 tok_free((ptr_t) tok->ifs); 155 tok_free((ptr_t) tok->wspace); 156 tok_free((ptr_t) tok->argv); 157 tok_free((ptr_t) tok); 158 } 159 160 161 162 /* tok_line(): 163 * Bourne shell like tokenizing 164 * Return: 165 * -1: Internal error 166 * 3: Quoted return 167 * 2: Unmatched double quote 168 * 1: Unmatched single quote 169 * 0: Ok 170 */ 171 public int 172 tok_line(Tokenizer *tok, const char *line, int *argc, const char ***argv) 173 { 174 const char *ptr; 175 176 for (;;) { 177 switch (*(ptr = line++)) { 178 case '\'': 179 tok->flags |= TOK_KEEP; 180 tok->flags &= ~TOK_EAT; 181 switch (tok->quote) { 182 case Q_none: 183 tok->quote = Q_single; /* Enter single quote 184 * mode */ 185 break; 186 187 case Q_single: /* Exit single quote mode */ 188 tok->quote = Q_none; 189 break; 190 191 case Q_one: /* Quote this ' */ 192 tok->quote = Q_none; 193 *tok->wptr++ = *ptr; 194 break; 195 196 case Q_double: /* Stay in double quote mode */ 197 *tok->wptr++ = *ptr; 198 break; 199 200 case Q_doubleone: /* Quote this ' */ 201 tok->quote = Q_double; 202 *tok->wptr++ = *ptr; 203 break; 204 205 default: 206 return (-1); 207 } 208 break; 209 210 case '"': 211 tok->flags &= ~TOK_EAT; 212 tok->flags |= TOK_KEEP; 213 switch (tok->quote) { 214 case Q_none: /* Enter double quote mode */ 215 tok->quote = Q_double; 216 break; 217 218 case Q_double: /* Exit double quote mode */ 219 tok->quote = Q_none; 220 break; 221 222 case Q_one: /* Quote this " */ 223 tok->quote = Q_none; 224 *tok->wptr++ = *ptr; 225 break; 226 227 case Q_single: /* Stay in single quote mode */ 228 *tok->wptr++ = *ptr; 229 break; 230 231 case Q_doubleone: /* Quote this " */ 232 tok->quote = Q_double; 233 *tok->wptr++ = *ptr; 234 break; 235 236 default: 237 return (-1); 238 } 239 break; 240 241 case '\\': 242 tok->flags |= TOK_KEEP; 243 tok->flags &= ~TOK_EAT; 244 switch (tok->quote) { 245 case Q_none: /* Quote next character */ 246 tok->quote = Q_one; 247 break; 248 249 case Q_double: /* Quote next character */ 250 tok->quote = Q_doubleone; 251 break; 252 253 case Q_one: /* Quote this, restore state */ 254 *tok->wptr++ = *ptr; 255 tok->quote = Q_none; 256 break; 257 258 case Q_single: /* Stay in single quote mode */ 259 *tok->wptr++ = *ptr; 260 break; 261 262 case Q_doubleone: /* Quote this \ */ 263 tok->quote = Q_double; 264 *tok->wptr++ = *ptr; 265 break; 266 267 default: 268 return (-1); 269 } 270 break; 271 272 case '\n': 273 tok->flags &= ~TOK_EAT; 274 switch (tok->quote) { 275 case Q_none: 276 tok_finish(tok); 277 *argv = (const char **)tok->argv; 278 *argc = tok->argc; 279 return (0); 280 281 case Q_single: 282 case Q_double: 283 *tok->wptr++ = *ptr; /* Add the return */ 284 break; 285 286 case Q_doubleone: /* Back to double, eat the '\n' */ 287 tok->flags |= TOK_EAT; 288 tok->quote = Q_double; 289 break; 290 291 case Q_one: /* No quote, more eat the '\n' */ 292 tok->flags |= TOK_EAT; 293 tok->quote = Q_none; 294 break; 295 296 default: 297 return (0); 298 } 299 break; 300 301 case '\0': 302 switch (tok->quote) { 303 case Q_none: 304 /* Finish word and return */ 305 if (tok->flags & TOK_EAT) { 306 tok->flags &= ~TOK_EAT; 307 return (3); 308 } 309 tok_finish(tok); 310 *argv = (const char **)tok->argv; 311 *argc = tok->argc; 312 return (0); 313 314 case Q_single: 315 return (1); 316 317 case Q_double: 318 return (2); 319 320 case Q_doubleone: 321 tok->quote = Q_double; 322 *tok->wptr++ = *ptr; 323 break; 324 325 case Q_one: 326 tok->quote = Q_none; 327 *tok->wptr++ = *ptr; 328 break; 329 330 default: 331 return (-1); 332 } 333 break; 334 335 default: 336 tok->flags &= ~TOK_EAT; 337 switch (tok->quote) { 338 case Q_none: 339 if (strchr(tok->ifs, *ptr) != NULL) 340 tok_finish(tok); 341 else 342 *tok->wptr++ = *ptr; 343 break; 344 345 case Q_single: 346 case Q_double: 347 *tok->wptr++ = *ptr; 348 break; 349 350 351 case Q_doubleone: 352 *tok->wptr++ = '\\'; 353 tok->quote = Q_double; 354 *tok->wptr++ = *ptr; 355 break; 356 357 case Q_one: 358 tok->quote = Q_none; 359 *tok->wptr++ = *ptr; 360 break; 361 362 default: 363 return (-1); 364 365 } 366 break; 367 } 368 369 if (tok->wptr >= tok->wmax - 4) { 370 size_t size = tok->wmax - tok->wspace + WINCR; 371 char *s = (char *) tok_realloc(tok->wspace, size); 372 if (s == NULL) 373 return (-1); 374 375 if (s != tok->wspace) { 376 int i; 377 for (i = 0; i < tok->argc; i++) { 378 tok->argv[i] = 379 (tok->argv[i] - tok->wspace) + s; 380 } 381 tok->wptr = (tok->wptr - tok->wspace) + s; 382 tok->wstart = (tok->wstart - tok->wspace) + s; 383 tok->wspace = s; 384 } 385 tok->wmax = s + size; 386 } 387 if (tok->argc >= tok->amax - 4) { 388 char **p; 389 tok->amax += AINCR; 390 p = (char **) tok_realloc(tok->argv, 391 tok->amax * sizeof(char *)); 392 if (p == NULL) 393 return (-1); 394 tok->argv = p; 395 } 396 } 397 } 398