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