unicode.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <locale.h>
  5. #include <limits.h>
  6. #include <wchar.h>
  7. #include <time.h>
  8. #include "putty.h"
  9. #include "charset.h"
  10. #include "terminal.h"
  11. #include "misc.h"
  12. /*
  13. * Unix Unicode-handling routines.
  14. */
  15. bool is_dbcs_leadbyte(int codepage, char byte)
  16. {
  17. return false; /* we don't do DBCS */
  18. }
  19. int mb_to_wc(int codepage, int flags, const char *mbstr, int mblen,
  20. wchar_t *wcstr, int wclen)
  21. {
  22. if (codepage == DEFAULT_CODEPAGE) {
  23. int n = 0;
  24. mbstate_t state;
  25. memset(&state, 0, sizeof state);
  26. while (mblen > 0) {
  27. if (n >= wclen)
  28. return n;
  29. size_t i = mbrtowc(wcstr+n, mbstr, (size_t)mblen, &state);
  30. if (i == (size_t)-1 || i == (size_t)-2)
  31. break;
  32. n++;
  33. mbstr += i;
  34. mblen -= i;
  35. }
  36. return n;
  37. } else if (codepage == CS_NONE) {
  38. int n = 0;
  39. while (mblen > 0) {
  40. if (n >= wclen)
  41. return n;
  42. wcstr[n] = 0xD800 | (mbstr[0] & 0xFF);
  43. n++;
  44. mbstr++;
  45. mblen--;
  46. }
  47. return n;
  48. } else
  49. return charset_to_unicode(&mbstr, &mblen, wcstr, wclen, codepage,
  50. NULL, NULL, 0);
  51. }
  52. int wc_to_mb(int codepage, int flags, const wchar_t *wcstr, int wclen,
  53. char *mbstr, int mblen, const char *defchr)
  54. {
  55. if (codepage == DEFAULT_CODEPAGE) {
  56. char output[MB_LEN_MAX];
  57. mbstate_t state;
  58. int n = 0;
  59. memset(&state, 0, sizeof state);
  60. while (wclen > 0) {
  61. size_t i = wcrtomb(output, wcstr[0], &state);
  62. if (i == (size_t)-1 || i > n - mblen)
  63. break;
  64. memcpy(mbstr+n, output, i);
  65. n += i;
  66. wcstr++;
  67. wclen--;
  68. }
  69. return n;
  70. } else if (codepage == CS_NONE) {
  71. int n = 0;
  72. while (wclen > 0 && n < mblen) {
  73. if (*wcstr >= 0xD800 && *wcstr < 0xD900)
  74. mbstr[n++] = (*wcstr & 0xFF);
  75. else if (defchr)
  76. mbstr[n++] = *defchr;
  77. wcstr++;
  78. wclen--;
  79. }
  80. return n;
  81. } else {
  82. return charset_from_unicode(&wcstr, &wclen, mbstr, mblen, codepage,
  83. NULL, defchr?defchr:NULL, defchr?1:0);
  84. }
  85. }
  86. /*
  87. * Return value is true if pterm is to run in direct-to-font mode.
  88. */
  89. bool init_ucs(struct unicode_data *ucsdata, char *linecharset,
  90. bool utf8_override, int font_charset, int vtmode)
  91. {
  92. int i;
  93. bool ret = false;
  94. /*
  95. * In the platform-independent parts of the code, font_codepage
  96. * is used only for system DBCS support - which we don't
  97. * support at all. So we set this to something which will never
  98. * be used.
  99. */
  100. ucsdata->font_codepage = -1;
  101. /*
  102. * If utf8_override is set and the POSIX locale settings
  103. * dictate a UTF-8 character set, then just go straight for
  104. * UTF-8.
  105. */
  106. ucsdata->line_codepage = CS_NONE;
  107. if (utf8_override) {
  108. const char *s;
  109. if (((s = getenv("LC_ALL")) && *s) ||
  110. ((s = getenv("LC_CTYPE")) && *s) ||
  111. ((s = getenv("LANG")) && *s)) {
  112. if (strstr(s, "UTF-8"))
  113. ucsdata->line_codepage = CS_UTF8;
  114. }
  115. }
  116. /*
  117. * Failing that, line_codepage should be decoded from the
  118. * specification in conf.
  119. */
  120. if (ucsdata->line_codepage == CS_NONE)
  121. ucsdata->line_codepage = decode_codepage(linecharset);
  122. /*
  123. * If line_codepage is _still_ CS_NONE, we assume we're using
  124. * the font's own encoding. This has been passed in to us, so
  125. * we use that. If it's still CS_NONE after _that_ - i.e. the
  126. * font we were given had an incomprehensible charset - then we
  127. * fall back to using the D800 page.
  128. */
  129. if (ucsdata->line_codepage == CS_NONE)
  130. ucsdata->line_codepage = font_charset;
  131. if (ucsdata->line_codepage == CS_NONE)
  132. ret = true;
  133. /*
  134. * Set up unitab_line, by translating each individual character
  135. * in the line codepage into Unicode.
  136. */
  137. for (i = 0; i < 256; i++) {
  138. char c[1];
  139. const char *p;
  140. wchar_t wc[1];
  141. int len;
  142. c[0] = i;
  143. p = c;
  144. len = 1;
  145. if (ucsdata->line_codepage == CS_NONE)
  146. ucsdata->unitab_line[i] = 0xD800 | i;
  147. else if (1 == charset_to_unicode(&p, &len, wc, 1,
  148. ucsdata->line_codepage,
  149. NULL, L"", 0))
  150. ucsdata->unitab_line[i] = wc[0];
  151. else
  152. ucsdata->unitab_line[i] = 0xFFFD;
  153. }
  154. /*
  155. * Set up unitab_xterm. This is the same as unitab_line except
  156. * in the line-drawing regions, where it follows the Unicode
  157. * encoding.
  158. *
  159. * (Note that the strange X encoding of line-drawing characters
  160. * in the bottom 32 glyphs of ISO8859-1 fonts is taken care of
  161. * by the font encoding, which will spot such a font and act as
  162. * if it were in a variant encoding of ISO8859-1.)
  163. */
  164. for (i = 0; i < 256; i++) {
  165. static const wchar_t unitab_xterm_std[32] = {
  166. 0x2666, 0x2592, 0x2409, 0x240c, 0x240d, 0x240a, 0x00b0, 0x00b1,
  167. 0x2424, 0x240b, 0x2518, 0x2510, 0x250c, 0x2514, 0x253c, 0x23ba,
  168. 0x23bb, 0x2500, 0x23bc, 0x23bd, 0x251c, 0x2524, 0x2534, 0x252c,
  169. 0x2502, 0x2264, 0x2265, 0x03c0, 0x2260, 0x00a3, 0x00b7, 0x0020
  170. };
  171. static const wchar_t unitab_xterm_poorman[32] =
  172. L"*#****o~**+++++-----++++|****L. ";
  173. const wchar_t *ptr;
  174. if (vtmode == VT_POORMAN)
  175. ptr = unitab_xterm_poorman;
  176. else
  177. ptr = unitab_xterm_std;
  178. if (i >= 0x5F && i < 0x7F)
  179. ucsdata->unitab_xterm[i] = ptr[i & 0x1F];
  180. else
  181. ucsdata->unitab_xterm[i] = ucsdata->unitab_line[i];
  182. }
  183. /*
  184. * Set up unitab_scoacs. The SCO Alternate Character Set is
  185. * simply CP437.
  186. */
  187. for (i = 0; i < 256; i++) {
  188. char c[1];
  189. const char *p;
  190. wchar_t wc[1];
  191. int len;
  192. c[0] = i;
  193. p = c;
  194. len = 1;
  195. if (1 == charset_to_unicode(&p, &len, wc, 1, CS_CP437, NULL, L"", 0))
  196. ucsdata->unitab_scoacs[i] = wc[0];
  197. else
  198. ucsdata->unitab_scoacs[i] = 0xFFFD;
  199. }
  200. /*
  201. * Find the control characters in the line codepage. For
  202. * direct-to-font mode using the D800 hack, we assume 00-1F and
  203. * 7F are controls, but allow 80-9F through. (It's as good a
  204. * guess as anything; and my bet is that half the weird fonts
  205. * used in this way will be IBM or MS code pages anyway.)
  206. */
  207. for (i = 0; i < 256; i++) {
  208. int lineval = ucsdata->unitab_line[i];
  209. if (lineval < ' ' || (lineval >= 0x7F && lineval < 0xA0) ||
  210. (lineval >= 0xD800 && lineval < 0xD820) || (lineval == 0xD87F))
  211. ucsdata->unitab_ctrl[i] = i;
  212. else
  213. ucsdata->unitab_ctrl[i] = 0xFF;
  214. }
  215. return ret;
  216. }
  217. void init_ucs_generic(Conf *conf, struct unicode_data *ucsdata)
  218. {
  219. init_ucs(ucsdata, conf_get_str(conf, CONF_line_codepage),
  220. conf_get_bool(conf, CONF_utf8_override),
  221. CS_NONE, conf_get_int(conf, CONF_vtmode));
  222. }
  223. const char *cp_name(int codepage)
  224. {
  225. if (codepage == CS_NONE)
  226. return "Use font encoding";
  227. return charset_to_localenc(codepage);
  228. }
  229. const char *cp_enumerate(int index)
  230. {
  231. int charset;
  232. charset = charset_localenc_nth(index);
  233. if (charset == CS_NONE) {
  234. /* "Use font encoding" comes after all the named charsets */
  235. if (charset_localenc_nth(index-1) != CS_NONE)
  236. return "Use font encoding";
  237. return NULL;
  238. }
  239. return charset_to_localenc(charset);
  240. }
  241. int decode_codepage(const char *cp_name)
  242. {
  243. if (!cp_name || !*cp_name)
  244. return CS_UTF8;
  245. return charset_from_localenc(cp_name);
  246. }