Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2. FUNCTION
  3. <<setlocale>>, <<localeconv>>---select or query locale
  4.  
  5. INDEX
  6.         setlocale
  7. INDEX
  8.         localeconv
  9. INDEX
  10.         _setlocale_r
  11. INDEX
  12.         _localeconv_r
  13.  
  14. ANSI_SYNOPSIS
  15.         #include <locale.h>
  16.         char *setlocale(int <[category]>, const char *<[locale]>);
  17.         lconv *localeconv(void);
  18.  
  19.         char *_setlocale_r(void *<[reent]>,
  20.                         int <[category]>, const char *<[locale]>);
  21.         lconv *_localeconv_r(void *<[reent]>);
  22.  
  23. TRAD_SYNOPSIS
  24.         #include <locale.h>
  25.         char *setlocale(<[category]>, <[locale]>)
  26.         int <[category]>;
  27.         char *<[locale]>;
  28.  
  29.         lconv *localeconv();
  30.  
  31.         char *_setlocale_r(<[reent]>, <[category]>, <[locale]>)
  32.         char *<[reent]>;
  33.         int <[category]>;
  34.         char *<[locale]>;
  35.  
  36.         lconv *_localeconv_r(<[reent]>);
  37.         char *<[reent]>;
  38.  
  39. DESCRIPTION
  40. <<setlocale>> is the facility defined by ANSI C to condition the
  41. execution environment for international collating and formatting
  42. information; <<localeconv>> reports on the settings of the current
  43. locale.
  44.  
  45. This is a minimal implementation, supporting only the required <<"POSIX">>
  46. and <<"C">> values for <[locale]>; strings representing other locales are not
  47. honored unless _MB_CAPABLE is defined.
  48.  
  49. If _MB_CAPABLE is defined, POSIX locale strings are allowed, following
  50. the form
  51.  
  52.   language[_TERRITORY][.charset][@@modifier]
  53.  
  54. <<"language">> is a two character string per ISO 639, or, if not available
  55. for a given language, a three character string per ISO 639-3.
  56. <<"TERRITORY">> is a country code per ISO 3166.  For <<"charset">> and
  57. <<"modifier">> see below.
  58.  
  59. Additionally to the POSIX specifier, the following extension is supported
  60. for backward compatibility with older implementations using newlib:
  61. <<"C-charset">>.
  62. Instead of <<"C-">>, you can also specify <<"C.">>.  Both variations allow
  63. to specify language neutral locales while using other charsets than ASCII,
  64. for instance <<"C.UTF-8">>, which keeps all settings as in the C locale,
  65. but uses the UTF-8 charset.
  66.  
  67. The following charsets are recognized:
  68. <<"UTF-8">>, <<"JIS">>, <<"EUCJP">>, <<"SJIS">>, <<"KOI8-R">>, <<"KOI8-U">>,
  69. <<"GEORGIAN-PS">>, <<"PT154">>, <<"TIS-620">>, <<"ISO-8859-x">> with
  70. 1 <= x <= 16, or <<"CPxxx">> with xxx in [437, 720, 737, 775, 850, 852, 855,
  71. 857, 858, 862, 866, 874, 932, 1125, 1250, 1251, 1252, 1253, 1254, 1255, 1256,
  72. 1257, 1258].
  73.  
  74. Charsets are case insensitive.  For instance, <<"EUCJP">> and <<"eucJP">>
  75. are equivalent.  Charset names with dashes can also be written without
  76. dashes, as in <<"UTF8">>, <<"iso88591">> or <<"koi8r">>.  <<"EUCJP">> and
  77. <<"EUCKR">> are also recognized with dash, <<"EUC-JP">> and <<"EUC-KR">>.
  78.  
  79. Full support for all of the above charsets requires that newlib has been
  80. build with multibyte support and support for all ISO and Windows Codepage.
  81. Otherwise all singlebyte charsets are simply mapped to ASCII.  Right now,
  82. only newlib for Cygwin is built with full charset support by default.
  83. Under Cygwin, this implementation additionally supports the charsets
  84. <<"GBK">>, <<"GB2312">>, <<"eucCN">>, <<"eucKR">>, and <<"Big5">>.  Cygwin
  85. does not support <<"JIS">>.
  86.  
  87. Cygwin additionally supports locales from the file
  88. /usr/share/locale/locale.alias.
  89.  
  90. (<<"">> is also accepted; if given, the settings are read from the
  91. corresponding LC_* environment variables and $LANG according to POSIX rules.
  92.  
  93. This implementation also supports a single modifier, <<"cjknarrow">>.
  94. Any other modifier is ignored.  <<"cjknarrow">>, in conjunction with one
  95. of the language specifiers <<"ja">>, <<"ko">>, and <<"zh">> specifies
  96. how the functions <<wcwidth>> and <<wcswidth>> handle characters from
  97. the "CJK Ambiguous Width" character class described in
  98. http://www.unicode.org/unicode/reports/tr11/.  Usually these characters
  99. have a width of 1, unless you specify one of the aforementioned
  100. languages, in which case these characters have a width of 2.  By
  101. specifying the <<"cjknarrow">> modifier, these characters will have a
  102. width of one in the languages <<"ja">>, <<"ko">>, and <<"zh">> as well.
  103.  
  104. If you use <<NULL>> as the <[locale]> argument, <<setlocale>> returns a
  105. pointer to the string representing the current locale.  The acceptable
  106. values for <[category]> are defined in `<<locale.h>>' as macros
  107. beginning with <<"LC_">>.
  108.  
  109. <<localeconv>> returns a pointer to a structure (also defined in
  110. `<<locale.h>>') describing the locale-specific conventions currently
  111. in effect.  
  112.  
  113. <<_localeconv_r>> and <<_setlocale_r>> are reentrant versions of
  114. <<localeconv>> and <<setlocale>> respectively.  The extra argument
  115. <[reent]> is a pointer to a reentrancy structure.
  116.  
  117. RETURNS
  118. A successful call to <<setlocale>> returns a pointer to a string
  119. associated with the specified category for the new locale.  The string
  120. returned by <<setlocale>> is such that a subsequent call using that
  121. string will restore that category (or all categories in case of LC_ALL),
  122. to that state.  The application shall not modify the string returned
  123. which may be overwritten by a subsequent call to <<setlocale>>.
  124. On error, <<setlocale>> returns <<NULL>>.
  125.  
  126. <<localeconv>> returns a pointer to a structure of type <<lconv>>,
  127. which describes the formatting and collating conventions in effect (in
  128. this implementation, always those of the C locale).
  129.  
  130. PORTABILITY
  131. ANSI C requires <<setlocale>>, but the only locale required across all
  132. implementations is the C locale.
  133.  
  134. NOTES
  135. There is no ISO-8859-12 codepage.  It's also refused by this implementation.
  136.  
  137. No supporting OS subroutines are required.
  138. */
  139.  
  140. /* Parts of this code are originally taken from FreeBSD. */
  141. /*
  142.  * Copyright (c) 1996 - 2002 FreeBSD Project
  143.  * Copyright (c) 1991, 1993
  144.  *      The Regents of the University of California.  All rights reserved.
  145.  *
  146.  * This code is derived from software contributed to Berkeley by
  147.  * Paul Borman at Krystal Technologies.
  148.  *
  149.  * Redistribution and use in source and binary forms, with or without
  150.  * modification, are permitted provided that the following conditions
  151.  * are met:
  152.  * 1. Redistributions of source code must retain the above copyright
  153.  *    notice, this list of conditions and the following disclaimer.
  154.  * 2. Redistributions in binary form must reproduce the above copyright
  155.  *    notice, this list of conditions and the following disclaimer in the
  156.  *    documentation and/or other materials provided with the distribution.
  157.  * 4. Neither the name of the University nor the names of its contributors
  158.  *    may be used to endorse or promote products derived from this software
  159.  *    without specific prior written permission.
  160.  *
  161.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  162.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  163.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  164.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  165.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  166.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  167.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  168.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  169.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  170.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  171.  * SUCH DAMAGE.
  172.  */
  173.  
  174. #include <newlib.h>
  175. #include <errno.h>
  176. #include <locale.h>
  177. #include <string.h>
  178. #include <limits.h>
  179. #include <reent.h>
  180. #include <stdlib.h>
  181. #include <wchar.h>
  182. #include "lmessages.h"
  183. #include "lmonetary.h"
  184. #include "lnumeric.h"
  185. #include "lctype.h"
  186. #include "../stdlib/local.h"
  187.  
  188. #define _LC_LAST      7
  189. #define ENCODING_LEN 31
  190.  
  191. int __EXPORT __mb_cur_max = 1;
  192.  
  193. int __nlocale_changed = 0;
  194. int __mlocale_changed = 0;
  195. char *_PathLocale = NULL;
  196.  
  197. static
  198. struct lconv lconv =
  199. {
  200.   ".", "", "", "", "", "", "", "", "", "",
  201.   CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX,
  202.   CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX,
  203.   CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX,
  204.   CHAR_MAX, CHAR_MAX
  205. };
  206.  
  207. #ifdef _MB_CAPABLE
  208. /*
  209.  * Category names for getenv()
  210.  */
  211. static char *categories[_LC_LAST] = {
  212.   "LC_ALL",
  213.   "LC_COLLATE",
  214.   "LC_CTYPE",
  215.   "LC_MONETARY",
  216.   "LC_NUMERIC",
  217.   "LC_TIME",
  218.   "LC_MESSAGES",
  219. };
  220.  
  221. /*
  222.  * Default locale per POSIX.  Can be overridden on a per-target base.
  223.  */
  224. #ifndef DEFAULT_LOCALE
  225. #define DEFAULT_LOCALE  "C"
  226. #endif
  227. /*
  228.  * This variable can be changed by any outside mechanism.  This allows,
  229.  * for instance, to load the default locale from a file.
  230.  */
  231. char __default_locale[ENCODING_LEN + 1] = DEFAULT_LOCALE;
  232.  
  233. /*
  234.  * Current locales for each category
  235.  */
  236. static char current_categories[_LC_LAST][ENCODING_LEN + 1] = {
  237.     "C",
  238.     "C",
  239.     "C",
  240.     "C",
  241.     "C",
  242.     "C",
  243.     "C",
  244. };
  245.  
  246. /*
  247.  * The locales we are going to try and load
  248.  */
  249. static char new_categories[_LC_LAST][ENCODING_LEN + 1];
  250. static char saved_categories[_LC_LAST][ENCODING_LEN + 1];
  251.  
  252. static char current_locale_string[_LC_LAST * (ENCODING_LEN + 1/*"/"*/ + 1)];
  253. static char *currentlocale(void);
  254. static char *loadlocale(struct _reent *, int);
  255. static const char *__get_locale_env(struct _reent *, int);
  256.  
  257. #endif /* _MB_CAPABLE */
  258.  
  259. #if 0 /*def __CYGWIN__  TODO: temporarily(?) disable C == UTF-8 */
  260. static char lc_ctype_charset[ENCODING_LEN + 1] = "UTF-8";
  261. static char lc_message_charset[ENCODING_LEN + 1] = "UTF-8";
  262. #else
  263. static char lc_ctype_charset[ENCODING_LEN + 1] = "ASCII";
  264. static char lc_message_charset[ENCODING_LEN + 1] = "ASCII";
  265. #endif
  266. static int lc_ctype_cjk_lang = 0;
  267.  
  268. char *
  269. _DEFUN(_setlocale_r, (p, category, locale),
  270.        struct _reent *p _AND
  271.        int category _AND
  272.        _CONST char *locale)
  273. {
  274. #ifndef _MB_CAPABLE
  275.   if (locale)
  276.     {
  277.       if (strcmp (locale, "POSIX") && strcmp (locale, "C")
  278.           && strcmp (locale, ""))
  279.         return NULL;
  280.     }
  281.   return "C";
  282. #else /* !_MB_CAPABLE */
  283.   int i, j, len, saverr;
  284.   const char *env, *r;
  285.  
  286.   if (category < LC_ALL || category >= _LC_LAST)
  287.     {
  288.       p->_errno = EINVAL;
  289.       return NULL;
  290.     }
  291.  
  292.   if (locale == NULL)
  293.     return category != LC_ALL ? current_categories[category] : currentlocale();
  294.  
  295.   /*
  296.    * Default to the current locale for everything.
  297.    */
  298.   for (i = 1; i < _LC_LAST; ++i)
  299.     strcpy (new_categories[i], current_categories[i]);
  300.  
  301.   /*
  302.    * Now go fill up new_categories from the locale argument
  303.    */
  304.   if (!*locale)
  305.     {
  306.       if (category == LC_ALL)
  307.         {
  308.           for (i = 1; i < _LC_LAST; ++i)
  309.             {
  310.               env = __get_locale_env (p, i);
  311.               if (strlen (env) > ENCODING_LEN)
  312.                 {
  313.                   p->_errno = EINVAL;
  314.                   return NULL;
  315.                 }
  316.               strcpy (new_categories[i], env);
  317.             }
  318.         }
  319.       else
  320.         {
  321.           env = __get_locale_env (p, category);
  322.           if (strlen (env) > ENCODING_LEN)
  323.             {
  324.               p->_errno = EINVAL;
  325.               return NULL;
  326.             }
  327.           strcpy (new_categories[category], env);
  328.         }
  329.     }
  330.   else if (category != LC_ALL)
  331.     {
  332.       if (strlen (locale) > ENCODING_LEN)
  333.         {
  334.           p->_errno = EINVAL;
  335.           return NULL;
  336.         }
  337.       strcpy (new_categories[category], locale);
  338.     }
  339.   else
  340.     {
  341.       if ((r = strchr (locale, '/')) == NULL)
  342.         {
  343.           if (strlen (locale) > ENCODING_LEN)
  344.             {
  345.               p->_errno = EINVAL;
  346.               return NULL;
  347.             }
  348.           for (i = 1; i < _LC_LAST; ++i)
  349.             strcpy (new_categories[i], locale);
  350.         }
  351.       else
  352.         {
  353.           for (i = 1; r[1] == '/'; ++r)
  354.             ;
  355.           if (!r[1])
  356.             {
  357.               p->_errno = EINVAL;
  358.               return NULL;  /* Hmm, just slashes... */
  359.             }
  360.           do
  361.             {
  362.               if (i == _LC_LAST)
  363.                 break;  /* Too many slashes... */
  364.               if ((len = r - locale) > ENCODING_LEN)
  365.                 {
  366.                   p->_errno = EINVAL;
  367.                   return NULL;
  368.                 }
  369.               strlcpy (new_categories[i], locale, len + 1);
  370.               i++;
  371.               while (*r == '/')
  372.                 r++;
  373.               locale = r;
  374.               while (*r && *r != '/')
  375.                 r++;
  376.             }
  377.           while (*locale);
  378.           while (i < _LC_LAST)
  379.             {
  380.               strcpy (new_categories[i], new_categories[i-1]);
  381.               i++;
  382.             }
  383.         }
  384.     }
  385.  
  386.   if (category != LC_ALL)
  387.     return loadlocale (p, category);
  388.  
  389.   for (i = 1; i < _LC_LAST; ++i)
  390.     {
  391.       strcpy (saved_categories[i], current_categories[i]);
  392.       if (loadlocale (p, i) == NULL)
  393.         {
  394.           saverr = p->_errno;
  395.           for (j = 1; j < i; j++)
  396.             {
  397.               strcpy (new_categories[j], saved_categories[j]);
  398.               if (loadlocale (p, j) == NULL)
  399.                 {
  400.                   strcpy (new_categories[j], "C");
  401.                   loadlocale (p, j);
  402.                 }
  403.             }
  404.           p->_errno = saverr;
  405.           return NULL;
  406.         }
  407.     }
  408.   return currentlocale ();
  409. #endif /* !_MB_CAPABLE */
  410. }
  411.  
  412. #ifdef _MB_CAPABLE
  413. static char *
  414. currentlocale()
  415. {
  416.         int i;
  417.  
  418.         (void)strcpy(current_locale_string, current_categories[1]);
  419.  
  420.         for (i = 2; i < _LC_LAST; ++i)
  421.                 if (strcmp(current_categories[1], current_categories[i])) {
  422.                         for (i = 2; i < _LC_LAST; ++i) {
  423.                                 (void)strcat(current_locale_string, "/");
  424.                                 (void)strcat(current_locale_string,
  425.                                              current_categories[i]);
  426.                         }
  427.                         break;
  428.                 }
  429.         return (current_locale_string);
  430. }
  431. #endif /* _MB_CAPABLE */
  432.  
  433. #ifdef _MB_CAPABLE
  434. #ifdef __CYGWIN__
  435. extern void __set_charset_from_locale (const char *locale, char *charset);
  436. extern int __set_locale_from_locale_alias (const char *, char *);
  437. extern int __collate_load_locale (const char *, void *, const char *);
  438. #endif /* __CYGWIN__ */
  439.  
  440. extern void __set_ctype (const char *charset);
  441.  
  442. static char *
  443. loadlocale(struct _reent *p, int category)
  444. {
  445.   /* At this point a full-featured system would just load the locale
  446.      specific data from the locale files.
  447.      What we do here for now is to check the incoming string for correctness.
  448.      The string must be in one of the allowed locale strings, either
  449.      one in POSIX-style, or one in the old newlib style to maintain
  450.      backward compatibility.  If the local string is correct, the charset
  451.      is extracted and stored in lc_ctype_charset or lc_message_charset
  452.      dependent on the cateogry. */
  453.   char *locale = NULL;
  454.   char charset[ENCODING_LEN + 1];
  455.   unsigned long val;
  456.   char *end, *c;
  457.   int mbc_max;
  458.   int (*l_wctomb) (struct _reent *, char *, wchar_t, const char *, mbstate_t *);
  459.   int (*l_mbtowc) (struct _reent *, wchar_t *, const char *, size_t,
  460.                    const char *, mbstate_t *);
  461.   int cjknarrow = 0;
  462.  
  463.   /* Avoid doing everything twice if nothing has changed. */
  464.   if (!strcmp (new_categories[category], current_categories[category]))
  465.     return current_categories[category];
  466.  
  467. #ifdef __CYGWIN__
  468.   /* This additional code handles the case that the incoming locale string
  469.      is not valid.  If so, it calls the function __set_locale_from_locale_alias,
  470.      which is only available on Cygwin right now.  The function reads the
  471.      file /usr/share/locale/locale.alias.  The file contains locale aliases
  472.      and their replacement locale.  For instance, the alias "french" is
  473.      translated to "fr_FR.ISO-8859-1", the alias "thai" is translated to
  474.      "th_TH.TIS-620".  If successful, the function returns with a pointer
  475.      to the second argument, which is a buffer in which the replacement locale
  476.      gets stored.  Otherwise the function returns NULL. */
  477.   char tmp_locale[ENCODING_LEN + 1];
  478.   int ret = 0;
  479.  
  480. restart:
  481.   if (!locale)
  482.     locale = new_categories[category];
  483.   else if (locale != tmp_locale)
  484.     {
  485.       locale = __set_locale_from_locale_alias (locale, tmp_locale);
  486.       if (!locale)
  487.         return NULL;
  488.     }
  489. # define FAIL   goto restart
  490. #else
  491.   locale = new_categories[category];
  492. # define FAIL   return NULL
  493. #endif
  494.  
  495.   /* "POSIX" is translated to "C", as on Linux. */
  496.   if (!strcmp (locale, "POSIX"))
  497.     strcpy (locale, "C");
  498.   if (!strcmp (locale, "C"))                            /* Default "C" locale */
  499. #if 0 /*def __CYGWIN__  TODO: temporarily(?) disable C == UTF-8 */
  500.     strcpy (charset, "UTF-8");
  501. #else
  502.     strcpy (charset, "ASCII");
  503. #endif
  504.   else if (locale[0] == 'C'
  505.            && (locale[1] == '-'         /* Old newlib style */
  506.                || locale[1] == '.'))    /* Extension for the C locale to allow
  507.                                            specifying different charsets while
  508.                                            sticking to the C locale in terms
  509.                                            of sort order, etc.  Proposed in
  510.                                            the Debian project. */
  511.     strcpy (charset, locale + 2);
  512.   else                                                  /* POSIX style */
  513.     {
  514.       c = locale;
  515.  
  516.       /* Don't use ctype macros here, they might be localized. */
  517.       /* Language */
  518.       if (c[0] < 'a' || c[0] > 'z'
  519.           || c[1] < 'a' || c[1] > 'z')
  520.         FAIL;
  521.       c += 2;
  522.       /* Allow three character Language per ISO 639-3 */
  523.       if (c[0] >= 'a' && c[0] <= 'z')
  524.         ++c;
  525.       if (c[0] == '_')
  526.         {
  527.           /* Territory */
  528.           ++c;
  529.           if (c[0] < 'A' || c[0] > 'Z'
  530.               || c[1] < 'A' || c[1] > 'Z')
  531.             FAIL;
  532.           c += 2;
  533.         }
  534.       if (c[0] == '.')
  535.         {
  536.           /* Charset */
  537.           char *chp;
  538.  
  539.           ++c;
  540.           strcpy (charset, c);
  541.           if ((chp = strchr (charset, '@')))
  542.             /* Strip off modifier */
  543.             *chp = '\0';
  544.           c += strlen (charset);
  545.         }
  546.       else if (c[0] == '\0' || c[0] == '@')
  547.         /* End of string or just a modifier */
  548. #ifdef __CYGWIN__
  549.         /* The Cygwin-only function __set_charset_from_locale checks
  550.            for the default charset which is connected to the given locale.
  551.            The function uses Windows functions in turn so it can't be easily
  552.            adapted to other targets.  However, if any other target provides
  553.            equivalent functionality, preferrably using the same function name
  554.            it would be sufficient to change the guarding #ifdef. */
  555.         __set_charset_from_locale (locale, charset);
  556. #else
  557.         strcpy (charset, "ISO-8859-1");
  558. #endif
  559.       else
  560.         /* Invalid string */
  561.         FAIL;
  562.       if (c[0] == '@')
  563.         {
  564.           /* Modifier */
  565.           /* Only one modifier is recognized right now.  "cjknarrow" is used
  566.              to modify the behaviour of wcwidth() for East Asian languages.
  567.              For details see the comment at the end of this function. */
  568.           if (!strcmp (c + 1, "cjknarrow"))
  569.             cjknarrow = 1;
  570.         }
  571.     }
  572.   /* We only support this subset of charsets. */
  573.   switch (charset[0])
  574.     {
  575.     case 'U':
  576.     case 'u':
  577.       if (strcasecmp (charset, "UTF-8") && strcasecmp (charset, "UTF8"))
  578.         FAIL;
  579.       strcpy (charset, "UTF-8");
  580.       mbc_max = 6;
  581.       l_wctomb = __utf8_wctomb;
  582.       l_mbtowc = __utf8_mbtowc;
  583.     break;
  584. #ifndef __CYGWIN__
  585.     /* Cygwin does not support JIS at all. */
  586.     case 'J':
  587.     case 'j':
  588.       if (strcasecmp (charset, "JIS"))
  589.         FAIL;
  590.       strcpy (charset, "JIS");
  591.       mbc_max = 8;
  592.       l_wctomb = __jis_wctomb;
  593.       l_mbtowc = __jis_mbtowc;
  594.     break;
  595. #endif /* !__CYGWIN__ */
  596.     case 'E':
  597.     case 'e':
  598.       if (strncasecmp (charset, "EUC", 3))
  599.         FAIL;
  600.       c = charset + 3;
  601.       if (*c == '-')
  602.         ++c;
  603.       if (!strcasecmp (c, "JP"))
  604.         {
  605.           strcpy (charset, "EUCJP");
  606.           mbc_max = 3;
  607.           l_wctomb = __eucjp_wctomb;
  608.           l_mbtowc = __eucjp_mbtowc;
  609.         }
  610. #ifdef __CYGWIN__
  611.       /* Newlib does neither provide EUC-KR nor EUC-CN, and Cygwin's
  612.          implementation requires Windows support. */
  613.       else if (!strcasecmp (c, "KR"))
  614.         {
  615.           strcpy (charset, "EUCKR");
  616.           mbc_max = 2;
  617.           l_wctomb = __kr_wctomb;
  618.           l_mbtowc = __kr_mbtowc;
  619.         }
  620.       else if (!strcasecmp (c, "CN"))
  621.         {
  622.           strcpy (charset, "EUCCN");
  623.           mbc_max = 2;
  624.           l_wctomb = __gbk_wctomb;
  625.           l_mbtowc = __gbk_mbtowc;
  626.         }
  627. #endif /* __CYGWIN__ */
  628.       else
  629.         FAIL;
  630.     break;
  631.     case 'S':
  632.     case 's':
  633.       if (strcasecmp (charset, "SJIS"))
  634.         FAIL;
  635.       strcpy (charset, "SJIS");
  636.       mbc_max = 2;
  637.       l_wctomb = __sjis_wctomb;
  638.       l_mbtowc = __sjis_mbtowc;
  639.     break;
  640.     case 'I':
  641.     case 'i':
  642.       /* Must be exactly one of ISO-8859-1, [...] ISO-8859-16, except for
  643.          ISO-8859-12.  This code also recognizes the aliases without dashes. */
  644.       if (strncasecmp (charset, "ISO", 3))
  645.         FAIL;
  646.       c = charset + 3;
  647.       if (*c == '-')
  648.         ++c;
  649.       if (strncasecmp (c, "8859", 4))
  650.         FAIL;
  651.       c += 4;
  652.       if (*c == '-')
  653.         ++c;
  654.       val = _strtol_r (p, c, &end, 10);
  655.       if (val < 1 || val > 16 || val == 12 || *end)
  656.         FAIL;
  657.       strcpy (charset, "ISO-8859-");
  658.       c = charset + 9;
  659.       if (val > 10)
  660.         *c++ = '1';
  661.       *c++ = val % 10 + '0';
  662.       *c = '\0';
  663.       mbc_max = 1;
  664. #ifdef _MB_EXTENDED_CHARSETS_ISO
  665.       l_wctomb = __iso_wctomb;
  666.       l_mbtowc = __iso_mbtowc;
  667. #else /* !_MB_EXTENDED_CHARSETS_ISO */
  668.       l_wctomb = __ascii_wctomb;
  669.       l_mbtowc = __ascii_mbtowc;
  670. #endif /* _MB_EXTENDED_CHARSETS_ISO */
  671.     break;
  672.     case 'C':
  673.     case 'c':
  674.       if (charset[1] != 'P' && charset[1] != 'p')
  675.         FAIL;
  676.       strncpy (charset, "CP", 2);
  677.       val = _strtol_r (p, charset + 2, &end, 10);
  678.       if (*end)
  679.         FAIL;
  680.       switch (val)
  681.         {
  682.         case 437:
  683.         case 720:
  684.         case 737:
  685.         case 775:
  686.         case 850:
  687.         case 852:
  688.         case 855:
  689.         case 857:
  690.         case 858:
  691.         case 862:
  692.         case 866:
  693.         case 874:
  694.         case 1125:
  695.         case 1250:
  696.         case 1251:
  697.         case 1252:
  698.         case 1253:
  699.         case 1254:
  700.         case 1255:
  701.         case 1256:
  702.         case 1257:
  703.         case 1258:
  704.           mbc_max = 1;
  705. #ifdef _MB_EXTENDED_CHARSETS_WINDOWS
  706.           l_wctomb = __cp_wctomb;
  707.           l_mbtowc = __cp_mbtowc;
  708. #else /* !_MB_EXTENDED_CHARSETS_WINDOWS */
  709.           l_wctomb = __ascii_wctomb;
  710.           l_mbtowc = __ascii_mbtowc;
  711. #endif /* _MB_EXTENDED_CHARSETS_WINDOWS */
  712.           break;
  713.         case 932:
  714.           mbc_max = 2;
  715.           l_wctomb = __sjis_wctomb;
  716.           l_mbtowc = __sjis_mbtowc;
  717.           break;
  718.         default:
  719.           FAIL;
  720.         }
  721.     break;
  722.     case 'K':
  723.     case 'k':
  724.       /* KOI8-R, KOI8-U and the aliases without dash */
  725.       if (strncasecmp (charset, "KOI8", 4))
  726.         FAIL;
  727.       c = charset + 4;
  728.       if (*c == '-')
  729.         ++c;
  730.       if (*c == 'R' || *c == 'r')
  731.         strcpy (charset, "CP20866");
  732.       else if (*c == 'U' || *c == 'u')
  733.         strcpy (charset, "CP21866");
  734.       else
  735.         FAIL;
  736.       mbc_max = 1;
  737. #ifdef _MB_EXTENDED_CHARSETS_WINDOWS
  738.       l_wctomb = __cp_wctomb;
  739.       l_mbtowc = __cp_mbtowc;
  740. #else /* !_MB_EXTENDED_CHARSETS_WINDOWS */
  741.       l_wctomb = __ascii_wctomb;
  742.       l_mbtowc = __ascii_mbtowc;
  743. #endif /* _MB_EXTENDED_CHARSETS_WINDOWS */
  744.       break;
  745.     case 'A':
  746.     case 'a':
  747.       if (strcasecmp (charset, "ASCII"))
  748.         FAIL;
  749.       strcpy (charset, "ASCII");
  750.       mbc_max = 1;
  751.       l_wctomb = __ascii_wctomb;
  752.       l_mbtowc = __ascii_mbtowc;
  753.       break;
  754.     case 'G':
  755.     case 'g':
  756. #ifdef __CYGWIN__
  757.       /* Newlib does not provide GBK/GB2312 and Cygwin's implementation
  758.          requires Windows support. */
  759.       if (!strcasecmp (charset, "GBK")
  760.           || !strcasecmp (charset, "GB2312"))
  761.         {
  762.           strcpy (charset, charset[2] == '2' ? "GB2312" : "GBK");
  763.       mbc_max = 2;
  764.       l_wctomb = __gbk_wctomb;
  765.       l_mbtowc = __gbk_mbtowc;
  766.         }
  767.       else
  768. #endif /* __CYGWIN__ */
  769.       /* GEORGIAN-PS and the alias without dash */
  770.       if (!strncasecmp (charset, "GEORGIAN", 8))
  771.         {
  772.           c = charset + 8;
  773.           if (*c == '-')
  774.             ++c;
  775.           if (strcasecmp (c, "PS"))
  776.             FAIL;
  777.           strcpy (charset, "CP101");
  778.           mbc_max = 1;
  779. #ifdef _MB_EXTENDED_CHARSETS_WINDOWS
  780.           l_wctomb = __cp_wctomb;
  781.           l_mbtowc = __cp_mbtowc;
  782. #else /* !_MB_EXTENDED_CHARSETS_WINDOWS */
  783.           l_wctomb = __ascii_wctomb;
  784.           l_mbtowc = __ascii_mbtowc;
  785. #endif /* _MB_EXTENDED_CHARSETS_WINDOWS */
  786.         }
  787.       else
  788.         FAIL;
  789.       break;
  790.     case 'P':
  791.     case 'p':
  792.       /* PT154 */
  793.       if (strcasecmp (charset, "PT154"))
  794.         FAIL;
  795.       strcpy (charset, "CP102");
  796.       mbc_max = 1;
  797. #ifdef _MB_EXTENDED_CHARSETS_WINDOWS
  798.       l_wctomb = __cp_wctomb;
  799.       l_mbtowc = __cp_mbtowc;
  800. #else /* !_MB_EXTENDED_CHARSETS_WINDOWS */
  801.       l_wctomb = __ascii_wctomb;
  802.       l_mbtowc = __ascii_mbtowc;
  803. #endif /* _MB_EXTENDED_CHARSETS_WINDOWS */
  804.       break;
  805.     case 'T':
  806.     case 't':
  807.       if (strncasecmp (charset, "TIS", 3))
  808.         FAIL;
  809.       c = charset + 3;
  810.       if (*c == '-')
  811.         ++c;
  812.       if (strcasecmp (c, "620"))
  813.         FAIL;
  814.       strcpy (charset, "CP874");
  815.       mbc_max = 1;
  816. #ifdef _MB_EXTENDED_CHARSETS_WINDOWS
  817.       l_wctomb = __cp_wctomb;
  818.       l_mbtowc = __cp_mbtowc;
  819. #else /* !_MB_EXTENDED_CHARSETS_WINDOWS */
  820.       l_wctomb = __ascii_wctomb;
  821.       l_mbtowc = __ascii_mbtowc;
  822. #endif /* _MB_EXTENDED_CHARSETS_WINDOWS */
  823.       break;
  824. #ifdef __CYGWIN__
  825.     /* Newlib does not provide Big5 and Cygwin's implementation
  826.        requires Windows support. */
  827.     case 'B':
  828.     case 'b':
  829.       if (strcasecmp (charset, "BIG5"))
  830.         FAIL;
  831.       strcpy (charset, "BIG5");
  832.       mbc_max = 2;
  833.       l_wctomb = __big5_wctomb;
  834.       l_mbtowc = __big5_mbtowc;
  835.       break;
  836. #endif /* __CYGWIN__ */
  837.     default:
  838.       FAIL;
  839.     }
  840.   switch (category)
  841.     {
  842.     case LC_CTYPE:
  843.       strcpy (lc_ctype_charset, charset);
  844.       __mb_cur_max = mbc_max;
  845.       __wctomb = l_wctomb;
  846.       __mbtowc = l_mbtowc;
  847.       __set_ctype (charset);
  848.       /* Check for the language part of the locale specifier.  In case
  849.          of "ja", "ko", or "zh", assume the use of CJK fonts, unless the
  850.          "@cjknarrow" modifier has been specifed.
  851.          The result is stored in lc_ctype_cjk_lang and tested in wcwidth()
  852.          to figure out the width to return (1 or 2) for the "CJK Ambiguous
  853.          Width" category of characters. */
  854.       lc_ctype_cjk_lang = !cjknarrow
  855.                           && ((strncmp (locale, "ja", 2) == 0
  856.                               || strncmp (locale, "ko", 2) == 0
  857.                               || strncmp (locale, "zh", 2) == 0));
  858. #ifdef __HAVE_LOCALE_INFO__
  859.       ret = __ctype_load_locale (locale, (void *) l_wctomb, charset, mbc_max);
  860. #endif /* __HAVE_LOCALE_INFO__ */
  861.       break;
  862.     case LC_MESSAGES:
  863.       strcpy (lc_message_charset, charset);
  864. #ifdef __HAVE_LOCALE_INFO__
  865.       ret = __messages_load_locale (locale, (void *) l_wctomb, charset);
  866.       if (!ret)
  867. #endif /* __HAVE_LOCALE_INFO__ */
  868.       break;
  869. #ifdef __HAVE_LOCALE_INFO__
  870. #ifdef __CYGWIN__
  871.   /* Right now only Cygwin supports a __collate_load_locale function at all. */
  872.     case LC_COLLATE:
  873.       ret = __collate_load_locale (locale, (void *) l_mbtowc, charset);
  874.       break;
  875. #endif
  876.     case LC_MONETARY:
  877.       ret = __monetary_load_locale (locale, (void *) l_wctomb, charset);
  878.       break;
  879.     case LC_NUMERIC:
  880.       ret = __numeric_load_locale (locale, (void *) l_wctomb, charset);
  881.       break;
  882.     case LC_TIME:
  883.       ret = __time_load_locale (locale, (void *) l_wctomb, charset);
  884.       break;
  885. #endif /* __HAVE_LOCALE_INFO__ */
  886.     default:
  887.       break;
  888.     }
  889. #ifdef __HAVE_LOCALE_INFO__
  890.   if (ret)
  891.     FAIL;
  892. #endif /* __HAVE_LOCALE_INFO__ */
  893.   return strcpy(current_categories[category], new_categories[category]);
  894. }
  895.  
  896. static const char *
  897. __get_locale_env(struct _reent *p, int category)
  898. {
  899.   const char *env;
  900.  
  901.   /* 1. check LC_ALL. */
  902.   env = _getenv_r (p, categories[0]);
  903.  
  904.   /* 2. check LC_* */
  905.   if (env == NULL || !*env)
  906.     env = _getenv_r (p, categories[category]);
  907.  
  908.   /* 3. check LANG */
  909.   if (env == NULL || !*env)
  910.     env = _getenv_r (p, "LANG");
  911.  
  912.   /* 4. if none is set, fall to default locale */
  913.   if (env == NULL || !*env)
  914.     env = __default_locale;
  915.  
  916.   return env;
  917. }
  918. #endif /* _MB_CAPABLE */
  919.  
  920. char *
  921. _DEFUN_VOID(__locale_charset)
  922. {
  923. #if 0//def __HAVE_LOCALE_INFO__
  924.   return __get_current_ctype_locale ()->codeset;
  925. #else
  926.   return lc_ctype_charset;
  927. #endif
  928. }
  929.  
  930. int
  931. _DEFUN_VOID(__locale_mb_cur_max)
  932. {
  933. #if 0//def __HAVE_LOCALE_INFO__
  934.   return __get_current_ctype_locale ()->mb_cur_max[0];
  935. #else
  936.   return __mb_cur_max;
  937. #endif
  938. }
  939.  
  940.  
  941. char *
  942. _DEFUN_VOID(__locale_msgcharset)
  943. {
  944. #ifdef __HAVE_LOCALE_INFO__
  945.   return __get_current_messages_locale ()->codeset;
  946. #else
  947.   return lc_message_charset;
  948. #endif
  949. }
  950.  
  951. int
  952. _DEFUN_VOID(__locale_cjk_lang)
  953. {
  954.   return lc_ctype_cjk_lang;
  955. }
  956.  
  957. struct lconv *
  958. _DEFUN(_localeconv_r, (data),
  959.       struct _reent *data)
  960. {
  961. #ifdef __HAVE_LOCALE_INFO__
  962.   if (__nlocale_changed)
  963.     {
  964.       struct lc_numeric_T *n = __get_current_numeric_locale ();
  965.       lconv.decimal_point = n->decimal_point;
  966.       lconv.thousands_sep = n->thousands_sep;
  967.       lconv.grouping = n->grouping;
  968.       __nlocale_changed = 0;
  969.     }
  970.   if (__mlocale_changed)
  971.     {
  972.       struct lc_monetary_T *m = __get_current_monetary_locale ();
  973.       lconv.int_curr_symbol = m->int_curr_symbol;
  974.       lconv.currency_symbol = m->currency_symbol;
  975.       lconv.mon_decimal_point = m->mon_decimal_point;
  976.       lconv.mon_thousands_sep = m->mon_thousands_sep;
  977.       lconv.mon_grouping = m->mon_grouping;
  978.       lconv.positive_sign = m->positive_sign;
  979.       lconv.negative_sign = m->negative_sign;
  980.       lconv.int_frac_digits = m->int_frac_digits[0];
  981.       lconv.frac_digits = m->frac_digits[0];
  982.       lconv.p_cs_precedes = m->p_cs_precedes[0];
  983.       lconv.p_sep_by_space = m->p_sep_by_space[0];
  984.       lconv.n_cs_precedes = m->n_cs_precedes[0];
  985.       lconv.n_sep_by_space = m->n_sep_by_space[0];
  986.       lconv.p_sign_posn = m->p_sign_posn[0];
  987.       lconv.n_sign_posn = m->n_sign_posn[0];
  988. #ifdef __HAVE_LOCALE_INFO_EXTENDED__
  989.       lconv.int_p_cs_precedes = m->int_p_cs_precedes[0];
  990.       lconv.int_p_sep_by_space = m->int_p_sep_by_space[0];
  991.       lconv.int_n_cs_precedes = m->int_n_cs_precedes[0];
  992.       lconv.int_n_sep_by_space = m->int_n_sep_by_space[0];
  993.       lconv.int_n_sign_posn = m->int_n_sign_posn[0];
  994.       lconv.int_p_sign_posn = m->int_p_sign_posn[0];
  995. #else /* !__HAVE_LOCALE_INFO_EXTENDED__ */
  996.       lconv.int_p_cs_precedes = m->p_cs_precedes[0];
  997.       lconv.int_p_sep_by_space = m->p_sep_by_space[0];
  998.       lconv.int_n_cs_precedes = m->n_cs_precedes[0];
  999.       lconv.int_n_sep_by_space = m->n_sep_by_space[0];
  1000.       lconv.int_n_sign_posn = m->n_sign_posn[0];
  1001.       lconv.int_p_sign_posn = m->p_sign_posn[0];
  1002. #endif /* !__HAVE_LOCALE_INFO_EXTENDED__ */
  1003.       __mlocale_changed = 0;
  1004.     }
  1005. #endif /* __HAVE_LOCALE_INFO__ */
  1006.   return (struct lconv *) &lconv;
  1007. }
  1008.  
  1009. #ifndef _REENT_ONLY
  1010.  
  1011. #ifndef __CYGWIN__
  1012. /* Cygwin provides its own version of setlocale to perform some more
  1013.    initialization work.  It calls _setlocale_r, though. */
  1014. char *
  1015. _DEFUN(setlocale, (category, locale),
  1016.        int category _AND
  1017.        _CONST char *locale)
  1018. {
  1019.   return _setlocale_r (_REENT, category, locale);
  1020. }
  1021. #endif /* __CYGWIN__ */
  1022.  
  1023. struct lconv *
  1024. _DEFUN_VOID(localeconv)
  1025. {
  1026.   return _localeconv_r (_REENT);
  1027. }
  1028.  
  1029. #endif
  1030.