ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cal/lam.c
Revision: 1.27
Committed: Thu Feb 6 19:52:05 2025 UTC (3 months, 1 week ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.26: +11 -8 lines
Log Message:
fix(rlam): Fixed processing of -in option

File Contents

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