1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-02-24 23:14:14 +00:00
cde/src/cmd/ksh93/tests/grep.sh
Martijn Dekker d7afb57c49 shtests: use central temporary directory; add --keep option
This gets rid of repetitive code in test scripts to create their
own temporary directories. Instead, shtests exports a $tmp to each
test script that is a subdirectory of its own temporary directory.
This has the advantage of having all test script temporary
directories in one hierarchy. Along with a new option to keep
temporary files, this makes it easy to inspect them if wanted.

This does make the test scripts less self-contained as they now
depend on a temporary directory being exported as $tmp. But they
already depended on $SHELL being the shell to test, so they already
were not quite self-contained.

src/cmd/ksh93/tests/shtests:
- Add -k/--keep option to keep temporary directory. Make the EXIT
  trap report its location instead of deleting it.
- For each test, create a subdirectory of $tmp (named after the
  test script plus the tested locale or 'shcomp') and export that
  subdirectory to the test script as its own $tmp.
- If -k is not given, delete each script's temporary files
  immediately after running it to minimise disk usage.

src/cmd/ksh93/tests/*.sh:
- Don't make own temp directory.
- Refuse to run if $tmp is not set.
- Miscellaneous tweaks.
2020-07-04 01:28:08 +02:00

104 lines
3.3 KiB
Bash
Executable file

########################################################################
# #
# This software is part of the ast package #
# Copyright (c) 1982-2011 AT&T Intellectual Property #
# and is licensed under the #
# Eclipse Public License, Version 1.0 #
# by AT&T Intellectual Property #
# #
# A copy of the License is available at #
# http://www.eclipse.org/org/documents/epl-v10.html #
# (with md5 checksum b35adb5213ca9657e911e9befb180842) #
# #
# Information and Software Systems Research #
# AT&T Research #
# Florham Park NJ #
# #
# David Korn <dgk@research.att.com> #
# #
########################################################################
function err_exit
{
print -u2 -n "\t"
print -u2 -r ${Command}[$1]: "${@:2}"
let Errors+=1
}
alias err_exit='err_exit $LINENO'
Command=${0##*/}
integer Errors=0
[[ -d $tmp && -w $tmp ]] || { err\_exit "$LINENO" '$tmp not set; run this from shtests. Aborting.'; exit 1; }
function grep
{
#
# SHELL VERSION OF GREP
#
vflag= xflag= cflag= lflag= nflag=
set -f
while ((1)) # look for grep options
do case "$1" in
-v*) vflag=1;;
-x*) xflag=1;;
-c*) cflag=1;;
-l*) lflag=1;;
-n*) nflag=1;;
-b*) print 'b option not supported';;
-e*) shift;expr="$1";;
-f*) shift;expr=$(< $1);;
-*) print $0: 'unknown flag';return 2;;
*)
if test "$expr" = ''
then expr="$1";shift
fi
test "$xflag" || expr="*${expr}*"
break;;
esac
shift # next argument
done
noprint=$vflag$cflag$lflag # don't print if these flags are set
integer n=0 c=0 tc=0 nargs=$# # initialize counters
for i in "$@" # go thru the files
do if ((nargs<=1))
then fname=''
else fname="$i":
fi
test "$i" && exec 0< $i # open file if necessary
while read -r line # read in a line
do let n=n+1
case "$line" in
$expr) # line matches pattern
test "$noprint" || print -r -- "$fname${nflag:+$n:}$line"
let c=c+1 ;;
*) # not a match
if test "$vflag"
then print -r -- "$fname${nflag:+$n:}$line"
fi;;
esac
done
if test "$lflag" && ((c))
then print -r -- "$i"
fi
let tc=tc+c n=0 c=0
done
test "$cflag" && print $tc # print count if cflag is set
let tc # set the return value
}
cat > $tmp/grep <<\!
this is a food bar test
to see how many lines find both foo and bar.
Some line contain foo only,
and some lines contain bar only.
However, many lines contain both foo and also bar.
A line containing foobar should also be counted.
There should be six lines with foo and bar.
There are only two line with out foo but with bar.
!
if (( $(grep -c 'foo*bar' $tmp/grep ) != 6))
then err_exit
fi
exit $((Errors<125?Errors:125))