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