| 22 |
|
static int swapped = 0; /* input is byte-swapped */ |
| 23 |
|
|
| 24 |
|
static FILE *output[MAXFILE]; |
| 25 |
+ |
static int ncomp[MAXFILE]; |
| 26 |
|
static int bytsiz[MAXFILE]; |
| 27 |
< |
static short hdrflags[MAXFILE]; |
| 27 |
> |
static int hdrflags[MAXFILE]; |
| 28 |
|
static const char *format[MAXFILE]; |
| 29 |
|
static int termc[MAXFILE]; |
| 30 |
|
static int nfiles = 0; |
| 65 |
|
static int |
| 66 |
|
scanOK(int termc) |
| 67 |
|
{ |
| 68 |
+ |
int skip_white = (termc == ' '); |
| 69 |
|
char *cp = buf; |
| 70 |
|
int c; |
| 71 |
|
|
| 72 |
|
while ((c = getchar()) != EOF) { |
| 73 |
+ |
if (skip_white && isspace(c)) |
| 74 |
+ |
continue; |
| 75 |
+ |
skip_white = 0; |
| 76 |
+ |
if (c == '\n' && isspace(termc)) |
| 77 |
+ |
c = termc; /* forgiving assumption */ |
| 78 |
|
*cp++ = c; |
| 79 |
|
if (cp-buf >= sizeof(buf)) |
| 80 |
|
break; |
| 81 |
< |
if (c == termc) { |
| 81 |
> |
if ((termc == ' ') ? isspace(c) : (c == termc)) { |
| 82 |
|
*cp = '\0'; |
| 83 |
|
return(cp-buf); |
| 84 |
|
} |
| 97 |
|
long outcnt = 0; |
| 98 |
|
int bininp = 0; |
| 99 |
|
int curterm = '\n'; |
| 100 |
+ |
int curncomp = 1; |
| 101 |
|
int curbytes = 0; |
| 102 |
|
int curflags = 0; |
| 103 |
|
const char *curfmt = "ascii"; |
| 167 |
|
break; |
| 168 |
|
case 'a': |
| 169 |
|
curfmt = "ascii"; |
| 170 |
< |
curbytes = -1; |
| 170 |
> |
curbytes = 0; |
| 171 |
|
break; |
| 172 |
|
default: |
| 173 |
|
goto badopt; |
| 174 |
|
} |
| 175 |
< |
if (isdigit(argv[i][3])) |
| 176 |
< |
curbytes *= atoi(argv[i]+3); |
| 177 |
< |
curbytes += (curbytes == -1); |
| 170 |
< |
if (curbytes > (int)sizeof(buf)) { |
| 175 |
> |
curncomp = isdigit(argv[i][3]) ? |
| 176 |
> |
atoi(argv[i]+3) : 1 ; |
| 177 |
> |
if (curbytes*curncomp > (int)sizeof(buf)) { |
| 178 |
|
fputs(argv[0], stderr); |
| 179 |
|
fputs(": output size too big\n", stderr); |
| 180 |
|
return(1); |
| 189 |
|
if (curbytes > 0) |
| 190 |
|
SET_FILE_BINARY(output[nfiles]); |
| 191 |
|
format[nfiles] = curfmt; |
| 192 |
+ |
ncomp[nfiles] = curncomp; |
| 193 |
|
bytsiz[nfiles++] = curbytes; |
| 194 |
|
break; |
| 195 |
|
badopt:; |
| 198 |
|
fputs(": bad option\n", stderr); |
| 199 |
|
return(1); |
| 200 |
|
} |
| 201 |
+ |
} else if (argv[i][0] == '.' && !argv[i][1]) { |
| 202 |
+ |
output[nfiles] = NULL; /* discard data */ |
| 203 |
+ |
termc[nfiles] = curterm; |
| 204 |
+ |
format[nfiles] = curfmt; |
| 205 |
+ |
ncomp[nfiles] = curncomp; |
| 206 |
+ |
bytsiz[nfiles++] = curbytes; |
| 207 |
|
} else if (argv[i][0] == '!') { |
| 208 |
|
needres |= (curflags & DORESOLU); |
| 209 |
|
termc[nfiles] = curterm; |
| 216 |
|
if (curbytes > 0) |
| 217 |
|
SET_FILE_BINARY(output[nfiles]); |
| 218 |
|
format[nfiles] = curfmt; |
| 219 |
+ |
ncomp[nfiles] = curncomp; |
| 220 |
|
bytsiz[nfiles++] = curbytes; |
| 221 |
|
} else { |
| 222 |
|
if (append & (curflags != 0)) { |
| 243 |
|
if (curbytes > 0) |
| 244 |
|
SET_FILE_BINARY(output[nfiles]); |
| 245 |
|
format[nfiles] = curfmt; |
| 246 |
+ |
ncomp[nfiles] = curncomp; |
| 247 |
|
bytsiz[nfiles++] = curbytes; |
| 248 |
|
} |
| 249 |
|
if (nfiles >= MAXFILE) { |
| 262 |
|
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
| 263 |
|
flockfile(stdin); |
| 264 |
|
for (i = nfiles; i--; ) |
| 265 |
< |
flockfile(output[i]); |
| 265 |
> |
if (output[i] != NULL) |
| 266 |
> |
flockfile(output[i]); |
| 267 |
|
#endif |
| 268 |
|
/* load/copy header */ |
| 269 |
|
if (inpflags & DOHEADER && getheader(stdin, headline, NULL) < 0) { |
| 298 |
|
if (!(inpflags & DOHEADER)) |
| 299 |
|
newheader("RADIANCE", output[i]); |
| 300 |
|
printargs(argc, argv, output[i]); |
| 301 |
+ |
fprintf(output[i], "NCOMP=%d\n", ncomp[i]); |
| 302 |
|
if (format[i] != NULL) { |
| 303 |
|
extern const char BIGEND[]; |
| 304 |
|
if ((format[i][0] == 'f') | |
| 317 |
|
do { /* main loop */ |
| 318 |
|
for (i = 0; i < nfiles; i++) { |
| 319 |
|
if (bytsiz[i] > 0) { /* binary output */ |
| 320 |
< |
if (getbinary(buf, bytsiz[i], 1, stdin) < 1) |
| 320 |
> |
if (getbinary(buf, bytsiz[i], ncomp[i], |
| 321 |
> |
stdin) < ncomp[i]) |
| 322 |
|
break; |
| 323 |
< |
if (putbinary(buf, bytsiz[i], 1, output[i]) < 1) |
| 323 |
> |
if (output[i] != NULL && |
| 324 |
> |
putbinary(buf, bytsiz[i], ncomp[i], |
| 325 |
> |
output[i]) < ncomp[i]) |
| 326 |
|
break; |
| 327 |
< |
} else if (bytsiz[i] < 0) { /* N-field output */ |
| 328 |
< |
int n = -bytsiz[i]; |
| 327 |
> |
} else if (ncomp[i] > 1) { /* N-field output */ |
| 328 |
> |
int n = ncomp[i]; |
| 329 |
|
while (n--) { |
| 330 |
|
if (!scanOK(termc[i])) |
| 331 |
|
break; |
| 332 |
< |
if (fputs(buf, output[i]) == EOF) |
| 332 |
> |
if (output[i] != NULL && |
| 333 |
> |
fputs(buf, output[i]) == EOF) |
| 334 |
|
break; |
| 335 |
|
} |
| 336 |
|
if (n >= 0) /* fell short? */ |
| 337 |
|
break; |
| 338 |
< |
if (termc[i] != '\n') /* add EOL if none */ |
| 338 |
> |
if ((output[i] != NULL) & /* add EOL if none */ |
| 339 |
> |
(termc[i] != '\n')) |
| 340 |
|
fputc('\n', output[i]); |
| 341 |
|
} else { /* 1-field output */ |
| 342 |
|
if (!scanOK(termc[i])) |
| 343 |
|
break; |
| 344 |
< |
if (fputs(buf, output[i]) == EOF) |
| 345 |
< |
break; |
| 346 |
< |
if (termc[i] != '\n') /* add EOL if none */ |
| 347 |
< |
fputc('\n', output[i]); |
| 344 |
> |
if (output[i] != NULL) { |
| 345 |
> |
if (fputs(buf, output[i]) == EOF) |
| 346 |
> |
break; |
| 347 |
> |
if (termc[i] != '\n') /* add EOL? */ |
| 348 |
> |
fputc('\n', output[i]); |
| 349 |
> |
} |
| 350 |
|
} |
| 351 |
|
/* skip input EOL? */ |
| 352 |
|
if (!bininp && termc[nfiles-1] != '\n') { |