source: diffutils/vendor/current/gnulib-tests/setlocale.c@ 530

Last change on this file since 530 was 530, checked in by Yuri Dario, 12 years ago

diffutils: initial vendor import of diffutils 3.2.0.

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