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
76 decho "Adding default manpath entries"
78 for path in $man_default_path; do
79 add_to_manpath "$path"
88 decho "Using manual path: $MANPATH"
91 # Usage: check_cat catglob
92 # Checks to see if a cat glob is available.
97 setup_cattool $catpage
98 decho " Found catpage $catpage"
105 # Usage: check_man manglob catglob
106 # Given 2 globs, figures out if the manglob is available, if so, check to
107 # see if the catglob is also available and up to date.
110 # We have a match, check for a cat page
112 setup_cattool $manpage
113 decho " Found manpage $manpage"
115 if [ -n "${use_width}" ]; then
118 decho " Skipping catpage: non-standard page width"
119 elif exists "$2" && is_newer $found $manpage; then
120 # cat page found and is newer, use that
123 setup_cattool $catpage
124 decho " Using catpage $catpage"
126 # no cat page or is older
128 decho " Skipping catpage: not found or old"
136 # Usage: decho "string" [debuglevel]
137 # Echoes to stderr string prefaced with -- if high enough debuglevel.
139 if [ $debug -ge ${2:-1} ]; then
145 # Returns true if glob resolves to a real file.
149 # Don't accidentally inherit callers IFS (breaks perl manpages)
152 # Use some globbing tricks in the shell to determine if a file
158 if [ "$1" != "$2" -a -r "$2" ]; then
166 # Usage: find_file path section subdir pagename
167 # Returns: true if something is matched and found.
168 # Search the given path/section combo for a given page.
170 local manroot catroot mann man0 catn cat0
175 manroot="$manroot/$3"
176 catroot="$catroot/$3"
179 if [ ! -d "$manroot" ]; then
182 decho " Searching directory $manroot" 2
184 mann="$manroot/$4.$2*"
185 man0="$manroot/$4.0*"
186 catn="$catroot/$4.$2*"
187 cat0="$catroot/$4.0*"
189 # This is the behavior as seen by the original man utility.
190 # Let's not change that which doesn't seem broken.
191 if check_man "$mann" "$catn"; then
193 elif check_man "$man0" "$cat0"; then
195 elif check_cat "$catn"; then
197 elif check_cat "$cat0"; then
204 # Usage: is_newer file1 file2
205 # Returns true if file1 is newer than file2 as calculated by mtime.
207 if ! [ "$1" -ot "$2" ]; then
208 decho " mtime: $1 not older than $2" 3
211 decho " mtime: $1 older than $2" 3
216 # Usage: manpath_parse_args "$@"
217 # Parses commandline options for manpath.
218 manpath_parse_args() {
221 while getopts 'Ldq' cmd_arg; do
224 d) debug=$(( $debug + 1 )) ;;
231 # Usage: manpath_usage
232 # Display usage for the manpath(1) utility.
234 echo 'usage: manpath [-Ldq]' >&2
238 # Usage: manpath_warnings
239 # Display some warnings to stderr.
241 if [ -z "$Lflag" -a -n "$MANPATH" ]; then
242 echo "(Warning: MANPATH environment variable set)" >&2
245 if [ -n "$Lflag" -a -n "$MANLOCALES" ]; then
246 echo "(Warning: MANLOCALES environment variable set)" >&2
250 # Usage: man_check_for_so page path
251 # Returns: True if able to resolve the file, false if it ended in tears.
252 # Detects the presence of the .so directive and causes the file to be
253 # redirected to another source file.
259 # We need to loop to accommodate multiple .so directives.
262 line=$($cattool $manpage | head -1)
264 .so*) trim "${line#.so}"
265 decho "$manpage includes $tstr"
266 # Glob and check for the file.
267 if ! check_man "$path/$tstr*" ""; then
268 decho " Unable to find $tstr"
279 # Usage: man_display_page
280 # Display either the manpage or catpage depending on the use_cat variable
282 local IFS pipeline testline
284 # We are called with IFS set to colon. This causes really weird
285 # things to happen for the variables that have spaces in them.
288 # If we are supposed to use a catpage and we aren't using troff(1)
289 # just zcat the catpage and we are done.
290 if [ -z "$tflag" -a -n "$use_cat" ]; then
291 if [ -n "$wflag" ]; then
292 echo "$catpage (source: $manpage)"
295 if [ $debug -gt 0 ]; then
296 decho "Command: $cattool $catpage | $MANPAGER"
299 eval "$cattool $catpage | $MANPAGER"
306 # Okay, we are using the manpage, do we just need to output the
307 # name of the manpage?
308 if [ -n "$wflag" ]; then
314 if [ -n "$use_width" ]; then
315 mandoc_args="-O width=${use_width}"
317 testline="mandoc -Tlint -Wunsupp 2>/dev/null"
318 if [ -n "$tflag" ]; then
319 pipeline="mandoc -Tps $mandoc_args"
321 pipeline="mandoc $mandoc_args | $MANPAGER"
324 if ! eval "$cattool $manpage | $testline" ;then
325 if which -s groff; then
326 man_display_page_groff
328 echo "This manpage needs groff(1) to be rendered" >&2
329 echo "First install groff(1): " >&2
330 echo "pkg install groff " >&2
336 if [ $debug -gt 0 ]; then
337 decho "Command: $cattool $manpage | $pipeline"
340 eval "$cattool $manpage | $pipeline"
345 # Usage: man_display_page_groff
346 # Display the manpage using groff
347 man_display_page_groff() {
348 local EQN NROFF PIC TBL TROFF REFER VGRIND
349 local IFS l nroff_dev pipeline preproc_arg tool
351 # So, we really do need to parse the manpage. First, figure out the
352 # device flag (-T) we have to pass to eqn(1) and groff(1). Then,
353 # setup the pipeline of commands based on the user's request.
355 # If the manpage is from a particular charset, we need to setup nroff
356 # to properly output for the correct device.
359 # I don't pretend to know this; I'm just copying from the
360 # previous version of man(1).
361 case "$man_charset" in
362 KOI8-R) nroff_dev="koi8-r" ;;
363 ISO8859-1) nroff_dev="latin1" ;;
364 ISO8859-15) nroff_dev="latin1" ;;
365 UTF-8) nroff_dev="utf8" ;;
366 *) nroff_dev="ascii" ;;
369 NROFF="$NROFF -T$nroff_dev"
370 EQN="$EQN -T$nroff_dev"
372 # Iff the manpage is from the locale and not just the charset,
373 # then we need to define the locale string.
375 */${man_lang}_${man_country}.${man_charset}/*)
376 NROFF="$NROFF -dlocale=$man_lang.$man_charset"
378 */${man_lang}.${man_charset}/*)
379 NROFF="$NROFF -dlocale=$man_lang.$man_charset"
383 # Allow language specific calls to override the default
385 l=$(echo $man_lang | tr [:lower:] [:upper:])
386 for tool in EQN NROFF PIC TBL TROFF REFER VGRIND; do
387 eval "$tool=\${${tool}_$l:-\$$tool}"
390 *) NROFF="$NROFF -Tascii"
395 if [ -z "$MANCOLOR" ]; then
399 if [ -n "${use_width}" ]; then
400 NROFF="$NROFF -rLL=${use_width}n -rLT=${use_width}n"
403 if [ -n "$MANROFFSEQ" ]; then
405 while getopts 'egprtv' preproc_arg; do
406 case "${preproc_arg}" in
407 e) pipeline="$pipeline | $EQN" ;;
408 g) ;; # Ignore for compatibility.
409 p) pipeline="$pipeline | $PIC" ;;
410 r) pipeline="$pipeline | $REFER" ;;
411 t) pipeline="$pipeline | $TBL" ;;
412 v) pipeline="$pipeline | $VGRIND" ;;
416 # Strip the leading " | " from the resulting pipeline.
417 pipeline="${pipeline#" | "}"
422 if [ -n "$tflag" ]; then
423 pipeline="$pipeline | $TROFF"
425 pipeline="$pipeline | $NROFF | $MANPAGER"
428 if [ $debug -gt 0 ]; then
429 decho "Command: $cattool $manpage | $pipeline"
432 eval "$cattool $manpage | $pipeline"
437 # Usage: man_find_and_display page
438 # Search through the manpaths looking for the given page.
439 man_find_and_display() {
440 local found_page locpath p path sect
442 # Check to see if it's a file. But only if it has a '/' in
445 */*) if [ -f "$1" -a -r "$1" ]; then
446 decho "Found a usable page, displaying that"
449 setup_cattool $manpage
450 if man_check_for_so $manpage $(dirname $manpage); then
460 for sect in $MANSECT; do
461 decho "Searching section $sect" 2
462 for path in $MANPATH; do
463 for locpath in $locpaths; do
465 p=${p%/.} # Rid ourselves of the trailing /.
467 # Check if there is a MACHINE specific manpath.
468 if find_file $p $sect $MACHINE "$1"; then
469 if man_check_for_so $manpage $p; then
472 if [ -n "$aflag" ]; then
480 # Check if there is a MACHINE_ARCH
482 if find_file $p $sect $MACHINE_ARCH "$1"; then
483 if man_check_for_so $manpage $p; then
486 if [ -n "$aflag" ]; then
494 # Check plain old manpath.
495 if find_file $p $sect '' "$1"; then
496 if man_check_for_so $manpage $p; then
499 if [ -n "$aflag" ]; then
511 # Nothing? Well, we are done then.
512 if [ -z "$found_page" ]; then
513 echo "No manual entry for $1" >&2
519 # Usage: man_parse_args "$@"
520 # Parses commandline options for man.
524 while getopts 'M:P:S:adfhkm:op:tw' cmd_arg; do
526 M) MANPATH=$OPTARG ;;
527 P) MANPAGER=$OPTARG ;;
528 S) MANSECT=$OPTARG ;;
530 d) debug=$(( $debug + 1 )) ;;
536 p) MANROFFSEQ=$OPTARG ;;
543 shift $(( $OPTIND - 1 ))
545 # Check the args for incompatible options.
546 case "${fflag}${kflag}${tflag}${wflag}" in
547 fflagkflag*) echo "Incompatible options: -f and -k"; man_usage ;;
548 fflag*tflag*) echo "Incompatible options: -f and -t"; man_usage ;;
549 fflag*wflag) echo "Incompatible options: -f and -w"; man_usage ;;
550 *kflagtflag*) echo "Incompatible options: -k and -t"; man_usage ;;
551 *kflag*wflag) echo "Incompatible options: -k and -w"; man_usage ;;
552 *tflagwflag) echo "Incompatible options: -t and -w"; man_usage ;;
555 # Short circuit for whatis(1) and apropos(1)
556 if [ -n "$fflag" ]; then
561 if [ -n "$kflag" ]; then
567 for sect in $man_default_sections; do
568 if [ "$sect" = "$1" ]; then
569 decho "Detected manual section as first arg: $1"
581 # Setup various trivial but essential variables.
583 # Setup machine and architecture variables.
584 if [ -n "$mflag" ]; then
585 MACHINE_ARCH=${mflag%%:*}
588 if [ -z "$MACHINE_ARCH" ]; then
589 MACHINE_ARCH=$($SYSCTL -n hw.machine_arch)
591 if [ -z "$MACHINE" ]; then
592 MACHINE=$($SYSCTL -n hw.machine)
594 decho "Using architecture: $MACHINE_ARCH:$MACHINE"
598 # Setup manual sections to search.
599 if [ -z "$MANSECT" ]; then
600 MANSECT=$man_default_sections
602 decho "Using manual sections: $MANSECT"
609 # Usage: man_setup_width
617 if [ "$MANWIDTH" -gt 0 2>/dev/null ]; then
622 if { sizes=$($STTY size 0>&3 2>/dev/null); } 3>&1; then
624 if [ $2 -gt 80 ]; then
630 if [ -n "$use_width" ]; then
631 decho "Using non-standard page width: ${use_width}"
633 decho 'Using standard page width'
637 # Usage: man_setup_locale
638 # Setup necessary locale variables.
643 man_charset='US-ASCII'
645 # Setup locale information.
646 if [ -n "$oflag" ]; then
647 decho 'Using non-localized manpages'
649 # Use the locale tool to give us the proper LC_CTYPE
655 [a-z][a-z]_[A-Z][A-Z]\.*)
656 lang_cc="${LC_CTYPE%.*}"
657 man_lang="${LC_CTYPE%_*}"
658 man_country="${lang_cc#*_}"
659 man_charset="${LC_CTYPE#*.}"
661 locpaths="$locpaths:$man_lang.$man_charset"
662 if [ "$man_lang" != "en" ]; then
663 locpaths="$locpaths:en.$man_charset"
665 locpaths="$locpaths:."
667 *) echo 'Unknown locale, assuming C' >&2
672 decho "Using locale paths: $locpaths"
675 # Usage: man_usage [exitcode]
676 # Display usage for the man utility.
679 echo ' man [-adho] [-t | -w] [-M manpath] [-P pager] [-S mansect]'
680 echo ' [-m arch[:machine]] [-p [eprtv]] [mansect] page [...]'
681 echo ' man -f page [...] -- Emulates whatis(1)'
682 echo ' man -k page [...] -- Emulates apropos(1)'
684 # When exit'ing with -h, it's not an error.
688 # Usage: parse_configs
689 # Reads the end-user adjustable config files.
693 if [ -n "$parsed_configs" ]; then
699 # Read the global config first in case the user wants
700 # to override config_local.
701 if [ -r "$config_global" ]; then
702 parse_file "$config_global"
705 # Glob the list of files to parse.
707 files=$(echo $config_local)
710 for file in $files; do
711 if [ -r "$file" ]; then
719 # Usage: parse_file file
720 # Reads the specified config files.
722 local file line tstr var
725 decho "Parsing config file: $file"
729 \#*) decho " Comment" 3
731 MANPATH*) decho " MANPATH" 3
732 trim "${line#MANPATH}"
733 add_to_manpath "$tstr"
735 MANLOCALE*) decho " MANLOCALE" 3
736 trim "${line#MANLOCALE}"
737 manlocales="$manlocales:$tstr"
739 MANCONFIG*) decho " MANCONFIG" 3
740 trim "${line#MANCONFIG}"
743 # Set variables in the form of FOO_BAR
744 *_*[\ \ ]*) var="${line%%[\ \ ]*}"
746 eval "$var=\"$tstr\""
747 decho " Parsed $var" 3
754 # Traverse $PATH looking for manpaths.
758 decho "Searching PATH for man directories"
761 for path in $PATH; do
762 # Do a little special casing since the base manpages
763 # are in /usr/share/man instead of /usr/man or /man.
765 /bin|/usr/bin) add_to_manpath "/usr/share/man" ;;
766 *) if add_to_manpath "$path/man"; then
768 elif add_to_manpath "$path/MAN"; then
772 */bin) p="${path%/bin}/man"
783 if [ -z "$manpath" ]; then
784 decho ' Unable to find any manpaths, using default'
785 manpath=$man_default_path
789 # Usage: search_whatis cmd [arglist]
790 # Do the heavy lifting for apropos/whatis
792 local IFS bad cmd f good key keywords loc opt out path rval wlist
797 whatis_parse_args "$@"
803 if [ "$cmd" = "whatis" ]; then
810 for path in $MANPATH; do
811 if [ \! -d "$path" ]; then
812 decho "Skipping non-existent path: $path" 2
816 if [ -f "$path/$f" -a -r "$path/$f" ]; then
817 decho "Found whatis: $path/$f"
818 wlist="$wlist $path/$f"
821 for loc in $MANLOCALES; do
822 if [ -f "$path/$loc/$f" -a -r "$path/$loc/$f" ]; then
823 decho "Found whatis: $path/$loc/$f"
824 wlist="$wlist $path/$loc/$f"
830 if [ -z "$wlist" ]; then
831 echo "$cmd: no whatis databases in $MANPATH" >&2
836 for key in $keywords; do
837 out=$(grep -Ehi $opt -- "$key" $wlist)
838 if [ -n "$out" ]; then
841 bad="$bad\\n$key: nothing appropriate"
846 # Strip leading carriage return.
850 if [ -n "$good" ]; then
851 echo -e "$good" | $MANPAGER
854 if [ -n "$bad" ]; then
861 # Usage: setup_cattool page
862 # Finds an appropriate decompressor based on extension
865 *.bz) cattool='/usr/bin/bzcat' ;;
866 *.bz2) cattool='/usr/bin/bzcat' ;;
867 *.gz) cattool='/usr/bin/zcat' ;;
868 *.lzma) cattool='/usr/bin/lzcat' ;;
869 *.xz) cattool='/usr/bin/xzcat' ;;
870 *) cattool='/usr/bin/zcat -f' ;;
875 # Correctly sets $MANPAGER
878 if [ -z "$MANPAGER" ]; then
879 if [ -n "$MANCOLOR" ]; then
882 if [ -n "$PAGER" ]; then
889 decho "Using pager: $MANPAGER"
893 # Trims whitespace from beginning and end of a variable
898 [\ \ ]*) tstr="${tstr##[\ \ ]}" ;;
899 *[\ \ ]) tstr="${tstr%%[\ \ ]}" ;;
905 # Usage: whatis_parse_args "$@"
906 # Parse commandline args for whatis and apropos.
907 whatis_parse_args() {
909 while getopts 'd' cmd_arg; do
911 d) debug=$(( $debug + 1 )) ;;
916 shift $(( $OPTIND - 1 ))
921 # Usage: whatis_usage
922 # Display usage for the whatis/apropos utility.
924 echo "usage: $cmd [-d] keyword [...]"
932 [ $(stat -f %i /usr/bin/man) -ne $(stat -f %i /usr/bin/apropos) ] && \
934 search_whatis apropos "$@"
939 if [ -z "$pages" ]; then
940 echo 'What manual page do you want?' >&2
945 for page in $pages; do
946 decho "Searching for $page"
947 man_find_and_display "$page"
954 manpath_parse_args "$@"
955 if [ -z "$qflag" ]; then
958 if [ -n "$Lflag" ]; then
969 [ $(stat -f %i /usr/bin/man) -ne $(stat -f %i /usr/bin/whatis) ] && \
971 search_whatis whatis "$@"
974 # User's PATH setting decides on the groff-suite to pick up.
976 NROFF='groff -S -P-h -Wall -mtty-char -man'
980 TROFF='groff -S -man'
983 LOCALE=/usr/bin/locale
988 man_default_sections='1:8:2:3:n:4:5:6:7:9:l'
989 man_default_path='/usr/share/man:/usr/share/openssl/man:/usr/local/man'
990 cattool='/usr/bin/zcat -f'
992 config_global='/etc/man.conf'
994 # This can be overridden via a setting in /etc/man.conf.
995 config_local='/usr/local/etc/man.d/*.conf'
997 # Set noglobbing for now. I don't want spurious globbing.
1001 *apropos) do_apropos "$@" ;;
1002 *manpath) do_manpath "$@" ;;
1003 *whatis) do_whatis "$@" ;;