5965fd777b
* Works fine under Debian GNU/Linux 7.4, NetBSD/amd64 6.99.36 * Merge pkgsrc specific changes Changelog: Dec 20, 2012: fiddled makefile to get correct yacc and bison flags. pick yacc (linux) or bison (mac) as necessary. added __attribute__((__noreturn__)) to a couple of lines in proto.h, to silence someone's enthusiastic checker. fixed obscure call by value bug in split(a[1],a) reported on 9fans. the management of temporary values is just a mess; i took a shortcut by making an extra string copy. thanks to paul patience and arnold robbins for passing it on and for proposed patches. tiny fiddle in setfval to eliminate -0 results in T.expr, which has irritated me for 20+ years. Aug 10, 2011: another fix to avoid core dump with delete(ARGV); again, many thanks to ruslan ermilov. Aug 7, 2011: split(s, a, //) now behaves the same as split(s, a, "") Jun 12, 2011: /pat/, \n /pat/ {...} is now legal, though bad style to use. added checks to new -v code that permits -vnospace; thanks to ruslan ermilov for spotting this and providing the patch. removed fixed limit on number of open files; thanks to aleksey cheusov and christos zoulos. fixed day 1 bug that resurrected deleted elements of ARGV when used as filenames (in lib.c). minor type fiddles to make gcc -Wall -pedantic happier (but not totally so); turned on -fno-strict-aliasing in makefile. May 6, 2011: added #ifdef for isblank. now allows -ffoo as well as -f foo arguments. (thanks, ruslan) May 1, 2011: after advice from todd miller, kevin lo, ruslan ermilov, and arnold robbins, changed srand() to return the previous seed (which is 1 on the first call of srand). the seed is an Awkfloat internally though converted to unsigned int to pass to the library srand(). thanks, everyone. fixed a subtle (and i hope low-probability) overflow error in fldbld, by adding space for one extra \0. thanks to robert bassett for spotting this one and providing a fix. removed the files related to compilation on windows. i no longer have anything like a current windows environment, so i can't test any of it. May 23, 2010: fixed long-standing overflow bug in run.c; many thanks to nelson beebe for spotting it and providing the fix. fixed bug that didn't parse -vd=1 properly; thanks to santiago vila for spotting it. Feb 8, 2010: i give up. replaced isblank with isspace in b.c; there are no consistent header files. Nov 26, 2009: fixed a long-standing issue with when FS takes effect. a change to FS is now noticed immediately for subsequent splits. changed the name getline() to awkgetline() to avoid yet another name conflict somewhere. Feb 11, 2009: temporarily for now defined HAS_ISBLANK, since that seems to be the best way through the thicket. isblank arrived in C99, but seems to be arriving at different systems at different times. Oct 8, 2008: fixed typo in b.c that set tmpvec wrongly. no one had ever run into the problem, apparently. thanks to alistair crooks. Oct 23, 2007: minor fix in lib.c: increase inputFS to 100, change malloc for fields to n+1. fixed memory fault caused by out of order test in setsval. thanks to david o'brien, freebsd, for both fixes. May 1, 2007: fiddle in makefile to fix for BSD make; thanks to igor sobrado. Mar 31, 2007: fixed some null pointer refs calling adjbuf. Feb 21, 2007: fixed a bug in matching the null RE in sub and gsub. thanks to al aho who actually did the fix (in b.c), and to wolfgang seeberg for finding it and providing a very compact test case. fixed quotation in b.c; thanks to Hal Pratt and the Princeton Dante Project. removed some no-effect asserts in run.c. fiddled maketab.c to not complain about bison-generated values. removed the obsolete -V argument; fixed --version to print the version and exit. fixed wording and an outright error in the usage message; thanks to igor sobrado and jason mcintyre. fixed a bug in -d that caused core dump if no program followed. Jan 1, 2007: dropped mac.code from makefile; there are few non-MacOSX mac's these days. Jan 17, 2006: system() not flagged as unsafe in the unadvertised -safe option. found it while enhancing tests before shipping the ;login: article. practice what you preach. removed the 9-years-obsolete -mr and -mf flags. added -version and --version options. core dump on linux with BEGIN {nextfile}, now fixed. removed some #ifdef's in run.c and lex.c that appear to no longer be necessary.
463 lines
12 KiB
C
463 lines
12 KiB
C
/* $NetBSD: tran.c,v 1.4 2014/03/12 14:20:43 ryoon Exp $ */
|
|
|
|
/****************************************************************
|
|
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 <math.h>
|
|
#include <ctype.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include "awk.h"
|
|
#include "ytab.h"
|
|
|
|
#define FULLTAB 2 /* rehash when table gets this x full */
|
|
#define GROWTAB 4 /* grow table by this factor */
|
|
|
|
Array *symtab; /* main symbol table */
|
|
|
|
char **FS; /* initial field sep */
|
|
char **RS; /* initial record sep */
|
|
char **OFS; /* output field sep */
|
|
char **ORS; /* output record sep */
|
|
char **OFMT; /* output format for numbers */
|
|
char **CONVFMT; /* format for conversions in getsval */
|
|
Awkfloat *NF; /* number of fields in current record */
|
|
Awkfloat *NR; /* number of current record */
|
|
Awkfloat *FNR; /* number of current record in current file */
|
|
char **FILENAME; /* current filename argument */
|
|
Awkfloat *ARGC; /* number of arguments from command line */
|
|
char **SUBSEP; /* subscript separator for a[i,j,k]; default \034 */
|
|
Awkfloat *RSTART; /* start of re matched with ~; origin 1 (!) */
|
|
Awkfloat *RLENGTH; /* length of same */
|
|
|
|
Cell *fsloc; /* FS */
|
|
Cell *nrloc; /* NR */
|
|
Cell *nfloc; /* NF */
|
|
Cell *fnrloc; /* FNR */
|
|
Array *ARGVtab; /* symbol table containing ARGV[...] */
|
|
Array *ENVtab; /* symbol table containing ENVIRON[...] */
|
|
Cell *rstartloc; /* RSTART */
|
|
Cell *rlengthloc; /* RLENGTH */
|
|
Cell *symtabloc; /* SYMTAB */
|
|
|
|
Cell *nullloc; /* a guaranteed empty cell */
|
|
Node *nullnode; /* zero&null, converted into a node for comparisons */
|
|
Cell *literal0;
|
|
|
|
extern Cell **fldtab;
|
|
|
|
void syminit(void) /* initialize symbol table with builtin vars */
|
|
{
|
|
literal0 = setsymtab("0", "0", 0.0, NUM|STR|CON|DONTFREE, symtab);
|
|
/* this is used for if(x)... tests: */
|
|
nullloc = setsymtab("$zero&null", "", 0.0, NUM|STR|CON|DONTFREE, symtab);
|
|
nullnode = celltonode(nullloc, CCON);
|
|
|
|
fsloc = setsymtab("FS", " ", 0.0, STR|DONTFREE, symtab);
|
|
FS = &fsloc->sval;
|
|
RS = &setsymtab("RS", "\n", 0.0, STR|DONTFREE, symtab)->sval;
|
|
OFS = &setsymtab("OFS", " ", 0.0, STR|DONTFREE, symtab)->sval;
|
|
ORS = &setsymtab("ORS", "\n", 0.0, STR|DONTFREE, symtab)->sval;
|
|
OFMT = &setsymtab("OFMT", "%.6g", 0.0, STR|DONTFREE, symtab)->sval;
|
|
CONVFMT = &setsymtab("CONVFMT", "%.6g", 0.0, STR|DONTFREE, symtab)->sval;
|
|
FILENAME = &setsymtab("FILENAME", "", 0.0, STR|DONTFREE, symtab)->sval;
|
|
nfloc = setsymtab("NF", "", 0.0, NUM, symtab);
|
|
NF = &nfloc->fval;
|
|
nrloc = setsymtab("NR", "", 0.0, NUM, symtab);
|
|
NR = &nrloc->fval;
|
|
fnrloc = setsymtab("FNR", "", 0.0, NUM, symtab);
|
|
FNR = &fnrloc->fval;
|
|
SUBSEP = &setsymtab("SUBSEP", "\034", 0.0, STR|DONTFREE, symtab)->sval;
|
|
rstartloc = setsymtab("RSTART", "", 0.0, NUM, symtab);
|
|
RSTART = &rstartloc->fval;
|
|
rlengthloc = setsymtab("RLENGTH", "", 0.0, NUM, symtab);
|
|
RLENGTH = &rlengthloc->fval;
|
|
symtabloc = setsymtab("SYMTAB", "", 0.0, ARR, symtab);
|
|
symtabloc->sval = (char *) symtab;
|
|
}
|
|
|
|
void arginit(int ac, char **av) /* set up ARGV and ARGC */
|
|
{
|
|
Cell *cp;
|
|
int i;
|
|
char temp[50];
|
|
|
|
ARGC = &setsymtab("ARGC", "", (Awkfloat) ac, NUM, symtab)->fval;
|
|
cp = setsymtab("ARGV", "", 0.0, ARR, symtab);
|
|
ARGVtab = makesymtab(NSYMTAB); /* could be (int) ARGC as well */
|
|
cp->sval = (char *) ARGVtab;
|
|
for (i = 0; i < ac; i++) {
|
|
sprintf(temp, "%d", i);
|
|
if (is_number(*av))
|
|
setsymtab(temp, *av, atof(*av), STR|NUM, ARGVtab);
|
|
else
|
|
setsymtab(temp, *av, 0.0, STR, ARGVtab);
|
|
av++;
|
|
}
|
|
}
|
|
|
|
void envinit(char **envp) /* set up ENVIRON variable */
|
|
{
|
|
Cell *cp;
|
|
char *p;
|
|
|
|
cp = setsymtab("ENVIRON", "", 0.0, ARR, symtab);
|
|
ENVtab = makesymtab(NSYMTAB);
|
|
cp->sval = (char *) ENVtab;
|
|
for ( ; *envp; envp++) {
|
|
if ((p = strchr(*envp, '=')) == NULL)
|
|
continue;
|
|
if( p == *envp ) /* no left hand side name in env string */
|
|
continue;
|
|
*p++ = 0; /* split into two strings at = */
|
|
if (is_number(p))
|
|
setsymtab(*envp, p, atof(p), STR|NUM, ENVtab);
|
|
else
|
|
setsymtab(*envp, p, 0.0, STR, ENVtab);
|
|
p[-1] = '='; /* restore in case env is passed down to a shell */
|
|
}
|
|
}
|
|
|
|
Array *makesymtab(int n) /* make a new symbol table */
|
|
{
|
|
Array *ap;
|
|
Cell **tp;
|
|
|
|
ap = (Array *) malloc(sizeof(Array));
|
|
tp = (Cell **) calloc(n, sizeof(Cell *));
|
|
if (ap == NULL || tp == NULL)
|
|
FATAL("out of space in makesymtab");
|
|
ap->nelem = 0;
|
|
ap->size = n;
|
|
ap->tab = tp;
|
|
return(ap);
|
|
}
|
|
|
|
void freesymtab(Cell *ap) /* free a symbol table */
|
|
{
|
|
Cell *cp, *temp;
|
|
Array *tp;
|
|
int i;
|
|
|
|
if (!isarr(ap))
|
|
return;
|
|
tp = (Array *) ap->sval;
|
|
if (tp == NULL)
|
|
return;
|
|
for (i = 0; i < tp->size; i++) {
|
|
for (cp = tp->tab[i]; cp != NULL; cp = temp) {
|
|
xfree(cp->nval);
|
|
if (freeable(cp))
|
|
xfree(cp->sval);
|
|
temp = cp->cnext; /* avoids freeing then using */
|
|
free(cp);
|
|
tp->nelem--;
|
|
}
|
|
tp->tab[i] = 0;
|
|
}
|
|
if (tp->nelem != 0)
|
|
WARNING("can't happen: inconsistent element count freeing %s", ap->nval);
|
|
free(tp->tab);
|
|
free(tp);
|
|
}
|
|
|
|
void freeelem(Cell *ap, const char *s) /* free elem s from ap (i.e., ap["s"] */
|
|
{
|
|
Array *tp;
|
|
Cell *p, *prev = NULL;
|
|
int h;
|
|
|
|
tp = (Array *) ap->sval;
|
|
h = hash(s, tp->size);
|
|
for (p = tp->tab[h]; p != NULL; prev = p, p = p->cnext)
|
|
if (strcmp(s, p->nval) == 0) {
|
|
if (prev == NULL) /* 1st one */
|
|
tp->tab[h] = p->cnext;
|
|
else /* middle somewhere */
|
|
prev->cnext = p->cnext;
|
|
if (freeable(p))
|
|
xfree(p->sval);
|
|
free(p->nval);
|
|
free(p);
|
|
tp->nelem--;
|
|
return;
|
|
}
|
|
}
|
|
|
|
Cell *setsymtab(const char *n, const char *s, Awkfloat f, unsigned t, Array *tp)
|
|
{
|
|
int h;
|
|
Cell *p;
|
|
|
|
if (n != NULL && (p = lookup(n, tp)) != NULL) {
|
|
dprintf( ("setsymtab found %p: n=%s s=\"%s\" f=%g t=%o\n",
|
|
(void*)p, NN(p->nval), NN(p->sval), p->fval, p->tval) );
|
|
return(p);
|
|
}
|
|
p = (Cell *) malloc(sizeof(Cell));
|
|
if (p == NULL)
|
|
FATAL("out of space for symbol table at %s", n);
|
|
p->nval = tostring(n);
|
|
p->sval = s ? tostring(s) : tostring("");
|
|
p->fval = f;
|
|
p->tval = t;
|
|
p->csub = CUNK;
|
|
p->ctype = OCELL;
|
|
tp->nelem++;
|
|
if (tp->nelem > FULLTAB * tp->size)
|
|
rehash(tp);
|
|
h = hash(n, tp->size);
|
|
p->cnext = tp->tab[h];
|
|
tp->tab[h] = p;
|
|
dprintf( ("setsymtab set %p: n=%s s=\"%s\" f=%g t=%o\n",
|
|
(void*)p, p->nval, p->sval, p->fval, p->tval) );
|
|
return(p);
|
|
}
|
|
|
|
int hash(const char *s, int n) /* form hash value for string s */
|
|
{
|
|
unsigned hashval;
|
|
|
|
for (hashval = 0; *s != '\0'; s++)
|
|
hashval = (*s + 31 * hashval);
|
|
return hashval % n;
|
|
}
|
|
|
|
void rehash(Array *tp) /* rehash items in small table into big one */
|
|
{
|
|
int i, nh, nsz;
|
|
Cell *cp, *op, **np;
|
|
|
|
nsz = GROWTAB * tp->size;
|
|
np = (Cell **) calloc(nsz, sizeof(Cell *));
|
|
if (np == NULL) /* can't do it, but can keep running. */
|
|
return; /* someone else will run out later. */
|
|
for (i = 0; i < tp->size; i++) {
|
|
for (cp = tp->tab[i]; cp; cp = op) {
|
|
op = cp->cnext;
|
|
nh = hash(cp->nval, nsz);
|
|
cp->cnext = np[nh];
|
|
np[nh] = cp;
|
|
}
|
|
}
|
|
free(tp->tab);
|
|
tp->tab = np;
|
|
tp->size = nsz;
|
|
}
|
|
|
|
Cell *lookup(const char *s, Array *tp) /* look for s in tp */
|
|
{
|
|
Cell *p;
|
|
int h;
|
|
|
|
h = hash(s, tp->size);
|
|
for (p = tp->tab[h]; p != NULL; p = p->cnext)
|
|
if (strcmp(s, p->nval) == 0)
|
|
return(p); /* found it */
|
|
return(NULL); /* not found */
|
|
}
|
|
|
|
Awkfloat setfval(Cell *vp, Awkfloat f) /* set float val of a Cell */
|
|
{
|
|
int fldno;
|
|
|
|
if ((vp->tval & (NUM | STR)) == 0)
|
|
funnyvar(vp, "assign to");
|
|
if (isfld(vp)) {
|
|
donerec = 0; /* mark $0 invalid */
|
|
fldno = atoi(vp->nval);
|
|
if (fldno > *NF)
|
|
newfld(fldno);
|
|
dprintf( ("setting field %d to %g\n", fldno, f) );
|
|
} else if (isrec(vp)) {
|
|
donefld = 0; /* mark $1... invalid */
|
|
donerec = 1;
|
|
}
|
|
if (freeable(vp))
|
|
xfree(vp->sval); /* free any previous string */
|
|
vp->tval &= ~STR; /* mark string invalid */
|
|
vp->tval |= NUM; /* mark number ok */
|
|
if (f == -0) /* who would have thought this possible? */
|
|
f = 0;
|
|
dprintf( ("setfval %p: %s = %g, t=%o\n", (void*)vp, NN(vp->nval), f, vp->tval) );
|
|
return vp->fval = f;
|
|
}
|
|
|
|
void funnyvar(Cell *vp, const char *rw)
|
|
{
|
|
if (isarr(vp))
|
|
FATAL("can't %s %s; it's an array name.", rw, vp->nval);
|
|
if (vp->tval & FCN)
|
|
FATAL("can't %s %s; it's a function.", rw, vp->nval);
|
|
WARNING("funny variable %p: n=%s s=\"%s\" f=%g t=%o",
|
|
vp, vp->nval, vp->sval, vp->fval, vp->tval);
|
|
}
|
|
|
|
char *setsval(Cell *vp, const char *s) /* set string val of a Cell */
|
|
{
|
|
char *t;
|
|
int fldno;
|
|
|
|
dprintf( ("starting setsval %p: %s = \"%s\", t=%o, r,f=%d,%d\n",
|
|
(void*)vp, NN(vp->nval), s, vp->tval, donerec, donefld) );
|
|
if ((vp->tval & (NUM | STR)) == 0)
|
|
funnyvar(vp, "assign to");
|
|
if (isfld(vp)) {
|
|
donerec = 0; /* mark $0 invalid */
|
|
fldno = atoi(vp->nval);
|
|
if (fldno > *NF)
|
|
newfld(fldno);
|
|
dprintf( ("setting field %d to %s (%p)\n", fldno, s, s) );
|
|
} else if (isrec(vp)) {
|
|
donefld = 0; /* mark $1... invalid */
|
|
donerec = 1;
|
|
}
|
|
t = tostring(s); /* in case it's self-assign */
|
|
if (freeable(vp))
|
|
xfree(vp->sval);
|
|
vp->tval &= ~NUM;
|
|
vp->tval |= STR;
|
|
vp->tval &= ~DONTFREE;
|
|
dprintf( ("setsval %p: %s = \"%s (%p) \", t=%o r,f=%d,%d\n",
|
|
(void*)vp, NN(vp->nval), t,t, vp->tval, donerec, donefld) );
|
|
return(vp->sval = t);
|
|
}
|
|
|
|
Awkfloat getfval(Cell *vp) /* get float val of a Cell */
|
|
{
|
|
if ((vp->tval & (NUM | STR)) == 0)
|
|
funnyvar(vp, "read value of");
|
|
if (isfld(vp) && donefld == 0)
|
|
fldbld();
|
|
else if (isrec(vp) && donerec == 0)
|
|
recbld();
|
|
if (!isnum(vp)) { /* not a number */
|
|
vp->fval = atof(vp->sval); /* best guess */
|
|
if (is_number(vp->sval) && !(vp->tval&CON))
|
|
vp->tval |= NUM; /* make NUM only sparingly */
|
|
}
|
|
dprintf( ("getfval %p: %s = %g, t=%o\n",
|
|
(void*)vp, NN(vp->nval), vp->fval, vp->tval) );
|
|
return(vp->fval);
|
|
}
|
|
|
|
static char *get_str_val(Cell *vp, char **fmt) /* get string val of a Cell */
|
|
{
|
|
char s[100]; /* BUG: unchecked */
|
|
double dtemp;
|
|
|
|
if ((vp->tval & (NUM | STR)) == 0)
|
|
funnyvar(vp, "read value of");
|
|
if (isfld(vp) && donefld == 0)
|
|
fldbld();
|
|
else if (isrec(vp) && donerec == 0)
|
|
recbld();
|
|
if (isstr(vp) == 0) {
|
|
if (freeable(vp))
|
|
xfree(vp->sval);
|
|
if (modf(vp->fval, &dtemp) == 0) /* it's integral */
|
|
sprintf(s, "%.30g", vp->fval);
|
|
else
|
|
sprintf(s, *fmt, vp->fval);
|
|
vp->sval = tostring(s);
|
|
vp->tval &= ~DONTFREE;
|
|
vp->tval |= STR;
|
|
}
|
|
dprintf( ("getsval %p: %s = \"%s (%p)\", t=%o\n",
|
|
(void*)vp, NN(vp->nval), vp->sval, vp->sval, vp->tval) );
|
|
return(vp->sval);
|
|
}
|
|
|
|
char *getsval(Cell *vp) /* get string val of a Cell */
|
|
{
|
|
return get_str_val(vp, CONVFMT);
|
|
}
|
|
|
|
char *getpssval(Cell *vp) /* get string val of a Cell for print */
|
|
{
|
|
return get_str_val(vp, OFMT);
|
|
}
|
|
|
|
|
|
char *tostring(const char *s) /* make a copy of string s */
|
|
{
|
|
size_t len;
|
|
char *p;
|
|
|
|
len = strlen(s) + 1;
|
|
p = (char *) malloc(len);
|
|
if (p == NULL)
|
|
FATAL("out of space in tostring on %s", s);
|
|
memcpy(p, s, len);
|
|
return(p);
|
|
}
|
|
|
|
char *qstring(const char *is, int delim) /* collect string up to next delim */
|
|
{
|
|
const char *os = is;
|
|
int c, n;
|
|
uschar *s = (uschar *) is;
|
|
uschar *buf, *bp;
|
|
|
|
if ((buf = (uschar *) malloc(strlen(is)+3)) == NULL)
|
|
FATAL( "out of space in qstring(%s)", s);
|
|
for (bp = buf; (c = *s) != delim; s++) {
|
|
if (c == '\n')
|
|
SYNTAX( "newline in string %.20s...", os );
|
|
else if (c != '\\')
|
|
*bp++ = c;
|
|
else { /* \something */
|
|
c = *++s;
|
|
if (c == 0) { /* \ at end */
|
|
*bp++ = '\\';
|
|
break; /* for loop */
|
|
}
|
|
switch (c) {
|
|
case '\\': *bp++ = '\\'; break;
|
|
case 'n': *bp++ = '\n'; break;
|
|
case 't': *bp++ = '\t'; break;
|
|
case 'b': *bp++ = '\b'; break;
|
|
case 'f': *bp++ = '\f'; break;
|
|
case 'r': *bp++ = '\r'; break;
|
|
default:
|
|
if (!isdigit(c)) {
|
|
*bp++ = c;
|
|
break;
|
|
}
|
|
n = c - '0';
|
|
if (isdigit(s[1])) {
|
|
n = 8 * n + *++s - '0';
|
|
if (isdigit(s[1]))
|
|
n = 8 * n + *++s - '0';
|
|
}
|
|
*bp++ = n;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
*bp++ = 0;
|
|
return (char *) buf;
|
|
}
|