implement “enable” (like GNU bash) to stop Harzilein from complaining

about mksh’s “rename” builtin
This commit is contained in:
tg 2015-03-08 23:50:00 +00:00
parent 8f53c68265
commit f893c7a347
1 changed files with 192 additions and 1 deletions

View File

@ -1,5 +1,5 @@
# $Id$
# $MirOS: src/bin/mksh/dot.mkshrc,v 1.92 2015/03/08 22:54:33 tg Exp $
# $MirOS: src/bin/mksh/dot.mkshrc,v 1.93 2015/03/08 23:50:00 tg Exp $
#-
# Copyright (c) 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010,
# 2011, 2012, 2013, 2014, 2015
@ -381,6 +381,197 @@ function setenv {
\eval "'export' \"$1\""'="$2"'
}
# toggle built-in aliases and utilities, and aliases and functions from here
function enable {
\builtin typeset doprnt=0 mode=1 x y z rv=0
\builtin typeset b_alias i_alias i_func nalias=0 nfunc=0 i_all
\set -A b_alias
\set -A i_alias
\set -A i_func
# accumulate mksh built-in aliases, in ASCIIbetical order
i_alias[nalias]=autoload; b_alias[nalias++]='\builtin typeset -fu'
i_alias[nalias]=functions; b_alias[nalias++]='\builtin typeset -f'
i_alias[nalias]=hash; b_alias[nalias++]='\builtin alias -t'
i_alias[nalias]=history; b_alias[nalias++]='\builtin fc -l'
i_alias[nalias]=integer; b_alias[nalias++]='\builtin typeset -i'
i_alias[nalias]=local; b_alias[nalias++]='\builtin typeset'
i_alias[nalias]=login; b_alias[nalias++]='\exec login'
i_alias[nalias]=nameref; b_alias[nalias++]='\builtin typeset -n'
i_alias[nalias]=nohup; b_alias[nalias++]='nohup '
i_alias[nalias]=r; b_alias[nalias++]='\builtin fc -e -'
i_alias[nalias]=source; b_alias[nalias++]='PATH=$PATH:. \command .'
i_alias[nalias]=stop; b_alias[nalias++]='\kill -STOP'
i_alias[nalias]=type; b_alias[nalias++]='\builtin whence -v'
# accumulate mksh built-in utilities, in definition order, even ifndef
i_func[nfunc++]=.
i_func[nfunc++]=:
i_func[nfunc++]='['
i_func[nfunc++]=alias
i_func[nfunc++]=break
i_func[nfunc++]=builtin
i_func[nfunc++]=cat
i_func[nfunc++]=cd
i_func[nfunc++]=chdir
i_func[nfunc++]=command
i_func[nfunc++]=continue
i_func[nfunc++]=echo
i_func[nfunc++]=eval
i_func[nfunc++]=exec
i_func[nfunc++]=exit
i_func[nfunc++]=export
i_func[nfunc++]=false
i_func[nfunc++]=fc
i_func[nfunc++]=getopts
i_func[nfunc++]=global
i_func[nfunc++]=jobs
i_func[nfunc++]=kill
i_func[nfunc++]=let
i_func[nfunc++]='let]'
i_func[nfunc++]=print
i_func[nfunc++]=pwd
i_func[nfunc++]=read
i_func[nfunc++]=readonly
i_func[nfunc++]=realpath
i_func[nfunc++]=rename
i_func[nfunc++]=return
i_func[nfunc++]=set
i_func[nfunc++]=shift
i_func[nfunc++]=suspend
i_func[nfunc++]=test
i_func[nfunc++]=times
i_func[nfunc++]=trap
i_func[nfunc++]=true
i_func[nfunc++]=typeset
i_func[nfunc++]=ulimit
i_func[nfunc++]=umask
i_func[nfunc++]=unalias
i_func[nfunc++]=unset
i_func[nfunc++]=wait
i_func[nfunc++]=whence
i_func[nfunc++]=bg
i_func[nfunc++]=fg
i_func[nfunc++]=bind
i_func[nfunc++]=mknod
i_func[nfunc++]=printf
i_func[nfunc++]=sleep
i_func[nfunc++]=domainname
# accumulate aliases from dot.mkshrc, in definition order
i_alias[nalias]=l; b_alias[nalias++]='ls -F'
i_alias[nalias]=la; b_alias[nalias++]='l -a'
i_alias[nalias]=ll; b_alias[nalias++]='l -l'
i_alias[nalias]=lo; b_alias[nalias++]='l -alo'
i_alias[nalias]=doch; b_alias[nalias++]='sudo mksh -c "$(\builtin fc -ln -1)"'
i_alias[nalias]=rot13; b_alias[nalias++]='tr abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM'
i_alias[nalias]=cls; b_alias[nalias++]='\builtin print -n \\033c'
# accumulate functions from dot.mkshrc, in definition order
i_func[nfunc++]=hd
i_func[nfunc++]=chpwd
i_func[nfunc++]=cd_csh
i_func[nfunc++]=dirs
i_func[nfunc++]=popd
i_func[nfunc++]=pushd
i_func[nfunc++]=smores
i_func[nfunc++]=Lb64decode
i_func[nfunc++]=Lb64encode
i_func[nfunc++]=Lbafh_init
i_func[nfunc++]=Lbafh_add
i_func[nfunc++]=Lbafh_finish
i_func[nfunc++]=Lstripcom
i_func[nfunc++]=setenv
i_func[nfunc++]=enable
# collect all identifiers, sorted ASCIIbetically
\set -sA i_all -- "${i_alias[@]}" "${i_func[@]}"
# handle options, we don't do dynamic loading
while \getopts "adf:nps" x; do
case $x {
(a)
mode=-1
;;
(d)
# deliberately causing an error, like bash-static
;|
(f)
\builtin print -u2 enable: dynamic loading not available
\return 2
;;
(n)
mode=0
;;
(p)
doprnt=1
;;
(s)
\set -sA i_all -- . : break continue eval exec exit \
export readonly return set shift times trap unset
;;
(*)
\builtin print -u2 enable: usage: \
"enable [-adnps] [-f filename] [name ...]"
return 2
;;
}
done
\shift $((OPTIND - 1))
# display builtins enabled/disabled/all/special?
if (( doprnt || ($# == 0) )); then
for x in "${i_all[@]}"; do
y=$(\alias "$x") || y=
[[ $y = "$x='\\builtin whence -p $x >/dev/null || (\\builtin print mksh: $x: not found; exit 127) && \$(\\builtin whence -p $x)'" ]]; z=$?
case $mode:$z {
(-1:0|0:0)
\print -r -- "enable -n $x"
;;
(-1:1|1:1)
\print -r -- "enable $x"
;;
}
done
\return 0
fi
for x in "$@"; do
z=0
for y in "${i_alias[@]}" "${i_func[@]}"; do
[[ $x = "$y" ]] || \continue
z=1
\break
done
if (( !z )); then
\builtin print -ru2 enable: "$x": not a shell builtin
rv=1
\continue
fi
if (( !mode )); then
# disable this
\alias "$x=\\builtin whence -p $x >/dev/null || (\\builtin print mksh: $x: not found; exit 127) && \$(\\builtin whence -p $x)"
else
# find out if this is an alias or not, first
z=0
y=-1
while (( ++y < nalias )); do
[[ $x = "${i_alias[y]}" ]] || \continue
z=1
\break
done
if (( z )); then
# re-enable the original alias body
\alias "$x=${b_alias[y]}"
else
# re-enable the original utility/function
\unalias "$x"
fi
fi
done
\return $rv
}
\: place customisations below this line
for p in ~/.etc/bin ~/bin; do