| 15 |
|
#include "view.h" |
| 16 |
|
#include "resolu.h" |
| 17 |
|
|
| 18 |
+ |
#ifndef NFS |
| 19 |
+ |
#define NFS 1 |
| 20 |
+ |
#endif |
| 21 |
|
/* set the following to 0 to forgo forking */ |
| 22 |
|
#ifndef MAXFORK |
| 23 |
< |
#define MAXFORK 2 /* max. unreaped child processes */ |
| 23 |
> |
#if NFS |
| 24 |
> |
#define MAXFORK 3 /* allotment of duped processes */ |
| 25 |
> |
#else |
| 26 |
> |
#define MAXFORK 0 |
| 27 |
|
#endif |
| 28 |
+ |
#endif |
| 29 |
|
|
| 30 |
|
/* rpict command */ |
| 31 |
|
char *rpargv[128] = {"rpict", "-S", "1", "-x", "512", "-y", "512", "-pa", "1"}; |
| 130 |
|
} |
| 131 |
|
init(argc, argv); |
| 132 |
|
rpiece(); |
| 133 |
< |
rval = cleanup(); |
| 133 |
> |
rval = cleanup(0); |
| 134 |
|
exit(rval); |
| 135 |
|
} |
| 136 |
|
|
| 171 |
|
} else if ((outfd = open(outfile, O_RDWR)) >= 0) { |
| 172 |
|
if ((fp = fdopen(dup(outfd), "r+")) == NULL) |
| 173 |
|
goto filerr; |
| 174 |
< |
getheader(fp, NULL); /* skip header */ |
| 174 |
> |
getheader(fp, NULL, NULL); /* skip header */ |
| 175 |
|
if (fscnresolu(&hr, &vr, fp) < 0 || /* check resolution */ |
| 176 |
|
hr != hres*hmult || vr != vres*vmult) { |
| 177 |
|
fprintf(stderr, "%s: resolution mismatch on file \"%s\"\n", |
| 186 |
|
scanorig = ftell(fp); /* record position of first scanline */ |
| 187 |
|
if (fclose(fp) == -1) /* done with stream i/o */ |
| 188 |
|
goto filerr; |
| 189 |
+ |
#if NFS |
| 190 |
|
sync(); /* flush NFS buffers */ |
| 191 |
+ |
#endif |
| 192 |
|
/* start rpict process */ |
| 193 |
|
if (open_process(rpd, rpargv) <= 0) { |
| 194 |
|
fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]); |
| 253 |
|
if (sscanf(buf, "%d %d", xp, yp) == 2) |
| 254 |
|
return(1); |
| 255 |
|
fprintf(stderr, "%s: input format error\n", progname); |
| 256 |
< |
exit(1); |
| 256 |
> |
exit(cleanup(1)); |
| 257 |
|
} |
| 258 |
|
|
| 259 |
|
|
| 260 |
|
int |
| 261 |
< |
cleanup() /* close rpict process and clean up */ |
| 261 |
> |
cleanup(rstat) /* close rpict process and clean up */ |
| 262 |
> |
int rstat; |
| 263 |
|
{ |
| 264 |
< |
register int pid; |
| 255 |
< |
int status, rstat = 0; |
| 264 |
> |
int status; |
| 265 |
|
|
| 266 |
|
free((char *)pbuf); |
| 267 |
|
fclose(torp); |
| 268 |
|
fclose(fromrp); |
| 269 |
< |
while ((pid = wait(&status)) != -1) |
| 269 |
> |
while (wait(&status) != -1) |
| 270 |
|
if (rstat == 0) |
| 271 |
|
rstat = status>>8 & 0xff; |
| 272 |
|
return(rstat); |
| 277 |
|
{ |
| 278 |
|
VIEW pview; |
| 279 |
|
int xorg, yorg; |
| 280 |
< |
|
| 280 |
> |
/* compute view parameters */ |
| 281 |
> |
copystruct(&pview, &ourview); |
| 282 |
> |
switch (ourview.type) { |
| 283 |
> |
case VT_PER: |
| 284 |
> |
pview.horiz = 2.*180./PI*atan( |
| 285 |
> |
tan(PI/180./2.*ourview.horiz)/hmult ); |
| 286 |
> |
pview.vert = 2.*180./PI*atan( |
| 287 |
> |
tan(PI/180./2.*ourview.vert)/vmult ); |
| 288 |
> |
break; |
| 289 |
> |
case VT_PAR: |
| 290 |
> |
case VT_ANG: |
| 291 |
> |
pview.horiz = ourview.horiz / hmult; |
| 292 |
> |
pview.vert = ourview.vert / vmult; |
| 293 |
> |
break; |
| 294 |
> |
case VT_HEM: |
| 295 |
> |
pview.horiz = 2.*180./PI*asin( |
| 296 |
> |
sin(PI/180./2.*ourview.horiz)/hmult ); |
| 297 |
> |
pview.vert = 2.*180./PI*asin( |
| 298 |
> |
sin(PI/180./2.*ourview.vert)/vmult ); |
| 299 |
> |
break; |
| 300 |
> |
default: |
| 301 |
> |
fprintf(stderr, "%s: unknown view type '-vt%c'\n", |
| 302 |
> |
progname, ourview.type); |
| 303 |
> |
exit(cleanup(1)); |
| 304 |
> |
} |
| 305 |
> |
/* render each piece */ |
| 306 |
|
while (nextpiece(&xorg, &yorg)) { |
| 307 |
< |
copystruct(&pview, &ourview); /* compute view for piece */ |
| 308 |
< |
switch (ourview.type) { |
| 275 |
< |
case VT_PER: |
| 276 |
< |
pview.horiz = 2.*180./PI*atan( |
| 277 |
< |
tan(PI/180./2.*ourview.horiz)/hmult ); |
| 278 |
< |
pview.vert = 2.*180./PI*atan( |
| 279 |
< |
tan(PI/180./2.*ourview.vert)/vmult ); |
| 280 |
< |
break; |
| 281 |
< |
case VT_PAR: |
| 282 |
< |
case VT_ANG: |
| 283 |
< |
pview.horiz = ourview.horiz / hmult; |
| 284 |
< |
pview.vert = ourview.vert / vmult; |
| 285 |
< |
break; |
| 286 |
< |
case VT_HEM: |
| 287 |
< |
pview.horiz = 2.*180./PI*asin( |
| 288 |
< |
sin(PI/180./2.*ourview.horiz)/hmult ); |
| 289 |
< |
pview.vert = 2.*180./PI*asin( |
| 290 |
< |
sin(PI/180./2.*ourview.vert)/vmult ); |
| 291 |
< |
break; |
| 292 |
< |
default: |
| 293 |
< |
fprintf(stderr, "%s: unknown view type '-vt%c'\n", |
| 294 |
< |
progname, ourview.type); |
| 295 |
< |
exit(1); |
| 296 |
< |
} |
| 297 |
< |
pview.hoff += xorg - 0.5*(hmult-1); |
| 298 |
< |
pview.voff += yorg - 0.5*(vmult-1); |
| 307 |
> |
pview.hoff = ourview.hoff + xorg - 0.5*(hmult-1); |
| 308 |
> |
pview.voff = ourview.voff + yorg - 0.5*(vmult-1); |
| 309 |
|
fputs(VIEWSTR, torp); |
| 310 |
|
fprintview(&pview, torp); |
| 311 |
|
putc('\n', torp); |
| 312 |
< |
fflush(torp); /* assign piece to rpict */ |
| 312 |
> |
fflush(torp); /* assigns piece to rpict */ |
| 313 |
|
putpiece(xorg, yorg); /* place piece in output */ |
| 314 |
|
if (verbose) { /* notify caller */ |
| 315 |
|
printf("%d %d done\n", xorg, yorg); |
| 331 |
|
if (xpos < 0 | ypos < 0 | xpos >= hmult | ypos >= vmult) { |
| 332 |
|
fprintf(stderr, "%s: requested piece (%d,%d) out of range\n", |
| 333 |
|
progname, xpos, ypos); |
| 334 |
< |
exit(1); |
| 334 |
> |
exit(cleanup(1)); |
| 335 |
|
} |
| 336 |
|
/* check header from rpict */ |
| 337 |
< |
getheader(fromrp, NULL); |
| 338 |
< |
if (fscnresolu(&hr, &vr, fromrp) < 0 || hr != hres || vr != vres) { |
| 337 |
> |
getheader(fromrp, NULL, NULL); |
| 338 |
> |
if (fscnresolu(&hr, &vr, fromrp) < 0 || hr != hres | vr != vres) { |
| 339 |
|
fprintf(stderr, "%s: resolution mismatch from %s\n", |
| 340 |
|
progname, rpargv[0]); |
| 341 |
< |
exit(1); |
| 341 |
> |
exit(cleanup(1)); |
| 342 |
|
} |
| 343 |
|
/* load new piece into buffer */ |
| 344 |
|
for (y = 0; y < vr; y++) |
| 345 |
|
if (freadcolrs(pbuf+y*hr, hr, fromrp) < 0) { |
| 346 |
|
fprintf(stderr, "%s: read error from %s\n", |
| 347 |
|
progname, rpargv[0]); |
| 348 |
< |
exit(1); |
| 348 |
> |
exit(cleanup(1)); |
| 349 |
|
} |
| 350 |
|
#if MAXFORK |
| 351 |
|
/* fork so we don't slow rpict down */ |
| 352 |
|
if ((pid = fork()) > 0) { |
| 353 |
< |
if (++nforked > MAXFORK) { |
| 353 |
> |
if (++nforked >= MAXFORK) { |
| 354 |
|
wait(&status); /* reap a child */ |
| 355 |
|
if (status) |
| 356 |
< |
exit(status>>8 & 0xff); |
| 356 |
> |
exit(cleanup(status>>8 & 0xff)); |
| 357 |
|
nforked--; |
| 358 |
|
} |
| 359 |
|
return(pid); |
| 361 |
|
#else |
| 362 |
|
pid = -1; /* no forking */ |
| 363 |
|
#endif |
| 364 |
+ |
#if NFS |
| 365 |
|
/* lock file section so NFS doesn't mess up */ |
| 366 |
|
fls.l_whence = 0; |
| 367 |
|
fls.l_len = (long)vres*hmult*hres*sizeof(COLR); |
| 368 |
|
fls.l_start = scanorig + (vmult-1-ypos)*fls.l_len; |
| 369 |
|
fls.l_type = F_WRLCK; |
| 370 |
|
fcntl(outfd, F_SETLKW, &fls); |
| 371 |
+ |
#endif |
| 372 |
|
/* write new piece to file */ |
| 373 |
|
if (lseek(outfd, fls.l_start+(long)xpos*hres*sizeof(COLR), 0) == -1) |
| 374 |
|
goto seekerr; |
| 387 |
|
goto seekerr; |
| 388 |
|
} |
| 389 |
|
if (pid == -1) { /* fork failed */ |
| 390 |
+ |
#if NFS |
| 391 |
|
fls.l_type = F_UNLCK; /* release lock */ |
| 392 |
|
fcntl(outfd, F_SETLKW, &fls); |
| 393 |
+ |
#endif |
| 394 |
|
return(0); |
| 395 |
|
} |
| 396 |
|
_exit(0); /* else exit child process (releasing lock) */ |