pkgsrc-wip/pkg_summary-utils/files/pkg_grep_summary.in
Aleksey Cheusov 3c6dd17086 pkg_grep_summary:
- update manual page
   - fix for PKGPATHe
   - ++patchlevel
   - one more regression test
2010-03-27 21:45:33 +00:00

127 lines
3.2 KiB
Bash
Executable file

#!@SH@
#-*-mode: sh -*-
# Copyright (c) 2007-2008 Aleksey Cheusov <vle@gmx.net>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
set -e
usage (){
cat 1>&2 <<EOF
pkg_grep_summary - output summaries about packages that matches
the specified condition.
USAGE: pkg_grep_summary -h
pkg_grep_summary -e <field>
pkg_grep_summary -m <field> <regexp>
pkg_grep_summary -s <field> <string>
pkg_grep_summary -S <field> <file>
pkg_grep_summary <field> <awk_condition>
<field> - PKGBASE, PKGNAME, PKGPATH, DEPENDS etc.
<awk_condition> - boolean expression written in AWK language
<regexp> - AWK regular expression
<string> - text string
OPTIONS:
-h|--help display this message
-e|--empty matches empty and absent fields only
(equivalent to '<field>' 'fvalue == ""')
-m matches <field> against regular expression <regexp>
-s matches <field> equal to <string>
-S matches <field> equal to any string in <file>,
one string per line
EOF
}
while test $# -ne 0; do
case "$1" in
-h|--help)
usage
exit 0;;
-e|--empty)
empty=1
field=$2
shift;;
-s)
field=$2
string=$3
shift
shift;;
-S)
field=$2
strings_fn=$3
shift
shift;;
-m)
field=$2
regex=$3
shift
shift;;
--)
shift
break;;
-*)
echo "Bad option $1" 1>&2
exit 1;;
*)
break
esac
shift
done
if test -n "$empty"; then
condition='fvalue == ""'
elif test -n "$string"; then
condition="fvalue == \"$string\""
elif test -n "$strings_fn"; then
condition="fvalue in strings"
elif test -n "$regex"; then
re=`echo $regex | sed 's|/|\\\/|g'`
condition="fvalue ~ /$re/"
elif test $# -eq 2; then
field="$1"
condition="$2"
shift
shift
fi
if test -z "$condition" -o $# -ne 0; then
usage
exit 1
fi
runawk -v strings_fn="$strings_fn" -e '
#use "pkg_grep_summary.awk"
#use "xgetline.awk"
BEGIN {
if (strings_fn != ""){
while(xgetline0(strings_fn)){
strings [$0] = 1
}
}
grep_summary__field="'"$field"'"
}
function grep_summary__condition (){
return '"$condition"'
}
'