File: tfconfig

package info (click to toggle)
tf 1%3A4.0s1-22
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,132 kB
  • sloc: ansic: 15,621; sh: 44; makefile: 38
file content (1004 lines) | stat: -rwxr-xr-x 29,653 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
#!/bin/sh
########################################################################
#  TinyFugue - programmable mud client
#  Copyright (C) 1994 - 1999 Ken Keys
#
#  TinyFugue (aka "tf") is protected under the terms of the GNU
#  General Public License.  See the file "COPYING" for details.
#
#  DO NOT EDIT THIS FILE.
#  Any configuration changes should be made to the unix/Config file.
########################################################################

# TinyFugue automatic configuration
# Descriptor 4 is ../src/config.h, descriptor 5 is vars.mak.

### A few portability notes.
# Some old shells don't support the ':' in ${VAR:-default} syntax.
# Some old shells don't support shell functions.
# Some versions of 'test' have the wrong precedence for unary operators,
#   so we use the shell's '&&' and '||' operators instead of '-a' and '-o'.
# 4.3 BSD 'test' does not have '-x'.  Use '-f' or '-r' instead.
# Some stupid compilers return 0 even if linking fails, so we explicitly
#   test for the existance of the executable.
# ULTRIX 3.x has a truly lame /bin/sh.  ULTRIX 4.x is okay.

### Some notes on predefined preprocessor identifiers.
# M_UNIX	SCO UNIX
# hpux		HP/UX
# sequent	Dynix
# _SEQUENT_	Dynix/ptx

TFVERSION=`sed -n -e '/Fugue version/s/^.*"\(.*\)".*$/\1/p' ../src/main.c`

echo "Configuring $TFVERSION"; echo

### Initialize variables, just in case they're already defined.

AOUT=tfconfig.out
BINDIR=''
# CC=''		;# Use the value from the environment if there is one.
CCFLAGS=${TFDEV_CCFLAGS-'-O'}
# CPP=''	;# Use the value from the environment if there is one.
CYGWIN32=0
DMALLOC="${TFDEV_DMALLOC}"
TF="${TFDEV_TF}"
FLAGS=''
GNU_C=0
LIBDIR="${TFDEV_LIBDIR}"
LIBS=''
MAILDIR=''
# MAKE=''	;# Use the value from the environment if there is one.
MANPAGE=''
MANTYPE='cat'
MODE=''
POSIX=0
QUIET="${TFDEV_QUIET}"
SOCKS=''
STD_C=0
STRIP="${TFDEV_STRIP-strip}"
SYMLINK="${TFDEV_SYMLINK}"
TERMINAL=''
TFVER="$*"
TTYDRIVER=''
X=''

if [ -z "$USER" ]; then USER=$LOGNAME; fi
export USER

UNAME=`{ uname -smo; } 2>/dev/null`
if test -n "$SOURCE_DATE_EPOCH"; then
    echo "#define UNAME     \"\"" >&4
else
    echo "#define UNAME     " \"$UNAME\" >&4
fi
case "$UNAME" in
"SunOS 5.4")
    echo "#define SUNOS_5_4" >&4
    ;;
"CYGWIN32"*)
    TERMINAL="HARDCODE TERM_ansi"
    AOUT="a.exe"
    X=".exe"
    TF="${TFDEV_TF}${X}"
    CYGWIN32=1
    ;;
esac

### Read in user definitions.
if [ -r ./Config ]; then . ./Config; fi

echo '#### DO NOT EDIT THIS FILE.' >&5
echo '#### This file was automatically generated by unix/tfconfig.' >&5
echo '#### Any configuration changes should be made in unix/Config.' >&5
echo >&5
echo "TFVERSION  = \"${TFVERSION}\"" >&5
echo "X          = ${X}" >&5
echo "O          = o" >&5

cat >&4 <<EOF
/* config.h
 * This file was automatically generated by unix/tfconfig.
 */
#define PLATFORM_UNIX
EOF

if [ -z "$QUIET" ] && echo $TFVERSION | egrep "alpha" >/dev/null 2>&1; then
cat <<EOF
This is an alpha version, and so may not be fully tested on some platforms.
With the new features comes the possibility of new bugs.  If you don't want
to deal with this, get the latest beta version.

EOF
fi


### Figure out installation locations.

if [ -z "$BINDIR" ]; then
    if [ -w /usr/local/bin ]; then
        BINDIR=/usr/local/bin
    else
        [ -d $HOME/bin ] && BINDIR="$HOME/bin" || BINDIR="$HOME"
    fi
fi

if [ -z "$LIBDIR" ]; then
    if [ -w /usr/local/lib ]; then
        LIBDIR=/usr/local/lib/tf-lib
    else
        [ -d $HOME/lib ] && LIBDIR="$HOME/lib/tf-lib" || LIBDIR="$HOME/tf-lib"
    fi
