From 1884f57a748812df03d8eb7ccbf6439d7ab834c5 Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Wed, 20 Jul 2022 04:10:57 +0200 Subject: [PATCH] Streamline some nval(3) and related flaggery In the olden days, ksh used the hash(3) library to store variables, aliases, functions, etc. For many years, it's been using the cdt(3) library instead. But the low-level nv_search() name-value tree lookup function was still repurposing some bit flags from the old hash API for its options, though that API is otherwise unused. So we were still including the entire obsolete API just to use two repurposed HASH_* bit flags for nv_search(). This commit makes nv_search() repurpose some flags from instead. This commit should not change ksh's behaviour. src/cmd/ksh93/sh/nvdisc.c: - Make nv_search() use NV_NOSCOPE instead of HASH_NOSCOPE and NV_REF instead of HASH_BUCKET. - The HASH_SCOPE flag was also passed to some nv_search() calls, but nv_search() ignores it, so that flag is deleted from those. - Document nv_search() in a comment. src/cmd/ksh93/include/name.h: - Move NV_UNATTR to nval.h to join the other nv_open() options there. (re: 1184b2ad) src/cmd/ksh93/include/nval.h: - Since we no longer use HASH_* macros, do not include . - Remove unused NV_NOASSIGN macro, defined to 0. This was there for "backward compatibility" since before 1995; long enough. src/cmd/ksh93/include/shell.h: - Bump SH_VERSION due to the nv_search() API change (even though no changes were made to the APIs documented in nval.3 or shell.3). All other changed files: - Update to match the flaggery changes. --- src/cmd/ksh93/Mamfile | 4 ---- src/cmd/ksh93/bltins/getopts.c | 2 +- src/cmd/ksh93/bltins/mkservice.c | 2 +- src/cmd/ksh93/bltins/print.c | 4 ++-- src/cmd/ksh93/bltins/read.c | 2 +- src/cmd/ksh93/bltins/typeset.c | 12 ++++++------ src/cmd/ksh93/edit/completion.c | 2 +- src/cmd/ksh93/include/name.h | 1 - src/cmd/ksh93/include/nval.h | 3 +-- src/cmd/ksh93/include/shell.h | 2 +- src/cmd/ksh93/sh/args.c | 2 +- src/cmd/ksh93/sh/arith.c | 8 ++++---- src/cmd/ksh93/sh/array.c | 6 +++--- src/cmd/ksh93/sh/io.c | 2 +- src/cmd/ksh93/sh/lex.c | 2 +- src/cmd/ksh93/sh/macro.c | 2 +- src/cmd/ksh93/sh/name.c | 22 +++++++++++----------- src/cmd/ksh93/sh/nvdisc.c | 21 ++++++++++++++++----- src/cmd/ksh93/sh/nvtree.c | 12 ++++++------ src/cmd/ksh93/sh/path.c | 2 +- src/cmd/ksh93/sh/xec.c | 30 +++++++++++++++--------------- 21 files changed, 74 insertions(+), 69 deletions(-) diff --git a/src/cmd/ksh93/Mamfile b/src/cmd/ksh93/Mamfile index 0ed601706..3eaa45c41 100644 --- a/src/cmd/ksh93/Mamfile +++ b/src/cmd/ksh93/Mamfile @@ -198,10 +198,6 @@ make install done ${PACKAGE_ast_INCLUDE}/cmd.h dontcare prev ${PACKAGE_ast_INCLUDE}/shcmd.h implicit make include/nval.h implicit - make ${PACKAGE_ast_INCLUDE}/hash.h implicit - make ${PACKAGE_ast_INCLUDE}/hashpart.h implicit - done ${PACKAGE_ast_INCLUDE}/hashpart.h dontcare - done ${PACKAGE_ast_INCLUDE}/hash.h dontcare prev ${PACKAGE_ast_INCLUDE}/option.h implicit make ${PACKAGE_ast_INCLUDE}/cdt.h implicit prev ${PACKAGE_ast_INCLUDE}/ast_common.h implicit diff --git a/src/cmd/ksh93/bltins/getopts.c b/src/cmd/ksh93/bltins/getopts.c index ef2f08f49..c26d9e6fe 100644 --- a/src/cmd/ksh93/bltins/getopts.c +++ b/src/cmd/ksh93/bltins/getopts.c @@ -91,7 +91,7 @@ int b_getopts(int argc,char *argv[],Shbltin_t *context) error_info.context->flags |= ERROR_SILENT; error_info.id = options; options = argv[0]; - np = nv_open(argv[1],sh.var_tree,NV_NOASSIGN|NV_VARNAME); + np = nv_open(argv[1],sh.var_tree,NV_VARNAME); if(argc>2) { argv +=1; diff --git a/src/cmd/ksh93/bltins/mkservice.c b/src/cmd/ksh93/bltins/mkservice.c index 944b6a5b7..d8e21eef2 100644 --- a/src/cmd/ksh93/bltins/mkservice.c +++ b/src/cmd/ksh93/bltins/mkservice.c @@ -454,7 +454,7 @@ int b_mkservice(int argc, char** argv, Shbltin_t *context) close(fd); else sp->fd = fd; - np = nv_open(var,sh.var_tree,NV_ARRAY|NV_VARNAME|NV_NOASSIGN); + np = nv_open(var,sh.var_tree,NV_ARRAY|NV_VARNAME); sp->node = np; nv_putval(np, path, 0); nv_stack(np, (Namfun_t*)sp); diff --git a/src/cmd/ksh93/bltins/print.c b/src/cmd/ksh93/bltins/print.c index 46002f1bd..f5d2d8866 100644 --- a/src/cmd/ksh93/bltins/print.c +++ b/src/cmd/ksh93/bltins/print.c @@ -558,7 +558,7 @@ static ssize_t fmtbase64(Sfio_t *iop, char *string, int alt) char *cp; Sfdouble_t d; ssize_t size; - Namval_t *np = nv_open(string, NiL, NV_VARNAME|NV_NOASSIGN|NV_NOADD); + Namval_t *np = nv_open(string, NiL, NV_VARNAME|NV_NOADD); Namarr_t *ap; static union types_t number; if(!np || nv_isnull(np)) @@ -795,7 +795,7 @@ static int extend(Sfio_t* sp, void* v, Sffmt_t* fe) case 'n': { Namval_t *np; - np = nv_open(argp,sh.var_tree,NV_VARNAME|NV_NOASSIGN|NV_NOARRAY); + np = nv_open(argp,sh.var_tree,NV_VARNAME|NV_NOARRAY); _nv_unset(np,0); nv_onattr(np,NV_INTEGER); if (np->nvalue.lp = new_of(int32_t,0)) diff --git a/src/cmd/ksh93/bltins/read.c b/src/cmd/ksh93/bltins/read.c index d78f206de..61e64d96d 100644 --- a/src/cmd/ksh93/bltins/read.c +++ b/src/cmd/ksh93/bltins/read.c @@ -801,7 +801,7 @@ int sh_readline(char **names, volatile int fd, int flags, ssize_t size, long tim { if(name) { - np = nv_open(name,sh.var_tree,NV_NOASSIGN|NV_VARNAME); + np = nv_open(name,sh.var_tree,NV_VARNAME); name = *++names; } else diff --git a/src/cmd/ksh93/bltins/typeset.c b/src/cmd/ksh93/bltins/typeset.c index 82d8894b4..4c99c7106 100644 --- a/src/cmd/ksh93/bltins/typeset.c +++ b/src/cmd/ksh93/bltins/typeset.c @@ -529,12 +529,12 @@ endargs: if(NV_CLASS[sizeof(NV_CLASS)-2]!='.') sfputc(stkp,'.'); sfputr(stkp,tdata.prefix,0); - tdata.tp = nv_open(stkptr(stkp,offset),sh.var_tree,NV_VARNAME|NV_NOARRAY|NV_NOASSIGN); + tdata.tp = nv_open(stkptr(stkp,offset),sh.var_tree,NV_VARNAME|NV_NOARRAY); #if SHOPT_NAMESPACE if(!tdata.tp && off) { *stkptr(stkp,off)=0; - tdata.tp = nv_open(stkptr(stkp,offset),sh.var_tree,NV_VARNAME|NV_NOARRAY|NV_NOASSIGN); + tdata.tp = nv_open(stkptr(stkp,offset),sh.var_tree,NV_VARNAME|NV_NOARRAY); } #endif /* SHOPT_NAMESPACE */ stkseek(stkp,offset); @@ -699,7 +699,7 @@ static int setall(char **argv,register int flag,Dt_t *troot,struct tdata *tp } #if SHOPT_NAMESPACE if(sh.namespace) - np = sh_fsearch(name,NV_ADD|HASH_NOSCOPE); + np = sh_fsearch(name,NV_ADD|NV_NOSCOPE); else #endif /* SHOPT_NAMESPACE */ np = nv_open(name,sh_subfuntree(1),NV_NOARRAY|NV_IDENT|NV_NOSCOPE); @@ -714,7 +714,7 @@ static int setall(char **argv,register int flag,Dt_t *troot,struct tdata *tp #if SHOPT_NAMESPACE np = 0; if(sh.namespace) - np = sh_fsearch(name,HASH_NOSCOPE); + np = sh_fsearch(name,NV_NOSCOPE); if(!np) #endif /* SHOPT_NAMESPACE */ { @@ -761,7 +761,7 @@ static int setall(char **argv,register int flag,Dt_t *troot,struct tdata *tp /* tracked alias */ if(troot==sh.track_tree && tp->aflag=='-') { - np = nv_search(name,troot,NV_ADD|HASH_NOSCOPE); + np = nv_search(name,troot,NV_ADD|NV_NOSCOPE); path_alias(np,path_absolute(nv_name(np),NIL(Pathcomp_t*),0)); continue; } @@ -1349,7 +1349,7 @@ static int unall(int argc, char **argv, register Dt_t *troot) { #if SHOPT_NAMESPACE if(sh.namespace && troot!=sh.var_tree) - np = sh_fsearch(name,nflag?HASH_NOSCOPE:0); + np = sh_fsearch(name,nflag); if(!np) #endif /* SHOPT_NAMESPACE */ np=nv_open(name,troot,NV_NOADD|nflag); diff --git a/src/cmd/ksh93/edit/completion.c b/src/cmd/ksh93/edit/completion.c index fd4137b6c..661012cda 100644 --- a/src/cmd/ksh93/edit/completion.c +++ b/src/cmd/ksh93/edit/completion.c @@ -580,7 +580,7 @@ int ed_macro(Edit_t *ep, register int i) ep->e_macro[2] = ed_getchar(ep,1); else ep->e_macro[2] = 0; - if (isalnum(i)&&(np=nv_search(ep->e_macro,sh.alias_tree,HASH_SCOPE))&&(out=nv_getval(np))) + if (isalnum(i)&&(np=nv_search(ep->e_macro,sh.alias_tree,0))&&(out=nv_getval(np))) { #if SHOPT_MULTIBYTE /* copy to buff in internal representation */ diff --git a/src/cmd/ksh93/include/name.h b/src/cmd/ksh93/include/name.h index 5710c00ee..804169f3c 100644 --- a/src/cmd/ksh93/include/name.h +++ b/src/cmd/ksh93/include/name.h @@ -126,7 +126,6 @@ struct Ufunction #define NV_TYPE 0x1000000 #define NV_STATIC 0x2000000 #define NV_COMVAR 0x4000000 -#define NV_UNATTR 0x800000 /* unset attributes before assignment */ #define NV_FUNCTION (NV_RJUST|NV_FUNCT) /* value is shell function */ #define NV_FPOSIX NV_LJUST /* POSIX function semantics */ #define NV_FTMP NV_ZFILL /* function source in tmpfile */ diff --git a/src/cmd/ksh93/include/nval.h b/src/cmd/ksh93/include/nval.h index 2a0b73cde..6d9963fdb 100644 --- a/src/cmd/ksh93/include/nval.h +++ b/src/cmd/ksh93/include/nval.h @@ -32,7 +32,6 @@ #include #include #include -#include typedef struct Namval Namval_t; typedef struct Namfun Namfun_t; @@ -174,7 +173,6 @@ struct Namval #define NV_ADD 8 /* add node if not found */ #define NV_ASSIGN NV_NOFREE /* assignment is possible */ -#define NV_NOASSIGN 0 /* backward compatibility */ #define NV_NOARRAY 0x200000 /* array name not possible */ #define NV_IARRAY 0x400000 /* for indexed array */ #define NV_NOREF NV_REF /* don't follow reference */ @@ -184,6 +182,7 @@ struct Namval #define NV_NOSCOPE 0x80000 /* look only in current scope */ #define NV_NOFAIL 0x100000 /* return 0 on failure, no msg */ #define NV_NODISC NV_IDENT /* ignore disciplines */ +#define NV_UNATTR 0x800000 /* unset attributes before assignment */ #define NV_GLOBAL 0x20000000 /* create global variable, ignoring local scope */ #define NV_FUNCT NV_IDENT /* option for nv_create */ diff --git a/src/cmd/ksh93/include/shell.h b/src/cmd/ksh93/include/shell.h index f409b16d3..bf215ef9b 100644 --- a/src/cmd/ksh93/include/shell.h +++ b/src/cmd/ksh93/include/shell.h @@ -28,7 +28,7 @@ * */ -#define SH_VERSION 20220106 +#define SH_VERSION 20220719 #include #include diff --git a/src/cmd/ksh93/sh/args.c b/src/cmd/ksh93/sh/args.c index b37f32f3f..18c76efe6 100644 --- a/src/cmd/ksh93/sh/args.c +++ b/src/cmd/ksh93/sh/args.c @@ -141,7 +141,7 @@ int sh_argopts(int argc,register char *argv[]) switch(n) { case 'A': - np = nv_open(opt_info.arg,sh.var_tree,NV_NOASSIGN|NV_ARRAY|NV_VARNAME); + np = nv_open(opt_info.arg,sh.var_tree,NV_ARRAY|NV_VARNAME); if(f) nv_unset(np); continue; diff --git a/src/cmd/ksh93/sh/arith.c b/src/cmd/ksh93/sh/arith.c index 85f1f64f5..1918b7772 100644 --- a/src/cmd/ksh93/sh/arith.c +++ b/src/cmd/ksh93/sh/arith.c @@ -67,7 +67,7 @@ static Namval_t *scope(register Namval_t *np,register struct lval *lvalue,int as Dt_t *sdict = (sh.st.real_fun? sh.st.real_fun->sdict:0); Dt_t *nsdict = (sh.namespace?nv_dict(sh.namespace):0); Dt_t *root = sh.var_tree; - assign = assign?NV_ASSIGN:NV_NOASSIGN; + assign = assign?NV_ASSIGN:0; lvalue->nosub = 0; if(nosub<0 && lvalue->ovalue) return((Namval_t*)lvalue->ovalue); @@ -101,9 +101,9 @@ static Namval_t *scope(register Namval_t *np,register struct lval *lvalue,int as } if((lvalue->emode & ARITH_COMP) && dtvnext(root)) { - if(mp = nv_search(cp, sdict ? sdict : root, HASH_NOSCOPE|HASH_SCOPE|HASH_BUCKET)) + if(mp = nv_search(cp, sdict ? sdict : root, NV_NOSCOPE|NV_REF)) np = mp; - else if(nsdict && (mp = nv_search(cp, nsdict, HASH_SCOPE|HASH_BUCKET))) + else if(nsdict && (mp = nv_search(cp, nsdict, NV_REF))) np = mp; } while(nv_isref(np)) @@ -365,7 +365,7 @@ static Sfdouble_t arith(const char **ptr, struct lval *lvalue, int type, Sfdoubl np = &NaNnod; nv_onattr(np,NV_NOFREE|NV_LDOUBLE|NV_RDONLY); } - else if(!(np = nv_open(*ptr,root,NV_NOREF|NV_NOASSIGN|NV_VARNAME|dot))) + else if(!(np = nv_open(*ptr,root,NV_NOREF|NV_VARNAME|dot))) { lvalue->value = (char*)*ptr; lvalue->flag = str-lvalue->value; diff --git a/src/cmd/ksh93/sh/array.c b/src/cmd/ksh93/sh/array.c index 4705ff7d8..2f73d1108 100644 --- a/src/cmd/ksh93/sh/array.c +++ b/src/cmd/ksh93/sh/array.c @@ -1744,9 +1744,9 @@ void *nv_associative(register Namval_t *np,const char *sp,int mode) return(0); type = nv_isattr(np,NV_PUBLIC&~(NV_ARRAY|NV_CHILD|NV_MINIMAL)); if(mode) - mode = NV_ADD|HASH_NOSCOPE; + mode = NV_ADD|NV_NOSCOPE; else if(ap->header.nelem&ARRAY_NOSCOPE) - mode = HASH_NOSCOPE; + mode = NV_NOSCOPE; if(*sp==0 && sh_isoption(SH_XTRACE) && (mode&NV_ADD)) errormsg(SH_DICT,ERROR_warn(0),"adding empty subscript"); if(sh.subshell && (mp=nv_search(sp,ap->header.table,0)) && nv_isnull(mp)) @@ -1781,7 +1781,7 @@ void *nv_associative(register Namval_t *np,const char *sp,int mode) ap->nextpos = (Namval_t*)dtnext(ap->header.table,mp); } else if(!mp && *sp && mode==0) - mp = nv_search(sp,ap->header.table,NV_ADD|HASH_NOSCOPE); + mp = nv_search(sp,ap->header.table,NV_ADD|NV_NOSCOPE); np = mp; if(ap->pos && ap->pos==np) ap->header.nelem |= ARRAY_SCAN; diff --git a/src/cmd/ksh93/sh/io.c b/src/cmd/ksh93/sh/io.c index a6e12eef9..602195bf6 100644 --- a/src/cmd/ksh93/sh/io.c +++ b/src/cmd/ksh93/sh/io.c @@ -1183,7 +1183,7 @@ int sh_redirect(struct ionod *iop, int flag) np = 0; if(iop->iovname) { - np = nv_open(iop->iovname,sh.var_tree,NV_NOASSIGN|NV_VARNAME); + np = nv_open(iop->iovname,sh.var_tree,NV_VARNAME); if(nv_isattr(np,NV_RDONLY)) { errormsg(SH_DICT,ERROR_exit(1),e_readonly, nv_name(np)); diff --git a/src/cmd/ksh93/sh/lex.c b/src/cmd/ksh93/sh/lex.c index 3062d1abe..a4efe8427 100644 --- a/src/cmd/ksh93/sh/lex.c +++ b/src/cmd/ksh93/sh/lex.c @@ -1490,7 +1490,7 @@ breakloop: /* check for aliases */ Namval_t* np; if(!lp->lex.incase && !assignment && fcpeek(0)!=LPAREN && - (np=nv_search(state,sh.alias_tree,HASH_SCOPE)) + (np=nv_search(state,sh.alias_tree,0)) && !nv_isattr(np,NV_NOEXPAND) && (lp->aliasok!=2 || nv_isattr(np,BLT_DCL)) && (!sh_isstate(SH_NOALIAS) || nv_isattr(np,NV_NOFREE)) diff --git a/src/cmd/ksh93/sh/macro.c b/src/cmd/ksh93/sh/macro.c index c139ed285..6a2d7efa5 100644 --- a/src/cmd/ksh93/sh/macro.c +++ b/src/cmd/ksh93/sh/macro.c @@ -1317,7 +1317,7 @@ retry1: } goto nosub; } - flag |= NV_NOASSIGN|NV_VARNAME|NV_NOADD; + flag |= NV_VARNAME|NV_NOADD; if(c=='=' || c=='?' || (c==':' && ((d=fcpeek(0))=='=' || d=='?'))) { if(c=='=' || (c==':' && d=='=')) diff --git a/src/cmd/ksh93/sh/name.c b/src/cmd/ksh93/sh/name.c index 6d0651464..c7e97ffca 100644 --- a/src/cmd/ksh93/sh/name.c +++ b/src/cmd/ksh93/sh/name.c @@ -575,7 +575,7 @@ void nv_setlist(register struct argnod *arg,register int flags, Namval_t *typ) *eqp = '\0'; if((np = nv_search(cp,dtvnext(vartree),0)) && np->nvflag) { - mp = nv_search(cp,vartree,NV_ADD|HASH_NOSCOPE); + mp = nv_search(cp,vartree,NV_ADD|NV_NOSCOPE); mp->nvname = np->nvname; /* put_lang() (init.c) compares nvname pointers */ mp->nvflag = np->nvflag; mp->nvsize = np->nvsize; @@ -785,7 +785,7 @@ Namval_t *nv_create(const char *name, Dt_t *root, int flags, Namfun_t *dp) dp->last = cp; mode = (c=='.' || (flags&NV_NOADD))?add:NV_ADD; if(level++ || ((flags&NV_NOSCOPE) && c!='.')) - mode |= HASH_NOSCOPE; + mode |= NV_NOSCOPE; np=0; if(top) { @@ -806,7 +806,7 @@ Namval_t *nv_create(const char *name, Dt_t *root, int flags, Namfun_t *dp) #endif /* SHOPT_NAMESPACE */ { #if SHOPT_NAMESPACE - if(!(nq = nv_search((char*)np,sh.var_base,HASH_BUCKET))) + if(!(nq = nv_search((char*)np,sh.var_base,NV_REF))) #endif /* SHOPT_NAMESPACE */ nq = np; sh.last_root = sh.var_tree->walk; @@ -839,7 +839,7 @@ Namval_t *nv_create(const char *name, Dt_t *root, int flags, Namfun_t *dp) np = 0; } if(!np || sh.var_tree->walk!=root) - np = nv_search(name,root,HASH_NOSCOPE|NV_ADD); + np = nv_search(name,root,NV_NOSCOPE|NV_ADD); } } #if SHOPT_NAMESPACE @@ -1026,7 +1026,7 @@ Namval_t *nv_create(const char *name, Dt_t *root, int flags, Namfun_t *dp) { int n = 0; sub = 0; - mode &= ~HASH_NOSCOPE; + mode &= ~NV_NOSCOPE; if(c=='[') { #if SHOPT_FIXEDARRAY @@ -1343,7 +1343,7 @@ Namval_t *nv_open(const char *name, Dt_t *root, int flags) { long mode = ((flags&NV_NOADD)?0:NV_ADD); if(flags&NV_NOSCOPE) - mode |= HASH_SCOPE|HASH_NOSCOPE; + mode |= NV_NOSCOPE; np = nv_search(name,root,mode); if(np && !(flags&NV_REF)) { @@ -1443,7 +1443,7 @@ nocache: #endif if(fname) { - c = ((flags&NV_NOSCOPE)?HASH_NOSCOPE:0)|((flags&NV_NOADD)?0:NV_ADD); + c = (flags&NV_NOSCOPE)|((flags&NV_NOADD)?0:NV_ADD); *fname = '.'; np = nv_search(name, funroot, c); *fname = 0; @@ -1510,7 +1510,7 @@ skip: { if(mp=nv_opensub(np)) np = mp; - else if(!array_assoc(ap) && (mp = nv_open(cp,sh.var_tree,NV_NOFAIL|NV_VARNAME|NV_NOARRAY|NV_NOASSIGN|NV_NOADD)) && nv_isvtree(np)) + else if(!array_assoc(ap) && (mp = nv_open(cp,sh.var_tree,NV_NOFAIL|NV_VARNAME|NV_NOARRAY|NV_NOADD)) && nv_isvtree(np)) { ap->nelem |= ARRAY_TREE; nv_putsub(np,(char*)0,ARRAY_ADD|nv_aindex(np)); @@ -1524,7 +1524,7 @@ skip: nv_putval(np, cp, c); if(isref) { - if(nv_search((char*)np,sh.var_base,HASH_BUCKET)) + if(nv_search((char*)np,sh.var_base,NV_REF)) sh.last_root = sh.var_base; nv_setref(np,(Dt_t*)0,NV_VARNAME); } @@ -2260,7 +2260,7 @@ void nv_rehash(register Namval_t *np,void *data) if(sh.subshell) { /* invalidate subshell node; if none exists, add a dummy */ - np = nv_search(nv_name(np),sh.track_tree,NV_ADD|HASH_NOSCOPE); + np = nv_search(nv_name(np),sh.track_tree,NV_ADD|NV_NOSCOPE); } nv_onattr(np,NV_NOALIAS); } @@ -3369,7 +3369,7 @@ void nv_setref(register Namval_t *np, Dt_t *hp, int flags) UNREACHABLE(); } /* bind to earlier scope, or add to global scope */ - if(!(hp=dtvnext(hp)) || (nq=nv_search((char*)np,hp,NV_ADD|HASH_BUCKET))==np) + if(!(hp=dtvnext(hp)) || (nq=nv_search((char*)np,hp,NV_ADD|NV_REF))==np) { errormsg(SH_DICT,ERROR_exit(1),e_selfref,nv_name(np)); UNREACHABLE(); diff --git a/src/cmd/ksh93/sh/nvdisc.c b/src/cmd/ksh93/sh/nvdisc.c index e543853d9..70acaae5d 100644 --- a/src/cmd/ksh93/sh/nvdisc.c +++ b/src/cmd/ksh93/sh/nvdisc.c @@ -941,13 +941,24 @@ int nv_clone(Namval_t *np, Namval_t *mp, int flags) return(1); } +/* + * Low-level function to look up in . Returns a pointer to the + * name-value node found, or NULL if not found. is an options bitmask: + * - If NV_NOSCOPE is set, the search is only done in itself and + * not in any trees it has a view to (i.e. is connected to through scoping). + * - If NV_ADD is set and is not fond, a node by that name is created + * in and a pointer to the newly created node is returned. + * - If NV_REF is set, is a pointer to a name-value node, and that + * node's name is used. + * Note: The mode bitmask is NOT compatible with nv_open's flags bitmask. + */ Namval_t *nv_search(const char *name, Dt_t *root, int mode) { register Namval_t *np; register Dt_t *dp = 0; - if(mode&HASH_NOSCOPE) + if(mode&NV_NOSCOPE) dp = dtview(root,0); - if(mode&HASH_BUCKET) + if(mode&NV_REF) { Namval_t *mp = (void*)name; if(!(np = dtsearch(root,mp)) && (mode&NV_ADD)) @@ -961,9 +972,9 @@ Namval_t *nv_search(const char *name, Dt_t *root, int mode) } if(!np && (mode&NV_ADD)) { - if(sh.namespace && !(mode&HASH_NOSCOPE) && root==sh.var_tree) + if(sh.namespace && !(mode&NV_NOSCOPE) && root==sh.var_tree) root = nv_dict(sh.namespace); - else if(!dp && !(mode&HASH_NOSCOPE)) + else if(!dp && !(mode&NV_NOSCOPE)) { register Dt_t *next; while(next=dtvnext(root)) @@ -1025,7 +1036,7 @@ Namval_t *nv_bfsearch(const char *name, Dt_t *root, Namval_t **var, char **last) *last = cp; c = *cp; *cp = 0; - nq=nv_open(stakptr(offset),0,NV_VARNAME|NV_NOASSIGN|NV_NOADD|NV_NOFAIL); + nq=nv_open(stakptr(offset),0,NV_VARNAME|NV_NOADD|NV_NOFAIL); *cp = c; if(!nq) { diff --git a/src/cmd/ksh93/sh/nvtree.c b/src/cmd/ksh93/sh/nvtree.c index 1c281767b..58611a8a9 100644 --- a/src/cmd/ksh93/sh/nvtree.c +++ b/src/cmd/ksh93/sh/nvtree.c @@ -676,7 +676,7 @@ static void outval(char *name, const char *vname, struct Walk *wp) Dt_t *root = wp->root?wp->root:sh.var_base; if(*name!='.' || vname[strlen(vname)-1]==']') mode = NV_ARRAY; - if(!(np=nv_open(vname,root,mode|NV_VARNAME|NV_NOADD|NV_NOASSIGN|NV_NOFAIL|wp->noscope))) + if(!(np=nv_open(vname,root,mode|NV_VARNAME|NV_NOADD|NV_NOFAIL|wp->noscope))) { sh.last_table = last_table; return; @@ -833,7 +833,7 @@ static char **genvalue(char **argv, const char *prefix, int n, struct Walk *wp) { Namval_t *np,*tp; *nextcp = 0; - np=nv_open(arg,wp->root,NV_VARNAME|NV_NOADD|NV_NOASSIGN|NV_NOFAIL|wp->noscope); + np=nv_open(arg,wp->root,NV_VARNAME|NV_NOADD|NV_NOFAIL|wp->noscope); if(!np || (nv_isarray(np) && (!(tp=nv_opensub(np)) || !nv_isvtree(tp)))) { *nextcp = '.'; @@ -870,7 +870,7 @@ static char **genvalue(char **argv, const char *prefix, int n, struct Walk *wp) { int k=1; Namarr_t *ap=0; - Namval_t *np = nv_open(arg,wp->root,NV_VARNAME|NV_NOADD|NV_NOASSIGN|wp->noscope); + Namval_t *np = nv_open(arg,wp->root,NV_VARNAME|NV_NOADD|wp->noscope); if(!np) continue; if((wp->array = nv_isarray(np)) && (ap=nv_arrayptr(np))) @@ -995,7 +995,7 @@ static char *walk_tree(register Namval_t *np, Namval_t *xp, int flags) Namval_t *nq, *mq; if(strlen(cp)<=len) continue; - nq = nv_open(cp,walk.root,NV_VARNAME|NV_NOADD|NV_NOASSIGN|NV_NOFAIL); + nq = nv_open(cp,walk.root,NV_VARNAME|NV_NOADD|NV_NOFAIL); if(!nq && (flags&NV_MOVE)) nq = nv_search(cp,walk.root,NV_NOADD); stakseek(0); @@ -1003,7 +1003,7 @@ static char *walk_tree(register Namval_t *np, Namval_t *xp, int flags) stakputs(cp+len); stakputc(0); sh.var_tree = save_tree; - mq = nv_open(stakptr(0),sh.prev_root,NV_VARNAME|NV_NOASSIGN|NV_NOFAIL); + mq = nv_open(stakptr(0),sh.prev_root,NV_VARNAME|NV_NOFAIL); sh.var_tree = dp; if(nq && mq) { @@ -1100,7 +1100,7 @@ static void put_tree(register Namval_t *np, const char *val, int flags,Namfun_t { Namval_t *last_table = sh.last_table; Dt_t *last_root = sh.last_root; - Namval_t *mp = val?nv_open(val,sh.var_tree,NV_VARNAME|NV_NOADD|NV_NOASSIGN|NV_ARRAY|NV_NOFAIL):0; + Namval_t *mp = val?nv_open(val,sh.var_tree,NV_VARNAME|NV_NOADD|NV_ARRAY|NV_NOFAIL):0; if(mp && nv_isvtree(mp)) { sh.prev_table = sh.last_table; diff --git a/src/cmd/ksh93/sh/path.c b/src/cmd/ksh93/sh/path.c index 121e880dd..50d6ad864 100644 --- a/src/cmd/ksh93/sh/path.c +++ b/src/cmd/ksh93/sh/path.c @@ -707,7 +707,7 @@ int path_search(register const char *name,Pathcomp_t **oldpp, int flag) } else if(pp && !sh_isstate(SH_DEFPATH) && *name!='/' && flag<3) { - if(np=nv_search(name,sh_subtracktree(1),NV_ADD|HASH_NOSCOPE)) + if(np=nv_search(name,sh_subtracktree(1),NV_ADD|NV_NOSCOPE)) path_alias(np,pp); } return(0); diff --git a/src/cmd/ksh93/sh/xec.c b/src/cmd/ksh93/sh/xec.c index 412f26a2e..1b1d4e330 100644 --- a/src/cmd/ksh93/sh/xec.c +++ b/src/cmd/ksh93/sh/xec.c @@ -760,7 +760,7 @@ static int set_instance(Namval_t *nq, Namval_t *node, struct Namref *nr) if((ap=nv_arrayptr(nq)) && (sp = nv_getsub(nq))) sp = sh_strdup(sp); sh.instance = 0; - if(sh.var_tree!=sh.var_base && !nv_search((char*)nq,nr->root,HASH_BUCKET|HASH_NOSCOPE)) + if(sh.var_tree!=sh.var_base && !nv_search((char*)nq,nr->root,NV_REF|NV_NOSCOPE)) { #if SHOPT_NAMESPACE nr->root = sh.namespace?nv_dict(sh.namespace):sh.var_base; @@ -837,8 +837,8 @@ static Namval_t *enter_namespace(Namval_t *nsp) oroot = nv_dict(onsp); if(!nsp) { - path = nv_search(PATHNOD->nvname,oroot,HASH_NOSCOPE); - fpath = nv_search(FPATHNOD->nvname,oroot,HASH_NOSCOPE); + path = nv_search(PATHNOD->nvname,oroot,NV_NOSCOPE); + fpath = nv_search(FPATHNOD->nvname,oroot,NV_NOSCOPE); } if(sh.var_tree==oroot) { @@ -857,9 +857,9 @@ static Namval_t *enter_namespace(Namval_t *nsp) } } sh.namespace = nsp; - if(path && (path = nv_search(PATHNOD->nvname,sh.var_tree,HASH_NOSCOPE)) && (val=nv_getval(path))) + if(path && (path = nv_search(PATHNOD->nvname,sh.var_tree,NV_NOSCOPE)) && (val=nv_getval(path))) nv_putval(path,val,NV_RDONLY); - if(fpath && (fpath = nv_search(FPATHNOD->nvname,sh.var_tree,HASH_NOSCOPE)) && (val=nv_getval(fpath))) + if(fpath && (fpath = nv_search(FPATHNOD->nvname,sh.var_tree,NV_NOSCOPE)) && (val=nv_getval(fpath))) nv_putval(fpath,val,NV_RDONLY); return(onsp); } @@ -1381,7 +1381,7 @@ int sh_exec(register const Shnode_t *t, int flags) np = sh_fsearch(com0,0); else #endif /* SHOPT_NAMESPACE */ - np = nv_search(com0,sh.fun_tree,HASH_NOSCOPE); + np = nv_search(com0,sh.fun_tree,NV_NOSCOPE); } if(!np->nvalue.ip) { @@ -1428,7 +1428,7 @@ int sh_exec(register const Shnode_t *t, int flags) if(cp) { *cp = 0; - namespace = nv_search(np->nvname,sh.var_base,HASH_NOSCOPE); + namespace = nv_search(np->nvname,sh.var_base,NV_NOSCOPE); *cp = '.'; } } @@ -2036,7 +2036,7 @@ int sh_exec(register const Shnode_t *t, int flags) args=sh_argbuild(&argn,tp,0); nargs = argn; } - np = nv_open(t->for_.fornam, sh.var_tree,NV_NOASSIGN|NV_NOARRAY|NV_VARNAME|NV_NOREF); + np = nv_open(t->for_.fornam, sh.var_tree,NV_NOARRAY|NV_VARNAME|NV_NOREF); nameref = nv_isref(np)!=0; sh.st.loopcnt++; cp = *args; @@ -2419,7 +2419,7 @@ int sh_exec(register const Shnode_t *t, int flags) Dt_t *root; Namval_t *oldnspace = sh.namespace; int offset = stktell(stkp); - int flags=NV_NOASSIGN|NV_NOARRAY|NV_VARNAME; + int flags=NV_NOARRAY|NV_VARNAME; struct checkpt *chkp = (struct checkpt*)stakalloc(sizeof(struct checkpt)); int jmpval; if(cp) @@ -2468,7 +2468,7 @@ int sh_exec(register const Shnode_t *t, int flags) { cp = sh.prefix; sh.prefix = 0; - npv = nv_open(cp,sh.var_tree,NV_NOASSIGN|NV_NOARRAY|NV_VARNAME); + npv = nv_open(cp,sh.var_tree,NV_NOARRAY|NV_VARNAME); sh.prefix = cp; cp = fname; } @@ -2476,7 +2476,7 @@ int sh_exec(register const Shnode_t *t, int flags) { sfwrite(stkp,fname,cp++-fname); sfputc(stkp,0); - npv = nv_open(stkptr(stkp,offset),sh.var_tree,NV_NOASSIGN|NV_NOARRAY|NV_VARNAME); + npv = nv_open(stkptr(stkp,offset),sh.var_tree,NV_NOARRAY|NV_VARNAME); } offset = stktell(stkp); sfprintf(stkp,"%s.%s%c",nv_name(npv),cp,0); @@ -2489,10 +2489,10 @@ int sh_exec(register const Shnode_t *t, int flags) } #if SHOPT_NAMESPACE if(sh.namespace && !sh.prefix && *fname!='.') - np = sh_fsearch(fname,NV_ADD|HASH_NOSCOPE); + np = sh_fsearch(fname,NV_ADD|NV_NOSCOPE); if(!np) #endif /* SHOPT_NAMESPACE */ - np = nv_open(fname,sh_subfuntree(1),NV_NOASSIGN|NV_NOARRAY|NV_VARNAME|NV_NOSCOPE); + np = nv_open(fname,sh_subfuntree(1),NV_NOARRAY|NV_VARNAME|NV_NOSCOPE); if(npv) { if(!sh.mktype) @@ -2986,7 +2986,7 @@ static void local_exports(register Namval_t *np, void *data) { register Namval_t *mp; NOT_USED(data); - if(!nv_isnull(np) && (mp = nv_search(nv_name(np), sh.var_tree, NV_ADD|HASH_NOSCOPE)) && nv_isnull(mp)) + if(!nv_isnull(np) && (mp = nv_search(nv_name(np), sh.var_tree, NV_ADD|NV_NOSCOPE)) && nv_isnull(mp)) nv_clone(np,mp,0); } @@ -3145,7 +3145,7 @@ int sh_funscope(int argn, char *argv[],int(*fun)(void*),void *arg,int execflg) sh.last_root = 0; for(r=0; arg[r]; r++) { - np = nv_search(arg[r],sh.var_tree,HASH_NOSCOPE|NV_ADD); + np = nv_search(arg[r],sh.var_tree,NV_NOSCOPE|NV_ADD); if(np && (nq=*nref++)) { np->nvalue.nrp = sh_newof(0,struct Namref,1,0);