1 /* $NetBSD: aliaslist.c,v 1.4 2014/12/10 04:37:55 christos Exp $ */
4 static char *rcsid
= "Id: aliaslist.c,v 1.1 2003/06/04 00:25:47 marka Exp ";
8 * Copyright (c) 2002 Japan Network Information Center. All rights reserved.
10 * By using this file, you agree to the terms and conditions set forth bellow.
12 * LICENSE TERMS AND CONDITIONS
14 * The following License Terms and Conditions apply, unless a different
15 * license is obtained from Japan Network Information Center ("JPNIC"),
16 * a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
17 * Chiyoda-ku, Tokyo 101-0047, Japan.
19 * 1. Use, Modification and Redistribution (including distribution of any
20 * modified or derived work) in source and/or binary forms is permitted
21 * under this License Terms and Conditions.
23 * 2. Redistribution of source code must retain the copyright notices as they
24 * appear in each source code file, this License Terms and Conditions.
26 * 3. Redistribution in binary form must reproduce the Copyright Notice,
27 * this License Terms and Conditions, in the documentation and/or other
28 * materials provided with the distribution. For the purposes of binary
29 * distribution the "Copyright Notice" refers to the following language:
30 * "Copyright (c) 2000-2002 Japan Network Information Center. All rights reserved."
32 * 4. The name of JPNIC may not be used to endorse or promote products
33 * derived from this Software without specific prior written approval of
36 * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
37 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
38 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
39 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JPNIC BE LIABLE
40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
41 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
42 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
45 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
46 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
57 #include <idn/aliaslist.h>
58 #include <idn/assert.h>
59 #include <idn/logmacro.h>
60 #include <idn/result.h>
63 char *pattern
; /* name pattern */
64 char *encoding
; /* MIME-preferred charset name */
65 struct aliasitem
*next
;
67 typedef struct aliasitem
*aliasitem_t
;
69 struct idn__aliaslist
{
70 aliasitem_t first_item
; /* first item of the list */
74 additem_to_top(idn__aliaslist_t list
,
75 const char *pattern
, const char *encoding
);
78 additem_to_bottom(idn__aliaslist_t list
,
79 const char *pattern
, const char *encoding
);
81 static int match(const char *pattern
, const char *str
);
83 static idn_result_t
create_item(const char *pattern
, const char *encoding
,
87 static void dump_list(idn__aliaslist_t list
);
91 idn__aliaslist_create(idn__aliaslist_t
*listp
) {
92 static int size
= sizeof(struct idn__aliaslist
);
94 TRACE(("idn__aliaslist_create()\n"));
96 assert(listp
!= NULL
);
98 if ((*listp
= malloc(size
)) == NULL
) {
99 return (idn_nomemory
);
101 (*listp
)->first_item
= NULL
;
103 return (idn_success
);
107 idn__aliaslist_destroy(idn__aliaslist_t list
) {
111 TRACE(("idn__aliaslist_destroy()\n"));
113 assert(list
!= NULL
);
115 current
= list
->first_item
;
116 while (current
!= NULL
) {
117 if (current
->pattern
!= NULL
) {
118 free(current
->pattern
);
120 if (current
->encoding
!= NULL
) {
121 free(current
->encoding
);
123 next
= current
->next
;
131 idn__aliaslist_aliasfile(idn__aliaslist_t list
, const char *path
) {
134 idn_result_t r
= idn_success
;
135 char line
[200], alias
[200], real
[200];
137 assert(path
!= NULL
);
139 TRACE(("idn__aliaslist_aliasfile(path=%s)\n", path
));
141 if ((fp
= fopen(path
, "r")) == NULL
) {
144 for (line_no
= 1; fgets(line
, sizeof(line
), fp
) != NULL
; line_no
++) {
145 unsigned char *p
= (unsigned char *)line
;
147 while (isascii(*p
) && isspace(*p
))
149 if (*p
== '#' || *p
== '\n')
151 if (sscanf((char *)p
, "%s %s", alias
, real
) == 2) {
152 r
= additem_to_bottom(list
, alias
, real
);
153 if (r
!= idn_success
)
156 INFO(("idn__aliaslist_aliasfile: file %s has "
157 "invalid contents at line %d\n",
159 r
= idn_invalid_syntax
;
173 idn__aliaslist_additem(idn__aliaslist_t list
,
174 const char *pattern
, const char *encoding
,
177 return additem_to_top(list
, pattern
, encoding
);
179 return additem_to_bottom(list
, pattern
, encoding
);
184 additem_to_top(idn__aliaslist_t list
,
185 const char *pattern
, const char *encoding
) {
186 aliasitem_t new_item
;
189 TRACE(("additem_to_top()\n"));
191 assert(list
!= NULL
);
192 assert(pattern
!= NULL
);
193 assert(encoding
!= NULL
);
195 if ((r
= create_item(pattern
, encoding
, &new_item
))
197 WARNING(("additem_to_top: malloc failed\n"));
201 new_item
->next
= list
->first_item
;
202 list
->first_item
= new_item
;
208 return (idn_success
);
212 additem_to_bottom(idn__aliaslist_t list
,
213 const char *pattern
, const char *encoding
) {
214 aliasitem_t new_item
;
217 TRACE(("additem_to_bottom()\n"));
219 assert(list
!= NULL
);
220 assert(pattern
!= NULL
);
221 assert(encoding
!= NULL
);
223 r
= create_item(pattern
, encoding
, &new_item
);
224 if (r
!= idn_success
) {
225 WARNING(("additem_to_bottom: malloc failed\n"));
229 if (list
->first_item
== NULL
) {
230 list
->first_item
= new_item
;
232 aliasitem_t cur_item
= list
->first_item
;
234 if (cur_item
->next
== NULL
) {
237 cur_item
= cur_item
->next
;
239 cur_item
->next
= new_item
;
242 return (idn_success
);
246 idn__aliaslist_find(idn__aliaslist_t list
,
247 const char *pattern
, char **encodingp
) {
250 TRACE(("idn__aliaslist_find()\n"));
252 assert(list
!= NULL
);
253 assert(pattern
!= NULL
);
256 DUMP(("target pattern: %s\n", pattern
));
258 current
= list
->first_item
;
259 while (current
!= NULL
) {
261 DUMP(("current pattern: %s, encoding: %s\n",
262 current
->pattern
, current
->encoding
));
264 if (match(current
->pattern
, pattern
)) {
265 *encodingp
= current
->encoding
;
266 return (idn_success
);
268 current
= current
->next
;
271 TRACE(("idn__aliaslist_find(): not found\n"));
272 *encodingp
= (char *)pattern
;
273 return (idn_notfound
);
277 * Wild card matching function that supports only '*'.
280 match(const char *pattern
, const char *str
) {
284 switch (c
= *pattern
++) {
286 return (*str
== '\0');
288 while (!match(pattern
, str
)) {
304 * List item creation.
305 * pattern and encoding must not be NULL.
308 create_item(const char *pattern
, const char *encoding
,
309 aliasitem_t
*itemp
) {
310 static size_t size
= sizeof(struct aliasitem
);
312 assert(pattern
!= NULL
);
313 assert(encoding
!= NULL
);
315 if ((*itemp
= malloc(size
)) == NULL
)
316 return (idn_nomemory
);
318 if (((*itemp
)->pattern
= malloc(strlen(pattern
) + 1)) == NULL
) {
321 return (idn_nomemory
);
324 if (((*itemp
)->encoding
= malloc(strlen(encoding
) + 1)) == NULL
) {
325 free((*itemp
)->pattern
);
328 return (idn_nomemory
);
331 (void)strcpy((*itemp
)->pattern
, pattern
);
332 (void)strcpy((*itemp
)->encoding
, encoding
);
333 (*itemp
)->next
= NULL
;
335 return (idn_success
);
340 dump_list(idn__aliaslist_t list
) {
344 TRACE(("dump_list()\n"));
346 TRACE(("list is NULL\n"));
349 item
= list
->first_item
;
351 while (item
!= NULL
) {
352 DUMP(("%d: %s\t%s\n", i
, item
->pattern
, item
->encoding
));