Skip to content

Commit 6465cfc

Browse files
committed
Further modernise the stk(3) stack routines (re: f619b48)
*** 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 a34e831) 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.
1 parent 6628b52 commit 6465cfc

File tree

37 files changed

+316
-257
lines changed

37 files changed

+316
-257
lines changed

src/cmd/ksh93/bltins/cd_pwd.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* *
33
* This software is part of the ast package *
44
* Copyright (c) 1982-2012 AT&T Intellectual Property *
5-
* Copyright (c) 2020-2023 Contributors to ksh 93u+m *
5+
* Copyright (c) 2020-2024 Contributors to ksh 93u+m *
66
* and is licensed under the *
77
* Eclipse Public License, Version 2.0 *
88
* *
@@ -171,7 +171,7 @@ int b_cd(int argc, char *argv[],Shbltin_t *context)
171171
#endif /* _WINIX */
172172
if(*stkptr(sh.stk,PATH_OFFSET)!='/')
173173
{
174-
char *last=(char*)stkfreeze(sh.stk,1);
174+
char *last = stkfreeze(sh.stk,1);
175175
stkseek(sh.stk,PATH_OFFSET);
176176
sfputr(sh.stk,oldpwd,-1);
177177
/* don't add '/' if oldpwd is / itself */

src/cmd/ksh93/bltins/getopts.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* *
33
* This software is part of the ast package *
44
* Copyright (c) 1982-2012 AT&T Intellectual Property *
5-
* Copyright (c) 2020-2023 Contributors to ksh 93u+m *
5+
* Copyright (c) 2020-2024 Contributors to ksh 93u+m *
66
* and is licensed under the *
77
* Eclipse Public License, Version 2.0 *
88
* *
@@ -41,7 +41,7 @@ static int infof(Opt_t* op, Sfio_t* sp, const char* s, Optdisc_t* dp)
4141
#endif /* SHOPT_NAMESPACE */
4242
{
4343
int savtop = stktell(stkp);
44-
char *savptr = stkfreeze(stkp,0);
44+
void *savptr = stkfreeze(stkp,0);
4545
sfputc(stkp,'$');
4646
sfputc(stkp,'(');
4747
sfputr(stkp,s,')');

src/cmd/ksh93/bltins/test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* *
33
* This software is part of the ast package *
44
* Copyright (c) 1982-2012 AT&T Intellectual Property *
5-
* Copyright (c) 2020-2023 Contributors to ksh 93u+m *
5+
* Copyright (c) 2020-2024 Contributors to ksh 93u+m *
66
* and is licensed under the *
77
* Eclipse Public License, Version 2.0 *
88
* *
@@ -665,7 +665,7 @@ int sh_access(const char *name, int mode)
665665
maxgroups = (int)astconf_long(CONF_NGROUPS_MAX);
666666
}
667667
}
668-
groups = (gid_t*)stkalloc(sh.stk,(maxgroups+1)*sizeof(gid_t));
668+
groups = stkalloc(sh.stk,(maxgroups+1)*sizeof(gid_t));
669669
n = getgroups(maxgroups,groups);
670670
while(--n >= 0)
671671
{

src/cmd/ksh93/bltins/typeset.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ int b_typeset(int argc,char *argv[],Shbltin_t *context)
233233
}
234234
else if(argv[0][0] != 't') /* not <t>ypeset */
235235
{
236-
char **new_argv = (char **)stkalloc(sh.stk, (argc + 2) * sizeof(char*));
236+
char **new_argv = stkalloc(sh.stk, (argc + 2) * sizeof(char*));
237237
error_info.id = new_argv[0] = SYSTYPESET->nvname;
238238
if(argv[0][0] == 'a') /* <a>utoload == typeset -fu */
239239
new_argv[1] = "-fu";
@@ -1607,7 +1607,7 @@ static void print_scan(Sfio_t *file, int flag, Dt_t *root, int option,struct tda
16071607
if(flag==NV_LTOU || flag==NV_UTOL)
16081608
tp->scanmask |= NV_UTOL|NV_LTOU;
16091609
namec = nv_scan(root, nullscan, tp, tp->scanmask, flag&~NV_IARRAY);
1610-
argv = tp->argnam = (char**)stkalloc(sh.stk,(namec+1)*sizeof(char*));
1610+
argv = tp->argnam = stkalloc(sh.stk,(namec+1)*sizeof(char*));
16111611
namec = nv_scan(root, pushname, tp, tp->scanmask, flag&~NV_IARRAY);
16121612
if(mbcoll())
16131613
strsort(argv,namec,strcoll);

src/cmd/ksh93/edit/completion.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,8 @@ int ed_expand(Edit_t *ep, char outbuff[],int *cur,int *eol,int mode, int count)
277277
ep->e_nlist = 0;
278278
}
279279
}
280-
comptr = (struct comnod*)stkalloc(sh.stk,sizeof(struct comnod));
281-
ap = (struct argnod*)stkseek(sh.stk,ARGVAL);
280+
comptr = stkalloc(sh.stk,sizeof(struct comnod));
281+
ap = stkseek(sh.stk,ARGVAL);
282282
#if SHOPT_MULTIBYTE
283283
{
284284
int c = *cur;
@@ -368,7 +368,7 @@ int ed_expand(Edit_t *ep, char outbuff[],int *cur,int *eol,int mode, int count)
368368
}
369369
if(addstar)
370370
sfputc(sh.stk,addstar);
371-
ap = (struct argnod*)stkfreeze(sh.stk,1);
371+
ap = stkfreeze(sh.stk,1);
372372
}
373373
if(mode!='*')
374374
sh_onoption(SH_MARKDIRS);

