interp_parse.c revision 1.2
11.2Scherry/*	$NetBSD: interp_parse.c,v 1.2 2006/04/22 07:58:53 cherry Exp $	*/
21.1Scherry
31.1Scherry/*-
41.1Scherry * Redistribution and use in source and binary forms, with or without
51.1Scherry * modification, are permitted provided that the following conditions
61.1Scherry * are met:
71.1Scherry * 1. Redistributions of source code must retain the above copyright
81.1Scherry *    notice, this list of conditions and the following disclaimer.
91.1Scherry * 2. Redistributions in binary form must reproduce the above copyright
101.1Scherry *    notice, this list of conditions and the following disclaimer in the
111.1Scherry *    documentation and/or other materials provided with the distribution.
121.1Scherry *
131.1Scherry * Jordan K. Hubbard
141.1Scherry * 29 August 1998
151.1Scherry *
161.1Scherry * The meat of the simple parser.
171.1Scherry */
181.1Scherry
191.1Scherry#include <sys/cdefs.h>
201.2Scherry/* __FBSDID("$FreeBSD: src/sys/boot/common/interp_parse.c,v 1.10 2003/08/25 23:30:41 obrien Exp $"); */
211.1Scherry
221.1Scherry#include <lib/libsa/stand.h>
231.1Scherry#include <lib/libkern/libkern.h>
241.1Scherry
251.1Scherry#include "bootstrap.h"
261.1Scherry
271.1Scherrystatic void	 clean(void);
281.1Scherrystatic int	 insert(int *argcp, char *buf);
291.1Scherrystatic char	*variable_lookup(char *name);
301.1Scherry
311.1Scherry#define PARSE_BUFSIZE	1024	/* maximum size of one element */
321.1Scherry#define MAXARGS		20	/* maximum number of elements */
331.1Scherrystatic char		*args[MAXARGS];
341.1Scherry
351.1Scherry/*
361.1Scherry * parse: accept a string of input and "parse" it for backslash
371.1Scherry * substitutions and environment variable expansions (${var}),
381.1Scherry * returning an argc/argv style vector of whitespace separated
391.1Scherry * arguments.  Returns 0 on success, 1 on failure (ok, ok, so I
401.1Scherry * wimped-out on the error codes! :).
411.1Scherry *
421.1Scherry * Note that the argv array returned must be freed by the caller, but
431.1Scherry * we own the space allocated for arguments and will free that on next
441.1Scherry * invocation.  This allows argv consumers to modify the array if
451.1Scherry * required.
461.1Scherry *
471.1Scherry * NB: environment variables that expand to more than one whitespace
481.1Scherry * separated token will be returned as a single argv[] element, not
491.1Scherry * split in turn.  Expanded text is also immune to further backslash
501.1Scherry * elimination or expansion since this is a one-pass, non-recursive
511.1Scherry * parser.  You didn't specify more than this so if you want more, ask
521.1Scherry * me. - jkh
531.1Scherry */
541.1Scherry
551.1Scherry#define PARSE_FAIL(expr) \
561.1Scherryif (expr) { \
571.1Scherry    printf("fail at line %d\n", __LINE__); \
581.1Scherry    clean(); \
591.1Scherry    free(copy); \
601.1Scherry    free(buf); \
611.1Scherry    return 1; \
621.1Scherry}
631.1Scherry
641.1Scherry/* Accept the usual delimiters for a variable, returning counterpart */
651.1Scherrystatic char
661.1Scherryisdelim(int ch)
671.1Scherry{
681.1Scherry    if (ch == '{')
691.1Scherry	return '}';
701.1Scherry    else if (ch == '(')
711.1Scherry	return ')';
721.1Scherry    return '\0';
731.1Scherry}
741.1Scherry
751.1Scherrystatic int
761.1Scherryisquote(int ch)
771.1Scherry{
781.1Scherry    return (ch == '\'' || ch == '"');
791.1Scherry}
801.1Scherry
811.1Scherryint
821.1Scherryparse(int *argc, char ***argv, char *str)
831.1Scherry{
841.1Scherry    int ac;
851.1Scherry    char *val, *p, *q, *copy = NULL;
861.1Scherry    size_t i = 0;
871.1Scherry    char token, tmp, quote, *buf;
881.1Scherry    enum { STR, VAR, WHITE } state;
891.1Scherry
901.1Scherry    ac = *argc = 0;
911.1Scherry    quote = 0;
921.1Scherry    if (!str || (p = copy = backslash(str)) == NULL)
931.1Scherry	return 1;
941.1Scherry
951.1Scherry    /* Initialize vector and state */
961.1Scherry    clean();
971.1Scherry    state = STR;
981.1Scherry    buf = (char *)alloc(PARSE_BUFSIZE);
991.1Scherry    token = 0;
1001.1Scherry
1011.1Scherry    /* And awaaaaaaaaay we go! */
1021.1Scherry    while (*p) {
1031.1Scherry	switch (state) {
1041.1Scherry	case STR:
1051.1Scherry	    if ((*p == '\\') && p[1]) {
1061.1Scherry		p++;
1071.1Scherry		PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
1081.1Scherry		buf[i++] = *p++;
1091.1Scherry	    } else if (isquote(*p)) {
1101.1Scherry		quote = quote ? 0 : *p;
1111.1Scherry		++p;
1121.1Scherry	    }
1131.1Scherry	    else if (isspace(*p) && !quote) {
1141.1Scherry		state = WHITE;
1151.1Scherry		if (i) {
1161.1Scherry		    buf[i] = '\0';
1171.1Scherry		    PARSE_FAIL(insert(&ac, buf));
1181.1Scherry		    i = 0;
1191.1Scherry		}
1201.1Scherry		++p;
1211.1Scherry	    } else if (*p == '$') {
1221.1Scherry		token = isdelim(*(p + 1));
1231.1Scherry		if (token)
1241.1Scherry		    p += 2;
1251.1Scherry		else
1261.1Scherry		    ++p;
1271.1Scherry		state = VAR;
1281.1Scherry	    } else {
1291.1Scherry		PARSE_FAIL(i == (PARSE_BUFSIZE - 1));
1301.1Scherry		buf[i++] = *p++;
1311.1Scherry	    }
1321.1Scherry	    break;
1331.1Scherry
1341.1Scherry	case WHITE:
1351.1Scherry	    if (isspace(*p))
1361.1Scherry		++p;
1371.1Scherry	    else
1381.1Scherry		state = STR;
1391.1Scherry	    break;
1401.1Scherry
1411.1Scherry	case VAR:
1421.1Scherry	    if (token) {
1431.1Scherry		PARSE_FAIL((q = strchr(p, token)) == NULL);
1441.1Scherry	    } else {
1451.1Scherry		q = p;
1461.1Scherry		while (*q && !isspace(*q))
1471.1Scherry		    ++q;
1481.1Scherry	    }
1491.1Scherry	    tmp = *q;
1501.1Scherry	    *q = '\0';
1511.1Scherry	    if ((val = variable_lookup(p)) != NULL) {
1521.1Scherry		size_t len = strlen(val);
1531.1Scherry
1541.1Scherry		strncpy(buf + i, val, PARSE_BUFSIZE - (i + 1));
1551.1Scherry		i += min(len, PARSE_BUFSIZE - 1);
1561.1Scherry	    }
1571.1Scherry	    *q = tmp;	/* restore value */
1581.1Scherry	    p = q + (token ? 1 : 0);
1591.1Scherry	    state = STR;
1601.1Scherry	    break;
1611.1Scherry	}
1621.1Scherry    }
1631.1Scherry    /* If at end of token, add it */
1641.1Scherry    if (i && state == STR) {
1651.1Scherry	buf[i] = '\0';
1661.1Scherry	PARSE_FAIL(insert(&ac, buf));
1671.1Scherry    }
1681.1Scherry    args[ac] = NULL;
1691.1Scherry    *argc = ac;
1701.1Scherry    *argv = (char **)alloc((sizeof(char *) * ac + 1));
1711.1Scherry    bcopy(args, *argv, sizeof(char *) * ac + 1);
1721.1Scherry    free(buf);
1731.1Scherry    free(copy);
1741.1Scherry    return 0;
1751.1Scherry}
1761.1Scherry
1771.1Scherry#define MAXARGS	20
1781.1Scherry
1791.1Scherry/* Clean vector space */
1801.1Scherrystatic void
1811.1Scherryclean(void)
1821.1Scherry{
1831.1Scherry    int		i;
1841.1Scherry
1851.1Scherry    for (i = 0; i < MAXARGS; i++) {
1861.1Scherry	if (args[i] != NULL) {
1871.1Scherry	    free(args[i]);
1881.1Scherry	    args[i] = NULL;
1891.1Scherry	}
1901.1Scherry    }
1911.1Scherry}
1921.1Scherry
1931.1Scherrystatic int
1941.1Scherryinsert(int *argcp, char *buf)
1951.1Scherry{
1961.1Scherry    if (*argcp >= MAXARGS)
1971.1Scherry	return 1;
1981.1Scherry    args[(*argcp)++] = strdup(buf);
1991.1Scherry    return 0;
2001.1Scherry}
2011.1Scherry
2021.1Scherrystatic char *
2031.1Scherryvariable_lookup(char *name)
2041.1Scherry{
2051.1Scherry    /* XXX search "special variable" space first? */
2061.1Scherry    return (char *)getenv(name);
2071.1Scherry}
208