localcharset.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /* Determine a canonical name for the current locale's character encoding.
  2. Copyright (C) 2000-2006, 2008 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with this program; if not, write to the Free Software Foundation,
  13. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  14. /* Written by Bruno Haible <bruno@clisp.org>. */
  15. #include <config.h>
  16. /* Specification. */
  17. #include "localcharset.h"
  18. #include <stddef.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #if defined _WIN32 || defined __WIN32__
  23. # define WIN32_NATIVE
  24. #endif
  25. #if defined __EMX__
  26. /* Assume EMX program runs on OS/2, even if compiled under DOS. */
  27. # ifndef OS2
  28. # define OS2
  29. # endif
  30. #endif
  31. #if !defined WIN32_NATIVE
  32. # if HAVE_LANGINFO_CODESET
  33. # include <langinfo.h>
  34. # else
  35. # if 0 /* see comment below */
  36. # include <locale.h>
  37. # endif
  38. # endif
  39. # ifdef __CYGWIN__
  40. # define WIN32_LEAN_AND_MEAN
  41. # include <windows.h>
  42. # endif
  43. #elif defined WIN32_NATIVE
  44. # define WIN32_LEAN_AND_MEAN
  45. # include <windows.h>
  46. #endif
  47. #if defined OS2
  48. # define INCL_DOS
  49. # include <os2.h>
  50. #endif
  51. #if ENABLE_RELOCATABLE
  52. # include "relocatable.h"
  53. #else
  54. # define relocate(pathname) (pathname)
  55. #endif
  56. /* Get LIBDIR. */
  57. #ifndef LIBDIR
  58. # include "configmake.h"
  59. #endif
  60. #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__
  61. /* Win32, Cygwin, OS/2, DOS */
  62. # define ISSLASH(C) ((C) == '/' || (C) == '\\')
  63. #endif
  64. #ifndef DIRECTORY_SEPARATOR
  65. # define DIRECTORY_SEPARATOR '/'
  66. #endif
  67. #ifndef ISSLASH
  68. # define ISSLASH(C) ((C) == DIRECTORY_SEPARATOR)
  69. #endif
  70. #if HAVE_DECL_GETC_UNLOCKED
  71. # undef getc
  72. # define getc getc_unlocked
  73. #endif
  74. /* The following static variable is declared 'volatile' to avoid a
  75. possible multithread problem in the function get_charset_aliases. If we
  76. are running in a threaded environment, and if two threads initialize
  77. 'charset_aliases' simultaneously, both will produce the same value,
  78. and everything will be ok if the two assignments to 'charset_aliases'
  79. are atomic. But I don't know what will happen if the two assignments mix. */
  80. #if __STDC__ != 1
  81. # define volatile /* empty */
  82. #endif
  83. /* Pointer to the contents of the charset.alias file, if it has already been
  84. read, else NULL. Its format is:
  85. ALIAS_1 '\0' CANONICAL_1 '\0' ... ALIAS_n '\0' CANONICAL_n '\0' '\0' */
  86. static const char * volatile charset_aliases;
  87. /* Return a pointer to the contents of the charset.alias file. */
  88. static const char *
  89. get_charset_aliases (void)
  90. {
  91. const char *cp;
  92. cp = charset_aliases;
  93. if (cp == NULL)
  94. {
  95. #if !(defined VMS || defined WIN32_NATIVE || defined __CYGWIN__)
  96. FILE *fp;
  97. const char *dir;
  98. const char *base = "charset.alias";
  99. char *file_name;
  100. /* Make it possible to override the charset.alias location. This is
  101. necessary for running the testsuite before "make install". */
  102. dir = getenv ("CHARSETALIASDIR");
  103. if (dir == NULL || dir[0] == '\0')
  104. dir = relocate (LIBDIR);
  105. /* Concatenate dir and base into freshly allocated file_name. */
  106. {
  107. size_t dir_len = strlen (dir);
  108. size_t base_len = strlen (base);
  109. int add_slash = (dir_len > 0 && !ISSLASH (dir[dir_len - 1]));
  110. file_name = (char *) malloc (dir_len + add_slash + base_len + 1);
  111. if (file_name != NULL)
  112. {
  113. memcpy (file_name, dir, dir_len);
  114. if (add_slash)
  115. file_name[dir_len] = DIRECTORY_SEPARATOR;
  116. memcpy (file_name + dir_len + add_slash, base, base_len + 1);
  117. }
  118. }
  119. if (file_name == NULL || (fp = fopen (file_name, "r")) == NULL)
  120. /* Out of memory or file not found, treat it as empty. */
  121. cp = "";
  122. else
  123. {
  124. /* Parse the file's contents. */
  125. char *res_ptr = NULL;
  126. size_t res_size = 0;
  127. for (;;)
  128. {
  129. int c;
  130. char buf1[50+1];
  131. char buf2[50+1];
  132. size_t l1, l2;
  133. char *old_res_ptr;
  134. c = getc (fp);
  135. if (c == EOF)
  136. break;
  137. if (c == '\n' || c == ' ' || c == '\t')
  138. continue;
  139. if (c == '#')
  140. {
  141. /* Skip comment, to end of line. */
  142. do
  143. c = getc (fp);
  144. while (!(c == EOF || c == '\n'));
  145. if (c == EOF)
  146. break;
  147. continue;
  148. }
  149. ungetc (c, fp);
  150. if (fscanf (fp, "%50s %50s", buf1, buf2) < 2)
  151. break;
  152. l1 = strlen (buf1);
  153. l2 = strlen (buf2);
  154. old_res_ptr = res_ptr;
  155. if (res_size == 0)
  156. {
  157. res_size = l1 + 1 + l2 + 1;
  158. res_ptr = (char *) malloc (res_size + 1);
  159. }
  160. else
  161. {
  162. res_size += l1 + 1 + l2 + 1;
  163. res_ptr = (char *) realloc (res_ptr, res_size + 1);
  164. }
  165. if (res_ptr == NULL)
  166. {
  167. /* Out of memory. */
  168. res_size = 0;
  169. if (old_res_ptr != NULL)
  170. free (old_res_ptr);
  171. break;
  172. }
  173. strcpy (res_ptr + res_size - (l2 + 1) - (l1 + 1), buf1);
  174. strcpy (res_ptr + res_size - (l2 + 1), buf2);
  175. }
  176. fclose (fp);
  177. if (res_size == 0)
  178. cp = "";
  179. else
  180. {
  181. *(res_ptr + res_size) = '\0';
  182. cp = res_ptr;
  183. }
  184. }
  185. if (file_name != NULL)
  186. free (file_name);
  187. #else
  188. # if defined VMS
  189. /* To avoid the troubles of an extra file charset.alias_vms in the
  190. sources of many GNU packages, simply inline the aliases here. */
  191. /* The list of encodings is taken from the OpenVMS 7.3-1 documentation
  192. "Compaq C Run-Time Library Reference Manual for OpenVMS systems"
  193. section 10.7 "Handling Different Character Sets". */
  194. cp = "ISO8859-1" "\0" "ISO-8859-1" "\0"
  195. "ISO8859-2" "\0" "ISO-8859-2" "\0"
  196. "ISO8859-5" "\0" "ISO-8859-5" "\0"
  197. "ISO8859-7" "\0" "ISO-8859-7" "\0"
  198. "ISO8859-8" "\0" "ISO-8859-8" "\0"
  199. "ISO8859-9" "\0" "ISO-8859-9" "\0"
  200. /* Japanese */
  201. "eucJP" "\0" "EUC-JP" "\0"
  202. "SJIS" "\0" "SHIFT_JIS" "\0"
  203. "DECKANJI" "\0" "DEC-KANJI" "\0"
  204. "SDECKANJI" "\0" "EUC-JP" "\0"
  205. /* Chinese */
  206. "eucTW" "\0" "EUC-TW" "\0"
  207. "DECHANYU" "\0" "DEC-HANYU" "\0"
  208. "DECHANZI" "\0" "GB2312" "\0"
  209. /* Korean */
  210. "DECKOREAN" "\0" "EUC-KR" "\0";
  211. # endif
  212. # if defined WIN32_NATIVE || defined __CYGWIN__
  213. /* To avoid the troubles of installing a separate file in the same
  214. directory as the DLL and of retrieving the DLL's directory at
  215. runtime, simply inline the aliases here. */
  216. cp = "CP936" "\0" "GBK" "\0"
  217. "CP1361" "\0" "JOHAB" "\0"
  218. "CP20127" "\0" "ASCII" "\0"
  219. "CP20866" "\0" "KOI8-R" "\0"
  220. "CP20936" "\0" "GB2312" "\0"
  221. "CP21866" "\0" "KOI8-RU" "\0"
  222. "CP28591" "\0" "ISO-8859-1" "\0"
  223. "CP28592" "\0" "ISO-8859-2" "\0"
  224. "CP28593" "\0" "ISO-8859-3" "\0"
  225. "CP28594" "\0" "ISO-8859-4" "\0"
  226. "CP28595" "\0" "ISO-8859-5" "\0"
  227. "CP28596" "\0" "ISO-8859-6" "\0"
  228. "CP28597" "\0" "ISO-8859-7" "\0"
  229. "CP28598" "\0" "ISO-8859-8" "\0"
  230. "CP28599" "\0" "ISO-8859-9" "\0"
  231. "CP28605" "\0" "ISO-8859-15" "\0"
  232. "CP38598" "\0" "ISO-8859-8" "\0"
  233. "CP51932" "\0" "EUC-JP" "\0"
  234. "CP51936" "\0" "GB2312" "\0"
  235. "CP51949" "\0" "EUC-KR" "\0"
  236. "CP51950" "\0" "EUC-TW" "\0"
  237. "CP54936" "\0" "GB18030" "\0"
  238. "CP65001" "\0" "UTF-8" "\0";
  239. # endif
  240. #endif
  241. charset_aliases = cp;
  242. }
  243. return cp;
  244. }
  245. /* Determine the current locale's character encoding, and canonicalize it
  246. into one of the canonical names listed in config.charset.
  247. The result must not be freed; it is statically allocated.
  248. If the canonical name cannot be determined, the result is a non-canonical
  249. name. */
  250. #ifdef STATIC
  251. STATIC
  252. #endif
  253. const char *
  254. locale_charset (void)
  255. {
  256. const char *codeset;
  257. const char *aliases;
  258. #if !(defined WIN32_NATIVE || defined OS2)
  259. # if HAVE_LANGINFO_CODESET
  260. /* Most systems support nl_langinfo (CODESET) nowadays. */
  261. codeset = nl_langinfo (CODESET);
  262. # ifdef __CYGWIN__
  263. /* Cygwin 2006 does not have locales. nl_langinfo (CODESET) always
  264. returns "US-ASCII". As long as this is not fixed, return the suffix
  265. of the locale name from the environment variables (if present) or
  266. the codepage as a number. */
  267. if (codeset != NULL && strcmp (codeset, "US-ASCII") == 0)
  268. {
  269. const char *locale;
  270. static char buf[2 + 10 + 1];
  271. locale = getenv ("LC_ALL");
  272. if (locale == NULL || locale[0] == '\0')
  273. {
  274. locale = getenv ("LC_CTYPE");
  275. if (locale == NULL || locale[0] == '\0')
  276. locale = getenv ("LANG");
  277. }
  278. if (locale != NULL && locale[0] != '\0')
  279. {
  280. /* If the locale name contains an encoding after the dot, return
  281. it. */
  282. const char *dot = strchr (locale, '.');
  283. if (dot != NULL)
  284. {
  285. const char *modifier;
  286. dot++;
  287. /* Look for the possible @... trailer and remove it, if any. */
  288. modifier = strchr (dot, '@');
  289. if (modifier == NULL)
  290. return dot;
  291. if (modifier - dot < sizeof (buf))
  292. {
  293. memcpy (buf, dot, modifier - dot);
  294. buf [modifier - dot] = '\0';
  295. return buf;
  296. }
  297. }
  298. }
  299. /* Woe32 has a function returning the locale's codepage as a number. */
  300. sprintf (buf, "CP%u", GetACP ());
  301. codeset = buf;
  302. }
  303. # endif
  304. # else
  305. /* On old systems which lack it, use setlocale or getenv. */
  306. const char *locale = NULL;
  307. /* But most old systems don't have a complete set of locales. Some
  308. (like SunOS 4 or DJGPP) have only the C locale. Therefore we don't
  309. use setlocale here; it would return "C" when it doesn't support the
  310. locale name the user has set. */
  311. # if 0
  312. locale = setlocale (LC_CTYPE, NULL);
  313. # endif
  314. if (locale == NULL || locale[0] == '\0')
  315. {
  316. locale = getenv ("LC_ALL");
  317. if (locale == NULL || locale[0] == '\0')
  318. {
  319. locale = getenv ("LC_CTYPE");
  320. if (locale == NULL || locale[0] == '\0')
  321. locale = getenv ("LANG");
  322. }
  323. }
  324. /* On some old systems, one used to set locale = "iso8859_1". On others,
  325. you set it to "language_COUNTRY.charset". In any case, we resolve it
  326. through the charset.alias file. */
  327. codeset = locale;
  328. # endif
  329. #elif defined WIN32_NATIVE
  330. static char buf[2 + 10 + 1];
  331. /* Woe32 has a function returning the locale's codepage as a number. */
  332. sprintf (buf, "CP%u", GetACP ());
  333. codeset = buf;
  334. #elif defined OS2
  335. const char *locale;
  336. static char buf[2 + 10 + 1];
  337. ULONG cp[3];
  338. ULONG cplen;
  339. /* Allow user to override the codeset, as set in the operating system,
  340. with standard language environment variables. */
  341. locale = getenv ("LC_ALL");
  342. if (locale == NULL || locale[0] == '\0')
  343. {
  344. locale = getenv ("LC_CTYPE");
  345. if (locale == NULL || locale[0] == '\0')
  346. locale = getenv ("LANG");
  347. }
  348. if (locale != NULL && locale[0] != '\0')
  349. {
  350. /* If the locale name contains an encoding after the dot, return it. */
  351. const char *dot = strchr (locale, '.');
  352. if (dot != NULL)
  353. {
  354. const char *modifier;
  355. dot++;
  356. /* Look for the possible @... trailer and remove it, if any. */
  357. modifier = strchr (dot, '@');
  358. if (modifier == NULL)
  359. return dot;
  360. if (modifier - dot < sizeof (buf))
  361. {
  362. memcpy (buf, dot, modifier - dot);
  363. buf [modifier - dot] = '\0';
  364. return buf;
  365. }
  366. }
  367. /* Resolve through the charset.alias file. */
  368. codeset = locale;
  369. }
  370. else
  371. {
  372. /* OS/2 has a function returning the locale's codepage as a number. */
  373. if (DosQueryCp (sizeof (cp), cp, &cplen))
  374. codeset = "";
  375. else
  376. {
  377. sprintf (buf, "CP%u", cp[0]);
  378. codeset = buf;
  379. }
  380. }
  381. #endif
  382. if (codeset == NULL)
  383. /* The canonical name cannot be determined. */
  384. codeset = "";
  385. /* Resolve alias. */
  386. for (aliases = get_charset_aliases ();
  387. *aliases != '\0';
  388. aliases += strlen (aliases) + 1, aliases += strlen (aliases) + 1)
  389. if (strcmp (codeset, aliases) == 0
  390. || (aliases[0] == '*' && aliases[1] == '\0'))
  391. {
  392. codeset = aliases + strlen (aliases) + 1;
  393. break;
  394. }
  395. /* Don't return an empty string. GNU libc and GNU libiconv interpret
  396. the empty string as denoting "the locale's character encoding",
  397. thus GNU libiconv would call this function a second time. */
  398. if (codeset[0] == '\0')
  399. codeset = "ASCII";
  400. return codeset;
  401. }