1 /* $MirOS: src/usr.bin/oldroff/refer/glue1.c,v 1.2 2008/11/08 23:04:43 tg Exp $ */
2 
3 /*-
4  * Copyright (c) 1979, 1980, 1981, 1986, 1988, 1990, 1991, 1992
5  *     The Regents of the University of California.
6  * Copyright (C) Caldera International Inc.  2001-2002.
7  * Copyright (c) 2003, 2004
8  *	Thorsten "mirabilos" Glaser <tg@mirbsd.org>
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms,
12  * with or without modification, are permitted provided
13  * that the following conditions are met:
14  *
15  * Redistributions of source code and documentation must retain
16  * the above copyright notice, this list of conditions and the
17  * following disclaimer.  Redistributions in binary form must
18  * reproduce the above copyright notice, this list of conditions
19  * and the following disclaimer in the documentation and/or other
20  * materials provided with the distribution.
21  *
22  * All advertising materials mentioning features or use of this
23  * software must display the following acknowledgement:
24  *   This product includes software developed or owned by
25  *   Caldera International, Inc.
26  *
27  * Neither the name of Caldera International, Inc. nor the names
28  * of other contributors may be used to endorse or promote products
29  * derived from this software without specific prior written permission.
30  *
31  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
32  * INTERNATIONAL, INC. AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
33  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
34  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35  * ARE DISCLAIMED.  IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE
36  * LIABLE FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR
37  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
39  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
40  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
41  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
42  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43  */
44 
45 #ifndef lint
46 static char sccsid[] = "@(#)glue1.c	4.5 (Berkeley) 4/18/91";
47 #endif /* not lint */
48 
49 #include <stdio.h>
50 #define unopen(fil) {if (fil!=NULL) {fclose(fil); fil=NULL;}}
51 
52 extern char refdir[];
53 int lmaster = 1000;
54 int reached = 0;
55 FILE *fd = 0;
56 int *hfreq, hfrflg;
57 int colevel = 0;
58 unsigned *master = 0;
59 int iflong;
60 extern char *fgnames[], **fgnamp;
61 extern FILE *iopen();
62 char *todir();
63 int prfreqs = 0;
64 int typeindex = 0;
65 char usedir[100];
66 static int full = 1000;
67 static int tags = 0;
68 char *sinput, *soutput, *tagout;
69 long indexdate = 0, gdate();
70 int soutlen = 1000;
71 int taglen = 1000;
72 
huntmain(argc,argv)73 huntmain(argc,argv)
74 char *argv[];
75 {
76 	/* read query from stdin, expect name of indexes in argv[1] */
77 	static FILE *fa, *fb, *fc;
78 	char indexname[100], *qitem[100], *rprog = 0;
79 	char grepquery[200];
80 	static char oldname[30] ;
81 	static int nhash = 0;
82 	static int maxhash = 0;
83 	int falseflg = 0, nitem, nfound, frtbl;
84 	static long *hpt = 0;
85 # if D1
86 	fprintf(stderr, "in glue1 argc %d argv %o %o\n", argc, argv[0],argv[1]);
87 # endif
88 	savedir();
89 	while (argv[1][0] == '-')
90 	{
91 # if D1
92 		fprintf(stderr, "argv.1 is %s\n",argv[1]);
93 # endif
94 		switch(argv[1][1])
95 		{
96 		case 'a': /* all output, incl. false drops */
97 			falseflg = 1;
98 			break;
99 		case 'r':
100 			argc--;
101 			argv++;
102 			rprog = argv[1];
103 			break;
104 		case 'F': /* put out full text */
105 			full = setfrom(argv[1][2]);
106 			break;
107 		case 'T': /* put out tags */
108 			tags = setfrom(argv[1][2]);
109 			break;
110 		case 'i': /* input in argument string */
111 			argc--;
112 			argv++;
113 			sinput = argv[1];
114 			break;
115 		case 's': /*text output to string */
116 		case 'o':
117 			argc--;
118 			argv++;
119 			soutput = argv[1];
120 			if ((int) argv[2]<16000)
121 			{
122 				soutlen = (int) argv[2];
123 				argc--;
124 				argv++;
125 			}
126 			break;
127 		case 't': /*tag output to string */
128 			argc--;
129 			argv++;
130 			tagout = argv[1];
131 			if ((int)argv[2]<16000)
132 			{
133 				taglen = (int)argv[2];
134 				argc--;
135 				argv++;
136 			}
137 			break;
138 		case 'l': /* specify length of lists */
139 			argc--;
140 			argv++;
141 			lmaster = atoi(argv[1]);
142 # if D1
143 			fprintf(stderr, "lmaster now %d\n",lmaster);
144 # endif
145 			break;
146 		case 'C':
147 			argc--;
148 			argv++;
149 			colevel = atoi(argv[1]);
150 			break;
151 		}
152 		argc--;
153 		argv++;
154 	}
155 	strcpy (indexname, todir(argv[1]));
156 # if D1
157 	fprintf(stderr, "in huntmain indexname %s typeindex %d\n", indexname, typeindex);
158 # endif
159 	if (typeindex == 0 || strcmp (oldname, indexname) !=0)
160 	{
161 		strcpy (oldname, indexname);
162 		unopen(fa);
163 		unopen(fb);
164 		unopen(fc);
165 
166 		if (ckexist(indexname, ".ib"))
167 		{
168 # if D1
169 			fprintf(stderr, "found old index\n");
170 # endif
171 			fa = iopen(indexname, ".ia");
172 			fb = iopen(indexname, ".ib");
173 			fc = iopen(indexname, ".ic");
174 			typeindex =1;
175 # if D1
176 			fprintf(stderr, "opened f's as %o %o %o\n",fa,fb,fc);
177 # endif
178 			indexdate = gdate(fb);
179 			fread (&nhash, sizeof(nhash), 1, fa);
180 			fread (&iflong, sizeof(iflong), 1, fa);
181 			if (nhash > maxhash)
182 			{
183 				if (hpt)
184 					free (hpt, maxhash, sizeof(*hpt));
185 				hpt=0;
186 				if (hfreq)
187 					free(hfreq, maxhash, sizeof(*hfreq));
188 				hfreq=0;
189 				maxhash=nhash;
190 # if D1
191 				fprintf(stderr, "Freed if needed maxhash %d\n",maxhash);
192 # endif
193 			}
194 			if (hpt==0)
195 				hpt = (long *) zalloc(nhash, sizeof(*hpt));
196 # if D1
197 			fprintf(stderr, "hpt now %o\n",hpt);
198 # endif
199 			if (hpt == NULL)
200 				err ("No space for hash list (%d)", nhash);
201 			fread( hpt, sizeof(*hpt), nhash, fa);
202 			if (hfreq==0)
203 				hfreq=(int *)zalloc(nhash, sizeof(*hfreq));
204 			if (hfreq==NULL)
205 				err ("No space for hash frequencies (%d)", nhash);
206 			frtbl = fread(hfreq, sizeof(*hfreq), nhash, fa);
207 			hfrflg = (frtbl == nhash);
208 # if D1
209 			fprintf(stderr,"Read pointer files\n");
210 # endif
211 			if(master==0)
212 				master = (unsigned *) zalloc (lmaster, iflong? sizeof(long): sizeof(unsigned));
213 			if (master == NULL)
214 				err ("no space for answer list",0);
215 		}
216 		else
217 			if (makefgrep(indexname))
218 				typeindex=2;
219 			else
220 			{
221 				err ("No files %s\n",indexname);
222 				exit(1);
223 			}
224 	}
225 
226 # if D1
227 	fprintf(stderr, "typeindex now %d\n",typeindex);
228 # endif
229 	tagout[0]=0;
230 	if (typeindex==2)
231 	{
232 		grepcall(sinput, tagout, indexname);
233 # if D1
234 		fprintf(stderr, " back from grepcall\n");
235 # endif
236 		restodir();
237 		return;
238 	}
239 	nitem = getq(qitem);
240 # if D1
241 	fprintf(stderr, "approaching doquery fb %o\n", fb);
242 # endif
243 	nfound = doquery(hpt, nhash, fb, nitem, qitem, master);
244 # ifdef D1
245 	fprintf(stderr, "return from doquery with nfound %d\n", nfound);
246 # endif
247 	if (falseflg == 0)
248 		nfound = baddrop(master, nfound, fc, nitem, qitem, rprog, full);
249 # ifdef D1
250 	fprintf(stderr, "after baddrop with nfound %d\n",nfound);
251 	fprintf(stderr, "tagout is /%s/, sout /%s/\n",tagout, soutput);
252 # endif
253 	if (tags)
254 		result (master, nfound >tags ? tags : nfound, fc);
255 # if D1
256 	fprintf(stderr, "done with huntmain\n");
257 	fprintf(stderr, "tagout is /%s/\n", tagout);
258 	fprintf(stderr, "string out is /%s/\n", soutput);
259 # endif
260 	if (fgnamp>fgnames)
261 	{
262 		char **fgp;
263 		int k;
264 # if D1
265 		fprintf(stderr, "were %d bad files\n", fgnamp-fgnames);
266 # endif
267 		grepquery[0]=0;
268 		for(k=0; k<nitem; k++)
269 		{
270 			strcat(grepquery, " ");
271 			strcat(grepquery, qitem[k]);
272 		}
273 		for(fgp=fgnames; fgp<fgnamp; fgp++)
274 		{
275 # if D1
276 			fprintf(stderr, "Now on %s query /%s/\n", *fgp, grepquery);
277 # endif
278 			makefgrep(*fgp);
279 			grepcall(grepquery, tagout, *fgp);
280 # if D1
281 			fprintf(stderr, "tagout now /%s/\n", tagout);
282 # endif
283 		}
284 	}
285 	restodir();
286 }
287 
288 char *
todir(t)289 todir(t)
290 char *t;
291 {
292 	char *s;
293 
294 	usedir[0] = 0;
295 	s=t;
296 	while (*s) s++;
297 	while (s>=t && *s != '/') s--;
298 	if (s<t) return(t);
299 	*s++ = 0;
300 	t = (*t ? t : "/");
301 	chdir (t);
302 	strcpy (usedir,t);
303 	return(s);
304 }
305 
setfrom(c)306 setfrom(c)
307 {
308 	switch(c)
309 	{
310 	case 'y':
311 	case '\0':
312 	default:
313 		return(1000);
314 	case '1':
315 	case '2':
316 	case '3':
317 	case '4':
318 	case '5':
319 	case '6':
320 	case '7':
321 	case '8':
322 	case '9':
323 		return(c-'0');
324 	case 'n':
325 	case '0':
326 		return(0);
327 	}
328 }
329