| 1 | # include "stdio.h" |
| 2 | # define unopen(fil) {if (fil!=NULL) {fclose(fil); fil=NULL;}} |
| 3 | extern char refdir[]; |
| 4 | int lmaster = 1000; |
| 5 | int reached = 0; |
| 6 | FILE *fd = 0; |
| 7 | int *hfreq, hfrflg; |
| 8 | int colevel = 0; |
| 9 | static union firetruck {unsigned *a; long *b;} master; |
| 10 | int iflong; |
| 11 | extern char *fgnames[], **fgnamp; |
| 12 | extern FILE *iopen(); |
| 13 | char *todir(); |
| 14 | int prfreqs = 0; |
| 15 | int typeindex = 0; |
| 16 | char usedir[100]; |
| 17 | static int full = 1000; |
| 18 | static int tags = 0; |
| 19 | char *sinput, *soutput, *tagout; |
| 20 | long indexdate = 0, gdate(); |
| 21 | int soutlen = 1000; |
| 22 | int taglen = 1000; |
| 23 | |
| 24 | huntmain(argc,argv) |
| 25 | char *argv[]; |
| 26 | { |
| 27 | /* read query from stdin, expect name of indexes in argv[1] */ |
| 28 | static FILE *fa, *fb, *fc; |
| 29 | char indexname[100], *qitem[100], *rprog = 0; |
| 30 | char grepquery[200]; |
| 31 | static char oldname[30] ; |
| 32 | static int nhash = 0; |
| 33 | static int maxhash = 0; |
| 34 | int falseflg = 0, nitem, nfound, frtbl; |
| 35 | static long *hpt = 0; |
| 36 | # if D1 |
| 37 | fprintf(stderr, "in glue1 argc %d argv %o %o\n", argc, argv[0],argv[1]); |
| 38 | # endif |
| 39 | savedir(); |
| 40 | while (argv[1][0] == '-') |
| 41 | { |
| 42 | # if D1 |
| 43 | fprintf(stderr, "argv.1 is %s\n",argv[1]); |
| 44 | # endif |
| 45 | switch(argv[1][1]) |
| 46 | { |
| 47 | case 'a': /* all output, incl. false drops */ |
| 48 | falseflg = 1; break; |
| 49 | case 'r': |
| 50 | argc--; argv++; |
| 51 | rprog = argv[1]; |
| 52 | break; |
| 53 | case 'F': /* put out full text */ |
| 54 | full = setfrom(argv[1][2]); |
| 55 | break; |
| 56 | case 'T': /* put out tags */ |
| 57 | tags = setfrom(argv[1][2]); |
| 58 | break; |
| 59 | case 'i': /* input in argument string */ |
| 60 | argc--; argv++; |
| 61 | sinput = argv[1]; |
| 62 | break; |
| 63 | case 's': /*text output to string */ |
| 64 | case 'o': |
| 65 | argc--; argv++; |
| 66 | soutput = argv[1]; |
| 67 | if (argv[2]<16000) |
| 68 | { |
| 69 | soutlen = argv[2]; |
| 70 | argc--; argv++; |
| 71 | } |
| 72 | break; |
| 73 | case 't': /*tag output to string */ |
| 74 | argc--; argv++; |
| 75 | tagout = argv[1]; |
| 76 | if (argv[2]<16000) |
| 77 | { |
| 78 | taglen = argv[2]; |
| 79 | argc--; argv++; |
| 80 | } |
| 81 | break; |
| 82 | case 'l': /* specify length of lists */ |
| 83 | argc--; argv++; |
| 84 | lmaster = atoi(argv[1]); |
| 85 | # if D1 |
| 86 | fprintf(stderr, "lmaster now %d\n",lmaster); |
| 87 | # endif |
| 88 | break; |
| 89 | case 'C': |
| 90 | argc--; argv++; |
| 91 | colevel = atoi(argv[1]); |
| 92 | break; |
| 93 | } |
| 94 | argc--; argv++; |
| 95 | } |
| 96 | strcpy (indexname, todir(argv[1])); |
| 97 | # if D1 |
| 98 | fprintf(stderr, "in huntmain indexname %s typeindex %d\n", indexname, typeindex); |
| 99 | # endif |
| 100 | if (typeindex == 0 || strcmp (oldname, indexname) !=0) |
| 101 | { |
| 102 | strcpy (oldname, indexname); |
| 103 | unopen(fa); unopen(fb); unopen(fc); |
| 104 | |
| 105 | if (ckexist(indexname, ".ib")) |
| 106 | { |
| 107 | # if D1 |
| 108 | fprintf(stderr, "found old index\n"); |
| 109 | # endif |
| 110 | fa = iopen(indexname, ".ia"); |
| 111 | fb = iopen(indexname, ".ib"); |
| 112 | fc = iopen(indexname, ".ic"); |
| 113 | typeindex =1; |
| 114 | # if D1 |
| 115 | fprintf(stderr, "opened f's as %o %o %o\n",fa,fb,fc); |
| 116 | # endif |
| 117 | indexdate = gdate(fb); |
| 118 | fread (&nhash, sizeof(nhash), 1, fa); |
| 119 | fread (&iflong, sizeof(iflong), 1, fa); |
| 120 | if (nhash > maxhash) |
| 121 | { |
| 122 | if (hpt) |
| 123 | free (hpt, maxhash, sizeof(*hpt)); |
| 124 | hpt=0; |
| 125 | if (hfreq) |
| 126 | free(hfreq, maxhash, sizeof(*hfreq)); |
| 127 | hfreq=0; |
| 128 | maxhash=nhash; |
| 129 | # if D1 |
| 130 | fprintf(stderr, "Freed if needed maxhash %d\n",maxhash); |
| 131 | # endif |
| 132 | } |
| 133 | if (hpt==0) |
| 134 | hpt = zalloc(nhash, sizeof(*hpt)); |
| 135 | # if D1 |
| 136 | fprintf(stderr, "hpt now %o\n",hpt); |
| 137 | # endif |
| 138 | if (hpt == NULL) |
| 139 | err ("No space for hash list (%d)", nhash); |
| 140 | fread( hpt, sizeof(*hpt), nhash, fa); |
| 141 | if (hfreq==0) |
| 142 | hfreq=zalloc(nhash, sizeof(*hfreq)); |
| 143 | if (hfreq==NULL) |
| 144 | err ("No space for hash frequencies (%d)", nhash); |
| 145 | frtbl = fread(hfreq, sizeof(*hfreq), nhash, fa); |
| 146 | hfrflg = (frtbl == nhash); |
| 147 | # if D1 |
| 148 | fprintf(stderr,"Read pointer files\n"); |
| 149 | # endif |
| 150 | if (master.a == NULL) |
| 151 | if (iflong) |
| 152 | master.b = zalloc(lmaster, sizeof(long)); |
| 153 | else |
| 154 | master.a = zalloc(lmaster, sizeof(int)); |
| 155 | if (master.a == NULL) |
| 156 | err ("no space for answer list",0); |
| 157 | } |
| 158 | else |
| 159 | if (makefgrep(indexname)) |
| 160 | typeindex=2; |
| 161 | else |
| 162 | { |
| 163 | err ("No files %s\n",indexname); |
| 164 | exit(1); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | # if D1 |
| 169 | fprintf(stderr, "typeindex now %d\n",typeindex); |
| 170 | # endif |
| 171 | tagout[0]=0; |
| 172 | if (typeindex==2) |
| 173 | { |
| 174 | grepcall(sinput, tagout, indexname); |
| 175 | # if D1 |
| 176 | fprintf(stderr, " back from grepcall\n"); |
| 177 | # endif |
| 178 | restodir(); |
| 179 | return; |
| 180 | } |
| 181 | nitem = getq(qitem); |
| 182 | # if D1 |
| 183 | fprintf(stderr, "approaching doquery fb %o\n", fb); |
| 184 | # endif |
| 185 | nfound = doquery(hpt, nhash, fb, nitem, qitem, master); |
| 186 | # ifdef D1 |
| 187 | fprintf(stderr, "return from doquery with nfound %d\n", nfound); |
| 188 | # endif |
| 189 | if (falseflg == 0) |
| 190 | nfound = baddrop(master, nfound, fc, nitem, qitem, rprog, full); |
| 191 | # ifdef D1 |
| 192 | fprintf(stderr, "after baddrop with nfound %d\n",nfound); |
| 193 | fprintf(stderr, "tagout is /%s/, sout /%s/\n",tagout, soutput); |
| 194 | # endif |
| 195 | if (tags) |
| 196 | result (master, nfound >tags ? tags : nfound, fc); |
| 197 | # if D1 |
| 198 | fprintf(stderr, "done with huntmain\n"); |
| 199 | fprintf(stderr, "tagout is /%s/\n", tagout); |
| 200 | fprintf(stderr, "string out is /%s/\n", soutput); |
| 201 | # endif |
| 202 | if (fgnamp>fgnames) |
| 203 | { |
| 204 | char **fgp; |
| 205 | int k; |
| 206 | # if D1 |
| 207 | fprintf(stderr, "were %d bad files\n", fgnamp-fgnames); |
| 208 | # endif |
| 209 | grepquery[0]=0; |
| 210 | for(k=0; k<nitem; k++) |
| 211 | { |
| 212 | strcat(grepquery, " "); |
| 213 | strcat(grepquery, qitem[k]); |
| 214 | } |
| 215 | for(fgp=fgnames; fgp<fgnamp; fgp++) |
| 216 | { |
| 217 | # if D1 |
| 218 | fprintf(stderr, "Now on %s query /%s/\n", *fgp, grepquery); |
| 219 | # endif |
| 220 | makefgrep(*fgp); |
| 221 | grepcall(grepquery, tagout, *fgp); |
| 222 | # if D1 |
| 223 | fprintf(stderr, "tagout now /%s/\n", tagout); |
| 224 | # endif |
| 225 | } |
| 226 | } |
| 227 | restodir(); |
| 228 | } |
| 229 | |
| 230 | char * |
| 231 | todir(t) |
| 232 | char *t; |
| 233 | { |
| 234 | char *s; |
| 235 | s=t; |
| 236 | while (*s) s++; |
| 237 | while (s>=t && *s != '/') s--; |
| 238 | if (s<t) return(t); |
| 239 | *s++ = 0; |
| 240 | t = (*t ? t : "/"); |
| 241 | chdir (t); |
| 242 | strcpy (usedir,t); |
| 243 | return(s); |
| 244 | } |
| 245 | setfrom(c) |
| 246 | { |
| 247 | switch(c) |
| 248 | { |
| 249 | case 'y': case '\0': |
| 250 | default: |
| 251 | return(1000); |
| 252 | case '1': |
| 253 | case '2': case '3': case '4': case '5': |
| 254 | case '6': case '7': case '8': case '9': |
| 255 | return(c-'0'); |
| 256 | case 'n': case '0': |
| 257 | return(0); |
| 258 | } |
| 259 | } |