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 |
|
|
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 |
> |
if (nbicols*sizeof(double) > INBSIZ) { |
151 |
> |
eputs(argv[0]); |
152 |
> |
eputs(": too many input columns\n"); |
153 |
> |
quit(1); |
154 |
> |
} |
155 |
> |
break; |
156 |
> |
case 'f': |
157 |
> |
if (isdigit(argv[i][3])) |
158 |
> |
nbicols = -atoi(argv[i]+3); |
159 |
> |
else |
160 |
> |
nbicols = -1; |
161 |
> |
if (-nbicols*sizeof(float) > INBSIZ) { |
162 |
> |
eputs(argv[0]); |
163 |
> |
eputs(": too many input columns\n"); |
164 |
> |
quit(1); |
165 |
> |
} |
166 |
> |
break; |
167 |
> |
default: |
168 |
> |
goto userr; |
169 |
> |
} |
170 |
|
break; |
171 |
|
case 'o': |
172 |
< |
readfmt(argv[++i], 1); |
172 |
> |
switch (argv[i][2]) { |
173 |
> |
case '\0': |
174 |
> |
bocols = 0; |
175 |
> |
readfmt(argv[++i], 1); |
176 |
> |
break; |
177 |
> |
case 'a': |
178 |
> |
bocols = 0; |
179 |
> |
break; |
180 |
> |
case 'd': |
181 |
> |
bocols = 1; |
182 |
> |
break; |
183 |
> |
case 'f': |
184 |
> |
bocols = -1; |
185 |
> |
break; |
186 |
> |
} |
187 |
|
break; |
188 |
|
case 'w': |
189 |
|
nowarn = !nowarn; |
191 |
|
case 'u': |
192 |
|
unbuff = !unbuff; |
193 |
|
break; |
194 |
< |
default: |
194 |
> |
default:; |
195 |
> |
userr: |
196 |
|
eputs("Usage: "); |
197 |
|
eputs(argv[0]); |
198 |
|
eputs(" [-b][-l][-n][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n"); |
218 |
|
} |
219 |
|
|
220 |
|
|
221 |
< |
nbsynch() /* non-blank starting synch character */ |
221 |
> |
static void |
222 |
> |
nbsynch(void) /* non-blank starting synch character */ |
223 |
|
{ |
224 |
|
if (inpfmt == NULL || (inpfmt->type & F_TYP) != T_LIT) |
225 |
|
return; |
230 |
|
} |
231 |
|
|
232 |
|
|
233 |
< |
execute(file) /* process a file */ |
234 |
< |
char *file; |
233 |
> |
int |
234 |
> |
getinputrec( /* get next input record */ |
235 |
> |
FILE *fp |
236 |
> |
) |
237 |
|
{ |
238 |
+ |
if (inpfmt != NULL) |
239 |
+ |
return(getrec()); |
240 |
+ |
if (nbicols > 0) |
241 |
+ |
return(fread(inpbuf, sizeof(double), |
242 |
+ |
nbicols, fp) == nbicols); |
243 |
+ |
if (nbicols < 0) |
244 |
+ |
return(fread(inpbuf, sizeof(float), |
245 |
+ |
-nbicols, fp) == -nbicols); |
246 |
+ |
return(fgets(inpbuf, INBSIZ, fp) != NULL); |
247 |
+ |
} |
248 |
+ |
|
249 |
+ |
|
250 |
+ |
static void |
251 |
+ |
execute( /* process a file */ |
252 |
+ |
char *file |
253 |
+ |
) |
254 |
+ |
{ |
255 |
|
int conditional = vardefined("cond"); |
256 |
|
long nrecs = 0; |
257 |
|
long nout = 0; |
266 |
|
} |
267 |
|
if (inpfmt != NULL) |
268 |
|
initinp(fp); |
269 |
< |
|
270 |
< |
while (inpfmt != NULL ? getrec() : fgets(inpbuf, INBSIZ, fp) != NULL) { |
269 |
> |
|
270 |
> |
while (getinputrec(fp)) { |
271 |
|
varset("recno", '=', (double)++nrecs); |
272 |
|
colflg = 0; |
273 |
|
eclock++; |
280 |
|
} |
281 |
|
|
282 |
|
|
283 |
< |
putout() /* produce an output record */ |
283 |
> |
static void |
284 |
> |
putout(void) /* produce an output record */ |
285 |
|
{ |
208 |
– |
extern int chanset(); |
286 |
|
|
287 |
|
colpos = 0; |
288 |
|
if (outfmt != NULL) |
289 |
|
putrec(); |
290 |
+ |
else if (bocols) |
291 |
+ |
chanout(bchanset); |
292 |
|
else |
293 |
|
chanout(chanset); |
294 |
< |
if (colpos) |
294 |
> |
if (colpos && !bocols) |
295 |
|
putchar('\n'); |
296 |
|
if (unbuff) |
297 |
|
fflush(stdout); |
299 |
|
|
300 |
|
|
301 |
|
double |
302 |
< |
chanvalue(n) /* return value for column n */ |
303 |
< |
int n; |
302 |
> |
chanvalue( /* return value for column n */ |
303 |
> |
int n |
304 |
> |
) |
305 |
|
{ |
306 |
|
int i; |
307 |
|
register char *cp; |
314 |
|
eputs("illegal channel number\n"); |
315 |
|
quit(1); |
316 |
|
} |
317 |
+ |
if (nbicols > 0) { |
318 |
+ |
if (n > nbicols) |
319 |
+ |
return(0.0); |
320 |
+ |
cp = inpbuf + (n-1)*sizeof(double); |
321 |
+ |
return(*(double *)cp); |
322 |
+ |
} |
323 |
+ |
if (nbicols < 0) { |
324 |
+ |
if (n > -nbicols) |
325 |
+ |
return(0.0); |
326 |
+ |
cp = inpbuf + (n-1)*sizeof(float); |
327 |
+ |
return(*(float *)cp); |
328 |
+ |
} |
329 |
|
if (n <= MAXCOL && colflg & 1L<<(n-1)) |
330 |
|
return(colval[n-1]); |
331 |
|
|
351 |
|
} |
352 |
|
|
353 |
|
|
354 |
< |
chanset(n, v) /* output column n */ |
355 |
< |
int n; |
356 |
< |
double v; |
354 |
> |
void |
355 |
> |
chanset( /* output column n */ |
356 |
> |
int n, |
357 |
> |
double v |
358 |
> |
) |
359 |
|
{ |
360 |
|
if (colpos == 0) /* no leading separator */ |
361 |
|
colpos = 1; |
367 |
|
} |
368 |
|
|
369 |
|
|
370 |
< |
readfmt(spec, output) /* read record format */ |
371 |
< |
char *spec; |
372 |
< |
int output; |
370 |
> |
void |
371 |
> |
bchanset( /* output binary channel n */ |
372 |
> |
int n, |
373 |
> |
double v |
374 |
> |
) |
375 |
|
{ |
376 |
+ |
static char zerobuf[sizeof(double)]; |
377 |
+ |
|
378 |
+ |
while (++colpos < n) |
379 |
+ |
fwrite(zerobuf, |
380 |
+ |
bocols>0 ? sizeof(double) : sizeof(float), |
381 |
+ |
1, stdout); |
382 |
+ |
if (bocols > 0) |
383 |
+ |
fwrite(&v, sizeof(double), 1, stdout); |
384 |
+ |
else { |
385 |
+ |
float fval = v; |
386 |
+ |
fwrite(&fval, sizeof(float), 1, stdout); |
387 |
+ |
} |
388 |
+ |
} |
389 |
+ |
|
390 |
+ |
|
391 |
+ |
static void |
392 |
+ |
readfmt( /* read record format */ |
393 |
+ |
char *spec, |
394 |
+ |
int output |
395 |
+ |
) |
396 |
+ |
{ |
397 |
|
int fd; |
398 |
|
char *inptr; |
399 |
|
struct field fmt; |
456 |
|
} |
457 |
|
|
458 |
|
|
459 |
< |
int |
460 |
< |
readfield(pp) /* get next field in format */ |
461 |
< |
register char **pp; |
459 |
> |
static int |
460 |
> |
readfield( /* get next field in format */ |
461 |
> |
register char **pp |
462 |
> |
) |
463 |
|
{ |
464 |
|
int type = F_NUL; |
465 |
|
int width = 0; |
524 |
|
|
525 |
|
|
526 |
|
struct strvar * |
527 |
< |
getsvar(svname) /* get string variable */ |
528 |
< |
char *svname; |
527 |
> |
getsvar( /* get string variable */ |
528 |
> |
char *svname |
529 |
> |
) |
530 |
|
{ |
531 |
|
register struct strvar *sv; |
532 |
|
|
542 |
|
} |
543 |
|
|
544 |
|
|
545 |
< |
svpreset(eqn) /* preset a string variable */ |
546 |
< |
char *eqn; |
545 |
> |
static void |
546 |
> |
svpreset( /* preset a string variable */ |
547 |
> |
char *eqn |
548 |
> |
) |
549 |
|
{ |
550 |
|
register struct strvar *sv; |
551 |
|
register char *val; |
564 |
|
} |
565 |
|
|
566 |
|
|
567 |
< |
clearrec() /* clear input record variables */ |
567 |
> |
static void |
568 |
> |
clearrec(void) /* clear input record variables */ |
569 |
|
{ |
570 |
|
register struct field *f; |
571 |
|
|
584 |
|
} |
585 |
|
|
586 |
|
|
587 |
< |
getrec() /* get next record from file */ |
587 |
> |
static int |
588 |
> |
getrec(void) /* get next record from file */ |
589 |
|
{ |
590 |
|
int eatline; |
591 |
|
register struct field *f; |
618 |
|
} |
619 |
|
|
620 |
|
|
621 |
< |
getfield(f) /* get next field */ |
622 |
< |
register struct field *f; |
621 |
> |
static int |
622 |
> |
getfield( /* get next field */ |
623 |
> |
register struct field *f |
624 |
> |
) |
625 |
|
{ |
626 |
< |
static char buf[MAXWORD+1]; /* no recursion! */ |
626 |
> |
static char buf[RMAXWORD+1]; /* no recursion! */ |
627 |
|
int delim, inword; |
628 |
|
double d; |
629 |
|
char *np; |
656 |
|
delim = f->next->f.sl[0]; |
657 |
|
cp = buf; |
658 |
|
do { |
659 |
< |
if (ipb.chr == EOF) |
659 |
> |
if (ipb.chr == EOF || ipb.chr == '\n') |
660 |
|
inword = 0; |
661 |
|
else if (blnkeq && delim != EOF) |
662 |
|
inword = isblnk(delim) ? |
668 |
|
*cp++ = ipb.chr; |
669 |
|
scaninp(); |
670 |
|
} |
671 |
< |
} while (inword && cp < &buf[MAXWORD]); |
671 |
> |
} while (inword && cp < &buf[RMAXWORD]); |
672 |
|
*cp = '\0'; |
673 |
|
if (f->f.sv->val == NULL) |
674 |
|
f->f.sv->val = savqstr(buf); /* first setting */ |
697 |
|
*cp++ = ipb.chr; |
698 |
|
scaninp(); |
699 |
|
} |
700 |
< |
} while (inword && cp < &buf[MAXWORD]); |
700 |
> |
} while (inword && cp < &buf[RMAXWORD]); |
701 |
|
*cp = '\0'; |
702 |
|
d = np==NULL ? 0. : atof(np); |
703 |
|
if (!vardefined(f->f.nv)) |
707 |
|
return(-1); /* doesn't match! */ |
708 |
|
return(0); |
709 |
|
} |
710 |
+ |
return -1; /* pro forma return */ |
711 |
|
} |
712 |
|
|
713 |
|
|
714 |
< |
putrec() /* output a record */ |
714 |
> |
static void |
715 |
> |
putrec(void) /* output a record */ |
716 |
|
{ |
717 |
|
char fmt[32]; |
718 |
|
register int n; |
762 |
|
} |
763 |
|
|
764 |
|
|
765 |
< |
initinp(fp) /* prepare lookahead buffer */ |
766 |
< |
FILE *fp; |
765 |
> |
static void |
766 |
> |
initinp(FILE *fp) /* prepare lookahead buffer */ |
767 |
> |
|
768 |
|
{ |
769 |
|
ipb.fin = fp; |
770 |
|
ipb.beg = ipb.end = inpbuf; |
774 |
|
} |
775 |
|
|
776 |
|
|
777 |
< |
scaninp() /* scan next character */ |
777 |
> |
static void |
778 |
> |
scaninp(void) /* scan next character */ |
779 |
|
{ |
780 |
|
if (ipb.chr == EOF) |
781 |
|
return; |
794 |
|
} |
795 |
|
|
796 |
|
|
797 |
< |
advinp() /* move home to current position */ |
797 |
> |
static void |
798 |
> |
advinp(void) /* move home to current position */ |
799 |
|
{ |
800 |
|
ipb.beg = ipb.pos; |
801 |
|
} |
802 |
|
|
803 |
|
|
804 |
< |
resetinp() /* rewind position and advance 1 */ |
804 |
> |
static void |
805 |
> |
resetinp(void) /* rewind position and advance 1 */ |
806 |
|
{ |
807 |
|
if (ipb.beg == NULL) /* full */ |
808 |
|
ipb.beg = ipb.end; |
815 |
|
|
816 |
|
|
817 |
|
void |
818 |
< |
eputs(msg) |
688 |
< |
char *msg; |
818 |
> |
eputs(char *msg) |
819 |
|
{ |
820 |
|
fputs(msg, stderr); |
821 |
|
} |
822 |
|
|
823 |
|
|
824 |
|
void |
825 |
< |
wputs(msg) |
696 |
< |
char *msg; |
825 |
> |
wputs(char *msg) |
826 |
|
{ |
827 |
|
if (!nowarn) |
828 |
|
eputs(msg); |
830 |
|
|
831 |
|
|
832 |
|
void |
833 |
< |
quit(code) |
705 |
< |
int code; |
833 |
> |
quit(int code) |
834 |
|
{ |
835 |
|
exit(code); |
836 |
|
} |