Skip to content

Instantly share code, notes, and snippets.

@amuramatsu
Last active January 29, 2019 11:13
Show Gist options
  • Save amuramatsu/014dd7c0bc0dc2fb1c27d7cf0e13e7cc to your computer and use it in GitHub Desktop.
Save amuramatsu/014dd7c0bc0dc2fb1c27d7cf0e13e7cc to your computer and use it in GitHub Desktop.
diff -ruN nethack-3.6.1.orig/include/extern.h nethack-3.6.1/include/extern.h
--- nethack-3.6.1.orig/include/extern.h 2019-01-04 21:24:10.000000000 +0900
+++ nethack-3.6.1/include/extern.h 2019-01-04 21:53:50.000000000 +0900
@@ -2908,12 +2908,12 @@
E unsigned char *FDECL(e2sj, (unsigned char *));
E unsigned char *FDECL(sj2e, (unsigned char *));
E const char *FDECL(str2ic, (const char *));
-#ifdef SJIS_FILESYSTEM
E const char *FDECL(ic2str, (const char *));
-#endif
+E const char *FDECL(gc2str, (const char *));
E int FDECL(jbuffer, (unsigned int, unsigned int *, void (*)(unsigned int), void (*)(unsigned int, unsigned int)));
E int FDECL(cbuffer, (unsigned int, unsigned int *, void (*)(unsigned int), void (*)(unsigned int, unsigned int)));
E void FDECL(cputchar,(int));
+E void FDECL(gputchar,(int));
E void FDECL(jputchar,(int));
E void FDECL(jputs,(const char *));
E int FDECL(is_kanji2, (const char *,int));
diff -ruN nethack-3.6.1.orig/japanese/jlib.c nethack-3.6.1/japanese/jlib.c
--- nethack-3.6.1.orig/japanese/jlib.c 2019-01-04 21:24:10.000000000 +0900
+++ nethack-3.6.1/japanese/jlib.c 2019-01-04 21:54:50.000000000 +0900
@@ -6,7 +6,9 @@
#include <stdio.h>
#include <ctype.h>
#include "hack.h"
-#ifdef POSIX_ICONV
+#if defined(ICU)
+#include <unicode/ucnv.h>
+#elif defined(POSIX_ICONV)
#include <iconv.h>
#endif
@@ -18,7 +20,13 @@
#define SJIS 1
#define UTF8 2
-#ifdef POSIX_ICONV
+#if defined(ICU)
+static char* ccode[]={
+ "euc-jp-2007",
+ "windows-31j",
+ "utf-8"
+};
+#elif defined(POSIX_ICONV)
static const char* ccode[]={
"EUC-JP-MS",
"CP932",
@@ -60,9 +68,13 @@
static int output_kcode = OUTPUT_KCODE;
static int input_kcode = INPUT_KCODE;
-#ifdef POSIX_ICONV
-static iconv_t output_dsc = 0;
-static iconv_t input_dsc = 0;
+#if defined(ICU)
+static UConverter *output_dsc;
+static UConverter *input_dsc;
+static UConverter *internal_dsc;
+#elif defined(POSIX_ICONV)
+static iconv_t output_dsc = (iconv_t)-1;
+static iconv_t input_dsc = (iconv_t)-1;
#endif
/*
@@ -104,8 +116,21 @@
}
input_kcode = output_kcode;
-#ifdef POSIX_ICONV
- if (output_dsc)
+#if defined(ICU)
+ {
+ UErrorCode status;
+ if (output_dsc)
+ ucnv_close(output_dsc);
+ output_dsc = ucnv_open(ccode[output_kcode], &status);
+ if (input_dsc)
+ ucnv_close(input_dsc);
+ input_dsc = ucnv_open(ccode[input_kcode], &status);
+ if (internal_dsc)
+ ucnv_close(internal_dsc);
+ internal_dsc = ucnv_open(ccode[IC], &status);
+ }
+#elif defined(POSIX_ICONV)
+ if (output_dsc != (iconv_t)-1)
iconv_close(output_dsc);
output_dsc = iconv_open(ccode[output_kcode], ccode[IC]);
if (output_dsc == (iconv_t)-1)
@@ -114,7 +139,7 @@
output_dsc = iconv_open(ccode[output_kcode], ccode_alt[IC]);
if (output_dsc == (iconv_t)-1)
output_dsc = iconv_open(ccode_alt[output_kcode], ccode_alt[IC]);
- if(input_dsc)
+ if (input_dsc != (iconv_t)-1)
iconv_close(input_dsc);
input_dsc = iconv_open(ccode[IC] ,ccode[input_kcode]);
if (input_dsc == (iconv_t)-1)
@@ -193,8 +218,23 @@
}
p = buf;
-#ifdef POSIX_ICONV
- if (input_dsc) {
+#if defined(ICU)
+ if (internal_dsc && input_dsc) {
+ UErrorCode status = 0;
+ size_t src_len, dst_len;
+ up = s;
+ src_len = strlen(s);
+ dst_len = sizeof(buf);
+ ucnv_convertEx(internal_dsc, input_dsc,
+ (char**)&p, p+dst_len,
+ (char**)&up, up+src_len,
+ NULL, NULL, NULL, NULL, TRUE, TRUE,
+ &status);
+ if (U_FAILURE(status))
+ goto noconvert;
+ }
+#elif defined(POSIX_ICONV)
+ if (input_dsc != (iconv_t)-1) {
size_t src_len, dst_len;
up = (unsigned char *)s;
src_len = strlen(s);
@@ -250,8 +290,23 @@
buf[0] = '\0';
p = buf;
-#ifdef POSIX_ICONV
- if(output_dsc){
+#if defined(ICU)
+ if(internal_dsc && output_dsc){
+ UErrorCode status = 0;
+ size_t src_len, dst_len;
+ up = s;
+ src_len = strlen(s);
+ dst_len = sizeof(buf);
+ ucnv_convertEx(output_dsc, internal_dsc,
+ (char**)&p, p+dst_len,
+ (char**)&up, up+src_len,
+ NULL, NULL, NULL, NULL, TRUE, TRUE,
+ &status);
+ if (U_FAILURE(status))
+ goto noconvert;
+ }
+#elif defined(POSIX_ICONV)
+ if(output_dsc != (iconv_t)-1){
size_t src_len, dst_len;
up = (unsigned char *)s;
src_len = strlen(s);
@@ -287,6 +342,99 @@
return (char *)buf;
}
+/* CP437 to Unicode mapping according to the Unicode Consortium */
+static unsigned short cp437[] =
+{
+ 0x0020, 0x263A, 0x263B, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022,
+ 0x25D8, 0x25CB, 0x25D9, 0x2642, 0x2640, 0x266A, 0x266B, 0x263C,
+ 0x25BA, 0x25C4, 0x2195, 0x203C, 0x00B6, 0x00A7, 0x25AC, 0x21A8,
+ 0x2191, 0x2193, 0x2192, 0x2190, 0x221F, 0x2194, 0x25B2, 0x25BC,
+ 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
+ 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x002f,
+ 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
+ 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f,
+ 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
+ 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f,
+ 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
+ 0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f,
+ 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
+ 0x0068, 0x0069, 0x006a, 0x006b, 0x006c, 0x006d, 0x006e, 0x006f,
+ 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
+ 0x0078, 0x0079, 0x007a, 0x007b, 0x007c, 0x007d, 0x007e, 0x2302,
+ 0x00c7, 0x00fc, 0x00e9, 0x00e2, 0x00e4, 0x00e0, 0x00e5, 0x00e7,
+ 0x00ea, 0x00eb, 0x00e8, 0x00ef, 0x00ee, 0x00ec, 0x00c4, 0x00c5,
+ 0x00c9, 0x00e6, 0x00c6, 0x00f4, 0x00f6, 0x00f2, 0x00fb, 0x00f9,
+ 0x00ff, 0x00d6, 0x00dc, 0x00a2, 0x00a3, 0x00a5, 0x20a7, 0x0192,
+ 0x00e1, 0x00ed, 0x00f3, 0x00fa, 0x00f1, 0x00d1, 0x00aa, 0x00ba,
+ 0x00bf, 0x2310, 0x00ac, 0x00bd, 0x00bc, 0x00a1, 0x00ab, 0x00bb,
+ 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556,
+ 0x2555, 0x2563, 0x2551, 0x2557, 0x255d, 0x255c, 0x255b, 0x2510,
+ 0x2514, 0x2534, 0x252c, 0x251c, 0x2500, 0x253c, 0x255e, 0x255f,
+ 0x255a, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256c, 0x2567,
+ 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256b,
+ 0x256a, 0x2518, 0x250c, 0x2588, 0x2584, 0x258c, 0x2590, 0x2580,
+ 0x03b1, 0x00df, 0x0393, 0x03c0, 0x03a3, 0x03c3, 0x00b5, 0x03c4,
+ 0x03a6, 0x0398, 0x03a9, 0x03b4, 0x221e, 0x03c6, 0x03b5, 0x2229,
+ 0x2261, 0x00b1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00f7, 0x2248,
+ 0x00b0, 0x2219, 0x00b7, 0x221a, 0x207f, 0x00b2, 0x25a0, 0x00a0
+};
+
+static char *
+glyph_to_utf8str(int c, char *buf)
+{
+ unsigned short code;
+
+ /* Convert selected code to UTF-8 */
+ if (SYMHANDLING(H_IBM))
+ code = cp437[c & 0xff];
+ else
+ code = c & 0xff; /* iso8859-1 charactor */
+
+ /* iconv cannot convert controll chars, they are converted hear */
+ if (code < 0x80) {
+ *(buf++) = (char)code;
+ } else if (code < 0x0800) {
+ *(buf++) = (char)(0xC0 | ((code>> 6) & 0x1F));
+ *(buf++) = (char)(0x80 | ( code & 0x3F));
+ } else {
+ *(buf++) = (char)(0xE0 | ((code>>12) & 0x1F));
+ *(buf++) = (char)(0x80 | ((code>> 6) & 0x3F));
+ *(buf++) = (char)(0x80 | ( code & 0x3F));
+ }
+ *buf = '\0';
+ return buf;
+}
+
+/*
+** translate glyph code (cp437) string to output kcode
+*/
+const char *
+gc2str(const char *s)
+{
+ static unsigned char buf[1024];
+ const unsigned char *up;
+ unsigned char *p;
+
+ if(!s)
+ return s;
+
+ up = s;
+ p = buf;
+ if(output_kcode==UTF8){
+ while(*up){
+ p = glyph_to_utf8str(*(up++), p);
+ }
+ }
+ else{
+notsupport:
+ strcpy((char *)buf, s);
+ return (char *)buf;
+ }
+
+ *(p++) = '\0';
+ return (char *)buf;
+}
+
/*
** primitive function
*/
@@ -371,9 +519,24 @@
c2 = c;
if(IC == output_kcode)
-#ifdef POSIX_ICONV
+#if defined(ICU) || defined(POSIX_ICONV)
f2(c1, c2);
- else if (output_dsc) {
+# if defined(ICU)
+ else if (internal_dsc && output_dsc) {
+ UErrorCode status = 0;
+ char buf_in[2], buf_out[16];
+ char *src = buf_in, *dst=buf_out;
+ size_t src_len = 2, dst_len = sizeof(buf_out);
+ *buf_in = c1;
+ *(buf_in + 1) = c2;
+ ucnv_convertEx(output_dsc, internal_dsc,
+ &dst, dst+dst_len,
+ &src, src+src_len,
+ NULL, NULL, NULL, NULL, TRUE, TRUE,
+ &status);
+ if (U_FAILURE(status)) {
+# elif defined(POSIX_ICONV)
+ else if (output_dsc != (iconv_t)-1) {
char buf_in[2], buf_out[16];
char *src = buf_in, *dst=buf_out;
size_t src_len = 2, dst_len = sizeof(buf_out);
@@ -381,6 +544,7 @@
*(buf_in + 1) = c2;
if (iconv(output_dsc, &src,
&src_len, &dst, &dst_len) == (size_t)-1) {
+# endif
f2(c1, c2);
} else {
*dst = '\0';
@@ -445,7 +609,7 @@
if(!f1) f1 = tty_cputc;
if(!f2) f2 = tty_cputc2;
-#ifdef POSIX_ICONV
+#if defined(ICU) || defined(POSIX_ICONV)
if (output_kcode == UTF8) {
if (c) {
f1(c);
@@ -482,6 +646,21 @@
cbuffer((unsigned int)(c & 0xff), NULL, NULL, NULL);
}
+/* print out glyph (cp437) character to tty */
+void
+gputchar(int c)
+{
+ char buf[6];
+ char *str = buf;
+
+ if (output_kcode == UTF8) {
+ glyph_to_utf8str(c, buf);
+ while(*str) tty_cputc(*(str++));
+ } else {
+ tty_cputc((unsigned int)(c&0xFF));
+ }
+}
+
void
jputs(s)
const char *s;
diff -ruN nethack-3.6.1.orig/sys/unix/unixmain.c nethack-3.6.1/sys/unix/unixmain.c
--- nethack-3.6.1.orig/sys/unix/unixmain.c 2019-01-04 21:24:10.000000000 +0900
+++ nethack-3.6.1/sys/unix/unixmain.c 2019-01-04 21:53:50.000000000 +0900
@@ -18,6 +18,8 @@
#ifdef XI18N
#include <X11/Xlocale.h>
+#else
+#include <locale.h>
#endif
#if !defined(_BULL_SOURCE) && !defined(__sgi) && !defined(_M_UNIX)
@@ -64,7 +66,15 @@
sys_early_init();
-#ifdef XI18N
+#if 1 /*JP*/
+ {
+ char *locale, *dot;
+ locale = setlocale(LC_ALL, "");
+ dot = strchr(locale, '.');
+ if (dot) setkcode(*(dot+1));
+ setlocale(LC_ALL, "ja_JP.eucJP");
+ }
+#elif defined(XI18N)
setlocale(LC_ALL, "");
#endif
#if defined(__APPLE__)
@@ -103,7 +113,7 @@
hackpid = getpid();
(void) umask(0777 & ~FCMASK);
- choose_windows(DEFAULT_WINDOW_SYS);
+ choose_windows(getenv("DISPLAY") ? DEFAULT_WINDOW_SYS : "tty");
#ifdef CHDIR /* otherwise no chdir() */
/*
@@ -162,7 +172,6 @@
#if 0 /*JP*/
prscore(argc, argv);
#else
- setkcode('I');
initoptions();
prscore(argc, argv);
jputchar('\0'); /* reset */
diff -ruN nethack-3.6.1.orig/win/X11/winX.c nethack-3.6.1/win/X11/winX.c
--- nethack-3.6.1.orig/win/X11/winX.c 2019-01-04 21:24:10.000000000 +0900
+++ nethack-3.6.1/win/X11/winX.c 2019-01-04 21:53:50.000000000 +0900
@@ -168,6 +168,9 @@
static int FDECL(input_event, (int));
static void FDECL(win_visible, (Widget, XtPointer, XEvent *, Boolean *));
static void NDECL(init_standard_windows);
+#ifdef XI18N
+static String FDECL(lang_proc, (Display *,String, XtPointer));
+#endif
#ifdef INSTALLCOLORMAP
Colormap cmap;
@@ -1283,7 +1286,7 @@
XSetIOErrorHandler((XIOErrorHandler) hangup);
#ifdef XI18N
- XtSetLanguageProc(NULL, NULL, NULL);
+ XtSetLanguageProc(NULL, lang_proc, NULL);
#endif
#if 1 /*JP*/
XSetIOErrorHandler((XIOErrorHandler) hangup);
@@ -2657,4 +2660,18 @@
}
}
+#ifdef XI18N
+static String lang_proc (Display *did, String lid, XtPointer cdata)
+{
+ if (! XSupportsLocale()) {
+ XtWarning("Current locale is not supported\n");
+ setlocale(LC_ALL, "C");
+ }
+ if (XSetLocaleModifiers("") == NULL) {
+ XtWarning("Can't set locale modifiers\n");
+ }
+ return setlocale(LC_ALL, NULL);
+}
+#endif
+
/*winX.c*/
diff -ruN nethack-3.6.1.orig/win/tty/wintty.c nethack-3.6.1/win/tty/wintty.c
--- nethack-3.6.1.orig/win/tty/wintty.c 2019-01-04 21:24:10.000000000 +0900
+++ nethack-3.6.1/win/tty/wintty.c 2019-01-04 21:53:50.000000000 +0900
@@ -3366,7 +3366,7 @@
# if 0 /*JP*/
(void) putchar(ch);
# else
- (void) cputchar(ch);
+ (void) gputchar(ch);
# endif
} else if (ch & 0x80) {
if (!GFlag || HE_resets_AS) {
@@ -3386,7 +3386,7 @@
# if 0 /*JP*/
(void) putchar(ch);
# else
- (void) jputchar(ch);
+ (void) cputchar(ch);
# endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment