Hi,

for better support of the UTF-8 mode of the mksh I've done a small
patch which enables the mksh to change to UTF-8 mode even if the 
locale is changed at runtime ', e.g in ~/.profile, ~/.mkshrc or
in the system wide /etc/profile.  The attached patch is for R54.

Werner

-- 
  "Having a smoking section in a restaurant is like having
          a peeing section in a swimming pool." -- Edward Burr
Add support for setting locale after mksh has started.
With this change it is possible to override the locale
in the personal ~/.profile or ~/.mkshrc as well as in
the system wide /etc/profile.

---
 main.c     |    3 +--
 sh.h       |    1 +
 var.c      |   51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 var_spec.h |    6 ++++++
 4 files changed, 59 insertions(+), 2 deletions(-)

--- main.c
+++ main.c	2017-12-15 07:34:47.135915295 +0000
@@ -46,7 +46,6 @@ extern char **environ;
 #define MKSH_DEFAULT_TMPDIR	MKSH_UNIXROOT "/tmp"
 #endif
 
-static uint8_t isuc(const char *);
 static int main_init(int, const char *[], Source **, struct block **);
 void chvt_reinit(void);
 static void reclaim(void);
@@ -155,7 +154,7 @@ static const char *empty_argv[] = {
 	Tmksh, NULL
 };
 
-static uint8_t
+uint8_t
 isuc(const char *cx) {
 	char *cp, *x;
 	uint8_t rv = 0;
--- sh.h
+++ sh.h	2017-12-15 07:35:43.938851966 +0000
@@ -2169,6 +2169,7 @@ void set_prompt(int, Source *);
 int pprompt(const char *, int);
 /* main.c */
 int include(const char *, int, const char **, bool);
+uint8_t isuc(const char *);
 int command(const char *, int);
 int shell(Source * volatile, volatile bool);
 /* argument MUST NOT be 0 */
--- var.c
+++ var.c	2017-12-15 07:33:49.364996826 +0000
@@ -28,6 +28,13 @@
 #include <sys/sysctl.h>
 #endif
 
+#if HAVE_LANGINFO_CODESET
+#include <langinfo.h>
+#endif
+#if HAVE_SETLOCALE_CTYPE
+#include <locale.h>
+#endif
+
 __RCSID("$MirOS: src/bin/mksh/var.c,v 1.209 2016/11/11 23:31:39 tg Exp $");
 
 /*-
@@ -1326,6 +1333,28 @@ setspec(struct tbl *vp)
 				strdupx(tmpdir, s, APERM);
 		}
 		return;
+#if HAVE_SETLOCALE_CTYPE
+	case V_LC_ALL:
+	case V_LC_CTYPE:
+	case V_LANG:
+		{
+			char *loc;
+			loc = str_val(global("LC_ALL"));
+			if (loc == null)
+				loc = str_val(global("LC_CTYPE"));
+			if (loc == null)
+				loc = str_val(global("LANG"));
+			if (loc == null)
+				return;
+			setlocale(LC_CTYPE, loc);
+#if HAVE_LANGINFO_CODESET
+			if (!isuc(loc))
+				loc = nl_langinfo(CODESET);
+#endif
+			UTFMODE = isuc(loc);
+		}
+		return;
+#endif
 	/* common sub-cases */
 	case V_COLUMNS:
 	case V_LINES:
@@ -1444,6 +1473,28 @@ unsetspec(struct tbl *vp)
 			tmpdir = NULL;
 		}
 		break;
+#if HAVE_SETLOCALE_CTYPE
+	case V_LC_ALL:
+	case V_LC_CTYPE:
+	case V_LANG:
+		{
+			char *loc;
+			loc = str_val(global("LC_ALL"));
+			if (loc == null)
+				loc = str_val(global("LC_CTYPE"));
+			if (loc == null)
+				loc = str_val(global("LANG"));
+			if (loc == null)
+				break;
+			setlocale(LC_CTYPE, loc);
+#if HAVE_LANGINFO_CODESET
+			if (!isuc(loc))
+				loc = nl_langinfo(CODESET);
+#endif
+			UTFMODE = isuc(loc);
+		}
+		break;
+#endif
 	case V_LINENO:
 	case V_RANDOM:
 	case V_SECONDS:
--- var_spec.h
+++ var_spec.h	2017-12-15 07:29:01.550386292 +0000
@@ -68,6 +68,12 @@ FN(TERM)
 FN(TMOUT)
 FN(TMPDIR)
 
+#if HAVE_SETLOCALE_CTYPE
+FN(LANG)
+FN(LC_CTYPE)
+FN(LC_ALL)
+#endif
+
 #undef FN
 #undef F0
 #undef VARSPEC_DEFNS

Attachment: signature.asc
Description: PGP signature

Reply via email to