robert.c 7.1 KB

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