interp_parse.c revision 38789
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 *
1438789Smsmith *	$Id: interp_parse.c,v 1.2 1998/09/03 06:14:41 jkh Exp $
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
7438765Sjkhstatic int
7538765Sjkhisquote(char ch)
7638765Sjkh{
7738765Sjkh    return (ch == '\'' || ch == '"');
7838765Sjkh}
7938765Sjkh
8038716Smsmithint
8138716Smsmithparse(int *argc, char ***argv, char *str)
8238716Smsmith{
8338716Smsmith    int ac;
8438716Smsmith    char *val, *p, *q, *copy = NULL;
8538716Smsmith    int i = 0;
8638765Sjkh    char token, tmp, quote, *buf;
8738716Smsmith    enum { STR, VAR, WHITE } state;
8838716Smsmith
8938716Smsmith    ac = *argc = 0;
9038765Sjkh    quote = 0;
9138716Smsmith    if (!str || (p = copy = backslash(str)) == NULL)
9238716Smsmith	return 1;
9338716Smsmith
9438716Smsmith    /* Initialize vector and state */
9538716Smsmith    clean();
9638716Smsmith    state = STR;
9738765Sjkh    buf = (char *)malloc(PARSE_BUFSIZE);
9838716Smsmith    token = 0;
9938716Smsmith
10038716Smsmith    /* And awaaaaaaaaay we go! */
10138716Smsmith    while (*p) {
10238716Smsmith	switch (state) {
10338716Smsmith	case STR:
10438765Sjkh	    if (isquote(*p)) {
10538765Sjkh		quote = quote ? 0 : *p;
10638765Sjkh		++p;
10738765Sjkh	    }
10838765Sjkh	    else if (isspace(*p) && !quote) {
10938716Smsmith		state = WHITE;
11038716Smsmith		if (i) {
11138716Smsmith		    buf[i] = '\0';
11238716Smsmith		    PARSE_FAIL(insert(&ac, buf));
11338716Smsmith		    i = 0;
11438716Smsmith		}
11538716Smsmith		++p;
11638716Smsmith	    } else if (*p == '$') {
11738716Smsmith		token = isdelim(*(p + 1));
11838716Smsmith		if (token)
11938716Smsmith		    p += 2;
12038716Smsmith		else
12138716Smsmith		    ++p;
12238716Smsmith		state = VAR;
12338716Smsmith	    } else {
12438716Smsmith		PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
12538716Smsmith		buf[i++] = *p++;
12638716Smsmith	    }
12738716Smsmith	    break;
12838716Smsmith
12938716Smsmith	case WHITE:
13038716Smsmith	    if (isspace(*p))
13138716Smsmith		++p;
13238716Smsmith	    else
13338716Smsmith		state = STR;
13438716Smsmith	    break;
13538716Smsmith
13638716Smsmith	case VAR:
13738716Smsmith	    if (token) {
13838716Smsmith		PARSE_FAIL((q = index(p, token)) == NULL);
13938716Smsmith	    } else {
14038716Smsmith		q = p;
14138716Smsmith		while (*q && !isspace(*q))
14238716Smsmith		    ++q;
14338716Smsmith	    }
14438716Smsmith	    tmp = *q;
14538716Smsmith	    *q = '\0';
14638716Smsmith	    if ((val = variable_lookup(p)) != NULL) {
14738716Smsmith		int len = strlen(val);
14838716Smsmith
14938716Smsmith		strncpy(buf + i, val, PARSE_BUFSIZE - (i + 1));
15038789Smsmith		i += min(len, PARSE_BUFSIZE - 1);
15138716Smsmith	    }
15238716Smsmith	    *q = tmp;	/* restore value */
15338716Smsmith	    p = q + (token ? 1 : 0);
15438716Smsmith	    state = STR;
15538716Smsmith	    break;
15638716Smsmith	}
15738716Smsmith    }
15838716Smsmith    /* If at end of token, add it */
15938716Smsmith    if (i && state == STR) {
16038716Smsmith	buf[i] = '\0';
16138716Smsmith	PARSE_FAIL(insert(&ac, buf));
16238716Smsmith    }
16338716Smsmith    args[ac] = NULL;
16438716Smsmith    *argc = ac;
16538765Sjkh    *argv = (char **)malloc((sizeof(char *) * ac + 1));
16638716Smsmith    bcopy(args, *argv, sizeof(char *) * ac + 1);
16738716Smsmith    free(copy);
16838716Smsmith    return 0;
16938716Smsmith}
17038716Smsmith
17138716Smsmith#define MAXARGS	20
17238716Smsmith
17338716Smsmith/* Clean vector space */
17438716Smsmithstatic void
17538716Smsmithclean(void)
17638716Smsmith{
17738716Smsmith    int		i;
17838716Smsmith
17938716Smsmith    for (i = 0; i < MAXARGS; i++) {
18038716Smsmith	if (args[i] != NULL) {
18138716Smsmith	    free(args[i]);
18238716Smsmith	    args[i] = NULL;
18338716Smsmith	}
18438716Smsmith    }
18538716Smsmith}
18638716Smsmith
18738716Smsmithstatic int
18838716Smsmithinsert(int *argcp, char *buf)
18938716Smsmith{
19038716Smsmith    if (*argcp >= MAXARGS)
19138716Smsmith	return 1;
19238716Smsmith    args[(*argcp)++] = strdup(buf);
19338716Smsmith    return 0;
19438716Smsmith}
19538716Smsmith
19638716Smsmithstatic char *
19738716Smsmithvariable_lookup(char *name)
19838716Smsmith{
19938716Smsmith    /* XXX search "special variable" space first? */
20038716Smsmith    return (char *)getenv(name);
20138716Smsmith}
202