xref: /plan9/rc/bin/man (revision e06f534bbaa4097bc6f4764ef1dd2dc3338fbd40)
1#!/bin/rc
2
3rfork e
4
5. /sys/man/fonts
6
7cmd=n
8sec=()
9S=/sys/man
10d=0
11
12fn roff {
13	preproc=()
14	x=`{doctype $2}
15	if (~ $1 t) {
16		if(~ $x *grap*)
17			preproc=($preproc grap)
18		if(~ $x *pic*)
19			preproc=($preproc pic)
20		Nflag=-Tutf
21	}
22	if not {
23		Nflag=-N
24	}
25	if(~ $x *eqn*)
26		preproc=($preproc eqn)
27	if(~ $x *tbl*)
28		preproc=($preproc tbl)
29	switch($#preproc) {
30		case 0
31			{echo -n $FONTS; cat $2< /dev/null} | troff $Nflag -$MAN
32		case 1
33			{echo -n $FONTS; cat $2< /dev/null} | $preproc | troff $Nflag -$MAN
34		case 2
35			{echo -n $FONTS; cat $2< /dev/null} | $preproc(1) | $preproc(2) | troff $Nflag -$MAN
36		case 3
37			{echo -n $FONTS; cat $2< /dev/null} | $preproc(1) | $preproc(2) | $preproc(3) | troff $Nflag -$MAN
38		case *
39			{echo -n $FONTS; cat $2< /dev/null} | $preproc(1) | $preproc(2) | $preproc(3) | $preproc(4) | troff $Nflag -$MAN
40	}
41}
42
43fn page {
44	if(test -d /mnt/wsys/acme)
45		/bin/page -w
46	if not
47		/bin/page
48}
49
50
51search=yes
52while(~ $d 0) {
53	if(~ $#* 0) {
54		echo 'Usage: man [-bntpPSw] [0-9] [0-9] ... name1 name2 ...' >[1=2]
55		exit
56	}
57	if(test -d $S/$1){
58		sec=($sec $1)
59		shift
60	}
61	if not switch($1) {
62		case -t ; cmd=t ; shift
63		case -n ; cmd=n ; shift
64		case -p ; cmd=p ; shift
65		case -P ; cmd=P ; shift
66		case -w ; cmd=w ; shift
67		case -b ; cmd=b ; shift
68		case -S ; search=no ; shift
69		case * ; d=1
70	}
71}
72if(~ $#sec 0) {
73	sec=`{ls -pd $S/[0-9]* }
74}
75ix=$S/$sec/INDEX
76if(~ $#* 1) pat='^'^$1^' '
77if not pat='^('^`{echo $* | sed 's/ /|/g'}^') '
78fils=()
79if(~ $search yes)
80for(i in $S/$sec){
81	if(/bin/test -f $i/INDEX){
82		try=`{grep -i $pat $i/INDEX | sed 's/^[^ ]* //'}
83		if(! ~ $#try 0)
84			fils=($fils $i/$try)
85	}
86}
87# bug: should also do following loop if not all pages found
88if(~ $#fils 0) {
89	# nothing in INDEX. try for file of given name
90	for(i) {
91		if(~ $i intro) i=0intro
92		for(n in $sec) {
93			try=`{echo $S/$n/$i | tr A-Z a-z}
94			if (/bin/test -f $try)
95				fils=($fils $try)
96		}
97	}
98	if(~ $#fils 0) {
99		echo 'man: no manual page' >[1=2]
100		exit 'no man'
101	}
102}
103for(i in $fils) {
104	if(! /bin/test -f $i)
105		echo need $i >[1=2]
106	if not {
107		switch($cmd) {
108		case w
109			echo $i
110
111		case t
112			roff t $i
113
114		case p
115			roff t $i | proof
116
117		case P
118			roff t $i | page
119
120		case n
121			roff n $i | sed '
122				${
123        			       /^$/p
124				}
125				//N
126				/^\n$/D'
127
128		case b
129			x=`{echo $i | sed 's;/sys/man/(.*)/(.*);\1 \2;'}
130			if(~ $x(2) 0intro) x=($x(1) intro)
131			roff n $i | sed '
132				${
133        			       /^$/p
134				}
135				//N
136				/^\n$/D' |
137			plumb -i -d edit -a 'action=showdata filename=/man/'$x(2)^'('$x(1)^')'
138		}
139	}
140}
141