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 static Lig ligtab[Nligs] = {
174 [LACU-LIGS] {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}},
175 [LGRV-LIGS] {0x2cb, {0x41, 0xc0, 0x61, 0xe0, 0x45, 0xc8, 0x65, 0xe8, 0x49, 0xcc, 0x69, 0xec, 0x131, 0xec, 0x4f, 0xd2, 0x6f, 0xf2, 0x55, 0xd9, 0x75, 0xf9, 0}},
176 [LUML-LIGS] {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}},
177 [LCED-LIGS] {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}},
178 [LTIL-LIGS] {0x2dc, {0x41, 0xc3, 0x61, 0xe3, 0x49, 0x128, 0x69, 0x129, 0x131, 0x129, 0x4e, 0xd1, 0x6e, 0xf1, 0x4f, 0xd5, 0x6f, 0xf5, 0x55, 0x168, 0x75, 0x169, 0}},
179 [LBRV-LIGS] {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}},
180 [LRNG-LIGS] {0x2da, {0x41, 0xc5, 0x61, 0xe5, 0x55, 0x16e, 0x75, 0x16f, 0}},
181 [LDOT-LIGS] {0x2d9, {0x43, 0x10a, 0x63, 0x10b, 0x45, 0x116, 0x65, 0x117, 0x47, 0x120, 0x67, 0x121, 0x49, 0x130, 0x4c, 0x13f, 0x6c, 0x140, 0x5a, 0x17b, 0x7a, 0x17c, 0}},
182 [LDTB-LIGS] {0x2e, {0}},
183 [LFRN-LIGS] {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}},
184 [LFRB-LIGS] {0x32f, {0}},
185 [LOGO-LIGS] {0x2db, {0x41, 0x104, 0x61, 0x105, 0x45, 0x118, 0x65, 0x119, 0x49, 0x12e, 0x69, 0x12f, 0x131, 0x12f, 0x55, 0x172, 0x75, 0x173, 0}},
186 [LMAC-LIGS] {0xaf, {0x41, 0x100, 0x61, 0x101, 0x45, 0x112, 0x65, 0x113, 0x49, 0x12a, 0x69, 0x12b, 0x131, 0x12b, 0x4f, 0x14c, 0x6f, 0x14d, 0x55, 0x16a, 0x75, 0x16b, 0}},
187 [LHCK-LIGS] {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}},
188 [LASP-LIGS] {0x2bd, {0}},
189 [LLEN-LIGS] {0x2bc, {0}},
190 [LBRB-LIGS] {0x32e, {0}}
191 };
193 Rune multitab[Nmulti][5] = {
194 [MAAS-MULTI] {0x2bd, 0x3b1, 0},
195 [MALN-MULTI] {0x2bc, 0x3b1, 0},
196 [MAND-MULTI] {0x61, 0x6e, 0x64, 0},
197 [MAOQ-MULTI] {0x61, 0x2f, 0x71, 0},
198 [MBRA-MULTI] {0x3c, 0x7c, 0},
199 [MDD-MULTI] {0x2e, 0x2e, 0},
200 [MDDD-MULTI] {0x2e, 0x2e, 0x2e, 0},
201 [MEAS-MULTI] {0x2bd, 0x3b5, 0},
202 [MELN-MULTI] {0x2bc, 0x3b5, 0},
203 [MEMM-MULTI] {0x2014, 0x2014, 0},
204 [MHAS-MULTI] {0x2bd, 0x3b7, 0},
205 [MHLN-MULTI] {0x2bc, 0x3b7, 0},
206 [MIAS-MULTI] {0x2bd, 0x3b9, 0},
207 [MILN-MULTI] {0x2bc, 0x3b9, 0},
208 [MLCT-MULTI] {0x63, 0x74, 0},
209 [MLFF-MULTI] {0x66, 0x66, 0},
210 [MLFFI-MULTI] {0x66, 0x66, 0x69, 0},
211 [MLFFL-MULTI] {0x66, 0x66, 0x6c, 0},
212 [MLFL-MULTI] {0x66, 0x6c, 0},
213 [MLFI-MULTI] {0x66, 0x69, 0},
214 [MLLS-MULTI] {0x26b, 0x26b, 0},
215 [MLST-MULTI] {0x73, 0x74, 0},
216 [MOAS-MULTI] {0x2bd, 0x3bf, 0},
217 [MOLN-MULTI] {0x2bc, 0x3bf, 0},
218 [MOR-MULTI] {0x6f, 0x72, 0},
219 [MRAS-MULTI] {0x2bd, 0x3c1, 0},
220 [MRLN-MULTI] {0x2bc, 0x3c1, 0},
221 [MTT-MULTI] {0x7e, 0x7e, 0},
222 [MUAS-MULTI] {0x2bd, 0x3c5, 0},
223 [MULN-MULTI] {0x2bc, 0x3c5, 0},
224 [MWAS-MULTI] {0x2bd, 0x3c9, 0},
225 [MWLN-MULTI] {0x2bc, 0x3c9, 0},
226 [MOE-MULTI] {0x6f, 0x65, 0},
227 [MES-MULTI] {0x20, 0x20, 0},
228 };
230 #define risupper(r) (0x41 <= (r) && (r) <= 0x5a)
231 #define rislatin1(r) (0xC0 <= (r) && (r) <= 0xFF)
232 #define rtolower(r) ((r)-'A'+'a')
234 static Rune latin_fold_tab[] =
236 /* Table to fold latin 1 characters to ASCII equivalents
237 based at Rune value 0xc0
239 À Á Â Ã Ä Å Æ Ç
240 È É Ê Ë Ì Í Î Ï
241 Ð Ñ Ò Ó Ô Õ Ö ×
242 Ø Ù Ú Û Ü Ý Þ ß
243 à á â ã ä å æ ç
244 è é ê ë ì í î ï
245 ð ñ ò ó ô õ ö ÷
246 ø ù ú û ü ý þ ÿ
247 */
248 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'c',
249 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i',
250 'd', 'n', 'o', 'o', 'o', 'o', 'o', 0 ,
251 'o', 'u', 'u', 'u', 'u', 'y', 0 , 0 ,
252 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'c',
253 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i',
254 'd', 'n', 'o', 'o', 'o', 'o', 'o', 0 ,
255 'o', 'u', 'u', 'u', 'u', 'y', 0 , 'y',
256 };
258 static Rune *ttabstack[20];
259 static int ntt;
261 /*
262 * tab is an array of n Assoc's, sorted by key.
263 * Look for key in tab, and return corresponding val
264 * or -1 if not there
265 */
266 long
267 lookassoc(Assoc *tab, int n, char *key)
269 Assoc *q;
270 long i, low, high;
271 int r;
273 for(low = -1, high = n; high > low+1; ){
274 i = (high+low)/2;
275 q = &tab[i];
276 if((r=strcmp(key, q->key))<0)
277 high = i;
278 else if(r == 0)
279 return q->val;
280 else
281 low=i;
283 return -1;
286 long
287 looknassoc(Nassoc *tab, int n, long key)
289 Nassoc *q;
290 long i, low, high;
292 for(low = -1, high = n; high > low+1; ){
293 i = (high+low)/2;
294 q = &tab[i];
295 if(key < q->key)
296 high = i;
297 else if(key == q->key)
298 return q->val;
299 else
300 low=i;
302 return -1;
305 void
306 err(char *fmt, ...)
308 char buf[1000];
309 va_list v;
311 va_start(v, fmt);
312 vsnprint(buf, sizeof(buf), fmt, v);
313 va_end(v);
314 fprint(2, "%s: %s\n", argv0, buf);
317 /*
318 * Write the rune r to bout, keeping track of line length
319 * and breaking the lines (at blanks) when they get too long
320 */
321 void
322 outrune(long r)
324 if(outinhibit)
325 return;
326 if(++linelen > breaklen && r == 0x20) {
327 Bputc(bout, '\n');
328 linelen = 0;
329 } else
330 Bputrune(bout, r);
333 void
334 outrunes(Rune *rp)
336 Rune r;
338 while((r = *rp++) != 0)
339 outrune(r);
342 /* like outrune, but when arg is know to be a char */
343 void
344 outchar(int c)
346 if(outinhibit)
347 return;
348 if(++linelen > breaklen && c == ' ') {
349 c ='\n';
350 linelen = 0;
352 Bputc(bout, c);
355 void
356 outchars(char *s)
358 char c;
360 while((c = *s++) != 0)
361 outchar(c);
364 void
365 outprint(char *fmt, ...)
367 char buf[1000];
368 va_list v;
370 va_start(v, fmt);
371 vsnprint(buf, sizeof(buf), fmt, v);
372 va_end(v);
373 outchars(buf);
376 void
377 outpiece(char *b, char *e)
379 int c, lastc;
381 lastc = 0;
382 while(b < e) {
383 c = *b++;
384 if(c == '\n')
385 c = ' ';
386 if(!(c == ' ' && lastc == ' '))
387 outchar(c);
388 lastc = c;
392 /*
393 * Go to new line if not already there; indent if ind != 0.
394 * If ind > 1, leave a blank line too.
395 * Slight hack: assume if current line is only one or two
396 * characters long, then they were spaces.
397 */
398 void
399 outnl(int ind)
401 if(outinhibit)
402 return;
403 if(ind) {
404 if(ind > 1) {
405 if(linelen > 2)
406 Bputc(bout, '\n');
407 Bprint(bout, "\n ");
408 } else if(linelen == 0)
409 Bprint(bout, " ");
410 else if(linelen == 1)
411 Bputc(bout, ' ');
412 else if(linelen != 2)
413 Bprint(bout, "\n ");
414 linelen = 2;
415 } else {
416 if(linelen) {
417 Bputc(bout, '\n');
418 linelen = 0;
423 /*
424 * Fold the runes in null-terminated rp.
425 * Use the sort(1) definition of folding (uppercase to lowercase,
426 * latin1-accented characters to corresponding unaccented chars)
427 */
428 void
429 fold(Rune *rp)
431 Rune r;
433 while((r = *rp) != 0) {
434 if (rislatin1(r) && latin_fold_tab[r-0xc0])
435 r = latin_fold_tab[r-0xc0];
436 if(risupper(r))
437 r = rtolower(r);
438 *rp++ = r;
442 /*
443 * Like fold, but put folded result into new
444 * (assumed to have enough space).
445 * old is a regular expression, but we know that
446 * metacharacters aren't affected
447 */
448 void
449 foldre(char *new, char *old)
451 Rune r;
453 while(*old) {
454 old += chartorune(&r, old);
455 if (rislatin1(r) && latin_fold_tab[r-0xc0])
456 r = latin_fold_tab[r-0xc0];
457 if(risupper(r))
458 r = rtolower(r);
459 new += runetochar(new, &r);
461 *new = 0;
464 /*
465 * acomp(s, t) returns:
466 * -2 if s strictly precedes t
467 * -1 if s is a prefix of t
468 * 0 if s is the same as t
469 * 1 if t is a prefix of s
470 * 2 if t strictly precedes s
471 */
473 int
474 acomp(Rune *s, Rune *t)
476 int cs, ct;
478 for(;;) {
479 cs = *s;
480 ct = *t;
481 if(cs != ct)
482 break;
483 if(cs == 0)
484 return 0;
485 s++;
486 t++;
488 if(cs == 0)
489 return -1;
490 if(ct == 0)
491 return 1;
492 if(cs < ct)
493 return -2;
494 return 2;
497 /*
498 * Copy null terminated Runes from 'from' to 'to'.
499 */
500 void
501 runescpy(Rune *to, Rune *from)
503 while((*to++ = *from++) != 0)
504 continue;
507 /*
508 * Conversion of unsigned number to long, no overflow detection
509 */
510 long
511 runetol(Rune *r)
513 int c;
514 long n;
516 n = 0;
517 for(;; r++){
518 c = *r;
519 if(0x30<=c && c<=0x39)
520 c -= '0';
521 else
522 break;
523 n = n*10 + c;
525 return n;
528 /*
529 * See if there is a rune corresponding to the accented
530 * version of r with accent acc (acc in [LIGS..LIGE-1]),
531 * and return it if so, else return NONE.
532 */
533 Rune
534 liglookup(Rune acc, Rune r)
536 Rune *p;
538 if(acc < LIGS || acc >= LIGE)
539 return NONE;
540 for(p = ligtab[acc-LIGS].pairs; *p; p += 2)
541 if(*p == r)
542 return *(p+1);
543 return NONE;
546 /*
547 * Maintain a translation table stack (a translation table
548 * is an array of Runes indexed by bytes or 7-bit bytes).
549 * If starting is true, push the curtab onto the stack
550 * and return newtab; else pop the top of the stack and
551 * return it.
552 * If curtab is 0, initialize the stack and return.
553 */
554 Rune *
555 changett(Rune *curtab, Rune *newtab, int starting)
557 if(curtab == 0) {
558 ntt = 0;
559 return 0;
561 if(starting) {
562 if(ntt >= asize(ttabstack)) {
563 if(debug)
564 err("translation stack overflow");
565 return curtab;
567 ttabstack[ntt++] = curtab;
568 return newtab;
569 } else {
570 if(ntt == 0) {
571 if(debug)
572 err("translation stack underflow");
573 return curtab;
575 return ttabstack[--ntt];