localcharset.c 12 KB

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