xref: /freebsd/stand/common/interp_parse.c (revision 61ba55bcf70f2340f9c943c9571113b3fd8eda69)
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 #include <stand.h>
19 #include <string.h>
20 #include "bootstrap.h"
21 
22 static void	 clean(void);
23 static int	 insert(int *argcp, char *buf);
24 static char	*variable_lookup(char *name);
25 
26 #define PARSE_BUFSIZE	1024	/* maximum size of one element */
27 #define MAXARGS		20	/* maximum number of elements */
28 static char		*args[MAXARGS];
29 
30 /*
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! :).
36  *
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
40  * required.
41  *
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
47  * me. - jkh
48  */
49 
50 #define PARSE_FAIL(expr) \
51 	if (expr) { \
52 		printf("fail at line %d\n", __LINE__); \
53 		clean(); \
54 		free(copy); \
55 		free(buf); \
56 		return 1; \
57 	}
58 
59 /* Accept the usual delimiters for a variable, returning counterpart */
60 static char
61 isdelim(int ch)
62 {
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 
75 	return (ch == '\'');
76 }
77 
78 static int
79 isdquote(int ch)
80 {
81 
82 	return (ch == '"');
83 }
84 
85 int
86 parse(int *argc, char ***argv, const char *str)
87 {
88 	int ac;
89 	char *val, *p, *q, *copy = NULL;
90 	size_t i = 0;
91 	char token, tmp, quote, dquote, *buf;
92 	enum { STR, VAR, WHITE } state;
93 
94 	ac = *argc = 0;
95 	dquote = quote = 0;
96 	if (!str || (p = copy = backslash(str)) == NULL)
97 		return 1;
98 
99 	/* Initialize vector and state */
100 	clean();
101 	state = STR;
102 	buf = (char *)malloc(PARSE_BUFSIZE);
103 	token = 0;
104 
105 	/* And awaaaaaaaaay we go! */
106 	while (*p) {
107 		switch (state) {
108 		case STR:
109 			if ((*p == '\\') && p[1]) {
110 				p++;
111 				PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
112 				buf[i++] = *p++;
113 			} else if (isquote(*p)) {
114 				quote = quote ? 0 : *p;
115 				if (dquote) { /* keep quote */
116 					PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
117 					buf[i++] = *p++;
118 				} else
119 					++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;
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 				strncpy(buf + i, val, PARSE_BUFSIZE - (i + 1));
169 				i += min(len, PARSE_BUFSIZE - 1);
170 			}
171 			*q = tmp;	/* restore value */
172 			p = q + (token ? 1 : 0);
173 			state = STR;
174 			break;
175 		}
176 	}
177 	/* missing terminating ' or " */
178 	PARSE_FAIL(quote || dquote);
179 	/* If at end of token, add it */
180 	if (i && state == STR) {
181 		buf[i] = '\0';
182 		PARSE_FAIL(insert(&ac, buf));
183 	}
184 	args[ac] = NULL;
185 	*argc = ac;
186 	*argv = (char **)malloc((sizeof(char *) * ac + 1));
187 	bcopy(args, *argv, sizeof(char *) * ac + 1);
188 	free(buf);
189 	free(copy);
190 	return 0;
191 }
192 
193 #define MAXARGS	20
194 
195 /* Clean vector space */
196 static void
197 clean(void)
198 {
199 	int		i;
200 
201 	for (i = 0; i < MAXARGS; i++) {
202 		if (args[i] != NULL) {
203 			free(args[i]);
204 			args[i] = NULL;
205 		}
206 	}
207 }
208 
209 static int
210 insert(int *argcp, char *buf)
211 {
212 
213 	if (*argcp >= MAXARGS)
214 		return 1;
215 	args[(*argcp)++] = strdup(buf);
216 	return 0;
217 }
218 
219 static char *
220 variable_lookup(char *name)
221 {
222 
223 	/* XXX search "special variable" space first? */
224 	return (char *)getenv(name);
225 }
226