git: 2f99190a9234 - stable/14 - man: Exit cleanly on SIGPIPE.

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Wed, 21 May 2025 16:57:39 UTC
The branch stable/14 has been updated by des:

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

commit 2f99190a9234b119bcec1e4645e87d4e6016e5a5
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2025-05-16 14:56:13 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2025-05-21 16:55:37 +0000

    man: Exit cleanly on SIGPIPE.
    
    The first attempt at addressing this simply suppressed SIGPIPE, which
    resulted in mandoc printing out error messages instead.  This was then
    reverted, but the pipefail was (correctly) left in, so man still
    returned a nonzero exit code if you quit a page before the end.
    
    PR:             223516, 279542
    Fixes:          14a5c1068d37, a85d870007e7
    MFC after:      1 week
    Reviewed by:    ziaee, kevans
    Differential Revision:  https://reviews.freebsd.org/D50302
    
    (cherry picked from commit fbaba7aa432257a9b787edc6bfdbfbde94f2e0d5)
---
 usr.bin/man/man.sh | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/usr.bin/man/man.sh b/usr.bin/man/man.sh
index 341a57c6d7dc..b1cfc6c41752 100755
--- a/usr.bin/man/man.sh
+++ b/usr.bin/man/man.sh
@@ -33,7 +33,10 @@
 # it is better to terminate it.
 ulimit -t 20
 
-# do not ignore the exit status of roff tools
+# Do not ignore the exit codes of roff tools, as they may indicate a
+# problem with the page being rendered.  Note that this also causes a
+# nonzero exit when the user quits reading before reaching the end, so
+# we need to look out for and deal with that specific case.
 set -o pipefail
 
 # Usage: add_to_manpath path
@@ -1055,6 +1058,16 @@ do_man() {
 		man_find_and_display "$page"
 	done
 
+	# The user will very commonly quit reading the page before
+	# reaching the bottom.  Depending on the length of the page
+	# and the pager's buffer size, this may result in a SIGPIPE.
+	# This is normal, so convert that exit code to zero.
+	if [ ${ret:-0} -gt 128 ]; then
+		if [ "$(kill -l "${ret}")" = "PIPE" ]; then
+			ret=0
+		fi
+	fi
+
 	exit ${ret:-0}
 }