openpkg/man.sh

changeset 13
cb59d6afeb61
child 428
f880f219c566
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/openpkg/man.sh	Tue Jan 06 23:40:39 2009 +0100
     1.3 @@ -0,0 +1,154 @@
     1.4 +#!@l_prefix@/lib/openpkg/bash
     1.5 +##
     1.6 +##  man -- OpenPKG Tool Chain "man" command
     1.7 +##  Copyright (c) 2000-2007 OpenPKG Foundation e.V. <http://openpkg.net/>
     1.8 +##  Copyright (c) 2000-2007 Ralf S. Engelschall <http://engelschall.com/>
     1.9 +##
    1.10 +##  Permission to use, copy, modify, and distribute this software for
    1.11 +##  any purpose with or without fee is hereby granted, provided that
    1.12 +##  the above copyright notice and this permission notice appear in all
    1.13 +##  copies.
    1.14 +##
    1.15 +##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
    1.16 +##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    1.17 +##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    1.18 +##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
    1.19 +##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.20 +##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.21 +##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
    1.22 +##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    1.23 +##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    1.24 +##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
    1.25 +##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    1.26 +##  SUCH DAMAGE.
    1.27 +##
    1.28 +
    1.29 +#   configuration
    1.30 +prefix="@l_prefix@"
    1.31 +
    1.32 +#   minimum command line parsing
    1.33 +opt_h=no
    1.34 +while [ 1 ]; do
    1.35 +    case "$1" in
    1.36 +        -h | --help ) opt_h=yes; shift ;;
    1.37 +        * ) break ;;
    1.38 +    esac
    1.39 +done
    1.40 +if [ ".$opt_h" = .yes ]; then
    1.41 +    echo "openpkg:man:USAGE: openpkg man [--help] [<section>] <command>"
    1.42 +    exit 0
    1.43 +fi
    1.44 +if [ $# -eq 1 ]; then
    1.45 +    man_page="$1"
    1.46 +elif [ $# -eq 2 ]; then
    1.47 +    man_sect="$1"
    1.48 +    man_page="$2"
    1.49 +else
    1.50 +    echo "openpkg:man:ERROR: invalid number of arguments" 1>&2
    1.51 +    exit 1
    1.52 +fi
    1.53 +
    1.54 +#   determine path to commands
    1.55 +openpkg_tools="${OPENPKG_TOOLS}"
    1.56 +openpkg_tools_cmdpath="${OPENPKG_TOOLS_CMDPATH}"
    1.57 +if [ ".${openpkg_tools}" != . -a ".${openpkg_tools_cmdpath}" = . ]; then
    1.58 +    openpkg_tools_cmdpath="${openpkg_tools}/cmd:@"
    1.59 +fi
    1.60 +cmdpath="${prefix}/libexec/openpkg"
    1.61 +if [ -d "${prefix}/libexec/openpkg-tools" ]; then
    1.62 +    cmdpath="${prefix}/libexec/openpkg-tools:${cmdpath}"
    1.63 +fi
    1.64 +if [ -d "${prefix}/libexec/openpkg-audit" ]; then
    1.65 +    cmdpath="${prefix}/libexec/openpkg-audit:${cmdpath}"
    1.66 +fi
    1.67 +if [ ".${openpkg_tools_cmdpath}" != . ]; then
    1.68 +    cmdpath=`echo "${openpkg_tools_cmdpath}" | sed -e "s;@;${cmdpath};"`
    1.69 +fi
    1.70 +openpkg_tools_cmdpath=`echo "${cmdpath}" | sed -e 's/::/:/g' -e 's/^://' -e 's/:$//'`
    1.71 +
    1.72 +#   search for manual page in OpenPKG Tool Chain
    1.73 +man_file=""
    1.74 +man_type=""
    1.75 +OIFS="$IFS"; IFS=":"
    1.76 +for dir in ${openpkg_tools_cmdpath}; do
    1.77 +    IFS="$OIFS"
    1.78 +    for file in $dir/$man_page.${man_sect-"1"} $dir/$man_page.[1-9]; do
    1.79 +        if [ -f "$file" ]; then
    1.80 +            man_file="$file"
    1.81 +            man_type="roff"
    1.82 +            if [ ".$man_sect" = . ]; then
    1.83 +                man_sect=`echo "$man_file" |\
    1.84 +                    sed -e 's;^;X;' -e 's;^X.*\.\([1-9]\)$;\1;' -e 's;^X.*;;'`
    1.85 +            fi
    1.86 +            break
    1.87 +        fi
    1.88 +    done
    1.89 +    if [ ".$man_type" = . ]; then
    1.90 +        for file in $dir/$man_page.pod $dir/$man_page.pl $dir/$man_page.sh; do
    1.91 +            if [ -f "$file" ]; then
    1.92 +                if [ ".`grep '^=pod' $file`" != . ]; then
    1.93 +                    man_file="$file"
    1.94 +                    man_type="pod"
    1.95 +                    break
    1.96 +                fi
    1.97 +            fi
    1.98 +        done
    1.99 +    fi
   1.100 +    if [ ".$man_type" != . ]; then
   1.101 +        break
   1.102 +    fi
   1.103 +done
   1.104 +IFS="$OIFS"
   1.105 +if [ ".$man_type" != . ]; then
   1.106 +    #   determine POD to Roff converter
   1.107 +    pod2roff=""
   1.108 +    if [ ".$man_type" = .pod ]; then
   1.109 +        pod2roff=`$prefix/lib/openpkg/shtool path -p "$prefix/bin:$PATH" pod2man`
   1.110 +        if [ ".$pod2roff" = . ]; then
   1.111 +            echo "openpkg:man:ERROR: unable to find POD to Roff converter (pod2man)" 1>&2
   1.112 +            exit 1
   1.113 +        fi
   1.114 +        if [ ".$man_sect" = . ]; then
   1.115 +            man_sect=1
   1.116 +        fi
   1.117 +        pod2roff="$pod2roff --section=${man_sect}"
   1.118 +        pod2roff="$pod2roff --release=\"$man_page(${man_sect})\""
   1.119 +        pod2roff="$pod2roff --center=OpenPKG --date=OpenPKG"
   1.120 +        pod2roff="$pod2roff --quotes=none"
   1.121 +    fi
   1.122 +
   1.123 +    #   determine Roff to ASCII converter
   1.124 +    roff2ascii=`$prefix/lib/openpkg/shtool path -p "$prefix/bin:$PATH" nroff groff`
   1.125 +    if [ ".$roff2ascii" = . ]; then
   1.126 +        echo "openpkg:man:ERROR: unable to find Roff to ASCII converter (nroff, groff)" 1>&2
   1.127 +        exit 1
   1.128 +    fi
   1.129 +    roff2ascii="$roff2ascii -man"
   1.130 +    case "$roff2ascii" in
   1.131 +        */groff ) roff2ascii="$roff2ascii -Tascii" ;;
   1.132 +    esac
   1.133 +
   1.134 +    #   determine pager
   1.135 +    pager="$PAGER"
   1.136 +    if [ ".$pager" = . ]; then
   1.137 +        pager=`$prefix/lib/openpkg/shtool path -p "$prefix/bin:$PATH" less more cat`
   1.138 +    fi
   1.139 +    if [ ".$pager" = . ]; then
   1.140 +        echo "openpkg:man:ERROR: unable to find text viewer ($PAGER, less, more, cat)" 1>&2
   1.141 +        exit 1
   1.142 +    fi
   1.143 +
   1.144 +    #   render the manual page to the TTY
   1.145 +    if [ ".$man_type" = .roff ]; then
   1.146 +        eval "$roff2ascii $man_file | $pager"
   1.147 +    elif [ ".$man_type" = .pod ]; then
   1.148 +        eval "$pod2roff $man_file | $roff2ascii | $pager"
   1.149 +    fi
   1.150 +    exit 0
   1.151 +fi
   1.152 +
   1.153 +#   fallback to the man(1) command
   1.154 +MANPATH="$prefix/man:$prefix/local/man:${MANPATH-/usr/man}:/usr/man:/usr/share/man"
   1.155 +export MANPATH
   1.156 +eval "exec man $man_sect $man_page"
   1.157 +

mercurial