Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include "dict.h"
6 Dict dicts[] = {
7 {"oed", "Oxford English Dictionary, 2nd Ed.",
8 "#9/dict/oed2", "#9/dict/oed2index",
9 oednextoff, oedprintentry, oedprintkey},
10 {"ahd", "American Heritage Dictionary, 2nd College Ed.",
11 "ahd/DICT.DB", "ahd/index",
12 ahdnextoff, ahdprintentry, ahdprintkey},
13 {"pgw", "Project Gutenberg Webster Dictionary",
14 "#9/dict/pgw", "#9/dict/pgwindex",
15 pgwnextoff, pgwprintentry, pgwprintkey},
16 {"thesaurus", "Collins Thesaurus",
17 "#9/dict/thesaurus", "#9/dict/thesindex",
18 thesnextoff, thesprintentry, thesprintkey},
20 {"ce", "Gendai Chinese->English",
21 "#9/dict/world/sansdata/sandic24.dat",
22 "#9/dict/world/sansdata/ceindex",
23 worldnextoff, worldprintentry, worldprintkey},
24 {"ceh", "Gendai Chinese->English (Hanzi index)",
25 "#9/dict/world/sansdata/sandic24.dat",
26 "#9/dict/world/sansdata/cehindex",
27 worldnextoff, worldprintentry, worldprintkey},
28 {"ec", "Gendai English->Chinese",
29 "#9/dict/world/sansdata/sandic24.dat",
30 "#9/dict/world/sansdata/ecindex",
31 worldnextoff, worldprintentry, worldprintkey},
33 {"dae", "Gyldendal Danish->English",
34 "#9/dict/world/gylddata/sandic30.dat",
35 "#9/dict/world/gylddata/daeindex",
36 worldnextoff, worldprintentry, worldprintkey},
37 {"eda", "Gyldendal English->Danish",
38 "#9/dict/world/gylddata/sandic29.dat",
39 "#9/dict/world/gylddata/edaindex",
40 worldnextoff, worldprintentry, worldprintkey},
42 {"due", "Wolters-Noordhoff Dutch->English",
43 "#9/dict/world/woltdata/sandic07.dat",
44 "#9/dict/world/woltdata/deindex",
45 worldnextoff, worldprintentry, worldprintkey},
46 {"edu", "Wolters-Noordhoff English->Dutch",
47 "#9/dict/world/woltdata/sandic06.dat",
48 "#9/dict/world/woltdata/edindex",
49 worldnextoff, worldprintentry, worldprintkey},
51 {"fie", "WSOY Finnish->English",
52 "#9/dict/world/werndata/sandic32.dat",
53 "#9/dict/world/werndata/fieindex",
54 worldnextoff, worldprintentry, worldprintkey},
55 {"efi", "WSOY English->Finnish",
56 "#9/dict/world/werndata/sandic31.dat",
57 "#9/dict/world/werndata/efiindex",
58 worldnextoff, worldprintentry, worldprintkey},
60 {"fe", "Collins French->English",
61 "#9/dict/fe", "#9/dict/feindex",
62 pcollnextoff, pcollprintentry, pcollprintkey},
63 {"ef", "Collins English->French",
64 "#9/dict/ef", "#9/dict/efindex",
65 pcollnextoff, pcollprintentry, pcollprintkey},
67 {"ge", "Collins German->English",
68 "#9/dict/ge", "#9/dict/geindex",
69 pcollgnextoff, pcollgprintentry, pcollgprintkey},
70 {"eg", "Collins English->German",
71 "#9/dict/eg", "#9/dict/egindex",
72 pcollgnextoff, pcollgprintentry, pcollgprintkey},
74 {"ie", "Collins Italian->English",
75 "#9/dict/ie", "#9/dict/ieindex",
76 pcollnextoff, pcollprintentry, pcollprintkey},
77 {"ei", "Collins English->Italian",
78 "#9/dict/ei", "#9/dict/eiindex",
79 pcollnextoff, pcollprintentry, pcollprintkey},
81 {"je", "Sanshusha Japanese->English",
82 "#9/dict/world/sansdata/sandic18.dat",
83 "#9/dict/world/sansdata/jeindex",
84 worldnextoff, worldprintentry, worldprintkey},
85 {"jek", "Sanshusha Japanese->English (Kanji index)",
86 "#9/dict/world/sansdata/sandic18.dat",
87 "#9/dict/world/sansdata/jekindex",
88 worldnextoff, worldprintentry, worldprintkey},
89 {"ej", "Sanshusha English->Japanese",
90 "#9/dict/world/sansdata/sandic18.dat",
91 "#9/dict/world/sansdata/ejindex",
92 worldnextoff, worldprintentry, worldprintkey},
94 {"tjeg", "Sanshusha technical Japanese->English,German",
95 "#9/dict/world/sansdata/sandic16.dat",
96 "#9/dict/world/sansdata/tjegindex",
97 worldnextoff, worldprintentry, worldprintkey},
98 {"tjegk", "Sanshusha technical Japanese->English,German (Kanji index)",
99 "#9/dict/world/sansdata/sandic16.dat",
100 "#9/dict/world/sansdata/tjegkindex",
101 worldnextoff, worldprintentry, worldprintkey},
102 {"tegj", "Sanshusha technical English->German,Japanese",
103 "#9/dict/world/sansdata/sandic16.dat",
104 "#9/dict/world/sansdata/tegjindex",
105 worldnextoff, worldprintentry, worldprintkey},
106 {"tgje", "Sanshusha technical German->Japanese,English",
107 "#9/dict/world/sansdata/sandic16.dat",
108 "#9/dict/world/sansdata/tgjeindex",
109 worldnextoff, worldprintentry, worldprintkey},
111 {"ne", "Kunnskapforlaget Norwegian->English",
112 "#9/dict/world/kunndata/sandic28.dat",
113 "#9/dict/world/kunndata/neindex",
114 worldnextoff, worldprintentry, worldprintkey},
115 {"en", "Kunnskapforlaget English->Norwegian",
116 "#9/dict/world/kunndata/sandic27.dat",
117 "#9/dict/world/kunndata/enindex",
118 worldnextoff, worldprintentry, worldprintkey},
120 {"re", "Leon Ungier Russian->English",
121 "#9/dict/re", "#9/dict/reindex",
122 simplenextoff, simpleprintentry, simpleprintkey},
123 {"er", "Leon Ungier English->Russian",
124 "#9/dict/re", "#9/dict/erindex",
125 simplenextoff, simpleprintentry, simpleprintkey},
127 {"se", "Collins Spanish->English",
128 "#9/dict/se", "#9/dict/seindex",
129 pcollnextoff, pcollprintentry, pcollprintkey},
130 {"es", "Collins English->Spanish",
131 "#9/dict/es", "#9/dict/esindex",
132 pcollnextoff, pcollprintentry, pcollprintkey},
134 {"swe", "Esselte Studium Swedish->English",
135 "#9/dict/world/essedata/sandic34.dat",
136 "#9/dict/world/essedata/sweindex",
137 worldnextoff, worldprintentry, worldprintkey},
138 {"esw", "Esselte Studium English->Swedish",
139 "#9/dict/world/essedata/sandic33.dat",
140 "#9/dict/world/essedata/eswindex",
141 worldnextoff, worldprintentry, worldprintkey},
143 {"movie", "Movies -- by title",
144 "movie/data", "#9/dict/movtindex",
145 movienextoff, movieprintentry, movieprintkey},
146 {"moviea", "Movies -- by actor",
147 "movie/data", "#9/dict/movaindex",
148 movienextoff, movieprintentry, movieprintkey},
149 {"movied", "Movies -- by director",
150 "movie/data", "#9/dict/movdindex",
151 movienextoff, movieprintentry, movieprintkey},
153 {"slang", "English Slang",
154 "#9/dict/slang", "#9/dict/slangindex",
155 slangnextoff, slangprintentry, slangprintkey},
157 {"robert", "Robert Électronique",
158 "#9/dict/robert/_pointers", "#9/dict/robert/_index",
159 robertnextoff, robertindexentry, robertprintkey},
160 {"robertv", "Robert Électronique - formes des verbes",
161 "#9/dict/robert/flex.rob", "#9/dict/robert/_flexindex",
162 robertnextflex, robertflexentry, robertprintkey},
164 {0, 0, 0, 0, 0}
165 };
167 typedef struct Lig Lig;
168 struct Lig {
169 Rune start; /* accent rune */
170 Rune pairs[100]; /* <char,accented version> pairs */
171 };
173 /* keep in sync with dict.h */
174 static Lig ligtab[Nligs] = {
175 {0xb4, {0x41, 0xc1, 0x61, 0xe1, 0x43, 0x106, 0x63, 0x107, 0x45, 0xc9, 0x65, 0xe9, 0x67, 0x123, 0x49, 0xcd, 0x69, 0xed, 0x131, 0xed, 0x4c, 0x139, 0x6c, 0x13a, 0x4e, 0x143, 0x6e, 0x144, 0x4f, 0xd3, 0x6f, 0xf3, 0x52, 0x154, 0x72, 0x155, 0x53, 0x15a, 0x73, 0x15b, 0x55, 0xda, 0x75, 0xfa, 0x59, 0xdd, 0x79, 0xfd, 0x5a, 0x179, 0x7a, 0x17a, 0}},
176 {0x2cb, {0x41, 0xc0, 0x61, 0xe0, 0x45, 0xc8, 0x65, 0xe8, 0x49, 0xcc, 0x69, 0xec, 0x131, 0xec, 0x4f, 0xd2, 0x6f, 0xf2, 0x55, 0xd9, 0x75, 0xf9, 0}},
177 {0xa8, {0x41, 0xc4, 0x61, 0xe4, 0x45, 0xcb, 0x65, 0xeb, 0x49, 0xcf, 0x69, 0xef, 0x4f, 0xd6, 0x6f, 0xf6, 0x55, 0xdc, 0x75, 0xfc, 0x59, 0x178, 0x79, 0xff, 0}},
178 {0xb8, {0x43, 0xc7, 0x63, 0xe7, 0x47, 0x122, 0x4b, 0x136, 0x6b, 0x137, 0x4c, 0x13b, 0x6c, 0x13c, 0x4e, 0x145, 0x6e, 0x146, 0x52, 0x156, 0x72, 0x157, 0x53, 0x15e, 0x73, 0x15f, 0x54, 0x162, 0x74, 0x163, 0}},
179 {0x2dc, {0x41, 0xc3, 0x61, 0xe3, 0x49, 0x128, 0x69, 0x129, 0x131, 0x129, 0x4e, 0xd1, 0x6e, 0xf1, 0x4f, 0xd5, 0x6f, 0xf5, 0x55, 0x168, 0x75, 0x169, 0}},
180 {0x2d8, {0x41, 0x102, 0x61, 0x103, 0x45, 0x114, 0x65, 0x115, 0x47, 0x11e, 0x67, 0x11f, 0x49, 0x12c, 0x69, 0x12d, 0x131, 0x12d, 0x4f, 0x14e, 0x6f, 0x14f, 0x55, 0x16c, 0x75, 0x16d, 0}},
181 {0x2da, {0x41, 0xc5, 0x61, 0xe5, 0x55, 0x16e, 0x75, 0x16f, 0}},
182 {0x2d9, {0x43, 0x10a, 0x63, 0x10b, 0x45, 0x116, 0x65, 0x117, 0x47, 0x120, 0x67, 0x121, 0x49, 0x130, 0x4c, 0x13f, 0x6c, 0x140, 0x5a, 0x17b, 0x7a, 0x17c, 0}},
183 {0x2e, {0}},
184 {0x2322, {0x41, 0xc2, 0x61, 0xe2, 0x43, 0x108, 0x63, 0x109, 0x45, 0xca, 0x65, 0xea, 0x47, 0x11c, 0x67, 0x11d, 0x48, 0x124, 0x68, 0x125, 0x49, 0xce, 0x69, 0xee, 0x131, 0xee, 0x4a, 0x134, 0x6a, 0x135, 0x4f, 0xd4, 0x6f, 0xf4, 0x53, 0x15c, 0x73, 0x15d, 0x55, 0xdb, 0x75, 0xfb, 0x57, 0x174, 0x77, 0x175, 0x59, 0x176, 0x79, 0x177, 0}},
185 {0x32f, {0}},
186 {0x2db, {0x41, 0x104, 0x61, 0x105, 0x45, 0x118, 0x65, 0x119, 0x49, 0x12e, 0x69, 0x12f, 0x131, 0x12f, 0x55, 0x172, 0x75, 0x173, 0}},
187 {0xaf, {0x41, 0x100, 0x61, 0x101, 0x45, 0x112, 0x65, 0x113, 0x49, 0x12a, 0x69, 0x12b, 0x131, 0x12b, 0x4f, 0x14c, 0x6f, 0x14d, 0x55, 0x16a, 0x75, 0x16b, 0}},
188 {0x2c7, {0x43, 0x10c, 0x63, 0x10d, 0x44, 0x10e, 0x64, 0x10f, 0x45, 0x11a, 0x65, 0x11b, 0x4c, 0x13d, 0x6c, 0x13e, 0x4e, 0x147, 0x6e, 0x148, 0x52, 0x158, 0x72, 0x159, 0x53, 0x160, 0x73, 0x161, 0x54, 0x164, 0x74, 0x165, 0x5a, 0x17d, 0x7a, 0x17e, 0}},
189 {0x2bd, {0}},
190 {0x2bc, {0}},
191 {0x32e, {0}}
192 };
194 Rune multitab[Nmulti][5] = {
195 {0x2bd, 0x3b1, 0},
196 {0x2bc, 0x3b1, 0},
197 {0x61, 0x6e, 0x64, 0},
198 {0x61, 0x2f, 0x71, 0},
199 {0x3c, 0x7c, 0},
200 {0x2e, 0x2e, 0},
201 {0x2e, 0x2e, 0x2e, 0},
202 {0x2bd, 0x3b5, 0},
203 {0x2bc, 0x3b5, 0},
204 {0x2014, 0x2014, 0},
205 {0x2bd, 0x3b7, 0},
206 {0x2bc, 0x3b7, 0},
207 {0x2bd, 0x3b9, 0},
208 {0x2bc, 0x3b9, 0},
209 {0x63, 0x74, 0},
210 {0x66, 0x66, 0},
211 {0x66, 0x66, 0x69, 0},
212 {0x66, 0x66, 0x6c, 0},
213 {0x66, 0x6c, 0},
214 {0x66, 0x69, 0},
215 {0x26b, 0x26b, 0},
216 {0x73, 0x74, 0},
217 {0x2bd, 0x3bf, 0},
218 {0x2bc, 0x3bf, 0},
219 {0x6f, 0x72, 0},
220 {0x2bd, 0x3c1, 0},
221 {0x2bc, 0x3c1, 0},
222 {0x7e, 0x7e, 0},
223 {0x2bd, 0x3c5, 0},
224 {0x2bc, 0x3c5, 0},
225 {0x2bd, 0x3c9, 0},
226 {0x2bc, 0x3c9, 0},
227 {0x6f, 0x65, 0},
228 {0x20, 0x20, 0},
229 };
231 #define risupper(r) (0x41 <= (r) && (r) <= 0x5a)
232 #define rislatin1(r) (0xC0 <= (r) && (r) <= 0xFF)
233 #define rtolower(r) ((r)-'A'+'a')
235 static Rune latin_fold_tab[] =
237 /* Table to fold latin 1 characters to ASCII equivalents
238 based at Rune value 0xc0
240 À Á Â Ã Ä Å Æ Ç
241 È É Ê Ë Ì Í Î Ï
242 Ð Ñ Ò Ó Ô Õ Ö ×
243 Ø Ù Ú Û Ü Ý Þ ß
244 à á â ã ä å æ ç
245 è é ê ë ì í î ï
246 ð ñ ò ó ô õ ö ÷
247 ø ù ú û ü ý þ ÿ
248 */
249 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'c',
250 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i',
251 'd', 'n', 'o', 'o', 'o', 'o', 'o', 0 ,
252 'o', 'u', 'u', 'u', 'u', 'y', 0 , 0 ,
253 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'c',
254 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i',
255 'd', 'n', 'o', 'o', 'o', 'o', 'o', 0 ,
256 'o', 'u', 'u', 'u', 'u', 'y', 0 , 'y',
257 };
259 static Rune *ttabstack[20];
260 static int ntt;
262 /*
263 * tab is an array of n Assoc's, sorted by key.
264 * Look for key in tab, and return corresponding val
265 * or -1 if not there
266 */
267 long
268 lookassoc(Assoc *tab, int n, char *key)
270 Assoc *q;
271 long i, low, high;
272 int r;
274 for(low = -1, high = n; high > low+1; ){
275 i = (high+low)/2;
276 q = &tab[i];
277 if((r=strcmp(key, q->key))<0)
278 high = i;
279 else if(r == 0)
280 return q->val;
281 else
282 low=i;
284 return -1;
287 long
288 looknassoc(Nassoc *tab, int n, long key)
290 Nassoc *q;
291 long i, low, high;
293 for(low = -1, high = n; high > low+1; ){
294 i = (high+low)/2;
295 q = &tab[i];
296 if(key < q->key)
297 high = i;
298 else if(key == q->key)
299 return q->val;
300 else
301 low=i;
303 return -1;
306 void
307 err(char *fmt, ...)
309 char buf[1000];
310 va_list v;
312 va_start(v, fmt);
313 vsnprint(buf, sizeof(buf), fmt, v);
314 va_end(v);
315 fprint(2, "%s: %s\n", argv0, buf);
318 /*
319 * Write the rune r to bout, keeping track of line length
320 * and breaking the lines (at blanks) when they get too long
321 */
322 void
323 outrune(long r)
325 if(outinhibit)
326 return;
327 if(++linelen > breaklen && r == 0x20) {
328 Bputc(bout, '\n');
329 linelen = 0;
330 } else
331 Bputrune(bout, r);
334 void
335 outrunes(Rune *rp)
337 Rune r;
339 while((r = *rp++) != 0)
340 outrune(r);
343 /* like outrune, but when arg is know to be a char */
344 void
345 outchar(int c)
347 if(outinhibit)
348 return;
349 if(++linelen > breaklen && c == ' ') {
350 c ='\n';
351 linelen = 0;
353 Bputc(bout, c);
356 void
357 outchars(char *s)
359 char c;
361 while((c = *s++) != 0)
362 outchar(c);
365 void
366 outprint(char *fmt, ...)
368 char buf[1000];
369 va_list v;
371 va_start(v, fmt);
372 vsnprint(buf, sizeof(buf), fmt, v);
373 va_end(v);
374 outchars(buf);
377 void
378 outpiece(char *b, char *e)
380 int c, lastc;
382 lastc = 0;
383 while(b < e) {
384 c = *b++;
385 if(c == '\n')
386 c = ' ';
387 if(!(c == ' ' && lastc == ' '))
388 outchar(c);
389 lastc = c;
393 /*
394 * Go to new line if not already there; indent if ind != 0.
395 * If ind > 1, leave a blank line too.
396 * Slight hack: assume if current line is only one or two
397 * characters long, then they were spaces.
398 */
399 void
400 outnl(int ind)
402 if(outinhibit)
403 return;
404 if(ind) {
405 if(ind > 1) {
406 if(linelen > 2)
407 Bputc(bout, '\n');
408 Bprint(bout, "\n ");
409 } else if(linelen == 0)
410 Bprint(bout, " ");
411 else if(linelen == 1)
412 Bputc(bout, ' ');
413 else if(linelen != 2)
414 Bprint(bout, "\n ");
415 linelen = 2;
416 } else {
417 if(linelen) {
418 Bputc(bout, '\n');
419 linelen = 0;
424 /*
425 * Fold the runes in null-terminated rp.
426 * Use the sort(1) definition of folding (uppercase to lowercase,
427 * latin1-accented characters to corresponding unaccented chars)
428 */
429 void
430 fold(Rune *rp)
432 Rune r;
434 while((r = *rp) != 0) {
435 if (rislatin1(r) && latin_fold_tab[r-0xc0])
436 r = latin_fold_tab[r-0xc0];
437 if(risupper(r))
438 r = rtolower(r);
439 *rp++ = r;
443 /*
444 * Like fold, but put folded result into new
445 * (assumed to have enough space).
446 * old is a regular expression, but we know that
447 * metacharacters aren't affected
448 */
449 void
450 foldre(char *new, char *old)
452 Rune r;
454 while(*old) {
455 old += chartorune(&r, old);
456 if (rislatin1(r) && latin_fold_tab[r-0xc0])
457 r = latin_fold_tab[r-0xc0];
458 if(risupper(r))
459 r = rtolower(r);
460 new += runetochar(new, &r);
462 *new = 0;
465 /*
466 * acomp(s, t) returns:
467 * -2 if s strictly precedes t
468 * -1 if s is a prefix of t
469 * 0 if s is the same as t
470 * 1 if t is a prefix of s
471 * 2 if t strictly precedes s
472 */
474 int
475 acomp(Rune *s, Rune *t)
477 int cs, ct;
479 for(;;) {
480 cs = *s;
481 ct = *t;
482 if(cs != ct)
483 break;
484 if(cs == 0)
485 return 0;
486 s++;
487 t++;
489 if(cs == 0)
490 return -1;
491 if(ct == 0)
492 return 1;
493 if(cs < ct)
494 return -2;
495 return 2;
498 /*
499 * Copy null terminated Runes from 'from' to 'to'.
500 */
501 void
502 runescpy(Rune *to, Rune *from)
504 while((*to++ = *from++) != 0)
505 continue;
508 /*
509 * Conversion of unsigned number to long, no overflow detection
510 */
511 long
512 runetol(Rune *r)
514 int c;
515 long n;
517 n = 0;
518 for(;; r++){
519 c = *r;
520 if(0x30<=c && c<=0x39)
521 c -= '0';
522 else
523 break;
524 n = n*10 + c;
526 return n;
529 /*
530 * See if there is a rune corresponding to the accented
531 * version of r with accent acc (acc in [LIGS..LIGE-1]),
532 * and return it if so, else return NONE.
533 */
534 Rune
535 liglookup(Rune acc, Rune r)
537 Rune *p;
539 if(acc < LIGS || acc >= LIGE)
540 return NONE;
541 for(p = ligtab[acc-LIGS].pairs; *p; p += 2)
542 if(*p == r)
543 return *(p+1);
544 return NONE;
547 /*
548 * Maintain a translation table stack (a translation table
549 * is an array of Runes indexed by bytes or 7-bit bytes).
550 * If starting is true, push the curtab onto the stack
551 * and return newtab; else pop the top of the stack and
552 * return it.
553 * If curtab is 0, initialize the stack and return.
554 */
555 Rune *
556 changett(Rune *curtab, Rune *newtab, int starting)
558 if(curtab == 0) {
559 ntt = 0;
560 return 0;
562 if(starting) {
563 if(ntt >= asize(ttabstack)) {
564 if(debug)
565 err("translation stack overflow");
566 return curtab;
568 ttabstack[ntt++] = curtab;
569 return newtab;
570 } else {
571 if(ntt == 0) {
572 if(debug)
573 err("translation stack underflow");
574 return curtab;
576 return ttabstack[--ntt];