src/cmd/ksh93/features/externs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ tst note{ determining default number of extra bytes per argument for arguments l
8383
int bytec;
8484

8585
error_info.id="_arg_extrabytes test (child)";
86-
argv = (char **)stkalloc(stkstd, (argmax / 2 + 1) * sizeof(char*));
86+
argv = stkalloc(stkstd, (argmax / 2 + 1) * sizeof(char*));
8787
argc = bytec = 0;
8888
while(bytec < argmax)
8989
{

src/cmd/ksh93/include/defs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* *
33
* This software is part of the ast package *
44
* Copyright (c) 1982-2012 AT&T Intellectual Property *
5-
* Copyright (c) 2020-2023 Contributors to ksh 93u+m *
5+
* Copyright (c) 2020-2024 Contributors to ksh 93u+m *
66
* and is licensed under the *
77
* Eclipse Public License, Version 2.0 *
88
* *

src/cmd/ksh93/sh/args.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ char **sh_argbuild(int *nargs, const struct comnod *comptr,int flag)
681681
* TODO: find/fix root cause, eliminate argi
682682
*/
683683
argn = *nargs + 1; /* allow room to prepend args */
684-
comargn=(char**)stkalloc(sh.stk,(unsigned)(argn+1)*sizeof(char*));
684+
comargn = stkalloc(sh.stk,(unsigned)(argn+1)*sizeof(char*));
685685
comargm = comargn += argn;
686686
*comargn = NULL;
687687
if(!argp)
@@ -724,7 +724,7 @@ struct argnod *sh_argprocsub(struct argnod *argp)
724724
int savestates = sh_getstate();
725725
char savejobcontrol = job.jobcontrol;
726726
unsigned int savesubshell = sh.subshell;
727-
ap = (struct argnod*)stkseek(sh.stk,ARGVAL);
727+
ap = stkseek(sh.stk,ARGVAL);
728728
ap->argflag |= ARG_MAKE;
729729
ap->argflag &= ~ARG_RAW;
730730
fd = argp->argflag&ARG_RAW;
@@ -752,7 +752,7 @@ struct argnod *sh_argprocsub(struct argnod *argp)
752752
sfputr(sh.stk,sh.fifo,0);
753753
#endif /* SHOPT_DEVFD */
754754
sfputr(sh.stk,fmtint(pv[fd],1),0);
755-
ap = (struct argnod*)stkfreeze(sh.stk,0);
755+
ap = stkfreeze(sh.stk,0);
756756
sh.inpipe = sh.outpipe = 0;
757757
/* turn off job control */
758758
sh_offstate(SH_INTERACTIVE);

src/cmd/ksh93/sh/expand.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ int path_expand(const char *pattern, struct argnod **arghead, int musttrim)
115115
*/
116116
if((ap = (struct argnod*)gp->gl_list) && !ap->argnxt.ap && strcmp(ap->argval,trimmedpat)==0)
117117
{
118-
gp->gl_list = (globlist_t*)stkalloc(sh.stk,ARGVAL+strlen(pattern)+1);
118+
gp->gl_list = stkalloc(sh.stk,ARGVAL+strlen(pattern)+1);
119119
memcpy(gp->gl_list,ap,ARGVAL); /* copy fields *before* argval/gl_path */
120120
strcpy(gp->gl_list->gl_path,pattern);
121121
}
@@ -149,9 +149,9 @@ static int scantree(Dt_t *tree, const char *pattern, struct argnod **arghead)
149149
continue;
150150
if(strmatch(cp=nv_name(np),pattern))
151151
{
152-
(void)stkseek(sh.stk,ARGVAL);
152+
stkseek(sh.stk,ARGVAL);
153153
sfputr(sh.stk,cp,-1);
154-
ap = (struct argnod*)stkfreeze(sh.stk,1);
154+
ap = stkfreeze(sh.stk,1);
155155
ap->argbegin = NULL;
156156
ap->argchn.ap = *arghead;
157157
ap->argflag = ARG_RAW|ARG_MAKE;
@@ -431,13 +431,13 @@ int path_generate(struct argnod *todo, struct argnod **arghead, int musttrim)
431431
brace = *cp;
432432
*cp = 0;
433433
sh_sigcheck();
434-
ap = (struct argnod*)stkseek(sh.stk,ARGVAL);
434+
ap = stkseek(sh.stk,ARGVAL);
435435
ap->argflag = ARG_RAW;
436436
ap->argchn.ap = todo;
437437
sfputr(sh.stk,apin->argval,-1);
438438
sfputr(sh.stk,pat,-1);
439439
sfputr(sh.stk,rescan,-1);
440-
todo = ap = (struct argnod*)stkfreeze(sh.stk,1);
440+
todo = ap = stkfreeze(sh.stk,1);
441441
if(brace == '}')
442442
break;
443443
if(!range)

src/cmd/ksh93/sh/fault.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ int sh_trap(const char *trap, int mode)
497497
char was_no_trapdontexec = !sh.st.trapdontexec;
498498
char save_chldexitsig = sh.chldexitsig;
499499
int staktop = stktell(sh.stk);
500-
char *savptr = stkfreeze(sh.stk,0);
500+
void *savptr = stkfreeze(sh.stk,0);
501501
struct checkpt buff;
502502
Fcin_t savefc;
503503
fcsave(&savefc);

0 commit comments

Comments
 (0)