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>.
18 #include <lib-lib/macros.h>
33 int Charset_is_utf8 = 0;
36 static int charset_is_ja = 0;
37 static iconv_t charset_to_utf8 = (iconv_t) (-1);
38 static iconv_t charset_from_utf8 = (iconv_t) (-1);
41 void mutt_set_charset (char *charset)
45 mutt_canonical_charset (buffer, sizeof (buffer), charset);
50 if (charset_to_utf8 != (iconv_t) (-1)) {
51 iconv_close (charset_to_utf8);
52 charset_to_utf8 = (iconv_t) (-1);
54 if (charset_from_utf8 != (iconv_t) (-1)) {
55 iconv_close (charset_from_utf8);
56 charset_from_utf8 = (iconv_t) (-1);
60 if (!strcmp (buffer, "utf-8"))
63 else if (!ascii_strcasecmp (buffer, "euc-jp")
64 || !ascii_strcasecmp (buffer, "shift_jis")
65 || !ascii_strcasecmp (buffer, "cp932")
66 || !ascii_strcasecmp (buffer, "eucJP-ms")) {
68 charset_to_utf8 = iconv_open ("UTF-8", charset);
69 charset_from_utf8 = iconv_open (charset, "UTF-8");
73 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
74 bind_textdomain_codeset (PACKAGE, buffer);
81 * For systems that don't have them, we provide here our own
82 * implementations of wcrtomb(), mbrtowc(), iswprint() and wcwidth().
83 * Instead of using the locale, as these functions normally would,
84 * we use Mutt's Charset variable. We support 3 types of charset:
85 * (1) For 8-bit charsets, wchar_t uses the same encoding as char.
86 * (2) For UTF-8, wchar_t uses UCS.
87 * (3) For stateless Japanese encodings, we use UCS and convert
88 * via UTF-8 using iconv.
89 * Unfortunately, we can't handle non-stateless encodings.
92 static size_t wcrtomb_iconv (char *s, wchar_t wc, iconv_t cd)
100 ibl = mutt_wctoutf8 (buf, wc);
101 if (ibl == (size_t) (-1))
102 return (size_t) (-1);
106 r = my_iconv(cd, &ib, &ibl, &ob, &obl);
113 r = my_iconv(cd, &ib, &ibl, &ob, &obl);
118 size_t wcrtomb (char *s, wchar_t wc, mbstate_t * ps)
120 /* We only handle stateless encodings, so we can ignore ps. */
123 return mutt_wctoutf8 (s, wc);
124 else if (charset_from_utf8 != (iconv_t) (-1))
125 return wcrtomb_iconv (s, wc, charset_from_utf8);
134 return (size_t) (-1);
138 size_t mbrtowc_iconv (wchar_t * pwc, const char *s, size_t n,
139 mbstate_t * ps, iconv_t cd)
141 static mbstate_t mbstate;
142 const char *ib, *ibmax;
144 size_t ibl, obl, k, r;
145 char bufi[8], bufo[6];
148 return (size_t) (-2);
150 t = memchr (ps, 0, sizeof (*ps));
151 k = t ? (t - (char *) ps) : sizeof (*ps);
152 if (k > sizeof (bufi))
155 /* use the buffer for input */
156 memcpy (bufi, ps, k);
158 ibmax = bufi + (k + n < sizeof (bufi) ? k + n : sizeof (bufi));
159 memcpy (bufi + k, s, ibmax - bufi - k);
162 /* use the real input */
172 r = my_iconv(cd, &ib, &ibl, &ob, &obl);
173 if (ob > bufo && (!k || ib > bufi + k)) {
174 /* we have a character */
176 utf8rtowc (pwc, bufo, ob - bufo, &mbstate);
177 return (pwc && *pwc) ? (ib - (k ? bufi + k : s)) : 0;
179 else if (!r || (r == (size_t) (-1) && errno == EINVAL)) {
180 if (ib + ibl < ibmax)
181 /* try using more input */
183 else if (k && ib > bufi + k && bufi + k + n > ibmax) {
184 /* switch to using real input */
185 ib = s + (ib - bufi - k);
191 /* save the state and give up */
193 if (ibl <= sizeof (mbstate_t)) /* need extra condition here! */
194 memcpy (ps, ib, ibl);
195 return (size_t) (-2);
201 return (size_t) (-1);
206 size_t mbrtowc (wchar_t * pwc, const char *s, size_t n, mbstate_t * ps)
208 static mbstate_t mbstate;
214 return utf8rtowc (pwc, s, n, ps);
215 else if (charset_to_utf8 != (iconv_t) (-1))
216 return mbrtowc_iconv (pwc, s, n, ps, charset_to_utf8);
225 *pwc = (wchar_t) (unsigned char) *s;
230 int iswprint (wint_t wc)
232 if (Charset_is_utf8 || charset_is_ja)
233 return ((0x20 <= wc && wc < 0x7f) || 0xa0 <= wc);
235 return (0 <= wc && wc < 256) ? IsPrint (wc) : 0;
238 int iswspace (wint_t wc)
240 if (Charset_is_utf8 || charset_is_ja)
241 return (9 <= wc && wc <= 13) || wc == 32;
243 return (0 <= wc && wc < 256) ? isspace (wc) : 0;
246 static wint_t towupper_ucs (wint_t x)
248 /* Only works for x < 0x130 */
249 if ((0x60 < x && x < 0x7b) || (0xe0 <= x && x < 0xff && x != 0xf7))
251 else if (0x100 <= x && x < 0x130)
261 static wint_t towlower_ucs (wint_t x)
263 /* Only works for x < 0x130 */
264 if ((0x40 < x && x < 0x5b) || (0xc0 <= x && x < 0xdf && x != 0xd7))
266 else if (0x100 <= x && x < 0x130)
272 static int iswalnum_ucs (wint_t wc)
274 /* Only works for x < 0x220 */
282 return (0x40 < (wc & ~0x20) && (wc & ~0x20) < 0x5b);
284 return (wc == 0xaa || wc == 0xb5 || wc == 0xba);
286 return !(wc == 0xd7 || wc == 0xf7);
289 wint_t towupper (wint_t wc)
291 if (Charset_is_utf8 || charset_is_ja)
292 return towupper_ucs (wc);
294 return (0 <= wc && wc < 256) ? toupper (wc) : wc;
297 wint_t towlower (wint_t wc)
299 if (Charset_is_utf8 || charset_is_ja)
300 return towlower_ucs (wc);
302 return (0 <= wc && wc < 256) ? tolower (wc) : wc;
305 int iswalnum (wint_t wc)
307 if (Charset_is_utf8 || charset_is_ja)
308 return iswalnum_ucs (wc);
310 return (0 <= wc && wc < 256) ? isalnum (wc) : 0;
315 * Symbols, Greek and Cyrillic in JIS X 0208, Japanese Kanji
316 * Character Set, have a column width of 2.
318 int wcwidth_ja (wchar_t ucs)
321 return -1; /* continue with the normal check */
322 /* a rough range for quick check */
323 if ((ucs >= 0x00a1 && ucs <= 0x00fe) || /* Latin-1 Supplement */
324 (ucs >= 0x0391 && ucs <= 0x0451) || /* Greek and Cyrillic */
325 (ucs >= 0x2010 && ucs <= 0x266f) || /* Symbols */
326 (ucs >= 0x3000 && ucs <= 0x3020)) /* CJK Symbols and Punctuation */
332 int wcwidth_ucs (wchar_t ucs);
334 int wcwidth (wchar_t wc)
336 if (!Charset_is_utf8) {
337 if (!charset_is_ja) {
341 else if ((0 <= wc && wc < 256) && IsPrint (wc))
348 int k = wcwidth_ja (wc);
354 return wcwidth_ucs (wc);
357 size_t utf8rtowc (wchar_t * pwc, const char *s, size_t n, mbstate_t * _ps)
359 static wchar_t mbstate;
360 wchar_t *ps = (wchar_t *) _ps;
377 c = (unsigned char) *s;
388 wc = ((c & 0x1f) << 6) + (count = 0);
390 wc = ((c & 0x0f) << 12) + (count = 1);
392 wc = ((c & 0x07) << 18) + (count = 2);
394 wc = ((c & 0x03) << 24) + (count = 3);
396 wc = ((c & 0x01) << 30) + (count = 4);
404 wc = *ps & 0x7fffffff;
405 count = wc & 7; /* if count > 4 it will be caught below */
408 for (; n; ++s, --n, ++k) {
409 c = (unsigned char) *s;
410 if (0x80 <= c && c < 0xc0) {
411 wc |= (c & 0x3f) << (6 * count);
419 if (!(wc >> (11 + count * 5))) {
420 errno = count < 4 ? EILSEQ : EINVAL;
433 #endif /* !HAVE_WC_FUNCS */
435 wchar_t replacement_char (void)
437 return Charset_is_utf8 ? 0xfffd : '?';