1*4887Schin########################################################################
2*4887Schin#                                                                      #
3*4887Schin#               This software is part of the ast package               #
4*4887Schin#           Copyright (c) 1982-2007 AT&T Knowledge Ventures            #
5*4887Schin#                      and is licensed under the                       #
6*4887Schin#                  Common Public License, Version 1.0                  #
7*4887Schin#                      by AT&T Knowledge Ventures                      #
8*4887Schin#                                                                      #
9*4887Schin#                A copy of the License is available at                 #
10*4887Schin#            http://www.opensource.org/licenses/cpl1.0.txt             #
11*4887Schin#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
12*4887Schin#                                                                      #
13*4887Schin#              Information and Software Systems Research               #
14*4887Schin#                            AT&T Research                             #
15*4887Schin#                           Florham Park NJ                            #
16*4887Schin#                                                                      #
17*4887Schin#                  David Korn <dgk@research.att.com>                   #
18*4887Schin#                                                                      #
19*4887Schin########################################################################
20*4887Schinfunction err_exit
21*4887Schin{
22*4887Schin	print -u2 -n "\t"
23*4887Schin	print -u2 -r ${Command}[$1]: "${@:2}"
24*4887Schin	let Errors+=1
25*4887Schin}
26*4887Schinalias err_exit='err_exit $LINENO'
27*4887Schin
28*4887SchinCommand=${0##*/}
29*4887Schininteger Errors=0
30*4887Schin
31*4887Schinfunction grep
32*4887Schin{
33*4887Schin	#
34*4887Schin	#	SHELL VERSION OF GREP
35*4887Schin	#
36*4887Schin	vflag= xflag= cflag= lflag= nflag=
37*4887Schin	set -f
38*4887Schin	while	((1))				# look for grep options
39*4887Schin	do	case	"$1" in
40*4887Schin		-v*)	vflag=1;;
41*4887Schin		-x*)	xflag=1;;
42*4887Schin		-c*)	cflag=1;;
43*4887Schin		-l*)	lflag=1;;
44*4887Schin		-n*)	nflag=1;;
45*4887Schin		-b*)	print 'b option not supported';;
46*4887Schin		-e*)	shift;expr="$1";;
47*4887Schin		-f*)	shift;expr=$(< $1);;
48*4887Schin		-*)	print $0: 'unknown flag';return 2;;
49*4887Schin		*)
50*4887Schin			if	test "$expr" = ''
51*4887Schin			then	expr="$1";shift
52*4887Schin			fi
53*4887Schin			test "$xflag" || expr="*${expr}*"
54*4887Schin			break;;
55*4887Schin		esac
56*4887Schin		shift				# next argument
57*4887Schin	done
58*4887Schin	noprint=$vflag$cflag$lflag		# don't print if these flags are set
59*4887Schin	integer n=0 c=0 tc=0 nargs=$#		# initialize counters
60*4887Schin	for i in "$@"				# go thru the files
61*4887Schin	do	if	((nargs<=1))
62*4887Schin		then	fname=''
63*4887Schin		else	fname="$i":
64*4887Schin		fi
65*4887Schin		test "$i"  &&  exec 0< $i	# open file if necessary
66*4887Schin		while	read -r line		# read in a line
67*4887Schin		do	let n=n+1
68*4887Schin			case	"$line" in
69*4887Schin			$expr)			# line matches pattern
70*4887Schin				test "$noprint" || print -r -- "$fname${nflag:+$n:}$line"
71*4887Schin				let c=c+1 ;;
72*4887Schin			*)			# not a match
73*4887Schin				if	test "$vflag"
74*4887Schin				then	print -r -- "$fname${nflag:+$n:}$line"
75*4887Schin				fi;;
76*4887Schin			esac
77*4887Schin		done
78*4887Schin		if	test "$lflag" && ((c))
79*4887Schin		then	print -r -- "$i"
80*4887Schin		fi
81*4887Schin		let tc=tc+c n=0 c=0
82*4887Schin	done
83*4887Schin	test "$cflag" && print $tc		#  print count if cflag is set
84*4887Schin	let tc					#  set the return value
85*4887Schin}
86*4887Schin
87*4887Schintrap 'rm -f /tmp/grep$$' EXIT
88*4887Schincat > /tmp/grep$$ <<\!
89*4887Schinthis is a food bar test
90*4887Schinto see how many lines find both foo and bar.
91*4887SchinSome line contain foo only,
92*4887Schinand some lines contain bar only.
93*4887SchinHowever, many lines contain both foo and also bar.
94*4887SchinA line containing foobar should also be counted.
95*4887SchinThere should be six lines with foo and bar.
96*4887SchinThere are only two line with out foo but with bar.
97*4887Schin!
98*4887Schin
99*4887Schinif	(( $(grep -c 'foo*bar' /tmp/grep$$ ) != 6))
100*4887Schinthen	err_exit
101*4887Schinfi
102*4887Schinexit $((Errors))
103