xref: /illumos-gate/usr/src/boot/common/interp_parse.c (revision 30b280d1254c7864ed6ca5515d969e55e82bf2b9)
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.
10  *
11  * Jordan K. Hubbard
12  * 29 August 1998
13  *
14  * The meat of the simple parser.
15  */
16 
17 #include <sys/cdefs.h>
18 
19 #include <stand.h>
20 #include <string.h>
21 #include "bootstrap.h"
22 
23 static void	 clean(void);
24 static int	 insert(int *argcp, char *buf);
25 static char	*variable_lookup(char *name);
26 
27 #define	PARSE_BUFSIZE	1024	/* maximum size of one element */
28 #define	MAXARGS		20	/* maximum number of elements */
29 static char		*args[MAXARGS];
30 
31 /*
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! :).
37  *
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
41  * required.
42  *
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
48  * me. - jkh
49  */
50 
51 #define	PARSE_FAIL(expr) \
52 	if (expr) { \
53 		printf("fail at line %d\n", __LINE__); \
54 		clean(); \
55 		free(copy); \
56 		free(buf); \
57 		return (1); \
58 	}
59 
60 /* Accept the usual delimiters for a variable, returning counterpart */
61 static char
62 isdelim(int ch)
63 {
64 	if (ch == '{')
65 		return ('}');
66 	else if (ch == '(')
67 		return (')');
68 	return ('\0');
69 }
70 
71 static int
72 isquote(int ch)
73 {
74 	return (ch == '\'');
75 }
76 
77 static int
78 isdquote(int ch)
79 {
80 	return (ch == '"');
81 }
82 
83 int
84 parse(int *argc, char ***argv, char *str)
85 {
86 	int ac;
87 	char *val, *p, *q, *copy = NULL;
88 	size_t i = 0;
89 	char token, tmp, quote, dquote, *buf;
90 	enum { STR, VAR, WHITE } state;
91 
92 	ac = *argc = 0;
93 	dquote = quote = 0;
94 	if (!str || (p = copy = backslash(str)) == NULL)
95 		return (1);
96 
97 	/* Initialize vector and state */
98 	clean();
99 	state = STR;
100 	buf = malloc(PARSE_BUFSIZE);
101 	token = 0;
102 
103 	/* And awaaaaaaaaay we go! */
104 	while (*p) {
105 		switch (state) {
106 		case STR:
107 			if ((*p == '\\') && p[1]) {
108 				p++;
109 				PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
110 				buf[i++] = *p++;
111 			} else if (isquote(*p)) {
112 				quote = quote ? 0 : *p;
113 				if (dquote) { /* keep quote */
114 					PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
115 					buf[i++] = *p++;
116 				} else {
117 					++p;
118 				}
119 			} else if (isdquote(*p)) {
120 				dquote = dquote ? 0 : *p;
121 				if (quote) { /* keep dquote */
122 					PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
123 					buf[i++] = *p++;
124 				} else {
125 					++p;
126 				}
127 			} else if (isspace(*p) && !quote && !dquote) {
128 				state = WHITE;
129 				if (i) {
130 					buf[i] = '\0';
131 					PARSE_FAIL(insert(&ac, buf));
132 					i = 0;
133 				}
134 				++p;
135 			} else if (*p == '$' && !quote) {
136 				token = isdelim(*(p + 1));
137 				if (token)
138 					p += 2;
139 				else
140 					++p;
141 				state = VAR;
142 			} else {
143 				PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
144 				buf[i++] = *p++;
145 			}
146 			break;
147 
148 		case WHITE:
149 			if (isspace(*p))
150 				++p;
151 			else
152 				state = STR;
153 			break;
154 
155 		case VAR:
156 			if (token) {
157 				PARSE_FAIL((q = strchr(p, token)) == NULL);
158 			} else {
159 				q = p;
160 				while (*q && !isspace(*q))
161 					++q;
162 			}
163 			tmp = *q;
164 			*q = '\0';
165 			if ((val = variable_lookup(p)) != NULL) {
166 				size_t len = strlen(val);
167 
168 				(void) strncpy(buf + i, val,
169 				    PARSE_BUFSIZE - (i + 1));
170 				i += min(len, PARSE_BUFSIZE - 1);
171 			}
172 			*q = tmp;	/* restore value */
173 			p = q + (token ? 1 : 0);
174 			state = STR;
175 			break;
176 		}
177 	}
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));
184 	}
185 	args[ac] = NULL;
186 	*argc = ac;
187 	*argv = malloc((sizeof (char *) * ac + 1));
188 	bcopy(args, *argv, sizeof (char *) * ac + 1);
189 	free(buf);
190 	free(copy);
191 	return (0);
192 }
193 
194 #define	MAXARGS	20
195 
196 /* Clean vector space */
197 static void
198 clean(void)
199 {
200 	int i;
201 
202 	for (i = 0; i < MAXARGS; i++) {
203 		free(args[i]);
204 		args[i] = NULL;
205 	}
206 }
207 
208 static int
209 insert(int *argcp, char *buf)
210 {
211 	if (*argcp >= MAXARGS)
212 		return (1);
213 	args[(*argcp)++] = strdup(buf);
214 	return (0);
215 }
216 
217 static char *
218 variable_lookup(char *name)
219 {
220 
221 	/* XXX search "special variable" space first? */
222 	return (getenv(name));
223 }
224