shithub: x

ref: 9bd6366c2795decf34b2367ecf8e492d3a399835
dir: /man/

View raw version
#!/bin/rc
# x/man -- print manual pages from remote systems
rfork e

flags=()
section=()
width=72

fn usage {
	>[1=2] echo usage: x/man [-b] [man-flags] [section] title
	exit usage
}

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 $*
if not
	x/run 'MANWIDTH='$width man $flags -s $section $*
}| output