1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 23*0Sstevel@tonic-gate /* All Rights Reserved */ 24*0Sstevel@tonic-gate 25*0Sstevel@tonic-gate 26*0Sstevel@tonic-gate /* 27*0Sstevel@tonic-gate * Copyright (c) 1996-2001 by Sun Microsystems, Inc. 28*0Sstevel@tonic-gate * All rights reserved. 29*0Sstevel@tonic-gate */ 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 2.13 */ 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate #include <stdio.h> 34*0Sstevel@tonic-gate #include <ctype.h> 35*0Sstevel@tonic-gate #include <errno.h> 36*0Sstevel@tonic-gate #include <libintl.h> 37*0Sstevel@tonic-gate #include "awk.h" 38*0Sstevel@tonic-gate #include "y.tab.h" 39*0Sstevel@tonic-gate 40*0Sstevel@tonic-gate #define getfval(p) \ 41*0Sstevel@tonic-gate (((p)->tval & (ARR|FLD|REC|NUM)) == NUM ? (p)->fval : r_getfval(p)) 42*0Sstevel@tonic-gate #define getsval(p) \ 43*0Sstevel@tonic-gate (((p)->tval & (ARR|FLD|REC|STR)) == STR ? (p)->sval : r_getsval(p)) 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate extern Awkfloat r_getfval(); 46*0Sstevel@tonic-gate extern uchar *r_getsval(); 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gate FILE *infile = NULL; 49*0Sstevel@tonic-gate uchar *file = (uchar*) ""; 50*0Sstevel@tonic-gate uchar recdata[RECSIZE]; 51*0Sstevel@tonic-gate uchar *record = recdata; 52*0Sstevel@tonic-gate uchar fields[RECSIZE]; 53*0Sstevel@tonic-gate 54*0Sstevel@tonic-gate int donefld; /* 1 = implies rec broken into fields */ 55*0Sstevel@tonic-gate int donerec; /* 1 = record is valid (no flds have changed) */ 56*0Sstevel@tonic-gate 57*0Sstevel@tonic-gate Cell fldtab[MAXFLD]; /* room for fields */ 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate int maxfld = 0; /* last used field */ 60*0Sstevel@tonic-gate int argno = 1; /* current input argument number */ 61*0Sstevel@tonic-gate extern Awkfloat *ARGC; 62*0Sstevel@tonic-gate extern uchar *getargv(); 63*0Sstevel@tonic-gate 64*0Sstevel@tonic-gate initgetrec() 65*0Sstevel@tonic-gate { 66*0Sstevel@tonic-gate int i; 67*0Sstevel@tonic-gate uchar *p; 68*0Sstevel@tonic-gate 69*0Sstevel@tonic-gate for (i = 1; i < *ARGC; i++) { 70*0Sstevel@tonic-gate if (!isclvar(p = getargv(i))) /* find 1st real filename */ 71*0Sstevel@tonic-gate return; 72*0Sstevel@tonic-gate setclvar(p); /* a commandline assignment before filename */ 73*0Sstevel@tonic-gate argno++; 74*0Sstevel@tonic-gate } 75*0Sstevel@tonic-gate infile = stdin; /* no filenames, so use stdin */ 76*0Sstevel@tonic-gate /* *FILENAME = file = (uchar*) "-"; */ 77*0Sstevel@tonic-gate } 78*0Sstevel@tonic-gate 79*0Sstevel@tonic-gate getrec(buf) 80*0Sstevel@tonic-gate uchar *buf; 81*0Sstevel@tonic-gate { 82*0Sstevel@tonic-gate int c; 83*0Sstevel@tonic-gate static int firsttime = 1; 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gate if (firsttime) { 86*0Sstevel@tonic-gate firsttime = 0; 87*0Sstevel@tonic-gate initgetrec(); 88*0Sstevel@tonic-gate } 89*0Sstevel@tonic-gate dprintf(("RS=<%s>, FS=<%s>, ARGC=%f, FILENAME=%s\n", 90*0Sstevel@tonic-gate *RS, *FS, *ARGC, *FILENAME)); 91*0Sstevel@tonic-gate donefld = 0; 92*0Sstevel@tonic-gate donerec = 1; 93*0Sstevel@tonic-gate buf[0] = 0; 94*0Sstevel@tonic-gate while (argno < *ARGC || infile == stdin) { 95*0Sstevel@tonic-gate dprintf(("argno=%d, file=|%s|\n", argno, file)); 96*0Sstevel@tonic-gate if (infile == NULL) { /* have to open a new file */ 97*0Sstevel@tonic-gate file = getargv(argno); 98*0Sstevel@tonic-gate if (*file == '\0') { /* it's been zapped */ 99*0Sstevel@tonic-gate argno++; 100*0Sstevel@tonic-gate continue; 101*0Sstevel@tonic-gate } 102*0Sstevel@tonic-gate if (isclvar(file)) { /* a var=value arg */ 103*0Sstevel@tonic-gate setclvar(file); 104*0Sstevel@tonic-gate argno++; 105*0Sstevel@tonic-gate continue; 106*0Sstevel@tonic-gate } 107*0Sstevel@tonic-gate *FILENAME = file; 108*0Sstevel@tonic-gate dprintf(("opening file %s\n", file)); 109*0Sstevel@tonic-gate if (*file == '-' && *(file+1) == '\0') 110*0Sstevel@tonic-gate infile = stdin; 111*0Sstevel@tonic-gate else if ((infile = fopen((char *)file, "r")) == NULL) 112*0Sstevel@tonic-gate ERROR "can't open file %s", file FATAL; 113*0Sstevel@tonic-gate setfval(fnrloc, 0.0); 114*0Sstevel@tonic-gate } 115*0Sstevel@tonic-gate c = readrec(buf, RECSIZE, infile); 116*0Sstevel@tonic-gate if (c != 0 || buf[0] != '\0') { /* normal record */ 117*0Sstevel@tonic-gate if (buf == record) { 118*0Sstevel@tonic-gate if (!(recloc->tval & DONTFREE)) 119*0Sstevel@tonic-gate xfree(recloc->sval); 120*0Sstevel@tonic-gate recloc->sval = record; 121*0Sstevel@tonic-gate recloc->tval = REC | STR | DONTFREE; 122*0Sstevel@tonic-gate if (isnumber(recloc->sval)) { 123*0Sstevel@tonic-gate recloc->fval = atof(recloc->sval); 124*0Sstevel@tonic-gate recloc->tval |= NUM; 125*0Sstevel@tonic-gate } 126*0Sstevel@tonic-gate } 127*0Sstevel@tonic-gate setfval(nrloc, nrloc->fval+1); 128*0Sstevel@tonic-gate setfval(fnrloc, fnrloc->fval+1); 129*0Sstevel@tonic-gate return (1); 130*0Sstevel@tonic-gate } 131*0Sstevel@tonic-gate /* EOF arrived on this file; set up next */ 132*0Sstevel@tonic-gate if (infile != stdin) 133*0Sstevel@tonic-gate fclose(infile); 134*0Sstevel@tonic-gate infile = NULL; 135*0Sstevel@tonic-gate argno++; 136*0Sstevel@tonic-gate } 137*0Sstevel@tonic-gate return (0); /* true end of file */ 138*0Sstevel@tonic-gate } 139*0Sstevel@tonic-gate 140*0Sstevel@tonic-gate readrec(buf, bufsize, inf) /* read one record into buf */ 141*0Sstevel@tonic-gate uchar *buf; 142*0Sstevel@tonic-gate int bufsize; 143*0Sstevel@tonic-gate FILE *inf; 144*0Sstevel@tonic-gate { 145*0Sstevel@tonic-gate register int sep, c; 146*0Sstevel@tonic-gate register uchar *rr; 147*0Sstevel@tonic-gate int count; 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gate if ((sep = **RS) == 0) { 150*0Sstevel@tonic-gate sep = '\n'; 151*0Sstevel@tonic-gate /* skip leading \n's */ 152*0Sstevel@tonic-gate while ((c = getc(inf)) == '\n' && c != EOF) 153*0Sstevel@tonic-gate ; 154*0Sstevel@tonic-gate if (c != EOF) 155*0Sstevel@tonic-gate ungetc(c, inf); 156*0Sstevel@tonic-gate } 157*0Sstevel@tonic-gate for (rr = buf, count = 0; ; ) { 158*0Sstevel@tonic-gate while ((c = getc(inf)) != sep && c != EOF) { 159*0Sstevel@tonic-gate count++; 160*0Sstevel@tonic-gate if (count > bufsize) 161*0Sstevel@tonic-gate ERROR "input record `%.20s...' too long", 162*0Sstevel@tonic-gate buf FATAL; 163*0Sstevel@tonic-gate *rr++ = c; 164*0Sstevel@tonic-gate } 165*0Sstevel@tonic-gate if (**RS == sep || c == EOF) 166*0Sstevel@tonic-gate break; 167*0Sstevel@tonic-gate if ((c = getc(inf)) == '\n' || c == EOF) /* 2 in a row */ 168*0Sstevel@tonic-gate break; 169*0Sstevel@tonic-gate count += 2; 170*0Sstevel@tonic-gate if (count > bufsize) 171*0Sstevel@tonic-gate ERROR "input record `%.20s...' too long", buf FATAL; 172*0Sstevel@tonic-gate *rr++ = '\n'; 173*0Sstevel@tonic-gate *rr++ = c; 174*0Sstevel@tonic-gate } 175*0Sstevel@tonic-gate *rr = 0; 176*0Sstevel@tonic-gate dprintf(("readrec saw <%s>, returns %d\n", 177*0Sstevel@tonic-gate buf, c == EOF && rr == buf ? 0 : 1)); 178*0Sstevel@tonic-gate return (c == EOF && rr == buf ? 0 : 1); 179*0Sstevel@tonic-gate } 180*0Sstevel@tonic-gate 181*0Sstevel@tonic-gate /* get ARGV[n] */ 182*0Sstevel@tonic-gate uchar * 183*0Sstevel@tonic-gate getargv(n) 184*0Sstevel@tonic-gate int n; 185*0Sstevel@tonic-gate { 186*0Sstevel@tonic-gate Cell *x; 187*0Sstevel@tonic-gate uchar *s, temp[10]; 188*0Sstevel@tonic-gate extern Array *ARGVtab; 189*0Sstevel@tonic-gate 190*0Sstevel@tonic-gate sprintf((char *)temp, "%d", n); 191*0Sstevel@tonic-gate x = setsymtab(temp, "", 0.0, STR, ARGVtab); 192*0Sstevel@tonic-gate s = getsval(x); 193*0Sstevel@tonic-gate dprintf(("getargv(%d) returns |%s|\n", n, s)); 194*0Sstevel@tonic-gate return (s); 195*0Sstevel@tonic-gate } 196*0Sstevel@tonic-gate 197*0Sstevel@tonic-gate setclvar(s) /* set var=value from s */ 198*0Sstevel@tonic-gate uchar *s; 199*0Sstevel@tonic-gate { 200*0Sstevel@tonic-gate uchar *p; 201*0Sstevel@tonic-gate Cell *q; 202*0Sstevel@tonic-gate 203*0Sstevel@tonic-gate for (p = s; *p != '='; p++) 204*0Sstevel@tonic-gate ; 205*0Sstevel@tonic-gate *p++ = 0; 206*0Sstevel@tonic-gate p = qstring(p, '\0'); 207*0Sstevel@tonic-gate q = setsymtab(s, p, 0.0, STR, symtab); 208*0Sstevel@tonic-gate setsval(q, p); 209*0Sstevel@tonic-gate if (isnumber(q->sval)) { 210*0Sstevel@tonic-gate q->fval = atof(q->sval); 211*0Sstevel@tonic-gate q->tval |= NUM; 212*0Sstevel@tonic-gate } 213*0Sstevel@tonic-gate dprintf(("command line set %s to |%s|\n", s, p)); 214*0Sstevel@tonic-gate } 215*0Sstevel@tonic-gate 216*0Sstevel@tonic-gate 217*0Sstevel@tonic-gate fldbld() 218*0Sstevel@tonic-gate { 219*0Sstevel@tonic-gate register uchar *r, *fr, sep; 220*0Sstevel@tonic-gate Cell *p; 221*0Sstevel@tonic-gate int i; 222*0Sstevel@tonic-gate 223*0Sstevel@tonic-gate if (donefld) 224*0Sstevel@tonic-gate return; 225*0Sstevel@tonic-gate if (!(recloc->tval & STR)) 226*0Sstevel@tonic-gate getsval(recloc); 227*0Sstevel@tonic-gate r = recloc->sval; /* was record! */ 228*0Sstevel@tonic-gate fr = fields; 229*0Sstevel@tonic-gate i = 0; /* number of fields accumulated here */ 230*0Sstevel@tonic-gate if (strlen(*FS) > 1) { /* it's a regular expression */ 231*0Sstevel@tonic-gate i = refldbld(r, *FS); 232*0Sstevel@tonic-gate } else if ((sep = **FS) == ' ') { 233*0Sstevel@tonic-gate for (i = 0; ; ) { 234*0Sstevel@tonic-gate while (*r == ' ' || *r == '\t' || *r == '\n') 235*0Sstevel@tonic-gate r++; 236*0Sstevel@tonic-gate if (*r == 0) 237*0Sstevel@tonic-gate break; 238*0Sstevel@tonic-gate i++; 239*0Sstevel@tonic-gate if (i >= MAXFLD) 240*0Sstevel@tonic-gate break; 241*0Sstevel@tonic-gate if (!(fldtab[i].tval & DONTFREE)) 242*0Sstevel@tonic-gate xfree(fldtab[i].sval); 243*0Sstevel@tonic-gate fldtab[i].sval = fr; 244*0Sstevel@tonic-gate fldtab[i].tval = FLD | STR | DONTFREE; 245*0Sstevel@tonic-gate do 246*0Sstevel@tonic-gate *fr++ = *r++; 247*0Sstevel@tonic-gate while (*r != ' ' && *r != '\t' && *r != '\n' && 248*0Sstevel@tonic-gate *r != '\0'); 249*0Sstevel@tonic-gate *fr++ = 0; 250*0Sstevel@tonic-gate } 251*0Sstevel@tonic-gate *fr = 0; 252*0Sstevel@tonic-gate } else if (*r != 0) { /* if 0, it's a null field */ 253*0Sstevel@tonic-gate for (;;) { 254*0Sstevel@tonic-gate i++; 255*0Sstevel@tonic-gate if (i >= MAXFLD) 256*0Sstevel@tonic-gate break; 257*0Sstevel@tonic-gate if (!(fldtab[i].tval & DONTFREE)) 258*0Sstevel@tonic-gate xfree(fldtab[i].sval); 259*0Sstevel@tonic-gate fldtab[i].sval = fr; 260*0Sstevel@tonic-gate fldtab[i].tval = FLD | STR | DONTFREE; 261*0Sstevel@tonic-gate /* \n always a separator */ 262*0Sstevel@tonic-gate while (*r != sep && *r != '\n' && *r != '\0') 263*0Sstevel@tonic-gate *fr++ = *r++; 264*0Sstevel@tonic-gate *fr++ = 0; 265*0Sstevel@tonic-gate if (*r++ == 0) 266*0Sstevel@tonic-gate break; 267*0Sstevel@tonic-gate } 268*0Sstevel@tonic-gate *fr = 0; 269*0Sstevel@tonic-gate } 270*0Sstevel@tonic-gate if (i >= MAXFLD) 271*0Sstevel@tonic-gate ERROR "record `%.20s...' has too many fields", record FATAL; 272*0Sstevel@tonic-gate /* clean out junk from previous record */ 273*0Sstevel@tonic-gate cleanfld(i, maxfld); 274*0Sstevel@tonic-gate maxfld = i; 275*0Sstevel@tonic-gate donefld = 1; 276*0Sstevel@tonic-gate for (p = fldtab+1; p <= fldtab+maxfld; p++) { 277*0Sstevel@tonic-gate if (isnumber(p->sval)) { 278*0Sstevel@tonic-gate p->fval = atof(p->sval); 279*0Sstevel@tonic-gate p->tval |= NUM; 280*0Sstevel@tonic-gate } 281*0Sstevel@tonic-gate } 282*0Sstevel@tonic-gate setfval(nfloc, (Awkfloat) maxfld); 283*0Sstevel@tonic-gate if (dbg) 284*0Sstevel@tonic-gate for (p = fldtab; p <= fldtab+maxfld; p++) 285*0Sstevel@tonic-gate printf("field %d: |%s|\n", p-fldtab, p->sval); 286*0Sstevel@tonic-gate } 287*0Sstevel@tonic-gate 288*0Sstevel@tonic-gate cleanfld(n1, n2) /* clean out fields n1..n2 inclusive */ 289*0Sstevel@tonic-gate { 290*0Sstevel@tonic-gate static uchar *nullstat = (uchar *) ""; 291*0Sstevel@tonic-gate register Cell *p, *q; 292*0Sstevel@tonic-gate 293*0Sstevel@tonic-gate for (p = &fldtab[n2], q = &fldtab[n1]; p > q; p--) { 294*0Sstevel@tonic-gate if (!(p->tval & DONTFREE)) 295*0Sstevel@tonic-gate xfree(p->sval); 296*0Sstevel@tonic-gate p->tval = FLD | STR | DONTFREE; 297*0Sstevel@tonic-gate p->sval = nullstat; 298*0Sstevel@tonic-gate } 299*0Sstevel@tonic-gate } 300*0Sstevel@tonic-gate 301*0Sstevel@tonic-gate newfld(n) /* add field n (after end) */ 302*0Sstevel@tonic-gate { 303*0Sstevel@tonic-gate if (n >= MAXFLD) 304*0Sstevel@tonic-gate ERROR "creating too many fields", record FATAL; 305*0Sstevel@tonic-gate cleanfld(maxfld, n); 306*0Sstevel@tonic-gate maxfld = n; 307*0Sstevel@tonic-gate setfval(nfloc, (Awkfloat) n); 308*0Sstevel@tonic-gate } 309*0Sstevel@tonic-gate 310*0Sstevel@tonic-gate refldbld(rec, fs) /* build fields from reg expr in FS */ 311*0Sstevel@tonic-gate uchar *rec, *fs; 312*0Sstevel@tonic-gate { 313*0Sstevel@tonic-gate fa *makedfa(); 314*0Sstevel@tonic-gate uchar *fr; 315*0Sstevel@tonic-gate int i, tempstat; 316*0Sstevel@tonic-gate fa *pfa; 317*0Sstevel@tonic-gate 318*0Sstevel@tonic-gate fr = fields; 319*0Sstevel@tonic-gate *fr = '\0'; 320*0Sstevel@tonic-gate if (*rec == '\0') 321*0Sstevel@tonic-gate return (0); 322*0Sstevel@tonic-gate pfa = makedfa(fs, 1); 323*0Sstevel@tonic-gate dprintf(("into refldbld, rec = <%s>, pat = <%s>\n", rec, fs)); 324*0Sstevel@tonic-gate tempstat = pfa->initstat; 325*0Sstevel@tonic-gate for (i = 1; i < MAXFLD; i++) { 326*0Sstevel@tonic-gate if (!(fldtab[i].tval & DONTFREE)) 327*0Sstevel@tonic-gate xfree(fldtab[i].sval); 328*0Sstevel@tonic-gate fldtab[i].tval = FLD | STR | DONTFREE; 329*0Sstevel@tonic-gate fldtab[i].sval = fr; 330*0Sstevel@tonic-gate dprintf(("refldbld: i=%d\n", i)); 331*0Sstevel@tonic-gate if (nematch(pfa, rec)) { 332*0Sstevel@tonic-gate pfa->initstat = 2; 333*0Sstevel@tonic-gate dprintf(("match %s (%d chars)\n", patbeg, patlen)); 334*0Sstevel@tonic-gate strncpy(fr, rec, patbeg-rec); 335*0Sstevel@tonic-gate fr += patbeg - rec + 1; 336*0Sstevel@tonic-gate *(fr-1) = '\0'; 337*0Sstevel@tonic-gate rec = patbeg + patlen; 338*0Sstevel@tonic-gate } else { 339*0Sstevel@tonic-gate dprintf(("no match %s\n", rec)); 340*0Sstevel@tonic-gate strcpy(fr, rec); 341*0Sstevel@tonic-gate pfa->initstat = tempstat; 342*0Sstevel@tonic-gate break; 343*0Sstevel@tonic-gate } 344*0Sstevel@tonic-gate } 345*0Sstevel@tonic-gate return (i); 346*0Sstevel@tonic-gate } 347*0Sstevel@tonic-gate 348*0Sstevel@tonic-gate recbld() 349*0Sstevel@tonic-gate { 350*0Sstevel@tonic-gate int i; 351*0Sstevel@tonic-gate register uchar *r, *p; 352*0Sstevel@tonic-gate static uchar rec[RECSIZE]; 353*0Sstevel@tonic-gate 354*0Sstevel@tonic-gate if (donerec == 1) 355*0Sstevel@tonic-gate return; 356*0Sstevel@tonic-gate r = rec; 357*0Sstevel@tonic-gate for (i = 1; i <= *NF; i++) { 358*0Sstevel@tonic-gate p = getsval(&fldtab[i]); 359*0Sstevel@tonic-gate while ((r < rec + RECSIZE) && (*r = *p++)) 360*0Sstevel@tonic-gate r++; 361*0Sstevel@tonic-gate if (i < *NF) 362*0Sstevel@tonic-gate for (p = *OFS; (r < rec + RECSIZE) && (*r = *p++); ) 363*0Sstevel@tonic-gate r++; 364*0Sstevel@tonic-gate } 365*0Sstevel@tonic-gate if (r >= rec + RECSIZE) 366*0Sstevel@tonic-gate ERROR "built giant record `%.20s...'", record FATAL; 367*0Sstevel@tonic-gate *r = '\0'; 368*0Sstevel@tonic-gate dprintf(("in recbld FS=%o, recloc=%o\n", **FS, recloc)); 369*0Sstevel@tonic-gate recloc->tval = REC | STR | DONTFREE; 370*0Sstevel@tonic-gate recloc->sval = record = rec; 371*0Sstevel@tonic-gate dprintf(("in recbld FS=%o, recloc=%o\n", **FS, recloc)); 372*0Sstevel@tonic-gate dprintf(("recbld = |%s|\n", record)); 373*0Sstevel@tonic-gate donerec = 1; 374*0Sstevel@tonic-gate } 375*0Sstevel@tonic-gate 376*0Sstevel@tonic-gate Cell * 377*0Sstevel@tonic-gate fieldadr(n) 378*0Sstevel@tonic-gate { 379*0Sstevel@tonic-gate if (n < 0 || n >= MAXFLD) 380*0Sstevel@tonic-gate ERROR "trying to access field %d", n FATAL; 381*0Sstevel@tonic-gate return (&fldtab[n]); 382*0Sstevel@tonic-gate } 383*0Sstevel@tonic-gate 384*0Sstevel@tonic-gate int errorflag = 0; 385*0Sstevel@tonic-gate char errbuf[200]; 386*0Sstevel@tonic-gate 387*0Sstevel@tonic-gate yyerror(s) 388*0Sstevel@tonic-gate uchar *s; 389*0Sstevel@tonic-gate { 390*0Sstevel@tonic-gate extern uchar *cmdname, *curfname; 391*0Sstevel@tonic-gate static int been_here = 0; 392*0Sstevel@tonic-gate 393*0Sstevel@tonic-gate if (been_here++ > 2) 394*0Sstevel@tonic-gate return; 395*0Sstevel@tonic-gate fprintf(stderr, "%s: %s", cmdname, s); 396*0Sstevel@tonic-gate fprintf(stderr, gettext(" at source line %lld"), lineno); 397*0Sstevel@tonic-gate if (curfname != NULL) 398*0Sstevel@tonic-gate fprintf(stderr, gettext(" in function %s"), curfname); 399*0Sstevel@tonic-gate fprintf(stderr, "\n"); 400*0Sstevel@tonic-gate errorflag = 2; 401*0Sstevel@tonic-gate eprint(); 402*0Sstevel@tonic-gate } 403*0Sstevel@tonic-gate 404*0Sstevel@tonic-gate fpecatch() 405*0Sstevel@tonic-gate { 406*0Sstevel@tonic-gate ERROR "floating point exception" FATAL; 407*0Sstevel@tonic-gate } 408*0Sstevel@tonic-gate 409*0Sstevel@tonic-gate extern int bracecnt, brackcnt, parencnt; 410*0Sstevel@tonic-gate 411*0Sstevel@tonic-gate bracecheck() 412*0Sstevel@tonic-gate { 413*0Sstevel@tonic-gate int c; 414*0Sstevel@tonic-gate static int beenhere = 0; 415*0Sstevel@tonic-gate 416*0Sstevel@tonic-gate if (beenhere++) 417*0Sstevel@tonic-gate return; 418*0Sstevel@tonic-gate while ((c = input()) != EOF && c != '\0') 419*0Sstevel@tonic-gate bclass(c); 420*0Sstevel@tonic-gate bcheck2(bracecnt, '{', '}'); 421*0Sstevel@tonic-gate bcheck2(brackcnt, '[', ']'); 422*0Sstevel@tonic-gate bcheck2(parencnt, '(', ')'); 423*0Sstevel@tonic-gate } 424*0Sstevel@tonic-gate 425*0Sstevel@tonic-gate bcheck2(n, c1, c2) 426*0Sstevel@tonic-gate { 427*0Sstevel@tonic-gate if (n == 1) 428*0Sstevel@tonic-gate fprintf(stderr, gettext("\tmissing %c\n"), c2); 429*0Sstevel@tonic-gate else if (n > 1) 430*0Sstevel@tonic-gate fprintf(stderr, gettext("\t%d missing %c's\n"), n, c2); 431*0Sstevel@tonic-gate else if (n == -1) 432*0Sstevel@tonic-gate fprintf(stderr, gettext("\textra %c\n"), c2); 433*0Sstevel@tonic-gate else if (n < -1) 434*0Sstevel@tonic-gate fprintf(stderr, gettext("\t%d extra %c's\n"), -n, c2); 435*0Sstevel@tonic-gate } 436*0Sstevel@tonic-gate 437*0Sstevel@tonic-gate error(f, s) 438*0Sstevel@tonic-gate int f; 439*0Sstevel@tonic-gate char *s; 440*0Sstevel@tonic-gate { 441*0Sstevel@tonic-gate extern Node *curnode; 442*0Sstevel@tonic-gate extern uchar *cmdname; 443*0Sstevel@tonic-gate 444*0Sstevel@tonic-gate fflush(stdout); 445*0Sstevel@tonic-gate fprintf(stderr, "%s: ", cmdname); 446*0Sstevel@tonic-gate fprintf(stderr, "%s", s); 447*0Sstevel@tonic-gate fprintf(stderr, "\n"); 448*0Sstevel@tonic-gate if (compile_time != 2 && NR && *NR > 0) { 449*0Sstevel@tonic-gate fprintf(stderr, gettext(" input record number %g"), *FNR); 450*0Sstevel@tonic-gate if (strcmp(*FILENAME, "-") != 0) 451*0Sstevel@tonic-gate fprintf(stderr, gettext(", file %s"), *FILENAME); 452*0Sstevel@tonic-gate fprintf(stderr, "\n"); 453*0Sstevel@tonic-gate } 454*0Sstevel@tonic-gate if (compile_time != 2 && curnode) 455*0Sstevel@tonic-gate fprintf(stderr, gettext(" source line number %lld\n"), 456*0Sstevel@tonic-gate curnode->lineno); 457*0Sstevel@tonic-gate else if (compile_time != 2 && lineno) 458*0Sstevel@tonic-gate fprintf(stderr, gettext(" source line number %lld\n"), lineno); 459*0Sstevel@tonic-gate eprint(); 460*0Sstevel@tonic-gate if (f) { 461*0Sstevel@tonic-gate if (dbg) 462*0Sstevel@tonic-gate abort(); 463*0Sstevel@tonic-gate exit(2); 464*0Sstevel@tonic-gate } 465*0Sstevel@tonic-gate } 466*0Sstevel@tonic-gate 467*0Sstevel@tonic-gate eprint() /* try to print context around error */ 468*0Sstevel@tonic-gate { 469*0Sstevel@tonic-gate uchar *p, *q; 470*0Sstevel@tonic-gate int c; 471*0Sstevel@tonic-gate static int been_here = 0; 472*0Sstevel@tonic-gate extern uchar ebuf[300], *ep; 473*0Sstevel@tonic-gate 474*0Sstevel@tonic-gate if (compile_time == 2 || compile_time == 0 || been_here++ > 0) 475*0Sstevel@tonic-gate return; 476*0Sstevel@tonic-gate p = ep - 1; 477*0Sstevel@tonic-gate if (p > ebuf && *p == '\n') 478*0Sstevel@tonic-gate p--; 479*0Sstevel@tonic-gate for (; p > ebuf && *p != '\n' && *p != '\0'; p--) 480*0Sstevel@tonic-gate ; 481*0Sstevel@tonic-gate while (*p == '\n') 482*0Sstevel@tonic-gate p++; 483*0Sstevel@tonic-gate fprintf(stderr, gettext(" context is\n\t")); 484*0Sstevel@tonic-gate for (q = ep-1; q >= p && *q != ' ' && *q != '\t' && *q != '\n'; 485*0Sstevel@tonic-gate q--) 486*0Sstevel@tonic-gate ; 487*0Sstevel@tonic-gate for (; p < q; p++) 488*0Sstevel@tonic-gate if (*p) 489*0Sstevel@tonic-gate putc(*p, stderr); 490*0Sstevel@tonic-gate fprintf(stderr, " >>> "); 491*0Sstevel@tonic-gate for (; p < ep; p++) 492*0Sstevel@tonic-gate if (*p) 493*0Sstevel@tonic-gate putc(*p, stderr); 494*0Sstevel@tonic-gate fprintf(stderr, " <<< "); 495*0Sstevel@tonic-gate if (*ep) 496*0Sstevel@tonic-gate while ((c = input()) != '\n' && c != '\0' && c != EOF) { 497*0Sstevel@tonic-gate putc(c, stderr); 498*0Sstevel@tonic-gate bclass(c); 499*0Sstevel@tonic-gate } 500*0Sstevel@tonic-gate putc('\n', stderr); 501*0Sstevel@tonic-gate ep = ebuf; 502*0Sstevel@tonic-gate } 503*0Sstevel@tonic-gate 504*0Sstevel@tonic-gate bclass(c) 505*0Sstevel@tonic-gate { 506*0Sstevel@tonic-gate switch (c) { 507*0Sstevel@tonic-gate case '{': bracecnt++; break; 508*0Sstevel@tonic-gate case '}': bracecnt--; break; 509*0Sstevel@tonic-gate case '[': brackcnt++; break; 510*0Sstevel@tonic-gate case ']': brackcnt--; break; 511*0Sstevel@tonic-gate case '(': parencnt++; break; 512*0Sstevel@tonic-gate case ')': parencnt--; break; 513*0Sstevel@tonic-gate } 514*0Sstevel@tonic-gate } 515*0Sstevel@tonic-gate 516*0Sstevel@tonic-gate double 517*0Sstevel@tonic-gate errcheck(x, s) 518*0Sstevel@tonic-gate double x; 519*0Sstevel@tonic-gate uchar *s; 520*0Sstevel@tonic-gate { 521*0Sstevel@tonic-gate extern int errno; 522*0Sstevel@tonic-gate 523*0Sstevel@tonic-gate if (errno == EDOM) { 524*0Sstevel@tonic-gate errno = 0; 525*0Sstevel@tonic-gate ERROR "%s argument out of domain", s WARNING; 526*0Sstevel@tonic-gate x = 1; 527*0Sstevel@tonic-gate } else if (errno == ERANGE) { 528*0Sstevel@tonic-gate errno = 0; 529*0Sstevel@tonic-gate ERROR "%s result out of range", s WARNING; 530*0Sstevel@tonic-gate x = 1; 531*0Sstevel@tonic-gate } 532*0Sstevel@tonic-gate return (x); 533*0Sstevel@tonic-gate } 534*0Sstevel@tonic-gate 535*0Sstevel@tonic-gate PUTS(s) uchar *s; { 536*0Sstevel@tonic-gate dprintf(("%s\n", s)); 537*0Sstevel@tonic-gate } 538*0Sstevel@tonic-gate 539*0Sstevel@tonic-gate isclvar(s) /* is s of form var=something? */ 540*0Sstevel@tonic-gate char *s; 541*0Sstevel@tonic-gate { 542*0Sstevel@tonic-gate char *os = s; 543*0Sstevel@tonic-gate 544*0Sstevel@tonic-gate for (; *s; s++) 545*0Sstevel@tonic-gate if (!(isalnum(*s) || *s == '_')) 546*0Sstevel@tonic-gate break; 547*0Sstevel@tonic-gate return (*s == '=' && s > os && *(s+1) != '='); 548*0Sstevel@tonic-gate } 549*0Sstevel@tonic-gate 550*0Sstevel@tonic-gate #define MAXEXPON 38 /* maximum exponent for fp number */ 551*0Sstevel@tonic-gate 552*0Sstevel@tonic-gate isnumber(s) 553*0Sstevel@tonic-gate register uchar *s; 554*0Sstevel@tonic-gate { 555*0Sstevel@tonic-gate register int d1, d2; 556*0Sstevel@tonic-gate int point; 557*0Sstevel@tonic-gate uchar *es; 558*0Sstevel@tonic-gate extern char radixpoint; 559*0Sstevel@tonic-gate 560*0Sstevel@tonic-gate d1 = d2 = point = 0; 561*0Sstevel@tonic-gate while (*s == ' ' || *s == '\t' || *s == '\n') 562*0Sstevel@tonic-gate s++; 563*0Sstevel@tonic-gate if (*s == '\0') 564*0Sstevel@tonic-gate return (0); /* empty stuff isn't number */ 565*0Sstevel@tonic-gate if (*s == '+' || *s == '-') 566*0Sstevel@tonic-gate s++; 567*0Sstevel@tonic-gate if (!isdigit(*s) && *s != radixpoint) 568*0Sstevel@tonic-gate return (0); 569*0Sstevel@tonic-gate if (isdigit(*s)) { 570*0Sstevel@tonic-gate do { 571*0Sstevel@tonic-gate d1++; 572*0Sstevel@tonic-gate s++; 573*0Sstevel@tonic-gate } while (isdigit(*s)); 574*0Sstevel@tonic-gate } 575*0Sstevel@tonic-gate if (d1 >= MAXEXPON) 576*0Sstevel@tonic-gate return (0); /* too many digits to convert */ 577*0Sstevel@tonic-gate if (*s == radixpoint) { 578*0Sstevel@tonic-gate point++; 579*0Sstevel@tonic-gate s++; 580*0Sstevel@tonic-gate } 581*0Sstevel@tonic-gate if (isdigit(*s)) { 582*0Sstevel@tonic-gate d2++; 583*0Sstevel@tonic-gate do { 584*0Sstevel@tonic-gate s++; 585*0Sstevel@tonic-gate } while (isdigit(*s)); 586*0Sstevel@tonic-gate } 587*0Sstevel@tonic-gate if (!(d1 || point && d2)) 588*0Sstevel@tonic-gate return (0); 589*0Sstevel@tonic-gate if (*s == 'e' || *s == 'E') { 590*0Sstevel@tonic-gate s++; 591*0Sstevel@tonic-gate if (*s == '+' || *s == '-') 592*0Sstevel@tonic-gate s++; 593*0Sstevel@tonic-gate if (!isdigit(*s)) 594*0Sstevel@tonic-gate return (0); 595*0Sstevel@tonic-gate es = s; 596*0Sstevel@tonic-gate do { 597*0Sstevel@tonic-gate s++; 598*0Sstevel@tonic-gate } while (isdigit(*s)); 599*0Sstevel@tonic-gate if (s - es > 2) 600*0Sstevel@tonic-gate return (0); 601*0Sstevel@tonic-gate else if (s - es == 2 && 602*0Sstevel@tonic-gate (int)(10 * (*es-'0') + *(es+1)-'0') >= MAXEXPON) 603*0Sstevel@tonic-gate return (0); 604*0Sstevel@tonic-gate } 605*0Sstevel@tonic-gate while (*s == ' ' || *s == '\t' || *s == '\n') 606*0Sstevel@tonic-gate s++; 607*0Sstevel@tonic-gate if (*s == '\0') 608*0Sstevel@tonic-gate return (1); 609*0Sstevel@tonic-gate else 610*0Sstevel@tonic-gate return (0); 611*0Sstevel@tonic-gate } 612