1*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 2*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 3*7c478bd9Sstevel@tonic-gate 4*7c478bd9Sstevel@tonic-gate 5*7c478bd9Sstevel@tonic-gate /* 6*7c478bd9Sstevel@tonic-gate * Copyright (c) 1980 Regents of the University of California. 7*7c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement 8*7c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution. 9*7c478bd9Sstevel@tonic-gate */ 10*7c478bd9Sstevel@tonic-gate 11*7c478bd9Sstevel@tonic-gate /* 12*7c478bd9Sstevel@tonic-gate * Copyright (c) 1983, 1984 1985, 1986, 1987, 1988, Sun Microsystems, Inc. 13*7c478bd9Sstevel@tonic-gate * All Rights Reserved. 14*7c478bd9Sstevel@tonic-gate */ 15*7c478bd9Sstevel@tonic-gate 16*7c478bd9Sstevel@tonic-gate 17*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 18*7c478bd9Sstevel@tonic-gate 19*7c478bd9Sstevel@tonic-gate #include <stdio.h> 20*7c478bd9Sstevel@tonic-gate #define unopen(fil) {if (fil!=NULL) {fclose(fil); fil=NULL;}} 21*7c478bd9Sstevel@tonic-gate 22*7c478bd9Sstevel@tonic-gate extern char refdir[]; 23*7c478bd9Sstevel@tonic-gate int lmaster = 1000; 24*7c478bd9Sstevel@tonic-gate int reached = 0; 25*7c478bd9Sstevel@tonic-gate FILE *fd = 0; 26*7c478bd9Sstevel@tonic-gate int *hfreq, hfrflg; 27*7c478bd9Sstevel@tonic-gate int colevel = 0; 28*7c478bd9Sstevel@tonic-gate static union firetruck { 29*7c478bd9Sstevel@tonic-gate unsigned *a; 30*7c478bd9Sstevel@tonic-gate long *b; 31*7c478bd9Sstevel@tonic-gate } master; 32*7c478bd9Sstevel@tonic-gate int iflong; 33*7c478bd9Sstevel@tonic-gate extern char *fgnames[], **fgnamp; 34*7c478bd9Sstevel@tonic-gate extern FILE *iopen(); 35*7c478bd9Sstevel@tonic-gate char *todir(); 36*7c478bd9Sstevel@tonic-gate int prfreqs = 0; 37*7c478bd9Sstevel@tonic-gate int typeindex = 0; 38*7c478bd9Sstevel@tonic-gate char usedir[100]; 39*7c478bd9Sstevel@tonic-gate static int full = 1000; 40*7c478bd9Sstevel@tonic-gate static int tags = 0; 41*7c478bd9Sstevel@tonic-gate char *sinput, *soutput, *tagout; 42*7c478bd9Sstevel@tonic-gate long indexdate = 0, gdate(); 43*7c478bd9Sstevel@tonic-gate int soutlen = 1000; 44*7c478bd9Sstevel@tonic-gate int taglen = 1000; 45*7c478bd9Sstevel@tonic-gate 46*7c478bd9Sstevel@tonic-gate huntmain(argc,argv) 47*7c478bd9Sstevel@tonic-gate char *argv[]; 48*7c478bd9Sstevel@tonic-gate { 49*7c478bd9Sstevel@tonic-gate /* read query from stdin, expect name of indexes in argv[1] */ 50*7c478bd9Sstevel@tonic-gate static FILE *fa, *fb, *fc; 51*7c478bd9Sstevel@tonic-gate char indexname[100], *qitem[100], *rprog = 0; 52*7c478bd9Sstevel@tonic-gate char grepquery[200]; 53*7c478bd9Sstevel@tonic-gate static char oldname[30] ; 54*7c478bd9Sstevel@tonic-gate static int nhash = 0; 55*7c478bd9Sstevel@tonic-gate static int maxhash = 0; 56*7c478bd9Sstevel@tonic-gate int falseflg = 0, nitem, nfound, frtbl; 57*7c478bd9Sstevel@tonic-gate static long *hpt = 0; 58*7c478bd9Sstevel@tonic-gate unsigned *masterp; 59*7c478bd9Sstevel@tonic-gate 60*7c478bd9Sstevel@tonic-gate # if D1 61*7c478bd9Sstevel@tonic-gate fprintf(stderr, "in glue1 argc %d argv %o %o\n", argc, argv[0],argv[1]); 62*7c478bd9Sstevel@tonic-gate # endif 63*7c478bd9Sstevel@tonic-gate savedir(); 64*7c478bd9Sstevel@tonic-gate while (argv[1][0] == '-') 65*7c478bd9Sstevel@tonic-gate { 66*7c478bd9Sstevel@tonic-gate # if D1 67*7c478bd9Sstevel@tonic-gate fprintf(stderr, "argv.1 is %s\n",argv[1]); 68*7c478bd9Sstevel@tonic-gate # endif 69*7c478bd9Sstevel@tonic-gate switch(argv[1][1]) 70*7c478bd9Sstevel@tonic-gate { 71*7c478bd9Sstevel@tonic-gate case 'a': /* all output, incl. false drops */ 72*7c478bd9Sstevel@tonic-gate falseflg = 1; 73*7c478bd9Sstevel@tonic-gate break; 74*7c478bd9Sstevel@tonic-gate case 'r': 75*7c478bd9Sstevel@tonic-gate argc--; 76*7c478bd9Sstevel@tonic-gate argv++; 77*7c478bd9Sstevel@tonic-gate rprog = argv[1]; 78*7c478bd9Sstevel@tonic-gate break; 79*7c478bd9Sstevel@tonic-gate case 'F': /* put out full text */ 80*7c478bd9Sstevel@tonic-gate full = setfrom(argv[1][2]); 81*7c478bd9Sstevel@tonic-gate break; 82*7c478bd9Sstevel@tonic-gate case 'T': /* put out tags */ 83*7c478bd9Sstevel@tonic-gate tags = setfrom(argv[1][2]); 84*7c478bd9Sstevel@tonic-gate break; 85*7c478bd9Sstevel@tonic-gate case 'i': /* input in argument string */ 86*7c478bd9Sstevel@tonic-gate argc--; 87*7c478bd9Sstevel@tonic-gate argv++; 88*7c478bd9Sstevel@tonic-gate sinput = argv[1]; 89*7c478bd9Sstevel@tonic-gate break; 90*7c478bd9Sstevel@tonic-gate case 's': /*text output to string */ 91*7c478bd9Sstevel@tonic-gate case 'o': 92*7c478bd9Sstevel@tonic-gate argc--; 93*7c478bd9Sstevel@tonic-gate argv++; 94*7c478bd9Sstevel@tonic-gate soutput = argv[1]; 95*7c478bd9Sstevel@tonic-gate if ((int) argv[2]<16000) 96*7c478bd9Sstevel@tonic-gate { 97*7c478bd9Sstevel@tonic-gate soutlen = (int) argv[2]; 98*7c478bd9Sstevel@tonic-gate argc--; 99*7c478bd9Sstevel@tonic-gate argv++; 100*7c478bd9Sstevel@tonic-gate } 101*7c478bd9Sstevel@tonic-gate break; 102*7c478bd9Sstevel@tonic-gate case 't': /*tag output to string */ 103*7c478bd9Sstevel@tonic-gate argc--; 104*7c478bd9Sstevel@tonic-gate argv++; 105*7c478bd9Sstevel@tonic-gate tagout = argv[1]; 106*7c478bd9Sstevel@tonic-gate if ((int)argv[2]<16000) 107*7c478bd9Sstevel@tonic-gate { 108*7c478bd9Sstevel@tonic-gate taglen = (int)argv[2]; 109*7c478bd9Sstevel@tonic-gate argc--; 110*7c478bd9Sstevel@tonic-gate argv++; 111*7c478bd9Sstevel@tonic-gate } 112*7c478bd9Sstevel@tonic-gate break; 113*7c478bd9Sstevel@tonic-gate case 'l': /* specify length of lists */ 114*7c478bd9Sstevel@tonic-gate argc--; 115*7c478bd9Sstevel@tonic-gate argv++; 116*7c478bd9Sstevel@tonic-gate lmaster = atoi(argv[1]); 117*7c478bd9Sstevel@tonic-gate # if D1 118*7c478bd9Sstevel@tonic-gate fprintf(stderr, "lmaster now %d\n",lmaster); 119*7c478bd9Sstevel@tonic-gate # endif 120*7c478bd9Sstevel@tonic-gate break; 121*7c478bd9Sstevel@tonic-gate case 'C': 122*7c478bd9Sstevel@tonic-gate argc--; 123*7c478bd9Sstevel@tonic-gate argv++; 124*7c478bd9Sstevel@tonic-gate colevel = atoi(argv[1]); 125*7c478bd9Sstevel@tonic-gate break; 126*7c478bd9Sstevel@tonic-gate } 127*7c478bd9Sstevel@tonic-gate argc--; 128*7c478bd9Sstevel@tonic-gate argv++; 129*7c478bd9Sstevel@tonic-gate } 130*7c478bd9Sstevel@tonic-gate strcpy (indexname, todir(argv[1])); 131*7c478bd9Sstevel@tonic-gate # if D1 132*7c478bd9Sstevel@tonic-gate fprintf(stderr, "in huntmain indexname %s typeindex %d\n", indexname, typeindex); 133*7c478bd9Sstevel@tonic-gate # endif 134*7c478bd9Sstevel@tonic-gate if (typeindex == 0 || strcmp (oldname, indexname) !=0) 135*7c478bd9Sstevel@tonic-gate { 136*7c478bd9Sstevel@tonic-gate strcpy (oldname, indexname); 137*7c478bd9Sstevel@tonic-gate unopen(fa); 138*7c478bd9Sstevel@tonic-gate unopen(fb); 139*7c478bd9Sstevel@tonic-gate unopen(fc); 140*7c478bd9Sstevel@tonic-gate 141*7c478bd9Sstevel@tonic-gate if (ckexist(indexname, ".ib")) 142*7c478bd9Sstevel@tonic-gate { 143*7c478bd9Sstevel@tonic-gate # if D1 144*7c478bd9Sstevel@tonic-gate fprintf(stderr, "found old index\n"); 145*7c478bd9Sstevel@tonic-gate # endif 146*7c478bd9Sstevel@tonic-gate fa = iopen(indexname, ".ia"); 147*7c478bd9Sstevel@tonic-gate fb = iopen(indexname, ".ib"); 148*7c478bd9Sstevel@tonic-gate fc = iopen(indexname, ".ic"); 149*7c478bd9Sstevel@tonic-gate typeindex =1; 150*7c478bd9Sstevel@tonic-gate # if D1 151*7c478bd9Sstevel@tonic-gate fprintf(stderr, "opened f's as %o %o %o\n",fa,fb,fc); 152*7c478bd9Sstevel@tonic-gate # endif 153*7c478bd9Sstevel@tonic-gate indexdate = gdate(fb); 154*7c478bd9Sstevel@tonic-gate fread (&nhash, sizeof(nhash), 1, fa); 155*7c478bd9Sstevel@tonic-gate fread (&iflong, sizeof(iflong), 1, fa); 156*7c478bd9Sstevel@tonic-gate if (nhash > maxhash) 157*7c478bd9Sstevel@tonic-gate { 158*7c478bd9Sstevel@tonic-gate if (hpt) 159*7c478bd9Sstevel@tonic-gate free (hpt, maxhash, sizeof(*hpt)); 160*7c478bd9Sstevel@tonic-gate hpt=0; 161*7c478bd9Sstevel@tonic-gate if (hfreq) 162*7c478bd9Sstevel@tonic-gate free(hfreq, maxhash, sizeof(*hfreq)); 163*7c478bd9Sstevel@tonic-gate hfreq=0; 164*7c478bd9Sstevel@tonic-gate maxhash=nhash; 165*7c478bd9Sstevel@tonic-gate # if D1 166*7c478bd9Sstevel@tonic-gate fprintf(stderr, "Freed if needed maxhash %d\n",maxhash); 167*7c478bd9Sstevel@tonic-gate # endif 168*7c478bd9Sstevel@tonic-gate } 169*7c478bd9Sstevel@tonic-gate if (hpt==0) 170*7c478bd9Sstevel@tonic-gate hpt = (long *) zalloc(nhash, sizeof(*hpt)); 171*7c478bd9Sstevel@tonic-gate # if D1 172*7c478bd9Sstevel@tonic-gate fprintf(stderr, "hpt now %o\n",hpt); 173*7c478bd9Sstevel@tonic-gate # endif 174*7c478bd9Sstevel@tonic-gate if (hpt == NULL) 175*7c478bd9Sstevel@tonic-gate /* 176*7c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE 177*7c478bd9Sstevel@tonic-gate * %d is the size of the hash table - not 178*7c478bd9Sstevel@tonic-gate * very interesting info for the end users. 179*7c478bd9Sstevel@tonic-gate * Hash is a computer science terminology. 180*7c478bd9Sstevel@tonic-gate */ 181*7c478bd9Sstevel@tonic-gate err(gettext("No space for hash list (%d)"), 182*7c478bd9Sstevel@tonic-gate nhash); 183*7c478bd9Sstevel@tonic-gate fread( hpt, sizeof(*hpt), nhash, fa); 184*7c478bd9Sstevel@tonic-gate if (hfreq==0) 185*7c478bd9Sstevel@tonic-gate hfreq=(int *)zalloc(nhash, sizeof(*hfreq)); 186*7c478bd9Sstevel@tonic-gate if (hfreq==NULL) 187*7c478bd9Sstevel@tonic-gate /* 188*7c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE 189*7c478bd9Sstevel@tonic-gate * %d is the size of the hash table. 190*7c478bd9Sstevel@tonic-gate */ 191*7c478bd9Sstevel@tonic-gate err(gettext( 192*7c478bd9Sstevel@tonic-gate "No space for hash frequencies (%d)"), 193*7c478bd9Sstevel@tonic-gate nhash); 194*7c478bd9Sstevel@tonic-gate frtbl = fread(hfreq, sizeof(*hfreq), nhash, fa); 195*7c478bd9Sstevel@tonic-gate hfrflg = (frtbl == nhash); 196*7c478bd9Sstevel@tonic-gate # if D1 197*7c478bd9Sstevel@tonic-gate fprintf(stderr,"Read pointer files\n"); 198*7c478bd9Sstevel@tonic-gate # endif 199*7c478bd9Sstevel@tonic-gate if (master.a == NULL) 200*7c478bd9Sstevel@tonic-gate if (iflong) 201*7c478bd9Sstevel@tonic-gate master.b = (long *)zalloc(lmaster, sizeof(long)); 202*7c478bd9Sstevel@tonic-gate else 203*7c478bd9Sstevel@tonic-gate master.a = (unsigned *)zalloc(lmaster, sizeof(int)); 204*7c478bd9Sstevel@tonic-gate if (master.a == NULL) 205*7c478bd9Sstevel@tonic-gate err(gettext("no space for answer list"), 0); 206*7c478bd9Sstevel@tonic-gate } 207*7c478bd9Sstevel@tonic-gate else 208*7c478bd9Sstevel@tonic-gate if (makefgrep(indexname)) 209*7c478bd9Sstevel@tonic-gate typeindex=2; 210*7c478bd9Sstevel@tonic-gate else 211*7c478bd9Sstevel@tonic-gate { 212*7c478bd9Sstevel@tonic-gate err(gettext("No files %s\n"), indexname); 213*7c478bd9Sstevel@tonic-gate exit(1); 214*7c478bd9Sstevel@tonic-gate } 215*7c478bd9Sstevel@tonic-gate } 216*7c478bd9Sstevel@tonic-gate 217*7c478bd9Sstevel@tonic-gate if (iflong) 218*7c478bd9Sstevel@tonic-gate masterp = (unsigned *) master.b; 219*7c478bd9Sstevel@tonic-gate else 220*7c478bd9Sstevel@tonic-gate masterp = master.a; 221*7c478bd9Sstevel@tonic-gate 222*7c478bd9Sstevel@tonic-gate # if D1 223*7c478bd9Sstevel@tonic-gate fprintf(stderr, "typeindex now %d\n",typeindex); 224*7c478bd9Sstevel@tonic-gate # endif 225*7c478bd9Sstevel@tonic-gate tagout[0]=0; 226*7c478bd9Sstevel@tonic-gate if (typeindex==2) 227*7c478bd9Sstevel@tonic-gate { 228*7c478bd9Sstevel@tonic-gate grepcall(sinput, tagout, indexname); 229*7c478bd9Sstevel@tonic-gate # if D1 230*7c478bd9Sstevel@tonic-gate fprintf(stderr, " back from grepcall\n"); 231*7c478bd9Sstevel@tonic-gate # endif 232*7c478bd9Sstevel@tonic-gate restodir(); 233*7c478bd9Sstevel@tonic-gate return; 234*7c478bd9Sstevel@tonic-gate } 235*7c478bd9Sstevel@tonic-gate nitem = getq(qitem); 236*7c478bd9Sstevel@tonic-gate # if D1 237*7c478bd9Sstevel@tonic-gate fprintf(stderr, "approaching doquery fb %o\n", fb); 238*7c478bd9Sstevel@tonic-gate # endif 239*7c478bd9Sstevel@tonic-gate nfound = doquery(hpt, nhash, fb, nitem, qitem, masterp); 240*7c478bd9Sstevel@tonic-gate # ifdef D1 241*7c478bd9Sstevel@tonic-gate fprintf(stderr, "return from doquery with nfound %d\n", nfound); 242*7c478bd9Sstevel@tonic-gate # endif 243*7c478bd9Sstevel@tonic-gate if (falseflg == 0) 244*7c478bd9Sstevel@tonic-gate nfound = baddrop(masterp, nfound, fc, nitem, qitem, rprog, full); 245*7c478bd9Sstevel@tonic-gate # ifdef D1 246*7c478bd9Sstevel@tonic-gate fprintf(stderr, "after baddrop with nfound %d\n",nfound); 247*7c478bd9Sstevel@tonic-gate fprintf(stderr, "tagout is /%s/, sout /%s/\n",tagout, soutput); 248*7c478bd9Sstevel@tonic-gate # endif 249*7c478bd9Sstevel@tonic-gate if (tags) 250*7c478bd9Sstevel@tonic-gate result (masterp, nfound >tags ? tags : nfound, fc); 251*7c478bd9Sstevel@tonic-gate # if D1 252*7c478bd9Sstevel@tonic-gate fprintf(stderr, "done with huntmain\n"); 253*7c478bd9Sstevel@tonic-gate fprintf(stderr, "tagout is /%s/\n", tagout); 254*7c478bd9Sstevel@tonic-gate fprintf(stderr, "string out is /%s/\n", soutput); 255*7c478bd9Sstevel@tonic-gate # endif 256*7c478bd9Sstevel@tonic-gate if (fgnamp>fgnames) 257*7c478bd9Sstevel@tonic-gate { 258*7c478bd9Sstevel@tonic-gate char **fgp; 259*7c478bd9Sstevel@tonic-gate int k; 260*7c478bd9Sstevel@tonic-gate # if D1 261*7c478bd9Sstevel@tonic-gate fprintf(stderr, "were %d bad files\n", fgnamp-fgnames); 262*7c478bd9Sstevel@tonic-gate # endif 263*7c478bd9Sstevel@tonic-gate grepquery[0]=0; 264*7c478bd9Sstevel@tonic-gate for(k=0; k<nitem; k++) 265*7c478bd9Sstevel@tonic-gate { 266*7c478bd9Sstevel@tonic-gate strcat(grepquery, " "); 267*7c478bd9Sstevel@tonic-gate strcat(grepquery, qitem[k]); 268*7c478bd9Sstevel@tonic-gate } 269*7c478bd9Sstevel@tonic-gate for(fgp=fgnames; fgp<fgnamp; fgp++) 270*7c478bd9Sstevel@tonic-gate { 271*7c478bd9Sstevel@tonic-gate # if D1 272*7c478bd9Sstevel@tonic-gate fprintf(stderr, "Now on %s query /%s/\n", *fgp, grepquery); 273*7c478bd9Sstevel@tonic-gate # endif 274*7c478bd9Sstevel@tonic-gate makefgrep(*fgp); 275*7c478bd9Sstevel@tonic-gate grepcall(grepquery, tagout, *fgp); 276*7c478bd9Sstevel@tonic-gate # if D1 277*7c478bd9Sstevel@tonic-gate fprintf(stderr, "tagout now /%s/\n", tagout); 278*7c478bd9Sstevel@tonic-gate # endif 279*7c478bd9Sstevel@tonic-gate } 280*7c478bd9Sstevel@tonic-gate } 281*7c478bd9Sstevel@tonic-gate restodir(); 282*7c478bd9Sstevel@tonic-gate } 283*7c478bd9Sstevel@tonic-gate 284*7c478bd9Sstevel@tonic-gate char * 285*7c478bd9Sstevel@tonic-gate todir(t) 286*7c478bd9Sstevel@tonic-gate char *t; 287*7c478bd9Sstevel@tonic-gate { 288*7c478bd9Sstevel@tonic-gate char *s; 289*7c478bd9Sstevel@tonic-gate 290*7c478bd9Sstevel@tonic-gate usedir[0] = 0; 291*7c478bd9Sstevel@tonic-gate s=t; 292*7c478bd9Sstevel@tonic-gate while (*s) s++; 293*7c478bd9Sstevel@tonic-gate while (s>=t && *s != '/') s--; 294*7c478bd9Sstevel@tonic-gate if (s<t) return(t); 295*7c478bd9Sstevel@tonic-gate *s++ = 0; 296*7c478bd9Sstevel@tonic-gate t = (*t ? t : "/"); 297*7c478bd9Sstevel@tonic-gate chdir (t); 298*7c478bd9Sstevel@tonic-gate strcpy (usedir,t); 299*7c478bd9Sstevel@tonic-gate return(s); 300*7c478bd9Sstevel@tonic-gate } 301*7c478bd9Sstevel@tonic-gate 302*7c478bd9Sstevel@tonic-gate setfrom(c) 303*7c478bd9Sstevel@tonic-gate { 304*7c478bd9Sstevel@tonic-gate switch(c) 305*7c478bd9Sstevel@tonic-gate { 306*7c478bd9Sstevel@tonic-gate case 'y': 307*7c478bd9Sstevel@tonic-gate case '\0': 308*7c478bd9Sstevel@tonic-gate default: 309*7c478bd9Sstevel@tonic-gate return(1000); 310*7c478bd9Sstevel@tonic-gate case '1': 311*7c478bd9Sstevel@tonic-gate case '2': 312*7c478bd9Sstevel@tonic-gate case '3': 313*7c478bd9Sstevel@tonic-gate case '4': 314*7c478bd9Sstevel@tonic-gate case '5': 315*7c478bd9Sstevel@tonic-gate case '6': 316*7c478bd9Sstevel@tonic-gate case '7': 317*7c478bd9Sstevel@tonic-gate case '8': 318*7c478bd9Sstevel@tonic-gate case '9': 319*7c478bd9Sstevel@tonic-gate return(c-'0'); 320*7c478bd9Sstevel@tonic-gate case 'n': 321*7c478bd9Sstevel@tonic-gate case '0': 322*7c478bd9Sstevel@tonic-gate return(0); 323*7c478bd9Sstevel@tonic-gate } 324*7c478bd9Sstevel@tonic-gate } 325