(INLINE_SYSCALL): Don't mark asm input operand as clobbered.
[glibc.git] / iconv / gconv_conf.c
blobdb5bb476fd3626936c813ace5830110aea816b24
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
19 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 <stdio_ext.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <sys/param.h>
34 #include <bits/libc-lock.h>
35 #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
51 along the path. */
52 static const char gconv_conf_filename[] = "gconv-modules";
54 /* Filename extension for the modules. */
55 #ifndef MODULE_EXT
56 # define MODULE_EXT ".so"
57 #endif
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, \
64 MinT, MaxT) \
65 { \
66 from_string: From, \
67 to_string: To, \
68 cost_hi: Cost, \
69 cost_lo: INT_MAX, \
70 module_name: Name \
72 #define BUILTIN_ALIAS(From, To)
74 #include "gconv_builtin.h"
77 #undef BUILTIN_TRANSFORMATION
78 #undef BUILTIN_ALIAS
80 static const char *builtin_aliases[] =
82 #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, MinF, MaxF, \
83 MinT, MaxT)
84 #define BUILTIN_ALIAS(From, To) From " " To,
86 #include "gconv_builtin.h"
89 #ifdef USE_IN_LIBIO
90 # include <libio/libioP.h>
91 # define __getdelim(line, len, c, fp) _IO_getdelim (line, len, c, fp)
92 #endif
95 /* Value of the GCONV_PATH environment variable. */
96 const char *__gconv_path_envvar;
99 /* Test whether there is already a matching module known. */
100 static int
101 internal_function
102 detect_conflict (const char *alias)
104 struct gconv_module *node = __gconv_modules_db;
106 while (node != NULL)
108 int cmpres = strcmp (alias, node->from_string);
110 if (cmpres == 0)
111 /* We have a conflict. */
112 return 1;
113 else if (cmpres < 0)
114 node = node->left;
115 else
116 node = node->right;
119 return node != NULL;
123 /* Add new alias. */
124 static inline void
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;
132 while (isspace (*rp))
133 ++rp;
134 from = wp = rp;
135 while (*rp != '\0' && !isspace (*rp))
136 *wp++ = toupper (*rp++);
137 if (*rp == '\0')
138 /* There is no `to' string on the line. Ignore it. */
139 return;
140 *wp++ = '\0';
141 to = ++rp;
142 while (isspace (*rp))
143 ++rp;
144 while (*rp != '\0' && !isspace (*rp))
145 *wp++ = toupper (*rp++);
146 if (to == wp)
147 /* No `to' string, ignore the line. */
148 return;
149 *wp++ = '\0';
151 /* Test whether this alias conflicts with any available module. */
152 if (detect_conflict (from))
153 /* It does conflict, don't add the alias. */
154 return;
156 new_alias = (struct gconv_alias *)
157 malloc (sizeof (struct gconv_alias) + (wp - from));
158 if (new_alias != NULL)
160 void **inserted;
162 new_alias->fromname = memcpy ((char *) new_alias
163 + sizeof (struct gconv_alias),
164 from, wp - from);
165 new_alias->toname = new_alias->fromname + (to - from);
167 inserted = (void **) __tsearch (new_alias, &__gconv_alias_db,
168 __gconv_alias_compare);
169 if (inserted == NULL || *inserted != new_alias)
170 /* Something went wrong, free this entry. */
171 free (new_alias);
176 /* Insert a data structure for a new module in the search tree. */
177 static inline void
178 internal_function
179 insert_module (struct gconv_module *newp, int tobefreed)
181 struct gconv_module **rootp = &__gconv_modules_db;
183 while (*rootp != NULL)
185 struct gconv_module *root = *rootp;
186 int cmpres;
188 cmpres = strcmp (newp->from_string, root->from_string);
189 if (cmpres == 0)
191 /* Both strings are identical. Insert the string at the
192 end of the `same' list if it is not already there. */
193 while (strcmp (newp->from_string, root->from_string) != 0
194 || strcmp (newp->to_string, root->to_string) != 0)
196 rootp = &root->same;
197 root = *rootp;
198 if (root == NULL)
199 break;
202 if (root != NULL)
204 /* This is a no new conversion. But maybe the cost is
205 better. */
206 if (newp->cost_hi < root->cost_hi
207 || (newp->cost_hi == root->cost_hi
208 && newp->cost_lo < root->cost_lo))
210 newp->left = root->left;
211 newp->right = root->right;
212 newp->same = root->same;
213 *rootp = newp;
215 free (root);
217 else if (tobefreed)
218 free (newp);
219 return;
222 break;
224 else if (cmpres < 0)
225 rootp = &root->left;
226 else
227 rootp = &root->right;
230 /* Plug in the new node here. */
231 *rootp = newp;
235 /* Add new module. */
236 static void
237 internal_function
238 add_module (char *rp, const char *directory, size_t dir_len, void **modules,
239 size_t *nmodules, int modcounter)
241 /* We expect now
242 1. `from' name
243 2. `to' name
244 3. filename of the module
245 4. an optional cost value
247 struct gconv_alias fake_alias;
248 struct gconv_module *new_module;
249 char *from, *to, *module, *wp;
250 int need_ext;
251 int cost_hi;
253 while (isspace (*rp))
254 ++rp;
255 from = rp;
256 while (*rp != '\0' && !isspace (*rp))
258 *rp = toupper (*rp);
259 ++rp;
261 if (*rp == '\0')
262 return;
263 *rp++ = '\0';
264 to = wp = rp;
265 while (isspace (*rp))
266 ++rp;
267 while (*rp != '\0' && !isspace (*rp))
268 *wp++ = toupper (*rp++);
269 if (*rp == '\0')
270 return;
271 *wp++ = '\0';
273 ++rp;
274 while (isspace (*rp));
275 module = wp;
276 while (*rp != '\0' && !isspace (*rp))
277 *wp++ = *rp++;
278 if (*rp == '\0')
280 /* There is no cost, use one by default. */
281 *wp++ = '\0';
282 cost_hi = 1;
284 else
286 /* There might be a cost value. */
287 char *endp;
289 *wp++ = '\0';
290 cost_hi = strtol (rp, &endp, 10);
291 if (rp == endp || cost_hi < 1)
292 /* No useful information. */
293 cost_hi = 1;
296 if (module[0] == '\0')
297 /* No module name given. */
298 return;
299 if (module[0] == '/')
300 dir_len = 0;
302 /* See whether we must add the ending. */
303 need_ext = 0;
304 if (wp - module < (ptrdiff_t) sizeof (gconv_module_ext)
305 || memcmp (wp - sizeof (gconv_module_ext), gconv_module_ext,
306 sizeof (gconv_module_ext)) != 0)
307 /* We must add the module extension. */
308 need_ext = sizeof (gconv_module_ext) - 1;
310 /* See whether we have already an alias with this name defined. */
311 fake_alias.fromname = strndupa (from, to - from);
313 if (__tfind (&fake_alias, &__gconv_alias_db, __gconv_alias_compare) != NULL)
314 /* This module duplicates an alias. */
315 return;
317 new_module = (struct gconv_module *) calloc (1,
318 sizeof (struct gconv_module)
319 + (wp - from)
320 + dir_len + need_ext);
321 if (new_module != NULL)
323 char *tmp;
325 new_module->from_string = tmp = (char *) (new_module + 1);
326 tmp = __mempcpy (tmp, from, to - from);
328 new_module->to_string = tmp;
329 tmp = __mempcpy (tmp, to, module - to);
331 new_module->cost_hi = cost_hi;
332 new_module->cost_lo = modcounter;
334 new_module->module_name = tmp;
336 if (dir_len != 0)
337 tmp = __mempcpy (tmp, directory, dir_len);
339 tmp = __mempcpy (tmp, module, wp - module);
341 if (need_ext)
342 memcpy (tmp - 1, gconv_module_ext, sizeof (gconv_module_ext));
344 /* Now insert the new module data structure in our search tree. */
345 insert_module (new_module, 1);
350 /* Read the next configuration file. */
351 static void
352 internal_function
353 read_conf_file (const char *filename, const char *directory, size_t dir_len,
354 void **modules, size_t *nmodules)
356 FILE *fp = fopen (filename, "r");
357 char *line = NULL;
358 size_t line_len = 0;
359 static int modcounter;
361 /* Don't complain if a file is not present or readable, simply silently
362 ignore it. */
363 if (fp == NULL)
364 return;
366 /* No threads reading from this stream. */
367 __fsetlocking (fp, FSETLOCKING_BYCALLER);
369 /* Process the known entries of the file. Comments start with `#' and
370 end with the end of the line. Empty lines are ignored. */
371 while (!feof_unlocked (fp))
373 char *rp, *endp, *word;
374 ssize_t n = __getdelim (&line, &line_len, '\n', fp);
375 if (n < 0)
376 /* An error occurred. */
377 break;
379 rp = line;
380 /* Terminate the line (excluding comments or newline) by an NUL byte
381 to simplify the following code. */
382 endp = strchr (rp, '#');
383 if (endp != NULL)
384 *endp = '\0';
385 else
386 if (rp[n - 1] == '\n')
387 rp[n - 1] = '\0';
389 while (isspace (*rp))
390 ++rp;
392 /* If this is an empty line go on with the next one. */
393 if (rp == endp)
394 continue;
396 word = rp;
397 while (*rp != '\0' && !isspace (*rp))
398 ++rp;
400 if (rp - word == sizeof ("alias") - 1
401 && memcmp (word, "alias", sizeof ("alias") - 1) == 0)
402 add_alias (rp, *modules);
403 else if (rp - word == sizeof ("module") - 1
404 && memcmp (word, "module", sizeof ("module") - 1) == 0)
405 add_module (rp, directory, dir_len, modules, nmodules, modcounter++);
406 /* else */
407 /* Otherwise ignore the line. */
410 free (line);
412 fclose (fp);
416 /* Determine the directories we are looking for data in. */
417 void
418 __gconv_get_path (void)
420 struct path_elem *result;
421 __libc_lock_define_initialized (static, lock);
423 __libc_lock_lock (lock);
425 /* Make sure there wasn't a second thread doing it already. */
426 result = (struct path_elem *) __gconv_path_elem;
427 if (result == NULL)
429 /* Determine the complete path first. */
430 char *gconv_path;
431 size_t gconv_path_len;
432 char *elem;
433 char *oldp;
434 char *cp;
435 int nelems;
436 char *cwd;
437 size_t cwdlen;
439 if (__gconv_path_envvar == NULL)
441 /* No user-defined path. Make a modifiable copy of the
442 default path. */
443 gconv_path = strdupa (default_gconv_path);
444 gconv_path_len = sizeof (default_gconv_path);
445 cwd = NULL;
446 cwdlen = 0;
448 else
450 /* Append the default path to the user-defined path. */
451 size_t user_len = strlen (__gconv_path_envvar);
453 gconv_path_len = user_len + 1 + sizeof (default_gconv_path);
454 gconv_path = alloca (gconv_path_len);
455 __mempcpy (__mempcpy (__mempcpy (gconv_path, __gconv_path_envvar,
456 user_len),
457 ":", 1),
458 default_gconv_path, sizeof (default_gconv_path));
459 cwd = __getcwd (NULL, 0);
460 cwdlen = strlen (cwd);
462 assert (default_gconv_path[0] == '/');
464 /* In a first pass we calculate the number of elements. */
465 oldp = NULL;
466 cp = strchr (gconv_path, ':');
467 nelems = 1;
468 while (cp != NULL)
470 if (cp != oldp + 1)
471 ++nelems;
472 oldp = cp;
473 cp = strchr (cp + 1, ':');
476 /* Allocate the memory for the result. */
477 result = (struct path_elem *) malloc ((nelems + 1)
478 * sizeof (struct path_elem)
479 + gconv_path_len + nelems
480 + (nelems - 1) * (cwdlen + 1));
481 if (result != NULL)
483 char *strspace = (char *) &result[nelems + 1];
484 int n = 0;
486 /* Separate the individual parts. */
487 __gconv_max_path_elem_len = 0;
488 elem = __strtok_r (gconv_path, ":", &gconv_path);
489 assert (elem != NULL);
492 result[n].name = strspace;
493 if (elem[0] != '/')
495 assert (cwd != NULL);
496 strspace = __mempcpy (strspace, cwd, cwdlen);
497 *strspace++ = '/';
499 strspace = __stpcpy (strspace, elem);
500 if (strspace[-1] != '/')
501 *strspace++ = '/';
503 result[n].len = strspace - result[n].name;
504 if (result[n].len > __gconv_max_path_elem_len)
505 __gconv_max_path_elem_len = result[n].len;
507 *strspace++ = '\0';
508 ++n;
510 while ((elem = __strtok_r (NULL, ":", &gconv_path)) != NULL);
512 result[n].name = NULL;
513 result[n].len = 0;
516 __gconv_path_elem = result ?: (struct path_elem *) &empty_path_elem;
518 if (cwd != NULL)
519 free (cwd);
522 __libc_lock_unlock (lock);
526 /* Read all configuration files found in the user-specified and the default
527 path. */
528 void
529 __gconv_read_conf (void)
531 void *modules = NULL;
532 size_t nmodules = 0;
533 int save_errno = errno;
534 size_t cnt;
536 /* First see whether we should use the cache. */
537 if (__gconv_load_cache () == 0)
539 /* Yes, we are done. */
540 __set_errno (save_errno);
541 return;
544 #ifndef STATIC_GCONV
545 /* Find out where we have to look. */
546 if (__gconv_path_elem == NULL)
547 __gconv_get_path ();
549 for (cnt = 0; __gconv_path_elem[cnt].name != NULL; ++cnt)
551 const char *elem = __gconv_path_elem[cnt].name;
552 size_t elem_len = __gconv_path_elem[cnt].len;
553 char *filename;
555 /* No slash needs to be inserted between elem and gconv_conf_filename;
556 elem already ends in a slash. */
557 filename = alloca (elem_len + sizeof (gconv_conf_filename));
558 __mempcpy (__mempcpy (filename, elem, elem_len),
559 gconv_conf_filename, sizeof (gconv_conf_filename));
561 /* Read the next configuration file. */
562 read_conf_file (filename, elem, elem_len, &modules, &nmodules);
564 #endif
566 /* Add the internal modules. */
567 for (cnt = 0; cnt < sizeof (builtin_modules) / sizeof (builtin_modules[0]);
568 ++cnt)
570 struct gconv_alias fake_alias;
572 fake_alias.fromname = (char *) builtin_modules[cnt].from_string;
574 if (__tfind (&fake_alias, &__gconv_alias_db, __gconv_alias_compare)
575 != NULL)
576 /* It'll conflict so don't add it. */
577 continue;
579 insert_module (&builtin_modules[cnt], 0);
582 /* Add aliases for builtin conversions. */
583 cnt = sizeof (builtin_aliases) / sizeof (builtin_aliases[0]);
584 while (cnt > 0)
586 char *copy = strdupa (builtin_aliases[--cnt]);
587 add_alias (copy, modules);
590 /* Restore the error number. */
591 __set_errno (save_errno);
596 /* Free all resources if necessary. */
597 static void __attribute__ ((unused))
598 free_mem (void)
600 if (__gconv_path_elem != NULL && __gconv_path_elem != &empty_path_elem)
601 free ((void *) __gconv_path_elem);
604 text_set_element (__libc_subfreeres, free_mem);