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.16 by greg, Fri Dec 10 05:52:14 2004 UTC vs.
Revision 1.39 by greg, Fri Feb 23 03:47:57 2024 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  <fcntl.h>
12 < #include  <stdio.h>
13 < #include  <string.h>
14 < #include  <math.h>
15 < #include  <ctype.h>
10 > #include <stdlib.h>
11 > #include <math.h>
12 > #include <ctype.h>
13  
14 < #include  "platform.h"
15 < #include  "rterror.h"
16 < #include  "rtmisc.h"
17 < #include  "rtio.h"
18 < #include  "calcomp.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         16384   /* 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 72 | 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? */
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  nbicols = 0;               /* number of binary input columns */
85 < int  bocols = 0;                /* produce binary output columns */
86 < char  inpbuf[INBSIZ];           /* input buffer */
87 < double  colval[MAXCOL];         /* input column values */
88 < unsigned long  colflg = 0;      /* column retrieved flags */
89 < 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 126 | Line 128 | char  *argv[]
128                          igneol = !igneol;
129                          break;
130                  case 'p':
131 <                        passive = !passive;
131 >                        passive = 1;
132                          break;
133 +                case 'P':
134 +                        passive = -1;
135 +                        break;
136                  case 't':
137                          sepchar = argv[i][2];
138                          break;
# Line 135 | 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 164 | 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 181 | 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;
192 <                                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 205 | Line 233 | char  *argv[]
233                  userr:
234                          eputs("Usage: ");
235                          eputs(argv[0]);
236 < eputs(" [-b][-l][-n][-p][-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 >        doptimize(1);   /* optimize definitions */
263 >
264 >        if (i == argc)  /* from stdin */
265                  execute(NULL);
266 <        else                    /* from one or more files */
266 >        else            /* from one or more files */
267                  for ( ; i < argc; i++)
268                          execute(argv[i]);
269          
# Line 230 | Line 273 | eputs(" [-b][-l][-n][-p][-w][-u][-tS][-s svar=sval][-e
273  
274  
275   static void
276 < nbsynch(void)               /* non-blank starting synch character */
276 > nbsynch(void)   /* non-blank starting synch character */
277   {
278          if (inpfmt == NULL || (inpfmt->type & F_TYP) != T_LIT)
279                  return;
# Line 243 | Line 286 | nbsynch(void)               /* non-blank starting sync
286  
287   static int
288   getinputrec(            /* get next input record */
289 < FILE  *fp
289 > FILE *fp
290   )
291   {
292 <        if (inpfmt != NULL)
293 <                return(getrec());
294 <        if (nbicols > 0)
295 <                return(fread(inpbuf, sizeof(double),
296 <                                        nbicols, fp) == nbicols);
297 <        if (nbicols < 0)
298 <                return(fread(inpbuf, sizeof(float),
299 <                                        -nbicols, fp) == -nbicols);
292 >        if ((itype == 'd') | (itype == 'D')) {
293 >                if (getbinary(inpbuf, sizeof(double), nbicols, fp) != nbicols)
294 >                        return(0);
295 >                if (itype == 'D')
296 >                        swap64(inpbuf, nbicols);
297 >                return(1);
298 >        }
299 >        if ((itype == 'f') | (itype == 'F')) {
300 >                if (getbinary(inpbuf, sizeof(float), nbicols, fp) != nbicols)
301 >                        return(0);
302 >                if (itype == 'F')
303 >                        swap32(inpbuf, nbicols);
304 >                return(1);
305 >        }
306          return(fgets(inpbuf, INBSIZ, fp) != NULL);
307   }
308  
309  
310   static void
311 < execute(           /* process a file */
312 < char  *file
311 > execute(        /* process a file */
312 > char *file
313   )
314   {
315 <        int  conditional = vardefined("cond");
316 <        long  nrecs = 0;
317 <        long  nout = 0;
318 <        FILE  *fp;
315 >        static char     condVN[] = "cond";
316 >        static char     recnoVN[] = "recno";
317 >        static char     outnoVN[] = "outno";
318 >        const int       conditional = vardefined(condVN);
319 >        const int       set_recno = (varlookup(recnoVN) != NULL);
320 >        const int       set_outno = (varlookup(outnoVN) != NULL);
321 >        long            nrecs = 0;
322 >        long            nout = 0;
323 >        FILE            *fp;
324          
325          if (file == NULL)
326                  fp = stdin;
# Line 275 | Line 329 | char  *file
329                  eputs(": cannot open\n");
330                  quit(1);
331          }
332 +        if (itype != 'a')
333 +                SET_FILE_BINARY(fp);
334 + #ifdef getc_unlocked            /* avoid lock/unlock overhead */
335 +        flockfile(fp);
336 + #endif
337 +        if (conditional == ':') {
338 +                eputs(condVN);
339 +                eputs(": defined as constant\n");
340 +                quit(1);
341 +        }
342          if (inpfmt != NULL)
343                  initinp(fp);
344          
345 <        while (getinputrec(fp)) {
346 <                varset("recno", '=', (double)++nrecs);
345 >        while (inpfmt != NULL ? getrec() : getinputrec(fp)) {
346 >                ++nrecs;
347 >                if (set_recno)
348 >                        varset(recnoVN, '=', (double)nrecs);
349 >                if (set_outno)
350 >                        varset(outnoVN, '=', (double)(nout+1));
351                  colflg = 0;
352                  eclock++;
353 <                if (!conditional || varvalue("cond") > 0.0) {
354 <                        varset("outno", '=', (double)++nout);
353 >                if (!conditional || varvalue(condVN) > 0.0) {
354 >                        if (inpfmt != NULL)
355 >                                advinp(0);
356                          putout();
357 +                        ++nout;
358 +                } else if (inpfmt != NULL) {
359 +                        advinp(1);
360                  }
361 +                if (incnt && nrecs >= incnt)
362 +                        break;
363 +                if (outcnt && nout >= outcnt)
364 +                        break;
365          }
366          fclose(fp);
367   }
368  
369  
370   static void
371 < putout(void)                /* produce an output record */
371 > putout(void)            /* produce an output record */
372   {
373  
374          colpos = 0;
375          if (outfmt != NULL)
376                  putrec();
377 <        else if (bocols)
302 <                chanout(bchanset);
303 <        else
377 >        else if (otype == 'a')
378                  chanout(chanset);
379 <        if (colpos && !bocols)
379 >        else
380 >                chanout(bchanset);
381 >        if (colpos && otype == 'a')
382                  putchar('\n');
383          if (unbuff)
384                  fflush(stdout);
# Line 312 | Line 388 | putout(void)                /* produce an output recor
388   static double
389   l_in(char *funame)      /* function call for $channel */
390   {
391 <        int  n;
392 <        register char  *cp;
391 >        int n;
392 >        char *cp;
393                          /* get argument as integer */
394          n = (int)(argument(1) + .5);
395          if (n != 0)     /* return channel value */
# Line 321 | Line 397 | l_in(char *funame)     /* function call for $channel */
397                          /* determine number of channels */
398          if (noinput || inpfmt != NULL)
399                  return(0);
400 <        if (nbicols > 0)
400 >        if (nbicols)
401                  return(nbicols);
326        if (nbicols < 0)
327                return(-nbicols);
402          cp = inpbuf;    /* need to count */
403          for (n = 0; *cp; )
404                  if (blnkeq && isspace(sepchar)) {
# Line 342 | Line 416 | l_in(char *funame)     /* function call for $channel */
416   }
417  
418   double
419 < chanvalue(            /* return value for column n */
420 < int  n
419 > chanvalue(      /* return value for column n */
420 > int n
421   )
422   {
423 <        int  i;
424 <        register char  *cp;
423 >        int i;
424 >        char *cp;
425  
426          if (noinput || inpfmt != NULL) {
427                  eputs("no column input\n");
# Line 357 | Line 431 | int  n
431                  eputs("illegal channel number\n");
432                  quit(1);
433          }
434 <        if (nbicols > 0) {
434 >        if (nbicols) {
435                  if (n > nbicols)
436                          return(0.0);
437 <                cp = inpbuf + (n-1)*sizeof(double);
438 <                return(*(double *)cp);
439 <        }
440 <        if (nbicols < 0) {
367 <                if (n > -nbicols)
368 <                        return(0.0);
437 >                if ((itype == 'd') | (itype == 'D')) {
438 >                        cp = inpbuf + (n-1)*sizeof(double);
439 >                        return(*(double *)cp);
440 >                }
441                  cp = inpbuf + (n-1)*sizeof(float);
442                  return(*(float *)cp);
443          }
# Line 383 | Line 455 | int  n
455                          while (*cp && *cp++ != sepchar)
456                                  ;
457  
458 <        while (isspace(*cp))            /* some atof()'s don't like tabs */
458 >        while (isspace(*cp))    /* some atof()'s don't like tabs */
459                  cp++;
460  
461          if (n <= MAXCOL) {
# Line 395 | Line 467 | int  n
467  
468  
469   void
470 < chanset(                   /* output column n */
471 < int  n,
472 < double  v
470 > chanset(                /* output column n */
471 > int n,
472 > double v
473   )
474   {
475 <        if (colpos == 0)                /* no leading separator */
475 >        if (colpos == 0)                /* no leading separator */
476                  colpos = 1;
477          while (colpos < n) {
478                  putchar(sepchar);
# Line 411 | Line 483 | double  v
483  
484  
485   void
486 < bchanset(                   /* output binary channel n */
487 < int  n,
488 < double  v
486 > bchanset(               /* output binary channel n */
487 > int n,
488 > double v
489   )
490   {
491          static char     zerobuf[sizeof(double)];
492 +        const int       otlen = ((otype == 'd') | (otype == 'D')) ?
493 +                                        sizeof(double) : sizeof(float);
494 +        float   fval = v;
495  
496          while (++colpos < n)
497 <                fwrite(zerobuf,
498 <                        bocols>0 ? sizeof(double) : sizeof(float),
499 <                        1, stdout);
500 <        if (bocols > 0)
501 <                fwrite(&v, sizeof(double), 1, stdout);
502 <        else {
503 <                float   fval = v;
504 <                fwrite(&fval, sizeof(float), 1, stdout);
497 >                putbinary(zerobuf, otlen, 1, stdout);
498 >        switch (otype) {
499 >        case 'D':
500 >                swap64((char *)&v, 1);
501 >                /* fall through */
502 >        case 'd':
503 >                putbinary(&v, sizeof(double), 1, stdout);
504 >                break;
505 >        case 'F':
506 >                swap32((char *)&fval, 1);
507 >                /* fall through */
508 >        case 'f':
509 >                putbinary(&fval, sizeof(float), 1, stdout);
510 >                break;
511          }
512   }
513  
514  
515   static void
516 < readfmt(                   /* read record format */
517 < char  *spec,
518 < int  output
516 > readfmt(                /* read record format */
517 > char *spec,
518 > int output
519   )
520   {
521 <        int  fd;
522 <        char  *inptr;
523 <        struct field  fmt;
524 <        int  res;
525 <        register struct field  *f;
521 >        int fd;
522 >        char *inptr;
523 >        struct field fmt;
524 >        int res;
525 >        struct field *f;
526                                                  /* check for inline format */
527          for (inptr = spec; *inptr; inptr++)
528                  if (*inptr == '$')
529                          break;
530 <        if (*inptr)                             /* inline */
530 >        if (*inptr)                     /* inline */
531                  inptr = spec;
532 <        else {                                  /* from file */
532 >        else {                          /* from file */
533                  if ((fd = open(spec, 0)) == -1) {
534                          eputs(spec);
535                          eputs(": cannot open\n");
# Line 468 | Line 549 | int  output
549                  close(fd);
550                  (inptr=inpbuf+2)[res] = '\0';
551          }
552 <        f = &fmt;                               /* get fields */
552 >        f = &fmt;                       /* get fields */
553          while ((res = readfield(&inptr)) != F_NUL) {
554                  f->next = (struct field *)emalloc(sizeof(struct field));
555                  f = f->next;
# Line 500 | Line 581 | int  output
581  
582  
583   static int
584 < readfield(                   /* get next field in format */
585 < register char  **pp
584 > readfield(              /* get next field in format */
585 > char **pp
586   )
587   {
588 <        int  type = F_NUL;
589 <        int  width = 0;
590 <        register char  *cp;
588 >        int type = F_NUL;
589 >        int width = 0;
590 >        char *cp;
591          
592          cp = inpbuf;
593          while (cp < &inpbuf[INBSIZ-1] && **pp != '\0') {
# Line 567 | Line 648 | register char  **pp
648  
649  
650   struct strvar *
651 < getsvar(                         /* get string variable */
652 < char  *svname
651 > getsvar(                        /* get string variable */
652 > char *svname
653   )
654   {
655 <        register struct strvar  *sv;
655 >        struct strvar *sv;
656          
657          for (sv = svhead; sv != NULL; sv = sv->next)
658                  if (!strcmp(sv->name, svname))
# Line 586 | Line 667 | char  *svname
667  
668  
669   static void
670 < svpreset(                    /* preset a string variable */
671 < char  *eqn
670 > svpreset(               /* preset a string variable */
671 > char *eqn
672   )
673   {
674 <        register struct strvar  *sv;
675 <        register char  *val;
674 >        struct strvar *sv;
675 >        char *val;
676  
677          for (val = eqn; *val != '='; val++)
678                  if (!*val)
# Line 610 | Line 691 | char  *eqn
691   static void
692   clearrec(void)                  /* clear input record variables */
693   {
694 <        register struct field  *f;
694 >        struct field *f;
695  
696          for (f = inpfmt; f != NULL; f = f->next)
697                  switch (f->type & F_TYP) {
# Line 628 | Line 709 | clearrec(void)                 /* clear input record variables */
709  
710  
711   static int
712 < getrec(void)                            /* get next record from file */
712 > getrec(void)                    /* get next record from file */
713   {
714 <        int  eatline;
715 <        register struct field  *f;
714 >        int eatline;
715 >        struct field *f;
716  
717          while (ipb.chr != EOF) {
718 <                if (blnkeq)             /* beware of nbsynch() */
718 >                if (blnkeq) {           /* beware of nbsynch() */
719                          while (isblnk(ipb.chr))
720 <                                resetinp();
721 <                eatline = (!igneol && ipb.chr != '\n');
720 >                                skipinp();
721 >                        if (ipb.chr == EOF)
722 >                                return(0);
723 >                }
724 >                eatline = !igneol & (ipb.chr != '\n');
725                  clearrec();             /* start with fresh record */
726                  for (f = inpfmt; f != NULL; f = f->next)
727 <                        if (getfield(f) == -1)
727 >                        if (!getfield(f))
728                                  break;
729 <                if (f == NULL) {
646 <                        advinp();       /* got one! */
729 >                if (f == NULL)          /* got one? */
730                          return(1);
731 <                }
732 <                resetinp();             /* eat false start */
650 <                if (eatline) {          /* eat rest of line */
731 >                skipinp();              /* else eat false start */
732 >                if (eatline) {          /* eat rest of line */
733                          while (ipb.chr != '\n') {
734                                  if (ipb.chr == EOF)
735                                          return(0);
736 <                                resetinp();
736 >                                skipinp();
737                          }
738 <                        resetinp();
738 >                        skipinp();
739                  }
740          }
741          return(0);
# Line 661 | Line 743 | getrec(void)                           /* get next record from file */
743  
744  
745   static int
746 < getfield(                             /* get next field */
747 < register struct field  *f
746 > getfield(                       /* get next field */
747 > struct field *f
748   )
749   {
750 <        static char  buf[RMAXWORD+1];            /* no recursion! */
751 <        int  delim, inword;
752 <        double  d;
753 <        char  *np;
754 <        register char  *cp;
750 >        static char buf[RMAXWORD+1];    /* no recursion! */
751 >        int delim, inword;
752 >        double d;
753 >        char *np;
754 >        char *cp;
755  
756          switch (f->type & F_TYP) {
757          case T_LIT:
# Line 677 | Line 759 | register struct field  *f
759                  do {
760                          if (blnkeq && isblnk(*cp)) {
761                                  if (!isblnk(ipb.chr))
762 <                                        return(-1);
762 >                                        return(0);
763                                  do
764                                          cp++;
765                                  while (isblnk(*cp));
# Line 688 | Line 770 | register struct field  *f
770                                  cp++;
771                                  scaninp();
772                          } else
773 <                                return(-1);
773 >                                return(0);
774                  } while (*cp);
775 <                return(0);
775 >                break;
776          case T_STR:
777                  if (f->next == NULL || (f->next->type & F_TYP) != T_LIT)
778                          delim = EOF;
# Line 698 | Line 780 | register struct field  *f
780                          delim = f->next->f.sl[0];
781                  cp = buf;
782                  do {
783 <                        if (ipb.chr == EOF || ipb.chr == '\n')
783 >                        if ((ipb.chr == EOF) | (ipb.chr == '\n'))
784                                  inword = 0;
785                          else if (blnkeq && delim != EOF)
786                                  inword = isblnk(delim) ?
# Line 715 | Line 797 | register struct field  *f
797                  if (f->f.sv->val == NULL)
798                          f->f.sv->val = savqstr(buf);    /* first setting */
799                  else if (strcmp(f->f.sv->val, buf))
800 <                        return(-1);                     /* doesn't match! */
801 <                return(0);
800 >                        return(0);                      /* doesn't match! */
801 >                break;
802          case T_NUM:
803                  if (f->next == NULL || (f->next->type & F_TYP) != T_LIT)
804                          delim = EOF;
# Line 746 | Line 828 | register struct field  *f
828                          varset(f->f.nv, '=', d);        /* first setting */
829                  else if ((d = (varvalue(f->f.nv)-d)/(d==0.?1.:d)) > .001
830                                  || d < -.001)
831 <                        return(-1);                     /* doesn't match! */
832 <                return(0);
831 >                        return(0);                      /* doesn't match! */
832 >                break;
833          }
834 <        return -1; /* pro forma return */
834 >        return(1);      /* success! */
835   }
836  
837  
838   static void
839 < putrec(void)                                /* output a record */
839 > putrec(void)                            /* output a record */
840   {
841 <        char  fmt[32];
842 <        register int  n;
843 <        register struct field  *f;
844 <        int  adlast, adnext;
841 >        char fmt[32], typ[16];
842 >        int n;
843 >        struct field *f;
844 >        int adlast, adnext;
845 >        double dv, av;
846          
847          adlast = 0;
848          for (f = outfmt; f != NULL; f = f->next) {
849 <                adnext =        blnkeq &&
849 >                adnext =        blnkeq &&
850                                  f->next != NULL &&
851                                  !( (f->next->type&F_TYP) == T_LIT &&
852                                          f->next->f.sl[0] == ' ' );
# Line 790 | Line 873 | putrec(void)                                /* output
873                          break;
874                  case T_NUM:
875                          n = f->type & F_WID;
876 +                        dv = evalue(f->f.ne);
877 +                        av = fabs(dv);
878 +                        if (n <= 9)
879 +                                strcpy(typ, "g");
880 +                        else
881 +                                sprintf(typ, ".%de", n-5);
882 +                        if (av < 1L<<31) {
883 +                                long    iv = (int)(av + .5);
884 +                                if (iv && fabs(av-iv) <= av*1e-14)
885 +                                        strcpy(typ, ".0f");
886 +                        }
887                          if (adlast && adnext)
888 <                                strcpy(fmt, "%g");
888 >                                sprintf(fmt, "%%%s", typ);
889                          else if (adlast)
890 <                                sprintf(fmt, "%%-%dg", n);
890 >                                sprintf(fmt, "%%-%d%s", n, typ);
891                          else
892 <                                sprintf(fmt, "%%%dg", n);
893 <                        printf(fmt, evalue(f->f.ne));
892 >                                sprintf(fmt, "%%%d%s", n, typ);
893 >                        printf(fmt, dv);
894                          adlast = 1;
895                          break;
896                  }
# Line 805 | Line 899 | putrec(void)                                /* output
899  
900  
901   static void
902 < initinp(FILE  *fp)                     /* prepare lookahead buffer */
902 > initinp(FILE *fp)               /* prepare lookahead buffer */
903  
904   {
905          ipb.fin = fp;
906          ipb.beg = ipb.end = inpbuf;
907 <        ipb.pos = inpbuf-1;             /* position before beginning */
907 >        ipb.pos = inpbuf-1;             /* position before beginning */
908          ipb.chr = '\0';
909          scaninp();
910   }
911  
912  
913   static void
914 < scaninp(void)                       /* scan next character */
914 > scaninp(void)                   /* scan next character */
915   {
916          if (ipb.chr == EOF)
917                  return;
918          if (++ipb.pos >= &inpbuf[INBSIZ])
919                  ipb.pos = inpbuf;
920 <        if (ipb.pos == ipb.end) {               /* new character */
920 >        if (ipb.pos == ipb.end) {       /* new character */
921                  if ((ipb.chr = getc(ipb.fin)) != EOF) {
922                          *ipb.end = ipb.chr;
923                          if (++ipb.end >= &inpbuf[INBSIZ])
# Line 837 | Line 931 | scaninp(void)                       /* scan next chara
931  
932  
933   static void
934 < advinp(void)                        /* move home to current position */
934 > skipinp(void)                   /* rewind position and advance 1 */
935   {
936 <        ipb.beg = ipb.pos;
843 < }
844 <
845 <
846 < static void
847 < resetinp(void)                      /* rewind position and advance 1 */
848 < {
849 <        if (ipb.beg == NULL)            /* full */
936 >        if (ipb.beg == NULL)            /* can't fully rewind? */
937                  ipb.beg = ipb.end;
938          ipb.pos = ipb.beg;
939          ipb.chr = *ipb.pos;
940          if (passive)                    /* transmit unmatched character? */
941 <                fputc(ipb.chr, stdout);
941 >                putchar(ipb.chr);
942          if (++ipb.beg >= &inpbuf[INBSIZ])
943                  ipb.beg = inpbuf;
944          scaninp();
945   }
946  
947  
948 + static void
949 + advinp(int skip)                /* advance home to current position */
950 + {
951 +        if (!skip | (passive >= 0)) {
952 +                ipb.beg = ipb.pos;      /* no need to copy input */
953 +                return;
954 +        }
955 +        if (ipb.beg == NULL) {          /* buffer overflowed a bit? */
956 +                wputs("buffer overflow\n");
957 +                puts("\n*** MISSING DATA ***");
958 +                ipb.beg = ipb.end;
959 +        }
960 +        while (ipb.beg != ipb.pos) {    /* copy buffer to current */
961 +                putchar(*ipb.beg);
962 +                if (++ipb.beg >= &inpbuf[INBSIZ])
963 +                        ipb.beg = inpbuf;
964 +        }
965 + }
966 +
967 +
968   void
969 < eputs(char  *msg)
969 > eputs(const char *msg)
970   {
971          fputs(msg, stderr);
972   }
973  
974  
975   void
976 < wputs(char  *msg)
976 > wputs(const char *msg)
977   {
978          if (!nowarn)
979                  eputs(msg);
# Line 874 | Line 981 | wputs(char  *msg)
981  
982  
983   void
984 < quit(int  code)
984 > quit(int code)
985   {
986          exit(code);
987   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines