File tcsh-6.17.02-kanji.dif of Package tcsh

--- ed.inputl.c
+++ ed.inputl.c	2010-12-09 10:01:31.000000000 +0000
@@ -683,7 +683,7 @@ GetNextCommand(KEYCMD *cmdnum, Char *ch)
 #ifdef DSPMBYTE
 	     _enable_mbdisp &&
 #else
-	     MB_LEN_MAX == 1 &&
+	     MB_CUR_MAX == 1 &&
 #endif
 	     !adrof(STRnokanji) && (*ch & META)) {
 	    MetaNext = 0;
--- sh.c
+++ sh.c	2010-12-10 16:10:45.000000000 +0000
@@ -804,6 +804,18 @@ main(int argc, char **argv)
       nt_autoset_dspmbyte();
 #endif /* WINNT_NATIVE */
 #endif
+#if defined(KANJI)
+#if defined(WIDE_STRINGS) && defined(HAVE_NL_LANGINFO) && defined(CODESET)
+#if defined(NLS) && defined(LC_CTYPE)
+    if (setlocale(LC_CTYPE, NULL) != NULL || getenv("LANG") != NULL)
+#else
+    if (getenv("LANG") != NULL)
+#endif
+    {
+	autoset_kanji();
+    }
+#endif
+#endif
 
     fix_version();		/* publish the shell version */
 
--- sh.decls.h
+++ sh.decls.h	2010-12-07 10:09:50.000000000 +0000
@@ -392,6 +392,11 @@ extern	Char		 *unparse	(struct command *
 extern	void 		  update_dspmbyte_vars	(void);
 extern	void		  autoset_dspmbyte	(const Char *);
 #endif
+#if defined(KANJI)
+#if defined(WIDE_STRINGS) && defined(HAVE_NL_LANGINFO) && defined(CODESET)
+extern	void		  autoset_kanji	(void);
+#endif
+#endif
 
 /*
  * sh.time.c
--- sh.func.c
+++ sh.func.c	2010-12-09 09:15:06.000000000 +0000
@@ -1422,6 +1422,10 @@ dosetenv(Char **v, struct command *c)
 # endif
 # ifdef LC_CTYPE
 	(void) setlocale(LC_CTYPE, ""); /* for iscntrl */
+#  if defined(KANJI) && defined(WIDE_STRINGS) && defined(HAVE_NL_LANGINFO) && defined(CODESET)
+        autoset_kanji();
+#  endif
+
 # endif /* LC_CTYPE */
 # ifdef NLS_CATALOGS
 #  ifdef LC_MESSAGES
--- sh.set.c
+++ sh.set.c	2010-12-09 11:07:56.000000000 +0000
@@ -1098,7 +1098,8 @@ x:
     }
 }
 
-#if defined(KANJI) && defined(SHORT_STRINGS) && defined(DSPMBYTE)
+#if defined(KANJI)
+#if defined(SHORT_STRINGS) && defined(DSPMBYTE)
 extern int dspmbyte_ls;
 
 void
@@ -1273,4 +1274,26 @@ autoset_dspmbyte(const Char *pcp)
 	}
     }
 }
+#elif defined(WIDE_STRINGS) && defined(HAVE_NL_LANGINFO) && defined(CODESET)
+void
+autoset_kanji(void)
+{
+    char *codeset = nl_langinfo(CODESET);
+    
+    if (*codeset == '\0') {
+	if (adrof(STRnokanji) == NULL)
+	    setNS(STRnokanji);
+	return;
+    }
+
+    if (strcasestr(codeset, "SHIFT_JIS") == (char*)0) {
+	if (adrof(STRnokanji) == NULL)
+	    setNS(STRnokanji);
+	return;
+    }
+
+    if (adrof(STRnokanji) != NULL)
+	unsetv(STRnokanji);
+}
+#endif
 #endif
--- tc.str.c
+++ tc.str.c	2010-12-14 16:47:24.383925459 +0000
@@ -94,13 +94,31 @@ one_wctomb(char *s, Char wchar)
     return len;
 }
 
+#if defined(KANJI) && defined(WIDE_STRINGS) && defined(HAVE_NL_LANGINFO) && defined(CODESET)
+static mbstate_t mb_zero;
+#endif
+
 int
 rt_mbtowc(Char *pwc, const char *s, size_t n)
 {
     int ret;
     char back[MB_LEN_MAX];
     wchar_t tmp;
+#if defined(KANJI) && defined(WIDE_STRINGS) && defined(HAVE_NL_LANGINFO) && defined(CODESET)
+    static mbstate_t mb;
+
+    /*
+     * Workaround the Shift-JIS endcoding that translates unshifted 7 bit ASCII!
+     */
+    if (!adrof(STRnokanji) && n && pwc && s && (*s == '\\' || *s == '~') &&
+	!memcmp(&mb, &mb_zero, sizeof(mb)))
+    {
+	*pwc = *s;
+	return 1;
+    }
+#else
     mbstate_t mb;
+#endif
 
     memset (&mb, 0, sizeof mb);
     ret = mbrtowc(&tmp, s, n, &mb);