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