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.
15 static const char sccsid
[] = "$Id: conv.c,v 2.39 2013/07/01 23:28:13 zy Exp $";
18 #include <sys/types.h>
19 #include <sys/queue.h>
22 #include <bitstring.h>
37 * Get the locale encoding.
39 * PUBLIC: char * codeset __P((void));
46 cs
= nl_langinfo(CODESET
);
52 raw2int(SCR
*sp
, const char * str
, ssize_t len
, CONVWIN
*cw
,
53 size_t *tolen
, CHAR_T
**dst
)
56 CHAR_T
**tostr
= &cw
->bp1
.wc
;
57 size_t *blen
= &cw
->blen1
;
59 BINC_RETW(NULL
, *tostr
, *blen
, len
);
62 for (i
= 0; i
< len
; ++i
)
63 (*tostr
)[i
] = (u_char
) str
[i
];
70 #define CONV_BUFFER_SIZE 512
71 /* fill the buffer with codeset encoding of string pointed to by str
72 * left has the number of bytes left in str and is adjusted
73 * len contains the number of bytes put in the buffer
76 #define CONVERT(str, left, src, len) \
80 outleft = CONV_BUFFER_SIZE; \
82 if (iconv(id, (iconv_src_t)&str, &left, &bp, &outleft) == -1 && \
85 if ((len = CONV_BUFFER_SIZE - outleft) == 0) { \
94 if (id != (iconv_t)-1) \
95 iconv(id, NULL, NULL, NULL, NULL); \
98 #define CONVERT(str, left, src, len)
103 default_char2int(SCR
*sp
, const char * str
, ssize_t len
, CONVWIN
*cw
,
104 size_t *tolen
, CHAR_T
**dst
, iconv_t id
)
107 CHAR_T
**tostr
= &cw
->bp1
.wc
;
108 size_t *blen
= &cw
->blen1
;
112 char *src
= (char *)str
;
114 char buffer
[CONV_BUFFER_SIZE
];
120 BINC_RETW(NULL
, *tostr
, *blen
, nlen
);
123 if (id
!= (iconv_t
)-1)
124 CONVERT(str
, left
, src
, len
);
127 for (i
= 0, j
= 0; j
< len
; ) {
128 n
= mbrtowc((*tostr
)+i
, src
+j
, len
-j
, &mbs
);
129 /* NULL character converted */
130 if (n
== -2) error
= -(len
-j
);
131 if (n
== -1 || n
== -2) goto err
;
136 BINC_RETW(NULL
, *tostr
, *blen
, nlen
);
138 if (id
!= (iconv_t
)-1 && j
== len
&& left
) {
139 CONVERT(str
, left
, src
, len
);
154 fe_char2int(SCR
*sp
, const char * str
, ssize_t len
, CONVWIN
*cw
,
155 size_t *tolen
, CHAR_T
**dst
)
157 return default_char2int(sp
, str
, len
, cw
, tolen
, dst
,
158 sp
->conv
.id
[IC_FE_CHAR2INT
]);
162 ie_char2int(SCR
*sp
, const char * str
, ssize_t len
, CONVWIN
*cw
,
163 size_t *tolen
, CHAR_T
**dst
)
165 return default_char2int(sp
, str
, len
, cw
, tolen
, dst
,
166 sp
->conv
.id
[IC_IE_CHAR2INT
]);
170 cs_char2int(SCR
*sp
, const char * str
, ssize_t len
, CONVWIN
*cw
,
171 size_t *tolen
, CHAR_T
**dst
)
173 return default_char2int(sp
, str
, len
, cw
, tolen
, dst
,
178 int2raw(SCR
*sp
, const CHAR_T
* str
, ssize_t len
, CONVWIN
*cw
,
179 size_t *tolen
, char **dst
)
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, nlen); \
231 ret = iconv(id, (iconv_src_t)&bp, lenp, &obp, &outleft); \
232 if (ret == -1 && errno != E2BIG) \
234 offset = cw->blen1 - outleft; \
235 } while (ret != 0); \
238 #define CONVERT2(_buffer, lenp, cw, offset)
243 BINC_RETC(NULL
, *tostr
, *blen
, nlen
);
244 dst
= *tostr
; buflen
= *blen
;
247 if (id
!= (iconv_t
)-1) {
248 dst
= buffer
; buflen
= CONV_BUFFER_SIZE
;
252 for (i
= 0, j
= 0; i
< len
; ++i
) {
253 n
= wcrtomb(dst
+j
, str
[i
], &mbs
);
254 if (n
== -1) goto err
;
256 if (buflen
< j
+ MB_CUR_MAX
) {
257 if (id
!= (iconv_t
)-1) {
258 CONVERT2(buffer
, &j
, cw
, offset
);
261 BINC_RETC(NULL
, *tostr
, *blen
, nlen
);
262 dst
= *tostr
; buflen
= *blen
;
267 n
= wcrtomb(dst
+j
, L
'\0', &mbs
);
268 j
+= n
- 1; /* don't count NUL at the end */
271 if (id
!= (iconv_t
)-1) {
272 CONVERT2(buffer
, &j
, cw
, offset
);
273 CONVERT2(NULL
, NULL
, cw
, offset
); /* back to the initial state */
288 fe_int2char(SCR
*sp
, const CHAR_T
* str
, ssize_t len
, CONVWIN
*cw
,
289 size_t *tolen
, char **dst
)
291 return default_int2char(sp
, str
, len
, cw
, tolen
, dst
,
292 sp
->conv
.id
[IC_FE_INT2CHAR
]);
296 cs_int2char(SCR
*sp
, const CHAR_T
* str
, ssize_t len
, CONVWIN
*cw
,
297 size_t *tolen
, char **dst
)
299 return default_int2char(sp
, str
, len
, cw
, tolen
, dst
,
307 * Initialize the iconv environment.
309 * PUBLIC: void conv_init __P((SCR *, SCR *));
312 conv_init(SCR
*orig
, SCR
*sp
)
317 setlocale(LC_ALL
, "");
319 BCOPY(&orig
->conv
, &sp
->conv
, 1);
322 char *ctype
= setlocale(LC_CTYPE
, NULL
);
326 * This hack fixes the libncursesw issue on FreeBSD.
328 if (!strcmp(ctype
, "ko_KR.CP949"))
329 setlocale(LC_CTYPE
, "ko_KR.eucKR");
330 else if (!strcmp(ctype
, "zh_CN.GB2312"))
331 setlocale(LC_CTYPE
, "zh_CN.eucCN");
332 else if (!strcmp(ctype
, "zh_CN.GBK"))
333 setlocale(LC_CTYPE
, "zh_CN.GB18030");
336 * Switch to 8bit mode if locale is C;
337 * LC_CTYPE should be reseted to C if unmatched.
339 if (!strcmp(ctype
, "C") || !strcmp(ctype
, "POSIX")) {
340 sp
->conv
.sys2int
= sp
->conv
.file2int
= raw2int
;
341 sp
->conv
.int2sys
= sp
->conv
.int2file
= int2raw
;
342 sp
->conv
.input2int
= raw2int
;
344 sp
->conv
.sys2int
= cs_char2int
;
345 sp
->conv
.int2sys
= cs_int2char
;
346 sp
->conv
.file2int
= fe_char2int
;
347 sp
->conv
.int2file
= fe_int2char
;
348 sp
->conv
.input2int
= ie_char2int
;
351 o_set(sp
, O_INPUTENCODING
, OS_STRDUP
, codeset(), 0);
356 /* iconv descriptors must be distinct to screens. */
357 for (i
= 0; i
<= IC_IE_TO_UTF16
; ++i
)
358 sp
->conv
.id
[i
] = (iconv_t
)-1;
360 conv_enc(sp
, O_INPUTENCODING
, 0);
366 * Convert file/input encoding.
368 * PUBLIC: int conv_enc __P((SCR *, int, char *));
371 conv_enc(SCR
*sp
, int option
, char *enc
)
373 #if defined(USE_WIDECHAR) && defined(USE_ICONV)
378 c2w
= sp
->conv
.id
+ IC_FE_CHAR2INT
;
379 w2c
= sp
->conv
.id
+ IC_FE_INT2CHAR
;
380 if (!enc
) enc
= O_STR(sp
, O_FILEENCODING
);
381 if (*c2w
!= (iconv_t
)-1)
383 if (*w2c
!= (iconv_t
)-1)
385 if (strcasecmp(codeset(), enc
)) {
386 if ((*c2w
= iconv_open(codeset(), enc
)) == (iconv_t
)-1)
388 if ((*w2c
= iconv_open(enc
, codeset())) == (iconv_t
)-1)
390 } else *c2w
= *w2c
= (iconv_t
)-1;
392 case O_INPUTENCODING
:
393 c2w
= sp
->conv
.id
+ IC_IE_CHAR2INT
;
394 w2c
= sp
->conv
.id
+ IC_IE_TO_UTF16
;
395 if (!enc
) enc
= O_STR(sp
, O_INPUTENCODING
);
396 if (*c2w
!= (iconv_t
)-1)
398 if (*w2c
!= (iconv_t
)-1)
400 if (strcasecmp(codeset(), enc
)) {
401 if ((*c2w
= iconv_open(codeset(), enc
)) == (iconv_t
)-1)
403 } else *c2w
= (iconv_t
)-1;
404 /* UTF-16 can not be locale and can not be inputed. */
405 if ((*w2c
= iconv_open("utf-16be", enc
)) == (iconv_t
)-1)
410 F_CLR(sp
, SC_CONV_ERROR
);
411 F_SET(sp
, SC_SCR_REFORMAT
);
419 "321|File encoding conversion not supported");
421 case O_INPUTENCODING
:
423 "322|Input encoding conversion not supported");
431 * Close the iconv descriptors, release the buffer.
433 * PUBLIC: void conv_end __P((SCR *));
438 #if defined(USE_WIDECHAR) && defined(USE_ICONV)
440 for (i
= 0; i
<= IC_IE_TO_UTF16
; ++i
)
441 if (sp
->conv
.id
[i
] != (iconv_t
)-1)
442 iconv_close(sp
->conv
.id
[i
]);
443 if (sp
->cw
.bp1
.c
!= NULL
)