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