From 93adc51c0135d274cea75f2de2b328480c72a94c Mon Sep 17 00:00:00 2001
From: Yugo Nagata <nagata@sraoss.co.jp>
Date: Tue, 19 Nov 2024 19:19:14 +0900
Subject: [PATCH v3 3/3] Check whether iconv exists for detecting non-latin1
 characters

---
 configure              | 65 ++++++++++++++++++++++++++++++++++++++----
 configure.ac           |  1 +
 doc/src/sgml/Makefile  |  6 +++-
 src/Makefile.global.in |  1 +
 4 files changed, 67 insertions(+), 6 deletions(-)

diff --git a/configure b/configure
index f58eae1baa..eaf02c5660 100755
--- a/configure
+++ b/configure
@@ -632,6 +632,7 @@ PG_VERSION_NUM
 LDFLAGS_EX_BE
 PROVE
 DBTOEPUB
+ICONV
 FOP
 XSLTPROC
 XMLLINT
@@ -14728,7 +14729,7 @@ else
     We can't simply define LARGE_OFF_T to be 9223372036854775807,
     since some C++ compilers masquerading as C compilers
     incorrectly reject 9223372036854775807.  */
-#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62))
+#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31))
   int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721
 		       && LARGE_OFF_T % 2147483647 == 1)
 		      ? 1 : -1];
@@ -14774,7 +14775,7 @@ else
     We can't simply define LARGE_OFF_T to be 9223372036854775807,
     since some C++ compilers masquerading as C compilers
     incorrectly reject 9223372036854775807.  */
-#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62))
+#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31))
   int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721
 		       && LARGE_OFF_T % 2147483647 == 1)
 		      ? 1 : -1];
@@ -14798,7 +14799,7 @@ rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
     We can't simply define LARGE_OFF_T to be 9223372036854775807,
     since some C++ compilers masquerading as C compilers
     incorrectly reject 9223372036854775807.  */
-#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62))
+#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31))
   int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721
 		       && LARGE_OFF_T % 2147483647 == 1)
 		      ? 1 : -1];
@@ -14843,7 +14844,7 @@ else
     We can't simply define LARGE_OFF_T to be 9223372036854775807,
     since some C++ compilers masquerading as C compilers
     incorrectly reject 9223372036854775807.  */
-#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62))
+#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31))
   int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721
 		       && LARGE_OFF_T % 2147483647 == 1)
 		      ? 1 : -1];
@@ -14867,7 +14868,7 @@ rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
     We can't simply define LARGE_OFF_T to be 9223372036854775807,
     since some C++ compilers masquerading as C compilers
     incorrectly reject 9223372036854775807.  */
-#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62))
+#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31))
   int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721
 		       && LARGE_OFF_T % 2147483647 == 1)
 		      ? 1 : -1];
@@ -18535,6 +18536,60 @@ $as_echo_n "checking for FOP... " >&6; }
 $as_echo "$FOP" >&6; }
 fi
 
+if test -z "$ICONV"; then
+  for ac_prog in iconv
+do
+  # Extract the first word of "$ac_prog", so it can be a program name with args.
+set dummy $ac_prog; ac_word=$2
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5
+$as_echo_n "checking for $ac_word... " >&6; }
+if ${ac_cv_path_ICONV+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  case $ICONV in
+  [\\/]* | ?:[\\/]*)
+  ac_cv_path_ICONV="$ICONV" # Let the user override the test with a path.
+  ;;
+  *)
+  as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
+for as_dir in $PATH
+do
+  IFS=$as_save_IFS
+  test -z "$as_dir" && as_dir=.
+    for ac_exec_ext in '' $ac_executable_extensions; do
+  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+    ac_cv_path_ICONV="$as_dir/$ac_word$ac_exec_ext"
+    $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
+    break 2
+  fi
+done
+  done
+IFS=$as_save_IFS
+
+  ;;
+esac
+fi
+ICONV=$ac_cv_path_ICONV
+if test -n "$ICONV"; then
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ICONV" >&5
+$as_echo "$ICONV" >&6; }
+else
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+fi
+
+
+  test -n "$ICONV" && break
+done
+
+else
+  # Report the value of ICONV in configure's output in all cases.
+  { $as_echo "$as_me:${as_lineno-$LINENO}: checking for ICONV" >&5
+$as_echo_n "checking for ICONV... " >&6; }
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ICONV" >&5
+$as_echo "$ICONV" >&6; }
+fi
+
 if test -z "$DBTOEPUB"; then
   for ac_prog in dbtoepub
 do
diff --git a/configure.ac b/configure.ac
index 82c5009e3e..1196f857cf 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2321,6 +2321,7 @@ fi
 PGAC_PATH_PROGS(XMLLINT, xmllint)
 PGAC_PATH_PROGS(XSLTPROC, xsltproc)
 PGAC_PATH_PROGS(FOP, fop)
+PGAC_PATH_PROGS(ICONV, iconv)
 PGAC_PATH_PROGS(DBTOEPUB, dbtoepub)
 
 #
diff --git a/doc/src/sgml/Makefile b/doc/src/sgml/Makefile
index 820ae7c456..416dfc6c89 100644
--- a/doc/src/sgml/Makefile
+++ b/doc/src/sgml/Makefile
@@ -36,6 +36,10 @@ ifndef FOP
 FOP = $(missing) fop
 endif
 
+ifndef ICONV
+ICONV = $(missing) iconv
+endif
+
 PANDOC = pandoc
 
 XMLINCLUDE = --path . --path $(srcdir)
@@ -271,7 +275,7 @@ check-nbsp:
 
 # Non-Latin1 characters cannot be displayed in PDF.
 check-non-latin1:
-	@ ( $(PERL) -ne '/[\x80-\xFF]/ and `iconv -f UTF-8 -t ISO-8859-1 "$$ARGV" 2>/dev/null` and print("$$ARGV:$$_"),$$n++; END {exit($$n>0)}' \
+	@ ( $(PERL) -ne '/[\x80-\xFF]/ and `LANG=C ${ICONV} -f UTF-8 -t ISO-8859-1 "$$ARGV"` and print("$$ARGV:$$_"),$$n++; END {exit($$n>0)}' \
 	  $(wildcard $(srcdir)/*.sgml $(srcdir)/ref/*.sgml $(srcdir)/*.xsl $(srcdir)/images/*.xsl) ) || \
 	(echo "Non-Latin1 characters appear in SGML/XML files" 1>&2;  exit 1)
 
diff --git a/src/Makefile.global.in b/src/Makefile.global.in
index 0f38d712d1..f3bd700664 100644
--- a/src/Makefile.global.in
+++ b/src/Makefile.global.in
@@ -517,6 +517,7 @@ STRIP_SHARED_LIB = @STRIP_SHARED_LIB@
 
 DBTOEPUB	= @DBTOEPUB@
 FOP				= @FOP@
+ICONV			= @ICONV@
 XMLLINT			= @XMLLINT@
 XSLTPROC		= @XSLTPROC@
 
-- 
2.34.1

