#! /bin/bash
istrue()
{
test 0 -ne "$1"
}
isfalse()
{
test 0 -eq "$1"
}
FindSectionsInIndex ()
{
typeset index indexes section mpath page=$1
typeset -i i=0 NIndex=0
for mpath in "${patharr[@]}"; do
if [ -r $mpath/index ]; then
indexes="$indexes $mpath/index"
let NIndex+=1
fi
done
[ -z "$indexes" ] && return
[ NIndex -lt 2 ] && indexes="$indexes /dev/null"
set -- `grep "^$page[ ]" $indexes`
while [ $ FileNames[i]=${1%%index*}cat$3/$2.$3
Sections[i]=$3
shift 3
let i+=1
done
}
FindSectionsInDirs ()
{
local page=$1 mpath AllPaths Path
typeset -i i
for mpath in "${patharr[@]}"; do
AllPaths="$AllPaths $mpath/cat[0-9]*/$page.* $mpath/man[0-9]*/$page.*"
done
i=0
for Path in $AllPaths; do
istrue $debug && echo Path = $Path
case "$Path" in
*\*) ;;
*)
FileNames[i]=${Path%.[zZ]}
Path=${Path%/*}
Sections[i]=${Path let i+=1 ;;
esac
done
}
FindSection ()
{
typeset -i NumPages i foundsec
local section OtherSec filename NPAGER=$PAGER POpt page=$1 Pat
local PageFile
NumPages=${ isfalse $NumPages && return 1
case "$PAGER" in
*less) Popt="-p$page" ;;
esac
for section in "${ordarr[@]}"; do
i=0
foundsec=0
while [ $i -lt $NumPages ]; do
if [ "${Sections[i]}" = $section ]; then
filename=${FileNames[i]}
if [ -z "$PageFile" ]; then
PageFile=$filename
else
if istrue $foundsec; then
OtherSec="$OtherSec$page(${filename%/*/*} $section) "
else
OtherSec="$OtherSec$page($section) "
fi
fi
foundsec=1
istrue $exist && return
fi
let i+=1
done
done
[ -z "$PageFile" ] && return 1
[ "$exist" = 1 ] && return 0
if [ -z "$OtherSec" ]; then
NPAGER="exec $PAGER"
fi
if [ -r $PageFile ]; then
$NPAGER $POpt $PageFile
elif [ -r $PageFile.z ]; then
pcat $PageFile.z | $NPAGER $POpt
elif [ -r $PageFile.Z ]; then
zcat $PageFile.Z | $NPAGER $POpt
elif [ -f $PageFile.gz ]; then
gzip -dc $PageFile.gz | $NPAGER $POpt
else
echo "$PageFile: cannot open." 1>&2
OtherSec=
unset Sections[i]
let i+=1
continue
fi
echo "See also $OtherSec"
exit 0
}
phelp()
{
echo "$name: print man pages.
$name locates and prints the specified manual pages from the online UNIX
documentation.
$Usage
Options:
-e: Determine whether the specified man page exists. Nothing is printed;
$0 exits with a zero status if the page exists and a nonzero status if
it does not.
-h: Print this help."
}
typeset -i exist=0 debug=0
name=${0Usage="Usage: $name [-eh] [[manpath] section] command-name"
while getopts :hex opt; do
case $opt in
h) phelp; exit 0;;
e) exist=1 ;;
x) debug=1 ;;
+?) echo "$name: options should not be preceded by a '+'." 1>&2; exit 2;;
?)
echo "$name: $OPTARG: bad option. Use -h for help." 1>&2 ; exit 2 ;;
esac
done
shift $((OPTIND-1))
if [ $ echo -e "$Usage\nUse -h for help." 1>&2
exit
fi
P=$PAGER
O=1:n:l:6:8:2:3:4:5:7:p:o
T=$TERM
M=${MANPATH:-/usr/local/man:/usr/man}
[ -f /etc/default/man ] && . /etc/default/man
[ -n "$P" ] && PAGER=$P
[ -n "$O" ] && ORDER=$O
[ -n "$T" ] && TERM=$T
[ -n "$M" ] && MANPATH=$M
case $0) echo "No man page specified." ; exit 1;;
1) page=$1;;
2) ORDER=$(echo $1 | tr a-z A-Z) ; page=$2;;
3) MANPATH=$1
[ -n "$2" ] && ORDER=$(echo $2 | tr a-z A-Z)
page=$3;;
*) echo "Too many arguments."; exit 1;;
esac
aargs=("$@")
[ ! -t 0 ] && PAGER=cat
OIFS=$IFS
IFS=:
patharr=($MANPATH)
i=0
for d in $MANPATH; do
for sec in $ORDER; do
ordarr[i]=$d/cat${sec}
let i+=1
ordarr[i]=$d/man${sec}
let i+=1
done
done
IFS=$OIFS
istrue $debug && echo patharr = "${patharr[@]}"
export LESS="-s $LESS"
export MORE="-s $MORE"
FindSectionsInIndex "$page"
FindSection "$page" && exit 0
unset Sections[*]
FindSectionsInDirs "$page"
FindSection "$page" && exit 0
istrue $exist && exit 1
export PAGER
cmd=(man $manopt "${aargs[@]}")
istrue $debug && echo "$name: running ${cmd[*]}" 1>&2
exec "${cmd[@]}"