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