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