Commit MetaInfo

Revision65d479b9095d8585da4774b97ec15aab26023c1d (tree)
Time2018-01-22 03:42:22
AuthorSHIRAKATA Kentaro <argrath@ub32...>
CommiterSHIRAKATA Kentaro

Log Message

fix #37910

Change Summary

Incremental Difference

--- a/ChangeLog.j
+++ b/ChangeLog.j
@@ -1,4 +1,5 @@
11 * ISO-2022-JP対応を削除
2+ * UTF-8出力に対応 (#37910)
23
34 Fri Feb 17 2017 Kentaro Shirakata <argrath@ub32.org>
45
--- a/include/unixconf.h
+++ b/include/unixconf.h
@@ -64,6 +64,7 @@
6464 * For example, platforms using the GNU libraries,
6565 * Linux, Solaris 2.x
6666 */
67+#define POSIX_ICONV /* use POSIX iconv functions */
6768
6869 /* #define OPENWINBUG */ /* avoid a problem using OpenWindows 3.0 for
6970 X11 on SunOS 4.1.x, x>= 2. Do not define
--- a/japanese/jlib.c
+++ b/japanese/jlib.c
@@ -10,6 +10,9 @@
1010 #include <stdio.h>
1111 #include <ctype.h>
1212 #include "hack.h"
13+#ifdef POSIX_ICONV
14+#include <iconv.h>
15+#endif
1316
1417 int xputc(CHAR_P);
1518 int xputc2(int, int);
@@ -17,6 +20,20 @@ int xputc2(int, int);
1720
1821 #define EUC 0
1922 #define SJIS 1
23+#define UTF8 2
24+
25+#ifdef POSIX_ICONV
26+static char* ccode[]={
27+ "EUC-JP-MS",
28+ "CP932",
29+ "UTF-8"
30+};
31+static char* ccode_alt[]={
32+ "EUC-JP",
33+ "ShiftJIS",
34+ "UTF-8"
35+};
36+#endif
2037
2138 /* internal kcode */
2239 /* IC=0 EUC */
@@ -47,6 +64,10 @@ int xputc2(int, int);
4764
4865 static int output_kcode = OUTPUT_KCODE;
4966 static int input_kcode = INPUT_KCODE;
67+#ifdef POSIX_ICONV
68+static iconv_t output_dsc = 0;
69+static iconv_t input_dsc = 0;
70+#endif
5071
5172 /*
5273 ** Kanji code library....
@@ -74,6 +95,8 @@ setkcode(c)
7495 output_kcode = EUC;
7596 else if(c == 'S' || c == 's')
7697 output_kcode = SJIS;
98+ else if(c == 'U' || c == 'u')
99+ output_kcode = UTF8;
77100 else if(c == 'I' || c == 'i')
78101 #ifdef MSDOS
79102 output_kcode = SJIS;
@@ -84,6 +107,27 @@ setkcode(c)
84107 output_kcode = IC;
85108 }
86109 input_kcode = output_kcode;
110+
111+#ifdef POSIX_ICONV
112+ if (output_dsc)
113+ iconv_close(output_dsc);
114+ output_dsc = iconv_open(ccode[output_kcode], ccode[IC]);
115+ if ((size_t)output_dsc == -1)
116+ output_dsc = iconv_open(ccode_alt[output_kcode], ccode[IC]);
117+ if ((size_t)output_dsc == -1)
118+ output_dsc = iconv_open(ccode[output_kcode], ccode_alt[IC]);
119+ if ((size_t)output_dsc == -1)
120+ output_dsc = iconv_open(ccode_alt[output_kcode], ccode_alt[IC]);
121+ if(input_dsc)
122+ iconv_close(input_dsc);
123+ input_dsc = iconv_open(ccode[IC] ,ccode[input_kcode]);
124+ if ((size_t)input_dsc == -1)
125+ input_dsc = iconv_open(ccode_alt[IC] ,ccode[input_kcode]);
126+ if ((size_t)input_dsc == -1)
127+ input_dsc = iconv_open(ccode[IC] ,ccode_alt[input_kcode]);
128+ if ((size_t)input_dsc == -1)
129+ input_dsc = iconv_open(ccode_alt[IC] ,ccode_alt[input_kcode]);
130+#endif
87131 }
88132 /*
89133 ** EUC->SJIS
@@ -150,6 +194,17 @@ str2ic(s)
150194 }
151195
152196 p = buf;
197+#ifdef POSIX_ICONV
198+ if (input_dsc) {
199+ size_t src_len, dst_len;
200+ up = s;
201+ src_len = strlen(s);
202+ dst_len = sizeof(buf);
203+ if (iconv(input_dsc, (char**)&up, &src_len,
204+ (char**)&p, &dst_len) == (size_t)-1)
205+ goto noconvert;
206+ }
207+#else
153208 if( IC==EUC && input_kcode == SJIS ){
154209 while(*s){
155210 up = s;
@@ -163,7 +218,9 @@ str2ic(s)
163218 *(p++) = (unsigned char)*(s++);
164219 }
165220 }
221+#endif
166222 else{
223+noconvert:
167224 strcpy((char *)buf, s);
168225 return (char *)buf;
169226 }
@@ -172,7 +229,6 @@ str2ic(s)
172229 return (char *)buf;
173230 }
174231
175-#ifdef SJIS_FILESYSTEM
176232 /*
177233 ** translate string to output kcode
178234 */
@@ -190,6 +246,17 @@ ic2str(s)
190246 buf[0] = '\0';
191247
192248 p = buf;
249+#ifdef POSIX_ICONV
250+ if(output_dsc){
251+ size_t src_len, dst_len;
252+ up = s;
253+ src_len = strlen(s);
254+ dst_len = sizeof(buf);
255+ if(iconv(output_dsc, (char**)&up, &src_len,
256+ (char**)&p, &dst_len) == (size_t)-1)
257+ goto noconvert;
258+ }
259+#else
193260 if( IC==EUC && output_kcode == SJIS ){
194261 while(*s){
195262 up = s;
@@ -203,7 +270,9 @@ ic2str(s)
203270 *(p++) = (unsigned char)*(s++);
204271 }
205272 }
273+#endif
206274 else{
275+noconvert:
207276 strcpy((char *)buf, s);
208277 return (char *)buf;
209278 }
@@ -211,7 +280,6 @@ ic2str(s)
211280 *(p++) = '\0';
212281 return (char *)buf;
213282 }
214-#endif /* MSDOS */
215283
216284 /*
217285 ** primitive function
@@ -295,6 +363,24 @@ jbuffer(
295363 c2 = c;
296364
297365 if(IC == output_kcode)
366+#ifdef POSIX_ICONV
367+ f2(c1, c2);
368+ else if (output_dsc) {
369+ char buf_in[2], buf_out[16];
370+ char *src = buf_in, *dst=buf_out;
371+ size_t src_len = 2, dst_len = sizeof(buf_out);
372+ *buf_in = c1;
373+ *(buf_in + 1) = c2;
374+ if (iconv(output_dsc, &src,
375+ &src_len, &dst, &dst_len) == (size_t)-1) {
376+ f2(c1, c2);
377+ } else {
378+ *dst = '\0';
379+ dst = buf_out;
380+ while(*dst) f1(*(dst++));
381+ }
382+ }
383+#else
298384 ;
299385 else if(IC == EUC){
300386 switch(output_kcode){
@@ -323,6 +409,7 @@ jbuffer(
323409 }
324410 }
325411 f2(c1, c2);
412+#endif
326413 buf[0] = 0;
327414 return 2;
328415 }
@@ -350,6 +437,15 @@ cbuffer(
350437 if(!f1) f1 = tty_cputc;
351438 if(!f2) f2 = tty_cputc2;
352439
440+#ifdef POSIX_ICONV
441+ if (output_kcode == UTF8) {
442+ if (c) {
443+ f1(c);
444+ return 1;
445+ }
446+ }
447+ else
448+#endif
353449 if(!(buf[0]) && is_kanji(c)){
354450 buf[1] = c;
355451 ++buf[0];
@@ -370,14 +466,12 @@ cbuffer(
370466 void
371467 jputchar(int c)
372468 {
373- static unsigned int buf[2];
374- jbuffer((unsigned int)(c & 0xff), buf, NULL, NULL);
469+ jbuffer((unsigned int)(c & 0xff), NULL, NULL, NULL);
375470 }
376471 void
377472 cputchar(int c)
378473 {
379- static unsigned int buf[2];
380- cbuffer((unsigned int)(c & 0xff), buf, NULL, NULL);
474+ cbuffer((unsigned int)(c & 0xff), NULL, NULL, NULL);
381475 }
382476
383477 void
Show on old repository browser