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