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.30 by greg, Tue Dec 10 19:00:26 2019 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  <math.h>
12 < #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)=='.') \
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 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 < long  incnt = 0;                /* limit number of input records? */
77 < long  outcnt = 0;               /* limit number of output records? */
76 > long incnt = 0;                 /* limit number of input records? */
77 > long outcnt = 0;                /* limit number of output records? */
78  
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 */
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) */
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 <        char  *fpath;
111 <        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 128 | 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 152 | Line 155 | char  *argv[]
155                          break;
156                  case 'n':
157                          noinput = 1;
158 +                        esupport &= ~E_RCONST;
159                          break;
160                  case 'i':
161                          switch (argv[i][2]) {
# Line 229 | 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          if (otype != 'a')
# Line 237 | Line 241 | eputs(" [-b][-l][-n][-p][-w][-u][-tS][-s svar=sval][-e
241   #ifdef getc_unlocked            /* avoid lock/unlock overhead */
242          flockfile(stdout);
243   #endif
244 <        if (noinput) {          /* produce a single output record */
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");
# Line 247 | Line 251 | eputs(" [-b][-l][-n][-p][-w][-u][-tS][-s svar=sval][-e
251                  putout();
252                  quit(0);
253          }
254 <        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          
# Line 262 | Line 271 | eputs(" [-b][-l][-n][-p][-w][-u][-tS][-s svar=sval][-e
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 275 | 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   {
281        if (inpfmt != NULL)
282                return(getrec());
290          if ((itype == 'd') | (itype == 'D')) {
291                  if (getbinary(inpbuf, sizeof(double), nbicols, fp) != nbicols)
292                          return(0);
# Line 299 | Line 306 | FILE  *fp
306  
307  
308   static void
309 < execute(           /* process a file */
310 < char  *file
309 > execute(        /* process a file */
310 > char *file
311   )
312   {
313 <        const int  conditional = vardefined("cond");
314 <        const int  set_recno = (varlookup("recno") != NULL);
315 <        const int  set_outno = (varlookup("outno") != NULL);
316 <        long  nrecs = 0;
317 <        long  nout = 0;
318 <        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 322 | Line 332 | char  *file
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)) {
343 >        while (inpfmt != NULL ? getrec() : getinputrec(fp)) {
344                  ++nrecs;
345                  if (set_recno)
346 <                        varset("recno", '=', (double)nrecs);
346 >                        varset(recnoVN, '=', (double)nrecs);
347                  if (set_outno)
348 <                        varset("outno", '=', (double)(nout+1));
348 >                        varset(outnoVN, '=', (double)(nout+1));
349                  colflg = 0;
350                  eclock++;
351 <                if (!conditional || varvalue("cond") > 0.0) {
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;
# Line 347 | Line 366 | char  *file
366  
367  
368   static void
369 < putout(void)                /* produce an output record */
369 > putout(void)            /* produce an output record */
370   {
371  
372          colpos = 0;
# Line 367 | 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 <        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 395 | 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 <        char  *cp;
421 >        int i;
422 >        char *cp;
423  
424          if (noinput || inpfmt != NULL) {
425                  eputs("no column input\n");
# Line 434 | 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 446 | 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 462 | 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)];
# Line 492 | Line 511 | double  v
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 <        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");
# Line 528 | Line 547 | int  output
547                  close(fd);
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 560 | Line 579 | int  output
579  
580  
581   static int
582 < readfield(                   /* get next field in format */
583 < char  **pp
582 > readfield(              /* get next field in format */
583 > char **pp
584   )
585   {
586 <        int  type = F_NUL;
587 <        int  width = 0;
588 <        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 627 | Line 646 | 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 <        struct strvar  *sv;
653 >        struct strvar *sv;
654          
655          for (sv = svhead; sv != NULL; sv = sv->next)
656                  if (!strcmp(sv->name, svname))
# Line 646 | 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 <        struct strvar  *sv;
673 <        char  *val;
672 >        struct strvar *sv;
673 >        char *val;
674  
675          for (val = eqn; *val != '='; val++)
676                  if (!*val)
# Line 670 | Line 689 | char  *eqn
689   static void
690   clearrec(void)                  /* clear input record variables */
691   {
692 <        struct field  *f;
692 >        struct field *f;
693  
694          for (f = inpfmt; f != NULL; f = f->next)
695                  switch (f->type & F_TYP) {
# Line 688 | 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 <        struct field  *f;
712 >        int eatline;
713 >        struct field *f;
714  
715          while (ipb.chr != EOF) {
716                  if (blnkeq) {           /* beware of nbsynch() */
717                          while (isblnk(ipb.chr))
718 <                                resetinp();
718 >                                skipinp();
719                          if (ipb.chr == EOF)
720                                  return(0);
721                  }
# Line 705 | Line 724 | getrec(void)                           /* get next record from file */
724                  for (f = inpfmt; f != NULL; f = f->next)
725                          if (!getfield(f))
726                                  break;
727 <                if (f == NULL) {
709 <                        advinp();       /* got one! */
727 >                if (f == NULL)          /* got one? */
728                          return(1);
729 <                }
730 <                resetinp();             /* eat false start */
713 <                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 <                                resetinp();
734 >                                skipinp();
735                          }
736 <                        resetinp();
736 >                        skipinp();
737                  }
738          }
739          return(0);
# Line 724 | Line 741 | getrec(void)                           /* get next record from file */
741  
742  
743   static int
744 < getfield(                             /* get next field */
745 < 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 <        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 817 | Line 834 | struct field  *f
834  
835  
836   static void
837 < putrec(void)                                /* output a record */
837 > putrec(void)                            /* output a record */
838   {
839 <        char  fmt[32], typ[16];
840 <        int  n;
841 <        struct field  *f;
842 <        int  adlast, adnext;
843 <        double  dv, av;
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 880 | 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 912 | 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;
918 < }
919 <
920 <
921 < static void
922 < resetinp(void)                      /* rewind position and advance 1 */
923 < {
924 <        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 <                fputc(ipb.chr, stdout);
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 949 | 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