2 * Redistribution and use in source and binary forms, with or without
3 * modification, are permitted provided that the following conditions
5 * 1. Redistributions of source code must retain the above copyright
6 * notice, this list of conditions and the following disclaimer.
7 * 2. Redistributions in binary form must reproduce the above copyright
8 * notice, this list of conditions and the following disclaimer in the
9 * documentation and/or other materials provided with the distribution.
14 * $FreeBSD: src/sys/boot/common/interp_parse.c,v 1.8.2.1 2000/12/28 13:12:35 ps Exp $
16 * The meat of the simple parser.
21 #include "bootstrap.h"
23 static void clean(void);
24 static int insert(int *argcp, char *buf);
25 static char *variable_lookup(char *name);
27 #define PARSE_BUFSIZE 1024 /* maximum size of one element */
28 #define MAXARGS 20 /* maximum number of elements */
29 static char *args[MAXARGS];
32 * parse: accept a string of input and "parse" it for backslash
33 * substitutions and environment variable expansions (${var}),
34 * returning an argc/argv style vector of whitespace separated
35 * arguments. Returns 0 on success, 1 on failure (ok, ok, so I
36 * wimped-out on the error codes! :).
38 * Note that the argv array returned must be freed by the caller, but
39 * we own the space allocated for arguments and will free that on next
40 * invocation. This allows argv consumers to modify the array if
43 * NB: environment variables that expand to more than one whitespace
44 * separated token will be returned as a single argv[] element, not
45 * split in turn. Expanded text is also immune to further backslash
46 * elimination or expansion since this is a one-pass, non-recursive
47 * parser. You didn't specify more than this so if you want more, ask
51 #define PARSE_FAIL(expr) \
53 printf("fail at line %d\n", __LINE__); \
60 /* Accept the usual delimiters for a variable, returning counterpart */
74 return (ch == '\'' || ch == '"');
78 parse(int *argc, char ***argv, char *str)
81 char *val, *p, *q, *copy = NULL;
83 char token, tmp, quote, *buf;
84 enum { STR, VAR, WHITE } state;
88 if (!str || (p = copy = backslash(str)) == NULL)
91 /* Initialize vector and state */
94 buf = (char *)malloc(PARSE_BUFSIZE);
97 /* And awaaaaaaaaay we go! */
101 if ((*p == '\\') && p[1]) {
103 PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
105 } else if (isquote(*p)) {
106 quote = quote ? 0 : *p;
109 else if (isspace(*p) && !quote) {
113 PARSE_FAIL(insert(&ac, buf));
117 } else if (*p == '$') {
118 token = isdelim(*(p + 1));
125 PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
139 PARSE_FAIL((q = index(p, token)) == NULL);
142 while (*q && !isspace(*q))
147 if ((val = variable_lookup(p)) != NULL) {
148 size_t len = strlen(val);
150 strncpy(buf + i, val, PARSE_BUFSIZE - (i + 1));
151 i += min(len, PARSE_BUFSIZE - 1);
153 *q = tmp; /* restore value */
154 p = q + (token ? 1 : 0);
159 /* If at end of token, add it */
160 if (i && state == STR) {
162 PARSE_FAIL(insert(&ac, buf));
166 *argv = (char **)malloc((sizeof(char *) * ac + 1));
167 bcopy(args, *argv, sizeof(char *) * ac + 1);
175 /* Clean vector space */
181 for (i = 0; i < MAXARGS; i++) {
182 if (args[i] != NULL) {
190 insert(int *argcp, char *buf)
192 if (*argcp >= MAXARGS)
194 args[(*argcp)++] = strdup(buf);
199 variable_lookup(char *name)
201 /* XXX search "special variable" space first? */
202 return (char *)getenv(name);