xref: /netbsd-src/sys/arch/ia64/stand/common/interp_parse.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: interp_parse.c,v 1.2 2006/04/22 07:58:53 cherry Exp $	*/
2 
3 /*-
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * Jordan K. Hubbard
14  * 29 August 1998
15  *
16  * The meat of the simple parser.
17  */
18 
19 #include <sys/cdefs.h>
20 /* __FBSDID("$FreeBSD: src/sys/boot/common/interp_parse.c,v 1.10 2003/08/25 23:30:41 obrien Exp $"); */
21 
22 #include <lib/libsa/stand.h>
23 #include <lib/libkern/libkern.h>
24 
25 #include "bootstrap.h"
26 
27 static void	 clean(void);
28 static int	 insert(int *argcp, char *buf);
29 static char	*variable_lookup(char *name);
30 
31 #define PARSE_BUFSIZE	1024	/* maximum size of one element */
32 #define MAXARGS		20	/* maximum number of elements */
33 static char		*args[MAXARGS];
34 
35 /*
36  * parse: accept a string of input and "parse" it for backslash
37  * substitutions and environment variable expansions (${var}),
38  * returning an argc/argv style vector of whitespace separated
39  * arguments.  Returns 0 on success, 1 on failure (ok, ok, so I
40  * wimped-out on the error codes! :).
41  *
42  * Note that the argv array returned must be freed by the caller, but
43  * we own the space allocated for arguments and will free that on next
44  * invocation.  This allows argv consumers to modify the array if
45  * required.
46  *
47  * NB: environment variables that expand to more than one whitespace
48  * separated token will be returned as a single argv[] element, not
49  * split in turn.  Expanded text is also immune to further backslash
50  * elimination or expansion since this is a one-pass, non-recursive
51  * parser.  You didn't specify more than this so if you want more, ask
52  * me. - jkh
53  */
54 
55 #define PARSE_FAIL(expr) \
56 if (expr) { \
57     printf("fail at line %d\n", __LINE__); \
58     clean(); \
59     free(copy); \
60     free(buf); \
61     return 1; \
62 }
63 
64 /* Accept the usual delimiters for a variable, returning counterpart */
65 static char
66 isdelim(int ch)
67 {
68     if (ch == '{')
69 	return '}';
70     else if (ch == '(')
71 	return ')';
72     return '\0';
73 }
74 
75 static int
76 isquote(int ch)
77 {
78     return (ch == '\'' || ch == '"');
79 }
80 
81 int
82 parse(int *argc, char ***argv, char *str)
83 {
84     int ac;
85     char *val, *p, *q, *copy = NULL;
86     size_t i = 0;
87     char token, tmp, quote, *buf;
88     enum { STR, VAR, WHITE } state;
89 
90     ac = *argc = 0;
91     quote = 0;
92     if (!str || (p = copy = backslash(str)) == NULL)
93 	return 1;
94 
95     /* Initialize vector and state */
96     clean();
97     state = STR;
98     buf = (char *)alloc(PARSE_BUFSIZE);
99     token = 0;
100 
101     /* And awaaaaaaaaay we go! */
102     while (*p) {
103 	switch (state) {
104 	case STR:
105 	    if ((*p == '\\') && p[1]) {
106 		p++;
107 		PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
108 		buf[i++] = *p++;
109 	    } else if (isquote(*p)) {
110 		quote = quote ? 0 : *p;
111 		++p;
112 	    }
113 	    else if (isspace(*p) && !quote) {
114 		state = WHITE;
115 		if (i) {
116 		    buf[i] = '\0';
117 		    PARSE_FAIL(insert(&ac, buf));
118 		    i = 0;
119 		}
120 		++p;
121 	    } else if (*p == '$') {
122 		token = isdelim(*(p + 1));
123 		if (token)
124 		    p += 2;
125 		else
126 		    ++p;
127 		state = VAR;
128 	    } else {
129 		PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
130 		buf[i++] = *p++;
131 	    }
132 	    break;
133 
134 	case WHITE:
135 	    if (isspace(*p))
136 		++p;
137 	    else
138 		state = STR;
139 	    break;
140 
141 	case VAR:
142 	    if (token) {
143 		PARSE_FAIL((q = strchr(p, token)) == NULL);
144 	    } else {
145 		q = p;
146 		while (*q && !isspace(*q))
147 		    ++q;
148 	    }
149 	    tmp = *q;
150 	    *q = '\0';
151 	    if ((val = variable_lookup(p)) != NULL) {
152 		size_t len = strlen(val);
153 
154 		strncpy(buf + i, val, PARSE_BUFSIZE - (i + 1));
155 		i += min(len, PARSE_BUFSIZE - 1);
156 	    }
157 	    *q = tmp;	/* restore value */
158 	    p = q + (token ? 1 : 0);
159 	    state = STR;
160 	    break;
161 	}
162     }
163     /* If at end of token, add it */
164     if (i && state == STR) {
165 	buf[i] = '\0';
166 	PARSE_FAIL(insert(&ac, buf));
167     }
168     args[ac] = NULL;
169     *argc = ac;
170     *argv = (char **)alloc((sizeof(char *) * ac + 1));
171     bcopy(args, *argv, sizeof(char *) * ac + 1);
172     free(buf);
173     free(copy);
174     return 0;
175 }
176 
177 #define MAXARGS	20
178 
179 /* Clean vector space */
180 static void
181 clean(void)
182 {
183     int		i;
184 
185     for (i = 0; i < MAXARGS; i++) {
186 	if (args[i] != NULL) {
187 	    free(args[i]);
188 	    args[i] = NULL;
189 	}
190     }
191 }
192 
193 static int
194 insert(int *argcp, char *buf)
195 {
196     if (*argcp >= MAXARGS)
197 	return 1;
198     args[(*argcp)++] = strdup(buf);
199     return 0;
200 }
201 
202 static char *
203 variable_lookup(char *name)
204 {
205     /* XXX search "special variable" space first? */
206     return (char *)getenv(name);
207 }
208