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