xref: /dragonfly/stand/boot/common/interp_parse.c (revision 7d3e9a5b)
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  * $FreeBSD: src/sys/boot/common/interp_parse.c,v 1.10 2003/08/25 23:30:41 obrien Exp $
17  * $DragonFly: src/sys/boot/common/interp_parse.c,v 1.3 2003/11/10 06:08:31 dillon Exp $
18  */
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     if (ch == '{')
66 	return '}';
67     else if (ch == '(')
68 	return ')';
69     return '\0';
70 }
71 
72 static int
73 isquote(int ch)
74 {
75     return (ch == '\'' || ch == '"');
76 }
77 
78 int
79 parse(int *argc, char ***argv, char *str)
80 {
81     int ac;
82     char *val, *p, *q, *copy = NULL;
83     size_t i = 0;
84     char token, tmp, quote, *buf;
85     enum { STR, VAR, WHITE } state;
86 
87     ac = *argc = 0;
88     quote = 0;
89     if (!str || (p = copy = backslash(str)) == NULL)
90 	return 1;
91 
92     /* Initialize vector and state */
93     clean();
94     state = STR;
95     buf = (char *)malloc(PARSE_BUFSIZE);
96     token = 0;
97 
98     /* And awaaaaaaaaay we go! */
99     while (*p) {
100 	switch (state) {
101 	case STR:
102 	    /*
103 	     * Check comment
104 	     */
105 	    if (*p == '#' && quote == 0) {
106 		*p = 0;
107 		break;
108 	    }
109 
110 	    /*
111 	     * Check line continuation
112 	     */
113 	    if (*p == '\\' && p[1]) {
114 		p++;
115 		PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
116 		buf[i++] = *p++;
117 	    } else if (isquote(*p)) {
118 		quote = quote ? 0 : *p;
119 		++p;
120 	    }
121 	    else if (isspace(*p) && !quote) {
122 		state = WHITE;
123 		if (i) {
124 		    buf[i] = '\0';
125 		    PARSE_FAIL(insert(&ac, buf));
126 		    i = 0;
127 		}
128 		++p;
129 	    } else if (*p == '$') {
130 		token = isdelim(*(p + 1));
131 		if (token)
132 		    p += 2;
133 		else
134 		    ++p;
135 		state = VAR;
136 	    } else {
137 		PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
138 		buf[i++] = *p++;
139 	    }
140 	    break;
141 
142 	case WHITE:
143 	    if (isspace(*p))
144 		++p;
145 	    else
146 		state = STR;
147 	    break;
148 
149 	case VAR:
150 	    if (token) {
151 		PARSE_FAIL((q = index(p, token)) == NULL);
152 	    } else {
153 		q = p;
154 		while (*q && !isspace(*q))
155 		    ++q;
156 	    }
157 	    tmp = *q;
158 	    *q = '\0';
159 	    if ((val = variable_lookup(p)) != NULL) {
160 		size_t len = strlen(val);
161 
162 		strncpy(buf + i, val, PARSE_BUFSIZE - (i + 1));
163 		i += min(len, PARSE_BUFSIZE - 1);
164 	    }
165 	    *q = tmp;	/* restore value */
166 	    p = q + (token ? 1 : 0);
167 	    state = STR;
168 	    break;
169 	}
170     }
171     /* If at end of token, add it */
172     if (i && state == STR) {
173 	buf[i] = '\0';
174 	PARSE_FAIL(insert(&ac, buf));
175     }
176     args[ac] = NULL;
177     *argc = ac;
178     *argv = (char **)malloc(sizeof(char *) * (ac + 1));
179     bcopy(args, *argv, sizeof(char *) * (ac + 1));
180     free(buf);
181     free(copy);
182     return 0;
183 }
184 
185 #define MAXARGS	20
186 
187 /* Clean vector space */
188 static void
189 clean(void)
190 {
191     int		i;
192 
193     for (i = 0; i < MAXARGS; i++) {
194 	if (args[i] != NULL) {
195 	    free(args[i]);
196 	    args[i] = NULL;
197 	}
198     }
199 }
200 
201 static int
202 insert(int *argcp, char *buf)
203 {
204     if (*argcp >= MAXARGS)
205 	return 1;
206     args[(*argcp)++] = strdup(buf);
207     return 0;
208 }
209 
210 static char *
211 variable_lookup(char *name)
212 {
213     /* XXX search "special variable" space first? */
214     return (char *)getenv(name);
215 }
216