OSDN Git Service

2004-06-25 Andrew Cagney <cagney@gnu.org>
[pf3gnuchains/pf3gnuchains3x.git] / newlib / libc / iconv / lib / aliases.c
1 /*
2  * Copyright (c) 2003, Artem B. Bityuckiy, SoftMine Corporation.
3  * Rights transferred to Franklin Electronic Publishers.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 #include <string.h>
27 #include <stdlib.h>
28 #include <reent.h>
29 #include "local.h"
30
31 /*
32  * strnstr - locate a substring in a fixed-size string.
33  *
34  * PARAMETERS:
35  *    _CONST char *haystack - the string in which to search.
36  *    _CONST char *needle   - the string which to search.
37  *    int length            - the maximum 'haystack' string length.
38  *
39  * DESCRIPTION:
40  *    The  strstr() function finds the first occurrence of the substring 
41  *    'needle'in the string 'haystack'. At most 'length' bytes is searched.
42  *
43  * RETURN:
44  *    Returns a pointer to the beginning of the substring, or NULL if the 
45  *    substring was not found.
46  */
47 static char *
48 _DEFUN(strnstr, (haystack, needle, length),
49                 _CONST char *haystack _AND
50                 _CONST char *needle   _AND
51                 int length)
52 {
53   int i;
54   _CONST char *max = haystack + length;
55
56   if (*haystack == '\0')
57       return *needle == '\0' ? (char *)haystack : NULL;
58
59   while (haystack < max)
60   {
61       i = 0;
62       while (1)
63       {
64           if (needle[i] == '\0')
65               return (char *)haystack;
66           if (needle[i] != haystack[i++])
67               break;
68       }
69       haystack += 1;
70   }
71   return (char *)NULL;
72 }
73
74 /*
75  * canonical_form - canonize 'str'.
76  *
77  * PARAMETERS:
78  *    struct _reent *rptr - reent structure of curent thread/process.
79  *    _CONST char *str - string to canonize. 
80  *
81  * DESCRIPTION:
82  *     Converts all letters to small and substitute all '-' by '_'.
83  *
84  * RETURN:
85  *     Returns canonical form of 'str' if success, NULL if failure.
86  */
87 static _CONST char *
88 _DEFUN(canonical_form, (rptr, str), 
89                        struct _reent *rptr _AND
90                        _CONST char *str)
91 {
92     char *p, *p1;
93
94     if (str == NULL || (p = p1 = _strdup_r(rptr, str)) == NULL)
95         return NULL;
96
97     for (; *str; str++, p++)
98     {
99         if (*str == '-')
100             *p = '_';
101         else
102             *p = tolower(*str);
103     }
104
105     return (_CONST char *)p1; 
106 }
107
108 /*
109  * find_alias - find "official" name by alias. 
110  *
111  * PARAMETERS:
112  *    struct _reent *rptr - reent structure of curent thread/process.
113  *    _CONST char *alias  - alias by which "official" name should be found.
114  *    _CONST char *table  - aliases table.
115  *    int len             - aliases table length.
116  *
117  * DESCRIPTION:
118  *     'table' contains the list of names and their aliases. "Official" 
119  *      names go first, e.g.:
120  *
121  *     Official_name1 alias11 alias12 alias1N
122  *     Official_name2 alias21 alias22 alias2N
123  *     Official_nameM aliasM1 aliasM2 aliasMN
124  *
125  *     If line begins with backspace it is considered as the continuation of
126  *     previous line.
127  *
128  * RETURN:
129  *     Returns pointer to "official" name if success, NULL if failure.
130  */
131 static _CONST char *
132 _DEFUN(find_alias, (rptr, alias, table, len),
133        struct _reent *rptr _AND
134        _CONST char *alias  _AND
135        _CONST char *table  _AND
136        int len)
137 {
138     _CONST char *end;
139     _CONST char *p;
140     _CONST char *candidate;
141     int l = strlen(alias);
142     _CONST char *ptable = table;
143     _CONST char *table_end = table + len;
144
145     if (table == NULL || alias == NULL || *table == '\0' || *alias == '\0')
146         return NULL;
147
148 search_again:
149     if (len < l)
150         return NULL;
151     
152     if ((p = strnstr(ptable, alias, len)) == NULL)
153         return NULL;
154     
155     /* Check that substring is segregated by '\n', '\t' or ' ' */
156     if (!((p == table || *(p-1) == ' ' || *(p-1) == '\n' || *(p-1) == '\t') &&
157           (p+l == table_end || *(p+l) == ' ' || *(p+l) == '\n' || *(p+l) == '\t')))
158     {
159         ptable = p + l;
160         len -= table - p;
161         goto search_again;
162     }
163
164     while(!(--p < table || (*p == '\n' && *(p+1) != ' ' && *(p+1) != '\t')));
165
166     if (*(++p) == '#')
167         return NULL;
168
169     for (end = p + 1; *end != '\t' && *end != ' ' && *end != '\n' && *end != '\0'; end++);
170
171     return _strndup_r(rptr, p, (size_t)(end - p));
172 }
173
174 /*
175  * _iconv_construct_filename -- constructs full file name from it's 
176  * path and name
177  *
178  * PARAMETERS:
179  *    struct _reent *rptr - reent structure of current thread/process.  
180  *    _CONST char *path - path to file.
181  *    _CONST char *file - the name of file.
182  *
183  * DESCRIPTION:
184  *     Function concatenates 'path' and 'file'.
185  *     'path' and 'name' shouldn't be NULL.
186  *
187  * RETURN:
188  *     The pointer to full file name if success, NULL if faulture.
189  */
190 char *
191 _DEFUN(_iconv_construct_filename, (rptr, path, file),
192        struct _reent *rptr _AND
193        _CONST char *path   _AND
194        _CONST char *file)
195 {
196     int len1, len2;
197     char *fname;
198     
199     if (path == NULL || file == NULL)
200         return NULL;
201   
202     len1 = strlen(path);
203     len2 = strlen(file);
204
205     if ((fname = _malloc_r(rptr, len1 + len2 + 2 /* '/' + '\0' */)) == NULL)
206         return NULL;
207
208     memcpy(fname, path, len1);
209     if (fname[len1 - 1] != '/')
210         fname[len1++] = '/';
211     memcpy(fname + len1, file, len2);
212     fname[len1 + len2] = '\0';
213     
214     return fname;
215 }
216
217 /*
218  * _iconv_resolve_alias - resolves "official" name by given alias. 
219  *
220  * PARAMETERS:
221  *    struct _reent *rptr    - reent structure of curent thread/process.
222  *    _CONST char *alias     - alias to resolve.
223  *    _CONST char *bialiases - aliases table.
224  *    int cf                 - canonize flag.
225  *    _CONST char *fname     - name of external file with list uf aliases.
226  *
227  * DESCRIPTION:
228  *     Tries to find 'alias' in aliases list ('bialiases'). If not 
229  *     found, searches at 'fname' file. 'cf' flag shows if 'alias' should be 
230  *     canonized before searching. Both 'bialiases' and 'fname' can't be NULL
231  *     pointers.
232  * 
233  * RETURN:
234  *     Returns pointer to name found if success, NULL if failure.
235  */
236 char *
237 _DEFUN(_iconv_resolve_alias, (rptr, alias, bialiases, cf, fname), 
238        struct _reent *rptr _AND
239        _CONST char *alias _AND
240        _CONST char *bialiases _AND
241        int cf _AND
242        _CONST char *fname)
243 {
244     _CONST char *name = alias;
245     _CONST char *ptr = alias;
246     _CONST char *table;
247     _iconv_fd_t desc;
248
249     /* Alias shouldn't contain whitespaces, '\n' and '\r' symbols */ 
250     while (*ptr)
251         if (*ptr == ' ' || *ptr == '\r' || *ptr++ == '\n')
252             return NULL;
253     
254     if (cf && (name = canonical_form(rptr, alias)) == NULL)
255         return NULL;
256
257     if ((alias = find_alias(rptr, name, (char *)bialiases, 
258                             strlen(bialiases))) != NULL)
259         goto free_and_return;
260     
261     if (_iconv_load_file(rptr, fname, &desc) != 0)
262         goto free_and_return;
263
264     alias = find_alias(rptr, name, desc.mem, desc.len);
265
266 close_free_and_return:    
267     _iconv_unload_file(rptr, &desc);
268 free_and_return:
269     if (cf)
270         _free_r(rptr, (_VOID_PTR)name);
271     return (char*)alias;
272 }
273
274 /*
275  * _iconv_resolve_cs_name - resolves convrter's "official" name by given alias. 
276  *
277  * PARAMETERS:
278  *     struct _reent *rptr - reent structure of curent thread/process.
279  *     _CONST char *cs     - charset alias to resolve.
280  *     _CONST char *path   - external file with aliases list.
281  *
282  * DESCRIPTION: 
283  * First, tries to find 'cs' among built-in aliases. If not fount, tries to 
284  * find it external file.
285  *
286  * RETURN: Official name if found, NULL else.
287  */
288 char *
289 _DEFUN(_iconv_resolve_cs_name, (rptr, cs, path), 
290                             struct _reent *rptr _AND
291                             _CONST char *cs    _AND
292                             _CONST char *path)
293 {
294   char *fname, *p;
295
296   if (path == NULL || *path == (_CONST char)0)
297       if ((path = _getenv_r(rptr, "NLSPATH")) == NULL || *path == '\0')
298           path = NLS_DEFAULT_NLSPATH;
299   
300   fname = _iconv_construct_filename(rptr, path, ICONV_ALIASES_FNAME);
301   if (fname == NULL)
302       return NULL;
303   
304   p = (char *)_iconv_resolve_alias(rptr, cs, _iconv_builtin_aliases, 
305                                 1, (_CONST char *)fname);
306   if (fname != NULL)
307       _free_r(rptr, (_VOID_PTR)fname);
308   return p;
309 }
310