lib.c (112336) | lib.c (146299) |
---|---|
1/**************************************************************** 2Copyright (C) Lucent Technologies 1997 3All Rights Reserved 4 5Permission to use, copy, modify, and distribute this software and 6its documentation for any purpose and without fee is hereby 7granted, provided that the above copyright notice appear in all 8copies and that both that the copyright notice and this --- 43 unchanged lines hidden (view full) --- 52int argno = 1; /* current input argument number */ 53extern Awkfloat *ARGC; 54 55static Cell dollar0 = { OCELL, CFLD, NULL, "", 0.0, REC|STR|DONTFREE }; 56static Cell dollar1 = { OCELL, CFLD, NULL, "", 0.0, FLD|STR|DONTFREE }; 57 58void recinit(unsigned int n) 59{ | 1/**************************************************************** 2Copyright (C) Lucent Technologies 1997 3All Rights Reserved 4 5Permission to use, copy, modify, and distribute this software and 6its documentation for any purpose and without fee is hereby 7granted, provided that the above copyright notice appear in all 8copies and that both that the copyright notice and this --- 43 unchanged lines hidden (view full) --- 52int argno = 1; /* current input argument number */ 53extern Awkfloat *ARGC; 54 55static Cell dollar0 = { OCELL, CFLD, NULL, "", 0.0, REC|STR|DONTFREE }; 56static Cell dollar1 = { OCELL, CFLD, NULL, "", 0.0, FLD|STR|DONTFREE }; 57 58void recinit(unsigned int n) 59{ |
60 record = (char *) malloc(n); 61 fields = (char *) malloc(n); 62 fldtab = (Cell **) malloc((nfields+1) * sizeof(Cell *)); 63 if (record == NULL || fields == NULL || fldtab == NULL) | 60 if ( (record = (char *) malloc(n)) == NULL 61 || (fields = (char *) malloc(n)) == NULL 62 || (fldtab = (Cell **) malloc((nfields+1) * sizeof(Cell *))) == NULL 63 || (fldtab[0] = (Cell *) malloc(sizeof(Cell))) == NULL ) |
64 FATAL("out of space for $0 and fields"); | 64 FATAL("out of space for $0 and fields"); |
65 66 fldtab[0] = (Cell *) malloc(sizeof (Cell)); | |
67 *fldtab[0] = dollar0; 68 fldtab[0]->sval = record; 69 fldtab[0]->nval = tostring("0"); 70 makefields(1, nfields); 71} 72 73void makefields(int n1, int n2) /* create $n1..$n2 inclusive */ 74{ --- 21 unchanged lines hidden (view full) --- 96 return; 97 } 98 setclvar(p); /* a commandline assignment before filename */ 99 argno++; 100 } 101 infile = stdin; /* no filenames, so use stdin */ 102} 103 | 65 *fldtab[0] = dollar0; 66 fldtab[0]->sval = record; 67 fldtab[0]->nval = tostring("0"); 68 makefields(1, nfields); 69} 70 71void makefields(int n1, int n2) /* create $n1..$n2 inclusive */ 72{ --- 21 unchanged lines hidden (view full) --- 94 return; 95 } 96 setclvar(p); /* a commandline assignment before filename */ 97 argno++; 98 } 99 infile = stdin; /* no filenames, so use stdin */ 100} 101 |
102static int firsttime = 1; 103 |
|
104int getrec(char **pbuf, int *pbufsize, int isrecord) /* get next input record */ 105{ /* note: cares whether buf == record */ 106 int c; | 104int getrec(char **pbuf, int *pbufsize, int isrecord) /* get next input record */ 105{ /* note: cares whether buf == record */ 106 int c; |
107 static int firsttime = 1; | |
108 char *buf = *pbuf; | 107 char *buf = *pbuf; |
109 int bufsize = *pbufsize; | 108 uschar saveb0; 109 int bufsize = *pbufsize, savebufsize = bufsize; |
110 111 if (firsttime) { 112 firsttime = 0; 113 initgetrec(); 114 } 115 dprintf( ("RS=<%s>, FS=<%s>, ARGC=%g, FILENAME=%s\n", 116 *RS, *FS, *ARGC, *FILENAME) ); 117 if (isrecord) { 118 donefld = 0; 119 donerec = 1; 120 } | 110 111 if (firsttime) { 112 firsttime = 0; 113 initgetrec(); 114 } 115 dprintf( ("RS=<%s>, FS=<%s>, ARGC=%g, FILENAME=%s\n", 116 *RS, *FS, *ARGC, *FILENAME) ); 117 if (isrecord) { 118 donefld = 0; 119 donerec = 1; 120 } |
121 saveb0 = buf[0]; |
|
121 buf[0] = 0; 122 while (argno < *ARGC || infile == stdin) { 123 dprintf( ("argno=%d, file=|%s|\n", argno, file) ); 124 if (infile == NULL) { /* have to open a new file */ 125 file = getargv(argno); 126 if (*file == '\0') { /* it's been zapped */ 127 argno++; 128 continue; --- 30 unchanged lines hidden (view full) --- 159 return 1; 160 } 161 /* EOF arrived on this file; set up next */ 162 if (infile != stdin) 163 fclose(infile); 164 infile = NULL; 165 argno++; 166 } | 122 buf[0] = 0; 123 while (argno < *ARGC || infile == stdin) { 124 dprintf( ("argno=%d, file=|%s|\n", argno, file) ); 125 if (infile == NULL) { /* have to open a new file */ 126 file = getargv(argno); 127 if (*file == '\0') { /* it's been zapped */ 128 argno++; 129 continue; --- 30 unchanged lines hidden (view full) --- 160 return 1; 161 } 162 /* EOF arrived on this file; set up next */ 163 if (infile != stdin) 164 fclose(infile); 165 infile = NULL; 166 argno++; 167 } |
168 buf[0] = saveb0; |
|
167 *pbuf = buf; | 169 *pbuf = buf; |
168 *pbufsize = bufsize; | 170 *pbufsize = savebufsize; |
169 return 0; /* true end of file */ 170} 171 172void nextfile(void) 173{ 174 if (infile != stdin) 175 fclose(infile); 176 infile = NULL; --- 196 unchanged lines hidden (view full) --- 373 cleanfld(lastfld+1, n); 374 lastfld = n; 375 setfval(nfloc, (Awkfloat) n); 376} 377 378Cell *fieldadr(int n) /* get nth field */ 379{ 380 if (n < 0) | 171 return 0; /* true end of file */ 172} 173 174void nextfile(void) 175{ 176 if (infile != stdin) 177 fclose(infile); 178 infile = NULL; --- 196 unchanged lines hidden (view full) --- 375 cleanfld(lastfld+1, n); 376 lastfld = n; 377 setfval(nfloc, (Awkfloat) n); 378} 379 380Cell *fieldadr(int n) /* get nth field */ 381{ 382 if (n < 0) |
381 FATAL("trying to access field %d", n); | 383 FATAL("trying to access out of range field %d", n); |
382 if (n > nfields) /* fields after NF are empty */ 383 growfldtab(n); /* but does not increase NF */ 384 return(fldtab[n]); 385} 386 387void growfldtab(int n) /* make new fields up to at least $n */ 388{ 389 int nf = 2 * nfields; | 384 if (n > nfields) /* fields after NF are empty */ 385 growfldtab(n); /* but does not increase NF */ 386 return(fldtab[n]); 387} 388 389void growfldtab(int n) /* make new fields up to at least $n */ 390{ 391 int nf = 2 * nfields; |
392 size_t s; |
|
390 391 if (n > nf) 392 nf = n; | 393 394 if (n > nf) 395 nf = n; |
393 fldtab = (Cell **) realloc(fldtab, (nf+1) * (sizeof (struct Cell *))); | 396 s = (nf+1) * (sizeof (struct Cell *)); /* freebsd: how much do we need? */ 397 if (s / sizeof(struct Cell *) - 1 == nf) /* didn't overflow */ 398 fldtab = (Cell **) realloc(fldtab, s); 399 else /* overflow sizeof int */ 400 xfree(fldtab); /* make it null */ |
394 if (fldtab == NULL) 395 FATAL("out of space creating %d fields", nf); 396 makefields(nfields+1, nf); 397 nfields = nf; 398} 399 400int refldbld(const char *rec, const char *fs) /* build fields from reg expr in FS */ 401{ --- 77 unchanged lines hidden (view full) --- 479 dprintf( ("recbld = |%s|\n", record) ); 480 donerec = 1; 481} 482 483int errorflag = 0; 484 485void yyerror(const char *s) 486{ | 401 if (fldtab == NULL) 402 FATAL("out of space creating %d fields", nf); 403 makefields(nfields+1, nf); 404 nfields = nf; 405} 406 407int refldbld(const char *rec, const char *fs) /* build fields from reg expr in FS */ 408{ --- 77 unchanged lines hidden (view full) --- 486 dprintf( ("recbld = |%s|\n", record) ); 487 donerec = 1; 488} 489 490int errorflag = 0; 491 492void yyerror(const char *s) 493{ |
487 SYNTAX(s); | 494 SYNTAX("%s", s); |
488} 489 490void SYNTAX(const char *fmt, ...) 491{ 492 extern char *cmdname, *curfname; 493 static int been_here = 0; 494 va_list varg; 495 --- 194 unchanged lines hidden --- | 495} 496 497void SYNTAX(const char *fmt, ...) 498{ 499 extern char *cmdname, *curfname; 500 static int been_here = 0; 501 va_list varg; 502 --- 194 unchanged lines hidden --- |