From 2c56b665394c80195b976537e608b690947fcb14 Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Tue, 14 Nov 2006 22:42:37 +0100 Subject: [PATCH] simplify charset and things in the sockets API. Signed-off-by: Pierre Habouzit --- charset.c | 189 ++++++++++++++++++++++-------------------- charset.h | 38 ++++----- imap/command.c | 4 +- lib-mime/rfc3676.c | 10 +-- lib-sys/mutt_socket.c | 5 +- lib-sys/mutt_socket.h | 7 +- nntp/nntp.c | 13 ++- pop/pop_lib.c | 6 +- protos.h | 5 -- 9 files changed, 134 insertions(+), 143 deletions(-) diff --git a/charset.c b/charset.c index bf56607..bbaf8ca 100644 --- a/charset.c +++ b/charset.c @@ -21,6 +21,9 @@ #include #include #include +#ifdef HAVE_LANGINFO_CODESET +# include +#endif #include #include @@ -31,7 +34,7 @@ #include "charset.h" #ifndef EILSEQ -# define EILSEQ EINVAL +# define EILSEQ EINVAL #endif char *Charset; @@ -58,82 +61,82 @@ static struct { {"us", "us-ascii"}, {"ibm367", "us-ascii"}, {"cp367", "us-ascii"}, - {"csASCII", "us-ascii"}, - {"csISO2022KR", "iso-2022-kr"}, - {"csEUCKR", "euc-kr"}, - {"csISO2022JP", "iso-2022-jp"}, - {"csISO2022JP2", "iso-2022-jp-2"}, - {"ISO_8859-1:1987", "iso-8859-1"}, + {"csascii", "us-ascii"}, + {"csiso2022kr", "iso-2022-kr"}, + {"cseuckr", "euc-kr"}, + {"csiso2022jp", "iso-2022-jp"}, + {"csiso2022jp2", "iso-2022-jp-2"}, + {"iso_8859-1:1987", "iso-8859-1"}, {"iso-ir-100", "iso-8859-1"}, {"iso_8859-1", "iso-8859-1"}, {"latin1", "iso-8859-1"}, {"l1", "iso-8859-1"}, - {"IBM819", "iso-8859-1"}, - {"CP819", "iso-8859-1"}, - {"csISOLatin1", "iso-8859-1"}, - {"ISO_8859-2:1987", "iso-8859-2"}, + {"ibm819", "iso-8859-1"}, + {"cp819", "iso-8859-1"}, + {"csisolatin1", "iso-8859-1"}, + {"iso_8859-2:1987", "iso-8859-2"}, {"iso-ir-101", "iso-8859-2"}, {"iso_8859-2", "iso-8859-2"}, {"latin2", "iso-8859-2"}, {"l2", "iso-8859-2"}, - {"csISOLatin2", "iso-8859-2"}, - {"ISO_8859-3:1988", "iso-8859-3"}, + {"csisolatin2", "iso-8859-2"}, + {"iso_8859-3:1988", "iso-8859-3"}, {"iso-ir-109", "iso-8859-3"}, - {"ISO_8859-3", "iso-8859-3"}, + {"iso_8859-3", "iso-8859-3"}, {"latin3", "iso-8859-3"}, {"l3", "iso-8859-3"}, - {"csISOLatin3", "iso-8859-3"}, - {"ISO_8859-4:1988", "iso-8859-4"}, + {"csisolatin3", "iso-8859-3"}, + {"iso_8859-4:1988", "iso-8859-4"}, {"iso-ir-110", "iso-8859-4"}, - {"ISO_8859-4", "iso-8859-4"}, + {"iso_8859-4", "iso-8859-4"}, {"latin4", "iso-8859-4"}, {"l4", "iso-8859-4"}, - {"csISOLatin4", "iso-8859-4"}, - {"ISO_8859-6:1987", "iso-8859-6"}, + {"csisolatin4", "iso-8859-4"}, + {"iso_8859-6:1987", "iso-8859-6"}, {"iso-ir-127", "iso-8859-6"}, {"iso_8859-6", "iso-8859-6"}, - {"ECMA-114", "iso-8859-6"}, - {"ASMO-708", "iso-8859-6"}, + {"ecma-114", "iso-8859-6"}, + {"asmo-708", "iso-8859-6"}, {"arabic", "iso-8859-6"}, - {"csISOLatinArabic", "iso-8859-6"}, - {"ISO_8859-7:1987", "iso-8859-7"}, + {"csisolatinarabic", "iso-8859-6"}, + {"iso_8859-7:1987", "iso-8859-7"}, {"iso-ir-126", "iso-8859-7"}, - {"ISO_8859-7", "iso-8859-7"}, - {"ELOT_928", "iso-8859-7"}, - {"ECMA-118", "iso-8859-7"}, + {"iso_8859-7", "iso-8859-7"}, + {"elot_928", "iso-8859-7"}, + {"ecma-118", "iso-8859-7"}, {"greek", "iso-8859-7"}, {"greek8", "iso-8859-7"}, - {"csISOLatinGreek", "iso-8859-7"}, - {"ISO_8859-8:1988", "iso-8859-8"}, + {"csisolatingreek", "iso-8859-7"}, + {"iso_8859-8:1988", "iso-8859-8"}, {"iso-ir-138", "iso-8859-8"}, - {"ISO_8859-8", "iso-8859-8"}, + {"iso_8859-8", "iso-8859-8"}, {"hebrew", "iso-8859-8"}, - {"csISOLatinHebrew", "iso-8859-8"}, - {"ISO_8859-5:1988", "iso-8859-5"}, + {"csisolatinhebrew", "iso-8859-8"}, + {"iso_8859-5:1988", "iso-8859-5"}, {"iso-ir-144", "iso-8859-5"}, - {"ISO_8859-5", "iso-8859-5"}, + {"iso_8859-5", "iso-8859-5"}, {"cyrillic", "iso-8859-5"}, - {"csISOLatinCyrillic", "iso8859-5"}, - {"ISO_8859-9:1989", "iso-8859-9"}, + {"csisolatincyrillic", "iso8859-5"}, + {"iso_8859-9:1989", "iso-8859-9"}, {"iso-ir-148", "iso-8859-9"}, - {"ISO_8859-9", "iso-8859-9"}, + {"iso_8859-9", "iso-8859-9"}, {"latin5", "iso-8859-9"}, {"l5", "iso-8859-9"}, - {"csISOLatin5", "iso-8859-9"}, - {"ISO_8859-10:1992", "iso-8859-10"}, + {"csisolatin5", "iso-8859-9"}, + {"iso_8859-10:1992", "iso-8859-10"}, {"iso-ir-157", "iso-8859-10"}, {"latin6", "iso-8859-10"}, {"l6", "iso-8859-10"}, - {"csISOLatin6", "iso-8859-10"}, - {"csKOI8r", "koi8-r"}, - {"MS_Kanji", "Shift_JIS"}, - {"csShiftJis", "Shift_JIS"}, - {"Extended_UNIX_Code_Packed_Format_for_Japanese", "EUC-JP"}, - {"csEUCPkdFmtJapanese", "EUC-JP"}, - {"csGB2312", "gb2312"}, + {"csisolatin6", "iso-8859-10"}, + {"cskoi8r", "koi8-r"}, + {"ms_kanji", "shift_jis"}, + {"csshiftjis", "shift_jis"}, + {"extended_unix_code_packed_format_for_japanese", "euc-jp"}, + {"cseucpkdfmtjapanese", "euc-jp"}, + {"csgb2312", "gb2312"}, {"csbig5", "big5"}, - /* End of official brain damage. - What follows has been taken * from glibc's localedata files. */ + /* end of official brain damage. + what follows has been taken * from glibc's localedata files. */ {"iso_8859-13", "iso-8859-13"}, {"iso-ir-179", "iso-8859-13"}, {"latin7", "iso-8859-13"}, @@ -147,19 +150,15 @@ static struct { {"iso_8859-16", "iso-8859-16"}, {"latin10", "iso-8859-16"}, {"646", "us-ascii"}, - {"eucJP", "euc-jp"}, - {"PCK", "Shift_JIS"}, - {"ko_KR-euc", "euc-kr"}, - {"zh_TW-big5", "big5"}, - {"sjis", "Shift_JIS"}, - {"euc-jp-ms", "eucJP-ms"}, + {"eucjp", "euc-jp"}, + {"pck", "shift_jis"}, + {"ko_kr-euc", "euc-kr"}, + {"zh_tw-big5", "big5"}, + {"sjis", "shift_jis"}, + {"euc-jp-ms", "eucjp-ms"}, {NULL, NULL} }; -#ifdef HAVE_LANGINFO_CODESET -# include -#endif - void mutt_set_langinfo_charset (void) { #ifdef HAVE_LANGINFO_CODESET @@ -167,7 +166,7 @@ void mutt_set_langinfo_charset (void) char buff2[LONG_STRING]; m_strcpy(buff, sizeof(buff), nl_langinfo(CODESET)); - mutt_canonical_charset (buff2, sizeof (buff2), buff); + mutt_canonical_charset(buff2, sizeof(buff2), buff); /* finally, set $charset */ if (!(Charset = m_strdup(buff2))) @@ -178,45 +177,54 @@ void mutt_set_langinfo_charset (void) void mutt_canonical_charset(char *dest, ssize_t dlen, const char *name) { - ssize_t i; - char *p; - char scratch[LONG_STRING]; - - /* catch some common iso-8859-something misspellings */ - if (!ascii_strncasecmp (name, "8859", 4) && name[4] != '-') - snprintf (scratch, sizeof (scratch), "iso-8859-%s", name + 4); - else if (!ascii_strncasecmp (name, "8859-", 5)) - snprintf (scratch, sizeof (scratch), "iso-8859-%s", name + 5); - else if (!ascii_strncasecmp (name, "iso8859", 7) && name[7] != '-') - snprintf (scratch, sizeof (scratch), "iso_8859-%s", name + 7); - else if (!ascii_strncasecmp (name, "iso8859-", 8)) - snprintf (scratch, sizeof (scratch), "iso_8859-%s", name + 8); - else - m_strcpy(scratch, sizeof(scratch), NONULL(name)); - - for (i = 0; PreferredMIMENames[i].key; i++) - if (!ascii_strcasecmp (scratch, PreferredMIMENames[i].key) || - !m_strcasecmp(scratch, PreferredMIMENames[i].key)) { - m_strcpy(dest, dlen, PreferredMIMENames[i].pref); - return; + ssize_t i; + char *p; + char scratch[LONG_STRING]; + + m_strcpy(scratch, sizeof(scratch), name); + m_strtolower(scratch); + + /* catch some common iso-8859-something misspellings */ + if (!strncmp(scratch, "8859", 4)) { + snprintf(scratch, sizeof(scratch), "iso-8859-%s", + name + 4 + (name[4] == '-')); + m_strtolower(scratch); + } else + if (!strncmp(scratch, "iso8859", 7)) { + snprintf(scratch, sizeof(scratch), "iso-8859-%s", + name + 7 + (name[7] == '-')); + m_strtolower(scratch); } - m_strcpy(dest, dlen, scratch); + for (i = 0; PreferredMIMENames[i].key; i++) { + if (!strcmp(scratch, PreferredMIMENames[i].key)) { + m_strcpy(dest, dlen, PreferredMIMENames[i].pref); + return; + } + } - /* for cosmetics' sake, transform to lowercase. */ - for (p = dest; *p; p++) - *p = ascii_tolower (*p); + m_strcpy(dest, dlen, scratch); } -int mutt_chscmp (const char *s, const char *chs) +static int mutt_chscmp(const char *s, const char *chs) { - char buffer[STRING]; + char buffer[STRING]; - if (!s) - return 0; + if (!s) + return 0; + + mutt_canonical_charset(buffer, sizeof(buffer), s); + return !strcmp(buffer, chs); +} - mutt_canonical_charset (buffer, sizeof (buffer), s); - return !ascii_strcasecmp (buffer, chs); +int mutt_is_utf8(const char *s) +{ + return mutt_chscmp(s, "utf-8"); +} + +int mutt_is_us_ascii(const char *s) +{ + return mutt_chscmp(s, "us-ascii"); } @@ -584,10 +592,7 @@ void mutt_set_charset (char *charset) char buffer[STRING]; mutt_canonical_charset (buffer, sizeof (buffer), charset); - - Charset_is_utf8 = 0; - if (!strcmp (buffer, "utf-8")) - Charset_is_utf8 = 1; + Charset_is_utf8 = !strcmp(buffer, "utf-8"); #ifdef HAVE_BIND_TEXTDOMAIN_CODESET bind_textdomain_codeset (PACKAGE, buffer); diff --git a/charset.h b/charset.h index 4640b80..702f8f1 100644 --- a/charset.h +++ b/charset.h @@ -11,32 +11,37 @@ #define _CHARSET_H #ifdef HAVE_CONFIG_H -#include "config.h" +# include "config.h" #endif -#ifdef HAVE_ICONV_H -#include -#endif + +#define M_ICONV_HOOK_FROM 1 +#define M_ICONV_HOOK_TO 2 extern char *Charset; extern int Charset_is_utf8; -#ifndef HAVE_ICONV_T_DEF -typedef void *iconv_t; -#endif +#ifdef HAVE_ICONV_H +# include -#ifndef HAVE_ICONV +static inline ssize_t +my_iconv(iconv_t ict, const char **in, ssize_t *il, char **out, ssize_t *ol) { + return iconv(ict, (char **)in, (size_t*)il, out, (size_t*)ol); +} + +#else +# define iconv_t void* # define iconv_open(a, b) ((iconv_t)(-1)) # define my_iconv(a,b,c,d,e) 0 # define iconv_close(a) 0 -#else -static inline ssize_t my_iconv(iconv_t ict, const char **inbuf, ssize_t *ilen, - char **outbuf, ssize_t *olen) -{ - return iconv(ict, (char **)inbuf, (size_t*)ilen, outbuf, (size_t*)olen); -} #endif +void mutt_set_langinfo_charset (void); +void mutt_canonical_charset(char *, ssize_t, const char *); + +int mutt_is_utf8(const char *s); +int mutt_is_us_ascii(const char *s); + int mutt_convert_string (char **, const char *, const char *, int); const char *mutt_get_first_charset (const char *); int mutt_convert_nonmime_string (char **); @@ -52,11 +57,6 @@ int fgetconv (FGETCONV *); char *fgetconvs (char *, ssize_t, FGETCONV *); void fgetconv_close (FGETCONV **); -void mutt_set_langinfo_charset (void); - -#define M_ICONV_HOOK_FROM 1 -#define M_ICONV_HOOK_TO 2 - void mutt_set_charset(char *charset); wchar_t replacement_char(void); diff --git a/imap/command.c b/imap/command.c index 5caaee9..53132f7 100644 --- a/imap/command.c +++ b/imap/command.c @@ -186,9 +186,7 @@ int imap_exec (IMAP_DATA * idata, const char *cmd, int flags) out = p_new(char, outlen); snprintf (out, outlen, "%s %s\r\n", idata->cmd.seq, cmd); - rc = mutt_socket_write_d (idata->conn, out, - flags & IMAP_CMD_PASS ? IMAP_LOG_PASS : - IMAP_LOG_CMD); + rc = mutt_socket_write(idata->conn, out); p_delete(&out); if (rc < 0) { diff --git a/lib-mime/rfc3676.c b/lib-mime/rfc3676.c index 51b6450..b93a766 100644 --- a/lib-mime/rfc3676.c +++ b/lib-mime/rfc3676.c @@ -220,15 +220,15 @@ int rfc3676_handler (BODY * a, STATE * s) { void rfc3676_space_stuff (HEADER* hdr) { FILE* in = NULL, *out = NULL; char buf[LONG_STRING]; - char tmpfile[_POSIX_PATH_MAX]; + char tmpf[_POSIX_PATH_MAX]; if (!hdr || !hdr->content || !hdr->content->filename) return; if ((in = safe_fopen (hdr->content->filename, "r")) == NULL) return; - mutt_mktemp (tmpfile); - if ((out = safe_fopen (tmpfile, "w+")) == NULL) { + mutt_mktemp (tmpf); + if ((out = safe_fopen (tmpf, "w+")) == NULL) { fclose (in); return; } @@ -241,7 +241,7 @@ void rfc3676_space_stuff (HEADER* hdr) { } fclose (in); fclose (out); - mutt_set_mtime (hdr->content->filename, tmpfile); + mutt_set_mtime (hdr->content->filename, tmpf); unlink (hdr->content->filename); - m_strreplace(&hdr->content->filename, tmpfile); + m_strreplace(&hdr->content->filename, tmpf); } diff --git a/lib-sys/mutt_socket.c b/lib-sys/mutt_socket.c index 78a609f..0218610 100644 --- a/lib-sys/mutt_socket.c +++ b/lib-sys/mutt_socket.c @@ -88,7 +88,7 @@ int mutt_socket_read (CONNECTION * conn, char *buf, ssize_t len) return rc; } -int mutt_socket_write_d (CONNECTION * conn, const char *buf, int dbg) +int mutt_socket_write(CONNECTION * conn, const char *buf) { int rc; int len; @@ -132,8 +132,7 @@ int mutt_socket_readchar (CONNECTION * conn, char *c) return 1; } -int mutt_socket_readln_d (char *buf, ssize_t buflen, CONNECTION * conn, - int dbg) +int mutt_socket_readln(char *buf, ssize_t buflen, CONNECTION * conn) { char ch; ssize_t i; diff --git a/lib-sys/mutt_socket.h b/lib-sys/mutt_socket.h index 84664e1..25ad7c0 100644 --- a/lib-sys/mutt_socket.h +++ b/lib-sys/mutt_socket.h @@ -46,11 +46,8 @@ int mutt_socket_close (CONNECTION * conn); int mutt_socket_read (CONNECTION * conn, char *buf, ssize_t len); int mutt_socket_readchar (CONNECTION * conn, char *c); -#define mutt_socket_readln(A,B,C) mutt_socket_readln_d(A,B,C,M_SOCK_LOG_CMD) -int mutt_socket_readln_d (char *buf, ssize_t buflen, CONNECTION * conn, - int dbg); -#define mutt_socket_write(A,B) mutt_socket_write_d(A,B,M_SOCK_LOG_CMD); -int mutt_socket_write_d (CONNECTION * conn, const char *buf, int dbg); +int mutt_socket_readln(char *buf, ssize_t buflen, CONNECTION * conn); +int mutt_socket_write(CONNECTION * conn, const char *buf); /* stupid hack for imap_logout_all */ CONNECTION *mutt_socket_head (void); diff --git a/nntp/nntp.c b/nntp/nntp.c index 6e20eef..1432523 100644 --- a/nntp/nntp.c +++ b/nntp/nntp.c @@ -103,7 +103,7 @@ static int nntp_auth (NNTP_SERVER * serv) } snprintf (buf, sizeof (buf), "AUTHINFO PASS %s\r\n", conn->account.pass); - mutt_socket_write_d (conn, buf, M_SOCK_LOG_FULL); + mutt_socket_write(conn, buf); if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) { conn->account.flags = flags; return -1; @@ -341,8 +341,7 @@ static int mutt_nntp_fetch (NNTP_DATA * nntp_data, const char *query, char *msg, inbuf = p_new(char, sizeof(buf)); for (;;) { - chunk = mutt_socket_readln_d (buf, sizeof (buf), nntp_data->nserv->conn, - M_SOCK_LOG_HDR); + chunk = mutt_socket_readln(buf, sizeof (buf), nntp_data->nserv->conn); if (chunk < 0) break; @@ -1035,15 +1034,15 @@ int nntp_post (const char *msg) buf[len] = '\0'; } if (buf[1] == '.') - mutt_socket_write_d (nntp_data->nserv->conn, buf, M_SOCK_LOG_HDR); + mutt_socket_write(nntp_data->nserv->conn, buf); else - mutt_socket_write_d (nntp_data->nserv->conn, buf + 1, M_SOCK_LOG_HDR); + mutt_socket_write(nntp_data->nserv->conn, buf + 1); } fclose (f); if (buf[m_strlen(buf) - 1] != '\n') - mutt_socket_write_d (nntp_data->nserv->conn, "\r\n", M_SOCK_LOG_HDR); - mutt_socket_write_d (nntp_data->nserv->conn, ".\r\n", M_SOCK_LOG_HDR); + mutt_socket_write(nntp_data->nserv->conn, "\r\n"); + mutt_socket_write(nntp_data->nserv->conn, ".\r\n"); if (mutt_socket_readln (buf, sizeof (buf), nntp_data->nserv->conn) < 0) { mutt_error (_("Can't post article. Connection to %s lost."), nntp_data->nserv->conn->account.host); diff --git a/pop/pop_lib.c b/pop/pop_lib.c index 424ca73..b5aa605 100644 --- a/pop/pop_lib.c +++ b/pop/pop_lib.c @@ -382,13 +382,12 @@ void pop_logout (CONTEXT * ctx) */ pop_query_status pop_query_d (POP_DATA * pop_data, char *buf, size_t buflen, const char *msg) { - int dbg = M_SOCK_LOG_CMD; char *c; if (pop_data->status != POP_CONNECTED) return PQ_NOT_CONNECTED; - mutt_socket_write_d (pop_data->conn, buf, dbg); + mutt_socket_write(pop_data->conn, buf); c = strpbrk (buf, " \r\n"); *c = '\0'; @@ -434,8 +433,7 @@ pop_query_status pop_fetch_data (POP_DATA * pop_data, const char *query, progres for (;;) { chunk = - mutt_socket_readln_d (buf, sizeof (buf), pop_data->conn, - M_SOCK_LOG_HDR); + mutt_socket_readln(buf, sizeof (buf), pop_data->conn); if (chunk < 0) { pop_data->status = POP_DISCONNECTED; ret = PQ_NOT_CONNECTED; diff --git a/protos.h b/protos.h index 53b2b4f..ef5baf1 100644 --- a/protos.h +++ b/protos.h @@ -96,7 +96,6 @@ void mutt_add_to_reference_headers (ENVELOPE * env, ENVELOPE * curenv, string_list_t *** pp, string_list_t *** qq); void mutt_adv_mktemp (const char*, char*, ssize_t); int mutt_bounce_message (FILE * fp, HEADER *, address_t *); -void mutt_canonical_charset (char *, ssize_t, const char *); void mutt_check_rescore (CONTEXT *); void mutt_clear_error (void); void mutt_default_save (char *, ssize_t, HEADER *); @@ -191,10 +190,6 @@ int mutt_display_message (HEADER * h); int mutt_dump_variables (int full); int mutt_edit_message (CONTEXT *, HEADER *); int mutt_fetch_recips (ENVELOPE * out, ENVELOPE * in, int flags); -int mutt_chscmp (const char *s, const char *chs); - -#define mutt_is_utf8(a) mutt_chscmp (a, "utf-8") -#define mutt_is_us_ascii(a) mutt_chscmp (a, "us-ascii") int mutt_prepare_template (FILE *, CONTEXT *, HEADER *, HEADER *, short); int mutt_resend_message (FILE *, CONTEXT *, HEADER *); -- 2.20.1