1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
/*
* db_gdbm.c - bindings for gdbm
*
* This file is part of zsh, the Z shell.
*
* Copyright (c) 2008 Clint Adams
* All rights reserved.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and to distribute modified versions of this software for any
* purpose, provided that the above copyright notice and the following
* two paragraphs appear in all copies of this software.
*
* In no event shall Clint Adams or the Zsh Development
* Group be liable to any party for direct, indirect, special, incidental, or
* consequential damages arising out of the use of this software and its
* documentation, even if Peter Stephenson, Sven Wischnowsky and the Zsh
* Development Group have been advised of the possibility of such damage.
*
* Clint Adams and the Zsh Development Group
* specifically disclaim any warranties, including, but not limited to, the
* implied warranties of merchantability and fitness for a particular purpose.
* The software provided hereunder is on an "as is" basis, and Peter
* Stephenson, Sven Wischnowsky and the Zsh Development Group have no
* obligation to provide maintenance, support, updates, enhancements, or
* modifications.
*
*/
#include "db_gdbm.mdh"
#include "db_gdbm.pro"
/*
* Make sure we have all the bits I'm using for memory mapping, otherwise
* I don't know what I'm doing.
*/
#if defined(HAVE_GDBM_H) && defined(HAVE_GDBM_OPEN)
#include <gdbm.h>
static char *backtype = "db/gdbm";
static const struct gsu_scalar gdbm_gsu =
{ gdbmgetfn, gdbmsetfn, gdbmunsetfn };
/**/
static const struct gsu_hash gdbm_hash_gsu =
{ hashgetfn, gdbmhashsetfn, gdbmhashunsetfn };
static struct builtin bintab[] = {
BUILTIN("ztie", 0, bin_ztie, 1, -1, 0, "d:f:r", NULL),
BUILTIN("zuntie", 0, bin_zuntie, 1, -1, 0, "u", NULL),
};
/**/
static int
bin_ztie(char *nam, char **args, Options ops, UNUSED(int func))
{
char *resource_name, *pmname;
GDBM_FILE dbf = NULL;
int read_write = GDBM_SYNC, pmflags = PM_REMOVABLE;
Param tied_param;
if(!OPT_ISSET(ops,'d')) {
zwarnnam(nam, "you must pass `-d %s'", backtype);
return 1;
}
if(!OPT_ISSET(ops,'f')) {
zwarnnam(nam, "you must pass `-f' with a filename", NULL);
return 1;
}
if (OPT_ISSET(ops,'r')) {
read_write |= GDBM_READER;
pmflags |= PM_READONLY;
} else {
read_write |= GDBM_WRCREAT;
}
/* Here should be a lookup of the backend type against
* a registry.
*/
if (strcmp(OPT_ARG(ops, 'd'), backtype) != 0) {
zwarnnam(nam, "unsupported backend type `%s'", OPT_ARG(ops, 'd'));
return 1;
}
resource_name = OPT_ARG(ops, 'f');
pmname = *args;
if ((tied_param = (Param)paramtab->getnode(paramtab, pmname)) &&
!(tied_param->node.flags & PM_UNSET)) {
/*
* Unset any existing parameter. Note there's no implicit
* "local" here, but if the existing parameter is local
* that will be reflected in the new one.
*
* We need to do this before attempting to open the DB
* in case this variable is already tied to a DB.
*
* This can fail if the variable is readonly or restricted.
* We could call unsetparam() and check errflag instead
* of the return status.
*/
if (unsetparam_pm(tied_param, 0, 1))
return 1;
}
dbf = gdbm_open(resource_name, 0, read_write, 0666, 0);
if(dbf)
addmodulefd(gdbm_fdesc(dbf), FDT_INTERNAL);
else {
zwarnnam(nam, "error opening database file %s", resource_name);
return 1;
}
if (!(tied_param = createspecialhash(pmname, &getgdbmnode, &scangdbmkeys,
pmflags))) {
zwarnnam(nam, "cannot create the requested parameter %s", pmname);
fdtable[gdbm_fdesc(dbf)] = FDT_UNUSED;
gdbm_close(dbf);
return 1;
}
tied_param->gsu.h = &gdbm_hash_gsu;
tied_param->u.hash->tmpdata = (void *)dbf;
return 0;
}
/**/
static int
bin_zuntie(char *nam, char **args, Options ops, UNUSED(int func))
{
Param pm;
char *pmname;
int ret = 0;
for (pmname = *args; *args++; pmname = *args) {
pm = (Param) paramtab->getnode(paramtab, pmname);
if(!pm) {
zwarnnam(nam, "cannot untie %s", pmname);
ret = 1;
continue;
}
if (pm->gsu.h != &gdbm_hash_gsu) {
zwarnnam(nam, "not a tied gdbm hash: %s", pmname);
ret = 1;
continue;
}
queue_signals();
if (OPT_ISSET(ops,'u'))
gdbmuntie(pm); /* clear read-only-ness */
if (unsetparam_pm(pm, 0, 1)) {
/* assume already reported */
ret = 1;
}
unqueue_signals();
}
return ret;
}
/**/
static char *
gdbmgetfn(Param pm)
{
datum key, content;
int ret;
GDBM_FILE dbf;
key.dptr = pm->node.nam;
key.dsize = strlen(key.dptr);
dbf = (GDBM_FILE)(pm->u.hash->tmpdata);
ret = gdbm_exists(dbf, key);
if(ret) {
content = gdbm_fetch(dbf, key);
} else {
content.dptr = dupstring("");
}
return content.dptr;
}
/**/
static void
gdbmsetfn(Param pm, char *val)
{
datum key, content;
GDBM_FILE dbf;
key.dptr = pm->node.nam;
key.dsize = strlen(key.dptr);
content.dptr = val;
content.dsize = strlen(content.dptr);
dbf = (GDBM_FILE)(pm->u.hash->tmpdata);
(void)gdbm_store(dbf, key, content, GDBM_REPLACE);
}
/**/
static void
gdbmunsetfn(Param pm, UNUSED(int um))
{
datum key;
GDBM_FILE dbf;
key.dptr = pm->node.nam;
key.dsize = strlen(key.dptr);
dbf = (GDBM_FILE)(pm->u.hash->tmpdata);
(void)gdbm_delete(dbf, key);
}
/**/
static HashNode
getgdbmnode(HashTable ht, const char *name)
{
int len;
char *nameu;
Param pm = NULL;
nameu = dupstring(name);
unmetafy(nameu, &len);
pm = (Param) hcalloc(sizeof(struct param));
pm->node.nam = nameu;
pm->node.flags = PM_SCALAR;
pm->gsu.s = &gdbm_gsu;
pm->u.hash = ht;
return &pm->node;
}
/**/
static void
scangdbmkeys(HashTable ht, ScanFunc func, int flags)
{
Param pm = NULL;
datum key, content;
GDBM_FILE dbf = (GDBM_FILE)(ht->tmpdata);
pm = (Param) hcalloc(sizeof(struct param));
pm->node.flags = PM_SCALAR;
pm->gsu.s = &nullsetscalar_gsu;
key = gdbm_firstkey(dbf);
while(key.dptr) {
content = gdbm_fetch(dbf, key);
pm->node.nam = key.dptr;
pm->u.str = content.dptr;
pm->gsu.s = &nullsetscalar_gsu;
func(&pm->node, flags);
key = gdbm_nextkey(dbf, key);
}
}
/**/
static void
gdbmhashsetfn(Param pm, HashTable ht)
{
int i;
HashNode hn;
GDBM_FILE dbf;
datum key, content;
if (!pm->u.hash || pm->u.hash == ht)
return;
if (!(dbf = (GDBM_FILE)(pm->u.hash->tmpdata)))
return;
key = gdbm_firstkey(dbf);
while (key.dptr) {
queue_signals();
(void)gdbm_delete(dbf, key);
free(key.dptr);
unqueue_signals();
key = gdbm_firstkey(dbf);
}
/* just deleted everything, clean up */
(void)gdbm_reorganize(dbf);
if (!ht)
return;
for (i = 0; i < ht->hsize; i++)
for (hn = ht->nodes[i]; hn; hn = hn->next) {
struct value v;
v.isarr = v.flags = v.start = 0;
v.end = -1;
v.arr = NULL;
v.pm = (Param) hn;
key.dptr = v.pm->node.nam;
key.dsize = strlen(key.dptr);
queue_signals();
content.dptr = getstrvalue(&v);
content.dsize = strlen(content.dptr);
(void)gdbm_store(dbf, key, content, GDBM_REPLACE);
unqueue_signals();
}
}
/**/
static void
gdbmuntie(Param pm)
{
GDBM_FILE dbf = (GDBM_FILE)(pm->u.hash->tmpdata);
HashTable ht = pm->u.hash;
if (dbf) { /* paranoia */
fdtable[gdbm_fdesc(dbf)] = FDT_UNUSED;
gdbm_close(dbf);
}
ht->tmpdata = NULL;
/* for completeness ... createspecialhash() should have an inverse */
ht->getnode = ht->getnode2 = gethashnode2;
ht->scantab = NULL;
pm->node.flags &= ~(PM_SPECIAL|PM_READONLY);
pm->gsu.h = &stdhash_gsu;
}
/**/
static void
gdbmhashunsetfn(Param pm, UNUSED(int exp))
{
gdbmuntie(pm);
/* hash table is now normal, so proceed normally... */
pm->gsu.h->setfn(pm, NULL);
pm->node.flags |= PM_UNSET;
}
#else
# error no gdbm
#endif /* have gdbm */
static struct features module_features = {
bintab, sizeof(bintab)/sizeof(*bintab),
NULL, 0,
NULL, 0,
NULL, 0,
0
};
/**/
int
setup_(UNUSED(Module m))
{
return 0;
}
/**/
int
features_(Module m, char ***features)
{
*features = featuresarray(m, &module_features);
return 0;
}
/**/
int
enables_(Module m, int **enables)
{
return handlefeatures(m, &module_features, enables);
}
/**/
int
boot_(UNUSED(Module m))
{
return 0;
}
/**/
int
cleanup_(Module m)
{
return setfeatureenables(m, &module_features, NULL);
}
/**/
int
finish_(UNUSED(Module m))
{
return 0;
}
|