| 7 |
|
* 7/4/19 Greg Ward |
| 8 |
|
*/ |
| 9 |
|
|
| 10 |
– |
#include <stdlib.h> |
| 11 |
– |
#include <string.h> |
| 12 |
– |
#include <stdio.h> |
| 10 |
|
#include <ctype.h> |
| 11 |
|
|
| 12 |
+ |
#include "rtio.h" |
| 13 |
|
#include "platform.h" |
| 14 |
+ |
#include "paths.h" |
| 15 |
|
#include "resolu.h" |
| 17 |
– |
#include "rtio.h" |
| 16 |
|
|
| 17 |
|
#define DOHEADER 1 |
| 18 |
|
#define DORESOLU 2 |
| 19 |
|
|
| 20 |
|
#define MAXFILE 512 /* maximum number of files */ |
| 21 |
|
|
| 22 |
< |
FILE *output[MAXFILE]; |
| 25 |
< |
int bytsiz[MAXFILE]; |
| 26 |
< |
short hdrflags[MAXFILE]; |
| 27 |
< |
const char *format[MAXFILE]; |
| 28 |
< |
int termc[MAXFILE]; |
| 29 |
< |
int nfiles = 0; |
| 22 |
> |
static int swapped = 0; /* input is byte-swapped */ |
| 23 |
|
|
| 24 |
< |
int outheader = 0; /* output header to each stream? */ |
| 24 |
> |
static FILE *output[MAXFILE]; |
| 25 |
> |
static int bytsiz[MAXFILE]; |
| 26 |
> |
static short hdrflags[MAXFILE]; |
| 27 |
> |
static const char *format[MAXFILE]; |
| 28 |
> |
static int termc[MAXFILE]; |
| 29 |
> |
static int nfiles = 0; |
| 30 |
|
|
| 31 |
< |
RESOLU ourres = {PIXSTANDARD, 0, 0}; |
| 31 |
> |
static RESOLU ourres = {PIXSTANDARD, 0, 0}; |
| 32 |
|
|
| 33 |
< |
char buf[16384]; |
| 33 |
> |
static char buf[16384]; /* input buffer used in scanOK() */ |
| 34 |
|
|
| 35 |
|
|
| 36 |
|
/* process header line */ |
| 38 |
|
headline(char *s, void *p) |
| 39 |
|
{ |
| 40 |
|
extern const char FMTSTR[]; |
| 41 |
< |
int i = nfiles; |
| 41 |
> |
int i; |
| 42 |
|
|
| 43 |
|
if (strstr(s, FMTSTR) == s) |
| 44 |
|
return(0); /* don't copy format */ |
| 48 |
|
return(0); |
| 49 |
|
if (!strncmp(s, "NCOMP=", 6)) |
| 50 |
|
return(0); |
| 51 |
+ |
if ((i = isbigendian(s)) >= 0) { |
| 52 |
+ |
swapped = (nativebigendian() != i); |
| 53 |
+ |
return(0); |
| 54 |
+ |
} |
| 55 |
+ |
i = nfiles; |
| 56 |
|
while (i--) /* else copy line to output streams */ |
| 57 |
|
if (hdrflags[i] & DOHEADER) |
| 58 |
|
fputs(s, output[i]); |
| 83 |
|
int |
| 84 |
|
main(int argc, char *argv[]) |
| 85 |
|
{ |
| 86 |
< |
int inpheader = 0; |
| 87 |
< |
int inpres = 0; |
| 85 |
< |
int outres = 0; |
| 86 |
> |
int inpflags = 0; |
| 87 |
> |
int needres = 0; |
| 88 |
|
int force = 0; |
| 89 |
|
int append = 0; |
| 90 |
|
long outcnt = 0; |
| 100 |
|
switch (argv[i][1]) { |
| 101 |
|
case 't': |
| 102 |
|
curterm = argv[i][2]; |
| 103 |
+ |
if (!curterm) curterm = '\n'; |
| 104 |
|
break; |
| 105 |
|
case 'i': |
| 106 |
|
switch (argv[i][2]) { |
| 107 |
|
case 'h': |
| 108 |
< |
inpheader = !inpheader; |
| 108 |
> |
inpflags ^= DOHEADER; |
| 109 |
|
break; |
| 110 |
|
case 'H': |
| 111 |
< |
inpres = !inpres; |
| 111 |
> |
inpflags ^= DORESOLU; |
| 112 |
|
break; |
| 113 |
|
default: |
| 114 |
|
goto badopt; |
| 115 |
|
} |
| 116 |
|
break; |
| 117 |
|
case 'f': |
| 118 |
< |
++force; |
| 116 |
< |
append = 0; |
| 118 |
> |
force = !force; |
| 119 |
|
break; |
| 120 |
|
case 'a': |
| 121 |
< |
if (outheader | outres) { |
| 120 |
< |
fputs(argv[0], stderr); |
| 121 |
< |
fputs(": -a option incompatible with -oh and -oH\n", |
| 122 |
< |
stderr); |
| 123 |
< |
return(1); |
| 124 |
< |
} |
| 125 |
< |
append = 1; |
| 121 |
> |
append = !append; |
| 122 |
|
break; |
| 123 |
|
case 'x': |
| 124 |
|
ourres.xr = atoi(argv[++i]); |
| 129 |
– |
outres = 1; |
| 125 |
|
break; |
| 126 |
|
case 'y': |
| 127 |
|
ourres.yr = atoi(argv[++i]); |
| 133 |
– |
outres = 1; |
| 128 |
|
break; |
| 129 |
|
case 'o': |
| 130 |
|
switch (argv[i][2]) { |
| 159 |
|
break; |
| 160 |
|
case 'a': |
| 161 |
|
curfmt = "ascii"; |
| 162 |
< |
curbytes = argv[i][3] ? -1 : 0; |
| 162 |
> |
curbytes = -1; |
| 163 |
|
break; |
| 164 |
|
default: |
| 165 |
|
goto badopt; |
| 172 |
|
fputs(": output size too big\n", stderr); |
| 173 |
|
return(1); |
| 174 |
|
} |
| 175 |
< |
if (curbytes > 0) { |
| 182 |
< |
curterm = '\0'; |
| 183 |
< |
++bininp; |
| 184 |
< |
} |
| 175 |
> |
bininp += (curbytes > 0); |
| 176 |
|
break; |
| 177 |
|
case '\0': |
| 178 |
< |
outres |= (curflags & DORESOLU); |
| 178 |
> |
needres |= (curflags & DORESOLU); |
| 179 |
|
termc[nfiles] = curterm; |
| 180 |
|
hdrflags[nfiles] = curflags; |
| 181 |
|
output[nfiles] = stdout; |
| 191 |
|
return(1); |
| 192 |
|
} |
| 193 |
|
} else if (argv[i][0] == '!') { |
| 194 |
< |
outres |= (curflags & DORESOLU); |
| 194 |
> |
needres |= (curflags & DORESOLU); |
| 195 |
|
termc[nfiles] = curterm; |
| 196 |
|
hdrflags[nfiles] = curflags; |
| 197 |
|
if ((output[nfiles] = popen(argv[i]+1, "w")) == NULL) { |
| 204 |
|
format[nfiles] = curfmt; |
| 205 |
|
bytsiz[nfiles++] = curbytes; |
| 206 |
|
} else { |
| 207 |
< |
outres |= (curflags & DORESOLU); |
| 207 |
> |
if (append & (curflags != 0)) { |
| 208 |
> |
fputs(argv[0], stderr); |
| 209 |
> |
fputs(": -a option incompatible with -oh and -oH\n", |
| 210 |
> |
stderr); |
| 211 |
> |
return(1); |
| 212 |
> |
} |
| 213 |
> |
needres |= (curflags & DORESOLU); |
| 214 |
|
termc[nfiles] = curterm; |
| 215 |
|
hdrflags[nfiles] = curflags; |
| 216 |
|
if (!append & !force && access(argv[i], F_OK) == 0) { |
| 249 |
|
flockfile(output[i]); |
| 250 |
|
#endif |
| 251 |
|
/* load/copy header */ |
| 252 |
< |
if (inpheader && getheader(stdin, headline, NULL) < 0) { |
| 252 |
> |
if (inpflags & DOHEADER && getheader(stdin, headline, NULL) < 0) { |
| 253 |
|
fputs(argv[0], stderr); |
| 254 |
|
fputs(": cannot get header from standard input\n", |
| 255 |
|
stderr); |
| 256 |
|
return(1); |
| 257 |
|
} |
| 258 |
|
/* handle resolution string */ |
| 259 |
< |
if (inpres && !fgetsresolu(&ourres, stdin)) { |
| 259 |
> |
if (inpflags & DORESOLU && !fgetsresolu(&ourres, stdin)) { |
| 260 |
|
fputs(argv[0], stderr); |
| 261 |
|
fputs(": cannot get resolution string from standard input\n", |
| 262 |
|
stderr); |
| 263 |
|
return(1); |
| 264 |
|
} |
| 265 |
< |
if (outres && (ourres.xr <= 0) | (ourres.yr <= 0)) { |
| 265 |
> |
if (needres && (ourres.xr <= 0) | (ourres.yr <= 0)) { |
| 266 |
|
fputs(argv[0], stderr); |
| 267 |
|
fputs(": -oH option requires -iH or -x and -y options\n", stderr); |
| 268 |
|
return(1); |
| 278 |
|
} |
| 279 |
|
for (i = 0; i < nfiles; i++) { /* complete headers */ |
| 280 |
|
if (hdrflags[i] & DOHEADER) { |
| 281 |
< |
if (!inpheader) |
| 281 |
> |
if (!(inpflags & DOHEADER)) |
| 282 |
|
newheader("RADIANCE", output[i]); |
| 283 |
|
printargs(argc, argv, output[i]); |
| 284 |
< |
if (format[i] != NULL) |
| 284 |
> |
if (format[i] != NULL) { |
| 285 |
> |
extern const char BIGEND[]; |
| 286 |
> |
if ((format[i][0] == 'f') | |
| 287 |
> |
(format[i][0] == 'd')) { |
| 288 |
> |
fputs(BIGEND, output[i]); |
| 289 |
> |
fputs(nativebigendian() ^ swapped ? |
| 290 |
> |
"1\n" : "0\n", output[i]); |
| 291 |
> |
} |
| 292 |
|
fputformat(format[i], output[i]); |
| 293 |
+ |
} |
| 294 |
|
fputc('\n', output[i]); |
| 295 |
|
} |
| 296 |
|
if (hdrflags[i] & DORESOLU) |
| 301 |
|
if (bytsiz[i] > 0) { /* binary output */ |
| 302 |
|
if (getbinary(buf, bytsiz[i], 1, stdin) < 1) |
| 303 |
|
break; |
| 304 |
< |
putbinary(buf, bytsiz[i], 1, output[i]); |
| 304 |
> |
if (putbinary(buf, bytsiz[i], 1, output[i]) < 1) |
| 305 |
> |
break; |
| 306 |
|
} else if (bytsiz[i] < 0) { /* N-field output */ |
| 307 |
|
int n = -bytsiz[i]; |
| 308 |
|
while (n--) { |
| 309 |
|
if (!scanOK(termc[i])) |
| 310 |
|
break; |
| 311 |
< |
fputs(buf, output[i]); |
| 311 |
> |
if (fputs(buf, output[i]) == EOF) |
| 312 |
> |
break; |
| 313 |
|
} |
| 314 |
|
if (n >= 0) /* fell short? */ |
| 315 |
|
break; |
| 316 |
+ |
if (termc[i] != '\n') /* add EOL if none */ |
| 317 |
+ |
fputc('\n', output[i]); |
| 318 |
|
} else { /* 1-field output */ |
| 319 |
|
if (!scanOK(termc[i])) |
| 320 |
|
break; |
| 321 |
< |
fputs(buf, output[i]); |
| 321 |
> |
if (fputs(buf, output[i]) == EOF) |
| 322 |
> |
break; |
| 323 |
> |
if (termc[i] != '\n') /* add EOL if none */ |
| 324 |
> |
fputc('\n', output[i]); |
| 325 |
|
} |
| 326 |
+ |
/* skip input EOL? */ |
| 327 |
+ |
if (!bininp && termc[nfiles-1] != '\n') { |
| 328 |
+ |
int c = getchar(); |
| 329 |
+ |
if ((c != '\n') & (c != EOF)) |
| 330 |
+ |
ungetc(c, stdin); |
| 331 |
+ |
} |
| 332 |
|
} |
| 333 |
|
if (i < nfiles) |
| 334 |
|
break; |
| 335 |
|
} while (--outcnt); |
| 336 |
+ |
/* check ending */ |
| 337 |
+ |
if (fflush(NULL) == EOF) { |
| 338 |
+ |
fputs(argv[0], stderr); |
| 339 |
+ |
fputs(": write error on one or more outputs\n", stderr); |
| 340 |
+ |
return(1); |
| 341 |
+ |
} |
| 342 |
+ |
if (outcnt > 0) { |
| 343 |
+ |
fputs(argv[0], stderr); |
| 344 |
+ |
fputs(": warning: premature EOD\n", stderr); |
| 345 |
+ |
} |
| 346 |
|
return(0); |
| 347 |
|
} |