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