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