interp_parse.c revision 64187
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 * 1450477Speter * $FreeBSD: head/sys/boot/common/interp_parse.c 64187 2000-08-03 09:14:02Z jhb $ 1538716Smsmith * 1638716Smsmith * The meat of the simple parser. 1738716Smsmith */ 1838716Smsmith 1938716Smsmith#include <stand.h> 2038716Smsmith#include <string.h> 2164187Sjhb#include "bootstrap.h" 2238716Smsmith 2364187Sjhbstatic void clean(void); 2464187Sjhbstatic int insert(int *argcp, char *buf); 2564187Sjhbstatic char *variable_lookup(char *name); 2638716Smsmith 2738716Smsmith#define PARSE_BUFSIZE 1024 /* maximum size of one element */ 2838716Smsmith#define MAXARGS 20 /* maximum number of elements */ 2938716Smsmithstatic char *args[MAXARGS]; 3038716Smsmith 3138716Smsmith/* 3238716Smsmith * parse: accept a string of input and "parse" it for backslash 3338716Smsmith * substitutions and environment variable expansions (${var}), 3438716Smsmith * returning an argc/argv style vector of whitespace separated 3538716Smsmith * arguments. Returns 0 on success, 1 on failure (ok, ok, so I 3638716Smsmith * wimped-out on the error codes! :). 3738716Smsmith * 3838716Smsmith * Note that the argv array returned must be freed by the caller, but 3938716Smsmith * we own the space allocated for arguments and will free that on next 4038716Smsmith * invocation. This allows argv consumers to modify the array if 4138716Smsmith * required. 4238716Smsmith * 4338716Smsmith * NB: environment variables that expand to more than one whitespace 4438716Smsmith * separated token will be returned as a single argv[] element, not 4538716Smsmith * split in turn. Expanded text is also immune to further backslash 4638716Smsmith * elimination or expansion since this is a one-pass, non-recursive 4738716Smsmith * parser. You didn't specify more than this so if you want more, ask 4838716Smsmith * me. - jkh 4938716Smsmith */ 5038716Smsmith 5138716Smsmith#define PARSE_FAIL(expr) \ 5238716Smsmithif (expr) { \ 5338716Smsmith printf("fail at line %d\n", __LINE__); \ 5438716Smsmith clean(); \ 5538716Smsmith free(copy); \ 5638716Smsmith free(buf); \ 5738716Smsmith return 1; \ 5838716Smsmith} 5938716Smsmith 6038716Smsmith/* Accept the usual delimiters for a variable, returning counterpart */ 6138716Smsmithstatic char 6264187Sjhbisdelim(int ch) 6338716Smsmith{ 6438716Smsmith if (ch == '{') 6538716Smsmith return '}'; 6638716Smsmith else if (ch == '(') 6738716Smsmith return ')'; 6838716Smsmith return '\0'; 6938716Smsmith} 7038716Smsmith 7138765Sjkhstatic int 7264187Sjhbisquote(int ch) 7338765Sjkh{ 7438765Sjkh return (ch == '\'' || ch == '"'); 7538765Sjkh} 7638765Sjkh 7738716Smsmithint 7838716Smsmithparse(int *argc, char ***argv, char *str) 7938716Smsmith{ 8038716Smsmith int ac; 8138716Smsmith char *val, *p, *q, *copy = NULL; 8264187Sjhb size_t i = 0; 8338765Sjkh char token, tmp, quote, *buf; 8438716Smsmith enum { STR, VAR, WHITE } state; 8538716Smsmith 8638716Smsmith ac = *argc = 0; 8738765Sjkh quote = 0; 8838716Smsmith if (!str || (p = copy = backslash(str)) == NULL) 8938716Smsmith return 1; 9038716Smsmith 9138716Smsmith /* Initialize vector and state */ 9238716Smsmith clean(); 9338716Smsmith state = STR; 9438765Sjkh buf = (char *)malloc(PARSE_BUFSIZE); 9538716Smsmith token = 0; 9638716Smsmith 9738716Smsmith /* And awaaaaaaaaay we go! */ 9838716Smsmith while (*p) { 9938716Smsmith switch (state) { 10038716Smsmith case STR: 10142465Smsmith if ((*p == '\\') && p[1]) { 10242465Smsmith p++; 10342465Smsmith PARSE_FAIL(i == (PARSE_BUFSIZE - 1)); 10442620Smsmith buf[i++] = *p++; 10542465Smsmith } else if (isquote(*p)) { 10638765Sjkh quote = quote ? 0 : *p; 10738765Sjkh ++p; 10838765Sjkh } 10938765Sjkh else if (isspace(*p) && !quote) { 11038716Smsmith state = WHITE; 11138716Smsmith if (i) { 11238716Smsmith buf[i] = '\0'; 11338716Smsmith PARSE_FAIL(insert(&ac, buf)); 11438716Smsmith i = 0; 11538716Smsmith } 11638716Smsmith ++p; 11738716Smsmith } else if (*p == '$') { 11838716Smsmith token = isdelim(*(p + 1)); 11938716Smsmith if (token) 12038716Smsmith p += 2; 12138716Smsmith else 12238716Smsmith ++p; 12338716Smsmith state = VAR; 12438716Smsmith } else { 12538716Smsmith PARSE_FAIL(i == (PARSE_BUFSIZE - 1)); 12638716Smsmith buf[i++] = *p++; 12738716Smsmith } 12838716Smsmith break; 12938716Smsmith 13038716Smsmith case WHITE: 13138716Smsmith if (isspace(*p)) 13238716Smsmith ++p; 13338716Smsmith else 13438716Smsmith state = STR; 13538716Smsmith break; 13638716Smsmith 13738716Smsmith case VAR: 13838716Smsmith if (token) { 13938716Smsmith PARSE_FAIL((q = index(p, token)) == NULL); 14038716Smsmith } else { 14138716Smsmith q = p; 14238716Smsmith while (*q && !isspace(*q)) 14338716Smsmith ++q; 14438716Smsmith } 14538716Smsmith tmp = *q; 14638716Smsmith *q = '\0'; 14738716Smsmith if ((val = variable_lookup(p)) != NULL) { 14864187Sjhb size_t len = strlen(val); 14938716Smsmith 15038716Smsmith strncpy(buf + i, val, PARSE_BUFSIZE - (i + 1)); 15138789Smsmith i += min(len, PARSE_BUFSIZE - 1); 15238716Smsmith } 15338716Smsmith *q = tmp; /* restore value */ 15438716Smsmith p = q + (token ? 1 : 0); 15538716Smsmith state = STR; 15638716Smsmith break; 15738716Smsmith } 15838716Smsmith } 15938716Smsmith /* If at end of token, add it */ 16038716Smsmith if (i && state == STR) { 16138716Smsmith buf[i] = '\0'; 16238716Smsmith PARSE_FAIL(insert(&ac, buf)); 16338716Smsmith } 16438716Smsmith args[ac] = NULL; 16538716Smsmith *argc = ac; 16638765Sjkh *argv = (char **)malloc((sizeof(char *) * ac + 1)); 16738716Smsmith bcopy(args, *argv, sizeof(char *) * ac + 1); 16844570Sdcs free(buf); 16938716Smsmith free(copy); 17038716Smsmith return 0; 17138716Smsmith} 17238716Smsmith 17338716Smsmith#define MAXARGS 20 17438716Smsmith 17538716Smsmith/* Clean vector space */ 17638716Smsmithstatic void 17738716Smsmithclean(void) 17838716Smsmith{ 17938716Smsmith int i; 18038716Smsmith 18138716Smsmith for (i = 0; i < MAXARGS; i++) { 18238716Smsmith if (args[i] != NULL) { 18338716Smsmith free(args[i]); 18438716Smsmith args[i] = NULL; 18538716Smsmith } 18638716Smsmith } 18738716Smsmith} 18838716Smsmith 18938716Smsmithstatic int 19038716Smsmithinsert(int *argcp, char *buf) 19138716Smsmith{ 19238716Smsmith if (*argcp >= MAXARGS) 19338716Smsmith return 1; 19438716Smsmith args[(*argcp)++] = strdup(buf); 19538716Smsmith return 0; 19638716Smsmith} 19738716Smsmith 19838716Smsmithstatic char * 19938716Smsmithvariable_lookup(char *name) 20038716Smsmith{ 20138716Smsmith /* XXX search "special variable" space first? */ 20238716Smsmith return (char *)getenv(name); 20338716Smsmith} 204