2 * Copyright (c) 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
6 * Copyright (c) 2011, 2012
7 * Zhihao Yuan. All rights reserved.
9 * See the LICENSE file for redistribution information.
14 #include <sys/types.h>
15 #include <sys/queue.h>
18 #include <bitstring.h>
33 * Get the locale encoding.
35 * PUBLIC: char * codeset(void);
43 cs = nl_langinfo(CODESET);
50 raw2int(SCR *sp, const char * str, ssize_t len, CONVWIN *cw, size_t *tolen,
54 CHAR_T **tostr = &cw->bp1.wc;
55 size_t *blen = &cw->blen1;
57 BINC_RETW(NULL, *tostr, *blen, len);
60 for (i = 0; i < len; ++i)
61 (*tostr)[i] = (u_char) str[i];
68 #define CONV_BUFFER_SIZE 512
69 /* fill the buffer with codeset encoding of string pointed to by str
70 * left has the number of bytes left in str and is adjusted
71 * len contains the number of bytes put in the buffer
74 #define CONVERT(str, left, src, len) \
78 outleft = CONV_BUFFER_SIZE; \
80 if (iconv(id, (iconv_src_t)&str, &left, &bp, &outleft) \
81 == -1 && errno != E2BIG) \
83 if ((len = CONV_BUFFER_SIZE - outleft) == 0) { \
92 if (id != (iconv_t)-1) \
93 iconv(id, NULL, NULL, NULL, NULL); \
96 #define CONVERT(str, left, src, len)
101 default_char2int(SCR *sp, const char * str, ssize_t len, CONVWIN *cw,
102 size_t *tolen, CHAR_T **dst, iconv_t id)
105 CHAR_T **tostr = &cw->bp1.wc;
106 size_t *blen = &cw->blen1;
110 char *src = (char *)str;
112 char buffer[CONV_BUFFER_SIZE];
117 memset(&mbs, 0, sizeof(mbs));
118 BINC_RETW(NULL, *tostr, *blen, nlen);
121 if (id != (iconv_t)-1)
122 CONVERT(str, left, src, len);
125 for (i = 0, j = 0; j < len; ) {
126 n = mbrtowc((*tostr)+i, src+j, len-j, &mbs);
127 /* NULL character converted */
130 if (n == -1 || n == -2)
137 BINC_RETW(NULL, *tostr, *blen, nlen);
139 if (id != (iconv_t)-1 && j == len && left) {
140 CONVERT(str, left, src, len);
155 fe_char2int(SCR *sp, const char * str, ssize_t len, CONVWIN *cw, size_t *tolen,
158 return default_char2int(sp, str, len, cw, tolen, dst,
159 sp->conv.id[IC_FE_CHAR2INT]);
163 ie_char2int(SCR *sp, const char * str, ssize_t len, CONVWIN *cw, size_t *tolen,
166 return default_char2int(sp, str, len, cw, tolen, dst,
167 sp->conv.id[IC_IE_CHAR2INT]);
171 cs_char2int(SCR *sp, const char * str, ssize_t len, CONVWIN *cw, size_t *tolen,
174 return default_char2int(sp, str, len, cw, tolen, dst, (iconv_t)-1);
178 int2raw(SCR *sp, const CHAR_T * str, ssize_t len, CONVWIN *cw, size_t *tolen,
182 char **tostr = &cw->bp1.c;
183 size_t *blen = &cw->blen1;
185 BINC_RETC(NULL, *tostr, *blen, len);
188 for (i = 0; i < len; ++i)
189 (*tostr)[i] = str[i];
197 default_int2char(SCR *sp, const CHAR_T * str, ssize_t len, CONVWIN *cw,
198 size_t *tolen, char **pdst, iconv_t id)
200 size_t i, j, offset = 0;
201 char **tostr = &cw->bp1.c;
202 size_t *blen = &cw->blen1;
205 ssize_t nlen = len + MB_CUR_MAX;
209 char buffer[CONV_BUFFER_SIZE];
213 /* convert first len bytes of buffer and append it to cw->bp
214 * len is adjusted => 0
215 * offset contains the offset in cw->bp and is adjusted
216 * cw->bp is grown as required
219 #define CONVERT2(_buffer, lenp, cw, offset) \
221 char *bp = _buffer; \
224 size_t outleft = cw->blen1 - offset; \
225 char *obp = cw->bp1.c + offset; \
226 if (cw->blen1 < offset + MB_CUR_MAX) { \
228 BINC_RETC(NULL, cw->bp1.c, cw->blen1, \
232 ret = iconv(id, (iconv_src_t)&bp, lenp, &obp, \
234 if (ret == -1 && errno != E2BIG) \
236 offset = cw->blen1 - outleft; \
237 } while (ret != 0); \
240 #define CONVERT2(_buffer, lenp, cw, offset)
244 memset(&mbs, 0, sizeof(mbs));
245 BINC_RETC(NULL, *tostr, *blen, nlen);
246 dst = *tostr; buflen = *blen;
249 if (id != (iconv_t)-1) {
250 dst = buffer; buflen = CONV_BUFFER_SIZE;
254 for (i = 0, j = 0; i < len; ++i) {
255 n = wcrtomb(dst+j, str[i], &mbs);
259 if (buflen < j + MB_CUR_MAX) {
260 if (id != (iconv_t)-1) {
261 CONVERT2(buffer, &j, cw, offset);
264 BINC_RETC(NULL, *tostr, *blen, nlen);
265 dst = *tostr; buflen = *blen;
270 n = wcrtomb(dst+j, L'\0', &mbs);
271 j += n - 1; /* don't count NUL at the end */
274 if (id != (iconv_t)-1) {
275 CONVERT2(buffer, &j, cw, offset);
276 /* back to the initial state */
277 CONVERT2(NULL, NULL, cw, offset);
292 fe_int2char(SCR *sp, const CHAR_T * str, ssize_t len, CONVWIN *cw,
293 size_t *tolen, char **dst)
295 return default_int2char(sp, str, len, cw, tolen, dst,
296 sp->conv.id[IC_FE_INT2CHAR]);
300 cs_int2char(SCR *sp, const CHAR_T * str, ssize_t len, CONVWIN *cw,
301 size_t *tolen, char **dst)
303 return default_int2char(sp, str, len, cw, tolen, dst, (iconv_t)-1);
310 * Initialize the iconv environment.
312 * PUBLIC: void conv_init(SCR *, SCR *);
315 conv_init(SCR *orig, SCR *sp)
320 setlocale(LC_ALL, "");
322 memmove(&sp->conv, &orig->conv, sizeof(CONV));
325 char *ctype = setlocale(LC_CTYPE, NULL);
329 * This hack fixes the libncursesw issue on FreeBSD.
331 if (!strcmp(ctype, "ko_KR.CP949"))
332 setlocale(LC_CTYPE, "ko_KR.eucKR");
333 else if (!strcmp(ctype, "zh_CN.GB2312"))
334 setlocale(LC_CTYPE, "zh_CN.eucCN");
335 else if (!strcmp(ctype, "zh_CN.GBK"))
336 setlocale(LC_CTYPE, "zh_CN.GB18030");
339 * Switch to 8bit mode if locale is C;
340 * LC_CTYPE should be reseted to C if unmatched.
342 if (!strcmp(ctype, "C") || !strcmp(ctype, "POSIX")) {
343 sp->conv.sys2int = sp->conv.file2int = raw2int;
344 sp->conv.int2sys = sp->conv.int2file = int2raw;
345 sp->conv.input2int = raw2int;
347 sp->conv.sys2int = cs_char2int;
348 sp->conv.int2sys = cs_int2char;
349 sp->conv.file2int = fe_char2int;
350 sp->conv.int2file = fe_int2char;
351 sp->conv.input2int = ie_char2int;
354 o_set(sp, O_INPUTENCODING, OS_STRDUP, codeset(), 0);
359 /* iconv descriptors must be distinct to screens. */
360 for (i = 0; i <= IC_IE_TO_UTF16; ++i)
361 sp->conv.id[i] = (iconv_t)-1;
363 conv_enc(sp, O_INPUTENCODING, 0);
369 * Convert file/input encoding.
371 * PUBLIC: int conv_enc(SCR *, int, char *);
374 conv_enc(SCR *sp, int option, char *enc)
376 #if defined(USE_WIDECHAR) && defined(USE_ICONV)
378 iconv_t id_c2w, id_w2c;
382 c2w = sp->conv.id + IC_FE_CHAR2INT;
383 w2c = sp->conv.id + IC_FE_INT2CHAR;
385 enc = O_STR(sp, O_FILEENCODING);
387 if (strcasecmp(codeset(), enc)) {
388 if ((id_c2w = iconv_open(codeset(), enc)) ==
391 if ((id_w2c = iconv_open(enc, codeset())) ==
395 id_c2w = (iconv_t)-1;
396 id_w2c = (iconv_t)-1;
401 case O_INPUTENCODING:
402 c2w = sp->conv.id + IC_IE_CHAR2INT;
403 w2c = sp->conv.id + IC_IE_TO_UTF16;
405 enc = O_STR(sp, O_INPUTENCODING);
407 if (strcasecmp(codeset(), enc)) {
408 if ((id_c2w = iconv_open(codeset(), enc)) ==
412 id_c2w = (iconv_t)-1;
414 /* UTF-16 can not be locale and can not be inputed. */
415 if ((id_w2c = iconv_open("utf-16be", enc)) == (iconv_t)-1)
424 if (*c2w != (iconv_t)-1)
426 if (*w2c != (iconv_t)-1)
432 F_CLR(sp, SC_CONV_ERROR);
433 F_SET(sp, SC_SCR_REFORMAT);
440 msgq(sp, M_ERR, "321|File encoding conversion not supported");
442 case O_INPUTENCODING:
443 msgq(sp, M_ERR, "322|Input encoding conversion not supported");
451 * Close the iconv descriptors, release the buffer.
453 * PUBLIC: void conv_end(SCR *);
458 #if defined(USE_WIDECHAR) && defined(USE_ICONV)
460 for (i = 0; i <= IC_IE_TO_UTF16; ++i)
461 if (sp->conv.id[i] != (iconv_t)-1)
462 iconv_close(sp->conv.id[i]);