summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Marlow <marlowsd@gmail.com>2009-06-23 14:38:13 +0000
committerSimon Marlow <marlowsd@gmail.com>2009-06-23 14:38:13 +0000
commit77bdf4db9c309ffe4696822a856d4d1c69b924a0 (patch)
treee98a38665c70953be8fabae68a16eed7023df7fb
parent82ea26e243e338b9cff4f52e8e0c699501c9549a (diff)
downloadhaskell-77bdf4db9c309ffe4696822a856d4d1c69b924a0.tar.gz
Call nl_langinfo(CODESET) to get the name of the locale encoding on Unix
-rw-r--r--libraries/base/GHC/IO/Encoding/Iconv.hs17
-rw-r--r--libraries/base/cbits/PrelIOUtils.c10
-rw-r--r--libraries/base/configure.ac2
3 files changed, 26 insertions, 3 deletions
diff --git a/libraries/base/GHC/IO/Encoding/Iconv.hs b/libraries/base/GHC/IO/Encoding/Iconv.hs
index 237468a76f..25e22ab6bb 100644
--- a/libraries/base/GHC/IO/Encoding/Iconv.hs
+++ b/libraries/base/GHC/IO/Encoding/Iconv.hs
@@ -1,4 +1,4 @@
-{-# OPTIONS_GHC -fno-implicit-prelude -#include "HsBase.h" #-}
+{-# OPTIONS_GHC -fno-implicit-prelude #-}
-----------------------------------------------------------------------------
-- |
-- Module : GHC.IO.Encoding.Iconv
@@ -25,6 +25,8 @@ module GHC.IO.Encoding.Iconv (
#endif
) where
+#include "HsBaseConfig.h"
+
#if !defined(mingw32_HOST_OS)
#undef DEBUG_DUMP
@@ -98,7 +100,15 @@ utf32be = unsafePerformIO (mkTextEncoding "UTF32BE")
{-# NOINLINE localeEncoding #-}
localeEncoding :: TextEncoding
-localeEncoding = unsafePerformIO (mkTextEncoding "")
+localeEncoding = unsafePerformIO $ do
+#if HAVE_LANGINFO_H
+ cstr <- c_localeEncoding -- use nl_langinfo(CODESET) to get the encoding
+ -- if we have it
+ r <- peekCString cstr
+ mkTextEncoding r
+#else
+ mkTextEncoding "" -- GNU iconv accepts "" to mean the -- locale encoding.
+#endif
-- We hope iconv_t is a storable type. It should be, since it has at least the
-- value -1, which is a possible return value from iconv_open.
@@ -114,6 +124,9 @@ foreign import ccall unsafe "iconv"
iconv :: IConv -> Ptr CString -> Ptr CSize -> Ptr CString -> Ptr CSize
-> IO CSize
+foreign import ccall unsafe "localeEncoding"
+ c_localeEncoding :: IO CString
+
haskellChar :: String
#ifdef WORDS_BIGENDIAN
haskellChar | charSize == 2 = "UTF16BE"
diff --git a/libraries/base/cbits/PrelIOUtils.c b/libraries/base/cbits/PrelIOUtils.c
index de7fed4387..6444bd04e6 100644
--- a/libraries/base/cbits/PrelIOUtils.c
+++ b/libraries/base/cbits/PrelIOUtils.c
@@ -24,4 +24,14 @@ void debugBelch2(const char*s, char *t)
{
debugBelch(s,t);
}
+
+// Use a C wrapper for this because we avoid hsc2hs in base
+#if HAVE_LANGINFO_H
+#include <langinfo.h>
+char *localeEncoding (void)
+{
+ return nl_langinfo(CODESET);
+}
+#endif
+
#endif /* __GLASGOW_HASKELL__ */
diff --git a/libraries/base/configure.ac b/libraries/base/configure.ac
index 4ae0be1cb5..cd153c3bc0 100644
--- a/libraries/base/configure.ac
+++ b/libraries/base/configure.ac
@@ -17,7 +17,7 @@ dnl ** check for full ANSI header (.h) files
AC_HEADER_STDC
# check for specific header (.h) files that we are interested in
-AC_CHECK_HEADERS([ctype.h dirent.h errno.h fcntl.h inttypes.h limits.h signal.h sys/resource.h sys/select.h sys/stat.h sys/syscall.h sys/time.h sys/timeb.h sys/timers.h sys/times.h sys/types.h sys/utsname.h sys/wait.h termios.h time.h unistd.h utime.h windows.h winsock.h])
+AC_CHECK_HEADERS([ctype.h dirent.h errno.h fcntl.h inttypes.h limits.h signal.h sys/resource.h sys/select.h sys/stat.h sys/syscall.h sys/time.h sys/timeb.h sys/timers.h sys/times.h sys/types.h sys/utsname.h sys/wait.h termios.h time.h unistd.h utime.h windows.h winsock.h langinfo.h])
# Enable large file support. Do this before testing the types ino_t, off_t, and
# rlim_t, because it will affect the result of that test.