55 |
|
#define freqstr(s) efree(s) |
56 |
|
|
57 |
|
static int getinputrec(FILE *fp); |
58 |
< |
static void scaninp(void), advinp(void), passinp(void), skipinp(void); |
58 |
> |
static void scaninp(void), skipinp(void), advinp(int skip); |
59 |
|
static void putrec(void), putout(void), nbsynch(void); |
60 |
|
static int getrec(void); |
61 |
|
static void execute(char *file); |
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 (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 |
> |
if (inpfmt != NULL) |
352 |
> |
advinp(0); |
353 |
|
putout(); |
354 |
|
++nout; |
345 |
– |
advinp(); |
355 |
|
} else if (inpfmt != NULL) { |
356 |
< |
if (passive < 0) |
348 |
< |
passinp(); |
349 |
< |
else |
350 |
< |
advinp(); |
356 |
> |
advinp(1); |
357 |
|
} |
358 |
|
if (incnt && nrecs >= incnt) |
359 |
|
break; |
928 |
|
|
929 |
|
|
930 |
|
static void |
931 |
< |
advinp(void) /* move home to current position */ |
931 |
> |
skipinp(void) /* rewind position and advance 1 */ |
932 |
|
{ |
933 |
< |
ipb.beg = ipb.pos; |
933 |
> |
if (ipb.beg == NULL) /* can't fully rewind? */ |
934 |
> |
ipb.beg = ipb.end; |
935 |
> |
ipb.pos = ipb.beg; |
936 |
> |
ipb.chr = *ipb.pos; |
937 |
> |
if (passive) /* transmit unmatched character? */ |
938 |
> |
putchar(ipb.chr); |
939 |
> |
if (++ipb.beg >= &inpbuf[INBSIZ]) |
940 |
> |
ipb.beg = inpbuf; |
941 |
> |
scaninp(); |
942 |
|
} |
943 |
|
|
944 |
|
|
945 |
|
static void |
946 |
< |
passinp(void) /* pass beginning to current position */ |
946 |
> |
advinp(int skip) /* advance home to current position */ |
947 |
|
{ |
948 |
< |
if (!passive | (ipb.beg == NULL)) { |
949 |
< |
advinp(); |
948 |
> |
if (!skip | (passive >= 0)) { |
949 |
> |
ipb.beg = ipb.pos; /* no need to copy input */ |
950 |
|
return; |
951 |
|
} |
952 |
< |
while (ipb.beg != ipb.pos) { /* transfer buffer unaltered */ |
952 |
> |
if (ipb.beg == NULL) /* buffer overflowed a bit? */ |
953 |
> |
ipb.beg = ipb.end; |
954 |
> |
while (ipb.beg != ipb.pos) { /* copy buffer to current */ |
955 |
|
putchar(*ipb.beg); |
956 |
|
if (++ipb.beg >= &inpbuf[INBSIZ]) |
957 |
|
ipb.beg = inpbuf; |
958 |
|
} |
943 |
– |
} |
944 |
– |
|
945 |
– |
|
946 |
– |
static void |
947 |
– |
skipinp(void) /* rewind position and advance 1 */ |
948 |
– |
{ |
949 |
– |
if (ipb.beg == NULL) /* full */ |
950 |
– |
ipb.beg = ipb.end; |
951 |
– |
ipb.pos = ipb.beg; |
952 |
– |
ipb.chr = *ipb.pos; |
953 |
– |
if (passive) /* transmit unmatched character? */ |
954 |
– |
putchar(ipb.chr); |
955 |
– |
if (++ipb.beg >= &inpbuf[INBSIZ]) |
956 |
– |
ipb.beg = inpbuf; |
957 |
– |
scaninp(); |
959 |
|
} |
960 |
|
|
961 |
|
|