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