
#$Id: autoconf,v 2.15 92/01/31 13:01:34 berg Rel $

SHELL=/bin/sh || exec /bin/sh autoconf $* # we're in a csh, feed myself to sh

# All possible entries in autoconf.h:

#	#define const
#	#define volatile
#	#define void char
#	typedef int mode_t;
#	typedef int pid_t;
#	typedef int uid_t;
#	typedef int gid_t;
#	typedef unsigned size_t;
#	typedef long time_t;
#	#define NOmemmove
#	#define NObcopy
#	#define NOstrstr
#	#define NOstrcspn
#	#define NOstrpbrk
#	#define NOrename
#	#define strchr(s,c) index(s,c)
#	#define endpwent()
#	#define strtol(str,ptr,base) ((long)atoi(str))
#	#define oBRAIN_DAMAGE
#	#define SMALLHEAP
#	#define SYSTEM_MAILBOX "/usr/spool/mail/$USER"
#	#define SENDMAIL "/usr/lib/sendmail"

# A conforming ANSI compiler and POSIX library should only produce four
# entries in autoconf.h: endpwent(), rename(), SYSTEM_MAILBOX, SENDMAIL (and
# perhaps SMALLHEAP)
# Anything else indicates failure of your installation to comply with either
# the ANSI or POSIX standards (but procmail should be installable anyway).

PATH=:$PATH
ACONF=$3
MAKE=$2
DEVNULL=/dev/null
export SHELL
if test -f $ACONF
then
 trap "exit 1" 1 2 3 15
else
 trap "rm -f $ACONF; exit 1" 1 2 3 15
fi

cat >grepfor <<HERE
if fgrep "\$1" _autotst.rrr >$DEVNULL
then
 echo "\$2" >>$ACONF
 exit 0
fi
exit 1
HERE
chmod 0755 grepfor

cat >$ACONF <<HERE
/* This file was automagically generated by autoconf */

HERE

# WARNING: in ./include/stdlib.h the const keyword is already used!
#	   hence the const test has to precede all others.

cat >_autotst.c <<HERE
main()
{ char*const*p;char*q;
  p= &q;return 0;
}
HERE

echo 'Testing for const'
if ${MAKE} _autotst.$1 >_autotst.rrr 2>&1 && test -f _autotst.$1
then
 grepfor const '#define const'
else
 echo '#define const' >>$ACONF
fi
rm -f _autotst.$1

cat >_autotst.c <<HERE
main(){volatile int i;return(i=0);}
HERE

echo 'Testing for volatile'
if ${MAKE} _autotst.$1 >$DEVNULL 2>&1 && test -f _autotst.$1
then
:
else
 echo '#define volatile' >>$ACONF
fi
rm -f _autotst.$1

cat >_autotst.c <<HERE
#include "includes.h"
void*vvoid;
main(){int i;char*p="t";
 {size_t vsize_t;i=vsize_t=1;}
 {pid_t vpid_t;i=vpid_t=1;}
 {time_t vtime_t;i=vtime_t=1;}
 {mode_t vmode_t;i=vmode_t=1;}
 {uid_t vuid_t;i=vuid_t=1;}
 {gid_t vgid_t;i=vgid_t=1;}
 vvoid=p;
 return !vvoid;}
HERE

echo 'Testing for void*,size_t,pid_t,time_t,mode_t,uid_t,gid_t'
${MAKE} _autotst.$1 >_autotst.rrr 2>&1
rm -f _autotst.$1

grepfor void '#define void char'
grepfor size_t 'typedef unsigned size_t;'
grepfor pid_t 'typedef int pid_t;'
grepfor time_t 'typedef long time_t;'
grepfor mode_t 'typedef int mode_t;'
grepfor uid_t 'typedef int uid_t;'
grepfor gid_t 'typedef int gid_t;'

cat >_autotst.c <<HERE
#include "includes.h"
main(){char a[2];
 endpwent();memmove(a,"0",1);bcopy("0",a,1);strstr(a,"0");strcspn(a,"0");
 strtol("0",(char**)0,10);strchr("0",'0');strpbrk(a,"0");rename(a,"0");
 return 0;}
HERE

echo 'Testing for memmove, strstr, strchr, strcspn & strtol'
${MAKE} _autotst.$1 >$DEVNULL 2>&1
${MAKE} _autotst >_autotst.rrr 2>&1
rm -f _autotst _autotst.$1

grepfor strstr '#define NOstrstr'
grepfor strcspn '#define NOstrcspn'
grepfor strpbrk '#define NOstrpbrk'
grepfor rename '#define NOrename'
grepfor strchr '#define strchr(s,c) index(s,c)'
grepfor setpwent '#define setpwent()'
grepfor endpwent '#define endpwent()'
grepfor strtol '#define strtol(str,ptr,base) ((long)atoi(str))'
grepfor memmove '#define NOmemmove' &&
if fgrep bcopy _autotst.rrr >$DEVNULL
then
 echo '#define NObcopy' >>$ACONF

 echo 'Testing for brain damage'
 cat >_autotst.c <<HERE
#include "includes.h"
struct tests{int a,b;};
main(){
 return offsetof(struct tests,b);}
HERE
 if ${MAKE} _autotst.$1 >$DEVNULL 2>&1
 then
    :
 else
    echo 'Yep, it is'			# ISC chokes on its own offsetof()
    echo '#define oBRAIN_DAMAGE' >>$ACONF
 fi
 rm -f _autotst.$1

else

 cat >_autotst.c <<HERE
#include "includes.h"
#define M256	256
#define F33	33
main(){int j=0,i=M256-1;static char a[M256];
 do a[i]=i;while(i--);
 bcopy(a+F33,a,M256-F33);bcopy(a,a+F33,M256-F33);i=F33-1;
 do j|=a[i]!=(char)(i+F33);while(i--);i=M256-1;
 do j|=a[i]!=(char)i;while(--i!=F33-1);return!j;}
HERE

 echo 'Testing for bcopy handling overlaps'
 ${MAKE} _autotst >$DEVNULL 2>&1

 if _autotst
 then
   echo 'Sorry, incompetent bcopy'
   echo '#define NObcopy' >>$ACONF
 fi
 rm -f _autotst
fi

cat >_autotst.c <<HERE
#include "includes.h"
main(){unsigned long s=(size_t)~0;int i;
 for(i=1;s>>=1;++i);
 if(i<=16)
    puts("#define SMALLHEAP");
 return 0;}
HERE

echo 'Determining the most applicable block size'

${MAKE} _autotst >$DEVNULL 2>&1
_autotst >>$ACONF
rm -f _autotst

if test -d /usr/spool/mail
then
 echo '#define SYSTEM_MAILBOX "/usr/spool/mail/$USER"' >>$ACONF
elif test -d /usr/mail
then
 echo '#define SYSTEM_MAILBOX "/usr/mail/$USER"' >>$ACONF
else
 echo '#define SYSTEM_MAILBOX "$HOME/.mail"' >>$ACONF
 echo Could not find the system-mailbox directory, supplied substitute
fi

cat >lookfor <<HERE
for a in /usr/lib /lib /usr/etc /etc /usr/bin /bin /usr/local/bin /usr/lbin \
/usr/local/lib /usr/local
do
 if test -f "\$a/\$1"
 then
    echo "#define SENDMAIL \"\$a/\$1\"" >>$ACONF
    exit 0
 fi
done
exit 1
HERE
chmod 0755 lookfor

if lookfor sendmail || lookfor smail
then
:
else
 echo 'Could not find any mailer.  It should be a mailer accepting at least'
 echo "one plain destination address as it's only argument (any sendmail"
 echo 'compatible mailer will do), and the mail-to-be-sent on stdin.'
 echo 'What is your mailer called?  (You can override this in config.h)'
 read a
 echo "#define SENDMAIL \"$a\"" >>$ACONF
fi

rm -f _autotst* lookfor grepfor

echo -----------------------------autoconf.h-----------------------------------
cat $ACONF >&2
echo --------------------------------------------------------------------------
