12 |
|
|
13 |
|
#include "holo.h" |
14 |
|
|
15 |
+ |
#include <signal.h> |
16 |
+ |
|
17 |
|
#ifndef BKBSIZE |
18 |
|
#define BKBSIZE 256 /* beam clump size (kilobytes) */ |
19 |
|
#endif |
20 |
|
|
19 |
– |
#define flgop(p,i,op) ((p)[(i)>>5] op (1L<<((i)&0x1f))) |
20 |
– |
#define isset(p,i) flgop(p,i,&) |
21 |
– |
#define setfl(p,i) flgop(p,i,|=) |
22 |
– |
#define clrfl(p,i) flgop(p,i,&=~) |
23 |
– |
|
21 |
|
char *progname; |
22 |
+ |
char tempfile[128]; |
23 |
|
|
24 |
+ |
extern char *rindex(); |
25 |
+ |
extern int quit(); |
26 |
|
extern long rhinitcopy(); |
27 |
|
|
28 |
|
|
30 |
|
int argc; |
31 |
|
char *argv[]; |
32 |
|
{ |
33 |
– |
char nambuf[128]; |
33 |
|
char *inpname, *outname; |
34 |
|
int hdfd[2]; |
35 |
|
long nextipos, lastopos, thisopos; |
42 |
|
inpname = argv[1]; |
43 |
|
if (argc == 3) /* use given output file */ |
44 |
|
outname = argv[2]; |
45 |
< |
else { /* else create temporary file */ |
46 |
< |
strcpy(nambuf, inpname); |
47 |
< |
if ((outname = strrchr(nambuf, '/')) != NULL) |
45 |
> |
else { /* else use temporary file */ |
46 |
> |
if (access(inpname, R_OK|W_OK) < 0) { /* check permissions */ |
47 |
> |
sprintf(errmsg, "cannot access \"%s\"", inpname); |
48 |
> |
error(SYSTEM, errmsg); |
49 |
> |
} |
50 |
> |
strcpy(tempfile, inpname); |
51 |
> |
if ((outname = rindex(tempfile, '/')) != NULL) |
52 |
|
outname++; |
53 |
|
else |
54 |
< |
outname = nambuf; |
54 |
> |
outname = tempfile; |
55 |
|
sprintf(outname, "rho%d.hdk", getpid()); |
56 |
< |
outname = nambuf; |
56 |
> |
outname = tempfile; |
57 |
|
} |
58 |
|
/* copy holodeck file header */ |
59 |
|
nextipos = rhinitcopy(hdfd, inpname, outname); |
78 |
|
/* clean up */ |
79 |
|
close(hdfd[0]); |
80 |
|
close(hdfd[1]); |
81 |
< |
if (argc == 2 && rename(outname, inpname) < 0) { |
81 |
> |
if (outname == tempfile && rename(outname, inpname) < 0) { |
82 |
|
sprintf(errmsg, "cannot rename \"%s\" to \"%s\"", |
83 |
|
outname, inpname); |
84 |
|
error(SYSTEM, errmsg); |
103 |
|
sprintf(errmsg, "cannot open \"%s\" for writing", outfn); |
104 |
|
error(SYSTEM, errmsg); |
105 |
|
} |
106 |
+ |
/* set up signal handling */ |
107 |
+ |
if (signal(SIGINT, quit) == SIG_IGN) signal(SIGINT, SIG_IGN); |
108 |
+ |
if (signal(SIGHUP, quit) == SIG_IGN) signal(SIGHUP, SIG_IGN); |
109 |
+ |
if (signal(SIGTERM, quit) == SIG_IGN) signal(SIGTERM, SIG_IGN); |
110 |
+ |
#ifdef SIGXCPU |
111 |
+ |
if (signal(SIGXCPU, quit) == SIG_IGN) signal(SIGXCPU, SIG_IGN); |
112 |
+ |
if (signal(SIGXFSZ, quit) == SIG_IGN) signal(SIGXFSZ, SIG_IGN); |
113 |
+ |
#endif |
114 |
|
/* copy and verify header */ |
115 |
< |
if (checkheader(infp, HOLOFMT, outfp) < 0 || |
105 |
< |
getw(infp) != HOLOMAGIC) |
115 |
> |
if (checkheader(infp, HOLOFMT, outfp) < 0 || getw(infp) != HOLOMAGIC) |
116 |
|
error(USER, "input not in holodeck format"); |
117 |
|
fputformat(HOLOFMT, outfp); |
118 |
|
fputc('\n', outfp); |
125 |
|
fclose(infp); |
126 |
|
if (fclose(outfp) == EOF) |
127 |
|
error(SYSTEM, "file flushing error in rhinitcopy"); |
128 |
< |
/* we flush everything manually */ |
129 |
< |
hdcachesize = 0; |
128 |
> |
/* check cache size */ |
129 |
> |
if (BKBSIZE*1024*1.5 > hdcachesize) |
130 |
> |
hdcachesize = BKBSIZE*1024*1.5; |
131 |
|
/* return input position */ |
132 |
|
return(ifpos); |
133 |
|
} |
134 |
|
|
135 |
|
|
136 |
< |
gcshifti(gc, ia, di, hp) /* shift cell row or column */ |
126 |
< |
register GCOORD *gc; |
127 |
< |
int ia, di; |
128 |
< |
register HOLO *hp; |
129 |
< |
{ |
130 |
< |
int nw; |
136 |
> |
static HOLO *hout; /* output holodeck section */ |
137 |
|
|
138 |
< |
if (di > 0) { |
139 |
< |
if (++gc->i[ia] >= hp->grid[((gc->w>>1)+1+ia)%3]) { |
140 |
< |
nw = ((gc->w&~1) + (ia<<1) + 3) % 6; |
141 |
< |
gc->i[ia] = gc->i[1-ia]; |
136 |
< |
gc->i[1-ia] = gc->w&1 ? hp->grid[((nw>>1)+2-ia)%3]-1 : 0; |
137 |
< |
gc->w = nw; |
138 |
< |
} |
139 |
< |
} else if (di < 0) { |
140 |
< |
if (--gc->i[ia] < 0) { |
141 |
< |
nw = ((gc->w&~1) + (ia<<1) + 2) % 6; |
142 |
< |
gc->i[ia] = gc->i[1-ia]; |
143 |
< |
gc->i[1-ia] = gc->w&1 ? hp->grid[((nw>>1)+2-ia)%3]-1 : 0; |
144 |
< |
gc->w = nw; |
145 |
< |
} |
146 |
< |
} |
147 |
< |
} |
148 |
< |
|
149 |
< |
|
150 |
< |
mkneighgrid(ng, hp, gc) /* compute neighborhood for grid cell */ |
151 |
< |
GCOORD ng[3*3]; |
152 |
< |
HOLO *hp; |
153 |
< |
GCOORD *gc; |
138 |
> |
static int |
139 |
> |
xferbeam(bp, hb) /* transfer the given beam to hout and free */ |
140 |
> |
register BEAM *bp; |
141 |
> |
HDBEAMI *hb; |
142 |
|
{ |
143 |
< |
GCOORD gci0; |
144 |
< |
register int i, j; |
145 |
< |
|
158 |
< |
for (i = 3; i--; ) { |
159 |
< |
copystruct(&gci0, gc); |
160 |
< |
gcshifti(&gci0, 0, i-1, hp); |
161 |
< |
for (j = 3; j--; ) { |
162 |
< |
copystruct(ng+(3*i+j), &gci0); |
163 |
< |
gcshifti(ng+(3*i+j), gci0.w==gc->w, j-1, hp); |
164 |
< |
} |
165 |
< |
} |
166 |
< |
} |
167 |
< |
|
168 |
< |
|
169 |
< |
int bneighlist[9*9-1]; |
170 |
< |
int bneighrem; |
171 |
< |
|
172 |
< |
#define nextneigh() (bneighrem<=0 ? 0 : bneighlist[--bneighrem]) |
173 |
< |
|
174 |
< |
int |
175 |
< |
firstneigh(hp, b) /* initialize neighbor list and return first */ |
176 |
< |
HOLO *hp; |
177 |
< |
int b; |
178 |
< |
{ |
179 |
< |
GCOORD wg0[9], wg1[9], bgc[2]; |
180 |
< |
int i, j; |
181 |
< |
|
182 |
< |
hdbcoord(bgc, hp, b); |
183 |
< |
mkneighgrid(wg0, hp, bgc); |
184 |
< |
mkneighgrid(wg1, hp, bgc+1); |
185 |
< |
bneighrem = 0; |
186 |
< |
for (i = 9; i--; ) |
187 |
< |
for (j = 9; j--; ) { |
188 |
< |
if (i == 4 & j == 4) |
189 |
< |
continue; |
190 |
< |
if (wg0[i].w == wg1[j].w) |
191 |
< |
continue; |
192 |
< |
copystruct(bgc, wg0+i); |
193 |
< |
copystruct(bgc+1, wg1+j); |
194 |
< |
bneighlist[bneighrem++] = hdbindex(hp, bgc); |
195 |
< |
#ifdef DEBUG |
196 |
< |
if (bneighlist[bneighrem-1] <= 0) |
197 |
< |
error(CONSISTENCY, "bad beam in firstneigh"); |
198 |
< |
#endif |
199 |
< |
} |
200 |
< |
return(nextneigh()); |
201 |
< |
} |
202 |
< |
|
203 |
< |
|
204 |
< |
BEAMI *beamdir; |
205 |
< |
|
206 |
< |
int |
207 |
< |
bpcmp(b1p, b2p) /* compare beam positions on disk */ |
208 |
< |
int *b1p, *b2p; |
209 |
< |
{ |
210 |
< |
register long pdif = beamdir[*b1p].fo - beamdir[*b2p].fo; |
211 |
< |
|
212 |
< |
if (pdif > 0) return(1); |
213 |
< |
if (pdif < 0) return(-1); |
143 |
> |
bcopy((char *)hdbray(bp), (char *)hdnewrays(hout,hb->b,bp->nrm), |
144 |
> |
bp->nrm*sizeof(RAYVAL)); |
145 |
> |
hdfreebeam(hb->h, hb->b); |
146 |
|
return(0); |
147 |
|
} |
148 |
|
|
217 |
– |
|
149 |
|
copysect(ifd, ofd) /* copy holodeck section from ifd to ofd */ |
150 |
|
int ifd, ofd; |
151 |
|
{ |
152 |
< |
static short primes[] = {9431,6803,4177,2659,1609,887,587,251,47,1}; |
222 |
< |
register HOLO *hinp; |
223 |
< |
HOLO *hout; |
224 |
< |
register BEAM *bp; |
225 |
< |
unsigned int4 *bflags; |
226 |
< |
int *bqueue; |
227 |
< |
int bqlen; |
228 |
< |
int4 bqtotal; |
229 |
< |
int bc, bci, bqc, myprime; |
230 |
< |
register int i; |
152 |
> |
HOLO *hinp; |
153 |
|
/* load input section directory */ |
154 |
|
hinp = hdinit(ifd, NULL); |
155 |
|
/* create output section directory */ |
156 |
|
hout = hdinit(ofd, (HDGRID *)hinp); |
157 |
< |
/* allocate beam queue */ |
158 |
< |
bqueue = (int *)malloc(nbeams(hinp)*sizeof(int)); |
159 |
< |
bflags = (int4 *)calloc((nbeams(hinp)>>3)+1, sizeof(int4)); |
238 |
< |
if (bqueue == NULL | bflags == NULL) |
239 |
< |
error(SYSTEM, "out of memory in copysect"); |
240 |
< |
/* mark empty beams as done */ |
241 |
< |
for (i = nbeams(hinp); i-- > 0; ) |
242 |
< |
if (!hinp->bi[i].nrd) |
243 |
< |
setfl(bflags, i); |
244 |
< |
/* pick a good prime step size */ |
245 |
< |
for (i = 0; primes[i]<<5 >= nbeams(hinp); i++) |
246 |
< |
; |
247 |
< |
while ((myprime = primes[i++]) > 1) |
248 |
< |
if (nbeams(hinp) % myprime) |
249 |
< |
break; |
250 |
< |
/* add each input beam and neighbors */ |
251 |
< |
for (bc = bci = nbeams(hinp); bc > 0; bc--, |
252 |
< |
bci += bci>myprime ? -myprime : nbeams(hinp)-myprime) { |
253 |
< |
if (isset(bflags, bci)) |
254 |
< |
continue; |
255 |
< |
bqueue[0] = bci; /* initialize queue */ |
256 |
< |
bqlen = 1; |
257 |
< |
bqtotal = bnrays(hinp, bci); |
258 |
< |
setfl(bflags, bci); |
259 |
< |
/* run through growing queue */ |
260 |
< |
for (bqc = 0; bqc < bqlen; bqc++) { |
261 |
< |
/* add neighbors until full */ |
262 |
< |
for (i = firstneigh(hinp,bqueue[bqc]); i > 0; |
263 |
< |
i = nextneigh()) { |
264 |
< |
if (isset(bflags, i)) /* done already? */ |
265 |
< |
continue; |
266 |
< |
bqueue[bqlen++] = i; /* add it */ |
267 |
< |
bqtotal += bnrays(hinp, i); |
268 |
< |
setfl(bflags, i); |
269 |
< |
if (bqtotal >= BKBSIZE*1024/sizeof(RAYVAL)) |
270 |
< |
break; /* queue full */ |
271 |
< |
} |
272 |
< |
if (i > 0) |
273 |
< |
break; |
274 |
< |
} |
275 |
< |
beamdir = hinp->bi; /* sort queue */ |
276 |
< |
qsort((char *)bqueue, bqlen, sizeof(*bqueue), bpcmp); |
277 |
< |
/* transfer each beam */ |
278 |
< |
for (i = 0; i < bqlen; i++) { |
279 |
< |
bp = hdgetbeam(hinp, bqueue[i]); |
280 |
< |
bcopy((char *)hdbray(bp), |
281 |
< |
(char *)hdnewrays(hout,bqueue[i],bp->nrm), |
282 |
< |
bp->nrm*sizeof(RAYVAL)); |
283 |
< |
hdfreebeam(hinp, bqueue[i]); |
284 |
< |
} |
285 |
< |
hdfreebeam(hout, 0); /* flush output block */ |
286 |
< |
#ifdef DEBUG |
287 |
< |
hdsync(hout, 0); |
288 |
< |
#endif |
289 |
< |
} |
290 |
< |
/* we're done -- clean up */ |
291 |
< |
free((char *)bqueue); |
292 |
< |
free((char *)bflags); |
157 |
> |
/* clump the beams */ |
158 |
> |
clumpbeams(hinp, 0, BKBSIZE*1024, xferbeam); |
159 |
> |
/* clean up */ |
160 |
|
hddone(hinp); |
161 |
|
hddone(hout); |
162 |
|
} |
184 |
|
quit(code) /* exit the program gracefully */ |
185 |
|
int code; |
186 |
|
{ |
187 |
< |
hdsync(NULL, 1); /* write out any buffered data */ |
187 |
> |
if (tempfile[0]) |
188 |
> |
unlink(tempfile); |
189 |
|
exit(code); |
190 |
|
} |