2 # $MirOS: src/bin/mksh/dot.mkshrc,v 1.121 2017/08/08 21:10:21 tg Exp $
4 # Copyright (c) 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010,
5 # 2011, 2012, 2013, 2014, 2015, 2016, 2017
6 # mirabilos <m@mirbsd.org>
8 # Provided that these terms and disclaimer and all copyright notices
9 # are retained or reproduced in an accompanying document, permission
10 # is granted to deal in this work without restriction, including un-
11 # limited rights to use, publicly perform, distribute, sell, modify,
12 # merge, give away, or sublicence.
14 # This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
15 # the utmost extent permitted by applicable law, neither express nor
16 # implied; without malicious intent or gross negligence. In no event
17 # may a licensor, author or contributor be held liable for indirect,
18 # direct, other damage, loss, or other issues arising in any way out
19 # of dealing in the work, even if advised of the possibility of such
20 # damage or existence of a defect, except proven that it results out
21 # of said person's immediate fault when using the work as intended.
23 # ${ENV:-~/.mkshrc}: mksh initialisation file for interactive shells
25 # catch non-mksh, non-lksh, trying to run this file
26 case ${KSH_VERSION:-} in
27 *LEGACY\ KSH*|*MIRBSD\ KSH*) ;;
31 # give MidnightBSD's laffer1 a bit of csh feeling
34 \\builtin eval '\\builtin export "$1"="${2:-}"'
40 # pager (not control character safe)
44 \\builtin trap "rv=\$?; \\\\builtin kill $! >/dev/null 2>&1; \\\\builtin exit \$rv" EXIT
45 while IFS= \\builtin read -pr line; do
47 (( llen == -1 )) && llen=${#line}
48 (( llen = llen ? (llen + COLUMNS - 1) / COLUMNS : 1 ))
49 if (( (curlin += llen) >= LINES )); then
50 \\builtin print -nr -- $'\e[7m--more--\e[0m'
51 \\builtin read -u1 || \\builtin exit $?
52 [[ $REPLY = [Qq]* ]] && \\builtin exit 0
55 \\builtin print -r -- "$line"
59 # customise your favourite editor here; the first one found is used
60 for EDITOR in "${EDITOR:-}" jupp jstar mcedit ed vi; do
61 EDITOR=$(\\builtin whence -p "$EDITOR") || EDITOR=
62 [[ -n $EDITOR && -x $EDITOR ]] && break
66 \\builtin alias ls=ls l='ls -F' la='l -a' ll='l -l' lo='l -alo'
67 \: "${HOSTNAME:=$(\\builtin ulimit -c 0; \\builtin print -r -- $(hostname \
68 2>/dev/null))}${EDITOR:=/bin/ed}${TERM:=vt100}${USER:=$(\\builtin ulimit \
69 -c 0; id -un 2>/dev/null)}${USER:=?}"
70 [[ $HOSTNAME = ?(?(ip6-)localhost?(6)) ]] && HOSTNAME=nil; \\builtin unalias ls
71 \\builtin export EDITOR HOSTNAME TERM USER
73 # minimal support for lksh users
74 if [[ $KSH_VERSION = *LEGACY\ KSH* ]]; then
75 PS1='$USER@${HOSTNAME%%.*}:$PWD>'
79 # mksh-specific from here
80 \: "${MKSH:=$(\\builtin whence -p mksh)}${MKSH:=/bin/mksh}"
84 PS4='[$EPOCHREALTIME] '; PS1='#'; (( USER_ID )) && PS1='$'; PS1=$'\001\r''${|
85 \\builtin typeset e=$?
87 (( e )) && REPLY+="$e|"
88 REPLY+=${USER}@${HOSTNAME%%.*}:
90 \\builtin typeset d=${PWD:-?}/ p=~; [[ $p = ?(*/) ]] || d=${d/#$p\//\~/}
91 d=${d%/}; \\builtin typeset m=${%d} n p=...; (( m > 0 )) || m=${#d}
92 (( m > (n = (COLUMNS/3 < 7 ? 7 : COLUMNS/3)) )) && d=${d:(-n)} || p=
99 \\builtin alias doch='sudo mksh -c "$(\\builtin fc -ln -1)"'
100 \\builtin command -v rot13 >/dev/null || \\builtin alias rot13='tr \
101 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ \
102 nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM'
103 if \\builtin command -v hd >/dev/null; then
105 elif \\builtin command -v hexdump >/dev/null; then
107 hexdump -e '"%08.8_ax " 8/1 "%02X " " - " 8/1 "%02X "' \
108 -e '" |" "%_p"' -e '"|\n"' "$@"
112 \\builtin cat "$@" | hd_mksh "$@"
116 # NUL-safe and EBCDIC-safe hexdump (from stdin)
118 \\builtin typeset -Uui16 -Z11 pos=0
119 \\builtin typeset -Uui16 -Z5 hv=2147483647
120 \\builtin typeset dasc dn line i
123 while \\builtin read -arn 512 line; do
124 \\builtin typeset -i1 'line[*]'
126 while (( i < ${#line[*]} )); do
128 (( (hv = line[i++]) != 0 )) && dn=${line[i-1]#1#}
129 if (( (pos & 15) == 0 )); then
131 \\builtin print -r -- "$dasc|"
132 \\builtin print -nr "${pos#16#} "
135 \\builtin print -nr "${hv#16#} "
136 if [[ $dn = [[:print:]] ]]; then
141 (( (pos++ & 15) == 7 )) && \
142 \\builtin print -nr -- '- '
145 while (( pos & 15 )); do
146 \\builtin print -nr ' '
147 (( (pos++ & 15) == 7 )) && \
148 \\builtin print -nr -- '- '
150 (( hv == 2147483647 )) || \\builtin print -r -- "$dasc|"
153 # Berkeley C shell compatible dirs, popd, and pushd functions
154 # Z shell compatible chpwd() hook, used to update DIRSTACK[0]
155 DIRSTACKBASE=$(\\builtin realpath ~/. 2>/dev/null || \
156 \\builtin print -nr -- "${HOME:-/}")
157 \\builtin set -A DIRSTACK
159 DIRSTACK[0]=$(\\builtin realpath . 2>/dev/null || \
160 \\builtin print -nr -- "$PWD")
161 [[ $DIRSTACKBASE = ?(*/) ]] || \
162 DIRSTACK[0]=${DIRSTACK[0]/#$DIRSTACKBASE/\~}
167 \\builtin cd "$@" || \\builtin return $?
171 \\builtin typeset d t=${1/#\~/$DIRSTACKBASE}
173 if ! d=$(\\builtin cd "$t" 2>&1); then
174 \\builtin print -ru2 "${1}: ${d##*cd: $t: }."
180 \\builtin typeset d dwidth
181 \\builtin typeset -i fl=0 fv=0 fn=0 cpos=0
183 while \\builtin getopts ":lvn" d; do
188 (*) \\builtin print -ru2 'Usage: dirs [-lvn].'
189 \\builtin return 1 ;;
192 \\builtin shift $((OPTIND - 1))
193 if (( $# > 0 )); then
194 \\builtin print -ru2 'Usage: dirs [-lvn].'
199 while (( fv < ${#DIRSTACK[*]} )); do
201 (( fl )) && d=${d/#\~/$DIRSTACKBASE}
202 \\builtin print -r -- "$fv $d"
207 while (( fv < ${#DIRSTACK[*]} )); do
209 (( fl )) && d=${d/#\~/$DIRSTACKBASE}
210 (( dwidth = (${%d} > 0 ? ${%d} : ${#d}) ))
211 if (( fn && (cpos += dwidth + 1) >= 79 && \
214 (( cpos = dwidth + 1 ))
216 \\builtin print -nr -- "$d "
224 \\builtin typeset d fa
225 \\builtin typeset -i n=1
227 while \\builtin getopts ":0123456789lvn" d; do
229 (l|v|n) fa+=" -$d" ;;
232 (*) \\builtin print -ru2 'Usage: popd [-lvn] [+<n>].'
233 \\builtin return 1 ;;
236 \\builtin shift $((OPTIND - n))
238 if (( $# > 1 )); then
239 \\builtin print -ru2 popd: Too many arguments.
241 elif [[ $1 = ++([0-9]) && $1 != +0 ]]; then
242 if (( (n = ${1#+}) >= ${#DIRSTACK[*]} )); then
243 \\builtin print -ru2 popd: Directory stack not that deep.
246 elif [[ -n $1 ]]; then
247 \\builtin print -ru2 popd: Bad directory.
250 if (( ${#DIRSTACK[*]} < 2 )); then
251 \\builtin print -ru2 popd: Directory stack empty.
254 \\builtin unset DIRSTACK[n]
255 \\builtin set -A DIRSTACK -- "${DIRSTACK[@]}"
256 \cd_csh "${DIRSTACK[0]}" || \\builtin return 1
260 \\builtin typeset d fa
261 \\builtin typeset -i n=1
263 while \\builtin getopts ":0123456789lvn" d; do
265 (l|v|n) fa+=" -$d" ;;
268 (*) \\builtin print -ru2 'Usage: pushd [-lvn] [<dir>|+<n>].'
269 \\builtin return 1 ;;
272 \\builtin shift $((OPTIND - n))
273 if (( $# == 0 )); then
274 if (( ${#DIRSTACK[*]} < 2 )); then
275 \\builtin print -ru2 pushd: No other directory.
279 DIRSTACK[1]=${DIRSTACK[0]}
280 \cd_csh "$d" || \\builtin return 1
281 elif (( $# > 1 )); then
282 \\builtin print -ru2 pushd: Too many arguments.
284 elif [[ $1 = ++([0-9]) && $1 != +0 ]]; then
285 if (( (n = ${1#+}) >= ${#DIRSTACK[*]} )); then
286 \\builtin print -ru2 pushd: Directory stack not that deep.
291 \\builtin unset DIRSTACK[0]
292 \\builtin set -A DIRSTACK -- "${DIRSTACK[@]}" "$d"
294 \cd_csh "${DIRSTACK[0]}" || \\builtin return 1
296 \\builtin set -A DIRSTACK -- placeholder "${DIRSTACK[@]}"
297 \cd_csh "$1" || \\builtin return 1
302 # base64 encoder and decoder, RFC compliant, NUL safe, not EBCDIC safe
303 function Lb64decode {
305 \\builtin typeset c s="$*" t
306 [[ -n $s ]] || { s=$(\\builtin cat; \\builtin print x); s=${s%x}; }
307 \\builtin typeset -i i=0 j=0 n=${#s} p=0 v x
308 \\builtin typeset -i16 o
310 while (( i < n )); do
313 (=) \\builtin break ;;
314 ([A-Z]) (( v = 1#$c - 65 )) ;;
315 ([a-z]) (( v = 1#$c - 71 )) ;;
316 ([0-9]) (( v = 1#$c + 4 )) ;;
319 (*) \\builtin continue ;;
321 (( x = (x << 6) | v ))
323 (0) \\builtin continue ;;
324 (1) (( o = (x >> 4) & 255 )) ;;
325 (2) (( o = (x >> 2) & 255 )) ;;
326 (3) (( o = x & 255 ))
331 (( ++j & 4095 )) && \\builtin continue
332 \\builtin print -n $t
335 \\builtin print -n $t
337 function Lb64encode {
339 \\builtin typeset c s t table
340 \\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 \
341 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 + /
343 \\builtin read -raN-1 s <<<"$*"
344 \\builtin unset s[${#s[*]}-1]
346 \\builtin read -raN-1 s
348 \\builtin typeset -i i=0 n=${#s[*]} v
350 while (( i < n )); do
351 (( v = s[i++] << 16 ))
352 (( v |= s[i++] << 8 ))
354 t+=${table[v >> 18]}${table[v >> 12 & 63]}
355 c=${table[v >> 6 & 63]}
356 if (( i <= n )); then
357 t+=$c${table[v & 63]}
358 elif (( i == n + 1 )); then
363 if (( ${#t} == 76 || i >= n )); then
364 \\builtin print -r $t
370 # Better Avalanche for the Jenkins Hash
371 \\builtin typeset -Z11 -Uui16 Lbafh_v
372 function Lbafh_init {
379 \\builtin read -raN-1 s <<<"$*"
380 \\builtin unset s[${#s[*]}-1]
382 \\builtin read -raN-1 s
384 \\builtin typeset -i i=0 n=${#s[*]}
386 while (( i < n )); do
387 ((# Lbafh_v = (Lbafh_v + s[i++] + 1) * 1025 ))
388 ((# Lbafh_v ^= Lbafh_v >> 6 ))
391 function Lbafh_finish {
392 \\builtin typeset -Ui t
394 ((# t = (((Lbafh_v >> 7) & 0x01010101) * 0x1B) ^ \
395 ((Lbafh_v << 1) & 0xFEFEFEFE) ))
396 ((# Lbafh_v = t ^ (t ^> 8) ^ (Lbafh_v ^> 8) ^ \
397 (Lbafh_v ^> 16) ^ (Lbafh_v ^> 24) ))
401 # strip comments (and leading/trailing whitespace if IFS is set) from
402 # any file(s) given as argument, or stdin if none, and spew to stdout
404 \\builtin set -o noglob
405 \\builtin cat "$@" | while \\builtin read _line; do
407 [[ -n $_line ]] && \\builtin print -r -- $_line
411 # toggle built-in aliases and utilities, and aliases and functions from mkshrc
413 \\builtin typeset doprnt=0 mode=1 x y z rv=0
414 \\builtin typeset b_alias i_alias i_func nalias=0 nfunc=0 i_all
415 \\builtin set -A b_alias
416 \\builtin set -A i_alias
417 \\builtin set -A i_func
419 # accumulate mksh built-in aliases, in ASCIIbetical order
420 i_alias[nalias]=autoload; b_alias[nalias++]='\\builtin typeset -fu'
421 i_alias[nalias]=functions; b_alias[nalias++]='\\builtin typeset -f'
422 i_alias[nalias]=hash; b_alias[nalias++]='\\builtin alias -t'
423 i_alias[nalias]=history; b_alias[nalias++]='\\builtin fc -l'
424 i_alias[nalias]=integer; b_alias[nalias++]='\\builtin typeset -i'
425 i_alias[nalias]=local; b_alias[nalias++]='\\builtin typeset'
426 i_alias[nalias]=login; b_alias[nalias++]='\\builtin exec login'
427 i_alias[nalias]=nameref; b_alias[nalias++]='\\builtin typeset -n'
428 i_alias[nalias]=nohup; b_alias[nalias++]='nohup '
429 i_alias[nalias]=r; b_alias[nalias++]='\\builtin fc -e -'
430 i_alias[nalias]=type; b_alias[nalias++]='\\builtin whence -v'
432 # accumulate mksh built-in utilities, in definition order, even ifndef
436 i_func[nfunc++]=alias
437 i_func[nfunc++]=break
438 # \\builtin cannot, by design, be overridden
439 i_func[nfunc++]=builtin
442 i_func[nfunc++]=chdir
443 i_func[nfunc++]=command
444 i_func[nfunc++]=continue
449 i_func[nfunc++]=export
450 i_func[nfunc++]=false
452 i_func[nfunc++]=getopts
453 i_func[nfunc++]=global
457 i_func[nfunc++]=print
460 i_func[nfunc++]=readonly
461 i_func[nfunc++]=realpath
462 i_func[nfunc++]=rename
463 i_func[nfunc++]=return
465 i_func[nfunc++]=shift
466 i_func[nfunc++]=source
467 i_func[nfunc++]=suspend
469 i_func[nfunc++]=times
472 i_func[nfunc++]=typeset
473 i_func[nfunc++]=ulimit
474 i_func[nfunc++]=umask
475 i_func[nfunc++]=unalias
476 i_func[nfunc++]=unset
478 i_func[nfunc++]=whence
482 i_func[nfunc++]=mknod
483 i_func[nfunc++]=printf
484 i_func[nfunc++]=sleep
485 i_func[nfunc++]=domainname
486 i_func[nfunc++]=extproc
488 # accumulate aliases from dot.mkshrc, in definition order
489 i_alias[nalias]=l; b_alias[nalias++]='ls -F'
490 i_alias[nalias]=la; b_alias[nalias++]='l -a'
491 i_alias[nalias]=ll; b_alias[nalias++]='l -l'
492 i_alias[nalias]=lo; b_alias[nalias++]='l -alo'
493 i_alias[nalias]=doch; b_alias[nalias++]='sudo mksh -c "$(\\builtin fc -ln -1)"'
494 i_alias[nalias]=rot13; b_alias[nalias++]='tr abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM'
495 i_alias[nalias]=cls; b_alias[nalias++]='\\builtin print -n \\ec'
497 # accumulate functions from dot.mkshrc, in definition order
498 i_func[nfunc++]=setenv
499 i_func[nfunc++]=smores
501 i_func[nfunc++]=hd_mksh
502 i_func[nfunc++]=chpwd
504 i_func[nfunc++]=cd_csh
507 i_func[nfunc++]=pushd
508 i_func[nfunc++]=Lb64decode
509 i_func[nfunc++]=Lb64encode
510 i_func[nfunc++]=Lbafh_init
511 i_func[nfunc++]=Lbafh_add
512 i_func[nfunc++]=Lbafh_finish
513 i_func[nfunc++]=Lstripcom
514 i_func[nfunc++]=enable
516 # collect all identifiers, sorted ASCIIbetically
517 \\builtin set -sA i_all -- "${i_alias[@]}" "${i_func[@]}"
519 # handle options, we don't do dynamic loading
520 while \\builtin getopts "adf:nps" x; do
526 # deliberately causing an error, like bash-static
529 \\builtin print -ru2 enable: dynamic loading not available
539 \\builtin set -sA i_all -- . : break continue eval \
540 exec exit export readonly return set shift times \
544 \\builtin print -ru2 enable: usage: \
545 "enable [-adnps] [-f filename] [name ...]"
550 \\builtin shift $((OPTIND - 1))
552 # display builtins enabled/disabled/all/special?
553 if (( doprnt || ($# == 0) )); then
554 for x in "${i_all[@]}"; do
555 y=$(\\builtin alias "$x") || y=
556 [[ $y = "$x='\\\\builtin whence -p $x >/dev/null || (\\\\builtin print -r mksh: $x: not found; \\\\builtin exit 127) && \$(\\\\builtin whence -p $x)'" ]]; z=$?
559 \\builtin print -r -- "enable -n $x"
562 \\builtin print -r -- "enable $x"
571 for y in "${i_alias[@]}" "${i_func[@]}"; do
572 [[ $x = "$y" ]] || \\builtin continue
577 \\builtin print -ru2 enable: "$x": not a shell builtin
583 \\builtin alias "$x=\\\\builtin whence -p $x >/dev/null || (\\\\builtin print -r mksh: $x: not found; \\\\builtin exit 127) && \$(\\\\builtin whence -p $x)"
585 # find out if this is an alias or not, first
588 while (( ++y < nalias )); do
589 [[ $x = "${i_alias[y]}" ]] || \\builtin continue
594 # re-enable the original alias body
595 \\builtin alias "$x=${b_alias[y]}"
597 # re-enable the original utility/function
598 \\builtin unalias "$x"
605 \: place customisations below this line
607 # some defaults follow — you are supposed to adjust these to your
608 # liking; by default we add ~/.etc/bin and ~/bin (whichever exist)
609 # to $PATH, set $SHELL to mksh, set some defaults for man and less
610 # and show a few more possible things for users to begin moving in
612 for p in ~/.etc/bin ~/bin; do
613 [[ -d $p/. ]] || \\builtin continue
614 [[ $PATHSEP$PATH$PATHSEP = *"$PATHSEP$p$PATHSEP"* ]] || \
618 \\builtin export SHELL=$MKSH MANWIDTH=80 LESSHISTFILE=-
619 \\builtin alias cls='\\builtin print -n \\ec'
621 #\\builtin unset LANGUAGE LC_ADDRESS LC_ALL LC_COLLATE LC_IDENTIFICATION LC_MONETARY \
622 # LC_NAME LC_NUMERIC LC_TELEPHONE LC_TIME
624 #\\builtin export LANG=C LC_CTYPE=$p LC_MEASUREMENT=$p LC_MESSAGES=$p LC_PAPER=$p
629 \: place customisations above this line