From a45a0ebd670b401ffead0861b2ad01e578dc6c33 Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Tue, 27 Feb 2024 22:22:25 +0000 Subject: [PATCH] Further modernise the stk(3) stack routines (re: a8eae75d) *** Void pointers *** The stk(3) interface used char pointers throughout for K&R C and C++ compatibility. I gave up all pretense of that long ago (see a34e8319) and we now use C89/C90 as our lowest common denominator, so we're allowed to use void pointers. The stkalloc(), stkset(), stkseek() and stkfreeze() functions now return void* instead of char*. This should compatible with old code as implicit typecasts are done. I scanned all the uses of these in current code as well as the complete ast-open-history repo and didn't find problems, nor does 'clang -Wall' throw any warnings. So, no changes should be required. However, this change does allow eliminating a lot of verbose/annoying typecasts from the code, as void pointers can be assigned/passed to all sorts of types. This commit removes those explicit typecasts, improving code legibility. The stkcopy() and stkptr() functions continue to return char*, as the first is purely for copying strings and the second's result is often dereferenced directly to read individual bytes on the stack. *** Deprecate/disuse stkinstall() *** The old stak(3) interface did not pass stack pointers to every function, so to use multiple stacks, one had to be "installed" (activated) and there was only one "active" stack at the time. The new interface has stkinstall() for backward compatibility with this practice; it updates the stack that stkstd refers to. But this is completely redundant as it's much more efficient to simply change the pointer passed to the stack functions. So stkinstall() is deprecated and its use replaced accordingly in the following files: - src/lib/libast/misc/glob.c - src/cmd/ksh93/sh/parse.c - src/cmd/ksh93/sh/trestore.c *** Add stkoverflow() *** In typical AT&T fashion, stkinstall() had another, unrelated function: set a pointer to a char* function called when the system runs out of memory to allocate new stack frames. It can either find memory somewhere and return a pointer, or error out. stkinstall() did not allow unsetting this function pointer to restore the default. This commit adds a replacement stkoverflow() function that sets this function (now of type void*) or restores the default. (The unset functionality is currently unused but may come in handy someday and in any case it's trivial.) This is currently used in: - src/cmd/ksh93/sh/init.c *** Bump libast API version *** In src/lib/libast/features/api, the libast API version is bumped to 20240227 due to the changes above. *** Rework stk(3) manual page *** Rewrote parts to match current practice, e.g., removed references to "the active stack" which never really applied to stk(3). *** Restore stak.h *** Perhaps it was overzealous to remove that in the referenced commit. It's a small header file that simply maps the old stak(3) interface onto stk(3) using some #defines. It remains unused, but the cost of having it is negligible and restoring it restores compatbility with old code we might want to test or backport at some point. --- src/cmd/ksh93/bltins/cd_pwd.c | 4 +- src/cmd/ksh93/bltins/getopts.c | 4 +- src/cmd/ksh93/bltins/test.c | 4 +- src/cmd/ksh93/bltins/typeset.c | 4 +- src/cmd/ksh93/edit/completion.c | 6 +- src/cmd/ksh93/features/externs | 2 +- src/cmd/ksh93/include/defs.h | 2 +- src/cmd/ksh93/sh/args.c | 6 +- src/cmd/ksh93/sh/expand.c | 10 +-- src/cmd/ksh93/sh/fault.c | 2 +- src/cmd/ksh93/sh/init.c | 5 +- src/cmd/ksh93/sh/io.c | 6 +- src/cmd/ksh93/sh/lex.c | 13 ++-- src/cmd/ksh93/sh/macro.c | 7 +- src/cmd/ksh93/sh/name.c | 2 +- src/cmd/ksh93/sh/nvtree.c | 8 +-- src/cmd/ksh93/sh/parse.c | 51 +++++++------- src/cmd/ksh93/sh/streval.c | 4 +- src/cmd/ksh93/sh/string.c | 2 +- src/cmd/ksh93/sh/trestore.c | 21 +++--- src/cmd/ksh93/sh/xec.c | 44 ++++++------ src/lib/libast/Mamfile | 2 +- src/lib/libast/features/api | 2 +- src/lib/libast/include/stak.h | 51 ++++++++++++++ src/lib/libast/include/stk.h | 18 ++--- src/lib/libast/man/stk.3 | 116 +++++++++++++++++++------------- src/lib/libast/misc/glob.c | 44 ++++++------ src/lib/libast/misc/stk.c | 93 ++++++++++--------------- src/lib/libast/regex/regnexec.c | 18 ++--- src/lib/libcmd/chgrp.c | 2 +- src/lib/libcmd/cp.c | 2 +- src/lib/libcmd/cut.c | 2 +- src/lib/libcmd/paste.c | 2 +- src/lib/libcmd/tail.c | 4 +- src/lib/libcmd/tee.c | 4 +- src/lib/libcmd/uname.c | 4 +- src/lib/libcmd/wclib.c | 2 +- 37 files changed, 316 insertions(+), 257 deletions(-) create mode 100644 src/lib/libast/include/stak.h diff --git a/src/cmd/ksh93/bltins/cd_pwd.c b/src/cmd/ksh93/bltins/cd_pwd.c index fa765fba6472..5cc3d4fc4d0c 100644 --- a/src/cmd/ksh93/bltins/cd_pwd.c +++ b/src/cmd/ksh93/bltins/cd_pwd.c @@ -2,7 +2,7 @@ * * * This software is part of the ast package * * Copyright (c) 1982-2012 AT&T Intellectual Property * -* Copyright (c) 2020-2023 Contributors to ksh 93u+m * +* Copyright (c) 2020-2024 Contributors to ksh 93u+m * * and is licensed under the * * Eclipse Public License, Version 2.0 * * * @@ -171,7 +171,7 @@ int b_cd(int argc, char *argv[],Shbltin_t *context) #endif /* _WINIX */ if(*stkptr(sh.stk,PATH_OFFSET)!='/') { - char *last=(char*)stkfreeze(sh.stk,1); + char *last = stkfreeze(sh.stk,1); stkseek(sh.stk,PATH_OFFSET); sfputr(sh.stk,oldpwd,-1); /* don't add '/' if oldpwd is / itself */ diff --git a/src/cmd/ksh93/bltins/getopts.c b/src/cmd/ksh93/bltins/getopts.c index 5374d088fff7..0e93110736be 100644 --- a/src/cmd/ksh93/bltins/getopts.c +++ b/src/cmd/ksh93/bltins/getopts.c @@ -2,7 +2,7 @@ * * * This software is part of the ast package * * Copyright (c) 1982-2012 AT&T Intellectual Property * -* Copyright (c) 2020-2023 Contributors to ksh 93u+m * +* Copyright (c) 2020-2024 Contributors to ksh 93u+m * * and is licensed under the * * Eclipse Public License, Version 2.0 * * * @@ -41,7 +41,7 @@ static int infof(Opt_t* op, Sfio_t* sp, const char* s, Optdisc_t* dp) #endif /* SHOPT_NAMESPACE */ { int savtop = stktell(stkp); - char *savptr = stkfreeze(stkp,0); + void *savptr = stkfreeze(stkp,0); sfputc(stkp,'$'); sfputc(stkp,'('); sfputr(stkp,s,')'); diff --git a/src/cmd/ksh93/bltins/test.c b/src/cmd/ksh93/bltins/test.c index 220534ab5e31..9a7464845224 100644 --- a/src/cmd/ksh93/bltins/test.c +++ b/src/cmd/ksh93/bltins/test.c @@ -2,7 +2,7 @@ * * * This software is part of the ast package * * Copyright (c) 1982-2012 AT&T Intellectual Property * -* Copyright (c) 2020-2023 Contributors to ksh 93u+m * +* Copyright (c) 2020-2024 Contributors to ksh 93u+m * * and is licensed under the * * Eclipse Public License, Version 2.0 * * * @@ -665,7 +665,7 @@ int sh_access(const char *name, int mode) maxgroups = (int)astconf_long(CONF_NGROUPS_MAX); } } - groups = (gid_t*)stkalloc(sh.stk,(maxgroups+1)*sizeof(gid_t)); + groups = stkalloc(sh.stk,(maxgroups+1)*sizeof(gid_t)); n = getgroups(maxgroups,groups); while(--n >= 0) { diff --git a/src/cmd/ksh93/bltins/typeset.c b/src/cmd/ksh93/bltins/typeset.c index 0bf2f823d0bc..e821933b69a5 100644 --- a/src/cmd/ksh93/bltins/typeset.c +++ b/src/cmd/ksh93/bltins/typeset.c @@ -233,7 +233,7 @@ int b_typeset(int argc,char *argv[],Shbltin_t *context) } else if(argv[0][0] != 't') /* not ypeset */ { - char **new_argv = (char **)stkalloc(sh.stk, (argc + 2) * sizeof(char*)); + char **new_argv = stkalloc(sh.stk, (argc + 2) * sizeof(char*)); error_info.id = new_argv[0] = SYSTYPESET->nvname; if(argv[0][0] == 'a') /* utoload == typeset -fu */ new_argv[1] = "-fu"; @@ -1614,7 +1614,7 @@ static void print_scan(Sfio_t *file, int flag, Dt_t *root, int option,struct tda if(flag==NV_LTOU || flag==NV_UTOL) tp->scanmask |= NV_UTOL|NV_LTOU; namec = nv_scan(root, nullscan, tp, tp->scanmask, flag&~NV_IARRAY); - argv = tp->argnam = (char**)stkalloc(sh.stk,(namec+1)*sizeof(char*)); + argv = tp->argnam = stkalloc(sh.stk,(namec+1)*sizeof(char*)); namec = nv_scan(root, pushname, tp, tp->scanmask, flag&~NV_IARRAY); if(mbcoll()) strsort(argv,namec,strcoll); diff --git a/src/cmd/ksh93/edit/completion.c b/src/cmd/ksh93/edit/completion.c index 9686025df4f9..fdb1e8578795 100644 --- a/src/cmd/ksh93/edit/completion.c +++ b/src/cmd/ksh93/edit/completion.c @@ -277,8 +277,8 @@ int ed_expand(Edit_t *ep, char outbuff[],int *cur,int *eol,int mode, int count) ep->e_nlist = 0; } } - comptr = (struct comnod*)stkalloc(sh.stk,sizeof(struct comnod)); - ap = (struct argnod*)stkseek(sh.stk,ARGVAL); + comptr = stkalloc(sh.stk,sizeof(struct comnod)); + ap = stkseek(sh.stk,ARGVAL); #if SHOPT_MULTIBYTE { int c = *cur; @@ -368,7 +368,7 @@ int ed_expand(Edit_t *ep, char outbuff[],int *cur,int *eol,int mode, int count) } if(addstar) sfputc(sh.stk,addstar); - ap = (struct argnod*)stkfreeze(sh.stk,1); + ap = stkfreeze(sh.stk,1); } if(mode!='*') sh_onoption(SH_MARKDIRS); diff --git a/src/cmd/ksh93/features/externs b/src/cmd/ksh93/features/externs index 957b9297a543..d9e9774053d0 100644 --- a/src/cmd/ksh93/features/externs +++ b/src/cmd/ksh93/features/externs @@ -83,7 +83,7 @@ tst note{ determining default number of extra bytes per argument for arguments l int bytec; error_info.id="_arg_extrabytes test (child)"; - argv = (char **)stkalloc(stkstd, (argmax / 2 + 1) * sizeof(char*)); + argv = stkalloc(stkstd, (argmax / 2 + 1) * sizeof(char*)); argc = bytec = 0; while(bytec < argmax) { diff --git a/src/cmd/ksh93/include/defs.h b/src/cmd/ksh93/include/defs.h index 59804a23a083..b5572683b5f6 100644 --- a/src/cmd/ksh93/include/defs.h +++ b/src/cmd/ksh93/include/defs.h @@ -2,7 +2,7 @@ * * * This software is part of the ast package * * Copyright (c) 1982-2012 AT&T Intellectual Property * -* Copyright (c) 2020-2023 Contributors to ksh 93u+m * +* Copyright (c) 2020-2024 Contributors to ksh 93u+m * * and is licensed under the * * Eclipse Public License, Version 2.0 * * * diff --git a/src/cmd/ksh93/sh/args.c b/src/cmd/ksh93/sh/args.c index cee50bf0b3e4..c39be337de80 100644 --- a/src/cmd/ksh93/sh/args.c +++ b/src/cmd/ksh93/sh/args.c @@ -681,7 +681,7 @@ char **sh_argbuild(int *nargs, const struct comnod *comptr,int flag) * TODO: find/fix root cause, eliminate argi */ argn = *nargs + 1; /* allow room to prepend args */ - comargn=(char**)stkalloc(sh.stk,(unsigned)(argn+1)*sizeof(char*)); + comargn = stkalloc(sh.stk,(unsigned)(argn+1)*sizeof(char*)); comargm = comargn += argn; *comargn = NULL; if(!argp) @@ -724,7 +724,7 @@ struct argnod *sh_argprocsub(struct argnod *argp) int savestates = sh_getstate(); char savejobcontrol = job.jobcontrol; unsigned int savesubshell = sh.subshell; - ap = (struct argnod*)stkseek(sh.stk,ARGVAL); + ap = stkseek(sh.stk,ARGVAL); ap->argflag |= ARG_MAKE; ap->argflag &= ~ARG_RAW; fd = argp->argflag&ARG_RAW; @@ -752,7 +752,7 @@ struct argnod *sh_argprocsub(struct argnod *argp) sfputr(sh.stk,sh.fifo,0); #endif /* SHOPT_DEVFD */ sfputr(sh.stk,fmtint(pv[fd],1),0); - ap = (struct argnod*)stkfreeze(sh.stk,0); + ap = stkfreeze(sh.stk,0); sh.inpipe = sh.outpipe = 0; /* turn off job control */ sh_offstate(SH_INTERACTIVE); diff --git a/src/cmd/ksh93/sh/expand.c b/src/cmd/ksh93/sh/expand.c index 10b307d9aba3..940dbe08cafc 100644 --- a/src/cmd/ksh93/sh/expand.c +++ b/src/cmd/ksh93/sh/expand.c @@ -115,7 +115,7 @@ int path_expand(const char *pattern, struct argnod **arghead, int musttrim) */ if((ap = (struct argnod*)gp->gl_list) && !ap->argnxt.ap && strcmp(ap->argval,trimmedpat)==0) { - gp->gl_list = (globlist_t*)stkalloc(sh.stk,ARGVAL+strlen(pattern)+1); + gp->gl_list = stkalloc(sh.stk,ARGVAL+strlen(pattern)+1); memcpy(gp->gl_list,ap,ARGVAL); /* copy fields *before* argval/gl_path */ strcpy(gp->gl_list->gl_path,pattern); } @@ -149,9 +149,9 @@ static int scantree(Dt_t *tree, const char *pattern, struct argnod **arghead) continue; if(strmatch(cp=nv_name(np),pattern)) { - (void)stkseek(sh.stk,ARGVAL); + stkseek(sh.stk,ARGVAL); sfputr(sh.stk,cp,-1); - ap = (struct argnod*)stkfreeze(sh.stk,1); + ap = stkfreeze(sh.stk,1); ap->argbegin = NULL; ap->argchn.ap = *arghead; ap->argflag = ARG_RAW|ARG_MAKE; @@ -431,13 +431,13 @@ int path_generate(struct argnod *todo, struct argnod **arghead, int musttrim) brace = *cp; *cp = 0; sh_sigcheck(); - ap = (struct argnod*)stkseek(sh.stk,ARGVAL); + ap = stkseek(sh.stk,ARGVAL); ap->argflag = ARG_RAW; ap->argchn.ap = todo; sfputr(sh.stk,apin->argval,-1); sfputr(sh.stk,pat,-1); sfputr(sh.stk,rescan,-1); - todo = ap = (struct argnod*)stkfreeze(sh.stk,1); + todo = ap = stkfreeze(sh.stk,1); if(brace == '}') break; if(!range) diff --git a/src/cmd/ksh93/sh/fault.c b/src/cmd/ksh93/sh/fault.c index daca5acafe6a..00899ac05209 100644 --- a/src/cmd/ksh93/sh/fault.c +++ b/src/cmd/ksh93/sh/fault.c @@ -495,7 +495,7 @@ int sh_trap(const char *trap, int mode) char was_no_trapdontexec = !sh.st.trapdontexec; char save_chldexitsig = sh.chldexitsig; int staktop = stktell(sh.stk); - char *savptr = stkfreeze(sh.stk,0); + void *savptr = stkfreeze(sh.stk,0); struct checkpt buff; Fcin_t savefc; fcsave(&savefc); diff --git a/src/cmd/ksh93/sh/init.c b/src/cmd/ksh93/sh/init.c index b9e342d9b947..6391b180b965 100644 --- a/src/cmd/ksh93/sh/init.c +++ b/src/cmd/ksh93/sh/init.c @@ -230,7 +230,7 @@ static int rand_shift; /* * Exception callback routine for stk(3) and sh_*alloc wrappers. */ -static noreturn char *nomemory(size_t s) +static noreturn void *nomemory(size_t s) { errormsg(SH_DICT, ERROR_SYSTEM|ERROR_PANIC, "out of memory (needed %llu bytes)", (uintmax_t)s); UNREACHABLE(); @@ -1281,7 +1281,7 @@ Shell_t *sh_init(int argc,char *argv[], Shinit_f userinit) sh.lex_context = sh_lexopen(0,1); sh.radixpoint = '.'; /* pre-locale init */ sh.strbuf = sfstropen(); - sh.stk = stkstd; + stkoverflow(sh.stk = stkstd, nomemory); sfsetbuf(sh.strbuf,NULL,64); error_info.catalog = e_dict; #if SHOPT_REGRESS @@ -1330,7 +1330,6 @@ Shell_t *sh_init(int argc,char *argv[], Shinit_f userinit) sh_ioinit(); /* initialize signal handling */ sh_siginit(); - stkinstall(NULL,nomemory); /* set up memory for name-value pairs */ sh.init_context = nv_init(); /* initialize shell type */ diff --git a/src/cmd/ksh93/sh/io.c b/src/cmd/ksh93/sh/io.c index d63d7f40414a..4af8da2afaf2 100644 --- a/src/cmd/ksh93/sh/io.c +++ b/src/cmd/ksh93/sh/io.c @@ -1190,7 +1190,7 @@ int sh_redirect(struct ionod *iop, int flag) { if(iof&IOLSEEK) { - struct argnod *ap = (struct argnod*)stkalloc(sh.stk,ARGVAL+strlen(iop->ioname)); + struct argnod *ap = stkalloc(sh.stk,ARGVAL+strlen(iop->ioname)); memset(ap, 0, ARGVAL); ap->argflag = ARG_MAC; strcpy(ap->argval,iop->ioname); @@ -1202,7 +1202,7 @@ int sh_redirect(struct ionod *iop, int flag) if((iof&IOPROCSUB) && !(iof&IOLSEEK)) { /* handle process substitution passed to redirection */ - struct argnod *ap = (struct argnod*)stkalloc(sh.stk,ARGVAL+strlen(iop->ioname)); + struct argnod *ap = stkalloc(sh.stk,ARGVAL+strlen(iop->ioname)); memset(ap, 0, ARGVAL); if(iof&IOPUT) ap->argflag = ARG_RAW; @@ -2201,7 +2201,7 @@ static int io_prompt(Sfio_t *iop,int flag) /* PS2 prompt. Save stack state to avoid corrupting command substitutions * in case we're executing a PS2.get discipline function at parse time. */ int savestacktop = stktell(sh.stk); - char *savestackptr = stkfreeze(sh.stk,0); + void *savestackptr = stkfreeze(sh.stk,0); cp = nv_getval(sh_scoped(PS2NOD)); stkset(sh.stk, savestackptr, savestacktop); break; diff --git a/src/cmd/ksh93/sh/lex.c b/src/cmd/ksh93/sh/lex.c index 5f208a6d7a80..9b1a432544ca 100644 --- a/src/cmd/ksh93/sh/lex.c +++ b/src/cmd/ksh93/sh/lex.c @@ -94,7 +94,8 @@ static void refvar(Lex_t *lp, int type) else { int n,offset = stktell(sh.stk); - char *savptr,*begin; + void *savptr; + char *begin; off = offset + (fcseek(0)-(type+1)) - fcfirst(); if(lp->lexd.kiaoff < offset) { @@ -152,7 +153,7 @@ static void lex_advance(Sfio_t *iop, const char *buff, int size, void *context) size -= (lp->lexd.first-(char*)buff); buff = lp->lexd.first; if(!lp->lexd.noarg) - lp->arg = (struct argnod*)stkseek(sh.stk,ARGVAL); + lp->arg = stkseek(sh.stk,ARGVAL); #if SHOPT_KIA lp->lexd.kiaoff += ARGVAL; #endif /* SHOPT_KIA */ @@ -1254,7 +1255,7 @@ int sh_lex(Lex_t* lp) state = fcfirst(); n = fcseek(0)-(char*)state; if(!lp->arg) - lp->arg = (struct argnod*)stkseek(sh.stk,ARGVAL); + lp->arg = stkseek(sh.stk,ARGVAL); if(n>0) sfwrite(sh.stk,state,n); sfputc(sh.stk,0); @@ -1288,7 +1289,7 @@ int sh_lex(Lex_t* lp) { /* Redirection of the form {varname}>file, etc. */ stkseek(sh.stk,stktell(sh.stk)-1); - lp->arg = (struct argnod*)stkfreeze(sh.stk,1); + lp->arg = stkfreeze(sh.stk,1); return lp->token=IOVNAME; } c = wordflags; @@ -1312,7 +1313,7 @@ int sh_lex(Lex_t* lp) } if(c==0 || (c&(ARG_MAC|ARG_EXP|ARG_MESSAGE))) { - lp->arg = (struct argnod*)stkfreeze(sh.stk,1); + lp->arg = stkfreeze(sh.stk,1); lp->arg->argflag = (c?c:ARG_RAW); } else if(mode==ST_NONE) @@ -2211,7 +2212,7 @@ static struct argnod *endword(int mode) stkseek(sh.stk,dp - (unsigned char*)stkptr(sh.stk,0)); if(mode<=0) { - argp = (struct argnod*)stkfreeze(sh.stk,0); + argp = stkfreeze(sh.stk,0); argp->argflag = ARG_RAW|ARG_QUOTED; } return argp; diff --git a/src/cmd/ksh93/sh/macro.c b/src/cmd/ksh93/sh/macro.c index 937c309b56da..cb4c8179fa48 100644 --- a/src/cmd/ksh93/sh/macro.c +++ b/src/cmd/ksh93/sh/macro.c @@ -1484,7 +1484,7 @@ static int varsub(Mac_t *mp) if(np && (type==M_BRACE ? !nv_isnull(np) : (type==M_TREE || !c || !ap))) { /* Either the parameter is set, or it's a special type of expansion where 'unset' doesn't apply. */ - char *savptr; + void *savptr; c = *((unsigned char*)stkptr(stkp,offset-1)); savptr = stkfreeze(stkp,0); if(type==M_VNAME || (type==M_SUBNAME && ap)) @@ -2156,7 +2156,8 @@ static void comsubst(Mac_t *mp,Shnode_t* t, int type) struct slnod *saveslp = sh.st.staklist; Mac_t savemac = *mp; int savtop = stktell(stkp); - char lastc=0, *savptr = stkfreeze(stkp,0); + char lastc = '\0'; + void *savptr = stkfreeze(stkp,0); int was_history = sh_isstate(SH_HISTORY); int was_verbose = sh_isstate(SH_VERBOSE); int was_interactive = sh_isstate(SH_INTERACTIVE); @@ -2572,7 +2573,7 @@ static void endfield(Mac_t *mp,int split) Stk_t *stkp = sh.stk; if(stktell(stkp) > ARGVAL || split) { - argp = (struct argnod*)stkfreeze(stkp,1); + argp = stkfreeze(stkp,1); argp->argnxt.cp = 0; argp->argflag = 0; mp->atmode = 0; diff --git a/src/cmd/ksh93/sh/name.c b/src/cmd/ksh93/sh/name.c index ac87dba3ae25..c5abc2ebb9c6 100644 --- a/src/cmd/ksh93/sh/name.c +++ b/src/cmd/ksh93/sh/name.c @@ -2224,7 +2224,7 @@ char **sh_envgen(void) data.attsize = 6; namec = nv_scan(sh.var_tree,nullscan,NULL,NV_EXPORT,NV_EXPORT); namec += sh.nenv; - er = (char**)stkalloc(sh.stk,(namec+4)*sizeof(char*)); + er = stkalloc(sh.stk,(namec+4)*sizeof(char*)); data.argnam = (er+=2) + sh.nenv; if(sh.nenv) memcpy(er,environ,sh.nenv*sizeof(char*)); diff --git a/src/cmd/ksh93/sh/nvtree.c b/src/cmd/ksh93/sh/nvtree.c index 9405a79f9fd1..58e185a5d95e 100644 --- a/src/cmd/ksh93/sh/nvtree.c +++ b/src/cmd/ksh93/sh/nvtree.c @@ -2,7 +2,7 @@ * * * This software is part of the ast package * * Copyright (c) 1982-2012 AT&T Intellectual Property * -* Copyright (c) 2020-2023 Contributors to ksh 93u+m * +* Copyright (c) 2020-2024 Contributors to ksh 93u+m * * and is licensed under the * * Eclipse Public License, Version 2.0 * * * @@ -942,7 +942,7 @@ static char *walk_tree(Namval_t *np, Namval_t *xp, int flags) Sfio_t *outfile; Sfoff_t off = 0; int len, savtop = stktell(sh.stk); - char *savptr = stkfreeze(sh.stk,0); + void *savptr = stkfreeze(sh.stk,0); struct argnod *ap=0; struct argnod *arglist=0; char *name,*cp, **argv; @@ -1006,7 +1006,7 @@ static char *walk_tree(Namval_t *np, Namval_t *xp, int flags) } stkseek(sh.stk,ARGVAL); sfputr(sh.stk,cp,-1); - ap = (struct argnod*)stkfreeze(sh.stk,1); + ap = stkfreeze(sh.stk,1); ap->argflag = ARG_RAW; ap->argchn.ap = arglist; n++; @@ -1018,7 +1018,7 @@ static char *walk_tree(Namval_t *np, Namval_t *xp, int flags) sh.var_tree = save_tree; return NULL; } - argv = (char**)stkalloc(sh.stk,(n+1)*sizeof(char*)); + argv = stkalloc(sh.stk,(n+1)*sizeof(char*)); argv += n; *argv = 0; for(; ap; ap=ap->argchn.ap) diff --git a/src/cmd/ksh93/sh/parse.c b/src/cmd/ksh93/sh/parse.c index 85a7b683b96a..ada018607935 100644 --- a/src/cmd/ksh93/sh/parse.c +++ b/src/cmd/ksh93/sh/parse.c @@ -68,7 +68,7 @@ static unsigned dcl_recursion; static int opt_get; -#define getnode(type) ((Shnode_t*)stkalloc(sh.stk,sizeof(struct type))) +#define getnode(type) stkalloc(sh.stk,sizeof(struct type)) #if SHOPT_KIA /* @@ -677,7 +677,7 @@ static struct regnod* syncase(Lex_t *lexp,int esym) struct regnod *r; if(tok==esym) return NULL; - r = (struct regnod*)stkalloc(sh.stk,sizeof(struct regnod)); + r = stkalloc(sh.stk,sizeof(struct regnod)); r->regptr=0; r->regflag=0; if(tok==LPAREN) @@ -736,7 +736,7 @@ static Shnode_t *arithfor(Lex_t *lexp,Shnode_t *tf) for(n=0; ; n++) { int c; - argp = (struct argnod*)stkseek(sh.stk,ARGVAL); + argp = stkseek(sh.stk,ARGVAL); argp->argnxt.ap = 0; argp->argchn.cp = 0; argp->argflag = argflag; @@ -757,7 +757,7 @@ static Shnode_t *arithfor(Lex_t *lexp,Shnode_t *tf) /* check for empty condition and treat as while((1)) */ if(offset==ARGVAL) sfputc(sh.stk,'1'); - argp = (struct argnod*)stkfreeze(sh.stk,1); + argp = stkfreeze(sh.stk,1); t = getanode(lexp,argp); if(n==0) tf = makelist(lexp,TLST,t,tw); @@ -767,7 +767,7 @@ static Shnode_t *arithfor(Lex_t *lexp,Shnode_t *tf) while((offset=fcpeek(0)) && isspace(offset)) fcseek(1); sfputr(sh.stk,fcseek(0),-1); - argp = (struct argnod*)stkfreeze(sh.stk,1); + argp = stkfreeze(sh.stk,1); fcrestore(&sav_input); if(n<2) { @@ -898,10 +898,10 @@ static Shnode_t *funct(Lex_t *lexp) jmpval = sigsetjmp(buff.buff,0); if(jmpval == 0) { - /* create a new stack frame to compile the command */ - savstak = stkopen(STK_SMALL); - savstak = stkinstall(savstak, 0); - slp = (struct slnod*)stkalloc(sh.stk,sizeof(struct slnod)+sizeof(struct functnod)); + /* create a new stack to compile the command */ + savstak = sh.stk; + sh.stk = stkopen(STK_SMALL); + slp = stkalloc(sh.stk,sizeof(struct slnod)+sizeof(struct functnod)); slp->slchild = 0; slp->slnext = sh.st.staklist; sh.st.staklist = 0; @@ -919,7 +919,7 @@ static Shnode_t *funct(Lex_t *lexp) fp->functnam = stkcopy(sh.stk,sh.st.filename); if(size) { - struct dolnod *dp = (struct dolnod*)stkalloc(sh.stk,size); + struct dolnod *dp = stkalloc(sh.stk,size); char *cp, *sp, **argv, **old = ((struct dolnod*)t->funct.functargs->comarg)->dolval+1; argv = ((char**)(dp->dolval))+1; dp->dolnum = ((struct dolnod*)t->funct.functargs->comarg)->dolnum; @@ -935,7 +935,7 @@ static Shnode_t *funct(Lex_t *lexp) { /* functname() simple_command: copy current word token to current stack frame */ size_t sz = ARGVAL + strlen(lexp->arg->argval) + 1; /* include terminating 0 */ - struct argnod *ap = (struct argnod*)stkalloc(sh.stk,sz); + struct argnod *ap = stkalloc(sh.stk,sz); memcpy(ap,lexp->arg,sz); lexp->arg = ap; } @@ -948,7 +948,8 @@ static Shnode_t *funct(Lex_t *lexp) /* restore the old stack */ if(slp) { - slp->slptr = stkinstall(savstak,0); + slp->slptr = sh.stk; + sh.stk = savstak; slp->slchild = sh.st.staklist; } #if SHOPT_KIA @@ -1055,14 +1056,14 @@ static struct argnod *assign(Lex_t *lexp, struct argnod *ap, int type) if((n=skipnl(lexp,0))==RPAREN || n==LPAREN) { struct argnod *ar,*aq,**settail; - ac = (struct comnod*)getnode(comnod); + ac = getnode(comnod); memset(ac,0,sizeof(*ac)); comarray: settail= &ac->comset; ac->comline = sh_getlineno(lexp); while(n==LPAREN) { - ar = (struct argnod*)stkseek(sh.stk,ARGVAL); + ar = stkseek(sh.stk,ARGVAL); ar->argflag= ARG_ASSIGN; sfprintf(sh.stk,"[%d]=",index++); if(aq=ac->comarg) @@ -1071,7 +1072,7 @@ static struct argnod *assign(Lex_t *lexp, struct argnod *ap, int type) sfprintf(sh.stk,"%s",aq->argval); ar->argflag |= aq->argflag; } - ar = (struct argnod*)stkfreeze(sh.stk,1); + ar = stkfreeze(sh.stk,1); ar->argnxt.ap = 0; if(!aq) ar = assign(lexp,ar,0); @@ -1082,11 +1083,11 @@ static struct argnod *assign(Lex_t *lexp, struct argnod *ap, int type) continue; while((n = skipnl(lexp,0))==0) { - ar = (struct argnod*)stkseek(sh.stk,ARGVAL); + ar = stkseek(sh.stk,ARGVAL); ar->argflag= ARG_ASSIGN; sfprintf(sh.stk,"[%d]=",index++); sfputr(sh.stk,lexp->arg->argval,-1); - ar = (struct argnod*)stkfreeze(sh.stk,1); + ar = stkfreeze(sh.stk,1); ar->argnxt.ap = 0; ar->argflag = lexp->arg->argflag; *settail = ar; @@ -1313,7 +1314,7 @@ static Shnode_t *item(Lex_t *lexp,int flag) /* some Linux scripts assume this */ if(sh_isoption(SH_NOEXEC)) errormsg(SH_DICT,ERROR_warn(0),e_lexemptyfor,sh.inlineno-(lexp->token=='\n')); - t->for_.forlst = (struct comnod*)getnode(comnod); + t->for_.forlst = getnode(comnod); (t->for_.forlst)->comarg = 0; (t->for_.forlst)->comset = 0; (t->for_.forlst)->comnamp = 0; @@ -1424,7 +1425,7 @@ static struct argnod *process_sub(Lex_t *lexp,int tok) Shnode_t *t; int mode = (tok==OPROCSYM); t = sh_cmd(lexp,RPAREN,SH_NL); - argp = (struct argnod*)stkalloc(sh.stk,sizeof(struct argnod)); + argp = stkalloc(sh.stk,sizeof(struct argnod)); *argp->argval = 0; argp->argchn.ap = (struct argnod*)makeparent(lexp,mode?TFORK|FPIN|FAMP|FPCL:TFORK|FPOU,t); argp->argflag = (ARG_EXP|mode); @@ -1454,7 +1455,7 @@ static Shnode_t *simple(Lex_t *lexp,int flag, struct ionod *io) flag |= SH_ARRAY; associative = 1; } - t = (struct comnod*)getnode(comnod); + t = getnode(comnod); t->comio=io; /* initial io chain */ /* set command line number for error messages */ t->comline = sh_getlineno(lexp); @@ -1490,7 +1491,7 @@ static Shnode_t *simple(Lex_t *lexp,int flag, struct ionod *io) { stkseek(sh.stk,ARGVAL); sfwrite(sh.stk,argp->argval,lexp->varnamelength); - ap=(struct argnod*)stkfreeze(sh.stk,1); + ap = stkfreeze(sh.stk,1); ap->argflag = ARG_RAW; ap->argchn.ap = 0; } @@ -1742,13 +1743,13 @@ static struct ionod *inout(Lex_t *lexp,struct ionod *lastio,int flag) return lastio; } lexp->digits=0; - iop=(struct ionod*) stkalloc(sh.stk,sizeof(struct ionod)); + iop = stkalloc(sh.stk,sizeof(struct ionod)); iop->iodelim = 0; if(token=sh_lex(lexp)) { if(token==RPAREN && (iof&IOLSEEK) && lexp->comsub) { - lexp->arg = (struct argnod*)stkalloc(sh.stk,sizeof(struct argnod)+3); + lexp->arg = stkalloc(sh.stk,sizeof(struct argnod)+3); strcpy(lexp->arg->argval,"CUR"); lexp->arg->argflag = ARG_RAW; iof |= IOARITH; @@ -1819,7 +1820,7 @@ static struct ionod *inout(Lex_t *lexp,struct ionod *lastio,int flag) if(errout) { /* redirect standard output to standard error */ - ioq = (struct ionod*)stkalloc(sh.stk,sizeof(struct ionod)); + ioq = stkalloc(sh.stk,sizeof(struct ionod)); memset(ioq,0,sizeof(*ioq)); ioq->ioname = "1"; ioq->iolst = 0; @@ -1880,7 +1881,7 @@ static struct argnod *qscan(struct comnod *ac,int argn) errormsg(SH_DICT,ERROR_warn(0),message,ac->comline); } /* leave space for an extra argument at the front */ - dp = (struct dolnod*)stkalloc(sh.stk,(unsigned)sizeof(struct dolnod) + ARG_SPARE*sizeof(char*) + argn*sizeof(char*)); + dp = stkalloc(sh.stk,(unsigned)sizeof(struct dolnod) + ARG_SPARE*sizeof(char*) + argn*sizeof(char*)); cp = dp->dolval+ARG_SPARE; dp->dolnum = argn; dp->dolbot = ARG_SPARE; diff --git a/src/cmd/ksh93/sh/streval.c b/src/cmd/ksh93/sh/streval.c index cfff4b39204b..79f75ee1402f 100644 --- a/src/cmd/ksh93/sh/streval.c +++ b/src/cmd/ksh93/sh/streval.c @@ -172,7 +172,7 @@ Sfdouble_t arith_exec(Arith_t *ep) if(ep->staksize < SMALL_STACK) sp = small_stack; else - sp = (Sfdouble_t*)stkalloc(sh.stk,ep->staksize*(sizeof(Sfdouble_t)+1)); + sp = stkalloc(sh.stk,ep->staksize*(sizeof(Sfdouble_t)+1)); tp = (char*)(sp+ep->staksize); tp--,sp--; while(c = *cp++) @@ -915,7 +915,7 @@ Arith_t *arith_compile(const char *string,char **last,Sfdouble_t(*fun)(const cha } sfputc(sh.stk,0); offset = stktell(sh.stk); - ep = (Arith_t*)stkfreeze(sh.stk,0); + ep = stkfreeze(sh.stk,0); ep->expr = string; ep->elen = strlen(string); ep->code = (unsigned char*)(ep+1); diff --git a/src/cmd/ksh93/sh/string.c b/src/cmd/ksh93/sh/string.c index 9f07233f236e..7cb1ab2a13c7 100644 --- a/src/cmd/ksh93/sh/string.c +++ b/src/cmd/ksh93/sh/string.c @@ -2,7 +2,7 @@ * * * This software is part of the ast package * * Copyright (c) 1982-2012 AT&T Intellectual Property * -* Copyright (c) 2020-2023 Contributors to ksh 93u+m * +* Copyright (c) 2020-2024 Contributors to ksh 93u+m * * and is licensed under the * * Eclipse Public License, Version 2.0 * * * diff --git a/src/cmd/ksh93/sh/trestore.c b/src/cmd/ksh93/sh/trestore.c index 455b87a6c2ae..b5bdad8fbd18 100644 --- a/src/cmd/ksh93/sh/trestore.c +++ b/src/cmd/ksh93/sh/trestore.c @@ -2,7 +2,7 @@ * * * This software is part of the ast package * * Copyright (c) 1982-2011 AT&T Intellectual Property * -* Copyright (c) 2020-2023 Contributors to ksh 93u+m * +* Copyright (c) 2020-2024 Contributors to ksh 93u+m * * and is licensed under the * * Eclipse Public License, Version 2.0 * * * @@ -40,7 +40,7 @@ static void r_comarg(struct comnod*); static Sfio_t *infile; -#define getnode(type) ((Shnode_t*)stkalloc(sh.stk,sizeof(struct type))) +#define getnode(type) stkalloc(sh.stk,sizeof(struct type)) Shnode_t *sh_trestore(Sfio_t *in) { @@ -135,9 +135,9 @@ static Shnode_t *r_tree(void) t->funct.functloc = -1; t->funct.functline = sfgetu(infile); t->funct.functnam = r_string(); - savstak = stkopen(STK_SMALL); - savstak = stkinstall(savstak, 0); - slp = (struct slnod*)stkalloc(sh.stk,sizeof(struct slnod)+sizeof(struct functnod)); + savstak = sh.stk; + sh.stk = stkopen(STK_SMALL); + slp = stkalloc(sh.stk,sizeof(struct slnod)+sizeof(struct functnod)); slp->slchild = 0; slp->slnext = sh.st.staklist; sh.st.staklist = 0; @@ -149,7 +149,8 @@ static Shnode_t *r_tree(void) t->funct.functtre = r_tree(); t->funct.functstak = slp; t->funct.functargs = (struct comnod*)r_tree(); - slp->slptr = stkinstall(savstak,0); + slp->slptr = sh.stk; + sh.stk = savstak; slp->slchild = sh.st.staklist; break; } @@ -177,7 +178,7 @@ static struct argnod *r_arg(void) Stk_t *stkp=sh.stk; while((l=sfgetu(infile))>0) { - ap = (struct argnod*)stkseek(stkp,(unsigned)l+ARGVAL); + ap = stkseek(stkp,(unsigned)l+ARGVAL); if(!aptop) aptop = ap; else @@ -190,7 +191,7 @@ static struct argnod *r_arg(void) ap->argval[l] = 0; ap->argchn.cp = 0; ap->argflag = sfgetc(infile); - ap = (struct argnod*)stkfreeze(stkp,0); + ap = stkfreeze(stkp,0); if(*ap->argval==0 && (ap->argflag&ARG_EXP)) ap->argchn.ap = (struct argnod*)r_tree(); else if(*ap->argval==0 && (ap->argflag&~(ARG_APPEND|ARG_MESSAGE|ARG_QUOTED|ARG_ARRAY))==0) @@ -286,7 +287,7 @@ static struct dolnod *r_comlist(void) char **argv; if((l=sfgetl(infile))>0) { - dol = (struct dolnod*)stkalloc(sh.stk,sizeof(struct dolnod) + sizeof(char*)*(l+ARG_SPARE)); + dol = stkalloc(sh.stk,sizeof(struct dolnod) + sizeof(char*)*(l+ARG_SPARE)); dol->dolnum = l; dol->dolbot = ARG_SPARE; argv = dol->dolval+ARG_SPARE; @@ -301,7 +302,7 @@ static struct regnod *r_switch(void) struct regnod *reg=0,*regold,*regtop=0; while((l=sfgetl(infile))>=0) { - reg = (struct regnod*)getnode(regnod); + reg = getnode(regnod); if(!regtop) regtop = reg; else diff --git a/src/cmd/ksh93/sh/xec.c b/src/cmd/ksh93/sh/xec.c index 6fcf2a4e4338..924acf266d02 100644 --- a/src/cmd/ksh93/sh/xec.c +++ b/src/cmd/ksh93/sh/xec.c @@ -529,8 +529,8 @@ int sh_debug(const char *trap, const char *name, const char *subscript, char *co { Namval_t *np = SH_COMMANDNOD; int n=4, offset=stktell(sh.stk); - char *sav = stkfreeze(sh.stk,0); - struct sh_scoped *savst = (struct sh_scoped*)stkalloc(sh.stk,sizeof(struct sh_scoped)); + void *sav = stkfreeze(sh.stk,0); + struct sh_scoped *savst = stkalloc(sh.stk,sizeof(struct sh_scoped)); const char *cp = "+=( "; if(sh.indebug) return 0; @@ -597,7 +597,7 @@ int sh_eval(Sfio_t *iop, int mode) struct slnod *saveslp = sh.st.staklist; int jmpval; struct checkpt *pp = (struct checkpt*)sh.jmplist; - struct checkpt *buffp = (struct checkpt*)stkalloc(sh.stk,sizeof(struct checkpt)); + struct checkpt *buffp = stkalloc(sh.stk,sizeof(struct checkpt)); static Sfio_t *io_save; volatile int traceon=0, lineno=0; int binscript=sh.binscript; @@ -1035,7 +1035,7 @@ int sh_exec(const Shnode_t *t, int flags) else { /* avoid exit on error from nv_setlist, e.g. read-only variable */ - struct checkpt *chkp = (struct checkpt*)stkalloc(sh.stk,sizeof(struct checkpt)); + struct checkpt *chkp = stkalloc(sh.stk,sizeof(struct checkpt)); sh_pushcontext(chkp,SH_JMPCMD); jmpval = sigsetjmp(chkp->buff,0); if(!jmpval) @@ -1147,7 +1147,7 @@ int sh_exec(const Shnode_t *t, int flags) volatile void *save_data; int save_prompt; int was_nofork = execflg?sh_isstate(SH_NOFORK):0; - struct checkpt *buffp = (struct checkpt*)stkalloc(sh.stk,sizeof(struct checkpt)); + struct checkpt *buffp = stkalloc(sh.stk,sizeof(struct checkpt)); bp = &sh.bltindata; save_ptr = bp->ptr; save_data = bp->data; @@ -1294,7 +1294,7 @@ int sh_exec(const Shnode_t *t, int flags) { volatile int indx; volatile char scope = 0; - struct checkpt *buffp = (struct checkpt*)stkalloc(sh.stk,sizeof(struct checkpt)); + struct checkpt *buffp = stkalloc(sh.stk,sizeof(struct checkpt)); #if SHOPT_NAMESPACE Namval_t *namespace=0; #endif /* SHOPT_NAMESPACE */ @@ -1340,8 +1340,8 @@ int sh_exec(const Shnode_t *t, int flags) if(nv_isattr(np,NV_STATICF) && (mp=nv_type(nq))) nq = mp; sh.last_table = last_table; - nodep = (Namval_t*)stkalloc(sh.stk,sizeof(Namval_t)); - nrp = (struct Namref*)stkalloc(sh.stk,sizeof(struct Namref)); + nodep = stkalloc(sh.stk,sizeof(Namval_t)); + nrp = stkalloc(sh.stk,sizeof(struct Namref)); mode = set_instance(nq,nodep,nrp); } if(io) @@ -1514,7 +1514,7 @@ int sh_exec(const Shnode_t *t, int flags) */ { volatile int jmpval; - struct checkpt *buffp = (struct checkpt*)stkalloc(sh.stk,sizeof(struct checkpt)); + struct checkpt *buffp = stkalloc(sh.stk,sizeof(struct checkpt)); struct ionod *iop; int rewrite=0; #if !SHOPT_DEVFD @@ -1656,7 +1656,7 @@ int sh_exec(const Shnode_t *t, int flags) pid_t pid = 0; int jmpval, waitall = 0; int simple = (t->fork.forktre->tre.tretyp&COMMSK)==TCOM; - struct checkpt *buffp = (struct checkpt*)stkalloc(sh.stk,sizeof(struct checkpt)); + struct checkpt *buffp = stkalloc(sh.stk,sizeof(struct checkpt)); if(sh.subshell && !sh.subshare && t->fork.forkio) { /* Subshell forking workaround for https://github.com/ksh93/ksh/issues/161 @@ -1747,7 +1747,7 @@ int sh_exec(const Shnode_t *t, int flags) /* This is the last command, so avoid creating a subshell */ char *savsig; int nsig,jmpval; - struct checkpt *buffp = (struct checkpt*)stkalloc(sh.stk,sizeof(struct checkpt)); + struct checkpt *buffp = stkalloc(sh.stk,sizeof(struct checkpt)); sh.st.otrapcom = 0; if((nsig=sh.st.trapmax*sizeof(char*))>0 || sh.st.trapcom[0]) { @@ -1808,7 +1808,7 @@ int sh_exec(const Shnode_t *t, int flags) job.curpgid = 0; while((tn=tn->lst.lstrit) && tn->tre.tretyp==TFIL) job.waitall++; - exitval = job.exitval = (int*)stkalloc(sh.stk,job.waitall*sizeof(int)); + exitval = job.exitval = stkalloc(sh.stk,job.waitall*sizeof(int)); memset(exitval,0,job.waitall*sizeof(int)); } else @@ -1937,7 +1937,7 @@ int sh_exec(const Shnode_t *t, int flags) char *av[5]; #if SHOPT_OPTIMIZE int jmpval = ((struct checkpt*)sh.jmplist)->mode; - struct checkpt *buffp = (struct checkpt*)stkalloc(sh.stk,sizeof(struct checkpt)); + struct checkpt *buffp = stkalloc(sh.stk,sizeof(struct checkpt)); void *optlist = sh.optlist; sh.optlist = 0; sh_tclear(t->for_.fortre); @@ -2071,7 +2071,7 @@ int sh_exec(const Shnode_t *t, int flags) #endif /* SHOPT_FILESCAN */ #if SHOPT_OPTIMIZE int jmpval = ((struct checkpt*)sh.jmplist)->mode; - struct checkpt *buffp = (struct checkpt*)stkalloc(sh.stk,sizeof(struct checkpt)); + struct checkpt *buffp = stkalloc(sh.stk,sizeof(struct checkpt)); void *optlist = sh.optlist; sh.optlist = 0; sh_tclear(t->wh.whtre); @@ -2340,7 +2340,7 @@ int sh_exec(const Shnode_t *t, int flags) Namval_t *oldnspace = sh.namespace; int offset = stktell(sh.stk); int flags=NV_NOARRAY|NV_VARNAME; - struct checkpt *chkp = (struct checkpt*)stkalloc(sh.stk,sizeof(struct checkpt)); + struct checkpt *chkp = stkalloc(sh.stk,sizeof(struct checkpt)); int jmpval; if(cp) { @@ -2648,15 +2648,15 @@ int sh_exec(const Shnode_t *t, int flags) int sh_run(int argn, char *argv[]) { struct dolnod *dp; - struct comnod *t = (struct comnod*)stkalloc(sh.stk,sizeof(struct comnod)); + struct comnod *t = stkalloc(sh.stk,sizeof(struct comnod)); int savtop = stktell(sh.stk); - char *savptr = stkfreeze(sh.stk,0); + void *savptr = stkfreeze(sh.stk,0); Opt_t *op, *np = optctx(0, 0); Shbltin_t bltindata; bltindata = sh.bltindata; op = optctx(np, 0); memset(t, 0, sizeof(struct comnod)); - dp = (struct dolnod*)stkalloc(sh.stk, (unsigned)sizeof(struct dolnod) + ARG_SPARE*sizeof(char*) + argn*sizeof(char*)); + dp = stkalloc(sh.stk, (unsigned)sizeof(struct dolnod) + ARG_SPARE*sizeof(char*) + argn*sizeof(char*)); dp->dolnum = argn; dp->dolbot = ARG_SPARE; memcpy(dp->dolval+ARG_SPARE, argv, (argn+1)*sizeof(char*)); @@ -2948,7 +2948,7 @@ int sh_funscope(int argn, char *argv[],int(*fun)(void*),void *arg,int execflg) char *trap; int nsig; struct dolnod *argsav=0,*saveargfor; - struct sh_scoped *savst = (struct sh_scoped*)stkalloc(sh.stk,sizeof(struct sh_scoped)); + struct sh_scoped *savst = stkalloc(sh.stk,sizeof(struct sh_scoped)); struct sh_scoped *prevscope = sh.st.self; struct argnod *envlist=0; int isig,jmpval; @@ -2957,7 +2957,7 @@ int sh_funscope(int argn, char *argv[],int(*fun)(void*),void *arg,int execflg) char save_invoc_local; char **savsig, *save_debugtrap = 0; struct funenv *fp = 0; - struct checkpt *buffp = (struct checkpt*)stkalloc(sh.stk,sizeof(struct checkpt)); + struct checkpt *buffp = stkalloc(sh.stk,sizeof(struct checkpt)); Namval_t *nspace = sh.namespace; Dt_t *last_root = sh.last_root; Shopt_t options; @@ -3212,7 +3212,7 @@ int sh_fun(Namval_t *np, Namval_t *nq, char *argv[]) if(nq) mode = set_instance(nq,&node, &nr); jmpthresh = is_abuiltin(np) ? SH_JMPCMD : SH_JMPFUN; - checkpoint = (struct checkpt*)stkalloc(sh.stk,sizeof(struct checkpt)); + checkpoint = stkalloc(sh.stk,sizeof(struct checkpt)); sh_pushcontext(checkpoint, jmpthresh); jmpval = sigsetjmp(checkpoint->buff,1); if(jmpval == 0) @@ -3309,7 +3309,7 @@ static void sigreset(int mode) static pid_t sh_ntfork(const Shnode_t *t,char *argv[],int *jobid,int topfd) { static pid_t spawnpid; - struct checkpt *buffp = (struct checkpt*)stkalloc(sh.stk,sizeof(struct checkpt)); + struct checkpt *buffp = stkalloc(sh.stk,sizeof(struct checkpt)); int jmpval,jobfork=0; volatile int scope=0, sigwasset=0; char **arge, *path; diff --git a/src/lib/libast/Mamfile b/src/lib/libast/Mamfile index be28ed7d21c8..b306b133ab6d 100644 --- a/src/lib/libast/Mamfile +++ b/src/lib/libast/Mamfile @@ -4966,7 +4966,7 @@ make install virtual done done note * ...main AST headers - loop HDR ast ast_dir ast_getopt ast_std ast_windows ccode cdt cmdarg debug dt error find ftwalk fts glob hash hashkey hashpart ip6 ls magic mc mime mnt modecanon modex namval option proc recfmt regex sfio sfio_s sfio_t sfdisc shcmd stk swap tar times tm tok usage vdb vmalloc wait magicid fnv aso + loop HDR ast ast_dir ast_getopt ast_std ast_windows ccode cdt cmdarg debug dt error find ftwalk fts glob hash hashkey hashpart ip6 ls magic mc mime mnt modecanon modex namval option proc recfmt regex sfio sfio_s sfio_t sfdisc shcmd stk stak swap tar times tm tok usage vdb vmalloc wait magicid fnv aso make ${INSTALLROOT}/include/ast/${HDR}.h prev include/${HDR}.h exec - cp -f ${<} ${@} diff --git a/src/lib/libast/features/api b/src/lib/libast/features/api index dada8fbe54e8..fe12dd27897d 100644 --- a/src/lib/libast/features/api +++ b/src/lib/libast/features/api @@ -1,6 +1,6 @@ iff AST_API -ver ast 20230909 +ver ast 20240227 api ast 20120528 regexec regnexec regrexec regsubexec diff --git a/src/lib/libast/include/stak.h b/src/lib/libast/include/stak.h new file mode 100644 index 000000000000..8d4a18b60aec --- /dev/null +++ b/src/lib/libast/include/stak.h @@ -0,0 +1,51 @@ +/*********************************************************************** +* * +* This software is part of the ast package * +* Copyright (c) 1985-2011 AT&T Intellectual Property * +* Copyright (c) 2020-2024 Contributors to ksh 93u+m * +* and is licensed under the * +* Eclipse Public License, Version 2.0 * +* * +* A copy of the License is available at * +* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html * +* (with md5 checksum 84283fa8859daf213bdda5a9f8d1be1d) * +* * +* Glenn Fowler * +* David Korn * +* Phong Vo * +* Martijn Dekker * +* * +***********************************************************************/ +/* + * David Korn + * AT&T Research + * + * Obsolete interface definitions for a stack-like storage library. + * These now simply map onto the current stk(3) functions as below. + */ + +#ifndef _STAK_H +#define _STAK_H + +#include + +#define Stak_t Sfio_t +#define staksp stkstd +#define STAK_SMALL STK_SMALL + +#define stakptr(n) stkptr(stkstd,n) +#define staktell() stktell(stkstd) +#define stakputc(c) sfputc(stkstd,(c)) +#define stakwrite(b,n) sfwrite(stkstd,(b),(n)) +#define stakputs(s) (sfputr(stkstd,(s),0),--stkstd->_next) +#define stakseek(n) stkseek(stkstd,n) +#define stakcreate(n) stkopen(n) +#define stakinstall(s,f) stkinstall(s,f) +#define stakdelete(s) stkclose(s) +#define staklink(s) stklink(s) +#define stakalloc(n) stkalloc(stkstd,n) +#define stakcopy(s) stkcopy(stkstd,s) +#define stakset(c,n) stkset(stkstd,c,n) +#define stakfreeze(n) stkfreeze(stkstd,n) + +#endif diff --git a/src/lib/libast/include/stk.h b/src/lib/libast/include/stk.h index 9593e1849fab..17ad8e9068e6 100644 --- a/src/lib/libast/include/stk.h +++ b/src/lib/libast/include/stk.h @@ -2,7 +2,7 @@ * * * This software is part of the ast package * * Copyright (c) 1985-2011 AT&T Intellectual Property * -* Copyright (c) 2020-2022 Contributors to ksh 93u+m * +* Copyright (c) 2020-2024 Contributors to ksh 93u+m * * and is licensed under the * * Eclipse Public License, Version 2.0 * * * @@ -35,25 +35,27 @@ #define Stk_t Sfio_t -#define STK_SMALL 1 /* small stkopen stack */ +/* option bits for stkopen() */ +#define STK_SMALL 1 /* allocate small stack frames */ #define STK_NULL 2 /* return NULL on overflow */ #define stkptr(sp,n) ((char*)((sp)->_data)+(n)) #define stktop(sp) ((char*)(sp)->_next) #define stktell(sp) ((sp)->_next-(sp)->_data) -#define stkseek(sp,n) ((n)==0?(char*)((sp)->_next=(sp)->_data):_stkseek(sp,n)) +#define stkseek(sp,n) ((n)==0?(void*)((sp)->_next=(sp)->_data):_stkseek(sp,n)) extern Sfio_t _Stk_data; extern Stk_t* stkopen(int); -extern Stk_t* stkinstall(Stk_t*, char*(*)(size_t)); +extern Stk_t* stkinstall(Stk_t*, char*(*)(size_t)); /* deprecated */ +extern void stkoverflow(Stk_t*, void*(*)(size_t)); extern int stkclose(Stk_t*); extern unsigned int stklink(Stk_t*); -extern char* stkalloc(Stk_t*, size_t); +extern void* stkalloc(Stk_t*, size_t); extern char* stkcopy(Stk_t*, const char*); -extern char* stkset(Stk_t*, char*, size_t); -extern char* _stkseek(Stk_t*, ssize_t); -extern char* stkfreeze(Stk_t*, size_t); +extern void* stkset(Stk_t*, void*, size_t); +extern void* _stkseek(Stk_t*, ssize_t); +extern void* stkfreeze(Stk_t*, size_t); extern int stkon(Stk_t*, char*); #endif diff --git a/src/lib/libast/man/stk.3 b/src/lib/libast/man/stk.3 index cfc2959adb71..2a88017de324 100644 --- a/src/lib/libast/man/stk.3 +++ b/src/lib/libast/man/stk.3 @@ -4,24 +4,30 @@ \fBstk\fR \- data stack storage library .SH SYNOPSIS .ta .75i 1.5i 2.25i 3i 3.75i 4.5i 5.25i 6i +.de Ss +.fl +.ne 3 +.SS "\\$1" +.. .PP .nf \f3 #include Stk_t *stkopen(int \fIflags\fP); +void stkoverflow(Stk_t *\fIstack\fP, void *(\fIoverflow\fP)(size_t)); Stk_t *stkinstall(Stk_t *\fIstack\fP, char *(\fIoverflow\fP)(size_t)); int stkclose(Stk_t *\fIstack\fP); unsigned int stklink(Stk_t *\fIstack\fP) -char *stkalloc(Stk_t *\fIstack\fP, unsigned \fIsize\fP); +void *stkalloc(Stk_t *\fIstack\fP, unsigned \fIsize\fP); char *stkcopy(Stk_t *\fIstack\fP, const char *\fIstring\fP); -char *stkset(Stk_t *\fIstack\fP, char *\fIaddress\fP, unsigned \fIoffset\fP); +void *stkset(Stk_t *\fIstack\fP, void *\fIaddress\fP, unsigned \fIoffset\fP); -char *stkseek(Stk_t *\fIstack\fP, unsigned \fIoffset\fP); +void *stkseek(Stk_t *\fIstack\fP, unsigned \fIoffset\fP); int stktell(Stk_t *\fIstack\fP); char *stkptr(Stk_t *\fIstack\fP, unsigned \fIoffset\fP); -char *stkfreeze(Stk_t *\fIstack\fP, unsigned \fIextra\fP); +void *stkfreeze(Stk_t *\fIstack\fP, unsigned \fIextra\fP); int stkon(Stk *\fIstack\fP, char* \fIaddr\fP) \fR .fi @@ -33,16 +39,15 @@ A stack abstraction consists of an ordered list of contiguous memory regions, called stack frames, that can hold objects of arbitrary size. A stack is represented by the type \f3Stk_t\fP -defined in header \f3\fP. +defined in the header \f3\fP. The type \f3Stk_t\fP is compatible with the type \f3Sfio_t\fP defined by the \f3sfio\fP(3) library. +There is a preset initial stack which can be referenced +by the constant \f3stkstd\fP (stack standard). .PP -At any instant there is one active stack which can be referenced -by the constant \f3stkstd\fP. -Variable size objects can be -added to the active stack +Objects of variable size can be +added to a stack, and programs can reference these objects directly with pointers. -.PP In addition, the last object on the stack (referred to here as the current object) can be built incrementally. @@ -53,50 +58,72 @@ its location might change so that it is necessary to reference the object with relative offsets ranging from zero to the current offset of the object. .PP -There is a preset initial active stack. -To use an additional stack, it is necessary to create it and to -install it as the active stack. +The \f3sfio\fP(3) output functions can be used to build +the current object incrementally. +An object that is built incrementally on the stack will +always occupy contiguous memory within a stack frame, +but until \f3stkfreeze\fP() is called, +the location in memory for the object can change. +There is a current offset associated with the current object that +determines where subsequent operations apply. +Initially, this offset is zero, and the offset changes as a result +of the operations you specify. +.Ss "Descriptions of the functions" .PP -A stack is created with the \f3stkopen\fP() function. +The \f3stkopen\fP() function creates a new stack. The \fIflags\fP argument is an options bitmask. If the \f3STK_SMALL\fP bit is set, the stack allocates memory in -small blocks, optimizing for memory usage at the expense of performance. +small blocks, optimizing memory usage at the expense of performance. If the \f3STK_NULL\fP bit is set, a stack overflow will cause stack operations to return a null pointer instead of throwing an exception. +If the \f3STK_NULL\fP bit is not set and the \f3stkstd\fP stack has +a custom overflow function set (see \f3stkoverflow\fP() below), +then the new stack inherits the pointer to that overflow function. If successful, \f3stkopen\fP() returns a pointer to a stack whose reference count is 1. Otherwise, \f3stkopen\fP() returns a null pointer. .PP -The \f3stklink\fP() function increases the reference count for the -given \fIstack\fP and returns the increased count. -.PP -The \f3stkinstall\fP() function -makes the specified \fIstack\fP the active stack and returns a pointer -to the previous active stack. -If the \fIoverflow\fP argument is not null and the stack was not opened with -the \f3STK_NULL\fP option, \fIoverflow\fP specifies a function that will +The \f3stkoverflow\fP() function specifies a function that will be called whenever \f3malloc\fP(3) fails while trying to grow the stack. The \fIoverflow\fP function will be called with the size that was passed to \f3malloc\fP(3). The \fIoverflow\fP function can call \f3exit\fP(3), call \f3longjmp\fP(3) or return. -If the \f3overflow\fP function returns, +If the \f2overflow\fP function returns, it must return a pointer to a memory region of the given size. -The default action is to write an error to standard error and to +If \f2overflow\fP is \f3NULL\fP, then the stack overflow action +is reset to the default. +If the stack was opened with the \f3STK_NULL\fP option, +the default is to return a null pointer, +otherwise the default is to write an error to standard error and to call \f3exit\fP(2) with a non-zero exit value. +.PP +The \f3stkinstall\fP() function +is deprecated and provided for backward compatibility. +\f3stkinstall\fP() makes \f3stkstd\fP refer to the specified \fIstack\fP pointer +and returns a pointer to the previous \f3stkstd\fP stack. +If the \fIoverflow\fP argument is not null and the stack was not opened with +the \f3STK_NULL\fP option, then \fIoverflow\fP sets the overflow action +(see \f3overflow\fP() above). When \fIstack\fP is a null pointer, -the active stack is not changed -but the \fIoverflow\fP function for the active stack can be changed -and a pointer to the active stack is returned. +the \f3stkstd\fP stack is not changed +but the \fIoverflow\fP function for the \f3stkstd\fP stack can be changed +and a pointer to the \f3stkstd\fP stack is returned. +(Current usage is simply +passing the pointer to the desired stack to the \f3stk\fP functions, +and using \f3stkoverflow\fP() to set the stack overflow function). .PP The \f3stkclose\fP() function decrements the reference count and frees the memory associated with the specified stack when the reference count is zero. The effect of subsequent references to objects -on the stack are undefined. +on the stack is undefined. +.PP +The \f3stklink\fP() function increases the reference count for the +given \fIstack\fP and returns the increased count. .PP The \f3stkalloc\fP() function returns an aligned pointer to space on the @@ -122,17 +149,6 @@ If \fIaddress\fP is null, the stack is reset to the beginning. If it is non-null, but is not the address of an object on the stack, the program aborts and dumps core. .PP -The \f3sfio\fP(3) output functions can be used to build -current object incrementally. -An object that is built incrementally on the stack will -always occupy contiguous memory within a stack frame but -until \f3stkfreeze\fP() is called, -the location in memory for the object can change. -There is a current offset associated with the current object that -determines where subsequent operations apply. -Initially, this offset is zero, and the offset changes as a result -of the operations you specify. -.PP The \f3stkseek\fP() function is used set the offset for the current object. The \fIoffset\fP argument to \f3stkseek\fP() specifies the new @@ -149,6 +165,9 @@ for the current object into a memory address on the stack. This address is only valid until another stack operation is given. The result is not defined if \fIoffset\fP exceeds the size of the current object. +This function returns a \f3char\fP pointer instead of \f3void\fP pointer +to make it easy to dereference the return value directly and read +an individual byte on the stack. .PP The \f3stkfreeze\fP() function terminates the current object on the @@ -167,10 +186,17 @@ interface was derived from similar routines in the KornShell code that is used for building parse trees and carrying out expansions. It provides an efficient mechanism for grouping dynamically allocated objects so that they can be freed all at once rather than individually. +.PP +In 2024, as part of changes made by +Martijn Dekker for ksh 93u+m, +the return types of \f3stkalloc\fP(), \f3stkset\fP(), \f3stkseek\fP() and +\f3stkfreeze\fP() were changed from \f3char*\fP to \f3void*\fP, +the \f3stkoverflow\fP() function was added, +and the \f3stkinstall\fP() function was deprecated. .SH AUTHOR - David Korn +David Korn .SH SEE ALSO -\f3exit(2)\fP -\f3longjmp(3)\fP -\f3malloc(3)\fP -\f3sfio(3)\fP +\f3exit\fP(2), +\f3longjmp\fP(3), +\f3malloc\fP(3), +\f3sfio\fP(3) diff --git a/src/lib/libast/misc/glob.c b/src/lib/libast/misc/glob.c index bb9a0293e6d1..88a8523f95db 100644 --- a/src/lib/libast/misc/glob.c +++ b/src/lib/libast/misc/glob.c @@ -73,6 +73,8 @@ typedef int (*GL_stat_f)(const char*, struct stat*); #include +static Stk_t *globstk = stkstd; + /* * default gl_diropen */ @@ -158,7 +160,7 @@ static char* gl_nextdir(glob_t* gp, char* dir) { if (!(dir = gp->gl_nextpath)) - dir = gp->gl_nextpath = stkcopy(stkstd,pathbin()); + dir = gp->gl_nextpath = stkcopy(globstk,pathbin()); switch (*gp->gl_nextpath) { case 0: @@ -237,46 +239,46 @@ addmatch(glob_t* gp, const char* dir, const char* pat, const char* rescan, char* int offset; int type; - stkseek(stkstd,MATCHPATH(gp)); + stkseek(globstk,MATCHPATH(gp)); if (dir) { - sfputr(stkstd,dir,-1); - sfputc(stkstd,gp->gl_delim); + sfputr(globstk,dir,-1); + sfputc(globstk,gp->gl_delim); } if (endslash) *endslash = 0; - sfputr(stkstd,pat,-1); + sfputr(globstk,pat,-1); if (rescan) { - if ((*gp->gl_type)(gp, stkptr(stkstd,MATCHPATH(gp)), 0) != GLOB_DIR) + if ((*gp->gl_type)(gp, stkptr(globstk,MATCHPATH(gp)), 0) != GLOB_DIR) return; - sfputc(stkstd,gp->gl_delim); - offset = stktell(stkstd); + sfputc(globstk,gp->gl_delim); + offset = stktell(globstk); /* if null, reserve room for . */ if (*rescan) - sfputr(stkstd,rescan,-1); + sfputr(globstk,rescan,-1); else - sfputc(stkstd,0); - sfputc(stkstd,0); - rescan = stkptr(stkstd,offset); - ap = (globlist_t*)stkfreeze(stkstd,0); + sfputc(globstk,0); + sfputc(globstk,0); + rescan = stkptr(globstk,offset); + ap = stkfreeze(globstk,0); ap->gl_begin = (char*)rescan; ap->gl_next = gp->gl_rescan; gp->gl_rescan = ap; } else { - if (!endslash && (gp->gl_flags & GLOB_MARK) && (type = (*gp->gl_type)(gp, stkptr(stkstd,MATCHPATH(gp)), 0))) + if (!endslash && (gp->gl_flags & GLOB_MARK) && (type = (*gp->gl_type)(gp, stkptr(globstk,MATCHPATH(gp)), 0))) { if ((gp->gl_flags & GLOB_COMPLETE) && type != GLOB_EXE) { - stkseek(stkstd,0); + stkseek(globstk,0); return; } else if (type == GLOB_DIR && (gp->gl_flags & GLOB_MARK)) - sfputc(stkstd,gp->gl_delim); + sfputc(globstk,gp->gl_delim); } - ap = (globlist_t*)stkfreeze(stkstd,1); + ap = stkfreeze(globstk,1); ap->gl_next = gp->gl_match; gp->gl_match = ap; gp->gl_pathc++; @@ -697,7 +699,7 @@ _ast_glob(const char* pattern, int flags, int (*errfn)(const char*, int), glob_t } skip = gp->gl_pathc; if (gp->gl_stak) - oldstak = stkinstall(gp->gl_stak, 0); + oldstak = globstk, globstk = gp->gl_stak; if (flags & GLOB_DOOFFS) extra += gp->gl_offs; if (gp->gl_suffix) @@ -761,7 +763,7 @@ _ast_glob(const char* pattern, int flags, int (*errfn)(const char*, int), glob_t break; } } - top = ap = (globlist_t*)stkalloc(stkstd,(optlen ? 2 : 1) * strlen(pattern) + sizeof(globlist_t) + suflen + gp->gl_extra); + top = ap = stkalloc(globstk,(optlen ? 2 : 1) * strlen(pattern) + sizeof(globlist_t) + suflen + gp->gl_extra); ap->gl_next = 0; ap->gl_flags = 0; ap->gl_begin = ap->gl_path + gp->gl_extra; @@ -799,7 +801,7 @@ _ast_glob(const char* pattern, int flags, int (*errfn)(const char*, int), glob_t gp->gl_list = gp->gl_match; else { - argv = (char**)stkalloc(stkstd,(gp->gl_pathc + extra) * sizeof(char*)); + argv = stkalloc(globstk,(gp->gl_pathc + extra) * sizeof(char*)); if (gp->gl_flags & GLOB_APPEND) { skip += --extra; @@ -832,7 +834,7 @@ _ast_glob(const char* pattern, int flags, int (*errfn)(const char*, int), glob_t if (gp->gl_starstar > 1) gp->gl_flags &= ~GLOB_STARSTAR; if (gp->gl_stak) - stkinstall(oldstak, 0); + globstk = oldstak; return gp->gl_error; } diff --git a/src/lib/libast/misc/stk.c b/src/lib/libast/misc/stk.c index ea103b44034d..bd00d41e3cbd 100644 --- a/src/lib/libast/misc/stk.c +++ b/src/lib/libast/misc/stk.c @@ -2,7 +2,7 @@ * * * This software is part of the ast package * * Copyright (c) 1985-2012 AT&T Intellectual Property * -* Copyright (c) 2020-2023 Contributors to ksh 93u+m * +* Copyright (c) 2020-2024 Contributors to ksh 93u+m * * and is licensed under the * * Eclipse Public License, Version 2.0 * * * @@ -53,7 +53,8 @@ #define STK_FSIZE (1024*sizeof(char*)) #define STK_HDRSIZE (sizeof(Sfio_t)+sizeof(Sfdisc_t)) -typedef char* (*_stk_overflow_)(size_t); +typedef void* (*_stk_overflow_)(size_t); +typedef char* (*_old_stk_overflow_)(size_t); /* for stkinstall (deprecated) */ static int stkexcept(Sfio_t*,int,void*,Sfdisc_t*); static Sfdisc_t stkdisc = { 0, 0, 0, stkexcept }; @@ -86,40 +87,16 @@ static char *stkgrow(Sfio_t*, size_t); #define stk2stream(sp) ((Sfio_t*)(((char*)(sp))-STK_HDRSIZE)) #define stkleft(stream) ((stream)->_endb-(stream)->_data) - -#ifdef STKSTATS - static struct - { - int create; - int delete; - int install; - int alloc; - int copy; - int puts; - int seek; - int set; - int grow; - int addsize; - int delsize; - int movsize; - } _stkstats; -# define increment(x) (_stkstats.x++) -# define count(x,n) (_stkstats.x += (n)) -#else -# define increment(x) -# define count(x,n) -#endif /* STKSTATS */ - -static const char Omsg[] = "malloc failed while growing stack\n"; +static const char Omsg[] = "out of memory while growing stack\n"; /* * default overflow exception */ -static noreturn char *overflow(size_t n) +static noreturn void *overflow(size_t n) { NoP(n); write(2,Omsg, sizeof(Omsg)-1); - exit(2); + exit(128); UNREACHABLE(); } @@ -132,7 +109,7 @@ static void stkinit(size_t size) init = size; sp = stkopen(0); init = 1; - stkinstall(sp,overflow); + stkinstall(sp,(_old_stk_overflow_)overflow); } static int stkexcept(Sfio_t *stream, int type, void* val, Sfdisc_t* dp) @@ -148,7 +125,6 @@ static int stkexcept(Sfio_t *stream, int type, void* val, Sfdisc_t* dp) struct frame *fp; if(--sp->stkref == 0) { - increment(delete); if(stream==stkstd) stkset(stream,NULL,0); else @@ -214,13 +190,11 @@ Sfio_t *stkopen(int flags) char *cp; if(!(stream=newof(NULL,Sfio_t, 1, sizeof(*dp)+sizeof(*sp)))) return NULL; - increment(create); - count(addsize,sizeof(*stream)+sizeof(*dp)+sizeof(*sp)); dp = (Sfdisc_t*)(stream+1); dp->exceptf = stkexcept; sp = (struct stk*)(dp+1); sp->stkref = 1; - sp->stkflags = (flags&STK_SMALL); + sp->stkflags = flags; if(flags&STK_NULL) sp->stkoverflow = 0; else sp->stkoverflow = stkcur?stkcur->stkoverflow:overflow; bsize = init+sizeof(struct frame); @@ -234,7 +208,6 @@ Sfio_t *stkopen(int flags) free(stream); return NULL; } - count(addsize,sizeof(*fp)+bsize); cp = (char*)(fp+1); sp->stkbase = (char*)fp; fp->prev = 0; @@ -252,7 +225,7 @@ Sfio_t *stkopen(int flags) * if is not null, it becomes the new current stack * becomes the new overflow function */ -Sfio_t *stkinstall(Sfio_t *stream, _stk_overflow_ oflow) +Sfio_t *stkinstall(Sfio_t *stream, _old_stk_overflow_ oflow) { Sfio_t *old; struct stk *sp; @@ -260,10 +233,9 @@ Sfio_t *stkinstall(Sfio_t *stream, _stk_overflow_ oflow) { stkinit(1); if(oflow) - stkcur->stkoverflow = oflow; + stkcur->stkoverflow = (_stk_overflow_)oflow; return NULL; } - increment(install); old = stkcur?stk2stream(stkcur):0; if(stream) { @@ -276,10 +248,22 @@ Sfio_t *stkinstall(Sfio_t *stream, _stk_overflow_ oflow) else sp = stkcur; if(oflow) - sp->stkoverflow = oflow; + sp->stkoverflow = (_stk_overflow_)oflow; return old; } +/* + * set or unset the overflow function + */ +void stkoverflow(Sfio_t *stream, _stk_overflow_ oflow) +{ + struct stk *sp; + if(!init) + stkinit(1); + sp = stream2stk(stream); + sp->stkoverflow = oflow ? oflow : (sp->stkflags & STK_NULL ? NULL : overflow); +} + /* * increase the reference count on the given */ @@ -319,21 +303,20 @@ int stkon(Sfio_t * stream, char* loc) return 0; } /* - * reset the bottom of the current stack back to - * if is null, then the stack is reset to the beginning - * if is not in this stack, the program dumps core + * reset the bottom of the current stack back to
+ * if
is null, then the stack is reset to the beginning + * if
is not in this stack, the program dumps core * otherwise, the top of the stack is set to stkbot+ */ -char *stkset(Sfio_t * stream, char* loc, size_t offset) +void *stkset(Sfio_t *stream, void *address, size_t offset) { struct stk *sp = stream2stk(stream); - char *cp; + char *cp, *loc = (char*)address; struct frame *fp; int frames = 0; int n; if(!init) stkinit(offset+1); - increment(set); while(1) { fp = (struct frame*)sp->stkbase; @@ -376,45 +359,43 @@ char *stkset(Sfio_t * stream, char* loc, size_t offset) else stream->_data = stream->_next = (unsigned char*)cp; found: - return (char*)stream->_data; + return stream->_data; } /* * allocate bytes on the current stack */ -char *stkalloc(Sfio_t *stream, size_t n) +void *stkalloc(Sfio_t *stream, size_t n) { unsigned char *old; if(!init) stkinit(n); - increment(alloc); n = roundof(n,STK_ALIGN); if(stkleft(stream) <= (int)n && !stkgrow(stream,n)) return NULL; old = stream->_data; stream->_data = stream->_next = old+n; - return (char*)old; + return old; } /* * begin a new stack word of at least bytes */ -char *_stkseek(Sfio_t *stream, ssize_t n) +void *_stkseek(Sfio_t *stream, ssize_t n) { if(!init) stkinit(n); - increment(seek); if(stkleft(stream) <= n && !stkgrow(stream,n)) return NULL; stream->_next = stream->_data+n; - return (char*)stream->_data; + return stream->_data; } /* * advance the stack to the current top * if extra is non-zero, first add extra bytes and zero the first */ -char *stkfreeze(Sfio_t *stream, size_t extra) +void *stkfreeze(Sfio_t *stream, size_t extra) { unsigned char *old, *top; if(!init) @@ -462,7 +443,6 @@ char *stkcopy(Sfio_t *stream, const char* str) n = roundof(cp-(unsigned char*)str,STK_ALIGN); if(!init) stkinit(n); - increment(copy); if(stkleft(stream) <= n && !stkgrow(stream,n)) cp = 0; else @@ -515,8 +495,6 @@ static char *stkgrow(Sfio_t *stream, size_t size) cp = newof(dp, char, n, nn*sizeof(char*)); if(!cp && (!sp->stkoverflow || !(cp = (*sp->stkoverflow)(n)))) return NULL; - increment(grow); - count(addsize,n - (dp?m:0)); if(dp==cp) { nn--; @@ -542,10 +520,7 @@ static char *stkgrow(Sfio_t *stream, size_t size) fp->aliases[nn-1] = oldbase + roundof(sizeof(struct frame),STK_ALIGN); } if(m && !dp) - { memcpy(cp,(char*)stream->_data,m); - count(movsize,m); - } sfsetbuf(stream,cp,sp->stkend-cp); return (char*)(stream->_next = stream->_data+m); } diff --git a/src/lib/libast/regex/regnexec.c b/src/lib/libast/regex/regnexec.c index 46ec1a55f83e..78dc9e16625f 100644 --- a/src/lib/libast/regex/regnexec.c +++ b/src/lib/libast/regex/regnexec.c @@ -150,7 +150,7 @@ vecopen(int inc, int siz) inc = 16; if (!(sp = stkopen(STK_SMALL|STK_NULL))) return NULL; - if (!(v = (Vector_t*)stkseek(sp, sizeof(Vector_t) + inc * siz))) + if (!(v = stkseek(sp, sizeof(Vector_t) + inc * siz))) { stkclose(sp); return NULL; @@ -171,7 +171,7 @@ vecseek(Vector_t** p, int index) if (index >= v->max) { while ((v->max += v->inc) <= index); - if (!(v = (Vector_t*)stkseek(v->stk, sizeof(Vector_t) + v->max * v->siz))) + if (!(v = stkseek(v->stk, sizeof(Vector_t) + v->max * v->siz))) return NULL; *p = v; v->vec = (char*)v + sizeof(Vector_t); @@ -207,7 +207,7 @@ stkpush(Stk_t* sp, size_t size) stknew(sp, &p); size = sizeof(Stk_frame_t) + sizeof(size_t) + size - 1; - if (!(f = (Stk_frame_t*)stkalloc(sp, sizeof(Stk_frame_t) + sizeof(Stk_frame_t*) + size - 1))) + if (!(f = stkalloc(sp, sizeof(Stk_frame_t) + sizeof(Stk_frame_t*) + size - 1))) return NULL; f->pos = p; stkframe(sp) = f; @@ -252,7 +252,7 @@ _matchpush(Env_t* env, Rex_t* rex) if (rex->re.group.number <= 0 || (num = rex->re.group.last - rex->re.group.number + 1) <= 0) num = 0; - if (!(f = (Match_frame_t*)stkpush(env->mst, sizeof(Match_frame_t) + (num - 1) * sizeof(regmatch_t)))) + if (!(f = stkpush(env->mst, sizeof(Match_frame_t) + (num - 1) * sizeof(regmatch_t)))) { env->error = REG_ESPACE; return 1; @@ -960,7 +960,7 @@ DEBUG_TEST(0x0008,(sfprintf(sfstdout, "AHA#%04d 0x%04x parse %s `%-.*s'\n", __LI e = env->end; if (!(rex->flags & REG_MINIMAL)) { - if (!(b = (unsigned char*)stkpush(env->mst, n))) + if (!(b = stkpush(env->mst, n))) { env->error = REG_ESPACE; return BAD; @@ -1110,7 +1110,7 @@ DEBUG_TEST(0x0008,(sfprintf(sfstdout, "AHA#%04d 0x%04x parse %s `%-.*s'\n", __LI } else { - if (!(b = (unsigned char*)stkpush(env->mst, n))) + if (!(b = stkpush(env->mst, n))) { env->error = REG_ESPACE; return BAD; @@ -1422,7 +1422,7 @@ DEBUG_TEST(0x0200,(sfprintf(sfstdout,"AHA#%04d 0x%04x parse %s=>%s `%-.*s'\n", _ n = ((i + 7) >> 3) + 1; catcher.type = REX_NEG_CATCH; catcher.re.neg_catch.beg = s; - if (!(p = (unsigned char*)stkpush(env->mst, n))) + if (!(p = stkpush(env->mst, n))) return BAD; memset(catcher.re.neg_catch.index = p, 0, n); catcher.next = rex->next; @@ -1521,7 +1521,7 @@ DEBUG_TEST(0x0200,(sfprintf(sfstdout,"AHA#%04d 0x%04x parse %s=>%s `%-.*s'\n", _ } else { - if (!(b = (unsigned char*)stkpush(env->mst, n))) + if (!(b = stkpush(env->mst, n))) { env->error = REG_ESPACE; return BAD; @@ -1888,7 +1888,7 @@ regnexec_20120528(const regex_t* p, const char* s, size_t len, size_t nmatch, re if (env->stack = env->hard || !(env->flags & REG_NOSUB) && nmatch) { n = env->nsub; - if (!(env->match = (regmatch_t*)stkpush(env->mst, 2 * (n + 1) * sizeof(regmatch_t))) || + if (!(env->match = stkpush(env->mst, 2 * (n + 1) * sizeof(regmatch_t))) || !env->pos && !(env->pos = vecopen(16, sizeof(Pos_t))) || !env->bestpos && !(env->bestpos = vecopen(16, sizeof(Pos_t)))) { diff --git a/src/lib/libcmd/chgrp.c b/src/lib/libcmd/chgrp.c index e751194c42da..3c7708da5909 100644 --- a/src/lib/libcmd/chgrp.c +++ b/src/lib/libcmd/chgrp.c @@ -367,7 +367,7 @@ b_chgrp(int argc, char** argv, Shbltin_t* context) getids(s, &t, &key, options); if (!(m = (Map_t*)dtmatch(map, &key))) { - if (!(m = (Map_t*)stkalloc(stkstd, sizeof(Map_t)))) + if (!(m = stkalloc(stkstd, sizeof(Map_t)))) { error(ERROR_SYSTEM|ERROR_PANIC, "out of memory [id dictionary]"); UNREACHABLE(); diff --git a/src/lib/libcmd/cp.c b/src/lib/libcmd/cp.c index f3cf2974cb8f..9730d3ff61ad 100644 --- a/src/lib/libcmd/cp.c +++ b/src/lib/libcmd/cp.c @@ -872,7 +872,7 @@ b_cp(int argc, char** argv, Shbltin_t* context) argc--; argv++; } - if (!(v = (char**)stkalloc(stkstd, (argc + 2) * sizeof(char*)))) + if (!(v = stkalloc(stkstd, (argc + 2) * sizeof(char*)))) { error(ERROR_SYSTEM|ERROR_PANIC, "out of memory"); UNREACHABLE(); diff --git a/src/lib/libcmd/cut.c b/src/lib/libcmd/cut.c index ceb6e2556427..4353cdd9e5ac 100644 --- a/src/lib/libcmd/cut.c +++ b/src/lib/libcmd/cut.c @@ -134,7 +134,7 @@ cutinit(int mode, char* str, Delim_t* wdelim, Delim_t* ldelim, size_t reclen) char* cp = str; Cut_t* cut; - if (!(cut = (Cut_t*)stkalloc(stkstd, sizeof(Cut_t) + strlen(cp) * sizeof(int)))) + if (!(cut = stkalloc(stkstd, sizeof(Cut_t) + strlen(cp) * sizeof(int)))) { error(ERROR_SYSTEM|ERROR_PANIC, "out of memory"); UNREACHABLE(); diff --git a/src/lib/libcmd/paste.c b/src/lib/libcmd/paste.c index a5102cd374d6..1259c9dc7339 100644 --- a/src/lib/libcmd/paste.c +++ b/src/lib/libcmd/paste.c @@ -257,7 +257,7 @@ b_paste(int argc, char** argv, Shbltin_t* context) n = 1; if(!sflag) { - if (!(streams = (Sfio_t**)stkalloc(stkstd,n*sizeof(Sfio_t*)))) + if (!(streams = stkalloc(stkstd,n*sizeof(Sfio_t*)))) { error(ERROR_SYSTEM|ERROR_PANIC, "out of memory"); UNREACHABLE(); diff --git a/src/lib/libcmd/tail.c b/src/lib/libcmd/tail.c index 2021dd5632a0..b49913eeb824 100644 --- a/src/lib/libcmd/tail.c +++ b/src/lib/libcmd/tail.c @@ -2,7 +2,7 @@ * * * This software is part of the ast package * * Copyright (c) 1992-2013 AT&T Intellectual Property * -* Copyright (c) 2020-2023 Contributors to ksh 93u+m * +* Copyright (c) 2020-2024 Contributors to ksh 93u+m * * and is licensed under the * * Eclipse Public License, Version 2.0 * * * @@ -629,7 +629,7 @@ b_tail(int argc, char** argv, Shbltin_t* context) } if (flags & FOLLOW) { - if (!(fp = (Tail_t*)stkalloc(stkstd, argc * sizeof(Tail_t)))) + if (!(fp = stkalloc(stkstd, argc * sizeof(Tail_t)))) { error(ERROR_SYSTEM|ERROR_PANIC, "out of memory"); UNREACHABLE(); diff --git a/src/lib/libcmd/tee.c b/src/lib/libcmd/tee.c index 6399e71f73e1..fa65b4c29abb 100644 --- a/src/lib/libcmd/tee.c +++ b/src/lib/libcmd/tee.c @@ -2,7 +2,7 @@ * * * This software is part of the ast package * * Copyright (c) 1992-2012 AT&T Intellectual Property * -* Copyright (c) 2020-2023 Contributors to ksh 93u+m * +* Copyright (c) 2020-2024 Contributors to ksh 93u+m * * and is licensed under the * * Eclipse Public License, Version 2.0 * * * @@ -167,7 +167,7 @@ b_tee(int argc, char** argv, Shbltin_t* context) #endif if (argc > 0) { - if (tp = (Tee_t*)stkalloc(stkstd, sizeof(Tee_t) + argc * sizeof(int))) + if (tp = stkalloc(stkstd, sizeof(Tee_t) + argc * sizeof(int))) { memset(&tp->disc, 0, sizeof(tp->disc)); tp->disc.writef = tee_write; diff --git a/src/lib/libcmd/uname.c b/src/lib/libcmd/uname.c index 880100d5eeb4..23958d19cb54 100644 --- a/src/lib/libcmd/uname.c +++ b/src/lib/libcmd/uname.c @@ -2,7 +2,7 @@ * * * This software is part of the ast package * * Copyright (c) 1992-2012 AT&T Intellectual Property * -* Copyright (c) 2020-2023 Contributors to ksh 93u+m * +* Copyright (c) 2020-2024 Contributors to ksh 93u+m * * and is licensed under the * * Eclipse Public License, Version 2.0 * * * @@ -297,7 +297,7 @@ b_uname(int argc, char** argv, Shbltin_t* context) continue; case ':': { - char **new_argv = (char **)stkalloc(stkstd, (argc + 3) * sizeof(char*)); + char **new_argv = stkalloc(stkstd, (argc + 3) * sizeof(char*)); new_argv[0] = "command"; new_argv[1] = "-px"; for (n = 0; n <= argc; n++) diff --git a/src/lib/libcmd/wclib.c b/src/lib/libcmd/wclib.c index 0a4850d23b96..517a0a6f1111 100644 --- a/src/lib/libcmd/wclib.c +++ b/src/lib/libcmd/wclib.c @@ -56,7 +56,7 @@ Wc_t* wc_init(int mode) int w; Wc_t* wp; - if (!(wp = (Wc_t*)stkalloc(stkstd,sizeof(Wc_t)))) + if (!(wp = stkalloc(stkstd,sizeof(Wc_t)))) return NULL; if (!mbwide()) wp->mb = 0;