xref: /titanic_51/usr/src/lib/libshell/common/tests/grep.sh (revision 37714ae43602c675f9dc59b070bfdf9fa702872c)
1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#          Copyright (c) 1982-2008 AT&T Intellectual Property          #
5#                      and is licensed under the                       #
6#                  Common Public License, Version 1.0                  #
7#                    by AT&T Intellectual Property                     #
8#                                                                      #
9#                A copy of the License is available at                 #
10#            http://www.opensource.org/licenses/cpl1.0.txt             #
11#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
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
31function grep
32{
33	#
34	#	SHELL VERSION OF GREP
35	#
36	vflag= xflag= cflag= lflag= nflag=
37	set -f
38	while	((1))				# look for grep options
39	do	case	"$1" in
40		-v*)	vflag=1;;
41		-x*)	xflag=1;;
42		-c*)	cflag=1;;
43		-l*)	lflag=1;;
44		-n*)	nflag=1;;
45		-b*)	print 'b option not supported';;
46		-e*)	shift;expr="$1";;
47		-f*)	shift;expr=$(< $1);;
48		-*)	print $0: 'unknown flag';return 2;;
49		*)
50			if	test "$expr" = ''
51			then	expr="$1";shift
52			fi
53			test "$xflag" || expr="*${expr}*"
54			break;;
55		esac
56		shift				# next argument
57	done
58	noprint=$vflag$cflag$lflag		# don't print if these flags are set
59	integer n=0 c=0 tc=0 nargs=$#		# initialize counters
60	for i in "$@"				# go thru the files
61	do	if	((nargs<=1))
62		then	fname=''
63		else	fname="$i":
64		fi
65		test "$i"  &&  exec 0< $i	# open file if necessary
66		while	read -r line		# read in a line
67		do	let n=n+1
68			case	"$line" in
69			$expr)			# line matches pattern
70				test "$noprint" || print -r -- "$fname${nflag:+$n:}$line"
71				let c=c+1 ;;
72			*)			# not a match
73				if	test "$vflag"
74				then	print -r -- "$fname${nflag:+$n:}$line"
75				fi;;
76			esac
77		done
78		if	test "$lflag" && ((c))
79		then	print -r -- "$i"
80		fi
81		let tc=tc+c n=0 c=0
82	done
83	test "$cflag" && print $tc		#  print count if cflag is set
84	let tc					#  set the return value
85}
86
87trap 'rm -f /tmp/grep$$' EXIT
88cat > /tmp/grep$$ <<\!
89this is a food bar test
90to see how many lines find both foo and bar.
91Some line contain foo only,
92and some lines contain bar only.
93However, many lines contain both foo and also bar.
94A line containing foobar should also be counted.
95There should be six lines with foo and bar.
96There are only two line with out foo but with bar.
97!
98
99if	(( $(grep -c 'foo*bar' /tmp/grep$$ ) != 6))
100then	err_exit
101fi
102exit $((Errors))
103