Skip to content

Commit

Permalink
cleanup: complete the transition from stak(3) to stk(3)/sfio(3)
Browse files Browse the repository at this point in the history
Having two different interfaces to the same AST stack routines is a
botch that damages code legibility. The "new" stk(3) interface is a
reimplementation (on top of sfio(3)) of the old stak(3) routines,
the latter now being just a set of macros that use the former. The
code liberally mixes both interfcaces.

I'm scare-quoted "new" above because, as the ksh93-history repo
shows, the new stk(3) interface has been present since 1995 or
before! I am not aware of any surviving copy of the original
stak(3) routines. Yet, the transition was never close to completed,
not even in the last AT&T beta from 2016.

This commit finally finishes the job. Unless I've done something
wrong, there should be no change in ksh's behaviour.

src/lib/libast/include/stak.h,
src/lib/libast/man/stak.3:
- Removed.

Every other file:
- Translate the ancient interface to the slightly less ancient one.
- Related housekeeping.
  • Loading branch information
McDutchie committed May 15, 2023
1 parent 3447402 commit a8eae75
Show file tree
Hide file tree
Showing 54 changed files with 694 additions and 1,029 deletions.
8 changes: 3 additions & 5 deletions src/cmd/builtin/Mamfile
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,9 @@ make install
prev ${PACKAGE_ast_INCLUDE}/shcmd.h implicit
done ${PACKAGE_ast_INCLUDE}/cmdext.h
prev ${PACKAGE_ast_INCLUDE}/shcmd.h
make ${PACKAGE_ast_INCLUDE}/stak.h implicit
make ${PACKAGE_ast_INCLUDE}/stk.h implicit
prev ${PACKAGE_ast_INCLUDE}/sfio.h
done ${PACKAGE_ast_INCLUDE}/stk.h
done ${PACKAGE_ast_INCLUDE}/stak.h
make ${PACKAGE_ast_INCLUDE}/stk.h implicit
prev ${PACKAGE_ast_INCLUDE}/sfio.h
done ${PACKAGE_ast_INCLUDE}/stk.h
prev ${PACKAGE_ast_INCLUDE}/error.h
prev ${PACKAGE_ast_INCLUDE}/ast.h
done ${PACKAGE_ast_INCLUDE}/cmd.h
Expand Down
18 changes: 2 additions & 16 deletions src/cmd/ksh93/Mamfile
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,7 @@ make install
prev ${PACKAGE_ast_INCLUDE}/shcmd.h implicit
done ${PACKAGE_ast_INCLUDE}/cmdext.h
prev ${PACKAGE_ast_INCLUDE}/shcmd.h
make ${PACKAGE_ast_INCLUDE}/stak.h implicit
prev ${PACKAGE_ast_INCLUDE}/stk.h
done ${PACKAGE_ast_INCLUDE}/stak.h
prev ${PACKAGE_ast_INCLUDE}/stk.h
prev ${PACKAGE_ast_INCLUDE}/error.h
prev ${PACKAGE_ast_INCLUDE}/ast.h
done ${PACKAGE_ast_INCLUDE}/cmd.h
Expand Down Expand Up @@ -229,9 +227,7 @@ make install
prev include/shell.h
prev ${PACKAGE_ast_INCLUDE}/endian.h
prev include/name.h
make include/argnod.h implicit
prev ${PACKAGE_ast_INCLUDE}/stak.h
done include/argnod.h dontcare
prev include/argnod.h implicit
prev ${PACKAGE_ast_INCLUDE}/cdt.h
prev ${PACKAGE_ast_INCLUDE}/error.h
prev ${PACKAGE_ast_INCLUDE}/sfio.h
Expand Down Expand Up @@ -267,7 +263,6 @@ make install
prev include/nval.h
done include/variables.h
prev ${PACKAGE_ast_INCLUDE}/error.h
prev ${PACKAGE_ast_INCLUDE}/stak.h
prev include/defs.h
prev shopt.h
done bltins/cd_pwd.c
Expand Down Expand Up @@ -335,7 +330,6 @@ make install
prev include/variables.h
prev ${PACKAGE_ast_INCLUDE}/error.h
prev ${PACKAGE_ast_INCLUDE}/ls.h
prev ${PACKAGE_ast_INCLUDE}/stak.h
prev include/defs.h
prev shopt.h
done bltins/hist.c
Expand Down Expand Up @@ -412,7 +406,6 @@ make install
prev include/history.h
prev include/name.h
prev include/io.h
prev ${PACKAGE_ast_INCLUDE}/stak.h
prev ${PACKAGE_ast_INCLUDE}/error.h
prev include/defs.h
prev shopt.h
Expand Down Expand Up @@ -575,7 +568,6 @@ make install
prev ${PACKAGE_ast_INCLUDE}/sfio.h
done include/fcin.h
prev ${PACKAGE_ast_INCLUDE}/ls.h
prev ${PACKAGE_ast_INCLUDE}/stak.h
prev ${PACKAGE_ast_INCLUDE}/sfio.h
prev ${PACKAGE_ast_INCLUDE}/ast.h
prev shopt.h
Expand Down Expand Up @@ -634,7 +626,6 @@ make install
make array.o
make sh/array.c
prev include/name.h
prev ${PACKAGE_ast_INCLUDE}/stak.h
prev include/defs.h
prev shopt.h
done sh/array.c
Expand Down Expand Up @@ -695,7 +686,6 @@ make install
prev dirlib.h implicit dontcare virtual
prev ${PACKAGE_ast_INCLUDE}/ast_lib.h
done ${PACKAGE_ast_INCLUDE}/ast_dir.h
prev ${PACKAGE_ast_INCLUDE}/stak.h
prev ${PACKAGE_ast_INCLUDE}/ls.h
prev ${PACKAGE_ast_INCLUDE}/glob.h implicit
prev ${PACKAGE_ast_INCLUDE}/ast.h
Expand Down Expand Up @@ -786,7 +776,6 @@ make install
prev ${PACKAGE_ast_INCLUDE}/regex.h
prev ${PACKAGE_ast_INCLUDE}/tmx.h
prev ${PACKAGE_ast_INCLUDE}/ccode.h
prev ${PACKAGE_ast_INCLUDE}/stak.h
prev include/defs.h
prev shopt.h
done sh/init.c
Expand Down Expand Up @@ -840,7 +829,6 @@ make install
prev include/defs.h
prev include/nval.h
prev include/fcin.h
prev ${PACKAGE_ast_INCLUDE}/stak.h
prev ${PACKAGE_ast_INCLUDE}/releaseflags.h
prev ${PACKAGE_ast_INCLUDE}/ast.h
prev shopt.h
Expand Down Expand Up @@ -933,7 +921,6 @@ make install
prev include/lexstates.h
prev include/shtable.h
prev ${PACKAGE_ast_INCLUDE}/ccode.h
prev ${PACKAGE_ast_INCLUDE}/stak.h
prev include/defs.h
prev ${PACKAGE_ast_INCLUDE}/ast_wchar.h
prev ${PACKAGE_ast_INCLUDE}/ast.h
Expand All @@ -944,7 +931,6 @@ make install
make streval.o
make sh/streval.c
prev FEATURE/externs
prev ${PACKAGE_ast_INCLUDE}/stak.h
prev ${PACKAGE_ast_INCLUDE}/error.h
prev include/streval.h
prev shopt.h
Expand Down
38 changes: 18 additions & 20 deletions src/cmd/ksh93/bltins/cd_pwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

