16 * Unix Unicode-handling routines.
19 int is_dbcs_leadbyte(int codepage, char byte)
21 return 0; /* we don't do DBCS */
24 int mb_to_wc(int codepage, int flags, char *mbstr, int mblen,
25 wchar_t *wcstr, int wclen)
27 if (codepage == DEFAULT_CODEPAGE) {
31 memset(&state, 0, sizeof state);
32 setlocale(LC_CTYPE, "");
35 size_t i = mbrtowc(wcstr+n, mbstr, (size_t)mblen, &state);
36 if (i == (size_t)-1 || i == (size_t)-2)
43 setlocale(LC_CTYPE, "C");
46 } else if (codepage == CS_NONE) {
50 wcstr[n] = 0xD800 | (mbstr[0] & 0xFF);
58 return charset_to_unicode(&mbstr, &mblen, wcstr, wclen, codepage,
62 int wc_to_mb(int codepage, int flags, wchar_t *wcstr, int wclen,
63 char *mbstr, int mblen, char *defchr, int *defused,
64 struct unicode_data *ucsdata)
66 /* FIXME: we should remove the defused param completely... */
70 if (codepage == DEFAULT_CODEPAGE) {
71 char output[MB_LEN_MAX];
75 memset(&state, 0, sizeof state);
76 setlocale(LC_CTYPE, "");
79 int i = wcrtomb(output, wcstr[0], &state);
80 if (i == (size_t)-1 || i > n - mblen)
82 memcpy(mbstr+n, output, i);
88 setlocale(LC_CTYPE, "C");
91 } else if (codepage == CS_NONE) {
93 while (wclen > 0 && n < mblen) {
94 if (*wcstr >= 0xD800 && *wcstr < 0xD900)
95 mbstr[n++] = (*wcstr & 0xFF);
103 return charset_from_unicode(&wcstr, &wclen, mbstr, mblen, codepage,
104 NULL, defchr?defchr:NULL, defchr?1:0);
109 * Return value is TRUE if pterm is to run in direct-to-font mode.
111 int init_ucs(struct unicode_data *ucsdata, char *linecharset,
112 int utf8_override, int font_charset, int vtmode)
117 * In the platform-independent parts of the code, font_codepage
118 * is used only for system DBCS support - which we don't
119 * support at all. So we set this to something which will never
122 ucsdata->font_codepage = -1;
125 * If utf8_override is set and the POSIX locale settings
126 * dictate a UTF-8 character set, then just go straight for
129 ucsdata->line_codepage = CS_NONE;
132 if (((s = getenv("LC_ALL")) && *s) ||
133 ((s = getenv("LC_CTYPE")) && *s) ||
134 ((s = getenv("LANG")) && *s)) {
135 if (strstr(s, "UTF-8"))
136 ucsdata->line_codepage = CS_UTF8;
141 * Failing that, line_codepage should be decoded from the
142 * specification in cfg.
144 if (ucsdata->line_codepage == CS_NONE)
145 ucsdata->line_codepage = decode_codepage(linecharset);
148 * If line_codepage is _still_ CS_NONE, we assume we're using
149 * the font's own encoding. This has been passed in to us, so
150 * we use that. If it's still CS_NONE after _that_ - i.e. the
151 * font we were given had an incomprehensible charset - then we
152 * fall back to using the D800 page.
154 if (ucsdata->line_codepage == CS_NONE)
155 ucsdata->line_codepage = font_charset;
157 if (ucsdata->line_codepage == CS_NONE)
161 * Set up unitab_line, by translating each individual character
162 * in the line codepage into Unicode.
164 for (i = 0; i < 256; i++) {
171 if (ucsdata->line_codepage == CS_NONE)
172 ucsdata->unitab_line[i] = 0xD800 | i;
173 else if (1 == charset_to_unicode(&p, &len, wc, 1,
174 ucsdata->line_codepage,
176 ucsdata->unitab_line[i] = wc[0];
178 ucsdata->unitab_line[i] = 0xFFFD;
182 * Set up unitab_xterm. This is the same as unitab_line except
183 * in the line-drawing regions, where it follows the Unicode
186 * (Note that the strange X encoding of line-drawing characters
187 * in the bottom 32 glyphs of ISO8859-1 fonts is taken care of
188 * by the font encoding, which will spot such a font and act as
189 * if it were in a variant encoding of ISO8859-1.)
191 for (i = 0; i < 256; i++) {
192 static const wchar_t unitab_xterm_std[32] = {
193 0x2666, 0x2592, 0x2409, 0x240c, 0x240d, 0x240a, 0x00b0, 0x00b1,
194 0x2424, 0x240b, 0x2518, 0x2510, 0x250c, 0x2514, 0x253c, 0x23ba,
195 0x23bb, 0x2500, 0x23bc, 0x23bd, 0x251c, 0x2524, 0x2534, 0x252c,
196 0x2502, 0x2264, 0x2265, 0x03c0, 0x2260, 0x00a3, 0x00b7, 0x0020
198 static const wchar_t unitab_xterm_poorman[32] =
199 L"*#****o~**+++++-----++++|****L. ";
203 if (vtmode == VT_POORMAN)
204 ptr = unitab_xterm_poorman;
206 ptr = unitab_xterm_std;
208 if (i >= 0x5F && i < 0x7F)
209 ucsdata->unitab_xterm[i] = ptr[i & 0x1F];
211 ucsdata->unitab_xterm[i] = ucsdata->unitab_line[i];
215 * Set up unitab_scoacs. The SCO Alternate Character Set is
218 for (i = 0; i < 256; i++) {
225 if (1 == charset_to_unicode(&p, &len, wc, 1, CS_CP437, NULL, L"", 0))
226 ucsdata->unitab_scoacs[i] = wc[0];
228 ucsdata->unitab_scoacs[i] = 0xFFFD;
232 * Find the control characters in the line codepage. For
233 * direct-to-font mode using the D800 hack, we assume 00-1F and
234 * 7F are controls, but allow 80-9F through. (It's as good a
235 * guess as anything; and my bet is that half the weird fonts
236 * used in this way will be IBM or MS code pages anyway.)
238 for (i = 0; i < 256; i++) {
239 int lineval = ucsdata->unitab_line[i];
240 if (lineval < ' ' || (lineval >= 0x7F && lineval < 0xA0) ||
241 (lineval >= 0xD800 && lineval < 0xD820) || (lineval == 0xD87F))
242 ucsdata->unitab_ctrl[i] = i;
244 ucsdata->unitab_ctrl[i] = 0xFF;
250 const char *cp_name(int codepage)
252 if (codepage == CS_NONE)
253 return "Use font encoding";
254 return charset_to_localenc(codepage);
257 const char *cp_enumerate(int index)
261 return "Use font encoding";
262 charset = charset_localenc_nth(index-1);
263 if (charset == CS_NONE)
265 return charset_to_localenc(charset);
268 int decode_codepage(char *cp_name)
271 return CS_NONE; /* use font encoding */
272 return charset_from_localenc(cp_name);