1 /* Handle configuration data.
2 Copyright (C) 1997,98,99,2000,2001 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
29 #include <stdio_ext.h>
33 #include <sys/param.h>
36 #include <gconv_int.h>
38 /* This is the default path where we look for module lists. */
39 static const char default_gconv_path[] = GCONV_PATH;
41 /* The path elements, as determined by the __gconv_get_path function.
42 All path elements end in a slash. */
43 struct path_elem *__gconv_path_elem;
44 /* Maximum length of a single path element in __gconv_path_elem. */
45 size_t __gconv_max_path_elem_len;
47 /* We use the following struct if we couldn't allocate memory. */
48 static const struct path_elem empty_path_elem;
50 /* Name of the file containing the module information in the directories
52 static const char gconv_conf_filename[] = "gconv-modules";
54 /* Filename extension for the modules. */
56 # define MODULE_EXT ".so"
58 static const char gconv_module_ext[] = MODULE_EXT;
60 /* We have a few builtin transformations. */
61 static struct gconv_module builtin_modules[] =
63 #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, MinF, MaxF, \
72 #define BUILTIN_ALIAS(From, To)
74 #include "gconv_builtin.h"
77 #undef BUILTIN_TRANSFORMATION
80 static const char *builtin_aliases[] =
82 #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, MinF, MaxF, \
84 #define BUILTIN_ALIAS(From, To) From " " To,
86 #include "gconv_builtin.h"
90 # include <libio/libioP.h>
91 # define __getdelim(line, len, c, fp) _IO_getdelim (line, len, c, fp)
95 /* Value of the GCONV_PATH environment variable. */
96 const char *__gconv_path_envvar;
99 /* Test whether there is already a matching module known. */
102 detect_conflict (const char *alias)
104 struct gconv_module *node = __gconv_modules_db;
108 int cmpres = strcmp (alias, node->from_string);
111 /* We have a conflict. */
125 add_alias (char *rp, void *modules)
127 /* We now expect two more string. The strings are normalized
128 (converted to UPPER case) and strored in the alias database. */
129 struct gconv_alias *new_alias;
130 char *from, *to, *wp;
131 char old_locale[20], *old_locale_p;
133 /* Set locale to default C locale. */
134 old_locale_p = setlocale(LC_ALL, "C");
135 strncpy(old_locale, old_locale_p, 20);
137 while (isspace (*rp))
140 while (*rp != '\0' && !isspace (*rp))
141 *wp++ = toupper (*rp++);
144 setlocale(LC_ALL, old_locale);
145 /* There is no `to' string on the line. Ignore it. */
150 while (isspace (*rp))
152 while (*rp != '\0' && !isspace (*rp))
153 *wp++ = toupper (*rp++);
156 setlocale(LC_ALL, old_locale);
157 /* No `to' string, ignore the line. */
162 /* Test whether this alias conflicts with any available module. */
163 if (detect_conflict (from))
165 setlocale(LC_ALL, old_locale);
166 /* It does conflict, don't add the alias. */
170 new_alias = (struct gconv_alias *) malloc (sizeof (struct gconv_alias) + (wp - from));
171 if (new_alias != NULL)
175 new_alias->fromname = memcpy ((char *) new_alias
176 + sizeof (struct gconv_alias),
178 new_alias->toname = new_alias->fromname + (to - from);
180 inserted = (void **) tsearch (new_alias, &__gconv_alias_db,
181 __gconv_alias_compare);
182 if (inserted == NULL || *inserted != new_alias)
183 /* Something went wrong, free this entry. */
186 setlocale(LC_ALL, old_locale);
190 /* Insert a data structure for a new module in the search tree. */
193 insert_module (struct gconv_module *newp, int tobefreed)
195 struct gconv_module **rootp = &__gconv_modules_db;
197 while (*rootp != NULL)
199 struct gconv_module *root = *rootp;
202 cmpres = strcmp (newp->from_string, root->from_string);
205 /* Both strings are identical. Insert the string at the
206 end of the `same' list if it is not already there. */
207 while (strcmp (newp->from_string, root->from_string) != 0
208 || strcmp (newp->to_string, root->to_string) != 0)
218 /* This is a no new conversion. But maybe the cost is
220 if (newp->cost_hi < root->cost_hi
221 || (newp->cost_hi == root->cost_hi
222 && newp->cost_lo < root->cost_lo))
224 newp->left = root->left;
225 newp->right = root->right;
226 newp->same = root->same;
241 rootp = &root->right;
244 /* Plug in the new node here. */
249 /* Add new module. */
252 add_module (char *rp, const char *directory, size_t dir_len, void **modules,
253 size_t *nmodules, int modcounter)
258 3. filename of the module
259 4. an optional cost value
261 struct gconv_alias fake_alias;
262 struct gconv_module *new_module;
263 char *from, *to, *module, *wp;
266 char old_locale[20], *old_locale_p;
271 /* Set locale to default C locale. */
272 old_locale_p = setlocale(LC_ALL, "C");
273 strncpy(old_locale, old_locale_p, 20);
275 while (isspace (*rp))
278 while (*rp != '\0' && !isspace (*rp))
285 setlocale(LC_ALL, old_locale);
290 while (isspace (*rp))
292 setlocale(LC_ALL, old_locale);
295 while (*rp != '\0' && !isspace (*rp))
296 *wp++ = toupper (*rp++);
299 setlocale(LC_ALL, old_locale);
305 while (isspace (*rp));
307 while (*rp != '\0' && !isspace (*rp))
311 /* There is no cost, use one by default. */
317 /* There might be a cost value. */
321 cost_hi = strtol (rp, &endp, 10);
322 if (rp == endp || cost_hi < 1)
323 /* No useful information. */
327 if (module[0] == '\0')
329 setlocale(LC_ALL, old_locale);
330 /* No module name given. */
333 if (module[0] == '/')
336 /* See whether we must add the ending. */
338 if (wp - module < (ptrdiff_t) sizeof (gconv_module_ext)
339 || memcmp (wp - sizeof (gconv_module_ext), gconv_module_ext,
340 sizeof (gconv_module_ext)) != 0)
341 /* We must add the module extension. */
342 need_ext = sizeof (gconv_module_ext) - 1;
344 /* See whether we have already an alias with this name defined. */
346 len = strnlen (old, to - from);
347 new = (char *) alloca (len + 1);
349 fake_alias.fromname = (char *) memcpy (new, old, len);
351 if (tfind (&fake_alias, &__gconv_alias_db, __gconv_alias_compare) != NULL)
353 setlocale(LC_ALL, old_locale);
354 /* This module duplicates an alias. */
358 new_module = (struct gconv_module *) calloc (1,
359 sizeof (struct gconv_module)
361 + dir_len + need_ext);
362 if (new_module != NULL)
366 new_module->from_string = tmp = (char *) (new_module + 1);
367 tmp = memcpy (tmp, from, to - from);
370 new_module->to_string = tmp;
371 tmp = memcpy (tmp, to, module - to);
372 tmp += (module - to);
374 new_module->cost_hi = cost_hi;
375 new_module->cost_lo = modcounter;
377 new_module->module_name = tmp;
381 tmp = memcpy (tmp, directory, dir_len);
385 tmp = memcpy (tmp, module, wp - module);
386 tmp += (wp - module);
389 memcpy (tmp - 1, gconv_module_ext, sizeof (gconv_module_ext));
391 /* Now insert the new module data structure in our search tree. */
392 insert_module (new_module, 1);
394 setlocale(LC_ALL, old_locale);
398 /* Read the next configuration file. */
401 read_conf_file (const char *filename, const char *directory, size_t dir_len,
402 void **modules, size_t *nmodules)
404 FILE *fp = fopen (filename, "r");
407 static int modcounter;
408 char old_locale[20], *old_locale_p;
410 /* Don't complain if a file is not present or readable, simply silently
415 /* Set locale to default C locale. */
416 old_locale_p = setlocale(LC_ALL, "C");
417 strncpy(old_locale, old_locale_p, 20);
419 /* Process the known entries of the file. Comments start with `#' and
420 end with the end of the line. Empty lines are ignored. */
423 char *rp, *endp, *word;
424 ssize_t n = __getdelim (&line, &line_len, '\n', fp);
426 /* An error occurred. */
430 /* Terminate the line (excluding comments or newline) by an NUL byte
431 to simplify the following code. */
432 endp = strchr (rp, '#');
436 if (rp[n - 1] == '\n')
439 while (isspace (*rp))
442 /* If this is an empty line go on with the next one. */
447 while (*rp != '\0' && !isspace (*rp))
450 if (rp - word == sizeof ("alias") - 1
451 && memcmp (word, "alias", sizeof ("alias") - 1) == 0)
452 add_alias (rp, *modules);
453 else if (rp - word == sizeof ("module") - 1
454 && memcmp (word, "module", sizeof ("module") - 1) == 0)
455 add_module (rp, directory, dir_len, modules, nmodules, modcounter++);
457 /* Otherwise ignore the line. */
464 setlocale(LC_ALL, old_locale);
468 /* Determine the directories we are looking for data in. */
470 __gconv_get_path (void)
472 struct path_elem *result;
473 __LOCK_INIT(static, path_lock);
476 __lock_acquire(path_lock);
479 /* Make sure there wasn't a second thread doing it already. */
480 result = (struct path_elem *) __gconv_path_elem;
483 /* Determine the complete path first. */
485 size_t gconv_path_len;
493 if (__gconv_path_envvar == NULL)
495 char * old = default_gconv_path;
496 size_t len = strlen (old) + 1;
497 char *new = (char *) alloca (len);
499 /* No user-defined path. Make a modifiable copy of the
501 gconv_path = (char *) memcpy (new, old, len);
502 gconv_path_len = sizeof (default_gconv_path);
508 /* Append the default path to the user-defined path. */
509 size_t user_len = strlen (__gconv_path_envvar);
512 gconv_path_len = user_len + 1 + sizeof (default_gconv_path);
513 gconv_path = alloca (gconv_path_len);
514 tmp = memcpy (gconv_path, __gconv_path_envvar,
517 memcpy (tmp, ":", 1);
520 default_gconv_path, sizeof (default_gconv_path));
522 cwd = getcwd (NULL, 0);
523 cwdlen = strlen (cwd);
525 assert (default_gconv_path[0] == '/');
527 /* In a first pass we calculate the number of elements. */
529 cp = strchr (gconv_path, ':');
536 cp = strchr (cp + 1, ':');
539 /* Allocate the memory for the result. */
540 result = (struct path_elem *) malloc ((nelems + 1)
541 * sizeof (struct path_elem)
542 + gconv_path_len + nelems
543 + (nelems - 1) * (cwdlen + 1));
546 char *strspace = (char *) &result[nelems + 1];
549 /* Separate the individual parts. */
550 __gconv_max_path_elem_len = 0;
551 elem = strtok_r (gconv_path, ":", &gconv_path);
552 assert (elem != NULL);
555 result[n].name = strspace;
558 assert (cwd != NULL);
559 strspace = memcpy (strspace, cwd, cwdlen);
563 strspace = strcpy (strspace, elem);
564 while(*strspace != '\0') strspace++;
566 if (strspace[-1] != '/')
569 result[n].len = strspace - result[n].name;
570 if (result[n].len > __gconv_max_path_elem_len)
571 __gconv_max_path_elem_len = result[n].len;
576 while ((elem = strtok_r (NULL, ":", &gconv_path)) != NULL);
578 result[n].name = NULL;
582 __gconv_path_elem = result ?: (struct path_elem *) &empty_path_elem;
589 __lock_release(path_lock);
594 /* Read all configuration files found in the user-specified and the default
597 __gconv_read_conf (void)
599 void *modules = NULL;
601 int save_errno = errno;
608 /* First see whether we should use the cache. */
609 if (__gconv_load_cache () == 0)
611 /* Yes, we are done. */
612 __set_errno (save_errno);
617 /* Find out where we have to look. */
618 if (__gconv_path_elem == NULL)
621 for (cnt = 0; __gconv_path_elem[cnt].name != NULL; ++cnt)
623 elem = __gconv_path_elem[cnt].name;
624 elem_len = __gconv_path_elem[cnt].len;
626 /* No slash needs to be inserted between elem and gconv_conf_filename;
627 elem already ends in a slash. */
628 filename = alloca (elem_len + sizeof (gconv_conf_filename));
629 tmp = memcpy (filename, elem, elem_len);
631 memcpy (tmp, gconv_conf_filename, sizeof (gconv_conf_filename));
633 /* Read the next configuration file. */
634 read_conf_file (filename, elem, elem_len, &modules, &nmodules);
638 /* Add the internal modules. */
639 for (cnt = 0; cnt < sizeof (builtin_modules) / sizeof (builtin_modules[0]);
642 struct gconv_alias fake_alias;
644 fake_alias.fromname = (char *) builtin_modules[cnt].from_string;
646 if (tfind (&fake_alias, &__gconv_alias_db, __gconv_alias_compare)
648 /* It'll conflict so don't add it. */
651 insert_module (&builtin_modules[cnt], 0);
654 /* Add aliases for builtin conversions. */
655 cnt = sizeof (builtin_aliases) / sizeof (builtin_aliases[0]);
658 char * old = builtin_aliases[--cnt];
659 size_t len = strlen (old) + 1;
660 char *new = (char *) alloca (len);
661 char *copy = (char *) memcpy (new, old, len);
663 add_alias (copy, modules);
666 /* Restore the error number. */
667 __set_errno (save_errno);
672 /* Free all resources if necessary. */
673 static void __attribute__ ((unused))
676 if (__gconv_path_elem != NULL && __gconv_path_elem != &empty_path_elem)
677 free ((void *) __gconv_path_elem);
680 text_set_element (__libc_subfreeres, free_mem);