|
|
1.1 root 1: #include <stdio.h>
2: #include <ctype.h>
3:
4: #define DICT "/s5/oed/conv.raw"
5:
6: char *filename = DICT;
7: extern char *ggets(), *fgets(), *strchr();
8: FILE *dfile;
9:
10: int exact;
11: int iflag;
12: int acomp();
13: int (*compare)() = acomp;
14: int tab = 0374;
15: char entry[250];
16: char word[250];
17: char key[50];
18:
19: main(argc,argv)
20: char **argv;
21: {
22: char *arg;
23: while(argc>=2 && *argv[1]=='-') {
24: for(arg=argv[1];;arg++) {
25: switch(arg[1]) {
26: case 'i':
27: iflag++;
28: continue;
29: case 'x':
30: exact++;
31: continue;
32: case 0:
33: break;
34: default:
35: fprintf(stderr,"look: bad option %s\n",arg);
36: return(2);
37: }
38: break;
39: }
40: argc --;
41: argv++;
42: }
43: if(!iflag) {
44: if(argc<2)
45: iflag++;
46: else {
47: canon(argv[1],key);
48: argv++;
49: argc--;
50: }
51: }
52: if(argc>=2)
53: filename = argv[1];
54: dfile = fopen(filename,"r");
55: if(dfile==NULL) {
56: fprintf(stderr,"look: can't open %s\n",filename);
57: return(2);
58: }
59: if(!iflag) {
60: if(locate(key,entry)==0)
61: return(1);
62: }
63: do {
64: if(iflag) {
65: if(ggets(entry,sizeof entry,stdin)==0)
66: return(0);
67: canon(entry,key);
68: if(locate(key,entry)==0)
69: continue;
70: }
71: put(entry,dfile,stdout);
72: while(getword(entry)) {
73: canon(entry,word);
74: switch((*compare)(key,word)) {
75: case -1:
76: if(exact)
77: break;
78: case 0:
79: put(entry,dfile,stdout);
80: continue;
81: }
82: break;
83: }
84: } while(iflag);
85: return(0);
86:
87: }
88:
89: locate(key,entry)
90: char *key;
91: {
92: long top,bot,mid;
93: register c;
94: bot = 0;
95: fseek(dfile,0L,2);
96: top = ftell(dfile);
97: for(;;) {
98: mid = (top+bot)/2;
99: fseek(dfile,mid,0);
100: do {
101: c = getc(dfile);
102: mid++;
103: } while(c!=EOF && c!='\n');
104: if(!getword(entry))
105: break;
106: canon(entry,word);
107: switch((*compare)(key,word)) {
108: case -2:
109: case -1:
110: case 0:
111: if(top<=mid)
112: break;
113: top = mid;
114: continue;
115: case 1:
116: case 2:
117: bot = mid;
118: continue;
119: }
120: break;
121: }
122: fseek(dfile,bot,0);
123: while(getword(entry)) {
124: canon(entry,word);
125: switch((*compare)(key,word)) {
126: case -2:
127: return(0);
128: case -1:
129: if(exact)
130: return(0);
131: case 0:
132: return(1);
133: case 1:
134: case 2:
135: while((c=getc(dfile))!='\n' && c!=EOF)
136: continue;
137: }
138: }
139: return(0);
140: }
141:
142: /* acomp(s,t) returns -2 if s strictly precedes t
143: -1 if s is a prefix of t
144: 0 if s is the same as t
145: 1 if t is a prefix of s
146: 2 if t strictly precedes s
147: */
148:
149: acomp(s,t)
150: register char *s,*t;
151: {
152: for(;*s==*t;s++,t++)
153: if(*s==0)
154: return(0);
155: return(*s==0? -1:
156: *t==0? 1:
157: *s<*t? -2:
158: 2);
159: }
160:
161: getword(w)
162: char *w;
163: {
164: register c;
165: for(;;) {
166: c = getc(dfile);
167: if(c==EOF)
168: return(0);
169: if(c==tab)
170: break;
171: *w++ = c;
172: }
173: *w = 0;
174: return(1);
175: }
176:
177: put(entry, ifile, ofile)
178: char *entry;
179: FILE *ofile, *ifile;
180: {
181: fputs(entry, ofile);
182: while( putc(getc(ifile), ofile) != '\n')
183: continue;
184: fflush(ofile);
185: }
186:
187: canon(old,new)
188: unsigned char *old,*new;
189: {
190: register c;
191: unsigned char *savo = old, *savn = new;
192: for(;;) {
193: *new = c = *old++;
194: if(c==0||c==tab||c==',') {
195: *new = 0;
196: break;
197: }
198: if(c=='+') {
199: if(old[0]=='9')
200: old++;
201: if(old[0]=='2') {
202: if(old[1]=='3')
203: *new++ = 'a', *new++ = 'e';
204: else if(old[1]=='4')
205: *new++ = 'o', *new++ = 'e';
206: }
207: for(;; old++) {
208: c = *old;
209: if(c==' ' || c=='|' || c==tab)
210: break;
211: }
212: }
213: if(!isalpha(c))
214: continue;
215: if(isupper(c))
216: *new += 'a' - 'A';
217: new++;
218: }
219: /* printf(">%s %s\n",savo,savn); */
220: }
221:
222: char *
223: ggets(s,n,f)
224: char *s;
225: FILE *f;
226: {
227: char *p = fgets(s,n,f);
228: char *q;
229: if(p && (q=strchr(s,'\n')))
230: *q = 0;
231: return p;
232: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.