ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cal/lam.c
(Generate patch)

Comparing ray/src/cal/lam.c (file contents):
Revision 1.15 by greg, Thu Mar 24 18:48:28 2016 UTC vs.
Revision 1.28 by greg, Thu Mar 6 20:19:03 2025 UTC

# Line 7 | Line 7 | static const char      RCSid[] = "$Id$";
7   *      7/14/88         Greg Ward
8   */
9  
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stdio.h>
10   #include <ctype.h>
11  
12 + #include "rtio.h"
13   #include "platform.h"
14 < #include "rtprocess.h"
14 > #include "paths.h"
15  
16 < #define MAXFILE         512             /* maximum number of files */
16 > #ifndef MAXLINE
17 > #define MAXLINE         (1L<<20)        /* maximum input line per stream */
18 > #endif
19  
20 < #define MAXLINE         65536           /* maximum input line */
20 > struct instream {               /* structure to hold input stream info */
21 >        FILE            *input;         /* input stream */
22 >        int             bytsiz;         /* bytes/component if binary */
23 >        char            *tabc;          /* data separation string */
24 > }       *rifile = NULL;
25  
26 < long    incnt = 0;                      /* limit number of records? */
26 > int     nfiles = 0;
27  
24 FILE    *input[MAXFILE];
25 int     bytsiz[MAXFILE];
26 char    *tabc[MAXFILE];
27 int     nfiles;
28
28   char    buf[MAXLINE];
29  
30   int
31 < main(argc, argv)
33 < int     argc;
34 < char    *argv[];
31 > main(int argc, char *argv[])
32   {
33 +        long    incnt = 0;
34          int     unbuff = 0;
35          int     binout = 0;
36 <        int     i;
37 <        char    *curtab;
40 <        int     curbytes;
36 >        char    *curtab = "\t";
37 >        int     curbytes = 0;
38          int     puteol;
39 +        int     i;
40  
41 <        curtab = "\t";
42 <        curbytes = 0;
43 <        nfiles = 0;
41 >        rifile = (struct instream *)calloc(argc-1, sizeof(struct instream));
42 >        if (!rifile) {
43 >                fputs(argv[0], stderr);
44 >                fputs(": not enough memory\n", stderr);
45 >                return(1);
46 >        }
47          for (i = 1; i < argc; i++) {
48                  if (argv[i][0] == '-') {
49                          switch (argv[i][1]) {
50                          case 't':
51                                  curtab = argv[i]+2;
52 <                                break;
52 >                                if (!*curtab) curtab = "\n";
53 >                                continue;
54                          case 'u':
55                                  unbuff = !unbuff;
56 <                                break;
56 >                                continue;
57                          case 'i':
58                                  switch (argv[i][2]) {
59                                  case 'n':
60                                          incnt = atol(argv[++i]);
61 <                                        break;
61 >                                        continue;
62                                  case 'f':
63 +                                case 'F':
64                                          curbytes = sizeof(float);
65                                          break;
66                                  case 'd':
67 +                                case 'D':
68                                          curbytes = sizeof(double);
69                                          break;
70                                  case 'i':
71 +                                case 'I':
72                                          curbytes = sizeof(int);
73                                          break;
74                                  case 'w':
75 +                                case 'W':
76                                          curbytes = 2;
77                                          break;
78                                  case 'b':
# Line 80 | Line 86 | char   *argv[];
86                                  }
87                                  if (isdigit(argv[i][3]))
88                                          curbytes *= atoi(argv[i]+3);
89 <                                if (abs(curbytes) > MAXLINE) {
89 >                                curbytes += (curbytes == -1);
90 >                                if (curbytes > MAXLINE) {
91                                          fputs(argv[0], stderr);
92                                          fputs(": input size too big\n", stderr);
93 <                                        exit(1);
93 >                                        return(1);
94                                  }
95 <                                if (curbytes) {
95 >                                if (curbytes > 0) {
96                                          curtab = "";
97                                          ++binout;
98                                  }
99 <                                break;
99 >                                continue;
100                          case '\0':
101 <                                tabc[nfiles] = curtab;
102 <                                input[nfiles] = stdin;
101 >                                rifile[nfiles].tabc = curtab;
102 >                                rifile[nfiles].input = stdin;
103                                  if (curbytes > 0)
104 <                                        SET_FILE_BINARY(input[nfiles]);
105 <                                else
106 <                                        curbytes = -curbytes;
100 <                                bytsiz[nfiles++] = curbytes;
101 <                                break;
104 >                                        SET_FILE_BINARY(rifile[nfiles].input);
105 >                                rifile[nfiles++].bytsiz = curbytes;
106 >                                continue;
107                          badopt:;
108                          default:
109                                  fputs(argv[0], stderr);
110 <                                fputs(": bad option\n", stderr);
111 <                                exit(1);
110 >                                fputs(": unknown option '", stderr);
111 >                                fputs(argv[i], stderr);
112 >                                fputs("'\n", stderr);
113 >                                return(1);
114                          }
115 <                } else if (argv[i][0] == '!') {
116 <                        tabc[nfiles] = curtab;
117 <                        if ((input[nfiles] = popen(argv[i]+1, "r")) == NULL) {
115 >                }
116 >                if (argv[i][0] == '!') {
117 >                        rifile[nfiles].tabc = curtab;
118 >                        if ((rifile[nfiles].input = popen(argv[i]+1, "r")) == NULL) {
119                                  fputs(argv[i], stderr);
120                                  fputs(": cannot start command\n", stderr);
121 <                                exit(1);
121 >                                return(1);
122                          }
123                          if (curbytes > 0)
124 <                                SET_FILE_BINARY(input[nfiles]);
125 <                        else
118 <                                curbytes = -curbytes;
119 <                        bytsiz[nfiles++] = curbytes;
124 >                                SET_FILE_BINARY(rifile[nfiles].input);
125 >                        rifile[nfiles++].bytsiz = curbytes;
126                  } else {
127 <                        tabc[nfiles] = curtab;
128 <                        if ((input[nfiles] = fopen(argv[i], "r")) == NULL) {
127 >                        rifile[nfiles].tabc = curtab;
128 >                        if ((rifile[nfiles].input = fopen(argv[i], "r")) == NULL) {
129                                  fputs(argv[i], stderr);
130                                  fputs(": cannot open file\n", stderr);
131 <                                exit(1);
131 >                                return(1);
132                          }
133                          if (curbytes > 0)
134 <                                SET_FILE_BINARY(input[nfiles]);
135 <                        else
130 <                                curbytes = -curbytes;
131 <                        bytsiz[nfiles++] = curbytes;
134 >                                SET_FILE_BINARY(rifile[nfiles].input);
135 >                        rifile[nfiles++].bytsiz = curbytes;
136                  }
133                if (nfiles >= MAXFILE) {
134                        fputs(argv[0], stderr);
135                        fputs(": too many input streams\n", stderr);
136                        exit(1);
137                }
137          }
138 +        if (!nfiles) {
139 +                fputs(argv[0], stderr);
140 +                fputs(": no input streams\n", stderr);
141 +                return(1);
142 +        }                                       /* reduce array to size we need */
143 +        rifile = (struct instream *)realloc(rifile, nfiles*sizeof(struct instream));
144 +        if (!rifile) {
145 +                fputs(argv[0], stderr);
146 +                fputs(": realloc() failed!\n", stderr);
147 +                return(1);
148 +        }
149          if (binout)                             /* binary output? */
150                  SET_FILE_BINARY(stdout);
151   #ifdef getc_unlocked                            /* avoid lock/unlock overhead */
152          for (i = nfiles; i--; )
153 <                flockfile(input[i]);
153 >                flockfile(rifile[i].input);
154          flockfile(stdout);
155   #endif
156          puteol = 0;                             /* any ASCII output at all? */
157          for (i = nfiles; i--; )
158 <                if (!bytsiz[i] || isprint(tabc[i][0]) || tabc[i][0] == '\t') {
159 <                        puteol++;
150 <                        break;
151 <                }
152 <        while (--incnt) {                       /* main loop */
158 >                puteol += (rifile[i].bytsiz <= 0);
159 >        do {                                    /* main loop */
160                  for (i = 0; i < nfiles; i++) {
161 <                        if (bytsiz[i]) {                /* binary file */
162 <                                if (fread(buf, bytsiz[i], 1, input[i]) < 1)
161 >                        if (rifile[i].bytsiz > 0) {             /* binary input */
162 >                                if (getbinary(buf, rifile[i].bytsiz, 1, rifile[i].input) < 1)
163                                          break;
164 <                                if (i)
158 <                                        fputs(tabc[i], stdout);
159 <                                fwrite(buf, bytsiz[i], 1, stdout);
160 <                        } else {
161 <                                if (fgets(buf, MAXLINE, input[i]) == NULL)
164 >                                if (putbinary(buf, rifile[i].bytsiz, 1, stdout) != 1)
165                                          break;
166 +                        } else if (rifile[i].bytsiz < 0) {      /* multi-line input */
167 +                                int     n = -rifile[i].bytsiz;
168 +                                while (n--) {
169 +                                        if (fgets(buf, MAXLINE, rifile[i].input) == NULL)
170 +                                                break;
171 +                                        if ((i > 0) | (n < -rifile[i].bytsiz-1))
172 +                                                fputs(rifile[i].tabc, stdout);
173 +                                        buf[strlen(buf)-1] = '\0';
174 +                                        if (fputs(buf, stdout) == EOF)
175 +                                                break;
176 +                                }
177 +                                if (n >= 0)             /* fell short? */
178 +                                        break;
179 +                        } else {                        /* single-line input */
180 +                                if (fgets(buf, MAXLINE, rifile[i].input) == NULL)
181 +                                        break;
182                                  if (i)
183 <                                        fputs(tabc[i], stdout);
183 >                                        fputs(rifile[i].tabc, stdout);
184                                  buf[strlen(buf)-1] = '\0';
185 <                                fputs(buf, stdout);
185 >                                if (fputs(buf, stdout) == EOF)
186 >                                        break;
187                          }
188                  }
189                  if (i < nfiles)
# Line 172 | Line 192 | char   *argv[];
192                          putchar('\n');
193                  if (unbuff)
194                          fflush(stdout);
195 +        } while (--incnt);
196 +                                                        /* check ending */
197 +        if (fflush(stdout) == EOF) {
198 +                fputs(argv[0], stderr);
199 +                fputs(": write error on standard output\n", stderr);
200 +                return(1);
201 +        }
202 +        if (incnt > 0) {
203 +                fputs(argv[0], stderr);
204 +                fputs(": warning: premature EOD\n", stderr);
205          }
206          return(0);
207   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines