xref: /titanic_53/usr/src/cmd/refer/hunt1.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
17*7c478bd9Sstevel@tonic-gate 
18*7c478bd9Sstevel@tonic-gate # include <locale.h>
19*7c478bd9Sstevel@tonic-gate # include <stdio.h>
20*7c478bd9Sstevel@tonic-gate # include <assert.h>
21*7c478bd9Sstevel@tonic-gate extern char refdir[];
22*7c478bd9Sstevel@tonic-gate extern int keepold;
23*7c478bd9Sstevel@tonic-gate extern char *fgnames[];
24*7c478bd9Sstevel@tonic-gate extern char **fgnamp;
25*7c478bd9Sstevel@tonic-gate FILE *fd =NULL;
26*7c478bd9Sstevel@tonic-gate int lmaster =500;
27*7c478bd9Sstevel@tonic-gate int *hfreq, hfrflg;
28*7c478bd9Sstevel@tonic-gate int colevel =0;
29*7c478bd9Sstevel@tonic-gate int measure=0;
30*7c478bd9Sstevel@tonic-gate int soutlen =1000;
31*7c478bd9Sstevel@tonic-gate int reached =0;
32*7c478bd9Sstevel@tonic-gate int iflong =0;
33*7c478bd9Sstevel@tonic-gate int prfreqs =0;
34*7c478bd9Sstevel@tonic-gate char usedir[100];
35*7c478bd9Sstevel@tonic-gate char * calloc();
36*7c478bd9Sstevel@tonic-gate char * todir();
37*7c478bd9Sstevel@tonic-gate char gfile[50];
38*7c478bd9Sstevel@tonic-gate static int full =1000;
39*7c478bd9Sstevel@tonic-gate static int tags =0;
40*7c478bd9Sstevel@tonic-gate char *sinput, *soutput, *tagout;
41*7c478bd9Sstevel@tonic-gate long indexdate =0, gdate();
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate main(argc,argv)
44*7c478bd9Sstevel@tonic-gate char *argv[];
45*7c478bd9Sstevel@tonic-gate {
46*7c478bd9Sstevel@tonic-gate 	/* read query from stdin, expect name of indexes in argv[1] */
47*7c478bd9Sstevel@tonic-gate 	static FILE *fa, *fb, *fc;
48*7c478bd9Sstevel@tonic-gate 	char nma[100], nmb[100], nmc[100], *qitem[100], *rprog = NULL;
49*7c478bd9Sstevel@tonic-gate 	char nmd[100], grepquery[256];
50*7c478bd9Sstevel@tonic-gate 	static char oldname[30] ;
51*7c478bd9Sstevel@tonic-gate 	static int was =0;
52*7c478bd9Sstevel@tonic-gate 	/* these pointers are unions of pointer to int and pointer to long */
53*7c478bd9Sstevel@tonic-gate 	long *hpt;
54*7c478bd9Sstevel@tonic-gate 	unsigned *master =0;
55*7c478bd9Sstevel@tonic-gate 	int falseflg, nhash, nitem, nfound, frtbl, kk;
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate 	/* special wart for refpart: default is tags only */
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
62*7c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
63*7c478bd9Sstevel@tonic-gate #endif
64*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate 	falseflg = 0;
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate 	while (argc > 1 && argv[1][0] == '-')
69*7c478bd9Sstevel@tonic-gate 	{
70*7c478bd9Sstevel@tonic-gate 		switch(argv[1][1])
71*7c478bd9Sstevel@tonic-gate 		{
72*7c478bd9Sstevel@tonic-gate 		case 'a': /* all output, incl. false drops */
73*7c478bd9Sstevel@tonic-gate 			falseflg = 1;
74*7c478bd9Sstevel@tonic-gate 			break;
75*7c478bd9Sstevel@tonic-gate 		case 'r':
76*7c478bd9Sstevel@tonic-gate 			argc--;
77*7c478bd9Sstevel@tonic-gate 			argv++;
78*7c478bd9Sstevel@tonic-gate 			rprog = argv[1];
79*7c478bd9Sstevel@tonic-gate 			break;
80*7c478bd9Sstevel@tonic-gate 		case 'F': /* put out full text */
81*7c478bd9Sstevel@tonic-gate 			full = setfrom(argv[1][2]);
82*7c478bd9Sstevel@tonic-gate 			break;
83*7c478bd9Sstevel@tonic-gate 		case 'T': /* put out tags */
84*7c478bd9Sstevel@tonic-gate 			tags = setfrom(argv[1][2]);
85*7c478bd9Sstevel@tonic-gate 			break;
86*7c478bd9Sstevel@tonic-gate 		case 'i': /* input in argument string */
87*7c478bd9Sstevel@tonic-gate 			argc--;
88*7c478bd9Sstevel@tonic-gate 			argv++;
89*7c478bd9Sstevel@tonic-gate 			sinput = argv[1];
90*7c478bd9Sstevel@tonic-gate 			break;
91*7c478bd9Sstevel@tonic-gate 		case 's': /*text output to string */
92*7c478bd9Sstevel@tonic-gate 		case 'o':
93*7c478bd9Sstevel@tonic-gate 			argc--;
94*7c478bd9Sstevel@tonic-gate 			argv++;
95*7c478bd9Sstevel@tonic-gate 			soutput = argv[1];
96*7c478bd9Sstevel@tonic-gate 			if ((int) argv[2]<16000)
97*7c478bd9Sstevel@tonic-gate 			{
98*7c478bd9Sstevel@tonic-gate 				soutlen = (int)argv[2];
99*7c478bd9Sstevel@tonic-gate 				argc--;
100*7c478bd9Sstevel@tonic-gate 				argv++;
101*7c478bd9Sstevel@tonic-gate 			}
102*7c478bd9Sstevel@tonic-gate 			break;
103*7c478bd9Sstevel@tonic-gate 		case 't': /*tag output to string */
104*7c478bd9Sstevel@tonic-gate 			argc--;
105*7c478bd9Sstevel@tonic-gate 			argv++;
106*7c478bd9Sstevel@tonic-gate 			tagout = argv[1];
107*7c478bd9Sstevel@tonic-gate 			break;
108*7c478bd9Sstevel@tonic-gate 		case 'l': /* length of internal lists */
109*7c478bd9Sstevel@tonic-gate 			argc--;
110*7c478bd9Sstevel@tonic-gate 			argv++;
111*7c478bd9Sstevel@tonic-gate 			lmaster = atoi(argv[1]);
112*7c478bd9Sstevel@tonic-gate 			break;
113*7c478bd9Sstevel@tonic-gate 		case 'g': /* suppress fgrep search on old files */
114*7c478bd9Sstevel@tonic-gate 			keepold = 0;
115*7c478bd9Sstevel@tonic-gate 			break;
116*7c478bd9Sstevel@tonic-gate 		case 'C': /* coordination level */
117*7c478bd9Sstevel@tonic-gate 			colevel = atoi(argv[1]+2);
118*7c478bd9Sstevel@tonic-gate # if D1
119*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "colevel set to %d\n",colevel);
120*7c478bd9Sstevel@tonic-gate # endif
121*7c478bd9Sstevel@tonic-gate 			break;
122*7c478bd9Sstevel@tonic-gate 		case 'P': /* print term freqs */
123*7c478bd9Sstevel@tonic-gate 			prfreqs=1;
124*7c478bd9Sstevel@tonic-gate 			break;
125*7c478bd9Sstevel@tonic-gate 		case 'm':
126*7c478bd9Sstevel@tonic-gate 			measure=1;
127*7c478bd9Sstevel@tonic-gate 			break;
128*7c478bd9Sstevel@tonic-gate 		}
129*7c478bd9Sstevel@tonic-gate 		argc--;
130*7c478bd9Sstevel@tonic-gate 		argv++;
131*7c478bd9Sstevel@tonic-gate 	}
132*7c478bd9Sstevel@tonic-gate 	if(argc < 2)
133*7c478bd9Sstevel@tonic-gate 		exit(1);
134*7c478bd9Sstevel@tonic-gate 	strcpy (nma, todir(argv[1]));
135*7c478bd9Sstevel@tonic-gate 	if (was == 0 || strcmp (oldname, nma) !=0)
136*7c478bd9Sstevel@tonic-gate 	{
137*7c478bd9Sstevel@tonic-gate 		strcpy (oldname,nma);
138*7c478bd9Sstevel@tonic-gate 		strcpy (nmb, nma);
139*7c478bd9Sstevel@tonic-gate 		strcpy (nmc, nmb);
140*7c478bd9Sstevel@tonic-gate 		strcpy(nmd,nma);
141*7c478bd9Sstevel@tonic-gate 		strcat (nma, ".ia");
142*7c478bd9Sstevel@tonic-gate 		strcat (nmb, ".ib");
143*7c478bd9Sstevel@tonic-gate 		strcat (nmc, ".ic");
144*7c478bd9Sstevel@tonic-gate 		strcat (nmd, ".id");
145*7c478bd9Sstevel@tonic-gate 		if (was)
146*7c478bd9Sstevel@tonic-gate 		{
147*7c478bd9Sstevel@tonic-gate 			fclose(fa);
148*7c478bd9Sstevel@tonic-gate 			fclose(fb);
149*7c478bd9Sstevel@tonic-gate 			fclose(fc);
150*7c478bd9Sstevel@tonic-gate 		}
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 		fa = fopen(nma, "r");
153*7c478bd9Sstevel@tonic-gate 		if (fa==NULL)
154*7c478bd9Sstevel@tonic-gate 		{
155*7c478bd9Sstevel@tonic-gate 			strcpy(*fgnamp++ = calloc(strlen(oldname)+2,1), oldname);
156*7c478bd9Sstevel@tonic-gate 			fb=NULL;
157*7c478bd9Sstevel@tonic-gate 			goto search;
158*7c478bd9Sstevel@tonic-gate 		}
159*7c478bd9Sstevel@tonic-gate 		fb = fopen(nmb, "r");
160*7c478bd9Sstevel@tonic-gate 		fc = fopen(nmc, "r");
161*7c478bd9Sstevel@tonic-gate 		was =1;
162*7c478bd9Sstevel@tonic-gate 		if (fb== NULL || fc ==NULL)
163*7c478bd9Sstevel@tonic-gate 		{
164*7c478bd9Sstevel@tonic-gate 			err(gettext("Index incomplete %s"), nmb);
165*7c478bd9Sstevel@tonic-gate 			exit(1);
166*7c478bd9Sstevel@tonic-gate 		}
167*7c478bd9Sstevel@tonic-gate 		indexdate = gdate(fb);
168*7c478bd9Sstevel@tonic-gate 		fd = fopen(nmd, "r");
169*7c478bd9Sstevel@tonic-gate 	}
170*7c478bd9Sstevel@tonic-gate 	fseek (fa, 0L, 0);
171*7c478bd9Sstevel@tonic-gate 	fread (&nhash, sizeof(nhash), 1, fa);
172*7c478bd9Sstevel@tonic-gate 	fread (&iflong, sizeof(iflong), 1, fa);
173*7c478bd9Sstevel@tonic-gate 	if(master==0)
174*7c478bd9Sstevel@tonic-gate 		master = (unsigned *) calloc (lmaster, iflong? sizeof(long): sizeof(unsigned));
175*7c478bd9Sstevel@tonic-gate 	hpt = (long *) calloc(nhash, sizeof(*hpt));
176*7c478bd9Sstevel@tonic-gate 	kk=fread( hpt, sizeof(*hpt), nhash, fa);
177*7c478bd9Sstevel@tonic-gate # if D1
178*7c478bd9Sstevel@tonic-gate 	fprintf(stderr,"read %d hashes, iflong %d, nhash %d\n", kk, iflong, nhash);
179*7c478bd9Sstevel@tonic-gate # endif
180*7c478bd9Sstevel@tonic-gate 	assert (kk==nhash);
181*7c478bd9Sstevel@tonic-gate 	hfreq = (int *) calloc(nhash, sizeof(*hfreq));
182*7c478bd9Sstevel@tonic-gate 	assert (hfreq != NULL);
183*7c478bd9Sstevel@tonic-gate 	frtbl = fread(hfreq, sizeof(*hfreq), nhash, fa);
184*7c478bd9Sstevel@tonic-gate 	hfrflg = (frtbl == nhash);
185*7c478bd9Sstevel@tonic-gate # if D1
186*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "read freqs %d\n", frtbl);
187*7c478bd9Sstevel@tonic-gate # endif
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate search:
190*7c478bd9Sstevel@tonic-gate 	while (1)
191*7c478bd9Sstevel@tonic-gate 	{
192*7c478bd9Sstevel@tonic-gate 		nitem = getq(qitem);
193*7c478bd9Sstevel@tonic-gate 		if (measure) tick();
194*7c478bd9Sstevel@tonic-gate 		if (nitem==0) continue;
195*7c478bd9Sstevel@tonic-gate 		if (nitem < 0) break;
196*7c478bd9Sstevel@tonic-gate 		if (tagout) tagout[0]=0;
197*7c478bd9Sstevel@tonic-gate 		if (fb!=NULL)
198*7c478bd9Sstevel@tonic-gate 		{
199*7c478bd9Sstevel@tonic-gate 			nfound = doquery(hpt, nhash, fb, nitem, qitem, master);
200*7c478bd9Sstevel@tonic-gate # if D1
201*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,"after doquery nfound %d\n", nfound);
202*7c478bd9Sstevel@tonic-gate # endif
203*7c478bd9Sstevel@tonic-gate 			fgnamp=fgnames;
204*7c478bd9Sstevel@tonic-gate 			if (falseflg == 0)
205*7c478bd9Sstevel@tonic-gate 				nfound = baddrop(master, nfound, fc, nitem, qitem, rprog, full);
206*7c478bd9Sstevel@tonic-gate # if D1
207*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,"after baddrop nfound %d\n", nfound);
208*7c478bd9Sstevel@tonic-gate # endif
209*7c478bd9Sstevel@tonic-gate 		}
210*7c478bd9Sstevel@tonic-gate 		if (fgnamp>fgnames)
211*7c478bd9Sstevel@tonic-gate 		{
212*7c478bd9Sstevel@tonic-gate 			char **fgp, tgbuff[100];
213*7c478bd9Sstevel@tonic-gate 			int k;
214*7c478bd9Sstevel@tonic-gate # if D1
215*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "were %d bad files\n", fgnamp-fgnames);
216*7c478bd9Sstevel@tonic-gate # endif
217*7c478bd9Sstevel@tonic-gate 			(void) memset(tgbuff, 0, sizeof (tgbuff));
218*7c478bd9Sstevel@tonic-gate 			grepquery[0]=0;
219*7c478bd9Sstevel@tonic-gate 			for(k=0; k<nitem; k++)
220*7c478bd9Sstevel@tonic-gate 			{
221*7c478bd9Sstevel@tonic-gate 				strcat(grepquery, " ");
222*7c478bd9Sstevel@tonic-gate 				strcat(grepquery, qitem[k]);
223*7c478bd9Sstevel@tonic-gate 			}
224*7c478bd9Sstevel@tonic-gate # if D1
225*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "grepquery %s\n",grepquery);
226*7c478bd9Sstevel@tonic-gate # endif
227*7c478bd9Sstevel@tonic-gate 			for(fgp=fgnames; fgp<fgnamp; fgp++)
228*7c478bd9Sstevel@tonic-gate 			{
229*7c478bd9Sstevel@tonic-gate # if D1
230*7c478bd9Sstevel@tonic-gate 				fprintf(stderr, "Now on %s query /%s/\n", *fgp, grepquery);
231*7c478bd9Sstevel@tonic-gate # endif
232*7c478bd9Sstevel@tonic-gate 				makefgrep(*fgp);
233*7c478bd9Sstevel@tonic-gate # if D1
234*7c478bd9Sstevel@tonic-gate 				fprintf(stderr, "grepmade\n");
235*7c478bd9Sstevel@tonic-gate # endif
236*7c478bd9Sstevel@tonic-gate 				if (tagout==0)
237*7c478bd9Sstevel@tonic-gate 					tagout=tgbuff;
238*7c478bd9Sstevel@tonic-gate 				grepcall(grepquery, tagout, *fgp);
239*7c478bd9Sstevel@tonic-gate # if D1
240*7c478bd9Sstevel@tonic-gate 				fprintf(stderr, "tagout now /%s/\n", tagout);
241*7c478bd9Sstevel@tonic-gate # endif
242*7c478bd9Sstevel@tonic-gate 				if (full)
243*7c478bd9Sstevel@tonic-gate 				{
244*7c478bd9Sstevel@tonic-gate 					int nout;
245*7c478bd9Sstevel@tonic-gate 					char *bout;
246*7c478bd9Sstevel@tonic-gate 					char *tagp;
247*7c478bd9Sstevel@tonic-gate 					char *oldtagp;
248*7c478bd9Sstevel@tonic-gate 					tagp = tagout;
249*7c478bd9Sstevel@tonic-gate 					while (*tagp) {
250*7c478bd9Sstevel@tonic-gate 						oldtagp = tagp;
251*7c478bd9Sstevel@tonic-gate 						while (*tagp && (*tagp != '\n'))
252*7c478bd9Sstevel@tonic-gate 							tagp++;
253*7c478bd9Sstevel@tonic-gate 						if (*tagp)
254*7c478bd9Sstevel@tonic-gate 							tagp++;
255*7c478bd9Sstevel@tonic-gate 				                nout = findline(oldtagp, &bout, 1000, 0L);
256*7c478bd9Sstevel@tonic-gate 						if (nout > 0)
257*7c478bd9Sstevel@tonic-gate 						{
258*7c478bd9Sstevel@tonic-gate 							fputs(bout, stdout);
259*7c478bd9Sstevel@tonic-gate 							free(bout);
260*7c478bd9Sstevel@tonic-gate 						}
261*7c478bd9Sstevel@tonic-gate 					}
262*7c478bd9Sstevel@tonic-gate 				}
263*7c478bd9Sstevel@tonic-gate 			}
264*7c478bd9Sstevel@tonic-gate 		}
265*7c478bd9Sstevel@tonic-gate 		if (tags)
266*7c478bd9Sstevel@tonic-gate 			result (master, nfound >tags ? tags: nfound, fc);
267*7c478bd9Sstevel@tonic-gate 		if (measure) tock();
268*7c478bd9Sstevel@tonic-gate 	}
269*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
270*7c478bd9Sstevel@tonic-gate }
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate char *
273*7c478bd9Sstevel@tonic-gate todir(t)
274*7c478bd9Sstevel@tonic-gate char *t;
275*7c478bd9Sstevel@tonic-gate {
276*7c478bd9Sstevel@tonic-gate 	char *s;
277*7c478bd9Sstevel@tonic-gate 	s=t;
278*7c478bd9Sstevel@tonic-gate 	while (*s) s++;
279*7c478bd9Sstevel@tonic-gate 	while (s>=t && *s != '/') s--;
280*7c478bd9Sstevel@tonic-gate 	if (s<t) return(t);
281*7c478bd9Sstevel@tonic-gate 	*s++ = 0;
282*7c478bd9Sstevel@tonic-gate 	t = (*t ? t : "/");
283*7c478bd9Sstevel@tonic-gate 	chdir (t);
284*7c478bd9Sstevel@tonic-gate 	strcpy (usedir,t);
285*7c478bd9Sstevel@tonic-gate 	return(s);
286*7c478bd9Sstevel@tonic-gate }
287*7c478bd9Sstevel@tonic-gate setfrom(c)
288*7c478bd9Sstevel@tonic-gate {
289*7c478bd9Sstevel@tonic-gate 	switch(c)
290*7c478bd9Sstevel@tonic-gate 	{
291*7c478bd9Sstevel@tonic-gate 	case 'y':
292*7c478bd9Sstevel@tonic-gate 	case '\0':
293*7c478bd9Sstevel@tonic-gate 	default:
294*7c478bd9Sstevel@tonic-gate 		return(1000);
295*7c478bd9Sstevel@tonic-gate 	case '1':
296*7c478bd9Sstevel@tonic-gate 	case '2':
297*7c478bd9Sstevel@tonic-gate 	case '3':
298*7c478bd9Sstevel@tonic-gate 	case '4':
299*7c478bd9Sstevel@tonic-gate 	case '5':
300*7c478bd9Sstevel@tonic-gate 	case '6':
301*7c478bd9Sstevel@tonic-gate 	case '7':
302*7c478bd9Sstevel@tonic-gate 	case '8':
303*7c478bd9Sstevel@tonic-gate 	case '9':
304*7c478bd9Sstevel@tonic-gate 		return(c-'0');
305*7c478bd9Sstevel@tonic-gate 	case 'n':
306*7c478bd9Sstevel@tonic-gate 	case '0':
307*7c478bd9Sstevel@tonic-gate 		return(0);
308*7c478bd9Sstevel@tonic-gate 	}
309*7c478bd9Sstevel@tonic-gate }
310