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.21 by greg, Sun Jun 14 00:33:16 2009 UTC vs.
Revision 1.37 by greg, Mon Feb 6 22:40:21 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  <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  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 */
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 127 | 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 136 | 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);
# Line 151 | Line 163 | char  *argv[]
163                                  nbicols = 0;
164                                  readfmt(argv[++i], 0);
165                                  break;
166 +                        case 'n':
167 +                                incnt = atol(argv[++i]);
168 +                                break;
169                          case 'a':
170                                  itype = 'a';
171                                  nbicols = 0;
# Line 191 | Line 206 | char  *argv[]
206                                  otype = 'a';
207                                  readfmt(argv[++i], 1);
208                                  break;
209 +                        case 'n':
210 +                                outcnt = atol(argv[++i]);
211 +                                break;
212                          case 'a':
213                                  otype = 'a';
214                                  break;
# Line 214 | Line 232 | char  *argv[]
232                  userr:
233                          eputs("Usage: ");
234                          eputs(argv[0]);
235 < eputs(" [-b][-l][-n][-p][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n");
235 > eputs(" [-b][-l][-n][-p|-P][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n");
236                          quit(1);
237                  }
238          if (otype != 'a')
239                  SET_FILE_BINARY(stdout);
240 <        if (noinput) {          /* produce a single output record */
240 > #ifdef getc_unlocked            /* avoid lock/unlock overhead */
241 >        flockfile(stdout);
242 > #endif
243 >        if (noinput) {  /* produce a single output record */
244                  if (i < argc) {
245                          eputs(argv[0]);
246                          eputs(": file argument(s) incompatible with -n\n");
# Line 229 | Line 250 | eputs(" [-b][-l][-n][-p][-w][-u][-tS][-s svar=sval][-e
250                  putout();
251                  quit(0);
252          }
253 <        if (itype != 'a')
254 <                SET_FILE_BINARY(stdin);
255 <
256 <        if (blnkeq)             /* for efficiency */
253 >        if (passive && (inpfmt == NULL) | (outfmt == NULL)) {
254 >                eputs(argv[0]);
255 >                eputs(": options -p and -P require -i and -o formats\n");
256 >                quit(1);
257 >        }
258 >        if (blnkeq)     /* for efficiency */
259                  nbsynch();
260  
261 <        if (i == argc)          /* from stdin */
261 >        if (i == argc)  /* from stdin */
262                  execute(NULL);
263 <        else                    /* from one or more files */
263 >        else            /* from one or more files */
264                  for ( ; i < argc; i++)
265                          execute(argv[i]);
266          
# Line 247 | Line 270 | eputs(" [-b][-l][-n][-p][-w][-u][-tS][-s svar=sval][-e
270  
271  
272   static void
273 < nbsynch(void)               /* non-blank starting synch character */
273 > nbsynch(void)   /* non-blank starting synch character */
274   {
275          if (inpfmt == NULL || (inpfmt->type & F_TYP) != T_LIT)
276                  return;
# Line 260 | Line 283 | nbsynch(void)               /* non-blank starting sync
283  
284   static int
285   getinputrec(            /* get next input record */
286 < FILE  *fp
286 > FILE *fp
287   )
288   {
289 <        if (inpfmt != NULL)
290 <                return(getrec());
268 <        if (tolower(itype) == 'd') {
269 <                if (fread(inpbuf, sizeof(double), nbicols, fp) != nbicols)
289 >        if ((itype == 'd') | (itype == 'D')) {
290 >                if (getbinary(inpbuf, sizeof(double), nbicols, fp) != nbicols)
291                          return(0);
292                  if (itype == 'D')
293                          swap64(inpbuf, nbicols);
294                  return(1);
295          }
296 <        if (tolower(itype) == 'f') {
297 <                if (fread(inpbuf, sizeof(float), nbicols, fp) != nbicols)
296 >        if ((itype == 'f') | (itype == 'F')) {
297 >                if (getbinary(inpbuf, sizeof(float), nbicols, fp) != nbicols)
298                          return(0);
299                  if (itype == 'F')
300                          swap32(inpbuf, nbicols);
# Line 284 | Line 305 | FILE  *fp
305  
306  
307   static void
308 < execute(           /* process a file */
309 < char  *file
308 > execute(        /* process a file */
309 > char *file
310   )
311   {
312 <        int  conditional = vardefined("cond");
313 <        long  nrecs = 0;
314 <        long  nout = 0;
315 <        FILE  *fp;
312 >        static char     condVN[] = "cond";
313 >        static char     recnoVN[] = "recno";
314 >        static char     outnoVN[] = "outno";
315 >        const int       conditional = vardefined(condVN);
316 >        const int       set_recno = (varlookup(recnoVN) != NULL);
317 >        const int       set_outno = (varlookup(outnoVN) != NULL);
318 >        long            nrecs = 0;
319 >        long            nout = 0;
320 >        FILE            *fp;
321          
322          if (file == NULL)
323                  fp = stdin;
# Line 300 | Line 326 | char  *file
326                  eputs(": cannot open\n");
327                  quit(1);
328          }
329 +        if (itype != 'a')
330 +                SET_FILE_BINARY(fp);
331 + #ifdef getc_unlocked            /* avoid lock/unlock overhead */
332 +        flockfile(fp);
333 + #endif
334 +        if (conditional == ':') {
335 +                eputs(condVN);
336 +                eputs(": defined as constant\n");
337 +                quit(1);
338 +        }
339          if (inpfmt != NULL)
340                  initinp(fp);
341          
342 <        while (getinputrec(fp)) {
343 <                varset("recno", '=', (double)++nrecs);
344 <                varset("outno", '=', (double)(nout+1));
342 >        while (inpfmt != NULL ? getrec() : getinputrec(fp)) {
343 >                ++nrecs;
344 >                if (set_recno)
345 >                        varset(recnoVN, '=', (double)nrecs);
346 >                if (set_outno)
347 >                        varset(outnoVN, '=', (double)(nout+1));
348                  colflg = 0;
349                  eclock++;
350 <                if (!conditional || varvalue("cond") > 0.0) {
350 >                if (!conditional || varvalue(condVN) > 0.0) {
351 >                        if (inpfmt != NULL)
352 >                                advinp(0);
353                          putout();
354                          ++nout;
355 +                } else if (inpfmt != NULL) {
356 +                        advinp(1);
357                  }
358 +                if (incnt && nrecs >= incnt)
359 +                        break;
360 +                if (outcnt && nout >= outcnt)
361 +                        break;
362          }
363          fclose(fp);
364   }
365  
366  
367   static void
368 < putout(void)                /* produce an output record */
368 > putout(void)            /* produce an output record */
369   {
370  
371          colpos = 0;
# Line 338 | Line 385 | putout(void)                /* produce an output recor
385   static double
386   l_in(char *funame)      /* function call for $channel */
387   {
388 <        int  n;
389 <        register char  *cp;
388 >        int n;
389 >        char *cp;
390                          /* get argument as integer */
391          n = (int)(argument(1) + .5);
392          if (n != 0)     /* return channel value */
# Line 366 | Line 413 | l_in(char *funame)     /* function call for $channel */
413   }
414  
415   double
416 < chanvalue(            /* return value for column n */
417 < int  n
416 > chanvalue(      /* return value for column n */
417 > int n
418   )
419   {
420 <        int  i;
421 <        register char  *cp;
420 >        int i;
421 >        char *cp;
422  
423          if (noinput || inpfmt != NULL) {
424                  eputs("no column input\n");
# Line 384 | Line 431 | int  n
431          if (nbicols) {
432                  if (n > nbicols)
433                          return(0.0);
434 <                if (tolower(itype) == 'd') {
434 >                if ((itype == 'd') | (itype == 'D')) {
435                          cp = inpbuf + (n-1)*sizeof(double);
436                          return(*(double *)cp);
437                  }
# Line 405 | Line 452 | int  n
452                          while (*cp && *cp++ != sepchar)
453                                  ;
454  
455 <        while (isspace(*cp))            /* some atof()'s don't like tabs */
455 >        while (isspace(*cp))    /* some atof()'s don't like tabs */
456                  cp++;
457  
458          if (n <= MAXCOL) {
# Line 417 | Line 464 | int  n
464  
465  
466   void
467 < chanset(                   /* output column n */
468 < int  n,
469 < double  v
467 > chanset(                /* output column n */
468 > int n,
469 > double v
470   )
471   {
472 <        if (colpos == 0)                /* no leading separator */
472 >        if (colpos == 0)                /* no leading separator */
473                  colpos = 1;
474          while (colpos < n) {
475                  putchar(sepchar);
# Line 433 | Line 480 | double  v
480  
481  
482   void
483 < bchanset(                   /* output binary channel n */
484 < int  n,
485 < double  v
483 > bchanset(               /* output binary channel n */
484 > int n,
485 > double v
486   )
487   {
488          static char     zerobuf[sizeof(double)];
489 +        const int       otlen = ((otype == 'd') | (otype == 'D')) ?
490 +                                        sizeof(double) : sizeof(float);
491          float   fval = v;
492  
493          while (++colpos < n)
494 <                fwrite(zerobuf,
446 <                        tolower(otype)=='d' ? sizeof(double) : sizeof(float),
447 <                        1, stdout);
494 >                putbinary(zerobuf, otlen, 1, stdout);
495          switch (otype) {
496          case 'D':
497                  swap64((char *)&v, 1);
498                  /* fall through */
499          case 'd':
500 <                fwrite(&v, sizeof(double), 1, stdout);
500 >                putbinary(&v, sizeof(double), 1, stdout);
501                  break;
502          case 'F':
503                  swap32((char *)&fval, 1);
504                  /* fall through */
505          case 'f':
506 <                fwrite(&fval, sizeof(float), 1, stdout);
506 >                putbinary(&fval, sizeof(float), 1, stdout);
507                  break;
508          }
509   }
510  
511  
512   static void
513 < readfmt(                   /* read record format */
514 < char  *spec,
515 < int  output
513 > readfmt(                /* read record format */
514 > char *spec,
515 > int output
516   )
517   {
518 <        int  fd;
519 <        char  *inptr;
520 <        struct field  fmt;
521 <        int  res;
522 <        register struct field  *f;
518 >        int fd;
519 >        char *inptr;
520 >        struct field fmt;
521 >        int res;
522 >        struct field *f;
523                                                  /* check for inline format */
524          for (inptr = spec; *inptr; inptr++)
525                  if (*inptr == '$')
526                          break;
527 <        if (*inptr)                             /* inline */
527 >        if (*inptr)                     /* inline */
528                  inptr = spec;
529 <        else {                                  /* from file */
529 >        else {                          /* from file */
530                  if ((fd = open(spec, 0)) == -1) {
531                          eputs(spec);
532                          eputs(": cannot open\n");
# Line 499 | Line 546 | int  output
546                  close(fd);
547                  (inptr=inpbuf+2)[res] = '\0';
548          }
549 <        f = &fmt;                               /* get fields */
549 >        f = &fmt;                       /* get fields */
550          while ((res = readfield(&inptr)) != F_NUL) {
551                  f->next = (struct field *)emalloc(sizeof(struct field));
552                  f = f->next;
# Line 531 | Line 578 | int  output
578  
579  
580   static int
581 < readfield(                   /* get next field in format */
582 < register char  **pp
581 > readfield(              /* get next field in format */
582 > char **pp
583   )
584   {
585 <        int  type = F_NUL;
586 <        int  width = 0;
587 <        register char  *cp;
585 >        int type = F_NUL;
586 >        int width = 0;
587 >        char *cp;
588          
589          cp = inpbuf;
590          while (cp < &inpbuf[INBSIZ-1] && **pp != '\0') {
# Line 598 | Line 645 | register char  **pp
645  
646  
647   struct strvar *
648 < getsvar(                         /* get string variable */
649 < char  *svname
648 > getsvar(                        /* get string variable */
649 > char *svname
650   )
651   {
652 <        register struct strvar  *sv;
652 >        struct strvar *sv;
653          
654          for (sv = svhead; sv != NULL; sv = sv->next)
655                  if (!strcmp(sv->name, svname))
# Line 617 | Line 664 | char  *svname
664  
665  
666   static void
667 < svpreset(                    /* preset a string variable */
668 < char  *eqn
667 > svpreset(               /* preset a string variable */
668 > char *eqn
669   )
670   {
671 <        register struct strvar  *sv;
672 <        register char  *val;
671 >        struct strvar *sv;
672 >        char *val;
673  
674          for (val = eqn; *val != '='; val++)
675                  if (!*val)
# Line 641 | Line 688 | char  *eqn
688   static void
689   clearrec(void)                  /* clear input record variables */
690   {
691 <        register struct field  *f;
691 >        struct field *f;
692  
693          for (f = inpfmt; f != NULL; f = f->next)
694                  switch (f->type & F_TYP) {
# Line 659 | Line 706 | clearrec(void)                 /* clear input record variables */
706  
707  
708   static int
709 < getrec(void)                            /* get next record from file */
709 > getrec(void)                    /* get next record from file */
710   {
711 <        int  eatline;
712 <        register struct field  *f;
711 >        int eatline;
712 >        struct field *f;
713  
714          while (ipb.chr != EOF) {
715                  if (blnkeq) {           /* beware of nbsynch() */
716                          while (isblnk(ipb.chr))
717 <                                resetinp();
717 >                                skipinp();
718                          if (ipb.chr == EOF)
719                                  return(0);
720                  }
721 <                eatline = (!igneol && ipb.chr != '\n');
721 >                eatline = !igneol & (ipb.chr != '\n');
722                  clearrec();             /* start with fresh record */
723                  for (f = inpfmt; f != NULL; f = f->next)
724 <                        if (getfield(f) == -1)
724 >                        if (!getfield(f))
725                                  break;
726 <                if (f == NULL) {
680 <                        advinp();       /* got one! */
726 >                if (f == NULL)          /* got one? */
727                          return(1);
728 <                }
729 <                resetinp();             /* eat false start */
684 <                if (eatline) {          /* eat rest of line */
728 >                skipinp();              /* else eat false start */
729 >                if (eatline) {          /* eat rest of line */
730                          while (ipb.chr != '\n') {
731                                  if (ipb.chr == EOF)
732                                          return(0);
733 <                                resetinp();
733 >                                skipinp();
734                          }
735 <                        resetinp();
735 >                        skipinp();
736                  }
737          }
738          return(0);
# Line 695 | Line 740 | getrec(void)                           /* get next record from file */
740  
741  
742   static int
743 < getfield(                             /* get next field */
744 < register struct field  *f
743 > getfield(                       /* get next field */
744 > struct field *f
745   )
746   {
747 <        static char  buf[RMAXWORD+1];            /* no recursion! */
748 <        int  delim, inword;
749 <        double  d;
750 <        char  *np;
751 <        register char  *cp;
747 >        static char buf[RMAXWORD+1];    /* no recursion! */
748 >        int delim, inword;
749 >        double d;
750 >        char *np;
751 >        char *cp;
752  
753          switch (f->type & F_TYP) {
754          case T_LIT:
# Line 711 | Line 756 | register struct field  *f
756                  do {
757                          if (blnkeq && isblnk(*cp)) {
758                                  if (!isblnk(ipb.chr))
759 <                                        return(-1);
759 >                                        return(0);
760                                  do
761                                          cp++;
762                                  while (isblnk(*cp));
# Line 722 | Line 767 | register struct field  *f
767                                  cp++;
768                                  scaninp();
769                          } else
770 <                                return(-1);
770 >                                return(0);
771                  } while (*cp);
772 <                return(0);
772 >                break;
773          case T_STR:
774                  if (f->next == NULL || (f->next->type & F_TYP) != T_LIT)
775                          delim = EOF;
# Line 732 | Line 777 | register struct field  *f
777                          delim = f->next->f.sl[0];
778                  cp = buf;
779                  do {
780 <                        if (ipb.chr == EOF || ipb.chr == '\n')
780 >                        if ((ipb.chr == EOF) | (ipb.chr == '\n'))
781                                  inword = 0;
782                          else if (blnkeq && delim != EOF)
783                                  inword = isblnk(delim) ?
# Line 749 | Line 794 | register struct field  *f
794                  if (f->f.sv->val == NULL)
795                          f->f.sv->val = savqstr(buf);    /* first setting */
796                  else if (strcmp(f->f.sv->val, buf))
797 <                        return(-1);                     /* doesn't match! */
798 <                return(0);
797 >                        return(0);                      /* doesn't match! */
798 >                break;
799          case T_NUM:
800                  if (f->next == NULL || (f->next->type & F_TYP) != T_LIT)
801                          delim = EOF;
# Line 780 | Line 825 | register struct field  *f
825                          varset(f->f.nv, '=', d);        /* first setting */
826                  else if ((d = (varvalue(f->f.nv)-d)/(d==0.?1.:d)) > .001
827                                  || d < -.001)
828 <                        return(-1);                     /* doesn't match! */
829 <                return(0);
828 >                        return(0);                      /* doesn't match! */
829 >                break;
830          }
831 <        return -1; /* pro forma return */
831 >        return(1);      /* success! */
832   }
833  
834  
835   static void
836 < putrec(void)                                /* output a record */
836 > putrec(void)                            /* output a record */
837   {
838 <        char  fmt[32];
839 <        register int  n;
840 <        register struct field  *f;
841 <        int  adlast, adnext;
838 >        char fmt[32], typ[16];
839 >        int n;
840 >        struct field *f;
841 >        int adlast, adnext;
842 >        double dv, av;
843          
844          adlast = 0;
845          for (f = outfmt; f != NULL; f = f->next) {
846 <                adnext =        blnkeq &&
846 >                adnext =        blnkeq &&
847                                  f->next != NULL &&
848                                  !( (f->next->type&F_TYP) == T_LIT &&
849                                          f->next->f.sl[0] == ' ' );
# Line 824 | Line 870 | putrec(void)                                /* output
870                          break;
871                  case T_NUM:
872                          n = f->type & F_WID;
873 +                        dv = evalue(f->f.ne);
874 +                        av = fabs(dv);
875 +                        if (n <= 9)
876 +                                strcpy(typ, "g");
877 +                        else
878 +                                sprintf(typ, ".%de", n-5);
879 +                        if (av < 1L<<31) {
880 +                                long    iv = (int)(av + .5);
881 +                                if (iv && fabs(av-iv) <= av*1e-14)
882 +                                        strcpy(typ, ".0f");
883 +                        }
884                          if (adlast && adnext)
885 <                                strcpy(fmt, "%g");
885 >                                sprintf(fmt, "%%%s", typ);
886                          else if (adlast)
887 <                                sprintf(fmt, "%%-%dg", n);
887 >                                sprintf(fmt, "%%-%d%s", n, typ);
888                          else
889 <                                sprintf(fmt, "%%%dg", n);
890 <                        printf(fmt, evalue(f->f.ne));
889 >                                sprintf(fmt, "%%%d%s", n, typ);
890 >                        printf(fmt, dv);
891                          adlast = 1;
892                          break;
893                  }
# Line 839 | Line 896 | putrec(void)                                /* output
896  
897  
898   static void
899 < initinp(FILE  *fp)                     /* prepare lookahead buffer */
899 > initinp(FILE *fp)               /* prepare lookahead buffer */
900  
901   {
902          ipb.fin = fp;
903          ipb.beg = ipb.end = inpbuf;
904 <        ipb.pos = inpbuf-1;             /* position before beginning */
904 >        ipb.pos = inpbuf-1;             /* position before beginning */
905          ipb.chr = '\0';
906          scaninp();
907   }
908  
909  
910   static void
911 < scaninp(void)                       /* scan next character */
911 > scaninp(void)                   /* scan next character */
912   {
913          if (ipb.chr == EOF)
914                  return;
915          if (++ipb.pos >= &inpbuf[INBSIZ])
916                  ipb.pos = inpbuf;
917 <        if (ipb.pos == ipb.end) {               /* new character */
917 >        if (ipb.pos == ipb.end) {       /* new character */
918                  if ((ipb.chr = getc(ipb.fin)) != EOF) {
919                          *ipb.end = ipb.chr;
920                          if (++ipb.end >= &inpbuf[INBSIZ])
# Line 871 | Line 928 | scaninp(void)                       /* scan next chara
928  
929  
930   static void
931 < advinp(void)                        /* move home to current position */
931 > skipinp(void)                   /* rewind position and advance 1 */
932   {
933 <        ipb.beg = ipb.pos;
877 < }
878 <
879 <
880 < static void
881 < resetinp(void)                      /* rewind position and advance 1 */
882 < {
883 <        if (ipb.beg == NULL)            /* full */
933 >        if (ipb.beg == NULL)            /* can't fully rewind? */
934                  ipb.beg = ipb.end;
935          ipb.pos = ipb.beg;
936          ipb.chr = *ipb.pos;
937          if (passive)                    /* transmit unmatched character? */
938 <                fputc(ipb.chr, stdout);
938 >                putchar(ipb.chr);
939          if (++ipb.beg >= &inpbuf[INBSIZ])
940                  ipb.beg = inpbuf;
941          scaninp();
942   }
943  
944  
945 + static void
946 + advinp(int skip)                /* advance home to current position */
947 + {
948 +        if (!skip | (passive >= 0)) {
949 +                ipb.beg = ipb.pos;      /* no need to copy input */
950 +                return;
951 +        }
952 +        if (ipb.beg == NULL) {          /* buffer overflowed a bit? */
953 +                wputs("buffer overflow\n");
954 +                puts("\n*** MISSING DATA ***");
955 +                ipb.beg = ipb.end;
956 +        }
957 +        while (ipb.beg != ipb.pos) {    /* copy buffer to current */
958 +                putchar(*ipb.beg);
959 +                if (++ipb.beg >= &inpbuf[INBSIZ])
960 +                        ipb.beg = inpbuf;
961 +        }
962 + }
963 +
964 +
965   void
966 < eputs(char  *msg)
966 > eputs(const char *msg)
967   {
968          fputs(msg, stderr);
969   }
970  
971  
972   void
973 < wputs(char  *msg)
973 > wputs(const char *msg)
974   {
975          if (!nowarn)
976                  eputs(msg);
# Line 908 | Line 978 | wputs(char  *msg)
978  
979  
980   void
981 < quit(int  code)
981 > quit(int code)
982   {
983          exit(code);
984   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines