3 # SPDX-License-Identifier: BSD-2-Clause
5 # Copyright (c) 2010 Gordon Tetlow
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
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.
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
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.
36 # Usage: add_to_manpath path
37 # Adds a variable to manpath while ensuring we don't have duplicates.
38 # Returns true if we were able to add something. False otherwise.
41 *:$1) decho " Skipping duplicate manpath entry $1" 2 ;;
42 $1:*) decho " Skipping duplicate manpath entry $1" 2 ;;
43 *:$1:*) decho " Skipping duplicate manpath entry $1" 2 ;;
44 *) if [ -d "$1" ]; then
45 decho " Adding $1 to manpath"
55 # Usage: build_manlocales
56 # Builds a correct MANLOCALES variable.
58 # If the user has set manlocales, who are we to argue.
59 if [ -n "$MANLOCALES" ]; then
66 MANLOCALES=${manlocales#:}
68 decho "Available manual locales: $MANLOCALES"
71 # Usage: build_mansect
72 # Builds a correct MANSECT variable.
74 # If the user has set mansect, who are we to argue.
75 if [ -n "$MANSECT" ]; then
84 if [ -z "$MANSECT" ]; then
85 MANSECT=$man_default_sections
87 decho "Using manual sections: $MANSECT"
90 # Usage: build_manpath
91 # Builds a correct MANPATH variable.
95 # If the user has set a manpath, who are we to argue.
96 if [ -n "$MANPATH" ]; then
98 *:) PREPEND_MANPATH=${MANPATH} ;;
99 :*) APPEND_MANPATH=${MANPATH} ;;
101 PREPEND_MANPATH=${MANPATH%%::*}
102 APPEND_MANPATH=${MANPATH#*::}
108 if [ -n "$PREPEND_MANPATH" ]; then
110 for path in $PREPEND_MANPATH; do
111 add_to_manpath "$path"
118 decho "Adding default manpath entries"
120 for path in $man_default_path; do
121 add_to_manpath "$path"
127 if [ -n "$APPEND_MANPATH" ]; then
129 for path in $APPEND_MANPATH; do
130 add_to_manpath "$path"
137 decho "Using manual path: $MANPATH"
140 # Usage: check_cat catglob
141 # Checks to see if a cat glob is available.
146 setup_cattool "$catpage"
147 decho " Found catpage \"$catpage\""
154 # Usage: check_man manglob catglob
155 # Given 2 globs, figures out if the manglob is available, if so, check to
156 # see if the catglob is also available and up to date.
159 # We have a match, check for a cat page
161 setup_cattool "$manpage"
162 decho " Found manpage \"$manpage\""
164 if [ -n "${use_width}" ]; then
167 decho " Skipping catpage: non-standard page width"
168 elif exists "$2" && is_newer $found "$manpage"; then
169 # cat page found and is newer, use that
172 setup_cattool "$catpage"
173 decho " Using catpage \"$catpage\""
175 # no cat page or is older
177 decho " Skipping catpage: not found or old"
185 # Usage: decho "string" [debuglevel]
186 # Echoes to stderr string prefaced with -- if high enough debuglevel.
188 if [ $debug -ge ${2:-1} ]; then
194 # Returns true if glob resolves to a real file.
198 # Don't accidentally inherit callers IFS (breaks perl manpages)
201 # Use some globbing tricks in the shell to determine if a file
207 if [ "$1" != "$2" -a -r "$2" ]; then
215 # Usage: find_file path section subdir pagename
216 # Returns: true if something is matched and found.
217 # Search the given path/section combo for a given page.
219 local manroot catroot mann man0 catn cat0
224 manroot="$manroot/$3"
225 catroot="$catroot/$3"
228 if [ ! -d "$manroot" -a ! -d "$catroot" ]; then
231 decho " Searching directory $manroot" 2
233 mann="$manroot/$4.$2*"
234 man0="$manroot/$4.0*"
235 catn="$catroot/$4.$2*"
236 cat0="$catroot/$4.0*"
238 # This is the behavior as seen by the original man utility.
239 # Let's not change that which doesn't seem broken.
240 if check_man "$mann" "$catn"; then
242 elif check_man "$man0" "$cat0"; then
244 elif check_cat "$catn"; then
246 elif check_cat "$cat0"; then
253 # Usage: is_newer file1 file2
254 # Returns true if file1 is newer than file2 as calculated by mtime.
256 if ! [ "$1" -ot "$2" ]; then
257 decho " mtime: $1 not older than $2" 3
260 decho " mtime: $1 older than $2" 3
265 # Usage: manpath_parse_args "$@"
266 # Parses commandline options for manpath.
267 manpath_parse_args() {
271 while getopts 'Ldq' cmd_arg; do
274 d) debug=$(( $debug + 1 )) ;;
281 # Usage: manpath_usage
282 # Display usage for the manpath(1) utility.
284 echo 'usage: manpath [-Ldq]' >&2
288 # Usage: manpath_warnings
289 # Display some warnings to stderr.
291 if [ -n "$Lflag" -a -n "$MANLOCALES" ]; then
292 echo "(Warning: MANLOCALES environment variable set)" >&2
296 # Usage: man_check_for_so page path
297 # Returns: True if able to resolve the file, false if it ended in tears.
298 # Detects the presence of the .so directive and causes the file to be
299 # redirected to another source file.
304 if [ -n "$catpage" ]; then
308 # We need to loop to accommodate multiple .so directives.
311 line=$($cattool "$manpage" | head -1)
313 .so*) trim "${line#.so}"
314 decho "$manpage includes $tstr"
315 # Glob and check for the file.
316 if ! check_man "$path/$tstr*" ""; then
317 decho " Unable to find $tstr"
328 # Usage: man_display_page
329 # Display either the manpage or catpage depending on the use_cat variable
331 local IFS pipeline testline
333 # We are called with IFS set to colon. This causes really weird
334 # things to happen for the variables that have spaces in them.
337 # If we are supposed to use a catpage and we aren't using troff(1)
338 # just zcat the catpage and we are done.
339 if [ -z "$tflag" -a -n "$use_cat" ]; then
340 if [ -n "$wflag" ]; then
341 echo "$catpage (source: \"$manpage\")"
344 if [ $debug -gt 0 ]; then
345 decho "Command: $cattool \"$catpage\" | $MANPAGER"
348 eval "$cattool \"$catpage\" | $MANPAGER"
355 # Okay, we are using the manpage, do we just need to output the
356 # name of the manpage?
357 if [ -n "$wflag" ]; then
363 if [ -n "$use_width" ]; then
364 mandoc_args="-O width=${use_width}"
366 testline="mandoc -Tlint -Wunsupp >/dev/null 2>&1"
367 if [ -n "$tflag" ]; then
368 pipeline="mandoc -Tps $mandoc_args"
370 pipeline="mandoc $mandoc_args | $MANPAGER"
373 if ! eval "$cattool \"$manpage\" | $testline" ;then
374 if which -s groff; then
375 man_display_page_groff
377 echo "This manpage needs groff(1) to be rendered" >&2
378 echo "First install groff(1): " >&2
379 echo "pkg install groff " >&2
385 if [ $debug -gt 0 ]; then
386 decho "Command: $cattool \"$manpage\" | $pipeline"
389 eval "$cattool \"$manpage\" | $pipeline"
394 # Usage: man_display_page_groff
395 # Display the manpage using groff
396 man_display_page_groff() {
397 local EQN NROFF PIC TBL TROFF REFER VGRIND
398 local IFS l nroff_dev pipeline preproc_arg tool
400 # So, we really do need to parse the manpage. First, figure out the
401 # device flag (-T) we have to pass to eqn(1) and groff(1). Then,
402 # setup the pipeline of commands based on the user's request.
404 # If the manpage is from a particular charset, we need to setup nroff
405 # to properly output for the correct device.
408 # I don't pretend to know this; I'm just copying from the
409 # previous version of man(1).
410 case "$man_charset" in
411 KOI8-R) nroff_dev="koi8-r" ;;
412 ISO8859-1) nroff_dev="latin1" ;;
413 ISO8859-15) nroff_dev="latin1" ;;
414 UTF-8) nroff_dev="utf8" ;;
415 *) nroff_dev="ascii" ;;
418 NROFF="$NROFF -T$nroff_dev"
419 EQN="$EQN -T$nroff_dev"
421 # Iff the manpage is from the locale and not just the charset,
422 # then we need to define the locale string.
424 */${man_lang}_${man_country}.${man_charset}/*)
425 NROFF="$NROFF -dlocale=$man_lang.$man_charset"
427 */${man_lang}.${man_charset}/*)
428 NROFF="$NROFF -dlocale=$man_lang.$man_charset"
432 # Allow language specific calls to override the default
434 l=$(echo $man_lang | tr [:lower:] [:upper:])
435 for tool in EQN NROFF PIC TBL TROFF REFER VGRIND; do
436 eval "$tool=\${${tool}_$l:-\$$tool}"
439 *) NROFF="$NROFF -Tascii"
444 if [ -z "$MANCOLOR" ]; then
448 if [ -n "${use_width}" ]; then
449 NROFF="$NROFF -rLL=${use_width}n -rLT=${use_width}n"
452 if [ -n "$MANROFFSEQ" ]; then
455 while getopts 'egprtv' preproc_arg; do
456 case "${preproc_arg}" in
457 e) pipeline="$pipeline | $EQN" ;;
458 g) ;; # Ignore for compatibility.
459 p) pipeline="$pipeline | $PIC" ;;
460 r) pipeline="$pipeline | $REFER" ;;
461 t) pipeline="$pipeline | $TBL" ;;
462 v) pipeline="$pipeline | $VGRIND" ;;
466 # Strip the leading " | " from the resulting pipeline.
467 pipeline="${pipeline#" | "}"
472 if [ -n "$tflag" ]; then
473 pipeline="$pipeline | $TROFF"
475 pipeline="$pipeline | $NROFF | $MANPAGER"
478 if [ $debug -gt 0 ]; then
479 decho "Command: $cattool \"$manpage\" | $pipeline"
482 eval "$cattool \"$manpage\" | $pipeline"
487 # Usage: man_find_and_display page
488 # Search through the manpaths looking for the given page.
489 man_find_and_display() {
490 local found_page locpath p path sect
492 # Check to see if it's a file. But only if it has a '/' in
495 */*) if [ -f "$1" -a -r "$1" ]; then
496 decho "Found a usable page, displaying that"
499 setup_cattool "$manpage"
500 if man_check_for_so "$manpage" "$(dirname \"$manpage"")"; then
510 for sect in $MANSECT; do
511 decho "Searching section $sect" 2
512 for path in $MANPATH; do
513 for locpath in $locpaths; do
515 p=${p%/.} # Rid ourselves of the trailing /.
517 # Check if there is a MACHINE specific manpath.
518 if find_file $p $sect $MACHINE "$1"; then
519 if man_check_for_so "$manpage" $p; then
522 if [ -n "$aflag" ]; then
530 # Check if there is a MACHINE_ARCH
532 if find_file $p $sect $MACHINE_ARCH "$1"; then
533 if man_check_for_so "$manpage" $p; then
536 if [ -n "$aflag" ]; then
544 # Check plain old manpath.
545 if find_file $p $sect '' "$1"; then
546 if man_check_for_so "$manpage" $p; then
549 if [ -n "$aflag" ]; then
561 # Nothing? Well, we are done then.
562 if [ -z "$found_page" ]; then
563 echo "No manual entry for \"$1\"" >&2
569 # Usage: man_parse_opts "$@"
570 # Parses commandline options for man.
575 while getopts 'K:M:P:S:adfhkm:op:tw' cmd_arg; do
579 M) MANPATH=$OPTARG ;;
580 P) MANPAGER=$OPTARG ;;
581 S) MANSECT=$OPTARG ;;
583 d) debug=$(( $debug + 1 )) ;;
589 p) MANROFFSEQ=$OPTARG ;;
596 shift $(( $OPTIND - 1 ))
598 # Check the args for incompatible options.
600 case "${Kflag}${fflag}${kflag}${tflag}${wflag}" in
601 Kflagfflag*) echo "Incompatible options: -K and -f"; man_usage ;;
602 Kflag*kflag*) echo "Incompatible options: -K and -k"; man_usage ;;
603 Kflag*tflag) echo "Incompatible options: -K and -t"; man_usage ;;
604 fflagkflag*) echo "Incompatible options: -f and -k"; man_usage ;;
605 fflag*tflag*) echo "Incompatible options: -f and -t"; man_usage ;;
606 fflag*wflag) echo "Incompatible options: -f and -w"; man_usage ;;
607 *kflagtflag*) echo "Incompatible options: -k and -t"; man_usage ;;
608 *kflag*wflag) echo "Incompatible options: -k and -w"; man_usage ;;
609 *tflagwflag) echo "Incompatible options: -t and -w"; man_usage ;;
612 # Short circuit for whatis(1) and apropos(1)
613 if [ -n "$fflag" ]; then
618 if [ -n "$kflag" ]; then
625 # Setup various trivial but essential variables.
627 # Setup machine and architecture variables.
628 if [ -n "$mflag" ]; then
629 MACHINE_ARCH=${mflag%%:*}
632 if [ -z "$MACHINE_ARCH" ]; then
633 MACHINE_ARCH=$($SYSCTL -n hw.machine_arch)
635 if [ -z "$MACHINE" ]; then
636 MACHINE=$($SYSCTL -n hw.machine)
638 decho "Using architecture: $MACHINE_ARCH:$MACHINE"
647 # Usage: man_setup_width
655 if [ "$MANWIDTH" -gt 0 2>/dev/null ]; then
660 if { sizes=$($STTY size 0>&3 2>/dev/null); } 3>&1; then
662 if [ $2 -gt 80 ]; then
668 if [ -n "$use_width" ]; then
669 decho "Using non-standard page width: ${use_width}"
671 decho 'Using standard page width'
675 # Usage: man_setup_locale
676 # Setup necessary locale variables.
682 man_charset='US-ASCII'
684 # Setup locale information.
685 if [ -n "$oflag" ]; then
686 decho 'Using non-localized manpages'
688 # Use the locale tool to give us proper locale information
691 if [ -n "$LANG" ]; then
701 [a-z][a-z]_[A-Z][A-Z]\.*)
702 lang_cc="${locstr%.*}"
703 man_lang="${locstr%_*}"
704 man_country="${lang_cc#*_}"
705 man_charset="${locstr#*.}"
707 locpaths="$locpaths:$man_lang.$man_charset"
708 if [ "$man_lang" != "en" ]; then
709 locpaths="$locpaths:en.$man_charset"
711 locpaths="$locpaths:."
713 *) echo 'Unknown locale, assuming C' >&2
718 decho "Using locale paths: $locpaths"
721 # Usage: man_usage [exitcode]
722 # Display usage for the man utility.
725 echo ' man [-adho] [-t | -w] [-K regexp] [-M manpath] [-P pager] [-S mansect]'
726 echo ' [-m arch[:machine]] [-p [eprtv]] [mansect] page [...]'
727 echo ' man -f page [...] -- Emulates whatis(1)'
728 echo ' man -k page [...] -- Emulates apropos(1)'
730 # When exit'ing with -h, it's not an error.
734 # Usage: parse_configs
735 # Reads the end-user adjustable config files.
739 if [ -n "$parsed_configs" ]; then
745 # Read the global config first in case the user wants
746 # to override config_local.
747 if [ -r "$config_global" ]; then
748 parse_file "$config_global"
751 # Glob the list of files to parse.
753 files=$(echo $config_local)
756 for file in $files; do
757 if [ -r "$file" ]; then
765 # Usage: parse_file file
766 # Reads the specified config files.
768 local file line tstr var
771 decho "Parsing config file: $file"
775 \#*) decho " Comment" 3
777 MANPATH*) decho " MANPATH" 3
778 trim "${line#MANPATH}"
779 add_to_manpath "$tstr"
781 MANLOCALE*) decho " MANLOCALE" 3
782 trim "${line#MANLOCALE}"
783 manlocales="$manlocales:$tstr"
785 MANCONFIG*) decho " MANCONFIG" 3
786 trim "${line#MANCONFIG}"
789 MANSECT*) decho " MANSECT" 3
790 trim "${line#MANSECT}"
791 mansect="$mansect:$tstr"
793 # Set variables in the form of FOO_BAR
794 *_*[\ \ ]*) var="${line%%[\ \ ]*}"
796 eval "$var=\"$tstr\""
797 decho " Parsed $var" 3
804 # Traverse $PATH looking for manpaths.
808 decho "Searching PATH for man directories"
811 for path in $PATH; do
812 if add_to_manpath "$path/man"; then
814 elif add_to_manpath "$path/MAN"; then
818 */bin) p="${path%/bin}/share/man"
828 if [ -z "$manpath" ]; then
829 decho ' Unable to find any manpaths, using default'
830 manpath=$man_default_path
834 # Usage: search_whatis cmd [arglist]
835 # Do the heavy lifting for apropos/whatis
837 local IFS bad cmd f good key keywords loc opt out path rval wlist
842 whatis_parse_args "$@"
848 if [ "$cmd" = "whatis" ]; then
855 for path in $MANPATH; do
856 if [ \! -d "$path" ]; then
857 decho "Skipping non-existent path: $path" 2
861 if [ -f "$path/$f" -a -r "$path/$f" ]; then
862 decho "Found whatis: $path/$f"
863 wlist="$wlist $path/$f"
866 for loc in $MANLOCALES; do
867 if [ -f "$path/$loc/$f" -a -r "$path/$loc/$f" ]; then
868 decho "Found whatis: $path/$loc/$f"
869 wlist="$wlist $path/$loc/$f"
875 if [ -z "$wlist" ]; then
876 echo "$cmd: no whatis databases in $MANPATH" >&2
881 for key in $keywords; do
882 out=$(grep -Ehi $opt -- "$key" $wlist)
883 if [ -n "$out" ]; then
886 bad="$bad\\n$key: nothing appropriate"
891 # Strip leading carriage return.
895 if [ -n "$good" ]; then
896 echo -e "$good" | $MANPAGER
899 if [ -n "$bad" ]; then
906 # Usage: setup_cattool page
907 # Finds an appropriate decompressor based on extension
910 *.bz) cattool='/usr/bin/bzcat' ;;
911 *.bz2) cattool='/usr/bin/bzcat' ;;
912 *.gz) cattool='/usr/bin/gzcat' ;;
913 *.lzma) cattool='/usr/bin/lzcat' ;;
914 *.xz) cattool='/usr/bin/xzcat' ;;
915 *.zst) cattool='/usr/bin/zstdcat' ;;
916 *) cattool='/usr/bin/zcat -f' ;;
921 # Correctly sets $MANPAGER
924 if [ -z "$MANPAGER" ]; then
925 if [ -n "$MANCOLOR" ]; then
928 if [ -n "$PAGER" ]; then
935 decho "Using pager: $MANPAGER"
939 # Trims whitespace from beginning and end of a variable
944 [\ \ ]*) tstr="${tstr##[\ \ ]}" ;;
945 *[\ \ ]) tstr="${tstr%%[\ \ ]}" ;;
951 # Usage: whatis_parse_args "$@"
952 # Parse commandline args for whatis and apropos.
953 whatis_parse_args() {
956 while getopts 'd' cmd_arg; do
958 d) debug=$(( $debug + 1 )) ;;
963 shift $(( $OPTIND - 1 ))
968 # Usage: whatis_usage
969 # Display usage for the whatis/apropos utility.
971 echo "usage: $cmd [-d] keyword [...]"
979 [ $(stat -f %i /usr/bin/man) -ne $(stat -f %i /usr/bin/apropos) ] && \
981 search_whatis apropos "$@"
984 # Usage: do_full_search reg_exp
985 # Do a full search of the regular expression passed
986 # as parameter in all man pages
991 # Build grep(1) flags
994 # wflag implies -l for grep(1)
995 if [ -n "$wflag" ]; then
996 gflags="${gflags} -l"
999 gflags="${gflags} --label"
1002 for mpath in $(echo "${MANPATH}" | tr : '[:blank:]'); do
1003 for section in $(echo "${MANSECT}" | tr : '[:blank:]'); do
1004 for manfile in ${mpath}/man${section}/*.${section}*; do
1005 mandoc "${manfile}" 2>/dev/null |
1006 grep -E ${gflags} "${manfile}" -e "${re}"
1019 shift $(( $OPTIND - 1 ))
1021 for sect in $MANSECT; do
1022 if [ "$sect" = "$1" ]; then
1023 decho "Detected manual section as first arg: $1"
1032 if [ -z "$pages" -a -z "${Kflag}" ]; then
1033 echo 'What manual page do you want?' >&2
1037 if [ ! -z "${Kflag}" ]; then
1038 # Short circuit because -K flag does a sufficiently
1039 # different thing like not showing the man page at all
1040 do_full_search "${REGEXP}"
1043 for page in "$@"; do
1044 decho "Searching for \"$page\""
1045 man_find_and_display "$page"
1052 manpath_parse_args "$@"
1053 if [ -z "$qflag" ]; then
1056 if [ -n "$Lflag" ]; then
1067 [ $(stat -f %i /usr/bin/man) -ne $(stat -f %i /usr/bin/whatis) ] && \
1069 search_whatis whatis "$@"
1072 # User's PATH setting decides on the groff-suite to pick up.
1074 NROFF='groff -S -P-h -Wall -mtty-char -mandoc'
1078 TROFF='groff -S -mandoc'
1081 LOCALE=/usr/bin/locale
1086 man_default_sections='1:8:2:3:3lua:n:4:5:6:7:9:l'
1087 man_default_path='/usr/share/man:/usr/share/openssl/man:/usr/local/share/man:/usr/local/man'
1088 cattool='/usr/bin/zcat -f'
1090 config_global='/etc/man.conf'
1092 # This can be overridden via a setting in /etc/man.conf.
1093 config_local='/usr/local/etc/man.d/*.conf'
1095 # Set noglobbing for now. I don't want spurious globbing.
1099 *apropos) do_apropos "$@" ;;
1100 *manpath) do_manpath "$@" ;;
1101 *whatis) do_whatis "$@" ;;