=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/awk/lib.c,v retrieving revision 1.6 retrieving revision 1.7 diff -c -r1.6 -r1.7 *** src/usr.bin/awk/lib.c 1999/04/20 17:31:29 1.6 --- src/usr.bin/awk/lib.c 1999/12/08 23:09:45 1.7 *************** *** 1,4 **** ! /* $OpenBSD: lib.c,v 1.6 1999/04/20 17:31:29 millert Exp $ */ /**************************************************************** Copyright (C) Lucent Technologies 1997 All Rights Reserved --- 1,4 ---- ! /* $OpenBSD: lib.c,v 1.7 1999/12/08 23:09:45 millert Exp $ */ /**************************************************************** Copyright (C) Lucent Technologies 1997 All Rights Reserved *************** *** 29,34 **** --- 29,35 ---- #include #include #include + #include #include "awk.h" #include "ytab.h" *************** *** 61,67 **** fields = (char *) malloc(n); fldtab = (Cell **) malloc((nfields+1) * sizeof(Cell *)); if (record == NULL || fields == NULL || fldtab == NULL) ! ERROR "out of space for $0 and fields" FATAL; fldtab[0] = (Cell *) malloc(sizeof (Cell)); *fldtab[0] = dollar0; --- 62,68 ---- fields = (char *) malloc(n); fldtab = (Cell **) malloc((nfields+1) * sizeof(Cell *)); if (record == NULL || fields == NULL || fldtab == NULL) ! FATAL("out of space for $0 and fields"); fldtab[0] = (Cell *) malloc(sizeof (Cell)); *fldtab[0] = dollar0; *************** *** 78,84 **** for (i = n1; i <= n2; i++) { fldtab[i] = (Cell *) malloc(sizeof (struct Cell)); if (fldtab[i] == NULL) ! ERROR "out of space in makefields %d", i FATAL; *fldtab[i] = dollar1; sprintf(temp, "%d", i); fldtab[i]->nval = tostring(temp); --- 79,85 ---- for (i = n1; i <= n2; i++) { fldtab[i] = (Cell *) malloc(sizeof (struct Cell)); if (fldtab[i] == NULL) ! FATAL("out of space in makefields %d", i); *fldtab[i] = dollar1; sprintf(temp, "%d", i); fldtab[i]->nval = tostring(temp); *************** *** 137,143 **** if (*file == '-' && *(file+1) == '\0') infile = stdin; else if ((infile = fopen(file, "r")) == NULL) ! ERROR "can't open file %s", file FATAL; setfval(fnrloc, 0.0); } c = readrec(&buf, &bufsize, infile); --- 138,144 ---- if (*file == '-' && *(file+1) == '\0') infile = stdin; else if ((infile = fopen(file, "r")) == NULL) ! FATAL("can't open file %s", file); setfval(fnrloc, 0.0); } c = readrec(&buf, &bufsize, infile); *************** *** 184,190 **** int bufsize = *pbufsize; if (strlen(*FS) >= sizeof(inputFS)) ! ERROR "field separator %.10s... is too long", *FS FATAL; strcpy(inputFS, *FS); /* for subsequent field splitting */ if ((sep = **RS) == 0) { sep = '\n'; --- 185,191 ---- int bufsize = *pbufsize; if (strlen(*FS) >= sizeof(inputFS)) ! FATAL("field separator %.10s... is too long", *FS); strcpy(inputFS, *FS); /* for subsequent field splitting */ if ((sep = **RS) == 0) { sep = '\n'; *************** *** 197,203 **** for (; (c=getc(inf)) != sep && c != EOF; ) { if (rr-buf+1 > bufsize) if (!adjbuf(&buf, &bufsize, 1+rr-buf, recsize, &rr, "readrec 1")) ! ERROR "input record `%.30s...' too long", buf FATAL; *rr++ = c; } if (**RS == sep || c == EOF) --- 198,204 ---- for (; (c=getc(inf)) != sep && c != EOF; ) { if (rr-buf+1 > bufsize) if (!adjbuf(&buf, &bufsize, 1+rr-buf, recsize, &rr, "readrec 1")) ! FATAL("input record `%.30s...' too long", buf); *rr++ = c; } if (**RS == sep || c == EOF) *************** *** 205,216 **** if ((c = getc(inf)) == '\n' || c == EOF) /* 2 in a row */ break; if (!adjbuf(&buf, &bufsize, 2+rr-buf, recsize, &rr, "readrec 2")) ! ERROR "input record `%.30s...' too long", buf FATAL; *rr++ = '\n'; *rr++ = c; } if (!adjbuf(&buf, &bufsize, 1+rr-buf, recsize, &rr, "readrec 3")) ! ERROR "input record `%.30s...' too long", buf FATAL; *rr = 0; dprintf( ("readrec saw <%s>, returns %d\n", buf, c == EOF && rr == buf ? 0 : 1) ); *pbuf = buf; --- 206,217 ---- if ((c = getc(inf)) == '\n' || c == EOF) /* 2 in a row */ break; if (!adjbuf(&buf, &bufsize, 2+rr-buf, recsize, &rr, "readrec 2")) ! FATAL("input record `%.30s...' too long", buf); *rr++ = '\n'; *rr++ = c; } if (!adjbuf(&buf, &bufsize, 1+rr-buf, recsize, &rr, "readrec 3")) ! FATAL("input record `%.30s...' too long", buf); *rr = 0; dprintf( ("readrec saw <%s>, returns %d\n", buf, c == EOF && rr == buf ? 0 : 1) ); *pbuf = buf; *************** *** 267,273 **** if (n > fieldssize) { xfree(fields); if ((fields = (char *) malloc(n+1)) == NULL) ! ERROR "out of space for fields in fldbld %d", n FATAL; fieldssize = n; } fr = fields; --- 268,274 ---- if (n > fieldssize) { xfree(fields); if ((fields = (char *) malloc(n+1)) == NULL) ! FATAL("out of space for fields in fldbld %d", n); fieldssize = n; } fr = fields; *************** *** 325,331 **** *fr = 0; } if (i > nfields) ! ERROR "record `%.30s...' has too many fields; can't happen", r FATAL; cleanfld(i+1, lastfld); /* clean out junk from previous record */ lastfld = i; donefld = 1; --- 326,332 ---- *fr = 0; } if (i > nfields) ! FATAL("record `%.30s...' has too many fields; can't happen", r); cleanfld(i+1, lastfld); /* clean out junk from previous record */ lastfld = i; donefld = 1; *************** *** 371,377 **** Cell *fieldadr(int n) /* get nth field */ { if (n < 0) ! ERROR "trying to access field %d", n FATAL; if (n > nfields) /* fields after NF are empty */ growfldtab(n); /* but does not increase NF */ return(fldtab[n]); --- 372,378 ---- Cell *fieldadr(int n) /* get nth field */ { if (n < 0) ! FATAL("trying to access field %d", n); if (n > nfields) /* fields after NF are empty */ growfldtab(n); /* but does not increase NF */ return(fldtab[n]); *************** *** 385,391 **** nf = n; fldtab = (Cell **) realloc(fldtab, (nf+1) * (sizeof (struct Cell *))); if (fldtab == NULL) ! ERROR "out of space creating %d fields", nf FATAL; makefields(nfields+1, nf); nfields = nf; } --- 386,392 ---- nf = n; fldtab = (Cell **) realloc(fldtab, (nf+1) * (sizeof (struct Cell *))); if (fldtab == NULL) ! FATAL("out of space creating %d fields", nf); makefields(nfields+1, nf); nfields = nf; } *************** *** 402,408 **** if (n > fieldssize) { xfree(fields); if ((fields = (char *) malloc(n+1)) == NULL) ! ERROR "out of space for fields in refldbld %d", n FATAL; fieldssize = n; } fr = fields; --- 403,409 ---- if (n > fieldssize) { xfree(fields); if ((fields = (char *) malloc(n+1)) == NULL) ! FATAL("out of space for fields in refldbld %d", n); fieldssize = n; } fr = fields; *************** *** 448,465 **** for (i = 1; i <= *NF; i++) { p = getsval(fldtab[i]); if (!adjbuf(&record, &recsize, 1+strlen(p)+r-record, recsize, &r, "recbld 1")) ! ERROR "created $0 `%.30s...' too long", record FATAL; while ((*r = *p++) != 0) r++; if (i < *NF) { if (!adjbuf(&record, &recsize, 2+strlen(*OFS)+r-record, recsize, &r, "recbld 2")) ! ERROR "created $0 `%.30s...' too long", record FATAL; for (p = *OFS; (*r = *p++) != 0; ) r++; } } if (!adjbuf(&record, &recsize, 2+r-record, recsize, &r, "recbld 3")) ! ERROR "built giant record `%.30s...'", record FATAL; *r = '\0'; dprintf( ("in recbld inputFS=%s, fldtab[0]=%p\n", inputFS, fldtab[0]) ); --- 449,466 ---- for (i = 1; i <= *NF; i++) { p = getsval(fldtab[i]); if (!adjbuf(&record, &recsize, 1+strlen(p)+r-record, recsize, &r, "recbld 1")) ! FATAL("created $0 `%.30s...' too long", record); while ((*r = *p++) != 0) r++; if (i < *NF) { if (!adjbuf(&record, &recsize, 2+strlen(*OFS)+r-record, recsize, &r, "recbld 2")) ! FATAL("created $0 `%.30s...' too long", record); for (p = *OFS; (*r = *p++) != 0; ) r++; } } if (!adjbuf(&record, &recsize, 2+r-record, recsize, &r, "recbld 3")) ! FATAL("built giant record `%.30s...'", record); *r = '\0'; dprintf( ("in recbld inputFS=%s, fldtab[0]=%p\n", inputFS, fldtab[0]) ); *************** *** 474,489 **** } int errorflag = 0; - char errbuf[300]; /* used by ERROR macro */ void yyerror(char *s) { extern char *cmdname, *curfname; static int been_here = 0; if (been_here++ > 2) return; ! fprintf(stderr, "%s: %s", cmdname, s); fprintf(stderr, " at source line %d", lineno); if (curfname != NULL) fprintf(stderr, " in function %s", curfname); --- 475,498 ---- } int errorflag = 0; void yyerror(char *s) { + SYNTAX(s); + } + + void SYNTAX(char *fmt, ...) + { extern char *cmdname, *curfname; static int been_here = 0; + va_list varg; if (been_here++ > 2) return; ! fprintf(stderr, "%s: ", cmdname); ! va_start(varg, fmt); ! vfprintf(stderr, fmt, varg); ! va_end(varg); fprintf(stderr, " at source line %d", lineno); if (curfname != NULL) fprintf(stderr, " in function %s", curfname); *************** *** 496,502 **** void fpecatch(int n) { ! ERROR "floating point exception %d", n FATAL; } extern int bracecnt, brackcnt, parencnt; --- 505,511 ---- void fpecatch(int n) { ! FATAL("floating point exception %d", n); } extern int bracecnt, brackcnt, parencnt; *************** *** 527,540 **** fprintf(stderr, "\t%d extra %c's\n", -n, c2); } ! void error(int f, char *s) { - extern Node *curnode; extern char *cmdname; fflush(stdout); fprintf(stderr, "%s: ", cmdname); ! fprintf(stderr, "%s", s); fprintf(stderr, "\n"); if (compile_time != 2 && NR && *NR > 0) { fprintf(stderr, " input record number %d", (int) (*FNR)); --- 536,574 ---- fprintf(stderr, "\t%d extra %c's\n", -n, c2); } ! void FATAL(char *fmt, ...) { extern char *cmdname; + va_list varg; fflush(stdout); fprintf(stderr, "%s: ", cmdname); ! va_start(varg, fmt); ! vfprintf(stderr, fmt, varg); ! va_end(varg); ! error(); ! if (dbg > 1) /* core dump if serious debugging on */ ! abort(); ! exit(2); ! } ! ! void WARNING(char *fmt, ...) ! { ! extern char *cmdname; ! va_list varg; ! ! fflush(stdout); ! fprintf(stderr, "%s: ", cmdname); ! va_start(varg, fmt); ! vfprintf(stderr, fmt, varg); ! va_end(varg); ! error(); ! } ! ! void error() ! { ! extern Node *curnode; ! fprintf(stderr, "\n"); if (compile_time != 2 && NR && *NR > 0) { fprintf(stderr, " input record number %d", (int) (*FNR)); *************** *** 550,560 **** fprintf(stderr, " source file %s", cursource()); fprintf(stderr, "\n"); eprint(); - if (f) { - if (dbg > 1) /* core dump if serious debugging on */ - abort(); - exit(2); - } } void eprint(void) /* try to print context around error */ --- 584,589 ---- *************** *** 610,620 **** if (errno == EDOM) { errno = 0; ! ERROR "%s argument out of domain", s WARNING; x = 1; } else if (errno == ERANGE) { errno = 0; ! ERROR "%s result out of range", s WARNING; x = 1; } return x; --- 639,649 ---- if (errno == EDOM) { errno = 0; ! WARNING("%s argument out of domain", s); x = 1; } else if (errno == ERANGE) { errno = 0; ! WARNING("%s result out of range", s); x = 1; } return x;