fi


### Find mail directory by looking at $MAIL, then looking in the usual places.

if [ -z "$MAILDIR" ]; then
    if [ -n "$MAIL" ]; then
#       Not everybody has dirname.  sigh.
        MAILDIR=`echo $MAIL | sed 's;/[^/]*$;;'`
    elif [ -d /usr/spool/mail ]; then
        MAILDIR="/usr/spool/mail"
    elif [ -d /var/spool/mail ]; then
        MAILDIR="/var/spool/mail"
    elif [ -d /usr/mail ]; then
        MAILDIR="/usr/mail"
    elif [ -d /var/mail ]; then
        MAILDIR="/var/mail"
    else
        MAILDIR=""
    fi
fi


[ -z "$MODE" ]      && MODE=755
[ -z "$TF" ]        && TF="${BINDIR}/tf${X}"


### Sanity check for filenames (some people do pretty stupid things)
# The "${SYMLINK}/" is just to make the grep return false if SYMLINK is empty.

if echo "$TF"          | egrep "^[^/]" >/dev/null 2>&1 ||
   echo "$LIBDIR"      | egrep "^[^/]" >/dev/null 2>&1 ||
   echo "${SYMLINK}/"  | egrep "^[^/]" >/dev/null 2>&1
then
   echo "Don't install files with relative paths or '~'."
   echo "Edit unix/Config and try again."
   exit 2
fi

# Don't allow installation in the build tree (people have actually tried this).
# The cd;pwd is needed to normalize the directory name in case of links, etc.

DIR1=`echo $TF      | sed 's;/[^/]*$;;'`
#DIR1=`cd $DIR1 && pwd`
DIR2=`echo $LIBDIR  | sed 's;/[^/]*$;;'`
#DIR2=`cd $DIR2 && pwd`
DIR3=`echo $SYMLINK | sed 's;/[^/]*$;;'`
#DIR3=`cd $DIR3 && pwd`

if [ -z "$DIR1" ] || [ -z "$DIR2" ]; then
    echo "Error in directory."
    exit 1;
fi

#DIR1=`cd $DIR1 && pwd || { echo "Error in directory $DIR1."; false; }`

BUILDTREE=`cd .. && pwd`
if echo "${DIR1}/" | egrep "^${BUILDTREE}/" >/dev/null 2>&1 ||
   echo "${DIR2}/" | egrep "^${BUILDTREE}/" >/dev/null 2>&1 ||
   echo "${DIR3}/" | egrep "^${BUILDTREE}/" >/dev/null 2>&1
then
    echo
    echo 'You can not install files in the build tree,'
    echo "${BUILDTREE}."
    echo 'Edit unix/Config and try again.'
    exit 2
fi

# Too many people have renamed the distribution directory to "$HOME/tf", and
# then wondered why they couldn't install the executable as "$HOME/tf".
if [ -d "${TF}" ]; then
    echo "\"${TF}\" is a directory; you can not install the executable"
    echo 'with the same name.  Either rename the directory, or choose a'
    echo 'different name for the executable.'
    exit 1
fi


### Confirm.

echo "TF binary will be installed as     $TF"
if [ -n "$SYMLINK" ]; then
    echo "    with a symbolic link:          $SYMLINK"
fi
echo "Library, help, and utilities:      ${LIBDIR}/"
if [ -n "$MAILDIR" ]; then
    echo "Default mail directory:            ${MAILDIR}/"
else
    echo 'Mail directory unknown.  TF will not check mail if MAIL is unset.'
fi
if [ -n "$MANPAGE" ]; then
    echo "Man page will be installed in      $MANPAGE in $MANTYPE format"
else
    echo 'Man page will not be installed.'
fi
echo

echo 'To change these locations type "n" now and edit the unix/Config file.'
while [ -z "$ans" ]; do
    echo 'Continue? (y/n)'
    read ans;
    case "$ans" in
    y|Y)  break ;;
    n|N)  exit 1 ;;
    *)    ans=
          echo 'Please answer "y" or "n".'
          ;;
    esac
done
echo; echo

rm -f ../src/Makefile ${AOUT}

echo "#define LIBDIR     \"${LIBDIR}\"" >&4
if [ -n "$MAILDIR" ]; then
    echo "#define MAILDIR    \"${MAILDIR}\"" >&4
else
    echo "#undef  MAILDIR" >&4
fi

echo "MAKE       = ${MAKE}" >&5
echo "TF         = ${TF}" >&5
echo "SYMLINK    = ${SYMLINK}" >&5
echo "LIBDIR     = ${LIBDIR}" >&5
echo "MAILDIR    = ${MAILDIR}" >&5
echo "MANPAGE    = ${MANPAGE}" >&5
echo "MANTYPE    = ${MANTYPE}" >&5
echo "MODE       = ${MODE}" >&5


### Compiler.
### We'll find extra needed libs later, so we don't need /usr/ucb/cc
### or /usr/ucb/cc-bsd like we used to.
### Use GNU C if it's available.
### Note: GNU C's -ansi option does not guarantee existance of standard
### headers or libraries, so we must still verify them seperately.
### Note: many Solaris systems have a "cc" that does nothing.

if [ -z "${CC}" ]; then
    if { gcc -v; } > /dev/null 2>&1; then
        CC='gcc'
    else
        CC='cc'
    fi
fi

echo "Will compile with: ${CC} ${CCFLAGS}"
echo "CC         = ${CC}" >&5

### Figure out how to run preprocessor.
# Most compilers have an -E flag to run the preprocessor only.
# If -E doesn't work, try some common paths.

if [ -z "$CPP" ]; then
    echo 'main() { exit(0);}' > test.c
    if ${CC} -E test.c >/dev/null; then
        CPP="${CC} ${CCFLAGS} -E"
    elif { cpp test.c; } >/dev/null 2>&1; then
        CPP='cpp'
    elif { /lib/cpp test.c; } >/dev/null 2>&1; then
        CPP='/lib/cpp'
    elif { /usr/lib/cpp test.c; } >/dev/null 2>&1; then
        CPP='/usr/lib/cpp'
    elif { /usr/ccs/bin/cpp test.c; } >/dev/null 2>&1; then
        CPP='/usr/ccs/bin/cpp'
    else
        echo "I can't figure out how to run the preprocessor."
        exit 1
    fi
fi
echo "Preprocessor: ${CPP}"

### Test for ANSI.

cat > test.c <<EOP
#ifdef __GNUC__
    extern gnuc;
#endif
#if __STDC__ - 0
    extern stdc;
#endif
EOP
${CPP} test.c >test.out 2>&1
if [ $? != 0 ] ; then
    echo
    cat test.out
    echo
    echo "Ack!  Your compiler doesn't work.  Maybe you used invalid flags."
    echo "I give up."
    exit 2
elif egrep 'gnuc' test.out >/dev/null 2>&1; then
    echo "Ah ha, ${CC} is GNU C `${CC} --version`"
    GNU_C=1
elif egrep 'stdc' test.out >/dev/null 2>&1; then
    echo "Cool, you appear have a standard (ANSI/ISO) compiler."
    STD_C=1
else
    echo "Your compiler doesn't appear to be standard (ANSI/ISO)."
fi


### Test for POSIX.
### If test.c fails to preprocess, unistd.h must be missing, and system is not
### POSIX.  Otherwise, test.out contains 'posix' if posix.
### Note: in general, it is usually better to test a specific feature than
### to rely on the system's claim of POSIX compliance.

cat > test.c <<EOP
#include <unistd.h>
#ifdef _POSIX_VERSION
    extern posix;
#else
#endif
EOP
if ${CPP} test.c > test.out 2>/dev/null; then
    echo '#define UNISTD_H <unistd.h>' >&4
    if egrep 'posix' test.out >/dev/null 2>&1; then
        echo "Right on, your system is POSIX."
        POSIX=1
    else
        echo "This doesn't appear to be a POSIX system."
        POSIX=0
    fi
else
    echo '#undef  UNISTD_H' >&4
    echo "I can't find <unistd.h>.  The compiler may produce a lot of"
    echo "    warnings, but you can probably ignore them."
    echo "This doesn't appear to be a POSIX system."
    POSIX=0
fi


### Make sure the PATH is sane.
# Ideally, this should be done before the compiler check.  But a compiler
# found now in the extended PATH won't be found in src/Makefile, because it
# won't inherit this PATH.
PATH="$PATH:/bin:/usr/bin:/usr/local/bin:/usr/ucb:/usr/local:/usr/lbin:/etc:/usr/new:/usr/new/bin:/usr/nbin:/usr/ccs/bin"


### This isn't required, but it reduces warnings.
if echo '#include <stdlib.h>' >test.c; ${CPP} test.c >/dev/null 2>&1; then
    echo '#define STDLIB_H <stdlib.h>' >&4
    echo "Found <stdlib.h>."
else
    echo '#undef  STDLIB_H' >&4
    echo "I can't find <stdlib.h>.  The compiler may produce a lot of"
    echo "    warnings, but you can probably ignore them."
fi

### Figure out which string header to include.
if echo '#include <string.h>' >test.c; ${CPP} test.c >/dev/null 2>&1; then
    echo '#define STRING_H <string.h>' >&4
    echo "Found <string.h>."
elif echo '#include <strings.h>' >test.c; ${CPP} test.c >/dev/null 2>&1; then
    echo '#define STRING_H <strings.h>' >&4
    echo "Found <strings.h>."
else
    echo "I can't find <string.h> or <strings.h>.  I give up."
    exit 1
fi

### SunOS 4.x doesn't define mem... in string.h.
if echo '#include <memory.h>' >test.c; ${CPP} test.c >/dev/null 2>&1; then
    echo '#define MEMORY_H <memory.h>' >&4
    echo "Found <memory.h>."
else
    echo '#undef  MEMORY_H' >&4
fi

### This isn't required, but it reduces warnings.
if echo '#include <sys/select.h>' >test.c; ${CPP} test.c >/dev/null 2>&1; then
    echo '#define SYS_SELECT_H <sys/select.h>' >&4
    echo "Found <sys/select.h>."
else
    echo '#undef  SYS_SELECT_H' >&4
    echo "I can't find <sys/select.h>, but that's ok."
fi

### For optional language support.
if echo '#include <locale.h>' >test.c; ${CPP} test.c >/dev/null 2>&1; then
    echo '#define LOCALE_H <locale.h>' >&4
    echo "Found <locale.h>."
else
    echo '#undef  LOCALE_H' >&4
    echo "I can't find <locale.h>; alternate locales will not be supported."
fi


### Find internet headers

if cat > test.c <<EOP
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
EOP
${CPP} test.c >/dev/null 2>&1; then
    echo '#define NETINET_IN_H <netinet/in.h>' >&4
    echo "Found <netinet/in.h>."

elif cat > test.c <<EOP
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/in.h>
EOP
${CPP} test.c >/dev/null 2>&1; then
    echo '#define NETINET_IN_H <sys/in.h>' >&4
    echo "Found <sys/in.h>."

elif cat > test.c <<EOP
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/netinet/in.h>
EOP
${CPP} test.c >/dev/null 2>&1; then
    echo '#define NETINET_IN_H <sys/netinet/in.h>' >&4
    echo "Found <sys/netinet/in.h>."

else
    echo "I can't find <netinet/in.h>, <sys/in.h>, or <sys/netinet/in.h>."
    echo "  I'll use my own definitions.  Good luck."
    echo '#undef  NETINET_IN_H' >&4
fi

### Find internet headers
if echo '#include <arpa/inet.h>' >test.c; ${CPP} test.c >/dev/null 2>&1; then
    echo '#define ARPA_INET_H <arpa/inet.h>' >&4
    echo "Found <arpa/inet.h>."
elif echo '#include <sys/inet.h>' >test.c; ${CPP} test.c >/dev/null 2>&1; then
    echo '#define ARPA_INET_H <sys/inet.h>' >&4
    echo "Found <sys/inet.h>."
else
    echo '#undef  ARPA_INET_H' >&4
    echo "I can't find <arpa/inet.h> or <sys/inet.h>, but we should be ok."
fi

### Find netdb header
if echo '#include <netdb.h>' >test.c; ${CPP} test.c >/dev/null 2>&1; then
    echo '#define NETDB_H <netdb.h>' >&4
    echo "Found <netdb.h>."
elif echo '#include <sys/netdb.h>' >test.c; ${CPP} test.c >/dev/null 2>&1; then
    echo '#define NETDB_H <sys/netdb.h>' >&4
    echo "Found <sys/netdb.h>."
else
    echo '#define NO_NETDB' >&4
    echo "I can't find <netdb.h> or <sys/netdb.h>."
    echo "    Hostname resolution and port service names will not be available."
fi

### Find pwd.h
if echo '#include <pwd.h>' >test.c; ${CPP} test.c >/dev/null 2>&1; then
    echo '#define HAVE_PWD_H' >&4
    echo "Found <pwd.h>."
else
    echo '#undef  HAVE_PWD_H' >&4
    echo "I can't find <pwd.h>.  Filename '~user' expansion won't be supported."
fi

### Find zlib.h
if echo '#include <zlib.h>' >test.c; ${CPP} test.c >/dev/null 2>&1; then
    echo "#define MUDCOMPRESS" >&4
    echo "Found <zlib.h>."
else
    echo "I can't find <zlib.h>.  The Mud Client Compression Protocol will not be supported."
fi

### Figure out how to do varadic args.
if [ "$STD_C" = "1" ] || [ "$GNU_C" = "1" ]; then
    if echo '#include <stdarg.h>' >test.c; ${CPP} test.c >/dev/null 2>&1; then
        echo '#define HAVE_STDARG' >&4
        echo "Found <stdarg.h>."
    elif [ "$STD_C" = "1" ]; then
