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.10 by greg, Thu Nov 6 05:39:33 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  <stdlib.h>
11 < #include  <stdio.h>
12 < #include  <string.h>
13 < #include  <math.h>
14 < #include  <ctype.h>
10 > #include <stdlib.h>
11 > #include <math.h>
12 > #include <ctype.h>
13  
14 < #include  "platform.h"
15 < #include  "calcomp.h"
16 < #include  "rterror.h"
14 > #include "platform.h"
15 > #include "rterror.h"
16 > #include "rtmisc.h"
17 > #include "rtio.h"
18 > #include "calcomp.h"
19  
20 < #define  isnum(c)       (isdigit(c) || (c)=='-' || (c)=='.' \
21 <                                || (c)=='+' || (c)=='e' || (c)=='E')
20 > #define isnum(c) (isdigit(c) || ((c)=='-') | ((c)=='.') \
21 >                                | ((c)=='+') | ((c)=='e') | ((c)=='E'))
22  
23 < #define  isblnk(c)      (igneol ? isspace(c) : (c)==' '||(c)=='\t')
23 > #define isblnk(c) (igneol ? isspace(c) : ((c)==' ')|((c)=='\t'))
24  
25 < #define  INBSIZ         4096    /* longest record */
26 < #define  MAXCOL         32      /* number of columns recorded */
25 > #define INBSIZ  16384   /* longest record */
26 > #define MAXCOL  32      /* number of columns recorded */
27  
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 */
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 < struct strvar {                 /* string variable */
37 <        char  *name;
38 <        char  *val;
39 <        char  *preset;
40 <        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   static int getinputrec(FILE *fp);
58 < static void scaninp(void), advinp(void), resetinp(void);
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);
# Line 69 | Line 69 | 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? */
78 < char  sepchar = '\t';           /* input/output separator */
79 < int  noinput = 0;               /* no input records? */
80 < int  nbicols = 0;               /* number of binary input columns */
81 < int  bocols = 0;                /* produce binary output columns */
82 < char  inpbuf[INBSIZ];           /* input buffer */
83 < double  colval[MAXCOL];         /* input column values */
84 < unsigned long  colflg = 0;      /* column retrieved flags */
85 < 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   int
105   main(
106 < int  argc,
107 < char  *argv[]
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);
# Line 121 | 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 128 | 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                          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
# Line 157 | Line 185 | char  *argv[]
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);
191 >                                        nbicols = atoi(argv[i]+3);
192                                  else
193 <                                        nbicols = -1;
194 <                                if (-nbicols*sizeof(float) > INBSIZ) {
193 >                                        nbicols = 1;
194 >                                if (nbicols*sizeof(float) > INBSIZ) {
195                                          eputs(argv[0]);
196                                          eputs(": too many input columns\n");
197                                          quit(1);
# Line 174 | Line 204 | char  *argv[]
204                  case 'o':
205                          switch (argv[i][2]) {
206                          case '\0':
207 <                                bocols = 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 <                                bocols = 0;
214 >                                otype = 'a';
215                                  break;
216                          case 'd':
217 <                                bocols = 1;
185 <                                break;
217 >                        case 'D':
218                          case 'f':
219 <                                bocols = -1;
219 >                        case 'F':
220 >                                otype = argv[i][2];
221                                  break;
222 +                        default:
223 +                                goto userr;
224                          }
225                          break;
226                  case 'w':
# Line 198 | Line 233 | char  *argv[]
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   static void
274 < nbsynch(void)               /* non-blank starting synch character */
274 > nbsynch(void)   /* non-blank starting synch character */
275   {
276          if (inpfmt == NULL || (inpfmt->type & F_TYP) != T_LIT)
277                  return;
# Line 235 | Line 284 | nbsynch(void)               /* non-blank starting sync
284  
285   static int
286   getinputrec(            /* get next input record */
287 < FILE  *fp
287 > FILE *fp
288   )
289   {
290 <        if (inpfmt != NULL)
291 <                return(getrec());
292 <        if (nbicols > 0)
293 <                return(fread(inpbuf, sizeof(double),
294 <                                        nbicols, fp) == nbicols);
295 <        if (nbicols < 0)
296 <                return(fread(inpbuf, sizeof(float),
297 <                                        -nbicols, fp) == -nbicols);
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
309 > execute(        /* process a file */
310 > char *file
311   )
312   {
313 <        int  conditional = vardefined("cond");
314 <        long  nrecs = 0;
315 <        long  nout = 0;
316 <        FILE  *fp;
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 267 | 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 (getinputrec(fp)) {
344 <                varset("recno", '=', (double)++nrecs);
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   static void
369 < putout(void)                /* produce an output record */
369 > putout(void)            /* produce an output record */
370   {
371  
372          colpos = 0;
373          if (outfmt != NULL)
374                  putrec();
375 <        else if (bocols)
294 <                chanout(bchanset);
295 <        else
375 >        else if (otype == 'a')
376                  chanout(chanset);
377 <        if (colpos && !bocols)
377 >        else
378 >                chanout(bchanset);
379 >        if (colpos && otype == 'a')
380                  putchar('\n');
381          if (unbuff)
382                  fflush(stdout);
# Line 304 | Line 386 | putout(void)                /* produce an output recor
386   static double
387   l_in(char *funame)      /* function call for $channel */
388   {
389 <        int  n;
390 <        register char  *cp;
389 >        int n;
390 >        char *cp;
391                          /* get argument as integer */
392          n = (int)(argument(1) + .5);
393          if (n != 0)     /* return channel value */
# Line 313 | Line 395 | l_in(char *funame)     /* function call for $channel */
395                          /* determine number of channels */
396          if (noinput || inpfmt != NULL)
397                  return(0);
398 <        if (nbicols > 0)
398 >        if (nbicols)
399                  return(nbicols);
318        if (nbicols < 0)
319                return(-nbicols);
400          cp = inpbuf;    /* need to count */
401          for (n = 0; *cp; )
402                  if (blnkeq && isspace(sepchar)) {
# Line 334 | Line 414 | l_in(char *funame)     /* function call for $channel */
414   }
415  
416   double
417 < chanvalue(            /* 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 349 | Line 429 | int  n
429                  eputs("illegal channel number\n");
430                  quit(1);
431          }
432 <        if (nbicols > 0) {
432 >        if (nbicols) {
433                  if (n > nbicols)
434                          return(0.0);
435 <                cp = inpbuf + (n-1)*sizeof(double);
436 <                return(*(double *)cp);
437 <        }
438 <        if (nbicols < 0) {
359 <                if (n > -nbicols)
360 <                        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          }
# Line 375 | 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 387 | Line 465 | int  n
465  
466  
467   void
468 < chanset(                   /* output column n */
469 < int  n,
470 < double  v
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 403 | Line 481 | double  v
481  
482  
483   void
484 < bchanset(                   /* output binary channel n */
485 < int  n,
486 < double  v
484 > bchanset(               /* output binary channel n */
485 > int n,
486 > double v
487   )
488   {
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 <                fwrite(zerobuf,
496 <                        bocols>0 ? sizeof(double) : sizeof(float),
497 <                        1, stdout);
498 <        if (bocols > 0)
499 <                fwrite(&v, sizeof(double), 1, stdout);
500 <        else {
501 <                float   fval = v;
502 <                fwrite(&fval, sizeof(float), 1, stdout);
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
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 <        register struct field  *f;
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 458 | 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 492 | Line 579 | int  output
579  
580  
581   static int
582 < readfield(                   /* get next field in format */
583 < register char  **pp
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 559 | Line 646 | register char  **pp
646  
647  
648   struct strvar *
649 < getsvar(                         /* 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 578 | Line 665 | char  *svname
665  
666  
667   static void
668 < svpreset(                    /* preset a string variable */
669 < char  *eqn
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 602 | Line 689 | char  *eqn
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 620 | Line 707 | clearrec(void)                 /* clear input record variables */
707  
708  
709   static int
710 < getrec(void)                                /* get next record from file */
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';
630 <                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) {
638 <                        advinp();
727 >                if (f == NULL)          /* got one? */
728                          return(1);
729 <                }
730 <                resetinp();
642 <                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();
649 <                        advinp();
736 >                        skipinp();
737                  }
738          }
739          return(0);
# Line 654 | Line 741 | getrec(void)                                /* get nex
741  
742  
743   static int
744 < getfield(                             /* get next field */
745 < register struct field  *f
744 > getfield(                       /* get next field */
745 > struct field *f
746   )
747   {
748 <        static char  buf[RMAXWORD+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 670 | 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 681 | 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 691 | Line 778 | register struct field  *f
778                          delim = f->next->f.sl[0];
779                  cp = buf;
780                  do {
781 <                        if (ipb.chr == EOF || ipb.chr == '\n')
781 >                        if ((ipb.chr == EOF) | (ipb.chr == '\n'))
782                                  inword = 0;
783                          else if (blnkeq && delim != EOF)
784                                  inword = isblnk(delim) ?
# Line 708 | Line 795 | register struct field  *f
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 739 | Line 826 | register struct field  *f
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; /* pro forma return */
832 >        return(1);      /* success! */
833   }
834  
835  
836   static void
837 < putrec(void)                                /* output a record */
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 783 | Line 871 | putrec(void)                                /* output
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 798 | Line 897 | putrec(void)                                /* output
897  
898  
899   static void
900 < initinp(FILE  *fp)                     /* prepare lookahead buffer */
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   static void
912 < scaninp(void)                       /* scan next character */
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 830 | Line 929 | scaninp(void)                       /* scan next chara
929  
930  
931   static void
932 < advinp(void)                        /* move home to current position */
932 > skipinp(void)                   /* rewind position and advance 1 */
933   {
934 <        ipb.beg = ipb.pos;
836 < }
837 <
838 <
839 < static void
840 < resetinp(void)                      /* rewind position and advance 1 */
841 < {
842 <        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(char  *msg)
967 > eputs(const char *msg)
968   {
969          fputs(msg, stderr);
970   }
971  
972  
973   void
974 < wputs(char  *msg)
974 > wputs(const char *msg)
975   {
976          if (!nowarn)
977                  eputs(msg);
# Line 865 | Line 979 | wputs(char  *msg)
979  
980  
981   void
982 < quit(int  code)
982 > quit(int code)
983   {
984          exit(code);
985   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines