| 13 | | +# $MirOS: src/bin/mksh/dot.mkshrc,v 1.22 2007/09/25 22:36:36 tg Stab $ |
|---|
| 14 | | +#- |
|---|
| 15 | | +# Copyright (c) 2007 |
|---|
| 16 | | +# Thorsten Glaser <tg@mirbsd.de> |
|---|
| 17 | | +# |
|---|
| 18 | | +# Provided that these terms and disclaimer and all copyright notices |
|---|
| 19 | | +# are retained or reproduced in an accompanying document, permission |
|---|
| 20 | | +# is granted to deal in this work without restriction, including un- |
|---|
| 21 | | +# limited rights to use, publicly perform, distribute, sell, modify, |
|---|
| 22 | | +# merge, give away, or sublicence. |
|---|
| 23 | | +# |
|---|
| 24 | | +# Advertising materials mentioning features or use of this work must |
|---|
| 25 | | +# display the following acknowledgement: |
|---|
| 26 | | +# This product includes material provided by Thorsten Glaser. |
|---|
| 27 | | +# |
|---|
| 28 | | +# This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to |
|---|
| 29 | | +# the utmost extent permitted by applicable law, neither express nor |
|---|
| 30 | | +# implied; without malicious intent or gross negligence. In no event |
|---|
| 31 | | +# may a licensor, author or contributor be held liable for indirect, |
|---|
| 32 | | +# direct, other damage, loss, or other issues arising in any way out |
|---|
| 33 | | +# of dealing in the work, even if advised of the possibility of such |
|---|
| 34 | | +# damage or existence of a defect, except proven that it results out |
|---|
| 35 | | +# of said person's immediate fault when using the work as intended. |
|---|
| 36 | | +#- |
|---|
| 37 | | +# sample mksh initialisation file for interactive shells |
|---|
| 38 | | + |
|---|
| 65 | | @@ -26,6 +59,162 @@ whence -p hd >&- || function hd { |
|---|
| 66 | | -e '" |" "%_p"' -e '"|\n"' "$@" |
|---|
| 67 | | } |
|---|
| 68 | | |
|---|
| 69 | | +# Berkeley C shell compatible dirs, popd, and pushd functions |
|---|
| 70 | | +# Z shell compatible chpwd() hook, used to update DIRSTACK[0] |
|---|
| 71 | | +DIRSTACKBASE=$(readlink -nf ~/. 2>&- || print -nr -- "$HOME") |
|---|
| 72 | | +typeset -r DIRSTACKBASE |
|---|
| 73 | | +set -A DIRSTACK |
|---|
| 74 | | +function chpwd { |
|---|
| 75 | | + DIRSTACK[0]=$(readlink -nf . 2>&- || print -nr -- "$PWD") |
|---|
| 76 | | + [[ ${DIRSTACK[0]#$DIRSTACKBASE} = ${DIRSTACK[0]} ]] || \ |
|---|
| 77 | | + DIRSTACK[0]=\~${DIRSTACK[0]#$DIRSTACKBASE} |
|---|
| 78 | | + : |
|---|
| 79 | | +} |
|---|
| 80 | | +chpwd . |
|---|
| 81 | | +function cd { |
|---|
| 82 | | + builtin cd "$@" |
|---|
| 83 | | + chpwd "$@" |
|---|
| 84 | | +} |
|---|
| 85 | | +function cd_csh { |
|---|
| 86 | | + typeset d t=$1 |
|---|
| 87 | | + |
|---|
| 88 | | + [[ $t = ~* ]] && t=$DIRSTACKBASE${t#~} |
|---|
| 89 | | + if ! d=$(builtin cd "$t" 2>&1); then |
|---|
| 90 | | + print -u2 "${1}: ${d##*$t - }." |
|---|
| 91 | | + return 1 |
|---|
| 92 | | + fi |
|---|
| 93 | | + cd "$t" |
|---|
| 94 | | +} |
|---|
| 95 | | +function dirs { |
|---|
| 96 | | + typeset d |
|---|
| 97 | | + typeset -i isnoglob=0 fl=0 fv=0 fn=0 cpos=0 |
|---|
| 98 | | + |
|---|
| 99 | | + [[ $(set +o) == *-o\ noglob* ]] && isnoglob=1 |
|---|
| 100 | | + set -o noglob |
|---|
| 101 | | + while getopts ":lvn" d; do |
|---|
| 102 | | + case $d { |
|---|
| 103 | | + (l) fl=1 ;; |
|---|
| 104 | | + (v) fv=1 ;; |
|---|
| 105 | | + (n) fn=1 ;; |
|---|
| 106 | | + (*) print -u2 'Usage: dirs [-lvn].' |
|---|
| 107 | | + return 1 ;; |
|---|
| 108 | | + } |
|---|
| 109 | | + done |
|---|
| 110 | | + shift $((OPTIND - 1)) |
|---|
| 111 | | + if (( $# > 0 )); then |
|---|
| 112 | | + print -u2 'Usage: dirs [-lvn].' |
|---|
| 113 | | + return 1 |
|---|
| 114 | | + fi |
|---|
| 115 | | + if (( fv )); then |
|---|
| 116 | | + fv=0 |
|---|
| 117 | | + while (( fv < ${#DIRSTACK[*]} )); do |
|---|
| 118 | | + d=${DIRSTACK[fv]} |
|---|
| 119 | | + [[ $fl$d = 1~* ]] && d=$DIRSTACKBASE${d#~} |
|---|
| 120 | | + print -r -- "$fv $d" |
|---|
| 121 | | + let fv++ |
|---|
| 122 | | + done |
|---|
| 123 | | + else |
|---|
| 124 | | + fv=0 |
|---|
| 125 | | + while (( fv < ${#DIRSTACK[*]} )); do |
|---|
| 126 | | + d=${DIRSTACK[fv]} |
|---|
| 127 | | + [[ $fl$d = 1~* ]] && d=$DIRSTACKBASE${d#~} |
|---|
| 128 | | + if (( fn && (cpos+=${#d}+1) >= 79 && ${#d} < 80 )); then |
|---|
| 129 | | + print |
|---|
| 130 | | + (( cpos = ${#d} + 1 )) |
|---|
| 131 | | + fi |
|---|
| 132 | | + print -nr -- "$d " |
|---|
| 133 | | + let fv++ |
|---|
| 134 | | + done |
|---|
| 135 | | + print |
|---|
| 136 | | + fi |
|---|
| 137 | | + (( isnoglob )) || set +o noglob |
|---|
| 138 | | + return 0 |
|---|
| 139 | | +} |
|---|
| 140 | | +function popd { |
|---|
| 141 | | + typeset d fa |
|---|
| 142 | | + typeset -i isnoglob=0 n=1 |
|---|
| 143 | | + |
|---|
| 144 | | + [[ $(set +o) == *-o\ noglob* ]] && isnoglob=1 |
|---|
| 145 | | + set -o noglob |
|---|
| 146 | | + while getopts ":0123456789lvn" d; do |
|---|
| 147 | | + case $d { |
|---|
| 148 | | + (l|v|n) fa="$fa -$d" ;; |
|---|
| 149 | | + (+*) n=2 |
|---|
| 150 | | + break ;; |
|---|
| 151 | | + (*) print -u2 'Usage: popd [-lvn] [+<n>].' |
|---|
| 152 | | + return 1 ;; |
|---|
| 153 | | + } |
|---|
| 154 | | + done |
|---|
| 155 | | + shift $((OPTIND - n)) |
|---|
| 156 | | + n=0 |
|---|
| 157 | | + if (( $# > 1 )); then |
|---|
| 158 | | + print -u2 popd: Too many arguments. |
|---|
| 159 | | + return 1 |
|---|
| 160 | | + elif [[ $1 = ++([0-9]) && $1 != +0 ]]; then |
|---|
| 161 | | + if (( (n = ${1#+}) >= ${#DIRSTACK[*]} )); then |
|---|
| 162 | | + print -u2 popd: Directory stack not that deep. |
|---|
| 163 | | + return 1 |
|---|
| 164 | | + fi |
|---|
| 165 | | + elif [[ -n $1 ]]; then |
|---|
| 166 | | + print -u2 popd: Bad directory. |
|---|
| 167 | | + return 1 |
|---|
| 168 | | + fi |
|---|
| 169 | | + if (( ${#DIRSTACK[*]} < 2 )); then |
|---|
| 170 | | + print -u2 popd: Directory stack empty. |
|---|
| 171 | | + return 1 |
|---|
| 172 | | + fi |
|---|
| 173 | | + unset DIRSTACK[n] |
|---|
| 174 | | + set -A DIRSTACK -- "${DIRSTACK[@]}" |
|---|
| 175 | | + cd_csh "${DIRSTACK[0]}" || return 1 |
|---|
| 176 | | + (( isnoglob )) || set +o noglob |
|---|
| 177 | | + dirs $fa |
|---|
| 178 | | +} |
|---|
| 179 | | +function pushd { |
|---|
| 180 | | + typeset d fa |
|---|
| 181 | | + typeset -i isnoglob=0 n=1 |
|---|
| 182 | | + |
|---|
| 183 | | + [[ $(set +o) == *-o\ noglob* ]] && isnoglob=1 |
|---|
| 184 | | + set -o noglob |
|---|
| 185 | | + while getopts ":0123456789lvn" d; do |
|---|
| 186 | | + case $d { |
|---|
| 187 | | + (l|v|n) fa="$fa -$d" ;; |
|---|
| 188 | | + (+*) n=2 |
|---|
| 189 | | + break ;; |
|---|
| 190 | | + (*) print -u2 'Usage: pushd [-lvn] [<dir>|+<n>].' |
|---|
| 191 | | + return 1 ;; |
|---|
| 192 | | + } |
|---|
| 193 | | + done |
|---|
| 194 | | + shift $((OPTIND - n)) |
|---|
| 195 | | + if (( $# == 0 )); then |
|---|
| 196 | | + if (( ${#DIRSTACK[*]} < 2 )); then |
|---|
| 197 | | + print -u2 pushd: No other directory. |
|---|
| 198 | | + return 1 |
|---|
| 199 | | + fi |
|---|
| 200 | | + d=${DIRSTACK[1]} |
|---|
| 201 | | + DIRSTACK[1]=${DIRSTACK[0]} |
|---|
| 202 | | + cd_csh "$d" || return 1 |
|---|
| 203 | | + elif (( $# > 1 )); then |
|---|
| 204 | | + print -u2 pushd: Too many arguments. |
|---|
| 205 | | + return 1 |
|---|
| 206 | | + elif [[ $1 = ++([0-9]) && $1 != +0 ]]; then |
|---|
| 207 | | + if (( (n = ${1#+}) >= ${#DIRSTACK[*]} )); then |
|---|
| 208 | | + print -u2 pushd: Directory stack not that deep. |
|---|
| 209 | | + return 1 |
|---|
| 210 | | + fi |
|---|
| 211 | | + while (( n-- )); do |
|---|
| 212 | | + d=${DIRSTACK[0]} |
|---|
| 213 | | + unset DIRSTACK[0] |
|---|
| 214 | | + set -A DIRSTACK -- "${DIRSTACK[@]}" "$d" |
|---|
| 215 | | + done |
|---|
| 216 | | + cd_csh "${DIRSTACK[0]}" || return 1 |
|---|
| 217 | | + else |
|---|
| 218 | | + set -A DIRSTACK -- placeholder "${DIRSTACK[@]}" |
|---|
| 219 | | + cd_csh "$1" || return 1 |
|---|
| 220 | | + fi |
|---|
| 221 | | + (( isnoglob )) || set +o noglob |
|---|
| 222 | | + dirs $fa |
|---|
| 223 | | +} |
|---|
| 224 | | + |
|---|
| 225 | | # strip comments (and leading/trailing whitespace if IFS is set) from |
|---|
| 226 | | # any file(s) given as argument, or stdin if none, and spew to stdout |
|---|
| 227 | | function Lstripcom { |
|---|
| 228 | | @@ -35,6 +224,4 @@ function Lstripcom { |
|---|
| 229 | | done; } |
|---|
| 230 | | } |
|---|
| 231 | | |
|---|
| 232 | | -# place customisations between this line and the “: RCSID” line below |
|---|
| 233 | | - |
|---|
| 234 | | -: $MirOS: src/bin/mksh/dot.mkshrc,v 1.19 2007/09/03 09:25:05 tg Rel $ |
|---|
| 235 | | +: place customsations above this line! |
|---|