Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: PATCH: gdbm bindings



ztie -d db/gdbm -f /tmp/data.base hokeypokey
print -l -- ${hokeypokey}

Those option letters could probably be changed to something more
sensible too.

diff --git a/Src/Modules/db_gdbm.c b/Src/Modules/db_gdbm.c
index 33a4712..f0915e0 100644
--- a/Src/Modules/db_gdbm.c
+++ b/Src/Modules/db_gdbm.c
@@ -43,11 +43,10 @@ static const struct gsu_hash gdbm_gsu =
 { gdbmgetfn, gdbmsetfn, gdbmunsetfn };
 
 static struct builtin bintab[] = {
-    BUILTIN("ztie", 0, bin_ztie, 1, -1, 0, "d:", NULL),
+    BUILTIN("ztie", 0, bin_ztie, 1, -1, 0, "d:f:", NULL),
     BUILTIN("zuntie", 0, bin_zuntie, 1, -1, 0, NULL, NULL),
 };
 
-static char resource_name[] = "/tmp/db_test";
 GDBM_FILE dbf = NULL;
 Param tied_param;
 
@@ -55,25 +54,29 @@ Param tied_param;
 static int
 bin_ztie(char *nam, char **args, Options ops, UNUSED(int func))
 {
+    char *resource_name, *pmname;
+
     if(!OPT_ISSET(ops,'d')) {
         zwarnnam(nam, "you must pass `-d db/gdbm' to ztie", NULL);
 	return 1;
     }
+    if(!OPT_ISSET(ops,'f')) {
+        zwarnnam(nam, "you must pass `-f' with a filename to ztie", NULL);
+	return 1;
+    }
 
     /* Here should be a lookup of the backend type against
      * a registry.
      */
 
-    /* Here should be an assignment of a resource name
-     * for the backend.
-     */
-
     if(dbf) {
         zwarnnam(nam, "something is already ztied and this implementation is flawed", NULL);
 	return 1;
     }
 
-    char *pmname = ztrdup(*args);
+    pmname = ztrdup(*args);
+
+    resource_name = OPT_ARG(ops, 'f');
 
     if (!(tied_param = createspecialhash(pmname, &getgdbmnode, &scangdbmkeys, PM_SPECIAL | PM_HASHED))) {
         zwarnnam(nam, "cannot create the requested parameter name", NULL);
@@ -82,7 +85,7 @@ bin_ztie(char *nam, char **args, Options ops, UNUSED(int func))
 
     dbf = gdbm_open(resource_name, 0, GDBM_WRCREAT | GDBM_SYNC, 0666, 0);
     if(!dbf) {
-        zwarnnam(nam, "error opening database file", NULL);
+        zwarnnam(nam, "error opening database file %s", resource_name);
 	return 1;
     }
 
@@ -93,10 +96,11 @@ bin_ztie(char *nam, char **args, Options ops, UNUSED(int func))
 static int
 bin_zuntie(char *nam, char **args, Options ops, UNUSED(int func))
 {
-    /* should untie the argument */
-
     paramtab->removenode(paramtab, tied_param->node.nam);
+    free(tied_param);
+    tied_param = NULL;
     gdbm_close(dbf);
+    dbf = NULL;
 
     return 0;
 }



Messages sorted by: Reverse Date, Date, Thread, Author