shithub: purgatorio

ref: c116550e6a41572796e4db65e4f6acbcb3d9d6f8
dir: /dis/man/

View raw version
#!/dis/sh.dis
# Plan 9 man script adaptation for Inferno
# for more details see the real man(1)
load std

fils=()
cmd=n
sec=()
S=/man
d=0

x=$* # see sh(1) for details about s/$*/$x/.
x=`{echo $x|tr A-Z a-z|tr -dc 'a-z0-9_ \012-/'}	# fold case, delete funny chars

search=yes
while {~ $d 0}{
	#echo d$d search$search cmd$cmd fils$fils: $"x
	if {no $x}{
		echo 'Usage: man [-bnSwpf] [0-9] [0-9] ... name1 name2 ...'
		exit
	}
	if{ftest -d $S/${hd $x}}{
		sec=($sec ${hd $x}); x=${tl $x};
	}{
		if{~ ${hd $x} '-n'}{
			cmd=n; x=${tl $x};
		}{~ ${hd $x} '-w'}{
			cmd=w; x=${tl $x};
		}{~ ${hd $x} '-b'}{
			cmd=b; x=${tl $x};
		}{~ ${hd $x} '-p'}{
			cmd=p; x=${tl $x};
		}{~ ${hd $x} '-s'}{
			search=no; x=${tl $x};
		}{~ ${hd $x} '-f'}{
			search=no; x=${tl $x};
			if {ftest -f ${hd $x}}{
				fils=(${hd $x});
			}{
				echo 'man: bad option -f '${hd $x}
				exit 'no file'
			}
		}{
			d=1;
		}
		
		if{no $x}{
			d=1;
		}
	}
}
if{no $sec} {
	sec=`{ls -pd $S/[0-9]* }
}

ix=$S/$sec/INDEX
if{~ $#x 1} {pat='^'^$x^' '}
{pat='^('^`{echo $x | sed 's/ /|/g'}^') '}

if{~ $search yes} {
	for i in $S/$sec {
		if {ftest -f $i/INDEX}{
			try=`{grep -i $pat $i/INDEX | sed 's/^[^ ]*//'}
			if {! no $try}{
				fils=($fils $i/$try)
			}
		}
	}
}

if{no $fils} {
	# nothing in INDEX. try for file or given name
	for i in $x {
		if{~ $i intro} {i=0intro}
		for n in $sec {
			try=`{echo $S/$n/$i | tr A-Z a-z}
			if {ftest -f $try} {
				fils=($fils $try)
			}
		}
	}
	if{~ $#fils 0} {
		echo 'man: no manual page'
		exit 'no man'
	}
}

for i in $fils {
	if {! ftest -f $i}{
		echo need:$i
	}{
		if {~ $cmd n}{
			man2txt $i
		}{~ $cmd w}{
			echo $i
		}{~ $cmd p}{
			wm/man -f $i
		}{~ $cmd b}{
			if{~ $i '/man/*/0intro'}{
				x=`{echo $i | sed 's;/man/(.*)/.*;\1 intro;'}
			}{~ $i '/man/*/*'}{
				x=`{echo $i | sed 's;/man/(.*)/(.*);\1 \2;'}			
			}{
				x=($i '')
			}

			man2txt $i |
		 	plumb -i -d edit -a action showdata -a filename '/man/'^${tl $x}^'('^${hd $x}^')'

		}
	}
	
}