fastfind.c revision 29739
117980Swosch/*
217980Swosch * Copyright (c) 1995 Wolfram Schneider <wosch@FreeBSD.org>. Berlin.
317980Swosch * Copyright (c) 1989, 1993
417980Swosch *	The Regents of the University of California.  All rights reserved.
517980Swosch *
617980Swosch * This code is derived from software contributed to Berkeley by
717980Swosch * James A. Woods.
817980Swosch *
917980Swosch * Redistribution and use in source and binary forms, with or without
1017980Swosch * modification, are permitted provided that the following conditions
1117980Swosch * are met:
1217980Swosch * 1. Redistributions of source code must retain the above copyright
1317980Swosch *    notice, this list of conditions and the following disclaimer.
1417980Swosch * 2. Redistributions in binary form must reproduce the above copyright
1517980Swosch *    notice, this list of conditions and the following disclaimer in the
1617980Swosch *    documentation and/or other materials provided with the distribution.
1717980Swosch * 3. All advertising materials mentioning features or use of this software
1817980Swosch *    must display the following acknowledgement:
1917980Swosch *	This product includes software developed by the University of
2017980Swosch *	California, Berkeley and its contributors.
2117980Swosch * 4. Neither the name of the University nor the names of its contributors
2217980Swosch *    may be used to endorse or promote products derived from this software
2317980Swosch *    without specific prior written permission.
2417980Swosch *
2517980Swosch * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2617980Swosch * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2717980Swosch * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2817980Swosch * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2917980Swosch * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3017980Swosch * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3117980Swosch * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3217980Swosch * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3317980Swosch * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3417980Swosch * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3517980Swosch * SUCH DAMAGE.
3617980Swosch *
3729739Swosch * $Id: fastfind.c,v 1.10 1997/07/21 12:08:13 charnier Exp $
3817980Swosch */
3917980Swosch
4017980Swosch
4117980Swosch#ifndef _LOCATE_STATISTIC_
4217980Swosch#define _LOCATE_STATISTIC_
4317980Swosch
4417980Swoschvoid
4517980Swoschstatistic (fp, path_fcodes)
4617980Swosch	FILE *fp;               /* open database */
4717980Swosch	char *path_fcodes;  	/* for error message */
4817980Swosch{
4918905Swosch	register int lines, chars, size, big, zwerg;
5017980Swosch	register u_char *p, *s;
5117980Swosch	register int c;
5218905Swosch	int count, umlaut;
5317980Swosch	u_char bigram1[NBG], bigram2[NBG], path[MAXPATHLEN];
5417980Swosch
5517980Swosch	for (c = 0, p = bigram1, s = bigram2; c < NBG; c++) {
5617980Swosch		p[c] = check_bigram_char(getc(fp));
5717980Swosch		s[c] = check_bigram_char(getc(fp));
5817980Swosch	}
5917980Swosch
6018905Swosch	lines = chars = big = zwerg = umlaut = 0;
6117980Swosch	size = NBG + NBG;
6217980Swosch
6317980Swosch	for (c = getc(fp), count = 0; c != EOF; size++) {
6417980Swosch		if (c == SWITCH) {
6517980Swosch			count += getwf(fp) - OFFSET;
6617980Swosch			size += sizeof(int);
6718905Swosch			zwerg++;
6817980Swosch		} else
6917980Swosch			count += c - OFFSET;
7017980Swosch
7117980Swosch		for (p = path + count; (c = getc(fp)) > SWITCH; size++)
7218905Swosch			if (c < PARITY) {
7318905Swosch				if (c == UMLAUT) {
7418905Swosch					c = getc(fp);
7518905Swosch					size++;
7618905Swosch					umlaut++;
7718905Swosch				}
7817980Swosch				p++;
7918905Swosch			} else {
8018905Swosch				/* bigram char */
8117980Swosch				big++;
8217980Swosch				p += 2;
8317980Swosch			}
8417980Swosch
8517980Swosch		p++;
8617980Swosch		lines++;
8717980Swosch		chars += (p - path);
8817980Swosch	}
8917980Swosch
9017980Swosch	(void)printf("\nDatabase: %s\n", path_fcodes);
9117980Swosch	(void)printf("Compression: Front: %2.2f%%, ",
9229739Swosch		     (size + big - (2 * NBG)) / (chars / (float)100));
9329739Swosch	(void)printf("Bigram: %2.2f%%, ", (size - big) / (size / (float)100));
9418905Swosch	(void)printf("Total: %2.2f%%\n",
9529739Swosch		     (size - (2 * NBG)) / (chars / (float)100));
9617980Swosch	(void)printf("Filenames: %d, ", lines);
9718905Swosch	(void)printf("Characters: %d, ", chars);
9818905Swosch	(void)printf("Database size: %d\n", size);
9918905Swosch	(void)printf("Bigram characters: %d, ", big);
10018905Swosch	(void)printf("Integers: %d, ", zwerg);
10118905Swosch	(void)printf("8-Bit characters: %d\n", umlaut);
10217980Swosch
10317980Swosch}
10417980Swosch#endif /* _LOCATE_STATISTIC_ */
10517980Swosch
10617980Swosch
10717980Swoschvoid
10817980Swosch#ifdef FF_MMAP
10917980Swosch
11017980Swosch
11117980Swosch#ifdef FF_ICASE
11217980Swoschfastfind_mmap_icase
11317980Swosch#else
11417980Swoschfastfind_mmap
11518905Swosch#endif /* FF_ICASE */
11617980Swosch(pathpart, paddr, len, database)
11717980Swosch	char *pathpart; 	/* search string */
11817980Swosch	caddr_t paddr;  	/* mmap pointer */
11917980Swosch	int len;        	/* length of database */
12017980Swosch	char *database; 	/* for error message */
12117980Swosch
12217980Swosch
12317980Swosch#else /* MMAP */
12417980Swosch
12517980Swosch
12617980Swosch#ifdef FF_ICASE
12717980Swoschfastfind_icase
12818905Swosch#else
12917980Swoschfastfind
13017980Swosch#endif /* FF_ICASE */
13117980Swosch
13217980Swosch(fp, pathpart, database)
13317980Swosch	FILE *fp;               /* open database */
13417980Swosch	char *pathpart;		/* search string */
13517980Swosch	char *database;		/* for error message */
13617980Swosch
13717980Swosch
13817980Swosch#endif /* MMAP */
13917980Swosch
14017980Swosch{
14117980Swosch	register u_char *p, *s, *patend, *q, *foundchar;
14217980Swosch	register int c, cc;
14317980Swosch	int count, found, globflag;
14417980Swosch	u_char *cutoff;
14517980Swosch	u_char bigram1[NBG], bigram2[NBG], path[MAXPATHLEN];
14617980Swosch
14717980Swosch#ifdef FF_ICASE
14817980Swosch	/* use a lookup table for case insensitive search */
14918905Swosch	u_char table[UCHAR_MAX + 1];
15017980Swosch
15117980Swosch	tolower_word(pathpart);
15218905Swosch#endif /* FF_ICASE*/
15317980Swosch
15417980Swosch	/* init bigram table */
15517980Swosch#ifdef FF_MMAP
15627574Scharnier	if (len < (2*NBG))
15727574Scharnier		errx(1, "database too small: %s", database);
15817980Swosch
15917980Swosch	for (c = 0, p = bigram1, s = bigram2; c < NBG; c++, len-= 2) {
16017980Swosch		p[c] = check_bigram_char(*paddr++);
16117980Swosch		s[c] = check_bigram_char(*paddr++);
16217980Swosch	}
16317980Swosch#else
16417980Swosch	for (c = 0, p = bigram1, s = bigram2; c < NBG; c++) {
16517980Swosch		p[c] = check_bigram_char(getc(fp));
16617980Swosch		s[c] = check_bigram_char(getc(fp));
16717980Swosch	}
16818905Swosch#endif /* FF_MMAP */
16917980Swosch
17017980Swosch	/* find optimal (last) char for searching */
17118829Swosch	for (p = pathpart; *p != '\0'; p++)
17218829Swosch		if (index(LOCATE_REG, *p) != NULL)
17318829Swosch			break;
17418829Swosch
17518829Swosch	if (*p == '\0')
17618829Swosch		globflag = 0;
17718829Swosch	else
17818829Swosch		globflag = 1;
17918829Swosch
18017980Swosch	p = pathpart;
18117980Swosch	patend = patprep(p);
18217980Swosch	cc = *patend;
18317980Swosch
18417980Swosch#ifdef FF_ICASE
18517980Swosch	/* set patend char to true */
18620406Swosch        for (c = 0; c < UCHAR_MAX + 1; c++)
18720406Swosch                table[c] = 0;
18820406Swosch
18917980Swosch	table[TOLOWER(*patend)] = 1;
19017980Swosch	table[toupper(*patend)] = 1;
19118905Swosch#endif /* FF_ICASE */
19217980Swosch
19317980Swosch
19417980Swosch	/* main loop */
19517980Swosch	found = count = 0;
19617980Swosch	foundchar = 0;
19717980Swosch
19817980Swosch#ifdef FF_MMAP
19918905Swosch	c = (u_char)*paddr++; len--;
20018905Swosch	for (; len > 0; ) {
20117980Swosch#else
20218905Swosch	c = getc(fp);
20318905Swosch	for (; c != EOF; ) {
20418905Swosch#endif /* FF_MMAP */
20517980Swosch
20617980Swosch		/* go forward or backward */
20717980Swosch		if (c == SWITCH) { /* big step, an integer */
20817980Swosch#ifdef FF_MMAP
20917980Swosch			count += getwm(paddr) - OFFSET;
21017980Swosch			len -= INTSIZE; paddr += INTSIZE;
21117980Swosch#else
21217980Swosch			count +=  getwf(fp) - OFFSET;
21318905Swosch#endif /* FF_MMAP */
21417980Swosch		} else {	   /* slow step, =< 14 chars */
21517980Swosch			count += c - OFFSET;
21617980Swosch		}
21717980Swosch
21817980Swosch		/* overlay old path */
21917980Swosch		p = path + count;
22017980Swosch		foundchar = p - 1;
22118905Swosch
22218905Swosch		for (;;) {
22317980Swosch#ifdef FF_MMAP
22418905Swosch			c = (u_char)*paddr++;
22518905Swosch		        len--;
22617980Swosch#else
22718905Swosch			c = getc(fp);
22818905Swosch#endif /* FF_MMAP */
22918905Swosch			/*
23018905Swosch			 * == UMLAUT: 8 bit char followed
23118905Swosch			 * <= SWITCH: offset
23218905Swosch			 * >= PARITY: bigram
23318905Swosch			 * rest:      single ascii char
23418905Swosch			 *
23518905Swosch			 * offset < SWITCH < UMLAUT < ascii < PARITY < bigram
23618905Swosch			 */
23717980Swosch			if (c < PARITY) {
23818905Swosch				if (c <= UMLAUT) {
23918905Swosch					if (c == UMLAUT) {
24018905Swosch#ifdef FF_MMAP
24118905Swosch						c = (u_char)*paddr++;
24218905Swosch						len--;
24318905Swosch#else
24418905Swosch						c = getc(fp);
24518905Swosch#endif /* FF_MMAP */
24618905Swosch
24718905Swosch					} else
24818905Swosch						break; /* SWITCH */
24918905Swosch				}
25017980Swosch#ifdef FF_ICASE
25117980Swosch				if (table[c])
25217980Swosch#else
25317980Swosch				if (c == cc)
25418905Swosch#endif /* FF_ICASE */
25517980Swosch					foundchar = p;
25617980Swosch				*p++ = c;
25717980Swosch			}
25817980Swosch			else {
25917980Swosch				/* bigrams are parity-marked */
26017980Swosch				TO7BIT(c);
26117980Swosch
26217980Swosch#ifndef FF_ICASE
26317980Swosch				if (bigram1[c] == cc ||
26417980Swosch				    bigram2[c] == cc)
26517980Swosch#else
26617980Swosch
26717980Swosch					if (table[bigram1[c]] ||
26817980Swosch					    table[bigram2[c]])
26918905Swosch#endif /* FF_ICASE */
27017980Swosch						foundchar = p + 1;
27117980Swosch
27217980Swosch				*p++ = bigram1[c];
27317980Swosch				*p++ = bigram2[c];
27417980Swosch			}
27518905Swosch		}
27617980Swosch
27717980Swosch		if (found) {                     /* previous line matched */
27817980Swosch			cutoff = path;
27917980Swosch			*p-- = '\0';
28017980Swosch			foundchar = p;
28117980Swosch		} else if (foundchar >= path + count) { /* a char matched */
28217980Swosch			*p-- = '\0';
28317980Swosch			cutoff = path + count;
28417980Swosch		} else                           /* nothing to do */
28517980Swosch			continue;
28617980Swosch
28717980Swosch		found = 0;
28817980Swosch		for (s = foundchar; s >= cutoff; s--) {
28917980Swosch			if (*s == cc
29017980Swosch#ifdef FF_ICASE
29117980Swosch			    || TOLOWER(*s) == cc
29218905Swosch#endif /* FF_ICASE */
29317980Swosch			    ) {	/* fast first char check */
29417980Swosch				for (p = patend - 1, q = s - 1; *p != '\0';
29517980Swosch				     p--, q--)
29617980Swosch					if (*q != *p
29717980Swosch#ifdef FF_ICASE
29817980Swosch					    && TOLOWER(*q) != *p
29918905Swosch#endif /* FF_ICASE */
30017980Swosch					    )
30117980Swosch						break;
30217980Swosch				if (*p == '\0') {   /* fast match success */
30317980Swosch					found = 1;
30419060Swosch					if (!globflag ||
30519060Swosch#ifndef FF_ICASE
30619060Swosch					    !fnmatch(pathpart, path, 0))
30719060Swosch#else
30819060Swosch					    !fnmatch(pathpart, path,
30919134Sache						     FNM_CASEFOLD))
31019060Swosch#endif /* !FF_ICASE */
31119060Swosch					{
31217980Swosch						if (f_silent)
31317980Swosch							counter++;
31417980Swosch						else if (f_limit) {
31517980Swosch							counter++;
31617980Swosch							if (f_limit >= counter)
31717980Swosch								(void)puts(path);
31827574Scharnier							else
31927574Scharnier								errx(0, "[show only %d lines]", counter - 1);
32017980Swosch						} else
32117980Swosch							(void)puts(path);
32217980Swosch					}
32317980Swosch					break;
32417980Swosch				}
32517980Swosch			}
32617980Swosch		}
32717980Swosch	}
32817980Swosch}
329