3 # SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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_manpath
73 # Builds a correct MANPATH variable.
77 # If the user has set a manpath, who are we to argue.
78 if [ -n "$MANPATH" ]; then
80 *:) PREPEND_MANPATH=${MANPATH} ;;
81 :*) APPEND_MANPATH=${MANPATH} ;;
83 PREPEND_MANPATH=${MANPATH%%::*}
84 APPEND_MANPATH=${MANPATH#*::}
90 if [ -n "$PREPEND_MANPATH" ]; then
92 for path in $PREPEND_MANPATH; do
93 add_to_manpath "$path"
100 decho "Adding default manpath entries"
102 for path in $man_default_path; do
103 add_to_manpath "$path"
109 if [ -n "$APPEND_MANPATH" ]; then
111 for path in $APPEND_MANPATH; do
112 add_to_manpath "$path"
119 decho "Using manual path: $MANPATH"
122 # Usage: check_cat catglob
123 # Checks to see if a cat glob is available.
128 setup_cattool $catpage
129 decho " Found catpage $catpage"
136 # Usage: check_man manglob catglob
137 # Given 2 globs, figures out if the manglob is available, if so, check to
138 # see if the catglob is also available and up to date.
141 # We have a match, check for a cat page
143 setup_cattool $manpage
144 decho " Found manpage $manpage"
146 if [ -n "${use_width}" ]; then
149 decho " Skipping catpage: non-standard page width"
150 elif exists "$2" && is_newer $found $manpage; then
151 # cat page found and is newer, use that
154 setup_cattool $catpage
155 decho " Using catpage $catpage"
157 # no cat page or is older
159 decho " Skipping catpage: not found or old"
167 # Usage: decho "string" [debuglevel]
168 # Echoes to stderr string prefaced with -- if high enough debuglevel.
170 if [ $debug -ge ${2:-1} ]; then
176 # Returns true if glob resolves to a real file.
180 # Don't accidentally inherit callers IFS (breaks perl manpages)
183 # Use some globbing tricks in the shell to determine if a file
189 if [ "$1" != "$2" -a -r "$2" ]; then
197 # Usage: find_file path section subdir pagename
198 # Returns: true if something is matched and found.
199 # Search the given path/section combo for a given page.
201 local manroot catroot mann man0 catn cat0
206 manroot="$manroot/$3"
207 catroot="$catroot/$3"
210 if [ ! -d "$manroot" -a ! -d "$catroot" ]; then
213 decho " Searching directory $manroot" 2
215 mann="$manroot/$4.$2*"
216 man0="$manroot/$4.0*"
217 catn="$catroot/$4.$2*"
218 cat0="$catroot/$4.0*"
220 # This is the behavior as seen by the original man utility.
221 # Let's not change that which doesn't seem broken.
222 if check_man "$mann" "$catn"; then
224 elif check_man "$man0" "$cat0"; then
226 elif check_cat "$catn"; then
228 elif check_cat "$cat0"; then
235 # Usage: is_newer file1 file2
236 # Returns true if file1 is newer than file2 as calculated by mtime.
238 if ! [ "$1" -ot "$2" ]; then
239 decho " mtime: $1 not older than $2" 3
242 decho " mtime: $1 older than $2" 3
247 # Usage: manpath_parse_args "$@"
248 # Parses commandline options for manpath.
249 manpath_parse_args() {
253 while getopts 'Ldq' cmd_arg; do
256 d) debug=$(( $debug + 1 )) ;;
263 # Usage: manpath_usage
264 # Display usage for the manpath(1) utility.
266 echo 'usage: manpath [-Ldq]' >&2
270 # Usage: manpath_warnings
271 # Display some warnings to stderr.
273 if [ -n "$Lflag" -a -n "$MANLOCALES" ]; then
274 echo "(Warning: MANLOCALES environment variable set)" >&2
278 # Usage: man_check_for_so page path
279 # Returns: True if able to resolve the file, false if it ended in tears.
280 # Detects the presence of the .so directive and causes the file to be
281 # redirected to another source file.
286 if [ -n "$catpage" ]; then
290 # We need to loop to accommodate multiple .so directives.
293 line=$($cattool $manpage | head -1)
295 .so*) trim "${line#.so}"
296 decho "$manpage includes $tstr"
297 # Glob and check for the file.
298 if ! check_man "$path/$tstr*" ""; then
299 decho " Unable to find $tstr"
310 # Usage: man_display_page
311 # Display either the manpage or catpage depending on the use_cat variable
313 local IFS pipeline testline
315 # We are called with IFS set to colon. This causes really weird
316 # things to happen for the variables that have spaces in them.
319 # If we are supposed to use a catpage and we aren't using troff(1)
320 # just zcat the catpage and we are done.
321 if [ -z "$tflag" -a -n "$use_cat" ]; then
322 if [ -n "$wflag" ]; then
323 echo "$catpage (source: $manpage)"
326 if [ $debug -gt 0 ]; then
327 decho "Command: $cattool $catpage | $MANPAGER"
330 eval "$cattool $catpage | $MANPAGER"
337 # Okay, we are using the manpage, do we just need to output the
338 # name of the manpage?
339 if [ -n "$wflag" ]; then
345 if [ -n "$use_width" ]; then
346 mandoc_args="-O width=${use_width}"
348 testline="mandoc -Tlint -Wunsupp >/dev/null 2>&1"
349 if [ -n "$tflag" ]; then
350 pipeline="mandoc -Tps $mandoc_args"
352 pipeline="mandoc $mandoc_args | $MANPAGER"
355 if ! eval "$cattool $manpage | $testline" ;then
356 if which -s groff; then
357 man_display_page_groff
359 echo "This manpage needs groff(1) to be rendered" >&2
360 echo "First install groff(1): " >&2
361 echo "pkg install groff " >&2
367 if [ $debug -gt 0 ]; then
368 decho "Command: $cattool $manpage | $pipeline"
371 eval "$cattool $manpage | $pipeline"
376 # Usage: man_display_page_groff
377 # Display the manpage using groff
378 man_display_page_groff() {
379 local EQN NROFF PIC TBL TROFF REFER VGRIND
380 local IFS l nroff_dev pipeline preproc_arg tool
382 # So, we really do need to parse the manpage. First, figure out the
383 # device flag (-T) we have to pass to eqn(1) and groff(1). Then,
384 # setup the pipeline of commands based on the user's request.
386 # If the manpage is from a particular charset, we need to setup nroff
387 # to properly output for the correct device.
390 # I don't pretend to know this; I'm just copying from the
391 # previous version of man(1).
392 case "$man_charset" in
393 KOI8-R) nroff_dev="koi8-r" ;;
394 ISO8859-1) nroff_dev="latin1" ;;
395 ISO8859-15) nroff_dev="latin1" ;;
396 UTF-8) nroff_dev="utf8" ;;
397 *) nroff_dev="ascii" ;;
400 NROFF="$NROFF -T$nroff_dev"
401 EQN="$EQN -T$nroff_dev"
403 # Iff the manpage is from the locale and not just the charset,
404 # then we need to define the locale string.
406 */${man_lang}_${man_country}.${man_charset}/*)
407 NROFF="$NROFF -dlocale=$man_lang.$man_charset"
409 */${man_lang}.${man_charset}/*)
410 NROFF="$NROFF -dlocale=$man_lang.$man_charset"
414 # Allow language specific calls to override the default
416 l=$(echo $man_lang | tr [:lower:] [:upper:])
417 for tool in EQN NROFF PIC TBL TROFF REFER VGRIND; do
418 eval "$tool=\${${tool}_$l:-\$$tool}"
421 *) NROFF="$NROFF -Tascii"
426 if [ -z "$MANCOLOR" ]; then
430 if [ -n "${use_width}" ]; then
431 NROFF="$NROFF -rLL=${use_width}n -rLT=${use_width}n"
434 if [ -n "$MANROFFSEQ" ]; then
437 while getopts 'egprtv' preproc_arg; do
438 case "${preproc_arg}" in
439 e) pipeline="$pipeline | $EQN" ;;
440 g) ;; # Ignore for compatibility.
441 p) pipeline="$pipeline | $PIC" ;;
442 r) pipeline="$pipeline | $REFER" ;;
443 t) pipeline="$pipeline | $TBL" ;;
444 v) pipeline="$pipeline | $VGRIND" ;;
448 # Strip the leading " | " from the resulting pipeline.
449 pipeline="${pipeline#" | "}"
454 if [ -n "$tflag" ]; then
455 pipeline="$pipeline | $TROFF"
457 pipeline="$pipeline | $NROFF | $MANPAGER"
460 if [ $debug -gt 0 ]; then
461 decho "Command: $cattool $manpage | $pipeline"
464 eval "$cattool $manpage | $pipeline"
469 # Usage: man_find_and_display page
470 # Search through the manpaths looking for the given page.
471 man_find_and_display() {
472 local found_page locpath p path sect
474 # Check to see if it's a file. But only if it has a '/' in
477 */*) if [ -f "$1" -a -r "$1" ]; then
478 decho "Found a usable page, displaying that"
481 setup_cattool $manpage
482 if man_check_for_so $manpage $(dirname $manpage); then
492 for sect in $MANSECT; do
493 decho "Searching section $sect" 2
494 for path in $MANPATH; do
495 for locpath in $locpaths; do
497 p=${p%/.} # Rid ourselves of the trailing /.
499 # Check if there is a MACHINE specific manpath.
500 if find_file $p $sect $MACHINE "$1"; then
501 if man_check_for_so $manpage $p; then
504 if [ -n "$aflag" ]; then
512 # Check if there is a MACHINE_ARCH
514 if find_file $p $sect $MACHINE_ARCH "$1"; then
515 if man_check_for_so $manpage $p; then
518 if [ -n "$aflag" ]; then
526 # Check plain old manpath.
527 if find_file $p $sect '' "$1"; then
528 if man_check_for_so $manpage $p; then
531 if [ -n "$aflag" ]; then
543 # Nothing? Well, we are done then.
544 if [ -z "$found_page" ]; then
545 echo "No manual entry for $1" >&2
551 # Usage: man_parse_args "$@"
552 # Parses commandline options for man.
557 while getopts 'K:M:P:S:adfhkm:op:tw' cmd_arg; do
561 M) MANPATH=$OPTARG ;;
562 P) MANPAGER=$OPTARG ;;
563 S) MANSECT=$OPTARG ;;
565 d) debug=$(( $debug + 1 )) ;;
571 p) MANROFFSEQ=$OPTARG ;;
578 shift $(( $OPTIND - 1 ))
580 # Check the args for incompatible options.
582 case "${Kflag}${fflag}${kflag}${tflag}${wflag}" in
583 Kflagfflag*) echo "Incompatible options: -K and -f"; man_usage ;;
584 Kflag*kflag*) echo "Incompatible options: -K and -k"; man_usage ;;
585 Kflag*tflag) echo "Incompatible options: -K and -t"; man_usage ;;
586 fflagkflag*) echo "Incompatible options: -f and -k"; man_usage ;;
587 fflag*tflag*) echo "Incompatible options: -f and -t"; man_usage ;;
588 fflag*wflag) echo "Incompatible options: -f and -w"; man_usage ;;
589 *kflagtflag*) echo "Incompatible options: -k and -t"; man_usage ;;
590 *kflag*wflag) echo "Incompatible options: -k and -w"; man_usage ;;
591 *tflagwflag) echo "Incompatible options: -t and -w"; man_usage ;;
594 # Short circuit for whatis(1) and apropos(1)
595 if [ -n "$fflag" ]; then
600 if [ -n "$kflag" ]; then
606 for sect in $man_default_sections; do
607 if [ "$sect" = "$1" ]; then
608 decho "Detected manual section as first arg: $1"
620 # Setup various trivial but essential variables.
622 # Setup machine and architecture variables.
623 if [ -n "$mflag" ]; then
624 MACHINE_ARCH=${mflag%%:*}
627 if [ -z "$MACHINE_ARCH" ]; then
628 MACHINE_ARCH=$($SYSCTL -n hw.machine_arch)
630 if [ -z "$MACHINE" ]; then
631 MACHINE=$($SYSCTL -n hw.machine)
633 decho "Using architecture: $MACHINE_ARCH:$MACHINE"
637 # Setup manual sections to search.
638 if [ -z "$MANSECT" ]; then
639 MANSECT=$man_default_sections
641 decho "Using manual sections: $MANSECT"
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 # Set variables in the form of FOO_BAR
791 *_*[\ \ ]*) var="${line%%[\ \ ]*}"
793 eval "$var=\"$tstr\""
794 decho " Parsed $var" 3
801 # Traverse $PATH looking for manpaths.
805 decho "Searching PATH for man directories"
808 for path in $PATH; do
809 if add_to_manpath "$path/man"; then
811 elif add_to_manpath "$path/MAN"; then
815 */bin) p="${path%/bin}/share/man"
825 if [ -z "$manpath" ]; then
826 decho ' Unable to find any manpaths, using default'
827 manpath=$man_default_path
831 # Usage: search_whatis cmd [arglist]
832 # Do the heavy lifting for apropos/whatis
834 local IFS bad cmd f good key keywords loc opt out path rval wlist
839 whatis_parse_args "$@"
845 if [ "$cmd" = "whatis" ]; then
852 for path in $MANPATH; do
853 if [ \! -d "$path" ]; then
854 decho "Skipping non-existent path: $path" 2
858 if [ -f "$path/$f" -a -r "$path/$f" ]; then
859 decho "Found whatis: $path/$f"
860 wlist="$wlist $path/$f"
863 for loc in $MANLOCALES; do
864 if [ -f "$path/$loc/$f" -a -r "$path/$loc/$f" ]; then
865 decho "Found whatis: $path/$loc/$f"
866 wlist="$wlist $path/$loc/$f"
872 if [ -z "$wlist" ]; then
873 echo "$cmd: no whatis databases in $MANPATH" >&2
878 for key in $keywords; do
879 out=$(grep -Ehi $opt -- "$key" $wlist)
880 if [ -n "$out" ]; then
883 bad="$bad\\n$key: nothing appropriate"
888 # Strip leading carriage return.
892 if [ -n "$good" ]; then
893 echo -e "$good" | $MANPAGER
896 if [ -n "$bad" ]; then
903 # Usage: setup_cattool page
904 # Finds an appropriate decompressor based on extension
907 *.bz) cattool='/usr/bin/bzcat' ;;
908 *.bz2) cattool='/usr/bin/bzcat' ;;
909 *.gz) cattool='/usr/bin/zcat' ;;
910 *.lzma) cattool='/usr/bin/lzcat' ;;
911 *.xz) cattool='/usr/bin/xzcat' ;;
912 *.zst) cattool='/usr/bin/zstdcat' ;;
913 *) cattool='/usr/bin/zcat -f' ;;
918 # Correctly sets $MANPAGER
921 if [ -z "$MANPAGER" ]; then
922 if [ -n "$MANCOLOR" ]; then
925 if [ -n "$PAGER" ]; then
932 decho "Using pager: $MANPAGER"
936 # Trims whitespace from beginning and end of a variable
941 [\ \ ]*) tstr="${tstr##[\ \ ]}" ;;
942 *[\ \ ]) tstr="${tstr%%[\ \ ]}" ;;
948 # Usage: whatis_parse_args "$@"
949 # Parse commandline args for whatis and apropos.
950 whatis_parse_args() {
953 while getopts 'd' cmd_arg; do
955 d) debug=$(( $debug + 1 )) ;;
960 shift $(( $OPTIND - 1 ))
965 # Usage: whatis_usage
966 # Display usage for the whatis/apropos utility.
968 echo "usage: $cmd [-d] keyword [...]"
976 [ $(stat -f %i /usr/bin/man) -ne $(stat -f %i /usr/bin/apropos) ] && \
978 search_whatis apropos "$@"
981 # Usage: do_full_search reg_exp
982 # Do a full search of the regular expression passed
983 # as parameter in all man pages
988 # Build grep(1) flags
991 # wflag implies -l for grep(1)
992 if [ -n "$wflag" ]; then
993 gflags="${gflags} -l"
996 gflags="${gflags} --label"
999 for mpath in $(echo "${MANPATH}" | tr : [:blank:]); do
1000 for section in $(echo "${MANSECT}" | tr : [:blank:]); do
1001 for manfile in ${mpath}/man${section}/*.${section}*; do
1002 mandoc "${manfile}" 2>/dev/null |
1003 grep -E ${gflags} "${manfile}" -e ${re}
1012 if [ -z "$pages" -a -z "${Kflag}" ]; then
1013 echo 'What manual page do you want?' >&2
1018 if [ ! -z "${Kflag}" ]; then
1019 # Short circuit because -K flag does a sufficiently
1020 # different thing like not showing the man page at all
1021 do_full_search "${REGEXP}"
1024 for page in $pages; do
1025 decho "Searching for $page"
1026 man_find_and_display "$page"
1033 manpath_parse_args "$@"
1034 if [ -z "$qflag" ]; then
1037 if [ -n "$Lflag" ]; then
1048 [ $(stat -f %i /usr/bin/man) -ne $(stat -f %i /usr/bin/whatis) ] && \
1050 search_whatis whatis "$@"
1053 # User's PATH setting decides on the groff-suite to pick up.
1055 NROFF='groff -S -P-h -Wall -mtty-char -man'
1059 TROFF='groff -S -man'
1062 LOCALE=/usr/bin/locale
1067 man_default_sections='1:8:2:3:3lua:n:4:5:6:7:9:l'
1068 man_default_path='/usr/share/man:/usr/share/openssl/man:/usr/local/share/man:/usr/local/man'
1069 cattool='/usr/bin/zcat -f'
1071 config_global='/etc/man.conf'
1073 # This can be overridden via a setting in /etc/man.conf.
1074 config_local='/usr/local/etc/man.d/*.conf'
1076 # Set noglobbing for now. I don't want spurious globbing.
1080 *apropos) do_apropos "$@" ;;
1081 *manpath) do_manpath "$@" ;;
1082 *whatis) do_whatis "$@" ;;