1*d24a8065Schristos /* $NetBSD: locate.code.c,v 1.12 2019/10/13 19:39:15 christos Exp $ */
27f8ee3fcSjtc
361f28255Scgd /*
47f8ee3fcSjtc * Copyright (c) 1989, 1993
57f8ee3fcSjtc * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * This code is derived from software contributed to Berkeley by
861f28255Scgd * James A. Woods.
961f28255Scgd *
1061f28255Scgd * Redistribution and use in source and binary forms, with or without
1161f28255Scgd * modification, are permitted provided that the following conditions
1261f28255Scgd * are met:
1361f28255Scgd * 1. Redistributions of source code must retain the above copyright
1461f28255Scgd * notice, this list of conditions and the following disclaimer.
1561f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1661f28255Scgd * notice, this list of conditions and the following disclaimer in the
1761f28255Scgd * documentation and/or other materials provided with the distribution.
1889aaa1bbSagc * 3. Neither the name of the University nor the names of its contributors
1961f28255Scgd * may be used to endorse or promote products derived from this software
2061f28255Scgd * without specific prior written permission.
2161f28255Scgd *
2261f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2361f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2461f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2561f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2661f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2761f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2861f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2961f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3061f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3161f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3261f28255Scgd * SUCH DAMAGE.
3361f28255Scgd */
3461f28255Scgd
352f6bf413Slukem #include <sys/cdefs.h>
3661f28255Scgd #ifndef lint
3798e5374cSlukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
3898e5374cSlukem The Regents of the University of California. All rights reserved.");
3961f28255Scgd #endif /* not lint */
4061f28255Scgd
4161f28255Scgd #ifndef lint
427f8ee3fcSjtc #if 0
434d85334fSjtc static char sccsid[] = "@(#)locate.code.c 8.4 (Berkeley) 5/4/95";
447f8ee3fcSjtc #endif
45*d24a8065Schristos __RCSID("$NetBSD: locate.code.c,v 1.12 2019/10/13 19:39:15 christos Exp $");
4661f28255Scgd #endif /* not lint */
4761f28255Scgd
4861f28255Scgd /*
4961f28255Scgd * PURPOSE: sorted list compressor (works with a modified 'find'
5061f28255Scgd * to encode/decode a filename database)
5161f28255Scgd *
5261f28255Scgd * USAGE: bigram < list > bigrams
5361f28255Scgd * process bigrams (see updatedb) > common_bigrams
5461f28255Scgd * code common_bigrams < list > squozen_list
5561f28255Scgd *
5661f28255Scgd * METHOD: Uses 'front compression' (see ";login:", Volume 8, Number 1
5761f28255Scgd * February/March 1983, p. 8). Output format is, per line, an
5861f28255Scgd * offset differential count byte followed by a partially bigram-
5961f28255Scgd * encoded ascii residue. A bigram is a two-character sequence,
6061f28255Scgd * the first 128 most common of which are encoded in one byte.
6161f28255Scgd *
6261f28255Scgd * EXAMPLE: For simple front compression with no bigram encoding,
6361f28255Scgd * if the input is... then the output is...
6461f28255Scgd *
6561f28255Scgd * /usr/src 0 /usr/src
6661f28255Scgd * /usr/src/cmd/aardvark.c 8 /cmd/aardvark.c
6761f28255Scgd * /usr/src/cmd/armadillo.c 14 armadillo.c
6861f28255Scgd * /usr/tmp/zoo 5 tmp/zoo
6961f28255Scgd *
7061f28255Scgd * The codes are:
7161f28255Scgd *
7261f28255Scgd * 0-28 likeliest differential counts + offset to make nonnegative
7361f28255Scgd * 30 switch code for out-of-range count to follow in next word
7461f28255Scgd * 128-255 bigram codes (128 most common, as determined by 'updatedb')
7561f28255Scgd * 32-127 single character (printable) ascii residue (ie, literal)
7661f28255Scgd *
777f8ee3fcSjtc * SEE ALSO: updatedb.csh, bigram.c
7861f28255Scgd *
7961f28255Scgd * AUTHOR: James A. Woods, Informatics General Corp.,
8061f28255Scgd * NASA Ames Research Center, 10/82
8161f28255Scgd */
8261f28255Scgd
8361f28255Scgd #include <sys/param.h>
844d85334fSjtc
857f8ee3fcSjtc #include <err.h>
867f8ee3fcSjtc #include <errno.h>
874d85334fSjtc #include <stdio.h>
887f8ee3fcSjtc #include <stdlib.h>
897f8ee3fcSjtc #include <string.h>
904d85334fSjtc #include <unistd.h>
914d85334fSjtc
9261f28255Scgd #include "locate.h"
9361f28255Scgd
9461f28255Scgd #define BGBUFSIZE (NBG * 2) /* size of bigram buffer */
9561f28255Scgd
96cb377560Sjoerg static char buf1[MAXPATHLEN] = " ";
97cb377560Sjoerg static char buf2[MAXPATHLEN];
98cb377560Sjoerg static char bigrams[BGBUFSIZE + 1] = { 0 };
9961f28255Scgd
100cb377560Sjoerg static int bgindex(char *);
101cb377560Sjoerg __dead static void usage(void);
1027f8ee3fcSjtc
1037f8ee3fcSjtc int
main(int argc,char * argv[])104cb377560Sjoerg main(int argc, char *argv[])
10561f28255Scgd {
1062f6bf413Slukem char *cp, *oldpath, *path;
1077f8ee3fcSjtc int ch, code, count, diffcount, oldcount;
10861f28255Scgd
1092f6bf413Slukem while ((ch = getopt(argc, argv, "")) != -1)
1107f8ee3fcSjtc switch(ch) {
1117f8ee3fcSjtc case '?':
1127f8ee3fcSjtc default:
1137f8ee3fcSjtc usage();
11461f28255Scgd }
1157f8ee3fcSjtc argc -= optind;
1167f8ee3fcSjtc argv += optind;
11761f28255Scgd
1187f8ee3fcSjtc if (argc != 1)
1197f8ee3fcSjtc usage();
1207f8ee3fcSjtc
1217f8ee3fcSjtc /* First copy bigram array to stdout. */
122*d24a8065Schristos strncpy(bigrams, argv[0], BGBUFSIZE);
123*d24a8065Schristos bigrams[BGBUFSIZE] = '\0';
124e8509e91Smycroft if (fwrite(bigrams, 1, BGBUFSIZE, stdout) != BGBUFSIZE)
1257f8ee3fcSjtc err(1, "stdout");
1267f8ee3fcSjtc
1277f8ee3fcSjtc oldpath = buf1;
1287f8ee3fcSjtc path = buf2;
1297f8ee3fcSjtc oldcount = 0;
13061f28255Scgd while (fgets(path, sizeof(buf2), stdin) != NULL) {
1317f8ee3fcSjtc /* Truncate newline. */
13261f28255Scgd cp = path + strlen(path) - 1;
13361f28255Scgd if (cp > path && *cp == '\n')
13461f28255Scgd *cp = '\0';
1357f8ee3fcSjtc
1367f8ee3fcSjtc /* Squelch characters that would botch the decoding. */
1374d85334fSjtc for (cp = path; *cp != '\0'; cp++) {
13861f28255Scgd *cp &= PARITY-1;
139535f5e40Sjtc if (*cp <= SWITCH)
14061f28255Scgd *cp = '?';
14161f28255Scgd }
1427f8ee3fcSjtc
1437f8ee3fcSjtc /* Skip longest common prefix. */
14461f28255Scgd for (cp = path; *cp == *oldpath; cp++, oldpath++)
1454d85334fSjtc if (*oldpath == '\0')
14661f28255Scgd break;
14761f28255Scgd count = cp - path;
14861f28255Scgd diffcount = count - oldcount + OFFSET;
14961f28255Scgd oldcount = count;
15061f28255Scgd if (diffcount < 0 || diffcount > 2 * OFFSET) {
1517f8ee3fcSjtc if (putchar(SWITCH) == EOF ||
1527f8ee3fcSjtc putw(diffcount, stdout) == EOF)
1537f8ee3fcSjtc err(1, "stdout");
1547f8ee3fcSjtc } else
1557f8ee3fcSjtc if (putchar(diffcount) == EOF)
1567f8ee3fcSjtc err(1, "stdout");
15761f28255Scgd
1584d85334fSjtc while (*cp != '\0') {
1594d85334fSjtc if (*(cp + 1) == '\0') {
1607f8ee3fcSjtc if (putchar(*cp) == EOF)
1617f8ee3fcSjtc err(1, "stdout");
16261f28255Scgd break;
16361f28255Scgd }
16461f28255Scgd if ((code = bgindex(cp)) < 0) {
1657f8ee3fcSjtc if (putchar(*cp++) == EOF ||
1667f8ee3fcSjtc putchar(*cp++) == EOF)
1677f8ee3fcSjtc err(1, "stdout");
1687f8ee3fcSjtc } else {
1697f8ee3fcSjtc /* Found, so mark byte with parity bit. */
1707f8ee3fcSjtc if (putchar((code / 2) | PARITY) == EOF)
1717f8ee3fcSjtc err(1, "stdout");
17261f28255Scgd cp += 2;
17361f28255Scgd }
17461f28255Scgd }
1757f8ee3fcSjtc if (path == buf1) { /* swap pointers */
1767f8ee3fcSjtc path = buf2;
1777f8ee3fcSjtc oldpath = buf1;
1787f8ee3fcSjtc } else {
1797f8ee3fcSjtc path = buf1;
1807f8ee3fcSjtc oldpath = buf2;
18161f28255Scgd }
18261f28255Scgd }
1837f8ee3fcSjtc /* Non-zero status if there were errors */
1847f8ee3fcSjtc if (fflush(stdout) != 0 || ferror(stdout))
1857f8ee3fcSjtc exit(1);
1867f8ee3fcSjtc exit(0);
1877f8ee3fcSjtc }
18861f28255Scgd
189cb377560Sjoerg static int
bgindex(char * bg)190cb377560Sjoerg bgindex(char *bg) /* Return location of bg in bigrams or -1. */
19161f28255Scgd {
1922f6bf413Slukem char bg0, bg1, *p;
19361f28255Scgd
1947f8ee3fcSjtc bg0 = bg[0];
1957f8ee3fcSjtc bg1 = bg[1];
196e8509e91Smycroft for (p = bigrams; *p != '\0'; p += 2)
197e8509e91Smycroft if (p[0] == bg0 && p[1] == bg1)
19861f28255Scgd break;
199e8509e91Smycroft return (*p == '\0' ? -1 : p - bigrams);
20061f28255Scgd }
2017f8ee3fcSjtc
202cb377560Sjoerg static void
usage(void)203cb377560Sjoerg usage(void)
2047f8ee3fcSjtc {
2057f8ee3fcSjtc (void)fprintf(stderr,
2067f8ee3fcSjtc "usage: locate.code common_bigrams < list > squozen_list\n");
2077f8ee3fcSjtc exit(1);
2087f8ee3fcSjtc }
209