Skip to content

Commit

Permalink
simplify ${.sh.version} / $((.sh.version)) init
Browse files Browse the repository at this point in the history
We don't need to determine the length of the e_version string at
runtime. Since e_version is declared as a static array, its size is
known at compile time, and the string length is a function of that,
so this is now done at compile time to save cycles on init.

This commit also removes a few tests for library functions that we
can now safely assume to be present: nice(3) and fork(2), as well
as pathnative() and pathposix() which are provided by libast
(unlike the old AT&T practice, we always compile ksh against our
shipped and known version of libast).
  • Loading branch information
McDutchie committed Jan 7, 2025
1 parent 1b6157e commit a4e1bb5
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 31 deletions.
3 changes: 1 addition & 2 deletions src/cmd/ksh93/features/externs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
hdr nc
mem exception.name,_exception.name math.h
lib setreuid,setregid,nice,fork
lib pathnative,pathposix
lib setreuid,setregid
lib memcntl sys/mman.h

# for main.c fixargs():
Expand Down
7 changes: 2 additions & 5 deletions src/cmd/ksh93/include/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,8 @@
#endif

#include <ast.h>
#if !defined(AST_VERSION) || AST_VERSION < 20220801
#error libast version 20220801 or later is required
#endif
#if !_lib_fork
#error In 2021, ksh joined the 21st century and started requiring fork(2).
#if !defined(AST_VERSION) || AST_VERSION < 20240811
#error libast version 20240811 or later is required
#endif

#include <sfio.h>
Expand Down
20 changes: 7 additions & 13 deletions src/cmd/ksh93/sh/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -1017,17 +1017,14 @@ static char* get_version(Namval_t* np, Namfun_t *fp)

static Sfdouble_t nget_version(Namval_t* np, Namfun_t *fp)
{
const char *cp = e_version + strlen(e_version)-10;
const char *cp = e_version + sizeof e_version - 15; /* version date */
int c;
Sflong_t t = 0;
NOT_USED(np);
NOT_USED(fp);
while (c = *cp++)
if (c >= '0' && c <= '9')
{
t *= 10;
t += c - '0';
}
if (isdigit(c))
t = 10 * t + c - '0';
return (Sfdouble_t)t;
}

Expand Down Expand Up @@ -1260,13 +1257,11 @@ int sh_type(const char *path)
*/
Shell_t *sh_init(int argc,char *argv[], Shinit_f userinit)
{
size_t n;
int type = 0;
static char *login_files[2];
sh_onstate(SH_INIT);
n = strlen(e_version);
if(e_version[n-1]=='$' && e_version[n-2]==' ')
e_version[n-2]=0;
/* truncate final " $\0\n" from e_version for ${.sh.version} output (it's there for what(1) or ident(1)) */
e_version[sizeof e_version - 5] = '\0';
memcpy(sh_lexstates,sh_lexrstates,ST_NONE*sizeof(char*));
sh.current_pid = sh.pid = getpid();
sh.current_ppid = sh.ppid = getppid();
Expand Down Expand Up @@ -1327,6 +1322,7 @@ Shell_t *sh_init(int argc,char *argv[], Shinit_f userinit)
*/
char *cp=nv_getval(L_ARGNOD);
char buff[PATH_MAX+1];
size_t n;
sh.shpath = 0;
if((n = pathprog(NULL, buff, sizeof(buff))) > 0 && n <= sizeof(buff))
sh.shpath = sh_strdup(buff);
Expand Down Expand Up @@ -1390,17 +1386,15 @@ Shell_t *sh_init(int argc,char *argv[], Shinit_f userinit)
name = sh.st.dolv[0];
if(name[1]==':' && (name[2]=='/' || name[2]=='\\'))
{
#if _lib_pathposix
char* p;

size_t n;
if((n = pathposix(name, NULL, 0)) > 0)
{
p = (char*)sh_malloc(++n);
pathposix(name, p, n);
name = p;
}
else
#endif
{
name[1] = name[0];
name[0] = name[2] = '/';
Expand Down
6 changes: 0 additions & 6 deletions src/cmd/ksh93/sh/name.c
Original file line number Diff line number Diff line change
Expand Up @@ -1822,9 +1822,7 @@ void nv_putval(Namval_t *np, const char *string, int flags)
{
void *tofree=0;
int offset = 0;
#if _lib_pathnative
char buff[PATH_MAX];
#endif /* _lib_pathnative */
if(flags&NV_INTEGER)
{
if((flags&NV_DOUBLE)==NV_DOUBLE)
Expand Down Expand Up @@ -1852,7 +1850,6 @@ void nv_putval(Namval_t *np, const char *string, int flags)
}
if(nv_isattr(np, NV_HOST|NV_INTEGER)==NV_HOST && sp)
{
#if _lib_pathnative
/*
* return the host file name given the UNIX name
*/
Expand All @@ -1864,9 +1861,6 @@ void nv_putval(Namval_t *np, const char *string, int flags)
*buff += 'a'-'A';
}
sp = buff;
#else
;
#endif /* _lib_pathnative */
}
else if((nv_isattr(np, NV_RJUST|NV_ZFILL|NV_LJUST)) && sp)
{
Expand Down
5 changes: 0 additions & 5 deletions src/cmd/ksh93/sh/xec.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@
#define _use_ntfork_tcpgrp 1
#endif

#if _lib_nice
extern int nice(int);
#endif /* _lib_nice */
#if SHOPT_SPAWN
static pid_t sh_ntfork(const Shnode_t*,char*[],int*,int);
#endif /* SHOPT_SPAWN */
Expand Down Expand Up @@ -1535,10 +1532,8 @@ int sh_exec(const Shnode_t *t, int flags)
}
sh_offstate(SH_INTERACTIVE);
/* pipe in or out */
#if _lib_nice
if((type&FAMP) && sh_isoption(SH_BGNICE))
nice(4);
#endif /* _lib_nice */
#if !SHOPT_DEVFD
if(sh.fifo && (type&(FPIN|FPOU)))
{
Expand Down
7 changes: 7 additions & 0 deletions src/cmd/ksh93/tests/variables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1734,5 +1734,12 @@ EOF
wait "$parallel_1" || err_exit 'setting TMOUT in a virtual subshell removes its special meaning'
wait "$parallel_2" || err_exit "TMOUT applies to 'read' from a non-terminal"
# ======
# TODO: fix to support > 4 year digits well before the year 10,000 :)
got=$((.sh.version))
exp='^[[:digit:]]{8}$'
[[ $got =~ $exp ]] || err_exit '$((.sh.version)) does not yield YYYYMMDD digits' \
"(expected match of ERE $exp, got '$got')"
# ======
exit $((Errors<125?Errors:125))

0 comments on commit a4e1bb5

Please sign in to comment.