uxucs.c 6.8 KB

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