interp_parse.c revision 1.1
11.1Scherry/* $NetBSD: interp_parse.c,v 1.1 2006/04/07 14:21:29 cherry Exp $ */ 21.1Scherry 31.1Scherry/*- 41.1Scherry * Redistribution and use in source and binary forms, with or without 51.1Scherry * modification, are permitted provided that the following conditions 61.1Scherry * are met: 71.1Scherry * 1. Redistributions of source code must retain the above copyright 81.1Scherry * notice, this list of conditions and the following disclaimer. 91.1Scherry * 2. Redistributions in binary form must reproduce the above copyright 101.1Scherry * notice, this list of conditions and the following disclaimer in the 111.1Scherry * documentation and/or other materials provided with the distribution. 121.1Scherry * 131.1Scherry * Jordan K. Hubbard 141.1Scherry * 29 August 1998 151.1Scherry * 161.1Scherry * The meat of the simple parser. 171.1Scherry */ 181.1Scherry 191.1Scherry#include <sys/cdefs.h> 201.1Scherry 211.1Scherry#include <lib/libsa/stand.h> 221.1Scherry#include <lib/libkern/libkern.h> 231.1Scherry 241.1Scherry#include "bootstrap.h" 251.1Scherry 261.1Scherrystatic void clean(void); 271.1Scherrystatic int insert(int *argcp, char *buf); 281.1Scherrystatic char *variable_lookup(char *name); 291.1Scherry 301.1Scherry#define PARSE_BUFSIZE 1024 /* maximum size of one element */ 311.1Scherry#define MAXARGS 20 /* maximum number of elements */ 321.1Scherrystatic char *args[MAXARGS]; 331.1Scherry 341.1Scherry/* 351.1Scherry * parse: accept a string of input and "parse" it for backslash 361.1Scherry * substitutions and environment variable expansions (${var}), 371.1Scherry * returning an argc/argv style vector of whitespace separated 381.1Scherry * arguments. Returns 0 on success, 1 on failure (ok, ok, so I 391.1Scherry * wimped-out on the error codes! :). 401.1Scherry * 411.1Scherry * Note that the argv array returned must be freed by the caller, but 421.1Scherry * we own the space allocated for arguments and will free that on next 431.1Scherry * invocation. This allows argv consumers to modify the array if 441.1Scherry * required. 451.1Scherry * 461.1Scherry * NB: environment variables that expand to more than one whitespace 471.1Scherry * separated token will be returned as a single argv[] element, not 481.1Scherry * split in turn. Expanded text is also immune to further backslash 491.1Scherry * elimination or expansion since this is a one-pass, non-recursive 501.1Scherry * parser. You didn't specify more than this so if you want more, ask 511.1Scherry * me. - jkh 521.1Scherry */ 531.1Scherry 541.1Scherry#define PARSE_FAIL(expr) \ 551.1Scherryif (expr) { \ 561.1Scherry printf("fail at line %d\n", __LINE__); \ 571.1Scherry clean(); \ 581.1Scherry free(copy); \ 591.1Scherry free(buf); \ 601.1Scherry return 1; \ 611.1Scherry} 621.1Scherry 631.1Scherry/* Accept the usual delimiters for a variable, returning counterpart */ 641.1Scherrystatic char 651.1Scherryisdelim(int ch) 661.1Scherry{ 671.1Scherry if (ch == '{') 681.1Scherry return '}'; 691.1Scherry else if (ch == '(') 701.1Scherry return ')'; 711.1Scherry return '\0'; 721.1Scherry} 731.1Scherry 741.1Scherrystatic int 751.1Scherryisquote(int ch) 761.1Scherry{ 771.1Scherry return (ch == '\'' || ch == '"'); 781.1Scherry} 791.1Scherry 801.1Scherryint 811.1Scherryparse(int *argc, char ***argv, char *str) 821.1Scherry{ 831.1Scherry int ac; 841.1Scherry char *val, *p, *q, *copy = NULL; 851.1Scherry size_t i = 0; 861.1Scherry char token, tmp, quote, *buf; 871.1Scherry enum { STR, VAR, WHITE } state; 881.1Scherry 891.1Scherry ac = *argc = 0; 901.1Scherry quote = 0; 911.1Scherry if (!str || (p = copy = backslash(str)) == NULL) 921.1Scherry return 1; 931.1Scherry 941.1Scherry /* Initialize vector and state */ 951.1Scherry clean(); 961.1Scherry state = STR; 971.1Scherry buf = (char *)alloc(PARSE_BUFSIZE); 981.1Scherry token = 0; 991.1Scherry 1001.1Scherry /* And awaaaaaaaaay we go! */ 1011.1Scherry while (*p) { 1021.1Scherry switch (state) { 1031.1Scherry case STR: 1041.1Scherry if ((*p == '\\') && p[1]) { 1051.1Scherry p++; 1061.1Scherry PARSE_FAIL(i == (PARSE_BUFSIZE - 1)); 1071.1Scherry buf[i++] = *p++; 1081.1Scherry } else if (isquote(*p)) { 1091.1Scherry quote = quote ? 0 : *p; 1101.1Scherry ++p; 1111.1Scherry } 1121.1Scherry else if (isspace(*p) && !quote) { 1131.1Scherry state = WHITE; 1141.1Scherry if (i) { 1151.1Scherry buf[i] = '\0'; 1161.1Scherry PARSE_FAIL(insert(&ac, buf)); 1171.1Scherry i = 0; 1181.1Scherry } 1191.1Scherry ++p; 1201.1Scherry } else if (*p == '$') { 1211.1Scherry token = isdelim(*(p + 1)); 1221.1Scherry if (token) 1231.1Scherry p += 2; 1241.1Scherry else 1251.1Scherry ++p; 1261.1Scherry state = VAR; 1271.1Scherry } else { 1281.1Scherry PARSE_FAIL(i == (PARSE_BUFSIZE - 1)); 1291.1Scherry buf[i++] = *p++; 1301.1Scherry } 1311.1Scherry break; 1321.1Scherry 1331.1Scherry case WHITE: 1341.1Scherry if (isspace(*p)) 1351.1Scherry ++p; 1361.1Scherry else 1371.1Scherry state = STR; 1381.1Scherry break; 1391.1Scherry 1401.1Scherry case VAR: 1411.1Scherry if (token) { 1421.1Scherry PARSE_FAIL((q = strchr(p, token)) == NULL); 1431.1Scherry } else { 1441.1Scherry q = p; 1451.1Scherry while (*q && !isspace(*q)) 1461.1Scherry ++q; 1471.1Scherry } 1481.1Scherry tmp = *q; 1491.1Scherry *q = '\0'; 1501.1Scherry if ((val = variable_lookup(p)) != NULL) { 1511.1Scherry size_t len = strlen(val); 1521.1Scherry 1531.1Scherry strncpy(buf + i, val, PARSE_BUFSIZE - (i + 1)); 1541.1Scherry i += min(len, PARSE_BUFSIZE - 1); 1551.1Scherry } 1561.1Scherry *q = tmp; /* restore value */ 1571.1Scherry p = q + (token ? 1 : 0); 1581.1Scherry state = STR; 1591.1Scherry break; 1601.1Scherry } 1611.1Scherry } 1621.1Scherry /* If at end of token, add it */ 1631.1Scherry if (i && state == STR) { 1641.1Scherry buf[i] = '\0'; 1651.1Scherry PARSE_FAIL(insert(&ac, buf)); 1661.1Scherry } 1671.1Scherry args[ac] = NULL; 1681.1Scherry *argc = ac; 1691.1Scherry *argv = (char **)alloc((sizeof(char *) * ac + 1)); 1701.1Scherry bcopy(args, *argv, sizeof(char *) * ac + 1); 1711.1Scherry free(buf); 1721.1Scherry free(copy); 1731.1Scherry return 0; 1741.1Scherry} 1751.1Scherry 1761.1Scherry#define MAXARGS 20 1771.1Scherry 1781.1Scherry/* Clean vector space */ 1791.1Scherrystatic void 1801.1Scherryclean(void) 1811.1Scherry{ 1821.1Scherry int i; 1831.1Scherry 1841.1Scherry for (i = 0; i < MAXARGS; i++) { 1851.1Scherry if (args[i] != NULL) { 1861.1Scherry free(args[i]); 1871.1Scherry args[i] = NULL; 1881.1Scherry } 1891.1Scherry } 1901.1Scherry} 1911.1Scherry 1921.1Scherrystatic int 1931.1Scherryinsert(int *argcp, char *buf) 1941.1Scherry{ 1951.1Scherry if (*argcp >= MAXARGS) 1961.1Scherry return 1; 1971.1Scherry args[(*argcp)++] = strdup(buf); 1981.1Scherry return 0; 1991.1Scherry} 2001.1Scherry 2011.1Scherrystatic char * 2021.1Scherryvariable_lookup(char *name) 2031.1Scherry{ 2041.1Scherry /* XXX search "special variable" space first? */ 2051.1Scherry return (char *)getenv(name); 2061.1Scherry} 207