#include "shopt.h"
#include "defs.h"
#include <stak.h>
#include <error.h>
#include "variables.h"
#include "path.h"
Expand Down Expand Up @@ -164,38 +163,37 @@ int b_cd(int argc, char *argv[],Shbltin_t *context)
dp = cdpath?cdpath->name:"";
cdpath = path_nextcomp(cdpath,dir,0);
#if _WINIX
if(*stakptr(PATH_OFFSET+1)==':' && isalpha(*stakptr(PATH_OFFSET)))
if(*stkptr(sh.stk,PATH_OFFSET+1)==':' && isalpha(*stkptr(sh.stk,PATH_OFFSET)))
{
*stakptr(PATH_OFFSET+1) = *stakptr(PATH_OFFSET);
*stakptr(PATH_OFFSET)='/';
*stkptr(sh.stk,PATH_OFFSET+1) = *stkptr(sh.stk,PATH_OFFSET);
*stkptr(sh.stk,PATH_OFFSET)='/';
}
#endif /* _WINIX */
if(*stakptr(PATH_OFFSET)!='/')
if(*stkptr(sh.stk,PATH_OFFSET)!='/')
{
char *last=(char*)stakfreeze(1);
stakseek(PATH_OFFSET);
stakputs(oldpwd);
char *last=(char*)stkfreeze(sh.stk,1);
stkseek(sh.stk,PATH_OFFSET);
sfputr(sh.stk,oldpwd,-1);
/* don't add '/' if oldpwd is / itself */
if(*oldpwd!='/' || oldpwd[1])
stakputc('/');
stakputs(last+PATH_OFFSET);
stakputc(0);
sfputc(sh.stk,'/');
sfputr(sh.stk,last+PATH_OFFSET,0);
}
if(!pflag)
{
char *cp;
stakseek(PATH_MAX+PATH_OFFSET);
if(*(cp=stakptr(PATH_OFFSET))=='/')
stkseek(sh.stk,PATH_MAX+PATH_OFFSET);
if(*(cp=stkptr(sh.stk,PATH_OFFSET))=='/')
if(!pathcanon(cp,PATH_DOTDOT))
continue;
}
if((rval=chdir(path_relative(stakptr(PATH_OFFSET)))) >= 0)
if((rval=chdir(path_relative(stkptr(sh.stk,PATH_OFFSET)))) >= 0)
goto success;
if(errno!=ENOENT && saverrno==0)
saverrno=errno;
}
while(cdpath);
if(rval<0 && *dir=='/' && *(path_relative(stakptr(PATH_OFFSET)))!='/')
if(rval<0 && *dir=='/' && *(path_relative(stkptr(sh.stk,PATH_OFFSET)))!='/')
rval = chdir(dir);
/* use absolute chdir() if relative chdir() fails */
if(rval<0)
Expand All @@ -210,16 +208,16 @@ int b_cd(int argc, char *argv[],Shbltin_t *context)
dp = dir; /* print out directory for cd - */
if(pflag)
{
dir = stakptr(PATH_OFFSET);
dir = stkptr(sh.stk,PATH_OFFSET);
if (!(dir=pathcanon(dir,PATH_PHYSICAL)))
{
dir = stakptr(PATH_OFFSET);
dir = stkptr(sh.stk,PATH_OFFSET);
errormsg(SH_DICT,ERROR_system(ret),"%s:",dir);
UNREACHABLE();
}
stakseek(dir-stakptr(0));
stkseek(sh.stk,dir-stkptr(sh.stk,0));
}
dir = (char*)stakfreeze(1)+PATH_OFFSET;
dir = stkfreeze(sh.stk,1)+PATH_OFFSET;
if(*dp && (*dp!='.'||dp[1]) && strchr(dir,'/'))
sfputr(sfstdout,dir,'\n');
nv_putval(opwdnod,oldpwd,NV_RDONLY);
Expand Down Expand Up @@ -290,7 +288,7 @@ int b_pwd(int argc, char *argv[],Shbltin_t *context)
}
if(flag)
{
cp = strcpy(stakseek(strlen(cp)+PATH_MAX),cp);
cp = strcpy(stkseek(sh.stk,strlen(cp)+PATH_MAX),cp);
pathcanon(cp,PATH_PHYSICAL);
}
sfputr(sfstdout,cp,'\n');
Expand Down
6 changes: 2 additions & 4 deletions src/cmd/ksh93/bltins/enum.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,8 @@ int b_enum(int argc, char** argv, Shbltin_t *context)
error(ERROR_exit(1), "%s must name an array containing at least two elements",cp);
UNREACHABLE();
}
n = staktell();
sfprintf(stkstd,"%s.%s%c",NV_CLASS,np->nvname,0);
tp = nv_open(stakptr(n), sh.var_tree, NV_VARNAME);
stakseek(n);
sfprintf(sh.strbuf,"%s.%s",NV_CLASS,np->nvname);
tp = nv_open(sfstruse(sh.strbuf), sh.var_tree, NV_VARNAME);
n = sz;
i = 0;
nv_onattr(tp, NV_UINT16);
Expand Down
3 changes: 1 addition & 2 deletions src/cmd/ksh93/bltins/hist.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
***********************************************************************/
#include "shopt.h"
#include "defs.h"
#include <stak.h>
#include <ls.h>
#include <error.h>
#include "variables.h"
Expand Down Expand Up @@ -322,7 +321,7 @@ static void hist_subst(const char *command,int fd,char *replace)
return;
lseek(fd,0,SEEK_SET);
c = (int)size;
string = stakalloc(c+1);
string = stkalloc(sh.stk,c+1);
if(read(fd,string,c)!=c)
return;
string[c] = 0;
Expand Down
57 changes: 28 additions & 29 deletions src/cmd/ksh93/bltins/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include "shopt.h"
#include "defs.h"
#include <error.h>
#include <stak.h>
#include "io.h"
#include "name.h"
#include "history.h"
Expand Down Expand Up @@ -359,7 +358,7 @@ int b_print(int argc, char *argv[], Shbltin_t *context)
sfprintf(outfile,"%!",&pdata);
} while(*pdata.nextarg && pdata.nextarg!=argv);
if(pdata.nextarg == nullarg && pdata.argsize>0)
if(sfwrite(outfile,stakptr(staktell()),pdata.argsize) < 0)
if(sfwrite(outfile,stkptr(sh.stk,stktell(sh.stk)),pdata.argsize) < 0)
exitval = 1;
sfpool(sfstderr,pool,SF_WRITE);
if (pdata.err)
Expand Down Expand Up @@ -424,7 +423,7 @@ static int echolist(Sfio_t *outfile, int raw, char *argv[])
if(!raw && (n=fmtvecho(cp,&pdata))>=0)
{
if(n)
if(sfwrite(outfile,stakptr(staktell()),n) < 0)
if(sfwrite(outfile,stkptr(sh.stk,stktell(sh.stk)),n) < 0)
exitval = 1;
}
else
Expand Down Expand Up @@ -498,17 +497,17 @@ static char strformat(char *s)
static char *genformat(char *format)
{
char *fp;
stakseek(0);
stakputs(format);
fp = (char*)stakfreeze(1);
stkseek(sh.stk,0);
sfputr(sh.stk,format,-1);
fp = stkfreeze(sh.stk,1);
strformat(fp);
return fp;
}

