0
|
1 /* Database access routines
|
|
2 Copyright (C) 1996, William M. Perry
|
|
3
|
|
4 This file is part of XEmacs.
|
|
5
|
|
6 XEmacs is free software; you can redistribute it and/or modify it
|
|
7 under the terms of the GNU General Public License as published by the
|
|
8 Free Software Foundation; either version 2, or (at your option) any
|
|
9 later version.
|
|
10
|
|
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with XEmacs; see the file COPYING. If not, write to
|
|
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
19 Boston, MA 02111-1307, USA. */
|
|
20
|
|
21 /* Synched up with: Not in FSF. */
|
|
22
|
|
23 /* Written by Bill Perry */
|
185
|
24 /* Substantially rewritten by Martin Buchholz */
|
0
|
25
|
|
26 #include <config.h>
|
|
27 #include "lisp.h"
|
|
28 #include <errno.h>
|
|
29
|
149
|
30 #ifndef HAVE_DATABASE
|
|
31 #error database.c being compiled, but HAVE_DATABASE not defined!
|
|
32 #endif /* HAVE_DATABASE */
|
|
33
|
157
|
34 #include "database.h" /* Our include file */
|
149
|
35
|
0
|
36 #ifdef HAVE_BERKELEY_DB
|
149
|
37 /* Work around Berkeley DB's use of int types which are defined
|
|
38 slightly differently in the not quite yet standard <inttypes.h>.
|
|
39 See db.h for details of why we're resorting to this... */
|
|
40 #ifdef HAVE_INTTYPES_H
|
|
41 #define __BIT_TYPES_DEFINED__
|
|
42 #include <inttypes.h>
|
|
43 typedef uint8_t u_int8_t;
|
|
44 typedef uint16_t u_int16_t;
|
|
45 typedef uint32_t u_int32_t;
|
|
46 #ifdef WE_DONT_NEED_QUADS
|
|
47 typedef uint64_t u_int64_t;
|
|
48 #endif /* WE_DONT_NEED_QUADS */
|
|
49 #endif /* HAVE_INTTYPES_H */
|
|
50 #include DB_H_PATH /* Berkeley db's header file */
|
0
|
51 Lisp_Object Qberkeley_db;
|
|
52 Lisp_Object Qhash;
|
|
53 Lisp_Object Qbtree;
|
|
54 Lisp_Object Qrecno;
|
149
|
55 #endif /* HAVE_BERKELEY_DB */
|
|
56
|
|
57 #ifdef HAVE_DBM
|
|
58 #include <ndbm.h>
|
|
59 Lisp_Object Qdbm;
|
|
60 #endif /* HAVE_DBM */
|
|
61
|
|
62 Lisp_Object Qdatabasep;
|
0
|
63
|
|
64 typedef enum { DB_DBM, DB_BERKELEY, DB_UNKNOWN } XEMACS_DB_TYPE;
|
|
65
|
185
|
66 struct database;
|
|
67 typedef struct database database;
|
0
|
68
|
149
|
69 typedef struct
|
0
|
70 {
|
185
|
71 CONST char * (*get_subtype) (struct database *);
|
|
72 CONST char * (*get_type) (struct database *);
|
|
73 Lisp_Object (*get) (struct database *, Lisp_Object);
|
|
74 int (*put) (struct database *, Lisp_Object, Lisp_Object, Lisp_Object);
|
|
75 int (*rem) (struct database *, Lisp_Object);
|
|
76 void (*map) (struct database *, Lisp_Object);
|
|
77 Lisp_Object (*get_lisp_type) (struct database *);
|
|
78 void (*close) (struct database *);
|
|
79 Lisp_Object (*last_error) (struct database *);
|
0
|
80 } DB_FUNCS;
|
|
81
|
185
|
82 struct database
|
0
|
83 {
|
|
84 struct lcrecord_header header;
|
|
85 Lisp_Object fname;
|
|
86 XEMACS_DB_TYPE type;
|
|
87 int mode;
|
173
|
88 int access_;
|
94
|
89 int dberrno;
|
185
|
90 int live_p;
|
|
91 #ifdef HAVE_DBM
|
|
92 DBM *dbm_handle;
|
|
93 #endif
|
|
94 #ifdef HAVE_BERKELEY_DB
|
|
95 DB *db_handle;
|
|
96 #endif
|
0
|
97 DB_FUNCS *funcs;
|
149
|
98 #ifdef MULE
|
|
99 Lisp_Object coding_system;
|
|
100 #endif
|
0
|
101 };
|
|
102
|
185
|
103 #define XDATABASE(x) XRECORD (x, database, struct database)
|
0
|
104 #define XSETDATABASE(x, p) XSETRECORD (x, p, database)
|
|
105 #define DATABASEP(x) RECORDP (x, database)
|
|
106 #define GC_DATABASEP(x) GC_RECORDP (x, database)
|
|
107 #define CHECK_DATABASE(x) CHECK_RECORD (x, database)
|
185
|
108 #define DATABASE_LIVE_P(x) (x->live_p)
|
0
|
109 static Lisp_Object mark_database (Lisp_Object, void (*) (Lisp_Object));
|
|
110 static void print_database (Lisp_Object, Lisp_Object, int);
|
|
111 static void finalize_database (void *, int);
|
|
112 DEFINE_LRECORD_IMPLEMENTATION ("database", database,
|
|
113 mark_database, print_database,
|
|
114 finalize_database, 0, 0,
|
185
|
115 struct database);
|
0
|
116
|
185
|
117 #define CHECK_LIVE_DATABASE(db) do { \
|
|
118 CHECK_DATABASE(db); \
|
|
119 if (!DATABASE_LIVE_P (XDATABASE(db))) \
|
|
120 signal_simple_error ("Attempting to access closed database", db); \
|
|
121 } while (0)
|
|
122
|
|
123
|
|
124 static struct database *
|
0
|
125 new_database (void)
|
|
126 {
|
185
|
127 struct database *dbase =
|
|
128 alloc_lcrecord_type (struct database, lrecord_database);
|
0
|
129
|
|
130 dbase->fname = Qnil;
|
185
|
131 dbase->live_p = 0;
|
|
132 #ifdef HAVE_BERKELEY_DB
|
0
|
133 dbase->db_handle = NULL;
|
185
|
134 #endif
|
|
135 #ifdef HAVE_DBM
|
|
136 dbase->dbm_handle = NULL;
|
|
137 #endif
|
173
|
138 dbase->access_ = 0;
|
0
|
139 dbase->mode = 0;
|
94
|
140 dbase->dberrno = 0;
|
0
|
141 dbase->type = DB_UNKNOWN;
|
149
|
142 #ifdef MULE
|
|
143 dbase->coding_system = Fget_coding_system (Qbinary);
|
|
144 #endif
|
173
|
145 return dbase;
|
0
|
146 }
|
|
147
|
|
148 static Lisp_Object
|
|
149 mark_database (Lisp_Object obj, void (*markobj) (Lisp_Object))
|
|
150 {
|
185
|
151 struct database *dbase = XDATABASE (obj);
|
0
|
152
|
|
153 ((markobj) (dbase->fname));
|
173
|
154 return Qnil;
|
0
|
155 }
|
|
156
|
|
157 static void
|
|
158 print_database (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
159 {
|
185
|
160 struct database *dbase = XDATABASE (obj);
|
0
|
161 char buf[200];
|
|
162
|
|
163 if (print_readably)
|
|
164 {
|
|
165 error ("printing unreadable object #<database 0x%x>", dbase->header.uid);
|
|
166 }
|
|
167 else
|
|
168 {
|
185
|
169 sprintf (buf, "#<database \"%s\" (%s/%s/%s) 0x%x>",
|
|
170 XSTRING_DATA (dbase->fname),
|
|
171 dbase->funcs->get_type (dbase),
|
|
172 dbase->funcs->get_subtype (dbase),
|
|
173 (!DATABASE_LIVE_P (dbase) ? "closed" :
|
|
174 (dbase->access_ & O_WRONLY) ? "writeonly" :
|
|
175 (dbase->access_ & O_RDWR) ? "readwrite" : "readonly"),
|
0
|
176 dbase->header.uid);
|
|
177 write_c_string (buf, printcharfun);
|
|
178 }
|
|
179 }
|
|
180
|
|
181 static void
|
|
182 finalize_database (void *header, int for_disksave)
|
|
183 {
|
185
|
184 struct database *db = (struct database *) header;
|
0
|
185
|
|
186 if (for_disksave)
|
|
187 {
|
185
|
188 Lisp_Object obj;
|
|
189 XSETOBJ (obj, Lisp_Type_Record, (void *) db);
|
|
190
|
0
|
191 signal_simple_error
|
|
192 ("Can't dump an emacs containing window system objects", obj);
|
|
193 }
|
|
194 db->funcs->close (db);
|
|
195 }
|
|
196
|
20
|
197 DEFUN ("close-database", Fdatabase_close, 1, 1, 0, /*
|
0
|
198 Close database OBJ.
|
20
|
199 */
|
|
200 (obj))
|
0
|
201 {
|
185
|
202 CHECK_LIVE_DATABASE (obj);
|
|
203 XDATABASE (obj)->funcs->close (XDATABASE (obj));
|
|
204 XDATABASE (obj)->live_p = 0;
|
173
|
205 return Qnil;
|
0
|
206 }
|
|
207
|
20
|
208 DEFUN ("database-type", Fdatabase_type, 1, 1, 0, /*
|
0
|
209 Return the type of database OBJ.
|
20
|
210 */
|
|
211 (obj))
|
0
|
212 {
|
|
213 CHECK_DATABASE (obj);
|
|
214
|
185
|
215 return XDATABASE (obj)->funcs->get_lisp_type (XDATABASE (obj));
|
0
|
216 }
|
|
217
|
20
|
218 DEFUN ("database-subtype", Fdatabase_subtype, 1, 1, 0, /*
|
0
|
219 Return the subtype of database OBJ, if any.
|
20
|
220 */
|
|
221 (obj))
|
0
|
222 {
|
|
223 CHECK_DATABASE (obj);
|
185
|
224
|
|
225 return intern (XDATABASE (obj)->funcs->get_subtype (XDATABASE (obj)));
|
0
|
226 }
|
|
227
|
20
|
228 DEFUN ("database-live-p", Fdatabase_live_p, 1, 1, 0, /*
|
0
|
229 Return t iff OBJ is an active database, else nil.
|
20
|
230 */
|
|
231 (obj))
|
0
|
232 {
|
|
233 CHECK_DATABASE (obj);
|
|
234
|
185
|
235 return DATABASE_LIVE_P (XDATABASE (obj)) ? Qt : Qnil;
|
0
|
236 }
|
|
237
|
20
|
238 DEFUN ("database-file-name", Fdatabase_file_name, 1, 1, 0, /*
|
0
|
239 Return the filename associated with the database OBJ.
|
20
|
240 */
|
|
241 (obj))
|
0
|
242 {
|
|
243 CHECK_DATABASE (obj);
|
185
|
244
|
|
245 return XDATABASE (obj)->fname;
|
0
|
246 }
|
|
247
|
20
|
248 DEFUN ("databasep", Fdatabasep, 1, 1, 0, /*
|
0
|
249 Return t iff OBJ is a database, else nil.
|
20
|
250 */
|
|
251 (obj))
|
0
|
252 {
|
173
|
253 return DATABASEP (obj) ? Qt : Qnil;
|
0
|
254 }
|
|
255
|
|
256 #ifdef HAVE_DBM
|
|
257 static void
|
185
|
258 dbm_map (struct database *db, Lisp_Object func)
|
0
|
259 {
|
|
260 datum keydatum, valdatum;
|
|
261 Lisp_Object key, val;
|
|
262
|
185
|
263 for (keydatum = dbm_firstkey (db->dbm_handle);
|
0
|
264 keydatum.dptr != NULL;
|
185
|
265 keydatum = dbm_nextkey (db->dbm_handle))
|
0
|
266 {
|
185
|
267 valdatum = dbm_fetch (db->dbm_handle, keydatum);
|
0
|
268 key = make_string ((unsigned char *) keydatum.dptr, keydatum.dsize);
|
|
269 val = make_string ((unsigned char *) valdatum.dptr, valdatum.dsize);
|
|
270 call2 (func, key, val);
|
|
271 }
|
|
272 }
|
|
273
|
|
274 static Lisp_Object
|
185
|
275 dbm_get (struct database *db, Lisp_Object key)
|
0
|
276 {
|
|
277 datum keydatum, valdatum;
|
185
|
278
|
14
|
279 keydatum.dptr = (char *) XSTRING_DATA (key);
|
|
280 keydatum.dsize = XSTRING_LENGTH (key);
|
185
|
281 valdatum = dbm_fetch (db->dbm_handle, keydatum);
|
0
|
282
|
|
283 return (valdatum.dptr
|
|
284 ? make_string ((unsigned char *) valdatum.dptr, valdatum.dsize)
|
|
285 : Qnil);
|
|
286 }
|
|
287
|
|
288 static int
|
185
|
289 dbm_put (struct database *db,
|
|
290 Lisp_Object key, Lisp_Object val, Lisp_Object replace)
|
0
|
291 {
|
|
292 datum keydatum, valdatum;
|
185
|
293
|
14
|
294 valdatum.dptr = (char *) XSTRING_DATA (val);
|
|
295 valdatum.dsize = XSTRING_LENGTH (val);
|
|
296 keydatum.dptr = (char *) XSTRING_DATA (key);
|
|
297 keydatum.dsize = XSTRING_LENGTH (key);
|
0
|
298
|
185
|
299 return !dbm_store (db->dbm_handle, keydatum, valdatum,
|
|
300 NILP (replace) ? DBM_INSERT : DBM_REPLACE);
|
0
|
301 }
|
|
302
|
|
303 static int
|
185
|
304 dbm_remove (struct database *db, Lisp_Object key)
|
0
|
305 {
|
|
306 datum keydatum;
|
185
|
307
|
14
|
308 keydatum.dptr = (char *) XSTRING_DATA (key);
|
|
309 keydatum.dsize = XSTRING_LENGTH (key);
|
185
|
310
|
|
311 return dbm_delete (db->dbm_handle, keydatum);
|
0
|
312 }
|
|
313
|
|
314 static Lisp_Object
|
185
|
315 dbm_lisp_type (struct database *db)
|
0
|
316 {
|
173
|
317 return Qdbm;
|
0
|
318 }
|
|
319
|
|
320 static CONST char *
|
185
|
321 dbm_type (struct database *db)
|
0
|
322 {
|
173
|
323 return "dbm";
|
0
|
324 }
|
|
325
|
|
326 static CONST char *
|
185
|
327 dbm_subtype (struct database *db)
|
0
|
328 {
|
173
|
329 return "nil";
|
0
|
330 }
|
|
331
|
|
332 static Lisp_Object
|
185
|
333 dbm_lasterr (struct database *dbp)
|
0
|
334 {
|
151
|
335 return lisp_strerror (dbp->dberrno);
|
0
|
336 }
|
|
337
|
|
338 static void
|
185
|
339 dbm_closeit (struct database *db)
|
0
|
340 {
|
185
|
341 if (db->dbm_handle)
|
|
342 {
|
|
343 dbm_close (db->dbm_handle);
|
|
344 db->dbm_handle = NULL;
|
|
345 }
|
0
|
346 }
|
|
347
|
|
348 static DB_FUNCS ndbm_func_block =
|
|
349 {
|
|
350 dbm_subtype,
|
|
351 dbm_type,
|
|
352 dbm_get,
|
|
353 dbm_put,
|
|
354 dbm_remove,
|
|
355 dbm_map,
|
|
356 dbm_lisp_type,
|
|
357 dbm_closeit,
|
|
358 dbm_lasterr
|
|
359 };
|
185
|
360 #endif /* HAVE_DBM */
|
0
|
361
|
|
362 #ifdef HAVE_BERKELEY_DB
|
|
363 static Lisp_Object
|
185
|
364 berkdb_lisp_type (struct database *db)
|
0
|
365 {
|
173
|
366 return Qberkeley_db;
|
0
|
367 }
|
|
368
|
|
369 static CONST char *
|
185
|
370 berkdb_type (struct database *db)
|
0
|
371 {
|
173
|
372 return "berkeley";
|
0
|
373 }
|
|
374
|
|
375 static CONST char *
|
185
|
376 berkdb_subtype (struct database *db)
|
0
|
377 {
|
185
|
378 if (!db->db_handle)
|
173
|
379 return "nil";
|
0
|
380
|
185
|
381 switch (db->db_handle->type)
|
|
382 {
|
|
383 case DB_BTREE: return "btree";
|
|
384 case DB_HASH: return "hash";
|
|
385 case DB_RECNO: return "recno";
|
|
386 default: return "unknown";
|
|
387 }
|
0
|
388 }
|
|
389
|
|
390 static Lisp_Object
|
185
|
391 berkdb_lasterr (struct database *dbp)
|
0
|
392 {
|
151
|
393 return lisp_strerror (dbp->dberrno);
|
0
|
394 }
|
|
395
|
|
396 static Lisp_Object
|
185
|
397 berkdb_get (struct database *db, Lisp_Object key)
|
0
|
398 {
|
185
|
399 /* #### Needs mule-izing */
|
0
|
400 DBT keydatum, valdatum;
|
|
401 int status = 0;
|
|
402
|
14
|
403 keydatum.data = XSTRING_DATA (key);
|
|
404 keydatum.size = XSTRING_LENGTH (key);
|
185
|
405
|
|
406 status = db->db_handle->get (db->db_handle, &keydatum, &valdatum, 0);
|
0
|
407
|
|
408 if (!status)
|
185
|
409 return make_string ((Bufbyte *) valdatum.data, valdatum.size);
|
0
|
410
|
94
|
411 db->dberrno = (status == 1) ? -1 : errno;
|
173
|
412 return Qnil;
|
0
|
413 }
|
|
414
|
|
415 static int
|
185
|
416 berkdb_put (struct database *db,
|
0
|
417 Lisp_Object key,
|
|
418 Lisp_Object val,
|
|
419 Lisp_Object replace)
|
|
420 {
|
|
421 DBT keydatum, valdatum;
|
|
422 int status = 0;
|
|
423
|
14
|
424 keydatum.data = XSTRING_DATA (key);
|
|
425 keydatum.size = XSTRING_LENGTH (key);
|
|
426 valdatum.data = XSTRING_DATA (val);
|
|
427 valdatum.size = XSTRING_LENGTH (val);
|
185
|
428 status = db->db_handle->put (db->db_handle, &keydatum, &valdatum,
|
|
429 NILP (replace) ? R_NOOVERWRITE : 0);
|
94
|
430 db->dberrno = (status == 1) ? -1 : errno;
|
0
|
431 return status;
|
|
432 }
|
|
433
|
|
434 static int
|
185
|
435 berkdb_remove (struct database *db, Lisp_Object key)
|
0
|
436 {
|
|
437 DBT keydatum;
|
|
438 int status;
|
|
439
|
14
|
440 keydatum.data = XSTRING_DATA (key);
|
|
441 keydatum.size = XSTRING_LENGTH (key);
|
185
|
442
|
|
443 status = db->db_handle->del (db->db_handle, &keydatum, 0);
|
0
|
444 if (!status)
|
|
445 return 0;
|
185
|
446
|
94
|
447 db->dberrno = (status == 1) ? -1 : errno;
|
0
|
448 return 1;
|
|
449 }
|
|
450
|
|
451 static void
|
185
|
452 berkdb_map (struct database *db, Lisp_Object func)
|
0
|
453 {
|
|
454 DBT keydatum, valdatum;
|
|
455 Lisp_Object key, val;
|
185
|
456 DB *dbp = db->db_handle;
|
0
|
457 int status;
|
|
458
|
|
459 for (status = dbp->seq (dbp, &keydatum, &valdatum, R_FIRST);
|
|
460 status == 0;
|
|
461 status = dbp->seq (dbp, &keydatum, &valdatum, R_NEXT))
|
|
462 {
|
185
|
463 /* ### Needs mule-izing */
|
|
464 key = make_string ((Bufbyte *) keydatum.data, keydatum.size);
|
|
465 val = make_string ((Bufbyte *) valdatum.data, valdatum.size);
|
0
|
466 call2 (func, key, val);
|
|
467 }
|
|
468 }
|
|
469
|
|
470 static void
|
185
|
471 berkdb_close (struct database *db)
|
0
|
472 {
|
185
|
473 if (db->db_handle)
|
0
|
474 {
|
185
|
475 db->db_handle->sync (db->db_handle, 0);
|
|
476 db->db_handle->close (db->db_handle);
|
|
477 db->db_handle = NULL;
|
0
|
478 }
|
|
479 }
|
|
480
|
|
481 static DB_FUNCS berk_func_block =
|
|
482 {
|
|
483 berkdb_subtype,
|
|
484 berkdb_type,
|
|
485 berkdb_get,
|
|
486 berkdb_put,
|
|
487 berkdb_remove,
|
|
488 berkdb_map,
|
|
489 berkdb_lisp_type,
|
|
490 berkdb_close,
|
|
491 berkdb_lasterr
|
|
492 };
|
185
|
493 #endif /* HAVE_BERKELEY_DB */
|
0
|
494
|
20
|
495 DEFUN ("database-last-error", Fdatabase_error, 0, 1, 0, /*
|
0
|
496 Return the last error associated with database OBJ.
|
20
|
497 */
|
|
498 (obj))
|
0
|
499 {
|
|
500 if (NILP (obj))
|
151
|
501 return lisp_strerror (errno);
|
185
|
502
|
0
|
503 CHECK_DATABASE (obj);
|
185
|
504
|
|
505 return XDATABASE (obj)->funcs->last_error (XDATABASE (obj));
|
0
|
506 }
|
|
507
|
20
|
508 DEFUN ("open-database", Fmake_database, 1, 5, 0, /*
|
0
|
509 Open database FILE, using database method TYPE and SUBTYPE, with
|
|
510 access rights ACCESS and permissions MODE. ACCESS can be any
|
|
511 combination of 'r' 'w' and '+', for read, write, and creation flags.
|
20
|
512 */
|
173
|
513 (file, type, subtype, access_, mode))
|
0
|
514 {
|
187
|
515 /* This function can GC */
|
0
|
516 Lisp_Object retval = Qnil;
|
|
517 int modemask;
|
|
518 int accessmask = 0;
|
185
|
519 struct database *dbase = NULL;
|
|
520 char *filename;
|
187
|
521 struct gcpro gcpro1, gcpro2;
|
0
|
522
|
70
|
523 CHECK_STRING (file);
|
187
|
524 GCPRO2 (file, access_);
|
|
525 file = Fexpand_file_name (file, Qnil);
|
|
526 UNGCPRO;
|
185
|
527 filename = (char *) XSTRING_DATA (file);
|
0
|
528
|
173
|
529 if (NILP (access_))
|
0
|
530 {
|
|
531 accessmask = O_RDWR | O_CREAT;
|
|
532 }
|
|
533 else
|
|
534 {
|
|
535 char *acc;
|
173
|
536 CHECK_STRING (access_);
|
|
537 acc = (char *) XSTRING_DATA (access_);
|
185
|
538
|
0
|
539 if (strchr (acc, '+'))
|
|
540 accessmask |= O_CREAT;
|
185
|
541
|
0
|
542 if (strchr (acc, 'r') && strchr (acc, 'w'))
|
|
543 accessmask |= O_RDWR;
|
|
544 else if (strchr (acc, 'w'))
|
|
545 accessmask |= O_WRONLY;
|
|
546 else
|
|
547 accessmask |= O_RDONLY;
|
|
548 }
|
|
549
|
|
550 if (NILP (mode))
|
|
551 {
|
185
|
552 modemask = 0755; /* rwxr-xr-x */
|
0
|
553 }
|
|
554 else
|
|
555 {
|
|
556 CHECK_INT (mode);
|
|
557 modemask = XINT (mode);
|
|
558 }
|
|
559
|
|
560 #ifdef HAVE_DBM
|
|
561 if (NILP (type) || EQ (type, Qdbm))
|
|
562 {
|
185
|
563 DBM *dbm = dbm_open (filename, accessmask, modemask);
|
|
564 if (!dbm)
|
|
565 return Qnil;
|
|
566
|
|
567 dbase = new_database ();
|
|
568 dbase->dbm_handle = dbm;
|
|
569 dbase->type = DB_DBM;
|
|
570 dbase->funcs = &ndbm_func_block;
|
0
|
571 goto db_done;
|
|
572 }
|
185
|
573 #endif /* HAVE_DBM */
|
0
|
574
|
|
575 #ifdef HAVE_BERKELEY_DB
|
|
576 if (NILP (type) || EQ (type, Qberkeley_db))
|
|
577 {
|
185
|
578 DBTYPE real_subtype;
|
|
579 DB *db;
|
0
|
580
|
185
|
581 if (EQ (subtype, Qhash) || NILP (subtype))
|
|
582 real_subtype = DB_HASH;
|
|
583 else if (EQ (subtype, Qbtree))
|
|
584 real_subtype = DB_BTREE;
|
|
585 else if (EQ (subtype, Qrecno))
|
|
586 real_subtype = DB_RECNO;
|
|
587 else
|
|
588 signal_simple_error ("Unsupported subtype", subtype);
|
|
589
|
|
590 db = dbopen (filename, accessmask, modemask, real_subtype, NULL);
|
|
591 if (!db)
|
|
592 return Qnil;
|
|
593
|
|
594 dbase = new_database ();
|
|
595 dbase->db_handle = db;
|
|
596 dbase->type = DB_BERKELEY;
|
|
597 dbase->funcs = &berk_func_block;
|
0
|
598 goto db_done;
|
|
599 }
|
185
|
600 #endif /* HAVE_BERKELEY_DB */
|
|
601
|
0
|
602 signal_simple_error ("Unsupported database type", type);
|
173
|
603 return Qnil;
|
0
|
604
|
|
605 db_done:
|
185
|
606 dbase->live_p = 1;
|
0
|
607 dbase->fname = file;
|
|
608 dbase->mode = modemask;
|
173
|
609 dbase->access_ = accessmask;
|
0
|
610 XSETDATABASE (retval, dbase);
|
|
611
|
173
|
612 return retval;
|
0
|
613 }
|
|
614
|
20
|
615 DEFUN ("put-database", Fputdatabase, 3, 4, 0, /*
|
185
|
616 Store KEY and VAL in DATABASE. If optional fourth arg REPLACE is
|
0
|
617 non-nil, replace any existing entry in the database.
|
20
|
618 */
|
|
619 (key, val, dbase, replace))
|
0
|
620 {
|
185
|
621 CHECK_LIVE_DATABASE (dbase);
|
0
|
622 CHECK_STRING (key);
|
177
|
623 CHECK_STRING (val);
|
0
|
624
|
185
|
625 {
|
|
626 int status =
|
|
627 XDATABASE (dbase)->funcs->put (XDATABASE (dbase), key, val, replace);
|
|
628 return status ? Qt : Qnil;
|
|
629 }
|
0
|
630 }
|
|
631
|
20
|
632 DEFUN ("remove-database", Fremdatabase, 2, 2, 0, /*
|
0
|
633 Remove KEY from DATABASE.
|
20
|
634 */
|
|
635 (key, dbase))
|
0
|
636 {
|
185
|
637 CHECK_LIVE_DATABASE (dbase);
|
0
|
638 CHECK_STRING (key);
|
|
639
|
185
|
640 return XDATABASE (dbase)->funcs->rem (XDATABASE (dbase), key) ? Qt : Qnil;
|
0
|
641 }
|
185
|
642
|
20
|
643 DEFUN ("get-database", Fgetdatabase, 2, 3, 0, /*
|
0
|
644 Find value for KEY in DATABASE.
|
|
645 If there is no corresponding value, return DEFAULT (defaults to nil).
|
20
|
646 */
|
173
|
647 (key, dbase, default_))
|
0
|
648 {
|
|
649
|
185
|
650 CHECK_LIVE_DATABASE (dbase);
|
0
|
651 CHECK_STRING (key);
|
|
652
|
185
|
653 {
|
|
654 Lisp_Object retval =
|
|
655 XDATABASE (dbase)->funcs->get (XDATABASE (dbase), key);
|
|
656 return NILP (retval) ? default_ : retval;
|
|
657 }
|
0
|
658 }
|
|
659
|
20
|
660 DEFUN ("map-database", Fmapdatabase, 2, 2, 0, /*
|
0
|
661 Map FUNCTION over entries in DATABASE, calling it with two args,
|
|
662 each key and value in the database.
|
20
|
663 */
|
|
664 (function, dbase))
|
0
|
665 {
|
185
|
666 CHECK_LIVE_DATABASE (dbase);
|
0
|
667
|
185
|
668 XDATABASE (dbase)->funcs->map (XDATABASE (dbase), function);
|
|
669
|
0
|
670 return Qnil;
|
|
671 }
|
|
672
|
|
673 void
|
|
674 syms_of_dbm (void)
|
|
675 {
|
|
676 defsymbol (&Qdatabasep, "databasep");
|
|
677 #ifdef HAVE_DBM
|
|
678 defsymbol (&Qdbm, "dbm");
|
|
679 #endif
|
|
680 #ifdef HAVE_BERKELEY_DB
|
|
681 defsymbol (&Qberkeley_db, "berkeley-db");
|
|
682 defsymbol (&Qhash, "hash");
|
|
683 defsymbol (&Qbtree, "btree");
|
|
684 defsymbol (&Qrecno, "recno");
|
|
685 #endif
|
|
686
|
20
|
687 DEFSUBR (Fmake_database);
|
|
688 DEFSUBR (Fdatabasep);
|
|
689 DEFSUBR (Fmapdatabase);
|
|
690 DEFSUBR (Fputdatabase);
|
|
691 DEFSUBR (Fgetdatabase);
|
|
692 DEFSUBR (Fremdatabase);
|
|
693 DEFSUBR (Fdatabase_type);
|
|
694 DEFSUBR (Fdatabase_subtype);
|
|
695 DEFSUBR (Fdatabase_error);
|
|
696 DEFSUBR (Fdatabase_live_p);
|
|
697 DEFSUBR (Fdatabase_file_name);
|
|
698 DEFSUBR (Fdatabase_close);
|
0
|
699 }
|
|
700
|
|
701 void
|
|
702 vars_of_dbm (void)
|
|
703 {
|
|
704 #ifdef HAVE_DBM
|
|
705 Fprovide (Qdbm);
|
|
706 #endif
|
|
707 #ifdef HAVE_BERKELEY_DB
|
|
708 Fprovide (Qberkeley_db);
|
|
709 #endif
|
|
710 }
|