1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00

Remove abandoned SHOPT_ENV experiment

SHOPT_ENV is an undocumented compile-time option implementing an
experimental method for handling environment variables, which is
implemented in env.h and env.c. There is no mention in the docs or
Makefile, and no mention in the mailing list archives. It adds no
new functionality, but at first glance it's a clean-looking
interface.

However, unfortunately, it's broken. Compiling with -DSHOPT_ENV
added to CCFLAGS causes bin/shtests to show these regressions:

functions.sh[341]: export not restored name=value function call -- expected 'base', got ''
functions.sh[1274]: Environment variable is not passed to a function
substring.sh[236]: export not restored name=value function call
variables.sh[782]: SHLVL should be 3 not 2

In addition, 'export' stops working on unset variables.

In the 93v- beta this code is still present, unchanged, though 93v-
made lots of incompatible changes. By the time ksh2020 noticed it,
it was no longer compiling, so it probably wasn't compiling in the
93v- beta either. Discussion: https://github.com/att/ast/issues/504
So the experiment was already abandoned by D. Korn and his team.

Meanwhile it was leaving sh/name.c with two versions of several
enviornment-related functions, and it's not clear which one is
actually compiled without doing detective work tracing header files
(most of the code was made conditional on _ENV_H, which is defined
in env.h, which is included by defs.h if SHOPT_ENV is defined).
This actively hinders understanding of the codebase. And any
changes to these functions would need to be implemented twice.

src/cmd/ksh93/include/env.h,
src/cmd/ksh93/sh/env.c:
- Removed.

src/cmd/ksh93/DESIGN,
src/cmd/ksh93/Makefile,
src/cmd/ksh93/Mamfile:
- Update accordingly.

All other changed files:
- Remove deactivated code behind SHOPT_ENV and _ENV_H.
This commit is contained in:
Martijn Dekker 2020-09-02 15:52:41 +01:00
parent bc4dbe0627
commit 8d7f616e75
10 changed files with 57 additions and 488 deletions

View file