static char *fmthtml(const char *string, int flags)
{
const char *cp = string, *op;
int c, offset = staktell();
int c, offset = stktell(sh.stk);
/*
* The only multibyte locale ksh currently supports is UTF-8, which is a superset of ASCII. So, if we're on an
* EBCDIC system, below we attempt to convert EBCDIC to ASCII only if we're not in a multibyte locale (mbwide()).
Expand All @@ -522,19 +521,19 @@ static char *fmthtml(const char *string, int flags)
if(!mbwide())
c = CCMAPC(c,CC_NATIVE,CC_ASCII);
if(mbwide() && c < 0) /* invalid multibyte char */
stakputc('?');
sfputc(sh.stk,'?');
else if(c == 60) /* < */
stakputs("&lt;");
sfputr(sh.stk,"&lt;",-1);
else if(c == 62) /* > */
stakputs("&gt;");
sfputr(sh.stk,"&gt;",-1);
else if(c == 38) /* & */
stakputs("&amp;");
sfputr(sh.stk,"&amp;",-1);
else if(c == 34) /* " */
stakputs("&quot;");
sfputr(sh.stk,"&quot;",-1);
else if(c == 39) /* ' (&apos; is not HTML) */
stakputs("&#39;");
sfputr(sh.stk,"&#39;",-1);
else
stakwrite(op, cp-op);
sfwrite(sh.stk, op, cp-op);
}
}
else
Expand All @@ -545,27 +544,27 @@ static char *fmthtml(const char *string, int flags)
while(op = cp, c = mbchar(cp))
{
if(c < 0)
stakputs("%3F");
sfputr(sh.stk,"%3F",-1);
else if(c < 128 && strchr(URI_RFC3986_UNRESERVED, c))
stakputc(c);
sfputc(sh.stk,c);
else
while(c = *(unsigned char*)op++, op <= cp)
sfprintf(stkstd, "%%%02X", c);
sfprintf(sh.stk, "%%%02X", c);
}
}
else
{
while(c = *(unsigned char*)cp++)
{
if(strchr(URI_RFC3986_UNRESERVED, c))
stakputc(c);
sfputc(sh.stk,c);
else
sfprintf(stkstd, "%%%02X", CCMAPC(c, CC_NATIVE, CC_ASCII));
sfprintf(sh.stk, "%%%02X", CCMAPC(c, CC_NATIVE, CC_ASCII));
}
}
}
stakputc(0);
return stakptr(offset);
sfputc(sh.stk,0);
return stkptr(sh.stk,offset);
}

