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 |
|
|
19 |
|
#ifdef CPM |
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 void scaninp(void), advinp(void), resetinp(void); |
61 |
> |
static void putrec(void), putout(void), nbsynch(void); |
62 |
> |
static int getrec(void); |
63 |
> |
static void execute(char *file); |
64 |
> |
static void initinp(FILE *fp); |
65 |
> |
static void svpreset(char *eqn); |
66 |
> |
static void readfmt(char *spec, int output); |
67 |
> |
static int readfield(char **pp); |
68 |
> |
static int getfield(struct field *f); |
69 |
> |
static void chanset(int n, double v); |
70 |
> |
static void bchanset(int n, double v); |
71 |
> |
static struct strvar* getsvar(char *svname); |
72 |
|
|
73 |
|
struct field *inpfmt = NULL; /* input record format */ |
74 |
|
struct field *outfmt = NULL; /* output record structure */ |
78 |
|
int igneol = 0; /* ignore end of line? */ |
79 |
|
char sepchar = '\t'; /* input/output separator */ |
80 |
|
int noinput = 0; /* no input records? */ |
81 |
+ |
int nbicols = 0; /* number of binary input columns */ |
82 |
+ |
int bocols = 0; /* produce binary output columns */ |
83 |
|
char inpbuf[INBSIZ]; /* input buffer */ |
84 |
|
double colval[MAXCOL]; /* input column values */ |
85 |
|
unsigned long colflg = 0; /* column retrieved flags */ |
97 |
|
} ipb; /* circular lookahead buffer */ |
98 |
|
|
99 |
|
|
100 |
< |
main(argc, argv) |
101 |
< |
int argc; |
102 |
< |
char *argv[]; |
100 |
> |
int |
101 |
> |
main( |
102 |
> |
int argc, |
103 |
> |
char *argv[] |
104 |
> |
) |
105 |
|
{ |
106 |
|
int i; |
107 |
|
|
108 |
< |
esupport |= (E_VARIABLE|E_FUNCTION|E_INCHAN|E_OUTCHAN|E_RCONST); |
108 |
> |
esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_OUTCHAN|E_RCONST; |
109 |
> |
esupport &= ~(E_REDEFW); |
110 |
|
|
111 |
|
#ifdef BIGGERLIB |
112 |
|
biggerlib(); |
137 |
|
noinput = 1; |
138 |
|
break; |
139 |
|
case 'i': |
140 |
< |
readfmt(argv[++i], 0); |
140 |
> |
switch (argv[i][2]) { |
141 |
> |
case '\0': |
142 |
> |
nbicols = 0; |
143 |
> |
readfmt(argv[++i], 0); |
144 |
> |
break; |
145 |
> |
case 'a': |
146 |
> |
nbicols = 0; |
147 |
> |
break; |
148 |
> |
case 'd': |
149 |
> |
if (isdigit(argv[i][3])) |
150 |
> |
nbicols = atoi(argv[i]+3); |
151 |
> |
else |
152 |
> |
nbicols = 1; |
153 |
> |
break; |
154 |
> |
case 'f': |
155 |
> |
if (isdigit(argv[i][3])) |
156 |
> |
nbicols = -atoi(argv[i]+3); |
157 |
> |
else |
158 |
> |
nbicols = -1; |
159 |
> |
break; |
160 |
> |
default: |
161 |
> |
goto userr; |
162 |
> |
} |
163 |
|
break; |
164 |
|
case 'o': |
165 |
< |
readfmt(argv[++i], 1); |
165 |
> |
switch (argv[i][2]) { |
166 |
> |
case '\0': |
167 |
> |
bocols = 0; |
168 |
> |
readfmt(argv[++i], 1); |
169 |
> |
break; |
170 |
> |
case 'a': |
171 |
> |
bocols = 0; |
172 |
> |
break; |
173 |
> |
case 'd': |
174 |
> |
bocols = 1; |
175 |
> |
break; |
176 |
> |
case 'f': |
177 |
> |
bocols = -1; |
178 |
> |
break; |
179 |
> |
} |
180 |
|
break; |
181 |
|
case 'w': |
182 |
|
nowarn = !nowarn; |
184 |
|
case 'u': |
185 |
|
unbuff = !unbuff; |
186 |
|
break; |
187 |
< |
default: |
187 |
> |
default:; |
188 |
> |
userr: |
189 |
|
eputs("Usage: "); |
190 |
|
eputs(argv[0]); |
191 |
|
eputs(" [-b][-l][-n][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n"); |
211 |
|
} |
212 |
|
|
213 |
|
|
214 |
< |
nbsynch() /* non-blank starting synch character */ |
214 |
> |
static void |
215 |
> |
nbsynch(void) /* non-blank starting synch character */ |
216 |
|
{ |
217 |
|
if (inpfmt == NULL || (inpfmt->type & F_TYP) != T_LIT) |
218 |
|
return; |
223 |
|
} |
224 |
|
|
225 |
|
|
226 |
< |
execute(file) /* process a file */ |
227 |
< |
char *file; |
226 |
> |
int |
227 |
> |
getinputrec( /* get next input record */ |
228 |
> |
FILE *fp |
229 |
> |
) |
230 |
|
{ |
231 |
+ |
if (inpfmt != NULL) |
232 |
+ |
return(getrec()); |
233 |
+ |
if (nbicols > 0) |
234 |
+ |
return(fread(inpbuf, sizeof(double), |
235 |
+ |
nbicols, fp) == nbicols); |
236 |
+ |
if (nbicols < 0) |
237 |
+ |
return(fread(inpbuf, sizeof(float), |
238 |
+ |
-nbicols, fp) == -nbicols); |
239 |
+ |
return(fgets(inpbuf, INBSIZ, fp) != NULL); |
240 |
+ |
} |
241 |
+ |
|
242 |
+ |
|
243 |
+ |
static void |
244 |
+ |
execute( /* process a file */ |
245 |
+ |
char *file |
246 |
+ |
) |
247 |
+ |
{ |
248 |
|
int conditional = vardefined("cond"); |
249 |
|
long nrecs = 0; |
250 |
|
long nout = 0; |
259 |
|
} |
260 |
|
if (inpfmt != NULL) |
261 |
|
initinp(fp); |
262 |
< |
|
263 |
< |
while (inpfmt != NULL ? getrec() : fgets(inpbuf, INBSIZ, fp) != NULL) { |
262 |
> |
|
263 |
> |
while (getinputrec(fp)) { |
264 |
|
varset("recno", '=', (double)++nrecs); |
265 |
|
colflg = 0; |
266 |
|
eclock++; |
273 |
|
} |
274 |
|
|
275 |
|
|
276 |
< |
putout() /* produce an output record */ |
276 |
> |
static void |
277 |
> |
putout(void) /* produce an output record */ |
278 |
|
{ |
207 |
– |
extern int chanset(); |
279 |
|
|
280 |
|
colpos = 0; |
281 |
|
if (outfmt != NULL) |
282 |
|
putrec(); |
283 |
+ |
else if (bocols) |
284 |
+ |
chanout(bchanset); |
285 |
|
else |
286 |
|
chanout(chanset); |
287 |
< |
if (colpos) |
287 |
> |
if (colpos && !bocols) |
288 |
|
putchar('\n'); |
289 |
|
if (unbuff) |
290 |
|
fflush(stdout); |
292 |
|
|
293 |
|
|
294 |
|
double |
295 |
< |
chanvalue(n) /* return value for column n */ |
296 |
< |
int n; |
295 |
> |
chanvalue( /* return value for column n */ |
296 |
> |
int n |
297 |
> |
) |
298 |
|
{ |
299 |
|
int i; |
300 |
|
register char *cp; |
307 |
|
eputs("illegal channel number\n"); |
308 |
|
quit(1); |
309 |
|
} |
310 |
+ |
if (nbicols > 0) { |
311 |
+ |
if (n > nbicols) |
312 |
+ |
return(0.0); |
313 |
+ |
cp = inpbuf + (n-1)*sizeof(double); |
314 |
+ |
return(*(double *)cp); |
315 |
+ |
} |
316 |
+ |
if (nbicols < 0) { |
317 |
+ |
if (n > -nbicols) |
318 |
+ |
return(0.0); |
319 |
+ |
cp = inpbuf + (n-1)*sizeof(float); |
320 |
+ |
return(*(float *)cp); |
321 |
+ |
} |
322 |
|
if (n <= MAXCOL && colflg & 1L<<(n-1)) |
323 |
|
return(colval[n-1]); |
324 |
|
|
344 |
|
} |
345 |
|
|
346 |
|
|
347 |
< |
chanset(n, v) /* output column n */ |
348 |
< |
int n; |
349 |
< |
double v; |
347 |
> |
void |
348 |
> |
chanset( /* output column n */ |
349 |
> |
int n, |
350 |
> |
double v |
351 |
> |
) |
352 |
|
{ |
353 |
|
if (colpos == 0) /* no leading separator */ |
354 |
|
colpos = 1; |
360 |
|
} |
361 |
|
|
362 |
|
|
363 |
< |
readfmt(spec, output) /* read record format */ |
364 |
< |
char *spec; |
365 |
< |
int output; |
363 |
> |
void |
364 |
> |
bchanset( /* output binary channel n */ |
365 |
> |
int n, |
366 |
> |
double v |
367 |
> |
) |
368 |
|
{ |
369 |
+ |
static char zerobuf[sizeof(double)]; |
370 |
+ |
|
371 |
+ |
while (++colpos < n) |
372 |
+ |
fwrite(zerobuf, |
373 |
+ |
bocols>0 ? sizeof(double) : sizeof(float), |
374 |
+ |
1, stdout); |
375 |
+ |
if (bocols > 0) |
376 |
+ |
fwrite(&v, sizeof(double), 1, stdout); |
377 |
+ |
else { |
378 |
+ |
float fval = v; |
379 |
+ |
fwrite(&fval, sizeof(float), 1, stdout); |
380 |
+ |
} |
381 |
+ |
} |
382 |
+ |
|
383 |
+ |
|
384 |
+ |
static void |
385 |
+ |
readfmt( /* read record format */ |
386 |
+ |
char *spec, |
387 |
+ |
int output |
388 |
+ |
) |
389 |
+ |
{ |
390 |
|
int fd; |
391 |
|
char *inptr; |
392 |
|
struct field fmt; |
449 |
|
} |
450 |
|
|
451 |
|
|
452 |
< |
int |
453 |
< |
readfield(pp) /* get next field in format */ |
454 |
< |
register char **pp; |
452 |
> |
static int |
453 |
> |
readfield( /* get next field in format */ |
454 |
> |
register char **pp |
455 |
> |
) |
456 |
|
{ |
457 |
|
int type = F_NUL; |
458 |
|
int width = 0; |
517 |
|
|
518 |
|
|
519 |
|
struct strvar * |
520 |
< |
getsvar(svname) /* get string variable */ |
521 |
< |
char *svname; |
520 |
> |
getsvar( /* get string variable */ |
521 |
> |
char *svname |
522 |
> |
) |
523 |
|
{ |
524 |
|
register struct strvar *sv; |
525 |
|
|
535 |
|
} |
536 |
|
|
537 |
|
|
538 |
< |
svpreset(eqn) /* preset a string variable */ |
539 |
< |
char *eqn; |
538 |
> |
static void |
539 |
> |
svpreset( /* preset a string variable */ |
540 |
> |
char *eqn |
541 |
> |
) |
542 |
|
{ |
543 |
|
register struct strvar *sv; |
544 |
|
register char *val; |
557 |
|
} |
558 |
|
|
559 |
|
|
560 |
< |
clearrec() /* clear input record variables */ |
560 |
> |
static void |
561 |
> |
clearrec(void) /* clear input record variables */ |
562 |
|
{ |
563 |
|
register struct field *f; |
564 |
|
|
577 |
|
} |
578 |
|
|
579 |
|
|
580 |
< |
getrec() /* get next record from file */ |
580 |
> |
static int |
581 |
> |
getrec(void) /* get next record from file */ |
582 |
|
{ |
583 |
|
int eatline; |
584 |
|
register struct field *f; |
611 |
|
} |
612 |
|
|
613 |
|
|
614 |
< |
getfield(f) /* get next field */ |
615 |
< |
register struct field *f; |
614 |
> |
static int |
615 |
> |
getfield( /* get next field */ |
616 |
> |
register struct field *f |
617 |
> |
) |
618 |
|
{ |
619 |
< |
static char buf[MAXWORD+1]; /* no recursion! */ |
619 |
> |
static char buf[RMAXWORD+1]; /* no recursion! */ |
620 |
|
int delim, inword; |
621 |
|
double d; |
622 |
|
char *np; |
661 |
|
*cp++ = ipb.chr; |
662 |
|
scaninp(); |
663 |
|
} |
664 |
< |
} while (inword && cp < &buf[MAXWORD]); |
664 |
> |
} while (inword && cp < &buf[RMAXWORD]); |
665 |
|
*cp = '\0'; |
666 |
|
if (f->f.sv->val == NULL) |
667 |
|
f->f.sv->val = savqstr(buf); /* first setting */ |
690 |
|
*cp++ = ipb.chr; |
691 |
|
scaninp(); |
692 |
|
} |
693 |
< |
} while (inword && cp < &buf[MAXWORD]); |
693 |
> |
} while (inword && cp < &buf[RMAXWORD]); |
694 |
|
*cp = '\0'; |
695 |
|
d = np==NULL ? 0. : atof(np); |
696 |
|
if (!vardefined(f->f.nv)) |
700 |
|
return(-1); /* doesn't match! */ |
701 |
|
return(0); |
702 |
|
} |
703 |
+ |
return -1; /* pro forma return */ |
704 |
|
} |
705 |
|
|
706 |
|
|
707 |
< |
putrec() /* output a record */ |
707 |
> |
static void |
708 |
> |
putrec(void) /* output a record */ |
709 |
|
{ |
710 |
|
char fmt[32]; |
711 |
|
register int n; |
755 |
|
} |
756 |
|
|
757 |
|
|
758 |
< |
initinp(fp) /* prepare lookahead buffer */ |
759 |
< |
FILE *fp; |
758 |
> |
static void |
759 |
> |
initinp(FILE *fp) /* prepare lookahead buffer */ |
760 |
> |
|
761 |
|
{ |
762 |
|
ipb.fin = fp; |
763 |
|
ipb.beg = ipb.end = inpbuf; |
767 |
|
} |
768 |
|
|
769 |
|
|
770 |
< |
scaninp() /* scan next character */ |
770 |
> |
static void |
771 |
> |
scaninp(void) /* scan next character */ |
772 |
|
{ |
773 |
|
if (ipb.chr == EOF) |
774 |
|
return; |
787 |
|
} |
788 |
|
|
789 |
|
|
790 |
< |
advinp() /* move home to current position */ |
790 |
> |
static void |
791 |
> |
advinp(void) /* move home to current position */ |
792 |
|
{ |
793 |
|
ipb.beg = ipb.pos; |
794 |
|
} |
795 |
|
|
796 |
|
|
797 |
< |
resetinp() /* rewind position and advance 1 */ |
797 |
> |
static void |
798 |
> |
resetinp(void) /* rewind position and advance 1 */ |
799 |
|
{ |
800 |
|
if (ipb.beg == NULL) /* full */ |
801 |
|
ipb.beg = ipb.end; |
808 |
|
|
809 |
|
|
810 |
|
void |
811 |
< |
eputs(msg) |
687 |
< |
char *msg; |
811 |
> |
eputs(char *msg) |
812 |
|
{ |
813 |
|
fputs(msg, stderr); |
814 |
|
} |
815 |
|
|
816 |
|
|
817 |
|
void |
818 |
< |
wputs(msg) |
695 |
< |
char *msg; |
818 |
> |
wputs(char *msg) |
819 |
|
{ |
820 |
|
if (!nowarn) |
821 |
|
eputs(msg); |
823 |
|
|
824 |
|
|
825 |
|
void |
826 |
< |
quit(code) |
704 |
< |
int code; |
826 |
> |
quit(int code) |
827 |
|
{ |
828 |
|
exit(code); |
829 |
|
} |