dnl Process this file with autoconf to produce a configure script.
AC_REVISION($Revision: 1.358 $)
AC_PREREQ(2.52.20231210)

### Use "configure -with-screen" to override the default configuration, which is
### termcap-based on unix systems.

dnl $Id: configure.in,v 1.358 2025/01/26 10:05:47 tom Exp $

define(MAKELIST, sh "$srcdir/filters/makelist.sh" "$srcdir/filters/genmake.mak")

dnl
AC_INIT(mktbls.c)
AC_CONFIG_HEADER(config.h:config_h.in)

CF_CHECK_CACHE
CF_WITH_SYSTYPE
AC_ARG_PROGRAM

###	checks for compiler
CF_PROG_CC
AC_PROG_CPP
AC_GCC_TRADITIONAL

###	checks for compiler characteristics
CF_BUILD_CC
AC_CONST
AC_C_INLINE
CF_CC_INIT_UNIONS
CF_XOPEN_SOURCE
CF_SIGWINCH

# force HPUX lex to use %pointer for $LEX
case $host_os in
(hpux*)
	CPPFLAGS="-DYYCHAR_ARRAY $CPPFLAGS"
	;;
esac
AC_DECL_YYTEXT

TRY_CFLAGS=
case $host_os in
(irix6.*)
	TRY_CFLAGS="$TRY_CFLAGS -woff 3970"
	;;
esac

dnl Collect tests for compiler options into one place
if test -n "$TRY_CFLAGS" ; then
	CF_CHECK_CFLAGS($TRY_CFLAGS)
fi

###	checks for alternative programs
CF_MAKEFLAGS
AC_PROG_INSTALL

AC_PROG_MAKE_SET
AC_PROG_AWK
AC_PROG_RANLIB
AC_PROG_LN_S
CF_AR_FLAGS

CF_DISABLE_ECHO
CF_PATHSEP
CF_LIB_PREFIX

###	options to control how much we build
BUILTHDRS="nebind.h neproto.h neexec.h nefunc.h nemode.h nename.h nevars.h nefkeys.h nefsms.h"
BUILTLIBS=
BUILTSRCS=

AC_MSG_CHECKING(if you wish to build only core functions)
CF_ARG_DISABLE(extensions,
	[  --disable-extensions    test: build only core functions],
	[DISABLE_EXTS=yes],
	[DISABLE_EXTS=no])
AC_MSG_RESULT($DISABLE_EXTS)
if test "$DISABLE_EXTS" = yes ; then
	AC_DEFINE(SMALLER,1,[Define to 1 to build only core functions])
	AC_DEFINE(FEWNAMES,1,[Define to 1 to build provide core function names])
	cf_extensions=no
else
	EXTRAOBJS="$EXTRAOBJS api\$o btree\$o history\$o isearch\$o select\$o statevar\$o ucrypt\$o xterm\$o"
	cf_extensions=yes
fi

AC_MSG_CHECKING(if you wish to disable shell/external commands)
CF_ARG_DISABLE(shell,
	[  --disable-shell         test: disable shell/external commands],
	[DISABLE_SHELL=yes],
	[DISABLE_SHELL=no])
AC_MSG_RESULT($DISABLE_SHELL)
if test "$DISABLE_SHELL" = yes ; then
	AC_DEFINE(OPT_SHELL,0,[Define to 0 to disable shell/external commands])
else
	EXTRAOBJS="$EXTRAOBJS npopen\$o spawn\$o"
fi

###	use option --with-cflags to set default CFLAGS
AC_MSG_CHECKING(for CFLAGS options)
AC_ARG_WITH(CFLAGS,
	[  --with-CFLAGS=value     set default for $CFLAGS],
	[cflags="$withval"])
if test -z "$cflags"; then
	AC_ARG_WITH(cflags,
		[  --with-cflags=value     (same as --with-CFLAGS)],
		[cflags="$withval"])
fi

# if the user specified nothing else, set -O.  user can override
# with --with-cflags=" "
if test -n "$cflags"; then
	CFLAGS="$cflags"
elif test -z "$CFLAGS" ; then
	CFLAGS="-O"
fi
AC_MSG_RESULT($cflags)

if test "$DISABLE_EXTS" != yes
then
	CF_WITH_PERL
fi

###############################################################################
CF_HELP_MESSAGE(Options for syntax filters)
AC_MSG_CHECKING(if you want to build syntax filters)
CF_ARG_DISABLE(filters,
	[  --disable-filters       do not build syntax filters],
	[enable_filters=no],
	[enable_filters=yes])
AC_MSG_RESULT($enable_filters)

MAKE_FILTERS=
AC_SUBST(MAKE_FILTERS)

if test "$enable_filters" = yes ; then

# allow user to preset these environment variables:
: "${VILE_BUILT_IN_FILTERS=none}"
: "${VILE_LOADABLE_FILTERS=none}"
: "${VILE_EXTERNAL_FILTERS=all}"

AC_MSG_CHECKING(if you want built-in syntax filters)
AC_ARG_WITH(builtin-filters,
[  --with-builtin-filters=XX specify filters to compile built-in vs external.
                          Give an option value to specify only certain filters,
			  i.e., a comma-separated list of filter-names.
                          The default, if no value is given is 'all'.
                          The default if the option is not given is 'none'],
[test "$withval" = yes && withval=all; VILE_BUILT_IN_FILTERS="$withval"],

[VILE_BUILT_IN_FILTERS=none])
test "$VILE_BUILT_IN_FILTERS" = no && VILE_BUILT_IN_FILTERS=none
AC_MSG_RESULT($VILE_BUILT_IN_FILTERS)

