Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1 | # |
| 2 | # .kshenv -- functions and aliases to provide the beginnings of a ksh |
| 3 | # environment for bash. |
| 4 | # |
| 5 | # Chet Ramey |
| 6 | # chet@ins.CWRU.Edu |
| 7 | # |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 8 | |
| 9 | # |
| 10 | # Copyright 2002 Chester Ramey |
| 11 | # |
| 12 | # This program is free software; you can redistribute it and/or modify |
| 13 | # it under the terms of the GNU General Public License as published by |
| 14 | # the Free Software Foundation; either version 2, or (at your option) |
| 15 | # any later version. |
| 16 | # |
| 17 | # TThis program is distributed in the hope that it will be useful, |
| 18 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | # GNU General Public License for more details. |
| 21 | # |
| 22 | # You should have received a copy of the GNU General Public License |
| 23 | # along with this program; if not, write to the Free Software Foundation, |
| 24 | # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 26 | # |
| 27 | # These are definitions for the ksh compiled-in `exported aliases'. There |
| 28 | # are others, but we already have substitutes for them: "history", "type", |
| 29 | # and "hash". |
| 30 | # |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 31 | alias r="fc -s" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 32 | alias functions="typeset -f" |
| 33 | alias integer="typeset -i" |
| 34 | alias nohup="nohup " |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 35 | alias command="command " |
| 36 | alias stop="kill -s STOP" |
| 37 | alias redirect="command exec" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 38 | alias hist="fc" |
| 39 | |
| 40 | # |
| 41 | # An almost-ksh compatible `whence' command. This is as hairy as it is |
| 42 | # because of the desire to exactly mimic ksh (whose behavior was determined |
| 43 | # empirically). |
| 44 | # |
| 45 | # This depends somewhat on knowing the format of the output of the bash |
| 46 | # `builtin type' command. |
| 47 | # |
| 48 | |
| 49 | whence() |
| 50 | { |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 51 | local vflag pflag fflag defarg c |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 52 | local path |
| 53 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 54 | vflag= aflag= pflag= fflag= |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 55 | path= |
| 56 | if [ "$#" = "0" ] ; then |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 57 | echo "whence: usage: whence [-afpv] name..." >&2 |
| 58 | return 2 |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 59 | fi |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 60 | |
| 61 | OPTIND=1 |
| 62 | while getopts "avfp" c |
| 63 | do |
| 64 | case "$c" in |
| 65 | a) defarg=-a ;; |
| 66 | f) fflag=1 ;; # no-op |
| 67 | p) pflag=1 ;; |
| 68 | v) vflag=1 ;; |
| 69 | ?) echo "whence: $1: unknown option" >&2 |
| 70 | echo "whence: usage: whence [-afpv] name..." >&2 |
| 71 | return 2 ;; |
| 72 | esac |
| 73 | done |
| 74 | |
| 75 | shift $(( $OPTIND - 1 )) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 76 | |
| 77 | if [ "$#" = "0" ] ; then |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 78 | echo "whence: usage: whence [-afpv] name..." >&2 |
| 79 | return 2 |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 80 | fi |
| 81 | |
| 82 | for cmd |
| 83 | do |
| 84 | if [ "$vflag" ] ; then |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 85 | if [ -z "$defarg" ]; then |
| 86 | builtin type $cmd | sed 1q |
| 87 | else |
| 88 | if builtin type $defarg -t $cmd | grep 'function$' >/dev/null 2>&1; then |
| 89 | # HAIRY awk script to suppress |
| 90 | # printing of function body -- could |
| 91 | # do it with sed, but I don't have |
| 92 | # that kind of time |
| 93 | builtin type $defarg $cmd | awk ' |
| 94 | BEGIN {printit = 1;} |
| 95 | $1 == "'$cmd'" && $2 == "()" {printit=0; next; } |
| 96 | /^}$/ { if (printit == 0) printit=1 ; else print $0; next ; } |
| 97 | /.*/ { if (printit) print $0; }' |
| 98 | else |
| 99 | builtin type $defarg $cmd |
| 100 | fi |
| 101 | fi |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 102 | else |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 103 | path=$(builtin type $defarg -p $cmd) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 104 | if [ "$path" ] ; then |
| 105 | echo $path |
| 106 | else |
| 107 | case "$cmd" in |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 108 | /*) echo "" ;; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 109 | *) case "$(builtin type -t $cmd)" in |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 110 | "") echo "" ;; |
| 111 | *) echo "$cmd" ;; |
| 112 | esac |
| 113 | ;; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 114 | esac |
| 115 | fi |
| 116 | fi |
| 117 | done |
| 118 | return 0 |
| 119 | } |
| 120 | |
| 121 | # |
| 122 | # For real ksh homeboy fanatics, redefine the `type' builtin with a ksh |
| 123 | # version. |
| 124 | # |
| 125 | #type() |
| 126 | #{ |
| 127 | # whence -v "$*" |
| 128 | #} |
| 129 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 130 | # |
| 131 | # ksh-like `cd': cd [-LP] [dir [change]] |
| 132 | # |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 133 | cd() |
| 134 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 135 | OPTIND=1 |
| 136 | while getopts "LP" opt |
| 137 | do |
| 138 | case $opt in |
| 139 | L|P) CDOPTS="$CDOPTS -$opt" ;; |
| 140 | *) echo "$FUNCNAME: usage: $FUNCNAME [-LP] [dir] [change]" >&2 |
| 141 | return 2;; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 142 | esac |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 143 | done |
| 144 | |
| 145 | shift $(( $OPTIND - 1 )) |
| 146 | |
| 147 | case $# in |
| 148 | 0) builtin cd $CDOPTS "$HOME" ;; |
| 149 | 1) builtin cd $CDOPTS "$@" ;; |
| 150 | 2) old="$1" new="$2" |
| 151 | case "$PWD" in |
| 152 | *$old*) ;; |
| 153 | *) echo "${0##*/}: $FUNCNAME: bad substitution" >&2 ; return 1 ;; |
| 154 | esac |
| 155 | |
| 156 | dir=${PWD//$old/$new} |
| 157 | |
| 158 | builtin cd $CDOPTS "$dir" && echo "$PWD" |
| 159 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 160 | ;; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 161 | *) echo "${0##*/}: $FUNCNAME: usage: $FUNCNAME [-LP] [dir] [change]" >&2 |
| 162 | return 2 ;; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 163 | esac |
| 164 | } |
| 165 | |
| 166 | # |
| 167 | # ksh print emulation |
| 168 | # |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 169 | # print [-Rnprsu[n]] [-f format] [arg ...] |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 170 | # |
| 171 | # - end of options |
| 172 | # -R BSD-style -- only accept -n, no escapes |
| 173 | # -n do not add trailing newline |
| 174 | # -p no-op (no coprocesses) |
| 175 | # -r no escapes |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 176 | # -s print to the history file |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 177 | # -u n redirect output to fd n |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 178 | # -f format printf "$format" "$@" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 179 | # |
| 180 | |
| 181 | print() |
| 182 | { |
| 183 | local eflag=-e |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 184 | local nflag= fflag= c |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 185 | local fd=1 |
| 186 | |
| 187 | OPTIND=1 |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 188 | while getopts "fRnprsu:" c |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 189 | do |
| 190 | case $c in |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 191 | R) eflag= ;; |
| 192 | r) eflag= ;; |
| 193 | n) nflag=-n ;; |
| 194 | s) sflag=y ;; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 195 | f) fflag=y ;; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 196 | u) fd=$OPTARG ;; |
| 197 | p) ;; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 198 | esac |
| 199 | done |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 200 | shift $(( $OPTIND - 1 )) |
| 201 | |
| 202 | if [ -n "$fflag" ]; then |
| 203 | builtin printf "$@" >&$fd |
| 204 | return |
| 205 | fi |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 206 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 207 | case "$sflag" in |
| 208 | y) builtin history -s "$*" ;; |
| 209 | *) builtin echo $eflag $nflag "$@" >&$fd |
| 210 | esac |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | # substring function |
| 214 | # this function should be equivalent to the substring built-in which was |
| 215 | # eliminated after the 06/29/84 version |
| 216 | substring () |
| 217 | { |
| 218 | local lpat flag str #local variables |
| 219 | set -f |
| 220 | case $1 in |
| 221 | -l|-L) |
| 222 | flag=$1 |
| 223 | lpat=$2 |
| 224 | shift 2 |
| 225 | ;; |
| 226 | esac |
| 227 | # test for too few or too many arguments |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 228 | if [ x"$1" = x ] || [ $# -gt 2 ]; then |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 229 | print -u2 'substring: bad argument count' |
| 230 | return 1 |
| 231 | fi |
| 232 | str=$1 |
| 233 | if [ x"$flag" = x-l ]; then #substring -l lpat |
| 234 | str=${str#$lpat} |
| 235 | elif [ x"$flag" = x-L ]; then |
| 236 | str=${str##$lpat} #substring -L lpat |
| 237 | fi |
| 238 | |
| 239 | if [ x"$2" != x ]; then |
| 240 | echo ${str%$2} |
| 241 | else |
| 242 | echo $str |
| 243 | fi |
| 244 | |
| 245 | return 0 |
| 246 | } |