]> git.ipfire.org Git - thirdparty/glibc.git/blob - iconv/gconv_conf.c
Update.
[thirdparty/glibc.git] / iconv / gconv_conf.c
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.
5
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.
10
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.
15
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. */
20
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>
32
33 #include <bits/libc-lock.h>
34 #include <gconv_int.h>
35
36
37 /* This is the default path where we look for module lists. */
38 static const char default_gconv_path[] = GCONV_PATH;
39
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;
45
46 /* We use the following struct if we couldn't allocate memory. */
47 static const struct path_elem empty_path_elem;
48
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";
52
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;
58
59 /* We have a few builtin transformations. */
60 static struct gconv_module builtin_modules[] =
61 {
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 \
70 },
71 #define BUILTIN_ALIAS(From, To)
72
73 #include "gconv_builtin.h"
74 };
75
76 #undef BUILTIN_TRANSFORMATION
77 #undef BUILTIN_ALIAS
78
79 static const char *builtin_aliases[] =
80 {
81 #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, Init, End, MinF, \
82 MaxF, MinT, MaxT)
83 #define BUILTIN_ALIAS(From, To) From " " To,
84
85 #include "gconv_builtin.h"
86 };
87
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
92
93
94 /* Test whether there is already a matching module known. */
95 static int
96 internal_function
97 detect_conflict (const char *alias)
98 {
99 struct gconv_module *node = __gconv_modules_db;
100
101 while (node != NULL)
102 {
103 int cmpres = strcmp (alias, node->from_string);
104
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;
112 }
113
114 return node != NULL;
115 }
116
117
118 /* Add new alias. */
119 static inline void
120 add_alias (char *rp, void *modules)
121 {
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;
126
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';
145
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;
150
151 new_alias = (struct gconv_alias *)
152 malloc (sizeof (struct gconv_alias) + (wp - from));
153 if (new_alias != NULL)
154 {
155 void **inserted;
156
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);
161
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);
167 }
168 }
169
170
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)
175 {
176 struct gconv_module **rootp = &__gconv_modules_db;
177
178 while (*rootp != NULL)
179 {
180 struct gconv_module *root = *rootp;
181 int cmpres;
182
183 cmpres = strcmp (newp->from_string, root->from_string);
184 if (cmpres == 0)
185 {
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)
190 {
191 rootp = &root->same;
192 root = *rootp;
193 if (root == NULL)
194 break;
195 }
196
197 if (root != NULL)
198 {
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))
204 {
205 root->cost_hi = newp->cost_hi;
206 root->cost_lo = newp->cost_lo;
207 root->module_name = newp->module_name;
208 }
209
210 if (tobefreed)
211 free (newp);
212 return;
213 }
214
215 break;
216 }
217 else if (cmpres < 0)
218 rootp = &root->left;
219 else
220 rootp = &root->right;
221 }
222
223 /* Plug in the new node here. */
224 *rootp = newp;
225 }
226
227
228 /* Add new module. */
229 static void
230 internal_function
231 add_module (char *rp, const char *directory, size_t dir_len, void **modules,
232 size_t *nmodules, int modcounter)
233 {
234 /* We expect now
235 1. `from' name
236 2. `to' name
237 3. filename of the module
238 4. an optional cost value
239 */
240 struct gconv_alias fake_alias;
241 struct gconv_module *new_module;
242 char *from, *to, *module, *wp;
243 int need_ext;
244 int cost_hi;
245
246 while (isspace (*rp))
247 ++rp;
248 from = rp;
249 while (*rp != '\0' && !isspace (*rp))
250 {
251 *rp = toupper (*rp);
252 ++rp;
253 }
254 if (*rp == '\0')
255 return;
256 *rp++ = '\0';
257 to = wp = rp;
258 while (isspace (*rp))
259 ++rp;
260 while (*rp != '\0' && !isspace (*rp))
261 *wp++ = toupper (*rp++);
262 if (*rp == '\0')
263 return;
264 *wp++ = '\0';
265 do
266 ++rp;
267 while (isspace (*rp));
268 module = wp;
269 while (*rp != '\0' && !isspace (*rp))
270 *wp++ = *rp++;
271 if (*rp == '\0')
272 {
273 /* There is no cost, use one by default. */
274 *wp++ = '\0';
275 cost_hi = 1;
276 }
277 else
278 {
279 /* There might be a cost value. */
280 char *endp;
281
282 *wp++ = '\0';
283 cost_hi = strtol (rp, &endp, 10);
284 if (rp == endp || cost_hi < 1)
285 /* No useful information. */
286 cost_hi = 1;
287 }
288
289 if (module[0] == '\0')
290 /* No module name given. */
291 return;
292 if (module[0] == '/')
293 dir_len = 0;
294
295 /* See whether we must add the ending. */
296 need_ext = 0;
297 if (wp - module < (ptrdiff_t) sizeof (gconv_module_ext)
298 || memcmp (wp - sizeof (gconv_module_ext), gconv_module_ext,
299 sizeof (gconv_module_ext)) != 0)
300 /* We must add the module extension. */
301 need_ext = sizeof (gconv_module_ext) - 1;
302
303 /* See whether we have already an alias with this name defined. */
304 fake_alias.fromname = strndupa (from, to - from);
305
306 if (__tfind (&fake_alias, &__gconv_alias_db, __gconv_alias_compare) != NULL)
307 /* This module duplicates an alias. */
308 return;
309
310 new_module = (struct gconv_module *) calloc (1,
311 sizeof (struct gconv_module)
312 + (wp - from)
313 + dir_len + need_ext);
314 if (new_module != NULL)
315 {
316 char *tmp;
317
318 new_module->from_string = tmp = (char *) (new_module + 1);
319 tmp = __mempcpy (tmp, from, to - from);
320
321 new_module->to_string = tmp;
322 tmp = __mempcpy (tmp, to, module - to);
323
324 new_module->cost_hi = cost_hi;
325 new_module->cost_lo = modcounter;
326
327 new_module->module_name = tmp;
328
329 if (dir_len != 0)
330 tmp = __mempcpy (tmp, directory, dir_len);
331
332 tmp = __mempcpy (tmp, module, wp - module);
333
334 if (need_ext)
335 memcpy (tmp - 1, gconv_module_ext, sizeof (gconv_module_ext));
336
337 /* Now insert the new module data structure in our search tree. */
338 insert_module (new_module, 1);
339 }
340 }
341
342
343 /* Read the next configuration file. */
344 static void
345 internal_function
346 read_conf_file (const char *filename, const char *directory, size_t dir_len,
347 void **modules, size_t *nmodules)
348 {
349 FILE *fp = fopen (filename, "r");
350 char *line = NULL;
351 size_t line_len = 0;
352 int modcounter = 0;
353
354 /* Don't complain if a file is not present or readable, simply silently
355 ignore it. */
356 if (fp == NULL)
357 return;
358
359 /* Process the known entries of the file. Comments start with `#' and
360 end with the end of the line. Empty lines are ignored. */
361 while (!feof_unlocked (fp))
362 {
363 char *rp, *endp, *word;
364 ssize_t n = __getdelim (&line, &line_len, '\n', fp);
365 if (n < 0)
366 /* An error occurred. */
367 break;
368
369 rp = line;
370 /* Terminate the line (excluding comments or newline) by an NUL byte
371 to simplify the following code. */
372 endp = strchr (rp, '#');
373 if (endp != NULL)
374 *endp = '\0';
375 else
376 if (rp[n - 1] == '\n')
377 rp[n - 1] = '\0';
378
379 while (isspace (*rp))
380 ++rp;
381
382 /* If this is an empty line go on with the next one. */
383 if (rp == endp)
384 continue;
385
386 word = rp;
387 while (*rp != '\0' && !isspace (*rp))
388 ++rp;
389
390 if (rp - word == sizeof ("alias") - 1
391 && memcmp (word, "alias", sizeof ("alias") - 1) == 0)
392 add_alias (rp, *modules);
393 else if (rp - word == sizeof ("module") - 1
394 && memcmp (word, "module", sizeof ("module") - 1) == 0)
395 add_module (rp, directory, dir_len, modules, nmodules, modcounter++);
396 /* else */
397 /* Otherwise ignore the line. */
398 }
399
400 free (line);
401
402 fclose (fp);
403 }
404
405
406 /* Determine the directories we are looking for data in. */
407 void
408 __gconv_get_path (void)
409 {
410 struct path_elem *result;
411 __libc_lock_define_initialized (static, lock);
412
413 __libc_lock_lock (lock);
414
415 /* Make sure there wasn't a second thread doing it already. */
416 result = (struct path_elem *) __gconv_path_elem;
417 if (result == NULL)
418 {
419 /* Determine the complete path first. */
420 const char *user_path;
421 char *gconv_path;
422 size_t gconv_path_len;
423 char *elem;
424 char *oldp;
425 char *cp;
426 int nelems;
427 char *cwd;
428 size_t cwdlen;
429
430 user_path = getenv ("GCONV_PATH");
431 if (user_path == NULL)
432 {
433 /* No user-defined path. Make a modifiable copy of the
434 default path. */
435 gconv_path = strdupa (default_gconv_path);
436 gconv_path_len = sizeof (default_gconv_path);
437 cwd = NULL;
438 cwdlen = 0;
439 }
440 else
441 {
442 /* Append the default path to the user-defined path. */
443 size_t user_len = strlen (user_path);
444
445 gconv_path_len = user_len + 1 + sizeof (default_gconv_path);
446 gconv_path = alloca (gconv_path_len);
447 __mempcpy (__mempcpy (__mempcpy (gconv_path, user_path, user_len),
448 ":", 1),
449 default_gconv_path, sizeof (default_gconv_path));
450 cwd = __getcwd (NULL, 0);
451 cwdlen = strlen (cwd);
452 }
453 assert (default_gconv_path[0] == '/');
454
455 /* In a first pass we calculate the number of elements. */
456 oldp = NULL;
457 cp = strchr (gconv_path, ':');
458 nelems = 1;
459 while (cp != NULL)
460 {
461 if (cp != oldp + 1)
462 ++nelems;
463 oldp = cp;
464 cp = strchr (cp + 1, ':');
465 }
466
467 /* Allocate the memory for the result. */
468 result = (struct path_elem *) malloc ((nelems + 1)
469 * sizeof (struct path_elem)
470 + gconv_path_len + nelems
471 + (nelems - 1) * (cwdlen + 1));
472 if (result != NULL)
473 {
474 char *strspace = (char *) &result[nelems + 1];
475 int n = 0;
476
477 /* Separate the individual parts. */
478 __gconv_max_path_elem_len = 0;
479 elem = __strtok_r (gconv_path, ":", &gconv_path);
480 assert (elem != NULL);
481 do
482 {
483 result[n].name = strspace;
484 if (elem[0] != '/')
485 {
486 assert (cwd != NULL);
487 strspace = __mempcpy (strspace, cwd, cwdlen);
488 *strspace++ = '/';
489 }
490 strspace = __stpcpy (strspace, elem);
491 if (strspace[-1] != '/')
492 *strspace++ = '/';
493
494 result[n].len = strspace - result[n].name;
495 if (result[n].len > __gconv_max_path_elem_len)
496 __gconv_max_path_elem_len = result[n].len;
497
498 *strspace++ = '\0';
499 ++n;
500 }
501 while ((elem = __strtok_r (NULL, ":", &gconv_path)) != NULL);
502
503 result[n].name = NULL;
504 result[n].len = 0;
505 }
506
507 __gconv_path_elem = result ?: &empty_path_elem;
508
509 if (cwd != NULL)
510 free (cwd);
511 }
512
513 __libc_lock_unlock (lock);
514 }
515
516
517 /* Read all configuration files found in the user-specified and the default
518 path. */
519 void
520 __gconv_read_conf (void)
521 {
522 void *modules = NULL;
523 size_t nmodules = 0;
524 int save_errno = errno;
525 size_t cnt;
526
527 /* Find out where we have to look. */
528 if (__gconv_path_elem == NULL)
529 __gconv_get_path ();
530
531 for (cnt = 0; __gconv_path_elem[cnt].name != NULL; ++cnt)
532 {
533 const char *elem = __gconv_path_elem[cnt].name;
534 size_t elem_len = __gconv_path_elem[cnt].len;
535 char *filename;
536
537 /* No slash needs to be inserted between elem and gconv_conf_filename;
538 elem already ends in a slash. */
539 filename = alloca (elem_len + sizeof (gconv_conf_filename));
540 __mempcpy (__mempcpy (filename, elem, elem_len),
541 gconv_conf_filename, sizeof (gconv_conf_filename));
542
543 /* Read the next configuration file. */
544 read_conf_file (filename, elem, elem_len, &modules, &nmodules);
545 }
546
547 /* Add the internal modules. */
548 for (cnt = 0; cnt < sizeof (builtin_modules) / sizeof (builtin_modules[0]);
549 ++cnt)
550 {
551 struct gconv_alias fake_alias;
552
553 fake_alias.fromname = builtin_modules[cnt].from_string;
554
555 if (__tfind (&fake_alias, &__gconv_alias_db, __gconv_alias_compare)
556 != NULL)
557 /* It'll conflict so don't add it. */
558 continue;
559
560 insert_module (&builtin_modules[cnt], 0);
561 }
562
563 /* Add aliases for builtin conversions. */
564 cnt = sizeof (builtin_aliases) / sizeof (builtin_aliases[0]);
565 while (cnt > 0)
566 {
567 char *copy = strdupa (builtin_aliases[--cnt]);
568 add_alias (copy, modules);
569 }
570
571 /* Restore the error number. */
572 __set_errno (save_errno);
573 }
574
575
576
577 /* Free all resources if necessary. */
578 static void __attribute__ ((unused))
579 free_mem (void)
580 {
581 if (__gconv_path_elem != NULL && __gconv_path_elem != &empty_path_elem)
582 free ((void *) __gconv_path_elem);
583 }
584
585 text_set_element (__libc_subfreeres, free_mem);