1#! /bin/sh 2# 3# SPDX-License-Identifier: BSD-2-Clause 4# 5# Copyright (c) 2010 Gordon Tetlow 6# All rights reserved. 7# 8# Redistribution and use in source and binary forms, with or without 9# modification, are permitted provided that the following conditions 10# are met: 11# 1. Redistributions of source code must retain the above copyright 12# notice, this list of conditions and the following disclaimer. 13# 2. Redistributions in binary form must reproduce the above copyright 14# notice, this list of conditions and the following disclaimer in the 15# documentation and/or other materials provided with the distribution. 16# 17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27# SUCH DAMAGE. 28# 29 30# Rendering a manual page is fast. Even a manual page several 100k in size 31# takes less than a CPU second. If it takes much longer, it is very likely 32# that a tool like mandoc(1) is running in an infinite loop. In this case 33# it is better to terminate it. 34ulimit -t 20 35 36# Do not ignore the exit codes of roff tools, as they may indicate a 37# problem with the page being rendered. Note that this also causes a 38# nonzero exit when the user quits reading before reaching the end, so 39# we need to look out for and deal with that specific case. 40set -o pipefail 41 42# Usage: add_to_manpath path 43# Adds a variable to manpath while ensuring we don't have duplicates. 44# Returns true if we were able to add something. False otherwise. 45add_to_manpath() { 46 case "$manpath" in 47 *:$1) decho " Skipping duplicate manpath entry $1" 2 ;; 48 $1:*) decho " Skipping duplicate manpath entry $1" 2 ;; 49 *:$1:*) decho " Skipping duplicate manpath entry $1" 2 ;; 50 *) if [ -d "$1" ]; then 51 decho " Adding $1 to manpath" 52 manpath="$manpath:$1" 53 return 0 54 fi 55 ;; 56 esac 57 58 return 1 59} 60 61# Usage: build_manlocales 62# Builds a correct MANLOCALES variable. 63build_manlocales() { 64 # If the user has set manlocales, who are we to argue. 65 if [ -n "$MANLOCALES" ]; then 66 return 67 fi 68 69 parse_configs 70 71 # Trim leading colon 72 MANLOCALES=${manlocales#:} 73 74 decho "Available manual locales: $MANLOCALES" 75} 76 77# Usage: build_mansect 78# Builds a correct MANSECT variable. 79build_mansect() { 80 # If the user has set mansect, who are we to argue. 81 if [ -n "$MANSECT" ]; then 82 return 83 fi 84 85 parse_configs 86 87 # Trim leading colon 88 MANSECT=${mansect#:} 89 90 if [ -z "$MANSECT" ]; then 91 MANSECT=$man_default_sections 92 fi 93 decho "Using manual sections: $MANSECT" 94} 95 96# Usage: build_manpath 97# Builds a correct MANPATH variable. 98build_manpath() { 99 local IFS 100 101 # If the user has set a manpath, who are we to argue. 102 if [ -n "$MANPATH" ]; then 103 case "$MANPATH" in 104 *:) PREPEND_MANPATH=${MANPATH} ;; 105 :*) APPEND_MANPATH=${MANPATH} ;; 106 *::*) 107 PREPEND_MANPATH=${MANPATH%%::*} 108 APPEND_MANPATH=${MANPATH#*::} 109 ;; 110 *) return ;; 111 esac 112 fi 113 114 if [ -n "$PREPEND_MANPATH" ]; then 115 IFS=: 116 for path in $PREPEND_MANPATH; do 117 add_to_manpath "$path" 118 done 119 unset IFS 120 fi 121 122 search_path 123 124 decho "Adding default manpath entries" 125 IFS=: 126 for path in $man_default_path; do 127 add_to_manpath "$path" 128 done 129 unset IFS 130 131 parse_configs 132 133 if [ -n "$APPEND_MANPATH" ]; then 134 IFS=: 135 for path in $APPEND_MANPATH; do 136 add_to_manpath "$path" 137 done 138 unset IFS 139 fi 140 # Trim leading colon 141 MANPATH=${manpath#:} 142 143 decho "Using manual path: $MANPATH" 144} 145 146# Usage: check_cat catglob 147# Checks to see if a cat glob is available. 148check_cat() { 149 if exists "$1"; then 150 use_cat=yes 151 catpage=$found 152 setup_cattool "$catpage" 153 decho " Found catpage \"$catpage\"" 154 return 0 155 else 156 return 1 157 fi 158} 159 160# Usage: check_man manglob catglob 161# Given 2 globs, figures out if the manglob is available, if so, check to 162# see if the catglob is also available and up to date. 163check_man() { 164 if exists "$1"; then 165 # We have a match, check for a cat page 166 manpage=$found 167 setup_cattool "$manpage" 168 decho " Found manpage \"$manpage\"" 169 170 if [ -n "${use_width}" ]; then 171 # non-standard width 172 unset use_cat 173 decho " Skipping catpage: non-standard page width" 174 elif exists "$2" && is_newer $found "$manpage"; then 175 # cat page found and is newer, use that 176 use_cat=yes 177 catpage=$found 178 setup_cattool "$catpage" 179 decho " Using catpage \"$catpage\"" 180 else 181 # no cat page or is older 182 unset use_cat 183 decho " Skipping catpage: not found or old" 184 fi 185 return 0 186 fi 187 188 return 1 189} 190 191# Usage: decho "string" [debuglevel] 192# Echoes to stderr string prefaced with -- if high enough debuglevel. 193decho() { 194 if [ $debug -ge ${2:-1} ]; then 195 echo "-- $1" >&2 196 fi 197} 198 199# Usage: exists glob 200# 201# Returns true if glob resolves to a real file and store the first 202# found filename in the variable $found 203exists() { 204 if [ -z "$1" ]; then 205 return 1 206 fi 207 208 local IFS 209 210 # Don't accidentally inherit callers IFS (breaks perl manpages) 211 unset IFS 212 213 # Use some globbing tricks in the shell to determine if a file 214 # exists or not. 215 set +f 216 for file in "$1"* 217 do 218 if [ -r "$file" ]; then 219 found="$file" 220 set -f 221 return 0 222 fi 223 done 224 set -f 225 226 return 1 227} 228 229# Usage: find_file path section subdir pagename 230# Returns: true if something is matched and found. 231# Search the given path/section combo for a given page. 232find_file() { 233 local manroot catroot mann man0 catn cat0 234 235 manroot="$1/man$2" 236 catroot="$1/cat$2" 237 if [ -n "$3" ]; then 238 manroot="$manroot/$3" 239 catroot="$catroot/$3" 240 fi 241 242 if [ ! -d "$manroot" -a ! -d "$catroot" ]; then 243 return 1 244 fi 245 decho " Searching directory $manroot" 2 246 247 mann="$manroot/$4.$2" 248 man0="$manroot/$4.0" 249 catn="$catroot/$4.$2" 250 cat0="$catroot/$4.0" 251 252 # This is the behavior as seen by the original man utility. 253 # Let's not change that which doesn't seem broken. 254 if check_man "$mann" "$catn"; then 255 return 0 256 elif check_man "$man0" "$cat0"; then 257 return 0 258 elif check_cat "$catn"; then 259 return 0 260 elif check_cat "$cat0"; then 261 return 0 262 fi 263 264 return 1 265} 266 267# Usage: is_newer file1 file2 268# Returns true if file1 is newer than file2 as calculated by mtime. 269is_newer() { 270 if ! [ "$1" -ot "$2" ]; then 271 decho " mtime: $1 not older than $2" 3 272 return 0 273 else 274 decho " mtime: $1 older than $2" 3 275 return 1 276 fi 277} 278 279# Usage: manpath_parse_args "$@" 280# Parses commandline options for manpath. 281manpath_parse_args() { 282 local cmd_arg 283 284 OPTIND=1 285 while getopts 'Ldq' cmd_arg; do 286 case "${cmd_arg}" in 287 L) Lflag=Lflag ;; 288 d) debug=$(( $debug + 1 )) ;; 289 q) qflag=qflag ;; 290 *) manpath_usage ;; 291 esac 292 done >&2 293} 294 295# Usage: manpath_usage 296# Display usage for the manpath(1) utility. 297manpath_usage() { 298 echo 'usage: manpath [-Ldq]' >&2 299 exit 1 300} 301 302# Usage: manpath_warnings 303# Display some warnings to stderr. 304manpath_warnings() { 305 if [ -n "$Lflag" -a -n "$MANLOCALES" ]; then 306 echo "(Warning: MANLOCALES environment variable set)" >&2 307 fi 308} 309 310# Usage: man_check_for_so path 311# Returns: True if able to resolve the file, false if it ended in tears. 312# Detects the presence of the .so directive and causes the file to be 313# redirected to another source file. 314man_check_for_so() { 315 local IFS line tstr 316 317 unset IFS 318 if [ -n "$catpage" ]; then 319 return 0 320 fi 321 322 # We need to loop to accommodate multiple .so directives. 323 while true 324 do 325 line=$($cattool "$manpage" 2>/dev/null | grep -E -m1 -v '^\.\\"[ ]*|^[ ]*$') 326 case "$line" in 327 '.so /'*) break ;; # ignore absolute path 328 '.so '*) trim "${line#.so}" 329 decho "$manpage includes $tstr" 330 # Glob and check for the file. 331 if ! check_man "$1/$tstr" ""; then 332 decho " Unable to find $tstr" 333 return 1 334 fi 335 ;; 336 *) break ;; 337 esac 338 done 339 340 return 0 341} 342 343# Usage: man_display_page 344# Display either the manpage or catpage depending on the use_cat variable 345man_display_page() { 346 local IFS pipeline testline 347 348 # We are called with IFS set to colon. This causes really weird 349 # things to happen for the variables that have spaces in them. 350 unset IFS 351 352 # If we are supposed to use a catpage and we aren't using troff(1) 353 # just zcat the catpage and we are done. 354 if [ -z "$tflag" -a -n "$use_cat" ]; then 355 if [ -n "$wflag" ]; then 356 echo "$catpage (source: \"$manpage\")" 357 ret=0 358 else 359 if [ $debug -gt 0 ]; then 360 decho "Command: $cattool \"$catpage\" | $MANPAGER" 361 ret=0 362 else 363 $cattool "$catpage" | $MANPAGER 364 ret=$? 365 fi 366 fi 367 return 368 fi 369 370 # Okay, we are using the manpage, do we just need to output the 371 # name of the manpage? 372 if [ -n "$wflag" ]; then 373 echo "$manpage" 374 ret=0 375 return 376 fi 377 378 if [ -n "$use_width" ]; then 379 mandoc_args="-O width=${use_width}" 380 fi 381 testline="mandoc -Tlint -Wunsupp >/dev/null 2>&1" 382 if [ -n "$tflag" ]; then 383 pipeline="mandoc -Tps $mandoc_args" 384 else 385 pipeline="mandoc $mandoc_args | $MANPAGER" 386 fi 387 388 if ! $cattool "$manpage" | eval "$testline"; then 389 if which -s groff; then 390 man_display_page_groff 391 else 392 echo "This manpage needs groff(1) to be rendered" >&2 393 echo "First install groff(1): " >&2 394 echo "pkg install groff " >&2 395 ret=1 396 fi 397 return 398 fi 399 400 if [ $debug -gt 0 ]; then 401 decho "Command: $cattool \"$manpage\" | eval \"$pipeline\"" 402 ret=0 403 else 404 $cattool "$manpage" | eval "$pipeline" 405 ret=$? 406 fi 407} 408 409# Usage: man_display_page_groff 410# Display the manpage using groff 411man_display_page_groff() { 412 local EQN NROFF PIC TBL TROFF REFER VGRIND 413 local IFS l nroff_dev pipeline preproc_arg tool 414 415 # So, we really do need to parse the manpage. First, figure out the 416 # device flag (-T) we have to pass to eqn(1) and groff(1). Then, 417 # setup the pipeline of commands based on the user's request. 418 419 # If the manpage is from a particular charset, we need to setup nroff 420 # to properly output for the correct device. 421 case "${manpage}" in 422 *.${man_charset}/*) 423 # I don't pretend to know this; I'm just copying from the 424 # previous version of man(1). 425 case "$man_charset" in 426 KOI8-R) nroff_dev="koi8-r" ;; 427 ISO8859-1) nroff_dev="latin1" ;; 428 ISO8859-15) nroff_dev="latin1" ;; 429 UTF-8) nroff_dev="utf8" ;; 430 *) nroff_dev="ascii" ;; 431 esac 432 433 NROFF="$NROFF -T$nroff_dev" 434 EQN="$EQN -T$nroff_dev" 435 436 # Iff the manpage is from the locale and not just the charset, 437 # then we need to define the locale string. 438 case "${manpage}" in 439 */${man_lang}_${man_country}.${man_charset}/*) 440 NROFF="$NROFF -dlocale=$man_lang.$man_charset" 441 ;; 442 */${man_lang}.${man_charset}/*) 443 NROFF="$NROFF -dlocale=$man_lang.$man_charset" 444 ;; 445 esac 446 447 # Allow language specific calls to override the default 448 # set of utilities. 449 l=$(echo $man_lang | tr [:lower:] [:upper:]) 450 for tool in EQN NROFF PIC TBL TROFF REFER VGRIND; do 451 eval "$tool=\${${tool}_$l:-\$$tool}" 452 done 453 ;; 454 *) NROFF="$NROFF -Tascii" 455 EQN="$EQN -Tascii" 456 ;; 457 esac 458 459 if [ -z "$MANCOLOR" ]; then 460 NROFF="$NROFF -P-c" 461 fi 462 463 if [ -n "${use_width}" ]; then 464 NROFF="$NROFF -rLL=${use_width}n -rLT=${use_width}n" 465 fi 466 467 if [ -n "$MANROFFSEQ" ]; then 468 set -- -$MANROFFSEQ 469 OPTIND=1 470 while getopts 'egprtv' preproc_arg; do 471 case "${preproc_arg}" in 472 e) pipeline="$pipeline | $EQN" ;; 473 g) ;; # Ignore for compatibility. 474 p) pipeline="$pipeline | $PIC" ;; 475 r) pipeline="$pipeline | $REFER" ;; 476 t) pipeline="$pipeline | $TBL" ;; 477 v) pipeline="$pipeline | $VGRIND" ;; 478 *) usage ;; 479 esac 480 done 481 # Strip the leading " | " from the resulting pipeline. 482 pipeline="${pipeline#" | "}" 483 else 484 pipeline="$TBL" 485 fi 486 487 if [ -n "$tflag" ]; then 488 pipeline="$pipeline | $TROFF" 489 else 490 pipeline="$pipeline | $NROFF | $MANPAGER" 491 fi 492 493 if [ $debug -gt 0 ]; then 494 decho "Command: $cattool \"$manpage\" | eval \"$pipeline\"" 495 ret=0 496 else 497 $cattool "$manpage" | eval "$pipeline" 498 ret=$? 499 fi 500} 501 502# Usage: man_find_and_display page 503# Search through the manpaths looking for the given page. 504man_find_and_display() { 505 local found_page locpath p path sect 506 507 # Check to see if it's a file. But only if it has a '/' in 508 # the filename. 509 case "$1" in 510 */*) if [ -f "$1" -a -r "$1" ]; then 511 decho "Found a usable page, displaying that" 512 unset use_cat 513 manpage="$1" 514 setup_cattool "$manpage" 515 p=$(cd "$(dirname "$manpage")" && pwd) 516 case "$(basename "$p")" in 517 man*|cat*) p=$p/.. ;; 518 *) p=$p/../.. ;; 519 esac 520 if man_check_for_so "$p"; then 521 found_page=yes 522 man_display_page 523 fi 524 return 525 fi 526 ;; 527 esac 528 529 IFS=: 530 for sect in $MANSECT; do 531 decho "Searching section $sect" 2 532 for path in $MANPATH; do 533 for locpath in $locpaths; do 534 p=$path/$locpath 535 p=${p%/.} # Rid ourselves of the trailing /. 536 537 # Check if there is a MACHINE specific manpath. 538 if find_file $p $sect $MACHINE "$1"; then 539 if man_check_for_so $p; then 540 found_page=yes 541 man_display_page 542 if [ -n "$aflag" ]; then 543 continue 2 544 else 545 return 546 fi 547 fi 548 fi 549 550 # Check if there is a MACHINE_ARCH 551 # specific manpath. 552 if find_file $p $sect $MACHINE_ARCH "$1"; then 553 if man_check_for_so $p; then 554 found_page=yes 555 man_display_page 556 if [ -n "$aflag" ]; then 557 continue 2 558 else 559 return 560 fi 561 fi 562 fi 563 564 # Check plain old manpath. 565 if find_file $p $sect '' "$1"; then 566 if man_check_for_so $p; then 567 found_page=yes 568 man_display_page 569 if [ -n "$aflag" ]; then 570 continue 2 571 else 572 return 573 fi 574 fi 575 fi 576 done 577 done 578 done 579 unset IFS 580 581 # Nothing? Well, we are done then. 582 if [ -z "$found_page" ]; then 583 echo "No manual entry for \"$1\"" >&2 584 ret=1 585 return 586 fi 587} 588 589# Usage: man_parse_opts "$@" 590# Parses commandline options for man. 591man_parse_opts() { 592 local cmd_arg 593 594 OPTIND=1 595 while getopts 'K:M:P:S:adfhkm:op:tw' cmd_arg; do 596 case "${cmd_arg}" in 597 K) Kflag=Kflag 598 REGEXP=$OPTARG ;; 599 M) MANPATH=$OPTARG ;; 600 P) MANPAGER=$OPTARG ;; 601 S) MANSECT=$OPTARG ;; 602 a) aflag=aflag ;; 603 d) debug=$(( $debug + 1 )) ;; 604 f) fflag=fflag ;; 605 h) man_usage 0 ;; 606 k) kflag=kflag ;; 607 m) mflag=$OPTARG ;; 608 o) oflag=oflag ;; 609 p) MANROFFSEQ=$OPTARG ;; 610 t) tflag=tflag ;; 611 w) wflag=wflag ;; 612 *) man_usage ;; 613 esac 614 done >&2 615 616 shift $(( $OPTIND - 1 )) 617 618 # Check the args for incompatible options. 619 620 case "${Kflag}${fflag}${kflag}${tflag}${wflag}" in 621 Kflagfflag*) echo "Incompatible options: -K and -f"; man_usage ;; 622 Kflag*kflag*) echo "Incompatible options: -K and -k"; man_usage ;; 623 Kflag*tflag) echo "Incompatible options: -K and -t"; man_usage ;; 624 fflagkflag*) echo "Incompatible options: -f and -k"; man_usage ;; 625 fflag*tflag*) echo "Incompatible options: -f and -t"; man_usage ;; 626 fflag*wflag) echo "Incompatible options: -f and -w"; man_usage ;; 627 *kflagtflag*) echo "Incompatible options: -k and -t"; man_usage ;; 628 *kflag*wflag) echo "Incompatible options: -k and -w"; man_usage ;; 629 *tflagwflag) echo "Incompatible options: -t and -w"; man_usage ;; 630 esac 631 632 # Short circuit for whatis(1) and apropos(1) 633 if [ -n "$fflag" ]; then 634 do_whatis "$@" 635 exit 636 fi 637 638 if [ -n "$kflag" ]; then 639 do_apropos "$@" 640 exit 641 fi 642} 643 644# Usage: man_setup 645# Setup various trivial but essential variables. 646man_setup() { 647 # Setup machine and architecture variables. 648 if [ -n "$mflag" ]; then 649 MACHINE_ARCH=${mflag%%:*} 650 MACHINE=${mflag##*:} 651 fi 652 if [ -z "$MACHINE_ARCH" ]; then 653 MACHINE_ARCH=$($SYSCTL -n hw.machine_arch) 654 fi 655 if [ -z "$MACHINE" ]; then 656 MACHINE=$($SYSCTL -n hw.machine) 657 fi 658 decho "Using architecture: $MACHINE_ARCH:$MACHINE" 659 660 setup_pager 661 build_manpath 662 build_mansect 663 man_setup_locale 664 man_setup_width 665} 666 667# Usage: man_setup_width 668# Set up page width. 669man_setup_width() { 670 local sizes 671 672 unset use_width 673 case "$MANWIDTH" in 674 [0-9]*) 675 if [ "$MANWIDTH" -gt 0 2>/dev/null ]; then 676 use_width=$MANWIDTH 677 fi 678 ;; 679 [Tt][Tt][Yy]) 680 if { sizes=$($STTY size 0>&3 2>/dev/null); } 3>&1; then 681 set -- $sizes 682 if [ $2 -gt 80 ]; then 683 use_width=$(($2-2)) 684 fi 685 fi 686 ;; 687 esac 688 if [ -n "$use_width" ]; then 689 decho "Using non-standard page width: ${use_width}" 690 else 691 decho 'Using standard page width' 692 fi 693} 694 695# Usage: man_setup_locale 696# Setup necessary locale variables. 697man_setup_locale() { 698 local lang_cc 699 local locstr 700 701 locpaths='.' 702 man_charset='US-ASCII' 703 704 # Setup locale information. 705 if [ -n "$oflag" ]; then 706 decho 'Using non-localized manpages' 707 else 708 # Use the locale tool to give us proper locale information 709 eval $( $LOCALE ) 710 711 if [ -n "$LANG" ]; then 712 locstr=$LANG 713 else 714 locstr=$LC_CTYPE 715 fi 716 717 case "$locstr" in 718 C) ;; 719 C.UTF-8) ;; 720 POSIX) ;; 721 [a-z][a-z]_[A-Z][A-Z]\.*) 722 lang_cc="${locstr%.*}" 723 man_lang="${locstr%_*}" 724 man_country="${lang_cc#*_}" 725 man_charset="${locstr#*.}" 726 locpaths="$locstr" 727 locpaths="$locpaths:$man_lang.$man_charset" 728 if [ "$man_lang" != "en" ]; then 729 locpaths="$locpaths:en.$man_charset" 730 fi 731 locpaths="$locpaths:." 732 ;; 733 *) echo 'Unknown locale, assuming C' >&2 734 ;; 735 esac 736 fi 737 738 decho "Using locale paths: $locpaths" 739} 740 741# Usage: man_usage [exitcode] 742# Display usage for the man utility. 743man_usage() { 744 echo 'Usage:' 745 echo ' man [-adho] [-t | -w] [-M manpath] [-P pager] [-S mansect]' 746 echo ' [-m arch[:machine]] [-p [eprtv]] [mansect] page [...]' 747 echo ' man -K | -f | -k expression [...] -- Search manual pages' 748 749 # When exit'ing with -h, it's not an error. 750 exit ${1:-1} 751} 752 753# Usage: parse_configs 754# Reads the end-user adjustable config files. 755parse_configs() { 756 local IFS file files 757 758 if [ -n "$parsed_configs" ]; then 759 return 760 fi 761 762 unset IFS 763 764 # Read the global config first in case the user wants 765 # to override config_local. 766 if [ -r "$config_global" ]; then 767 parse_file "$config_global" 768 fi 769 770 # Glob the list of files to parse. 771 set +f 772 files=$(echo $config_local) 773 set -f 774 775 for file in $files; do 776 if [ -r "$file" ]; then 777 parse_file "$file" 778 fi 779 done 780 781 parsed_configs='yes' 782} 783 784# Usage: parse_file file 785# Reads the specified config files. 786parse_file() { 787 local file line tstr var 788 789 file="$1" 790 decho "Parsing config file: $file" 791 while read line; do 792 decho " $line" 2 793 case "$line" in 794 \#*) decho " Comment" 3 795 ;; 796 MANPATH*) decho " MANPATH" 3 797 trim "${line#MANPATH}" 798 add_to_manpath "$tstr" 799 ;; 800 MANLOCALE*) decho " MANLOCALE" 3 801 trim "${line#MANLOCALE}" 802 manlocales="$manlocales:$tstr" 803 ;; 804 MANCONFIG*) decho " MANCONFIG" 3 805 trim "${line#MANCONFIG}" 806 config_local="$tstr" 807 ;; 808 MANSECT*) decho " MANSECT" 3 809 trim "${line#MANSECT}" 810 mansect="$mansect:$tstr" 811 ;; 812 # Set variables in the form of FOO_BAR 813 *_*[\ \ ]*) var="${line%%[\ \ ]*}" 814 trim "${line#$var}" 815 eval "$var=\"$tstr\"" 816 decho " Parsed $var" 3 817 ;; 818 esac 819 done < "$file" 820} 821 822# Usage: search_path 823# Traverse $PATH looking for manpaths. 824search_path() { 825 local IFS p path 826 827 decho "Searching PATH for man directories" 828 829 IFS=: 830 for path in $PATH; do 831 if add_to_manpath "$path/man"; then 832 : 833 elif add_to_manpath "$path/MAN"; then 834 : 835 else 836 case "$path" in 837 */bin) p="${path%/bin}/share/man" 838 add_to_manpath "$p" 839 p="${path%/bin}/man" 840 add_to_manpath "$p" 841 ;; 842 esac 843 fi 844 done 845 unset IFS 846 847 if [ -z "$manpath" ]; then 848 decho ' Unable to find any manpaths, using default' 849 manpath=$man_default_path 850 fi 851} 852 853# Usage: search_whatis cmd [arglist] 854# Do the heavy lifting for apropos/whatis 855search_whatis() { 856 local IFS bad cmd f good key keywords loc opt out path rval wlist 857 858 cmd="$1" 859 shift 860 861 whatis_parse_args "$@" 862 863 build_manpath 864 build_manlocales 865 setup_pager 866 867 if [ "$cmd" = "whatis" ]; then 868 opt="-w" 869 fi 870 871 f='whatis' 872 873 IFS=: 874 for path in $MANPATH; do 875 if [ \! -d "$path" ]; then 876 decho "Skipping non-existent path: $path" 2 877 continue 878 fi 879 880 if [ -f "$path/$f" -a -r "$path/$f" ]; then 881 decho "Found whatis: $path/$f" 882 wlist="$wlist $path/$f" 883 fi 884 885 for loc in $MANLOCALES; do 886 if [ -f "$path/$loc/$f" -a -r "$path/$loc/$f" ]; then 887 decho "Found whatis: $path/$loc/$f" 888 wlist="$wlist $path/$loc/$f" 889 fi 890 done 891 done 892 unset IFS 893 894 if [ -z "$wlist" ]; then 895 echo "$cmd: no whatis databases in $MANPATH" >&2 896 exit 1 897 fi 898 899 rval=0 900 for key in $keywords; do 901 out=$(grep -Ehi $opt -- "$key" $wlist) 902 if [ -n "$out" ]; then 903 good="$good\\n$out" 904 else 905 bad="$bad\\n$key: nothing appropriate" 906 rval=1 907 fi 908 done 909 910 # Strip leading carriage return. 911 good=${good#\\n} 912 bad=${bad#\\n} 913 914 if [ -n "$good" ]; then 915 printf '%b\n' "$good" | $MANPAGER 916 fi 917 918 if [ -n "$bad" ]; then 919 printf '%b\n' "$bad" >&2 920 fi 921 922 exit $rval 923} 924 925# Usage: setup_cattool page 926# Finds an appropriate decompressor based on extension 927setup_cattool() { 928 case "$1" in 929 *.bz) cattool='/usr/bin/bzcat' ;; 930 *.bz2) cattool='/usr/bin/bzcat' ;; 931 *.gz) cattool='/usr/bin/gzcat' ;; 932 *.lzma) cattool='/usr/bin/lzcat' ;; 933 *.xz) cattool='/usr/bin/xzcat' ;; 934 *.zst) cattool='/usr/bin/zstdcat' ;; 935 *) cattool='/usr/bin/zcat -f' ;; 936 esac 937} 938 939# Usage: setup_pager 940# Correctly sets $MANPAGER 941setup_pager() { 942 # Setup pager. 943 if [ -z "$MANPAGER" ]; then 944 if [ -n "$MANCOLOR" ]; then 945 MANPAGER="less -sR" 946 else 947 if [ -n "$PAGER" ]; then 948 MANPAGER="$PAGER" 949 else 950 MANPAGER="less -s" 951 fi 952 fi 953 fi 954 decho "Using pager: $MANPAGER" 955} 956 957# Usage: trim string 958# Trims whitespace from beginning and end of a variable 959trim() { 960 tstr=$1 961 while true; do 962 case "$tstr" in 963 [\ \ ]*) tstr="${tstr##[\ \ ]}" ;; 964 *[\ \ ]) tstr="${tstr%%[\ \ ]}" ;; 965 *) break ;; 966 esac 967 done 968} 969 970# Usage: whatis_parse_args "$@" 971# Parse commandline args for whatis and apropos. 972whatis_parse_args() { 973 local cmd_arg 974 OPTIND=1 975 while getopts 'd' cmd_arg; do 976 case "${cmd_arg}" in 977 d) debug=$(( $debug + 1 )) ;; 978 *) whatis_usage ;; 979 esac 980 done >&2 981 982 shift $(( $OPTIND - 1 )) 983 984 keywords="$*" 985} 986 987# Usage: whatis_usage 988# Display usage for the whatis/apropos utility. 989whatis_usage() { 990 echo "usage: $cmd [-d] keyword [...]" 991 exit 1 992} 993 994 995 996# Supported commands 997do_apropos() { 998 [ $(stat -f %i /usr/bin/man) -ne $(stat -f %i /usr/bin/apropos) ] && \ 999 exec apropos "$@" 1000 search_whatis apropos "$@" 1001} 1002 1003# Usage: do_full_search reg_exp 1004# Do a full search of the regular expression passed 1005# as parameter in all man pages 1006do_full_search() { 1007 local gflags re 1008 re=${1} 1009 1010 # Build grep(1) flags 1011 gflags="-H" 1012 1013 # wflag implies -l for grep(1) 1014 if [ -n "$wflag" ]; then 1015 gflags="${gflags} -l" 1016 fi 1017 1018 gflags="${gflags} --label" 1019 1020 set +f 1021 for mpath in $(echo "${MANPATH}" | tr : '[:blank:]'); do 1022 for section in $(echo "${MANSECT}" | tr : '[:blank:]'); do 1023 for manfile in ${mpath}/man${section}/*.${section}*; do 1024 mandoc "${manfile}" 2>/dev/null | 1025 grep -E ${gflags} "${manfile}" -e "${re}" 1026 done 1027 done 1028 done 1029 set -f 1030} 1031 1032do_man() { 1033 local IFS 1034 1035 man_parse_opts "$@" 1036 man_setup 1037 1038 shift $(( $OPTIND - 1 )) 1039 IFS=: 1040 for sect in $MANSECT; do 1041 if [ "$sect" = "$1" ]; then 1042 decho "Detected manual section as first arg: $1" 1043 MANSECT="$1" 1044 shift 1045 break 1046 fi 1047 done 1048 unset IFS 1049 pages="$*" 1050 1051 if [ -z "$pages" -a -z "${Kflag}" ]; then 1052 echo 'What manual page do you want?' >&2 1053 exit 1 1054 fi 1055 1056 if [ ! -z "${Kflag}" ]; then 1057 # Short circuit because -K flag does a sufficiently 1058 # different thing like not showing the man page at all 1059 do_full_search "${REGEXP}" 1060 fi 1061 1062 for page in "$@"; do 1063 decho "Searching for \"$page\"" 1064 man_find_and_display "$page" 1065 done 1066 1067 # The user will very commonly quit reading the page before 1068 # reaching the bottom. Depending on the length of the page 1069 # and the pager's buffer size, this may result in a SIGPIPE. 1070 # This is normal, so convert that exit code to zero. 1071 if [ ${ret:-0} -gt 128 ]; then 1072 if [ "$(kill -l "${ret}")" = "PIPE" ]; then 1073 ret=0 1074 fi 1075 fi 1076 1077 exit ${ret:-0} 1078} 1079 1080do_manpath() { 1081 manpath_parse_args "$@" 1082 if [ -z "$qflag" ]; then 1083 manpath_warnings 1084 fi 1085 if [ -n "$Lflag" ]; then 1086 build_manlocales 1087 echo $MANLOCALES 1088 else 1089 build_manpath 1090 echo $MANPATH 1091 fi 1092 exit 0 1093} 1094 1095do_whatis() { 1096 [ $(stat -f %i /usr/bin/man) -ne $(stat -f %i /usr/bin/whatis) ] && \ 1097 exec whatis "$@" 1098 search_whatis whatis "$@" 1099} 1100 1101# User's PATH setting decides on the groff-suite to pick up. 1102EQN=eqn 1103NROFF='groff -S -P-h -Wall -mtty-char -mandoc' 1104PIC=pic 1105REFER=refer 1106TBL=tbl 1107TROFF='groff -S -mandoc' 1108VGRIND=vgrind 1109 1110LOCALE=/usr/bin/locale 1111STTY=/bin/stty 1112SYSCTL=/sbin/sysctl 1113 1114debug=0 1115man_default_sections='1:8:2:3:3lua:n:4:5:6:7:9:l' 1116man_default_path='/usr/share/man:/usr/share/openssl/man:/usr/local/share/man:/usr/local/man' 1117cattool='/usr/bin/zcat -f' 1118 1119config_global='/etc/man.conf' 1120 1121# This can be overridden via a setting in /etc/man.conf. 1122config_local='/usr/local/etc/man.d/*.conf' 1123 1124# Set noglobbing for now. I don't want spurious globbing. 1125set -f 1126 1127case "$0" in 1128*apropos) do_apropos "$@" ;; 1129*manpath) do_manpath "$@" ;; 1130*whatis) do_whatis "$@" ;; 1131*) do_man "$@" ;; 1132esac 1133