Skip to content

Commit

Permalink
Rename char *nvenv to void *nvmeta (re: 4d68815)
Browse files Browse the repository at this point in the history
The previous commit removed the redundant use of the nvenv pointer
of struct Namval to point directly to values in environ[]. That was
its original purpose, but there are many remaining additional uses.
So far, I've discovered:
    - help string for type commands (typeset -T -h)
      (typeset.c, nvtype.c)
    - pointer to dimension 2+ array
      (array.c and loads of other places)
    - compound variable (NV_COMVAR, NV_TABLE)
      (arith.c, nvdisc.c, name.c)
    - struct slnod: linked list of stacks (for functions;
      see also sh_funstaks())
      (xec.c, name.c)
      name.c:2375
    - flaggery for builtins (purpose to be worked out):
      (nvdisc.c, path.c)

So, this follow-up commit has no actual code changes; it is for
code legibility. It:

- Removes the now-unused NV_IMPORT attribute. It shared a bit value
  with NV_MINIMAL, so the few remaining uses of it in the code has
  been changed to NV_MIMIMAL to avoid potential bugs.
- Renames nvenv to nvmeta, in recognition of the fact that it is
  used for many kinds of purposes.
- Makes nvmeta a void pointer instead of a char pointer.
- Accordingly removes now-redundant explicit typecasts.
  • Loading branch information
McDutchie committed Feb 29, 2024
1 parent 4d68815 commit 04b4868
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 81 deletions.
6 changes: 3 additions & 3 deletions src/cmd/ksh93/bltins/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,11 @@ int sh_readline(char **names, volatile int fd, int flags, ssize_t size, Sflong_t
}
else if(flags&C_FLAG)
{
char *sp = np->nvenv;
void *sp = np->nvmeta;
delim = -1;
nv_unset(np);
if(!nv_isattr(np,NV_MINIMAL))
np->nvenv = sp;
np->nvmeta = sp;
nv_setvtree(np);
}
else
Expand Down Expand Up @@ -499,7 +499,7 @@ int sh_readline(char **names, volatile int fd, int flags, ssize_t size, Sflong_t
var = sh_memdup(var,c+1);
nv_putval(np,var,NV_RAW);
nv_setsize(np,c);
if(!nv_isattr(np,NV_IMPORT|NV_EXPORT) && (mp=(Namval_t*)np->nvenv))
if(!nv_isattr(np,NV_MINIMAL|NV_EXPORT) && (mp = np->nvmeta))
nv_setsize(mp,c);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/ksh93/bltins/typeset.c
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ int b_typeset(int argc,char *argv[],Shbltin_t *context)
}
else if(nv_isnull(tdata.tp) && sh.envlist) /* only create a type command if there were assignment(s) */
nv_newtype(tdata.tp);
tdata.tp->nvenv = tdata.help;
tdata.tp->nvmeta = tdata.help;
flag &= ~NV_TYPE;
if(nv_isattr(tdata.tp,NV_TAGGED))
{
Expand Down Expand Up @@ -790,7 +790,7 @@ static int setall(char **argv,int flag,Dt_t *troot,struct tdata *tp)
}
if(nv_isnull(np) && !nv_isarray(np) && nv_isattr(np,NV_NOFREE))
nv_offattr(np,NV_NOFREE);
else if(tp->tp && !nv_isattr(np,NV_MINIMAL|NV_EXPORT) && (mp=(Namval_t*)np->nvenv) && (ap=nv_arrayptr(mp)) && (ap->nelem&ARRAY_TREE))
else if(tp->tp && !nv_isattr(np,NV_MINIMAL|NV_EXPORT) && (mp = np->nvmeta) && (ap = nv_arrayptr(mp)) && (ap->nelem & ARRAY_TREE))
{
errormsg(SH_DICT,ERROR_exit(1),e_typecompat,nv_name(np));
UNREACHABLE();
Expand Down Expand Up @@ -958,7 +958,7 @@ static int setall(char **argv,int flag,Dt_t *troot,struct tdata *tp)
}
if(tp->help && !nv_isattr(np,NV_MINIMAL|NV_EXPORT))
{
np->nvenv = tp->help;
np->nvmeta = tp->help;
nv_onattr(np,NV_EXPORT);
}
if(last)
Expand Down
8 changes: 4 additions & 4 deletions src/cmd/ksh93/include/name.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#define _NV_PRIVATE \
Namfun_t *nvfun; /* pointer to trap functions */ \
union Value nvalue; /* value field */ \
char *nvenv; /* pointer to environment name */
void *nvmeta; /* pointer to any of various kinds of type-dependent data */

#include <ast.h>
#include <cdt.h>
Expand Down Expand Up @@ -116,7 +116,7 @@ struct Ufunction
/* attributes of Namval_t items */

/* The following attributes are for internal use */
#define NV_NOCHANGE (NV_EXPORT|NV_IMPORT|NV_RDONLY|NV_TAGGED|NV_NOFREE|NV_ARRAY)
#define NV_NOCHANGE (NV_EXPORT|NV_MINIMAL|NV_RDONLY|NV_TAGGED|NV_NOFREE|NV_ARRAY)
#define NV_ATTRIBUTES (~(NV_NOSCOPE|NV_ARRAY|NV_NOARRAY|NV_IDENT|NV_ASSIGN|NV_REF|NV_VARNAME|NV_STATIC))
#define NV_PARAM NV_NODISC /* expansion use positional params */

Expand All @@ -130,15 +130,15 @@ struct Ufunction
#define NV_STATICF NV_INTEGER /* static class function */

#define NV_NOPRINT (NV_LTOU|NV_UTOL) /* do not print */
#define NV_NOALIAS (NV_NOPRINT|NV_IMPORT)
#define NV_NOALIAS (NV_NOPRINT|NV_MINIMAL)
#define NV_NOEXPAND NV_RJUST /* do not expand alias */
#define NV_BLTIN (NV_NOPRINT|NV_EXPORT)
#define BLT_ENV (NV_RDONLY) /* non-stoppable,
* can modify environment */
#define BLT_SPC (NV_LJUST) /* special built-ins */
#define BLT_EXIT (NV_RJUST) /* exit value can be > 255 or < 0 */
#define BLT_DCL (NV_TAGGED) /* declaration command */
#define BLT_NOSFIO (NV_IMPORT) /* doesn't use sfio */
#define BLT_NOSFIO (NV_MINIMAL) /* doesn't use sfio */
#define NV_OPTGET (NV_BINARY) /* function calls getopts */
#define nv_isref(n) (nv_isattr((n),NV_REF|NV_TAGGED|NV_FUNCT)==NV_REF)
#define is_abuiltin(n) (nv_isattr(n,NV_BLTIN|NV_INTEGER)==NV_BLTIN)
Expand Down
3 changes: 1 addition & 2 deletions src/cmd/ksh93/include/nval.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ struct Namval
#define NV_ARRAY 0x400 /* node is an array */
#define NV_REF 0x4000 /* reference bit */
#define NV_TABLE 0x800 /* node is a dictionary table */
#define NV_IMPORT 0x1000 /* value imported from environment */
#define NV_MINIMAL NV_IMPORT /* node does not contain all fields */
#define NV_MINIMAL 0x1000 /* node does not contain all fields */
#if SHOPT_OPTIMIZE
#define NV_NOOPTIMIZE NV_TABLE /* disable loop invariants optimizer */
#else
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/ksh93/sh/arith.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ static Namval_t *scope(Namval_t *np,struct lval *lvalue,int assign)
if(ap && !ap->table)
ap->table = dtopen(&_Nvdisc,Dtoset);
if(ap && ap->table && (nq=nv_search(nv_getsub(np),ap->table,NV_ADD)))
nq->nvenv = (char*)np;
nq->nvmeta = np;
if(nq && nv_isnull(nq))
np = nv_arraychild(np,nq,0);
}
Expand Down
16 changes: 8 additions & 8 deletions src/cmd/ksh93/sh/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ static Namval_t *array_find(Namval_t *np,Namarr_t *arp, int flag)
sfprintf(sh.strbuf,"%d",ap->cur);
cp = sfstruse(sh.strbuf);
mp = nv_search(cp, ap->header.table, NV_ADD);
mp->nvenv = (char*)np;
mp->nvmeta = np;
nv_arraychild(np,mp,0);
}
if(up->np && array_isbit(ap->bits,ap->cur,ARRAY_CHILD))
Expand Down Expand Up @@ -446,7 +446,7 @@ static Namfun_t *array_clone(Namval_t *np, Namval_t *mp, int flags, Namfun_t *fp
do
{
if(nq=nv_opensub(np))
nq->nvenv = (void*)mp;
nq->nvmeta = mp;
}
while(nv_nextsub(np));
}
Expand Down Expand Up @@ -790,7 +790,7 @@ static void array_copytree(Namval_t *np, Namval_t *mp)
nv_disc(np,(Namfun_t*)fp, NV_FIRST);
fp->nofree |= 1;
nv_onattr(np,NV_ARRAY);
mp->nvenv = (char*)np;
mp->nvmeta = np;
}