#       Workaround for AOS (IBM RT), possibly Domain/OS (Apollo).
#       Not tested very well; but, this case is rare.
        echo "Your compiler says it's ANSI, but <stdarg.h> fails.  Tsk tsk."
        echo "    I'm going to undefine __STDC__.  Cross your fingers."
        echo '#undef  __STDC__' >&4
        echo '#undef  HAVE_STDARG' >&4
    fi
else
    echo '#undef  HAVE_STDARG' >&4
fi


### Test for function availability.
### We try to compile a program that calls the functions we're testing.
### If a function name appears in the stderr of the linking phase, we
### conclude it is unresolved or otherwise unavailable; if not, we conclude
### it's safe to use the function in tf.
### Testing each function in a seperate program might be slightly more
### reliable, but also much slower.

echo 'Testing for library symbols...'
cat > symtest.c <<EOF

#include <pwd.h>
#include <sys/time.h>
#include <unistd.h>
#include <signal.h>
#include <limits.h>
#include <locale.h>
#include <strings.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <time.h> 

#include "../src/config.h"    /* for header file macros */
#include <stdio.h>
#ifdef UNISTD_H
# include UNISTD_H
#endif
#ifdef STDLIB_H
# include STDLIB_H
#endif
#include <sys/types.h>
#include <sys/socket.h>  /* for connect() */
#ifdef NETDB_H
# include NETDB_H  /* for gethostbyname(), h_errno */
#endif
#include STRING_H
#include <math.h>
#ifndef h_errno
extern int h_errno;
#endif

void nonexistant_function();

int main() {
    int i;
    char *s;

    bcopy(NULL, NULL, 0);
    bsearch((void*)NULL, (void*)NULL, 0, 0, NULL);
    bzero(NULL, 0);
    connect(0, (void*)NULL, 0);
    fileno(stdout);
    getcwd(NULL, 0);
    gethostbyname(NULL);
    getpwnam(NULL);
    gettimeofday(NULL, NULL);
    getwd(NULL);
    i = h_errno;
    hstrerror(i);
    index(NULL, 0);
    kill(0, 0);
    memcpy(NULL, NULL, 0);
    memset(NULL, 0, 0);
    raise(0);
    setlocale(0, "C");
    sigaction(0, NULL, NULL);
    srand(0);
    srandom(0);
    strcasecmp(NULL, NULL);
    strchr(NULL, 0);
    strcasecmp(NULL, NULL);
    strcspn(NULL, NULL);
    strerror(0);
    strftime(NULL, 0, NULL, NULL);
    strcasecmp(NULL, NULL);
    strstr(NULL, NULL);
    strtod(NULL, NULL);
    strtol(NULL, NULL, 0);
    tzset();
    waitpid(0, NULL, 0);

    nonexistant_function();
    return 0;
}
EOF

# We don't care about compiler warnings, as long as an object file is created.
${CC} ${CCFLAGS} -c symtest.c >symtest.out 2>&1 || {
    echo
    cat symtest.out
    echo
    echo "## The messages above indicate one of these configuration problems:"
    echo "##   1. there is no compiler;"
    echo "##   2. the compiler is not installed correctly;"
    echo "##   3. you do not have permission to execute the compiler; or"
    echo "##   4. CCFLAGS='${CCFLAGS}' in unix/Config is invalid."
    echo "## In any case, this is a local problem that can only be"
    echo "## solved by you or a system administrator, so don't ask me."
    echo
    exit 1
}

${CC} ${CCFLAGS} symtest.o -o ${AOUT}>symtest.out 2>&1

# If "nonexistant_function" didn't appear in stderr, something else is wrong.
if egrep nonexistant_function symtest.out >/dev/null; then
    : expected error
else
    echo
    cat symtest.out
    echo
    echo "$0:  don't know how to test for symbols."
    exit 1;
fi

for sym in \
    bcopy bsearch bzero connect fileno getcwd gethostbyname getpwnam \
    gettimeofday getwd h_errno hstrerror index kill memcpy memset raise \
    setlocale sigaction srand srandom strcasecmp strchr strcasecmp strcspn \
    strerror strftime strcasecmp strstr strtod strtol tzset waitpid
do
    if egrep "(^|[^a-z_])_?_?${sym}([^a-z]|\$)" symtest.out >/dev/null; then
        echo "Did not find ${sym}, but that's ok."
        echo "#undef  HAVE_${sym}" >&4
    else
        echo "Found ${sym}."
        echo "#define HAVE_${sym}" >&4
    fi
done


