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;
|
48
|
546 struct gcpro gcpro1;
|
0
|
547
|
48
|
548 GCPRO1 (file);
|
|
549 file = Fexpand_file_name (file, Qnil);
|
0
|
550
|
|
551 if (NILP (ackcess))
|
|
552 {
|
|
553 accessmask = O_RDWR | O_CREAT;
|
|
554 }
|
|
555 else
|
|
556 {
|
|
557 char *acc;
|
|
558 CHECK_STRING (ackcess);
|
14
|
559 acc = (char *) XSTRING_DATA (ackcess);
|
0
|
560
|
|
561 if (strchr (acc, '+'))
|
|
562 accessmask |= O_CREAT;
|
|
563
|
|
564 if (strchr (acc, 'r') && strchr (acc, 'w'))
|
|
565 {
|
|
566 accessmask |= O_RDWR;
|
|
567 }
|
|
568 else if (strchr (acc, 'w'))
|
|
569 {
|
|
570 accessmask |= O_WRONLY;
|
|
571 }
|
|
572 else
|
|
573 {
|
|
574 accessmask |= O_RDONLY;
|
|
575 }
|
|
576 }
|
|
577
|
|
578 if (NILP (mode))
|
|
579 {
|
|
580 modemask = 493; /* rwxr-xr-x */
|
|
581 }
|
|
582 else
|
|
583 {
|
|
584 CHECK_INT (mode);
|
|
585 modemask = XINT (mode);
|
|
586 }
|
|
587
|
|
588 #ifdef HAVE_DBM
|
|
589 if (NILP (type) || EQ (type, Qdbm))
|
|
590 {
|
|
591 the_type = DB_DBM;
|
|
592 funcblock = &ndbm_func_block;
|
|
593 goto db_done;
|
|
594 }
|
|
595 #endif
|
|
596
|
|
597 #ifdef HAVE_BERKELEY_DB
|
|
598 if (NILP (type) || EQ (type, Qberkeley_db))
|
|
599 {
|
|
600
|
|
601 funcblock = &berk_func_block;
|
|
602 the_type = DB_BERKELEY;
|
|
603 goto db_done;
|
|
604 }
|
|
605 #endif
|
|
606
|
|
607 signal_simple_error ("Unsupported database type", type);
|
|
608 return (Qnil);
|
|
609
|
|
610 db_done:
|
14
|
611 db = funcblock->open_file ((char *) XSTRING_DATA (file), subtype,
|
0
|
612 accessmask, modemask);
|
|
613
|
|
614 if (!db)
|
|
615 {
|
|
616 return (Qnil);
|
|
617 }
|
|
618
|
|
619 dbase = new_database ();
|
|
620 dbase->fname = file;
|
|
621 dbase->type = the_type;
|
|
622 dbase->mode = modemask;
|
|
623 dbase->ackcess = accessmask;
|
|
624 dbase->db_handle = db;
|
|
625 dbase->funcs = funcblock;
|
|
626 XSETDATABASE (retval, dbase);
|
|
627
|
48
|
628 UNGCPRO;
|
|
629
|
0
|
630 return (retval);
|
|
631 }
|
|
632
|
20
|
633 DEFUN ("put-database", Fputdatabase, 3, 4, 0, /*
|
0
|
634 Store KEY and VAL in DATABASE. If optinal fourth arg REPLACE is
|
|
635 non-nil, replace any existing entry in the database.
|
20
|
636 */
|
|
637 (key, val, dbase, replace))
|
0
|
638 {
|
|
639 struct database_struct *db;
|
|
640 int status;
|
|
641 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
|
|
642
|
|
643 CHECK_DATABASE (dbase);
|
|
644 CHECK_STRING (key);
|
|
645 db = XDATABASE (dbase);
|
|
646 if (!DATABASE_LIVE_P (db))
|
|
647 signal_simple_error ("Attempting to access closed database", dbase);
|
|
648
|
|
649 GCPRO4 (key, val, dbase, replace);
|
|
650 status = db->funcs->put (db, key, val, replace);
|
|
651 UNGCPRO;
|
|
652 return status ? Qt : Qnil;
|
|
653 }
|
|
654
|
20
|
655 DEFUN ("remove-database", Fremdatabase, 2, 2, 0, /*
|
0
|
656 Remove KEY from DATABASE.
|
20
|
657 */
|
|
658 (key, dbase))
|
0
|
659 {
|
|
660 struct database_struct *db;
|
|
661 CHECK_DATABASE (dbase);
|
|
662 CHECK_STRING (key);
|
|
663
|
|
664 db = XDATABASE (dbase);
|
|
665 if (!DATABASE_LIVE_P (db))
|
|
666 signal_simple_error ("Attempting to access closed database", dbase);
|
|
667 return db->funcs->rem (db, key) ? Qt : Qnil;
|
|
668 }
|
|
669
|
20
|
670 DEFUN ("get-database", Fgetdatabase, 2, 3, 0, /*
|
0
|
671 Find value for KEY in DATABASE.
|
|
672 If there is no corresponding value, return DEFAULT (defaults to nil).
|
20
|
673 */
|
|
674 (key, dbase, defalt))
|
0
|
675 {
|
|
676 Lisp_Object retval;
|
|
677 struct database_struct *db;
|
|
678
|
|
679 CHECK_DATABASE (dbase);
|
|
680 CHECK_STRING (key);
|
|
681 db = XDATABASE (dbase);
|
|
682 if (!DATABASE_LIVE_P (db))
|
|
683 signal_simple_error ("Attempting to access closed database", dbase);
|
|
684
|
|
685 retval = db->funcs->get (db, key);
|
|
686
|
|
687 return (NILP (retval) ? defalt : retval);
|
|
688 }
|
|
689
|
20
|
690 DEFUN ("map-database", Fmapdatabase, 2, 2, 0, /*
|
0
|
691 Map FUNCTION over entries in DATABASE, calling it with two args,
|
|
692 each key and value in the database.
|
20
|
693 */
|
|
694 (function, dbase))
|
0
|
695 {
|
|
696 struct gcpro gcpro1, gcpro2;
|
|
697 struct database_struct *db;
|
|
698
|
|
699 CHECK_DATABASE (dbase);
|
|
700 GCPRO2 (dbase, function);
|
|
701
|
|
702 db = XDATABASE (dbase);
|
|
703 if (!DATABASE_LIVE_P (db))
|
|
704 signal_simple_error ("Attempting to access closed database", dbase);
|
|
705 db->funcs->map (db, function);
|
|
706 UNGCPRO;
|
|
707 return Qnil;
|
|
708 }
|
|
709
|
|
710 void
|
|
711 syms_of_dbm (void)
|
|
712 {
|
|
713 defsymbol (&Qdatabasep, "databasep");
|
|
714 #ifdef HAVE_DBM
|
|
715 defsymbol (&Qdbm, "dbm");
|
|
716 #endif
|
|
717 #ifdef HAVE_BERKELEY_DB
|
|
718 defsymbol (&Qberkeley_db, "berkeley-db");
|
|
719 defsymbol (&Qhash, "hash");
|
|
720 defsymbol (&Qbtree, "btree");
|
|
721 defsymbol (&Qrecno, "recno");
|
|
722 #endif
|
|
723
|
20
|
724 DEFSUBR (Fmake_database);
|
|
725 DEFSUBR (Fdatabasep);
|
|
726 DEFSUBR (Fmapdatabase);
|
|
727 DEFSUBR (Fputdatabase);
|
|
728 DEFSUBR (Fgetdatabase);
|
|
729 DEFSUBR (Fremdatabase);
|
|
730 DEFSUBR (Fdatabase_type);
|
|
731 DEFSUBR (Fdatabase_subtype);
|
|
732 DEFSUBR (Fdatabase_error);
|
|
733 DEFSUBR (Fdatabase_live_p);
|
|
734 DEFSUBR (Fdatabase_file_name);
|
|
735 DEFSUBR (Fdatabase_close);
|
0
|
736 }
|
|
737
|
|
738 void
|
|
739 vars_of_dbm (void)
|
|
740 {
|
|
741 #ifdef HAVE_DBM
|
|
742 Fprovide (Qdbm);
|
|
743 #endif
|
|
744 #ifdef HAVE_BERKELEY_DB
|
|
745 Fprovide (Qberkeley_db);
|
|
746 #endif
|
|
747 }
|
|
748 #endif /* HAVE_DATABASE */
|