Pass a valid pointer to pthread_setspecific to avoid GCC 11 warning.
[glibc.git] / iconv / gconv_conf.c
blob682f9498343ccc96ec5ceef8842cd74d2dec34f2
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/>. */
20 #include <assert.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <limits.h>
24 #include <locale.h>
25 #include <search.h>
26 #include <stddef.h>
27 #include <stdio.h>
28 #include <stdio_ext.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <sys/param.h>
34 #include <libc-lock.h>
35 #include <gconv_int.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. */
42 struct path_elem
44 const char *name;
45 size_t len;
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 /* Name of the file containing the module information in the directories
58 along the path. */
59 static const char gconv_conf_filename[] = "gconv-modules";
61 /* Filename extension for the modules. */
62 #ifndef MODULE_EXT
63 # define MODULE_EXT ".so"
64 #endif
65 static const char gconv_module_ext[] = MODULE_EXT;
67 /* We have a few builtin transformations. */
68 static struct gconv_module builtin_modules[] =
70 #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, BtowcFct, \
71 MinF, MaxF, MinT, MaxT) \
72 { \
73 .from_string = From, \
74 .to_string = To, \
75 .cost_hi = Cost, \
76 .cost_lo = INT_MAX, \
77 .module_name = Name \
79 #define BUILTIN_ALIAS(From, To)
81 #include "gconv_builtin.h"
83 #undef BUILTIN_TRANSFORMATION
84 #undef BUILTIN_ALIAS
87 static const char builtin_aliases[] =
89 #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, BtowcFct, \
90 MinF, MaxF, MinT, MaxT)
91 #define BUILTIN_ALIAS(From, To) From "\0" To "\0"
93 #include "gconv_builtin.h"
95 #undef BUILTIN_TRANSFORMATION
96 #undef BUILTIN_ALIAS
99 #include <libio/libioP.h>
100 #define __getdelim(line, len, c, fp) _IO_getdelim (line, len, c, fp)
103 /* Value of the GCONV_PATH environment variable. */
104 const char *__gconv_path_envvar;
107 /* Test whether there is already a matching module known. */
108 static int
109 detect_conflict (const char *alias)
111 struct gconv_module *node = __gconv_modules_db;
113 while (node != NULL)
115 int cmpres = strcmp (alias, node->from_string);
117 if (cmpres == 0)
118 /* We have a conflict. */
119 return 1;
120 else if (cmpres < 0)
121 node = node->left;
122 else
123 node = node->right;
126 return node != NULL;
130 /* The actual code to add aliases. */
131 static void
132 add_alias2 (const char *from, const char *to, const char *wp, void *modules)
134 /* Test whether this alias conflicts with any available module. */
135 if (detect_conflict (from))
136 /* It does conflict, don't add the alias. */
137 return;
139 struct gconv_alias *new_alias = (struct gconv_alias *)
140 malloc (sizeof (struct gconv_alias) + (wp - from));
141 if (new_alias != NULL)
143 void **inserted;
145 new_alias->fromname = memcpy ((char *) new_alias
146 + sizeof (struct gconv_alias),
147 from, wp - from);
148 new_alias->toname = new_alias->fromname + (to - from);
150 inserted = (void **) __tsearch (new_alias, &__gconv_alias_db,
151 __gconv_alias_compare);
152 if (inserted == NULL || *inserted != new_alias)
153 /* Something went wrong, free this entry. */
154 free (new_alias);
159 /* Add new alias. */
160 static void
161 add_alias (char *rp, void *modules)
163 /* We now expect two more string. The strings are normalized
164 (converted to UPPER case) and strored in the alias database. */
165 char *from, *to, *wp;
167 while (__isspace_l (*rp, _nl_C_locobj_ptr))
168 ++rp;
169 from = wp = rp;
170 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
171 *wp++ = __toupper_l (*rp++, _nl_C_locobj_ptr);
172 if (*rp == '\0')
173 /* There is no `to' string on the line. Ignore it. */
174 return;
175 *wp++ = '\0';
176 to = ++rp;
177 while (__isspace_l (*rp, _nl_C_locobj_ptr))
178 ++rp;
179 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
180 *wp++ = __toupper_l (*rp++, _nl_C_locobj_ptr);
181 if (to == wp)
182 /* No `to' string, ignore the line. */
183 return;
184 *wp++ = '\0';
186 add_alias2 (from, to, wp, modules);
190 /* Insert a data structure for a new module in the search tree. */
191 static void
192 insert_module (struct gconv_module *newp, int tobefreed)
194 struct gconv_module **rootp = &__gconv_modules_db;
196 while (*rootp != NULL)
198 struct gconv_module *root = *rootp;
199 int cmpres;
201 cmpres = strcmp (newp->from_string, root->from_string);
202 if (cmpres == 0)
204 /* Both strings are identical. Insert the string at the
205 end of the `same' list if it is not already there. */
206 while (strcmp (newp->from_string, root->from_string) != 0
207 || strcmp (newp->to_string, root->to_string) != 0)
209 rootp = &root->same;
210 root = *rootp;
211 if (root == NULL)
212 break;
215 if (root != NULL)
217 /* This is a no new conversion. But maybe the cost is
218 better. */
219 if (newp->cost_hi < root->cost_hi
220 || (newp->cost_hi == root->cost_hi
221 && newp->cost_lo < root->cost_lo))
223 newp->left = root->left;
224 newp->right = root->right;
225 newp->same = root->same;
226 *rootp = newp;
228 free (root);
230 else if (tobefreed)
231 free (newp);
232 return;
235 break;
237 else if (cmpres < 0)
238 rootp = &root->left;
239 else
240 rootp = &root->right;
243 /* Plug in the new node here. */
244 *rootp = newp;
248 /* Add new module. */
249 static void
250 add_module (char *rp, const char *directory, size_t dir_len, void **modules,
251 size_t *nmodules, int modcounter)
253 /* We expect now
254 1. `from' name
255 2. `to' name
256 3. filename of the module
257 4. an optional cost value
259 struct gconv_alias fake_alias;
260 struct gconv_module *new_module;
261 char *from, *to, *module, *wp;
262 int need_ext;
263 int cost_hi;
265 while (__isspace_l (*rp, _nl_C_locobj_ptr))
266 ++rp;
267 from = rp;
268 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
270 *rp = __toupper_l (*rp, _nl_C_locobj_ptr);
271 ++rp;
273 if (*rp == '\0')
274 return;
275 *rp++ = '\0';
276 to = wp = rp;
277 while (__isspace_l (*rp, _nl_C_locobj_ptr))
278 ++rp;
279 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
280 *wp++ = __toupper_l (*rp++, _nl_C_locobj_ptr);
281 if (*rp == '\0')
282 return;
283 *wp++ = '\0';
285 ++rp;
286 while (__isspace_l (*rp, _nl_C_locobj_ptr));
287 module = wp;
288 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
289 *wp++ = *rp++;
290 if (*rp == '\0')
292 /* There is no cost, use one by default. */
293 *wp++ = '\0';
294 cost_hi = 1;
296 else
298 /* There might be a cost value. */
299 char *endp;
301 *wp++ = '\0';
302 cost_hi = strtol (rp, &endp, 10);
303 if (rp == endp || cost_hi < 1)
304 /* No useful information. */
305 cost_hi = 1;
308 if (module[0] == '\0')
309 /* No module name given. */
310 return;
311 if (module[0] == '/')
312 dir_len = 0;
314 /* See whether we must add the ending. */
315 need_ext = 0;
316 if (wp - module < (ptrdiff_t) sizeof (gconv_module_ext)
317 || memcmp (wp - sizeof (gconv_module_ext), gconv_module_ext,
318 sizeof (gconv_module_ext)) != 0)
319 /* We must add the module extension. */
320 need_ext = sizeof (gconv_module_ext) - 1;
322 /* See whether we have already an alias with this name defined. */
323 fake_alias.fromname = strndupa (from, to - from);
325 if (__tfind (&fake_alias, &__gconv_alias_db, __gconv_alias_compare) != NULL)
326 /* This module duplicates an alias. */
327 return;
329 new_module = (struct gconv_module *) calloc (1,
330 sizeof (struct gconv_module)
331 + (wp - from)
332 + dir_len + need_ext);
333 if (new_module != NULL)
335 char *tmp;
337 new_module->from_string = tmp = (char *) (new_module + 1);
338 tmp = __mempcpy (tmp, from, to - from);
340 new_module->to_string = tmp;
341 tmp = __mempcpy (tmp, to, module - to);
343 new_module->cost_hi = cost_hi;
344 new_module->cost_lo = modcounter;
346 new_module->module_name = tmp;
348 if (dir_len != 0)
349 tmp = __mempcpy (tmp, directory, dir_len);
351 tmp = __mempcpy (tmp, module, wp - module);
353 if (need_ext)
354 memcpy (tmp - 1, gconv_module_ext, sizeof (gconv_module_ext));
356 /* Now insert the new module data structure in our search tree. */
357 insert_module (new_module, 1);
362 /* Read the next configuration file. */
363 static void
364 read_conf_file (const char *filename, const char *directory, size_t dir_len,
365 void **modules, size_t *nmodules)
367 /* Note the file is opened with cancellation in the I/O functions
368 disabled. */
369 FILE *fp = fopen (filename, "rce");
370 char *line = NULL;
371 size_t line_len = 0;
372 static int modcounter;
374 /* Don't complain if a file is not present or readable, simply silently
375 ignore it. */
376 if (fp == NULL)
377 return;
379 /* No threads reading from this stream. */
380 __fsetlocking (fp, FSETLOCKING_BYCALLER);
382 /* Process the known entries of the file. Comments start with `#' and
383 end with the end of the line. Empty lines are ignored. */
384 while (!__feof_unlocked (fp))
386 char *rp, *endp, *word;
387 ssize_t n = __getdelim (&line, &line_len, '\n', fp);
388 if (n < 0)
389 /* An error occurred. */
390 break;
392 rp = line;
393 /* Terminate the line (excluding comments or newline) by an NUL byte
394 to simplify the following code. */
395 endp = strchr (rp, '#');
396 if (endp != NULL)
397 *endp = '\0';
398 else
399 if (rp[n - 1] == '\n')
400 rp[n - 1] = '\0';
402 while (__isspace_l (*rp, _nl_C_locobj_ptr))
403 ++rp;
405 /* If this is an empty line go on with the next one. */
406 if (rp == endp)
407 continue;
409 word = rp;
410 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
411 ++rp;
413 if (rp - word == sizeof ("alias") - 1
414 && memcmp (word, "alias", sizeof ("alias") - 1) == 0)
415 add_alias (rp, *modules);
416 else if (rp - word == sizeof ("module") - 1
417 && memcmp (word, "module", sizeof ("module") - 1) == 0)
418 add_module (rp, directory, dir_len, modules, nmodules, modcounter++);
419 /* else */
420 /* Otherwise ignore the line. */
423 free (line);
425 fclose (fp);
429 /* Determine the directories we are looking for data in. This function should
430 only be called from __gconv_read_conf. */
431 static void
432 __gconv_get_path (void)
434 struct path_elem *result;
436 /* This function is only ever called when __gconv_path_elem is NULL. */
437 result = __gconv_path_elem;
438 assert (result == NULL);
440 /* Determine the complete path first. */
441 char *gconv_path;
442 size_t gconv_path_len;
443 char *elem;
444 char *oldp;
445 char *cp;
446 int nelems;
447 char *cwd;
448 size_t cwdlen;
450 if (__gconv_path_envvar == NULL)
452 /* No user-defined path. Make a modifiable copy of the
453 default path. */
454 gconv_path = strdupa (default_gconv_path);
455 gconv_path_len = sizeof (default_gconv_path);
456 cwd = NULL;
457 cwdlen = 0;
459 else
461 /* Append the default path to the user-defined path. */
462 size_t user_len = strlen (__gconv_path_envvar);
464 gconv_path_len = user_len + 1 + sizeof (default_gconv_path);
465 gconv_path = alloca (gconv_path_len);
466 __mempcpy (__mempcpy (__mempcpy (gconv_path, __gconv_path_envvar,
467 user_len),
468 ":", 1),
469 default_gconv_path, sizeof (default_gconv_path));
470 cwd = __getcwd (NULL, 0);
471 cwdlen = __glibc_unlikely (cwd == NULL) ? 0 : strlen (cwd);
473 assert (default_gconv_path[0] == '/');
475 /* In a first pass we calculate the number of elements. */
476 oldp = NULL;
477 cp = strchr (gconv_path, ':');
478 nelems = 1;
479 while (cp != NULL)
481 if (cp != oldp + 1)
482 ++nelems;
483 oldp = cp;
484 cp = strchr (cp + 1, ':');
487 /* Allocate the memory for the result. */
488 result = malloc ((nelems + 1)
489 * sizeof (struct path_elem)
490 + gconv_path_len + nelems
491 + (nelems - 1) * (cwdlen + 1));
492 if (result != NULL)
494 char *strspace = (char *) &result[nelems + 1];
495 int n = 0;
497 /* Separate the individual parts. */
498 __gconv_max_path_elem_len = 0;
499 elem = __strtok_r (gconv_path, ":", &gconv_path);
500 assert (elem != NULL);
503 result[n].name = strspace;
504 if (elem[0] != '/')
506 assert (cwd != NULL);
507 strspace = __mempcpy (strspace, cwd, cwdlen);
508 *strspace++ = '/';
510 strspace = __stpcpy (strspace, elem);
511 if (strspace[-1] != '/')
512 *strspace++ = '/';
514 result[n].len = strspace - result[n].name;
515 if (result[n].len > __gconv_max_path_elem_len)
516 __gconv_max_path_elem_len = result[n].len;
518 *strspace++ = '\0';
519 ++n;
521 while ((elem = __strtok_r (NULL, ":", &gconv_path)) != NULL);
523 result[n].name = NULL;
524 result[n].len = 0;
527 __gconv_path_elem = result ?: (struct path_elem *) &empty_path_elem;
529 free (cwd);
533 /* Read all configuration files found in the user-specified and the default
534 path. This function should only be called once during the program's
535 lifetime. It disregards locking and synchronization because its only
536 caller, __gconv_load_conf, handles this. */
537 static void
538 __gconv_read_conf (void)
540 void *modules = NULL;
541 size_t nmodules = 0;
542 int save_errno = errno;
543 size_t cnt;
545 /* First see whether we should use the cache. */
546 if (__gconv_load_cache () == 0)
548 /* Yes, we are done. */
549 __set_errno (save_errno);
550 return;
553 #ifndef STATIC_GCONV
554 /* Find out where we have to look. */
555 __gconv_get_path ();
557 for (cnt = 0; __gconv_path_elem[cnt].name != NULL; ++cnt)
559 const char *elem = __gconv_path_elem[cnt].name;
560 size_t elem_len = __gconv_path_elem[cnt].len;
561 char *filename;
563 /* No slash needs to be inserted between elem and gconv_conf_filename;
564 elem already ends in a slash. */
565 filename = alloca (elem_len + sizeof (gconv_conf_filename));
566 __mempcpy (__mempcpy (filename, elem, elem_len),
567 gconv_conf_filename, sizeof (gconv_conf_filename));
569 /* Read the next configuration file. */
570 read_conf_file (filename, elem, elem_len, &modules, &nmodules);
572 #endif
574 /* Add the internal modules. */
575 for (cnt = 0; cnt < sizeof (builtin_modules) / sizeof (builtin_modules[0]);
576 ++cnt)
578 struct gconv_alias fake_alias;
580 fake_alias.fromname = (char *) builtin_modules[cnt].from_string;
582 if (__tfind (&fake_alias, &__gconv_alias_db, __gconv_alias_compare)
583 != NULL)
584 /* It'll conflict so don't add it. */
585 continue;
587 insert_module (&builtin_modules[cnt], 0);
590 /* Add aliases for builtin conversions. */
591 const char *cp = builtin_aliases;
594 const char *from = cp;
595 const char *to = __rawmemchr (from, '\0') + 1;
596 cp = __rawmemchr (to, '\0') + 1;
598 add_alias2 (from, to, cp, modules);
600 while (*cp != '\0');
602 /* Restore the error number. */
603 __set_errno (save_errno);
607 /* This "once" variable is used to do a one-time load of the configuration. */
608 __libc_once_define (static, once);
611 /* Read all configuration files found in the user-specified and the default
612 path, but do it only "once" using __gconv_read_conf to do the actual
613 work. This is the function that must be called when reading iconv
614 configuration. */
615 void
616 __gconv_load_conf (void)
618 __libc_once (once, __gconv_read_conf);
622 /* Free all resources if necessary. */
623 libc_freeres_fn (free_mem)
625 if (__gconv_path_elem != NULL && __gconv_path_elem != &empty_path_elem)
626 free ((void *) __gconv_path_elem);