dnl	This file is an input file used by the GNU "autoconf" program to
dnl	generate the file "configure", which is run during Tcl installation
dnl	to configure the system for the local environment.

AC_INIT(tcl.h)
AC_PROG_INSTALL
AC_PROG_RANLIB

dnl--------------------------------------------------------------------
dnl	Supply substitutes for missing POSIX library procedures, or set
dnl	flags so Tcl uses alternate procedures.
dnl--------------------------------------------------------------------

AC_REPLACE_FUNCS(getcwd opendir strerror strstr strtod)
AC_REPLACE_FUNCS(strtol strtoul tmpnam waitpid)
AC_FUNC_CHECK(gettimeofday, , AC_DEFINE(NO_GETTOD))
AC_FUNC_CHECK(getwd, , AC_DEFINE(NO_GETWD))
AC_FUNC_CHECK(wait3, , AC_DEFINE(NO_WAIT3))


dnl--------------------------------------------------------------------
dnl	Supply substitutes for missing POSIX header files.  Special
dnl	notes:
dnl	    - Sprite's dirent.h exists but is bogus.
dnl	    - stdlib.h doesn't define strtol, strtoul, or
dnl	      strtod insome versions of SunOS
dnl	    - some versions of string.h don't declare procedures such
dnl	      as strstr
dnl--------------------------------------------------------------------

AC_UNISTD_H
AC_COMPILE_CHECK(dirent.h, [#include <sys/types.h>
#include <dirent.h>], [
DIR *d;
struct dirent *entryPtr;
char *p;
d = opendir("foobar");
entryPtr = readdir(d);
p = entryPtr->d_name;
closedir(d);
], , AC_DEFINE(NO_DIRENT_H))
AC_HEADER_EGREP([Sprite version.* NOT POSIX], AC_DEFINE(NO_DIRENT_H))
AC_HEADER_CHECK(float.h, , AC_DEFINE(NO_FLOAT_H))
AC_HEADER_CHECK(limits.h, , AC_DEFINE(NO_LIMITS_H))
AC_HEADER_CHECK(stdlib.h, , AC_DEFINE(NO_STDLIB_H))
AC_HEADER_EGREP(strtol, stdlib.h, , AC_DEFINE(NO_STDLIB_H))
AC_HEADER_EGREP(strtoul, stdlib.h, , AC_DEFINE(NO_STDLIB_H))
AC_HEADER_EGREP(strtod, stdlib.h, , AC_DEFINE(NO_STDLIB_H))
AC_HEADER_CHECK(string.h, , AC_DEFINE(NO_STRING_H))
AC_HEADER_EGREP(strstr, string.h, , AC_DEFINE(NO_STRING_H))
AC_HEADER_EGREP(strerror, string.h, , AC_DEFINE(NO_STRING_H))
AC_HEADER_CHECK(sys/time.h, , AC_DEFINE(NO_SYS_TIME_H))
AC_HEADER_CHECK(sys/wait.h, , AC_DEFINE(NO_SYS_WAIT_H))

dnl--------------------------------------------------------------------
dnl	On some systems strstr is broken: it returns a pointer even
dnl	even if the original string is empty.
dnl--------------------------------------------------------------------

AC_TEST_PROGRAM([
extern int strstr();
int main()
{
    exit(strstr("\0test", "test") ? 1 : 0);
}
],  , [LIBOBJS="$LIBOBJS strstr.o"])

dnl--------------------------------------------------------------------
dnl	Under some versions of AIX strtoul returns an incorrect terminator
dnl	pointer for the string "0".
dnl--------------------------------------------------------------------

AC_TEST_PROGRAM([
extern int strtoul();
int main()
{
    char *string = "0";
    char *term;
    int value;
    value = strtoul(string, &term, 0);
    if ((value != 0) || (term != (string+1))) {
        exit(1);
    }
    exit(0);
}], , [LIBOBJS="$LIBOBJS strtoul.o"])

dnl--------------------------------------------------------------------
dnl	Check for missing pid_t and uid_t typedefs.
dnl--------------------------------------------------------------------

AC_PID_T
AC_UID_T

dnl--------------------------------------------------------------------
dnl	If a system doesn't have an opendir function (man, that's old!)
dnl	then we have to supply a different version of dirent.h which
dnl	is compatible with the substitute version of opendir that's
dnl	provided.  This version only works with V7-style directories.
dnl--------------------------------------------------------------------

AC_FUNC_CHECK(opendir, , AC_DEFINE(USE_DIRENT2_H))

dnl--------------------------------------------------------------------
dnl	Check for the existence of sys_errlist (this is only needed if
dnl	there's no strerror, but I don't know how to conditionalize the
dnl	check).
dnl--------------------------------------------------------------------

AC_COMPILE_CHECK(sys_errlist, , [
extern char *sys_errlist[];
extern int sys_nerr;
sys_errlist[sys_nerr-1][0] = 0;
], , AC_DEFINE(NO_SYS_ERRLIST))

dnl--------------------------------------------------------------------
dnl	The check below checks whether <sys/wait.h> defines the type
dnl	"union wait" correctly.  It's needed because of weirdness in
dnl	HP-UX where "union wait" is defined in both the BSD and SYS-V
dnl	environments.  Checking the usability of WIFEXITED seems to do
dnl	the trick.
dnl--------------------------------------------------------------------

AC_COMPILE_CHECK([union wait], [#include <sys/types.h> 
#include <sys/wait.h>], [
union wait x;
WIFEXITED(x);		/* Generates compiler error if WIFEXITED
			 * uses an int. */
], , AC_DEFINE(NO_UNION_WAIT))

dnl--------------------------------------------------------------------
dnl	The code below cleans up the DEFS variable to eliminate
dnl	duplicate entries.  This makes the eventual make output
dnl	look a bit cleaner.
dnl--------------------------------------------------------------------

newDefs=""
for i in $DEFS; do
    if test -z "$newDefs"; then
        newDefs=$i
    elif echo $newDefs | fgrep -v -e $i >/dev/null 2>&1; then
        newDefs="$newDefs $i"
    fi
done
DEFS=$newDefs

AC_OUTPUT(Makefile)
