Changeset View
Changeset View
Standalone View
Standalone View
usr.bin/man/man.sh
Show First 20 Lines • Show All 542 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
# Usage: man_parse_args "$@" | # Usage: man_parse_args "$@" | ||||
# Parses commandline options for man. | # Parses commandline options for man. | ||||
man_parse_args() { | man_parse_args() { | ||||
local IFS cmd_arg | local IFS cmd_arg | ||||
OPTIND=1 | OPTIND=1 | ||||
while getopts 'M:P:S:adfhkm:op:tw' cmd_arg; do | while getopts 'K:M:P:S:adfhkm:op:tw' cmd_arg; do | ||||
case "${cmd_arg}" in | case "${cmd_arg}" in | ||||
K) Kflag=Kflag | |||||
REGEXP=$OPTARG ;; | |||||
M) MANPATH=$OPTARG ;; | M) MANPATH=$OPTARG ;; | ||||
P) MANPAGER=$OPTARG ;; | P) MANPAGER=$OPTARG ;; | ||||
S) MANSECT=$OPTARG ;; | S) MANSECT=$OPTARG ;; | ||||
a) aflag=aflag ;; | a) aflag=aflag ;; | ||||
d) debug=$(( $debug + 1 )) ;; | d) debug=$(( $debug + 1 )) ;; | ||||
f) fflag=fflag ;; | f) fflag=fflag ;; | ||||
h) man_usage 0 ;; | h) man_usage 0 ;; | ||||
k) kflag=kflag ;; | k) kflag=kflag ;; | ||||
m) mflag=$OPTARG ;; | m) mflag=$OPTARG ;; | ||||
o) oflag=oflag ;; | o) oflag=oflag ;; | ||||
p) MANROFFSEQ=$OPTARG ;; | p) MANROFFSEQ=$OPTARG ;; | ||||
t) tflag=tflag ;; | t) tflag=tflag ;; | ||||
w) wflag=wflag ;; | w) wflag=wflag ;; | ||||
*) man_usage ;; | *) man_usage ;; | ||||
esac | esac | ||||
done >&2 | done >&2 | ||||
shift $(( $OPTIND - 1 )) | shift $(( $OPTIND - 1 )) | ||||
# Check the args for incompatible options. | # Check the args for incompatible options. | ||||
case "${fflag}${kflag}${tflag}${wflag}" in | |||||
case "${Kflag}${fflag}${kflag}${tflag}${wflag}" in | |||||
Kflagfflag*) echo "Incompatible options: -K and -f"; man_usage ;; | |||||
Kflag*kflag*) echo "Incompatible options: -K and -k"; man_usage ;; | |||||
Kflag*tflag) echo "Incompatible options: -K and -t"; man_usage ;; | |||||
fflagkflag*) echo "Incompatible options: -f and -k"; man_usage ;; | fflagkflag*) echo "Incompatible options: -f and -k"; man_usage ;; | ||||
fflag*tflag*) echo "Incompatible options: -f and -t"; man_usage ;; | fflag*tflag*) echo "Incompatible options: -f and -t"; man_usage ;; | ||||
fflag*wflag) echo "Incompatible options: -f and -w"; man_usage ;; | fflag*wflag) echo "Incompatible options: -f and -w"; man_usage ;; | ||||
*kflagtflag*) echo "Incompatible options: -k and -t"; man_usage ;; | *kflagtflag*) echo "Incompatible options: -k and -t"; man_usage ;; | ||||
*kflag*wflag) echo "Incompatible options: -k and -w"; man_usage ;; | *kflag*wflag) echo "Incompatible options: -k and -w"; man_usage ;; | ||||
*tflagwflag) echo "Incompatible options: -t and -w"; man_usage ;; | *tflagwflag) echo "Incompatible options: -t and -w"; man_usage ;; | ||||
esac | esac | ||||
▲ Show 20 Lines • Show All 124 Lines • ▼ Show 20 Lines | man_setup_locale() { | ||||
decho "Using locale paths: $locpaths" | decho "Using locale paths: $locpaths" | ||||
} | } | ||||
# Usage: man_usage [exitcode] | # Usage: man_usage [exitcode] | ||||
# Display usage for the man utility. | # Display usage for the man utility. | ||||
man_usage() { | man_usage() { | ||||
echo 'Usage:' | echo 'Usage:' | ||||
echo ' man [-adho] [-t | -w] [-M manpath] [-P pager] [-S mansect]' | echo ' man [-adho] [-t | -w] [-K regexp] [-M manpath] [-P pager] [-S mansect]' | ||||
echo ' [-m arch[:machine]] [-p [eprtv]] [mansect] page [...]' | echo ' [-m arch[:machine]] [-p [eprtv]] [mansect] page [...]' | ||||
echo ' man -f page [...] -- Emulates whatis(1)' | echo ' man -f page [...] -- Emulates whatis(1)' | ||||
echo ' man -k page [...] -- Emulates apropos(1)' | echo ' man -k page [...] -- Emulates apropos(1)' | ||||
# When exit'ing with -h, it's not an error. | # When exit'ing with -h, it's not an error. | ||||
exit ${1:-1} | exit ${1:-1} | ||||
} | } | ||||
▲ Show 20 Lines • Show All 237 Lines • ▼ Show 20 Lines | |||||
# Supported commands | # Supported commands | ||||
do_apropos() { | do_apropos() { | ||||
[ $(stat -f %i /usr/bin/man) -ne $(stat -f %i /usr/bin/apropos) ] && \ | [ $(stat -f %i /usr/bin/man) -ne $(stat -f %i /usr/bin/apropos) ] && \ | ||||
exec apropos "$@" | exec apropos "$@" | ||||
search_whatis apropos "$@" | search_whatis apropos "$@" | ||||
} | } | ||||
# Usage: do_full_search reg_exp | |||||
# Do a full search of the regular expression passed | |||||
# as parameter in all man pages | |||||
do_full_search() { | |||||
local gflags re | |||||
re=${1} | |||||
# Build grep(1) flags | |||||
gflags="-H" | |||||
# wflag implies -l for grep(1) | |||||
if [ -n "$wflag" ]; then | |||||
gflags="${gflags} -l" | |||||
fi | |||||
gflags="${gflags} --label" | |||||
set +f | |||||
for mpath in $(echo "${MANPATH}" | tr : [:blank:]); do | |||||
for section in $(echo "${MANSECT}" | tr : [:blank:]); do | |||||
for manfile in ${mpath}/man${section}/*.${section}*; do | |||||
mandoc "${manfile}" 2>/dev/null | | |||||
grep -E ${gflags} "${manfile}" -e ${re} | |||||
done | |||||
done | |||||
done | |||||
set -f | |||||
} | |||||
do_man() { | do_man() { | ||||
man_parse_args "$@" | man_parse_args "$@" | ||||
if [ -z "$pages" ]; then | if [ -z "$pages" -a -z "${Kflag}" ]; then | ||||
echo 'What manual page do you want?' >&2 | echo 'What manual page do you want?' >&2 | ||||
exit 1 | exit 1 | ||||
fi | fi | ||||
man_setup | man_setup | ||||
if [ ! -z "${Kflag}" ]; then | |||||
# Short circuit because -K flag does a sufficiently | |||||
# different thing like not showing the man page at all | |||||
do_full_search "${REGEXP}" | |||||
fi | |||||
for page in $pages; do | for page in $pages; do | ||||
decho "Searching for $page" | decho "Searching for $page" | ||||
man_find_and_display "$page" | man_find_and_display "$page" | ||||
done | done | ||||
exit ${ret:-0} | exit ${ret:-0} | ||||
} | } | ||||
▲ Show 20 Lines • Show All 54 Lines • Show Last 20 Lines |