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
isdelim(int ch)62 isdelim(int ch)
63 {
64 	if (ch == '{')
65 		return ('}');
66 	else if (ch == '(')
67 		return (')');
68 	return ('\0');
69 }
70 
71 static int
isquote(int ch)72 isquote(int ch)
73 {
74 	return (ch == '\'');
75 }
76 
77 static int
isdquote(int ch)78 isdquote(int ch)
79 {
80 	return (ch == '"');
81 }
82 
83 int
parse(int * argc,char *** argv,char * str)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 				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 = 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
clean(void)197 clean(void)
198 {
199 	int i;
200 
201 	for (i = 0; i < MAXARGS; i++) {
202 		free(args[i]);
203 		args[i] = NULL;
204 	}
205 }
206 
207 static int
insert(int * argcp,char * buf)208 insert(int *argcp, char *buf)
209 {
210 	if (*argcp >= MAXARGS)
211 		return (1);
212 	args[(*argcp)++] = strdup(buf);
213 	return (0);
214 }
215 
216 static char *
variable_lookup(char * name)217 variable_lookup(char *name)
218 {
219 
220 	/* XXX search "special variable" space first? */
221 	return (getenv(name));
222 }
223