pkgsrc-wip/pkg_summary-utils/files/pkg_grep_summary

115 lines
2.4 KiB
Text
Raw Normal View History

#!/bin/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
2008-05-07 13:03:33 +02:00
usage (){
cat 1>&2 <<EOF
2008-05-07 20:42:06 +02:00
pkg_grep_summary - grep pkg_summary(5) input (stdin) using AWK
condition, entire summaries of found packages are output, that is
pkg_grep_summary is not per-line but per-package search tool.
2008-05-07 13:03:33 +02:00
usage: pkg_grep_summary <FIELD> <AWK_CONDITION>
examples:
pkg_grep_summary 'PKGNAME' '$2 ~ /judy/' \
2008-05-07 20:42:06 +02:00
< /usr/pkgsrc/packages/pkg_summary.txt
2008-05-07 13:03:33 +02:00
pkg_grep_summary 'PKGPATH' '$2 ~ /^lang\//' \
2008-05-07 20:42:06 +02:00
< /usr/pkgsrc/packages/pkg_summary.txt
2008-05-07 13:03:33 +02:00
pkg_grep_summary 'DEPENDS' '$2 ~ /libX/' \
2008-05-07 20:42:06 +02:00
< /usr/pkgsrc/pkg_src_summary.txt
2008-05-07 13:03:33 +02:00
EOF
}
while test $# -ne 0; do
case "$1" in
-h|--help)
usage
exit 0;;
--)
shift
break;;
-*)
echo "Bad option $1" 1>&2
exit 1;;
*)
break
esac
shift
done
test $# -eq 2
field=$1
true_condition="$2"
shift
shift
awk -v field="$field" '
BEGIN {
FS = "="
skip = -1 # -1 - unknown, 0 - false, 1 - true
count = 0
}
function update_skip (){
skip = !('"$true_condition"')
if (skip == 0){
for (i=0; i < count; ++i){
print accu [i]
}
}
delete accu
count = 0
}
skip == 1 && NF > 0 {
next
}
$1 == field {
update_skip()
}
skip == 0 && NF > 0 {
print $0
next
}
skip == -1 && NF > 0 {
accu [count++] = $0
}
NF == 0 {
if (skip == 0){
print ""
}
delete accu
count = 0
skip = -1
}
'