1 |
|
|
/* $OpenBSD: hashed_db.c,v 1.1 2010/01/12 23:22:06 nicm Exp $ */ |
2 |
|
|
|
3 |
|
|
/**************************************************************************** |
4 |
|
|
* Copyright (c) 2006 Free Software Foundation, Inc. * |
5 |
|
|
* * |
6 |
|
|
* Permission is hereby granted, free of charge, to any person obtaining a * |
7 |
|
|
* copy of this software and associated documentation files (the * |
8 |
|
|
* "Software"), to deal in the Software without restriction, including * |
9 |
|
|
* without limitation the rights to use, copy, modify, merge, publish, * |
10 |
|
|
* distribute, distribute with modifications, sublicense, and/or sell * |
11 |
|
|
* copies of the Software, and to permit persons to whom the Software is * |
12 |
|
|
* furnished to do so, subject to the following conditions: * |
13 |
|
|
* * |
14 |
|
|
* The above copyright notice and this permission notice shall be included * |
15 |
|
|
* in all copies or substantial portions of the Software. * |
16 |
|
|
* * |
17 |
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * |
18 |
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * |
19 |
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * |
20 |
|
|
* IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * |
21 |
|
|
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * |
22 |
|
|
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * |
23 |
|
|
* THE USE OR OTHER DEALINGS IN THE SOFTWARE. * |
24 |
|
|
* * |
25 |
|
|
* Except as contained in this notice, the name(s) of the above copyright * |
26 |
|
|
* holders shall not be used in advertising or otherwise to promote the * |
27 |
|
|
* sale, use or other dealings in this Software without prior written * |
28 |
|
|
* authorization. * |
29 |
|
|
****************************************************************************/ |
30 |
|
|
|
31 |
|
|
/**************************************************************************** |
32 |
|
|
* Author: Thomas E. Dickey 2006 * |
33 |
|
|
****************************************************************************/ |
34 |
|
|
|
35 |
|
|
#include <curses.priv.h> |
36 |
|
|
#include <tic.h> |
37 |
|
|
#include <hashed_db.h> |
38 |
|
|
|
39 |
|
|
#if USE_HASHED_DB |
40 |
|
|
|
41 |
|
|
MODULE_ID("$Id: hashed_db.c,v 1.1 2010/01/12 23:22:06 nicm Exp $") |
42 |
|
|
|
43 |
|
|
#if HASHED_DB_API >= 2 |
44 |
|
|
static DBC *cursor; |
45 |
|
|
#endif |
46 |
|
|
|
47 |
|
|
/* |
48 |
|
|
* Open the database. |
49 |
|
|
*/ |
50 |
|
|
NCURSES_EXPORT(DB *) |
51 |
|
|
_nc_db_open(const char *path, bool modify) |
52 |
|
|
{ |
53 |
|
|
DB *result = 0; |
54 |
|
|
|
55 |
|
|
#if HASHED_DB_API >= 4 |
56 |
|
|
db_create(&result, NULL, 0); |
57 |
|
|
result->open(result, |
58 |
|
|
NULL, |
59 |
|
|
path, |
60 |
|
|
NULL, |
61 |
|
|
DB_HASH, |
62 |
|
|
modify ? DB_CREATE : DB_RDONLY, |
63 |
|
|
0644); |
64 |
|
|
#elif HASHED_DB_API >= 3 |
65 |
|
|
db_create(&result, NULL, 0); |
66 |
|
|
result->open(result, |
67 |
|
|
path, |
68 |
|
|
NULL, |
69 |
|
|
DB_HASH, |
70 |
|
|
modify ? DB_CREATE : DB_RDONLY, |
71 |
|
|
0644); |
72 |
|
|
#elif HASHED_DB_API >= 2 |
73 |
|
|
int code; |
74 |
|
|
|
75 |
|
|
if ((code = db_open(path, |
76 |
|
|
DB_HASH, |
77 |
|
|
modify ? DB_CREATE : DB_RDONLY, |
78 |
|
|
0644, |
79 |
|
|
(DB_ENV *) 0, |
80 |
|
|
(DB_INFO *) 0, |
81 |
|
|
&result)) != 0) { |
82 |
|
|
T(("cannot open %s: %s", path, strerror(code))); |
83 |
|
|
result = 0; |
84 |
|
|
} else { |
85 |
|
|
T(("opened %s", path)); |
86 |
|
|
} |
87 |
|
|
#else |
88 |
|
|
result = dbopen(path, |
89 |
|
|
modify ? (O_CREAT | O_RDWR) : O_RDONLY, |
90 |
|
|
0644, |
91 |
|
|
DB_HASH, |
92 |
|
|
NULL); |
93 |
|
|
if (result != 0) { |
94 |
|
|
T(("opened %s", path)); |
95 |
|
|
} |
96 |
|
|
#endif |
97 |
|
|
return result; |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
/* |
101 |
|
|
* Close the database. Do not attempt to use the 'db' handle after this call. |
102 |
|
|
*/ |
103 |
|
|
NCURSES_EXPORT(int) |
104 |
|
|
_nc_db_close(DB * db) |
105 |
|
|
{ |
106 |
|
|
int result; |
107 |
|
|
|
108 |
|
|
#if HASHED_DB_API >= 2 |
109 |
|
|
result = db->close(db, 0); |
110 |
|
|
#else |
111 |
|
|
result = db->close(db); |
112 |
|
|
#endif |
113 |
|
|
return result; |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
/* |
117 |
|
|
* Write a record to the database. |
118 |
|
|
* |
119 |
|
|
* Returns 0 on success. |
120 |
|
|
* |
121 |
|
|
* FIXME: the FreeBSD cap_mkdb program assumes the database could have |
122 |
|
|
* duplicates. There appears to be no good reason for that (review/fix). |
123 |
|
|
*/ |
124 |
|
|
NCURSES_EXPORT(int) |
125 |
|
|
_nc_db_put(DB * db, DBT * key, DBT * data) |
126 |
|
|
{ |
127 |
|
|
int result; |
128 |
|
|
#if HASHED_DB_API >= 2 |
129 |
|
|
/* remove any pre-existing value, since we do not want duplicates */ |
130 |
|
|
(void) db->del(db, NULL, key, 0); |
131 |
|
|
result = db->put(db, NULL, key, data, DB_NOOVERWRITE); |
132 |
|
|
#else |
133 |
|
|
result = db->put(db, key, data, R_NOOVERWRITE); |
134 |
|
|
#endif |
135 |
|
|
return result; |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
/* |
139 |
|
|
* Read a record from the database. |
140 |
|
|
* |
141 |
|
|
* Returns 0 on success. |
142 |
|
|
*/ |
143 |
|
|
NCURSES_EXPORT(int) |
144 |
|
|
_nc_db_get(DB * db, DBT * key, DBT * data) |
145 |
|
|
{ |
146 |
|
|
int result; |
147 |
|
|
|
148 |
|
|
memset(data, 0, sizeof(*data)); |
149 |
|
|
#if HASHED_DB_API >= 2 |
150 |
|
|
result = db->get(db, NULL, key, data, 0); |
151 |
|
|
#else |
152 |
|
|
result = db->get(db, key, data, 0); |
153 |
|
|
#endif |
154 |
|
|
return result; |
155 |
|
|
} |
156 |
|
|
|
157 |
|
|
/* |
158 |
|
|
* Read the first record from the database, ignoring order. |
159 |
|
|
* |
160 |
|
|
* Returns 0 on success. |
161 |
|
|
*/ |
162 |
|
|
NCURSES_EXPORT(int) |
163 |
|
|
_nc_db_first(DB * db, DBT * key, DBT * data) |
164 |
|
|
{ |
165 |
|
|
int result; |
166 |
|
|
|
167 |
|
|
memset(key, 0, sizeof(*key)); |
168 |
|
|
memset(data, 0, sizeof(*data)); |
169 |
|
|
#if HASHED_DB_API >= 2 |
170 |
|
|
if ((result = db->cursor(db, NULL, &cursor, 0)) == 0) { |
171 |
|
|
result = cursor->c_get(cursor, key, data, DB_FIRST); |
172 |
|
|
} |
173 |
|
|
#else |
174 |
|
|
result = db->seq(db, key, data, 0); |
175 |
|
|
#endif |
176 |
|
|
return result; |
177 |
|
|
} |
178 |
|
|
|
179 |
|
|
/* |
180 |
|
|
* Read the next record from the database, ignoring order. |
181 |
|
|
* |
182 |
|
|
* Returns 0 on success. |
183 |
|
|
*/ |
184 |
|
|
NCURSES_EXPORT(int) |
185 |
|
|
_nc_db_next(DB * db, DBT * key, DBT * data) |
186 |
|
|
{ |
187 |
|
|
int result; |
188 |
|
|
|
189 |
|
|
#if HASHED_DB_API >= 2 |
190 |
|
|
(void) db; |
191 |
|
|
if (cursor != 0) { |
192 |
|
|
result = cursor->c_get(cursor, key, data, DB_NEXT); |
193 |
|
|
} else { |
194 |
|
|
result = -1; |
195 |
|
|
} |
196 |
|
|
#else |
197 |
|
|
result = db->seq(db, key, data, 0); |
198 |
|
|
#endif |
199 |
|
|
return result; |
200 |
|
|
} |
201 |
|
|
|
202 |
|
|
/* |
203 |
|
|
* Check if a record is a terminfo index record. Index records are those that |
204 |
|
|
* contain only an alias pointing to a list of aliases. |
205 |
|
|
*/ |
206 |
|
|
NCURSES_EXPORT(bool) |
207 |
|
|
_nc_db_have_index(DBT * key, DBT * data, char **buffer, int *size) |
208 |
|
|
{ |
209 |
|
|
bool result = FALSE; |
210 |
|
|
int used = data->size - 1; |
211 |
|
|
char *have = (char *) data->data; |
212 |
|
|
|
213 |
|
|
(void) key; |
214 |
|
|
if (*have++ == 2) { |
215 |
|
|
result = TRUE; |
216 |
|
|
} |
217 |
|
|
/* |
218 |
|
|
* Update params in any case for consistency with _nc_db_have_data(). |
219 |
|
|
*/ |
220 |
|
|
*buffer = have; |
221 |
|
|
*size = used; |
222 |
|
|
return result; |
223 |
|
|
} |
224 |
|
|
|
225 |
|
|
/* |
226 |
|
|
* Check if a record is the terminfo data record. Ignore index records, e.g., |
227 |
|
|
* those that contain only an alias pointing to a list of aliases. |
228 |
|
|
*/ |
229 |
|
|
NCURSES_EXPORT(bool) |
230 |
|
|
_nc_db_have_data(DBT * key, DBT * data, char **buffer, int *size) |
231 |
|
|
{ |
232 |
|
|
bool result = FALSE; |
233 |
|
|
int used = data->size - 1; |
234 |
|
|
char *have = (char *) data->data; |
235 |
|
|
|
236 |
|
|
if (*have++ == 0) { |
237 |
|
|
if (data->size > key->size |
238 |
|
|
&& IS_TIC_MAGIC(have)) { |
239 |
|
|
result = TRUE; |
240 |
|
|
} |
241 |
|
|
} |
242 |
|
|
/* |
243 |
|
|
* Update params in any case to make it simple to follow a index record |
244 |
|
|
* to the data record. |
245 |
|
|
*/ |
246 |
|
|
*buffer = have; |
247 |
|
|
*size = used; |
248 |
|
|
return result; |
249 |
|
|
} |
250 |
|
|
|
251 |
|
|
#else |
252 |
|
|
|
253 |
|
|
extern |
254 |
|
|
NCURSES_EXPORT(void) |
255 |
|
|
_nc_hashed_db(void); |
256 |
|
|
|
257 |
|
|
NCURSES_EXPORT(void) |
258 |
|
|
_nc_hashed_db(void) |
259 |
|
|
{ |
260 |
|
|
} |
261 |
|
|
|
262 |
|
|
#endif /* USE_HASHED_DB */ |