/*
Expand Down Expand Up @@ -1028,15 +1028,15 @@ Namval_t *nv_arraychild(Namval_t *np, Namval_t *nq, int c)
if((tp=nv_type(np)) || c)
{
ap->nelem |= ARRAY_NOCLONE;
nq->nvenv = (char*)np;
nq->nvmeta = np;
if(c=='t')
nv_clone(tp,nq, 0);
else
nv_clone(np, nq, NV_NODISC);
nv_offattr(nq,NV_ARRAY);
ap->nelem &= ~ARRAY_NOCLONE;
}
nq->nvenv = (char*)np;
nq->nvmeta = np;
if((fp=nq->nvfun) && fp->disc && fp->disc->setdisc && (fp = nv_disc(nq,fp,NV_POP)))
free(fp);
if(!ap->fun)
Expand Down Expand Up @@ -1237,7 +1237,7 @@ Namval_t *nv_putsub(Namval_t *np,char *sp,long mode)
sfprintf(sh.strbuf,"%d",ap->cur);
cp = sfstruse(sh.strbuf);
mp = nv_search(cp, ap->header.table, NV_ADD);
mp->nvenv = (char*)np;
mp->nvmeta = np;
nv_arraychild(np,mp,0);
nv_setvtree(mp);
}
Expand Down Expand Up @@ -1720,7 +1720,7 @@ void *nv_associative(Namval_t *np,const char *sp,int mode)
return ap->cur;
case NV_ACURRENT:
if(ap->cur)
ap->cur->nvenv = (char*)np;
ap->cur->nvmeta = np;
return ap->cur;
case NV_ANAME:
if(ap->cur)
Expand Down Expand Up @@ -1749,7 +1749,7 @@ void *nv_associative(Namval_t *np,const char *sp,int mode)
if((mp || (mp=nv_search(sp,ap->header.table,mode))) && nv_isnull(mp) && (mode&NV_ADD))
{
nv_onattr(mp,type);
mp->nvenv = (char*)np;
mp->nvmeta = np;
if((mode&NV_ADD) && nv_type(np))
nv_arraychild(np,mp,0);
if(sh.subshell)
Expand Down
8 changes: 4 additions & 4 deletions src/cmd/ksh93/sh/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ static void put_lastarg(Namval_t* np,const char *val,int flags,Namfun_t *fp)
nv_offattr(np,NV_NOFREE);
sh.lastarg = (char*)val;
nv_offattr(np,NV_EXPORT);
np->nvenv = 0;
np->nvmeta = NULL;
}

static void match2d(struct match *mp)
Expand Down Expand Up @@ -1950,7 +1950,7 @@ Dt_t *sh_inittree(const struct shtable2 *name_vals)
np->nvname = (char*)tp->sh_name;
treep = base_treep;
}
np->nvenv = 0;
np->nvmeta = NULL;
if(name_vals==(const struct shtable2*)shtab_builtins)
np->nvalue.bfp = (void*)((struct shtable3*)tp)->sh_value;
else
Expand Down Expand Up @@ -2032,7 +2032,7 @@ static void env_import_attributes(char *next)
if(next = strchr(++cp,'='))
*next = 0;
np = nv_search(cp+2,sh.var_tree,NV_ADD);
if(np!=SHLVL && nv_isattr(np,NV_IMPORT|NV_EXPORT))
if(np!=SHLVL && nv_isattr(np,NV_EXPORT))
{
int flag = *(unsigned char*)cp-' ';
int size = *(unsigned char*)(cp+1)-' ';
Expand Down Expand Up @@ -2064,7 +2064,7 @@ static void env_import_attributes(char *next)
flag &= ~NV_RDONLY; /* refuse to import readonly attribute */
if(!flag)
continue;
nv_newattr(np,flag|NV_IMPORT|NV_EXPORT,size);
nv_newattr(np,flag|NV_EXPORT,size);
}
}
return;
Expand Down
33 changes: 17 additions & 16 deletions src/cmd/ksh93/sh/name.c
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,7 @@ Namval_t *nv_create(const char *name, Dt_t *root, int flags, Namfun_t *dp)
if(n && ap && !ap->table)
ap->table = dtopen(&_Nvdisc,Dtoset);
if(ap && ap->table && (nq=nv_search(sub,ap->table,n)))
nq->nvenv = (char*)np;
nq->nvmeta = np;
if(nq && nv_isnull(nq))
nq = nv_arraychild(np,nq,c);
}
Expand Down Expand Up @@ -2404,7 +2404,7 @@ static void table_unset(Dt_t *root, int flags, Dt_t *oroot)
* will retain its attributes.
* <flags> can contain NV_RDONLY to override the readonly attribute
* being cleared.
* <flags> can contain NV_EXPORT to override preserve nvenv
* <flags> can contain NV_EXPORT to preserve nvmeta.
*/
void _nv_unset(Namval_t *np,int flags)
{
Expand All @@ -2421,7 +2421,7 @@ void _nv_unset(Namval_t *np,int flags)
}
if(is_afunction(np) && np->nvalue.ip)
{
struct slnod *slp = (struct slnod*)(np->nvenv);
struct slnod *slp = np->nvmeta;
if(np->nvalue.rp->running)
{
np->nvalue.rp->running |= 1;
Expand Down Expand Up @@ -2531,7 +2531,7 @@ void _nv_unset(Namval_t *np,int flags)
if(nv_isattr(np,NV_EXPORT) && !strchr(np->nvname,'['))
env_change();
if(!(flags&NV_EXPORT) || nv_isattr(np,NV_EXPORT))
np->nvenv = 0;
np->nvmeta = NULL;
nv_setattr(np,0);
}
else
Expand Down Expand Up @@ -3156,16 +3156,17 @@ int nv_rename(Namval_t *np, int flags)
Namval_t *last_table = sh.last_table;
Dt_t *last_root = sh.last_root;
Dt_t *hp = 0;
char *nvenv=0,*prefix=sh.prefix;
void *nvmeta = NULL;
char *prefix = sh.prefix;
Namarr_t *ap;
if(nv_isattr(np,NV_PARAM) && sh.st.prevst)
{
if(!(hp=(Dt_t*)sh.st.prevst->save_tree))
hp = dtvnext(sh.var_tree);
}
if(!nv_isattr(np,NV_MINIMAL))
nvenv = np->nvenv;
if(nvenv || (cp = nv_name(np)) && nv_isarray(np) && cp[strlen(cp)-1] == ']')
nvmeta = np->nvmeta;
if(nvmeta || (cp = nv_name(np)) && nv_isarray(np) && cp[strlen(cp)-1] == ']')
arraynp = 1;
if(!(cp=nv_getval(np)))
{
Expand Down Expand Up @@ -3215,12 +3216,12 @@ int nv_rename(Namval_t *np, int flags)
{
if(ap = nv_arrayptr(np))
ap->nelem++;
mp->nvenv = nvenv = (void*)np;
mp->nvmeta = nvmeta = np;
}
}
if(mp)
{
nvenv = (char*)np;
nvmeta = np;
np = mp;
}
if(nr==np)
Expand All @@ -3244,7 +3245,7 @@ int nv_rename(Namval_t *np, int flags)
sh.last_root = last_root;
if(flags&NV_MOVE)
{
if(arraynp && !nv_isattr(np,NV_MINIMAL) && (mp=(Namval_t*)np->nvenv) && (ap=nv_arrayptr(mp)) && !ap->fun)
if(arraynp && !nv_isattr(np,NV_MINIMAL) && (mp = np->nvmeta) && (ap = nv_arrayptr(mp)) && !ap->fun)
ap->nelem++;
}
if((nv_arrayptr(nr) && !arraynr) || nv_isvtree(nr))
Expand All @@ -3256,7 +3257,7 @@ int nv_rename(Namval_t *np, int flags)
if(ap->table)
mp = nv_search(nv_getsub(np),ap->table,NV_ADD);
nv_arraychild(np,mp,0);
nvenv = (void*)np;
nvmeta = np;
}
else
mp = np;
Expand All @@ -3282,10 +3283,10 @@ int nv_rename(Namval_t *np, int flags)
}
else
nv_clone(nr,mp,(flags&NV_MOVE)|NV_COMVAR);
mp->nvenv = nvenv;
mp->nvmeta = nvmeta;
if(flags&NV_MOVE)
{
if(arraynr && !nv_isattr(nr,NV_MINIMAL) && (mp=(Namval_t*)nr->nvenv) && (ap=nv_arrayptr(mp)))
if(arraynr && !nv_isattr(nr,NV_MINIMAL) && (mp = nr->nvmeta) && (ap = nv_arrayptr(mp)))
{
nv_putsub(mp,nr->nvname,0);
_nv_unset(mp,0);
Expand All @@ -3298,7 +3299,7 @@ int nv_rename(Namval_t *np, int flags)
nv_putval(np,nv_getval(nr),0);
if(flags&NV_MOVE)
{
if(!nv_isattr(nr,NV_MINIMAL) && (mp=(Namval_t*)(nr->nvenv)) && (ap=nv_arrayptr(mp)))
if(!nv_isattr(nr,NV_MINIMAL) && (mp = nr->nvmeta) && (ap = nv_arrayptr(mp)))
ap->nelem--;
_nv_unset(nr,0);
}
Expand Down Expand Up @@ -3552,9 +3553,9 @@ char *nv_name(Namval_t *np)
#if SHOPT_FIXEDARRAY
ap = nv_arrayptr(np);
#endif /* SHOPT_FIXEDARRAY */
if(!nv_isattr(np,NV_MINIMAL|NV_EXPORT) && np->nvenv)
if(!nv_isattr(np,NV_MINIMAL|NV_EXPORT) && np->nvmeta)
{
Namval_t *nq= sh.last_table, *mp= (Namval_t*)np->nvenv;
Namval_t *nq = sh.last_table, *mp = np->nvmeta;
if(np==sh.last_table)
sh.last_table = 0;
if(nv_isarray(mp))
Expand Down
Loading

0 comments on commit 04b4868

Please sign in to comment.