source: trunk/third/bash/examples/functions/manpage @ 21276

Revision 21276, 2.7 KB checked in by zacheiss, 20 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r21275, which included commits to RCS files with non-trunk default branches.
Line 
1# Written from scratch by Tom Tromey (tromey@cns.caltech.edu)
2#
3# manpage -- find and print a manual page.
4# usage: manpage section name [printing]
5#
6function manpage ()
7{
8  local i h cmd zot sec
9  local num="$1"
10  local page="$2"
11  local printing="$3"
12  local mp
13
14  mp="${MANPATH:-/usr/man}"
15  if [ "$#" -lt 2 ]; then return 1; fi # should print usage
16  if [ "$num" != "" ]; then
17     sec="${num%%[a-zA-Z]*}"
18  else
19     sec='[168234571lnpo]'
20     num="$sec"
21  fi
22  for i in $(echo "$mp" | tr : ' '); do
23    if [ ! -d "$i" ]; then continue; fi
24    file="$i"/man"$sec"/"$page"."$num"*
25    set $file
26    file="$1"
27    if [ -f "$file" ]; then
28      zot=$(sed 1q "$file")
29      cmd=${MANROFF:-"nroff -man - | col | cat -s"}
30      h=${zot##"'"'\"'}
31      if [ "$h" != "$zot" ]; then
32        while [ "$h" != "" ]; do
33          case "$h" in
34            *e) cmd="${MANEQN:-neqn} | $cmd";;
35            *r) cmd="refer | $cmd";;
36            *t) cmd="tbl | $cmd";;
37            *v) cmd="vgrind | $cmd";;
38            *) ;; # should print error
39          esac
40          h=${h%?}
41        done
42      fi
43      if [ "$printing" != "" ]; then
44        (cd "$i"; eval "$cmd") < "$file" | ${PAGER:-more}
45      else
46        (cd "$i"; eval "$cmd") < "$file" > /tmp/manpage-$$
47        ${PAGER:-more} /tmp/manpage-$$
48        rm -f /tmp/manpage-$$
49      fi
50      break
51    fi
52  done
53}
54
55function whatis_internal ()
56{
57  local j
58  for j in $(echo "$MANPATH" | tr : ' '); do
59    if [ -f "$j/whatis" ]; then
60      eval $2 -i -e "$1" $j/whatis
61    fi
62  done
63}
64
65function whatis ()
66{
67  local name=$(basename "$1")
68  whatis_internal "$name" "grep -w"
69}
70
71function apropos ()
72{
73  whatis_internal "$1" "grep -F"
74}
75
76# Note: "-" and "-t" together not supported.  This man could be
77# made a lot better, but it does everything I want.
78function man ()
79{
80  local PAGER printing mpath MANROFF num
81  mpath="${MANPATH:-/usr/man}"
82  while true; do
83    case "$1" in
84      -) PAGER=cat
85         printing= ;;
86      -t)
87         MANROFF=${TROFF:-"ptroff -man -t"}
88         PAGER="${TCAT:-lpr}"
89         printing=yes ;;
90      -M)
91         mpath="$2"
92         shift;;
93      *) break;;
94    esac
95    shift
96  done
97  local MANPATH="$mpath"
98  case "$1" in
99    -f | -k)
100      local g a
101      if [ "$1" = "-f" ]; then
102        g="grep -w"
103        a=$(basename "$2")
104      else
105        g="grep -F"
106        a="$2"
107      fi
108      whatis_internal "$a" "$g"
109      ;;
110    [0-9npol] | [0-9][a-z]* | new | public | old | local)
111      if [ "$1" = "new" ]; then
112        num=n
113      elif [ "$1" = "public" ]; then
114        num=p
115      elif [ "$1" = "old" ]; then
116        num=o
117      elif [ "$1" = "local" ]; then
118        num=l
119      else
120        num="$1"
121      fi
122      shift
123      manpage "$num" "$1" "$printing"
124      ;;
125    *)
126      manpage "$num" "$1" "$printing"
127      ;;
128  esac
129}
Note: See TracBrowser for help on using the repository browser.