Deleted Added
full compact
locate.code.c (5761) locate.code.c (17592)
1/*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * James A. Woods.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 75 unchanged lines hidden (view full) ---

84#include <errno.h>
85#include <stdlib.h>
86#include <string.h>
87#include <stdio.h>
88#include "locate.h"
89
90#define BGBUFSIZE (NBG * 2) /* size of bigram buffer */
91
1/*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * James A. Woods.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 75 unchanged lines hidden (view full) ---

84#include <errno.h>
85#include <stdlib.h>
86#include <string.h>
87#include <stdio.h>
88#include "locate.h"
89
90#define BGBUFSIZE (NBG * 2) /* size of bigram buffer */
91
92char buf1[MAXPATHLEN + 1] = " ";
93char buf2[MAXPATHLEN + 1];
92u_char buf1[MAXPATHLEN] = " ";
93u_char buf2[MAXPATHLEN];
94char bigrams[BGBUFSIZE + 1] = { 0 };
95
94char bigrams[BGBUFSIZE + 1] = { 0 };
95
96#define LOOKUP 1
97#ifdef LOOKUP
98#define BGINDEX(x) (big[(u_int)*x][(u_int)*(x+1)])
99typedef u_char bg_t;
100bg_t big[UCHAR_MAX][UCHAR_MAX];
101
102#else
103#define BGINDEX(x) bgindex(x)
104typedef int bg_t;
105#endif
106
96int bgindex __P((char *));
97void usage __P((void));
107int bgindex __P((char *));
108void usage __P((void));
109extern int optind;
110extern int optopt;
98
99int
100main(argc, argv)
101 int argc;
102 char *argv[];
103{
111
112int
113main(argc, argv)
114 int argc;
115 char *argv[];
116{
104 register char *cp, *oldpath, *path;
117 register u_char *cp, *oldpath, *path;
105 int ch, code, count, diffcount, oldcount;
106 FILE *fp;
118 int ch, code, count, diffcount, oldcount;
119 FILE *fp;
120 register int i, j;
107
108 while ((ch = getopt(argc, argv, "")) != EOF)
109 switch(ch) {
121
122 while ((ch = getopt(argc, argv, "")) != EOF)
123 switch(ch) {
110 case '?':
111 default:
112 usage();
113 }
114 argc -= optind;
115 argv += optind;
116
117 if (argc != 1)
118 usage();
119
120 if ((fp = fopen(argv[0], "r")) == NULL)
121 err(1, "%s", argv[0]);
122
123 /* First copy bigram array to stdout. */
124 (void)fgets(bigrams, BGBUFSIZE + 1, fp);
125 if (fwrite(bigrams, 1, BGBUFSIZE, stdout) != BGBUFSIZE)
126 err(1, "stdout");
127 (void)fclose(fp);
128
124 default:
125 usage();
126 }
127 argc -= optind;
128 argv += optind;
129
130 if (argc != 1)
131 usage();
132
133 if ((fp = fopen(argv[0], "r")) == NULL)
134 err(1, "%s", argv[0]);
135
136 /* First copy bigram array to stdout. */
137 (void)fgets(bigrams, BGBUFSIZE + 1, fp);
138 if (fwrite(bigrams, 1, BGBUFSIZE, stdout) != BGBUFSIZE)
139 err(1, "stdout");
140 (void)fclose(fp);
141
142#ifdef LOOKUP
143 /* init lookup table */
144 for (i = 0; i < UCHAR_MAX; i++)
145 for (j = 0; j < UCHAR_MAX; j++)
146 big[i][j] = (bg_t)-1;
147
148 for (cp = bigrams, i = 0; *cp != NULL; i += 2, cp += 2)
149 big[(int)*cp][(int)*(cp + 1)] = (bg_t)i;
150#endif
151
129 oldpath = buf1;
130 path = buf2;
131 oldcount = 0;
152 oldpath = buf1;
153 path = buf2;
154 oldcount = 0;
132 while (fgets(path, sizeof(buf2) - 1, stdin) != NULL) {
133 /* Truncate newline. */
134 cp = path + strlen(path) - 1;
135 if (cp > path && *cp == '\n')
136 *cp = '\0';
155 while (fgets(path, sizeof(buf2), stdin) != NULL) {
137
156
157 /* skip empty lines */
158 if (*path == '\n')
159 continue;
160
138 /* Squelch characters that would botch the decoding. */
139 for (cp = path; *cp != NULL; cp++) {
161 /* Squelch characters that would botch the decoding. */
162 for (cp = path; *cp != NULL; cp++) {
140 if ((u_char)*cp >= PARITY)
141 *cp &= PARITY-1;
142 if (*cp <= SWITCH)
163 /* chop newline */
164 if (*cp == '\n')
165 *cp = NULL;
166 /* range */
167 else if (*cp < ASCII_MIN || *cp > ASCII_MAX)
143 *cp = '?';
144 }
145
146 /* Skip longest common prefix. */
168 *cp = '?';
169 }
170
171 /* Skip longest common prefix. */
147 for (cp = path; *cp == *oldpath; cp++, oldpath++)
148 if (*oldpath == NULL)
149 break;
172 for (cp = path; *cp == *oldpath && *cp; cp++, oldpath++);
173
150 count = cp - path;
151 diffcount = count - oldcount + OFFSET;
152 oldcount = count;
153 if (diffcount < 0 || diffcount > 2 * OFFSET) {
154 if (putchar(SWITCH) == EOF ||
155 putw(diffcount, stdout) == EOF)
156 err(1, "stdout");
157 } else
158 if (putchar(diffcount) == EOF)
159 err(1, "stdout");
160
161 while (*cp != NULL) {
162 if (*(cp + 1) == NULL) {
163 if (putchar(*cp) == EOF)
164 err(1, "stdout");
165 break;
166 }
174 count = cp - path;
175 diffcount = count - oldcount + OFFSET;
176 oldcount = count;
177 if (diffcount < 0 || diffcount > 2 * OFFSET) {
178 if (putchar(SWITCH) == EOF ||
179 putw(diffcount, stdout) == EOF)
180 err(1, "stdout");
181 } else
182 if (putchar(diffcount) == EOF)
183 err(1, "stdout");
184
185 while (*cp != NULL) {
186 if (*(cp + 1) == NULL) {
187 if (putchar(*cp) == EOF)
188 err(1, "stdout");
189 break;
190 }
167 if ((code = bgindex(cp)) < 0) {
191 if ((code = BGINDEX(cp)) == (bg_t)-1) {
168 if (putchar(*cp++) == EOF ||
169 putchar(*cp++) == EOF)
170 err(1, "stdout");
171 } else {
172 /* Found, so mark byte with parity bit. */
173 if (putchar((code / 2) | PARITY) == EOF)
174 err(1, "stdout");
175 cp += 2;

--- 8 unchanged lines hidden (view full) ---

184 }
185 }
186 /* Non-zero status if there were errors */
187 if (fflush(stdout) != 0 || ferror(stdout))
188 exit(1);
189 exit(0);
190}
191
192 if (putchar(*cp++) == EOF ||
193 putchar(*cp++) == EOF)
194 err(1, "stdout");
195 } else {
196 /* Found, so mark byte with parity bit. */
197 if (putchar((code / 2) | PARITY) == EOF)
198 err(1, "stdout");
199 cp += 2;

--- 8 unchanged lines hidden (view full) ---

208 }
209 }
210 /* Non-zero status if there were errors */
211 if (fflush(stdout) != 0 || ferror(stdout))
212 exit(1);
213 exit(0);
214}
215
216#ifndef LOOKUP
192int
193bgindex(bg) /* Return location of bg in bigrams or -1. */
194 char *bg;
195{
196 register char bg0, bg1, *p;
197
198 bg0 = bg[0];
199 bg1 = bg[1];
200 for (p = bigrams; *p != NULL; p++)
201 if (*p++ == bg0 && *p == bg1)
202 break;
203 return (*p == NULL ? -1 : --p - bigrams);
204}
217int
218bgindex(bg) /* Return location of bg in bigrams or -1. */
219 char *bg;
220{
221 register char bg0, bg1, *p;
222
223 bg0 = bg[0];
224 bg1 = bg[1];
225 for (p = bigrams; *p != NULL; p++)
226 if (*p++ == bg0 && *p == bg1)
227 break;
228 return (*p == NULL ? -1 : --p - bigrams);
229}
230#endif /* !LOOKUP */
205
206void
207usage()
208{
209 (void)fprintf(stderr,
210 "usage: locate.code common_bigrams < list > squozen_list\n");
211 exit(1);
212}
231
232void
233usage()
234{
235 (void)fprintf(stderr,
236 "usage: locate.code common_bigrams < list > squozen_list\n");
237 exit(1);
238}