OSDN Git Service

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