3 # Copyright (c) 2010 Gordon Tetlow
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
15 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 # Usage: add_to_manpath path
30 # Adds a variable to manpath while ensuring we don't have duplicates.
31 # Returns true if we were able to add something. False otherwise.
34 *:$1) decho " Skipping duplicate manpath entry $1" 2 ;;
35 $1:*) decho " Skipping duplicate manpath entry $1" 2 ;;
36 *:$1:*) decho " Skipping duplicate manpath entry $1" 2 ;;
37 *) if [ -d "$1" ]; then
38 decho " Adding $1 to manpath"
48 # Usage: build_manlocales
49 # Builds a correct MANLOCALES variable.
51 # If the user has set manlocales, who are we to argue.
52 if [ -n "$MANLOCALES" ]; then
59 MANLOCALES=${manlocales#:}
61 decho "Available manual locales: $MANLOCALES"
64 # Usage: build_manpath
65 # Builds a correct MANPATH variable.
69 # If the user has set a manpath, who are we to argue.
70 if [ -n "$MANPATH" ]; then
72 *:) PREPEND_MANPATH=${MANPATH} ;;
73 :*) APPEND_MANPATH=${MANPATH} ;;
75 PREPEND_MANPATH=${MANPATH%%::*}
76 APPEND_MANPATH=${MANPATH#*::}
82 if [ -n "$PREPEND_MANPATH" ]; then
84 for path in $PREPEND_MANPATH; do
85 add_to_manpath "$path"
92 decho "Adding default manpath entries"
94 for path in $man_default_path; do
95 add_to_manpath "$path"
101 if [ -n "$APPEND_MANPATH" ]; then
103 for path in $APPEND_MANPATH; do
104 add_to_manpath "$path"
111 decho "Using manual path: $MANPATH"
114 # Usage: check_cat catglob
115 # Checks to see if a cat glob is available.
120 setup_cattool $catpage
121 decho " Found catpage $catpage"
128 # Usage: check_man manglob catglob
129 # Given 2 globs, figures out if the manglob is available, if so, check to
130 # see if the catglob is also available and up to date.
133 # We have a match, check for a cat page
135 setup_cattool $manpage
136 decho " Found manpage $manpage"
138 if [ -n "${use_width}" ]; then
141 decho " Skipping catpage: non-standard page width"
142 elif exists "$2" && is_newer $found $manpage; then
143 # cat page found and is newer, use that
146 setup_cattool $catpage
147 decho " Using catpage $catpage"
149 # no cat page or is older
151 decho " Skipping catpage: not found or old"
159 # Usage: decho "string" [debuglevel]
160 # Echoes to stderr string prefaced with -- if high enough debuglevel.
162 if [ $debug -ge ${2:-1} ]; then
168 # Returns true if glob resolves to a real file.
172 # Don't accidentally inherit callers IFS (breaks perl manpages)
175 # Use some globbing tricks in the shell to determine if a file
181 if [ "$1" != "$2" -a -r "$2" ]; then
189 # Usage: find_file path section subdir pagename
190 # Returns: true if something is matched and found.
191 # Search the given path/section combo for a given page.
193 local manroot catroot mann man0 catn cat0
198 manroot="$manroot/$3"
199 catroot="$catroot/$3"
202 if [ ! -d "$manroot" ]; then
205 decho " Searching directory $manroot" 2
207 mann="$manroot/$4.$2*"
208 man0="$manroot/$4.0*"
209 catn="$catroot/$4.$2*"
210 cat0="$catroot/$4.0*"
212 # This is the behavior as seen by the original man utility.
213 # Let's not change that which doesn't seem broken.
214 if check_man "$mann" "$catn"; then
216 elif check_man "$man0" "$cat0"; then
218 elif check_cat "$catn"; then
220 elif check_cat "$cat0"; then
227 # Usage: is_newer file1 file2
228 # Returns true if file1 is newer than file2 as calculated by mtime.
230 if ! [ "$1" -ot "$2" ]; then
231 decho " mtime: $1 not older than $2" 3
234 decho " mtime: $1 older than $2" 3
239 # Usage: manpath_parse_args "$@"
240 # Parses commandline options for manpath.
241 manpath_parse_args() {
244 while getopts 'Ldq' cmd_arg; do
247 d) debug=$(( $debug + 1 )) ;;
254 # Usage: manpath_usage
255 # Display usage for the manpath(1) utility.
257 echo 'usage: manpath [-Ldq]' >&2
261 # Usage: manpath_warnings
262 # Display some warnings to stderr.
264 if [ -n "$Lflag" -a -n "$MANLOCALES" ]; then
265 echo "(Warning: MANLOCALES environment variable set)" >&2
269 # Usage: man_check_for_so page path
270 # Returns: True if able to resolve the file, false if it ended in tears.
271 # Detects the presence of the .so directive and causes the file to be
272 # redirected to another source file.
278 # We need to loop to accommodate multiple .so directives.
281 line=$($cattool $manpage | head -1)
283 .so*) trim "${line#.so}"
284 decho "$manpage includes $tstr"
285 # Glob and check for the file.
286 if ! check_man "$path/$tstr*" ""; then
287 decho " Unable to find $tstr"
298 # Usage: man_display_page
299 # Display either the manpage or catpage depending on the use_cat variable
301 local IFS pipeline testline
303 # We are called with IFS set to colon. This causes really weird
304 # things to happen for the variables that have spaces in them.
307 # If we are supposed to use a catpage and we aren't using troff(1)
308 # just zcat the catpage and we are done.
309 if [ -z "$tflag" -a -n "$use_cat" ]; then
310 if [ -n "$wflag" ]; then
311 echo "$catpage (source: $manpage)"
314 if [ $debug -gt 0 ]; then
315 decho "Command: $cattool $catpage | $MANPAGER"
318 eval "$cattool $catpage | $MANPAGER"
325 # Okay, we are using the manpage, do we just need to output the
326 # name of the manpage?
327 if [ -n "$wflag" ]; then
333 if [ -n "$use_width" ]; then
334 mandoc_args="-O width=${use_width}"
336 testline="mandoc -Tlint -Wunsupp 2>/dev/null"
337 if [ -n "$tflag" ]; then
338 pipeline="mandoc -Tps $mandoc_args"
340 pipeline="mandoc $mandoc_args | $MANPAGER"
343 if ! eval "$cattool $manpage | $testline" ;then
344 if which -s groff; then
345 man_display_page_groff
347 echo "This manpage needs groff(1) to be rendered" >&2
348 echo "First install groff(1): " >&2
349 echo "pkg install groff " >&2
355 if [ $debug -gt 0 ]; then
356 decho "Command: $cattool $manpage | $pipeline"
359 eval "$cattool $manpage | $pipeline"
364 # Usage: man_display_page_groff
365 # Display the manpage using groff
366 man_display_page_groff() {
367 local EQN NROFF PIC TBL TROFF REFER VGRIND
368 local IFS l nroff_dev pipeline preproc_arg tool
370 # So, we really do need to parse the manpage. First, figure out the
371 # device flag (-T) we have to pass to eqn(1) and groff(1). Then,
372 # setup the pipeline of commands based on the user's request.
374 # If the manpage is from a particular charset, we need to setup nroff
375 # to properly output for the correct device.
378 # I don't pretend to know this; I'm just copying from the
379 # previous version of man(1).
380 case "$man_charset" in
381 KOI8-R) nroff_dev="koi8-r" ;;
382 ISO8859-1) nroff_dev="latin1" ;;
383 ISO8859-15) nroff_dev="latin1" ;;
384 UTF-8) nroff_dev="utf8" ;;
385 *) nroff_dev="ascii" ;;
388 NROFF="$NROFF -T$nroff_dev"
389 EQN="$EQN -T$nroff_dev"
391 # Iff the manpage is from the locale and not just the charset,
392 # then we need to define the locale string.
394 */${man_lang}_${man_country}.${man_charset}/*)
395 NROFF="$NROFF -dlocale=$man_lang.$man_charset"
397 */${man_lang}.${man_charset}/*)
398 NROFF="$NROFF -dlocale=$man_lang.$man_charset"
402 # Allow language specific calls to override the default
404 l=$(echo $man_lang | tr [:lower:] [:upper:])
405 for tool in EQN NROFF PIC TBL TROFF REFER VGRIND; do
406 eval "$tool=\${${tool}_$l:-\$$tool}"
409 *) NROFF="$NROFF -Tascii"
414 if [ -z "$MANCOLOR" ]; then
418 if [ -n "${use_width}" ]; then
419 NROFF="$NROFF -rLL=${use_width}n -rLT=${use_width}n"
422 if [ -n "$MANROFFSEQ" ]; then
424 while getopts 'egprtv' preproc_arg; do
425 case "${preproc_arg}" in
426 e) pipeline="$pipeline | $EQN" ;;
427 g) ;; # Ignore for compatibility.
428 p) pipeline="$pipeline | $PIC" ;;
429 r) pipeline="$pipeline | $REFER" ;;
430 t) pipeline="$pipeline | $TBL" ;;
431 v) pipeline="$pipeline | $VGRIND" ;;
435 # Strip the leading " | " from the resulting pipeline.
436 pipeline="${pipeline#" | "}"
441 if [ -n "$tflag" ]; then
442 pipeline="$pipeline | $TROFF"
444 pipeline="$pipeline | $NROFF | $MANPAGER"
447 if [ $debug -gt 0 ]; then
448 decho "Command: $cattool $manpage | $pipeline"
451 eval "$cattool $manpage | $pipeline"
456 # Usage: man_find_and_display page
457 # Search through the manpaths looking for the given page.
458 man_find_and_display() {
459 local found_page locpath p path sect
461 # Check to see if it's a file. But only if it has a '/' in
464 */*) if [ -f "$1" -a -r "$1" ]; then
465 decho "Found a usable page, displaying that"
468 setup_cattool $manpage
469 if man_check_for_so $manpage $(dirname $manpage); then
479 for sect in $MANSECT; do
480 decho "Searching section $sect" 2
481 for path in $MANPATH; do
482 for locpath in $locpaths; do
484 p=${p%/.} # Rid ourselves of the trailing /.
486 # Check if there is a MACHINE specific manpath.
487 if find_file $p $sect $MACHINE "$1"; then
488 if man_check_for_so $manpage $p; then
491 if [ -n "$aflag" ]; then
499 # Check if there is a MACHINE_ARCH
501 if find_file $p $sect $MACHINE_ARCH "$1"; then
502 if man_check_for_so $manpage $p; then
505 if [ -n "$aflag" ]; then
513 # Check plain old manpath.
514 if find_file $p $sect '' "$1"; then
515 if man_check_for_so $manpage $p; then
518 if [ -n "$aflag" ]; then
530 # Nothing? Well, we are done then.
531 if [ -z "$found_page" ]; then
532 echo "No manual entry for $1" >&2
538 # Usage: man_parse_args "$@"
539 # Parses commandline options for man.
543 while getopts 'M:P:S:adfhkm:op:tw' cmd_arg; do
545 M) MANPATH=$OPTARG ;;
546 P) MANPAGER=$OPTARG ;;
547 S) MANSECT=$OPTARG ;;
549 d) debug=$(( $debug + 1 )) ;;
555 p) MANROFFSEQ=$OPTARG ;;
562 shift $(( $OPTIND - 1 ))
564 # Check the args for incompatible options.
565 case "${fflag}${kflag}${tflag}${wflag}" in
566 fflagkflag*) echo "Incompatible options: -f and -k"; man_usage ;;
567 fflag*tflag*) echo "Incompatible options: -f and -t"; man_usage ;;
568 fflag*wflag) echo "Incompatible options: -f and -w"; man_usage ;;
569 *kflagtflag*) echo "Incompatible options: -k and -t"; man_usage ;;
570 *kflag*wflag) echo "Incompatible options: -k and -w"; man_usage ;;
571 *tflagwflag) echo "Incompatible options: -t and -w"; man_usage ;;
574 # Short circuit for whatis(1) and apropos(1)
575 if [ -n "$fflag" ]; then
580 if [ -n "$kflag" ]; then
586 for sect in $man_default_sections; do
587 if [ "$sect" = "$1" ]; then
588 decho "Detected manual section as first arg: $1"
600 # Setup various trivial but essential variables.
602 # Setup machine and architecture variables.
603 if [ -n "$mflag" ]; then
604 MACHINE_ARCH=${mflag%%:*}
607 if [ -z "$MACHINE_ARCH" ]; then
608 MACHINE_ARCH=$($SYSCTL -n hw.machine_arch)
610 if [ -z "$MACHINE" ]; then
611 MACHINE=$($SYSCTL -n hw.machine)
613 decho "Using architecture: $MACHINE_ARCH:$MACHINE"
617 # Setup manual sections to search.
618 if [ -z "$MANSECT" ]; then
619 MANSECT=$man_default_sections
621 decho "Using manual sections: $MANSECT"
628 # Usage: man_setup_width
636 if [ "$MANWIDTH" -gt 0 2>/dev/null ]; then
641 if { sizes=$($STTY size 0>&3 2>/dev/null); } 3>&1; then
643 if [ $2 -gt 80 ]; then
649 if [ -n "$use_width" ]; then
650 decho "Using non-standard page width: ${use_width}"
652 decho 'Using standard page width'
656 # Usage: man_setup_locale
657 # Setup necessary locale variables.
662 man_charset='US-ASCII'
664 # Setup locale information.
665 if [ -n "$oflag" ]; then
666 decho 'Using non-localized manpages'
668 # Use the locale tool to give us the proper LC_CTYPE
674 [a-z][a-z]_[A-Z][A-Z]\.*)
675 lang_cc="${LC_CTYPE%.*}"
676 man_lang="${LC_CTYPE%_*}"
677 man_country="${lang_cc#*_}"
678 man_charset="${LC_CTYPE#*.}"
680 locpaths="$locpaths:$man_lang.$man_charset"
681 if [ "$man_lang" != "en" ]; then
682 locpaths="$locpaths:en.$man_charset"
684 locpaths="$locpaths:."
686 *) echo 'Unknown locale, assuming C' >&2
691 decho "Using locale paths: $locpaths"
694 # Usage: man_usage [exitcode]
695 # Display usage for the man utility.
698 echo ' man [-adho] [-t | -w] [-M manpath] [-P pager] [-S mansect]'
699 echo ' [-m arch[:machine]] [-p [eprtv]] [mansect] page [...]'
700 echo ' man -f page [...] -- Emulates whatis(1)'
701 echo ' man -k page [...] -- Emulates apropos(1)'
703 # When exit'ing with -h, it's not an error.
707 # Usage: parse_configs
708 # Reads the end-user adjustable config files.
712 if [ -n "$parsed_configs" ]; then
718 # Read the global config first in case the user wants
719 # to override config_local.
720 if [ -r "$config_global" ]; then
721 parse_file "$config_global"
724 # Glob the list of files to parse.
726 files=$(echo $config_local)
729 for file in $files; do
730 if [ -r "$file" ]; then
738 # Usage: parse_file file
739 # Reads the specified config files.
741 local file line tstr var
744 decho "Parsing config file: $file"
748 \#*) decho " Comment" 3
750 MANPATH*) decho " MANPATH" 3
751 trim "${line#MANPATH}"
752 add_to_manpath "$tstr"
754 MANLOCALE*) decho " MANLOCALE" 3
755 trim "${line#MANLOCALE}"
756 manlocales="$manlocales:$tstr"
758 MANCONFIG*) decho " MANCONFIG" 3
759 trim "${line#MANCONFIG}"
762 # Set variables in the form of FOO_BAR
763 *_*[\ \ ]*) var="${line%%[\ \ ]*}"
765 eval "$var=\"$tstr\""
766 decho " Parsed $var" 3
773 # Traverse $PATH looking for manpaths.
777 decho "Searching PATH for man directories"
780 for path in $PATH; do
781 # Do a little special casing since the base manpages
782 # are in /usr/share/man instead of /usr/man or /man.
784 /bin|/usr/bin) add_to_manpath "/usr/share/man" ;;
785 *) if add_to_manpath "$path/man"; then
787 elif add_to_manpath "$path/MAN"; then
791 */bin) p="${path%/bin}/man"
793 p="${path%/bin}/share/man"
804 if [ -z "$manpath" ]; then
805 decho ' Unable to find any manpaths, using default'
806 manpath=$man_default_path
810 # Usage: search_whatis cmd [arglist]
811 # Do the heavy lifting for apropos/whatis
813 local IFS bad cmd f good key keywords loc opt out path rval wlist
818 whatis_parse_args "$@"
824 if [ "$cmd" = "whatis" ]; then
831 for path in $MANPATH; do
832 if [ \! -d "$path" ]; then
833 decho "Skipping non-existent path: $path" 2
837 if [ -f "$path/$f" -a -r "$path/$f" ]; then
838 decho "Found whatis: $path/$f"
839 wlist="$wlist $path/$f"
842 for loc in $MANLOCALES; do
843 if [ -f "$path/$loc/$f" -a -r "$path/$loc/$f" ]; then
844 decho "Found whatis: $path/$loc/$f"
845 wlist="$wlist $path/$loc/$f"
851 if [ -z "$wlist" ]; then
852 echo "$cmd: no whatis databases in $MANPATH" >&2
857 for key in $keywords; do
858 out=$(grep -Ehi $opt -- "$key" $wlist)
859 if [ -n "$out" ]; then
862 bad="$bad\\n$key: nothing appropriate"
867 # Strip leading carriage return.
871 if [ -n "$good" ]; then
872 echo -e "$good" | $MANPAGER
875 if [ -n "$bad" ]; then
882 # Usage: setup_cattool page
883 # Finds an appropriate decompressor based on extension
886 *.bz) cattool='/usr/bin/bzcat' ;;
887 *.bz2) cattool='/usr/bin/bzcat' ;;
888 *.gz) cattool='/usr/bin/zcat' ;;
889 *.lzma) cattool='/usr/bin/lzcat' ;;
890 *.xz) cattool='/usr/bin/xzcat' ;;
891 *) cattool='/usr/bin/zcat -f' ;;
896 # Correctly sets $MANPAGER
899 if [ -z "$MANPAGER" ]; then
900 if [ -n "$MANCOLOR" ]; then
903 if [ -n "$PAGER" ]; then
910 decho "Using pager: $MANPAGER"
914 # Trims whitespace from beginning and end of a variable
919 [\ \ ]*) tstr="${tstr##[\ \ ]}" ;;
920 *[\ \ ]) tstr="${tstr%%[\ \ ]}" ;;
926 # Usage: whatis_parse_args "$@"
927 # Parse commandline args for whatis and apropos.
928 whatis_parse_args() {
930 while getopts 'd' cmd_arg; do
932 d) debug=$(( $debug + 1 )) ;;
937 shift $(( $OPTIND - 1 ))
942 # Usage: whatis_usage
943 # Display usage for the whatis/apropos utility.
945 echo "usage: $cmd [-d] keyword [...]"
953 [ $(stat -f %i /usr/bin/man) -ne $(stat -f %i /usr/bin/apropos) ] && \
955 search_whatis apropos "$@"
960 if [ -z "$pages" ]; then
961 echo 'What manual page do you want?' >&2
966 for page in $pages; do
967 decho "Searching for $page"
968 man_find_and_display "$page"
975 manpath_parse_args "$@"
976 if [ -z "$qflag" ]; then
979 if [ -n "$Lflag" ]; then
990 [ $(stat -f %i /usr/bin/man) -ne $(stat -f %i /usr/bin/whatis) ] && \
992 search_whatis whatis "$@"
995 # User's PATH setting decides on the groff-suite to pick up.
997 NROFF='groff -S -P-h -Wall -mtty-char -man'
1001 TROFF='groff -S -man'
1004 LOCALE=/usr/bin/locale
1009 man_default_sections='1:8:2:3:n:4:5:6:7:9:l'
1010 man_default_path='/usr/share/man:/usr/share/openssl/man:/usr/local/man'
1011 cattool='/usr/bin/zcat -f'
1013 config_global='/etc/man.conf'
1015 # This can be overridden via a setting in /etc/man.conf.
1016 config_local='/usr/local/etc/man.d/*.conf'
1018 # Set noglobbing for now. I don't want spurious globbing.
1022 *apropos) do_apropos "$@" ;;
1023 *manpath) do_manpath "$@" ;;
1024 *whatis) do_whatis "$@" ;;