annotate src/database.c @ 0:376386a54a3c r19-14

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