AC_MSG_CHECKING(if you want loadable syntax filters)
AC_ARG_WITH(loadable-filters,
[  --with-loadable-filters=XX specify filters to dynamically load at runtime.
                          Give an option value to specify only certain filters,
			  i.e., a comma-separated list of filter-names.
                          The default, if no value is given is 'all'.
                          The default if the option is not given is 'none'],
[test "$withval" = yes && withval=all; VILE_LOADABLE_FILTERS="$withval"],

[VILE_LOADABLE_FILTERS=none])
test "$VILE_LOADABLE_FILTERS" = no && VILE_LOADABLE_FILTERS=none
AC_MSG_RESULT($VILE_LOADABLE_FILTERS)

# If both "--with-builtin-filters" and "--with-loadable-filters" were given,
# the user must provide an explicit list since we cannot build both at once.
if test "$VILE_BUILT_IN_FILTERS" != none && test "$VILE_LOADABLE_FILTERS" != none
then
	builtins=`MAKELIST lc "$VILE_BUILT_IN_FILTERS"`
	test "$builtins" = "." && exit 1
	loadable=`MAKELIST lc "$VILE_LOADABLE_FILTERS"`
	test "$loadable" = "." && exit 1

	for cf_b in $builtins
	do
		for cf_l in $loadable
		do
			if test "$cf_b" = "$cf_l"
			then
				AC_MSG_ERROR(Cannot overlap built-in and loadable filter lists)
			fi
		done
	done
fi

# Anything that is neither a built-in or a loadable filter is an external
# filter.  We could have 3 options, but resolving inconsistencies would be
# a lot more work.
if test "$VILE_BUILT_IN_FILTERS" = none && test "$VILE_LOADABLE_FILTERS" = none
then
	VILE_EXTERNAL_FILTERS=all
elif test "$VILE_BUILT_IN_FILTERS" = all || test "$VILE_LOADABLE_FILTERS" = all
then
	VILE_EXTERNAL_FILTERS=none
else
	complete=`MAKELIST lc all`
	builtins=`MAKELIST lc "$VILE_BUILT_IN_FILTERS"`
	loadable=`MAKELIST lc "$VILE_LOADABLE_FILTERS"`
	VILE_EXTERNAL_FILTERS=
	for cf_a in $complete
	do
		found=no
		for cf_b in $builtins $loadable
		do
			if test "$cf_b" = "$cf_a" ; then
				found=yes
				break
			fi
		done
		if test $found = no ; then
			if test -z "$VILE_EXTERNAL_FILTERS" ; then
				VILE_EXTERNAL_FILTERS=$cf_a
			else
				VILE_EXTERNAL_FILTERS="$VILE_EXTERNAL_FILTERS,$cf_a"
			fi
		fi
	done
	test -z "$VILE_EXTERNAL_FILTERS" && VILE_EXTERNAL_FILTERS=none
fi

AC_MSG_CHECKING(if any filters are built-in/loadable)

builtins=`MAKELIST l "$VILE_BUILT_IN_FILTERS"`
loadable=`MAKELIST l "$VILE_LOADABLE_FILTERS"`

if test -n "$builtins" ; then
	cf_needflex=yes
elif test -n "$loadable" ; then
	cf_needflex=yes
else
	cf_needflex=no
fi
AC_MSG_RESULT($cf_needflex)

# check for $LEX and see if it is "flex"
CF_LEX_VERSION
if test -z "$LEX_VERSION" ; then
    LEX_IS_FLEX=no
else
    LEX_IS_FLEX=yes
    AC_DEFINE(LEX_IS_FLEX,1,[Define to 1 if lex is really flex])
fi

if test "$cf_needflex" = yes ; then
	cf_broken_flex=yes
	cf_lex_version=`echo "$LEX_VERSION" | \
		sed \
			-e 's/^\([[0-9]][[0-9]]*\.\)\([[0-9]][[0-9]]*\.\)\([[0-9]][[0-9]]*\).*$/\1\2\3/' \
			-e 's/_.*//g' \
			-e 's/\.\([[0-9]]\)/_00\1/g' \
			-e 's/\..*//g' \
			-e 's/_[[0]]*\([[0-9]]\{3,3\}\)/\1/g'`
	if test -z "$cf_lex_version"
	then
		AC_MSG_WARN(You need flex to compile builtin filters)
	elif test "$cf_lex_version" -lt 2005000
	then
		AC_MSG_WARN(Sorry - your version of flex is too old: $LEX_VERSION)
	elif test "$cf_lex_version" -ge 2006000
	then
		cf_broken_flex=maybe
	else
		LEX_SUBVERSION=`echo "$LEX_VERSION" | \
			sed \
				-e 's/^[[0-9]][[0-9]]*\.[[0-9]][[0-9]]*\.//' \
				-e 's/\.[[0-9.]]*//'`
		if test -z "$LEX_SUBVERSION"
		then
			AC_MSG_WARN(This version of flex cannot compile builtin filters)
		elif test "$LEX_SUBVERSION" = 4a
		then
			CF_VERBOSE(good)
			cf_broken_flex=no
		elif test "$LEX_SUBVERSION" != 4
		then
			AC_MSG_WARN(This version of flex cannot compile builtin filters)
		else
			cf_broken_flex=no
		fi
	fi
else
	cf_broken_flex=unknown
fi

# "--disable-filters" was given.
else
	VILE_BUILT_IN_FILTERS=none
	VILE_LOADABLE_FILTERS=none
	VILE_EXTERNAL_FILTERS=none
	MAKE_FILTERS="#"
fi

dnl These substitutions are used in makefile.in to build builtflt.h
AC_SUBST(VILE_BUILT_IN_FILTERS)
AC_SUBST(VILE_LOADABLE_FILTERS)

cf_default="\[$]{libdir}/vile"
AC_MSG_CHECKING(for library path)
CF_WITH_PATHLIST(libdir-path,
[  --with-libdir-path=LIST specify locations to search for filters, etc.],
VILE_LIBDIR_PATH,
${libdir}/vile,$cf_default,[define])
AC_MSG_RESULT($VILE_LIBDIR_PATH)

