* iconvdata/Makefile (iconv-rules): Use LC_ALL=C when running awk
[glibc.git] / iconv / gconv_open.c
blob07ebb9b9591b00b2396b3e4de84067422d8aa255
1 /* Find matching transformation algorithms and initialize steps.
2 Copyright (C) 1997,1998,1999,2000,2001,2004,2005,2007
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA. */
22 #include <errno.h>
23 #include <locale.h>
24 #include "../locale/localeinfo.h"
25 #include <stdlib.h>
26 #include <string.h>
28 #include <gconv_int.h>
31 int
32 internal_function
33 __gconv_open (const char *toset, const char *fromset, __gconv_t *handle,
34 int flags)
36 struct __gconv_step *steps;
37 size_t nsteps;
38 __gconv_t result = NULL;
39 size_t cnt = 0;
40 int res;
41 int conv_flags = 0;
42 const char *errhand;
43 const char *ignore;
44 struct trans_struct *trans = NULL;
46 /* Find out whether any error handling method is specified. */
47 errhand = strchr (toset, '/');
48 if (errhand != NULL)
49 errhand = strchr (errhand + 1, '/');
50 if (__builtin_expect (errhand != NULL, 1))
52 if (*++errhand == '\0')
53 errhand = NULL;
54 else
56 /* Make copy without the error handling description. */
57 char *newtoset = (char *) alloca (errhand - toset + 1);
58 char *tok;
59 char *ptr = NULL /* Work around a bogus warning */;
61 newtoset[errhand - toset] = '\0';
62 toset = memcpy (newtoset, toset, errhand - toset);
64 /* Find the appropriate transliteration handlers. */
65 tok = strdupa (errhand);
67 tok = __strtok_r (tok, ",", &ptr);
68 while (tok != NULL)
70 if (__strcasecmp_l (tok, "TRANSLIT", _nl_C_locobj_ptr) == 0)
72 /* It's the builtin transliteration handling. We only
73 support it for working on the internal encoding. */
74 static const char *const internal_trans_names[1]
75 = { "INTERNAL" };
76 struct trans_struct *lastp = NULL;
77 struct trans_struct *runp;
79 for (runp = trans; runp != NULL; runp = runp->next)
80 if (runp->trans_fct == __gconv_transliterate)
81 break;
82 else
83 lastp = runp;
85 if (runp == NULL)
87 struct trans_struct *newp;
89 newp = (struct trans_struct *) alloca (sizeof (*newp));
90 memset (newp, '\0', sizeof (*newp));
92 /* We leave the `name' field zero to signal that
93 this is an internal transliteration step. */
94 newp->csnames = (const char **) internal_trans_names;
95 newp->ncsnames = 1;
96 newp->trans_fct = __gconv_transliterate;
98 if (lastp == NULL)
99 trans = newp;
100 else
101 lastp->next = newp;
104 else if (__strcasecmp_l (tok, "IGNORE", _nl_C_locobj_ptr) == 0)
105 /* Set the flag to ignore all errors. */
106 conv_flags |= __GCONV_IGNORE_ERRORS;
107 else
109 /* `tok' is possibly a module name. We'll see later
110 whether we can find it. But first see that we do
111 not already a module of this name. */
112 struct trans_struct *lastp = NULL;
113 struct trans_struct *runp;
115 for (runp = trans; runp != NULL; runp = runp->next)
116 if (runp->name != NULL
117 && __strcasecmp_l (tok, runp->name,
118 _nl_C_locobj_ptr) == 0)
119 break;
120 else
121 lastp = runp;
123 if (runp == NULL)
125 struct trans_struct *newp;
127 newp = (struct trans_struct *) alloca (sizeof (*newp));
128 memset (newp, '\0', sizeof (*newp));
129 newp->name = tok;
131 if (lastp == NULL)
132 trans = newp;
133 else
134 lastp->next = newp;
138 tok = __strtok_r (NULL, ",", &ptr);
143 /* For the source character set we ignore the error handler specification.
144 XXX Is this really always the best? */
145 ignore = strchr (fromset, '/');
146 if (ignore != NULL && (ignore = strchr (ignore + 1, '/')) != NULL
147 && *++ignore != '\0')
149 char *newfromset = (char *) alloca (ignore - fromset + 1);
151 newfromset[ignore - fromset] = '\0';
152 fromset = memcpy (newfromset, fromset, ignore - fromset);
155 /* If the string is empty define this to mean the charset of the
156 currently selected locale. */
157 if (strcmp (toset, "//") == 0)
159 const char *codeset = _NL_CURRENT (LC_CTYPE, CODESET);
160 size_t len = strlen (codeset);
161 char *dest;
162 toset = dest = (char *) alloca (len + 3);
163 memcpy (__mempcpy (dest, codeset, len), "//", 3);
165 if (strcmp (fromset, "//") == 0)
167 const char *codeset = _NL_CURRENT (LC_CTYPE, CODESET);
168 size_t len = strlen (codeset);
169 char *dest;
170 fromset = dest = (char *) alloca (len + 3);
171 memcpy (__mempcpy (dest, codeset, len), "//", 3);
174 res = __gconv_find_transform (toset, fromset, &steps, &nsteps, flags);
175 if (res == __GCONV_OK)
177 /* Find the modules. */
178 struct trans_struct *lastp = NULL;
179 struct trans_struct *runp;
181 for (runp = trans; runp != NULL; runp = runp->next)
183 if (runp->name == NULL
184 || __builtin_expect (__gconv_translit_find (runp), 0) == 0)
185 lastp = runp;
186 else
188 /* This means we haven't found the module. Remove it. */
189 if (lastp == NULL)
190 trans = runp->next;
191 else
192 lastp->next = runp->next;
196 /* Allocate room for handle. */
197 result = (__gconv_t) malloc (sizeof (struct __gconv_info)
198 + (nsteps
199 * sizeof (struct __gconv_step_data)));
200 if (result == NULL)
201 res = __GCONV_NOMEM;
202 else
204 size_t n;
206 /* Remember the list of steps. */
207 result->__steps = steps;
208 result->__nsteps = nsteps;
210 /* Clear the array for the step data. */
211 memset (result->__data, '\0',
212 nsteps * sizeof (struct __gconv_step_data));
214 /* Call all initialization functions for the transformation
215 step implementations. */
216 for (cnt = 0; cnt < nsteps; ++cnt)
218 size_t size;
220 /* Would have to be done if we would not clear the whole
221 array above. */
222 #if 0
223 /* Reset the counter. */
224 result->__data[cnt].__invocation_counter = 0;
226 /* It's a regular use. */
227 result->__data[cnt].__internal_use = 0;
228 #endif
230 /* We use the `mbstate_t' member in DATA. */
231 result->__data[cnt].__statep = &result->__data[cnt].__state;
233 /* Now see whether we can use any of the transliteration
234 modules for this step. */
235 for (runp = trans; runp != NULL; runp = runp->next)
236 for (n = 0; n < runp->ncsnames; ++n)
237 if (__strcasecmp_l (steps[cnt].__from_name,
238 runp->csnames[n], _nl_C_locobj_ptr) == 0)
240 void *data = NULL;
242 /* Match! Now try the initializer. */
243 if (runp->trans_init_fct == NULL
244 || (runp->trans_init_fct (&data,
245 steps[cnt].__to_name)
246 == __GCONV_OK))
248 /* Append at the end of the list. */
249 struct __gconv_trans_data *newp;
250 struct __gconv_trans_data **lastp;
252 newp = (struct __gconv_trans_data *)
253 malloc (sizeof (struct __gconv_trans_data));
254 if (newp == NULL)
256 res = __GCONV_NOMEM;
257 goto bail;
260 newp->__trans_fct = runp->trans_fct;
261 newp->__trans_context_fct = runp->trans_context_fct;
262 newp->__trans_end_fct = runp->trans_end_fct;
263 newp->__data = data;
264 newp->__next = NULL;
266 lastp = &result->__data[cnt].__trans;
267 while (*lastp != NULL)
268 lastp = &(*lastp)->__next;
270 *lastp = newp;
272 break;
275 /* If this is the last step we must not allocate an
276 output buffer. */
277 if (cnt < nsteps - 1)
279 result->__data[cnt].__flags = conv_flags;
281 /* Allocate the buffer. */
282 size = (GCONV_NCHAR_GOAL * steps[cnt].__max_needed_to);
284 result->__data[cnt].__outbuf = malloc (size);
285 if (result->__data[cnt].__outbuf == NULL)
287 res = __GCONV_NOMEM;
288 goto bail;
291 result->__data[cnt].__outbufend =
292 result->__data[cnt].__outbuf + size;
294 else
296 /* Handle the last entry. */
297 result->__data[cnt].__flags = conv_flags | __GCONV_IS_LAST;
299 break;
304 if (res != __GCONV_OK)
306 /* Something went wrong. Free all the resources. */
307 int serrno;
308 bail:
309 serrno = errno;
311 if (result != NULL)
313 while (cnt-- > 0)
315 struct __gconv_trans_data *transp;
317 transp = result->__data[cnt].__trans;
318 while (transp != NULL)
320 struct __gconv_trans_data *curp = transp;
321 transp = transp->__next;
323 if (__builtin_expect (curp->__trans_end_fct != NULL, 0))
324 curp->__trans_end_fct (curp->__data);
326 free (curp);
329 free (result->__data[cnt].__outbuf);
332 free (result);
333 result = NULL;
336 __gconv_close_transform (steps, nsteps);
338 __set_errno (serrno);
342 *handle = result;
343 return res;