robert.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include "dict.h"
  5. /*
  6. * Robert Électronique.
  7. */
  8. enum
  9. {
  10. CIT = MULTIE+1, /* citation ptr followed by long int and ascii label */
  11. BROM, /* bold roman */
  12. ITON, /* start italic */
  13. ROM, /* roman */
  14. SYM, /* symbol font? */
  15. HEL, /* helvetica */
  16. BHEL, /* helvetica bold */
  17. SMALL, /* smaller? */
  18. ITOFF, /* end italic */
  19. SUP, /* following character is superscript */
  20. SUB /* following character is subscript */
  21. };
  22. static Rune intab[256] = {
  23. /*0*/ /*1*/ /*2*/ /*3*/ /*4*/ /*5*/ /*6*/ /*7*/
  24. /*00*/ NONE, L'☺', L'☻', L'♥', L'♦', L'♣', L'♠', L'•',
  25. 0x25d8, L'ʘ', L'\n', L'♂', L'♀', L'♪', 0x266b, L'※',
  26. /*10*/ L'⇨', L'⇦', L'↕', L'‼', L'¶', L'§', L'⁃', L'↨',
  27. L'↑', L'↓', L'→', L'←', L'⌙', L'↔', 0x25b4, 0x25be,
  28. /*20*/ L' ', L'!', L'"', L'#', L'$', L'%', L'&', L''',
  29. L'(', L')', L'*', L'+', L',', L'-', L'.', L'/',
  30. /*30*/ L'0', L'1', L'2', L'3', L'4', L'5', L'6', L'7',
  31. L'8', L'9', L':', L';', L'<', L'=', L'>', L'?',
  32. /*40*/ L'@', L'A', L'B', L'C', L'D', L'E', L'F', L'G',
  33. L'H', L'I', L'J', L'K', L'L', L'M', L'N', L'O',
  34. /*50*/ L'P', L'Q', L'R', L'S', L'T', L'U', L'V', L'W',
  35. L'X', L'Y', L'Z', L'[', L'\\', L']', L'^', L'_',
  36. /*60*/ L'`', L'a', L'b', L'c', L'd', L'e', L'f', L'g',
  37. L'h', L'i', L'j', L'k', L'l', L'm', L'n', L'o',
  38. /*70*/ L'p', L'q', L'r', L's', L't', L'u', L'v', L'w',
  39. L'x', L'y', L'z', L'{', L'|', L'}', L'~', L'',
  40. /*80*/ L'Ç', L'ü', L'é', L'â', L'ä', L'à', L'å', L'ç',
  41. L'ê', L'ë', L'è', L'ï', L'î', L'ì', L'Ä', L'Å',
  42. /*90*/ L'É', L'æ', L'Æ', L'ô', L'ö', L'ò', L'û', L'ù',
  43. L'ÿ', L'Ö', L'Ü', L'¢', L'£', L'¥', L'₧', L'ʃ',
  44. /*a0*/ L'á', L'í', L'ó', L'ú', L'ñ', L'Ñ', L'ª', L'º',
  45. L'¿', L'⌐', L'¬', L'½', L'¼', L'¡', L'«', L'»',
  46. /*b0*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
  47. NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
  48. /*c0*/ NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
  49. CIT, BROM, NONE, ITON, ROM, SYM, HEL, BHEL,
  50. /*d0*/ NONE, SMALL, ITOFF, SUP, SUB, NONE, NONE, NONE,
  51. NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
  52. /*e0*/ L'α', L'ß', L'γ', L'π', L'Σ', L'σ', L'µ', L'τ',
  53. L'Φ', L'Θ', L'Ω', L'δ', L'∞', L'Ø', L'ε', L'∩',
  54. /*f0*/ L'≡', L'±', L'≥', L'≤', L'⌠', L'⌡', L'÷', L'≈',
  55. L'°', L'∙', L'·', L'√', L'ⁿ', L'²', L'∎', L' ',
  56. };
  57. static Rune suptab[] = {
  58. ['0'] L'⁰', ['1'] L'ⁱ', ['2'] L'⁲', ['3'] L'⁳',
  59. ['4'] L'⁴', ['5'] L'⁵', ['6'] L'⁶', ['7'] L'⁷',
  60. ['8'] L'⁸', ['9'] L'⁹', ['+'] L'⁺', ['-'] L'⁻',
  61. ['='] L'⁼', ['('] L'⁽', [')'] L'⁾', ['a'] L'ª',
  62. ['n'] L'ⁿ', ['o'] L'º'
  63. };
  64. static Rune subtab[] = {
  65. ['0'] L'₀', ['1'] L'₁', ['2'] L'₂', ['3'] L'₃',
  66. ['4'] L'₄', ['5'] L'₅', ['6'] L'₆', ['7'] L'₇',
  67. ['8'] L'₈', ['9'] L'₉', ['+'] L'₊', ['-'] L'₋',
  68. ['='] L'₌', ['('] L'₍', [')'] L'₎'
  69. };
  70. #define GSHORT(p) (((p)[0]<<8) | (p)[1])
  71. #define GLONG(p) (((p)[0]<<24) | ((p)[1]<<16) | ((p)[2]<<8) | (p)[3])
  72. static char cfile[] = "/lib/dict/robert/cits.rob";
  73. static char dfile[] = "/lib/dict/robert/defs.rob";
  74. static char efile[] = "/lib/dict/robert/etym.rob";
  75. static char kfile[] = "/lib/dict/robert/_phon";
  76. static Biobuf * cb;
  77. static Biobuf * db;
  78. static Biobuf * eb;
  79. static Biobuf * Bouvrir(char*);
  80. static void citation(int, int);
  81. static void robertprintentry(Entry*, Entry*, int);
  82. void
  83. robertindexentry(Entry e, int cmd)
  84. {
  85. uchar *p = (uchar *)e.start;
  86. long ea, el, da, dl, fa;
  87. Entry def, etym;
  88. ea = GLONG(&p[0]);
  89. el = GSHORT(&p[4]);
  90. da = GLONG(&p[6]);
  91. dl = GSHORT(&p[10]);
  92. fa = GLONG(&p[12]);
  93. USED(fa);
  94. if(db == 0)
  95. db = Bouvrir(dfile);
  96. def.start = malloc(dl+1);
  97. def.end = def.start + dl;
  98. def.doff = da;
  99. Bseek(db, da, 0);
  100. Bread(db, def.start, dl);
  101. *def.end = 0;
  102. if(cmd == 'h'){
  103. robertprintentry(&def, 0, cmd);
  104. }else{
  105. if(eb == 0)
  106. eb = Bouvrir(efile);
  107. etym.start = malloc(el+1);
  108. etym.end = etym.start + el;
  109. etym.doff = ea;
  110. Bseek(eb, ea, 0);
  111. Bread(eb, etym.start, el);
  112. *etym.end = 0;
  113. robertprintentry(&def, &etym, cmd);
  114. free(etym.start);
  115. }
  116. free(def.start);
  117. }
  118. static void
  119. robertprintentry(Entry *def, Entry *etym, int cmd)
  120. {
  121. uchar *p, *pe;
  122. Rune r; int c, n;
  123. int baseline = 0;
  124. int lineno = 0;
  125. int cit = 0;
  126. p = (uchar *)def->start;
  127. pe = (uchar *)def->end;
  128. while(p < pe){
  129. if(cmd == 'r'){
  130. outchar(*p++);
  131. continue;
  132. }
  133. c = *p++;
  134. switch(r = intab[c]){ /* assign = */
  135. case BROM:
  136. case ITON:
  137. case ROM:
  138. case SYM:
  139. case HEL:
  140. case BHEL:
  141. case SMALL:
  142. case ITOFF:
  143. case NONE:
  144. if(debug)
  145. outprint("\\%.2ux", c);
  146. baseline = 0;
  147. break;
  148. case SUP:
  149. baseline = 1;
  150. break;
  151. case SUB:
  152. baseline = -1;
  153. break;
  154. case CIT:
  155. n = p[0] | (p[1]<<8) | (p[2]<<16) | (p[3]<<24);
  156. p += 4;
  157. if(debug)
  158. outprint("[%d]", n);
  159. while(*p == ' ' || ('0'<=*p && *p<='9') || *p == '.'){
  160. if(debug)
  161. outchar(*p);
  162. ++p;
  163. }
  164. ++cit;
  165. outnl(2);
  166. citation(n, cmd);
  167. baseline = 0;
  168. break;
  169. case '\n':
  170. outnl(0);
  171. baseline = 0;
  172. ++lineno;
  173. break;
  174. default:
  175. if(baseline > 0 && r < nelem(suptab))
  176. r = suptab[r];
  177. else if(baseline < 0 && r < nelem(subtab))
  178. r = subtab[r];
  179. if(cit){
  180. outchar('\n');
  181. cit = 0;
  182. }
  183. outrune(r);
  184. baseline = 0;
  185. break;
  186. }
  187. if(r == '\n'){
  188. if(cmd == 'h')
  189. break;
  190. if(lineno == 1 && etym)
  191. robertprintentry(etym, 0, cmd);
  192. }
  193. }
  194. outnl(0);
  195. }
  196. static void
  197. citation(int addr, int cmd)
  198. {
  199. Entry cit;
  200. if(cb == 0)
  201. cb = Bouvrir(cfile);
  202. Bseek(cb, addr, 0);
  203. cit.start = Brdline(cb, 0xc8);
  204. cit.end = cit.start + Blinelen(cb) - 1;
  205. cit.doff = addr;
  206. *cit.end = 0;
  207. robertprintentry(&cit, 0, cmd);
  208. }
  209. long
  210. robertnextoff(long fromoff)
  211. {
  212. return (fromoff & ~15) + 16;
  213. }
  214. void
  215. robertprintkey(void)
  216. {
  217. Biobuf *db;
  218. char *l;
  219. db = Bouvrir(kfile);
  220. while(l = Brdline(db, '\n')) /* assign = */
  221. Bwrite(bout, l, Blinelen(db));
  222. Bterm(db);
  223. }
  224. void
  225. robertflexentry(Entry e, int cmd)
  226. {
  227. uchar *p, *pe;
  228. Rune r; int c;
  229. int lineno = 1;
  230. p = (uchar *)e.start;
  231. pe = (uchar *)e.end;
  232. while(p < pe){
  233. if(cmd == 'r'){
  234. Bputc(bout, *p++);
  235. continue;
  236. }
  237. c = *p++;
  238. r = intab[c];
  239. if(r == '$')
  240. r = '\n';
  241. if(r == '\n'){
  242. ++lineno;
  243. if(cmd == 'h' && lineno > 2)
  244. break;
  245. }
  246. if(cmd == 'h' && lineno < 2)
  247. continue;
  248. if(r > MULTIE){
  249. if(debug)
  250. Bprint(bout, "\\%.2ux", c);
  251. continue;
  252. }
  253. if(r < Runeself)
  254. Bputc(bout, r);
  255. else
  256. Bputrune(bout, r);
  257. }
  258. outnl(0);
  259. }
  260. long
  261. robertnextflex(long fromoff)
  262. {
  263. int c;
  264. if(Bseek(bdict, fromoff, 0) < 0)
  265. return -1;
  266. while((c = Bgetc(bdict)) >= 0){
  267. if(c == '$')
  268. return Boffset(bdict);
  269. }
  270. return -1;
  271. }
  272. static Biobuf *
  273. Bouvrir(char *fichier)
  274. {
  275. Biobuf *db;
  276. db = Bopen(fichier, OREAD);
  277. if(db == 0){
  278. fprint(2, "%s: impossible d'ouvrir %s: %r\n", argv0, fichier);
  279. exits("ouvrir");
  280. }
  281. return db;
  282. }