Home | History | Annotate | Line # | Download | only in code
locate.code.c revision 1.5
      1  1.5  jtc /*	$NetBSD: locate.code.c,v 1.5 1995/08/31 22:36:33 jtc 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.1  cgd #ifndef lint
     40  1.3  jtc static char copyright[] =
     41  1.3  jtc "@(#) Copyright (c) 1989, 1993\n\
     42  1.3  jtc 	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.5  jtc static char rcsid[] = "$NetBSD: locate.code.c,v 1.5 1995/08/31 22:36:33 jtc 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.1  cgd char bigrams[BGBUFSIZE + 1] = { 0 };
    103  1.1  cgd 
    104  1.3  jtc int	bgindex __P((char *));
    105  1.3  jtc void	usage __P((void));
    106  1.3  jtc 
    107  1.3  jtc int
    108  1.3  jtc main(argc, argv)
    109  1.3  jtc 	int argc;
    110  1.3  jtc 	char *argv[];
    111  1.1  cgd {
    112  1.3  jtc 	register char *cp, *oldpath, *path;
    113  1.3  jtc 	int ch, code, count, diffcount, oldcount;
    114  1.1  cgd 	FILE *fp;
    115  1.1  cgd 
    116  1.3  jtc 	while ((ch = getopt(argc, argv, "")) != EOF)
    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 	if ((fp = fopen(argv[0], "r")) == NULL)
    129  1.3  jtc 		err(1, "%s", argv[0]);
    130  1.1  cgd 
    131  1.3  jtc 	/* First copy bigram array to stdout. */
    132  1.3  jtc 	(void)fgets(bigrams, BGBUFSIZE + 1, fp);
    133  1.3  jtc 	if (fwrite(bigrams, 1, BGBUFSIZE, stdout) != BGBUFSIZE)
    134  1.3  jtc 		err(1, "stdout");
    135  1.3  jtc 	(void)fclose(fp);
    136  1.3  jtc 
    137  1.3  jtc 	oldpath = buf1;
    138  1.3  jtc 	path = buf2;
    139  1.3  jtc 	oldcount = 0;
    140  1.3  jtc 	while (fgets(path, sizeof(buf2), stdin) != NULL) {
    141  1.3  jtc 		/* Truncate newline. */
    142  1.1  cgd 		cp = path + strlen(path) - 1;
    143  1.1  cgd 		if (cp > path && *cp == '\n')
    144  1.1  cgd 			*cp = '\0';
    145  1.3  jtc 
    146  1.3  jtc 		/* Squelch characters that would botch the decoding. */
    147  1.5  jtc 		for (cp = path; *cp != '\0'; cp++) {
    148  1.4  jtc 			*cp &= PARITY-1;
    149  1.4  jtc 			if (*cp <= SWITCH)
    150  1.1  cgd 				*cp = '?';
    151  1.1  cgd 		}
    152  1.3  jtc 
    153  1.3  jtc 		/* Skip longest common prefix. */
    154  1.3  jtc 		for (cp = path; *cp == *oldpath; cp++, oldpath++)
    155  1.5  jtc 			if (*oldpath == '\0')
    156  1.1  cgd 				break;
    157  1.1  cgd 		count = cp - path;
    158  1.1  cgd 		diffcount = count - oldcount + OFFSET;
    159  1.1  cgd 		oldcount = count;
    160  1.3  jtc 		if (diffcount < 0 || diffcount > 2 * OFFSET) {
    161  1.3  jtc 			if (putchar(SWITCH) == EOF ||
    162  1.3  jtc 			    putw(diffcount, stdout) == EOF)
    163  1.3  jtc 				err(1, "stdout");
    164  1.3  jtc 		} else
    165  1.3  jtc 			if (putchar(diffcount) == EOF)
    166  1.3  jtc 				err(1, "stdout");
    167  1.3  jtc 
    168  1.5  jtc 		while (*cp != '\0') {
    169  1.5  jtc 			if (*(cp + 1) == '\0') {
    170  1.3  jtc 				if (putchar(*cp) == EOF)
    171  1.3  jtc 					err(1, "stdout");
    172  1.1  cgd 				break;
    173  1.1  cgd 			}
    174  1.3  jtc 			if ((code = bgindex(cp)) < 0) {
    175  1.3  jtc 				if (putchar(*cp++) == EOF ||
    176  1.3  jtc 				    putchar(*cp++) == EOF)
    177  1.3  jtc 					err(1, "stdout");
    178  1.3  jtc 			} else {
    179  1.3  jtc 				/* Found, so mark byte with parity bit. */
    180  1.3  jtc 				if (putchar((code / 2) | PARITY) == EOF)
    181  1.3  jtc 					err(1, "stdout");
    182  1.1  cgd 				cp += 2;
    183  1.1  cgd 			}
    184  1.1  cgd 		}
    185  1.3  jtc 		if (path == buf1) {		/* swap pointers */
    186  1.3  jtc 			path = buf2;
    187  1.3  jtc 			oldpath = buf1;
    188  1.3  jtc 		} else {
    189  1.3  jtc 			path = buf1;
    190  1.3  jtc 			oldpath = buf2;
    191  1.3  jtc 		}
    192  1.1  cgd 	}
    193  1.3  jtc 	/* Non-zero status if there were errors */
    194  1.3  jtc 	if (fflush(stdout) != 0 || ferror(stdout))
    195  1.3  jtc 		exit(1);
    196  1.3  jtc 	exit(0);
    197  1.1  cgd }
    198  1.1  cgd 
    199  1.3  jtc int
    200  1.3  jtc bgindex(bg)			/* Return location of bg in bigrams or -1. */
    201  1.1  cgd 	char *bg;
    202  1.1  cgd {
    203  1.3  jtc 	register char bg0, bg1, *p;
    204  1.1  cgd 
    205  1.3  jtc 	bg0 = bg[0];
    206  1.3  jtc 	bg1 = bg[1];
    207  1.5  jtc 	for (p = bigrams; *p != '\0'; p++)
    208  1.3  jtc 		if (*p++ == bg0 && *p == bg1)
    209  1.1  cgd 			break;
    210  1.5  jtc 	return (*p == '\0' ? -1 : --p - bigrams);
    211  1.3  jtc }
    212  1.3  jtc 
    213  1.3  jtc void
    214  1.3  jtc usage()
    215  1.3  jtc {
    216  1.3  jtc 	(void)fprintf(stderr,
    217  1.3  jtc 	    "usage: locate.code common_bigrams < list > squozen_list\n");
    218  1.3  jtc 	exit(1);
    219  1.1  cgd }
    220