tran.c (90902) | tran.c (107806) |
---|---|
1/**************************************************************** 2Copyright (C) Lucent Technologies 1997 3All Rights Reserved 4 5Permission to use, copy, modify, and distribute this software and 6its documentation for any purpose and without fee is hereby 7granted, provided that the above copyright notice appear in all 8copies and that both that the copyright notice and this --- 166 unchanged lines hidden (view full) --- 175 tp->tab[i] = 0; 176 } 177 if (tp->nelem != 0) 178 WARNING("can't happen: inconsistent element count freeing %s", ap->nval); 179 free(tp->tab); 180 free(tp); 181} 182 | 1/**************************************************************** 2Copyright (C) Lucent Technologies 1997 3All Rights Reserved 4 5Permission to use, copy, modify, and distribute this software and 6its documentation for any purpose and without fee is hereby 7granted, provided that the above copyright notice appear in all 8copies and that both that the copyright notice and this --- 166 unchanged lines hidden (view full) --- 175 tp->tab[i] = 0; 176 } 177 if (tp->nelem != 0) 178 WARNING("can't happen: inconsistent element count freeing %s", ap->nval); 179 free(tp->tab); 180 free(tp); 181} 182 |
183void freeelem(Cell *ap, char *s) /* free elem s from ap (i.e., ap["s"] */ | 183void freeelem(Cell *ap, const char *s) /* free elem s from ap (i.e., ap["s"] */ |
184{ 185 Array *tp; 186 Cell *p, *prev = NULL; 187 int h; 188 189 tp = (Array *) ap->sval; 190 h = hash(s, tp->size); 191 for (p = tp->tab[h]; p != NULL; prev = p, p = p->cnext) --- 6 unchanged lines hidden (view full) --- 198 xfree(p->sval); 199 free(p->nval); 200 free(p); 201 tp->nelem--; 202 return; 203 } 204} 205 | 184{ 185 Array *tp; 186 Cell *p, *prev = NULL; 187 int h; 188 189 tp = (Array *) ap->sval; 190 h = hash(s, tp->size); 191 for (p = tp->tab[h]; p != NULL; prev = p, p = p->cnext) --- 6 unchanged lines hidden (view full) --- 198 xfree(p->sval); 199 free(p->nval); 200 free(p); 201 tp->nelem--; 202 return; 203 } 204} 205 |
206Cell *setsymtab(char *n, char *s, Awkfloat f, unsigned t, Array *tp) | 206Cell *setsymtab(const char *n, const char *s, Awkfloat f, unsigned t, Array *tp) |
207{ 208 int h; 209 Cell *p; 210 211 if (n != NULL && (p = lookup(n, tp)) != NULL) { 212 dprintf( ("setsymtab found %p: n=%s s=\"%s\" f=%g t=%o\n", | 207{ 208 int h; 209 Cell *p; 210 211 if (n != NULL && (p = lookup(n, tp)) != NULL) { 212 dprintf( ("setsymtab found %p: n=%s s=\"%s\" f=%g t=%o\n", |
213 p, p->nval, p->sval, p->fval, p->tval) ); | 213 p, NN(p->nval), NN(p->sval), p->fval, p->tval) ); |
214 return(p); 215 } 216 p = (Cell *) malloc(sizeof(Cell)); 217 if (p == NULL) 218 FATAL("out of space for symbol table at %s", n); 219 p->nval = tostring(n); 220 p->sval = s ? tostring(s) : tostring(""); 221 p->fval = f; --- 6 unchanged lines hidden (view full) --- 228 h = hash(n, tp->size); 229 p->cnext = tp->tab[h]; 230 tp->tab[h] = p; 231 dprintf( ("setsymtab set %p: n=%s s=\"%s\" f=%g t=%o\n", 232 p, p->nval, p->sval, p->fval, p->tval) ); 233 return(p); 234} 235 | 214 return(p); 215 } 216 p = (Cell *) malloc(sizeof(Cell)); 217 if (p == NULL) 218 FATAL("out of space for symbol table at %s", n); 219 p->nval = tostring(n); 220 p->sval = s ? tostring(s) : tostring(""); 221 p->fval = f; --- 6 unchanged lines hidden (view full) --- 228 h = hash(n, tp->size); 229 p->cnext = tp->tab[h]; 230 tp->tab[h] = p; 231 dprintf( ("setsymtab set %p: n=%s s=\"%s\" f=%g t=%o\n", 232 p, p->nval, p->sval, p->fval, p->tval) ); 233 return(p); 234} 235 |
236int hash(char *s, int n) /* form hash value for string s */ | 236int hash(const char *s, int n) /* form hash value for string s */ |
237{ 238 unsigned hashval; 239 240 for (hashval = 0; *s != '\0'; s++) 241 hashval = (*s + 31 * hashval); 242 return hashval % n; 243} 244 --- 14 unchanged lines hidden (view full) --- 259 np[nh] = cp; 260 } 261 } 262 free(tp->tab); 263 tp->tab = np; 264 tp->size = nsz; 265} 266 | 237{ 238 unsigned hashval; 239 240 for (hashval = 0; *s != '\0'; s++) 241 hashval = (*s + 31 * hashval); 242 return hashval % n; 243} 244 --- 14 unchanged lines hidden (view full) --- 259 np[nh] = cp; 260 } 261 } 262 free(tp->tab); 263 tp->tab = np; 264 tp->size = nsz; 265} 266 |
267Cell *lookup(char *s, Array *tp) /* look for s in tp */ | 267Cell *lookup(const char *s, Array *tp) /* look for s in tp */ |
268{ 269 Cell *p; 270 int h; 271 272 h = hash(s, tp->size); 273 for (p = tp->tab[h]; p != NULL; p = p->cnext) 274 if (strcmp(s, p->nval) == 0) 275 return(p); /* found it */ --- 15 unchanged lines hidden (view full) --- 291 } else if (isrec(vp)) { 292 donefld = 0; /* mark $1... invalid */ 293 donerec = 1; 294 } 295 if (freeable(vp)) 296 xfree(vp->sval); /* free any previous string */ 297 vp->tval &= ~STR; /* mark string invalid */ 298 vp->tval |= NUM; /* mark number ok */ | 268{ 269 Cell *p; 270 int h; 271 272 h = hash(s, tp->size); 273 for (p = tp->tab[h]; p != NULL; p = p->cnext) 274 if (strcmp(s, p->nval) == 0) 275 return(p); /* found it */ --- 15 unchanged lines hidden (view full) --- 291 } else if (isrec(vp)) { 292 donefld = 0; /* mark $1... invalid */ 293 donerec = 1; 294 } 295 if (freeable(vp)) 296 xfree(vp->sval); /* free any previous string */ 297 vp->tval &= ~STR; /* mark string invalid */ 298 vp->tval |= NUM; /* mark number ok */ |
299 dprintf( ("setfval %p: %s = %g, t=%o\n", vp, vp->nval, f, vp->tval) ); | 299 dprintf( ("setfval %p: %s = %g, t=%o\n", vp, NN(vp->nval), f, vp->tval) ); |
300 return vp->fval = f; 301} 302 | 300 return vp->fval = f; 301} 302 |
303void funnyvar(Cell *vp, char *rw) | 303void funnyvar(Cell *vp, const char *rw) |
304{ 305 if (isarr(vp)) 306 FATAL("can't %s %s; it's an array name.", rw, vp->nval); 307 if (vp->tval & FCN) 308 FATAL("can't %s %s; it's a function.", rw, vp->nval); 309 WARNING("funny variable %p: n=%s s=\"%s\" f=%g t=%o", 310 vp, vp->nval, vp->sval, vp->fval, vp->tval); 311} 312 | 304{ 305 if (isarr(vp)) 306 FATAL("can't %s %s; it's an array name.", rw, vp->nval); 307 if (vp->tval & FCN) 308 FATAL("can't %s %s; it's a function.", rw, vp->nval); 309 WARNING("funny variable %p: n=%s s=\"%s\" f=%g t=%o", 310 vp, vp->nval, vp->sval, vp->fval, vp->tval); 311} 312 |
313char *setsval(Cell *vp, char *s) /* set string val of a Cell */ | 313char *setsval(Cell *vp, const char *s) /* set string val of a Cell */ |
314{ 315 char *t; 316 int fldno; 317 | 314{ 315 char *t; 316 int fldno; 317 |
318 dprintf( ("starting setsval %p: %s = \"%s\", t=%o\n", vp, vp->nval, s, vp->tval) ); | 318 dprintf( ("starting setsval %p: %s = \"%s\", t=%o\n", vp, NN(vp->nval), s, vp->tval) ); |
319 if ((vp->tval & (NUM | STR)) == 0) 320 funnyvar(vp, "assign to"); 321 if (isfld(vp)) { 322 donerec = 0; /* mark $0 invalid */ 323 fldno = atoi(vp->nval); 324 if (fldno > *NF) 325 newfld(fldno); 326 dprintf( ("setting field %d to %s (%p)\n", fldno, s, s) ); 327 } else if (isrec(vp)) { 328 donefld = 0; /* mark $1... invalid */ 329 donerec = 1; 330 } 331 t = tostring(s); /* in case it's self-assign */ 332 vp->tval &= ~NUM; 333 vp->tval |= STR; 334 if (freeable(vp)) 335 xfree(vp->sval); 336 vp->tval &= ~DONTFREE; | 319 if ((vp->tval & (NUM | STR)) == 0) 320 funnyvar(vp, "assign to"); 321 if (isfld(vp)) { 322 donerec = 0; /* mark $0 invalid */ 323 fldno = atoi(vp->nval); 324 if (fldno > *NF) 325 newfld(fldno); 326 dprintf( ("setting field %d to %s (%p)\n", fldno, s, s) ); 327 } else if (isrec(vp)) { 328 donefld = 0; /* mark $1... invalid */ 329 donerec = 1; 330 } 331 t = tostring(s); /* in case it's self-assign */ 332 vp->tval &= ~NUM; 333 vp->tval |= STR; 334 if (freeable(vp)) 335 xfree(vp->sval); 336 vp->tval &= ~DONTFREE; |
337 dprintf( ("setsval %p: %s = \"%s (%p)\", t=%o\n", vp, vp->nval, t,t, vp->tval) ); | 337 dprintf( ("setsval %p: %s = \"%s (%p)\", t=%o\n", vp, NN(vp->nval), t,t, vp->tval) ); |
338 return(vp->sval = t); 339} 340 341Awkfloat getfval(Cell *vp) /* get float val of a Cell */ 342{ 343 if ((vp->tval & (NUM | STR)) == 0) 344 funnyvar(vp, "read value of"); 345 if (isfld(vp) && donefld == 0) 346 fldbld(); 347 else if (isrec(vp) && donerec == 0) 348 recbld(); 349 if (!isnum(vp)) { /* not a number */ 350 vp->fval = atof(vp->sval); /* best guess */ 351 if (is_number(vp->sval) && !(vp->tval&CON)) 352 vp->tval |= NUM; /* make NUM only sparingly */ 353 } | 338 return(vp->sval = t); 339} 340 341Awkfloat getfval(Cell *vp) /* get float val of a Cell */ 342{ 343 if ((vp->tval & (NUM | STR)) == 0) 344 funnyvar(vp, "read value of"); 345 if (isfld(vp) && donefld == 0) 346 fldbld(); 347 else if (isrec(vp) && donerec == 0) 348 recbld(); 349 if (!isnum(vp)) { /* not a number */ 350 vp->fval = atof(vp->sval); /* best guess */ 351 if (is_number(vp->sval) && !(vp->tval&CON)) 352 vp->tval |= NUM; /* make NUM only sparingly */ 353 } |
354 dprintf( ("getfval %p: %s = %g, t=%o\n", vp, vp->nval, vp->fval, vp->tval) ); | 354 dprintf( ("getfval %p: %s = %g, t=%o\n", vp, NN(vp->nval), vp->fval, vp->tval) ); |
355 return(vp->fval); 356} 357 | 355 return(vp->fval); 356} 357 |
358char *getsval(Cell *vp) /* get string val of a Cell */ | 358 static char *get_str_val(Cell *vp, char **fmt) /* get string val of a Cell */ 359 |
359{ 360 char s[100]; /* BUG: unchecked */ 361 double dtemp; 362 363 if ((vp->tval & (NUM | STR)) == 0) 364 funnyvar(vp, "read value of"); 365 if (isfld(vp) && donefld == 0) 366 fldbld(); 367 else if (isrec(vp) && donerec == 0) 368 recbld(); 369 if (isstr(vp) == 0) { 370 if (freeable(vp)) 371 xfree(vp->sval); 372 if (modf(vp->fval, &dtemp) == 0) /* it's integral */ 373 sprintf(s, "%.30g", vp->fval); 374 else | 360{ 361 char s[100]; /* BUG: unchecked */ 362 double dtemp; 363 364 if ((vp->tval & (NUM | STR)) == 0) 365 funnyvar(vp, "read value of"); 366 if (isfld(vp) && donefld == 0) 367 fldbld(); 368 else if (isrec(vp) && donerec == 0) 369 recbld(); 370 if (isstr(vp) == 0) { 371 if (freeable(vp)) 372 xfree(vp->sval); 373 if (modf(vp->fval, &dtemp) == 0) /* it's integral */ 374 sprintf(s, "%.30g", vp->fval); 375 else |
375 sprintf(s, *CONVFMT, vp->fval); | 376 sprintf(s, *fmt, vp->fval); |
376 vp->sval = tostring(s); 377 vp->tval &= ~DONTFREE; 378 vp->tval |= STR; 379 } | 377 vp->sval = tostring(s); 378 vp->tval &= ~DONTFREE; 379 vp->tval |= STR; 380 } |
380 dprintf( ("getsval %p: %s = \"%s (%p)\", t=%o\n", vp, vp->nval, vp->sval, vp->sval, vp->tval) ); | 381 dprintf( ("getsval %p: %s = \"%s (%p)\", t=%o\n", vp, NN(vp->nval), vp->sval, vp->sval, vp->tval) ); |
381 return(vp->sval); 382} 383 | 382 return(vp->sval); 383} 384 |
384char *tostring(char *s) /* make a copy of string s */ | 385char *getsval(Cell *vp) /* get string val of a Cell */ |
385{ | 386{ |
387 return get_str_val(vp, CONVFMT); 388} 389 390char *getpssval(Cell *vp) /* get string val of a Cell for print */ 391{ 392 return get_str_val(vp, OFMT); 393} 394 395 396char *tostring(const char *s) /* make a copy of string s */ 397{ |
|
386 char *p; 387 388 p = (char *) malloc(strlen(s)+1); 389 if (p == NULL) 390 FATAL("out of space in tostring on %s", s); 391 strcpy(p, s); 392 return(p); 393} 394 | 398 char *p; 399 400 p = (char *) malloc(strlen(s)+1); 401 if (p == NULL) 402 FATAL("out of space in tostring on %s", s); 403 strcpy(p, s); 404 return(p); 405} 406 |
395char *qstring(char *is, int delim) /* collect string up to next delim */ | 407char *qstring(const char *is, int delim) /* collect string up to next delim */ |
396{ | 408{ |
397 char *os = is; | 409 const char *os = is; |
398 int c, n; 399 uschar *s = (uschar *) is; 400 uschar *buf, *bp; 401 402 if ((buf = (uschar *) malloc(strlen(is)+3)) == NULL) 403 FATAL( "out of space in qstring(%s)", s); 404 for (bp = buf; (c = *s) != delim; s++) { 405 if (c == '\n') --- 35 unchanged lines hidden --- | 410 int c, n; 411 uschar *s = (uschar *) is; 412 uschar *buf, *bp; 413 414 if ((buf = (uschar *) malloc(strlen(is)+3)) == NULL) 415 FATAL( "out of space in qstring(%s)", s); 416 for (bp = buf; (c = *s) != delim; s++) { 417 if (c == '\n') --- 35 unchanged lines hidden --- |