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. */
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
51 static const char gconv_conf_filename
[] = "gconv-modules";
53 /* Filename extension for the modules. */
55 # define MODULE_EXT ".so"
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, \
71 #define BUILTIN_ALIAS(From, To)
73 #include "gconv_builtin.h"
76 #undef BUILTIN_TRANSFORMATION
79 static const char *builtin_aliases
[] =
81 #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, Init, End, MinF, \
83 #define BUILTIN_ALIAS(From, To) From " " To,
85 #include "gconv_builtin.h"
89 # include <libio/libioP.h>
90 # define __getdelim(line, len, c, fp) _IO_getdelim (line, len, c, fp)
94 /* Test whether there is already a matching module known. */
97 detect_conflict (const char *alias
)
99 struct gconv_module
*node
= __gconv_modules_db
;
103 int cmpres
= strcmp (alias
, node
->from_string
);
106 /* We have a conflict. */
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
))
130 while (*rp
!= '\0' && !isspace (*rp
))
131 *wp
++ = toupper (*rp
++);
133 /* There is no `to' string on the line. Ignore it. */
137 while (isspace (*rp
))
139 while (*rp
!= '\0' && !isspace (*rp
))
140 *wp
++ = toupper (*rp
++);
142 /* No `to' string, ignore the line. */
146 /* Test whether this alias conflicts with any available module. */
147 if (detect_conflict (from
))
148 /* It does conflict, don't add the alias. */
151 new_alias
= (struct gconv_alias
*)
152 malloc (sizeof (struct gconv_alias
) + (wp
- from
));
153 if (new_alias
!= NULL
)
157 new_alias
->fromname
= memcpy ((char *) new_alias
158 + sizeof (struct gconv_alias
),
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. */
171 /* Insert a data structure for a new module in the search tree. */
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
;
183 cmpres
= strcmp (newp
->from_string
, root
->from_string
);
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)
199 /* This is a no new conversion. But maybe the cost is
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
;
222 rootp
= &root
->right
;
225 /* Plug in the new node here. */
230 /* Add new module. */
233 add_module (char *rp
, const char *directory
, size_t dir_len
, void **modules
,
234 size_t *nmodules
, int modcounter
)
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
;
248 while (isspace (*rp
))
251 while (*rp
!= '\0' && !isspace (*rp
))
260 while (isspace (*rp
))
262 while (*rp
!= '\0' && !isspace (*rp
))
263 *wp
++ = toupper (*rp
++);
269 while (isspace (*rp
));
271 while (*rp
!= '\0' && !isspace (*rp
))
275 /* There is no cost, use one by default. */
281 /* There might be a cost value. */
285 cost_hi
= strtol (rp
, &endp
, 10);
286 if (rp
== endp
|| cost_hi
< 1)
287 /* No useful information. */
291 if (module
[0] == '\0')
292 /* No module name given. */
294 if (module
[0] == '/')
297 /* See whether we must add the ending. */
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. */
312 new_module
= (struct gconv_module
*) calloc (1,
313 sizeof (struct gconv_module
)
315 + dir_len
+ need_ext
);
316 if (new_module
!= NULL
)
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
;
332 tmp
= __mempcpy (tmp
, directory
, dir_len
);
334 tmp
= __mempcpy (tmp
, module
, wp
- module
);
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. */
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");
354 static int modcounter
;
356 /* Don't complain if a file is not present or readable, simply silently
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
);
368 /* An error occurred. */
372 /* Terminate the line (excluding comments or newline) by an NUL byte
373 to simplify the following code. */
374 endp
= strchr (rp
, '#');
378 if (rp
[n
- 1] == '\n')
381 while (isspace (*rp
))
384 /* If this is an empty line go on with the next one. */
389 while (*rp
!= '\0' && !isspace (*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
++);
399 /* Otherwise ignore the line. */
408 /* Determine the directories we are looking for data in. */
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
;
421 /* Determine the complete path first. */
422 const char *user_path
;
424 size_t gconv_path_len
;
432 user_path
= getenv ("GCONV_PATH");
433 if (user_path
== NULL
)
435 /* No user-defined path. Make a modifiable copy of the
437 gconv_path
= strdupa (default_gconv_path
);
438 gconv_path_len
= sizeof (default_gconv_path
);
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
),
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. */
459 cp
= strchr (gconv_path
, ':');
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));
476 char *strspace
= (char *) &result
[nelems
+ 1];
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
;
488 assert (cwd
!= NULL
);
489 strspace
= __mempcpy (strspace
, cwd
, cwdlen
);
492 strspace
= __stpcpy (strspace
, elem
);
493 if (strspace
[-1] != '/')
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
;
503 while ((elem
= __strtok_r (NULL
, ":", &gconv_path
)) != NULL
);
505 result
[n
].name
= NULL
;
509 __gconv_path_elem
= result
?: (struct path_elem
*) &empty_path_elem
;
515 __libc_lock_unlock (lock
);
519 /* Read all configuration files found in the user-specified and the default
522 __gconv_read_conf (void)
524 void *modules
= NULL
;
526 int save_errno
= errno
;
529 /* Find out where we have to look. */
530 if (__gconv_path_elem
== NULL
)
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
;
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]);
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
)
559 /* It'll conflict so don't add it. */
562 insert_module (&builtin_modules
[cnt
], 0);
565 /* Add aliases for builtin conversions. */
566 cnt
= sizeof (builtin_aliases
) / sizeof (builtin_aliases
[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
))
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
);