ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cal/lam.c
Revision: 1.12
Committed: Fri Jul 26 16:45:04 2013 UTC (10 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.11: +18 -12 lines
Log Message:
Improved handling of -iaN option and binary input from stdin

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: lam.c,v 1.11 2010/06/18 21:22:49 greg Exp $";
3 #endif
4 /*
5 * lam.c - simple program to laminate files.
6 *
7 * 7/14/88 Greg Ward
8 */
9
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stdio.h>
13 #include <ctype.h>
14
15 #include "platform.h"
16 #include "rtprocess.h"
17
18 #define MAXFILE 512 /* maximum number of files */
19
20 #define MAXLINE 4096 /* maximum input line */
21
22 FILE *input[MAXFILE];
23 int bytsiz[MAXFILE];
24 char *tabc[MAXFILE];
25 int nfiles;
26
27 char buf[MAXLINE];
28
29 int
30 main(argc, argv)
31 int argc;
32 char *argv[];
33 {
34 int unbuff = 0;
35 int i;
36 char *curtab;
37 int curbytes;
38 int puteol;
39
40 curtab = "\t";
41 curbytes = 0;
42 nfiles = 0;
43 for (i = 1; i < argc; i++) {
44 if (argv[i][0] == '-') {
45 switch (argv[i][1]) {
46 case 't':
47 curtab = argv[i]+2;
48 break;
49 case 'u':
50 unbuff = !unbuff;
51 break;
52 case 'i':
53 switch (argv[i][2]) {
54 case 'f':
55 curbytes = sizeof(float);
56 break;
57 case 'd':
58 curbytes = sizeof(double);
59 break;
60 case 'i':
61 curbytes = sizeof(int);
62 break;
63 case 'w':
64 curbytes = 2;
65 break;
66 case 'b':
67 curbytes = 1;
68 break;
69 case 'a':
70 curbytes = argv[i][3] ? -1 : 0;
71 break;
72 default:
73 goto badopt;
74 }
75 if (isdigit(argv[i][3]))
76 curbytes *= atoi(argv[i]+3);
77 if (abs(curbytes) > MAXLINE) {
78 fputs(argv[0], stderr);
79 fputs(": input size too big\n", stderr);
80 exit(1);
81 }
82 if (curbytes)
83 curtab = "";
84 break;
85 case '\0':
86 tabc[nfiles] = curtab;
87 input[nfiles] = stdin;
88 if (curbytes > 0)
89 SET_FILE_BINARY(input[nfiles]);
90 else
91 curbytes = -curbytes;
92 bytsiz[nfiles++] = curbytes;
93 break;
94 badopt:;
95 default:
96 fputs(argv[0], stderr);
97 fputs(": bad option\n", stderr);
98 exit(1);
99 }
100 } else if (argv[i][0] == '!') {
101 tabc[nfiles] = curtab;
102 if ((input[nfiles] = popen(argv[i]+1, "r")) == NULL) {
103 fputs(argv[i], stderr);
104 fputs(": cannot start command\n", stderr);
105 exit(1);
106 }
107 if (curbytes > 0)
108 SET_FILE_BINARY(input[nfiles]);
109 else
110 curbytes = -curbytes;
111 bytsiz[nfiles++] = curbytes;
112 } else {
113 tabc[nfiles] = curtab;
114 if ((input[nfiles] = fopen(argv[i], "r")) == NULL) {
115 fputs(argv[i], stderr);
116 fputs(": cannot open file\n", stderr);
117 exit(1);
118 }
119 if (curbytes > 0)
120 SET_FILE_BINARY(input[nfiles]);
121 else
122 curbytes = -curbytes;
123 bytsiz[nfiles++] = curbytes;
124 }
125 if (nfiles >= MAXFILE) {
126 fputs(argv[0], stderr);
127 fputs(": too many input streams\n", stderr);
128 exit(1);
129 }
130 }
131 puteol = 0; /* check for ASCII output */
132 for (i = nfiles; i--; )
133 if (!bytsiz[i] || isprint(tabc[i][0]) || tabc[i][0] == '\t') {
134 puteol++;
135 break;
136 }
137 for ( ; ; ) { /* main loop */
138 for (i = 0; i < nfiles; i++) {
139 if (bytsiz[i]) { /* binary file */
140 if (fread(buf, bytsiz[i], 1, input[i]) < 1)
141 break;
142 if (i)
143 fputs(tabc[i], stdout);
144 fwrite(buf, bytsiz[i], 1, stdout);
145 } else {
146 if (fgets(buf, MAXLINE, input[i]) == NULL)
147 break;
148 if (i)
149 fputs(tabc[i], stdout);
150 buf[strlen(buf)-1] = '\0';
151 fputs(buf, stdout);
152 }
153 }
154 if (i < nfiles)
155 break;
156 if (puteol)
157 putchar('\n');
158 if (unbuff)
159 fflush(stdout);
160 }
161 return(0);
162 }