2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
6 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
7 /* All Rights Reserved */
10 * Copyright (c) 1980 Regents of the University of California.
11 * All rights reserved. The Berkeley software License Agreement
12 * specifies the terms and conditions for redistribution.
15 #pragma ident "%Z%%M% %I% %E% SMI"
19 #define unopen(fil) {if (fil != NULL) {fclose(fil); fil = NULL; }}
27 static union firetruck
{
32 extern char *fgnames
[], **fgnamp
;
37 static int full
= 1000;
39 char *sinput
, *soutput
, *tagout
;
40 long indexdate
= 0, gdate();
49 extern void grepcall();
50 extern int makefgrep();
51 extern void restodir();
53 extern void savedir();
54 extern void *zalloc();
56 static int setfrom(char);
60 huntmain(int argc
, char *argv
[])
62 /* read query from stdin, expect name of indexes in argv[1] */
63 static FILE *fa
, *fb
, *fc
;
64 char indexname
[100], *qitem
[100], *rprog
= 0;
66 static char oldname
[30];
68 static int maxhash
= 0;
69 int falseflg
= 0, nitem
, nfound
, frtbl
;
74 fprintf(stderr
, "in glue1 argc %d argv %o %o\n",
75 argc
, argv
[0], argv
[1]);
78 while (argv
[1][0] == '-') {
80 fprintf(stderr
, "argv.1 is %s\n", argv
[1]);
83 case 'a': /* all output, incl. false drops */
91 case 'F': /* put out full text */
92 full
= setfrom(argv
[1][2]);
94 case 'T': /* put out tags */
95 tags
= setfrom(argv
[1][2]);
97 case 'i': /* input in argument string */
102 case 's': /* text output to string */
107 if ((int)argv
[2] < 16000) {
108 soutlen
= (int)argv
[2];
113 case 't': /* tag output to string */
117 if ((int)argv
[2] < 16000) {
118 taglen
= (int)argv
[2];
123 case 'l': /* specify length of lists */
126 lmaster
= atoi(argv
[1]);
128 fprintf(stderr
, "lmaster now %d\n", lmaster
);
134 colevel
= atoi(argv
[1]);
140 strcpy(indexname
, todir(argv
[1]));
142 fprintf(stderr
, "in huntmain indexname %s typeindex %d\n",
143 indexname
, typeindex
);
145 if (typeindex
== 0 || strcmp(oldname
, indexname
) != 0) {
146 strcpy(oldname
, indexname
);
151 if (ckexist(indexname
, ".ib")) {
153 fprintf(stderr
, "found old index\n");
155 fa
= iopen(indexname
, ".ia");
156 fb
= iopen(indexname
, ".ib");
157 fc
= iopen(indexname
, ".ic");
160 fprintf(stderr
, "opened f's as %o %o %o\n", fa
, fb
, fc
);
162 indexdate
= gdate(fb
);
163 fread(&nhash
, sizeof (nhash
), 1, fa
);
164 fread(&iflong
, sizeof (iflong
), 1, fa
);
165 if (nhash
> maxhash
) {
167 free(hpt
, maxhash
, sizeof (*hpt
));
170 free(hfreq
, maxhash
, sizeof (*hfreq
));
174 fprintf(stderr
, "Freed if needed maxhash %d\n",
179 hpt
= (long *)zalloc(nhash
, sizeof (*hpt
));
181 fprintf(stderr
, "hpt now %o\n", hpt
);
186 * %d is the size of the hash table - not
187 * very interesting info for the end users.
188 * Hash is a computer science terminology.
190 err(gettext("No space for hash list (%d)"),
192 fread(hpt
, sizeof (*hpt
), nhash
, fa
);
194 hfreq
= (int *)zalloc(nhash
, sizeof (*hfreq
));
198 * %d is the size of the hash table.
201 "No space for hash frequencies (%d)"),
203 frtbl
= fread(hfreq
, sizeof (*hfreq
), nhash
, fa
);
204 hfrflg
= (frtbl
== nhash
);
206 fprintf(stderr
, "Read pointer files\n");
208 if (master
.a
== NULL
)
210 master
.b
= (long *)zalloc(lmaster
,
213 master
.a
= (unsigned *)zalloc(lmaster
,
215 if (master
.a
== NULL
)
216 err(gettext("no space for answer list"), 0);
218 if (makefgrep(indexname
))
221 err(gettext("No files %s\n"), indexname
);
227 masterp
= (unsigned *)master
.b
;
232 fprintf(stderr
, "typeindex now %d\n", typeindex
);
235 if (typeindex
== 2) {
236 grepcall(sinput
, tagout
, indexname
);
238 fprintf(stderr
, " back from grepcall\n");
245 fprintf(stderr
, "approaching doquery fb %o\n", fb
);
247 nfound
= doquery(hpt
, nhash
, fb
, nitem
, qitem
, masterp
);
249 fprintf(stderr
, "return from doquery with nfound %d\n", nfound
);
252 nfound
= baddrop(masterp
, nfound
, fc
, nitem
, qitem
,
255 fprintf(stderr
, "after baddrop with nfound %d\n", nfound
);
256 fprintf(stderr
, "tagout is /%s/, sout /%s/\n", tagout
, soutput
);
259 result(masterp
, nfound
> tags
? tags
: nfound
, fc
);
261 fprintf(stderr
, "done with huntmain\n");
262 fprintf(stderr
, "tagout is /%s/\n", tagout
);
263 fprintf(stderr
, "string out is /%s/\n", soutput
);
265 if (fgnamp
> fgnames
) {
269 fprintf(stderr
, "were %d bad files\n", fgnamp
-fgnames
);
272 for (k
= 0; k
< nitem
; k
++) {
273 strcat(grepquery
, " ");
274 strcat(grepquery
, qitem
[k
]);
276 for (fgp
= fgnames
; fgp
< fgnamp
; fgp
++) {
278 fprintf(stderr
, "Now on %s query /%s/\n",
282 grepcall(grepquery
, tagout
, *fgp
);
284 fprintf(stderr
, "tagout now /%s/\n", tagout
);
299 while (s
>= t
&& *s
!= '/') s
--;