/*-
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Jordan K. Hubbard
* 29 August 1998
*
* The meat of the simple parser.
*/
#define PARSE_BUFSIZE 1024 /* maximum size of one element */
#define MAXARGS 20 /* maximum number of elements */
static char *args[MAXARGS];
/*
* parse: accept a string of input and "parse" it for backslash
* substitutions and environment variable expansions (${var}),
* returning an argc/argv style vector of whitespace separated
* arguments. Returns 0 on success, 1 on failure (ok, ok, so I
* wimped-out on the error codes! :).
*
* Note that the argv array returned must be freed by the caller, but
* we own the space allocated for arguments and will free that on next
* invocation. This allows argv consumers to modify the array if
* required.
*
* NB: environment variables that expand to more than one whitespace
* separated token will be returned as a single argv[] element, not
* split in turn. Expanded text is also immune to further backslash
* elimination or expansion since this is a one-pass, non-recursive
* parser. You didn't specify more than this so if you want more, ask
* me. - jkh
*/
#define PARSE_FAIL(expr) \
if (expr) { \
printf("fail at line %d\n", __LINE__); \
clean(); \
free(copy); \
free(buf); \
return 1; \
}
/* Accept the usual delimiters for a variable, returning counterpart */
static char
isdelim(int ch)
{
if (ch == '{')
return '}';
else if (ch == '(')
return ')';
return '\0';
}