summaryrefslogtreecommitdiff
path: root/rc/bin/man
blob: f002c21215781aa3a0ecd4c0faa37579bde8369e (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/bin/rc
# man - print manual pages
rfork e

. /sys/man/fonts

cmd=n
sec=()
S=/sys/man
d=0

fn roff {
	preproc=()
	postproc=cat
	x=`{doctype $2}
	if (~ $1 t) {
		if(~ $x *grap*)
			preproc=($preproc grap)
		if(~ $x *pic*)
			preproc=($preproc pic)
		Nflag=-Tutf
	}
	if not {
		Nflag='-N'
		Lflag='-rL1000i'
		# setting L changes page length to infinity (sed script removes empty lines)
		if (grep -s '^\.(2C|sp *[0-9]*\.)' $2)
			postproc=col
	}
	if(~ $x *eqn*)
		preproc=($preproc eqn)
	if(~ $x *tbl*)
		preproc=($preproc tbl)
	{echo -n $FONTS; cat $2 </dev/null} |
		switch($#preproc) {
		case 0
			troff $Nflag $Lflag -$MAN 
		case 1
			$preproc | troff $Nflag $Lflag -$MAN
		case 2
			$preproc(1) | $preproc(2) | troff $Nflag $Lflag -$MAN
		case 3
			$preproc(1) | $preproc(2) | $preproc(3) |
				troff $Nflag $Lflag -$MAN
		case *
			$preproc(1) | $preproc(2) | $preproc(3) |
				$preproc(4) | troff $Nflag $Lflag -$MAN
		} | $postproc
}

fn page {
	if(test -d /mnt/wsys/acme)
		/bin/page -w
	if not
		/bin/page
}


search=yes
while(~ $d 0) {
	if(~ $#* 0) {
		echo 'usage: man [-bntpPSw] [0-9] [0-9] ... name1 name2 ...' >[1=2]
		exit usage
	}
	if(test -d $S/$1){
		sec=($sec $1)
		shift
	}
	if not
		switch($1) {
		case -b ; cmd=b ; shift
		case -n ; cmd=n ; shift
		case -P ; cmd=P ; shift
		case -p ; cmd=p ; shift
		case -S ; search=no ; shift
		case -t ; cmd=t ; shift
		case -w ; cmd=w ; shift
		case * ; d=1
		}
}
if(~ $#sec 0) {
	sec=`{ls -pd $S/[0-9]* }
}
ix=$S/$sec/INDEX
if(~ $#* 1) pat='^'^$1^' '
if not pat='^('^`{echo $* | sed 's/ /|/g'}^') '
fils=()
if(~ $search yes)
for(i in $S/$sec){
	if(/bin/test -f $i/INDEX){
		try=`{grep -i $pat $i/INDEX | sed 's/^[^ ]* //' | sort -u}
		if(! ~ $#try 0)
			fils=($fils $i/$try)
	}
}
# bug: should also do following loop if not all pages found
if(~ $#fils 0) {
	# nothing in INDEX. try for file of given name
	for(i) {
		if(~ $i intro) i=0intro
		for(n in $sec) {
			try=`{echo $S/$n/$i | tr A-Z a-z}
			if (/bin/test -f $try)
				fils=($fils $try)
		}
	}
	if(~ $#fils 0) {
		echo 'man: no manual page' >[1=2]
		exit 'no man'
	}
}
switch($cmd) {
case p; out=proof
case P; out=page
case *; out=cat
}
{
	for(i in $fils) {
		if(! /bin/test -f $i)
			echo need $i >[1=2]
		if not {
			switch($cmd) {
			case w
				echo $i

			case t
				roff t $i

			case p
				roff t $i | grep -v '^x X html'

			case P
				roff t $i

			case n
				roff n $i | sed '
					${
        				       /^$/p
					}
					//N
					/^\n$/D'

			case b
				x=`{echo $i | sed 's;/sys/man/(.*)/(.*);\1 \2;'}
				if(~ $x(2) 0intro) x=($x(1) intro)
				roff n $i | sed '
					${
        				       /^$/p
					}
					//N
					/^\n$/D' |
				plumb -i -d edit -a 'action=showdata filename=/man/'$x(2)^'('$x(1)^')'
			}
		}
	}
} | $out