if [ "$CYGWIN32" = "1" ]; then
    echo "#undef  HAVE_bsearch  /* bsearch() is broken in cygwin32 b18. */" >&4
    echo "#undef  HAVE_h_errno  /* h_errno is broken in cygwin32 b18. */" >&4
fi


### Test for correct toupper() and tolower()
echo "Testing case conversion..."
cat >test.c <<EOF
#include <ctype.h>
int main() {
    return !(toupper('a') == 'A' && toupper('A') == 'A' && toupper('@') == '@');
}
EOF
if ${CC} ${CCFLAGS} test.c -o ${AOUT}>/dev/null 2>&1 && ./${AOUT}; then
    echo "Case conversion works."
    echo '#define CASE_OK' >&4
else
    echo "Case conversion is broken, I'll have to use my own."
    echo '#undef  CASE_OK' >&4
fi


### Figure out return type of signal handlers.
echo "Testing type of signal handlers..."
cat >test.c <<EOF
#include <sys/types.h>
#include <signal.h>
#undef signal
extern void (*signal())();
main() { exit(0); }
EOF
if ${CC} ${CCFLAGS} -c test.c -o ${AOUT}>/dev/null 2>&1; then
    echo "Signal handlers return void, as they should."
    echo '#define RETSIG void' >&4
else
    echo "Assuming signal handlers return int."
    echo '#define RETSIG int' >&4
fi


### Test for time_t.
echo "Testing for time_t..."
cat >test.c <<EOF
#include <time.h>
extern time_t time();
main() { exit(0); }
EOF
if ${CC} ${CCFLAGS} -c test.c -o ${AOUT}>/dev/null 2>&1; then
    echo "time() returns time_t, as it should."
    echo '#define TIME_T time_t' >&4
else
    echo "Assuming time() returns long."
    echo '#define TIME_T long' >&4
fi


rm -f symtest.out ${AOUT} symtest.o symtest.c

### Library testing.
# If libfoo.a make references to libbar.a, the order must be "-lfoo -lbar";
# it's okay to test -lbar alone, but to test -lfoo we must use "-lfoo -lbar".
# So we test incrementally, building the list from right to left.
# For Dynix/ptx, order must be: -lsocket -linet -lnsl
# For SysV, -lsocket test needs -lnsl.

### Create a trivial object with which to test library linking.
### Much faster than starting from a .c each time.

echo "Creating linker test..."
echo 'main() { exit(0); }' > libtest.c
${CC} ${CCFLAGS} -c libtest.c >/dev/null 2>&1
rm libtest.c
LIBTEST="rm -f ${AOUT}; ${CC} ${CCFLAGS} libtest.o -o ${AOUT}"

### Test LIBS, to make sure user didn't screw it up.

if [ -n "$LIBS" ]; then
    if eval "${LIBTEST} ${LIBS} >/dev/null" && test -f ${AOUT}; then
        : okay
    else
        echo
        echo "Above problem may be due to unix/Config: LIBS=\"$LIBS\""
        exit 1
    fi
fi

### If -lbsd exists, we might need it (and it shouldn't make a difference if
### we don't need it, especially since it's the last library we link).

if eval "${LIBTEST} -lbsd ${LIBS} >/dev/null 2>&1" && test -f ${AOUT}; then
    echo "Will link with -lbsd compatibility library."
    LIBS="-lbsd $LIBS"
fi

### If gethostbyname() isn't in libc, look for -lnsl_s or -lnsl.
# note: can't assume existance of libs implies need; must check libc first. (?)

if egrep '^#define HAVE_gethostbyname$' ../src/config.h >/dev/null 2>&1; then
    : Already found it.
elif eval "${LIBTEST} -lnsl_s ${LIBS} >/dev/null 2>&1" && test -f ${AOUT}; then
    echo "Will link with -lnsl_s for gethostbyname()."
    LIBS="-lnsl_s $LIBS"
elif eval "${LIBTEST} -lnsl ${LIBS} >/dev/null 2>&1" && test -f ${AOUT}; then
    echo "Will link with -lnsl for gethostbyname()."
    LIBS="-lnsl $LIBS"
else
    echo "Warning: can't find gethostbyname() or a library that might have it."
    echo "  Host name resolution will not be available."
    echo "/* warning: tfconfig couldn't find gethostbyname() */" >&4
fi

### If -linet exists, assume we need it.
# note: -linet may require -lnsl (Dynix/ptx), so ordering is important.

if eval "${LIBTEST} -linet ${LIBS} >/dev/null 2>&1" && test -f ${AOUT}; then
    echo "Will link with -linet."
    LIBS="-linet $LIBS"
fi

### If -lnet exists, assume we need it.

if eval "${LIBTEST} -lnet ${LIBS} >/dev/null 2>&1" && test -f ${AOUT}; then
    echo "Will link with -lnet."
    LIBS="-lnet $LIBS"