cf_default="\[$]{datadir}/vile"
AC_MSG_CHECKING(for startup path)
CF_WITH_PATHLIST(startup-path,
[  --with-startup-path=LIST specify locations to search for vile.hlp, etc.],
VILE_STARTUP_PATH,
${datadir},$cf_default,[define])
AC_MSG_RESULT($VILE_STARTUP_PATH)

CF_LEX_POINTER
CF_LEX_CHAR_CLASSES
CF_LEX_OPTIONS
CF_LEX_STATES

# we can work around some limitations of lex implementations
if test "$LEX_CHAR_CLASSES" = no ; then
	LEX_CHAR_CLASSES='sh $(srcdir)/noclass.sh '
elif test "$lex_pointer" = no ; then
	LEX_CHAR_CLASSES='sh $(srcdir)/noclass.sh '
else
	LEX_CHAR_CLASSES=
fi

# but other problems are showstoppers
if test "$cf_lex_options" = no ; then
	MAKE_LEX="#"
elif test "$cf_lex_states" = no ; then
	MAKE_LEX="#"
else
	MAKE_LEX=
fi
AC_SUBST(MAKE_LEX)

if test -n "$MAKE_LEX" ; then
	AC_MSG_WARN(disabling build for lex-filters)
fi

# X11 checks will sometimes add to CFLAGS.  We want the state of CFLAGS
# prior to these checks

AC_CHECK_FUNC(getpwnam,,AC_CHECK_LIB(sun,getpwnam))

# check for a lint program.
CF_PROG_LINT
if test -n "$LINT"
then
	AC_DEFINE(SYSTEM_HAS_LINT_PROG,1,[Define to 1 if the system has a lint program])
fi

###	checks for header files
AC_CHECK_DECL(exit)
AC_HEADER_DIRENT
AM_LANGINFO_CODESET
AC_CHECK_HEADERS( \
fcntl.h \
ioctl.h \
limits.h \
poll.h \
pwd.h \
search.h \
select.h \
setjmp.h \
sgtty.h \
signal.h \
stdarg.h \
stddef.h \
sys/filio.h \
sys/ioctl.h \
sys/itimer.h \
sys/param.h \
sys/resource.h \
sys/select.h \
sys/socket.h \
sys/time.h \
sys/wait.h \
termio.h \
termios.h \
utime.h \
varargs.h \
)

###	checks for typedefs
AC_TYPE_SIGNAL
AC_DEFINE(SIGT,RETSIGTYPE,[Define to override the signal return-type])
if test $ac_cv_type_signal = void; then
	AC_DEFINE(SIGRET,[/*do nothing*/],[Define as needed to return value from signal handler])
else
	AC_DEFINE(SIGRET,[return 0],[Define as needed to return value from signal handler])
fi

AC_TYPE_SIZE_T
AC_TYPE_UID_T
AC_TYPE_PID_T
AC_TYPE_MODE_T
AC_CHECK_TYPE(time_t, long)
AC_CHECK_TYPE(intptr_t, long)
CF_SIG_ATOMIC_T

###	checks for library functions
if test "$cross_compiling" = no; then
	AC_FUNC_SETVBUF_REVERSED
fi

###	checks for structures
AC_STAT_MACROS_BROKEN
AC_ST_BLKSIZE
AC_ST_BLOCKS
AC_ST_RDEV
AC_STRUCT_TM
CF_HEADER_RESOURCE
CF_SIZECHANGE
CF_FP_ISREADY
CF_WCTYPE

###	checks for system services and user specified options
AC_LONG_FILE_NAMES
CF_RESTARTABLE_PIPEREAD		# is a read() of a pipe restartable?
CF_VA_COPY

AC_SUBST(CFLAGS)
AC_SUBST(LIBS)
AC_SUBST(X_CFLAGS)
AC_SUBST(X_LIBS)
AC_SUBST(X_EXTRA_LIBS)
AC_SUBST(SCREEN)
AC_SUBST(TARGET)
dnl
dnl The killpg test and the tests for the presence of certain functions on
dnl a particular operating system are done prior to the checks for existence
dnl of screen libraries due to a problem with -lXt on certain SunOS systems.
dnl The nature of this problem is described in the FAQ on X.  Look for the
dnl following subject: ``What are these problems with "_XtInherit not found"
dnl on the Sun? ...''  The long and the short of it is that AC_HAVE_FUNCS will
dnl not work properly (without being rewritten) if placed after adding -lXt to
dnl LIBS on SunOS (and perhaps some others as well).
dnl
CF_KILLPG
CF_ERRNO
CF_CHECK_ENVIRON(environ)
CF_SYS_ERRLIST
CF_PKG_CONFIG

###############################################################################
CF_HELP_MESSAGE(Select screen-type)
AC_DEFINE(scrn_chosen,1,[Define to 1 if screen-type has been chosen])
# Try to eventually eliminate the following
AC_DEFINE(os_chosen,1,[Define to 1 if operating system type has been chosen])

AC_MSG_CHECKING(for screen type)
AC_ARG_WITH(screen,
[  --with-screen=value     specify terminal driver.  The default is tcap, for
                          the termcap/terminfo driver.  Other values include:
                              curses, ncurses, ncursesw,
                              X11, Motif, Athena, Xaw{3d|3dxft|Plus}, neXtaw,
                              DOS, Windows and ansi.],
[screen=$withval])

if test -z "$screen"; then
	AC_ARG_WITH(scr,
		[  --with-scr=value        (same as --with-screen)],
		[screen=$withval])
fi
test -z "$screen" && test "$with_x" = yes && screen=x11
if test -z "$screen"; then
	screen=termcap;
