xref: /illumos-gate/usr/src/cmd/refer/hunt1.c (revision 11a8fa6c)
1*11a8fa6cSceastha /*
2*11a8fa6cSceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3*11a8fa6cSceastha  * Use is subject to license terms.
4*11a8fa6cSceastha  */
5*11a8fa6cSceastha 
67c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
77c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
117c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
127c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
137c478bd9Sstevel@tonic-gate  */
147c478bd9Sstevel@tonic-gate 
15*11a8fa6cSceastha #pragma ident	"%Z%%M%	%I%	%E% SMI"
167c478bd9Sstevel@tonic-gate 
17*11a8fa6cSceastha #include <locale.h>
18*11a8fa6cSceastha #include <stdio.h>
19*11a8fa6cSceastha #include <assert.h>
207c478bd9Sstevel@tonic-gate extern char refdir[];
217c478bd9Sstevel@tonic-gate extern int keepold;
227c478bd9Sstevel@tonic-gate extern char *fgnames[];
237c478bd9Sstevel@tonic-gate extern char **fgnamp;
24*11a8fa6cSceastha FILE *fd = NULL;
25*11a8fa6cSceastha int lmaster = 500;
267c478bd9Sstevel@tonic-gate int *hfreq, hfrflg;
27*11a8fa6cSceastha int colevel = 0;
28*11a8fa6cSceastha int measure = 0;
29*11a8fa6cSceastha int soutlen = 1000;
30*11a8fa6cSceastha int reached = 0;
31*11a8fa6cSceastha int iflong = 0;
32*11a8fa6cSceastha int prfreqs = 0;
337c478bd9Sstevel@tonic-gate char usedir[100];
34*11a8fa6cSceastha char *calloc();
35*11a8fa6cSceastha char *todir();
367c478bd9Sstevel@tonic-gate char gfile[50];
37*11a8fa6cSceastha static int full = 1000;
38*11a8fa6cSceastha static int tags = 0;
397c478bd9Sstevel@tonic-gate char *sinput, *soutput, *tagout;
40*11a8fa6cSceastha long indexdate = 0, gdate();
41*11a8fa6cSceastha 
42*11a8fa6cSceastha extern int baddrop();
43*11a8fa6cSceastha extern int doquery();
44*11a8fa6cSceastha extern void err();
45*11a8fa6cSceastha extern long findline();
46*11a8fa6cSceastha extern int getq();
47*11a8fa6cSceastha extern void grepcall();
48*11a8fa6cSceastha extern int makefgrep();
49*11a8fa6cSceastha extern void result();
50*11a8fa6cSceastha extern void tick();
51*11a8fa6cSceastha extern void tock();
527c478bd9Sstevel@tonic-gate 
53*11a8fa6cSceastha static int setfrom(char);
54*11a8fa6cSceastha 
55*11a8fa6cSceastha int
main(int argc,char * argv[])56*11a8fa6cSceastha main(int argc, char *argv[])
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate 	/* read query from stdin, expect name of indexes in argv[1] */
597c478bd9Sstevel@tonic-gate 	static FILE *fa, *fb, *fc;
607c478bd9Sstevel@tonic-gate 	char nma[100], nmb[100], nmc[100], *qitem[100], *rprog = NULL;
617c478bd9Sstevel@tonic-gate 	char nmd[100], grepquery[256];
62*11a8fa6cSceastha 	static char oldname[30];
63*11a8fa6cSceastha 	static int was = 0;
647c478bd9Sstevel@tonic-gate 	/* these pointers are unions of pointer to int and pointer to long */
657c478bd9Sstevel@tonic-gate 	long *hpt;
66*11a8fa6cSceastha 	unsigned *master = 0;
677c478bd9Sstevel@tonic-gate 	int falseflg, nhash, nitem, nfound, frtbl, kk;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	/* special wart for refpart: default is tags only */
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
74*11a8fa6cSceastha #define	TEXT_DOMAIN "SYS_TEST"
757c478bd9Sstevel@tonic-gate #endif
767c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	falseflg = 0;
797c478bd9Sstevel@tonic-gate 
80*11a8fa6cSceastha 	while (argc > 1 && argv[1][0] == '-') {
81*11a8fa6cSceastha 		switch (argv[1][1]) {
827c478bd9Sstevel@tonic-gate 		case 'a': /* all output, incl. false drops */
83*11a8fa6cSceastha 			falseflg = 1;
847c478bd9Sstevel@tonic-gate 			break;
857c478bd9Sstevel@tonic-gate 		case 'r':
86*11a8fa6cSceastha 			argc--;
877c478bd9Sstevel@tonic-gate 			argv++;
887c478bd9Sstevel@tonic-gate 			rprog = argv[1];
897c478bd9Sstevel@tonic-gate 			break;
907c478bd9Sstevel@tonic-gate 		case 'F': /* put out full text */
917c478bd9Sstevel@tonic-gate 			full = setfrom(argv[1][2]);
927c478bd9Sstevel@tonic-gate 			break;
937c478bd9Sstevel@tonic-gate 		case 'T': /* put out tags */
947c478bd9Sstevel@tonic-gate 			tags = setfrom(argv[1][2]);
957c478bd9Sstevel@tonic-gate 			break;
967c478bd9Sstevel@tonic-gate 		case 'i': /* input in argument string */
97*11a8fa6cSceastha 			argc--;
987c478bd9Sstevel@tonic-gate 			argv++;
997c478bd9Sstevel@tonic-gate 			sinput = argv[1];
1007c478bd9Sstevel@tonic-gate 			break;
101*11a8fa6cSceastha 		case 's': /* text output to string */
1027c478bd9Sstevel@tonic-gate 		case 'o':
103*11a8fa6cSceastha 			argc--;
1047c478bd9Sstevel@tonic-gate 			argv++;
1057c478bd9Sstevel@tonic-gate 			soutput = argv[1];
106*11a8fa6cSceastha 			if ((int)argv[2] < 16000) {
1077c478bd9Sstevel@tonic-gate 				soutlen = (int)argv[2];
108*11a8fa6cSceastha 				argc--;
1097c478bd9Sstevel@tonic-gate 				argv++;
1107c478bd9Sstevel@tonic-gate 			}
1117c478bd9Sstevel@tonic-gate 			break;
112*11a8fa6cSceastha 		case 't': /* tag output to string */
113*11a8fa6cSceastha 			argc--;
1147c478bd9Sstevel@tonic-gate 			argv++;
1157c478bd9Sstevel@tonic-gate 			tagout = argv[1];
1167c478bd9Sstevel@tonic-gate 			break;
1177c478bd9Sstevel@tonic-gate 		case 'l': /* length of internal lists */
118*11a8fa6cSceastha 			argc--;
1197c478bd9Sstevel@tonic-gate 			argv++;
1207c478bd9Sstevel@tonic-gate 			lmaster = atoi(argv[1]);
1217c478bd9Sstevel@tonic-gate 			break;
1227c478bd9Sstevel@tonic-gate 		case 'g': /* suppress fgrep search on old files */
1237c478bd9Sstevel@tonic-gate 			keepold = 0;
1247c478bd9Sstevel@tonic-gate 			break;
1257c478bd9Sstevel@tonic-gate 		case 'C': /* coordination level */
1267c478bd9Sstevel@tonic-gate 			colevel = atoi(argv[1]+2);
127*11a8fa6cSceastha #if D1
128*11a8fa6cSceastha 			fprintf(stderr, "colevel set to %d\n", colevel);
129*11a8fa6cSceastha #endif
1307c478bd9Sstevel@tonic-gate 			break;
1317c478bd9Sstevel@tonic-gate 		case 'P': /* print term freqs */
132*11a8fa6cSceastha 			prfreqs = 1;
1337c478bd9Sstevel@tonic-gate 			break;
1347c478bd9Sstevel@tonic-gate 		case 'm':
135*11a8fa6cSceastha 			measure = 1;
1367c478bd9Sstevel@tonic-gate 			break;
1377c478bd9Sstevel@tonic-gate 		}
138*11a8fa6cSceastha 		argc--;
1397c478bd9Sstevel@tonic-gate 		argv++;
1407c478bd9Sstevel@tonic-gate 	}
141*11a8fa6cSceastha 	if (argc < 2)
1427c478bd9Sstevel@tonic-gate 		exit(1);
143*11a8fa6cSceastha 	strcpy(nma, todir(argv[1]));
144*11a8fa6cSceastha 	if (was == 0 || strcmp(oldname, nma) != 0) {
145*11a8fa6cSceastha 		strcpy(oldname, nma);
146*11a8fa6cSceastha 		strcpy(nmb, nma);
147*11a8fa6cSceastha 		strcpy(nmc, nmb);
148*11a8fa6cSceastha 		strcpy(nmd, nma);
149*11a8fa6cSceastha 		strcat(nma, ".ia");
150*11a8fa6cSceastha 		strcat(nmb, ".ib");
151*11a8fa6cSceastha 		strcat(nmc, ".ic");
152*11a8fa6cSceastha 		strcat(nmd, ".id");
153*11a8fa6cSceastha 		if (was) {
154*11a8fa6cSceastha 			fclose(fa);
155*11a8fa6cSceastha 			fclose(fb);
1567c478bd9Sstevel@tonic-gate 			fclose(fc);
1577c478bd9Sstevel@tonic-gate 		}
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 		fa = fopen(nma, "r");
160*11a8fa6cSceastha 		if (fa == NULL) {
161*11a8fa6cSceastha 			strcpy(*fgnamp++ = calloc(strlen(oldname)+2, 1),
162*11a8fa6cSceastha 			    oldname);
163*11a8fa6cSceastha 			fb = NULL;
1647c478bd9Sstevel@tonic-gate 			goto search;
1657c478bd9Sstevel@tonic-gate 		}
1667c478bd9Sstevel@tonic-gate 		fb = fopen(nmb, "r");
1677c478bd9Sstevel@tonic-gate 		fc = fopen(nmc, "r");
168*11a8fa6cSceastha 		was = 1;
169*11a8fa6cSceastha 		if (fb == NULL || fc == NULL) {
1707c478bd9Sstevel@tonic-gate 			err(gettext("Index incomplete %s"), nmb);
1717c478bd9Sstevel@tonic-gate 			exit(1);
1727c478bd9Sstevel@tonic-gate 		}
1737c478bd9Sstevel@tonic-gate 		indexdate = gdate(fb);
1747c478bd9Sstevel@tonic-gate 		fd = fopen(nmd, "r");
1757c478bd9Sstevel@tonic-gate 	}
176*11a8fa6cSceastha 	fseek(fa, 0L, 0);
177*11a8fa6cSceastha 	fread(&nhash, sizeof (nhash), 1, fa);
178*11a8fa6cSceastha 	fread(&iflong, sizeof (iflong), 1, fa);
179*11a8fa6cSceastha 	if (master == 0)
180*11a8fa6cSceastha 		master = (unsigned *)calloc(lmaster, iflong ?
181*11a8fa6cSceastha 		    sizeof (long) : sizeof (unsigned));
182*11a8fa6cSceastha 	hpt = (long *)calloc(nhash, sizeof (*hpt));
183*11a8fa6cSceastha 	kk = fread(hpt, sizeof (*hpt), nhash, fa);
184*11a8fa6cSceastha #if D1
185*11a8fa6cSceastha 	fprintf(stderr, "read %d hashes, iflong %d, nhash %d\n",
186*11a8fa6cSceastha 	    kk, iflong, nhash);
187*11a8fa6cSceastha #endif
188*11a8fa6cSceastha 	assert(kk == nhash);
189*11a8fa6cSceastha 	hfreq = (int *)calloc(nhash, sizeof (*hfreq));
190*11a8fa6cSceastha 	assert(hfreq != NULL);
191*11a8fa6cSceastha 	frtbl = fread(hfreq, sizeof (*hfreq), nhash, fa);
1927c478bd9Sstevel@tonic-gate 	hfrflg = (frtbl == nhash);
193*11a8fa6cSceastha #if D1
1947c478bd9Sstevel@tonic-gate 	fprintf(stderr, "read freqs %d\n", frtbl);
195*11a8fa6cSceastha #endif
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate search:
198*11a8fa6cSceastha 	while (1) {
1997c478bd9Sstevel@tonic-gate 		nitem = getq(qitem);
2007c478bd9Sstevel@tonic-gate 		if (measure) tick();
201*11a8fa6cSceastha 		if (nitem == 0) continue;
2027c478bd9Sstevel@tonic-gate 		if (nitem < 0) break;
203*11a8fa6cSceastha 		if (tagout) tagout[0] = 0;
204*11a8fa6cSceastha 		if (fb != NULL) {
2057c478bd9Sstevel@tonic-gate 			nfound = doquery(hpt, nhash, fb, nitem, qitem, master);
206*11a8fa6cSceastha #if D1
207*11a8fa6cSceastha 			fprintf(stderr, "after doquery nfound %d\n", nfound);
208*11a8fa6cSceastha #endif
209*11a8fa6cSceastha 			fgnamp = fgnames;
2107c478bd9Sstevel@tonic-gate 			if (falseflg == 0)
211*11a8fa6cSceastha 				nfound = baddrop(master, nfound, fc,
212*11a8fa6cSceastha 				    nitem, qitem, rprog, full);
213*11a8fa6cSceastha #if D1
214*11a8fa6cSceastha 			fprintf(stderr, "after baddrop nfound %d\n", nfound);
215*11a8fa6cSceastha #endif
2167c478bd9Sstevel@tonic-gate 		}
217*11a8fa6cSceastha 		if (fgnamp > fgnames) {
2187c478bd9Sstevel@tonic-gate 			char **fgp, tgbuff[100];
2197c478bd9Sstevel@tonic-gate 			int k;
220*11a8fa6cSceastha #if D1
2217c478bd9Sstevel@tonic-gate 			fprintf(stderr, "were %d bad files\n", fgnamp-fgnames);
222*11a8fa6cSceastha #endif
2237c478bd9Sstevel@tonic-gate 			(void) memset(tgbuff, 0, sizeof (tgbuff));
224*11a8fa6cSceastha 			grepquery[0] = 0;
225*11a8fa6cSceastha 			for (k = 0; k < nitem; k++) {
2267c478bd9Sstevel@tonic-gate 				strcat(grepquery, " ");
2277c478bd9Sstevel@tonic-gate 				strcat(grepquery, qitem[k]);
2287c478bd9Sstevel@tonic-gate 			}
229*11a8fa6cSceastha #if D1
230*11a8fa6cSceastha 			fprintf(stderr, "grepquery %s\n", grepquery);
231*11a8fa6cSceastha #endif
232*11a8fa6cSceastha 			for (fgp = fgnames; fgp < fgnamp; fgp++) {
233*11a8fa6cSceastha #if D1
234*11a8fa6cSceastha 				fprintf(stderr, "Now on %s query /%s/\n",
235*11a8fa6cSceastha 				    *fgp, grepquery);
236*11a8fa6cSceastha #endif
2377c478bd9Sstevel@tonic-gate 				makefgrep(*fgp);
238*11a8fa6cSceastha #if D1
2397c478bd9Sstevel@tonic-gate 				fprintf(stderr, "grepmade\n");
240*11a8fa6cSceastha #endif
241*11a8fa6cSceastha 				if (tagout == 0)
242*11a8fa6cSceastha 					tagout = tgbuff;
2437c478bd9Sstevel@tonic-gate 				grepcall(grepquery, tagout, *fgp);
244*11a8fa6cSceastha #if D1
2457c478bd9Sstevel@tonic-gate 				fprintf(stderr, "tagout now /%s/\n", tagout);
246*11a8fa6cSceastha #endif
247*11a8fa6cSceastha 				if (full) {
2487c478bd9Sstevel@tonic-gate 					int nout;
2497c478bd9Sstevel@tonic-gate 					char *bout;
2507c478bd9Sstevel@tonic-gate 					char *tagp;
2517c478bd9Sstevel@tonic-gate 					char *oldtagp;
2527c478bd9Sstevel@tonic-gate 					tagp = tagout;
2537c478bd9Sstevel@tonic-gate 					while (*tagp) {
2547c478bd9Sstevel@tonic-gate 						oldtagp = tagp;
255*11a8fa6cSceastha 						while (*tagp &&
256*11a8fa6cSceastha 						    (*tagp != '\n'))
2577c478bd9Sstevel@tonic-gate 							tagp++;
258*11a8fa6cSceastha 						if (*tagp)
2597c478bd9Sstevel@tonic-gate 							tagp++;
260*11a8fa6cSceastha 						nout = findline(oldtagp, &bout,
261*11a8fa6cSceastha 						    1000, 0L);
262*11a8fa6cSceastha 						if (nout > 0) {
2637c478bd9Sstevel@tonic-gate 							fputs(bout, stdout);
264*11a8fa6cSceastha 							free(bout);
2657c478bd9Sstevel@tonic-gate 						}
2667c478bd9Sstevel@tonic-gate 					}
2677c478bd9Sstevel@tonic-gate 				}
2687c478bd9Sstevel@tonic-gate 			}
2697c478bd9Sstevel@tonic-gate 		}
2707c478bd9Sstevel@tonic-gate 		if (tags)
271*11a8fa6cSceastha 			result(master, nfound > tags ? tags : nfound, fc);
2727c478bd9Sstevel@tonic-gate 		if (measure) tock();
2737c478bd9Sstevel@tonic-gate 	}
274*11a8fa6cSceastha 	return (0);
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate char *
todir(char * t)278*11a8fa6cSceastha todir(char *t)
2797c478bd9Sstevel@tonic-gate {
2807c478bd9Sstevel@tonic-gate 	char *s;
281*11a8fa6cSceastha 	s = t;
2827c478bd9Sstevel@tonic-gate 	while (*s) s++;
283*11a8fa6cSceastha 	while (s >= t && *s != '/') s--;
284*11a8fa6cSceastha 	if (s < t)
285*11a8fa6cSceastha 		return (t);
2867c478bd9Sstevel@tonic-gate 	*s++ = 0;
2877c478bd9Sstevel@tonic-gate 	t = (*t ? t : "/");
288*11a8fa6cSceastha 	chdir(t);
289*11a8fa6cSceastha 	strcpy(usedir, t);
290*11a8fa6cSceastha 	return (s);
2917c478bd9Sstevel@tonic-gate }
292*11a8fa6cSceastha 
293*11a8fa6cSceastha static int
setfrom(char c)294*11a8fa6cSceastha setfrom(char c)
2957c478bd9Sstevel@tonic-gate {
296*11a8fa6cSceastha 	switch (c) {
297*11a8fa6cSceastha 	case 'y':
2987c478bd9Sstevel@tonic-gate 	case '\0':
2997c478bd9Sstevel@tonic-gate 	default:
300*11a8fa6cSceastha 		return (1000);
3017c478bd9Sstevel@tonic-gate 	case '1':
302*11a8fa6cSceastha 	case '2':
303*11a8fa6cSceastha 	case '3':
304*11a8fa6cSceastha 	case '4':
3057c478bd9Sstevel@tonic-gate 	case '5':
306*11a8fa6cSceastha 	case '6':
307*11a8fa6cSceastha 	case '7':
308*11a8fa6cSceastha 	case '8':
3097c478bd9Sstevel@tonic-gate 	case '9':
310*11a8fa6cSceastha 		return (c-'0');
311*11a8fa6cSceastha 	case 'n':
3127c478bd9Sstevel@tonic-gate 	case '0':
313*11a8fa6cSceastha 		return (0);
3147c478bd9Sstevel@tonic-gate 	}
3157c478bd9Sstevel@tonic-gate }
316