1#!/bin/sh
2
3#-
4# Copyright 2006 Ricardo A. Reis ricardo.areis@gmail.com
5# All rights reserved
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted providing that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26# POSSIBILITY OF SUCH DAMAGE.
27
28SEARCH_PATH=`kldconfig -r |tr ';' ' '`
29EXIT_ERROR="1"
30EXIT_OK="0"
31EXIT_USAGE="64"
32LOCAL_VERSION="0.61"
33
34usage()
35{
36cat <<EOF
37usage: `basename $0` [-qv] -c category ...
38       `basename $0` [-qv] -s string   ...
39       `basename $0`  -h
40EOF
41exit $EXIT_USAGE
42}
43
44help()
45{
46cat <<EOF
47kldfind Version $LOCAL_VERSION
48Options:
49  -c	--  find all matchs for category
50  -s	--  string match
51  -v    --  verbose 
52  -q    --  quiet  
53EOF
54exit $EXIT_USAGE
55}
56output()
57{
58OUT="$1"
59RESULT="$2"
60SEARCH_PATH="$3"
61NUMBER_R=`echo $RESULT | tr ' ' '\n' |grep .|wc -l|sed -e 's/ //g'`
62if [ "$OUT" = "VERBOSE" ];then
63    if [ $NUMBER_R -ne 0 ];then
64        cat <<EOF
65
66
67====>   Search finished, $NUMBER_R resultes for $SEARCH_PATH
68
69EOF
70printf  "\n%-15s \t\t%s" "       KLD" "    Description"
71printf  "\n%-15s \t\t%s" "       ---" "    -----------"
72echo ""
73    for _result in $RESULT;do
74        if man ${_result} >/dev/null 2>&1;then
75            SHORT_DESC=`man ${_result} |col -b|awk '/^NAME/,/-/ { gsub(/^.* [-]+ |(  )+|^[ ]+|--$/,"") ; print  }'|\
76                tail -n1 |tr '\n' ' ' 2>&1`
77        else
78            SHORT_DESC="No manual entry for ${_result}"
79        fi
80        printf  "\n%-15s \t\t%s" "===>   $_result," "--- $SHORT_DESC"
81    done
82   	    return $EXIT_OK
83    fi
84fi
85if [ ! -z "$RESULT" -a -z $SEARCH_PATH  ];then
86    if   [ "$OUT" = "SHORT" ];then
87            echo "$RESULT"
88    	    exit  $EXIT_OK
89    elif [ "$OUT" = "QUIET" ];then
90            exit  $EXIT_OK
91    fi
92else
93    exit $EXIT_ERROR
94fi
95}
96
97search()
98{
99OUT="$1";shift
100TYPE="$1";shift
101STRING="$1";shift
102if [ $OUT = "QUIET" ];then
103    if [ -d $_search_path ];then
104        if   [ $TYPE = "c" ];then
105            KLD=`find ${SEARCH_PATH} -iregex ".*/${STRING}_.*" -print | sed -e 's#^.*/## ; s#.ko##'`
106        elif [ $TYPE = "s" ];then
107            KLD=`find ${SEARCH_PATH} -iname  "*${STRING}*" -print | sed -e 's#^.*/## ; s#.ko##'`
108        fi
109			output $OUT "$KLD"
110    fi
111fi
112if [ $OUT = "SHORT" ];then
113    if [ -d $_search_path ];then
114        if   [ $TYPE = "c" ];then
115            KLD=`find ${SEARCH_PATH}  -iregex  ".*/${STRING}_.*" -print`
116        elif [ $TYPE = "s" ];then
117            KLD=`find ${SEARCH_PATH} -iname   "*${STRING}*" -print`
118        fi
119			output $OUT "$KLD"
120    fi
121fi
122for _search_path in ${SEARCH_PATH};do
123	if [ -d $_search_path ];then
124		if   [ $TYPE = "c" ];then
125			KLD=`find $_search_path -iregex  ".*/${STRING}_.*" -print | sed -e 's#^.*/## ; s#.ko##'`
126		elif [ $TYPE = "s" ];then
127			KLD=`find $_search_path -iname   "*${STRING}*" -print | sed -e 's#^.*/## ; s#.ko##'`
128		fi
129			output $OUT "$KLD" "$_search_path"
130	fi
131done
132}
133set_out()
134{
135# if output option is set after c or s options
136#getopt failt in set default output.
137THIRD_ARG=${1}
138
139if [ ! -z $THIRD_ARG ];then
140    if   [ ${THIRD_ARG} != "-v" -a ${THIRD_ARG} != "-q" ];then
141        VERBOSE=${VERBOSE:-0}
142        QUIET=${QUIET:-0}
143	elif [ ${THIRD_ARG}  = "-v" ];then
144		VERBOSE=1
145	elif [ ${THIRD_ARG}  = "-q" ];then
146		QUIET=1
147	else
148		usage
149	fi
150fi
151
152if   [ ${VERBOSE:-0} -eq 1 -a ${QUIET:-0} -eq 1 ];then
153	    usage
154elif [ ${VERBOSE:-0} -eq 1 -a ${QUIET:-0} -ne 1 ];then
155	    OUT="VERBOSE"
156elif [ ${VERBOSE:-0} -ne 1 -a ${QUIET:-0} -eq 1 ];then
157	    OUT="QUIET"
158else
159	    OUT="SHORT"
160fi
161}
162
163if [ $# -eq 0 ];then
164 	usage
165fi
166while getopts ":hvqs:c:" OPT ;do
167      case "$OPT" in
168      "c")
169	        if [ $# -eq 1 ];then help ;fi
170	    	CATEGORY=$OPTARG
171		    #transmit last option to set_out()
172		    set_out $3
173    	    search $OUT $OPT $CATEGORY
174	    	;;
175      "q")
176            if [ $# -eq 1 ];then help ;fi
177		    QUIET="1"
178        	;;
179      "s")
180            if [ $# -eq 1 ];then help ;fi
181       		STRING=$OPTARG
182		    #transmit last option to set_out()
183		    set_out $3
184        	search $OUT $OPT $STRING
185        	;;
186      "v")
187            if [ $# -eq 1 ];then help ;fi
188		    VERBOSE="1"
189        	;;
190      *)
191        help ;;
192      esac
193done
194