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 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 | head -n1) 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 "$1/$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 p=$(cd "$(dirname "$manpage")" && pwd) 515 case "$(basename "$p")" in 516 man*|cat*) p=$p/.. ;; 517 *) p=$p/../.. ;; 518 esac 519 if man_check_for_so "$p"; then 520 found_page=yes 521 man_display_page 522 fi 523 return 524 fi 525 ;; 526 esac 527 528 IFS=: 529 for sect in $MANSECT; do 530 decho "Searching section $sect" 2 531 for path in $MANPATH; do 532 for locpath in $locpaths; do 533 p=$path/$locpath 534 p=${p%/.} # Rid ourselves of the trailing /. 535 536 # Check if there is a MACHINE specific manpath. 537 if find_file $p $sect $MACHINE "$1"; then 538 if man_check_for_so $p; then 539 found_page=yes 540 man_display_page 541 if [ -n "$aflag" ]; then 542 continue 2 543 else 544 return 545 fi 546 fi 547 fi 548 549 # Check if there is a MACHINE_ARCH 550 # specific manpath. 551 if find_file $p $sect $MACHINE_ARCH "$1"; then 552 if man_check_for_so $p; then 553 found_page=yes 554 man_display_page 555 if [ -n "$aflag" ]; then 556 continue 2 557 else 558 return 559 fi 560 fi 561 fi 562 563 # Check plain old manpath. 564 if find_file $p $sect '' "$1"; then 565 if man_check_for_so $p; then 566 found_page=yes 567 man_display_page 568 if [ -n "$aflag" ]; then 569 continue 2 570 else 571 return 572 fi 573 fi 574 fi 575 done 576 done 577 done 578 unset IFS 579 580 # Nothing? Well, we are done then. 581 if [ -z "$found_page" ]; then 582 echo "No manual entry for \"$1\"" >&2 583 ret=1 584 return 585 fi 586} 587 588# Usage: man_parse_opts "$@" 589# Parses commandline options for man. 590man_parse_opts() { 591 local cmd_arg 592 593 OPTIND=1 594 while getopts 'K:M:P:S:adfhkm:op:tw' cmd_arg; do 595 case "${cmd_arg}" in 596 K) Kflag=Kflag 597 REGEXP=$OPTARG ;; 598 M) MANPATH=$OPTARG ;; 599 P) MANPAGER=$OPTARG ;; 600 S) MANSECT=$OPTARG ;; 601 a) aflag=aflag ;; 602 d) debug=$(( $debug + 1 )) ;; 603 f) fflag=fflag ;; 604 h) man_usage 0 ;; 605 k) kflag=kflag ;; 606 m) mflag=$OPTARG ;; 607 o) oflag=oflag ;; 608 p) MANROFFSEQ=$OPTARG ;; 609 t) tflag=tflag ;; 610 w) wflag=wflag ;; 611 *) man_usage ;; 612 esac 613 done >&2 614 615 shift $(( $OPTIND - 1 )) 616 617 # Check the args for incompatible options. 618 619 case "${Kflag}${fflag}${kflag}${tflag}${wflag}" in 620 Kflagfflag*) echo "Incompatible options: -K and -f"; man_usage ;; 621 Kflag*kflag*) echo "Incompatible options: -K and -k"; man_usage ;; 622 Kflag*tflag) echo "Incompatible options: -K and -t"; man_usage ;; 623 fflagkflag*) echo "Incompatible options: -f and -k"; man_usage ;; 624 fflag*tflag*) echo "Incompatible options: -f and -t"; man_usage ;; 625 fflag*wflag) echo "Incompatible options: -f and -w"; man_usage ;; 626 *kflagtflag*) echo "Incompatible options: -k and -t"; man_usage ;; 627 *kflag*wflag) echo "Incompatible options: -k and -w"; man_usage ;; 628 *tflagwflag) echo "Incompatible options: -t and -w"; man_usage ;; 629 esac 630 631 # Short circuit for whatis(1) and apropos(1) 632 if [ -n "$fflag" ]; then 633 do_whatis "$@" 634 exit 635 fi 636 637 if [ -n "$kflag" ]; then 638 do_apropos "$@" 639 exit 640 fi 641} 642 643# Usage: man_setup 644# Setup various trivial but essential variables. 645man_setup() { 646 # Setup machine and architecture variables. 647 if [ -n "$mflag" ]; then 648 MACHINE_ARCH=${mflag%%:*} 649 MACHINE=${mflag##*:} 650 fi 651 if [ -z "$MACHINE_ARCH" ]; then 652 MACHINE_ARCH=$($SYSCTL -n hw.machine_arch) 653 fi 654 if [ -z "$MACHINE" ]; then 655 MACHINE=$($SYSCTL -n hw.machine) 656 fi 657 decho "Using architecture: $MACHINE_ARCH:$MACHINE" 658 659 setup_pager 660 build_manpath 661 build_mansect 662 man_setup_locale 663 man_setup_width 664} 665 666# Usage: man_setup_width 667# Set up page width. 668man_setup_width() { 669 local sizes 670 671 unset use_width 672 case "$MANWIDTH" in 673 [0-9]*) 674 if [ "$MANWIDTH" -gt 0 2>/dev/null ]; then 675 use_width=$MANWIDTH 676 fi 677 ;; 678 [Tt][Tt][Yy]) 679 if { sizes=$($STTY size 0>&3 2>/dev/null); } 3>&1; then 680 set -- $sizes 681 if [ $2 -gt 80 ]; then 682 use_width=$(($2-2)) 683 fi 684 fi 685 ;; 686 esac 687 if [ -n "$use_width" ]; then 688 decho "Using non-standard page width: ${use_width}" 689 else 690 decho 'Using standard page width' 691 fi 692} 693 694# Usage: man_setup_locale 695# Setup necessary locale variables. 696man_setup_locale() { 697 local lang_cc 698 local locstr 699 700 locpaths='.' 701 man_charset='US-ASCII' 702 703 # Setup locale information. 704 if [ -n "$oflag" ]; then 705 decho 'Using non-localized manpages' 706 else 707 # Use the locale tool to give us proper locale information 708 eval $( $LOCALE ) 709 710 if [ -n "$LANG" ]; then 711 locstr=$LANG 712 else 713 locstr=$LC_CTYPE 714 fi 715 716 case "$locstr" in 717 C) ;; 718 C.UTF-8) ;; 719 POSIX) ;; 720 [a-z][a-z]_[A-Z][A-Z]\.*) 721 lang_cc="${locstr%.*}" 722 man_lang="${locstr%_*}" 723 man_country="${lang_cc#*_}" 724 man_charset="${locstr#*.}" 725 locpaths="$locstr" 726 locpaths="$locpaths:$man_lang.$man_charset" 727 if [ "$man_lang" != "en" ]; then 728 locpaths="$locpaths:en.$man_charset" 729 fi 730 locpaths="$locpaths:." 731 ;; 732 *) echo 'Unknown locale, assuming C' >&2 733 ;; 734 esac 735 fi 736 737 decho "Using locale paths: $locpaths" 738} 739 740# Usage: man_usage [exitcode] 741# Display usage for the man utility. 742man_usage() { 743 echo 'Usage:' 744 echo ' man [-adho] [-t | -w] [-K regexp] [-M manpath] [-P pager] [-S mansect]' 745 echo ' [-m arch[:machine]] [-p [eprtv]] [mansect] page [...]' 746 echo ' man -f page [...] -- Emulates whatis(1)' 747 echo ' man -k page [...] -- Emulates apropos(1)' 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 exit ${ret:-0} 1068} 1069 1070do_manpath() { 1071 manpath_parse_args "$@" 1072 if [ -z "$qflag" ]; then 1073 manpath_warnings 1074 fi 1075 if [ -n "$Lflag" ]; then 1076 build_manlocales 1077 echo $MANLOCALES 1078 else 1079 build_manpath 1080 echo $MANPATH 1081 fi 1082 exit 0 1083} 1084 1085do_whatis() { 1086 [ $(stat -f %i /usr/bin/man) -ne $(stat -f %i /usr/bin/whatis) ] && \ 1087 exec whatis "$@" 1088 search_whatis whatis "$@" 1089} 1090 1091# User's PATH setting decides on the groff-suite to pick up. 1092EQN=eqn 1093NROFF='groff -S -P-h -Wall -mtty-char -mandoc' 1094PIC=pic 1095REFER=refer 1096TBL=tbl 1097TROFF='groff -S -mandoc' 1098VGRIND=vgrind 1099 1100LOCALE=/usr/bin/locale 1101STTY=/bin/stty 1102SYSCTL=/sbin/sysctl 1103 1104debug=0 1105man_default_sections='1:8:2:3:3lua:n:4:5:6:7:9:l' 1106man_default_path='/usr/share/man:/usr/share/openssl/man:/usr/local/share/man:/usr/local/man' 1107cattool='/usr/bin/zcat -f' 1108 1109config_global='/etc/man.conf' 1110 1111# This can be overridden via a setting in /etc/man.conf. 1112config_local='/usr/local/etc/man.d/*.conf' 1113 1114# Set noglobbing for now. I don't want spurious globbing. 1115set -f 1116 1117case "$0" in 1118*apropos) do_apropos "$@" ;; 1119*manpath) do_manpath "$@" ;; 1120*whatis) do_whatis "$@" ;; 1121*) do_man "$@" ;; 1122esac 1123