1c535eb59SGordon Tetlow#! /bin/sh 2c535eb59SGordon Tetlow# 34d846d26SWarner Losh# SPDX-License-Identifier: BSD-2-Clause 41de7b4b8SPedro F. Giffuni# 5c535eb59SGordon Tetlow# Copyright (c) 2010 Gordon Tetlow 6c535eb59SGordon Tetlow# All rights reserved. 7c535eb59SGordon Tetlow# 8c535eb59SGordon Tetlow# Redistribution and use in source and binary forms, with or without 9c535eb59SGordon Tetlow# modification, are permitted provided that the following conditions 10c535eb59SGordon Tetlow# are met: 11c535eb59SGordon Tetlow# 1. Redistributions of source code must retain the above copyright 12c535eb59SGordon Tetlow# notice, this list of conditions and the following disclaimer. 13c535eb59SGordon Tetlow# 2. Redistributions in binary form must reproduce the above copyright 14c535eb59SGordon Tetlow# notice, this list of conditions and the following disclaimer in the 15c535eb59SGordon Tetlow# documentation and/or other materials provided with the distribution. 16c535eb59SGordon Tetlow# 17c535eb59SGordon Tetlow# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18c535eb59SGordon Tetlow# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19c535eb59SGordon Tetlow# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20c535eb59SGordon Tetlow# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21c535eb59SGordon Tetlow# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22c535eb59SGordon Tetlow# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23c535eb59SGordon Tetlow# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24c535eb59SGordon Tetlow# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25c535eb59SGordon Tetlow# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26c535eb59SGordon Tetlow# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27c535eb59SGordon Tetlow# SUCH DAMAGE. 28c535eb59SGordon Tetlow# 29c535eb59SGordon Tetlow 30433c5a8aSWolfram Schneider# Rendering a manual page is fast. Even a manual page several 100k in size 31433c5a8aSWolfram Schneider# takes less than a CPU second. If it takes much longer, it is very likely 32433c5a8aSWolfram Schneider# that a tool like mandoc(1) is running in an infinite loop. In this case 33433c5a8aSWolfram Schneider# it is better to terminate it. 34433c5a8aSWolfram Schneiderulimit -t 20 35433c5a8aSWolfram Schneider 36*14a5c106SWolfram Schneider# do not ignore the exit status of roff tools 37*14a5c106SWolfram Schneiderset -o pipefail 38*14a5c106SWolfram Schneider 39*14a5c106SWolfram Schneider# ignore SIGPIPE exits because pagers may exit before reading all their input. 40*14a5c106SWolfram Schneidertrap '' SIGPIPE 41*14a5c106SWolfram Schneider 42c535eb59SGordon Tetlow# Usage: add_to_manpath path 43c535eb59SGordon Tetlow# Adds a variable to manpath while ensuring we don't have duplicates. 44c535eb59SGordon Tetlow# Returns true if we were able to add something. False otherwise. 45c535eb59SGordon Tetlowadd_to_manpath() { 46c535eb59SGordon Tetlow case "$manpath" in 47c535eb59SGordon Tetlow *:$1) decho " Skipping duplicate manpath entry $1" 2 ;; 48c535eb59SGordon Tetlow $1:*) decho " Skipping duplicate manpath entry $1" 2 ;; 49c535eb59SGordon Tetlow *:$1:*) decho " Skipping duplicate manpath entry $1" 2 ;; 50c535eb59SGordon Tetlow *) if [ -d "$1" ]; then 51c535eb59SGordon Tetlow decho " Adding $1 to manpath" 52c535eb59SGordon Tetlow manpath="$manpath:$1" 53c535eb59SGordon Tetlow return 0 54c535eb59SGordon Tetlow fi 55c535eb59SGordon Tetlow ;; 56c535eb59SGordon Tetlow esac 57c535eb59SGordon Tetlow 58c535eb59SGordon Tetlow return 1 59c535eb59SGordon Tetlow} 60c535eb59SGordon Tetlow 61c535eb59SGordon Tetlow# Usage: build_manlocales 62c535eb59SGordon Tetlow# Builds a correct MANLOCALES variable. 63c535eb59SGordon Tetlowbuild_manlocales() { 64c535eb59SGordon Tetlow # If the user has set manlocales, who are we to argue. 65c535eb59SGordon Tetlow if [ -n "$MANLOCALES" ]; then 66c535eb59SGordon Tetlow return 67c535eb59SGordon Tetlow fi 68c535eb59SGordon Tetlow 69c535eb59SGordon Tetlow parse_configs 70c535eb59SGordon Tetlow 71c535eb59SGordon Tetlow # Trim leading colon 72c535eb59SGordon Tetlow MANLOCALES=${manlocales#:} 73c535eb59SGordon Tetlow 74c535eb59SGordon Tetlow decho "Available manual locales: $MANLOCALES" 75c535eb59SGordon Tetlow} 76c535eb59SGordon Tetlow 778edb6fb5SMohamed Akram# Usage: build_mansect 788edb6fb5SMohamed Akram# Builds a correct MANSECT variable. 798edb6fb5SMohamed Akrambuild_mansect() { 808edb6fb5SMohamed Akram # If the user has set mansect, who are we to argue. 818edb6fb5SMohamed Akram if [ -n "$MANSECT" ]; then 828edb6fb5SMohamed Akram return 838edb6fb5SMohamed Akram fi 848edb6fb5SMohamed Akram 858edb6fb5SMohamed Akram parse_configs 868edb6fb5SMohamed Akram 878edb6fb5SMohamed Akram # Trim leading colon 888edb6fb5SMohamed Akram MANSECT=${mansect#:} 898edb6fb5SMohamed Akram 908edb6fb5SMohamed Akram if [ -z "$MANSECT" ]; then 918edb6fb5SMohamed Akram MANSECT=$man_default_sections 928edb6fb5SMohamed Akram fi 938edb6fb5SMohamed Akram decho "Using manual sections: $MANSECT" 948edb6fb5SMohamed Akram} 958edb6fb5SMohamed Akram 96c535eb59SGordon Tetlow# Usage: build_manpath 97c535eb59SGordon Tetlow# Builds a correct MANPATH variable. 98c535eb59SGordon Tetlowbuild_manpath() { 99c535eb59SGordon Tetlow local IFS 100c535eb59SGordon Tetlow 101c535eb59SGordon Tetlow # If the user has set a manpath, who are we to argue. 102c535eb59SGordon Tetlow if [ -n "$MANPATH" ]; then 103b2394e73SBaptiste Daroussin case "$MANPATH" in 104b2394e73SBaptiste Daroussin *:) PREPEND_MANPATH=${MANPATH} ;; 105b2394e73SBaptiste Daroussin :*) APPEND_MANPATH=${MANPATH} ;; 106b2394e73SBaptiste Daroussin *::*) 107b2394e73SBaptiste Daroussin PREPEND_MANPATH=${MANPATH%%::*} 108b2394e73SBaptiste Daroussin APPEND_MANPATH=${MANPATH#*::} 109b2394e73SBaptiste Daroussin ;; 110b2394e73SBaptiste Daroussin *) return ;; 111b2394e73SBaptiste Daroussin esac 112b2394e73SBaptiste Daroussin fi 113b2394e73SBaptiste Daroussin 114b2394e73SBaptiste Daroussin if [ -n "$PREPEND_MANPATH" ]; then 115b2394e73SBaptiste Daroussin IFS=: 116b2394e73SBaptiste Daroussin for path in $PREPEND_MANPATH; do 117b2394e73SBaptiste Daroussin add_to_manpath "$path" 118b2394e73SBaptiste Daroussin done 119b2394e73SBaptiste Daroussin unset IFS 120c535eb59SGordon Tetlow fi 121c535eb59SGordon Tetlow 122c535eb59SGordon Tetlow search_path 123c535eb59SGordon Tetlow 124c535eb59SGordon Tetlow decho "Adding default manpath entries" 125c535eb59SGordon Tetlow IFS=: 126c535eb59SGordon Tetlow for path in $man_default_path; do 127c535eb59SGordon Tetlow add_to_manpath "$path" 128c535eb59SGordon Tetlow done 129c535eb59SGordon Tetlow unset IFS 130c535eb59SGordon Tetlow 131c535eb59SGordon Tetlow parse_configs 132c535eb59SGordon Tetlow 133b2394e73SBaptiste Daroussin if [ -n "$APPEND_MANPATH" ]; then 134b2394e73SBaptiste Daroussin IFS=: 135b2394e73SBaptiste Daroussin for path in $APPEND_MANPATH; do 136b2394e73SBaptiste Daroussin add_to_manpath "$path" 137b2394e73SBaptiste Daroussin done 138b2394e73SBaptiste Daroussin unset IFS 139b2394e73SBaptiste Daroussin fi 140c535eb59SGordon Tetlow # Trim leading colon 141c535eb59SGordon Tetlow MANPATH=${manpath#:} 142c535eb59SGordon Tetlow 143c535eb59SGordon Tetlow decho "Using manual path: $MANPATH" 144c535eb59SGordon Tetlow} 145c535eb59SGordon Tetlow 146c535eb59SGordon Tetlow# Usage: check_cat catglob 147c535eb59SGordon Tetlow# Checks to see if a cat glob is available. 148c535eb59SGordon Tetlowcheck_cat() { 149c535eb59SGordon Tetlow if exists "$1"; then 150c535eb59SGordon Tetlow use_cat=yes 151c535eb59SGordon Tetlow catpage=$found 152c4368d03SWolfram Schneider setup_cattool "$catpage" 153c4368d03SWolfram Schneider decho " Found catpage \"$catpage\"" 154c535eb59SGordon Tetlow return 0 155c535eb59SGordon Tetlow else 156c535eb59SGordon Tetlow return 1 157c535eb59SGordon Tetlow fi 158c535eb59SGordon Tetlow} 159c535eb59SGordon Tetlow 160c535eb59SGordon Tetlow# Usage: check_man manglob catglob 161c535eb59SGordon Tetlow# Given 2 globs, figures out if the manglob is available, if so, check to 162c535eb59SGordon Tetlow# see if the catglob is also available and up to date. 163c535eb59SGordon Tetlowcheck_man() { 164c535eb59SGordon Tetlow if exists "$1"; then 165c535eb59SGordon Tetlow # We have a match, check for a cat page 166c535eb59SGordon Tetlow manpage=$found 16778948070SWolfram Schneider setup_cattool "$manpage" 16878948070SWolfram Schneider decho " Found manpage \"$manpage\"" 169c535eb59SGordon Tetlow 170a0094449SRuslan Ermilov if [ -n "${use_width}" ]; then 171a0094449SRuslan Ermilov # non-standard width 172a0094449SRuslan Ermilov unset use_cat 173a0094449SRuslan Ermilov decho " Skipping catpage: non-standard page width" 17478948070SWolfram Schneider elif exists "$2" && is_newer $found "$manpage"; then 175c535eb59SGordon Tetlow # cat page found and is newer, use that 176c535eb59SGordon Tetlow use_cat=yes 177c535eb59SGordon Tetlow catpage=$found 178c4368d03SWolfram Schneider setup_cattool "$catpage" 179c4368d03SWolfram Schneider decho " Using catpage \"$catpage\"" 180c535eb59SGordon Tetlow else 181c535eb59SGordon Tetlow # no cat page or is older 182c535eb59SGordon Tetlow unset use_cat 183c535eb59SGordon Tetlow decho " Skipping catpage: not found or old" 184c535eb59SGordon Tetlow fi 185c535eb59SGordon Tetlow return 0 186c535eb59SGordon Tetlow fi 187c535eb59SGordon Tetlow 188c535eb59SGordon Tetlow return 1 189c535eb59SGordon Tetlow} 190c535eb59SGordon Tetlow 191c535eb59SGordon Tetlow# Usage: decho "string" [debuglevel] 192c535eb59SGordon Tetlow# Echoes to stderr string prefaced with -- if high enough debuglevel. 193c535eb59SGordon Tetlowdecho() { 194c535eb59SGordon Tetlow if [ $debug -ge ${2:-1} ]; then 195c535eb59SGordon Tetlow echo "-- $1" >&2 196c535eb59SGordon Tetlow fi 197c535eb59SGordon Tetlow} 198c535eb59SGordon Tetlow 199c535eb59SGordon Tetlow# Usage: exists glob 200e3c7b76fSWolfram Schneider# 201e3c7b76fSWolfram Schneider# Returns true if glob resolves to a real file and store the first 202e3c7b76fSWolfram Schneider# found filename in the variable $found 203c535eb59SGordon Tetlowexists() { 204c535eb59SGordon Tetlow local IFS 205c535eb59SGordon Tetlow 206c535eb59SGordon Tetlow # Don't accidentally inherit callers IFS (breaks perl manpages) 207c535eb59SGordon Tetlow unset IFS 208c535eb59SGordon Tetlow 209c535eb59SGordon Tetlow # Use some globbing tricks in the shell to determine if a file 210c535eb59SGordon Tetlow # exists or not. 211c535eb59SGordon Tetlow set +f 212e3c7b76fSWolfram Schneider for file in "$1"* 213e3c7b76fSWolfram Schneider do 214e3c7b76fSWolfram Schneider if [ -r "$file" ]; then 215e3c7b76fSWolfram Schneider found="$file" 216c535eb59SGordon Tetlow set -f 217c535eb59SGordon Tetlow return 0 218c535eb59SGordon Tetlow fi 219e3c7b76fSWolfram Schneider done 220e3c7b76fSWolfram Schneider set -f 221c535eb59SGordon Tetlow 222c535eb59SGordon Tetlow return 1 223c535eb59SGordon Tetlow} 224c535eb59SGordon Tetlow 225c535eb59SGordon Tetlow# Usage: find_file path section subdir pagename 226c535eb59SGordon Tetlow# Returns: true if something is matched and found. 227c535eb59SGordon Tetlow# Search the given path/section combo for a given page. 228c535eb59SGordon Tetlowfind_file() { 229c535eb59SGordon Tetlow local manroot catroot mann man0 catn cat0 230c535eb59SGordon Tetlow 231c535eb59SGordon Tetlow manroot="$1/man$2" 232c535eb59SGordon Tetlow catroot="$1/cat$2" 233c535eb59SGordon Tetlow if [ -n "$3" ]; then 234c535eb59SGordon Tetlow manroot="$manroot/$3" 235c535eb59SGordon Tetlow catroot="$catroot/$3" 236c535eb59SGordon Tetlow fi 237c535eb59SGordon Tetlow 238625490e8SBaptiste Daroussin if [ ! -d "$manroot" -a ! -d "$catroot" ]; then 239c535eb59SGordon Tetlow return 1 240c535eb59SGordon Tetlow fi 241c535eb59SGordon Tetlow decho " Searching directory $manroot" 2 242c535eb59SGordon Tetlow 243e3c7b76fSWolfram Schneider mann="$manroot/$4.$2" 244e3c7b76fSWolfram Schneider man0="$manroot/$4.0" 245e3c7b76fSWolfram Schneider catn="$catroot/$4.$2" 246e3c7b76fSWolfram Schneider cat0="$catroot/$4.0" 247c535eb59SGordon Tetlow 248c535eb59SGordon Tetlow # This is the behavior as seen by the original man utility. 249c535eb59SGordon Tetlow # Let's not change that which doesn't seem broken. 250c535eb59SGordon Tetlow if check_man "$mann" "$catn"; then 251c535eb59SGordon Tetlow return 0 252c535eb59SGordon Tetlow elif check_man "$man0" "$cat0"; then 253c535eb59SGordon Tetlow return 0 254c535eb59SGordon Tetlow elif check_cat "$catn"; then 255c535eb59SGordon Tetlow return 0 256c535eb59SGordon Tetlow elif check_cat "$cat0"; then 257c535eb59SGordon Tetlow return 0 258c535eb59SGordon Tetlow fi 259c535eb59SGordon Tetlow 260c535eb59SGordon Tetlow return 1 261c535eb59SGordon Tetlow} 262c535eb59SGordon Tetlow 263c535eb59SGordon Tetlow# Usage: is_newer file1 file2 264c535eb59SGordon Tetlow# Returns true if file1 is newer than file2 as calculated by mtime. 265c535eb59SGordon Tetlowis_newer() { 2669b61837aSUlrich Spörlein if ! [ "$1" -ot "$2" ]; then 2679b61837aSUlrich Spörlein decho " mtime: $1 not older than $2" 3 268c535eb59SGordon Tetlow return 0 269c535eb59SGordon Tetlow else 270c535eb59SGordon Tetlow decho " mtime: $1 older than $2" 3 271c535eb59SGordon Tetlow return 1 272c535eb59SGordon Tetlow fi 273c535eb59SGordon Tetlow} 274c535eb59SGordon Tetlow 275c535eb59SGordon Tetlow# Usage: manpath_parse_args "$@" 276c535eb59SGordon Tetlow# Parses commandline options for manpath. 277c535eb59SGordon Tetlowmanpath_parse_args() { 278c535eb59SGordon Tetlow local cmd_arg 279c535eb59SGordon Tetlow 280f555b39eSKyle Evans OPTIND=1 281c535eb59SGordon Tetlow while getopts 'Ldq' cmd_arg; do 282c535eb59SGordon Tetlow case "${cmd_arg}" in 283c535eb59SGordon Tetlow L) Lflag=Lflag ;; 284c535eb59SGordon Tetlow d) debug=$(( $debug + 1 )) ;; 285c535eb59SGordon Tetlow q) qflag=qflag ;; 286c535eb59SGordon Tetlow *) manpath_usage ;; 287c535eb59SGordon Tetlow esac 288c535eb59SGordon Tetlow done >&2 289c535eb59SGordon Tetlow} 290c535eb59SGordon Tetlow 291c535eb59SGordon Tetlow# Usage: manpath_usage 292c535eb59SGordon Tetlow# Display usage for the manpath(1) utility. 293c535eb59SGordon Tetlowmanpath_usage() { 294c535eb59SGordon Tetlow echo 'usage: manpath [-Ldq]' >&2 295c535eb59SGordon Tetlow exit 1 296c535eb59SGordon Tetlow} 297c535eb59SGordon Tetlow 298c535eb59SGordon Tetlow# Usage: manpath_warnings 299c535eb59SGordon Tetlow# Display some warnings to stderr. 300c535eb59SGordon Tetlowmanpath_warnings() { 301c535eb59SGordon Tetlow if [ -n "$Lflag" -a -n "$MANLOCALES" ]; then 302c535eb59SGordon Tetlow echo "(Warning: MANLOCALES environment variable set)" >&2 303c535eb59SGordon Tetlow fi 304c535eb59SGordon Tetlow} 305c535eb59SGordon Tetlow 306e85a6f8dSMohamed Akram# Usage: man_check_for_so path 30757cd9717SGordon Tetlow# Returns: True if able to resolve the file, false if it ended in tears. 30857cd9717SGordon Tetlow# Detects the presence of the .so directive and causes the file to be 30957cd9717SGordon Tetlow# redirected to another source file. 31057cd9717SGordon Tetlowman_check_for_so() { 31157cd9717SGordon Tetlow local IFS line tstr 31257cd9717SGordon Tetlow 31357cd9717SGordon Tetlow unset IFS 314d9405a92SBaptiste Daroussin if [ -n "$catpage" ]; then 315d9405a92SBaptiste Daroussin return 0 316d9405a92SBaptiste Daroussin fi 31757cd9717SGordon Tetlow 31857cd9717SGordon Tetlow # We need to loop to accommodate multiple .so directives. 31957cd9717SGordon Tetlow while true 32057cd9717SGordon Tetlow do 321*14a5c106SWolfram Schneider line=$($cattool "$manpage" 2>/dev/null | head -n1) 32257cd9717SGordon Tetlow case "$line" in 32357cd9717SGordon Tetlow .so*) trim "${line#.so}" 32457cd9717SGordon Tetlow decho "$manpage includes $tstr" 32557cd9717SGordon Tetlow # Glob and check for the file. 326e85a6f8dSMohamed Akram if ! check_man "$1/$tstr" ""; then 32757cd9717SGordon Tetlow decho " Unable to find $tstr" 32857cd9717SGordon Tetlow return 1 32957cd9717SGordon Tetlow fi 33057cd9717SGordon Tetlow ;; 33157cd9717SGordon Tetlow *) break ;; 33257cd9717SGordon Tetlow esac 33357cd9717SGordon Tetlow done 33457cd9717SGordon Tetlow 33557cd9717SGordon Tetlow return 0 33657cd9717SGordon Tetlow} 33757cd9717SGordon Tetlow 338b43edc06SBaptiste Daroussin# Usage: man_display_page 339b43edc06SBaptiste Daroussin# Display either the manpage or catpage depending on the use_cat variable 340c535eb59SGordon Tetlowman_display_page() { 3411fb816daSBaptiste Daroussin local IFS pipeline testline 342c535eb59SGordon Tetlow 343c535eb59SGordon Tetlow # We are called with IFS set to colon. This causes really weird 344c535eb59SGordon Tetlow # things to happen for the variables that have spaces in them. 345c535eb59SGordon Tetlow unset IFS 346c535eb59SGordon Tetlow 347c535eb59SGordon Tetlow # If we are supposed to use a catpage and we aren't using troff(1) 348c535eb59SGordon Tetlow # just zcat the catpage and we are done. 349c535eb59SGordon Tetlow if [ -z "$tflag" -a -n "$use_cat" ]; then 350c535eb59SGordon Tetlow if [ -n "$wflag" ]; then 35178948070SWolfram Schneider echo "$catpage (source: \"$manpage\")" 352c535eb59SGordon Tetlow ret=0 353c535eb59SGordon Tetlow else 354c535eb59SGordon Tetlow if [ $debug -gt 0 ]; then 355c4368d03SWolfram Schneider decho "Command: $cattool \"$catpage\" | $MANPAGER" 356c535eb59SGordon Tetlow ret=0 357c535eb59SGordon Tetlow else 358b8a484ecSWolfram Schneider $cattool "$catpage" | $MANPAGER 359c535eb59SGordon Tetlow ret=$? 360c535eb59SGordon Tetlow fi 361c535eb59SGordon Tetlow fi 362c535eb59SGordon Tetlow return 363c535eb59SGordon Tetlow fi 364c535eb59SGordon Tetlow 365c535eb59SGordon Tetlow # Okay, we are using the manpage, do we just need to output the 366c535eb59SGordon Tetlow # name of the manpage? 367c535eb59SGordon Tetlow if [ -n "$wflag" ]; then 368c535eb59SGordon Tetlow echo "$manpage" 369c535eb59SGordon Tetlow ret=0 370c535eb59SGordon Tetlow return 371c535eb59SGordon Tetlow fi 372c535eb59SGordon Tetlow 373d433cf9aSBaptiste Daroussin if [ -n "$use_width" ]; then 374d433cf9aSBaptiste Daroussin mandoc_args="-O width=${use_width}" 375d433cf9aSBaptiste Daroussin fi 376451c2becSBaptiste Daroussin testline="mandoc -Tlint -Wunsupp >/dev/null 2>&1" 377449a792dSBaptiste Daroussin if [ -n "$tflag" ]; then 378449a792dSBaptiste Daroussin pipeline="mandoc -Tps $mandoc_args" 379449a792dSBaptiste Daroussin else 380d433cf9aSBaptiste Daroussin pipeline="mandoc $mandoc_args | $MANPAGER" 381449a792dSBaptiste Daroussin fi 382d6096801SBaptiste Daroussin 383b8a484ecSWolfram Schneider if ! $cattool "$manpage" | eval "$testline"; then 384f17575acSBaptiste Daroussin if which -s groff; then 385d6096801SBaptiste Daroussin man_display_page_groff 386d6096801SBaptiste Daroussin else 387d6096801SBaptiste Daroussin echo "This manpage needs groff(1) to be rendered" >&2 388d6096801SBaptiste Daroussin echo "First install groff(1): " >&2 389d6096801SBaptiste Daroussin echo "pkg install groff " >&2 390d6096801SBaptiste Daroussin ret=1 391d6096801SBaptiste Daroussin fi 392d6096801SBaptiste Daroussin return 393d6096801SBaptiste Daroussin fi 394d6096801SBaptiste Daroussin 395d6096801SBaptiste Daroussin if [ $debug -gt 0 ]; then 396b8a484ecSWolfram Schneider decho "Command: $cattool \"$manpage\" | eval \"$pipeline\"" 397d6096801SBaptiste Daroussin ret=0 398d6096801SBaptiste Daroussin else 399b8a484ecSWolfram Schneider $cattool "$manpage" | eval "$pipeline" 400d6096801SBaptiste Daroussin ret=$? 401d6096801SBaptiste Daroussin fi 402d6096801SBaptiste Daroussin} 403d6096801SBaptiste Daroussin 404b43edc06SBaptiste Daroussin# Usage: man_display_page_groff 405b43edc06SBaptiste Daroussin# Display the manpage using groff 406d6096801SBaptiste Daroussinman_display_page_groff() { 407d6096801SBaptiste Daroussin local EQN NROFF PIC TBL TROFF REFER VGRIND 408d6096801SBaptiste Daroussin local IFS l nroff_dev pipeline preproc_arg tool 409d6096801SBaptiste Daroussin 410c535eb59SGordon Tetlow # So, we really do need to parse the manpage. First, figure out the 411c535eb59SGordon Tetlow # device flag (-T) we have to pass to eqn(1) and groff(1). Then, 412c535eb59SGordon Tetlow # setup the pipeline of commands based on the user's request. 413c535eb59SGordon Tetlow 414deeff310SGordon Tetlow # If the manpage is from a particular charset, we need to setup nroff 415deeff310SGordon Tetlow # to properly output for the correct device. 416deeff310SGordon Tetlow case "${manpage}" in 417deeff310SGordon Tetlow *.${man_charset}/*) 418c535eb59SGordon Tetlow # I don't pretend to know this; I'm just copying from the 419c535eb59SGordon Tetlow # previous version of man(1). 420c535eb59SGordon Tetlow case "$man_charset" in 421c535eb59SGordon Tetlow KOI8-R) nroff_dev="koi8-r" ;; 422c535eb59SGordon Tetlow ISO8859-1) nroff_dev="latin1" ;; 423c535eb59SGordon Tetlow ISO8859-15) nroff_dev="latin1" ;; 424c535eb59SGordon Tetlow UTF-8) nroff_dev="utf8" ;; 425c535eb59SGordon Tetlow *) nroff_dev="ascii" ;; 426c535eb59SGordon Tetlow esac 427c535eb59SGordon Tetlow 428deeff310SGordon Tetlow NROFF="$NROFF -T$nroff_dev" 429c535eb59SGordon Tetlow EQN="$EQN -T$nroff_dev" 430c535eb59SGordon Tetlow 431deeff310SGordon Tetlow # Iff the manpage is from the locale and not just the charset, 432deeff310SGordon Tetlow # then we need to define the locale string. 433deeff310SGordon Tetlow case "${manpage}" in 434deeff310SGordon Tetlow */${man_lang}_${man_country}.${man_charset}/*) 435deeff310SGordon Tetlow NROFF="$NROFF -dlocale=$man_lang.$man_charset" 436deeff310SGordon Tetlow ;; 437deeff310SGordon Tetlow */${man_lang}.${man_charset}/*) 438deeff310SGordon Tetlow NROFF="$NROFF -dlocale=$man_lang.$man_charset" 439deeff310SGordon Tetlow ;; 440deeff310SGordon Tetlow esac 441deeff310SGordon Tetlow 442c535eb59SGordon Tetlow # Allow language specific calls to override the default 443c535eb59SGordon Tetlow # set of utilities. 444c535eb59SGordon Tetlow l=$(echo $man_lang | tr [:lower:] [:upper:]) 445b70e2025SRuslan Ermilov for tool in EQN NROFF PIC TBL TROFF REFER VGRIND; do 446c535eb59SGordon Tetlow eval "$tool=\${${tool}_$l:-\$$tool}" 447c535eb59SGordon Tetlow done 448c535eb59SGordon Tetlow ;; 449c535eb59SGordon Tetlow *) NROFF="$NROFF -Tascii" 450c535eb59SGordon Tetlow EQN="$EQN -Tascii" 451c535eb59SGordon Tetlow ;; 452c535eb59SGordon Tetlow esac 453c535eb59SGordon Tetlow 454a6a3e856SRuslan Ermilov if [ -z "$MANCOLOR" ]; then 455a6a3e856SRuslan Ermilov NROFF="$NROFF -P-c" 456a6a3e856SRuslan Ermilov fi 457a6a3e856SRuslan Ermilov 458a0094449SRuslan Ermilov if [ -n "${use_width}" ]; then 459a0094449SRuslan Ermilov NROFF="$NROFF -rLL=${use_width}n -rLT=${use_width}n" 460a0094449SRuslan Ermilov fi 461a0094449SRuslan Ermilov 462c535eb59SGordon Tetlow if [ -n "$MANROFFSEQ" ]; then 463c535eb59SGordon Tetlow set -- -$MANROFFSEQ 464f555b39eSKyle Evans OPTIND=1 465c535eb59SGordon Tetlow while getopts 'egprtv' preproc_arg; do 466c535eb59SGordon Tetlow case "${preproc_arg}" in 467c535eb59SGordon Tetlow e) pipeline="$pipeline | $EQN" ;; 468487ac9acSUlrich Spörlein g) ;; # Ignore for compatibility. 469c535eb59SGordon Tetlow p) pipeline="$pipeline | $PIC" ;; 470c535eb59SGordon Tetlow r) pipeline="$pipeline | $REFER" ;; 471b70e2025SRuslan Ermilov t) pipeline="$pipeline | $TBL" ;; 472c535eb59SGordon Tetlow v) pipeline="$pipeline | $VGRIND" ;; 473c535eb59SGordon Tetlow *) usage ;; 474c535eb59SGordon Tetlow esac 475c535eb59SGordon Tetlow done 476c535eb59SGordon Tetlow # Strip the leading " | " from the resulting pipeline. 477c535eb59SGordon Tetlow pipeline="${pipeline#" | "}" 478c535eb59SGordon Tetlow else 479c535eb59SGordon Tetlow pipeline="$TBL" 480c535eb59SGordon Tetlow fi 481c535eb59SGordon Tetlow 482c535eb59SGordon Tetlow if [ -n "$tflag" ]; then 483c535eb59SGordon Tetlow pipeline="$pipeline | $TROFF" 484c535eb59SGordon Tetlow else 485a6a3e856SRuslan Ermilov pipeline="$pipeline | $NROFF | $MANPAGER" 486c535eb59SGordon Tetlow fi 487c535eb59SGordon Tetlow 488c535eb59SGordon Tetlow if [ $debug -gt 0 ]; then 489b8a484ecSWolfram Schneider decho "Command: $cattool \"$manpage\" | eval \"$pipeline\"" 490c535eb59SGordon Tetlow ret=0 491c535eb59SGordon Tetlow else 492b8a484ecSWolfram Schneider $cattool "$manpage" | eval "$pipeline" 493c535eb59SGordon Tetlow ret=$? 494c535eb59SGordon Tetlow fi 495c535eb59SGordon Tetlow} 496c535eb59SGordon Tetlow 497c535eb59SGordon Tetlow# Usage: man_find_and_display page 498c535eb59SGordon Tetlow# Search through the manpaths looking for the given page. 499c535eb59SGordon Tetlowman_find_and_display() { 500c535eb59SGordon Tetlow local found_page locpath p path sect 501c535eb59SGordon Tetlow 5023d9127f1SGordon Tetlow # Check to see if it's a file. But only if it has a '/' in 5033d9127f1SGordon Tetlow # the filename. 5043d9127f1SGordon Tetlow case "$1" in 5053d9127f1SGordon Tetlow */*) if [ -f "$1" -a -r "$1" ]; then 5063d9127f1SGordon Tetlow decho "Found a usable page, displaying that" 5073d9127f1SGordon Tetlow unset use_cat 5083d9127f1SGordon Tetlow manpage="$1" 50978948070SWolfram Schneider setup_cattool "$manpage" 510e85a6f8dSMohamed Akram p=$(cd "$(dirname "$manpage")" && pwd) 511e85a6f8dSMohamed Akram case "$(basename "$p")" in 512e85a6f8dSMohamed Akram man*|cat*) p=$p/.. ;; 513e85a6f8dSMohamed Akram *) p=$p/../.. ;; 514e85a6f8dSMohamed Akram esac 515e85a6f8dSMohamed Akram if man_check_for_so "$p"; then 51657cd9717SGordon Tetlow found_page=yes 5173d9127f1SGordon Tetlow man_display_page 51857cd9717SGordon Tetlow fi 5193d9127f1SGordon Tetlow return 5203d9127f1SGordon Tetlow fi 5213d9127f1SGordon Tetlow ;; 5223d9127f1SGordon Tetlow esac 5233d9127f1SGordon Tetlow 524c535eb59SGordon Tetlow IFS=: 525c535eb59SGordon Tetlow for sect in $MANSECT; do 526c535eb59SGordon Tetlow decho "Searching section $sect" 2 527c535eb59SGordon Tetlow for path in $MANPATH; do 528c535eb59SGordon Tetlow for locpath in $locpaths; do 529c535eb59SGordon Tetlow p=$path/$locpath 530c535eb59SGordon Tetlow p=${p%/.} # Rid ourselves of the trailing /. 531c535eb59SGordon Tetlow 532c535eb59SGordon Tetlow # Check if there is a MACHINE specific manpath. 533c535eb59SGordon Tetlow if find_file $p $sect $MACHINE "$1"; then 534e85a6f8dSMohamed Akram if man_check_for_so $p; then 535c535eb59SGordon Tetlow found_page=yes 536c535eb59SGordon Tetlow man_display_page 5371d7c660aSGordon Tetlow if [ -n "$aflag" ]; then 5381d7c660aSGordon Tetlow continue 2 5391d7c660aSGordon Tetlow else 540c535eb59SGordon Tetlow return 541c535eb59SGordon Tetlow fi 542c535eb59SGordon Tetlow fi 54357cd9717SGordon Tetlow fi 544c535eb59SGordon Tetlow 545c535eb59SGordon Tetlow # Check if there is a MACHINE_ARCH 546c535eb59SGordon Tetlow # specific manpath. 547c535eb59SGordon Tetlow if find_file $p $sect $MACHINE_ARCH "$1"; then 548e85a6f8dSMohamed Akram if man_check_for_so $p; then 549c535eb59SGordon Tetlow found_page=yes 550c535eb59SGordon Tetlow man_display_page 5511d7c660aSGordon Tetlow if [ -n "$aflag" ]; then 5521d7c660aSGordon Tetlow continue 2 5531d7c660aSGordon Tetlow else 554c535eb59SGordon Tetlow return 555c535eb59SGordon Tetlow fi 556c535eb59SGordon Tetlow fi 55757cd9717SGordon Tetlow fi 558c535eb59SGordon Tetlow 559c535eb59SGordon Tetlow # Check plain old manpath. 560c535eb59SGordon Tetlow if find_file $p $sect '' "$1"; then 561e85a6f8dSMohamed Akram if man_check_for_so $p; then 562c535eb59SGordon Tetlow found_page=yes 563c535eb59SGordon Tetlow man_display_page 5641d7c660aSGordon Tetlow if [ -n "$aflag" ]; then 5651d7c660aSGordon Tetlow continue 2 5661d7c660aSGordon Tetlow else 567c535eb59SGordon Tetlow return 568c535eb59SGordon Tetlow fi 569c535eb59SGordon Tetlow fi 57057cd9717SGordon Tetlow fi 571c535eb59SGordon Tetlow done 572c535eb59SGordon Tetlow done 573c535eb59SGordon Tetlow done 574c535eb59SGordon Tetlow unset IFS 575c535eb59SGordon Tetlow 576c535eb59SGordon Tetlow # Nothing? Well, we are done then. 577c535eb59SGordon Tetlow if [ -z "$found_page" ]; then 57878948070SWolfram Schneider echo "No manual entry for \"$1\"" >&2 579c535eb59SGordon Tetlow ret=1 580c535eb59SGordon Tetlow return 581c535eb59SGordon Tetlow fi 582c535eb59SGordon Tetlow} 583c535eb59SGordon Tetlow 5848edb6fb5SMohamed Akram# Usage: man_parse_opts "$@" 585c535eb59SGordon Tetlow# Parses commandline options for man. 5868edb6fb5SMohamed Akramman_parse_opts() { 5878edb6fb5SMohamed Akram local cmd_arg 588c535eb59SGordon Tetlow 589f555b39eSKyle Evans OPTIND=1 5901594084fSFernando Apesteguía while getopts 'K:M:P:S:adfhkm:op:tw' cmd_arg; do 591c535eb59SGordon Tetlow case "${cmd_arg}" in 5921594084fSFernando Apesteguía K) Kflag=Kflag 5931594084fSFernando Apesteguía REGEXP=$OPTARG ;; 594c535eb59SGordon Tetlow M) MANPATH=$OPTARG ;; 595a6a3e856SRuslan Ermilov P) MANPAGER=$OPTARG ;; 596c535eb59SGordon Tetlow S) MANSECT=$OPTARG ;; 597c535eb59SGordon Tetlow a) aflag=aflag ;; 598c535eb59SGordon Tetlow d) debug=$(( $debug + 1 )) ;; 599c535eb59SGordon Tetlow f) fflag=fflag ;; 600c535eb59SGordon Tetlow h) man_usage 0 ;; 601c535eb59SGordon Tetlow k) kflag=kflag ;; 602c535eb59SGordon Tetlow m) mflag=$OPTARG ;; 603c535eb59SGordon Tetlow o) oflag=oflag ;; 604c535eb59SGordon Tetlow p) MANROFFSEQ=$OPTARG ;; 605c535eb59SGordon Tetlow t) tflag=tflag ;; 606c535eb59SGordon Tetlow w) wflag=wflag ;; 607c535eb59SGordon Tetlow *) man_usage ;; 608c535eb59SGordon Tetlow esac 609c535eb59SGordon Tetlow done >&2 610c535eb59SGordon Tetlow 611c535eb59SGordon Tetlow shift $(( $OPTIND - 1 )) 612c535eb59SGordon Tetlow 613c535eb59SGordon Tetlow # Check the args for incompatible options. 6141594084fSFernando Apesteguía 6151594084fSFernando Apesteguía case "${Kflag}${fflag}${kflag}${tflag}${wflag}" in 6161594084fSFernando Apesteguía Kflagfflag*) echo "Incompatible options: -K and -f"; man_usage ;; 6171594084fSFernando Apesteguía Kflag*kflag*) echo "Incompatible options: -K and -k"; man_usage ;; 6181594084fSFernando Apesteguía Kflag*tflag) echo "Incompatible options: -K and -t"; man_usage ;; 619c535eb59SGordon Tetlow fflagkflag*) echo "Incompatible options: -f and -k"; man_usage ;; 620c535eb59SGordon Tetlow fflag*tflag*) echo "Incompatible options: -f and -t"; man_usage ;; 621c535eb59SGordon Tetlow fflag*wflag) echo "Incompatible options: -f and -w"; man_usage ;; 622c535eb59SGordon Tetlow *kflagtflag*) echo "Incompatible options: -k and -t"; man_usage ;; 623c535eb59SGordon Tetlow *kflag*wflag) echo "Incompatible options: -k and -w"; man_usage ;; 624c535eb59SGordon Tetlow *tflagwflag) echo "Incompatible options: -t and -w"; man_usage ;; 625c535eb59SGordon Tetlow esac 626c535eb59SGordon Tetlow 627c535eb59SGordon Tetlow # Short circuit for whatis(1) and apropos(1) 628c535eb59SGordon Tetlow if [ -n "$fflag" ]; then 629c535eb59SGordon Tetlow do_whatis "$@" 630c535eb59SGordon Tetlow exit 631c535eb59SGordon Tetlow fi 632c535eb59SGordon Tetlow 633c535eb59SGordon Tetlow if [ -n "$kflag" ]; then 634c535eb59SGordon Tetlow do_apropos "$@" 635c535eb59SGordon Tetlow exit 636c535eb59SGordon Tetlow fi 637c535eb59SGordon Tetlow} 638c535eb59SGordon Tetlow 639c535eb59SGordon Tetlow# Usage: man_setup 640c535eb59SGordon Tetlow# Setup various trivial but essential variables. 641c535eb59SGordon Tetlowman_setup() { 642c535eb59SGordon Tetlow # Setup machine and architecture variables. 643c535eb59SGordon Tetlow if [ -n "$mflag" ]; then 644c535eb59SGordon Tetlow MACHINE_ARCH=${mflag%%:*} 645c535eb59SGordon Tetlow MACHINE=${mflag##*:} 646c535eb59SGordon Tetlow fi 647c535eb59SGordon Tetlow if [ -z "$MACHINE_ARCH" ]; then 64882db8a5eSGordon Tetlow MACHINE_ARCH=$($SYSCTL -n hw.machine_arch) 649c535eb59SGordon Tetlow fi 650c535eb59SGordon Tetlow if [ -z "$MACHINE" ]; then 65182db8a5eSGordon Tetlow MACHINE=$($SYSCTL -n hw.machine) 652c535eb59SGordon Tetlow fi 653c535eb59SGordon Tetlow decho "Using architecture: $MACHINE_ARCH:$MACHINE" 654c535eb59SGordon Tetlow 655c535eb59SGordon Tetlow setup_pager 656c535eb59SGordon Tetlow build_manpath 6578edb6fb5SMohamed Akram build_mansect 658c535eb59SGordon Tetlow man_setup_locale 659a0094449SRuslan Ermilov man_setup_width 660a0094449SRuslan Ermilov} 661a0094449SRuslan Ermilov 662a0094449SRuslan Ermilov# Usage: man_setup_width 663a0094449SRuslan Ermilov# Set up page width. 664a0094449SRuslan Ermilovman_setup_width() { 665a0094449SRuslan Ermilov local sizes 666a0094449SRuslan Ermilov 667a0094449SRuslan Ermilov unset use_width 668a0094449SRuslan Ermilov case "$MANWIDTH" in 669a0094449SRuslan Ermilov [0-9]*) 670a0094449SRuslan Ermilov if [ "$MANWIDTH" -gt 0 2>/dev/null ]; then 671a0094449SRuslan Ermilov use_width=$MANWIDTH 672a0094449SRuslan Ermilov fi 673a0094449SRuslan Ermilov ;; 674a0094449SRuslan Ermilov [Tt][Tt][Yy]) 675a0094449SRuslan Ermilov if { sizes=$($STTY size 0>&3 2>/dev/null); } 3>&1; then 676a0094449SRuslan Ermilov set -- $sizes 677a0094449SRuslan Ermilov if [ $2 -gt 80 ]; then 678a0094449SRuslan Ermilov use_width=$(($2-2)) 679a0094449SRuslan Ermilov fi 680a0094449SRuslan Ermilov fi 681a0094449SRuslan Ermilov ;; 682a0094449SRuslan Ermilov esac 683a0094449SRuslan Ermilov if [ -n "$use_width" ]; then 684a0094449SRuslan Ermilov decho "Using non-standard page width: ${use_width}" 685a0094449SRuslan Ermilov else 686a0094449SRuslan Ermilov decho 'Using standard page width' 687a0094449SRuslan Ermilov fi 688c535eb59SGordon Tetlow} 689c535eb59SGordon Tetlow 690c535eb59SGordon Tetlow# Usage: man_setup_locale 691c535eb59SGordon Tetlow# Setup necessary locale variables. 692c535eb59SGordon Tetlowman_setup_locale() { 693deeff310SGordon Tetlow local lang_cc 6949508f8c0SYuri Pankov local locstr 695deeff310SGordon Tetlow 696deeff310SGordon Tetlow locpaths='.' 697deeff310SGordon Tetlow man_charset='US-ASCII' 698deeff310SGordon Tetlow 699c535eb59SGordon Tetlow # Setup locale information. 700c535eb59SGordon Tetlow if [ -n "$oflag" ]; then 701deeff310SGordon Tetlow decho 'Using non-localized manpages' 702deeff310SGordon Tetlow else 7039508f8c0SYuri Pankov # Use the locale tool to give us proper locale information 704deeff310SGordon Tetlow eval $( $LOCALE ) 705c535eb59SGordon Tetlow 7069508f8c0SYuri Pankov if [ -n "$LANG" ]; then 7079508f8c0SYuri Pankov locstr=$LANG 7089508f8c0SYuri Pankov else 7099508f8c0SYuri Pankov locstr=$LC_CTYPE 7109508f8c0SYuri Pankov fi 7119508f8c0SYuri Pankov 7129508f8c0SYuri Pankov case "$locstr" in 713deeff310SGordon Tetlow C) ;; 7149508f8c0SYuri Pankov C.UTF-8) ;; 715deeff310SGordon Tetlow POSIX) ;; 716deeff310SGordon Tetlow [a-z][a-z]_[A-Z][A-Z]\.*) 7179508f8c0SYuri Pankov lang_cc="${locstr%.*}" 7189508f8c0SYuri Pankov man_lang="${locstr%_*}" 719deeff310SGordon Tetlow man_country="${lang_cc#*_}" 7209508f8c0SYuri Pankov man_charset="${locstr#*.}" 7219508f8c0SYuri Pankov locpaths="$locstr" 722c535eb59SGordon Tetlow locpaths="$locpaths:$man_lang.$man_charset" 723c535eb59SGordon Tetlow if [ "$man_lang" != "en" ]; then 724c535eb59SGordon Tetlow locpaths="$locpaths:en.$man_charset" 725c535eb59SGordon Tetlow fi 726c535eb59SGordon Tetlow locpaths="$locpaths:." 727deeff310SGordon Tetlow ;; 728deeff310SGordon Tetlow *) echo 'Unknown locale, assuming C' >&2 729deeff310SGordon Tetlow ;; 730deeff310SGordon Tetlow esac 731c535eb59SGordon Tetlow fi 732deeff310SGordon Tetlow 733c535eb59SGordon Tetlow decho "Using locale paths: $locpaths" 734c535eb59SGordon Tetlow} 735c535eb59SGordon Tetlow 736c535eb59SGordon Tetlow# Usage: man_usage [exitcode] 737c535eb59SGordon Tetlow# Display usage for the man utility. 738c535eb59SGordon Tetlowman_usage() { 739c535eb59SGordon Tetlow echo 'Usage:' 7401594084fSFernando Apesteguía echo ' man [-adho] [-t | -w] [-K regexp] [-M manpath] [-P pager] [-S mansect]' 741c535eb59SGordon Tetlow echo ' [-m arch[:machine]] [-p [eprtv]] [mansect] page [...]' 742c535eb59SGordon Tetlow echo ' man -f page [...] -- Emulates whatis(1)' 743c535eb59SGordon Tetlow echo ' man -k page [...] -- Emulates apropos(1)' 744c535eb59SGordon Tetlow 745c535eb59SGordon Tetlow # When exit'ing with -h, it's not an error. 746c535eb59SGordon Tetlow exit ${1:-1} 747c535eb59SGordon Tetlow} 748c535eb59SGordon Tetlow 749c535eb59SGordon Tetlow# Usage: parse_configs 750c535eb59SGordon Tetlow# Reads the end-user adjustable config files. 751c535eb59SGordon Tetlowparse_configs() { 752c535eb59SGordon Tetlow local IFS file files 753c535eb59SGordon Tetlow 754c535eb59SGordon Tetlow if [ -n "$parsed_configs" ]; then 755c535eb59SGordon Tetlow return 756c535eb59SGordon Tetlow fi 757c535eb59SGordon Tetlow 758c535eb59SGordon Tetlow unset IFS 759c535eb59SGordon Tetlow 760c535eb59SGordon Tetlow # Read the global config first in case the user wants 761c535eb59SGordon Tetlow # to override config_local. 762c535eb59SGordon Tetlow if [ -r "$config_global" ]; then 763c535eb59SGordon Tetlow parse_file "$config_global" 764c535eb59SGordon Tetlow fi 765c535eb59SGordon Tetlow 766c535eb59SGordon Tetlow # Glob the list of files to parse. 767c535eb59SGordon Tetlow set +f 768c535eb59SGordon Tetlow files=$(echo $config_local) 769c535eb59SGordon Tetlow set -f 770c535eb59SGordon Tetlow 771c535eb59SGordon Tetlow for file in $files; do 772c535eb59SGordon Tetlow if [ -r "$file" ]; then 773c535eb59SGordon Tetlow parse_file "$file" 774c535eb59SGordon Tetlow fi 775c535eb59SGordon Tetlow done 776c535eb59SGordon Tetlow 777c535eb59SGordon Tetlow parsed_configs='yes' 778c535eb59SGordon Tetlow} 779c535eb59SGordon Tetlow 780c535eb59SGordon Tetlow# Usage: parse_file file 781c535eb59SGordon Tetlow# Reads the specified config files. 782c535eb59SGordon Tetlowparse_file() { 783c535eb59SGordon Tetlow local file line tstr var 784c535eb59SGordon Tetlow 785c535eb59SGordon Tetlow file="$1" 786c535eb59SGordon Tetlow decho "Parsing config file: $file" 787c535eb59SGordon Tetlow while read line; do 788c535eb59SGordon Tetlow decho " $line" 2 789c535eb59SGordon Tetlow case "$line" in 790c535eb59SGordon Tetlow \#*) decho " Comment" 3 791c535eb59SGordon Tetlow ;; 792c535eb59SGordon Tetlow MANPATH*) decho " MANPATH" 3 793c535eb59SGordon Tetlow trim "${line#MANPATH}" 794c535eb59SGordon Tetlow add_to_manpath "$tstr" 795c535eb59SGordon Tetlow ;; 796c535eb59SGordon Tetlow MANLOCALE*) decho " MANLOCALE" 3 797c535eb59SGordon Tetlow trim "${line#MANLOCALE}" 798c535eb59SGordon Tetlow manlocales="$manlocales:$tstr" 799c535eb59SGordon Tetlow ;; 800c535eb59SGordon Tetlow MANCONFIG*) decho " MANCONFIG" 3 801a1528c80SRuslan Ermilov trim "${line#MANCONFIG}" 802c535eb59SGordon Tetlow config_local="$tstr" 803c535eb59SGordon Tetlow ;; 8048edb6fb5SMohamed Akram MANSECT*) decho " MANSECT" 3 8058edb6fb5SMohamed Akram trim "${line#MANSECT}" 8068edb6fb5SMohamed Akram mansect="$mansect:$tstr" 8078edb6fb5SMohamed Akram ;; 808c535eb59SGordon Tetlow # Set variables in the form of FOO_BAR 809c535eb59SGordon Tetlow *_*[\ \ ]*) var="${line%%[\ \ ]*}" 810c535eb59SGordon Tetlow trim "${line#$var}" 811c535eb59SGordon Tetlow eval "$var=\"$tstr\"" 812c535eb59SGordon Tetlow decho " Parsed $var" 3 813c535eb59SGordon Tetlow ;; 814c535eb59SGordon Tetlow esac 815c535eb59SGordon Tetlow done < "$file" 816c535eb59SGordon Tetlow} 817c535eb59SGordon Tetlow 818c535eb59SGordon Tetlow# Usage: search_path 819c535eb59SGordon Tetlow# Traverse $PATH looking for manpaths. 820c535eb59SGordon Tetlowsearch_path() { 821c535eb59SGordon Tetlow local IFS p path 822c535eb59SGordon Tetlow 823c535eb59SGordon Tetlow decho "Searching PATH for man directories" 824c535eb59SGordon Tetlow 825c535eb59SGordon Tetlow IFS=: 826c535eb59SGordon Tetlow for path in $PATH; do 827971c1c42STijl Coosemans if add_to_manpath "$path/man"; then 828c535eb59SGordon Tetlow : 829c535eb59SGordon Tetlow elif add_to_manpath "$path/MAN"; then 830c535eb59SGordon Tetlow : 831c535eb59SGordon Tetlow else 832c535eb59SGordon Tetlow case "$path" in 833971c1c42STijl Coosemans */bin) p="${path%/bin}/share/man" 834c535eb59SGordon Tetlow add_to_manpath "$p" 835971c1c42STijl Coosemans p="${path%/bin}/man" 83661d5f2d1SBaptiste Daroussin add_to_manpath "$p" 837c535eb59SGordon Tetlow ;; 838c535eb59SGordon Tetlow esac 839c535eb59SGordon Tetlow fi 840c535eb59SGordon Tetlow done 841c535eb59SGordon Tetlow unset IFS 842c535eb59SGordon Tetlow 843c535eb59SGordon Tetlow if [ -z "$manpath" ]; then 844c535eb59SGordon Tetlow decho ' Unable to find any manpaths, using default' 845c535eb59SGordon Tetlow manpath=$man_default_path 846c535eb59SGordon Tetlow fi 847c535eb59SGordon Tetlow} 848c535eb59SGordon Tetlow 849c535eb59SGordon Tetlow# Usage: search_whatis cmd [arglist] 850c535eb59SGordon Tetlow# Do the heavy lifting for apropos/whatis 851c535eb59SGordon Tetlowsearch_whatis() { 852c535eb59SGordon Tetlow local IFS bad cmd f good key keywords loc opt out path rval wlist 853c535eb59SGordon Tetlow 854c535eb59SGordon Tetlow cmd="$1" 855c535eb59SGordon Tetlow shift 856c535eb59SGordon Tetlow 857c535eb59SGordon Tetlow whatis_parse_args "$@" 858c535eb59SGordon Tetlow 859c535eb59SGordon Tetlow build_manpath 860c535eb59SGordon Tetlow build_manlocales 861c535eb59SGordon Tetlow setup_pager 862c535eb59SGordon Tetlow 863c535eb59SGordon Tetlow if [ "$cmd" = "whatis" ]; then 864c535eb59SGordon Tetlow opt="-w" 865c535eb59SGordon Tetlow fi 866c535eb59SGordon Tetlow 867c535eb59SGordon Tetlow f='whatis' 868c535eb59SGordon Tetlow 869c535eb59SGordon Tetlow IFS=: 870c535eb59SGordon Tetlow for path in $MANPATH; do 871c535eb59SGordon Tetlow if [ \! -d "$path" ]; then 872c535eb59SGordon Tetlow decho "Skipping non-existent path: $path" 2 873c535eb59SGordon Tetlow continue 874c535eb59SGordon Tetlow fi 875c535eb59SGordon Tetlow 876c535eb59SGordon Tetlow if [ -f "$path/$f" -a -r "$path/$f" ]; then 877c535eb59SGordon Tetlow decho "Found whatis: $path/$f" 878c535eb59SGordon Tetlow wlist="$wlist $path/$f" 879c535eb59SGordon Tetlow fi 880c535eb59SGordon Tetlow 881c535eb59SGordon Tetlow for loc in $MANLOCALES; do 882c535eb59SGordon Tetlow if [ -f "$path/$loc/$f" -a -r "$path/$loc/$f" ]; then 883c535eb59SGordon Tetlow decho "Found whatis: $path/$loc/$f" 884c535eb59SGordon Tetlow wlist="$wlist $path/$loc/$f" 885c535eb59SGordon Tetlow fi 886c535eb59SGordon Tetlow done 887c535eb59SGordon Tetlow done 888c535eb59SGordon Tetlow unset IFS 889c535eb59SGordon Tetlow 890c535eb59SGordon Tetlow if [ -z "$wlist" ]; then 891c535eb59SGordon Tetlow echo "$cmd: no whatis databases in $MANPATH" >&2 892c535eb59SGordon Tetlow exit 1 893c535eb59SGordon Tetlow fi 894c535eb59SGordon Tetlow 895c535eb59SGordon Tetlow rval=0 896c535eb59SGordon Tetlow for key in $keywords; do 897c535eb59SGordon Tetlow out=$(grep -Ehi $opt -- "$key" $wlist) 898c535eb59SGordon Tetlow if [ -n "$out" ]; then 899c535eb59SGordon Tetlow good="$good\\n$out" 900c535eb59SGordon Tetlow else 901c535eb59SGordon Tetlow bad="$bad\\n$key: nothing appropriate" 902c535eb59SGordon Tetlow rval=1 903c535eb59SGordon Tetlow fi 904c535eb59SGordon Tetlow done 905c535eb59SGordon Tetlow 906c535eb59SGordon Tetlow # Strip leading carriage return. 907c535eb59SGordon Tetlow good=${good#\\n} 908c535eb59SGordon Tetlow bad=${bad#\\n} 909c535eb59SGordon Tetlow 910c535eb59SGordon Tetlow if [ -n "$good" ]; then 911ec13a838SMohamed Akram printf '%b\n' "$good" | $MANPAGER 912c535eb59SGordon Tetlow fi 913c535eb59SGordon Tetlow 914c535eb59SGordon Tetlow if [ -n "$bad" ]; then 915ec13a838SMohamed Akram printf '%b\n' "$bad" >&2 916c535eb59SGordon Tetlow fi 917c535eb59SGordon Tetlow 918c535eb59SGordon Tetlow exit $rval 919c535eb59SGordon Tetlow} 920c535eb59SGordon Tetlow 92157cd9717SGordon Tetlow# Usage: setup_cattool page 92257cd9717SGordon Tetlow# Finds an appropriate decompressor based on extension 92357cd9717SGordon Tetlowsetup_cattool() { 92457cd9717SGordon Tetlow case "$1" in 92557cd9717SGordon Tetlow *.bz) cattool='/usr/bin/bzcat' ;; 92657cd9717SGordon Tetlow *.bz2) cattool='/usr/bin/bzcat' ;; 927b35ea9baSMohamed Akram *.gz) cattool='/usr/bin/gzcat' ;; 92857cd9717SGordon Tetlow *.lzma) cattool='/usr/bin/lzcat' ;; 92957cd9717SGordon Tetlow *.xz) cattool='/usr/bin/xzcat' ;; 930c8abb673SCameron Katri *.zst) cattool='/usr/bin/zstdcat' ;; 93157cd9717SGordon Tetlow *) cattool='/usr/bin/zcat -f' ;; 93257cd9717SGordon Tetlow esac 93357cd9717SGordon Tetlow} 93457cd9717SGordon Tetlow 935c535eb59SGordon Tetlow# Usage: setup_pager 936a6a3e856SRuslan Ermilov# Correctly sets $MANPAGER 937c535eb59SGordon Tetlowsetup_pager() { 938c535eb59SGordon Tetlow # Setup pager. 939a6a3e856SRuslan Ermilov if [ -z "$MANPAGER" ]; then 940a6a3e856SRuslan Ermilov if [ -n "$MANCOLOR" ]; then 941a6a3e856SRuslan Ermilov MANPAGER="less -sR" 942a6a3e856SRuslan Ermilov else 943a6a3e856SRuslan Ermilov if [ -n "$PAGER" ]; then 944a6a3e856SRuslan Ermilov MANPAGER="$PAGER" 945a6a3e856SRuslan Ermilov else 94647cc9ee1SAlan Somers MANPAGER="less -s" 947c535eb59SGordon Tetlow fi 948a6a3e856SRuslan Ermilov fi 949a6a3e856SRuslan Ermilov fi 950a6a3e856SRuslan Ermilov decho "Using pager: $MANPAGER" 951c535eb59SGordon Tetlow} 952c535eb59SGordon Tetlow 953c535eb59SGordon Tetlow# Usage: trim string 954c535eb59SGordon Tetlow# Trims whitespace from beginning and end of a variable 955c535eb59SGordon Tetlowtrim() { 956c535eb59SGordon Tetlow tstr=$1 957c535eb59SGordon Tetlow while true; do 958c535eb59SGordon Tetlow case "$tstr" in 959c535eb59SGordon Tetlow [\ \ ]*) tstr="${tstr##[\ \ ]}" ;; 960c535eb59SGordon Tetlow *[\ \ ]) tstr="${tstr%%[\ \ ]}" ;; 961c535eb59SGordon Tetlow *) break ;; 962c535eb59SGordon Tetlow esac 963c535eb59SGordon Tetlow done 964c535eb59SGordon Tetlow} 965c535eb59SGordon Tetlow 966c535eb59SGordon Tetlow# Usage: whatis_parse_args "$@" 967c535eb59SGordon Tetlow# Parse commandline args for whatis and apropos. 968c535eb59SGordon Tetlowwhatis_parse_args() { 969c535eb59SGordon Tetlow local cmd_arg 970f555b39eSKyle Evans OPTIND=1 971c535eb59SGordon Tetlow while getopts 'd' cmd_arg; do 972c535eb59SGordon Tetlow case "${cmd_arg}" in 973c535eb59SGordon Tetlow d) debug=$(( $debug + 1 )) ;; 974c535eb59SGordon Tetlow *) whatis_usage ;; 975c535eb59SGordon Tetlow esac 976c535eb59SGordon Tetlow done >&2 977c535eb59SGordon Tetlow 978c535eb59SGordon Tetlow shift $(( $OPTIND - 1 )) 979c535eb59SGordon Tetlow 980c535eb59SGordon Tetlow keywords="$*" 981c535eb59SGordon Tetlow} 982c535eb59SGordon Tetlow 983c535eb59SGordon Tetlow# Usage: whatis_usage 984c535eb59SGordon Tetlow# Display usage for the whatis/apropos utility. 985c535eb59SGordon Tetlowwhatis_usage() { 986c535eb59SGordon Tetlow echo "usage: $cmd [-d] keyword [...]" 987c535eb59SGordon Tetlow exit 1 988c535eb59SGordon Tetlow} 989c535eb59SGordon Tetlow 990c535eb59SGordon Tetlow 991c535eb59SGordon Tetlow 992c535eb59SGordon Tetlow# Supported commands 993c535eb59SGordon Tetlowdo_apropos() { 99424ef7420SBaptiste Daroussin [ $(stat -f %i /usr/bin/man) -ne $(stat -f %i /usr/bin/apropos) ] && \ 995772246efSBaptiste Daroussin exec apropos "$@" 996c535eb59SGordon Tetlow search_whatis apropos "$@" 997c535eb59SGordon Tetlow} 998c535eb59SGordon Tetlow 9991594084fSFernando Apesteguía# Usage: do_full_search reg_exp 10001594084fSFernando Apesteguía# Do a full search of the regular expression passed 10011594084fSFernando Apesteguía# as parameter in all man pages 10021594084fSFernando Apesteguíado_full_search() { 10031594084fSFernando Apesteguía local gflags re 10041594084fSFernando Apesteguía re=${1} 10051594084fSFernando Apesteguía 10061594084fSFernando Apesteguía # Build grep(1) flags 10071594084fSFernando Apesteguía gflags="-H" 10081594084fSFernando Apesteguía 10091594084fSFernando Apesteguía # wflag implies -l for grep(1) 10101594084fSFernando Apesteguía if [ -n "$wflag" ]; then 10111594084fSFernando Apesteguía gflags="${gflags} -l" 10121594084fSFernando Apesteguía fi 10131594084fSFernando Apesteguía 10141594084fSFernando Apesteguía gflags="${gflags} --label" 10151594084fSFernando Apesteguía 10161594084fSFernando Apesteguía set +f 10178a5c836bSEd Maste for mpath in $(echo "${MANPATH}" | tr : '[:blank:]'); do 10188a5c836bSEd Maste for section in $(echo "${MANSECT}" | tr : '[:blank:]'); do 10191594084fSFernando Apesteguía for manfile in ${mpath}/man${section}/*.${section}*; do 10201594084fSFernando Apesteguía mandoc "${manfile}" 2>/dev/null | 10218a5c836bSEd Maste grep -E ${gflags} "${manfile}" -e "${re}" 10221594084fSFernando Apesteguía done 10231594084fSFernando Apesteguía done 10241594084fSFernando Apesteguía done 10251594084fSFernando Apesteguía set -f 10261594084fSFernando Apesteguía} 10271594084fSFernando Apesteguía 1028c535eb59SGordon Tetlowdo_man() { 10298edb6fb5SMohamed Akram local IFS 10308edb6fb5SMohamed Akram 10318edb6fb5SMohamed Akram man_parse_opts "$@" 10328edb6fb5SMohamed Akram man_setup 10338edb6fb5SMohamed Akram 10348edb6fb5SMohamed Akram shift $(( $OPTIND - 1 )) 10358edb6fb5SMohamed Akram IFS=: 10368edb6fb5SMohamed Akram for sect in $MANSECT; do 10378edb6fb5SMohamed Akram if [ "$sect" = "$1" ]; then 10388edb6fb5SMohamed Akram decho "Detected manual section as first arg: $1" 10398edb6fb5SMohamed Akram MANSECT="$1" 10408edb6fb5SMohamed Akram shift 10418edb6fb5SMohamed Akram break 10428edb6fb5SMohamed Akram fi 10438edb6fb5SMohamed Akram done 10448edb6fb5SMohamed Akram unset IFS 10458edb6fb5SMohamed Akram pages="$*" 10468edb6fb5SMohamed Akram 10471594084fSFernando Apesteguía if [ -z "$pages" -a -z "${Kflag}" ]; then 1048c535eb59SGordon Tetlow echo 'What manual page do you want?' >&2 1049c535eb59SGordon Tetlow exit 1 1050c535eb59SGordon Tetlow fi 1051c535eb59SGordon Tetlow 10521594084fSFernando Apesteguía if [ ! -z "${Kflag}" ]; then 10531594084fSFernando Apesteguía # Short circuit because -K flag does a sufficiently 10541594084fSFernando Apesteguía # different thing like not showing the man page at all 10551594084fSFernando Apesteguía do_full_search "${REGEXP}" 10561594084fSFernando Apesteguía fi 10571594084fSFernando Apesteguía 10581e82d882SWolfram Schneider for page in "$@"; do 105978948070SWolfram Schneider decho "Searching for \"$page\"" 1060c535eb59SGordon Tetlow man_find_and_display "$page" 1061c535eb59SGordon Tetlow done 1062c535eb59SGordon Tetlow 1063c535eb59SGordon Tetlow exit ${ret:-0} 1064c535eb59SGordon Tetlow} 1065c535eb59SGordon Tetlow 1066c535eb59SGordon Tetlowdo_manpath() { 1067c535eb59SGordon Tetlow manpath_parse_args "$@" 1068c535eb59SGordon Tetlow if [ -z "$qflag" ]; then 1069c535eb59SGordon Tetlow manpath_warnings 1070c535eb59SGordon Tetlow fi 1071c535eb59SGordon Tetlow if [ -n "$Lflag" ]; then 1072c535eb59SGordon Tetlow build_manlocales 1073c535eb59SGordon Tetlow echo $MANLOCALES 1074c535eb59SGordon Tetlow else 1075c535eb59SGordon Tetlow build_manpath 1076c535eb59SGordon Tetlow echo $MANPATH 1077c535eb59SGordon Tetlow fi 1078c535eb59SGordon Tetlow exit 0 1079c535eb59SGordon Tetlow} 1080c535eb59SGordon Tetlow 1081c535eb59SGordon Tetlowdo_whatis() { 108224ef7420SBaptiste Daroussin [ $(stat -f %i /usr/bin/man) -ne $(stat -f %i /usr/bin/whatis) ] && \ 1083772246efSBaptiste Daroussin exec whatis "$@" 1084c535eb59SGordon Tetlow search_whatis whatis "$@" 1085c535eb59SGordon Tetlow} 1086c535eb59SGordon Tetlow 1087aeea395eSUlrich Spörlein# User's PATH setting decides on the groff-suite to pick up. 1088aeea395eSUlrich SpörleinEQN=eqn 1089035f7c9aSWolfram SchneiderNROFF='groff -S -P-h -Wall -mtty-char -mandoc' 1090aeea395eSUlrich SpörleinPIC=pic 1091aeea395eSUlrich SpörleinREFER=refer 1092aeea395eSUlrich SpörleinTBL=tbl 1093035f7c9aSWolfram SchneiderTROFF='groff -S -mandoc' 1094aeea395eSUlrich SpörleinVGRIND=vgrind 1095aeea395eSUlrich Spörlein 1096deeff310SGordon TetlowLOCALE=/usr/bin/locale 1097a0094449SRuslan ErmilovSTTY=/bin/stty 109882db8a5eSGordon TetlowSYSCTL=/sbin/sysctl 1099c535eb59SGordon Tetlow 1100c535eb59SGordon Tetlowdebug=0 110173577bf0SRyan Moellerman_default_sections='1:8:2:3:3lua:n:4:5:6:7:9:l' 1102971c1c42STijl Coosemansman_default_path='/usr/share/man:/usr/share/openssl/man:/usr/local/share/man:/usr/local/man' 110357cd9717SGordon Tetlowcattool='/usr/bin/zcat -f' 1104c535eb59SGordon Tetlow 1105c535eb59SGordon Tetlowconfig_global='/etc/man.conf' 1106c535eb59SGordon Tetlow 1107c535eb59SGordon Tetlow# This can be overridden via a setting in /etc/man.conf. 1108c535eb59SGordon Tetlowconfig_local='/usr/local/etc/man.d/*.conf' 1109c535eb59SGordon Tetlow 1110c535eb59SGordon Tetlow# Set noglobbing for now. I don't want spurious globbing. 1111c535eb59SGordon Tetlowset -f 1112c535eb59SGordon Tetlow 1113c535eb59SGordon Tetlowcase "$0" in 1114c535eb59SGordon Tetlow*apropos) do_apropos "$@" ;; 1115c535eb59SGordon Tetlow*manpath) do_manpath "$@" ;; 1116c535eb59SGordon Tetlow*whatis) do_whatis "$@" ;; 1117c535eb59SGordon Tetlow*) do_man "$@" ;; 1118c535eb59SGordon Tetlowesac 1119