137637Sbostic /*
237637Sbostic  * Copyright (c) 1989 The Regents of the University of California.
337637Sbostic  * All rights reserved.
437637Sbostic  *
537637Sbostic  * This code is derived from software contributed to Berkeley by
637637Sbostic  * James A. Woods.
737637Sbostic  *
837637Sbostic  * Redistribution and use in source and binary forms are permitted
937637Sbostic  * provided that the above copyright notice and this paragraph are
1037637Sbostic  * duplicated in all such forms and that any documentation,
1137637Sbostic  * advertising materials, and other materials related to such
1237637Sbostic  * distribution and use acknowledge that the software was developed
1337637Sbostic  * by the University of California, Berkeley.  The name of the
1437637Sbostic  * University may not be used to endorse or promote products derived
1537637Sbostic  * from this software without specific prior written permission.
1637637Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1737637Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1837637Sbostic  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1937637Sbostic  */
2037637Sbostic 
2114040Smckusick #ifndef lint
2237637Sbostic char copyright[] =
2337637Sbostic "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
2437637Sbostic  All rights reserved.\n";
2537637Sbostic #endif /* not lint */
2614040Smckusick 
2737637Sbostic #ifndef lint
28*38085Sbostic static char sccsid[] = "@(#)locate.code.c	4.5 (Berkeley) 05/19/89";
2937637Sbostic #endif /* not lint */
3037637Sbostic 
3114040Smckusick /*
3214040Smckusick  * PURPOSE:	sorted list compressor (works with a modified 'find'
3314040Smckusick  *		to encode/decode a filename database)
3414040Smckusick  *
3514051Smckusick  * USAGE:	bigram < list > bigrams
3614051Smckusick  *		process bigrams (see updatedb) > common_bigrams
3714051Smckusick  *		code common_bigrams < list > squozen_list
3814040Smckusick  *
3937636Sjak  * METHOD:	Uses 'front compression' (see ";login:", Volume 8, Number 1
4037636Sjak  *		February/March 1983, p. 8 ).  Output format is, per line, an
4137636Sjak  *		offset differential count byte followed by a partially bigram-
4237636Sjak  *		encoded ascii residue.  A bigram is a two-character sequence,
4337636Sjak  *		the first 128 most common of which are encoded in one byte.
4414040Smckusick  *
4537636Sjak  * EXAMPLE:	For simple front compression with no bigram encoding,
4637636Sjak  *		if the input is...		then the output is...
4737636Sjak  *
4837636Sjak  *		/usr/src			 0 /usr/src
4937636Sjak  *		/usr/src/cmd/aardvark.c		 8 /cmd/aardvark.c
5037636Sjak  *		/usr/src/cmd/armadillo.c	14 armadillo.c
5137636Sjak  *		/usr/tmp/zoo			 5 tmp/zoo
5237636Sjak  *
5314040Smckusick  *  	The codes are:
5414040Smckusick  *
5514040Smckusick  *	0-28	likeliest differential counts + offset to make nonnegative
5637636Sjak  *	30	switch code for out-of-range count to follow in next word
5737636Sjak  *	128-255 bigram codes (128 most common, as determined by 'updatedb')
5837636Sjak  *	32-127  single character (printable) ascii residue (ie, literal)
5914040Smckusick  *
6014051Smckusick  * SEE ALSO:	updatedb.csh, bigram.c, find.c
6114040Smckusick  *
6214040Smckusick  * AUTHOR:	James A. Woods, Informatics General Corp.,
6314040Smckusick  *		NASA Ames Research Center, 10/82
6414040Smckusick  */
6514040Smckusick 
6614040Smckusick #include <stdio.h>
6737636Sjak #include <sys/param.h>
6837636Sjak #include "find.h"
6914040Smckusick 
7037636Sjak #define BGBUFSIZE	(NBG * 2)	/* size of bigram buffer */
7114040Smckusick 
7237636Sjak char buf1[MAXPATHLEN] = " ";
7337636Sjak char buf2[MAXPATHLEN];
7437636Sjak char bigrams[BGBUFSIZE + 1] = { 0 };
7514040Smckusick 
7614040Smckusick main ( argc, argv )
7714040Smckusick 	int argc; char *argv[];
7814040Smckusick {
7937636Sjak 	register char *cp, *oldpath = buf1, *path = buf2;
8037636Sjak   	int code, count, diffcount, oldcount = 0;
8114040Smckusick 	FILE *fp;
8214040Smckusick 
8314051Smckusick 	if ((fp = fopen(argv[1], "r")) == NULL) {
8437636Sjak 		printf("Usage: code common_bigrams < list > squozen_list\n");
8514051Smckusick 		exit(1);
8614051Smckusick 	}
8737636Sjak 	/* first copy bigram array to stdout */
8837636Sjak 	fgets ( bigrams, BGBUFSIZE + 1, fp );
8937636Sjak 	fwrite ( bigrams, 1, BGBUFSIZE, stdout );
9037636Sjak 	fclose( fp );
9114040Smckusick 
92*38085Sbostic      	while ( fgets ( path, sizeof(buf2), stdin ) != NULL ) {
9337636Sjak 		/* squelch characters that would botch the decoding */
9437636Sjak 		for ( cp = path; *cp != NULL; cp++ ) {
9537636Sjak 			if ( *cp >= PARITY )
9637636Sjak 				*cp &= PARITY-1;
9737636Sjak 			else if ( *cp <= SWITCH )
9837636Sjak 				*cp = '?';
9914040Smckusick 		}
10037636Sjak 		/* skip longest common prefix */
10137636Sjak 		for ( cp = path; *cp == *oldpath; cp++, oldpath++ )
10237636Sjak 			if ( *oldpath == NULL )
10337636Sjak 				break;
10437636Sjak 		count = cp - path;
10537636Sjak 		diffcount = count - oldcount + OFFSET;
10637636Sjak 		oldcount = count;
10737636Sjak 		if ( diffcount < 0 || diffcount > 2*OFFSET ) {
10837636Sjak 			putc ( SWITCH, stdout );
10937636Sjak 			putw ( diffcount, stdout );
11014040Smckusick 		}
11114040Smckusick 		else
11237636Sjak 			putc ( diffcount, stdout );
11314040Smckusick 
11437636Sjak 		while ( *cp != NULL ) {
11537636Sjak 			if ( *(cp + 1) == NULL ) {
11637636Sjak 				putchar ( *cp );
11714040Smckusick 				break;
11814040Smckusick 			}
11937636Sjak 			if ( (code = bgindex ( cp )) < 0 ) {
12037636Sjak 				putchar ( *cp++ );
12137636Sjak 				putchar ( *cp++ );
12237636Sjak 			}
12337636Sjak 			else {	/* found, so mark byte with parity bit */
12437636Sjak 				putchar ( (code / 2) | PARITY );
12537636Sjak 				cp += 2;
12637636Sjak 			}
12714040Smckusick 		}
12837636Sjak 		if ( path == buf1 )		/* swap pointers */
12937636Sjak 			path = buf2, oldpath = buf1;
13037636Sjak 		else
13137636Sjak 			path = buf1, oldpath = buf2;
13214040Smckusick 	}
13314040Smckusick }
13414040Smckusick 
13537636Sjak bgindex ( bg )			/* return location of bg in bigrams or -1 */
13637636Sjak 	char *bg;
13714040Smckusick {
13837636Sjak 	register char *p;
13937636Sjak 	register char bg0 = bg[0], bg1 = bg[1];
14014040Smckusick 
14137636Sjak 	for ( p = bigrams; *p != NULL; p++ )
14237636Sjak 		if ( *p++ == bg0 && *p == bg1 )
14337636Sjak 			break;
14437636Sjak 	return ( *p == NULL ? -1 : --p - bigrams );
14514040Smckusick }
146