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. */
30 #include <sys/param.h>
32 #include <bits/libc-lock.h>
33 #include <gconv_int.h>
36 /* This is the default path where we look for module lists. */
37 static const char default_gconv_path
[] = GCONV_PATH
;
39 /* The path element in use. */
40 const struct path_elem
*__gconv_path_elem
;
41 /* Maximum length of a single path element. */
42 size_t __gconv_max_path_elem_len
;
44 /* We use the following struct if we couldn't allocate memory. */
45 static const struct path_elem empty_path_elem
;
47 /* Name of the file containing the module information in the directories
49 static const char gconv_conf_filename
[] = "gconv-modules";
51 /* Filename extension for the modules. */
53 # define MODULE_EXT ".so"
55 static const char gconv_module_ext
[] = MODULE_EXT
;
57 /* We have a few builtin transformations. */
58 static struct gconv_module builtin_modules
[] =
60 #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, Init, End, MinF, \
69 #define BUILTIN_ALIAS(From, To)
71 #include "gconv_builtin.h"
74 #undef BUILTIN_TRANSFORMATION
77 static const char *builtin_aliases
[] =
79 #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, Init, End, MinF, \
81 #define BUILTIN_ALIAS(From, To) From " " To,
83 #include "gconv_builtin.h"
87 # include <libio/libioP.h>
88 # define __getdelim(line, len, c, fp) _IO_getdelim (line, len, c, fp)
92 /* Test whether there is already a matching module known. */
95 detect_conflict (const char *alias
)
97 struct gconv_module
*node
= __gconv_modules_db
;
101 int cmpres
= strcmp (alias
, node
->from_string
);
104 /* We have a conflict. */
118 add_alias (char *rp
, void *modules
)
120 /* We now expect two more string. The strings are normalized
121 (converted to UPPER case) and strored in the alias database. */
122 struct gconv_alias
*new_alias
;
123 char *from
, *to
, *wp
;
125 while (isspace (*rp
))
128 while (*rp
!= '\0' && !isspace (*rp
))
129 *wp
++ = toupper (*rp
++);
131 /* There is no `to' string on the line. Ignore it. */
135 while (isspace (*rp
))
137 while (*rp
!= '\0' && !isspace (*rp
))
138 *wp
++ = toupper (*rp
++);
140 /* No `to' string, ignore the line. */
144 /* Test whether this alias conflicts with any available module. */
145 if (detect_conflict (from
))
146 /* It does conflict, don't add the alias. */
149 new_alias
= (struct gconv_alias
*)
150 malloc (sizeof (struct gconv_alias
) + (wp
- from
));
151 if (new_alias
!= NULL
)
155 new_alias
->fromname
= memcpy ((char *) new_alias
156 + sizeof (struct gconv_alias
),
158 new_alias
->toname
= new_alias
->fromname
+ (to
- from
);
160 inserted
= (void **) __tsearch (new_alias
, &__gconv_alias_db
,
161 __gconv_alias_compare
);
162 if (inserted
== NULL
|| *inserted
!= new_alias
)
163 /* Something went wrong, free this entry. */
169 /* Insert a data structure for a new module in the search tree. */
172 insert_module (struct gconv_module
*newp
)
174 struct gconv_module
**rootp
= &__gconv_modules_db
;
176 while (*rootp
!= NULL
)
178 struct gconv_module
*root
= *rootp
;
181 cmpres
= strcmp (newp
->from_string
, root
->from_string
);
184 /* Both strings are identical. Insert the string at the
185 end of the `same' list if it is not already there. */
186 while (strcmp (newp
->from_string
, root
->from_string
) != 0
187 || strcmp (newp
->to_string
, root
->to_string
) != 0)
196 /* This is a no new conversion. */
204 rootp
= &root
->right
;
207 /* Plug in the new node here. */
212 /* Add new module. */
215 add_module (char *rp
, const char *directory
, size_t dir_len
, void **modules
,
216 size_t *nmodules
, int modcounter
)
221 3. filename of the module
222 4. an optional cost value
224 struct gconv_alias fake_alias
;
225 struct gconv_module
*new_module
;
226 char *from
, *to
, *module
, *wp
;
230 while (isspace (*rp
))
233 while (*rp
!= '\0' && !isspace (*rp
))
242 while (isspace (*rp
))
244 while (*rp
!= '\0' && !isspace (*rp
))
245 *wp
++ = toupper (*rp
++);
251 while (isspace (*rp
));
253 while (*rp
!= '\0' && !isspace (*rp
))
257 /* There is no cost, use one by default. */
263 /* There might be a cost value. */
267 cost_hi
= strtol (rp
, &endp
, 10);
268 if (rp
== endp
|| cost_hi
< 1)
269 /* No useful information. */
273 if (module
[0] == '\0')
274 /* No module name given. */
276 if (module
[0] == '/')
279 /* Increment by one for the slash. */
282 /* See whether we must add the ending. */
284 if (wp
- module
< sizeof (gconv_module_ext
)
285 || memcmp (wp
- sizeof (gconv_module_ext
), gconv_module_ext
,
286 sizeof (gconv_module_ext
)) != 0)
287 /* We must add the module extension. */
288 need_ext
= sizeof (gconv_module_ext
) - 1;
290 /* See whether we have already an alias with this name defined. */
291 fake_alias
.fromname
= strndupa (from
, to
- from
);
293 if (__tfind (&fake_alias
, &__gconv_alias_db
, __gconv_alias_compare
) != NULL
)
294 /* This module duplicates an alias. */
297 new_module
= (struct gconv_module
*) calloc (1,
298 sizeof (struct gconv_module
)
300 + dir_len
+ need_ext
);
301 if (new_module
!= NULL
)
305 new_module
->from_string
= memcpy ((char *) new_module
306 + sizeof (struct gconv_module
),
309 new_module
->to_string
= memcpy ((char *) new_module
->from_string
310 + (to
- from
), to
, module
- to
);
312 new_module
->cost_hi
= cost_hi
;
313 new_module
->cost_lo
= modcounter
;
315 new_module
->module_name
= (char *) new_module
->to_string
+ (module
- to
);
318 tmp
= (char *) new_module
->module_name
;
321 tmp
= __mempcpy ((char *) new_module
->module_name
,
322 directory
, dir_len
- 1);
326 tmp
= __mempcpy (tmp
, module
, wp
- module
);
329 memcpy (tmp
- 1, gconv_module_ext
, sizeof (gconv_module_ext
));
331 /* Now insert the new module data structure in our search tree. */
332 insert_module (new_module
);
337 /* Read the next configuration file. */
340 read_conf_file (const char *filename
, const char *directory
, size_t dir_len
,
341 void **modules
, size_t *nmodules
)
343 FILE *fp
= fopen (filename
, "r");
348 /* Don't complain if a file is not present or readable, simply silently
353 /* Process the known entries of the file. Comments start with `#' and
354 end with the end of the line. Empty lines are ignored. */
355 while (!feof_unlocked (fp
))
357 char *rp
, *endp
, *word
;
358 ssize_t n
= __getdelim (&line
, &line_len
, '\n', fp
);
360 /* An error occurred. */
364 /* Terminate the line (excluding comments or newline) by an NUL byte
365 to simplify the following code. */
366 endp
= strchr (rp
, '#');
370 if (rp
[n
- 1] == '\n')
373 while (isspace (*rp
))
376 /* If this is an empty line go on with the next one. */
381 while (*rp
!= '\0' && !isspace (*rp
))
384 if (rp
- word
== sizeof ("alias") - 1
385 && memcmp (word
, "alias", sizeof ("alias") - 1) == 0)
386 add_alias (rp
, *modules
);
387 else if (rp
- word
== sizeof ("module") - 1
388 && memcmp (word
, "module", sizeof ("module") - 1) == 0)
389 add_module (rp
, directory
, dir_len
, modules
, nmodules
, modcounter
++);
391 /* Otherwise ignore the line. */
400 /* Determine the directories we are looking for data in. */
402 __gconv_get_path (void)
404 struct path_elem
*result
;
405 __libc_lock_define_initialized (static, lock
);
407 __libc_lock_lock (lock
);
409 /* Make sure there wasn't a second thread doing it already. */
410 result
= (struct path_elem
*) __gconv_path_elem
;
413 /* Determine the complete path first. */
414 const char *user_path
;
416 size_t gconv_path_len
;
422 user_path
= __secure_getenv ("GCONV_PATH");
423 if (user_path
== NULL
)
425 /* No user-defined path. Make a modifiable copy of the
427 gconv_path
= strdupa (default_gconv_path
);
428 gconv_path_len
= sizeof (default_gconv_path
);
432 /* Append the default path to the user-defined path. */
433 size_t user_len
= strlen (user_path
);
435 gconv_path_len
= user_len
+ 1 + sizeof (default_gconv_path
);
436 gconv_path
= alloca (gconv_path_len
);
437 __mempcpy (__mempcpy (__mempcpy (gconv_path
, user_path
, user_len
),
439 default_gconv_path
, sizeof (default_gconv_path
));
442 /* In a first pass we calculate the number of elements. */
444 cp
= strchr (gconv_path
, ':');
451 cp
= strchr (cp
+ 1, ':');
454 /* Allocate the memory for the result. */
455 result
= (struct path_elem
*) malloc ((nelems
+ 1)
456 * sizeof (struct path_elem
)
457 + gconv_path_len
+ nelems
);
460 char *strspace
= (char *) &result
[nelems
+ 1];
463 /* Separate the individual parts. */
464 __gconv_max_path_elem_len
= 0;
465 elem
= __strtok_r (gconv_path
, ":", &gconv_path
);
466 assert (elem
!= NULL
);
469 result
[n
].name
= strspace
;
470 strspace
= __stpcpy (strspace
, elem
);
471 if (strspace
[-1] != '/')
474 result
[n
].len
= strspace
- result
[n
].name
;
475 if (result
[n
].len
> __gconv_max_path_elem_len
)
476 __gconv_max_path_elem_len
= result
[n
].len
;
481 while ((elem
= __strtok_r (NULL
, ":", &gconv_path
)) != NULL
);
483 result
[n
].name
= NULL
;
487 __gconv_path_elem
= result
?: &empty_path_elem
;
490 __libc_lock_unlock (lock
);
494 /* Read all configuration files found in the user-specified and the default
497 __gconv_read_conf (void)
499 void *modules
= NULL
;
501 int save_errno
= errno
;
504 /* Find out where we have to look. */
505 if (__gconv_path_elem
== NULL
)
508 for (cnt
= 0; __gconv_path_elem
[cnt
].name
!= NULL
; ++cnt
)
510 char real_elem
[__gconv_max_path_elem_len
+ sizeof (gconv_conf_filename
)];
512 if (__realpath (__gconv_path_elem
[cnt
].name
, real_elem
) != NULL
)
514 size_t elem_len
= strlen (real_elem
);
517 filename
= alloca (elem_len
+ 1 + sizeof (gconv_conf_filename
));
518 __mempcpy (__mempcpy (__mempcpy (filename
, real_elem
, elem_len
),
520 gconv_conf_filename
, sizeof (gconv_conf_filename
));
522 /* Read the next configuration file. */
523 read_conf_file (filename
, real_elem
, elem_len
, &modules
, &nmodules
);
527 /* Add the internal modules. */
528 for (cnt
= 0; cnt
< sizeof (builtin_modules
) / sizeof (builtin_modules
[0]);
531 struct gconv_alias fake_alias
;
533 fake_alias
.fromname
= builtin_modules
[cnt
].from_string
;
535 if (__tfind (&fake_alias
, &__gconv_alias_db
, __gconv_alias_compare
)
537 /* It'll conflict so don't add it. */
540 insert_module (&builtin_modules
[cnt
]);
543 /* Add aliases for builtin conversions. */
544 cnt
= sizeof (builtin_aliases
) / sizeof (builtin_aliases
[0]);
547 char *copy
= strdupa (builtin_aliases
[--cnt
]);
548 add_alias (copy
, modules
);
551 /* Restore the error number. */
552 __set_errno (save_errno
);
557 /* Free all resources if necessary. */
558 static void __attribute__ ((unused
))
561 if (__gconv_path_elem
!= NULL
&& __gconv_path_elem
!= &empty_path_elem
)
562 free ((void *) __gconv_path_elem
);
565 text_set_element (__libc_subfreeres
, free_mem
);