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.2 by schorsch, Sun Jun 8 12:03:09 2003 UTC vs.
Revision 1.21 by greg, Fri Jul 5 00:46:23 2019 UTC

# Line 10 | Line 10 | static const char      RCSid[] = "$Id$";
10   #include <stdlib.h>
11   #include <string.h>
12   #include <stdio.h>
13 + #include <ctype.h>
14  
15   #include "platform.h"
16 + #include "rtio.h"
17  
18 < #define MAXFILE         16              /* maximum number of files */
18 > #define MAXFILE         512             /* maximum number of files */
19  
20 < #define MAXLINE         512             /* maximum input line */
20 > #define MAXLINE         65536           /* maximum input line */
21  
22   FILE    *input[MAXFILE];
23 < int     tabc[MAXFILE];
24 < int     nfiles;
23 > int     bytsiz[MAXFILE];
24 > char    *tabc[MAXFILE];
25 > int     nfiles = 0;
26  
27   char    buf[MAXLINE];
28  
29 < main(argc, argv)
30 < int     argc;
28 < char    *argv[];
29 > int
30 > main(int argc, char *argv[])
31   {
32 <        register int    i;
33 <        int     curtab;
34 <        int     running;
32 >        long    incnt = 0;
33 >        int     unbuff = 0;
34 >        int     binout = 0;
35 >        char    *curtab = "\t";
36 >        int     curbytes = 0;
37 >        int     puteol;
38 >        int     i;
39  
34        curtab = '\t';
35        nfiles = 0;
40          for (i = 1; i < argc; i++) {
41                  if (argv[i][0] == '-') {
42                          switch (argv[i][1]) {
43                          case 't':
44 <                                curtab = argv[i][2];
44 >                                curtab = argv[i]+2;
45                                  break;
46 +                        case 'u':
47 +                                unbuff = !unbuff;
48 +                                break;
49 +                        case 'i':
50 +                                switch (argv[i][2]) {
51 +                                case 'n':
52 +                                        incnt = atol(argv[++i]);
53 +                                        break;
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 +                                curbytes += (curbytes == -1);
78 +                                if (curbytes > MAXLINE) {
79 +                                        fputs(argv[0], stderr);
80 +                                        fputs(": input size too big\n", stderr);
81 +                                        return(1);
82 +                                }
83 +                                if (curbytes > 0) {
84 +                                        curtab = "";
85 +                                        ++binout;
86 +                                }
87 +                                break;
88                          case '\0':
89                                  tabc[nfiles] = curtab;
90 <                                input[nfiles++] = stdin;
90 >                                input[nfiles] = stdin;
91 >                                if (curbytes > 0)
92 >                                        SET_FILE_BINARY(input[nfiles]);
93 >                                bytsiz[nfiles++] = curbytes;
94                                  break;
95 +                        badopt:;
96                          default:
97                                  fputs(argv[0], stderr);
98                                  fputs(": bad option\n", stderr);
99 <                                exit(1);
99 >                                return(1);
100                          }
101                  } else if (argv[i][0] == '!') {
102                          tabc[nfiles] = curtab;
103 <                        if ((input[nfiles++] = popen(argv[i]+1, "r")) == NULL) {
103 >                        if ((input[nfiles] = popen(argv[i]+1, "r")) == NULL) {
104                                  fputs(argv[i], stderr);
105                                  fputs(": cannot start command\n", stderr);
106 <                                exit(1);
106 >                                return(1);
107                          }
108 +                        if (curbytes > 0)
109 +                                SET_FILE_BINARY(input[nfiles]);
110 +                        bytsiz[nfiles++] = curbytes;
111                  } else {
112                          tabc[nfiles] = curtab;
113 <                        if ((input[nfiles++] = fopen(argv[i], "r")) == NULL) {
113 >                        if ((input[nfiles] = fopen(argv[i], "r")) == NULL) {
114                                  fputs(argv[i], stderr);
115                                  fputs(": cannot open file\n", stderr);
116 <                                exit(1);
116 >                                return(1);
117                          }
118 +                        if (curbytes > 0)
119 +                                SET_FILE_BINARY(input[nfiles]);
120 +                        bytsiz[nfiles++] = curbytes;
121                  }
122                  if (nfiles >= MAXFILE) {
123                          fputs(argv[0], stderr);
124                          fputs(": too many input streams\n", stderr);
125 <                        exit(1);
125 >                        return(1);
126                  }
127          }
128 <
129 <        do {
130 <                running = 0;
128 >        if (!nfiles) {
129 >                fputs(argv[0], stderr);
130 >                fputs(": no input streams\n", stderr);
131 >                return(1);
132 >        }
133 >        if (binout)                             /* binary output? */
134 >                SET_FILE_BINARY(stdout);
135 > #ifdef getc_unlocked                            /* avoid lock/unlock overhead */
136 >        for (i = nfiles; i--; )
137 >                flockfile(input[i]);
138 >        flockfile(stdout);
139 > #endif
140 >        puteol = 0;                             /* any ASCII output at all? */
141 >        for (i = nfiles; i--; )
142 >                puteol += (bytsiz[i] <= 0);
143 >        do {                                    /* main loop */
144                  for (i = 0; i < nfiles; i++) {
145 <                        if (fgets(buf, MAXLINE, input[i]) != NULL) {
145 >                        if (bytsiz[i] > 0) {            /* binary input */
146 >                                if (getbinary(buf, bytsiz[i], 1, input[i]) < 1)
147 >                                        break;
148 >                                putbinary(buf, bytsiz[i], 1, stdout);
149 >                        } else if (bytsiz[i] < 0) {     /* multi-line input */
150 >                                int     n = -bytsiz[i];
151 >                                while (n--) {
152 >                                        if (fgets(buf, MAXLINE, input[i]) == NULL)
153 >                                                break;
154 >                                        if ((i > 0) | (n < -bytsiz[i]-1))
155 >                                                fputs(tabc[i], stdout);
156 >                                        buf[strlen(buf)-1] = '\0';
157 >                                        fputs(buf, stdout);
158 >                                }
159 >                                if (n >= 0)             /* fell short? */
160 >                                        break;
161 >                        } else {                        /* single-line input */
162 >                                if (fgets(buf, MAXLINE, input[i]) == NULL)
163 >                                        break;
164                                  if (i)
165 <                                        putchar(tabc[i]);
165 >                                        fputs(tabc[i], stdout);
166                                  buf[strlen(buf)-1] = '\0';
167                                  fputs(buf, stdout);
81                                running++;
168                          }
169                  }
170 <                if (running)
170 >                if (i < nfiles)
171 >                        break;
172 >                if (puteol)
173                          putchar('\n');
174 <        } while (running);
175 <
176 <        exit(0);
174 >                if (unbuff)
175 >                        fflush(stdout);
176 >        } while (--incnt);
177 >                                                        /* check ending */
178 >        if (incnt > 0) {
179 >                fputs(argv[0], stderr);
180 >                fputs(": warning: premature EOD\n", stderr);
181 >        }
182 >        return(0);
183   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines