xref: /freebsd/usr.bin/locate/code/locate.code.c (revision c83667e6)
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: locate.code.c,v 1.6 1996/10/13 01:44:28 wosch Exp $
38  */
39 
40 #ifndef lint
41 static char copyright[] =
42 "@(#) Copyright (c) 1989, 1993\n\
43 	The Regents of the University of California.  All rights reserved.\n";
44 #endif /* not lint */
45 
46 #ifndef lint
47 static char sccsid[] = "@(#)locate.code.c	8.1 (Berkeley) 6/6/93";
48 #endif /* not lint */
49 
50 /*
51  * PURPOSE:	sorted list compressor (works with a modified 'find'
52  *		to encode/decode a filename database)
53  *
54  * USAGE:	bigram < list > bigrams
55  *		process bigrams (see updatedb) > common_bigrams
56  *		code common_bigrams < list > squozen_list
57  *
58  * METHOD:	Uses 'front compression' (see ";login:", Volume 8, Number 1
59  *		February/March 1983, p. 8).  Output format is, per line, an
60  *		offset differential count byte followed by a partially bigram-
61  *		encoded ascii residue.  A bigram is a two-character sequence,
62  *		the first 128 most common of which are encoded in one byte.
63  *
64  * EXAMPLE:	For simple front compression with no bigram encoding,
65  *		if the input is...		then the output is...
66  *
67  *		/usr/src			 0 /usr/src
68  *		/usr/src/cmd/aardvark.c		 8 /cmd/aardvark.c
69  *		/usr/src/cmd/armadillo.c	14 armadillo.c
70  *		/usr/tmp/zoo			 5 tmp/zoo
71  *
72  *	The codes are:
73  *
74  *	0-28	likeliest differential counts + offset to make nonnegative
75  *	30	switch code for out-of-range count to follow in next word
76  *      31      an 8 bit char followed
77  *	128-255 bigram codes (128 most common, as determined by 'updatedb')
78  *	32-127  single character (printable) ascii residue (ie, literal)
79  *
80  * The locate database store any character except newline ('\n')
81  * and NUL ('\0'). The 8-bit character support don't wast extra
82  * space until you have characters in file names less than 32
83  * or greather than 127.
84  *
85  *
86  * SEE ALSO:	updatedb.sh, ../bigram/locate.bigram.c
87  *
88  * AUTHOR:	James A. Woods, Informatics General Corp.,
89  *		NASA Ames Research Center, 10/82
90  *              8-bit file names characters:
91  *              	Wolfram Schneider, Berlin September 1996
92  */
93 
94 #include <sys/param.h>
95 #include <err.h>
96 #include <errno.h>
97 #include <stdlib.h>
98 #include <string.h>
99 #include <stdio.h>
100 #include "locate.h"
101 
102 #define	BGBUFSIZE	(NBG * 2)	/* size of bigram buffer */
103 
104 u_char buf1[MAXPATHLEN] = " ";
105 u_char buf2[MAXPATHLEN];
106 u_char bigrams[BGBUFSIZE + 1] = { 0 };
107 
108 #define LOOKUP 1 /* use a lookup array instead a function, 3x faster */
109 
110 #ifdef LOOKUP
111 #define BGINDEX(x) (big[(u_char)*x][(u_char)*(x + 1)])
112 typedef short bg_t;
113 bg_t big[UCHAR_MAX + 1][UCHAR_MAX + 1];
114 #else
115 #define BGINDEX(x) bgindex(x)
116 typedef int bg_t;
117 int	bgindex __P((char *));
118 #endif /* LOOKUP */
119 
120 
121 void	usage __P((void));
122 extern int optind;
123 extern int optopt;
124 
125 int
126 main(argc, argv)
127 	int argc;
128 	char *argv[];
129 {
130 	register u_char *cp, *oldpath, *path;
131 	int ch, code, count, diffcount, oldcount;
132 	FILE *fp;
133 	register int i, j;
134 
135 	while ((ch = getopt(argc, argv, "")) != EOF)
136 		switch(ch) {
137 		default:
138 			usage();
139 		}
140 	argc -= optind;
141 	argv += optind;
142 
143 	if (argc != 1)
144 		usage();
145 
146 	if ((fp = fopen(argv[0], "r")) == NULL)
147 		err(1, "%s", argv[0]);
148 
149 	/* First copy bigram array to stdout. */
150 	(void)fgets(bigrams, BGBUFSIZE + 1, fp);
151 
152 	if (fwrite(bigrams, 1, BGBUFSIZE, stdout) != BGBUFSIZE)
153 		err(1, "stdout");
154 	(void)fclose(fp);
155 
156 #ifdef LOOKUP
157 	/* init lookup table */
158 	for (i = 0; i < UCHAR_MAX + 1; i++)
159 	    	for (j = 0; j < UCHAR_MAX + 1; j++)
160 			big[i][j] = (bg_t)-1;
161 
162 	for (cp = bigrams, i = 0; *cp != '\0'; i += 2, cp += 2)
163 	        big[(u_char)*cp][(u_char)*(cp + 1)] = (bg_t)i;
164 
165 #endif /* LOOKUP */
166 
167 	oldpath = buf1;
168 	path = buf2;
169 	oldcount = 0;
170 
171 	while (fgets(path, sizeof(buf2), stdin) != NULL) {
172 
173 		/* skip empty lines */
174 		if (*path == '\n')
175 			continue;
176 
177 		/* remove newline */
178 		for (cp = path; *cp != '\0'; cp++) {
179 #ifndef LOCATE_CHAR30
180 			/* old locate implementations core'd for char 30 */
181 			if (*cp == SWITCH)
182 				*cp = '?';
183 			else
184 #endif /* !LOCATE_CHAR30 */
185 
186 			/* chop newline */
187 			if (*cp == '\n')
188 				*cp = '\0';
189 		}
190 
191 		/* Skip longest common prefix. */
192 		for (cp = path; *cp == *oldpath; cp++, oldpath++)
193 			if (*cp == '\0')
194 				break;
195 
196 		count = cp - path;
197 		diffcount = count - oldcount + OFFSET;
198 		oldcount = count;
199 		if (diffcount < 0 || diffcount > 2 * OFFSET) {
200 			if (putchar(SWITCH) == EOF ||
201 			    putw(diffcount, stdout) == EOF)
202 				err(1, "stdout");
203 		} else
204 			if (putchar(diffcount) == EOF)
205 				err(1, "stdout");
206 
207 		while (*cp != '\0') {
208 			/* print *two* characters */
209 
210 			if ((code = BGINDEX(cp)) != (bg_t)-1) {
211 				/*
212 				 * print *one* as bigram
213 				 * Found, so mark byte with
214 				 *  parity bit.
215 				 */
216 				if (putchar((code / 2) | PARITY) == EOF)
217 					err(1, "stdout");
218 				cp += 2;
219 			}
220 
221 			else {
222 				for (i = 0; i < 2; i++) {
223 					if (*cp == '\0')
224 						break;
225 
226 					/* print umlauts in file names */
227 					if (*cp < ASCII_MIN ||
228 					    *cp > ASCII_MAX) {
229 						if (putchar(UMLAUT) == EOF ||
230 						    putchar(*cp++) == EOF)
231 							err(1, "stdout");
232 					}
233 
234 					else {
235 						/* normal character */
236 						if(putchar(*cp++) == EOF)
237 							err(1, "stdout");
238 					}
239 				}
240 
241 			}
242 		}
243 
244 		if (path == buf1) {		/* swap pointers */
245 			path = buf2;
246 			oldpath = buf1;
247 		} else {
248 			path = buf1;
249 			oldpath = buf2;
250 		}
251 	}
252 	/* Non-zero status if there were errors */
253 	if (fflush(stdout) != 0 || ferror(stdout))
254 		exit(1);
255 	exit(0);
256 }
257 
258 #ifndef LOOKUP
259 int
260 bgindex(bg)			/* Return location of bg in bigrams or -1. */
261 	char *bg;
262 {
263 	register char bg0, bg1, *p;
264 
265 	bg0 = bg[0];
266 	bg1 = bg[1];
267 	for (p = bigrams; *p != NULL; p++)
268 		if (*p++ == bg0 && *p == bg1)
269 			break;
270 	return (*p == NULL ? -1 : (--p - bigrams));
271 }
272 #endif /* !LOOKUP */
273 
274 void
275 usage()
276 {
277 	(void)fprintf(stderr,
278 	    "usage: locate.code common_bigrams < list > squozen_list\n");
279 	exit(1);
280 }
281