shithub: x

ref: 7760194f148720cc9d9432d47090ad4f1954a297
dir: x/man

View raw version
#!/bin/rc
rfork e
. /sys/lib/x/lib.rc
usage = '
	x/man [-b] [man-flags] [section] title
'

flags=()
width=72
out='stdout'
section=()
title=()
while(~ $1 -* && ! ~ $1 --){
	switch($1){
	case -b
		out='plumb'
	case *
		flags=($flags $1)
	}
	shift
}
if(~ $1 --)
	shift
if(~ $#* 2){
	section = $1; shift
}
title = $1
if(~ $#title 0)
	usage

if(~ $out 'plumb')
	fn output {
		t = /tmp/xman
		mkdir -p $t
		col -b >$t/tmp.$pid
		if(~ $#section 0){
			<$t/tmp.$pid awk -F'[)(]' '
				NR == 1 {printf($2) >"/env/section"}
			'
			section = `{cat /env/section}
		}
		if(~ $#section 0)
			section = ''
		mkdir -p $t/$section
		mv $t/tmp.$pid $t/$section/$title
		plumb $t/$section/$title
	}
if not
	fn output {col -b}

{if(~ $#section 0)
	x/run 'MANWIDTH='$width man $flags $title
if not
	x/run 'MANWIDTH='$width man $flags -s $section $title
}| output