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