Annotation of src/usr.bin/awk/run.c, Revision 1.49
1.49 ! millert 1: /* $OpenBSD: run.c,v 1.48 2020/06/10 21:00:31 millert Exp $ */
1.1 tholo 2: /****************************************************************
1.13 kstailey 3: Copyright (C) Lucent Technologies 1997
1.1 tholo 4: All Rights Reserved
5:
6: Permission to use, copy, modify, and distribute this software and
7: its documentation for any purpose and without fee is hereby
8: granted, provided that the above copyright notice appear in all
9: copies and that both that the copyright notice and this
10: permission notice and warranty disclaimer appear in supporting
1.13 kstailey 11: documentation, and that the name Lucent Technologies or any of
12: its entities not be used in advertising or publicity pertaining
13: to distribution of the software without specific, written prior
14: permission.
15:
16: LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17: INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
18: IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
19: SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20: WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
21: IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
22: ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
23: THIS SOFTWARE.
1.1 tholo 24: ****************************************************************/
25:
26: #define DEBUG
27: #include <stdio.h>
28: #include <ctype.h>
1.45 guenther 29: #include <errno.h>
1.1 tholo 30: #include <setjmp.h>
1.25 millert 31: #include <limits.h>
1.1 tholo 32: #include <math.h>
33: #include <string.h>
34: #include <stdlib.h>
35: #include <time.h>
1.47 millert 36: #include <sys/types.h>
37: #include <sys/wait.h>
1.1 tholo 38: #include "awk.h"
1.13 kstailey 39: #include "ytab.h"
1.1 tholo 40:
41: #define tempfree(x) if (istemp(x)) tfree(x); else
42:
43: /*
44: #undef tempfree
45:
46: void tempfree(Cell *p) {
47: if (p->ctype == OCELL && (p->csub < CUNK || p->csub > CFREE)) {
1.16 millert 48: WARNING("bad csub %d in Cell %d %s",
49: p->csub, p->ctype, p->sval);
1.1 tholo 50: }
51: if (istemp(p))
52: tfree(p);
53: }
54: */
55:
1.30 millert 56: /* do we really need these? */
57: /* #ifdef _NFILE */
58: /* #ifndef FOPEN_MAX */
59: /* #define FOPEN_MAX _NFILE */
60: /* #endif */
61: /* #endif */
62: /* */
63: /* #ifndef FOPEN_MAX */
64: /* #define FOPEN_MAX 40 */ /* max number of open files */
65: /* #endif */
66: /* */
67: /* #ifndef RAND_MAX */
68: /* #define RAND_MAX 32767 */ /* all that ansi guarantees */
69: /* #endif */
1.1 tholo 70:
71: jmp_buf env;
1.13 kstailey 72: extern int pairstack[];
1.33 millert 73: extern Awkfloat srand_seed;
1.1 tholo 74:
75: Node *winner = NULL; /* root of parse tree */
76: Cell *tmps; /* free temporary cells for execution */
77:
1.46 millert 78: static Cell truecell ={ OBOOL, BTRUE, 0, 0, 1.0, NUM, NULL };
1.15 millert 79: Cell *True = &truecell;
1.46 millert 80: static Cell falsecell ={ OBOOL, BFALSE, 0, 0, 0.0, NUM, NULL };
1.15 millert 81: Cell *False = &falsecell;
1.46 millert 82: static Cell breakcell ={ OJUMP, JBREAK, 0, 0, 0.0, NUM, NULL };
1.1 tholo 83: Cell *jbreak = &breakcell;
1.46 millert 84: static Cell contcell ={ OJUMP, JCONT, 0, 0, 0.0, NUM, NULL };
1.1 tholo 85: Cell *jcont = &contcell;
1.46 millert 86: static Cell nextcell ={ OJUMP, JNEXT, 0, 0, 0.0, NUM, NULL };
1.1 tholo 87: Cell *jnext = &nextcell;
1.46 millert 88: static Cell nextfilecell ={ OJUMP, JNEXTFILE, 0, 0, 0.0, NUM, NULL };
1.1 tholo 89: Cell *jnextfile = &nextfilecell;
1.46 millert 90: static Cell exitcell ={ OJUMP, JEXIT, 0, 0, 0.0, NUM, NULL };
1.1 tholo 91: Cell *jexit = &exitcell;
1.46 millert 92: static Cell retcell ={ OJUMP, JRET, 0, 0, 0.0, NUM, NULL };
1.1 tholo 93: Cell *jret = &retcell;
1.46 millert 94: static Cell tempcell ={ OCELL, CTEMP, 0, "", 0.0, NUM|STR|DONTFREE, NULL };
1.1 tholo 95:
96: Node *curnode = NULL; /* the node being executed, for debugging */
1.22 deraadt 97:
98: void stdinit(void);
99: void flush_all(void);
100:
1.13 kstailey 101: /* buffer memory management */
102: int adjbuf(char **pbuf, int *psiz, int minlen, int quantum, char **pbptr,
1.18 millert 103: const char *whatrtn)
1.13 kstailey 104: /* pbuf: address of pointer to buffer being managed
105: * psiz: address of buffer size variable
106: * minlen: minimum length of buffer needed
107: * quantum: buffer size quantum
108: * pbptr: address of movable pointer into buffer, or 0 if none
109: * whatrtn: name of the calling routine if failure should cause fatal error
110: *
111: * return 0 for realloc failure, !=0 for success
112: */
113: {
114: if (minlen > *psiz) {
115: char *tbuf;
116: int rminlen = quantum ? minlen % quantum : 0;
117: int boff = pbptr ? *pbptr - *pbuf : 0;
118: /* round up to next multiple of quantum */
119: if (rminlen)
120: minlen += quantum - rminlen;
1.15 millert 121: tbuf = (char *) realloc(*pbuf, minlen);
1.48 millert 122: DPRINTF( ("adjbuf %s: %d %d (pbuf=%p, tbuf=%p)\n", whatrtn, *psiz, minlen, (void *) *pbuf, (void *) tbuf) );
1.13 kstailey 123: if (tbuf == NULL) {
124: if (whatrtn)
1.16 millert 125: FATAL("out of memory in %s", whatrtn);
1.13 kstailey 126: return 0;
127: }
128: *pbuf = tbuf;
129: *psiz = minlen;
130: if (pbptr)
131: *pbptr = tbuf + boff;
132: }
133: return 1;
134: }
135:
1.1 tholo 136: void run(Node *a) /* execution of parse tree starts here */
137: {
1.16 millert 138: stdinit();
1.1 tholo 139: execute(a);
140: closeall();
141: }
142:
143: Cell *execute(Node *u) /* execute a node of the parse tree */
144: {
145: Cell *(*proc)(Node **, int);
146: Cell *x;
147: Node *a;
148:
149: if (u == NULL)
1.15 millert 150: return(True);
1.1 tholo 151: for (a = u; ; a = a->nnext) {
152: curnode = a;
153: if (isvalue(a)) {
1.2 millert 154: x = (Cell *) (a->narg[0]);
1.13 kstailey 155: if (isfld(x) && !donefld)
1.1 tholo 156: fldbld();
1.13 kstailey 157: else if (isrec(x) && !donerec)
1.1 tholo 158: recbld();
159: return(x);
160: }
161: if (notlegal(a->nobj)) /* probably a Cell* but too risky to print */
1.16 millert 162: FATAL("illegal statement");
1.1 tholo 163: proc = proctab[a->nobj-FIRSTTOKEN];
164: x = (*proc)(a->narg, a->nobj);
1.13 kstailey 165: if (isfld(x) && !donefld)
1.1 tholo 166: fldbld();
1.13 kstailey 167: else if (isrec(x) && !donerec)
1.1 tholo 168: recbld();
169: if (isexpr(a))
170: return(x);
171: if (isjump(x))
172: return(x);
173: if (a->nnext == NULL)
174: return(x);
175: tempfree(x);
176: }
177: }
178:
179:
180: Cell *program(Node **a, int n) /* execute an awk program */
181: { /* a[0] = BEGIN, a[1] = body, a[2] = END */
182: Cell *x;
183:
184: if (setjmp(env) != 0)
185: goto ex;
186: if (a[0]) { /* BEGIN */
187: x = execute(a[0]);
188: if (isexit(x))
1.15 millert 189: return(True);
1.1 tholo 190: if (isjump(x))
1.16 millert 191: FATAL("illegal break, continue, next or nextfile from BEGIN");
1.1 tholo 192: tempfree(x);
193: }
194: if (a[1] || a[2])
1.13 kstailey 195: while (getrec(&record, &recsize, 1) > 0) {
1.1 tholo 196: x = execute(a[1]);
197: if (isexit(x))
198: break;
199: tempfree(x);
200: }
201: ex:
202: if (setjmp(env) != 0) /* handles exit within END */
203: goto ex1;
204: if (a[2]) { /* END */
205: x = execute(a[2]);
206: if (isbreak(x) || isnext(x) || iscont(x))
1.16 millert 207: FATAL("illegal break, continue, next or nextfile from END");
1.1 tholo 208: tempfree(x);
209: }
210: ex1:
1.15 millert 211: return(True);
1.1 tholo 212: }
213:
214: struct Frame { /* stack frame for awk function calls */
215: int nargs; /* number of arguments in this call */
216: Cell *fcncell; /* pointer to Cell for function */
217: Cell **args; /* pointer to array of arguments after execute */
218: Cell *retval; /* return value */
219: };
220:
221: #define NARGS 50 /* max args in a call */
222:
223: struct Frame *frame = NULL; /* base of stack frames; dynamically allocated */
224: int nframe = 0; /* number of frames allocated */
225: struct Frame *fp = NULL; /* frame pointer. bottom level unused */
226:
227: Cell *call(Node **a, int n) /* function call. very kludgy and fragile */
228: {
1.46 millert 229: static Cell newcopycell = { OCELL, CCOPY, 0, "", 0.0, NUM|STR|DONTFREE, NULL };
1.1 tholo 230: int i, ncall, ndef;
1.25 millert 231: int freed = 0; /* handles potential double freeing when fcn & param share a tempcell */
1.1 tholo 232: Node *x;
1.13 kstailey 233: Cell *args[NARGS], *oargs[NARGS]; /* BUG: fixed size arrays */
234: Cell *y, *z, *fcn;
1.1 tholo 235: char *s;
236:
237: fcn = execute(a[0]); /* the function itself */
238: s = fcn->nval;
1.13 kstailey 239: if (!isfcn(fcn))
1.16 millert 240: FATAL("calling undefined function %s", s);
1.1 tholo 241: if (frame == NULL) {
242: fp = frame = (struct Frame *) calloc(nframe += 100, sizeof(struct Frame));
243: if (frame == NULL)
1.16 millert 244: FATAL("out of space for stack frames calling %s", s);
1.1 tholo 245: }
246: for (ncall = 0, x = a[1]; x != NULL; x = x->nnext) /* args in call */
247: ncall++;
1.12 millert 248: ndef = (int) fcn->fval; /* args in defn */
1.42 deraadt 249: DPRINTF( ("calling %s, %d args (%d in defn), fp=%d\n", s, ncall, ndef, (int) (fp-frame)) );
1.1 tholo 250: if (ncall > ndef)
1.16 millert 251: WARNING("function %s called with %d args, uses only %d",
252: s, ncall, ndef);
1.1 tholo 253: if (ncall + ndef > NARGS)
1.16 millert 254: FATAL("function %s has %d arguments, limit %d", s, ncall+ndef, NARGS);
1.1 tholo 255: for (i = 0, x = a[1]; x != NULL; i++, x = x->nnext) { /* get call args */
1.42 deraadt 256: DPRINTF( ("evaluate args[%d], fp=%d:\n", i, (int) (fp-frame)) );
1.1 tholo 257: y = execute(x);
258: oargs[i] = y;
1.42 deraadt 259: DPRINTF( ("args[%d]: %s %f <%s>, t=%o\n",
1.18 millert 260: i, NN(y->nval), y->fval, isarr(y) ? "(array)" : NN(y->sval), y->tval) );
1.13 kstailey 261: if (isfcn(y))
1.16 millert 262: FATAL("can't use function %s as argument in %s", y->nval, s);
1.1 tholo 263: if (isarr(y))
264: args[i] = y; /* arrays by ref */
265: else
266: args[i] = copycell(y);
267: tempfree(y);
268: }
269: for ( ; i < ndef; i++) { /* add null args for ones not provided */
270: args[i] = gettemp();
271: *args[i] = newcopycell;
272: }
273: fp++; /* now ok to up frame */
274: if (fp >= frame + nframe) {
275: int dfp = fp - frame; /* old index */
1.38 deraadt 276: frame = reallocarray(frame, (nframe += 100),
277: sizeof(struct Frame));
1.1 tholo 278: if (frame == NULL)
1.16 millert 279: FATAL("out of space for stack frames in %s", s);
1.1 tholo 280: fp = frame + dfp;
281: }
282: fp->fcncell = fcn;
283: fp->args = args;
284: fp->nargs = ndef; /* number defined with (excess are locals) */
285: fp->retval = gettemp();
286:
1.42 deraadt 287: DPRINTF( ("start exec of %s, fp=%d\n", s, (int) (fp-frame)) );
1.1 tholo 288: y = execute((Node *)(fcn->sval)); /* execute body */
1.42 deraadt 289: DPRINTF( ("finished exec of %s, fp=%d\n", s, (int) (fp-frame)) );
1.1 tholo 290:
291: for (i = 0; i < ndef; i++) {
292: Cell *t = fp->args[i];
293: if (isarr(t)) {
294: if (t->csub == CCOPY) {
295: if (i >= ncall) {
296: freesymtab(t);
297: t->csub = CTEMP;
1.14 millert 298: tempfree(t);
1.1 tholo 299: } else {
300: oargs[i]->tval = t->tval;
301: oargs[i]->tval &= ~(STR|NUM|DONTFREE);
302: oargs[i]->sval = t->sval;
303: tempfree(t);
304: }
305: }
306: } else if (t != y) { /* kludge to prevent freeing twice */
307: t->csub = CTEMP;
308: tempfree(t);
1.25 millert 309: } else if (t == y && t->csub == CCOPY) {
310: t->csub = CTEMP;
311: tempfree(t);
312: freed = 1;
1.1 tholo 313: }
314: }
315: tempfree(fcn);
1.17 millert 316: if (isexit(y) || isnext(y))
1.1 tholo 317: return y;
1.25 millert 318: if (freed == 0) {
319: tempfree(y); /* don't free twice! */
320: }
1.1 tholo 321: z = fp->retval; /* return value */
1.42 deraadt 322: DPRINTF( ("%s returns %g |%s| %o\n", s, getfval(z), getsval(z), z->tval) );
1.1 tholo 323: fp--;
324: return(z);
325: }
326:
327: Cell *copycell(Cell *x) /* make a copy of a cell in a temp */
328: {
329: Cell *y;
330:
1.47 millert 331: /* copy is not constant or field */
332:
1.1 tholo 333: y = gettemp();
1.47 millert 334: y->tval = x->tval & ~(CON|FLD|REC);
1.1 tholo 335: y->csub = CCOPY; /* prevents freeing until call is over */
1.13 kstailey 336: y->nval = x->nval; /* BUG? */
1.47 millert 337: if (isstr(x) /* || x->ctype == OCELL */) {
1.17 millert 338: y->sval = tostring(x->sval);
1.47 millert 339: y->tval &= ~DONTFREE;
340: } else
341: y->tval |= DONTFREE;
1.1 tholo 342: y->fval = x->fval;
343: return y;
344: }
345:
346: Cell *arg(Node **a, int n) /* nth argument of a function */
347: {
348:
1.15 millert 349: n = ptoi(a[0]); /* argument number, counting from 0 */
1.42 deraadt 350: DPRINTF( ("arg(%d), fp->nargs=%d\n", n, fp->nargs) );
1.1 tholo 351: if (n+1 > fp->nargs)
1.16 millert 352: FATAL("argument #%d of function %s was not supplied",
353: n+1, fp->fcncell->nval);
1.1 tholo 354: return fp->args[n];
355: }
356:
357: Cell *jump(Node **a, int n) /* break, continue, next, nextfile, return */
358: {
359: Cell *y;
360:
361: switch (n) {
362: case EXIT:
363: if (a[0] != NULL) {
364: y = execute(a[0]);
1.14 millert 365: errorflag = (int) getfval(y);
1.1 tholo 366: tempfree(y);
367: }
368: longjmp(env, 1);
369: case RETURN:
370: if (a[0] != NULL) {
371: y = execute(a[0]);
372: if ((y->tval & (STR|NUM)) == (STR|NUM)) {
373: setsval(fp->retval, getsval(y));
374: fp->retval->fval = getfval(y);
375: fp->retval->tval |= NUM;
376: }
377: else if (y->tval & STR)
378: setsval(fp->retval, getsval(y));
379: else if (y->tval & NUM)
380: setfval(fp->retval, getfval(y));
381: else /* can't happen */
1.16 millert 382: FATAL("bad type variable %d", y->tval);
1.1 tholo 383: tempfree(y);
384: }
385: return(jret);
386: case NEXT:
387: return(jnext);
388: case NEXTFILE:
389: nextfile();
390: return(jnextfile);
391: case BREAK:
392: return(jbreak);
393: case CONTINUE:
394: return(jcont);
395: default: /* can't happen */
1.16 millert 396: FATAL("illegal jump type %d", n);
1.1 tholo 397: }
398: return 0; /* not reached */
399: }
400:
1.31 millert 401: Cell *awkgetline(Node **a, int n) /* get next line from specific input */
1.1 tholo 402: { /* a[0] is variable, a[1] is operator, a[2] is filename */
403: Cell *r, *x;
1.13 kstailey 404: extern Cell **fldtab;
1.1 tholo 405: FILE *fp;
1.13 kstailey 406: char *buf;
407: int bufsize = recsize;
1.15 millert 408: int mode;
1.13 kstailey 409:
410: if ((buf = (char *) malloc(bufsize)) == NULL)
1.16 millert 411: FATAL("out of memory in getline");
1.1 tholo 412:
413: fflush(stdout); /* in case someone is waiting for a prompt */
414: r = gettemp();
415: if (a[1] != NULL) { /* getline < file */
416: x = execute(a[2]); /* filename */
1.15 millert 417: mode = ptoi(a[1]);
418: if (mode == '|') /* input pipe */
419: mode = LE; /* arbitrary flag */
420: fp = openfile(mode, getsval(x));
1.1 tholo 421: tempfree(x);
422: if (fp == NULL)
423: n = -1;
424: else
1.13 kstailey 425: n = readrec(&buf, &bufsize, fp);
1.1 tholo 426: if (n <= 0) {
427: ;
428: } else if (a[0] != NULL) { /* getline var <file */
1.13 kstailey 429: x = execute(a[0]);
430: setsval(x, buf);
1.49 ! millert 431: if (is_number(x->sval)) {
! 432: x->fval = atof(x->sval);
! 433: x->tval |= NUM;
! 434: }
1.13 kstailey 435: tempfree(x);
1.1 tholo 436: } else { /* getline <file */
1.13 kstailey 437: setsval(fldtab[0], buf);
1.14 millert 438: if (is_number(fldtab[0]->sval)) {
1.13 kstailey 439: fldtab[0]->fval = atof(fldtab[0]->sval);
440: fldtab[0]->tval |= NUM;
1.1 tholo 441: }
442: }
443: } else { /* bare getline; use current input */
444: if (a[0] == NULL) /* getline */
1.13 kstailey 445: n = getrec(&record, &recsize, 1);
1.1 tholo 446: else { /* getline var */
1.13 kstailey 447: n = getrec(&buf, &bufsize, 0);
448: x = execute(a[0]);
449: setsval(x, buf);
1.49 ! millert 450: if (is_number(x->sval)) {
! 451: x->fval = atof(x->sval);
! 452: x->tval |= NUM;
! 453: }
1.13 kstailey 454: tempfree(x);
1.1 tholo 455: }
456: }
457: setfval(r, (Awkfloat) n);
1.13 kstailey 458: free(buf);
1.1 tholo 459: return r;
460: }
461:
462: Cell *getnf(Node **a, int n) /* get NF */
463: {
464: if (donefld == 0)
465: fldbld();
466: return (Cell *) a[0];
467: }
468:
469: Cell *array(Node **a, int n) /* a[0] is symtab, a[1] is list of subscripts */
470: {
471: Cell *x, *y, *z;
472: char *s;
473: Node *np;
1.13 kstailey 474: char *buf;
475: int bufsz = recsize;
1.49 ! millert 476: int nsub;
1.13 kstailey 477:
1.15 millert 478: if ((buf = (char *) malloc(bufsz)) == NULL)
1.16 millert 479: FATAL("out of memory in array");
1.1 tholo 480:
481: x = execute(a[0]); /* Cell* for symbol table */
482: buf[0] = 0;
483: for (np = a[1]; np; np = np->nnext) {
484: y = execute(np); /* subscript */
485: s = getsval(y);
1.49 ! millert 486: nsub = strlen(getsval(subseploc));
1.30 millert 487: if (!adjbuf(&buf, &bufsz, strlen(buf)+strlen(s)+nsub+1, recsize, 0, "array"))
1.16 millert 488: FATAL("out of memory for %s[%s...]", x->nval, buf);
1.20 pvalchev 489: strlcat(buf, s, bufsz);
1.1 tholo 490: if (np->nnext)
1.20 pvalchev 491: strlcat(buf, *SUBSEP, bufsz);
1.1 tholo 492: tempfree(y);
493: }
494: if (!isarr(x)) {
1.42 deraadt 495: DPRINTF( ("making %s into an array\n", NN(x->nval)) );
1.1 tholo 496: if (freeable(x))
497: xfree(x->sval);
498: x->tval &= ~(STR|NUM|DONTFREE);
499: x->tval |= ARR;
500: x->sval = (char *) makesymtab(NSYMTAB);
501: }
502: z = setsymtab(buf, "", 0.0, STR|NUM, (Array *) x->sval);
503: z->ctype = OCELL;
504: z->csub = CVAR;
505: tempfree(x);
1.13 kstailey 506: free(buf);
1.1 tholo 507: return(z);
508: }
509:
1.14 millert 510: Cell *awkdelete(Node **a, int n) /* a[0] is symtab, a[1] is list of subscripts */
1.1 tholo 511: {
512: Cell *x, *y;
513: Node *np;
1.13 kstailey 514: char *s;
1.49 ! millert 515: int nsub;
1.1 tholo 516:
517: x = execute(a[0]); /* Cell* for symbol table */
518: if (!isarr(x))
1.15 millert 519: return True;
1.1 tholo 520: if (a[1] == 0) { /* delete the elements, not the table */
521: freesymtab(x);
522: x->tval &= ~STR;
523: x->tval |= ARR;
524: x->sval = (char *) makesymtab(NSYMTAB);
525: } else {
1.13 kstailey 526: int bufsz = recsize;
527: char *buf;
1.15 millert 528: if ((buf = (char *) malloc(bufsz)) == NULL)
1.16 millert 529: FATAL("out of memory in adelete");
1.1 tholo 530: buf[0] = 0;
531: for (np = a[1]; np; np = np->nnext) {
532: y = execute(np); /* subscript */
533: s = getsval(y);
1.49 ! millert 534: nsub = strlen(getsval(subseploc));
1.30 millert 535: if (!adjbuf(&buf, &bufsz, strlen(buf)+strlen(s)+nsub+1, recsize, 0, "awkdelete"))
1.16 millert 536: FATAL("out of memory deleting %s[%s...]", x->nval, buf);
1.49 ! millert 537: strlcat(buf, s, bufsz);
1.1 tholo 538: if (np->nnext)
1.20 pvalchev 539: strlcat(buf, *SUBSEP, bufsz);
1.1 tholo 540: tempfree(y);
541: }
542: freeelem(x, buf);
1.13 kstailey 543: free(buf);
1.1 tholo 544: }
545: tempfree(x);
1.15 millert 546: return True;
1.1 tholo 547: }
548:
549: Cell *intest(Node **a, int n) /* a[0] is index (list), a[1] is symtab */
550: {
551: Cell *x, *ap, *k;
552: Node *p;
1.13 kstailey 553: char *buf;
1.1 tholo 554: char *s;
1.13 kstailey 555: int bufsz = recsize;
1.49 ! millert 556: int nsub;
1.1 tholo 557:
558: ap = execute(a[1]); /* array name */
559: if (!isarr(ap)) {
1.42 deraadt 560: DPRINTF( ("making %s into an array\n", ap->nval) );
1.1 tholo 561: if (freeable(ap))
562: xfree(ap->sval);
563: ap->tval &= ~(STR|NUM|DONTFREE);
564: ap->tval |= ARR;
565: ap->sval = (char *) makesymtab(NSYMTAB);
566: }
1.15 millert 567: if ((buf = (char *) malloc(bufsz)) == NULL) {
1.16 millert 568: FATAL("out of memory in intest");
1.13 kstailey 569: }
1.1 tholo 570: buf[0] = 0;
571: for (p = a[0]; p; p = p->nnext) {
572: x = execute(p); /* expr */
573: s = getsval(x);
1.49 ! millert 574: nsub = strlen(getsval(subseploc));
1.30 millert 575: if (!adjbuf(&buf, &bufsz, strlen(buf)+strlen(s)+nsub+1, recsize, 0, "intest"))
1.16 millert 576: FATAL("out of memory deleting %s[%s...]", x->nval, buf);
1.20 pvalchev 577: strlcat(buf, s, bufsz);
1.1 tholo 578: tempfree(x);
579: if (p->nnext)
1.20 pvalchev 580: strlcat(buf, *SUBSEP, bufsz);
1.1 tholo 581: }
582: k = lookup(buf, (Array *) ap->sval);
583: tempfree(ap);
1.13 kstailey 584: free(buf);
1.1 tholo 585: if (k == NULL)
1.15 millert 586: return(False);
1.1 tholo 587: else
1.15 millert 588: return(True);
1.1 tholo 589: }
590:
591:
592: Cell *matchop(Node **a, int n) /* ~ and match() */
593: {
594: Cell *x, *y;
595: char *s, *t;
596: int i;
597: fa *pfa;
1.18 millert 598: int (*mf)(fa *, const char *) = match, mode = 0;
1.1 tholo 599:
600: if (n == MATCHFCN) {
601: mf = pmatch;
602: mode = 1;
603: }
604: x = execute(a[1]); /* a[1] = target text */
605: s = getsval(x);
606: if (a[0] == 0) /* a[1] == 0: already-compiled reg expr */
607: i = (*mf)((fa *) a[2], s);
608: else {
609: y = execute(a[2]); /* a[2] = regular expr */
610: t = getsval(y);
611: pfa = makedfa(t, mode);
612: i = (*mf)(pfa, s);
613: tempfree(y);
614: }
615: tempfree(x);
616: if (n == MATCHFCN) {
617: int start = patbeg - s + 1;
618: if (patlen < 0)
619: start = 0;
620: setfval(rstartloc, (Awkfloat) start);
621: setfval(rlengthloc, (Awkfloat) patlen);
622: x = gettemp();
623: x->tval = NUM;
624: x->fval = start;
625: return x;
626: } else if ((n == MATCH && i == 1) || (n == NOTMATCH && i == 0))
1.15 millert 627: return(True);
1.1 tholo 628: else
1.15 millert 629: return(False);
1.1 tholo 630: }
631:
632:
633: Cell *boolop(Node **a, int n) /* a[0] || a[1], a[0] && a[1], !a[0] */
634: {
635: Cell *x, *y;
636: int i;
637:
638: x = execute(a[0]);
639: i = istrue(x);
640: tempfree(x);
641: switch (n) {
642: case BOR:
1.15 millert 643: if (i) return(True);
1.1 tholo 644: y = execute(a[1]);
645: i = istrue(y);
646: tempfree(y);
1.15 millert 647: if (i) return(True);
648: else return(False);
1.1 tholo 649: case AND:
1.15 millert 650: if ( !i ) return(False);
1.1 tholo 651: y = execute(a[1]);
652: i = istrue(y);
653: tempfree(y);
1.15 millert 654: if (i) return(True);
655: else return(False);
1.1 tholo 656: case NOT:
1.15 millert 657: if (i) return(False);
658: else return(True);
1.1 tholo 659: default: /* can't happen */
1.16 millert 660: FATAL("unknown boolean operator %d", n);
1.1 tholo 661: }
662: return 0; /*NOTREACHED*/
663: }
664:
665: Cell *relop(Node **a, int n) /* a[0 < a[1], etc. */
666: {
667: int i;
668: Cell *x, *y;
669: Awkfloat j;
670:
671: x = execute(a[0]);
672: y = execute(a[1]);
673: if (x->tval&NUM && y->tval&NUM) {
674: j = x->fval - y->fval;
675: i = j<0? -1: (j>0? 1: 0);
676: } else {
677: i = strcmp(getsval(x), getsval(y));
678: }
679: tempfree(x);
680: tempfree(y);
681: switch (n) {
1.15 millert 682: case LT: if (i<0) return(True);
683: else return(False);
684: case LE: if (i<=0) return(True);
685: else return(False);
686: case NE: if (i!=0) return(True);
687: else return(False);
688: case EQ: if (i == 0) return(True);
689: else return(False);
690: case GE: if (i>=0) return(True);
691: else return(False);
692: case GT: if (i>0) return(True);
693: else return(False);
1.1 tholo 694: default: /* can't happen */
1.16 millert 695: FATAL("unknown relational operator %d", n);
1.1 tholo 696: }
697: return 0; /*NOTREACHED*/
698: }
699:
700: void tfree(Cell *a) /* free a tempcell */
701: {
1.13 kstailey 702: if (freeable(a)) {
1.42 deraadt 703: DPRINTF( ("freeing %s %s %o\n", NN(a->nval), NN(a->sval), a->tval) );
1.1 tholo 704: xfree(a->sval);
1.13 kstailey 705: }
1.1 tholo 706: if (a == tmps)
1.16 millert 707: FATAL("tempcell list is curdled");
1.1 tholo 708: a->cnext = tmps;
709: tmps = a;
710: }
711:
712: Cell *gettemp(void) /* get a tempcell */
713: { int i;
714: Cell *x;
715:
716: if (!tmps) {
717: tmps = (Cell *) calloc(100, sizeof(Cell));
718: if (!tmps)
1.16 millert 719: FATAL("out of space for temporaries");
1.1 tholo 720: for(i = 1; i < 100; i++)
721: tmps[i-1].cnext = &tmps[i];
722: tmps[i-1].cnext = 0;
723: }
724: x = tmps;
725: tmps = x->cnext;
726: *x = tempcell;
727: return(x);
728: }
729:
730: Cell *indirect(Node **a, int n) /* $( a[0] ) */
731: {
1.25 millert 732: Awkfloat val;
1.1 tholo 733: Cell *x;
734: int m;
735: char *s;
736:
737: x = execute(a[0]);
1.25 millert 738: val = getfval(x); /* freebsd: defend against super large field numbers */
739: if ((Awkfloat)INT_MAX < val)
740: FATAL("trying to access out of range field %s", x->nval);
741: m = (int) val;
1.14 millert 742: if (m == 0 && !is_number(s = getsval(x))) /* suspicion! */
1.16 millert 743: FATAL("illegal field $(%s), name \"%s\"", s, x->nval);
1.13 kstailey 744: /* BUG: can x->nval ever be null??? */
1.1 tholo 745: tempfree(x);
746: x = fieldadr(m);
1.13 kstailey 747: x->ctype = OCELL; /* BUG? why are these needed? */
1.1 tholo 748: x->csub = CFLD;
749: return(x);
750: }
751:
752: Cell *substr(Node **a, int nnn) /* substr(a[0], a[1], a[2]) */
753: {
754: int k, m, n;
755: char *s;
756: int temp;
757: Cell *x, *y, *z = 0;
758:
759: x = execute(a[0]);
760: y = execute(a[1]);
761: if (a[2] != 0)
762: z = execute(a[2]);
763: s = getsval(x);
764: k = strlen(s) + 1;
765: if (k <= 1) {
766: tempfree(x);
767: tempfree(y);
1.17 millert 768: if (a[2] != 0) {
1.1 tholo 769: tempfree(z);
1.17 millert 770: }
1.1 tholo 771: x = gettemp();
772: setsval(x, "");
773: return(x);
774: }
1.14 millert 775: m = (int) getfval(y);
1.1 tholo 776: if (m <= 0)
777: m = 1;
778: else if (m > k)
779: m = k;
780: tempfree(y);
781: if (a[2] != 0) {
1.14 millert 782: n = (int) getfval(z);
1.1 tholo 783: tempfree(z);
784: } else
785: n = k - 1;
786: if (n < 0)
787: n = 0;
788: else if (n > k - m)
789: n = k - m;
1.42 deraadt 790: DPRINTF( ("substr: m=%d, n=%d, s=%s\n", m, n, s) );
1.1 tholo 791: y = gettemp();
792: temp = s[n+m-1]; /* with thanks to John Linderman */
793: s[n+m-1] = '\0';
794: setsval(y, s + m - 1);
795: s[n+m-1] = temp;
796: tempfree(x);
797: return(y);
798: }
799:
800: Cell *sindex(Node **a, int nnn) /* index(a[0], a[1]) */
801: {
802: Cell *x, *y, *z;
803: char *s1, *s2, *p1, *p2, *q;
804: Awkfloat v = 0.0;
805:
806: x = execute(a[0]);
807: s1 = getsval(x);
808: y = execute(a[1]);
809: s2 = getsval(y);
810:
811: z = gettemp();
812: for (p1 = s1; *p1 != '\0'; p1++) {
813: for (q=p1, p2=s2; *p2 != '\0' && *q == *p2; q++, p2++)
814: ;
815: if (*p2 == '\0') {
816: v = (Awkfloat) (p1 - s1 + 1); /* origin 1 */
817: break;
818: }
819: }
820: tempfree(x);
821: tempfree(y);
822: setfval(z, v);
823: return(z);
824: }
825:
1.13 kstailey 826: #define MAXNUMSIZE 50
827:
1.18 millert 828: int format(char **pbuf, int *pbufsize, const char *s, Node *a) /* printf-like conversions */
1.1 tholo 829: {
1.13 kstailey 830: char *fmt;
1.18 millert 831: char *p, *t;
832: const char *os;
1.1 tholo 833: Cell *x;
1.10 kstailey 834: int flag = 0, n;
1.13 kstailey 835: int fmtwd; /* format width */
836: int fmtsz = recsize;
837: char *buf = *pbuf;
838: int bufsize = *pbufsize;
1.1 tholo 839:
1.47 millert 840: static int first = 1;
841: static int have_a_format = 0;
842:
843: if (first) {
844: char buf[100];
845:
846: snprintf(buf, sizeof(buf), "%a", 42.0);
847: have_a_format = (strcmp(buf, "0x1.5p+5") == 0);
848: first = 0;
849: }
850:
1.1 tholo 851: os = s;
852: p = buf;
1.15 millert 853: if ((fmt = (char *) malloc(fmtsz)) == NULL)
1.16 millert 854: FATAL("out of memory in format()");
1.1 tholo 855: while (*s) {
1.30 millert 856: adjbuf(&buf, &bufsize, MAXNUMSIZE+1+p-buf, recsize, &p, "format1");
1.1 tholo 857: if (*s != '%') {
858: *p++ = *s++;
859: continue;
860: }
861: if (*(s+1) == '%') {
862: *p++ = '%';
863: s += 2;
864: continue;
865: }
1.13 kstailey 866: /* have to be real careful in case this is a huge number, eg, %100000d */
867: fmtwd = atoi(s+1);
868: if (fmtwd < 0)
869: fmtwd = -fmtwd;
1.30 millert 870: adjbuf(&buf, &bufsize, fmtwd+1+p-buf, recsize, &p, "format2");
1.13 kstailey 871: for (t = fmt; (*t++ = *s) != '\0'; s++) {
1.30 millert 872: if (!adjbuf(&fmt, &fmtsz, MAXNUMSIZE+1+t-fmt, recsize, &t, "format3"))
1.16 millert 873: FATAL("format item %.30s... ran format() out of memory", os);
1.17 millert 874: if (isalpha((uschar)*s) && *s != 'l' && *s != 'h' && *s != 'L')
1.1 tholo 875: break; /* the ansi panoply */
1.48 millert 876: if (*s == '$') {
877: FATAL("'$' not permitted in awk formats");
878: }
1.1 tholo 879: if (*s == '*') {
1.49 ! millert 880: if (a == NULL) {
1.27 deraadt 881: FATAL("not enough args in printf(%s)", os);
1.49 ! millert 882: }
1.1 tholo 883: x = execute(a);
884: a = a->nnext;
1.21 tedu 885: snprintf(t-1, fmt + fmtsz - (t-1), "%d", fmtwd=(int) getfval(x));
1.13 kstailey 886: if (fmtwd < 0)
887: fmtwd = -fmtwd;
888: adjbuf(&buf, &bufsize, fmtwd+1+p-buf, recsize, &p, "format");
1.1 tholo 889: t = fmt + strlen(fmt);
890: tempfree(x);
891: }
892: }
893: *t = '\0';
1.13 kstailey 894: if (fmtwd < 0)
895: fmtwd = -fmtwd;
1.30 millert 896: adjbuf(&buf, &bufsize, fmtwd+1+p-buf, recsize, &p, "format4");
1.1 tholo 897: switch (*s) {
1.47 millert 898: case 'a': case 'A':
899: if (have_a_format)
900: flag = *s;
901: else
902: flag = 'f';
903: break;
1.1 tholo 904: case 'f': case 'e': case 'g': case 'E': case 'G':
1.18 millert 905: flag = 'f';
1.1 tholo 906: break;
907: case 'd': case 'i':
1.18 millert 908: flag = 'd';
1.1 tholo 909: if(*(s-1) == 'l') break;
910: *(t-1) = 'l';
911: *t = 'd';
912: *++t = '\0';
913: break;
914: case 'o': case 'x': case 'X': case 'u':
1.18 millert 915: flag = *(s-1) == 'l' ? 'd' : 'u';
1.1 tholo 916: break;
917: case 's':
1.18 millert 918: flag = 's';
1.1 tholo 919: break;
920: case 'c':
1.18 millert 921: flag = 'c';
1.1 tholo 922: break;
923: default:
1.16 millert 924: WARNING("weird printf conversion %s", fmt);
1.18 millert 925: flag = '?';
1.1 tholo 926: break;
927: }
928: if (a == NULL)
1.16 millert 929: FATAL("not enough args in printf(%s)", os);
1.1 tholo 930: x = execute(a);
931: a = a->nnext;
1.13 kstailey 932: n = MAXNUMSIZE;
933: if (fmtwd > n)
934: n = fmtwd;
1.30 millert 935: adjbuf(&buf, &bufsize, 1+n+p-buf, recsize, &p, "format5");
1.1 tholo 936: switch (flag) {
1.21 tedu 937: case '?': /* unknown, so dump it too */
938: snprintf(p, buf + bufsize - p, "%s", fmt);
1.13 kstailey 939: t = getsval(x);
940: n = strlen(t);
941: if (fmtwd > n)
942: n = fmtwd;
1.30 millert 943: adjbuf(&buf, &bufsize, 1+strlen(p)+n+p-buf, recsize, &p, "format6");
1.5 kstailey 944: p += strlen(p);
1.21 tedu 945: snprintf(p, buf + bufsize - p, "%s", t);
1.1 tholo 946: break;
1.47 millert 947: case 'a':
948: case 'A':
1.21 tedu 949: case 'f': snprintf(p, buf + bufsize - p, fmt, getfval(x)); break;
950: case 'd': snprintf(p, buf + bufsize - p, fmt, (long) getfval(x)); break;
951: case 'u': snprintf(p, buf + bufsize - p, fmt, (int) getfval(x)); break;
1.18 millert 952: case 's':
1.1 tholo 953: t = getsval(x);
954: n = strlen(t);
1.13 kstailey 955: if (fmtwd > n)
956: n = fmtwd;
1.30 millert 957: if (!adjbuf(&buf, &bufsize, 1+n+p-buf, recsize, &p, "format7"))
1.16 millert 958: FATAL("huge string/format (%d chars) in printf %.30s... ran format() out of memory", n, t);
1.21 tedu 959: snprintf(p, buf + bufsize - p, fmt, t);
1.1 tholo 960: break;
1.18 millert 961: case 'c':
1.13 kstailey 962: if (isnum(x)) {
1.36 millert 963: if ((int)getfval(x))
1.21 tedu 964: snprintf(p, buf + bufsize - p, fmt, (int) getfval(x));
1.18 millert 965: else {
966: *p++ = '\0'; /* explicit null byte */
967: *p = '\0'; /* next output will start here */
968: }
1.13 kstailey 969: } else
1.21 tedu 970: snprintf(p, buf + bufsize - p, fmt, getsval(x)[0]);
1.1 tholo 971: break;
1.18 millert 972: default:
973: FATAL("can't happen: bad conversion %c in format()", flag);
1.1 tholo 974: }
975: tempfree(x);
1.5 kstailey 976: p += strlen(p);
1.1 tholo 977: s++;
978: }
979: *p = '\0';
1.13 kstailey 980: free(fmt);
1.1 tholo 981: for ( ; a; a = a->nnext) /* evaluate any remaining args */
982: execute(a);
1.13 kstailey 983: *pbuf = buf;
984: *pbufsize = bufsize;
985: return p - buf;
1.1 tholo 986: }
987:
988: Cell *awksprintf(Node **a, int n) /* sprintf(a[0]) */
989: {
990: Cell *x;
991: Node *y;
1.13 kstailey 992: char *buf;
993: int bufsz=3*recsize;
1.1 tholo 994:
1.15 millert 995: if ((buf = (char *) malloc(bufsz)) == NULL)
1.16 millert 996: FATAL("out of memory in awksprintf");
1.1 tholo 997: y = a[0]->nnext;
998: x = execute(a[0]);
1.13 kstailey 999: if (format(&buf, &bufsz, getsval(x), y) == -1)
1.16 millert 1000: FATAL("sprintf string %.30s... too long. can't happen.", buf);
1.1 tholo 1001: tempfree(x);
1002: x = gettemp();
1.13 kstailey 1003: x->sval = buf;
1.1 tholo 1004: x->tval = STR;
1005: return(x);
1006: }
1007:
1008: Cell *awkprintf(Node **a, int n) /* printf */
1009: { /* a[0] is list of args, starting with format string */
1010: /* a[1] is redirection operator, a[2] is redirection file */
1011: FILE *fp;
1012: Cell *x;
1013: Node *y;
1.13 kstailey 1014: char *buf;
1.9 kstailey 1015: int len;
1.13 kstailey 1016: int bufsz=3*recsize;
1.1 tholo 1017:
1.15 millert 1018: if ((buf = (char *) malloc(bufsz)) == NULL)
1.16 millert 1019: FATAL("out of memory in awkprintf");
1.1 tholo 1020: y = a[0]->nnext;
1021: x = execute(a[0]);
1.13 kstailey 1022: if ((len = format(&buf, &bufsz, getsval(x), y)) == -1)
1.16 millert 1023: FATAL("printf string %.30s... too long. can't happen.", buf);
1.1 tholo 1024: tempfree(x);
1025: if (a[1] == NULL) {
1.13 kstailey 1026: /* fputs(buf, stdout); */
1.9 kstailey 1027: fwrite(buf, len, 1, stdout);
1.8 kstailey 1028: if (ferror(stdout))
1.16 millert 1029: FATAL("write error on stdout");
1.1 tholo 1030: } else {
1.15 millert 1031: fp = redirect(ptoi(a[1]), a[2]);
1.13 kstailey 1032: /* fputs(buf, fp); */
1.9 kstailey 1033: fwrite(buf, len, 1, fp);
1.8 kstailey 1034: fflush(fp);
1035: if (ferror(fp))
1.16 millert 1036: FATAL("write error on %s", filename(fp));
1.1 tholo 1037: }
1.13 kstailey 1038: free(buf);
1.15 millert 1039: return(True);
1.1 tholo 1040: }
1041:
1042: Cell *arith(Node **a, int n) /* a[0] + a[1], etc. also -a[0] */
1043: {
1044: Awkfloat i, j = 0;
1045: double v;
1046: Cell *x, *y, *z;
1047:
1048: x = execute(a[0]);
1049: i = getfval(x);
1050: tempfree(x);
1.47 millert 1051: if (n != UMINUS && n != UPLUS) {
1.1 tholo 1052: y = execute(a[1]);
1053: j = getfval(y);
1054: tempfree(y);
1055: }
1056: z = gettemp();
1057: switch (n) {
1058: case ADD:
1059: i += j;
1060: break;
1061: case MINUS:
1062: i -= j;
1063: break;
1064: case MULT:
1065: i *= j;
1066: break;
1067: case DIVIDE:
1068: if (j == 0)
1.16 millert 1069: FATAL("division by zero");
1.1 tholo 1070: i /= j;
1071: break;
1072: case MOD:
1073: if (j == 0)
1.16 millert 1074: FATAL("division by zero in mod");
1.1 tholo 1075: modf(i/j, &v);
1076: i = i - j * v;
1077: break;
1078: case UMINUS:
1079: i = -i;
1080: break;
1.47 millert 1081: case UPLUS: /* handled by getfval(), above */
1082: break;
1.1 tholo 1083: case POWER:
1084: if (j >= 0 && modf(j, &v) == 0.0) /* pos integer exponent */
1.12 millert 1085: i = ipow(i, (int) j);
1.45 guenther 1086: else {
1087: errno = 0;
1.1 tholo 1088: i = errcheck(pow(i, j), "pow");
1.45 guenther 1089: }
1.1 tholo 1090: break;
1091: default: /* can't happen */
1.16 millert 1092: FATAL("illegal arithmetic operator %d", n);
1.1 tholo 1093: }
1094: setfval(z, i);
1095: return(z);
1096: }
1097:
1098: double ipow(double x, int n) /* x**n. ought to be done by pow, but isn't always */
1099: {
1100: double v;
1101:
1102: if (n <= 0)
1103: return 1;
1104: v = ipow(x, n/2);
1105: if (n % 2 == 0)
1106: return v * v;
1107: else
1108: return x * v * v;
1109: }
1110:
1111: Cell *incrdecr(Node **a, int n) /* a[0]++, etc. */
1112: {
1113: Cell *x, *z;
1114: int k;
1115: Awkfloat xf;
1116:
1117: x = execute(a[0]);
1118: xf = getfval(x);
1119: k = (n == PREINCR || n == POSTINCR) ? 1 : -1;
1120: if (n == PREINCR || n == PREDECR) {
1121: setfval(x, xf + k);
1122: return(x);
1123: }
1124: z = gettemp();
1125: setfval(z, xf);
1126: setfval(x, xf + k);
1127: tempfree(x);
1128: return(z);
1129: }
1130:
1131: Cell *assign(Node **a, int n) /* a[0] = a[1], a[0] += a[1], etc. */
1132: { /* this is subtle; don't muck with it. */
1133: Cell *x, *y;
1134: Awkfloat xf, yf;
1135: double v;
1136:
1137: y = execute(a[1]);
1138: x = execute(a[0]);
1139: if (n == ASSIGN) { /* ordinary assignment */
1.49 ! millert 1140: if (x == y && !(x->tval & (FLD|REC)) && x != nfloc)
! 1141: ; /* self-assignment: leave alone unless it's a field or NF */
1.1 tholo 1142: else if ((y->tval & (STR|NUM)) == (STR|NUM)) {
1143: setsval(x, getsval(y));
1144: x->fval = getfval(y);
1145: x->tval |= NUM;
1146: }
1.13 kstailey 1147: else if (isstr(y))
1.1 tholo 1148: setsval(x, getsval(y));
1.13 kstailey 1149: else if (isnum(y))
1.1 tholo 1150: setfval(x, getfval(y));
1151: else
1152: funnyvar(y, "read value of");
1153: tempfree(y);
1154: return(x);
1155: }
1156: xf = getfval(x);
1157: yf = getfval(y);
1158: switch (n) {
1159: case ADDEQ:
1160: xf += yf;
1161: break;
1162: case SUBEQ:
1163: xf -= yf;
1164: break;
1165: case MULTEQ:
1166: xf *= yf;
1167: break;
1168: case DIVEQ:
1169: if (yf == 0)
1.16 millert 1170: FATAL("division by zero in /=");
1.1 tholo 1171: xf /= yf;
1172: break;
1173: case MODEQ:
1174: if (yf == 0)
1.16 millert 1175: FATAL("division by zero in %%=");
1.1 tholo 1176: modf(xf/yf, &v);
1177: xf = xf - yf * v;
1178: break;
1179: case POWEQ:
1180: if (yf >= 0 && modf(yf, &v) == 0.0) /* pos integer exponent */
1.12 millert 1181: xf = ipow(xf, (int) yf);
1.45 guenther 1182: else {
1183: errno = 0;
1.1 tholo 1184: xf = errcheck(pow(xf, yf), "pow");
1.45 guenther 1185: }
1.1 tholo 1186: break;
1187: default:
1.16 millert 1188: FATAL("illegal assignment operator %d", n);
1.1 tholo 1189: break;
1190: }
1191: tempfree(y);
1192: setfval(x, xf);
1193: return(x);
1194: }
1195:
1196: Cell *cat(Node **a, int q) /* a[0] cat a[1] */
1197: {
1198: Cell *x, *y, *z;
1199: int n1, n2;
1.49 ! millert 1200: char *s = NULL;
! 1201: int ssz = 0;
1.1 tholo 1202:
1203: x = execute(a[0]);
1.49 ! millert 1204: n1 = strlen(getsval(x));
! 1205: adjbuf(&s, &ssz, n1 + 1, recsize, 0, "cat1");
! 1206: strlcpy(s, x->sval, ssz);
! 1207:
1.1 tholo 1208: y = execute(a[1]);
1.49 ! millert 1209: n2 = strlen(getsval(y));
! 1210: adjbuf(&s, &ssz, n1 + n2 + 1, recsize, 0, "cat2");
! 1211: strlcpy(s + n1, y->sval, ssz - n1);
! 1212:
1.31 millert 1213: tempfree(x);
1.1 tholo 1214: tempfree(y);
1.49 ! millert 1215:
1.1 tholo 1216: z = gettemp();
1217: z->sval = s;
1218: z->tval = STR;
1.49 ! millert 1219:
1.1 tholo 1220: return(z);
1221: }
1222:
1223: Cell *pastat(Node **a, int n) /* a[0] { a[1] } */
1224: {
1225: Cell *x;
1226:
1227: if (a[0] == 0)
1228: x = execute(a[1]);
1229: else {
1230: x = execute(a[0]);
1231: if (istrue(x)) {
1232: tempfree(x);
1233: x = execute(a[1]);
1234: }
1235: }
1236: return x;
1237: }
1238:
1239: Cell *dopa2(Node **a, int n) /* a[0], a[1] { a[2] } */
1240: {
1241: Cell *x;
1242: int pair;
1243:
1.15 millert 1244: pair = ptoi(a[3]);
1.1 tholo 1245: if (pairstack[pair] == 0) {
1246: x = execute(a[0]);
1247: if (istrue(x))
1248: pairstack[pair] = 1;
1249: tempfree(x);
1250: }
1251: if (pairstack[pair] == 1) {
1252: x = execute(a[1]);
1253: if (istrue(x))
1254: pairstack[pair] = 0;
1255: tempfree(x);
1256: x = execute(a[2]);
1257: return(x);
1258: }
1.15 millert 1259: return(False);
1.1 tholo 1260: }
1261:
1262: Cell *split(Node **a, int nnn) /* split(a[0], a[1], a[2]); a[3] is type */
1263: {
1264: Cell *x = 0, *y, *ap;
1.43 fcambus 1265: char *s, *origs;
1.49 ! millert 1266: char *fs, *origfs = NULL;
1.1 tholo 1267: int sep;
1.49 ! millert 1268: char *t, temp, num[50];
1.15 millert 1269: int n, tempstat, arg3type;
1.1 tholo 1270:
1271: y = execute(a[0]); /* source string */
1.43 fcambus 1272: origs = s = strdup(getsval(y));
1.44 fcambus 1273: if (s == NULL)
1274: FATAL("out of space in split");
1.15 millert 1275: arg3type = ptoi(a[3]);
1.1 tholo 1276: if (a[2] == 0) /* fs string */
1.49 ! millert 1277: fs = getsval(fsloc);
1.15 millert 1278: else if (arg3type == STRING) { /* split(str,arr,"string") */
1.1 tholo 1279: x = execute(a[2]);
1.49 ! millert 1280: origfs = fs = strdup(getsval(x));
! 1281: if (fs == NULL)
! 1282: FATAL("out of space in split");
! 1283: tempfree(x);
1.15 millert 1284: } else if (arg3type == REGEXPR)
1.13 kstailey 1285: fs = "(regexpr)"; /* split(str,arr,/regexpr/) */
1.1 tholo 1286: else
1.16 millert 1287: FATAL("illegal type of split");
1.1 tholo 1288: sep = *fs;
1289: ap = execute(a[1]); /* array name */
1290: freesymtab(ap);
1.42 deraadt 1291: DPRINTF( ("split: s=|%s|, a=%s, sep=|%s|\n", s, NN(ap->nval), fs) );
1.1 tholo 1292: ap->tval &= ~STR;
1293: ap->tval |= ARR;
1294: ap->sval = (char *) makesymtab(NSYMTAB);
1295:
1296: n = 0;
1.33 millert 1297: if (arg3type == REGEXPR && strlen((char*)((fa*)a[2])->restr) == 0) {
1298: /* split(s, a, //); have to arrange that it looks like empty sep */
1299: arg3type = 0;
1300: fs = "";
1301: sep = 0;
1302: }
1.25 millert 1303: if (*s != '\0' && (strlen(fs) > 1 || arg3type == REGEXPR)) { /* reg expr */
1.1 tholo 1304: fa *pfa;
1.15 millert 1305: if (arg3type == REGEXPR) { /* it's ready already */
1.1 tholo 1306: pfa = (fa *) a[2];
1307: } else {
1308: pfa = makedfa(fs, 1);
1309: }
1310: if (nematch(pfa,s)) {
1311: tempstat = pfa->initstat;
1312: pfa->initstat = 2;
1313: do {
1314: n++;
1.19 deraadt 1315: snprintf(num, sizeof num, "%d", n);
1.1 tholo 1316: temp = *patbeg;
1317: *patbeg = '\0';
1.14 millert 1318: if (is_number(s))
1.13 kstailey 1319: setsymtab(num, s, atof(s), STR|NUM, (Array *) ap->sval);
1.1 tholo 1320: else
1321: setsymtab(num, s, 0.0, STR, (Array *) ap->sval);
1322: *patbeg = temp;
1323: s = patbeg + patlen;
1324: if (*(patbeg+patlen-1) == 0 || *s == 0) {
1325: n++;
1.19 deraadt 1326: snprintf(num, sizeof num, "%d", n);
1.1 tholo 1327: setsymtab(num, "", 0.0, STR, (Array *) ap->sval);
1328: pfa->initstat = tempstat;
1329: goto spdone;
1330: }
1331: } while (nematch(pfa,s));
1.25 millert 1332: pfa->initstat = tempstat; /* bwk: has to be here to reset */
1333: /* cf gsub and refldbld */
1.1 tholo 1334: }
1335: n++;
1.19 deraadt 1336: snprintf(num, sizeof num, "%d", n);
1.14 millert 1337: if (is_number(s))
1.13 kstailey 1338: setsymtab(num, s, atof(s), STR|NUM, (Array *) ap->sval);
1.1 tholo 1339: else
1340: setsymtab(num, s, 0.0, STR, (Array *) ap->sval);
1341: spdone:
1342: pfa = NULL;
1343: } else if (sep == ' ') {
1344: for (n = 0; ; ) {
1345: while (*s == ' ' || *s == '\t' || *s == '\n')
1346: s++;
1347: if (*s == 0)
1348: break;
1349: n++;
1350: t = s;
1351: do
1352: s++;
1353: while (*s!=' ' && *s!='\t' && *s!='\n' && *s!='\0');
1354: temp = *s;
1355: *s = '\0';
1.19 deraadt 1356: snprintf(num, sizeof num, "%d", n);
1.14 millert 1357: if (is_number(t))
1.13 kstailey 1358: setsymtab(num, t, atof(t), STR|NUM, (Array *) ap->sval);
1.1 tholo 1359: else
1360: setsymtab(num, t, 0.0, STR, (Array *) ap->sval);
1361: *s = temp;
1362: if (*s != 0)
1363: s++;
1364: }
1365: } else if (sep == 0) { /* new: split(s, a, "") => 1 char/elem */
1366: for (n = 0; *s != 0; s++) {
1367: char buf[2];
1368: n++;
1.19 deraadt 1369: snprintf(num, sizeof num, "%d", n);
1.1 tholo 1370: buf[0] = *s;
1371: buf[1] = 0;
1.17 millert 1372: if (isdigit((uschar)buf[0]))
1.1 tholo 1373: setsymtab(num, buf, atof(buf), STR|NUM, (Array *) ap->sval);
1374: else
1375: setsymtab(num, buf, 0.0, STR, (Array *) ap->sval);
1376: }
1377: } else if (*s != 0) {
1378: for (;;) {
1379: n++;
1380: t = s;
1381: while (*s != sep && *s != '\n' && *s != '\0')
1382: s++;
1383: temp = *s;
1384: *s = '\0';
1.19 deraadt 1385: snprintf(num, sizeof num, "%d", n);
1.14 millert 1386: if (is_number(t))
1.13 kstailey 1387: setsymtab(num, t, atof(t), STR|NUM, (Array *) ap->sval);
1.1 tholo 1388: else
1389: setsymtab(num, t, 0.0, STR, (Array *) ap->sval);
1390: *s = temp;
1391: if (*s++ == 0)
1392: break;
1393: }
1394: }
1395: tempfree(ap);
1396: tempfree(y);
1.43 fcambus 1397: free(origs);
1.49 ! millert 1398: free(origfs);
1.1 tholo 1399: x = gettemp();
1400: x->tval = NUM;
1401: x->fval = n;
1402: return(x);
1403: }
1404:
1405: Cell *condexpr(Node **a, int n) /* a[0] ? a[1] : a[2] */
1406: {
1407: Cell *x;
1408:
1409: x = execute(a[0]);
1410: if (istrue(x)) {
1411: tempfree(x);
1412: x = execute(a[1]);
1413: } else {
1414: tempfree(x);
1415: x = execute(a[2]);
1416: }
1417: return(x);
1418: }
1419:
1420: Cell *ifstat(Node **a, int n) /* if (a[0]) a[1]; else a[2] */
1421: {
1422: Cell *x;
1423:
1424: x = execute(a[0]);
1425: if (istrue(x)) {
1426: tempfree(x);
1427: x = execute(a[1]);
1428: } else if (a[2] != 0) {
1429: tempfree(x);
1430: x = execute(a[2]);
1431: }
1432: return(x);
1433: }
1434:
1435: Cell *whilestat(Node **a, int n) /* while (a[0]) a[1] */
1436: {
1437: Cell *x;
1438:
1439: for (;;) {
1440: x = execute(a[0]);
1441: if (!istrue(x))
1442: return(x);
1443: tempfree(x);
1444: x = execute(a[1]);
1445: if (isbreak(x)) {
1.15 millert 1446: x = True;
1.1 tholo 1447: return(x);
1448: }
1449: if (isnext(x) || isexit(x) || isret(x))
1450: return(x);
1451: tempfree(x);
1452: }
1453: }
1454:
1455: Cell *dostat(Node **a, int n) /* do a[0]; while(a[1]) */
1456: {
1457: Cell *x;
1458:
1459: for (;;) {
1460: x = execute(a[0]);
1461: if (isbreak(x))
1.15 millert 1462: return True;
1.17 millert 1463: if (isnext(x) || isexit(x) || isret(x))
1.1 tholo 1464: return(x);
1465: tempfree(x);
1466: x = execute(a[1]);
1467: if (!istrue(x))
1468: return(x);
1469: tempfree(x);
1470: }
1471: }
1472:
1473: Cell *forstat(Node **a, int n) /* for (a[0]; a[1]; a[2]) a[3] */
1474: {
1475: Cell *x;
1476:
1477: x = execute(a[0]);
1478: tempfree(x);
1479: for (;;) {
1480: if (a[1]!=0) {
1481: x = execute(a[1]);
1482: if (!istrue(x)) return(x);
1483: else tempfree(x);
1484: }
1485: x = execute(a[3]);
1486: if (isbreak(x)) /* turn off break */
1.15 millert 1487: return True;
1.1 tholo 1488: if (isnext(x) || isexit(x) || isret(x))
1489: return(x);
1490: tempfree(x);
1491: x = execute(a[2]);
1492: tempfree(x);
1493: }
1494: }
1495:
1496: Cell *instat(Node **a, int n) /* for (a[0] in a[1]) a[2] */
1497: {
1498: Cell *x, *vp, *arrayp, *cp, *ncp;
1499: Array *tp;
1500: int i;
1501:
1502: vp = execute(a[0]);
1503: arrayp = execute(a[1]);
1504: if (!isarr(arrayp)) {
1.15 millert 1505: return True;
1.1 tholo 1506: }
1507: tp = (Array *) arrayp->sval;
1508: tempfree(arrayp);
1509: for (i = 0; i < tp->size; i++) { /* this routine knows too much */
1510: for (cp = tp->tab[i]; cp != NULL; cp = ncp) {
1511: setsval(vp, cp->nval);
1512: ncp = cp->cnext;
1513: x = execute(a[2]);
1514: if (isbreak(x)) {
1515: tempfree(vp);
1.15 millert 1516: return True;
1.1 tholo 1517: }
1518: if (isnext(x) || isexit(x) || isret(x)) {
1519: tempfree(vp);
1520: return(x);
1521: }
1522: tempfree(x);
1523: }
1524: }
1.15 millert 1525: return True;
1.1 tholo 1526: }
1527:
1528: Cell *bltin(Node **a, int n) /* builtin functions. a[0] is type, a[1] is arg list */
1529: {
1530: Cell *x, *y;
1531: Awkfloat u;
1532: int t;
1.33 millert 1533: Awkfloat tmp;
1.13 kstailey 1534: char *p, *buf;
1.1 tholo 1535: Node *nextarg;
1536: FILE *fp;
1.47 millert 1537: int status = 0;
1.1 tholo 1538:
1.15 millert 1539: t = ptoi(a[0]);
1.1 tholo 1540: x = execute(a[1]);
1541: nextarg = a[1]->nnext;
1542: switch (t) {
1543: case FLENGTH:
1.18 millert 1544: if (isarr(x))
1545: u = ((Array *) x->sval)->nelem; /* GROT. should be function*/
1546: else
1547: u = strlen(getsval(x));
1548: break;
1.1 tholo 1549: case FLOG:
1.45 guenther 1550: errno = 0;
1.1 tholo 1551: u = errcheck(log(getfval(x)), "log"); break;
1552: case FINT:
1553: modf(getfval(x), &u); break;
1554: case FEXP:
1.45 guenther 1555: errno = 0;
1.1 tholo 1556: u = errcheck(exp(getfval(x)), "exp"); break;
1557: case FSQRT:
1.45 guenther 1558: errno = 0;
1.1 tholo 1559: u = errcheck(sqrt(getfval(x)), "sqrt"); break;
1560: case FSIN:
1561: u = sin(getfval(x)); break;
1562: case FCOS:
1563: u = cos(getfval(x)); break;
1564: case FATAN:
1565: if (nextarg == 0) {
1.16 millert 1566: WARNING("atan2 requires two arguments; returning 1.0");
1.1 tholo 1567: u = 1.0;
1568: } else {
1569: y = execute(a[1]->nnext);
1570: u = atan2(getfval(x), getfval(y));
1571: tempfree(y);
1572: nextarg = nextarg->nnext;
1573: }
1.29 pyr 1574: break;
1575: case FCOMPL:
1576: u = ~((int)getfval(x));
1577: break;
1578: case FAND:
1579: if (nextarg == 0) {
1580: WARNING("and requires two arguments; returning 0");
1581: u = 0;
1582: break;
1583: }
1584: y = execute(a[1]->nnext);
1585: u = ((int)getfval(x)) & ((int)getfval(y));
1586: tempfree(y);
1587: nextarg = nextarg->nnext;
1588: break;
1589: case FFOR:
1590: if (nextarg == 0) {
1591: WARNING("or requires two arguments; returning 0");
1592: u = 0;
1593: break;
1594: }
1595: y = execute(a[1]->nnext);
1596: u = ((int)getfval(x)) | ((int)getfval(y));
1597: tempfree(y);
1598: nextarg = nextarg->nnext;
1599: break;
1600: case FXOR:
1601: if (nextarg == 0) {
1.41 ajacouto 1602: WARNING("xor requires two arguments; returning 0");
1.29 pyr 1603: u = 0;
1604: break;
1605: }
1606: y = execute(a[1]->nnext);
1607: u = ((int)getfval(x)) ^ ((int)getfval(y));
1608: tempfree(y);
1609: nextarg = nextarg->nnext;
1610: break;
1611: case FLSHIFT:
1612: if (nextarg == 0) {
1.41 ajacouto 1613: WARNING("lshift requires two arguments; returning 0");
1.29 pyr 1614: u = 0;
1615: break;
1616: }
1617: y = execute(a[1]->nnext);
1618: u = ((int)getfval(x)) << ((int)getfval(y));
1619: tempfree(y);
1620: nextarg = nextarg->nnext;
1621: break;
1622: case FRSHIFT:
1623: if (nextarg == 0) {
1.41 ajacouto 1624: WARNING("rshift requires two arguments; returning 0");
1.29 pyr 1625: u = 0;
1626: break;
1627: }
1628: y = execute(a[1]->nnext);
1629: u = ((int)getfval(x)) >> ((int)getfval(y));
1630: tempfree(y);
1631: nextarg = nextarg->nnext;
1.1 tholo 1632: break;
1633: case FSYSTEM:
1634: fflush(stdout); /* in case something is buffered already */
1.47 millert 1635: status = system(getsval(x));
1636: u = status;
1637: if (status != -1) {
1638: if (WIFEXITED(status)) {
1639: u = WEXITSTATUS(status);
1640: } else if (WIFSIGNALED(status)) {
1641: u = WTERMSIG(status) + 256;
1642: #ifdef WCOREDUMP
1643: if (WCOREDUMP(status))
1644: u += 256;
1645: #endif
1646: } else /* something else?!? */
1647: u = 0;
1648: }
1.1 tholo 1649: break;
1650: case FRAND:
1.40 tb 1651: u = (Awkfloat) (random() & RAND_MAX) / ((u_int)RAND_MAX + 1);
1.1 tholo 1652: break;
1653: case FSRAND:
1.39 deraadt 1654: if (isrec(x)) { /* no argument provided */
1655: u = time(NULL);
1656: tmp = u;
1657: srandom((unsigned int) u);
1658: } else {
1.33 millert 1659: u = getfval(x);
1660: tmp = u;
1.37 deraadt 1661: srandom_deterministic((unsigned int) u);
1.24 millert 1662: }
1.39 deraadt 1663: u = srand_seed;
1664: srand_seed = tmp;
1.1 tholo 1665: break;
1666: case FTOUPPER:
1667: case FTOLOWER:
1.13 kstailey 1668: buf = tostring(getsval(x));
1.1 tholo 1669: if (t == FTOUPPER) {
1670: for (p = buf; *p; p++)
1.17 millert 1671: if (islower((uschar) *p))
1.25 millert 1672: *p = toupper((uschar)*p);
1.1 tholo 1673: } else {
1674: for (p = buf; *p; p++)
1.17 millert 1675: if (isupper((uschar) *p))
1.25 millert 1676: *p = tolower((uschar)*p);
1.1 tholo 1677: }
1678: tempfree(x);
1679: x = gettemp();
1680: setsval(x, buf);
1.13 kstailey 1681: free(buf);
1.1 tholo 1682: return x;
1683: case FFLUSH:
1.18 millert 1684: if (isrec(x) || strlen(getsval(x)) == 0) {
1685: flush_all(); /* fflush() or fflush("") -> all */
1686: u = 0;
1687: } else if ((fp = openfile(FFLUSH, getsval(x))) == NULL)
1.1 tholo 1688: u = EOF;
1689: else
1690: u = fflush(fp);
1691: break;
1692: default: /* can't happen */
1.16 millert 1693: FATAL("illegal function type %d", t);
1.1 tholo 1694: break;
1695: }
1696: tempfree(x);
1697: x = gettemp();
1698: setfval(x, u);
1699: if (nextarg != 0) {
1.16 millert 1700: WARNING("warning: function has too many arguments");
1.1 tholo 1701: for ( ; nextarg; nextarg = nextarg->nnext)
1702: execute(nextarg);
1703: }
1704: return(x);
1705: }
1706:
1707: Cell *printstat(Node **a, int n) /* print a[0] */
1708: {
1709: Node *x;
1710: Cell *y;
1711: FILE *fp;
1712:
1713: if (a[1] == 0) /* a[1] is redirection operator, a[2] is file */
1714: fp = stdout;
1715: else
1.15 millert 1716: fp = redirect(ptoi(a[1]), a[2]);
1.1 tholo 1717: for (x = a[0]; x != NULL; x = x->nnext) {
1718: y = execute(x);
1.18 millert 1719: fputs(getpssval(y), fp);
1.1 tholo 1720: tempfree(y);
1721: if (x->nnext == NULL)
1.49 ! millert 1722: fputs(getsval(orsloc), fp);
1.1 tholo 1723: else
1.49 ! millert 1724: fputs(getsval(ofsloc), fp);
1.1 tholo 1725: }
1726: if (a[1] != 0)
1727: fflush(fp);
1728: if (ferror(fp))
1.16 millert 1729: FATAL("write error on %s", filename(fp));
1.15 millert 1730: return(True);
1.1 tholo 1731: }
1732:
1733: Cell *nullproc(Node **a, int n)
1734: {
1735: return 0;
1736: }
1737:
1738:
1739: FILE *redirect(int a, Node *b) /* set up all i/o redirections */
1740: {
1741: FILE *fp;
1742: Cell *x;
1743: char *fname;
1744:
1745: x = execute(b);
1746: fname = getsval(x);
1747: fp = openfile(a, fname);
1748: if (fp == NULL)
1.16 millert 1749: FATAL("can't open file %s", fname);
1.1 tholo 1750: tempfree(x);
1751: return fp;
1752: }
1753:
1754: struct files {
1755: FILE *fp;
1.18 millert 1756: const char *fname;
1.1 tholo 1757: int mode; /* '|', 'a', 'w' => LE/LT, GT */
1.33 millert 1758: } *files;
1759:
1760: int nfiles;
1.1 tholo 1761:
1.16 millert 1762: void stdinit(void) /* in case stdin, etc., are not constants */
1763: {
1.33 millert 1764: nfiles = FOPEN_MAX;
1765: files = calloc(nfiles, sizeof(*files));
1766: if (files == NULL)
1767: FATAL("can't allocate file memory for %u files", nfiles);
1768: files[0].fp = stdin;
1769: files[0].fname = "/dev/stdin";
1770: files[0].mode = LT;
1771: files[1].fp = stdout;
1772: files[1].fname = "/dev/stdout";
1773: files[1].mode = GT;
1774: files[2].fp = stderr;
1775: files[2].fname = "/dev/stderr";
1776: files[2].mode = GT;
1.16 millert 1777: }
1778:
1.18 millert 1779: FILE *openfile(int a, const char *us)
1.1 tholo 1780: {
1.18 millert 1781: const char *s = us;
1.1 tholo 1782: int i, m;
1783: FILE *fp = 0;
1784:
1785: if (*s == '\0')
1.16 millert 1786: FATAL("null file name in print or getline");
1.33 millert 1787: for (i=0; i < nfiles; i++)
1.13 kstailey 1788: if (files[i].fname && strcmp(s, files[i].fname) == 0) {
1.1 tholo 1789: if (a == files[i].mode || (a==APPEND && files[i].mode==GT))
1790: return files[i].fp;
1.13 kstailey 1791: if (a == FFLUSH)
1792: return files[i].fp;
1793: }
1794: if (a == FFLUSH) /* didn't find it, so don't create it! */
1795: return NULL;
1796:
1.33 millert 1797: for (i=0; i < nfiles; i++)
1.1 tholo 1798: if (files[i].fp == 0)
1799: break;
1.33 millert 1800: if (i >= nfiles) {
1801: struct files *nf;
1802: int nnf = nfiles + FOPEN_MAX;
1.35 doug 1803: nf = reallocarray(files, nnf, sizeof(*nf));
1.33 millert 1804: if (nf == NULL)
1805: FATAL("cannot grow files for %s and %d files", s, nnf);
1806: memset(&nf[nfiles], 0, FOPEN_MAX * sizeof(*nf));
1807: nfiles = nnf;
1808: files = nf;
1809: }
1.1 tholo 1810: fflush(stdout); /* force a semblance of order */
1811: m = a;
1812: if (a == GT) {
1813: fp = fopen(s, "w");
1814: } else if (a == APPEND) {
1815: fp = fopen(s, "a");
1816: m = GT; /* so can mix > and >> */
1817: } else if (a == '|') { /* output pipe */
1818: fp = popen(s, "w");
1819: } else if (a == LE) { /* input pipe */
1820: fp = popen(s, "r");
1821: } else if (a == LT) { /* getline <file */
1822: fp = strcmp(s, "-") == 0 ? stdin : fopen(s, "r"); /* "-" is stdin */
1823: } else /* can't happen */
1.16 millert 1824: FATAL("illegal redirection %d", a);
1.1 tholo 1825: if (fp != NULL) {
1826: files[i].fname = tostring(s);
1827: files[i].fp = fp;
1828: files[i].mode = m;
1829: }
1830: return fp;
1831: }
1832:
1.18 millert 1833: const char *filename(FILE *fp)
1.1 tholo 1834: {
1835: int i;
1836:
1.33 millert 1837: for (i = 0; i < nfiles; i++)
1.1 tholo 1838: if (fp == files[i].fp)
1839: return files[i].fname;
1840: return "???";
1841: }
1842:
1843: Cell *closefile(Node **a, int n)
1844: {
1845: Cell *x;
1846: int i, stat;
1847:
1848: x = execute(a[0]);
1849: getsval(x);
1.17 millert 1850: stat = -1;
1.33 millert 1851: for (i = 0; i < nfiles; i++) {
1.1 tholo 1852: if (files[i].fname && strcmp(x->sval, files[i].fname) == 0) {
1853: if (ferror(files[i].fp))
1.16 millert 1854: WARNING( "i/o error occurred on %s", files[i].fname );
1.1 tholo 1855: if (files[i].mode == '|' || files[i].mode == LE)
1856: stat = pclose(files[i].fp);
1857: else
1858: stat = fclose(files[i].fp);
1859: if (stat == EOF)
1.16 millert 1860: WARNING( "i/o error occurred closing %s", files[i].fname );
1.1 tholo 1861: if (i > 2) /* don't do /dev/std... */
1862: xfree(files[i].fname);
1863: files[i].fname = NULL; /* watch out for ref thru this */
1864: files[i].fp = NULL;
1865: }
1.17 millert 1866: }
1.1 tholo 1867: tempfree(x);
1.17 millert 1868: x = gettemp();
1869: setfval(x, (Awkfloat) stat);
1870: return(x);
1.1 tholo 1871: }
1872:
1873: void closeall(void)
1874: {
1875: int i, stat;
1876:
1.17 millert 1877: for (i = 0; i < FOPEN_MAX; i++) {
1.1 tholo 1878: if (files[i].fp) {
1879: if (ferror(files[i].fp))
1.16 millert 1880: WARNING( "i/o error occurred on %s", files[i].fname );
1.1 tholo 1881: if (files[i].mode == '|' || files[i].mode == LE)
1882: stat = pclose(files[i].fp);
1883: else
1884: stat = fclose(files[i].fp);
1885: if (stat == EOF)
1.16 millert 1886: WARNING( "i/o error occurred while closing %s", files[i].fname );
1.1 tholo 1887: }
1.17 millert 1888: }
1.18 millert 1889: }
1890:
1891: void flush_all(void)
1892: {
1893: int i;
1894:
1.33 millert 1895: for (i = 0; i < nfiles; i++)
1.18 millert 1896: if (files[i].fp)
1897: fflush(files[i].fp);
1.1 tholo 1898: }
1899:
1.13 kstailey 1900: void backsub(char **pb_ptr, char **sptr_ptr);
1.1 tholo 1901:
1902: Cell *sub(Node **a, int nnn) /* substitute command */
1903: {
1904: char *sptr, *pb, *q;
1905: Cell *x, *y, *result;
1.13 kstailey 1906: char *t, *buf;
1.1 tholo 1907: fa *pfa;
1.13 kstailey 1908: int bufsz = recsize;
1.1 tholo 1909:
1.15 millert 1910: if ((buf = (char *) malloc(bufsz)) == NULL)
1.16 millert 1911: FATAL("out of memory in sub");
1.1 tholo 1912: x = execute(a[3]); /* target string */
1913: t = getsval(x);
1914: if (a[0] == 0) /* 0 => a[1] is already-compiled regexpr */
1915: pfa = (fa *) a[1]; /* regular expression */
1916: else {
1917: y = execute(a[1]);
1918: pfa = makedfa(getsval(y), 1);
1919: tempfree(y);
1920: }
1921: y = execute(a[2]); /* replacement string */
1.15 millert 1922: result = False;
1.1 tholo 1923: if (pmatch(pfa, t)) {
1.13 kstailey 1924: sptr = t;
1925: adjbuf(&buf, &bufsz, 1+patbeg-sptr, recsize, 0, "sub");
1.1 tholo 1926: pb = buf;
1927: while (sptr < patbeg)
1928: *pb++ = *sptr++;
1929: sptr = getsval(y);
1.13 kstailey 1930: while (*sptr != 0) {
1931: adjbuf(&buf, &bufsz, 5+pb-buf, recsize, &pb, "sub");
1932: if (*sptr == '\\') {
1933: backsub(&pb, &sptr);
1.1 tholo 1934: } else if (*sptr == '&') {
1935: sptr++;
1.13 kstailey 1936: adjbuf(&buf, &bufsz, 1+patlen+pb-buf, recsize, &pb, "sub");
1.1 tholo 1937: for (q = patbeg; q < patbeg+patlen; )
1938: *pb++ = *q++;
1939: } else
1940: *pb++ = *sptr++;
1.13 kstailey 1941: }
1.1 tholo 1942: *pb = '\0';
1.13 kstailey 1943: if (pb > buf + bufsz)
1.16 millert 1944: FATAL("sub result1 %.30s too big; can't happen", buf);
1.1 tholo 1945: sptr = patbeg + patlen;
1.13 kstailey 1946: if ((patlen == 0 && *patbeg) || (patlen && *(sptr-1))) {
1947: adjbuf(&buf, &bufsz, 1+strlen(sptr)+pb-buf, 0, &pb, "sub");
1.1 tholo 1948: while ((*pb++ = *sptr++) != 0)
1949: ;
1.13 kstailey 1950: }
1951: if (pb > buf + bufsz)
1.16 millert 1952: FATAL("sub result2 %.30s too big; can't happen", buf);
1.13 kstailey 1953: setsval(x, buf); /* BUG: should be able to avoid copy */
1.23 millert 1954: result = True;
1.1 tholo 1955: }
1956: tempfree(x);
1957: tempfree(y);
1.13 kstailey 1958: free(buf);
1.1 tholo 1959: return result;
1960: }
1961:
1962: Cell *gsub(Node **a, int nnn) /* global substitute */
1963: {
1964: Cell *x, *y;
1.13 kstailey 1965: char *rptr, *sptr, *t, *pb, *q;
1966: char *buf;
1.1 tholo 1967: fa *pfa;
1968: int mflag, tempstat, num;
1.13 kstailey 1969: int bufsz = recsize;
1.1 tholo 1970:
1.15 millert 1971: if ((buf = (char *) malloc(bufsz)) == NULL)
1.16 millert 1972: FATAL("out of memory in gsub");
1.1 tholo 1973: mflag = 0; /* if mflag == 0, can replace empty string */
1974: num = 0;
1975: x = execute(a[3]); /* target string */
1976: t = getsval(x);
1977: if (a[0] == 0) /* 0 => a[1] is already-compiled regexpr */
1978: pfa = (fa *) a[1]; /* regular expression */
1979: else {
1980: y = execute(a[1]);
1981: pfa = makedfa(getsval(y), 1);
1982: tempfree(y);
1983: }
1984: y = execute(a[2]); /* replacement string */
1985: if (pmatch(pfa, t)) {
1986: tempstat = pfa->initstat;
1987: pfa->initstat = 2;
1988: pb = buf;
1989: rptr = getsval(y);
1990: do {
1991: if (patlen == 0 && *patbeg != 0) { /* matched empty string */
1992: if (mflag == 0) { /* can replace empty */
1993: num++;
1994: sptr = rptr;
1.13 kstailey 1995: while (*sptr != 0) {
1996: adjbuf(&buf, &bufsz, 5+pb-buf, recsize, &pb, "gsub");
1997: if (*sptr == '\\') {
1998: backsub(&pb, &sptr);
1.1 tholo 1999: } else if (*sptr == '&') {
2000: sptr++;
1.13 kstailey 2001: adjbuf(&buf, &bufsz, 1+patlen+pb-buf, recsize, &pb, "gsub");
1.1 tholo 2002: for (q = patbeg; q < patbeg+patlen; )
2003: *pb++ = *q++;
2004: } else
2005: *pb++ = *sptr++;
1.13 kstailey 2006: }
1.1 tholo 2007: }
2008: if (*t == 0) /* at end */
2009: goto done;
1.13 kstailey 2010: adjbuf(&buf, &bufsz, 2+pb-buf, recsize, &pb, "gsub");
1.1 tholo 2011: *pb++ = *t++;
1.13 kstailey 2012: if (pb > buf + bufsz) /* BUG: not sure of this test */
1.16 millert 2013: FATAL("gsub result0 %.30s too big; can't happen", buf);
1.1 tholo 2014: mflag = 0;
2015: }
2016: else { /* matched nonempty string */
2017: num++;
2018: sptr = t;
1.13 kstailey 2019: adjbuf(&buf, &bufsz, 1+(patbeg-sptr)+pb-buf, recsize, &pb, "gsub");
2020: while (sptr < patbeg)
1.1 tholo 2021: *pb++ = *sptr++;
2022: sptr = rptr;
1.13 kstailey 2023: while (*sptr != 0) {
2024: adjbuf(&buf, &bufsz, 5+pb-buf, recsize, &pb, "gsub");
2025: if (*sptr == '\\') {
2026: backsub(&pb, &sptr);
1.1 tholo 2027: } else if (*sptr == '&') {
2028: sptr++;
1.13 kstailey 2029: adjbuf(&buf, &bufsz, 1+patlen+pb-buf, recsize, &pb, "gsub");
1.1 tholo 2030: for (q = patbeg; q < patbeg+patlen; )
2031: *pb++ = *q++;
2032: } else
2033: *pb++ = *sptr++;
1.13 kstailey 2034: }
1.1 tholo 2035: t = patbeg + patlen;
2036: if (patlen == 0 || *t == 0 || *(t-1) == 0)
2037: goto done;
1.13 kstailey 2038: if (pb > buf + bufsz)
1.16 millert 2039: FATAL("gsub result1 %.30s too big; can't happen", buf);
1.1 tholo 2040: mflag = 1;
2041: }
2042: } while (pmatch(pfa,t));
2043: sptr = t;
1.13 kstailey 2044: adjbuf(&buf, &bufsz, 1+strlen(sptr)+pb-buf, 0, &pb, "gsub");
1.1 tholo 2045: while ((*pb++ = *sptr++) != 0)
2046: ;
1.31 millert 2047: done: if (pb < buf + bufsz)
2048: *pb = '\0';
2049: else if (*(pb-1) != '\0')
2050: FATAL("gsub result2 %.30s truncated; can't happen", buf);
1.13 kstailey 2051: setsval(x, buf); /* BUG: should be able to avoid copy + free */
1.1 tholo 2052: pfa->initstat = tempstat;
2053: }
2054: tempfree(x);
2055: tempfree(y);
2056: x = gettemp();
2057: x->tval = NUM;
2058: x->fval = num;
1.13 kstailey 2059: free(buf);
1.1 tholo 2060: return(x);
1.13 kstailey 2061: }
2062:
2063: void backsub(char **pb_ptr, char **sptr_ptr) /* handle \\& variations */
2064: { /* sptr[0] == '\\' */
2065: char *pb = *pb_ptr, *sptr = *sptr_ptr;
2066:
2067: if (sptr[1] == '\\') {
2068: if (sptr[2] == '\\' && sptr[3] == '&') { /* \\\& -> \& */
2069: *pb++ = '\\';
2070: *pb++ = '&';
2071: sptr += 4;
2072: } else if (sptr[2] == '&') { /* \\& -> \ + matched */
2073: *pb++ = '\\';
2074: sptr += 2;
2075: } else { /* \\x -> \\x */
2076: *pb++ = *sptr++;
2077: *pb++ = *sptr++;
2078: }
2079: } else if (sptr[1] == '&') { /* literal & */
2080: sptr++;
2081: *pb++ = *sptr++;
2082: } else /* literal \ */
2083: *pb++ = *sptr++;
2084:
2085: *pb_ptr = pb;
2086: *sptr_ptr = sptr;
1.1 tholo 2087: }