xref: /titanic_52/usr/src/cmd/refer/hunt1.c (revision 11a8fa6cb17403e630122ac19b39a323c6e64142)
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 
157c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
167c478bd9Sstevel@tonic-gate 
177c478bd9Sstevel@tonic-gate #include <locale.h>
187c478bd9Sstevel@tonic-gate #include <stdio.h>
197c478bd9Sstevel@tonic-gate #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;
247c478bd9Sstevel@tonic-gate FILE *fd = NULL;
257c478bd9Sstevel@tonic-gate int lmaster = 500;
267c478bd9Sstevel@tonic-gate int *hfreq, hfrflg;
277c478bd9Sstevel@tonic-gate int colevel = 0;
287c478bd9Sstevel@tonic-gate int measure = 0;
297c478bd9Sstevel@tonic-gate int soutlen = 1000;
307c478bd9Sstevel@tonic-gate int reached = 0;
317c478bd9Sstevel@tonic-gate int iflong = 0;
327c478bd9Sstevel@tonic-gate int prfreqs = 0;
337c478bd9Sstevel@tonic-gate char usedir[100];
347c478bd9Sstevel@tonic-gate char *calloc();
357c478bd9Sstevel@tonic-gate char *todir();
367c478bd9Sstevel@tonic-gate char gfile[50];
377c478bd9Sstevel@tonic-gate static int full = 1000;
387c478bd9Sstevel@tonic-gate static int tags = 0;
397c478bd9Sstevel@tonic-gate char *sinput, *soutput, *tagout;
407c478bd9Sstevel@tonic-gate long indexdate = 0, gdate();
417c478bd9Sstevel@tonic-gate 
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();
52*11a8fa6cSceastha 
53*11a8fa6cSceastha static int setfrom(char);
54*11a8fa6cSceastha 
55*11a8fa6cSceastha int
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];
627c478bd9Sstevel@tonic-gate 	static char oldname[30];
637c478bd9Sstevel@tonic-gate 	static int was = 0;
647c478bd9Sstevel@tonic-gate 	/* these pointers are unions of pointer to int and pointer to long */
657c478bd9Sstevel@tonic-gate 	long *hpt;
667c478bd9Sstevel@tonic-gate 	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)
747c478bd9Sstevel@tonic-gate #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 */
837c478bd9Sstevel@tonic-gate 			falseflg = 1;
847c478bd9Sstevel@tonic-gate 			break;
857c478bd9Sstevel@tonic-gate 		case 'r':
867c478bd9Sstevel@tonic-gate 			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 */
977c478bd9Sstevel@tonic-gate 			argc--;
987c478bd9Sstevel@tonic-gate 			argv++;
997c478bd9Sstevel@tonic-gate 			sinput = argv[1];
1007c478bd9Sstevel@tonic-gate 			break;
1017c478bd9Sstevel@tonic-gate 		case 's': /* text output to string */
1027c478bd9Sstevel@tonic-gate 		case 'o':
1037c478bd9Sstevel@tonic-gate 			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];
1087c478bd9Sstevel@tonic-gate 				argc--;
1097c478bd9Sstevel@tonic-gate 				argv++;
1107c478bd9Sstevel@tonic-gate 			}
1117c478bd9Sstevel@tonic-gate 			break;
1127c478bd9Sstevel@tonic-gate 		case 't': /* tag output to string */
1137c478bd9Sstevel@tonic-gate 			argc--;
1147c478bd9Sstevel@tonic-gate 			argv++;
1157c478bd9Sstevel@tonic-gate 			tagout = argv[1];
1167c478bd9Sstevel@tonic-gate 			break;
1177c478bd9Sstevel@tonic-gate 		case 'l': /* length of internal lists */
1187c478bd9Sstevel@tonic-gate 			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);
1277c478bd9Sstevel@tonic-gate #if D1
1287c478bd9Sstevel@tonic-gate 			fprintf(stderr, "colevel set to %d\n", colevel);
1297c478bd9Sstevel@tonic-gate #endif
1307c478bd9Sstevel@tonic-gate 			break;
1317c478bd9Sstevel@tonic-gate 		case 'P': /* print term freqs */
1327c478bd9Sstevel@tonic-gate 			prfreqs = 1;
1337c478bd9Sstevel@tonic-gate 			break;
1347c478bd9Sstevel@tonic-gate 		case 'm':
1357c478bd9Sstevel@tonic-gate 			measure = 1;
1367c478bd9Sstevel@tonic-gate 			break;
1377c478bd9Sstevel@tonic-gate 		}
1387c478bd9Sstevel@tonic-gate 		argc--;
1397c478bd9Sstevel@tonic-gate 		argv++;
1407c478bd9Sstevel@tonic-gate 	}
1417c478bd9Sstevel@tonic-gate 	if (argc < 2)
1427c478bd9Sstevel@tonic-gate 		exit(1);
1437c478bd9Sstevel@tonic-gate 	strcpy(nma, todir(argv[1]));
144*11a8fa6cSceastha 	if (was == 0 || strcmp(oldname, nma) != 0) {
1457c478bd9Sstevel@tonic-gate 		strcpy(oldname, nma);
1467c478bd9Sstevel@tonic-gate 		strcpy(nmb, nma);
1477c478bd9Sstevel@tonic-gate 		strcpy(nmc, nmb);
1487c478bd9Sstevel@tonic-gate 		strcpy(nmd, nma);
1497c478bd9Sstevel@tonic-gate 		strcat(nma, ".ia");
1507c478bd9Sstevel@tonic-gate 		strcat(nmb, ".ib");
1517c478bd9Sstevel@tonic-gate 		strcat(nmc, ".ic");
1527c478bd9Sstevel@tonic-gate 		strcat(nmd, ".id");
153*11a8fa6cSceastha 		if (was) {
1547c478bd9Sstevel@tonic-gate 			fclose(fa);
1557c478bd9Sstevel@tonic-gate 			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);
1637c478bd9Sstevel@tonic-gate 			fb = NULL;
1647c478bd9Sstevel@tonic-gate 			goto search;
1657c478bd9Sstevel@tonic-gate 		}
1667c478bd9Sstevel@tonic-gate 		fb = fopen(nmb, "r");
1677c478bd9Sstevel@tonic-gate 		fc = fopen(nmc, "r");
1687c478bd9Sstevel@tonic-gate 		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 	}
1767c478bd9Sstevel@tonic-gate 	fseek(fa, 0L, 0);
1777c478bd9Sstevel@tonic-gate 	fread(&nhash, sizeof (nhash), 1, fa);
1787c478bd9Sstevel@tonic-gate 	fread(&iflong, sizeof (iflong), 1, fa);
1797c478bd9Sstevel@tonic-gate 	if (master == 0)
180*11a8fa6cSceastha 		master = (unsigned *)calloc(lmaster, iflong ?
181*11a8fa6cSceastha 		    sizeof (long) : sizeof (unsigned));
1827c478bd9Sstevel@tonic-gate 	hpt = (long *)calloc(nhash, sizeof (*hpt));
1837c478bd9Sstevel@tonic-gate 	kk = fread(hpt, sizeof (*hpt), nhash, fa);
1847c478bd9Sstevel@tonic-gate #if D1
185*11a8fa6cSceastha 	fprintf(stderr, "read %d hashes, iflong %d, nhash %d\n",
186*11a8fa6cSceastha 	    kk, iflong, nhash);
1877c478bd9Sstevel@tonic-gate #endif
1887c478bd9Sstevel@tonic-gate 	assert(kk == nhash);
1897c478bd9Sstevel@tonic-gate 	hfreq = (int *)calloc(nhash, sizeof (*hfreq));
1907c478bd9Sstevel@tonic-gate 	assert(hfreq != NULL);
1917c478bd9Sstevel@tonic-gate 	frtbl = fread(hfreq, sizeof (*hfreq), nhash, fa);
1927c478bd9Sstevel@tonic-gate 	hfrflg = (frtbl == nhash);
1937c478bd9Sstevel@tonic-gate #if D1
1947c478bd9Sstevel@tonic-gate 	fprintf(stderr, "read freqs %d\n", frtbl);
1957c478bd9Sstevel@tonic-gate #endif
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate search:
198*11a8fa6cSceastha 	while (1) {
1997c478bd9Sstevel@tonic-gate 		nitem = getq(qitem);
2007c478bd9Sstevel@tonic-gate 		if (measure) tick();
2017c478bd9Sstevel@tonic-gate 		if (nitem == 0) continue;
2027c478bd9Sstevel@tonic-gate 		if (nitem < 0) break;
2037c478bd9Sstevel@tonic-gate 		if (tagout) tagout[0] = 0;
204*11a8fa6cSceastha 		if (fb != NULL) {
2057c478bd9Sstevel@tonic-gate 			nfound = doquery(hpt, nhash, fb, nitem, qitem, master);
2067c478bd9Sstevel@tonic-gate #if D1
2077c478bd9Sstevel@tonic-gate 			fprintf(stderr, "after doquery nfound %d\n", nfound);
2087c478bd9Sstevel@tonic-gate #endif
2097c478bd9Sstevel@tonic-gate 			fgnamp = fgnames;
2107c478bd9Sstevel@tonic-gate 			if (falseflg == 0)
211*11a8fa6cSceastha 				nfound = baddrop(master, nfound, fc,
212*11a8fa6cSceastha 				    nitem, qitem, rprog, full);
2137c478bd9Sstevel@tonic-gate #if D1
2147c478bd9Sstevel@tonic-gate 			fprintf(stderr, "after baddrop nfound %d\n", nfound);
2157c478bd9Sstevel@tonic-gate #endif
2167c478bd9Sstevel@tonic-gate 		}
217*11a8fa6cSceastha 		if (fgnamp > fgnames) {
2187c478bd9Sstevel@tonic-gate 			char **fgp, tgbuff[100];
2197c478bd9Sstevel@tonic-gate 			int k;
2207c478bd9Sstevel@tonic-gate #if D1
2217c478bd9Sstevel@tonic-gate 			fprintf(stderr, "were %d bad files\n", fgnamp-fgnames);
2227c478bd9Sstevel@tonic-gate #endif
2237c478bd9Sstevel@tonic-gate 			(void) memset(tgbuff, 0, sizeof (tgbuff));
2247c478bd9Sstevel@tonic-gate 			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 			}
2297c478bd9Sstevel@tonic-gate #if D1
2307c478bd9Sstevel@tonic-gate 			fprintf(stderr, "grepquery %s\n", grepquery);
2317c478bd9Sstevel@tonic-gate #endif
232*11a8fa6cSceastha 			for (fgp = fgnames; fgp < fgnamp; fgp++) {
2337c478bd9Sstevel@tonic-gate #if D1
234*11a8fa6cSceastha 				fprintf(stderr, "Now on %s query /%s/\n",
235*11a8fa6cSceastha 				    *fgp, grepquery);
2367c478bd9Sstevel@tonic-gate #endif
2377c478bd9Sstevel@tonic-gate 				makefgrep(*fgp);
2387c478bd9Sstevel@tonic-gate #if D1
2397c478bd9Sstevel@tonic-gate 				fprintf(stderr, "grepmade\n");
2407c478bd9Sstevel@tonic-gate #endif
2417c478bd9Sstevel@tonic-gate 				if (tagout == 0)
2427c478bd9Sstevel@tonic-gate 					tagout = tgbuff;
2437c478bd9Sstevel@tonic-gate 				grepcall(grepquery, tagout, *fgp);
2447c478bd9Sstevel@tonic-gate #if D1
2457c478bd9Sstevel@tonic-gate 				fprintf(stderr, "tagout now /%s/\n", tagout);
2467c478bd9Sstevel@tonic-gate #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++;
2587c478bd9Sstevel@tonic-gate 						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);
2647c478bd9Sstevel@tonic-gate 							free(bout);
2657c478bd9Sstevel@tonic-gate 						}
2667c478bd9Sstevel@tonic-gate 					}
2677c478bd9Sstevel@tonic-gate 				}
2687c478bd9Sstevel@tonic-gate 			}
2697c478bd9Sstevel@tonic-gate 		}
2707c478bd9Sstevel@tonic-gate 		if (tags)
2717c478bd9Sstevel@tonic-gate 			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 *
278*11a8fa6cSceastha todir(char *t)
2797c478bd9Sstevel@tonic-gate {
2807c478bd9Sstevel@tonic-gate 	char *s;
2817c478bd9Sstevel@tonic-gate 	s = t;
2827c478bd9Sstevel@tonic-gate 	while (*s) s++;
2837c478bd9Sstevel@tonic-gate 	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 : "/");
2887c478bd9Sstevel@tonic-gate 	chdir(t);
2897c478bd9Sstevel@tonic-gate 	strcpy(usedir, t);
2907c478bd9Sstevel@tonic-gate 	return (s);
2917c478bd9Sstevel@tonic-gate }
292*11a8fa6cSceastha 
293*11a8fa6cSceastha static int
294*11a8fa6cSceastha setfrom(char c)
2957c478bd9Sstevel@tonic-gate {
296*11a8fa6cSceastha 	switch (c) {
2977c478bd9Sstevel@tonic-gate 	case 'y':
2987c478bd9Sstevel@tonic-gate 	case '\0':
2997c478bd9Sstevel@tonic-gate 	default:
3007c478bd9Sstevel@tonic-gate 		return (1000);
3017c478bd9Sstevel@tonic-gate 	case '1':
3027c478bd9Sstevel@tonic-gate 	case '2':
3037c478bd9Sstevel@tonic-gate 	case '3':
3047c478bd9Sstevel@tonic-gate 	case '4':
3057c478bd9Sstevel@tonic-gate 	case '5':
3067c478bd9Sstevel@tonic-gate 	case '6':
3077c478bd9Sstevel@tonic-gate 	case '7':
3087c478bd9Sstevel@tonic-gate 	case '8':
3097c478bd9Sstevel@tonic-gate 	case '9':
3107c478bd9Sstevel@tonic-gate 		return (c-'0');
3117c478bd9Sstevel@tonic-gate 	case 'n':
3127c478bd9Sstevel@tonic-gate 	case '0':
3137c478bd9Sstevel@tonic-gate 		return (0);
3147c478bd9Sstevel@tonic-gate 	}
3157c478bd9Sstevel@tonic-gate }
316