1 /* setlocale() function that respects the locale chosen by the user.
2 Copyright (C) 2009, 2015-2016 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2009.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 /* Override setlocale() and newlocale() so that when the default locale is
23 requested (locale = "") and no relevant environment variable is set, the
24 locale chosen by the user is used.
25 This matters on MacOS X 10 and Windows.
26 See the comments in localename.c, function gl_locale_name_default. */
27
28 #include <locale.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 /* When building a DLL, we must export some functions. Note that because
33 the functions are only defined for binary backward compatibility, we
34 don't need to use __declspec(dllimport) in any case. */
35 #if HAVE_VISIBILITY && BUILDING_DLL
36 # define DLL_EXPORTED __attribute__((__visibility__("default")))
37 #elif defined _MSC_VER && BUILDING_DLL
38 # define DLL_EXPORTED __declspec(dllexport)
39 #else
40 # define DLL_EXPORTED
41 #endif
42
43 #include "gettextP.h"
44
45 #if (defined __APPLE__ && defined __MACH__) || defined _WIN32 || defined __WIN32__ || defined __CYGWIN__
46
47 # undef setlocale
48 # undef newlocale
49
50 /* Return string representation of locale category CATEGORY. */
51 static const char *
category_to_name(int category)52 category_to_name (int category)
53 {
54 const char *retval;
55
56 switch (category)
57 {
58 case LC_COLLATE:
59 retval = "LC_COLLATE";
60 break;
61 case LC_CTYPE:
62 retval = "LC_CTYPE";
63 break;
64 case LC_MONETARY:
65 retval = "LC_MONETARY";
66 break;
67 case LC_NUMERIC:
68 retval = "LC_NUMERIC";
69 break;
70 case LC_TIME:
71 retval = "LC_TIME";
72 break;
73 case LC_MESSAGES:
74 retval = "LC_MESSAGES";
75 break;
76 default:
77 /* If you have a better idea for a default value let me know. */
78 retval = "LC_XXX";
79 }
80
81 return retval;
82 }
83
84 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
85
86 /* The native Win32 setlocale() function expects locale names of the form
87 "German" or "German_Germany" or "DEU", but not "de" or "de_DE". We need
88 to convert the names from the form with ISO 639 language code and ISO 3166
89 country code to the form with English names or with three-letter identifier.
90 The three-letter identifiers known by a Windows XP SP2 or SP3 are:
91 AFK Afrikaans_South Africa.1252
92 ARA Arabic_Saudi Arabia.1256
93 ARB Arabic_Lebanon.1256
94 ARE Arabic_Egypt.1256
95 ARG Arabic_Algeria.1256
96 ARH Arabic_Bahrain.1256
97 ARI Arabic_Iraq.1256
98 ARJ Arabic_Jordan.1256
99 ARK Arabic_Kuwait.1256
100 ARL Arabic_Libya.1256
101 ARM Arabic_Morocco.1256
102 ARO Arabic_Oman.1256
103 ARQ Arabic_Qatar.1256
104 ARS Arabic_Syria.1256
105 ART Arabic_Tunisia.1256
106 ARU Arabic_U.A.E..1256
107 ARY Arabic_Yemen.1256
108 AZE Azeri (Latin)_Azerbaijan.1254
109 BEL Belarusian_Belarus.1251
110 BGR Bulgarian_Bulgaria.1251
111 BSB Bosnian_Bosnia and Herzegovina.1250
112 BSC Bosnian (Cyrillic)_Bosnia and Herzegovina.1250 (wrong encoding!)
113 CAT Catalan_Spain.1252
114 CHH Chinese_Hong Kong S.A.R..950
115 CHI Chinese_Singapore.936
116 CHS Chinese_People's Republic of China.936
117 CHT Chinese_Taiwan.950
118 CSY Czech_Czech Republic.1250
119 CYM Welsh_United Kingdom.1252
120 DAN Danish_Denmark.1252
121 DEA German_Austria.1252
122 DEC German_Liechtenstein.1252
123 DEL German_Luxembourg.1252
124 DES German_Switzerland.1252
125 DEU German_Germany.1252
126 ELL Greek_Greece.1253
127 ENA English_Australia.1252
128 ENB English_Caribbean.1252
129 ENC English_Canada.1252
130 ENG English_United Kingdom.1252
131 ENI English_Ireland.1252
132 ENJ English_Jamaica.1252
133 ENL English_Belize.1252
134 ENP English_Republic of the Philippines.1252
135 ENS English_South Africa.1252
136 ENT English_Trinidad and Tobago.1252
137 ENU English_United States.1252
138 ENW English_Zimbabwe.1252
139 ENZ English_New Zealand.1252
140 ESA Spanish_Panama.1252
141 ESB Spanish_Bolivia.1252
142 ESC Spanish_Costa Rica.1252
143 ESD Spanish_Dominican Republic.1252
144 ESE Spanish_El Salvador.1252
145 ESF Spanish_Ecuador.1252
146 ESG Spanish_Guatemala.1252
147 ESH Spanish_Honduras.1252
148 ESI Spanish_Nicaragua.1252
149 ESL Spanish_Chile.1252
150 ESM Spanish_Mexico.1252
151 ESN Spanish_Spain.1252
152 ESO Spanish_Colombia.1252
153 ESP Spanish_Spain.1252
154 ESR Spanish_Peru.1252
155 ESS Spanish_Argentina.1252
156 ESU Spanish_Puerto Rico.1252
157 ESV Spanish_Venezuela.1252
158 ESY Spanish_Uruguay.1252
159 ESZ Spanish_Paraguay.1252
160 ETI Estonian_Estonia.1257
161 EUQ Basque_Spain.1252
162 FAR Farsi_Iran.1256
163 FIN Finnish_Finland.1252
164 FOS Faroese_Faroe Islands.1252
165 FPO Filipino_Philippines.1252
166 FRA French_France.1252
167 FRB French_Belgium.1252
168 FRC French_Canada.1252
169 FRL French_Luxembourg.1252
170 FRM French_Principality of Monaco.1252
171 FRS French_Switzerland.1252
172 FYN Frisian_Netherlands.1252
173 GLC Galician_Spain.1252
174 HEB Hebrew_Israel.1255
175 HRB Croatian_Bosnia and Herzegovina.1250
176 HRV Croatian_Croatia.1250
177 HUN Hungarian_Hungary.1250
178 IND Indonesian_Indonesia.1252
179 IRE Irish_Ireland.1252
180 ISL Icelandic_Iceland.1252
181 ITA Italian_Italy.1252
182 ITS Italian_Switzerland.1252
183 IUK Inuktitut (Latin)_Canada.1252
184 JPN Japanese_Japan.932
185 KKZ Kazakh_Kazakhstan.1251
186 KOR Korean_Korea.949
187 KYR Kyrgyz_Kyrgyzstan.1251
188 LBX Luxembourgish_Luxembourg.1252
189 LTH Lithuanian_Lithuania.1257
190 LVI Latvian_Latvia.1257
191 MKI FYRO Macedonian_Former Yugoslav Republic of Macedonia.1251
192 MON Mongolian_Mongolia.1251
193 MPD Mapudungun_Chile.1252
194 MSB Malay_Brunei Darussalam.1252
195 MSL Malay_Malaysia.1252
196 MWK Mohawk_Canada.1252
197 NLB Dutch_Belgium.1252
198 NLD Dutch_Netherlands.1252
199 NON Norwegian-Nynorsk_Norway.1252
200 NOR Norwegian (Bokmål)_Norway.1252
201 NSO Northern Sotho_South Africa.1252
202 PLK Polish_Poland.1250
203 PTB Portuguese_Brazil.1252
204 PTG Portuguese_Portugal.1252
205 QUB Quechua_Bolivia.1252
206 QUE Quechua_Ecuador.1252
207 QUP Quechua_Peru.1252
208 RMC Romansh_Switzerland.1252
209 ROM Romanian_Romania.1250
210 RUS Russian_Russia.1251
211 SKY Slovak_Slovakia.1250
212 SLV Slovenian_Slovenia.1250
213 SMA Sami (Southern)_Norway.1252
214 SMB Sami (Southern)_Sweden.1252
215 SME Sami (Northern)_Norway.1252
216 SMF Sami (Northern)_Sweden.1252
217 SMG Sami (Northern)_Finland.1252
218 SMJ Sami (Lule)_Norway.1252
219 SMK Sami (Lule)_Sweden.1252
220 SMN Sami (Inari)_Finland.1252
221 SMS Sami (Skolt)_Finland.1252
222 SQI Albanian_Albania.1250
223 SRB Serbian (Cyrillic)_Serbia and Montenegro.1251
224 SRL Serbian (Latin)_Serbia and Montenegro.1250
225 SRN Serbian (Cyrillic)_Bosnia and Herzegovina.1251
226 SRS Serbian (Latin)_Bosnia and Herzegovina.1250
227 SVE Swedish_Sweden.1252
228 SVF Swedish_Finland.1252
229 SWK Swahili_Kenya.1252
230 THA Thai_Thailand.874
231 TRK Turkish_Turkey.1254
232 TSN Tswana_South Africa.1252
233 TTT Tatar_Russia.1251
234 UKR Ukrainian_Ukraine.1251
235 URD Urdu_Islamic Republic of Pakistan.1256
236 USA English_United States.1252
237 UZB Uzbek (Latin)_Uzbekistan.1254
238 VIT Vietnamese_Viet Nam.1258
239 XHO Xhosa_South Africa.1252
240 ZHH Chinese_Hong Kong S.A.R..950
241 ZHI Chinese_Singapore.936
242 ZHM Chinese_Macau S.A.R..950
243 ZUL Zulu_South Africa.1252
244 */
245
246 /* Table from ISO 639 language code, optionally with country or script suffix,
247 to English name.
248 Keep in sync with the gl_locale_name_from_win32_LANGID function in
249 localename.c! */
250 struct table_entry
251 {
252 const char *code;
253 const char *english;
254 };
255 static const struct table_entry language_table[] =
256 {
257 { "af", "Afrikaans" },
258 { "am", "Amharic" },
259 { "ar", "Arabic" },
260 { "arn", "Mapudungun" },
261 { "as", "Assamese" },
262 { "az@cyrillic", "Azeri (Cyrillic)" },
263 { "az@latin", "Azeri (Latin)" },
264 { "ba", "Bashkir" },
265 { "be", "Belarusian" },
266 { "ber", "Tamazight" },
267 { "ber@arabic", "Tamazight (Arabic)" },
268 { "ber@latin", "Tamazight (Latin)" },
269 { "bg", "Bulgarian" },
270 { "bin", "Edo" },
271 { "bn", "Bengali" },
272 { "bn_BD", "Bengali (Bangladesh)" },
273 { "bn_IN", "Bengali (India)" },
274 { "bnt", "Sutu" },
275 { "bo", "Tibetan" },
276 { "br", "Breton" },
277 { "bs", "BSB" }, /* "Bosnian (Latin)" */
278 { "bs@cyrillic", "BSC" }, /* Bosnian (Cyrillic) */
279 { "ca", "Catalan" },
280 { "chr", "Cherokee" },
281 { "co", "Corsican" },
282 { "cpe", "Hawaiian" },
283 { "cs", "Czech" },
284 { "cy", "Welsh" },
285 { "da", "Danish" },
286 { "de", "German" },
287 { "dsb", "Lower Sorbian" },
288 { "dv", "Divehi" },
289 { "el", "Greek" },
290 { "en", "English" },
291 { "es", "Spanish" },
292 { "et", "Estonian" },
293 { "eu", "Basque" },
294 { "fa", "Farsi" },
295 { "ff", "Fulfulde" },
296 { "fi", "Finnish" },
297 { "fo", "Faroese" }, /* "Faeroese" does not work */
298 { "fr", "French" },
299 { "fy", "Frisian" },
300 { "ga", "IRE" }, /* Gaelic (Ireland) */
301 { "gd", "Gaelic (Scotland)" },
302 { "gd", "Scottish Gaelic" },
303 { "gl", "Galician" },
304 { "gn", "Guarani" },
305 { "gsw", "Alsatian" },
306 { "gu", "Gujarati" },
307 { "ha", "Hausa" },
308 { "he", "Hebrew" },
309 { "hi", "Hindi" },
310 { "hr", "Croatian" },
311 { "hsb", "Upper Sorbian" },
312 { "hu", "Hungarian" },
313 { "hy", "Armenian" },
314 { "id", "Indonesian" },
315 { "ig", "Igbo" },
316 { "ii", "Yi" },
317 { "is", "Icelandic" },
318 { "it", "Italian" },
319 { "iu", "IUK" }, /* Inuktitut */
320 { "ja", "Japanese" },
321 { "ka", "Georgian" },
322 { "kk", "Kazakh" },
323 { "kl", "Greenlandic" },
324 { "km", "Cambodian" },
325 { "km", "Khmer" },
326 { "kn", "Kannada" },
327 { "ko", "Korean" },
328 { "kok", "Konkani" },
329 { "kr", "Kanuri" },
330 { "ks", "Kashmiri" },
331 { "ks_IN", "Kashmiri_India" },
332 { "ks_PK", "Kashmiri (Arabic)_Pakistan" },
333 { "ky", "Kyrgyz" },
334 { "la", "Latin" },
335 { "lb", "Luxembourgish" },
336 { "lo", "Lao" },
337 { "lt", "Lithuanian" },
338 { "lv", "Latvian" },
339 { "mi", "Maori" },
340 { "mk", "FYRO Macedonian" },
341 { "mk", "Macedonian" },
342 { "ml", "Malayalam" },
343 { "mn", "Mongolian" },
344 { "mni", "Manipuri" },
345 { "moh", "Mohawk" },
346 { "mr", "Marathi" },
347 { "ms", "Malay" },
348 { "mt", "Maltese" },
349 { "my", "Burmese" },
350 { "nb", "NOR" }, /* Norwegian Bokmål */
351 { "ne", "Nepali" },
352 { "nic", "Ibibio" },
353 { "nl", "Dutch" },
354 { "nn", "NON" }, /* Norwegian Nynorsk */
355 { "no", "Norwegian" },
356 { "nso", "Northern Sotho" },
357 { "nso", "Sepedi" },
358 { "oc", "Occitan" },
359 { "om", "Oromo" },
360 { "or", "Oriya" },
361 { "pa", "Punjabi" },
362 { "pap", "Papiamentu" },
363 { "pl", "Polish" },
364 { "prs", "Dari" },
365 { "ps", "Pashto" },
366 { "pt", "Portuguese" },
367 { "qu", "Quechua" },
368 { "qut", "K'iche'" },
369 { "rm", "Romansh" },
370 { "ro", "Romanian" },
371 { "ru", "Russian" },
372 { "rw", "Kinyarwanda" },
373 { "sa", "Sanskrit" },
374 { "sah", "Yakut" },
375 { "sd", "Sindhi" },
376 { "se", "Sami (Northern)" },
377 { "se", "Northern Sami" },
378 { "si", "Sinhalese" },
379 { "sk", "Slovak" },
380 { "sl", "Slovenian" },
381 { "sma", "Sami (Southern)" },
382 { "sma", "Southern Sami" },
383 { "smj", "Sami (Lule)" },
384 { "smj", "Lule Sami" },
385 { "smn", "Sami (Inari)" },
386 { "smn", "Inari Sami" },
387 { "sms", "Sami (Skolt)" },
388 { "sms", "Skolt Sami" },
389 { "so", "Somali" },
390 { "sq", "Albanian" },
391 { "sr", "Serbian (Latin)" },
392 { "sr@cyrillic", "SRB" }, /* Serbian (Cyrillic) */
393 { "sw", "Swahili" },
394 { "syr", "Syriac" },
395 { "ta", "Tamil" },
396 { "te", "Telugu" },
397 { "tg", "Tajik" },
398 { "th", "Thai" },
399 { "ti", "Tigrinya" },
400 { "tk", "Turkmen" },
401 { "tl", "Filipino" },
402 { "tn", "Tswana" },
403 { "tr", "Turkish" },
404 { "ts", "Tsonga" },
405 { "tt", "Tatar" },
406 { "ug", "Uighur" },
407 { "uk", "Ukrainian" },
408 { "ur", "Urdu" },
409 { "uz", "Uzbek" },
410 { "uz", "Uzbek (Latin)" },
411 { "uz@cyrillic", "Uzbek (Cyrillic)" },
412 { "ve", "Venda" },
413 { "vi", "Vietnamese" },
414 { "wen", "Sorbian" },
415 { "wo", "Wolof" },
416 { "xh", "Xhosa" },
417 { "yi", "Yiddish" },
418 { "yo", "Yoruba" },
419 { "zh", "Chinese" },
420 { "zu", "Zulu" }
421 };
422
423 /* Table from ISO 3166 country code to English name.
424 Keep in sync with the gl_locale_name_from_win32_LANGID function in
425 localename.c! */
426 static const struct table_entry country_table[] =
427 {
428 { "AE", "U.A.E." },
429 { "AF", "Afghanistan" },
430 { "AL", "Albania" },
431 { "AM", "Armenia" },
432 { "AN", "Netherlands Antilles" },
433 { "AR", "Argentina" },
434 { "AT", "Austria" },
435 { "AU", "Australia" },
436 { "AZ", "Azerbaijan" },
437 { "BA", "Bosnia and Herzegovina" },
438 { "BD", "Bangladesh" },
439 { "BE", "Belgium" },
440 { "BG", "Bulgaria" },
441 { "BH", "Bahrain" },
442 { "BN", "Brunei Darussalam" },
443 { "BO", "Bolivia" },
444 { "BR", "Brazil" },
445 { "BT", "Bhutan" },
446 { "BY", "Belarus" },
447 { "BZ", "Belize" },
448 { "CA", "Canada" },
449 { "CG", "Congo" },
450 { "CH", "Switzerland" },
451 { "CI", "Cote d'Ivoire" },
452 { "CL", "Chile" },
453 { "CM", "Cameroon" },
454 { "CN", "People's Republic of China" },
455 { "CO", "Colombia" },
456 { "CR", "Costa Rica" },
457 { "CS", "Serbia and Montenegro" },
458 { "CZ", "Czech Republic" },
459 { "DE", "Germany" },
460 { "DK", "Denmark" },
461 { "DO", "Dominican Republic" },
462 { "DZ", "Algeria" },
463 { "EC", "Ecuador" },
464 { "EE", "Estonia" },
465 { "EG", "Egypt" },
466 { "ER", "Eritrea" },
467 { "ES", "Spain" },
468 { "ET", "Ethiopia" },
469 { "FI", "Finland" },
470 { "FO", "Faroe Islands" },
471 { "FR", "France" },
472 { "GB", "United Kingdom" },
473 { "GD", "Caribbean" },
474 { "GE", "Georgia" },
475 { "GL", "Greenland" },
476 { "GR", "Greece" },
477 { "GT", "Guatemala" },
478 { "HK", "Hong Kong" },
479 { "HK", "Hong Kong S.A.R." },
480 { "HN", "Honduras" },
481 { "HR", "Croatia" },
482 { "HT", "Haiti" },
483 { "HU", "Hungary" },
484 { "ID", "Indonesia" },
485 { "IE", "Ireland" },
486 { "IL", "Israel" },
487 { "IN", "India" },
488 { "IQ", "Iraq" },
489 { "IR", "Iran" },
490 { "IS", "Iceland" },
491 { "IT", "Italy" },
492 { "JM", "Jamaica" },
493 { "JO", "Jordan" },
494 { "JP", "Japan" },
495 { "KE", "Kenya" },
496 { "KG", "Kyrgyzstan" },
497 { "KH", "Cambodia" },
498 { "KR", "South Korea" },
499 { "KW", "Kuwait" },
500 { "KZ", "Kazakhstan" },
501 { "LA", "Laos" },
502 { "LB", "Lebanon" },
503 { "LI", "Liechtenstein" },
504 { "LK", "Sri Lanka" },
505 { "LT", "Lithuania" },
506 { "LU", "Luxembourg" },
507 { "LV", "Latvia" },
508 { "LY", "Libya" },
509 { "MA", "Morocco" },
510 { "MC", "Principality of Monaco" },
511 { "MD", "Moldava" },
512 { "MD", "Moldova" },
513 { "ME", "Montenegro" },
514 { "MK", "Former Yugoslav Republic of Macedonia" },
515 { "ML", "Mali" },
516 { "MM", "Myanmar" },
517 { "MN", "Mongolia" },
518 { "MO", "Macau S.A.R." },
519 { "MT", "Malta" },
520 { "MV", "Maldives" },
521 { "MX", "Mexico" },
522 { "MY", "Malaysia" },
523 { "NG", "Nigeria" },
524 { "NI", "Nicaragua" },
525 { "NL", "Netherlands" },
526 { "NO", "Norway" },
527 { "NP", "Nepal" },
528 { "NZ", "New Zealand" },
529 { "OM", "Oman" },
530 { "PA", "Panama" },
531 { "PE", "Peru" },
532 { "PH", "Philippines" },
533 { "PK", "Islamic Republic of Pakistan" },
534 { "PL", "Poland" },
535 { "PR", "Puerto Rico" },
536 { "PT", "Portugal" },
537 { "PY", "Paraguay" },
538 { "QA", "Qatar" },
539 { "RE", "Reunion" },
540 { "RO", "Romania" },
541 { "RS", "Serbia" },
542 { "RU", "Russia" },
543 { "RW", "Rwanda" },
544 { "SA", "Saudi Arabia" },
545 { "SE", "Sweden" },
546 { "SG", "Singapore" },
547 { "SI", "Slovenia" },
548 { "SK", "Slovak" },
549 { "SN", "Senegal" },
550 { "SO", "Somalia" },
551 { "SR", "Suriname" },
552 { "SV", "El Salvador" },
553 { "SY", "Syria" },
554 { "TH", "Thailand" },
555 { "TJ", "Tajikistan" },
556 { "TM", "Turkmenistan" },
557 { "TN", "Tunisia" },
558 { "TR", "Turkey" },
559 { "TT", "Trinidad and Tobago" },
560 { "TW", "Taiwan" },
561 { "TZ", "Tanzania" },
562 { "UA", "Ukraine" },
563 { "US", "United States" },
564 { "UY", "Uruguay" },
565 { "VA", "Vatican" },
566 { "VE", "Venezuela" },
567 { "VN", "Viet Nam" },
568 { "YE", "Yemen" },
569 { "ZA", "South Africa" },
570 { "ZW", "Zimbabwe" }
571 };
572
573 /* Given a string STRING, find the set of indices i such that TABLE[i].code is
574 the given STRING. It is a range [lo,hi-1]. */
575 typedef struct { size_t lo; size_t hi; } range_t;
576 static void
search(const struct table_entry * table,size_t table_size,const char * string,range_t * result)577 search (const struct table_entry *table, size_t table_size, const char *string,
578 range_t *result)
579 {
580 /* The table is sorted. Perform a binary search. */
581 size_t hi = table_size;
582 size_t lo = 0;
583 while (lo < hi)
584 {
585 /* Invariant:
586 for i < lo, strcmp (table[i].code, string) < 0,
587 for i >= hi, strcmp (table[i].code, string) > 0. */
588 size_t mid = (hi + lo) >> 1; /* >= lo, < hi */
589 int cmp = strcmp (table[mid].code, string);
590 if (cmp < 0)
591 lo = mid + 1;
592 else if (cmp > 0)
593 hi = mid;
594 else
595 {
596 /* Found an i with
597 strcmp (language_table[i].code, string) == 0.
598 Find the entire interval of such i. */
599 {
600 size_t i;
601
602 for (i = mid; i > lo; )
603 {
604 i--;
605 if (strcmp (table[i].code, string) < 0)
606 {
607 lo = i + 1;
608 break;
609 }
610 }
611 }
612 {
613 size_t i;
614
615 for (i = mid; i < hi; i++)
616 {
617 if (strcmp (table[i].code, string) > 0)
618 {
619 hi = i;
620 break;
621 }
622 }
623 }
624 /* The set of i with
625 strcmp (language_table[i].code, string) == 0
626 is the interval [lo, hi-1]. */
627 break;
628 }
629 }
630 result->lo = lo;
631 result->hi = hi;
632 }
633
634 /* Like setlocale, but accept also locale names in the form ll or ll_CC,
635 where ll is an ISO 639 language code and CC is an ISO 3166 country code. */
636 static char *
setlocale_unixlike(int category,const char * locale)637 setlocale_unixlike (int category, const char *locale)
638 {
639 char *result;
640 char llCC_buf[64];
641 char ll_buf[64];
642 char CC_buf[64];
643
644 /* First, try setlocale with the original argument unchanged. */
645 result = setlocale (category, locale);
646 if (result != NULL)
647 return result;
648
649 /* Otherwise, assume the argument is in the form
650 language[_territory][.codeset][@modifier]
651 and try to map it using the tables. */
652 if (strlen (locale) < sizeof (llCC_buf))
653 {
654 /* Second try: Remove the codeset part. */
655 {
656 const char *p = locale;
657 char *q = llCC_buf;
658
659 /* Copy the part before the dot. */
660 for (; *p != '\0' && *p != '.'; p++, q++)
661 *q = *p;
662 if (*p == '.')
663 /* Skip the part up to the '@', if any. */
664 for (; *p != '\0' && *p != '@'; p++)
665 ;
666 /* Copy the part starting with '@', if any. */
667 for (; *p != '\0'; p++, q++)
668 *q = *p;
669 *q = '\0';
670 }
671 /* llCC_buf now contains
672 language[_territory][@modifier]
673 */
674 if (strcmp (llCC_buf, locale) != 0)
675 {
676 result = setlocale (category, llCC_buf);
677 if (result != NULL)
678 return result;
679 }
680 /* Look it up in language_table. */
681 {
682 range_t range;
683 size_t i;
684
685 search (language_table,
686 sizeof (language_table) / sizeof (language_table[0]),
687 llCC_buf,
688 &range);
689
690 for (i = range.lo; i < range.hi; i++)
691 {
692 /* Try the replacement in language_table[i]. */
693 result = setlocale (category, language_table[i].english);
694 if (result != NULL)
695 return result;
696 }
697 }
698 /* Split language[_territory][@modifier]
699 into ll_buf = language[@modifier]
700 and CC_buf = territory
701 */
702 {
703 const char *underscore = strchr (llCC_buf, '_');
704 if (underscore != NULL)
705 {
706 const char *territory_start = underscore + 1;
707 const char *territory_end = strchr (territory_start, '@');
708 if (territory_end == NULL)
709 territory_end = territory_start + strlen (territory_start);
710
711 memcpy (ll_buf, llCC_buf, underscore - llCC_buf);
712 strcpy (ll_buf + (underscore - llCC_buf), territory_end);
713
714 memcpy (CC_buf, territory_start, territory_end - territory_start);
715 CC_buf[territory_end - territory_start] = '\0';
716
717 {
718 /* Look up ll_buf in language_table
719 and CC_buf in country_table. */
720 range_t language_range;
721
722 search (language_table,
723 sizeof (language_table) / sizeof (language_table[0]),
724 ll_buf,
725 &language_range);
726 if (language_range.lo < language_range.hi)
727 {
728 range_t country_range;
729
730 search (country_table,
731 sizeof (country_table) / sizeof (country_table[0]),
732 CC_buf,
733 &country_range);
734 if (country_range.lo < country_range.hi)
735 {
736 size_t i;
737 size_t j;
738
739 for (i = language_range.lo; i < language_range.hi; i++)
740 for (j = country_range.lo; j < country_range.hi; j++)
741 {
742 /* Concatenate the replacements. */
743 const char *part1 = language_table[i].english;
744 size_t part1_len = strlen (part1);
745 const char *part2 = country_table[j].english;
746 size_t part2_len = strlen (part2) + 1;
747 char buf[64+64];
748
749 if (!(part1_len + 1 + part2_len <= sizeof (buf)))
750 abort ();
751 memcpy (buf, part1, part1_len);
752 buf[part1_len] = '_';
753 memcpy (buf + part1_len + 1, part2, part2_len);
754
755 /* Try the concatenated replacements. */
756 result = setlocale (category, buf);
757 if (result != NULL)
758 return result;
759 }
760 }
761
762 /* Try omitting the country entirely. This may set a locale
763 corresponding to the wrong country, but is better than
764 failing entirely. */
765 {
766 size_t i;
767
768 for (i = language_range.lo; i < language_range.hi; i++)
769 {
770 /* Try only the language replacement. */
771 result =
772 setlocale (category, language_table[i].english);
773 if (result != NULL)
774 return result;
775 }
776 }
777 }
778 }
779 }
780 }
781 }
782
783 /* Failed. */
784 return NULL;
785 }
786
787 # else
788 # define setlocale_unixlike setlocale
789 # endif
790
791 # if LC_MESSAGES == 1729
792
793 /* The system does not store an LC_MESSAGES locale category. Do it here. */
794 static char lc_messages_name[64] = "C";
795
796 /* Like setlocale, but support also LC_MESSAGES. */
797 static char *
setlocale_single(int category,const char * locale)798 setlocale_single (int category, const char *locale)
799 {
800 if (category == LC_MESSAGES)
801 {
802 if (locale != NULL)
803 {
804 lc_messages_name[sizeof (lc_messages_name) - 1] = '\0';
805 strncpy (lc_messages_name, locale, sizeof (lc_messages_name) - 1);
806 }
807 return lc_messages_name;
808 }
809 else
810 return setlocale_unixlike (category, locale);
811 }
812
813 # else
814 # define setlocale_single setlocale_unixlike
815 # endif
816
817 DLL_EXPORTED
818 char *
libintl_setlocale(int category,const char * locale)819 libintl_setlocale (int category, const char *locale)
820 {
821 if (locale != NULL && locale[0] == '\0')
822 {
823 /* A request to the set the current locale to the default locale. */
824 if (category == LC_ALL)
825 {
826 /* Set LC_CTYPE first. Then the other categories. */
827 static int const categories[] =
828 {
829 LC_NUMERIC,
830 LC_TIME,
831 LC_COLLATE,
832 LC_MONETARY,
833 LC_MESSAGES
834 };
835 char *saved_locale;
836 const char *base_name;
837 unsigned int i;
838
839 /* Back up the old locale, in case one of the steps fails. */
840 saved_locale = setlocale (LC_ALL, NULL);
841 if (saved_locale == NULL)
842 return NULL;
843 saved_locale = strdup (saved_locale);
844 if (saved_locale == NULL)
845 return NULL;
846
847 /* Set LC_CTYPE category. Set all other categories (except possibly
848 LC_MESSAGES) to the same value in the same call; this is likely to
849 save calls. */
850 base_name =
851 gl_locale_name_environ (LC_CTYPE, category_to_name (LC_CTYPE));
852 if (base_name == NULL)
853 base_name = gl_locale_name_default ();
854
855 if (setlocale_unixlike (LC_ALL, base_name) == NULL)
856 goto fail;
857 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
858 /* On native Windows, setlocale(LC_ALL,...) may succeed but set the
859 LC_CTYPE category to an invalid value ("C") when it does not
860 support the specified encoding. Report a failure instead. */
861 if (strchr (base_name, '.') != NULL
862 && strcmp (setlocale (LC_CTYPE, NULL), "C") == 0)
863 goto fail;
864 # endif
865
866 for (i = 0; i < sizeof (categories) / sizeof (categories[0]); i++)
867 {
868 int cat = categories[i];
869 const char *name;
870
871 name = gl_locale_name_environ (cat, category_to_name (cat));
872 if (name == NULL)
873 name = gl_locale_name_default ();
874
875 /* If name is the same as base_name, it has already been set
876 through the setlocale call before the loop. */
877 if (strcmp (name, base_name) != 0
878 # if LC_MESSAGES == 1729
879 || cat == LC_MESSAGES
880 # endif
881 )
882 if (setlocale_single (cat, name) == NULL)
883 goto fail;
884 }
885
886 /* All steps were successful. */
887 ++_nl_msg_cat_cntr;
888 free (saved_locale);
889 return setlocale (LC_ALL, NULL);
890
891 fail:
892 if (saved_locale[0] != '\0') /* don't risk an endless recursion */
893 setlocale (LC_ALL, saved_locale);
894 free (saved_locale);
895 return NULL;
896 }
897 else
898 {
899 char *result;
900 const char *name =
901 gl_locale_name_environ (category, category_to_name (category));
902 if (name == NULL)
903 name = gl_locale_name_default ();
904
905 result = setlocale_single (category, name);
906 if (result != NULL)
907 ++_nl_msg_cat_cntr;
908 return result;
909 }
910 }
911 else
912 {
913 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
914 if (category == LC_ALL && locale != NULL && strchr (locale, '.') != NULL)
915 {
916 char *saved_locale;
917
918 /* Back up the old locale. */
919 saved_locale = setlocale (LC_ALL, NULL);
920 if (saved_locale == NULL)
921 return NULL;
922 saved_locale = strdup (saved_locale);
923 if (saved_locale == NULL)
924 return NULL;
925
926 if (setlocale_unixlike (LC_ALL, locale) == NULL)
927 {
928 free (saved_locale);
929 return NULL;
930 }
931
932 /* On native Windows, setlocale(LC_ALL,...) may succeed but set the
933 LC_CTYPE category to an invalid value ("C") when it does not
934 support the specified encoding. Report a failure instead. */
935 if (strcmp (setlocale (LC_CTYPE, NULL), "C") == 0)
936 {
937 if (saved_locale[0] != '\0') /* don't risk an endless recursion */
938 setlocale (LC_ALL, saved_locale);
939 free (saved_locale);
940 return NULL;
941 }
942
943 /* It was really successful. */
944 ++_nl_msg_cat_cntr;
945 free (saved_locale);
946 return setlocale (LC_ALL, NULL);
947 }
948 else
949 # endif
950 {
951 char *result = setlocale_single (category, locale);
952 if (result != NULL)
953 ++_nl_msg_cat_cntr;
954 return result;
955 }
956 }
957 }
958
959 # if HAVE_NEWLOCALE
960
961 DLL_EXPORTED
962 locale_t
libintl_newlocale(int category_mask,const char * locale,locale_t base)963 libintl_newlocale (int category_mask, const char *locale, locale_t base)
964 {
965 if (category_mask != 0 && locale != NULL && locale[0] == '\0')
966 {
967 /* A request to construct a locale_t object that refers to the default
968 locale. */
969
970 /* Set LC_CTYPE first. Then the other categories. */
971 static struct { int cat; int mask; } const categories[] =
972 {
973 { LC_CTYPE, LC_CTYPE_MASK },
974 { LC_NUMERIC, LC_NUMERIC_MASK },
975 { LC_TIME, LC_TIME_MASK },
976 { LC_COLLATE, LC_COLLATE_MASK },
977 { LC_MONETARY, LC_MONETARY_MASK },
978 { LC_MESSAGES, LC_MESSAGES_MASK }
979 };
980
981 locale_t orig_base = base;
982
983 if ((LC_ALL_MASK & ~category_mask) == 0)
984 {
985 const char *base_name;
986 unsigned int i;
987
988 /* Set LC_CTYPE category. Set all other categories (except possibly
989 LC_MESSAGES) to the same value in the same call; this is likely to
990 save calls. */
991 base_name =
992 gl_locale_name_environ (LC_CTYPE, category_to_name (LC_CTYPE));
993 if (base_name == NULL)
994 base_name = gl_locale_name_default ();
995
996 base = newlocale (LC_ALL_MASK, base_name, base);
997 if (base == NULL)
998 return NULL;
999
1000 for (i = 1; i < sizeof (categories) / sizeof (categories[0]); i++)
1001 {
1002 int category = categories[i].cat;
1003 int category_mask = categories[i].mask;
1004 const char *name;
1005
1006 name =
1007 gl_locale_name_environ (category, category_to_name (category));
1008 if (name == NULL)
1009 name = gl_locale_name_default ();
1010
1011 /* If name is the same as base_name, it has already been set
1012 through the setlocale call before the loop. */
1013 if (strcmp (name, base_name) != 0)
1014 {
1015 locale_t copy = newlocale (category_mask, name, base);
1016 if (copy == NULL)
1017 goto fail;
1018 /* No need to call freelocale (base) if copy != base; the
1019 newlocale function already takes care of doing it. */
1020 base = copy;
1021 }
1022 }
1023 }
1024 else
1025 {
1026 unsigned int i;
1027
1028 for (i = 0; i < sizeof (categories) / sizeof (categories[0]); i++)
1029 {
1030 int cat_mask = categories[i].mask;
1031
1032 if ((category_mask & cat_mask) != 0)
1033 {
1034 int cat = categories[i].cat;
1035 const char *name;
1036 locale_t copy;
1037
1038 name = gl_locale_name_environ (cat, category_to_name (cat));
1039 if (name == NULL)
1040 name = gl_locale_name_default ();
1041
1042 copy = newlocale (cat_mask, name, base);
1043 if (copy == NULL)
1044 goto fail;
1045 /* No need to call freelocale (base) if copy != base; the
1046 newlocale function already takes care of doing it. */
1047 base = copy;
1048 }
1049 }
1050 }
1051
1052 /* All steps were successful. */
1053 return base;
1054
1055 fail:
1056 if (base != NULL && orig_base == NULL)
1057 {
1058 int saved_errno = errno;
1059 freelocale (base);
1060 errno = saved_errno;
1061 }
1062 return NULL;
1063 }
1064 else
1065 return newlocale (category_mask, locale, base);
1066 }
1067
1068 # endif
1069
1070 #endif
1071