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