1 /* Handle configuration data.
2 Copyright (C) 1997, 1998, 1999, 2000 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 Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
30 #include <sys/param.h>
32 #include <bits/libc-lock.h>
33 #include <gconv_int.h>
36 /* This is the default path where we look for module lists. */
37 static const char default_gconv_path[] = GCONV_PATH;
39 /* The path elements, as determined by the __gconv_get_path function.
40 All path elements end in a slash. */
41 const struct path_elem *__gconv_path_elem;
42 /* Maximum length of a single path element in __gconv_path_elem. */
43 size_t __gconv_max_path_elem_len;
45 /* We use the following struct if we couldn't allocate memory. */
46 static const struct path_elem empty_path_elem;
48 /* Name of the file containing the module information in the directories
50 static const char gconv_conf_filename[] = "gconv-modules";
52 /* Filename extension for the modules. */
54 # define MODULE_EXT ".so"
56 static const char gconv_module_ext[] = MODULE_EXT;
58 /* We have a few builtin transformations. */
59 static struct gconv_module builtin_modules[] =
61 #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, Init, End, MinF, \
70 #define BUILTIN_ALIAS(From, To)
72 #include "gconv_builtin.h"
75 #undef BUILTIN_TRANSFORMATION
78 static const char *builtin_aliases[] =
80 #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, Init, End, MinF, \
82 #define BUILTIN_ALIAS(From, To) From " " To,
84 #include "gconv_builtin.h"
88 # include <libio/libioP.h>
89 # define __getdelim(line, len, c, fp) _IO_getdelim (line, len, c, fp)
93 /* Test whether there is already a matching module known. */
96 detect_conflict (const char *alias)
98 struct gconv_module *node = __gconv_modules_db;
102 int cmpres = strcmp (alias, node->from_string);
105 /* We have a conflict. */
119 add_alias (char *rp, void *modules)
121 /* We now expect two more string. The strings are normalized
122 (converted to UPPER case) and strored in the alias database. */
123 struct gconv_alias *new_alias;
124 char *from, *to, *wp;
126 while (isspace (*rp))
129 while (*rp != '\0' && !isspace (*rp))
130 *wp++ = toupper (*rp++);
132 /* There is no `to' string on the line. Ignore it. */
136 while (isspace (*rp))
138 while (*rp != '\0' && !isspace (*rp))
139 *wp++ = toupper (*rp++);
141 /* No `to' string, ignore the line. */
145 /* Test whether this alias conflicts with any available module. */
146 if (detect_conflict (from))
147 /* It does conflict, don't add the alias. */
150 new_alias = (struct gconv_alias *)
151 malloc (sizeof (struct gconv_alias) + (wp - from));
152 if (new_alias != NULL)
156 new_alias->fromname = memcpy ((char *) new_alias
157 + sizeof (struct gconv_alias),
159 new_alias->toname = new_alias->fromname + (to - from);
161 inserted = (void **) __tsearch (new_alias, &__gconv_alias_db,
162 __gconv_alias_compare);
163 if (inserted == NULL || *inserted != new_alias)
164 /* Something went wrong, free this entry. */
170 /* Insert a data structure for a new module in the search tree. */
173 insert_module (struct gconv_module *newp, int tobefreed)
175 struct gconv_module **rootp = &__gconv_modules_db;
177 while (*rootp != NULL)
179 struct gconv_module *root = *rootp;
182 cmpres = strcmp (newp->from_string, root->from_string);
185 /* Both strings are identical. Insert the string at the
186 end of the `same' list if it is not already there. */
187 while (strcmp (newp->from_string, root->from_string) != 0
188 || strcmp (newp->to_string, root->to_string) != 0)
198 /* This is a no new conversion. */
209 rootp = &root->right;
212 /* Plug in the new node here. */
217 /* Add new module. */
220 add_module (char *rp, const char *directory, size_t dir_len, void **modules,
221 size_t *nmodules, int modcounter)
226 3. filename of the module
227 4. an optional cost value
229 struct gconv_alias fake_alias;
230 struct gconv_module *new_module;
231 char *from, *to, *module, *wp;
235 while (isspace (*rp))
238 while (*rp != '\0' && !isspace (*rp))
247 while (isspace (*rp))
249 while (*rp != '\0' && !isspace (*rp))
250 *wp++ = toupper (*rp++);
256 while (isspace (*rp));
258 while (*rp != '\0' && !isspace (*rp))
262 /* There is no cost, use one by default. */
268 /* There might be a cost value. */
272 cost_hi = strtol (rp, &endp, 10);
273 if (rp == endp || cost_hi < 1)
274 /* No useful information. */
278 if (module[0] == '\0')
279 /* No module name given. */
281 if (module[0] == '/')
284 /* See whether we must add the ending. */
286 if (wp - module < sizeof (gconv_module_ext)
287 || memcmp (wp - sizeof (gconv_module_ext), gconv_module_ext,
288 sizeof (gconv_module_ext)) != 0)
289 /* We must add the module extension. */
290 need_ext = sizeof (gconv_module_ext) - 1;
292 /* See whether we have already an alias with this name defined. */
293 fake_alias.fromname = strndupa (from, to - from);
295 if (__tfind (&fake_alias, &__gconv_alias_db, __gconv_alias_compare) != NULL)
296 /* This module duplicates an alias. */
299 new_module = (struct gconv_module *) calloc (1,
300 sizeof (struct gconv_module)
302 + dir_len + need_ext);
303 if (new_module != NULL)
307 new_module->from_string = memcpy ((char *) new_module
308 + sizeof (struct gconv_module),
311 new_module->to_string = memcpy ((char *) new_module->from_string
312 + (to - from), to, module - to);
314 new_module->cost_hi = cost_hi;
315 new_module->cost_lo = modcounter;
317 new_module->module_name = (char *) new_module->to_string + (module - to);
320 tmp = (char *) new_module->module_name;
322 tmp = __mempcpy ((char *) new_module->module_name,
325 tmp = __mempcpy (tmp, module, wp - module);
328 memcpy (tmp - 1, gconv_module_ext, sizeof (gconv_module_ext));
330 /* Now insert the new module data structure in our search tree. */
331 insert_module (new_module, 1);
336 /* Read the next configuration file. */
339 read_conf_file (const char *filename, const char *directory, size_t dir_len,
340 void **modules, size_t *nmodules)
342 FILE *fp = fopen (filename, "r");
347 /* Don't complain if a file is not present or readable, simply silently
352 /* Process the known entries of the file. Comments start with `#' and
353 end with the end of the line. Empty lines are ignored. */
354 while (!feof_unlocked (fp))
356 char *rp, *endp, *word;
357 ssize_t n = __getdelim (&line, &line_len, '\n', fp);
359 /* An error occurred. */
363 /* Terminate the line (excluding comments or newline) by an NUL byte
364 to simplify the following code. */
365 endp = strchr (rp, '#');
369 if (rp[n - 1] == '\n')
372 while (isspace (*rp))
375 /* If this is an empty line go on with the next one. */
380 while (*rp != '\0' && !isspace (*rp))
383 if (rp - word == sizeof ("alias") - 1
384 && memcmp (word, "alias", sizeof ("alias") - 1) == 0)
385 add_alias (rp, *modules);
386 else if (rp - word == sizeof ("module") - 1
387 && memcmp (word, "module", sizeof ("module") - 1) == 0)
388 add_module (rp, directory, dir_len, modules, nmodules, modcounter++);
390 /* Otherwise ignore the line. */
399 /* Determine the directories we are looking for data in. */
401 __gconv_get_path (void)
403 struct path_elem *result;
404 __libc_lock_define_initialized (static, lock);
406 __libc_lock_lock (lock);
408 /* Make sure there wasn't a second thread doing it already. */
409 result = (struct path_elem *) __gconv_path_elem;
412 /* Determine the complete path first. */
413 const char *user_path;
415 size_t gconv_path_len;
423 user_path = __secure_getenv ("GCONV_PATH");
424 if (user_path == NULL)
426 /* No user-defined path. Make a modifiable copy of the
428 gconv_path = strdupa (default_gconv_path);
429 gconv_path_len = sizeof (default_gconv_path);
435 /* Append the default path to the user-defined path. */
436 size_t user_len = strlen (user_path);
438 gconv_path_len = user_len + 1 + sizeof (default_gconv_path);
439 gconv_path = alloca (gconv_path_len);
440 __mempcpy (__mempcpy (__mempcpy (gconv_path, user_path, user_len),
442 default_gconv_path, sizeof (default_gconv_path));
443 cwd = __getcwd (NULL, 0);
444 cwdlen = strlen (cwd);
446 assert (default_gconv_path[0] == '/');
448 /* In a first pass we calculate the number of elements. */
450 cp = strchr (gconv_path, ':');
457 cp = strchr (cp + 1, ':');
460 /* Allocate the memory for the result. */
461 result = (struct path_elem *) malloc ((nelems + 1)
462 * sizeof (struct path_elem)
463 + gconv_path_len + nelems
464 + (nelems - 1) * (cwdlen + 1));
467 char *strspace = (char *) &result[nelems + 1];
470 /* Separate the individual parts. */
471 __gconv_max_path_elem_len = 0;
472 elem = __strtok_r (gconv_path, ":", &gconv_path);
473 assert (elem != NULL);
476 result[n].name = strspace;
479 assert (cwd != NULL);
480 strspace = __mempcpy (strspace, cwd, cwdlen);
483 strspace = __stpcpy (strspace, elem);
484 if (strspace[-1] != '/')
487 result[n].len = strspace - result[n].name;
488 if (result[n].len > __gconv_max_path_elem_len)
489 __gconv_max_path_elem_len = result[n].len;
494 while ((elem = __strtok_r (NULL, ":", &gconv_path)) != NULL);
496 result[n].name = NULL;
500 __gconv_path_elem = result ?: &empty_path_elem;
506 __libc_lock_unlock (lock);
510 /* Read all configuration files found in the user-specified and the default
513 __gconv_read_conf (void)
515 void *modules = NULL;
517 int save_errno = errno;
520 /* Find out where we have to look. */
521 if (__gconv_path_elem == NULL)
524 for (cnt = 0; __gconv_path_elem[cnt].name != NULL; ++cnt)
526 const char *elem = __gconv_path_elem[cnt].name;
527 size_t elem_len = __gconv_path_elem[cnt].len;
530 /* No slash needs to be inserted between elem and gconv_conf_filename;
531 elem already ends in a slash. */
532 filename = alloca (elem_len + sizeof (gconv_conf_filename));
533 __mempcpy (__mempcpy (filename, elem, elem_len),
534 gconv_conf_filename, sizeof (gconv_conf_filename));
536 /* Read the next configuration file. */
537 read_conf_file (filename, elem, elem_len, &modules, &nmodules);
540 /* Add the internal modules. */
541 for (cnt = 0; cnt < sizeof (builtin_modules) / sizeof (builtin_modules[0]);
544 struct gconv_alias fake_alias;
546 fake_alias.fromname = builtin_modules[cnt].from_string;
548 if (__tfind (&fake_alias, &__gconv_alias_db, __gconv_alias_compare)
550 /* It'll conflict so don't add it. */
553 insert_module (&builtin_modules[cnt], 0);
556 /* Add aliases for builtin conversions. */
557 cnt = sizeof (builtin_aliases) / sizeof (builtin_aliases[0]);
560 char *copy = strdupa (builtin_aliases[--cnt]);
561 add_alias (copy, modules);
564 /* Restore the error number. */
565 __set_errno (save_errno);
570 /* Free all resources if necessary. */
571 static void __attribute__ ((unused))
574 if (__gconv_path_elem != NULL && __gconv_path_elem != &empty_path_elem)
575 free ((void *) __gconv_path_elem);
578 text_set_element (__libc_subfreeres, free_mem);