Bug 271830 - man: ignores MANSECT and -S
Summary: man: ignores MANSECT and -S
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 13.2-RELEASE
Hardware: Any Any
: --- Affects Only Me
Assignee: freebsd-bugs (Nobody)
URL: https://github.com/freebsd/freebsd-sr...
Keywords: needs-qa
Depends on:
Blocks:
 
Reported: 2023-06-04 22:58 UTC by Mohamed Akram
Modified: 2024-02-12 15:49 UTC (History)
2 users (show)

See Also:


Attachments
patch to fix the issue (610 bytes, patch)
2023-06-05 09:16 UTC, Mohamed Akram
no flags Details | Diff
improved patch (1.11 KB, patch)
2023-06-05 10:10 UTC, Mohamed Akram
no flags Details | Diff
fix issues in last patch (1.88 KB, patch)
2023-06-05 10:42 UTC, Mohamed Akram
no flags Details | Diff
fix whatis and apropos (1.71 KB, patch)
2023-06-05 11:01 UTC, Mohamed Akram
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Mohamed Akram 2023-06-04 22:58:15 UTC
The following should work whether MANSECT is set in a man.conf file, via an environment variable, or via the -S flag. It fails in all cases.

To reproduce:

MANSECT=1p man 1p grep

or

man -S 1p 1p grep

Expected output:

No manual entry for grep

Actual output:

No manual entry for 1p
No manual entry for grep


To reproduce:

MANSECT=3 man 1 grep

or

man -S 3 1 grep

Expected output:

No manual entry for 1
No manual entry for grep

Actual output:

grep(1) man page is displayed
Comment 1 Yuri Pankov freebsd_committer freebsd_triage 2023-06-05 07:13:02 UTC
There are 2 possible issues here:

1. Running "man -S 1p 1 grep" displays the grep(1) man page -- explicitly specifying mansect overrides -S/MANSECT.
2. "No manual entry for 1p" -- 1p is missing from the default sections list, so it's treated as man page name and not section.

What is the real use case here that is broken?
Comment 2 Mohamed Akram 2023-06-05 08:57:07 UTC
The reason for filing this issue is to allow using custom sections, such as man1p (used on Linux for POSIX man pages). As can be seen in the first instance, because man is ignoring MANSECT it thinks 1p is a page and not a section so this is impossible.

The second variation I posted is just to illustrate the same bug in a different way. Fixing the first will likely fix this. It's more simple and straightforward if what is considered a section is what the user specifies, falling back to the default otherwise. This will lead to consistent behavior instead of special casing "default sections" (this is how it works on Linux as well).

Also, FYI, setting MANSECT in a file doesn't work at all because it reads the config file after already deciding on the default list.
Comment 3 Mohamed Akram 2023-06-05 09:16:58 UTC
Created attachment 242606 [details]
patch to fix the issue
Comment 4 Mohamed Akram 2023-06-05 09:46:19 UTC
Comment on attachment 242606 [details]
patch to fix the issue

--- man.sh.orig	2023-06-05 09:11:06.617877000 +0000
+++ man.sh	2023-06-05 09:43:16.432524000 +0000
@@ -562,7 +562,7 @@
 		P)	MANPAGER=$OPTARG ;;
 		S)	MANSECT=$OPTARG ;;
 		a)	aflag=aflag ;;
-		d)	debug=$(( $debug + 1 )) ;;
+		d)      ;; # handled in man_setup
 		f)	fflag=fflag ;;
 		h)	man_usage 0 ;;
 		k)	kflag=kflag ;;
@@ -603,7 +603,7 @@
 	fi
 
 	IFS=:
-	for sect in $man_default_sections; do
+	for sect in $MANSECT; do
 		if [ "$sect" = "$1" ]; then
 			decho "Detected manual section as first arg: $1"
 			MANSECT="$1"
@@ -619,6 +619,15 @@
 # Usage: man_setup
 # Setup various trivial but essential variables.
 man_setup() {
+	local cmd_arg
+	# Parse the debug flag prior to everything
+	OPTIND=1
+	while getopts ':d' cmd_arg; do
+		case "${cmd_arg}" in
+		d)	debug=$(( $debug + 1 )) ;;
+		esac
+	done
+
 	# Setup machine and architecture variables.
 	if [ -n "$mflag" ]; then
 		MACHINE_ARCH=${mflag%%:*}
@@ -787,8 +796,8 @@
 				trim "${line#MANCONFIG}"
 				config_local="$tstr"
 				;;
-		# Set variables in the form of FOO_BAR
-		*_*[\ \	]*)	var="${line%%[\ \	]*}"
+		# Set other variables
+		*[\ \	]*)	var="${line%%[\ \	]*}"
 				trim "${line#$var}"
 				eval "$var=\"$tstr\""
 				decho "    Parsed $var" 3
@@ -1008,12 +1017,12 @@
 }
 
 do_man() {
+	man_setup "$@"
 	man_parse_args "$@"
 	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
Comment 5 Mohamed Akram 2023-06-05 10:10:47 UTC
Created attachment 242612 [details]
improved patch
Comment 6 Mohamed Akram 2023-06-05 10:42:31 UTC
Created attachment 242613 [details]
fix issues in last patch
Comment 7 Mohamed Akram 2023-06-05 11:01:14 UTC
Created attachment 242614 [details]
fix whatis and apropos
Comment 8 Mohamed Akram 2023-06-05 16:32:20 UTC
Please ignore the patches, I've created a PR instead - https://github.com/freebsd/freebsd-src/pull/764.
Comment 9 commit-hook freebsd_committer freebsd_triage 2023-06-27 15:04:36 UTC
A commit in branch main references this bug:

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(-)
Comment 10 commit-hook freebsd_committer freebsd_triage 2024-02-12 15:49:49 UTC
A commit in branch stable/13 references this bug:

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(-)