Home | History | Annotate | Download | only in src
      1 # $Id$
      2 # $MirOS: src/bin/mksh/dot.mkshrc,v 1.84 2013/08/10 13:43:50 tg Exp $
      3 #-
      4 # Copyright (c) 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010,
      5 #		2011, 2012, 2013
      6 #	Thorsten Glaser <tg (a] mirbsd.org>
      7 #
      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.
     13 #
     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.
     22 #-
     23 # ${ENV:-~/.mkshrc}: mksh initialisation file for interactive shells
     24 
     25 # catch non-mksh (including lksh) trying to shell this file
     26 case $KSH_VERSION in
     27 *MIRBSD\ KSH*) ;;
     28 *) return 0 ;;
     29 esac
     30 
     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''${|
     35 	local e=$?
     36 
     37 	(( e )) && REPLY+="$e|"
     38 	REPLY+=${USER:=$(ulimit -c 0; id -un 2>/dev/null || echo \?)}
     39 	REPLY+=@${HOSTNAME%%.*}:
     40 
     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=
     44 	REPLY+=$p$d
     45 
     46 	return $e
     47 } '"$PS1 "; export EDITOR HOSTNAME MKSH TERM USER
     48 alias ls=ls
     49 unalias ls
     50 alias l='ls -F'
     51 alias la='l -a'
     52 alias ll='l -l'
     53 alias lo='l -alo'
     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
     59 	function hd {
     60 		hexdump -e '"%08.8_ax  " 8/1 "%02X " " - " 8/1 "%02X "' \
     61 		    -e '"  |" "%_p"' -e '"|\n"' "$@"
     62 	}
     63 else
     64 	function hd {
     65 		local -Uui16 -Z11 pos=0
     66 		local -Uui16 -Z5 hv=2147483647
     67 		local dasc line i
     68 
     69 		cat "$@" | { set +U; if read -arN -1 line; then
     70 			typeset -i1 line
     71 			i=0
     72 			while (( i < ${#line[*]} )); do
     73 				hv=${line[i++]}
     74 				if (( (pos & 15) == 0 )); then
     75 					(( pos )) && print -r -- "$dasc|"
     76 					print -n "${pos#16#}  "
     77 					dasc=' |'
     78 				fi
     79 				print -n "${hv#16#} "
     80 				if (( (hv < 32) || (hv > 126) )); then
     81 					dasc+=.
     82 				else
     83 					dasc+=${line[i-1]#1#}
     84 				fi
     85 				(( (pos++ & 15) == 7 )) && print -n -- '- '
     86 			done
     87 			while (( pos & 15 )); do
     88 				print -n '   '
     89 				(( (pos++ & 15) == 7 )) && print -n -- '- '
     90 			done
     91 			(( hv == 2147483647 )) || print -r -- "$dasc|"
     92 		fi; }
     93 	}
     94 fi
     95 
     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:-/}")
     99 set -A DIRSTACK
    100 function chpwd {
    101 	DIRSTACK[0]=$(realpath . 2>/dev/null || print -r -- "$PWD")
    102 	[[ $DIRSTACKBASE = ?(*/) ]] || \
    103 	    DIRSTACK[0]=${DIRSTACK[0]/#$DIRSTACKBASE/~}
    104 	:
    105 }
    106 chpwd .
    107 function cd {
    108 	builtin cd "$@"
    109 	chpwd "$@"
    110 }
    111 function cd_csh {
    112 	local d t=${1/#~/$DIRSTACKBASE}
    113 
    114 	if ! d=$(builtin cd "$t" 2>&1); then
    115 		print -u2 "${1}: ${d##*$t - }."
    116 		return 1
    117 	fi
    118 	cd "$t"
    119 }
    120 function dirs {
    121 	local d dwidth
    122 	local -i fl=0 fv=0 fn=0 cpos=0
    123 
    124 	while getopts ":lvn" d; do
    125 		case $d {
    126 		(l)	fl=1 ;;
    127 		(v)	fv=1 ;;
    128 		(n)	fn=1 ;;
    129 		(*)	print -u2 'Usage: dirs [-lvn].'
    130 			return 1 ;;
    131 		}
    132 	done
    133 	shift $((OPTIND - 1))
    134 	if (( $# > 0 )); then
    135 		print -u2 'Usage: dirs [-lvn].'
    136 		return 1
    137 	fi
    138 	if (( fv )); then
    139 		fv=0
    140 		while (( fv < ${#DIRSTACK[*]} )); do
    141 			d=${DIRSTACK[fv]}
    142 			(( fl )) && d=${d/#~/$DIRSTACKBASE}
    143 			print -r -- "$fv	$d"
    144 			let fv++
    145 		done
    146 	else
    147 		fv=0
    148 		while (( fv < ${#DIRSTACK[*]} )); do
    149 			d=${DIRSTACK[fv]}
    150 			(( fl )) && d=${d/#~/$DIRSTACKBASE}
    151 			(( dwidth = (${%d} > 0 ? ${%d} : ${#d}) ))
    152 			if (( fn && (cpos += dwidth + 1) >= 79 && \
    153 			    dwidth < 80 )); then
    154 				print
    155 				(( cpos = dwidth + 1 ))
    156 			fi
    157 			print -nr -- "$d "
    158 			let fv++
    159 		done
    160 		print
    161 	fi
    162 	return 0
    163 }
    164 function popd {
    165 	local d fa
    166 	local -i n=1
    167 
    168 	while getopts ":0123456789lvn" d; do
    169 		case $d {
    170 		(l|v|n)	fa+=" -$d" ;;
    171 		(+*)	n=2
    172 			break ;;
    173 		(*)	print -u2 'Usage: popd [-lvn] [+<n>].'
    174 			return 1 ;;
    175 		}
    176 	done
    177 	shift $((OPTIND - n))
    178 	n=0
    179 	if (( $# > 1 )); then
    180 		print -u2 popd: Too many arguments.
    181 		return 1
    182 	elif [[ $1 = ++([0-9]) && $1 != +0 ]]; then
    183 		if (( (n = ${1#+}) >= ${#DIRSTACK[*]} )); then
    184 			print -u2 popd: Directory stack not that deep.
    185 			return 1
    186 		fi
    187 	elif [[ -n $1 ]]; then
    188 		print -u2 popd: Bad directory.
    189 		return 1
    190 	fi
    191 	if (( ${#DIRSTACK[*]} < 2 )); then
    192 		print -u2 popd: Directory stack empty.
    193 		return 1
    194 	fi
    195 	unset DIRSTACK[n]
    196 	set -A DIRSTACK -- "${DIRSTACK[@]}"
    197 	cd_csh "${DIRSTACK[0]}" || return 1
    198 	dirs $fa
    199 }
    200 function pushd {
    201 	local d fa
    202 	local -i n=1
    203 
    204 	while getopts ":0123456789lvn" d; do
    205 		case $d {
    206 		(l|v|n)	fa+=" -$d" ;;
    207 		(+*)	n=2
    208 			break ;;
    209 		(*)	print -u2 'Usage: pushd [-lvn] [<dir>|+<n>].'
    210 			return 1 ;;
    211 		}
    212 	done
    213 	shift $((OPTIND - n))
    214 	if (( $# == 0 )); then
    215 		if (( ${#DIRSTACK[*]} < 2 )); then
    216 			print -u2 pushd: No other directory.
    217 			return 1
    218 		fi
    219 		d=${DIRSTACK[1]}
    220 		DIRSTACK[1]=${DIRSTACK[0]}
    221 		cd_csh "$d" || return 1
    222 	elif (( $# > 1 )); then
    223 		print -u2 pushd: Too many arguments.
    224 		return 1
    225 	elif [[ $1 = ++([0-9]) && $1 != +0 ]]; then
    226 		if (( (n = ${1#+}) >= ${#DIRSTACK[*]} )); then
    227 			print -u2 pushd: Directory stack not that deep.
    228 			return 1
    229 		fi
    230 		while (( n-- )); do
    231 			d=${DIRSTACK[0]}
    232 			unset DIRSTACK[0]
    233 			set -A DIRSTACK -- "${DIRSTACK[@]}" "$d"
    234 		done
    235 		cd_csh "${DIRSTACK[0]}" || return 1
    236 	else
    237 		set -A DIRSTACK -- placeholder "${DIRSTACK[@]}"
    238 		cd_csh "$1" || return 1
    239 	fi
    240 	dirs $fa
    241 }
    242 
    243 # pager (not control character safe)
    244 function smores {
    245 	local dummy line llen curlin=0
    246 
    247 	cat "$@" | while IFS= read -r line; do
    248 		llen=${%line}
    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'
    253 			read -u1 dummy
    254 			[[ $dummy = [Qq]* ]] && return 0
    255 			curlin=$llen
    256 		fi
    257 		print -r -- "$line"
    258 	done
    259 }
    260 
    261 # base64 encoder and decoder, RFC compliant, NUL safe
    262 function Lb64decode {
    263 	[[ -o utf8-mode ]]; local u=$?
    264 	set +U
    265 	local c s="$*" t=
    266 	[[ -n $s ]] || { s=$(cat; print x); s=${s%x}; }
    267 	local -i i=0 j=0 n=${#s} p=0 v x
    268 	local -i16 o
    269 
    270 	while (( i < n )); do
    271 		c=${s:(i++):1}
    272 		case $c {
    273 		(=)	break ;;
    274 		([A-Z])	(( v = 1#$c - 65 )) ;;
    275 		([a-z])	(( v = 1#$c - 71 )) ;;
    276 		([0-9])	(( v = 1#$c + 4 )) ;;
    277 		(+)	v=62 ;;
    278 		(/)	v=63 ;;
    279 		(*)	continue ;;
    280 		}
    281 		(( x = (x << 6) | v ))
    282 		case $((p++)) {
    283 		(0)	continue ;;
    284 		(1)	(( o = (x >> 4) & 255 )) ;;
    285 		(2)	(( o = (x >> 2) & 255 )) ;;
    286 		(3)	(( o = x & 255 ))
    287 			p=0
    288 			;;
    289 		}
    290 		t+=\\x${o#16#}
    291 		(( ++j & 4095 )) && continue
    292 		print -n $t
    293 		t=
    294 	done
    295 	print -n $t
    296 	(( u )) || set -U
    297 }
    298 
    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=$?
    303 	set +U
    304 	local c s t
    305 	if (( $# )); then
    306 		read -raN-1 s <<<"$*"
    307 		unset s[${#s[*]}-1]
    308 	else
    309 		read -raN-1 s
    310 	fi
    311 	local -i i=0 n=${#s[*]} j v
    312 
    313 	while (( i < n )); do
    314 		(( v = s[i++] << 16 ))
    315 		(( j = i < n ? s[i++] : 0 ))
    316 		(( v |= j << 8 ))
    317 		(( j = i < n ? s[i++] : 0 ))
    318 		(( v |= j ))
    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
    324 			t+=$c=
    325 		else
    326 			t+===
    327 		fi
    328 		if (( ${#t} == 76 || i >= n )); then
    329 			print $t
    330 			t=
    331 		fi
    332 	done
    333 	(( u )) || set -U
    334 }
    335 
    336 # mksh NUL counting, never zero
    337 typeset -Z11 -Uui16 Lnzathash_v
    338 function Lnzathash_add {
    339 	[[ -o utf8-mode ]]; local u=$?
    340 	set +U
    341 	local s
    342 	if (( $# )); then
    343 		read -raN-1 s <<<"$*"
    344 		unset s[${#s[*]}-1]
    345 	else
    346 		read -raN-1 s
    347 	fi
    348 	local -i i=0 n=${#s[*]}
    349 
    350 	while (( i < n )); do
    351 		((# Lnzathash_v = (Lnzathash_v + s[i++] + 1) * 1025 ))
    352 		((# Lnzathash_v ^= Lnzathash_v >> 6 ))
    353 	done
    354 
    355 	(( u )) || set -U
    356 }
    357 function Lnzaathash_end {
    358 	((# Lnzathash_v *= 1025 ))
    359 	((# Lnzathash_v ^= Lnzathash_v >> 6 ))
    360 	((# Lnzathash_v += Lnzathash_v << 3 ))
    361 	((# Lnzathash_v = (Lnzathash_v ^
    362 	    (Lnzathash_v >> 11)) * 32769 ))
    363 	print ${Lnzathash_v#16#}
    364 }
    365 function Lnzaathash {
    366 	Lnzathash_v=0
    367 	Lnzathash_add "$@"
    368 	Lnzaathash_end
    369 }
    370 function Lnzathash {
    371 	Lnzathash_v=0
    372 	Lnzathash_add "$@"
    373 	Lnzathash_end
    374 }
    375 function Lnzathash_end {
    376 	if (( Lnzathash_v )); then
    377 		Lnzaathash_end
    378 	else
    379 		Lnzathash_v=1
    380 		print ${Lnzathash_v#16#}
    381 	fi
    382 }
    383 
    384 # strip comments (and leading/trailing whitespace if IFS is set) from
    385 # any file(s) given as argument, or stdin if none, and spew to stdout
    386 function Lstripcom {
    387 	cat "$@" | { set -o noglob; while read _line; do
    388 		_line=${_line%%#*}
    389 		[[ -n $_line ]] && print -r -- $_line
    390 	done; }
    391 }
    392 
    393 # give MidnightBSD's laffer1 a bit of csh feeling
    394 function setenv {
    395 	eval export "\"$1\""'="$2"'
    396 }
    397 
    398 : place customisations below this line
    399 
    400 for p in ~/.etc/bin ~/bin; do
    401 	[[ -d $p/. ]] || continue
    402 	[[ :$PATH: = *:$p:* ]] || PATH=$p:$PATH
    403 done
    404 
    405 export SHELL=$MKSH MANWIDTH=80 LESSHISTFILE=-
    406 alias cls='print -n \\033c'
    407 
    408 #unset LANGUAGE LC_ADDRESS LC_ALL LC_COLLATE LC_IDENTIFICATION LC_MONETARY \
    409 #    LC_NAME LC_NUMERIC LC_TELEPHONE LC_TIME
    410 #p=en_GB.UTF-8
    411 #set -U
    412 #export LANG=C LC_CTYPE=$p LC_MEASUREMENT=$p LC_MESSAGES=$p LC_PAPER=$p
    413 
    414 unset p
    415 
    416 : place customisations above this line
    417