Update.
[platform/upstream/glibc.git] / wcsmbs / wcsmbsload.c
1 /* Copyright (C) 1998, 1999 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public
16    License along with the GNU C Library; see the file COPYING.LIB.  If not,
17    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, USA.  */
19
20 #include <ctype.h>
21 #include <langinfo.h>
22 #include <limits.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <locale/localeinfo.h>
27 #include <wcsmbsload.h>
28 #include <bits/libc-lock.h>
29 #include <iconv/gconv_int.h>
30
31
32 /* Last loaded locale for LC_CTYPE.  We initialize for the C locale
33    which is enabled at startup.  */
34 extern const struct locale_data _nl_C_LC_CTYPE;
35 const struct locale_data *__wcsmbs_last_locale = &_nl_C_LC_CTYPE;
36
37
38 /* These are the descriptions for the default conversion functions.  */
39 static struct __gconv_step to_wc =
40 {
41   .__shlib_handle = NULL,
42   .__modname = NULL,
43   .__counter = INT_MAX,
44   .__from_name = "ANSI_X3.4-1968//",
45   .__to_name = "INTERNAL",
46   .__fct = __gconv_transform_ascii_internal,
47   .__init_fct = NULL,
48   .__end_fct = NULL,
49   .__min_needed_from = 1,
50   .__max_needed_from = 1,
51   .__min_needed_to = 4,
52   .__max_needed_to = 4,
53   .__stateful = 0,
54   .__data = NULL
55 };
56
57 static struct __gconv_step to_mb =
58 {
59   .__shlib_handle = NULL,
60   .__modname = NULL,
61   .__counter = INT_MAX,
62   .__from_name = "INTERNAL",
63   .__to_name = "ANSI_X3.4-1968//",
64   .__fct = __gconv_transform_internal_ascii,
65   .__init_fct = NULL,
66   .__end_fct = NULL,
67   .__min_needed_from = 4,
68   .__max_needed_from = 4,
69   .__min_needed_to = 1,
70   .__max_needed_to = 1,
71   .__stateful = 0,
72   .__data = NULL
73 };
74
75
76 /* For the default locale we only have to handle ANSI_X3.4-1968.  */
77 struct gconv_fcts __wcsmbs_gconv_fcts =
78 {
79   .towc = &to_wc,
80   .tomb = &to_mb
81 };
82
83
84 static inline struct __gconv_step *
85 getfct (const char *to, const char *from)
86 {
87   size_t nsteps;
88   struct __gconv_step *result;
89   size_t nstateful;
90   size_t cnt;
91
92   if (__gconv_find_transform (to, from, &result, &nsteps) != __GCONV_OK)
93     /* Loading the conversion step is not possible.  */
94     return NULL;
95
96   /* Count the number of stateful conversions.  Since we will only
97      have one 'mbstate_t' object available we can only deal with one
98      stateful conversion.  */
99   nstateful = 0;
100   for (cnt = 0; cnt < nsteps; ++cnt)
101     if (result[cnt].__stateful)
102       ++nstateful;
103   if (nstateful > 1)
104     {
105       /* We cannot handle this case.  */
106       __gconv_close_transform (result, nsteps);
107       result = NULL;
108     }
109
110   return result;
111 }
112
113
114 /* Extract from the given locale name the character set portion.  Since
115    only the XPG form of the name includes this information we don't have
116    to take care for the CEN form.  */
117 #define extract_charset_name(str) \
118   ({                                                                          \
119     const char *cp = str;                                                     \
120     char *result = NULL;                                                      \
121                                                                               \
122     cp += strcspn (cp, "@.+,");                                               \
123     if (*cp == '.')                                                           \
124       {                                                                       \
125         const char *endp = ++cp;                                              \
126         while (*endp != '\0' && *endp != '@')                                 \
127           ++endp;                                                             \
128         if (endp != cp)                                                       \
129           result = strndupa (cp, endp - cp);                                  \
130       }                                                                       \
131     result;                                                                   \
132   })
133
134
135 /* The gconv functions expects the name to be complete, including the
136    trailing shashes if necessary.  */
137 #define norm_add_slashes(str) \
138   ({                                                                          \
139     const char *cp = str;                                                     \
140     char *result;                                                             \
141     char *tmp;                                                                \
142     size_t cnt = 0;                                                           \
143                                                                               \
144     while (*cp != '\0')                                                       \
145       if (*cp++ == '/')                                                       \
146         ++cnt;                                                                \
147                                                                               \
148     tmp = result = alloca (cp - str + 3);                                     \
149     cp = str;                                                                 \
150     while (*cp != '\0')                                                       \
151       *tmp++ = _toupper (*cp++);                                              \
152     if (cnt < 2)                                                              \
153       {                                                                       \
154         *tmp++ = '/';                                                         \
155         if (cnt < 1)                                                          \
156           *tmp++ = '/';                                                       \
157       }                                                                       \
158     *tmp = '\0';                                                              \
159     result;                                                                   \
160   })
161
162
163 /* We must modify global data.  */
164 __libc_lock_define_initialized (static, lock)
165
166
167 /* Load conversion functions for the currently selected locale.  */
168 void
169 internal_function
170 __wcsmbs_load_conv (const struct locale_data *new_category)
171 {
172   /* Acquire the lock.  */
173   __libc_lock_lock (lock);
174
175   /* We should repest the test since while we waited some other thread
176      might have run this function.  */
177   if (__wcsmbs_last_locale != new_category)
178     {
179       if (new_category->name == _nl_C_name)     /* Yes, pointer comparison.  */
180         {
181         failed:
182           __wcsmbs_gconv_fcts.towc = &to_wc;
183           __wcsmbs_gconv_fcts.tomb = &to_mb;
184         }
185       else
186         {
187           /* We must find the real functions.  */
188           const char *charset_name;
189           const char *complete_name;
190           struct __gconv_step *new_towc;
191           struct __gconv_step *new_tomb;
192
193           /* Free the old conversions.  */
194           __gconv_close_transform (__wcsmbs_gconv_fcts.tomb, 1);
195           __gconv_close_transform (__wcsmbs_gconv_fcts.towc, 1);
196
197           /* Get name of charset of the locale.  We first examine
198              whether we have a character set mentioned in the locale
199              name.  If this isn't the case we use the information from
200              the locale files.  */
201           charset_name = extract_charset_name (setlocale (LC_CTYPE, NULL));
202           if (charset_name == NULL)
203             charset_name =
204               new_category->values[_NL_ITEM_INDEX(CODESET)].string;
205
206           /* Normalize the name and add the slashes necessary for a
207              complete lookup.  */
208           complete_name = norm_add_slashes (charset_name);
209
210           new_towc = getfct ("INTERNAL", complete_name);
211           if (new_towc != NULL)
212             new_tomb = getfct (complete_name, "INTERNAL");
213
214           /* If any of the conversion functions is not available we don't
215              use any since this would mean we cannot convert back and
216              forth.*/
217           if (new_towc == NULL || new_tomb == NULL)
218             {
219               if (new_towc != NULL)
220                 __gconv_close_transform (new_towc, 1);
221
222               goto failed;
223             }
224
225           __wcsmbs_gconv_fcts.tomb = new_tomb;
226           __wcsmbs_gconv_fcts.towc = new_towc;
227         }
228
229       /* Set last-used variable for current locale.  */
230       __wcsmbs_last_locale = new_category;
231     }
232
233   __libc_lock_unlock (lock);
234 }
235
236
237 /* Clone the current conversion function set.  */
238 void
239 internal_function
240 __wcsmbs_clone_conv (struct gconv_fcts *copy)
241 {
242   /* Make sure the data structures remain the same until we are finished.  */
243   __libc_lock_lock (lock);
244
245   /* Copy the data.  */
246   *copy = __wcsmbs_gconv_fcts;
247
248   /* Now increment the usage counters.  */
249   if (copy->towc->__shlib_handle != NULL)
250     ++copy->towc->__counter;
251   if (copy->tomb->__shlib_handle != NULL)
252     ++copy->tomb->__counter;
253
254   __libc_lock_unlock (lock);
255 }