git: 8edb6fb572f2 - main - man(1): Support custom sections

From: Warner Losh <imp_at_FreeBSD.org>
Date: Tue, 27 Jun 2023 15:04:23 UTC
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=8edb6fb572f29578877fdac3c30f718e589a0360

commit 8edb6fb572f29578877fdac3c30f718e589a0360
Author:     Mohamed Akram <mohd.akram@outlook.com>
AuthorDate: 2023-06-27 13:51:24 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2023-06-27 13:51:27 +0000

    man(1): Support custom sections
    
    PR: 271830
    Signed-off-by: Mohamed Akram <mohd.akram@outlook.com>
    Reviewed by: imp
    Pull Request: https://github.com/freebsd/freebsd-src/pull/764
---
 usr.bin/man/man.sh | 70 +++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 45 insertions(+), 25 deletions(-)

diff --git a/usr.bin/man/man.sh b/usr.bin/man/man.sh
index aa7d1287405a..2b20c4394733 100755
--- a/usr.bin/man/man.sh
+++ b/usr.bin/man/man.sh
@@ -69,6 +69,25 @@ build_manlocales() {
 	decho "Available manual locales: $MANLOCALES"
 }
 
+# Usage: build_mansect
+# Builds a correct MANSECT variable.
+build_mansect() {
+	# If the user has set mansect, who are we to argue.
+	if [ -n "$MANSECT" ]; then
+		return
+	fi
+
+	parse_configs
+
+	# Trim leading colon
+	MANSECT=${mansect#:}
+
+	if [ -z "$MANSECT" ]; then
+		MANSECT=$man_default_sections
+	fi
+	decho "Using manual sections: $MANSECT"
+}
+
 # Usage: build_manpath
 # Builds a correct MANPATH variable.
 build_manpath() {
@@ -548,10 +567,10 @@ man_find_and_display() {
 	fi
 }
 
-# Usage: man_parse_args "$@"
+# Usage: man_parse_opts "$@"
 # Parses commandline options for man.
-man_parse_args() {
-	local IFS cmd_arg
+man_parse_opts() {
+	local cmd_arg
 
 	OPTIND=1
 	while getopts 'K:M:P:S:adfhkm:op:tw' cmd_arg; do
@@ -601,19 +620,6 @@ man_parse_args() {
 		do_apropos "$@"
 		exit
 	fi
-
-	IFS=:
-	for sect in $man_default_sections; do
-		if [ "$sect" = "$1" ]; then
-			decho "Detected manual section as first arg: $1"
-			MANSECT="$1"
-			shift
-			break
-		fi
-	done
-	unset IFS
-
-	pages="$*"
 }
 
 # Usage: man_setup
@@ -633,14 +639,8 @@ man_setup() {
 	decho "Using architecture: $MACHINE_ARCH:$MACHINE"
 
 	setup_pager
-
-	# Setup manual sections to search.
-	if [ -z "$MANSECT" ]; then
-		MANSECT=$man_default_sections
-	fi
-	decho "Using manual sections: $MANSECT"
-
 	build_manpath
+	build_mansect
 	man_setup_locale
 	man_setup_width
 }
@@ -787,6 +787,10 @@ parse_file() {
 				trim "${line#MANCONFIG}"
 				config_local="$tstr"
 				;;
+		MANSECT*)	decho "    MANSECT" 3
+				trim "${line#MANSECT}"
+				mansect="$mansect:$tstr"
+				;;
 		# Set variables in the form of FOO_BAR
 		*_*[\ \	]*)	var="${line%%[\ \	]*}"
 				trim "${line#$var}"
@@ -1008,12 +1012,28 @@ do_full_search() {
 }
 
 do_man() {
-	man_parse_args "$@"
+	local IFS
+
+	man_parse_opts "$@"
+	man_setup
+
+	shift $(( $OPTIND - 1 ))
+	IFS=:
+	for sect in $MANSECT; do
+		if [ "$sect" = "$1" ]; then
+			decho "Detected manual section as first arg: $1"
+			MANSECT="$1"
+			shift
+			break
+		fi
+	done
+	unset IFS
+	pages="$*"
+
 	if [ -z "$pages" -a -z "${Kflag}" ]; then
 		echo 'What manual page do you want?' >&2
 		exit 1
 	fi
-	man_setup
 
 	if [ ! -z "${Kflag}" ]; then
 		# Short circuit because -K flag does a sufficiently