Cell *execute(Node *u) /* execute a node of the parse tree */ { Cell *(*proc)(Node **, int); Cell *x; Node *a; if (u == NULL) return(true); for (a = u; ; a = a->nnext) { curnode = a; if (isvalue(a)) { x = (Cell *) (a->narg[0]); if ((x->tval & FLD) && !donefld) fldbld(); else if ((x->tval & REC) && !donerec) recbld(); return(x); } if (notlegal(a->nobj)) /* probably a Cell* but too risky to print */ ERROR "illegal statement" FATAL; proc = proctab[a->nobj-FIRSTTOKEN]; x = (*proc)(a->narg, a->nobj); if ((x->tval & FLD) && !donefld) fldbld(); else if ((x->tval & REC) && !donerec) recbld(); if (isexpr(a)) return(x); if (isjump(x)) return(x); if (a->nnext == NULL) return(x); tempfree(x); } }
static char *get_str_val(Cell *vp, char **fmt) /* get string val of a Cell */ { char s[100]; /* BUG: unchecked */ double dtemp; if ((vp->tval & (NUM | STR)) == 0) funnyvar(vp, "read value of"); if (isfld(vp) && donefld == 0) fldbld(); else if (isrec(vp) && donerec == 0) recbld(); if (isstr(vp) == 0) { if (freeable(vp)) xfree(vp->sval); if (modf(vp->fval, &dtemp) == 0) /* it's integral */ sprintf(s, "%.30g", vp->fval); else sprintf(s, *fmt, vp->fval); vp->sval = tostring(s); vp->tval &= ~DONTFREE; vp->tval |= STR; } dprintf( ("getsval %p: %s = \"%s (%p)\", t=%o\n", (void*)vp, NN(vp->nval), vp->sval, vp->sval, vp->tval) ); return(vp->sval); }
uchar *r_getsval(Cell *vp) { uchar s[100]; double dtemp; /* if (vp->tval & ARR) ERROR "illegal reference to array %s", vp->nval FATAL; return ""; */ if ((vp->tval & (NUM | STR)) == 0) funnyvar(vp, "read value of"); if ((vp->tval & FLD) && donefld == 0) fldbld(); else if ((vp->tval & REC) && donerec == 0) recbld(); if ((vp->tval & STR) == 0) { if (!(vp->tval&DONTFREE)) xfree(vp->sval); if (modf(vp->fval, &dtemp) == 0) /* it's integral */ sprintf((char *)s, "%.20g", vp->fval); else sprintf((char *)s, (char *)*OFMT, vp->fval); vp->sval = tostring(s); vp->tval &= ~DONTFREE; vp->tval |= STR; } dprintf( ("getsval %o: %s = \"%s\", t=%o\n", vp, vp->nval, vp->sval, vp->tval) ); return(vp->sval); }
uchar *r_getsval(Cell *vp) /* get string val of a Cell */ { uchar s[100]; double dtemp; if ((vp->tval & (NUM | STR)) == 0) funnyvar(vp, "read value of"); if ((vp->tval & FLD) && donefld == 0) fldbld(); else if ((vp->tval & REC) && donerec == 0) recbld(); if ((vp->tval & STR) == 0) { if (!(vp->tval&DONTFREE)) xfree(vp->sval); if (modf(vp->fval, &dtemp) == 0) /* it's integral */ sprintf((char *)s, "%.20g", vp->fval); else sprintf((char *)s, (char *)*CONVFMT, vp->fval); vp->sval = tostring(s); vp->tval &= ~DONTFREE; vp->tval |= STR; } dprintf( ("getsval %o: %s = \"%s\", t=%o\n", vp, vp->nval, vp->sval, vp->tval) ); return(vp->sval); }
getrec() { register char *rr; extern int svargc; extern char **svargv; register c, sep; dprintf("**RS=%o, **FS=%o\n", **RS, **FS, NULL); donefld = 0; donerec = 1; record[0] = 0; while (svargc > 0) { dprintf("svargc=%d, *svargv=%s\n", svargc, *svargv, NULL); if (infile == NULL) { /* have to open a new file */ if (member('=', *svargv)) { /* it's a var=value argument */ setclvar(*svargv); svargv++; svargc--; continue; } *FILENAME = file = *svargv; dprintf("opening file %s\n", file, NULL, NULL); if (*file == '-') infile = stdin; else if ((infile = fopen(file, "r")) == NULL) error(FATAL, "can't open %s", file); } if ((sep = **RS) == 0) sep = '\n'; for (rr = record; ; ) { for (; (c=getc(infile)) != sep && c != EOF; *rr++ = c) ; if (**RS == sep || c == EOF) break; if ((c = getc(infile)) == '\n' || c == EOF) /* 2 in a row */ break; *rr++ = '\n'; *rr++ = c; } if (rr > record+RECSIZE) error(FATAL, "record `%.20s...' too long", record); *rr = 0; if (mustfld) fldbld(); if (c != EOF) /* normal record */ return(1); /* EOF arrived on this file; set up next */ if (infile != stdin) fclose(infile); infile = NULL; svargc--; svargv++; } return(0); /* true end of file */ }
Awkfloat getfval(Cell *vp) /* get float val of a Cell */ { if ((vp->tval & (NUM | STR)) == 0) funnyvar(vp, "read value of"); if (isfld(vp) && donefld == 0) fldbld(); else if (isrec(vp) && donerec == 0) recbld(); if (!isnum(vp)) { /* not a number */ vp->fval = atof(vp->sval); /* best guess */ if (is_number(vp->sval) && !(vp->tval&CON)) vp->tval |= NUM; /* make NUM only sparingly */ } dprintf( ("getfval %p: %s = %g, t=%o\n", vp, NN(vp->nval), vp->fval, vp->tval) ); return(vp->fval); }
Awkfloat r_getfval(Cell *vp) { /* if (vp->tval & ARR) ERROR "illegal reference to array %s", vp->nval FATAL; return 0.0; */ if ((vp->tval & (NUM | STR)) == 0) funnyvar(vp, "read value of"); if ((vp->tval & FLD) && donefld == 0) fldbld(); else if ((vp->tval & REC) && donerec == 0) recbld(); if (!isnum(vp)) { /* not a number */ vp->fval = atof(vp->sval); /* best guess */ if (isnumber(vp->sval) && !(vp->tval&CON)) vp->tval |= NUM; /* make NUM only sparingly */ } dprintf( ("getfval %o: %s = %g, t=%o\n", vp, vp->nval, vp->fval, vp->tval) ); return(vp->fval); }
int getrec(void) { register char *rr; extern int svargc; extern char **svargv; register int c, sep, k, m, n; wchar_t wc; dprintf("**RS=%o, **FS=%o\n", **RS, **FS); donefld = 0; donerec = 1; record[0] = 0; while (svargc > 0) { dprintf("svargc=%d, *svargv=%s\n", svargc, *svargv); if (infile == NULL) { /* have to open a new file */ if (member('=', *svargv)) { /* it's a var=value argument */ setclvar(*svargv); svargv++; svargc--; continue; } *FILENAME = file = *svargv; dprintf("opening file %s\n", file); if (*file == '-') { if (yyin == stdin && ! lexprog) error(FATAL, "standard input already used for reading commands"); else infile = stdin; } else if ((infile = fopen(file, "r")) == NULL) error(FATAL, "can't open %s", file); } next(wc, *RS, n); if ((sep = **RS) == 0) sep = '\n'; for (rr = record; ; ) { cont: for (; (c=getc(infile)) != sep && c != EOF; *rr++ = c) { if (rr >= record+RECSIZE-n-3) { size_t diff = rr - record; growrec(); rr = &record[diff]; } } if (c != EOF) { /* * Note: This code does not restrict occurences * of the multibyte sequence in RS to the start * of an input character. */ for (m = 1; m < n; m++) { if ((c = getc(infile)) == EOF || c != (*RS)[m]) { for (k = 0; k < m; k++) *rr++ = (*RS)[k]; if (c == EOF) break; *rr++ = c; goto cont; } } } if (**RS == sep || c == EOF) break; if ((c = getc(infile)) == '\n' || c == EOF) /* 2 in a row */ break; if (rr >= record+RECSIZE-n-3) { size_t diff = rr - record; growrec(); rr = &record[diff]; } *rr++ = '\n'; *rr++ = c; } *rr = 0; if (mustfld) fldbld(); if (c != EOF || rr > record) { /* normal record */ recloc->tval &= ~NUM; recloc->tval |= STR; ++nrloc->fval; nrloc->tval &= ~STR; nrloc->tval |= NUM; return(1); } /* EOF arrived on this file; set up next */ if (infile != stdin) fclose(infile); infile = NULL; svargc--; svargv++; } return(0); /* true end of file */ }
Cell *getnf(Node **a, int n) /* get NF */ { if (donefld == 0) fldbld(); return (Cell *) a[0]; }
int getrec(char **pbuf, int *pbufsize, int isrecord) /* get next input record */ { /* note: cares whether buf == record */ int c; char *buf = *pbuf; uschar saveb0; int bufsize = *pbufsize, savebufsize = bufsize; if (awk_firsttime) { awk_firsttime = 0; initgetrec(); } dprintf( ("RS=<%s>, FS=<%s>, ARGC=%g, FILENAME=%s\n", *RS, *FS, *ARGC, *FILENAME) ); if (isrecord) { donefld = 0; donerec = 1; } saveb0 = buf[0]; buf[0] = 0; while (argno < *ARGC || infile == stdin) { dprintf( ("argno=%d, file=|%s|\n", argno, file) ); if (infile == NULL) { /* have to open a new file */ file = getargv(argno); if (*file == '\0') { /* it's been zapped */ argno++; continue; } if (isclvar(file)) { /* a var=value arg */ setclvar(file); argno++; continue; } *FILENAME = file; dprintf( ("opening file %s\n", file) ); if (*file == '-' && *(file+1) == '\0') infile = stdin; else if ((infile = fopen(file, "r")) == NULL) FATAL("can't open file %s", file); setfval(fnrloc, 0.0); } c = readrec(&buf, &bufsize, infile); if (c != 0 || buf[0] != '\0') { /* normal record */ if (isrecord) { if (freeable(fldtab[0])) xfree(fldtab[0]->sval); fldtab[0]->sval = buf; /* buf == record */ fldtab[0]->tval = REC | STR | DONTFREE; if (is_number(fldtab[0]->sval)) { fldtab[0]->fval = atof(fldtab[0]->sval); fldtab[0]->tval |= NUM; } } setfval(nrloc, nrloc->fval+1); setfval(fnrloc, fnrloc->fval+1); if (donefld == 0) fldbld(); *pbuf = buf; *pbufsize = bufsize; return 1; } /* EOF arrived on this file; set up next */ if (infile != stdin) fclose(infile); infile = NULL; argno++; } buf[0] = saveb0; *pbuf = buf; *pbufsize = savebufsize; return 0; /* true end of file */ }