summaryrefslogtreecommitdiffstats
path: root/examples/functions/kshenv
blob: 9faba08cd71f01ba54aa30675f556339428000ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#
# .kshenv -- functions and aliases to provide the beginnings of a ksh 
#	     environment for bash.
#
# Chet Ramey
# chet@ins.CWRU.Edu
#

#
#  Copyright 2002 Chester Ramey
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2, or (at your option)
#   any later version.
#
#   TThis program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software Foundation,
#   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

#
# These are definitions for the ksh compiled-in `exported aliases'.  There
# are others, but we already have substitutes for them: "history", "type",
# and "hash".
#
alias r="fc -s"
alias functions="typeset -f"
alias integer="typeset -i"
alias nohup="nohup "
alias command="command "
alias stop="kill -s STOP"
alias redirect="command exec"
alias hist="fc"

#
# An almost-ksh compatible `whence' command.  This is as hairy as it is 
# because of the desire to exactly mimic ksh (whose behavior was determined
# empirically).
# 
# This depends somewhat on knowing the format of the output of the bash
# `builtin type' command.
#

whence()
{
	local vflag pflag fflag defarg c
	local path

	vflag= aflag= pflag= fflag=
	path=
	if [ "$#" = "0" ] ; then
		echo "whence: usage: whence [-afpv] name..." >&2
		return 2
	fi

	OPTIND=1
	while getopts "avfp" c
	do
		case "$c" in
		a) defarg=-a ;;
		f) fflag=1 ;;	# no-op
		p) pflag=1 ;;
		v) vflag=1 ;;
		?) echo "whence: $1: unknown option" >&2
		   echo "whence: usage: whence [-afpv] name..." >&2
		   return 2 ;;
		esac
	done

	shift $(( $OPTIND - 1 ))

	if [ "$#" = "0" ] ; then
		echo "whence: usage: whence [-afpv] name..." >&2
		return 2
	fi

	for cmd
	do
		if [ "$vflag" ]	 ; then
			if [ -z "$defarg" ]; then
				builtin type $cmd | sed 1q
			else
				if builtin type $defarg -t $cmd | grep 'function$' >/dev/null 2>&1; then
					# HAIRY awk script to suppress
					# printing of function body -- could
					# do it with sed, but I don't have
					# that kind of time
					builtin type $defarg $cmd | awk '
BEGIN {printit = 1;}
$1 == "'$cmd'" && $2 == "()" {printit=0; next; }
/^}$/ { if (printit == 0) printit=1 ; else print $0; next ; }
/.*/ { if (printit) print $0; }'
				else
					builtin type $defarg $cmd
				fi
			fi
		else
			path=$(builtin type $defarg -p $cmd)
			if [ "$path" ] ; then
				echo $path
			else
				case "$cmd" in
				/*) echo "" ;;
				 *) case "$(builtin type -t $cmd)" in
				    "") echo "" ;;
				    *) echo "$cmd" ;;
				    esac
				    ;;
				esac
			fi
		fi
	done
	return 0
}

#
# For real ksh homeboy fanatics, redefine the `type' builtin with a ksh
# version.
#
#type()
#{
#	whence -v "$*"
#}

#
# ksh-like `cd': cd [-LP] [dir [change]]
#
cd()
{
	OPTIND=1
	while getopts "LP" opt
	do
		case $opt in
		L|P)	CDOPTS="$CDOPTS -$opt" ;;
		*)	echo "$FUNCNAME: usage: $FUNCNAME [-LP] [dir] [change]" >&2
			return 2;;
		esac
	done

	shift $(( $OPTIND - 1 ))

	case $# in
	0)	builtin cd $CDOPTS "$HOME" ;;
	1) 	builtin cd $CDOPTS "$@" ;;
	2)	old="$1" new="$2"
		case "$PWD" in
		*$old*)	;;
		*)	 echo "${0##*/}: $FUNCNAME: bad substitution" >&2 ; return 1 ;;
		esac

		dir=${PWD//$old/$new}

		builtin cd $CDOPTS "$dir" && echo "$PWD"

		;;
	*)	echo "${0##*/}: $FUNCNAME: usage: $FUNCNAME [-LP] [dir] [change]" >&2
		return 2 ;;
	esac
}

#
# ksh print emulation
#
#	print [-Rnprsu[n]] [-f format] [arg ...]
#
#	-	end of options
#	-R	BSD-style -- only accept -n, no escapes
#	-n	do not add trailing newline
#	-p	no-op (no coprocesses)
#	-r	no escapes
#	-s	print to the history file
#	-u n	redirect output to fd n
#	-f format	printf "$format" "$@"
#

print()
{
	local eflag=-e
	local nflag= fflag= c
	local fd=1

	OPTIND=1
	while getopts "fRnprsu:" c
	do
		case $c in
		R)	eflag= ;;
		r)	eflag= ;;
		n)	nflag=-n ;;
		s)	sflag=y ;;
		f)	fflag=y ;;
		u)	fd=$OPTARG ;;
		p)	;;
		esac
	done
	shift $(( $OPTIND - 1 ))

	if [ -n "$fflag" ]; then
		builtin printf "$@" >&$fd
		return
	fi

	case "$sflag" in
	y)	builtin history -s "$*" ;;
	*)	builtin echo $eflag $nflag "$@" >&$fd
	esac
}

# substring function
# this function should be equivalent to the substring built-in which was
# eliminated after the 06/29/84 version
substring ()
{
	local lpat flag str	#local variables
	set -f
	case $1 in
	-l|-L)
		flag=$1
		lpat=$2
		shift 2
		;;
	esac
	# test for too few or too many arguments
	if [ x"$1" = x ] || [ $# -gt 2 ]; then
		print -u2 'substring: bad argument count'
		return 1
	fi
	str=$1
	if [ x"$flag" = x-l ]; then		#substring -l lpat
		str=${str#$lpat}
	elif [ x"$flag" = x-L ]; then
		str=${str##$lpat}		#substring -L lpat
	fi

	if [ x"$2" != x ]; then
		echo ${str%$2}
	else
		echo $str
	fi

	return 0
}