| 12 |
|
|
| 13 |
|
#include "holo.h" |
| 14 |
|
|
| 15 |
+ |
#include <signal.h> |
| 16 |
+ |
|
| 17 |
|
#ifndef BKBSIZE |
| 18 |
< |
#define BKBSIZE 64 /* beam clump size (kilobytes) */ |
| 18 |
> |
#define BKBSIZE 256 /* beam clump size (kilobytes) */ |
| 19 |
|
#endif |
| 20 |
|
|
| 19 |
– |
|
| 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 |
|
{ |
| 29 |
– |
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 || |
| 101 |
< |
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 */ |
| 122 |
< |
register GCOORD *gc; |
| 123 |
< |
int ia, di; |
| 124 |
< |
register HOLO *hp; |
| 125 |
< |
{ |
| 126 |
< |
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]; |
| 132 |
< |
gc->i[1-ia] = gc->w&1 ? hp->grid[((nw>>1)+2-ia)%3]-1 : 0; |
| 133 |
< |
gc->w = nw; |
| 134 |
< |
} |
| 135 |
< |
} else if (di < 0) { |
| 136 |
< |
if (--gc->i[ia] < 0) { |
| 137 |
< |
nw = ((gc->w&~1) + (ia<<1) + 2) % 6; |
| 138 |
< |
gc->i[ia] = gc->i[1-ia]; |
| 139 |
< |
gc->i[1-ia] = gc->w&1 ? hp->grid[((nw>>1)+2-ia)%3]-1 : 0; |
| 140 |
< |
gc->w = nw; |
| 141 |
< |
} |
| 142 |
< |
} |
| 143 |
< |
} |
| 144 |
< |
|
| 145 |
< |
|
| 146 |
< |
mkneighgrid(ng, hp, gc) /* compute neighborhood for grid cell */ |
| 147 |
< |
GCOORD ng[3*3]; |
| 148 |
< |
HOLO *hp; |
| 149 |
< |
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 |
< |
int i, j; |
| 145 |
< |
|
| 146 |
< |
for (i = 3; i--; ) { |
| 155 |
< |
copystruct(&gci0, gc); |
| 156 |
< |
gcshifti(&gci0, 0, i-1, hp); |
| 157 |
< |
for (j = 3; j--; ) { |
| 158 |
< |
copystruct(ng+(3*i+j), &gci0); |
| 159 |
< |
gcshifti(ng+(3*i+j), 1, j-1, hp); |
| 160 |
< |
} |
| 161 |
< |
} |
| 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 |
|
|
| 164 |
– |
|
| 165 |
– |
int bneighlist[9*9-1]; |
| 166 |
– |
int bneighrem; |
| 167 |
– |
|
| 168 |
– |
#define nextneigh() (bneighrem<=0 ? 0 : bneighlist[--bneighrem]) |
| 169 |
– |
|
| 170 |
– |
int |
| 171 |
– |
firstneigh(hp, b) /* initialize neighbor list and return first */ |
| 172 |
– |
HOLO *hp; |
| 173 |
– |
int b; |
| 174 |
– |
{ |
| 175 |
– |
GCOORD wg0[9], wg1[9], bgc[2]; |
| 176 |
– |
int i, j; |
| 177 |
– |
|
| 178 |
– |
hdbcoord(bgc, hp, b); |
| 179 |
– |
mkneighgrid(wg0, hp, bgc); |
| 180 |
– |
mkneighgrid(wg1, hp, bgc+1); |
| 181 |
– |
bneighrem = 0; |
| 182 |
– |
for (i = 9; i--; ) |
| 183 |
– |
for (j = 9; j--; ) { |
| 184 |
– |
if (i == 4 & j == 4) |
| 185 |
– |
continue; |
| 186 |
– |
copystruct(bgc, wg0+i); |
| 187 |
– |
copystruct(bgc+1, wg1+j); |
| 188 |
– |
bneighlist[bneighrem++] = hdbindex(hp, bgc); |
| 189 |
– |
} |
| 190 |
– |
return(nextneigh()); |
| 191 |
– |
} |
| 192 |
– |
|
| 193 |
– |
|
| 149 |
|
copysect(ifd, ofd) /* copy holodeck section from ifd to ofd */ |
| 150 |
|
int ifd, ofd; |
| 151 |
|
{ |
| 152 |
< |
#define beamdone(b) (!hinp->bi[b].nrd || bnrays(hout,b)) |
| 198 |
< |
static short primes[] = {9431,6803,4177,2659,1609,887,587,251,47,1}; |
| 199 |
< |
register HOLO *hinp, *hout; |
| 200 |
< |
register BEAM *bp; |
| 201 |
< |
int *bqueue; |
| 202 |
< |
int bqlen; |
| 203 |
< |
int4 bqtotal; |
| 204 |
< |
int bc, bci, bqc, bnc, myprime; |
| 205 |
< |
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 |
< |
if ((bqueue = (int *)malloc(nbeams(hinp)*sizeof(int))) == NULL) |
| 159 |
< |
error(SYSTEM, "out of memory in copysect"); |
| 213 |
< |
/* pick a good prime step size */ |
| 214 |
< |
for (i = 0; primes[i]<<5 >= nbeams(hinp); i++) |
| 215 |
< |
; |
| 216 |
< |
while ((myprime = primes[i++]) > 1) |
| 217 |
< |
if (nbeams(hinp) % myprime) |
| 218 |
< |
break; |
| 219 |
< |
/* add each input beam and neighbors */ |
| 220 |
< |
for (bc = bci = nbeams(hinp); bc > 0; bc--, |
| 221 |
< |
bci += bci>myprime ? -myprime : nbeams(hinp)-myprime) { |
| 222 |
< |
if (beamdone(bci)) |
| 223 |
< |
continue; |
| 224 |
< |
bqueue[0] = bci; /* initialize queue */ |
| 225 |
< |
bqlen = 1; |
| 226 |
< |
bqtotal = bnrays(hinp, bci); |
| 227 |
< |
/* run through growing queue */ |
| 228 |
< |
for (bqc = 0; bqc < bqlen; bqc++) { |
| 229 |
< |
/* transfer the beam */ |
| 230 |
< |
bp = hdgetbeam(hinp, bqueue[bqc]); |
| 231 |
< |
bcopy((char *)hdbray(bp), |
| 232 |
< |
(char *)hdnewrays(hout,bqueue[bqc],bp->nrm), |
| 233 |
< |
bp->nrm*sizeof(RAYVAL)); |
| 234 |
< |
hdfreebeam(hinp, bqueue[bqc]); |
| 235 |
< |
/* check queue size */ |
| 236 |
< |
if (bqtotal >= BKBSIZE*1024/sizeof(RAYVAL)) |
| 237 |
< |
continue; |
| 238 |
< |
/* add neighbors to queue */ |
| 239 |
< |
for (bnc = firstneigh(hinp,bqueue[bqc]); bnc > 0; |
| 240 |
< |
bnc = nextneigh()) { |
| 241 |
< |
if (beamdone(bnc)) /* see if valid */ |
| 242 |
< |
continue; |
| 243 |
< |
for (i = bqlen; i--; ) |
| 244 |
< |
if (bqueue[i] == bnc) break; |
| 245 |
< |
if (i >= 0) |
| 246 |
< |
continue; |
| 247 |
< |
bqueue[bqlen++] = bnc; /* add it */ |
| 248 |
< |
bqtotal += bnrays(hinp, bnc); |
| 249 |
< |
if (bqtotal >= BKBSIZE*1024/sizeof(RAYVAL)) |
| 250 |
< |
break; /* queue full */ |
| 251 |
< |
} |
| 252 |
< |
} |
| 253 |
< |
hdfreebeam(hout, 0); /* flush output block */ |
| 254 |
< |
} |
| 255 |
< |
/* we're done -- clean up */ |
| 256 |
< |
free((char *)bqueue); |
| 157 |
> |
/* clump the beams */ |
| 158 |
> |
clumpbeams(hinp, 0, BKBSIZE*1024, xferbeam); |
| 159 |
> |
/* clean up */ |
| 160 |
|
hddone(hinp); |
| 161 |
|
hddone(hout); |
| 259 |
– |
#undef beamdone |
| 162 |
|
} |
| 163 |
|
|
| 164 |
|
|
| 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 |
|
} |