--- ray/src/cal/rcalc.c 2003/04/27 06:08:03 1.3 +++ ray/src/cal/rcalc.c 2024/02/23 03:47:57 1.39 @@ -1,106 +1,123 @@ #ifndef lint -static const char RCSid[] = "$Id: rcalc.c,v 1.3 2003/04/27 06:08:03 greg Exp $"; +static const char RCSid[] = "$Id: rcalc.c,v 1.39 2024/02/23 03:47:57 greg Exp $"; #endif /* - * rcalc.c - record calculator program. + * rcalc.c - record calculator program. * - * 9/11/87 + * 9/11/87 */ -#include +#include +#include +#include -#include +#include "platform.h" +#include "rterror.h" +#include "rtmisc.h" +#include "rtio.h" +#include "calcomp.h" -#include +#define isnum(c) (isdigit(c) || ((c)=='-') | ((c)=='.') \ + | ((c)=='+') | ((c)=='e') | ((c)=='E')) -#include +#define isblnk(c) (igneol ? isspace(c) : ((c)==' ')|((c)=='\t')) -#include "calcomp.h" +#define INBSIZ 16384 /* longest record */ +#define MAXCOL 32 /* number of columns recorded */ -#ifdef CPM -#define getc agetc /* text files only, right? */ -#endif + /* field type specifications */ +#define F_NUL 0 /* empty */ +#define F_TYP 0x7000 /* mask for type */ +#define F_WID 0x0fff /* mask for width */ +#define T_LIT 0x1000 /* string literal */ +#define T_STR 0x2000 /* string variable */ +#define T_NUM 0x3000 /* numeric value */ -#define isnum(c) (isdigit(c) || (c)=='-' || (c)=='.' \ - || (c)=='+' || (c)=='e' || (c)=='E') - -#define isblnk(c) (igneol ? isspace(c) : (c)==' '||(c)=='\t') - -#define INBSIZ 4096 /* longest record */ -#define MAXCOL 32 /* number of columns recorded */ - - /* field type specifications */ -#define F_NUL 0 /* empty */ -#define F_TYP 0x7000 /* mask for type */ -#define F_WID 0x0fff /* mask for width */ -#define T_LIT 0x1000 /* string literal */ -#define T_STR 0x2000 /* string variable */ -#define T_NUM 0x3000 /* numeric value */ - -struct strvar { /* string variable */ - char *name; - char *val; - char *preset; - struct strvar *next; +struct strvar { /* string variable */ + char *name; + char *val; + char *preset; + struct strvar *next; }; -struct field { /* record format structure */ - int type; /* type of field (& width) */ +struct field { /* record format structure */ + int type; /* type of field (& width) */ union { - char *sl; /* string literal */ - struct strvar *sv; /* string variable */ - char *nv; /* numeric variable */ - EPNODE *ne; /* numeric expression */ - } f; /* field contents */ - struct field *next; /* next field in record */ + char *sl; /* string literal */ + struct strvar *sv; /* string variable */ + char *nv; /* numeric variable */ + EPNODE *ne; /* numeric expression */ + } f; /* field contents */ + struct field *next; /* next field in record */ }; -#define savqstr(s) strcpy(emalloc(strlen(s)+1),s) -#define freqstr(s) efree(s) +#define savqstr(s) strcpy(emalloc(strlen(s)+1),s) +#define freqstr(s) efree(s) -extern char *strcpy(), *emalloc(), *savestr(); -struct strvar *getsvar(); +static int getinputrec(FILE *fp); +static void scaninp(void), skipinp(void), advinp(int skip); +static void putrec(void), putout(void), nbsynch(void); +static int getrec(void); +static void execute(char *file); +static void initinp(FILE *fp); +static void svpreset(char *eqn); +static void readfmt(char *spec, int output); +static int readfield(char **pp); +static int getfield(struct field *f); +static void chanset(int n, double v); +static void bchanset(int n, double v); +static struct strvar* getsvar(char *svname); +static double l_in(char *); -struct field *inpfmt = NULL; /* input record format */ -struct field *outfmt = NULL; /* output record structure */ -struct strvar *svhead = NULL; /* string variables */ +struct field *inpfmt = NULL; /* input record format */ +struct field *outfmt = NULL; /* output record structure */ +struct strvar *svhead = NULL; /* string variables */ -int blnkeq = 1; /* blanks compare equal? */ -int igneol = 0; /* ignore end of line? */ -char sepchar = '\t'; /* input/output separator */ -int noinput = 0; /* no input records? */ -int nbicols = 0; /* number of binary input columns */ -int bocols = 0; /* produce binary output columns */ -char inpbuf[INBSIZ]; /* input buffer */ -double colval[MAXCOL]; /* input column values */ -unsigned long colflg = 0; /* column retrieved flags */ -int colpos; /* output column position */ +long incnt = 0; /* limit number of input records? */ +long outcnt = 0; /* limit number of output records? */ -int nowarn = 0; /* non-fatal diagnostic output */ -int unbuff = 0; /* unbuffered output (flush each record) */ +int blnkeq = 1; /* blanks compare equal? */ +int igneol = 0; /* ignore end of line? */ +int passive = 0; /* passive mode (transmit unmatched input) */ +char sepchar = '\t'; /* input/output separator */ +int noinput = 0; /* no input records? */ +int itype = 'a'; /* input type (a/f/F/d/D) */ +int nbicols = 0; /* number of binary input columns */ +int otype = 'a'; /* output format (a/f/F/d/D) */ +char inpbuf[INBSIZ]; /* input buffer */ +double colval[MAXCOL]; /* input column values */ +unsigned long colflg = 0; /* column retrieved flags */ +int colpos; /* output column position */ +int nowarn = 0; /* non-fatal diagnostic output */ +int unbuff = 0; /* unbuffered output (flush each record) */ + struct { - FILE *fin; /* input file */ - int chr; /* next character */ - char *beg; /* home position */ - char *pos; /* scan position */ - char *end; /* read position */ -} ipb; /* circular lookahead buffer */ + FILE *fin; /* input file */ + int chr; /* next character */ + char *beg; /* home position */ + char *pos; /* scan position */ + char *end; /* read position */ +} ipb; /* circular lookahead buffer */ -main(argc, argv) -int argc; -char *argv[]; +int +main( +int argc, +char *argv[] +) { - int i; + char *fpath; + int i; esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_OUTCHAN|E_RCONST; esupport &= ~(E_REDEFW); -#ifdef BIGGERLIB +#ifdef BIGGERLIB biggerlib(); #endif varset("PI", ':', 3.14159265358979323846); + funset("in", 1, '=', &l_in); for (i = 1; i < argc && argv[i][0] == '-'; i++) switch (argv[i][1]) { @@ -110,6 +127,12 @@ char *argv[]; case 'l': igneol = !igneol; break; + case 'p': + passive = 1; + break; + case 'P': + passive = -1; + break; case 't': sepchar = argv[i][2]; break; @@ -117,34 +140,62 @@ char *argv[]; svpreset(argv[++i]); break; case 'f': - fcompile(argv[++i]); + fpath = getpath(argv[++i], getrlibpath(), 0); + if (fpath == NULL) { + eputs(argv[0]); + eputs(": cannot find file '"); + eputs(argv[i]); + eputs("'\n"); + quit(1); + } + fcompile(fpath); break; case 'e': scompile(argv[++i], NULL, 0); break; case 'n': noinput = 1; + esupport &= ~E_RCONST; break; case 'i': switch (argv[i][2]) { case '\0': + itype = 'a'; nbicols = 0; readfmt(argv[++i], 0); break; + case 'n': + incnt = atol(argv[++i]); + break; case 'a': + itype = 'a'; nbicols = 0; break; case 'd': + case 'D': + itype = argv[i][2]; if (isdigit(argv[i][3])) nbicols = atoi(argv[i]+3); else nbicols = 1; + if (nbicols*sizeof(double) > INBSIZ) { + eputs(argv[0]); + eputs(": too many input columns\n"); + quit(1); + } break; case 'f': + case 'F': + itype = argv[i][2]; if (isdigit(argv[i][3])) - nbicols = -atoi(argv[i]+3); + nbicols = atoi(argv[i]+3); else - nbicols = -1; + nbicols = 1; + if (nbicols*sizeof(float) > INBSIZ) { + eputs(argv[0]); + eputs(": too many input columns\n"); + quit(1); + } break; default: goto userr; @@ -153,18 +204,23 @@ char *argv[]; case 'o': switch (argv[i][2]) { case '\0': - bocols = 0; + otype = 'a'; readfmt(argv[++i], 1); break; + case 'n': + outcnt = atol(argv[++i]); + break; case 'a': - bocols = 0; + otype = 'a'; break; case 'd': - bocols = 1; - break; + case 'D': case 'f': - bocols = -1; + case 'F': + otype = argv[i][2]; break; + default: + goto userr; } break; case 'w': @@ -177,30 +233,47 @@ char *argv[]; userr: eputs("Usage: "); eputs(argv[0]); -eputs(" [-b][-l][-n][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n"); +eputs(" [-b][-l][-n][-p|-P][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n"); quit(1); } - - if (noinput) { /* produce a single output record */ + if (otype != 'a') + SET_FILE_BINARY(stdout); +#ifdef getc_unlocked /* avoid lock/unlock overhead */ + flockfile(stdout); +#endif + if (noinput) { /* produce a single output record */ + if (i < argc) { + eputs(argv[0]); + eputs(": file argument(s) incompatible with -n\n"); + quit(1); + } eclock++; putout(); quit(0); } - - if (blnkeq) /* for efficiency */ + if (passive && (inpfmt == NULL) | (outfmt == NULL)) { + eputs(argv[0]); + eputs(": options -p and -P require -i and -o formats\n"); + quit(1); + } + if (blnkeq) /* for efficiency */ nbsynch(); - if (i == argc) /* from stdin */ + doptimize(1); /* optimize definitions */ + + if (i == argc) /* from stdin */ execute(NULL); - else /* from one or more files */ + else /* from one or more files */ for ( ; i < argc; i++) execute(argv[i]); quit(0); + return 0; /* pro forma return */ } -nbsynch() /* non-blank starting synch character */ +static void +nbsynch(void) /* non-blank starting synch character */ { if (inpfmt == NULL || (inpfmt->type & F_TYP) != T_LIT) return; @@ -211,29 +284,43 @@ nbsynch() /* non-blank starting synch ch } -int -getinputrec(fp) /* get next input record */ -FILE *fp; +static int +getinputrec( /* get next input record */ +FILE *fp +) { - if (inpfmt != NULL) - return(getrec()); - if (nbicols > 0) - return(fread(inpbuf, sizeof(double), - nbicols, fp) == nbicols); - if (nbicols < 0) - return(fread(inpbuf, sizeof(float), - -nbicols, fp) == -nbicols); + if ((itype == 'd') | (itype == 'D')) { + if (getbinary(inpbuf, sizeof(double), nbicols, fp) != nbicols) + return(0); + if (itype == 'D') + swap64(inpbuf, nbicols); + return(1); + } + if ((itype == 'f') | (itype == 'F')) { + if (getbinary(inpbuf, sizeof(float), nbicols, fp) != nbicols) + return(0); + if (itype == 'F') + swap32(inpbuf, nbicols); + return(1); + } return(fgets(inpbuf, INBSIZ, fp) != NULL); } -execute(file) /* process a file */ -char *file; +static void +execute( /* process a file */ +char *file +) { - int conditional = vardefined("cond"); - long nrecs = 0; - long nout = 0; - FILE *fp; + static char condVN[] = "cond"; + static char recnoVN[] = "recno"; + static char outnoVN[] = "outno"; + const int conditional = vardefined(condVN); + const int set_recno = (varlookup(recnoVN) != NULL); + const int set_outno = (varlookup(outnoVN) != NULL); + long nrecs = 0; + long nout = 0; + FILE *fp; if (file == NULL) fp = stdin; @@ -242,46 +329,99 @@ char *file; eputs(": cannot open\n"); quit(1); } + if (itype != 'a') + SET_FILE_BINARY(fp); +#ifdef getc_unlocked /* avoid lock/unlock overhead */ + flockfile(fp); +#endif + if (conditional == ':') { + eputs(condVN); + eputs(": defined as constant\n"); + quit(1); + } if (inpfmt != NULL) initinp(fp); - while (getinputrec(fp)) { - varset("recno", '=', (double)++nrecs); + while (inpfmt != NULL ? getrec() : getinputrec(fp)) { + ++nrecs; + if (set_recno) + varset(recnoVN, '=', (double)nrecs); + if (set_outno) + varset(outnoVN, '=', (double)(nout+1)); colflg = 0; eclock++; - if (!conditional || varvalue("cond") > 0.0) { - varset("outno", '=', (double)++nout); + if (!conditional || varvalue(condVN) > 0.0) { + if (inpfmt != NULL) + advinp(0); putout(); + ++nout; + } else if (inpfmt != NULL) { + advinp(1); } + if (incnt && nrecs >= incnt) + break; + if (outcnt && nout >= outcnt) + break; } fclose(fp); } -putout() /* produce an output record */ +static void +putout(void) /* produce an output record */ { - extern void chanset(), bchanset(); colpos = 0; if (outfmt != NULL) putrec(); - else if (bocols) - chanout(bchanset); - else + else if (otype == 'a') chanout(chanset); - if (colpos && !bchanset) + else + chanout(bchanset); + if (colpos && otype == 'a') putchar('\n'); if (unbuff) fflush(stdout); } +static double +l_in(char *funame) /* function call for $channel */ +{ + int n; + char *cp; + /* get argument as integer */ + n = (int)(argument(1) + .5); + if (n != 0) /* return channel value */ + return(chanvalue(n)); + /* determine number of channels */ + if (noinput || inpfmt != NULL) + return(0); + if (nbicols) + return(nbicols); + cp = inpbuf; /* need to count */ + for (n = 0; *cp; ) + if (blnkeq && isspace(sepchar)) { + while (isspace(*cp)) + cp++; + n += *cp != '\0'; + while (*cp && !isspace(*cp)) + cp++; + } else { + n += *cp != '\n'; + while (*cp && *cp++ != sepchar) + ; + } + return(n); +} + double -chanvalue(n) /* return value for column n */ -int n; +chanvalue( /* return value for column n */ +int n +) { - int i; - register char *cp; + int i; + char *cp; if (noinput || inpfmt != NULL) { eputs("no column input\n"); @@ -291,15 +431,13 @@ int n; eputs("illegal channel number\n"); quit(1); } - if (nbicols > 0) { + if (nbicols) { if (n > nbicols) return(0.0); - cp = inpbuf + (n-1)*sizeof(double); - return(*(double *)cp); - } - if (nbicols < 0) { - if (n > -nbicols) - return(0.0); + if ((itype == 'd') | (itype == 'D')) { + cp = inpbuf + (n-1)*sizeof(double); + return(*(double *)cp); + } cp = inpbuf + (n-1)*sizeof(float); return(*(float *)cp); } @@ -317,7 +455,7 @@ int n; while (*cp && *cp++ != sepchar) ; - while (isspace(*cp)) /* some atof()'s don't like tabs */ + while (isspace(*cp)) /* some atof()'s don't like tabs */ cp++; if (n <= MAXCOL) { @@ -329,11 +467,12 @@ int n; void -chanset(n, v) /* output column n */ -int n; -double v; +chanset( /* output column n */ +int n, +double v +) { - if (colpos == 0) /* no leading separator */ + if (colpos == 0) /* no leading separator */ colpos = 1; while (colpos < n) { putchar(sepchar); @@ -344,47 +483,59 @@ double v; void -bchanset(n, v) /* output binary channel n */ -int n; -double v; +bchanset( /* output binary channel n */ +int n, +double v +) { static char zerobuf[sizeof(double)]; + const int otlen = ((otype == 'd') | (otype == 'D')) ? + sizeof(double) : sizeof(float); + float fval = v; while (++colpos < n) - fwrite(zerobuf, - bocols>0 ? sizeof(double) : sizeof(float), - 1, stdout); - if (bocols > 0) - fwrite(&v, sizeof(double), 1, stdout); - else { - float fval = v; - fwrite(&fval, sizeof(float), 1, stdout); + putbinary(zerobuf, otlen, 1, stdout); + switch (otype) { + case 'D': + swap64((char *)&v, 1); + /* fall through */ + case 'd': + putbinary(&v, sizeof(double), 1, stdout); + break; + case 'F': + swap32((char *)&fval, 1); + /* fall through */ + case 'f': + putbinary(&fval, sizeof(float), 1, stdout); + break; } } -readfmt(spec, output) /* read record format */ -char *spec; -int output; +static void +readfmt( /* read record format */ +char *spec, +int output +) { - int fd; - char *inptr; - struct field fmt; - int res; - register struct field *f; + int fd; + char *inptr; + struct field fmt; + int res; + struct field *f; /* check for inline format */ for (inptr = spec; *inptr; inptr++) if (*inptr == '$') break; - if (*inptr) /* inline */ + if (*inptr) /* inline */ inptr = spec; - else { /* from file */ + else { /* from file */ if ((fd = open(spec, 0)) == -1) { eputs(spec); eputs(": cannot open\n"); quit(1); } - res = read(fd, inpbuf+1, INBSIZ-1); + res = read(fd, inpbuf+2, INBSIZ-2); if (res <= 0 || res >= INBSIZ-1) { eputs(spec); if (res < 0) @@ -396,9 +547,9 @@ int output; quit(1); } close(fd); - (inptr=inpbuf+1)[res] = '\0'; + (inptr=inpbuf+2)[res] = '\0'; } - f = &fmt; /* get fields */ + f = &fmt; /* get fields */ while ((res = readfield(&inptr)) != F_NUL) { f->next = (struct field *)emalloc(sizeof(struct field)); f = f->next; @@ -429,13 +580,14 @@ int output; } -int -readfield(pp) /* get next field in format */ -register char **pp; +static int +readfield( /* get next field in format */ +char **pp +) { - int type = F_NUL; - int width = 0; - register char *cp; + int type = F_NUL; + int width = 0; + char *cp; cp = inpbuf; while (cp < &inpbuf[INBSIZ-1] && **pp != '\0') { @@ -496,10 +648,11 @@ register char **pp; struct strvar * -getsvar(svname) /* get string variable */ -char *svname; +getsvar( /* get string variable */ +char *svname +) { - register struct strvar *sv; + struct strvar *sv; for (sv = svhead; sv != NULL; sv = sv->next) if (!strcmp(sv->name, svname)) @@ -513,11 +666,13 @@ char *svname; } -svpreset(eqn) /* preset a string variable */ -char *eqn; +static void +svpreset( /* preset a string variable */ +char *eqn +) { - register struct strvar *sv; - register char *val; + struct strvar *sv; + char *val; for (val = eqn; *val != '='; val++) if (!*val) @@ -533,9 +688,10 @@ char *eqn; } -clearrec() /* clear input record variables */ +static void +clearrec(void) /* clear input record variables */ { - register struct field *f; + struct field *f; for (f = inpfmt; f != NULL; f = f->next) switch (f->type & F_TYP) { @@ -552,47 +708,50 @@ clearrec() /* clear input record variables */ } -getrec() /* get next record from file */ +static int +getrec(void) /* get next record from file */ { - int eatline; - register struct field *f; - + int eatline; + struct field *f; + while (ipb.chr != EOF) { - eatline = !igneol && ipb.chr != '\n'; - if (blnkeq) /* beware of nbsynch() */ + if (blnkeq) { /* beware of nbsynch() */ while (isblnk(ipb.chr)) - scaninp(); + skipinp(); + if (ipb.chr == EOF) + return(0); + } + eatline = !igneol & (ipb.chr != '\n'); clearrec(); /* start with fresh record */ for (f = inpfmt; f != NULL; f = f->next) - if (getfield(f) == -1) + if (!getfield(f)) break; - if (f == NULL) { - advinp(); + if (f == NULL) /* got one? */ return(1); - } - resetinp(); - if (eatline) { /* eat rest of line */ + skipinp(); /* else eat false start */ + if (eatline) { /* eat rest of line */ while (ipb.chr != '\n') { if (ipb.chr == EOF) return(0); - scaninp(); + skipinp(); } - scaninp(); - advinp(); + skipinp(); } } return(0); } -getfield(f) /* get next field */ -register struct field *f; +static int +getfield( /* get next field */ +struct field *f +) { - static char buf[MAXWORD+1]; /* no recursion! */ - int delim, inword; - double d; - char *np; - register char *cp; + static char buf[RMAXWORD+1]; /* no recursion! */ + int delim, inword; + double d; + char *np; + char *cp; switch (f->type & F_TYP) { case T_LIT: @@ -600,7 +759,7 @@ register struct field *f; do { if (blnkeq && isblnk(*cp)) { if (!isblnk(ipb.chr)) - return(-1); + return(0); do cp++; while (isblnk(*cp)); @@ -611,9 +770,9 @@ register struct field *f; cp++; scaninp(); } else - return(-1); + return(0); } while (*cp); - return(0); + break; case T_STR: if (f->next == NULL || (f->next->type & F_TYP) != T_LIT) delim = EOF; @@ -621,7 +780,7 @@ register struct field *f; delim = f->next->f.sl[0]; cp = buf; do { - if (ipb.chr == EOF) + if ((ipb.chr == EOF) | (ipb.chr == '\n')) inword = 0; else if (blnkeq && delim != EOF) inword = isblnk(delim) ? @@ -633,13 +792,13 @@ register struct field *f; *cp++ = ipb.chr; scaninp(); } - } while (inword && cp < &buf[MAXWORD]); + } while (inword && cp < &buf[RMAXWORD]); *cp = '\0'; if (f->f.sv->val == NULL) f->f.sv->val = savqstr(buf); /* first setting */ else if (strcmp(f->f.sv->val, buf)) - return(-1); /* doesn't match! */ - return(0); + return(0); /* doesn't match! */ + break; case T_NUM: if (f->next == NULL || (f->next->type & F_TYP) != T_LIT) delim = EOF; @@ -662,29 +821,32 @@ register struct field *f; *cp++ = ipb.chr; scaninp(); } - } while (inword && cp < &buf[MAXWORD]); + } while (inword && cp < &buf[RMAXWORD]); *cp = '\0'; d = np==NULL ? 0. : atof(np); if (!vardefined(f->f.nv)) varset(f->f.nv, '=', d); /* first setting */ else if ((d = (varvalue(f->f.nv)-d)/(d==0.?1.:d)) > .001 || d < -.001) - return(-1); /* doesn't match! */ - return(0); + return(0); /* doesn't match! */ + break; } + return(1); /* success! */ } -putrec() /* output a record */ +static void +putrec(void) /* output a record */ { - char fmt[32]; - register int n; - register struct field *f; - int adlast, adnext; + char fmt[32], typ[16]; + int n; + struct field *f; + int adlast, adnext; + double dv, av; adlast = 0; for (f = outfmt; f != NULL; f = f->next) { - adnext = blnkeq && + adnext = blnkeq && f->next != NULL && !( (f->next->type&F_TYP) == T_LIT && f->next->f.sl[0] == ' ' ); @@ -711,13 +873,24 @@ putrec() /* output a re break; case T_NUM: n = f->type & F_WID; + dv = evalue(f->f.ne); + av = fabs(dv); + if (n <= 9) + strcpy(typ, "g"); + else + sprintf(typ, ".%de", n-5); + if (av < 1L<<31) { + long iv = (int)(av + .5); + if (iv && fabs(av-iv) <= av*1e-14) + strcpy(typ, ".0f"); + } if (adlast && adnext) - strcpy(fmt, "%g"); + sprintf(fmt, "%%%s", typ); else if (adlast) - sprintf(fmt, "%%-%dg", n); + sprintf(fmt, "%%-%d%s", n, typ); else - sprintf(fmt, "%%%dg", n); - printf(fmt, evalue(f->f.ne)); + sprintf(fmt, "%%%d%s", n, typ); + printf(fmt, dv); adlast = 1; break; } @@ -725,24 +898,26 @@ putrec() /* output a re } -initinp(fp) /* prepare lookahead buffer */ -FILE *fp; +static void +initinp(FILE *fp) /* prepare lookahead buffer */ + { ipb.fin = fp; ipb.beg = ipb.end = inpbuf; - ipb.pos = inpbuf-1; /* position before beginning */ + ipb.pos = inpbuf-1; /* position before beginning */ ipb.chr = '\0'; scaninp(); } -scaninp() /* scan next character */ +static void +scaninp(void) /* scan next character */ { if (ipb.chr == EOF) return; if (++ipb.pos >= &inpbuf[INBSIZ]) ipb.pos = inpbuf; - if (ipb.pos == ipb.end) { /* new character */ + if (ipb.pos == ipb.end) { /* new character */ if ((ipb.chr = getc(ipb.fin)) != EOF) { *ipb.end = ipb.chr; if (++ipb.end >= &inpbuf[INBSIZ]) @@ -755,35 +930,50 @@ scaninp() /* scan next character } -advinp() /* move home to current position */ +static void +skipinp(void) /* rewind position and advance 1 */ { - ipb.beg = ipb.pos; -} - - -resetinp() /* rewind position and advance 1 */ -{ - if (ipb.beg == NULL) /* full */ + if (ipb.beg == NULL) /* can't fully rewind? */ ipb.beg = ipb.end; ipb.pos = ipb.beg; ipb.chr = *ipb.pos; + if (passive) /* transmit unmatched character? */ + putchar(ipb.chr); if (++ipb.beg >= &inpbuf[INBSIZ]) ipb.beg = inpbuf; scaninp(); } +static void +advinp(int skip) /* advance home to current position */ +{ + if (!skip | (passive >= 0)) { + ipb.beg = ipb.pos; /* no need to copy input */ + return; + } + if (ipb.beg == NULL) { /* buffer overflowed a bit? */ + wputs("buffer overflow\n"); + puts("\n*** MISSING DATA ***"); + ipb.beg = ipb.end; + } + while (ipb.beg != ipb.pos) { /* copy buffer to current */ + putchar(*ipb.beg); + if (++ipb.beg >= &inpbuf[INBSIZ]) + ipb.beg = inpbuf; + } +} + + void -eputs(msg) -char *msg; +eputs(const char *msg) { fputs(msg, stderr); } void -wputs(msg) -char *msg; +wputs(const char *msg) { if (!nowarn) eputs(msg); @@ -791,8 +981,7 @@ char *msg; void -quit(code) -int code; +quit(int code) { exit(code); }