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