Add citrus backend code and iconv front end. This is intentionally
[dragonfly.git] / lib / libc / citrus / citrus_lookup.c
1 /*      $NetBSD: src/lib/libc/citrus/citrus_lookup.c,v 1.3 2004/07/21 14:16:34 tshiozak Exp $   */
2 /*      $DragonFly: src/lib/libc/citrus/citrus_lookup.c,v 1.1 2005/03/11 23:33:53 joerg Exp $ */
3
4 /*-
5  * Copyright (c)2003 Citrus Project,
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <assert.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <limits.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <paths.h>
39 #include <dirent.h>
40 #include <sys/types.h>
41
42 #include "citrus_namespace.h"
43 #include "citrus_bcs.h"
44 #include "citrus_region.h"
45 #include "citrus_memstream.h"
46 #include "citrus_mmap.h"
47 #include "citrus_db.h"
48 #include "citrus_db_hash.h"
49 #include "citrus_lookup.h"
50 #include "citrus_lookup_file.h"
51
52 struct _citrus_lookup {
53         union {
54                 struct {
55                         struct _citrus_db *db;
56                         struct _citrus_region file;
57                         int num, idx;
58                         struct _db_locator locator;
59                 } db;
60                 struct {
61                         struct _region r;
62                         struct _memstream ms;
63                 } plain;
64         } u;
65 #define cl_db           u.db.db
66 #define cl_dbidx        u.db.idx
67 #define cl_dbfile       u.db.file
68 #define cl_dbnum        u.db.num
69 #define cl_dblocator    u.db.locator
70 #define cl_plainr       u.plain.r
71 #define cl_plainms      u.plain.ms
72         int cl_ignore_case;
73         int cl_rewind;
74         char *cl_key;
75         size_t cl_keylen;
76         int (*cl_next)(struct _citrus_lookup *, struct _region *,
77                        struct _region *);
78         int (*cl_lookup)(struct _citrus_lookup *, const char *,
79                          struct _region *);
80         int (*cl_num_entries)(struct _citrus_lookup *);
81         void (*cl_close)(struct _citrus_lookup *);
82 };
83
84 static int
85 seq_get_num_entries_db(struct _citrus_lookup *cl)
86 {
87         return cl->cl_dbnum;
88 }
89
90 static int
91 seq_next_db(struct _citrus_lookup *cl,
92             struct _region *key, struct _region *data)
93 {
94
95         if (cl->cl_key) {
96                 if (key)
97                         _region_init(key, cl->cl_key, cl->cl_keylen);
98                 return _db_lookup_by_s(cl->cl_db, cl->cl_key, data,
99                                        &cl->cl_dblocator);
100         }
101
102         if (cl->cl_rewind) {
103                 cl->cl_dbidx = 0;
104         }
105         cl->cl_rewind = 0;
106         if (cl->cl_dbidx >= cl->cl_dbnum)
107                 return ENOENT;
108
109         return _db_get_entry(cl->cl_db, cl->cl_dbidx++, key, data);
110 }
111
112 static int
113 seq_lookup_db(struct _citrus_lookup *cl, const char *key,
114               struct _region *data)
115 {
116         cl->cl_rewind = 0;
117         free(cl->cl_key);
118         cl->cl_key = strdup(key);
119         if (cl->cl_ignore_case)
120                 _bcs_convert_to_lower(cl->cl_key);
121         cl->cl_keylen = strlen(cl->cl_key);
122         _db_locator_init(&cl->cl_dblocator);
123         return _db_lookup_by_s(cl->cl_db, cl->cl_key, data, &cl->cl_dblocator);
124 }
125
126 static void
127 seq_close_db(struct _citrus_lookup *cl)
128 {
129         _db_close(cl->cl_db);
130         _unmap_file(&cl->cl_dbfile);
131 }
132
133 static int
134 seq_open_db(struct _citrus_lookup *cl, const char *name)
135 {
136         int ret;
137         struct _region r;
138         char path[PATH_MAX];
139
140         snprintf(path, sizeof(path), "%s.db", name);
141         ret = _map_file(&r, path);
142         if (ret)
143                 return ret;
144
145         ret = _db_open(&cl->cl_db, &r, _CITRUS_LOOKUP_MAGIC,
146                        _db_hash_std, NULL);
147         if (ret) {
148                 _unmap_file(&r);
149                 return ret;
150         }
151
152         cl->cl_dbfile = r;
153         cl->cl_dbnum = _db_get_num_entries(cl->cl_db);
154         cl->cl_dbidx = 0;
155         cl->cl_rewind = 1;
156         cl->cl_lookup = &seq_lookup_db;
157         cl->cl_next = &seq_next_db;
158         cl->cl_num_entries = &seq_get_num_entries_db;
159         cl->cl_close = &seq_close_db;
160
161         return 0;
162 }
163
164 #define T_COMM '#'
165 static int
166 seq_next_plain(struct _citrus_lookup *cl, struct _region *key,
167                struct _region *data)
168 {
169         const char *p, *q;
170         size_t len;
171
172         if (cl->cl_rewind)
173                 _memstream_bind(&cl->cl_plainms, &cl->cl_plainr);
174         cl->cl_rewind = 0;
175
176 retry:
177         p = _memstream_getln(&cl->cl_plainms, &len);
178         if (p == NULL)
179                 return ENOENT;
180         /* ignore comment */
181         q = memchr(p, T_COMM, len);
182         if (q) {
183                 len = q-p;
184         }
185         /* ignore trailing spaces */
186         _bcs_trunc_rws_len(p, &len);
187         p = _bcs_skip_ws_len(p, &len);
188         q = _bcs_skip_nonws_len(p, &len);
189         if (p==q)
190                 goto retry;
191         if (cl->cl_key && (q-p != cl->cl_keylen ||
192                            memcmp(p, cl->cl_key, (size_t)(q-p)) != 0))
193                 goto retry;
194
195         /* found a entry */
196         if (key)
197                 /* LINTED: discard const */
198                 _region_init(key, (char *)p, q-p);
199         p = _bcs_skip_ws_len(q, &len);
200         if (data)
201                 /* LINTED: discard const */
202                 _region_init(data, len ? (char *)p : NULL, len);
203
204         return 0;
205 }
206
207 static int
208 seq_get_num_entries_plain(struct _citrus_lookup *cl)
209 {
210         int num;
211
212         num = 0;
213         while (seq_next_plain(cl, NULL, NULL) == 0)
214                 num++;
215
216         return num;
217 }
218
219 static int
220 seq_lookup_plain(struct _citrus_lookup *cl, const char *key,
221                  struct _region *data)
222 {
223         size_t len;
224         const char *p;
225
226         cl->cl_rewind = 0;
227         free(cl->cl_key);
228         cl->cl_key = strdup(key);
229         if (cl->cl_ignore_case)
230                 _bcs_convert_to_lower(cl->cl_key);
231         cl->cl_keylen = strlen(cl->cl_key);
232         _memstream_bind(&cl->cl_plainms, &cl->cl_plainr);
233         p = _memstream_matchline(&cl->cl_plainms, cl->cl_key, &len, 0);
234         if (p == NULL)
235                 return ENOENT;
236         if (data)
237                 /* LINTED: discard const */
238                 _region_init(data, (char *)p, len);
239
240         return 0;
241 }
242
243 static void
244 seq_close_plain(struct _citrus_lookup *cl)
245 {
246         _unmap_file(&cl->cl_plainr);
247 }
248
249 static int
250 seq_open_plain(struct _citrus_lookup *cl, const char *name)
251 {
252         int ret;
253
254         /* open read stream */
255         ret = _map_file(&cl->cl_plainr, name);
256         if (ret)
257                 return ret;
258
259         cl->cl_rewind = 1;
260         cl->cl_next = &seq_next_plain;
261         cl->cl_lookup = &seq_lookup_plain;
262         cl->cl_num_entries = &seq_get_num_entries_plain;
263         cl->cl_close = &seq_close_plain;
264
265         return 0;
266 }
267
268 int
269 _citrus_lookup_seq_open(struct _citrus_lookup **rcl, const char *name,
270                         int ignore_case)
271 {
272         int ret;
273         struct _citrus_lookup *cl;
274
275         cl = malloc(sizeof(*cl));
276         if (cl == NULL)
277                 return errno;
278
279         cl->cl_key = NULL;
280         cl->cl_keylen = 0;
281         cl->cl_ignore_case = ignore_case;
282         ret = seq_open_db(cl, name);
283         if (ret == ENOENT)
284                 ret = seq_open_plain(cl, name);
285         if (!ret)
286                 *rcl = cl;
287         else
288                 free(cl);
289
290         return ret;
291 }
292
293 void
294 _citrus_lookup_seq_rewind(struct _citrus_lookup *cl)
295 {
296         cl->cl_rewind = 1;
297         free(cl->cl_key);
298         cl->cl_key = NULL;
299         cl->cl_keylen = 0;
300 }
301
302 int
303 _citrus_lookup_seq_next(struct _citrus_lookup *cl,
304                         struct _region *key, struct _region *data)
305 {
306         return (*cl->cl_next)(cl, key, data);
307 }
308
309 int
310 _citrus_lookup_seq_lookup(struct _citrus_lookup *cl, const char *key,
311                           struct _region *data)
312 {
313         return (*cl->cl_lookup)(cl, key, data);
314 }
315
316 int
317 _citrus_lookup_get_number_of_entries(struct _citrus_lookup *cl)
318 {
319         return (*cl->cl_num_entries)(cl);
320 }
321
322 void
323 _citrus_lookup_seq_close(struct _citrus_lookup *cl)
324 {
325         free(cl->cl_key);
326         (*cl->cl_close)(cl);
327 }
328
329 char *
330 _citrus_lookup_simple(const char *name, const char *key,
331                       char *linebuf, size_t linebufsize, int ignore_case)
332 {
333         int ret;
334         struct _citrus_lookup *cl;
335         struct _region data;
336
337         ret = _citrus_lookup_seq_open(&cl, name, ignore_case);
338         if (ret)
339                 return NULL;
340
341         ret = _citrus_lookup_seq_lookup(cl, key, &data);
342         if (ret) {
343                 _citrus_lookup_seq_close(cl);
344                 return NULL;
345         }
346
347         snprintf(linebuf, linebufsize, "%.*s",
348                  (int)_region_size(&data), (const char *)_region_head(&data));
349
350         _citrus_lookup_seq_close(cl);
351
352         return linebuf;
353 }