pkgsrc-wip/pkg_summary-utils/files/pkg_grep_summary.in
Aleksey Cheusov a9a9f884f6 pkg_grep_summary:
- new search strategy "kw" (or "keywords") for matching all words
    within query.
2010-05-30 18:21:26 +00:00

190 lines
5 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
export LC_ALL=C
usage (){
cat 1>&2 <<EOF
pkg_grep_summary - output summaries about packages that matches
the specified condition, summaries are read from stdin.
USAGE: pkg_grep_summary -h
pkg_grep_summary -e <field>
pkg_grep_summary [-i] -m <field> <regexp>
pkg_grep_summary [-i] -s <field> <string>
pkg_grep_summary [-i] -S <field> <file>
pkg_grep_summary [-i] -t <strategy> <field> <condition>
pkg_grep_summary -T
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
<strategy> - Search strategy. The following strategies are supported:
exact, prefix, suffix, substring, word (separate words within
fields are matched), re (extended regular expresion), first (only
first word is matched), last (only last word is matched)
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
-t matches <field> using search strategy <strategy>
and condition specified in <condition>
-T displays all search strategies available for -t and exits
-i ignore case
EOF
}
while test $# -ne 0; do
case "$1" in
-h|--help)
usage
exit 0;;
-e|--empty)
strategy=empty
field=$2
shift;;
-s)
strategy=exact
field=$2
string=$3
shift
shift;;
-S)
strategy=strfile
field=$2
string=$3
shift
shift;;
-m)
strategy=re
field=$2
string=$3
shift
shift;;
-t)
strategy=$2
field=$3
string=$4
shift
shift
shift;;
-T)
echo 'exact prefix suffix substring word first last re strfile awk empty kw'
exit 0;;
-i)
ic=1;;
--)
shift
break;;
-*)
echo "Bad option $1" 1>&2
exit 1;;
*)
break
esac
shift
done
if test $# -eq 2 -a -z "$strategy"; then
strategy=awk
field="$1"
string="$2"
shift
shift
elif test $# -eq 0; then
true
else
usage
exit 1
fi
case "$strategy" in
exact)
condition="fvalue == \"$string\"";;
prefix)
condition="has_prefix(fvalue, \"$string\")";;
suffix)
condition="has_suffix(fvalue, \"$string\")";;
substring)
condition="index(fvalue, \"$string\")";;
first)
condition="match_first_word(fvalue, \"$string\")";;
last)
condition="match_last_word(fvalue, \"$string\")";;
word)
condition="match_word(fvalue, \"$string\")";;
re)
re=`echo $string | sed 's|/|\\\/|g'`
condition="fvalue ~ /$re/";;
strfile)
condition="fvalue in strings";;
awk)
condition="$string";;
empty)
condition='fvalue == ""';;
kw|keywords)
strategy=kw
condition="match_keywords(fvalue)";;
*)
echo "Unknown search strategy: $strategy" 1>&2
exit 1;;
esac
runawk -v ic="$ic" \
-v string="$string" -v strategy="$strategy" -e '
#use "pkg_grep_summary.awk"
#use "xgetline.awk"
#use "str2regexp.awk"
BEGIN {
if (strategy == "strfile"){
while(xgetline0(string)){
strings [$0] = 1
}
close(string)
}else if (strategy == "kw"){
cnt_kw = split(string, arr_kw)
for (i=1; i <= cnt_kw; ++i){
if (i > 1)
re_kw = re_kw "|"
re_kw = re_kw str2regexp(arr_kw [i])
}
re_kw = "(" re_kw ")"
}
grep_summary__field="'"$field"'"
keep_fields = (grep_summary__field == "" || grep_summary__field == ".")
}
function grep_summary__condition (){
return '"$condition"'
}
'