Update.
[glibc.git] / iconv / gconv_conf.c
blob8c2f4b014e9577d3445f9216fd113383376c9a71
1 /* Handle configuration data.
2 Copyright (C) 1997,98,99,2000,2001,2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <assert.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <limits.h>
25 #include <locale.h>
26 #include <search.h>
27 #include <stddef.h>
28 #include <stdio.h>
29 #include <stdio_ext.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <sys/param.h>
35 #include <bits/libc-lock.h>
36 #include <gconv_int.h>
39 /* This is the default path where we look for module lists. */
40 static const char default_gconv_path[] = GCONV_PATH;
42 /* The path elements, as determined by the __gconv_get_path function.
43 All path elements end in a slash. */
44 struct path_elem *__gconv_path_elem;
45 /* Maximum length of a single path element in __gconv_path_elem. */
46 size_t __gconv_max_path_elem_len;
48 /* We use the following struct if we couldn't allocate memory. */
49 static const struct path_elem empty_path_elem;
51 /* Name of the file containing the module information in the directories
52 along the path. */
53 static const char gconv_conf_filename[] = "gconv-modules";
55 /* Filename extension for the modules. */
56 #ifndef MODULE_EXT
57 # define MODULE_EXT ".so"
58 #endif
59 static const char gconv_module_ext[] = MODULE_EXT;
61 /* We have a few builtin transformations. */
62 static struct gconv_module builtin_modules[] =
64 #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, MinF, MaxF, \
65 MinT, MaxT) \
66 { \
67 from_string: From, \
68 to_string: To, \
69 cost_hi: Cost, \
70 cost_lo: INT_MAX, \
71 module_name: Name \
73 #define BUILTIN_ALIAS(From, To)
75 #include "gconv_builtin.h"
78 #undef BUILTIN_TRANSFORMATION
79 #undef BUILTIN_ALIAS
81 static const char *builtin_aliases[] =
83 #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, MinF, MaxF, \
84 MinT, MaxT)
85 #define BUILTIN_ALIAS(From, To) From " " To,
87 #include "gconv_builtin.h"
90 #ifdef USE_IN_LIBIO
91 # include <libio/libioP.h>
92 # define __getdelim(line, len, c, fp) _IO_getdelim (line, len, c, fp)
93 #endif
96 /* Value of the GCONV_PATH environment variable. */
97 const char *__gconv_path_envvar;
100 /* Test whether there is already a matching module known. */
101 static int
102 internal_function
103 detect_conflict (const char *alias)
105 struct gconv_module *node = __gconv_modules_db;
107 while (node != NULL)
109 int cmpres = strcmp (alias, node->from_string);
111 if (cmpres == 0)
112 /* We have a conflict. */
113 return 1;
114 else if (cmpres < 0)
115 node = node->left;
116 else
117 node = node->right;
120 return node != NULL;
124 /* Add new alias. */
125 static inline void
126 add_alias (char *rp, void *modules)
128 /* We now expect two more string. The strings are normalized
129 (converted to UPPER case) and strored in the alias database. */
130 struct gconv_alias *new_alias;
131 char *from, *to, *wp;
133 while (__isspace_l (*rp, &_nl_C_locobj))
134 ++rp;
135 from = wp = rp;
136 while (*rp != '\0' && !__isspace_l (*rp, &_nl_C_locobj))
137 *wp++ = __toupper_l (*rp++, &_nl_C_locobj);
138 if (*rp == '\0')
139 /* There is no `to' string on the line. Ignore it. */
140 return;
141 *wp++ = '\0';
142 to = ++rp;
143 while (__isspace_l (*rp, &_nl_C_locobj))
144 ++rp;
145 while (*rp != '\0' && !__isspace_l (*rp, &_nl_C_locobj))
146 *wp++ = __toupper_l (*rp++, &_nl_C_locobj);
147 if (to == wp)
148 /* No `to' string, ignore the line. */
149 return;
150 *wp++ = '\0';
152 /* Test whether this alias conflicts with any available module. */
153 if (detect_conflict (from))
154 /* It does conflict, don't add the alias. */
155 return;
157 new_alias = (struct gconv_alias *)
158 malloc (sizeof (struct gconv_alias) + (wp - from));
159 if (new_alias != NULL)
161 void **inserted;
163 new_alias->fromname = memcpy ((char *) new_alias
164 + sizeof (struct gconv_alias),
165 from, wp - from);
166 new_alias->toname = new_alias->fromname + (to - from);
168 inserted = (void **) __tsearch (new_alias, &__gconv_alias_db,
169 __gconv_alias_compare);
170 if (inserted == NULL || *inserted != new_alias)
171 /* Something went wrong, free this entry. */
172 free (new_alias);
177 /* Insert a data structure for a new module in the search tree. */
178 static inline void
179 internal_function
180 insert_module (struct gconv_module *newp, int tobefreed)
182 struct gconv_module **rootp = &__gconv_modules_db;
184 while (*rootp != NULL)
186 struct gconv_module *root = *rootp;
187 int cmpres;
189 cmpres = strcmp (newp->from_string, root->from_string);
190 if (cmpres == 0)
192 /* Both strings are identical. Insert the string at the
193 end of the `same' list if it is not already there. */
194 while (strcmp (newp->from_string, root->from_string) != 0
195 || strcmp (newp->to_string, root->to_string) != 0)
197 rootp = &root->same;
198 root = *rootp;
199 if (root == NULL)
200 break;
203 if (root != NULL)
205 /* This is a no new conversion. But maybe the cost is
206 better. */
207 if (newp->cost_hi < root->cost_hi
208 || (newp->cost_hi == root->cost_hi
209 && newp->cost_lo < root->cost_lo))
211 newp->left = root->left;
212 newp->right = root->right;
213 newp->same = root->same;
214 *rootp = newp;
216 free (root);
218 else if (tobefreed)
219 free (newp);
220 return;
223 break;
225 else if (cmpres < 0)
226 rootp = &root->left;
227 else
228 rootp = &root->right;
231 /* Plug in the new node here. */
232 *rootp = newp;
236 /* Add new module. */
237 static void
238 internal_function
239 add_module (char *rp, const char *directory, size_t dir_len, void **modules,
240 size_t *nmodules, int modcounter)
242 /* We expect now
243 1. `from' name
244 2. `to' name
245 3. filename of the module
246 4. an optional cost value
248 struct gconv_alias fake_alias;
249 struct gconv_module *new_module;
250 char *from, *to, *module, *wp;
251 int need_ext;
252 int cost_hi;
254 while (__isspace_l (*rp, &_nl_C_locobj))
255 ++rp;
256 from = rp;
257 while (*rp != '\0' && !__isspace_l (*rp, &_nl_C_locobj))
259 *rp = __toupper_l (*rp, &_nl_C_locobj);
260 ++rp;
262 if (*rp == '\0')
263 return;
264 *rp++ = '\0';
265 to = wp = rp;
266 while (__isspace_l (*rp, &_nl_C_locobj))
267 ++rp;
268 while (*rp != '\0' && !__isspace_l (*rp, &_nl_C_locobj))
269 *wp++ = __toupper_l (*rp++, &_nl_C_locobj);
270 if (*rp == '\0')
271 return;
272 *wp++ = '\0';
274 ++rp;
275 while (__isspace_l (*rp, &_nl_C_locobj));
276 module = wp;
277 while (*rp != '\0' && !__isspace_l (*rp, &_nl_C_locobj))
278 *wp++ = *rp++;
279 if (*rp == '\0')
281 /* There is no cost, use one by default. */
282 *wp++ = '\0';
283 cost_hi = 1;
285 else
287 /* There might be a cost value. */
288 char *endp;
290 *wp++ = '\0';
291 cost_hi = strtol (rp, &endp, 10);
292 if (rp == endp || cost_hi < 1)
293 /* No useful information. */
294 cost_hi = 1;
297 if (module[0] == '\0')
298 /* No module name given. */
299 return;
300 if (module[0] == '/')
301 dir_len = 0;
303 /* See whether we must add the ending. */
304 need_ext = 0;
305 if (wp - module < (ptrdiff_t) sizeof (gconv_module_ext)
306 || memcmp (wp - sizeof (gconv_module_ext), gconv_module_ext,
307 sizeof (gconv_module_ext)) != 0)
308 /* We must add the module extension. */
309 need_ext = sizeof (gconv_module_ext) - 1;
311 /* See whether we have already an alias with this name defined. */
312 fake_alias.fromname = strndupa (from, to - from);
314 if (__tfind (&fake_alias, &__gconv_alias_db, __gconv_alias_compare) != NULL)
315 /* This module duplicates an alias. */
316 return;
318 new_module = (struct gconv_module *) calloc (1,
319 sizeof (struct gconv_module)
320 + (wp - from)
321 + dir_len + need_ext);
322 if (new_module != NULL)
324 char *tmp;
326 new_module->from_string = tmp = (char *) (new_module + 1);
327 tmp = __mempcpy (tmp, from, to - from);
329 new_module->to_string = tmp;
330 tmp = __mempcpy (tmp, to, module - to);
332 new_module->cost_hi = cost_hi;
333 new_module->cost_lo = modcounter;
335 new_module->module_name = tmp;
337 if (dir_len != 0)
338 tmp = __mempcpy (tmp, directory, dir_len);
340 tmp = __mempcpy (tmp, module, wp - module);
342 if (need_ext)
343 memcpy (tmp - 1, gconv_module_ext, sizeof (gconv_module_ext));
345 /* Now insert the new module data structure in our search tree. */
346 insert_module (new_module, 1);
351 /* Read the next configuration file. */
352 static void
353 internal_function
354 read_conf_file (const char *filename, const char *directory, size_t dir_len,
355 void **modules, size_t *nmodules)
357 FILE *fp = fopen (filename, "r");
358 char *line = NULL;
359 size_t line_len = 0;
360 static int modcounter;
362 /* Don't complain if a file is not present or readable, simply silently
363 ignore it. */
364 if (fp == NULL)
365 return;
367 /* No threads reading from this stream. */
368 __fsetlocking (fp, FSETLOCKING_BYCALLER);
370 /* Process the known entries of the file. Comments start with `#' and
371 end with the end of the line. Empty lines are ignored. */
372 while (!feof_unlocked (fp))
374 char *rp, *endp, *word;
375 ssize_t n = __getdelim (&line, &line_len, '\n', fp);
376 if (n < 0)
377 /* An error occurred. */
378 break;
380 rp = line;
381 /* Terminate the line (excluding comments or newline) by an NUL byte
382 to simplify the following code. */
383 endp = strchr (rp, '#');
384 if (endp != NULL)
385 *endp = '\0';
386 else
387 if (rp[n - 1] == '\n')
388 rp[n - 1] = '\0';
390 while (__isspace_l (*rp, &_nl_C_locobj))
391 ++rp;
393 /* If this is an empty line go on with the next one. */
394 if (rp == endp)
395 continue;
397 word = rp;
398 while (*rp != '\0' && !__isspace_l (*rp, &_nl_C_locobj))
399 ++rp;
401 if (rp - word == sizeof ("alias") - 1
402 && memcmp (word, "alias", sizeof ("alias") - 1) == 0)
403 add_alias (rp, *modules);
404 else if (rp - word == sizeof ("module") - 1
405 && memcmp (word, "module", sizeof ("module") - 1) == 0)
406 add_module (rp, directory, dir_len, modules, nmodules, modcounter++);
407 /* else */
408 /* Otherwise ignore the line. */
411 free (line);
413 fclose (fp);
417 /* Determine the directories we are looking for data in. */
418 void
419 internal_function
420 __gconv_get_path (void)
422 struct path_elem *result;
423 __libc_lock_define_initialized (static, lock);
425 __libc_lock_lock (lock);
427 /* Make sure there wasn't a second thread doing it already. */
428 result = (struct path_elem *) __gconv_path_elem;
429 if (result == NULL)
431 /* Determine the complete path first. */
432 char *gconv_path;
433 size_t gconv_path_len;
434 char *elem;
435 char *oldp;
436 char *cp;
437 int nelems;
438 char *cwd;
439 size_t cwdlen;
441 if (__gconv_path_envvar == NULL)
443 /* No user-defined path. Make a modifiable copy of the
444 default path. */
445 gconv_path = strdupa (default_gconv_path);
446 gconv_path_len = sizeof (default_gconv_path);
447 cwd = NULL;
448 cwdlen = 0;
450 else
452 /* Append the default path to the user-defined path. */
453 size_t user_len = strlen (__gconv_path_envvar);
455 gconv_path_len = user_len + 1 + sizeof (default_gconv_path);
456 gconv_path = alloca (gconv_path_len);
457 __mempcpy (__mempcpy (__mempcpy (gconv_path, __gconv_path_envvar,
458 user_len),
459 ":", 1),
460 default_gconv_path, sizeof (default_gconv_path));
461 cwd = __getcwd (NULL, 0);
462 cwdlen = strlen (cwd);
464 assert (default_gconv_path[0] == '/');
466 /* In a first pass we calculate the number of elements. */
467 oldp = NULL;
468 cp = strchr (gconv_path, ':');
469 nelems = 1;
470 while (cp != NULL)
472 if (cp != oldp + 1)
473 ++nelems;
474 oldp = cp;
475 cp = strchr (cp + 1, ':');
478 /* Allocate the memory for the result. */
479 result = (struct path_elem *) malloc ((nelems + 1)
480 * sizeof (struct path_elem)
481 + gconv_path_len + nelems
482 + (nelems - 1) * (cwdlen + 1));
483 if (result != NULL)
485 char *strspace = (char *) &result[nelems + 1];
486 int n = 0;
488 /* Separate the individual parts. */
489 __gconv_max_path_elem_len = 0;
490 elem = __strtok_r (gconv_path, ":", &gconv_path);
491 assert (elem != NULL);
494 result[n].name = strspace;
495 if (elem[0] != '/')
497 assert (cwd != NULL);
498 strspace = __mempcpy (strspace, cwd, cwdlen);
499 *strspace++ = '/';
501 strspace = __stpcpy (strspace, elem);
502 if (strspace[-1] != '/')
503 *strspace++ = '/';
505 result[n].len = strspace - result[n].name;
506 if (result[n].len > __gconv_max_path_elem_len)
507 __gconv_max_path_elem_len = result[n].len;
509 *strspace++ = '\0';
510 ++n;
512 while ((elem = __strtok_r (NULL, ":", &gconv_path)) != NULL);
514 result[n].name = NULL;
515 result[n].len = 0;
518 __gconv_path_elem = result ?: (struct path_elem *) &empty_path_elem;
520 if (cwd != NULL)
521 free (cwd);
524 __libc_lock_unlock (lock);
528 /* Read all configuration files found in the user-specified and the default
529 path. */
530 void
531 attribute_hidden
532 __gconv_read_conf (void)
534 void *modules = NULL;
535 size_t nmodules = 0;
536 int save_errno = errno;
537 size_t cnt;
539 /* First see whether we should use the cache. */
540 if (__gconv_load_cache () == 0)
542 /* Yes, we are done. */
543 __set_errno (save_errno);
544 return;
547 #ifndef STATIC_GCONV
548 /* Find out where we have to look. */
549 if (__gconv_path_elem == NULL)
550 __gconv_get_path ();
552 for (cnt = 0; __gconv_path_elem[cnt].name != NULL; ++cnt)
554 const char *elem = __gconv_path_elem[cnt].name;
555 size_t elem_len = __gconv_path_elem[cnt].len;
556 char *filename;
558 /* No slash needs to be inserted between elem and gconv_conf_filename;
559 elem already ends in a slash. */
560 filename = alloca (elem_len + sizeof (gconv_conf_filename));
561 __mempcpy (__mempcpy (filename, elem, elem_len),
562 gconv_conf_filename, sizeof (gconv_conf_filename));
564 /* Read the next configuration file. */
565 read_conf_file (filename, elem, elem_len, &modules, &nmodules);
567 #endif
569 /* Add the internal modules. */
570 for (cnt = 0; cnt < sizeof (builtin_modules) / sizeof (builtin_modules[0]);
571 ++cnt)
573 struct gconv_alias fake_alias;
575 fake_alias.fromname = (char *) builtin_modules[cnt].from_string;
577 if (__tfind (&fake_alias, &__gconv_alias_db, __gconv_alias_compare)
578 != NULL)
579 /* It'll conflict so don't add it. */
580 continue;
582 insert_module (&builtin_modules[cnt], 0);
585 /* Add aliases for builtin conversions. */
586 cnt = sizeof (builtin_aliases) / sizeof (builtin_aliases[0]);
587 while (cnt > 0)
589 char *copy = strdupa (builtin_aliases[--cnt]);
590 add_alias (copy, modules);
593 /* Restore the error number. */
594 __set_errno (save_errno);
599 /* Free all resources if necessary. */
600 static void __attribute__ ((unused))
601 free_mem (void)
603 if (__gconv_path_elem != NULL && __gconv_path_elem != &empty_path_elem)
604 free ((void *) __gconv_path_elem);
607 text_set_element (__libc_subfreeres, free_mem);