fastfind.c revision 19060
1/*
2 * Copyright (c) 1995 Wolfram Schneider <wosch@FreeBSD.org>. Berlin.
3 * Copyright (c) 1989, 1993
4 *	The Regents of the University of California.  All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * James A. Woods.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 *    must display the following acknowledgement:
19 *	This product includes software developed by the University of
20 *	California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 *    may be used to endorse or promote products derived from this software
23 *    without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * $Id: fastfind.c,v 1.3 1996/10/13 01:44:34 wosch Exp $
38 */
39
40
41#ifndef _LOCATE_STATISTIC_
42#define _LOCATE_STATISTIC_
43
44void
45statistic (fp, path_fcodes)
46	FILE *fp;               /* open database */
47	char *path_fcodes;  	/* for error message */
48{
49	register int lines, chars, size, big, zwerg;
50	register u_char *p, *s;
51	register int c;
52	int count, umlaut;
53	u_char bigram1[NBG], bigram2[NBG], path[MAXPATHLEN];
54
55	for (c = 0, p = bigram1, s = bigram2; c < NBG; c++) {
56		p[c] = check_bigram_char(getc(fp));
57		s[c] = check_bigram_char(getc(fp));
58	}
59
60	lines = chars = big = zwerg = umlaut = 0;
61	size = NBG + NBG;
62
63	for (c = getc(fp), count = 0; c != EOF; size++) {
64		if (c == SWITCH) {
65			count += getwf(fp) - OFFSET;
66			size += sizeof(int);
67			zwerg++;
68		} else
69			count += c - OFFSET;
70
71		for (p = path + count; (c = getc(fp)) > SWITCH; size++)
72			if (c < PARITY) {
73				if (c == UMLAUT) {
74					c = getc(fp);
75					size++;
76					umlaut++;
77				}
78				p++;
79			} else {
80				/* bigram char */
81				big++;
82				p += 2;
83			}
84
85		p++;
86		lines++;
87		chars += (p - path);
88	}
89
90	(void)printf("\nDatabase: %s\n", path_fcodes);
91	(void)printf("Compression: Front: %2.2f%%, ",
92		     (float)(100 * (size + big - (2 * NBG))) / chars);
93	(void)printf("Bigram: %2.2f%%, ", (float)(100 * (size - big)) / size);
94	(void)printf("Total: %2.2f%%\n",
95		     (float)(100 * (size - (2 * NBG))) / chars);
96	(void)printf("Filenames: %d, ", lines);
97	(void)printf("Characters: %d, ", chars);
98	(void)printf("Database size: %d\n", size);
99	(void)printf("Bigram characters: %d, ", big);
100	(void)printf("Integers: %d, ", zwerg);
101	(void)printf("8-Bit characters: %d\n", umlaut);
102
103}
104#endif /* _LOCATE_STATISTIC_ */
105
106
107void
108#ifdef FF_MMAP
109
110
111#ifdef FF_ICASE
112fastfind_mmap_icase
113#else
114fastfind_mmap
115#endif /* FF_ICASE */
116(pathpart, paddr, len, database)
117	char *pathpart; 	/* search string */
118	caddr_t paddr;  	/* mmap pointer */
119	int len;        	/* length of database */
120	char *database; 	/* for error message */
121
122
123#else /* MMAP */
124
125
126#ifdef FF_ICASE
127fastfind_icase
128#else
129fastfind
130#endif /* FF_ICASE */
131
132(fp, pathpart, database)
133	FILE *fp;               /* open database */
134	char *pathpart;		/* search string */
135	char *database;		/* for error message */
136
137
138#endif /* MMAP */
139
140{
141	register u_char *p, *s, *patend, *q, *foundchar;
142	register int c, cc;
143	int count, found, globflag;
144	u_char *cutoff;
145	u_char bigram1[NBG], bigram2[NBG], path[MAXPATHLEN];
146
147#ifdef FF_ICASE
148	/* use a lookup table for case insensitive search */
149	u_char table[UCHAR_MAX + 1];
150
151	tolower_word(pathpart);
152#endif /* FF_ICASE*/
153
154	/* init bigram table */
155#ifdef FF_MMAP
156	if (len < (2*NBG)) {
157		(void)fprintf(stderr, "database to small: %s\n", database);
158		exit(1);
159	}
160
161	for (c = 0, p = bigram1, s = bigram2; c < NBG; c++, len-= 2) {
162		p[c] = check_bigram_char(*paddr++);
163		s[c] = check_bigram_char(*paddr++);
164	}
165#else
166	for (c = 0, p = bigram1, s = bigram2; c < NBG; c++) {
167		p[c] = check_bigram_char(getc(fp));
168		s[c] = check_bigram_char(getc(fp));
169	}
170#endif /* FF_MMAP */
171
172	/* find optimal (last) char for searching */
173	for (p = pathpart; *p != '\0'; p++)
174		if (index(LOCATE_REG, *p) != NULL)
175			break;
176
177	if (*p == '\0')
178		globflag = 0;
179	else
180		globflag = 1;
181
182	p = pathpart;
183	patend = patprep(p);
184	cc = *patend;
185
186#ifdef FF_ICASE
187	/* set patend char to true */
188	table[TOLOWER(*patend)] = 1;
189	table[toupper(*patend)] = 1;
190#endif /* FF_ICASE */
191
192
193	/* main loop */
194	found = count = 0;
195	foundchar = 0;
196
197#ifdef FF_MMAP
198	c = (u_char)*paddr++; len--;
199	for (; len > 0; ) {
200#else
201	c = getc(fp);
202	for (; c != EOF; ) {
203#endif /* FF_MMAP */
204
205		/* go forward or backward */
206		if (c == SWITCH) { /* big step, an integer */
207#ifdef FF_MMAP
208			count += getwm(paddr) - OFFSET;
209			len -= INTSIZE; paddr += INTSIZE;
210#else
211			count +=  getwf(fp) - OFFSET;
212#endif /* FF_MMAP */
213		} else {	   /* slow step, =< 14 chars */
214			count += c - OFFSET;
215		}
216
217		/* overlay old path */
218		p = path + count;
219		foundchar = p - 1;
220
221		for (;;) {
222#ifdef FF_MMAP
223			c = (u_char)*paddr++;
224		        len--;
225#else
226			c = getc(fp);
227#endif /* FF_MMAP */
228			/*
229			 * == UMLAUT: 8 bit char followed
230			 * <= SWITCH: offset
231			 * >= PARITY: bigram
232			 * rest:      single ascii char
233			 *
234			 * offset < SWITCH < UMLAUT < ascii < PARITY < bigram
235			 */
236			if (c < PARITY) {
237				if (c <= UMLAUT) {
238					if (c == UMLAUT) {
239#ifdef FF_MMAP
240						c = (u_char)*paddr++;
241						len--;
242#else
243						c = getc(fp);
244#endif /* FF_MMAP */
245
246					} else
247						break; /* SWITCH */
248				}
249#ifdef FF_ICASE
250				if (table[c])
251#else
252				if (c == cc)
253#endif /* FF_ICASE */
254					foundchar = p;
255				*p++ = c;
256			}
257			else {
258				/* bigrams are parity-marked */
259				TO7BIT(c);
260
261#ifndef FF_ICASE
262				if (bigram1[c] == cc ||
263				    bigram2[c] == cc)
264#else
265
266					if (table[bigram1[c]] ||
267					    table[bigram2[c]])
268#endif /* FF_ICASE */
269						foundchar = p + 1;
270
271				*p++ = bigram1[c];
272				*p++ = bigram2[c];
273			}
274		}
275
276		if (found) {                     /* previous line matched */
277			cutoff = path;
278			*p-- = '\0';
279			foundchar = p;
280		} else if (foundchar >= path + count) { /* a char matched */
281			*p-- = '\0';
282			cutoff = path + count;
283		} else                           /* nothing to do */
284			continue;
285
286		found = 0;
287		for (s = foundchar; s >= cutoff; s--) {
288			if (*s == cc
289#ifdef FF_ICASE
290			    || TOLOWER(*s) == cc
291#endif /* FF_ICASE */
292			    ) {	/* fast first char check */
293				for (p = patend - 1, q = s - 1; *p != '\0';
294				     p--, q--)
295					if (*q != *p
296#ifdef FF_ICASE
297					    && TOLOWER(*q) != *p
298#endif /* FF_ICASE */
299					    )
300						break;
301				if (*p == '\0') {   /* fast match success */
302					found = 1;
303					if (!globflag ||
304#ifndef FF_ICASE
305					    !fnmatch(pathpart, path, 0))
306#else
307					    !fnmatch(pathpart, path,
308						     FNM_ICASE))
309#endif /* !FF_ICASE */
310					{
311						if (f_silent)
312							counter++;
313						else if (f_limit) {
314							counter++;
315							if (f_limit >= counter)
316								(void)puts(path);
317							else  {
318								(void)fprintf(stderr, "[show only %d lines]\n", counter - 1);
319								exit(0);
320							}
321						} else
322							(void)puts(path);
323					}
324					break;
325				}
326			}
327		}
328	}
329}
330