xref: /inferno-os/dis/man (revision 492f69dc458aa1415bdf4490b615e71609e4a4ea)
1#!/dis/sh.dis
2# Plan 9 man script adaptation for Inferno
3# for more details see the real man(1)
4load std
5
6fils=()
7cmd=n
8sec=()
9S=/man
10d=0
11
12x=$* # see sh(1) for details about s/$*/$x/.
13x=`{echo $x|tr A-Z a-z|tr -dc 'a-z0-9_ \012-/'}	# fold case, delete funny chars
14
15search=yes
16while {~ $d 0}{
17	#echo d$d search$search cmd$cmd fils$fils: $"x
18	if {no $x}{
19		echo 'Usage: man [-bnSwpf] [0-9] [0-9] ... name1 name2 ...'
20		exit
21	}
22	if{ftest -d $S/${hd $x}}{
23		sec=($sec ${hd $x}); x=${tl $x};
24	}{
25		if{~ ${hd $x} '-n'}{
26			cmd=n; x=${tl $x};
27		}{~ ${hd $x} '-w'}{
28			cmd=w; x=${tl $x};
29		}{~ ${hd $x} '-b'}{
30			cmd=b; x=${tl $x};
31		}{~ ${hd $x} '-p'}{
32			cmd=p; x=${tl $x};
33		}{~ ${hd $x} '-s'}{
34			search=no; x=${tl $x};
35		}{~ ${hd $x} '-f'}{
36			search=no; x=${tl $x};
37			if {ftest -f ${hd $x}}{
38				fils=(${hd $x});
39			}{
40				echo 'man: bad option -f '${hd $x}
41				exit 'no file'
42			}
43		}{
44			d=1;
45		}
46
47		if{no $x}{
48			d=1;
49		}
50	}
51}
52if{no $sec} {
53	sec=`{ls -pd $S/[0-9]* }
54}
55
56ix=$S/$sec/INDEX
57if{~ $#x 1} {pat='^'^$x^' '}
58{pat='^('^`{echo $x | sed 's/ /|/g'}^') '}
59
60if{~ $search yes} {
61	for i in $S/$sec {
62		if {ftest -f $i/INDEX}{
63			try=`{grep -i $pat $i/INDEX | sed 's/^[^ ]*//'}
64			if {! no $try}{
65				fils=($fils $i/$try)
66			}
67		}
68	}
69}
70
71if{no $fils} {
72	# nothing in INDEX. try for file or given name
73	for i in $x {
74		if{~ $i intro} {i=0intro}
75		for n in $sec {
76			try=`{echo $S/$n/$i | tr A-Z a-z}
77			if {ftest -f $try} {
78				fils=($fils $try)
79			}
80		}
81	}
82	if{~ $#fils 0} {
83		echo 'man: no manual page'
84		exit 'no man'
85	}
86}
87
88for i in $fils {
89	if {! ftest -f $i}{
90		echo need:$i
91	}{
92		if {~ $cmd n}{
93			man2txt $i
94		}{~ $cmd w}{
95			echo $i
96		}{~ $cmd p}{
97			wm/man -f $i
98		}{~ $cmd b}{
99			if{~ $i '/man/*/0intro'}{
100				x=`{echo $i | sed 's;/man/(.*)/.*;\1 intro;'}
101			}{~ $i '/man/*/*'}{
102				x=`{echo $i | sed 's;/man/(.*)/(.*);\1 \2;'}
103			}{
104				x=($i '')
105			}
106
107			man2txt $i |
108		 	plumb -i -d edit -a action showdata -a filename '/man/'^${tl $x}^'('^${hd $x}^')'
109
110		}
111	}
112
113}
114