1d66a72cfSToomas Soome /*
2199767f8SToomas Soome  * Redistribution and use in source and binary forms, with or without
3199767f8SToomas Soome  * modification, are permitted provided that the following conditions
4199767f8SToomas Soome  * are met:
5199767f8SToomas Soome  * 1. Redistributions of source code must retain the above copyright
6199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer.
7199767f8SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
8199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
9199767f8SToomas Soome  *    documentation and/or other materials provided with the distribution.
10d66a72cfSToomas Soome  *
11199767f8SToomas Soome  * Jordan K. Hubbard
12199767f8SToomas Soome  * 29 August 1998
13199767f8SToomas Soome  *
14199767f8SToomas Soome  * The meat of the simple parser.
15199767f8SToomas Soome  */
16199767f8SToomas Soome 
17199767f8SToomas Soome #include <sys/cdefs.h>
18199767f8SToomas Soome 
19199767f8SToomas Soome #include <stand.h>
20199767f8SToomas Soome #include <string.h>
21199767f8SToomas Soome #include "bootstrap.h"
22199767f8SToomas Soome 
23199767f8SToomas Soome static void	 clean(void);
24199767f8SToomas Soome static int	 insert(int *argcp, char *buf);
25199767f8SToomas Soome static char	*variable_lookup(char *name);
26199767f8SToomas Soome 
27d66a72cfSToomas Soome #define	PARSE_BUFSIZE	1024	/* maximum size of one element */
28d66a72cfSToomas Soome #define	MAXARGS		20	/* maximum number of elements */
29199767f8SToomas Soome static char		*args[MAXARGS];
30199767f8SToomas Soome 
31199767f8SToomas Soome /*
32199767f8SToomas Soome  * parse: accept a string of input and "parse" it for backslash
33199767f8SToomas Soome  * substitutions and environment variable expansions (${var}),
34199767f8SToomas Soome  * returning an argc/argv style vector of whitespace separated
35199767f8SToomas Soome  * arguments.  Returns 0 on success, 1 on failure (ok, ok, so I
36199767f8SToomas Soome  * wimped-out on the error codes! :).
37199767f8SToomas Soome  *
38199767f8SToomas Soome  * Note that the argv array returned must be freed by the caller, but
39199767f8SToomas Soome  * we own the space allocated for arguments and will free that on next
40199767f8SToomas Soome  * invocation.  This allows argv consumers to modify the array if
41199767f8SToomas Soome  * required.
42199767f8SToomas Soome  *
43199767f8SToomas Soome  * NB: environment variables that expand to more than one whitespace
44199767f8SToomas Soome  * separated token will be returned as a single argv[] element, not
45199767f8SToomas Soome  * split in turn.  Expanded text is also immune to further backslash
46199767f8SToomas Soome  * elimination or expansion since this is a one-pass, non-recursive
47199767f8SToomas Soome  * parser.  You didn't specify more than this so if you want more, ask
48199767f8SToomas Soome  * me. - jkh
49199767f8SToomas Soome  */
50199767f8SToomas Soome 
51d66a72cfSToomas Soome #define	PARSE_FAIL(expr) \
52d66a72cfSToomas Soome 	if (expr) { \
53d66a72cfSToomas Soome 		printf("fail at line %d\n", __LINE__); \
54d66a72cfSToomas Soome 		clean(); \
55d66a72cfSToomas Soome 		free(copy); \
56d66a72cfSToomas Soome 		free(buf); \
57d66a72cfSToomas Soome 		return (1); \
58d66a72cfSToomas Soome 	}
59199767f8SToomas Soome 
60199767f8SToomas Soome /* Accept the usual delimiters for a variable, returning counterpart */
61199767f8SToomas Soome static char
isdelim(int ch)62199767f8SToomas Soome isdelim(int ch)
63199767f8SToomas Soome {
64d66a72cfSToomas Soome 	if (ch == '{')
65d66a72cfSToomas Soome 		return ('}');
66d66a72cfSToomas Soome 	else if (ch == '(')
67d66a72cfSToomas Soome 		return (')');
68d66a72cfSToomas Soome 	return ('\0');
69199767f8SToomas Soome }
70199767f8SToomas Soome 
71199767f8SToomas Soome static int
isquote(int ch)72199767f8SToomas Soome isquote(int ch)
73199767f8SToomas Soome {
74d66a72cfSToomas Soome 	return (ch == '\'');
75199767f8SToomas Soome }
76199767f8SToomas Soome 
77199767f8SToomas Soome static int
isdquote(int ch)78199767f8SToomas Soome isdquote(int ch)
79199767f8SToomas Soome {
80d66a72cfSToomas Soome 	return (ch == '"');
81199767f8SToomas Soome }
82199767f8SToomas Soome 
83199767f8SToomas Soome int
parse(int * argc,char *** argv,char * str)84199767f8SToomas Soome parse(int *argc, char ***argv, char *str)
85199767f8SToomas Soome {
86d66a72cfSToomas Soome 	int ac;
87d66a72cfSToomas Soome 	char *val, *p, *q, *copy = NULL;
88d66a72cfSToomas Soome 	size_t i = 0;
89d66a72cfSToomas Soome 	char token, tmp, quote, dquote, *buf;
90d66a72cfSToomas Soome 	enum { STR, VAR, WHITE } state;
91d66a72cfSToomas Soome 
92d66a72cfSToomas Soome 	ac = *argc = 0;
93d66a72cfSToomas Soome 	dquote = quote = 0;
94d66a72cfSToomas Soome 	if (!str || (p = copy = backslash(str)) == NULL)
95d66a72cfSToomas Soome 		return (1);
96d66a72cfSToomas Soome 
97d66a72cfSToomas Soome 	/* Initialize vector and state */
98d66a72cfSToomas Soome 	clean();
99d66a72cfSToomas Soome 	state = STR;
100d66a72cfSToomas Soome 	buf = malloc(PARSE_BUFSIZE);
101d66a72cfSToomas Soome 	token = 0;
102d66a72cfSToomas Soome 
103d66a72cfSToomas Soome 	/* And awaaaaaaaaay we go! */
104d66a72cfSToomas Soome 	while (*p) {
105d66a72cfSToomas Soome 		switch (state) {
106d66a72cfSToomas Soome 		case STR:
107d66a72cfSToomas Soome 			if ((*p == '\\') && p[1]) {
108d66a72cfSToomas Soome 				p++;
109d66a72cfSToomas Soome 				PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
110d66a72cfSToomas Soome 				buf[i++] = *p++;
111d66a72cfSToomas Soome 			} else if (isquote(*p)) {
112d66a72cfSToomas Soome 				quote = quote ? 0 : *p;
113d66a72cfSToomas Soome 				if (dquote) { /* keep quote */
114d66a72cfSToomas Soome 					PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
115d66a72cfSToomas Soome 					buf[i++] = *p++;
116d66a72cfSToomas Soome 				} else {
117d66a72cfSToomas Soome 					++p;
118d66a72cfSToomas Soome 				}
119d66a72cfSToomas Soome 			} else if (isdquote(*p)) {
120d66a72cfSToomas Soome 				dquote = dquote ? 0 : *p;
121d66a72cfSToomas Soome 				if (quote) { /* keep dquote */
122d66a72cfSToomas Soome 					PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
123d66a72cfSToomas Soome 					buf[i++] = *p++;
124d66a72cfSToomas Soome 				} else {
125d66a72cfSToomas Soome 					++p;
126d66a72cfSToomas Soome 				}
127d66a72cfSToomas Soome 			} else if (isspace(*p) && !quote && !dquote) {
128d66a72cfSToomas Soome 				state = WHITE;
129d66a72cfSToomas Soome 				if (i) {
130d66a72cfSToomas Soome 					buf[i] = '\0';
131d66a72cfSToomas Soome 					PARSE_FAIL(insert(&ac, buf));
132d66a72cfSToomas Soome 					i = 0;
133d66a72cfSToomas Soome 				}
134d66a72cfSToomas Soome 				++p;
135d66a72cfSToomas Soome 			} else if (*p == '$' && !quote) {
136d66a72cfSToomas Soome 				token = isdelim(*(p + 1));
137d66a72cfSToomas Soome 				if (token)
138d66a72cfSToomas Soome 					p += 2;
139d66a72cfSToomas Soome 				else
140d66a72cfSToomas Soome 					++p;
141d66a72cfSToomas Soome 				state = VAR;
142d66a72cfSToomas Soome 			} else {
143d66a72cfSToomas Soome 				PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
144d66a72cfSToomas Soome 				buf[i++] = *p++;
145d66a72cfSToomas Soome 			}
146d66a72cfSToomas Soome 			break;
147d66a72cfSToomas Soome 
148d66a72cfSToomas Soome 		case WHITE:
149d66a72cfSToomas Soome 			if (isspace(*p))
150d66a72cfSToomas Soome 				++p;
151d66a72cfSToomas Soome 			else
152d66a72cfSToomas Soome 				state = STR;
153d66a72cfSToomas Soome 			break;
154d66a72cfSToomas Soome 
155d66a72cfSToomas Soome 		case VAR:
156d66a72cfSToomas Soome 			if (token) {
157d66a72cfSToomas Soome 				PARSE_FAIL((q = strchr(p, token)) == NULL);
158d66a72cfSToomas Soome 			} else {
159d66a72cfSToomas Soome 				q = p;
160d66a72cfSToomas Soome 				while (*q && !isspace(*q))
161d66a72cfSToomas Soome 					++q;
162d66a72cfSToomas Soome 			}
163d66a72cfSToomas Soome 			tmp = *q;
164d66a72cfSToomas Soome 			*q = '\0';
165d66a72cfSToomas Soome 			if ((val = variable_lookup(p)) != NULL) {
166d66a72cfSToomas Soome 				size_t len = strlen(val);
167d66a72cfSToomas Soome 
168d66a72cfSToomas Soome 				strncpy(buf + i, val, PARSE_BUFSIZE - (i + 1));
169d66a72cfSToomas Soome 				i += min(len, PARSE_BUFSIZE - 1);
170d66a72cfSToomas Soome 			}
171d66a72cfSToomas Soome 			*q = tmp;	/* restore value */
172d66a72cfSToomas Soome 			p = q + (token ? 1 : 0);
173d66a72cfSToomas Soome 			state = STR;
174d66a72cfSToomas Soome 			break;
175199767f8SToomas Soome 		}
176199767f8SToomas Soome 	}
177d66a72cfSToomas Soome 	/* missing terminating ' or " */
178d66a72cfSToomas Soome 	PARSE_FAIL(quote || dquote);
179d66a72cfSToomas Soome 	/* If at end of token, add it */
180d66a72cfSToomas Soome 	if (i && state == STR) {
181d66a72cfSToomas Soome 		buf[i] = '\0';
182d66a72cfSToomas Soome 		PARSE_FAIL(insert(&ac, buf));
183d66a72cfSToomas Soome 	}
184d66a72cfSToomas Soome 	args[ac] = NULL;
185d66a72cfSToomas Soome 	*argc = ac;
186d66a72cfSToomas Soome 	*argv = malloc((sizeof (char *) * ac + 1));
187d66a72cfSToomas Soome 	bcopy(args, *argv, sizeof (char *) * ac + 1);
188d66a72cfSToomas Soome 	free(buf);
189d66a72cfSToomas Soome 	free(copy);
190d66a72cfSToomas Soome 	return (0);
191199767f8SToomas Soome }
192199767f8SToomas Soome 
193d66a72cfSToomas Soome #define	MAXARGS	20
194199767f8SToomas Soome 
195199767f8SToomas Soome /* Clean vector space */
196199767f8SToomas Soome static void
clean(void)197199767f8SToomas Soome clean(void)
198199767f8SToomas Soome {
199d66a72cfSToomas Soome 	int i;
200199767f8SToomas Soome 
201d66a72cfSToomas Soome 	for (i = 0; i < MAXARGS; i++) {
202d66a72cfSToomas Soome 		free(args[i]);
203d66a72cfSToomas Soome 		args[i] = NULL;
204199767f8SToomas Soome 	}
205199767f8SToomas Soome }
206199767f8SToomas Soome 
207199767f8SToomas Soome static int
insert(int * argcp,char * buf)208199767f8SToomas Soome insert(int *argcp, char *buf)
209199767f8SToomas Soome {
210d66a72cfSToomas Soome 	if (*argcp >= MAXARGS)
211d66a72cfSToomas Soome 		return (1);
212d66a72cfSToomas Soome 	args[(*argcp)++] = strdup(buf);
213d66a72cfSToomas Soome 	return (0);
214199767f8SToomas Soome }
215199767f8SToomas Soome 
216199767f8SToomas Soome static char *
variable_lookup(char * name)217199767f8SToomas Soome variable_lookup(char *name)
218199767f8SToomas Soome {
219d66a72cfSToomas Soome 
220d66a72cfSToomas Soome 	/* XXX search "special variable" space first? */
221d66a72cfSToomas Soome 	return (getenv(name));
222199767f8SToomas Soome }
223