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  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char copyright[] =
13 "@(#) Copyright (c) 1989, 1993\n\
14 	The Regents of the University of California.  All rights reserved.\n";
15 #endif /* not lint */
16 
17 #ifndef lint
18 static char sccsid[] = "@(#)locate.code.c	8.2 (Berkeley) 04/27/95";
19 #endif /* not lint */
20 
21 /*
22  * PURPOSE:	sorted list compressor (works with a modified 'find'
23  *		to encode/decode a filename database)
24  *
25  * USAGE:	bigram < list > bigrams
26  *		process bigrams (see updatedb) > common_bigrams
27  *		code common_bigrams < list > squozen_list
28  *
29  * METHOD:	Uses 'front compression' (see ";login:", Volume 8, Number 1
30  *		February/March 1983, p. 8).  Output format is, per line, an
31  *		offset differential count byte followed by a partially bigram-
32  *		encoded ascii residue.  A bigram is a two-character sequence,
33  *		the first 128 most common of which are encoded in one byte.
34  *
35  * EXAMPLE:	For simple front compression with no bigram encoding,
36  *		if the input is...		then the output is...
37  *
38  *		/usr/src			 0 /usr/src
39  *		/usr/src/cmd/aardvark.c		 8 /cmd/aardvark.c
40  *		/usr/src/cmd/armadillo.c	14 armadillo.c
41  *		/usr/tmp/zoo			 5 tmp/zoo
42  *
43  *	The codes are:
44  *
45  *	0-28	likeliest differential counts + offset to make nonnegative
46  *	30	switch code for out-of-range count to follow in next word
47  *	128-255 bigram codes (128 most common, as determined by 'updatedb')
48  *	32-127  single character (printable) ascii residue (ie, literal)
49  *
50  * SEE ALSO:	updatedb.csh, bigram.c
51  *
52  * AUTHOR:	James A. Woods, Informatics General Corp.,
53  *		NASA Ames Research Center, 10/82
54  */
55 
56 #include <sys/param.h>
57 #include <err.h>
58 #include <errno.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <stdio.h>
62 #include "locate.h"
63 
64 #define	BGBUFSIZE	(NBG * 2)	/* size of bigram buffer */
65 
66 char buf1[MAXPATHLEN] = " ";
67 char buf2[MAXPATHLEN];
68 char bigrams[BGBUFSIZE + 1] = { 0 };
69 
70 int	bgindex __P((char *));
71 void	usage __P((void));
72 
73 int
74 main(argc, argv)
75 	int argc;
76 	char *argv[];
77 {
78 	register char *cp, *oldpath, *path;
79 	int ch, code, count, diffcount, oldcount;
80 	FILE *fp;
81 
82 	while ((ch = getopt(argc, argv, "")) != EOF)
83 		switch(ch) {
84 		case '?':
85 		default:
86 			usage();
87 		}
88 	argc -= optind;
89 	argv += optind;
90 
91 	if (argc != 1)
92 		usage();
93 
94 	if ((fp = fopen(argv[0], "r")) == NULL)
95 		err(1, "%s", argv[0]);
96 
97 	/* First copy bigram array to stdout. */
98 	(void)fgets(bigrams, BGBUFSIZE + 1, fp);
99 	if (fwrite(bigrams, 1, BGBUFSIZE, stdout) != BGBUFSIZE)
100 		err(1, "stdout");
101 	(void)fclose(fp);
102 
103 	oldpath = buf1;
104 	path = buf2;
105 	oldcount = 0;
106 	while (fgets(path, sizeof(buf2), stdin) != NULL) {
107 		/* Truncate newline. */
108 		cp = path + strlen(path) - 1;
109 		if (cp > path && *cp == '\n')
110 			*cp = '\0';
111 
112 		/* Squelch characters that would botch the decoding. */
113 		for (cp = path; *cp != NULL; cp++) {
114 			if ((u_char)*cp >= PARITY)
115 				*cp &= PARITY-1;
116 			if (*cp <= SWITCH)
117 				*cp = '?';
118 		}
119 
120 		/* Skip longest common prefix. */
121 		for (cp = path; *cp == *oldpath; cp++, oldpath++)
122 			if (*oldpath == NULL)
123 				break;
124 		count = cp - path;
125 		diffcount = count - oldcount + OFFSET;
126 		oldcount = count;
127 		if (diffcount < 0 || diffcount > 2 * OFFSET) {
128 			if (putchar(SWITCH) == EOF ||
129 			    putw(diffcount, stdout) == EOF)
130 				err(1, "stdout");
131 		} else
132 			if (putchar(diffcount) == EOF)
133 				err(1, "stdout");
134 
135 		while (*cp != NULL) {
136 			if (*(cp + 1) == NULL) {
137 				if (putchar(*cp) == EOF)
138 					err(1, "stdout");
139 				break;
140 			}
141 			if ((code = bgindex(cp)) < 0) {
142 				if (putchar(*cp++) == EOF ||
143 				    putchar(*cp++) == EOF)
144 					err(1, "stdout");
145 			} else {
146 				/* Found, so mark byte with parity bit. */
147 				if (putchar((code / 2) | PARITY) == EOF)
148 					err(1, "stdout");
149 				cp += 2;
150 			}
151 		}
152 		if (path == buf1) {		/* swap pointers */
153 			path = buf2;
154 			oldpath = buf1;
155 		} else {
156 			path = buf1;
157 			oldpath = buf2;
158 		}
159 	}
160 	/* Non-zero status if there were errors */
161 	if (fflush(stdout) != 0 || ferror(stdout))
162 		exit(1);
163 	exit(0);
164 }
165 
166 int
167 bgindex(bg)			/* Return location of bg in bigrams or -1. */
168 	char *bg;
169 {
170 	register char bg0, bg1, *p;
171 
172 	bg0 = bg[0];
173 	bg1 = bg[1];
174 	for (p = bigrams; *p != NULL; p++)
175 		if (*p++ == bg0 && *p == bg1)
176 			break;
177 	return (*p == NULL ? -1 : --p - bigrams);
178 }
179 
180 void
181 usage()
182 {
183 	(void)fprintf(stderr,
184 	    "usage: locate.code common_bigrams < list > squozen_list\n");
185 	exit(1);
186 }
187