fi

## If we haven't found connect(), look for -lsocket.
## Note: on IRIX 5, -lsocket exists, but we must not use its gethostbyname().
if egrep '^#define HAVE_connect$' ../src/config.h >/dev/null 2>&1; then
    : Already found it.
elif eval "${LIBTEST} -lsocket ${LIBS} >/dev/null 2>&1" && test -f ${AOUT}; then
    echo "Will link with -lsocket for connect(), etc."
    echo "#define HAVE_connect" >&4
    LIBS="-lsocket $LIBS"
else
    echo "Warning: can't find connect() or a library that might have it."
    echo "/* warning: tfconfig couldn't find connect() */" >&4
fi

### Test for zlib for mud compression
if eval "${LIBTEST} -lz ${LIBS} >/dev/null 2>&1" && test -f ${AOUT}; then
    echo "Will link with -lz."
    LIBS="-lz $LIBS"
fi

### Test for SOCKS firewall proxy server.
if [ -n "$SOCKS" ]; then
    # SOCKS uses res_init(), so we need -lresolv if there is one.
    if eval "${LIBTEST} -lresolv ${LIBS} >/dev/null 2>&1" && test -f ${AOUT}; then
        echo "Will link with -lresolv."
        LIBS="-lresolv $LIBS"
    fi
    if eval "${LIBTEST} ${SOCKSLIBDIR} ${SOCKSLIB} ${LIBS} >/dev/null 2>&1" && test -f ${AOUT};
    then
        echo "Will link with ${SOCKSLIB}."
        LIBS="${SOCKSLIBDIR} ${SOCKSLIB} $LIBS"
        CCFLAGS="${CCFLAGS} ${SOCKSINCDIR}"
        echo "#define SOCKS ${SOCKS}" >&4
        if [ -n "$SOCKS_NONBLOCK" ]; then
            echo "#define SOCKS_NONBLOCK" >&4
        fi
    else
        echo "Can't find SOCKS library.  You may need to edit the SOCKSLIBDIR"
        echo "  line of unix/Config."
        exit 1
    fi
fi

### SCO keeps strftime() in -lintl, but others have -lintl without strftime().

if egrep '^#define HAVE_strftime$' ../src/config.h >/dev/null 2>&1; then
    : Already found it.
else
    echo "Testing for strftime() in -lintl."
    cat >test.c <<EOF
#include <time.h>
#include "../src/config.h"
main() { char s[9]; TIME_T t; strftime(s, sizeof(s), "%H", localtime(&t)); }
EOF
    if ${CC} ${CCFLAGS} test.c -lintl ${LIBS} -o ${AOUT}>/dev/null 2>&1; then
        echo "Will link with -lintl for strftime()."
        LIBS="-lintl $LIBS"
        echo "#define HAVE_strftime" >&4
    else
        echo "strftime() not found.  Time formatting will not be supported."
    fi
    rm -f test.c test.o ${AOUT}
fi


### I'm guessing setlocale() is in -lintl on some systems.

if egrep '^#define HAVE_setlocale$' ../src/config.h >/dev/null 2>&1; then
    : Already found it.
else
    echo "Testing for setlocale() in -lintl."
    cat >test.c <<EOF
#include "../src/config.h"
#ifdef LOCALE_H
# include LOCALE_H
#endif
main() { setlocale(LC_ALL, "C"); }
EOF
    if ${CC} ${CCFLAGS} test.c -lintl ${LIBS} -o ${AOUT}>/dev/null 2>&1; then
        echo "Will link with -lintl for setlocale()."
        LIBS="-lintl $LIBS"
        echo "#define HAVE_setlocale" >&4
    else
        echo "setlocale() not found.  Locales will not be supported."
    fi
    rm -f test.c test.o ${AOUT}
fi


### test termcap.
# At least one system (Red Hat Linux) has a broken ncurses and a working
# termcap, so we try termcap first.

ERR=">/dev/null 2>&1"

case "$TERMINAL" in
HARDCODE*) # hardcode for a specific terminal
  ;;
*)         # use a termcap library
  if test -n "$TERMINAL" &&
  eval "$LIBTEST -l$TERMINAL $LIBS $ERR" && test -f ${AOUT}; then
    : okay
  elif eval "$LIBTEST -ltermcap $LIBS $ERR" && test -f ${AOUT}; then
    TERMINAL=termcap
  elif eval "$LIBTEST -lncurses $LIBS $ERR" && test -f ${AOUT}; then
    TERMINAL=ncurses
  elif eval "$LIBTEST -lcurses $LIBS $ERR" && test -f ${AOUT}; then
    TERMINAL=curses
  else
    echo ''
    echo "WARNING:  Can't link with ${TERMINAL:-term library}."
    TERMINAL="HARDCODE TERM_vt100"
    echo ''
  fi
  if [ "$TERMINAL" != "termcap" ] &&
    test -n "/usr/doc/debian*" && test -d /usr/i486-linuxaout/bin; then
    echo ''
    echo 'WARNING: this looks like Debian Linux without an ELF term library.'
    echo 'Get one, or try prepending "/usr/i486-linuxaout/bin" to your PATH'
    echo 'before calling unixmake.'
    echo ''
  fi
  ;;
