(ipc_perm): Put back __key.
[glibc.git] / iconv / gconv_conf.c
blobd36a751a8ce24a0c9a22b74a60aa543357b44212
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 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 <stddef.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <sys/param.h>
33 #include <bits/libc-lock.h>
34 #include <gconv_int.h>
37 /* This is the default path where we look for module lists. */
38 static const char default_gconv_path[] = GCONV_PATH;
40 /* The path elements, as determined by the __gconv_get_path function.
41 All path elements end in a slash. */
42 struct path_elem *__gconv_path_elem;
43 /* Maximum length of a single path element in __gconv_path_elem. */
44 size_t __gconv_max_path_elem_len;
46 /* We use the following struct if we couldn't allocate memory. */
47 static const struct path_elem empty_path_elem;
49 /* Name of the file containing the module information in the directories
50 along the path. */
51 static const char gconv_conf_filename[] = "gconv-modules";
53 /* Filename extension for the modules. */
54 #ifndef MODULE_EXT
55 # define MODULE_EXT ".so"
56 #endif
57 static const char gconv_module_ext[] = MODULE_EXT;
59 /* We have a few builtin transformations. */
60 static struct gconv_module builtin_modules[] =
62 #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, Init, End, MinF, \
63 MaxF, MinT, MaxT) \
64 { \
65 from_string: From, \
66 to_string: To, \
67 cost_hi: Cost, \
68 cost_lo: INT_MAX, \
69 module_name: Name \
71 #define BUILTIN_ALIAS(From, To)
73 #include "gconv_builtin.h"
76 #undef BUILTIN_TRANSFORMATION
77 #undef BUILTIN_ALIAS
79 static const char *builtin_aliases[] =
81 #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, Init, End, MinF, \
82 MaxF, MinT, MaxT)
83 #define BUILTIN_ALIAS(From, To) From " " To,
85 #include "gconv_builtin.h"
88 #ifdef USE_IN_LIBIO
89 # include <libio/libioP.h>
90 # define __getdelim(line, len, c, fp) _IO_getdelim (line, len, c, fp)
91 #endif
94 /* Test whether there is already a matching module known. */
95 static int
96 internal_function
97 detect_conflict (const char *alias)
99 struct gconv_module *node = __gconv_modules_db;
101 while (node != NULL)
103 int cmpres = strcmp (alias, node->from_string);
105 if (cmpres == 0)
106 /* We have a conflict. */
107 return 1;
108 else if (cmpres < 0)
109 node = node->left;
110 else
111 node = node->right;
114 return node != NULL;
118 /* Add new alias. */
119 static inline void
120 add_alias (char *rp, void *modules)
122 /* We now expect two more string. The strings are normalized
123 (converted to UPPER case) and strored in the alias database. */
124 struct gconv_alias *new_alias;
125 char *from, *to, *wp;
127 while (isspace (*rp))
128 ++rp;
129 from = wp = rp;
130 while (*rp != '\0' && !isspace (*rp))
131 *wp++ = toupper (*rp++);
132 if (*rp == '\0')
133 /* There is no `to' string on the line. Ignore it. */
134 return;
135 *wp++ = '\0';
136 to = ++rp;
137 while (isspace (*rp))
138 ++rp;
139 while (*rp != '\0' && !isspace (*rp))
140 *wp++ = toupper (*rp++);
141 if (to == wp)
142 /* No `to' string, ignore the line. */
143 return;
144 *wp++ = '\0';
146 /* Test whether this alias conflicts with any available module. */
147 if (detect_conflict (from))
148 /* It does conflict, don't add the alias. */
149 return;
151 new_alias = (struct gconv_alias *)
152 malloc (sizeof (struct gconv_alias) + (wp - from));
153 if (new_alias != NULL)
155 void **inserted;
157 new_alias->fromname = memcpy ((char *) new_alias
158 + sizeof (struct gconv_alias),
159 from, wp - from);
160 new_alias->toname = new_alias->fromname + (to - from);
162 inserted = (void **) __tsearch (new_alias, &__gconv_alias_db,
163 __gconv_alias_compare);
164 if (inserted == NULL || *inserted != new_alias)
165 /* Something went wrong, free this entry. */
166 free (new_alias);
171 /* Insert a data structure for a new module in the search tree. */
172 static inline void
173 internal_function
174 insert_module (struct gconv_module *newp, int tobefreed)
176 struct gconv_module **rootp = &__gconv_modules_db;
178 while (*rootp != NULL)
180 struct gconv_module *root = *rootp;
181 int cmpres;
183 cmpres = strcmp (newp->from_string, root->from_string);
184 if (cmpres == 0)
186 /* Both strings are identical. Insert the string at the
187 end of the `same' list if it is not already there. */
188 while (strcmp (newp->from_string, root->from_string) != 0
189 || strcmp (newp->to_string, root->to_string) != 0)
191 rootp = &root->same;
192 root = *rootp;
193 if (root == NULL)
194 break;
197 if (root != NULL)
199 /* This is a no new conversion. But maybe the cost is
200 better. */
201 if (newp->cost_hi < root->cost_hi
202 || (newp->cost_hi == root->cost_hi
203 && newp->cost_lo < root->cost_lo))
205 newp->left = root->left;
206 newp->right = root->right;
207 newp->same = root->same;
208 *rootp = newp;
210 free (root);
212 else if (tobefreed)
213 free (newp);
214 return;
217 break;
219 else if (cmpres < 0)
220 rootp = &root->left;
221 else
222 rootp = &root->right;
225 /* Plug in the new node here. */
226 *rootp = newp;
230 /* Add new module. */
231 static void
232 internal_function
233 add_module (char *rp, const char *directory, size_t dir_len, void **modules,
234 size_t *nmodules, int modcounter)
236 /* We expect now
237 1. `from' name
238 2. `to' name
239 3. filename of the module
240 4. an optional cost value
242 struct gconv_alias fake_alias;
243 struct gconv_module *new_module;
244 char *from, *to, *module, *wp;
245 int need_ext;
246 int cost_hi;
248 while (isspace (*rp))
249 ++rp;
250 from = rp;
251 while (*rp != '\0' && !isspace (*rp))
253 *rp = toupper (*rp);
254 ++rp;
256 if (*rp == '\0')
257 return;
258 *rp++ = '\0';
259 to = wp = rp;
260 while (isspace (*rp))
261 ++rp;
262 while (*rp != '\0' && !isspace (*rp))
263 *wp++ = toupper (*rp++);
264 if (*rp == '\0')
265 return;
266 *wp++ = '\0';
268 ++rp;
269 while (isspace (*rp));
270 module = wp;
271 while (*rp != '\0' && !isspace (*rp))
272 *wp++ = *rp++;
273 if (*rp == '\0')
275 /* There is no cost, use one by default. */
276 *wp++ = '\0';
277 cost_hi = 1;
279 else
281 /* There might be a cost value. */
282 char *endp;
284 *wp++ = '\0';
285 cost_hi = strtol (rp, &endp, 10);
286 if (rp == endp || cost_hi < 1)
287 /* No useful information. */
288 cost_hi = 1;
291 if (module[0] == '\0')
292 /* No module name given. */
293 return;
294 if (module[0] == '/')
295 dir_len = 0;
297 /* See whether we must add the ending. */
298 need_ext = 0;
299 if (wp - module < (ptrdiff_t) sizeof (gconv_module_ext)
300 || memcmp (wp - sizeof (gconv_module_ext), gconv_module_ext,
301 sizeof (gconv_module_ext)) != 0)
302 /* We must add the module extension. */
303 need_ext = sizeof (gconv_module_ext) - 1;
305 /* See whether we have already an alias with this name defined. */
306 fake_alias.fromname = strndupa (from, to - from);
308 if (__tfind (&fake_alias, &__gconv_alias_db, __gconv_alias_compare) != NULL)
309 /* This module duplicates an alias. */
310 return;
312 new_module = (struct gconv_module *) calloc (1,
313 sizeof (struct gconv_module)
314 + (wp - from)
315 + dir_len + need_ext);
316 if (new_module != NULL)
318 char *tmp;
320 new_module->from_string = tmp = (char *) (new_module + 1);
321 tmp = __mempcpy (tmp, from, to - from);
323 new_module->to_string = tmp;
324 tmp = __mempcpy (tmp, to, module - to);
326 new_module->cost_hi = cost_hi;
327 new_module->cost_lo = modcounter;
329 new_module->module_name = tmp;
331 if (dir_len != 0)
332 tmp = __mempcpy (tmp, directory, dir_len);
334 tmp = __mempcpy (tmp, module, wp - module);
336 if (need_ext)
337 memcpy (tmp - 1, gconv_module_ext, sizeof (gconv_module_ext));
339 /* Now insert the new module data structure in our search tree. */
340 insert_module (new_module, 1);
345 /* Read the next configuration file. */
346 static void
347 internal_function
348 read_conf_file (const char *filename, const char *directory, size_t dir_len,
349 void **modules, size_t *nmodules)
351 FILE *fp = fopen (filename, "r");
352 char *line = NULL;
353 size_t line_len = 0;
354 static int modcounter;
356 /* Don't complain if a file is not present or readable, simply silently
357 ignore it. */
358 if (fp == NULL)
359 return;
361 /* Process the known entries of the file. Comments start with `#' and
362 end with the end of the line. Empty lines are ignored. */
363 while (!feof_unlocked (fp))
365 char *rp, *endp, *word;
366 ssize_t n = __getdelim (&line, &line_len, '\n', fp);
367 if (n < 0)
368 /* An error occurred. */
369 break;
371 rp = line;
372 /* Terminate the line (excluding comments or newline) by an NUL byte
373 to simplify the following code. */
374 endp = strchr (rp, '#');
375 if (endp != NULL)
376 *endp = '\0';
377 else
378 if (rp[n - 1] == '\n')
379 rp[n - 1] = '\0';
381 while (isspace (*rp))
382 ++rp;
384 /* If this is an empty line go on with the next one. */
385 if (rp == endp)
386 continue;
388 word = rp;
389 while (*rp != '\0' && !isspace (*rp))
390 ++rp;
392 if (rp - word == sizeof ("alias") - 1
393 && memcmp (word, "alias", sizeof ("alias") - 1) == 0)
394 add_alias (rp, *modules);
395 else if (rp - word == sizeof ("module") - 1
396 && memcmp (word, "module", sizeof ("module") - 1) == 0)
397 add_module (rp, directory, dir_len, modules, nmodules, modcounter++);
398 /* else */
399 /* Otherwise ignore the line. */
402 free (line);
404 fclose (fp);
408 /* Determine the directories we are looking for data in. */
409 void
410 __gconv_get_path (void)
412 struct path_elem *result;
413 __libc_lock_define_initialized (static, lock);
415 __libc_lock_lock (lock);
417 /* Make sure there wasn't a second thread doing it already. */
418 result = (struct path_elem *) __gconv_path_elem;
419 if (result == NULL)
421 /* Determine the complete path first. */
422 const char *user_path;
423 char *gconv_path;
424 size_t gconv_path_len;
425 char *elem;
426 char *oldp;
427 char *cp;
428 int nelems;
429 char *cwd;
430 size_t cwdlen;
432 user_path = getenv ("GCONV_PATH");
433 if (user_path == NULL)
435 /* No user-defined path. Make a modifiable copy of the
436 default path. */
437 gconv_path = strdupa (default_gconv_path);
438 gconv_path_len = sizeof (default_gconv_path);
439 cwd = NULL;
440 cwdlen = 0;
442 else
444 /* Append the default path to the user-defined path. */
445 size_t user_len = strlen (user_path);
447 gconv_path_len = user_len + 1 + sizeof (default_gconv_path);
448 gconv_path = alloca (gconv_path_len);
449 __mempcpy (__mempcpy (__mempcpy (gconv_path, user_path, user_len),
450 ":", 1),
451 default_gconv_path, sizeof (default_gconv_path));
452 cwd = __getcwd (NULL, 0);
453 cwdlen = strlen (cwd);
455 assert (default_gconv_path[0] == '/');
457 /* In a first pass we calculate the number of elements. */
458 oldp = NULL;
459 cp = strchr (gconv_path, ':');
460 nelems = 1;
461 while (cp != NULL)
463 if (cp != oldp + 1)
464 ++nelems;
465 oldp = cp;
466 cp = strchr (cp + 1, ':');
469 /* Allocate the memory for the result. */
470 result = (struct path_elem *) malloc ((nelems + 1)
471 * sizeof (struct path_elem)
472 + gconv_path_len + nelems
473 + (nelems - 1) * (cwdlen + 1));
474 if (result != NULL)
476 char *strspace = (char *) &result[nelems + 1];
477 int n = 0;
479 /* Separate the individual parts. */
480 __gconv_max_path_elem_len = 0;
481 elem = __strtok_r (gconv_path, ":", &gconv_path);
482 assert (elem != NULL);
485 result[n].name = strspace;
486 if (elem[0] != '/')
488 assert (cwd != NULL);
489 strspace = __mempcpy (strspace, cwd, cwdlen);
490 *strspace++ = '/';
492 strspace = __stpcpy (strspace, elem);
493 if (strspace[-1] != '/')
494 *strspace++ = '/';
496 result[n].len = strspace - result[n].name;
497 if (result[n].len > __gconv_max_path_elem_len)
498 __gconv_max_path_elem_len = result[n].len;
500 *strspace++ = '\0';
501 ++n;
503 while ((elem = __strtok_r (NULL, ":", &gconv_path)) != NULL);
505 result[n].name = NULL;
506 result[n].len = 0;
509 __gconv_path_elem = result ?: (struct path_elem *) &empty_path_elem;
511 if (cwd != NULL)
512 free (cwd);
515 __libc_lock_unlock (lock);
519 /* Read all configuration files found in the user-specified and the default
520 path. */
521 void
522 __gconv_read_conf (void)
524 void *modules = NULL;
525 size_t nmodules = 0;
526 int save_errno = errno;
527 size_t cnt;
529 /* Find out where we have to look. */
530 if (__gconv_path_elem == NULL)
531 __gconv_get_path ();
533 for (cnt = 0; __gconv_path_elem[cnt].name != NULL; ++cnt)
535 const char *elem = __gconv_path_elem[cnt].name;
536 size_t elem_len = __gconv_path_elem[cnt].len;
537 char *filename;
539 /* No slash needs to be inserted between elem and gconv_conf_filename;
540 elem already ends in a slash. */
541 filename = alloca (elem_len + sizeof (gconv_conf_filename));
542 __mempcpy (__mempcpy (filename, elem, elem_len),
543 gconv_conf_filename, sizeof (gconv_conf_filename));
545 /* Read the next configuration file. */
546 read_conf_file (filename, elem, elem_len, &modules, &nmodules);
549 /* Add the internal modules. */
550 for (cnt = 0; cnt < sizeof (builtin_modules) / sizeof (builtin_modules[0]);
551 ++cnt)
553 struct gconv_alias fake_alias;
555 fake_alias.fromname = (char *) builtin_modules[cnt].from_string;
557 if (__tfind (&fake_alias, &__gconv_alias_db, __gconv_alias_compare)
558 != NULL)
559 /* It'll conflict so don't add it. */
560 continue;
562 insert_module (&builtin_modules[cnt], 0);
565 /* Add aliases for builtin conversions. */
566 cnt = sizeof (builtin_aliases) / sizeof (builtin_aliases[0]);
567 while (cnt > 0)
569 char *copy = strdupa (builtin_aliases[--cnt]);
570 add_alias (copy, modules);
573 /* Restore the error number. */
574 __set_errno (save_errno);
579 /* Free all resources if necessary. */
580 static void __attribute__ ((unused))
581 free_mem (void)
583 if (__gconv_path_elem != NULL && __gconv_path_elem != &empty_path_elem)
584 free ((void *) __gconv_path_elem);
587 text_set_element (__libc_subfreeres, free_mem);