1 /* localcharset.c - Determine a canonical name for the current locale's character encoding. */
3 /* Copyright (C) 2000-2003, 2005-2009 Free Software Foundation, Inc.
5 This file is part of GNU Bush.
7 Bush is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bush is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bush. If not, see <http://www.gnu.org/licenses/>.
21 /* Written by Bruno Haible <bruno@clisp.org>. */
28 #include "localcharset.h"
44 #if defined _WIN32 || defined __WIN32__
45 # undef WIN32 /* avoid warning on mingw32 */
50 /* Assume EMX program runs on OS/2, even if compiled under DOS. */
55 # if HAVE_LANGINFO_CODESET
56 # include <langinfo.h>
63 # define WIN32_LEAN_AND_MEAN
71 #if ENABLE_RELOCATABLE
72 # include "relocatable.h"
74 # define relocate(pathname) (pathname)
77 #if defined _WIN32 || defined __WIN32__ || defined __EMX__ || defined __DJGPP__
78 /* Win32, OS/2, DOS */
79 # define ISSLASH(C) ((C) == '/' || (C) == '\\')
82 #ifndef DIRECTORY_SEPARATOR
83 # define DIRECTORY_SEPARATOR '/'
87 # define ISSLASH(C) ((C) == DIRECTORY_SEPARATOR)
90 #ifdef HAVE_GETC_UNLOCKED
92 # define getc getc_unlocked
95 /* The following static variable is declared 'volatile' to avoid a
96 possible multithread problem in the function get_charset_aliases. If we
97 are running in a threaded environment, and if two threads initialize
98 'charset_aliases' simultaneously, both will produce the same value,
99 and everything will be ok if the two assignments to 'charset_aliases'
100 are atomic. But I don't know what will happen if the two assignments mix. */
102 # define volatile /* empty */
104 /* Pointer to the contents of the charset.alias file, if it has already been
105 read, else NULL. Its format is:
106 ALIAS_1 '\0' CANONICAL_1 '\0' ... ALIAS_n '\0' CANONICAL_n '\0' '\0' */
107 static const char * volatile charset_aliases
;
109 /* Return a pointer to the contents of the charset.alias file. */
111 get_charset_aliases ()
115 cp
= charset_aliases
;
118 #if !(defined VMS || defined WIN32)
120 const char *dir
= relocate (LIBDIR
);
121 const char *base
= "charset.alias";
124 /* Concatenate dir and base into freshly allocated file_name. */
126 size_t dir_len
= strlen (dir
);
127 size_t base_len
= strlen (base
);
128 int add_slash
= (dir_len
> 0 && !ISSLASH (dir
[dir_len
- 1]));
129 file_name
= (char *) malloc (dir_len
+ add_slash
+ base_len
+ 1);
130 if (file_name
!= NULL
)
132 memcpy (file_name
, dir
, dir_len
);
134 file_name
[dir_len
] = DIRECTORY_SEPARATOR
;
135 memcpy (file_name
+ dir_len
+ add_slash
, base
, base_len
+ 1);
139 if (file_name
== NULL
|| (fp
= fopen (file_name
, "r")) == NULL
)
140 /* Out of memory or file not found, treat it as empty. */
144 /* Parse the file's contents. */
148 char *res_ptr
= NULL
;
157 if (c
== '\n' || c
== ' ' || c
== '\t')
161 /* Skip comment, to end of line. */
164 while (!(c
== EOF
|| c
== '\n'));
170 if (fscanf (fp
, "%50s %50s", buf1
, buf2
) < 2)
176 res_size
= l1
+ 1 + l2
+ 1;
177 res_ptr
= (char *) malloc (res_size
+ 1);
181 res_size
+= l1
+ 1 + l2
+ 1;
182 res_ptr
= (char *) realloc (res_ptr
, res_size
+ 1);
190 strcpy (res_ptr
+ res_size
- (l2
+ 1) - (l1
+ 1), buf1
);
191 strcpy (res_ptr
+ res_size
- (l2
+ 1), buf2
);
198 *(res_ptr
+ res_size
) = '\0';
203 if (file_name
!= NULL
)
209 /* To avoid the troubles of an extra file charset.alias_vms in the
210 sources of many GNU packages, simply inline the aliases here. */
211 /* The list of encodings is taken from the OpenVMS 7.3-1 documentation
212 "Compaq C Run-Time Library Reference Manual for OpenVMS systems"
213 section 10.7 "Handling Different Character Sets". */
214 cp
= "ISO8859-1" "\0" "ISO-8859-1" "\0"
215 "ISO8859-2" "\0" "ISO-8859-2" "\0"
216 "ISO8859-5" "\0" "ISO-8859-5" "\0"
217 "ISO8859-7" "\0" "ISO-8859-7" "\0"
218 "ISO8859-8" "\0" "ISO-8859-8" "\0"
219 "ISO8859-9" "\0" "ISO-8859-9" "\0"
221 "eucJP" "\0" "EUC-JP" "\0"
222 "SJIS" "\0" "SHIFT_JIS" "\0"
223 "DECKANJI" "\0" "DEC-KANJI" "\0"
224 "SDECKANJI" "\0" "EUC-JP" "\0"
226 "eucTW" "\0" "EUC-TW" "\0"
227 "DECHANYU" "\0" "DEC-HANYU" "\0"
228 "DECHANZI" "\0" "GB2312" "\0"
230 "DECKOREAN" "\0" "EUC-KR" "\0";
234 /* To avoid the troubles of installing a separate file in the same
235 directory as the DLL and of retrieving the DLL's directory at
236 runtime, simply inline the aliases here. */
238 cp
= "CP936" "\0" "GBK" "\0"
239 "CP1361" "\0" "JOHAB" "\0"
240 "CP20127" "\0" "ASCII" "\0"
241 "CP20866" "\0" "KOI8-R" "\0"
242 "CP21866" "\0" "KOI8-RU" "\0"
243 "CP28591" "\0" "ISO-8859-1" "\0"
244 "CP28592" "\0" "ISO-8859-2" "\0"
245 "CP28593" "\0" "ISO-8859-3" "\0"
246 "CP28594" "\0" "ISO-8859-4" "\0"
247 "CP28595" "\0" "ISO-8859-5" "\0"
248 "CP28596" "\0" "ISO-8859-6" "\0"
249 "CP28597" "\0" "ISO-8859-7" "\0"
250 "CP28598" "\0" "ISO-8859-8" "\0"
251 "CP28599" "\0" "ISO-8859-9" "\0"
252 "CP28605" "\0" "ISO-8859-15" "\0";
256 charset_aliases
= cp
;
262 /* Determine the current locale's character encoding, and canonicalize it
263 into one of the canonical names listed in config.charset.
264 The result must not be freed; it is statically allocated.
265 If the canonical name cannot be determined, the result is a non-canonical
277 #if !(defined WIN32 || defined OS2)
279 # if HAVE_LANGINFO_CODESET
281 /* Most systems support nl_langinfo (CODESET) nowadays. */
282 codeset
= nl_langinfo (CODESET
);
286 /* On old systems which lack it, use setlocale or getenv. */
287 const char *locale
= NULL
;
289 /* But most old systems don't have a complete set of locales. Some
290 (like SunOS 4 or DJGPP) have only the C locale. Therefore we don't
291 use setlocale here; it would return "C" when it doesn't support the
292 locale name the user has set. */
293 # if HAVE_SETLOCALE && 0
294 locale
= setlocale (LC_CTYPE
, NULL
);
296 if (locale
== NULL
|| locale
[0] == '\0')
298 locale
= getenv ("LC_ALL");
299 if (locale
== NULL
|| locale
[0] == '\0')
301 locale
= getenv ("LC_CTYPE");
302 if (locale
== NULL
|| locale
[0] == '\0')
303 locale
= getenv ("LANG");
307 /* On some old systems, one used to set locale = "iso8859_1". On others,
308 you set it to "language_COUNTRY.charset". In any case, we resolve it
309 through the charset.alias file. */
316 static char buf
[2 + 10 + 1];
318 /* Woe32 has a function returning the locale's codepage as a number. */
319 sprintf (buf
, "CP%u", GetACP ());
325 static char buf
[2 + 10 + 1];
329 /* Allow user to override the codeset, as set in the operating system,
330 with standard language environment variables. */
331 locale
= getenv ("LC_ALL");
332 if (locale
== NULL
|| locale
[0] == '\0')
334 locale
= getenv ("LC_CTYPE");
335 if (locale
== NULL
|| locale
[0] == '\0')
336 locale
= getenv ("LANG");
338 if (locale
!= NULL
&& locale
[0] != '\0')
340 /* If the locale name contains an encoding after the dot, return it. */
341 const char *dot
= strchr (locale
, '.');
345 const char *modifier
;
348 /* Look for the possible @... trailer and remove it, if any. */
349 modifier
= strchr (dot
, '@');
350 if (modifier
== NULL
)
352 if (modifier
- dot
< sizeof (buf
))
354 memcpy (buf
, dot
, modifier
- dot
);
355 buf
[modifier
- dot
] = '\0';
360 /* Resolve through the charset.alias file. */
365 /* OS/2 has a function returning the locale's codepage as a number. */
366 if (DosQueryCp (sizeof (cp
), cp
, &cplen
))
370 sprintf (buf
, "CP%u", cp
[0]);
378 /* The canonical name cannot be determined. */
382 for (aliases
= get_charset_aliases ();
384 aliases
+= strlen (aliases
) + 1, aliases
+= strlen (aliases
) + 1)
385 if (strcmp (codeset
, aliases
) == 0
386 || (aliases
[0] == '*' && aliases
[1] == '\0'))
388 codeset
= aliases
+ strlen (aliases
) + 1;
392 /* Don't return an empty string. GNU libc and GNU libiconv interpret
393 the empty string as denoting "the locale's character encoding",
394 thus GNU libiconv would call this function a second time. */
395 if (codeset
[0] == '\0')