blob: b59420585eac2ab7489983fa5d3d6cf782bca572 [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
Steve Kondikae271bc2015-11-15 02:50:53 +01002 * Copyright (c) 2006-2011,2013 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05303 * *
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 ****************************************************************************/
28
29/****************************************************************************
Steve Kondikae271bc2015-11-15 02:50:53 +010030 * Author: Thomas E. Dickey 2006-on *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053031 ****************************************************************************/
32
33#include <curses.priv.h>
34#include <tic.h>
35#include <hashed_db.h>
36
37#if USE_HASHED_DB
38
Steve Kondikae271bc2015-11-15 02:50:53 +010039MODULE_ID("$Id: hashed_db.c,v 1.17 2013/12/15 00:33:01 tom Exp $")
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053040
41#if HASHED_DB_API >= 2
42static DBC *cursor;
43#endif
44
Steve Kondikae271bc2015-11-15 02:50:53 +010045typedef struct _myconn {
46 struct _myconn *next;
47 DB *db;
48 char *path;
49 bool modify;
50} MYCONN;
51
52static MYCONN *connections;
53
54static void
55cleanup(void)
56{
57 while (connections != 0) {
58 _nc_db_close(connections->db);
59 }
60}
61
62static DB *
63find_connection(const char *path, bool modify)
64{
65 DB *result = 0;
66 MYCONN *p;
67
68 for (p = connections; p != 0; p = p->next) {
69 if (!strcmp(p->path, path) && p->modify == modify) {
70 result = p->db;
71 break;
72 }
73 }
74
75 return result;
76}
77
78static void
79drop_connection(DB * db)
80{
81 MYCONN *p, *q;
82
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;
92 }
93 }
94}
95
96static void
97make_connection(DB * db, const char *path, bool modify)
98{
99 MYCONN *p = typeCalloc(MYCONN, 1);
100
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);
110 }
111 }
112}
113
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530114/*
115 * Open the database.
116 */
117NCURSES_EXPORT(DB *)
118_nc_db_open(const char *path, bool modify)
119{
120 DB *result = 0;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530121 int code;
122
Steve Kondikae271bc2015-11-15 02:50:53 +0100123 if (connections == 0)
124 atexit(cleanup);
125
126 if ((result = find_connection(path, modify)) == 0) {
127
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;
138 }
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;
148 }
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;
158 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530159#else
Steve Kondikae271bc2015-11-15 02:50:53 +0100160 if ((result = dbopen(path,
161 modify ? (O_CREAT | O_RDWR) : O_RDONLY,
162 0644,
163 DB_HASH,
164 NULL)) == 0) {
165 code = errno;
166 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530167#endif
Steve Kondikae271bc2015-11-15 02:50:53 +0100168 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)));
173 }
174 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530175 return result;
176}
177
178/*
179 * Close the database. Do not attempt to use the 'db' handle after this call.
180 */
181NCURSES_EXPORT(int)
182_nc_db_close(DB * db)
183{
184 int result;
185
Steve Kondikae271bc2015-11-15 02:50:53 +0100186 drop_connection(db);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530187#if HASHED_DB_API >= 2
188 result = db->close(db, 0);
189#else
190 result = db->close(db);
191#endif
192 return result;
193}
194
195/*
196 * Write a record to the database.
197 *
198 * Returns 0 on success.
199 *
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).
202 */
203NCURSES_EXPORT(int)
204_nc_db_put(DB * db, DBT * key, DBT * data)
205{
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;
215}
216
217/*
218 * Read a record from the database.
219 *
220 * Returns 0 on success.
221 */
222NCURSES_EXPORT(int)
223_nc_db_get(DB * db, DBT * key, DBT * data)
224{
225 int result;
226
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;
234}
235
236/*
237 * Read the first record from the database, ignoring order.
238 *
239 * Returns 0 on success.
240 */
241NCURSES_EXPORT(int)
242_nc_db_first(DB * db, DBT * key, DBT * data)
243{
244 int result;
245
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);
251 }
252#else
253 result = db->seq(db, key, data, 0);
254#endif
255 return result;
256}
257
258/*
259 * Read the next record from the database, ignoring order.
260 *
261 * Returns 0 on success.
262 */
263NCURSES_EXPORT(int)
264_nc_db_next(DB * db, DBT * key, DBT * data)
265{
266 int result;
267
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;
274 }
275#else
276 result = db->seq(db, key, data, 0);
277#endif
278 return result;
279}
280
281/*
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.
284 */
285NCURSES_EXPORT(bool)
286_nc_db_have_index(DBT * key, DBT * data, char **buffer, int *size)
287{
288 bool result = FALSE;
Steve Kondikae271bc2015-11-15 02:50:53 +0100289 int used = (int) data->size - 1;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530290 char *have = (char *) data->data;
291
292 (void) key;
293 if (*have++ == 2) {
294 result = TRUE;
295 }
296 /*
297 * Update params in any case for consistency with _nc_db_have_data().
298 */
299 *buffer = have;
300 *size = used;
301 return result;
302}
303
304/*
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.
307 */
308NCURSES_EXPORT(bool)
309_nc_db_have_data(DBT * key, DBT * data, char **buffer, int *size)
310{
311 bool result = FALSE;
Steve Kondikae271bc2015-11-15 02:50:53 +0100312 int used = (int) data->size - 1;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530313 char *have = (char *) data->data;
314
315 if (*have++ == 0) {
316 if (data->size > key->size
317 && IS_TIC_MAGIC(have)) {
318 result = TRUE;
319 }
320 }
321 /*
322 * Update params in any case to make it simple to follow a index record
323 * to the data record.
324 */
325 *buffer = have;
326 *size = used;
327 return result;
328}
329
330#else
331
332extern
333NCURSES_EXPORT(void)
334_nc_hashed_db(void);
335
336NCURSES_EXPORT(void)
337_nc_hashed_db(void)
338{
339}
340
341#endif /* USE_HASHED_DB */