xref: /freebsd/usr.bin/man/man.sh (revision dcf58f92e2c19a32fc171f763698e711c719badc)
1#! /bin/sh
2#
3#  Copyright (c) 2010 Gordon Tetlow
4#  All rights reserved.
5#
6#  Redistribution and use in source and binary forms, with or without
7#  modification, are permitted provided that the following conditions
8#  are met:
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.
14#
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
25#  SUCH DAMAGE.
26#
27# $FreeBSD$
28
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.
32add_to_manpath() {
33	case "$manpath" in
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"
39			manpath="$manpath:$1"
40			return 0
41		fi
42		;;
43	esac
44
45	return 1
46}
47
48# Usage: build_manlocales
49# Builds a correct MANLOCALES variable.
50build_manlocales() {
51	# If the user has set manlocales, who are we to argue.
52	if [ -n "$MANLOCALES" ]; then
53		return
54	fi
55
56	parse_configs
57
58	# Trim leading colon
59	MANLOCALES=${manlocales#:}
60
61	decho "Available manual locales: $MANLOCALES"
62}
63
64# Usage: build_manpath
65# Builds a correct MANPATH variable.
66build_manpath() {
67	local IFS
68
69	# If the user has set a manpath, who are we to argue.
70	if [ -n "$MANPATH" ]; then
71		return
72	fi
73
74	search_path
75
76	decho "Adding default manpath entries"
77	IFS=:
78	for path in $man_default_path; do
79		add_to_manpath "$path"
80	done
81	unset IFS
82
83	parse_configs
84
85	# Trim leading colon
86	MANPATH=${manpath#:}
87
88	decho "Using manual path: $MANPATH"
89}
90
91# Usage: check_cat catglob
92# Checks to see if a cat glob is available.
93check_cat() {
94	if exists "$1"; then
95		use_cat=yes
96		catpage=$found
97		setup_cattool $catpage
98		decho "    Found catpage $catpage"
99		return 0
100	else
101		return 1
102	fi
103}
104
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.
108check_man() {
109	if exists "$1"; then
110		# We have a match, check for a cat page
111		manpage=$found
112		setup_cattool $manpage
113		decho "    Found manpage $manpage"
114
115		if [ -n "${use_width}" ]; then
116			# non-standard width
117			unset use_cat
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
121			use_cat=yes
122			catpage=$found
123			setup_cattool $catpage
124			decho "    Using catpage $catpage"
125		else
126			# no cat page or is older
127			unset use_cat
128			decho "    Skipping catpage: not found or old"
129		fi
130		return 0
131	fi
132
133	return 1
134}
135
136# Usage: decho "string" [debuglevel]
137# Echoes to stderr string prefaced with -- if high enough debuglevel.
138decho() {
139	if [ $debug -ge ${2:-1} ]; then
140		echo "-- $1" >&2
141	fi
142}
143
144# Usage: exists glob
145# Returns true if glob resolves to a real file.
146exists() {
147	local IFS
148
149	# Don't accidentally inherit callers IFS (breaks perl manpages)
150	unset IFS
151
152	# Use some globbing tricks in the shell to determine if a file
153	# exists or not.
154	set +f
155	set -- "$1" $1
156	set -f
157
158	if [ "$1" != "$2" -a -r "$2" ]; then
159		found="$2"
160		return 0
161	fi
162
163	return 1
164}
165
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.
169find_file() {
170	local manroot catroot mann man0 catn cat0
171
172	manroot="$1/man$2"
173	catroot="$1/cat$2"
174	if [ -n "$3" ]; then
175		manroot="$manroot/$3"
176		catroot="$catroot/$3"
177	fi
178
179	if [ ! -d "$manroot" ]; then
180		return 1
181	fi
182	decho "  Searching directory $manroot" 2
183
184	mann="$manroot/$4.$2*"
185	man0="$manroot/$4.0*"
186	catn="$catroot/$4.$2*"
187	cat0="$catroot/$4.0*"
188
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
192		return 0
193	elif check_man "$man0" "$cat0"; then
194		return 0
195	elif check_cat "$catn"; then
196		return 0
197	elif check_cat "$cat0"; then
198		return 0
199	fi
200
201	return 1
202}
203
204# Usage: is_newer file1 file2
205# Returns true if file1 is newer than file2 as calculated by mtime.
206is_newer() {
207	if ! [ "$1" -ot "$2" ]; then
208		decho "    mtime: $1 not older than $2" 3
209		return 0
210	else
211		decho "    mtime: $1 older than $2" 3
212		return 1
213	fi
214}
215
216# Usage: manpath_parse_args "$@"
217# Parses commandline options for manpath.
218manpath_parse_args() {
219	local cmd_arg
220
221	while getopts 'Ldq' cmd_arg; do
222		case "${cmd_arg}" in
223		L)	Lflag=Lflag ;;
224		d)	debug=$(( $debug + 1 )) ;;
225		q)	qflag=qflag ;;
226		*)	manpath_usage ;;
227		esac
228	done >&2
229}
230
231# Usage: manpath_usage
232# Display usage for the manpath(1) utility.
233manpath_usage() {
234	echo 'usage: manpath [-Ldq]' >&2
235	exit 1
236}
237
238# Usage: manpath_warnings
239# Display some warnings to stderr.
240manpath_warnings() {
241	if [ -z "$Lflag" -a -n "$MANPATH" ]; then
242		echo "(Warning: MANPATH environment variable set)" >&2
243	fi
244
245	if [ -n "$Lflag" -a -n "$MANLOCALES" ]; then
246		echo "(Warning: MANLOCALES environment variable set)" >&2
247	fi
248}
249
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.
254man_check_for_so() {
255	local IFS line tstr
256
257	unset IFS
258
259	# We need to loop to accommodate multiple .so directives.
260	while true
261	do
262		line=$($cattool $manpage | head -1)
263		case "$line" in
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"
269				return 1
270			fi
271			;;
272		*)	break ;;
273		esac
274	done
275
276	return 0
277}
278
279# Usage: man_display_page
280# Display either the manpage or catpage depending on the use_cat variable
281man_display_page() {
282	local IFS pipeline preconv_enc testline
283
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.
286	unset IFS
287
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)"
293			ret=0
294		else
295			if [ $debug -gt 0 ]; then
296				decho "Command: $cattool $catpage | $MANPAGER"
297				ret=0
298			else
299				eval "$cattool $catpage | $MANPAGER"
300				ret=$?
301			fi
302		fi
303		return
304	fi
305
306	# Okay, we are using the manpage, do we just need to output the
307	# name of the manpage?
308	if [ -n "$wflag" ]; then
309		echo "$manpage"
310		ret=0
311		return
312	fi
313
314	case "${manpage}" in
315	*.${man_charset}/*)
316		case "$man_charset" in
317		ISO8859-1) preconv_enc="latin-1" ;;
318		ISO8859-15) preconv_enc="latin-1" ;;
319		UTF-8) preconv_enc="utf-8" ;;
320		esac
321		;;
322	esac
323
324	if [ -n "$preconv_enc" ]; then
325		pipeline="preconv -e $preconv_enc |"
326	fi
327	testline="$pipeline mandoc -Tlint -Werror 2>/dev/null"
328	pipeline="$pipeline mandoc -Tlocale | $MANPAGER"
329
330	if ! eval "$cattool $manpage | $testline" ;then
331		if which -s groff; then
332			man_display_page_groff
333		else
334			echo "This manpage needs groff(1) to be rendered" >&2
335			echo "First install groff(1): " >&2
336			echo "pkg install groff " >&2
337			ret=1
338		fi
339		return
340	fi
341
342	if [ $debug -gt 0 ]; then
343		decho "Command: $cattool $manpage | $pipeline"
344		ret=0
345	else
346		eval "$cattool $manpage | $pipeline"
347		ret=$?
348	fi
349}
350
351# Usage: man_display_page_groff
352# Display the manpage using groff
353man_display_page_groff() {
354	local EQN NROFF PIC TBL TROFF REFER VGRIND
355	local IFS l nroff_dev pipeline preproc_arg tool
356
357	# So, we really do need to parse the manpage. First, figure out the
358	# device flag (-T) we have to pass to eqn(1) and groff(1). Then,
359	# setup the pipeline of commands based on the user's request.
360
361	# If the manpage is from a particular charset, we need to setup nroff
362	# to properly output for the correct device.
363	case "${manpage}" in
364	*.${man_charset}/*)
365		# I don't pretend to know this; I'm just copying from the
366		# previous version of man(1).
367		case "$man_charset" in
368		KOI8-R)		nroff_dev="koi8-r" ;;
369		ISO8859-1)	nroff_dev="latin1" ;;
370		ISO8859-15)	nroff_dev="latin1" ;;
371		UTF-8)		nroff_dev="utf8" ;;
372		*)		nroff_dev="ascii" ;;
373		esac
374
375		NROFF="$NROFF -T$nroff_dev"
376		EQN="$EQN -T$nroff_dev"
377
378		# Iff the manpage is from the locale and not just the charset,
379		# then we need to define the locale string.
380		case "${manpage}" in
381		*/${man_lang}_${man_country}.${man_charset}/*)
382			NROFF="$NROFF -dlocale=$man_lang.$man_charset"
383			;;
384		*/${man_lang}.${man_charset}/*)
385			NROFF="$NROFF -dlocale=$man_lang.$man_charset"
386			;;
387		esac
388
389		# Allow language specific calls to override the default
390		# set of utilities.
391		l=$(echo $man_lang | tr [:lower:] [:upper:])
392		for tool in EQN NROFF PIC TBL TROFF REFER VGRIND; do
393			eval "$tool=\${${tool}_$l:-\$$tool}"
394		done
395		;;
396	*)	NROFF="$NROFF -Tascii"
397		EQN="$EQN -Tascii"
398		;;
399	esac
400
401	if [ -z "$MANCOLOR" ]; then
402		NROFF="$NROFF -P-c"
403	fi
404
405	if [ -n "${use_width}" ]; then
406		NROFF="$NROFF -rLL=${use_width}n -rLT=${use_width}n"
407	fi
408
409	if [ -n "$MANROFFSEQ" ]; then
410		set -- -$MANROFFSEQ
411		while getopts 'egprtv' preproc_arg; do
412			case "${preproc_arg}" in
413			e)	pipeline="$pipeline | $EQN" ;;
414			g)	;; # Ignore for compatibility.
415			p)	pipeline="$pipeline | $PIC" ;;
416			r)	pipeline="$pipeline | $REFER" ;;
417			t)	pipeline="$pipeline | $TBL" ;;
418			v)	pipeline="$pipeline | $VGRIND" ;;
419			*)	usage ;;
420			esac
421		done
422		# Strip the leading " | " from the resulting pipeline.
423		pipeline="${pipeline#" | "}"
424	else
425		pipeline="$TBL"
426	fi
427
428	if [ -n "$tflag" ]; then
429		pipeline="$pipeline | $TROFF"
430	else
431		pipeline="$pipeline | $NROFF | $MANPAGER"
432	fi
433
434	if [ $debug -gt 0 ]; then
435		decho "Command: $cattool $manpage | $pipeline"
436		ret=0
437	else
438		eval "$cattool $manpage | $pipeline"
439		ret=$?
440	fi
441}
442
443# Usage: man_find_and_display page
444# Search through the manpaths looking for the given page.
445man_find_and_display() {
446	local found_page locpath p path sect
447
448	# Check to see if it's a file. But only if it has a '/' in
449	# the filename.
450	case "$1" in
451	*/*)	if [ -f "$1" -a -r "$1" ]; then
452			decho "Found a usable page, displaying that"
453			unset use_cat
454			manpage="$1"
455			setup_cattool $manpage
456			if man_check_for_so $manpage $(dirname $manpage); then
457				found_page=yes
458				man_display_page
459			fi
460			return
461		fi
462		;;
463	esac
464
465	IFS=:
466	for sect in $MANSECT; do
467		decho "Searching section $sect" 2
468		for path in $MANPATH; do
469			for locpath in $locpaths; do
470				p=$path/$locpath
471				p=${p%/.} # Rid ourselves of the trailing /.
472
473				# Check if there is a MACHINE specific manpath.
474				if find_file $p $sect $MACHINE "$1"; then
475					if man_check_for_so $manpage $p; then
476						found_page=yes
477						man_display_page
478						if [ -n "$aflag" ]; then
479							continue 2
480						else
481							return
482						fi
483					fi
484				fi
485
486				# Check if there is a MACHINE_ARCH
487				# specific manpath.
488				if find_file $p $sect $MACHINE_ARCH "$1"; then
489					if man_check_for_so $manpage $p; then
490						found_page=yes
491						man_display_page
492						if [ -n "$aflag" ]; then
493							continue 2
494						else
495							return
496						fi
497					fi
498				fi
499
500				# Check plain old manpath.
501				if find_file $p $sect '' "$1"; then
502					if man_check_for_so $manpage $p; then
503						found_page=yes
504						man_display_page
505						if [ -n "$aflag" ]; then
506							continue 2
507						else
508							return
509						fi
510					fi
511				fi
512			done
513		done
514	done
515	unset IFS
516
517	# Nothing? Well, we are done then.
518	if [ -z "$found_page" ]; then
519		echo "No manual entry for $1" >&2
520		ret=1
521		return
522	fi
523}
524
525# Usage: man_parse_args "$@"
526# Parses commandline options for man.
527man_parse_args() {
528	local IFS cmd_arg
529
530	while getopts 'M:P:S:adfhkm:op:tw' cmd_arg; do
531		case "${cmd_arg}" in
532		M)	MANPATH=$OPTARG ;;
533		P)	MANPAGER=$OPTARG ;;
534		S)	MANSECT=$OPTARG ;;
535		a)	aflag=aflag ;;
536		d)	debug=$(( $debug + 1 )) ;;
537		f)	fflag=fflag ;;
538		h)	man_usage 0 ;;
539		k)	kflag=kflag ;;
540		m)	mflag=$OPTARG ;;
541		o)	oflag=oflag ;;
542		p)	MANROFFSEQ=$OPTARG ;;
543		t)	tflag=tflag ;;
544		w)	wflag=wflag ;;
545		*)	man_usage ;;
546		esac
547	done >&2
548
549	shift $(( $OPTIND - 1 ))
550
551	# Check the args for incompatible options.
552	case "${fflag}${kflag}${tflag}${wflag}" in
553	fflagkflag*)	echo "Incompatible options: -f and -k"; man_usage ;;
554	fflag*tflag*)	echo "Incompatible options: -f and -t"; man_usage ;;
555	fflag*wflag)	echo "Incompatible options: -f and -w"; man_usage ;;
556	*kflagtflag*)	echo "Incompatible options: -k and -t"; man_usage ;;
557	*kflag*wflag)	echo "Incompatible options: -k and -w"; man_usage ;;
558	*tflagwflag)	echo "Incompatible options: -t and -w"; man_usage ;;
559	esac
560
561	# Short circuit for whatis(1) and apropos(1)
562	if [ -n "$fflag" ]; then
563		do_whatis "$@"
564		exit
565	fi
566
567	if [ -n "$kflag" ]; then
568		do_apropos "$@"
569		exit
570	fi
571
572	IFS=:
573	for sect in $man_default_sections; do
574		if [ "$sect" = "$1" ]; then
575			decho "Detected manual section as first arg: $1"
576			MANSECT="$1"
577			shift
578			break
579		fi
580	done
581	unset IFS
582
583	pages="$*"
584}
585
586# Usage: man_setup
587# Setup various trivial but essential variables.
588man_setup() {
589	# Setup machine and architecture variables.
590	if [ -n "$mflag" ]; then
591		MACHINE_ARCH=${mflag%%:*}
592		MACHINE=${mflag##*:}
593	fi
594	if [ -z "$MACHINE_ARCH" ]; then
595		MACHINE_ARCH=$($SYSCTL -n hw.machine_arch)
596	fi
597	if [ -z "$MACHINE" ]; then
598		MACHINE=$($SYSCTL -n hw.machine)
599	fi
600	decho "Using architecture: $MACHINE_ARCH:$MACHINE"
601
602	setup_pager
603
604	# Setup manual sections to search.
605	if [ -z "$MANSECT" ]; then
606		MANSECT=$man_default_sections
607	fi
608	decho "Using manual sections: $MANSECT"
609
610	build_manpath
611	man_setup_locale
612	man_setup_width
613}
614
615# Usage: man_setup_width
616# Set up page width.
617man_setup_width() {
618	local sizes
619
620	unset use_width
621	case "$MANWIDTH" in
622	[0-9]*)
623		if [ "$MANWIDTH" -gt 0 2>/dev/null ]; then
624			use_width=$MANWIDTH
625		fi
626		;;
627	[Tt][Tt][Yy])
628		if { sizes=$($STTY size 0>&3 2>/dev/null); } 3>&1; then
629			set -- $sizes
630			if [ $2 -gt 80 ]; then
631				use_width=$(($2-2))
632			fi
633		fi
634		;;
635	esac
636	if [ -n "$use_width" ]; then
637		decho "Using non-standard page width: ${use_width}"
638	else
639		decho 'Using standard page width'
640	fi
641}
642
643# Usage: man_setup_locale
644# Setup necessary locale variables.
645man_setup_locale() {
646	local lang_cc
647
648	locpaths='.'
649	man_charset='US-ASCII'
650
651	# Setup locale information.
652	if [ -n "$oflag" ]; then
653		decho 'Using non-localized manpages'
654	else
655		# Use the locale tool to give us the proper LC_CTYPE
656		eval $( $LOCALE )
657
658		case "$LC_CTYPE" in
659		C)		;;
660		POSIX)		;;
661		[a-z][a-z]_[A-Z][A-Z]\.*)
662				lang_cc="${LC_CTYPE%.*}"
663				man_lang="${LC_CTYPE%_*}"
664				man_country="${lang_cc#*_}"
665				man_charset="${LC_CTYPE#*.}"
666				locpaths="$LC_CTYPE"
667				locpaths="$locpaths:$man_lang.$man_charset"
668				if [ "$man_lang" != "en" ]; then
669					locpaths="$locpaths:en.$man_charset"
670				fi
671				locpaths="$locpaths:."
672				;;
673		*)		echo 'Unknown locale, assuming C' >&2
674				;;
675		esac
676	fi
677
678	decho "Using locale paths: $locpaths"
679}
680
681# Usage: man_usage [exitcode]
682# Display usage for the man utility.
683man_usage() {
684	echo 'Usage:'
685	echo ' man [-adho] [-t | -w] [-M manpath] [-P pager] [-S mansect]'
686	echo '     [-m arch[:machine]] [-p [eprtv]] [mansect] page [...]'
687	echo ' man -f page [...] -- Emulates whatis(1)'
688	echo ' man -k page [...] -- Emulates apropos(1)'
689
690	# When exit'ing with -h, it's not an error.
691	exit ${1:-1}
692}
693
694# Usage: parse_configs
695# Reads the end-user adjustable config files.
696parse_configs() {
697	local IFS file files
698
699	if [ -n "$parsed_configs" ]; then
700		return
701	fi
702
703	unset IFS
704
705	# Read the global config first in case the user wants
706	# to override config_local.
707	if [ -r "$config_global" ]; then
708		parse_file "$config_global"
709	fi
710
711	# Glob the list of files to parse.
712	set +f
713	files=$(echo $config_local)
714	set -f
715
716	for file in $files; do
717		if [ -r "$file" ]; then
718			parse_file "$file"
719		fi
720	done
721
722	parsed_configs='yes'
723}
724
725# Usage: parse_file file
726# Reads the specified config files.
727parse_file() {
728	local file line tstr var
729
730	file="$1"
731	decho "Parsing config file: $file"
732	while read line; do
733		decho "  $line" 2
734		case "$line" in
735		\#*)		decho "    Comment" 3
736				;;
737		MANPATH*)	decho "    MANPATH" 3
738				trim "${line#MANPATH}"
739				add_to_manpath "$tstr"
740				;;
741		MANLOCALE*)	decho "    MANLOCALE" 3
742				trim "${line#MANLOCALE}"
743				manlocales="$manlocales:$tstr"
744				;;
745		MANCONFIG*)	decho "    MANCONFIG" 3
746				trim "${line#MANCONFIG}"
747				config_local="$tstr"
748				;;
749		# Set variables in the form of FOO_BAR
750		*_*[\ \	]*)	var="${line%%[\ \	]*}"
751				trim "${line#$var}"
752				eval "$var=\"$tstr\""
753				decho "    Parsed $var" 3
754				;;
755		esac
756	done < "$file"
757}
758
759# Usage: search_path
760# Traverse $PATH looking for manpaths.
761search_path() {
762	local IFS p path
763
764	decho "Searching PATH for man directories"
765
766	IFS=:
767	for path in $PATH; do
768		# Do a little special casing since the base manpages
769		# are in /usr/share/man instead of /usr/man or /man.
770		case "$path" in
771		/bin|/usr/bin)	add_to_manpath "/usr/share/man" ;;
772		*)	if add_to_manpath "$path/man"; then
773				:
774			elif add_to_manpath "$path/MAN"; then
775				:
776			else
777				case "$path" in
778				*/bin)	p="${path%/bin}/man"
779					add_to_manpath "$p"
780					;;
781				*)	;;
782				esac
783			fi
784			;;
785		esac
786	done
787	unset IFS
788
789	if [ -z "$manpath" ]; then
790		decho '  Unable to find any manpaths, using default'
791		manpath=$man_default_path
792	fi
793}
794
795# Usage: search_whatis cmd [arglist]
796# Do the heavy lifting for apropos/whatis
797search_whatis() {
798	local IFS bad cmd f good key keywords loc opt out path rval wlist
799
800	cmd="$1"
801	shift
802
803	whatis_parse_args "$@"
804
805	build_manpath
806	build_manlocales
807	setup_pager
808
809	if [ "$cmd" = "whatis" ]; then
810		opt="-w"
811	fi
812
813	f='whatis'
814
815	IFS=:
816	for path in $MANPATH; do
817		if [ \! -d "$path" ]; then
818			decho "Skipping non-existent path: $path" 2
819			continue
820		fi
821
822		if [ -f "$path/$f" -a -r "$path/$f" ]; then
823			decho "Found whatis: $path/$f"
824			wlist="$wlist $path/$f"
825		fi
826
827		for loc in $MANLOCALES; do
828			if [ -f "$path/$loc/$f" -a -r "$path/$loc/$f" ]; then
829				decho "Found whatis: $path/$loc/$f"
830				wlist="$wlist $path/$loc/$f"
831			fi
832		done
833	done
834	unset IFS
835
836	if [ -z "$wlist" ]; then
837		echo "$cmd: no whatis databases in $MANPATH" >&2
838		exit 1
839	fi
840
841	rval=0
842	for key in $keywords; do
843		out=$(grep -Ehi $opt -- "$key" $wlist)
844		if [ -n "$out" ]; then
845			good="$good\\n$out"
846		else
847			bad="$bad\\n$key: nothing appropriate"
848			rval=1
849		fi
850	done
851
852	# Strip leading carriage return.
853	good=${good#\\n}
854	bad=${bad#\\n}
855
856	if [ -n "$good" ]; then
857		echo -e "$good" | $MANPAGER
858	fi
859
860	if [ -n "$bad" ]; then
861		echo -e "$bad" >&2
862	fi
863
864	exit $rval
865}
866
867# Usage: setup_cattool page
868# Finds an appropriate decompressor based on extension
869setup_cattool() {
870	case "$1" in
871	*.bz)	cattool='/usr/bin/bzcat' ;;
872	*.bz2)	cattool='/usr/bin/bzcat' ;;
873	*.gz)	cattool='/usr/bin/zcat' ;;
874	*.lzma)	cattool='/usr/bin/lzcat' ;;
875	*.xz)	cattool='/usr/bin/xzcat' ;;
876	*)	cattool='/usr/bin/zcat -f' ;;
877	esac
878}
879
880# Usage: setup_pager
881# Correctly sets $MANPAGER
882setup_pager() {
883	# Setup pager.
884	if [ -z "$MANPAGER" ]; then
885		if [ -n "$MANCOLOR" ]; then
886			MANPAGER="less -sR"
887		else
888			if [ -n "$PAGER" ]; then
889				MANPAGER="$PAGER"
890			else
891				MANPAGER="more -s"
892			fi
893		fi
894	fi
895	decho "Using pager: $MANPAGER"
896}
897
898# Usage: trim string
899# Trims whitespace from beginning and end of a variable
900trim() {
901	tstr=$1
902	while true; do
903		case "$tstr" in
904		[\ \	]*)	tstr="${tstr##[\ \	]}" ;;
905		*[\ \	])	tstr="${tstr%%[\ \	]}" ;;
906		*)		break ;;
907		esac
908	done
909}
910
911# Usage: whatis_parse_args "$@"
912# Parse commandline args for whatis and apropos.
913whatis_parse_args() {
914	local cmd_arg
915	while getopts 'd' cmd_arg; do
916		case "${cmd_arg}" in
917		d)	debug=$(( $debug + 1 )) ;;
918		*)	whatis_usage ;;
919		esac
920	done >&2
921
922	shift $(( $OPTIND - 1 ))
923
924	keywords="$*"
925}
926
927# Usage: whatis_usage
928# Display usage for the whatis/apropos utility.
929whatis_usage() {
930	echo "usage: $cmd [-d] keyword [...]"
931	exit 1
932}
933
934
935
936# Supported commands
937do_apropos() {
938	search_whatis apropos "$@"
939}
940
941do_man() {
942	man_parse_args "$@"
943	if [ -z "$pages" ]; then
944		echo 'What manual page do you want?' >&2
945		exit 1
946	fi
947	man_setup
948
949	for page in $pages; do
950		decho "Searching for $page"
951		man_find_and_display "$page"
952	done
953
954	exit ${ret:-0}
955}
956
957do_manpath() {
958	manpath_parse_args "$@"
959	if [ -z "$qflag" ]; then
960		manpath_warnings
961	fi
962	if [ -n "$Lflag" ]; then
963		build_manlocales
964		echo $MANLOCALES
965	else
966		build_manpath
967		echo $MANPATH
968	fi
969	exit 0
970}
971
972do_whatis() {
973	search_whatis whatis "$@"
974}
975
976# User's PATH setting decides on the groff-suite to pick up.
977EQN=eqn
978NROFF='groff -S -P-h -Wall -mtty-char -man'
979PIC=pic
980REFER=refer
981TBL=tbl
982TROFF='groff -S -man'
983VGRIND=vgrind
984
985LOCALE=/usr/bin/locale
986STTY=/bin/stty
987SYSCTL=/sbin/sysctl
988
989debug=0
990man_default_sections='1:8:2:3:n:4:5:6:7:9:l'
991man_default_path='/usr/share/man:/usr/share/openssl/man:/usr/local/man'
992cattool='/usr/bin/zcat -f'
993
994config_global='/etc/man.conf'
995
996# This can be overridden via a setting in /etc/man.conf.
997config_local='/usr/local/etc/man.d/*.conf'
998
999# Set noglobbing for now. I don't want spurious globbing.
1000set -f
1001
1002case "$0" in
1003*apropos)	do_apropos "$@" ;;
1004*manpath)	do_manpath "$@" ;;
1005*whatis)	do_whatis "$@" ;;
1006*)		do_man "$@" ;;
1007esac
1008