2012-12-22 10:35:39 -05:00
|
|
|
/****************************************************************
|
|
|
|
Copyright (C) Lucent Technologies 1997
|
|
|
|
All Rights Reserved
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and distribute this software and
|
|
|
|
its documentation for any purpose and without fee is hereby
|
|
|
|
granted, provided that the above copyright notice appear in all
|
|
|
|
copies and that both that the copyright notice and this
|
|
|
|
permission notice and warranty disclaimer appear in supporting
|
|
|
|
documentation, and that the name Lucent Technologies or any of
|
|
|
|
its entities not be used in advertising or publicity pertaining
|
|
|
|
to distribution of the software without specific, written prior
|
|
|
|
permission.
|
|
|
|
|
|
|
|
LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
|
|
|
|
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
|
|
|
|
IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
|
|
|
|
SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
|
|
|
|
IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
|
|
|
|
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
|
|
|
|
THIS SOFTWARE.
|
|
|
|
****************************************************************/
|
|
|
|
|
|
|
|
#define DEBUG
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
2020-06-12 07:16:12 -04:00
|
|
|
#include <errno.h>
|
2020-02-06 15:27:31 -05:00
|
|
|
#include <wchar.h>
|
|
|
|
#include <wctype.h>
|
2020-01-22 04:10:59 -05:00
|
|
|
#include <fcntl.h>
|
2012-12-22 10:35:39 -05:00
|
|
|
#include <setjmp.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
2018-08-22 13:40:26 -04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2012-12-22 10:35:39 -05:00
|
|
|
#include "awk.h"
|
2020-07-30 10:12:45 -04:00
|
|
|
#include "awkgram.tab.h"
|
2012-12-22 10:35:39 -05:00
|
|
|
|
2020-02-06 15:27:31 -05:00
|
|
|
static void stdinit(void);
|
|
|
|
static void flush_all(void);
|
2012-12-22 10:35:39 -05:00
|
|
|
|
2020-02-06 15:27:31 -05:00
|
|
|
#if 1
|
|
|
|
#define tempfree(x) do { if (istemp(x)) tfree(x); } while (/*CONSTCOND*/0)
|
|
|
|
#else
|
2012-12-22 10:35:39 -05:00
|
|
|
void tempfree(Cell *p) {
|
|
|
|
if (p->ctype == OCELL && (p->csub < CUNK || p->csub > CFREE)) {
|
|
|
|
WARNING("bad csub %d in Cell %d %s",
|
|
|
|
p->csub, p->ctype, p->sval);
|
|
|
|
}
|
|
|
|
if (istemp(p))
|
|
|
|
tfree(p);
|
|
|
|
}
|
2020-02-06 15:27:31 -05:00
|
|
|
#endif
|
2012-12-22 10:35:39 -05:00
|
|
|
|
|
|
|
/* do we really need these? */
|
|
|
|
/* #ifdef _NFILE */
|
|
|
|
/* #ifndef FOPEN_MAX */
|
|
|
|
/* #define FOPEN_MAX _NFILE */
|
|
|
|
/* #endif */
|
|
|
|
/* #endif */
|
|
|
|
/* */
|
|
|
|
/* #ifndef FOPEN_MAX */
|
|
|
|
/* #define FOPEN_MAX 40 */ /* max number of open files */
|
|
|
|
/* #endif */
|
|
|
|
/* */
|
|
|
|
/* #ifndef RAND_MAX */
|
|
|
|
/* #define RAND_MAX 32767 */ /* all that ansi guarantees */
|
|
|
|
/* #endif */
|
|
|
|
|
|
|
|
jmp_buf env;
|
|
|
|
extern int pairstack[];
|
|
|
|
extern Awkfloat srand_seed;
|
|
|
|
|
|
|
|
Node *winner = NULL; /* root of parse tree */
|
|
|
|
Cell *tmps; /* free temporary cells for execution */
|
|
|
|
|
2020-02-19 13:44:49 -05:00
|
|
|
static Cell truecell ={ OBOOL, BTRUE, 0, 0, 1.0, NUM, NULL, NULL };
|
2012-12-22 10:35:39 -05:00
|
|
|
Cell *True = &truecell;
|
2020-02-19 13:44:49 -05:00
|
|
|
static Cell falsecell ={ OBOOL, BFALSE, 0, 0, 0.0, NUM, NULL, NULL };
|
2012-12-22 10:35:39 -05:00
|
|
|
Cell *False = &falsecell;
|
2020-02-19 13:44:49 -05:00
|
|
|
static Cell breakcell ={ OJUMP, JBREAK, 0, 0, 0.0, NUM, NULL, NULL };
|
2012-12-22 10:35:39 -05:00
|
|
|
Cell *jbreak = &breakcell;
|
2020-02-19 13:44:49 -05:00
|
|
|
static Cell contcell ={ OJUMP, JCONT, 0, 0, 0.0, NUM, NULL, NULL };
|
2012-12-22 10:35:39 -05:00
|
|
|
Cell *jcont = &contcell;
|
2020-02-19 13:44:49 -05:00
|
|
|
static Cell nextcell ={ OJUMP, JNEXT, 0, 0, 0.0, NUM, NULL, NULL };
|
2012-12-22 10:35:39 -05:00
|
|
|
Cell *jnext = &nextcell;
|
2020-02-19 13:44:49 -05:00
|
|
|
static Cell nextfilecell ={ OJUMP, JNEXTFILE, 0, 0, 0.0, NUM, NULL, NULL };
|
2012-12-22 10:35:39 -05:00
|
|
|
Cell *jnextfile = &nextfilecell;
|
2020-02-19 13:44:49 -05:00
|
|
|
static Cell exitcell ={ OJUMP, JEXIT, 0, 0, 0.0, NUM, NULL, NULL };
|
2012-12-22 10:35:39 -05:00
|
|
|
Cell *jexit = &exitcell;
|
2020-02-19 13:44:49 -05:00
|
|
|
static Cell retcell ={ OJUMP, JRET, 0, 0, 0.0, NUM, NULL, NULL };
|
2012-12-22 10:35:39 -05:00
|
|
|
Cell *jret = &retcell;
|
2020-02-19 13:44:49 -05:00
|
|
|
static Cell tempcell ={ OCELL, CTEMP, 0, EMPTY, 0.0, NUM|STR|DONTFREE, NULL, NULL };
|
2012-12-22 10:35:39 -05:00
|
|
|
|
|
|
|
Node *curnode = NULL; /* the node being executed, for debugging */
|
|
|
|
|
|
|
|
/* buffer memory management */
|
|
|
|
int adjbuf(char **pbuf, int *psiz, int minlen, int quantum, char **pbptr,
|
|
|
|
const char *whatrtn)
|
|
|
|
/* pbuf: address of pointer to buffer being managed
|
|
|
|
* psiz: address of buffer size variable
|
|
|
|
* minlen: minimum length of buffer needed
|
|
|
|
* quantum: buffer size quantum
|
|
|
|
* pbptr: address of movable pointer into buffer, or 0 if none
|
|
|
|
* whatrtn: name of the calling routine if failure should cause fatal error
|
|
|
|
*
|
|
|
|
* return 0 for realloc failure, !=0 for success
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
if (minlen > *psiz) {
|
|
|
|
char *tbuf;
|
|
|
|
int rminlen = quantum ? minlen % quantum : 0;
|
|
|
|
int boff = pbptr ? *pbptr - *pbuf : 0;
|
|
|
|
/* round up to next multiple of quantum */
|
|
|
|
if (rminlen)
|
|
|
|
minlen += quantum - rminlen;
|
2020-10-13 13:52:43 -04:00
|
|
|
tbuf = (char *) realloc(*pbuf, minlen);
|
2020-08-07 06:10:20 -04:00
|
|
|
DPRINTF("adjbuf %s: %d %d (pbuf=%p, tbuf=%p)\n", whatrtn, *psiz, minlen, (void*)*pbuf, (void*)tbuf);
|
2012-12-22 10:35:39 -05:00
|
|
|
if (tbuf == NULL) {
|
|
|
|
if (whatrtn)
|
|
|
|
FATAL("out of memory in %s", whatrtn);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*pbuf = tbuf;
|
|
|
|
*psiz = minlen;
|
|
|
|
if (pbptr)
|
|
|
|
*pbptr = tbuf + boff;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void run(Node *a) /* execution of parse tree starts here */
|
|
|
|
{
|
|
|
|
|
|
|
|
stdinit();
|
|
|
|
execute(a);
|
|
|
|
closeall();
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell *execute(Node *u) /* execute a node of the parse tree */
|
|
|
|
{
|
|
|
|
Cell *(*proc)(Node **, int);
|
|
|
|
Cell *x;
|
|
|
|
Node *a;
|
|
|
|
|
|
|
|
if (u == NULL)
|
|
|
|
return(True);
|
|
|
|
for (a = u; ; a = a->nnext) {
|
|
|
|
curnode = a;
|
|
|
|
if (isvalue(a)) {
|
|
|
|
x = (Cell *) (a->narg[0]);
|
|
|
|
if (isfld(x) && !donefld)
|
|
|
|
fldbld();
|
|
|
|
else if (isrec(x) && !donerec)
|
|
|
|
recbld();
|
|
|
|
return(x);
|
|
|
|
}
|
|
|
|
if (notlegal(a->nobj)) /* probably a Cell* but too risky to print */
|
|
|
|
FATAL("illegal statement");
|
|
|
|
proc = proctab[a->nobj-FIRSTTOKEN];
|
|
|
|
x = (*proc)(a->narg, a->nobj);
|
|
|
|
if (isfld(x) && !donefld)
|
|
|
|
fldbld();
|
|
|
|
else if (isrec(x) && !donerec)
|
|
|
|
recbld();
|
|
|
|
if (isexpr(a))
|
|
|
|
return(x);
|
|
|
|
if (isjump(x))
|
|
|
|
return(x);
|
|
|
|
if (a->nnext == NULL)
|
|
|
|
return(x);
|
|
|
|
tempfree(x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Cell *program(Node **a, int n) /* execute an awk program */
|
|
|
|
{ /* a[0] = BEGIN, a[1] = body, a[2] = END */
|
|
|
|
Cell *x;
|
|
|
|
|
|
|
|
if (setjmp(env) != 0)
|
|
|
|
goto ex;
|
|
|
|
if (a[0]) { /* BEGIN */
|
|
|
|
x = execute(a[0]);
|
|
|
|
if (isexit(x))
|
|
|
|
return(True);
|
|
|
|
if (isjump(x))
|
|
|
|
FATAL("illegal break, continue, next or nextfile from BEGIN");
|
|
|
|
tempfree(x);
|
|
|
|
}
|
|
|
|
if (a[1] || a[2])
|
2019-11-10 14:19:18 -05:00
|
|
|
while (getrec(&record, &recsize, true) > 0) {
|
2012-12-22 10:35:39 -05:00
|
|
|
x = execute(a[1]);
|
|
|
|
if (isexit(x))
|
|
|
|
break;
|
|
|
|
tempfree(x);
|
|
|
|
}
|
|
|
|
ex:
|
|
|
|
if (setjmp(env) != 0) /* handles exit within END */
|
|
|
|
goto ex1;
|
|
|
|
if (a[2]) { /* END */
|
|
|
|
x = execute(a[2]);
|
|
|
|
if (isbreak(x) || isnext(x) || iscont(x))
|
|
|
|
FATAL("illegal break, continue, next or nextfile from END");
|
|
|
|
tempfree(x);
|
|
|
|
}
|
|
|
|
ex1:
|
|
|
|
return(True);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Frame { /* stack frame for awk function calls */
|
|
|
|
int nargs; /* number of arguments in this call */
|
|
|
|
Cell *fcncell; /* pointer to Cell for function */
|
|
|
|
Cell **args; /* pointer to array of arguments after execute */
|
|
|
|
Cell *retval; /* return value */
|
|
|
|
};
|
|
|
|
|
|
|
|
#define NARGS 50 /* max args in a call */
|
|
|
|
|
|
|
|
struct Frame *frame = NULL; /* base of stack frames; dynamically allocated */
|
|
|
|
int nframe = 0; /* number of frames allocated */
|
2020-02-06 15:27:31 -05:00
|
|
|
struct Frame *frp = NULL; /* frame pointer. bottom level unused */
|
2012-12-22 10:35:39 -05:00
|
|
|
|
|
|
|
Cell *call(Node **a, int n) /* function call. very kludgy and fragile */
|
|
|
|
{
|
2020-02-19 13:44:49 -05:00
|
|
|
static const Cell newcopycell = { OCELL, CCOPY, 0, EMPTY, 0.0, NUM|STR|DONTFREE, NULL, NULL };
|
2012-12-22 10:35:39 -05:00
|
|
|
int i, ncall, ndef;
|
|
|
|
int freed = 0; /* handles potential double freeing when fcn & param share a tempcell */
|
|
|
|
Node *x;
|
|
|
|
Cell *args[NARGS], *oargs[NARGS]; /* BUG: fixed size arrays */
|
|
|
|
Cell *y, *z, *fcn;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
fcn = execute(a[0]); /* the function itself */
|
|
|
|
s = fcn->nval;
|
|
|
|
if (!isfcn(fcn))
|
|
|
|
FATAL("calling undefined function %s", s);
|
|
|
|
if (frame == NULL) {
|
2020-10-13 13:52:43 -04:00
|
|
|
frp = frame = (struct Frame *) calloc(nframe += 100, sizeof(*frame));
|
2012-12-22 10:35:39 -05:00
|
|
|
if (frame == NULL)
|
|
|
|
FATAL("out of space for stack frames calling %s", s);
|
|
|
|
}
|
|
|
|
for (ncall = 0, x = a[1]; x != NULL; x = x->nnext) /* args in call */
|
|
|
|
ncall++;
|
|
|
|
ndef = (int) fcn->fval; /* args in defn */
|
2020-06-25 14:32:34 -04:00
|
|
|
DPRINTF("calling %s, %d args (%d in defn), frp=%d\n", s, ncall, ndef, (int) (frp-frame));
|
2012-12-22 10:35:39 -05:00
|
|
|
if (ncall > ndef)
|
|
|
|
WARNING("function %s called with %d args, uses only %d",
|
|
|
|
s, ncall, ndef);
|
|
|
|
if (ncall + ndef > NARGS)
|
|
|
|
FATAL("function %s has %d arguments, limit %d", s, ncall+ndef, NARGS);
|
|
|
|
for (i = 0, x = a[1]; x != NULL; i++, x = x->nnext) { /* get call args */
|
2020-06-25 14:32:34 -04:00
|
|
|
DPRINTF("evaluate args[%d], frp=%d:\n", i, (int) (frp-frame));
|
2012-12-22 10:35:39 -05:00
|
|
|
y = execute(x);
|
|
|
|
oargs[i] = y;
|
2020-06-25 14:32:34 -04:00
|
|
|
DPRINTF("args[%d]: %s %f <%s>, t=%o\n",
|
|
|
|
i, NN(y->nval), y->fval, isarr(y) ? "(array)" : NN(y->sval), y->tval);
|
2012-12-22 10:35:39 -05:00
|
|
|
if (isfcn(y))
|
|
|
|
FATAL("can't use function %s as argument in %s", y->nval, s);
|
|
|
|
if (isarr(y))
|
|
|
|
args[i] = y; /* arrays by ref */
|
|
|
|
else
|
|
|
|
args[i] = copycell(y);
|
|
|
|
tempfree(y);
|
|
|
|
}
|
|
|
|
for ( ; i < ndef; i++) { /* add null args for ones not provided */
|
|
|
|
args[i] = gettemp();
|
|
|
|
*args[i] = newcopycell;
|
|
|
|
}
|
2020-02-06 15:27:31 -05:00
|
|
|
frp++; /* now ok to up frame */
|
|
|
|
if (frp >= frame + nframe) {
|
|
|
|
int dfp = frp - frame; /* old index */
|
2020-10-13 13:52:43 -04:00
|
|
|
frame = (struct Frame *) realloc(frame, (nframe += 100) * sizeof(*frame));
|
2012-12-22 10:35:39 -05:00
|
|
|
if (frame == NULL)
|
|
|
|
FATAL("out of space for stack frames in %s", s);
|
2020-02-06 15:27:31 -05:00
|
|
|
frp = frame + dfp;
|
2012-12-22 10:35:39 -05:00
|
|
|
}
|
2020-02-06 15:27:31 -05:00
|
|
|
frp->fcncell = fcn;
|
|
|
|
frp->args = args;
|
|
|
|
frp->nargs = ndef; /* number defined with (excess are locals) */
|
|
|
|
frp->retval = gettemp();
|
2012-12-22 10:35:39 -05:00
|
|
|
|
2020-06-25 14:32:34 -04:00
|
|
|
DPRINTF("start exec of %s, frp=%d\n", s, (int) (frp-frame));
|
2012-12-22 10:35:39 -05:00
|
|
|
y = execute((Node *)(fcn->sval)); /* execute body */
|
2020-06-25 14:32:34 -04:00
|
|
|
DPRINTF("finished exec of %s, frp=%d\n", s, (int) (frp-frame));
|
2012-12-22 10:35:39 -05:00
|
|
|
|
|
|
|
for (i = 0; i < ndef; i++) {
|
2020-02-06 15:27:31 -05:00
|
|
|
Cell *t = frp->args[i];
|
2012-12-22 10:35:39 -05:00
|
|
|
if (isarr(t)) {
|
|
|
|
if (t->csub == CCOPY) {
|
|
|
|
if (i >= ncall) {
|
|
|
|
freesymtab(t);
|
|
|
|
t->csub = CTEMP;
|
|
|
|
tempfree(t);
|
|
|
|
} else {
|
|
|
|
oargs[i]->tval = t->tval;
|
|
|
|
oargs[i]->tval &= ~(STR|NUM|DONTFREE);
|
|
|
|
oargs[i]->sval = t->sval;
|
|
|
|
tempfree(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (t != y) { /* kludge to prevent freeing twice */
|
|
|
|
t->csub = CTEMP;
|
|
|
|
tempfree(t);
|
|
|
|
} else if (t == y && t->csub == CCOPY) {
|
|
|
|
t->csub = CTEMP;
|
|
|
|
tempfree(t);
|
|
|
|
freed = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tempfree(fcn);
|
|
|
|
if (isexit(y) || isnext(y))
|
|
|
|
return y;
|
|
|
|
if (freed == 0) {
|
|
|
|
tempfree(y); /* don't free twice! */
|
|
|
|
}
|
2020-02-06 15:27:31 -05:00
|
|
|
z = frp->retval; /* return value */
|
2020-06-25 14:32:34 -04:00
|
|
|
DPRINTF("%s returns %g |%s| %o\n", s, getfval(z), getsval(z), z->tval);
|
2020-02-06 15:27:31 -05:00
|
|
|
frp--;
|
2012-12-22 10:35:39 -05:00
|
|
|
return(z);
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell *copycell(Cell *x) /* make a copy of a cell in a temp */
|
|
|
|
{
|
|
|
|
Cell *y;
|
|
|
|
|
2018-08-22 13:40:26 -04:00
|
|
|
/* copy is not constant or field */
|
|
|
|
|
2012-12-22 10:35:39 -05:00
|
|
|
y = gettemp();
|
2018-08-22 13:40:26 -04:00
|
|
|
y->tval = x->tval & ~(CON|FLD|REC);
|
2012-12-22 10:35:39 -05:00
|
|
|
y->csub = CCOPY; /* prevents freeing until call is over */
|
|
|
|
y->nval = x->nval; /* BUG? */
|
2018-08-22 13:40:26 -04:00
|
|
|
if (isstr(x) /* || x->ctype == OCELL */) {
|
2012-12-22 10:35:39 -05:00
|
|
|
y->sval = tostring(x->sval);
|
2018-08-22 13:40:26 -04:00
|
|
|
y->tval &= ~DONTFREE;
|
|
|
|
} else
|
|
|
|
y->tval |= DONTFREE;
|
2012-12-22 10:35:39 -05:00
|
|
|
y->fval = x->fval;
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell *arg(Node **a, int n) /* nth argument of a function */
|
|
|
|
{
|
|
|
|
|
|
|
|
n = ptoi(a[0]); /* argument number, counting from 0 */
|
2020-06-25 14:32:34 -04:00
|
|
|
DPRINTF("arg(%d), frp->nargs=%d\n", n, frp->nargs);
|
2020-02-06 15:27:31 -05:00
|
|
|
if (n+1 > frp->nargs)
|
2012-12-22 10:35:39 -05:00
|
|
|
FATAL("argument #%d of function %s was not supplied",
|
2020-02-06 15:27:31 -05:00
|
|
|
n+1, frp->fcncell->nval);
|
|
|
|
return frp->args[n];
|
2012-12-22 10:35:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Cell *jump(Node **a, int n) /* break, continue, next, nextfile, return */
|
|
|
|
{
|
|
|
|
Cell *y;
|
|
|
|
|
|
|
|
switch (n) {
|
|
|
|
case EXIT:
|
|
|
|
if (a[0] != NULL) {
|
|
|
|
y = execute(a[0]);
|
|
|
|
errorflag = (int) getfval(y);
|
|
|
|
tempfree(y);
|
|
|
|
}
|
|
|
|
longjmp(env, 1);
|
|
|
|
case RETURN:
|
|
|
|
if (a[0] != NULL) {
|
|
|
|
y = execute(a[0]);
|
|
|
|
if ((y->tval & (STR|NUM)) == (STR|NUM)) {
|
2020-02-06 15:27:31 -05:00
|
|
|
setsval(frp->retval, getsval(y));
|
|
|
|
frp->retval->fval = getfval(y);
|
|
|
|
frp->retval->tval |= NUM;
|
2012-12-22 10:35:39 -05:00
|
|
|
}
|
|
|
|
else if (y->tval & STR)
|
2020-02-06 15:27:31 -05:00
|
|
|
setsval(frp->retval, getsval(y));
|
2012-12-22 10:35:39 -05:00
|
|
|
else if (y->tval & NUM)
|
2020-02-06 15:27:31 -05:00
|
|
|
setfval(frp->retval, getfval(y));
|
2012-12-22 10:35:39 -05:00
|
|
|
else /* can't happen */
|
|
|
|
FATAL("bad type variable %d", y->tval);
|
|
|
|
tempfree(y);
|
|
|
|
}
|
|
|
|
return(jret);
|
|
|
|
case NEXT:
|
|
|
|
return(jnext);
|
|
|
|
case NEXTFILE:
|
|
|
|
nextfile();
|
|
|
|
return(jnextfile);
|
|
|
|
case BREAK:
|
|
|
|
return(jbreak);
|
|
|
|
case CONTINUE:
|
|
|
|
return(jcont);
|
|
|
|
default: /* can't happen */
|
|
|
|
FATAL("illegal jump type %d", n);
|
|
|
|
}
|
|
|
|
return 0; /* not reached */
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell *awkgetline(Node **a, int n) /* get next line from specific input */
|
|
|
|
{ /* a[0] is variable, a[1] is operator, a[2] is filename */
|
|
|
|
Cell *r, *x;
|
|
|
|
extern Cell **fldtab;
|
|
|
|
FILE *fp;
|
|
|
|
char *buf;
|
|
|
|
int bufsize = recsize;
|
|
|
|
int mode;
|
2020-02-28 06:23:54 -05:00
|
|
|
bool newflag;
|
2020-12-08 01:05:22 -05:00
|
|
|
double result;
|
2012-12-22 10:35:39 -05:00
|
|
|
|
2020-10-13 13:52:43 -04:00
|
|
|
if ((buf = (char *) malloc(bufsize)) == NULL)
|
2012-12-22 10:35:39 -05:00
|
|
|
FATAL("out of memory in getline");
|
|
|
|
|
|
|
|
fflush(stdout); /* in case someone is waiting for a prompt */
|
|
|
|
r = gettemp();
|
|
|
|
if (a[1] != NULL) { /* getline < file */
|
|
|
|
x = execute(a[2]); /* filename */
|
|
|
|
mode = ptoi(a[1]);
|
|
|
|
if (mode == '|') /* input pipe */
|
|
|
|
mode = LE; /* arbitrary flag */
|
2020-02-28 06:23:54 -05:00
|
|
|
fp = openfile(mode, getsval(x), &newflag);
|
2012-12-22 10:35:39 -05:00
|
|
|
tempfree(x);
|
|
|
|
if (fp == NULL)
|
|
|
|
n = -1;
|
|
|
|
else
|
2020-02-28 06:23:54 -05:00
|
|
|
n = readrec(&buf, &bufsize, fp, newflag);
|
2012-12-22 10:35:39 -05:00
|
|
|
if (n <= 0) {
|
|
|
|
;
|
|
|
|
} else if (a[0] != NULL) { /* getline var <file */
|
|
|
|
x = execute(a[0]);
|
|
|
|
setsval(x, buf);
|
2020-12-08 01:05:22 -05:00
|
|
|
if (is_number(x->sval, & result)) {
|
|
|
|
x->fval = result;
|
2019-01-25 05:56:06 -05:00
|
|
|
x->tval |= NUM;
|
|
|
|
}
|
2012-12-22 10:35:39 -05:00
|
|
|
tempfree(x);
|
|
|
|
} else { /* getline <file */
|
|
|
|
setsval(fldtab[0], buf);
|
2020-12-08 01:05:22 -05:00
|
|
|
if (is_number(fldtab[0]->sval, & result)) {
|
|
|
|
fldtab[0]->fval = result;
|
2012-12-22 10:35:39 -05:00
|
|
|
fldtab[0]->tval |= NUM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { /* bare getline; use current input */
|
|
|
|
if (a[0] == NULL) /* getline */
|
2019-11-10 14:19:18 -05:00
|
|
|
n = getrec(&record, &recsize, true);
|
2012-12-22 10:35:39 -05:00
|
|
|
else { /* getline var */
|
2019-11-10 14:19:18 -05:00
|
|
|
n = getrec(&buf, &bufsize, false);
|
2021-11-01 13:58:54 -04:00
|
|
|
if (n > 0) {
|
|
|
|
x = execute(a[0]);
|
|
|
|
setsval(x, buf);
|
|
|
|
if (is_number(x->sval, & result)) {
|
|
|
|
x->fval = result;
|
|
|
|
x->tval |= NUM;
|
|
|
|
}
|
|
|
|
tempfree(x);
|
2019-01-25 05:56:06 -05:00
|
|
|
}
|
2012-12-22 10:35:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
setfval(r, (Awkfloat) n);
|
|
|
|
free(buf);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell *getnf(Node **a, int n) /* get NF */
|
|
|
|
{
|
2019-11-10 14:19:18 -05:00
|
|
|
if (!donefld)
|
2012-12-22 10:35:39 -05:00
|
|
|
fldbld();
|
|
|
|
return (Cell *) a[0];
|
|
|
|
}
|
|
|
|
|
2019-10-25 10:59:10 -04:00
|
|
|
static char *
|
|
|
|
makearraystring(Node *p, const char *func)
|
2012-12-22 10:35:39 -05:00
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
int bufsz = recsize;
|
2020-07-02 14:22:15 -04:00
|
|
|
size_t blen;
|
2012-12-22 10:35:39 -05:00
|
|
|
|
2020-10-13 13:52:43 -04:00
|
|
|
if ((buf = (char *) malloc(bufsz)) == NULL) {
|
2019-10-25 10:59:10 -04:00
|
|
|
FATAL("%s: out of memory", func);
|
|
|
|
}
|
2012-12-22 10:35:39 -05:00
|
|
|
|
2019-10-25 10:59:10 -04:00
|
|
|
blen = 0;
|
|
|
|
buf[blen] = '\0';
|
|
|
|
|
|
|
|
for (; p; p = p->nnext) {
|
|
|
|
Cell *x = execute(p); /* expr */
|
|
|
|
char *s = getsval(x);
|
2020-07-02 14:22:15 -04:00
|
|
|
size_t seplen = strlen(getsval(subseploc));
|
2019-10-25 10:59:10 -04:00
|
|
|
size_t nsub = p->nnext ? seplen : 0;
|
|
|
|
size_t slen = strlen(s);
|
|
|
|
size_t tlen = blen + slen + nsub;
|
|
|
|
|
|
|
|
if (!adjbuf(&buf, &bufsz, tlen + 1, recsize, 0, func)) {
|
|
|
|
FATAL("%s: out of memory %s[%s...]",
|
|
|
|
func, x->nval, buf);
|
|
|
|
}
|
|
|
|
memcpy(buf + blen, s, slen);
|
|
|
|
if (nsub) {
|
|
|
|
memcpy(buf + blen + slen, *SUBSEP, nsub);
|
|
|
|
}
|
|
|
|
buf[tlen] = '\0';
|
|
|
|
blen = tlen;
|
|
|
|
tempfree(x);
|
2012-12-22 10:35:39 -05:00
|
|
|
}
|
2019-10-25 10:59:10 -04:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell *array(Node **a, int n) /* a[0] is symtab, a[1] is list of subscripts */
|
|
|
|
{
|
|
|
|
Cell *x, *z;
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
x = execute(a[0]); /* Cell* for symbol table */
|
|
|
|
buf = makearraystring(a[1], __func__);
|
2012-12-22 10:35:39 -05:00
|
|
|
if (!isarr(x)) {
|
2020-06-25 14:32:34 -04:00
|
|
|
DPRINTF("making %s into an array\n", NN(x->nval));
|
2012-12-22 10:35:39 -05:00
|
|
|
if (freeable(x))
|
|
|
|
xfree(x->sval);
|
|
|
|
x->tval &= ~(STR|NUM|DONTFREE);
|
|
|
|
x->tval |= ARR;
|
|
|
|
x->sval = (char *) makesymtab(NSYMTAB);
|
|
|
|
}
|
|
|
|
z = setsymtab(buf, "", 0.0, STR|NUM, (Array *) x->sval);
|
|
|
|
z->ctype = OCELL;
|
|
|
|
z->csub = CVAR;
|
|
|
|
tempfree(x);
|
|
|
|
free(buf);
|
|
|
|
return(z);
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell *awkdelete(Node **a, int n) /* a[0] is symtab, a[1] is list of subscripts */
|
|
|
|
{
|
2019-10-25 10:59:10 -04:00
|
|
|
Cell *x;
|
2012-12-22 10:35:39 -05:00
|
|
|
|
|
|
|
x = execute(a[0]); /* Cell* for symbol table */
|
2019-06-17 15:08:54 -04:00
|
|
|
if (x == symtabloc) {
|
|
|
|
FATAL("cannot delete SYMTAB or its elements");
|
|
|
|
}
|
2012-12-22 10:35:39 -05:00
|
|
|
if (!isarr(x))
|
|
|
|
return True;
|
2016-06-03 17:23:11 -04:00
|
|
|
if (a[1] == NULL) { /* delete the elements, not the table */
|
2012-12-22 10:35:39 -05:00
|
|
|
freesymtab(x);
|
|
|
|
x->tval &= ~STR;
|
|
|
|
x->tval |= ARR;
|
|
|
|
x->sval = (char *) makesymtab(NSYMTAB);
|
|
|
|
} else {
|
2019-10-25 10:59:10 -04:00
|
|
|
char *buf = makearraystring(a[1], __func__);
|
2012-12-22 10:35:39 -05:00
|
|
|
freeelem(x, buf);
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
tempfree(x);
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell *intest(Node **a, int n) /* a[0] is index (list), a[1] is symtab */
|
|
|
|
{
|
2019-10-25 10:59:10 -04:00
|
|
|
Cell *ap, *k;
|
2012-12-22 10:35:39 -05:00
|
|
|
char *buf;
|
|
|
|
|
|
|
|
ap = execute(a[1]); /* array name */
|
|
|
|
if (!isarr(ap)) {
|
2020-06-25 14:32:34 -04:00
|
|
|
DPRINTF("making %s into an array\n", ap->nval);
|
2012-12-22 10:35:39 -05:00
|
|
|
if (freeable(ap))
|
|
|
|
xfree(ap->sval);
|
|
|
|
ap->tval &= ~(STR|NUM|DONTFREE);
|
|
|
|
ap->tval |= ARR;
|
|
|
|
ap->sval = (char *) makesymtab(NSYMTAB);
|
|
|
|
}
|
2019-10-25 10:59:10 -04:00
|
|
|
buf = makearraystring(a[0], __func__);
|
2012-12-22 10:35:39 -05:00
|
|
|
k = lookup(buf, (Array *) ap->sval);
|
|
|
|
tempfree(ap);
|
|
|
|
free(buf);
|
|
|
|
if (k == NULL)
|
|
|
|
return(False);
|
|
|
|
else
|
|
|
|
return(True);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Cell *matchop(Node **a, int n) /* ~ and match() */
|
|
|
|
{
|
|
|
|
Cell *x, *y;
|
|
|
|
char *s, *t;
|
|
|
|
int i;
|
|
|
|
fa *pfa;
|
|
|
|
int (*mf)(fa *, const char *) = match, mode = 0;
|
|
|
|
|
|
|
|
if (n == MATCHFCN) {
|
|
|
|
mf = pmatch;
|
|
|
|
mode = 1;
|
|
|
|
}
|
|
|
|
x = execute(a[1]); /* a[1] = target text */
|
|
|
|
s = getsval(x);
|
2016-06-03 17:23:11 -04:00
|
|
|
if (a[0] == NULL) /* a[1] == 0: already-compiled reg expr */
|
2012-12-22 10:35:39 -05:00
|
|
|
i = (*mf)((fa *) a[2], s);
|
|
|
|
else {
|
|
|
|
y = execute(a[2]); /* a[2] = regular expr */
|
|
|
|
t = getsval(y);
|
|
|
|
pfa = makedfa(t, mode);
|
|
|
|
i = (*mf)(pfa, s);
|
|
|
|
tempfree(y);
|
|
|
|
}
|
|
|
|
tempfree(x);
|
|
|
|
if (n == MATCHFCN) {
|
|
|
|
int start = patbeg - s + 1;
|
|
|
|
if (patlen < 0)
|
|
|
|
start = 0;
|
|
|
|
setfval(rstartloc, (Awkfloat) start);
|
|
|
|
setfval(rlengthloc, (Awkfloat) patlen);
|
|
|
|
x = gettemp();
|
|
|
|
x->tval = NUM;
|
|
|
|
x->fval = start;
|
|
|
|
return x;
|
|
|
|
} else if ((n == MATCH && i == 1) || (n == NOTMATCH && i == 0))
|
|
|
|
return(True);
|
|
|
|
else
|
|
|
|
return(False);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Cell *boolop(Node **a, int n) /* a[0] || a[1], a[0] && a[1], !a[0] */
|
|
|
|
{
|
|
|
|
Cell *x, *y;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
x = execute(a[0]);
|
|
|
|
i = istrue(x);
|
|
|
|
tempfree(x);
|
|
|
|
switch (n) {
|
|
|
|
case BOR:
|
|
|
|
if (i) return(True);
|
|
|
|
y = execute(a[1]);
|
|
|
|
i = istrue(y);
|
|
|
|
tempfree(y);
|
|
|
|
if (i) return(True);
|
|
|
|
else return(False);
|
|
|
|
case AND:
|
|
|
|
if ( !i ) return(False);
|
|
|
|
y = execute(a[1]);
|
|
|
|
i = istrue(y);
|
|
|
|
tempfree(y);
|
|
|
|
if (i) return(True);
|
|
|
|
else return(False);
|
|
|
|
case NOT:
|
|
|
|
if (i) return(False);
|
|
|
|
else return(True);
|
|
|
|
default: /* can't happen */
|
|
|
|
FATAL("unknown boolean operator %d", n);
|
|
|
|
}
|
|
|
|
return 0; /*NOTREACHED*/
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell *relop(Node **a, int n) /* a[0 < a[1], etc. */
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
Cell *x, *y;
|
|
|
|
Awkfloat j;
|
|
|
|
|
|
|
|
x = execute(a[0]);
|
|
|
|
y = execute(a[1]);
|
|
|
|
if (x->tval&NUM && y->tval&NUM) {
|
|
|
|
j = x->fval - y->fval;
|
|
|
|
i = j<0? -1: (j>0? 1: 0);
|
|
|
|
} else {
|
|
|
|
i = strcmp(getsval(x), getsval(y));
|
|
|
|
}
|
|
|
|
tempfree(x);
|
|
|
|
tempfree(y);
|
|
|
|
switch (n) {
|
|
|
|
case LT: if (i<0) return(True);
|
|
|
|
else return(False);
|
|
|
|
case LE: if (i<=0) return(True);
|
|
|
|
else return(False);
|
|
|
|
case NE: if (i!=0) return(True);
|
|
|
|
else return(False);
|
|
|
|
case EQ: if (i == 0) return(True);
|
|
|
|
else return(False);
|
|
|
|
case GE: if (i>=0) return(True);
|
|
|
|
else return(False);
|
|
|
|
case GT: if (i>0) return(True);
|
|
|
|
else return(False);
|
|
|
|
default: /* can't happen */
|
|
|
|
FATAL("unknown relational operator %d", n);
|
|
|
|
}
|
|
|
|
return 0; /*NOTREACHED*/
|
|
|
|
}
|
|
|
|
|
|
|
|
void tfree(Cell *a) /* free a tempcell */
|
|
|
|
{
|
|
|
|
if (freeable(a)) {
|
2020-06-25 14:32:34 -04:00
|
|
|
DPRINTF("freeing %s %s %o\n", NN(a->nval), NN(a->sval), a->tval);
|
2012-12-22 10:35:39 -05:00
|
|
|
xfree(a->sval);
|
|
|
|
}
|
|
|
|
if (a == tmps)
|
|
|
|
FATAL("tempcell list is curdled");
|
|
|
|
a->cnext = tmps;
|
|
|
|
tmps = a;
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell *gettemp(void) /* get a tempcell */
|
|
|
|
{ int i;
|
|
|
|
Cell *x;
|
|
|
|
|
|
|
|
if (!tmps) {
|
2020-10-13 13:52:43 -04:00
|
|
|
tmps = (Cell *) calloc(100, sizeof(*tmps));
|
2012-12-22 10:35:39 -05:00
|
|
|
if (!tmps)
|
|
|
|
FATAL("out of space for temporaries");
|
2019-10-08 03:30:09 -04:00
|
|
|
for (i = 1; i < 100; i++)
|
2012-12-22 10:35:39 -05:00
|
|
|
tmps[i-1].cnext = &tmps[i];
|
2016-06-03 17:23:11 -04:00
|
|
|
tmps[i-1].cnext = NULL;
|
2012-12-22 10:35:39 -05:00
|
|
|
}
|
|
|
|
x = tmps;
|
|
|
|
tmps = x->cnext;
|
|
|
|
*x = tempcell;
|
|
|
|
return(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell *indirect(Node **a, int n) /* $( a[0] ) */
|
|
|
|
{
|
|
|
|
Awkfloat val;
|
|
|
|
Cell *x;
|
|
|
|
int m;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
x = execute(a[0]);
|
|
|
|
val = getfval(x); /* freebsd: defend against super large field numbers */
|
|
|
|
if ((Awkfloat)INT_MAX < val)
|
|
|
|
FATAL("trying to access out of range field %s", x->nval);
|
|
|
|
m = (int) val;
|
2020-12-08 01:05:22 -05:00
|
|
|
if (m == 0 && !is_number(s = getsval(x), NULL)) /* suspicion! */
|
2012-12-22 10:35:39 -05:00
|
|
|
FATAL("illegal field $(%s), name \"%s\"", s, x->nval);
|
|
|
|
/* BUG: can x->nval ever be null??? */
|
|
|
|
tempfree(x);
|
|
|
|
x = fieldadr(m);
|
|
|
|
x->ctype = OCELL; /* BUG? why are these needed? */
|
|
|
|
x->csub = CFLD;
|
|
|
|
return(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell *substr(Node **a, int nnn) /* substr(a[0], a[1], a[2]) */
|
|
|
|
{
|
|
|
|
int k, m, n;
|
|
|
|
char *s;
|
|
|
|
int temp;
|
2016-06-03 17:23:11 -04:00
|
|
|
Cell *x, *y, *z = NULL;
|
2012-12-22 10:35:39 -05:00
|
|
|
|
|
|
|
x = execute(a[0]);
|
|
|
|
y = execute(a[1]);
|
2016-06-03 17:23:11 -04:00
|
|
|
if (a[2] != NULL)
|
2012-12-22 10:35:39 -05:00
|
|
|
z = execute(a[2]);
|
|
|
|
s = getsval(x);
|
|
|
|
k = strlen(s) + 1;
|
|
|
|
if (k <= 1) {
|
|
|
|
tempfree(x);
|
|
|
|
tempfree(y);
|
2016-06-03 17:23:11 -04:00
|
|
|
if (a[2] != NULL) {
|
2012-12-22 10:35:39 -05:00
|
|
|
tempfree(z);
|
|
|
|
}
|
|
|
|
x = gettemp();
|
|
|
|
setsval(x, "");
|
|
|
|
return(x);
|
|
|
|
}
|
|
|
|
m = (int) getfval(y);
|
|
|
|
if (m <= 0)
|
|
|
|
m = 1;
|
|
|
|
else if (m > k)
|
|
|
|
m = k;
|
|
|
|
tempfree(y);
|
2016-06-03 17:23:11 -04:00
|
|
|
if (a[2] != NULL) {
|
2012-12-22 10:35:39 -05:00
|
|
|
n = (int) getfval(z);
|
|
|
|
tempfree(z);
|
|
|
|
} else
|
|
|
|
n = k - 1;
|
|
|
|
if (n < 0)
|
|
|
|
n = 0;
|
|
|
|
else if (n > k - m)
|
|
|
|
n = k - m;
|
2020-06-25 14:32:34 -04:00
|
|
|
DPRINTF("substr: m=%d, n=%d, s=%s\n", m, n, s);
|
2012-12-22 10:35:39 -05:00
|
|
|
y = gettemp();
|
|
|
|
temp = s[n+m-1]; /* with thanks to John Linderman */
|
|
|
|
s[n+m-1] = '\0';
|
|
|
|
setsval(y, s + m - 1);
|
|
|
|
s[n+m-1] = temp;
|
|
|
|
tempfree(x);
|
|
|
|
return(y);
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell *sindex(Node **a, int nnn) /* index(a[0], a[1]) */
|
|
|
|
{
|
|
|
|
Cell *x, *y, *z;
|
|
|
|
char *s1, *s2, *p1, *p2, *q;
|
|
|
|
Awkfloat v = 0.0;
|
|
|
|
|
|
|
|
x = execute(a[0]);
|
|
|
|
s1 = getsval(x);
|
|
|
|
y = execute(a[1]);
|
|
|
|
s2 = getsval(y);
|
|
|
|
|
|
|
|
z = gettemp();
|
|
|
|
for (p1 = s1; *p1 != '\0'; p1++) {
|
2020-02-06 15:27:31 -05:00
|
|
|
for (q = p1, p2 = s2; *p2 != '\0' && *q == *p2; q++, p2++)
|
|
|
|
continue;
|
2012-12-22 10:35:39 -05:00
|
|
|
if (*p2 == '\0') {
|
|
|
|
v = (Awkfloat) (p1 - s1 + 1); /* origin 1 */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tempfree(x);
|
|
|
|
tempfree(y);
|
|
|
|
setfval(z, v);
|
|
|
|
return(z);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MAXNUMSIZE 50
|
|
|
|
|
|
|
|
int format(char **pbuf, int *pbufsize, const char *s, Node *a) /* printf-like conversions */
|
|
|
|
{
|
|
|
|
char *fmt;
|
|
|
|
char *p, *t;
|
|
|
|
const char *os;
|
|
|
|
Cell *x;
|
|
|
|
int flag = 0, n;
|
|
|
|
int fmtwd; /* format width */
|
|
|
|
int fmtsz = recsize;
|
|
|
|
char *buf = *pbuf;
|
|
|
|
int bufsize = *pbufsize;
|
2019-10-25 10:59:10 -04:00
|
|
|
#define FMTSZ(a) (fmtsz - ((a) - fmt))
|
|
|
|
#define BUFSZ(a) (bufsize - ((a) - buf))
|
2012-12-22 10:35:39 -05:00
|
|
|
|
2019-11-10 14:19:18 -05:00
|
|
|
static bool first = true;
|
|
|
|
static bool have_a_format = false;
|
2018-08-22 13:40:26 -04:00
|
|
|
|
|
|
|
if (first) {
|
2020-02-19 13:44:49 -05:00
|
|
|
char xbuf[100];
|
2018-08-22 13:40:26 -04:00
|
|
|
|
2020-02-19 13:44:49 -05:00
|
|
|
snprintf(xbuf, sizeof(xbuf), "%a", 42.0);
|
|
|
|
have_a_format = (strcmp(xbuf, "0x1.5p+5") == 0);
|
2019-11-10 14:19:18 -05:00
|
|
|
first = false;
|
2018-08-22 13:40:26 -04:00
|
|
|
}
|
|
|
|
|
2012-12-22 10:35:39 -05:00
|
|
|
os = s;
|
|
|
|
p = buf;
|
2020-10-13 13:52:43 -04:00
|
|
|
if ((fmt = (char *) malloc(fmtsz)) == NULL)
|
2012-12-22 10:35:39 -05:00
|
|
|
FATAL("out of memory in format()");
|
|
|
|
while (*s) {
|
|
|
|
adjbuf(&buf, &bufsize, MAXNUMSIZE+1+p-buf, recsize, &p, "format1");
|
|
|
|
if (*s != '%') {
|
|
|
|
*p++ = *s++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*(s+1) == '%') {
|
|
|
|
*p++ = '%';
|
|
|
|
s += 2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* have to be real careful in case this is a huge number, eg, %100000d */
|
|
|
|
fmtwd = atoi(s+1);
|
|
|
|
if (fmtwd < 0)
|
|
|
|
fmtwd = -fmtwd;
|
|
|
|
adjbuf(&buf, &bufsize, fmtwd+1+p-buf, recsize, &p, "format2");
|
|
|
|
for (t = fmt; (*t++ = *s) != '\0'; s++) {
|
|
|
|
if (!adjbuf(&fmt, &fmtsz, MAXNUMSIZE+1+t-fmt, recsize, &t, "format3"))
|
|
|
|
FATAL("format item %.30s... ran format() out of memory", os);
|
2019-12-11 02:17:34 -05:00
|
|
|
/* Ignore size specifiers */
|
2019-12-11 02:24:38 -05:00
|
|
|
if (strchr("hjLlqtz", *s) != NULL) { /* the ansi panoply */
|
2019-12-11 02:17:34 -05:00
|
|
|
t--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (isalpha((uschar)*s))
|
2019-12-11 02:24:38 -05:00
|
|
|
break;
|
2018-08-27 08:52:34 -04:00
|
|
|
if (*s == '$') {
|
|
|
|
FATAL("'$' not permitted in awk formats");
|
|
|
|
}
|
2012-12-22 10:35:39 -05:00
|
|
|
if (*s == '*') {
|
2018-09-14 20:29:06 -04:00
|
|
|
if (a == NULL) {
|
|
|
|
FATAL("not enough args in printf(%s)", os);
|
|
|
|
}
|
2012-12-22 10:35:39 -05:00
|
|
|
x = execute(a);
|
|
|
|
a = a->nnext;
|
2019-10-25 10:59:10 -04:00
|
|
|
snprintf(t - 1, FMTSZ(t - 1),
|
|
|
|
"%d", fmtwd=(int) getfval(x));
|
2012-12-22 10:35:39 -05:00
|
|
|
if (fmtwd < 0)
|
|
|
|
fmtwd = -fmtwd;
|
|
|
|
adjbuf(&buf, &bufsize, fmtwd+1+p-buf, recsize, &p, "format");
|
|
|
|
t = fmt + strlen(fmt);
|
|
|
|
tempfree(x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*t = '\0';
|
|
|
|
if (fmtwd < 0)
|
|
|
|
fmtwd = -fmtwd;
|
|
|
|
adjbuf(&buf, &bufsize, fmtwd+1+p-buf, recsize, &p, "format4");
|
|
|
|
switch (*s) {
|
2018-08-22 13:40:26 -04:00
|
|
|
case 'a': case 'A':
|
|
|
|
if (have_a_format)
|
|
|
|
flag = *s;
|
|
|
|
else
|
|
|
|
flag = 'f';
|
|
|
|
break;
|
2012-12-22 10:35:39 -05:00
|
|
|
case 'f': case 'e': case 'g': case 'E': case 'G':
|
|
|
|
flag = 'f';
|
|
|
|
break;
|
2019-12-11 02:17:34 -05:00
|
|
|
case 'd': case 'i': case 'o': case 'x': case 'X': case 'u':
|
2019-12-11 02:24:38 -05:00
|
|
|
flag = (*s == 'd' || *s == 'i') ? 'd' : 'u';
|
2019-12-08 14:41:27 -05:00
|
|
|
*(t-1) = 'j';
|
2019-10-25 10:59:10 -04:00
|
|
|
*t = *s;
|
|
|
|
*++t = '\0';
|
2012-12-22 10:35:39 -05:00
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
flag = 's';
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
flag = 'c';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
WARNING("weird printf conversion %s", fmt);
|
|
|
|
flag = '?';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (a == NULL)
|
|
|
|
FATAL("not enough args in printf(%s)", os);
|
|
|
|
x = execute(a);
|
|
|
|
a = a->nnext;
|
|
|
|
n = MAXNUMSIZE;
|
|
|
|
if (fmtwd > n)
|
|
|
|
n = fmtwd;
|
|
|
|
adjbuf(&buf, &bufsize, 1+n+p-buf, recsize, &p, "format5");
|
|
|
|
switch (flag) {
|
2019-10-25 10:59:10 -04:00
|
|
|
case '?': snprintf(p, BUFSZ(p), "%s", fmt); /* unknown, so dump it too */
|
2012-12-22 10:35:39 -05:00
|
|
|
t = getsval(x);
|
|
|
|
n = strlen(t);
|
|
|
|
if (fmtwd > n)
|
|
|
|
n = fmtwd;
|
|
|
|
adjbuf(&buf, &bufsize, 1+strlen(p)+n+p-buf, recsize, &p, "format6");
|
|
|
|
p += strlen(p);
|
2019-10-25 10:59:10 -04:00
|
|
|
snprintf(p, BUFSZ(p), "%s", t);
|
2012-12-22 10:35:39 -05:00
|
|
|
break;
|
2018-08-22 13:40:26 -04:00
|
|
|
case 'a':
|
|
|
|
case 'A':
|
2019-10-25 10:59:10 -04:00
|
|
|
case 'f': snprintf(p, BUFSZ(p), fmt, getfval(x)); break;
|
2019-12-08 14:41:27 -05:00
|
|
|
case 'd': snprintf(p, BUFSZ(p), fmt, (intmax_t) getfval(x)); break;
|
|
|
|
case 'u': snprintf(p, BUFSZ(p), fmt, (uintmax_t) getfval(x)); break;
|
2012-12-22 10:35:39 -05:00
|
|
|
case 's':
|
|
|
|
t = getsval(x);
|
|
|
|
n = strlen(t);
|
|
|
|
if (fmtwd > n)
|
|
|
|
n = fmtwd;
|
|
|
|
if (!adjbuf(&buf, &bufsize, 1+n+p-buf, recsize, &p, "format7"))
|
|
|
|
FATAL("huge string/format (%d chars) in printf %.30s... ran format() out of memory", n, t);
|
2019-10-25 10:59:10 -04:00
|
|
|
snprintf(p, BUFSZ(p), fmt, t);
|
2012-12-22 10:35:39 -05:00
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
if (isnum(x)) {
|
2019-06-02 16:53:15 -04:00
|
|
|
if ((int)getfval(x))
|
2019-10-25 10:59:10 -04:00
|
|
|
snprintf(p, BUFSZ(p), fmt, (int) getfval(x));
|
2012-12-22 10:35:39 -05:00
|
|
|
else {
|
|
|
|
*p++ = '\0'; /* explicit null byte */
|
|
|
|
*p = '\0'; /* next output will start here */
|
|
|
|
}
|
|
|
|
} else
|
2019-10-25 10:59:10 -04:00
|
|
|
snprintf(p, BUFSZ(p), fmt, getsval(x)[0]);
|
2012-12-22 10:35:39 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
FATAL("can't happen: bad conversion %c in format()", flag);
|
|
|
|
}
|
|
|
|
tempfree(x);
|
|
|
|
p += strlen(p);
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
free(fmt);
|
|
|
|
for ( ; a; a = a->nnext) /* evaluate any remaining args */
|
|
|
|
execute(a);
|
|
|
|
*pbuf = buf;
|
|
|
|
*pbufsize = bufsize;
|
|
|
|
return p - buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell *awksprintf(Node **a, int n) /* sprintf(a[0]) */
|
|
|
|
{
|
|
|
|
Cell *x;
|
|
|
|
Node *y;
|
|
|
|
char *buf;
|
|
|
|
int bufsz=3*recsize;
|
|
|
|
|
2020-10-13 13:52:43 -04:00
|
|
|
if ((buf = (char *) malloc(bufsz)) == NULL)
|
2012-12-22 10:35:39 -05:00
|
|
|
FATAL("out of memory in awksprintf");
|
|
|
|
y = a[0]->nnext;
|
|
|
|
x = execute(a[0]);
|
|
|
|
if (format(&buf, &bufsz, getsval(x), y) == -1)
|
|
|
|
FATAL("sprintf string %.30s... too long. can't happen.", buf);
|
|
|
|
tempfree(x);
|
|
|
|
x = gettemp();
|
|
|
|
x->sval = buf;
|
|
|
|
x->tval = STR;
|
|
|
|
return(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell *awkprintf(Node **a, int n) /* printf */
|
|
|
|
{ /* a[0] is list of args, starting with format string */
|
|
|
|
/* a[1] is redirection operator, a[2] is redirection file */
|
|
|
|
FILE *fp;
|
|
|
|
Cell *x;
|
|
|
|
Node *y;
|
|
|
|
char *buf;
|
|
|
|
int len;
|
|
|
|
int bufsz=3*recsize;
|
|
|
|
|
2020-10-13 13:52:43 -04:00
|
|
|
if ((buf = (char *) malloc(bufsz)) == NULL)
|
2012-12-22 10:35:39 -05:00
|
|
|
FATAL("out of memory in awkprintf");
|
|
|
|
y = a[0]->nnext;
|
|
|
|
x = execute(a[0]);
|
|
|
|
if ((len = format(&buf, &bufsz, getsval(x), y)) == -1)
|
|
|
|
FATAL("printf string %.30s... too long. can't happen.", buf);
|
|
|
|
tempfree(x);
|
|
|
|
if (a[1] == NULL) {
|
|
|
|
/* fputs(buf, stdout); */
|
|
|
|
fwrite(buf, len, 1, stdout);
|
|
|
|
if (ferror(stdout))
|
|
|
|
FATAL("write error on stdout");
|
|
|
|
} else {
|
|
|
|
fp = redirect(ptoi(a[1]), a[2]);
|
|
|
|
/* fputs(buf, fp); */
|
|
|
|
fwrite(buf, len, 1, fp);
|
|
|
|
fflush(fp);
|
|
|
|
if (ferror(fp))
|
|
|
|
FATAL("write error on %s", filename(fp));
|
|
|
|
}
|
|
|
|
free(buf);
|
|
|
|
return(True);
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell *arith(Node **a, int n) /* a[0] + a[1], etc. also -a[0] */
|
|
|
|
{
|
|
|
|
Awkfloat i, j = 0;
|
|
|
|
double v;
|
|
|
|
Cell *x, *y, *z;
|
|
|
|
|
|
|
|
x = execute(a[0]);
|
|
|
|
i = getfval(x);
|
|
|
|
tempfree(x);
|
2018-08-22 13:40:26 -04:00
|
|
|
if (n != UMINUS && n != UPLUS) {
|
2012-12-22 10:35:39 -05:00
|
|
|
y = execute(a[1]);
|
|
|
|
j = getfval(y);
|
|
|
|
tempfree(y);
|
|
|
|
}
|
|
|
|
z = gettemp();
|
|
|
|
switch (n) {
|
|
|
|
case ADD:
|
|
|
|
i += j;
|
|
|
|
break;
|
|
|
|
case MINUS:
|
|
|
|
i -= j;
|
|
|
|
break;
|
|
|
|
case MULT:
|
|
|
|
i *= j;
|
|
|
|
break;
|
|
|
|
case DIVIDE:
|
|
|
|
if (j == 0)
|
|
|
|
FATAL("division by zero");
|
|
|
|
i /= j;
|
|
|
|
break;
|
|
|
|
case MOD:
|
|
|
|
if (j == 0)
|
|
|
|
FATAL("division by zero in mod");
|
|
|
|
modf(i/j, &v);
|
|
|
|
i = i - j * v;
|
|
|
|
break;
|
|
|
|
case UMINUS:
|
|
|
|
i = -i;
|
|
|
|
break;
|
2020-02-06 15:27:31 -05:00
|
|
|
case UPLUS: /* handled by getfval(), above */
|
2018-08-22 13:40:26 -04:00
|
|
|
break;
|
2012-12-22 10:35:39 -05:00
|
|
|
case POWER:
|
|
|
|
if (j >= 0 && modf(j, &v) == 0.0) /* pos integer exponent */
|
|
|
|
i = ipow(i, (int) j);
|
2020-06-12 07:16:12 -04:00
|
|
|
else {
|
|
|
|
errno = 0;
|
2012-12-22 10:35:39 -05:00
|
|
|
i = errcheck(pow(i, j), "pow");
|
2020-06-12 07:16:12 -04:00
|
|
|
}
|
2012-12-22 10:35:39 -05:00
|
|
|
break;
|
|
|
|
default: /* can't happen */
|
|
|
|
FATAL("illegal arithmetic operator %d", n);
|
|
|
|
}
|
|
|
|
setfval(z, i);
|
|
|
|
return(z);
|
|
|
|
}
|
|
|
|
|
|
|
|
double ipow(double x, int n) /* x**n. ought to be done by pow, but isn't always */
|
|
|
|
{
|
|
|
|
double v;
|
|
|
|
|
|
|
|
if (n <= 0)
|
|
|
|
return 1;
|
|
|
|
v = ipow(x, n/2);
|
|
|
|
if (n % 2 == 0)
|
|
|
|
return v * v;
|
|
|
|
else
|
|
|
|
return x * v * v;
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell *incrdecr(Node **a, int n) /* a[0]++, etc. */
|
|
|
|
{
|
|
|
|
Cell *x, *z;
|
|
|
|
int k;
|
|
|
|
Awkfloat xf;
|
|
|
|
|
|
|
|
x = execute(a[0]);
|
|
|
|
xf = getfval(x);
|
|
|
|
k = (n == PREINCR || n == POSTINCR) ? 1 : -1;
|
|
|
|
if (n == PREINCR || n == PREDECR) {
|
|
|
|
setfval(x, xf + k);
|
|
|
|
return(x);
|
|
|
|
}
|
|
|
|
z = gettemp();
|
|
|
|
setfval(z, xf);
|
|
|
|
setfval(x, xf + k);
|
|
|
|
tempfree(x);
|
|
|
|
return(z);
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell *assign(Node **a, int n) /* a[0] = a[1], a[0] += a[1], etc. */
|
|
|
|
{ /* this is subtle; don't muck with it. */
|
|
|
|
Cell *x, *y;
|
|
|
|
Awkfloat xf, yf;
|
|
|
|
double v;
|
|
|
|
|
|
|
|
y = execute(a[1]);
|
|
|
|
x = execute(a[0]);
|
|
|
|
if (n == ASSIGN) { /* ordinary assignment */
|
2018-09-23 20:59:52 -04:00
|
|
|
if (x == y && !(x->tval & (FLD|REC)) && x != nfloc)
|
|
|
|
; /* self-assignment: leave alone unless it's a field or NF */
|
2012-12-22 10:35:39 -05:00
|
|
|
else if ((y->tval & (STR|NUM)) == (STR|NUM)) {
|
|
|
|
setsval(x, getsval(y));
|
|
|
|
x->fval = getfval(y);
|
|
|
|
x->tval |= NUM;
|
|
|
|
}
|
|
|
|
else if (isstr(y))
|
|
|
|
setsval(x, getsval(y));
|
|
|
|
else if (isnum(y))
|
|
|
|
setfval(x, getfval(y));
|
|
|
|
else
|
|
|
|
funnyvar(y, "read value of");
|
|
|
|
tempfree(y);
|
|
|
|
return(x);
|
|
|
|
}
|
|
|
|
xf = getfval(x);
|
|
|
|
yf = getfval(y);
|
|
|
|
switch (n) {
|
|
|
|
case ADDEQ:
|
|
|
|
xf += yf;
|
|
|
|
break;
|
|
|
|
case SUBEQ:
|
|
|
|
xf -= yf;
|
|
|
|
break;
|
|
|
|
case MULTEQ:
|
|
|
|
xf *= yf;
|
|
|
|
break;
|
|
|
|
case DIVEQ:
|
|
|
|
if (yf == 0)
|
|
|
|
FATAL("division by zero in /=");
|
|
|
|
xf /= yf;
|
|
|
|
break;
|
|
|
|
case MODEQ:
|
|
|
|
if (yf == 0)
|
|
|
|
FATAL("division by zero in %%=");
|
|
|
|
modf(xf/yf, &v);
|
|
|
|
xf = xf - yf * v;
|
|
|
|
break;
|
|
|
|
case POWEQ:
|
|
|
|
if (yf >= 0 && modf(yf, &v) == 0.0) /* pos integer exponent */
|
|
|
|
xf = ipow(xf, (int) yf);
|
2020-06-12 07:16:12 -04:00
|
|
|
else {
|
|
|
|
errno = 0;
|
2012-12-22 10:35:39 -05:00
|
|
|
xf = errcheck(pow(xf, yf), "pow");
|
2020-06-12 07:16:12 -04:00
|
|
|
}
|
2012-12-22 10:35:39 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
FATAL("illegal assignment operator %d", n);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tempfree(y);
|
|
|
|
setfval(x, xf);
|
|
|
|
return(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell *cat(Node **a, int q) /* a[0] cat a[1] */
|
|
|
|
{
|
|
|
|
Cell *x, *y, *z;
|
|
|
|
int n1, n2;
|
2018-09-15 04:38:39 -04:00
|
|
|
char *s = NULL;
|
|
|
|
int ssz = 0;
|
2012-12-22 10:35:39 -05:00
|
|
|
|
|
|
|
x = execute(a[0]);
|
2018-09-15 04:38:39 -04:00
|
|
|
n1 = strlen(getsval(x));
|
2020-07-02 14:21:10 -04:00
|
|
|
adjbuf(&s, &ssz, n1, recsize, 0, "cat1");
|
|
|
|
memcpy(s, x->sval, n1);
|
2018-09-15 04:38:39 -04:00
|
|
|
|
2012-12-22 10:35:39 -05:00
|
|
|
y = execute(a[1]);
|
2018-09-15 04:38:39 -04:00
|
|
|
n2 = strlen(getsval(y));
|
2020-07-02 14:21:10 -04:00
|
|
|
adjbuf(&s, &ssz, n1 + n2 + 1, recsize, 0, "cat2");
|
2019-10-24 10:06:10 -04:00
|
|
|
memcpy(s + n1, y->sval, n2);
|
|
|
|
s[n1 + n2] = '\0';
|
2018-09-15 04:38:39 -04:00
|
|
|
|
2012-12-22 10:35:39 -05:00
|
|
|
tempfree(x);
|
|
|
|
tempfree(y);
|
2018-09-15 04:38:39 -04:00
|
|
|
|
2012-12-22 10:35:39 -05:00
|
|
|
z = gettemp();
|
|
|
|
z->sval = s;
|
|
|
|
z->tval = STR;
|
2018-09-15 04:38:39 -04:00
|
|
|
|
2012-12-22 10:35:39 -05:00
|
|
|
return(z);
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell *pastat(Node **a, int n) /* a[0] { a[1] } */
|
|
|
|
{
|
|
|
|
Cell *x;
|
|
|
|
|
2016-06-03 17:23:11 -04:00
|
|
|
if (a[0] == NULL)
|
2012-12-22 10:35:39 -05:00
|
|
|
x = execute(a[1]);
|
|
|
|
else {
|
|
|
|
x = execute(a[0]);
|
|
|
|
if (istrue(x)) {
|
|
|
|
tempfree(x);
|
|
|
|
x = execute(a[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell *dopa2(Node **a, int n) /* a[0], a[1] { a[2] } */
|
|
|
|
{
|
|
|
|
Cell *x;
|
|
|
|
int pair;
|
|
|
|
|
|
|
|
pair = ptoi(a[3]);
|
|
|
|
if (pairstack[pair] == 0) {
|
|
|
|
x = execute(a[0]);
|
|
|
|
if (istrue(x))
|
|
|
|
pairstack[pair] = 1;
|
|
|
|
tempfree(x);
|
|
|
|
}
|
|
|
|
if (pairstack[pair] == 1) {
|
|
|
|
x = execute(a[1]);
|
|
|
|
if (istrue(x))
|
|
|
|
pairstack[pair] = 0;
|
|
|
|
tempfree(x);
|
|
|
|
x = execute(a[2]);
|
|
|
|
return(x);
|
|
|
|
}
|
|
|
|
return(False);
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell *split(Node **a, int nnn) /* split(a[0], a[1], a[2]); a[3] is type */
|
|
|
|
{
|
2016-06-03 17:23:11 -04:00
|
|
|
Cell *x = NULL, *y, *ap;
|
2019-10-24 09:40:15 -04:00
|
|
|
const char *s, *origs, *t;
|
2020-01-24 04:11:59 -05:00
|
|
|
const char *fs = NULL;
|
|
|
|
char *origfs = NULL;
|
2012-12-22 10:35:39 -05:00
|
|
|
int sep;
|
2019-10-24 09:40:15 -04:00
|
|
|
char temp, num[50];
|
2012-12-22 10:35:39 -05:00
|
|
|
int n, tempstat, arg3type;
|
2020-12-08 01:05:22 -05:00
|
|
|
double result;
|
2012-12-22 10:35:39 -05:00
|
|
|
|
|
|
|
y = execute(a[0]); /* source string */
|
|
|
|
origs = s = strdup(getsval(y));
|
|
|
|
arg3type = ptoi(a[3]);
|
2016-06-03 17:23:11 -04:00
|
|
|
if (a[2] == NULL) /* fs string */
|
2018-09-18 18:45:55 -04:00
|
|
|
fs = getsval(fsloc);
|
2012-12-22 10:35:39 -05:00
|
|
|
else if (arg3type == STRING) { /* split(str,arr,"string") */
|
|
|
|
x = execute(a[2]);
|
2020-01-24 04:11:59 -05:00
|
|
|
fs = origfs = strdup(getsval(x));
|
2018-09-18 18:20:44 -04:00
|
|
|
tempfree(x);
|
2012-12-22 10:35:39 -05:00
|
|
|
} else if (arg3type == REGEXPR)
|
|
|
|
fs = "(regexpr)"; /* split(str,arr,/regexpr/) */
|
|
|
|
else
|
|
|
|
FATAL("illegal type of split");
|
|
|
|
sep = *fs;
|
|
|
|
ap = execute(a[1]); /* array name */
|
|
|
|
freesymtab(ap);
|
2020-06-25 14:32:34 -04:00
|
|
|
DPRINTF("split: s=|%s|, a=%s, sep=|%s|\n", s, NN(ap->nval), fs);
|
2012-12-22 10:35:39 -05:00
|
|
|
ap->tval &= ~STR;
|
|
|
|
ap->tval |= ARR;
|
|
|
|
ap->sval = (char *) makesymtab(NSYMTAB);
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
if (arg3type == REGEXPR && strlen((char*)((fa*)a[2])->restr) == 0) {
|
|
|
|
/* split(s, a, //); have to arrange that it looks like empty sep */
|
|
|
|
arg3type = 0;
|
|
|
|
fs = "";
|
|
|
|
sep = 0;
|
|
|
|
}
|
|
|
|
if (*s != '\0' && (strlen(fs) > 1 || arg3type == REGEXPR)) { /* reg expr */
|
|
|
|
fa *pfa;
|
|
|
|
if (arg3type == REGEXPR) { /* it's ready already */
|
|
|
|
pfa = (fa *) a[2];
|
|
|
|
} else {
|
|
|
|
pfa = makedfa(fs, 1);
|
|
|
|
}
|
|
|
|
if (nematch(pfa,s)) {
|
|
|
|
tempstat = pfa->initstat;
|
|
|
|
pfa->initstat = 2;
|
|
|
|
do {
|
|
|
|
n++;
|
2019-10-25 10:59:10 -04:00
|
|
|
snprintf(num, sizeof(num), "%d", n);
|
2012-12-22 10:35:39 -05:00
|
|
|
temp = *patbeg;
|
2019-10-24 09:40:15 -04:00
|
|
|
setptr(patbeg, '\0');
|
2020-12-08 01:05:22 -05:00
|
|
|
if (is_number(s, & result))
|
|
|
|
setsymtab(num, s, result, STR|NUM, (Array *) ap->sval);
|
2012-12-22 10:35:39 -05:00
|
|
|
else
|
|
|
|
setsymtab(num, s, 0.0, STR, (Array *) ap->sval);
|
2019-10-24 09:40:15 -04:00
|
|
|
setptr(patbeg, temp);
|
2012-12-22 10:35:39 -05:00
|
|
|
s = patbeg + patlen;
|
2020-02-06 15:27:31 -05:00
|
|
|
if (*(patbeg+patlen-1) == '\0' || *s == '\0') {
|
2012-12-22 10:35:39 -05:00
|
|
|
n++;
|
2019-10-25 10:59:10 -04:00
|
|
|
snprintf(num, sizeof(num), "%d", n);
|
2012-12-22 10:35:39 -05:00
|
|
|
setsymtab(num, "", 0.0, STR, (Array *) ap->sval);
|
|
|
|
pfa->initstat = tempstat;
|
|
|
|
goto spdone;
|
|
|
|
}
|
|
|
|
} while (nematch(pfa,s));
|
|
|
|
pfa->initstat = tempstat; /* bwk: has to be here to reset */
|
|
|
|
/* cf gsub and refldbld */
|
|
|
|
}
|
|
|
|
n++;
|
2019-10-25 10:59:10 -04:00
|
|
|
snprintf(num, sizeof(num), "%d", n);
|
2020-12-08 01:05:22 -05:00
|
|
|
if (is_number(s, & result))
|
|
|
|
setsymtab(num, s, result, STR|NUM, (Array *) ap->sval);
|
2012-12-22 10:35:39 -05:00
|
|
|
else
|
|
|
|
setsymtab(num, s, 0.0, STR, (Array *) ap->sval);
|
|
|
|
spdone:
|
|
|
|
pfa = NULL;
|
|
|
|
} else if (sep == ' ') {
|
|
|
|
for (n = 0; ; ) {
|
2020-02-06 15:27:31 -05:00
|
|
|
#define ISWS(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
|
|
|
|
while (ISWS(*s))
|
2012-12-22 10:35:39 -05:00
|
|
|
s++;
|
2020-02-06 15:27:31 -05:00
|
|
|
if (*s == '\0')
|
2012-12-22 10:35:39 -05:00
|
|
|
break;
|
|
|
|
n++;
|
|
|
|
t = s;
|
|
|
|
do
|
|
|
|
s++;
|
2020-02-06 15:27:31 -05:00
|
|
|
while (*s != '\0' && !ISWS(*s));
|
2012-12-22 10:35:39 -05:00
|
|
|
temp = *s;
|
2019-10-24 09:40:15 -04:00
|
|
|
setptr(s, '\0');
|
2019-10-25 10:59:10 -04:00
|
|
|
snprintf(num, sizeof(num), "%d", n);
|
2020-12-08 01:05:22 -05:00
|
|
|
if (is_number(t, & result))
|
|
|
|
setsymtab(num, t, result, STR|NUM, (Array *) ap->sval);
|
2012-12-22 10:35:39 -05:00
|
|
|
else
|
|
|
|
setsymtab(num, t, 0.0, STR, (Array *) ap->sval);
|
2019-10-24 09:40:15 -04:00
|
|
|
setptr(s, temp);
|
2020-02-06 15:27:31 -05:00
|
|
|
if (*s != '\0')
|
2012-12-22 10:35:39 -05:00
|
|
|
s++;
|
|
|
|
}
|
|
|
|
} else if (sep == 0) { /* new: split(s, a, "") => 1 char/elem */
|
2020-02-06 15:27:31 -05:00
|
|
|
for (n = 0; *s != '\0'; s++) {
|
2012-12-22 10:35:39 -05:00
|
|
|
char buf[2];
|
|
|
|
n++;
|
2019-10-25 10:59:10 -04:00
|
|
|
snprintf(num, sizeof(num), "%d", n);
|
2012-12-22 10:35:39 -05:00
|
|
|
buf[0] = *s;
|
2020-02-06 15:27:31 -05:00
|
|
|
buf[1] = '\0';
|
2012-12-22 10:35:39 -05:00
|
|
|
if (isdigit((uschar)buf[0]))
|
|
|
|
setsymtab(num, buf, atof(buf), STR|NUM, (Array *) ap->sval);
|
|
|
|
else
|
|
|
|
setsymtab(num, buf, 0.0, STR, (Array *) ap->sval);
|
|
|
|
}
|
2020-02-06 15:27:31 -05:00
|
|
|
} else if (*s != '\0') {
|
2012-12-22 10:35:39 -05:00
|
|
|
for (;;) {
|
|
|
|
n++;
|
|
|
|
t = s;
|
|
|
|
while (*s != sep && *s != '\n' && *s != '\0')
|
|
|
|
s++;
|
|
|
|
temp = *s;
|
2019-10-24 09:40:15 -04:00
|
|
|
setptr(s, '\0');
|
2019-10-25 10:59:10 -04:00
|
|
|
snprintf(num, sizeof(num), "%d", n);
|
2020-12-08 01:05:22 -05:00
|
|
|
if (is_number(t, & result))
|
|
|
|
setsymtab(num, t, result, STR|NUM, (Array *) ap->sval);
|
2012-12-22 10:35:39 -05:00
|
|
|
else
|
|
|
|
setsymtab(num, t, 0.0, STR, (Array *) ap->sval);
|
2019-10-24 09:40:15 -04:00
|
|
|
setptr(s, temp);
|
2020-02-06 15:27:31 -05:00
|
|
|
if (*s++ == '\0')
|
2012-12-22 10:35:39 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tempfree(ap);
|
|
|
|
tempfree(y);
|
2019-10-24 09:40:15 -04:00
|
|
|
xfree(origs);
|
|
|
|
xfree(origfs);
|
2012-12-22 10:35:39 -05:00
|
|
|
x = gettemp();
|
|
|
|
x->tval = NUM;
|
|
|
|
x->fval = n;
|
|
|
|
return(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell *condexpr(Node **a, int n) /* a[0] ? a[1] : a[2] */
|
|
|
|
{
|
|
|
|
Cell *x;
|
|
|
|
|
|
|
|
x = execute(a[0]);
|
|
|
|
if (istrue(x)) {
|
|
|
|
tempfree(x);
|
|
|
|
x = execute(a[1]);
|
|
|
|
} else {
|
|
|
|
tempfree(x);
|
|
|
|
x = execute(a[2]);
|
|
|
|
}
|
|
|
|
return(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell *ifstat(Node **a, int n) /* if (a[0]) a[1]; else a[2] */
|
|
|
|
{
|
|
|
|
Cell *x;
|
|
|
|
|
|
|
|
x = execute(a[0]);
|
|
|
|
if (istrue(x)) {
|
|
|
|
tempfree(x);
|
|
|
|
x = execute(a[1]);
|
2016-06-03 17:23:11 -04:00
|
|
|
} else if (a[2] != NULL) {
|
2012-12-22 10:35:39 -05:00
|
|
|
tempfree(x);
|
|
|
|
x = execute(a[2]);
|
|
|
|
}
|
|
|
|
return(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell *whilestat(Node **a, int n) /* while (a[0]) a[1] */
|
|
|
|
{
|
|
|
|
Cell *x;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
x = execute(a[0]);
|
|
|
|
if (!istrue(x))
|
|
|
|
return(x);
|
|
|
|
tempfree(x);
|
|
|
|
x = execute(a[1]);
|
|
|
|
if (isbreak(x)) {
|
|
|
|
x = True;
|
|
|
|
return(x);
|
|
|
|
}
|
|
|
|
if (isnext(x) || isexit(x) || isret(x))
|
|
|
|
return(x);
|
|
|
|
tempfree(x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell *dostat(Node **a, int n) /* do a[0]; while(a[1]) */
|
|
|
|
{
|
|
|
|
Cell *x;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
x = execute(a[0]);
|
|
|
|
if (isbreak(x))
|
|
|
|
return True;
|
|
|
|
if (isnext(x) || isexit(x) || isret(x))
|
|
|
|
return(x);
|
|
|
|
tempfree(x);
|
|
|
|
x = execute(a[1]);
|
|
|
|
if (!istrue(x))
|
|
|
|
return(x);
|
|
|
|
tempfree(x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell *forstat(Node **a, int n) /* for (a[0]; a[1]; a[2]) a[3] */
|
|
|
|
{
|
|
|
|
Cell *x;
|
|
|
|
|
|
|
|
x = execute(a[0]);
|
|
|
|
tempfree(x);
|
|
|
|
for (;;) {
|
2016-06-03 17:23:11 -04:00
|
|
|
if (a[1]!=NULL) {
|
2012-12-22 10:35:39 -05:00
|
|
|
x = execute(a[1]);
|
|
|
|
if (!istrue(x)) return(x);
|
|
|
|
else tempfree(x);
|
|
|
|
}
|
|
|
|
x = execute(a[3]);
|
|
|
|
if (isbreak(x)) /* turn off break */
|
|
|
|
return True;
|
|
|
|
if (isnext(x) || isexit(x) || isret(x))
|
|
|
|
return(x);
|
|
|
|
tempfree(x);
|
|
|
|
x = execute(a[2]);
|
|
|
|
tempfree(x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell *instat(Node **a, int n) /* for (a[0] in a[1]) a[2] */
|
|
|
|
{
|
|
|
|
Cell *x, *vp, *arrayp, *cp, *ncp;
|
|
|
|
Array *tp;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
vp = execute(a[0]);
|
|
|
|
arrayp = execute(a[1]);
|
|
|
|
if (!isarr(arrayp)) {
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
tp = (Array *) arrayp->sval;
|
|
|
|
tempfree(arrayp);
|
|
|
|
for (i = 0; i < tp->size; i++) { /* this routine knows too much */
|
|
|
|
for (cp = tp->tab[i]; cp != NULL; cp = ncp) {
|
|
|
|
setsval(vp, cp->nval);
|
|
|
|
ncp = cp->cnext;
|
|
|
|
x = execute(a[2]);
|
|
|
|
if (isbreak(x)) {
|
|
|
|
tempfree(vp);
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
if (isnext(x) || isexit(x) || isret(x)) {
|
|
|
|
tempfree(vp);
|
|
|
|
return(x);
|
|
|
|
}
|
|
|
|
tempfree(x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
2020-02-06 15:27:31 -05:00
|
|
|
static char *nawk_convert(const char *s, int (*fun_c)(int),
|
|
|
|
wint_t (*fun_wc)(wint_t))
|
|
|
|
{
|
|
|
|
char *buf = NULL;
|
|
|
|
char *pbuf = NULL;
|
|
|
|
const char *ps = NULL;
|
|
|
|
size_t n = 0;
|
|
|
|
wchar_t wc;
|
|
|
|
size_t sz = MB_CUR_MAX;
|
|
|
|
|
|
|
|
if (sz == 1) {
|
|
|
|
buf = tostring(s);
|
|
|
|
|
|
|
|
for (pbuf = buf; *pbuf; pbuf++)
|
|
|
|
*pbuf = fun_c((uschar)*pbuf);
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
} else {
|
|
|
|
/* upper/lower character may be shorter/longer */
|
|
|
|
buf = tostringN(s, strlen(s) * sz + 1);
|
|
|
|
|
2020-08-04 03:02:26 -04:00
|
|
|
(void) mbtowc(NULL, NULL, 0); /* reset internal state */
|
|
|
|
/*
|
|
|
|
* Reset internal state here too.
|
|
|
|
* Assign result to avoid a compiler warning. (Casting to void
|
|
|
|
* doesn't work.)
|
|
|
|
* Increment said variable to avoid a different warning.
|
|
|
|
*/
|
|
|
|
int unused = wctomb(NULL, L'\0');
|
|
|
|
unused++;
|
2020-02-06 15:27:31 -05:00
|
|
|
|
|
|
|
ps = s;
|
|
|
|
pbuf = buf;
|
2020-08-04 03:02:26 -04:00
|
|
|
while (n = mbtowc(&wc, ps, sz),
|
2020-02-06 15:27:31 -05:00
|
|
|
n > 0 && n != (size_t)-1 && n != (size_t)-2)
|
|
|
|
{
|
|
|
|
ps += n;
|
|
|
|
|
2020-08-04 03:02:26 -04:00
|
|
|
n = wctomb(pbuf, fun_wc(wc));
|
2020-02-06 15:27:31 -05:00
|
|
|
if (n == (size_t)-1)
|
|
|
|
FATAL("illegal wide character %s", s);
|
|
|
|
|
|
|
|
pbuf += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
*pbuf = '\0';
|
|
|
|
|
|
|
|
if (n)
|
|
|
|
FATAL("illegal byte sequence %s", s);
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-16 11:48:05 -04:00
|
|
|
#ifdef __DJGPP__
|
|
|
|
static wint_t towupper(wint_t wc)
|
|
|
|
{
|
|
|
|
if (wc >= 0 && wc < 256)
|
|
|
|
return toupper(wc & 0xFF);
|
|
|
|
|
|
|
|
return wc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static wint_t towlower(wint_t wc)
|
|
|
|
{
|
|
|
|
if (wc >= 0 && wc < 256)
|
|
|
|
return tolower(wc & 0xFF);
|
|
|
|
|
|
|
|
return wc;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-02-06 15:27:31 -05:00
|
|
|
static char *nawk_toupper(const char *s)
|
|
|
|
{
|
|
|
|
return nawk_convert(s, toupper, towupper);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *nawk_tolower(const char *s)
|
|
|
|
{
|
|
|
|
return nawk_convert(s, tolower, towlower);
|
|
|
|
}
|
|
|
|
|
2012-12-22 10:35:39 -05:00
|
|
|
Cell *bltin(Node **a, int n) /* builtin functions. a[0] is type, a[1] is arg list */
|
|
|
|
{
|
|
|
|
Cell *x, *y;
|
|
|
|
Awkfloat u;
|
|
|
|
int t;
|
|
|
|
Awkfloat tmp;
|
2020-02-06 15:27:31 -05:00
|
|
|
char *buf;
|
2012-12-22 10:35:39 -05:00
|
|
|
Node *nextarg;
|
|
|
|
FILE *fp;
|
2018-08-22 13:40:26 -04:00
|
|
|
int status = 0;
|
2012-12-22 10:35:39 -05:00
|
|
|
|
|
|
|
t = ptoi(a[0]);
|
|
|
|
x = execute(a[1]);
|
|
|
|
nextarg = a[1]->nnext;
|
|
|
|
switch (t) {
|
|
|
|
case FLENGTH:
|
|
|
|
if (isarr(x))
|
|
|
|
u = ((Array *) x->sval)->nelem; /* GROT. should be function*/
|
|
|
|
else
|
|
|
|
u = strlen(getsval(x));
|
|
|
|
break;
|
|
|
|
case FLOG:
|
2020-06-12 07:16:12 -04:00
|
|
|
errno = 0;
|
|
|
|
u = errcheck(log(getfval(x)), "log");
|
|
|
|
break;
|
2012-12-22 10:35:39 -05:00
|
|
|
case FINT:
|
|
|
|
modf(getfval(x), &u); break;
|
|
|
|
case FEXP:
|
2020-06-12 07:16:12 -04:00
|
|
|
errno = 0;
|
|
|
|
u = errcheck(exp(getfval(x)), "exp");
|
|
|
|
break;
|
2012-12-22 10:35:39 -05:00
|
|
|
case FSQRT:
|
2020-06-12 07:16:12 -04:00
|
|
|
errno = 0;
|
|
|
|
u = errcheck(sqrt(getfval(x)), "sqrt");
|
|
|
|
break;
|
2012-12-22 10:35:39 -05:00
|
|
|
case FSIN:
|
|
|
|
u = sin(getfval(x)); break;
|
|
|
|
case FCOS:
|
|
|
|
u = cos(getfval(x)); break;
|
|
|
|
case FATAN:
|
2016-06-03 17:23:11 -04:00
|
|
|
if (nextarg == NULL) {
|
2012-12-22 10:35:39 -05:00
|
|
|
WARNING("atan2 requires two arguments; returning 1.0");
|
|
|
|
u = 1.0;
|
|
|
|
} else {
|
|
|
|
y = execute(a[1]->nnext);
|
|
|
|
u = atan2(getfval(x), getfval(y));
|
|
|
|
tempfree(y);
|
|
|
|
nextarg = nextarg->nnext;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FSYSTEM:
|
|
|
|
fflush(stdout); /* in case something is buffered already */
|
2018-08-22 13:40:26 -04:00
|
|
|
status = system(getsval(x));
|
|
|
|
u = status;
|
|
|
|
if (status != -1) {
|
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
u = WEXITSTATUS(status);
|
|
|
|
} else if (WIFSIGNALED(status)) {
|
|
|
|
u = WTERMSIG(status) + 256;
|
|
|
|
#ifdef WCOREDUMP
|
|
|
|
if (WCOREDUMP(status))
|
|
|
|
u += 256;
|
|
|
|
#endif
|
|
|
|
} else /* something else?!? */
|
|
|
|
u = 0;
|
|
|
|
}
|
2012-12-22 10:35:39 -05:00
|
|
|
break;
|
|
|
|
case FRAND:
|
2014-09-19 14:24:02 -04:00
|
|
|
/* random() returns numbers in [0..2^31-1]
|
|
|
|
* in order to get a number in [0, 1), divide it by 2^31
|
|
|
|
*/
|
|
|
|
u = (Awkfloat) random() / (0x7fffffffL + 0x1UL);
|
2012-12-22 10:35:39 -05:00
|
|
|
break;
|
|
|
|
case FSRAND:
|
|
|
|
if (isrec(x)) /* no argument provided */
|
|
|
|
u = time((time_t *)0);
|
|
|
|
else
|
|
|
|
u = getfval(x);
|
|
|
|
tmp = u;
|
2014-09-19 14:24:02 -04:00
|
|
|
srandom((unsigned long) u);
|
2012-12-22 10:35:39 -05:00
|
|
|
u = srand_seed;
|
|
|
|
srand_seed = tmp;
|
|
|
|
break;
|
|
|
|
case FTOUPPER:
|
|
|
|
case FTOLOWER:
|
2020-02-06 15:27:31 -05:00
|
|
|
if (t == FTOUPPER)
|
|
|
|
buf = nawk_toupper(getsval(x));
|
|
|
|
else
|
|
|
|
buf = nawk_tolower(getsval(x));
|
2012-12-22 10:35:39 -05:00
|
|
|
tempfree(x);
|
|
|
|
x = gettemp();
|
|
|
|
setsval(x, buf);
|
|
|
|
free(buf);
|
|
|
|
return x;
|
|
|
|
case FFLUSH:
|
|
|
|
if (isrec(x) || strlen(getsval(x)) == 0) {
|
|
|
|
flush_all(); /* fflush() or fflush("") -> all */
|
|
|
|
u = 0;
|
2020-02-28 06:23:54 -05:00
|
|
|
} else if ((fp = openfile(FFLUSH, getsval(x), NULL)) == NULL)
|
2012-12-22 10:35:39 -05:00
|
|
|
u = EOF;
|
|
|
|
else
|
|
|
|
u = fflush(fp);
|
|
|
|
break;
|
|
|
|
default: /* can't happen */
|
|
|
|
FATAL("illegal function type %d", t);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tempfree(x);
|
|
|
|
x = gettemp();
|
|
|
|
setfval(x, u);
|
2016-06-03 17:23:11 -04:00
|
|
|
if (nextarg != NULL) {
|
2012-12-22 10:35:39 -05:00
|
|
|
WARNING("warning: function has too many arguments");
|
|
|
|
for ( ; nextarg; nextarg = nextarg->nnext)
|
|
|
|
execute(nextarg);
|
|
|
|
}
|
|
|
|
return(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell *printstat(Node **a, int n) /* print a[0] */
|
|
|
|
{
|
|
|
|
Node *x;
|
|
|
|
Cell *y;
|
|
|
|
FILE *fp;
|
|
|
|
|
2016-06-03 17:23:11 -04:00
|
|
|
if (a[1] == NULL) /* a[1] is redirection operator, a[2] is file */
|
2012-12-22 10:35:39 -05:00
|
|
|
fp = stdout;
|
|
|
|
else
|
|
|
|
fp = redirect(ptoi(a[1]), a[2]);
|
|
|
|
for (x = a[0]; x != NULL; x = x->nnext) {
|
|
|
|
y = execute(x);
|
|
|
|
fputs(getpssval(y), fp);
|
|
|
|
tempfree(y);
|
|
|
|
if (x->nnext == NULL)
|
2018-09-18 18:45:55 -04:00
|
|
|
fputs(getsval(orsloc), fp);
|
2012-12-22 10:35:39 -05:00
|
|
|
else
|
2018-09-18 18:45:55 -04:00
|
|
|
fputs(getsval(ofsloc), fp);
|
2012-12-22 10:35:39 -05:00
|
|
|
}
|
2016-06-03 17:23:11 -04:00
|
|
|
if (a[1] != NULL)
|
2012-12-22 10:35:39 -05:00
|
|
|
fflush(fp);
|
|
|
|
if (ferror(fp))
|
|
|
|
FATAL("write error on %s", filename(fp));
|
|
|
|
return(True);
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell *nullproc(Node **a, int n)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FILE *redirect(int a, Node *b) /* set up all i/o redirections */
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
Cell *x;
|
|
|
|
char *fname;
|
|
|
|
|
|
|
|
x = execute(b);
|
|
|
|
fname = getsval(x);
|
2020-02-28 06:23:54 -05:00
|
|
|
fp = openfile(a, fname, NULL);
|
2012-12-22 10:35:39 -05:00
|
|
|
if (fp == NULL)
|
|
|
|
FATAL("can't open file %s", fname);
|
|
|
|
tempfree(x);
|
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct files {
|
|
|
|
FILE *fp;
|
|
|
|
const char *fname;
|
|
|
|
int mode; /* '|', 'a', 'w' => LE/LT, GT */
|
|
|
|
} *files;
|
|
|
|
|
2020-02-06 15:27:31 -05:00
|
|
|
size_t nfiles;
|
2012-12-22 10:35:39 -05:00
|
|
|
|
2020-02-06 15:27:31 -05:00
|
|
|
static void stdinit(void) /* in case stdin, etc., are not constants */
|
2012-12-22 10:35:39 -05:00
|
|
|
{
|
|
|
|
nfiles = FOPEN_MAX;
|
2020-10-13 13:52:43 -04:00
|
|
|
files = (struct files *) calloc(nfiles, sizeof(*files));
|
2012-12-22 10:35:39 -05:00
|
|
|
if (files == NULL)
|
2020-02-06 15:27:31 -05:00
|
|
|
FATAL("can't allocate file memory for %zu files", nfiles);
|
2012-12-22 10:35:39 -05:00
|
|
|
files[0].fp = stdin;
|
|
|
|
files[0].fname = "/dev/stdin";
|
|
|
|
files[0].mode = LT;
|
|
|
|
files[1].fp = stdout;
|
|
|
|
files[1].fname = "/dev/stdout";
|
|
|
|
files[1].mode = GT;
|
|
|
|
files[2].fp = stderr;
|
|
|
|
files[2].fname = "/dev/stderr";
|
|
|
|
files[2].mode = GT;
|
|
|
|
}
|
|
|
|
|
2020-02-28 06:23:54 -05:00
|
|
|
FILE *openfile(int a, const char *us, bool *pnewflag)
|
2012-12-22 10:35:39 -05:00
|
|
|
{
|
|
|
|
const char *s = us;
|
2020-02-06 15:27:31 -05:00
|
|
|
size_t i;
|
|
|
|
int m;
|
2016-06-03 17:23:11 -04:00
|
|
|
FILE *fp = NULL;
|
2012-12-22 10:35:39 -05:00
|
|
|
|
|
|
|
if (*s == '\0')
|
|
|
|
FATAL("null file name in print or getline");
|
2020-02-06 15:27:31 -05:00
|
|
|
for (i = 0; i < nfiles; i++)
|
2020-02-28 06:23:54 -05:00
|
|
|
if (files[i].fname && strcmp(s, files[i].fname) == 0 &&
|
|
|
|
(a == files[i].mode || (a==APPEND && files[i].mode==GT) ||
|
|
|
|
a == FFLUSH)) {
|
|
|
|
if (pnewflag)
|
|
|
|
*pnewflag = false;
|
|
|
|
return files[i].fp;
|
2012-12-22 10:35:39 -05:00
|
|
|
}
|
|
|
|
if (a == FFLUSH) /* didn't find it, so don't create it! */
|
|
|
|
return NULL;
|
|
|
|
|
2020-02-06 15:27:31 -05:00
|
|
|
for (i = 0; i < nfiles; i++)
|
2016-06-03 17:23:11 -04:00
|
|
|
if (files[i].fp == NULL)
|
2012-12-22 10:35:39 -05:00
|
|
|
break;
|
|
|
|
if (i >= nfiles) {
|
|
|
|
struct files *nf;
|
2020-02-06 15:27:31 -05:00
|
|
|
size_t nnf = nfiles + FOPEN_MAX;
|
2020-10-13 13:52:43 -04:00
|
|
|
nf = (struct files *) realloc(files, nnf * sizeof(*nf));
|
2012-12-22 10:35:39 -05:00
|
|
|
if (nf == NULL)
|
2020-02-06 15:27:31 -05:00
|
|
|
FATAL("cannot grow files for %s and %zu files", s, nnf);
|
2012-12-22 10:35:39 -05:00
|
|
|
memset(&nf[nfiles], 0, FOPEN_MAX * sizeof(*nf));
|
|
|
|
nfiles = nnf;
|
|
|
|
files = nf;
|
|
|
|
}
|
|
|
|
fflush(stdout); /* force a semblance of order */
|
|
|
|
m = a;
|
|
|
|
if (a == GT) {
|
|
|
|
fp = fopen(s, "w");
|
|
|
|
} else if (a == APPEND) {
|
|
|
|
fp = fopen(s, "a");
|
|
|
|
m = GT; /* so can mix > and >> */
|
|
|
|
} else if (a == '|') { /* output pipe */
|
|
|
|
fp = popen(s, "w");
|
|
|
|
} else if (a == LE) { /* input pipe */
|
|
|
|
fp = popen(s, "r");
|
|
|
|
} else if (a == LT) { /* getline <file */
|
|
|
|
fp = strcmp(s, "-") == 0 ? stdin : fopen(s, "r"); /* "-" is stdin */
|
|
|
|
} else /* can't happen */
|
|
|
|
FATAL("illegal redirection %d", a);
|
|
|
|
if (fp != NULL) {
|
|
|
|
files[i].fname = tostring(s);
|
|
|
|
files[i].fp = fp;
|
|
|
|
files[i].mode = m;
|
2020-02-28 06:23:54 -05:00
|
|
|
if (pnewflag)
|
|
|
|
*pnewflag = true;
|
2020-01-22 04:10:59 -05:00
|
|
|
if (fp != stdin && fp != stdout && fp != stderr)
|
|
|
|
(void) fcntl(fileno(fp), F_SETFD, FD_CLOEXEC);
|
2012-12-22 10:35:39 -05:00
|
|
|
}
|
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *filename(FILE *fp)
|
|
|
|
{
|
2020-02-06 15:27:31 -05:00
|
|
|
size_t i;
|
2012-12-22 10:35:39 -05:00
|
|
|
|
|
|
|
for (i = 0; i < nfiles; i++)
|
|
|
|
if (fp == files[i].fp)
|
|
|
|
return files[i].fname;
|
|
|
|
return "???";
|
|
|
|
}
|
|
|
|
|
2020-02-06 15:32:55 -05:00
|
|
|
Cell *closefile(Node **a, int n)
|
|
|
|
{
|
|
|
|
Cell *x;
|
|
|
|
size_t i;
|
|
|
|
bool stat;
|
2020-08-07 06:10:20 -04:00
|
|
|
|
2020-02-06 15:32:55 -05:00
|
|
|
x = execute(a[0]);
|
|
|
|
getsval(x);
|
|
|
|
stat = true;
|
|
|
|
for (i = 0; i < nfiles; i++) {
|
|
|
|
if (!files[i].fname || strcmp(x->sval, files[i].fname) != 0)
|
|
|
|
continue;
|
|
|
|
if (ferror(files[i].fp))
|
|
|
|
FATAL("i/o error occurred on %s", files[i].fname);
|
2020-07-27 03:03:58 -04:00
|
|
|
if (files[i].fp == stdin || files[i].fp == stdout ||
|
|
|
|
files[i].fp == stderr)
|
|
|
|
stat = freopen("/dev/null", "r+", files[i].fp) == NULL;
|
|
|
|
else if (files[i].mode == '|' || files[i].mode == LE)
|
2020-02-06 15:32:55 -05:00
|
|
|
stat = pclose(files[i].fp) == -1;
|
|
|
|
else
|
|
|
|
stat = fclose(files[i].fp) == EOF;
|
|
|
|
if (stat)
|
|
|
|
FATAL("i/o error occurred closing %s", files[i].fname);
|
|
|
|
if (i > 2) /* don't do /dev/std... */
|
|
|
|
xfree(files[i].fname);
|
|
|
|
files[i].fname = NULL; /* watch out for ref thru this */
|
|
|
|
files[i].fp = NULL;
|
2020-07-27 03:03:58 -04:00
|
|
|
break;
|
2020-02-06 15:32:55 -05:00
|
|
|
}
|
|
|
|
tempfree(x);
|
|
|
|
x = gettemp();
|
|
|
|
setfval(x, (Awkfloat) (stat ? -1 : 0));
|
|
|
|
return(x);
|
|
|
|
}
|
2012-12-22 10:35:39 -05:00
|
|
|
|
|
|
|
void closeall(void)
|
|
|
|
{
|
2020-02-06 15:38:30 -05:00
|
|
|
size_t i;
|
|
|
|
bool stat = false;
|
|
|
|
|
|
|
|
for (i = 0; i < nfiles; i++) {
|
|
|
|
if (! files[i].fp)
|
|
|
|
continue;
|
|
|
|
if (ferror(files[i].fp))
|
|
|
|
FATAL( "i/o error occurred on %s", files[i].fname );
|
2020-07-27 03:03:58 -04:00
|
|
|
if (files[i].fp == stdin)
|
|
|
|
continue;
|
2020-02-06 15:38:30 -05:00
|
|
|
if (files[i].mode == '|' || files[i].mode == LE)
|
|
|
|
stat = pclose(files[i].fp) == -1;
|
2020-07-27 03:03:58 -04:00
|
|
|
else if (files[i].fp == stdout || files[i].fp == stderr)
|
|
|
|
stat = fflush(files[i].fp) == EOF;
|
2020-02-06 15:38:30 -05:00
|
|
|
else
|
|
|
|
stat = fclose(files[i].fp) == EOF;
|
|
|
|
if (stat)
|
|
|
|
FATAL( "i/o error occurred while closing %s", files[i].fname );
|
2012-12-22 10:35:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-06 15:38:30 -05:00
|
|
|
static void flush_all(void)
|
2012-12-22 10:35:39 -05:00
|
|
|
{
|
2020-02-19 13:44:49 -05:00
|
|
|
size_t i;
|
2012-12-22 10:35:39 -05:00
|
|
|
|
|
|
|
for (i = 0; i < nfiles; i++)
|
|
|
|
if (files[i].fp)
|
|
|
|
fflush(files[i].fp);
|
|
|
|
}
|
|
|
|
|
2019-10-24 09:40:15 -04:00
|
|
|
void backsub(char **pb_ptr, const char **sptr_ptr);
|
2012-12-22 10:35:39 -05:00
|
|
|
|
|
|
|
Cell *sub(Node **a, int nnn) /* substitute command */
|
|
|
|
{
|
2019-10-24 09:40:15 -04:00
|
|
|
const char *sptr, *q;
|
2012-12-22 10:35:39 -05:00
|
|
|
Cell *x, *y, *result;
|
2019-10-24 09:40:15 -04:00
|
|
|
char *t, *buf, *pb;
|
2012-12-22 10:35:39 -05:00
|
|
|
fa *pfa;
|
|
|
|
int bufsz = recsize;
|
|
|
|
|
2020-10-13 13:52:43 -04:00
|
|
|
if ((buf = (char *) malloc(bufsz)) == NULL)
|
2012-12-22 10:35:39 -05:00
|
|
|
FATAL("out of memory in sub");
|
|
|
|
x = execute(a[3]); /* target string */
|
|
|
|
t = getsval(x);
|
2016-06-03 17:23:11 -04:00
|
|
|
if (a[0] == NULL) /* 0 => a[1] is already-compiled regexpr */
|
2012-12-22 10:35:39 -05:00
|
|
|
pfa = (fa *) a[1]; /* regular expression */
|
|
|
|
else {
|
|
|
|
y = execute(a[1]);
|
|
|
|
pfa = makedfa(getsval(y), 1);
|
|
|
|
tempfree(y);
|
|
|
|
}
|
|
|
|
y = execute(a[2]); /* replacement string */
|
|
|
|
result = False;
|
|
|
|
if (pmatch(pfa, t)) {
|
|
|
|
sptr = t;
|
|
|
|
adjbuf(&buf, &bufsz, 1+patbeg-sptr, recsize, 0, "sub");
|
|
|
|
pb = buf;
|
|
|
|
while (sptr < patbeg)
|
|
|
|
*pb++ = *sptr++;
|
|
|
|
sptr = getsval(y);
|
2020-02-06 15:27:31 -05:00
|
|
|
while (*sptr != '\0') {
|
2012-12-22 10:35:39 -05:00
|
|
|
adjbuf(&buf, &bufsz, 5+pb-buf, recsize, &pb, "sub");
|
|
|
|
if (*sptr == '\\') {
|
|
|
|
backsub(&pb, &sptr);
|
|
|
|
} else if (*sptr == '&') {
|
|
|
|
sptr++;
|
|
|
|
adjbuf(&buf, &bufsz, 1+patlen+pb-buf, recsize, &pb, "sub");
|
|
|
|
for (q = patbeg; q < patbeg+patlen; )
|
|
|
|
*pb++ = *q++;
|
|
|
|
} else
|
|
|
|
*pb++ = *sptr++;
|
|
|
|
}
|
|
|
|
*pb = '\0';
|
|
|
|
if (pb > buf + bufsz)
|
|
|
|
FATAL("sub result1 %.30s too big; can't happen", buf);
|
|
|
|
sptr = patbeg + patlen;
|
|
|
|
if ((patlen == 0 && *patbeg) || (patlen && *(sptr-1))) {
|
|
|
|
adjbuf(&buf, &bufsz, 1+strlen(sptr)+pb-buf, 0, &pb, "sub");
|
2020-02-06 15:27:31 -05:00
|
|
|
while ((*pb++ = *sptr++) != '\0')
|
|
|
|
continue;
|
2012-12-22 10:35:39 -05:00
|
|
|
}
|
|
|
|
if (pb > buf + bufsz)
|
|
|
|
FATAL("sub result2 %.30s too big; can't happen", buf);
|
|
|
|
setsval(x, buf); /* BUG: should be able to avoid copy */
|
2016-06-03 17:23:11 -04:00
|
|
|
result = True;
|
2012-12-22 10:35:39 -05:00
|
|
|
}
|
|
|
|
tempfree(x);
|
|
|
|
tempfree(y);
|
|
|
|
free(buf);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell *gsub(Node **a, int nnn) /* global substitute */
|
|
|
|
{
|
|
|
|
Cell *x, *y;
|
2019-10-24 09:40:15 -04:00
|
|
|
char *rptr, *pb;
|
|
|
|
const char *q, *t, *sptr;
|
2012-12-22 10:35:39 -05:00
|
|
|
char *buf;
|
|
|
|
fa *pfa;
|
|
|
|
int mflag, tempstat, num;
|
|
|
|
int bufsz = recsize;
|
|
|
|
|
2020-10-13 13:52:43 -04:00
|
|
|
if ((buf = (char *) malloc(bufsz)) == NULL)
|
2012-12-22 10:35:39 -05:00
|
|
|
FATAL("out of memory in gsub");
|
|
|
|
mflag = 0; /* if mflag == 0, can replace empty string */
|
|
|
|
num = 0;
|
|
|
|
x = execute(a[3]); /* target string */
|
|
|
|
t = getsval(x);
|
2016-06-03 17:23:11 -04:00
|
|
|
if (a[0] == NULL) /* 0 => a[1] is already-compiled regexpr */
|
2012-12-22 10:35:39 -05:00
|
|
|
pfa = (fa *) a[1]; /* regular expression */
|
|
|
|
else {
|
|
|
|
y = execute(a[1]);
|
|
|
|
pfa = makedfa(getsval(y), 1);
|
|
|
|
tempfree(y);
|
|
|
|
}
|
|
|
|
y = execute(a[2]); /* replacement string */
|
|
|
|
if (pmatch(pfa, t)) {
|
|
|
|
tempstat = pfa->initstat;
|
|
|
|
pfa->initstat = 2;
|
|
|
|
pb = buf;
|
|
|
|
rptr = getsval(y);
|
|
|
|
do {
|
2020-02-06 15:27:31 -05:00
|
|
|
if (patlen == 0 && *patbeg != '\0') { /* matched empty string */
|
2012-12-22 10:35:39 -05:00
|
|
|
if (mflag == 0) { /* can replace empty */
|
|
|
|
num++;
|
|
|
|
sptr = rptr;
|
2020-02-06 15:27:31 -05:00
|
|
|
while (*sptr != '\0') {
|
2012-12-22 10:35:39 -05:00
|
|
|
adjbuf(&buf, &bufsz, 5+pb-buf, recsize, &pb, "gsub");
|
|
|
|
if (*sptr == '\\') {
|
|
|
|
backsub(&pb, &sptr);
|
|
|
|
} else if (*sptr == '&') {
|
|
|
|
sptr++;
|
|
|
|
adjbuf(&buf, &bufsz, 1+patlen+pb-buf, recsize, &pb, "gsub");
|
|
|
|
for (q = patbeg; q < patbeg+patlen; )
|
|
|
|
*pb++ = *q++;
|
|
|
|
} else
|
|
|
|
*pb++ = *sptr++;
|
|
|
|
}
|
|
|
|
}
|
2020-02-06 15:27:31 -05:00
|
|
|
if (*t == '\0') /* at end */
|
2012-12-22 10:35:39 -05:00
|
|
|
goto done;
|
|
|
|
adjbuf(&buf, &bufsz, 2+pb-buf, recsize, &pb, "gsub");
|
|
|
|
*pb++ = *t++;
|
|
|
|
if (pb > buf + bufsz) /* BUG: not sure of this test */
|
|
|
|
FATAL("gsub result0 %.30s too big; can't happen", buf);
|
|
|
|
mflag = 0;
|
|
|
|
}
|
|
|
|
else { /* matched nonempty string */
|
|
|
|
num++;
|
|
|
|
sptr = t;
|
|
|
|
adjbuf(&buf, &bufsz, 1+(patbeg-sptr)+pb-buf, recsize, &pb, "gsub");
|
|
|
|
while (sptr < patbeg)
|
|
|
|
*pb++ = *sptr++;
|
|
|
|
sptr = rptr;
|
2020-02-06 15:27:31 -05:00
|
|
|
while (*sptr != '\0') {
|
2012-12-22 10:35:39 -05:00
|
|
|
adjbuf(&buf, &bufsz, 5+pb-buf, recsize, &pb, "gsub");
|
|
|
|
if (*sptr == '\\') {
|
|
|
|
backsub(&pb, &sptr);
|
|
|
|
} else if (*sptr == '&') {
|
|
|
|
sptr++;
|
|
|
|
adjbuf(&buf, &bufsz, 1+patlen+pb-buf, recsize, &pb, "gsub");
|
|
|
|
for (q = patbeg; q < patbeg+patlen; )
|
|
|
|
*pb++ = *q++;
|
|
|
|
} else
|
|
|
|
*pb++ = *sptr++;
|
|
|
|
}
|
|
|
|
t = patbeg + patlen;
|
2020-02-06 15:27:31 -05:00
|
|
|
if (patlen == 0 || *t == '\0' || *(t-1) == '\0')
|
2012-12-22 10:35:39 -05:00
|
|
|
goto done;
|
|
|
|
if (pb > buf + bufsz)
|
|
|
|
FATAL("gsub result1 %.30s too big; can't happen", buf);
|
|
|
|
mflag = 1;
|
|
|
|
}
|
|
|
|
} while (pmatch(pfa,t));
|
|
|
|
sptr = t;
|
|
|
|
adjbuf(&buf, &bufsz, 1+strlen(sptr)+pb-buf, 0, &pb, "gsub");
|
2020-02-06 15:27:31 -05:00
|
|
|
while ((*pb++ = *sptr++) != '\0')
|
|
|
|
continue;
|
2012-12-22 10:35:39 -05:00
|
|
|
done: if (pb < buf + bufsz)
|
|
|
|
*pb = '\0';
|
|
|
|
else if (*(pb-1) != '\0')
|
|
|
|
FATAL("gsub result2 %.30s truncated; can't happen", buf);
|
|
|
|
setsval(x, buf); /* BUG: should be able to avoid copy + free */
|
|
|
|
pfa->initstat = tempstat;
|
|
|
|
}
|
|
|
|
tempfree(x);
|
|
|
|
tempfree(y);
|
|
|
|
x = gettemp();
|
|
|
|
x->tval = NUM;
|
|
|
|
x->fval = num;
|
|
|
|
free(buf);
|
|
|
|
return(x);
|
|
|
|
}
|
|
|
|
|
2019-10-24 09:40:15 -04:00
|
|
|
void backsub(char **pb_ptr, const char **sptr_ptr) /* handle \\& variations */
|
2012-12-22 10:35:39 -05:00
|
|
|
{ /* sptr[0] == '\\' */
|
2019-10-24 09:40:15 -04:00
|
|
|
char *pb = *pb_ptr;
|
|
|
|
const char *sptr = *sptr_ptr;
|
2020-01-19 13:37:33 -05:00
|
|
|
static bool first = true;
|
|
|
|
static bool do_posix = false;
|
|
|
|
|
|
|
|
if (first) {
|
|
|
|
first = false;
|
|
|
|
do_posix = (getenv("POSIXLY_CORRECT") != NULL);
|
|
|
|
}
|
2012-12-22 10:35:39 -05:00
|
|
|
|
|
|
|
if (sptr[1] == '\\') {
|
|
|
|
if (sptr[2] == '\\' && sptr[3] == '&') { /* \\\& -> \& */
|
|
|
|
*pb++ = '\\';
|
|
|
|
*pb++ = '&';
|
|
|
|
sptr += 4;
|
|
|
|
} else if (sptr[2] == '&') { /* \\& -> \ + matched */
|
|
|
|
*pb++ = '\\';
|
|
|
|
sptr += 2;
|
2020-01-19 13:37:33 -05:00
|
|
|
} else if (do_posix) { /* \\x -> \x */
|
|
|
|
sptr++;
|
|
|
|
*pb++ = *sptr++;
|
2012-12-22 10:35:39 -05:00
|
|
|
} else { /* \\x -> \\x */
|
|
|
|
*pb++ = *sptr++;
|
|
|
|
*pb++ = *sptr++;
|
|
|
|
}
|
|
|
|
} else if (sptr[1] == '&') { /* literal & */
|
|
|
|
sptr++;
|
|
|
|
*pb++ = *sptr++;
|
|
|
|
} else /* literal \ */
|
|
|
|
*pb++ = *sptr++;
|
|
|
|
|
|
|
|
*pb_ptr = pb;
|
|
|
|
*sptr_ptr = sptr;
|
|
|
|
}
|