xref: /freebsd/usr.bin/locate/code/locate.code.c (revision c44252b6)
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 
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.4 (Berkeley) 5/4/95";
45 #endif /* not lint */
46 
47 /*
48  * PURPOSE:	sorted list compressor (works with a modified 'find'
49  *		to encode/decode a filename database)
50  *
51  * USAGE:	bigram < list > bigrams
52  *		process bigrams (see updatedb) > common_bigrams
53  *		code common_bigrams < list > squozen_list
54  *
55  * METHOD:	Uses 'front compression' (see ";login:", Volume 8, Number 1
56  *		February/March 1983, p. 8).  Output format is, per line, an
57  *		offset differential count byte followed by a partially bigram-
58  *		encoded ascii residue.  A bigram is a two-character sequence,
59  *		the first 128 most common of which are encoded in one byte.
60  *
61  * EXAMPLE:	For simple front compression with no bigram encoding,
62  *		if the input is...		then the output is...
63  *
64  *		/usr/src			 0 /usr/src
65  *		/usr/src/cmd/aardvark.c		 8 /cmd/aardvark.c
66  *		/usr/src/cmd/armadillo.c	14 armadillo.c
67  *		/usr/tmp/zoo			 5 tmp/zoo
68  *
69  *	The codes are:
70  *
71  *	0-28	likeliest differential counts + offset to make nonnegative
72  *	30	switch code for out-of-range count to follow in next word
73  *	128-255 bigram codes (128 most common, as determined by 'updatedb')
74  *	32-127  single character (printable) ascii residue (ie, literal)
75  *
76  * SEE ALSO:	updatedb.csh, bigram.c
77  *
78  * AUTHOR:	James A. Woods, Informatics General Corp.,
79  *		NASA Ames Research Center, 10/82
80  */
81 
82 #include <sys/param.h>
83 
84 #include <err.h>
85 #include <errno.h>
86 #include <stdio.h>
87 #include <stdlib.h>
88 #include <string.h>
89 #include <unistd.h>
90 
91 #include "locate.h"
92 
93 #define	BGBUFSIZE	(NBG * 2)	/* size of bigram buffer */
94 
95 char buf1[MAXPATHLEN] = " ";
96 char buf2[MAXPATHLEN];
97 char bigrams[BGBUFSIZE + 1] = { 0 };
98 
99 int	bgindex __P((char *));
100 void	usage __P((void));
101 
102 int
103 main(argc, argv)
104 	int argc;
105 	char *argv[];
106 {
107 	register char *cp, *oldpath, *path;
108 	int ch, code, count, diffcount, oldcount;
109 	FILE *fp;
110 
111 	while ((ch = getopt(argc, argv, "")) != EOF)
112 		switch(ch) {
113 		case '?':
114 		default:
115 			usage();
116 		}
117 	argc -= optind;
118 	argv += optind;
119 
120 	if (argc != 1)
121 		usage();
122 
123 	if ((fp = fopen(argv[0], "r")) == NULL)
124 		err(1, "%s", argv[0]);
125 
126 	/* First copy bigram array to stdout. */
127 	(void)fgets(bigrams, BGBUFSIZE + 1, fp);
128 	if (fwrite(bigrams, 1, BGBUFSIZE, stdout) != BGBUFSIZE)
129 		err(1, "stdout");
130 	(void)fclose(fp);
131 
132 	oldpath = buf1;
133 	path = buf2;
134 	oldcount = 0;
135 	while (fgets(path, sizeof(buf2), stdin) != NULL) {
136 		/* Truncate newline. */
137 		cp = path + strlen(path) - 1;
138 		if (cp > path && *cp == '\n')
139 			*cp = '\0';
140 
141 		/* Squelch characters that would botch the decoding. */
142 		for (cp = path; *cp != '\0'; cp++) {
143 			if ((u_char)*cp >= PARITY || *cp <= SWITCH)
144 				*cp = '?';
145 		}
146 
147 		/* Skip longest common prefix. */
148 		for (cp = path; *cp == *oldpath; cp++, oldpath++)
149 			if (*oldpath == '\0')
150 				break;
151 		count = cp - path;
152 		diffcount = count - oldcount + OFFSET;
153 		oldcount = count;
154 		if (diffcount < 0 || diffcount > 2 * OFFSET) {
155 			if (putchar(SWITCH) == EOF ||
156 			    putw(diffcount, stdout) == EOF)
157 				err(1, "stdout");
158 		} else
159 			if (putchar(diffcount) == EOF)
160 				err(1, "stdout");
161 
162 		while (*cp != '\0') {
163 			if (*(cp + 1) == '\0') {
164 				if (putchar(*cp) == EOF)
165 					err(1, "stdout");
166 				break;
167 			}
168 			if ((code = bgindex(cp)) < 0) {
169 				if (putchar(*cp++) == EOF ||
170 				    putchar(*cp++) == EOF)
171 					err(1, "stdout");
172 			} else {
173 				/* Found, so mark byte with parity bit. */
174 				if (putchar((code / 2) | PARITY) == EOF)
175 					err(1, "stdout");
176 				cp += 2;
177 			}
178 		}
179 		if (path == buf1) {		/* swap pointers */
180 			path = buf2;
181 			oldpath = buf1;
182 		} else {
183 			path = buf1;
184 			oldpath = buf2;
185 		}
186 	}
187 	/* Non-zero status if there were errors */
188 	if (fflush(stdout) != 0 || ferror(stdout))
189 		exit(1);
190 	exit(0);
191 }
192 
193 int
194 bgindex(bg)			/* Return location of bg in bigrams or -1. */
195 	char *bg;
196 {
197 	register char bg0, bg1, *p;
198 
199 	bg0 = bg[0];
200 	bg1 = bg[1];
201 	for (p = bigrams; *p != '\0'; p++)
202 		if (*p++ == bg0 && *p == bg1)
203 			break;
204 	return (*p == '\0' ? -1 : --p - bigrams);
205 }
206 
207 void
208 usage()
209 {
210 	(void)fprintf(stderr,
211 	    "usage: locate.code common_bigrams < list > squozen_list\n");
212 	exit(1);
213 }
214