git: b8f3f97c5ce2 - stable/13 - man(1): Support custom sections

From: Wolfram Schneider <wosch_at_FreeBSD.org>
Date: Mon, 12 Feb 2024 15:49:36 UTC
The branch stable/13 has been updated by wosch:

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

commit b8f3f97c5ce240c156fab0205e0e3938196ed1e2
Author:     Wolfram Schneider <wosch@FreeBSD.org>
AuthorDate: 2023-06-27 13:51:24 +0000
Commit:     Wolfram Schneider <wosch@FreeBSD.org>
CommitDate: 2024-02-12 15:41:01 +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
    
    (cherry picked from commit 8edb6fb572f29578877fdac3c30f718e589a0360 - needed for further bugfixes)
---
 usr.bin/man/man.sh | 76 ++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 48 insertions(+), 28 deletions(-)

diff --git a/usr.bin/man/man.sh b/usr.bin/man/man.sh
index 1d9eb423b707..6ddeb62a3764 100755
--- a/usr.bin/man/man.sh
+++ b/usr.bin/man/man.sh
@@ -68,6 +68,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() {
@@ -547,10 +566,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 'M:P:S:adfhkm:op:tw' cmd_arg; do
@@ -594,19 +613,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
@@ -626,14 +632,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
 }
@@ -780,6 +780,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}"
@@ -972,13 +976,29 @@ do_apropos() {
 }
 
 do_man() {
-	man_parse_args "$@"
-	if [ -z "$pages" ]; then
-		echo 'What manual page do you want?' >&2
-		exit 1
-	fi
+	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" ]; then
+                echo 'What manual page do you want?' >&2
+                exit 1
+        fi
+
 	for page in "$pages"; do
 		decho "Searching for \"$page\""
 		man_find_and_display "$page"