1 |
|
#ifndef lint |
2 |
< |
static const char RCSid[] = "$Id$"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
4 |
|
/* |
5 |
|
* rcalc.c - record calculator program. |
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 */ |
78 |
|
|
79 |
|
int blnkeq = 1; /* blanks compare equal? */ |
80 |
|
int igneol = 0; /* ignore end of line? */ |
81 |
+ |
int passive = 0; /* passive mode (transmit unmatched input) */ |
82 |
|
char sepchar = '\t'; /* input/output separator */ |
83 |
|
int noinput = 0; /* no input records? */ |
84 |
+ |
int nbicols = 0; /* number of binary input columns */ |
85 |
+ |
int bocols = 0; /* produce binary output columns */ |
86 |
|
char inpbuf[INBSIZ]; /* input buffer */ |
87 |
|
double colval[MAXCOL]; /* input column values */ |
88 |
|
unsigned long colflg = 0; /* column retrieved flags */ |
100 |
|
} ipb; /* circular lookahead buffer */ |
101 |
|
|
102 |
|
|
103 |
< |
main(argc, argv) |
104 |
< |
int argc; |
105 |
< |
char *argv[]; |
103 |
> |
int |
104 |
> |
main( |
105 |
> |
int argc, |
106 |
> |
char *argv[] |
107 |
> |
) |
108 |
|
{ |
109 |
|
int i; |
110 |
|
|
111 |
< |
esupport |= (E_VARIABLE|E_FUNCTION|E_INCHAN|E_OUTCHAN|E_RCONST); |
111 |
> |
esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_OUTCHAN|E_RCONST; |
112 |
> |
esupport &= ~(E_REDEFW); |
113 |
|
|
114 |
|
#ifdef BIGGERLIB |
115 |
|
biggerlib(); |
116 |
|
#endif |
117 |
|
varset("PI", ':', 3.14159265358979323846); |
118 |
+ |
funset("in", 1, '=', &l_in); |
119 |
|
|
120 |
|
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
121 |
|
switch (argv[i][1]) { |
125 |
|
case 'l': |
126 |
|
igneol = !igneol; |
127 |
|
break; |
128 |
+ |
case 'p': |
129 |
+ |
passive = !passive; |
130 |
+ |
break; |
131 |
|
case 't': |
132 |
|
sepchar = argv[i][2]; |
133 |
|
break; |
144 |
|
noinput = 1; |
145 |
|
break; |
146 |
|
case 'i': |
147 |
< |
readfmt(argv[++i], 0); |
147 |
> |
switch (argv[i][2]) { |
148 |
> |
case '\0': |
149 |
> |
nbicols = 0; |
150 |
> |
readfmt(argv[++i], 0); |
151 |
> |
break; |
152 |
> |
case 'a': |
153 |
> |
nbicols = 0; |
154 |
> |
break; |
155 |
> |
case 'd': |
156 |
> |
if (isdigit(argv[i][3])) |
157 |
> |
nbicols = atoi(argv[i]+3); |
158 |
> |
else |
159 |
> |
nbicols = 1; |
160 |
> |
if (nbicols*sizeof(double) > INBSIZ) { |
161 |
> |
eputs(argv[0]); |
162 |
> |
eputs(": too many input columns\n"); |
163 |
> |
quit(1); |
164 |
> |
} |
165 |
> |
break; |
166 |
> |
case 'f': |
167 |
> |
if (isdigit(argv[i][3])) |
168 |
> |
nbicols = -atoi(argv[i]+3); |
169 |
> |
else |
170 |
> |
nbicols = -1; |
171 |
> |
if (-nbicols*sizeof(float) > INBSIZ) { |
172 |
> |
eputs(argv[0]); |
173 |
> |
eputs(": too many input columns\n"); |
174 |
> |
quit(1); |
175 |
> |
} |
176 |
> |
break; |
177 |
> |
default: |
178 |
> |
goto userr; |
179 |
> |
} |
180 |
|
break; |
181 |
|
case 'o': |
182 |
< |
readfmt(argv[++i], 1); |
182 |
> |
switch (argv[i][2]) { |
183 |
> |
case '\0': |
184 |
> |
bocols = 0; |
185 |
> |
readfmt(argv[++i], 1); |
186 |
> |
break; |
187 |
> |
case 'a': |
188 |
> |
bocols = 0; |
189 |
> |
break; |
190 |
> |
case 'd': |
191 |
> |
bocols = 1; |
192 |
> |
break; |
193 |
> |
case 'f': |
194 |
> |
bocols = -1; |
195 |
> |
break; |
196 |
> |
} |
197 |
|
break; |
198 |
|
case 'w': |
199 |
|
nowarn = !nowarn; |
201 |
|
case 'u': |
202 |
|
unbuff = !unbuff; |
203 |
|
break; |
204 |
< |
default: |
204 |
> |
default:; |
205 |
> |
userr: |
206 |
|
eputs("Usage: "); |
207 |
|
eputs(argv[0]); |
208 |
< |
eputs(" [-b][-l][-n][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n"); |
208 |
> |
eputs(" [-b][-l][-n][-p][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n"); |
209 |
|
quit(1); |
210 |
|
} |
211 |
|
|
225 |
|
execute(argv[i]); |
226 |
|
|
227 |
|
quit(0); |
228 |
+ |
return 0; /* pro forma return */ |
229 |
|
} |
230 |
|
|
231 |
|
|
232 |
< |
nbsynch() /* non-blank starting synch character */ |
232 |
> |
static void |
233 |
> |
nbsynch(void) /* non-blank starting synch character */ |
234 |
|
{ |
235 |
|
if (inpfmt == NULL || (inpfmt->type & F_TYP) != T_LIT) |
236 |
|
return; |
241 |
|
} |
242 |
|
|
243 |
|
|
244 |
< |
execute(file) /* process a file */ |
245 |
< |
char *file; |
244 |
> |
static int |
245 |
> |
getinputrec( /* get next input record */ |
246 |
> |
FILE *fp |
247 |
> |
) |
248 |
|
{ |
249 |
+ |
if (inpfmt != NULL) |
250 |
+ |
return(getrec()); |
251 |
+ |
if (nbicols > 0) |
252 |
+ |
return(fread(inpbuf, sizeof(double), |
253 |
+ |
nbicols, fp) == nbicols); |
254 |
+ |
if (nbicols < 0) |
255 |
+ |
return(fread(inpbuf, sizeof(float), |
256 |
+ |
-nbicols, fp) == -nbicols); |
257 |
+ |
return(fgets(inpbuf, INBSIZ, fp) != NULL); |
258 |
+ |
} |
259 |
+ |
|
260 |
+ |
|
261 |
+ |
static void |
262 |
+ |
execute( /* process a file */ |
263 |
+ |
char *file |
264 |
+ |
) |
265 |
+ |
{ |
266 |
|
int conditional = vardefined("cond"); |
267 |
|
long nrecs = 0; |
268 |
|
long nout = 0; |
277 |
|
} |
278 |
|
if (inpfmt != NULL) |
279 |
|
initinp(fp); |
280 |
< |
|
281 |
< |
while (inpfmt != NULL ? getrec() : fgets(inpbuf, INBSIZ, fp) != NULL) { |
280 |
> |
|
281 |
> |
while (getinputrec(fp)) { |
282 |
|
varset("recno", '=', (double)++nrecs); |
283 |
|
colflg = 0; |
284 |
|
eclock++; |
291 |
|
} |
292 |
|
|
293 |
|
|
294 |
< |
putout() /* produce an output record */ |
294 |
> |
static void |
295 |
> |
putout(void) /* produce an output record */ |
296 |
|
{ |
207 |
– |
extern int chanset(); |
297 |
|
|
298 |
|
colpos = 0; |
299 |
|
if (outfmt != NULL) |
300 |
|
putrec(); |
301 |
+ |
else if (bocols) |
302 |
+ |
chanout(bchanset); |
303 |
|
else |
304 |
|
chanout(chanset); |
305 |
< |
if (colpos) |
305 |
> |
if (colpos && !bocols) |
306 |
|
putchar('\n'); |
307 |
|
if (unbuff) |
308 |
|
fflush(stdout); |
309 |
|
} |
310 |
|
|
311 |
|
|
312 |
+ |
static double |
313 |
+ |
l_in(char *funame) /* function call for $channel */ |
314 |
+ |
{ |
315 |
+ |
int n; |
316 |
+ |
register char *cp; |
317 |
+ |
/* get argument as integer */ |
318 |
+ |
n = (int)(argument(1) + .5); |
319 |
+ |
if (n != 0) /* return channel value */ |
320 |
+ |
return(chanvalue(n)); |
321 |
+ |
/* determine number of channels */ |
322 |
+ |
if (noinput || inpfmt != NULL) |
323 |
+ |
return(0); |
324 |
+ |
if (nbicols > 0) |
325 |
+ |
return(nbicols); |
326 |
+ |
if (nbicols < 0) |
327 |
+ |
return(-nbicols); |
328 |
+ |
cp = inpbuf; /* need to count */ |
329 |
+ |
for (n = 0; *cp; ) |
330 |
+ |
if (blnkeq && isspace(sepchar)) { |
331 |
+ |
while (isspace(*cp)) |
332 |
+ |
cp++; |
333 |
+ |
n += *cp != '\0'; |
334 |
+ |
while (*cp && !isspace(*cp)) |
335 |
+ |
cp++; |
336 |
+ |
} else { |
337 |
+ |
n += *cp != '\n'; |
338 |
+ |
while (*cp && *cp++ != sepchar) |
339 |
+ |
; |
340 |
+ |
} |
341 |
+ |
return(n); |
342 |
+ |
} |
343 |
+ |
|
344 |
|
double |
345 |
< |
chanvalue(n) /* return value for column n */ |
346 |
< |
int n; |
345 |
> |
chanvalue( /* return value for column n */ |
346 |
> |
int n |
347 |
> |
) |
348 |
|
{ |
349 |
|
int i; |
350 |
|
register char *cp; |
357 |
|
eputs("illegal channel number\n"); |
358 |
|
quit(1); |
359 |
|
} |
360 |
+ |
if (nbicols > 0) { |
361 |
+ |
if (n > nbicols) |
362 |
+ |
return(0.0); |
363 |
+ |
cp = inpbuf + (n-1)*sizeof(double); |
364 |
+ |
return(*(double *)cp); |
365 |
+ |
} |
366 |
+ |
if (nbicols < 0) { |
367 |
+ |
if (n > -nbicols) |
368 |
+ |
return(0.0); |
369 |
+ |
cp = inpbuf + (n-1)*sizeof(float); |
370 |
+ |
return(*(float *)cp); |
371 |
+ |
} |
372 |
|
if (n <= MAXCOL && colflg & 1L<<(n-1)) |
373 |
|
return(colval[n-1]); |
374 |
|
|
394 |
|
} |
395 |
|
|
396 |
|
|
397 |
< |
chanset(n, v) /* output column n */ |
398 |
< |
int n; |
399 |
< |
double v; |
397 |
> |
void |
398 |
> |
chanset( /* output column n */ |
399 |
> |
int n, |
400 |
> |
double v |
401 |
> |
) |
402 |
|
{ |
403 |
|
if (colpos == 0) /* no leading separator */ |
404 |
|
colpos = 1; |
410 |
|
} |
411 |
|
|
412 |
|
|
413 |
< |
readfmt(spec, output) /* read record format */ |
414 |
< |
char *spec; |
415 |
< |
int output; |
413 |
> |
void |
414 |
> |
bchanset( /* output binary channel n */ |
415 |
> |
int n, |
416 |
> |
double v |
417 |
> |
) |
418 |
|
{ |
419 |
+ |
static char zerobuf[sizeof(double)]; |
420 |
+ |
|
421 |
+ |
while (++colpos < n) |
422 |
+ |
fwrite(zerobuf, |
423 |
+ |
bocols>0 ? sizeof(double) : sizeof(float), |
424 |
+ |
1, stdout); |
425 |
+ |
if (bocols > 0) |
426 |
+ |
fwrite(&v, sizeof(double), 1, stdout); |
427 |
+ |
else { |
428 |
+ |
float fval = v; |
429 |
+ |
fwrite(&fval, sizeof(float), 1, stdout); |
430 |
+ |
} |
431 |
+ |
} |
432 |
+ |
|
433 |
+ |
|
434 |
+ |
static void |
435 |
+ |
readfmt( /* read record format */ |
436 |
+ |
char *spec, |
437 |
+ |
int output |
438 |
+ |
) |
439 |
+ |
{ |
440 |
|
int fd; |
441 |
|
char *inptr; |
442 |
|
struct field fmt; |
499 |
|
} |
500 |
|
|
501 |
|
|
502 |
< |
int |
503 |
< |
readfield(pp) /* get next field in format */ |
504 |
< |
register char **pp; |
502 |
> |
static int |
503 |
> |
readfield( /* get next field in format */ |
504 |
> |
register char **pp |
505 |
> |
) |
506 |
|
{ |
507 |
|
int type = F_NUL; |
508 |
|
int width = 0; |
567 |
|
|
568 |
|
|
569 |
|
struct strvar * |
570 |
< |
getsvar(svname) /* get string variable */ |
571 |
< |
char *svname; |
570 |
> |
getsvar( /* get string variable */ |
571 |
> |
char *svname |
572 |
> |
) |
573 |
|
{ |
574 |
|
register struct strvar *sv; |
575 |
|
|
585 |
|
} |
586 |
|
|
587 |
|
|
588 |
< |
svpreset(eqn) /* preset a string variable */ |
589 |
< |
char *eqn; |
588 |
> |
static void |
589 |
> |
svpreset( /* preset a string variable */ |
590 |
> |
char *eqn |
591 |
> |
) |
592 |
|
{ |
593 |
|
register struct strvar *sv; |
594 |
|
register char *val; |
607 |
|
} |
608 |
|
|
609 |
|
|
610 |
< |
clearrec() /* clear input record variables */ |
610 |
> |
static void |
611 |
> |
clearrec(void) /* clear input record variables */ |
612 |
|
{ |
613 |
|
register struct field *f; |
614 |
|
|
627 |
|
} |
628 |
|
|
629 |
|
|
630 |
< |
getrec() /* get next record from file */ |
630 |
> |
static int |
631 |
> |
getrec(void) /* get next record from file */ |
632 |
|
{ |
633 |
|
int eatline; |
634 |
|
register struct field *f; |
637 |
|
eatline = !igneol && ipb.chr != '\n'; |
638 |
|
if (blnkeq) /* beware of nbsynch() */ |
639 |
|
while (isblnk(ipb.chr)) |
640 |
< |
scaninp(); |
640 |
> |
resetinp(); |
641 |
|
clearrec(); /* start with fresh record */ |
642 |
|
for (f = inpfmt; f != NULL; f = f->next) |
643 |
|
if (getfield(f) == -1) |
644 |
|
break; |
645 |
|
if (f == NULL) { |
646 |
< |
advinp(); |
646 |
> |
advinp(); /* got one! */ |
647 |
|
return(1); |
648 |
|
} |
649 |
< |
resetinp(); |
649 |
> |
resetinp(); /* eat false start */ |
650 |
|
if (eatline) { /* eat rest of line */ |
651 |
|
while (ipb.chr != '\n') { |
652 |
|
if (ipb.chr == EOF) |
653 |
|
return(0); |
654 |
< |
scaninp(); |
654 |
> |
resetinp(); |
655 |
|
} |
656 |
< |
scaninp(); |
490 |
< |
advinp(); |
656 |
> |
resetinp(); |
657 |
|
} |
658 |
|
} |
659 |
|
return(0); |
660 |
|
} |
661 |
|
|
662 |
|
|
663 |
< |
getfield(f) /* get next field */ |
664 |
< |
register struct field *f; |
663 |
> |
static int |
664 |
> |
getfield( /* get next field */ |
665 |
> |
register struct field *f |
666 |
> |
) |
667 |
|
{ |
668 |
< |
static char buf[MAXWORD+1]; /* no recursion! */ |
668 |
> |
static char buf[RMAXWORD+1]; /* no recursion! */ |
669 |
|
int delim, inword; |
670 |
|
double d; |
671 |
|
char *np; |
698 |
|
delim = f->next->f.sl[0]; |
699 |
|
cp = buf; |
700 |
|
do { |
701 |
< |
if (ipb.chr == EOF) |
701 |
> |
if (ipb.chr == EOF || ipb.chr == '\n') |
702 |
|
inword = 0; |
703 |
|
else if (blnkeq && delim != EOF) |
704 |
|
inword = isblnk(delim) ? |
710 |
|
*cp++ = ipb.chr; |
711 |
|
scaninp(); |
712 |
|
} |
713 |
< |
} while (inword && cp < &buf[MAXWORD]); |
713 |
> |
} while (inword && cp < &buf[RMAXWORD]); |
714 |
|
*cp = '\0'; |
715 |
|
if (f->f.sv->val == NULL) |
716 |
|
f->f.sv->val = savqstr(buf); /* first setting */ |
739 |
|
*cp++ = ipb.chr; |
740 |
|
scaninp(); |
741 |
|
} |
742 |
< |
} while (inword && cp < &buf[MAXWORD]); |
742 |
> |
} while (inword && cp < &buf[RMAXWORD]); |
743 |
|
*cp = '\0'; |
744 |
|
d = np==NULL ? 0. : atof(np); |
745 |
|
if (!vardefined(f->f.nv)) |
749 |
|
return(-1); /* doesn't match! */ |
750 |
|
return(0); |
751 |
|
} |
752 |
+ |
return -1; /* pro forma return */ |
753 |
|
} |
754 |
|
|
755 |
|
|
756 |
< |
putrec() /* output a record */ |
756 |
> |
static void |
757 |
> |
putrec(void) /* output a record */ |
758 |
|
{ |
759 |
|
char fmt[32]; |
760 |
|
register int n; |
804 |
|
} |
805 |
|
|
806 |
|
|
807 |
< |
initinp(fp) /* prepare lookahead buffer */ |
808 |
< |
FILE *fp; |
807 |
> |
static void |
808 |
> |
initinp(FILE *fp) /* prepare lookahead buffer */ |
809 |
> |
|
810 |
|
{ |
811 |
|
ipb.fin = fp; |
812 |
|
ipb.beg = ipb.end = inpbuf; |
816 |
|
} |
817 |
|
|
818 |
|
|
819 |
< |
scaninp() /* scan next character */ |
819 |
> |
static void |
820 |
> |
scaninp(void) /* scan next character */ |
821 |
|
{ |
822 |
|
if (ipb.chr == EOF) |
823 |
|
return; |
836 |
|
} |
837 |
|
|
838 |
|
|
839 |
< |
advinp() /* move home to current position */ |
839 |
> |
static void |
840 |
> |
advinp(void) /* move home to current position */ |
841 |
|
{ |
842 |
|
ipb.beg = ipb.pos; |
843 |
|
} |
844 |
|
|
845 |
|
|
846 |
< |
resetinp() /* rewind position and advance 1 */ |
846 |
> |
static void |
847 |
> |
resetinp(void) /* rewind position and advance 1 */ |
848 |
|
{ |
849 |
|
if (ipb.beg == NULL) /* full */ |
850 |
|
ipb.beg = ipb.end; |
851 |
|
ipb.pos = ipb.beg; |
852 |
|
ipb.chr = *ipb.pos; |
853 |
+ |
if (passive) /* transmit unmatched character? */ |
854 |
+ |
fputc(ipb.chr, stdout); |
855 |
|
if (++ipb.beg >= &inpbuf[INBSIZ]) |
856 |
|
ipb.beg = inpbuf; |
857 |
|
scaninp(); |
859 |
|
|
860 |
|
|
861 |
|
void |
862 |
< |
eputs(msg) |
687 |
< |
char *msg; |
862 |
> |
eputs(char *msg) |
863 |
|
{ |
864 |
|
fputs(msg, stderr); |
865 |
|
} |
866 |
|
|
867 |
|
|
868 |
|
void |
869 |
< |
wputs(msg) |
695 |
< |
char *msg; |
869 |
> |
wputs(char *msg) |
870 |
|
{ |
871 |
|
if (!nowarn) |
872 |
|
eputs(msg); |
874 |
|
|
875 |
|
|
876 |
|
void |
877 |
< |
quit(code) |
704 |
< |
int code; |
877 |
> |
quit(int code) |
878 |
|
{ |
879 |
|
exit(code); |
880 |
|
} |