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.
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
30 #include "gconv_int.h"
31 #include "localeinfo.h"
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)
46 /* Structure to represent results of found (or not) transliteration
50 /* This structure must remain the first member. */
51 struct trans_struct info;
59 /* Tree with results of previous calls to __gconv_translit_find. */
60 static void *search_tree;
62 /* We modify global data. */
63 __LOCK_INIT(static, lock);
65 /* Compare two transliteration entries. */
67 trans_compare (const void *p1, const void *p2)
69 const struct known_trans *s1 = (const struct known_trans *) p1;
70 const struct known_trans *s2 = (const struct known_trans *) p2;
72 return strcmp (s1->info.name, s2->info.name);
76 /* Open (maybe reopen) the module named in the struct. Get the function
77 and data structure pointers we need. */
79 open_translit (struct known_trans *trans)
81 __gconv_trans_query_fct queryfct;
83 trans->handle = __libc_dlopen (trans->fname);
84 if (trans->handle == NULL)
88 /* Find the required symbol. */
89 queryfct = __libc_dlsym (trans->handle, "gconv_trans_context");
92 /* We cannot live with that. */
94 __libc_dlclose (trans->handle);
99 /* Get the context. */
100 if (queryfct (trans->info.name, &trans->info.csnames, &trans->info.ncsnames)
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)
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");
117 trans->open_count = 1;
125 __gconv_translit_find (struct trans_struct *trans)
127 struct known_trans **found;
128 const struct path_elem *runp;
131 /* We have to have a name. */
132 assert (trans->name != NULL);
134 /* Acquire the lock. */
136 __lock_acquire(lock);
139 /* See whether we know this module already. */
140 found = tfind (trans, &search_tree, trans_compare);
143 /* Is this module available? */
144 if ((*found)->handle != NULL)
146 /* Maybe we have to reopen the file. */
147 if ((*found)->handle != (void *) -1)
148 /* The object is not unloaded. */
150 else if (open_translit (*found) == 0)
153 *trans = (*found)->info;
154 (*found)->open_count++;
161 size_t name_len = strlen (trans->name) + 1;
163 struct known_trans *newp;
165 /* We have to continue looking for the module. */
166 if (__gconv_path_elem == NULL)
169 /* See whether we have to append .so. */
170 if (name_len <= 4 || memcmp (&trans->name[name_len - 4], ".so", 3) != 0)
173 /* Create a new entry. */
174 newp = (struct known_trans *) malloc (sizeof (struct known_trans)
175 + (__gconv_max_path_elem_len
182 /* Clear the struct. */
183 memset (newp, '\0', sizeof (struct known_trans));
185 /* Store a copy of the module name. */
186 newp->info.name = cp = (char *) (newp + 1);
187 cp = memcpy (cp, trans->name, name_len);
192 /* Search in all the directories. */
193 for (runp = __gconv_path_elem; runp->name != NULL; ++runp)
195 strcpy ((char *) newp->fname, runp->name);
196 while(newp->fname != '\0') newp->fname++;
198 cp = memcpy (newp->fname,
199 trans->name, name_len);
202 memcpy (cp, ".so", sizeof (".so"));
204 if (open_translit (newp) == 0)
206 /* We found a module. */
215 /* In any case we'll add the entry to our search tree. */
216 if (tsearch (newp, &search_tree, trans_compare) == NULL)
218 /* Yickes, this should not happen. Unload the object. */
220 /* XXX unload here. */
226 __lock_release(lock);