ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cal/lam.c
Revision: 1.11
Committed: Fri Jun 18 21:22:49 2010 UTC (13 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R1
Changes since 1.10: +25 -20 lines
Log Message:
Added -u option to rlam

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: lam.c,v 1.10 2010/06/18 01:12:57 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 (curbytes < 0 || curbytes > MAXLINE) {
78 fputs(argv[0], stderr);
79 fputs(": illegal input size\n", stderr);
80 exit(1);
81 }
82 if (curbytes)
83 curtab = "";
84 break;
85 case '\0':
86 tabc[nfiles] = curtab;
87 bytsiz[nfiles] = curbytes;
88 input[nfiles++] = stdin;
89 break;
90 badopt:;
91 default:
92 fputs(argv[0], stderr);
93 fputs(": bad option\n", stderr);
94 exit(1);
95 }
96 } else if (argv[i][0] == '!') {
97 tabc[nfiles] = curtab;
98 bytsiz[nfiles] = curbytes;
99 if ((input[nfiles] = popen(argv[i]+1, "r")) == NULL) {
100 fputs(argv[i], stderr);
101 fputs(": cannot start command\n", stderr);
102 exit(1);
103 }
104 if (bytsiz[nfiles])
105 SET_FILE_BINARY(input[nfiles]);
106 ++nfiles;
107 } else {
108 tabc[nfiles] = curtab;
109 bytsiz[nfiles] = curbytes;
110 if ((input[nfiles] = fopen(argv[i], "r")) == NULL) {
111 fputs(argv[i], stderr);
112 fputs(": cannot open file\n", stderr);
113 exit(1);
114 }
115 if (bytsiz[nfiles])
116 SET_FILE_BINARY(input[nfiles]);
117 ++nfiles;
118 }
119 if (nfiles >= MAXFILE) {
120 fputs(argv[0], stderr);
121 fputs(": too many input streams\n", stderr);
122 exit(1);
123 }
124 }
125 puteol = 0; /* check for ASCII output */
126 for (i = nfiles; i--; )
127 if (!bytsiz[i] || isprint(tabc[i][0]) || tabc[i][0] == '\t') {
128 puteol++;
129 break;
130 }
131 for ( ; ; ) { /* main loop */
132 for (i = 0; i < nfiles; i++) {
133 if (bytsiz[i]) { /* binary file */
134 if (fread(buf, bytsiz[i], 1, input[i]) < 1)
135 break;
136 if (i)
137 fputs(tabc[i], stdout);
138 fwrite(buf, bytsiz[i], 1, stdout);
139 } else {
140 if (fgets(buf, MAXLINE, input[i]) == NULL)
141 break;
142 if (i)
143 fputs(tabc[i], stdout);
144 buf[strlen(buf)-1] = '\0';
145 fputs(buf, stdout);
146 }
147 }
148 if (i < nfiles)
149 break;
150 if (puteol)
151 putchar('\n');
152 if (unbuff)
153 fflush(stdout);
154 }
155 return(0);
156 }