plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

man (2708B)


      1 #!/usr/local/plan9/bin/rc
      2 
      3 . 9.rc
      4 . $PLAN9/man/fonts
      5 
      6 #
      7 # formatters
      8 # 
      9 fn roff {
     10 	preproc=()
     11 	x=`{doctype $2}
     12 	if (~ $1 t) {
     13 		if(~ $x *grap*)
     14 			preproc=($preproc grap)
     15 		if(~ $x *pic*)
     16 			preproc=($preproc pic)
     17 		Nflag=-Tutf
     18 	}
     19 	if not {
     20 		Nflag=-N
     21 		Lflag='-rL1000i'
     22 	}
     23 	if(~ $x *eqn*)
     24 		preproc=($preproc eqn)
     25 	if(~ $x *tbl*)
     26 		preproc=($preproc tbl)
     27 
     28 	switch($#preproc) {
     29 		case 0
     30 			{echo -n $FONTS; cat $2< /dev/null} | troff $Nflag $Lflag -$MAN 
     31 		case 1
     32 			{echo -n $FONTS; cat $2< /dev/null} | $preproc | troff $Nflag $Lflag -$MAN
     33 		case 2
     34 			{echo -n $FONTS; cat $2< /dev/null} | $preproc(1) | $preproc(2) | troff $Nflag $Lflag -$MAN
     35 		case 3
     36 			{echo -n $FONTS; cat $2< /dev/null} | $preproc(1) | $preproc(2) | $preproc(3) | | troff $Nflag $Lflag -$MAN
     37 		case *
     38 			{echo -n $FONTS; cat $2< /dev/null} | $preproc(1) | $preproc(2) | $preproc(3) | | $preproc(4) | troff $Nflag $Lflag -$MAN
     39 	}
     40 }
     41 
     42 fn doecho { echo $1 }
     43 fn dotroff { roff t $1 }
     44 fn doproof { roff t $1 | proof }
     45 fn dopage { roff t $1 | tr2post | page }
     46 fn donroff {
     47 	roff n $1 | sed '
     48 		${
     49 			/^$/p
     50 		}
     51 		//N
     52 		/^\n$/D
     53 	'
     54 }
     55 seq=0
     56 fn dohtml {
     57 	MAN=manhtml
     58 	b=`{echo $1 | sed 's/\.[0-9].*//'}
     59 	if(test -f $b.html)
     60 		web $b.html
     61 	if not{
     62 		roff t $1 | troff2html >/tmp/man.$pid.$seq.html
     63 		web /tmp/man.$pid.$seq.html
     64 		seq=`{echo 1+$seq | hoc}
     65 	}
     66 }
     67 
     68 #
     69 # parse flags and sections
     70 #
     71 fn usage { echo 'Usage: man [-hnpPtw] [-s sec] [0-9] [0-9] ... [--] name1 name2 ...' >[1=2] }
     72 cmd=donroff
     73 sec=()
     74 S=$PLAN9/man
     75 d=0
     76 while(~ $d 0) {
     77 	if(~ $#* 0) {
     78 		usage
     79 		exit 1
     80 	}
     81 	if(test -d $S/man$1){
     82 		sec=($sec $1)
     83 		shift
     84 	}
     85 	if not switch($1) {
     86 		case -t ; cmd=dotroff ; shift
     87 		case -n ; cmd=donroff ; shift
     88 		case -p ; cmd=doproof ; shift
     89 		case -P ; cmd=dopage ; shift
     90 		case -w ; cmd=doecho ; shift
     91 		case -h ; cmd=dohtml ; shift
     92 		case -- ; d=1 ; shift
     93 		case -* ; usage ; exit 1
     94 		case * ; d=1
     95 	}
     96 }
     97 if(~ $#sec 0) {
     98 	sec=`{ls -pd $S/man[0-9]* | sed 's/man//'}
     99 }
    100 
    101 #
    102 # search index
    103 #
    104 allfiles=()
    105 missing=no
    106 for(word){
    107 	files=()
    108 	regexp='^'^$word^' '
    109 	for(i in $S/man$sec){
    110 		if(test -f $i/INDEX){
    111 			try=`{grep $regexp $i/INDEX | sed 's/^[^ ]* //'}
    112 			if(! ~ $#try 0)
    113 				files=($files $i/$try)
    114 		}
    115 	}
    116 	if(~ $#files 0){
    117 		for(i in $S/man$sec){
    118 			if(test -f $i/$word.[0-9]*)
    119 				files=($files $i/$word.[0-9]*)
    120 		}
    121 	}
    122 	if(~ $#files 0){
    123 		echo 'man: no manual page' $word >[1=2]
    124 		missing=yes
    125 	}
    126 	allfiles=($allfiles $files)
    127 }
    128 if(~ $#allfiles 0)
    129 	exit 'no man'
    130 # complicated sort order: want 9p.3, 9p-cmdbuf.3, 9pclient.3
    131 allfiles=`{ls $allfiles | sed 's/[.\-]/ &/g;s/\./ &/g' | sort -u | tr -d ' '}
    132 
    133 files=()
    134 for(i in $allfiles){
    135 	if(test -f $i)
    136 		files=($files $i)
    137 	if not
    138 		echo need $i >[1=2]
    139 }
    140 
    141 #
    142 # format pages
    143 # 
    144 for(i in $files)
    145 	$cmd $i
    146