]> git.ipfire.org Git - thirdparty/glibc.git/blob - iconv/gconv_conf.c
Update.
[thirdparty/glibc.git] / iconv / gconv_conf.c
1 /* Handle configuration data.
2 Copyright (C) 1997, 1998 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5
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.
10
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.
15
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. */
20
21 #include <ctype.h>
22 #include <errno.h>
23 #include <search.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/param.h>
29
30 #include <gconv_int.h>
31
32
33 /* This is the default path where we look for module lists. */
34 static const char default_gconv_path[] = GCONV_PATH;
35
36 /* Name of the file containing the module information in the directories
37 along the path. */
38 static const char gconv_conf_filename[] = "gconv-modules";
39
40 /* Filename extension for the modules. */
41 #ifndef MODULE_EXT
42 # define MODULE_EXT ".so"
43 #endif
44 static const char gconv_module_ext[] = MODULE_EXT;
45
46 /* We have a few builtin transformations. */
47 static struct gconv_module builtin_modules[] =
48 {
49 #define BUILTIN_TRANSFORMATION(From, ConstPfx, ConstLen, To, Cost, Name, \
50 Fct, Init, End) \
51 { \
52 from_pattern: From, \
53 from_constpfx: ConstPfx, \
54 from_constpfx_len: ConstLen, \
55 from_regex: NULL, \
56 to_string: To, \
57 cost: Cost, \
58 module_name: Name \
59 },
60 #define BUILTIN_ALIAS(From, To)
61
62 #include "gconv_builtin.h"
63 };
64
65 #undef BUILTIN_TRANSFORMATION
66 #undef BUILTIN_ALIAS
67
68 static const char *
69 builtin_aliases[] =
70 {
71 #define BUILTIN_TRANSFORMATION(From, ConstPfx, ConstLen, To, Cost, Name, \
72 Fct, Init, End)
73 #define BUILTIN_ALIAS(From, To) From " " To,
74
75 #include "gconv_builtin.h"
76 };
77
78
79 /* Function for searching module. */
80 static int
81 module_compare (const void *p1, const void *p2)
82 {
83 struct gconv_module *s1 = (struct gconv_module *) p1;
84 struct gconv_module *s2 = (struct gconv_module *) p2;
85 int result;
86
87 if (s1->from_pattern == NULL)
88 {
89 if (s2->from_pattern == NULL)
90 result = strcmp (s1->from_constpfx, s2->from_constpfx);
91 else
92 result = -1;
93 }
94 else if (s2->from_pattern == NULL)
95 result = 1;
96 else
97 result = strcmp (s1->from_pattern, s2->from_pattern);
98
99 if (result == 0)
100 result = strcmp (s1->to_string, s2->to_string);
101
102 return result;
103 }
104
105
106 /* Add new alias. */
107 static inline void
108 add_alias (char *rp)
109 {
110 /* We now expect two more string. The strings are normalized
111 (converted to UPPER case) and strored in the alias database. */
112 struct gconv_alias *new_alias;
113 char *from, *to, *wp;
114
115 while (isspace (*rp))
116 ++rp;
117 from = wp = rp;
118 while (*rp != '\0' && !isspace (*rp))
119 ++rp;
120 if (*rp == '\0')
121 /* There is no `to' string on the line. Ignore it. */
122 return;
123 *rp++ = '\0';
124 to = wp = rp;
125 while (isspace (*rp))
126 ++rp;
127 while (*rp != '\0' && !isspace (*rp))
128 *wp++ = *rp++;
129 if (to == wp)
130 /* No `to' string, ignore the line. */
131 return;
132 *wp++ = '\0';
133
134 new_alias = (struct gconv_alias *)
135 malloc (sizeof (struct gconv_alias) + (wp - from));
136 if (new_alias != NULL)
137 {
138 new_alias->fromname = memcpy ((char *) new_alias
139 + sizeof (struct gconv_alias),
140 from, wp - from);
141 new_alias->toname = new_alias->fromname + (to - from);
142
143 if (__tsearch (new_alias, &__gconv_alias_db, __gconv_alias_compare)
144 == NULL)
145 /* Something went wrong, free this entry. */
146 free (new_alias);
147 }
148 }
149
150
151 /* Add new module. */
152 static inline void
153 add_module (char *rp, const char *directory, size_t dir_len, void **modules,
154 size_t *nmodules)
155 {
156 /* We expect now
157 1. `from' name
158 2. `to' name
159 3. filename of the module
160 4. an optional cost value
161 */
162 struct gconv_module *new_module;
163 char *from, *to, *module, *wp;
164 size_t const_len;
165 int from_is_regex;
166 int need_ext;
167 int cost;
168
169 while (isspace (*rp))
170 ++rp;
171 from = rp;
172 from_is_regex = 0;
173 while (*rp != '\0' && !isspace (*rp))
174 {
175 if (!isalnum (*rp) && *rp != '-' && *rp != '/' && *rp != '.'
176 && *rp != '_')
177 from_is_regex = 1;
178 ++rp;
179 }
180 if (*rp == '\0')
181 return;
182 *rp++ = '\0';
183 to = wp = rp;
184 while (isspace (*rp))
185 ++rp;
186 while (*rp != '\0' && !isspace (*rp))
187 *wp++ = *rp++;
188 if (*rp == '\0')
189 return;
190 *wp++ = '\0';
191 do
192 ++rp;
193 while (isspace (*rp));
194 module = wp;
195 while (*rp != '\0' && !isspace (*rp))
196 *wp++ = *rp++;
197 if (*rp == '\0')
198 {
199 /* There is no cost, use one by default. */
200 *wp++ = '\0';
201 cost = 1;
202 }
203 else
204 {
205 /* There might be a cost value. */
206 char *endp;
207
208 *wp++ = '\0';
209 cost = strtol (rp, &endp, 10);
210 if (rp == endp)
211 /* No useful information. */
212 cost = 1;
213 }
214
215 if (module[0] == '\0')
216 /* No module name given. */
217 return;
218 if (module[0] == '/')
219 dir_len = 0;
220 else
221 /* Increment by one for the slash. */
222 ++dir_len;
223
224 /* See whether we must add the ending. */
225 need_ext = 0;
226 if (wp - module < sizeof (gconv_module_ext)
227 || memcmp (wp - sizeof (gconv_module_ext), gconv_module_ext,
228 sizeof (gconv_module_ext)) != 0)
229 /* We must add the module extension. */
230 need_ext = sizeof (gconv_module_ext) - 1;
231
232 /* We've collected all the information, now create an entry. */
233
234 if (from_is_regex)
235 {
236 const_len = 0;
237 while (isalnum (from[const_len]) || from[const_len] == '-'
238 || from[const_len] == '/' || from[const_len] == '.'
239 || from[const_len] == '_')
240 ++const_len;
241 }
242 else
243 const_len = to - from - 1;
244
245 new_module = (struct gconv_module *) malloc (sizeof (struct gconv_module)
246 + (wp - from)
247 + dir_len + need_ext);
248 if (new_module != NULL)
249 {
250 char *tmp;
251
252 new_module->from_constpfx = memcpy ((char *) new_module
253 + sizeof (struct gconv_module),
254 from, to - from);
255 if (from_is_regex)
256 new_module->from_pattern = new_module->from_constpfx;
257 else
258 new_module->from_pattern = NULL;
259
260 new_module->from_constpfx_len = const_len;
261
262 new_module->from_regex = NULL;
263
264 new_module->to_string = memcpy ((char *) new_module->from_constpfx
265 + (to - from), to, module - to);
266
267 new_module->cost = cost;
268
269 new_module->module_name = (char *) new_module->to_string + (module - to);
270
271 if (dir_len == 0)
272 tmp = (char *) new_module->module_name;
273 else
274 {
275 tmp = __mempcpy ((char *) new_module->module_name,
276 directory, dir_len - 1);
277 *tmp++ = '/';
278 }
279
280 tmp = __mempcpy (tmp, module, wp - module);
281
282 if (need_ext)
283 memcpy (tmp - 1, gconv_module_ext, sizeof (gconv_module_ext));
284
285 if (__tfind (new_module, modules, module_compare) == NULL)
286 if (__tsearch (new_module, modules, module_compare) == NULL)
287 /* Something went wrong while inserting the new module. */
288 free (new_module);
289 else
290 ++*nmodules;
291 }
292 }
293
294
295 static void
296 insert_module (const void *nodep, VISIT value, int level)
297 {
298 if (value == preorder || value == leaf)
299 __gconv_modules_db[__gconv_nmodules++] = *(struct gconv_module **) nodep;
300 }
301
302 static void
303 nothing (void *unused __attribute__ ((unused)))
304 {
305 }
306
307
308 /* Read the next configuration file. */
309 static void
310 internal_function
311 read_conf_file (const char *filename, const char *directory, size_t dir_len,
312 void **modules, size_t *nmodules)
313 {
314 FILE *fp = fopen (filename, "r");
315 char *line = NULL;
316 size_t line_len = 0;
317
318 /* Don't complain if a file is not present or readable, simply silently
319 ignore it. */
320 if (fp == NULL)
321 return;
322
323 /* Process the known entries of the file. Comments start with `#' and
324 end with the end of the line. Empty lines are ignored. */
325 while (!feof (fp))
326 {
327 char *rp, *endp, *word;
328 ssize_t n = __getdelim (&line, &line_len, '\n', fp);
329 if (n < 0)
330 /* An error occurred. */
331 break;
332
333 rp = line;
334 /* Terminate the line (excluding comments or newline) by an NUL byte
335 to simplify the following code. */
336 endp = strchr (rp, '#');
337 if (endp != NULL)
338 *endp = '\0';
339 else
340 if (rp[n - 1] == '\n')
341 rp[n - 1] = '\0';
342
343 while (isspace (*rp))
344 ++rp;
345
346 /* If this is an empty line go on with the next one. */
347 if (rp == endp)
348 continue;
349
350 word = rp;
351 while (*rp != '\0' && !isspace (*rp))
352 ++rp;
353
354 if (rp - word == sizeof ("alias") - 1
355 && memcmp (word, "alias", sizeof ("alias") - 1) == 0)
356 add_alias (rp);
357 else if (rp - word == sizeof ("module") - 1
358 && memcmp (word, "module", sizeof ("module") - 1) == 0)
359 add_module (rp, directory, dir_len, modules, nmodules);
360 /* else */
361 /* Otherwise ignore the line. */
362 }
363
364 if (line != NULL)
365 free (line);
366 fclose (fp);
367 }
368
369
370 /* Read all configuration files found in the user-specified and the default
371 path. */
372 void
373 internal_function
374 __gconv_read_conf (void)
375 {
376 const char *user_path = __secure_getenv ("GCONV_PATH");
377 char *gconv_path, *elem;
378 void *modules = NULL;
379 size_t nmodules = 0;
380 int save_errno = errno;
381 size_t cnt;
382
383 if (user_path == NULL)
384 /* No user-defined path. Make a modifiable copy of the default path. */
385 gconv_path = strdupa (default_gconv_path);
386 else
387 {
388 /* Append the default path to the user-defined path. */
389 size_t user_len = strlen (user_path);
390 char *tmp;
391
392 gconv_path = alloca (user_len + 1 + sizeof (default_gconv_path));
393 tmp = __mempcpy (gconv_path, user_path, user_len);
394 *tmp++ = ':';
395 __mempcpy (tmp, default_gconv_path, sizeof (default_gconv_path));
396 }
397
398 elem = strtok_r (gconv_path, ":", &gconv_path);
399 while (elem != NULL)
400 {
401 char real_elem[MAXPATHLEN];
402
403 if (realpath (elem, real_elem) != NULL)
404 {
405 size_t elem_len = strlen (real_elem);
406 char *filename, *tmp;
407
408 filename = alloca (elem_len + 1 + sizeof (gconv_conf_filename));
409 tmp = __mempcpy (filename, real_elem, elem_len);
410 *tmp++ = '/';
411 __mempcpy (tmp, gconv_conf_filename, sizeof (gconv_conf_filename));
412
413 /* Read the next configuration file. */
414 read_conf_file (filename, real_elem, elem_len, &modules, &nmodules);
415 }
416
417 /* Get next element in the path. */
418 elem = strtok_r (NULL, ":", &gconv_path);
419 }
420
421 /* If the configuration files do not contain any valid module specification
422 remember this by setting the pointer to the module array to NULL. */
423 nmodules += sizeof (builtin_modules) / sizeof (builtin_modules[0]);
424 if (nmodules == 0)
425 __gconv_modules_db = NULL;
426 else
427 {
428 __gconv_modules_db =
429 (struct gconv_module **) malloc (nmodules
430 * sizeof (struct gconv_module));
431 if (__gconv_modules_db != NULL)
432 {
433 size_t cnt;
434
435 /* Insert all module entries into the array. */
436 __twalk (modules, insert_module);
437
438 /* No remove the tree data structure. */
439 __tdestroy (modules, nothing);
440
441 /* Finally insert the builtin transformations. */
442 for (cnt = 0; cnt < (sizeof (builtin_modules)
443 / sizeof (struct gconv_module)); ++cnt)
444 __gconv_modules_db[__gconv_nmodules++] = &builtin_modules[cnt];
445 }
446 }
447
448 /* Add aliases for builtin conversions. */
449 cnt = sizeof (builtin_aliases) / sizeof (builtin_aliases[0]);
450 while (cnt > 0)
451 {
452 char *copy = strdupa (builtin_aliases[--cnt]);
453 add_alias (copy);
454 }
455
456 /* Restore the error number. */
457 __set_errno (save_errno);
458 }