+# Select a UTF-8 locale.
+get_locale() {
+ [ -n "${HAVE_WCHAR}" ] && [ "${HAVE_WCHAR}" -eq 0 ] && return 0
+ ismanual UTF8_LOCALE UTF8_LOCALE "$UTF8_LOCALE" && return 0
+ echo "testing UTF8_LOCALE ..." 1>&3
+ UTF8_LOCALE=`locale -a | grep -i '^en_US\.UTF-*8$' | head -n 1`
+ if [ -z "${UTF8_LOCALE}" ]; then
+ UTF8_LOCALE=`locale -a | grep -i '\.UTF-*8' | head -n 1`
+ [ -n "${UTF8_LOCALE}" ] || return 1
+ fi
+ echo "selected UTF8_LOCALE=${UTF8_LOCALE}" 1>&2
+ echo "selected UTF8_LOCALE=${UTF8_LOCALE}" 1>&3
+ echo 1>&3
+ return 0;
+}
+
+# --- operating system -------------------------------------------------
+
+if [ -n "${OSENUM}" ]; then
+ echo "OSENUM specified manually: ${OSENUM}" 1>&2
+ echo "OSENUM specified manually: ${OSENUM}" 1>&3
+else
+ OSDETECT=`uname`
+ if [ "X${OSDETECT}" = "XNetBSD" ]; then
+ OSENUM=MANDOC_OS_NETBSD
+ elif [ "X${OSDETECT}" = "XOpenBSD" ]; then
+ OSENUM=MANDOC_OS_OPENBSD
+ else
+ OSENUM=MANDOC_OS_OTHER
+ fi
+ echo "tested operating system: ${OSDETECT} -> OSENUM=${OSENUM}" 1>&2
+ echo "tested operating system: ${OSDETECT} -> OSENUM=${OSENUM}" 1>&3
+ unset OSDETECT
+fi
+echo 1>&3
+
+# --- compiler options -------------------------------------------------
+
+DEFCFLAGS="-g -W -Wall -Wmissing-prototypes -Wstrict-prototypes -Wwrite-strings -Wno-unused-parameter"
+
+if [ -n "${CFLAGS}" ]; then
+ COMP="${CC} ${CFLAGS} -Wno-unused -Werror"
+else
+ COMP="${CC} ${DEFCFLAGS} -Wno-unused -Werror"
+fi
+echo -n "tested ${CC} -W: " 1>&2
+echo -n "testing ${CC} -W: " 1>&3
+runtest noop WFLAG || true
+
+if [ -n "${CFLAGS}" ]; then
+ echo "CFLAGS specified manually:" 1>&3
+elif [ ${HAVE_WFLAG} -eq 0 ]; then
+ CFLAGS="-g"
+else
+ CFLAGS="${DEFCFLAGS}"
+fi
+echo "selected CFLAGS=\"${CFLAGS}\"" 1>&2
+echo "selected CFLAGS=\"${CFLAGS}\"" 1>&3
+echo 1>&3
+
+COMP="${CC} ${CFLAGS}"
+[ ${HAVE_WFLAG} -eq 0 ] || COMP="${COMP} -Wno-unused -Werror"
+
+if [ -n "${STATIC}" ]; then
+ echo "selected STATIC=\"${STATIC}\" (manual)" 1>&2
+ echo "selected STATIC=\"${STATIC}\" (manual)" 1>&3
+ echo 1>&3
+else
+ runtest noop STATIC -static || true
+ [ ${HAVE_STATIC} -eq 0 ] || STATIC="-static"
+ echo "selected STATIC=\"${STATIC}\"" 1>&2
+ echo "selected STATIC=\"${STATIC}\"" 1>&3
+ echo 1>&3
+fi
+
+# --- tests for config.h ----------------------------------------------
+
+# --- library functions ---
+runtest dirent-namlen DIRENT_NAMLEN || true
+runtest be32toh ENDIAN || true
+runtest be32toh SYS_ENDIAN -DSYS_ENDIAN || true
+runtest EFTYPE EFTYPE || true
+runtest err ERR || true
+runtest getline GETLINE || true
+singletest getsubopt GETSUBOPT || \
+ runtest getsubopt GETSUBOPT -D_GNU_SOURCE || true
+runtest isblank ISBLANK || true
+runtest mkdtemp MKDTEMP || true
+runtest ntohl NTOHL || true
+runtest O_DIRECTORY O_DIRECTORY || true
+runtest PATH_MAX PATH_MAX || true
+runtest pledge PLEDGE || true
+runtest sandbox_init SANDBOX_INIT || true
+runtest progname PROGNAME || true
+singletest reallocarray REALLOCARRAY || \
+ runtest reallocarray REALLOCARRAY -D_OPENBSD_SOURCE || true
+singletest recallocarray RECALLOCARRAY || \
+ runtest recallocarray RECALLOCARRAY -D_OPENBSD_SOURCE || true
+runtest rewb-bsd REWB_BSD || true
+runtest rewb-sysv REWB_SYSV || true
+singletest strcasestr STRCASESTR || \
+ runtest strcasestr STRCASESTR -D_GNU_SOURCE || true
+runtest stringlist STRINGLIST || true
+runtest strlcat STRLCAT || true
+runtest strlcpy STRLCPY || true
+runtest strndup STRNDUP || true
+singletest strptime STRPTIME || \
+ runtest strptime STRPTIME -D_GNU_SOURCE || true
+runtest strsep STRSEP || true
+singletest strtonum STRTONUM || \
+ runtest strtonum STRTONUM -D_OPENBSD_SOURCE || true
+singletest vasprintf VASPRINTF || \
+ runtest vasprintf VASPRINTF -D_GNU_SOURCE || true
+
+if [ ${HAVE_ENDIAN} -eq 0 -a \
+ ${HAVE_SYS_ENDIAN} -eq 0 -a \
+ ${HAVE_NTOHL} -eq 0 ]; then
+ echo "FATAL: no endian conversion functions found" 1>&2
+ echo "FATAL: no endian conversion functions found" 1>&3
+ exit 1
+fi
+
+if ismanual fts FTS ${HAVE_FTS}; then
+ HAVE_FTS_COMPARE_CONST=0
+elif runtest fts FTS_COMPARE_CONST -DFTS_COMPARE_CONST; then
+ HAVE_FTS=1
+else
+ runtest fts FTS || true
+fi
+
+if ismanual "less -T" LESS_T ${HAVE_LESS_T}; then
+ :
+elif less -ET /dev/null test-noop.c 1>/dev/null 2>&3; then
+ HAVE_LESS_T=1
+ echo "tested less -T: yes" 1>&2
+ echo "tested less -T: yes" 1>&3
+ echo 1>&3
+else
+ HAVE_LESS_T=0
+ echo "tested less -T: no" 1>&2
+ echo "tested less -T: no" 1>&3
+ echo 1>&3
+fi
+
+# --- wide character and locale support ---
+if get_locale; then
+ singletest wchar WCHAR -DUTF8_LOCALE=\"${UTF8_LOCALE}\" || \
+ runtest wchar WCHAR -D_GNU_SOURCE \
+ -DUTF8_LOCALE=\"${UTF8_LOCALE}\" || true
+else
+ HAVE_WCHAR=0
+ echo "tested wchar: no (no UTF8_LOCALE)" 1>&2
+ echo "tested wchar: no (no UTF8_LOCALE)" 1>&3
+ echo 1>&3
+fi
+
+# --- nanosleep ---
+if [ -n "${LD_NANOSLEEP}" ]; then
+ runtest nanosleep NANOSLEEP "${LD_NANOSLEEP}" || true
+elif singletest nanosleep NANOSLEEP; then
+ :
+elif runtest nanosleep NANOSLEEP "-lrt"; then
+ LD_NANOSLEEP="-lrt"
+fi
+if [ "${HAVE_NANOSLEEP}" -eq 0 ]; then
+ echo "FATAL: nanosleep: no" 1>&2
+ echo "FATAL: nanosleep: no" 1>&3
+ exit 1
+fi
+
+if [ ${BUILD_CATMAN} -gt 0 ]; then
+ # --- recvmsg ---
+ if [ -n "${LD_RECVMSG}" ]; then
+ runtest recvmsg RECVMSG "${LD_RECVMSG}" || true
+ elif singletest recvmsg RECVMSG; then
+ :
+ elif runtest recvmsg RECVMSG "-lsocket"; then
+ LD_RECVMSG="-lsocket"
+ fi
+ if [ "${HAVE_RECVMSG}" -eq 0 ]; then
+ echo "FATAL: recvmsg: no" 1>&2
+ echo "FATAL: recvmsg: no" 1>&3
+ echo "Without recvmsg(2), you cannot BUILD_CATMAN." 1>&2
+ exit 1
+ fi
+
+ # --- cmsg ---
+ if singletest cmsg CMSG; then
+ :
+ elif runtest cmsg CMSG "-D_XPG4_2"; then
+ HAVE_CMSG_XPG42=1
+ fi
+ if [ "${HAVE_CMSG}" -eq 0 ]; then
+ echo "FATAL: cmsg: no" 1>&2
+ echo "FATAL: cmsg: no" 1>&3
+ echo "Without CMSG_FIRSTHDR(3), you cannot BUILD_CATMAN." 1>&2
+ exit 1
+ fi
+fi
+
+# --- ohash ---
+if ismanual ohash OHASH "${HAVE_OHASH}"; then
+ :
+elif [ -n "${LD_OHASH}" ]; then
+ runtest ohash OHASH "${LD_OHASH}" || true
+elif singletest ohash OHASH; then
+ :
+elif runtest ohash OHASH "-lutil"; then
+ LD_OHASH="-lutil"
+fi
+if [ "${HAVE_OHASH}" -eq 0 ]; then
+ LD_OHASH=
+fi
+
+# --- LDADD ---
+LDADD="${LDADD} ${LD_NANOSLEEP} ${LD_RECVMSG} ${LD_OHASH} -lz"
+echo "selected LDADD=\"${LDADD}\"" 1>&2
+echo "selected LDADD=\"${LDADD}\"" 1>&3
+echo 1>&3
+
+# --- write config.h ---
+
+exec > config.h
+
+cat << __HEREDOC__
+#ifdef __cplusplus
+#error "Do not use C++. See the INSTALL file."
+#endif
+
+#if !defined(__GNUC__) || (__GNUC__ < 4)
+#define __attribute__(x)
+#endif
+
+__HEREDOC__
+
+[ ${NEED_GNU_SOURCE} -eq 0 ] || echo "#define _GNU_SOURCE"
+[ ${NEED_OPENBSD_SOURCE} -eq 0 ] || echo "#define _OPENBSD_SOURCE"
+
+[ ${HAVE_GETLINE} -eq 0 -o \
+ ${HAVE_REALLOCARRAY} -eq 0 -o ${HAVE_RECALLOCARRAY} -eq 0 -o \
+ ${HAVE_STRLCAT} -eq 0 -o ${HAVE_STRLCPY} -eq 0 -o \
+ ${HAVE_STRNDUP} -eq 0 ] \
+ && echo "#include <sys/types.h>"
+[ ${HAVE_VASPRINTF} -eq 0 ] && echo "#include <stdarg.h>"
+[ ${HAVE_GETLINE} -eq 0 ] && echo "#include <stdio.h>"
+