interp_parse.c revision 39441
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.3 1998/09/04 02:43:26 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 (isquote(*p)) {
104		quote = quote ? 0 : *p;
105		++p;
106	    }
107	    else if (isspace(*p) && !quote) {
108		state = WHITE;
109		if (i) {
110		    buf[i] = '\0';
111		    PARSE_FAIL(insert(&ac, buf));
112		    i = 0;
113		}
114		++p;
115	    } else if (*p == '$') {
116		token = isdelim(*(p + 1));
117		if (token)
118		    p += 2;
119		else
120		    ++p;
121		state = VAR;
122	    } else {
123		PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
124		buf[i++] = *p++;
125	    }
126	    break;
127
128	case WHITE:
129	    if (isspace(*p))
130		++p;
131	    else
132		state = STR;
133	    break;
134
135	case VAR:
136	    if (token) {
137		PARSE_FAIL((q = index(p, token)) == NULL);
138	    } else {
139		q = p;
140		while (*q && !isspace(*q))
141		    ++q;
142	    }
143	    tmp = *q;
144	    *q = '\0';
145	    if ((val = variable_lookup(p)) != NULL) {
146		int len = strlen(val);
147
148		strncpy(buf + i, val, PARSE_BUFSIZE - (i + 1));
149		i += min(len, PARSE_BUFSIZE - 1);
150	    }
151	    *q = tmp;	/* restore value */
152	    p = q + (token ? 1 : 0);
153	    state = STR;
154	    break;
155	}
156    }
157    /* If at end of token, add it */
158    if (i && state == STR) {
159	buf[i] = '\0';
160	PARSE_FAIL(insert(&ac, buf));
161    }
162    args[ac] = NULL;
163    *argc = ac;
164    *argv = (char **)malloc((sizeof(char *) * ac + 1));
165    bcopy(args, *argv, sizeof(char *) * ac + 1);
166    free(copy);
167    return 0;
168}
169
170#define MAXARGS	20
171
172/* Clean vector space */
173static void
174clean(void)
175{
176    int		i;
177
178    for (i = 0; i < MAXARGS; i++) {
179	if (args[i] != NULL) {
180	    free(args[i]);
181	    args[i] = NULL;
182	}
183    }
184}
185
186static int
187insert(int *argcp, char *buf)
188{
189    if (*argcp >= MAXARGS)
190	return 1;
191    args[(*argcp)++] = strdup(buf);
192    return 0;
193}
194
195static char *
196variable_lookup(char *name)
197{
198    /* XXX search "special variable" space first? */
199    return (char *)getenv(name);
200}
201