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. */
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 root
->cost_hi
= newp
->cost_hi
;
206 root
->cost_lo
= newp
->cost_lo
;
207 root
->module_name
= newp
->module_name
;
220 rootp
= &root
->right
;
223 /* Plug in the new node here. */
228 /* Add new module. */
231 add_module (char *rp
, const char *directory
, size_t dir_len
, void **modules
,
232 size_t *nmodules
, int modcounter
)
237 3. filename of the module
238 4. an optional cost value
240 struct gconv_alias fake_alias
;
241 struct gconv_module
*new_module
;
242 char *from
, *to
, *module
, *wp
;
246 while (isspace (*rp
))
249 while (*rp
!= '\0' && !isspace (*rp
))
258 while (isspace (*rp
))
260 while (*rp
!= '\0' && !isspace (*rp
))
261 *wp
++ = toupper (*rp
++);
267 while (isspace (*rp
));
269 while (*rp
!= '\0' && !isspace (*rp
))
273 /* There is no cost, use one by default. */
279 /* There might be a cost value. */
283 cost_hi
= strtol (rp
, &endp
, 10);
284 if (rp
== endp
|| cost_hi
< 1)
285 /* No useful information. */
289 if (module
[0] == '\0')
290 /* No module name given. */
292 if (module
[0] == '/')
295 /* See whether we must add the ending. */
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;
303 /* See whether we have already an alias with this name defined. */
304 fake_alias
.fromname
= strndupa (from
, to
- from
);
306 if (__tfind (&fake_alias
, &__gconv_alias_db
, __gconv_alias_compare
) != NULL
)
307 /* This module duplicates an alias. */
310 new_module
= (struct gconv_module
*) calloc (1,
311 sizeof (struct gconv_module
)
313 + dir_len
+ need_ext
);
314 if (new_module
!= NULL
)
318 new_module
->from_string
= tmp
= (char *) (new_module
+ 1);
319 tmp
= __mempcpy (tmp
, from
, to
- from
);
321 new_module
->to_string
= tmp
;
322 tmp
= __mempcpy (tmp
, to
, module
- to
);
324 new_module
->cost_hi
= cost_hi
;
325 new_module
->cost_lo
= modcounter
;
327 new_module
->module_name
= tmp
;
330 tmp
= __mempcpy (tmp
, directory
, dir_len
);
332 tmp
= __mempcpy (tmp
, module
, wp
- module
);
335 memcpy (tmp
- 1, gconv_module_ext
, sizeof (gconv_module_ext
));
337 /* Now insert the new module data structure in our search tree. */
338 insert_module (new_module
, 1);
343 /* Read the next configuration file. */
346 read_conf_file (const char *filename
, const char *directory
, size_t dir_len
,
347 void **modules
, size_t *nmodules
)
349 FILE *fp
= fopen (filename
, "r");
354 /* Don't complain if a file is not present or readable, simply silently
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
))
363 char *rp
, *endp
, *word
;
364 ssize_t n
= __getdelim (&line
, &line_len
, '\n', fp
);
366 /* An error occurred. */
370 /* Terminate the line (excluding comments or newline) by an NUL byte
371 to simplify the following code. */
372 endp
= strchr (rp
, '#');
376 if (rp
[n
- 1] == '\n')
379 while (isspace (*rp
))
382 /* If this is an empty line go on with the next one. */
387 while (*rp
!= '\0' && !isspace (*rp
))
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
++);
397 /* Otherwise ignore the line. */
406 /* Determine the directories we are looking for data in. */
408 __gconv_get_path (void)
410 struct path_elem
*result
;
411 __libc_lock_define_initialized (static, lock
);
413 __libc_lock_lock (lock
);
415 /* Make sure there wasn't a second thread doing it already. */
416 result
= (struct path_elem
*) __gconv_path_elem
;
419 /* Determine the complete path first. */
420 const char *user_path
;
422 size_t gconv_path_len
;
430 user_path
= getenv ("GCONV_PATH");
431 if (user_path
== NULL
)
433 /* No user-defined path. Make a modifiable copy of the
435 gconv_path
= strdupa (default_gconv_path
);
436 gconv_path_len
= sizeof (default_gconv_path
);
442 /* Append the default path to the user-defined path. */
443 size_t user_len
= strlen (user_path
);
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
),
449 default_gconv_path
, sizeof (default_gconv_path
));
450 cwd
= __getcwd (NULL
, 0);
451 cwdlen
= strlen (cwd
);
453 assert (default_gconv_path
[0] == '/');
455 /* In a first pass we calculate the number of elements. */
457 cp
= strchr (gconv_path
, ':');
464 cp
= strchr (cp
+ 1, ':');
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));
474 char *strspace
= (char *) &result
[nelems
+ 1];
477 /* Separate the individual parts. */
478 __gconv_max_path_elem_len
= 0;
479 elem
= __strtok_r (gconv_path
, ":", &gconv_path
);
480 assert (elem
!= NULL
);
483 result
[n
].name
= strspace
;
486 assert (cwd
!= NULL
);
487 strspace
= __mempcpy (strspace
, cwd
, cwdlen
);
490 strspace
= __stpcpy (strspace
, elem
);
491 if (strspace
[-1] != '/')
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
;
501 while ((elem
= __strtok_r (NULL
, ":", &gconv_path
)) != NULL
);
503 result
[n
].name
= NULL
;
507 __gconv_path_elem
= result
?: &empty_path_elem
;
513 __libc_lock_unlock (lock
);
517 /* Read all configuration files found in the user-specified and the default
520 __gconv_read_conf (void)
522 void *modules
= NULL
;
524 int save_errno
= errno
;
527 /* Find out where we have to look. */
528 if (__gconv_path_elem
== NULL
)
531 for (cnt
= 0; __gconv_path_elem
[cnt
].name
!= NULL
; ++cnt
)
533 const char *elem
= __gconv_path_elem
[cnt
].name
;
534 size_t elem_len
= __gconv_path_elem
[cnt
].len
;
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
));
543 /* Read the next configuration file. */
544 read_conf_file (filename
, elem
, elem_len
, &modules
, &nmodules
);
547 /* Add the internal modules. */
548 for (cnt
= 0; cnt
< sizeof (builtin_modules
) / sizeof (builtin_modules
[0]);
551 struct gconv_alias fake_alias
;
553 fake_alias
.fromname
= builtin_modules
[cnt
].from_string
;
555 if (__tfind (&fake_alias
, &__gconv_alias_db
, __gconv_alias_compare
)
557 /* It'll conflict so don't add it. */
560 insert_module (&builtin_modules
[cnt
], 0);
563 /* Add aliases for builtin conversions. */
564 cnt
= sizeof (builtin_aliases
) / sizeof (builtin_aliases
[0]);
567 char *copy
= strdupa (builtin_aliases
[--cnt
]);
568 add_alias (copy
, modules
);
571 /* Restore the error number. */
572 __set_errno (save_errno
);
577 /* Free all resources if necessary. */
578 static void __attribute__ ((unused
))
581 if (__gconv_path_elem
!= NULL
&& __gconv_path_elem
!= &empty_path_elem
)
582 free ((void *) __gconv_path_elem
);
585 text_set_element (__libc_subfreeres
, free_mem
);