Skip to content

Commit

Permalink
Mamfiles: use normal command names
Browse files Browse the repository at this point in the history
All that ${STDCP} instead of cp, etc. is annoying. These are
hardcoded paths determined by mamprobe, so they point to known-good
standard utilities on the default system PATH.

But it makes the Mamfiles harder to read. They also weren't even
being used for all of them, e.g., there is no ${STDMKDIR}.

This commit uses another strategy: bin/package ensures setting a
sane system PATH with:
- the /opt/ast/bin virtual directory first (for ksh)
- $(getconf PATH) directories next
- all other user PATH directories follow these
- duplicate directories removed
- nonexistent directories removed

This applies to 'bin/package use' environments as well.

This is then preceded by two bin directories in $INSTALLROOT, but
this build system controls those, so that's OK.

The Mamfiles can now use standard command names without worrying
about invoking potentially dodgy versions on the user's PATH.

The code to set the ${STD...} variables is kept in mamprobe for
backward compatibility with old Mamfiles.
  • Loading branch information
McDutchie committed Feb 12, 2024
1 parent fea5419 commit 357fc44
Show file tree
Hide file tree
Showing 9 changed files with 296 additions and 259 deletions.
42 changes: 39 additions & 3 deletions bin/package
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ if (eval "$min_posix") 2>/dev/null
then : good shell
else "$SHELL" -c "$min_posix" 2>/dev/null && exec "$SHELL" -- "$0" ${1+"$@"}
sh -c "$min_posix" 2>/dev/null && exec sh -- "$0" ${1+"$@"}
DEFPATH=`getconf PATH` 2>/dev/null || DEFPATH=/usr/xpg4/bin:/bin:/usr/bin:/sbin:/usr/sbin
DEFPATH=`getconf PATH 2>/dev/null` || DEFPATH=/usr/xpg4/bin:/bin:/usr/bin:/sbin:/usr/sbin
PATH=$DEFPATH:$PATH
export PATH
sh -c "$min_posix" 2>/dev/null && exec sh -- "$0" ${1+"$@"}
Expand Down Expand Up @@ -82,6 +82,42 @@ case $PWD in
exit 1 ;;
esac || exit

