Annotation of src/usr.bin/awk/lib.c, Revision 1.26
1.26 ! millert 1: /* $OpenBSD: lib.c,v 1.25 2017/12/08 17:04:15 deraadt Exp $ */
1.1 tholo 2: /****************************************************************
1.4 kstailey 3: Copyright (C) Lucent Technologies 1997
1.1 tholo 4: All Rights Reserved
5:
6: Permission to use, copy, modify, and distribute this software and
7: its documentation for any purpose and without fee is hereby
8: granted, provided that the above copyright notice appear in all
9: copies and that both that the copyright notice and this
10: permission notice and warranty disclaimer appear in supporting
1.4 kstailey 11: documentation, and that the name Lucent Technologies or any of
12: its entities not be used in advertising or publicity pertaining
13: to distribution of the software without specific, written prior
14: permission.
15:
16: LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17: INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
18: IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
19: SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20: WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
21: IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
22: ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
23: THIS SOFTWARE.
1.1 tholo 24: ****************************************************************/
25:
26: #define DEBUG
27: #include <stdio.h>
28: #include <string.h>
29: #include <ctype.h>
30: #include <errno.h>
31: #include <stdlib.h>
1.9 deraadt 32: #include <unistd.h>
1.7 millert 33: #include <stdarg.h>
1.1 tholo 34: #include "awk.h"
1.4 kstailey 35: #include "ytab.h"
1.1 tholo 36:
37: FILE *infile = NULL;
38: char *file = "";
1.4 kstailey 39: char *record;
1.1 tholo 40: int recsize = RECSIZE;
41: char *fields;
1.4 kstailey 42: int fieldssize = RECSIZE;
43:
44: Cell **fldtab; /* pointers to Cells */
1.5 millert 45: char inputFS[100] = " ";
1.1 tholo 46:
1.18 millert 47: #define MAXFLD 2
1.4 kstailey 48: int nfields = MAXFLD; /* last allocated slot for $i */
1.1 tholo 49:
50: int donefld; /* 1 = implies rec broken into fields */
51: int donerec; /* 1 = record is valid (no flds have changed) */
52:
1.4 kstailey 53: int lastfld = 0; /* last used field */
1.1 tholo 54: int argno = 1; /* current input argument number */
55: extern Awkfloat *ARGC;
56:
1.4 kstailey 57: static Cell dollar0 = { OCELL, CFLD, NULL, "", 0.0, REC|STR|DONTFREE };
58: static Cell dollar1 = { OCELL, CFLD, NULL, "", 0.0, FLD|STR|DONTFREE };
59:
1.1 tholo 60: void recinit(unsigned int n)
61: {
1.18 millert 62: if ( (record = (char *) malloc(n)) == NULL
63: || (fields = (char *) malloc(n+1)) == NULL
64: || (fldtab = (Cell **) calloc(nfields+1, sizeof(Cell *))) == NULL
65: || (fldtab[0] = (Cell *) malloc(sizeof(Cell))) == NULL )
1.7 millert 66: FATAL("out of space for $0 and fields");
1.22 millert 67: *record = '\0';
1.4 kstailey 68: *fldtab[0] = dollar0;
69: fldtab[0]->sval = record;
70: fldtab[0]->nval = tostring("0");
71: makefields(1, nfields);
72: }
73:
74: void makefields(int n1, int n2) /* create $n1..$n2 inclusive */
75: {
76: char temp[50];
1.1 tholo 77: int i;
78:
1.4 kstailey 79: for (i = n1; i <= n2; i++) {
80: fldtab[i] = (Cell *) malloc(sizeof (struct Cell));
81: if (fldtab[i] == NULL)
1.7 millert 82: FATAL("out of space in makefields %d", i);
1.4 kstailey 83: *fldtab[i] = dollar1;
1.12 deraadt 84: snprintf(temp, sizeof temp, "%d", i);
1.4 kstailey 85: fldtab[i]->nval = tostring(temp);
86: }
1.1 tholo 87: }
88:
89: void initgetrec(void)
90: {
91: int i;
92: char *p;
93:
94: for (i = 1; i < *ARGC; i++) {
1.20 millert 95: p = getargv(i); /* find 1st real filename */
96: if (p == NULL || *p == '\0') { /* deleted or zapped */
97: argno++;
98: continue;
99: }
100: if (!isclvar(p)) {
101: setsval(lookup("FILENAME", symtab), p);
1.1 tholo 102: return;
103: }
104: setclvar(p); /* a commandline assignment before filename */
105: argno++;
106: }
107: infile = stdin; /* no filenames, so use stdin */
108: }
109:
1.15 millert 110: static int firsttime = 1;
111:
1.4 kstailey 112: int getrec(char **pbuf, int *pbufsize, int isrecord) /* get next input record */
113: { /* note: cares whether buf == record */
1.1 tholo 114: int c;
1.4 kstailey 115: char *buf = *pbuf;
1.18 millert 116: uschar saveb0;
117: int bufsize = *pbufsize, savebufsize = bufsize;
1.1 tholo 118:
119: if (firsttime) {
120: firsttime = 0;
121: initgetrec();
122: }
1.24 deraadt 123: DPRINTF( ("RS=<%s>, FS=<%s>, ARGC=%g, FILENAME=%s\n",
1.1 tholo 124: *RS, *FS, *ARGC, *FILENAME) );
1.5 millert 125: if (isrecord) {
126: donefld = 0;
127: donerec = 1;
128: }
1.18 millert 129: saveb0 = buf[0];
1.1 tholo 130: buf[0] = 0;
131: while (argno < *ARGC || infile == stdin) {
1.24 deraadt 132: DPRINTF( ("argno=%d, file=|%s|\n", argno, file) );
1.1 tholo 133: if (infile == NULL) { /* have to open a new file */
134: file = getargv(argno);
1.20 millert 135: if (file == NULL || *file == '\0') { /* deleted or zapped */
1.1 tholo 136: argno++;
137: continue;
138: }
139: if (isclvar(file)) { /* a var=value arg */
140: setclvar(file);
141: argno++;
142: continue;
143: }
144: *FILENAME = file;
1.24 deraadt 145: DPRINTF( ("opening file %s\n", file) );
1.1 tholo 146: if (*file == '-' && *(file+1) == '\0')
147: infile = stdin;
1.4 kstailey 148: else if ((infile = fopen(file, "r")) == NULL)
1.7 millert 149: FATAL("can't open file %s", file);
1.1 tholo 150: setfval(fnrloc, 0.0);
151: }
1.4 kstailey 152: c = readrec(&buf, &bufsize, infile);
1.1 tholo 153: if (c != 0 || buf[0] != '\0') { /* normal record */
1.4 kstailey 154: if (isrecord) {
155: if (freeable(fldtab[0]))
156: xfree(fldtab[0]->sval);
157: fldtab[0]->sval = buf; /* buf == record */
158: fldtab[0]->tval = REC | STR | DONTFREE;
1.5 millert 159: if (is_number(fldtab[0]->sval)) {
1.4 kstailey 160: fldtab[0]->fval = atof(fldtab[0]->sval);
161: fldtab[0]->tval |= NUM;
1.1 tholo 162: }
163: }
164: setfval(nrloc, nrloc->fval+1);
165: setfval(fnrloc, fnrloc->fval+1);
1.4 kstailey 166: *pbuf = buf;
167: *pbufsize = bufsize;
1.1 tholo 168: return 1;
169: }
170: /* EOF arrived on this file; set up next */
171: if (infile != stdin)
172: fclose(infile);
173: infile = NULL;
174: argno++;
175: }
1.18 millert 176: buf[0] = saveb0;
1.4 kstailey 177: *pbuf = buf;
1.18 millert 178: *pbufsize = savebufsize;
1.1 tholo 179: return 0; /* true end of file */
180: }
181:
182: void nextfile(void)
183: {
1.18 millert 184: if (infile != NULL && infile != stdin)
1.1 tholo 185: fclose(infile);
186: infile = NULL;
187: argno++;
188: }
189:
1.4 kstailey 190: int readrec(char **pbuf, int *pbufsize, FILE *inf) /* read one record into buf */
1.1 tholo 191: {
192: int sep, c;
1.4 kstailey 193: char *rr, *buf = *pbuf;
194: int bufsize = *pbufsize;
1.1 tholo 195:
1.4 kstailey 196: if (strlen(*FS) >= sizeof(inputFS))
1.7 millert 197: FATAL("field separator %.10s... is too long", *FS);
1.20 millert 198: /*fflush(stdout); avoids some buffering problem but makes it 25% slower*/
1.12 deraadt 199: strlcpy(inputFS, *FS, sizeof inputFS); /* for subsequent field splitting */
1.1 tholo 200: if ((sep = **RS) == 0) {
201: sep = '\n';
202: while ((c=getc(inf)) == '\n' && c != EOF) /* skip leading \n's */
203: ;
204: if (c != EOF)
205: ungetc(c, inf);
206: }
1.4 kstailey 207: for (rr = buf; ; ) {
208: for (; (c=getc(inf)) != sep && c != EOF; ) {
209: if (rr-buf+1 > bufsize)
210: if (!adjbuf(&buf, &bufsize, 1+rr-buf, recsize, &rr, "readrec 1"))
1.7 millert 211: FATAL("input record `%.30s...' too long", buf);
1.4 kstailey 212: *rr++ = c;
213: }
1.1 tholo 214: if (**RS == sep || c == EOF)
215: break;
216: if ((c = getc(inf)) == '\n' || c == EOF) /* 2 in a row */
217: break;
1.4 kstailey 218: if (!adjbuf(&buf, &bufsize, 2+rr-buf, recsize, &rr, "readrec 2"))
1.7 millert 219: FATAL("input record `%.30s...' too long", buf);
1.1 tholo 220: *rr++ = '\n';
221: *rr++ = c;
222: }
1.4 kstailey 223: if (!adjbuf(&buf, &bufsize, 1+rr-buf, recsize, &rr, "readrec 3"))
1.7 millert 224: FATAL("input record `%.30s...' too long", buf);
1.1 tholo 225: *rr = 0;
1.24 deraadt 226: DPRINTF( ("readrec saw <%s>, returns %d\n", buf, c == EOF && rr == buf ? 0 : 1) );
1.4 kstailey 227: *pbuf = buf;
228: *pbufsize = bufsize;
1.1 tholo 229: return c == EOF && rr == buf ? 0 : 1;
230: }
231:
232: char *getargv(int n) /* get ARGV[n] */
233: {
234: Cell *x;
1.4 kstailey 235: char *s, temp[50];
1.1 tholo 236: extern Array *ARGVtab;
237:
1.12 deraadt 238: snprintf(temp, sizeof temp, "%d", n);
1.20 millert 239: if (lookup(temp, ARGVtab) == NULL)
240: return NULL;
1.1 tholo 241: x = setsymtab(temp, "", 0.0, STR, ARGVtab);
242: s = getsval(x);
1.24 deraadt 243: DPRINTF( ("getargv(%d) returns |%s|\n", n, s) );
1.1 tholo 244: return s;
245: }
246:
247: void setclvar(char *s) /* set var=value from s */
248: {
249: char *p;
250: Cell *q;
251:
252: for (p=s; *p != '='; p++)
253: ;
254: *p++ = 0;
255: p = qstring(p, '\0');
256: q = setsymtab(s, p, 0.0, STR, symtab);
257: setsval(q, p);
1.5 millert 258: if (is_number(q->sval)) {
1.1 tholo 259: q->fval = atof(q->sval);
260: q->tval |= NUM;
261: }
1.24 deraadt 262: DPRINTF( ("command line set %s to |%s|\n", s, p) );
1.1 tholo 263: }
264:
265:
266: void fldbld(void) /* create fields from current record */
267: {
1.4 kstailey 268: /* this relies on having fields[] the same length as $0 */
269: /* the fields are all stored in this one array with \0's */
1.20 millert 270: /* possibly with a final trailing \0 not associated with any field */
1.1 tholo 271: char *r, *fr, sep;
272: Cell *p;
1.4 kstailey 273: int i, j, n;
1.1 tholo 274:
275: if (donefld)
276: return;
1.4 kstailey 277: if (!isstr(fldtab[0]))
278: getsval(fldtab[0]);
279: r = fldtab[0]->sval;
280: n = strlen(r);
281: if (n > fieldssize) {
282: xfree(fields);
1.20 millert 283: if ((fields = (char *) malloc(n+2)) == NULL) /* possibly 2 final \0s */
1.7 millert 284: FATAL("out of space for fields in fldbld %d", n);
1.4 kstailey 285: fieldssize = n;
286: }
1.1 tholo 287: fr = fields;
288: i = 0; /* number of fields accumulated here */
1.19 millert 289: strlcpy(inputFS, *FS, sizeof(inputFS));
1.2 millert 290: if (strlen(inputFS) > 1) { /* it's a regular expression */
291: i = refldbld(r, inputFS);
292: } else if ((sep = *inputFS) == ' ') { /* default whitespace */
1.1 tholo 293: for (i = 0; ; ) {
294: while (*r == ' ' || *r == '\t' || *r == '\n')
295: r++;
296: if (*r == 0)
297: break;
298: i++;
1.4 kstailey 299: if (i > nfields)
300: growfldtab(i);
301: if (freeable(fldtab[i]))
302: xfree(fldtab[i]->sval);
303: fldtab[i]->sval = fr;
304: fldtab[i]->tval = FLD | STR | DONTFREE;
1.1 tholo 305: do
306: *fr++ = *r++;
307: while (*r != ' ' && *r != '\t' && *r != '\n' && *r != '\0');
308: *fr++ = 0;
309: }
310: *fr = 0;
1.2 millert 311: } else if ((sep = *inputFS) == 0) { /* new: FS="" => 1 char/field */
1.1 tholo 312: for (i = 0; *r != 0; r++) {
313: char buf[2];
314: i++;
1.4 kstailey 315: if (i > nfields)
316: growfldtab(i);
317: if (freeable(fldtab[i]))
318: xfree(fldtab[i]->sval);
1.1 tholo 319: buf[0] = *r;
320: buf[1] = 0;
1.4 kstailey 321: fldtab[i]->sval = tostring(buf);
322: fldtab[i]->tval = FLD | STR;
1.1 tholo 323: }
324: *fr = 0;
325: } else if (*r != 0) { /* if 0, it's a null field */
1.15 millert 326: /* subtlecase : if length(FS) == 1 && length(RS > 0)
327: * \n is NOT a field separator (cf awk book 61,84).
328: * this variable is tested in the inner while loop.
329: */
330: int rtest = '\n'; /* normal case */
331: if (strlen(*RS) > 0)
332: rtest = '\0';
1.1 tholo 333: for (;;) {
334: i++;
1.4 kstailey 335: if (i > nfields)
336: growfldtab(i);
337: if (freeable(fldtab[i]))
338: xfree(fldtab[i]->sval);
339: fldtab[i]->sval = fr;
340: fldtab[i]->tval = FLD | STR | DONTFREE;
1.15 millert 341: while (*r != sep && *r != rtest && *r != '\0') /* \n is always a separator */
1.1 tholo 342: *fr++ = *r++;
343: *fr++ = 0;
344: if (*r++ == 0)
345: break;
346: }
347: *fr = 0;
348: }
1.4 kstailey 349: if (i > nfields)
1.7 millert 350: FATAL("record `%.30s...' has too many fields; can't happen", r);
1.4 kstailey 351: cleanfld(i+1, lastfld); /* clean out junk from previous record */
352: lastfld = i;
1.1 tholo 353: donefld = 1;
1.4 kstailey 354: for (j = 1; j <= lastfld; j++) {
355: p = fldtab[j];
1.5 millert 356: if(is_number(p->sval)) {
1.1 tholo 357: p->fval = atof(p->sval);
358: p->tval |= NUM;
359: }
360: }
1.4 kstailey 361: setfval(nfloc, (Awkfloat) lastfld);
1.26 ! millert 362: donerec = 1; /* restore */
1.4 kstailey 363: if (dbg) {
364: for (j = 0; j <= lastfld; j++) {
365: p = fldtab[j];
366: printf("field %d (%s): |%s|\n", j, p->nval, p->sval);
367: }
368: }
1.1 tholo 369: }
370:
1.4 kstailey 371: void cleanfld(int n1, int n2) /* clean out fields n1 .. n2 inclusive */
372: { /* nvals remain intact */
373: Cell *p;
374: int i;
1.1 tholo 375:
1.4 kstailey 376: for (i = n1; i <= n2; i++) {
377: p = fldtab[i];
378: if (freeable(p))
1.1 tholo 379: xfree(p->sval);
1.4 kstailey 380: p->sval = "";
1.1 tholo 381: p->tval = FLD | STR | DONTFREE;
382: }
383: }
384:
1.4 kstailey 385: void newfld(int n) /* add field n after end of existing lastfld */
1.1 tholo 386: {
1.4 kstailey 387: if (n > nfields)
388: growfldtab(n);
389: cleanfld(lastfld+1, n);
390: lastfld = n;
1.1 tholo 391: setfval(nfloc, (Awkfloat) n);
1.26 ! millert 392: }
! 393:
! 394: void setlastfld(int n) /* set lastfld cleaning fldtab cells if necessary */
! 395: {
! 396: if (n > nfields)
! 397: growfldtab(n);
! 398:
! 399: if (lastfld < n)
! 400: cleanfld(lastfld+1, n);
! 401: else
! 402: cleanfld(n+1, lastfld);
! 403:
! 404: lastfld = n;
1.1 tholo 405: }
406:
1.4 kstailey 407: Cell *fieldadr(int n) /* get nth field */
408: {
409: if (n < 0)
1.15 millert 410: FATAL("trying to access out of range field %d", n);
1.4 kstailey 411: if (n > nfields) /* fields after NF are empty */
412: growfldtab(n); /* but does not increase NF */
413: return(fldtab[n]);
414: }
415:
416: void growfldtab(int n) /* make new fields up to at least $n */
417: {
418: int nf = 2 * nfields;
1.15 millert 419: size_t s;
1.4 kstailey 420:
421: if (n > nf)
422: nf = n;
1.15 millert 423: s = (nf+1) * (sizeof (struct Cell *)); /* freebsd: how much do we need? */
424: if (s / sizeof(struct Cell *) - 1 == nf) /* didn't overflow */
425: fldtab = (Cell **) realloc(fldtab, s);
426: else /* overflow sizeof int */
427: xfree(fldtab); /* make it null */
1.4 kstailey 428: if (fldtab == NULL)
1.7 millert 429: FATAL("out of space creating %d fields", nf);
1.4 kstailey 430: makefields(nfields+1, nf);
431: nfields = nf;
432: }
433:
1.11 millert 434: int refldbld(const char *rec, const char *fs) /* build fields from reg expr in FS */
1.1 tholo 435: {
1.4 kstailey 436: /* this relies on having fields[] the same length as $0 */
437: /* the fields are all stored in this one array with \0's */
1.1 tholo 438: char *fr;
1.4 kstailey 439: int i, tempstat, n;
1.1 tholo 440: fa *pfa;
441:
1.4 kstailey 442: n = strlen(rec);
443: if (n > fieldssize) {
444: xfree(fields);
445: if ((fields = (char *) malloc(n+1)) == NULL)
1.7 millert 446: FATAL("out of space for fields in refldbld %d", n);
1.4 kstailey 447: fieldssize = n;
448: }
1.1 tholo 449: fr = fields;
450: *fr = '\0';
451: if (*rec == '\0')
452: return 0;
453: pfa = makedfa(fs, 1);
1.24 deraadt 454: DPRINTF( ("into refldbld, rec = <%s>, pat = <%s>\n", rec, fs) );
1.1 tholo 455: tempstat = pfa->initstat;
1.4 kstailey 456: for (i = 1; ; i++) {
457: if (i > nfields)
458: growfldtab(i);
459: if (freeable(fldtab[i]))
460: xfree(fldtab[i]->sval);
461: fldtab[i]->tval = FLD | STR | DONTFREE;
462: fldtab[i]->sval = fr;
1.24 deraadt 463: DPRINTF( ("refldbld: i=%d\n", i) );
1.1 tholo 464: if (nematch(pfa, rec)) {
1.4 kstailey 465: pfa->initstat = 2; /* horrible coupling to b.c */
1.24 deraadt 466: DPRINTF( ("match %s (%d chars)\n", patbeg, patlen) );
1.1 tholo 467: strncpy(fr, rec, patbeg-rec);
468: fr += patbeg - rec + 1;
469: *(fr-1) = '\0';
470: rec = patbeg + patlen;
471: } else {
1.24 deraadt 472: DPRINTF( ("no match %s\n", rec) );
1.13 tedu 473: strlcpy(fr, rec, fields + fieldssize - fr);
1.1 tholo 474: pfa->initstat = tempstat;
475: break;
476: }
477: }
478: return i;
479: }
480:
481: void recbld(void) /* create $0 from $1..$NF if necessary */
482: {
483: int i;
484: char *r, *p;
485:
486: if (donerec == 1)
487: return;
1.5 millert 488: r = record;
1.1 tholo 489: for (i = 1; i <= *NF; i++) {
1.4 kstailey 490: p = getsval(fldtab[i]);
1.5 millert 491: if (!adjbuf(&record, &recsize, 1+strlen(p)+r-record, recsize, &r, "recbld 1"))
1.7 millert 492: FATAL("created $0 `%.30s...' too long", record);
1.4 kstailey 493: while ((*r = *p++) != 0)
1.1 tholo 494: r++;
1.4 kstailey 495: if (i < *NF) {
1.5 millert 496: if (!adjbuf(&record, &recsize, 2+strlen(*OFS)+r-record, recsize, &r, "recbld 2"))
1.7 millert 497: FATAL("created $0 `%.30s...' too long", record);
1.4 kstailey 498: for (p = *OFS; (*r = *p++) != 0; )
1.1 tholo 499: r++;
1.4 kstailey 500: }
1.1 tholo 501: }
1.5 millert 502: if (!adjbuf(&record, &recsize, 2+r-record, recsize, &r, "recbld 3"))
1.7 millert 503: FATAL("built giant record `%.30s...'", record);
1.1 tholo 504: *r = '\0';
1.24 deraadt 505: DPRINTF( ("in recbld inputFS=%s, fldtab[0]=%p\n", inputFS, (void*)fldtab[0]) );
1.4 kstailey 506:
507: if (freeable(fldtab[0]))
508: xfree(fldtab[0]->sval);
509: fldtab[0]->tval = REC | STR | DONTFREE;
510: fldtab[0]->sval = record;
511:
1.24 deraadt 512: DPRINTF( ("in recbld inputFS=%s, fldtab[0]=%p\n", inputFS, (void*)fldtab[0]) );
513: DPRINTF( ("recbld = |%s|\n", record) );
1.1 tholo 514: donerec = 1;
515: }
516:
517: int errorflag = 0;
518:
1.11 millert 519: void yyerror(const char *s)
1.1 tholo 520: {
1.14 grange 521: SYNTAX("%s", s);
1.7 millert 522: }
523:
1.11 millert 524: void SYNTAX(const char *fmt, ...)
1.7 millert 525: {
1.1 tholo 526: extern char *cmdname, *curfname;
527: static int been_here = 0;
1.7 millert 528: va_list varg;
1.1 tholo 529:
530: if (been_here++ > 2)
531: return;
1.7 millert 532: fprintf(stderr, "%s: ", cmdname);
533: va_start(varg, fmt);
534: vfprintf(stderr, fmt, varg);
535: va_end(varg);
1.1 tholo 536: fprintf(stderr, " at source line %d", lineno);
537: if (curfname != NULL)
538: fprintf(stderr, " in function %s", curfname);
1.6 millert 539: if (compile_time == 1 && cursource() != NULL)
540: fprintf(stderr, " source file %s", cursource());
1.1 tholo 541: fprintf(stderr, "\n");
542: errorflag = 2;
543: eprint();
544: }
545:
1.9 deraadt 546: void fpecatch(int sig)
1.1 tholo 547: {
1.9 deraadt 548: extern Node *curnode;
549:
1.25 deraadt 550: dprintf(STDERR_FILENO, "floating point exception\n");
1.9 deraadt 551:
552: if (compile_time != 2 && NR && *NR > 0) {
1.25 deraadt 553: dprintf(STDERR_FILENO, " input record number %d", (int) (*FNR));
1.9 deraadt 554: if (strcmp(*FILENAME, "-") != 0) {
1.25 deraadt 555: dprintf(STDERR_FILENO, ", file %s", *FILENAME);
1.9 deraadt 556: }
1.25 deraadt 557: dprintf(STDERR_FILENO, "\n");
1.9 deraadt 558: }
559: if (compile_time != 2 && curnode) {
1.25 deraadt 560: dprintf(STDERR_FILENO, " source line number %d", curnode->lineno);
1.9 deraadt 561: } else if (compile_time != 2 && lineno) {
1.25 deraadt 562: dprintf(STDERR_FILENO, " source line number %d", lineno);
1.9 deraadt 563: }
564: if (compile_time == 1 && cursource() != NULL) {
1.25 deraadt 565: dprintf(STDERR_FILENO, " source file %s", cursource());
1.9 deraadt 566: }
1.25 deraadt 567: dprintf(STDERR_FILENO, "\n");
1.9 deraadt 568: if (dbg > 1) /* core dump if serious debugging on */
569: abort();
570: _exit(1);
1.1 tholo 571: }
572:
573: extern int bracecnt, brackcnt, parencnt;
574:
575: void bracecheck(void)
576: {
577: int c;
578: static int beenhere = 0;
579:
580: if (beenhere++)
581: return;
1.3 millert 582: while ((c = input()) != EOF && c != '\0')
1.1 tholo 583: bclass(c);
584: bcheck2(bracecnt, '{', '}');
585: bcheck2(brackcnt, '[', ']');
586: bcheck2(parencnt, '(', ')');
587: }
588:
589: void bcheck2(int n, int c1, int c2)
590: {
591: if (n == 1)
592: fprintf(stderr, "\tmissing %c\n", c2);
593: else if (n > 1)
594: fprintf(stderr, "\t%d missing %c's\n", n, c2);
595: else if (n == -1)
596: fprintf(stderr, "\textra %c\n", c2);
597: else if (n < -1)
598: fprintf(stderr, "\t%d extra %c's\n", -n, c2);
599: }
600:
1.23 krw 601: __dead void FATAL(const char *fmt, ...)
1.7 millert 602: {
603: extern char *cmdname;
604: va_list varg;
605:
606: fflush(stdout);
607: fprintf(stderr, "%s: ", cmdname);
608: va_start(varg, fmt);
609: vfprintf(stderr, fmt, varg);
610: va_end(varg);
611: error();
612: if (dbg > 1) /* core dump if serious debugging on */
613: abort();
614: exit(2);
615: }
616:
1.11 millert 617: void WARNING(const char *fmt, ...)
1.1 tholo 618: {
619: extern char *cmdname;
1.7 millert 620: va_list varg;
1.1 tholo 621:
622: fflush(stdout);
623: fprintf(stderr, "%s: ", cmdname);
1.7 millert 624: va_start(varg, fmt);
625: vfprintf(stderr, fmt, varg);
626: va_end(varg);
627: error();
628: }
629:
630: void error()
631: {
632: extern Node *curnode;
633:
1.1 tholo 634: fprintf(stderr, "\n");
635: if (compile_time != 2 && NR && *NR > 0) {
636: fprintf(stderr, " input record number %d", (int) (*FNR));
637: if (strcmp(*FILENAME, "-") != 0)
638: fprintf(stderr, ", file %s", *FILENAME);
639: fprintf(stderr, "\n");
640: }
641: if (compile_time != 2 && curnode)
1.6 millert 642: fprintf(stderr, " source line number %d", curnode->lineno);
1.1 tholo 643: else if (compile_time != 2 && lineno)
1.6 millert 644: fprintf(stderr, " source line number %d", lineno);
645: if (compile_time == 1 && cursource() != NULL)
646: fprintf(stderr, " source file %s", cursource());
647: fprintf(stderr, "\n");
1.1 tholo 648: eprint();
649: }
650:
651: void eprint(void) /* try to print context around error */
652: {
653: char *p, *q;
654: int c;
655: static int been_here = 0;
656: extern char ebuf[], *ep;
657:
1.21 tobiasu 658: if (compile_time == 2 || compile_time == 0 || been_here++ > 0 ||
659: ebuf == ep)
1.1 tholo 660: return;
661: p = ep - 1;
662: if (p > ebuf && *p == '\n')
663: p--;
664: for ( ; p > ebuf && *p != '\n' && *p != '\0'; p--)
665: ;
666: while (*p == '\n')
667: p++;
668: fprintf(stderr, " context is\n\t");
669: for (q=ep-1; q>=p && *q!=' ' && *q!='\t' && *q!='\n'; q--)
670: ;
671: for ( ; p < q; p++)
672: if (*p)
673: putc(*p, stderr);
674: fprintf(stderr, " >>> ");
675: for ( ; p < ep; p++)
676: if (*p)
677: putc(*p, stderr);
678: fprintf(stderr, " <<< ");
679: if (*ep)
680: while ((c = input()) != '\n' && c != '\0' && c != EOF) {
681: putc(c, stderr);
682: bclass(c);
683: }
684: putc('\n', stderr);
685: ep = ebuf;
686: }
687:
688: void bclass(int c)
689: {
690: switch (c) {
691: case '{': bracecnt++; break;
692: case '}': bracecnt--; break;
693: case '[': brackcnt++; break;
694: case ']': brackcnt--; break;
695: case '(': parencnt++; break;
696: case ')': parencnt--; break;
697: }
698: }
699:
1.11 millert 700: double errcheck(double x, const char *s)
1.1 tholo 701: {
702:
703: if (errno == EDOM) {
704: errno = 0;
1.7 millert 705: WARNING("%s argument out of domain", s);
1.1 tholo 706: x = 1;
707: } else if (errno == ERANGE) {
708: errno = 0;
1.7 millert 709: WARNING("%s result out of range", s);
1.1 tholo 710: x = 1;
711: }
712: return x;
713: }
714:
1.11 millert 715: int isclvar(const char *s) /* is s of form var=something ? */
1.1 tholo 716: {
1.11 millert 717: const char *os = s;
1.1 tholo 718:
1.8 millert 719: if (!isalpha((uschar) *s) && *s != '_')
1.1 tholo 720: return 0;
721: for ( ; *s; s++)
1.8 millert 722: if (!(isalnum((uschar) *s) || *s == '_'))
1.1 tholo 723: break;
724: return *s == '=' && s > os && *(s+1) != '=';
725: }
726:
1.4 kstailey 727: /* strtod is supposed to be a proper test of what's a valid number */
1.8 millert 728: /* appears to be broken in gcc on linux: thinks 0x123 is a valid FP number */
729: /* wrong: violates 4.10.1.4 of ansi C standard */
1.1 tholo 730:
1.4 kstailey 731: #include <math.h>
1.11 millert 732: int is_number(const char *s)
1.1 tholo 733: {
1.4 kstailey 734: double r;
735: char *ep;
736: errno = 0;
737: r = strtod(s, &ep);
738: if (ep == s || r == HUGE_VAL || errno == ERANGE)
739: return 0;
740: while (*ep == ' ' || *ep == '\t' || *ep == '\n')
741: ep++;
742: if (*ep == '\0')
743: return 1;
1.1 tholo 744: else
1.4 kstailey 745: return 0;
1.1 tholo 746: }