From 657288a08cb1e1fe38f1cfcf350f2e328bcd7130 Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Thu, 28 Aug 2025 08:45:51 -0700
Subject: [PROPOSED 2/2] Formatted man pages (*.txt) now use UTF-8

(Inspired by G. Branden Robinson.)
* Makefile (MANFLAGS): New macro.
(MANTXTS): Use it.
* NEWS: Mention this.
* workman.sh (manflags): New var.  Accumulate options into it, and
pass them to groff and to mandoc.  Try groff first and use -dAD=l
-rHY=0 -mtty-char as well when using groff.  Fall back on
traditional nroff last, after mdoc.  When trying groff, make sure
all the flags work, so that we do not use old groff such as the
groff 1.16.1 (2000) shipped with Solaris 10 and still supported.
Use "$@" rather than ${1+"$@"} as we no longer need to port to
pre-POSIX shells that lack "$@".
---
 Makefile   |  8 +++++++-
 NEWS       |  5 +++++
 workman.sh | 38 +++++++++++++++++++++-----------------
 3 files changed, 33 insertions(+), 18 deletions(-)

diff --git a/Makefile b/Makefile
index 99407112..7d77da60 100644
--- a/Makefile
+++ b/Makefile
@@ -219,6 +219,12 @@ PACKRATLIST=
 
 UTF8_LOCALE=	en_US.utf8
 
+# Extra flags for producing man page files like tzfile.5.txt.
+# These flags are used only if groff (or mandoc) is present.
+# Each option should begin with "-" and should lack shell metacharacters.
+# Plausible options include -Tascii and -Tutf8.
+MANFLAGS=	-Tutf8
+
 # Non-default libraries needed to link.
 # On some hosts, this should have -lintl unless CFLAGS has -DHAVE_GETTEXT=0.
 LDLIBS=
@@ -1075,7 +1081,7 @@ zdump.8.txt:	zdump.8
 zic.8.txt:	zic.8
 
 $(MANTXTS):	workman.sh
-		LC_ALL=C sh workman.sh $(@:.txt=) >$@.out
+		LC_ALL=C sh workman.sh $(MANFLAGS) $(@:.txt=) >$@.out
 		mv $@.out $@
 
 # Set file timestamps deterministically if possible,
diff --git a/NEWS b/NEWS
index a7916e69..9163c1fe 100644
--- a/NEWS
+++ b/NEWS
@@ -17,6 +17,11 @@ Unreleased, experimental changes
     reproducible timestamps.  Formerly, only the contents of the
     compressed tarballs had reproducible timestamps.
 
+    By default, distributed formatted man pages (*.txt) now use UTF-8
+    and are left-adjusted more consistently.  A new Makefile macro
+    MANFLAGS can override these defaults.  (Thanks to G. Branden
+    Robinson for inspiring these changes.)
+
   Changes to commentary
 
     The leapseconds file contains commentary about the IERS and NIST
diff --git a/workman.sh b/workman.sh
index ad1a9532..91621b10 100644
--- a/workman.sh
+++ b/workman.sh
@@ -4,29 +4,35 @@
 # This file is in the public domain, so clarified as of
 # 2009-05-17 by Arthur David Olson.
 
-if (type nroff && type perl) >/dev/null 2>&1; then
-
-  # Tell groff not to emit SGR escape sequences (ANSI color escapes).
-  export GROFF_NO_SGR=1
+manflags=
+while
+  case $1 in
+    -*) :;;
+    *) false;;
+  esac
+do
+  manflags="$manflags $1"
+  shift
+done
 
+groff="groff -dAD=l -rHY=0 $manflags -mtty-char -man -ww -P-bcou"
+if ($groff) </dev/null >/dev/null 2>&1; then
+  $groff "$@"
+elif (type mandoc && type col) >/dev/null 2>&1; then
+  mandoc $manflags -man "$@" | col -bx
+elif (type nroff && type perl) >/dev/null 2>&1; then
   printf '%s\n' '.
-.\" -- Tailor groff -man --
-.
-.\" Left-adjust and do not hyphenate.
-.ds AD l
-.nr HY 0
-.
-.\" -- Tailor traditional troff -man --
-.
 .\" Left-adjust and do not hyphenate.
 .am TH
 .na
 .hy 0
 ..
-.\" Omit page headers and footers.
+.\" Omit internal page headers and footers.
+.\" Unfortunately this also omits the starting header and ending footer,
+.\" but that is the best old nroff can easily do.
 .rm }H
 .rm }F
-.' | nroff -man - ${1+"$@"} | perl -ne '
+.' | nroff -man - "$@" | perl -ne '
 	binmode STDIN, '\'':encoding(utf8)'\'';
 	binmode STDOUT, '\'':encoding(utf8)'\'';
 	chomp;
@@ -44,9 +50,7 @@ if (type nroff && type perl) >/dev/null 2>&1; then
 		$didprint = 1;
 	}
   '
-elif (type mandoc && type col) >/dev/null 2>&1; then
-  mandoc -man -T ascii "$@" | col -bx
 else
-  echo >&2 "$0: please install nroff and perl, or mandoc and col"
+  echo >&2 "$0: please install groff, or mandoc and col"
   exit 1
 fi
-- 
2.48.1

