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