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 | 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] [-K regexp] [-M manpath] [-P pager] [-S mansect]' 746 echo ' [-m arch[:machine]] [-p [eprtv]] [mansect] page [...]' 747 echo ' man -f page [...] -- Emulates whatis(1)' 748 echo ' man -k page [...] -- Emulates apropos(1)' 749 750 # When exit'ing with -h, it's not an error. 751 exit ${1:-1} 752} 753 754# Usage: parse_configs 755# Reads the end-user adjustable config files. 756parse_configs() { 757 local IFS file files 758 759 if [ -n "$parsed_configs" ]; then 760 return 761 fi 762 763 unset IFS 764 765 # Read the global config first in case the user wants 766 # to override config_local. 767 if [ -r "$config_global" ]; then 768 parse_file "$config_global" 769 fi 770 771 # Glob the list of files to parse. 772 set +f 773 files=$(echo $config_local) 774 set -f 775 776 for file in $files; do 777 if [ -r "$file" ]; then 778 parse_file "$file" 779 fi 780 done 781 782 parsed_configs='yes' 783} 784 785# Usage: parse_file file 786# Reads the specified config files. 787parse_file() { 788 local file line tstr var 789 790 file="$1" 791 decho "Parsing config file: $file" 792 while read line; do 793 decho " $line" 2 794 case "$line" in 795 \#*) decho " Comment" 3 796 ;; 797 MANPATH*) decho " MANPATH" 3 798 trim "${line#MANPATH}" 799 add_to_manpath "$tstr" 800 ;; 801 MANLOCALE*) decho " MANLOCALE" 3 802 trim "${line#MANLOCALE}" 803 manlocales="$manlocales:$tstr" 804 ;; 805 MANCONFIG*) decho " MANCONFIG" 3 806 trim "${line#MANCONFIG}" 807 config_local="$tstr" 808 ;; 809 MANSECT*) decho " MANSECT" 3 810 trim "${line#MANSECT}" 811 mansect="$mansect:$tstr" 812 ;; 813 # Set variables in the form of FOO_BAR 814 *_*[\ \ ]*) var="${line%%[\ \ ]*}" 815 trim "${line#$var}" 816 eval "$var=\"$tstr\"" 817 decho " Parsed $var" 3 818 ;; 819 esac 820 done < "$file" 821} 822 823# Usage: search_path 824# Traverse $PATH looking for manpaths. 825search_path() { 826 local IFS p path 827 828 decho "Searching PATH for man directories" 829 830 IFS=: 831 for path in $PATH; do 832 if add_to_manpath "$path/man"; then 833 : 834 elif add_to_manpath "$path/MAN"; then 835 : 836 else 837 case "$path" in 838 */bin) p="${path%/bin}/share/man" 839 add_to_manpath "$p" 840 p="${path%/bin}/man" 841 add_to_manpath "$p" 842 ;; 843 esac 844 fi 845 done 846 unset IFS 847 848 if [ -z "$manpath" ]; then 849 decho ' Unable to find any manpaths, using default' 850 manpath=$man_default_path 851 fi 852} 853 854# Usage: search_whatis cmd [arglist] 855# Do the heavy lifting for apropos/whatis 856search_whatis() { 857 local IFS bad cmd f good key keywords loc opt out path rval wlist 858 859 cmd="$1" 860 shift 861 862 whatis_parse_args "$@" 863 864 build_manpath 865 build_manlocales 866 setup_pager 867 868 if [ "$cmd" = "whatis" ]; then 869 opt="-w" 870 fi 871 872 f='whatis' 873 874 IFS=: 875 for path in $MANPATH; do 876 if [ \! -d "$path" ]; then 877 decho "Skipping non-existent path: $path" 2 878 continue 879 fi 880 881 if [ -f "$path/$f" -a -r "$path/$f" ]; then 882 decho "Found whatis: $path/$f" 883 wlist="$wlist $path/$f" 884 fi 885 886 for loc in $MANLOCALES; do 887 if [ -f "$path/$loc/$f" -a -r "$path/$loc/$f" ]; then 888 decho "Found whatis: $path/$loc/$f" 889 wlist="$wlist $path/$loc/$f" 890 fi 891 done 892 done 893 unset IFS 894 895 if [ -z "$wlist" ]; then 896 echo "$cmd: no whatis databases in $MANPATH" >&2 897 exit 1 898 fi 899 900 rval=0 901 for key in $keywords; do 902 out=$(grep -Ehi $opt -- "$key" $wlist) 903 if [ -n "$out" ]; then 904 good="$good\\n$out" 905 else 906 bad="$bad\\n$key: nothing appropriate" 907 rval=1 908 fi 909 done 910 911 # Strip leading carriage return. 912 good=${good#\\n} 913 bad=${bad#\\n} 914 915 if [ -n "$good" ]; then 916 printf '%b\n' "$good" | $MANPAGER 917 fi 918 919 if [ -n "$bad" ]; then 920 printf '%b\n' "$bad" >&2 921 fi 922 923 exit $rval 924} 925 926# Usage: setup_cattool page 927# Finds an appropriate decompressor based on extension 928setup_cattool() { 929 case "$1" in 930 *.bz) cattool='/usr/bin/bzcat' ;; 931 *.bz2) cattool='/usr/bin/bzcat' ;; 932 *.gz) cattool='/usr/bin/gzcat' ;; 933 *.lzma) cattool='/usr/bin/lzcat' ;; 934 *.xz) cattool='/usr/bin/xzcat' ;; 935 *.zst) cattool='/usr/bin/zstdcat' ;; 936 *) cattool='/usr/bin/zcat -f' ;; 937 esac 938} 939 940# Usage: setup_pager 941# Correctly sets $MANPAGER 942setup_pager() { 943 # Setup pager. 944 if [ -z "$MANPAGER" ]; then 945 if [ -n "$MANCOLOR" ]; then 946 MANPAGER="less -sR" 947 else 948 if [ -n "$PAGER" ]; then 949 MANPAGER="$PAGER" 950 else 951 MANPAGER="less -s" 952 fi 953 fi 954 fi 955 decho "Using pager: $MANPAGER" 956} 957 958# Usage: trim string 959# Trims whitespace from beginning and end of a variable 960trim() { 961 tstr=$1 962 while true; do 963 case "$tstr" in 964 [\ \ ]*) tstr="${tstr##[\ \ ]}" ;; 965 *[\ \ ]) tstr="${tstr%%[\ \ ]}" ;; 966 *) break ;; 967 esac 968 done 969} 970 971# Usage: whatis_parse_args "$@" 972# Parse commandline args for whatis and apropos. 973whatis_parse_args() { 974 local cmd_arg 975 OPTIND=1 976 while getopts 'd' cmd_arg; do 977 case "${cmd_arg}" in 978 d) debug=$(( $debug + 1 )) ;; 979 *) whatis_usage ;; 980 esac 981 done >&2 982 983 shift $(( $OPTIND - 1 )) 984 985 keywords="$*" 986} 987 988# Usage: whatis_usage 989# Display usage for the whatis/apropos utility. 990whatis_usage() { 991 echo "usage: $cmd [-d] keyword [...]" 992 exit 1 993} 994 995 996 997# Supported commands 998do_apropos() { 999 [ $(stat -f %i /usr/bin/man) -ne $(stat -f %i /usr/bin/apropos) ] && \ 1000 exec apropos "$@" 1001 search_whatis apropos "$@" 1002} 1003 1004# Usage: do_full_search reg_exp 1005# Do a full search of the regular expression passed 1006# as parameter in all man pages 1007do_full_search() { 1008 local gflags re 1009 re=${1} 1010 1011 # Build grep(1) flags 1012 gflags="-H" 1013 1014 # wflag implies -l for grep(1) 1015 if [ -n "$wflag" ]; then 1016 gflags="${gflags} -l" 1017 fi 1018 1019 gflags="${gflags} --label" 1020 1021 set +f 1022 for mpath in $(echo "${MANPATH}" | tr : '[:blank:]'); do 1023 for section in $(echo "${MANSECT}" | tr : '[:blank:]'); do 1024 for manfile in ${mpath}/man${section}/*.${section}*; do 1025 mandoc "${manfile}" 2>/dev/null | 1026 grep -E ${gflags} "${manfile}" -e "${re}" 1027 done 1028 done 1029 done 1030 set -f 1031} 1032 1033do_man() { 1034 local IFS 1035 1036 man_parse_opts "$@" 1037 man_setup 1038 1039 shift $(( $OPTIND - 1 )) 1040 IFS=: 1041 for sect in $MANSECT; do 1042 if [ "$sect" = "$1" ]; then 1043 decho "Detected manual section as first arg: $1" 1044 MANSECT="$1" 1045 shift 1046 break 1047 fi 1048 done 1049 unset IFS 1050 pages="$*" 1051 1052 if [ -z "$pages" -a -z "${Kflag}" ]; then 1053 echo 'What manual page do you want?' >&2 1054 exit 1 1055 fi 1056 1057 if [ ! -z "${Kflag}" ]; then 1058 # Short circuit because -K flag does a sufficiently 1059 # different thing like not showing the man page at all 1060 do_full_search "${REGEXP}" 1061 fi 1062 1063 for page in "$@"; do 1064 decho "Searching for \"$page\"" 1065 man_find_and_display "$page" 1066 done 1067 1068 exit ${ret:-0} 1069} 1070 1071do_manpath() { 1072 manpath_parse_args "$@" 1073 if [ -z "$qflag" ]; then 1074 manpath_warnings 1075 fi 1076 if [ -n "$Lflag" ]; then 1077 build_manlocales 1078 echo $MANLOCALES 1079 else 1080 build_manpath 1081 echo $MANPATH 1082 fi 1083 exit 0 1084} 1085 1086do_whatis() { 1087 [ $(stat -f %i /usr/bin/man) -ne $(stat -f %i /usr/bin/whatis) ] && \ 1088 exec whatis "$@" 1089 search_whatis whatis "$@" 1090} 1091 1092# User's PATH setting decides on the groff-suite to pick up. 1093EQN=eqn 1094NROFF='groff -S -P-h -Wall -mtty-char -mandoc' 1095PIC=pic 1096REFER=refer 1097TBL=tbl 1098TROFF='groff -S -mandoc' 1099VGRIND=vgrind 1100 1101LOCALE=/usr/bin/locale 1102STTY=/bin/stty 1103SYSCTL=/sbin/sysctl 1104 1105debug=0 1106man_default_sections='1:8:2:3:3lua:n:4:5:6:7:9:l' 1107man_default_path='/usr/share/man:/usr/share/openssl/man:/usr/local/share/man:/usr/local/man' 1108cattool='/usr/bin/zcat -f' 1109 1110config_global='/etc/man.conf' 1111 1112# This can be overridden via a setting in /etc/man.conf. 1113config_local='/usr/local/etc/man.d/*.conf' 1114 1115# Set noglobbing for now. I don't want spurious globbing. 1116set -f 1117 1118case "$0" in 1119*apropos) do_apropos "$@" ;; 1120*manpath) do_manpath "$@" ;; 1121*whatis) do_whatis "$@" ;; 1122*) do_man "$@" ;; 1123esac 1124