1119483Sobrien/*-
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 * The meat of the simple parser.
1538716Smsmith */
1638716Smsmith
17119483Sobrien#include <sys/cdefs.h>
18119483Sobrien__FBSDID("$FreeBSD$");
19119483Sobrien
2038716Smsmith#include <stand.h>
2138716Smsmith#include <string.h>
2264187Sjhb#include "bootstrap.h"
2338716Smsmith
2464187Sjhbstatic void	 clean(void);
2564187Sjhbstatic int	 insert(int *argcp, char *buf);
2664187Sjhbstatic char	*variable_lookup(char *name);
2738716Smsmith
2838716Smsmith#define PARSE_BUFSIZE	1024	/* maximum size of one element */
2938716Smsmith#define MAXARGS		20	/* maximum number of elements */
3038716Smsmithstatic char		*args[MAXARGS];
3138716Smsmith
3238716Smsmith/*
3338716Smsmith * parse: accept a string of input and "parse" it for backslash
3438716Smsmith * substitutions and environment variable expansions (${var}),
3538716Smsmith * returning an argc/argv style vector of whitespace separated
3638716Smsmith * arguments.  Returns 0 on success, 1 on failure (ok, ok, so I
3738716Smsmith * wimped-out on the error codes! :).
3838716Smsmith *
3938716Smsmith * Note that the argv array returned must be freed by the caller, but
4038716Smsmith * we own the space allocated for arguments and will free that on next
4138716Smsmith * invocation.  This allows argv consumers to modify the array if
4238716Smsmith * required.
4338716Smsmith *
4438716Smsmith * NB: environment variables that expand to more than one whitespace
4538716Smsmith * separated token will be returned as a single argv[] element, not
4638716Smsmith * split in turn.  Expanded text is also immune to further backslash
4738716Smsmith * elimination or expansion since this is a one-pass, non-recursive
4838716Smsmith * parser.  You didn't specify more than this so if you want more, ask
4938716Smsmith * me. - jkh
5038716Smsmith */
5138716Smsmith
5238716Smsmith#define PARSE_FAIL(expr) \
5338716Smsmithif (expr) { \
5438716Smsmith    printf("fail at line %d\n", __LINE__); \
5538716Smsmith    clean(); \
5638716Smsmith    free(copy); \
5738716Smsmith    free(buf); \
5838716Smsmith    return 1; \
5938716Smsmith}
6038716Smsmith
6138716Smsmith/* Accept the usual delimiters for a variable, returning counterpart */
6238716Smsmithstatic char
6364187Sjhbisdelim(int ch)
6438716Smsmith{
6538716Smsmith    if (ch == '{')
6638716Smsmith	return '}';
6738716Smsmith    else if (ch == '(')
6838716Smsmith	return ')';
6938716Smsmith    return '\0';
7038716Smsmith}
7138716Smsmith
7238765Sjkhstatic int
7364187Sjhbisquote(int ch)
7438765Sjkh{
7538765Sjkh    return (ch == '\'' || ch == '"');
7638765Sjkh}
7738765Sjkh
7838716Smsmithint
7938716Smsmithparse(int *argc, char ***argv, char *str)
8038716Smsmith{
8138716Smsmith    int ac;
8238716Smsmith    char *val, *p, *q, *copy = NULL;
8364187Sjhb    size_t i = 0;
8438765Sjkh    char token, tmp, quote, *buf;
8538716Smsmith    enum { STR, VAR, WHITE } state;
8638716Smsmith
8738716Smsmith    ac = *argc = 0;
8838765Sjkh    quote = 0;
8938716Smsmith    if (!str || (p = copy = backslash(str)) == NULL)
9038716Smsmith	return 1;
9138716Smsmith
9238716Smsmith    /* Initialize vector and state */
9338716Smsmith    clean();
9438716Smsmith    state = STR;
9538765Sjkh    buf = (char *)malloc(PARSE_BUFSIZE);
9638716Smsmith    token = 0;
9738716Smsmith
9838716Smsmith    /* And awaaaaaaaaay we go! */
9938716Smsmith    while (*p) {
10038716Smsmith	switch (state) {
10138716Smsmith	case STR:
10242465Smsmith	    if ((*p == '\\') && p[1]) {
10342465Smsmith		p++;
10442465Smsmith		PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
10542620Smsmith		buf[i++] = *p++;
10642465Smsmith	    } else if (isquote(*p)) {
10738765Sjkh		quote = quote ? 0 : *p;
10838765Sjkh		++p;
10938765Sjkh	    }
11038765Sjkh	    else if (isspace(*p) && !quote) {
11138716Smsmith		state = WHITE;
11238716Smsmith		if (i) {
11338716Smsmith		    buf[i] = '\0';
11438716Smsmith		    PARSE_FAIL(insert(&ac, buf));
11538716Smsmith		    i = 0;
11638716Smsmith		}
11738716Smsmith		++p;
11838716Smsmith	    } else if (*p == '$') {
11938716Smsmith		token = isdelim(*(p + 1));
12038716Smsmith		if (token)
12138716Smsmith		    p += 2;
12238716Smsmith		else
12338716Smsmith		    ++p;
12438716Smsmith		state = VAR;
12538716Smsmith	    } else {
12638716Smsmith		PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
12738716Smsmith		buf[i++] = *p++;
12838716Smsmith	    }
12938716Smsmith	    break;
13038716Smsmith
13138716Smsmith	case WHITE:
13238716Smsmith	    if (isspace(*p))
13338716Smsmith		++p;
13438716Smsmith	    else
13538716Smsmith		state = STR;
13638716Smsmith	    break;
13738716Smsmith
13838716Smsmith	case VAR:
13938716Smsmith	    if (token) {
14038716Smsmith		PARSE_FAIL((q = index(p, token)) == NULL);
14138716Smsmith	    } else {
14238716Smsmith		q = p;
14338716Smsmith		while (*q && !isspace(*q))
14438716Smsmith		    ++q;
14538716Smsmith	    }
14638716Smsmith	    tmp = *q;
14738716Smsmith	    *q = '\0';
14838716Smsmith	    if ((val = variable_lookup(p)) != NULL) {
14964187Sjhb		size_t len = strlen(val);
15038716Smsmith
15138716Smsmith		strncpy(buf + i, val, PARSE_BUFSIZE - (i + 1));
15238789Smsmith		i += min(len, PARSE_BUFSIZE - 1);
15338716Smsmith	    }
15438716Smsmith	    *q = tmp;	/* restore value */
15538716Smsmith	    p = q + (token ? 1 : 0);
15638716Smsmith	    state = STR;
15738716Smsmith	    break;
15838716Smsmith	}
15938716Smsmith    }
16038716Smsmith    /* If at end of token, add it */
16138716Smsmith    if (i && state == STR) {
16238716Smsmith	buf[i] = '\0';
16338716Smsmith	PARSE_FAIL(insert(&ac, buf));
16438716Smsmith    }
16538716Smsmith    args[ac] = NULL;
16638716Smsmith    *argc = ac;
16738765Sjkh    *argv = (char **)malloc((sizeof(char *) * ac + 1));
16838716Smsmith    bcopy(args, *argv, sizeof(char *) * ac + 1);
16944570Sdcs    free(buf);
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