fi
AC_MSG_RESULT($screen)

###############################################################################
CF_HELP_MESSAGE(Options specific to screen-types)
: See if this is any type of xvile configuration:
case "$screen" in
	([[Mm]]otif*|[[xX]]|[[Aa]]thena|[[xX]][[atTmMoO]]*|[[xX]]11|neXtaw|XawPlus)
		EXTRA_INSTALL_FILES="$EXTRA_INSTALL_FILES \$(INSTALL_X_FILES)"
		CF_X_TOOLKIT
		CF_WITH_IMAKE_CFLAGS
		CF_ADD_LDFLAGS($IMAKE_LOADFLAGS)
		X_CFLAGS="$X_CFLAGS $IMAKE_CFLAGS"
		LDFLAGS="$LDFLAGS $X_LIBS"
		CF_CHECK_CFLAGS($X_CFLAGS)

		SCREEN=x11
		TARGET=xvile
		X11_FONT=x11
		X11_SETS=plain

		AC_MSG_CHECKING(if you want to use FreeType library)
		CF_ARG_ENABLE(freetype,
			[  --enable-freetype       enable freetype library-support],
			[enable_freetype=yes],
			[enable_freetype=no])
		AC_MSG_RESULT($enable_freetype)
		if test "$enable_freetype" = yes ; then
			CF_X_FONTCONFIG
			X11_FONT=xft
		fi

		AC_MSG_CHECKING(if you want to use fontsets)
		CF_ARG_ENABLE(fontsets,
			[  --enable-fontsets       enable fontset-support],
			[enable_fontsets=yes],
			[enable_fontsets=no])
		AC_MSG_RESULT($enable_fontsets)
		if test "$enable_fontsets" = yes ; then
			X11_SETS=fancy
		fi

		EXTRAOBJS="$EXTRAOBJS $X11_FONT$X11_SETS\$o"

		AC_DEFINE(DISP_X11,1,[Define to 1 if terminal driver uses X11])
		AC_DEFINE(XTOOLKIT,1,[Define to 1 if terminal driver uses Xt])

		AC_CHECK_HEADERS(X11/IntrinsicI.h X11/Xpoll.h X11/Xmu/Atoms.h)
		AC_HAVE_FUNCS(Xutf8LookupString Xutf8TextPropertyToTextList XmbTextPropertyToTextList)
	;;
esac

: Find the libraries that apply to the selected screen-type:
case "$screen" in
	([[mM]]otif*|[[Xx]]m)
		AC_DEFINE(MOTIF_WIDGETS,1,[Define to 1 if using Motify widgets])
		CF_X_MOTIF
		test "$cf_extensions" = yes && EXTRAOBJS="$EXTRAOBJS x11menu\$o menu\$o"
		test "$cf_extensions" = no && EXTRAOBJS="$EXTRAOBJS select\$o"
	;;
	([[Aa]]thena*|[[Xx]]aw*|neXtaw|XawPlus)
		AC_DEFINE(ATHENA_WIDGETS,1,[Define to 1 if using Xaw widgets])
		case $screen in
		(*aw*)
			cf_x_athena=`echo $screen | sed -e s/x/X/ -e s/Xft/xft/`
			;;
		esac
		CF_X_ATHENA
		test "$cf_extensions" = yes && EXTRAOBJS="$EXTRAOBJS x11menu\$o menu\$o"
		test "$cf_extensions" = no && EXTRAOBJS="$EXTRAOBJS select\$o"
	;;
	(x|X|[[Xx]][[Tt]]*|[[Xx]]11)
		AC_DEFINE(NO_WIDGETS,1,[Define to 1 if using xvile with selection extension])
		test "$cf_extensions" = no && EXTRAOBJS="$EXTRAOBJS select\$o"
	;;
	(ansi)
		SCREEN=ansi
		TARGET=vile
		AC_DEFINE(DISP_ANSI,1,[Define to 1 if using dumb ANSI driver])
	;;
	(curses)
		SCREEN=curses
		TARGET=vile
		AC_DEFINE(DISP_CURSES,1,[Define to 1 if using curses terminal driver])
		CF_CURSES_CPPFLAGS
		CF_NCURSES_VERSION
		CF_CURSES_LIBS
	;;
	(ncursesw*)
		SCREEN=curses
		TARGET=vile
		AC_DEFINE(DISP_CURSES)
		CF_UTF8_LIB
		CF_NCURSES_CONFIG($screen)
	;;
	(ncurses*)
		SCREEN=curses
		TARGET=vile
		AC_DEFINE(DISP_CURSES,1,[Define to 1 using ncurses terminal driver])
		CF_NCURSES_CONFIG($screen)
	;;
	([[Dd]][[Oo]][[Ss]])
		SCREEN=ntconio
		TARGET=convile
		AC_DEFINE(DISP_NTCONS,1,[Define to 1 if using Windows (NT and later) console driver])
	;;
	([[Ww]]indows)
		SCREEN=ntwinio
		TARGET=minvile
		AC_DEFINE(DISP_NTWIN,1,[Define to 1 if using Windows (NT and later) GUI driver])
	;;
	(*)
		SCREEN=tcap
		TARGET=vile
		AC_DEFINE(DISP_TERMCAP,1,[Define to 1 if using termcap/terminfo driver])
		AC_MSG_CHECKING(if you want to use ncurses)
		AC_ARG_WITH(ncurses,
			[  --with-ncurses          use ncurses rather than termcap/terminfo],
			[screen=ncurses],
			[withval=no])
		AC_MSG_RESULT($withval)
		CF_TERMCAP_LIBS($screen)
	;;
esac

# work around for broken FreeBSD header files (see tcap.h)
case "$cf_cv_ncurses_version" in
(5.2.20020615)
	AC_DEFINE(FREEBSD_BROKE_NCURSES,1,[Define to 1 as workaround using ncurses 5.2 on FreeBSD])
	;;
