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.3 (Berkeley) 04/28/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 != '\0'; cp++) {
114 			if ((u_char)*cp >= PARITY || *cp <= SWITCH)
115 				*cp = '?';
116 		}
117 
118 		/* Skip longest common prefix. */
119 		for (cp = path; *cp == *oldpath; cp++, oldpath++)
120 			if (*oldpath == '\0')
121 				break;
122 		count = cp - path;
123 		diffcount = count - oldcount + OFFSET;
124 		oldcount = count;
125 		if (diffcount < 0 || diffcount > 2 * OFFSET) {
126 			if (putchar(SWITCH) == EOF ||
127 			    putw(diffcount, stdout) == EOF)
128 				err(1, "stdout");
129 		} else
130 			if (putchar(diffcount) == EOF)
131 				err(1, "stdout");
132 
133 		while (*cp != '\0') {
134 			if (*(cp + 1) == '\0') {
135 				if (putchar(*cp) == EOF)
136 					err(1, "stdout");
137 				break;
138 			}
139 			if ((code = bgindex(cp)) < 0) {
140 				if (putchar(*cp++) == EOF ||
141 				    putchar(*cp++) == EOF)
142 					err(1, "stdout");
143 			} else {
144 				/* Found, so mark byte with parity bit. */
145 				if (putchar((code / 2) | PARITY) == EOF)
146 					err(1, "stdout");
147 				cp += 2;
148 			}
149 		}
150 		if (path == buf1) {		/* swap pointers */
151 			path = buf2;
152 			oldpath = buf1;
153 		} else {
154 			path = buf1;
155 			oldpath = buf2;
156 		}
157 	}
158 	/* Non-zero status if there were errors */
159 	if (fflush(stdout) != 0 || ferror(stdout))
160 		exit(1);
161 	exit(0);
162 }
163 
164 int
165 bgindex(bg)			/* Return location of bg in bigrams or -1. */
166 	char *bg;
167 {
168 	register char bg0, bg1, *p;
169 
170 	bg0 = bg[0];
171 	bg1 = bg[1];
172 	for (p = bigrams; *p != '\0'; p++)
173 		if (*p++ == bg0 && *p == bg1)
174 			break;
175 	return (*p == '\0' ? -1 : --p - bigrams);
176 }
177 
178 void
179 usage()
180 {
181 	(void)fprintf(stderr,
182 	    "usage: locate.code common_bigrams < list > squozen_list\n");
183 	exit(1);
184 }
185