[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Nmh-workers] show's checkmime
From: |
Oliver Kiddle |
Subject: |
Re: [Nmh-workers] show's checkmime |
Date: |
Mon, 24 Jan 2005 16:04:38 +0100 |
On 21 Jan, wrote:
>
> a user may have done 'export MM_CHARSET="$LANG";', so we might want to check
> if UTF-8 is found anywhere in the string:
User's really shouldn't set MM_CHARSET like that. That'd result in
outgoing mime messages having something like charset="fr_FR.UTF-8" in
the Content-Type header instead of one of the canonical forms. If we
want to make it easier for user's to set MM_CHARSET, using nl_langinfo
is better. It was a mistake that I used strncasecmp with 5, in the
patch, just using strcasecmp is I think better.
> The only reason it doesn't is because MM_CHARSET was created before
> nl_langinfo()
> existed. Also, we may still be advertising the ability to build it in some
> old
> crufty environments that don't have nl_langinfo() *yet*, so we may need to
> have
> an #ifdef/#else/#endif and some ./configure magic (or heave said environments
> from last century over the side and be done with it.. ;)
How about we continue to look in MM_CHARSET but check nl_langinfo where
possible if the environment variable isn't set. The simple patch for
that follows. The only difficult part is that nl_langinfo doesn't
necessarily return character sets in canonical MIME form. There's code
to convert it at http://www.cl.cam.ac.uk/~mgk25/ucs/norm_charmap.c
If nobody objects, the easiest is just to include that.
Oliver
Index: configure.in
===================================================================
RCS file: /cvsroot/nmh/nmh/configure.in,v
retrieving revision 1.65
diff -u -r1.65 configure.in
--- configure.in 6 Oct 2003 20:11:54 -0000 1.65
+++ configure.in 24 Jan 2005 14:49:48 -0000
@@ -444,8 +444,8 @@
AC_HEADER_STAT
AC_CHECK_HEADERS(string.h memory.h stdlib.h unistd.h errno.h fcntl.h \
limits.h crypt.h termcap.h termio.h termios.h locale.h \
- netdb.h sys/param.h sys/time.h sys/utsname.h arpa/inet.h \
- arpa/ftp.h)
+ langinfo.h netdb.h sys/param.h sys/time.h sys/utsname.h \
+ arpa/inet.h arpa/ftp.h)
AC_CACHE_CHECK(POSIX termios, nmh_cv_sys_posix_termios,
@@ -499,7 +499,7 @@
AC_CHECK_LIB(mkstemp,mkstemp)
AC_CHECK_FUNCS(waitpid wait3 sigaction sigprocmask sigblock sigsetmask \
sighold sigrelse writev lstat uname tzset killpg mkstemp \
- sethostent getutent)
+ sethostent getutent nl_langinfo)
dnl solaris screws this up
AC_CHECK_FUNC(gethostbyname, [AC_DEFINE(HAVE_GETHOSTBYNAME)],
Index: h/prototypes.h
===================================================================
RCS file: /cvsroot/nmh/nmh/h/prototypes.h,v
retrieving revision 1.8
diff -u -r1.8 prototypes.h
--- h/prototypes.h 19 Nov 2004 05:06:16 -0000 1.8
+++ h/prototypes.h 24 Jan 2005 14:49:48 -0000
@@ -83,6 +83,7 @@
void m_unknown(FILE *);
int makedir (char *);
char *nmh_getpass(const char *);
+char *norm_charmap(char *);
char *new_fs (char *, char *, char *);
char *path(char *, int);
int peekc(FILE *ib);
Index: sbr/Makefile.in
===================================================================
RCS file: /cvsroot/nmh/nmh/sbr/Makefile.in,v
retrieving revision 1.15
diff -u -r1.15 Makefile.in
--- sbr/Makefile.in 21 Nov 2002 21:22:24 -0000 1.15
+++ sbr/Makefile.in 24 Jan 2005 14:49:48 -0000
@@ -65,7 +65,8 @@
fmt_scan.c lock_file.c m_atoi.c m_backup.c \
m_convert.c m_draft.c m_getfld.c m_gmprot.c \
m_maildir.c m_name.c m_scratch.c m_tmpfil.c \
- makedir.c mts.c path.c peekc.c pidwait.c pidstatus.c \
+ makedir.c mts.c norm_charmap.c \
+ path.c peekc.c pidwait.c pidstatus.c \
print_help.c print_sw.c print_version.c push.c \
putenv.c pwd.c refile.c remdir.c r1bindex.c \
readconfig.c ruserpass.c seq_add.c seq_bits.c \
Index: sbr/check_charset.c
===================================================================
RCS file: /cvsroot/nmh/nmh/sbr/check_charset.c,v
retrieving revision 1.3
diff -u -r1.3 check_charset.c
--- sbr/check_charset.c 21 Jan 2005 19:25:44 -0000 1.3
+++ sbr/check_charset.c 24 Jan 2005 14:49:48 -0000
@@ -10,6 +10,25 @@
*/
#include <h/mh.h>
+#ifdef HAVE_LANGINFO_H
+# include <langinfo.h>
+#endif
+
+
+/*
+ * Get the current character set
+ */
+char *
+get_charset ()
+{
+ char *charset = getenv ("MM_CHARSET");
+#if defined(HAVE_NL_LANGINFO) && defined(CODESET)
+ if (!charset)
+ charset = norm_charmap(nl_langinfo (CODESET));
+#endif
+ return charset;
+}
+
/*
* Check if we can display a given character set natively.
@@ -28,7 +47,7 @@
/* Cache the name of our default character set */
if (!mm_charset) {
- if (!(mm_charset = getenv ("MM_CHARSET")))
+ if (!(mm_charset = get_charset ()))
mm_charset = "US-ASCII";
mm_len = strlen (mm_charset);
@@ -63,7 +82,7 @@
* Cache the name of the character set to
* use for 8bit text.
*/
- if (!mm_charset && !(mm_charset = getenv ("MM_CHARSET")))
+ if (!mm_charset && !(mm_charset = get_charset ()))
mm_charset = "x-unknown";
return mm_charset;
Index: sbr/norm_charmap.c
===================================================================
RCS file: sbr/norm_charmap.c
diff -N sbr/norm_charmap.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ sbr/norm_charmap.c 24 Jan 2005 14:49:48 -0000
@@ -0,0 +1,112 @@
+/*
+ * The Single Unix Specification function nl_langinfo(CODESET)
+ * returns the name of the encoding used by the currently selected
+ * locale:
+ *
+ * http://www.opengroup.org/onlinepubs/7908799/xsh/langinfo.h.html
+ *
+ * Unfortunately the encoding names are not yet standardized.
+ * This function knows about the encoding names used on many
+ * different systems and converts them where possible into
+ * the corresponding MIME charset name registered in
+ *
+ * http://www.iana.org/assignments/character-sets
+ *
+ * Please extend it as needed and suggest improvements to the author.
+ *
+ * address@hidden -- 2002-03-11
+ * Permission to use, copy, modify, and distribute this software
+ * for any purpose and without fee is hereby granted. The author
+ * disclaims all warranties with regard to this software.
+ *
+ * Latest version:
+ *
+ * http://www.cl.cam.ac.uk/~mgk25/ucs/norm_charmap.c
+ */
+
+#include <string.h>
+
+#define digit(x) ((x) >= '0' && (x) <= '9')
+
+static char buf[16];
+
+char *
+norm_charmap(char *name)
+{
+ char *p;
+
+ if (!name)
+ return name;
+
+ /* Many need no remapping, but they are listed here so you
+ * can see what output to expect, and modify for your needs
+ * as necessary. */
+ if (!strcmp(name, "UTF-8"))
+ return "UTF-8";
+ if (!strcmp(name, "EUC-JP"))
+ return "EUC-JP";
+ if (!strcmp(name, "EUC-KR"))
+ return "EUC-KR";
+ if (!strcmp(name, "EUC-TW"))
+ return "EUC-TW";
+ if (!strcmp(name, "KOI8-R"))
+ return "KOI8-R";
+ if (!strcmp(name, "KOI8-U"))
+ return "KOI8-U";
+ if (!strcmp(name, "GBK"))
+ return "GBK";
+ if (!strcmp(name, "GB2312"))
+ return "GB2312";
+ if (!strcmp(name, "GB18030"))
+ return "GB18030";
+ if (!strcmp(name, "VSCII"))
+ return "VSCII";
+
+ /* ASCII comes in many names */
+ if (!strcmp(name, "ASCII") ||
+ !strcmp(name, "US-ASCII") ||
+ !strcmp(name, "ANSI_X3.4-1968") ||
+ !strcmp(name, "646") ||
+ !strcmp(name, "ISO646") ||
+ !strcmp(name, "ISO_646.IRV"))
+ return "US-ASCII";
+
+ /* ISO 8859 will be converted to "ISO-8859-x" */
+ if ((p = strstr(name, "8859-"))) {
+ memcpy(buf, "ISO-8859-\0\0", 12);
+ p += 5;
+ if (digit(*p)) {
+ buf[9] = *p++;
+ if (digit(*p)) buf[10] = *p++;
+ return buf;
+ }
+ }
+
+ /* Windows code pages will be converted to "WINDOWS-12xx" */
+ if ((p = strstr(name, "CP12"))) {
+ memcpy(buf, "WINDOWS-12\0\0", 13);
+ p += 4;
+ if (digit(*p)) {
+ buf[10] = *p++;
+ if (digit(*p)) buf[11] = *p++;
+ return buf;
+ }
+ }
+
+ /* TIS-620 comes in at least the following two forms */
+ if (!strcmp(name, "TIS-620") ||
+ !strcmp(name, "TIS620.2533"))
+ return "ISO-8859-11";
+
+ /* For some, uppercase/lowercase might differ */
+ if (!strcmp(name, "Big5") || !strcmp(name, "BIG5"))
+ return "Big5";
+ if (!strcmp(name, "Big5HKSCS") || !strcmp(name, "BIG5HKSCS"))
+ return "Big5HKSCS";
+
+ /* I don't know of any implementation of nl_langinfo(CODESET) out
+ * there that returns anything else (and I'm not even certain all of
+ * the above occur in the wild), but just in case, as a fallback,
+ * return the unmodified name. */
+ return name;
+}