]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - gnu/usr.bin/man/apropos/apropos.sh
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.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 db=whatis       # name of whatis data base
22 grepopt=''
23
24 # man -k complains if exit_nomatch=1 and no keyword matched
25 : ${exit_nomatch=0}     
26 exit_error=2
27
28 # argument test
29 case $# in 0)  
30         echo "usage: `basename $0` keyword ..." >&2
31         exit $exit_error
32         ;; 
33 esac
34
35 case "$0" in
36         *whatis) grepopt='-w';; # run as whatis(1)
37         *)       grepopt='';;   # otherwise run as apropos(1)
38 esac
39
40 # test manpath
41 manpath=`%bindir%/manpath -q | tr : '\040'`
42 case X"$manpath" in X) 
43         echo "`basename $0`: manpath is null, using \"/usr/share/man\"" >&2
44         manpath=/usr/share/man
45         ;;
46 esac
47
48
49 # reset $PAGER if $PAGER is empty
50 case X"$PAGER" in X) 
51         PAGER="%pager%"
52         ;; 
53 esac
54
55 man_locales=`%bindir%/manpath -qL`
56
57 # search for existing */whatis databases
58 mandir=''
59 for d in $manpath
60 do
61         if [ -f "$d/$db" -a -r "$d/$db" ]
62         then
63                 mandir="$mandir $d/$db"
64         fi
65
66         # Check for localized manpage subdirectories
67         if [ X"$man_locales" != X ]; then
68                 for l in $man_locales
69                 do
70                         if [ -f "$d/$l/$db" -a -r "$d/$l/$db" ];
71                         then
72                                 mandir="$mandir $d/$l/$db"
73                         fi
74                 done
75         fi
76 done
77
78 case X"$mandir" in X)
79         echo "`basename $0`: no whatis databases in $manpath" >&2
80         exit $exit_error
81 esac
82
83
84 for manpage
85 do
86         if grep -Ehi $grepopt -- "$manpage" $mandir; then :
87         else
88                 echo "$manpage: nothing appropriate"
89         fi
90 done | 
91
92 (       # start $PAGER only if we find a manual page
93         while read line
94         do
95                 case $line in
96                         # collect error(s)
97                         *": nothing appropriate") line2="$line2$line\n";;
98                         # matched line or EOF
99                         *) break;;
100                 esac
101         done
102
103         # nothing found, exit
104         if [ -z "$line" -a ! -z "$line2" ]; then
105                 printf -- "$line2"
106                 exit $exit_nomatch
107         else
108                 ( printf -- "$line2"; echo "$line"; cat ) | $PAGER
109         fi
110 )