Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
NetHack 3.4.3 wide character patch
This patch translates NetHack 3.4.3's CP437 output into C wide characters
with Unicode code points. The current locale (typically a flavor of UTF-8)
is used to properly render IBMgraphics in unix terminals. Input is not
translated and has the original limitations.
This patch is compatible with Menucolors.
--- a/sys/unix/unixmain.c
+++ b/sys/unix/unixmain.c
@@ -14,6 +14,9 @@
#include <fcntl.h>
+#include <wchar.h>
+#include <locale.h>
#if !defined(_BULL_SOURCE) && !defined(__sgi) && !defined(_M_UNIX)
# if !defined(SUNOS4) && !(defined(ULTRIX) && defined(__GNUC__))
# if defined(POSIX_TYPES) || defined(SVR4) || defined(HPUX)
@@ -55,6 +58,9 @@
boolean exact_username;
+ /* Prepare standard I/O for wide characters. */
+ setlocale(LC_ALL, "");
#if defined(__APPLE__)
/* special hack to change working directory to a resource fork when
running from finder --sam */
@@ -538,4 +544,59 @@
+ * Translate CP437 to Unicode characters and write as a wide character.
+ */
+int c;
+ static const wchar_t translate[] = {
+ 0x0000U, 0x0001U, 0x0002U, 0x0003U, 0x0004U, 0x0005U,
+ 0x0006U, 0x0007U, 0x0008U, 0x0009U, 0x000aU, 0x000bU,
+ 0x000cU, 0x000dU, 0x000eU, 0x000fU, 0x0010U, 0x0011U,
+ 0x0012U, 0x0013U, 0x0014U, 0x0015U, 0x0016U, 0x0017U,
+ 0x0018U, 0x0019U, 0x001aU, 0x001bU, 0x001cU, 0x001dU,
+ 0x001eU, 0x001fU, 0x0020U, 0x0021U, 0x0022U, 0x0023U,
+ 0x0024U, 0x0025U, 0x0026U, 0x0027U, 0x0028U, 0x0029U,
+ 0x002aU, 0x002bU, 0x002cU, 0x002dU, 0x002eU, 0x002fU,
+ 0x0030U, 0x0031U, 0x0032U, 0x0033U, 0x0034U, 0x0035U,
+ 0x0036U, 0x0037U, 0x0038U, 0x0039U, 0x003aU, 0x003bU,
+ 0x003cU, 0x003dU, 0x003eU, 0x003fU, 0x0040U, 0x0041U,
+ 0x0042U, 0x0043U, 0x0044U, 0x0045U, 0x0046U, 0x0047U,
+ 0x0048U, 0x0049U, 0x004aU, 0x004bU, 0x004cU, 0x004dU,
+ 0x004eU, 0x004fU, 0x0050U, 0x0051U, 0x0052U, 0x0053U,
+ 0x0054U, 0x0055U, 0x0056U, 0x0057U, 0x0058U, 0x0059U,
+ 0x005aU, 0x005bU, 0x005cU, 0x005dU, 0x005eU, 0x005fU,
+ 0x0060U, 0x0061U, 0x0062U, 0x0063U, 0x0064U, 0x0065U,
+ 0x0066U, 0x0067U, 0x0068U, 0x0069U, 0x006aU, 0x006bU,
+ 0x006cU, 0x006dU, 0x006eU, 0x006fU, 0x0070U, 0x0071U,
+ 0x0072U, 0x0073U, 0x0074U, 0x0075U, 0x0076U, 0x0077U,
+ 0x0078U, 0x0079U, 0x007aU, 0x007bU, 0x007cU, 0x007dU,
+ 0x007eU, 0x007fU, 0x00c7U, 0x00fcU, 0x00e9U, 0x00e2U,
+ 0x00e4U, 0x00e0U, 0x00e5U, 0x00e7U, 0x00eaU, 0x00ebU,
+ 0x00e8U, 0x00efU, 0x00eeU, 0x00ecU, 0x00c4U, 0x00c5U,
+ 0x00c9U, 0x00e6U, 0x00c6U, 0x00f4U, 0x00f6U, 0x00f2U,
+ 0x00fbU, 0x00f9U, 0x00ffU, 0x00d6U, 0x00dcU, 0x00a2U,
+ 0x00a3U, 0x00a5U, 0x20a7U, 0x0192U, 0x00e1U, 0x00edU,
+ 0x00f3U, 0x00faU, 0x00f1U, 0x00d1U, 0x00aaU, 0x00baU,
+ 0x00bfU, 0x2310U, 0x00acU, 0x00bdU, 0x00bcU, 0x00a1U,
+ 0x00abU, 0x00bbU, 0x2591U, 0x2592U, 0x2593U, 0x2502U,
+ 0x2524U, 0x2561U, 0x2562U, 0x2556U, 0x2555U, 0x2563U,
+ 0x2551U, 0x2557U, 0x255dU, 0x255cU, 0x255bU, 0x2510U,
+ 0x2514U, 0x2534U, 0x252cU, 0x251cU, 0x2500U, 0x253cU,
+ 0x255eU, 0x255fU, 0x255aU, 0x2554U, 0x2569U, 0x2566U,
+ 0x2560U, 0x2550U, 0x256cU, 0x2567U, 0x2568U, 0x2564U,
+ 0x2565U, 0x2559U, 0x2558U, 0x2552U, 0x2553U, 0x256bU,
+ 0x256aU, 0x2518U, 0x250cU, 0x2588U, 0x2584U, 0x258cU,
+ 0x2590U, 0x2580U, 0x03b1U, 0x00dfU, 0x0393U, 0x03c0U,
+ 0x03a3U, 0x03c3U, 0x00b5U, 0x03c4U, 0x03a6U, 0x0398U,
+ 0x03a9U, 0x03b4U, 0x221eU, 0x03c6U, 0x03b5U, 0x2229U,
+ 0x2261U, 0x00b1U, 0x2265U, 0x2264U, 0x2320U, 0x2321U,
+ 0x00f7U, 0x2248U, 0x00b0U, 0x2219U, 0x00b7U, 0x221aU,
+ 0x207fU, 0x00b2U, 0x25a0U, 0x00a0U
+ };
+ return (int)fputwc(translate[c & 0xffU], stdout);
--- a/include/unixconf.h
+++ b/include/unixconf.h
@@ -347,5 +347,12 @@
#endif /* LINUX */
#endif /* GNOME_GRAPHICS */
+/* Redirect output through a wide character function. */
+int wide_putchar(int);
+#ifdef putchar
+# undef putchar
+#define putchar(c) wide_putchar(c)
#endif /* UNIXCONF_H */
#endif /* UNIX */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment