dnl	$Id: configure.in,v 8.192 2003/02/17 20:09:07 skimo Exp $
dnl Process this file with autoconf to produce a configure script.

AC_INIT(../common/main.c)
AC_CONFIG_AUX_DIR(.)
AM_INIT_AUTOMAKE(vi, 1.81.6)
AM_CONFIG_HEADER(config.h)

dnl Configure setup.
AC_PROG_INSTALL()
AC_CANONICAL_HOST
dnl part of AM_INIT_AUTOMAKE ?
dnl AC_ARG_PROGRAM()

AC_PROG_LIBTOOL

dnl If the user wants a debugging environment, set OPTFLAG now, some
dnl compilers won't mix optimizing and debug flags.)
AC_MSG_CHECKING(if --enable-debug option specified)
AC_ARG_ENABLE(debug,
	[  --enable-debug          Build a debugging version.],
	[vi_cv_debug=$enableval], [vi_cv_debug="no"])
AC_MSG_RESULT($vi_cv_debug)
if test "$vi_cv_debug" = yes; then
	AC_DEFINE(DEBUG)
	OPTFLAG=${OPTFLAG-"-g"}
	no_op_OPTFLAG=${no_op_OPTFLAG-"-g"}
fi

dnl This is where we handle stuff that autoconf can't handle.
dnl XXX
dnl Don't override anything if it's already set from the environment.

dnl Compiler, preprocessor and load flags.
dnl AUX:	-ZP disables _BSD_SOURCE et al, but enables POSIX at link time.
dnl LynxOS:	We check for gcc 2.x or better, the gcc 1 that was shipped with
dnl		LynxOS historically wasn't good enough.
AC_SUBST(CPPFLAGS)
case "$host_os" in
aix3.2.5)  OPTFLAG=${OPTFLAG-"-O"};;
aix4.1*)   CFLAGS=${CFLAGS-"-qstrict"}
	   OPTFLAG=${OPTFLAG-"-O3"};;
aux*)	   CPPFLAGS=${CPPFLAGS-"-ZP -D_BSD_SOURCE -D_SYSV_SOURCE -D_AUX_SOURCE"}
	   LDFLAGS=${LDFLAGS-"-ZP"}
	   OPTFLAG=${OPTFLAG-"-O"};;
bsd4.4)    OPTFLAG=${OPTFLAG-"-O2"};;
bsdi*)	   CC=${CC-"shlicc"}
	   OPTFLAG=${OPTFLAG-"-O2"};;
irix6*)	   OPTFLAG=${OPTFLAG-"-O2"};;
irix*)	   OPTFLAG=${OPTFLAG-"-O2"};;
lynxos*)   AC_PROG_CC()
	   AC_MSG_CHECKING([for GNU C (gcc) version 2.x])
	   ac_cv_gcc_vers=`${CC-cc} -v 2>&1 | \
		grep "gcc version " | sed 's/.*version //'`
	   ac_cv_gcc_major=`echo "$ac_cv_gcc_vers" | sed 's/\..*//'`
	   if test "$ac_cv_gcc_major" = "2" ; then
		AC_MSG_RESULT(yes)
	   else
		AC_MSG_RESULT(no)
		AC_MSG_WARN([Nvi requires gcc 2.x to build on LynxOS.])
		AC_MSG_ERROR([See build/README.LynxOS for more information.])
	   fi;;
nextstep3) CPPFLAGS=${CPPFLAGS-"-w -pipe -posix"}
	   LDFLAGS=${LDFLAGS-"-posix"}
	   OPTFLAG=${OPTFLAG-"-O2"};;
osf*)	   CFLAGS=${CFLAGS-"-Olimit 1000"};;
solaris*)  no_op_OPTFLAG=${no_op_OPTFLAG-""};;
sunos*)	   no_op_OPTFLAG=${no_op_OPTFLAG-""};;
esac

dnl The default OPTFLAG is -O
AC_SUBST(OPTFLAG)
OPTFLAG=${OPTFLAG-"-O"}

dnl The default compiler is cc (NOT gcc), the default CFLAGS is empty,
dnl NOT whatever AC_PROG_CC sets.
AC_SUBST(CC)
CC=${CC-cc}
CFLAGS=${CFLAGS-""}
AC_PROG_CC

if test "$GCC" = yes; then
	AC_DEFINE(HAVE_GCC)
fi

dnl The SunOS/Solaris compiler can't optimize vi/v_txt.c; the symptom is
dnl that the command 35i==<esc> turns into an infinite loop.
AC_SUBST(no_op_OPTFLAG)
no_op_OPTFLAG=${no_op_OPTFLAG-"$OPTFLAG"}

dnl Libraries.
case "$host_os" in
bsdi2.1)   LIBS=${LIBS-"-lipc"};;
dgux*)	   LIBS=${LIBS-"-ldgc"};;
irix6*)	   LIBS=${LIBS-"-lbsd"};;
irix*)	   LIBS=${LIBS-"-lc_s -lbsd"};;
isc*)	   LIBS=${LIBS-"-lcposix -linet"};;
netbsd1*)  LIBS=${LIBS-"-lcrypt"};;
ptx*)	   LIBS=${LIBS-"-lseq -linet -lsocket"};;
sco3.2*)   LIBS=${LIBS-"-lsocket"};;
sinix*)	   LIBS=${LIBS-"-lelf -lc"};;
solaris*)  LIBS=${LIBS-"-lsocket -lnsl -ldl"};;
wgs*)	   LIBS=${LIBS-"-lnsl"};;
esac

dnl A/UX has a broken getopt(3), strpbrk(3).
case "$host_os" in
aux*)	   LIBOBJS="getopt.o strpbrk.o $LIBOBJS";;
esac

dnl Ultrix has a broken POSIX.1 VDISABLE value.
case "$host_os" in
ultrix*)   AC_DEFINE(HAVE_BROKEN_VDISABLE);;
esac

AC_MSG_CHECKING(whether recording of pathnames of libraries is enabled)
AC_ARG_ENABLE(runpath,
	[  --disable-runpath       Don't record path of libraries in binary.],
	[vi_cv_runpath="$enableval"], [vi_cv_runpath="yes"])
AC_MSG_RESULT($vi_cv_runpath)

if test "X$vi_cv_runpath" = "Xyes"; then
	LRscript='s/^\(.*\)/-R\1 -L\1 /'
else
	LRscript='s/^\(.*\)/-L\1 /'
fi

dnl If the user wants a tracing version, add the TRACE compile flag.
AC_MSG_CHECKING(if --enable-trace option specified)
AC_ARG_ENABLE(trace,
	[  --enable-trace          Build a tracing version.],
	[vi_cv_trace="yes"], [vi_cv_trace="no"])
if test "$vi_cv_trace" = yes; then
	CPPFLAGS="-DTRACE $CPPFLAGS"
fi
AC_MSG_RESULT($vi_cv_trace)

dnl The user may have additional CPP information.
CPPFLAGS="$ADDCPPFLAGS $CPPFLAGS"

dnl The user may have additional load line information.
LDFLAGS="$ADDLDFLAGS $LDFLAGS"

dnl The user may have additional library information.
LIBS="$ADDLIBS $LIBS"

dnl Checks for programs.
PATH="$PATH:/usr/bin:/usr/sbin:/sbin:/etc:/usr/etc:/usr/lib:/usr/ucblib"

dnl Check for the shell path.
AC_PATH_PROG(vi_cv_path_shell, sh, no)
if test "$vi_cv_path_shell" = no; then
	AC_MSG_ERROR([No shell utility found.])
fi

dnl Check for the sendmail path.
AC_PATH_PROG(vi_cv_path_sendmail, sendmail, no)
if test "$vi_cv_path_sendmail" = no; then
	AC_MSG_WARN([No sendmail utility found;])
	AC_MSG_WARN([    users will not be told of saved files.])
fi

dnl Check for the perl5/perl path.
AC_SUBST(vi_cv_path_perl)
AC_PATH_PROGS(vi_cv_path_perl, perl5 perl, no)

dnl Check for the "preserve" path.
dnl Historically, nvi has used /var/tmp/vi.recover.  The Linux filesystem
dnl standard (FSSTND) uses /var/preserve; we add the vi.recover directory
dnl beneath it so that we don't have name collisions with other editors.
dnl Other systems have /var/preserve as well, so we test first for an already
dnl existing name, and then use the first one that's writeable.
AC_SUBST(vi_cv_path_preserve)
AC_MSG_CHECKING(for preserve directory)
AC_CACHE_VAL(vi_cv_path_preserve, [dnl
	dirlist="/var/preserve /var/tmp /usr/tmp"
	vi_cv_path_preserve=no
	for i in $dirlist; do
		if test -d $i/vi.recover; then
			vi_cv_path_preserve=$i/vi.recover
			break;
		fi
	done
	if test "$vi_cv_path_preserve" = no; then
		for i in $dirlist; do
			if test -d $i -a -w $i; then
				vi_cv_path_preserve=$i/vi.recover
				break;
			fi
		done

	fi])
if test "$vi_cv_path_preserve" = no; then
	AC_MSG_ERROR([No writeable preserve directory found.])
fi
AC_MSG_RESULT($vi_cv_path_preserve)
AC_PATH_PROG(vi_cv_path_fuser, fuser, no)
AC_PATH_PROG(vi_cv_path_lsof, lsof, no)
AC_SUBST(INUSE)
INUSE=""
if test "$vi_cv_path_lsof" != no; then
	INUSE='test `lsof -t $i`'
fi
if test "$vi_cv_path_fuser" != no; then
	INUSE='fuser -s $i'
fi

dnl Check for programs used for installation
AC_PROG_AWK
AC_PATH_PROG(vi_cv_path_ar, ar, missing_ar)
AC_PATH_PROG(vi_cv_path_chmod, chmod, missing_chmod)
AC_PATH_PROG(vi_cv_path_cp, cp, missing_cp)
AC_PATH_PROG(vi_cv_path_ln, ln, missing_ln)
AC_PATH_PROG(vi_cv_path_mkdir, mkdir, missing_mkdir)
AC_PATH_PROG(vi_cv_path_rm, rm, missing_rm)
AC_PATH_PROG(vi_cv_path_ranlib, ranlib, missing_ranlib)
AC_PATH_PROG(vi_cv_path_strip, strip, missing_strip)

dnl Checks for libraries.
dnl Find the X libraries and includes.
AC_PATH_X
AC_SUBST(XINCS)
if test "$no_x" != yes; then
	if test "X$x_libraries" != "X"; then
		XLIBS="`echo $x_libraries | sed "$LRscript"` $XLIBS"
	fi
	XLIBS="$XLIBS -lX11"
	if test "X$x_includes" != "X"; then
		XINCS="-I$x_includes"
	fi
fi

AC_DEFUN([VI_CV_REPLACE_FUNCS],
[AC_FOREACH([AC_Func], [$1], [])dnl
AC_CHECK_FUNCS([$1], , [_AC_LIBOBJ($ac_func)])
])

dnl Check if the user wants widechar support.
AC_MSG_CHECKING(if --enable-widechar option specified)
AC_ARG_ENABLE(widechar,
	[  --enable-widechar       Build a wide char aware vi.],
	[vi_cv_widechar=$enableval], [vi_cv_widechar="no"])
if test "$vi_cv_widechar" = "yes"; then
	AC_DEFINE(USE_WIDECHAR)
fi
AC_MSG_RESULT($vi_cv_widechar)

dnl Check whether we can use iconv
AC_CHECK_HEADER(langinfo.h, [
	AC_CHECK_HEADER(iconv.h, [
		AC_DEFINE(USE_ICONV)
		VI_CV_REPLACE_FUNCS(iswblank)
	])
])

dnl If the user wants a Gtk IPC front-end for nvi, build it.
AC_SUBST(vi_programs)
AC_SUBST(vi_ipc)
AC_SUBST(GTKLIBS)
AC_SUBST(GTKINCS)
AC_MSG_CHECKING(if --enable-gtk option specified)
AC_ARG_ENABLE(gtk,
	[  --enable-gtk            Build a Gtk front-end for vi.],
	[vi_cv_gtk=$enableval], [vi_cv_gtk="no"])
AC_MSG_RESULT($vi_cv_gtk)
if test "$vi_cv_gtk" = "yes"; then
	#
	# Find pkg-config
	#
	AC_PATH_PROG(PKG_CONFIG, pkg-config, no)
	if test x$PKG_CONFIG = xno ; then
	    if test "$vi_cv_widechar" = "yes"; then
	        AC_MSG_ERROR(
[*** pkg-config not found. See http://pkgconfig.sourceforge.net])
	    fi

	else
	    if ! $PKG_CONFIG --atleast-pkgconfig-version 0.5 ; then
		if test "$vi_cv_widechar" = "yes"; then
		    AC_MSG_ERROR(
[*** pkg-config too old; version 0.5 or better required.])
		fi
		PKG_CONFIG=no
	    fi

	fi
	if test x$PKG_CONFIG = xno ; then
	    PKG_CONFIG=/bin/false
	fi

	vi_programs="$vi_programs vi-gtk"
	vi_ipc=vi-ipc
	AC_MSG_CHECKING(for gtk+)
	if $PKG_CONFIG gtk+-2.0 ; then
	      GTKINCS=`$PKG_CONFIG --cflags gtk+-2.0`
	      GTKLIBS=`$PKG_CONFIG --libs gtk+-2.0`

	      AC_MSG_RESULT(ok)
	      AC_DEFINE(HAVE_PANGO)
	else
	    if test "$vi_cv_widechar" = "yes"; then
	        AC_MSG_ERROR([
*** gtk+-2.0 is required. The latest version of gtk
*** is always available from ftp://ftp.gtk.org/.
	  	])
	    else
		AM_PATH_GTK(1.2.0)
		GTKINCS="$GTK_CFLAGS"
		GTKLIBS="$GTK_LIBS $LIBS"
	    fi
	fi

	SAVELIBS="$LIBS"
	LIBS="-lutil $GTKLIBS"
	AC_CHECK_LIB(nvizvt, zvt_get_ptys,
		[vi_cv_zvt="yes"], [vi_cv_zvt="no"])
	if test "$vi_cv_zvt" = "yes"; then
		GTKLIBS="-lnvizvt $LIBS"
		AC_DEFINE(HAVE_ZVT)
	fi
	LIBS="$SAVELIBS"
#	AC_PATH_PROG(vi_cv_gnome_config, gnome-config, no)
#	if test "$vi_cv_gnome_config" != "no"; then
#		ZVTLIBS=`$vi_cv_gnome_config zvt --libs`
#		GTKLIBS="$ZVTLIBS $GTKLIBS"
#	fi
fi

dnl If the user wants a Motif IPC front-end for nvi, build it.
AC_SUBST(MOTIFLIBS)
AC_MSG_CHECKING(if --enable-motif option specified)
AC_ARG_ENABLE(motif,
	[  --enable-motif          Build a Motif front-end for vi.],
	[vi_cv_motif=$enableval], [vi_cv_motif="no"])
AC_MSG_RESULT($vi_cv_motif)
if test "$vi_cv_motif" = "yes"; then
	vi_programs="$vi_programs vi-motif"
	vi_ipc=vi-ipc

	dnl XXX
	dnl BSDI's Motif libraries aren't shared.

	case "$host_os" in
	bsdi*)	CC=cc;;
	esac

	dnl XXX
	dnl The Xinside Motif distribution requires -lSM, -lICE and -lXext.
	dnl -lXext is generally available, the others aren't.
	dnl
	dnl XXX
	dnl Color icons require -lXpm, which may or may not be available,
	dnl depending on the version of X11.

	MOTIFLIBS="-lXm -lXt"
	AC_CHECK_LIB(SM, main,
	    [vi_cv_smlib="yes"], [vi_cv_smlib="no"], "$XLIBS")
	if test "$vi_cv_smlib" = "yes"; then
		MOTIFLIBS="$MOTIFLIBS -lSM"
	fi
	AC_CHECK_LIB(ICE, main,
	    [vi_cv_icelib="yes"], [vi_cv_icelib="no"], "$XLIBS")
	if test "$vi_cv_icelib" = "yes"; then
		MOTIFLIBS="$MOTIFLIBS -lICE"
	fi
	AC_CHECK_LIB(Xext, main,
	    [vi_cv_xextlib="yes"], [vi_cv_xextlib="no"], "$XLIBS")
	if test "$vi_cv_xextlib" = "yes"; then
		MOTIFLIBS="$MOTIFLIBS -lXext"
	fi
	AC_CHECK_LIB(Xpm, main,
	    [vi_cv_xpmlib="yes"], [vi_cv_xpmlib="no"], "$XLIBS")
	if test "$vi_cv_xpmlib" = "yes"; then
		MOTIFLIBS="$MOTIFLIBS -lXpm"
	fi
	MOTIFLIBS="$MOTIFLIBS $XLIBS -lm $LIBS"
fi

AC_SUBST(IPCOBJS)
dnl Check for threads
AC_MSG_CHECKING(if --enable-threads option specified)
AC_ARG_ENABLE(threads,
	[  --enable-threads        Turn on thread support.],
	[vi_cv_threads=$enableval], [vi_cv_threads="no"])
AC_MSG_RESULT($vi_cv_threads)
if test "$vi_cv_threads" = "yes"; then
	LIBS="$LIBS -lpthread"
	AC_CACHE_VAL(vi_cv_have_pthreads, [dnl
	AC_TRY_LINK([#include <pthread.h>],
		[pthread_self()],
		[vi_cv_have_pthreads=yes],
		[vi_cv_have_pthreads=no])])
	if test "$vi_cv_have_pthreads" = "no"; then
		AC_MSG_ERROR(No threading library found)
	fi
	IPCOBJS="pthread.o $IPCOBJS"
	AC_DEFINE(HAVE_PTHREAD)
else
	IPCOBJS="nothread.o $IPCOBJS"
fi

dnl If the user wants a Perl interpreter in nvi, load it.
AC_SUBST(shrpenv)
AC_SUBST(perlldflags)
AC_SUBST(perllibs)
AC_SUBST(vi_cv_perllib)
AC_MSG_CHECKING(if --enable-perlinterp option specified)
AC_ARG_ENABLE(perlinterp,
	[  --enable-perlinterp     Include a Perl interpreter in vi.],
	[vi_cv_perlinterp=$enableval], [vi_cv_perlinterp="no"])
AC_MSG_RESULT($vi_cv_perlinterp)
if test "$vi_cv_perlinterp" = "yes"; then
	if test "$vi_cv_path_perl" = no; then
		AC_MSG_ERROR([No perl5 utility found.])
	fi
	$vi_cv_path_perl -e 'require 5.004' || {
		AC_MSG_ERROR([perl5 must be version 5.004 or later.])
	}
	if test "$vi_cv_threads" = "yes"; then
		useithreads=`$vi_cv_path_perl -MConfig -e 'print $Config{useithreads}'`
		if test "X$useithreads" != "Xdefine"; then
			AC_MSG_ERROR([vi threading only compatible with perl's ithreads.])
		fi
	fi

	eval `$vi_cv_path_perl -V:shrpenv`
	vi_cv_perllib=`$vi_cv_path_perl -MConfig -e 'print $Config{privlib}'`
	perlcppflags=`$vi_cv_path_perl -Mlib=$srcdir -MExtUtils::Embed \
        	-e 'ccflags;perl_inc'`
	if test "X$perlcppflags" != "X"; then
		CPPFLAGS="$perlcppflags $CPPFLAGS"
	fi
	perllibs=`cd $srcdir;$vi_cv_path_perl -MExtUtils::Embed \
        	-e 'ldopts'`
	perlldflags=`cd $srcdir;$vi_cv_path_perl -MExtUtils::Embed \
        	-e 'ccdlflags'`
	LIBOBJS="perl.o perlxsi.o perlsfio.o $LIBOBJS"
	AC_DEFINE(HAVE_PERL_INTERP)
	AC_CACHE_CHECK([whether we need to use perl's setenv], 
		vi_cv_perl_setenv, [
		AC_TRY_RUN([
#include <EXTERN.h>
#include <perl.h>
#include <XSUB.h>
int main(){
#if defined(USE_ENVIRON_ARRAY) && !defined(PERL_USE_SAFE_PUTENV)
exit(0);
#else
exit(1);
#endif
}
		], [vi_cv_perl_setenv=yes], [vi_cv_perl_setenv=no])])
	if test "$vi_cv_perl_setenv" = yes; then
		AC_DEFINE(USE_PERL_SETENV)
	fi
fi

dnl If the user wants a Tk/Tcl interpreter in nvi, load it.  Make sure that
dnl we can find a Tk/Tcl library.
AC_MSG_CHECKING(if --enable-tclinterp option specified)
AC_ARG_ENABLE(tclinterp,
	[  --enable-tclinterp      Include a Tk/Tcl interpreter in vi.],
	[vi_cv_tclinterp=$enableval], [vi_cv_tclinterp="no"])
AC_MSG_RESULT($vi_cv_tclinterp)
if test "$vi_cv_tclinterp" = "yes"; then
	AC_MSG_CHECKING([for tclConfig.sh])
	AC_CACHE_VAL(vi_cv_tclconfig, [
		vi_cv_tclconfig=`$srcdir/findconfig`])
	AC_MSG_RESULT($vi_cv_tclconfig)
	if test "x$vi_cv_tclconfig" = x; then
		AC_MSG_ERROR([No Tcl library found;])
	fi
	. $vi_cv_tclconfig
	LIBOBJS="tcl.o $LIBOBJS"
	LIBS="$TCL_LIB_SPEC $TCL_LIBS $LIBS"
	AC_DEFINE(HAVE_TCL_INTERP)
fi

dnl check for curses
AC_SUBST(CURSLIBS)
AC_SUBST(CURSCPPFLAGS)
AC_SUBST(CURSLDFLAGS)
AC_SUBST(HAVE_NCURSESW)

dnl AC_ARG_WITH(slang,
dnl 	[  --with-slang[=DIR]     Use S-Lang instead of curses. ],
dnl 	[
dnl 	if test "$withval" != yes; then
dnl 		LDFLAGS="-L${withval}/lib $LDFLAGS"
dnl 		CPPFLAGS="-I${withval}/include $CPPFLAGS"
dnl 	fi
dnl 	AC_DEFINE(USE_SLANG_CURSES)
dnl         AC_CHECK_LIB(slang, SLtt_get_terminfo,
dnl                 [CURSLIBS="$CURSLIBS -lslang -lm"],
dnl                 [AC_MSG_ERROR(unable to compile.  check config.log)], -lm)
dnl 
dnl 	vi_cv_curses=slcurses
dnl 	],
dnl 	[
	SAVELIBS="$LIBS"
	SAVELDFLAGS="$LDFLAGS"
	SAVECPPFLAGS="$CPPFLAGS"
	AC_ARG_WITH(curses,
		[  --with-curses=DIR       Path to curses installation. ])
	if test "x$with_curses" != "x"; then
		CURSLDFLAGS="`echo $with_curses/lib | sed "$LRscript"` $CURSLDFLAGS"
		CURSCPPFLAGS="-I$with_curses/include $CURSCPPFLAGS"
	fi;
	LDFLAGS="$CURSLDFLAGS $LDFLAGS"
	CPPFLAGS="$CURSCPPFLAGS $CPPFLAGS"

	if test "$vi_cv_widechar" = "yes"; then
	    for vi_cv_curses in ncurses ncursesw curses; do
		AC_CHECK_LIB($vi_cv_curses, waddnwstr,[break])
		vi_cv_curses=unknown
	    done
	else 
	    for vi_cv_curses in ncurses ncursesw curses; do
		AC_CHECK_LIB($vi_cv_curses, initscr, [break])
		vi_cv_curses=unknown
	    done
	fi

	if test "$vi_cv_curses" != "unknown"; then
	    CURSHEADER=curses.h
	    if test "$vi_cv_curses" = "ncurses"; then
		AC_CHECK_HEADERS(ncurses.h, [CURSHEADER=ncurses.h])
	    fi
	    if test "$vi_cv_curses" = "ncursesw"; then
		AC_CHECK_HEADERS(ncursesw/ncurses.h, 
			[CURSHEADER=ncursesw/ncurses.h])
	    fi
	    vi_programs="vi $vi_programs"
	    CURSLIBS="-l$vi_cv_curses"
	else
	    AC_MSG_WARN([*** No suitable curses library found.])
	    if test "$vi_programs"X = X; then
		AC_MSG_ERROR([No executable to build.])
	    fi
	fi

dnl Check for the termcap/termlib library.  These two checks must occur in the
dnl current order, and -lcurses must be loaded before -ltermcap/-ltermlib.
dnl On Solaris curses==termlib, but different versions of curses
dnl can be used. Avoid loading termlib is curses has tgetent.
AC_CHECK_LIB($vi_cv_curses, tgetent,
	[vi_cv_curses_tgetent=yes], [vi_cv_curses_tgetent=no])
if test "$vi_cv_curses_tgetent" = no; then
	AC_CHECK_LIB(termlib, tgetent,
		[vi_cv_termlib=-ltermlib], [vi_cv_termlib=no])
	if test "$vi_cv_termlib" = no; then
		AC_CHECK_LIB(termcap, tgetent,
			[vi_cv_termlib=-ltermcap], [vi_cv_termlib=no])
	fi
fi
if test "$vi_cv_termlib" != no; then
	CURSLIBS="$CURSLIBS $vi_cv_termlib"
fi

	LIBS="$SAVELIBS"
	LDFLAGS="$SAVELDFLAGS"
	CPPFLAGS="$SAVECPPFLAGS"
dnl 	])

dnl Checks for header files.
AC_MSG_CHECKING(for sys/mman.h)
AC_CACHE_VAL(vi_cv_include_sys_mman, [dnl
AC_TRY_CPP([#include <sys/mman.h>],
	[vi_cv_include_sys_mman=yes], [vi_cv_include_sys_mman=no])])
if test "$vi_cv_include_sys_mman" = yes; then
	AC_DEFINE(HAVE_SYS_MMAN_H)
fi
AC_MSG_RESULT($vi_cv_include_sys_mman)

AC_MSG_CHECKING(for sys/select.h)
AC_CACHE_VAL(vi_cv_include_sys_select, [dnl
AC_TRY_CPP([#include <sys/select.h>],
	[vi_cv_include_sys_select=yes], [vi_cv_include_sys_select=no])])
if test "$vi_cv_include_sys_select" = yes; then
	AC_DEFINE(HAVE_SYS_SELECT_H)
fi
AC_MSG_RESULT($vi_cv_include_sys_select)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_CHECK_TYPE(ssize_t, int)
AC_C_BIGENDIAN
AC_C_CONST
AC_STRUCT_ST_BLKSIZE
AC_TYPE_MODE_T
AC_TYPE_OFF_T
AC_TYPE_PID_T
AC_TYPE_SIZE_T
AC_STRUCT_TM

dnl Checks for library functions.
VI_CV_REPLACE_FUNCS(bsearch gethostname memchr memset)
VI_CV_REPLACE_FUNCS(mkstemp mmap strdup strpbrk)
VI_CV_REPLACE_FUNCS(snprintf vsnprintf)

AC_CHECK_FUNCS(select)
AC_CHECK_FUNCS(setenv, [need_env=no], [need_env=yes])
AC_CHECK_FUNCS(strsep, [need_strsep=no], [need_strsep=yes])
AC_CHECK_FUNCS(unsetenv,, [need_env=yes])

AC_FUNC_MMAP
AC_FUNC_VFORK

dnl If we needed setenv or unsetenv, add in the clib/env.c replacement file.
if test "$need_env" = yes; then
	LIBOBJS="env.o $LIBOBJS"
fi

dnl If we need strsep, add it and define it so we get a prototype.
if test "$need_strsep" = yes; then
	LIBOBJS="strsep.o $LIBOBJS"
fi

dnl Check for fcntl/flock
dnl Use flock preferentially, since it has cleaner semantics and won't
dnl hang up the editor.
dnl XXX
dnl Ultrix has a broken fcntl, but a working flock.
dnl IRIX and DGUX have a broken flock, but working fcntl.
AC_MSG_CHECKING(for fcntl/flock)
AC_CACHE_VAL(vi_cv_lock, [dnl
	vi_cv_lock=none
	case "$host_os" in
	dgux*);;
	irix*);;
	*)
		AC_TRY_LINK([#include <fcntl.h>], [flock(0, 0);],
		    [vi_cv_lock=flock]);;
	esac
	if test "$vi_cv_lock" = none; then
		AC_TRY_LINK([#include <fcntl.h>], [fcntl(0, F_SETLK, 0);],
		    [vi_cv_lock=fcntl])
	fi])

if test "$vi_cv_lock" = flock; then
	AC_DEFINE(HAVE_LOCK_FLOCK)
fi
if test "$vi_cv_lock" = fcntl; then
	AC_DEFINE(HAVE_LOCK_FCNTL)
fi
AC_MSG_RESULT($vi_cv_lock)

dnl Check for ftruncate/chsize
AC_MSG_CHECKING(for ftruncate/chsize)
AC_CACHE_VAL(vi_cv_ftruncate, [dnl
AC_TRY_LINK([#include <unistd.h>], [ftruncate(0, 0);],
	[vi_cv_ftruncate=ftruncate],
AC_TRY_LINK([#include <unistd.h>], [chsize(0, 0);],
	[vi_cv_ftruncate=chsize], [vi_cv_ftruncate=no]))])
if test "$vi_cv_ftruncate" = ftruncate; then
	AC_DEFINE(HAVE_FTRUNCATE_FTRUNCATE)
fi
if test "$vi_cv_ftruncate" = chsize; then
	AC_DEFINE(HAVE_FTRUNCATE_CHSIZE)
fi
if test "$vi_cv_ftruncate" = no; then
	AC_MSG_ERROR([No file truncation system call.])
fi
AC_MSG_RESULT($vi_cv_ftruncate)

CSAVELIBS="$LIBS"
CSAVELDFLAGS="$LDFLAGS"
CSAVECPPFLAGS="$CPPFLAGS"
LIBS="$CURSLIBS $LIBS"
LDFLAGS="$CURSLDFLAGS $LDFLAGS"
CPPFLAGS="$CURSCPPFLAGS $CPPFLAGS"
dnl Check for the tigetstr/tigetnum functions.
AC_MSG_CHECKING(for tigetstr/tigetnum)
AC_CACHE_VAL(vi_cv_have_curses_tigetstr, [dnl
AC_TRY_LINK([#include <$CURSHEADER>], [tigetstr(0);],
	[vi_cv_have_curses_tigetstr=yes],
	[vi_cv_have_curses_tigetstr=no])])
if test "$vi_cv_have_curses_tigetstr" = yes; then
	AC_DEFINE(HAVE_CURSES_TIGETSTR)
fi
AC_MSG_RESULT($vi_cv_have_curses_tigetstr)

dnl Check for potentially missing curses functions in system or user-specified
dnl libraries.  We also have to guess at whether the specified library is a
dnl BSD or System V style curses.  Use the newterm function, all System V
dnl curses implementations have it, none, as far as I know, of the BSD ones do.
if test "$vi_cv_curses" = "bundled curses"; then
	AC_DEFINE(HAVE_BSD_CURSES)
	AC_DEFINE(HAVE_CURSES_WADDNSTR)
	AC_DEFINE(HAVE_CURSES_IDLOK)
else
	dnl Check for the waddnstr function.
	AC_MSG_CHECKING(for waddnstr)
	AC_CACHE_VAL(vi_cv_have_curses_waddnstr, [dnl
	AC_TRY_LINK([#include <$CURSHEADER>], [waddnstr(stdscr, 0, 0);],
		[vi_cv_have_curses_waddnstr=yes],
		[vi_cv_have_curses_waddnstr=no])])
	if test "$vi_cv_have_curses_waddnstr" = yes; then
		AC_DEFINE(HAVE_CURSES_WADDNSTR)
	fi
	AC_MSG_RESULT($vi_cv_have_curses_waddnstr)

	dnl Check for the beep function.
	AC_MSG_CHECKING(for beep)
	AC_CACHE_VAL(vi_cv_have_curses_beep, [dnl
	AC_TRY_LINK([#include <$CURSHEADER>], [beep();],
		[vi_cv_have_curses_beep=yes],
		[vi_cv_have_curses_beep=no])])
	if test "$vi_cv_have_curses_beep" = yes; then
		AC_DEFINE(HAVE_CURSES_BEEP)
	fi
	AC_MSG_RESULT($vi_cv_have_curses_beep)

	dnl Check for the flash function.
	AC_MSG_CHECKING(for flash)
	AC_CACHE_VAL(vi_cv_have_curses_flash, [dnl
	AC_TRY_LINK([#include <$CURSHEADER>], [flash();],
		[vi_cv_have_curses_flash=yes],
		[vi_cv_have_curses_flash=no])])
	if test "$vi_cv_have_curses_flash" = yes; then
		AC_DEFINE(HAVE_CURSES_FLASH)
	fi
	AC_MSG_RESULT($vi_cv_have_curses_flash)

	dnl Check for the idlok function.
	AC_MSG_CHECKING(for idlok)
	AC_CACHE_VAL(vi_cv_have_curses_idlok, [dnl
	AC_TRY_LINK([#include <$CURSHEADER>], [idlok(0, 0);],
		[vi_cv_have_curses_idlok=yes],
		[vi_cv_have_curses_idlok=no])])
	if test "$vi_cv_have_curses_idlok" = yes; then
		AC_DEFINE(HAVE_CURSES_IDLOK)
	fi
	AC_MSG_RESULT($vi_cv_have_curses_idlok)

	dnl Check for the keypad function.
	AC_MSG_CHECKING(for keypad)
	AC_CACHE_VAL(vi_cv_have_curses_keypad, [dnl
	AC_TRY_LINK([#include <$CURSHEADER>], [keypad(0, 0);],
		[vi_cv_have_curses_keypad=yes],
		[vi_cv_have_curses_keypad=no])])
	if test "$vi_cv_have_curses_keypad" = yes; then
		AC_DEFINE(HAVE_CURSES_KEYPAD)
	fi
	AC_MSG_RESULT($vi_cv_have_curses_keypad)

	dnl Check for the newterm function.
	AC_MSG_CHECKING(for newterm)
	AC_CACHE_VAL(vi_cv_have_curses_newterm, [dnl
	AC_TRY_LINK([#include <$CURSHEADER>], [newterm(0, 0, 0);],
		[vi_cv_have_curses_newterm=yes],
		[vi_cv_have_curses_newterm=no])])
	if test "$vi_cv_have_curses_newterm" = yes; then
		AC_DEFINE(HAVE_CURSES_NEWTERM)
	fi
	AC_MSG_RESULT($vi_cv_have_curses_newterm)

	if test "$vi_cv_have_curses_newterm" = no; then
		AC_DEFINE(HAVE_BSD_CURSES)
	fi
fi

dnl Check for the setupterm function.  We make this check regardless of
dnl using the system library, because it may be part of the underlying
dnl termcap/termlib support, and we want to use the local one.
AC_MSG_CHECKING(for setupterm)
AC_CACHE_VAL(vi_cv_have_curses_setupterm, [dnl
AC_TRY_LINK([#include <$CURSHEADER>], [setupterm(0, 0, 0);],
	[vi_cv_have_curses_setupterm=yes],
	[vi_cv_have_curses_setupterm=no])])
if test "$vi_cv_have_curses_setupterm" = yes; then
	AC_DEFINE(HAVE_CURSES_SETUPTERM)
fi
AC_MSG_RESULT($vi_cv_have_curses_setupterm)
LIBS="$CSAVELIBS"
LDFLAGS="$CSAVELDFLAGS"
CPPFLAGS="$CSAVECPPFLAGS"

dnl Some moron decided to drop off an argument from the gettimeofday call,
dnl without changing the name.
AC_MSG_CHECKING(for broken gettimeofday system call)
AC_CACHE_VAL(vi_cv_gettimeofday, [dnl
AC_TRY_LINK([#include <sys/types.h>
#include <sys/time.h>], [gettimeofday(0, 0);],
	[vi_cv_gettimeofday=okay], [vi_cv_gettimeofday=broken])])
if test "$vi_cv_gettimeofday" = broken; then
	AC_DEFINE(HAVE_BROKEN_GETTIMEOFDAY)
fi
AC_MSG_RESULT($vi_cv_gettimeofday)

dnl Check for which version of openpty to use, System V or Berkeley.
AC_MSG_CHECKING(for System V pty calls)
AC_CACHE_VAL(vi_cv_sys5_pty, [dnl
AC_TRY_LINK(, [grantpt(0);],
	[vi_cv_sys5_pty=yes], [vi_cv_sys5_pty=no])])
if test "$vi_cv_sys5_pty" = yes; then
	AC_DEFINE(HAVE_SYS5_PTY)
fi
AC_MSG_RESULT($vi_cv_sys5_pty)

dnl Check for the revoke system call.
AC_MSG_CHECKING(for revoke system call)
AC_CACHE_VAL(vi_cv_revoke, [dnl
AC_TRY_LINK(, [revoke("a");],
	[vi_cv_revoke=yes], [vi_cv_revoke=no])])
if test "$vi_cv_revoke" = yes; then
	AC_DEFINE(HAVE_REVOKE)
fi
AC_MSG_RESULT($vi_cv_revoke)

dnl Some versions of sprintf return a pointer to the first argument instead
dnl of a character count.  We assume that the return value of snprintf and
dnl vsprintf etc. will be the same as sprintf, and check the easy one.
AC_MSG_CHECKING(for int type sprintf return value)
AC_CACHE_VAL(vi_cv_sprintf_count, [dnl
AC_TRY_RUN([main(){char buf[20]; exit(sprintf(buf, "XXX") != 3);}],
	[vi_cv_sprintf_count=yes], [vi_cv_sprintf_count=no])])
if test "$vi_cv_sprintf_count" = no; then
	AC_DEFINE(SPRINTF_RET_CHARPNT)
fi
AC_MSG_RESULT($vi_cv_sprintf_count)


dnl Check for the standard shorthand types.
AC_SUBST(u_char_decl)
AC_CACHE_CHECK([for u_char], vi_cv_uchar, [dnl
AC_TRY_COMPILE([#include <sys/types.h>], u_char foo;,
	[vi_cv_uchar=yes], [vi_cv_uchar=no])])
if test "$vi_cv_uchar" = no; then
	u_char_decl="typedef unsigned char u_char;"
fi

AC_SUBST(u_short_decl)
AC_CACHE_CHECK([for u_short], vi_cv_ushort, [dnl
AC_TRY_COMPILE([#include <sys/types.h>], u_short foo;,
	[vi_cv_ushort=yes], [vi_cv_ushort=no])])
if test "$vi_cv_ushort" = no; then
	u_short_decl="typedef unsigned short u_short;"
fi

AC_SUBST(u_int_decl)
AC_CACHE_CHECK([for u_int], vi_cv_uint, [dnl
AC_TRY_COMPILE([#include <sys/types.h>], u_int foo;,
	[vi_cv_uint=yes], [vi_cv_uint=no])])
if test "$vi_cv_uint" = no; then
	u_int_decl="typedef unsigned int u_int;"
fi

AC_SUBST(u_long_decl)
AC_CACHE_CHECK([for u_long], vi_cv_ulong, [dnl
AC_TRY_COMPILE([#include <sys/types.h>], u_long foo;,
	[vi_cv_ulong=yes], [vi_cv_ulong=no])])
if test "$vi_cv_ulong" = no; then
	u_long_decl="typedef unsigned long u_long;"
fi

dnl DB/Vi use specific integer sizes.
AC_SUBST(u_int8_decl)
AC_CACHE_CHECK([for u_int8_t], vi_cv_uint8, [dnl
AC_TRY_COMPILE([#include <sys/types.h>], u_int8_t foo;,
	[vi_cv_uint8=yes],
	AC_TRY_RUN([main(){exit(sizeof(unsigned char) != 1);}],
	    [vi_cv_uint8="unsigned char"], [vi_cv_uint8=no]))])
if test "$vi_cv_uint8" = no; then
	AC_MSG_ERROR(No unsigned 8-bit integral type.)
fi
if test "$vi_cv_uint8" != yes; then
	u_int8_decl="typedef $vi_cv_uint8 u_int8_t;"
fi

AC_SUBST(u_int16_decl)
AC_CACHE_CHECK([for u_int16_t], vi_cv_uint16, [dnl
AC_TRY_COMPILE([#include <sys/types.h>], u_int16_t foo;,
	[vi_cv_uint16=yes],
AC_TRY_RUN([main(){exit(sizeof(unsigned short) != 2);}],
	[vi_cv_uint16="unsigned short"],
AC_TRY_RUN([main(){exit(sizeof(unsigned int) != 2);}],
	[vi_cv_uint16="unsigned int"], [vi_cv_uint16=no])))])
if test "$vi_cv_uint16" = no; then
	AC_MSG_ERROR([No unsigned 16-bit integral type.])
fi
if test "$vi_cv_uint16" != yes; then
	u_int16_decl="typedef $vi_cv_uint16 u_int16_t;"
fi

AC_SUBST(int16_decl)
AC_CACHE_CHECK([for int16_t], vi_cv_int16, [dnl
AC_TRY_COMPILE([#include <sys/types.h>], int16_t foo;,
	[vi_cv_int16=yes],
AC_TRY_RUN([main(){exit(sizeof(short) != 2);}],
	[vi_cv_int16="short"],
AC_TRY_RUN([main(){exit(sizeof(int) != 2);}],
	[vi_cv_int16="int"], [vi_cv_int16=no])))])
if test "$vi_cv_int16" = no; then
	AC_MSG_ERROR([No signed 16-bit integral type.])
fi
if test "$vi_cv_int16" != yes; then
	int16_decl="typedef $vi_cv_int16 int16_t;"
fi

AC_SUBST(u_int32_decl)
AC_CACHE_CHECK([for u_int32_t], vi_cv_uint32, [dnl
AC_TRY_COMPILE([#include <sys/types.h>], u_int32_t foo;,
	[vi_cv_uint32=yes],
AC_TRY_RUN([main(){exit(sizeof(unsigned int) != 4);}],
	[vi_cv_uint32="unsigned int"],
AC_TRY_RUN([main(){exit(sizeof(unsigned long) != 4);}],
	[vi_cv_uint32="unsigned long"], [vi_cv_uint32=no])))])
if test "$vi_cv_uint32" = no; then
	AC_MSG_ERROR([No unsigned 32-bit integral type.])
fi
if test "$vi_cv_uint32" != yes; then
	u_int32_decl="typedef $vi_cv_uint32 u_int32_t;"
fi

AC_SUBST(int32_decl)
AC_CACHE_CHECK([for int32_t], vi_cv_int32, [dnl
AC_TRY_COMPILE([#include <sys/types.h>], int32_t foo;,
	[vi_cv_int32=yes],
AC_TRY_RUN([main(){exit(sizeof(int) != 4);}],
	[vi_cv_int32="int"],
AC_TRY_RUN([main(){exit(sizeof(long) != 4);}],
	[vi_cv_int32="long"], [vi_cv_int32=no])))])
if test "$vi_cv_int32" = no; then
	AC_MSG_ERROR([No signed 32-bit integral type.])
fi
if test "$vi_cv_int32" != yes; then
	int32_decl="typedef $vi_cv_int32 int32_t;"
fi

AC_CACHE_CHECK([return type of fprintf], vi_cv_type_fprintf,
[AC_TRY_COMPILE([#include <stdio.h>
int fprintf ();
],[int i;], vi_cv_type_fprintf=int, vi_cv_type_fprintf=void)])

AC_SUBST(NEED_FPRINTF_PROTO)
AC_CACHE_CHECK([fprintf prototype needed], vi_cv_proto_fprintf,
[AC_TRY_COMPILE([ #include <stdio.h>
typedef int     (*funcPtr)();
],[funcPtr ptr = (funcPtr) fprintf;], 
vi_cv_proto_fprintf=no, 
[vi_cv_proto_fprintf=yes])])
if test "$vi_cv_proto_fprintf" = yes; then
	AC_DEFINE(NEED_FPRINTF_PROTO)
fi

AC_ARG_WITH(db_type,
	[  --with-db=bundled|system Which db to use. ])
case "$with_db_type" in
"db1")
	AC_MSG_WARN([Use of db1 is not officially supported.])
	AC_DEFINE(USE_DB1)
	;;
"system")
	;;
*)
	with_db_type="bundled"
	;;
esac

dnl Check for DB 3
AC_ARG_WITH(db_prefix,
	[  --with-db-prefix=PFX    Path to db installation. ])

SAVELDFLAGS="$LDFLAGS"
if test "x$with_db_prefix" != "x"; then
	LDFLAGS="-L$with_db_prefix/lib $LDFLAGS"
	CPPFLAGS="-I$with_db_prefix/include $CPPFLAGS"
fi
if test "$with_db_type" != db1; then
	SAVELIBS="$LIBS"

	LIBS="$LIBS -ldb"
	AC_TRY_LINK([#include <db.h>],
		[db_create(NULL,NULL,0)],
		[vi_cv_dbfatal="no"], [vi_cv_dbfatal="yes"])
	if test "$vi_cv_dbfatal" = "yes"; then
		AC_MSG_ERROR([Need DB 3.])
	fi

	LIBS="$SAVELIBS"
fi
LDFLAGS="$SAVELDFLAGS"

AC_SUBST(dl_src)
AC_MSG_CHECKING(if --enable-dynamic-loading option specified)
AC_ARG_ENABLE(dynamic-loading,
	[  --enable-dynamic-loading Load DB 3 dynamically.],
	[vi_cv_dl=$enableval], [vi_cv_dl="no"])
AC_MSG_RESULT($vi_cv_dl)
if test "$vi_cv_dl" = yes; then
	AC_CHECK_LIB(dl, dlopen,
		[vi_cv_dlfatal="no"], [vi_cv_dlfatal="yes"])
	if test "$vi_cv_dlfatal" = "yes"; then
		AC_MSG_ERROR([Need dl to support dynamic loading.])
	fi
	OLDPATH="$PATH"

	PATH="$with_db_prefix/lib:/usr/lib:/lib"
	AC_PATH_PROG(vi_cv_path_db3, libdb-3.so, no)
	if test "$vi_cv_path_db3" = no; then
		AC_MSG_ERROR([Path of libdb-3.so not found.])
	fi
	PATH="$OLDPATH"

	AC_DEFINE(USE_DYNAMIC_LOADING)
	LIBOBJS="dldb.o $LIBOBJS"
	dl_src=../common/dldb.c
	LIBS="-ldl $LIBS"
else
	LIBS="-ldb $LIBS"
	if test "X$with_db_prefix" != "X"; then
		LDFLAGS="`echo $with_db_prefix/lib | sed "$LRscript"` $LDFLAGS"
	fi
fi

AC_ARG_WITH(db_build,
	[  --with-db-build=prefix  Path to db build. ])
if test "x$with_db_build" != "x"; then
	vi_cv_dbsrc=`$AWK '/^srcdir/ {gsub("srcdir[[ \t]]*=","");print $1}' \
		     $with_db_build/Makefile`
	case $with_db_build in
	*[[\\/]]);;
	*)
	    with_db_build="$with_db_build/";;
	esac
	case $vi_cv_dbsrc in
	[[\\/]]*);;
	*)
	    vi_cv_dbsrc="$with_db_build$vi_cv_dbsrc";;
	esac
fi;

vi_cv_db4="no"
AC_ARG_ENABLE(log4,
	[  --enable-db4-logging    Enable experimental/broken db4 logging.],
	[
	AC_MSG_CHECKING(for DB 4)
	AC_EGREP_CPP([^4],
		[#include <db.h>
	DB_VERSION_MAJOR],
		[vi_cv_db4="yes"], [vi_cv_db4="no"])
	AC_MSG_RESULT($vi_cv_db4)
	])
if test "$vi_cv_db4" = "yes"; then
	CPPFLAGS="-I$vi_cv_dbsrc/include $CPPFLAGS"
	CPPFLAGS="-I$vi_cv_dbsrc/include_auto $CPPFLAGS"
	CPPFLAGS="-I$with_db_build $CPPFLAGS"
	AC_DEFINE(USE_DB4_LOGGING)
	LIBOBJS="log4.o vi_auto.o vi_rec.o $LIBOBJS"
else
	LIBOBJS="log.o $LIBOBJS"
fi

dnl We compile in nvi's RE routines unless the user specifies otherwise.
AC_MSG_CHECKING(if --disable-re option specified)
AC_ARG_ENABLE(re,
	[  --disable-re            DON'T use the nvi-provided RE routines.],
	[if test "$enable_re" = "yes"; then
		vi_cv_re_lib="bundled RE"
	else
		vi_cv_re_lib="other RE"
	fi],[vi_cv_re_lib="bundled RE"])
AC_MSG_RESULT($vi_cv_re_lib)
case "$vi_cv_re_lib" in
"bundled RE")
	CPPFLAGS="-I\$(visrcdir)/regex $CPPFLAGS"
	LIBOBJS="regcomp.o regerror.o regexec.o regfree.o $LIBOBJS";;
"other RE")
	;;
esac

AC_OUTPUT(Makefile port.h:port.h.in
    pathnames.h:pathnames.h.in recover:recover.in)
