ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cal/rcalc.c
(Generate patch)

Comparing ray/src/cal/rcalc.c (file contents):
Revision 1.2 by greg, Fri Feb 28 20:29:01 2003 UTC vs.
Revision 1.38 by greg, Fri Mar 10 17:38:01 2023 UTC

# Line 2 | Line 2
2   static const char RCSid[] = "$Id$";
3   #endif
4   /*
5 < *  rcalc.c - record calculator program.
5 > * rcalc.c - record calculator program.
6   *
7 < *     9/11/87
7 > * 9/11/87
8   */
9  
10 < #include  <stdio.h>
10 > #include <stdlib.h>
11 > #include <math.h>
12 > #include <ctype.h>
13  
14 < #include  <stdlib.h>
14 > #include "platform.h"
15 > #include "rterror.h"
16 > #include "rtmisc.h"
17 > #include "rtio.h"
18 > #include "calcomp.h"
19  
20 < #include  <math.h>
20 > #define isnum(c) (isdigit(c) || ((c)=='-') | ((c)=='.') \
21 >                                | ((c)=='+') | ((c)=='e') | ((c)=='E'))
22  
23 < #include  <ctype.h>
23 > #define isblnk(c) (igneol ? isspace(c) : ((c)==' ')|((c)=='\t'))
24  
25 < #include  "calcomp.h"
25 > #define INBSIZ  16384   /* longest record */
26 > #define MAXCOL  32      /* number of columns recorded */
27  
28 < #ifdef  CPM
29 < #define  getc           agetc   /* text files only, right? */
30 < #endif
28 >                        /* field type specifications */
29 > #define F_NUL   0               /* empty */
30 > #define F_TYP   0x7000          /* mask for type */
31 > #define F_WID   0x0fff          /* mask for width */
32 > #define T_LIT   0x1000          /* string literal */
33 > #define T_STR   0x2000          /* string variable */
34 > #define T_NUM   0x3000          /* numeric value */
35  
36 < #define  isnum(c)       (isdigit(c) || (c)=='-' || (c)=='.' \
37 <                                || (c)=='+' || (c)=='e' || (c)=='E')
38 <
39 < #define  isblnk(c)      (igneol ? isspace(c) : (c)==' '||(c)=='\t')
40 <
29 < #define  INBSIZ         4096    /* longest record */
30 < #define  MAXCOL         32      /* number of columns recorded */
31 <
32 <                                /* field type specifications */
33 < #define  F_NUL          0               /* empty */
34 < #define  F_TYP          0x7000          /* mask for type */
35 < #define  F_WID          0x0fff          /* mask for width */
36 < #define  T_LIT          0x1000          /* string literal */
37 < #define  T_STR          0x2000          /* string variable */
38 < #define  T_NUM          0x3000          /* numeric value */
39 <
40 < struct strvar {                 /* string variable */
41 <        char  *name;
42 <        char  *val;
43 <        char  *preset;
44 <        struct strvar  *next;
36 > struct strvar {         /* string variable */
37 >        char *name;
38 >        char *val;
39 >        char *preset;
40 >        struct strvar *next;
41   };
42  
43 < struct field {                  /* record format structure */
44 <        int  type;                      /* type of field (& width) */
43 > struct field {          /* record format structure */
44 >        int type;               /* type of field (& width) */
45          union {
46 <                char  *sl;                      /* string literal */
47 <                struct strvar  *sv;             /* string variable */
48 <                char  *nv;                      /* numeric variable */
49 <                EPNODE  *ne;                    /* numeric expression */
50 <        } f;                            /* field contents */
51 <        struct field  *next;            /* next field in record */
46 >                char *sl;               /* string literal */
47 >                struct strvar *sv;      /* string variable */
48 >                char *nv;               /* numeric variable */
49 >                EPNODE *ne;             /* numeric expression */
50 >        } f;                    /* field contents */
51 >        struct field *next;     /* next field in record */
52   };
53  
54 < #define  savqstr(s)     strcpy(emalloc(strlen(s)+1),s)
55 < #define  freqstr(s)     efree(s)
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 int getinputrec(FILE *fp);
58 > static void scaninp(void), skipinp(void), advinp(int skip);
59 > static void putrec(void), putout(void), nbsynch(void);
60 > static int getrec(void);
61 > static void execute(char *file);
62 > static void initinp(FILE *fp);
63 > static void svpreset(char *eqn);
64 > static void readfmt(char *spec, int output);
65 > static int readfield(char **pp);
66 > static int getfield(struct field *f);
67 > static void chanset(int n, double v);
68 > static void bchanset(int n, double v);
69 > static struct strvar* getsvar(char *svname);
70 > static double l_in(char *);
71  
72 < struct field  *inpfmt = NULL;   /* input record format */
73 < struct field  *outfmt = NULL;   /* output record structure */
74 < struct strvar  *svhead = NULL;  /* string variables */
72 > struct field *inpfmt = NULL;    /* input record format */
73 > struct field *outfmt = NULL;    /* output record structure */
74 > struct strvar *svhead = NULL;   /* string variables */
75  
76 < int  blnkeq = 1;                /* blanks compare equal? */
77 < int  igneol = 0;                /* ignore end of line? */
70 < char  sepchar = '\t';           /* input/output separator */
71 < int  noinput = 0;               /* no input records? */
72 < char  inpbuf[INBSIZ];           /* input buffer */
73 < double  colval[MAXCOL];         /* input column values */
74 < unsigned long  colflg = 0;      /* column retrieved flags */
75 < int  colpos;                    /* output column position */
76 > long incnt = 0;                 /* limit number of input records? */
77 > long outcnt = 0;                /* limit number of output records? */
78  
79 < int  nowarn = 0;                /* non-fatal diagnostic output */
80 < int  unbuff = 0;                /* unbuffered output (flush each record) */
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 itype = 'a';                /* input type (a/f/F/d/D) */
85 > int nbicols = 0;                /* number of binary input columns */
86 > int otype = 'a';                /* output format (a/f/F/d/D) */
87 > char inpbuf[INBSIZ];            /* input buffer */
88 > double colval[MAXCOL];          /* input column values */
89 > unsigned long colflg = 0;       /* column retrieved flags */
90 > int colpos;                     /* output column position */
91  
92 + int nowarn = 0;                 /* non-fatal diagnostic output */
93 + int unbuff = 0;                 /* unbuffered output (flush each record) */
94 +
95   struct {
96 <        FILE  *fin;                     /* input file */
97 <        int  chr;                       /* next character */
98 <        char  *beg;                     /* home position */
99 <        char  *pos;                     /* scan position */
100 <        char  *end;                     /* read position */
101 < } ipb;                          /* circular lookahead buffer */
96 >        FILE *fin;              /* input file */
97 >        int chr;                /* next character */
98 >        char *beg;              /* home position */
99 >        char *pos;              /* scan position */
100 >        char *end;              /* read position */
101 > } ipb;                  /* circular lookahead buffer */
102  
103  
104 < main(argc, argv)
105 < int  argc;
106 < char  *argv[];
104 > int
105 > main(
106 > int argc,
107 > char *argv[]
108 > )
109   {
110 <        int  i;
110 >        char *fpath;
111 >        int i;
112  
113          esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_OUTCHAN|E_RCONST;
114          esupport &= ~(E_REDEFW);
115  
116 < #ifdef  BIGGERLIB
116 > #ifdef BIGGERLIB
117          biggerlib();
118   #endif
119          varset("PI", ':', 3.14159265358979323846);
120 +        funset("in", 1, '=', &l_in);
121  
122          for (i = 1; i < argc && argv[i][0] == '-'; i++)
123                  switch (argv[i][1]) {
# Line 108 | Line 127 | char  *argv[];
127                  case 'l':
128                          igneol = !igneol;
129                          break;
130 +                case 'p':
131 +                        passive = 1;
132 +                        break;
133 +                case 'P':
134 +                        passive = -1;
135 +                        break;
136                  case 't':
137                          sepchar = argv[i][2];
138                          break;
# Line 115 | Line 140 | char  *argv[];
140                          svpreset(argv[++i]);
141                          break;
142                  case 'f':
143 <                        fcompile(argv[++i]);
143 >                        fpath = getpath(argv[++i], getrlibpath(), 0);
144 >                        if (fpath == NULL) {
145 >                                eputs(argv[0]);
146 >                                eputs(": cannot find file '");
147 >                                eputs(argv[i]);
148 >                                eputs("'\n");
149 >                                quit(1);
150 >                        }
151 >                        fcompile(fpath);
152                          break;
153                  case 'e':
154                          scompile(argv[++i], NULL, 0);
155                          break;
156                  case 'n':
157                          noinput = 1;
158 +                        esupport &= ~E_RCONST;
159                          break;
160                  case 'i':
161 <                        readfmt(argv[++i], 0);
161 >                        switch (argv[i][2]) {
162 >                        case '\0':
163 >                                itype = 'a';
164 >                                nbicols = 0;
165 >                                readfmt(argv[++i], 0);
166 >                                break;
167 >                        case 'n':
168 >                                incnt = atol(argv[++i]);
169 >                                break;
170 >                        case 'a':
171 >                                itype = 'a';
172 >                                nbicols = 0;
173 >                                break;
174 >                        case 'd':
175 >                        case 'D':
176 >                                itype = argv[i][2];
177 >                                if (isdigit(argv[i][3]))
178 >                                        nbicols = atoi(argv[i]+3);
179 >                                else
180 >                                        nbicols = 1;
181 >                                if (nbicols*sizeof(double) > INBSIZ) {
182 >                                        eputs(argv[0]);
183 >                                        eputs(": too many input columns\n");
184 >                                        quit(1);
185 >                                }
186 >                                break;
187 >                        case 'f':
188 >                        case 'F':
189 >                                itype = argv[i][2];
190 >                                if (isdigit(argv[i][3]))
191 >                                        nbicols = atoi(argv[i]+3);
192 >                                else
193 >                                        nbicols = 1;
194 >                                if (nbicols*sizeof(float) > INBSIZ) {
195 >                                        eputs(argv[0]);
196 >                                        eputs(": too many input columns\n");
197 >                                        quit(1);
198 >                                }
199 >                                break;
200 >                        default:
201 >                                goto userr;
202 >                        }
203                          break;
204                  case 'o':
205 <                        readfmt(argv[++i], 1);
205 >                        switch (argv[i][2]) {
206 >                        case '\0':
207 >                                otype = 'a';
208 >                                readfmt(argv[++i], 1);
209 >                                break;
210 >                        case 'n':
211 >                                outcnt = atol(argv[++i]);
212 >                                break;
213 >                        case 'a':
214 >                                otype = 'a';
215 >                                break;
216 >                        case 'd':
217 >                        case 'D':
218 >                        case 'f':
219 >                        case 'F':
220 >                                otype = argv[i][2];
221 >                                break;
222 >                        default:
223 >                                goto userr;
224 >                        }
225                          break;
226                  case 'w':
227                          nowarn = !nowarn;
# Line 135 | Line 229 | char  *argv[];
229                  case 'u':
230                          unbuff = !unbuff;
231                          break;
232 <                default:
232 >                default:;
233 >                userr:
234                          eputs("Usage: ");
235                          eputs(argv[0]);
236 < eputs(" [-b][-l][-n][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n");
236 > eputs(" [-b][-l][-n][-p|-P][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n");
237                          quit(1);
238                  }
239 <
240 <        if (noinput) {          /* produce a single output record */
239 >        if (otype != 'a')
240 >                SET_FILE_BINARY(stdout);
241 > #ifdef getc_unlocked            /* avoid lock/unlock overhead */
242 >        flockfile(stdout);
243 > #endif
244 >        if (noinput) {  /* produce a single output record */
245 >                if (i < argc) {
246 >                        eputs(argv[0]);
247 >                        eputs(": file argument(s) incompatible with -n\n");
248 >                        quit(1);
249 >                }
250                  eclock++;
251                  putout();
252                  quit(0);
253          }
254 <
255 <        if (blnkeq)             /* for efficiency */
254 >        if (passive && (inpfmt == NULL) | (outfmt == NULL)) {
255 >                eputs(argv[0]);
256 >                eputs(": options -p and -P require -i and -o formats\n");
257 >                quit(1);
258 >        }
259 >        if (blnkeq)     /* for efficiency */
260                  nbsynch();
261  
262 <        if (i == argc)          /* from stdin */
262 >        if (i == argc)  /* from stdin */
263                  execute(NULL);
264 <        else                    /* from one or more files */
264 >        else            /* from one or more files */
265                  for ( ; i < argc; i++)
266                          execute(argv[i]);
267          
268          quit(0);
269 +        return 0; /* pro forma return */
270   }
271  
272  
273 < nbsynch()               /* non-blank starting synch character */
273 > static void
274 > nbsynch(void)   /* non-blank starting synch character */
275   {
276          if (inpfmt == NULL || (inpfmt->type & F_TYP) != T_LIT)
277                  return;
# Line 172 | Line 282 | nbsynch()               /* non-blank starting synch ch
282   }
283  
284  
285 < execute(file)           /* process a file */
286 < char  *file;
285 > static int
286 > getinputrec(            /* get next input record */
287 > FILE *fp
288 > )
289   {
290 <        int  conditional = vardefined("cond");
291 <        long  nrecs = 0;
292 <        long  nout = 0;
293 <        FILE  *fp;
290 >        if ((itype == 'd') | (itype == 'D')) {
291 >                if (getbinary(inpbuf, sizeof(double), nbicols, fp) != nbicols)
292 >                        return(0);
293 >                if (itype == 'D')
294 >                        swap64(inpbuf, nbicols);
295 >                return(1);
296 >        }
297 >        if ((itype == 'f') | (itype == 'F')) {
298 >                if (getbinary(inpbuf, sizeof(float), nbicols, fp) != nbicols)
299 >                        return(0);
300 >                if (itype == 'F')
301 >                        swap32(inpbuf, nbicols);
302 >                return(1);
303 >        }
304 >        return(fgets(inpbuf, INBSIZ, fp) != NULL);
305 > }
306 >
307 >
308 > static void
309 > execute(        /* process a file */
310 > char *file
311 > )
312 > {
313 >        static char     condVN[] = "cond";
314 >        static char     recnoVN[] = "recno";
315 >        static char     outnoVN[] = "outno";
316 >        const int       conditional = vardefined(condVN);
317 >        const int       set_recno = (varlookup(recnoVN) != NULL);
318 >        const int       set_outno = (varlookup(outnoVN) != NULL);
319 >        long            nrecs = 0;
320 >        long            nout = 0;
321 >        FILE            *fp;
322          
323          if (file == NULL)
324                  fp = stdin;
# Line 187 | Line 327 | char  *file;
327                  eputs(": cannot open\n");
328                  quit(1);
329          }
330 +        if (itype != 'a')
331 +                SET_FILE_BINARY(fp);
332 + #ifdef getc_unlocked            /* avoid lock/unlock overhead */
333 +        flockfile(fp);
334 + #endif
335 +        if (conditional == ':') {
336 +                eputs(condVN);
337 +                eputs(": defined as constant\n");
338 +                quit(1);
339 +        }
340          if (inpfmt != NULL)
341                  initinp(fp);
342 <                
343 <        while (inpfmt != NULL ? getrec() : fgets(inpbuf, INBSIZ, fp) != NULL) {
344 <                varset("recno", '=', (double)++nrecs);
342 >        
343 >        while (inpfmt != NULL ? getrec() : getinputrec(fp)) {
344 >                ++nrecs;
345 >                if (set_recno)
346 >                        varset(recnoVN, '=', (double)nrecs);
347 >                if (set_outno)
348 >                        varset(outnoVN, '=', (double)(nout+1));
349                  colflg = 0;
350                  eclock++;
351 <                if (!conditional || varvalue("cond") > 0.0) {
352 <                        varset("outno", '=', (double)++nout);
351 >                if (!conditional || varvalue(condVN) > 0.0) {
352 >                        if (inpfmt != NULL)
353 >                                advinp(0);
354                          putout();
355 +                        ++nout;
356 +                } else if (inpfmt != NULL) {
357 +                        advinp(1);
358                  }
359 +                if (incnt && nrecs >= incnt)
360 +                        break;
361 +                if (outcnt && nout >= outcnt)
362 +                        break;
363          }
364          fclose(fp);
365   }
366  
367  
368 < putout()                /* produce an output record */
368 > static void
369 > putout(void)            /* produce an output record */
370   {
208        extern int  chanset();
371  
372          colpos = 0;
373          if (outfmt != NULL)
374                  putrec();
375 <        else
375 >        else if (otype == 'a')
376                  chanout(chanset);
377 <        if (colpos)
377 >        else
378 >                chanout(bchanset);
379 >        if (colpos && otype == 'a')
380                  putchar('\n');
381          if (unbuff)
382                  fflush(stdout);
383   }
384  
385  
386 + static double
387 + l_in(char *funame)      /* function call for $channel */
388 + {
389 +        int n;
390 +        char *cp;
391 +                        /* get argument as integer */
392 +        n = (int)(argument(1) + .5);
393 +        if (n != 0)     /* return channel value */
394 +                return(chanvalue(n));
395 +                        /* determine number of channels */
396 +        if (noinput || inpfmt != NULL)
397 +                return(0);
398 +        if (nbicols)
399 +                return(nbicols);
400 +        cp = inpbuf;    /* need to count */
401 +        for (n = 0; *cp; )
402 +                if (blnkeq && isspace(sepchar)) {
403 +                        while (isspace(*cp))
404 +                                cp++;
405 +                        n += *cp != '\0';
406 +                        while (*cp && !isspace(*cp))
407 +                                cp++;
408 +                } else {
409 +                        n += *cp != '\n';
410 +                        while (*cp && *cp++ != sepchar)
411 +                                ;
412 +                }
413 +        return(n);
414 + }
415 +
416   double
417 < chanvalue(n)            /* return value for column n */
418 < int  n;
417 > chanvalue(      /* return value for column n */
418 > int n
419 > )
420   {
421 <        int  i;
422 <        register char  *cp;
421 >        int i;
422 >        char *cp;
423  
424          if (noinput || inpfmt != NULL) {
425                  eputs("no column input\n");
# Line 234 | Line 429 | int  n;
429                  eputs("illegal channel number\n");
430                  quit(1);
431          }
432 +        if (nbicols) {
433 +                if (n > nbicols)
434 +                        return(0.0);
435 +                if ((itype == 'd') | (itype == 'D')) {
436 +                        cp = inpbuf + (n-1)*sizeof(double);
437 +                        return(*(double *)cp);
438 +                }
439 +                cp = inpbuf + (n-1)*sizeof(float);
440 +                return(*(float *)cp);
441 +        }
442          if (n <= MAXCOL && colflg & 1L<<(n-1))
443                  return(colval[n-1]);
444  
# Line 248 | Line 453 | int  n;
453                          while (*cp && *cp++ != sepchar)
454                                  ;
455  
456 <        while (isspace(*cp))            /* some atof()'s don't like tabs */
456 >        while (isspace(*cp))    /* some atof()'s don't like tabs */
457                  cp++;
458  
459          if (n <= MAXCOL) {
# Line 259 | Line 464 | int  n;
464   }
465  
466  
467 < chanset(n, v)                   /* output column n */
468 < int  n;
469 < double  v;
467 > void
468 > chanset(                /* output column n */
469 > int n,
470 > double v
471 > )
472   {
473 <        if (colpos == 0)                /* no leading separator */
473 >        if (colpos == 0)                /* no leading separator */
474                  colpos = 1;
475          while (colpos < n) {
476                  putchar(sepchar);
# Line 273 | Line 480 | double  v;
480   }
481  
482  
483 < readfmt(spec, output)                   /* read record format */
484 < char  *spec;
485 < int  output;
483 > void
484 > bchanset(               /* output binary channel n */
485 > int n,
486 > double v
487 > )
488   {
489 <        int  fd;
490 <        char  *inptr;
491 <        struct field  fmt;
492 <        int  res;
493 <        register struct field  *f;
489 >        static char     zerobuf[sizeof(double)];
490 >        const int       otlen = ((otype == 'd') | (otype == 'D')) ?
491 >                                        sizeof(double) : sizeof(float);
492 >        float   fval = v;
493 >
494 >        while (++colpos < n)
495 >                putbinary(zerobuf, otlen, 1, stdout);
496 >        switch (otype) {
497 >        case 'D':
498 >                swap64((char *)&v, 1);
499 >                /* fall through */
500 >        case 'd':
501 >                putbinary(&v, sizeof(double), 1, stdout);
502 >                break;
503 >        case 'F':
504 >                swap32((char *)&fval, 1);
505 >                /* fall through */
506 >        case 'f':
507 >                putbinary(&fval, sizeof(float), 1, stdout);
508 >                break;
509 >        }
510 > }
511 >
512 >
513 > static void
514 > readfmt(                /* read record format */
515 > char *spec,
516 > int output
517 > )
518 > {
519 >        int fd;
520 >        char *inptr;
521 >        struct field fmt;
522 >        int res;
523 >        struct field *f;
524                                                  /* check for inline format */
525          for (inptr = spec; *inptr; inptr++)
526                  if (*inptr == '$')
527                          break;
528 <        if (*inptr)                             /* inline */
528 >        if (*inptr)                     /* inline */
529                  inptr = spec;
530 <        else {                                  /* from file */
530 >        else {                          /* from file */
531                  if ((fd = open(spec, 0)) == -1) {
532                          eputs(spec);
533                          eputs(": cannot open\n");
534                          quit(1);
535                  }
536 <                res = read(fd, inpbuf+1, INBSIZ-1);
536 >                res = read(fd, inpbuf+2, INBSIZ-2);
537                  if (res <= 0 || res >= INBSIZ-1) {
538                          eputs(spec);
539                          if (res < 0)
# Line 306 | Line 545 | int  output;
545                          quit(1);
546                  }
547                  close(fd);
548 <                (inptr=inpbuf+1)[res] = '\0';
548 >                (inptr=inpbuf+2)[res] = '\0';
549          }
550 <        f = &fmt;                               /* get fields */
550 >        f = &fmt;                       /* get fields */
551          while ((res = readfield(&inptr)) != F_NUL) {
552                  f->next = (struct field *)emalloc(sizeof(struct field));
553                  f = f->next;
# Line 339 | Line 578 | int  output;
578   }
579  
580  
581 < int
582 < readfield(pp)                   /* get next field in format */
583 < register char  **pp;
581 > static int
582 > readfield(              /* get next field in format */
583 > char **pp
584 > )
585   {
586 <        int  type = F_NUL;
587 <        int  width = 0;
588 <        register char  *cp;
586 >        int type = F_NUL;
587 >        int width = 0;
588 >        char *cp;
589          
590          cp = inpbuf;
591          while (cp < &inpbuf[INBSIZ-1] && **pp != '\0') {
# Line 406 | Line 646 | register char  **pp;
646  
647  
648   struct strvar *
649 < getsvar(svname)                         /* get string variable */
650 < char  *svname;
649 > getsvar(                        /* get string variable */
650 > char *svname
651 > )
652   {
653 <        register struct strvar  *sv;
653 >        struct strvar *sv;
654          
655          for (sv = svhead; sv != NULL; sv = sv->next)
656                  if (!strcmp(sv->name, svname))
# Line 423 | Line 664 | char  *svname;
664   }
665  
666  
667 < svpreset(eqn)                    /* preset a string variable */
668 < char  *eqn;
667 > static void
668 > svpreset(               /* preset a string variable */
669 > char *eqn
670 > )
671   {
672 <        register struct strvar  *sv;
673 <        register char  *val;
672 >        struct strvar *sv;
673 >        char *val;
674  
675          for (val = eqn; *val != '='; val++)
676                  if (!*val)
# Line 443 | Line 686 | char  *eqn;
686   }
687  
688  
689 < clearrec()                      /* clear input record variables */
689 > static void
690 > clearrec(void)                  /* clear input record variables */
691   {
692 <        register struct field  *f;
692 >        struct field *f;
693  
694          for (f = inpfmt; f != NULL; f = f->next)
695                  switch (f->type & F_TYP) {
# Line 462 | Line 706 | clearrec()                     /* clear input record variables */
706   }
707  
708  
709 < getrec()                                /* get next record from file */
709 > static int
710 > getrec(void)                    /* get next record from file */
711   {
712 <        int  eatline;
713 <        register struct field  *f;
714 <        
712 >        int eatline;
713 >        struct field *f;
714 >
715          while (ipb.chr != EOF) {
716 <                eatline = !igneol && ipb.chr != '\n';
472 <                if (blnkeq)             /* beware of nbsynch() */
716 >                if (blnkeq) {           /* beware of nbsynch() */
717                          while (isblnk(ipb.chr))
718 <                                scaninp();
718 >                                skipinp();
719 >                        if (ipb.chr == EOF)
720 >                                return(0);
721 >                }
722 >                eatline = !igneol & (ipb.chr != '\n');
723                  clearrec();             /* start with fresh record */
724                  for (f = inpfmt; f != NULL; f = f->next)
725 <                        if (getfield(f) == -1)
725 >                        if (!getfield(f))
726                                  break;
727 <                if (f == NULL) {
480 <                        advinp();
727 >                if (f == NULL)          /* got one? */
728                          return(1);
729 <                }
730 <                resetinp();
484 <                if (eatline) {          /* eat rest of line */
729 >                skipinp();              /* else eat false start */
730 >                if (eatline) {          /* eat rest of line */
731                          while (ipb.chr != '\n') {
732                                  if (ipb.chr == EOF)
733                                          return(0);
734 <                                scaninp();
734 >                                skipinp();
735                          }
736 <                        scaninp();
491 <                        advinp();
736 >                        skipinp();
737                  }
738          }
739          return(0);
740   }
741  
742  
743 < getfield(f)                             /* get next field */
744 < register struct field  *f;
743 > static int
744 > getfield(                       /* get next field */
745 > struct field *f
746 > )
747   {
748 <        static char  buf[MAXWORD+1];            /* no recursion! */
749 <        int  delim, inword;
750 <        double  d;
751 <        char  *np;
752 <        register char  *cp;
748 >        static char buf[RMAXWORD+1];    /* no recursion! */
749 >        int delim, inword;
750 >        double d;
751 >        char *np;
752 >        char *cp;
753  
754          switch (f->type & F_TYP) {
755          case T_LIT:
# Line 510 | Line 757 | register struct field  *f;
757                  do {
758                          if (blnkeq && isblnk(*cp)) {
759                                  if (!isblnk(ipb.chr))
760 <                                        return(-1);
760 >                                        return(0);
761                                  do
762                                          cp++;
763                                  while (isblnk(*cp));
# Line 521 | Line 768 | register struct field  *f;
768                                  cp++;
769                                  scaninp();
770                          } else
771 <                                return(-1);
771 >                                return(0);
772                  } while (*cp);
773 <                return(0);
773 >                break;
774          case T_STR:
775                  if (f->next == NULL || (f->next->type & F_TYP) != T_LIT)
776                          delim = EOF;
# Line 531 | Line 778 | register struct field  *f;
778                          delim = f->next->f.sl[0];
779                  cp = buf;
780                  do {
781 <                        if (ipb.chr == EOF)
781 >                        if ((ipb.chr == EOF) | (ipb.chr == '\n'))
782                                  inword = 0;
783                          else if (blnkeq && delim != EOF)
784                                  inword = isblnk(delim) ?
# Line 543 | Line 790 | register struct field  *f;
790                                  *cp++ = ipb.chr;
791                                  scaninp();
792                          }
793 <                } while (inword && cp < &buf[MAXWORD]);
793 >                } while (inword && cp < &buf[RMAXWORD]);
794                  *cp = '\0';
795                  if (f->f.sv->val == NULL)
796                          f->f.sv->val = savqstr(buf);    /* first setting */
797                  else if (strcmp(f->f.sv->val, buf))
798 <                        return(-1);                     /* doesn't match! */
799 <                return(0);
798 >                        return(0);                      /* doesn't match! */
799 >                break;
800          case T_NUM:
801                  if (f->next == NULL || (f->next->type & F_TYP) != T_LIT)
802                          delim = EOF;
# Line 572 | Line 819 | register struct field  *f;
819                                  *cp++ = ipb.chr;
820                                  scaninp();
821                          }
822 <                } while (inword && cp < &buf[MAXWORD]);
822 >                } while (inword && cp < &buf[RMAXWORD]);
823                  *cp = '\0';
824                  d = np==NULL ? 0. : atof(np);
825                  if (!vardefined(f->f.nv))
826                          varset(f->f.nv, '=', d);        /* first setting */
827                  else if ((d = (varvalue(f->f.nv)-d)/(d==0.?1.:d)) > .001
828                                  || d < -.001)
829 <                        return(-1);                     /* doesn't match! */
830 <                return(0);
829 >                        return(0);                      /* doesn't match! */
830 >                break;
831          }
832 +        return(1);      /* success! */
833   }
834  
835  
836 < putrec()                                /* output a record */
836 > static void
837 > putrec(void)                            /* output a record */
838   {
839 <        char  fmt[32];
840 <        register int  n;
841 <        register struct field  *f;
842 <        int  adlast, adnext;
839 >        char fmt[32], typ[16];
840 >        int n;
841 >        struct field *f;
842 >        int adlast, adnext;
843 >        double dv, av;
844          
845          adlast = 0;
846          for (f = outfmt; f != NULL; f = f->next) {
847 <                adnext =        blnkeq &&
847 >                adnext =        blnkeq &&
848                                  f->next != NULL &&
849                                  !( (f->next->type&F_TYP) == T_LIT &&
850                                          f->next->f.sl[0] == ' ' );
# Line 621 | Line 871 | putrec()                                /* output a re
871                          break;
872                  case T_NUM:
873                          n = f->type & F_WID;
874 +                        dv = evalue(f->f.ne);
875 +                        av = fabs(dv);
876 +                        if (n <= 9)
877 +                                strcpy(typ, "g");
878 +                        else
879 +                                sprintf(typ, ".%de", n-5);
880 +                        if (av < 1L<<31) {
881 +                                long    iv = (int)(av + .5);
882 +                                if (iv && fabs(av-iv) <= av*1e-14)
883 +                                        strcpy(typ, ".0f");
884 +                        }
885                          if (adlast && adnext)
886 <                                strcpy(fmt, "%g");
886 >                                sprintf(fmt, "%%%s", typ);
887                          else if (adlast)
888 <                                sprintf(fmt, "%%-%dg", n);
888 >                                sprintf(fmt, "%%-%d%s", n, typ);
889                          else
890 <                                sprintf(fmt, "%%%dg", n);
891 <                        printf(fmt, evalue(f->f.ne));
890 >                                sprintf(fmt, "%%%d%s", n, typ);
891 >                        printf(fmt, dv);
892                          adlast = 1;
893                          break;
894                  }
# Line 635 | Line 896 | putrec()                                /* output a re
896   }
897  
898  
899 < initinp(fp)                     /* prepare lookahead buffer */
900 < FILE  *fp;
899 > static void
900 > initinp(FILE *fp)               /* prepare lookahead buffer */
901 >
902   {
903          ipb.fin = fp;
904          ipb.beg = ipb.end = inpbuf;
905 <        ipb.pos = inpbuf-1;             /* position before beginning */
905 >        ipb.pos = inpbuf-1;             /* position before beginning */
906          ipb.chr = '\0';
907          scaninp();
908   }
909  
910  
911 < scaninp()                       /* scan next character */
911 > static void
912 > scaninp(void)                   /* scan next character */
913   {
914          if (ipb.chr == EOF)
915                  return;
916          if (++ipb.pos >= &inpbuf[INBSIZ])
917                  ipb.pos = inpbuf;
918 <        if (ipb.pos == ipb.end) {               /* new character */
918 >        if (ipb.pos == ipb.end) {       /* new character */
919                  if ((ipb.chr = getc(ipb.fin)) != EOF) {
920                          *ipb.end = ipb.chr;
921                          if (++ipb.end >= &inpbuf[INBSIZ])
# Line 665 | Line 928 | scaninp()                       /* scan next character
928   }
929  
930  
931 < advinp()                        /* move home to current position */
931 > static void
932 > skipinp(void)                   /* rewind position and advance 1 */
933   {
934 <        ipb.beg = ipb.pos;
671 < }
672 <
673 <
674 < resetinp()                      /* rewind position and advance 1 */
675 < {
676 <        if (ipb.beg == NULL)            /* full */
934 >        if (ipb.beg == NULL)            /* can't fully rewind? */
935                  ipb.beg = ipb.end;
936          ipb.pos = ipb.beg;
937          ipb.chr = *ipb.pos;
938 +        if (passive)                    /* transmit unmatched character? */
939 +                putchar(ipb.chr);
940          if (++ipb.beg >= &inpbuf[INBSIZ])
941                  ipb.beg = inpbuf;
942          scaninp();
943   }
944  
945  
946 + static void
947 + advinp(int skip)                /* advance home to current position */
948 + {
949 +        if (!skip | (passive >= 0)) {
950 +                ipb.beg = ipb.pos;      /* no need to copy input */
951 +                return;
952 +        }
953 +        if (ipb.beg == NULL) {          /* buffer overflowed a bit? */
954 +                wputs("buffer overflow\n");
955 +                puts("\n*** MISSING DATA ***");
956 +                ipb.beg = ipb.end;
957 +        }
958 +        while (ipb.beg != ipb.pos) {    /* copy buffer to current */
959 +                putchar(*ipb.beg);
960 +                if (++ipb.beg >= &inpbuf[INBSIZ])
961 +                        ipb.beg = inpbuf;
962 +        }
963 + }
964 +
965 +
966   void
967 < eputs(msg)
688 < char  *msg;
967 > eputs(const char *msg)
968   {
969          fputs(msg, stderr);
970   }
971  
972  
973   void
974 < wputs(msg)
696 < char  *msg;
974 > wputs(const char *msg)
975   {
976          if (!nowarn)
977                  eputs(msg);
# Line 701 | Line 979 | char  *msg;
979  
980  
981   void
982 < quit(code)
705 < int  code;
982 > quit(int code)
983   {
984          exit(code);
985   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines