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