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