interp_parse.c revision 38716
138716Smsmith/*
238716Smsmith * Redistribution and use in source and binary forms, with or without
338716Smsmith * modification, are permitted provided that the following conditions
438716Smsmith * are met:
538716Smsmith * 1. Redistributions of source code must retain the above copyright
638716Smsmith *    notice, this list of conditions and the following disclaimer.
738716Smsmith * 2. Redistributions in binary form must reproduce the above copyright
838716Smsmith *    notice, this list of conditions and the following disclaimer in the
938716Smsmith *    documentation and/or other materials provided with the distribution.
1038716Smsmith *
1138716Smsmith * Jordan K. Hubbard
1238716Smsmith * 29 August 1998
1338716Smsmith *
1438716Smsmith *	$Id$
1538716Smsmith *
1638716Smsmith * The meat of the simple parser.
1738716Smsmith */
1838716Smsmith
1938716Smsmith#include <stand.h>
2038716Smsmith#include <string.h>
2138716Smsmith
2238716Smsmith/* Forward decls */
2338716Smsmithextern char *backslash(char *str);
2438716Smsmith
2538716Smsmithstatic void init(int *argcp, char ***argvp);
2638716Smsmithstatic void clean(void);
2738716Smsmithstatic int insert(int *argcp, char *buf);
2838716Smsmithstatic char *variable_lookup(char *name);
2938716Smsmith
3038716Smsmith#define PARSE_BUFSIZE	1024	/* maximum size of one element */
3138716Smsmith#define MAXARGS		20	/* maximum number of elements */
3238716Smsmithstatic char		*args[MAXARGS];
3338716Smsmith
3438716Smsmith/*
3538716Smsmith * parse: accept a string of input and "parse" it for backslash
3638716Smsmith * substitutions and environment variable expansions (${var}),
3738716Smsmith * returning an argc/argv style vector of whitespace separated
3838716Smsmith * arguments.  Returns 0 on success, 1 on failure (ok, ok, so I
3938716Smsmith * wimped-out on the error codes! :).
4038716Smsmith *
4138716Smsmith * Note that the argv array returned must be freed by the caller, but
4238716Smsmith * we own the space allocated for arguments and will free that on next
4338716Smsmith * invocation.  This allows argv consumers to modify the array if
4438716Smsmith * required.
4538716Smsmith *
4638716Smsmith * NB: environment variables that expand to more than one whitespace
4738716Smsmith * separated token will be returned as a single argv[] element, not
4838716Smsmith * split in turn.  Expanded text is also immune to further backslash
4938716Smsmith * elimination or expansion since this is a one-pass, non-recursive
5038716Smsmith * parser.  You didn't specify more than this so if you want more, ask
5138716Smsmith * me. - jkh
5238716Smsmith */
5338716Smsmith
5438716Smsmith#define PARSE_FAIL(expr) \
5538716Smsmithif (expr) { \
5638716Smsmith    printf("fail at line %d\n", __LINE__); \
5738716Smsmith    clean(); \
5838716Smsmith    free(copy); \
5938716Smsmith    free(buf); \
6038716Smsmith    return 1; \
6138716Smsmith}
6238716Smsmith
6338716Smsmith/* Accept the usual delimiters for a variable, returning counterpart */
6438716Smsmithstatic char
6538716Smsmithisdelim(char ch)
6638716Smsmith{
6738716Smsmith    if (ch == '{')
6838716Smsmith	return '}';
6938716Smsmith    else if (ch == '(')
7038716Smsmith	return ')';
7138716Smsmith    return '\0';
7238716Smsmith}
7338716Smsmith
7438716Smsmithint
7538716Smsmithparse(int *argc, char ***argv, char *str)
7638716Smsmith{
7738716Smsmith    int ac;
7838716Smsmith    char *val, *p, *q, *copy = NULL;
7938716Smsmith    int i = 0;
8038716Smsmith    char token, tmp, *buf;
8138716Smsmith    enum { STR, VAR, WHITE } state;
8238716Smsmith
8338716Smsmith    ac = *argc = 0;
8438716Smsmith    if (!str || (p = copy = backslash(str)) == NULL)
8538716Smsmith	return 1;
8638716Smsmith
8738716Smsmith    /* Initialize vector and state */
8838716Smsmith    clean();
8938716Smsmith    state = STR;
9038716Smsmith    buf = malloc(PARSE_BUFSIZE);
9138716Smsmith    token = 0;
9238716Smsmith
9338716Smsmith    /* And awaaaaaaaaay we go! */
9438716Smsmith    while (*p) {
9538716Smsmith	switch (state) {
9638716Smsmith	case STR:
9738716Smsmith	    if (isspace(*p)) {
9838716Smsmith		state = WHITE;
9938716Smsmith		if (i) {
10038716Smsmith		    buf[i] = '\0';
10138716Smsmith		    PARSE_FAIL(insert(&ac, buf));
10238716Smsmith		    i = 0;
10338716Smsmith		}
10438716Smsmith		++p;
10538716Smsmith	    } else if (*p == '$') {
10638716Smsmith		token = isdelim(*(p + 1));
10738716Smsmith		if (token)
10838716Smsmith		    p += 2;
10938716Smsmith		else
11038716Smsmith		    ++p;
11138716Smsmith		state = VAR;
11238716Smsmith	    } else {
11338716Smsmith		PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
11438716Smsmith		buf[i++] = *p++;
11538716Smsmith	    }
11638716Smsmith	    break;
11738716Smsmith
11838716Smsmith	case WHITE:
11938716Smsmith	    if (isspace(*p))
12038716Smsmith		++p;
12138716Smsmith	    else
12238716Smsmith		state = STR;
12338716Smsmith	    break;
12438716Smsmith
12538716Smsmith	case VAR:
12638716Smsmith	    if (token) {
12738716Smsmith		PARSE_FAIL((q = index(p, token)) == NULL);
12838716Smsmith	    } else {
12938716Smsmith		q = p;
13038716Smsmith		while (*q && !isspace(*q))
13138716Smsmith		    ++q;
13238716Smsmith	    }
13338716Smsmith	    tmp = *q;
13438716Smsmith	    *q = '\0';
13538716Smsmith	    if ((val = variable_lookup(p)) != NULL) {
13638716Smsmith		int len = strlen(val);
13738716Smsmith
13838716Smsmith		strncpy(buf + i, val, PARSE_BUFSIZE - (i + 1));
13938716Smsmith		i += min(len, sizeof(buf) - 1);
14038716Smsmith	    }
14138716Smsmith	    *q = tmp;	/* restore value */
14238716Smsmith	    p = q + (token ? 1 : 0);
14338716Smsmith	    state = STR;
14438716Smsmith	    break;
14538716Smsmith	}
14638716Smsmith    }
14738716Smsmith    /* If at end of token, add it */
14838716Smsmith    if (i && state == STR) {
14938716Smsmith	buf[i] = '\0';
15038716Smsmith	PARSE_FAIL(insert(&ac, buf));
15138716Smsmith    }
15238716Smsmith    args[ac] = NULL;
15338716Smsmith    *argc = ac;
15438716Smsmith    *argv = malloc((sizeof(char *) * ac + 1));
15538716Smsmith    bcopy(args, *argv, sizeof(char *) * ac + 1);
15638716Smsmith    free(copy);
15738716Smsmith    return 0;
15838716Smsmith}
15938716Smsmith
16038716Smsmith#define MAXARGS	20
16138716Smsmith
16238716Smsmith/* Clean vector space */
16338716Smsmithstatic void
16438716Smsmithclean(void)
16538716Smsmith{
16638716Smsmith    int		i;
16738716Smsmith
16838716Smsmith    for (i = 0; i < MAXARGS; i++) {
16938716Smsmith	if (args[i] != NULL) {
17038716Smsmith	    free(args[i]);
17138716Smsmith	    args[i] = NULL;
17238716Smsmith	}
17338716Smsmith    }
17438716Smsmith}
17538716Smsmith
17638716Smsmithstatic int
17738716Smsmithinsert(int *argcp, char *buf)
17838716Smsmith{
17938716Smsmith    if (*argcp >= MAXARGS)
18038716Smsmith	return 1;
18138716Smsmith    args[(*argcp)++] = strdup(buf);
18238716Smsmith    return 0;
18338716Smsmith}
18438716Smsmith
18538716Smsmithstatic char *
18638716Smsmithvariable_lookup(char *name)
18738716Smsmith{
18838716Smsmith    /* XXX search "special variable" space first? */
18938716Smsmith    return (char *)getenv(name);
19038716Smsmith}
191