123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684 |
- #include <config.h>
- #include "localcharset.h"
- #include <fcntl.h>
- #include <stddef.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #if defined __APPLE__ && defined __MACH__ && HAVE_LANGINFO_CODESET
- # define DARWIN7
- #endif
- #if defined _WIN32 || defined __WIN32__
- # define WINDOWS_NATIVE
- # include <locale.h>
- #endif
- #if defined __EMX__
- # ifndef OS2
- # define OS2
- # endif
- #endif
- #if !defined WINDOWS_NATIVE
- # include <unistd.h>
- # if HAVE_LANGINFO_CODESET
- # include <langinfo.h>
- # else
- # if 0
- # include <locale.h>
- # endif
- # endif
- # ifdef __CYGWIN__
- # define WIN32_LEAN_AND_MEAN
- # include <windows.h>
- # endif
- #elif defined WINDOWS_NATIVE
- # define WIN32_LEAN_AND_MEAN
- # include <windows.h>
- #endif
- #if defined OS2
- # define INCL_DOS
- # include <os2.h>
- #endif
- #if defined DARWIN7
- # include <xlocale.h>
- #endif
- #if ENABLE_RELOCATABLE
- # include "relocatable.h"
- #else
- # define relocate(pathname) (pathname)
- #endif
- #ifndef LIBDIR
- # include "configmake.h"
- #endif
- #ifndef O_NOFOLLOW
- # define O_NOFOLLOW 0
- #endif
- #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__
-
- # define ISSLASH(C) ((C) == '/' || (C) == '\\')
- #endif
- #ifndef DIRECTORY_SEPARATOR
- # define DIRECTORY_SEPARATOR '/'
- #endif
- #ifndef ISSLASH
- # define ISSLASH(C) ((C) == DIRECTORY_SEPARATOR)
- #endif
- #if HAVE_DECL_GETC_UNLOCKED
- # undef getc
- # define getc getc_unlocked
- #endif
- #if __STDC__ != 1
- # define volatile
- #endif
- static const char * volatile charset_aliases;
- static const char *
- get_charset_aliases (void)
- {
- const char *cp;
- cp = charset_aliases;
- if (cp == NULL)
- {
- #if !(defined DARWIN7 || defined VMS || defined WINDOWS_NATIVE || defined __CYGWIN__ || defined OS2)
- const char *dir;
- const char *base = "charset.alias";
- char *file_name;
-
- dir = getenv ("CHARSETALIASDIR");
- if (dir == NULL || dir[0] == '\0')
- dir = relocate (LIBDIR);
-
- {
- size_t dir_len = strlen (dir);
- size_t base_len = strlen (base);
- int add_slash = (dir_len > 0 && !ISSLASH (dir[dir_len - 1]));
- file_name = (char *) malloc (dir_len + add_slash + base_len + 1);
- if (file_name != NULL)
- {
- memcpy (file_name, dir, dir_len);
- if (add_slash)
- file_name[dir_len] = DIRECTORY_SEPARATOR;
- memcpy (file_name + dir_len + add_slash, base, base_len + 1);
- }
- }
- if (file_name == NULL)
-
- cp = "";
- else
- {
- int fd;
-
- fd = open (file_name,
- O_RDONLY | (HAVE_WORKING_O_NOFOLLOW ? O_NOFOLLOW : 0));
- if (fd < 0)
-
- cp = "";
- else
- {
- FILE *fp;
- fp = fdopen (fd, "r");
- if (fp == NULL)
- {
-
- close (fd);
- cp = "";
- }
- else
- {
-
- char *res_ptr = NULL;
- size_t res_size = 0;
- for (;;)
- {
- int c;
- char buf1[50+1];
- char buf2[50+1];
- size_t l1, l2;
- char *old_res_ptr;
- c = getc (fp);
- if (c == EOF)
- break;
- if (c == '\n' || c == ' ' || c == '\t')
- continue;
- if (c == '#')
- {
-
- do
- c = getc (fp);
- while (!(c == EOF || c == '\n'));
- if (c == EOF)
- break;
- continue;
- }
- ungetc (c, fp);
- if (fscanf (fp, "%50s %50s", buf1, buf2) < 2)
- break;
- l1 = strlen (buf1);
- l2 = strlen (buf2);
- old_res_ptr = res_ptr;
- if (res_size == 0)
- {
- res_size = l1 + 1 + l2 + 1;
- res_ptr = (char *) malloc (res_size + 1);
- }
- else
- {
- res_size += l1 + 1 + l2 + 1;
- res_ptr = (char *) realloc (res_ptr, res_size + 1);
- }
- if (res_ptr == NULL)
- {
-
- res_size = 0;
- free (old_res_ptr);
- break;
- }
- strcpy (res_ptr + res_size - (l2 + 1) - (l1 + 1), buf1);
- strcpy (res_ptr + res_size - (l2 + 1), buf2);
- }
- fclose (fp);
- if (res_size == 0)
- cp = "";
- else
- {
- *(res_ptr + res_size) = '\0';
- cp = res_ptr;
- }
- }
- }
- free (file_name);
- }
- #else
- # if defined DARWIN7
-
- cp = "ISO8859-1" "\0" "ISO-8859-1" "\0"
- "ISO8859-2" "\0" "ISO-8859-2" "\0"
- "ISO8859-4" "\0" "ISO-8859-4" "\0"
- "ISO8859-5" "\0" "ISO-8859-5" "\0"
- "ISO8859-7" "\0" "ISO-8859-7" "\0"
- "ISO8859-9" "\0" "ISO-8859-9" "\0"
- "ISO8859-13" "\0" "ISO-8859-13" "\0"
- "ISO8859-15" "\0" "ISO-8859-15" "\0"
- "KOI8-R" "\0" "KOI8-R" "\0"
- "KOI8-U" "\0" "KOI8-U" "\0"
- "CP866" "\0" "CP866" "\0"
- "CP949" "\0" "CP949" "\0"
- "CP1131" "\0" "CP1131" "\0"
- "CP1251" "\0" "CP1251" "\0"
- "eucCN" "\0" "GB2312" "\0"
- "GB2312" "\0" "GB2312" "\0"
- "eucJP" "\0" "EUC-JP" "\0"
- "eucKR" "\0" "EUC-KR" "\0"
- "Big5" "\0" "BIG5" "\0"
- "Big5HKSCS" "\0" "BIG5-HKSCS" "\0"
- "GBK" "\0" "GBK" "\0"
- "GB18030" "\0" "GB18030" "\0"
- "SJIS" "\0" "SHIFT_JIS" "\0"
- "ARMSCII-8" "\0" "ARMSCII-8" "\0"
- "PT154" "\0" "PT154" "\0"
-
- "*" "\0" "UTF-8" "\0";
- # endif
- # if defined VMS
-
-
- cp = "ISO8859-1" "\0" "ISO-8859-1" "\0"
- "ISO8859-2" "\0" "ISO-8859-2" "\0"
- "ISO8859-5" "\0" "ISO-8859-5" "\0"
- "ISO8859-7" "\0" "ISO-8859-7" "\0"
- "ISO8859-8" "\0" "ISO-8859-8" "\0"
- "ISO8859-9" "\0" "ISO-8859-9" "\0"
-
- "eucJP" "\0" "EUC-JP" "\0"
- "SJIS" "\0" "SHIFT_JIS" "\0"
- "DECKANJI" "\0" "DEC-KANJI" "\0"
- "SDECKANJI" "\0" "EUC-JP" "\0"
-
- "eucTW" "\0" "EUC-TW" "\0"
- "DECHANYU" "\0" "DEC-HANYU" "\0"
- "DECHANZI" "\0" "GB2312" "\0"
-
- "DECKOREAN" "\0" "EUC-KR" "\0";
- # endif
- # if defined WINDOWS_NATIVE || defined __CYGWIN__
-
- cp = "CP936" "\0" "GBK" "\0"
- "CP1361" "\0" "JOHAB" "\0"
- "CP20127" "\0" "ASCII" "\0"
- "CP20866" "\0" "KOI8-R" "\0"
- "CP20936" "\0" "GB2312" "\0"
- "CP21866" "\0" "KOI8-RU" "\0"
- "CP28591" "\0" "ISO-8859-1" "\0"
- "CP28592" "\0" "ISO-8859-2" "\0"
- "CP28593" "\0" "ISO-8859-3" "\0"
- "CP28594" "\0" "ISO-8859-4" "\0"
- "CP28595" "\0" "ISO-8859-5" "\0"
- "CP28596" "\0" "ISO-8859-6" "\0"
- "CP28597" "\0" "ISO-8859-7" "\0"
- "CP28598" "\0" "ISO-8859-8" "\0"
- "CP28599" "\0" "ISO-8859-9" "\0"
- "CP28605" "\0" "ISO-8859-15" "\0"
- "CP38598" "\0" "ISO-8859-8" "\0"
- "CP51932" "\0" "EUC-JP" "\0"
- "CP51936" "\0" "GB2312" "\0"
- "CP51949" "\0" "EUC-KR" "\0"
- "CP51950" "\0" "EUC-TW" "\0"
- "CP54936" "\0" "GB18030" "\0"
- "CP65001" "\0" "UTF-8" "\0";
- # endif
- # if defined OS2
-
-
- cp = "CP813" "\0" "ISO-8859-7" "\0"
- "CP878" "\0" "KOI8-R" "\0"
- "CP819" "\0" "ISO-8859-1" "\0"
- "CP912" "\0" "ISO-8859-2" "\0"
- "CP913" "\0" "ISO-8859-3" "\0"
- "CP914" "\0" "ISO-8859-4" "\0"
- "CP915" "\0" "ISO-8859-5" "\0"
- "CP916" "\0" "ISO-8859-8" "\0"
- "CP920" "\0" "ISO-8859-9" "\0"
- "CP921" "\0" "ISO-8859-13" "\0"
- "CP923" "\0" "ISO-8859-15" "\0"
- "CP954" "\0" "EUC-JP" "\0"
- "CP964" "\0" "EUC-TW" "\0"
- "CP970" "\0" "EUC-KR" "\0"
- "CP1089" "\0" "ISO-8859-6" "\0"
- "CP1208" "\0" "UTF-8" "\0"
- "CP1381" "\0" "GB2312" "\0"
- "CP1386" "\0" "GBK" "\0"
- "CP3372" "\0" "EUC-JP" "\0";
- # endif
- #endif
- charset_aliases = cp;
- }
- return cp;
- }
- #ifdef STATIC
- STATIC
- #endif
- const char *
- locale_charset (void)
- {
- const char *codeset;
- const char *aliases;
- #if !(defined WINDOWS_NATIVE || defined OS2)
- # if HAVE_LANGINFO_CODESET
-
- codeset = nl_langinfo (CODESET);
- # ifdef __CYGWIN__
-
- if (codeset != NULL && strcmp (codeset, "US-ASCII") == 0)
- {
- const char *locale;
- static char buf[2 + 10 + 1];
- locale = getenv ("LC_ALL");
- if (locale == NULL || locale[0] == '\0')
- {
- locale = getenv ("LC_CTYPE");
- if (locale == NULL || locale[0] == '\0')
- locale = getenv ("LANG");
- }
- if (locale != NULL && locale[0] != '\0')
- {
-
- const char *dot = strchr (locale, '.');
- if (dot != NULL)
- {
- const char *modifier;
- dot++;
-
- modifier = strchr (dot, '@');
- if (modifier == NULL)
- return dot;
- if (modifier - dot < sizeof (buf))
- {
- memcpy (buf, dot, modifier - dot);
- buf [modifier - dot] = '\0';
- return buf;
- }
- }
- }
-
- sprintf (buf, "CP%u", GetACP ());
- codeset = buf;
- }
- # endif
- # else
-
- const char *locale = NULL;
-
- # if 0
- locale = setlocale (LC_CTYPE, NULL);
- # endif
- if (locale == NULL || locale[0] == '\0')
- {
- locale = getenv ("LC_ALL");
- if (locale == NULL || locale[0] == '\0')
- {
- locale = getenv ("LC_CTYPE");
- if (locale == NULL || locale[0] == '\0')
- locale = getenv ("LANG");
- }
- }
-
- codeset = locale;
- # endif
- #elif defined WINDOWS_NATIVE
- static char buf[2 + 10 + 1];
-
- char *current_locale = setlocale (LC_ALL, NULL);
- char *pdot;
-
- if (strchr (current_locale, ';'))
- current_locale = setlocale (LC_CTYPE, NULL);
- pdot = strrchr (current_locale, '.');
- if (pdot && 2 + strlen (pdot + 1) + 1 <= sizeof (buf))
- sprintf (buf, "CP%s", pdot + 1);
- else
- {
-
- sprintf (buf, "CP%u", GetACP ());
- }
- codeset = buf;
- #elif defined OS2
- const char *locale;
- static char buf[2 + 10 + 1];
- ULONG cp[3];
- ULONG cplen;
- codeset = NULL;
-
- locale = getenv ("LC_ALL");
- if (locale == NULL || locale[0] == '\0')
- {
- locale = getenv ("LC_CTYPE");
- if (locale == NULL || locale[0] == '\0')
- locale = getenv ("LANG");
- }
- if (locale != NULL && locale[0] != '\0')
- {
-
- const char *dot = strchr (locale, '.');
- if (dot != NULL)
- {
- const char *modifier;
- dot++;
-
- modifier = strchr (dot, '@');
- if (modifier == NULL)
- return dot;
- if (modifier - dot < sizeof (buf))
- {
- memcpy (buf, dot, modifier - dot);
- buf [modifier - dot] = '\0';
- return buf;
- }
- }
-
- if (strcmp (locale, "C") == 0 || strcmp (locale, "POSIX") == 0)
- codeset = "";
- }
- if (codeset == NULL)
- {
-
- if (DosQueryCp (sizeof (cp), cp, &cplen))
- codeset = "";
- else
- {
- sprintf (buf, "CP%u", cp[0]);
- codeset = buf;
- }
- }
- #endif
- if (codeset == NULL)
-
- codeset = "";
-
- for (aliases = get_charset_aliases ();
- *aliases != '\0';
- aliases += strlen (aliases) + 1, aliases += strlen (aliases) + 1)
- if (strcmp (codeset, aliases) == 0
- || (aliases[0] == '*' && aliases[1] == '\0'))
- {
- codeset = aliases + strlen (aliases) + 1;
- break;
- }
-
- if (codeset[0] == '\0')
- codeset = "ASCII";
- #ifdef DARWIN7
-
- if (strcmp (codeset, "UTF-8") == 0 && MB_CUR_MAX_L (uselocale (NULL)) <= 1)
- codeset = "ASCII";
- #endif
- return codeset;
- }
- const char *
- environ_locale_charset (void)
- {
- static char buf[2 + 10 + 1];
- const char *codeset, *aliases;
- const char *locale = NULL;
- locale = getenv ("LC_ALL");
- if (locale == NULL || locale[0] == '\0')
- {
- locale = getenv ("LC_CTYPE");
- if (locale == NULL || locale[0] == '\0')
- locale = getenv ("LANG");
- }
- if (locale != NULL && locale[0] != '\0')
- {
-
- const char *dot = strchr (locale, '.');
- if (dot != NULL)
- {
- const char *modifier;
- dot++;
-
- modifier = strchr (dot, '@');
- if (modifier == NULL)
- return dot;
- if (modifier - dot < sizeof (buf))
- {
- memcpy (buf, dot, modifier - dot);
- buf [modifier - dot] = '\0';
- return buf;
- }
- }
- else if (strcmp (locale, "C") == 0)
- {
- strcpy (buf, "ASCII");
- return buf;
- }
- else
- codeset = "";
- }
- else
- codeset = "";
-
- for (aliases = get_charset_aliases ();
- *aliases != '\0';
- aliases += strlen (aliases) + 1, aliases += strlen (aliases) + 1)
- if (strcmp (codeset, aliases) == 0
- || (aliases[0] == '*' && aliases[1] == '\0'))
- {
- codeset = aliases + strlen (aliases) + 1;
- break;
- }
-
- if (codeset[0] == '\0')
-
- codeset = "ISO-8859-1";
- return codeset;
- }
|