| 7 |
|
* 9/11/87 |
| 8 |
|
*/ |
| 9 |
|
|
| 10 |
– |
#include <stdio.h> |
| 11 |
– |
|
| 10 |
|
#include <stdlib.h> |
| 11 |
< |
|
| 11 |
> |
#include <fcntl.h> |
| 12 |
> |
#include <stdio.h> |
| 13 |
> |
#include <string.h> |
| 14 |
|
#include <math.h> |
| 15 |
– |
|
| 15 |
|
#include <ctype.h> |
| 16 |
|
|
| 17 |
+ |
#include "platform.h" |
| 18 |
+ |
#include "rterror.h" |
| 19 |
+ |
#include "rtmisc.h" |
| 20 |
+ |
#include "rtio.h" |
| 21 |
|
#include "calcomp.h" |
| 22 |
|
|
| 20 |
– |
#ifdef CPM |
| 21 |
– |
#define getc agetc /* text files only, right? */ |
| 22 |
– |
#endif |
| 23 |
– |
|
| 23 |
|
#define isnum(c) (isdigit(c) || (c)=='-' || (c)=='.' \ |
| 24 |
|
|| (c)=='+' || (c)=='e' || (c)=='E') |
| 25 |
|
|
| 57 |
|
#define savqstr(s) strcpy(emalloc(strlen(s)+1),s) |
| 58 |
|
#define freqstr(s) efree(s) |
| 59 |
|
|
| 60 |
< |
extern char *strcpy(), *emalloc(), *savestr(); |
| 61 |
< |
struct strvar *getsvar(); |
| 60 |
> |
static int getinputrec(FILE *fp); |
| 61 |
> |
static void scaninp(void), advinp(void), resetinp(void); |
| 62 |
> |
static void putrec(void), putout(void), nbsynch(void); |
| 63 |
> |
static int getrec(void); |
| 64 |
> |
static void execute(char *file); |
| 65 |
> |
static void initinp(FILE *fp); |
| 66 |
> |
static void svpreset(char *eqn); |
| 67 |
> |
static void readfmt(char *spec, int output); |
| 68 |
> |
static int readfield(char **pp); |
| 69 |
> |
static int getfield(struct field *f); |
| 70 |
> |
static void chanset(int n, double v); |
| 71 |
> |
static void bchanset(int n, double v); |
| 72 |
> |
static struct strvar* getsvar(char *svname); |
| 73 |
> |
static double l_in(char *); |
| 74 |
|
|
| 75 |
|
struct field *inpfmt = NULL; /* input record format */ |
| 76 |
|
struct field *outfmt = NULL; /* output record structure */ |
| 99 |
|
} ipb; /* circular lookahead buffer */ |
| 100 |
|
|
| 101 |
|
|
| 102 |
< |
main(argc, argv) |
| 103 |
< |
int argc; |
| 104 |
< |
char *argv[]; |
| 102 |
> |
int |
| 103 |
> |
main( |
| 104 |
> |
int argc, |
| 105 |
> |
char *argv[] |
| 106 |
> |
) |
| 107 |
|
{ |
| 108 |
|
int i; |
| 109 |
|
|
| 114 |
|
biggerlib(); |
| 115 |
|
#endif |
| 116 |
|
varset("PI", ':', 3.14159265358979323846); |
| 117 |
+ |
funset("in", 1, '=', &l_in); |
| 118 |
|
|
| 119 |
|
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
| 120 |
|
switch (argv[i][1]) { |
| 153 |
|
nbicols = atoi(argv[i]+3); |
| 154 |
|
else |
| 155 |
|
nbicols = 1; |
| 156 |
+ |
if (nbicols*sizeof(double) > INBSIZ) { |
| 157 |
+ |
eputs(argv[0]); |
| 158 |
+ |
eputs(": too many input columns\n"); |
| 159 |
+ |
quit(1); |
| 160 |
+ |
} |
| 161 |
|
break; |
| 162 |
|
case 'f': |
| 163 |
|
if (isdigit(argv[i][3])) |
| 164 |
|
nbicols = -atoi(argv[i]+3); |
| 165 |
|
else |
| 166 |
|
nbicols = -1; |
| 167 |
+ |
if (-nbicols*sizeof(float) > INBSIZ) { |
| 168 |
+ |
eputs(argv[0]); |
| 169 |
+ |
eputs(": too many input columns\n"); |
| 170 |
+ |
quit(1); |
| 171 |
+ |
} |
| 172 |
|
break; |
| 173 |
|
default: |
| 174 |
|
goto userr; |
| 221 |
|
execute(argv[i]); |
| 222 |
|
|
| 223 |
|
quit(0); |
| 224 |
+ |
return 0; /* pro forma return */ |
| 225 |
|
} |
| 226 |
|
|
| 227 |
|
|
| 228 |
< |
nbsynch() /* non-blank starting synch character */ |
| 228 |
> |
static void |
| 229 |
> |
nbsynch(void) /* non-blank starting synch character */ |
| 230 |
|
{ |
| 231 |
|
if (inpfmt == NULL || (inpfmt->type & F_TYP) != T_LIT) |
| 232 |
|
return; |
| 237 |
|
} |
| 238 |
|
|
| 239 |
|
|
| 240 |
< |
int |
| 241 |
< |
getinputrec(fp) /* get next input record */ |
| 242 |
< |
FILE *fp; |
| 240 |
> |
static int |
| 241 |
> |
getinputrec( /* get next input record */ |
| 242 |
> |
FILE *fp |
| 243 |
> |
) |
| 244 |
|
{ |
| 245 |
|
if (inpfmt != NULL) |
| 246 |
|
return(getrec()); |
| 254 |
|
} |
| 255 |
|
|
| 256 |
|
|
| 257 |
< |
execute(file) /* process a file */ |
| 258 |
< |
char *file; |
| 257 |
> |
static void |
| 258 |
> |
execute( /* process a file */ |
| 259 |
> |
char *file |
| 260 |
> |
) |
| 261 |
|
{ |
| 262 |
|
int conditional = vardefined("cond"); |
| 263 |
|
long nrecs = 0; |
| 287 |
|
} |
| 288 |
|
|
| 289 |
|
|
| 290 |
< |
putout() /* produce an output record */ |
| 290 |
> |
static void |
| 291 |
> |
putout(void) /* produce an output record */ |
| 292 |
|
{ |
| 263 |
– |
extern void chanset(), bchanset(); |
| 293 |
|
|
| 294 |
|
colpos = 0; |
| 295 |
|
if (outfmt != NULL) |
| 298 |
|
chanout(bchanset); |
| 299 |
|
else |
| 300 |
|
chanout(chanset); |
| 301 |
< |
if (colpos && !bchanset) |
| 301 |
> |
if (colpos && !bocols) |
| 302 |
|
putchar('\n'); |
| 303 |
|
if (unbuff) |
| 304 |
|
fflush(stdout); |
| 305 |
|
} |
| 306 |
|
|
| 307 |
|
|
| 308 |
+ |
static double |
| 309 |
+ |
l_in(char *funame) /* function call for $channel */ |
| 310 |
+ |
{ |
| 311 |
+ |
int n; |
| 312 |
+ |
register char *cp; |
| 313 |
+ |
/* get argument as integer */ |
| 314 |
+ |
n = (int)(argument(1) + .5); |
| 315 |
+ |
if (n != 0) /* return channel value */ |
| 316 |
+ |
return(chanvalue(n)); |
| 317 |
+ |
/* determine number of channels */ |
| 318 |
+ |
if (noinput || inpfmt != NULL) |
| 319 |
+ |
return(0); |
| 320 |
+ |
if (nbicols > 0) |
| 321 |
+ |
return(nbicols); |
| 322 |
+ |
if (nbicols < 0) |
| 323 |
+ |
return(-nbicols); |
| 324 |
+ |
cp = inpbuf; /* need to count */ |
| 325 |
+ |
for (n = 0; *cp; ) |
| 326 |
+ |
if (blnkeq && isspace(sepchar)) { |
| 327 |
+ |
while (isspace(*cp)) |
| 328 |
+ |
cp++; |
| 329 |
+ |
n += *cp != '\0'; |
| 330 |
+ |
while (*cp && !isspace(*cp)) |
| 331 |
+ |
cp++; |
| 332 |
+ |
} else { |
| 333 |
+ |
n += *cp != '\n'; |
| 334 |
+ |
while (*cp && *cp++ != sepchar) |
| 335 |
+ |
; |
| 336 |
+ |
} |
| 337 |
+ |
return(n); |
| 338 |
+ |
} |
| 339 |
+ |
|
| 340 |
|
double |
| 341 |
< |
chanvalue(n) /* return value for column n */ |
| 342 |
< |
int n; |
| 341 |
> |
chanvalue( /* return value for column n */ |
| 342 |
> |
int n |
| 343 |
> |
) |
| 344 |
|
{ |
| 345 |
|
int i; |
| 346 |
|
register char *cp; |
| 391 |
|
|
| 392 |
|
|
| 393 |
|
void |
| 394 |
< |
chanset(n, v) /* output column n */ |
| 395 |
< |
int n; |
| 396 |
< |
double v; |
| 394 |
> |
chanset( /* output column n */ |
| 395 |
> |
int n, |
| 396 |
> |
double v |
| 397 |
> |
) |
| 398 |
|
{ |
| 399 |
|
if (colpos == 0) /* no leading separator */ |
| 400 |
|
colpos = 1; |
| 407 |
|
|
| 408 |
|
|
| 409 |
|
void |
| 410 |
< |
bchanset(n, v) /* output binary channel n */ |
| 411 |
< |
int n; |
| 412 |
< |
double v; |
| 410 |
> |
bchanset( /* output binary channel n */ |
| 411 |
> |
int n, |
| 412 |
> |
double v |
| 413 |
> |
) |
| 414 |
|
{ |
| 415 |
|
static char zerobuf[sizeof(double)]; |
| 416 |
|
|
| 427 |
|
} |
| 428 |
|
|
| 429 |
|
|
| 430 |
< |
readfmt(spec, output) /* read record format */ |
| 431 |
< |
char *spec; |
| 432 |
< |
int output; |
| 430 |
> |
static void |
| 431 |
> |
readfmt( /* read record format */ |
| 432 |
> |
char *spec, |
| 433 |
> |
int output |
| 434 |
> |
) |
| 435 |
|
{ |
| 436 |
|
int fd; |
| 437 |
|
char *inptr; |
| 495 |
|
} |
| 496 |
|
|
| 497 |
|
|
| 498 |
< |
int |
| 499 |
< |
readfield(pp) /* get next field in format */ |
| 500 |
< |
register char **pp; |
| 498 |
> |
static int |
| 499 |
> |
readfield( /* get next field in format */ |
| 500 |
> |
register char **pp |
| 501 |
> |
) |
| 502 |
|
{ |
| 503 |
|
int type = F_NUL; |
| 504 |
|
int width = 0; |
| 563 |
|
|
| 564 |
|
|
| 565 |
|
struct strvar * |
| 566 |
< |
getsvar(svname) /* get string variable */ |
| 567 |
< |
char *svname; |
| 566 |
> |
getsvar( /* get string variable */ |
| 567 |
> |
char *svname |
| 568 |
> |
) |
| 569 |
|
{ |
| 570 |
|
register struct strvar *sv; |
| 571 |
|
|
| 581 |
|
} |
| 582 |
|
|
| 583 |
|
|
| 584 |
< |
svpreset(eqn) /* preset a string variable */ |
| 585 |
< |
char *eqn; |
| 584 |
> |
static void |
| 585 |
> |
svpreset( /* preset a string variable */ |
| 586 |
> |
char *eqn |
| 587 |
> |
) |
| 588 |
|
{ |
| 589 |
|
register struct strvar *sv; |
| 590 |
|
register char *val; |
| 603 |
|
} |
| 604 |
|
|
| 605 |
|
|
| 606 |
< |
clearrec() /* clear input record variables */ |
| 606 |
> |
static void |
| 607 |
> |
clearrec(void) /* clear input record variables */ |
| 608 |
|
{ |
| 609 |
|
register struct field *f; |
| 610 |
|
|
| 623 |
|
} |
| 624 |
|
|
| 625 |
|
|
| 626 |
< |
getrec() /* get next record from file */ |
| 626 |
> |
static int |
| 627 |
> |
getrec(void) /* get next record from file */ |
| 628 |
|
{ |
| 629 |
|
int eatline; |
| 630 |
|
register struct field *f; |
| 657 |
|
} |
| 658 |
|
|
| 659 |
|
|
| 660 |
< |
getfield(f) /* get next field */ |
| 661 |
< |
register struct field *f; |
| 660 |
> |
static int |
| 661 |
> |
getfield( /* get next field */ |
| 662 |
> |
register struct field *f |
| 663 |
> |
) |
| 664 |
|
{ |
| 665 |
< |
static char buf[MAXWORD+1]; /* no recursion! */ |
| 665 |
> |
static char buf[RMAXWORD+1]; /* no recursion! */ |
| 666 |
|
int delim, inword; |
| 667 |
|
double d; |
| 668 |
|
char *np; |
| 695 |
|
delim = f->next->f.sl[0]; |
| 696 |
|
cp = buf; |
| 697 |
|
do { |
| 698 |
< |
if (ipb.chr == EOF) |
| 698 |
> |
if (ipb.chr == EOF || ipb.chr == '\n') |
| 699 |
|
inword = 0; |
| 700 |
|
else if (blnkeq && delim != EOF) |
| 701 |
|
inword = isblnk(delim) ? |
| 707 |
|
*cp++ = ipb.chr; |
| 708 |
|
scaninp(); |
| 709 |
|
} |
| 710 |
< |
} while (inword && cp < &buf[MAXWORD]); |
| 710 |
> |
} while (inword && cp < &buf[RMAXWORD]); |
| 711 |
|
*cp = '\0'; |
| 712 |
|
if (f->f.sv->val == NULL) |
| 713 |
|
f->f.sv->val = savqstr(buf); /* first setting */ |
| 736 |
|
*cp++ = ipb.chr; |
| 737 |
|
scaninp(); |
| 738 |
|
} |
| 739 |
< |
} while (inword && cp < &buf[MAXWORD]); |
| 739 |
> |
} while (inword && cp < &buf[RMAXWORD]); |
| 740 |
|
*cp = '\0'; |
| 741 |
|
d = np==NULL ? 0. : atof(np); |
| 742 |
|
if (!vardefined(f->f.nv)) |
| 746 |
|
return(-1); /* doesn't match! */ |
| 747 |
|
return(0); |
| 748 |
|
} |
| 749 |
+ |
return -1; /* pro forma return */ |
| 750 |
|
} |
| 751 |
|
|
| 752 |
|
|
| 753 |
< |
putrec() /* output a record */ |
| 753 |
> |
static void |
| 754 |
> |
putrec(void) /* output a record */ |
| 755 |
|
{ |
| 756 |
|
char fmt[32]; |
| 757 |
|
register int n; |
| 801 |
|
} |
| 802 |
|
|
| 803 |
|
|
| 804 |
< |
initinp(fp) /* prepare lookahead buffer */ |
| 805 |
< |
FILE *fp; |
| 804 |
> |
static void |
| 805 |
> |
initinp(FILE *fp) /* prepare lookahead buffer */ |
| 806 |
> |
|
| 807 |
|
{ |
| 808 |
|
ipb.fin = fp; |
| 809 |
|
ipb.beg = ipb.end = inpbuf; |
| 813 |
|
} |
| 814 |
|
|
| 815 |
|
|
| 816 |
< |
scaninp() /* scan next character */ |
| 816 |
> |
static void |
| 817 |
> |
scaninp(void) /* scan next character */ |
| 818 |
|
{ |
| 819 |
|
if (ipb.chr == EOF) |
| 820 |
|
return; |
| 833 |
|
} |
| 834 |
|
|
| 835 |
|
|
| 836 |
< |
advinp() /* move home to current position */ |
| 836 |
> |
static void |
| 837 |
> |
advinp(void) /* move home to current position */ |
| 838 |
|
{ |
| 839 |
|
ipb.beg = ipb.pos; |
| 840 |
|
} |
| 841 |
|
|
| 842 |
|
|
| 843 |
< |
resetinp() /* rewind position and advance 1 */ |
| 843 |
> |
static void |
| 844 |
> |
resetinp(void) /* rewind position and advance 1 */ |
| 845 |
|
{ |
| 846 |
|
if (ipb.beg == NULL) /* full */ |
| 847 |
|
ipb.beg = ipb.end; |
| 854 |
|
|
| 855 |
|
|
| 856 |
|
void |
| 857 |
< |
eputs(msg) |
| 778 |
< |
char *msg; |
| 857 |
> |
eputs(char *msg) |
| 858 |
|
{ |
| 859 |
|
fputs(msg, stderr); |
| 860 |
|
} |
| 861 |
|
|
| 862 |
|
|
| 863 |
|
void |
| 864 |
< |
wputs(msg) |
| 786 |
< |
char *msg; |
| 864 |
> |
wputs(char *msg) |
| 865 |
|
{ |
| 866 |
|
if (!nowarn) |
| 867 |
|
eputs(msg); |
| 869 |
|
|
| 870 |
|
|
| 871 |
|
void |
| 872 |
< |
quit(code) |
| 795 |
< |
int code; |
| 872 |
> |
quit(int code) |
| 873 |
|
{ |
| 874 |
|
exit(code); |
| 875 |
|
} |