X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=blobdiff_plain;f=charset.cpkg;h=89905b8ed4b11b6b1abbb8576815c06a6f2cf171;hp=2c8f507cf8ccc1b8ca81d7ec303b2d8e3ce4a4b1;hb=db3bd72d8b48f1f9b49899da081ffbec4ce2c1c6;hpb=94c83f42e786ffbebb2ea3defcd6df95efe116f0;ds=sidebyside diff --git a/charset.cpkg b/charset.cpkg index 2c8f507..89905b8 100644 --- a/charset.cpkg +++ b/charset.cpkg @@ -27,7 +27,7 @@ #include -#ifdef HAVE_LANGINFO_CODESET +#ifdef HAVE_LANGINFO_H # include #endif @@ -38,10 +38,38 @@ #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; -@package MCharset { +static char *charset_init(void) +{ + const char *res = "iso-8859-1"; +#ifdef HAVE_LANGINFO_H + 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 + bind_textdomain_codeset(PACKAGE, res); + return m_strdup(res); +} + +static void charset_onchange(const char *cset) +{ + Charset_is_utf8 = charset_is_utf8(cset); + CharsetReplacement = Charset_is_utf8 ? 0xfffd : '?'; +} + +@package mod_cset { /* ** .pp ** This variable is a colon-separated list of character encoding @@ -64,7 +92,10 @@ 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 = { + .init = charset_init(); + .onchange = charset_onchange($$); + }; /* ** .pp @@ -95,51 +126,21 @@ static rx_t *iconv_hooks = NULL; void charset_hook(rx_t local, const string_t alias) { rx_set_template(local, alias); - rx_list_append(&charset_hooks, local); + rx_list_add2(&charset_hooks, &local); RETURN(); }; void iconv_hook(rx_t local, const string_t alias) { rx_set_template(local, alias); - rx_list_append(&iconv_hooks, local); + rx_list_add2(&iconv_hooks, &local); RETURN(); }; }; -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) { @@ -206,8 +207,8 @@ int charset_is_us_ascii(const char *s) /* Like iconv_open, but canonicalises the charsets */ iconv_t mutt_iconv_open(const char *tocode, const char *fromcode, int flags) { - char to1[STRING]; - char from1[STRING]; + char to1[STRING], to2[STRING]; + char from1[STRING], from2[STRING]; char tmp[STRING]; iconv_t cd; @@ -228,14 +229,10 @@ iconv_t mutt_iconv_open(const char *tocode, const char *fromcode, int flags) if ((cd = iconv_open(to1, from1)) != MUTT_ICONV_ERROR) return cd; - { - char to2[STRING]; - char from2[STRING]; + if (rx_list_match2(iconv_hooks, to1, to2, sizeof(to2)) + && rx_list_match2(iconv_hooks, from1, from2, sizeof(from2))) + return iconv_open(to2, from2); - if (rx_list_match2(iconv_hooks, to1, to2, sizeof(to2)) - && rx_list_match2(iconv_hooks, from1, from2, sizeof(from2))) - return iconv_open(to2, from2); - } return MUTT_ICONV_ERROR; } @@ -386,7 +383,7 @@ static ssize_t convert_string(const char *f, ssize_t flen, int mutt_convert_nonmime_string(char **ps) { - const char *p = MCharset.assumed_charset; + const char *p = mod_cset.assumed_charset; ssize_t ulen = m_strlen(*ps); char *u = *ps; @@ -405,7 +402,7 @@ int mutt_convert_nonmime_string(char **ps) m_strncpy(fromcode, sizeof(fromcode), p, q - p); p = q; - if (convert_string(u, ulen, fromcode, MCharset.charset, &s, &slen) >= 0) { + if (convert_string(u, ulen, fromcode, mod_cset.charset, &s, &slen) >= 0) { p_delete(ps); *ps = s; return 0;