2 * Copyright notice from original mutt:
3 * Copyright (C) 2000 Edmund Grimley Evans <edmundo@rano.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
11 * Japanese support by TAKIZAWA Takashi <taki@luna.email.ne.jp>.
30 int Charset_is_utf8 = 0;
33 static int charset_is_ja = 0;
34 static iconv_t charset_to_utf8 = (iconv_t) (-1);
35 static iconv_t charset_from_utf8 = (iconv_t) (-1);
38 void mutt_set_charset (char *charset)
42 mutt_canonical_charset (buffer, sizeof (buffer), charset);
47 if (charset_to_utf8 != (iconv_t) (-1)) {
48 iconv_close (charset_to_utf8);
49 charset_to_utf8 = (iconv_t) (-1);
51 if (charset_from_utf8 != (iconv_t) (-1)) {
52 iconv_close (charset_from_utf8);
53 charset_from_utf8 = (iconv_t) (-1);
57 if (!strcmp (buffer, "utf-8"))
60 else if (!ascii_strcasecmp (buffer, "euc-jp")
61 || !ascii_strcasecmp (buffer, "shift_jis")
62 || !ascii_strcasecmp (buffer, "cp932")
63 || !ascii_strcasecmp (buffer, "eucJP-ms")) {
65 charset_to_utf8 = iconv_open ("UTF-8", charset);
66 charset_from_utf8 = iconv_open (charset, "UTF-8");
70 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
71 bind_textdomain_codeset (PACKAGE, buffer);
78 * For systems that don't have them, we provide here our own
79 * implementations of wcrtomb(), mbrtowc(), iswprint() and wcwidth().
80 * Instead of using the locale, as these functions normally would,
81 * we use Mutt's Charset variable. We support 3 types of charset:
82 * (1) For 8-bit charsets, wchar_t uses the same encoding as char.
83 * (2) For UTF-8, wchar_t uses UCS.
84 * (3) For stateless Japanese encodings, we use UCS and convert
85 * via UTF-8 using iconv.
86 * Unfortunately, we can't handle non-stateless encodings.
89 static size_t wcrtomb_iconv (char *s, wchar_t wc, iconv_t cd)
97 ibl = mutt_wctoutf8 (buf, wc);
98 if (ibl == (size_t) (-1))
103 r = iconv (cd, &ib, &ibl, &ob, &obl);
110 r = iconv (cd, &ib, &ibl, &ob, &obl);
115 size_t wcrtomb (char *s, wchar_t wc, mbstate_t * ps)
117 /* We only handle stateless encodings, so we can ignore ps. */
120 return mutt_wctoutf8 (s, wc);
121 else if (charset_from_utf8 != (iconv_t) (-1))
122 return wcrtomb_iconv (s, wc, charset_from_utf8);
131 return (size_t) (-1);
135 size_t mbrtowc_iconv (wchar_t * pwc, const char *s, size_t n,
136 mbstate_t * ps, iconv_t cd)
138 static mbstate_t mbstate;
139 ICONV_CONST char *ib, *ibmax;
141 size_t ibl, obl, k, r;
142 char bufi[8], bufo[6];
145 return (size_t) (-2);
147 t = memchr (ps, 0, sizeof (*ps));
148 k = t ? (t - (char *) ps) : sizeof (*ps);
149 if (k > sizeof (bufi))
152 /* use the buffer for input */
153 memcpy (bufi, ps, k);
155 ibmax = bufi + (k + n < sizeof (bufi) ? k + n : sizeof (bufi));
156 memcpy (bufi + k, s, ibmax - bufi - k);
159 /* use the real input */
169 r = iconv (cd, &ib, &ibl, &ob, &obl);
170 if (ob > bufo && (!k || ib > bufi + k)) {
171 /* we have a character */
172 memset (ps, 0, sizeof (*ps));
173 utf8rtowc (pwc, bufo, ob - bufo, &mbstate);
174 return (pwc && *pwc) ? (ib - (k ? bufi + k : s)) : 0;
176 else if (!r || (r == (size_t) (-1) && errno == EINVAL)) {
177 if (ib + ibl < ibmax)
178 /* try using more input */
180 else if (k && ib > bufi + k && bufi + k + n > ibmax) {
181 /* switch to using real input */
182 ib = s + (ib - bufi - k);
188 /* save the state and give up */
189 memset (ps, 0, sizeof (*ps));
190 if (ibl <= sizeof (mbstate_t)) /* need extra condition here! */
191 memcpy (ps, ib, ibl);
192 return (size_t) (-2);
198 return (size_t) (-1);
203 size_t mbrtowc (wchar_t * pwc, const char *s, size_t n, mbstate_t * ps)
205 static mbstate_t mbstate;
211 return utf8rtowc (pwc, s, n, ps);
212 else if (charset_to_utf8 != (iconv_t) (-1))
213 return mbrtowc_iconv (pwc, s, n, ps, charset_to_utf8);
216 memset (ps, 0, sizeof (*ps));
222 *pwc = (wchar_t) (unsigned char) *s;
227 int iswprint (wint_t wc)
229 if (Charset_is_utf8 || charset_is_ja)
230 return ((0x20 <= wc && wc < 0x7f) || 0xa0 <= wc);
232 return (0 <= wc && wc < 256) ? IsPrint (wc) : 0;
235 int iswspace (wint_t wc)
237 if (Charset_is_utf8 || charset_is_ja)
238 return (9 <= wc && wc <= 13) || wc == 32;
240 return (0 <= wc && wc < 256) ? isspace (wc) : 0;
243 static wint_t towupper_ucs (wint_t x)
245 /* Only works for x < 0x130 */
246 if ((0x60 < x && x < 0x7b) || (0xe0 <= x && x < 0xff && x != 0xf7))
248 else if (0x100 <= x && x < 0x130)
258 static wint_t towlower_ucs (wint_t x)
260 /* Only works for x < 0x130 */
261 if ((0x40 < x && x < 0x5b) || (0xc0 <= x && x < 0xdf && x != 0xd7))
263 else if (0x100 <= x && x < 0x130)
269 static int iswalnum_ucs (wint_t wc)
271 /* Only works for x < 0x220 */
279 return (0x40 < (wc & ~0x20) && (wc & ~0x20) < 0x5b);
281 return (wc == 0xaa || wc == 0xb5 || wc == 0xba);
283 return !(wc == 0xd7 || wc == 0xf7);
286 wint_t towupper (wint_t wc)
288 if (Charset_is_utf8 || charset_is_ja)
289 return towupper_ucs (wc);
291 return (0 <= wc && wc < 256) ? toupper (wc) : wc;
294 wint_t towlower (wint_t wc)
296 if (Charset_is_utf8 || charset_is_ja)
297 return towlower_ucs (wc);
299 return (0 <= wc && wc < 256) ? tolower (wc) : wc;
302 int iswalnum (wint_t wc)
304 if (Charset_is_utf8 || charset_is_ja)
305 return iswalnum_ucs (wc);
307 return (0 <= wc && wc < 256) ? isalnum (wc) : 0;
312 * Symbols, Greek and Cyrillic in JIS X 0208, Japanese Kanji
313 * Character Set, have a column width of 2.
315 int wcwidth_ja (wchar_t ucs)
318 return -1; /* continue with the normal check */
319 /* a rough range for quick check */
320 if ((ucs >= 0x00a1 && ucs <= 0x00fe) || /* Latin-1 Supplement */
321 (ucs >= 0x0391 && ucs <= 0x0451) || /* Greek and Cyrillic */
322 (ucs >= 0x2010 && ucs <= 0x266f) || /* Symbols */
323 (ucs >= 0x3000 && ucs <= 0x3020)) /* CJK Symbols and Punctuation */
329 int wcwidth_ucs (wchar_t ucs);
331 int wcwidth (wchar_t wc)
333 if (!Charset_is_utf8) {
334 if (!charset_is_ja) {
338 else if ((0 <= wc && wc < 256) && IsPrint (wc))
345 int k = wcwidth_ja (wc);
351 return wcwidth_ucs (wc);
354 size_t utf8rtowc (wchar_t * pwc, const char *s, size_t n, mbstate_t * _ps)
356 static wchar_t mbstate;
357 wchar_t *ps = (wchar_t *) _ps;
374 c = (unsigned char) *s;
385 wc = ((c & 0x1f) << 6) + (count = 0);
387 wc = ((c & 0x0f) << 12) + (count = 1);
389 wc = ((c & 0x07) << 18) + (count = 2);
391 wc = ((c & 0x03) << 24) + (count = 3);
393 wc = ((c & 0x01) << 30) + (count = 4);
401 wc = *ps & 0x7fffffff;
402 count = wc & 7; /* if count > 4 it will be caught below */
405 for (; n; ++s, --n, ++k) {
406 c = (unsigned char) *s;
407 if (0x80 <= c && c < 0xc0) {
408 wc |= (c & 0x3f) << (6 * count);
416 if (!(wc >> (11 + count * 5))) {
417 errno = count < 4 ? EILSEQ : EINVAL;
430 #endif /* !HAVE_WC_FUNCS */
432 wchar_t replacement_char ()
434 return Charset_is_utf8 ? 0xfffd : '?';