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;
|
20
|
74 int dberrno;
|
0
|
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;
|
20
|
103 dbase->dberrno = 0;
|
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>",
|
14
|
141 XSTRING_DATA (dbase->fname), type, subtype, perms,
|
0
|
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
|
20
|
163 DEFUN ("close-database", Fdatabase_close, 1, 1, 0, /*
|
0
|
164 Close database OBJ.
|
20
|
165 */
|
|
166 (obj))
|
0
|
167 {
|
|
168 struct database_struct *db;
|
|
169 CHECK_DATABASE (obj);
|
|
170 db = XDATABASE (obj);
|
|
171
|
|
172 if (DATABASE_LIVE_P (db))
|
|
173 db->funcs->close (db);
|
|
174 else
|
|
175 signal_simple_error ("Attempting to access closed database", obj);
|
|
176
|
|
177 return (Qnil);
|
|
178 }
|
|
179
|
20
|
180 DEFUN ("database-type", Fdatabase_type, 1, 1, 0, /*
|
0
|
181 Return the type of database OBJ.
|
20
|
182 */
|
|
183 (obj))
|
0
|
184 {
|
|
185 struct database_struct *db;
|
|
186 CHECK_DATABASE (obj);
|
|
187 db = XDATABASE (obj);
|
|
188
|
|
189 return db->funcs->get_lisp_type (db);
|
|
190 }
|
|
191
|
20
|
192 DEFUN ("database-subtype", Fdatabase_subtype, 1, 1, 0, /*
|
0
|
193 Return the subtype of database OBJ, if any.
|
20
|
194 */
|
|
195 (obj))
|
0
|
196 {
|
|
197 struct database_struct *db;
|
|
198
|
|
199 CHECK_DATABASE (obj);
|
|
200 db = XDATABASE (obj);
|
|
201
|
|
202 return (intern (db->funcs->get_subtype (db)));
|
|
203 }
|
|
204
|
20
|
205 DEFUN ("database-live-p", Fdatabase_live_p, 1, 1, 0, /*
|
0
|
206 Return t iff OBJ is an active database, else nil.
|
20
|
207 */
|
|
208 (obj))
|
0
|
209 {
|
|
210 struct database_struct *db;
|
|
211 CHECK_DATABASE (obj);
|
|
212 db = XDATABASE (obj);
|
|
213
|
|
214 return (DATABASE_LIVE_P (db) ? Qt : Qnil);
|
|
215 }
|
|
216
|
20
|
217 DEFUN ("database-file-name", Fdatabase_file_name, 1, 1, 0, /*
|
0
|
218 Return the filename associated with the database OBJ.
|
20
|
219 */
|
|
220 (obj))
|
0
|
221 {
|
|
222 struct database_struct *db;
|
|
223 CHECK_DATABASE (obj);
|
|
224 db = XDATABASE (obj);
|
|
225 return (db->fname);
|
|
226 }
|
|
227
|
20
|
228 DEFUN ("databasep", Fdatabasep, 1, 1, 0, /*
|
0
|
229 Return t iff OBJ is a database, else nil.
|
20
|
230 */
|
|
231 (obj))
|
0
|
232 {
|
|
233 return ((DATABASEP (obj)) ? Qt : Qnil);
|
|
234 }
|
|
235
|
|
236 #ifdef HAVE_DBM
|
|
237 static void
|
|
238 dbm_map (struct database_struct *db, Lisp_Object func)
|
|
239 {
|
|
240 datum keydatum, valdatum;
|
|
241 DBM *handle = (DBM *)db->db_handle;
|
|
242 Lisp_Object key, val;
|
|
243
|
|
244 for (keydatum = dbm_firstkey (handle);
|
|
245 keydatum.dptr != NULL;
|
|
246 keydatum = dbm_nextkey (handle))
|
|
247 {
|
|
248 valdatum = dbm_fetch (handle, keydatum);
|
|
249 key = make_string ((unsigned char *) keydatum.dptr, keydatum.dsize);
|
|
250 val = make_string ((unsigned char *) valdatum.dptr, valdatum.dsize);
|
|
251 call2 (func, key, val);
|
|
252 }
|
|
253 }
|
|
254
|
|
255 static Lisp_Object
|
|
256 dbm_get (struct database_struct *db, Lisp_Object key)
|
|
257 {
|
|
258 datum keydatum, valdatum;
|
|
259 DBM *handle = (DBM *)db->db_handle;
|
14
|
260 keydatum.dptr = (char *) XSTRING_DATA (key);
|
|
261 keydatum.dsize = XSTRING_LENGTH (key);
|
0
|
262 valdatum = dbm_fetch (handle, keydatum);
|
|
263
|
|
264 return (valdatum.dptr
|
|
265 ? make_string ((unsigned char *) valdatum.dptr, valdatum.dsize)
|
|
266 : Qnil);
|
|
267 }
|
|
268
|
|
269 static int
|
|
270 dbm_put (struct database_struct *db,
|
|
271 Lisp_Object key,
|
|
272 Lisp_Object val,
|
|
273 Lisp_Object replace)
|
|
274 {
|
|
275 DBM *handle = (DBM *)db->db_handle;
|
|
276 datum keydatum, valdatum;
|
|
277
|
14
|
278 valdatum.dptr = (char *) XSTRING_DATA (val);
|
|
279 valdatum.dsize = XSTRING_LENGTH (val);
|
|
280 keydatum.dptr = (char *) XSTRING_DATA (key);
|
|
281 keydatum.dsize = XSTRING_LENGTH (key);
|
0
|
282
|
|
283 return (!dbm_store (handle, keydatum, valdatum,
|
|
284 (NILP (replace)) ? DBM_INSERT : DBM_REPLACE));
|
|
285 }
|
|
286
|
|
287 static int
|
|
288 dbm_remove (struct database_struct *db, Lisp_Object key)
|
|
289 {
|
|
290 datum keydatum;
|
14
|
291 keydatum.dptr = (char *) XSTRING_DATA (key);
|
|
292 keydatum.dsize = XSTRING_LENGTH (key);
|
0
|
293 return (dbm_delete (db->db_handle, keydatum));
|
|
294 }
|
|
295
|
|
296 static Lisp_Object
|
|
297 dbm_lisp_type (struct database_struct *db)
|
|
298 {
|
|
299 return (Qdbm);
|
|
300 }
|
|
301
|
|
302 static CONST char *
|
|
303 dbm_type (struct database_struct *db)
|
|
304 {
|
|
305 return ("dbm");
|
|
306 }
|
|
307
|
|
308 static CONST char *
|
|
309 dbm_subtype (struct database_struct *db)
|
|
310 {
|
|
311 return ("nil");
|
|
312 }
|
|
313
|
|
314 static void *
|
|
315 new_dbm_file (CONST char *file, Lisp_Object subtype, int ackcess, int mode)
|
|
316 {
|
|
317 DBM *db = NULL;
|
|
318 db = dbm_open ((char *) file, ackcess, mode);
|
|
319 return (void *) db;
|
|
320 }
|
|
321
|
|
322 static Lisp_Object
|
|
323 dbm_lasterr (struct database_struct *dbp)
|
|
324 {
|
20
|
325 char *temp = strerror (dbp->dberrno);
|
0
|
326 return (make_string ((unsigned char *) temp, strlen (temp)));
|
|
327 }
|
|
328
|
|
329 static void
|
|
330 dbm_closeit (struct database_struct *db)
|
|
331 {
|
|
332 if (db->db_handle)
|
|
333 dbm_close ((DBM *)db->db_handle);
|
|
334 db->db_handle = NULL;
|
|
335 }
|
|
336
|
|
337 static DB_FUNCS ndbm_func_block =
|
|
338 {
|
|
339 dbm_subtype,
|
|
340 dbm_type,
|
|
341 new_dbm_file,
|
|
342 dbm_get,
|
|
343 dbm_put,
|
|
344 dbm_remove,
|
|
345 dbm_map,
|
|
346 dbm_lisp_type,
|
|
347 dbm_closeit,
|
|
348 dbm_lasterr
|
|
349 };
|
|
350 #endif
|
|
351
|
|
352 #ifdef HAVE_BERKELEY_DB
|
|
353 static Lisp_Object
|
|
354 berkdb_lisp_type (struct database_struct *db)
|
|
355 {
|
|
356 return (Qberkeley_db);
|
|
357 }
|
|
358
|
|
359 static CONST char *
|
|
360 berkdb_type (struct database_struct *db)
|
|
361 {
|
|
362 return ("berkeley");
|
|
363 }
|
|
364
|
|
365 static CONST char *
|
|
366 berkdb_subtype (struct database_struct *db)
|
|
367 {
|
|
368 DB *temp = (DB *)db->db_handle;
|
|
369
|
|
370 if (!temp)
|
|
371 return ("nil");
|
|
372
|
|
373 switch (temp->type)
|
|
374 {
|
|
375 case DB_BTREE:
|
|
376 return ("btree");
|
|
377 case DB_HASH:
|
|
378 return ("hash");
|
|
379 case DB_RECNO:
|
|
380 return ("recno");
|
|
381 }
|
|
382 return ("unknown");
|
|
383 }
|
|
384
|
|
385 static void *
|
|
386 berkdb_open (CONST char *file, Lisp_Object subtype, int ackcess, int mode)
|
|
387 {
|
|
388 DB *db;
|
|
389 DBTYPE real_subtype;
|
|
390
|
|
391 if (EQ (subtype, Qhash) || NILP (subtype))
|
|
392 real_subtype = DB_HASH;
|
|
393 else if (EQ (subtype, Qbtree))
|
|
394 real_subtype = DB_BTREE;
|
|
395 else if (EQ (subtype, Qrecno))
|
|
396 real_subtype = DB_RECNO;
|
|
397 else
|
|
398 signal_simple_error ("Unsupported subtype", subtype);
|
|
399
|
|
400 db = dbopen (file, ackcess, mode, real_subtype, NULL);
|
|
401
|
|
402 return (void *) db;
|
|
403 }
|
|
404
|
|
405 static Lisp_Object
|
|
406 berkdb_lasterr (struct database_struct *dbp)
|
|
407 {
|
20
|
408 char *temp = strerror (dbp->dberrno);
|
0
|
409 return (make_string ((unsigned char *) temp, strlen (temp)));
|
|
410 }
|
|
411
|
|
412 static Lisp_Object
|
|
413 berkdb_get (struct database_struct *db, Lisp_Object key)
|
|
414 {
|
|
415 DBT keydatum, valdatum;
|
|
416 DB *dbp = (DB *) db->db_handle;
|
|
417 int status = 0;
|
|
418
|
14
|
419 keydatum.data = XSTRING_DATA (key);
|
|
420 keydatum.size = XSTRING_LENGTH (key);
|
0
|
421
|
|
422 status = dbp->get (dbp, &keydatum, &valdatum, 0);
|
|
423
|
|
424 if (!status)
|
|
425 return (make_string (valdatum.data, valdatum.size));
|
|
426
|
20
|
427 db->dberrno = (status == 1) ? -1 : errno;
|
0
|
428 return (Qnil);
|
|
429 }
|
|
430
|
|
431 static int
|
|
432 berkdb_put (struct database_struct *db,
|
|
433 Lisp_Object key,
|
|
434 Lisp_Object val,
|
|
435 Lisp_Object replace)
|
|
436 {
|
|
437 DBT keydatum, valdatum;
|
|
438 DB *dbp = (DB *) db->db_handle;
|
|
439 int status = 0;
|
|
440
|
14
|
441 keydatum.data = XSTRING_DATA (key);
|
|
442 keydatum.size = XSTRING_LENGTH (key);
|
|
443 valdatum.data = XSTRING_DATA (val);
|
|
444 valdatum.size = XSTRING_LENGTH (val);
|
0
|
445 status = dbp->put (dbp, &keydatum, &valdatum, NILP (replace)
|
|
446 ? R_NOOVERWRITE : 0);
|
20
|
447 db->dberrno = (status == 1) ? -1 : errno;
|
0
|
448 return status;
|
|
449 }
|
|
450
|
|
451 static int
|
|
452 berkdb_remove (struct database_struct *db, Lisp_Object key)
|
|
453 {
|
|
454 DBT keydatum;
|
|
455 DB *dbp = (DB *) db->db_handle;
|
|
456 int status;
|
|
457
|
14
|
458 keydatum.data = XSTRING_DATA (key);
|
|
459 keydatum.size = XSTRING_LENGTH (key);
|
0
|
460
|
|
461 status = dbp->del (dbp, &keydatum, 0);
|
|
462 if (!status)
|
|
463 return 0;
|
|
464
|
20
|
465 db->dberrno = (status == 1) ? -1 : errno;
|
0
|
466 return 1;
|
|
467 }
|
|
468
|
|
469 static void
|
|
470 berkdb_map (struct database_struct *db, Lisp_Object func)
|
|
471 {
|
|
472 DBT keydatum, valdatum;
|
|
473 Lisp_Object key, val;
|
|
474 DB *dbp = (DB *) db->db_handle;
|
|
475 int status;
|
|
476
|
|
477 for (status = dbp->seq (dbp, &keydatum, &valdatum, R_FIRST);
|
|
478 status == 0;
|
|
479 status = dbp->seq (dbp, &keydatum, &valdatum, R_NEXT))
|
|
480 {
|
|
481 key = make_string (keydatum.data, keydatum.size);
|
|
482 val = make_string (valdatum.data, valdatum.size);
|
|
483 call2 (func, key, val);
|
|
484 }
|
|
485 }
|
|
486
|
|
487 static void
|
|
488 berkdb_close (struct database_struct *db)
|
|
489 {
|
|
490 DB *dbp = (DB *)db->db_handle;
|
|
491 if (dbp)
|
|
492 {
|
|
493 dbp->sync (dbp, 0);
|
|
494 dbp->close (dbp);
|
|
495 }
|
|
496 db->db_handle = NULL;
|
|
497 }
|
|
498
|
|
499 static DB_FUNCS berk_func_block =
|
|
500 {
|
|
501 berkdb_subtype,
|
|
502 berkdb_type,
|
|
503 berkdb_open,
|
|
504 berkdb_get,
|
|
505 berkdb_put,
|
|
506 berkdb_remove,
|
|
507 berkdb_map,
|
|
508 berkdb_lisp_type,
|
|
509 berkdb_close,
|
|
510 berkdb_lasterr
|
|
511 };
|
|
512 #endif
|
|
513
|
20
|
514 DEFUN ("database-last-error", Fdatabase_error, 0, 1, 0, /*
|
0
|
515 Return the last error associated with database OBJ.
|
20
|
516 */
|
|
517 (obj))
|
0
|
518 {
|
|
519 struct database_struct *db;
|
|
520
|
|
521 if (NILP (obj))
|
|
522 {
|
|
523 char *temp = strerror (errno);
|
|
524 return (make_string ((unsigned char *) temp, strlen (temp)));
|
|
525 }
|
|
526
|
|
527 CHECK_DATABASE (obj);
|
|
528 db = XDATABASE (obj);
|
|
529 return (db->funcs->last_error (db));
|
|
530 }
|
|
531
|
20
|
532 DEFUN ("open-database", Fmake_database, 1, 5, 0, /*
|
0
|
533 Open database FILE, using database method TYPE and SUBTYPE, with
|
|
534 access rights ACCESS and permissions MODE. ACCESS can be any
|
|
535 combination of 'r' 'w' and '+', for read, write, and creation flags.
|
20
|
536 */
|
|
537 (file, type, subtype, ackcess, mode))
|
0
|
538 {
|
|
539 Lisp_Object retval = Qnil;
|
|
540 int modemask;
|
|
541 int accessmask = 0;
|
|
542 XEMACS_DB_TYPE the_type;
|
|
543 DB_FUNCS *funcblock;
|
|
544 struct database_struct *dbase = NULL;
|
|
545 void *db = NULL;
|
|
546
|
|
547 CHECK_STRING (file);
|
|
548
|
|
549 if (NILP (ackcess))
|
|
550 {
|
|
551 accessmask = O_RDWR | O_CREAT;
|
|
552 }
|
|
553 else
|
|
554 {
|
|
555 char *acc;
|
|
556 CHECK_STRING (ackcess);
|
14
|
557 acc = (char *) XSTRING_DATA (ackcess);
|
0
|
558
|
|
559 if (strchr (acc, '+'))
|
|
560 accessmask |= O_CREAT;
|
|
561
|
|
562 if (strchr (acc, 'r') && strchr (acc, 'w'))
|
|
563 {
|
|
564 accessmask |= O_RDWR;
|
|
565 }
|
|
566 else if (strchr (acc, 'w'))
|
|
567 {
|
|
568 accessmask |= O_WRONLY;
|
|
569 }
|
|
570 else
|
|
571 {
|
|
572 accessmask |= O_RDONLY;
|
|
573 }
|
|
574 }
|
|
575
|
|
576 if (NILP (mode))
|
|
577 {
|
|
578 modemask = 493; /* rwxr-xr-x */
|
|
579 }
|
|
580 else
|
|
581 {
|
|
582 CHECK_INT (mode);
|
|
583 modemask = XINT (mode);
|
|
584 }
|
|
585
|
|
586 #ifdef HAVE_DBM
|
|
587 if (NILP (type) || EQ (type, Qdbm))
|
|
588 {
|
|
589 the_type = DB_DBM;
|
|
590 funcblock = &ndbm_func_block;
|
|
591 goto db_done;
|
|
592 }
|
|
593 #endif
|
|
594
|
|
595 #ifdef HAVE_BERKELEY_DB
|
|
596 if (NILP (type) || EQ (type, Qberkeley_db))
|
|
597 {
|
|
598
|
|
599 funcblock = &berk_func_block;
|
|
600 the_type = DB_BERKELEY;
|
|
601 goto db_done;
|
|
602 }
|
|
603 #endif
|
|
604
|
|
605 signal_simple_error ("Unsupported database type", type);
|
|
606 return (Qnil);
|
|
607
|
|
608 db_done:
|
14
|
609 db = funcblock->open_file ((char *) XSTRING_DATA (file), subtype,
|
0
|
610 accessmask, modemask);
|
|
611
|
|
612 if (!db)
|
|
613 {
|
|
614 return (Qnil);
|
|
615 }
|
|
616
|
|
617 dbase = new_database ();
|
|
618 dbase->fname = file;
|
|
619 dbase->type = the_type;
|
|
620 dbase->mode = modemask;
|
|
621 dbase->ackcess = accessmask;
|
|
622 dbase->db_handle = db;
|
|
623 dbase->funcs = funcblock;
|
|
624 XSETDATABASE (retval, dbase);
|
|
625
|
|
626 return (retval);
|
|
627 }
|
|
628
|
20
|
629 DEFUN ("put-database", Fputdatabase, 3, 4, 0, /*
|
0
|
630 Store KEY and VAL in DATABASE. If optinal fourth arg REPLACE is
|
|
631 non-nil, replace any existing entry in the database.
|
20
|
632 */
|
|
633 (key, val, dbase, replace))
|
0
|
634 {
|
|
635 struct database_struct *db;
|
|
636 int status;
|
|
637 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
|
|
638
|
|
639 CHECK_DATABASE (dbase);
|
|
640 CHECK_STRING (key);
|
|
641 db = XDATABASE (dbase);
|
|
642 if (!DATABASE_LIVE_P (db))
|
|
643 signal_simple_error ("Attempting to access closed database", dbase);
|
|
644
|
|
645 GCPRO4 (key, val, dbase, replace);
|
|
646 status = db->funcs->put (db, key, val, replace);
|
|
647 UNGCPRO;
|
|
648 return status ? Qt : Qnil;
|
|
649 }
|
|
650
|
20
|
651 DEFUN ("remove-database", Fremdatabase, 2, 2, 0, /*
|
0
|
652 Remove KEY from DATABASE.
|
20
|
653 */
|
|
654 (key, dbase))
|
0
|
655 {
|
|
656 struct database_struct *db;
|
|
657 CHECK_DATABASE (dbase);
|
|
658 CHECK_STRING (key);
|
|
659
|
|
660 db = XDATABASE (dbase);
|
|
661 if (!DATABASE_LIVE_P (db))
|
|
662 signal_simple_error ("Attempting to access closed database", dbase);
|
|
663 return db->funcs->rem (db, key) ? Qt : Qnil;
|
|
664 }
|
|
665
|
20
|
666 DEFUN ("get-database", Fgetdatabase, 2, 3, 0, /*
|
0
|
667 Find value for KEY in DATABASE.
|
|
668 If there is no corresponding value, return DEFAULT (defaults to nil).
|
20
|
669 */
|
|
670 (key, dbase, defalt))
|
0
|
671 {
|
|
672 Lisp_Object retval;
|
|
673 struct database_struct *db;
|
|
674
|
|
675 CHECK_DATABASE (dbase);
|
|
676 CHECK_STRING (key);
|
|
677 db = XDATABASE (dbase);
|
|
678 if (!DATABASE_LIVE_P (db))
|
|
679 signal_simple_error ("Attempting to access closed database", dbase);
|
|
680
|
|
681 retval = db->funcs->get (db, key);
|
|
682
|
|
683 return (NILP (retval) ? defalt : retval);
|
|
684 }
|
|
685
|
20
|
686 DEFUN ("map-database", Fmapdatabase, 2, 2, 0, /*
|
0
|
687 Map FUNCTION over entries in DATABASE, calling it with two args,
|
|
688 each key and value in the database.
|
20
|
689 */
|
|
690 (function, dbase))
|
0
|
691 {
|
|
692 struct gcpro gcpro1, gcpro2;
|
|
693 struct database_struct *db;
|
|
694
|
|
695 CHECK_DATABASE (dbase);
|
|
696 GCPRO2 (dbase, function);
|
|
697
|
|
698 db = XDATABASE (dbase);
|
|
699 if (!DATABASE_LIVE_P (db))
|
|
700 signal_simple_error ("Attempting to access closed database", dbase);
|
|
701 db->funcs->map (db, function);
|
|
702 UNGCPRO;
|
|
703 return Qnil;
|
|
704 }
|
|
705
|
|
706 void
|
|
707 syms_of_dbm (void)
|
|
708 {
|
|
709 defsymbol (&Qdatabasep, "databasep");
|
|
710 #ifdef HAVE_DBM
|
|
711 defsymbol (&Qdbm, "dbm");
|
|
712 #endif
|
|
713 #ifdef HAVE_BERKELEY_DB
|
|
714 defsymbol (&Qberkeley_db, "berkeley-db");
|
|
715 defsymbol (&Qhash, "hash");
|
|
716 defsymbol (&Qbtree, "btree");
|
|
717 defsymbol (&Qrecno, "recno");
|
|
718 #endif
|
|
719
|
20
|
720 DEFSUBR (Fmake_database);
|
|
721 DEFSUBR (Fdatabasep);
|
|
722 DEFSUBR (Fmapdatabase);
|
|
723 DEFSUBR (Fputdatabase);
|
|
724 DEFSUBR (Fgetdatabase);
|
|
725 DEFSUBR (Fremdatabase);
|
|
726 DEFSUBR (Fdatabase_type);
|
|
727 DEFSUBR (Fdatabase_subtype);
|
|
728 DEFSUBR (Fdatabase_error);
|
|
729 DEFSUBR (Fdatabase_live_p);
|
|
730 DEFSUBR (Fdatabase_file_name);
|
|
731 DEFSUBR (Fdatabase_close);
|
0
|
732 }
|
|
733
|
|
734 void
|
|
735 vars_of_dbm (void)
|
|
736 {
|
|
737 #ifdef HAVE_DBM
|
|
738 Fprovide (Qdbm);
|
|
739 #endif
|
|
740 #ifdef HAVE_BERKELEY_DB
|
|
741 Fprovide (Qberkeley_db);
|
|
742 #endif
|
|
743 }
|
|
744 #endif /* HAVE_DATABASE */
|