[BACK]Return to lib.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / awk

Diff for /src/usr.bin/awk/lib.c between version 1.6 and 1.7

version 1.6, 1999/04/20 17:31:29 version 1.7, 1999/12/08 23:09:45
Line 29 
Line 29 
 #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"
   
Line 61 
Line 62 
         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;
Line 78 
Line 79 
         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);
Line 137 
Line 138 
                         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);
Line 184 
Line 185 
         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';
Line 197 
Line 198 
                 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)
Line 205 
Line 206 
                 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;
Line 267 
Line 268 
         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;
Line 325 
Line 326 
                 *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;
Line 371 
Line 372 
 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]);
Line 385 
Line 386 
                 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;
 }  }
Line 402 
Line 403 
         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;
Line 448 
Line 449 
         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]) );
   
Line 474 
Line 475 
 }  }
   
 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);
Line 496 
Line 505 
   
 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;
Line 527 
Line 536 
                 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));
Line 550 
Line 584 
                 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 */
Line 610 
Line 639 
   
         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;

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.7