xref: /illumos-gate/usr/src/contrib/ast/src/cmd/ksh93/tests/grep.sh (revision b30d193948be5a7794d7ae3ba0ed9c2f72c88e0f)
1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#          Copyright (c) 1982-2011 AT&T Intellectual Property          #
5#                      and is licensed under the                       #
6#                 Eclipse Public License, Version 1.0                  #
7#                    by AT&T Intellectual Property                     #
8#                                                                      #
9#                A copy of the License is available at                 #
10#          http://www.eclipse.org/org/documents/epl-v10.html           #
11#         (with md5 checksum b35adb5213ca9657e911e9befb180842)         #
12#                                                                      #
13#              Information and Software Systems Research               #
14#                            AT&T Research                             #
15#                           Florham Park NJ                            #
16#                                                                      #
17#                  David Korn <dgk@research.att.com>                   #
18#                                                                      #
19########################################################################
20function err_exit
21{
22	print -u2 -n "\t"
23	print -u2 -r ${Command}[$1]: "${@:2}"
24	let Errors+=1
25}
26alias err_exit='err_exit $LINENO'
27
28Command=${0##*/}
29integer Errors=0
30
31tmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; }
32trap "cd /; rm -rf $tmp" EXIT
33
34function grep
35{
36	#
37	#	SHELL VERSION OF GREP
38	#
39	vflag= xflag= cflag= lflag= nflag=
40	set -f
41	while	((1))				# look for grep options
42	do	case	"$1" in
43		-v*)	vflag=1;;
44		-x*)	xflag=1;;
45		-c*)	cflag=1;;
46		-l*)	lflag=1;;
47		-n*)	nflag=1;;
48		-b*)	print 'b option not supported';;
49		-e*)	shift;expr="$1";;
50		-f*)	shift;expr=$(< $1);;
51		-*)	print $0: 'unknown flag';return 2;;
52		*)
53			if	test "$expr" = ''
54			then	expr="$1";shift
55			fi
56			test "$xflag" || expr="*${expr}*"
57			break;;
58		esac
59		shift				# next argument
60	done
61	noprint=$vflag$cflag$lflag		# don't print if these flags are set
62	integer n=0 c=0 tc=0 nargs=$#		# initialize counters
63	for i in "$@"				# go thru the files
64	do	if	((nargs<=1))
65		then	fname=''
66		else	fname="$i":
67		fi
68		test "$i"  &&  exec 0< $i	# open file if necessary
69		while	read -r line		# read in a line
70		do	let n=n+1
71			case	"$line" in
72			$expr)			# line matches pattern
73				test "$noprint" || print -r -- "$fname${nflag:+$n:}$line"
74				let c=c+1 ;;
75			*)			# not a match
76				if	test "$vflag"
77				then	print -r -- "$fname${nflag:+$n:}$line"
78				fi;;
79			esac
80		done
81		if	test "$lflag" && ((c))
82		then	print -r -- "$i"
83		fi
84		let tc=tc+c n=0 c=0
85	done
86	test "$cflag" && print $tc		#  print count if cflag is set
87	let tc					#  set the return value
88}
89
90cat > $tmp/grep <<\!
91this is a food bar test
92to see how many lines find both foo and bar.
93Some line contain foo only,
94and some lines contain bar only.
95However, many lines contain both foo and also bar.
96A line containing foobar should also be counted.
97There should be six lines with foo and bar.
98There are only two line with out foo but with bar.
99!
100
101if	(( $(grep -c 'foo*bar' $tmp/grep ) != 6))
102then	err_exit
103fi
104
105exit $((Errors<125?Errors:125))
106