14887Schin########################################################################
24887Schin#                                                                      #
34887Schin#               This software is part of the ast package               #
4*8462SApril.Chin@Sun.COM#          Copyright (c) 1982-2008 AT&T Intellectual Property          #
54887Schin#                      and is licensed under the                       #
64887Schin#                  Common Public License, Version 1.0                  #
7*8462SApril.Chin@Sun.COM#                    by AT&T Intellectual Property                     #
84887Schin#                                                                      #
94887Schin#                A copy of the License is available at                 #
104887Schin#            http://www.opensource.org/licenses/cpl1.0.txt             #
114887Schin#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
124887Schin#                                                                      #
134887Schin#              Information and Software Systems Research               #
144887Schin#                            AT&T Research                             #
154887Schin#                           Florham Park NJ                            #
164887Schin#                                                                      #
174887Schin#                  David Korn <dgk@research.att.com>                   #
184887Schin#                                                                      #
194887Schin########################################################################
204887Schinfunction err_exit
214887Schin{
224887Schin	print -u2 -n "\t"
234887Schin	print -u2 -r ${Command}[$1]: "${@:2}"
244887Schin	let Errors+=1
254887Schin}
264887Schinalias err_exit='err_exit $LINENO'
274887Schin
284887SchinCommand=${0##*/}
294887Schininteger Errors=0
304887Schin
314887Schinfunction grep
324887Schin{
334887Schin	#
344887Schin	#	SHELL VERSION OF GREP
354887Schin	#
364887Schin	vflag= xflag= cflag= lflag= nflag=
374887Schin	set -f
384887Schin	while	((1))				# look for grep options
394887Schin	do	case	"$1" in
404887Schin		-v*)	vflag=1;;
414887Schin		-x*)	xflag=1;;
424887Schin		-c*)	cflag=1;;
434887Schin		-l*)	lflag=1;;
444887Schin		-n*)	nflag=1;;
454887Schin		-b*)	print 'b option not supported';;
464887Schin		-e*)	shift;expr="$1";;
474887Schin		-f*)	shift;expr=$(< $1);;
484887Schin		-*)	print $0: 'unknown flag';return 2;;
494887Schin		*)
504887Schin			if	test "$expr" = ''
514887Schin			then	expr="$1";shift
524887Schin			fi
534887Schin			test "$xflag" || expr="*${expr}*"
544887Schin			break;;
554887Schin		esac
564887Schin		shift				# next argument
574887Schin	done
584887Schin	noprint=$vflag$cflag$lflag		# don't print if these flags are set
594887Schin	integer n=0 c=0 tc=0 nargs=$#		# initialize counters
604887Schin	for i in "$@"				# go thru the files
614887Schin	do	if	((nargs<=1))
624887Schin		then	fname=''
634887Schin		else	fname="$i":
644887Schin		fi
654887Schin		test "$i"  &&  exec 0< $i	# open file if necessary
664887Schin		while	read -r line		# read in a line
674887Schin		do	let n=n+1
684887Schin			case	"$line" in
694887Schin			$expr)			# line matches pattern
704887Schin				test "$noprint" || print -r -- "$fname${nflag:+$n:}$line"
714887Schin				let c=c+1 ;;
724887Schin			*)			# not a match
734887Schin				if	test "$vflag"
744887Schin				then	print -r -- "$fname${nflag:+$n:}$line"
754887Schin				fi;;
764887Schin			esac
774887Schin		done
784887Schin		if	test "$lflag" && ((c))
794887Schin		then	print -r -- "$i"
804887Schin		fi
814887Schin		let tc=tc+c n=0 c=0
824887Schin	done
834887Schin	test "$cflag" && print $tc		#  print count if cflag is set
844887Schin	let tc					#  set the return value
854887Schin}
864887Schin
874887Schintrap 'rm -f /tmp/grep$$' EXIT
884887Schincat > /tmp/grep$$ <<\!
894887Schinthis is a food bar test
904887Schinto see how many lines find both foo and bar.
914887SchinSome line contain foo only,
924887Schinand some lines contain bar only.
934887SchinHowever, many lines contain both foo and also bar.
944887SchinA line containing foobar should also be counted.
954887SchinThere should be six lines with foo and bar.
964887SchinThere are only two line with out foo but with bar.
974887Schin!
984887Schin
994887Schinif	(( $(grep -c 'foo*bar' /tmp/grep$$ ) != 6))
1004887Schinthen	err_exit
1014887Schinfi
1024887Schinexit $((Errors))
103