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.32 by greg, Fri Mar 11 22:50:13 2022 UTC vs.
Revision 1.37 by greg, Mon Feb 6 22:40:21 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 240 | Line 240 | eputs(" [-b][-l][-n][-p|-P][-w][-u][-tS][-s svar=sval]
240   #ifdef getc_unlocked            /* avoid lock/unlock overhead */
241          flockfile(stdout);
242   #endif
243 <        if (noinput) {   /* produce a single output record */
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 255 | Line 255 | eputs(" [-b][-l][-n][-p|-P][-w][-u][-tS][-s svar=sval]
255                  eputs(": options -p and -P require -i and -o formats\n");
256                  quit(1);
257          }
258 <        if (blnkeq)      /* for efficiency */
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 270 | Line 270 | eputs(" [-b][-l][-n][-p|-P][-w][-u][-tS][-s svar=sval]
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 305 | Line 305 | FILE *fp
305  
306  
307   static void
308 < execute(         /* process a file */
308 > execute(        /* process a file */
309   char *file
310   )
311   {
312 <        const int conditional = vardefined("cond");
313 <        const int set_recno = (varlookup("recno") != NULL);
314 <        const int set_outno = (varlookup("outno") != NULL);
315 <        long nrecs = 0;
316 <        long nout = 0;
317 <        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 328 | Line 331 | char *file
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 (inpfmt != NULL ? getrec() : getinputrec(fp)) {
343                  ++nrecs;
344                  if (set_recno)
345 <                        varset("recno", '=', (double)nrecs);
345 >                        varset(recnoVN, '=', (double)nrecs);
346                  if (set_outno)
347 <                        varset("outno", '=', (double)(nout+1));
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;
345                        advinp();
355                  } else if (inpfmt != NULL) {
356 <                        if (passive < 0)
348 <                                passinp();
349 <                        else
350 <                                advinp();
356 >                        advinp(1);
357                  }
358                  if (incnt && nrecs >= incnt)
359                          break;
# Line 407 | Line 413 | l_in(char *funame)     /* function call for $channel */
413   }
414  
415   double
416 < chanvalue(       /* return value for column n */
416 > chanvalue(      /* return value for column n */
417   int n
418   )
419   {
# Line 446 | 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 458 | Line 464 | int n
464  
465  
466   void
467 < chanset(                 /* output column n */
467 > chanset(                /* output column n */
468   int n,
469   double v
470   )
# Line 474 | Line 480 | double v
480  
481  
482   void
483 < bchanset(                /* output binary channel n */
483 > bchanset(               /* output binary channel n */
484   int n,
485   double v
486   )
# Line 504 | Line 510 | double v
510  
511  
512   static void
513 < readfmt(                 /* read record format */
513 > readfmt(                /* read record format */
514   char *spec,
515   int output
516   )
# Line 518 | Line 524 | int output
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 540 | 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 572 | Line 578 | int output
578  
579  
580   static int
581 < readfield(               /* get next field in format */
581 > readfield(              /* get next field in format */
582   char **pp
583   )
584   {
# Line 639 | Line 645 | char **pp
645  
646  
647   struct strvar *
648 < getsvar(                         /* get string variable */
648 > getsvar(                        /* get string variable */
649   char *svname
650   )
651   {
# Line 658 | Line 664 | char *svname
664  
665  
666   static void
667 < svpreset(                /* preset a string variable */
667 > svpreset(               /* preset a string variable */
668   char *eqn
669   )
670   {
# Line 734 | Line 740 | getrec(void)                   /* get next record from file */
740  
741  
742   static int
743 < getfield(                        /* get next field */
743 > getfield(                       /* get next field */
744   struct field *f
745   )
746   {
747 <        static char buf[RMAXWORD+1];     /* no recursion! */
747 >        static char buf[RMAXWORD+1];    /* no recursion! */
748          int delim, inword;
749          double d;
750          char *np;
# Line 890 | Line 896 | putrec(void)                           /* output a record */
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;
# Line 908 | Line 914 | scaninp(void)                  /* scan next character */
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 922 | Line 928 | scaninp(void)                  /* scan next character */
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;
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 >                putchar(ipb.chr);
939 >        if (++ipb.beg >= &inpbuf[INBSIZ])
940 >                ipb.beg = inpbuf;
941 >        scaninp();
942   }
943  
944  
945   static void
946 < passinp(void)                   /* pass beginning to current position */
946 > advinp(int skip)                /* advance home to current position */
947   {
948 <        if (!passive | (ipb.beg == NULL)) {
949 <                advinp();
948 >        if (!skip | (passive >= 0)) {
949 >                ipb.beg = ipb.pos;      /* no need to copy input */
950                  return;
951          }
952 <        while (ipb.beg != ipb.pos) {    /* transfer buffer unaltered */
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;
# Line 943 | Line 962 | passinp(void)                  /* pass beginning to current position
962   }
963  
964  
946 static void
947 skipinp(void)                   /* rewind position and advance 1 */
948 {
949        if (ipb.beg == NULL)            /* full */
950                ipb.beg = ipb.end;
951        ipb.pos = ipb.beg;
952        ipb.chr = *ipb.pos;
953        if (passive)                    /* transmit unmatched character? */
954                putchar(ipb.chr);
955        if (++ipb.beg >= &inpbuf[INBSIZ])
956                ipb.beg = inpbuf;
957        scaninp();
958 }
959
960
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);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines