--- ray/src/cal/rcalc.c 2003/02/22 02:07:20 1.1 +++ ray/src/cal/rcalc.c 2004/12/10 05:52:14 1.16 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rcalc.c,v 1.1 2003/02/22 02:07:20 greg Exp $"; +static const char RCSid[] = "$Id: rcalc.c,v 1.16 2004/12/10 05:52:14 greg Exp $"; #endif /* * rcalc.c - record calculator program. @@ -7,26 +7,25 @@ static const char RCSid[] = "$Id: rcalc.c,v 1.1 2003/0 * 9/11/87 */ -#include - #include - +#include +#include +#include #include - #include +#include "platform.h" +#include "rterror.h" +#include "rtmisc.h" +#include "rtio.h" #include "calcomp.h" -#ifdef CPM -#define getc agetc /* text files only, right? */ -#endif - #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 INBSIZ 16384 /* longest record */ #define MAXCOL 32 /* number of columns recorded */ /* field type specifications */ @@ -58,8 +57,20 @@ struct field { /* record format struc #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), advinp(void), resetinp(void); +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 */ @@ -67,8 +78,11 @@ struct strvar *svhead = NULL; /* string variables */ 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 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 */ @@ -86,18 +100,22 @@ struct { } ipb; /* circular lookahead buffer */ -main(argc, argv) -int argc; -char *argv[]; +int +main( +int argc, +char *argv[] +) { int i; - esupport |= (E_VARIABLE|E_FUNCTION|E_INCHAN|E_OUTCHAN|E_RCONST); + esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_OUTCHAN|E_RCONST; + esupport &= ~(E_REDEFW); #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]) { @@ -107,6 +125,9 @@ char *argv[]; case 'l': igneol = !igneol; break; + case 'p': + passive = !passive; + break; case 't': sepchar = argv[i][2]; break; @@ -123,10 +144,56 @@ char *argv[]; noinput = 1; break; case 'i': - readfmt(argv[++i], 0); + switch (argv[i][2]) { + case '\0': + nbicols = 0; + readfmt(argv[++i], 0); + break; + case 'a': + nbicols = 0; + break; + case 'd': + 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': + if (isdigit(argv[i][3])) + nbicols = -atoi(argv[i]+3); + else + nbicols = -1; + if (-nbicols*sizeof(float) > INBSIZ) { + eputs(argv[0]); + eputs(": too many input columns\n"); + quit(1); + } + break; + default: + goto userr; + } break; case 'o': - readfmt(argv[++i], 1); + switch (argv[i][2]) { + case '\0': + bocols = 0; + readfmt(argv[++i], 1); + break; + case 'a': + bocols = 0; + break; + case 'd': + bocols = 1; + break; + case 'f': + bocols = -1; + break; + } break; case 'w': nowarn = !nowarn; @@ -134,10 +201,11 @@ char *argv[]; case 'u': unbuff = !unbuff; break; - default: + default:; + 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][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n"); quit(1); } @@ -157,10 +225,12 @@ eputs(" [-b][-l][-n][-w][-u][-tS][-s svar=sval][-e exp 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; @@ -171,9 +241,28 @@ nbsynch() /* non-blank starting synch ch } -execute(file) /* process a file */ -char *file; +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); + return(fgets(inpbuf, INBSIZ, fp) != NULL); +} + + +static void +execute( /* process a file */ +char *file +) +{ int conditional = vardefined("cond"); long nrecs = 0; long nout = 0; @@ -188,8 +277,8 @@ char *file; } if (inpfmt != NULL) initinp(fp); - - while (inpfmt != NULL ? getrec() : fgets(inpbuf, INBSIZ, fp) != NULL) { + + while (getinputrec(fp)) { varset("recno", '=', (double)++nrecs); colflg = 0; eclock++; @@ -202,25 +291,60 @@ char *file; } -putout() /* produce an output record */ +static void +putout(void) /* produce an output record */ { - extern int chanset(); colpos = 0; if (outfmt != NULL) putrec(); + else if (bocols) + chanout(bchanset); else chanout(chanset); - if (colpos) + if (colpos && !bocols) putchar('\n'); if (unbuff) fflush(stdout); } +static double +l_in(char *funame) /* function call for $channel */ +{ + int n; + register 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 > 0) + return(nbicols); + if (nbicols < 0) + 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; @@ -233,6 +357,18 @@ int n; eputs("illegal channel number\n"); quit(1); } + if (nbicols > 0) { + if (n > nbicols) + return(0.0); + cp = inpbuf + (n-1)*sizeof(double); + return(*(double *)cp); + } + if (nbicols < 0) { + if (n > -nbicols) + return(0.0); + cp = inpbuf + (n-1)*sizeof(float); + return(*(float *)cp); + } if (n <= MAXCOL && colflg & 1L<<(n-1)) return(colval[n-1]); @@ -258,9 +394,11 @@ int n; } -chanset(n, v) /* output column n */ -int n; -double v; +void +chanset( /* output column n */ +int n, +double v +) { if (colpos == 0) /* no leading separator */ colpos = 1; @@ -272,10 +410,33 @@ double v; } -readfmt(spec, output) /* read record format */ -char *spec; -int output; +void +bchanset( /* output binary channel n */ +int n, +double v +) { + static char zerobuf[sizeof(double)]; + + 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); + } +} + + +static void +readfmt( /* read record format */ +char *spec, +int output +) +{ int fd; char *inptr; struct field fmt; @@ -293,7 +454,7 @@ int output; 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) @@ -305,7 +466,7 @@ int output; quit(1); } close(fd); - (inptr=inpbuf+1)[res] = '\0'; + (inptr=inpbuf+2)[res] = '\0'; } f = &fmt; /* get fields */ while ((res = readfield(&inptr)) != F_NUL) { @@ -338,9 +499,10 @@ int output; } -int -readfield(pp) /* get next field in format */ -register char **pp; +static int +readfield( /* get next field in format */ +register char **pp +) { int type = F_NUL; int width = 0; @@ -405,8 +567,9 @@ register char **pp; struct strvar * -getsvar(svname) /* get string variable */ -char *svname; +getsvar( /* get string variable */ +char *svname +) { register struct strvar *sv; @@ -422,8 +585,10 @@ 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; @@ -442,7 +607,8 @@ char *eqn; } -clearrec() /* clear input record variables */ +static void +clearrec(void) /* clear input record variables */ { register struct field *f; @@ -461,43 +627,45 @@ 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; - + while (ipb.chr != EOF) { - eatline = !igneol && ipb.chr != '\n'; if (blnkeq) /* beware of nbsynch() */ while (isblnk(ipb.chr)) - scaninp(); + resetinp(); + eatline = (!igneol && ipb.chr != '\n'); clearrec(); /* start with fresh record */ for (f = inpfmt; f != NULL; f = f->next) if (getfield(f) == -1) break; if (f == NULL) { - advinp(); + advinp(); /* got one! */ return(1); } - resetinp(); + resetinp(); /* eat false start */ if (eatline) { /* eat rest of line */ while (ipb.chr != '\n') { if (ipb.chr == EOF) return(0); - scaninp(); + resetinp(); } - scaninp(); - advinp(); + resetinp(); } } return(0); } -getfield(f) /* get next field */ -register struct field *f; +static int +getfield( /* get next field */ +register struct field *f +) { - static char buf[MAXWORD+1]; /* no recursion! */ + static char buf[RMAXWORD+1]; /* no recursion! */ int delim, inword; double d; char *np; @@ -530,7 +698,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) ? @@ -542,7 +710,7 @@ 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 */ @@ -571,7 +739,7 @@ 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)) @@ -581,10 +749,12 @@ register struct field *f; return(-1); /* doesn't match! */ return(0); } + return -1; /* pro forma return */ } -putrec() /* output a record */ +static void +putrec(void) /* output a record */ { char fmt[32]; register int n; @@ -634,8 +804,9 @@ 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; @@ -645,7 +816,8 @@ FILE *fp; } -scaninp() /* scan next character */ +static void +scaninp(void) /* scan next character */ { if (ipb.chr == EOF) return; @@ -664,18 +836,22 @@ scaninp() /* scan next character } -advinp() /* move home to current position */ +static void +advinp(void) /* move home to current position */ { ipb.beg = ipb.pos; } -resetinp() /* rewind position and advance 1 */ +static void +resetinp(void) /* rewind position and advance 1 */ { if (ipb.beg == NULL) /* full */ ipb.beg = ipb.end; ipb.pos = ipb.beg; ipb.chr = *ipb.pos; + if (passive) /* transmit unmatched character? */ + fputc(ipb.chr, stdout); if (++ipb.beg >= &inpbuf[INBSIZ]) ipb.beg = inpbuf; scaninp(); @@ -683,16 +859,14 @@ resetinp() /* rewind position and void -eputs(msg) -char *msg; +eputs(char *msg) { fputs(msg, stderr); } void -wputs(msg) -char *msg; +wputs(char *msg) { if (!nowarn) eputs(msg); @@ -700,8 +874,7 @@ char *msg; void -quit(code) -int code; +quit(int code) { exit(code); }