#!/usr/bin/env bash # $Id: manpage.cgi,v 1.26 2013/06/05 17:19:41 ksb Exp $ # # Display a manual page via http to a browser. Better than some programs # that try a lot harder. --ksb # # Called for example: http://www.sac.fedex.com/cgi-bin/manpage.cgi?chown&2 # to get chown from section 2 rather than perhaps section 8. Note that "1L" # changes the search path to move local first. That lets you get to # install&1L vs install&1. # : ${MANPATH:=/usr/share/man:/usr/local/man:/usr/X11R6/man:/usr/man} : ${manpath:=$MANPATH} export manpath MANPATH PATH set _ `echo ${QUERY_STRING} | tr '&' ' '` shift THEMANPAGE=${1:-intro} THESECTION=$2 if expr "$THESECTION" : '[0-9]*[Ll]' >/dev/null ; then manpath=~ksb/usr/local/man:/usr/local/man:$MANPATH THESECTION=`expr "$THESECTION" : '\([0-9]*\)[Ll]'` fi # bold(\fB) b^Hbo^Hol^Hld^Hd # italic(\fI) _^Hi_^Ht_^Ha_^Hl_^Hi_^Hc # bold italic (\f(BI) _^Hb^Hb_^Ho^Ho_^Hl^Hl_^Hd^Hd _^Hi^Hi_^Ht^Ht_^Ha^Ha... cat < Manual page ${THEMANPAGE}${THESECTION:+\($THESECTION\)}
!

Upper=`echo $THEMANPAGE | tr 'a-z' 'A-Z'`
/usr/bin/man -M ${manpath} ${THESECTION:+"$THESECTION"} "${THEMANPAGE}" 2>&1 |
sed -e '/\/\*[  ]*\(<[^>]*>\)[  ]*\*\//{
	s//\1/
	n
}' -e 's/[&][&]/BOLDamp;/g' -e 's!<>!BOLDgt;!g' \
	-e 's/[+]o/AMPoplus;/g' -e 's/o[+]/AMPoplus;/g' \
	-e 's/[&]/AMPamp;/g' -e 's//AMPgt;/g' \
	-e 's@\([^	 (<"][^ 	(<"]*\)(\([0-9][-a-zA-Z.]*\))@&@g' \
	-e 's/AMPlt;/\</g' -e 's/AMPgt;/\>/g' -e 's/AMPamp;/\&/g' \
	-e 's/AMPoplus;/\⊕/g' \
	-e 's!BOLDlt;!\<!g' -e 's!BOLDgt;!\>!g' \
	-e 's!BOLDamp;!\&!g' |
sed -e 's!_\([^_]\)\1!\1!g' -e 's!!!g' |
sed -e 's!_\([^_]\)!\1!g' -e 's!!!g' |
sed -e 's!\(.\)\1!\1!g' -e 's!\([ 	]*\)!\1!g' |
sed -e 's!!!g' -e 's!!!g' \
	-e 's!!!g' -e 's!!!g' |\
#	-e 's!!!g' -e 's!!!g' |
sed -e 's@\(\([^>]*\)\([^"]*">\)\([^>]*[^<]*\)@\1\2\3\4@g' \
	-e 's@\(\)\([^>]*[^<]*\)@\1\2\3\4@g' |
perl -pne 'BEGIN {
		my(%sect) = {};
		my(%dash) = {};
		my($opts) = 0;
		my($last) = undef;
	}
	if (m|^(\s*)([A-Z]+)(\s*[A-Z]*\s*)$|o) {
		$_ =~ s||$1$2$3| unless exists $sect{$2};
		$opts = ('OPTIONS' eq $2 || 'OPTION' eq $2);
		$sect{$2} = 'k';
		next;
	}
	# -count would be ok too --ksb
	if ($opts and m|^(\s*-(\w)(.*)$|o) {
		my($w,$l,$p) = ($1,$2,$3);
		my($tag) = 'opt_';
		if ($l =~ m/[a-z]/o) {
			$tag .= $l;
		} elsif ($l =~ m/[A-Z]/o) {
			$tag .= "u".lc $l;
		} else {
			next;
		}
		$_ = "$w id=\"$tag\">-$l$p\n" unless $dash{$l};
		$dash{$l} = 'b';
		next;
	}
	if (m|(.*)-<.b>\s*$|o) {
		$last = $1;
		next;
	}
	next unless defined($last);
	$_ =~ s|^(\s*
" exit 0