8748 loader: ptblread() is broken with >512B sectors
[unleashed.git] / usr / src / boot / sys / boot / common / interp_parse.c
blob64ddadea21303a92de61c0741d43e456318d340f
1 /*-
2 * Redistribution and use in source and binary forms, with or without
3 * modification, are permitted provided that the following conditions
4 * are met:
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.
11 * Jordan K. Hubbard
12 * 29 August 1998
14 * The meat of the simple parser.
17 #include <sys/cdefs.h>
18 __FBSDID("$FreeBSD$");
20 #include <stand.h>
21 #include <string.h>
22 #include "bootstrap.h"
24 static void clean(void);
25 static int insert(int *argcp, char *buf);
26 static char *variable_lookup(char *name);
28 #define PARSE_BUFSIZE 1024 /* maximum size of one element */
29 #define MAXARGS 20 /* maximum number of elements */
30 static char *args[MAXARGS];
33 * parse: accept a string of input and "parse" it for backslash
34 * substitutions and environment variable expansions (${var}),
35 * returning an argc/argv style vector of whitespace separated
36 * arguments. Returns 0 on success, 1 on failure (ok, ok, so I
37 * wimped-out on the error codes! :).
39 * Note that the argv array returned must be freed by the caller, but
40 * we own the space allocated for arguments and will free that on next
41 * invocation. This allows argv consumers to modify the array if
42 * required.
44 * NB: environment variables that expand to more than one whitespace
45 * separated token will be returned as a single argv[] element, not
46 * split in turn. Expanded text is also immune to further backslash
47 * elimination or expansion since this is a one-pass, non-recursive
48 * parser. You didn't specify more than this so if you want more, ask
49 * me. - jkh
52 #define PARSE_FAIL(expr) \
53 if (expr) { \
54 printf("fail at line %d\n", __LINE__); \
55 clean(); \
56 free(copy); \
57 free(buf); \
58 return 1; \
61 /* Accept the usual delimiters for a variable, returning counterpart */
62 static char
63 isdelim(int ch)
65 if (ch == '{')
66 return '}';
67 else if (ch == '(')
68 return ')';
69 return '\0';
72 static int
73 isquote(int ch)
75 return (ch == '\'');
78 static int
79 isdquote(int ch)
81 return (ch == '"');
84 int
85 parse(int *argc, char ***argv, char *str)
87 int ac;
88 char *val, *p, *q, *copy = NULL;
89 size_t i = 0;
90 char token, tmp, quote, dquote, *buf;
91 enum { STR, VAR, WHITE } state;
93 ac = *argc = 0;
94 dquote = quote = 0;
95 if (!str || (p = copy = backslash(str)) == NULL)
96 return 1;
98 /* Initialize vector and state */
99 clean();
100 state = STR;
101 buf = (char *)malloc(PARSE_BUFSIZE);
102 token = 0;
104 /* And awaaaaaaaaay we go! */
105 while (*p) {
106 switch (state) {
107 case STR:
108 if ((*p == '\\') && p[1]) {
109 p++;
110 PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
111 buf[i++] = *p++;
112 } else if (isquote(*p)) {
113 quote = quote ? 0 : *p;
114 if (dquote) { /* keep quote */
115 PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
116 buf[i++] = *p++;
117 } else {
118 ++p;
120 } else if (isdquote(*p)) {
121 dquote = dquote ? 0 : *p;
122 if (quote) { /* keep dquote */
123 PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
124 buf[i++] = *p++;
125 } else {
126 ++p;
128 } else if (isspace(*p) && !quote && !dquote) {
129 state = WHITE;
130 if (i) {
131 buf[i] = '\0';
132 PARSE_FAIL(insert(&ac, buf));
133 i = 0;
135 ++p;
136 } else if (*p == '$' && !quote) {
137 token = isdelim(*(p + 1));
138 if (token)
139 p += 2;
140 else
141 ++p;
142 state = VAR;
143 } else {
144 PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
145 buf[i++] = *p++;
147 break;
149 case WHITE:
150 if (isspace(*p))
151 ++p;
152 else
153 state = STR;
154 break;
156 case VAR:
157 if (token) {
158 PARSE_FAIL((q = strchr(p, token)) == NULL);
159 } else {
160 q = p;
161 while (*q && !isspace(*q))
162 ++q;
164 tmp = *q;
165 *q = '\0';
166 if ((val = variable_lookup(p)) != NULL) {
167 size_t len = strlen(val);
169 strncpy(buf + i, val, PARSE_BUFSIZE - (i + 1));
170 i += min(len, PARSE_BUFSIZE - 1);
172 *q = tmp; /* restore value */
173 p = q + (token ? 1 : 0);
174 state = STR;
175 break;
178 /* missing terminating ' or " */
179 PARSE_FAIL(quote || dquote);
180 /* If at end of token, add it */
181 if (i && state == STR) {
182 buf[i] = '\0';
183 PARSE_FAIL(insert(&ac, buf));
185 args[ac] = NULL;
186 *argc = ac;
187 *argv = (char **)malloc((sizeof(char *) * ac + 1));
188 bcopy(args, *argv, sizeof(char *) * ac + 1);
189 free(buf);
190 free(copy);
191 return 0;
194 #define MAXARGS 20
196 /* Clean vector space */
197 static void
198 clean(void)
200 int i;
202 for (i = 0; i < MAXARGS; i++) {
203 if (args[i] != NULL) {
204 free(args[i]);
205 args[i] = NULL;
210 static int
211 insert(int *argcp, char *buf)
213 if (*argcp >= MAXARGS)
214 return 1;
215 args[(*argcp)++] = strdup(buf);
216 return 0;
219 static char *
220 variable_lookup(char *name)
222 /* XXX search "special variable" space first? */
223 return (char *)getenv(name);