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
31 # Rendering a manual page is fast. Even a manual page several 100k in size
32 # takes less than a CPU second. If it takes much longer, it is very likely
33 # that a tool like mandoc(1) is running in an infinite loop. In this case
34 # it is better to terminate it.
37 # Usage: add_to_manpath path
38 # Adds a variable to manpath while ensuring we don't have duplicates.
39 # Returns true if we were able to add something. False otherwise.
42 *:$1) decho " Skipping duplicate manpath entry $1" 2 ;;
43 $1:*) decho " Skipping duplicate manpath entry $1" 2 ;;
44 *:$1:*) decho " Skipping duplicate manpath entry $1" 2 ;;
45 *) if [ -d "$1" ]; then
46 decho " Adding $1 to manpath"
56 # Usage: build_manlocales
57 # Builds a correct MANLOCALES variable.
59 # If the user has set manlocales, who are we to argue.
60 if [ -n "$MANLOCALES" ]; then
67 MANLOCALES=${manlocales#:}
69 decho "Available manual locales: $MANLOCALES"
72 # Usage: build_mansect
73 # Builds a correct MANSECT variable.
75 # If the user has set mansect, who are we to argue.
76 if [ -n "$MANSECT" ]; then
85 if [ -z "$MANSECT" ]; then
86 MANSECT=$man_default_sections
88 decho "Using manual sections: $MANSECT"
91 # Usage: build_manpath
92 # Builds a correct MANPATH variable.
96 # If the user has set a manpath, who are we to argue.
97 if [ -n "$MANPATH" ]; then
99 *:) PREPEND_MANPATH=${MANPATH} ;;
100 :*) APPEND_MANPATH=${MANPATH} ;;
102 PREPEND_MANPATH=${MANPATH%%::*}
103 APPEND_MANPATH=${MANPATH#*::}
109 if [ -n "$PREPEND_MANPATH" ]; then
111 for path in $PREPEND_MANPATH; do
112 add_to_manpath "$path"
119 decho "Adding default manpath entries"
121 for path in $man_default_path; do
122 add_to_manpath "$path"
128 if [ -n "$APPEND_MANPATH" ]; then
130 for path in $APPEND_MANPATH; do
131 add_to_manpath "$path"
138 decho "Using manual path: $MANPATH"
141 # Usage: check_cat catglob
142 # Checks to see if a cat glob is available.
147 setup_cattool $catpage
148 decho " Found catpage $catpage"
155 # Usage: check_man manglob catglob
156 # Given 2 globs, figures out if the manglob is available, if so, check to
157 # see if the catglob is also available and up to date.
160 # We have a match, check for a cat page
162 setup_cattool $manpage
163 decho " Found manpage $manpage"
165 if [ -n "${use_width}" ]; then
168 decho " Skipping catpage: non-standard page width"
169 elif exists "$2" && is_newer $found $manpage; then
170 # cat page found and is newer, use that
173 setup_cattool $catpage
174 decho " Using catpage $catpage"
176 # no cat page or is older
178 decho " Skipping catpage: not found or old"
186 # Usage: decho "string" [debuglevel]
187 # Echoes to stderr string prefaced with -- if high enough debuglevel.
189 if [ $debug -ge ${2:-1} ]; then
195 # Returns true if glob resolves to a real file.
199 # Don't accidentally inherit callers IFS (breaks perl manpages)
202 # Use some globbing tricks in the shell to determine if a file
208 if [ "$1" != "$2" -a -r "$2" ]; then
216 # Usage: find_file path section subdir pagename
217 # Returns: true if something is matched and found.
218 # Search the given path/section combo for a given page.
220 local manroot catroot mann man0 catn cat0
225 manroot="$manroot/$3"
226 catroot="$catroot/$3"
229 if [ ! -d "$manroot" -a ! -d "$catroot" ]; then
232 decho " Searching directory $manroot" 2
234 mann="$manroot/$4.$2*"
235 man0="$manroot/$4.0*"
236 catn="$catroot/$4.$2*"
237 cat0="$catroot/$4.0*"
239 # This is the behavior as seen by the original man utility.
240 # Let's not change that which doesn't seem broken.
241 if check_man "$mann" "$catn"; then
243 elif check_man "$man0" "$cat0"; then
245 elif check_cat "$catn"; then
247 elif check_cat "$cat0"; then
254 # Usage: is_newer file1 file2
255 # Returns true if file1 is newer than file2 as calculated by mtime.
257 if ! [ "$1" -ot "$2" ]; then
258 decho " mtime: $1 not older than $2" 3
261 decho " mtime: $1 older than $2" 3
266 # Usage: manpath_parse_args "$@"
267 # Parses commandline options for manpath.
268 manpath_parse_args() {
272 while getopts 'Ldq' cmd_arg; do
275 d) debug=$(( $debug + 1 )) ;;
282 # Usage: manpath_usage
283 # Display usage for the manpath(1) utility.
285 echo 'usage: manpath [-Ldq]' >&2
289 # Usage: manpath_warnings
290 # Display some warnings to stderr.
292 if [ -n "$Lflag" -a -n "$MANLOCALES" ]; then
293 echo "(Warning: MANLOCALES environment variable set)" >&2
297 # Usage: man_check_for_so page path
298 # Returns: True if able to resolve the file, false if it ended in tears.
299 # Detects the presence of the .so directive and causes the file to be
300 # redirected to another source file.
305 if [ -n "$catpage" ]; then
309 # We need to loop to accommodate multiple .so directives.
312 line=$($cattool $manpage | head -1)
314 .so*) trim "${line#.so}"
315 decho "$manpage includes $tstr"
316 # Glob and check for the file.
317 if ! check_man "$path/$tstr*" ""; then
318 decho " Unable to find $tstr"
329 # Usage: man_display_page
330 # Display either the manpage or catpage depending on the use_cat variable
332 local IFS pipeline testline
334 # We are called with IFS set to colon. This causes really weird
335 # things to happen for the variables that have spaces in them.
338 # If we are supposed to use a catpage and we aren't using troff(1)
339 # just zcat the catpage and we are done.
340 if [ -z "$tflag" -a -n "$use_cat" ]; then
341 if [ -n "$wflag" ]; then
342 echo "$catpage (source: $manpage)"
345 if [ $debug -gt 0 ]; then
346 decho "Command: $cattool $catpage | $MANPAGER"
349 eval "$cattool $catpage | $MANPAGER"
356 # Okay, we are using the manpage, do we just need to output the
357 # name of the manpage?
358 if [ -n "$wflag" ]; then
364 if [ -n "$use_width" ]; then
365 mandoc_args="-O width=${use_width}"
367 testline="mandoc -Tlint -Wunsupp >/dev/null 2>&1"
368 if [ -n "$tflag" ]; then
369 pipeline="mandoc -Tps $mandoc_args"
371 pipeline="mandoc $mandoc_args | $MANPAGER"
374 if ! eval "$cattool $manpage | $testline" ;then
375 if which -s groff; then
376 man_display_page_groff
378 echo "This manpage needs groff(1) to be rendered" >&2
379 echo "First install groff(1): " >&2
380 echo "pkg install groff " >&2
386 if [ $debug -gt 0 ]; then
387 decho "Command: $cattool $manpage | $pipeline"
390 eval "$cattool $manpage | $pipeline"
395 # Usage: man_display_page_groff
396 # Display the manpage using groff
397 man_display_page_groff() {
398 local EQN NROFF PIC TBL TROFF REFER VGRIND
399 local IFS l nroff_dev pipeline preproc_arg tool
401 # So, we really do need to parse the manpage. First, figure out the
402 # device flag (-T) we have to pass to eqn(1) and groff(1). Then,
403 # setup the pipeline of commands based on the user's request.
405 # If the manpage is from a particular charset, we need to setup nroff
406 # to properly output for the correct device.
409 # I don't pretend to know this; I'm just copying from the
410 # previous version of man(1).
411 case "$man_charset" in
412 KOI8-R) nroff_dev="koi8-r" ;;
413 ISO8859-1) nroff_dev="latin1" ;;
414 ISO8859-15) nroff_dev="latin1" ;;
415 UTF-8) nroff_dev="utf8" ;;
416 *) nroff_dev="ascii" ;;
419 NROFF="$NROFF -T$nroff_dev"
420 EQN="$EQN -T$nroff_dev"
422 # Iff the manpage is from the locale and not just the charset,
423 # then we need to define the locale string.
425 */${man_lang}_${man_country}.${man_charset}/*)
426 NROFF="$NROFF -dlocale=$man_lang.$man_charset"
428 */${man_lang}.${man_charset}/*)
429 NROFF="$NROFF -dlocale=$man_lang.$man_charset"
433 # Allow language specific calls to override the default
435 l=$(echo $man_lang | tr [:lower:] [:upper:])
436 for tool in EQN NROFF PIC TBL TROFF REFER VGRIND; do
437 eval "$tool=\${${tool}_$l:-\$$tool}"
440 *) NROFF="$NROFF -Tascii"
445 if [ -z "$MANCOLOR" ]; then
449 if [ -n "${use_width}" ]; then
450 NROFF="$NROFF -rLL=${use_width}n -rLT=${use_width}n"
453 if [ -n "$MANROFFSEQ" ]; then
456 while getopts 'egprtv' preproc_arg; do
457 case "${preproc_arg}" in
458 e) pipeline="$pipeline | $EQN" ;;
459 g) ;; # Ignore for compatibility.
460 p) pipeline="$pipeline | $PIC" ;;
461 r) pipeline="$pipeline | $REFER" ;;
462 t) pipeline="$pipeline | $TBL" ;;
463 v) pipeline="$pipeline | $VGRIND" ;;
467 # Strip the leading " | " from the resulting pipeline.
468 pipeline="${pipeline#" | "}"
473 if [ -n "$tflag" ]; then
474 pipeline="$pipeline | $TROFF"
476 pipeline="$pipeline | $NROFF | $MANPAGER"
479 if [ $debug -gt 0 ]; then
480 decho "Command: $cattool $manpage | $pipeline"
483 eval "$cattool $manpage | $pipeline"
488 # Usage: man_find_and_display page
489 # Search through the manpaths looking for the given page.
490 man_find_and_display() {
491 local found_page locpath p path sect
493 # Check to see if it's a file. But only if it has a '/' in
496 */*) if [ -f "$1" -a -r "$1" ]; then
497 decho "Found a usable page, displaying that"
500 setup_cattool $manpage
501 if man_check_for_so $manpage $(dirname $manpage); then
511 for sect in $MANSECT; do
512 decho "Searching section $sect" 2
513 for path in $MANPATH; do
514 for locpath in $locpaths; do
516 p=${p%/.} # Rid ourselves of the trailing /.
518 # Check if there is a MACHINE specific manpath.
519 if find_file $p $sect $MACHINE "$1"; then
520 if man_check_for_so $manpage $p; then
523 if [ -n "$aflag" ]; then
531 # Check if there is a MACHINE_ARCH
533 if find_file $p $sect $MACHINE_ARCH "$1"; then
534 if man_check_for_so $manpage $p; then
537 if [ -n "$aflag" ]; then
545 # Check plain old manpath.
546 if find_file $p $sect '' "$1"; then
547 if man_check_for_so $manpage $p; then
550 if [ -n "$aflag" ]; then
562 # Nothing? Well, we are done then.
563 if [ -z "$found_page" ]; then
564 echo "No manual entry for $1" >&2
570 # Usage: man_parse_opts "$@"
571 # Parses commandline options for man.
576 while getopts 'K:M:P:S:adfhkm:op:tw' cmd_arg; do
580 M) MANPATH=$OPTARG ;;
581 P) MANPAGER=$OPTARG ;;
582 S) MANSECT=$OPTARG ;;
584 d) debug=$(( $debug + 1 )) ;;
590 p) MANROFFSEQ=$OPTARG ;;
597 shift $(( $OPTIND - 1 ))
599 # Check the args for incompatible options.
601 case "${Kflag}${fflag}${kflag}${tflag}${wflag}" in
602 Kflagfflag*) echo "Incompatible options: -K and -f"; man_usage ;;
603 Kflag*kflag*) echo "Incompatible options: -K and -k"; man_usage ;;
604 Kflag*tflag) echo "Incompatible options: -K and -t"; man_usage ;;
605 fflagkflag*) echo "Incompatible options: -f and -k"; man_usage ;;
606 fflag*tflag*) echo "Incompatible options: -f and -t"; man_usage ;;
607 fflag*wflag) echo "Incompatible options: -f and -w"; man_usage ;;
608 *kflagtflag*) echo "Incompatible options: -k and -t"; man_usage ;;
609 *kflag*wflag) echo "Incompatible options: -k and -w"; man_usage ;;
610 *tflagwflag) echo "Incompatible options: -t and -w"; man_usage ;;
613 # Short circuit for whatis(1) and apropos(1)
614 if [ -n "$fflag" ]; then
619 if [ -n "$kflag" ]; then
626 # Setup various trivial but essential variables.
628 # Setup machine and architecture variables.
629 if [ -n "$mflag" ]; then
630 MACHINE_ARCH=${mflag%%:*}
633 if [ -z "$MACHINE_ARCH" ]; then
634 MACHINE_ARCH=$($SYSCTL -n hw.machine_arch)
636 if [ -z "$MACHINE" ]; then
637 MACHINE=$($SYSCTL -n hw.machine)
639 decho "Using architecture: $MACHINE_ARCH:$MACHINE"
648 # Usage: man_setup_width
656 if [ "$MANWIDTH" -gt 0 2>/dev/null ]; then
661 if { sizes=$($STTY size 0>&3 2>/dev/null); } 3>&1; then
663 if [ $2 -gt 80 ]; then
669 if [ -n "$use_width" ]; then
670 decho "Using non-standard page width: ${use_width}"
672 decho 'Using standard page width'
676 # Usage: man_setup_locale
677 # Setup necessary locale variables.
683 man_charset='US-ASCII'
685 # Setup locale information.
686 if [ -n "$oflag" ]; then
687 decho 'Using non-localized manpages'
689 # Use the locale tool to give us proper locale information
692 if [ -n "$LANG" ]; then
702 [a-z][a-z]_[A-Z][A-Z]\.*)
703 lang_cc="${locstr%.*}"
704 man_lang="${locstr%_*}"
705 man_country="${lang_cc#*_}"
706 man_charset="${locstr#*.}"
708 locpaths="$locpaths:$man_lang.$man_charset"
709 if [ "$man_lang" != "en" ]; then
710 locpaths="$locpaths:en.$man_charset"
712 locpaths="$locpaths:."
714 *) echo 'Unknown locale, assuming C' >&2
719 decho "Using locale paths: $locpaths"
722 # Usage: man_usage [exitcode]
723 # Display usage for the man utility.
726 echo ' man [-adho] [-t | -w] [-K regexp] [-M manpath] [-P pager] [-S mansect]'
727 echo ' [-m arch[:machine]] [-p [eprtv]] [mansect] page [...]'
728 echo ' man -f page [...] -- Emulates whatis(1)'
729 echo ' man -k page [...] -- Emulates apropos(1)'
731 # When exit'ing with -h, it's not an error.
735 # Usage: parse_configs
736 # Reads the end-user adjustable config files.
740 if [ -n "$parsed_configs" ]; then
746 # Read the global config first in case the user wants
747 # to override config_local.
748 if [ -r "$config_global" ]; then
749 parse_file "$config_global"
752 # Glob the list of files to parse.
754 files=$(echo $config_local)
757 for file in $files; do
758 if [ -r "$file" ]; then
766 # Usage: parse_file file
767 # Reads the specified config files.
769 local file line tstr var
772 decho "Parsing config file: $file"
776 \#*) decho " Comment" 3
778 MANPATH*) decho " MANPATH" 3
779 trim "${line#MANPATH}"
780 add_to_manpath "$tstr"
782 MANLOCALE*) decho " MANLOCALE" 3
783 trim "${line#MANLOCALE}"
784 manlocales="$manlocales:$tstr"
786 MANCONFIG*) decho " MANCONFIG" 3
787 trim "${line#MANCONFIG}"
790 MANSECT*) decho " MANSECT" 3
791 trim "${line#MANSECT}"
792 mansect="$mansect:$tstr"
794 # Set variables in the form of FOO_BAR
795 *_*[\ \ ]*) var="${line%%[\ \ ]*}"
797 eval "$var=\"$tstr\""
798 decho " Parsed $var" 3
805 # Traverse $PATH looking for manpaths.
809 decho "Searching PATH for man directories"
812 for path in $PATH; do
813 if add_to_manpath "$path/man"; then
815 elif add_to_manpath "$path/MAN"; then
819 */bin) p="${path%/bin}/share/man"
829 if [ -z "$manpath" ]; then
830 decho ' Unable to find any manpaths, using default'
831 manpath=$man_default_path
835 # Usage: search_whatis cmd [arglist]
836 # Do the heavy lifting for apropos/whatis
838 local IFS bad cmd f good key keywords loc opt out path rval wlist
843 whatis_parse_args "$@"
849 if [ "$cmd" = "whatis" ]; then
856 for path in $MANPATH; do
857 if [ \! -d "$path" ]; then
858 decho "Skipping non-existent path: $path" 2
862 if [ -f "$path/$f" -a -r "$path/$f" ]; then
863 decho "Found whatis: $path/$f"
864 wlist="$wlist $path/$f"
867 for loc in $MANLOCALES; do
868 if [ -f "$path/$loc/$f" -a -r "$path/$loc/$f" ]; then
869 decho "Found whatis: $path/$loc/$f"
870 wlist="$wlist $path/$loc/$f"
876 if [ -z "$wlist" ]; then
877 echo "$cmd: no whatis databases in $MANPATH" >&2
882 for key in $keywords; do
883 out=$(grep -Ehi $opt -- "$key" $wlist)
884 if [ -n "$out" ]; then
887 bad="$bad\\n$key: nothing appropriate"
892 # Strip leading carriage return.
896 if [ -n "$good" ]; then
897 echo -e "$good" | $MANPAGER
900 if [ -n "$bad" ]; then
907 # Usage: setup_cattool page
908 # Finds an appropriate decompressor based on extension
911 *.bz) cattool='/usr/bin/bzcat' ;;
912 *.bz2) cattool='/usr/bin/bzcat' ;;
913 *.gz) cattool='/usr/bin/gzcat' ;;
914 *.lzma) cattool='/usr/bin/lzcat' ;;
915 *.xz) cattool='/usr/bin/xzcat' ;;
916 *.zst) cattool='/usr/bin/zstdcat' ;;
917 *) cattool='/usr/bin/zcat -f' ;;
922 # Correctly sets $MANPAGER
925 if [ -z "$MANPAGER" ]; then
926 if [ -n "$MANCOLOR" ]; then
929 if [ -n "$PAGER" ]; then
936 decho "Using pager: $MANPAGER"
940 # Trims whitespace from beginning and end of a variable
945 [\ \ ]*) tstr="${tstr##[\ \ ]}" ;;
946 *[\ \ ]) tstr="${tstr%%[\ \ ]}" ;;
952 # Usage: whatis_parse_args "$@"
953 # Parse commandline args for whatis and apropos.
954 whatis_parse_args() {
957 while getopts 'd' cmd_arg; do
959 d) debug=$(( $debug + 1 )) ;;
964 shift $(( $OPTIND - 1 ))
969 # Usage: whatis_usage
970 # Display usage for the whatis/apropos utility.
972 echo "usage: $cmd [-d] keyword [...]"
980 [ $(stat -f %i /usr/bin/man) -ne $(stat -f %i /usr/bin/apropos) ] && \
982 search_whatis apropos "$@"
985 # Usage: do_full_search reg_exp
986 # Do a full search of the regular expression passed
987 # as parameter in all man pages
992 # Build grep(1) flags
995 # wflag implies -l for grep(1)
996 if [ -n "$wflag" ]; then
997 gflags="${gflags} -l"
1000 gflags="${gflags} --label"
1003 for mpath in $(echo "${MANPATH}" | tr : '[:blank:]'); do
1004 for section in $(echo "${MANSECT}" | tr : '[:blank:]'); do
1005 for manfile in ${mpath}/man${section}/*.${section}*; do
1006 mandoc "${manfile}" 2>/dev/null |
1007 grep -E ${gflags} "${manfile}" -e "${re}"
1020 shift $(( $OPTIND - 1 ))
1022 for sect in $MANSECT; do
1023 if [ "$sect" = "$1" ]; then
1024 decho "Detected manual section as first arg: $1"
1033 if [ -z "$pages" -a -z "${Kflag}" ]; then
1034 echo 'What manual page do you want?' >&2
1038 if [ ! -z "${Kflag}" ]; then
1039 # Short circuit because -K flag does a sufficiently
1040 # different thing like not showing the man page at all
1041 do_full_search "${REGEXP}"
1044 for page in $pages; do
1045 decho "Searching for $page"
1046 man_find_and_display "$page"
1053 manpath_parse_args "$@"
1054 if [ -z "$qflag" ]; then
1057 if [ -n "$Lflag" ]; then
1068 [ $(stat -f %i /usr/bin/man) -ne $(stat -f %i /usr/bin/whatis) ] && \
1070 search_whatis whatis "$@"
1073 # User's PATH setting decides on the groff-suite to pick up.
1075 NROFF='groff -S -P-h -Wall -mtty-char -man'
1079 TROFF='groff -S -man'
1082 LOCALE=/usr/bin/locale
1087 man_default_sections='1:8:2:3:3lua:n:4:5:6:7:9:l'
1088 man_default_path='/usr/share/man:/usr/share/openssl/man:/usr/local/share/man:/usr/local/man'
1089 cattool='/usr/bin/zcat -f'
1091 config_global='/etc/man.conf'
1093 # This can be overridden via a setting in /etc/man.conf.
1094 config_local='/usr/local/etc/man.d/*.conf'
1096 # Set noglobbing for now. I don't want spurious globbing.
1100 *apropos) do_apropos "$@" ;;
1101 *manpath) do_manpath "$@" ;;
1102 *whatis) do_whatis "$@" ;;