xref: /freebsd/stand/common/interp_parse.c (revision e1e636193db45630c7881246d25902e57c43d24e)
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 <stand.h>
18 #include <string.h>
19 #include "bootstrap.h"
20 
21 static void	 clean(void);
22 static int	 insert(int *argcp, char *buf);
23 static char	*variable_lookup(char *name);
24 
25 #define PARSE_BUFSIZE	1024	/* maximum size of one element */
26 #define MAXARGS		20	/* maximum number of elements */
27 static char		*args[MAXARGS];
28 
29 /*
30  * parse: accept a string of input and "parse" it for backslash
31  * substitutions and environment variable expansions (${var}),
32  * returning an argc/argv style vector of whitespace separated
33  * arguments.  Returns 0 on success, 1 on failure (ok, ok, so I
34  * wimped-out on the error codes! :).
35  *
36  * Note that the argv array returned must be freed by the caller, but
37  * we own the space allocated for arguments and will free that on next
38  * invocation.  This allows argv consumers to modify the array if
39  * required.
40  *
41  * NB: environment variables that expand to more than one whitespace
42  * separated token will be returned as a single argv[] element, not
43  * split in turn.  Expanded text is also immune to further backslash
44  * elimination or expansion since this is a one-pass, non-recursive
45  * parser.  You didn't specify more than this so if you want more, ask
46  * me. - jkh
47  */
48 
49 #define PARSE_FAIL(expr) \
50 	if (expr) { \
51 		printf("fail at line %d\n", __LINE__); \
52 		clean(); \
53 		free(copy); \
54 		free(buf); \
55 		return 1; \
56 	}
57 
58 /* Accept the usual delimiters for a variable, returning counterpart */
59 static char
60 isdelim(int ch)
61 {
62 
63 	if (ch == '{')
64 		return '}';
65 	else if (ch == '(')
66 		return ')';
67 	return '\0';
68 }
69 
70 static int
71 isquote(int ch)
72 {
73 
74 	return (ch == '\'');
75 }
76 
77 static int
78 isdquote(int ch)
79 {
80 
81 	return (ch == '"');
82 }
83 
84 int
85 parse(int *argc, char ***argv, const char *str)
86 {
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;
92 
93 	ac = *argc = 0;
94 	dquote = quote = 0;
95 	if (!str || (p = copy = backslash(str)) == NULL)
96 		return 1;
97 
98 	/* Initialize vector and state */
99 	clean();
100 	state = STR;
101 	buf = (char *)malloc(PARSE_BUFSIZE);
102 	token = 0;
103 
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;
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 			} else if (isspace(*p) && !quote && !dquote) {
127 				state = WHITE;
128 				if (i) {
129 					buf[i] = '\0';
130 					PARSE_FAIL(insert(&ac, buf));
131 					i = 0;
132 				}
133 				++p;
134 			} else if (*p == '$' && !quote) {
135 				token = isdelim(*(p + 1));
136 				if (token)
137 					p += 2;
138 				else
139 					++p;
140 				state = VAR;
141 			} else {
142 				PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
143 				buf[i++] = *p++;
144 			}
145 			break;
146 
147 		case WHITE:
148 			if (isspace(*p))
149 				++p;
150 			else
151 				state = STR;
152 			break;
153 
154 		case VAR:
155 			if (token) {
156 				PARSE_FAIL((q = strchr(p, token)) == NULL);
157 			} else {
158 				q = p;
159 				while (*q && !isspace(*q))
160 					++q;
161 			}
162 			tmp = *q;
163 			*q = '\0';
164 			if ((val = variable_lookup(p)) != NULL) {
165 				size_t len = strlen(val);
166 
167 				strncpy(buf + i, val, PARSE_BUFSIZE - (i + 1));
168 				i += min(len, PARSE_BUFSIZE - 1);
169 			}
170 			*q = tmp;	/* restore value */
171 			p = q + (token ? 1 : 0);
172 			state = STR;
173 			break;
174 		}
175 	}
176 	/* missing terminating ' or " */
177 	PARSE_FAIL(quote || dquote);
178 	/* If at end of token, add it */
179 	if (i && state == STR) {
180 		buf[i] = '\0';
181 		PARSE_FAIL(insert(&ac, buf));
182 	}
183 	args[ac] = NULL;
184 	*argc = ac;
185 	*argv = (char **)malloc((sizeof(char *) * ac + 1));
186 	bcopy(args, *argv, sizeof(char *) * ac + 1);
187 	free(buf);
188 	free(copy);
189 	return 0;
190 }
191 
192 #define MAXARGS	20
193 
194 /* Clean vector space */
195 static void
196 clean(void)
197 {
198 	int		i;
199 
200 	for (i = 0; i < MAXARGS; i++) {
201 		if (args[i] != NULL) {
202 			free(args[i]);
203 			args[i] = NULL;
204 		}
205 	}
206 }
207 
208 static int
209 insert(int *argcp, char *buf)
210 {
211 
212 	if (*argcp >= MAXARGS)
213 		return 1;
214 	args[(*argcp)++] = strdup(buf);
215 	return 0;
216 }
217 
218 static char *
219 variable_lookup(char *name)
220 {
221 
222 	/* XXX search "special variable" space first? */
223 	return (char *)getenv(name);
224 }
225