interp_parse.c revision 42620
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 *
1442620Smsmith *	$Id: interp_parse.c,v 1.5 1999/01/10 05:08:12 msmith 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 clean(void);
2638716Smsmithstatic int insert(int *argcp, char *buf);
2738716Smsmithstatic char *variable_lookup(char *name);
2838716Smsmith
2938716Smsmith#define PARSE_BUFSIZE	1024	/* maximum size of one element */
3038716Smsmith#define MAXARGS		20	/* maximum number of elements */
3138716Smsmithstatic char		*args[MAXARGS];
3238716Smsmith
3338716Smsmith/*
3438716Smsmith * parse: accept a string of input and "parse" it for backslash
3538716Smsmith * substitutions and environment variable expansions (${var}),
3638716Smsmith * returning an argc/argv style vector of whitespace separated
3738716Smsmith * arguments.  Returns 0 on success, 1 on failure (ok, ok, so I
3838716Smsmith * wimped-out on the error codes! :).
3938716Smsmith *
4038716Smsmith * Note that the argv array returned must be freed by the caller, but
4138716Smsmith * we own the space allocated for arguments and will free that on next
4238716Smsmith * invocation.  This allows argv consumers to modify the array if
4338716Smsmith * required.
4438716Smsmith *
4538716Smsmith * NB: environment variables that expand to more than one whitespace
4638716Smsmith * separated token will be returned as a single argv[] element, not
4738716Smsmith * split in turn.  Expanded text is also immune to further backslash
4838716Smsmith * elimination or expansion since this is a one-pass, non-recursive
4938716Smsmith * parser.  You didn't specify more than this so if you want more, ask
5038716Smsmith * me. - jkh
5138716Smsmith */
5238716Smsmith
5338716Smsmith#define PARSE_FAIL(expr) \
5438716Smsmithif (expr) { \
5538716Smsmith    printf("fail at line %d\n", __LINE__); \
5638716Smsmith    clean(); \
5738716Smsmith    free(copy); \
5838716Smsmith    free(buf); \
5938716Smsmith    return 1; \
6038716Smsmith}
6138716Smsmith
6238716Smsmith/* Accept the usual delimiters for a variable, returning counterpart */
6338716Smsmithstatic char
6438716Smsmithisdelim(char ch)
6538716Smsmith{
6638716Smsmith    if (ch == '{')
6738716Smsmith	return '}';
6838716Smsmith    else if (ch == '(')
6938716Smsmith	return ')';
7038716Smsmith    return '\0';
7138716Smsmith}
7238716Smsmith
7338765Sjkhstatic int
7438765Sjkhisquote(char ch)
7538765Sjkh{
7638765Sjkh    return (ch == '\'' || ch == '"');
7738765Sjkh}
7838765Sjkh
7938716Smsmithint
8038716Smsmithparse(int *argc, char ***argv, char *str)
8138716Smsmith{
8238716Smsmith    int ac;
8338716Smsmith    char *val, *p, *q, *copy = NULL;
8438716Smsmith    int i = 0;
8538765Sjkh    char token, tmp, quote, *buf;
8638716Smsmith    enum { STR, VAR, WHITE } state;
8738716Smsmith
8838716Smsmith    ac = *argc = 0;
8938765Sjkh    quote = 0;
9038716Smsmith    if (!str || (p = copy = backslash(str)) == NULL)
9138716Smsmith	return 1;
9238716Smsmith
9338716Smsmith    /* Initialize vector and state */
9438716Smsmith    clean();
9538716Smsmith    state = STR;
9638765Sjkh    buf = (char *)malloc(PARSE_BUFSIZE);
9738716Smsmith    token = 0;
9838716Smsmith
9938716Smsmith    /* And awaaaaaaaaay we go! */
10038716Smsmith    while (*p) {
10138716Smsmith	switch (state) {
10238716Smsmith	case STR:
10342465Smsmith	    if ((*p == '\\') && p[1]) {
10442465Smsmith		p++;
10542465Smsmith		PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
10642620Smsmith		buf[i++] = *p++;
10742465Smsmith	    } else if (isquote(*p)) {
10838765Sjkh		quote = quote ? 0 : *p;
10938765Sjkh		++p;
11038765Sjkh	    }
11138765Sjkh	    else if (isspace(*p) && !quote) {
11238716Smsmith		state = WHITE;
11338716Smsmith		if (i) {
11438716Smsmith		    buf[i] = '\0';
11538716Smsmith		    PARSE_FAIL(insert(&ac, buf));
11638716Smsmith		    i = 0;
11738716Smsmith		}
11838716Smsmith		++p;
11938716Smsmith	    } else if (*p == '$') {
12038716Smsmith		token = isdelim(*(p + 1));
12138716Smsmith		if (token)
12238716Smsmith		    p += 2;
12338716Smsmith		else
12438716Smsmith		    ++p;
12538716Smsmith		state = VAR;
12638716Smsmith	    } else {
12738716Smsmith		PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
12838716Smsmith		buf[i++] = *p++;
12938716Smsmith	    }
13038716Smsmith	    break;
13138716Smsmith
13238716Smsmith	case WHITE:
13338716Smsmith	    if (isspace(*p))
13438716Smsmith		++p;
13538716Smsmith	    else
13638716Smsmith		state = STR;
13738716Smsmith	    break;
13838716Smsmith
13938716Smsmith	case VAR:
14038716Smsmith	    if (token) {
14138716Smsmith		PARSE_FAIL((q = index(p, token)) == NULL);
14238716Smsmith	    } else {
14338716Smsmith		q = p;
14438716Smsmith		while (*q && !isspace(*q))
14538716Smsmith		    ++q;
14638716Smsmith	    }
14738716Smsmith	    tmp = *q;
14838716Smsmith	    *q = '\0';
14938716Smsmith	    if ((val = variable_lookup(p)) != NULL) {
15038716Smsmith		int len = strlen(val);
15138716Smsmith
15238716Smsmith		strncpy(buf + i, val, PARSE_BUFSIZE - (i + 1));
15338789Smsmith		i += min(len, PARSE_BUFSIZE - 1);
15438716Smsmith	    }
15538716Smsmith	    *q = tmp;	/* restore value */
15638716Smsmith	    p = q + (token ? 1 : 0);
15738716Smsmith	    state = STR;
15838716Smsmith	    break;
15938716Smsmith	}
16038716Smsmith    }
16138716Smsmith    /* If at end of token, add it */
16238716Smsmith    if (i && state == STR) {
16338716Smsmith	buf[i] = '\0';
16438716Smsmith	PARSE_FAIL(insert(&ac, buf));
16538716Smsmith    }
16638716Smsmith    args[ac] = NULL;
16738716Smsmith    *argc = ac;
16838765Sjkh    *argv = (char **)malloc((sizeof(char *) * ac + 1));
16938716Smsmith    bcopy(args, *argv, sizeof(char *) * ac + 1);
17038716Smsmith    free(copy);
17138716Smsmith    return 0;
17238716Smsmith}
17338716Smsmith
17438716Smsmith#define MAXARGS	20
17538716Smsmith
17638716Smsmith/* Clean vector space */
17738716Smsmithstatic void
17838716Smsmithclean(void)
17938716Smsmith{
18038716Smsmith    int		i;
18138716Smsmith
18238716Smsmith    for (i = 0; i < MAXARGS; i++) {
18338716Smsmith	if (args[i] != NULL) {
18438716Smsmith	    free(args[i]);
18538716Smsmith	    args[i] = NULL;
18638716Smsmith	}
18738716Smsmith    }
18838716Smsmith}
18938716Smsmith
19038716Smsmithstatic int
19138716Smsmithinsert(int *argcp, char *buf)
19238716Smsmith{
19338716Smsmith    if (*argcp >= MAXARGS)
19438716Smsmith	return 1;
19538716Smsmith    args[(*argcp)++] = strdup(buf);
19638716Smsmith    return 0;
19738716Smsmith}
19838716Smsmith
19938716Smsmithstatic char *
20038716Smsmithvariable_lookup(char *name)
20138716Smsmith{
20238716Smsmith    /* XXX search "special variable" space first? */
20338716Smsmith    return (char *)getenv(name);
20438716Smsmith}
205