1 |
< |
/* Copyright (c) 1992 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1993 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
9 |
|
*/ |
10 |
|
|
11 |
|
#include "standard.h" |
12 |
< |
#include <fcntl.h> |
12 |
> |
|
13 |
> |
#ifndef F_SETLKW |
14 |
> |
|
15 |
> |
main(argc, argv) |
16 |
> |
int argc; |
17 |
> |
char *argv[]; |
18 |
> |
{ |
19 |
> |
fprintf(stderr, "%s: no NFS lock manager on this machine\n", argv[0]); |
20 |
> |
exit(1); |
21 |
> |
} |
22 |
> |
|
23 |
> |
#else |
24 |
> |
|
25 |
|
#include <signal.h> |
26 |
|
#include "color.h" |
27 |
|
#include "view.h" |
38 |
|
#define MAXFORK 0 |
39 |
|
#endif |
40 |
|
#endif |
41 |
< |
|
41 |
> |
/* protection from SYSV signals(!) */ |
42 |
> |
#if defined(sgi) || defined(hpux) |
43 |
> |
#define guard_io() sighold(SIGALRM) |
44 |
> |
#define unguard() sigrelse(SIGALRM) |
45 |
> |
#endif |
46 |
> |
#ifndef guard_io |
47 |
> |
#define guard_io() 0 |
48 |
> |
#define unguard() 0 |
49 |
> |
#endif |
50 |
|
/* rpict command */ |
51 |
< |
char *rpargv[128] = {"rpict", "-S", "1", "-x", "512", "-y", "512", "-pa", "1"}; |
52 |
< |
int rpargc = 9; |
51 |
> |
char *rpargv[128] = {"rpict", "-S", "1"}; |
52 |
> |
int rpargc = 3; |
53 |
|
int rpd[3]; |
54 |
|
FILE *torp, *fromrp; |
55 |
|
COLR *pbuf; |
56 |
|
/* our view parameters */ |
57 |
|
VIEW ourview = STDVIEW; |
58 |
|
double pixaspect = 1.0; |
59 |
< |
int hres = 512, vres = 512, hmult = 2, vmult = 2; |
59 |
> |
int hres = 1024, vres = 1024, hmult = 4, vmult = 4; |
60 |
|
/* output file */ |
61 |
|
char *outfile = NULL; |
62 |
|
int outfd; |
63 |
|
long scanorig; |
64 |
< |
int syncfd = -1; /* lock file descriptor */ |
64 |
> |
FILE *syncfp = NULL; /* synchronization file pointer */ |
65 |
> |
int synclst = F_UNLCK; /* synchronization file lock status */ |
66 |
|
int nforked = 0; |
67 |
|
|
68 |
+ |
#define sflock(t) if ((t)!=synclst) dolock(fileno(syncfp),synclst=t) |
69 |
+ |
|
70 |
|
char *progname; |
71 |
|
int verbose = 0; |
72 |
+ |
int rvrlim = -1; |
73 |
|
|
74 |
|
extern long lseek(), ftell(); |
75 |
|
|
85 |
|
|
86 |
|
progname = argv[0]; |
87 |
|
for (i = 1; i < argc; i++) { |
88 |
+ |
/* expand arguments */ |
89 |
+ |
while (rval = expandarg(&argc, &argv, i)) |
90 |
+ |
if (rval < 0) { |
91 |
+ |
fprintf(stderr, "%s: cannot expand '%s'", |
92 |
+ |
argv[0], argv[i]); |
93 |
+ |
exit(1); |
94 |
+ |
} |
95 |
|
if (argv[i][0] == '-') |
96 |
|
switch (argv[i][1]) { |
97 |
|
case 'v': |
116 |
|
} |
117 |
|
break; |
118 |
|
case 'p': /* pixel aspect ratio? */ |
119 |
< |
if (argv[i][2] == 'a' && !argv[i][3]) |
120 |
< |
pixaspect = atof(argv[i+1]); |
121 |
< |
break; |
122 |
< |
case 'x': /* piece x resolution */ |
123 |
< |
if (!argv[i][2]) |
124 |
< |
hres = atoi(argv[i+1]); |
125 |
< |
break; |
126 |
< |
case 'y': /* piece y resolution */ |
127 |
< |
if (!argv[i][2]) |
128 |
< |
vres = atoi(argv[i+1]); |
129 |
< |
break; |
119 |
> |
if (argv[i][2] != 'a' || argv[i][3]) |
120 |
> |
break; |
121 |
> |
pixaspect = atof(argv[i+1]); |
122 |
> |
continue; |
123 |
> |
case 'x': /* overall x resolution */ |
124 |
> |
if (argv[i][2]) |
125 |
> |
break; |
126 |
> |
hres = atoi(argv[++i]); |
127 |
> |
continue; |
128 |
> |
case 'y': /* overall y resolution */ |
129 |
> |
if (argv[i][2]) |
130 |
> |
break; |
131 |
> |
vres = atoi(argv[++i]); |
132 |
> |
continue; |
133 |
|
case 'X': /* horizontal multiplier */ |
134 |
|
if (argv[i][2]) |
135 |
|
break; |
140 |
|
break; |
141 |
|
vmult = atoi(argv[++i]); |
142 |
|
continue; |
143 |
+ |
case 'R': /* recover */ |
144 |
+ |
if (argv[i][2]) |
145 |
+ |
break; |
146 |
+ |
rvrlim = 0; |
147 |
+ |
/* fall through */ |
148 |
|
case 'F': /* syncronization file */ |
149 |
|
if (argv[i][2]) |
150 |
|
break; |
151 |
< |
if ((syncfd = open(argv[++i], O_RDWR)) < 0) { |
151 |
> |
if ((syncfp = fopen(argv[++i],"r+")) == NULL) { |
152 |
|
fprintf(stderr, "%s: cannot open\n", |
153 |
|
argv[i]); |
154 |
|
exit(1); |
155 |
|
} |
156 |
|
continue; |
157 |
+ |
case 'z': /* z-file ist verbotten */ |
158 |
+ |
fprintf(stderr, "%s: -z option not allowed\n", |
159 |
+ |
argv[0]); |
160 |
+ |
exit(1); |
161 |
|
case 'o': /* output file */ |
162 |
|
if (argv[i][2]) |
163 |
|
break; |
164 |
|
outfile = argv[++i]; |
165 |
|
continue; |
166 |
< |
} |
166 |
> |
} else if (i >= argc-1) |
167 |
> |
break; |
168 |
|
rpargv[rpargc++] = argv[i]; |
169 |
|
} |
170 |
< |
rpargv[rpargc] = NULL; |
170 |
> |
if (i >= argc) { |
171 |
> |
fprintf(stderr, "%s: missing octree argument\n", argv[0]); |
172 |
> |
exit(1); |
173 |
> |
} |
174 |
|
if (outfile == NULL) { |
175 |
|
fprintf(stderr, "%s: missing output file\n", argv[0]); |
176 |
|
exit(1); |
177 |
|
} |
178 |
|
init(argc, argv); |
179 |
|
rpiece(); |
180 |
< |
rval = cleanup(0); |
134 |
< |
exit(rval); |
180 |
> |
exit(cleanup(0)); |
181 |
|
} |
182 |
|
|
183 |
|
|
184 |
+ |
dolock(fd, ltyp) /* lock or unlock a file */ |
185 |
+ |
int fd; |
186 |
+ |
int ltyp; |
187 |
+ |
{ |
188 |
+ |
static struct flock fls; /* static so initialized to zeroes */ |
189 |
+ |
|
190 |
+ |
fls.l_type = ltyp; |
191 |
+ |
if (fcntl(fd, F_SETLKW, &fls) < 0) { |
192 |
+ |
fprintf(stderr, "%s: cannot lock/unlock file\n", progname); |
193 |
+ |
exit(1); |
194 |
+ |
} |
195 |
+ |
} |
196 |
+ |
|
197 |
+ |
|
198 |
|
init(ac, av) /* set up output file and start rpict */ |
199 |
|
int ac; |
200 |
|
char **av; |
201 |
|
{ |
202 |
+ |
static char hrbuf[16], vrbuf[16]; |
203 |
|
extern char VersionID[]; |
204 |
|
char *err; |
205 |
|
FILE *fp; |
209 |
|
fprintf(stderr, "%s: %s\n", progname, err); |
210 |
|
exit(1); |
211 |
|
} |
212 |
< |
if (syncfd != -1) { |
213 |
< |
char buf[32]; |
214 |
< |
buf[read(syncfd, buf, sizeof(buf)-1)] = '\0'; |
215 |
< |
sscanf(buf, "%d %d", &hmult, &vmult); |
212 |
> |
if (syncfp != NULL) { |
213 |
> |
sflock(F_RDLCK); |
214 |
> |
fscanf(syncfp, "%d %d", &hmult, &vmult); |
215 |
> |
sflock(F_UNLCK); |
216 |
|
} |
217 |
+ |
/* compute piece size */ |
218 |
+ |
hres /= hmult; |
219 |
+ |
vres /= vmult; |
220 |
|
normaspect(viewaspect(&ourview)*hmult/vmult, &pixaspect, &hres, &vres); |
221 |
+ |
sprintf(hrbuf, "%d", hres); |
222 |
+ |
rpargv[rpargc++] = "-x"; rpargv[rpargc++] = hrbuf; |
223 |
+ |
sprintf(vrbuf, "%d", vres); |
224 |
+ |
rpargv[rpargc++] = "-y"; rpargv[rpargc++] = vrbuf; |
225 |
+ |
rpargv[rpargc++] = "-pa"; rpargv[rpargc++] = "0"; |
226 |
+ |
rpargv[rpargc++] = av[ac-1]; |
227 |
+ |
rpargv[rpargc] = NULL; |
228 |
|
/* open output file */ |
229 |
|
if ((outfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) >= 0) { |
230 |
+ |
dolock(outfd, F_WRLCK); |
231 |
|
if ((fp = fdopen(dup(outfd), "w")) == NULL) |
232 |
|
goto filerr; |
233 |
|
printargs(ac, av, fp); /* write header */ |
241 |
|
putc('\n', fp); |
242 |
|
fprtresolu(hres*hmult, vres*vmult, fp); |
243 |
|
} else if ((outfd = open(outfile, O_RDWR)) >= 0) { |
244 |
+ |
dolock(outfd, F_RDLCK); |
245 |
|
if ((fp = fdopen(dup(outfd), "r+")) == NULL) |
246 |
|
goto filerr; |
247 |
< |
getheader(fp, NULL); /* skip header */ |
248 |
< |
if (fscnresolu(&hr, &vr, fp) < 0 || /* check resolution */ |
247 |
> |
getheader(fp, NULL, NULL); /* skip header */ |
248 |
> |
if (!fscnresolu(&hr, &vr, fp) || /* check resolution */ |
249 |
|
hr != hres*hmult || vr != vres*vmult) { |
250 |
|
fprintf(stderr, "%s: resolution mismatch on file \"%s\"\n", |
251 |
|
progname, outfile); |
259 |
|
scanorig = ftell(fp); /* record position of first scanline */ |
260 |
|
if (fclose(fp) == -1) /* done with stream i/o */ |
261 |
|
goto filerr; |
262 |
< |
#if NFS |
190 |
< |
sync(); /* flush NFS buffers */ |
191 |
< |
#endif |
262 |
> |
dolock(outfd, F_UNLCK); |
263 |
|
/* start rpict process */ |
264 |
|
if (open_process(rpd, rpargv) <= 0) { |
265 |
|
fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]); |
271 |
|
progname, rpargv[0]); |
272 |
|
exit(1); |
273 |
|
} |
274 |
< |
if ((pbuf = (COLR *)malloc(hres*vres*sizeof(COLR))) == NULL) { |
274 |
> |
if ((pbuf = (COLR *)bmalloc(hres*vres*sizeof(COLR))) == NULL) { |
275 |
|
fprintf(stderr, "%s: out of memory\n", progname); |
276 |
|
exit(1); |
277 |
|
} |
287 |
|
nextpiece(xp, yp) /* get next piece assignment */ |
288 |
|
int *xp, *yp; |
289 |
|
{ |
219 |
– |
extern char *fgets(); |
220 |
– |
struct flock fls; |
221 |
– |
char buf[64]; |
222 |
– |
|
290 |
|
if (gotalrm) /* someone wants us to quit */ |
291 |
|
return(0); |
292 |
< |
if (syncfd != -1) { /* use sync file */ |
293 |
< |
fls.l_type = F_WRLCK; /* gain exclusive access */ |
294 |
< |
fls.l_whence = 0; |
295 |
< |
fls.l_start = 0L; |
296 |
< |
fls.l_len = 0L; |
297 |
< |
fcntl(syncfd, F_SETLKW, &fls); |
298 |
< |
lseek(syncfd, 0L, 0); |
299 |
< |
buf[read(syncfd, buf, sizeof(buf)-1)] = '\0'; |
300 |
< |
if (sscanf(buf, "%*d %*d %d %d", xp, yp) < 2) { |
292 |
> |
if (syncfp != NULL) { /* use sync file */ |
293 |
> |
/* |
294 |
> |
* So we don't necessarily have to lock and unlock the file |
295 |
> |
* multiple times (very slow), we establish an exclusive |
296 |
> |
* lock at the beginning on our synchronization file and |
297 |
> |
* maintain it in the subroutine rvrpiece(). |
298 |
> |
*/ |
299 |
> |
sflock(F_WRLCK); |
300 |
> |
fseek(syncfp, 0L, 0); /* read position */ |
301 |
> |
if (fscanf(syncfp, "%*d %*d %d %d", xp, yp) < 2) { |
302 |
|
*xp = hmult-1; |
303 |
|
*yp = vmult; |
304 |
|
} |
305 |
+ |
if (rvrlim == 0) /* initialize recovery limit */ |
306 |
+ |
rvrlim = *xp*vmult + *yp; |
307 |
+ |
if (rvrpiece(xp, yp)) { /* do stragglers first */ |
308 |
+ |
sflock(F_UNLCK); |
309 |
+ |
return(1); |
310 |
+ |
} |
311 |
|
if (--(*yp) < 0) { /* decrement position */ |
312 |
|
*yp = vmult-1; |
313 |
< |
if (--(*xp) < 0) { /* all done! */ |
314 |
< |
close(syncfd); |
313 |
> |
if (--(*xp) < 0) { /* all done */ |
314 |
> |
sflock(F_UNLCK); |
315 |
|
return(0); |
316 |
|
} |
317 |
|
} |
318 |
< |
sprintf(buf, "%d %d\n%d %d\n", hmult, vmult, *xp, *yp); |
319 |
< |
lseek(syncfd, 0L, 0); /* write new position */ |
320 |
< |
write(syncfd, buf, strlen(buf)); |
321 |
< |
fls.l_type = F_UNLCK; /* release sync file */ |
248 |
< |
fcntl(syncfd, F_SETLKW, &fls); |
318 |
> |
fseek(syncfp, 0L, 0); /* write new position */ |
319 |
> |
fprintf(syncfp, "%4d %4d\n%4d %4d\n\n", hmult, vmult, *xp, *yp); |
320 |
> |
fflush(syncfp); |
321 |
> |
sflock(F_UNLCK); /* release sync file */ |
322 |
|
return(1); |
323 |
|
} |
324 |
< |
if (fgets(buf, sizeof(buf), stdin) == NULL) /* use stdin */ |
252 |
< |
return(0); |
253 |
< |
if (sscanf(buf, "%d %d", xp, yp) == 2) |
324 |
> |
if (scanf("%d %d", xp, yp) == 2) /* use stdin */ |
325 |
|
return(1); |
326 |
|
fprintf(stderr, "%s: input format error\n", progname); |
327 |
|
exit(cleanup(1)); |
329 |
|
|
330 |
|
|
331 |
|
int |
332 |
+ |
rvrpiece(xp, yp) /* check for recoverable pieces */ |
333 |
+ |
register int *xp, *yp; |
334 |
+ |
{ |
335 |
+ |
static char *pdone = NULL; /* which pieces are done */ |
336 |
+ |
static long readpos = -1; /* how far we've read */ |
337 |
+ |
register int i; |
338 |
+ |
/* |
339 |
+ |
* This routine is called by nextpiece() with an |
340 |
+ |
* exclusive lock on syncfp and the file pointer at the |
341 |
+ |
* appropriate position to read in the finished pieces. |
342 |
+ |
*/ |
343 |
+ |
if (rvrlim < 0) |
344 |
+ |
return(0); /* only check if asked */ |
345 |
+ |
if (pdone == NULL) /* first call */ |
346 |
+ |
pdone = calloc(hmult*vmult, sizeof(char)); |
347 |
+ |
if (readpos != -1) /* mark what's been done */ |
348 |
+ |
fseek(syncfp, readpos, 0); |
349 |
+ |
while (fscanf(syncfp, "%d %d", xp, yp) == 2) |
350 |
+ |
pdone[*xp*vmult+*yp] = 1; |
351 |
+ |
if (!feof(syncfp)) { |
352 |
+ |
fprintf(stderr, "%s: format error in sync file\n", progname); |
353 |
+ |
exit(1); |
354 |
+ |
} |
355 |
+ |
readpos = ftell(syncfp); |
356 |
+ |
i = hmult*vmult; /* find an unaccounted for piece */ |
357 |
+ |
while (i-- > rvrlim) |
358 |
+ |
if (!pdone[i]) { |
359 |
+ |
*xp = i / vmult; |
360 |
+ |
*yp = i % vmult; |
361 |
+ |
pdone[i] = 1; /* consider it done */ |
362 |
+ |
return(1); |
363 |
+ |
} |
364 |
+ |
rvrlim = -1; /* nothing left to recover */ |
365 |
+ |
free(pdone); |
366 |
+ |
pdone = NULL; |
367 |
+ |
return(0); |
368 |
+ |
} |
369 |
+ |
|
370 |
+ |
|
371 |
+ |
int |
372 |
|
cleanup(rstat) /* close rpict process and clean up */ |
373 |
|
int rstat; |
374 |
|
{ |
375 |
|
int status; |
376 |
|
|
377 |
< |
free((char *)pbuf); |
377 |
> |
bfree((char *)pbuf, hres*vres*sizeof(COLR)); |
378 |
|
fclose(torp); |
379 |
|
fclose(fromrp); |
380 |
|
while (wait(&status) != -1) |
422 |
|
putc('\n', torp); |
423 |
|
fflush(torp); /* assigns piece to rpict */ |
424 |
|
putpiece(xorg, yorg); /* place piece in output */ |
314 |
– |
if (verbose) { /* notify caller */ |
315 |
– |
printf("%d %d done\n", xorg, yorg); |
316 |
– |
fflush(stdout); |
317 |
– |
} |
425 |
|
} |
426 |
|
} |
427 |
|
|
441 |
|
exit(cleanup(1)); |
442 |
|
} |
443 |
|
/* check header from rpict */ |
444 |
< |
getheader(fromrp, NULL); |
445 |
< |
if (fscnresolu(&hr, &vr, fromrp) < 0 || hr != hres | vr != vres) { |
444 |
> |
guard_io(); |
445 |
> |
getheader(fromrp, NULL, NULL); |
446 |
> |
if (!fscnresolu(&hr, &vr, fromrp) || hr != hres | vr != vres) { |
447 |
|
fprintf(stderr, "%s: resolution mismatch from %s\n", |
448 |
|
progname, rpargv[0]); |
449 |
|
exit(cleanup(1)); |
450 |
|
} |
451 |
+ |
if (verbose) { /* notify caller */ |
452 |
+ |
printf("%d %d begun\n", xpos, ypos); |
453 |
+ |
fflush(stdout); |
454 |
+ |
} |
455 |
+ |
unguard(); |
456 |
|
/* load new piece into buffer */ |
457 |
< |
for (y = 0; y < vr; y++) |
457 |
> |
for (y = 0; y < vr; y++) { |
458 |
> |
guard_io(); |
459 |
|
if (freadcolrs(pbuf+y*hr, hr, fromrp) < 0) { |
460 |
|
fprintf(stderr, "%s: read error from %s\n", |
461 |
|
progname, rpargv[0]); |
462 |
|
exit(cleanup(1)); |
463 |
|
} |
464 |
+ |
unguard(); |
465 |
+ |
} |
466 |
|
#if MAXFORK |
467 |
|
/* fork so we don't slow rpict down */ |
468 |
|
if ((pid = fork()) > 0) { |
477 |
|
#else |
478 |
|
pid = -1; /* no forking */ |
479 |
|
#endif |
480 |
+ |
fls.l_start = scanorig + |
481 |
+ |
((long)(vmult-1-ypos)*vres*hmult+xpos)*hres*sizeof(COLR); |
482 |
|
#if NFS |
483 |
+ |
fls.l_len = ((long)(vres-1)*hmult+1)*hres*sizeof(COLR); |
484 |
|
/* lock file section so NFS doesn't mess up */ |
485 |
|
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; |
486 |
|
fls.l_type = F_WRLCK; |
487 |
|
fcntl(outfd, F_SETLKW, &fls); |
488 |
|
#endif |
489 |
|
/* write new piece to file */ |
490 |
< |
if (lseek(outfd, fls.l_start+(long)xpos*hres*sizeof(COLR), 0) == -1) |
490 |
> |
if (lseek(outfd, fls.l_start, 0) == -1) |
491 |
|
goto seekerr; |
492 |
|
if (hmult == 1) { |
493 |
|
if (writebuf(outfd, (char *)pbuf, |
503 |
|
1) == -1) |
504 |
|
goto seekerr; |
505 |
|
} |
389 |
– |
if (pid == -1) { /* fork failed */ |
506 |
|
#if NFS |
507 |
< |
fls.l_type = F_UNLCK; /* release lock */ |
508 |
< |
fcntl(outfd, F_SETLKW, &fls); |
507 |
> |
fls.l_type = F_UNLCK; /* release lock */ |
508 |
> |
fcntl(outfd, F_SETLKW, &fls); |
509 |
|
#endif |
510 |
< |
return(0); |
510 |
> |
if (syncfp != NULL) { /* record what's been done */ |
511 |
> |
sflock(F_WRLCK); |
512 |
> |
fseek(syncfp, 0L, 2); /* append index */ |
513 |
> |
fprintf(syncfp, "%4d %4d\n", xpos, ypos); |
514 |
> |
fflush(syncfp); |
515 |
> |
/*** Unlock not necessary, since |
516 |
> |
sflock(F_UNLCK); _exit() or nextpiece() is next ***/ |
517 |
|
} |
518 |
< |
_exit(0); /* else exit child process (releasing lock) */ |
518 |
> |
if (verbose) { /* notify caller */ |
519 |
> |
printf("%d %d done\n", xpos, ypos); |
520 |
> |
fflush(stdout); |
521 |
> |
} |
522 |
> |
if (pid == -1) /* didn't fork or fork failed */ |
523 |
> |
return(0); |
524 |
> |
_exit(0); /* else exit child process (releasing locks) */ |
525 |
|
seekerr: |
526 |
|
fprintf(stderr, "%s: seek error on file \"%s\"\n", progname, outfile); |
527 |
|
_exit(1); |
529 |
|
fprintf(stderr, "%s: write error on file \"%s\"\n", progname, outfile); |
530 |
|
_exit(1); |
531 |
|
} |
532 |
+ |
|
533 |
+ |
#endif |