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.3 by schorsch, Mon Oct 27 10:26:05 2003 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         16              /* maximum number of files */
16 > #ifndef MAXLINE
17 > #define MAXLINE         (1L<<20)        /* maximum input line per stream */
18 > #endif
19  
20 < #define MAXLINE         512             /* 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 < FILE    *input[MAXFILE];
22 < int     tabc[MAXFILE];
23 < int     nfiles;
26 > int     nfiles = 0;
27  
28   char    buf[MAXLINE];
29  
30 < main(argc, argv)
31 < int     argc;
29 < char    *argv[];
30 > int
31 > main(int argc, char *argv[])
32   {
33 <        register int    i;
34 <        int     curtab;
35 <        int     running;
33 >        long    incnt = 0;
34 >        int     unbuff = 0;
35 >        int     binout = 0;
36 >        char    *curtab = "\t";
37 >        int     curbytes = 0;
38 >        int     puteol;
39 >        int     i;
40  
41 <        curtab = '\t';
42 <        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;
51 >                                curtab = argv[i]+2;
52 >                                if (!*curtab) curtab = "\n";
53 >                                continue;
54 >                        case 'u':
55 >                                unbuff = !unbuff;
56 >                                continue;
57 >                        case 'i':
58 >                                switch (argv[i][2]) {
59 >                                case 'n':
60 >                                        incnt = atol(argv[++i]);
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':
79 >                                        curbytes = 1;
80 >                                        break;
81 >                                case 'a':
82 >                                        curbytes = argv[i][3] ? -1 : 0;
83 >                                        break;
84 >                                default:
85 >                                        goto badopt;
86 >                                }
87 >                                if (isdigit(argv[i][3]))
88 >                                        curbytes *= atoi(argv[i]+3);
89 >                                curbytes += (curbytes == -1);
90 >                                if (curbytes > MAXLINE) {
91 >                                        fputs(argv[0], stderr);
92 >                                        fputs(": input size too big\n", stderr);
93 >                                        return(1);
94 >                                }
95 >                                if (curbytes > 0) {
96 >                                        curtab = "";
97 >                                        ++binout;
98 >                                }
99 >                                continue;
100                          case '\0':
101 <                                tabc[nfiles] = curtab;
102 <                                input[nfiles++] = stdin;
103 <                                break;
101 >                                rifile[nfiles].tabc = curtab;
102 >                                rifile[nfiles].input = stdin;
103 >                                if (curbytes > 0)
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(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(rifile[nfiles].input);
135 +                        rifile[nfiles++].bytsiz = curbytes;
136                  }
67                if (nfiles >= MAXFILE) {
68                        fputs(argv[0], stderr);
69                        fputs(": too many input streams\n", stderr);
70                        exit(1);
71                }
137          }
138 <
139 <        do {
140 <                running = 0;
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(rifile[i].input);
154 >        flockfile(stdout);
155 > #endif
156 >        puteol = 0;                             /* any ASCII output at all? */
157 >        for (i = nfiles; i--; )
158 >                puteol += (rifile[i].bytsiz <= 0);
159 >        do {                                    /* main loop */
160                  for (i = 0; i < nfiles; i++) {
161 <                        if (fgets(buf, MAXLINE, input[i]) != NULL) {
161 >                        if (rifile[i].bytsiz > 0) {             /* binary input */
162 >                                if (getbinary(buf, rifile[i].bytsiz, 1, rifile[i].input) < 1)
163 >                                        break;
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 <                                        putchar(tabc[i]);
183 >                                        fputs(rifile[i].tabc, stdout);
184                                  buf[strlen(buf)-1] = '\0';
185 <                                fputs(buf, stdout);
186 <                                running++;
185 >                                if (fputs(buf, stdout) == EOF)
186 >                                        break;
187                          }
188                  }
189 <                if (running)
189 >                if (i < nfiles)
190 >                        break;
191 >                if (puteol)
192                          putchar('\n');
193 <        } while (running);
194 <
195 <        exit(0);
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