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__FBSDID("$FreeBSD$");
19
20#include <stand.h>
21#include <string.h>
22#include "bootstrap.h"
23
24static void	 clean(void);
25static int	 insert(int *argcp, char *buf);
26static char	*variable_lookup(char *name);
27
28#define PARSE_BUFSIZE	1024	/* maximum size of one element */
29#define MAXARGS		20	/* maximum number of elements */
30static 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) \
53if (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 */
62static char
63isdelim(int ch)
64{
65    if (ch == '{')
66	return '}';
67    else if (ch == '(')
68	return ')';
69    return '\0';
70}
71
72static int
73isquote(int ch)
74{
75    return (ch == '\'' || ch == '"');
76}
77
78int
79parse(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	    if ((*p == '\\') && p[1]) {
103		p++;
104		PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
105		buf[i++] = *p++;
106	    } else if (isquote(*p)) {
107		quote = quote ? 0 : *p;
108		++p;
109	    }
110	    else if (isspace(*p) && !quote) {
111		state = WHITE;
112		if (i) {
113		    buf[i] = '\0';
114		    PARSE_FAIL(insert(&ac, buf));
115		    i = 0;
116		}
117		++p;
118	    } else if (*p == '$') {
119		token = isdelim(*(p + 1));
120		if (token)
121		    p += 2;
122		else
123		    ++p;
124		state = VAR;
125	    } else {
126		PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
127		buf[i++] = *p++;
128	    }
129	    break;
130
131	case WHITE:
132	    if (isspace(*p))
133		++p;
134	    else
135		state = STR;
136	    break;
137
138	case VAR:
139	    if (token) {
140		PARSE_FAIL((q = index(p, token)) == NULL);
141	    } else {
142		q = p;
143		while (*q && !isspace(*q))
144		    ++q;
145	    }
146	    tmp = *q;
147	    *q = '\0';
148	    if ((val = variable_lookup(p)) != NULL) {
149		size_t len = strlen(val);
150
151		strncpy(buf + i, val, PARSE_BUFSIZE - (i + 1));
152		i += min(len, PARSE_BUFSIZE - 1);
153	    }
154	    *q = tmp;	/* restore value */
155	    p = q + (token ? 1 : 0);
156	    state = STR;
157	    break;
158	}
159    }
160    /* If at end of token, add it */
161    if (i && state == STR) {
162	buf[i] = '\0';
163	PARSE_FAIL(insert(&ac, buf));
164    }
165    args[ac] = NULL;
166    *argc = ac;
167    *argv = (char **)malloc((sizeof(char *) * ac + 1));
168    bcopy(args, *argv, sizeof(char *) * ac + 1);
169    free(buf);
170    free(copy);
171    return 0;
172}
173
174#define MAXARGS	20
175
176/* Clean vector space */
177static void
178clean(void)
179{
180    int		i;
181
182    for (i = 0; i < MAXARGS; i++) {
183	if (args[i] != NULL) {
184	    free(args[i]);
185	    args[i] = NULL;
186	}
187    }
188}
189
190static int
191insert(int *argcp, char *buf)
192{
193    if (*argcp >= MAXARGS)
194	return 1;
195    args[(*argcp)++] = strdup(buf);
196    return 0;
197}
198
199static char *
200variable_lookup(char *name)
201{
202    /* XXX search "special variable" space first? */
203    return (char *)getenv(name);
204}
205