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