Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / boot / common / interp_parse.c
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  * $FreeBSD: src/sys/boot/common/interp_parse.c,v 1.8.2.1 2000/12/28 13:12:35 ps Exp $
15  * 
16  * The meat of the simple parser.
17  */
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
62 isdelim(int ch)
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     return (ch == '\'' || ch == '"');
75 }
76
77 int
78 parse(int *argc, char ***argv, char *str)
79 {
80     int ac;
81     char *val, *p, *q, *copy = NULL;
82     size_t i = 0;
83     char token, tmp, quote, *buf;
84     enum { STR, VAR, WHITE } state;
85
86     ac = *argc = 0;
87     quote = 0;
88     if (!str || (p = copy = backslash(str)) == NULL)
89         return 1;
90
91     /* Initialize vector and state */
92     clean();
93     state = STR;
94     buf = (char *)malloc(PARSE_BUFSIZE);
95     token = 0;
96
97     /* And awaaaaaaaaay we go! */
98     while (*p) {
99         switch (state) {
100         case STR:
101             if ((*p == '\\') && p[1]) {
102                 p++;
103                 PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
104                 buf[i++] = *p++;
105             } else if (isquote(*p)) {
106                 quote = quote ? 0 : *p;
107                 ++p;
108             }
109             else if (isspace(*p) && !quote) {
110                 state = WHITE;
111                 if (i) {
112                     buf[i] = '\0';
113                     PARSE_FAIL(insert(&ac, buf));
114                     i = 0;
115                 }
116                 ++p;
117             } else if (*p == '$') {
118                 token = isdelim(*(p + 1));
119                 if (token)
120                     p += 2;
121                 else
122                     ++p;
123                 state = VAR;
124             } else {
125                 PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
126                 buf[i++] = *p++;
127             }
128             break;
129
130         case WHITE:
131             if (isspace(*p))
132                 ++p;
133             else
134                 state = STR;
135             break;
136
137         case VAR:
138             if (token) {
139                 PARSE_FAIL((q = index(p, token)) == NULL);
140             } else {
141                 q = p;
142                 while (*q && !isspace(*q))
143                     ++q;
144             }
145             tmp = *q;
146             *q = '\0';
147             if ((val = variable_lookup(p)) != NULL) {
148                 size_t len = strlen(val);
149
150                 strncpy(buf + i, val, PARSE_BUFSIZE - (i + 1));
151                 i += min(len, PARSE_BUFSIZE - 1);
152             }
153             *q = tmp;   /* restore value */
154             p = q + (token ? 1 : 0);
155             state = STR;
156             break;
157         }
158     }
159     /* If at end of token, add it */
160     if (i && state == STR) {
161         buf[i] = '\0';
162         PARSE_FAIL(insert(&ac, buf));
163     }
164     args[ac] = NULL;
165     *argc = ac;
166     *argv = (char **)malloc((sizeof(char *) * ac + 1));
167     bcopy(args, *argv, sizeof(char *) * ac + 1);
168     free(buf);
169     free(copy);
170     return 0;
171 }
172
173 #define MAXARGS 20
174
175 /* Clean vector space */
176 static void
177 clean(void)
178 {
179     int         i;
180
181     for (i = 0; i < MAXARGS; i++) {
182         if (args[i] != NULL) {
183             free(args[i]);
184             args[i] = NULL;
185         }
186     }
187 }
188
189 static int
190 insert(int *argcp, char *buf)
191 {
192     if (*argcp >= MAXARGS)
193         return 1;
194     args[(*argcp)++] = strdup(buf);
195     return 0;
196 }
197
198 static char *
199 variable_lookup(char *name)
200 {
201     /* XXX search "special variable" space first? */
202     return (char *)getenv(name);
203 }