aboutsummaryrefslogtreecommitdiffstats
path: root/main/mdocml/man.sh
blob: 136cdd21818fbe573efec4f4f16704e4c5f052b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/sh

# man - a simple man program for ASCII pages
# Copyright (c) 2008 Matthew Hiles
#
# Licensed under GPLv2 or later, see file LICENSE in this tarball for details.

## requires: find, head, sort, tr, cat, and grep/egrep
## optional: zcat, bzcat, less

if [ -z "$MANPATH" ]; then
	echo "Warning: MANPATH is not set, assuming /usr/share/man." >&2
	MANPATH=/usr/share/man
fi

case $# in
1)
	pagearg=$1 ;;
2)
	section=man$1
	pagearg=$2 ;;
*)
	echo "Usage: man [section] <manpage>"
	exit
esac

paths=`echo "$MANPATH" | tr ":" " "`
pagefile=`find $paths | grep "/$section" | grep "/$pagearg\." | sort | head -n1`

if [ -z "$pagefile" ]; then
	echo -n No manual entry for $pagearg
	[ $section ] && echo -n " in section $1 of the manual."
	echo
	exit
fi

[ "$PAGER" ] || PAGER=less
tty -s <&1 || PAGER=cat

MANWIDTH=${MANWIDTH:-78}
if [ $MANWIDTH = 0 ]; then
    MANWIDTH=$(($(stty size | awk '{print $2}') - 2))
fi

case "$pagefile" in
*.bz2)
	exec bzcat "$pagefile" | mandoc -Tutf8 -Owidth=$MANWIDTH | "$PAGER" ;;
*.gz)
	exec zcat "$pagefile" | mandoc -Tutf8 -Owidth=$MANWIDTH | "$PAGER" ;;
*)
	exec mandoc -Tutf8 -Owidth=$MANWIDTH "$pagefile" | "$PAGER" ;;
esac

paths=
pagefile=
pagearg=
section=