esac

: Check for termcap vs terminfo:
cf_tc_funcs=""
cf_tc_externs=""
if test "$SCREEN" = curses ; then
	CF_TERMCAP_LIBS
    CF_CURSES_FUNCS(addnwstr resizeterm)
	cf_tc_funcs="use_default_colors"
	AC_DEFINE(NEED_CURSES_H,1,[Define to 1 if we must include curses.h])
fi
if test ".$cf_cv_termlib" = .terminfo ; then
	AC_DEFINE(USE_TERMINFO,1,[Define to 1 if we are actually using terminfo])
	cf_tc_funcs="$cf_tc_funcs tigetint tigetnum tparam tparm"
	cf_tc_externs="$cf_tc_funcs tgoto tigetstr tputs tigetflag"
elif test ".$cf_cv_termlib" = .termcap ; then
	# BSD 'tputs()' may need 'PC' to be set.
	AC_TRY_LINK([],[extern char PC; PC = 0],
		[AC_DEFINE(HAVE_EXTERN_TCAP_PC,1,[Define to 1 if we have the PC external termcap variable])])
	cf_tc_funcs="$cf_tc_funcs tgetint tgetnum tparam tparm"
	cf_tc_externs="$cf_tc_funcs tgoto tgetstr tputs tgetent tgetflag"
else
	cf_cv_termlib=none
fi

LIBS="$LIBS $X_EXTRA_LIBS"

CF_TERMIOS_TYPES
CF_CHAR_DEVICE(/dev/tty)

case $TARGET in
	(convile|minvile)
		CF_ACVERSION_CHECK(2.52,
			[AC_PATH_TOOL(WINDRES, windres, none)],
			[AC_PATH_PROG(WINDRES, windres, none)])
		CF_CHECK_LIBSSP
		;;
	(*)
		CF_CRYPT_FUNC
	;;
esac

CF_CHECK_FD_SET
CF_STDIO_UNLOCKED

AC_HAVE_FUNCS( \
_nc_freeall \
_nc_free_tinfo \
exit_curses \
exit_terminfo \
access \
alarm \
getcwd \
getegid \
geteuid \
gethostname \
getpass \
gettimeofday \
getwd \
isblank \
iswblank \
killpg \
mkdir \
mkdtemp \
poll \
popen \
putenv \
realpath \
select \
setbuffer \
setgid \
setgroups \
setitimer \
setsid \
setuid \
setvbuf \
sigaction \
sigprocmask \
strchr \
strerror \
strftime \
stricmp \
strnicmp \
strrchr \
strtoul \
symlink \
tcgetattr \
tdestroy \
tsearch \
ttyname \
umask \
utime \
utimes \
waitpid $cf_tc_funcs \
)

CF_MKSTEMP

###	Miscellaneous options

case "$TARGET" in
	(convile)
		AC_DEFINE(SYS_WINNT,1,[Define to 1 if the operating system is Windows (NT or later)])
		AC_DEFINE(CC_MINGW,1,[Define to 1 if building with MinGW])

		LIBS="$LIBS -lcomdlg32 -lole32 -lstdc++"
		ICON_SUFFIX=.ico
		LDFLAGS="$LDFLAGS -s"
		EXTRAOBJS="$EXTRAOBJS w32misc\$o w32cmd\$o w32cbrd\$o w32pipe\$o makeargv\$o"
		EXTRAOBJS2="$EXTRAOBJS2 w32info\$o w32oo\$o"
		EXEEXT=.exe

	;;

	(minvile)
		AC_DEFINE(SYS_WINNT,1,[Define to 1 if the operating system is Windows (NT or later)])
		AC_DEFINE(CC_MINGW,1,[Define to 1 if building with MinGW])

		LIBS="$LIBS -lws2_32 -lmswsock -lgdi32 -lcomdlg32 -lwinspool -lole32 -lstdc++"
		ICON_SUFFIX=.ico
		LDFLAGS="$LDFLAGS -s -mwindows"
		EXTRAOBJS="$EXTRAOBJS w32misc\$o w32cmd\$o w32cbrd\$o w32pipe\$o makeargv\$o"
		EXTRAOBJS2="$EXTRAOBJS2 winvile\$o w32info\$o w32oo\$o"
		EXEEXT=.exe
	;;

	(xvile)
		AC_DEFINE(SYS_UNIX,1,[Define to 1 if building on Unix and clones])

		CF_WITH_XPM

		AC_MSG_CHECKING(if you want to enable colored-menus)
		CF_ARG_ENABLE(colored-menus,
			[  --enable-colored-menus  enable colored-menus],
			[enable_colored_menus=yes],
			[enable_colored_menus=no])
		AC_MSG_RESULT($enable_colored_menus)
		test "$enable_colored_menus" = yes && AC_DEFINE(OPT_MENUS_COLORED,1,[Define to 1 if xvile menus can be colored])

