2 # $MirOS: src/bin/mksh/dot.mkshrc,v 1.114 2017/03/19 22:31:26 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 \\builtin alias ls=ls l='ls -F' la='l -a' ll='l -l' lo='l -alo'
60 \: "${HOSTNAME:=$(\\builtin ulimit -c 0; \\builtin print -r -- $(hostname \
61 2>/dev/null))}${EDITOR:=/bin/ed}${TERM:=vt100}${USER:=$(\\builtin ulimit \
62 -c 0; id -un 2>/dev/null)}${USER:=?}"
63 [[ $HOSTNAME = ?(?(ip6-)localhost?(6)) ]] && HOSTNAME=nil; \\builtin unalias ls
64 \\builtin export EDITOR HOSTNAME TERM USER
66 # minimal support for lksh users
67 if [[ $KSH_VERSION = *LEGACY\ KSH* ]]; then
68 PS1='$USER@${HOSTNAME%%.*}:$PWD>'
72 # mksh-specific from here
73 \: "${MKSH:=$(\\builtin whence -p mksh)}${MKSH:=/bin/mksh}"
76 PS4='[$EPOCHREALTIME] '; PS1='#'; (( USER_ID )) && PS1='$'; PS1=$'\001\r''${|
77 \\builtin typeset e=$?
79 (( e )) && REPLY+="$e|"
80 REPLY+=${USER}@${HOSTNAME%%.*}:
82 \\builtin typeset d=${PWD:-?}/ p=~; [[ $p = ?(*/) ]] || d=${d/#$p\//\~/}
83 d=${d%/}; \\builtin typeset m=${%d} n p=...; (( m > 0 )) || m=${#d}
84 (( m > (n = (COLUMNS/3 < 7 ? 7 : COLUMNS/3)) )) && d=${d:(-n)} || p=
89 \\builtin alias doch='sudo mksh -c "$(\\builtin fc -ln -1)"'
90 \\builtin command -v rot13 >/dev/null || \\builtin alias rot13='tr \
91 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ \
92 nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM'
93 if \\builtin command -v hd >/dev/null; then
95 elif \\builtin command -v hexdump >/dev/null; then
97 hexdump -e '"%08.8_ax " 8/1 "%02X " " - " 8/1 "%02X "' \
98 -e '" |" "%_p"' -e '"|\n"' "$@"
102 \\builtin typeset -Uui16 -Z11 pos=0
103 \\builtin typeset -Uui16 -Z5 hv=2147483647
104 \\builtin typeset dasc line i
107 \\builtin cat "$@" | if \\builtin read -arN -1 line; then
108 \\builtin typeset -i1 'line[*]'
110 while (( i < ${#line[*]} )); do
112 if (( (pos & 15) == 0 )); then
114 \\builtin print -r -- "$dasc|"
115 \\builtin print -nr "${pos#16#} "
118 \\builtin print -nr "${hv#16#} "
119 #XXX EBCDIC, but we need [[:print:]] to fix this
120 if (( (hv < 32) || (hv > 126) )); then
123 dasc+=${line[i-1]#1#}
125 (( (pos++ & 15) == 7 )) && \
126 \\builtin print -nr -- '- '
128 while (( pos & 15 )); do
129 \\builtin print -nr ' '
130 (( (pos++ & 15) == 7 )) && \
131 \\builtin print -nr -- '- '
133 (( hv == 2147483647 )) || \\builtin print -r -- "$dasc|"
138 # Berkeley C shell compatible dirs, popd, and pushd functions
139 # Z shell compatible chpwd() hook, used to update DIRSTACK[0]
140 DIRSTACKBASE=$(\\builtin realpath ~/. 2>/dev/null || \
141 \\builtin print -nr -- "${HOME:-/}")
142 \\builtin set -A DIRSTACK
144 DIRSTACK[0]=$(\\builtin realpath . 2>/dev/null || \
145 \\builtin print -nr -- "$PWD")
146 [[ $DIRSTACKBASE = ?(*/) ]] || \
147 DIRSTACK[0]=${DIRSTACK[0]/#$DIRSTACKBASE/\~}
152 \\builtin cd "$@" || \\builtin return $?
156 \\builtin typeset d t=${1/#\~/$DIRSTACKBASE}
158 if ! d=$(\\builtin cd "$t" 2>&1); then
159 \\builtin print -ru2 "${1}: ${d##*cd: $t: }."
165 \\builtin typeset d dwidth
166 \\builtin typeset -i fl=0 fv=0 fn=0 cpos=0
168 while \\builtin getopts ":lvn" d; do
173 (*) \\builtin print -ru2 'Usage: dirs [-lvn].'
174 \\builtin return 1 ;;
177 \\builtin shift $((OPTIND - 1))
178 if (( $# > 0 )); then
179 \\builtin print -ru2 'Usage: dirs [-lvn].'
184 while (( fv < ${#DIRSTACK[*]} )); do
186 (( fl )) && d=${d/#\~/$DIRSTACKBASE}
187 \\builtin print -r -- "$fv $d"
192 while (( fv < ${#DIRSTACK[*]} )); do
194 (( fl )) && d=${d/#\~/$DIRSTACKBASE}
195 (( dwidth = (${%d} > 0 ? ${%d} : ${#d}) ))
196 if (( fn && (cpos += dwidth + 1) >= 79 && \
199 (( cpos = dwidth + 1 ))
201 \\builtin print -nr -- "$d "
209 \\builtin typeset d fa
210 \\builtin typeset -i n=1
212 while \\builtin getopts ":0123456789lvn" d; do
214 (l|v|n) fa+=" -$d" ;;
217 (*) \\builtin print -ru2 'Usage: popd [-lvn] [+<n>].'
218 \\builtin return 1 ;;
221 \\builtin shift $((OPTIND - n))
223 if (( $# > 1 )); then
224 \\builtin print -ru2 popd: Too many arguments.
226 elif [[ $1 = ++([0-9]) && $1 != +0 ]]; then
227 if (( (n = ${1#+}) >= ${#DIRSTACK[*]} )); then
228 \\builtin print -ru2 popd: Directory stack not that deep.
231 elif [[ -n $1 ]]; then
232 \\builtin print -ru2 popd: Bad directory.
235 if (( ${#DIRSTACK[*]} < 2 )); then
236 \\builtin print -ru2 popd: Directory stack empty.
239 \\builtin unset DIRSTACK[n]
240 \\builtin set -A DIRSTACK -- "${DIRSTACK[@]}"
241 \cd_csh "${DIRSTACK[0]}" || \\builtin return 1
245 \\builtin typeset d fa
246 \\builtin typeset -i n=1
248 while \\builtin getopts ":0123456789lvn" d; do
250 (l|v|n) fa+=" -$d" ;;
253 (*) \\builtin print -ru2 'Usage: pushd [-lvn] [<dir>|+<n>].'
254 \\builtin return 1 ;;
257 \\builtin shift $((OPTIND - n))
258 if (( $# == 0 )); then
259 if (( ${#DIRSTACK[*]} < 2 )); then
260 \\builtin print -ru2 pushd: No other directory.
264 DIRSTACK[1]=${DIRSTACK[0]}
265 \cd_csh "$d" || \\builtin return 1
266 elif (( $# > 1 )); then
267 \\builtin print -ru2 pushd: Too many arguments.
269 elif [[ $1 = ++([0-9]) && $1 != +0 ]]; then
270 if (( (n = ${1#+}) >= ${#DIRSTACK[*]} )); then
271 \\builtin print -ru2 pushd: Directory stack not that deep.
276 \\builtin unset DIRSTACK[0]
277 \\builtin set -A DIRSTACK -- "${DIRSTACK[@]}" "$d"
279 \cd_csh "${DIRSTACK[0]}" || \\builtin return 1
281 \\builtin set -A DIRSTACK -- placeholder "${DIRSTACK[@]}"
282 \cd_csh "$1" || \\builtin return 1
287 # base64 encoder and decoder, RFC compliant, NUL safe, not EBCDIC safe
288 function Lb64decode {
290 \\builtin typeset c s="$*" t
291 [[ -n $s ]] || { s=$(\\builtin cat; \\builtin print x); s=${s%x}; }
292 \\builtin typeset -i i=0 j=0 n=${#s} p=0 v x
293 \\builtin typeset -i16 o
295 while (( i < n )); do
298 (=) \\builtin break ;;
299 ([A-Z]) (( v = 1#$c - 65 )) ;;
300 ([a-z]) (( v = 1#$c - 71 )) ;;
301 ([0-9]) (( v = 1#$c + 4 )) ;;
304 (*) \\builtin continue ;;
306 (( x = (x << 6) | v ))
308 (0) \\builtin continue ;;
309 (1) (( o = (x >> 4) & 255 )) ;;
310 (2) (( o = (x >> 2) & 255 )) ;;
311 (3) (( o = x & 255 ))
316 (( ++j & 4095 )) && \\builtin continue
317 \\builtin print -n $t
320 \\builtin print -n $t
322 function Lb64encode {
324 \\builtin typeset c s t table
325 \\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 \
326 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 + /
328 \\builtin read -raN-1 s <<<"$*"
329 \\builtin unset s[${#s[*]}-1]
331 \\builtin read -raN-1 s
333 \\builtin typeset -i i=0 n=${#s[*]} v
335 while (( i < n )); do
336 (( v = s[i++] << 16 ))
337 (( v |= s[i++] << 8 ))
339 t+=${table[v >> 18]}${table[v >> 12 & 63]}
340 c=${table[v >> 6 & 63]}
341 if (( i <= n )); then
342 t+=$c${table[v & 63]}
343 elif (( i == n + 1 )); then
348 if (( ${#t} == 76 || i >= n )); then
349 \\builtin print -r $t
355 # Better Avalanche for the Jenkins Hash
356 \\builtin typeset -Z11 -Uui16 Lbafh_v
357 function Lbafh_init {
364 \\builtin read -raN-1 s <<<"$*"
365 \\builtin unset s[${#s[*]}-1]
367 \\builtin read -raN-1 s
369 \\builtin typeset -i i=0 n=${#s[*]}
371 while (( i < n )); do
372 ((# Lbafh_v = (Lbafh_v + s[i++] + 1) * 1025 ))
373 ((# Lbafh_v ^= Lbafh_v >> 6 ))
376 function Lbafh_finish {
377 \\builtin typeset -Ui t
379 ((# t = (((Lbafh_v >> 7) & 0x01010101) * 0x1B) ^ \
380 ((Lbafh_v << 1) & 0xFEFEFEFE) ))
381 ((# Lbafh_v = t ^ (t ^> 8) ^ (Lbafh_v ^> 8) ^ \
382 (Lbafh_v ^> 16) ^ (Lbafh_v ^> 24) ))
386 # strip comments (and leading/trailing whitespace if IFS is set) from
387 # any file(s) given as argument, or stdin if none, and spew to stdout
389 \\builtin set -o noglob
390 \\builtin cat "$@" | while \\builtin read _line; do
392 [[ -n $_line ]] && \\builtin print -r -- $_line
396 # toggle built-in aliases and utilities, and aliases and functions from mkshrc
398 \\builtin typeset doprnt=0 mode=1 x y z rv=0
399 \\builtin typeset b_alias i_alias i_func nalias=0 nfunc=0 i_all
400 \\builtin set -A b_alias
401 \\builtin set -A i_alias
402 \\builtin set -A i_func
404 # accumulate mksh built-in aliases, in ASCIIbetical order
405 i_alias[nalias]=autoload; b_alias[nalias++]='\\builtin typeset -fu'
406 i_alias[nalias]=functions; b_alias[nalias++]='\\builtin typeset -f'
407 i_alias[nalias]=hash; b_alias[nalias++]='\\builtin alias -t'
408 i_alias[nalias]=history; b_alias[nalias++]='\\builtin fc -l'
409 i_alias[nalias]=integer; b_alias[nalias++]='\\builtin typeset -i'
410 i_alias[nalias]=local; b_alias[nalias++]='\\builtin typeset'
411 i_alias[nalias]=login; b_alias[nalias++]='\\builtin exec login'
412 i_alias[nalias]=nameref; b_alias[nalias++]='\\builtin typeset -n'
413 i_alias[nalias]=nohup; b_alias[nalias++]='nohup '
414 i_alias[nalias]=r; b_alias[nalias++]='\\builtin fc -e -'
415 i_alias[nalias]=type; b_alias[nalias++]='\\builtin whence -v'
417 # accumulate mksh built-in utilities, in definition order, even ifndef
421 i_func[nfunc++]=alias
422 i_func[nfunc++]=break
423 # \\builtin cannot, by design, be overridden
424 i_func[nfunc++]=builtin
427 i_func[nfunc++]=chdir
428 i_func[nfunc++]=command
429 i_func[nfunc++]=continue
434 i_func[nfunc++]=export
435 i_func[nfunc++]=false
437 i_func[nfunc++]=getopts
438 i_func[nfunc++]=global
442 i_func[nfunc++]=print
445 i_func[nfunc++]=readonly
446 i_func[nfunc++]=realpath
447 i_func[nfunc++]=rename
448 i_func[nfunc++]=return
450 i_func[nfunc++]=shift
451 i_func[nfunc++]=source
452 i_func[nfunc++]=suspend
454 i_func[nfunc++]=times
457 i_func[nfunc++]=typeset
458 i_func[nfunc++]=ulimit
459 i_func[nfunc++]=umask
460 i_func[nfunc++]=unalias
461 i_func[nfunc++]=unset
463 i_func[nfunc++]=whence
467 i_func[nfunc++]=mknod
468 i_func[nfunc++]=printf
469 i_func[nfunc++]=sleep
470 i_func[nfunc++]=domainname
471 i_func[nfunc++]=extproc
473 # accumulate aliases from dot.mkshrc, in definition order
474 i_alias[nalias]=l; b_alias[nalias++]='ls -F'
475 i_alias[nalias]=la; b_alias[nalias++]='l -a'
476 i_alias[nalias]=ll; b_alias[nalias++]='l -l'
477 i_alias[nalias]=lo; b_alias[nalias++]='l -alo'
478 i_alias[nalias]=doch; b_alias[nalias++]='sudo mksh -c "$(\\builtin fc -ln -1)"'
479 i_alias[nalias]=rot13; b_alias[nalias++]='tr abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM'
480 i_alias[nalias]=cls; b_alias[nalias++]='\\builtin print -n \\ec'
482 # accumulate functions from dot.mkshrc, in definition order
483 i_func[nfunc++]=setenv
484 i_func[nfunc++]=smores
486 i_func[nfunc++]=chpwd
488 i_func[nfunc++]=cd_csh
491 i_func[nfunc++]=pushd
492 i_func[nfunc++]=Lb64decode
493 i_func[nfunc++]=Lb64encode
494 i_func[nfunc++]=Lbafh_init
495 i_func[nfunc++]=Lbafh_add
496 i_func[nfunc++]=Lbafh_finish
497 i_func[nfunc++]=Lstripcom
498 i_func[nfunc++]=enable
500 # collect all identifiers, sorted ASCIIbetically
501 \\builtin set -sA i_all -- "${i_alias[@]}" "${i_func[@]}"
503 # handle options, we don't do dynamic loading
504 while \\builtin getopts "adf:nps" x; do
510 # deliberately causing an error, like bash-static
513 \\builtin print -ru2 enable: dynamic loading not available
523 \\builtin set -sA i_all -- . : break continue eval \
524 exec exit export readonly return set shift times \
528 \\builtin print -ru2 enable: usage: \
529 "enable [-adnps] [-f filename] [name ...]"
534 \\builtin shift $((OPTIND - 1))
536 # display builtins enabled/disabled/all/special?
537 if (( doprnt || ($# == 0) )); then
538 for x in "${i_all[@]}"; do
539 y=$(\\builtin alias "$x") || y=
540 [[ $y = "$x='\\\\builtin whence -p $x >/dev/null || (\\\\builtin print -r mksh: $x: not found; \\\\builtin exit 127) && \$(\\\\builtin whence -p $x)'" ]]; z=$?
543 \\builtin print -r -- "enable -n $x"
546 \\builtin print -r -- "enable $x"
555 for y in "${i_alias[@]}" "${i_func[@]}"; do
556 [[ $x = "$y" ]] || \\builtin continue
561 \\builtin print -ru2 enable: "$x": not a shell builtin
567 \\builtin alias "$x=\\\\builtin whence -p $x >/dev/null || (\\\\builtin print -r mksh: $x: not found; \\\\builtin exit 127) && \$(\\\\builtin whence -p $x)"
569 # find out if this is an alias or not, first
572 while (( ++y < nalias )); do
573 [[ $x = "${i_alias[y]}" ]] || \\builtin continue
578 # re-enable the original alias body
579 \\builtin alias "$x=${b_alias[y]}"
581 # re-enable the original utility/function
582 \\builtin unalias "$x"
589 \: place customisations below this line
591 for p in ~/.etc/bin ~/bin; do
592 [[ -d $p/. ]] || \\builtin continue
593 [[ $PATHSEP$PATH$PATHSEP = *"$PATHSEP$p$PATHSEP"* ]] || \
597 \\builtin export SHELL=$MKSH MANWIDTH=80 LESSHISTFILE=-
598 \\builtin alias cls='\\builtin print -n \\ec'
600 #\\builtin unset LANGUAGE LC_ADDRESS LC_ALL LC_COLLATE LC_IDENTIFICATION LC_MONETARY \
601 # LC_NAME LC_NUMERIC LC_TELEPHONE LC_TIME
603 #\\builtin export LANG=C LC_CTYPE=$p LC_MEASUREMENT=$p LC_MESSAGES=$p LC_PAPER=$p
608 \: place customisations above this line