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 exists "$2" && is_newer $found $manpage; then
116 # cat page found and is newer, use that
119 setup_cattool $catpage
120 decho " Using catpage $catpage"
122 # no cat page or is older
124 decho " Skipping catpage: not found or old"
132 # Usage: decho "string" [debuglevel]
133 # Echoes to stderr string prefaced with -- if high enough debuglevel.
135 if [ $debug -ge ${2:-1} ]; then
141 # Returns true if glob resolves to a real file.
145 # Don't accidentally inherit callers IFS (breaks perl manpages)
148 # Use some globbing tricks in the shell to determine if a file
154 if [ "$1" != "$2" -a -r "$2" ]; then
162 # Usage: find_file path section subdir pagename
163 # Returns: true if something is matched and found.
164 # Search the given path/section combo for a given page.
166 local manroot catroot mann man0 catn cat0
171 manroot="$manroot/$3"
172 catroot="$catroot/$3"
175 if [ ! -d "$manroot" ]; then
178 decho " Searching directory $manroot" 2
180 mann="$manroot/$4.$2*"
181 man0="$manroot/$4.0*"
182 catn="$catroot/$4.$2*"
183 cat0="$catroot/$4.0*"
185 # This is the behavior as seen by the original man utility.
186 # Let's not change that which doesn't seem broken.
187 if check_man "$mann" "$catn"; then
189 elif check_man "$man0" "$cat0"; then
191 elif check_cat "$catn"; then
193 elif check_cat "$cat0"; then
200 # Usage: is_newer file1 file2
201 # Returns true if file1 is newer than file2 as calculated by mtime.
203 if ! [ "$1" -ot "$2" ]; then
204 decho " mtime: $1 not older than $2" 3
207 decho " mtime: $1 older than $2" 3
212 # Usage: manpath_parse_args "$@"
213 # Parses commandline options for manpath.
214 manpath_parse_args() {
217 while getopts 'Ldq' cmd_arg; do
220 d) debug=$(( $debug + 1 )) ;;
227 # Usage: manpath_usage
228 # Display usage for the manpath(1) utility.
230 echo 'usage: manpath [-Ldq]' >&2
234 # Usage: manpath_warnings
235 # Display some warnings to stderr.
237 if [ -z "$Lflag" -a -n "$MANPATH" ]; then
238 echo "(Warning: MANPATH environment variable set)" >&2
241 if [ -n "$Lflag" -a -n "$MANLOCALES" ]; then
242 echo "(Warning: MANLOCALES environment variable set)" >&2
246 # Usage: man_check_for_so page path
247 # Returns: True if able to resolve the file, false if it ended in tears.
248 # Detects the presence of the .so directive and causes the file to be
249 # redirected to another source file.
255 # We need to loop to accommodate multiple .so directives.
258 line=$($cattool $manpage | head -1)
260 .so*) trim "${line#.so}"
261 decho "$manpage includes $tstr"
262 # Glob and check for the file.
263 if ! check_man "$path/$tstr*" ""; then
264 decho " Unable to find $tstr"
275 # Usage: man_display_page
276 # Display either the manpage or catpage depending on the use_cat variable
278 local EQN COL NROFF PIC TBL TROFF REFER VGRIND
279 local IFS l nroff_dev pipeline preproc_arg tool
281 # We are called with IFS set to colon. This causes really weird
282 # things to happen for the variables that have spaces in them.
285 # If we are supposed to use a catpage and we aren't using troff(1)
286 # just zcat the catpage and we are done.
287 if [ -z "$tflag" -a -n "$use_cat" ]; then
288 if [ -n "$wflag" ]; then
289 echo "$catpage (source: $manpage)"
292 if [ $debug -gt 0 ]; then
293 decho "Command: $cattool $catpage | $PAGER"
296 eval "$cattool $catpage | $PAGER"
303 # Okay, we are using the manpage, do we just need to output the
304 # name of the manpage?
305 if [ -n "$wflag" ]; then
311 # So, we really do need to parse the manpage. First, figure out the
312 # device flag (-T) we have to pass to eqn(1) and groff(1). Then,
313 # setup the pipeline of commands based on the user's request.
315 # If the manpage is from a particular charset, we need to setup nroff
316 # to properly output for the correct device.
319 # I don't pretend to know this; I'm just copying from the
320 # previous version of man(1).
321 case "$man_charset" in
322 KOI8-R) nroff_dev="koi8-r" ;;
323 ISO8859-1) nroff_dev="latin1" ;;
324 ISO8859-15) nroff_dev="latin1" ;;
325 UTF-8) nroff_dev="utf8" ;;
326 *) nroff_dev="ascii" ;;
329 NROFF="$NROFF -T$nroff_dev"
330 EQN="$EQN -T$nroff_dev"
332 # Iff the manpage is from the locale and not just the charset,
333 # then we need to define the locale string.
335 */${man_lang}_${man_country}.${man_charset}/*)
336 NROFF="$NROFF -dlocale=$man_lang.$man_charset"
338 */${man_lang}.${man_charset}/*)
339 NROFF="$NROFF -dlocale=$man_lang.$man_charset"
343 # Allow language specific calls to override the default
345 l=$(echo $man_lang | tr [:lower:] [:upper:])
346 for tool in EQN COL NROFF PIC TBL TROFF REFER VGRIND; do
347 eval "$tool=\${${tool}_$l:-\$$tool}"
350 *) NROFF="$NROFF -Tascii"
355 if [ -n "$MANROFFSEQ" ]; then
357 while getopts 'egprtv' preproc_arg; do
358 case "${preproc_arg}" in
359 e) pipeline="$pipeline | $EQN" ;;
360 g) ;; # Ignore for compatability.
361 p) pipeline="$pipeline | $PIC" ;;
362 r) pipeline="$pipeline | $REFER" ;;
363 t) pipeline="$pipeline | $TBL"; use_col=yes ;;
364 v) pipeline="$pipeline | $VGRIND" ;;
368 # Strip the leading " | " from the resulting pipeline.
369 pipeline="${pipeline#" | "}"
375 if [ -n "$tflag" ]; then
376 pipeline="$pipeline | $TROFF"
378 pipeline="$pipeline | $NROFF"
380 if [ -n "$use_col" ]; then
381 pipeline="$pipeline | $COL"
384 pipeline="$pipeline | $PAGER"
387 if [ $debug -gt 0 ]; then
388 decho "Command: $cattool $manpage | $pipeline"
391 eval "$cattool $manpage | $pipeline"
396 # Usage: man_find_and_display page
397 # Search through the manpaths looking for the given page.
398 man_find_and_display() {
399 local found_page locpath p path sect
401 # Check to see if it's a file. But only if it has a '/' in
404 */*) if [ -f "$1" -a -r "$1" ]; then
405 decho "Found a usable page, displaying that"
408 setup_cattool $manpage
409 if man_check_for_so $manpage $(dirname $manpage); then
419 for sect in $MANSECT; do
420 decho "Searching section $sect" 2
421 for path in $MANPATH; do
422 for locpath in $locpaths; do
424 p=${p%/.} # Rid ourselves of the trailing /.
426 # Check if there is a MACHINE specific manpath.
427 if find_file $p $sect $MACHINE "$1"; then
428 if man_check_for_so $manpage $p; then
431 if [ -n "$aflag" ]; then
439 # Check if there is a MACHINE_ARCH
441 if find_file $p $sect $MACHINE_ARCH "$1"; then
442 if man_check_for_so $manpage $p; then
445 if [ -n "$aflag" ]; then
453 # Check plain old manpath.
454 if find_file $p $sect '' "$1"; then
455 if man_check_for_so $manpage $p; then
458 if [ -n "$aflag" ]; then
470 # Nothing? Well, we are done then.
471 if [ -z "$found_page" ]; then
472 echo "No manual entry for $1" >&2
478 # Usage: man_parse_args "$@"
479 # Parses commandline options for man.
483 while getopts 'M:P:S:adfhkm:op:tw' cmd_arg; do
485 M) MANPATH=$OPTARG ;;
487 S) MANSECT=$OPTARG ;;
489 d) debug=$(( $debug + 1 )) ;;
495 p) MANROFFSEQ=$OPTARG ;;
502 shift $(( $OPTIND - 1 ))
504 # Check the args for incompatible options.
505 case "${fflag}${kflag}${tflag}${wflag}" in
506 fflagkflag*) echo "Incompatible options: -f and -k"; man_usage ;;
507 fflag*tflag*) echo "Incompatible options: -f and -t"; man_usage ;;
508 fflag*wflag) echo "Incompatible options: -f and -w"; man_usage ;;
509 *kflagtflag*) echo "Incompatible options: -k and -t"; man_usage ;;
510 *kflag*wflag) echo "Incompatible options: -k and -w"; man_usage ;;
511 *tflagwflag) echo "Incompatible options: -t and -w"; man_usage ;;
514 # Short circuit for whatis(1) and apropos(1)
515 if [ -n "$fflag" ]; then
520 if [ -n "$kflag" ]; then
526 for sect in $man_default_sections; do
527 if [ "$sect" = "$1" ]; then
528 decho "Detected manual section as first arg: $1"
540 # Setup various trivial but essential variables.
542 # Setup machine and architecture variables.
543 if [ -n "$mflag" ]; then
544 MACHINE_ARCH=${mflag%%:*}
547 if [ -z "$MACHINE_ARCH" ]; then
548 MACHINE_ARCH=$($SYSCTL -n hw.machine_arch)
550 if [ -z "$MACHINE" ]; then
551 MACHINE=$($SYSCTL -n hw.machine)
553 decho "Using architecture: $MACHINE_ARCH:$MACHINE"
557 # Setup manual sections to search.
558 if [ -z "$MANSECT" ]; then
559 MANSECT=$man_default_sections
561 decho "Using manual sections: $MANSECT"
567 # Usage: man_setup_locale
568 # Setup necessary locale variables.
573 man_charset='US-ASCII'
575 # Setup locale information.
576 if [ -n "$oflag" ]; then
577 decho 'Using non-localized manpages'
579 # Use the locale tool to give us the proper LC_CTYPE
585 [a-z][a-z]_[A-Z][A-Z]\.*)
586 lang_cc="${LC_CTYPE%.*}"
587 man_lang="${LC_CTYPE%_*}"
588 man_country="${lang_cc#*_}"
589 man_charset="${LC_CTYPE#*.}"
591 locpaths="$locpaths:$man_lang.$man_charset"
592 if [ "$man_lang" != "en" ]; then
593 locpaths="$locpaths:en.$man_charset"
595 locpaths="$locpaths:."
597 *) echo 'Unknown locale, assuming C' >&2
602 decho "Using locale paths: $locpaths"
605 # Usage: man_usage [exitcode]
606 # Display usage for the man utility.
609 echo ' man [-adho] [-t | -w] [-M manpath] [-P pager] [-S mansect]'
610 echo ' [-m arch[:machine]] [-p [eprtv]] [mansect] page [...]'
611 echo ' man -f page [...] -- Emulates whatis(1)'
612 echo ' man -k page [...] -- Emulates apropos(1)'
614 # When exit'ing with -h, it's not an error.
618 # Usage: parse_configs
619 # Reads the end-user adjustable config files.
623 if [ -n "$parsed_configs" ]; then
629 # Read the global config first in case the user wants
630 # to override config_local.
631 if [ -r "$config_global" ]; then
632 parse_file "$config_global"
635 # Glob the list of files to parse.
637 files=$(echo $config_local)
640 for file in $files; do
641 if [ -r "$file" ]; then
649 # Usage: parse_file file
650 # Reads the specified config files.
652 local file line tstr var
655 decho "Parsing config file: $file"
659 \#*) decho " Comment" 3
661 MANPATH*) decho " MANPATH" 3
662 trim "${line#MANPATH}"
663 add_to_manpath "$tstr"
665 MANLOCALE*) decho " MANLOCALE" 3
666 trim "${line#MANLOCALE}"
667 manlocales="$manlocales:$tstr"
669 MANCONFIG*) decho " MANCONFIG" 3
670 trim "${line#MANCONF}"
673 # Set variables in the form of FOO_BAR
674 *_*[\ \ ]*) var="${line%%[\ \ ]*}"
676 eval "$var=\"$tstr\""
677 decho " Parsed $var" 3
684 # Traverse $PATH looking for manpaths.
688 decho "Searching PATH for man directories"
691 for path in $PATH; do
692 # Do a little special casing since the base manpages
693 # are in /usr/share/man instead of /usr/man or /man.
695 /bin|/usr/bin) add_to_manpath "/usr/share/man" ;;
696 *) if add_to_manpath "$path/man"; then
698 elif add_to_manpath "$path/MAN"; then
702 */bin) p="${path%/bin}/man"
713 if [ -z "$manpath" ]; then
714 decho ' Unable to find any manpaths, using default'
715 manpath=$man_default_path
719 # Usage: search_whatis cmd [arglist]
720 # Do the heavy lifting for apropos/whatis
722 local IFS bad cmd f good key keywords loc opt out path rval wlist
727 whatis_parse_args "$@"
733 if [ "$cmd" = "whatis" ]; then
740 for path in $MANPATH; do
741 if [ \! -d "$path" ]; then
742 decho "Skipping non-existent path: $path" 2
746 if [ -f "$path/$f" -a -r "$path/$f" ]; then
747 decho "Found whatis: $path/$f"
748 wlist="$wlist $path/$f"
751 for loc in $MANLOCALES; do
752 if [ -f "$path/$loc/$f" -a -r "$path/$loc/$f" ]; then
753 decho "Found whatis: $path/$loc/$f"
754 wlist="$wlist $path/$loc/$f"
760 if [ -z "$wlist" ]; then
761 echo "$cmd: no whatis databases in $MANPATH" >&2
766 for key in $keywords; do
767 out=$(grep -Ehi $opt -- "$key" $wlist)
768 if [ -n "$out" ]; then
771 bad="$bad\\n$key: nothing appropriate"
776 # Strip leading carriage return.
780 if [ -n "$good" ]; then
781 echo -e "$good" | $PAGER
784 if [ -n "$bad" ]; then
791 # Usage: setup_cattool page
792 # Finds an appropriate decompressor based on extension
795 *.bz) cattool='/usr/bin/bzcat' ;;
796 *.bz2) cattool='/usr/bin/bzcat' ;;
797 *.gz) cattool='/usr/bin/zcat' ;;
798 *.lzma) cattool='/usr/bin/lzcat' ;;
799 *.xz) cattool='/usr/bin/xzcat' ;;
800 *) cattool='/usr/bin/zcat -f' ;;
805 # Correctly sets $PAGER
808 if [ -z "$PAGER" ]; then
811 decho "Using pager: $PAGER"
815 # Trims whitespace from beginning and end of a variable
820 [\ \ ]*) tstr="${tstr##[\ \ ]}" ;;
821 *[\ \ ]) tstr="${tstr%%[\ \ ]}" ;;
827 # Usage: whatis_parse_args "$@"
828 # Parse commandline args for whatis and apropos.
829 whatis_parse_args() {
831 while getopts 'd' cmd_arg; do
833 d) debug=$(( $debug + 1 )) ;;
838 shift $(( $OPTIND - 1 ))
843 # Usage: whatis_usage
844 # Display usage for the whatis/apropos utility.
846 echo "usage: $cmd [-d] keyword [...]"
854 search_whatis apropos "$@"
859 if [ -z "$pages" ]; then
860 echo 'What manual page do you want?' >&2
865 for page in $pages; do
866 decho "Searching for $page"
867 man_find_and_display "$page"
874 manpath_parse_args "$@"
875 if [ -z "$qflag" ]; then
878 if [ -n "$Lflag" ]; then
889 search_whatis whatis "$@"
892 # User's PATH setting decides on the groff-suite to pick up.
894 NROFF='groff -S -P-c -Wall -mtty-char -man'
898 TROFF='groff -S -man'
902 LOCALE=/usr/bin/locale
906 man_default_sections='1:1aout:8:2:3:n:4:5:6:7:9:l'
907 man_default_path='/usr/share/man:/usr/share/openssl/man:/usr/local/man'
908 cattool='/usr/bin/zcat -f'
910 config_global='/etc/man.conf'
912 # This can be overridden via a setting in /etc/man.conf.
913 config_local='/usr/local/etc/man.d/*.conf'
915 # Set noglobbing for now. I don't want spurious globbing.
919 *apropos) do_apropos "$@" ;;
920 *manpath) do_manpath "$@" ;;
921 *whatis) do_whatis "$@" ;;