Skip to content

Commit

Permalink
WIP: Expand nvflags to uint64_t to increase the number of bits
Browse files Browse the repository at this point in the history
I loosely based the nvflag_t type and NV_ADD's unique bit on ksh2020
code. However, most of this code is from scratch (in part because the
ksh2020 attempt was incomplete and unportable, and also because newly
written de novo code is easier for me to debug via e.g. changing the
type to uint64_t and seeing what breaks).

Also backported a small clarification from ksh2020 commit 8943739.

----TODO FORMAT----
- nv_create(): Rather than use a single int variable named 'n',
  use 'nvflags' to store flags, 'len' to store string length and 'idx'
  for the array index.
  • Loading branch information
JohnoKing committed Dec 27, 2024
1 parent e390adf commit 798d463
Show file tree
Hide file tree
Showing 24 changed files with 333 additions and 316 deletions.
2 changes: 1 addition & 1 deletion src/cmd/ksh93/bltins/alarm.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ static char *setdisc(Namval_t *np, const char *event, Namval_t* action, Namfun_t
/*
* catch assignments and set alarm traps
*/
static void putval(Namval_t* np, const char* val, int flag, Namfun_t* fp)
static void putval(Namval_t* np, const char* val, nvflag_t flag, Namfun_t* fp)
{
struct tevent *tp = (struct tevent*)fp;
double d;
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/ksh93/bltins/enum.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,15 @@ static int enuminfo(Opt_t* op, Sfio_t *out, const char *str, Optdisc_t *fp)
return 0;
}

static Namfun_t *clone_enum(Namval_t* np, Namval_t *mp, int flags, Namfun_t *fp)
static Namfun_t *clone_enum(Namval_t* np, Namval_t *mp, nvflag_t flags, Namfun_t *fp)
{
struct Enum *ep, *pp=(struct Enum*)fp;
ep = sh_newof(0,struct Enum,1,pp->nelem*sizeof(char*));
memcpy(ep,pp,sizeof(struct Enum)+pp->nelem*sizeof(char*));
return &ep->hdr;
}

static void put_enum(Namval_t* np,const char *val,int flags,Namfun_t *fp)
static void put_enum(Namval_t* np,const char *val,nvflag_t flags,Namfun_t *fp)
{
struct Enum *ep = (struct Enum*)fp;
const char *v;
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/ksh93/bltins/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ int sh_readline(char **names, volatile int fd, int flags, ssize_t size, Sflong_t
int delim = '\n';
int jmpval=0;
int binary;
int oflags=NV_VARNAME;
nvflag_t oflags=NV_VARNAME;
char inquote = 0;
struct checkpt buff;
Edit_t *ep = (struct edit*)sh.ed_context;
Expand Down
42 changes: 24 additions & 18 deletions src/cmd/ksh93/bltins/typeset.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ struct tdata
static int print_namval(Sfio_t*, Namval_t*, int, struct tdata*);
static void print_attribute(Namval_t*,void*);
static void print_all(Sfio_t*, Dt_t*, struct tdata*);
static void print_scan(Sfio_t*, int, Dt_t*, int, struct tdata*);
static void print_scan(Sfio_t*, nvflag_t, Dt_t*, int, struct tdata*);
static int unall(int, char**, Dt_t*);
static int setall(char**, int, Dt_t*, struct tdata*);
static int setall(char**, nvflag_t, Dt_t*, struct tdata*);
static void pushname(Namval_t*,void*);
static void(*nullscan)(Namval_t*,void*);

Expand All @@ -90,13 +90,14 @@ static void(*nullscan)(Namval_t*,void*);
#endif
int b_readonly(int argc,char *argv[],Shbltin_t *context)
{
int flag;
int n;
nvflag_t flag;
char *command = argv[0];
struct tdata tdata;
NOT_USED(argc);
memset(&tdata,0,sizeof(tdata));
tdata.aflag = '-';
while((flag = optget(argv,*command=='e'?sh_optexport:sh_optreadonly))) switch(flag)
while((n = optget(argv,*command=='e'?sh_optexport:sh_optreadonly))) switch(n)
{
case 'p':
tdata.prefix = command;
Expand Down Expand Up @@ -132,7 +133,7 @@ int b_readonly(int argc,char *argv[],Shbltin_t *context)
#endif
int b_alias(int argc,char *argv[],Shbltin_t *context)
{
unsigned flag = NV_NOARRAY|NV_NOSCOPE|NV_ASSIGN;
nvflag_t flag = NV_NOARRAY|NV_NOSCOPE|NV_ASSIGN;
Dt_t *troot;
int rflag=0, xflag=0, n;
struct tdata tdata;
Expand Down Expand Up @@ -216,7 +217,8 @@ int b_alias(int argc,char *argv[],Shbltin_t *context)
#endif
int b_typeset(int argc,char *argv[],Shbltin_t *context)
{
int n, flag = NV_VARNAME|NV_ASSIGN;
int n;
nvflag_t flag = NV_VARNAME|NV_ASSIGN;
struct tdata tdata;
const char *optstring = sh_opttypeset;
Namdecl_t *ntp = (Namdecl_t*)context->ptr;
Expand Down Expand Up @@ -644,12 +646,14 @@ static void print_value(Sfio_t *iop, Namval_t *np, struct tdata *tp)
}
}

static int setall(char **argv,int flag,Dt_t *troot,struct tdata *tp)
static int setall(char **argv,nvflag_t flag,Dt_t *troot,struct tdata *tp)
{
char *name;
char *last = 0;
int nvflags=(flag&(NV_ARRAY|NV_NOARRAY|NV_VARNAME|NV_IDENT|NV_ASSIGN|NV_STATIC|NV_MOVE));
int r=0, ref=0, comvar=(flag&NV_COMVAR),iarray=(flag&NV_IARRAY);
nvflag_t nvflags=(flag&(NV_ARRAY|NV_NOARRAY|NV_VARNAME|NV_IDENT|NV_ASSIGN|NV_STATIC|NV_MOVE));
int r=0, ref=0;
nvflag_t comvar=(flag&NV_COMVAR);
nvflag_t iarray=(flag&NV_IARRAY);
Dt_t *save_vartree = NULL;
#if SHOPT_NAMESPACE
Namval_t *save_namespace = NULL;
Expand Down Expand Up @@ -686,11 +690,11 @@ static int setall(char **argv,int flag,Dt_t *troot,struct tdata *tp)
nvflags |= (NV_NOREF|NV_NOADD|NV_NOFAIL);
while(name = *++argv)
{
unsigned newflag;
Namval_t *np;
nvflag_t newflag;
Namval_t *np;
Namarr_t *ap=0;
Namval_t *mp;
unsigned curflag;
nvflag_t curflag;
if(troot == sh.fun_tree)
{
/*
Expand Down Expand Up @@ -1122,7 +1126,8 @@ Shbltin_f sh_getlib(char* sym, Pathcomp_t* pp)
int b_builtin(int argc,char *argv[],Shbltin_t *context)
{
char *arg=0, *name;
int n, r=0, flag=0;
int n, r=0, s;
nvflag_t flag=0;
Namval_t *np;
long dlete=0;
struct tdata tdata;
Expand Down Expand Up @@ -1204,7 +1209,7 @@ int b_builtin(int argc,char *argv[],Shbltin_t *context)
return 0;
}
r = 0;
flag = stktell(stkp);
s = stktell(stkp);
while(arg = *argv)
{
name = path_basename(arg);
Expand All @@ -1218,7 +1223,7 @@ int b_builtin(int argc,char *argv[],Shbltin_t *context)
{
if(!dlete && !liblist[n].dll)
continue;
if(dlete || (addr = (Shbltin_f)dlllook(liblist[n].dll,stkptr(stkp,flag))))
if(dlete || (addr = (Shbltin_f)dlllook(liblist[n].dll,stkptr(stkp,s))))
#else
if(dlete)
for(n=dlete; --n>=0;)
Expand Down Expand Up @@ -1251,7 +1256,7 @@ int b_builtin(int argc,char *argv[],Shbltin_t *context)
errormsg(SH_DICT,ERROR_exit(0),"%s: %s",*argv,errmsg);
r = 1;
}
stkseek(stkp,flag);
stkseek(stkp,s);
argv++;
}
return r;
Expand Down Expand Up @@ -1302,7 +1307,8 @@ static int unall(int argc, char **argv, Dt_t *troot)
const char *name;
volatile int r;
Dt_t *dp;
int nflag=0,all=0,isfun,jmpval;
int all=0,isfun,jmpval;
nvflag_t nflag=0;
struct checkpt buff;
NOT_USED(argc);
if(troot==sh.alias_tree)
Expand Down Expand Up @@ -1597,7 +1603,7 @@ static void print_attribute(Namval_t *np,void *data)
* print the nodes in tree <root> which have attributes <flag> set
* if <option> is non-zero, no subscript or value is printed
*/
static void print_scan(Sfio_t *file, int flag, Dt_t *root, int option,struct tdata *tp)
static void print_scan(Sfio_t *file, nvflag_t flag, Dt_t *root, int option,struct tdata *tp)
{
char **argv;
Namval_t *np;
Expand Down
4 changes: 4 additions & 0 deletions src/cmd/ksh93/include/argnod.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ struct argnod
struct argnod *ap;
char *cp;
} argchn;
/*
* argflag must be 8 bits because the code expects that assigning
* to it will mask off any high bits; e.g., ARG_ARITH and friends.
*/
unsigned char argflag;
char argval[4];
};
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/ksh93/include/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ extern struct dolnod *sh_arguse(void);
extern char *sh_checkid(char*,char*);
extern void sh_chktrap(void);
extern void sh_deparse(Sfio_t*,const Shnode_t*,int,int);
extern int sh_debug(const char*,const char*,const char*,char *const[],int);
extern int sh_debug(const char*,const char*,const char*,char *const[],nvflag_t);
extern char **sh_envgen(void);
extern Sfdouble_t sh_arith(const char*);
extern void *sh_arithcomp(char*);
Expand Down Expand Up @@ -149,7 +149,7 @@ extern void sh_trim(char*);
extern int sh_type(const char*);
extern void sh_unscope(void);
#if SHOPT_NAMESPACE
extern Namval_t *sh_fsearch(const char *,int);
extern Namval_t *sh_fsearch(const char *,nvflag_t);
#endif /* SHOPT_NAMESPACE */

/* malloc related wrappers */
Expand Down
25 changes: 11 additions & 14 deletions src/cmd/ksh93/include/name.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
#if SHOPT_FIXEDARRAY
# define ARRAY_FIXED ARRAY_NOCLONE /* For index values */
#endif /* SHOPT_FIXEDARRAY */
#define NV_FARRAY 0x10000000 /* fixed-size arrays */
#define NV_ASETSUB 8 /* set subscript */

/* These flags are used as options to array_get() */
Expand Down Expand Up @@ -87,13 +86,11 @@ struct Ufunction

/* The following attributes are for internal use */
#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_ATTRIBUTES (NV_ARRAY|NV_IDENT|NV_ASSIGN|NV_REF)
#define NV_OPENMASK (NV_APPEND|NV_MOVE|NV_NOARRAY|NV_IARRAY|NV_VARNAME|NV_NOADD|NV_NOSCOPE|NV_NOFAIL|NV_UNATTR|NV_GLOBAL|NV_TYPE|NV_STATIC|NV_COMVAR|NV_ADD)
#define NV_PARAM NV_NODISC /* expansion use positional params */

/* This following are for use with nodes which are not name-values */
#define NV_TYPE 0x1000000
#define NV_STATIC 0x2000000
#define NV_COMVAR 0x4000000
#define NV_FUNCTION (NV_RJUST|NV_FUNCT) /* value is shell function */
#define NV_FPOSIX NV_LJUST /* POSIX function semantics */
#define NV_STATICF NV_INTEGER /* static class function */
Expand All @@ -117,7 +114,7 @@ struct Ufunction
/* NAMNOD MACROS */
/* ... for attributes */

#define nv_setattr(n,f) ((n)->nvflag = (f))
#define nv_setattr(n,f) ((n)->nvflag = f)
#define nv_context(n) ((void*)(n)->nvfun) /* for builtins */
/* The following are for name references */
#define nv_refnode(n) (((struct Namref*)(n)->nvalue)->np)
Expand All @@ -143,14 +140,14 @@ struct Ufunction
#define array_assoc(ap) ((ap)->fun)

extern int array_maxindex(Namval_t*);
extern char *nv_endsubscript(Namval_t*, char*, int);
extern char *nv_endsubscript(Namval_t*, char*, nvflag_t);
extern Namfun_t *nv_cover(Namval_t*);
extern Namarr_t *nv_arrayptr(Namval_t*);
extern int nv_arrayisset(Namval_t*, Namarr_t*);
extern int nv_arraysettype(Namval_t*, Namval_t*,const char*,int);
extern int nv_arraysettype(Namval_t*, Namval_t*,const char*,nvflag_t);
extern int nv_aimax(Namval_t*);
extern int nv_atypeindex(Namval_t*, const char*);
extern void nv_setlist(struct argnod*, int, Namval_t*);
extern void nv_setlist(struct argnod*, nvflag_t, Namval_t*);
#if SHOPT_OPTIMIZE
extern void nv_optimize(Namval_t*);
extern void nv_optimize_clear(Namval_t*);
Expand All @@ -164,11 +161,11 @@ extern void nv_setlist(struct argnod*, int, Namval_t*);
#endif /* SHOPT_OPTIMIZE */
extern void nv_outname(Sfio_t*,char*, int);
extern void nv_unref(Namval_t*);
extern void _nv_unset(Namval_t*,int);
extern void _nv_unset(Namval_t*,nvflag_t);
extern int nv_hasget(Namval_t*);
extern int nv_clone(Namval_t*, Namval_t*, int);
void clone_all_disc(Namval_t*, Namval_t*, int);
extern Namfun_t *nv_clone_disc(Namfun_t*, int);
extern int nv_clone(Namval_t*, Namval_t*, nvflag_t);
void clone_all_disc(Namval_t*, Namval_t*, nvflag_t);
extern Namfun_t *nv_clone_disc(Namfun_t*, nvflag_t);
extern void *nv_diropen(Namval_t*, const char*);
extern char *nv_dirnext(void*);
extern void nv_dirclose(void*);
Expand All @@ -182,7 +179,7 @@ extern Namval_t *nv_mount(Namval_t*, const char *name, Dt_t*);
extern Namval_t *nv_arraychild(Namval_t*, Namval_t*, int);
extern int nv_compare(Dt_t*, void*, void*, Dtdisc_t*);
extern void nv_outnode(Namval_t*,Sfio_t*, int, int);
extern int nv_subsaved(Namval_t*, int);
extern int nv_subsaved(Namval_t*, nvflag_t);
extern void nv_typename(Namval_t*, Sfio_t*);
extern void nv_newtype(Namval_t*);
extern Namval_t *nv_typeparent(Namval_t*);
Expand Down
Loading

0 comments on commit 798d463

Please sign in to comment.