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.33 by greg, Sat Mar 12 15:50:13 2022 UTC vs.
Revision 1.38 by greg, Fri Mar 10 17:38:01 2023 UTC

# Line 22 | Line 22 | static const char RCSid[] = "$Id$";
22  
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 */
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)
56  
57   static int getinputrec(FILE *fp);
58 < static void scaninp(void), advinp(void), passinp(void), skipinp(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 93 | Line 93 | 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
# Line 155 | 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 240 | Line 241 | eputs(" [-b][-l][-n][-p|-P][-w][-u][-tS][-s svar=sval]
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 255 | Line 256 | eputs(" [-b][-l][-n][-p|-P][-w][-u][-tS][-s svar=sval]
256                  eputs(": options -p and -P require -i and -o formats\n");
257                  quit(1);
258          }
259 <        if (blnkeq)      /* for efficiency */
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 270 | Line 271 | eputs(" [-b][-l][-n][-p|-P][-w][-u][-tS][-s svar=sval]
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 305 | Line 306 | FILE *fp
306  
307  
308   static void
309 < execute(         /* process a file */
309 > execute(        /* process a file */
310   char *file
311   )
312   {
# Line 348 | Line 349 | char *file
349                  colflg = 0;
350                  eclock++;
351                  if (!conditional || varvalue(condVN) > 0.0) {
352 +                        if (inpfmt != NULL)
353 +                                advinp(0);
354                          putout();
355                          ++nout;
353                        advinp();
356                  } else if (inpfmt != NULL) {
357 <                        if (passive < 0)
356 <                                passinp();
357 <                        else
358 <                                advinp();
357 >                        advinp(1);
358                  }
359                  if (incnt && nrecs >= incnt)
360                          break;
# Line 415 | Line 414 | l_in(char *funame)     /* function call for $channel */
414   }
415  
416   double
417 < chanvalue(       /* return value for column n */
417 > chanvalue(      /* return value for column n */
418   int n
419   )
420   {
# Line 454 | 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 466 | Line 465 | int n
465  
466  
467   void
468 < chanset(                 /* output column n */
468 > chanset(                /* output column n */
469   int n,
470   double v
471   )
# Line 482 | Line 481 | double v
481  
482  
483   void
484 < bchanset(                /* output binary channel n */
484 > bchanset(               /* output binary channel n */
485   int n,
486   double v
487   )
# Line 512 | Line 511 | double v
511  
512  
513   static void
514 < readfmt(                 /* read record format */
514 > readfmt(                /* read record format */
515   char *spec,
516   int output
517   )
# Line 526 | Line 525 | int output
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 548 | 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 580 | Line 579 | int output
579  
580  
581   static int
582 < readfield(               /* get next field in format */
582 > readfield(              /* get next field in format */
583   char **pp
584   )
585   {
# Line 647 | Line 646 | char **pp
646  
647  
648   struct strvar *
649 < getsvar(                         /* get string variable */
649 > getsvar(                        /* get string variable */
650   char *svname
651   )
652   {
# Line 666 | Line 665 | char *svname
665  
666  
667   static void
668 < svpreset(                /* preset a string variable */
668 > svpreset(               /* preset a string variable */
669   char *eqn
670   )
671   {
# Line 742 | Line 741 | getrec(void)                   /* get next record from file */
741  
742  
743   static int
744 < getfield(                        /* get next field */
744 > getfield(                       /* get next field */
745   struct field *f
746   )
747   {
748 <        static char buf[RMAXWORD+1];     /* no recursion! */
748 >        static char buf[RMAXWORD+1];    /* no recursion! */
749          int delim, inword;
750          double d;
751          char *np;
# Line 898 | Line 897 | putrec(void)                           /* output a record */
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;
# Line 916 | Line 915 | scaninp(void)                  /* scan next character */
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 930 | Line 929 | scaninp(void)                  /* scan next character */
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;
934 >        if (ipb.beg == NULL)            /* can't fully rewind? */
935 >                ipb.beg = ipb.end;
936 >        ipb.pos = ipb.beg;
937 >        ipb.chr = *ipb.pos;
938 >        if (passive)                    /* transmit unmatched character? */
939 >                putchar(ipb.chr);
940 >        if (++ipb.beg >= &inpbuf[INBSIZ])
941 >                ipb.beg = inpbuf;
942 >        scaninp();
943   }
944  
945  
946   static void
947 < passinp(void)                   /* pass beginning to current position */
947 > advinp(int skip)                /* advance home to current position */
948   {
949 <        if (!passive | (ipb.beg == NULL)) {
950 <                advinp();
949 >        if (!skip | (passive >= 0)) {
950 >                ipb.beg = ipb.pos;      /* no need to copy input */
951                  return;
952          }
953 <        while (ipb.beg != ipb.pos) {    /* transfer buffer unaltered */
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;
# Line 951 | Line 963 | passinp(void)                  /* pass beginning to current position
963   }
964  
965  
954 static void
955 skipinp(void)                   /* rewind position and advance 1 */
956 {
957        if (ipb.beg == NULL)            /* full */
958                ipb.beg = ipb.end;
959        ipb.pos = ipb.beg;
960        ipb.chr = *ipb.pos;
961        if (passive)                    /* transmit unmatched character? */
962                putchar(ipb.chr);
963        if (++ipb.beg >= &inpbuf[INBSIZ])
964                ipb.beg = inpbuf;
965        scaninp();
966 }
967
968
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);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines