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 */
|
207
|
25 /* db 2.x support added by Andreas Jaeger */
|
0
|
26
|
|
27 #include <config.h>
|
|
28 #include "lisp.h"
|
272
|
29 #include "sysfile.h"
|
380
|
30 #include "buffer.h"
|
0
|
31 #include <errno.h>
|
|
32
|
149
|
33 #ifndef HAVE_DATABASE
|
272
|
34 #error HAVE_DATABASE not defined!!
|
|
35 #endif
|
149
|
36
|
157
|
37 #include "database.h" /* Our include file */
|
149
|
38
|
0
|
39 #ifdef HAVE_BERKELEY_DB
|
149
|
40 /* Work around Berkeley DB's use of int types which are defined
|
|
41 slightly differently in the not quite yet standard <inttypes.h>.
|
|
42 See db.h for details of why we're resorting to this... */
|
207
|
43 /* glibc 2.1 doesn't have this problem with DB 2.x */
|
|
44 #if !(defined __GLIBC__ && __GLIBC_MINOR__ >= 1)
|
149
|
45 #ifdef HAVE_INTTYPES_H
|
|
46 #define __BIT_TYPES_DEFINED__
|
|
47 #include <inttypes.h>
|
|
48 typedef uint8_t u_int8_t;
|
|
49 typedef uint16_t u_int16_t;
|
|
50 typedef uint32_t u_int32_t;
|
|
51 #ifdef WE_DONT_NEED_QUADS
|
|
52 typedef uint64_t u_int64_t;
|
|
53 #endif /* WE_DONT_NEED_QUADS */
|
|
54 #endif /* HAVE_INTTYPES_H */
|
207
|
55 #endif /* !(defined __GLIBC__ && __GLIBC_MINOR__ >= 1) */
|
149
|
56 #include DB_H_PATH /* Berkeley db's header file */
|
207
|
57 #ifndef DB_VERSION_MAJOR
|
|
58 # define DB_VERSION_MAJOR 1
|
|
59 #endif /* DB_VERSION_MAJOR */
|
0
|
60 Lisp_Object Qberkeley_db;
|
272
|
61 Lisp_Object Qhash, Qbtree, Qrecno, Qunknown;
|
149
|
62 #endif /* HAVE_BERKELEY_DB */
|
|
63
|
|
64 #ifdef HAVE_DBM
|
|
65 #include <ndbm.h>
|
|
66 Lisp_Object Qdbm;
|
|
67 #endif /* HAVE_DBM */
|
|
68
|
380
|
69 #ifdef MULE
|
|
70 /* #### The following should be settable on a per-database level.
|
|
71 But the whole coding-system infrastructure should be rewritten someday.
|
|
72 We really need coding-system aliases. -- martin */
|
|
73 Lisp_Object Vdatabase_coding_system;
|
|
74 #endif
|
|
75
|
149
|
76 Lisp_Object Qdatabasep;
|
0
|
77
|
149
|
78 typedef struct
|
0
|
79 {
|
380
|
80 Lisp_Object (*get_subtype) (Lisp_Database *);
|
|
81 Lisp_Object (*get_type) (Lisp_Database *);
|
|
82 Lisp_Object (*get) (Lisp_Database *, Lisp_Object);
|
|
83 int (*put) (Lisp_Database *, Lisp_Object, Lisp_Object, Lisp_Object);
|
|
84 int (*rem) (Lisp_Database *, Lisp_Object);
|
|
85 void (*map) (Lisp_Database *, Lisp_Object);
|
|
86 void (*close) (Lisp_Database *);
|
|
87 Lisp_Object (*last_error) (Lisp_Database *);
|
0
|
88 } DB_FUNCS;
|
|
89
|
272
|
90 struct Lisp_Database
|
0
|
91 {
|
|
92 struct lcrecord_header header;
|
|
93 Lisp_Object fname;
|
|
94 int mode;
|
173
|
95 int access_;
|
94
|
96 int dberrno;
|
185
|
97 int live_p;
|
|
98 #ifdef HAVE_DBM
|
|
99 DBM *dbm_handle;
|
|
100 #endif
|
|
101 #ifdef HAVE_BERKELEY_DB
|
|
102 DB *db_handle;
|
|
103 #endif
|
0
|
104 DB_FUNCS *funcs;
|
149
|
105 #ifdef MULE
|
|
106 Lisp_Object coding_system;
|
|
107 #endif
|
0
|
108 };
|
|
109
|
380
|
110 #define XDATABASE(x) XRECORD (x, database, Lisp_Database)
|
0
|
111 #define XSETDATABASE(x, p) XSETRECORD (x, p, database)
|
|
112 #define DATABASEP(x) RECORDP (x, database)
|
|
113 #define CHECK_DATABASE(x) CHECK_RECORD (x, database)
|
209
|
114 #define CONCHECK_DATABASE(x) CONCHECK_RECORD (x, database)
|
185
|
115 #define DATABASE_LIVE_P(x) (x->live_p)
|
0
|
116
|
185
|
117 #define CHECK_LIVE_DATABASE(db) do { \
|
272
|
118 CHECK_DATABASE (db); \
|
185
|
119 if (!DATABASE_LIVE_P (XDATABASE(db))) \
|
|
120 signal_simple_error ("Attempting to access closed database", db); \
|
|
121 } while (0)
|
|
122
|
|
123
|
380
|
124 static Lisp_Database *
|
209
|
125 allocate_database (void)
|
0
|
126 {
|
398
|
127 Lisp_Database *db = alloc_lcrecord_type (Lisp_Database, &lrecord_database);
|
0
|
128
|
272
|
129 db->fname = Qnil;
|
|
130 db->live_p = 0;
|
185
|
131 #ifdef HAVE_BERKELEY_DB
|
272
|
132 db->db_handle = NULL;
|
185
|
133 #endif
|
|
134 #ifdef HAVE_DBM
|
272
|
135 db->dbm_handle = NULL;
|
185
|
136 #endif
|
272
|
137 db->access_ = 0;
|
|
138 db->mode = 0;
|
|
139 db->dberrno = 0;
|
149
|
140 #ifdef MULE
|
272
|
141 db->coding_system = Fget_coding_system (Qbinary);
|
149
|
142 #endif
|
272
|
143 return db;
|
0
|
144 }
|
|
145
|
|
146 static Lisp_Object
|
398
|
147 mark_database (Lisp_Object obj)
|
0
|
148 {
|
380
|
149 Lisp_Database *db = XDATABASE (obj);
|
398
|
150 return db->fname;
|
0
|
151 }
|
|
152
|
|
153 static void
|
|
154 print_database (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
155 {
|
209
|
156 char buf[64];
|
380
|
157 Lisp_Database *db = XDATABASE (obj);
|
0
|
158
|
|
159 if (print_readably)
|
272
|
160 error ("printing unreadable object #<database 0x%x>", db->header.uid);
|
209
|
161
|
|
162 write_c_string ("#<database \"", printcharfun);
|
272
|
163 print_internal (db->fname, printcharfun, 0);
|
209
|
164 sprintf (buf, "\" (%s/%s/%s) 0x%x>",
|
272
|
165 (char *) string_data (XSYMBOL (db->funcs->get_type (db))->name),
|
|
166 (char *) string_data (XSYMBOL (db->funcs->get_subtype (db))->name),
|
|
167 (!DATABASE_LIVE_P (db) ? "closed" :
|
|
168 (db->access_ & O_WRONLY) ? "writeonly" :
|
|
169 (db->access_ & O_RDWR) ? "readwrite" : "readonly"),
|
|
170 db->header.uid);
|
209
|
171 write_c_string (buf, printcharfun);
|
0
|
172 }
|
|
173
|
|
174 static void
|
|
175 finalize_database (void *header, int for_disksave)
|
|
176 {
|
380
|
177 Lisp_Database *db = (Lisp_Database *) header;
|
0
|
178
|
|
179 if (for_disksave)
|
|
180 {
|
185
|
181 Lisp_Object obj;
|
380
|
182 XSETDATABASE (obj, db);
|
185
|
183
|
0
|
184 signal_simple_error
|
272
|
185 ("Can't dump an emacs containing database objects", obj);
|
0
|
186 }
|
|
187 db->funcs->close (db);
|
|
188 }
|
|
189
|
272
|
190 DEFINE_LRECORD_IMPLEMENTATION ("database", database,
|
|
191 mark_database, print_database,
|
398
|
192 finalize_database, 0, 0, 0,
|
380
|
193 Lisp_Database);
|
272
|
194
|
203
|
195 DEFUN ("close-database", Fclose_database, 1, 1, 0, /*
|
272
|
196 Close database DATABASE.
|
20
|
197 */
|
272
|
198 (database))
|
0
|
199 {
|
380
|
200 Lisp_Database *db;
|
272
|
201 CHECK_LIVE_DATABASE (database);
|
|
202 db = XDATABASE (database);
|
|
203 db->funcs->close (db);
|
|
204 db->live_p = 0;
|
173
|
205 return Qnil;
|
0
|
206 }
|
|
207
|
20
|
208 DEFUN ("database-type", Fdatabase_type, 1, 1, 0, /*
|
272
|
209 Return the type of database DATABASE.
|
20
|
210 */
|
272
|
211 (database))
|
0
|
212 {
|
272
|
213 CHECK_DATABASE (database);
|
0
|
214
|
272
|
215 return XDATABASE (database)->funcs->get_type (XDATABASE (database));
|
0
|
216 }
|
|
217
|
20
|
218 DEFUN ("database-subtype", Fdatabase_subtype, 1, 1, 0, /*
|
272
|
219 Return the subtype of database DATABASE, if any.
|
20
|
220 */
|
272
|
221 (database))
|
0
|
222 {
|
272
|
223 CHECK_DATABASE (database);
|
185
|
224
|
272
|
225 return XDATABASE (database)->funcs->get_subtype (XDATABASE (database));
|
0
|
226 }
|
|
227
|
20
|
228 DEFUN ("database-live-p", Fdatabase_live_p, 1, 1, 0, /*
|
272
|
229 Return t if OBJ is an active database.
|
20
|
230 */
|
|
231 (obj))
|
0
|
232 {
|
272
|
233 return DATABASEP (obj) && DATABASE_LIVE_P (XDATABASE (obj)) ? Qt : Qnil;
|
0
|
234 }
|
|
235
|
20
|
236 DEFUN ("database-file-name", Fdatabase_file_name, 1, 1, 0, /*
|
272
|
237 Return the filename associated with the database DATABASE.
|
20
|
238 */
|
272
|
239 (database))
|
0
|
240 {
|
272
|
241 CHECK_DATABASE (database);
|
185
|
242
|
272
|
243 return XDATABASE (database)->fname;
|
0
|
244 }
|
|
245
|
20
|
246 DEFUN ("databasep", Fdatabasep, 1, 1, 0, /*
|
272
|
247 Return t if OBJ is a database.
|
20
|
248 */
|
|
249 (obj))
|
0
|
250 {
|
173
|
251 return DATABASEP (obj) ? Qt : Qnil;
|
0
|
252 }
|
|
253
|
|
254 #ifdef HAVE_DBM
|
|
255 static void
|
380
|
256 dbm_map (Lisp_Database *db, Lisp_Object func)
|
0
|
257 {
|
|
258 datum keydatum, valdatum;
|
|
259 Lisp_Object key, val;
|
|
260
|
185
|
261 for (keydatum = dbm_firstkey (db->dbm_handle);
|
0
|
262 keydatum.dptr != NULL;
|
185
|
263 keydatum = dbm_nextkey (db->dbm_handle))
|
0
|
264 {
|
185
|
265 valdatum = dbm_fetch (db->dbm_handle, keydatum);
|
0
|
266 key = make_string ((unsigned char *) keydatum.dptr, keydatum.dsize);
|
|
267 val = make_string ((unsigned char *) valdatum.dptr, valdatum.dsize);
|
|
268 call2 (func, key, val);
|
|
269 }
|
|
270 }
|
|
271
|
|
272 static Lisp_Object
|
380
|
273 dbm_get (Lisp_Database *db, Lisp_Object key)
|
0
|
274 {
|
|
275 datum keydatum, valdatum;
|
185
|
276
|
14
|
277 keydatum.dptr = (char *) XSTRING_DATA (key);
|
|
278 keydatum.dsize = XSTRING_LENGTH (key);
|
185
|
279 valdatum = dbm_fetch (db->dbm_handle, keydatum);
|
0
|
280
|
|
281 return (valdatum.dptr
|
|
282 ? make_string ((unsigned char *) valdatum.dptr, valdatum.dsize)
|
|
283 : Qnil);
|
|
284 }
|
|
285
|
|
286 static int
|
380
|
287 dbm_put (Lisp_Database *db,
|
185
|
288 Lisp_Object key, Lisp_Object val, Lisp_Object replace)
|
0
|
289 {
|
|
290 datum keydatum, valdatum;
|
185
|
291
|
14
|
292 valdatum.dptr = (char *) XSTRING_DATA (val);
|
|
293 valdatum.dsize = XSTRING_LENGTH (val);
|
|
294 keydatum.dptr = (char *) XSTRING_DATA (key);
|
|
295 keydatum.dsize = XSTRING_LENGTH (key);
|
0
|
296
|
185
|
297 return !dbm_store (db->dbm_handle, keydatum, valdatum,
|
|
298 NILP (replace) ? DBM_INSERT : DBM_REPLACE);
|
0
|
299 }
|
|
300
|
|
301 static int
|
380
|
302 dbm_remove (Lisp_Database *db, Lisp_Object key)
|
0
|
303 {
|
|
304 datum keydatum;
|
185
|
305
|
14
|
306 keydatum.dptr = (char *) XSTRING_DATA (key);
|
|
307 keydatum.dsize = XSTRING_LENGTH (key);
|
185
|
308
|
|
309 return dbm_delete (db->dbm_handle, keydatum);
|
0
|
310 }
|
|
311
|
|
312 static Lisp_Object
|
380
|
313 dbm_type (Lisp_Database *db)
|
0
|
314 {
|
173
|
315 return Qdbm;
|
0
|
316 }
|
|
317
|
272
|
318 static Lisp_Object
|
380
|
319 dbm_subtype (Lisp_Database *db)
|
0
|
320 {
|
272
|
321 return Qnil;
|
0
|
322 }
|
|
323
|
|
324 static Lisp_Object
|
380
|
325 dbm_lasterr (Lisp_Database *db)
|
0
|
326 {
|
272
|
327 return lisp_strerror (db->dberrno);
|
0
|
328 }
|
|
329
|
|
330 static void
|
380
|
331 dbm_closeit (Lisp_Database *db)
|
0
|
332 {
|
185
|
333 if (db->dbm_handle)
|
|
334 {
|
|
335 dbm_close (db->dbm_handle);
|
|
336 db->dbm_handle = NULL;
|
|
337 }
|
0
|
338 }
|
|
339
|
|
340 static DB_FUNCS ndbm_func_block =
|
|
341 {
|
|
342 dbm_subtype,
|
|
343 dbm_type,
|
|
344 dbm_get,
|
|
345 dbm_put,
|
|
346 dbm_remove,
|
|
347 dbm_map,
|
|
348 dbm_closeit,
|
|
349 dbm_lasterr
|
|
350 };
|
185
|
351 #endif /* HAVE_DBM */
|
0
|
352
|
|
353 #ifdef HAVE_BERKELEY_DB
|
|
354 static Lisp_Object
|
380
|
355 berkdb_type (Lisp_Database *db)
|
0
|
356 {
|
173
|
357 return Qberkeley_db;
|
0
|
358 }
|
|
359
|
272
|
360 static Lisp_Object
|
380
|
361 berkdb_subtype (Lisp_Database *db)
|
0
|
362 {
|
185
|
363 if (!db->db_handle)
|
272
|
364 return Qnil;
|
0
|
365
|
185
|
366 switch (db->db_handle->type)
|
|
367 {
|
272
|
368 case DB_BTREE: return Qbtree;
|
|
369 case DB_HASH: return Qhash;
|
|
370 case DB_RECNO: return Qrecno;
|
|
371 default: return Qunknown;
|
185
|
372 }
|
0
|
373 }
|
|
374
|
|
375 static Lisp_Object
|
380
|
376 berkdb_lasterr (Lisp_Database *db)
|
0
|
377 {
|
272
|
378 return lisp_strerror (db->dberrno);
|
0
|
379 }
|
|
380
|
|
381 static Lisp_Object
|
380
|
382 berkdb_get (Lisp_Database *db, Lisp_Object key)
|
0
|
383 {
|
|
384 DBT keydatum, valdatum;
|
|
385 int status = 0;
|
|
386
|
380
|
387 /* DB Version 2 requires DBT's to be zeroed before use. */
|
272
|
388 xzero (keydatum);
|
|
389 xzero (valdatum);
|
207
|
390
|
14
|
391 keydatum.data = XSTRING_DATA (key);
|
|
392 keydatum.size = XSTRING_LENGTH (key);
|
185
|
393
|
207
|
394 #if DB_VERSION_MAJOR == 1
|
185
|
395 status = db->db_handle->get (db->db_handle, &keydatum, &valdatum, 0);
|
207
|
396 #else
|
|
397 status = db->db_handle->get (db->db_handle, NULL, &keydatum, &valdatum, 0);
|
|
398 #endif /* DB_VERSION_MAJOR */
|
0
|
399
|
|
400 if (!status)
|
380
|
401 /* #### Not mule-ized! will crash! */
|
185
|
402 return make_string ((Bufbyte *) valdatum.data, valdatum.size);
|
0
|
403
|
207
|
404 #if DB_VERSION_MAJOR == 1
|
94
|
405 db->dberrno = (status == 1) ? -1 : errno;
|
207
|
406 #else
|
|
407 db->dberrno = (status < 0) ? -1 : errno;
|
|
408 #endif /* DB_VERSION_MAJOR */
|
|
409
|
173
|
410 return Qnil;
|
0
|
411 }
|
|
412
|
|
413 static int
|
380
|
414 berkdb_put (Lisp_Database *db,
|
0
|
415 Lisp_Object key,
|
|
416 Lisp_Object val,
|
|
417 Lisp_Object replace)
|
|
418 {
|
|
419 DBT keydatum, valdatum;
|
|
420 int status = 0;
|
|
421
|
380
|
422 /* DB Version 2 requires DBT's to be zeroed before use. */
|
272
|
423 xzero (keydatum);
|
|
424 xzero (valdatum);
|
207
|
425
|
14
|
426 keydatum.data = XSTRING_DATA (key);
|
|
427 keydatum.size = XSTRING_LENGTH (key);
|
|
428 valdatum.data = XSTRING_DATA (val);
|
|
429 valdatum.size = XSTRING_LENGTH (val);
|
207
|
430 #if DB_VERSION_MAJOR == 1
|
185
|
431 status = db->db_handle->put (db->db_handle, &keydatum, &valdatum,
|
207
|
432 NILP (replace) ? R_NOOVERWRITE : 0);
|
94
|
433 db->dberrno = (status == 1) ? -1 : errno;
|
207
|
434 #else
|
|
435 status = db->db_handle->put (db->db_handle, NULL, &keydatum, &valdatum,
|
|
436 NILP (replace) ? DB_NOOVERWRITE : 0);
|
|
437 db->dberrno = (status < 0) ? -1 : errno;
|
|
438 #endif/* DV_VERSION_MAJOR = 2 */
|
|
439
|
0
|
440 return status;
|
|
441 }
|
|
442
|
|
443 static int
|
380
|
444 berkdb_remove (Lisp_Database *db, Lisp_Object key)
|
0
|
445 {
|
|
446 DBT keydatum;
|
|
447 int status;
|
|
448
|
380
|
449 /* DB Version 2 requires DBT's to be zeroed before use. */
|
272
|
450 xzero (keydatum);
|
207
|
451
|
14
|
452 keydatum.data = XSTRING_DATA (key);
|
|
453 keydatum.size = XSTRING_LENGTH (key);
|
185
|
454
|
207
|
455 #if DB_VERSION_MAJOR == 1
|
185
|
456 status = db->db_handle->del (db->db_handle, &keydatum, 0);
|
207
|
457 #else
|
|
458 status = db->db_handle->del (db->db_handle, NULL, &keydatum, 0);
|
|
459 #endif /* DB_VERSION_MAJOR */
|
|
460
|
0
|
461 if (!status)
|
|
462 return 0;
|
185
|
463
|
207
|
464 #if DB_VERSION_MAJOR == 1
|
94
|
465 db->dberrno = (status == 1) ? -1 : errno;
|
207
|
466 #else
|
|
467 db->dberrno = (status < 0) ? -1 : errno;
|
|
468 #endif /* DB_VERSION_MAJOR */
|
|
469
|
0
|
470 return 1;
|
|
471 }
|
|
472
|
|
473 static void
|
380
|
474 berkdb_map (Lisp_Database *db, Lisp_Object func)
|
0
|
475 {
|
|
476 DBT keydatum, valdatum;
|
|
477 Lisp_Object key, val;
|
185
|
478 DB *dbp = db->db_handle;
|
0
|
479 int status;
|
|
480
|
380
|
481 xzero (keydatum);
|
|
482 xzero (valdatum);
|
|
483
|
207
|
484 #if DB_VERSION_MAJOR == 1
|
0
|
485 for (status = dbp->seq (dbp, &keydatum, &valdatum, R_FIRST);
|
|
486 status == 0;
|
|
487 status = dbp->seq (dbp, &keydatum, &valdatum, R_NEXT))
|
|
488 {
|
398
|
489 /* #### Needs mule-izing */
|
185
|
490 key = make_string ((Bufbyte *) keydatum.data, keydatum.size);
|
|
491 val = make_string ((Bufbyte *) valdatum.data, valdatum.size);
|
0
|
492 call2 (func, key, val);
|
|
493 }
|
207
|
494 #else
|
382
|
495 {
|
|
496 DBC *dbcp;
|
207
|
497
|
394
|
498 #if DB_VERSION_MAJOR > 2 || DB_VERSION_MINOR >=6
|
|
499 status = dbp->cursor (dbp, NULL, &dbcp, 0);
|
|
500 #else
|
382
|
501 status = dbp->cursor (dbp, NULL, &dbcp);
|
398
|
502 #endif
|
382
|
503 for (status = dbcp->c_get (dbcp, &keydatum, &valdatum, DB_FIRST);
|
|
504 status == 0;
|
|
505 status = dbcp->c_get (dbcp, &keydatum, &valdatum, DB_NEXT))
|
|
506 {
|
398
|
507 /* #### Needs mule-izing */
|
382
|
508 key = make_string ((Bufbyte *) keydatum.data, keydatum.size);
|
|
509 val = make_string ((Bufbyte *) valdatum.data, valdatum.size);
|
|
510 call2 (func, key, val);
|
|
511 }
|
|
512 dbcp->c_close (dbcp);
|
|
513 }
|
207
|
514 #endif /* DB_VERSION_MAJOR */
|
0
|
515 }
|
|
516
|
|
517 static void
|
380
|
518 berkdb_close (Lisp_Database *db)
|
0
|
519 {
|
185
|
520 if (db->db_handle)
|
0
|
521 {
|
207
|
522 #if DB_VERSION_MAJOR == 1
|
185
|
523 db->db_handle->sync (db->db_handle, 0);
|
|
524 db->db_handle->close (db->db_handle);
|
207
|
525 #else
|
|
526 db->db_handle->sync (db->db_handle, 0);
|
|
527 db->db_handle->close (db->db_handle, 0);
|
|
528 #endif /* DB_VERSION_MAJOR */
|
185
|
529 db->db_handle = NULL;
|
0
|
530 }
|
|
531 }
|
|
532
|
|
533 static DB_FUNCS berk_func_block =
|
|
534 {
|
|
535 berkdb_subtype,
|
|
536 berkdb_type,
|
|
537 berkdb_get,
|
|
538 berkdb_put,
|
|
539 berkdb_remove,
|
|
540 berkdb_map,
|
|
541 berkdb_close,
|
|
542 berkdb_lasterr
|
|
543 };
|
185
|
544 #endif /* HAVE_BERKELEY_DB */
|
0
|
545
|
203
|
546 DEFUN ("database-last-error", Fdatabase_last_error, 0, 1, 0, /*
|
272
|
547 Return the last error associated with DATABASE.
|
20
|
548 */
|
272
|
549 (database))
|
0
|
550 {
|
272
|
551 if (NILP (database))
|
151
|
552 return lisp_strerror (errno);
|
185
|
553
|
272
|
554 CHECK_DATABASE (database);
|
185
|
555
|
272
|
556 return XDATABASE (database)->funcs->last_error (XDATABASE (database));
|
0
|
557 }
|
|
558
|
203
|
559 DEFUN ("open-database", Fopen_database, 1, 5, 0, /*
|
272
|
560 Return a new database object opened on FILE.
|
|
561 Optional arguments TYPE and SUBTYPE specify the database type.
|
|
562 Optional argument ACCESS specifies the access rights, which may be any
|
0
|
563 combination of 'r' 'w' and '+', for read, write, and creation flags.
|
272
|
564 Optional argument MODE gives the permissions to use when opening FILE,
|
|
565 and defaults to 0755.
|
20
|
566 */
|
173
|
567 (file, type, subtype, access_, mode))
|
0
|
568 {
|
187
|
569 /* This function can GC */
|
0
|
570 int modemask;
|
|
571 int accessmask = 0;
|
380
|
572 Lisp_Database *db = NULL;
|
185
|
573 char *filename;
|
187
|
574 struct gcpro gcpro1, gcpro2;
|
0
|
575
|
70
|
576 CHECK_STRING (file);
|
187
|
577 GCPRO2 (file, access_);
|
|
578 file = Fexpand_file_name (file, Qnil);
|
|
579 UNGCPRO;
|
380
|
580
|
398
|
581 TO_EXTERNAL_FORMAT (LISP_STRING, file,
|
|
582 C_STRING_ALLOCA, filename,
|
|
583 Qfile_name);
|
0
|
584
|
173
|
585 if (NILP (access_))
|
0
|
586 {
|
|
587 accessmask = O_RDWR | O_CREAT;
|
|
588 }
|
|
589 else
|
|
590 {
|
|
591 char *acc;
|
173
|
592 CHECK_STRING (access_);
|
|
593 acc = (char *) XSTRING_DATA (access_);
|
185
|
594
|
0
|
595 if (strchr (acc, '+'))
|
|
596 accessmask |= O_CREAT;
|
185
|
597
|
272
|
598 {
|
|
599 char *rp = strchr (acc, 'r');
|
|
600 char *wp = strchr (acc, 'w');
|
|
601 if (rp && wp) accessmask |= O_RDWR;
|
|
602 else if (wp) accessmask |= O_WRONLY;
|
|
603 else accessmask |= O_RDONLY;
|
|
604 }
|
0
|
605 }
|
|
606
|
|
607 if (NILP (mode))
|
|
608 {
|
185
|
609 modemask = 0755; /* rwxr-xr-x */
|
0
|
610 }
|
|
611 else
|
|
612 {
|
|
613 CHECK_INT (mode);
|
|
614 modemask = XINT (mode);
|
|
615 }
|
|
616
|
|
617 #ifdef HAVE_DBM
|
|
618 if (NILP (type) || EQ (type, Qdbm))
|
|
619 {
|
272
|
620 DBM *dbase = dbm_open (filename, accessmask, modemask);
|
|
621 if (!dbase)
|
185
|
622 return Qnil;
|
|
623
|
272
|
624 db = allocate_database ();
|
|
625 db->dbm_handle = dbase;
|
|
626 db->funcs = &ndbm_func_block;
|
0
|
627 goto db_done;
|
|
628 }
|
185
|
629 #endif /* HAVE_DBM */
|
0
|
630
|
|
631 #ifdef HAVE_BERKELEY_DB
|
|
632 if (NILP (type) || EQ (type, Qberkeley_db))
|
|
633 {
|
185
|
634 DBTYPE real_subtype;
|
272
|
635 DB *dbase;
|
211
|
636 #if DB_VERSION_MAJOR != 1
|
207
|
637 int status;
|
211
|
638 #endif
|
|
639
|
185
|
640 if (EQ (subtype, Qhash) || NILP (subtype))
|
|
641 real_subtype = DB_HASH;
|
|
642 else if (EQ (subtype, Qbtree))
|
|
643 real_subtype = DB_BTREE;
|
|
644 else if (EQ (subtype, Qrecno))
|
|
645 real_subtype = DB_RECNO;
|
|
646 else
|
|
647 signal_simple_error ("Unsupported subtype", subtype);
|
|
648
|
207
|
649 #if DB_VERSION_MAJOR == 1
|
272
|
650 dbase = dbopen (filename, accessmask, modemask, real_subtype, NULL);
|
|
651 if (!dbase)
|
185
|
652 return Qnil;
|
207
|
653 #else
|
|
654 /* Berkeley DB Version 2 has only the accessmask DB_CREATE and DB_RDONLY,
|
|
655 other flags shouldn't be set */
|
|
656 if (NILP (access_))
|
272
|
657 accessmask = DB_CREATE;
|
207
|
658 else
|
|
659 {
|
|
660 char *acc;
|
|
661 CHECK_STRING (access_);
|
|
662 acc = (char *) XSTRING_DATA (access_);
|
|
663 accessmask = 0;
|
272
|
664
|
207
|
665 if (strchr (acc, '+'))
|
|
666 accessmask |= DB_CREATE;
|
185
|
667
|
207
|
668 if (strchr (acc, 'r') && !strchr (acc, 'w'))
|
|
669 accessmask |= DB_RDONLY;
|
|
670 }
|
272
|
671 status = db_open (filename, real_subtype, accessmask,
|
|
672 modemask, NULL , NULL, &dbase);
|
207
|
673 if (status)
|
|
674 return Qnil;
|
|
675 #endif /* DB_VERSION_MAJOR */
|
272
|
676
|
|
677 db = allocate_database ();
|
|
678 db->db_handle = dbase;
|
|
679 db->funcs = &berk_func_block;
|
0
|
680 goto db_done;
|
|
681 }
|
185
|
682 #endif /* HAVE_BERKELEY_DB */
|
|
683
|
0
|
684 signal_simple_error ("Unsupported database type", type);
|
173
|
685 return Qnil;
|
0
|
686
|
|
687 db_done:
|
272
|
688 db->live_p = 1;
|
|
689 db->fname = file;
|
|
690 db->mode = modemask;
|
|
691 db->access_ = accessmask;
|
0
|
692
|
272
|
693 {
|
|
694 Lisp_Object retval;
|
|
695 XSETDATABASE (retval, db);
|
|
696 return retval;
|
|
697 }
|
0
|
698 }
|
|
699
|
203
|
700 DEFUN ("put-database", Fput_database, 3, 4, 0, /*
|
272
|
701 Store KEY and VALUE in DATABASE.
|
|
702 If optional fourth arg REPLACE is non-nil,
|
|
703 replace any existing entry in the database.
|
20
|
704 */
|
272
|
705 (key, value, database, replace))
|
0
|
706 {
|
272
|
707 CHECK_LIVE_DATABASE (database);
|
0
|
708 CHECK_STRING (key);
|
272
|
709 CHECK_STRING (value);
|
185
|
710 {
|
380
|
711 Lisp_Database *db = XDATABASE (database);
|
272
|
712 int status = db->funcs->put (db, key, value, replace);
|
185
|
713 return status ? Qt : Qnil;
|
|
714 }
|
0
|
715 }
|
|
716
|
203
|
717 DEFUN ("remove-database", Fremove_database, 2, 2, 0, /*
|
0
|
718 Remove KEY from DATABASE.
|
20
|
719 */
|
272
|
720 (key, database))
|
0
|
721 {
|
272
|
722 CHECK_LIVE_DATABASE (database);
|
0
|
723 CHECK_STRING (key);
|
272
|
724 {
|
380
|
725 Lisp_Database *db = XDATABASE (database);
|
272
|
726 int status = db->funcs->rem (db, key);
|
|
727 return status ? Qt : Qnil;
|
|
728 }
|
0
|
729 }
|
185
|
730
|
203
|
731 DEFUN ("get-database", Fget_database, 2, 3, 0, /*
|
272
|
732 Return value for KEY in DATABASE.
|
0
|
733 If there is no corresponding value, return DEFAULT (defaults to nil).
|
20
|
734 */
|
272
|
735 (key, database, default_))
|
0
|
736 {
|
272
|
737 CHECK_LIVE_DATABASE (database);
|
0
|
738 CHECK_STRING (key);
|
185
|
739 {
|
380
|
740 Lisp_Database *db = XDATABASE (database);
|
272
|
741 Lisp_Object retval = db->funcs->get (db, key);
|
185
|
742 return NILP (retval) ? default_ : retval;
|
|
743 }
|
0
|
744 }
|
|
745
|
20
|
746 DEFUN ("map-database", Fmapdatabase, 2, 2, 0, /*
|
0
|
747 Map FUNCTION over entries in DATABASE, calling it with two args,
|
|
748 each key and value in the database.
|
20
|
749 */
|
272
|
750 (function, database))
|
0
|
751 {
|
272
|
752 CHECK_LIVE_DATABASE (database);
|
0
|
753
|
272
|
754 XDATABASE (database)->funcs->map (XDATABASE (database), function);
|
185
|
755
|
0
|
756 return Qnil;
|
|
757 }
|
|
758
|
|
759 void
|
272
|
760 syms_of_database (void)
|
0
|
761 {
|
|
762 defsymbol (&Qdatabasep, "databasep");
|
|
763 #ifdef HAVE_DBM
|
|
764 defsymbol (&Qdbm, "dbm");
|
|
765 #endif
|
|
766 #ifdef HAVE_BERKELEY_DB
|
|
767 defsymbol (&Qberkeley_db, "berkeley-db");
|
|
768 defsymbol (&Qhash, "hash");
|
|
769 defsymbol (&Qbtree, "btree");
|
|
770 defsymbol (&Qrecno, "recno");
|
272
|
771 defsymbol (&Qunknown, "unknown");
|
0
|
772 #endif
|
|
773
|
203
|
774 DEFSUBR (Fopen_database);
|
20
|
775 DEFSUBR (Fdatabasep);
|
|
776 DEFSUBR (Fmapdatabase);
|
203
|
777 DEFSUBR (Fput_database);
|
|
778 DEFSUBR (Fget_database);
|
|
779 DEFSUBR (Fremove_database);
|
20
|
780 DEFSUBR (Fdatabase_type);
|
|
781 DEFSUBR (Fdatabase_subtype);
|
203
|
782 DEFSUBR (Fdatabase_last_error);
|
20
|
783 DEFSUBR (Fdatabase_live_p);
|
|
784 DEFSUBR (Fdatabase_file_name);
|
203
|
785 DEFSUBR (Fclose_database);
|
0
|
786 }
|
|
787
|
|
788 void
|
272
|
789 vars_of_database (void)
|
0
|
790 {
|
|
791 #ifdef HAVE_DBM
|
|
792 Fprovide (Qdbm);
|
|
793 #endif
|
|
794 #ifdef HAVE_BERKELEY_DB
|
|
795 Fprovide (Qberkeley_db);
|
|
796 #endif
|
380
|
797
|
|
798 #if 0 /* #### implement me! */
|
|
799 #ifdef MULE
|
|
800 DEFVAR_LISP ("database-coding-system", &Vdatabase_coding_system /*
|
|
801 Coding system used to convert data in database files.
|
|
802 */ );
|
|
803 Vdatabase_coding_system = Qnil;
|
|
804 #endif
|
|
805 #endif /* 0 */
|
0
|
806 }
|