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