| 10 |
|
|
| 11 |
|
#include "standard.h" |
| 12 |
|
#include <fcntl.h> |
| 13 |
+ |
|
| 14 |
+ |
#ifndef F_SETLKW |
| 15 |
+ |
|
| 16 |
+ |
main(argc, argv) |
| 17 |
+ |
int argc; |
| 18 |
+ |
char *argv[]; |
| 19 |
+ |
{ |
| 20 |
+ |
fprintf(stderr, "%s: no NFS lock manager on this machine\n", argv[0]); |
| 21 |
+ |
exit(1); |
| 22 |
+ |
} |
| 23 |
+ |
|
| 24 |
+ |
#else |
| 25 |
+ |
|
| 26 |
|
#include <signal.h> |
| 27 |
|
#include "color.h" |
| 28 |
|
#include "view.h" |
| 29 |
|
#include "resolu.h" |
| 30 |
|
|
| 31 |
+ |
#ifndef NFS |
| 32 |
+ |
#define NFS 1 |
| 33 |
+ |
#endif |
| 34 |
+ |
/* set the following to 0 to forgo forking */ |
| 35 |
+ |
#ifndef MAXFORK |
| 36 |
+ |
#if NFS |
| 37 |
+ |
#define MAXFORK 3 /* allotment of duped processes */ |
| 38 |
+ |
#else |
| 39 |
+ |
#define MAXFORK 0 |
| 40 |
+ |
#endif |
| 41 |
+ |
#endif |
| 42 |
+ |
|
| 43 |
|
/* rpict command */ |
| 44 |
|
char *rpargv[128] = {"rpict", "-S", "1", "-x", "512", "-y", "512", "-pa", "1"}; |
| 45 |
|
int rpargc = 9; |
| 46 |
|
int rpd[3]; |
| 47 |
|
FILE *torp, *fromrp; |
| 48 |
< |
COLR *scanline; |
| 48 |
> |
COLR *pbuf; |
| 49 |
|
/* our view parameters */ |
| 50 |
|
VIEW ourview = STDVIEW; |
| 51 |
|
double pixaspect = 1.0; |
| 55 |
|
int outfd; |
| 56 |
|
long scanorig; |
| 57 |
|
int syncfd = -1; /* lock file descriptor */ |
| 58 |
+ |
int nforked = 0; |
| 59 |
|
|
| 60 |
|
char *progname; |
| 61 |
|
int verbose = 0; |
| 122 |
|
case 'F': /* syncronization file */ |
| 123 |
|
if (argv[i][2]) |
| 124 |
|
break; |
| 125 |
< |
if ((syncfd = open(argv[++i], O_RDWR)) < 0) { |
| 125 |
> |
if ((syncfd = open(argv[++i], |
| 126 |
> |
O_RDWR|O_CREAT, 0666)) < 0) { |
| 127 |
|
fprintf(stderr, "%s: cannot open\n", |
| 128 |
|
argv[i]); |
| 129 |
|
exit(1); |
| 130 |
|
} |
| 131 |
|
continue; |
| 132 |
+ |
case 'z': /* z-file ist verbotten */ |
| 133 |
+ |
fprintf(stderr, "%s: -z option not allowed\n", |
| 134 |
+ |
argv[0]); |
| 135 |
+ |
exit(1); |
| 136 |
|
case 'o': /* output file */ |
| 137 |
|
if (argv[i][2]) |
| 138 |
|
break; |
| 148 |
|
} |
| 149 |
|
init(argc, argv); |
| 150 |
|
rpiece(); |
| 151 |
< |
rval = cleanup(); |
| 121 |
< |
exit(rval); |
| 151 |
> |
exit(cleanup(0)); |
| 152 |
|
} |
| 153 |
|
|
| 154 |
|
|
| 188 |
|
} else if ((outfd = open(outfile, O_RDWR)) >= 0) { |
| 189 |
|
if ((fp = fdopen(dup(outfd), "r+")) == NULL) |
| 190 |
|
goto filerr; |
| 191 |
< |
getheader(fp, NULL); /* skip header */ |
| 192 |
< |
if (fscnresolu(&hr, &vr, fp) < 0 || /* check resolution */ |
| 191 |
> |
getheader(fp, NULL, NULL); /* skip header */ |
| 192 |
> |
if (!fscnresolu(&hr, &vr, fp) || /* check resolution */ |
| 193 |
|
hr != hres*hmult || vr != vres*vmult) { |
| 194 |
< |
fprintf(stderr, "%s: picture resolution mismatch\n", |
| 195 |
< |
outfile); |
| 194 |
> |
fprintf(stderr, "%s: resolution mismatch on file \"%s\"\n", |
| 195 |
> |
progname, outfile); |
| 196 |
|
exit(1); |
| 197 |
|
} |
| 198 |
|
} else { |
| 199 |
< |
fprintf(stderr, "%s: cannot open\n", outfile); |
| 199 |
> |
fprintf(stderr, "%s: cannot open file \"%s\"\n", |
| 200 |
> |
progname, outfile); |
| 201 |
|
exit(1); |
| 202 |
|
} |
| 203 |
|
scanorig = ftell(fp); /* record position of first scanline */ |
| 204 |
|
if (fclose(fp) == -1) /* done with stream i/o */ |
| 205 |
|
goto filerr; |
| 206 |
+ |
#if NFS |
| 207 |
+ |
sync(); /* flush NFS buffers */ |
| 208 |
+ |
#endif |
| 209 |
|
/* start rpict process */ |
| 210 |
|
if (open_process(rpd, rpargv) <= 0) { |
| 211 |
|
fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]); |
| 217 |
|
progname, rpargv[0]); |
| 218 |
|
exit(1); |
| 219 |
|
} |
| 220 |
< |
if ((scanline = (COLR *)malloc(hres*sizeof(COLR))) == NULL) { |
| 220 |
> |
if ((pbuf = (COLR *)malloc(hres*vres*sizeof(COLR))) == NULL) { |
| 221 |
|
fprintf(stderr, "%s: out of memory\n", progname); |
| 222 |
|
exit(1); |
| 223 |
|
} |
| 224 |
|
signal(SIGALRM, onalrm); |
| 225 |
|
return; |
| 226 |
|
filerr: |
| 227 |
< |
fprintf(stderr, "%s: file i/o error\n", outfile); |
| 227 |
> |
fprintf(stderr, "%s: i/o error on file \"%s\"\n", progname, outfile); |
| 228 |
|
exit(1); |
| 229 |
|
} |
| 230 |
|
|
| 248 |
|
lseek(syncfd, 0L, 0); |
| 249 |
|
buf[read(syncfd, buf, sizeof(buf)-1)] = '\0'; |
| 250 |
|
if (sscanf(buf, "%*d %*d %d %d", xp, yp) < 2) { |
| 217 |
– |
*xp = hmult; |
| 218 |
– |
*yp = vmult-1; |
| 219 |
– |
} |
| 220 |
– |
if (--(*xp) < 0) { /* decrement position */ |
| 251 |
|
*xp = hmult-1; |
| 252 |
< |
if (--(*yp) < 0) { /* all done! */ |
| 252 |
> |
*yp = vmult; |
| 253 |
> |
} |
| 254 |
> |
if (--(*yp) < 0) { /* decrement position */ |
| 255 |
> |
*yp = vmult-1; |
| 256 |
> |
if (--(*xp) < 0) { /* all done! */ |
| 257 |
|
close(syncfd); |
| 258 |
|
return(0); |
| 259 |
|
} |
| 260 |
|
} |
| 261 |
< |
sprintf(buf, "%d %d\n%d %d\n", hmult, vmult, *xp, *yp); |
| 261 |
> |
sprintf(buf, "%4d %4d\n%4d %4d\n", hmult, vmult, *xp, *yp); |
| 262 |
|
lseek(syncfd, 0L, 0); /* write new position */ |
| 263 |
|
write(syncfd, buf, strlen(buf)); |
| 264 |
|
fls.l_type = F_UNLCK; /* release sync file */ |
| 270 |
|
if (sscanf(buf, "%d %d", xp, yp) == 2) |
| 271 |
|
return(1); |
| 272 |
|
fprintf(stderr, "%s: input format error\n", progname); |
| 273 |
< |
exit(1); |
| 273 |
> |
exit(cleanup(1)); |
| 274 |
|
} |
| 275 |
|
|
| 276 |
|
|
| 277 |
|
int |
| 278 |
< |
cleanup() /* close rpict process and clean up */ |
| 278 |
> |
cleanup(rstat) /* close rpict process and clean up */ |
| 279 |
> |
int rstat; |
| 280 |
|
{ |
| 281 |
< |
register int rpstat; |
| 281 |
> |
int status; |
| 282 |
|
|
| 283 |
< |
free((char *)scanline); |
| 283 |
> |
free((char *)pbuf); |
| 284 |
|
fclose(torp); |
| 285 |
|
fclose(fromrp); |
| 286 |
< |
rpstat = close_process(rpd); |
| 287 |
< |
if (rpstat == -1) |
| 288 |
< |
rpstat = 0; |
| 289 |
< |
return(rpstat); |
| 286 |
> |
while (wait(&status) != -1) |
| 287 |
> |
if (rstat == 0) |
| 288 |
> |
rstat = status>>8 & 0xff; |
| 289 |
> |
return(rstat); |
| 290 |
|
} |
| 291 |
|
|
| 292 |
|
|
| 294 |
|
{ |
| 295 |
|
VIEW pview; |
| 296 |
|
int xorg, yorg; |
| 297 |
< |
|
| 297 |
> |
/* compute view parameters */ |
| 298 |
> |
copystruct(&pview, &ourview); |
| 299 |
> |
switch (ourview.type) { |
| 300 |
> |
case VT_PER: |
| 301 |
> |
pview.horiz = 2.*180./PI*atan( |
| 302 |
> |
tan(PI/180./2.*ourview.horiz)/hmult ); |
| 303 |
> |
pview.vert = 2.*180./PI*atan( |
| 304 |
> |
tan(PI/180./2.*ourview.vert)/vmult ); |
| 305 |
> |
break; |
| 306 |
> |
case VT_PAR: |
| 307 |
> |
case VT_ANG: |
| 308 |
> |
pview.horiz = ourview.horiz / hmult; |
| 309 |
> |
pview.vert = ourview.vert / vmult; |
| 310 |
> |
break; |
| 311 |
> |
case VT_HEM: |
| 312 |
> |
pview.horiz = 2.*180./PI*asin( |
| 313 |
> |
sin(PI/180./2.*ourview.horiz)/hmult ); |
| 314 |
> |
pview.vert = 2.*180./PI*asin( |
| 315 |
> |
sin(PI/180./2.*ourview.vert)/vmult ); |
| 316 |
> |
break; |
| 317 |
> |
default: |
| 318 |
> |
fprintf(stderr, "%s: unknown view type '-vt%c'\n", |
| 319 |
> |
progname, ourview.type); |
| 320 |
> |
exit(cleanup(1)); |
| 321 |
> |
} |
| 322 |
> |
/* render each piece */ |
| 323 |
|
while (nextpiece(&xorg, &yorg)) { |
| 324 |
< |
copystruct(&pview, &ourview); /* compute view for piece */ |
| 325 |
< |
switch (ourview.type) { |
| 266 |
< |
case VT_PER: |
| 267 |
< |
pview.horiz = 2.*180./PI*atan( |
| 268 |
< |
tan(PI/180./2.*ourview.horiz)/hmult ); |
| 269 |
< |
pview.vert = 2.*180./PI*atan( |
| 270 |
< |
tan(PI/180./2.*ourview.vert)/vmult ); |
| 271 |
< |
break; |
| 272 |
< |
case VT_PAR: |
| 273 |
< |
case VT_ANG: |
| 274 |
< |
pview.horiz = ourview.horiz / hmult; |
| 275 |
< |
pview.vert = ourview.vert / vmult; |
| 276 |
< |
break; |
| 277 |
< |
case VT_HEM: |
| 278 |
< |
pview.horiz = 2.*180./PI*asin( |
| 279 |
< |
sin(PI/180./2.*ourview.horiz)/hmult ); |
| 280 |
< |
pview.vert = 2.*180./PI*asin( |
| 281 |
< |
sin(PI/180./2.*ourview.vert)/vmult ); |
| 282 |
< |
break; |
| 283 |
< |
default: |
| 284 |
< |
fprintf(stderr, "%s: unknown view type '-vt%c'\n", |
| 285 |
< |
progname, ourview.type); |
| 286 |
< |
exit(1); |
| 287 |
< |
} |
| 288 |
< |
pview.hoff += xorg - 0.5*(hmult-1); |
| 289 |
< |
pview.voff += yorg - 0.5*(vmult-1); |
| 324 |
> |
pview.hoff = ourview.hoff + xorg - 0.5*(hmult-1); |
| 325 |
> |
pview.voff = ourview.voff + yorg - 0.5*(vmult-1); |
| 326 |
|
fputs(VIEWSTR, torp); |
| 327 |
|
fprintview(&pview, torp); |
| 328 |
|
putc('\n', torp); |
| 329 |
< |
fflush(torp); /* assign piece to rpict */ |
| 329 |
> |
fflush(torp); /* assigns piece to rpict */ |
| 330 |
|
putpiece(xorg, yorg); /* place piece in output */ |
| 331 |
|
if (verbose) { /* notify caller */ |
| 332 |
|
printf("%d %d done\n", xorg, yorg); |
| 336 |
|
} |
| 337 |
|
|
| 338 |
|
|
| 339 |
+ |
int |
| 340 |
|
putpiece(xpos, ypos) /* get next piece from rpict */ |
| 341 |
|
int xpos, ypos; |
| 342 |
|
{ |
| 343 |
|
struct flock fls; |
| 344 |
+ |
int pid, status; |
| 345 |
|
int hr, vr; |
| 346 |
< |
int y; |
| 347 |
< |
|
| 346 |
> |
register int y; |
| 347 |
> |
/* check bounds */ |
| 348 |
|
if (xpos < 0 | ypos < 0 | xpos >= hmult | ypos >= vmult) { |
| 349 |
|
fprintf(stderr, "%s: requested piece (%d,%d) out of range\n", |
| 350 |
|
progname, xpos, ypos); |
| 351 |
< |
exit(1); |
| 351 |
> |
exit(cleanup(1)); |
| 352 |
|
} |
| 353 |
< |
getheader(fromrp, NULL); /* discard header info. */ |
| 354 |
< |
if (fscnresolu(&hr, &vr, fromrp) < 0 || /* check resolution */ |
| 355 |
< |
hr != hres || vr != vres) { |
| 353 |
> |
/* check header from rpict */ |
| 354 |
> |
getheader(fromrp, NULL, NULL); |
| 355 |
> |
if (!fscnresolu(&hr, &vr, fromrp) || hr != hres | vr != vres) { |
| 356 |
|
fprintf(stderr, "%s: resolution mismatch from %s\n", |
| 357 |
|
progname, rpargv[0]); |
| 358 |
< |
exit(1); |
| 358 |
> |
exit(cleanup(1)); |
| 359 |
|
} |
| 360 |
< |
fls.l_whence = 1; |
| 361 |
< |
fls.l_start = 0L; |
| 362 |
< |
fls.l_len = hr*sizeof(COLR); |
| 325 |
< |
for (y = 0; y < vr; y++) { /* transfer scanlines */ |
| 326 |
< |
if (freadcolrs(scanline, hr, fromrp) < 0) { |
| 360 |
> |
/* load new piece into buffer */ |
| 361 |
> |
for (y = 0; y < vr; y++) |
| 362 |
> |
if (freadcolrs(pbuf+y*hr, hr, fromrp) < 0) { |
| 363 |
|
fprintf(stderr, "%s: read error from %s\n", |
| 364 |
|
progname, rpargv[0]); |
| 365 |
< |
exit(1); |
| 365 |
> |
exit(cleanup(1)); |
| 366 |
|
} |
| 367 |
< |
if ((y == 0 || hmult != 1) && lseek(outfd, |
| 368 |
< |
scanorig+((long)((vmult-1-ypos)*vres+y)*hmult*hres+xpos*hres)*sizeof(COLR), |
| 369 |
< |
0) == -1) { |
| 370 |
< |
fprintf(stderr, "%s: seek error\n", outfile); |
| 371 |
< |
exit(1); |
| 367 |
> |
#if MAXFORK |
| 368 |
> |
/* fork so we don't slow rpict down */ |
| 369 |
> |
if ((pid = fork()) > 0) { |
| 370 |
> |
if (++nforked >= MAXFORK) { |
| 371 |
> |
wait(&status); /* reap a child */ |
| 372 |
> |
if (status) |
| 373 |
> |
exit(cleanup(status>>8 & 0xff)); |
| 374 |
> |
nforked--; |
| 375 |
|
} |
| 376 |
< |
fls.l_type = F_WRLCK; |
| 377 |
< |
fcntl(outfd, F_SETLKW, &fls); |
| 378 |
< |
if (writebuf(outfd, (char *)scanline, hr*sizeof(COLR)) != |
| 379 |
< |
hr*sizeof(COLR)) { |
| 380 |
< |
fprintf(stderr, "%s: write error\n", outfile); |
| 381 |
< |
exit(1); |
| 376 |
> |
return(pid); |
| 377 |
> |
} |
| 378 |
> |
#else |
| 379 |
> |
pid = -1; /* no forking */ |
| 380 |
> |
#endif |
| 381 |
> |
fls.l_len = (long)vres*hmult*hres*sizeof(COLR); |
| 382 |
> |
fls.l_start = scanorig + (vmult-1-ypos)*fls.l_len; |
| 383 |
> |
#if NFS |
| 384 |
> |
/* lock file section so NFS doesn't mess up */ |
| 385 |
> |
fls.l_whence = 0; |
| 386 |
> |
fls.l_type = F_WRLCK; |
| 387 |
> |
fcntl(outfd, F_SETLKW, &fls); |
| 388 |
> |
#endif |
| 389 |
> |
/* write new piece to file */ |
| 390 |
> |
if (lseek(outfd, fls.l_start+(long)xpos*hres*sizeof(COLR), 0) == -1) |
| 391 |
> |
goto seekerr; |
| 392 |
> |
if (hmult == 1) { |
| 393 |
> |
if (writebuf(outfd, (char *)pbuf, |
| 394 |
> |
vr*hr*sizeof(COLR)) != vr*hr*sizeof(COLR)) |
| 395 |
> |
goto writerr; |
| 396 |
> |
} else |
| 397 |
> |
for (y = 0; y < vr; y++) { |
| 398 |
> |
if (writebuf(outfd, (char *)(pbuf+y*hr), |
| 399 |
> |
hr*sizeof(COLR)) != hr*sizeof(COLR)) |
| 400 |
> |
goto writerr; |
| 401 |
> |
if (y < vr-1 && lseek(outfd, |
| 402 |
> |
(long)(hmult-1)*hr*sizeof(COLR), |
| 403 |
> |
1) == -1) |
| 404 |
> |
goto seekerr; |
| 405 |
|
} |
| 406 |
< |
fls.l_type = F_UNLCK; |
| 406 |
> |
if (pid == -1) { /* fork failed */ |
| 407 |
> |
#if NFS |
| 408 |
> |
fls.l_type = F_UNLCK; /* release lock */ |
| 409 |
|
fcntl(outfd, F_SETLKW, &fls); |
| 410 |
+ |
#endif |
| 411 |
+ |
return(0); |
| 412 |
|
} |
| 413 |
+ |
_exit(0); /* else exit child process (releasing lock) */ |
| 414 |
+ |
seekerr: |
| 415 |
+ |
fprintf(stderr, "%s: seek error on file \"%s\"\n", progname, outfile); |
| 416 |
+ |
_exit(1); |
| 417 |
+ |
writerr: |
| 418 |
+ |
fprintf(stderr, "%s: write error on file \"%s\"\n", progname, outfile); |
| 419 |
+ |
_exit(1); |
| 420 |
|
} |
| 421 |
+ |
|
| 422 |
+ |
#endif |