2 * Copyright (c) 2003, Artem B. Bityuckiy, SoftMine Corporation.
3 * Rights transferred to Franklin Electronic Publishers.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
32 * strnstr - locate a substring in a fixed-size string.
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.
40 * The strstr() function finds the first occurrence of the substring
41 * 'needle'in the string 'haystack'. At most 'length' bytes is searched.
44 * Returns a pointer to the beginning of the substring, or NULL if the
45 * substring was not found.
48 _DEFUN(strnstr, (haystack, needle, length),
49 _CONST char *haystack _AND
50 _CONST char *needle _AND
54 _CONST char *max = haystack + length;
56 if (*haystack == '\0')
57 return *needle == '\0' ? (char *)haystack : NULL;
59 while (haystack < max)
64 if (needle[i] == '\0')
65 return (char *)haystack;
66 if (needle[i] != haystack[i++])
75 * canonical_form - canonize 'str'.
78 * struct _reent *rptr - reent structure of curent thread/process.
79 * _CONST char *str - string to canonize.
82 * Converts all letters to small and substitute all '-' by '_'.
85 * Returns canonical form of 'str' if success, NULL if failure.
88 _DEFUN(canonical_form, (rptr, str),
89 struct _reent *rptr _AND
94 if (str == NULL || (p = p1 = _strdup_r(rptr, str)) == NULL)
97 for (; *str; str++, p++)
105 return (_CONST char *)p1;
109 * find_alias - find "official" name by alias.
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.
118 * 'table' contains the list of names and their aliases. "Official"
119 * names go first, e.g.:
121 * Official_name1 alias11 alias12 alias1N
122 * Official_name2 alias21 alias22 alias2N
123 * Official_nameM aliasM1 aliasM2 aliasMN
125 * If line begins with backspace it is considered as the continuation of
129 * Returns pointer to "official" name if success, NULL if failure.
132 _DEFUN(find_alias, (rptr, alias, table, len),
133 struct _reent *rptr _AND
134 _CONST char *alias _AND
135 _CONST char *table _AND
140 _CONST char *candidate;
141 int l = strlen(alias);
142 _CONST char *ptable = table;
143 _CONST char *table_end = table + len;
145 if (table == NULL || alias == NULL || *table == '\0' || *alias == '\0')
152 if ((p = strnstr(ptable, alias, len)) == NULL)
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')))
164 while(!(--p < table || (*p == '\n' && *(p+1) != ' ' && *(p+1) != '\t')));
169 for (end = p + 1; *end != '\t' && *end != ' ' && *end != '\n' && *end != '\0'; end++);
171 return _strndup_r(rptr, p, (size_t)(end - p));
175 * _iconv_construct_filename -- constructs full file name from it's
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.
184 * Function concatenates 'path' and 'file'.
185 * 'path' and 'name' shouldn't be NULL.
188 * The pointer to full file name if success, NULL if faulture.
191 _DEFUN(_iconv_construct_filename, (rptr, path, file),
192 struct _reent *rptr _AND
193 _CONST char *path _AND
199 if (path == NULL || file == NULL)
205 if ((fname = _malloc_r(rptr, len1 + len2 + 2 /* '/' + '\0' */)) == NULL)
208 memcpy(fname, path, len1);
209 if (fname[len1 - 1] != '/')
211 memcpy(fname + len1, file, len2);
212 fname[len1 + len2] = '\0';
218 * _iconv_resolve_alias - resolves "official" name by given alias.
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.
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
234 * Returns pointer to name found if success, NULL if failure.
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
244 _CONST char *name = alias;
245 _CONST char *ptr = alias;
249 /* Alias shouldn't contain whitespaces, '\n' and '\r' symbols */
251 if (*ptr == ' ' || *ptr == '\r' || *ptr++ == '\n')
254 if (cf && (name = canonical_form(rptr, alias)) == NULL)
257 if ((alias = find_alias(rptr, name, (char *)bialiases,
258 strlen(bialiases))) != NULL)
259 goto free_and_return;
261 if (_iconv_load_file(rptr, fname, &desc) != 0)
262 goto free_and_return;
264 alias = find_alias(rptr, name, desc.mem, desc.len);
266 close_free_and_return:
267 _iconv_unload_file(rptr, &desc);
270 _free_r(rptr, (_VOID_PTR)name);
275 * _iconv_resolve_cs_name - resolves convrter's "official" name by given alias.
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.
283 * First, tries to find 'cs' among built-in aliases. If not fount, tries to
284 * find it external file.
286 * RETURN: Official name if found, NULL else.
289 _DEFUN(_iconv_resolve_cs_name, (rptr, cs, path),
290 struct _reent *rptr _AND
296 if (path == NULL || *path == (_CONST char)0)
297 if ((path = _getenv_r(rptr, "NLSPATH")) == NULL || *path == '\0')
298 path = NLS_DEFAULT_NLSPATH;
300 fname = _iconv_construct_filename(rptr, path, ICONV_ALIASES_FNAME);
304 p = (char *)_iconv_resolve_alias(rptr, cs, _iconv_builtin_aliases,
305 1, (_CONST char *)fname);
307 _free_r(rptr, (_VOID_PTR)fname);