@ -1,255 +0,0 @@
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1982-2011 AT&T Intellectual Property *
* and is licensed under the *
* Eclipse Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* http://www.eclipse.org/org/documents/epl-v10.html *
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* David Korn <dgk@research.att.com> *
* *
***********************************************************************/
#pragma prototyped
#include <ast.h>
#include <cdt.h>
#define env_change() (++ast.env_serial)
typedef struct _venv_ Evar_t;
struct _venv_
{
union
{
Evar_t *next;
char *ptr;
} un;
Dtlink_t link;
int index;
};
typedef struct _env_
{
Dt_t *dt;
Evar_t *freelist;
char **env;
int count;
int extra;
int max;
int flags;
} Env_t;
#define _BLD_env 1
#include <env.h>
#define ENV_VALID 2 /* set if env is valid */
#define ENV_PMALLOC 1 /* set if Evar_t->un.ptr *s malloced */
#define ENV_VMALLOC 2 /* set of Evar_t was malloced */
#define ENV_BITS 3
/*
* Compares the name portion of name=... only.
*/
static int compare(Dt_t *dt, Void_t* key1, Void_t* key2, Dtdisc_t* disc)
{
register int c,d;
const unsigned char *s1=(unsigned const char*)key1;
const unsigned char *s2=(unsigned const char*)key2;
while((c= *s1++) && c!='=' && c==*s2)
s2++;
if(c=='=')
c = 0;
if((d=*s2)=='=')
d = 0;
return(c-d);
}
static Dtdisc_t env_disc =
{
0, -1,
sizeof(char*),
0,
0,
compare
};
/*
* return a pointer to the environment in sorted order
* NULL is returned if there if there is nospace
*/
char **env_get(Env_t* ep)
{
register Evar_t *vp;
register int n=ep->extra;
if(ep->flags&ENV_VALID)
return(ep->env+n);
if(ep->count > ep->max)
{
if(ep->flags&ENV_MALLOCED)
free((void*)ep->env);
if(!(ep->env = (char**)malloc(sizeof(char*)*(ep->count+1))))
return(0);
ep->flags |= ENV_MALLOCED;
ep->max = ep->count;
}
for(vp=(Evar_t*)dtfirst(ep->dt);vp; vp=(Evar_t*)dtnext(ep->dt,vp))
{
vp->index = (n<<ENV_BITS) | (vp->index&((1<<ENV_BITS)-1));
ep->env[n++] = vp->un.ptr;
}
ep->env[n] = 0;
ep->flags |= ENV_VALID;
environ = ep->env+ep->extra;
return(ep->env+ep->extra);
}
/*
* add name=value pair given by <str> to <ep>
* if malloced is set, the variable will be freed when reassigned
* The environment list may become invalidated
* Returns 1 for success, 0 for failure
*/
int env_add(Env_t *ep, const char *str, int flags)
{
Evar_t *vp = (Evar_t*)dtmatch(ep->dt,(void*)str);
if(vp && strcmp(str,vp->un.ptr)==0)
return(1);
if(flags&ENV_STRDUP)
str = strdup(str);
if(vp)
{
if(vp->index&ENV_PMALLOC)
free((void*)vp->un.ptr);
vp->un.ptr = (char*)str;
if(ep->env && (ep->flags&ENV_VALID))
ep->env[vp->index>>ENV_BITS] = vp->un.ptr;
}
else
{
ep->flags &= ~ENV_VALID;
if(vp = ep->freelist)
ep->freelist = vp->un.next;
else if(vp = newof((Evar_t*)0,Evar_t,2,0))
{
vp->index = ENV_VMALLOC;
ep->freelist = (vp+1);
ep->freelist->un.next = 0;
}
else
return(0);
vp->un.ptr = (void*)str;
if(!(vp=dtinsert(ep->dt,vp)))
return(0);
ep->count++;
}
if(flags)
vp->index |= ENV_PMALLOC;
else
vp->index &= ~ENV_PMALLOC;
env_change();
return(1);
}
/*
* delete name from <ep>
* The environment list may become invalidated
* Returns 1 for success, 0 for if name is not present
*/
int env_delete(Env_t *ep, const char *str)
{
Evar_t *vp = (Evar_t*)dtmatch(ep->dt,(void*)str);
if(!vp)
return(0);
ep->flags &= ~ENV_VALID;
if(vp->index&ENV_PMALLOC)
free((void*)vp->un.ptr);
dtdelete(ep->dt,vp);
vp->un.next = ep->freelist;
ep->freelist = vp;
env_change();
return(1);
}
/*
* open up a structure to support environment variables
* initialize with environment give by <envp>
* If <extra> > 0, <extra> slots will be left at beginning of
* environment list when env_get() is involed.
* If <extra>==ENV_USABLE, then the original environ can be
* used and returned. Otherwise, a new one will be returned
*/
Env_t *env_open(char **envp, int extra)
{
char **env;
Env_t *ep;
Evar_t *vp;
int n=2;
if(!(ep = newof((Env_t*)0,Env_t,1,0)))
return(0);
if(!(ep->dt = dtopen(&env_disc,Dtoset)))
return(0);
if(env=envp)
{
while(*env++);
n = (env+2)-envp;
}
if(extra==ENV_STABLE)
{
ep->env = envp;
ep->max = n-1;
}
else
ep->count = ep->extra = extra;
ep->freelist = vp = newof((Evar_t*)0,Evar_t,n,0);
vp->index = ENV_VMALLOC;
while(--n>0)
{
vp->un.next = (vp+1);
vp++;
}
vp->un.next = 0;
if(env)
{
for(env=envp; *env; env++)
env_add(ep,*env,0);
}
return(ep);
}
/*
* close <ep> and free up all space used by it
*/
void env_close(Env_t *ep)
{
Evar_t *vp, *vpnext,*top;
if(ep->env && (ep->flags&ENV_MALLOCED))
free((void*)ep->env);
for(vp=(Evar_t*)dtfirst(ep->dt);vp; vp=vpnext)
{
vpnext = (Evar_t*)dtnext(ep->dt,vp);
env_delete(ep,vp->un.ptr);
}
for(top=0,vp = ep->freelist; vp; vp = vpnext)
{
vpnext = vp->un.next;
if(vp->index&ENV_VMALLOC)
{
vp->un.next = top;
top = vp;
}
}
for(vp=top; vp; vp = vpnext)
{
vpnext = vp->un.next;
free((void*)vp);
}
dtclose(ep->dt);
}

View file

