int main(int argc,char **argv) { /* read query from stdin, expect name of indexes in argv[1] */ static FILE *fa, *fb, *fc; char nma[PATH_MAX], nmb[PATH_MAX], nmc[PATH_MAX], *qitem[100], *rprog = NULL; char nmd[PATH_MAX], grepquery[256]; static char oldname[30] ; static int was =0; /* these pointers are unions of pointer to int and pointer to long */ long *hpt = 0; unsigned *master =0; int falseflg, nhash, nitem, nfound = 0, frtbl, kk; /* special wart for refpart: default is tags only */ falseflg = 0; while (argc > 1 && argv[1][0] == '-') { switch(argv[1][1]) { case 'a': /* all output, incl. false drops */ falseflg = 1; break; case 'r': argc--; argv++; rprog = argv[1]; break; case 'F': /* put out full text */ full = setfrom(argv[1][2]); break; case 'T': /* put out tags */ tags = setfrom(argv[1][2]); break; case 'i': /* input in argument string */ argc--; argv++; sinput = argv[1]; break; case 's': /*text output to string */ case 'o': argc--; argv++; soutput = argv[1]; if ((intptr_t) argv[2]<16000) { soutlen = (intptr_t)argv[2]; argc--; argv++; } break; case 't': /*tag output to string */ argc--; argv++; tagout = argv[1]; break; case 'l': /* length of internal lists */ argc--; argv++; lmaster = atoi(argv[1]); break; case 'g': /* suppress fgrep search on old files */ keepold = 0; break; case 'C': /* coordination level */ colevel = atoi(argv[1]+2); # if D1 fprintf(stderr, "colevel set to %d\n",colevel); # endif break; case 'P': /* print term freqs */ prfreqs=1; break; case 'm': measure=1; break; } argc--; argv++; } if(argc < 2) exit(1); strcpy (nma, todir(argv[1])); if (was == 0 || strcmp (oldname, nma) !=0) { strcpy (oldname,nma); strcpy (nmb, nma); strcpy (nmc, nmb); strcpy(nmd,nma); strcat (nma, ".ia"); strcat (nmb, ".ib"); strcat (nmc, ".ic"); strcat (nmd, ".id"); if (was) { fclose(fa); fclose(fb); fclose(fc); } fa = fopen(nma, "r"); if (fa==NULL) { strcpy(*fgnamp++ = calloc(strlen(oldname)+2,1), oldname); fb=NULL; goto search; } fb = fopen(nmb, "r"); fc = fopen(nmc, "r"); was =1; if (fb== NULL || fc ==NULL) { err("Index incomplete %s", nmb); exit(1); } indexdate = gdate(fb); fd = fopen(nmd, "r"); } fseek (fa, 0, SEEK_SET); fread (&nhash, sizeof(nhash), 1, fa); fread (&iflong, sizeof(iflong), 1, fa); if(master==0) master = calloc (lmaster, iflong? sizeof(long): sizeof(unsigned)); hpt = calloc(nhash, sizeof(*hpt)); kk=fread( hpt, sizeof(*hpt), nhash, fa); # if D1 fprintf(stderr,"read %d hashes, iflong %d, nhash %d\n", kk, iflong, nhash); # endif assert (kk==nhash); hfreq = calloc(nhash, sizeof(*hfreq)); assert (hfreq != NULL); frtbl = fread(hfreq, sizeof(*hfreq), nhash, fa); hfrflg = (frtbl == nhash); # if D1 fprintf(stderr, "read freqs %d\n", frtbl); # endif search: while (1) { nitem = getq(qitem); if (measure) tick(); if (nitem==0) continue; if (nitem < 0) break; if (tagout) tagout[0]=0; if (fb!=NULL) { nfound = doquery(hpt, nhash, fb, nitem, qitem, master); # if D1 fprintf(stderr,"after doquery nfound %d\n", nfound); # endif fgnamp=fgnames; if (falseflg == 0) nfound = baddrop(master, nfound, fc, nitem, qitem, rprog, full); # if D1 fprintf(stderr,"after baddrop nfound %d\n", nfound); # endif } if (fgnamp>fgnames) { char **fgp, tgbuff[100]; int k; # if D1 fprintf(stderr, "were %d bad files\n", fgnamp-fgnames); # endif memset(tgbuff, 0, sizeof (tgbuff)); grepquery[0]=0; for(k=0; k<nitem; k++) { strcat(grepquery, " "); strcat(grepquery, qitem[k]); } # if D1 fprintf(stderr, "grepquery %s\n",grepquery); # endif for(fgp=fgnames; fgp<fgnamp; fgp++) { # if D1 fprintf(stderr, "Now on %s query /%s/\n", *fgp, grepquery); # endif makefgrep(*fgp); # if D1 fprintf(stderr, "grepmade\n"); # endif if (tagout==0) tagout=tgbuff; grepcall(grepquery, tagout, *fgp); # if D1 fprintf(stderr, "tagout now /%s/\n", tagout); # endif if (full) { int nout; char *bout; char *tagp; char *oldtagp; tagp = tagout; while (*tagp) { oldtagp = tagp; while (*tagp && (*tagp != '\n')) tagp++; if (*tagp) tagp++; nout = findline(oldtagp, &bout, 1000, 0L); if (nout > 0) { fputs(bout, stdout); free(bout); } } } } } if (tags) result (master, nfound >tags ? tags: nfound, fc); if (measure) tock(); } /* NOTREACHED */ return 0; }
void huntmain(int argc,char **argv) { /* read query from stdin, expect name of indexes in argv[1] */ static FILE *fa, *fb, *fc; char indexname[PATH_MAX], *qitem[100], *rprog = 0; char grepquery[200]; static char oldname[30] ; static int nhash = 0; static int maxhash = 0; int falseflg = 0, nitem, nfound, frtbl; static long *hpt = 0; unsigned *masterp; # if D1 fprintf(stderr, "in glue1 argc %d argv %o %o\n", argc, argv[0],argv[1]); # endif savedir(); while (argv[1][0] == '-') { # if D1 fprintf(stderr, "argv.1 is %s\n",argv[1]); # endif switch(argv[1][1]) { case 'a': /* all output, incl. false drops */ falseflg = 1; break; case 'r': argc--; argv++; rprog = argv[1]; break; case 'F': /* put out full text */ full = setfrom(argv[1][2]); break; case 'T': /* put out tags */ tags = setfrom(argv[1][2]); break; case 'i': /* input in argument string */ argc--; argv++; sinput = argv[1]; break; case 's': /*text output to string */ case 'o': argc--; argv++; soutput = argv[1]; if ((intptr_t) argv[2]<16000) { soutlen = (intptr_t) argv[2]; argc--; argv++; } break; case 't': /*tag output to string */ argc--; argv++; tagout = argv[1]; if ((intptr_t)argv[2]<16000) { taglen = (intptr_t)argv[2]; argc--; argv++; } break; case 'l': /* specify length of lists */ argc--; argv++; lmaster = atoi(argv[1]); # if D1 fprintf(stderr, "lmaster now %d\n",lmaster); # endif break; case 'C': argc--; argv++; colevel = atoi(argv[1]); break; } argc--; argv++; } n_strcpy (indexname, todir(argv[1]), sizeof(indexname)); # if D1 fprintf(stderr, "in huntmain indexname %s typeindex %d\n", indexname, typeindex); # endif if (typeindex == 0 || strcmp (oldname, indexname) !=0) { n_strcpy (oldname, indexname, sizeof(oldname)); unopen(fa); unopen(fb); unopen(fc); if (ckexist(indexname, ".ib")) { # if D1 fprintf(stderr, "found old index\n"); # endif fa = iopen(indexname, ".ia"); fb = iopen(indexname, ".ib"); fc = iopen(indexname, ".ic"); typeindex =1; # if D1 fprintf(stderr, "opened f's as %o %o %o\n",fa,fb,fc); # endif indexdate = gdate(fb); fread (&nhash, sizeof(nhash), 1, fa); fread (&iflong, sizeof(iflong), 1, fa); if (nhash > maxhash) { if (hpt) free (hpt); hpt=0; if (hfreq) free(hfreq); hfreq=0; maxhash=nhash; # if D1 fprintf(stderr, "Freed if needed maxhash %d\n",maxhash); # endif } if (hpt==0) hpt = zalloc(nhash, sizeof(*hpt)); # if D1 fprintf(stderr, "hpt now %o\n",hpt); # endif if (hpt == NULL) err("No space for hash list (%d)", nhash); fread( hpt, sizeof(*hpt), nhash, fa); if (hfreq==0) hfreq=zalloc(nhash, sizeof(*hfreq)); if (hfreq==NULL) err("No space for hash frequencies (%d)", nhash); frtbl = fread(hfreq, sizeof(*hfreq), nhash, fa); hfrflg = (frtbl == nhash); # if D1 fprintf(stderr,"Read pointer files\n"); # endif if (master.a == NULL) { if (iflong) master.b = zalloc(lmaster, sizeof(long)); else master.a = zalloc(lmaster, sizeof(int)); } if (master.a == NULL) err("no space for answer list", 0); } else if (makefgrep(indexname)) typeindex=2; else { err("No files %s\n", indexname); exit(1); } } if (iflong) masterp = (unsigned *) master.b; else masterp = master.a; # if D1 fprintf(stderr, "typeindex now %d\n",typeindex); # endif tagout[0]=0; if (typeindex==2) { grepcall(sinput, tagout, indexname); # if D1 fprintf(stderr, " back from grepcall\n"); # endif restodir(); return; } nitem = getq(qitem); # if D1 fprintf(stderr, "approaching doquery fb %o\n", fb); # endif nfound = doquery(hpt, nhash, fb, nitem, qitem, masterp); # ifdef D1 fprintf(stderr, "return from doquery with nfound %d\n", nfound); # endif if (falseflg == 0) nfound = baddrop(masterp, nfound, fc, nitem, qitem, rprog, full); # ifdef D1 fprintf(stderr, "after baddrop with nfound %d\n",nfound); fprintf(stderr, "tagout is /%s/, sout /%s/\n",tagout, soutput); # endif if (tags) result (masterp, nfound >tags ? tags : nfound, fc); # if D1 fprintf(stderr, "done with huntmain\n"); fprintf(stderr, "tagout is /%s/\n", tagout); fprintf(stderr, "string out is /%s/\n", soutput); # endif if (fgnamp>fgnames) { char **fgp; int k; # if D1 fprintf(stderr, "were %d bad files\n", fgnamp-fgnames); # endif grepquery[0]=0; for(k=0; k<nitem; k++) { n_strcat(grepquery, " ", sizeof(grepquery)); n_strcat(grepquery, qitem[k], sizeof(grepquery)); } for(fgp=fgnames; fgp<fgnamp; fgp++) { # if D1 fprintf(stderr, "Now on %s query /%s/\n", *fgp, grepquery); # endif makefgrep(*fgp); grepcall(grepquery, tagout, *fgp); # if D1 fprintf(stderr, "tagout now /%s/\n", tagout); # endif } } restodir(); }