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