| 1 | /* ----------------------------- myz.c -------------------------------- */ |
|---|
| 2 | |
|---|
| 3 | #include <stdio.h> |
|---|
| 4 | #include <ctype.h> |
|---|
| 5 | #include <string.h> |
|---|
| 6 | |
|---|
| 7 | #include "cisis.h" |
|---|
| 8 | #include "cirun.h" |
|---|
| 9 | |
|---|
| 10 | /* ----------------------------- tlc.h -------------------------------- */ |
|---|
| 11 | |
|---|
| 12 | typedef union { |
|---|
| 13 | L0STRU l0x; |
|---|
| 14 | L1STRU l1x; |
|---|
| 15 | L2STRU l2x; |
|---|
| 16 | } LUSTRU; |
|---|
| 17 | |
|---|
| 18 | LUSTRU tlcleaf; |
|---|
| 19 | LZSTRU lzxleaf; |
|---|
| 20 | |
|---|
| 21 | #define MAXNAPU 256 |
|---|
| 22 | int napus; |
|---|
| 23 | LONGX *vapup[MAXNAPU]; |
|---|
| 24 | LONGX *apup; |
|---|
| 25 | LONGX maxapu,offapu,divapu; |
|---|
| 26 | |
|---|
| 27 | int parmtrace=0; |
|---|
| 28 | |
|---|
| 29 | #if ANSI |
|---|
| 30 | LONGX leafaddr(PUNT pos,PUNT punt); |
|---|
| 31 | #else |
|---|
| 32 | LONGX leafaddr(); |
|---|
| 33 | #endif |
|---|
| 34 | |
|---|
| 35 | void main(argc,argv) |
|---|
| 36 | int argc; |
|---|
| 37 | char *argv[]; |
|---|
| 38 | { |
|---|
| 39 | extern int trmtrace; |
|---|
| 40 | char *dbnp,*zdbnp; |
|---|
| 41 | int treecase,lex; |
|---|
| 42 | INVMAP *invp; |
|---|
| 43 | |
|---|
| 44 | PUNT punt; |
|---|
| 45 | LONGX punt1,punt2; |
|---|
| 46 | INFO info1,info2; |
|---|
| 47 | #if LIND |
|---|
| 48 | INFO info3; |
|---|
| 49 | LONGX lastaddr,thisaddr; |
|---|
| 50 | #endif |
|---|
| 51 | |
|---|
| 52 | int lzsize=0; |
|---|
| 53 | int filler=0; |
|---|
| 54 | LONGX nextaddr; |
|---|
| 55 | |
|---|
| 56 | LONGX tell,countell,rangtell; |
|---|
| 57 | LONGX telln,countelln,rangtelln; |
|---|
| 58 | int show; |
|---|
| 59 | FILE *fpx; |
|---|
| 60 | |
|---|
| 61 | int origlsiz; |
|---|
| 62 | LONGX origsize; |
|---|
| 63 | |
|---|
| 64 | char *p,*keyp,prevkey[LE2+1]; |
|---|
| 65 | int iarg,n,iock; |
|---|
| 66 | int eq,keylen,ne; |
|---|
| 67 | unsigned char *lzkp; |
|---|
| 68 | |
|---|
| 69 | N0STRU *n0p; |
|---|
| 70 | N1IDXE *n1xp; |
|---|
| 71 | N2IDXE *n2xp; |
|---|
| 72 | int nodesize; |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | if (argc < 3) { |
|---|
| 76 | printf("%s",cicopyr("Utility MYZ")); |
|---|
| 77 | printf("\n"); |
|---|
| 78 | printf("myz <dbn> <treecase> <zdbn> [<option> [...]] \n"); |
|---|
| 79 | printf("\n"); |
|---|
| 80 | printf("options: show[=<file>] \n"); |
|---|
| 81 | printf(" {tell|telln}=<times> \n"); |
|---|
| 82 | printf("\n"); |
|---|
| 83 | exit(1); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | dbnp=argv[1]; |
|---|
| 87 | dbxopt_ordwr=O_RDONLY; |
|---|
| 88 | invsetup(dbnp,0L,0L,0L); |
|---|
| 89 | dbxopt_ordwr=O_RDWR; |
|---|
| 90 | invp=dbxstorp(dbnp)->dbxifmap; |
|---|
| 91 | |
|---|
| 92 | if (sscanf(argv[2],"%d",&treecase) != 1) fatal(argv[3]); |
|---|
| 93 | if (treecase != 1 && treecase != 2) fatal("treecase 1/2"); |
|---|
| 94 | treecase--; |
|---|
| 95 | |
|---|
| 96 | zdbnp=argv[3]; |
|---|
| 97 | |
|---|
| 98 | punt1=1; punt2=invp->cn[treecase].fmaxpos; |
|---|
| 99 | rangtell=punt2-punt1+1; |
|---|
| 100 | if (rangtell <= 0) rangtell=1; |
|---|
| 101 | fpx=stderr; show=0; tell=telln=0; |
|---|
| 102 | |
|---|
| 103 | for (iarg=4; iarg < argc; iarg++) { |
|---|
| 104 | p=argv[iarg]; |
|---|
| 105 | if (strcmp(p,"show") == 0 || strncmp(p,"show=",5) == 0) { |
|---|
| 106 | if (strncmp(p,"show=",5) == 0) { |
|---|
| 107 | if (!*(p+5)) fatal(p); |
|---|
| 108 | if ((fpx=fopen(p+5,"w")) == NULL) fatal(p+5); |
|---|
| 109 | } |
|---|
| 110 | else fpx=stdout; |
|---|
| 111 | show=1; |
|---|
| 112 | continue; |
|---|
| 113 | } |
|---|
| 114 | if (strcmp(dbnp,"rdonly") == 0) { |
|---|
| 115 | dbxopt_ordwr=O_RDONLY; |
|---|
| 116 | continue; |
|---|
| 117 | } |
|---|
| 118 | if (strcmp(p,"trace") == 0) { |
|---|
| 119 | dbxtrace=trmtrace=parmtrace=1; continue; |
|---|
| 120 | } |
|---|
| 121 | if (strncmp(p,"tell=",5) == 0) { |
|---|
| 122 | if (sscanf(p+5,"%ld",&tell) != 1) fatal(p); |
|---|
| 123 | if (tell < 0) fatal(p); |
|---|
| 124 | continue; |
|---|
| 125 | } |
|---|
| 126 | if (strncmp(p,"telln=",6) == 0) { |
|---|
| 127 | if (sscanf(p+6,"%ld",&telln) != 1) fatal(p); |
|---|
| 128 | if (telln < 0) fatal(p); |
|---|
| 129 | continue; |
|---|
| 130 | } |
|---|
| 131 | fatal(p); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | if (parmtrace) printf("+++ fmaxpos=%ld \n",punt2); |
|---|
| 135 | |
|---|
| 136 | if (tell) { |
|---|
| 137 | tell=rangtell/tell; |
|---|
| 138 | if (tell <= 0) tell=1; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | origlsiz = (treecase) ? L2BSIZ : L1BSIZ; |
|---|
| 142 | origsize=0; |
|---|
| 143 | lex=vlex[treecase]; |
|---|
| 144 | countell=0; |
|---|
| 145 | napus=0; |
|---|
| 146 | maxapu=offapu=0; |
|---|
| 147 | divapu=ALLOMAXV/sizeof(LONGX); |
|---|
| 148 | nextaddr=0; |
|---|
| 149 | |
|---|
| 150 | if (parmtrace) printf("+++ coreleft=%ld\n",CORELEFT()); |
|---|
| 151 | |
|---|
| 152 | fpccreat("",zdbnp,lx12extp[treecase],0); |
|---|
| 153 | |
|---|
| 154 | /* |
|---|
| 155 | for each leaf |
|---|
| 156 | */ |
|---|
| 157 | for (punt=punt1; punt <= punt2; punt++) { |
|---|
| 158 | |
|---|
| 159 | if (punt >= maxapu) { |
|---|
| 160 | if (napus >= MAXNAPU) fatal("MAXNAPU"); |
|---|
| 161 | apup=(LONGX *)ALLOC((ALLOPARM)ALLOMAXV); |
|---|
| 162 | if (apup == (LONGX *)ALLONULL) fatal("ALLOC/vapup"); |
|---|
| 163 | maxapu+=divapu; |
|---|
| 164 | if (napus) offapu+=divapu; |
|---|
| 165 | vapup[napus++]=apup; |
|---|
| 166 | } |
|---|
| 167 | apup[punt-offapu]=nextaddr+1; /* punt < 0 */ |
|---|
| 168 | |
|---|
| 169 | /* read */ |
|---|
| 170 | leafread((char *)&tlcleaf,invp,treecase,punt,0); |
|---|
| 171 | |
|---|
| 172 | /* setup header */ |
|---|
| 173 | lzxleaf.pos = tlcleaf.l0x.pos; |
|---|
| 174 | lzxleaf.ock = tlcleaf.l0x.ock; |
|---|
| 175 | lzxleaf.it = tlcleaf.l0x.it; |
|---|
| 176 | lzxleaf.ps = tlcleaf.l0x.ps; |
|---|
| 177 | if (punt == punt2 && lzxleaf.ps != 0) { |
|---|
| 178 | fatal("lzxleaf.ps/last"); |
|---|
| 179 | } |
|---|
| 180 | if (punt != punt2 && lzxleaf.ps != punt+1) { |
|---|
| 181 | fatal("lzxleaf.ps/next"); |
|---|
| 182 | } |
|---|
| 183 | #if LIND |
|---|
| 184 | lzxleaf.psb = tlcleaf.l0x.psb; |
|---|
| 185 | if (lzxleaf.psb != punt-1) { |
|---|
| 186 | fatal("lzxleaf.psb"); |
|---|
| 187 | } |
|---|
| 188 | #endif |
|---|
| 189 | |
|---|
| 190 | lzsize = LZHSIZE; |
|---|
| 191 | lzkp = (unsigned char *)lzxleaf.keys; |
|---|
| 192 | |
|---|
| 193 | /* |
|---|
| 194 | for each term |
|---|
| 195 | */ |
|---|
| 196 | for (iock=0; iock < tlcleaf.l0x.ock; iock++) { |
|---|
| 197 | |
|---|
| 198 | if (treecase) { |
|---|
| 199 | keyp = tlcleaf.l2x.idx[iock].key; |
|---|
| 200 | info1 = tlcleaf.l2x.idx[iock].info1; |
|---|
| 201 | info2 = tlcleaf.l2x.idx[iock].info2; |
|---|
| 202 | #if LIND |
|---|
| 203 | info3 = tlcleaf.l2x.idx[iock].info3info4.info3; |
|---|
| 204 | #endif |
|---|
| 205 | } |
|---|
| 206 | else { |
|---|
| 207 | keyp = tlcleaf.l1x.idx[iock].key; |
|---|
| 208 | info1 = tlcleaf.l1x.idx[iock].info1; |
|---|
| 209 | info2 = tlcleaf.l1x.idx[iock].info2; |
|---|
| 210 | #if LIND |
|---|
| 211 | info3 = tlcleaf.l1x.idx[iock].info3info4.info3; |
|---|
| 212 | #endif |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | /* move existing info */ |
|---|
| 216 | lzxleaf.info1[iock] = info1; /* yaddr */ |
|---|
| 217 | lzxleaf.info2[iock] = info2; |
|---|
| 218 | #if LIND |
|---|
| 219 | lzxleaf.info3[iock] = info3; |
|---|
| 220 | #endif |
|---|
| 221 | |
|---|
| 222 | /* compress existing key */ |
|---|
| 223 | for (keylen=lex, p=keyp+keylen-1; keylen; ) { |
|---|
| 224 | if (*p != ' ') break; |
|---|
| 225 | p--; keylen--; |
|---|
| 226 | } |
|---|
| 227 | if (!keylen) { |
|---|
| 228 | fprintf(stderr,"*** punt=%ld/%d keylen=%d",punt,iock,keylen); |
|---|
| 229 | fatal("keylen"); |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | if (iock == 0) eq=0; |
|---|
| 233 | else { |
|---|
| 234 | for (eq=0; eq < keylen; eq++) |
|---|
| 235 | if (keyp[eq] != prevkey[eq]) break; |
|---|
| 236 | } |
|---|
| 237 | memcpy(prevkey,keyp,lex); |
|---|
| 238 | |
|---|
| 239 | if (show) { |
|---|
| 240 | fprintf(fpx,"%3d|%3d|",eq,keylen); |
|---|
| 241 | for (p=keyp, n=keylen; n--; p++) fprintf(fpx,"%c",*p); |
|---|
| 242 | fprintf(fpx,"|"); |
|---|
| 243 | for (n=lex-keylen; n--; ) fprintf(fpx," "); |
|---|
| 244 | fprintf(fpx,"|\n"); |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | *lzkp++ = (unsigned char)eq; |
|---|
| 248 | *lzkp++ = (unsigned char)keylen; |
|---|
| 249 | |
|---|
| 250 | memcpy(lzkp,keyp+eq,ne=keylen-eq); |
|---|
| 251 | lzkp+=ne; |
|---|
| 252 | |
|---|
| 253 | lzsize+=2; /* eq+keylen */ |
|---|
| 254 | lzsize+=ne; |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | for (iock=tlcleaf.l0x.ock; iock < TWORDF; iock++) { |
|---|
| 258 | lzxleaf.info1[iock] = 0; |
|---|
| 259 | lzxleaf.info2[iock] = 0; |
|---|
| 260 | #if LIND |
|---|
| 261 | lzxleaf.info3[iock] = 0; |
|---|
| 262 | #endif |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | #if LIND |
|---|
| 266 | thisaddr=nextaddr; |
|---|
| 267 | if (punt == punt1) lastaddr=0; |
|---|
| 268 | #endif |
|---|
| 269 | nextaddr+=lzsize; |
|---|
| 270 | origsize+=origlsiz; |
|---|
| 271 | |
|---|
| 272 | filler=0; |
|---|
| 273 | if (punt == punt2) { |
|---|
| 274 | n = (treecase) ? sizeof(LZSTRU) : LZHSIZE + LZKASZ1; |
|---|
| 275 | filler = n-lzsize; |
|---|
| 276 | p=(char *)&lzxleaf; memset(p+lzsize,0x00,filler); |
|---|
| 277 | lzsize+=filler; |
|---|
| 278 | nextaddr+=filler; |
|---|
| 279 | filler = n; |
|---|
| 280 | if (nextaddr % filler) { |
|---|
| 281 | filler = filler - nextaddr % filler; |
|---|
| 282 | nextaddr+=filler; |
|---|
| 283 | } else filler=0; |
|---|
| 284 | } |
|---|
| 285 | |
|---|
| 286 | /* fixup ps/psb header */ |
|---|
| 287 | lzxleaf.ps = (punt == punt2) ? 0L : nextaddr+1; /* punt < 0 */ |
|---|
| 288 | #if LIND |
|---|
| 289 | lzxleaf.psb = (punt == punt1) ? 0L : lastaddr+1; /* punt < 0 */ |
|---|
| 290 | lastaddr=thisaddr; |
|---|
| 291 | #endif |
|---|
| 292 | |
|---|
| 293 | fpcwrite((char *)&lzxleaf,lzsize); |
|---|
| 294 | |
|---|
| 295 | ++countell; |
|---|
| 296 | if (tell) |
|---|
| 297 | if (countell % tell == 0 || filler) { |
|---|
| 298 | fprintf(fpx,"+%5ldp %3ld%% ",countell, |
|---|
| 299 | (10 * (countell*100/rangtell) + 5) / 10); |
|---|
| 300 | fprintf(fpx,"orig=%7ld [%3d] ",origsize,origlsiz); |
|---|
| 301 | fprintf(fpx,"this=%7ld [%3d] ",nextaddr,lzsize); |
|---|
| 302 | fprintf(fpx,"save=%7ld (%2d%%)\n",origsize-nextaddr, |
|---|
| 303 | (10 * ((origsize-nextaddr)*100/origsize) + 5) / 10); |
|---|
| 304 | } |
|---|
| 305 | } |
|---|
| 306 | |
|---|
| 307 | if (filler) { |
|---|
| 308 | memset((char *)&lzxleaf,0x00,filler); |
|---|
| 309 | fpcwrite((char *)&lzxleaf,filler); |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | fpcclose(); |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | /* |
|---|
| 316 | rewrite .n0x |
|---|
| 317 | (pointing to the absolute leaf address) |
|---|
| 318 | */ |
|---|
| 319 | |
|---|
| 320 | punt1=1; punt2=invp->cn[treecase].nmaxpos; |
|---|
| 321 | rangtelln=punt2-punt1+1; |
|---|
| 322 | if (rangtelln <= 0) rangtelln=1; |
|---|
| 323 | countelln=0; |
|---|
| 324 | |
|---|
| 325 | if (parmtrace) printf("+++ nmaxpos=%ld \n",punt2); |
|---|
| 326 | |
|---|
| 327 | if (telln) { |
|---|
| 328 | telln=rangtelln/telln; |
|---|
| 329 | if (telln <= 0) telln=1; |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | fpccreat("",zdbnp,nx12extp[treecase],0); |
|---|
| 333 | nodesize=(treecase)?N2BSIZ:N1BSIZ; |
|---|
| 334 | |
|---|
| 335 | /* for each node */ |
|---|
| 336 | |
|---|
| 337 | for (punt=punt1; punt <= punt2; punt++) { |
|---|
| 338 | |
|---|
| 339 | /* read */ |
|---|
| 340 | n0p=noderead(invp,treecase,0,punt); |
|---|
| 341 | |
|---|
| 342 | /* update leaf punt's */ |
|---|
| 343 | n=n0p->ock; |
|---|
| 344 | if (treecase) |
|---|
| 345 | for (n2xp=((N2STRU *)n0p)->idx; n--; n2xp++) { |
|---|
| 346 | if (n2xp->punt < 0) |
|---|
| 347 | n2xp->punt = -leafaddr(punt,-n2xp->punt); |
|---|
| 348 | } |
|---|
| 349 | else |
|---|
| 350 | for (n1xp=((N1STRU *)n0p)->idx; n--; n1xp++) { |
|---|
| 351 | if (n1xp->punt < 0) |
|---|
| 352 | n1xp->punt = -leafaddr(punt,-n1xp->punt); |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| 355 | /* write */ |
|---|
| 356 | fpcwrite((char *)n0p,nodesize); |
|---|
| 357 | |
|---|
| 358 | ++countelln; |
|---|
| 359 | if (telln) |
|---|
| 360 | if (countelln % telln == 0) { |
|---|
| 361 | fprintf(fpx,"+%5ldp %3ld%% ",countelln, |
|---|
| 362 | (10 * (countelln*100/rangtelln) + 5) / 10); |
|---|
| 363 | fprintf(fpx,"orig=%7ld [%3d] \n",punt*nodesize,nodesize); |
|---|
| 364 | } |
|---|
| 365 | } |
|---|
| 366 | |
|---|
| 367 | fpcclose(); |
|---|
| 368 | |
|---|
| 369 | |
|---|
| 370 | /* |
|---|
| 371 | rewrite .cnt |
|---|
| 372 | */ |
|---|
| 373 | |
|---|
| 374 | if (parmtrace) printf("+++ ordf=%d \n",invp->cn[0].ordf); |
|---|
| 375 | |
|---|
| 376 | fpccreat("",zdbnp,cx1extp,0); |
|---|
| 377 | |
|---|
| 378 | invp->cn[0].ordf++; |
|---|
| 379 | fpcwrite((char *)&(invp->cn[0]),CNBSIZ); |
|---|
| 380 | fpcwrite((char *)&(invp->cn[1]),CNBSIZ); |
|---|
| 381 | |
|---|
| 382 | fpcclose(); |
|---|
| 383 | |
|---|
| 384 | |
|---|
| 385 | exit(0); |
|---|
| 386 | } |
|---|
| 387 | |
|---|
| 388 | |
|---|
| 389 | LONGX leafaddr(pos,punt) |
|---|
| 390 | PUNT pos; |
|---|
| 391 | PUNT punt; |
|---|
| 392 | { |
|---|
| 393 | int n,k; |
|---|
| 394 | |
|---|
| 395 | if (punt < 1 || punt >= maxapu) { |
|---|
| 396 | fprintf(stderr,"*** nodepos=%ld leafpunt=%ld \n",pos,punt); |
|---|
| 397 | fatal("leafaddr/punt"); |
|---|
| 398 | } |
|---|
| 399 | |
|---|
| 400 | n=punt/divapu; |
|---|
| 401 | k=punt%divapu; |
|---|
| 402 | |
|---|
| 403 | apup=vapup[n]; |
|---|
| 404 | |
|---|
| 405 | return(apup[k]); |
|---|
| 406 | } |
|---|