Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include "dict.h"
6 /*
7 * Robert Électronique.
8 */
10 enum
11 {
12 CIT = MULTIE+1, /* citation ptr followed by long int and ascii label */
13 BROM, /* bold roman */
14 ITON, /* start italic */
15 ROM, /* roman */
16 SYM, /* symbol font? */
17 HEL, /* helvetica */
18 BHEL, /* helvetica bold */
19 SMALL, /* smaller? */
20 ITOFF, /* end italic */
21 SUP, /* following character is superscript */
22 SUB /* following character is subscript */
23 };
25 static Rune intab[256] = {
26 /*0*/ /*1*/ /*2*/ /*3*/ /*4*/ /*5*/ /*6*/ /*7*/
27 /*00*/ NONE, 0x263a, 0x263b, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022,
28 0x25d8, 0x298, L'\n', 0x2642, 0x2640, 0x266a, 0x266b, 0x203b,
29 /*10*/ 0x21e8, 0x21e6, 0x2195, 0x203c, 0xb6, 0xa7, 0x2043, 0x21a8,
30 0x2191, 0x2193, 0x2192, 0x2190, 0x2319, 0x2194, 0x25b4, 0x25be,
31 /*20*/ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, L'\'',
32 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
33 /*30*/ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
34 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
35 /*40*/ 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
36 0x48, 0x49, 0x4a, 0x4b, L'L', 0x4d, 0x4e, 0x4f,
37 /*50*/ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
38 0x58, 0x59, 0x5a, 0x5b, L'\\', 0x5d, 0x5e, 0x5f,
39 /*60*/ 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
40 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
41 /*70*/ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
42 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
43 /*80*/ 0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7,
44 0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5,
45 /*90*/ 0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9,
46 0xff, 0xd6, 0xdc, 0xa2, 0xa3, 0xa5, 0x20a7, 0x283,
47 /*a0*/ 0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba,
48 0xbf, 0x2310, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb,
49 /*b0*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
50 NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
51 /*c0*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
52 CIT, BROM, NONE, ITON, ROM, SYM, HEL, BHEL,
53 /*d0*/ NONE, SMALL, ITOFF, SUP, SUB, NONE, NONE, NONE,
54 NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
55 /*e0*/ 0x3b1, 0xdf, 0x3b3, 0x3c0, 0x3a3, 0x3c3, 0xb5, 0x3c4,
56 0x3a6, 0x398, 0x3a9, 0x3b4, 0x221e, 0xd8, 0x3b5, 0x2229,
57 /*f0*/ 0x2261, 0xb1, 0x2265, 0x2264, 0x2320, 0x2321, 0xf7, 0x2248,
58 0xb0, 0x2219, 0xb7, 0x221a, 0x207f, 0xb2, 0x220e, 0xa0,
59 };
61 static Rune suptab[] = {
62 ['0'] 0x2070, ['1'] 0x2071, ['2'] 0x2072, ['3'] 0x2073,
63 ['4'] 0x2074, ['5'] 0x2075, ['6'] 0x2076, ['7'] 0x2077,
64 ['8'] 0x2078, ['9'] 0x2079, ['+'] 0x207a, ['-'] 0x207b,
65 ['='] 0x207c, ['('] 0x207d, [')'] 0x207e, ['a'] 0xaa,
66 ['n'] 0x207f, ['o'] 0xba
67 };
69 static Rune subtab[] = {
70 ['0'] 0x2080, ['1'] 0x2081, ['2'] 0x2082, ['3'] 0x2083,
71 ['4'] 0x2084, ['5'] 0x2085, ['6'] 0x2086, ['7'] 0x2087,
72 ['8'] 0x2088, ['9'] 0x2089, ['+'] 0x208a, ['-'] 0x208b,
73 ['='] 0x208c, ['('] 0x208d, [')'] 0x208e
74 };
76 #define GSHORT(p) (((p)[0]<<8) | (p)[1])
77 #define GLONG(p) (((p)[0]<<24) | ((p)[1]<<16) | ((p)[2]<<8) | (p)[3])
79 static char cfile[] = "/lib/dict/robert/cits.rob";
80 static char dfile[] = "/lib/dict/robert/defs.rob";
81 static char efile[] = "/lib/dict/robert/etym.rob";
82 static char kfile[] = "/lib/dict/robert/_phon";
84 static Biobuf * cb;
85 static Biobuf * db;
86 static Biobuf * eb;
88 static Biobuf * Bouvrir(char*);
89 static void citation(int, int);
90 static void robertprintentry(Entry*, Entry*, int);
92 void
93 robertindexentry(Entry e, int cmd)
94 {
95 uchar *p = (uchar *)e.start;
96 long ea, el, da, dl, fa;
97 Entry def, etym;
99 ea = GLONG(&p[0]);
100 el = GSHORT(&p[4]);
101 da = GLONG(&p[6]);
102 dl = GSHORT(&p[10]);
103 fa = GLONG(&p[12]);
104 USED(fa);
106 if(db == 0)
107 db = Bouvrir(dfile);
108 def.start = malloc(dl+1);
109 def.end = def.start + dl;
110 def.doff = da;
111 Bseek(db, da, 0);
112 Bread(db, def.start, dl);
113 *def.end = 0;
114 if(cmd == 'h'){
115 robertprintentry(&def, 0, cmd);
116 }else{
117 if(eb == 0)
118 eb = Bouvrir(efile);
119 etym.start = malloc(el+1);
120 etym.end = etym.start + el;
121 etym.doff = ea;
122 Bseek(eb, ea, 0);
123 Bread(eb, etym.start, el);
124 *etym.end = 0;
125 robertprintentry(&def, &etym, cmd);
126 free(etym.start);
128 free(def.start);
131 static void
132 robertprintentry(Entry *def, Entry *etym, int cmd)
134 uchar *p, *pe;
135 Rune r; int c, n;
136 int baseline = 0;
137 int lineno = 0;
138 int cit = 0;
140 p = (uchar *)def->start;
141 pe = (uchar *)def->end;
142 while(p < pe){
143 if(cmd == 'r'){
144 outchar(*p++);
145 continue;
147 c = *p++;
148 switch(r = intab[c]){ /* assign = */
149 case BROM:
150 case ITON:
151 case ROM:
152 case SYM:
153 case HEL:
154 case BHEL:
155 case SMALL:
156 case ITOFF:
157 case NONE:
158 if(debug)
159 outprint("\\%.2ux", c);
160 baseline = 0;
161 break;
163 case SUP:
164 baseline = 1;
165 break;
167 case SUB:
168 baseline = -1;
169 break;
171 case CIT:
172 n = p[0] | (p[1]<<8) | (p[2]<<16) | (p[3]<<24);
173 p += 4;
174 if(debug)
175 outprint("[%d]", n);
176 while(*p == ' ' || ('0'<=*p && *p<='9') || *p == '.'){
177 if(debug)
178 outchar(*p);
179 ++p;
181 ++cit;
182 outnl(2);
183 citation(n, cmd);
184 baseline = 0;
185 break;
187 case '\n':
188 outnl(0);
189 baseline = 0;
190 ++lineno;
191 break;
193 default:
194 if(baseline > 0 && r < nelem(suptab))
195 r = suptab[r];
196 else if(baseline < 0 && r < nelem(subtab))
197 r = subtab[r];
198 if(cit){
199 outchar('\n');
200 cit = 0;
202 outrune(r);
203 baseline = 0;
204 break;
206 if(r == '\n'){
207 if(cmd == 'h')
208 break;
209 if(lineno == 1 && etym)
210 robertprintentry(etym, 0, cmd);
213 outnl(0);
216 static void
217 citation(int addr, int cmd)
219 Entry cit;
221 if(cb == 0)
222 cb = Bouvrir(cfile);
223 Bseek(cb, addr, 0);
224 cit.start = Brdline(cb, 0xc8);
225 cit.end = cit.start + Blinelen(cb) - 1;
226 cit.doff = addr;
227 *cit.end = 0;
228 robertprintentry(&cit, 0, cmd);
231 long
232 robertnextoff(long fromoff)
234 return (fromoff & ~15) + 16;
237 void
238 robertprintkey(void)
240 Biobuf *db;
241 char *l;
243 db = Bouvrir(kfile);
244 while(l = Brdline(db, '\n')) /* assign = */
245 Bwrite(bout, l, Blinelen(db));
246 Bterm(db);
249 void
250 robertflexentry(Entry e, int cmd)
252 uchar *p, *pe;
253 Rune r; int c;
254 int lineno = 1;
256 p = (uchar *)e.start;
257 pe = (uchar *)e.end;
258 while(p < pe){
259 if(cmd == 'r'){
260 Bputc(bout, *p++);
261 continue;
263 c = *p++;
264 r = intab[c];
265 if(r == '$')
266 r = '\n';
267 if(r == '\n'){
268 ++lineno;
269 if(cmd == 'h' && lineno > 2)
270 break;
272 if(cmd == 'h' && lineno < 2)
273 continue;
274 if(r > MULTIE){
275 if(debug)
276 Bprint(bout, "\\%.2ux", c);
277 continue;
279 if(r < Runeself)
280 Bputc(bout, r);
281 else
282 Bputrune(bout, r);
284 outnl(0);
287 long
288 robertnextflex(long fromoff)
290 int c;
292 if(Bseek(bdict, fromoff, 0) < 0)
293 return -1;
294 while((c = Bgetc(bdict)) >= 0){
295 if(c == '$')
296 return Boffset(bdict);
298 return -1;
301 static Biobuf *
302 Bouvrir(char *fichier)
304 Biobuf *db;
306 db = Bopen(fichier, OREAD);
307 if(db == 0){
308 fprint(2, "%s: impossible d'ouvrir %s: %r\n", argv0, fichier);
309 exits("ouvrir");
311 return db;