(parse_grp_str): PIDLIST is supposed to have NGRPS elements.
[glibc/history.git] / iconv / gconv_conf.c
blobad5a7ee1051a3dc9d7519591a648ee649406b545
1 /* Handle configuration data.
2 Copyright (C) 1997-2003, 2005 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <assert.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <limits.h>
25 #include <locale.h>
26 #include <search.h>
27 #include <stddef.h>
28 #include <stdio.h>
29 #include <stdio_ext.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <sys/param.h>
35 #include <bits/libc-lock.h>
36 #include <gconv_int.h>
39 /* This is the default path where we look for module lists. */
40 static const char default_gconv_path[] = GCONV_PATH;
42 /* The path elements, as determined by the __gconv_get_path function.
43 All path elements end in a slash. */
44 struct path_elem *__gconv_path_elem;
45 /* Maximum length of a single path element in __gconv_path_elem. */
46 size_t __gconv_max_path_elem_len;
48 /* We use the following struct if we couldn't allocate memory. */
49 static const struct path_elem empty_path_elem = { NULL, 0 };
51 /* Name of the file containing the module information in the directories
52 along the path. */
53 static const char gconv_conf_filename[] = "gconv-modules";
55 /* Filename extension for the modules. */
56 #ifndef MODULE_EXT
57 # define MODULE_EXT ".so"
58 #endif
59 static const char gconv_module_ext[] = MODULE_EXT;
61 /* We have a few builtin transformations. */
62 static struct gconv_module builtin_modules[] =
64 #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, BtowcFct, \
65 MinF, MaxF, MinT, MaxT) \
66 { \
67 .from_string = From, \
68 .to_string = To, \
69 .cost_hi = Cost, \
70 .cost_lo = INT_MAX, \
71 .module_name = Name \
73 #define BUILTIN_ALIAS(From, To)
75 #include "gconv_builtin.h"
77 #undef BUILTIN_TRANSFORMATION
78 #undef BUILTIN_ALIAS
81 static const char *const builtin_aliases[] =
83 #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, BtowcFct, \
84 MinF, MaxF, MinT, MaxT)
85 #define BUILTIN_ALIAS(From, To) From " " To,
87 #include "gconv_builtin.h"
89 #undef BUILTIN_TRANSFORMATION
90 #undef BUILTIN_ALIAS
93 #ifdef USE_IN_LIBIO
94 # include <libio/libioP.h>
95 # define __getdelim(line, len, c, fp) _IO_getdelim (line, len, c, fp)
96 #endif
99 /* Value of the GCONV_PATH environment variable. */
100 const char *__gconv_path_envvar;
103 /* Test whether there is already a matching module known. */
104 static int
105 internal_function
106 detect_conflict (const char *alias)
108 struct gconv_module *node = __gconv_modules_db;
110 while (node != NULL)
112 int cmpres = strcmp (alias, node->from_string);
114 if (cmpres == 0)
115 /* We have a conflict. */
116 return 1;
117 else if (cmpres < 0)
118 node = node->left;
119 else
120 node = node->right;
123 return node != NULL;
127 /* Add new alias. */
128 static void
129 add_alias (char *rp, void *modules)
131 /* We now expect two more string. The strings are normalized
132 (converted to UPPER case) and strored in the alias database. */
133 struct gconv_alias *new_alias;
134 char *from, *to, *wp;
136 while (__isspace_l (*rp, _nl_C_locobj_ptr))
137 ++rp;
138 from = wp = rp;
139 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
140 *wp++ = __toupper_l (*rp++, _nl_C_locobj_ptr);
141 if (*rp == '\0')
142 /* There is no `to' string on the line. Ignore it. */
143 return;
144 *wp++ = '\0';
145 to = ++rp;
146 while (__isspace_l (*rp, _nl_C_locobj_ptr))
147 ++rp;
148 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
149 *wp++ = __toupper_l (*rp++, _nl_C_locobj_ptr);
150 if (to == wp)
151 /* No `to' string, ignore the line. */
152 return;
153 *wp++ = '\0';
155 /* Test whether this alias conflicts with any available module. */
156 if (detect_conflict (from))
157 /* It does conflict, don't add the alias. */
158 return;
160 new_alias = (struct gconv_alias *)
161 malloc (sizeof (struct gconv_alias) + (wp - from));
162 if (new_alias != NULL)
164 void **inserted;
166 new_alias->fromname = memcpy ((char *) new_alias
167 + sizeof (struct gconv_alias),
168 from, wp - from);
169 new_alias->toname = new_alias->fromname + (to - from);
171 inserted = (void **) __tsearch (new_alias, &__gconv_alias_db,
172 __gconv_alias_compare);
173 if (inserted == NULL || *inserted != new_alias)
174 /* Something went wrong, free this entry. */
175 free (new_alias);
180 /* Insert a data structure for a new module in the search tree. */
181 static void
182 internal_function
183 insert_module (struct gconv_module *newp, int tobefreed)
185 struct gconv_module **rootp = &__gconv_modules_db;
187 while (*rootp != NULL)
189 struct gconv_module *root = *rootp;
190 int cmpres;
192 cmpres = strcmp (newp->from_string, root->from_string);
193 if (cmpres == 0)
195 /* Both strings are identical. Insert the string at the
196 end of the `same' list if it is not already there. */
197 while (strcmp (newp->from_string, root->from_string) != 0
198 || strcmp (newp->to_string, root->to_string) != 0)
200 rootp = &root->same;
201 root = *rootp;
202 if (root == NULL)
203 break;
206 if (root != NULL)
208 /* This is a no new conversion. But maybe the cost is
209 better. */
210 if (newp->cost_hi < root->cost_hi
211 || (newp->cost_hi == root->cost_hi
212 && newp->cost_lo < root->cost_lo))
214 newp->left = root->left;
215 newp->right = root->right;
216 newp->same = root->same;
217 *rootp = newp;
219 free (root);
221 else if (tobefreed)
222 free (newp);
223 return;
226 break;
228 else if (cmpres < 0)
229 rootp = &root->left;
230 else
231 rootp = &root->right;
234 /* Plug in the new node here. */
235 *rootp = newp;
239 /* Add new module. */
240 static void
241 internal_function
242 add_module (char *rp, const char *directory, size_t dir_len, void **modules,
243 size_t *nmodules, int modcounter)
245 /* We expect now
246 1. `from' name
247 2. `to' name
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;
254 int need_ext;
255 int cost_hi;
257 while (__isspace_l (*rp, _nl_C_locobj_ptr))
258 ++rp;
259 from = rp;
260 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
262 *rp = __toupper_l (*rp, _nl_C_locobj_ptr);
263 ++rp;
265 if (*rp == '\0')
266 return;
267 *rp++ = '\0';
268 to = wp = rp;
269 while (__isspace_l (*rp, _nl_C_locobj_ptr))
270 ++rp;
271 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
272 *wp++ = __toupper_l (*rp++, _nl_C_locobj_ptr);
273 if (*rp == '\0')
274 return;
275 *wp++ = '\0';
277 ++rp;
278 while (__isspace_l (*rp, _nl_C_locobj_ptr));
279 module = wp;
280 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
281 *wp++ = *rp++;
282 if (*rp == '\0')
284 /* There is no cost, use one by default. */
285 *wp++ = '\0';
286 cost_hi = 1;
288 else
290 /* There might be a cost value. */
291 char *endp;
293 *wp++ = '\0';
294 cost_hi = strtol (rp, &endp, 10);
295 if (rp == endp || cost_hi < 1)
296 /* No useful information. */
297 cost_hi = 1;
300 if (module[0] == '\0')
301 /* No module name given. */
302 return;
303 if (module[0] == '/')
304 dir_len = 0;
306 /* See whether we must add the ending. */
307 need_ext = 0;
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. */
319 return;
321 new_module = (struct gconv_module *) calloc (1,
322 sizeof (struct gconv_module)
323 + (wp - from)
324 + dir_len + need_ext);
325 if (new_module != NULL)
327 char *tmp;
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;
340 if (dir_len != 0)
341 tmp = __mempcpy (tmp, directory, dir_len);
343 tmp = __mempcpy (tmp, module, wp - module);
345 if (need_ext)
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 /* Read the next configuration file. */
355 static void
356 internal_function
357 read_conf_file (const char *filename, const char *directory, size_t dir_len,
358 void **modules, size_t *nmodules)
360 /* Note the file is opened with cancellation in the I/O functions
361 disabled. */
362 FILE *fp = fopen (filename, "rc");
363 char *line = NULL;
364 size_t line_len = 0;
365 static int modcounter;
367 /* Don't complain if a file is not present or readable, simply silently
368 ignore it. */
369 if (fp == NULL)
370 return;
372 /* No threads reading from this stream. */
373 __fsetlocking (fp, FSETLOCKING_BYCALLER);
375 /* Process the known entries of the file. Comments start with `#' and
376 end with the end of the line. Empty lines are ignored. */
377 while (!feof_unlocked (fp))
379 char *rp, *endp, *word;
380 ssize_t n = __getdelim (&line, &line_len, '\n', fp);
381 if (n < 0)
382 /* An error occurred. */
383 break;
385 rp = line;
386 /* Terminate the line (excluding comments or newline) by an NUL byte
387 to simplify the following code. */
388 endp = strchr (rp, '#');
389 if (endp != NULL)
390 *endp = '\0';
391 else
392 if (rp[n - 1] == '\n')
393 rp[n - 1] = '\0';
395 while (__isspace_l (*rp, _nl_C_locobj_ptr))
396 ++rp;
398 /* If this is an empty line go on with the next one. */
399 if (rp == endp)
400 continue;
402 word = rp;
403 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
404 ++rp;
406 if (rp - word == sizeof ("alias") - 1
407 && memcmp (word, "alias", sizeof ("alias") - 1) == 0)
408 add_alias (rp, *modules);
409 else if (rp - word == sizeof ("module") - 1
410 && memcmp (word, "module", sizeof ("module") - 1) == 0)
411 add_module (rp, directory, dir_len, modules, nmodules, modcounter++);
412 /* else */
413 /* Otherwise ignore the line. */
416 free (line);
418 fclose (fp);
422 /* Determine the directories we are looking for data in. */
423 void
424 internal_function
425 __gconv_get_path (void)
427 struct path_elem *result;
428 __libc_lock_define_initialized (static, lock);
430 __libc_lock_lock (lock);
432 /* Make sure there wasn't a second thread doing it already. */
433 result = (struct path_elem *) __gconv_path_elem;
434 if (result == NULL)
436 /* Determine the complete path first. */
437 char *gconv_path;
438 size_t gconv_path_len;
439 char *elem;
440 char *oldp;
441 char *cp;
442 int nelems;
443 char *cwd;
444 size_t cwdlen;
446 if (__gconv_path_envvar == NULL)
448 /* No user-defined path. Make a modifiable copy of the
449 default path. */
450 gconv_path = strdupa (default_gconv_path);
451 gconv_path_len = sizeof (default_gconv_path);
452 cwd = NULL;
453 cwdlen = 0;
455 else
457 /* Append the default path to the user-defined path. */
458 size_t user_len = strlen (__gconv_path_envvar);
460 gconv_path_len = user_len + 1 + sizeof (default_gconv_path);
461 gconv_path = alloca (gconv_path_len);
462 __mempcpy (__mempcpy (__mempcpy (gconv_path, __gconv_path_envvar,
463 user_len),
464 ":", 1),
465 default_gconv_path, sizeof (default_gconv_path));
466 cwd = __getcwd (NULL, 0);
467 cwdlen = strlen (cwd);
469 assert (default_gconv_path[0] == '/');
471 /* In a first pass we calculate the number of elements. */
472 oldp = NULL;
473 cp = strchr (gconv_path, ':');
474 nelems = 1;
475 while (cp != NULL)
477 if (cp != oldp + 1)
478 ++nelems;
479 oldp = cp;
480 cp = strchr (cp + 1, ':');
483 /* Allocate the memory for the result. */
484 result = (struct path_elem *) malloc ((nelems + 1)
485 * sizeof (struct path_elem)
486 + gconv_path_len + nelems
487 + (nelems - 1) * (cwdlen + 1));
488 if (result != NULL)
490 char *strspace = (char *) &result[nelems + 1];
491 int n = 0;
493 /* Separate the individual parts. */
494 __gconv_max_path_elem_len = 0;
495 elem = __strtok_r (gconv_path, ":", &gconv_path);
496 assert (elem != NULL);
499 result[n].name = strspace;
500 if (elem[0] != '/')
502 assert (cwd != NULL);
503 strspace = __mempcpy (strspace, cwd, cwdlen);
504 *strspace++ = '/';
506 strspace = __stpcpy (strspace, elem);
507 if (strspace[-1] != '/')
508 *strspace++ = '/';
510 result[n].len = strspace - result[n].name;
511 if (result[n].len > __gconv_max_path_elem_len)
512 __gconv_max_path_elem_len = result[n].len;
514 *strspace++ = '\0';
515 ++n;
517 while ((elem = __strtok_r (NULL, ":", &gconv_path)) != NULL);
519 result[n].name = NULL;
520 result[n].len = 0;
523 __gconv_path_elem = result ?: (struct path_elem *) &empty_path_elem;
525 if (cwd != NULL)
526 free (cwd);
529 __libc_lock_unlock (lock);
533 /* Read all configuration files found in the user-specified and the default
534 path. */
535 void
536 attribute_hidden
537 __gconv_read_conf (void)
539 void *modules = NULL;
540 size_t nmodules = 0;
541 int save_errno = errno;
542 size_t cnt;
544 /* First see whether we should use the cache. */
545 if (__gconv_load_cache () == 0)
547 /* Yes, we are done. */
548 __set_errno (save_errno);
549 return;
552 #ifndef STATIC_GCONV
553 /* Find out where we have to look. */
554 if (__gconv_path_elem == NULL)
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 cnt = sizeof (builtin_aliases) / sizeof (builtin_aliases[0]);
592 while (cnt > 0)
594 char *copy = strdupa (builtin_aliases[--cnt]);
595 add_alias (copy, modules);
598 /* Restore the error number. */
599 __set_errno (save_errno);
604 /* Free all resources if necessary. */
605 libc_freeres_fn (free_mem)
607 if (__gconv_path_elem != NULL && __gconv_path_elem != &empty_path_elem)
608 free ((void *) __gconv_path_elem);