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