| 10 |
|
|
| 11 |
|
#include "standard.h" |
| 12 |
|
#include <fcntl.h> |
| 13 |
+ |
#include <signal.h> |
| 14 |
|
#include "color.h" |
| 15 |
|
#include "view.h" |
| 16 |
|
#include "resolu.h" |
| 17 |
|
|
| 18 |
+ |
/* set the following to 0 to forgo forking */ |
| 19 |
+ |
#ifndef MAXFORK |
| 20 |
+ |
#define MAXFORK 2 /* max. unreaped child processes */ |
| 21 |
+ |
#endif |
| 22 |
+ |
|
| 23 |
|
/* rpict command */ |
| 24 |
|
char *rpargv[128] = {"rpict", "-S", "1", "-x", "512", "-y", "512", "-pa", "1"}; |
| 25 |
|
int rpargc = 9; |
| 26 |
|
int rpd[3]; |
| 27 |
|
FILE *torp, *fromrp; |
| 28 |
< |
COLR *scanline; |
| 28 |
> |
COLR *pbuf; |
| 29 |
|
/* our view parameters */ |
| 30 |
|
VIEW ourview = STDVIEW; |
| 31 |
|
double pixaspect = 1.0; |
| 34 |
|
char *outfile = NULL; |
| 35 |
|
int outfd; |
| 36 |
|
long scanorig; |
| 37 |
+ |
int syncfd = -1; /* lock file descriptor */ |
| 38 |
+ |
int nforked = 0; |
| 39 |
|
|
| 40 |
|
char *progname; |
| 41 |
|
int verbose = 0; |
| 42 |
|
|
| 43 |
|
extern long lseek(), ftell(); |
| 44 |
|
|
| 45 |
+ |
int gotalrm = 0; |
| 46 |
+ |
int onalrm() { gotalrm++; } |
| 47 |
|
|
| 48 |
+ |
|
| 49 |
|
main(argc, argv) |
| 50 |
|
int argc; |
| 51 |
|
char *argv[]; |
| 99 |
|
break; |
| 100 |
|
vmult = atoi(argv[++i]); |
| 101 |
|
continue; |
| 102 |
+ |
case 'F': /* syncronization file */ |
| 103 |
+ |
if (argv[i][2]) |
| 104 |
+ |
break; |
| 105 |
+ |
if ((syncfd = open(argv[++i], O_RDWR)) < 0) { |
| 106 |
+ |
fprintf(stderr, "%s: cannot open\n", |
| 107 |
+ |
argv[i]); |
| 108 |
+ |
exit(1); |
| 109 |
+ |
} |
| 110 |
+ |
continue; |
| 111 |
|
case 'o': /* output file */ |
| 112 |
|
if (argv[i][2]) |
| 113 |
|
break; |
| 141 |
|
fprintf(stderr, "%s: %s\n", progname, err); |
| 142 |
|
exit(1); |
| 143 |
|
} |
| 144 |
+ |
if (syncfd != -1) { |
| 145 |
+ |
char buf[32]; |
| 146 |
+ |
buf[read(syncfd, buf, sizeof(buf)-1)] = '\0'; |
| 147 |
+ |
sscanf(buf, "%d %d", &hmult, &vmult); |
| 148 |
+ |
} |
| 149 |
|
normaspect(viewaspect(&ourview)*hmult/vmult, &pixaspect, &hres, &vres); |
| 150 |
|
/* open output file */ |
| 151 |
|
if ((outfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) >= 0) { |
| 162 |
|
putc('\n', fp); |
| 163 |
|
fprtresolu(hres*hmult, vres*vmult, fp); |
| 164 |
|
} else if ((outfd = open(outfile, O_RDWR)) >= 0) { |
| 140 |
– |
sleep(30); /* wait for header */ |
| 165 |
|
if ((fp = fdopen(dup(outfd), "r+")) == NULL) |
| 166 |
|
goto filerr; |
| 167 |
|
getheader(fp, NULL); /* skip header */ |
| 168 |
|
if (fscnresolu(&hr, &vr, fp) < 0 || /* check resolution */ |
| 169 |
|
hr != hres*hmult || vr != vres*vmult) { |
| 170 |
< |
fprintf(stderr, "%s: picture resolution mismatch\n", |
| 171 |
< |
outfile); |
| 170 |
> |
fprintf(stderr, "%s: resolution mismatch on file \"%s\"\n", |
| 171 |
> |
progname, outfile); |
| 172 |
|
exit(1); |
| 173 |
|
} |
| 174 |
|
} else { |
| 175 |
< |
fprintf(stderr, "%s: cannot open\n", outfile); |
| 175 |
> |
fprintf(stderr, "%s: cannot open file \"%s\"\n", |
| 176 |
> |
progname, outfile); |
| 177 |
|
exit(1); |
| 178 |
|
} |
| 179 |
|
scanorig = ftell(fp); /* record position of first scanline */ |
| 180 |
|
if (fclose(fp) == -1) /* done with stream i/o */ |
| 181 |
|
goto filerr; |
| 182 |
+ |
sync(); /* flush NFS buffers */ |
| 183 |
|
/* start rpict process */ |
| 184 |
|
if (open_process(rpd, rpargv) <= 0) { |
| 185 |
|
fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]); |
| 191 |
|
progname, rpargv[0]); |
| 192 |
|
exit(1); |
| 193 |
|
} |
| 194 |
< |
if ((scanline = (COLR *)malloc(hres*sizeof(COLR))) == NULL) { |
| 194 |
> |
if ((pbuf = (COLR *)malloc(hres*vres*sizeof(COLR))) == NULL) { |
| 195 |
|
fprintf(stderr, "%s: out of memory\n", progname); |
| 196 |
|
exit(1); |
| 197 |
|
} |
| 198 |
+ |
signal(SIGALRM, onalrm); |
| 199 |
|
return; |
| 200 |
|
filerr: |
| 201 |
< |
fprintf(stderr, "%s: file i/o error\n", outfile); |
| 201 |
> |
fprintf(stderr, "%s: i/o error on file \"%s\"\n", progname, outfile); |
| 202 |
|
exit(1); |
| 203 |
|
} |
| 204 |
|
|
| 205 |
|
|
| 206 |
|
int |
| 207 |
+ |
nextpiece(xp, yp) /* get next piece assignment */ |
| 208 |
+ |
int *xp, *yp; |
| 209 |
+ |
{ |
| 210 |
+ |
extern char *fgets(); |
| 211 |
+ |
struct flock fls; |
| 212 |
+ |
char buf[64]; |
| 213 |
+ |
|
| 214 |
+ |
if (gotalrm) /* someone wants us to quit */ |
| 215 |
+ |
return(0); |
| 216 |
+ |
if (syncfd != -1) { /* use sync file */ |
| 217 |
+ |
fls.l_type = F_WRLCK; /* gain exclusive access */ |
| 218 |
+ |
fls.l_whence = 0; |
| 219 |
+ |
fls.l_start = 0L; |
| 220 |
+ |
fls.l_len = 0L; |
| 221 |
+ |
fcntl(syncfd, F_SETLKW, &fls); |
| 222 |
+ |
lseek(syncfd, 0L, 0); |
| 223 |
+ |
buf[read(syncfd, buf, sizeof(buf)-1)] = '\0'; |
| 224 |
+ |
if (sscanf(buf, "%*d %*d %d %d", xp, yp) < 2) { |
| 225 |
+ |
*xp = hmult-1; |
| 226 |
+ |
*yp = vmult; |
| 227 |
+ |
} |
| 228 |
+ |
if (--(*yp) < 0) { /* decrement position */ |
| 229 |
+ |
*yp = vmult-1; |
| 230 |
+ |
if (--(*xp) < 0) { /* all done! */ |
| 231 |
+ |
close(syncfd); |
| 232 |
+ |
return(0); |
| 233 |
+ |
} |
| 234 |
+ |
} |
| 235 |
+ |
sprintf(buf, "%d %d\n%d %d\n", hmult, vmult, *xp, *yp); |
| 236 |
+ |
lseek(syncfd, 0L, 0); /* write new position */ |
| 237 |
+ |
write(syncfd, buf, strlen(buf)); |
| 238 |
+ |
fls.l_type = F_UNLCK; /* release sync file */ |
| 239 |
+ |
fcntl(syncfd, F_SETLKW, &fls); |
| 240 |
+ |
return(1); |
| 241 |
+ |
} |
| 242 |
+ |
if (fgets(buf, sizeof(buf), stdin) == NULL) /* use stdin */ |
| 243 |
+ |
return(0); |
| 244 |
+ |
if (sscanf(buf, "%d %d", xp, yp) == 2) |
| 245 |
+ |
return(1); |
| 246 |
+ |
fprintf(stderr, "%s: input format error\n", progname); |
| 247 |
+ |
exit(1); |
| 248 |
+ |
} |
| 249 |
+ |
|
| 250 |
+ |
|
| 251 |
+ |
int |
| 252 |
|
cleanup() /* close rpict process and clean up */ |
| 253 |
|
{ |
| 254 |
< |
register int rpstat; |
| 254 |
> |
register int pid; |
| 255 |
> |
int status, rstat = 0; |
| 256 |
|
|
| 257 |
< |
free((char *)scanline); |
| 257 |
> |
free((char *)pbuf); |
| 258 |
|
fclose(torp); |
| 259 |
|
fclose(fromrp); |
| 260 |
< |
rpstat = close_process(rpd); |
| 261 |
< |
if (rpstat == -1) |
| 262 |
< |
rpstat = 0; |
| 263 |
< |
return(rpstat); |
| 260 |
> |
while ((pid = wait(&status)) != -1) |
| 261 |
> |
if (rstat == 0) |
| 262 |
> |
rstat = status>>8 & 0xff; |
| 263 |
> |
return(rstat); |
| 264 |
|
} |
| 265 |
|
|
| 266 |
|
|
| 267 |
|
rpiece() /* render picture piece by piece */ |
| 268 |
|
{ |
| 196 |
– |
extern char *gets(), *strcat(); |
| 269 |
|
VIEW pview; |
| 198 |
– |
char buf[48]; |
| 270 |
|
int xorg, yorg; |
| 271 |
|
|
| 272 |
< |
while (gets(buf) != NULL) { |
| 202 |
< |
if (sscanf(buf, "%d %d", &xorg, &yorg) != 2) { |
| 203 |
< |
fprintf(stderr, "%s: input format error\n", progname); |
| 204 |
< |
exit(1); |
| 205 |
< |
} |
| 272 |
> |
while (nextpiece(&xorg, &yorg)) { |
| 273 |
|
copystruct(&pview, &ourview); /* compute view for piece */ |
| 274 |
|
switch (ourview.type) { |
| 275 |
|
case VT_PER: |
| 302 |
|
fflush(torp); /* assign piece to rpict */ |
| 303 |
|
putpiece(xorg, yorg); /* place piece in output */ |
| 304 |
|
if (verbose) { /* notify caller */ |
| 305 |
< |
strcat(buf, " done\n"); |
| 306 |
< |
writebuf(fileno(stdout), buf, strlen(buf)); |
| 305 |
> |
printf("%d %d done\n", xorg, yorg); |
| 306 |
> |
fflush(stdout); |
| 307 |
|
} |
| 308 |
|
} |
| 309 |
|
} |
| 310 |
|
|
| 311 |
|
|
| 312 |
+ |
int |
| 313 |
|
putpiece(xpos, ypos) /* get next piece from rpict */ |
| 314 |
|
int xpos, ypos; |
| 315 |
|
{ |
| 316 |
+ |
struct flock fls; |
| 317 |
+ |
int pid, status; |
| 318 |
|
int hr, vr; |
| 319 |
< |
int y; |
| 320 |
< |
|
| 321 |
< |
getheader(fromrp, NULL); /* discard header info. */ |
| 322 |
< |
if (fscnresolu(&hr, &vr, fromrp) < 0 || /* check resolution */ |
| 323 |
< |
hr != hres || vr != vres) { |
| 319 |
> |
register int y; |
| 320 |
> |
/* check bounds */ |
| 321 |
> |
if (xpos < 0 | ypos < 0 | xpos >= hmult | ypos >= vmult) { |
| 322 |
> |
fprintf(stderr, "%s: requested piece (%d,%d) out of range\n", |
| 323 |
> |
progname, xpos, ypos); |
| 324 |
> |
exit(1); |
| 325 |
> |
} |
| 326 |
> |
/* check header from rpict */ |
| 327 |
> |
getheader(fromrp, NULL); |
| 328 |
> |
if (fscnresolu(&hr, &vr, fromrp) < 0 || hr != hres || vr != vres) { |
| 329 |
|
fprintf(stderr, "%s: resolution mismatch from %s\n", |
| 330 |
|
progname, rpargv[0]); |
| 331 |
|
exit(1); |
| 332 |
|
} |
| 333 |
< |
for (y = 0; y < vr; y++) { /* transfer scanlines */ |
| 334 |
< |
if (freadcolrs(scanline, hr, fromrp) < 0) { |
| 333 |
> |
/* load new piece into buffer */ |
| 334 |
> |
for (y = 0; y < vr; y++) |
| 335 |
> |
if (freadcolrs(pbuf+y*hr, hr, fromrp) < 0) { |
| 336 |
|
fprintf(stderr, "%s: read error from %s\n", |
| 337 |
|
progname, rpargv[0]); |
| 338 |
|
exit(1); |
| 339 |
|
} |
| 340 |
< |
if ((y == 0 || hmult != 1) && lseek(outfd, |
| 341 |
< |
scanorig+((long)((vmult-1-ypos)*vres+y)*hmult*hres+xpos*hres)*sizeof(COLR), |
| 342 |
< |
0) == -1) { |
| 343 |
< |
fprintf(stderr, "%s: seek error\n", outfile); |
| 344 |
< |
exit(1); |
| 340 |
> |
#if MAXFORK |
| 341 |
> |
/* fork so we don't slow rpict down */ |
| 342 |
> |
if ((pid = fork()) > 0) { |
| 343 |
> |
if (++nforked > MAXFORK) { |
| 344 |
> |
wait(&status); /* reap a child */ |
| 345 |
> |
if (status) |
| 346 |
> |
exit(status>>8 & 0xff); |
| 347 |
> |
nforked--; |
| 348 |
|
} |
| 349 |
< |
if (writebuf(outfd, (char *)scanline, hr*sizeof(COLR)) != |
| 350 |
< |
hr*sizeof(COLR)) { |
| 351 |
< |
fprintf(stderr, "%s: write error\n", outfile); |
| 352 |
< |
exit(1); |
| 349 |
> |
return(pid); |
| 350 |
> |
} |
| 351 |
> |
#else |
| 352 |
> |
pid = -1; /* no forking */ |
| 353 |
> |
#endif |
| 354 |
> |
/* lock file section so NFS doesn't mess up */ |
| 355 |
> |
fls.l_whence = 0; |
| 356 |
> |
fls.l_len = (long)vres*hmult*hres*sizeof(COLR); |
| 357 |
> |
fls.l_start = scanorig + (vmult-1-ypos)*fls.l_len; |
| 358 |
> |
fls.l_type = F_WRLCK; |
| 359 |
> |
fcntl(outfd, F_SETLKW, &fls); |
| 360 |
> |
/* write new piece to file */ |
| 361 |
> |
if (lseek(outfd, fls.l_start+(long)xpos*hres*sizeof(COLR), 0) == -1) |
| 362 |
> |
goto seekerr; |
| 363 |
> |
if (hmult == 1) { |
| 364 |
> |
if (writebuf(outfd, (char *)pbuf, |
| 365 |
> |
vr*hr*sizeof(COLR)) != vr*hr*sizeof(COLR)) |
| 366 |
> |
goto writerr; |
| 367 |
> |
} else |
| 368 |
> |
for (y = 0; y < vr; y++) { |
| 369 |
> |
if (writebuf(outfd, (char *)(pbuf+y*hr), |
| 370 |
> |
hr*sizeof(COLR)) != hr*sizeof(COLR)) |
| 371 |
> |
goto writerr; |
| 372 |
> |
if (y < vr-1 && lseek(outfd, |
| 373 |
> |
(long)(hmult-1)*hr*sizeof(COLR), |
| 374 |
> |
1) == -1) |
| 375 |
> |
goto seekerr; |
| 376 |
|
} |
| 377 |
+ |
if (pid == -1) { /* fork failed */ |
| 378 |
+ |
fls.l_type = F_UNLCK; /* release lock */ |
| 379 |
+ |
fcntl(outfd, F_SETLKW, &fls); |
| 380 |
+ |
return(0); |
| 381 |
|
} |
| 382 |
+ |
_exit(0); /* else exit child process (releasing lock) */ |
| 383 |
+ |
seekerr: |
| 384 |
+ |
fprintf(stderr, "%s: seek error on file \"%s\"\n", progname, outfile); |
| 385 |
+ |
_exit(1); |
| 386 |
+ |
writerr: |
| 387 |
+ |
fprintf(stderr, "%s: write error on file \"%s\"\n", progname, outfile); |
| 388 |
+ |
_exit(1); |
| 389 |
|
} |