ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rpiece.c
(Generate patch)

Comparing ray/src/util/rpiece.c (file contents):
Revision 2.6 by greg, Fri Aug 7 11:34:27 1992 UTC vs.
Revision 2.65 by greg, Wed Aug 21 16:18:54 2024 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1992 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Generate sections of a picture.
6   */
7  
8 < #include "standard.h"
9 < #include <fcntl.h>
8 >
9 > #include <stdio.h>
10   #include <signal.h>
11 + #include <sys/types.h>
12 +
13 + #include "platform.h"
14 + #ifndef NON_POSIX /* XXX need abstraction for process management */
15 + #include <sys/wait.h>
16 + #endif
17 +
18 + #include "standard.h"
19   #include "color.h"
20   #include "view.h"
21 < #include "resolu.h"
21 > #include "rtprocess.h"
22  
23 + #ifndef F_SETLKW
24 +
25 + int
26 + main(
27 +        int argc,
28 +        char *argv[]
29 + )
30 + {
31 +        fprintf(stderr, "%s: no NFS lock manager on this machine\n", argv[0]);
32 +        exit(1);
33 + }
34 +
35 + #else
36 +
37 + #ifndef NFS
38 + #define  NFS                    1
39 + #endif
40 +
41 + #ifndef RHAS_FORK_EXEC
42 + #undef MAXFORK
43 + #define MAXFORK                 0
44 + #endif
45 + #ifndef MAXFORK
46 + #if NFS
47 + #define  MAXFORK                3       /* allotment of duped processes */
48 + #else
49 + #define  MAXFORK                0
50 + #endif
51 + #endif
52 +                                        /* protection from SYSV signals(!) */
53 + #if defined(sgi)
54 + #define guard_io()      sighold(SIGALRM)
55 + #define unguard()       sigrelse(SIGALRM)
56 + #endif
57 + #ifndef guard_io
58 + #define guard_io()      
59 + #define unguard()      
60 + #endif
61 +
62                                  /* rpict command */
63 < char  *rpargv[128] = {"rpict", "-S", "1", "-x", "512", "-y", "512", "-pa", "1"};
64 < int  rpargc = 9;
21 < int  rpd[3];
63 > char  *rpargv[128] = {"rpict", "-S", "1"};
64 > int  rpargc = 3;
65   FILE  *torp, *fromrp;
66 < COLR  *scanline;
66 > COLR  *pbuf;
67                                  /* our view parameters */
68   VIEW  ourview = STDVIEW;
69   double  pixaspect = 1.0;
70 < int  hres = 512, vres = 512, hmult = 2, vmult = 2;
70 > int  hres = 1024, vres = 1024, hmult = 4, vmult = 4;
71                                  /* output file */
72   char  *outfile = NULL;
73   int  outfd;
74   long  scanorig;
75 < int  syncfd = -1;               /* lock file descriptor */
75 > FILE  *syncfp = NULL;           /* synchronization file pointer */
76 > int  synclst = F_UNLCK;         /* synchronization file lock status */
77 > int  nforked = 0;
78  
79 + #define  sflock(t)      if ((t)!=synclst) dolock(fileno(syncfp),synclst=t)
80 +
81   char  *progname;
82   int  verbose = 0;
83 + int  nowarn = 0;
84 + unsigned  timelim = 0;
85 + int  rvrlim = -1;
86  
37 extern long  lseek(), ftell();
38
87   int  gotalrm = 0;
88 < int  onalrm() { gotalrm++; }
88 > void  onalrm(int i) { gotalrm++; }
89  
90 + static void dolock(int  fd, int  ltyp);
91 + static void init(int  ac, char  **av);
92 + static int nextpiece(int        *xp, int        *yp);
93 + static int rvrpiece(int *xp, int        *yp);
94 + static int cleanup(int  rstat);
95 + static void rpiece(void);
96 + static int putpiece(int xpos, int       ypos);
97 + static void filerr(char  *t);
98  
99 < main(argc, argv)
100 < int  argc;
101 < char  *argv[];
99 >
100 > int
101 > main(
102 >        int  argc,
103 >        char  *argv[]
104 > )
105   {
106 <        register int  i, rval;
106 >        int  i, rval;
107          
108          progname = argv[0];
109          for (i = 1; i < argc; i++) {
110 +                                                /* expand arguments */
111 +                while ((rval = expandarg(&argc, &argv, i)) > 0)
112 +                        ;
113 +                if (rval < 0) {
114 +                        fprintf(stderr, "%s: cannot expand '%s'\n",
115 +                                        argv[0], argv[i]);
116 +                        exit(1);
117 +                }
118                  if (argv[i][0] == '-')
119                          switch (argv[i][1]) {
120 +                        case 'w':
121 +                                if (!argv[i][2])
122 +                                        nowarn = !nowarn;
123 +                                else if (argv[i][2] == '+')
124 +                                        nowarn = 0;
125 +                                else if (argv[i][2] == '-')
126 +                                        nowarn = 1;
127 +                                break;
128                          case 'v':
129                                  switch (argv[i][2]) {
130                                  case '\0':      /* verbose option */
# Line 72 | Line 147 | char  *argv[];
147                                  }
148                                  break;
149                          case 'p':               /* pixel aspect ratio? */
150 <                                if (argv[i][2] == 'a' && !argv[i][3])
151 <                                        pixaspect = atof(argv[i+1]);
152 <                                break;
153 <                        case 'x':               /* piece x resolution */
154 <                                if (!argv[i][2])
155 <                                        hres = atoi(argv[i+1]);
156 <                                break;
157 <                        case 'y':               /* piece y resolution */
158 <                                if (!argv[i][2])
159 <                                        vres = atoi(argv[i+1]);
160 <                                break;
150 >                                if (argv[i][2] == 'm') {
151 >                                        fprintf(stderr, "%s: -pm unsupported\n",
152 >                                                argv[0]);
153 >                                        ++i;
154 >                                        continue;
155 >                                }
156 >                                if (argv[i][2] == 'X') {
157 >                                        fprintf(stderr, "%s: -pXYZ unsupported\n",
158 >                                                argv[0]);
159 >                                        ++i;
160 >                                        continue;
161 >                                }
162 >                                if (argv[i][2] == 'c') {
163 >                                        fprintf(stderr, "%s: -pc unsupported\n",
164 >                                                argv[0]);
165 >                                        i += 9;
166 >                                        continue;
167 >                                }
168 >                                if (argv[i][2] != 'a' || argv[i][3])
169 >                                        break;
170 >                                pixaspect = atof(argv[++i]);
171 >                                continue;
172 >                        case 'S':
173 >                                fprintf(stderr, "%s: -S unsupported\n", argv[0]);
174 >                                i++;
175 >                                continue;
176 >                        case 'T':               /* time limit (hours) */
177 >                                if (argv[i][2])
178 >                                        break;
179 >                                timelim = atof(argv[++i])*3600. + .5;
180 >                                continue;
181 >                        case 'x':               /* overall x resolution */
182 >                                if (argv[i][2])
183 >                                        break;
184 >                                hres = atoi(argv[++i]);
185 >                                continue;
186 >                        case 'y':               /* overall y resolution */
187 >                                if (argv[i][2])
188 >                                        break;
189 >                                vres = atoi(argv[++i]);
190 >                                continue;
191                          case 'X':               /* horizontal multiplier */
192                                  if (argv[i][2])
193                                          break;
# Line 93 | Line 198 | char  *argv[];
198                                          break;
199                                  vmult = atoi(argv[++i]);
200                                  continue;
201 +                        case 'R':               /* recover */
202 +                                if (argv[i][2])
203 +                                        break;
204 +                                rvrlim = 0;
205 +                        /* fall through */
206                          case 'F':               /* syncronization file */
207                                  if (argv[i][2])
208                                          break;
209 <                                if ((syncfd = open(argv[++i], O_RDWR)) < 0) {
209 >                                if ((syncfp =
210 >                fdopen(open(argv[++i],O_RDWR|O_CREAT,0666),"r+")) == NULL) {
211                                          fprintf(stderr, "%s: cannot open\n",
212                                                          argv[i]);
213                                          exit(1);
214                                  }
215                                  continue;
216 +                        case 'z':               /* z-file ist verbotten */
217 +                                fprintf(stderr, "%s: -z option not allowed\n",
218 +                                                argv[0]);
219 +                                exit(1);
220                          case 'o':               /* output file */
221                                  if (argv[i][2])
222                                          break;
223                                  outfile = argv[++i];
224                                  continue;
225                          }
226 +                else if (i >= argc-1)
227 +                        break;
228                  rpargv[rpargc++] = argv[i];
229          }
230 <        rpargv[rpargc] = NULL;
230 >        if (i >= argc) {
231 >                fprintf(stderr, "%s: missing octree argument\n", argv[0]);
232 >                exit(1);
233 >        }
234          if (outfile == NULL) {
235                  fprintf(stderr, "%s: missing output file\n", argv[0]);
236                  exit(1);
237          }
238          init(argc, argv);
239          rpiece();
240 <        rval = cleanup();
121 <        exit(rval);
240 >        exit(cleanup(0));
241   }
242  
243  
244 < init(ac, av)                    /* set up output file and start rpict */
245 < int  ac;
246 < char  **av;
244 > static void
245 > dolock(         /* lock or unlock a file */
246 >        int  fd,
247 >        int  ltyp
248 > )
249   {
250 +        static struct flock  fls;       /* static so initialized to zeroes */
251 +
252 +        fls.l_type = ltyp;
253 +        if (fcntl(fd, F_SETLKW, &fls) < 0) {
254 +                fprintf(stderr, "%s: cannot lock/unlock file: %s\n",
255 +                                progname, strerror(errno));
256 +                _exit(1);
257 +        }
258 + }
259 +
260 +
261 + static void
262 + init(                   /* set up output file and start rpict */
263 +        int  ac,
264 +        char  **av
265 + )
266 + {
267 +        static char  hrbuf[16], vrbuf[16];
268          extern char  VersionID[];
269          char  *err;
270          FILE  *fp;
271          int  hr, vr;
272 +        SUBPROC  rpd; /* since we don't close_process(), this can be local */
273                                          /* set up view */
274          if ((err = setview(&ourview)) != NULL) {
275                  fprintf(stderr, "%s: %s\n", progname, err);
276                  exit(1);
277          }
278 <        if (syncfd != -1) {
279 <                char  buf[32];
280 <                buf[read(syncfd, buf, sizeof(buf)-1)] = '\0';
281 <                sscanf(buf, "%d %d", &hmult, &vmult);
278 >        if (syncfp != NULL) {
279 >                sflock(F_RDLCK);
280 >                fscanf(syncfp, "%d %d", &hmult, &vmult);
281 >                sflock(F_UNLCK);
282          }
283 +                                        /* compute piece size */
284 +        hr = hres; vr = vres;
285 +        if (pixaspect > FTINY)
286 +                normaspect(viewaspect(&ourview), &pixaspect, &hr, &vr);
287 +        hres /= hmult;
288 +        vres /= vmult;
289 +        if (hres <= 0 || vres <= 0) {
290 +                fprintf(stderr, "%s: illegal resolution/subdivision\n", progname);
291 +                exit(1);
292 +        }
293          normaspect(viewaspect(&ourview)*hmult/vmult, &pixaspect, &hres, &vres);
294 +        if (!nowarn && (hr != hres*hmult) | (vr != vres*vmult))
295 +                fprintf(stderr,
296 +                "%s: warning - changed resolution from %dx%d to %dx%d\n",
297 +                                progname, hr, vr, hres*hmult, vres*vmult);
298 +        sprintf(hrbuf, "%d", hres);
299 +        rpargv[rpargc++] = "-x"; rpargv[rpargc++] = hrbuf;
300 +        sprintf(vrbuf, "%d", vres);
301 +        rpargv[rpargc++] = "-y"; rpargv[rpargc++] = vrbuf;
302 +        rpargv[rpargc++] = "-pa"; rpargv[rpargc++] = "0";
303 +        rpargv[rpargc++] = "-pm"; rpargv[rpargc++] = "0";
304 +        rpargv[rpargc++] = av[ac-1];
305 +        rpargv[rpargc] = NULL;
306                                          /* open output file */
307          if ((outfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) >= 0) {
308 +                dolock(outfd, F_WRLCK);
309                  if ((fp = fdopen(dup(outfd), "w")) == NULL)
310                          goto filerr;
311 <                printargs(ac, av, fp);          /* write header */
311 >                newheader("RADIANCE", fp);      /* create header */
312 >                printargs(ac, av, fp);
313                  fprintf(fp, "SOFTWARE= %s\n", VersionID);
314                  fputs(VIEWSTR, fp);
315                  fprintview(&ourview, fp);
316 <                putc('\n', fp);
316 >                fputc('\n', fp);
317 >                fputnow(fp);
318                  if (pixaspect < .99 || pixaspect > 1.01)
319                          fputaspect(pixaspect, fp);
320 +                fputprims(stdprims, fp);
321                  fputformat(COLRFMT, fp);
322 <                putc('\n', fp);
322 >                fputc('\n', fp);
323                  fprtresolu(hres*hmult, vres*vmult, fp);
324          } else if ((outfd = open(outfile, O_RDWR)) >= 0) {
325 +                dolock(outfd, F_RDLCK);
326                  if ((fp = fdopen(dup(outfd), "r+")) == NULL)
327                          goto filerr;
328 <                getheader(fp, NULL);            /* skip header */
329 <                if (fscnresolu(&hr, &vr, fp) < 0 ||     /* check resolution */
328 >                getheader(fp, NULL, NULL);      /* skip header */
329 >                if (!fscnresolu(&hr, &vr, fp) ||        /* check resolution */
330                                  hr != hres*hmult || vr != vres*vmult) {
331 <                        fprintf(stderr, "%s: picture resolution mismatch\n",
332 <                                        outfile);
331 >                        fprintf(stderr, "%s: resolution mismatch on file \"%s\"\n",
332 >                                        progname, outfile);
333                          exit(1);
334                  }
335          } else {
336 <                fprintf(stderr, "%s: cannot open\n", outfile);
336 >                fprintf(stderr, "%s: cannot open file \"%s\"\n",
337 >                                progname, outfile);
338                  exit(1);
339          }
340          scanorig = ftell(fp);           /* record position of first scanline */
341          if (fclose(fp) == -1)           /* done with stream i/o */
342                  goto filerr;
343 <        sync();                         /* avoid NFS buffering */
343 >        dolock(outfd, F_UNLCK);
344                                          /* start rpict process */
345 <        if (open_process(rpd, rpargv) <= 0) {
345 >        rpd = sp_inactive;
346 >        if (open_process(&rpd, rpargv) <= 0) {
347                  fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]);
348                  exit(1);
349          }
350 <        if ((fromrp = fdopen(rpd[0], "r")) == NULL ||
351 <                        (torp = fdopen(rpd[1], "w")) == NULL) {
350 >        if ((fromrp = fdopen(rpd.r, "r")) == NULL ||
351 >                        (torp = fdopen(rpd.w, "w")) == NULL) {
352                  fprintf(stderr, "%s: cannot open stream to %s\n",
353                                  progname, rpargv[0]);
354                  exit(1);
355          }
356 <        if ((scanline = (COLR *)malloc(hres*sizeof(COLR))) == NULL) {
356 >        if ((pbuf = (COLR *)bmalloc(hres*vres*sizeof(COLR))) == NULL) {
357                  fprintf(stderr, "%s: out of memory\n", progname);
358                  exit(1);
359          }
360          signal(SIGALRM, onalrm);
361 +        if (timelim)
362 +                alarm(timelim);
363          return;
364   filerr:
365 <        fprintf(stderr, "%s: file i/o error\n", outfile);
365 >        fprintf(stderr, "%s: i/o error on file \"%s\"\n", progname, outfile);
366          exit(1);
367   }
368  
369  
370 < int
371 < nextpiece(xp, yp)               /* get next piece assignment */
372 < int  *xp, *yp;
370 > static int
371 > nextpiece(              /* get next piece assignment */
372 >        int     *xp,
373 >        int     *yp
374 > )
375   {
203        extern char  *fgets();
204        struct flock  fls;
205        char  buf[64];
206
376          if (gotalrm)                    /* someone wants us to quit */
377                  return(0);
378 <        if (syncfd != -1) {             /* use sync file */
379 <                fls.l_type = F_WRLCK;           /* gain exclusive access */
380 <                fls.l_whence = 0;
381 <                fls.l_start = 0L;
382 <                fls.l_len = 0L;
383 <                fcntl(syncfd, F_SETLKW, &fls);
384 <                lseek(syncfd, 0L, 0);
385 <                buf[read(syncfd, buf, sizeof(buf)-1)] = '\0';
386 <                if (sscanf(buf, "%*d %*d %d %d", xp, yp) < 2) {
387 <                        *xp = hmult;
219 <                        *yp = vmult-1;
220 <                }
221 <                if (--(*xp) < 0) {              /* decrement position */
378 >        if (syncfp != NULL) {           /* use sync file */
379 >                /*
380 >                 * So we don't necessarily have to lock and unlock the file
381 >                 * multiple times (very slow), we establish an exclusive
382 >                 * lock at the beginning on our synchronization file and
383 >                 * maintain it in the subroutine rvrpiece().
384 >                 */
385 >                sflock(F_WRLCK);
386 >                fseek(syncfp, 0L, 0);           /* read position */
387 >                if (fscanf(syncfp, "%*d %*d %d %d", xp, yp) < 2) {
388                          *xp = hmult-1;
389 <                        if (--(*yp) < 0) {      /* all done! */
390 <                                close(syncfd);
389 >                        *yp = vmult;
390 >                }
391 >                if (rvrlim == 0)                /* initialize recovery limit */
392 >                        rvrlim = *xp*vmult + *yp;
393 >                if (rvrpiece(xp, yp)) {         /* do stragglers first */
394 >                        sflock(F_UNLCK);
395 >                        return(1);
396 >                }
397 >                if (--(*yp) < 0) {              /* decrement position */
398 >                        *yp = vmult-1;
399 >                        if (--(*xp) < 0) {      /* all done */
400 >                                sflock(F_UNLCK);
401                                  return(0);
402                          }
403                  }
404 <                sprintf(buf, "%d %d\n%d %d\n", hmult, vmult, *xp, *yp);
405 <                lseek(syncfd, 0L, 0);           /* write new position */
406 <                write(syncfd, buf, strlen(buf));
407 <                fls.l_type = F_UNLCK;           /* release sync file */
232 <                fcntl(syncfd, F_SETLKW, &fls);
404 >                fseek(syncfp, 0L, 0);           /* write new position */
405 >                fprintf(syncfp, "%4d %4d\n%4d %4d\n\n", hmult, vmult, *xp, *yp);
406 >                fflush(syncfp);
407 >                sflock(F_UNLCK);                /* release sync file */
408                  return(1);
409          }
410 <        if (fgets(buf, sizeof(buf), stdin) == NULL)     /* use stdin */
236 <                return(0);
237 <        if (sscanf(buf, "%d %d", xp, yp) == 2)
238 <                return(1);
239 <        fprintf(stderr, "%s: input format error\n", progname);
240 <        exit(1);
410 >        return(scanf("%d %d", xp, yp) == 2);    /* use stdin */
411   }
412  
413  
414 < int
415 < cleanup()                       /* close rpict process and clean up */
414 > static int
415 > rvrpiece(               /* check for recoverable pieces */
416 >        int     *xp,
417 >        int     *yp
418 > )
419   {
420 <        register int  rpstat;
420 >        static char  *pdone = NULL;     /* which pieces are done */
421 >        static long  readpos = -1;      /* how far we've read */
422 >        int  px, py, i;
423 >        /*
424 >         * This routine is called by nextpiece() with an
425 >         * exclusive lock on syncfp and the file pointer at the
426 >         * appropriate position to read in the finished pieces.
427 >         */
428 >        if (rvrlim < 0)
429 >                return(0);              /* only check if asked */
430 >        if (pdone == NULL)              /* first call */
431 >                pdone = (char *)calloc(hmult*vmult, sizeof(char));
432 >        if (pdone == NULL) {
433 >                fprintf(stderr, "%s: out of memory\n", progname);
434 >                exit(1);
435 >        }
436 >        if (readpos != -1)              /* mark what's been done */
437 >                fseek(syncfp, readpos, 0);
438 >        while (fscanf(syncfp, "%d %d", &px, &py) == 2)
439 >                pdone[px*vmult+py] = 1;
440 >        if (!feof(syncfp)) {
441 >                fprintf(stderr, "%s: format error in sync file\n", progname);
442 >                exit(1);
443 >        }
444 >        readpos = ftell(syncfp);
445 >        i = hmult*vmult;                /* find an unaccounted for piece */
446 >        while (i-- > rvrlim)
447 >                if (!pdone[i]) {
448 >                        *xp = i / vmult;
449 >                        *yp = i % vmult;
450 >                        pdone[i] = 1;   /* consider it done */
451 >                        return(1);
452 >                }
453 >        rvrlim = -1;                    /* nothing left to recover */
454 >        free(pdone);
455 >        pdone = NULL;
456 >        return(0);
457 > }
458  
459 <        free((char *)scanline);
459 >
460 > static int
461 > cleanup(                        /* close rpict process and clean up */
462 >        int  rstat
463 > )
464 > {
465 >        int  status;
466 >
467 >        bfree((char *)pbuf, hres*vres*sizeof(COLR));
468          fclose(torp);
469          fclose(fromrp);
470 <        rpstat = close_process(rpd);
471 <        if (rpstat == -1)
472 <                rpstat = 0;
473 <        return(rpstat);
470 >        while (wait(&status) != -1)
471 >                if (rstat == 0)
472 >                        rstat = status>>8 & 0xff;
473 >        return(rstat);
474   }
475  
476  
477 < rpiece()                        /* render picture piece by piece */
477 > static void
478 > rpiece(void)                    /* render picture piece by piece */
479   {
480 +        char  *err;
481          VIEW  pview;
482          int  xorg, yorg;
483 <        
483 >                                        /* render each piece */
484          while (nextpiece(&xorg, &yorg)) {
485 <                copystruct(&pview, &ourview);   /* compute view for piece */
486 <                switch (ourview.type) {
487 <                case VT_PER:
488 <                        pview.horiz = 2.*180./PI*atan(
489 <                                        tan(PI/180./2.*ourview.horiz)/hmult );
490 <                        pview.vert = 2.*180./PI*atan(
271 <                                        tan(PI/180./2.*ourview.vert)/vmult );
272 <                        break;
273 <                case VT_PAR:
274 <                case VT_ANG:
275 <                        pview.horiz = ourview.horiz / hmult;
276 <                        pview.vert = ourview.vert / vmult;
277 <                        break;
278 <                case VT_HEM:
279 <                        pview.horiz = 2.*180./PI*asin(
280 <                                        sin(PI/180./2.*ourview.horiz)/hmult );
281 <                        pview.vert = 2.*180./PI*asin(
282 <                                        sin(PI/180./2.*ourview.vert)/vmult );
283 <                        break;
284 <                default:
285 <                        fprintf(stderr, "%s: unknown view type '-vt%c'\n",
286 <                                        progname, ourview.type);
287 <                        exit(1);
485 >                pview = ourview;
486 >                err = cropview(&pview, (double)xorg/hmult, (double)yorg/vmult,
487 >                                        (xorg+1.)/hmult, (yorg+1.)/vmult);
488 >                if (err != NULL) {
489 >                        fprintf(stderr, "%s: %s\n", progname, err);
490 >                        exit(cleanup(1));
491                  }
289                pview.hoff += xorg - 0.5*(hmult-1);
290                pview.voff += yorg - 0.5*(vmult-1);
492                  fputs(VIEWSTR, torp);
493                  fprintview(&pview, torp);
494 <                putc('\n', torp);
495 <                fflush(torp);                   /* assign piece to rpict */
494 >                fputc('\n', torp);
495 >                fflush(torp);                   /* assigns piece to rpict */
496                  putpiece(xorg, yorg);           /* place piece in output */
296                if (verbose) {                  /* notify caller */
297                        printf("%d %d done\n", xorg, yorg);
298                        fflush(stdout);
299                }
497          }
498   }
499  
500  
501 < putpiece(xpos, ypos)            /* get next piece from rpict */
502 < int  xpos, ypos;
501 > static int
502 > putpiece(               /* get next piece from rpict */
503 > int     xpos,
504 > int     ypos
505 > )
506   {
507 +        struct flock  fls;
508 +        int  pid, status;
509          int  hr, vr;
510          int  y;
511 <
512 <        if (xpos < 0 | ypos < 0 | xpos >= hmult | ypos >= vmult) {
511 >                                /* check bounds */
512 >        if ((xpos < 0) | (ypos < 0) | (xpos >= hmult) | (ypos >= vmult)) {
513                  fprintf(stderr, "%s: requested piece (%d,%d) out of range\n",
514                                  progname, xpos, ypos);
515 <                exit(1);
515 >                exit(cleanup(1));
516          }
517 <        getheader(fromrp, NULL);        /* discard header info. */
518 <        if (fscnresolu(&hr, &vr, fromrp) < 0 || /* check resolution */
519 <                        hr != hres || vr != vres) {
517 >                                /* check header from rpict */
518 >        guard_io();
519 >        getheader(fromrp, NULL, NULL);
520 >        if (!fscnresolu(&hr, &vr, fromrp) || (hr != hres) | (vr != vres)) {
521                  fprintf(stderr, "%s: resolution mismatch from %s\n",
522                                  progname, rpargv[0]);
523 <                exit(1);
523 >                exit(cleanup(1));
524          }
525 <        for (y = 0; y < vr; y++) {      /* transfer scanlines */
526 <                if (freadcolrs(scanline, hr, fromrp) < 0) {
525 >        if (verbose) {                          /* notify caller */
526 >                printf("%d %d begun\n", xpos, ypos);
527 >                fflush(stdout);
528 >        }
529 >        unguard();
530 >                                /* load new piece into buffer */
531 >        for (y = 0; y < vr; y++) {
532 >                guard_io();
533 >                if (freadcolrs(pbuf+y*hr, hr, fromrp) < 0) {
534                          fprintf(stderr, "%s: read error from %s\n",
535                                          progname, rpargv[0]);
536 <                        exit(1);
536 >                        exit(cleanup(1));
537                  }
538 <                if ((y == 0 || hmult != 1) && lseek(outfd,
539 < scanorig+((long)((vmult-1-ypos)*vres+y)*hmult*hres+xpos*hres)*sizeof(COLR),
540 <                                0) == -1) {
541 <                        fprintf(stderr, "%s: seek error\n", outfile);
542 <                        exit(1);
538 >                unguard();
539 >        }
540 > #if MAXFORK
541 >                                /* fork so we don't slow rpict down */
542 >        if ((pid = fork()) > 0) {
543 >                if (++nforked >= MAXFORK) {
544 >                        wait(&status);          /* reap a child */
545 >                        if (status)
546 >                                exit(cleanup(status>>8 & 0xff));
547 >                        nforked--;
548                  }
549 <                if (writebuf(outfd, (char *)scanline, hr*sizeof(COLR)) !=
550 <                                hr*sizeof(COLR)) {
551 <                        fprintf(stderr, "%s: write error\n", outfile);
552 <                        exit(1);
549 >                return(pid);
550 >        }
551 > #else
552 >        pid = -1;               /* no forking */
553 > #endif
554 >        fls.l_start = scanorig +
555 >                ((long)(vmult-1-ypos)*vres*hmult+xpos)*hres*sizeof(COLR);
556 > #if NFS
557 >        fls.l_len = ((long)(vres-1)*hmult+1)*hres*sizeof(COLR);
558 >                                /* lock file section so NFS doesn't mess up */
559 >        fls.l_whence = 0;
560 >        fls.l_type = F_WRLCK;
561 > #if 0
562 >        if (fcntl(outfd, F_SETLKW, &fls) < 0)
563 >                filerr("lock");
564 > #else
565 >        dolock(outfd, F_WRLCK);
566 > #endif
567 > #endif
568 >                                /* write new piece to file */
569 >        if (lseek(outfd, (off_t)fls.l_start, SEEK_SET) < 0)
570 >                filerr("seek");
571 >        if (hmult == 1) {
572 >                if (writebuf(outfd, pbuf,
573 >                                vr*hr*sizeof(COLR)) != vr*hr*sizeof(COLR))
574 >                        filerr("write");
575 >        } else
576 >                for (y = 0; y < vr; y++) {
577 >                        if (writebuf(outfd, pbuf+y*hr,
578 >                                        hr*sizeof(COLR)) != hr*sizeof(COLR))
579 >                                filerr("write");
580 >                        if (y < vr-1 && lseek(outfd,
581 >                                        (off_t)(hmult-1)*hr*sizeof(COLR),
582 >                                        SEEK_CUR) < 0)
583 >                                filerr("seek");
584                  }
585 + #if NFS
586 +        fls.l_type = F_UNLCK;           /* release lock */
587 + #if 0
588 +        if (fcntl(outfd, F_SETLKW, &fls) < 0)
589 +                filerr("lock");
590 + #else
591 +        dolock(outfd, F_UNLCK);
592 + #endif
593 + #endif
594 +        if (verbose) {                          /* notify caller */
595 +                printf("%d %d done\n", xpos, ypos);
596 +                fflush(stdout);
597          }
598 +        if (syncfp != NULL) {                   /* record what's been done */
599 +                sflock(F_WRLCK);
600 +                fseek(syncfp, 0L, 2);           /* append index */
601 +                fprintf(syncfp, "%4d %4d\n", xpos, ypos);
602 +                fflush(syncfp);
603 +                                /*** Unlock not necessary, since
604 +                sflock(F_UNLCK);        _exit() or nextpiece() is next ***/
605 +        }
606 +        if (pid == -1)          /* didn't fork or fork failed */
607 +                return(0);
608 +        _exit(0);               /* else exit child process (releasing locks) */
609   }
610 +
611 +
612 + static void
613 + filerr(                 /* report file error and exit */
614 +        char  *t
615 + )
616 + {
617 +        fprintf(stderr, "%s: %s error on file \"%s\": %s\n",
618 +                        progname, t, outfile, strerror(errno));
619 +        _exit(1);
620 + }
621 +
622 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines