1 /* Handle configuration data.
2 Copyright (C) 1997-2021 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, see
18 <https://www.gnu.org/licenses/>. */
28 #include <stdio_ext.h>
32 #include <sys/param.h>
34 #include <libc-lock.h>
35 #include <gconv_int.h>
36 #include <gconv_parseconfdir.h>
38 /* This is the default path where we look for module lists. */
39 static const char default_gconv_path
[] = GCONV_PATH
;
41 /* Type to represent search path. */
48 /* The path elements, as determined by the __gconv_get_path function.
49 All path elements end in a slash. */
50 struct path_elem
*__gconv_path_elem
;
51 /* Maximum length of a single path element in __gconv_path_elem. */
52 size_t __gconv_max_path_elem_len
;
54 /* We use the following struct if we couldn't allocate memory. */
55 static const struct path_elem empty_path_elem
= { NULL
, 0 };
57 /* Filename extension for the modules. */
59 # define MODULE_EXT ".so"
61 static const char gconv_module_ext
[] = MODULE_EXT
;
63 /* We have a few builtin transformations. */
64 static struct gconv_module builtin_modules
[] =
66 #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, BtowcFct, \
67 MinF, MaxF, MinT, MaxT) \
69 .from_string = From, \
75 #define BUILTIN_ALIAS(From, To)
77 #include "gconv_builtin.h"
79 #undef BUILTIN_TRANSFORMATION
83 static const char builtin_aliases
[] =
85 #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, BtowcFct, \
86 MinF, MaxF, MinT, MaxT)
87 #define BUILTIN_ALIAS(From, To) From "\0" To "\0"
89 #include "gconv_builtin.h"
91 #undef BUILTIN_TRANSFORMATION
96 /* Value of the GCONV_PATH environment variable. */
97 const char *__gconv_path_envvar
;
100 /* Test whether there is already a matching module known. */
102 detect_conflict (const char *alias
)
104 struct gconv_module
*node
= __gconv_modules_db
;
108 int cmpres
= strcmp (alias
, node
->from_string
);
111 /* We have a conflict. */
123 /* The actual code to add aliases. */
125 add_alias2 (const char *from
, const char *to
, const char *wp
)
127 /* Test whether this alias conflicts with any available module. */
128 if (detect_conflict (from
))
129 /* It does conflict, don't add the alias. */
132 struct gconv_alias
*new_alias
= (struct gconv_alias
*)
133 malloc (sizeof (struct gconv_alias
) + (wp
- from
));
134 if (new_alias
!= NULL
)
138 new_alias
->fromname
= memcpy ((char *) new_alias
139 + sizeof (struct gconv_alias
),
141 new_alias
->toname
= new_alias
->fromname
+ (to
- from
);
143 inserted
= (void **) __tsearch (new_alias
, &__gconv_alias_db
,
144 __gconv_alias_compare
);
145 if (inserted
== NULL
|| *inserted
!= new_alias
)
146 /* Something went wrong, free this entry. */
156 /* We now expect two more string. The strings are normalized
157 (converted to UPPER case) and strored in the alias database. */
158 char *from
, *to
, *wp
;
160 while (__isspace_l (*rp
, _nl_C_locobj_ptr
))
163 while (*rp
!= '\0' && !__isspace_l (*rp
, _nl_C_locobj_ptr
))
164 *wp
++ = __toupper_l (*rp
++, _nl_C_locobj_ptr
);
166 /* There is no `to' string on the line. Ignore it. */
170 while (__isspace_l (*rp
, _nl_C_locobj_ptr
))
172 while (*rp
!= '\0' && !__isspace_l (*rp
, _nl_C_locobj_ptr
))
173 *wp
++ = __toupper_l (*rp
++, _nl_C_locobj_ptr
);
175 /* No `to' string, ignore the line. */
179 add_alias2 (from
, to
, wp
);
183 /* Insert a data structure for a new module in the search tree. */
185 insert_module (struct gconv_module
*newp
, int tobefreed
)
187 struct gconv_module
**rootp
= &__gconv_modules_db
;
189 while (*rootp
!= NULL
)
191 struct gconv_module
*root
= *rootp
;
194 cmpres
= strcmp (newp
->from_string
, root
->from_string
);
197 /* Both strings are identical. Insert the string at the
198 end of the `same' list if it is not already there. */
199 while (strcmp (newp
->from_string
, root
->from_string
) != 0
200 || strcmp (newp
->to_string
, root
->to_string
) != 0)
210 /* This is a no new conversion. But maybe the cost is
212 if (newp
->cost_hi
< root
->cost_hi
213 || (newp
->cost_hi
== root
->cost_hi
214 && newp
->cost_lo
< root
->cost_lo
))
216 newp
->left
= root
->left
;
217 newp
->right
= root
->right
;
218 newp
->same
= root
->same
;
233 rootp
= &root
->right
;
236 /* Plug in the new node here. */
241 /* Add new module. */
243 add_module (char *rp
, const char *directory
, size_t dir_len
, int modcounter
)
248 3. filename of the module
249 4. an optional cost value
251 struct gconv_alias fake_alias
;
252 struct gconv_module
*new_module
;
253 char *from
, *to
, *module
, *wp
;
257 while (__isspace_l (*rp
, _nl_C_locobj_ptr
))
260 while (*rp
!= '\0' && !__isspace_l (*rp
, _nl_C_locobj_ptr
))
262 *rp
= __toupper_l (*rp
, _nl_C_locobj_ptr
);
269 while (__isspace_l (*rp
, _nl_C_locobj_ptr
))
271 while (*rp
!= '\0' && !__isspace_l (*rp
, _nl_C_locobj_ptr
))
272 *wp
++ = __toupper_l (*rp
++, _nl_C_locobj_ptr
);
278 while (__isspace_l (*rp
, _nl_C_locobj_ptr
));
280 while (*rp
!= '\0' && !__isspace_l (*rp
, _nl_C_locobj_ptr
))
284 /* There is no cost, use one by default. */
290 /* There might be a cost value. */
294 cost_hi
= strtol (rp
, &endp
, 10);
295 if (rp
== endp
|| cost_hi
< 1)
296 /* No useful information. */
300 if (module
[0] == '\0')
301 /* No module name given. */
303 if (module
[0] == '/')
306 /* See whether we must add the ending. */
308 if (wp
- module
< (ptrdiff_t) sizeof (gconv_module_ext
)
309 || memcmp (wp
- sizeof (gconv_module_ext
), gconv_module_ext
,
310 sizeof (gconv_module_ext
)) != 0)
311 /* We must add the module extension. */
312 need_ext
= sizeof (gconv_module_ext
) - 1;
314 /* See whether we have already an alias with this name defined. */
315 fake_alias
.fromname
= strndupa (from
, to
- from
);
317 if (__tfind (&fake_alias
, &__gconv_alias_db
, __gconv_alias_compare
) != NULL
)
318 /* This module duplicates an alias. */
321 new_module
= (struct gconv_module
*) calloc (1,
322 sizeof (struct gconv_module
)
324 + dir_len
+ need_ext
);
325 if (new_module
!= NULL
)
329 new_module
->from_string
= tmp
= (char *) (new_module
+ 1);
330 tmp
= __mempcpy (tmp
, from
, to
- from
);
332 new_module
->to_string
= tmp
;
333 tmp
= __mempcpy (tmp
, to
, module
- to
);
335 new_module
->cost_hi
= cost_hi
;
336 new_module
->cost_lo
= modcounter
;
338 new_module
->module_name
= tmp
;
341 tmp
= __mempcpy (tmp
, directory
, dir_len
);
343 tmp
= __mempcpy (tmp
, module
, wp
- module
);
346 memcpy (tmp
- 1, gconv_module_ext
, sizeof (gconv_module_ext
));
348 /* Now insert the new module data structure in our search tree. */
349 insert_module (new_module
, 1);
354 /* Determine the directories we are looking for data in. This function should
355 only be called from __gconv_read_conf. */
357 __gconv_get_path (void)
359 struct path_elem
*result
;
361 /* This function is only ever called when __gconv_path_elem is NULL. */
362 result
= __gconv_path_elem
;
363 assert (result
== NULL
);
365 /* Determine the complete path first. */
367 size_t gconv_path_len
;
375 if (__gconv_path_envvar
== NULL
)
377 /* No user-defined path. Make a modifiable copy of the
379 gconv_path
= strdupa (default_gconv_path
);
380 gconv_path_len
= sizeof (default_gconv_path
);
386 /* Append the default path to the user-defined path. */
387 size_t user_len
= strlen (__gconv_path_envvar
);
389 gconv_path_len
= user_len
+ 1 + sizeof (default_gconv_path
);
390 gconv_path
= alloca (gconv_path_len
);
391 __mempcpy (__mempcpy (__mempcpy (gconv_path
, __gconv_path_envvar
,
394 default_gconv_path
, sizeof (default_gconv_path
));
395 cwd
= __getcwd (NULL
, 0);
396 cwdlen
= __glibc_unlikely (cwd
== NULL
) ? 0 : strlen (cwd
);
398 assert (default_gconv_path
[0] == '/');
400 /* In a first pass we calculate the number of elements. */
402 cp
= strchr (gconv_path
, ':');
409 cp
= strchr (cp
+ 1, ':');
412 /* Allocate the memory for the result. */
413 result
= malloc ((nelems
+ 1)
414 * sizeof (struct path_elem
)
415 + gconv_path_len
+ nelems
416 + (nelems
- 1) * (cwdlen
+ 1));
419 char *strspace
= (char *) &result
[nelems
+ 1];
422 /* Separate the individual parts. */
423 __gconv_max_path_elem_len
= 0;
424 elem
= __strtok_r (gconv_path
, ":", &gconv_path
);
425 assert (elem
!= NULL
);
428 result
[n
].name
= strspace
;
431 assert (cwd
!= NULL
);
432 strspace
= __mempcpy (strspace
, cwd
, cwdlen
);
435 strspace
= __stpcpy (strspace
, elem
);
436 if (strspace
[-1] != '/')
439 result
[n
].len
= strspace
- result
[n
].name
;
440 if (result
[n
].len
> __gconv_max_path_elem_len
)
441 __gconv_max_path_elem_len
= result
[n
].len
;
446 while ((elem
= __strtok_r (NULL
, ":", &gconv_path
)) != NULL
);
448 result
[n
].name
= NULL
;
452 __gconv_path_elem
= result
?: (struct path_elem
*) &empty_path_elem
;
458 /* Read all configuration files found in the user-specified and the default
459 path. This function should only be called once during the program's
460 lifetime. It disregards locking and synchronization because its only
461 caller, __gconv_load_conf, handles this. */
463 __gconv_read_conf (void)
465 int save_errno
= errno
;
468 /* First see whether we should use the cache. */
469 if (__gconv_load_cache () == 0)
471 /* Yes, we are done. */
472 __set_errno (save_errno
);
477 /* Find out where we have to look. */
480 for (cnt
= 0; __gconv_path_elem
[cnt
].name
!= NULL
; ++cnt
)
481 gconv_parseconfdir (__gconv_path_elem
[cnt
].name
,
482 __gconv_path_elem
[cnt
].len
);
485 /* Add the internal modules. */
486 for (cnt
= 0; cnt
< sizeof (builtin_modules
) / sizeof (builtin_modules
[0]);
489 struct gconv_alias fake_alias
;
491 fake_alias
.fromname
= (char *) builtin_modules
[cnt
].from_string
;
493 if (__tfind (&fake_alias
, &__gconv_alias_db
, __gconv_alias_compare
)
495 /* It'll conflict so don't add it. */
498 insert_module (&builtin_modules
[cnt
], 0);
501 /* Add aliases for builtin conversions. */
502 const char *cp
= builtin_aliases
;
505 const char *from
= cp
;
506 const char *to
= __rawmemchr (from
, '\0') + 1;
507 cp
= __rawmemchr (to
, '\0') + 1;
509 add_alias2 (from
, to
, cp
);
513 /* Restore the error number. */
514 __set_errno (save_errno
);
518 /* This "once" variable is used to do a one-time load of the configuration. */
519 __libc_once_define (static, once
);
522 /* Read all configuration files found in the user-specified and the default
523 path, but do it only "once" using __gconv_read_conf to do the actual
524 work. This is the function that must be called when reading iconv
527 __gconv_load_conf (void)
529 __libc_once (once
, __gconv_read_conf
);
533 /* Free all resources if necessary. */
534 libc_freeres_fn (free_mem
)
536 if (__gconv_path_elem
!= NULL
&& __gconv_path_elem
!= &empty_path_elem
)
537 free ((void *) __gconv_path_elem
);