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