1da2e3ebchin########################################################################
2da2e3ebchin#                                                                      #
3da2e3ebchin#               This software is part of the ast package               #
43e14f97Roger A. Faulkner#          Copyright (c) 1982-2010 AT&T Intellectual Property          #
5da2e3ebchin#                      and is licensed under the                       #
6da2e3ebchin#                  Common Public License, Version 1.0                  #
77c2fbfbApril Chin#                    by AT&T Intellectual Property                     #
8da2e3ebchin#                                                                      #
9da2e3ebchin#                A copy of the License is available at                 #
10da2e3ebchin#            http://www.opensource.org/licenses/cpl1.0.txt             #
11da2e3ebchin#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
12da2e3ebchin#                                                                      #
13da2e3ebchin#              Information and Software Systems Research               #
14da2e3ebchin#                            AT&T Research                             #
15da2e3ebchin#                           Florham Park NJ                            #
16da2e3ebchin#                                                                      #
17da2e3ebchin#                  David Korn <dgk@research.att.com>                   #
18da2e3ebchin#                                                                      #
19da2e3ebchin########################################################################
20da2e3ebchinfunction err_exit
21da2e3ebchin{
22da2e3ebchin	print -u2 -n "\t"
23da2e3ebchin	print -u2 -r ${Command}[$1]: "${@:2}"
24da2e3ebchin	let Errors+=1
25da2e3ebchin}
26da2e3ebchinalias err_exit='err_exit $LINENO'
27da2e3ebchin
28da2e3ebchinCommand=${0##*/}
29da2e3ebchininteger Errors=0
30da2e3ebchin
3134f9b3eRoland Mainztmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; }
3234f9b3eRoland Mainztrap "cd /; rm -rf $tmp" EXIT
3334f9b3eRoland Mainz
34da2e3ebchinfunction grep
35da2e3ebchin{
36da2e3ebchin	#
37da2e3ebchin	#	SHELL VERSION OF GREP
38da2e3ebchin	#
39da2e3ebchin	vflag= xflag= cflag= lflag= nflag=
40da2e3ebchin	set -f
41da2e3ebchin	while	((1))				# look for grep options
42da2e3ebchin	do	case	"$1" in
43da2e3ebchin		-v*)	vflag=1;;
44da2e3ebchin		-x*)	xflag=1;;
45da2e3ebchin		-c*)	cflag=1;;
46da2e3ebchin		-l*)	lflag=1;;
47da2e3ebchin		-n*)	nflag=1;;
48da2e3ebchin		-b*)	print 'b option not supported';;
49da2e3ebchin		-e*)	shift;expr="$1";;
50da2e3ebchin		-f*)	shift;expr=$(< $1);;
51da2e3ebchin		-*)	print $0: 'unknown flag';return 2;;
52da2e3ebchin		*)
53da2e3ebchin			if	test "$expr" = ''
54da2e3ebchin			then	expr="$1";shift
55da2e3ebchin			fi
56da2e3ebchin			test "$xflag" || expr="*${expr}*"
57da2e3ebchin			break;;
58da2e3ebchin		esac
59da2e3ebchin		shift				# next argument
60da2e3ebchin	done
61da2e3ebchin	noprint=$vflag$cflag$lflag		# don't print if these flags are set
62da2e3ebchin	integer n=0 c=0 tc=0 nargs=$#		# initialize counters
63da2e3ebchin	for i in "$@"				# go thru the files
64da2e3ebchin	do	if	((nargs<=1))
65da2e3ebchin		then	fname=''
66da2e3ebchin		else	fname="$i":
67da2e3ebchin		fi
68da2e3ebchin		test "$i"  &&  exec 0< $i	# open file if necessary
69da2e3ebchin		while	read -r line		# read in a line
70da2e3ebchin		do	let n=n+1
71da2e3ebchin			case	"$line" in
72da2e3ebchin			$expr)			# line matches pattern
73da2e3ebchin				test "$noprint" || print -r -- "$fname${nflag:+$n:}$line"
74da2e3ebchin				let c=c+1 ;;
75da2e3ebchin			*)			# not a match
76da2e3ebchin				if	test "$vflag"
77da2e3ebchin				then	print -r -- "$fname${nflag:+$n:}$line"
78da2e3ebchin				fi;;
79da2e3ebchin			esac
80da2e3ebchin		done
81da2e3ebchin		if	test "$lflag" && ((c))
82da2e3ebchin		then	print -r -- "$i"
83da2e3ebchin		fi
84da2e3ebchin		let tc=tc+c n=0 c=0
85da2e3ebchin	done
86da2e3ebchin	test "$cflag" && print $tc		#  print count if cflag is set
87da2e3ebchin	let tc					#  set the return value
88da2e3ebchin}
89da2e3ebchin
9034f9b3eRoland Mainzcat > $tmp/grep <<\!
91da2e3ebchinthis is a food bar test
92da2e3ebchinto see how many lines find both foo and bar.
93da2e3ebchinSome line contain foo only,
94da2e3ebchinand some lines contain bar only.
95da2e3ebchinHowever, many lines contain both foo and also bar.
96da2e3ebchinA line containing foobar should also be counted.
97da2e3ebchinThere should be six lines with foo and bar.
98da2e3ebchinThere are only two line with out foo but with bar.
99da2e3ebchin!
100da2e3ebchin
10134f9b3eRoland Mainzif	(( $(grep -c 'foo*bar' $tmp/grep ) != 6))
102da2e3ebchinthen	err_exit
103da2e3ebchinfi
104da2e3ebchinexit $((Errors))
105