version 1.6, 1999/04/20 17:31:29 |
version 1.7, 1999/12/08 23:09:45 |
|
|
#include <ctype.h> |
#include <ctype.h> |
#include <errno.h> |
#include <errno.h> |
#include <stdlib.h> |
#include <stdlib.h> |
|
#include <stdarg.h> |
#include "awk.h" |
#include "awk.h" |
#include "ytab.h" |
#include "ytab.h" |
|
|
|
|
fields = (char *) malloc(n); |
fields = (char *) malloc(n); |
fldtab = (Cell **) malloc((nfields+1) * sizeof(Cell *)); |
fldtab = (Cell **) malloc((nfields+1) * sizeof(Cell *)); |
if (record == NULL || fields == NULL || fldtab == NULL) |
if (record == NULL || fields == NULL || fldtab == NULL) |
ERROR "out of space for $0 and fields" FATAL; |
FATAL("out of space for $0 and fields"); |
|
|
fldtab[0] = (Cell *) malloc(sizeof (Cell)); |
fldtab[0] = (Cell *) malloc(sizeof (Cell)); |
*fldtab[0] = dollar0; |
*fldtab[0] = dollar0; |
|
|
for (i = n1; i <= n2; i++) { |
for (i = n1; i <= n2; i++) { |
fldtab[i] = (Cell *) malloc(sizeof (struct Cell)); |
fldtab[i] = (Cell *) malloc(sizeof (struct Cell)); |
if (fldtab[i] == NULL) |
if (fldtab[i] == NULL) |
ERROR "out of space in makefields %d", i FATAL; |
FATAL("out of space in makefields %d", i); |
*fldtab[i] = dollar1; |
*fldtab[i] = dollar1; |
sprintf(temp, "%d", i); |
sprintf(temp, "%d", i); |
fldtab[i]->nval = tostring(temp); |
fldtab[i]->nval = tostring(temp); |
|
|
if (*file == '-' && *(file+1) == '\0') |
if (*file == '-' && *(file+1) == '\0') |
infile = stdin; |
infile = stdin; |
else if ((infile = fopen(file, "r")) == NULL) |
else if ((infile = fopen(file, "r")) == NULL) |
ERROR "can't open file %s", file FATAL; |
FATAL("can't open file %s", file); |
setfval(fnrloc, 0.0); |
setfval(fnrloc, 0.0); |
} |
} |
c = readrec(&buf, &bufsize, infile); |
c = readrec(&buf, &bufsize, infile); |
|
|
int bufsize = *pbufsize; |
int bufsize = *pbufsize; |
|
|
if (strlen(*FS) >= sizeof(inputFS)) |
if (strlen(*FS) >= sizeof(inputFS)) |
ERROR "field separator %.10s... is too long", *FS FATAL; |
FATAL("field separator %.10s... is too long", *FS); |
strcpy(inputFS, *FS); /* for subsequent field splitting */ |
strcpy(inputFS, *FS); /* for subsequent field splitting */ |
if ((sep = **RS) == 0) { |
if ((sep = **RS) == 0) { |
sep = '\n'; |
sep = '\n'; |
|
|
for (; (c=getc(inf)) != sep && c != EOF; ) { |
for (; (c=getc(inf)) != sep && c != EOF; ) { |
if (rr-buf+1 > bufsize) |
if (rr-buf+1 > bufsize) |
if (!adjbuf(&buf, &bufsize, 1+rr-buf, recsize, &rr, "readrec 1")) |
if (!adjbuf(&buf, &bufsize, 1+rr-buf, recsize, &rr, "readrec 1")) |
ERROR "input record `%.30s...' too long", buf FATAL; |
FATAL("input record `%.30s...' too long", buf); |
*rr++ = c; |
*rr++ = c; |
} |
} |
if (**RS == sep || c == EOF) |
if (**RS == sep || c == EOF) |
|
|
if ((c = getc(inf)) == '\n' || c == EOF) /* 2 in a row */ |
if ((c = getc(inf)) == '\n' || c == EOF) /* 2 in a row */ |
break; |
break; |
if (!adjbuf(&buf, &bufsize, 2+rr-buf, recsize, &rr, "readrec 2")) |
if (!adjbuf(&buf, &bufsize, 2+rr-buf, recsize, &rr, "readrec 2")) |
ERROR "input record `%.30s...' too long", buf FATAL; |
FATAL("input record `%.30s...' too long", buf); |
*rr++ = '\n'; |
*rr++ = '\n'; |
*rr++ = c; |
*rr++ = c; |
} |
} |
if (!adjbuf(&buf, &bufsize, 1+rr-buf, recsize, &rr, "readrec 3")) |
if (!adjbuf(&buf, &bufsize, 1+rr-buf, recsize, &rr, "readrec 3")) |
ERROR "input record `%.30s...' too long", buf FATAL; |
FATAL("input record `%.30s...' too long", buf); |
*rr = 0; |
*rr = 0; |
dprintf( ("readrec saw <%s>, returns %d\n", buf, c == EOF && rr == buf ? 0 : 1) ); |
dprintf( ("readrec saw <%s>, returns %d\n", buf, c == EOF && rr == buf ? 0 : 1) ); |
*pbuf = buf; |
*pbuf = buf; |
|
|
if (n > fieldssize) { |
if (n > fieldssize) { |
xfree(fields); |
xfree(fields); |
if ((fields = (char *) malloc(n+1)) == NULL) |
if ((fields = (char *) malloc(n+1)) == NULL) |
ERROR "out of space for fields in fldbld %d", n FATAL; |
FATAL("out of space for fields in fldbld %d", n); |
fieldssize = n; |
fieldssize = n; |
} |
} |
fr = fields; |
fr = fields; |
|
|
*fr = 0; |
*fr = 0; |
} |
} |
if (i > nfields) |
if (i > nfields) |
ERROR "record `%.30s...' has too many fields; can't happen", r FATAL; |
FATAL("record `%.30s...' has too many fields; can't happen", r); |
cleanfld(i+1, lastfld); /* clean out junk from previous record */ |
cleanfld(i+1, lastfld); /* clean out junk from previous record */ |
lastfld = i; |
lastfld = i; |
donefld = 1; |
donefld = 1; |
|
|
Cell *fieldadr(int n) /* get nth field */ |
Cell *fieldadr(int n) /* get nth field */ |
{ |
{ |
if (n < 0) |
if (n < 0) |
ERROR "trying to access field %d", n FATAL; |
FATAL("trying to access field %d", n); |
if (n > nfields) /* fields after NF are empty */ |
if (n > nfields) /* fields after NF are empty */ |
growfldtab(n); /* but does not increase NF */ |
growfldtab(n); /* but does not increase NF */ |
return(fldtab[n]); |
return(fldtab[n]); |
|
|
nf = n; |
nf = n; |
fldtab = (Cell **) realloc(fldtab, (nf+1) * (sizeof (struct Cell *))); |
fldtab = (Cell **) realloc(fldtab, (nf+1) * (sizeof (struct Cell *))); |
if (fldtab == NULL) |
if (fldtab == NULL) |
ERROR "out of space creating %d fields", nf FATAL; |
FATAL("out of space creating %d fields", nf); |
makefields(nfields+1, nf); |
makefields(nfields+1, nf); |
nfields = nf; |
nfields = nf; |
} |
} |
|
|
if (n > fieldssize) { |
if (n > fieldssize) { |
xfree(fields); |
xfree(fields); |
if ((fields = (char *) malloc(n+1)) == NULL) |
if ((fields = (char *) malloc(n+1)) == NULL) |
ERROR "out of space for fields in refldbld %d", n FATAL; |
FATAL("out of space for fields in refldbld %d", n); |
fieldssize = n; |
fieldssize = n; |
} |
} |
fr = fields; |
fr = fields; |
|
|
for (i = 1; i <= *NF; i++) { |
for (i = 1; i <= *NF; i++) { |
p = getsval(fldtab[i]); |
p = getsval(fldtab[i]); |
if (!adjbuf(&record, &recsize, 1+strlen(p)+r-record, recsize, &r, "recbld 1")) |
if (!adjbuf(&record, &recsize, 1+strlen(p)+r-record, recsize, &r, "recbld 1")) |
ERROR "created $0 `%.30s...' too long", record FATAL; |
FATAL("created $0 `%.30s...' too long", record); |
while ((*r = *p++) != 0) |
while ((*r = *p++) != 0) |
r++; |
r++; |
if (i < *NF) { |
if (i < *NF) { |
if (!adjbuf(&record, &recsize, 2+strlen(*OFS)+r-record, recsize, &r, "recbld 2")) |
if (!adjbuf(&record, &recsize, 2+strlen(*OFS)+r-record, recsize, &r, "recbld 2")) |
ERROR "created $0 `%.30s...' too long", record FATAL; |
FATAL("created $0 `%.30s...' too long", record); |
for (p = *OFS; (*r = *p++) != 0; ) |
for (p = *OFS; (*r = *p++) != 0; ) |
r++; |
r++; |
} |
} |
} |
} |
if (!adjbuf(&record, &recsize, 2+r-record, recsize, &r, "recbld 3")) |
if (!adjbuf(&record, &recsize, 2+r-record, recsize, &r, "recbld 3")) |
ERROR "built giant record `%.30s...'", record FATAL; |
FATAL("built giant record `%.30s...'", record); |
*r = '\0'; |
*r = '\0'; |
dprintf( ("in recbld inputFS=%s, fldtab[0]=%p\n", inputFS, fldtab[0]) ); |
dprintf( ("in recbld inputFS=%s, fldtab[0]=%p\n", inputFS, fldtab[0]) ); |
|
|
|
|
} |
} |
|
|
int errorflag = 0; |
int errorflag = 0; |
char errbuf[300]; /* used by ERROR macro */ |
|
|
|
void yyerror(char *s) |
void yyerror(char *s) |
{ |
{ |
|
SYNTAX(s); |
|
} |
|
|
|
void SYNTAX(char *fmt, ...) |
|
{ |
extern char *cmdname, *curfname; |
extern char *cmdname, *curfname; |
static int been_here = 0; |
static int been_here = 0; |
|
va_list varg; |
|
|
if (been_here++ > 2) |
if (been_here++ > 2) |
return; |
return; |
fprintf(stderr, "%s: %s", cmdname, s); |
fprintf(stderr, "%s: ", cmdname); |
|
va_start(varg, fmt); |
|
vfprintf(stderr, fmt, varg); |
|
va_end(varg); |
fprintf(stderr, " at source line %d", lineno); |
fprintf(stderr, " at source line %d", lineno); |
if (curfname != NULL) |
if (curfname != NULL) |
fprintf(stderr, " in function %s", curfname); |
fprintf(stderr, " in function %s", curfname); |
|
|
|
|
void fpecatch(int n) |
void fpecatch(int n) |
{ |
{ |
ERROR "floating point exception %d", n FATAL; |
FATAL("floating point exception %d", n); |
} |
} |
|
|
extern int bracecnt, brackcnt, parencnt; |
extern int bracecnt, brackcnt, parencnt; |
|
|
fprintf(stderr, "\t%d extra %c's\n", -n, c2); |
fprintf(stderr, "\t%d extra %c's\n", -n, c2); |
} |
} |
|
|
void error(int f, char *s) |
void FATAL(char *fmt, ...) |
{ |
{ |
extern Node *curnode; |
|
extern char *cmdname; |
extern char *cmdname; |
|
va_list varg; |
|
|
fflush(stdout); |
fflush(stdout); |
fprintf(stderr, "%s: ", cmdname); |
fprintf(stderr, "%s: ", cmdname); |
fprintf(stderr, "%s", s); |
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"); |
fprintf(stderr, "\n"); |
if (compile_time != 2 && NR && *NR > 0) { |
if (compile_time != 2 && NR && *NR > 0) { |
fprintf(stderr, " input record number %d", (int) (*FNR)); |
fprintf(stderr, " input record number %d", (int) (*FNR)); |
|
|
fprintf(stderr, " source file %s", cursource()); |
fprintf(stderr, " source file %s", cursource()); |
fprintf(stderr, "\n"); |
fprintf(stderr, "\n"); |
eprint(); |
eprint(); |
if (f) { |
|
if (dbg > 1) /* core dump if serious debugging on */ |
|
abort(); |
|
exit(2); |
|
} |
|
} |
} |
|
|
void eprint(void) /* try to print context around error */ |
void eprint(void) /* try to print context around error */ |
|
|
|
|
if (errno == EDOM) { |
if (errno == EDOM) { |
errno = 0; |
errno = 0; |
ERROR "%s argument out of domain", s WARNING; |
WARNING("%s argument out of domain", s); |
x = 1; |
x = 1; |
} else if (errno == ERANGE) { |
} else if (errno == ERANGE) { |
errno = 0; |
errno = 0; |
ERROR "%s result out of range", s WARNING; |
WARNING("%s result out of range", s); |
x = 1; |
x = 1; |
} |
} |
return x; |
return x; |