10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
220Sstevel@tonic-gate
230Sstevel@tonic-gate /*
240Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
250Sstevel@tonic-gate * Use is subject to license terms.
260Sstevel@tonic-gate */
270Sstevel@tonic-gate
28*731Srobbin /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
29*731Srobbin /* All Rights Reserved */
30*731Srobbin
31*731Srobbin #pragma ident "%Z%%M% %I% %E% SMI"
320Sstevel@tonic-gate
330Sstevel@tonic-gate #include "stdio.h"
340Sstevel@tonic-gate #include "awk.def"
350Sstevel@tonic-gate #include "awk.h"
36*731Srobbin #include <stdlib.h>
370Sstevel@tonic-gate
380Sstevel@tonic-gate CELL *symtab[MAXSYM]; /* symbol table pointers */
390Sstevel@tonic-gate
400Sstevel@tonic-gate
410Sstevel@tonic-gate wchar_t **FS; /* initial field sep */
420Sstevel@tonic-gate wchar_t **RS; /* initial record sep */
430Sstevel@tonic-gate wchar_t **OFS; /* output field sep */
440Sstevel@tonic-gate wchar_t **ORS; /* output record sep */
450Sstevel@tonic-gate wchar_t **OFMT; /* output format for numbers */
460Sstevel@tonic-gate awkfloat *NF; /* number of fields in current record */
470Sstevel@tonic-gate awkfloat *NR; /* number of current record */
480Sstevel@tonic-gate wchar_t **FILENAME; /* current filename argument */
490Sstevel@tonic-gate
500Sstevel@tonic-gate
510Sstevel@tonic-gate CELL *recloc; /* location of record */
520Sstevel@tonic-gate CELL *nrloc; /* NR */
530Sstevel@tonic-gate CELL *nfloc; /* NF */
540Sstevel@tonic-gate
55*731Srobbin void recbld(void);
560Sstevel@tonic-gate
57*731Srobbin void
syminit(void)58*731Srobbin syminit(void)
590Sstevel@tonic-gate {
600Sstevel@tonic-gate static wchar_t L_0[] = L"0";
610Sstevel@tonic-gate static wchar_t L_zeronull[] = L"$zero&null";
620Sstevel@tonic-gate static wchar_t L_record[] = L"$record";
630Sstevel@tonic-gate static wchar_t L_FS[] = L"FS";
640Sstevel@tonic-gate static wchar_t L_OFS[] = L"OFS";
650Sstevel@tonic-gate static wchar_t L_ORS[] = L"ORS";
660Sstevel@tonic-gate static wchar_t L_RS[] = L"RS";
670Sstevel@tonic-gate static wchar_t L_OFMT[] = L"OFMT";
680Sstevel@tonic-gate static wchar_t L_space[] = L" ";
690Sstevel@tonic-gate static wchar_t L_newline[] = L"\n";
700Sstevel@tonic-gate static wchar_t L_dot6g[] = L"%.6g";
710Sstevel@tonic-gate static wchar_t L_FILENAME[] = L"FILENAME";
720Sstevel@tonic-gate static wchar_t L_NF[] = L"NF";
730Sstevel@tonic-gate static wchar_t L_NR[] = L"NR";
740Sstevel@tonic-gate
750Sstevel@tonic-gate
760Sstevel@tonic-gate setsymtab(L_0, tostring(L_0), 0.0, NUM|STR|CON|FLD, symtab);
770Sstevel@tonic-gate /* this one is used for if (x)... tests: */
780Sstevel@tonic-gate setsymtab(L_zeronull, tostring(L_NULL), 0.0, NUM|STR|CON|FLD, symtab);
790Sstevel@tonic-gate recloc = setsymtab(L_record, record, 0.0, STR|FLD, symtab);
800Sstevel@tonic-gate dprintf("recloc %o lookup %o\n",
810Sstevel@tonic-gate recloc, lookup(L_record, symtab, 0), NULL);
820Sstevel@tonic-gate FS = &setsymtab(L_FS, tostring(L_space), 0.0, STR|FLD, symtab)->sval;
830Sstevel@tonic-gate RS = &setsymtab(L_RS, tostring(L_newline), 0.0, STR|FLD, symtab)->sval;
840Sstevel@tonic-gate OFS = &setsymtab(L_OFS, tostring(L_space), 0.0, STR|FLD, symtab)->sval;
850Sstevel@tonic-gate ORS = &setsymtab(L_ORS, tostring(L_newline), 0.0, STR|FLD,
860Sstevel@tonic-gate symtab)->sval;
870Sstevel@tonic-gate OFMT = &setsymtab(L_OFMT, tostring(L_dot6g), 0.0, STR|FLD,
880Sstevel@tonic-gate symtab)->sval;
890Sstevel@tonic-gate FILENAME = &setsymtab(L_FILENAME, NULL, 0.0, STR|FLD, symtab)->sval;
900Sstevel@tonic-gate nfloc = setsymtab(L_NF, NULL, 0.0, NUM, symtab);
910Sstevel@tonic-gate NF = &nfloc->fval;
920Sstevel@tonic-gate nrloc = setsymtab(L_NR, NULL, 0.0, NUM, symtab);
930Sstevel@tonic-gate NR = &nrloc->fval;
940Sstevel@tonic-gate }
950Sstevel@tonic-gate
960Sstevel@tonic-gate
97*731Srobbin CELL **
makesymtab(void)98*731Srobbin makesymtab(void)
990Sstevel@tonic-gate {
1000Sstevel@tonic-gate int i;
1010Sstevel@tonic-gate CELL **cp;
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate cp = (CELL **) malloc(MAXSYM * sizeof (CELL *));
1050Sstevel@tonic-gate if (cp == NULL)
1060Sstevel@tonic-gate error(FATAL, "out of space in makesymtab");
1070Sstevel@tonic-gate for (i = 0; i < MAXSYM; i++)
1080Sstevel@tonic-gate cp[i] = 0;
1090Sstevel@tonic-gate return (cp);
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate
113*731Srobbin void
freesymtab(CELL * ap)114*731Srobbin freesymtab(CELL *ap) /* free symbol table */
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate CELL *cp, **tp, *next;
1170Sstevel@tonic-gate int i;
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate if (!(ap->tval & ARR))
1210Sstevel@tonic-gate return;
1220Sstevel@tonic-gate tp = (CELL **) ap->sval;
1230Sstevel@tonic-gate for (i = 0; i < MAXSYM; i++) {
1240Sstevel@tonic-gate for (cp = tp[i]; cp != NULL; cp = next) {
1250Sstevel@tonic-gate next = cp->nextval;
1260Sstevel@tonic-gate xfree(cp->nval);
1270Sstevel@tonic-gate xfree(cp->sval);
1280Sstevel@tonic-gate free(cp);
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate xfree(tp);
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate
135*731Srobbin CELL *
setsymtab(wchar_t * n,wchar_t * s,awkfloat f,unsigned t,CELL ** tab)136*731Srobbin setsymtab(wchar_t *n, wchar_t *s, awkfloat f, unsigned t, CELL **tab)
1370Sstevel@tonic-gate {
138*731Srobbin int h;
139*731Srobbin CELL *p;
1400Sstevel@tonic-gate CELL *lookup();
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate if (n != NULL && (p = lookup(n, tab, 0)) != NULL) {
1440Sstevel@tonic-gate xfree(s);
1450Sstevel@tonic-gate dprintf("setsymtab found %o: %ws\n", p, p->nval, NULL);
1460Sstevel@tonic-gate dprintf(" %ws %g %o\n", p->sval, p->fval, p->tval);
1470Sstevel@tonic-gate return (p);
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate p = (CELL *) malloc(sizeof (CELL));
1500Sstevel@tonic-gate if (p == NULL)
1510Sstevel@tonic-gate error(FATAL, "symbol table overflow at %ws", n);
1520Sstevel@tonic-gate p->nval = tostring(n);
1530Sstevel@tonic-gate p->sval = s;
1540Sstevel@tonic-gate p->fval = f;
1550Sstevel@tonic-gate p->tval = t;
1560Sstevel@tonic-gate h = hash(n);
1570Sstevel@tonic-gate p->nextval = tab[h];
1580Sstevel@tonic-gate tab[h] = p;
1590Sstevel@tonic-gate dprintf("setsymtab set %o: %ws\n", p, p->nval, NULL);
1600Sstevel@tonic-gate dprintf(" %ws %g %o\n", p->sval, p->fval, p->tval);
1610Sstevel@tonic-gate return (p);
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate
165*731Srobbin int
hash(wchar_t * s)166*731Srobbin hash(wchar_t *s) /* form hash value for string s */
1670Sstevel@tonic-gate {
168*731Srobbin unsigned hashval;
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate for (hashval = 0; *s != '\0'; /* dummy */)
1720Sstevel@tonic-gate hashval += *s++;
1730Sstevel@tonic-gate return (hashval % MAXSYM);
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate
177*731Srobbin CELL *
lookup(wchar_t * s,CELL ** tab,int flag)178*731Srobbin lookup(wchar_t *s, CELL **tab, int flag)
179*731Srobbin /* look for s in tab, flag must match */
1800Sstevel@tonic-gate {
181*731Srobbin CELL *p;
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate for (p = tab[hash(s)]; p != NULL; p = p->nextval)
1850Sstevel@tonic-gate if (wscmp(s, p->nval) == 0 &&
1860Sstevel@tonic-gate (flag == 0 || flag == p->tval))
1870Sstevel@tonic-gate return (p); /* found it */
1880Sstevel@tonic-gate return (NULL); /* not found */
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate
192*731Srobbin awkfloat
setfval(CELL * vp,awkfloat f)193*731Srobbin setfval(CELL *vp, awkfloat f)
1940Sstevel@tonic-gate {
1950Sstevel@tonic-gate dprintf("setfval: %o %g\n", vp, f, NULL);
1960Sstevel@tonic-gate /* imb */
1970Sstevel@tonic-gate if (vp->tval & ARR)
1980Sstevel@tonic-gate error(FATAL, "illegal reference to array %s", vp->nval);
1990Sstevel@tonic-gate if ((vp->tval & (NUM | STR)) == 0)
2000Sstevel@tonic-gate error(FATAL, "funny variable %o: %ws %ws %g %o", vp, vp->nval,
2010Sstevel@tonic-gate vp->sval, vp->fval, vp->tval);
2020Sstevel@tonic-gate /* imb */
2030Sstevel@tonic-gate if (vp == recloc)
2040Sstevel@tonic-gate error(FATAL, "can't set $0");
2050Sstevel@tonic-gate vp->tval &= ~STR; /* mark string invalid */
2060Sstevel@tonic-gate vp->tval |= NUM; /* mark number ok */
2070Sstevel@tonic-gate if ((vp->tval & FLD) && vp->nval == 0) {
2080Sstevel@tonic-gate /*
2090Sstevel@tonic-gate * FLD really means that the string value was not
2100Sstevel@tonic-gate * "malloc"ed and should not be freed. All fields
2110Sstevel@tonic-gate * have this property, but not all cells with this
2120Sstevel@tonic-gate * property are fields. However, all cells with
2130Sstevel@tonic-gate * this property and with a NULL "nval" are fields.
2140Sstevel@tonic-gate * If we are setting the value of a field, indicate
2150Sstevel@tonic-gate * that the value of the record has to be recomputed,
2160Sstevel@tonic-gate * and if it's a higher field than the last one we
2170Sstevel@tonic-gate * assigned to, remember it for when we clear the
2180Sstevel@tonic-gate * fields out for the next record.
2190Sstevel@tonic-gate */
2200Sstevel@tonic-gate donerec = 0;
2210Sstevel@tonic-gate if (vp > maxmfld)
2220Sstevel@tonic-gate maxmfld = vp;
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate return (vp->fval = f);
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate
228*731Srobbin wchar_t *
setsval(CELL * vp,wchar_t * s)229*731Srobbin setsval(CELL *vp, wchar_t *s)
2300Sstevel@tonic-gate {
2310Sstevel@tonic-gate dprintf("setsval: %o %ws\n", vp, s, NULL);
2320Sstevel@tonic-gate if (vp->tval & ARR)
2330Sstevel@tonic-gate error(FATAL, "illegal reference to array %ws", vp->nval);
2340Sstevel@tonic-gate if ((vp->tval & (NUM | STR)) == 0)
2350Sstevel@tonic-gate error(FATAL, "funny variable %o: %ws %ws %g %o", vp, vp->nval,
2360Sstevel@tonic-gate vp->sval, vp->fval, vp->tval);
2370Sstevel@tonic-gate if (vp == recloc)
2380Sstevel@tonic-gate error(FATAL, "can't set $0");
2390Sstevel@tonic-gate vp->tval &= ~NUM;
2400Sstevel@tonic-gate vp->tval |= STR;
2410Sstevel@tonic-gate if ((vp->tval & FLD) && vp->nval == 0) {
2420Sstevel@tonic-gate /*
2430Sstevel@tonic-gate * See comment in "setfval".
2440Sstevel@tonic-gate */
2450Sstevel@tonic-gate donerec = 0;
2460Sstevel@tonic-gate if (vp > maxmfld)
2470Sstevel@tonic-gate maxmfld = vp;
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate if (!(vp->tval&FLD))
2500Sstevel@tonic-gate xfree(vp->sval);
2510Sstevel@tonic-gate vp->tval &= ~FLD;
2520Sstevel@tonic-gate return (vp->sval = tostring(s));
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate
256*731Srobbin awkfloat
getfval(CELL * vp)257*731Srobbin getfval(CELL *vp)
2580Sstevel@tonic-gate {
2590Sstevel@tonic-gate
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate if (vp->sval == record && donerec == 0)
2620Sstevel@tonic-gate recbld();
2630Sstevel@tonic-gate dprintf("getfval: %o", vp, NULL, NULL);
2640Sstevel@tonic-gate if (vp->tval & ARR)
2650Sstevel@tonic-gate error(FATAL, "illegal reference to array %ws", vp->nval);
2660Sstevel@tonic-gate if ((vp->tval & (NUM | STR)) == 0)
2670Sstevel@tonic-gate error(FATAL, "funny variable %o: %ws %ws %g %o", vp, vp->nval,
2680Sstevel@tonic-gate vp->sval, vp->fval, vp->tval);
2690Sstevel@tonic-gate if ((vp->tval & NUM) == 0) {
2700Sstevel@tonic-gate /* the problem is to make non-numeric things */
2710Sstevel@tonic-gate /* have unlikely numeric variables, so that */
2720Sstevel@tonic-gate /* $1 == $2 comparisons sort of make sense when */
2730Sstevel@tonic-gate /* one or the other is numeric */
2740Sstevel@tonic-gate if (isanumber(vp->sval)) {
2750Sstevel@tonic-gate vp->fval = watof(vp->sval);
2760Sstevel@tonic-gate if (!(vp->tval & CON))
2770Sstevel@tonic-gate /* don't change type of a constant */
2780Sstevel@tonic-gate vp->tval |= NUM;
2790Sstevel@tonic-gate }
2800Sstevel@tonic-gate else
2810Sstevel@tonic-gate vp->fval = 0.0; /* not a very good idea */
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate dprintf(" %g\n", vp->fval, NULL, NULL);
2840Sstevel@tonic-gate return (vp->fval);
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate
2870Sstevel@tonic-gate
288*731Srobbin wchar_t *
getsval(CELL * vp)289*731Srobbin getsval(CELL *vp)
2900Sstevel@tonic-gate {
2910Sstevel@tonic-gate char s[100];
2920Sstevel@tonic-gate wchar_t ws[100];
2930Sstevel@tonic-gate
2940Sstevel@tonic-gate
2950Sstevel@tonic-gate if (vp->sval == record && donerec == 0)
2960Sstevel@tonic-gate recbld();
2970Sstevel@tonic-gate dprintf("getsval: %o", vp, NULL, NULL);
2980Sstevel@tonic-gate if (vp->tval & ARR)
2990Sstevel@tonic-gate error(FATAL, "illegal reference to array %ws", vp->nval);
3000Sstevel@tonic-gate if ((vp->tval & (NUM | STR)) == 0)
3010Sstevel@tonic-gate error(FATAL, "funny variable %o: %ws %ws %g %o", vp, vp->nval,
3020Sstevel@tonic-gate vp->sval, vp->fval, vp->tval);
3030Sstevel@tonic-gate if ((vp->tval & STR) == 0) {
3040Sstevel@tonic-gate if (!(vp->tval&FLD))
3050Sstevel@tonic-gate xfree(vp->sval);
306*731Srobbin if ((long long)vp->fval == vp->fval)
3070Sstevel@tonic-gate sprintf(s, "%.20g", vp->fval);
3080Sstevel@tonic-gate else
3090Sstevel@tonic-gate sprintf(s, toeuccode(*OFMT), vp->fval);
3100Sstevel@tonic-gate mbstowcs(ws, s, sizeof (ws) / sizeof (wchar_t));
3110Sstevel@tonic-gate vp->sval = tostring(ws);
3120Sstevel@tonic-gate vp->tval &= ~FLD;
3130Sstevel@tonic-gate vp->tval |= STR;
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate dprintf(" %ws\n", vp->sval, NULL, NULL);
3160Sstevel@tonic-gate return (vp->sval);
3170Sstevel@tonic-gate }
3180Sstevel@tonic-gate
3190Sstevel@tonic-gate
320*731Srobbin wchar_t *
tostring(wchar_t * s)321*731Srobbin tostring(wchar_t *s)
3220Sstevel@tonic-gate {
323*731Srobbin wchar_t *p;
3240Sstevel@tonic-gate
3250Sstevel@tonic-gate
3260Sstevel@tonic-gate p = (wchar_t *) malloc((wslen(s)+1)*sizeof (wchar_t));
3270Sstevel@tonic-gate if (p == NULL)
3280Sstevel@tonic-gate error(FATAL, "out of space in tostring on %ws", s);
3290Sstevel@tonic-gate wscpy(p, s);
3300Sstevel@tonic-gate return (p);
3310Sstevel@tonic-gate }
3320Sstevel@tonic-gate
3330Sstevel@tonic-gate
3340Sstevel@tonic-gate #ifndef yfree
yfree(char * a)335*731Srobbin yfree(char *a)
3360Sstevel@tonic-gate {
3370Sstevel@tonic-gate printf("%o\n", a);
3380Sstevel@tonic-gate free(a);
3390Sstevel@tonic-gate }
3400Sstevel@tonic-gate #endif
341