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