run.c revision 83efe03b07f2c50424cb88af7a19d789200bf17c
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#pragma ident "%Z%%M% %I% %E% SMI"
#define DEBUG
#include "awk.h"
#include <math.h>
#include "y.tab.h"
#include <stdio.h>
#include <ctype.h>
#include <setjmp.h>
#include <time.h>
#ifndef FOPEN_MAX
#endif
int paircnt;
static void closeall(void);
static double ipow(double, int);
void
{
(void) execute(a);
closeall();
}
static Cell *
{
register Cell *x;
register Node *a;
if (u == NULL)
return (true);
for (a = u; ; a = a->nnext) {
curnode = a;
if (isvalue(a)) {
fldbld();
recbld();
return (x);
}
/* probably a Cell* but too risky to print */
fldbld();
recbld();
if (isexpr(a))
return (x);
/* a statement, goto next statement */
if (isjump(x))
return (x);
return (x);
tempfree(x, "execute");
}
}
/*ARGSUSED*/
Cell *
{
register Cell *x;
goto ex;
if (a[0]) { /* BEGIN */
x = execute(a[0]);
if (isexit(x))
return (true);
if (isjump(x)) {
ERROR "illegal break, continue or next from BEGIN"
}
tempfree(x, "");
}
loop:
if (a[1] || a[2])
x = execute(a[1]);
if (isexit(x))
break;
tempfree(x, "");
}
ex:
goto ex1;
if (a[2]) { /* END */
x = execute(a[2]);
if (iscont(x)) /* read some more */
goto loop;
tempfree(x, "");
}
ex1:
return (true);
}
struct Frame {
int nargs; /* number of arguments in this call */
};
#define NARGS 30
int nframe = 0; /* number of frames allocated */
/*ARGSUSED*/
Cell *
{
static Cell newcopycell =
Node *x;
uchar *s;
sizeof (struct Frame));
ERROR "out of space for stack frames calling %s",
s FATAL;
}
}
ncall++;
dprintf(("calling %s, %d args (%d in defn), fp=%d\n",
ERROR "function %s called with %d args, uses only %d",
}
ERROR "function %s has %d arguments, limit %d",
}
/* get call args */
y = execute(x);
oargs[i] = y;
dprintf(("args[%d]: %s %f <%s>, t=%o\n",
if (isfunc(y)) {
ERROR "can't use function %s as argument in %s",
}
if (isarr(y))
args[i] = y; /* arrays by ref */
else
tempfree(y, "callargs");
}
for (; i < ndef; i++) { /* add null args for ones not provided */
*args[i] = newcopycell;
}
fp++; /* now ok to up frame */
}
/*LINTED align*/
for (i = 0; i < ndef; i++) {
if (isarr(t)) {
if (i >= ncall) {
freesymtab(t);
} else {
tempfree(t, "oargsarr");
}
}
} else {
tempfree(t, "fp->args");
if (t == y) freed = 1;
}
}
return (y);
if (!freed)
dprintf(("%s returns %g |%s| %o\n",
fp--;
return (z);
}
static Cell *
{
Cell *y;
y = gettemp("copycell");
/* copy is not constant or field is DONTFREE right? */
return (y);
}
/*ARGSUSED*/
Cell *
{
int n;
n = (int)a[0]; /* argument number, counting from 0 */
ERROR "argument #%d of function %s was not supplied",
}
}
Cell *
{
register Cell *y;
switch (n) {
case EXIT:
if (a[0] != NULL) {
y = execute(a[0]);
tempfree(y, "");
}
/*NOTREACHED*/
case RETURN:
if (a[0] != NULL) {
y = execute(a[0]);
tempfree(y, "");
}
return (jret);
case NEXT:
return (jnext);
case BREAK:
return (jbreak);
case CONTINUE:
return (jcont);
default: /* can't happen */
}
/*NOTREACHED*/
return (NULL);
}
Cell *
{
/* a[0] is variable, a[1] is operator, a[2] is filename */
register Cell *r, *x;
r = gettemp("");
if ((int)a[1] == '|') /* input pipe */
tempfree(x, "");
n = -1;
else
if (n > 0) {
if (a[0] != NULL) { /* getline var <file */
} else { /* getline <file */
}
}
} else { /* bare getline; use current input */
if (a[0] == NULL) /* getline */
else { /* getline var */
}
}
return (r);
}
/*ARGSUSED*/
Cell *
{
if (donefld == 0)
fldbld();
return ((Cell *)a[0]);
}
/*ARGSUSED*/
Cell *
{
register Cell *x, *y, *z;
register uchar *s;
x = execute(a[0]); /* Cell* for symbol table */
buf[0] = '\0';
tlen = 0;
s = getsval(y);
}
tempfree(y, "");
}
if (!isarr(x)) {
if (freeable(x))
}
/*LINTED align*/
tempfree(x, "");
return (z);
}
/*ARGSUSED*/
Cell *
{
Cell *x, *y;
x = execute(a[0]); /* Cell* for symbol table */
if (!isarr(x))
return (true);
buf[0] = '\0';
tlen = 0;
s = getsval(y);
}
tempfree(y, "");
}
tempfree(x, "");
return (true);
}
/*ARGSUSED*/
Cell *
{
Node *p;
uchar *s;
buf[0] = 0;
tlen = 0;
for (p = a[0]; p; p = p->nnext) {
x = execute(p); /* expr */
s = getsval(x);
tempfree(x, "");
if (p->nnext) {
}
}
/*LINTED align*/
if (k == NULL)
return (false);
else
return (true);
}
Cell *
{
register Cell *x, *y;
register uchar *s, *t;
register int i;
if (n == MATCHFCN) {
mode = 1;
}
x = execute(a[1]);
s = getsval(x);
if (a[0] == 0)
i = (*mf)(a[2], s);
else {
y = execute(a[2]);
t = getsval(y);
tempfree(y, "");
}
tempfree(x, "");
if (n == MATCHFCN) {
if (patlen < 0)
start = 0;
x = gettemp("");
return (x);
return (true);
else
return (false);
}
Cell *
{
register Cell *x, *y;
register 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, "");
return (i ? true : false);
case AND:
if (!i)
return (false);
y = execute(a[1]);
i = istrue(y);
tempfree(y, "");
return (i ? true : false);
case NOT:
return (i ? false : true);
default: /* can't happen */
}
/*NOTREACHED*/
return (NULL);
}
Cell *
{
register int i;
register Cell *x, *y;
Awkfloat j;
x = execute(a[0]);
y = execute(a[1]);
i = j < 0 ? -1: (j > 0 ? 1: 0);
} else {
}
tempfree(x, "");
tempfree(y, "");
switch (n) {
case LT: return (i < 0 ? true : false);
case LE: return (i <= 0 ? true : false);
case NE: return (i != 0 ? true : false);
case EQ: return (i == 0 ? true : false);
case GE: return (i >= 0 ? true : false);
case GT: return (i > 0 ? true : false);
default: /* can't happen */
}
/*NOTREACHED*/
return (false);
}
static void
{
if (dbg > 1) {
(void) printf("## tfree %.8s %06lo %s\n",
}
if (freeable(a))
if (a == tmps)
tmps = a;
}
static Cell *
gettemp(char *s)
{
int i;
register Cell *x;
if (!tmps) {
if (!tmps)
for (i = 1; i < 100; i++)
}
x = tmps;
*x = tempcell;
if (dbg > 1)
return (x);
}
/*ARGSUSED*/
Cell *
{
register Cell *x;
register int m;
register uchar *s;
x = execute(a[0]);
m = (int)getfval(x);
tempfree(x, "");
x = fieldadr(m);
return (x);
}
/*ARGSUSED*/
Cell *
{
register int k, m, n;
register uchar *s;
int temp;
register Cell *x, *y, *z;
x = execute(a[0]);
y = execute(a[1]);
if (a[2] != 0)
z = execute(a[2]);
s = getsval(x);
k = strlen((char *)s) + 1;
if (k <= 1) {
tempfree(x, "");
tempfree(y, "");
if (a[2] != 0)
tempfree(z, "");
x = gettemp("");
return (x);
}
m = (int)getfval(y);
if (m <= 0)
m = 1;
else if (m > k)
m = k;
tempfree(y, "");
if (a[2] != 0) {
n = (int)getfval(z);
tempfree(z, "");
} else
n = k - 1;
if (n < 0)
n = 0;
else if (n > k - m)
n = k - m;
dprintf(("substr: m=%d, n=%d, s=%s\n", m, n, s));
y = gettemp("");
s[n + m - 1] = '\0';
(void) setsval(y, s + m - 1);
s[n + m - 1] = temp;
tempfree(x, "");
return (y);
}
/*ARGSUSED*/
Cell *
{
register Cell *x, *y, *z;
Awkfloat v = 0.0;
x = execute(a[0]);
y = execute(a[1]);
z = gettemp("");
;
if (*p2 == '\0') {
break;
}
}
tempfree(x, "");
tempfree(y, "");
(void) setfval(z, v);
return (z);
}
void
{
register Cell *x;
os = s;
cnt = 0;
while (*s) {
if (*s != '%') {
continue;
}
if (*(s+1) == '%') {
s += 2;
continue;
}
for (tcnt = 0; ; s++) {
if (*s == '\0')
break;
break; /* the ansi panoply */
if (*s == '*') {
if (a == NULL) {
}
x = execute(a);
a = a->nnext;
tcnt--;
(int)getfval(x));
tempfree(x, "");
}
}
switch (*s) {
case 'f': case 'e': case 'g': case 'E': case 'G':
flag = 1;
break;
case 'd': case 'i':
flag = 2;
if (*(s-1) == 'l')
break;
break;
case 'o': case 'x': case 'X': case 'u':
break;
case 's':
flag = 4;
break;
case 'c':
flag = 5;
break;
default:
flag = 0;
break;
}
if (flag == 0) {
continue;
}
if (a == NULL) {
}
x = execute(a);
a = a->nnext;
for (;;) {
/* make sure we have at least 1 byte space */
switch (flag) {
case 1:
/*LINTED*/
break;
case 2:
/*LINTED*/
break;
case 3:
/*LINTED*/
break;
case 4:
/*LINTED*/
break;
case 5:
if (isnum(x)) {
/*LINTED*/
} else {
/*LINTED*/
}
break;
default:
ret = 0;
}
break;
}
tempfree(x, "");
s++;
}
for (; a; a = a->nnext) /* evaluate any remaining args */
(void) execute(a);
}
/*ARGSUSED*/
Cell *
{
register Cell *x;
register Node *y;
y = a[0]->nnext;
x = execute(a[0]);
tempfree(x, "");
x = gettemp("");
return (x);
}
/*ARGSUSED*/
Cell *
{
register Cell *x;
register Node *y;
y = a[0]->nnext;
x = execute(a[0]);
tempfree(x, "");
if (a[1] == NULL)
else {
}
return (true);
}
Cell *
{
Awkfloat i, j;
double v;
register Cell *x, *y, *z;
x = execute(a[0]);
i = getfval(x);
tempfree(x, "");
if (n != UMINUS) {
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)
i /= j;
break;
case MOD:
if (j == 0)
(void) modf(i/j, &v);
i = i - j * v;
break;
case UMINUS:
i = -i;
break;
case POWER:
i = ipow(i, (int)j);
else
break;
default: /* can't happen */
}
(void) setfval(z, i);
return (z);
}
static double
ipow(double x, int n)
{
double v;
if (n <= 0)
return (1.0);
v = ipow(x, n/2);
if (n % 2 == 0)
return (v * v);
else
return (x * v * v);
}
Cell *
{
register Cell *x, *z;
register int k;
x = execute(a[0]);
return (x);
}
z = gettemp("");
tempfree(x, "");
return (z);
}
Cell *
{
register Cell *x, *y;
double v;
y = execute(a[1]);
x = execute(a[0]); /* order reversed from before... */
if (n == ASSIGN) { /* ordinary assignment */
else
funnyvar(y, "read value of");
tempfree(y, "");
return (x);
}
switch (n) {
case ADDEQ:
break;
case SUBEQ:
break;
case MULTEQ:
break;
case DIVEQ:
if (yf == 0)
break;
case MODEQ:
if (yf == 0)
break;
case POWEQ:
else
break;
default:
break;
}
tempfree(y, "");
return (x);
}
/*ARGSUSED*/
Cell *
{
register Cell *x, *y, *z;
register uchar *s;
x = execute(a[0]);
y = execute(a[1]);
(void) getsval(x);
(void) getsval(y);
if (s == NULL) {
ERROR "out of space concatenating %.15s and %.15s",
}
tempfree(y, "");
z = gettemp("");
z->sval = s;
tempfree(x, "");
return (z);
}
/*ARGSUSED*/
Cell *
{
register Cell *x;
if (a[0] == 0)
x = execute(a[1]);
else {
x = execute(a[0]);
if (istrue(x)) {
tempfree(x, "");
x = execute(a[1]);
}
}
return (x);
}
/*ARGSUSED*/
Cell *
{
Cell *x;
int pair;
if (!pairstack) {
/* first time */
if (!pairstack)
}
pair = (int)a[3];
x = execute(a[0]);
if (istrue(x))
tempfree(x, "");
}
x = execute(a[1]);
if (istrue(x))
tempfree(x, "");
x = execute(a[2]);
return (x);
}
return (false);
}
/*ARGSUSED*/
Cell *
{
register uchar *s;
register int sep;
int n, tempstat;
y = execute(a[0]); /* source string */
s = getsval(y);
if (a[2] == 0) /* fs string */
x = execute(a[2]);
} else if ((int)a[3] == REGEXPR)
else
freesymtab(ap);
n = 0;
/* reg expr */
} else {
}
do {
n++;
*patbeg = '\0';
if (is_number(s)) {
atof((char *)s),
/*LINTED align*/
} else {
/*LINTED align*/
}
n++;
/*LINTED align*/
goto spdone;
}
}
n++;
if (is_number(s)) {
/*LINTED align*/
} else {
/*LINTED align*/
}
} else if (sep == ' ') {
for (n = 0; ; ) {
while (*s == ' ' || *s == '\t' || *s == '\n')
s++;
if (*s == 0)
break;
n++;
t = s;
do
s++;
while (*s != ' ' && *s != '\t' &&
*s != '\n' && *s != '\0')
;
temp = *s;
*s = '\0';
if (is_number(t)) {
/*LINTED align*/
} else {
/*LINTED align*/
}
*s = temp;
if (*s != 0)
s++;
}
} else if (*s != 0) {
for (;;) {
n++;
t = s;
s++;
temp = *s;
*s = '\0';
if (is_number(t)) {
/*LINTED align*/
} else {
/*LINTED align*/
}
*s = temp;
if (*s++ == 0)
break;
}
}
tempfree(y, "");
tempfree(x, "");
x = gettemp("");
x->fval = n;
return (x);
}
/*ARGSUSED*/
Cell *
{
register Cell *x;
x = execute(a[0]);
if (istrue(x)) {
tempfree(x, "");
x = execute(a[1]);
} else {
tempfree(x, "");
x = execute(a[2]);
}
return (x);
}
/*ARGSUSED*/
Cell *
{
register Cell *x;
x = execute(a[0]);
if (istrue(x)) {
tempfree(x, "");
x = execute(a[1]);
} else if (a[2] != 0) {
tempfree(x, "");
x = execute(a[2]);
}
return (x);
}
/*ARGSUSED*/
Cell *
{
register 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);
}
return (x);
tempfree(x, "");
}
}
/*ARGSUSED*/
Cell *
{
register Cell *x;
for (;;) {
x = execute(a[0]);
if (isbreak(x))
return (true);
return (x);
tempfree(x, "");
x = execute(a[1]);
if (!istrue(x))
return (x);
tempfree(x, "");
}
}
/*ARGSUSED*/
Cell *
{
register Cell *x;
x = execute(a[0]);
tempfree(x, "");
for (;;) {
if (a[1] != 0) {
x = execute(a[1]);
if (!istrue(x))
return (x);
else
tempfree(x, "");
}
x = execute(a[3]);
if (isbreak(x)) /* turn off break */
return (true);
return (x);
tempfree(x, "");
x = execute(a[2]);
tempfree(x, "");
}
}
/*ARGSUSED*/
Cell *
{
int i;
/*LINTED align*/
x = execute(a[2]);
if (isbreak(x)) {
return (true);
}
return (x);
}
tempfree(x, "");
}
}
return (true);
}
/*ARGSUSED*/
Cell *
{
register Cell *x, *y;
Awkfloat u;
register int t;
t = (int)a[0];
x = execute(a[1]);
switch (t) {
case FLENGTH:
case FLOG:
case FINT:
case FEXP:
case FSQRT:
case FSIN:
case FCOS:
case FATAN:
if (nextarg == 0) {
ERROR "atan2 requires two arguments; returning 1.0"
u = 1.0;
} else {
tempfree(y, "");
}
break;
case FSYSTEM:
/* in case something is buffered already */
/* 256 is unix-dep */
break;
case FRAND:
break;
case FSRAND:
else
u = getfval(x);
srand((int)u); u = (int)u;
break;
case FTOUPPER:
case FTOLOWER:
if (t == FTOUPPER) {
for (p = buf; *p; p++)
if (islower(*p))
*p = toupper(*p);
} else {
for (p = buf; *p; p++)
if (isupper(*p))
*p = tolower(*p);
}
tempfree(x, "");
x = gettemp("");
return (x);
default: /* can't happen */
break;
}
tempfree(x, "");
x = gettemp("");
(void) setfval(x, u);
if (nextarg != 0) {
}
return (x);
}
/*ARGSUSED*/
Cell *
{
register Node *x;
register Cell *y;
if (a[1] == 0)
else
y = execute(x);
tempfree(y, "");
else
}
if (a[1] != 0)
return (true);
}
/*ARGSUSED*/
Cell *
{
return (0);
}
struct {
int mode; /* '|', 'a', 'w' */
static FILE *
{
Cell *x;
x = execute(b);
tempfree(x, "");
return (fp);
}
static FILE *
{
register int i, m;
if (*s == '\0')
for (i = 0; i < FOPEN_MAX; i++) {
}
}
}
for (i = 0; i < FOPEN_MAX; i++) {
break;
}
if (i >= FOPEN_MAX)
m = a;
if (a == GT) {
} else if (a == APPEND) {
m = GT; /* so can mix > and >> */
} else if (a == '|') { /* output pipe */
} else if (a == LE) { /* input pipe */
} else if (a == LT) { /* getline <file */
} else /* can't happen */
}
return (fp);
}
/*ARGSUSED*/
Cell *
{
register Cell *x;
int i, stat;
x = execute(a[0]);
(void) getsval(x);
for (i = 0; i < FOPEN_MAX; i++) {
ERROR "i/o error occurred on %s",
}
else
ERROR "i/o error occurred closing %s",
}
/* watch out for ref thru this */
}
}
tempfree(x, "close");
return (true);
}
static void
closeall(void)
{
int i, stat;
for (i = 0; i < FOPEN_MAX; i++) {
ERROR "i/o error occurred on %s",
}
else
ERROR "i/o error occurred while closing %s",
}
}
}
}
/*ARGSUSED*/
Cell *
{
t = getsval(x);
if (a[0] == 0)
else {
y = execute(a[1]);
tempfree(y, "");
}
result = false;
cnt = 0;
sptr = t;
if (len > 0) {
}
while (*sptr != 0) {
if (*sptr == '\\' &&
sptr++; /* skip \, */
} else if (*sptr == '&') {
sptr++;
} else {
}
}
}
result = true;
}
tempfree(x, "");
tempfree(y, "");
return (result);
}
/*ARGSUSED*/
Cell *
{
register Cell *x, *y;
mflag = 0; /* if mflag == 0, can replace empty string */
num = 0;
t = getsval(x);
if (a[0] == 0)
else {
y = execute(a[1]);
tempfree(y, "");
}
cnt = 0;
do {
/* matched empty string */
if (mflag == 0) { /* can replace empty */
num++;
while (*sptr != 0) {
if (*sptr == '\\' &&
sptr++;
} else if (*sptr == '&') {
&bsize,
sptr++;
} else {
}
}
}
if (*t == 0) /* at end */
goto done;
mflag = 0;
} else { /* matched nonempty string */
num++;
sptr = t;
if (len > 0) {
}
while (*sptr != 0) {
if (*sptr == '\\' &&
sptr++;
} else if (*sptr == '&') {
sptr++;
} else {
}
}
if ((*(t-1) == 0) || (*t == 0))
goto done;
mflag = 1;
}
sptr = t;
done:
}
tempfree(x, "");
tempfree(y, "");
x = gettemp("");
return (x);
}