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 status of roff tools 37set -o pipefail 38 39# ignore SIGPIPE exits because pagers may exit before reading all their input. 40trap '' SIGPIPE 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 page 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*) trim "${line#.so}" 328 decho "$manpage includes $tstr" 329 # Glob and check for the file. 330 if ! check_man "$path/$tstr" ""; then 331 decho " Unable to find $tstr" 332 return 1 333 fi 334 ;; 335 *) break ;; 336 esac 337 done 338 339 return 0 340} 341 342# Usage: man_display_page 343# Display either the manpage or catpage depending on the use_cat variable 344man_display_page() { 345 local IFS pipeline testline 346 347 # We are called with IFS set to colon. This causes really weird 348 # things to happen for the variables that have spaces in them. 349 unset IFS 350 351 # If we are supposed to use a catpage and we aren't using troff(1) 352 # just zcat the catpage and we are done. 353 if [ -z "$tflag" -a -n "$use_cat" ]; then 354 if [ -n "$wflag" ]; then 355 echo "$catpage (source: \"$manpage\")" 356 ret=0 357 else 358 if [ $debug -gt 0 ]; then 359 decho "Command: $cattool \"$catpage\" | $MANPAGER" 360 ret=0 361 else 362 $cattool "$catpage" | $MANPAGER 363 ret=$? 364 fi 365 fi 366 return 367 fi 368 369 # Okay, we are using the manpage, do we just need to output the 370 # name of the manpage? 371 if [ -n "$wflag" ]; then 372 echo "$manpage" 373 ret=0 374 return 375 fi 376 377 if [ -n "$use_width" ]; then 378 mandoc_args="-O width=${use_width}" 379 fi 380 testline="mandoc -Tlint -Wunsupp >/dev/null 2>&1" 381 if [ -n "$tflag" ]; then 382 pipeline="mandoc -Tps $mandoc_args" 383 else 384 pipeline="mandoc $mandoc_args | $MANPAGER" 385 fi 386 387 if ! $cattool "$manpage" | eval "$testline"; then 388 if which -s groff; then 389 man_display_page_groff 390 else 391 echo "This manpage needs groff(1) to be rendered" >&2 392 echo "First install groff(1): " >&2 393 echo "pkg install groff " >&2 394 ret=1 395 fi 396 return 397 fi 398 399 if [ $debug -gt 0 ]; then 400 decho "Command: $cattool \"$manpage\" | eval \"$pipeline\"" 401 ret=0 402 else 403 $cattool "$manpage" | eval "$pipeline" 404 ret=$? 405 fi 406} 407 408# Usage: man_display_page_groff 409# Display the manpage using groff 410man_display_page_groff() { 411 local EQN NROFF PIC TBL TROFF REFER VGRIND 412 local IFS l nroff_dev pipeline preproc_arg tool 413 414 # So, we really do need to parse the manpage. First, figure out the 415 # device flag (-T) we have to pass to eqn(1) and groff(1). Then, 416 # setup the pipeline of commands based on the user's request. 417 418 # If the manpage is from a particular charset, we need to setup nroff 419 # to properly output for the correct device. 420 case "${manpage}" in 421 *.${man_charset}/*) 422 # I don't pretend to know this; I'm just copying from the 423 # previous version of man(1). 424 case "$man_charset" in 425 KOI8-R) nroff_dev="koi8-r" ;; 426 ISO8859-1) nroff_dev="latin1" ;; 427 ISO8859-15) nroff_dev="latin1" ;; 428 UTF-8) nroff_dev="utf8" ;; 429 *) nroff_dev="ascii" ;; 430 esac 431 432 NROFF="$NROFF -T$nroff_dev" 433 EQN="$EQN -T$nroff_dev" 434 435 # Iff the manpage is from the locale and not just the charset, 436 # then we need to define the locale string. 437 case "${manpage}" in 438 */${man_lang}_${man_country}.${man_charset}/*) 439 NROFF="$NROFF -dlocale=$man_lang.$man_charset" 440 ;; 441 */${man_lang}.${man_charset}/*) 442 NROFF="$NROFF -dlocale=$man_lang.$man_charset" 443 ;; 444 esac 445 446 # Allow language specific calls to override the default 447 # set of utilities. 448 l=$(echo $man_lang | tr [:lower:] [:upper:]) 449 for tool in EQN NROFF PIC TBL TROFF REFER VGRIND; do 450 eval "$tool=\${${tool}_$l:-\$$tool}" 451 done 452 ;; 453 *) NROFF="$NROFF -Tascii" 454 EQN="$EQN -Tascii" 455 ;; 456 esac 457 458 if [ -z "$MANCOLOR" ]; then 459 NROFF="$NROFF -P-c" 460 fi 461 462 if [ -n "${use_width}" ]; then 463 NROFF="$NROFF -rLL=${use_width}n -rLT=${use_width}n" 464 fi 465 466 if [ -n "$MANROFFSEQ" ]; then 467 set -- -$MANROFFSEQ 468 OPTIND=1 469 while getopts 'egprtv' preproc_arg; do 470 case "${preproc_arg}" in 471 e) pipeline="$pipeline | $EQN" ;; 472 g) ;; # Ignore for compatibility. 473 p) pipeline="$pipeline | $PIC" ;; 474 r) pipeline="$pipeline | $REFER" ;; 475 t) pipeline="$pipeline | $TBL" ;; 476 v) pipeline="$pipeline | $VGRIND" ;; 477 *) usage ;; 478 esac 479 done 480 # Strip the leading " | " from the resulting pipeline. 481 pipeline="${pipeline#" | "}" 482 else 483 pipeline="$TBL" 484 fi 485 486 if [ -n "$tflag" ]; then 487 pipeline="$pipeline | $TROFF" 488 else 489 pipeline="$pipeline | $NROFF | $MANPAGER" 490 fi 491 492 if [ $debug -gt 0 ]; then 493 decho "Command: $cattool \"$manpage\" | eval \"$pipeline\"" 494 ret=0 495 else 496 $cattool "$manpage" | eval "$pipeline" 497 ret=$? 498 fi 499} 500 501# Usage: man_find_and_display page 502# Search through the manpaths looking for the given page. 503man_find_and_display() { 504 local found_page locpath p path sect 505 506 # Check to see if it's a file. But only if it has a '/' in 507 # the filename. 508 case "$1" in 509 */*) if [ -f "$1" -a -r "$1" ]; then 510 decho "Found a usable page, displaying that" 511 unset use_cat 512 manpage="$1" 513 setup_cattool "$manpage" 514 if man_check_for_so "$manpage" "$(dirname \"$manpage"")"; then 515 found_page=yes 516 man_display_page 517 fi 518 return 519 fi 520 ;; 521 esac 522 523 IFS=: 524 for sect in $MANSECT; do 525 decho "Searching section $sect" 2 526 for path in $MANPATH; do 527 for locpath in $locpaths; do 528 p=$path/$locpath 529 p=${p%/.} # Rid ourselves of the trailing /. 530 531 # Check if there is a MACHINE specific manpath. 532 if find_file $p $sect $MACHINE "$1"; then 533 if man_check_for_so "$manpage" $p; then 534 found_page=yes 535 man_display_page 536 if [ -n "$aflag" ]; then 537 continue 2 538 else 539 return 540 fi 541 fi 542 fi 543 544 # Check if there is a MACHINE_ARCH 545 # specific manpath. 546 if find_file $p $sect $MACHINE_ARCH "$1"; then 547 if man_check_for_so "$manpage" $p; then 548 found_page=yes 549 man_display_page 550 if [ -n "$aflag" ]; then 551 continue 2 552 else 553 return 554 fi 555 fi 556 fi 557 558 # Check plain old manpath. 559 if find_file $p $sect '' "$1"; then 560 if man_check_for_so "$manpage" $p; then 561 found_page=yes 562 man_display_page 563 if [ -n "$aflag" ]; then 564 continue 2 565 else 566 return 567 fi 568 fi 569 fi 570 done 571 done 572 done 573 unset IFS 574 575 # Nothing? Well, we are done then. 576 if [ -z "$found_page" ]; then 577 echo "No manual entry for \"$1\"" >&2 578 ret=1 579 return 580 fi 581} 582 583# Usage: man_parse_opts "$@" 584# Parses commandline options for man. 585man_parse_opts() { 586 local cmd_arg 587 588 OPTIND=1 589 while getopts 'K:M:P:S:adfhkm:op:tw' cmd_arg; do 590 case "${cmd_arg}" in 591 K) Kflag=Kflag 592 REGEXP=$OPTARG ;; 593 M) MANPATH=$OPTARG ;; 594 P) MANPAGER=$OPTARG ;; 595 S) MANSECT=$OPTARG ;; 596 a) aflag=aflag ;; 597 d) debug=$(( $debug + 1 )) ;; 598 f) fflag=fflag ;; 599 h) man_usage 0 ;; 600 k) kflag=kflag ;; 601 m) mflag=$OPTARG ;; 602 o) oflag=oflag ;; 603 p) MANROFFSEQ=$OPTARG ;; 604 t) tflag=tflag ;; 605 w) wflag=wflag ;; 606 *) man_usage ;; 607 esac 608 done >&2 609 610 shift $(( $OPTIND - 1 )) 611 612 # Check the args for incompatible options. 613 614 case "${Kflag}${fflag}${kflag}${tflag}${wflag}" in 615 Kflagfflag*) echo "Incompatible options: -K and -f"; man_usage ;; 616 Kflag*kflag*) echo "Incompatible options: -K and -k"; man_usage ;; 617 Kflag*tflag) echo "Incompatible options: -K and -t"; man_usage ;; 618 fflagkflag*) echo "Incompatible options: -f and -k"; man_usage ;; 619 fflag*tflag*) echo "Incompatible options: -f and -t"; man_usage ;; 620 fflag*wflag) echo "Incompatible options: -f and -w"; man_usage ;; 621 *kflagtflag*) echo "Incompatible options: -k and -t"; man_usage ;; 622 *kflag*wflag) echo "Incompatible options: -k and -w"; man_usage ;; 623 *tflagwflag) echo "Incompatible options: -t and -w"; man_usage ;; 624 esac 625 626 # Short circuit for whatis(1) and apropos(1) 627 if [ -n "$fflag" ]; then 628 do_whatis "$@" 629 exit 630 fi 631 632 if [ -n "$kflag" ]; then 633 do_apropos "$@" 634 exit 635 fi 636} 637 638# Usage: man_setup 639# Setup various trivial but essential variables. 640man_setup() { 641 # Setup machine and architecture variables. 642 if [ -n "$mflag" ]; then 643 MACHINE_ARCH=${mflag%%:*} 644 MACHINE=${mflag##*:} 645 fi 646 if [ -z "$MACHINE_ARCH" ]; then 647 MACHINE_ARCH=$($SYSCTL -n hw.machine_arch) 648 fi 649 if [ -z "$MACHINE" ]; then 650 MACHINE=$($SYSCTL -n hw.machine) 651 fi 652 decho "Using architecture: $MACHINE_ARCH:$MACHINE" 653 654 setup_pager 655 build_manpath 656 build_mansect 657 man_setup_locale 658 man_setup_width 659} 660 661# Usage: man_setup_width 662# Set up page width. 663man_setup_width() { 664 local sizes 665 666 unset use_width 667 case "$MANWIDTH" in 668 [0-9]*) 669 if [ "$MANWIDTH" -gt 0 2>/dev/null ]; then 670 use_width=$MANWIDTH 671 fi 672 ;; 673 [Tt][Tt][Yy]) 674 if { sizes=$($STTY size 0>&3 2>/dev/null); } 3>&1; then 675 set -- $sizes 676 if [ $2 -gt 80 ]; then 677 use_width=$(($2-2)) 678 fi 679 fi 680 ;; 681 esac 682 if [ -n "$use_width" ]; then 683 decho "Using non-standard page width: ${use_width}" 684 else 685 decho 'Using standard page width' 686 fi 687} 688 689# Usage: man_setup_locale 690# Setup necessary locale variables. 691man_setup_locale() { 692 local lang_cc 693 local locstr 694 695 locpaths='.' 696 man_charset='US-ASCII' 697 698 # Setup locale information. 699 if [ -n "$oflag" ]; then 700 decho 'Using non-localized manpages' 701 else 702 # Use the locale tool to give us proper locale information 703 eval $( $LOCALE ) 704 705 if [ -n "$LANG" ]; then 706 locstr=$LANG 707 else 708 locstr=$LC_CTYPE 709 fi 710 711 case "$locstr" in 712 C) ;; 713 C.UTF-8) ;; 714 POSIX) ;; 715 [a-z][a-z]_[A-Z][A-Z]\.*) 716 lang_cc="${locstr%.*}" 717 man_lang="${locstr%_*}" 718 man_country="${lang_cc#*_}" 719 man_charset="${locstr#*.}" 720 locpaths="$locstr" 721 locpaths="$locpaths:$man_lang.$man_charset" 722 if [ "$man_lang" != "en" ]; then 723 locpaths="$locpaths:en.$man_charset" 724 fi 725 locpaths="$locpaths:." 726 ;; 727 *) echo 'Unknown locale, assuming C' >&2 728 ;; 729 esac 730 fi 731 732 decho "Using locale paths: $locpaths" 733} 734 735# Usage: man_usage [exitcode] 736# Display usage for the man utility. 737man_usage() { 738 echo 'Usage:' 739 echo ' man [-adho] [-t | -w] [-K regexp] [-M manpath] [-P pager] [-S mansect]' 740 echo ' [-m arch[:machine]] [-p [eprtv]] [mansect] page [...]' 741 echo ' man -f page [...] -- Emulates whatis(1)' 742 echo ' man -k page [...] -- Emulates apropos(1)' 743 744 # When exit'ing with -h, it's not an error. 745 exit ${1:-1} 746} 747 748# Usage: parse_configs 749# Reads the end-user adjustable config files. 750parse_configs() { 751 local IFS file files 752 753 if [ -n "$parsed_configs" ]; then 754 return 755 fi 756 757 unset IFS 758 759 # Read the global config first in case the user wants 760 # to override config_local. 761 if [ -r "$config_global" ]; then 762 parse_file "$config_global" 763 fi 764 765 # Glob the list of files to parse. 766 set +f 767 files=$(echo $config_local) 768 set -f 769 770 for file in $files; do 771 if [ -r "$file" ]; then 772 parse_file "$file" 773 fi 774 done 775 776 parsed_configs='yes' 777} 778 779# Usage: parse_file file 780# Reads the specified config files. 781parse_file() { 782 local file line tstr var 783 784 file="$1" 785 decho "Parsing config file: $file" 786 while read line; do 787 decho " $line" 2 788 case "$line" in 789 \#*) decho " Comment" 3 790 ;; 791 MANPATH*) decho " MANPATH" 3 792 trim "${line#MANPATH}" 793 add_to_manpath "$tstr" 794 ;; 795 MANLOCALE*) decho " MANLOCALE" 3 796 trim "${line#MANLOCALE}" 797 manlocales="$manlocales:$tstr" 798 ;; 799 MANCONFIG*) decho " MANCONFIG" 3 800 trim "${line#MANCONFIG}" 801 config_local="$tstr" 802 ;; 803 MANSECT*) decho " MANSECT" 3 804 trim "${line#MANSECT}" 805 mansect="$mansect:$tstr" 806 ;; 807 # Set variables in the form of FOO_BAR 808 *_*[\ \ ]*) var="${line%%[\ \ ]*}" 809 trim "${line#$var}" 810 eval "$var=\"$tstr\"" 811 decho " Parsed $var" 3 812 ;; 813 esac 814 done < "$file" 815} 816 817# Usage: search_path 818# Traverse $PATH looking for manpaths. 819search_path() { 820 local IFS p path 821 822 decho "Searching PATH for man directories" 823 824 IFS=: 825 for path in $PATH; do 826 if add_to_manpath "$path/man"; then 827 : 828 elif add_to_manpath "$path/MAN"; then 829 : 830 else 831 case "$path" in 832 */bin) p="${path%/bin}/share/man" 833 add_to_manpath "$p" 834 p="${path%/bin}/man" 835 add_to_manpath "$p" 836 ;; 837 esac 838 fi 839 done 840 unset IFS 841 842 if [ -z "$manpath" ]; then 843 decho ' Unable to find any manpaths, using default' 844 manpath=$man_default_path 845 fi 846} 847 848# Usage: search_whatis cmd [arglist] 849# Do the heavy lifting for apropos/whatis 850search_whatis() { 851 local IFS bad cmd f good key keywords loc opt out path rval wlist 852 853 cmd="$1" 854 shift 855 856 whatis_parse_args "$@" 857 858 build_manpath 859 build_manlocales 860 setup_pager 861 862 if [ "$cmd" = "whatis" ]; then 863 opt="-w" 864 fi 865 866 f='whatis' 867 868 IFS=: 869 for path in $MANPATH; do 870 if [ \! -d "$path" ]; then 871 decho "Skipping non-existent path: $path" 2 872 continue 873 fi 874 875 if [ -f "$path/$f" -a -r "$path/$f" ]; then 876 decho "Found whatis: $path/$f" 877 wlist="$wlist $path/$f" 878 fi 879 880 for loc in $MANLOCALES; do 881 if [ -f "$path/$loc/$f" -a -r "$path/$loc/$f" ]; then 882 decho "Found whatis: $path/$loc/$f" 883 wlist="$wlist $path/$loc/$f" 884 fi 885 done 886 done 887 unset IFS 888 889 if [ -z "$wlist" ]; then 890 echo "$cmd: no whatis databases in $MANPATH" >&2 891 exit 1 892 fi 893 894 rval=0 895 for key in $keywords; do 896 out=$(grep -Ehi $opt -- "$key" $wlist) 897 if [ -n "$out" ]; then 898 good="$good\\n$out" 899 else 900 bad="$bad\\n$key: nothing appropriate" 901 rval=1 902 fi 903 done 904 905 # Strip leading carriage return. 906 good=${good#\\n} 907 bad=${bad#\\n} 908 909 if [ -n "$good" ]; then 910 echo -e "$good" | $MANPAGER 911 fi 912 913 if [ -n "$bad" ]; then 914 echo -e "$bad" >&2 915 fi 916 917 exit $rval 918} 919 920# Usage: setup_cattool page 921# Finds an appropriate decompressor based on extension 922setup_cattool() { 923 case "$1" in 924 *.bz) cattool='/usr/bin/bzcat' ;; 925 *.bz2) cattool='/usr/bin/bzcat' ;; 926 *.gz) cattool='/usr/bin/gzcat' ;; 927 *.lzma) cattool='/usr/bin/lzcat' ;; 928 *.xz) cattool='/usr/bin/xzcat' ;; 929 *.zst) cattool='/usr/bin/zstdcat' ;; 930 *) cattool='/usr/bin/zcat -f' ;; 931 esac 932} 933 934# Usage: setup_pager 935# Correctly sets $MANPAGER 936setup_pager() { 937 # Setup pager. 938 if [ -z "$MANPAGER" ]; then 939 if [ -n "$MANCOLOR" ]; then 940 MANPAGER="less -sR" 941 else 942 if [ -n "$PAGER" ]; then 943 MANPAGER="$PAGER" 944 else 945 MANPAGER="less -s" 946 fi 947 fi 948 fi 949 decho "Using pager: $MANPAGER" 950} 951 952# Usage: trim string 953# Trims whitespace from beginning and end of a variable 954trim() { 955 tstr=$1 956 while true; do 957 case "$tstr" in 958 [\ \ ]*) tstr="${tstr##[\ \ ]}" ;; 959 *[\ \ ]) tstr="${tstr%%[\ \ ]}" ;; 960 *) break ;; 961 esac 962 done 963} 964 965# Usage: whatis_parse_args "$@" 966# Parse commandline args for whatis and apropos. 967whatis_parse_args() { 968 local cmd_arg 969 OPTIND=1 970 while getopts 'd' cmd_arg; do 971 case "${cmd_arg}" in 972 d) debug=$(( $debug + 1 )) ;; 973 *) whatis_usage ;; 974 esac 975 done >&2 976 977 shift $(( $OPTIND - 1 )) 978 979 keywords="$*" 980} 981 982# Usage: whatis_usage 983# Display usage for the whatis/apropos utility. 984whatis_usage() { 985 echo "usage: $cmd [-d] keyword [...]" 986 exit 1 987} 988 989 990 991# Supported commands 992do_apropos() { 993 [ $(stat -f %i /usr/bin/man) -ne $(stat -f %i /usr/bin/apropos) ] && \ 994 exec apropos "$@" 995 search_whatis apropos "$@" 996} 997 998# Usage: do_full_search reg_exp 999# Do a full search of the regular expression passed 1000# as parameter in all man pages 1001do_full_search() { 1002 local gflags re 1003 re=${1} 1004 1005 # Build grep(1) flags 1006 gflags="-H" 1007 1008 # wflag implies -l for grep(1) 1009 if [ -n "$wflag" ]; then 1010 gflags="${gflags} -l" 1011 fi 1012 1013 gflags="${gflags} --label" 1014 1015 set +f 1016 for mpath in $(echo "${MANPATH}" | tr : '[:blank:]'); do 1017 for section in $(echo "${MANSECT}" | tr : '[:blank:]'); do 1018 for manfile in ${mpath}/man${section}/*.${section}*; do 1019 mandoc "${manfile}" 2>/dev/null | 1020 grep -E ${gflags} "${manfile}" -e "${re}" 1021 done 1022 done 1023 done 1024 set -f 1025} 1026 1027do_man() { 1028 local IFS 1029 1030 man_parse_opts "$@" 1031 man_setup 1032 1033 shift $(( $OPTIND - 1 )) 1034 IFS=: 1035 for sect in $MANSECT; do 1036 if [ "$sect" = "$1" ]; then 1037 decho "Detected manual section as first arg: $1" 1038 MANSECT="$1" 1039 shift 1040 break 1041 fi 1042 done 1043 unset IFS 1044 pages="$*" 1045 1046 if [ -z "$pages" -a -z "${Kflag}" ]; then 1047 echo 'What manual page do you want?' >&2 1048 exit 1 1049 fi 1050 1051 if [ ! -z "${Kflag}" ]; then 1052 # Short circuit because -K flag does a sufficiently 1053 # different thing like not showing the man page at all 1054 do_full_search "${REGEXP}" 1055 fi 1056 1057 for page in "$@"; do 1058 decho "Searching for \"$page\"" 1059 man_find_and_display "$page" 1060 done 1061 1062 exit ${ret:-0} 1063} 1064 1065do_manpath() { 1066 manpath_parse_args "$@" 1067 if [ -z "$qflag" ]; then 1068 manpath_warnings 1069 fi 1070 if [ -n "$Lflag" ]; then 1071 build_manlocales 1072 echo $MANLOCALES 1073 else 1074 build_manpath 1075 echo $MANPATH 1076 fi 1077 exit 0 1078} 1079 1080do_whatis() { 1081 [ $(stat -f %i /usr/bin/man) -ne $(stat -f %i /usr/bin/whatis) ] && \ 1082 exec whatis "$@" 1083 search_whatis whatis "$@" 1084} 1085 1086# User's PATH setting decides on the groff-suite to pick up. 1087EQN=eqn 1088NROFF='groff -S -P-h -Wall -mtty-char -mandoc' 1089PIC=pic 1090REFER=refer 1091TBL=tbl 1092TROFF='groff -S -mandoc' 1093VGRIND=vgrind 1094 1095LOCALE=/usr/bin/locale 1096STTY=/bin/stty 1097SYSCTL=/sbin/sysctl 1098 1099debug=0 1100man_default_sections='1:8:2:3:3lua:n:4:5:6:7:9:l' 1101man_default_path='/usr/share/man:/usr/share/openssl/man:/usr/local/share/man:/usr/local/man' 1102cattool='/usr/bin/zcat -f' 1103 1104config_global='/etc/man.conf' 1105 1106# This can be overridden via a setting in /etc/man.conf. 1107config_local='/usr/local/etc/man.d/*.conf' 1108 1109# Set noglobbing for now. I don't want spurious globbing. 1110set -f 1111 1112case "$0" in 1113*apropos) do_apropos "$@" ;; 1114*manpath) do_manpath "$@" ;; 1115*whatis) do_whatis "$@" ;; 1116*) do_man "$@" ;; 1117esac 1118