]> git.lizzy.rs Git - plan9front.git/blob - rc/bin/man
git: fix non-interruptible temporary warning
[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 usage
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 switch($cmd) {
113 case p; out=proof
114 case P; out=page
115 case *; out=cat
116 }
117 {
118         for(i in $fils) {
119                 if(! /bin/test -f $i)
120                         echo need $i >[1=2]
121                 if not {
122                         switch($cmd) {
123                         case w
124                                 echo $i
125
126                         case t
127                                 roff t $i
128
129                         case p
130                                 roff t $i | grep -v '^x X html'
131
132                         case P
133                                 roff t $i
134
135                         case n
136                                 roff n $i | sed '
137                                         ${
138                                                /^$/p
139                                         }
140                                         //N
141                                         /^\n$/D'
142
143                         case b
144                                 x=`{echo $i | sed 's;/sys/man/(.*)/(.*);\1 \2;'}
145                                 if(~ $x(2) 0intro) x=($x(1) intro)
146                                 roff n $i | sed '
147                                         ${
148                                                /^$/p
149                                         }
150                                         //N
151                                         /^\n$/D' |
152                                 plumb -i -d edit -a 'action=showdata filename=/man/'$x(2)^'('$x(1)^')'
153                         }
154                 }
155         }
156 } | $out
157