libncurses: updated to 6.0
[tomato.git] / release / src / router / libncurses / ncurses / tinfo / hashed_db.c
blobb59420585eac2ab7489983fa5d3d6cf782bca572
1 /****************************************************************************
2 * Copyright (c) 2006-2011,2013 Free Software Foundation, Inc. *
3 * *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
11 * *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
14 * *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
22 * *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
26 * authorization. *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Thomas E. Dickey 2006-on *
31 ****************************************************************************/
33 #include <curses.priv.h>
34 #include <tic.h>
35 #include <hashed_db.h>
37 #if USE_HASHED_DB
39 MODULE_ID("$Id: hashed_db.c,v 1.17 2013/12/15 00:33:01 tom Exp $")
41 #if HASHED_DB_API >= 2
42 static DBC *cursor;
43 #endif
45 typedef struct _myconn {
46 struct _myconn *next;
47 DB *db;
48 char *path;
49 bool modify;
50 } MYCONN;
52 static MYCONN *connections;
54 static void
55 cleanup(void)
57 while (connections != 0) {
58 _nc_db_close(connections->db);
62 static DB *
63 find_connection(const char *path, bool modify)
65 DB *result = 0;
66 MYCONN *p;
68 for (p = connections; p != 0; p = p->next) {
69 if (!strcmp(p->path, path) && p->modify == modify) {
70 result = p->db;
71 break;
75 return result;
78 static void
79 drop_connection(DB * db)
81 MYCONN *p, *q;
83 for (p = connections, q = 0; p != 0; q = p, p = p->next) {
84 if (p->db == db) {
85 if (q != 0)
86 q->next = p->next;
87 else
88 connections = p->next;
89 free(p->path);
90 free(p);
91 break;
96 static void
97 make_connection(DB * db, const char *path, bool modify)
99 MYCONN *p = typeCalloc(MYCONN, 1);
101 if (p != 0) {
102 p->db = db;
103 p->path = strdup(path);
104 p->modify = modify;
105 if (p->path != 0) {
106 p->next = connections;
107 connections = p;
108 } else {
109 free(p);
115 * Open the database.
117 NCURSES_EXPORT(DB *)
118 _nc_db_open(const char *path, bool modify)
120 DB *result = 0;
121 int code;
123 if (connections == 0)
124 atexit(cleanup);
126 if ((result = find_connection(path, modify)) == 0) {
128 #if HASHED_DB_API >= 4
129 db_create(&result, NULL, 0);
130 if ((code = result->open(result,
131 NULL,
132 path,
133 NULL,
134 DB_HASH,
135 modify ? DB_CREATE : DB_RDONLY,
136 0644)) != 0) {
137 result = 0;
139 #elif HASHED_DB_API >= 3
140 db_create(&result, NULL, 0);
141 if ((code = result->open(result,
142 path,
143 NULL,
144 DB_HASH,
145 modify ? DB_CREATE : DB_RDONLY,
146 0644)) != 0) {
147 result = 0;
149 #elif HASHED_DB_API >= 2
150 if ((code = db_open(path,
151 DB_HASH,
152 modify ? DB_CREATE : DB_RDONLY,
153 0644,
154 (DB_ENV *) 0,
155 (DB_INFO *) 0,
156 &result)) != 0) {
157 result = 0;
159 #else
160 if ((result = dbopen(path,
161 modify ? (O_CREAT | O_RDWR) : O_RDONLY,
162 0644,
163 DB_HASH,
164 NULL)) == 0) {
165 code = errno;
167 #endif
168 if (result != 0) {
169 make_connection(result, path, modify);
170 T(("opened %s", path));
171 } else {
172 T(("cannot open %s: %s", path, strerror(code)));
175 return result;
179 * Close the database. Do not attempt to use the 'db' handle after this call.
181 NCURSES_EXPORT(int)
182 _nc_db_close(DB * db)
184 int result;
186 drop_connection(db);
187 #if HASHED_DB_API >= 2
188 result = db->close(db, 0);
189 #else
190 result = db->close(db);
191 #endif
192 return result;
196 * Write a record to the database.
198 * Returns 0 on success.
200 * FIXME: the FreeBSD cap_mkdb program assumes the database could have
201 * duplicates. There appears to be no good reason for that (review/fix).
203 NCURSES_EXPORT(int)
204 _nc_db_put(DB * db, DBT * key, DBT * data)
206 int result;
207 #if HASHED_DB_API >= 2
208 /* remove any pre-existing value, since we do not want duplicates */
209 (void) db->del(db, NULL, key, 0);
210 result = db->put(db, NULL, key, data, DB_NOOVERWRITE);
211 #else
212 result = db->put(db, key, data, R_NOOVERWRITE);
213 #endif
214 return result;
218 * Read a record from the database.
220 * Returns 0 on success.
222 NCURSES_EXPORT(int)
223 _nc_db_get(DB * db, DBT * key, DBT * data)
225 int result;
227 memset(data, 0, sizeof(*data));
228 #if HASHED_DB_API >= 2
229 result = db->get(db, NULL, key, data, 0);
230 #else
231 result = db->get(db, key, data, 0);
232 #endif
233 return result;
237 * Read the first record from the database, ignoring order.
239 * Returns 0 on success.
241 NCURSES_EXPORT(int)
242 _nc_db_first(DB * db, DBT * key, DBT * data)
244 int result;
246 memset(key, 0, sizeof(*key));
247 memset(data, 0, sizeof(*data));
248 #if HASHED_DB_API >= 2
249 if ((result = db->cursor(db, NULL, &cursor, 0)) == 0) {
250 result = cursor->c_get(cursor, key, data, DB_FIRST);
252 #else
253 result = db->seq(db, key, data, 0);
254 #endif
255 return result;
259 * Read the next record from the database, ignoring order.
261 * Returns 0 on success.
263 NCURSES_EXPORT(int)
264 _nc_db_next(DB * db, DBT * key, DBT * data)
266 int result;
268 #if HASHED_DB_API >= 2
269 (void) db;
270 if (cursor != 0) {
271 result = cursor->c_get(cursor, key, data, DB_NEXT);
272 } else {
273 result = -1;
275 #else
276 result = db->seq(db, key, data, 0);
277 #endif
278 return result;
282 * Check if a record is a terminfo index record. Index records are those that
283 * contain only an alias pointing to a list of aliases.
285 NCURSES_EXPORT(bool)
286 _nc_db_have_index(DBT * key, DBT * data, char **buffer, int *size)
288 bool result = FALSE;
289 int used = (int) data->size - 1;
290 char *have = (char *) data->data;
292 (void) key;
293 if (*have++ == 2) {
294 result = TRUE;
297 * Update params in any case for consistency with _nc_db_have_data().
299 *buffer = have;
300 *size = used;
301 return result;
305 * Check if a record is the terminfo data record. Ignore index records, e.g.,
306 * those that contain only an alias pointing to a list of aliases.
308 NCURSES_EXPORT(bool)
309 _nc_db_have_data(DBT * key, DBT * data, char **buffer, int *size)
311 bool result = FALSE;
312 int used = (int) data->size - 1;
313 char *have = (char *) data->data;
315 if (*have++ == 0) {
316 if (data->size > key->size
317 && IS_TIC_MAGIC(have)) {
318 result = TRUE;
322 * Update params in any case to make it simple to follow a index record
323 * to the data record.
325 *buffer = have;
326 *size = used;
327 return result;
330 #else
332 extern
333 NCURSES_EXPORT(void)
334 _nc_hashed_db(void);
336 NCURSES_EXPORT(void)
337 _nc_hashed_db(void)
341 #endif /* USE_HASHED_DB */