* stdlib/tst-setcontext.c (main): If makecontext does nothing,
[glibc.git] / wcsmbs / wcsmbsload.c
blobfc846c7237fab98f39c6bde8033bfc602f752b3a
1 /* Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <ctype.h>
21 #include <langinfo.h>
22 #include <limits.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #include <locale/localeinfo.h>
27 #include <wcsmbsload.h>
28 #include <bits/libc-lock.h>
31 /* Last loaded locale for LC_CTYPE. We initialize for the C locale
32 which is enabled at startup. */
33 extern const struct locale_data _nl_C_LC_CTYPE;
34 const struct locale_data *__wcsmbs_last_locale = &_nl_C_LC_CTYPE;
37 /* These are the descriptions for the default conversion functions. */
38 struct __gconv_step __wcsmbs_to_wc attribute_hidden =
40 .__shlib_handle = NULL,
41 .__modname = NULL,
42 .__counter = INT_MAX,
43 .__from_name = (char *) "ANSI_X3.4-1968//TRANSLIT",
44 .__to_name = (char *) "INTERNAL",
45 .__fct = __gconv_transform_ascii_internal,
46 .__init_fct = NULL,
47 .__end_fct = NULL,
48 .__min_needed_from = 1,
49 .__max_needed_from = 1,
50 .__min_needed_to = 4,
51 .__max_needed_to = 4,
52 .__stateful = 0,
53 .__data = NULL
56 static struct __gconv_step to_mb =
58 .__shlib_handle = NULL,
59 .__modname = NULL,
60 .__counter = INT_MAX,
61 .__from_name = (char *) "INTERNAL",
62 .__to_name = (char *) "ANSI_X3.4-1968//TRANSLIT",
63 .__fct = __gconv_transform_internal_ascii,
64 .__init_fct = NULL,
65 .__end_fct = NULL,
66 .__min_needed_from = 4,
67 .__max_needed_from = 4,
68 .__min_needed_to = 1,
69 .__max_needed_to = 1,
70 .__stateful = 0,
71 .__data = NULL
75 /* For the default locale we only have to handle ANSI_X3.4-1968. */
76 struct gconv_fcts __wcsmbs_gconv_fcts =
78 .towc = &__wcsmbs_to_wc,
79 .towc_nsteps = 1,
80 .tomb = &to_mb,
81 .tomb_nsteps = 1
85 struct __gconv_step *
86 attribute_hidden
87 __wcsmbs_getfct (const char *to, const char *from, size_t *nstepsp)
89 size_t nsteps;
90 struct __gconv_step *result;
91 #if 0
92 size_t nstateful;
93 size_t cnt;
94 #endif
96 if (__gconv_find_transform (to, from, &result, &nsteps, 0) != __GCONV_OK)
97 /* Loading the conversion step is not possible. */
98 return NULL;
100 /* Maybe it is someday necessary to allow more than one step.
101 Currently this is not the case since the conversions handled here
102 are from and to INTERNAL and there always is a converted for
103 that. It the directly following code is enabled the libio
104 functions will have to allocate appropriate __gconv_step_data
105 elements instead of only one. */
106 #if 0
107 /* Count the number of stateful conversions. Since we will only
108 have one 'mbstate_t' object available we can only deal with one
109 stateful conversion. */
110 nstateful = 0;
111 for (cnt = 0; cnt < nsteps; ++cnt)
112 if (result[cnt].__stateful)
113 ++nstateful;
114 if (nstateful > 1)
115 #else
116 if (nsteps > 1)
117 #endif
119 /* We cannot handle this case. */
120 __gconv_close_transform (result, nsteps);
121 result = NULL;
123 else
124 *nstepsp = nsteps;
126 return result;
130 /* Extract from the given locale name the character set portion. Since
131 only the XPG form of the name includes this information we don't have
132 to take care for the CEN form. */
133 #define extract_charset_name(str) \
134 ({ \
135 const char *cp = str; \
136 char *result = NULL; \
138 cp += strcspn (cp, "@.+,"); \
139 if (*cp == '.') \
141 const char *endp = ++cp; \
142 while (*endp != '\0' && *endp != '@') \
143 ++endp; \
144 if (endp != cp) \
145 result = strndupa (cp, endp - cp); \
147 result; \
151 /* We must modify global data. */
152 __libc_lock_define_initialized (static, lock)
155 /* Load conversion functions for the currently selected locale. */
156 void
157 internal_function
158 __wcsmbs_load_conv (const struct locale_data *new_category)
160 /* Acquire the lock. */
161 __libc_lock_lock (lock);
163 /* We should repeat the test since while we waited some other thread
164 might have run this function. */
165 if (__builtin_expect (__wcsmbs_last_locale != new_category, 1))
167 if (new_category->name == _nl_C_name) /* Yes, pointer comparison. */
169 failed:
170 __wcsmbs_gconv_fcts.towc = &__wcsmbs_to_wc;
171 __wcsmbs_gconv_fcts.tomb = &to_mb;
173 else
175 /* We must find the real functions. */
176 const char *charset_name;
177 const char *complete_name;
178 struct __gconv_step *new_towc;
179 size_t new_towc_nsteps;
180 struct __gconv_step *new_tomb;
181 size_t new_tomb_nsteps;
182 int use_translit;
184 /* Free the old conversions. */
185 if (__wcsmbs_gconv_fcts.tomb != &to_mb)
186 __gconv_close_transform (__wcsmbs_gconv_fcts.tomb,
187 __wcsmbs_gconv_fcts.tomb_nsteps);
188 if (__wcsmbs_gconv_fcts.towc != &__wcsmbs_to_wc)
189 __gconv_close_transform (__wcsmbs_gconv_fcts.towc,
190 __wcsmbs_gconv_fcts.towc_nsteps);
192 /* Get name of charset of the locale. */
193 charset_name = new_category->values[_NL_ITEM_INDEX(CODESET)].string;
195 /* Does the user want transliteration? */
196 use_translit = new_category->use_translit;
198 /* Normalize the name and add the slashes necessary for a
199 complete lookup. */
200 complete_name = norm_add_slashes (charset_name,
201 use_translit ? "TRANSLIT" : NULL);
203 /* It is not necessary to use transliteration in this direction
204 since the internal character set is supposed to be able to
205 represent all others. */
206 new_towc = __wcsmbs_getfct ("INTERNAL", complete_name,
207 &new_towc_nsteps);
208 new_tomb = (new_towc != NULL
209 ? __wcsmbs_getfct (complete_name, "INTERNAL",
210 &new_tomb_nsteps)
211 : NULL);
213 /* If any of the conversion functions is not available we don't
214 use any since this would mean we cannot convert back and
215 forth.*/
216 if (new_towc == NULL || new_tomb == NULL)
218 if (new_towc != NULL)
219 __gconv_close_transform (new_towc, 1);
221 goto failed;
224 __wcsmbs_gconv_fcts.tomb = new_tomb;
225 __wcsmbs_gconv_fcts.tomb_nsteps = new_tomb_nsteps;
226 __wcsmbs_gconv_fcts.towc = new_towc;
227 __wcsmbs_gconv_fcts.towc_nsteps = new_towc_nsteps;
230 /* Set last-used variable for current locale. */
231 __wcsmbs_last_locale = new_category;
234 __libc_lock_unlock (lock);
238 /* Clone the current conversion function set. */
239 void
240 internal_function
241 __wcsmbs_clone_conv (struct gconv_fcts *copy)
243 /* First make sure the function table is up-to-date. */
244 update_conversion_ptrs ();
246 /* Make sure the data structures remain the same until we are finished. */
247 __libc_lock_lock (lock);
249 /* Copy the data. */
250 *copy = __wcsmbs_gconv_fcts;
252 /* Now increment the usage counters. */
253 if (copy->towc->__shlib_handle != NULL)
254 ++copy->towc->__counter;
255 if (copy->tomb->__shlib_handle != NULL)
256 ++copy->tomb->__counter;
258 __libc_lock_unlock (lock);
262 /* Get converters for named charset. */
264 internal_function
265 __wcsmbs_named_conv (struct gconv_fcts *copy, const char *name)
267 copy->towc = __wcsmbs_getfct ("INTERNAL", name, &copy->towc_nsteps);
268 if (copy->towc != NULL)
270 copy->tomb = __wcsmbs_getfct (name, "INTERNAL", &copy->tomb_nsteps);
271 if (copy->tomb == NULL)
272 __gconv_close_transform (copy->towc, copy->towc_nsteps);
275 return copy->towc == NULL || copy->tomb == NULL ? 1 : 0;
279 /* Free all resources if necessary. */
280 static void __attribute__ ((unused))
281 free_mem (void)
283 if (__wcsmbs_gconv_fcts.tomb != &to_mb)
285 struct __gconv_step *old = __wcsmbs_gconv_fcts.tomb;
286 size_t nold = __wcsmbs_gconv_fcts.tomb_nsteps;
287 __wcsmbs_gconv_fcts.tomb = &to_mb;
288 __wcsmbs_gconv_fcts.tomb_nsteps = 1;
289 __gconv_release_cache (old, nold);
292 if (__wcsmbs_gconv_fcts.towc != &__wcsmbs_to_wc)
294 struct __gconv_step *old = __wcsmbs_gconv_fcts.towc;
295 size_t nold = __wcsmbs_gconv_fcts.towc_nsteps;
296 __wcsmbs_gconv_fcts.towc = &__wcsmbs_to_wc;
297 __wcsmbs_gconv_fcts.towc_nsteps = 1;
298 __gconv_release_cache (old, nold);
302 text_set_element (__libc_subfreeres, free_mem);