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 * The meat of the simple parser.
17 #include <sys/cdefs.h>
20 #include "bootstrap.h"
22 static void clean(void);
23 static int insert(int *argcp, char *buf);
24 static char *variable_lookup(char *name);
26 #define PARSE_BUFSIZE 1024 /* maximum size of one element */
27 #define MAXARGS 20 /* maximum number of elements */
28 static char *args[MAXARGS];
31 * parse: accept a string of input and "parse" it for backslash
32 * substitutions and environment variable expansions (${var}),
33 * returning an argc/argv style vector of whitespace separated
34 * arguments. Returns 0 on success, 1 on failure (ok, ok, so I
35 * wimped-out on the error codes! :).
37 * Note that the argv array returned must be freed by the caller, but
38 * we own the space allocated for arguments and will free that on next
39 * invocation. This allows argv consumers to modify the array if
42 * NB: environment variables that expand to more than one whitespace
43 * separated token will be returned as a single argv[] element, not
44 * split in turn. Expanded text is also immune to further backslash
45 * elimination or expansion since this is a one-pass, non-recursive
46 * parser. You didn't specify more than this so if you want more, ask
50 #define PARSE_FAIL(expr) \
52 printf("fail at line %d\n", __LINE__); \
59 /* Accept the usual delimiters for a variable, returning counterpart */
86 parse(int *argc, char ***argv, const char *str)
89 char *val, *p, *q, *copy = NULL;
91 char token, tmp, quote, dquote, *buf;
92 enum { STR, VAR, WHITE } state;
96 if (!str || (p = copy = backslash(str)) == NULL)
99 /* Initialize vector and state */
102 buf = (char *)malloc(PARSE_BUFSIZE);
105 /* And awaaaaaaaaay we go! */
109 if ((*p == '\\') && p[1]) {
111 PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
113 } else if (isquote(*p)) {
114 quote = quote ? 0 : *p;
115 if (dquote) { /* keep quote */
116 PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
120 } else if (isdquote(*p)) {
121 dquote = dquote ? 0 : *p;
122 if (quote) { /* keep dquote */
123 PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
127 } else if (isspace(*p) && !quote && !dquote) {
131 PARSE_FAIL(insert(&ac, buf));
135 } else if (*p == '$' && !quote) {
136 token = isdelim(*(p + 1));
143 PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
157 PARSE_FAIL((q = strchr(p, token)) == NULL);
160 while (*q && !isspace(*q))
165 if ((val = variable_lookup(p)) != NULL) {
166 size_t len = strlen(val);
168 strncpy(buf + i, val, PARSE_BUFSIZE - (i + 1));
169 i += min(len, PARSE_BUFSIZE - 1);
171 *q = tmp; /* restore value */
172 p = q + (token ? 1 : 0);
177 /* missing terminating ' or " */
178 PARSE_FAIL(quote || dquote);
179 /* If at end of token, add it */
180 if (i && state == STR) {
182 PARSE_FAIL(insert(&ac, buf));
186 *argv = (char **)malloc((sizeof(char *) * ac + 1));
187 bcopy(args, *argv, sizeof(char *) * ac + 1);
195 /* Clean vector space */
201 for (i = 0; i < MAXARGS; i++) {
202 if (args[i] != NULL) {
210 insert(int *argcp, char *buf)
213 if (*argcp >= MAXARGS)
215 args[(*argcp)++] = strdup(buf);
220 variable_lookup(char *name)
223 /* XXX search "special variable" space first? */
224 return (char *)getenv(name);