Home | History | Annotate | Line # | Download | only in code
locate.code.c revision 1.4
      1 /*	$NetBSD: locate.code.c,v 1.4 1995/05/06 06:39:33 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.4 1995/05/06 06:39:33 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 			*cp &= PARITY-1;
    146 			if (*cp <= SWITCH)
    147 				*cp = '?';
    148 		}
    149 
    150 		/* Skip longest common prefix. */
    151 		for (cp = path; *cp == *oldpath; cp++, oldpath++)
    152 			if (*oldpath == NULL)
    153 				break;
    154 		count = cp - path;
    155 		diffcount = count - oldcount + OFFSET;
    156 		oldcount = count;
    157 		if (diffcount < 0 || diffcount > 2 * OFFSET) {
    158 			if (putchar(SWITCH) == EOF ||
    159 			    putw(diffcount, stdout) == EOF)
    160 				err(1, "stdout");
    161 		} else
    162 			if (putchar(diffcount) == EOF)
    163 				err(1, "stdout");
    164 
    165 		while (*cp != NULL) {
    166 			if (*(cp + 1) == NULL) {
    167 				if (putchar(*cp) == EOF)
    168 					err(1, "stdout");
    169 				break;
    170 			}
    171 			if ((code = bgindex(cp)) < 0) {
    172 				if (putchar(*cp++) == EOF ||
    173 				    putchar(*cp++) == EOF)
    174 					err(1, "stdout");
    175 			} else {
    176 				/* Found, so mark byte with parity bit. */
    177 				if (putchar((code / 2) | PARITY) == EOF)
    178 					err(1, "stdout");
    179 				cp += 2;
    180 			}
    181 		}
    182 		if (path == buf1) {		/* swap pointers */
    183 			path = buf2;
    184 			oldpath = buf1;
    185 		} else {
    186 			path = buf1;
    187 			oldpath = buf2;
    188 		}
    189 	}
    190 	/* Non-zero status if there were errors */
    191 	if (fflush(stdout) != 0 || ferror(stdout))
    192 		exit(1);
    193 	exit(0);
    194 }
    195 
    196 int
    197 bgindex(bg)			/* Return location of bg in bigrams or -1. */
    198 	char *bg;
    199 {
    200 	register char bg0, bg1, *p;
    201 
    202 	bg0 = bg[0];
    203 	bg1 = bg[1];
    204 	for (p = bigrams; *p != NULL; p++)
    205 		if (*p++ == bg0 && *p == bg1)
    206 			break;
    207 	return (*p == NULL ? -1 : --p - bigrams);
    208 }
    209 
    210 void
    211 usage()
    212 {
    213 	(void)fprintf(stderr,
    214 	    "usage: locate.code common_bigrams < list > squozen_list\n");
    215 	exit(1);
    216 }
    217