diff options
author | Cody Peter Mello <cody.mello@joyent.com> | 2018-10-10 22:54:42 +0000 |
---|---|---|
committer | Cody Peter Mello <cody.mello@joyent.com> | 2019-06-13 18:47:50 +0000 |
commit | d0ca0fd545f68d9afab9400f4b46c79e6fedbce9 (patch) | |
tree | 7a0732421c19c725ffe054d079473bc61f754808 /usr/src/cmd/awk/run.c | |
parent | 486c29ab370b325b6cf8d9bbe8e4bc5bac22baeb (diff) | |
download | illumos-joyent-d0ca0fd545f68d9afab9400f4b46c79e6fedbce9.tar.gz |
OS-7209 Copy minor upstream nawk(1) code changes
Reviewed by: Robert Mustacchi <rm@joyent.com>
Approved by: Jason King <jbk@joyent.com>
Diffstat (limited to 'usr/src/cmd/awk/run.c')
-rw-r--r-- | usr/src/cmd/awk/run.c | 1076 |
1 files changed, 581 insertions, 495 deletions
diff --git a/usr/src/cmd/awk/run.c b/usr/src/cmd/awk/run.c index 3cc8341d84..29529bdb84 100644 --- a/usr/src/cmd/awk/run.c +++ b/usr/src/cmd/awk/run.c @@ -1,4 +1,28 @@ /* + * Copyright (C) Lucent Technologies 1997 + * All Rights Reserved + * + * Permission to use, copy, modify, and distribute this software and + * its documentation for any purpose and without fee is hereby + * granted, provided that the above copyright notice appear in all + * copies and that both that the copyright notice and this + * permission notice and warranty disclaimer appear in supporting + * documentation, and that the name Lucent Technologies or any of + * its entities not be used in advertising or publicity pertaining + * to distribution of the software without specific, written prior + * permission. + * + * LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. + * IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY + * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER + * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, + * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF + * THIS SOFTWARE. + */ + +/* * CDDL HEADER START * * The contents of this file are subject to the terms of the @@ -26,18 +50,16 @@ /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ /* All Rights Reserved */ -#define tempfree(x, s) if (istemp(x)) tfree(x, s) - -#define execute(p) r_execute(p) - #define DEBUG -#include "awk.h" -#include <math.h> -#include "y.tab.h" #include <stdio.h> #include <ctype.h> #include <setjmp.h> +#include <math.h> #include <time.h> +#include "awk.h" +#include "y.tab.h" + +#define tempfree(x) if (istemp(x)) tfree(x) #ifndef FOPEN_MAX #define FOPEN_MAX 15 /* max number of open files, from ANSI std. */ @@ -46,19 +68,18 @@ static jmp_buf env; -static Cell *r_execute(Node *); -static Cell *gettemp(char *), *copycell(Cell *); -static FILE *openfile(int, uchar *), *redirect(int, Node *); +static Cell *execute(Node *); +static Cell *gettemp(void), *copycell(Cell *); +static FILE *openfile(int, const char *), *redirect(int, Node *); -int paircnt; -Node *winner = NULL; +Node *winner = NULL; /* root of parse tree */ -static Cell *tmps; +static Cell *tmps; /* free temporary cells for execution */ static Cell truecell = { OBOOL, BTRUE, 0, 0, 1.0, NUM }; -Cell *true = &truecell; +Cell *True = &truecell; static Cell falsecell = { OBOOL, BFALSE, 0, 0, 0.0, NUM }; -Cell *false = &falsecell; +Cell *False = &falsecell; static Cell breakcell = { OJUMP, JBREAK, 0, 0, 0.0, NUM }; Cell *jbreak = &breakcell; static Cell contcell = { OJUMP, JCONT, 0, 0, 0.0, NUM }; @@ -73,131 +94,173 @@ static Cell tempcell = { OCELL, CTEMP, 0, 0, 0.0, NUM }; Node *curnode = NULL; /* the node being executed, for debugging */ -static void tfree(Cell *, char *); +static void tfree(Cell *); static void closeall(void); static double ipow(double, int); +/* + * buffer memory management + * + * pbuf: address of pointer to buffer being managed + * psiz: address of buffer size variable + * minlen: minimum length of buffer needed + * quantum: buffer size quantum + * pbptr: address of movable pointer into buffer, or 0 if none + * whatrtn: name of the calling routine if failure should cause fatal error + * + * return 0 for realloc failure, !=0 for success + */ +int +adjbuf(char **pbuf, size_t *psiz, size_t minlen, size_t quantum, char **pbptr, + const char *whatrtn) +{ + if (minlen > *psiz) { + char *tbuf; + int rminlen = quantum ? minlen % quantum : 0; + int boff = pbptr ? *pbptr - *pbuf : 0; + /* round up to next multiple of quantum */ + if (rminlen) + minlen += quantum - rminlen; + tbuf = (char *)realloc(*pbuf, minlen); + dprintf(("adjbuf %s: %d %d (pbuf=%p, tbuf=%p)\n", whatrtn, + *psiz, minlen, (void *)*pbuf, (void *)tbuf)); + if (tbuf == NULL) { + if (whatrtn) + FATAL("out of memory in %s", whatrtn); + return (0); + } + *pbuf = tbuf; + *psiz = minlen; + if (pbptr) + *pbptr = tbuf + boff; + } + return (1); +} + void -run(Node *a) +run(Node *a) /* execution of parse tree starts here */ { (void) execute(a); closeall(); } static Cell * -r_execute(Node *u) +execute(Node *u) /* execute a node of the parse tree */ { - register Cell *(*proc)(); - register Cell *x; - register Node *a; + Cell *(*proc)(Node **, int); + Cell *x; + Node *a; if (u == NULL) - return (true); + return (True); for (a = u; ; a = a->nnext) { curnode = a; if (isvalue(a)) { x = (Cell *) (a->narg[0]); - if ((x->tval & FLD) && !donefld) + if (isfld(x) && !donefld) fldbld(); - else if ((x->tval & REC) && !donerec) + else if (isrec(x) && !donerec) recbld(); return (x); } /* probably a Cell* but too risky to print */ if (notlegal(a->nobj)) - ERROR "illegal statement" FATAL; + FATAL("illegal statement"); proc = proctab[a->nobj-FIRSTTOKEN]; x = (*proc)(a->narg, a->nobj); - if ((x->tval & FLD) && !donefld) + if (isfld(x) && !donefld) fldbld(); - else if ((x->tval & REC) && !donerec) + else if (isrec(x) && !donerec) recbld(); if (isexpr(a)) return (x); /* a statement, goto next statement */ if (isjump(x)) return (x); - if (a->nnext == (Node *)NULL) + if (a->nnext == NULL) return (x); - tempfree(x, "execute"); + tempfree(x); } } +/* execute an awk program */ +/* a[0] = BEGIN, a[1] = body, a[2] = END */ /*ARGSUSED*/ Cell * program(Node **a, int n) { - register Cell *x; + Cell *x; if (setjmp(env) != 0) goto ex; if (a[0]) { /* BEGIN */ x = execute(a[0]); if (isexit(x)) - return (true); + return (True); if (isjump(x)) { - ERROR "illegal break, continue or next from BEGIN" - FATAL; + FATAL("illegal break, continue, or next from BEGIN"); } - tempfree(x, ""); + tempfree(x); } loop: if (a[1] || a[2]) - while (getrec(&record, &record_size) > 0) { + while (getrec(&record, &recsize) > 0) { x = execute(a[1]); if (isexit(x)) break; - tempfree(x, ""); + tempfree(x); } ex: - if (setjmp(env) != 0) + if (setjmp(env) != 0) /* handles exit within END */ goto ex1; if (a[2]) { /* END */ x = execute(a[2]); if (iscont(x)) /* read some more */ goto loop; if (isbreak(x) || isnext(x)) - ERROR "illegal break or next from END" FATAL; - tempfree(x, ""); + FATAL("illegal break or next from END"); + tempfree(x); } ex1: - return (true); + return (True); } -struct Frame { +struct Frame { /* stack frame for awk function calls */ int nargs; /* number of arguments in this call */ Cell *fcncell; /* pointer to Cell for function */ Cell **args; /* pointer to array of arguments after execute */ Cell *retval; /* return value */ }; -#define NARGS 30 +#define NARGS 30 /* max args in a call */ -struct Frame *frame = NULL; /* base of stack frames; dynamically allocated */ +struct Frame *frame = NULL; /* base of stack frames; dynamically alloc'd */ int nframe = 0; /* number of frames allocated */ struct Frame *fp = NULL; /* frame pointer. bottom level unused */ /*ARGSUSED*/ Cell * -call(Node **a, int n) +call(Node **a, int n) /* function call. very kludgy and fragile */ { static Cell newcopycell = - { OCELL, CCOPY, 0, (uchar *) "", 0.0, NUM|STR|DONTFREE }; - int i, ncall, ndef, freed = 0; + { OCELL, CCOPY, 0, "", 0.0, NUM|STR|DONTFREE }; + int i, ncall, ndef; + /* handles potential double freeing when fcn & param share a tempcell */ + int freed = 0; Node *x; - Cell *args[NARGS], *oargs[NARGS], *y, *z, *fcn; - uchar *s; + Cell *args[NARGS], *oargs[NARGS]; /* BUG: fixed size arrays */ + Cell *y, *z, *fcn; + char *s; fcn = execute(a[0]); /* the function itself */ s = fcn->nval; - if (!isfunc(fcn)) - ERROR "calling undefined function %s", s FATAL; + if (!isfcn(fcn)) + FATAL("calling undefined function %s", s); if (frame == NULL) { fp = frame = (struct Frame *)calloc(nframe += 100, sizeof (struct Frame)); if (frame == NULL) { - ERROR "out of space for stack frames calling %s", - s FATAL; + FATAL("out of space for stack frames calling %s", s); } } for (ncall = 0, x = a[1]; x != NULL; x = x->nnext) /* args in call */ @@ -206,12 +269,12 @@ call(Node **a, int n) dprintf(("calling %s, %d args (%d in defn), fp=%d\n", s, ncall, ndef, fp-frame)); if (ncall > ndef) { - ERROR "function %s called with %d args, uses only %d", - s, ncall, ndef WARNING; + WARNING("function %s called with %d args, uses only %d", + s, ncall, ndef); } if (ncall + ndef > NARGS) { - ERROR "function %s has %d arguments, limit %d", - s, ncall+ndef, NARGS FATAL; + FATAL("function %s has %d arguments, limit %d", + s, ncall+ndef, NARGS); } for (i = 0, x = a[1]; x != NULL; i++, x = x->nnext) { /* get call args */ @@ -219,20 +282,20 @@ call(Node **a, int n) y = execute(x); oargs[i] = y; dprintf(("args[%d]: %s %f <%s>, t=%o\n", - i, y->nval, y->fval, - isarr(y) ? "(array)" : (char *)y->sval, y->tval)); - if (isfunc(y)) { - ERROR "can't use function %s as argument in %s", - y->nval, s FATAL; + i, NN(y->nval), y->fval, + isarr(y) ? "(array)" : NN(y->sval), y->tval)); + if (isfcn(y)) { + FATAL("can't use function %s as argument in %s", + y->nval, s); } if (isarr(y)) args[i] = y; /* arrays by ref */ else args[i] = copycell(y); - tempfree(y, "callargs"); + tempfree(y); } - for (; i < ndef; i++) { /* add null args for ones not provided */ - args[i] = gettemp("nullargs"); + for (; i < ndef; i++) { /* add null args for ones not provided */ + args[i] = gettemp(); *args[i] = newcopycell; } fp++; /* now ok to up frame */ @@ -241,13 +304,13 @@ call(Node **a, int n) frame = (struct Frame *) realloc(frame, (nframe += 100) * sizeof (struct Frame)); if (frame == NULL) - ERROR "out of space for stack frames in %s", s FATAL; + FATAL("out of space for stack frames in %s", s); fp = frame + dfp; } fp->fcncell = fcn; fp->args = args; fp->nargs = ndef; /* number defined with (excess are locals) */ - fp->retval = gettemp("retval"); + fp->retval = gettemp(); dprintf(("start exec of %s, fp=%d\n", s, fp-frame)); /*LINTED align*/ @@ -265,20 +328,21 @@ call(Node **a, int n) oargs[i]->tval = t->tval; oargs[i]->tval &= ~(STR|NUM|DONTFREE); oargs[i]->sval = t->sval; - tempfree(t, "oargsarr"); + tempfree(t); } } } else { t->csub = CTEMP; - tempfree(t, "fp->args"); + tempfree(t); if (t == y) freed = 1; } } - tempfree(fcn, "call.fcn"); + tempfree(fcn); if (isexit(y) || isnext(y)) return (y); - if (!freed) - tempfree(y, "fcn ret"); /* this can free twice! */ + if (freed == 0) { + tempfree(y); /* don't free twice! */ + } z = fp->retval; /* return value */ dprintf(("%s returns %g |%s| %o\n", s, getfval(z), getsval(z), z->tval)); @@ -291,7 +355,7 @@ copycell(Cell *x) /* make a copy of a cell in a temp */ { Cell *y; - y = gettemp("copycell"); + y = gettemp(); y->csub = CCOPY; /* prevents freeing until call is over */ y->nval = x->nval; y->sval = x->sval ? tostring(x->sval) : NULL; @@ -303,30 +367,30 @@ copycell(Cell *x) /* make a copy of a cell in a temp */ /*ARGSUSED*/ Cell * -arg(Node **a, int nnn) +arg(Node **a, int nnn) /* nth argument of a function */ { int n; - n = (int)a[0]; /* argument number, counting from 0 */ + n = ptoi(a[0]); /* argument number, counting from 0 */ dprintf(("arg(%d), fp->nargs=%d\n", n, fp->nargs)); if (n+1 > fp->nargs) { - ERROR "argument #%d of function %s was not supplied", - n+1, fp->fcncell->nval FATAL; + FATAL("argument #%d of function %s was not supplied", + n+1, fp->fcncell->nval); } return (fp->args[n]); } Cell * -jump(Node **a, int n) +jump(Node **a, int n) /* break, continue, next, nextfile, return */ { - register Cell *y; + Cell *y; switch (n) { case EXIT: if (a[0] != NULL) { y = execute(a[0]); errorflag = (int)getfval(y); - tempfree(y, ""); + tempfree(y); } longjmp(env, 1); /*NOTREACHED*/ @@ -341,7 +405,9 @@ jump(Node **a, int n) (void) setsval(fp->retval, getsval(y)); else if (y->tval & NUM) (void) setfval(fp->retval, getfval(y)); - tempfree(y, ""); + else /* can't happen */ + FATAL("bad type variable %d", y->tval); + tempfree(y); } return (jret); case NEXT: @@ -351,29 +417,31 @@ jump(Node **a, int n) case CONTINUE: return (jcont); default: /* can't happen */ - ERROR "illegal jump type %d", n FATAL; + FATAL("illegal jump type %d", n); } /*NOTREACHED*/ return (NULL); } Cell * -getaline(Node **a, int n) +awkgetline(Node **a, int n) /* get next line from specific input */ { /* a[0] is variable, a[1] is operator, a[2] is filename */ - register Cell *r, *x; - uchar *buf; + Cell *r, *x; + char *buf; FILE *fp; size_t len; + int mode; (void) fflush(stdout); /* in case someone is waiting for a prompt */ - r = gettemp(""); + r = gettemp(); if (a[1] != NULL) { /* getline < file */ x = execute(a[2]); /* filename */ - if ((int)a[1] == '|') /* input pipe */ - a[1] = (Node *)LE; /* arbitrary flag */ - fp = openfile((int)a[1], getsval(x)); - tempfree(x, ""); + mode = ptoi(a[1]); + if (mode == '|') /* input pipe */ + mode = LE; /* arbitrary flag */ + fp = openfile(mode, getsval(x)); + tempfree(x); buf = NULL; if (fp == NULL) n = -1; @@ -385,7 +453,7 @@ getaline(Node **a, int n) } else { /* getline <file */ if (!(recloc->tval & DONTFREE)) xfree(recloc->sval); - expand_buf(&record, &record_size, len); + expand_buf(&record, &recsize, len); (void) memcpy(record, buf, len); record[len] = '\0'; recloc->sval = record; @@ -397,7 +465,7 @@ getaline(Node **a, int n) free(buf); } else { /* bare getline; use current input */ if (a[0] == NULL) /* getline */ - n = getrec(&record, &record_size); + n = getrec(&record, &recsize); else { /* getline var */ init_buf(&buf, &len, LINE_INCR); n = getrec(&buf, &len); @@ -411,7 +479,7 @@ getaline(Node **a, int n) /*ARGSUSED*/ Cell * -getnf(Node **a, int n) +getnf(Node **a, int n) /* get NF */ { if (donefld == 0) fldbld(); @@ -420,23 +488,23 @@ getnf(Node **a, int n) /*ARGSUSED*/ Cell * -array(Node **a, int n) +array(Node **a, int n) /* a[0] is symtab, a[1] is list of subscripts */ { - register Cell *x, *y, *z; - register uchar *s; - register Node *np; - uchar *buf; + Cell *x, *y, *z; + char *s; + Node *np; + char *buf; size_t bsize, tlen, len, slen; x = execute(a[0]); /* Cell* for symbol table */ init_buf(&buf, &bsize, LINE_INCR); buf[0] = '\0'; tlen = 0; - slen = strlen((char *)*SUBSEP); - for (np = a[1]; np; np = np->nnext) { + slen = strlen(*SUBSEP); + for (np = a[1]; np != NULL; np = np->nnext) { y = execute(np); /* subscript */ s = getsval(y); - len = strlen((char *)s); + len = strlen(s); expand_buf(&buf, &bsize, tlen + len + slen); (void) memcpy(&buf[tlen], s, len); tlen += len; @@ -445,45 +513,45 @@ array(Node **a, int n) tlen += slen; } buf[tlen] = '\0'; - tempfree(y, ""); + tempfree(y); } if (!isarr(x)) { - dprintf(("making %s into an array\n", x->nval)); + dprintf(("making %s into an array\n", NN(x->nval))); if (freeable(x)) xfree(x->sval); x->tval &= ~(STR|NUM|DONTFREE); x->tval |= ARR; - x->sval = (uchar *) makesymtab(NSYMTAB); + x->sval = (char *)makesymtab(NSYMTAB); } /*LINTED align*/ - z = setsymtab(buf, (uchar *)"", 0.0, STR|NUM, (Array *)x->sval); + z = setsymtab(buf, "", 0.0, STR|NUM, (Array *)x->sval); z->ctype = OCELL; z->csub = CVAR; - tempfree(x, ""); + tempfree(x); free(buf); return (z); } /*ARGSUSED*/ Cell * -delete(Node **a, int n) +awkdelete(Node **a, int n) /* a[0] is symtab, a[1] is list of subscripts */ { Cell *x, *y; Node *np; - uchar *buf, *s; + char *buf, *s; size_t bsize, tlen, slen, len; x = execute(a[0]); /* Cell* for symbol table */ if (!isarr(x)) - return (true); + return (True); init_buf(&buf, &bsize, LINE_INCR); buf[0] = '\0'; tlen = 0; - slen = strlen((char *)*SUBSEP); - for (np = a[1]; np; np = np->nnext) { + slen = strlen(*SUBSEP); + for (np = a[1]; np != NULL; np = np->nnext) { y = execute(np); /* subscript */ s = getsval(y); - len = strlen((char *)s); + len = strlen(s); expand_buf(&buf, &bsize, tlen + len + slen); (void) memcpy(&buf[tlen], s, len); tlen += len; @@ -492,39 +560,39 @@ delete(Node **a, int n) tlen += slen; } buf[tlen] = '\0'; - tempfree(y, ""); + tempfree(y); } freeelem(x, buf); - tempfree(x, ""); + tempfree(x); free(buf); - return (true); + return (True); } /*ARGSUSED*/ Cell * -intest(Node **a, int n) +intest(Node **a, int n) /* a[0] is index (list), a[1] is symtab */ { - register Cell *x, *ap, *k; + Cell *x, *ap, *k; Node *p; - uchar *buf; - uchar *s; + char *buf; + char *s; size_t bsize, tlen, slen, len; ap = execute(a[1]); /* array name */ if (!isarr(ap)) - ERROR "%s is not an array", ap->nval FATAL; + FATAL("%s is not an array", ap->nval); init_buf(&buf, &bsize, LINE_INCR); - buf[0] = 0; + buf[0] = '\0'; tlen = 0; - slen = strlen((char *)*SUBSEP); - for (p = a[0]; p; p = p->nnext) { + slen = strlen(*SUBSEP); + for (p = a[0]; p != NULL; p = p->nnext) { x = execute(p); /* expr */ s = getsval(x); - len = strlen((char *)s); + len = strlen(s); expand_buf(&buf, &bsize, tlen + len + slen); (void) memcpy(&buf[tlen], s, len); tlen += len; - tempfree(x, ""); + tempfree(x); if (p->nnext) { (void) memcpy(&buf[tlen], *SUBSEP, slen); tlen += slen; @@ -533,95 +601,95 @@ intest(Node **a, int n) } /*LINTED align*/ k = lookup(buf, (Array *)ap->sval); - tempfree(ap, ""); + tempfree(ap); free(buf); if (k == NULL) - return (false); + return (False); else - return (true); + return (True); } Cell * -matchop(Node **a, int n) +matchop(Node **a, int n) /* ~ and match() */ { - register Cell *x, *y; - register uchar *s, *t; - register int i; + Cell *x, *y; + char *s, *t; + int i; fa *pfa; - int (*mf)() = match, mode = 0; + int (*mf)(fa *, const char *) = match, mode = 0; if (n == MATCHFCN) { mf = pmatch; mode = 1; } - x = execute(a[1]); + x = execute(a[1]); /* a[1] = target text */ s = getsval(x); - if (a[0] == 0) - i = (*mf)(a[2], s); + if (a[0] == NULL) /* a[1] == 0: already-compiled reg expr */ + i = (*mf)((fa *)a[2], s); else { - y = execute(a[2]); + y = execute(a[2]); /* a[2] = regular expr */ t = getsval(y); pfa = makedfa(t, mode); i = (*mf)(pfa, s); - tempfree(y, ""); + tempfree(y); } - tempfree(x, ""); + tempfree(x); if (n == MATCHFCN) { int start = patbeg - s + 1; if (patlen < 0) start = 0; (void) setfval(rstartloc, (Awkfloat)start); (void) setfval(rlengthloc, (Awkfloat)patlen); - x = gettemp(""); + x = gettemp(); x->tval = NUM; x->fval = start; return (x); - } else if (n == MATCH && i == 1 || n == NOTMATCH && i == 0) - return (true); + } else if ((n == MATCH && i == 1) || (n == NOTMATCH && i == 0)) + return (True); else - return (false); + return (False); } Cell * -boolop(Node **a, int n) +boolop(Node **a, int n) /* a[0] || a[1], a[0] && a[1], !a[0] */ { - register Cell *x, *y; - register int i; + Cell *x, *y; + int i; x = execute(a[0]); i = istrue(x); - tempfree(x, ""); + tempfree(x); switch (n) { case BOR: if (i) - return (true); + return (True); y = execute(a[1]); i = istrue(y); - tempfree(y, ""); - return (i ? true : false); + tempfree(y); + return (i ? True : False); case AND: if (!i) - return (false); + return (False); y = execute(a[1]); i = istrue(y); - tempfree(y, ""); - return (i ? true : false); + tempfree(y); + return (i ? True : False); case NOT: - return (i ? false : true); + return (i ? False : True); default: /* can't happen */ - ERROR "unknown boolean operator %d", n FATAL; + FATAL("unknown boolean operator %d", n); } /*NOTREACHED*/ return (NULL); } Cell * -relop(Node **a, int n) +relop(Node **a, int n) /* a[0] < a[1], etc. */ { - register int i; - register Cell *x, *y; + int i; + Cell *x, *y; Awkfloat j; x = execute(a[0]); @@ -630,102 +698,101 @@ relop(Node **a, int n) j = x->fval - y->fval; i = j < 0 ? -1: (j > 0 ? 1: 0); } else { - i = strcmp((char *)getsval(x), (char *)getsval(y)); + i = strcmp(getsval(x), getsval(y)); } - tempfree(x, ""); - tempfree(y, ""); + tempfree(x); + tempfree(y); switch (n) { - case LT: return (i < 0 ? true : false); - case LE: return (i <= 0 ? true : false); - case NE: return (i != 0 ? true : false); - case EQ: return (i == 0 ? true : false); - case GE: return (i >= 0 ? true : false); - case GT: return (i > 0 ? true : false); + case LT: return (i < 0 ? True : False); + case LE: return (i <= 0 ? True : False); + case NE: return (i != 0 ? True : False); + case EQ: return (i == 0 ? True : False); + case GE: return (i >= 0 ? True : False); + case GT: return (i > 0 ? True : False); default: /* can't happen */ - ERROR "unknown relational operator %d", n FATAL; + FATAL("unknown relational operator %d", n); } /*NOTREACHED*/ - return (false); + return (False); } static void -tfree(Cell *a, char *s) +tfree(Cell *a) /* free a tempcell */ { - if (dbg > 1) { - (void) printf("## tfree %.8s %06lo %s\n", - s, (ulong_t)a, a->sval ? a->sval : (uchar *)""); - } - if (freeable(a)) + if (freeable(a)) { + dprintf(("freeing %s %s %o\n", + NN(a->nval), NN(a->sval), a->tval)); xfree(a->sval); + } if (a == tmps) - ERROR "tempcell list is curdled" FATAL; + FATAL("tempcell list is curdled"); a->cnext = tmps; tmps = a; } static Cell * -gettemp(char *s) +gettemp(void) /* get a tempcell */ { int i; - register Cell *x; + Cell *x; if (!tmps) { tmps = (Cell *)calloc(100, sizeof (Cell)); if (!tmps) - ERROR "no space for temporaries" FATAL; + FATAL("out of space for temporaries"); for (i = 1; i < 100; i++) tmps[i-1].cnext = &tmps[i]; - tmps[i-1].cnext = 0; + tmps[i-1].cnext = NULL; } x = tmps; tmps = x->cnext; *x = tempcell; - if (dbg > 1) - (void) printf("## gtemp %.8s %06lo\n", s, (ulong_t)x); + dprintf(("gtemp %.8s %06lo\n", NN(x->nval), (ulong_t)x)); return (x); } /*ARGSUSED*/ Cell * -indirect(Node **a, int n) +indirect(Node **a, int n) /* $( a[0] ) */ { - register Cell *x; - register int m; - register uchar *s; + Cell *x; + int m; + char *s; x = execute(a[0]); m = (int)getfval(x); if (m == 0 && !is_number(s = getsval(x))) /* suspicion! */ - ERROR "illegal field $(%s)", s FATAL; - tempfree(x, ""); + FATAL("illegal field $(%s)", s); + tempfree(x); x = fieldadr(m); - x->ctype = OCELL; + x->ctype = OCELL; /* BUG? why are these needed? */ x->csub = CFLD; return (x); } /*ARGSUSED*/ Cell * -substr(Node **a, int nnn) +substr(Node **a, int nnn) /* substr(a[0], a[1], a[2]) */ { - register int k, m, n; - register uchar *s; + int k, m, n; + char *s; int temp; - register Cell *x, *y, *z; + Cell *x, *y, *z = NULL; x = execute(a[0]); y = execute(a[1]); - if (a[2] != 0) + if (a[2] != NULL) z = execute(a[2]); s = getsval(x); - k = strlen((char *)s) + 1; + k = strlen(s) + 1; if (k <= 1) { - tempfree(x, ""); - tempfree(y, ""); - if (a[2] != 0) - tempfree(z, ""); - x = gettemp(""); - (void) setsval(x, (uchar *)""); + tempfree(x); + tempfree(y); + if (a[2] != NULL) { + tempfree(z); + } + x = gettemp(); + (void) setsval(x, ""); return (x); } m = (int)getfval(y); @@ -733,10 +800,10 @@ substr(Node **a, int nnn) m = 1; else if (m > k) m = k; - tempfree(y, ""); - if (a[2] != 0) { + tempfree(y); + if (a[2] != NULL) { n = (int)getfval(z); - tempfree(z, ""); + tempfree(z); } else n = k - 1; if (n < 0) @@ -744,21 +811,21 @@ substr(Node **a, int nnn) else if (n > k - m) n = k - m; dprintf(("substr: m=%d, n=%d, s=%s\n", m, n, s)); - y = gettemp(""); + y = gettemp(); temp = s[n + m - 1]; /* with thanks to John Linderman */ s[n + m - 1] = '\0'; (void) setsval(y, s + m - 1); s[n + m - 1] = temp; - tempfree(x, ""); + tempfree(x); return (y); } /*ARGSUSED*/ Cell * -sindex(Node **a, int nnn) +sindex(Node **a, int nnn) /* index(a[0], a[1]) */ { - register Cell *x, *y, *z; - register uchar *s1, *s2, *p1, *p2, *q; + Cell *x, *y, *z; + char *s1, *s2, *p1, *p2, *q; Awkfloat v = 0.0; x = execute(a[0]); @@ -766,7 +833,7 @@ sindex(Node **a, int nnn) y = execute(a[1]); s2 = getsval(y); - z = gettemp(""); + z = gettemp(); for (p1 = s1; *p1 != '\0'; p1++) { for (q = p1, p2 = s2; *p2 != '\0' && *q == *p2; q++, p2++) ; @@ -775,20 +842,20 @@ sindex(Node **a, int nnn) break; } } - tempfree(x, ""); - tempfree(y, ""); + tempfree(x); + tempfree(y); (void) setfval(z, v); return (z); } void -format(uchar **bufp, uchar *s, Node *a) +format(char **bufp, char *s, Node *a) { - uchar *fmt; - register uchar *os; - register Cell *x; + char *fmt; + const char *os; + Cell *x; int flag = 0, len; - uchar_t *buf; + char *buf; size_t bufsize, fmtsize, cnt, tcnt, ret; init_buf(&buf, &bufsize, LINE_INCR); @@ -812,21 +879,22 @@ format(uchar **bufp, uchar *s, Node *a) fmt[tcnt++] = *s; if (*s == '\0') break; - if (isalpha(*s) && *s != 'l' && *s != 'h' && *s != 'L') + if (isalpha((uschar)*s) && + *s != 'l' && *s != 'h' && *s != 'L') break; /* the ansi panoply */ if (*s == '*') { if (a == NULL) { - ERROR - "not enough args in printf(%s) or sprintf(%s)", os, os FATAL; + FATAL("not enough args in printf(%s) " + "or sprintf(%s)", os, os); } x = execute(a); a = a->nnext; tcnt--; expand_buf(&fmt, &fmtsize, tcnt + 12); - ret = sprintf((char *)&fmt[tcnt], "%d", + ret = sprintf(&fmt[tcnt], "%d", (int)getfval(x)); tcnt += ret; - tempfree(x, ""); + tempfree(x); } } fmt[tcnt] = '\0'; @@ -858,7 +926,7 @@ format(uchar **bufp, uchar *s, Node *a) break; } if (flag == 0) { - len = strlen((char *)fmt); + len = strlen(fmt); expand_buf(&buf, &bufsize, cnt + len); (void) memcpy(&buf[cnt], fmt, len); cnt += len; @@ -866,8 +934,8 @@ format(uchar **bufp, uchar *s, Node *a) continue; } if (a == NULL) { - ERROR - "not enough args in printf(%s) or sprintf(%s)", os, os FATAL; + FATAL("not enough args in printf(%s) " + "or sprintf(%s)", os, os); } x = execute(a); a = a->nnext; @@ -878,33 +946,33 @@ format(uchar **bufp, uchar *s, Node *a) switch (flag) { case 1: /*LINTED*/ - ret = snprintf((char *)&buf[cnt], len, - (char *)fmt, getfval(x)); + ret = snprintf(&buf[cnt], len, + fmt, getfval(x)); break; case 2: /*LINTED*/ - ret = snprintf((char *)&buf[cnt], len, - (char *)fmt, (long)getfval(x)); + ret = snprintf(&buf[cnt], len, + fmt, (long)getfval(x)); break; case 3: /*LINTED*/ - ret = snprintf((char *)&buf[cnt], len, - (char *)fmt, (int)getfval(x)); + ret = snprintf(&buf[cnt], len, + fmt, (int)getfval(x)); break; case 4: /*LINTED*/ - ret = snprintf((char *)&buf[cnt], len, - (char *)fmt, getsval(x)); + ret = snprintf(&buf[cnt], len, + fmt, getsval(x)); break; case 5: if (isnum(x)) { /*LINTED*/ - ret = snprintf((char *)&buf[cnt], len, - (char *)fmt, (int)getfval(x)); + ret = snprintf(&buf[cnt], len, + fmt, (int)getfval(x)); } else { /*LINTED*/ - ret = snprintf((char *)&buf[cnt], len, - (char *)fmt, getsval(x)[0]); + ret = snprintf(&buf[cnt], len, + fmt, getsval(x)[0]); } break; default: @@ -914,12 +982,12 @@ format(uchar **bufp, uchar *s, Node *a) break; expand_buf(&buf, &bufsize, cnt + ret); } - tempfree(x, ""); + tempfree(x); cnt += ret; s++; } buf[cnt] = '\0'; - for (; a; a = a->nnext) /* evaluate any remaining args */ + for (; a != NULL; a = a->nnext) /* evaluate any remaining args */ (void) execute(a); *bufp = tostring(buf); free(buf); @@ -928,17 +996,17 @@ format(uchar **bufp, uchar *s, Node *a) /*ARGSUSED*/ Cell * -a_sprintf(Node **a, int n) +awksprintf(Node **a, int n) /* sprintf(a[0]) */ { - register Cell *x; - register Node *y; - uchar *buf; + Cell *x; + Node *y; + char *buf; y = a[0]->nnext; x = execute(a[0]); format(&buf, getsval(x), y); - tempfree(x, ""); - x = gettemp(""); + tempfree(x); + x = gettemp(); x->sval = buf; x->tval = STR; return (x); @@ -946,44 +1014,46 @@ a_sprintf(Node **a, int n) /*ARGSUSED*/ Cell * -aprintf(Node **a, int n) +awkprintf(Node **a, int n) /* printf */ { + /* a[0] is list of args, starting with format string */ + /* a[1] is redirection operator, a[2] is redirection file */ FILE *fp; - register Cell *x; - register Node *y; - uchar *buf; + Cell *x; + Node *y; + char *buf; y = a[0]->nnext; x = execute(a[0]); format(&buf, getsval(x), y); - tempfree(x, ""); - if (a[1] == NULL) - (void) fputs((char *)buf, stdout); - else { - fp = redirect((int)a[1], a[2]); - (void) fputs((char *)buf, fp); + tempfree(x); + if (a[1] == NULL) { + (void) fputs(buf, stdout); + } else { + fp = redirect(ptoi(a[1]), a[2]); + (void) fputs(buf, fp); (void) fflush(fp); } free(buf); - return (true); + return (True); } Cell * -arith(Node **a, int n) +arith(Node **a, int n) /* a[0] + a[1], etc. also -a[0] */ { - Awkfloat i, j; + Awkfloat i, j = 0; double v; - register Cell *x, *y, *z; + Cell *x, *y, *z; x = execute(a[0]); i = getfval(x); - tempfree(x, ""); + tempfree(x); if (n != UMINUS) { y = execute(a[1]); j = getfval(y); - tempfree(y, ""); + tempfree(y); } - z = gettemp(""); + z = gettemp(); switch (n) { case ADD: i += j; @@ -996,12 +1066,12 @@ arith(Node **a, int n) break; case DIVIDE: if (j == 0) - ERROR "division by zero" FATAL; + FATAL("division by zero"); i /= j; break; case MOD: if (j == 0) - ERROR "division by zero in mod" FATAL; + FATAL("division by zero in mod"); (void) modf(i/j, &v); i = i - j * v; break; @@ -1015,14 +1085,14 @@ arith(Node **a, int n) i = errcheck(pow(i, j), "pow"); break; default: /* can't happen */ - ERROR "illegal arithmetic operator %d", n FATAL; + FATAL("illegal arithmetic operator %d", n); } (void) setfval(z, i); return (z); } static double -ipow(double x, int n) +ipow(double x, int n) /* x**n. ought to be done by pow, but isn't always */ { double v; @@ -1036,10 +1106,10 @@ ipow(double x, int n) } Cell * -incrdecr(Node **a, int n) +incrdecr(Node **a, int n) /* a[0]++, etc. */ { - register Cell *x, *z; - register int k; + Cell *x, *z; + int k; Awkfloat xf; x = execute(a[0]); @@ -1049,17 +1119,19 @@ incrdecr(Node **a, int n) (void) setfval(x, xf + k); return (x); } - z = gettemp(""); + z = gettemp(); (void) setfval(z, xf); (void) setfval(x, xf + k); - tempfree(x, ""); + tempfree(x); return (z); } +/* a[0] = a[1], a[0] += a[1], etc. */ +/* this is subtle; don't muck with it. */ Cell * assign(Node **a, int n) { - register Cell *x, *y; + Cell *x, *y; Awkfloat xf, yf; double v; @@ -1070,13 +1142,13 @@ assign(Node **a, int n) (void) setsval(x, getsval(y)); x->fval = getfval(y); x->tval |= NUM; - } else if (y->tval & STR) + } else if (isstr(y)) (void) setsval(x, getsval(y)); - else if (y->tval & NUM) + else if (isnum(y)) (void) setfval(x, getfval(y)); else funnyvar(y, "read value of"); - tempfree(y, ""); + tempfree(y); return (x); } xf = getfval(x); @@ -1093,12 +1165,12 @@ assign(Node **a, int n) break; case DIVEQ: if (yf == 0) - ERROR "division by zero in /=" FATAL; + FATAL("division by zero in /="); xf /= yf; break; case MODEQ: if (yf == 0) - ERROR "division by zero in %%=" FATAL; + FATAL("division by zero in %%="); (void) modf(xf/yf, &v); xf = xf - yf * v; break; @@ -1109,55 +1181,55 @@ assign(Node **a, int n) xf = errcheck(pow(xf, yf), "pow"); break; default: - ERROR "illegal assignment operator %d", n FATAL; + FATAL("illegal assignment operator %d", n); break; } - tempfree(y, ""); + tempfree(y); (void) setfval(x, xf); return (x); } /*ARGSUSED*/ Cell * -cat(Node **a, int q) +cat(Node **a, int q) /* a[0] cat a[1] */ { - register Cell *x, *y, *z; - register int n1, n2; - register uchar *s; + Cell *x, *y, *z; + int n1, n2; + char *s; x = execute(a[0]); y = execute(a[1]); (void) getsval(x); (void) getsval(y); - n1 = strlen((char *)x->sval); - n2 = strlen((char *)y->sval); - s = (uchar *)malloc(n1 + n2 + 1); + n1 = strlen(x->sval); + n2 = strlen(y->sval); + s = (char *)malloc(n1 + n2 + 1); if (s == NULL) { - ERROR "out of space concatenating %.15s and %.15s", - x->sval, y->sval FATAL; + FATAL("out of space concatenating %.15s... and %.15s...", + x->sval, y->sval); } - (void) strcpy((char *)s, (char *)x->sval); - (void) strcpy((char *)s + n1, (char *)y->sval); - tempfree(y, ""); - z = gettemp(""); + (void) strcpy(s, x->sval); + (void) strcpy(s + n1, y->sval); + tempfree(x); + tempfree(y); + z = gettemp(); z->sval = s; z->tval = STR; - tempfree(x, ""); return (z); } /*ARGSUSED*/ Cell * -pastat(Node **a, int n) +pastat(Node **a, int n) /* a[0] { a[1] } */ { - register Cell *x; + Cell *x; - if (a[0] == 0) + if (a[0] == NULL) x = execute(a[1]); else { x = execute(a[0]); if (istrue(x)) { - tempfree(x, ""); + tempfree(x); x = execute(a[1]); } } @@ -1166,73 +1238,72 @@ pastat(Node **a, int n) /*ARGSUSED*/ Cell * -dopa2(Node **a, int n) +dopa2(Node **a, int n) /* a[0], a[1] { a[2] } */ { Cell *x; int pair; - static int *pairstack = NULL; if (!pairstack) { /* first time */ dprintf(("paircnt: %d\n", paircnt)); - pairstack = (int *)malloc(sizeof (int) * paircnt); - if (!pairstack) - ERROR "out of space in dopa2" FATAL; - (void) memset(pairstack, 0, sizeof (int) * paircnt); + pairstack = (int *)calloc(paircnt, sizeof (int)); + if (pairstack == NULL) + FATAL("out of space in dopa2"); } - pair = (int)a[3]; + pair = ptoi(a[3]); if (pairstack[pair] == 0) { x = execute(a[0]); if (istrue(x)) pairstack[pair] = 1; - tempfree(x, ""); + tempfree(x); } if (pairstack[pair] == 1) { x = execute(a[1]); if (istrue(x)) pairstack[pair] = 0; - tempfree(x, ""); + tempfree(x); x = execute(a[2]); return (x); } - return (false); + return (False); } /*ARGSUSED*/ Cell * -split(Node **a, int nnn) +split(Node **a, int nnn) /* split(a[0], a[1], a[2]); a[3] is type */ { Cell *x, *y, *ap; - register uchar *s; - register int sep; - uchar *t, temp, num[11], *fs; - int n, tempstat; + char *s; + int sep; + char *t, temp, num[50], *fs = NULL; + int n, tempstat, arg3type; y = execute(a[0]); /* source string */ s = getsval(y); - if (a[2] == 0) /* fs string */ + arg3type = ptoi(a[3]); + if (a[2] == NULL) /* fs string */ fs = *FS; - else if ((int)a[3] == STRING) { /* split(str,arr,"string") */ + else if (arg3type == STRING) { /* split(str,arr,"string") */ x = execute(a[2]); fs = getsval(x); - } else if ((int)a[3] == REGEXPR) - fs = (uchar *)"(regexpr)"; /* split(str,arr,/regexpr/) */ + } else if (arg3type == REGEXPR) + fs = "(regexpr)"; /* split(str,arr,/regexpr/) */ else - ERROR "illegal type of split()" FATAL; + FATAL("illegal type of split"); sep = *fs; ap = execute(a[1]); /* array name */ freesymtab(ap); - dprintf(("split: s=|%s|, a=%s, sep=|%s|\n", s, ap->nval, fs)); + dprintf(("split: s=|%s|, a=%s, sep=|%s|\n", s, NN(ap->nval), fs)); ap->tval &= ~STR; ap->tval |= ARR; - ap->sval = (uchar *)makesymtab(NSYMTAB); + ap->sval = (char *)makesymtab(NSYMTAB); n = 0; - if (*s != '\0' && strlen((char *)fs) > 1 || (int)a[3] == REGEXPR) { + if (*s != '\0' && (strlen(fs) > 1 || arg3type == REGEXPR)) { /* reg expr */ fa *pfa; - if ((int)a[3] == REGEXPR) { /* it's ready already */ + if (arg3type == REGEXPR) { /* it's ready already */ pfa = (fa *)a[2]; } else { pfa = makedfa(fs, 1); @@ -1242,12 +1313,12 @@ split(Node **a, int nnn) pfa->initstat = 2; do { n++; - (void) sprintf((char *)num, "%d", n); + (void) sprintf(num, "%d", n); temp = *patbeg; *patbeg = '\0'; if (is_number(s)) { (void) setsymtab(num, s, - atof((char *)s), + atof(s), /*LINTED align*/ STR|NUM, (Array *)ap->sval); } else { @@ -1259,8 +1330,8 @@ split(Node **a, int nnn) s = patbeg + patlen; if (*(patbeg+patlen-1) == 0 || *s == 0) { n++; - (void) sprintf((char *)num, "%d", n); - (void) setsymtab(num, (uchar *)"", 0.0, + (void) sprintf(num, "%d", n); + (void) setsymtab(num, "", 0.0, /*LINTED align*/ STR, (Array *)ap->sval); pfa->initstat = tempstat; @@ -1269,9 +1340,9 @@ split(Node **a, int nnn) } while (nematch(pfa, s)); } n++; - (void) sprintf((char *)num, "%d", n); + (void) sprintf(num, "%d", n); if (is_number(s)) { - (void) setsymtab(num, s, atof((char *)s), + (void) setsymtab(num, s, atof(s), /*LINTED align*/ STR|NUM, (Array *)ap->sval); } else { @@ -1284,7 +1355,7 @@ spdone: for (n = 0; ; ) { while (*s == ' ' || *s == '\t' || *s == '\n') s++; - if (*s == 0) + if (*s == '\0') break; n++; t = s; @@ -1295,9 +1366,9 @@ spdone: ; temp = *s; *s = '\0'; - (void) sprintf((char *)num, "%d", n); + (void) sprintf(num, "%d", n); if (is_number(t)) { - (void) setsymtab(num, t, atof((char *)t), + (void) setsymtab(num, t, atof(t), /*LINTED align*/ STR|NUM, (Array *)ap->sval); } else { @@ -1306,10 +1377,10 @@ spdone: STR, (Array *)ap->sval); } *s = temp; - if (*s != 0) + if (*s != '\0') s++; } - } else if (*s != 0) { + } else if (*s != '\0') { for (;;) { n++; t = s; @@ -1317,9 +1388,9 @@ spdone: s++; temp = *s; *s = '\0'; - (void) sprintf((char *)num, "%d", n); + (void) sprintf(num, "%d", n); if (is_number(t)) { - (void) setsymtab(num, t, atof((char *)t), + (void) setsymtab(num, t, atof(t), /*LINTED align*/ STR|NUM, (Array *)ap->sval); } else { @@ -1328,15 +1399,16 @@ spdone: STR, (Array *)ap->sval); } *s = temp; - if (*s++ == 0) + if (*s++ == '\0') break; } } - tempfree(ap, ""); - tempfree(y, ""); - if (a[2] != 0 && (int)a[3] == STRING) - tempfree(x, ""); - x = gettemp(""); + tempfree(ap); + tempfree(y); + if (a[2] != NULL && arg3type == STRING) { + tempfree(x); + } + x = gettemp(); x->tval = NUM; x->fval = n; return (x); @@ -1344,16 +1416,16 @@ spdone: /*ARGSUSED*/ Cell * -condexpr(Node **a, int n) +condexpr(Node **a, int n) /* a[0] ? a[1] : a[2] */ { - register Cell *x; + Cell *x; x = execute(a[0]); if (istrue(x)) { - tempfree(x, ""); + tempfree(x); x = execute(a[1]); } else { - tempfree(x, ""); + tempfree(x); x = execute(a[2]); } return (x); @@ -1361,16 +1433,16 @@ condexpr(Node **a, int n) /*ARGSUSED*/ Cell * -ifstat(Node **a, int n) +ifstat(Node **a, int n) /* if (a[0]) a[1]; else a[2] */ { - register Cell *x; + Cell *x; x = execute(a[0]); if (istrue(x)) { - tempfree(x, ""); + tempfree(x); x = execute(a[1]); - } else if (a[2] != 0) { - tempfree(x, ""); + } else if (a[2] != NULL) { + tempfree(x); x = execute(a[2]); } return (x); @@ -1378,123 +1450,124 @@ ifstat(Node **a, int n) /*ARGSUSED*/ Cell * -whilestat(Node **a, int n) +whilestat(Node **a, int n) /* while (a[0]) a[1] */ { - register Cell *x; + Cell *x; for (;;) { x = execute(a[0]); if (!istrue(x)) return (x); - tempfree(x, ""); + tempfree(x); x = execute(a[1]); if (isbreak(x)) { - x = true; + x = True; return (x); } if (isnext(x) || isexit(x) || isret(x)) return (x); - tempfree(x, ""); + tempfree(x); } } /*ARGSUSED*/ Cell * -dostat(Node **a, int n) +dostat(Node **a, int n) /* do a[0]; while(a[1]) */ { - register Cell *x; + Cell *x; for (;;) { x = execute(a[0]); if (isbreak(x)) - return (true); + return (True); if (isnext(x) || isexit(x) || isret(x)) return (x); - tempfree(x, ""); + tempfree(x); x = execute(a[1]); if (!istrue(x)) return (x); - tempfree(x, ""); + tempfree(x); } } /*ARGSUSED*/ Cell * -forstat(Node **a, int n) +forstat(Node **a, int n) /* for (a[0]; a[1]; a[2]) a[3] */ { - register Cell *x; + Cell *x; x = execute(a[0]); - tempfree(x, ""); + tempfree(x); for (;;) { - if (a[1] != 0) { + if (a[1] != NULL) { x = execute(a[1]); if (!istrue(x)) return (x); else - tempfree(x, ""); + tempfree(x); } x = execute(a[3]); if (isbreak(x)) /* turn off break */ - return (true); + return (True); if (isnext(x) || isexit(x) || isret(x)) return (x); - tempfree(x, ""); + tempfree(x); x = execute(a[2]); - tempfree(x, ""); + tempfree(x); } } /*ARGSUSED*/ Cell * -instat(Node **a, int n) +instat(Node **a, int n) /* for (a[0] in a[1]) a[2] */ { - register Cell *x, *vp, *arrayp, *cp, *ncp; + Cell *x, *vp, *arrayp, *cp, *ncp; Array *tp; int i; vp = execute(a[0]); arrayp = execute(a[1]); - if (!isarr(arrayp)) - ERROR "%s is not an array", arrayp->nval FATAL; + if (!isarr(arrayp)) { + FATAL("%s is not an array", arrayp->nval); + } /*LINTED align*/ tp = (Array *)arrayp->sval; - tempfree(arrayp, ""); + tempfree(arrayp); for (i = 0; i < tp->size; i++) { /* this routine knows too much */ for (cp = tp->tab[i]; cp != NULL; cp = ncp) { (void) setsval(vp, cp->nval); ncp = cp->cnext; x = execute(a[2]); if (isbreak(x)) { - tempfree(vp, ""); - return (true); + tempfree(vp); + return (True); } if (isnext(x) || isexit(x) || isret(x)) { - tempfree(vp, ""); + tempfree(vp); return (x); } - tempfree(x, ""); + tempfree(x); } } - return (true); + return (True); } /*ARGSUSED*/ Cell * -bltin(Node **a, int n) +bltin(Node **a, int n) /* builtin functions. a[0] is type, a[1] is arg list */ { - register Cell *x, *y; + Cell *x, *y; Awkfloat u; - register int t; - uchar *p, *buf; + int t; + char *p, *buf; Node *nextarg; - t = (int)a[0]; + t = ptoi(a[0]); x = execute(a[1]); nextarg = a[1]->nnext; switch (t) { case FLENGTH: - u = (Awkfloat)strlen((char *)getsval(x)); break; + u = (Awkfloat)strlen(getsval(x)); break; case FLOG: u = errcheck(log(getfval(x)), "log"); break; case FINT: @@ -1508,14 +1581,13 @@ bltin(Node **a, int n) case FCOS: u = cos(getfval(x)); break; case FATAN: - if (nextarg == 0) { - ERROR "atan2 requires two arguments; returning 1.0" - WARNING; + if (nextarg == NULL) { + WARNING("atan2 requires two arguments; returning 1.0"); u = 1.0; } else { y = execute(a[1]->nnext); u = atan2(getfval(x), getfval(y)); - tempfree(y, ""); + tempfree(y); nextarg = nextarg->nnext; } break; @@ -1523,13 +1595,14 @@ bltin(Node **a, int n) /* in case something is buffered already */ (void) fflush(stdout); /* 256 is unix-dep */ - u = (Awkfloat)system((char *)getsval(x)) / 256; + u = (Awkfloat)system(getsval(x)) / 256; break; case FRAND: - u = (Awkfloat)(rand() % 32767) / 32767.0; + /* in principle, rand() returns something in 0..RAND_MAX */ + u = (Awkfloat) (rand() % RAND_MAX) / RAND_MAX; break; case FSRAND: - if (x->tval & REC) /* no argument provided */ + if (isrec(x)) /* no argument provided */ u = time((time_t *)0); else u = getfval(x); @@ -1540,28 +1613,28 @@ bltin(Node **a, int n) buf = tostring(getsval(x)); if (t == FTOUPPER) { for (p = buf; *p; p++) - if (islower(*p)) - *p = toupper(*p); + if (islower((uschar)*p)) + *p = toupper((uschar)*p); } else { for (p = buf; *p; p++) - if (isupper(*p)) - *p = tolower(*p); + if (isupper((uschar)*p)) + *p = tolower((uschar)*p); } - tempfree(x, ""); - x = gettemp(""); + tempfree(x); + x = gettemp(); (void) setsval(x, buf); free(buf); return (x); default: /* can't happen */ - ERROR "illegal function type %d", t FATAL; + FATAL("illegal function type %d", t); break; } - tempfree(x, ""); - x = gettemp(""); + tempfree(x); + x = gettemp(); (void) setfval(x, u); - if (nextarg != 0) { - ERROR "warning: function has too many arguments" WARNING; - for (; nextarg; nextarg = nextarg->nnext) + if (nextarg != NULL) { + WARNING("warning: function has too many arguments"); + for (; nextarg != NULL; nextarg = nextarg->nnext) (void) execute(nextarg); } return (x); @@ -1569,28 +1642,28 @@ bltin(Node **a, int n) /*ARGSUSED*/ Cell * -print(Node **a, int n) +printstat(Node **a, int n) /* print a[0] */ { - register Node *x; - register Cell *y; + Node *x; + Cell *y; FILE *fp; - if (a[1] == 0) + if (a[1] == NULL) /* a[1] is redirection operator, a[2] is file */ fp = stdout; else - fp = redirect((int)a[1], a[2]); + fp = redirect(ptoi(a[1]), a[2]); for (x = a[0]; x != NULL; x = x->nnext) { y = execute(x); - (void) fputs((char *)getsval(y), fp); - tempfree(y, ""); + (void) fputs(getsval(y), fp); + tempfree(y); if (x->nnext == NULL) - (void) fputs((char *)*ORS, fp); + (void) fputs(*ORS, fp); else - (void) fputs((char *)*OFS, fp); + (void) fputs(*OFS, fp); } - if (a[1] != 0) + if (a[1] != NULL) (void) fflush(fp); - return (true); + return (True); } /*ARGSUSED*/ @@ -1602,39 +1675,38 @@ nullproc(Node **a, int n) struct { FILE *fp; - uchar *fname; + char *fname; int mode; /* '|', 'a', 'w' */ } files[FOPEN_MAX]; static FILE * -redirect(int a, Node *b) +redirect(int a, Node *b) /* set up all i/o redirections */ { FILE *fp; Cell *x; - uchar *fname; + char *fname; x = execute(b); fname = getsval(x); fp = openfile(a, fname); if (fp == NULL) - ERROR "can't open file %s", fname FATAL; - tempfree(x, ""); + FATAL("can't open file %s", fname); + tempfree(x); return (fp); } static FILE * -openfile(int a, uchar *s) +openfile(int a, const char *s) { - register int i, m; - register FILE *fp; + int i, m; + FILE *fp = NULL; if (*s == '\0') - ERROR "null file name in print or getline" FATAL; + FATAL("null file name in print or getline"); for (i = 0; i < FOPEN_MAX; i++) { - if (files[i].fname && - strcmp((char *)s, (char *)files[i].fname) == 0) { + if (files[i].fname && strcmp(s, files[i].fname) == 0) { if (a == files[i].mode || - a == APPEND && files[i].mode == GT) { + (a == APPEND && files[i].mode == GT)) { return (files[i].fp); } } @@ -1644,23 +1716,23 @@ openfile(int a, uchar *s) break; } if (i >= FOPEN_MAX) - ERROR "%s makes too many open files", s FATAL; + FATAL("%s makes too many open files", s); (void) fflush(stdout); /* force a semblance of order */ m = a; if (a == GT) { - fp = fopen((char *)s, "w"); + fp = fopen(s, "w"); } else if (a == APPEND) { - fp = fopen((char *)s, "a"); + fp = fopen(s, "a"); m = GT; /* so can mix > and >> */ } else if (a == '|') { /* output pipe */ - fp = popen((char *)s, "w"); + fp = popen(s, "w"); } else if (a == LE) { /* input pipe */ - fp = popen((char *)s, "r"); + fp = popen(s, "r"); } else if (a == LT) { /* getline <file */ - fp = strcmp((char *)s, "-") == 0 ? - stdin : fopen((char *)s, "r"); /* "-" is stdin */ + fp = strcmp(s, "-") == 0 ? + stdin : fopen(s, "r"); /* "-" is stdin */ } else /* can't happen */ - ERROR "illegal redirection" FATAL; + FATAL("illegal redirection %d", a); if (fp != NULL) { files[i].fname = tostring(s); files[i].fp = fp; @@ -1673,25 +1745,24 @@ openfile(int a, uchar *s) Cell * closefile(Node **a, int n) { - register Cell *x; + Cell *x; int i, stat; x = execute(a[0]); (void) getsval(x); for (i = 0; i < FOPEN_MAX; i++) { - if (files[i].fname && - strcmp((char *)x->sval, (char *)files[i].fname) == 0) { + if (files[i].fname && strcmp(x->sval, files[i].fname) == 0) { if (ferror(files[i].fp)) { - ERROR "i/o error occurred on %s", - files[i].fname WARNING; + WARNING("i/o error occurred on %s", + files[i].fname); } if (files[i].mode == '|' || files[i].mode == LE) stat = pclose(files[i].fp); else stat = fclose(files[i].fp); if (stat == EOF) { - ERROR "i/o error occurred closing %s", - files[i].fname WARNING; + WARNING("i/o error occurred closing %s", + files[i].fname); } xfree(files[i].fname); /* watch out for ref thru this */ @@ -1699,8 +1770,8 @@ closefile(Node **a, int n) files[i].fp = NULL; } } - tempfree(x, "close"); - return (true); + tempfree(x); + return (True); } static void @@ -1711,16 +1782,16 @@ closeall(void) for (i = 0; i < FOPEN_MAX; i++) { if (files[i].fp) { if (ferror(files[i].fp)) { - ERROR "i/o error occurred on %s", - files[i].fname WARNING; + WARNING("i/o error occurred on %s", + files[i].fname); } if (files[i].mode == '|' || files[i].mode == LE) stat = pclose(files[i].fp); else stat = fclose(files[i].fp); if (stat == EOF) { - ERROR "i/o error occurred while closing %s", - files[i].fname WARNING; + WARNING("i/o error occurred while closing %s", + files[i].fname); } } } @@ -1728,44 +1799,46 @@ closeall(void) /*ARGSUSED*/ Cell * -sub(Node **a, int nnn) +sub(Node **a, int nnn) /* substitute command */ { - register uchar *sptr; - register Cell *x, *y, *result; - uchar *buf, *t; + char *sptr; + Cell *x, *y, *result; + char *t, *buf; fa *pfa; - size_t bsize, cnt, len; + size_t bufsz = recsize; + size_t cnt = 0, len; + if ((buf = (char *)malloc(bufsz)) == NULL) + FATAL("out of memory in sub"); x = execute(a[3]); /* target string */ t = getsval(x); - if (a[0] == 0) + if (a[0] == NULL) /* 0 => a[1] is already-compiled regexpr */ pfa = (fa *)a[1]; /* regular expression */ else { y = execute(a[1]); pfa = makedfa(getsval(y), 1); - tempfree(y, ""); + tempfree(y); } y = execute(a[2]); /* replacement string */ - result = false; + result = False; if (pmatch(pfa, t)) { - init_buf(&buf, &bsize, LINE_INCR); - cnt = 0; sptr = t; len = patbeg - sptr; if (len > 0) { - expand_buf(&buf, &bsize, cnt + len); + (void) adjbuf(&buf, &bufsz, cnt + len, + recsize, NULL, "sub"); (void) memcpy(buf, sptr, len); cnt += len; } sptr = getsval(y); - while (*sptr != 0) { - expand_buf(&buf, &bsize, cnt); + while (*sptr != '\0') { + (void) adjbuf(&buf, &bufsz, 1 + cnt + patlen, + recsize, NULL, "sub"); if (*sptr == '\\' && (*(sptr+1) == '&' || *(sptr+1) == '\\')) { sptr++; /* skip \, */ buf[cnt++] = *sptr++; /* add & or \ */ } else if (*sptr == '&') { - expand_buf(&buf, &bsize, cnt + patlen); sptr++; (void) memcpy(&buf[cnt], patbeg, patlen); cnt += patlen; @@ -1775,67 +1848,74 @@ sub(Node **a, int nnn) } sptr = patbeg + patlen; if ((patlen == 0 && *patbeg) || (patlen && *(sptr-1))) { - len = strlen((char *)sptr); - expand_buf(&buf, &bsize, cnt + len); + len = strlen(sptr); + (void) adjbuf(&buf, &bufsz, 1 + cnt + len, + recsize, NULL, "sub"); (void) memcpy(&buf[cnt], sptr, len); cnt += len; } buf[cnt] = '\0'; - (void) setsval(x, buf); - free(buf); - result = true; + (void) setsval(x, buf); /* BUG: should be able to avoid copy */ + result = True; } - tempfree(x, ""); - tempfree(y, ""); + tempfree(x); + tempfree(y); + free(buf); return (result); } /*ARGSUSED*/ Cell * -gsub(Node **a, int nnn) +gsub(Node **a, int nnn) /* global substitute */ { - register Cell *x, *y; - register uchar *rptr, *sptr, *t; - uchar *buf; - register fa *pfa; + Cell *x, *y; + char *rptr, *sptr, *t; + char *buf; + fa *pfa; int mflag, tempstat, num; - size_t bsize, cnt, len; + size_t bufsz = recsize; + size_t cnt, len; + if ((buf = (char *)malloc(bufsz)) == NULL) + FATAL("out of memory in gsub"); mflag = 0; /* if mflag == 0, can replace empty string */ num = 0; x = execute(a[3]); /* target string */ t = getsval(x); - if (a[0] == 0) - pfa = (fa *) a[1]; /* regular expression */ + if (a[0] == NULL) /* 0 => a[1] is already-compiled regexpr */ + pfa = (fa *)a[1]; /* regular expression */ else { y = execute(a[1]); pfa = makedfa(getsval(y), 1); - tempfree(y, ""); + tempfree(y); } y = execute(a[2]); /* replacement string */ if (pmatch(pfa, t)) { tempstat = pfa->initstat; pfa->initstat = 2; - init_buf(&buf, &bsize, LINE_INCR); rptr = getsval(y); cnt = 0; do { - if (patlen == 0 && *patbeg != 0) { + if (patlen == 0 && *patbeg != '\0') { /* matched empty string */ if (mflag == 0) { /* can replace empty */ num++; sptr = rptr; - while (*sptr != 0) { - expand_buf(&buf, &bsize, cnt); + while (*sptr != '\0') { + (void) adjbuf(&buf, &bufsz, + 1 + cnt, recsize, + NULL, "gsub"); if (*sptr == '\\' && (*(sptr+1) == '&' || *(sptr+1) == '\\')) { sptr++; buf[cnt++] = *sptr++; } else if (*sptr == '&') { - expand_buf(&buf, - &bsize, - cnt + patlen); + (void) adjbuf(&buf, + &bufsz, + 1 + cnt + patlen, + recsize, + NULL, "gsub"); sptr++; (void) memcpy(&buf[cnt], patbeg, patlen); @@ -1845,9 +1925,10 @@ gsub(Node **a, int nnn) } } } - if (*t == 0) /* at end */ + if (*t == '\0') /* at end */ goto done; - expand_buf(&buf, &bsize, cnt); + (void) adjbuf(&buf, &bufsz, 1 + cnt, + recsize, NULL, "gsub"); buf[cnt++] = *t++; mflag = 0; } else { /* matched nonempty string */ @@ -1855,22 +1936,26 @@ gsub(Node **a, int nnn) sptr = t; len = patbeg - sptr; if (len > 0) { - expand_buf(&buf, &bsize, cnt + len); + (void) adjbuf(&buf, &bufsz, + 1 + cnt + len, recsize, + NULL, "gsub"); (void) memcpy(&buf[cnt], sptr, len); cnt += len; } sptr = rptr; - while (*sptr != 0) { - expand_buf(&buf, &bsize, cnt); + while (*sptr != '\0') { + (void) adjbuf(&buf, &bufsz, 1 + cnt, + recsize, NULL, "gsub"); if (*sptr == '\\' && (*(sptr+1) == '&' || *(sptr+1) == '\\')) { sptr++; buf[cnt++] = *sptr++; } else if (*sptr == '&') { - expand_buf(&buf, &bsize, - cnt + patlen); sptr++; + (void) adjbuf(&buf, &bufsz, + 1 + cnt + patlen, recsize, + NULL, "gsub"); (void) memcpy(&buf[cnt], patbeg, patlen); cnt += patlen; @@ -1879,14 +1964,15 @@ gsub(Node **a, int nnn) } } t = patbeg + patlen; - if ((*(t-1) == 0) || (*t == 0)) + if ((*(t-1) == '\0') || (*t == '\0')) goto done; mflag = 1; } } while (pmatch(pfa, t)); sptr = t; - len = strlen((char *)sptr); - expand_buf(&buf, &bsize, len + cnt); + len = strlen(sptr); + (void) adjbuf(&buf, &bufsz, 1 + len + cnt, + recsize, NULL, "gsub"); (void) memcpy(&buf[cnt], sptr, len); cnt += len; done: @@ -1895,9 +1981,9 @@ gsub(Node **a, int nnn) free(buf); pfa->initstat = tempstat; } - tempfree(x, ""); - tempfree(y, ""); - x = gettemp(""); + tempfree(x); + tempfree(y); + x = gettemp(); x->tval = NUM; x->fval = num; return (x); |