]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - gnu/usr.bin/man/apropos/apropos.sh
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / gnu / usr.bin / man / apropos / apropos.sh
1 #!/bin/sh
2 #
3 # apropos -- search the whatis database for keywords.
4 #
5 # Copyright (c) February 1996 Wolfram Schneider <wosch@FreeBSD.org>. Berlin.
6 # Copyright (c) 1990, 1991, John W. Eaton.
7 #
8 # You may distribute under the terms of the GNU General Public
9 # License as specified in the README file that comes with the man
10 # distribution.  
11 #
12 # John W. Eaton
13 # jwe@che.utexas.edu
14 # Department of Chemical Engineering
15 # The University of Texas at Austin
16 # Austin, Texas  78712
17 #
18 # $FreeBSD$
19
20
21 PATH=/bin:/usr/bin:$PATH
22 db=whatis       # name of whatis data base
23 grepopt=''
24
25 # man -k complains if exit_nomatch=1 and no keyword matched
26 : ${exit_nomatch=0}     
27 exit_error=2
28
29 # argument test
30 case $# in 0)  
31         echo "usage: `basename $0` keyword ..." >&2
32         exit $exit_error
33         ;; 
34 esac
35
36 case "$0" in
37         *whatis) grepopt='-w';; # run as whatis(1)
38         *)       grepopt='';;   # otherwise run as apropos(1)
39 esac
40
41 # test manpath
42 manpath=`%bindir%/manpath -q | tr : '\040'`
43 case X"$manpath" in X) 
44         echo "`basename $0`: manpath is null, using \"/usr/share/man\"" >&2
45         manpath=/usr/share/man
46         ;;
47 esac
48
49
50 # reset $PAGER if $PAGER is empty
51 case X"$PAGER" in X) 
52         PAGER="%pager%"
53         ;; 
54 esac
55
56 man_locales=`%bindir%/manpath -qL`
57
58 # search for existing */whatis databases
59 mandir=''
60 for d in $manpath
61 do
62         if [ -f "$d/$db" -a -r "$d/$db" ]
63         then
64                 mandir="$mandir $d/$db"
65         fi
66
67         # Check for localized manpage subdirectories
68         if [ X"$man_locales" != X ]; then
69                 for l in $man_locales
70                 do
71                         if [ -f "$d/$l/$db" -a -r "$d/$l/$db" ];
72                         then
73                                 mandir="$mandir $d/$l/$db"
74                         fi
75                 done
76         fi
77 done
78
79 case X"$mandir" in X)
80         echo "`basename $0`: no whatis databases in $manpath" >&2
81         exit $exit_error
82 esac
83
84
85 for manpage
86 do
87         if grep -Ehi $grepopt -- "$manpage" $mandir; then :
88         else
89                 echo "$manpage: nothing appropriate"
90         fi
91 done | 
92
93 (       # start $PAGER only if we find a manual page
94         while read line
95         do
96                 case $line in
97                         # collect error(s)
98                         *": nothing appropriate") line2="$line2$line\n";;
99                         # matched line or EOF
100                         *) break;;
101                 esac
102         done
103
104         # nothing found, exit
105         if [ -z "$line" -a ! -z "$line2" ]; then
106                 printf -- "$line2"
107                 exit $exit_nomatch
108         else
109                 ( printf -- "$line2"; echo "$line"; cat ) | $PAGER
110         fi
111 )