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.

Approved by:	re (cperciva)
PR:		223516, 279542
Fixes:		14a5c1068d, a85d870007
MFC after:	1 week
Reviewed by:	ziaee, kevans
Differential Revision:	https://reviews.freebsd.org/D50302

(cherry picked from commit fbaba7aa43)
(cherry picked from commit 2f99190a9234b119bcec1e4645e87d4e6016e5a5)
This commit is contained in:
Dag-Erling Smørgrav 2025-05-16 16:56:13 +02:00
parent 6b6d6448fe
commit 84b971c918

View file

@ -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}
}