ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/getinfo.c
Revision: 2.20
Committed: Fri Jul 19 17:37:56 2019 UTC (3 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.19: +2 -4 lines
Log Message:
Moved declarations and definitions for header.c from resolu.h to rtio.h

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.20 static const char RCSid[] = "$Id: getinfo.c,v 2.19 2019/07/16 17:07:35 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.17 if (getheader(stdin, (gethfunc *)fputs, stdout) < 0)
68     return 1;
69 greg 2.10 printargs(argc-2, argv+2, stdout);
70     fputc('\n', stdout);
71 greg 2.18 if (dim) { /* copy resolution string? */
72     RESOLU rs;
73     if (!fgetsresolu(&rs, stdin)) {
74     fputs("No resolution string\n", stderr);
75     return 1;
76     }
77 greg 2.19 if (dim > 0)
78     fputsresolu(&rs, stdout);
79 greg 2.18 }
80 greg 2.10 fflush(stdout);
81     execvp(argv[2], argv+2);
82     perror(argv[2]);
83     return 1;
84 greg 2.14 } else if (argc > 2 && !strcmp(argv[1], "-a")) {
85     SET_FILE_BINARY(stdout);
86 greg 2.17 if (getheader(stdin, (gethfunc *)fputs, stdout) < 0)
87     return 1;
88 greg 2.14 for (i = 2; i < argc; i++) {
89     int len = strlen(argv[i]);
90     if (!len) continue;
91     fputs(argv[i], stdout);
92     if (argv[i][len-1] != '\n')
93     fputc('\n', stdout);
94     }
95     fputc('\n', stdout);
96     copycat();
97     return 0;
98 greg 1.2 } else if (argc == 2 && !strcmp(argv[1], "-")) {
99 schorsch 2.6 SET_FILE_BINARY(stdout);
100 greg 2.17 if (getheader(stdin, NULL, NULL) < 0)
101     return 1;
102 greg 2.19 if (dim < 0) { /* skip resolution string? */
103 greg 2.18 RESOLU rs;
104     if (!fgetsresolu(&rs, stdin)) {
105     fputs("No resolution string\n", stderr);
106     return 1;
107     }
108     }
109 greg 1.2 copycat();
110 schorsch 2.8 return 0;
111 greg 1.1 }
112     for (i = 1; i < argc; i++) {
113     fputs(argv[i], stdout);
114     if ((fp = fopen(argv[i], "r")) == NULL)
115     fputs(": cannot open\n", stdout);
116     else {
117 greg 2.19 if (dim < 0) { /* dimensions only */
118     if (getheader(fp, NULL, NULL) < 0) {
119     fputs("bad header\n", stdout);
120     continue;
121     }
122 greg 1.1 fputs(": ", stdout);
123     getdim(fp);
124     } else {
125 schorsch 2.8 tabstr(":\n", NULL);
126 greg 2.19 if (getheader(fp, tabstr, NULL) < 0)
127     return 1;
128 greg 2.10 fputc('\n', stdout);
129 greg 2.19 if (dim > 0) {
130     fputc('\t', stdout);
131     getdim(fp);
132     }
133 greg 1.1 }
134     fclose(fp);
135     }
136     }
137 schorsch 2.7 if (argc == 1) {
138 greg 2.19 if (dim < 0) {
139     if (getheader(stdin, NULL, NULL) < 0)
140     return 1;
141 greg 1.1 getdim(stdin);
142     } else {
143 greg 2.17 if (getheader(stdin, (gethfunc *)fputs, stdout) < 0)
144     return 1;
145 greg 2.10 fputc('\n', stdout);
146 greg 2.19 if (dim > 0)
147     getdim(stdin);
148 greg 1.1 }
149 schorsch 2.7 }
150 schorsch 2.8 return 0;
151 greg 1.1 }
152    
153    
154 schorsch 2.8 static void
155     getdim( /* get dimensions from file */
156 greg 2.10 FILE *fp
157 schorsch 2.8 )
158 greg 1.1 {
159     int j;
160 greg 2.10 int c;
161 greg 2.19
162 greg 1.1 switch (c = getc(fp)) {
163     case '+': /* picture */
164     case '-':
165     do
166     putchar(c);
167     while (c != '\n' && (c = getc(fp)) != EOF);
168     break;
169     case 1: /* octree */
170     getc(fp);
171     j = 0;
172     while ((c = getc(fp)) != EOF)
173 greg 2.10 if (c == 0) {
174 greg 1.1 if (++j >= 4)
175     break;
176 greg 2.10 putchar(' ');
177     } else {
178 greg 1.1 putchar(c);
179 greg 2.10 }
180 greg 1.1 putchar('\n');
181     break;
182     default: /* ??? */
183     fputs("unknown file type\n", stdout);
184     break;
185     }
186 greg 1.2 }
187    
188    
189 schorsch 2.8 static void
190     copycat(void) /* copy input to output */
191 greg 1.2 {
192 greg 2.10 char buf[8192];
193 greg 2.11 int n;
194 greg 2.3
195 greg 2.10 fflush(stdout);
196     while ((n = fread(buf, 1, sizeof(buf), stdin)) > 0)
197 greg 2.15 if (writebuf(fileno(stdout), buf, n) != n)
198 greg 2.10 break;
199 greg 1.1 }