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