Update.
[glibc.git] / iconv / gconv_conf.c
blob475338f7cab044f146bf3bd8d4b52b6aec7a68a1
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. */
21 #include <assert.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <limits.h>
25 #include <search.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
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 element in use. */
40 const struct path_elem *__gconv_path_elem;
41 /* Maximum length of a single path element. */
42 size_t __gconv_max_path_elem_len;
44 /* We use the following struct if we couldn't allocate memory. */
45 static const struct path_elem empty_path_elem;
47 /* Name of the file containing the module information in the directories
48 along the path. */
49 static const char gconv_conf_filename[] = "gconv-modules";
51 /* Filename extension for the modules. */
52 #ifndef MODULE_EXT
53 # define MODULE_EXT ".so"
54 #endif
55 static const char gconv_module_ext[] = MODULE_EXT;
57 /* We have a few builtin transformations. */
58 static struct gconv_module builtin_modules[] =
60 #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, Init, End, MinF, \
61 MaxF, MinT, MaxT) \
62 { \
63 from_string: From, \
64 to_string: To, \
65 cost_hi: Cost, \
66 cost_lo: INT_MAX, \
67 module_name: Name \
69 #define BUILTIN_ALIAS(From, To)
71 #include "gconv_builtin.h"
74 #undef BUILTIN_TRANSFORMATION
75 #undef BUILTIN_ALIAS
77 static const char *builtin_aliases[] =
79 #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, Init, End, MinF, \
80 MaxF, MinT, MaxT)
81 #define BUILTIN_ALIAS(From, To) From " " To,
83 #include "gconv_builtin.h"
86 #ifdef USE_IN_LIBIO
87 # include <libio/libioP.h>
88 # define __getdelim(line, len, c, fp) _IO_getdelim (line, len, c, fp)
89 #endif
92 /* Test whether there is already a matching module known. */
93 static int
94 internal_function
95 detect_conflict (const char *alias)
97 struct gconv_module *node = __gconv_modules_db;
99 while (node != NULL)
101 int cmpres = strcmp (alias, node->from_string);
103 if (cmpres == 0)
104 /* We have a conflict. */
105 return 1;
106 else if (cmpres < 0)
107 node = node->left;
108 else
109 node = node->right;
112 return node != NULL;
116 /* Add new alias. */
117 static inline void
118 add_alias (char *rp, void *modules)
120 /* We now expect two more string. The strings are normalized
121 (converted to UPPER case) and strored in the alias database. */
122 struct gconv_alias *new_alias;
123 char *from, *to, *wp;
125 while (isspace (*rp))
126 ++rp;
127 from = wp = rp;
128 while (*rp != '\0' && !isspace (*rp))
129 *wp++ = toupper (*rp++);
130 if (*rp == '\0')
131 /* There is no `to' string on the line. Ignore it. */
132 return;
133 *wp++ = '\0';
134 to = ++rp;
135 while (isspace (*rp))
136 ++rp;
137 while (*rp != '\0' && !isspace (*rp))
138 *wp++ = toupper (*rp++);
139 if (to == wp)
140 /* No `to' string, ignore the line. */
141 return;
142 *wp++ = '\0';
144 /* Test whether this alias conflicts with any available module. */
145 if (detect_conflict (from))
146 /* It does conflict, don't add the alias. */
147 return;
149 new_alias = (struct gconv_alias *)
150 malloc (sizeof (struct gconv_alias) + (wp - from));
151 if (new_alias != NULL)
153 void **inserted;
155 new_alias->fromname = memcpy ((char *) new_alias
156 + sizeof (struct gconv_alias),
157 from, wp - from);
158 new_alias->toname = new_alias->fromname + (to - from);
160 inserted = (void **) __tsearch (new_alias, &__gconv_alias_db,
161 __gconv_alias_compare);
162 if (inserted == NULL || *inserted != new_alias)
163 /* Something went wrong, free this entry. */
164 free (new_alias);
169 /* Insert a data structure for a new module in the search tree. */
170 static inline void
171 internal_function
172 insert_module (struct gconv_module *newp)
174 struct gconv_module **rootp = &__gconv_modules_db;
176 while (*rootp != NULL)
178 struct gconv_module *root = *rootp;
179 int cmpres;
181 cmpres = strcmp (newp->from_string, root->from_string);
182 if (cmpres == 0)
184 /* Both strings are identical. Insert the string at the
185 end of the `same' list if it is not already there. */
186 while (strcmp (newp->from_string, root->from_string) != 0
187 || strcmp (newp->to_string, root->to_string) != 0)
189 rootp = &root->same;
190 root = *rootp;
191 if (root == NULL)
192 break;
195 if (root != NULL)
196 /* This is a no new conversion. */
197 return;
199 break;
201 else if (cmpres < 0)
202 rootp = &root->left;
203 else
204 rootp = &root->right;
207 /* Plug in the new node here. */
208 *rootp = newp;
212 /* Add new module. */
213 static void
214 internal_function
215 add_module (char *rp, const char *directory, size_t dir_len, void **modules,
216 size_t *nmodules, int modcounter)
218 /* We expect now
219 1. `from' name
220 2. `to' name
221 3. filename of the module
222 4. an optional cost value
224 struct gconv_alias fake_alias;
225 struct gconv_module *new_module;
226 char *from, *to, *module, *wp;
227 int need_ext;
228 int cost_hi;
230 while (isspace (*rp))
231 ++rp;
232 from = rp;
233 while (*rp != '\0' && !isspace (*rp))
235 *rp = toupper (*rp);
236 ++rp;
238 if (*rp == '\0')
239 return;
240 *rp++ = '\0';
241 to = wp = rp;
242 while (isspace (*rp))
243 ++rp;
244 while (*rp != '\0' && !isspace (*rp))
245 *wp++ = toupper (*rp++);
246 if (*rp == '\0')
247 return;
248 *wp++ = '\0';
250 ++rp;
251 while (isspace (*rp));
252 module = wp;
253 while (*rp != '\0' && !isspace (*rp))
254 *wp++ = *rp++;
255 if (*rp == '\0')
257 /* There is no cost, use one by default. */
258 *wp++ = '\0';
259 cost_hi = 1;
261 else
263 /* There might be a cost value. */
264 char *endp;
266 *wp++ = '\0';
267 cost_hi = strtol (rp, &endp, 10);
268 if (rp == endp || cost_hi < 1)
269 /* No useful information. */
270 cost_hi = 1;
273 if (module[0] == '\0')
274 /* No module name given. */
275 return;
276 if (module[0] == '/')
277 dir_len = 0;
278 else
279 /* Increment by one for the slash. */
280 ++dir_len;
282 /* See whether we must add the ending. */
283 need_ext = 0;
284 if (wp - module < sizeof (gconv_module_ext)
285 || memcmp (wp - sizeof (gconv_module_ext), gconv_module_ext,
286 sizeof (gconv_module_ext)) != 0)
287 /* We must add the module extension. */
288 need_ext = sizeof (gconv_module_ext) - 1;
290 /* See whether we have already an alias with this name defined. */
291 fake_alias.fromname = strndupa (from, to - from);
293 if (__tfind (&fake_alias, &__gconv_alias_db, __gconv_alias_compare) != NULL)
294 /* This module duplicates an alias. */
295 return;
297 new_module = (struct gconv_module *) calloc (1,
298 sizeof (struct gconv_module)
299 + (wp - from)
300 + dir_len + need_ext);
301 if (new_module != NULL)
303 char *tmp;
305 new_module->from_string = memcpy ((char *) new_module
306 + sizeof (struct gconv_module),
307 from, to - from);
309 new_module->to_string = memcpy ((char *) new_module->from_string
310 + (to - from), to, module - to);
312 new_module->cost_hi = cost_hi;
313 new_module->cost_lo = modcounter;
315 new_module->module_name = (char *) new_module->to_string + (module - to);
317 if (dir_len == 0)
318 tmp = (char *) new_module->module_name;
319 else
321 tmp = __mempcpy ((char *) new_module->module_name,
322 directory, dir_len - 1);
323 *tmp++ = '/';
326 tmp = __mempcpy (tmp, module, wp - module);
328 if (need_ext)
329 memcpy (tmp - 1, gconv_module_ext, sizeof (gconv_module_ext));
331 /* Now insert the new module data structure in our search tree. */
332 insert_module (new_module);
337 /* Read the next configuration file. */
338 static void
339 internal_function
340 read_conf_file (const char *filename, const char *directory, size_t dir_len,
341 void **modules, size_t *nmodules)
343 FILE *fp = fopen (filename, "r");
344 char *line = NULL;
345 size_t line_len = 0;
346 int modcounter = 0;
348 /* Don't complain if a file is not present or readable, simply silently
349 ignore it. */
350 if (fp == NULL)
351 return;
353 /* Process the known entries of the file. Comments start with `#' and
354 end with the end of the line. Empty lines are ignored. */
355 while (!feof_unlocked (fp))
357 char *rp, *endp, *word;
358 ssize_t n = __getdelim (&line, &line_len, '\n', fp);
359 if (n < 0)
360 /* An error occurred. */
361 break;
363 rp = line;
364 /* Terminate the line (excluding comments or newline) by an NUL byte
365 to simplify the following code. */
366 endp = strchr (rp, '#');
367 if (endp != NULL)
368 *endp = '\0';
369 else
370 if (rp[n - 1] == '\n')
371 rp[n - 1] = '\0';
373 while (isspace (*rp))
374 ++rp;
376 /* If this is an empty line go on with the next one. */
377 if (rp == endp)
378 continue;
380 word = rp;
381 while (*rp != '\0' && !isspace (*rp))
382 ++rp;
384 if (rp - word == sizeof ("alias") - 1
385 && memcmp (word, "alias", sizeof ("alias") - 1) == 0)
386 add_alias (rp, *modules);
387 else if (rp - word == sizeof ("module") - 1
388 && memcmp (word, "module", sizeof ("module") - 1) == 0)
389 add_module (rp, directory, dir_len, modules, nmodules, modcounter++);
390 /* else */
391 /* Otherwise ignore the line. */
394 free (line);
396 fclose (fp);
400 /* Determine the directories we are looking for data in. */
401 void
402 __gconv_get_path (void)
404 struct path_elem *result;
405 __libc_lock_define_initialized (static, lock);
407 __libc_lock_lock (lock);
409 /* Make sure there wasn't a second thread doing it already. */
410 result = (struct path_elem *) __gconv_path_elem;
411 if (result == NULL)
413 /* Determine the complete path first. */
414 const char *user_path;
415 char *gconv_path;
416 size_t gconv_path_len;
417 char *elem;
418 char *oldp;
419 char *cp;
420 int nelems;
422 user_path = __secure_getenv ("GCONV_PATH");
423 if (user_path == NULL)
425 /* No user-defined path. Make a modifiable copy of the
426 default path. */
427 gconv_path = strdupa (default_gconv_path);
428 gconv_path_len = sizeof (default_gconv_path);
430 else
432 /* Append the default path to the user-defined path. */
433 size_t user_len = strlen (user_path);
435 gconv_path_len = user_len + 1 + sizeof (default_gconv_path);
436 gconv_path = alloca (gconv_path_len);
437 __mempcpy (__mempcpy (__mempcpy (gconv_path, user_path, user_len),
438 ":", 1),
439 default_gconv_path, sizeof (default_gconv_path));
442 /* In a first pass we calculate the number of elements. */
443 oldp = NULL;
444 cp = strchr (gconv_path, ':');
445 nelems = 1;
446 while (cp != NULL)
448 if (cp != oldp + 1)
449 ++nelems;
450 oldp = cp;
451 cp = strchr (cp + 1, ':');
454 /* Allocate the memory for the result. */
455 result = (struct path_elem *) malloc ((nelems + 1)
456 * sizeof (struct path_elem)
457 + gconv_path_len + nelems);
458 if (result != NULL)
460 char *strspace = (char *) &result[nelems + 1];
461 int n = 0;
463 /* Separate the individual parts. */
464 __gconv_max_path_elem_len = 0;
465 elem = __strtok_r (gconv_path, ":", &gconv_path);
466 assert (elem != NULL);
469 result[n].name = strspace;
470 strspace = __stpcpy (strspace, elem);
471 if (strspace[-1] != '/')
472 *strspace++ = '/';
474 result[n].len = strspace - result[n].name;
475 if (result[n].len > __gconv_max_path_elem_len)
476 __gconv_max_path_elem_len = result[n].len;
478 *strspace++ = '\0';
479 ++n;
481 while ((elem = __strtok_r (NULL, ":", &gconv_path)) != NULL);
483 result[n].name = NULL;
484 result[n].len = 0;
487 __gconv_path_elem = result ?: &empty_path_elem;
490 __libc_lock_unlock (lock);
494 /* Read all configuration files found in the user-specified and the default
495 path. */
496 void
497 __gconv_read_conf (void)
499 void *modules = NULL;
500 size_t nmodules = 0;
501 int save_errno = errno;
502 size_t cnt;
504 /* Find out where we have to look. */
505 if (__gconv_path_elem == NULL)
506 __gconv_get_path ();
508 for (cnt = 0; __gconv_path_elem[cnt].name != NULL; ++cnt)
510 char real_elem[__gconv_max_path_elem_len + sizeof (gconv_conf_filename)];
512 if (__realpath (__gconv_path_elem[cnt].name, real_elem) != NULL)
514 size_t elem_len = strlen (real_elem);
515 char *filename;
517 filename = alloca (elem_len + 1 + sizeof (gconv_conf_filename));
518 __mempcpy (__mempcpy (__mempcpy (filename, real_elem, elem_len),
519 "/", 1),
520 gconv_conf_filename, sizeof (gconv_conf_filename));
522 /* Read the next configuration file. */
523 read_conf_file (filename, real_elem, elem_len, &modules, &nmodules);
527 /* Add the internal modules. */
528 for (cnt = 0; cnt < sizeof (builtin_modules) / sizeof (builtin_modules[0]);
529 ++cnt)
531 struct gconv_alias fake_alias;
533 fake_alias.fromname = builtin_modules[cnt].from_string;
535 if (__tfind (&fake_alias, &__gconv_alias_db, __gconv_alias_compare)
536 != NULL)
537 /* It'll conflict so don't add it. */
538 continue;
540 insert_module (&builtin_modules[cnt]);
543 /* Add aliases for builtin conversions. */
544 cnt = sizeof (builtin_aliases) / sizeof (builtin_aliases[0]);
545 while (cnt > 0)
547 char *copy = strdupa (builtin_aliases[--cnt]);
548 add_alias (copy, modules);
551 /* Restore the error number. */
552 __set_errno (save_errno);
557 /* Free all resources if necessary. */
558 static void __attribute__ ((unused))
559 free_mem (void)
561 if (__gconv_path_elem != NULL && __gconv_path_elem != &empty_path_elem)
562 free ((void *) __gconv_path_elem);
565 text_set_element (__libc_subfreeres, free_mem);