annotate src/database.c @ 70:131b0175ea99 r20-0b30

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