xref: /illumos-gate/usr/src/cmd/refer/glue1.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
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 
17 #pragma ident	"%Z%%M%	%I%	%E% SMI"
18 
19 #include <stdio.h>
20 #define unopen(fil) {if (fil!=NULL) {fclose(fil); fil=NULL;}}
21 
22 extern char refdir[];
23 int lmaster = 1000;
24 int reached = 0;
25 FILE *fd = 0;
26 int *hfreq, hfrflg;
27 int colevel = 0;
28 static union firetruck {
29 	unsigned *a;
30 	long *b;
31 } master;
32 int iflong;
33 extern char *fgnames[], **fgnamp;
34 extern FILE *iopen();
35 char *todir();
36 int prfreqs = 0;
37 int typeindex = 0;
38 char usedir[100];
39 static int full = 1000;
40 static int tags = 0;
41 char *sinput, *soutput, *tagout;
42 long indexdate = 0, gdate();
43 int soutlen = 1000;
44 int taglen = 1000;
45 
46 huntmain(argc,argv)
47 char *argv[];
48 {
49 	/* read query from stdin, expect name of indexes in argv[1] */
50 	static FILE *fa, *fb, *fc;
51 	char indexname[100], *qitem[100], *rprog = 0;
52 	char grepquery[200];
53 	static char oldname[30] ;
54 	static int nhash = 0;
55 	static int maxhash = 0;
56 	int falseflg = 0, nitem, nfound, frtbl;
57 	static long *hpt = 0;
58 	unsigned *masterp;
59 
60 # if D1
61 	fprintf(stderr, "in glue1 argc %d argv %o %o\n", argc, argv[0],argv[1]);
62 # endif
63 	savedir();
64 	while (argv[1][0] == '-')
65 	{
66 # if D1
67 		fprintf(stderr, "argv.1 is %s\n",argv[1]);
68 # endif
69 		switch(argv[1][1])
70 		{
71 		case 'a': /* all output, incl. false drops */
72 			falseflg = 1;
73 			break;
74 		case 'r':
75 			argc--;
76 			argv++;
77 			rprog = argv[1];
78 			break;
79 		case 'F': /* put out full text */
80 			full = setfrom(argv[1][2]);
81 			break;
82 		case 'T': /* put out tags */
83 			tags = setfrom(argv[1][2]);
84 			break;
85 		case 'i': /* input in argument string */
86 			argc--;
87 			argv++;
88 			sinput = argv[1];
89 			break;
90 		case 's': /*text output to string */
91 		case 'o':
92 			argc--;
93 			argv++;
94 			soutput = argv[1];
95 			if ((int) argv[2]<16000)
96 			{
97 				soutlen = (int) argv[2];
98 				argc--;
99 				argv++;
100 			}
101 			break;
102 		case 't': /*tag output to string */
103 			argc--;
104 			argv++;
105 			tagout = argv[1];
106 			if ((int)argv[2]<16000)
107 			{
108 				taglen = (int)argv[2];
109 				argc--;
110 				argv++;
111 			}
112 			break;
113 		case 'l': /* specify length of lists */
114 			argc--;
115 			argv++;
116 			lmaster = atoi(argv[1]);
117 # if D1
118 			fprintf(stderr, "lmaster now %d\n",lmaster);
119 # endif
120 			break;
121 		case 'C':
122 			argc--;
123 			argv++;
124 			colevel = atoi(argv[1]);
125 			break;
126 		}
127 		argc--;
128 		argv++;
129 	}
130 	strcpy (indexname, todir(argv[1]));
131 # if D1
132 	fprintf(stderr, "in huntmain indexname %s typeindex %d\n", indexname, typeindex);
133 # endif
134 	if (typeindex == 0 || strcmp (oldname, indexname) !=0)
135 	{
136 		strcpy (oldname, indexname);
137 		unopen(fa);
138 		unopen(fb);
139 		unopen(fc);
140 
141 		if (ckexist(indexname, ".ib"))
142 		{
143 # if D1
144 			fprintf(stderr, "found old index\n");
145 # endif
146 			fa = iopen(indexname, ".ia");
147 			fb = iopen(indexname, ".ib");
148 			fc = iopen(indexname, ".ic");
149 			typeindex =1;
150 # if D1
151 			fprintf(stderr, "opened f's as %o %o %o\n",fa,fb,fc);
152 # endif
153 			indexdate = gdate(fb);
154 			fread (&nhash, sizeof(nhash), 1, fa);
155 			fread (&iflong, sizeof(iflong), 1, fa);
156 			if (nhash > maxhash)
157 			{
158 				if (hpt)
159 					free (hpt, maxhash, sizeof(*hpt));
160 				hpt=0;
161 				if (hfreq)
162 					free(hfreq, maxhash, sizeof(*hfreq));
163 				hfreq=0;
164 				maxhash=nhash;
165 # if D1
166 				fprintf(stderr, "Freed if needed maxhash %d\n",maxhash);
167 # endif
168 			}
169 			if (hpt==0)
170 				hpt = (long *) zalloc(nhash, sizeof(*hpt));
171 # if D1
172 			fprintf(stderr, "hpt now %o\n",hpt);
173 # endif
174 			if (hpt == NULL)
175 				/*
176 				 * TRANSLATION_NOTE
177 				 * %d is the size of the hash table - not
178 				 * very interesting info for the end users.
179 				 * Hash is a computer science terminology.
180 				 */
181 				err(gettext("No space for hash list (%d)"),
182 					    nhash);
183 			fread( hpt, sizeof(*hpt), nhash, fa);
184 			if (hfreq==0)
185 				hfreq=(int *)zalloc(nhash, sizeof(*hfreq));
186 			if (hfreq==NULL)
187 				/*
188 				 * TRANSLATION_NOTE
189 				 * %d is the size of the hash table.
190 				 */
191 				err(gettext(
192 				    "No space for hash frequencies (%d)"),
193 				    nhash);
194 			frtbl = fread(hfreq, sizeof(*hfreq), nhash, fa);
195 			hfrflg = (frtbl == nhash);
196 # if D1
197 			fprintf(stderr,"Read pointer files\n");
198 # endif
199 			if (master.a == NULL)
200 				if (iflong)
201 					master.b = (long *)zalloc(lmaster, sizeof(long));
202 				else
203 					master.a = (unsigned *)zalloc(lmaster, sizeof(int));
204 			if (master.a == NULL)
205 				err(gettext("no space for answer list"), 0);
206 		}
207 		else
208 			if (makefgrep(indexname))
209 				typeindex=2;
210 			else
211 			{
212 				err(gettext("No files %s\n"), indexname);
213 				exit(1);
214 			}
215 	}
216 
217 	if (iflong)
218 		masterp = (unsigned *) master.b;
219 	else
220 		masterp = master.a;
221 
222 # if D1
223 	fprintf(stderr, "typeindex now %d\n",typeindex);
224 # endif
225 	tagout[0]=0;
226 	if (typeindex==2)
227 	{
228 		grepcall(sinput, tagout, indexname);
229 # if D1
230 		fprintf(stderr, " back from grepcall\n");
231 # endif
232 		restodir();
233 		return;
234 	}
235 	nitem = getq(qitem);
236 # if D1
237 	fprintf(stderr, "approaching doquery fb %o\n", fb);
238 # endif
239 	nfound = doquery(hpt, nhash, fb, nitem, qitem, masterp);
240 # ifdef D1
241 	fprintf(stderr, "return from doquery with nfound %d\n", nfound);
242 # endif
243 	if (falseflg == 0)
244 		nfound = baddrop(masterp, nfound, fc, nitem, qitem, rprog, full);
245 # ifdef D1
246 	fprintf(stderr, "after baddrop with nfound %d\n",nfound);
247 	fprintf(stderr, "tagout is /%s/, sout /%s/\n",tagout, soutput);
248 # endif
249 	if (tags)
250 		result (masterp, nfound >tags ? tags : nfound, fc);
251 # if D1
252 	fprintf(stderr, "done with huntmain\n");
253 	fprintf(stderr, "tagout is /%s/\n", tagout);
254 	fprintf(stderr, "string out is /%s/\n", soutput);
255 # endif
256 	if (fgnamp>fgnames)
257 	{
258 		char **fgp;
259 		int k;
260 # if D1
261 		fprintf(stderr, "were %d bad files\n", fgnamp-fgnames);
262 # endif
263 		grepquery[0]=0;
264 		for(k=0; k<nitem; k++)
265 		{
266 			strcat(grepquery, " ");
267 			strcat(grepquery, qitem[k]);
268 		}
269 		for(fgp=fgnames; fgp<fgnamp; fgp++)
270 		{
271 # if D1
272 			fprintf(stderr, "Now on %s query /%s/\n", *fgp, grepquery);
273 # endif
274 			makefgrep(*fgp);
275 			grepcall(grepquery, tagout, *fgp);
276 # if D1
277 			fprintf(stderr, "tagout now /%s/\n", tagout);
278 # endif
279 		}
280 	}
281 	restodir();
282 }
283 
284 char *
285 todir(t)
286 char *t;
287 {
288 	char *s;
289 
290 	usedir[0] = 0;
291 	s=t;
292 	while (*s) s++;
293 	while (s>=t && *s != '/') s--;
294 	if (s<t) return(t);
295 	*s++ = 0;
296 	t = (*t ? t : "/");
297 	chdir (t);
298 	strcpy (usedir,t);
299 	return(s);
300 }
301 
302 setfrom(c)
303 {
304 	switch(c)
305 	{
306 	case 'y':
307 	case '\0':
308 	default:
309 		return(1000);
310 	case '1':
311 	case '2':
312 	case '3':
313 	case '4':
314 	case '5':
315 	case '6':
316 	case '7':
317 	case '8':
318 	case '9':
319 		return(c-'0');
320 	case 'n':
321 	case '0':
322 		return(0);
323 	}
324 }
325