55 |
|
#define freqstr(s) efree(s) |
56 |
|
|
57 |
|
static int getinputrec(FILE *fp); |
58 |
< |
static void scaninp(void), advinp(void), skipinp(void); |
58 |
> |
static void scaninp(void), advinp(void), passinp(void), skipinp(void); |
59 |
|
static void putrec(void), putout(void), nbsynch(void); |
60 |
|
static int getrec(void); |
61 |
|
static void execute(char *file); |
69 |
|
static struct strvar* getsvar(char *svname); |
70 |
|
static double l_in(char *); |
71 |
|
|
72 |
< |
struct field *inpfmt = NULL; /* input record format */ |
73 |
< |
struct field *outfmt = NULL; /* output record structure */ |
74 |
< |
struct strvar *svhead = NULL; /* string variables */ |
72 |
> |
struct field *inpfmt = NULL; /* input record format */ |
73 |
> |
struct field *outfmt = NULL; /* output record structure */ |
74 |
> |
struct strvar *svhead = NULL; /* string variables */ |
75 |
|
|
76 |
< |
long incnt = 0; /* limit number of input records? */ |
76 |
> |
long incnt = 0; /* limit number of input records? */ |
77 |
|
long outcnt = 0; /* limit number of output records? */ |
78 |
|
|
79 |
< |
int blnkeq = 1; /* blanks compare equal? */ |
80 |
< |
int igneol = 0; /* ignore end of line? */ |
79 |
> |
int blnkeq = 1; /* blanks compare equal? */ |
80 |
> |
int igneol = 0; /* ignore end of line? */ |
81 |
|
int passive = 0; /* passive mode (transmit unmatched input) */ |
82 |
< |
char sepchar = '\t'; /* input/output separator */ |
83 |
< |
int noinput = 0; /* no input records? */ |
82 |
> |
char sepchar = '\t'; /* input/output separator */ |
83 |
> |
int noinput = 0; /* no input records? */ |
84 |
|
int itype = 'a'; /* input type (a/f/F/d/D) */ |
85 |
|
int nbicols = 0; /* number of binary input columns */ |
86 |
|
int otype = 'a'; /* output format (a/f/F/d/D) */ |
87 |
< |
char inpbuf[INBSIZ]; /* input buffer */ |
88 |
< |
double colval[MAXCOL]; /* input column values */ |
89 |
< |
unsigned long colflg = 0; /* column retrieved flags */ |
90 |
< |
int colpos; /* output column position */ |
87 |
> |
char inpbuf[INBSIZ]; /* input buffer */ |
88 |
> |
double colval[MAXCOL]; /* input column values */ |
89 |
> |
unsigned long colflg = 0; /* column retrieved flags */ |
90 |
> |
int colpos; /* output column position */ |
91 |
|
|
92 |
< |
int nowarn = 0; /* non-fatal diagnostic output */ |
93 |
< |
int unbuff = 0; /* unbuffered output (flush each record) */ |
92 |
> |
int nowarn = 0; /* non-fatal diagnostic output */ |
93 |
> |
int unbuff = 0; /* unbuffered output (flush each record) */ |
94 |
|
|
95 |
|
struct { |
96 |
|
FILE *fin; /* input file */ |
128 |
|
igneol = !igneol; |
129 |
|
break; |
130 |
|
case 'p': |
131 |
< |
passive = !passive; |
131 |
> |
passive = 1; |
132 |
|
break; |
133 |
+ |
case 'P': |
134 |
+ |
passive = -1; |
135 |
+ |
break; |
136 |
|
case 't': |
137 |
|
sepchar = argv[i][2]; |
138 |
|
break; |
232 |
|
userr: |
233 |
|
eputs("Usage: "); |
234 |
|
eputs(argv[0]); |
235 |
< |
eputs(" [-b][-l][-n][-p][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n"); |
235 |
> |
eputs(" [-b][-l][-n][-p|-P][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n"); |
236 |
|
quit(1); |
237 |
|
} |
238 |
|
if (otype != 'a') |
250 |
|
putout(); |
251 |
|
quit(0); |
252 |
|
} |
253 |
+ |
if (passive && (inpfmt == NULL) | (outfmt == NULL)) { |
254 |
+ |
eputs(argv[0]); |
255 |
+ |
eputs(": options -p and -P require -i and -o formats\n"); |
256 |
+ |
quit(1); |
257 |
+ |
} |
258 |
|
if (blnkeq) /* for efficiency */ |
259 |
|
nbsynch(); |
260 |
|
|
286 |
|
FILE *fp |
287 |
|
) |
288 |
|
{ |
281 |
– |
if (inpfmt != NULL) |
282 |
– |
return(getrec()); |
289 |
|
if ((itype == 'd') | (itype == 'D')) { |
290 |
|
if (getbinary(inpbuf, sizeof(double), nbicols, fp) != nbicols) |
291 |
|
return(0); |
309 |
|
char *file |
310 |
|
) |
311 |
|
{ |
312 |
< |
const int conditional = vardefined("cond"); |
313 |
< |
const int set_recno = (varlookup("recno") != NULL); |
314 |
< |
const int set_outno = (varlookup("outno") != NULL); |
315 |
< |
long nrecs = 0; |
316 |
< |
long nout = 0; |
317 |
< |
FILE *fp; |
312 |
> |
static char condVN[] = "cond"; |
313 |
> |
static char recnoVN[] = "recno"; |
314 |
> |
static char outnoVN[] = "outno"; |
315 |
> |
const int conditional = vardefined(condVN); |
316 |
> |
const int set_recno = (varlookup(recnoVN) != NULL); |
317 |
> |
const int set_outno = (varlookup(outnoVN) != NULL); |
318 |
> |
long nrecs = 0; |
319 |
> |
long nout = 0; |
320 |
> |
FILE *fp; |
321 |
|
|
322 |
|
if (file == NULL) |
323 |
|
fp = stdin; |
331 |
|
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
332 |
|
flockfile(fp); |
333 |
|
#endif |
334 |
+ |
if (conditional == ':') { |
335 |
+ |
eputs(condVN); |
336 |
+ |
eputs(": defined as constant\n"); |
337 |
+ |
quit(1); |
338 |
+ |
} |
339 |
|
if (inpfmt != NULL) |
340 |
|
initinp(fp); |
341 |
|
|
342 |
< |
while (getinputrec(fp)) { |
342 |
> |
while (inpfmt != NULL ? getrec() : getinputrec(fp)) { |
343 |
|
++nrecs; |
344 |
|
if (set_recno) |
345 |
< |
varset("recno", '=', (double)nrecs); |
345 |
> |
varset(recnoVN, '=', (double)nrecs); |
346 |
|
if (set_outno) |
347 |
< |
varset("outno", '=', (double)(nout+1)); |
347 |
> |
varset(outnoVN, '=', (double)(nout+1)); |
348 |
|
colflg = 0; |
349 |
|
eclock++; |
350 |
< |
if (!conditional || varvalue("cond") > 0.0) { |
350 |
> |
if (!conditional || varvalue(condVN) > 0.0) { |
351 |
|
putout(); |
352 |
|
++nout; |
353 |
+ |
advinp(); |
354 |
+ |
} else if (inpfmt != NULL) { |
355 |
+ |
if (passive < 0) |
356 |
+ |
passinp(); |
357 |
+ |
else |
358 |
+ |
advinp(); |
359 |
|
} |
360 |
|
if (incnt && nrecs >= incnt) |
361 |
|
break; |
708 |
|
|
709 |
|
|
710 |
|
static int |
711 |
< |
getrec(void) /* get next record from file */ |
711 |
> |
getrec(void) /* get next record from file */ |
712 |
|
{ |
713 |
|
int eatline; |
714 |
|
struct field *f; |
725 |
|
for (f = inpfmt; f != NULL; f = f->next) |
726 |
|
if (!getfield(f)) |
727 |
|
break; |
728 |
< |
if (f == NULL) { |
709 |
< |
advinp(); /* got one! */ |
728 |
> |
if (f == NULL) /* got one? */ |
729 |
|
return(1); |
730 |
< |
} |
731 |
< |
skipinp(); /* eat false start */ |
713 |
< |
if (eatline) { /* eat rest of line */ |
730 |
> |
skipinp(); /* else eat false start */ |
731 |
> |
if (eatline) { /* eat rest of line */ |
732 |
|
while (ipb.chr != '\n') { |
733 |
|
if (ipb.chr == EOF) |
734 |
|
return(0); |
937 |
|
|
938 |
|
|
939 |
|
static void |
940 |
< |
skipinp(void) /* rewind position and advance 1 */ |
940 |
> |
passinp(void) /* pass beginning to current position */ |
941 |
|
{ |
942 |
< |
if (ipb.beg == NULL) /* full */ |
942 |
> |
if (!passive | (ipb.beg == NULL)) { |
943 |
> |
advinp(); |
944 |
> |
return; |
945 |
> |
} |
946 |
> |
while (ipb.beg != ipb.pos) { /* transfer buffer unaltered */ |
947 |
> |
putchar(*ipb.beg); |
948 |
> |
if (++ipb.beg >= &inpbuf[INBSIZ]) |
949 |
> |
ipb.beg = inpbuf; |
950 |
> |
} |
951 |
> |
} |
952 |
> |
|
953 |
> |
|
954 |
> |
static void |
955 |
> |
skipinp(void) /* rewind position and advance 1 */ |
956 |
> |
{ |
957 |
> |
if (ipb.beg == NULL) /* full */ |
958 |
|
ipb.beg = ipb.end; |
959 |
|
ipb.pos = ipb.beg; |
960 |
|
ipb.chr = *ipb.pos; |