xref: /csrg-svn/local/toolchest/ksh/substring (revision 35122)
1*35122Smarc# substring function
2*35122Smarc# this function should be equivalent to the substring built-in which was
3*35122Smarc# eliminated after the 06/29/84 version
4*35122Smarcfunction substring
5*35122Smarc{
6*35122Smarc	typeset lpat flag str	#local variables
7*35122Smarc	set -o noglob		#no file name generation
8*35122Smarc	case $1 in
9*35122Smarc	-l|-L)
10*35122Smarc		flag=$1
11*35122Smarc		lpat=$2
12*35122Smarc		shift 2
13*35122Smarc		;;
14*35122Smarc	esac
15*35122Smarc	# test for too few or too many arguments
16*35122Smarc	if	[ x"$1" = x -o $# -gt 2 ]
17*35122Smarc	then	print -u2 'substring: bad argument count'
18*35122Smarc		return 1
19*35122Smarc	fi
20*35122Smarc	str=$1
21*35122Smarc	if	[ x"$flag" = x-l ]		#substring -l lpat
22*35122Smarc	then	str=${str#$lpat}
23*35122Smarc	elif	[ x"$flag" = x-L ]
24*35122Smarc	then	str=${str##$lpat}		#substring -L lpat
25*35122Smarc	fi
26*35122Smarc	if	[ x"$2" != x ]
27*35122Smarc	then	echo ${str%$2}
28*35122Smarc	else	echo $str
29*35122Smarc	fi
30*35122Smarc	return 0
31*35122Smarc}
32