--- ray/src/cal/rcalc.c 2003/06/08 12:03:09 1.5 +++ ray/src/cal/rcalc.c 2003/11/14 17:31:24 1.11 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rcalc.c,v 1.5 2003/06/08 12:03:09 schorsch Exp $"; +static const char RCSid[] = "$Id: rcalc.c,v 1.11 2003/11/14 17:31:24 schorsch Exp $"; #endif /* * rcalc.c - record calculator program. @@ -8,6 +8,7 @@ static const char RCSid[] = "$Id: rcalc.c,v 1.5 2003/0 */ #include +#include #include #include #include @@ -15,11 +16,8 @@ static const char RCSid[] = "$Id: rcalc.c,v 1.5 2003/0 #include "platform.h" #include "calcomp.h" +#include "rterror.h" -#ifdef CPM -#define getc agetc /* text files only, right? */ -#endif - #define isnum(c) (isdigit(c) || (c)=='-' || (c)=='.' \ || (c)=='+' || (c)=='e' || (c)=='E') @@ -57,6 +55,7 @@ struct field { /* record format struc #define savqstr(s) strcpy(emalloc(strlen(s)+1),s) #define freqstr(s) efree(s) +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); @@ -69,6 +68,7 @@ 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 */ @@ -112,6 +112,7 @@ char *argv[] biggerlib(); #endif varset("PI", ':', 3.14159265358979323846); + funset("in", 1, '=', &l_in); for (i = 1; i < argc && argv[i][0] == '-'; i++) switch (argv[i][1]) { @@ -150,12 +151,22 @@ char *argv[] 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; @@ -208,6 +219,7 @@ eputs(" [-b][-l][-n][-w][-u][-tS][-s svar=sval][-e exp execute(argv[i]); quit(0); + return 0; /* pro forma return */ } @@ -223,7 +235,7 @@ nbsynch(void) /* non-blank starting sync } -int +static int getinputrec( /* get next input record */ FILE *fp ) @@ -291,6 +303,38 @@ putout(void) /* produce an output recor } +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( /* return value for column n */ int n @@ -616,7 +660,7 @@ getfield( /* get next fiel 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; @@ -649,7 +693,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) ? @@ -661,7 +705,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 */ @@ -690,7 +734,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))