ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/getinfo.c
Revision: 2.21
Committed: Wed Jul 24 17:27:54 2019 UTC (4 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R3
Changes since 2.20: +23 -10 lines
Log Message:
Improved error reporting for bad/illegal headers

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.21 static const char RCSid[] = "$Id: getinfo.c,v 2.20 2019/07/19 17:37:56 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * getinfo.c - program to read info. header from file.
6     *
7     * 1/3/86
8     */
9    
10 greg 2.20 #include "rtio.h"
11 schorsch 2.6 #include "platform.h"
12 greg 2.15 #include "rtprocess.h"
13 schorsch 2.8 #include "resolu.h"
14 greg 2.2
15 greg 2.9 #ifdef getc_unlocked /* avoid nasty file-locking overhead */
16 greg 2.16 #undef getc
17 greg 2.9 #undef getchar
18     #undef putchar
19 greg 2.16 #define getc getc_unlocked
20 greg 2.9 #define getchar getchar_unlocked
21     #define putchar putchar_unlocked
22     #endif
23    
24 schorsch 2.8 static gethfunc tabstr;
25 greg 2.10 static void getdim(FILE *fp);
26 schorsch 2.8 static void copycat(void);
27 greg 1.1
28 greg 1.3
29 schorsch 2.8 static int
30     tabstr( /* put out line followed by tab */
31 greg 2.10 char *s,
32 schorsch 2.8 void *p
33     )
34 greg 1.1 {
35     while (*s) {
36     putchar(*s);
37     s++;
38     }
39     if (*--s == '\n')
40     putchar('\t');
41 gwlarson 2.4 return(0);
42 greg 1.1 }
43    
44    
45 schorsch 2.8 int
46     main(
47     int argc,
48     char **argv
49     )
50 greg 1.1 {
51     int dim = 0;
52     FILE *fp;
53     int i;
54    
55 greg 2.19 if (argc > 1 && (argv[1][0] == '-') | (argv[1][0] == '+') &&
56     argv[1][1] == 'd') {
57     dim = 1 - 2*(argv[1][0] == '-');
58 greg 1.1 argc--; argv++;
59 greg 2.18 }
60     #ifdef getc_unlocked /* avoid lock/unlock overhead */
61     flockfile(stdin);
62     #endif
63     SET_FILE_BINARY(stdin);
64     if (argc > 2 && !strcmp(argv[1], "-c")) {
65 greg 2.10 SET_FILE_BINARY(stdout);
66 greg 2.12 setvbuf(stdin, NULL, _IONBF, 2);
67 greg 2.21 if (getheader(stdin, (gethfunc *)fputs, stdout) < 0) {
68     fputs("Bad header!\n", stderr);
69 greg 2.17 return 1;
70 greg 2.21 }
71 greg 2.10 printargs(argc-2, argv+2, stdout);
72     fputc('\n', stdout);
73 greg 2.18 if (dim) { /* copy resolution string? */
74     RESOLU rs;
75     if (!fgetsresolu(&rs, stdin)) {
76 greg 2.21 fputs("No resolution string!\n", stderr);
77 greg 2.18 return 1;
78     }
79 greg 2.19 if (dim > 0)
80     fputsresolu(&rs, stdout);
81 greg 2.18 }
82 greg 2.10 fflush(stdout);
83     execvp(argv[2], argv+2);
84     perror(argv[2]);
85     return 1;
86 greg 2.14 } else if (argc > 2 && !strcmp(argv[1], "-a")) {
87     SET_FILE_BINARY(stdout);
88 greg 2.21 if (getheader(stdin, (gethfunc *)fputs, stdout) < 0) {
89     fputs("Bad header!\n", stderr);
90 greg 2.17 return 1;
91 greg 2.21 }
92 greg 2.14 for (i = 2; i < argc; i++) {
93     int len = strlen(argv[i]);
94     if (!len) continue;
95     fputs(argv[i], stdout);
96     if (argv[i][len-1] != '\n')
97     fputc('\n', stdout);
98     }
99     fputc('\n', stdout);
100     copycat();
101     return 0;
102 greg 1.2 } else if (argc == 2 && !strcmp(argv[1], "-")) {
103 schorsch 2.6 SET_FILE_BINARY(stdout);
104 greg 2.21 if (getheader(stdin, NULL, NULL) < 0) {
105     fputs("Bad header!\n", stderr);
106 greg 2.17 return 1;
107 greg 2.21 }
108 greg 2.19 if (dim < 0) { /* skip resolution string? */
109 greg 2.18 RESOLU rs;
110     if (!fgetsresolu(&rs, stdin)) {
111 greg 2.21 fputs("No resolution string!\n", stderr);
112 greg 2.18 return 1;
113     }
114     }
115 greg 1.2 copycat();
116 schorsch 2.8 return 0;
117 greg 1.1 }
118     for (i = 1; i < argc; i++) {
119     fputs(argv[i], stdout);
120     if ((fp = fopen(argv[i], "r")) == NULL)
121     fputs(": cannot open\n", stdout);
122     else {
123 greg 2.19 if (dim < 0) { /* dimensions only */
124     if (getheader(fp, NULL, NULL) < 0) {
125 greg 2.21 fputs("bad header!\n", stdout);
126 greg 2.19 continue;
127     }
128 greg 1.1 fputs(": ", stdout);
129     getdim(fp);
130     } else {
131 schorsch 2.8 tabstr(":\n", NULL);
132 greg 2.21 if (getheader(fp, tabstr, NULL) < 0) {
133     fputs(argv[i], stderr);
134     fputs(": bad header!\n", stderr);
135 greg 2.19 return 1;
136 greg 2.21 }
137 greg 2.10 fputc('\n', stdout);
138 greg 2.19 if (dim > 0) {
139     fputc('\t', stdout);
140     getdim(fp);
141     }
142 greg 1.1 }
143     fclose(fp);
144     }
145     }
146 schorsch 2.7 if (argc == 1) {
147 greg 2.19 if (dim < 0) {
148 greg 2.21 if (getheader(stdin, NULL, NULL) < 0) {
149     fputs("Bad header!\n", stderr);
150 greg 2.19 return 1;
151 greg 2.21 }
152 greg 1.1 getdim(stdin);
153     } else {
154 greg 2.21 if (getheader(stdin, (gethfunc *)fputs, stdout) < 0) {
155     fputs("Bad header!\n", stderr);
156 greg 2.17 return 1;
157 greg 2.21 }
158 greg 2.10 fputc('\n', stdout);
159 greg 2.19 if (dim > 0)
160     getdim(stdin);
161 greg 1.1 }
162 schorsch 2.7 }
163 schorsch 2.8 return 0;
164 greg 1.1 }
165    
166    
167 schorsch 2.8 static void
168     getdim( /* get dimensions from file */
169 greg 2.10 FILE *fp
170 schorsch 2.8 )
171 greg 1.1 {
172     int j;
173 greg 2.10 int c;
174 greg 2.19
175 greg 1.1 switch (c = getc(fp)) {
176     case '+': /* picture */
177     case '-':
178     do
179     putchar(c);
180     while (c != '\n' && (c = getc(fp)) != EOF);
181     break;
182     case 1: /* octree */
183     getc(fp);
184     j = 0;
185     while ((c = getc(fp)) != EOF)
186 greg 2.10 if (c == 0) {
187 greg 1.1 if (++j >= 4)
188     break;
189 greg 2.10 putchar(' ');
190     } else {
191 greg 1.1 putchar(c);
192 greg 2.10 }
193 greg 1.1 putchar('\n');
194     break;
195     default: /* ??? */
196     fputs("unknown file type\n", stdout);
197     break;
198     }
199 greg 1.2 }
200    
201    
202 schorsch 2.8 static void
203     copycat(void) /* copy input to output */
204 greg 1.2 {
205 greg 2.10 char buf[8192];
206 greg 2.11 int n;
207 greg 2.3
208 greg 2.10 fflush(stdout);
209     while ((n = fread(buf, 1, sizeof(buf), stdin)) > 0)
210 greg 2.15 if (writebuf(fileno(stdout), buf, n) != n)
211 greg 2.10 break;
212 greg 1.1 }