1# $Id$ 2# $MirOS: src/bin/mksh/dot.mkshrc,v 1.65 2011/08/27 18:06:40 tg Exp $ 3#- 4# Copyright (c) 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010, 2011 5# Thorsten Glaser <tg@mirbsd.org> 6# 7# Provided that these terms and disclaimer and all copyright notices 8# are retained or reproduced in an accompanying document, permission 9# is granted to deal in this work without restriction, including un- 10# limited rights to use, publicly perform, distribute, sell, modify, 11# merge, give away, or sublicence. 12# 13# This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to 14# the utmost extent permitted by applicable law, neither express nor 15# implied; without malicious intent or gross negligence. In no event 16# may a licensor, author or contributor be held liable for indirect, 17# direct, other damage, loss, or other issues arising in any way out 18# of dealing in the work, even if advised of the possibility of such 19# damage or existence of a defect, except proven that it results out 20# of said person's immediate fault when using the work as intended. 21#- 22# ${ENV:-~/.mkshrc}: mksh initialisation file for interactive shells 23 24: ${EDITOR:=/bin/ed} ${TERM:=vt100} ${HOSTNAME:=$(ulimit -c 0;hostname -s 2>&-)} 25[[ $HOSTNAME = @(localhost|*([ ])) ]] && HOSTNAME=$(ulimit -c 0;hostname 2>&-) 26: ${HOSTNAME:=nil}; if (( USER_ID )); then PS1='$'; else PS1='#'; fi 27function precmd { 28 local e=$? 29 30 (( e )) && print -n "$e|" 31} 32PS1=$'\001\r''$(precmd)${USER:=$(ulimit -c 0; id -un 2>/dev/null || echo \? 33 )}@${HOSTNAME%%.*}:$(local d=${PWD:-?} p=~; [[ $p = ?(*/) ]] || \ 34 d=${d/#$p/~}; local m=${%d} n p=...; (( m > 0 )) || m=${#d} 35 (( m > (n = (COLUMNS/3 < 7 ? 7 : COLUMNS/3)) )) && d=${d:(-n)} || \ 36 p=; print -nr -- "$p$d") '"$PS1 " 37: ${MKSH:=$(whence -p mksh)}; export EDITOR HOSTNAME MKSH TERM USER 38alias ls=ls 39unalias ls 40alias l='ls -F' 41alias la='l -a' 42alias ll='l -l' 43alias lo='l -alo' 44whence -p rot13 >&- || alias rot13='tr \ 45 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ \ 46 nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM' 47whence -p hd >&- || function hd { 48 hexdump -e '"%08.8_ax " 8/1 "%02X " " - " 8/1 "%02X "' \ 49 -e '" |" "%_p"' -e '"|\n"' "$@" 50} 51 52# Berkeley C shell compatible dirs, popd, and pushd functions 53# Z shell compatible chpwd() hook, used to update DIRSTACK[0] 54DIRSTACKBASE=$(realpath ~/. 2>&- || print -nr -- "$HOME") 55set -A DIRSTACK 56function chpwd { 57 DIRSTACK[0]=$(realpath . 2>&- || print -r -- "$PWD") 58 [[ $DIRSTACKBASE = ?(*/) ]] || \ 59 DIRSTACK[0]=${DIRSTACK[0]/#$DIRSTACKBASE/~} 60 : 61} 62chpwd . 63function cd { 64 builtin cd "$@" 65 chpwd "$@" 66} 67function cd_csh { 68 local d t=${1/#~/$DIRSTACKBASE} 69 70 if ! d=$(builtin cd "$t" 2>&1); then 71 print -u2 "${1}: ${d##*$t - }." 72 return 1 73 fi 74 cd "$t" 75} 76function dirs { 77 local d dwidth 78 local -i isnoglob=0 fl=0 fv=0 fn=0 cpos=0 79 80 [[ $(set +o) == *@(-o noglob)@(| *) ]] && isnoglob=1 81 set -o noglob 82 while getopts ":lvn" d; do 83 case $d { 84 (l) fl=1 ;; 85 (v) fv=1 ;; 86 (n) fn=1 ;; 87 (*) print -u2 'Usage: dirs [-lvn].' 88 return 1 ;; 89 } 90 done 91 shift $((OPTIND - 1)) 92 if (( $# > 0 )); then 93 print -u2 'Usage: dirs [-lvn].' 94 return 1 95 fi 96 if (( fv )); then 97 fv=0 98 while (( fv < ${#DIRSTACK[*]} )); do 99 d=${DIRSTACK[fv]} 100 (( fl )) && d=${d/#~/$DIRSTACKBASE} 101 print -r -- "$fv $d" 102 let fv++ 103 done 104 else 105 fv=0 106 while (( fv < ${#DIRSTACK[*]} )); do 107 d=${DIRSTACK[fv]} 108 (( fl )) && d=${d/#~/$DIRSTACKBASE} 109 (( dwidth = (${%d} > 0 ? ${%d} : ${#d}) )) 110 if (( fn && (cpos += dwidth + 1) >= 79 && \ 111 dwidth < 80 )); then 112 print 113 (( cpos = dwidth + 1 )) 114 fi 115 print -nr -- "$d " 116 let fv++ 117 done 118 print 119 fi 120 (( isnoglob )) || set +o noglob 121 return 0 122} 123function popd { 124 local d fa 125 local -i isnoglob=0 n=1 126 127 [[ $(set +o) == *@(-o noglob)@(| *) ]] && isnoglob=1 128 set -o noglob 129 while getopts ":0123456789lvn" d; do 130 case $d { 131 (l|v|n) fa="$fa -$d" ;; 132 (+*) n=2 133 break ;; 134 (*) print -u2 'Usage: popd [-lvn] [+<n>].' 135 return 1 ;; 136 } 137 done 138 shift $((OPTIND - n)) 139 n=0 140 if (( $# > 1 )); then 141 print -u2 popd: Too many arguments. 142 return 1 143 elif [[ $1 = ++([0-9]) && $1 != +0 ]]; then 144 if (( (n = ${1#+}) >= ${#DIRSTACK[*]} )); then 145 print -u2 popd: Directory stack not that deep. 146 return 1 147 fi 148 elif [[ -n $1 ]]; then 149 print -u2 popd: Bad directory. 150 return 1 151 fi 152 if (( ${#DIRSTACK[*]} < 2 )); then 153 print -u2 popd: Directory stack empty. 154 return 1 155 fi 156 unset DIRSTACK[n] 157 set -A DIRSTACK -- "${DIRSTACK[@]}" 158 cd_csh "${DIRSTACK[0]}" || return 1 159 (( isnoglob )) || set +o noglob 160 dirs $fa 161} 162function pushd { 163 local d fa 164 local -i isnoglob=0 n=1 165 166 [[ $(set +o) == *@(-o noglob)@(| *) ]] && isnoglob=1 167 set -o noglob 168 while getopts ":0123456789lvn" d; do 169 case $d { 170 (l|v|n) fa="$fa -$d" ;; 171 (+*) n=2 172 break ;; 173 (*) print -u2 'Usage: pushd [-lvn] [<dir>|+<n>].' 174 return 1 ;; 175 } 176 done 177 shift $((OPTIND - n)) 178 if (( $# == 0 )); then 179 if (( ${#DIRSTACK[*]} < 2 )); then 180 print -u2 pushd: No other directory. 181 return 1 182 fi 183 d=${DIRSTACK[1]} 184 DIRSTACK[1]=${DIRSTACK[0]} 185 cd_csh "$d" || return 1 186 elif (( $# > 1 )); then 187 print -u2 pushd: Too many arguments. 188 return 1 189 elif [[ $1 = ++([0-9]) && $1 != +0 ]]; then 190 if (( (n = ${1#+}) >= ${#DIRSTACK[*]} )); then 191 print -u2 pushd: Directory stack not that deep. 192 return 1 193 fi 194 while (( n-- )); do 195 d=${DIRSTACK[0]} 196 unset DIRSTACK[0] 197 set -A DIRSTACK -- "${DIRSTACK[@]}" "$d" 198 done 199 cd_csh "${DIRSTACK[0]}" || return 1 200 else 201 set -A DIRSTACK -- placeholder "${DIRSTACK[@]}" 202 cd_csh "$1" || return 1 203 fi 204 (( isnoglob )) || set +o noglob 205 dirs $fa 206} 207 208# pager (not control character safe) 209function smores { 210 local dummy line llen curlin=0 211 212 cat "$@" | while IFS= read -r line; do 213 llen=${%line} 214 (( llen == -1 )) && llen=${#line} 215 (( llen = llen ? (llen + COLUMNS - 1) / COLUMNS : 1 )) 216 if (( (curlin += llen) >= LINES )); then 217 print -n -- '\033[7m--more--\033[0m' 218 read -u1 dummy 219 [[ $dummy = [Qq]* ]] && return 0 220 curlin=$llen 221 fi 222 print -r -- "$line" 223 done 224} 225 226# base64 encoder and decoder, RFC compliant, NUL safe 227function Lb64decode { 228 [[ -o utf8-mode ]]; local u=$? 229 set +U 230 local c s="$*" t= 231 [[ -n $s ]] || { s=$(cat;print x); s=${s%x}; } 232 local -i i=0 n=${#s} p=0 v x 233 local -i16 o 234 235 while (( i < n )); do 236 c=${s:(i++):1} 237 case $c { 238 (=) break ;; 239 ([A-Z]) (( v = 1#$c - 65 )) ;; 240 ([a-z]) (( v = 1#$c - 71 )) ;; 241 ([0-9]) (( v = 1#$c + 4 )) ;; 242 (+) v=62 ;; 243 (/) v=63 ;; 244 (*) continue ;; 245 } 246 (( x = (x << 6) | v )) 247 case $((p++)) { 248 (0) continue ;; 249 (1) (( o = (x >> 4) & 255 )) ;; 250 (2) (( o = (x >> 2) & 255 )) ;; 251 (3) (( o = x & 255 )) 252 p=0 253 ;; 254 } 255 t=$t\\x${o#16#} 256 done 257 print -n $t 258 (( u )) || set -U 259} 260 261set -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 \ 262 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 + / 263function Lb64encode { 264 [[ -o utf8-mode ]]; local u=$? 265 set +U 266 local c s t 267 if (( $# )); then 268 read -raN-1 s <<<"$*" 269 unset s[${#s[*]}-1] 270 else 271 read -raN-1 s 272 fi 273 local -i i=0 n=${#s[*]} j v 274 275 while (( i < n )); do 276 (( v = s[i++] << 16 )) 277 (( j = i < n ? s[i++] : 0 )) 278 (( v |= j << 8 )) 279 (( j = i < n ? s[i++] : 0 )) 280 (( v |= j )) 281 t=$t${Lb64encode_code[v >> 18]}${Lb64encode_code[v >> 12 & 63]} 282 c=${Lb64encode_code[v >> 6 & 63]} 283 if (( i <= n )); then 284 t=$t$c${Lb64encode_code[v & 63]} 285 elif (( i == n + 1 )); then 286 t=$t$c= 287 else 288 t=$t== 289 fi 290 if (( ${#t} == 76 || i >= n )); then 291 print $t 292 t= 293 fi 294 done 295 (( u )) || set -U 296} 297 298# mksh NUL counting, never zero 299typeset -Z11 -Uui16 Lnzathash_v 300function Lnzathash_add { 301 [[ -o utf8-mode ]]; local u=$? 302 set +U 303 local s 304 if (( $# )); then 305 read -raN-1 s <<<"$*" 306 unset s[${#s[*]}-1] 307 else 308 read -raN-1 s 309 fi 310 local -i i=0 n=${#s[*]} 311 312 while (( i < n )); do 313 ((# Lnzathash_v = (Lnzathash_v + s[i++] + 1) * 1025 )) 314 ((# Lnzathash_v ^= Lnzathash_v >> 6 )) 315 done 316 317 (( u )) || set -U 318} 319function Lnzaathash_end { 320 ((# Lnzathash_v *= 1025 )) 321 ((# Lnzathash_v ^= Lnzathash_v >> 6 )) 322 ((# Lnzathash_v += Lnzathash_v << 3 )) 323 ((# Lnzathash_v = (Lnzathash_v ^ 324 (Lnzathash_v >> 11)) * 32769 )) 325 print ${Lnzathash_v#16#} 326} 327function Lnzaathash { 328 Lnzathash_v=0 329 Lnzathash_add "$@" 330 Lnzaathash_end 331} 332function Lnzathash { 333 Lnzathash_v=0 334 Lnzathash_add "$@" 335 if (( Lnzathash_v )); then 336 Lnzaathash_end 337 else 338 Lnzathash_v=1 339 print ${Lnzathash_v#16#} 340 fi 341} 342 343# strip comments (and leading/trailing whitespace if IFS is set) from 344# any file(s) given as argument, or stdin if none, and spew to stdout 345function Lstripcom { 346 cat "$@" | { set -o noglob; while read _line; do 347 _line=${_line%%#*} 348 [[ -n $_line ]] && print -r -- $_line 349 done; } 350} 351 352# give MidnightBSD's laffer1 a bit of csh feeling 353function setenv { 354 eval export $1'="$2"' 355} 356 357: place customisations below this line 358 359for p in ~/.etc/bin ~/bin; do 360 [[ -d $p/. ]] || continue 361 [[ :$PATH: = *:$p:* ]] || PATH=$p:$PATH 362done 363 364export SHELL=$MKSH MANWIDTH=80 LESSHISTFILE=- 365alias cls='print -n \\033c' 366 367#unset LANGUAGE LC_ADDRESS LC_ALL LC_COLLATE LC_IDENTIFICATION LC_MONETARY \ 368# LC_NAME LC_NUMERIC LC_TELEPHONE LC_TIME 369#p=en_GB.UTF-8 370#set -U 371#export LANG=C LC_CTYPE=$p LC_MEASUREMENT=$p LC_MESSAGES=$p LC_PAPER=$p 372 373unset p 374 375: place customisations above this line 376