@ -1911,10 +1911,6 @@ static void env_init(Shell_t *shp)
char *dp,*next=0;
int nenv=0,k=0,size=0;
Namval_t *np0;
#ifdef _ENV_H
shp->env = env_open(environ,3);
env_delete(shp->env,"_");
#endif
if(!ep)
goto skip;
while(*ep++)
@ -2008,9 +2004,6 @@ static void env_init(Shell_t *shp)
cp += 2;
}
skip:
#ifdef _ENV_H
env_delete(shp->env,e_envmarker);
#endif
if(nv_isnull(PWDNOD) || nv_isattr(PWDNOD,NV_TAGGED))
{
nv_offattr(PWDNOD,NV_TAGGED);

View file

@ -59,10 +59,8 @@ pathnative(const char* path, char* buf, size_t siz)
#endif /* _lib_pathnative */
static void attstore(Namval_t*,void*);
#ifndef _ENV_H
static void pushnam(Namval_t*,void*);
static char *staknam(Namval_t*, char*);
#endif
static void pushnam(Namval_t*,void*);
static char *staknam(Namval_t*, char*);
static void rightjust(char*, int, int);
static char *lastdot(char*, int);
@ -109,9 +107,7 @@ struct adata
#endif
char nv_local = 0;
#ifndef _ENV_H
static void(*nullscan)(Namval_t*,void*);
#endif
#if ( SFIO_VERSION <= 20010201L )
# define _data data
@ -137,29 +133,6 @@ static char *getbuf(size_t len)
return(buf);
}
#ifdef _ENV_H
void sh_envput(Env_t* ep,Namval_t *np)
{
int offset = staktell();
Namarr_t *ap = nv_arrayptr(np);
char *val;
if(ap)
{
if(ap->nelem&ARRAY_UNDEF)
nv_putsub(np,"0",0L);
else if(!(val=nv_getsub(np)) || strcmp(val,"0"))
return;
}
if(!(val = nv_getval(np)))
return;
stakputs(nv_name(np));
stakputc('=');
stakputs(val);
stakseek(offset);
env_add(ep,stakptr(offset),ENV_STRDUP);
}
#endif
/*
* output variable name in format for re-input
*/
@ -2120,7 +2093,6 @@ static void rightjust(char *str, int size, int fill)
}
#endif /* SHOPT_MULTIBYTE */
#ifndef _ENV_H
static char *staknam(register Namval_t *np, char *value)
{
register char *p,*q;
@ -2133,35 +2105,10 @@ static char *staknam(register Namval_t *np, char *value)
}
return(q);
}
#endif
/*
* put the name and attribute into value of attributes variable
*/
#ifdef _ENV_H
static void attstore(register Namval_t *np, void *data)
{
register int flag, c = ' ';
NOT_USED(data);
if(!(nv_isattr(np,NV_EXPORT)))
return;
flag = nv_isattr(np,NV_RDONLY|NV_UTOL|NV_LTOU|NV_RJUST|NV_LJUST|NV_ZFILL|NV_INTEGER);
stakputc('=');
if((flag&NV_DOUBLE) == NV_DOUBLE)
{
/* export doubles as integers for ksh88 compatibility */
stakputc(c+NV_INTEGER|(flag&~(NV_DOUBLE|NV_EXPNOTE)));
}
else
{
stakputc(c+flag);
if(flag&NV_INTEGER)
c += nv_size(np);
}
stakputc(c);
stakputs(nv_name(np));
}
#else
static void attstore(register Namval_t *np, void *data)
{
register int flag = np->nvflag;
@ -2194,9 +2141,7 @@ static void attstore(register Namval_t *np, void *data)
}
ap->attval = strcopy(++ap->attval,nv_name(np));
}
#endif
#ifndef _ENV_H
static void pushnam(Namval_t *np, void *data)
{
register char *value;
@ -2210,31 +2155,11 @@ static void pushnam(Namval_t *np, void *data)
if(nv_isattr(np,NV_RDONLY|NV_UTOL|NV_LTOU|NV_RJUST|NV_LJUST|NV_ZFILL|NV_INTEGER))
ap->attsize += (strlen(nv_name(np))+4);
}
#endif
/*
* Generate the environment list for the child.
*/
#ifdef _ENV_H
char **sh_envgen(void)
{
Shell_t *shp = sh_getinterp();
int offset,tell;
register char **er;
env_delete(shp->env,"_");
er = env_get(shp->env);
offset = staktell();
stakputs(e_envmarker);
tell = staktell();
nv_scan(shp->var_tree, attstore,(void*)0,0,(NV_RDONLY|NV_UTOL|NV_LTOU|NV_RJUST|NV_LJUST|NV_ZFILL|NV_INTEGER));
if(tell ==staktell())
stakseek(offset);
else
*--er = stakfreeze(1)+offset;
return(er);
}
#else
char **sh_envgen(void)
{
register char **er;
@ -2264,7 +2189,6 @@ char **sh_envgen(void)
*data.argnam = 0;
return(er);
}
#endif
struct scan
{