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