Simplify charset.cpkg a bit
[apps/madmutt.git] / charset.cpkg
index 2c8f507..6934987 100644 (file)
 #endif
 @import "lib-lua/base.cpkg"
 
+int     Charset_is_utf8    = 0;
+wchar_t CharsetReplacement = '?';
+
 static rx_t *charset_hooks = NULL;
 static rx_t *iconv_hooks   = NULL;
 
+static char *charset_init(void)
+{
+    const char *res = "iso-8859-1";
+#ifdef HAVE_LANGINFO_CODESET
+    char buff[STRING];
+    char buff2[STRING];
+
+    m_strcpy(buff, sizeof(buff), nl_langinfo(CODESET));
+    charset_canonicalize(buff2, sizeof(buff2), buff);
+
+    /* finally, set $charset */
+    if (!m_strisempty(buff2)) {
+        res = buff2;
+    }
+#endif
+#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
+    bind_textdomain_codeset(PACKAGE, res);
+#endif
+
+    Charset_is_utf8    = charset_is_utf8(res);
+    CharsetReplacement = Charset_is_utf8 ? 0xfffd : '?';
+    return m_strdup(res);
+}
+
 @package MCharset {
     /*
      ** .pp
@@ -64,7 +91,7 @@ static rx_t *iconv_hooks   = NULL;
      ** .pp
      ** Character set your terminal uses to display and enter textual data.
      */
-    string_t charset         = NULL;
+    string_t charset         = charset_init();
 
     /*
      ** .pp
@@ -106,40 +133,10 @@ static rx_t *iconv_hooks   = NULL;
     };
 };
 
-int Charset_is_utf8 = 0;
-wchar_t CharsetReplacement = '?';
-
-
 /****************************************************************************/
 /* charset functions                                                        */
 /****************************************************************************/
 
-void charset_initialize(void)
-{
-#ifdef HAVE_LANGINFO_CODESET
-    char buff[STRING];
-    char buff2[STRING];
-
-    m_strcpy(buff, sizeof(buff), nl_langinfo(CODESET));
-    charset_canonicalize(buff2, sizeof(buff2), buff);
-
-    /* finally, set $charset */
-    if (!m_strisempty(buff2)) {
-        m_strreplace(&MCharset.charset, buff2);
-    } else
-#endif
-    {
-        m_strreplace(&MCharset.charset, "iso-8859-1");
-    }
-
-    Charset_is_utf8    = !m_strcmp(MCharset.charset, "utf-8");
-    CharsetReplacement = Charset_is_utf8 ? 0xfffd : '?';
-
-#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
-    bind_textdomain_codeset(PACKAGE, MCharset.charset);
-#endif
-}
-
 #include "charset.gperf"
 void charset_canonicalize(char *dest, ssize_t dlen, const char *name)
 {