OSDN Git Service

r288@cf-ppc-macosx: monabuilder | 2008-12-07 13:17:34 +0900
[pf3gnuchains/pf3gnuchains3x.git] / newlib / libc / sys / linux / iconv / gconv_trans.c
1 /* Transliteration using the locale's data.
2    Copyright (C) 2000 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
5
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.
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    Lesser General Public License for more details.
15
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.  */
20
21 #include <assert.h>
22 #include <dlfcn.h>
23 #include <search.h>
24 #include <stdint.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <dirent.h>
28 #include <ltdl.h>
29
30 #include "gconv_int.h"
31 #include "localeinfo.h"
32
33 int
34 __gconv_transliterate (struct __gconv_step *step,
35                        struct __gconv_step_data *step_data,
36                        void *trans_data __attribute__ ((unused)),
37                        const unsigned char *inbufstart,
38                        const unsigned char **inbufp,
39                        const unsigned char *inbufend,
40                        unsigned char **outbufstart, size_t *irreversible)
41 {
42   return 0;
43 }
44
45
46 /* Structure to represent results of found (or not) transliteration
47    modules.  */
48 struct known_trans
49 {
50   /* This structure must remain the first member.  */
51   struct trans_struct info;
52
53   char *fname;
54   void *handle;
55   int open_count;
56 };
57
58
59 /* Tree with results of previous calls to __gconv_translit_find.  */
60 static void *search_tree;
61
62 /* We modify global data.   */
63 __LOCK_INIT(static, lock);
64
65 /* Compare two transliteration entries.  */
66 static int
67 trans_compare (const void *p1, const void *p2)
68 {
69   const struct known_trans *s1 = (const struct known_trans *) p1;
70   const struct known_trans *s2 = (const struct known_trans *) p2;
71
72   return strcmp (s1->info.name, s2->info.name);
73 }
74
75
76 /* Open (maybe reopen) the module named in the struct.  Get the function
77    and data structure pointers we need.  */
78 static int
79 open_translit (struct known_trans *trans)
80 {
81   __gconv_trans_query_fct queryfct;
82
83   trans->handle = __libc_dlopen (trans->fname);
84   if (trans->handle == NULL)
85     /* Not available.  */
86     return 1;
87
88   /* Find the required symbol.  */
89   queryfct = __libc_dlsym (trans->handle, "gconv_trans_context");
90   if (queryfct == NULL)
91     {
92       /* We cannot live with that.  */
93     close_and_out:
94       __libc_dlclose (trans->handle);
95       trans->handle = NULL;
96       return 1;
97     }
98
99   /* Get the context.  */
100   if (queryfct (trans->info.name, &trans->info.csnames, &trans->info.ncsnames)
101       != 0)
102     goto close_and_out;
103
104   /* Of course we also have to have the actual function.  */
105   trans->info.trans_fct = __libc_dlsym (trans->handle, "gconv_trans");
106   if (trans->info.trans_fct == NULL)
107     goto close_and_out;
108
109   /* Now the optional functions.  */
110   trans->info.trans_init_fct =
111     __libc_dlsym (trans->handle, "gconv_trans_init");
112   trans->info.trans_context_fct =
113     __libc_dlsym (trans->handle, "gconv_trans_context");
114   trans->info.trans_end_fct =
115     __libc_dlsym (trans->handle, "gconv_trans_end");
116
117   trans->open_count = 1;
118
119   return 0;
120 }
121
122
123 int
124 internal_function
125 __gconv_translit_find (struct trans_struct *trans)
126 {
127   struct known_trans **found;
128   const struct path_elem *runp;
129   int res = 1;
130
131   /* We have to have a name.  */
132   assert (trans->name != NULL);
133
134   /* Acquire the lock.  */
135 #ifdef HAVE_DD_LOCK
136   __lock_acquire(lock);
137 #endif
138
139   /* See whether we know this module already.  */
140   found = tfind (trans, &search_tree, trans_compare);
141   if (found != NULL)
142     {
143       /* Is this module available?  */
144       if ((*found)->handle != NULL)
145         {
146           /* Maybe we have to reopen the file.  */
147           if ((*found)->handle != (void *) -1)
148             /* The object is not unloaded.  */
149             res = 0;
150           else if (open_translit (*found) == 0)
151             {
152               /* Copy the data.  */
153               *trans = (*found)->info;
154               (*found)->open_count++;
155               res = 0;
156             }
157         }
158     }
159   else
160     {
161       size_t name_len = strlen (trans->name) + 1;
162       int need_so = 0;
163       struct known_trans *newp;
164
165       /* We have to continue looking for the module.  */
166       if (__gconv_path_elem == NULL)
167         __gconv_get_path ();
168
169       /* See whether we have to append .so.  */
170       if (name_len <= 4 || memcmp (&trans->name[name_len - 4], ".so", 3) != 0)
171         need_so = 1;
172
173       /* Create a new entry.  */
174       newp = (struct known_trans *) malloc (sizeof (struct known_trans)
175                                             + (__gconv_max_path_elem_len
176                                                + name_len + 3)
177                                             + name_len);
178       if (newp != NULL)
179         {
180           char *cp;
181
182           /* Clear the struct.  */
183           memset (newp, '\0', sizeof (struct known_trans));
184
185           /* Store a copy of the module name.  */
186           newp->info.name = cp = (char *) (newp + 1);
187           cp = memcpy (cp, trans->name, name_len);
188           cp += name_len;
189
190           newp->fname = cp;
191
192           /* Search in all the directories.  */
193           for (runp = __gconv_path_elem; runp->name != NULL; ++runp)
194             {
195               strcpy ((char *) newp->fname, runp->name);
196               while(newp->fname != '\0') newp->fname++;
197
198               cp = memcpy (newp->fname,
199                             trans->name, name_len);
200               cp += name_len;
201               if (need_so)
202                 memcpy (cp, ".so", sizeof (".so"));
203
204               if (open_translit (newp) == 0)
205                 {
206                   /* We found a module.  */
207                   res = 0;
208                   break;
209                 }
210             }
211
212           if (res)
213             newp->fname = NULL;
214
215           /* In any case we'll add the entry to our search tree.  */
216           if (tsearch (newp, &search_tree, trans_compare) == NULL)
217             {
218               /* Yickes, this should not happen.  Unload the object.  */
219               res = 1;
220               /* XXX unload here.  */
221             }
222         }
223     }
224
225 #ifdef HAVE_DD_LOCK
226   __lock_release(lock);
227 #endif
228
229   return res;
230 }