if test -n "$cf_x_athena" ; then
    AC_MSG_CHECKING(if you want to use Xaw scrollbars rather than our own)
    AC_ARG_WITH(Xaw-scrollbars,
	[  --with-Xaw-scrollbars   use Xaw scrollbars rather than our own],
	[cf_Xaw_scrollbars="$withval"],
	[cf_Xaw_scrollbars=no])
    AC_MSG_RESULT($cf_Xaw_scrollbars)

    if test "$cf_Xaw_scrollbars" = no ; then
	AC_DEFINE(OPT_KEV_SCROLLBARS,1,[Define to 1 if you want to use Kevin's xvile scrollbars])
	AC_DEFINE(OPT_KEV_DRAGGING,1,[Define to 1 if you want to use Kevin's xvile dragging feature])
    else
	AC_DEFINE(OPT_XAW_SCROLLBARS,1,[Define to 1 if you want to use Xaw scrollbars])
	AC_MSG_CHECKING(if you want to use the drag/scrolling extension with Xaw)
	AC_ARG_WITH(drag-extension,
	    [  --with-drag-extension   use drag/scrolling extension with Xaw],
	    [cf_drag_extension="$withval"],
	    [cf_drag_extension=no])
	AC_MSG_RESULT($cf_drag_extension)
	if test "$cf_drag_extension" != "no" ; then
	    AC_DEFINE(OPT_KEV_DRAGGING,1,[Define to 1 if you want to use Kevin's xvile dragging feature])
	fi
    fi
fi
	;;

	(*)
		AC_DEFINE(SYS_UNIX)
		ICON_SUFFIX=
	;;

esac

if test -n "$ICON_SUFFIX"
then
	AC_MSG_CHECKING(for the icon name)
	AC_ARG_WITH(icon-name,
		[  --with-icon-name=XXXX   override icon name (default: vile)],
		[ICON_NAME="$withval"],
		[ICON_NAME=vile])
	case "x$ICON_NAME" in
	(xyes|xno|x)
		ICON_NAME=vile
		;;
	esac
	AC_MSG_RESULT($ICON_NAME)
fi

AC_SUBST(ICON_SUFFIX)

###############################################################################
CF_HELP_MESSAGE(Miscellaneous features)

AC_MSG_CHECKING(if you want to compile-in plugin support)
CF_ARG_ENABLE(plugins,
	[  --enable-plugins        compile-in plugin support],
	[enable_plugins=yes],
	[enable_plugins=no])
AC_MSG_RESULT($enable_plugins)

CF_LARGEFILE
CF_LINK_PREFIX

CF_WITH_SYMLINK(VILE_SYMLINK, $TARGET, no)
CF_AUTO_SYMLINK(VILE_SYMLINK)

AC_MSG_CHECKING(if you want to change the number of exec-macros)
AC_ARG_WITH(exec-macros,
	[  --with-exec-macros=N    specify count of numbered macros],
	[cf_exec_macros="$withval"],
	[cf_exec_macros=no])
AC_MSG_RESULT($cf_exec_macros)
test "$cf_exec_macros" != no && AC_DEFINE_UNQUOTED(OPT_EXEC_MACROS,$cf_exec_macros,[Define to 1 if you want to override the number of exec-macros])

# I don't know of a reliable test for working locale, but we can at least check
# that it is available before offering it as an option.
CF_LOCALE
if test $cf_cv_locale = yes ; then
AC_MSG_CHECKING(if you want to use i18n locale support)
AC_ARG_WITH(locale,
	[  --with-locale           use i18n support for character-types],
	[cf_locale="$withval"],
	[cf_locale="$cf_extensions"])
AC_MSG_RESULT($cf_locale)
if test "$cf_locale" != no ; then
	AC_DEFINE(OPT_LOCALE,1,[Define to 1 to use i18n support for character-types])
	EXTRAOBJS="$EXTRAOBJS charsets\$o eightbit\$o wcwidth\$o"
AC_MSG_CHECKING(if you want to use iconv() for locale support)
AC_ARG_WITH(iconv,
	[  --with-iconv            use iconv() support for character-types],
	[cf_func_iconv="$withval"],
	[cf_func_iconv=yes])
AC_MSG_RESULT($cf_func_iconv)
if test "$cf_func_iconv" = yes ; then
AM_ICONV
if test "$am_cv_func_iconv" = yes ; then
	AC_DEFINE(OPT_ICONV_FUNCS,1,[Define to 1 to use iconv() support for character-types])
	test "$am_cv_func_iconv" != yes && LIBS="$am_cv_func_iconv $LIBS"
else
    AC_MSG_WARN(skipping iconv since it was not found)
fi # test $am_cv_func_iconv = yes
fi # test $cf_func_iconv" = yes
fi # test $cf_locale != no
fi # test $cf_cv_locale = yes

CF_WITH_MAN2HTML

###############################################################################
CF_HELP_MESSAGE(Debugging/development options)
CF_WITH_NO_LEAKS

AC_ARG_WITH(fakevms,
	[  --with-fakevms          test: use fake VMS I/O module],
	[AC_DEFINE(OPT_VMS_PATH,1,[Define to 1 to test with fake VMS I/O module])
	 EXTRAOBJS="$EXTRAOBJS fakevms\$o vms2unix\$o"])

AC_ARG_WITH(trace,
	[  --with-trace            test: turn on debug-tracing],
	[AC_DEFINE(OPT_TRACE,1,[Define to 1 to turn on debug-tracing])
	 with_trace=yes],
	[with_trace=])

if test "x${with_trace}" = "xyes" ; then
	EXTRAOBJS="$EXTRAOBJS trace\$o"
elif test "${enable_leaks}" = no ; then
	test -z "${with_valgrind}" && EXTRAOBJS="$EXTRAOBJS trace\$o"
fi

dnl "yes" forces check for attributes even when warnings are unwanted
CF_ENABLE_WARNINGS(Wno-unknown-pragmas,yes)

CF_INSTALL_OPTS

###	Do these last, since they rely on a properly constructed config.h

###	Mandatory checks for missing prototypes:
CHECK_DECL_FLAG="-I. -I$srcdir -Dscrn_chosen"
VILE_CHECK_HDRS='
#define CHECK_PROTOTYPES 1
#include <estruct.h>
'
CHECK_DECL_HDRS="$VILE_CHECK_HDRS"

CF_MISSING_EXTERN( \
environ \
getenv \
getpass \
getwd \
malloc \
realloc \
rindex \
strerror \
strtol \
)

CF_CURSES_TERM_H
if test ".$cf_cv_termlib" != ".none"
then
CHECK_DECL_HDRS="$VILE_CHECK_HDRS

#include <tcap.h>

#if DISP_TERMCAP
int dummy_tcap(void);
int dummy_tcap(void)
{
	return vl_tgetflag(\"?\") + vl_tgetnum(\"?\") + vl_tgetstr(\"?\");
}
/* done with tcap-fixes */
#endif
"
	CF_TYPE_OUTCHAR
fi
if test -n "$cf_tc_externs"
then
CF_MISSING_EXTERN( \
$cf_tc_externs \
)
fi

###	Optional checks for missing functions (enabled with --with-warnings)
if test "x$enable_warnings" = "xyes"
then
CHECK_DECL_HDRS="$VILE_CHECK_HDRS"
CF_MISSING_EXTERN( \
_nc_freeall \
access \
alarm \
atoi \
chdir \
close \
crypt \
dup \
execlp \
fclose \
fflush \
fileno \
fork \
fprintf \
fputs \
fread \
free \
fseek \
fwrite \
gethostname \
getpgrp \
getpid \
getuid \
isatty \
kill \
killpg \
longjmp \
lstat \
memset \
mkdir \
open \
pclose \
perror \
pipe \
popen \
printf \
puts \
qsort \
read \
readlink \
select \
setbuf \
setbuffer \
setitimer \
setjmp \
setpgrp \
setsid \
setvbuf \
sleep \
sscanf \
system \
time \
unlink \
utime \
utimes \
wait \
write \
)

CHECK_DECL_HDRS="$VILE_CHECK_HDRS
#if HAVE_SYS_IOCTL_H
#include	<sys/ioctl.h>
#endif
"
CF_MISSING_EXTERN( \
ioctl \
)
fi

if test "$VILE_BUILT_IN_FILTERS" != none ; then
	cf_filter_libs=yes
elif test "$VILE_LOADABLE_FILTERS" != none ; then
	cf_filter_libs=yes
else
	cf_filter_libs=no
fi

if test "$cf_filter_libs" = yes ; then
	EXTRAOBJS="$EXTRAOBJS builtflt\$o"
	CPPFLAGS="-I\$(srcdir)/filters $CPPFLAGS"
	FILTER_LIBS="-Lfilters -lvlflt"
	LIBBUILTFLT="${LIB_PREFIX}vlflt.a"
	BUILTHDRS="$BUILTHDRS builtflt.h"
	BUILTLIBS="$BUILTLIBS filters/$LIBBUILTFLT"
	AC_DEFINE(OPT_FILTER,1,[Define to 1 if built-in filters are used])
else
	BUILTLIBS=
	FILTER_LIBS=
	LIBBUILTFLT=
fi

AC_SUBST(EXTRAOBJS)
AC_SUBST(EXTRAOBJS2)
AC_SUBST(BUILTHDRS)
AC_SUBST(BUILTLIBS)
AC_SUBST(BUILTSRCS)
AC_SUBST(FILTER_LIBS)
AC_SUBST(LIBBUILTFLT)

# --- check dynamic loading capabilities

cf_need_libdl=no

test "$VILE_LOADABLE_FILTERS" != none && cf_need_libdl=yes
test "$enable_plugins"        != no   && cf_need_libdl=yes

if test "$VILE_LOADABLE_FILTERS" != none || test "$enable_plugins" != no ; then
	CF_GCC_OPT_RDYNAMIC
fi

if test "$enable_plugins" != no ; then
	AC_DEFINE(OPT_PLUGIN,1,[Define to 1 if plugin support should be compiled-in])
	EXTRAOBJS="$EXTRAOBJS plugin\$o"
fi

if test "$cf_need_libdl" = yes ; then
	CF_FUNC_DLSYM
fi

if test "$VILE_LOADABLE_FILTERS" != none ; then
	if test "x$cf_cv_gcc_opt_rdynamic" = "xno"
	then
		if test "$VILE_BUILT_IN_FILTERS" = none
		then
			VILE_BUILT_IN_FILTERS="$VILE_LOADABLE_FILTERS"
			VILE_LOADABLE_FILTERS=none
			AC_MSG_WARN(will use built-in filters instead)
		else
			AC_MSG_ERROR(cannot mix built-in and loadable-filters)
		fi
	else
		if test "$GCC" = yes ; then
			SH_CFLAGS="-fPIC"
			SH_LDFLAGS="-shared"
		else
			# FIXME - CF_SHARED_OPTS?
			# this works for Solaris and IRIX64
			SH_CFLAGS="-KPIC"
			SH_LDFLAGS="-G"
		fi
	fi
fi

AC_SUBST(SH_CFLAGS)
AC_SUBST(SH_LDFLAGS)

AC_CHECK_PROGS(SPELL_PROG, ispell aspell spell, [])
case .$SPELL_PROG in
(.aspell)
	SPELL_OPTS="-x list"
	;;
(.ispell)
	SPELL_OPTS="-l -x"
	;;
(*)
	SPELL_PROG="spell"
	SPELL_OPTS="-l"
	;;
esac
AC_DEFINE_UNQUOTED(SPELL_PROG,"$SPELL_PROG",[Define to override default "spell" program name])
AC_DEFINE_UNQUOTED(SPELL_OPTS,"$SPELL_OPTS",[Define to override default "spell" program options "-l"])

filters_bindir=`echo "$VILE_LIBDIR_PATH" | sed -e 's%'$PATH_SEPARATOR'.*%%'`
echo "Computing location to install filter programs: $filters_bindir"
AC_SUBST(filters_bindir)

if test "$cf_broken_flex" = yes ; then
AC_MSG_WARN([You should get a working version of flex, for instance:
	http://invisible-island.net/reflex
])
fi

EXTRA_OUTPUT_FILES=

###############################################################################
CF_HELP_MESSAGE(Options for XVile installation directories)
if test "$TARGET" = xvile; then
	CF_WITH_APP_CLASS(XVile)
	CF_WITH_APP_DEFAULTS
	CF_WITH_ICON_THEME([icons/${ICON_NAME} icons/${ICON_NAME}_48x48],,,icons/${ICON_NAME}_48x48)
	CF_WITH_X_DESKTOP_UTILS
	if test "x$ICON_THEME" != xno; then
		AC_DEFINE(USE_ICON_THEME,1,[Define to 1 if using icon-theme])
	fi
	if test -z "$desktop_utils"
	then
		DESKTOP_CATEGORY="Application;Utility;TextEditor;"
		AC_SUBST(DESKTOP_CATEGORY)
		EXTRA_OUTPUT_FILES=df-install
	fi
else
	no_appsdir="#"
	no_icondir="#"
	no_pixmapdir="#"
	desktop_utils="#"
fi

if test "x$cf_cv_crypt_func" != xyes ; then
if test "$DISABLE_EXTS" != yes ; then
	CPPFLAGS="$CPPFLAGS -I\$(srcdir)/filters"
	EXTRAOBJS="$EXTRAOBJS encrypt\$o"
fi
fi

CF_MAKE_PHONY
CF_MAKE_TAGS
CF_DISABLE_RPATH_HACK

if test -n "$loadable" ; then
	save_LDFLAGS="$LDFLAGS"
	LDFLAGS=`echo "x$LDFLAGS" | sed -e 's/^.//' -e 's/-Wl,--as-needed//g' -e 's/-Wl,--no-undefined//g' -e 's/  / /g'`
	if test "x$save_LDFLAGS" != "x$LDFLAGS"
	then
		CF_VERBOSE(removed loader flags that interfere with loadable filters)
		CF_VERBOSE(flags are now $LDFLAGS)
		export LDFLAGS
	fi
fi

CF_MERGE_EXTRA_CFLAGS

###	output makefile and config.h
AC_OUTPUT(makefile filters/makefile filters/makefile.tmp:filters/makefile.2nd $EXTRA_OUTPUT_FILES,
{
test -n "$ac_top_srcdir" && top_srcdir="$ac_top_srcdir"
if test "$top_srcdir" = ".."
then
	cf_srcdir=.
else
	cf_srcdir=`echo $top_srcdir |sed -e 's%^../%%'`
fi

# if we're building in the source tree, make it possible to auto-update genmake
if test -f ./filters/genmake.sh ; then
	cf_srcdir=.
elif test -f $cf_srcdir/filters/genmake.sh ; then
	:
elif test -f $top_srcdir/filters/genmake.sh ; then
	cf_srcdir=$top_srcdir
else
	cf_srcdir=.
fi
cf_genmake=$cf_srcdir/filters/genmake.mak
FGREP="$FGREP" $SHELL $cf_srcdir/filters/genmake.sh $cf_genmake $cf_srcdir/filters

echo "Checking for built-in filters: $VILE_BUILT_IN_FILTERS"
$AWK -f $cf_srcdir/filters/mk-1st.awk <$cf_genmake mode=built-in list="$VILE_BUILT_IN_FILTERS" flex=$LEX_IS_FLEX >>filters/makefile

echo "Checking for external filters: $VILE_EXTERNAL_FILTERS"
$AWK -f $cf_srcdir/filters/mk-1st.awk <$cf_genmake mode=external list="$VILE_EXTERNAL_FILTERS" flex=$LEX_IS_FLEX >>filters/makefile

echo "Checking for loadable filters: $VILE_LOADABLE_FILTERS"
$AWK -f $cf_srcdir/filters/mk-1st.awk <$cf_genmake mode=loadable list="$VILE_LOADABLE_FILTERS" flex=$LEX_IS_FLEX >>filters/makefile

cat   filters/makefile.tmp >>filters/makefile
rm -f filters/makefile.tmp

case $cf_srcdir in
/*|?:*[)]
	cf_SRCDIR=$cf_srcdir
	;;
*[)]
	cf_SRCDIR=../$cf_srcdir
	;;
esac

$AWK -f $cf_srcdir/filters/mk-2nd.awk <$cf_genmake from=$cf_SRCDIR/filters linking="$ECHO_LD" compiling="$RULE_CC" show="$ECHO_CC" mode=built-in list="$VILE_BUILT_IN_FILTERS" flex=$LEX_IS_FLEX >>filters/makefile
$AWK -f $cf_srcdir/filters/mk-2nd.awk <$cf_genmake from=$cf_SRCDIR/filters linking="$ECHO_LD" compiling="$RULE_CC" show="$ECHO_CC" mode=external list="$VILE_EXTERNAL_FILTERS" flex=$LEX_IS_FLEX >>filters/makefile
$AWK -f $cf_srcdir/filters/mk-2nd.awk <$cf_genmake from=$cf_SRCDIR/filters linking="$ECHO_LD" compiling="$RULE_CC" show="$ECHO_CC" mode=loadable list="$VILE_LOADABLE_FILTERS" flex=$LEX_IS_FLEX >>filters/makefile

(for i in $cf_srcdir/filters/*.key;do echo $i;done) | $AWK -f $cf_srcdir/filters/mk-key.awk from=$cf_srcdir >>filters/makefile
},
{
RULE_CC='$RULE_CC'
ECHO_LD='$ECHO_LD'
ECHO_CC='$ECHO_CC'
LEX_IS_FLEX='$LEX_IS_FLEX'
MAKE_LEX='$MAKE_LEX'
AWK="$AWK"
VILE_BUILT_IN_FILTERS='$VILE_BUILT_IN_FILTERS'
VILE_LOADABLE_FILTERS='$VILE_LOADABLE_FILTERS'
VILE_EXTERNAL_FILTERS='$VILE_EXTERNAL_FILTERS'
},
sort)
