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 <stdio.h> |
12 |
> |
#include <string.h> |
13 |
|
#include <math.h> |
15 |
– |
|
14 |
|
#include <ctype.h> |
15 |
|
|
16 |
+ |
#include "platform.h" |
17 |
|
#include "calcomp.h" |
18 |
+ |
#include "rterror.h" |
19 |
|
|
20 |
– |
#ifdef CPM |
21 |
– |
#define getc agetc /* text files only, right? */ |
22 |
– |
#endif |
23 |
– |
|
20 |
|
#define isnum(c) (isdigit(c) || (c)=='-' || (c)=='.' \ |
21 |
|
|| (c)=='+' || (c)=='e' || (c)=='E') |
22 |
|
|
54 |
|
#define savqstr(s) strcpy(emalloc(strlen(s)+1),s) |
55 |
|
#define freqstr(s) efree(s) |
56 |
|
|
57 |
< |
extern char *strcpy(), *emalloc(), *savestr(); |
58 |
< |
struct strvar *getsvar(); |
57 |
> |
static void scaninp(void), advinp(void), resetinp(void); |
58 |
> |
static void putrec(void), putout(void), nbsynch(void); |
59 |
> |
static int getrec(void); |
60 |
> |
static void execute(char *file); |
61 |
> |
static void initinp(FILE *fp); |
62 |
> |
static void svpreset(char *eqn); |
63 |
> |
static void readfmt(char *spec, int output); |
64 |
> |
static int readfield(char **pp); |
65 |
> |
static int getfield(struct field *f); |
66 |
> |
static void chanset(int n, double v); |
67 |
> |
static void bchanset(int n, double v); |
68 |
> |
static struct strvar* getsvar(char *svname); |
69 |
|
|
70 |
|
struct field *inpfmt = NULL; /* input record format */ |
71 |
|
struct field *outfmt = NULL; /* output record structure */ |
75 |
|
int igneol = 0; /* ignore end of line? */ |
76 |
|
char sepchar = '\t'; /* input/output separator */ |
77 |
|
int noinput = 0; /* no input records? */ |
78 |
+ |
int nbicols = 0; /* number of binary input columns */ |
79 |
+ |
int bocols = 0; /* produce binary output columns */ |
80 |
|
char inpbuf[INBSIZ]; /* input buffer */ |
81 |
|
double colval[MAXCOL]; /* input column values */ |
82 |
|
unsigned long colflg = 0; /* column retrieved flags */ |
94 |
|
} ipb; /* circular lookahead buffer */ |
95 |
|
|
96 |
|
|
97 |
< |
main(argc, argv) |
98 |
< |
int argc; |
99 |
< |
char *argv[]; |
97 |
> |
int |
98 |
> |
main( |
99 |
> |
int argc, |
100 |
> |
char *argv[] |
101 |
> |
) |
102 |
|
{ |
103 |
|
int i; |
104 |
|
|
105 |
< |
esupport |= (E_VARIABLE|E_FUNCTION|E_INCHAN|E_OUTCHAN|E_RCONST); |
105 |
> |
esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_OUTCHAN|E_RCONST; |
106 |
> |
esupport &= ~(E_REDEFW); |
107 |
|
|
108 |
|
#ifdef BIGGERLIB |
109 |
|
biggerlib(); |
134 |
|
noinput = 1; |
135 |
|
break; |
136 |
|
case 'i': |
137 |
< |
readfmt(argv[++i], 0); |
137 |
> |
switch (argv[i][2]) { |
138 |
> |
case '\0': |
139 |
> |
nbicols = 0; |
140 |
> |
readfmt(argv[++i], 0); |
141 |
> |
break; |
142 |
> |
case 'a': |
143 |
> |
nbicols = 0; |
144 |
> |
break; |
145 |
> |
case 'd': |
146 |
> |
if (isdigit(argv[i][3])) |
147 |
> |
nbicols = atoi(argv[i]+3); |
148 |
> |
else |
149 |
> |
nbicols = 1; |
150 |
> |
break; |
151 |
> |
case 'f': |
152 |
> |
if (isdigit(argv[i][3])) |
153 |
> |
nbicols = -atoi(argv[i]+3); |
154 |
> |
else |
155 |
> |
nbicols = -1; |
156 |
> |
break; |
157 |
> |
default: |
158 |
> |
goto userr; |
159 |
> |
} |
160 |
|
break; |
161 |
|
case 'o': |
162 |
< |
readfmt(argv[++i], 1); |
162 |
> |
switch (argv[i][2]) { |
163 |
> |
case '\0': |
164 |
> |
bocols = 0; |
165 |
> |
readfmt(argv[++i], 1); |
166 |
> |
break; |
167 |
> |
case 'a': |
168 |
> |
bocols = 0; |
169 |
> |
break; |
170 |
> |
case 'd': |
171 |
> |
bocols = 1; |
172 |
> |
break; |
173 |
> |
case 'f': |
174 |
> |
bocols = -1; |
175 |
> |
break; |
176 |
> |
} |
177 |
|
break; |
178 |
|
case 'w': |
179 |
|
nowarn = !nowarn; |
181 |
|
case 'u': |
182 |
|
unbuff = !unbuff; |
183 |
|
break; |
184 |
< |
default: |
184 |
> |
default:; |
185 |
> |
userr: |
186 |
|
eputs("Usage: "); |
187 |
|
eputs(argv[0]); |
188 |
|
eputs(" [-b][-l][-n][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n"); |
208 |
|
} |
209 |
|
|
210 |
|
|
211 |
< |
nbsynch() /* non-blank starting synch character */ |
211 |
> |
static void |
212 |
> |
nbsynch(void) /* non-blank starting synch character */ |
213 |
|
{ |
214 |
|
if (inpfmt == NULL || (inpfmt->type & F_TYP) != T_LIT) |
215 |
|
return; |
220 |
|
} |
221 |
|
|
222 |
|
|
223 |
< |
execute(file) /* process a file */ |
224 |
< |
char *file; |
223 |
> |
int |
224 |
> |
getinputrec( /* get next input record */ |
225 |
> |
FILE *fp |
226 |
> |
) |
227 |
|
{ |
228 |
+ |
if (inpfmt != NULL) |
229 |
+ |
return(getrec()); |
230 |
+ |
if (nbicols > 0) |
231 |
+ |
return(fread(inpbuf, sizeof(double), |
232 |
+ |
nbicols, fp) == nbicols); |
233 |
+ |
if (nbicols < 0) |
234 |
+ |
return(fread(inpbuf, sizeof(float), |
235 |
+ |
-nbicols, fp) == -nbicols); |
236 |
+ |
return(fgets(inpbuf, INBSIZ, fp) != NULL); |
237 |
+ |
} |
238 |
+ |
|
239 |
+ |
|
240 |
+ |
static void |
241 |
+ |
execute( /* process a file */ |
242 |
+ |
char *file |
243 |
+ |
) |
244 |
+ |
{ |
245 |
|
int conditional = vardefined("cond"); |
246 |
|
long nrecs = 0; |
247 |
|
long nout = 0; |
256 |
|
} |
257 |
|
if (inpfmt != NULL) |
258 |
|
initinp(fp); |
259 |
< |
|
260 |
< |
while (inpfmt != NULL ? getrec() : fgets(inpbuf, INBSIZ, fp) != NULL) { |
259 |
> |
|
260 |
> |
while (getinputrec(fp)) { |
261 |
|
varset("recno", '=', (double)++nrecs); |
262 |
|
colflg = 0; |
263 |
|
eclock++; |
270 |
|
} |
271 |
|
|
272 |
|
|
273 |
< |
putout() /* produce an output record */ |
273 |
> |
static void |
274 |
> |
putout(void) /* produce an output record */ |
275 |
|
{ |
207 |
– |
extern int chanset(); |
276 |
|
|
277 |
|
colpos = 0; |
278 |
|
if (outfmt != NULL) |
279 |
|
putrec(); |
280 |
+ |
else if (bocols) |
281 |
+ |
chanout(bchanset); |
282 |
|
else |
283 |
|
chanout(chanset); |
284 |
< |
if (colpos) |
284 |
> |
if (colpos && !bocols) |
285 |
|
putchar('\n'); |
286 |
|
if (unbuff) |
287 |
|
fflush(stdout); |
289 |
|
|
290 |
|
|
291 |
|
double |
292 |
< |
chanvalue(n) /* return value for column n */ |
293 |
< |
int n; |
292 |
> |
chanvalue( /* return value for column n */ |
293 |
> |
int n |
294 |
> |
) |
295 |
|
{ |
296 |
|
int i; |
297 |
|
register char *cp; |
304 |
|
eputs("illegal channel number\n"); |
305 |
|
quit(1); |
306 |
|
} |
307 |
+ |
if (nbicols > 0) { |
308 |
+ |
if (n > nbicols) |
309 |
+ |
return(0.0); |
310 |
+ |
cp = inpbuf + (n-1)*sizeof(double); |
311 |
+ |
return(*(double *)cp); |
312 |
+ |
} |
313 |
+ |
if (nbicols < 0) { |
314 |
+ |
if (n > -nbicols) |
315 |
+ |
return(0.0); |
316 |
+ |
cp = inpbuf + (n-1)*sizeof(float); |
317 |
+ |
return(*(float *)cp); |
318 |
+ |
} |
319 |
|
if (n <= MAXCOL && colflg & 1L<<(n-1)) |
320 |
|
return(colval[n-1]); |
321 |
|
|
341 |
|
} |
342 |
|
|
343 |
|
|
344 |
< |
chanset(n, v) /* output column n */ |
345 |
< |
int n; |
346 |
< |
double v; |
344 |
> |
void |
345 |
> |
chanset( /* output column n */ |
346 |
> |
int n, |
347 |
> |
double v |
348 |
> |
) |
349 |
|
{ |
350 |
|
if (colpos == 0) /* no leading separator */ |
351 |
|
colpos = 1; |
357 |
|
} |
358 |
|
|
359 |
|
|
360 |
< |
readfmt(spec, output) /* read record format */ |
361 |
< |
char *spec; |
362 |
< |
int output; |
360 |
> |
void |
361 |
> |
bchanset( /* output binary channel n */ |
362 |
> |
int n, |
363 |
> |
double v |
364 |
> |
) |
365 |
|
{ |
366 |
+ |
static char zerobuf[sizeof(double)]; |
367 |
+ |
|
368 |
+ |
while (++colpos < n) |
369 |
+ |
fwrite(zerobuf, |
370 |
+ |
bocols>0 ? sizeof(double) : sizeof(float), |
371 |
+ |
1, stdout); |
372 |
+ |
if (bocols > 0) |
373 |
+ |
fwrite(&v, sizeof(double), 1, stdout); |
374 |
+ |
else { |
375 |
+ |
float fval = v; |
376 |
+ |
fwrite(&fval, sizeof(float), 1, stdout); |
377 |
+ |
} |
378 |
+ |
} |
379 |
+ |
|
380 |
+ |
|
381 |
+ |
static void |
382 |
+ |
readfmt( /* read record format */ |
383 |
+ |
char *spec, |
384 |
+ |
int output |
385 |
+ |
) |
386 |
+ |
{ |
387 |
|
int fd; |
388 |
|
char *inptr; |
389 |
|
struct field fmt; |
446 |
|
} |
447 |
|
|
448 |
|
|
449 |
< |
int |
450 |
< |
readfield(pp) /* get next field in format */ |
451 |
< |
register char **pp; |
449 |
> |
static int |
450 |
> |
readfield( /* get next field in format */ |
451 |
> |
register char **pp |
452 |
> |
) |
453 |
|
{ |
454 |
|
int type = F_NUL; |
455 |
|
int width = 0; |
514 |
|
|
515 |
|
|
516 |
|
struct strvar * |
517 |
< |
getsvar(svname) /* get string variable */ |
518 |
< |
char *svname; |
517 |
> |
getsvar( /* get string variable */ |
518 |
> |
char *svname |
519 |
> |
) |
520 |
|
{ |
521 |
|
register struct strvar *sv; |
522 |
|
|
532 |
|
} |
533 |
|
|
534 |
|
|
535 |
< |
svpreset(eqn) /* preset a string variable */ |
536 |
< |
char *eqn; |
535 |
> |
static void |
536 |
> |
svpreset( /* preset a string variable */ |
537 |
> |
char *eqn |
538 |
> |
) |
539 |
|
{ |
540 |
|
register struct strvar *sv; |
541 |
|
register char *val; |
554 |
|
} |
555 |
|
|
556 |
|
|
557 |
< |
clearrec() /* clear input record variables */ |
557 |
> |
static void |
558 |
> |
clearrec(void) /* clear input record variables */ |
559 |
|
{ |
560 |
|
register struct field *f; |
561 |
|
|
574 |
|
} |
575 |
|
|
576 |
|
|
577 |
< |
getrec() /* get next record from file */ |
577 |
> |
static int |
578 |
> |
getrec(void) /* get next record from file */ |
579 |
|
{ |
580 |
|
int eatline; |
581 |
|
register struct field *f; |
608 |
|
} |
609 |
|
|
610 |
|
|
611 |
< |
getfield(f) /* get next field */ |
612 |
< |
register struct field *f; |
611 |
> |
static int |
612 |
> |
getfield( /* get next field */ |
613 |
> |
register struct field *f |
614 |
> |
) |
615 |
|
{ |
616 |
< |
static char buf[MAXWORD+1]; /* no recursion! */ |
616 |
> |
static char buf[RMAXWORD+1]; /* no recursion! */ |
617 |
|
int delim, inword; |
618 |
|
double d; |
619 |
|
char *np; |
658 |
|
*cp++ = ipb.chr; |
659 |
|
scaninp(); |
660 |
|
} |
661 |
< |
} while (inword && cp < &buf[MAXWORD]); |
661 |
> |
} while (inword && cp < &buf[RMAXWORD]); |
662 |
|
*cp = '\0'; |
663 |
|
if (f->f.sv->val == NULL) |
664 |
|
f->f.sv->val = savqstr(buf); /* first setting */ |
687 |
|
*cp++ = ipb.chr; |
688 |
|
scaninp(); |
689 |
|
} |
690 |
< |
} while (inword && cp < &buf[MAXWORD]); |
690 |
> |
} while (inword && cp < &buf[RMAXWORD]); |
691 |
|
*cp = '\0'; |
692 |
|
d = np==NULL ? 0. : atof(np); |
693 |
|
if (!vardefined(f->f.nv)) |
697 |
|
return(-1); /* doesn't match! */ |
698 |
|
return(0); |
699 |
|
} |
700 |
+ |
return -1; /* pro forma return */ |
701 |
|
} |
702 |
|
|
703 |
|
|
704 |
< |
putrec() /* output a record */ |
704 |
> |
static void |
705 |
> |
putrec(void) /* output a record */ |
706 |
|
{ |
707 |
|
char fmt[32]; |
708 |
|
register int n; |
752 |
|
} |
753 |
|
|
754 |
|
|
755 |
< |
initinp(fp) /* prepare lookahead buffer */ |
756 |
< |
FILE *fp; |
755 |
> |
static void |
756 |
> |
initinp(FILE *fp) /* prepare lookahead buffer */ |
757 |
> |
|
758 |
|
{ |
759 |
|
ipb.fin = fp; |
760 |
|
ipb.beg = ipb.end = inpbuf; |
764 |
|
} |
765 |
|
|
766 |
|
|
767 |
< |
scaninp() /* scan next character */ |
767 |
> |
static void |
768 |
> |
scaninp(void) /* scan next character */ |
769 |
|
{ |
770 |
|
if (ipb.chr == EOF) |
771 |
|
return; |
784 |
|
} |
785 |
|
|
786 |
|
|
787 |
< |
advinp() /* move home to current position */ |
787 |
> |
static void |
788 |
> |
advinp(void) /* move home to current position */ |
789 |
|
{ |
790 |
|
ipb.beg = ipb.pos; |
791 |
|
} |
792 |
|
|
793 |
|
|
794 |
< |
resetinp() /* rewind position and advance 1 */ |
794 |
> |
static void |
795 |
> |
resetinp(void) /* rewind position and advance 1 */ |
796 |
|
{ |
797 |
|
if (ipb.beg == NULL) /* full */ |
798 |
|
ipb.beg = ipb.end; |
805 |
|
|
806 |
|
|
807 |
|
void |
808 |
< |
eputs(msg) |
687 |
< |
char *msg; |
808 |
> |
eputs(char *msg) |
809 |
|
{ |
810 |
|
fputs(msg, stderr); |
811 |
|
} |
812 |
|
|
813 |
|
|
814 |
|
void |
815 |
< |
wputs(msg) |
695 |
< |
char *msg; |
815 |
> |
wputs(char *msg) |
816 |
|
{ |
817 |
|
if (!nowarn) |
818 |
|
eputs(msg); |
820 |
|
|
821 |
|
|
822 |
|
void |
823 |
< |
quit(code) |
704 |
< |
int code; |
823 |
> |
quit(int code) |
824 |
|
{ |
825 |
|
exit(code); |
826 |
|
} |