From adcc9a1c228a3b7b38675541103ddd69c779d88e Mon Sep 17 00:00:00 2001 From: Philippe Altherr Date: Tue, 9 Jun 2026 21:06:12 -0700 Subject: 54731: Adapt gdbm code to named references --- Src/Modules/db_gdbm.c | 37 ++++++++++++++----------------------- Test/V11db_gdbm.ztst | 49 ++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 58 insertions(+), 28 deletions(-) diff --git a/Src/Modules/db_gdbm.c b/Src/Modules/db_gdbm.c index 604788ae8..09f8224f0 100644 --- a/Src/Modules/db_gdbm.c +++ b/Src/Modules/db_gdbm.c @@ -38,12 +38,6 @@ #define PM_UPTODATE PM_DONTIMPORT_SUID /* Safe PM_ bit to re-use */ #endif -static Param createhash( char *name, int flags ); -static int append_tied_name( const char *name ); -static int remove_tied_name( const char *name ); -static char *unmetafy_zalloc(const char *to_copy, int *new_len); -static void myfreeparamnode(HashNode hn); - static int no_database_action = 0; #include @@ -133,7 +127,7 @@ bin_ztie(char *nam, char **args, Options ops, UNUSED(int func)) resource_name = OPT_ARG(ops, 'f'); pmname = *args; - if ((tied_param = (Param)paramtab->getnode(paramtab, pmname)) && + if ((tied_param = (Param)realparamtab->getnode(realparamtab, pmname)) && !(tied_param->node.flags & PM_UNSET)) { /* * Unset any existing parameter. Note there's no implicit @@ -159,7 +153,7 @@ bin_ztie(char *nam, char **args, Options ops, UNUSED(int func)) return 1; } - if (!(tied_param = createhash(pmname, pmflags))) { + if (!(tied_param = creategdbmhash(pmname, pmflags))) { zwarnnam(nam, "cannot create the requested parameter %s", pmname); gdbm_close(dbf); return 1; @@ -185,7 +179,7 @@ bin_ztie(char *nam, char **args, Options ops, UNUSED(int func)) dbf_carrier->dbfile_path = ztrdup(resource_name); addmodulefd(gdbm_fdesc(dbf), FDT_INTERNAL); - append_tied_name(pmname); + append_tied_name(tied_param->node.nam); return 0; } @@ -199,7 +193,7 @@ bin_zuntie(char *nam, char **args, Options ops, UNUSED(int func)) int ret = 0; for (pmname = *args; *args++; pmname = *args) { - pm = (Param) paramtab->getnode(paramtab, pmname); + pm = (Param) realparamtab->getnode(realparamtab, pmname); if(!pm) { zwarnnam(nam, "cannot untie %s", pmname); ret = 1; @@ -239,7 +233,7 @@ bin_zgdbmpath(char *nam, char **args, UNUSED(Options ops), UNUSED(int func)) return 1; } - pm = (Param) paramtab->getnode(paramtab, pmname); + pm = (Param) realparamtab->getnode(realparamtab, pmname); if(!pm) { zwarnnam(nam, "no such parameter: %s", pmname); return 1; @@ -651,7 +645,8 @@ finish_(UNUSED(Module m)) * Utility functions * *********************/ -static Param createhash( char *name, int flags ) { +/**/ +static Param creategdbmhash( char *name, int flags ) { Param pm; HashTable ht; @@ -660,20 +655,11 @@ static Param createhash( char *name, int flags ) { return NULL; } - if (pm->old) - pm->level = locallevel; - /* This creates standard hash. */ ht = pm->u.hash = newparamtable(17, name); - if (!pm->u.hash) { - paramtab->removenode(paramtab, name); - paramtab->freenode(&pm->node); - zwarnnam(name, "out of memory when allocating hash"); - return NULL; - } /* Does free Param (unsetfn is called) */ - ht->freenode = myfreeparamnode; + ht->freenode = freegdbmnode; /* These provide special features */ ht->getnode = ht->getnode2 = getgdbmnode; @@ -686,6 +672,7 @@ static Param createhash( char *name, int flags ) { * Adds parameter name to `zgdbm_tied` */ +/**/ static int append_tied_name( const char *name ) { int old_len = arrlen(zgdbm_tied); char **new_zgdbm_tied = zshcalloc( (old_len+2) * sizeof(char *)); @@ -711,6 +698,7 @@ static int append_tied_name( const char *name ) { * Removes parameter name from `zgdbm_tied` */ +/**/ static int remove_tied_name( const char *name ) { int old_len = arrlen(zgdbm_tied); int new_len; @@ -766,6 +754,8 @@ static int remove_tied_name( const char *name ) { * - does zalloc of exact size for the new string, * - restores work buffer to original content, to restore strlen */ + +/**/ static char * unmetafy_zalloc(const char *to_copy, int *new_len) { char *work, *to_return; @@ -789,8 +779,9 @@ unmetafy_zalloc(const char *to_copy, int *new_len) { return to_return; } +/**/ static void -myfreeparamnode(HashNode hn) +freegdbmnode(HashNode hn) { Param pm = (Param) hn; diff --git a/Test/V11db_gdbm.ztst b/Test/V11db_gdbm.ztst index 6d74cef2c..1ac9fc0c2 100644 --- a/Test/V11db_gdbm.ztst +++ b/Test/V11db_gdbm.ztst @@ -204,34 +204,67 @@ local -a dbase dbase=( fromarray ) () { - local -a dbase + local dbase + () { ztie -d db/gdbm -f $dbfile dbase - echo ${(t)dbase} - zuntie dbase + echo "Connected:" $dbase[a] + } + echo "Still connected:" $dbase[a] } echo $dbase[1] ztie -d db/gdbm -f $dbfile dbase2 echo "Can connect, so untie happened:" $dbase2[a] zuntie dbase2 0:Test of automatic untie (use of local scope) and of scoping ->association-local-special +>Connected: b +>Still connected: b >fromarray >Can connect, so untie happened: b + local dbase + () { + local -n rbase=dbase + local dbase=not-a-dbase + ztie -d db/gdbm -f $dbfile rbase + echo $dbase + echo "Connected:" $rbase[a] + zuntie rbase + echo "Disconnected:" $rbase[a] + ztie -d db/gdbm -f $dbfile rbase + echo "Reconnected:" $rbase[a] + echo $dbase + } + echo "Still connected:" $dbase[a] + zuntie dbase +0:Test of tie to hidden local via named reference +>not-a-dbase +>Connected: b +>Disconnected: +>Reconnected: b +>not-a-dbase +>Still connected: b + echo $zgdbm_tied ${#zgdbm_tied} ztie -r -d db/gdbm -f $dbfile dbase echo $zgdbm_tied ${#zgdbm_tied} ztie -d db/gdbm -f ${dbfile}2 dbase2 echo $zgdbm_tied ${#zgdbm_tied} + local -n rbase3=dbase3 + ztie -d db/gdbm -f ${dbfile}3 rbase3 + echo $zgdbm_tied ${#zgdbm_tied} zuntie -u dbase echo $zgdbm_tied ${#zgdbm_tied} zuntie dbase2 echo $zgdbm_tied ${#zgdbm_tied} + zuntie rbase3 + echo $zgdbm_tied ${#zgdbm_tied} 0:zgdbm_tied parameter >0 >dbase 1 >dbase dbase2 2 ->dbase2 1 +>dbase dbase2 dbase3 3 +>dbase2 dbase3 2 +>dbase3 1 >0 unset zgdbm_tied 2>/dev/null @@ -311,8 +344,14 @@ zgdbmpath dbase [[ $REPLY = */Test/db.gdbm ]] && echo correct zuntie -u dbase + local -n rbase=dbase + ztie -r -d db/gdbm -f $dbfile rbase + zgdbmpath rbase + [[ $REPLY = */Test/db.gdbm ]] && echo correct + zuntie -u rbase 0:zgdbmpath builtin >correct +>correct >correct ztie -d db/gdbm -f $dbfile dbase -- cgit v1.3.1