xref: /freebsd/usr.bin/locate/code/locate.code.c (revision 1a1ee31f)
1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * James A. Woods.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * 	$Id: locate.code.c,v 1.4 1996/08/22 18:46:13 wosch Exp $
37  */
38 
39 #ifndef lint
40 static char copyright[] =
41 "@(#) Copyright (c) 1989, 1993\n\
42 	The Regents of the University of California.  All rights reserved.\n";
43 #endif /* not lint */
44 
45 #ifndef lint
46 static char sccsid[] = "@(#)locate.code.c	8.1 (Berkeley) 6/6/93";
47 #endif /* not lint */
48 
49 /*
50  * PURPOSE:	sorted list compressor (works with a modified 'find'
51  *		to encode/decode a filename database)
52  *
53  * USAGE:	bigram < list > bigrams
54  *		process bigrams (see updatedb) > common_bigrams
55  *		code common_bigrams < list > squozen_list
56  *
57  * METHOD:	Uses 'front compression' (see ";login:", Volume 8, Number 1
58  *		February/March 1983, p. 8).  Output format is, per line, an
59  *		offset differential count byte followed by a partially bigram-
60  *		encoded ascii residue.  A bigram is a two-character sequence,
61  *		the first 128 most common of which are encoded in one byte.
62  *
63  * EXAMPLE:	For simple front compression with no bigram encoding,
64  *		if the input is...		then the output is...
65  *
66  *		/usr/src			 0 /usr/src
67  *		/usr/src/cmd/aardvark.c		 8 /cmd/aardvark.c
68  *		/usr/src/cmd/armadillo.c	14 armadillo.c
69  *		/usr/tmp/zoo			 5 tmp/zoo
70  *
71  *	The codes are:
72  *
73  *	0-28	likeliest differential counts + offset to make nonnegative
74  *	30	switch code for out-of-range count to follow in next word
75  *	128-255 bigram codes (128 most common, as determined by 'updatedb')
76  *	32-127  single character (printable) ascii residue (ie, literal)
77  *
78  * SEE ALSO:	updatedb.csh, bigram.c
79  *
80  * AUTHOR:	James A. Woods, Informatics General Corp.,
81  *		NASA Ames Research Center, 10/82
82  */
83 
84 #include <sys/param.h>
85 #include <err.h>
86 #include <errno.h>
87 #include <stdlib.h>
88 #include <string.h>
89 #include <stdio.h>
90 #include "locate.h"
91 
92 #define	BGBUFSIZE	(NBG * 2)	/* size of bigram buffer */
93 
94 u_char buf1[MAXPATHLEN] = " ";
95 u_char buf2[MAXPATHLEN];
96 char bigrams[BGBUFSIZE + 1] = { 0 };
97 
98 #define LOOKUP 1 /* use a lookup array instead a function, 3x faster */
99 
100 #ifdef LOOKUP
101 #define BGINDEX(x) (big[(u_int)*x][(u_int)*(x+1)])
102 typedef u_char bg_t;
103 bg_t big[UCHAR_MAX][UCHAR_MAX];
104 #else
105 #define BGINDEX(x) bgindex(x)
106 typedef int bg_t;
107 int	bgindex __P((char *));
108 #endif /* LOOKUP */
109 
110 
111 void	usage __P((void));
112 extern int optind;
113 extern int optopt;
114 
115 int
116 main(argc, argv)
117 	int argc;
118 	char *argv[];
119 {
120 	register u_char *cp, *oldpath, *path;
121 	int ch, code, count, diffcount, oldcount;
122 	FILE *fp;
123 	register int i, j;
124 
125 	while ((ch = getopt(argc, argv, "")) != EOF)
126 		switch(ch) {
127 		default:
128 			usage();
129 		}
130 	argc -= optind;
131 	argv += optind;
132 
133 	if (argc != 1)
134 		usage();
135 
136 	if ((fp = fopen(argv[0], "r")) == NULL)
137 		err(1, "%s", argv[0]);
138 
139 	/* First copy bigram array to stdout. */
140 	(void)fgets(bigrams, BGBUFSIZE + 1, fp);
141 
142 	if (fwrite(bigrams, 1, BGBUFSIZE, stdout) != BGBUFSIZE)
143 		err(1, "stdout");
144 	(void)fclose(fp);
145 
146 #ifdef LOOKUP
147 	/* init lookup table */
148 	for (i = 0; i < UCHAR_MAX; i++)
149 	    	for (j = 0; j < UCHAR_MAX; j++)
150 			big[i][j] = (bg_t)-1;
151 
152 	for (cp = bigrams, i = 0; *cp != '\0'; i += 2, cp += 2)
153 	        big[(int)*cp][(int)*(cp + 1)] = (bg_t)i;
154 #endif /* LOOKUP */
155 
156 	oldpath = buf1;
157 	path = buf2;
158 	oldcount = 0;
159 
160 	while (fgets(path, sizeof(buf2), stdin) != NULL) {
161 
162 	    	/* skip empty lines */
163 		if (*path == '\n')
164 			continue;
165 
166 		/* Squelch characters that would botch the decoding. */
167 		for (cp = path; *cp != '\0'; cp++) {
168 			/* chop newline */
169 			if (*cp == '\n')
170 				*cp = '\0';
171 			/* range */
172 			else if (*cp < ASCII_MIN || *cp > ASCII_MAX)
173 				*cp = '?';
174 		}
175 
176 		/* Skip longest common prefix. */
177 		for (cp = path; *cp == *oldpath && *cp != '\0'; cp++, oldpath++);
178 
179 		count = cp - path;
180 		diffcount = count - oldcount + OFFSET;
181 		oldcount = count;
182 		if (diffcount < 0 || diffcount > 2 * OFFSET) {
183 			if (putchar(SWITCH) == EOF ||
184 			    putw(diffcount, stdout) == EOF)
185 				err(1, "stdout");
186 		} else
187 			if (putchar(diffcount) == EOF)
188 				err(1, "stdout");
189 
190 		while (*cp != '\0') {
191 			if (*(cp + 1) == '\0') {
192 				if (putchar(*cp) == EOF)
193 					err(1, "stdout");
194 				break;
195 			}
196 			if ((code = BGINDEX(cp)) == (bg_t)-1) {
197 				if (putchar(*cp++) == EOF ||
198 				    putchar(*cp++) == EOF)
199 					err(1, "stdout");
200 			} else {
201 				/* Found, so mark byte with parity bit. */
202 				if (putchar((code / 2) | PARITY) == EOF)
203 					err(1, "stdout");
204 				cp += 2;
205 			}
206 		}
207 		if (path == buf1) {		/* swap pointers */
208 			path = buf2;
209 			oldpath = buf1;
210 		} else {
211 			path = buf1;
212 			oldpath = buf2;
213 		}
214 	}
215 	/* Non-zero status if there were errors */
216 	if (fflush(stdout) != 0 || ferror(stdout))
217 		exit(1);
218 	exit(0);
219 }
220 
221 #ifndef LOOKUP
222 int
223 bgindex(bg)			/* Return location of bg in bigrams or -1. */
224 	char *bg;
225 {
226 	register char bg0, bg1, *p;
227 
228 	bg0 = bg[0];
229 	bg1 = bg[1];
230 	for (p = bigrams; *p != NULL; p++)
231 		if (*p++ == bg0 && *p == bg1)
232 			break;
233 	return (*p == NULL ? -1 : (--p - bigrams));
234 }
235 #endif /* !LOOKUP */
236 
237 void
238 usage()
239 {
240 	(void)fprintf(stderr,
241 	    "usage: locate.code common_bigrams < list > squozen_list\n");
242 	exit(1);
243 }
244