esac

case "$TERMINAL" in
HARDCODE*)
    echo "Will use hardcoded terminal codes: $TERMINAL"
    echo "#define $TERMINAL" >&4
    ;;
*)
    echo "Will link with -l${TERMINAL} terminal control library."
    LIBS="-l${TERMINAL} $LIBS"
    echo "#define TERMCAP" >&4
    ;;
esac


if echo "$FLAGS" | egrep 'NO_FLOAT' >/dev/null 2>&1; then
    echo "Floating point math disabled."
else
    if eval "$LIBTEST -lm $LIBS $ERR" && test -f ${AOUT}; then
        echo "Will link with -lm math library."
        LIBS="-lm $LIBS"
    fi
fi


### Figure out which terminal driver to use.

if [ -z "$TTYDRIVER" ]; then
    if echo "#include <termios.h>" >test.c; ${CPP} test.c >/dev/null 2>&1; then
        TTYDRIVER="USE_TERMIOS"
        echo "Using <termios.h>."
    elif [ "$POSIX" = 1 ]; then
        echo "Your system says it's POSIX, but I can't find <termios.h>.  Tsk."
    fi
fi
if [ -z "$TTYDRIVER" ]; then
    if echo "#include <termio.h>" >test.c; ${CPP} test.c >/dev/null 2>&1; then
        HAVE_TERMIO=1
    fi
    if echo "#include <sgtty.h>" >test.c; ${CPP} test.c >/dev/null 2>&1; then
        HAVE_SGTTY=1
    fi

    if [ "$HAVE_TERMIO" = 1 ] && [ "$HAVE_SGTTY" = 1 ]; then
        echo "Your system has both <termio.h> and <sgtty.h>..."
#       They should both work, but let's try to use the "native" driver.
#       Make a semi-educated guess, and warn the user.
#       Many USG systems have uname, many BSD systems don't.
        if { uname; } >/dev/null 2>&1; then
            TTYDRIVER="USE_TERMIO"
            echo "    I'm going to use <termio.h>."
            echo "    To change it, set TTYDRIVER='USE_SGTTY' in unix/Config."
        else
            TTYDRIVER="USE_SGTTY"
            echo "    I'm going to use <sgtty.h>."
            echo "    To change it, set TTYDRIVER='USE_TERMIO' in unix/Config."
        fi
    elif [ "$HAVE_TERMIO" = 1 ]; then
        echo "Your system has <termio.h>."
        TTYDRIVER="USE_TERMIO"
    elif [ "$HAVE_SGTTY" = 1 ]; then
        echo "Your system has <sgtty.h>."
        TTYDRIVER="USE_SGTTY"
    else
        echo "I can't find any terminal driver headers on your system!"
        echo "I give up."
        exit 3
    fi
fi
echo "#define $TTYDRIVER" >&4

### Some brain damaged systems (Xenix) need <sys/ptem.h> for struct winsize.
if { egrep 'winsize' /usr/include/sys/ptem.h; } >/dev/null 2>&1; then
    echo "#define NEED_PTEM_H" >&4
fi

FLAGS="${CCFLAGS} ${FLAGS}"

OTHER_OBJS=""

if [ "$DMALLOC" = "1" ]; then
    FLAGS="$FLAGS -DDMALLOC=$DMALLOC"
    OTHER_OBJS="$OTHER_OBJS dmalloc.o"
fi

### write variables

echo "STRIP      = $STRIP" >&5
echo "FLAGS      = $FLAGS" >&5
echo "LIBS       = $LIBS" >&5
echo "OTHER_OBJS = $OTHER_OBJS" >&5
echo >&5

### Clean up.

rm -f test.c test.out test.o libtest.o ${AOUT}

### Create ../src/Makefile.

cat > ../src/Makefile <<EOF
#### DO NOT EDIT THIS FILE.
#### This src/Makefile was automatically generated by unix/tfconfig.  The
#### correct installation precedure is to run ./unixmake in the top directory.
#### You should not edit this file; all configuration changes should
#### be made to the unix/Config file.

EOF

cat vars.mak ../src/vars.mak unix.mak ../src/rules.mak >> ../src/Makefile