1 1.12 christos /* $NetBSD: locate.code.c,v 1.12 2019/10/13 19:39:15 christos 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.12 christos __RCSID("$NetBSD: locate.code.c,v 1.12 2019/10/13 19:39:15 christos 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.11 joerg static char buf1[MAXPATHLEN] = " "; 97 1.11 joerg static char buf2[MAXPATHLEN]; 98 1.11 joerg static char bigrams[BGBUFSIZE + 1] = { 0 }; 99 1.11 joerg 100 1.11 joerg static int bgindex(char *); 101 1.11 joerg __dead static void usage(void); 102 1.3 jtc 103 1.3 jtc int 104 1.11 joerg main(int argc, char *argv[]) 105 1.1 cgd { 106 1.6 lukem char *cp, *oldpath, *path; 107 1.3 jtc int ch, code, count, diffcount, oldcount; 108 1.1 cgd 109 1.6 lukem while ((ch = getopt(argc, argv, "")) != -1) 110 1.3 jtc switch(ch) { 111 1.3 jtc case '?': 112 1.3 jtc default: 113 1.3 jtc usage(); 114 1.3 jtc } 115 1.3 jtc argc -= optind; 116 1.3 jtc argv += optind; 117 1.3 jtc 118 1.3 jtc if (argc != 1) 119 1.3 jtc usage(); 120 1.3 jtc 121 1.3 jtc /* First copy bigram array to stdout. */ 122 1.12 christos strncpy(bigrams, argv[0], BGBUFSIZE); 123 1.12 christos bigrams[BGBUFSIZE] = '\0'; 124 1.8 mycroft if (fwrite(bigrams, 1, BGBUFSIZE, stdout) != BGBUFSIZE) 125 1.3 jtc err(1, "stdout"); 126 1.3 jtc 127 1.3 jtc oldpath = buf1; 128 1.3 jtc path = buf2; 129 1.3 jtc oldcount = 0; 130 1.3 jtc while (fgets(path, sizeof(buf2), stdin) != NULL) { 131 1.3 jtc /* Truncate newline. */ 132 1.1 cgd cp = path + strlen(path) - 1; 133 1.1 cgd if (cp > path && *cp == '\n') 134 1.1 cgd *cp = '\0'; 135 1.3 jtc 136 1.3 jtc /* Squelch characters that would botch the decoding. */ 137 1.5 jtc for (cp = path; *cp != '\0'; cp++) { 138 1.4 jtc *cp &= PARITY-1; 139 1.4 jtc if (*cp <= SWITCH) 140 1.1 cgd *cp = '?'; 141 1.1 cgd } 142 1.3 jtc 143 1.3 jtc /* Skip longest common prefix. */ 144 1.3 jtc for (cp = path; *cp == *oldpath; cp++, oldpath++) 145 1.5 jtc if (*oldpath == '\0') 146 1.1 cgd break; 147 1.1 cgd count = cp - path; 148 1.1 cgd diffcount = count - oldcount + OFFSET; 149 1.1 cgd oldcount = count; 150 1.3 jtc if (diffcount < 0 || diffcount > 2 * OFFSET) { 151 1.3 jtc if (putchar(SWITCH) == EOF || 152 1.3 jtc putw(diffcount, stdout) == EOF) 153 1.3 jtc err(1, "stdout"); 154 1.3 jtc } else 155 1.3 jtc if (putchar(diffcount) == EOF) 156 1.3 jtc err(1, "stdout"); 157 1.3 jtc 158 1.5 jtc while (*cp != '\0') { 159 1.5 jtc if (*(cp + 1) == '\0') { 160 1.3 jtc if (putchar(*cp) == EOF) 161 1.3 jtc err(1, "stdout"); 162 1.1 cgd break; 163 1.1 cgd } 164 1.3 jtc if ((code = bgindex(cp)) < 0) { 165 1.3 jtc if (putchar(*cp++) == EOF || 166 1.3 jtc putchar(*cp++) == EOF) 167 1.3 jtc err(1, "stdout"); 168 1.3 jtc } else { 169 1.3 jtc /* Found, so mark byte with parity bit. */ 170 1.3 jtc if (putchar((code / 2) | PARITY) == EOF) 171 1.3 jtc err(1, "stdout"); 172 1.1 cgd cp += 2; 173 1.1 cgd } 174 1.1 cgd } 175 1.3 jtc if (path == buf1) { /* swap pointers */ 176 1.3 jtc path = buf2; 177 1.3 jtc oldpath = buf1; 178 1.3 jtc } else { 179 1.3 jtc path = buf1; 180 1.3 jtc oldpath = buf2; 181 1.3 jtc } 182 1.1 cgd } 183 1.3 jtc /* Non-zero status if there were errors */ 184 1.3 jtc if (fflush(stdout) != 0 || ferror(stdout)) 185 1.3 jtc exit(1); 186 1.3 jtc exit(0); 187 1.1 cgd } 188 1.1 cgd 189 1.11 joerg static int 190 1.11 joerg bgindex(char *bg) /* Return location of bg in bigrams or -1. */ 191 1.1 cgd { 192 1.6 lukem char bg0, bg1, *p; 193 1.1 cgd 194 1.3 jtc bg0 = bg[0]; 195 1.3 jtc bg1 = bg[1]; 196 1.8 mycroft for (p = bigrams; *p != '\0'; p += 2) 197 1.8 mycroft if (p[0] == bg0 && p[1] == bg1) 198 1.1 cgd break; 199 1.8 mycroft return (*p == '\0' ? -1 : p - bigrams); 200 1.3 jtc } 201 1.3 jtc 202 1.11 joerg static void 203 1.11 joerg usage(void) 204 1.3 jtc { 205 1.3 jtc (void)fprintf(stderr, 206 1.3 jtc "usage: locate.code common_bigrams < list > squozen_list\n"); 207 1.3 jtc exit(1); 208 1.1 cgd } 209