static ssize_t fmtbase64(Sfio_t *iop, char *string, int alt)
Expand Down Expand Up @@ -1013,7 +1012,7 @@ static int extend(Sfio_t* sp, void* v, Sffmt_t* fe)
pp->argsize = n;
return -1;
}
value->s = stakptr(staktell());
value->s = stkptr(sh.stk,stktell(sh.stk));
fe->size = n;
}
break;
Expand Down Expand Up @@ -1087,7 +1086,7 @@ static int fmtvecho(const char *string, struct printf *pp)
{
const char *cp = string, *cpmax;
int c;
int offset = staktell();
int offset = stktell(sh.stk);
int chlen;
if(mbwide())
{
Expand All @@ -1107,12 +1106,12 @@ static int fmtvecho(const char *string, struct printf *pp)
return -1;
c = --cp - string;
if(c>0)
stakwrite(string,c);
sfwrite(sh.stk,string,c);
for(; c= *cp; cp++)
{
if (mbwide() && ((chlen = mbsize(cp)) > 1))
{
stakwrite(cp,chlen);
sfwrite(sh.stk,cp,chlen);
cp += (chlen-1);
continue;
}
Expand Down Expand Up @@ -1161,11 +1160,11 @@ static int fmtvecho(const char *string, struct printf *pp)
default:
cp--;
}
stakputc(c);
sfputc(sh.stk,c);
}
done:
c = staktell()-offset;
stakputc(0);
stakseek(offset);
c = stktell(sh.stk)-offset;
sfputc(sh.stk,0);
stkseek(sh.stk,offset);
return c;
}
Loading

0 comments on commit a8eae75

Please sign in to comment.