Changeset 15878 for trunk/Ohana/src/opihi/lib.shell/parse.c
- Timestamp:
- Dec 16, 2007, 2:27:00 PM (18 years ago)
- File:
-
- 1 edited
-
trunk/Ohana/src/opihi/lib.shell/parse.c (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Ohana/src/opihi/lib.shell/parse.c
r10073 r15878 1 1 # include "opihi.h" 2 2 3 void interpolate_slash (char *line);4 5 3 char *parse (char *line) { 6 4 7 5 double fval; 8 char *newline, *N, *L, *val, *B, *V, *V0, *V1, * end, *c1, *c2;9 int Nval, Nbytes, status, size ;6 char *newline, *N, *L, *val, *B, *V, *V0, *V1, *c1, *c2, *p; 7 int Nval, Nbytes, status, size, NLINE; 10 8 FILE *f; 11 9 Vector *vec; … … 64 62 65 63 /* val will hold the result */ 64 // XXX this is limiting!!! 66 65 ALLOCATE (val, char, 1024); 67 66 … … 153 152 154 153 /* case 3: {expression} */ 155 ALLOCATE (newline, char, 1024); 156 for (L = line, N = newline; *L != 0; L++, N++) { 157 158 /* copy elements from L (line) to N (newline) until we hit a '{' */ 159 for (; (*L != '{') && (*L != 0); L++, N++) *N = *L; 160 if (*L == 0) break; 161 162 if ((L != line) && (*(L-1) == 0x5c)) { 163 N--; 164 *N = *L; 154 NLINE = MAX (128, strlen (line)); 155 ALLOCATE (newline, char, NLINE); 156 memset (newline, 0, NLINE); 157 for (L = line; *L != 0; ) { 158 159 // copy elements from L (line) to newline up to first '{' 160 p = strchr (L, '{'); 161 newline = opihi_append (newline, &NLINE, L, p); 162 if (p == NULL) break; 163 L = p + 1; 164 165 // check for \{ at this point, replace \ in newline with { 166 if ((p > line) && (*(p - 1) == 0x5c)) { 167 N = newline + strlen(newline) - 1; 168 *N = '{'; // replace \ with { 165 169 continue; 166 170 } 167 171 168 172 /* check on the syntax of the line */ 169 L ++;173 L = p + 1; 170 174 c1 = strchr (L, '}'); 171 175 if (c1 == NULL) { … … 178 182 goto escape; 179 183 } 180 end = c1;181 184 *c1 = 0; 182 185 … … 188 191 goto escape; 189 192 } 193 194 /* interpolate vector element into newline (being accumulated) */ 195 newline = opihi_append (newline, &NLINE, val, NULL); 196 190 197 /* copy val to outline */ 191 for (V = val; *V != 0; V++,N++) *N = *V; 192 L = end; 193 N--; 198 L = c1 + 1; 194 199 free (val); val = (char *) NULL; 195 200 } 196 197 *N = 0; 201 198 202 free (line); line = (char *) NULL; 199 203 … … 204 208 return (newline); 205 209 206 error:210 error: 207 211 gprint (GP_ERR, "syntax error\n"); 208 212 209 escape:213 escape: 210 214 if (line != (char *) NULL) free (line); 211 215 if (val != (char *) NULL) free (val); … … 214 218 } 215 219 216 /* replace all instances of \A with A in line */ 217 void interpolate_slash (char *line) { 218 219 char *in, *out; 220 221 for (in = out = line; *in != 0; in++, out++) { 222 if (*in == 0x5c) in++; 223 *out = *in; 224 if (*in == 0) return; 225 } 226 *out = *in; 227 } 228 229 /* this routine looks for math and/or logic expressions that 230 need to be evaluated and passes them on to "parenthesis", 231 which evaluates the expression */ 232 233 /* There are two situations now which define an expression to 234 be evaluated: 235 1) $var = expression -- everything after the = must be a math expression or a string 236 2) {expression} -- everything within the {} must be a math expression 237 */ 238 239 /* case 1: $var = expression. test that 240 a) there is a $ as the first character 241 b) the variable defined by that $ is not null 242 c) there is an = sign following the variable 243 d) there is something following the = sign 244 */ 220 /* this routine looks for math and/or logic expressions that 221 need to be evaluated and passes them on to "parenthesis", 222 which evaluates the expression */ 223 224 /* There are two situations now which define an expression to 225 be evaluated: 226 1) $var = expression -- everything after the = must be a math expression or a string 227 2) {expression} -- everything within the {} must be a math expression 228 */ 229 230 /* case 1: $var = expression. test that 231 a) there is a $ as the first character 232 b) the variable defined by that $ is not null 233 c) there is an = sign following the variable 234 d) there is something following the = sign 235 */ 245 236 246 /* case 2: vect[N] = expression. check syntax:247 a) last char must be ]248 b) word must contain [249 c) between [ & ] must be an integer250 d) vector must exist251 e) there is an = sign following the first word252 f) there is something following the = sign253 */237 /* case 2: vect[N] = expression. check syntax: 238 a) last char must be ] 239 b) word must contain [ 240 c) between [ & ] must be an integer 241 d) vector must exist 242 e) there is an = sign following the first word 243 f) there is something following the = sign 244 */ 254 245 255 246 256 /* case 3: we hunt for '{', and then the first '}' and pass everything257 inbetween. no nested {{}}s are allowed! */247 /* case 3: we hunt for '{', and then the first '}' and pass everything 248 inbetween. no nested {{}}s are allowed! */ 258 249 259 250 /* thisword ALLOCATES
Note:
See TracChangeset
for help on using the changeset viewer.
