locate.code.c revision 1.1.1.2 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.1 (Berkeley) 6/6/93";
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 #include <err.h>
84 #include <errno.h>
85 #include <stdlib.h>
86 #include <string.h>
87 #include <stdio.h>
88 #include "locate.h"
89
90 #define BGBUFSIZE (NBG * 2) /* size of bigram buffer */
91
92 char buf1[MAXPATHLEN] = " ";
93 char buf2[MAXPATHLEN];
94 char bigrams[BGBUFSIZE + 1] = { 0 };
95
96 int bgindex __P((char *));
97 void usage __P((void));
98
99 int
100 main(argc, argv)
101 int argc;
102 char *argv[];
103 {
104 register char *cp, *oldpath, *path;
105 int ch, code, count, diffcount, oldcount;
106 FILE *fp;
107
108 while ((ch = getopt(argc, argv, "")) != EOF)
109 switch(ch) {
110 case '?':
111 default:
112 usage();
113 }
114 argc -= optind;
115 argv += optind;
116
117 if (argc != 1)
118 usage();
119
120 if ((fp = fopen(argv[0], "r")) == NULL)
121 err(1, "%s", argv[0]);
122
123 /* First copy bigram array to stdout. */
124 (void)fgets(bigrams, BGBUFSIZE + 1, fp);
125 if (fwrite(bigrams, 1, BGBUFSIZE, stdout) != BGBUFSIZE)
126 err(1, "stdout");
127 (void)fclose(fp);
128
129 oldpath = buf1;
130 path = buf2;
131 oldcount = 0;
132 while (fgets(path, sizeof(buf2), stdin) != NULL) {
133 /* Truncate newline. */
134 cp = path + strlen(path) - 1;
135 if (cp > path && *cp == '\n')
136 *cp = '\0';
137
138 /* Squelch characters that would botch the decoding. */
139 for (cp = path; *cp != NULL; cp++) {
140 if ((u_char)*cp >= PARITY)
141 *cp &= PARITY-1;
142 else if (*cp <= SWITCH)
143 *cp = '?';
144 }
145
146 /* Skip longest common prefix. */
147 for (cp = path; *cp == *oldpath; cp++, oldpath++)
148 if (*oldpath == NULL)
149 break;
150 count = cp - path;
151 diffcount = count - oldcount + OFFSET;
152 oldcount = count;
153 if (diffcount < 0 || diffcount > 2 * OFFSET) {
154 if (putchar(SWITCH) == EOF ||
155 putw(diffcount, stdout) == EOF)
156 err(1, "stdout");
157 } else
158 if (putchar(diffcount) == EOF)
159 err(1, "stdout");
160
161 while (*cp != NULL) {
162 if (*(cp + 1) == NULL) {
163 if (putchar(*cp) == EOF)
164 err(1, "stdout");
165 break;
166 }
167 if ((code = bgindex(cp)) < 0) {
168 if (putchar(*cp++) == EOF ||
169 putchar(*cp++) == EOF)
170 err(1, "stdout");
171 } else {
172 /* Found, so mark byte with parity bit. */
173 if (putchar((code / 2) | PARITY) == EOF)
174 err(1, "stdout");
175 cp += 2;
176 }
177 }
178 if (path == buf1) { /* swap pointers */
179 path = buf2;
180 oldpath = buf1;
181 } else {
182 path = buf1;
183 oldpath = buf2;
184 }
185 }
186 /* Non-zero status if there were errors */
187 if (fflush(stdout) != 0 || ferror(stdout))
188 exit(1);
189 exit(0);
190 }
191
192 int
193 bgindex(bg) /* Return location of bg in bigrams or -1. */
194 char *bg;
195 {
196 register char bg0, bg1, *p;
197
198 bg0 = bg[0];
199 bg1 = bg[1];
200 for (p = bigrams; *p != NULL; p++)
201 if (*p++ == bg0 && *p == bg1)
202 break;
203 return (*p == NULL ? -1 : --p - bigrams);
204 }
205
206 void
207 usage()
208 {
209 (void)fprintf(stderr,
210 "usage: locate.code common_bigrams < list > squozen_list\n");
211 exit(1);
212 }
213