Back to index

courier  0.68.2
setlocale.c
Go to the documentation of this file.
00001 /* setlocale() function that respects the locale chosen by the user.
00002    Copyright (C) 2009 Free Software Foundation, Inc.
00003    Written by Bruno Haible <bruno@clisp.org>, 2009.
00004 
00005    This program is free software; you can redistribute it and/or modify it
00006    under the terms of the GNU Library General Public License as published
00007    by the Free Software Foundation; either version 2, or (at your option)
00008    any later version.
00009 
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public
00016    License along with this program; if not, write to the Free Software
00017    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00018    USA.  */
00019 
00020 #ifdef HAVE_CONFIG_H
00021 # include <config.h>
00022 #endif
00023 
00024 /* Override setlocale() and newlocale() so that when the default locale is
00025    requested (locale = "") and no relevant environment variable is set, the
00026    locale chosen by the user is used.
00027    This matters on MacOS X 10 and Windows.
00028    See the comments in localename.c, function gl_locale_name_default.  */
00029 
00030 #include <locale.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033 
00034 /* When building a DLL, we must export some functions.  Note that because
00035    the functions are only defined for binary backward compatibility, we
00036    don't need to use __declspec(dllimport) in any case.  */
00037 #if HAVE_VISIBILITY && BUILDING_DLL
00038 # define DLL_EXPORTED __attribute__((__visibility__("default")))
00039 #elif defined _MSC_VER && BUILDING_DLL
00040 # define DLL_EXPORTED __declspec(dllexport)
00041 #else
00042 # define DLL_EXPORTED
00043 #endif
00044 
00045 #include "gettextP.h"
00046 
00047 #if (defined __APPLE__ && defined __MACH__) || defined _WIN32 || defined __WIN32__ || defined __CYGWIN__
00048 
00049 # undef setlocale
00050 # undef newlocale
00051 
00052 /* Return string representation of locale category CATEGORY.  */
00053 static const char *
00054 category_to_name (int category)
00055 {
00056   const char *retval;
00057 
00058   switch (category)
00059   {
00060   case LC_COLLATE:
00061     retval = "LC_COLLATE";
00062     break;
00063   case LC_CTYPE:
00064     retval = "LC_CTYPE";
00065     break;
00066   case LC_MONETARY:
00067     retval = "LC_MONETARY";
00068     break;
00069   case LC_NUMERIC:
00070     retval = "LC_NUMERIC";
00071     break;
00072   case LC_TIME:
00073     retval = "LC_TIME";
00074     break;
00075   case LC_MESSAGES:
00076     retval = "LC_MESSAGES";
00077     break;
00078   default:
00079     /* If you have a better idea for a default value let me know.  */
00080     retval = "LC_XXX";
00081   }
00082 
00083   return retval;
00084 }
00085 
00086 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
00087 
00088 /* The native Win32 setlocale() function expects locale names of the form
00089    "German" or "German_Germany" or "DEU", but not "de" or "de_DE".  We need
00090    to convert the names from the form with ISO 639 language code and ISO 3166
00091    country code to the form with English names or with three-letter identifier.
00092    The three-letter identifiers known by a Windows XP SP2 or SP3 are:
00093      AFK  Afrikaans_South Africa.1252
00094      ARA  Arabic_Saudi Arabia.1256
00095      ARB  Arabic_Lebanon.1256
00096      ARE  Arabic_Egypt.1256
00097      ARG  Arabic_Algeria.1256
00098      ARH  Arabic_Bahrain.1256
00099      ARI  Arabic_Iraq.1256
00100      ARJ  Arabic_Jordan.1256
00101      ARK  Arabic_Kuwait.1256
00102      ARL  Arabic_Libya.1256
00103      ARM  Arabic_Morocco.1256
00104      ARO  Arabic_Oman.1256
00105      ARQ  Arabic_Qatar.1256
00106      ARS  Arabic_Syria.1256
00107      ART  Arabic_Tunisia.1256
00108      ARU  Arabic_U.A.E..1256
00109      ARY  Arabic_Yemen.1256
00110      AZE  Azeri (Latin)_Azerbaijan.1254
00111      BEL  Belarusian_Belarus.1251
00112      BGR  Bulgarian_Bulgaria.1251
00113      BSB  Bosnian_Bosnia and Herzegovina.1250
00114      BSC  Bosnian (Cyrillic)_Bosnia and Herzegovina.1250  (wrong encoding!)
00115      CAT  Catalan_Spain.1252
00116      CHH  Chinese_Hong Kong S.A.R..950
00117      CHI  Chinese_Singapore.936
00118      CHS  Chinese_People's Republic of China.936
00119      CHT  Chinese_Taiwan.950
00120      CSY  Czech_Czech Republic.1250
00121      CYM  Welsh_United Kingdom.1252
00122      DAN  Danish_Denmark.1252
00123      DEA  German_Austria.1252
00124      DEC  German_Liechtenstein.1252
00125      DEL  German_Luxembourg.1252
00126      DES  German_Switzerland.1252
00127      DEU  German_Germany.1252
00128      ELL  Greek_Greece.1253
00129      ENA  English_Australia.1252
00130      ENB  English_Caribbean.1252
00131      ENC  English_Canada.1252
00132      ENG  English_United Kingdom.1252
00133      ENI  English_Ireland.1252
00134      ENJ  English_Jamaica.1252
00135      ENL  English_Belize.1252
00136      ENP  English_Republic of the Philippines.1252
00137      ENS  English_South Africa.1252
00138      ENT  English_Trinidad and Tobago.1252
00139      ENU  English_United States.1252
00140      ENW  English_Zimbabwe.1252
00141      ENZ  English_New Zealand.1252
00142      ESA  Spanish_Panama.1252
00143      ESB  Spanish_Bolivia.1252
00144      ESC  Spanish_Costa Rica.1252
00145      ESD  Spanish_Dominican Republic.1252
00146      ESE  Spanish_El Salvador.1252
00147      ESF  Spanish_Ecuador.1252
00148      ESG  Spanish_Guatemala.1252
00149      ESH  Spanish_Honduras.1252
00150      ESI  Spanish_Nicaragua.1252
00151      ESL  Spanish_Chile.1252
00152      ESM  Spanish_Mexico.1252
00153      ESN  Spanish_Spain.1252
00154      ESO  Spanish_Colombia.1252
00155      ESP  Spanish_Spain.1252
00156      ESR  Spanish_Peru.1252
00157      ESS  Spanish_Argentina.1252
00158      ESU  Spanish_Puerto Rico.1252
00159      ESV  Spanish_Venezuela.1252
00160      ESY  Spanish_Uruguay.1252
00161      ESZ  Spanish_Paraguay.1252
00162      ETI  Estonian_Estonia.1257
00163      EUQ  Basque_Spain.1252
00164      FAR  Farsi_Iran.1256
00165      FIN  Finnish_Finland.1252
00166      FOS  Faroese_Faroe Islands.1252
00167      FPO  Filipino_Philippines.1252
00168      FRA  French_France.1252
00169      FRB  French_Belgium.1252
00170      FRC  French_Canada.1252
00171      FRL  French_Luxembourg.1252
00172      FRM  French_Principality of Monaco.1252
00173      FRS  French_Switzerland.1252
00174      FYN  Frisian_Netherlands.1252
00175      GLC  Galician_Spain.1252
00176      HEB  Hebrew_Israel.1255
00177      HRB  Croatian_Bosnia and Herzegovina.1250
00178      HRV  Croatian_Croatia.1250
00179      HUN  Hungarian_Hungary.1250
00180      IND  Indonesian_Indonesia.1252
00181      IRE  Irish_Ireland.1252
00182      ISL  Icelandic_Iceland.1252
00183      ITA  Italian_Italy.1252
00184      ITS  Italian_Switzerland.1252
00185      IUK  Inuktitut (Latin)_Canada.1252
00186      JPN  Japanese_Japan.932
00187      KKZ  Kazakh_Kazakhstan.1251
00188      KOR  Korean_Korea.949
00189      KYR  Kyrgyz_Kyrgyzstan.1251
00190      LBX  Luxembourgish_Luxembourg.1252
00191      LTH  Lithuanian_Lithuania.1257
00192      LVI  Latvian_Latvia.1257
00193      MKI  FYRO Macedonian_Former Yugoslav Republic of Macedonia.1251
00194      MON  Mongolian_Mongolia.1251
00195      MPD  Mapudungun_Chile.1252
00196      MSB  Malay_Brunei Darussalam.1252
00197      MSL  Malay_Malaysia.1252
00198      MWK  Mohawk_Canada.1252
00199      NLB  Dutch_Belgium.1252
00200      NLD  Dutch_Netherlands.1252
00201      NON  Norwegian-Nynorsk_Norway.1252
00202      NOR  Norwegian (Bokmål)_Norway.1252
00203      NSO  Northern Sotho_South Africa.1252
00204      PLK  Polish_Poland.1250
00205      PTB  Portuguese_Brazil.1252
00206      PTG  Portuguese_Portugal.1252
00207      QUB  Quechua_Bolivia.1252
00208      QUE  Quechua_Ecuador.1252
00209      QUP  Quechua_Peru.1252
00210      RMC  Romansh_Switzerland.1252
00211      ROM  Romanian_Romania.1250
00212      RUS  Russian_Russia.1251
00213      SKY  Slovak_Slovakia.1250
00214      SLV  Slovenian_Slovenia.1250
00215      SMA  Sami (Southern)_Norway.1252
00216      SMB  Sami (Southern)_Sweden.1252
00217      SME  Sami (Northern)_Norway.1252
00218      SMF  Sami (Northern)_Sweden.1252
00219      SMG  Sami (Northern)_Finland.1252
00220      SMJ  Sami (Lule)_Norway.1252
00221      SMK  Sami (Lule)_Sweden.1252
00222      SMN  Sami (Inari)_Finland.1252
00223      SMS  Sami (Skolt)_Finland.1252
00224      SQI  Albanian_Albania.1250
00225      SRB  Serbian (Cyrillic)_Serbia and Montenegro.1251
00226      SRL  Serbian (Latin)_Serbia and Montenegro.1250
00227      SRN  Serbian (Cyrillic)_Bosnia and Herzegovina.1251
00228      SRS  Serbian (Latin)_Bosnia and Herzegovina.1250
00229      SVE  Swedish_Sweden.1252
00230      SVF  Swedish_Finland.1252
00231      SWK  Swahili_Kenya.1252
00232      THA  Thai_Thailand.874
00233      TRK  Turkish_Turkey.1254
00234      TSN  Tswana_South Africa.1252
00235      TTT  Tatar_Russia.1251
00236      UKR  Ukrainian_Ukraine.1251
00237      URD  Urdu_Islamic Republic of Pakistan.1256
00238      USA  English_United States.1252
00239      UZB  Uzbek (Latin)_Uzbekistan.1254
00240      VIT  Vietnamese_Viet Nam.1258
00241      XHO  Xhosa_South Africa.1252
00242      ZHH  Chinese_Hong Kong S.A.R..950
00243      ZHI  Chinese_Singapore.936
00244      ZHM  Chinese_Macau S.A.R..950
00245      ZUL  Zulu_South Africa.1252
00246  */
00247 
00248 /* Table from ISO 639 language code, optionally with country or script suffix,
00249    to English name.
00250    Keep in sync with the gl_locale_name_from_win32_LANGID function in
00251    localename.c!  */
00252 struct table_entry
00253 {
00254   const char *code;
00255   const char *english;
00256 };
00257 static const struct table_entry language_table[] =
00258   {
00259     { "af", "Afrikaans" },
00260     { "am", "Amharic" },
00261     { "ar", "Arabic" },
00262     { "arn", "Mapudungun" },
00263     { "as", "Assamese" },
00264     { "az@cyrillic", "Azeri (Cyrillic)" },
00265     { "az@latin", "Azeri (Latin)" },
00266     { "ba", "Bashkir" },
00267     { "be", "Belarusian" },
00268     { "ber", "Tamazight" },
00269     { "ber@arabic", "Tamazight (Arabic)" },
00270     { "ber@latin", "Tamazight (Latin)" },
00271     { "bg", "Bulgarian" },
00272     { "bin", "Edo" },
00273     { "bn", "Bengali" },
00274     { "bn_BD", "Bengali (Bangladesh)" },
00275     { "bn_IN", "Bengali (India)" },
00276     { "bnt", "Sutu" },
00277     { "bo", "Tibetan" },
00278     { "br", "Breton" },
00279     { "bs", "BSB" }, /* "Bosnian (Latin)" */
00280     { "bs@cyrillic", "BSC" }, /* Bosnian (Cyrillic) */
00281     { "ca", "Catalan" },
00282     { "chr", "Cherokee" },
00283     { "co", "Corsican" },
00284     { "cpe", "Hawaiian" },
00285     { "cs", "Czech" },
00286     { "cy", "Welsh" },
00287     { "da", "Danish" },
00288     { "de", "German" },
00289     { "dsb", "Lower Sorbian" },
00290     { "dv", "Divehi" },
00291     { "el", "Greek" },
00292     { "en", "English" },
00293     { "es", "Spanish" },
00294     { "et", "Estonian" },
00295     { "eu", "Basque" },
00296     { "fa", "Farsi" },
00297     { "ff", "Fulfulde" },
00298     { "fi", "Finnish" },
00299     { "fo", "Faroese" }, /* "Faeroese" does not work */
00300     { "fr", "French" },
00301     { "fy", "Frisian" },
00302     { "ga", "IRE" }, /* Gaelic (Ireland) */
00303     { "gd", "Gaelic (Scotland)" },
00304     { "gd", "Scottish Gaelic" },
00305     { "gl", "Galician" },
00306     { "gn", "Guarani" },
00307     { "gsw", "Alsatian" },
00308     { "gu", "Gujarati" },
00309     { "ha", "Hausa" },
00310     { "he", "Hebrew" },
00311     { "hi", "Hindi" },
00312     { "hr", "Croatian" },
00313     { "hsb", "Upper Sorbian" },
00314     { "hu", "Hungarian" },
00315     { "hy", "Armenian" },
00316     { "id", "Indonesian" },
00317     { "ig", "Igbo" },
00318     { "ii", "Yi" },
00319     { "is", "Icelandic" },
00320     { "it", "Italian" },
00321     { "iu", "IUK" }, /* Inuktitut */
00322     { "ja", "Japanese" },
00323     { "ka", "Georgian" },
00324     { "kk", "Kazakh" },
00325     { "kl", "Greenlandic" },
00326     { "km", "Cambodian" },
00327     { "km", "Khmer" },
00328     { "kn", "Kannada" },
00329     { "ko", "Korean" },
00330     { "kok", "Konkani" },
00331     { "kr", "Kanuri" },
00332     { "ks", "Kashmiri" },
00333     { "ks_IN", "Kashmiri_India" },
00334     { "ks_PK", "Kashmiri (Arabic)_Pakistan" },
00335     { "ky", "Kyrgyz" },
00336     { "la", "Latin" },
00337     { "lb", "Luxembourgish" },
00338     { "lo", "Lao" },
00339     { "lt", "Lithuanian" },
00340     { "lv", "Latvian" },
00341     { "mi", "Maori" },
00342     { "mk", "FYRO Macedonian" },
00343     { "mk", "Macedonian" },
00344     { "ml", "Malayalam" },
00345     { "mn", "Mongolian" },
00346     { "mni", "Manipuri" },
00347     { "moh", "Mohawk" },
00348     { "mr", "Marathi" },
00349     { "ms", "Malay" },
00350     { "mt", "Maltese" },
00351     { "my", "Burmese" },
00352     { "nb", "NOR" }, /* Norwegian Bokmål */
00353     { "ne", "Nepali" },
00354     { "nic", "Ibibio" },
00355     { "nl", "Dutch" },
00356     { "nn", "NON" }, /* Norwegian Nynorsk */
00357     { "no", "Norwegian" },
00358     { "nso", "Northern Sotho" },
00359     { "nso", "Sepedi" },
00360     { "oc", "Occitan" },
00361     { "om", "Oromo" },
00362     { "or", "Oriya" },
00363     { "pa", "Punjabi" },
00364     { "pap", "Papiamentu" },
00365     { "pl", "Polish" },
00366     { "prs", "Dari" },
00367     { "ps", "Pashto" },
00368     { "pt", "Portuguese" },
00369     { "qu", "Quechua" },
00370     { "qut", "K'iche'" },
00371     { "rm", "Romansh" },
00372     { "ro", "Romanian" },
00373     { "ru", "Russian" },
00374     { "rw", "Kinyarwanda" },
00375     { "sa", "Sanskrit" },
00376     { "sah", "Yakut" },
00377     { "sd", "Sindhi" },
00378     { "se", "Sami (Northern)" },
00379     { "se", "Northern Sami" },
00380     { "si", "Sinhalese" },
00381     { "sk", "Slovak" },
00382     { "sl", "Slovenian" },
00383     { "sma", "Sami (Southern)" },
00384     { "sma", "Southern Sami" },
00385     { "smj", "Sami (Lule)" },
00386     { "smj", "Lule Sami" },
00387     { "smn", "Sami (Inari)" },
00388     { "smn", "Inari Sami" },
00389     { "sms", "Sami (Skolt)" },
00390     { "sms", "Skolt Sami" },
00391     { "so", "Somali" },
00392     { "sq", "Albanian" },
00393     { "sr", "Serbian (Latin)" },
00394     { "sr@cyrillic", "SRB" }, /* Serbian (Cyrillic) */
00395     { "sw", "Swahili" },
00396     { "syr", "Syriac" },
00397     { "ta", "Tamil" },
00398     { "te", "Telugu" },
00399     { "tg", "Tajik" },
00400     { "th", "Thai" },
00401     { "ti", "Tigrinya" },
00402     { "tk", "Turkmen" },
00403     { "tl", "Filipino" },
00404     { "tn", "Tswana" },
00405     { "tr", "Turkish" },
00406     { "ts", "Tsonga" },
00407     { "tt", "Tatar" },
00408     { "ug", "Uighur" },
00409     { "uk", "Ukrainian" },
00410     { "ur", "Urdu" },
00411     { "uz", "Uzbek" },
00412     { "uz", "Uzbek (Latin)" },
00413     { "uz@cyrillic", "Uzbek (Cyrillic)" },
00414     { "ve", "Venda" },
00415     { "vi", "Vietnamese" },
00416     { "wen", "Sorbian" },
00417     { "wo", "Wolof" },
00418     { "xh", "Xhosa" },
00419     { "yi", "Yiddish" },
00420     { "yo", "Yoruba" },
00421     { "zh", "Chinese" },
00422     { "zu", "Zulu" }
00423   };
00424 
00425 /* Table from ISO 3166 country code to English name.
00426    Keep in sync with the gl_locale_name_from_win32_LANGID function in
00427    localename.c!  */
00428 static const struct table_entry country_table[] =
00429   {
00430     { "AE", "U.A.E." },
00431     { "AF", "Afghanistan" },
00432     { "AL", "Albania" },
00433     { "AM", "Armenia" },
00434     { "AN", "Netherlands Antilles" },
00435     { "AR", "Argentina" },
00436     { "AT", "Austria" },
00437     { "AU", "Australia" },
00438     { "AZ", "Azerbaijan" },
00439     { "BA", "Bosnia and Herzegovina" },
00440     { "BD", "Bangladesh" },
00441     { "BE", "Belgium" },
00442     { "BG", "Bulgaria" },
00443     { "BH", "Bahrain" },
00444     { "BN", "Brunei Darussalam" },
00445     { "BO", "Bolivia" },
00446     { "BR", "Brazil" },
00447     { "BT", "Bhutan" },
00448     { "BY", "Belarus" },
00449     { "BZ", "Belize" },
00450     { "CA", "Canada" },
00451     { "CG", "Congo" },
00452     { "CH", "Switzerland" },
00453     { "CI", "Cote d'Ivoire" },
00454     { "CL", "Chile" },
00455     { "CM", "Cameroon" },
00456     { "CN", "People's Republic of China" },
00457     { "CO", "Colombia" },
00458     { "CR", "Costa Rica" },
00459     { "CS", "Serbia and Montenegro" },
00460     { "CZ", "Czech Republic" },
00461     { "DE", "Germany" },
00462     { "DK", "Denmark" },
00463     { "DO", "Dominican Republic" },
00464     { "DZ", "Algeria" },
00465     { "EC", "Ecuador" },
00466     { "EE", "Estonia" },
00467     { "EG", "Egypt" },
00468     { "ER", "Eritrea" },
00469     { "ES", "Spain" },
00470     { "ET", "Ethiopia" },
00471     { "FI", "Finland" },
00472     { "FO", "Faroe Islands" },
00473     { "FR", "France" },
00474     { "GB", "United Kingdom" },
00475     { "GD", "Caribbean" },
00476     { "GE", "Georgia" },
00477     { "GL", "Greenland" },
00478     { "GR", "Greece" },
00479     { "GT", "Guatemala" },
00480     { "HK", "Hong Kong" },
00481     { "HK", "Hong Kong S.A.R." },
00482     { "HN", "Honduras" },
00483     { "HR", "Croatia" },
00484     { "HT", "Haiti" },
00485     { "HU", "Hungary" },
00486     { "ID", "Indonesia" },
00487     { "IE", "Ireland" },
00488     { "IL", "Israel" },
00489     { "IN", "India" },
00490     { "IQ", "Iraq" },
00491     { "IR", "Iran" },
00492     { "IS", "Iceland" },
00493     { "IT", "Italy" },
00494     { "JM", "Jamaica" },
00495     { "JO", "Jordan" },
00496     { "JP", "Japan" },
00497     { "KE", "Kenya" },
00498     { "KG", "Kyrgyzstan" },
00499     { "KH", "Cambodia" },
00500     { "KR", "South Korea" },
00501     { "KW", "Kuwait" },
00502     { "KZ", "Kazakhstan" },
00503     { "LA", "Laos" },
00504     { "LB", "Lebanon" },
00505     { "LI", "Liechtenstein" },
00506     { "LK", "Sri Lanka" },
00507     { "LT", "Lithuania" },
00508     { "LU", "Luxembourg" },
00509     { "LV", "Latvia" },
00510     { "LY", "Libya" },
00511     { "MA", "Morocco" },
00512     { "MC", "Principality of Monaco" },
00513     { "MD", "Moldava" },
00514     { "MD", "Moldova" },
00515     { "ME", "Montenegro" },
00516     { "MK", "Former Yugoslav Republic of Macedonia" },
00517     { "ML", "Mali" },
00518     { "MM", "Myanmar" },
00519     { "MN", "Mongolia" },
00520     { "MO", "Macau S.A.R." },
00521     { "MT", "Malta" },
00522     { "MV", "Maldives" },
00523     { "MX", "Mexico" },
00524     { "MY", "Malaysia" },
00525     { "NG", "Nigeria" },
00526     { "NI", "Nicaragua" },
00527     { "NL", "Netherlands" },
00528     { "NO", "Norway" },
00529     { "NP", "Nepal" },
00530     { "NZ", "New Zealand" },
00531     { "OM", "Oman" },
00532     { "PA", "Panama" },
00533     { "PE", "Peru" },
00534     { "PH", "Philippines" },
00535     { "PK", "Islamic Republic of Pakistan" },
00536     { "PL", "Poland" },
00537     { "PR", "Puerto Rico" },
00538     { "PT", "Portugal" },
00539     { "PY", "Paraguay" },
00540     { "QA", "Qatar" },
00541     { "RE", "Reunion" },
00542     { "RO", "Romania" },
00543     { "RS", "Serbia" },
00544     { "RU", "Russia" },
00545     { "RW", "Rwanda" },
00546     { "SA", "Saudi Arabia" },
00547     { "SE", "Sweden" },
00548     { "SG", "Singapore" },
00549     { "SI", "Slovenia" },
00550     { "SK", "Slovak" },
00551     { "SN", "Senegal" },
00552     { "SO", "Somalia" },
00553     { "SR", "Suriname" },
00554     { "SV", "El Salvador" },
00555     { "SY", "Syria" },
00556     { "TH", "Thailand" },
00557     { "TJ", "Tajikistan" },
00558     { "TM", "Turkmenistan" },
00559     { "TN", "Tunisia" },
00560     { "TR", "Turkey" },
00561     { "TT", "Trinidad and Tobago" },
00562     { "TW", "Taiwan" },
00563     { "TZ", "Tanzania" },
00564     { "UA", "Ukraine" },
00565     { "US", "United States" },
00566     { "UY", "Uruguay" },
00567     { "VA", "Vatican" },
00568     { "VE", "Venezuela" },
00569     { "VN", "Viet Nam" },
00570     { "YE", "Yemen" },
00571     { "ZA", "South Africa" },
00572     { "ZW", "Zimbabwe" }
00573   };
00574 
00575 /* Given a string STRING, find the set of indices i such that TABLE[i].code is
00576    the given STRING.  It is a range [lo,hi-1].  */
00577 typedef struct { size_t lo; size_t hi; } range_t;
00578 static void
00579 search (const struct table_entry *table, size_t table_size, const char *string,
00580         range_t *result)
00581 {
00582   /* The table is sorted.  Perform a binary search.  */
00583   size_t hi = table_size;
00584   size_t lo = 0;
00585   while (lo < hi)
00586     {
00587       /* Invariant:
00588          for i < lo, strcmp (table[i].code, string) < 0,
00589          for i >= hi, strcmp (table[i].code, string) > 0.  */
00590       size_t mid = (hi + lo) >> 1; /* >= lo, < hi */
00591       int cmp = strcmp (table[mid].code, string);
00592       if (cmp < 0)
00593         lo = mid + 1;
00594       else if (cmp > 0)
00595         hi = mid;
00596       else
00597         {
00598           /* Found an i with
00599                strcmp (language_table[i].code, string) == 0.
00600              Find the entire interval of such i.  */
00601           {
00602             size_t i;
00603 
00604             for (i = mid; i > lo; )
00605               {
00606                 i--;
00607                 if (strcmp (table[i].code, string) < 0)
00608                   {
00609                     lo = i + 1;
00610                     break;
00611                   }
00612               }
00613           }
00614           {
00615             size_t i;
00616 
00617             for (i = mid; i < hi; i++)
00618               {
00619                 if (strcmp (table[i].code, string) > 0)
00620                   {
00621                     hi = i;
00622                     break;
00623                   }
00624               }
00625           }
00626           /* The set of i with
00627                strcmp (language_table[i].code, string) == 0
00628              is the interval [lo, hi-1].  */
00629           break;
00630         }
00631     }
00632   result->lo = lo;
00633   result->hi = hi;
00634 }
00635 
00636 /* Like setlocale, but accept also locale names in the form ll or ll_CC,
00637    where ll is an ISO 639 language code and CC is an ISO 3166 country code.  */
00638 static char *
00639 setlocale_unixlike (int category, const char *locale)
00640 {
00641   char *result;
00642   char llCC_buf[64];
00643   char ll_buf[64];
00644   char CC_buf[64];
00645 
00646   /* First, try setlocale with the original argument unchanged.  */
00647   result = setlocale (category, locale);
00648   if (result != NULL)
00649     return result;
00650 
00651   /* Otherwise, assume the argument is in the form
00652        language[_territory][.codeset][@modifier]
00653      and try to map it using the tables.  */
00654   if (strlen (locale) < sizeof (llCC_buf))
00655     {
00656       /* Second try: Remove the codeset part.  */
00657       {
00658         const char *p = locale;
00659         char *q = llCC_buf;
00660 
00661         /* Copy the part before the dot.  */
00662         for (; *p != '\0' && *p != '.'; p++, q++)
00663           *q = *p;
00664         if (*p == '.')
00665           /* Skip the part up to the '@', if any.  */
00666           for (; *p != '\0' && *p != '@'; p++)
00667             ;
00668         /* Copy the part starting with '@', if any.  */
00669         for (; *p != '\0'; p++, q++)
00670           *q = *p;
00671         *q = '\0';
00672       }
00673       /* llCC_buf now contains
00674            language[_territory][@modifier]
00675        */
00676       if (strcmp (llCC_buf, locale) != 0)
00677         {
00678           result = setlocale (category, llCC_buf);
00679           if (result != NULL)
00680             return result;
00681         }
00682       /* Look it up in language_table.  */
00683       {
00684         range_t range;
00685         size_t i;
00686 
00687         search (language_table,
00688                 sizeof (language_table) / sizeof (language_table[0]),
00689                 llCC_buf,
00690                 &range);
00691 
00692         for (i = range.lo; i < range.hi; i++)
00693           {
00694             /* Try the replacement in language_table[i].  */
00695             result = setlocale (category, language_table[i].english);
00696             if (result != NULL)
00697               return result;
00698           }
00699       }
00700       /* Split language[_territory][@modifier]
00701          into  ll_buf = language[@modifier]
00702          and   CC_buf = territory
00703        */
00704       {
00705         const char *underscore = strchr (llCC_buf, '_');
00706         if (underscore != NULL)
00707           {
00708             const char *territory_start = underscore + 1;
00709             const char *territory_end = strchr (territory_start, '@');
00710             if (territory_end == NULL)
00711               territory_end = territory_start + strlen (territory_start);
00712 
00713             memcpy (ll_buf, llCC_buf, underscore - llCC_buf);
00714             strcpy (ll_buf + (underscore - llCC_buf), territory_end);
00715 
00716             memcpy (CC_buf, territory_start, territory_end - territory_start);
00717             CC_buf[territory_end - territory_start] = '\0';
00718 
00719             {
00720               /* Look up ll_buf in language_table
00721                  and CC_buf in country_table.  */
00722               range_t language_range;
00723 
00724               search (language_table,
00725                       sizeof (language_table) / sizeof (language_table[0]),
00726                       ll_buf,
00727                       &language_range);
00728               if (language_range.lo < language_range.hi)
00729                 {
00730                   range_t country_range;
00731 
00732                   search (country_table,
00733                           sizeof (country_table) / sizeof (country_table[0]),
00734                           CC_buf,
00735                           &country_range);
00736                   if (country_range.lo < country_range.hi)
00737                     {
00738                       size_t i;
00739                       size_t j;
00740 
00741                       for (i = language_range.lo; i < language_range.hi; i++)
00742                         for (j = country_range.lo; j < country_range.hi; j++)
00743                           {
00744                             /* Concatenate the replacements.  */
00745                             const char *part1 = language_table[i].english;
00746                             size_t part1_len = strlen (part1);
00747                             const char *part2 = country_table[j].english;
00748                             size_t part2_len = strlen (part2) + 1;
00749                             char buf[64+64];
00750 
00751                             if (!(part1_len + 1 + part2_len <= sizeof (buf)))
00752                               abort ();
00753                             memcpy (buf, part1, part1_len);
00754                             buf[part1_len] = '_';
00755                             memcpy (buf + part1_len + 1, part2, part2_len);
00756 
00757                             /* Try the concatenated replacements.  */
00758                             result = setlocale (category, buf);
00759                             if (result != NULL)
00760                               return result;
00761                           }
00762                     }
00763 
00764                   /* Try omitting the country entirely.  This may set a locale
00765                      corresponding to the wrong country, but is better than
00766                      failing entirely.  */
00767                   {
00768                     size_t i;
00769 
00770                     for (i = language_range.lo; i < language_range.hi; i++)
00771                       {
00772                         /* Try only the language replacement.  */
00773                         result =
00774                           setlocale (category, language_table[i].english);
00775                         if (result != NULL)
00776                           return result;
00777                       }
00778                   }
00779                 }
00780             }
00781           }
00782       }
00783     }
00784 
00785   /* Failed.  */
00786   return NULL;
00787 }
00788 
00789 # else
00790 #  define setlocale_unixlike setlocale
00791 # endif
00792 
00793 # if LC_MESSAGES == 1729
00794 
00795 /* The system does not store an LC_MESSAGES locale category.  Do it here.  */
00796 static char lc_messages_name[64] = "C";
00797 
00798 /* Like setlocale, but support also LC_MESSAGES.  */
00799 static char *
00800 setlocale_single (int category, const char *locale)
00801 {
00802   if (category == LC_MESSAGES)
00803     {
00804       if (locale != NULL)
00805         {
00806           lc_messages_name[sizeof (lc_messages_name) - 1] = '\0';
00807           strncpy (lc_messages_name, locale, sizeof (lc_messages_name) - 1);
00808         }
00809       return lc_messages_name;
00810     }
00811   else
00812     return setlocale_unixlike (category, locale);
00813 }
00814 
00815 # else
00816 #  define setlocale_single setlocale_unixlike
00817 # endif
00818 
00819 DLL_EXPORTED
00820 char *
00821 libintl_setlocale (int category, const char *locale)
00822 {
00823   if (locale != NULL && locale[0] == '\0')
00824     {
00825       /* A request to the set the current locale to the default locale.  */
00826       if (category == LC_ALL)
00827         {
00828           /* Set LC_CTYPE first.  Then the other categories.  */
00829           static int const categories[] =
00830             {
00831               LC_NUMERIC,
00832               LC_TIME,
00833               LC_COLLATE,
00834               LC_MONETARY,
00835               LC_MESSAGES
00836             };
00837           char *saved_locale;
00838           const char *base_name;
00839           unsigned int i;
00840 
00841           /* Back up the old locale, in case one of the steps fails.  */
00842           saved_locale = setlocale (LC_ALL, NULL);
00843           if (saved_locale == NULL)
00844             return NULL;
00845           saved_locale = strdup (saved_locale);
00846           if (saved_locale == NULL)
00847             return NULL;
00848 
00849           /* Set LC_CTYPE category.  Set all other categories (except possibly
00850              LC_MESSAGES) to the same value in the same call; this is likely to
00851              save calls.  */
00852           base_name =
00853             gl_locale_name_environ (LC_CTYPE, category_to_name (LC_CTYPE));
00854           if (base_name == NULL)
00855             base_name = gl_locale_name_default ();
00856 
00857           if (setlocale_unixlike (LC_ALL, base_name) == NULL)
00858             goto fail;
00859 
00860           for (i = 0; i < sizeof (categories) / sizeof (categories[0]); i++)
00861             {
00862               int cat = categories[i];
00863               const char *name;
00864 
00865               name = gl_locale_name_environ (cat, category_to_name (cat));
00866               if (name == NULL)
00867                 name = gl_locale_name_default ();
00868 
00869               /* If name is the same as base_name, it has already been set
00870                  through the setlocale call before the loop.  */
00871               if (strcmp (name, base_name) != 0
00872 # if LC_MESSAGES == 1729
00873                   || cat == LC_MESSAGES
00874 # endif
00875                  )
00876                 if (setlocale_single (cat, name) == NULL)
00877                   goto fail;
00878             }
00879 
00880           /* All steps were successful.  */
00881           free (saved_locale);
00882           return setlocale (LC_ALL, NULL);
00883 
00884         fail:
00885           if (saved_locale[0] != '\0') /* don't risk an endless recursion */
00886             setlocale (LC_ALL, saved_locale);
00887           free (saved_locale);
00888           return NULL;
00889         }
00890       else
00891         {
00892           const char *name =
00893             gl_locale_name_environ (category, category_to_name (category));
00894           if (name == NULL)
00895             name = gl_locale_name_default ();
00896 
00897           return setlocale_single (category, name);
00898         }
00899     }
00900   else
00901     return setlocale_single (category, locale);
00902 }
00903 
00904 # if HAVE_NEWLOCALE
00905 
00906 DLL_EXPORTED
00907 locale_t
00908 libintl_newlocale (int category_mask, const char *locale, locale_t base)
00909 {
00910   if (category_mask != 0 && locale != NULL && locale[0] == '\0')
00911     {
00912       /* A request to construct a locale_t object that refers to the default
00913          locale.  */
00914 
00915       /* Set LC_CTYPE first.  Then the other categories.  */
00916       static struct { int cat; int mask; } const categories[] =
00917         {
00918           { LC_CTYPE,    LC_CTYPE_MASK },
00919           { LC_NUMERIC,  LC_NUMERIC_MASK },
00920           { LC_TIME,     LC_TIME_MASK },
00921           { LC_COLLATE,  LC_COLLATE_MASK },
00922           { LC_MONETARY, LC_MONETARY_MASK },
00923           { LC_MESSAGES, LC_MESSAGES_MASK }
00924         };
00925 
00926       locale_t orig_base = base;
00927 
00928       if ((LC_ALL_MASK & ~category_mask) == 0)
00929         {
00930           const char *base_name;
00931           unsigned int i;
00932 
00933           /* Set LC_CTYPE category.  Set all other categories (except possibly
00934              LC_MESSAGES) to the same value in the same call; this is likely to
00935              save calls.  */
00936           base_name =
00937             gl_locale_name_environ (LC_CTYPE, category_to_name (LC_CTYPE));
00938           if (base_name == NULL)
00939             base_name = gl_locale_name_default ();
00940 
00941           base = newlocale (LC_ALL_MASK, base_name, base);
00942           if (base == NULL)
00943             return NULL;
00944 
00945           for (i = 1; i < sizeof (categories) / sizeof (categories[0]); i++)
00946             {
00947               int category = categories[i].cat;
00948               int category_mask = categories[i].mask;
00949               const char *name;
00950 
00951               name =
00952                 gl_locale_name_environ (category, category_to_name (category));
00953               if (name == NULL)
00954                 name = gl_locale_name_default ();
00955 
00956               /* If name is the same as base_name, it has already been set
00957                  through the setlocale call before the loop.  */
00958               if (strcmp (name, base_name) != 0)
00959                 {
00960                   locale_t copy = newlocale (category_mask, name, base);
00961                   if (copy == NULL)
00962                     goto fail;
00963                   /* No need to call freelocale (base) if copy != base; the
00964                      newlocale function already takes care of doing it.  */
00965                   base = copy;
00966                 }
00967             }
00968         }
00969       else
00970         {
00971           unsigned int i;
00972 
00973           for (i = 0; i < sizeof (categories) / sizeof (categories[0]); i++)
00974             {
00975               int cat_mask = categories[i].mask;
00976 
00977               if ((category_mask & cat_mask) != 0)
00978                 {
00979                   int cat = categories[i].cat;
00980                   const char *name;
00981                   locale_t copy;
00982 
00983                   name = gl_locale_name_environ (cat, category_to_name (cat));
00984                   if (name == NULL)
00985                     name = gl_locale_name_default ();
00986 
00987                   copy = newlocale (cat_mask, name, base);
00988                   if (copy == NULL)
00989                     goto fail;
00990                   /* No need to call freelocale (base) if copy != base; the
00991                      newlocale function already takes care of doing it.  */
00992                   base = copy;
00993                 }
00994             }
00995         }
00996 
00997       /* All steps were successful.  */
00998       return base;
00999 
01000     fail:
01001       if (base != NULL && orig_base == NULL)
01002         {
01003           int saved_errno = errno;
01004           freelocale (base);
01005           errno = saved_errno;
01006         }
01007       return NULL;
01008     }
01009   else
01010     return newlocale (category_mask, locale, base);
01011 }
01012 
01013 # endif
01014 
01015 #endif