# Outputs sanitized system $PATH, eliminating duplicate and nonexistent dirs, '..', etc.
sanitize_PATH() (
set -fu +e
IFS=':'
unset -v CDPATH
sPATH=''
for dir in $1; do
# Sanitize this path, resolving symlinks,
# with special-casing of ksh's virtual built-ins directory
case $dir in
/opt/ast/bin)
test ! -d "$dir" && sdir=$dir ;;
*/* | [!+-]* | [+-]*[!0123456789]*)
sdir=$(cd -- $dir 2>/dev/null && pwd -P && echo X) ;;
*) sdir=$(cd ./$dir 2>/dev/null && pwd -P && echo X) ;;
esac || continue
sdir=${sdir%?X}
# Skip duplicates
case :$sPATH: in
*:"$sdir":*)
continue ;;
esac
# Found one, add it
sPATH=${sPATH:+$sPATH:}$sdir
done
printf '%s\n' "${sPATH#:}"
)

# Ensure a sane $PATH beginning with standard utilities.
# Find preferred 'getconf' on NixOS and Solaris/illumos.
DEFPATH=$(
PATH=/run/current-system/sw/bin:/usr/xpg7/bin:/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin:$PATH
getconf PATH 2>/dev/null
) || DEFPATH=/bin:/usr/bin:/sbin:/usr/sbin
PATH=$(sanitize_PATH "/opt/ast/bin:$DEFPATH:$PATH")

# shell checks
checksh()
{
Expand Down Expand Up @@ -115,7 +151,7 @@ command=${0##*/}
case $(getopts '[-][123:xyz]' opt --xyz 2>/dev/null; echo 0$opt) in
0123) USAGE=$'
[-?
@(#)$Id: '$command$' (ksh 93u+m) 2024-02-11 $
@(#)$Id: '$command$' (ksh 93u+m) 2024-02-12 $
]
[-author?Glenn Fowler <[email protected]>]
[-author?Contributors to https://github.com/ksh93/ksh]
Expand Down Expand Up @@ -543,7 +579,7 @@ SEE ALSO
pkgadd(1), pkgmk(1), rpm(1), sh(1), tar(1), optget(3)

IMPLEMENTATION
version package (ksh 93u+m) 2024-02-11
version package (ksh 93u+m) 2024-02-12
author Glenn Fowler <[email protected]>
author Contributors to https://github.com/ksh93/ksh
copyright (c) 1994-2012 AT&T Intellectual Property
Expand Down
54 changes: 27 additions & 27 deletions src/cmd/INIT/Mamfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,73 +20,73 @@ note *
make install virtual
make iffe
prev iffe.sh
exec - ${STDCP} ${<} ${@} && ${STDCHMOD} u+w,+x ${@}
exec - cp ${<} ${@} && chmod u+w,+x ${@}
done
make mktest
prev mktest.sh
exec - ${STDCP} ${<} ${@} && ${STDCHMOD} u+w,+x ${@}
exec - cp ${<} ${@} && chmod u+w,+x ${@}
done
make regress
prev regress.sh
exec - ${STDCP} ${<} ${@} && ${STDCHMOD} u+w,+x ${@}
exec - cp ${<} ${@} && chmod u+w,+x ${@}
done
make crossexec
prev crossexec.sh
exec - ${STDCP} ${<} ${@} && ${STDCHMOD} u+w,+x ${@}
exec - cp ${<} ${@} && chmod u+w,+x ${@}
done
make mkreq
prev mkreq.sh
exec - ${STDCP} ${<} ${@} && ${STDCHMOD} u+w,+x ${@}
exec - cp ${<} ${@} && chmod u+w,+x ${@}
done
make mkreq-maplib
prev mkreq-maplib.sh
exec - ${STDCP} ${<} ${@} && ${STDCHMOD} u+w,+x ${@}
exec - cp ${<} ${@} && chmod u+w,+x ${@}
done
make mprobe
prev mprobe.sh
exec - ${STDCP} ${<} ${@} && ${STDCHMOD} u+w,+x ${@}
exec - cp ${<} ${@} && chmod u+w,+x ${@}
done
make probe
make probe.sh
prev C+probe
prev make.probe
exec - ${STDCAT} ${^} > ${@}
exec - cat ${^} > ${@}
done
exec - ${STDCP} ${<} ${@} && ${STDCHMOD} u+w,+x ${@}
exec - cp ${<} ${@} && chmod u+w,+x ${@}
done
make ${INSTALLROOT}/bin
exec - mkdir -p ${@}
done
make ${INSTALLROOT}/bin/iffe
prev iffe
exec - ${STDCP} -f ${<} ${@}
exec - cp -f ${<} ${@}
done
make ${INSTALLROOT}/bin/mkreq
prev mkreq
exec - ${STDCP} -f ${<} ${@}
exec - cp -f ${<} ${@}
done
make ${INSTALLROOT}/bin/mktest
prev mktest
exec - ${STDCP} -f ${<} ${@}
exec - cp -f ${<} ${@}
done
make ${INSTALLROOT}/bin/regress
prev regress
exec - ${STDCP} -f ${<} ${@}
exec - cp -f ${<} ${@}
done
make ${PACKAGEROOT}/bin
exec - mkdir -p ${PACKAGEROOT}/bin
done
make ${INSTALLROOT}/bin/crossexec
prev crossexec
exec - ${STDCP} -f ${<} ${@}
exec - cp -f ${<} ${@}
done
make ${INSTALLROOT}/bin/proto
note *
note * proto(1) has been removed, but install a backward compatibility stub
note * that allows old Mamfiles containing proto commands to keep working.
note *
prev proto.sh
exec - ${STDCP} ${<} ${@} && ${STDCHMOD} u+w,+x ${@}
exec - cp ${<} ${@} && chmod u+w,+x ${@}
done
note *
note * ksh93 function search on PATH
Expand Down Expand Up @@ -233,7 +233,7 @@ make install virtual
done
make ${INSTALLROOT}/lib/probe/C/probe
prev C+probe
exec - ${STDCP} -f ${<} ${@}
exec - cp -f ${<} ${@}
done
make ${INSTALLROOT}/lib/probe/C/make
exec - mkdir -p ${@}
Expand All @@ -246,15 +246,15 @@ make install virtual
done
make ${INSTALLROOT}/lib/probe/C/mam/probe
prev mprobe
exec - ${STDCP} -f ${<} ${@}
exec - cp -f ${<} ${@}
done
make ${INSTALLROOT}/lib/probe/C/mam/mprobe
prev mprobe
exec - ${STDCP} -f ${<} ${@}
exec - cp -f ${<} ${@}
done
make ${INSTALLROOT}/lib/probe/C/make/probe
prev probe
exec - ${STDCP} -f ${<} ${@}
exec - cp -f ${<} ${@}
done
make ${INSTALLROOT}/include/ast
exec - mkdir -p ${@}
Expand All @@ -277,7 +277,7 @@ make install virtual
prev mkreq-maplib
exec - mkreq-maplib ${CC} : dl : dl.c : dl
done
exec - ${STDCP} -f ${<} ${@}
exec - cp -f ${<} ${@}
done
note *
note * requiring these is a botch
Expand All @@ -288,7 +288,7 @@ make install virtual
prev mkreq-maplib
exec - mkreq-maplib ${CC} : iconv : iconv.c : iconv
done
exec - ${STDCP} -f ${<} ${@}
exec - cp -f ${<} ${@}
done
make ${INSTALLROOT}/lib/lib/w
make w.req
Expand All @@ -297,7 +297,7 @@ make install virtual
prev mkreq-maplib
exec - mkreq-maplib ${CC} : w : w.c w2.c : w
done
exec - ${STDCP} -f ${<} ${@}
exec - cp -f ${<} ${@}
done
note *
note * miscellaneous -l* checks
Expand All @@ -308,7 +308,7 @@ make install virtual
prev mkreq-maplib
exec - mkreq-maplib ${CC} : intl : intl.c : intl
done
exec - ${STDCP} -f ${<} ${@}
exec - cp -f ${<} ${@}
done
make ${INSTALLROOT}/lib/lib/m
make m.req
Expand All @@ -321,15 +321,15 @@ make install virtual
prev mkreq-maplib
exec - mkreq-maplib ${CC} : m : m.c m2.c m3.c m4.c m5.c m6.c : m
done
exec - ${STDCP} -f ${<} ${@}
exec - cp -f ${<} ${@}
done
make ${INSTALLROOT}/lib/lib/nsl
make nsl.req
prev nsl.c
prev mkreq-maplib
exec - mkreq-maplib ${CC} : nsl : nsl.c : nsl
done
exec - ${STDCP} -f ${<} ${@}
exec - cp -f ${<} ${@}
done
note *
note * what was sco smoking
Expand All @@ -344,7 +344,7 @@ make install virtual
prev mkreq-maplib
exec - mkreq-maplib ${CC} : socket : socket.c nsl.c : socket
done
exec - ${STDCP} -f ${<} ${@}
exec - cp -f ${<} ${@}
done
note *
note * more substance abuse
Expand All @@ -367,7 +367,7 @@ make install virtual
prev mkreq-maplib
exec - mkreq-maplib ${CC} : dbm : db.c gdbm.c gdbm1.c gdbm2.c : db gdbm_compat gdbm ndbm dbm
done
exec - ${STDCP} -f ${<} ${@}
exec - cp -f ${<} ${@}
done
done install
make test dontcare virtual
Expand Down
1 change: 1 addition & 0 deletions src/cmd/INIT/mamprobe.sh
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ echo "note generated by $0 for $cc"
echo 'setv _hosttype_ ${mam_cc_HOSTTYPE}'

# STD* are standard commands/flags
# (Deprecated; kept for backward compatibility)

if (
ed <<!
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/builtin/Mamfile
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ make install virtual
make ${INSTALLROOT}/bin/pty
prev pty
note * macOS throws code signature error if 'cp' overwrites Mach-O binary; must remove first
exec - ${STDRM} -f ${@} && ${STDCP} ${<} ${@}
exec - rm -f ${@} && cp ${<} ${@}
done
done install
make test dontcare virtual
Expand Down
Loading

0 comments on commit 357fc44

Please sign in to comment.