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