2 # $MirOS: src/bin/mksh/dot.mkshrc,v 1.88 2014/01/11 18:09:39 tg Exp $
4 # Copyright (c) 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010,
5 # 2011, 2012, 2013, 2014
6 # Thorsten Glaser <tg@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 (including lksh) trying to shell this file
31 PS1='#'; (( USER_ID )) && PS1='$'; [[ ${HOSTNAME:=$(ulimit -c 0; hostname -s \
32 2>/dev/null)} = *([ ]|localhost) ]] && HOSTNAME=$(ulimit -c 0; hostname \
33 2>/dev/null); : ${EDITOR:=/bin/ed} ${HOSTNAME:=nil} ${TERM:=vt100}
34 : ${MKSH:=$(whence -p mksh)}; PS4='[$EPOCHREALTIME] '; PS1=$'\001\r''${|
37 (( e )) && REPLY+="$e|"
38 REPLY+=${USER:=$(ulimit -c 0; id -un 2>/dev/null || echo \?)}
39 REPLY+=@${HOSTNAME%%.*}:
41 local d=${PWD:-?} p=~; [[ $p = ?(*/) ]] || d=${d/#$p/~}
42 local m=${%d} n p=...; (( m > 0 )) || m=${#d}
43 (( m > (n = (COLUMNS/3 < 7 ? 7 : COLUMNS/3)) )) && d=${d:(-n)} || p=
47 } '"$PS1 "; export EDITOR HOSTNAME MKSH TERM USER
54 alias doch='sudo mksh -c "$(fc -ln -1)"'
55 whence -p rot13 >/dev/null || alias rot13='tr \
56 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ \
57 nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM'
58 if whence -p hd >/dev/null; then :; elif whence -p hexdump >/dev/null; then
60 hexdump -e '"%08.8_ax " 8/1 "%02X " " - " 8/1 "%02X "' \
61 -e '" |" "%_p"' -e '"|\n"' "$@"
65 local -Uui16 -Z11 pos=0
66 local -Uui16 -Z5 hv=2147483647
69 cat "$@" | { set +U; if read -arN -1 line; then
72 while (( i < ${#line[*]} )); do
74 if (( (pos & 15) == 0 )); then
75 (( pos )) && print -r -- "$dasc|"
76 print -n "${pos#16#} "
80 if (( (hv < 32) || (hv > 126) )); then
85 (( (pos++ & 15) == 7 )) && print -n -- '- '
87 while (( pos & 15 )); do
89 (( (pos++ & 15) == 7 )) && print -n -- '- '
91 (( hv == 2147483647 )) || print -r -- "$dasc|"
96 # Berkeley C shell compatible dirs, popd, and pushd functions
97 # Z shell compatible chpwd() hook, used to update DIRSTACK[0]
98 DIRSTACKBASE=$(realpath ~/. 2>/dev/null || print -nr -- "${HOME:-/}")
101 DIRSTACK[0]=$(realpath . 2>/dev/null || print -r -- "$PWD")
102 [[ $DIRSTACKBASE = ?(*/) ]] || \
103 DIRSTACK[0]=${DIRSTACK[0]/#$DIRSTACKBASE/~}
108 builtin cd "$@" || return $?
112 local d t=${1/#~/$DIRSTACKBASE}
114 if ! d=$(builtin cd "$t" 2>&1); then
115 print -u2 "${1}: ${d##*cd: $t: }."
122 local -i fl=0 fv=0 fn=0 cpos=0
124 while getopts ":lvn" d; do
129 (*) print -u2 'Usage: dirs [-lvn].'
133 shift $((OPTIND - 1))
134 if (( $# > 0 )); then
135 print -u2 'Usage: dirs [-lvn].'
140 while (( fv < ${#DIRSTACK[*]} )); do
142 (( fl )) && d=${d/#~/$DIRSTACKBASE}
148 while (( fv < ${#DIRSTACK[*]} )); do
150 (( fl )) && d=${d/#~/$DIRSTACKBASE}
151 (( dwidth = (${%d} > 0 ? ${%d} : ${#d}) ))
152 if (( fn && (cpos += dwidth + 1) >= 79 && \
155 (( cpos = dwidth + 1 ))
168 while getopts ":0123456789lvn" d; do
170 (l|v|n) fa+=" -$d" ;;
173 (*) print -u2 'Usage: popd [-lvn] [+<n>].'
177 shift $((OPTIND - n))
179 if (( $# > 1 )); then
180 print -u2 popd: Too many arguments.
182 elif [[ $1 = ++([0-9]) && $1 != +0 ]]; then
183 if (( (n = ${1#+}) >= ${#DIRSTACK[*]} )); then
184 print -u2 popd: Directory stack not that deep.
187 elif [[ -n $1 ]]; then
188 print -u2 popd: Bad directory.
191 if (( ${#DIRSTACK[*]} < 2 )); then
192 print -u2 popd: Directory stack empty.
196 set -A DIRSTACK -- "${DIRSTACK[@]}"
197 cd_csh "${DIRSTACK[0]}" || return 1
204 while getopts ":0123456789lvn" d; do
206 (l|v|n) fa+=" -$d" ;;
209 (*) print -u2 'Usage: pushd [-lvn] [<dir>|+<n>].'
213 shift $((OPTIND - n))
214 if (( $# == 0 )); then
215 if (( ${#DIRSTACK[*]} < 2 )); then
216 print -u2 pushd: No other directory.
220 DIRSTACK[1]=${DIRSTACK[0]}
221 cd_csh "$d" || return 1
222 elif (( $# > 1 )); then
223 print -u2 pushd: Too many arguments.
225 elif [[ $1 = ++([0-9]) && $1 != +0 ]]; then
226 if (( (n = ${1#+}) >= ${#DIRSTACK[*]} )); then
227 print -u2 pushd: Directory stack not that deep.
233 set -A DIRSTACK -- "${DIRSTACK[@]}" "$d"
235 cd_csh "${DIRSTACK[0]}" || return 1
237 set -A DIRSTACK -- placeholder "${DIRSTACK[@]}"
238 cd_csh "$1" || return 1
243 # pager (not control character safe)
245 local dummy line llen curlin=0
247 cat "$@" | while IFS= read -r line; do
249 (( llen == -1 )) && llen=${#line}
250 (( llen = llen ? (llen + COLUMNS - 1) / COLUMNS : 1 ))
251 if (( (curlin += llen) >= LINES )); then
252 print -n -- '\033[7m--more--\033[0m'
254 [[ $dummy = [Qq]* ]] && return 0
261 # base64 encoder and decoder, RFC compliant, NUL safe
262 function Lb64decode {
263 [[ -o utf8-mode ]]; local u=$?
266 [[ -n $s ]] || { s=$(cat; print x); s=${s%x}; }
267 local -i i=0 j=0 n=${#s} p=0 v x
270 while (( i < n )); do
274 ([A-Z]) (( v = 1#$c - 65 )) ;;
275 ([a-z]) (( v = 1#$c - 71 )) ;;
276 ([0-9]) (( v = 1#$c + 4 )) ;;
281 (( x = (x << 6) | v ))
284 (1) (( o = (x >> 4) & 255 )) ;;
285 (2) (( o = (x >> 2) & 255 )) ;;
286 (3) (( o = x & 255 ))
291 (( ++j & 4095 )) && continue
299 set -A Lb64encode_code -- 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 \
300 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 + /
301 function Lb64encode {
302 [[ -o utf8-mode ]]; local u=$?
306 read -raN-1 s <<<"$*"
311 local -i i=0 n=${#s[*]} j v
313 while (( i < n )); do
314 (( v = s[i++] << 16 ))
315 (( j = i < n ? s[i++] : 0 ))
317 (( j = i < n ? s[i++] : 0 ))
319 t+=${Lb64encode_code[v >> 18]}${Lb64encode_code[v >> 12 & 63]}
320 c=${Lb64encode_code[v >> 6 & 63]}
321 if (( i <= n )); then
322 t+=$c${Lb64encode_code[v & 63]}
323 elif (( i == n + 1 )); then
328 if (( ${#t} == 76 || i >= n )); then
336 # Better Avalanche for the Jenkins Hash
337 typeset -Z11 -Uui16 Lbafh_v
338 function Lbafh_init {
342 [[ -o utf8-mode ]]; local u=$?
346 read -raN-1 s <<<"$*"
351 local -i i=0 n=${#s[*]}
353 while (( i < n )); do
354 ((# Lbafh_v = (Lbafh_v + s[i++] + 1) * 1025 ))
355 ((# Lbafh_v ^= Lbafh_v >> 6 ))
360 function Lbafh_finish {
363 ((# t = (((Lbafh_v >> 7) & 0x01010101) * 0x1B) ^ \
364 ((Lbafh_v << 1) & 0xFEFEFEFE) ))
365 ((# Lbafh_v = t ^ (t >>> 8) ^ (Lbafh_v >>> 8) ^ \
366 (Lbafh_v >>> 16) ^ (Lbafh_v >>> 24) ))
370 # strip comments (and leading/trailing whitespace if IFS is set) from
371 # any file(s) given as argument, or stdin if none, and spew to stdout
373 cat "$@" | { set -o noglob; while read _line; do
375 [[ -n $_line ]] && print -r -- $_line
379 # give MidnightBSD's laffer1 a bit of csh feeling
381 eval export "\"$1\""'="$2"'
384 : place customisations below this line
386 for p in ~/.etc/bin ~/bin; do
387 [[ -d $p/. ]] || continue
388 [[ :$PATH: = *:$p:* ]] || PATH=$p:$PATH
391 export SHELL=$MKSH MANWIDTH=80 LESSHISTFILE=-
392 alias cls='print -n \\033c'
394 #unset LANGUAGE LC_ADDRESS LC_ALL LC_COLLATE LC_IDENTIFICATION LC_MONETARY \
395 # LC_NAME LC_NUMERIC LC_TELEPHONE LC_TIME
398 #export LANG=C LC_CTYPE=$p LC_MEASUREMENT=$p LC_MESSAGES=$p LC_PAPER=$p
402 : place customisations above this line