char.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 "a.h"
  10. /*
  11. * Translate Unicode to HTML by asking tcs(1).
  12. * This way we don't have yet another table.
  13. */
  14. Rune*
  15. rune2html(Rune r)
  16. {
  17. static Biobuf b;
  18. static int fd = -1;
  19. static Rune **tcscache[256];
  20. int p[2];
  21. char *q;
  22. if(r == '\n')
  23. return L("\n");
  24. if(((uint)r&~0xFFFF) != 0){
  25. /* The cache must grow a lot to handle them */
  26. fprint(2, "%s: can't handle rune '%C'\n", argv0, r);
  27. return L("?");
  28. }
  29. if(tcscache[r>>8] && tcscache[r>>8][r&0xFF])
  30. return tcscache[r>>8][r&0xFF];
  31. if(fd < 0){
  32. if(pipe(p) < 0)
  33. sysfatal("pipe: %r");
  34. switch(fork()){
  35. case -1:
  36. sysfatal("fork: %r");
  37. case 0:
  38. dup(p[0], 0);
  39. dup(p[1], 1);
  40. close(p[0]);
  41. close(p[1]);
  42. execl("/bin/tcs", "tcs", "-t", "html", nil);
  43. _exits(0);
  44. default:
  45. fd = p[1];
  46. Binit(&b, p[0], OREAD);
  47. break;
  48. }
  49. }
  50. /* HACK: extra newlines force rune+\n through tcs now */
  51. fprint(fd, "%C\n\n\n\n", r);
  52. q = Brdline(&b, '\n');
  53. while (q != nil && *q == '\n')
  54. q = Brdline(&b, '\n');
  55. if(q == nil)
  56. sysfatal("tcs: early eof");
  57. q[Blinelen(&b)-1] = 0;
  58. if(tcscache[r>>8] == nil)
  59. tcscache[r>>8] = emalloc(256*sizeof tcscache[0][0]);
  60. tcscache[r>>8][r&0xFF] = erunesmprint("%s", q);
  61. return tcscache[r>>8][r&0xFF];
  62. }
  63. /*
  64. * Translate troff to Unicode by looking in troff's utfmap.
  65. * This way we don't have yet another hard-coded table.
  66. */
  67. typedef struct Trtab Trtab;
  68. struct Trtab
  69. {
  70. char t[UTFmax];
  71. Rune r;
  72. };
  73. static Trtab trtab[200];
  74. int ntrtab;
  75. static Trtab trinit[] =
  76. {
  77. {"pl", Upl},
  78. {"eq", Ueq},
  79. {"em", 0x2014},
  80. {"en", 0x2013},
  81. {"mi", Umi},
  82. {"fm", 0x2032},
  83. };
  84. Rune
  85. troff2rune(Rune *rs)
  86. {
  87. char *file, *f[10], *p, s[3];
  88. int i, nf;
  89. Biobuf *b;
  90. if(rs[0] >= Runeself || rs[1] >= Runeself)
  91. return Runeerror;
  92. s[0] = rs[0];
  93. s[1] = rs[1];
  94. s[2] = 0;
  95. if(ntrtab == 0){
  96. for(i=0; i<nelem(trinit) && ntrtab < nelem(trtab); i++){
  97. trtab[ntrtab] = trinit[i];
  98. ntrtab++;
  99. }
  100. file = "/sys/lib/troff/font/devutf/utfmap";
  101. if((b = Bopen(file, OREAD)) == nil)
  102. sysfatal("open %s: %r", file);
  103. while((p = Brdline(b, '\n')) != nil){
  104. p[Blinelen(b)-1] = 0;
  105. nf = getfields(p, f, nelem(f), 0, "\t");
  106. for(i=0; i+2<=nf && ntrtab<nelem(trtab); i+=2){
  107. chartorune(&trtab[ntrtab].r, f[i]);
  108. memmove(trtab[ntrtab].t, f[i+1], 2);
  109. ntrtab++;
  110. }
  111. }
  112. Bterm(b);
  113. if(ntrtab >= nelem(trtab))
  114. fprint(2, "%s: trtab too small\n", argv0);
  115. }
  116. for(i=0; i<ntrtab; i++)
  117. if(strcmp(s, trtab[i].t) == 0)
  118. return trtab[i].r;
  119. return Runeerror;
  120. }