aboutsummaryrefslogtreecommitdiff
path: root/bin/man
blob: 1b0a1a6965f03f282afc061da7d93bcfb6581972 (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
#!/usr/local/plan9/bin/rc

. 9.rc
. $PLAN9/man/fonts

#
# formatters
# 
fn roff {
	preproc=()
	x=`{doctype $2}
	if (~ $1 t) {
		if(~ $x *grap*)
			preproc=($preproc grap)
		if(~ $x *pic*)
			preproc=($preproc pic)
		Nflag=-Tutf
	}
	if not {
		Nflag=-N
	}
	if(~ $x *eqn*)
		preproc=($preproc eqn)
	if(~ $x *tbl*)
		preproc=($preproc tbl)

	switch($#preproc) {
		case 0
			{echo -n $FONTS; cat $2< /dev/null} | troff $Nflag -$MAN 
		case 1
			{echo -n $FONTS; cat $2< /dev/null} | $preproc | troff $Nflag -$MAN
		case 2
			{echo -n $FONTS; cat $2< /dev/null} | $preproc(1) | $preproc(2) | troff $Nflag -$MAN
		case 3
			{echo -n $FONTS; cat $2< /dev/null} | $preproc(1) | $preproc(2) | $preproc(3) | | troff $Nflag -$MAN
		case *
			{echo -n $FONTS; cat $2< /dev/null} | $preproc(1) | $preproc(2) | $preproc(3) | | $preproc(4) | troff $Nflag -$MAN
	}
}

fn doecho { echo $1 }
fn dotroff { roff t $1 }
fn doproof { roff t $1 | proof }
fn dopage { roff t $1 | tr2post | page }
fn donroff {
	roff n $1 | sed '
		${
			/^$/p
		}
		//N
		/^\n$/D
	'
}
seq=0
fn dohtml {
	MAN=manhtml
	b=`{echo $1 | sed 's/\.[0-9].*//'}
	if(test -f $b.html)
		web $b.html
	if not{
		roff t $1 | troff2html >/tmp/man.$pid.$seq.html
		web /tmp/man.$pid.$seq.html
		seq=`{echo 1+$seq | hoc}
	}
}

#
# parse flags and sections
#
fn usage { echo 'Usage: man [-hnpPtw] [-s sec] [0-9] [0-9] ... [--] name1 name2 ...' >[1=2] }
cmd=donroff
sec=()
S=$PLAN9/man
d=0
while(~ $d 0) {
	if(~ $#* 0) {
		usage
		exit 1
	}
	if(test -d $S/man$1){
		sec=($sec $1)
		shift
	}
	if not switch($1) {
		case -t ; cmd=dotroff ; shift
		case -n ; cmd=donroff ; shift
		case -p ; cmd=doproof ; shift
		case -P ; cmd=dopage ; shift
		case -w ; cmd=doecho ; shift
		case -h ; cmd=dohtml ; shift
		case -- ; d=1 ; shift
		case -* ; usage ; exit 1
		case * ; d=1
	}
}
if(~ $#sec 0) {
	sec=`{ls -pd $S/man[0-9]* | sed 's/man//'}
}

#
# search index
#
allfiles=()
missing=no
for(word){
	files=()
	regexp='^'^$word^' '
	for(i in $S/man$sec){
		if(test -f $i/INDEX){
			try=`{grep $regexp $i/INDEX | sed 's/^[^ ]* //'}
			if(! ~ $#try 0)
				files=($files $i/$try)
		}
	}
	if(~ $#files 0){
		for(i in $S/man$sec){
			if(test -f $i/$word.[0-9]*)
				files=($files $i/$word.[0-9]*)
		}
	}
	if(~ $#files 0){
		echo 'man: no manual page' $word >[1=2]
		missing=yes
	}
	allfiles=($allfiles $files)
}
if(~ $#allfiles 0)
	exit 'no man'
# complicated sort order: want 9p.3, 9p-cmdbuf.3, 9pclient.3
allfiles=`{ls $allfiles | sed 's/[.\-]/ &/g;s/\./ &/g' | sort -u | tr -d ' '}

files=()
for(i in $allfiles){
	if(test -f $i)
		files=($files $i)
	if not
		echo need $i >[1=2]
}

#
# format pages
# 
for(i in $files)
	$cmd $i