interp_parse.c revision 38765
1/* 2 * Redistribution and use in source and binary forms, with or without 3 * modification, are permitted provided that the following conditions 4 * are met: 5 * 1. Redistributions of source code must retain the above copyright 6 * notice, this list of conditions and the following disclaimer. 7 * 2. Redistributions in binary form must reproduce the above copyright 8 * notice, this list of conditions and the following disclaimer in the 9 * documentation and/or other materials provided with the distribution. 10 * 11 * Jordan K. Hubbard 12 * 29 August 1998 13 * 14 * $Id: interp_parse.c,v 1.1 1998/09/01 00:41:24 msmith Exp $ 15 * 16 * The meat of the simple parser. 17 */ 18 19#include <stand.h> 20#include <string.h> 21 22/* Forward decls */ 23extern char *backslash(char *str); 24 25static void init(int *argcp, char ***argvp); 26static void clean(void); 27static int insert(int *argcp, char *buf); 28static char *variable_lookup(char *name); 29 30#define PARSE_BUFSIZE 1024 /* maximum size of one element */ 31#define MAXARGS 20 /* maximum number of elements */ 32static char *args[MAXARGS]; 33 34/* 35 * parse: accept a string of input and "parse" it for backslash 36 * substitutions and environment variable expansions (${var}), 37 * returning an argc/argv style vector of whitespace separated 38 * arguments. Returns 0 on success, 1 on failure (ok, ok, so I 39 * wimped-out on the error codes! :). 40 * 41 * Note that the argv array returned must be freed by the caller, but 42 * we own the space allocated for arguments and will free that on next 43 * invocation. This allows argv consumers to modify the array if 44 * required. 45 * 46 * NB: environment variables that expand to more than one whitespace 47 * separated token will be returned as a single argv[] element, not 48 * split in turn. Expanded text is also immune to further backslash 49 * elimination or expansion since this is a one-pass, non-recursive 50 * parser. You didn't specify more than this so if you want more, ask 51 * me. - jkh 52 */ 53 54#define PARSE_FAIL(expr) \ 55if (expr) { \ 56 printf("fail at line %d\n", __LINE__); \ 57 clean(); \ 58 free(copy); \ 59 free(buf); \ 60 return 1; \ 61} 62 63/* Accept the usual delimiters for a variable, returning counterpart */ 64static char 65isdelim(char ch) 66{ 67 if (ch == '{') 68 return '}'; 69 else if (ch == '(') 70 return ')'; 71 return '\0'; 72} 73 74static int 75isquote(char ch) 76{ 77 return (ch == '\'' || ch == '"'); 78} 79 80int 81parse(int *argc, char ***argv, char *str) 82{ 83 int ac; 84 char *val, *p, *q, *copy = NULL; 85 int i = 0; 86 char token, tmp, quote, *buf; 87 enum { STR, VAR, WHITE } state; 88 89 ac = *argc = 0; 90 quote = 0; 91 if (!str || (p = copy = backslash(str)) == NULL) 92 return 1; 93 94 /* Initialize vector and state */ 95 clean(); 96 state = STR; 97 buf = (char *)malloc(PARSE_BUFSIZE); 98 token = 0; 99 100 /* And awaaaaaaaaay we go! */ 101 while (*p) { 102 switch (state) { 103 case STR: 104 if (isquote(*p)) { 105 quote = quote ? 0 : *p; 106 ++p; 107 } 108 else if (isspace(*p) && !quote) { 109 state = WHITE; 110 if (i) { 111 buf[i] = '\0'; 112 PARSE_FAIL(insert(&ac, buf)); 113 i = 0; 114 } 115 ++p; 116 } else if (*p == '$') { 117 token = isdelim(*(p + 1)); 118 if (token) 119 p += 2; 120 else 121 ++p; 122 state = VAR; 123 } else { 124 PARSE_FAIL(i == (PARSE_BUFSIZE - 1)); 125 buf[i++] = *p++; 126 } 127 break; 128 129 case WHITE: 130 if (isspace(*p)) 131 ++p; 132 else 133 state = STR; 134 break; 135 136 case VAR: 137 if (token) { 138 PARSE_FAIL((q = index(p, token)) == NULL); 139 } else { 140 q = p; 141 while (*q && !isspace(*q)) 142 ++q; 143 } 144 tmp = *q; 145 *q = '\0'; 146 if ((val = variable_lookup(p)) != NULL) { 147 int len = strlen(val); 148 149 strncpy(buf + i, val, PARSE_BUFSIZE - (i + 1)); 150 i += min(len, sizeof(buf) - 1); 151 } 152 *q = tmp; /* restore value */ 153 p = q + (token ? 1 : 0); 154 state = STR; 155 break; 156 } 157 } 158 /* If at end of token, add it */ 159 if (i && state == STR) { 160 buf[i] = '\0'; 161 PARSE_FAIL(insert(&ac, buf)); 162 } 163 args[ac] = NULL; 164 *argc = ac; 165 *argv = (char **)malloc((sizeof(char *) * ac + 1)); 166 bcopy(args, *argv, sizeof(char *) * ac + 1); 167 free(copy); 168 return 0; 169} 170 171#define MAXARGS 20 172 173/* Clean vector space */ 174static void 175clean(void) 176{ 177 int i; 178 179 for (i = 0; i < MAXARGS; i++) { 180 if (args[i] != NULL) { 181 free(args[i]); 182 args[i] = NULL; 183 } 184 } 185} 186 187static int 188insert(int *argcp, char *buf) 189{ 190 if (*argcp >= MAXARGS) 191 return 1; 192 args[(*argcp)++] = strdup(buf); 193 return 0; 194} 195 196static char * 197variable_lookup(char *name) 198{ 199 /* XXX search "special variable" space first? */ 200 return (char *)getenv(name); 201} 202