2009-08-27 18:52:12 +02:00
|
|
|
# $Id$
|
2017-03-19 23:31:29 +01:00
|
|
|
# $MirOS: src/bin/mksh/dot.mkshrc,v 1.114 2017/03/19 22:31:26 tg Exp $
|
2009-05-16 18:59:42 +02:00
|
|
|
#-
|
2012-07-30 23:37:17 +02:00
|
|
|
# Copyright (c) 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010,
|
2017-02-18 03:33:15 +01:00
|
|
|
# 2011, 2012, 2013, 2014, 2015, 2016, 2017
|
2015-10-09 23:36:59 +02:00
|
|
|
# mirabilos <m@mirbsd.org>
|
2009-05-16 18:59:42 +02:00
|
|
|
#
|
|
|
|
# Provided that these terms and disclaimer and all copyright notices
|
|
|
|
# are retained or reproduced in an accompanying document, permission
|
|
|
|
# is granted to deal in this work without restriction, including un-
|
|
|
|
# limited rights to use, publicly perform, distribute, sell, modify,
|
|
|
|
# merge, give away, or sublicence.
|
|
|
|
#
|
|
|
|
# This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
|
|
|
|
# the utmost extent permitted by applicable law, neither express nor
|
|
|
|
# implied; without malicious intent or gross negligence. In no event
|
|
|
|
# may a licensor, author or contributor be held liable for indirect,
|
|
|
|
# direct, other damage, loss, or other issues arising in any way out
|
|
|
|
# of dealing in the work, even if advised of the possibility of such
|
|
|
|
# damage or existence of a defect, except proven that it results out
|
|
|
|
# of said person's immediate fault when using the work as intended.
|
2007-09-24 22:30:04 +02:00
|
|
|
#-
|
2010-02-25 21:48:10 +01:00
|
|
|
# ${ENV:-~/.mkshrc}: mksh initialisation file for interactive shells
|
2007-09-24 22:30:04 +02:00
|
|
|
|
2017-02-18 03:33:15 +01:00
|
|
|
# catch non-mksh, non-lksh, trying to run this file
|
2015-04-11 21:01:59 +02:00
|
|
|
case ${KSH_VERSION:-} in
|
2017-02-18 03:33:15 +01:00
|
|
|
*LEGACY\ KSH*|*MIRBSD\ KSH*) ;;
|
2017-03-19 21:36:08 +01:00
|
|
|
*) \return 0 ;;
|
2013-04-27 20:19:44 +02:00
|
|
|
esac
|
|
|
|
|
2017-02-18 03:33:15 +01:00
|
|
|
# give MidnightBSD's laffer1 a bit of csh feeling
|
|
|
|
function setenv {
|
|
|
|
if (( $# )); then
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin eval '\\builtin export "$1"="${2:-}"'
|
2017-02-18 03:33:15 +01:00
|
|
|
else
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin typeset -x
|
2017-02-18 03:33:15 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# pager (not control character safe)
|
|
|
|
smores() (
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin set +m
|
|
|
|
\\builtin cat "$@" |&
|
|
|
|
\\builtin trap "rv=\$?; \\\\builtin kill $! >/dev/null 2>&1; \\\\builtin exit \$rv" EXIT
|
|
|
|
while IFS= \\builtin read -pr line; do
|
2017-02-18 03:33:15 +01:00
|
|
|
llen=${%line}
|
|
|
|
(( llen == -1 )) && llen=${#line}
|
|
|
|
(( llen = llen ? (llen + COLUMNS - 1) / COLUMNS : 1 ))
|
|
|
|
if (( (curlin += llen) >= LINES )); then
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin print -nr -- $'\e[7m--more--\e[0m'
|
|
|
|
\\builtin read -u1 || \\builtin exit $?
|
|
|
|
[[ $REPLY = [Qq]* ]] && \\builtin exit 0
|
2017-02-18 03:33:15 +01:00
|
|
|
curlin=$llen
|
|
|
|
fi
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin print -r -- "$line"
|
2017-02-18 03:33:15 +01:00
|
|
|
done
|
|
|
|
)
|
|
|
|
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin alias ls=ls l='ls -F' la='l -a' ll='l -l' lo='l -alo'
|
|
|
|
\: "${HOSTNAME:=$(\\builtin ulimit -c 0; \\builtin print -r -- $(hostname \
|
|
|
|
2>/dev/null))}${EDITOR:=/bin/ed}${TERM:=vt100}${USER:=$(\\builtin ulimit \
|
|
|
|
-c 0; id -un 2>/dev/null)}${USER:=?}"
|
|
|
|
[[ $HOSTNAME = ?(?(ip6-)localhost?(6)) ]] && HOSTNAME=nil; \\builtin unalias ls
|
|
|
|
\\builtin export EDITOR HOSTNAME TERM USER
|
2017-02-18 03:33:15 +01:00
|
|
|
|
|
|
|
# minimal support for lksh users
|
|
|
|
if [[ $KSH_VERSION = *LEGACY\ KSH* ]]; then
|
|
|
|
PS1='$USER@${HOSTNAME%%.*}:$PWD>'
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin return 0
|
2017-02-18 03:33:15 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# mksh-specific from here
|
2017-03-19 21:36:08 +01:00
|
|
|
\: "${MKSH:=$(\\builtin whence -p mksh)}${MKSH:=/bin/mksh}"
|
|
|
|
\\builtin export MKSH
|
2017-02-18 03:33:15 +01:00
|
|
|
|
|
|
|
PS4='[$EPOCHREALTIME] '; PS1='#'; (( USER_ID )) && PS1='$'; PS1=$'\001\r''${|
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin typeset e=$?
|
2007-09-25 17:48:45 +02:00
|
|
|
|
2013-05-02 23:59:54 +02:00
|
|
|
(( e )) && REPLY+="$e|"
|
2015-04-11 20:08:56 +02:00
|
|
|
REPLY+=${USER}@${HOSTNAME%%.*}:
|
2013-05-02 23:59:54 +02:00
|
|
|
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin typeset d=${PWD:-?}/ p=~; [[ $p = ?(*/) ]] || d=${d/#$p\//\~/}
|
|
|
|
d=${d%/}; \\builtin typeset m=${%d} n p=...; (( m > 0 )) || m=${#d}
|
2013-05-02 23:59:54 +02:00
|
|
|
(( m > (n = (COLUMNS/3 < 7 ? 7 : COLUMNS/3)) )) && d=${d:(-n)} || p=
|
|
|
|
REPLY+=$p$d
|
|
|
|
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin return $e
|
2015-04-11 21:01:59 +02:00
|
|
|
} '"$PS1 "
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin alias doch='sudo mksh -c "$(\\builtin fc -ln -1)"'
|
|
|
|
\\builtin command -v rot13 >/dev/null || \\builtin alias rot13='tr \
|
2008-02-29 17:38:41 +01:00
|
|
|
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ \
|
|
|
|
nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM'
|
2017-03-19 21:36:08 +01:00
|
|
|
if \\builtin command -v hd >/dev/null; then
|
|
|
|
\:
|
|
|
|
elif \\builtin command -v hexdump >/dev/null; then
|
2013-05-02 23:46:29 +02:00
|
|
|
function hd {
|
|
|
|
hexdump -e '"%08.8_ax " 8/1 "%02X " " - " 8/1 "%02X "' \
|
|
|
|
-e '" |" "%_p"' -e '"|\n"' "$@"
|
|
|
|
}
|
|
|
|
else
|
|
|
|
function hd {
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin typeset -Uui16 -Z11 pos=0
|
|
|
|
\\builtin typeset -Uui16 -Z5 hv=2147483647
|
|
|
|
\\builtin typeset dasc line i
|
|
|
|
\\builtin set +U
|
2013-05-02 23:46:29 +02:00
|
|
|
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin cat "$@" | if \\builtin read -arN -1 line; then
|
|
|
|
\\builtin typeset -i1 'line[*]'
|
2013-05-02 23:46:29 +02:00
|
|
|
i=0
|
|
|
|
while (( i < ${#line[*]} )); do
|
|
|
|
hv=${line[i++]}
|
|
|
|
if (( (pos & 15) == 0 )); then
|
2015-04-11 21:01:59 +02:00
|
|
|
(( pos )) && \
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin print -r -- "$dasc|"
|
|
|
|
\\builtin print -nr "${pos#16#} "
|
2013-05-02 23:46:29 +02:00
|
|
|
dasc=' |'
|
|
|
|
fi
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin print -nr "${hv#16#} "
|
2015-04-29 22:56:18 +02:00
|
|
|
#XXX EBCDIC, but we need [[:print:]] to fix this
|
2013-05-02 23:46:29 +02:00
|
|
|
if (( (hv < 32) || (hv > 126) )); then
|
|
|
|
dasc+=.
|
|
|
|
else
|
|
|
|
dasc+=${line[i-1]#1#}
|
|
|
|
fi
|
2015-04-11 21:01:59 +02:00
|
|
|
(( (pos++ & 15) == 7 )) && \
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin print -nr -- '- '
|
2013-05-02 23:46:29 +02:00
|
|
|
done
|
|
|
|
while (( pos & 15 )); do
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin print -nr ' '
|
2015-04-11 21:01:59 +02:00
|
|
|
(( (pos++ & 15) == 7 )) && \
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin print -nr -- '- '
|
2013-05-02 23:46:29 +02:00
|
|
|
done
|
2017-03-19 21:36:08 +01:00
|
|
|
(( hv == 2147483647 )) || \\builtin print -r -- "$dasc|"
|
2015-07-05 21:02:16 +02:00
|
|
|
fi
|
2013-05-02 23:46:29 +02:00
|
|
|
}
|
|
|
|
fi
|
2006-07-23 20:44:22 +02:00
|
|
|
|
2007-09-24 22:30:04 +02:00
|
|
|
# Berkeley C shell compatible dirs, popd, and pushd functions
|
|
|
|
# Z shell compatible chpwd() hook, used to update DIRSTACK[0]
|
2017-03-19 21:36:08 +01:00
|
|
|
DIRSTACKBASE=$(\\builtin realpath ~/. 2>/dev/null || \
|
|
|
|
\\builtin print -nr -- "${HOME:-/}")
|
2017-03-19 21:59:29 +01:00
|
|
|
\\builtin set -A DIRSTACK
|
2007-09-24 22:30:04 +02:00
|
|
|
function chpwd {
|
2017-03-19 21:36:08 +01:00
|
|
|
DIRSTACK[0]=$(\\builtin realpath . 2>/dev/null || \
|
|
|
|
\\builtin print -nr -- "$PWD")
|
2008-05-17 00:22:11 +02:00
|
|
|
[[ $DIRSTACKBASE = ?(*/) ]] || \
|
2015-12-12 19:47:40 +01:00
|
|
|
DIRSTACK[0]=${DIRSTACK[0]/#$DIRSTACKBASE/\~}
|
2015-03-08 23:54:36 +01:00
|
|
|
\:
|
2007-09-24 22:30:04 +02:00
|
|
|
}
|
2015-03-08 23:54:36 +01:00
|
|
|
\chpwd .
|
2015-06-27 22:44:01 +02:00
|
|
|
cd() {
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin cd "$@" || \\builtin return $?
|
2015-03-08 23:54:36 +01:00
|
|
|
\chpwd "$@"
|
2007-09-24 22:30:04 +02:00
|
|
|
}
|
|
|
|
function cd_csh {
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin typeset d t=${1/#\~/$DIRSTACKBASE}
|
2007-09-24 22:30:04 +02:00
|
|
|
|
2017-03-19 21:36:08 +01:00
|
|
|
if ! d=$(\\builtin cd "$t" 2>&1); then
|
|
|
|
\\builtin print -ru2 "${1}: ${d##*cd: $t: }."
|
|
|
|
\\builtin return 1
|
2007-09-24 22:30:04 +02:00
|
|
|
fi
|
2015-03-08 23:54:36 +01:00
|
|
|
\cd "$t"
|
2007-09-24 22:30:04 +02:00
|
|
|
}
|
|
|
|
function dirs {
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin typeset d dwidth
|
|
|
|
\\builtin typeset -i fl=0 fv=0 fn=0 cpos=0
|
2007-09-24 22:30:04 +02:00
|
|
|
|
2017-03-19 21:36:08 +01:00
|
|
|
while \\builtin getopts ":lvn" d; do
|
2007-09-24 22:30:04 +02:00
|
|
|
case $d {
|
|
|
|
(l) fl=1 ;;
|
|
|
|
(v) fv=1 ;;
|
|
|
|
(n) fn=1 ;;
|
2017-03-19 21:36:08 +01:00
|
|
|
(*) \\builtin print -ru2 'Usage: dirs [-lvn].'
|
|
|
|
\\builtin return 1 ;;
|
2007-09-24 22:30:04 +02:00
|
|
|
}
|
|
|
|
done
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin shift $((OPTIND - 1))
|
2007-09-24 22:30:04 +02:00
|
|
|
if (( $# > 0 )); then
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin print -ru2 'Usage: dirs [-lvn].'
|
|
|
|
\\builtin return 1
|
2007-09-24 22:30:04 +02:00
|
|
|
fi
|
|
|
|
if (( fv )); then
|
|
|
|
fv=0
|
|
|
|
while (( fv < ${#DIRSTACK[*]} )); do
|
|
|
|
d=${DIRSTACK[fv]}
|
2015-12-12 19:47:40 +01:00
|
|
|
(( fl )) && d=${d/#\~/$DIRSTACKBASE}
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin print -r -- "$fv $d"
|
|
|
|
(( ++fv ))
|
2007-09-24 22:30:04 +02:00
|
|
|
done
|
|
|
|
else
|
|
|
|
fv=0
|
|
|
|
while (( fv < ${#DIRSTACK[*]} )); do
|
|
|
|
d=${DIRSTACK[fv]}
|
2015-12-12 19:47:40 +01:00
|
|
|
(( fl )) && d=${d/#\~/$DIRSTACKBASE}
|
2010-07-11 15:54:37 +02:00
|
|
|
(( dwidth = (${%d} > 0 ? ${%d} : ${#d}) ))
|
|
|
|
if (( fn && (cpos += dwidth + 1) >= 79 && \
|
|
|
|
dwidth < 80 )); then
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin print
|
2010-07-11 15:54:37 +02:00
|
|
|
(( cpos = dwidth + 1 ))
|
2007-09-24 22:30:04 +02:00
|
|
|
fi
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin print -nr -- "$d "
|
|
|
|
(( ++fv ))
|
2007-09-24 22:30:04 +02:00
|
|
|
done
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin print
|
2007-09-24 22:30:04 +02:00
|
|
|
fi
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin return 0
|
2007-09-24 22:30:04 +02:00
|
|
|
}
|
|
|
|
function popd {
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin typeset d fa
|
|
|
|
\\builtin typeset -i n=1
|
2007-09-24 22:30:04 +02:00
|
|
|
|
2017-03-19 21:36:08 +01:00
|
|
|
while \\builtin getopts ":0123456789lvn" d; do
|
2007-09-24 22:30:04 +02:00
|
|
|
case $d {
|
2011-11-26 00:58:04 +01:00
|
|
|
(l|v|n) fa+=" -$d" ;;
|
2007-09-24 22:30:04 +02:00
|
|
|
(+*) n=2
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin break ;;
|
|
|
|
(*) \\builtin print -ru2 'Usage: popd [-lvn] [+<n>].'
|
|
|
|
\\builtin return 1 ;;
|
2007-09-24 22:30:04 +02:00
|
|
|
}
|
|
|
|
done
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin shift $((OPTIND - n))
|
2007-09-24 22:30:04 +02:00
|
|
|
n=0
|
|
|
|
if (( $# > 1 )); then
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin print -ru2 popd: Too many arguments.
|
|
|
|
\\builtin return 1
|
2007-09-24 22:30:04 +02:00
|
|
|
elif [[ $1 = ++([0-9]) && $1 != +0 ]]; then
|
|
|
|
if (( (n = ${1#+}) >= ${#DIRSTACK[*]} )); then
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin print -ru2 popd: Directory stack not that deep.
|
|
|
|
\\builtin return 1
|
2007-09-24 22:30:04 +02:00
|
|
|
fi
|
|
|
|
elif [[ -n $1 ]]; then
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin print -ru2 popd: Bad directory.
|
|
|
|
\\builtin return 1
|
2007-09-24 22:30:04 +02:00
|
|
|
fi
|
|
|
|
if (( ${#DIRSTACK[*]} < 2 )); then
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin print -ru2 popd: Directory stack empty.
|
|
|
|
\\builtin return 1
|
2007-09-24 22:30:04 +02:00
|
|
|
fi
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin unset DIRSTACK[n]
|
|
|
|
\\builtin set -A DIRSTACK -- "${DIRSTACK[@]}"
|
|
|
|
\cd_csh "${DIRSTACK[0]}" || \\builtin return 1
|
2015-03-08 23:54:36 +01:00
|
|
|
\dirs $fa
|
2007-09-24 22:30:04 +02:00
|
|
|
}
|
|
|
|
function pushd {
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin typeset d fa
|
|
|
|
\\builtin typeset -i n=1
|
2007-09-24 22:30:04 +02:00
|
|
|
|
2017-03-19 21:36:08 +01:00
|
|
|
while \\builtin getopts ":0123456789lvn" d; do
|
2007-09-24 22:30:04 +02:00
|
|
|
case $d {
|
2011-11-26 00:58:04 +01:00
|
|
|
(l|v|n) fa+=" -$d" ;;
|
2007-09-24 22:30:04 +02:00
|
|
|
(+*) n=2
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin break ;;
|
|
|
|
(*) \\builtin print -ru2 'Usage: pushd [-lvn] [<dir>|+<n>].'
|
|
|
|
\\builtin return 1 ;;
|
2007-09-24 22:30:04 +02:00
|
|
|
}
|
|
|
|
done
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin shift $((OPTIND - n))
|
2007-09-24 22:30:04 +02:00
|
|
|
if (( $# == 0 )); then
|
|
|
|
if (( ${#DIRSTACK[*]} < 2 )); then
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin print -ru2 pushd: No other directory.
|
|
|
|
\\builtin return 1
|
2007-09-24 22:30:04 +02:00
|
|
|
fi
|
|
|
|
d=${DIRSTACK[1]}
|
|
|
|
DIRSTACK[1]=${DIRSTACK[0]}
|
2017-03-19 21:36:08 +01:00
|
|
|
\cd_csh "$d" || \\builtin return 1
|
2007-09-24 22:30:04 +02:00
|
|
|
elif (( $# > 1 )); then
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin print -ru2 pushd: Too many arguments.
|
|
|
|
\\builtin return 1
|
2007-09-24 22:30:04 +02:00
|
|
|
elif [[ $1 = ++([0-9]) && $1 != +0 ]]; then
|
|
|
|
if (( (n = ${1#+}) >= ${#DIRSTACK[*]} )); then
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin print -ru2 pushd: Directory stack not that deep.
|
|
|
|
\\builtin return 1
|
2007-09-24 22:30:04 +02:00
|
|
|
fi
|
|
|
|
while (( n-- )); do
|
|
|
|
d=${DIRSTACK[0]}
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin unset DIRSTACK[0]
|
|
|
|
\\builtin set -A DIRSTACK -- "${DIRSTACK[@]}" "$d"
|
2007-09-24 22:30:04 +02:00
|
|
|
done
|
2017-03-19 21:36:08 +01:00
|
|
|
\cd_csh "${DIRSTACK[0]}" || \\builtin return 1
|
2007-09-24 22:30:04 +02:00
|
|
|
else
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin set -A DIRSTACK -- placeholder "${DIRSTACK[@]}"
|
|
|
|
\cd_csh "$1" || \\builtin return 1
|
2007-09-24 22:30:04 +02:00
|
|
|
fi
|
2015-03-08 23:54:36 +01:00
|
|
|
\dirs $fa
|
2007-09-24 22:30:04 +02:00
|
|
|
}
|
|
|
|
|
2015-04-29 22:56:18 +02:00
|
|
|
# base64 encoder and decoder, RFC compliant, NUL safe, not EBCDIC safe
|
2009-05-31 19:17:33 +02:00
|
|
|
function Lb64decode {
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin set +U
|
|
|
|
\\builtin typeset c s="$*" t
|
|
|
|
[[ -n $s ]] || { s=$(\\builtin cat; \\builtin print x); s=${s%x}; }
|
|
|
|
\\builtin typeset -i i=0 j=0 n=${#s} p=0 v x
|
|
|
|
\\builtin typeset -i16 o
|
2009-05-31 19:17:33 +02:00
|
|
|
|
|
|
|
while (( i < n )); do
|
|
|
|
c=${s:(i++):1}
|
|
|
|
case $c {
|
2017-03-19 21:36:08 +01:00
|
|
|
(=) \\builtin break ;;
|
2009-05-31 19:17:33 +02:00
|
|
|
([A-Z]) (( v = 1#$c - 65 )) ;;
|
|
|
|
([a-z]) (( v = 1#$c - 71 )) ;;
|
|
|
|
([0-9]) (( v = 1#$c + 4 )) ;;
|
|
|
|
(+) v=62 ;;
|
|
|
|
(/) v=63 ;;
|
2017-03-19 21:36:08 +01:00
|
|
|
(*) \\builtin continue ;;
|
2009-05-31 19:17:33 +02:00
|
|
|
}
|
|
|
|
(( x = (x << 6) | v ))
|
|
|
|
case $((p++)) {
|
2017-03-19 21:36:08 +01:00
|
|
|
(0) \\builtin continue ;;
|
2009-05-31 19:17:33 +02:00
|
|
|
(1) (( o = (x >> 4) & 255 )) ;;
|
|
|
|
(2) (( o = (x >> 2) & 255 )) ;;
|
|
|
|
(3) (( o = x & 255 ))
|
|
|
|
p=0
|
|
|
|
;;
|
|
|
|
}
|
2011-11-26 00:58:04 +01:00
|
|
|
t+=\\x${o#16#}
|
2017-03-19 21:36:08 +01:00
|
|
|
(( ++j & 4095 )) && \\builtin continue
|
|
|
|
\\builtin print -n $t
|
2011-11-26 00:58:04 +01:00
|
|
|
t=
|
2009-05-31 19:17:33 +02:00
|
|
|
done
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin print -n $t
|
2009-05-31 19:17:33 +02:00
|
|
|
}
|
|
|
|
function Lb64encode {
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin set +U
|
|
|
|
\\builtin typeset c s t table
|
|
|
|
\\builtin set -A table -- A B C D E F G H I J K L M N O P Q R S T U V W X Y Z \
|
|
|
|
a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 + /
|
2011-05-29 04:18:57 +02:00
|
|
|
if (( $# )); then
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin read -raN-1 s <<<"$*"
|
|
|
|
\\builtin unset s[${#s[*]}-1]
|
2011-05-29 04:18:57 +02:00
|
|
|
else
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin read -raN-1 s
|
2011-05-29 04:18:57 +02:00
|
|
|
fi
|
2017-03-19 22:08:25 +01:00
|
|
|
\\builtin typeset -i i=0 n=${#s[*]} v
|
2009-05-31 19:17:33 +02:00
|
|
|
|
|
|
|
while (( i < n )); do
|
2011-05-29 04:18:57 +02:00
|
|
|
(( v = s[i++] << 16 ))
|
2017-03-19 22:08:25 +01:00
|
|
|
(( v |= s[i++] << 8 ))
|
|
|
|
(( v |= s[i++] ))
|
2017-03-19 21:36:08 +01:00
|
|
|
t+=${table[v >> 18]}${table[v >> 12 & 63]}
|
|
|
|
c=${table[v >> 6 & 63]}
|
2009-05-31 19:17:33 +02:00
|
|
|
if (( i <= n )); then
|
2017-03-19 21:36:08 +01:00
|
|
|
t+=$c${table[v & 63]}
|
2009-05-31 19:17:33 +02:00
|
|
|
elif (( i == n + 1 )); then
|
2011-11-26 00:58:04 +01:00
|
|
|
t+=$c=
|
2009-05-31 19:17:33 +02:00
|
|
|
else
|
2011-11-26 00:58:04 +01:00
|
|
|
t+===
|
2009-05-31 19:17:33 +02:00
|
|
|
fi
|
|
|
|
if (( ${#t} == 76 || i >= n )); then
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin print -r $t
|
2009-05-31 19:17:33 +02:00
|
|
|
t=
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2014-01-11 19:09:43 +01:00
|
|
|
# Better Avalanche for the Jenkins Hash
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin typeset -Z11 -Uui16 Lbafh_v
|
2014-01-11 19:09:43 +01:00
|
|
|
function Lbafh_init {
|
|
|
|
Lbafh_v=0
|
|
|
|
}
|
|
|
|
function Lbafh_add {
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin set +U
|
|
|
|
\\builtin typeset s
|
2011-05-29 04:18:57 +02:00
|
|
|
if (( $# )); then
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin read -raN-1 s <<<"$*"
|
|
|
|
\\builtin unset s[${#s[*]}-1]
|
2011-05-29 04:18:57 +02:00
|
|
|
else
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin read -raN-1 s
|
2011-05-29 04:18:57 +02:00
|
|
|
fi
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin typeset -i i=0 n=${#s[*]}
|
2009-06-22 19:38:39 +02:00
|
|
|
|
|
|
|
while (( i < n )); do
|
2014-01-11 19:09:43 +01:00
|
|
|
((# Lbafh_v = (Lbafh_v + s[i++] + 1) * 1025 ))
|
|
|
|
((# Lbafh_v ^= Lbafh_v >> 6 ))
|
2009-06-22 19:38:39 +02:00
|
|
|
done
|
|
|
|
}
|
2014-01-11 19:09:43 +01:00
|
|
|
function Lbafh_finish {
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin typeset -Ui t
|
2014-01-02 23:51:01 +01:00
|
|
|
|
2014-01-11 19:09:43 +01:00
|
|
|
((# t = (((Lbafh_v >> 7) & 0x01010101) * 0x1B) ^ \
|
|
|
|
((Lbafh_v << 1) & 0xFEFEFEFE) ))
|
2016-07-25 22:41:23 +02:00
|
|
|
((# Lbafh_v = t ^ (t ^> 8) ^ (Lbafh_v ^> 8) ^ \
|
|
|
|
(Lbafh_v ^> 16) ^ (Lbafh_v ^> 24) ))
|
2015-03-08 23:54:36 +01:00
|
|
|
\:
|
2014-01-02 23:51:01 +01:00
|
|
|
}
|
2009-08-27 18:29:21 +02:00
|
|
|
|
2006-08-14 22:32:44 +02:00
|
|
|
# strip comments (and leading/trailing whitespace if IFS is set) from
|
|
|
|
# any file(s) given as argument, or stdin if none, and spew to stdout
|
2007-04-17 22:04:08 +02:00
|
|
|
function Lstripcom {
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin set -o noglob
|
|
|
|
\\builtin cat "$@" | while \\builtin read _line; do
|
2006-07-23 20:44:22 +02:00
|
|
|
_line=${_line%%#*}
|
2017-03-19 21:36:08 +01:00
|
|
|
[[ -n $_line ]] && \\builtin print -r -- $_line
|
2015-07-05 21:02:16 +02:00
|
|
|
done
|
2006-07-23 20:44:22 +02:00
|
|
|
}
|
2007-03-04 07:20:48 +01:00
|
|
|
|
2015-04-11 21:01:59 +02:00
|
|
|
# toggle built-in aliases and utilities, and aliases and functions from mkshrc
|
2015-03-09 00:50:00 +01:00
|
|
|
function enable {
|
2017-03-19 21:36:08 +01:00
|
|
|
\\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
|
|
|
|
\\builtin set -A b_alias
|
|
|
|
\\builtin set -A i_alias
|
|
|
|
\\builtin set -A i_func
|
2015-03-09 00:50:00 +01:00
|
|
|
|
|
|
|
# accumulate mksh built-in aliases, in ASCIIbetical order
|
2017-03-19 21:36:08 +01:00
|
|
|
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++]='\\builtin exec login'
|
|
|
|
i_alias[nalias]=nameref; b_alias[nalias++]='\\builtin typeset -n'
|
2015-03-09 00:50:00 +01:00
|
|
|
i_alias[nalias]=nohup; b_alias[nalias++]='nohup '
|
2017-03-19 21:36:08 +01:00
|
|
|
i_alias[nalias]=r; b_alias[nalias++]='\\builtin fc -e -'
|
|
|
|
i_alias[nalias]=type; b_alias[nalias++]='\\builtin whence -v'
|
2015-03-09 00:50:00 +01:00
|
|
|
|
|
|
|
# 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
|
2017-03-19 21:36:08 +01:00
|
|
|
# \\builtin cannot, by design, be overridden
|
2015-03-09 00:50:00 +01:00
|
|
|
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++]=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
|
2015-10-09 23:36:59 +02:00
|
|
|
i_func[nfunc++]=source
|
2015-03-09 00:50:00 +01:00
|
|
|
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
|
2015-10-09 23:36:59 +02:00
|
|
|
i_func[nfunc++]=extproc
|
2015-03-09 00:50:00 +01:00
|
|
|
|
|
|
|
# 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'
|
2017-03-19 21:36:08 +01:00
|
|
|
i_alias[nalias]=doch; b_alias[nalias++]='sudo mksh -c "$(\\builtin fc -ln -1)"'
|
2015-03-09 00:50:00 +01:00
|
|
|
i_alias[nalias]=rot13; b_alias[nalias++]='tr abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM'
|
2017-03-19 21:36:08 +01:00
|
|
|
i_alias[nalias]=cls; b_alias[nalias++]='\\builtin print -n \\ec'
|
2015-03-09 00:50:00 +01:00
|
|
|
|
|
|
|
# accumulate functions from dot.mkshrc, in definition order
|
2017-03-19 21:36:08 +01:00
|
|
|
i_func[nfunc++]=setenv
|
|
|
|
i_func[nfunc++]=smores
|
2015-03-09 00:50:00 +01:00
|
|
|
i_func[nfunc++]=hd
|
|
|
|
i_func[nfunc++]=chpwd
|
2016-07-27 00:03:41 +02:00
|
|
|
i_func[nfunc++]=cd
|
2015-03-09 00:50:00 +01:00
|
|
|
i_func[nfunc++]=cd_csh
|
|
|
|
i_func[nfunc++]=dirs
|
|
|
|
i_func[nfunc++]=popd
|
|
|
|
i_func[nfunc++]=pushd
|
|
|
|
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++]=enable
|
|
|
|
|
|
|
|
# collect all identifiers, sorted ASCIIbetically
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin set -sA i_all -- "${i_alias[@]}" "${i_func[@]}"
|
2015-03-09 00:50:00 +01:00
|
|
|
|
|
|
|
# handle options, we don't do dynamic loading
|
2017-03-19 21:36:08 +01:00
|
|
|
while \\builtin getopts "adf:nps" x; do
|
2015-03-09 00:50:00 +01:00
|
|
|
case $x {
|
|
|
|
(a)
|
|
|
|
mode=-1
|
|
|
|
;;
|
|
|
|
(d)
|
|
|
|
# deliberately causing an error, like bash-static
|
|
|
|
;|
|
|
|
|
(f)
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin print -ru2 enable: dynamic loading not available
|
|
|
|
\\builtin return 2
|
2015-03-09 00:50:00 +01:00
|
|
|
;;
|
|
|
|
(n)
|
|
|
|
mode=0
|
|
|
|
;;
|
|
|
|
(p)
|
|
|
|
doprnt=1
|
|
|
|
;;
|
|
|
|
(s)
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin set -sA i_all -- . : break continue eval \
|
|
|
|
exec exit export readonly return set shift times \
|
|
|
|
trap unset
|
2015-03-09 00:50:00 +01:00
|
|
|
;;
|
|
|
|
(*)
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin print -ru2 enable: usage: \
|
2015-03-09 00:50:00 +01:00
|
|
|
"enable [-adnps] [-f filename] [name ...]"
|
|
|
|
return 2
|
|
|
|
;;
|
|
|
|
}
|
|
|
|
done
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin shift $((OPTIND - 1))
|
2015-03-09 00:50:00 +01:00
|
|
|
|
|
|
|
# display builtins enabled/disabled/all/special?
|
|
|
|
if (( doprnt || ($# == 0) )); then
|
|
|
|
for x in "${i_all[@]}"; do
|
2017-03-19 21:36:08 +01:00
|
|
|
y=$(\\builtin alias "$x") || y=
|
|
|
|
[[ $y = "$x='\\\\builtin whence -p $x >/dev/null || (\\\\builtin print -r mksh: $x: not found; \\\\builtin exit 127) && \$(\\\\builtin whence -p $x)'" ]]; z=$?
|
2015-03-09 00:50:00 +01:00
|
|
|
case $mode:$z {
|
|
|
|
(-1:0|0:0)
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin print -r -- "enable -n $x"
|
2015-03-09 00:50:00 +01:00
|
|
|
;;
|
|
|
|
(-1:1|1:1)
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin print -r -- "enable $x"
|
2015-03-09 00:50:00 +01:00
|
|
|
;;
|
|
|
|
}
|
|
|
|
done
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin return 0
|
2015-03-09 00:50:00 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
for x in "$@"; do
|
|
|
|
z=0
|
|
|
|
for y in "${i_alias[@]}" "${i_func[@]}"; do
|
2017-03-19 21:36:08 +01:00
|
|
|
[[ $x = "$y" ]] || \\builtin continue
|
2015-03-09 00:50:00 +01:00
|
|
|
z=1
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin break
|
2015-03-09 00:50:00 +01:00
|
|
|
done
|
|
|
|
if (( !z )); then
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin print -ru2 enable: "$x": not a shell builtin
|
2015-03-09 00:50:00 +01:00
|
|
|
rv=1
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin continue
|
2015-03-09 00:50:00 +01:00
|
|
|
fi
|
|
|
|
if (( !mode )); then
|
|
|
|
# disable this
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin alias "$x=\\\\builtin whence -p $x >/dev/null || (\\\\builtin print -r mksh: $x: not found; \\\\builtin exit 127) && \$(\\\\builtin whence -p $x)"
|
2015-03-09 00:50:00 +01:00
|
|
|
else
|
|
|
|
# find out if this is an alias or not, first
|
|
|
|
z=0
|
|
|
|
y=-1
|
|
|
|
while (( ++y < nalias )); do
|
2017-03-19 21:36:08 +01:00
|
|
|
[[ $x = "${i_alias[y]}" ]] || \\builtin continue
|
2015-03-09 00:50:00 +01:00
|
|
|
z=1
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin break
|
2015-03-09 00:50:00 +01:00
|
|
|
done
|
|
|
|
if (( z )); then
|
|
|
|
# re-enable the original alias body
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin alias "$x=${b_alias[y]}"
|
2015-03-09 00:50:00 +01:00
|
|
|
else
|
|
|
|
# re-enable the original utility/function
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin unalias "$x"
|
2015-03-09 00:50:00 +01:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin return $rv
|
2015-03-09 00:50:00 +01:00
|
|
|
}
|
|
|
|
|
2015-03-08 23:54:36 +01:00
|
|
|
\: place customisations below this line
|
2009-11-03 18:58:44 +01:00
|
|
|
|
2017-03-19 23:31:29 +01:00
|
|
|
for p in ~/.etc/bin ~/bin; do
|
2017-03-19 21:36:08 +01:00
|
|
|
[[ -d $p/. ]] || \\builtin continue
|
2017-03-19 23:31:29 +01:00
|
|
|
[[ $PATHSEP$PATH$PATHSEP = *"$PATHSEP$p$PATHSEP"* ]] || \
|
|
|
|
PATH=$p$PATHSEP$PATH
|
2009-11-03 18:58:44 +01:00
|
|
|
done
|
|
|
|
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin export SHELL=$MKSH MANWIDTH=80 LESSHISTFILE=-
|
|
|
|
\\builtin alias cls='\\builtin print -n \\ec'
|
2009-11-03 18:58:44 +01:00
|
|
|
|
2017-03-19 21:36:08 +01:00
|
|
|
#\\builtin unset LANGUAGE LC_ADDRESS LC_ALL LC_COLLATE LC_IDENTIFICATION LC_MONETARY \
|
2009-11-03 18:58:44 +01:00
|
|
|
# LC_NAME LC_NUMERIC LC_TELEPHONE LC_TIME
|
|
|
|
#p=en_GB.UTF-8
|
2017-03-19 21:36:08 +01:00
|
|
|
#\\builtin export LANG=C LC_CTYPE=$p LC_MEASUREMENT=$p LC_MESSAGES=$p LC_PAPER=$p
|
|
|
|
#\\builtin set -U
|
2009-11-03 18:58:44 +01:00
|
|
|
|
2017-03-19 21:36:08 +01:00
|
|
|
\\builtin unset p
|
2009-11-03 18:58:44 +01:00
|
|
|
|
2015-03-08 23:54:36 +01:00
|
|
|
\: place customisations above this line
|