OSDN Git Service

Upgrade to mksh 51.
[android-x86/external-mksh.git] / src / Build.sh
1 #!/bin/sh
2 srcversion='$MirOS: src/bin/mksh/Build.sh,v 1.689 2015/07/10 17:16:23 tg Exp $'
3 #-
4 # Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
5 #               2011, 2012, 2013, 2014, 2015
6 #       Thorsten “mirabilos” Glaser <tg@mirbsd.org>
7 #
8 # Provided that these terms and disclaimer and all copyright notices
9 # are retained or reproduced in an accompanying document, permission
10 # is granted to deal in this work without restriction, including un-
11 # limited rights to use, publicly perform, distribute, sell, modify,
12 # merge, give away, or sublicence.
13 #
14 # This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
15 # the utmost extent permitted by applicable law, neither express nor
16 # implied; without malicious intent or gross negligence. In no event
17 # may a licensor, author or contributor be held liable for indirect,
18 # direct, other damage, loss, or other issues arising in any way out
19 # of dealing in the work, even if advised of the possibility of such
20 # damage or existence of a defect, except proven that it results out
21 # of said person's immediate fault when using the work as intended.
22 #-
23 # People analysing the output must whitelist conftest.c for any kind
24 # of compiler warning checks (mirtoconf is by design not quiet).
25 #
26 # Used environment documentation is at the end of this file.
27
28 LC_ALL=C
29 export LC_ALL
30
31 case $ZSH_VERSION:$VERSION in
32 :zsh*) ZSH_VERSION=2 ;;
33 esac
34
35 if test -n "${ZSH_VERSION+x}" && (emulate sh) >/dev/null 2>&1; then
36         emulate sh
37         NULLCMD=:
38 fi
39
40 if test -d /usr/xpg4/bin/. >/dev/null 2>&1; then
41         # Solaris: some of the tools have weird behaviour, use portable ones
42         PATH=/usr/xpg4/bin:$PATH
43         export PATH
44 fi
45
46 nl='
47 '
48 safeIFS='       '
49 safeIFS=" $safeIFS$nl"
50 IFS=$safeIFS
51 allu=QWERTYUIOPASDFGHJKLZXCVBNM
52 alll=qwertyuiopasdfghjklzxcvbnm
53 alln=0123456789
54 alls=______________________________________________________________
55
56 genopt_die() {
57         if test -n "$1"; then
58                 echo >&2 "E: $*"
59                 echo >&2 "E: in '$srcfile': '$line'"
60         else
61                 echo >&2 "E: invalid input in '$srcfile': '$line'"
62         fi
63         rm -f "$bn.gen"
64         exit 1
65 }
66
67 genopt_soptc() {
68         optc=`echo "$line" | sed 's/^[<>]\(.\).*$/\1/'`
69         test x"$optc" = x'|' && return
70         optclo=`echo "$optc" | tr $allu $alll`
71         if test x"$optc" = x"$optclo"; then
72                 islo=1
73         else
74                 islo=0
75         fi
76         sym=`echo "$line" | sed 's/^[<>]/|/'`
77         o_str=$o_str$nl"<$optclo$islo$sym"
78 }
79
80 genopt_scond() {
81         case x$cond in
82         x)
83                 cond=
84                 ;;
85         x*' '*)
86                 cond=`echo "$cond" | sed 's/^ //'`
87                 cond="#if $cond"
88                 ;;
89         x'!'*)
90                 cond=`echo "$cond" | sed 's/^!//'`
91                 cond="#ifndef $cond"
92                 ;;
93         x*)
94                 cond="#ifdef $cond"
95                 ;;
96         esac
97 }
98
99 do_genopt() {
100         srcfile=$1
101         test -f "$srcfile" || genopt_die Source file \$srcfile not set.
102         bn=`basename "$srcfile" | sed 's/.opt$//'`
103         o_gen=
104         o_str=
105         o_sym=
106         ddefs=
107         state=0
108         exec <"$srcfile"
109         IFS=
110         while IFS= read line; do
111                 IFS=$safeIFS
112                 case $state:$line in
113                 2:'|'*)
114                         # end of input
115                         o_sym=`echo "$line" | sed 's/^.//'`
116                         o_gen=$o_gen$nl"#undef F0"
117                         o_gen=$o_gen$nl"#undef FN"
118                         o_gen=$o_gen$ddefs
119                         state=3
120                         ;;
121                 1:@@)
122                         # begin of data block
123                         o_gen=$o_gen$nl"#endif"
124                         o_gen=$o_gen$nl"#ifndef F0"
125                         o_gen=$o_gen$nl"#define F0 FN"
126                         o_gen=$o_gen$nl"#endif"
127                         state=2
128                         ;;
129                 *:@@*)
130                         genopt_die ;;
131                 0:@*|1:@*)
132                         # begin of a definition block
133                         sym=`echo "$line" | sed 's/^@//'`
134                         if test $state = 0; then
135                                 o_gen=$o_gen$nl"#if defined($sym)"
136                         else
137                                 o_gen=$o_gen$nl"#elif defined($sym)"
138                         fi
139                         ddefs="$ddefs$nl#undef $sym"
140                         state=1
141                         ;;
142                 0:*|3:*)
143                         genopt_die ;;
144                 1:*)
145                         # definition line
146                         o_gen=$o_gen$nl$line
147                         ;;
148                 2:'<'*'|'*)
149                         genopt_soptc
150                         ;;
151                 2:'>'*'|'*)
152                         genopt_soptc
153                         cond=`echo "$line" | sed 's/^[^|]*|//'`
154                         genopt_scond
155                         case $optc in
156                         '|') optc=0 ;;
157                         *) optc=\'$optc\' ;;
158                         esac
159                         IFS= read line || genopt_die Unexpected EOF
160                         IFS=$safeIFS
161                         test -n "$cond" && o_gen=$o_gen$nl"$cond"
162                         o_gen=$o_gen$nl"$line, $optc)"
163                         test -n "$cond" && o_gen=$o_gen$nl"#endif"
164                         ;;
165                 esac
166         done
167         case $state:$o_sym in
168         3:) genopt_die Expected optc sym at EOF ;;
169         3:*) ;;
170         *) genopt_die Missing EOF marker ;;
171         esac
172         echo "$o_str" | sort | while IFS='|' read x opts cond; do
173                 IFS=$safeIFS
174                 test -n "$x" || continue
175                 genopt_scond
176                 test -n "$cond" && echo "$cond"
177                 echo "\"$opts\""
178                 test -n "$cond" && echo "#endif"
179         done | {
180                 echo "#ifndef $o_sym$o_gen"
181                 echo "#else"
182                 cat
183                 echo "#undef $o_sym"
184                 echo "#endif"
185         } >"$bn.gen"
186         IFS=$safeIFS
187         return 0
188 }
189
190 if test x"$BUILDSH_RUN_GENOPT" = x"1"; then
191         set x -G "$srcfile"
192         shift
193 fi
194 if test x"$1" = x"-G"; then
195         do_genopt "$2"
196         exit $?
197 fi
198
199 echo "For the build logs, demonstrate that /dev/null and /dev/tty exist:"
200 ls -l /dev/null /dev/tty
201
202 v() {
203         $e "$*"
204         eval "$@"
205 }
206
207 vv() {
208         _c=$1
209         shift
210         $e "\$ $*" 2>&1
211         eval "$@" >vv.out 2>&1
212         sed "s\a^\a${_c} \a" <vv.out
213 }
214
215 vq() {
216         eval "$@"
217 }
218
219 rmf() {
220         for _f in "$@"; do
221                 case $_f in
222                 Build.sh|check.pl|check.t|dot.mkshrc|*.1|*.c|*.h|*.ico|*.opt) ;;
223                 *) rm -f "$_f" ;;
224                 esac
225         done
226 }
227
228 tcfn=no
229 bi=
230 ui=
231 ao=
232 fx=
233 me=`basename "$0"`
234 orig_CFLAGS=$CFLAGS
235 phase=x
236 oldish_ed=stdout-ed,no-stderr-ed
237
238 if test -t 1; then
239         bi='\e[1m'
240         ui='\e[4m'
241         ao='\e[0m'
242 fi
243
244 upper() {
245         echo :"$@" | sed 's/^://' | tr $alll $allu
246 }
247
248 # clean up after ac_testrun()
249 ac_testdone() {
250         eval HAVE_$fu=$fv
251         fr=no
252         test 0 = $fv || fr=yes
253         $e "$bi==> $fd...$ao $ui$fr$ao$fx"
254         fx=
255 }
256
257 # ac_cache label: sets f, fu, fv?=0
258 ac_cache() {
259         f=$1
260         fu=`upper $f`
261         eval fv=\$HAVE_$fu
262         case $fv in
263         0|1)
264                 fx=' (cached)'
265                 return 0
266                 ;;
267         esac
268         fv=0
269         return 1
270 }
271
272 # ac_testinit label [!] checkif[!]0 [setlabelifcheckis[!]0] useroutput
273 # returns 1 if value was cached/implied, 0 otherwise: call ac_testdone
274 ac_testinit() {
275         if ac_cache $1; then
276                 test x"$2" = x"!" && shift
277                 test x"$2" = x"" || shift
278                 fd=${3-$f}
279                 ac_testdone
280                 return 1
281         fi
282         fc=0
283         if test x"$2" = x""; then
284                 ft=1
285         else
286                 if test x"$2" = x"!"; then
287                         fc=1
288                         shift
289                 fi
290                 eval ft=\$HAVE_`upper $2`
291                 shift
292         fi
293         fd=${3-$f}
294         if test $fc = "$ft"; then
295                 fv=$2
296                 fx=' (implied)'
297                 ac_testdone
298                 return 1
299         fi
300         $e ... $fd
301         return 0
302 }
303
304 # pipe .c | ac_test[n] [!] label [!] checkif[!]0 [setlabelifcheckis[!]0] useroutput
305 ac_testnnd() {
306         if test x"$1" = x"!"; then
307                 fr=1
308                 shift
309         else
310                 fr=0
311         fi
312         ac_testinit "$@" || return 1
313         cat >conftest.c
314         vv ']' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN conftest.c $LIBS $ccpr"
315         test $tcfn = no && test -f a.out && tcfn=a.out
316         test $tcfn = no && test -f a.exe && tcfn=a.exe
317         test $tcfn = no && test -f conftest.exe && tcfn=conftest.exe
318         test $tcfn = no && test -f conftest && tcfn=conftest
319         if test -f $tcfn; then
320                 test 1 = $fr || fv=1
321         else
322                 test 0 = $fr || fv=1
323         fi
324         vscan=
325         if test $phase = u; then
326                 test $ct = gcc && vscan='unrecogni[sz]ed'
327                 test $ct = hpcc && vscan='unsupported'
328                 test $ct = pcc && vscan='unsupported'
329                 test $ct = sunpro && vscan='-e ignored -e turned.off'
330         fi
331         test -n "$vscan" && grep $vscan vv.out >/dev/null 2>&1 && fv=$fr
332         return 0
333 }
334 ac_testn() {
335         ac_testnnd "$@" || return
336         rmf conftest.c conftest.o ${tcfn}* vv.out
337         ac_testdone
338 }
339
340 # ac_ifcpp cppexpr [!] label [!] checkif[!]0 [setlabelifcheckis[!]0] useroutput
341 ac_ifcpp() {
342         expr=$1; shift
343         ac_testn "$@" <<-EOF
344                 #include <unistd.h>
345                 extern int thiswillneverbedefinedIhope(void);
346                 int main(void) { return (isatty(0) +
347                 #$expr
348                     0
349                 #else
350                 /* force a failure: expr is false */
351                     thiswillneverbedefinedIhope()
352                 #endif
353                     ); }
354 EOF
355         test x"$1" = x"!" && shift
356         f=$1
357         fu=`upper $f`
358         eval fv=\$HAVE_$fu
359         test x"$fv" = x"1"
360 }
361
362 add_cppflags() {
363         CPPFLAGS="$CPPFLAGS $*"
364 }
365
366 ac_cppflags() {
367         test x"$1" = x"" || fu=$1
368         fv=$2
369         test x"$2" = x"" && eval fv=\$HAVE_$fu
370         add_cppflags -DHAVE_$fu=$fv
371 }
372
373 ac_test() {
374         ac_testn "$@"
375         ac_cppflags
376 }
377
378 # ac_flags [-] add varname cflags [text] [ldflags]
379 ac_flags() {
380         if test x"$1" = x"-"; then
381                 shift
382                 hf=1
383         else
384                 hf=0
385         fi
386         fa=$1
387         vn=$2
388         f=$3
389         ft=$4
390         fl=$5
391         test x"$ft" = x"" && ft="if $f can be used"
392         save_CFLAGS=$CFLAGS
393         CFLAGS="$CFLAGS $f"
394         if test -n "$fl"; then
395                 save_LDFLAGS=$LDFLAGS
396                 LDFLAGS="$LDFLAGS $fl"
397         fi
398         if test 1 = $hf; then
399                 ac_testn can_$vn '' "$ft"
400         else
401                 ac_testn can_$vn '' "$ft" <<-'EOF'
402                         /* evil apo'stroph in comment test */
403                         #include <unistd.h>
404                         int main(void) { return (isatty(0)); }
405                 EOF
406         fi
407         eval fv=\$HAVE_CAN_`upper $vn`
408         if test -n "$fl"; then
409                 test 11 = $fa$fv || LDFLAGS=$save_LDFLAGS
410         fi
411         test 11 = $fa$fv || CFLAGS=$save_CFLAGS
412 }
413
414 # ac_header [!] header [prereq ...]
415 ac_header() {
416         if test x"$1" = x"!"; then
417                 na=1
418                 shift
419         else
420                 na=0
421         fi
422         hf=$1; shift
423         hv=`echo "$hf" | tr -d '\012\015' | tr -c $alll$allu$alln $alls`
424         echo "/* NeXTstep bug workaround */" >x
425         for i
426         do
427                 case $i in
428                 _time)
429                         echo '#if HAVE_BOTH_TIME_H' >>x
430                         echo '#include <sys/time.h>' >>x
431                         echo '#include <time.h>' >>x
432                         echo '#elif HAVE_SYS_TIME_H' >>x
433                         echo '#include <sys/time.h>' >>x
434                         echo '#elif HAVE_TIME_H' >>x
435                         echo '#include <time.h>' >>x
436                         echo '#endif' >>x
437                         ;;
438                 *)
439                         echo "#include <$i>" >>x
440                         ;;
441                 esac
442         done
443         echo "#include <$hf>" >>x
444         echo '#include <unistd.h>' >>x
445         echo 'int main(void) { return (isatty(0)); }' >>x
446         ac_testn "$hv" "" "<$hf>" <x
447         rmf x
448         test 1 = $na || ac_cppflags
449 }
450
451 addsrcs() {
452         if test x"$1" = x"!"; then
453                 fr=0
454                 shift
455         else
456                 fr=1
457         fi
458         eval i=\$$1
459         test $fr = "$i" && case " $SRCS " in
460         *\ $2\ *)       ;;
461         *)              SRCS="$SRCS $2" ;;
462         esac
463 }
464
465
466 curdir=`pwd` srcdir=`dirname "$0" 2>/dev/null`
467 case x$srcdir in
468 x)
469         srcdir=.
470         ;;
471 *\ *|*" "*|*"$nl"*)
472         echo >&2 Source directory should not contain space or tab or newline.
473         echo >&2 Errors may occur.
474         ;;
475 *"'"*)
476         echo Source directory must not contain single quotes.
477         exit 1
478         ;;
479 esac
480 dstversion=`sed -n '/define MKSH_VERSION/s/^.*"\([^"]*\)".*$/\1/p' "$srcdir/sh.h"`
481 add_cppflags -DMKSH_BUILDSH
482
483 e=echo
484 r=0
485 eq=0
486 pm=0
487 cm=normal
488 optflags=-std-compile-opts
489 check_categories=
490 last=
491 tfn=
492 legacy=0
493
494 for i
495 do
496         case $last:$i in
497         c:combine|c:dragonegg|c:llvm|c:lto)
498                 cm=$i
499                 last=
500                 ;;
501         c:*)
502                 echo "$me: Unknown option -c '$i'!" >&2
503                 exit 1
504                 ;;
505         o:*)
506                 optflags=$i
507                 last=
508                 ;;
509         t:*)
510                 tfn=$i
511                 last=
512                 ;;
513         :-c)
514                 last=c
515                 ;;
516         :-G)
517                 echo "$me: Do not call me with '-G'!" >&2
518                 exit 1
519                 ;;
520         :-g)
521                 # checker, debug, valgrind build
522                 add_cppflags -DDEBUG
523                 CFLAGS="$CFLAGS -g3 -fno-builtin"
524                 ;;
525         :-j)
526                 pm=1
527                 ;;
528         :-L)
529                 legacy=1
530                 ;;
531         :+L)
532                 legacy=0
533                 ;;
534         :-M)
535                 cm=makefile
536                 ;;
537         :-O)
538                 optflags=-std-compile-opts
539                 ;;
540         :-o)
541                 last=o
542                 ;;
543         :-Q)
544                 eq=1
545                 ;;
546         :-r)
547                 r=1
548                 ;;
549         :-t)
550                 last=t
551                 ;;
552         :-v)
553                 echo "Build.sh $srcversion"
554                 echo "for mksh $dstversion"
555                 exit 0
556                 ;;
557         :*)
558                 echo "$me: Unknown option '$i'!" >&2
559                 exit 1
560                 ;;
561         *)
562                 echo "$me: Unknown option -'$last' '$i'!" >&2
563                 exit 1
564                 ;;
565         esac
566 done
567 if test -n "$last"; then
568         echo "$me: Option -'$last' not followed by argument!" >&2
569         exit 1
570 fi
571
572 test -z "$tfn" && if test $legacy = 0; then
573         tfn=mksh
574 else
575         tfn=lksh
576 fi
577 if test -d $tfn || test -d $tfn.exe; then
578         echo "$me: Error: ./$tfn is a directory!" >&2
579         exit 1
580 fi
581 rmf a.exe* a.out* conftest.c conftest.exe* *core core.* ${tfn}* *.bc *.dbg \
582     *.ll *.o *.gen Rebuild.sh lft no signames.inc test.sh x vv.out
583
584 SRCS="lalloc.c eval.c exec.c expr.c funcs.c histrap.c jobs.c"
585 SRCS="$SRCS lex.c main.c misc.c shf.c syn.c tree.c var.c"
586
587 if test $legacy = 0; then
588         SRCS="$SRCS edit.c"
589         check_categories="$check_categories shell:legacy-no int:32"
590 else
591         check_categories="$check_categories shell:legacy-yes"
592         add_cppflags -DMKSH_LEGACY_MODE
593         HAVE_PERSISTENT_HISTORY=0
594         HAVE_ISSET_MKSH_CONSERVATIVE_FDS=1      # from sh.h
595 fi
596
597 if test x"$srcdir" = x"."; then
598         CPPFLAGS="-I. $CPPFLAGS"
599 else
600         CPPFLAGS="-I. -I'$srcdir' $CPPFLAGS"
601 fi
602 test -n "$LDSTATIC" && if test -n "$LDFLAGS"; then
603         LDFLAGS="$LDFLAGS $LDSTATIC"
604 else
605         LDFLAGS=$LDSTATIC
606 fi
607
608 if test -z "$TARGET_OS"; then
609         x=`uname -s 2>/dev/null || uname`
610         test x"$x" = x"`uname -n 2>/dev/null`" || TARGET_OS=$x
611 fi
612 if test -z "$TARGET_OS"; then
613         echo "$me: Set TARGET_OS, your uname is broken!" >&2
614         exit 1
615 fi
616 oswarn=
617 ccpc=-Wc,
618 ccpl=-Wl,
619 tsts=
620 ccpr='|| for _f in ${tcfn}*; do case $_f in Build.sh|check.pl|check.t|dot.mkshrc|*.1|*.c|*.h|*.ico|*.opt) ;; *) rm -f "$_f" ;; esac; done'
621
622 # Evil hack
623 if test x"$TARGET_OS" = x"Android"; then
624         check_categories="$check_categories android"
625         TARGET_OS=Linux
626 fi
627
628 # Evil OS
629 if test x"$TARGET_OS" = x"Minix"; then
630         echo >&2 "
631 WARNING: additional checks before running Build.sh required!
632 You can avoid these by calling Build.sh correctly, see below.
633 "
634         cat >conftest.c <<'EOF'
635 #include <sys/types.h>
636 const char *
637 #ifdef _NETBSD_SOURCE
638 ct="Ninix3"
639 #else
640 ct="Minix3"
641 #endif
642 ;
643 EOF
644         ct=unknown
645         vv ']' "${CC-cc} -E $CFLAGS $CPPFLAGS $NOWARN conftest.c | grep ct= | tr -d \\\\015 >x"
646         sed 's/^/[ /' x
647         eval `cat x`
648         rmf x vv.out
649         case $ct in
650         Minix3|Ninix3)
651                 echo >&2 "
652 Warning: you set TARGET_OS to $TARGET_OS but that is ambiguous.
653 Please set it to either Minix3 or Ninix3, whereas the latter is
654 all versions of Minix with even partial NetBSD(R) userland. The
655 value determined from your compiler for the current compilation
656 (which may be wrong) is: $ct
657 "
658                 TARGET_OS=$ct
659                 ;;
660         *)
661                 echo >&2 "
662 Warning: you set TARGET_OS to $TARGET_OS but that is ambiguous.
663 Please set it to either Minix3 or Ninix3, whereas the latter is
664 all versions of Minix with even partial NetBSD(R) userland. The
665 proper value couldn't be determined, continue at your own risk.
666 "
667                 ;;
668         esac
669 fi
670
671 # Configuration depending on OS revision, on OSes that need them
672 case $TARGET_OS in
673 NEXTSTEP)
674         test x"$TARGET_OSREV" = x"" && TARGET_OSREV=`hostinfo 2>&1 | \
675             grep 'NeXT Mach [0-9][0-9.]*:' | \
676             sed 's/^.*NeXT Mach \([0-9][0-9.]*\):.*$/\1/'`
677         ;;
678 QNX|SCO_SV)
679         test x"$TARGET_OSREV" = x"" && TARGET_OSREV=`uname -r`
680         ;;
681 esac
682
683 # Configuration depending on OS name
684 case $TARGET_OS in
685 386BSD)
686         : "${HAVE_CAN_OTWO=0}"
687         add_cppflags -DMKSH_NO_SIGSETJMP
688         add_cppflags -DMKSH_TYPEDEF_SIG_ATOMIC_T=int
689         add_cppflags -DMKSH_CONSERVATIVE_FDS
690         ;;
691 AIX)
692         add_cppflags -D_ALL_SOURCE
693         : "${HAVE_SETLOCALE_CTYPE=0}"
694         ;;
695 BeOS)
696         case $KSH_VERSION in
697         *MIRBSD\ KSH*)
698                 oswarn="; it has minor issues"
699                 ;;
700         *)
701                 oswarn="; you must recompile mksh with"
702                 oswarn="$oswarn${nl}itself in a second stage"
703                 ;;
704         esac
705         # BeOS has no real tty either
706         add_cppflags -DMKSH_UNEMPLOYED
707         add_cppflags -DMKSH_DISABLE_TTY_WARNING
708         # BeOS doesn't have different UIDs and GIDs
709         add_cppflags -DMKSH__NO_SETEUGID
710         ;;
711 BSD/OS)
712         : "${HAVE_SETLOCALE_CTYPE=0}"
713         ;;
714 Coherent)
715         oswarn="; it has major issues"
716         add_cppflags -DMKSH__NO_SYMLINK
717         check_categories="$check_categories nosymlink"
718         add_cppflags -DMKSH__NO_SETEUGID
719         add_cppflags -DMKSH_CONSERVATIVE_FDS
720         add_cppflags -DMKSH_DISABLE_TTY_WARNING
721         ;;
722 CYGWIN*)
723         : "${HAVE_SETLOCALE_CTYPE=0}"
724         ;;
725 Darwin)
726         add_cppflags -D_DARWIN_C_SOURCE
727         ;;
728 DragonFly)
729         ;;
730 FreeBSD)
731         ;;
732 FreeMiNT)
733         oswarn="; it has minor issues"
734         add_cppflags -D_GNU_SOURCE
735         add_cppflags -DMKSH_CONSERVATIVE_FDS
736         : "${HAVE_SETLOCALE_CTYPE=0}"
737         ;;
738 GNU)
739         case $CC in
740         *tendracc*) ;;
741         *) add_cppflags -D_GNU_SOURCE ;;
742         esac
743         # define MKSH__NO_PATH_MAX to use Hurd-only functions
744         add_cppflags -DMKSH__NO_PATH_MAX
745         ;;
746 GNU/kFreeBSD)
747         case $CC in
748         *tendracc*) ;;
749         *) add_cppflags -D_GNU_SOURCE ;;
750         esac
751         ;;
752 Haiku)
753         add_cppflags -DMKSH_ASSUME_UTF8; HAVE_ISSET_MKSH_ASSUME_UTF8=1
754         ;;
755 HP-UX)
756         ;;
757 Interix)
758         ccpc='-X '
759         ccpl='-Y '
760         add_cppflags -D_ALL_SOURCE
761         : "${LIBS=-lcrypt}"
762         : "${HAVE_SETLOCALE_CTYPE=0}"
763         ;;
764 IRIX*)
765         : "${HAVE_SETLOCALE_CTYPE=0}"
766         ;;
767 Linux)
768         case $CC in
769         *tendracc*) ;;
770         *) add_cppflags -D_GNU_SOURCE ;;
771         esac
772         add_cppflags -DSETUID_CAN_FAIL_WITH_EAGAIN
773         : "${HAVE_REVOKE=0}"
774         ;;
775 LynxOS)
776         oswarn="; it has minor issues"
777         ;;
778 MidnightBSD)
779         ;;
780 Minix-vmd)
781         add_cppflags -DMKSH__NO_SETEUGID
782         add_cppflags -DMKSH_UNEMPLOYED
783         add_cppflags -DMKSH_CONSERVATIVE_FDS
784         add_cppflags -D_MINIX_SOURCE
785         oldish_ed=no-stderr-ed          # no /bin/ed, maybe see below
786         : "${HAVE_SETLOCALE_CTYPE=0}"
787         ;;
788 Minix3)
789         add_cppflags -DMKSH_UNEMPLOYED
790         add_cppflags -DMKSH_CONSERVATIVE_FDS
791         add_cppflags -DMKSH_NO_LIMITS
792         add_cppflags -D_POSIX_SOURCE -D_POSIX_1_SOURCE=2 -D_MINIX
793         oldish_ed=no-stderr-ed          # /usr/bin/ed(!) is broken
794         : "${HAVE_SETLOCALE_CTYPE=0}"
795         ;;
796 MirBSD)
797         ;;
798 MSYS_*)
799         add_cppflags -DMKSH_ASSUME_UTF8=0; HAVE_ISSET_MKSH_ASSUME_UTF8=1
800         # almost same as CYGWIN* (from RT|Chatzilla)
801         : "${HAVE_SETLOCALE_CTYPE=0}"
802         # broken on this OE (from ir0nh34d)
803         : "${HAVE_STDINT_H=0}"
804         ;;
805 NetBSD)
806         ;;
807 NEXTSTEP)
808         add_cppflags -D_NEXT_SOURCE
809         add_cppflags -D_POSIX_SOURCE
810         : "${AWK=gawk}"
811         : "${CC=cc -posix}"
812         add_cppflags -DMKSH_NO_SIGSETJMP
813         # NeXTstep cannot get a controlling tty
814         add_cppflags -DMKSH_UNEMPLOYED
815         case $TARGET_OSREV in
816         4.2*)
817                 # OpenStep 4.2 is broken by default
818                 oswarn="; it needs libposix.a"
819                 ;;
820         esac
821         add_cppflags -DMKSH_CONSERVATIVE_FDS
822         ;;
823 Ninix3)
824         # similar to Minix3
825         add_cppflags -DMKSH_UNEMPLOYED
826         add_cppflags -DMKSH_CONSERVATIVE_FDS
827         add_cppflags -DMKSH_NO_LIMITS
828         # but no idea what else could be needed
829         oswarn="; it has unknown issues"
830         ;;
831 OpenBSD)
832         : "${HAVE_SETLOCALE_CTYPE=0}"
833         ;;
834 OS/2)
835         HAVE_TERMIOS_H=0
836         HAVE_MKNOD=0    # setmode() incompatible
837         oswarn="; it is currently being ported"
838         check_categories="$check_categories nosymlink"
839         : "${CC=gcc}"
840         : "${SIZE=: size}"
841         add_cppflags -DMKSH_UNEMPLOYED
842         add_cppflags -DMKSH_NOPROSPECTOFWORK
843         ;;
844 OSF1)
845         HAVE_SIG_T=0    # incompatible
846         add_cppflags -D_OSF_SOURCE
847         add_cppflags -D_POSIX_C_SOURCE=200112L
848         add_cppflags -D_XOPEN_SOURCE=600
849         add_cppflags -D_XOPEN_SOURCE_EXTENDED
850         : "${HAVE_SETLOCALE_CTYPE=0}"
851         ;;
852 Plan9)
853         add_cppflags -D_POSIX_SOURCE
854         add_cppflags -D_LIMITS_EXTENSION
855         add_cppflags -D_BSD_EXTENSION
856         add_cppflags -D_SUSV2_SOURCE
857         add_cppflags -DMKSH_ASSUME_UTF8; HAVE_ISSET_MKSH_ASSUME_UTF8=1
858         add_cppflags -DMKSH_NO_CMDLINE_EDITING
859         add_cppflags -DMKSH__NO_SETEUGID
860         oswarn=' and will currently not work'
861         add_cppflags -DMKSH_UNEMPLOYED
862         # this is for detecting kencc
863         add_cppflags -DMKSH_MAYBE_KENCC
864         ;;
865 PW32*)
866         HAVE_SIG_T=0    # incompatible
867         oswarn=' and will currently not work'
868         : "${HAVE_SETLOCALE_CTYPE=0}"
869         ;;
870 QNX)
871         add_cppflags -D__NO_EXT_QNX
872         add_cppflags -D__EXT_UNIX_MISC
873         case $TARGET_OSREV in
874         [012345].*|6.[0123].*|6.4.[01])
875                 oldish_ed=no-stderr-ed          # oldish /bin/ed is broken
876                 ;;
877         esac
878         : "${HAVE_SETLOCALE_CTYPE=0}"
879         ;;
880 SCO_SV)
881         case $TARGET_OSREV in
882         3.2*)
883                 # SCO OpenServer 5
884                 add_cppflags -DMKSH_UNEMPLOYED
885                 ;;
886         5*)
887                 # SCO OpenServer 6
888                 ;;
889         *)
890                 oswarn='; this is an unknown version of'
891                 oswarn="$oswarn$nl$TARGET_OS ${TARGET_OSREV}, please tell me what to do"
892                 ;;
893         esac
894         add_cppflags -DMKSH_CONSERVATIVE_FDS
895         : "${HAVE_SYS_SIGLIST=0}${HAVE__SYS_SIGLIST=0}"
896         ;;
897 skyos)
898         oswarn="; it has minor issues"
899         ;;
900 SunOS)
901         add_cppflags -D_BSD_SOURCE
902         add_cppflags -D__EXTENSIONS__
903         ;;
904 syllable)
905         add_cppflags -D_GNU_SOURCE
906         add_cppflags -DMKSH_NO_SIGSUSPEND
907         oswarn=' and will currently not work'
908         ;;
909 ULTRIX)
910         : "${CC=cc -YPOSIX}"
911         add_cppflags -DMKSH_TYPEDEF_SSIZE_T=int
912         add_cppflags -DMKSH_CONSERVATIVE_FDS
913         : "${HAVE_SETLOCALE_CTYPE=0}"
914         ;;
915 UnixWare|UNIX_SV)
916         # SCO UnixWare
917         add_cppflags -DMKSH_CONSERVATIVE_FDS
918         : "${HAVE_SYS_SIGLIST=0}${HAVE__SYS_SIGLIST=0}"
919         ;;
920 UWIN*)
921         ccpc='-Yc,'
922         ccpl='-Yl,'
923         tsts=" 3<>/dev/tty"
924         oswarn="; it will compile, but the target"
925         oswarn="$oswarn${nl}platform itself is very flakey/unreliable"
926         : "${HAVE_SETLOCALE_CTYPE=0}"
927         ;;
928 _svr4)
929         # generic target for SVR4 Unix with uname -s = uname -n
930         # this duplicates the * target below
931         oswarn='; it may or may not work'
932         test x"$TARGET_OSREV" = x"" && TARGET_OSREV=`uname -r`
933         ;;
934 *)
935         oswarn='; it may or may not work'
936         test x"$TARGET_OSREV" = x"" && TARGET_OSREV=`uname -r`
937         ;;
938 esac
939
940 : "${HAVE_MKNOD=0}"
941
942 : "${AWK=awk}${CC=cc}${NROFF=nroff}${SIZE=size}"
943 test 0 = $r && echo | $NROFF -v 2>&1 | grep GNU >/dev/null 2>&1 && \
944     echo | $NROFF -c >/dev/null 2>&1 && NROFF="$NROFF -c"
945
946 # this aids me in tracing FTBFSen without access to the buildd
947 $e "Hi from$ao $bi$srcversion$ao on:"
948 case $TARGET_OS in
949 AIX)
950         vv '|' "oslevel >&2"
951         vv '|' "uname -a >&2"
952         ;;
953 Darwin)
954         vv '|' "hwprefs machine_type os_type os_class >&2"
955         vv '|' "sw_vers >&2"
956         vv '|' "system_profiler SPSoftwareDataType SPHardwareDataType >&2"
957         vv '|' "/bin/sh --version >&2"
958         vv '|' "xcodebuild -version >&2"
959         vv '|' "uname -a >&2"
960         vv '|' "sysctl kern.version hw.machine hw.model hw.memsize hw.availcpu hw.cpufrequency hw.byteorder hw.cpu64bit_capable >&2"
961         ;;
962 IRIX*)
963         vv '|' "uname -a >&2"
964         vv '|' "hinv -v >&2"
965         ;;
966 OSF1)
967         vv '|' "uname -a >&2"
968         vv '|' "/usr/sbin/sizer -v >&2"
969         ;;
970 SCO_SV|UnixWare|UNIX_SV)
971         vv '|' "uname -a >&2"
972         vv '|' "uname -X >&2"
973         ;;
974 *)
975         vv '|' "uname -a >&2"
976         ;;
977 esac
978 test -z "$oswarn" || echo >&2 "
979 Warning: mksh has not yet been ported to or tested on your
980 operating system '$TARGET_OS'$oswarn. If you can provide
981 a shell account to the developer, this may improve; please
982 drop us a success or failure notice or even send in diffs.
983 "
984 $e "$bi$me: Building the MirBSD Korn Shell$ao $ui$dstversion$ao on $TARGET_OS ${TARGET_OSREV}..."
985
986 #
987 # Begin of mirtoconf checks
988 #
989 $e $bi$me: Scanning for functions... please ignore any errors.$ao
990
991 #
992 # Compiler: which one?
993 #
994 # notes:
995 # - ICC defines __GNUC__ too
996 # - GCC defines __hpux too
997 # - LLVM+clang defines __GNUC__ too
998 # - nwcc defines __GNUC__ too
999 CPP="$CC -E"
1000 $e ... which compiler seems to be used
1001 cat >conftest.c <<'EOF'
1002 const char *
1003 #if defined(__ICC) || defined(__INTEL_COMPILER)
1004 ct="icc"
1005 #elif defined(__xlC__) || defined(__IBMC__)
1006 ct="xlc"
1007 #elif defined(__SUNPRO_C)
1008 ct="sunpro"
1009 #elif defined(__ACK__)
1010 ct="ack"
1011 #elif defined(__BORLANDC__)
1012 ct="bcc"
1013 #elif defined(__WATCOMC__)
1014 ct="watcom"
1015 #elif defined(__MWERKS__)
1016 ct="metrowerks"
1017 #elif defined(__HP_cc)
1018 ct="hpcc"
1019 #elif defined(__DECC) || (defined(__osf__) && !defined(__GNUC__))
1020 ct="dec"
1021 #elif defined(__PGI)
1022 ct="pgi"
1023 #elif defined(__DMC__)
1024 ct="dmc"
1025 #elif defined(_MSC_VER)
1026 ct="msc"
1027 #elif defined(__ADSPBLACKFIN__) || defined(__ADSPTS__) || defined(__ADSP21000__)
1028 ct="adsp"
1029 #elif defined(__IAR_SYSTEMS_ICC__)
1030 ct="iar"
1031 #elif defined(SDCC)
1032 ct="sdcc"
1033 #elif defined(__PCC__)
1034 ct="pcc"
1035 #elif defined(__TenDRA__)
1036 ct="tendra"
1037 #elif defined(__TINYC__)
1038 ct="tcc"
1039 #elif defined(__llvm__) && defined(__clang__)
1040 ct="clang"
1041 #elif defined(__NWCC__)
1042 ct="nwcc"
1043 #elif defined(__GNUC__)
1044 ct="gcc"
1045 #elif defined(_COMPILER_VERSION)
1046 ct="mipspro"
1047 #elif defined(__sgi)
1048 ct="mipspro"
1049 #elif defined(__hpux) || defined(__hpua)
1050 ct="hpcc"
1051 #elif defined(__ultrix)
1052 ct="ucode"
1053 #elif defined(__USLC__)
1054 ct="uslc"
1055 #elif defined(__LCC__)
1056 ct="lcc"
1057 #elif defined(MKSH_MAYBE_KENCC)
1058 /* and none of the above matches */
1059 ct="kencc"
1060 #else
1061 ct="unknown"
1062 #endif
1063 ;
1064 const char *
1065 #if defined(__KLIBC__) && !defined(__OS2__)
1066 et="klibc"
1067 #else
1068 et="unknown"
1069 #endif
1070 ;
1071 EOF
1072 ct=untested
1073 et=untested
1074 vv ']' "$CPP $CFLAGS $CPPFLAGS $NOWARN conftest.c | \
1075     sed -n '/^ *[ce]t *= */s/^ *\([ce]t\) *= */\1=/p' | tr -d \\\\015 >x"
1076 sed 's/^/[ /' x
1077 eval `cat x`
1078 rmf x vv.out
1079 cat >conftest.c <<'EOF'
1080 #include <unistd.h>
1081 int main(void) { return (isatty(0)); }
1082 EOF
1083 case $ct in
1084 ack)
1085         # work around "the famous ACK const bug"
1086         CPPFLAGS="-Dconst= $CPPFLAGS"
1087         ;;
1088 adsp)
1089         echo >&2 'Warning: Analog Devices C++ compiler for Blackfin, TigerSHARC
1090     and SHARC (21000) DSPs detected. This compiler has not yet
1091     been tested for compatibility with mksh. Continue at your
1092     own risk, please report success/failure to the developers.'
1093         ;;
1094 bcc)
1095         echo >&2 "Warning: Borland C++ Builder detected. This compiler might
1096     produce broken executables. Continue at your own risk,
1097     please report success/failure to the developers."
1098         ;;
1099 clang)
1100         # does not work with current "ccc" compiler driver
1101         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS -version"
1102         # one of these two works, for now
1103         vv '|' "${CLANG-clang} -version"
1104         vv '|' "${CLANG-clang} --version"
1105         # ensure compiler and linker are in sync unless overridden
1106         case $CCC_CC:$CCC_LD in
1107         :*)     ;;
1108         *:)     CCC_LD=$CCC_CC; export CCC_LD ;;
1109         esac
1110         ;;
1111 dec)
1112         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS -V"
1113         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN -Wl,-V conftest.c $LIBS"
1114         ;;
1115 dmc)
1116         echo >&2 "Warning: Digital Mars Compiler detected. When running under"
1117         echo >&2 "    UWIN, mksh tends to be unstable due to the limitations"
1118         echo >&2 "    of this platform. Continue at your own risk,"
1119         echo >&2 "    please report success/failure to the developers."
1120         ;;
1121 gcc)
1122         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN -v conftest.c $LIBS"
1123         vv '|' 'echo `$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS \
1124             -dumpmachine` gcc`$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN \
1125             $LIBS -dumpversion`'
1126         ;;
1127 hpcc)
1128         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN -V conftest.c $LIBS"
1129         ;;
1130 iar)
1131         echo >&2 'Warning: IAR Systems (http://www.iar.com) compiler for embedded
1132     systems detected. This unsupported compiler has not yet
1133     been tested for compatibility with mksh. Continue at your
1134     own risk, please report success/failure to the developers.'
1135         ;;
1136 icc)
1137         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS -V"
1138         ;;
1139 kencc)
1140         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN -v conftest.c $LIBS"
1141         ;;
1142 lcc)
1143         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN -v conftest.c $LIBS"
1144         add_cppflags -D__inline__=__inline
1145         ;;
1146 metrowerks)
1147         echo >&2 'Warning: Metrowerks C compiler detected. This has not yet
1148     been tested for compatibility with mksh. Continue at your
1149     own risk, please report success/failure to the developers.'
1150         ;;
1151 mipspro)
1152         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS -version"
1153         ;;
1154 msc)
1155         ccpr=           # errorlevels are not reliable
1156         case $TARGET_OS in
1157         Interix)
1158                 if [[ -n $C89_COMPILER ]]; then
1159                         C89_COMPILER=`ntpath2posix -c "$C89_COMPILER"`
1160                 else
1161                         C89_COMPILER=CL.EXE
1162                 fi
1163                 if [[ -n $C89_LINKER ]]; then
1164                         C89_LINKER=`ntpath2posix -c "$C89_LINKER"`
1165                 else
1166                         C89_LINKER=LINK.EXE
1167                 fi
1168                 vv '|' "$C89_COMPILER /HELP >&2"
1169                 vv '|' "$C89_LINKER /LINK >&2"
1170                 ;;
1171         esac
1172         ;;
1173 nwcc)
1174         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS -version"
1175         ;;
1176 pcc)
1177         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS -v"
1178         ;;
1179 pgi)
1180         echo >&2 'Warning: PGI detected. This unknown compiler has not yet
1181     been tested for compatibility with mksh. Continue at your
1182     own risk, please report success/failure to the developers.'
1183         ;;
1184 sdcc)
1185         echo >&2 'Warning: sdcc (http://sdcc.sourceforge.net), the small devices
1186     C compiler for embedded systems detected. This has not yet
1187     been tested for compatibility with mksh. Continue at your
1188     own risk, please report success/failure to the developers.'
1189         ;;
1190 sunpro)
1191         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN -V conftest.c $LIBS"
1192         ;;
1193 tcc)
1194         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS -v"
1195         ;;
1196 tendra)
1197         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS -V 2>&1 | \
1198             fgrep -i -e version -e release"
1199         ;;
1200 ucode)
1201         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS -V"
1202         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN -Wl,-V conftest.c $LIBS"
1203         ;;
1204 uslc)
1205         case $TARGET_OS:$TARGET_OSREV in
1206         SCO_SV:3.2*)
1207                 # SCO OpenServer 5
1208                 CFLAGS="$CFLAGS -g"
1209                 : "${HAVE_CAN_OTWO=0}${HAVE_CAN_OPTIMISE=0}"
1210                 ;;
1211         esac
1212         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN -V conftest.c $LIBS"
1213         ;;
1214 watcom)
1215         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN -v conftest.c $LIBS"
1216         ;;
1217 xlc)
1218         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS -qversion"
1219         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN $LIBS -qversion=verbose"
1220         vv '|' "ld -V"
1221         ;;
1222 *)
1223         test x"$ct" = x"untested" && $e "!!! detecting preprocessor failed"
1224         ct=unknown
1225         vv "$CC --version"
1226         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN -v conftest.c $LIBS"
1227         vv '|' "$CC $CFLAGS $CPPFLAGS $LDFLAGS $NOWARN -V conftest.c $LIBS"
1228         ;;
1229 esac
1230 case $cm in
1231 dragonegg|llvm)
1232         vv '|' "llc -version"
1233         ;;
1234 esac
1235 case $et in
1236 klibc)
1237         add_cppflags -DMKSH_NO_LIMITS
1238         ;;
1239 unknown)
1240         # nothing special detected, don’t worry
1241         unset et
1242         ;;
1243 *)
1244         # huh?
1245         ;;
1246 esac
1247 $e "$bi==> which compiler seems to be used...$ao $ui$ct${et+ on $et}$ao"
1248 rmf conftest.c conftest.o conftest a.out* a.exe* conftest.exe* vv.out
1249
1250 #
1251 # Compiler: works as-is, with -Wno-error and -Werror
1252 #
1253 save_NOWARN=$NOWARN
1254 NOWARN=
1255 DOWARN=
1256 ac_flags 0 compiler_works '' 'if the compiler works'
1257 test 1 = $HAVE_CAN_COMPILER_WORKS || exit 1
1258 HAVE_COMPILER_KNOWN=0
1259 test $ct = unknown || HAVE_COMPILER_KNOWN=1
1260 if ac_ifcpp 'if 0' compiler_fails '' \
1261     'if the compiler does not fail correctly'; then
1262         save_CFLAGS=$CFLAGS
1263         : "${HAVE_CAN_DELEXE=x}"
1264         case $ct in
1265         dec)
1266                 CFLAGS="$CFLAGS ${ccpl}-non_shared"
1267                 ac_testn can_delexe compiler_fails 0 'for the -non_shared linker option' <<-EOF
1268                         #include <unistd.h>
1269                         int main(void) { return (isatty(0)); }
1270                 EOF
1271                 ;;
1272         dmc)
1273                 CFLAGS="$CFLAGS ${ccpl}/DELEXECUTABLE"
1274                 ac_testn can_delexe compiler_fails 0 'for the /DELEXECUTABLE linker option' <<-EOF
1275                         #include <unistd.h>
1276                         int main(void) { return (isatty(0)); }
1277                 EOF
1278                 ;;
1279         *)
1280                 exit 1
1281                 ;;
1282         esac
1283         test 1 = $HAVE_CAN_DELEXE || CFLAGS=$save_CFLAGS
1284         ac_testn compiler_still_fails '' 'if the compiler still does not fail correctly' <<-EOF
1285         EOF
1286         test 1 = $HAVE_COMPILER_STILL_FAILS && exit 1
1287 fi
1288 if ac_ifcpp 'ifdef __TINYC__' couldbe_tcc '!' compiler_known 0 \
1289     'if this could be tcc'; then
1290         ct=tcc
1291         CPP='cpp -D__TINYC__'
1292         HAVE_COMPILER_KNOWN=1
1293 fi
1294
1295 case $ct in
1296 bcc)
1297         save_NOWARN="${ccpc}-w"
1298         DOWARN="${ccpc}-w!"
1299         ;;
1300 dec)
1301         # -msg_* flags not used yet, or is -w2 correct?
1302         ;;
1303 dmc)
1304         save_NOWARN="${ccpc}-w"
1305         DOWARN="${ccpc}-wx"
1306         ;;
1307 hpcc)
1308         save_NOWARN=
1309         DOWARN=+We
1310         ;;
1311 kencc)
1312         save_NOWARN=
1313         DOWARN=
1314         ;;
1315 mipspro)
1316         save_NOWARN=
1317         DOWARN="-diag_error 1-10000"
1318         ;;
1319 msc)
1320         save_NOWARN="${ccpc}/w"
1321         DOWARN="${ccpc}/WX"
1322         ;;
1323 sunpro)
1324         test x"$save_NOWARN" = x"" && save_NOWARN='-errwarn=%none'
1325         ac_flags 0 errwarnnone "$save_NOWARN"
1326         test 1 = $HAVE_CAN_ERRWARNNONE || save_NOWARN=
1327         ac_flags 0 errwarnall "-errwarn=%all"
1328         test 1 = $HAVE_CAN_ERRWARNALL && DOWARN="-errwarn=%all"
1329         ;;
1330 tendra)
1331         save_NOWARN=-w
1332         ;;
1333 ucode)
1334         save_NOWARN=
1335         DOWARN=-w2
1336         ;;
1337 watcom)
1338         save_NOWARN=
1339         DOWARN=-Wc,-we
1340         ;;
1341 xlc)
1342         save_NOWARN=-qflag=i:e
1343         DOWARN=-qflag=i:i
1344         ;;
1345 *)
1346         test x"$save_NOWARN" = x"" && save_NOWARN=-Wno-error
1347         ac_flags 0 wnoerror "$save_NOWARN"
1348         test 1 = $HAVE_CAN_WNOERROR || save_NOWARN=
1349         ac_flags 0 werror -Werror
1350         test 1 = $HAVE_CAN_WERROR && DOWARN=-Werror
1351         test $ct = icc && DOWARN="$DOWARN -wd1419"
1352         ;;
1353 esac
1354 NOWARN=$save_NOWARN
1355
1356 #
1357 # Compiler: extra flags (-O2 -f* -W* etc.)
1358 #
1359 i=`echo :"$orig_CFLAGS" | sed 's/^://' | tr -c -d $alll$allu$alln`
1360 # optimisation: only if orig_CFLAGS is empty
1361 test x"$i" = x"" && case $ct in
1362 hpcc)
1363         phase=u
1364         ac_flags 1 otwo +O2
1365         phase=x
1366         ;;
1367 kencc|tcc|tendra)
1368         # no special optimisation
1369         ;;
1370 sunpro)
1371         cat >x <<-'EOF'
1372                 #include <unistd.h>
1373                 int main(void) { return (isatty(0)); }
1374                 #define __IDSTRING_CONCAT(l,p)  __LINTED__ ## l ## _ ## p
1375                 #define __IDSTRING_EXPAND(l,p)  __IDSTRING_CONCAT(l,p)
1376                 #define pad                     void __IDSTRING_EXPAND(__LINE__,x)(void) { }
1377         EOF
1378         yes pad | head -n 256 >>x
1379         ac_flags - 1 otwo -xO2 <x
1380         rmf x
1381         ;;
1382 xlc)
1383         ac_flags 1 othree "-O3 -qstrict"
1384         test 1 = $HAVE_CAN_OTHREE || ac_flags 1 otwo -O2
1385         ;;
1386 *)
1387         ac_flags 1 otwo -O2
1388         test 1 = $HAVE_CAN_OTWO || ac_flags 1 optimise -O
1389         ;;
1390 esac
1391 # other flags: just add them if they are supported
1392 i=0
1393 case $ct in
1394 bcc)
1395         ac_flags 1 strpool "${ccpc}-d" 'if string pooling can be enabled'
1396         ;;
1397 clang)
1398         i=1
1399         ;;
1400 dec)
1401         ac_flags 0 verb -verbose
1402         ac_flags 1 rodata -readonly_strings
1403         ;;
1404 dmc)
1405         ac_flags 1 decl "${ccpc}-r" 'for strict prototype checks'
1406         ac_flags 1 schk "${ccpc}-s" 'for stack overflow checking'
1407         ;;
1408 gcc)
1409         # The following tests run with -Werror (gcc only) if possible
1410         NOWARN=$DOWARN; phase=u
1411         ac_flags 1 wnodeprecateddecls -Wno-deprecated-declarations
1412         # mksh is not written in CFrustFrust!
1413         ac_flags 1 no_eh_frame -fno-asynchronous-unwind-tables
1414         ac_flags 1 fnostrictaliasing -fno-strict-aliasing
1415         ac_flags 1 fstackprotectorstrong -fstack-protector-strong
1416         test 1 = $HAVE_CAN_FSTACKPROTECTORSTRONG || \
1417             ac_flags 1 fstackprotectorall -fstack-protector-all
1418         test $cm = dragonegg && case " $CC $CFLAGS $LDFLAGS " in
1419         *\ -fplugin=*dragonegg*) ;;
1420         *) ac_flags 1 fplugin_dragonegg -fplugin=dragonegg ;;
1421         esac
1422         case $cm in
1423         combine)
1424                 fv=0
1425                 checks='7 8'
1426                 ;;
1427         lto)
1428                 fv=0
1429                 checks='1 2 3 4 5 6 7 8'
1430                 ;;
1431         *)
1432                 fv=1
1433                 ;;
1434         esac
1435         test $fv = 1 || for what in $checks; do
1436                 test $fv = 1 && break
1437                 case $what in
1438                 1)      t_cflags='-flto=jobserver'
1439                         t_ldflags='-fuse-linker-plugin'
1440                         t_use=1 t_name=fltojs_lp ;;
1441                 2)      t_cflags='-flto=jobserver' t_ldflags=''
1442                         t_use=1 t_name=fltojs_nn ;;
1443                 3)      t_cflags='-flto=jobserver'
1444                         t_ldflags='-fno-use-linker-plugin -fwhole-program'
1445                         t_use=1 t_name=fltojs_np ;;
1446                 4)      t_cflags='-flto'
1447                         t_ldflags='-fuse-linker-plugin'
1448                         t_use=1 t_name=fltons_lp ;;
1449                 5)      t_cflags='-flto' t_ldflags=''
1450                         t_use=1 t_name=fltons_nn ;;
1451                 6)      t_cflags='-flto'
1452                         t_ldflags='-fno-use-linker-plugin -fwhole-program'
1453                         t_use=1 t_name=fltons_np ;;
1454                 7)      t_cflags='-fwhole-program --combine' t_ldflags=''
1455                         t_use=0 t_name=combine cm=combine ;;
1456                 8)      fv=1 cm=normal ;;
1457                 esac
1458                 test $fv = 1 && break
1459                 ac_flags $t_use $t_name "$t_cflags" \
1460                     "if gcc supports $t_cflags $t_ldflags" "$t_ldflags"
1461         done
1462         i=1
1463         ;;
1464 hpcc)
1465         phase=u
1466         # probably not needed
1467         #ac_flags 1 agcc -Agcc 'for support of GCC extensions'
1468         phase=x
1469         ;;
1470 icc)
1471         ac_flags 1 fnobuiltinsetmode -fno-builtin-setmode
1472         ac_flags 1 fnostrictaliasing -fno-strict-aliasing
1473         ac_flags 1 fstacksecuritycheck -fstack-security-check
1474         i=1
1475         ;;
1476 mipspro)
1477         ac_flags 1 fullwarn -fullwarn 'for remark output support'
1478         ;;
1479 msc)
1480         ac_flags 1 strpool "${ccpc}/GF" 'if string pooling can be enabled'
1481         echo 'int main(void) { char test[64] = ""; return (*test); }' >x
1482         ac_flags - 1 stackon "${ccpc}/GZ" 'if stack checks can be enabled' <x
1483         ac_flags - 1 stckall "${ccpc}/Ge" 'stack checks for all functions' <x
1484         ac_flags - 1 secuchk "${ccpc}/GS" 'for compiler security checks' <x
1485         rmf x
1486         ac_flags 1 wall "${ccpc}/Wall" 'to enable all warnings'
1487         ac_flags 1 wp64 "${ccpc}/Wp64" 'to enable 64-bit warnings'
1488         ;;
1489 nwcc)
1490         i=1
1491         #broken# ac_flags 1 ssp -stackprotect
1492         ;;
1493 sunpro)
1494         phase=u
1495         ac_flags 1 v -v
1496         ac_flags 1 ipo -xipo 'for cross-module optimisation'
1497         phase=x
1498         ;;
1499 tcc)
1500         : #broken# ac_flags 1 boundschk -b
1501         ;;
1502 tendra)
1503         ac_flags 0 ysystem -Ysystem
1504         test 1 = $HAVE_CAN_YSYSTEM && CPPFLAGS="-Ysystem $CPPFLAGS"
1505         ac_flags 1 extansi -Xa
1506         ;;
1507 xlc)
1508         ac_flags 1 rodata "-qro -qroconst -qroptr"
1509         ac_flags 1 rtcheck -qcheck=all
1510         #ac_flags 1 rtchkc -qextchk     # reported broken
1511         ac_flags 1 wformat "-qformat=all -qformat=nozln"
1512         #ac_flags 1 wp64 -qwarn64       # too verbose for now
1513         ;;
1514 esac
1515 # flags common to a subset of compilers (run with -Werror on gcc)
1516 if test 1 = $i; then
1517         ac_flags 1 wall -Wall
1518         ac_flags 1 fwrapv -fwrapv
1519 fi
1520
1521 phase=x
1522 # The following tests run with -Werror or similar (all compilers) if possible
1523 NOWARN=$DOWARN
1524 test $ct = pcc && phase=u
1525
1526 #
1527 # Compiler: check for stuff that only generates warnings
1528 #
1529 ac_test attribute_bounded '' 'for __attribute__((__bounded__))' <<-'EOF'
1530         #if defined(__TenDRA__) || (defined(__GNUC__) && (__GNUC__ < 2))
1531         extern int thiswillneverbedefinedIhope(void);
1532         /* force a failure: TenDRA and gcc 1.42 have false positive here */
1533         int main(void) { return (thiswillneverbedefinedIhope()); }
1534         #else
1535         #include <string.h>
1536         #undef __attribute__
1537         int xcopy(const void *, void *, size_t)
1538             __attribute__((__bounded__(__buffer__, 1, 3)))
1539             __attribute__((__bounded__(__buffer__, 2, 3)));
1540         int main(int ac, char *av[]) { return (xcopy(av[0], av[--ac], 1)); }
1541         int xcopy(const void *s, void *d, size_t n) {
1542                 /*
1543                  * if memmove does not exist, we are not on a system
1544                  * with GCC with __bounded__ attribute either so poo
1545                  */
1546                 memmove(d, s, n); return ((int)n);
1547         }
1548         #endif
1549 EOF
1550 ac_test attribute_format '' 'for __attribute__((__format__))' <<-'EOF'
1551         #if defined(__TenDRA__) || (defined(__GNUC__) && (__GNUC__ < 2))
1552         extern int thiswillneverbedefinedIhope(void);
1553         /* force a failure: TenDRA and gcc 1.42 have false positive here */
1554         int main(void) { return (thiswillneverbedefinedIhope()); }
1555         #else
1556         #define fprintf printfoo
1557         #include <stdio.h>
1558         #undef __attribute__
1559         #undef fprintf
1560         extern int fprintf(FILE *, const char *format, ...)
1561             __attribute__((__format__(__printf__, 2, 3)));
1562         int main(int ac, char **av) { return (fprintf(stderr, "%s%d", *av, ac)); }
1563         #endif
1564 EOF
1565 ac_test attribute_noreturn '' 'for __attribute__((__noreturn__))' <<-'EOF'
1566         #if defined(__TenDRA__) || (defined(__GNUC__) && (__GNUC__ < 2))
1567         extern int thiswillneverbedefinedIhope(void);
1568         /* force a failure: TenDRA and gcc 1.42 have false positive here */
1569         int main(void) { return (thiswillneverbedefinedIhope()); }
1570         #else
1571         #include <stdlib.h>
1572         #undef __attribute__
1573         void fnord(void) __attribute__((__noreturn__));
1574         int main(void) { fnord(); }
1575         void fnord(void) { exit(0); }
1576         #endif
1577 EOF
1578 ac_test attribute_pure '' 'for __attribute__((__pure__))' <<-'EOF'
1579         #if defined(__TenDRA__) || (defined(__GNUC__) && (__GNUC__ < 2))
1580         extern int thiswillneverbedefinedIhope(void);
1581         /* force a failure: TenDRA and gcc 1.42 have false positive here */
1582         int main(void) { return (thiswillneverbedefinedIhope()); }
1583         #else
1584         #include <unistd.h>
1585         #undef __attribute__
1586         int foo(const char *) __attribute__((__pure__));
1587         int main(int ac, char **av) { return (foo(av[ac - 1]) + isatty(0)); }
1588         int foo(const char *s) { return ((int)s[0]); }
1589         #endif
1590 EOF
1591 ac_test attribute_unused '' 'for __attribute__((__unused__))' <<-'EOF'
1592         #if defined(__TenDRA__) || (defined(__GNUC__) && (__GNUC__ < 2))
1593         extern int thiswillneverbedefinedIhope(void);
1594         /* force a failure: TenDRA and gcc 1.42 have false positive here */
1595         int main(void) { return (thiswillneverbedefinedIhope()); }
1596         #else
1597         #include <unistd.h>
1598         #undef __attribute__
1599         int main(int ac __attribute__((__unused__)), char **av
1600             __attribute__((__unused__))) { return (isatty(0)); }
1601         #endif
1602 EOF
1603 ac_test attribute_used '' 'for __attribute__((__used__))' <<-'EOF'
1604         #if defined(__TenDRA__) || (defined(__GNUC__) && (__GNUC__ < 2))
1605         extern int thiswillneverbedefinedIhope(void);
1606         /* force a failure: TenDRA and gcc 1.42 have false positive here */
1607         int main(void) { return (thiswillneverbedefinedIhope()); }
1608         #else
1609         #include <unistd.h>
1610         #undef __attribute__
1611         static const char fnord[] __attribute__((__used__)) = "42";
1612         int main(void) { return (isatty(0)); }
1613         #endif
1614 EOF
1615
1616 # End of tests run with -Werror
1617 NOWARN=$save_NOWARN
1618 phase=x
1619
1620 #
1621 # mksh: flavours (full/small mksh, omit certain stuff)
1622 #
1623 if ac_ifcpp 'ifdef MKSH_SMALL' isset_MKSH_SMALL '' \
1624     "if a reduced-feature mksh is requested"; then
1625         : "${HAVE_NICE=0}"
1626         : "${HAVE_PERSISTENT_HISTORY=0}"
1627         check_categories="$check_categories smksh"
1628         HAVE_ISSET_MKSH_CONSERVATIVE_FDS=1      # from sh.h
1629 fi
1630 ac_ifcpp 'if defined(MKSH_BINSHPOSIX) || defined(MKSH_BINSHREDUCED)' \
1631     isset_MKSH_BINSH '' 'if invoking as sh should be handled specially' && \
1632     check_categories="$check_categories binsh"
1633 ac_ifcpp 'ifdef MKSH_UNEMPLOYED' isset_MKSH_UNEMPLOYED '' \
1634     "if mksh will be built without job control" && \
1635     check_categories="$check_categories arge"
1636 ac_ifcpp 'ifdef MKSH_NOPROSPECTOFWORK' isset_MKSH_NOPROSPECTOFWORK '' \
1637     "if mksh will be built without job signals" && \
1638     check_categories="$check_categories arge nojsig"
1639 ac_ifcpp 'ifdef MKSH_ASSUME_UTF8' isset_MKSH_ASSUME_UTF8 '' \
1640     'if the default UTF-8 mode is specified' && : "${HAVE_SETLOCALE_CTYPE=0}"
1641 ac_ifcpp 'ifdef MKSH_CONSERVATIVE_FDS' isset_MKSH_CONSERVATIVE_FDS '' \
1642     'if traditional/conservative fd use is requested' && \
1643     check_categories="$check_categories convfds"
1644 #ac_ifcpp 'ifdef MKSH_DISABLE_DEPRECATED' isset_MKSH_DISABLE_DEPRECATED '' \
1645 #    "if deprecated features are to be omitted" && \
1646 #    check_categories="$check_categories nodeprecated"
1647 #ac_ifcpp 'ifdef MKSH_DISABLE_EXPERIMENTAL' isset_MKSH_DISABLE_EXPERIMENTAL '' \
1648 #    "if experimental features are to be omitted" && \
1649 #    check_categories="$check_categories noexperimental"
1650 ac_ifcpp 'ifdef MKSH_MIDNIGHTBSD01ASH_COMPAT' isset_MKSH_MIDNIGHTBSD01ASH_COMPAT '' \
1651     'if the MidnightBSD 0.1 ash compatibility mode is requested' && \
1652     check_categories="$check_categories mnbsdash"
1653
1654 #
1655 # Environment: headers
1656 #
1657 ac_header sys/time.h sys/types.h
1658 ac_header time.h sys/types.h
1659 test "11" = "$HAVE_SYS_TIME_H$HAVE_TIME_H" || HAVE_BOTH_TIME_H=0
1660 ac_test both_time_h '' 'whether <sys/time.h> and <time.h> can both be included' <<-'EOF'
1661         #include <sys/types.h>
1662         #include <sys/time.h>
1663         #include <time.h>
1664         #include <unistd.h>
1665         int main(void) { struct tm tm; return ((int)sizeof(tm) + isatty(0)); }
1666 EOF
1667 ac_header sys/bsdtypes.h
1668 ac_header sys/file.h sys/types.h
1669 ac_header sys/mkdev.h sys/types.h
1670 ac_header sys/mman.h sys/types.h
1671 ac_header sys/param.h
1672 ac_header sys/resource.h sys/types.h _time
1673 ac_header sys/select.h sys/types.h
1674 ac_header sys/sysmacros.h
1675 ac_header bstring.h
1676 ac_header grp.h sys/types.h
1677 ac_header io.h
1678 ac_header libgen.h
1679 ac_header libutil.h sys/types.h
1680 ac_header paths.h
1681 ac_header stdint.h stdarg.h
1682 # include strings.h only if compatible with string.h
1683 ac_header strings.h sys/types.h string.h
1684 ac_header termios.h
1685 ac_header ulimit.h sys/types.h
1686 ac_header values.h
1687
1688 #
1689 # Environment: definitions
1690 #
1691 echo '#include <sys/types.h>
1692 #include <unistd.h>
1693 /* check that off_t can represent 2^63-1 correctly, thx FSF */
1694 #define LARGE_OFF_T ((((off_t)1 << 31) << 31) - 1 + (((off_t)1 << 31) << 31))
1695 int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 &&
1696     LARGE_OFF_T % 2147483647 == 1) ? 1 : -1];
1697 int main(void) { return (isatty(0)); }' >lft.c
1698 ac_testn can_lfs '' "for large file support" <lft.c
1699 save_CPPFLAGS=$CPPFLAGS
1700 add_cppflags -D_FILE_OFFSET_BITS=64
1701 ac_testn can_lfs_sus '!' can_lfs 0 "... with -D_FILE_OFFSET_BITS=64" <lft.c
1702 if test 0 = $HAVE_CAN_LFS_SUS; then
1703         CPPFLAGS=$save_CPPFLAGS
1704         add_cppflags -D_LARGE_FILES=1
1705         ac_testn can_lfs_aix '!' can_lfs 0 "... with -D_LARGE_FILES=1" <lft.c
1706         test 1 = $HAVE_CAN_LFS_AIX || CPPFLAGS=$save_CPPFLAGS
1707 fi
1708 rm -f lft.c
1709 rmf lft*        # end of large file support test
1710
1711 #
1712 # Environment: types
1713 #
1714 ac_test can_inttypes '!' stdint_h 1 "for standard 32-bit integer types" <<-'EOF'
1715         #include <sys/types.h>
1716         #include <stddef.h>
1717         int main(int ac, char **av) { return ((uint32_t)(size_t)*av + (int32_t)ac); }
1718 EOF
1719 ac_test can_ucbints '!' can_inttypes 1 "for UCB 32-bit integer types" <<-'EOF'
1720         #include <sys/types.h>
1721         #include <stddef.h>
1722         int main(int ac, char **av) { return ((u_int32_t)(size_t)*av + (int32_t)ac); }
1723 EOF
1724 ac_test can_int8type '!' stdint_h 1 "for standard 8-bit integer type" <<-'EOF'
1725         #include <sys/types.h>
1726         #include <stddef.h>
1727         int main(int ac, char **av) { return ((uint8_t)(size_t)av[ac]); }
1728 EOF
1729 ac_test can_ucbint8 '!' can_int8type 1 "for UCB 8-bit integer type" <<-'EOF'
1730         #include <sys/types.h>
1731         #include <stddef.h>
1732         int main(int ac, char **av) { return ((u_int8_t)(size_t)av[ac]); }
1733 EOF
1734
1735 ac_test rlim_t <<-'EOF'
1736         #include <sys/types.h>
1737         #if HAVE_BOTH_TIME_H
1738         #include <sys/time.h>
1739         #include <time.h>
1740         #elif HAVE_SYS_TIME_H
1741         #include <sys/time.h>
1742         #elif HAVE_TIME_H
1743         #include <time.h>
1744         #endif
1745         #if HAVE_SYS_RESOURCE_H
1746         #include <sys/resource.h>
1747         #endif
1748         #include <unistd.h>
1749         int main(void) { return (((int)(rlim_t)0) + isatty(0)); }
1750 EOF
1751
1752 # only testn: added later below
1753 ac_testn sig_t <<-'EOF'
1754         #include <sys/types.h>
1755         #include <signal.h>
1756         #include <stddef.h>
1757         volatile sig_t foo = (sig_t)0;
1758         int main(void) { return (foo == (sig_t)0); }
1759 EOF
1760
1761 ac_testn sighandler_t '!' sig_t 0 <<-'EOF'
1762         #include <sys/types.h>
1763         #include <signal.h>
1764         #include <stddef.h>
1765         volatile sighandler_t foo = (sighandler_t)0;
1766         int main(void) { return (foo == (sighandler_t)0); }
1767 EOF
1768 if test 1 = $HAVE_SIGHANDLER_T; then
1769         add_cppflags -Dsig_t=sighandler_t
1770         HAVE_SIG_T=1
1771 fi
1772
1773 ac_testn __sighandler_t '!' sig_t 0 <<-'EOF'
1774         #include <sys/types.h>
1775         #include <signal.h>
1776         #include <stddef.h>
1777         volatile __sighandler_t foo = (__sighandler_t)0;
1778         int main(void) { return (foo == (__sighandler_t)0); }
1779 EOF
1780 if test 1 = $HAVE___SIGHANDLER_T; then
1781         add_cppflags -Dsig_t=__sighandler_t
1782         HAVE_SIG_T=1
1783 fi
1784
1785 test 1 = $HAVE_SIG_T || add_cppflags -Dsig_t=nosig_t
1786 ac_cppflags SIG_T
1787
1788 #
1789 # check whether whatever we use for the final link will succeed
1790 #
1791 if test $cm = makefile; then
1792         : nothing to check
1793 else
1794         HAVE_LINK_WORKS=x
1795         ac_testinit link_works '' 'checking if the final link command may succeed'
1796         fv=1
1797         cat >conftest.c <<-EOF
1798                 #define EXTERN
1799                 #define MKSH_INCLUDES_ONLY
1800                 #include "sh.h"
1801                 __RCSID("$srcversion");
1802                 int main(void) { printf("Hello, World!\\n"); return (isatty(0)); }
1803 EOF
1804         case $cm in
1805         llvm)
1806                 v "$CC $CFLAGS $CPPFLAGS $NOWARN -emit-llvm -c conftest.c" || fv=0
1807                 rmf $tfn.s
1808                 test $fv = 0 || v "llvm-link -o - conftest.o | opt $optflags | llc -o $tfn.s" || fv=0
1809                 test $fv = 0 || v "$CC $CFLAGS $LDFLAGS -o $tcfn $tfn.s $LIBS $ccpr"
1810                 ;;
1811         dragonegg)
1812                 v "$CC $CFLAGS $CPPFLAGS $NOWARN -S -flto conftest.c" || fv=0
1813                 test $fv = 0 || v "mv conftest.s conftest.ll"
1814                 test $fv = 0 || v "llvm-as conftest.ll" || fv=0
1815                 rmf $tfn.s
1816                 test $fv = 0 || v "llvm-link -o - conftest.bc | opt $optflags | llc -o $tfn.s" || fv=0
1817                 test $fv = 0 || v "$CC $CFLAGS $LDFLAGS -o $tcfn $tfn.s $LIBS $ccpr"
1818                 ;;
1819         combine)
1820                 v "$CC $CFLAGS $CPPFLAGS $LDFLAGS -fwhole-program --combine $NOWARN -o $tcfn conftest.c $LIBS $ccpr"
1821                 ;;
1822         lto|normal)
1823                 cm=normal
1824                 v "$CC $CFLAGS $CPPFLAGS $NOWARN -c conftest.c" || fv=0
1825                 test $fv = 0 || v "$CC $CFLAGS $LDFLAGS -o $tcfn conftest.o $LIBS $ccpr"
1826                 ;;
1827         esac
1828         test -f $tcfn || fv=0
1829         ac_testdone
1830         test $fv = 1 || exit 1
1831 fi
1832
1833 #
1834 # Environment: errors and signals
1835 #
1836 test x"NetBSD" = x"$TARGET_OS" && $e Ignore the compatibility warning.
1837
1838 ac_testn sys_errlist '' "the sys_errlist[] array and sys_nerr" <<-'EOF'
1839         extern const int sys_nerr;
1840         extern const char * const sys_errlist[];
1841         extern int isatty(int);
1842         int main(void) { return (*sys_errlist[sys_nerr - 1] + isatty(0)); }
1843 EOF
1844 ac_testn _sys_errlist '!' sys_errlist 0 "the _sys_errlist[] array and _sys_nerr" <<-'EOF'
1845         extern const int _sys_nerr;
1846         extern const char * const _sys_errlist[];
1847         extern int isatty(int);
1848         int main(void) { return (*_sys_errlist[_sys_nerr - 1] + isatty(0)); }
1849 EOF
1850 if test 1 = "$HAVE__SYS_ERRLIST"; then
1851         add_cppflags -Dsys_nerr=_sys_nerr
1852         add_cppflags -Dsys_errlist=_sys_errlist
1853         HAVE_SYS_ERRLIST=1
1854 fi
1855 ac_cppflags SYS_ERRLIST
1856
1857 for what in name list; do
1858         uwhat=`upper $what`
1859         ac_testn sys_sig$what '' "the sys_sig${what}[] array" <<-EOF
1860                 extern const char * const sys_sig${what}[];
1861                 extern int isatty(int);
1862                 int main(void) { return (sys_sig${what}[0][0] + isatty(0)); }
1863         EOF
1864         ac_testn _sys_sig$what '!' sys_sig$what 0 "the _sys_sig${what}[] array" <<-EOF
1865                 extern const char * const _sys_sig${what}[];
1866                 extern int isatty(int);
1867                 int main(void) { return (_sys_sig${what}[0][0] + isatty(0)); }
1868         EOF
1869         eval uwhat_v=\$HAVE__SYS_SIG$uwhat
1870         if test 1 = "$uwhat_v"; then
1871                 add_cppflags -Dsys_sig$what=_sys_sig$what
1872                 eval HAVE_SYS_SIG$uwhat=1
1873         fi
1874         ac_cppflags SYS_SIG$uwhat
1875 done
1876
1877 #
1878 # Environment: library functions
1879 #
1880 ac_test flock <<-'EOF'
1881         #include <sys/types.h>
1882         #include <fcntl.h>
1883         #undef flock
1884         #if HAVE_SYS_FILE_H
1885         #include <sys/file.h>
1886         #endif
1887         int main(void) { return (flock(0, LOCK_EX | LOCK_UN)); }
1888 EOF
1889
1890 ac_test lock_fcntl '!' flock 1 'whether we can lock files with fcntl' <<-'EOF'
1891         #include <fcntl.h>
1892         #undef flock
1893         int main(void) {
1894                 struct flock lks;
1895                 lks.l_type = F_WRLCK | F_UNLCK;
1896                 return (fcntl(0, F_SETLKW, &lks));
1897         }
1898 EOF
1899
1900 ac_test getrusage <<-'EOF'
1901         #define MKSH_INCLUDES_ONLY
1902         #include "sh.h"
1903         int main(void) {
1904                 struct rusage ru;
1905                 return (getrusage(RUSAGE_SELF, &ru) +
1906                     getrusage(RUSAGE_CHILDREN, &ru));
1907         }
1908 EOF
1909
1910 ac_test getsid <<-'EOF'
1911         #include <unistd.h>
1912         int main(void) { return ((int)getsid(0)); }
1913 EOF
1914
1915 ac_test gettimeofday <<-'EOF'
1916         #define MKSH_INCLUDES_ONLY
1917         #include "sh.h"
1918         int main(void) { struct timeval tv; return (gettimeofday(&tv, NULL)); }
1919 EOF
1920
1921 ac_test issetugid <<-'EOF'
1922         #include <unistd.h>
1923         int main(void) { return (issetugid()); }
1924 EOF
1925
1926 ac_test killpg <<-'EOF'
1927         #include <signal.h>
1928         int main(int ac, char *av[]) { return (av[0][killpg(123, ac)]); }
1929 EOF
1930
1931 ac_test memmove <<-'EOF'
1932         #include <sys/types.h>
1933         #include <stddef.h>
1934         #include <string.h>
1935         #if HAVE_STRINGS_H
1936         #include <strings.h>
1937         #endif
1938         int main(int ac, char *av[]) {
1939                 return (*(int *)(void *)memmove(av[0], av[1], (size_t)ac));
1940         }
1941 EOF
1942
1943 ac_test mknod '' 'if to use mknod(), makedev() and friends' <<-'EOF'
1944         #define MKSH_INCLUDES_ONLY
1945         #include "sh.h"
1946         int main(int ac, char *av[]) {
1947                 dev_t dv;
1948                 dv = makedev((unsigned int)ac, (unsigned int)av[0][0]);
1949                 return (mknod(av[0], (mode_t)0, dv) ? (int)major(dv) :
1950                     (int)minor(dv));
1951         }
1952 EOF
1953
1954 ac_test mmap lock_fcntl 0 'for mmap and munmap' <<-'EOF'
1955         #include <sys/types.h>
1956         #if HAVE_SYS_FILE_H
1957         #include <sys/file.h>
1958         #endif
1959         #if HAVE_SYS_MMAN_H
1960         #include <sys/mman.h>
1961         #endif
1962         #include <stddef.h>
1963         #include <stdlib.h>
1964         int main(void) { return ((void *)mmap(NULL, (size_t)0,
1965             PROT_READ, MAP_PRIVATE, 0, (off_t)0) == (void *)NULL ? 1 :
1966             munmap(NULL, 0)); }
1967 EOF
1968
1969 ac_test nice <<-'EOF'
1970         #include <unistd.h>
1971         int main(void) { return (nice(4)); }
1972 EOF
1973
1974 ac_test revoke <<-'EOF'
1975         #include <sys/types.h>
1976         #if HAVE_LIBUTIL_H
1977         #include <libutil.h>
1978         #endif
1979         #include <unistd.h>
1980         int main(int ac, char *av[]) { return (ac + revoke(av[0])); }
1981 EOF
1982
1983 ac_test setlocale_ctype '' 'setlocale(LC_CTYPE, "")' <<-'EOF'
1984         #include <locale.h>
1985         #include <stddef.h>
1986         int main(void) { return ((int)(size_t)(void *)setlocale(LC_CTYPE, "")); }
1987 EOF
1988
1989 ac_test langinfo_codeset setlocale_ctype 0 'nl_langinfo(CODESET)' <<-'EOF'
1990         #include <langinfo.h>
1991         #include <stddef.h>
1992         int main(void) { return ((int)(size_t)(void *)nl_langinfo(CODESET)); }
1993 EOF
1994
1995 ac_test select <<-'EOF'
1996         #include <sys/types.h>
1997         #if HAVE_BOTH_TIME_H
1998         #include <sys/time.h>
1999         #include <time.h>
2000         #elif HAVE_SYS_TIME_H
2001         #include <sys/time.h>
2002         #elif HAVE_TIME_H
2003         #include <time.h>
2004         #endif
2005         #if HAVE_SYS_BSDTYPES_H
2006         #include <sys/bsdtypes.h>
2007         #endif
2008         #if HAVE_SYS_SELECT_H
2009         #include <sys/select.h>
2010         #endif
2011         #if HAVE_BSTRING_H
2012         #include <bstring.h>
2013         #endif
2014         #include <stddef.h>
2015         #include <stdlib.h>
2016         #include <string.h>
2017         #if HAVE_STRINGS_H
2018         #include <strings.h>
2019         #endif
2020         #include <unistd.h>
2021         int main(void) {
2022                 struct timeval tv = { 1, 200000 };
2023                 fd_set fds; FD_ZERO(&fds); FD_SET(0, &fds);
2024                 return (select(FD_SETSIZE, &fds, NULL, NULL, &tv));
2025         }
2026 EOF
2027
2028 ac_test setresugid <<-'EOF'
2029         #include <sys/types.h>
2030         #include <unistd.h>
2031         int main(void) { return (setresuid(0,0,0) + setresgid(0,0,0)); }
2032 EOF
2033
2034 ac_test setgroups setresugid 0 <<-'EOF'
2035         #include <sys/types.h>
2036         #if HAVE_GRP_H
2037         #include <grp.h>
2038         #endif
2039         #include <unistd.h>
2040         int main(void) { gid_t gid = 0; return (setgroups(0, &gid)); }
2041 EOF
2042
2043 if test x"$et" = x"klibc"; then
2044
2045         ac_testn __rt_sigsuspend '' 'whether klibc uses RT signals' <<-'EOF'
2046                 #define MKSH_INCLUDES_ONLY
2047                 #include "sh.h"
2048                 extern int __rt_sigsuspend(const sigset_t *, size_t);
2049                 int main(void) { return (__rt_sigsuspend(NULL, 0)); }
2050 EOF
2051
2052         # no? damn! legacy crap ahead!
2053
2054         ac_testn __sigsuspend_s '!' __rt_sigsuspend 1 \
2055             'whether sigsuspend is usable (1/2)' <<-'EOF'
2056                 #define MKSH_INCLUDES_ONLY
2057                 #include "sh.h"
2058                 extern int __sigsuspend_s(sigset_t);
2059                 int main(void) { return (__sigsuspend_s(0)); }
2060 EOF
2061         ac_testn __sigsuspend_xxs '!' __sigsuspend_s 1 \
2062             'whether sigsuspend is usable (2/2)' <<-'EOF'
2063                 #define MKSH_INCLUDES_ONLY
2064                 #include "sh.h"
2065                 extern int __sigsuspend_xxs(int, int, sigset_t);
2066                 int main(void) { return (__sigsuspend_xxs(0, 0, 0)); }
2067 EOF
2068
2069         if test "000" = "$HAVE___RT_SIGSUSPEND$HAVE___SIGSUSPEND_S$HAVE___SIGSUSPEND_XXS"; then
2070                 # no usable sigsuspend(), use pause() *ugh*
2071                 add_cppflags -DMKSH_NO_SIGSUSPEND
2072         fi
2073 fi
2074
2075 ac_test strerror '!' sys_errlist 0 <<-'EOF'
2076         extern char *strerror(int);
2077         int main(int ac, char *av[]) { return (*strerror(*av[ac])); }
2078 EOF
2079
2080 ac_test strsignal '!' sys_siglist 0 <<-'EOF'
2081         #include <string.h>
2082         #include <signal.h>
2083         int main(void) { return (strsignal(1)[0]); }
2084 EOF
2085
2086 ac_test strlcpy <<-'EOF'
2087         #include <string.h>
2088         int main(int ac, char *av[]) { return (strlcpy(*av, av[1],
2089             (size_t)ac)); }
2090 EOF
2091
2092 #
2093 # check headers for declarations
2094 #
2095 ac_test flock_decl flock 1 'for declaration of flock()' <<-'EOF'
2096         #define MKSH_INCLUDES_ONLY
2097         #include "sh.h"
2098         #if HAVE_SYS_FILE_H
2099         #include <sys/file.h>
2100         #endif
2101         int main(void) { return ((flock)(0, 0)); }
2102 EOF
2103 ac_test revoke_decl revoke 1 'for declaration of revoke()' <<-'EOF'
2104         #define MKSH_INCLUDES_ONLY
2105         #include "sh.h"
2106         int main(void) { return ((revoke)("")); }
2107 EOF
2108 ac_test sys_errlist_decl sys_errlist 0 "for declaration of sys_errlist[] and sys_nerr" <<-'EOF'
2109         #define MKSH_INCLUDES_ONLY
2110         #include "sh.h"
2111         int main(void) { return (*sys_errlist[sys_nerr - 1] + isatty(0)); }
2112 EOF
2113 ac_test sys_siglist_decl sys_siglist 0 'for declaration of sys_siglist[]' <<-'EOF'
2114         #define MKSH_INCLUDES_ONLY
2115         #include "sh.h"
2116         int main(void) { return (sys_siglist[0][0] + isatty(0)); }
2117 EOF
2118
2119 #
2120 # other checks
2121 #
2122 fd='if to use persistent history'
2123 ac_cache PERSISTENT_HISTORY || case $HAVE_MMAP$HAVE_FLOCK$HAVE_LOCK_FCNTL in
2124 11*|101) fv=1 ;;
2125 esac
2126 test 1 = $fv || check_categories="$check_categories no-histfile"
2127 ac_testdone
2128 ac_cppflags
2129
2130 save_CFLAGS=$CFLAGS
2131 ac_testn compile_time_asserts_$$ '' 'whether compile-time assertions pass' <<-'EOF'
2132         #define MKSH_INCLUDES_ONLY
2133         #include "sh.h"
2134         #ifndef CHAR_BIT
2135         #define CHAR_BIT 8      /* defuse this test on really legacy systems */
2136         #endif
2137         struct ctasserts {
2138         #define cta(name, assertion) char name[(assertion) ? 1 : -1]
2139 /* this one should be defined by the standard */
2140 cta(char_is_1_char, (sizeof(char) == 1) && (sizeof(signed char) == 1) &&
2141     (sizeof(unsigned char) == 1));
2142 cta(char_is_8_bits, ((CHAR_BIT) == 8) && ((int)(unsigned char)0xFF == 0xFF) &&
2143     ((int)(unsigned char)0x100 == 0) && ((int)(unsigned char)(int)-1 == 0xFF));
2144 /* the next assertion is probably not really needed */
2145 cta(short_is_2_char, sizeof(short) == 2);
2146 cta(short_size_no_matter_of_signedness, sizeof(short) == sizeof(unsigned short));
2147 /* the next assertion is probably not really needed */
2148 cta(int_is_4_char, sizeof(int) == 4);
2149 cta(int_size_no_matter_of_signedness, sizeof(int) == sizeof(unsigned int));
2150
2151 cta(long_ge_int, sizeof(long) >= sizeof(int));
2152 cta(long_size_no_matter_of_signedness, sizeof(long) == sizeof(unsigned long));
2153
2154 #ifndef MKSH_LEGACY_MODE
2155 /* the next assertion is probably not really needed */
2156 cta(ari_is_4_char, sizeof(mksh_ari_t) == 4);
2157 /* but this is */
2158 cta(ari_has_31_bit, 0 < (mksh_ari_t)(((((mksh_ari_t)1 << 15) << 15) - 1) * 2 + 1));
2159 /* the next assertion is probably not really needed */
2160 cta(uari_is_4_char, sizeof(mksh_uari_t) == 4);
2161 /* but the next three are; we REQUIRE unsigned integer wraparound */
2162 cta(uari_has_31_bit, 0 < (mksh_uari_t)(((((mksh_uari_t)1 << 15) << 15) - 1) * 2 + 1));
2163 cta(uari_has_32_bit, 0 < (mksh_uari_t)(((((mksh_uari_t)1 << 15) << 15) - 1) * 4 + 3));
2164 cta(uari_wrap_32_bit,
2165     (mksh_uari_t)(((((mksh_uari_t)1 << 15) << 15) - 1) * 4 + 3) >
2166     (mksh_uari_t)(((((mksh_uari_t)1 << 15) << 15) - 1) * 4 + 4));
2167 #define NUM 22
2168 #else
2169 #define NUM 16
2170 #endif
2171 /* these are always required */
2172 cta(ari_is_signed, (mksh_ari_t)-1 < (mksh_ari_t)0);
2173 cta(uari_is_unsigned, (mksh_uari_t)-1 > (mksh_uari_t)0);
2174 /* we require these to have the precisely same size and assume 2s complement */
2175 cta(ari_size_no_matter_of_signedness, sizeof(mksh_ari_t) == sizeof(mksh_uari_t));
2176
2177 cta(sizet_size_no_matter_of_signedness, sizeof(ssize_t) == sizeof(size_t));
2178 cta(sizet_voidptr_same_size, sizeof(size_t) == sizeof(void *));
2179 cta(sizet_funcptr_same_size, sizeof(size_t) == sizeof(void (*)(void)));
2180 /* our formatting routines assume this */
2181 cta(ptr_fits_in_long, sizeof(size_t) <= sizeof(long));
2182 cta(ari_fits_in_long, sizeof(mksh_ari_t) <= sizeof(long));
2183 /* for struct alignment people */
2184                 char padding[64 - NUM];
2185         };
2186 char ctasserts_dblcheck[sizeof(struct ctasserts) == 64 ? 1 : -1];
2187         int main(void) { return (sizeof(ctasserts_dblcheck) + isatty(0)); }
2188 EOF
2189 CFLAGS=$save_CFLAGS
2190 eval test 1 = \$HAVE_COMPILE_TIME_ASSERTS_$$ || exit 1
2191
2192 #
2193 # extra checks for legacy mksh
2194 #
2195 if test $legacy = 1; then
2196         ac_test long_32bit '' 'whether long is 32 bit wide' <<-'EOF'
2197                 #define MKSH_INCLUDES_ONLY
2198                 #include "sh.h"
2199                 #ifndef CHAR_BIT
2200                 #define CHAR_BIT 0
2201                 #endif
2202                 struct ctasserts {
2203                 #define cta(name, assertion) char name[(assertion) ? 1 : -1]
2204                         cta(char_is_8_bits, (CHAR_BIT) == 8);
2205                         cta(long_is_32_bits, sizeof(long) == 4);
2206                 };
2207                 int main(void) { return (sizeof(struct ctasserts)); }
2208 EOF
2209
2210         ac_test long_64bit '!' long_32bit 0 'whether long is 64 bit wide' <<-'EOF'
2211                 #define MKSH_INCLUDES_ONLY
2212                 #include "sh.h"
2213                 #ifndef CHAR_BIT
2214                 #define CHAR_BIT 0
2215                 #endif
2216                 struct ctasserts {
2217                 #define cta(name, assertion) char name[(assertion) ? 1 : -1]
2218                         cta(char_is_8_bits, (CHAR_BIT) == 8);
2219                         cta(long_is_64_bits, sizeof(long) == 8);
2220                 };
2221                 int main(void) { return (sizeof(struct ctasserts)); }
2222 EOF
2223
2224         case $HAVE_LONG_32BIT$HAVE_LONG_64BIT in
2225         10) check_categories="$check_categories int:32" ;;
2226         01) check_categories="$check_categories int:64" ;;
2227         *) check_categories="$check_categories int:u" ;;
2228         esac
2229 fi
2230
2231 #
2232 # Compiler: Praeprocessor (only if needed)
2233 #
2234 test 0 = $HAVE_SYS_SIGNAME && if ac_testinit cpp_dd '' \
2235     'checking if the C Preprocessor supports -dD'; then
2236         echo '#define foo bar' >conftest.c
2237         vv ']' "$CPP $CFLAGS $CPPFLAGS $NOWARN -dD conftest.c >x"
2238         grep '#define foo bar' x >/dev/null 2>&1 && fv=1
2239         rmf conftest.c x vv.out
2240         ac_testdone
2241 fi
2242
2243 #
2244 # End of mirtoconf checks
2245 #
2246 $e ... done.
2247
2248 # Some operating systems have ancient versions of ed(1) writing
2249 # the character count to standard output; cope for that
2250 echo wq >x
2251 ed x <x 2>/dev/null | grep 3 >/dev/null 2>&1 && \
2252     check_categories="$check_categories $oldish_ed"
2253 rmf x vv.out
2254
2255 if test 0 = $HAVE_SYS_SIGNAME; then
2256         if test 1 = $HAVE_CPP_DD; then
2257                 $e Generating list of signal names...
2258         else
2259                 $e No list of signal names available via cpp. Falling back...
2260         fi
2261         sigseenone=:
2262         sigseentwo=:
2263         echo '#include <signal.h>
2264 #if defined(NSIG_MAX)
2265 #define cfg_NSIG NSIG_MAX
2266 #elif defined(NSIG)
2267 #define cfg_NSIG NSIG
2268 #elif defined(_NSIG)
2269 #define cfg_NSIG _NSIG
2270 #elif defined(SIGMAX)
2271 #define cfg_NSIG (SIGMAX + 1)
2272 #elif defined(_SIGMAX)
2273 #define cfg_NSIG (_SIGMAX + 1)
2274 #else
2275 /*XXX better error out, see sh.h */
2276 #define cfg_NSIG 64
2277 #endif
2278 int
2279 mksh_cfg= cfg_NSIG
2280 ;' >conftest.c
2281         # GNU sed 2.03 segfaults when optimising this to sed -n
2282         NSIG=`vq "$CPP $CFLAGS $CPPFLAGS $NOWARN conftest.c" | \
2283             grep -v '^#' | \
2284             sed '/mksh_cfg.*= *$/{
2285                 N
2286                 s/\n/ /
2287                 }' | \
2288             grep '^ *mksh_cfg *=' | \
2289             sed 's/^ *mksh_cfg *=[       ]*\([()0-9x+-][()0-9x+  -]*\).*$/\1/'`
2290         case $NSIG in
2291         *mksh_cfg*) $e "Error: NSIG='$NSIG'"; NSIG=0 ;;
2292         *[\ \(\)+-]*) NSIG=`"$AWK" "BEGIN { print $NSIG }" </dev/null` ;;
2293         esac
2294         printf=printf
2295         (printf hallo) >/dev/null 2>&1 || printf=echo
2296         test $printf = echo || test "`printf %d 42`" = 42 || printf=echo
2297         test $printf = echo || NSIG=`printf %d "$NSIG" 2>/dev/null`
2298         $printf "NSIG=$NSIG ... "
2299         sigs="ABRT FPE ILL INT SEGV TERM ALRM BUS CHLD CONT HUP KILL PIPE QUIT"
2300         sigs="$sigs STOP TSTP TTIN TTOU USR1 USR2 POLL PROF SYS TRAP URG VTALRM"
2301         sigs="$sigs XCPU XFSZ INFO WINCH EMT IO DIL LOST PWR SAK CLD IOT STKFLT"
2302         sigs="$sigs ABND DCE DUMP IOERR TRACE DANGER THCONT THSTOP RESV UNUSED"
2303         test 1 = $HAVE_CPP_DD && test $NSIG -gt 1 && sigs="$sigs "`vq \
2304             "$CPP $CFLAGS $CPPFLAGS $NOWARN -dD conftest.c" | \
2305             grep '[      ]SIG[A-Z0-9][A-Z0-9]*[  ]' | \
2306             sed 's/^.*[  ]SIG\([A-Z0-9][A-Z0-9]*\)[      ].*$/\1/' | sort`
2307         test $NSIG -gt 1 || sigs=
2308         for name in $sigs; do
2309                 case $sigseenone in
2310                 *:$name:*) continue ;;
2311                 esac
2312                 sigseenone=$sigseenone$name:
2313                 echo '#include <signal.h>' >conftest.c
2314                 echo int >>conftest.c
2315                 echo mksh_cfg= SIG$name >>conftest.c
2316                 echo ';' >>conftest.c
2317                 # GNU sed 2.03 croaks on optimising this, too
2318                 vq "$CPP $CFLAGS $CPPFLAGS $NOWARN conftest.c" | \
2319                     grep -v '^#' | \
2320                     sed '/mksh_cfg.*= *$/{
2321                         N
2322                         s/\n/ /
2323                         }' | \
2324                     grep '^ *mksh_cfg *=' | \
2325                     sed 's/^ *mksh_cfg *=[       ]*\([0-9][0-9x]*\).*$/:\1 '$name/
2326         done | sed -n '/^:[^ ]/s/^://p' | while read nr name; do
2327                 test $printf = echo || nr=`printf %d "$nr" 2>/dev/null`
2328                 test $nr -gt 0 && test $nr -lt $NSIG || continue
2329                 case $sigseentwo in
2330                 *:$nr:*) ;;
2331                 *)      echo "          { \"$name\", $nr },"
2332                         sigseentwo=$sigseentwo$nr:
2333                         $printf "$name=$nr " >&2
2334                         ;;
2335                 esac
2336         done 2>&1 >signames.inc
2337         rmf conftest.c
2338         $e done.
2339 fi
2340
2341 addsrcs '!' HAVE_STRLCPY strlcpy.c
2342 addsrcs USE_PRINTF_BUILTIN printf.c
2343 test 1 = "$USE_PRINTF_BUILTIN" && add_cppflags -DMKSH_PRINTF_BUILTIN
2344 test 1 = "$HAVE_CAN_VERB" && CFLAGS="$CFLAGS -verbose"
2345 add_cppflags -DMKSH_BUILD_R=511
2346
2347 $e $bi$me: Finished configuration testing, now producing output.$ao
2348
2349 files=
2350 objs=
2351 sp=
2352 case $tcfn in
2353 a.exe|conftest.exe)
2354         mkshexe=$tfn.exe
2355         add_cppflags -DMKSH_EXE_EXT
2356         ;;
2357 *)
2358         mkshexe=$tfn
2359         ;;
2360 esac
2361 case $curdir in
2362 *\ *)   mkshshebang="#!./$mkshexe" ;;
2363 *)      mkshshebang="#!$curdir/$mkshexe" ;;
2364 esac
2365 cat >test.sh <<-EOF
2366         $mkshshebang
2367         LC_ALL=C PATH='$PATH'; export LC_ALL PATH
2368         test -n "\$KSH_VERSION" || exit 1
2369         set -A check_categories -- $check_categories
2370         pflag='$curdir/$mkshexe'
2371         sflag='$srcdir/check.t'
2372         usee=0 Pflag=0 Sflag=0 uset=0 vflag=1 xflag=0
2373         while getopts "C:e:fPp:QSs:t:v" ch; do case \$ch {
2374         (C)     check_categories[\${#check_categories[*]}]=\$OPTARG ;;
2375         (e)     usee=1; eflag=\$OPTARG ;;
2376         (f)     check_categories[\${#check_categories[*]}]=fastbox ;;
2377         (P)     Pflag=1 ;;
2378         (+P)    Pflag=0 ;;
2379         (p)     pflag=\$OPTARG ;;
2380         (Q)     vflag=0 ;;
2381         (+Q)    vflag=1 ;;
2382         (S)     Sflag=1 ;;
2383         (+S)    Sflag=0 ;;
2384         (s)     sflag=\$OPTARG ;;
2385         (t)     uset=1; tflag=\$OPTARG ;;
2386         (v)     vflag=1 ;;
2387         (+v)    vflag=0 ;;
2388         (*)     xflag=1 ;;
2389         }
2390         done
2391         shift \$((OPTIND - 1))
2392         set -A args -- '$srcdir/check.pl' -p "\$pflag"
2393         x=
2394         for y in "\${check_categories[@]}"; do
2395                 x=\$x,\$y
2396         done
2397         if [[ -n \$x ]]; then
2398                 args[\${#args[*]}]=-C
2399                 args[\${#args[*]}]=\${x#,}
2400         fi
2401         if (( usee )); then
2402                 args[\${#args[*]}]=-e
2403                 args[\${#args[*]}]=\$eflag
2404         fi
2405         (( Pflag )) && args[\${#args[*]}]=-P
2406         if (( uset )); then
2407                 args[\${#args[*]}]=-t
2408                 args[\${#args[*]}]=\$tflag
2409         fi
2410         (( vflag )) && args[\${#args[*]}]=-v
2411         (( xflag )) && args[\${#args[*]}]=-x    # force usage by synerr
2412         if [[ -n \$TMPDIR && -d \$TMPDIR/. ]]; then
2413                 args[\${#args[*]}]=-T
2414                 args[\${#args[*]}]=\$TMPDIR
2415         fi
2416         print Testing mksh for conformance:
2417         fgrep -e MirOS: -e MIRBSD "\$sflag"
2418         print "This shell is actually:\\n\\t\$KSH_VERSION"
2419         print 'test.sh built for mksh $dstversion'
2420         cstr='\$os = defined \$^O ? \$^O : "unknown";'
2421         cstr="\$cstr"'print \$os . ", Perl version " . \$];'
2422         for perli in \$PERL perl5 perl no; do
2423                 if [[ \$perli = no ]]; then
2424                         print Cannot find a working Perl interpreter, aborting.
2425                         exit 1
2426                 fi
2427                 print "Trying Perl interpreter '\$perli'..."
2428                 perlos=\$(\$perli -e "\$cstr")
2429                 rv=\$?
2430                 print "Errorlevel \$rv, running on '\$perlos'"
2431                 if (( rv )); then
2432                         print "=> not using"
2433                         continue
2434                 fi
2435                 if [[ -n \$perlos ]]; then
2436                         print "=> using it"
2437                         break
2438                 fi
2439         done
2440         (( Sflag )) || echo + \$perli "\${args[@]}" -s "\$sflag" "\$@"
2441         (( Sflag )) || exec \$perli "\${args[@]}" -s "\$sflag" "\$@"$tsts
2442         # use of the -S option for check.t split into multiple chunks
2443         rv=0
2444         for s in "\$sflag".*; do
2445                 echo + \$perli "\${args[@]}" -s "\$s" "\$@"
2446                 \$perli "\${args[@]}" -s "\$s" "\$@"$tsts
2447                 rc=\$?
2448                 (( rv = rv ? rv : rc ))
2449         done
2450         exit \$rv
2451 EOF
2452 chmod 755 test.sh
2453 case $cm in
2454 dragonegg)
2455         emitbc="-S -flto"
2456         ;;
2457 llvm)
2458         emitbc="-emit-llvm -c"
2459         ;;
2460 *)
2461         emitbc=-c
2462         ;;
2463 esac
2464 echo ": # work around NeXTstep bug" >Rebuild.sh
2465 cd "$srcdir"
2466 optfiles=`echo *.opt`
2467 cd "$curdir"
2468 for file in $optfiles; do
2469         echo "echo + Running genopt on '$file'..."
2470         echo "(srcfile='$srcdir/$file'; BUILDSH_RUN_GENOPT=1; . '$srcdir/Build.sh')"
2471 done >>Rebuild.sh
2472 echo set -x >>Rebuild.sh
2473 for file in $SRCS; do
2474         op=`echo x"$file" | sed 's/^x\(.*\)\.c$/\1./'`
2475         test -f $file || file=$srcdir/$file
2476         files="$files$sp$file"
2477         sp=' '
2478         echo "$CC $CFLAGS $CPPFLAGS $emitbc $file || exit 1" >>Rebuild.sh
2479         if test $cm = dragonegg; then
2480                 echo "mv ${op}s ${op}ll" >>Rebuild.sh
2481                 echo "llvm-as ${op}ll || exit 1" >>Rebuild.sh
2482                 objs="$objs$sp${op}bc"
2483         else
2484                 objs="$objs$sp${op}o"
2485         fi
2486 done
2487 case $cm in
2488 dragonegg|llvm)
2489         echo "rm -f $tfn.s" >>Rebuild.sh
2490         echo "llvm-link -o - $objs | opt $optflags | llc -o $tfn.s" >>Rebuild.sh
2491         lobjs=$tfn.s
2492         ;;
2493 *)
2494         lobjs=$objs
2495         ;;
2496 esac
2497 echo tcfn=$mkshexe >>Rebuild.sh
2498 echo "$CC $CFLAGS $LDFLAGS -o \$tcfn $lobjs $LIBS $ccpr" >>Rebuild.sh
2499 echo "test -f \$tcfn || exit 1; $SIZE \$tcfn" >>Rebuild.sh
2500 if test $cm = makefile; then
2501         extras='emacsfn.h rlimits.opt sh.h sh_flags.opt var_spec.h'
2502         test 0 = $HAVE_SYS_SIGNAME && extras="$extras signames.inc"
2503         gens= genq=
2504         for file in $optfiles; do
2505                 genf=`basename "$file" | sed 's/.opt$/.gen/'`
2506                 gens="$gens $genf"
2507                 genq="$genq$nl$genf: $srcdir/Build.sh $srcdir/$file
2508                         srcfile=$srcdir/$file; BUILDSH_RUN_GENOPT=1; . $srcdir/Build.sh"
2509         done
2510         cat >Makefrag.inc <<EOF
2511 # Makefile fragment for building mksh $dstversion
2512
2513 PROG=           $mkshexe
2514 MAN=            mksh.1
2515 SRCS=           $SRCS
2516 SRCS_FP=        $files
2517 OBJS_BP=        $objs
2518 INDSRCS=        $extras
2519 NONSRCS_INST=   dot.mkshrc \$(MAN)
2520 NONSRCS_NOINST= Build.sh Makefile Rebuild.sh check.pl check.t test.sh
2521 CC=             $CC
2522 CFLAGS=         $CFLAGS
2523 CPPFLAGS=       $CPPFLAGS
2524 LDFLAGS=        $LDFLAGS
2525 LIBS=           $LIBS
2526
2527 .depend \$(OBJS_BP):$gens$genq
2528
2529 # not BSD make only:
2530 #VPATH=         $srcdir
2531 #all: \$(PROG)
2532 #\$(PROG): \$(OBJS_BP)
2533 #       \$(CC) \$(CFLAGS) \$(LDFLAGS) -o \$@ \$(OBJS_BP) \$(LIBS)
2534 #\$(OBJS_BP): \$(SRCS_FP) \$(NONSRCS)
2535 #.c.o:
2536 #       \$(CC) \$(CFLAGS) \$(CPPFLAGS) -c \$<
2537
2538 # for all make variants:
2539 #REGRESS_FLAGS= -f
2540 #regress:
2541 #       ./test.sh \$(REGRESS_FLAGS)
2542 check_categories=$check_categories
2543
2544 # for BSD make only:
2545 #.PATH: $srcdir
2546 #.include <bsd.prog.mk>
2547 EOF
2548         $e
2549         $e Generated Makefrag.inc successfully.
2550         exit 0
2551 fi
2552 for file in $optfiles; do
2553         $e "+ Running genopt on '$file'..."
2554         do_genopt "$srcdir/$file" || exit 1
2555 done
2556 if test $cm = combine; then
2557         objs="-o $mkshexe"
2558         for file in $SRCS; do
2559                 test -f $file || file=$srcdir/$file
2560                 objs="$objs $file"
2561         done
2562         emitbc="-fwhole-program --combine"
2563         v "$CC $CFLAGS $CPPFLAGS $LDFLAGS $emitbc $objs $LIBS $ccpr"
2564 elif test 1 = $pm; then
2565         for file in $SRCS; do
2566                 test -f $file || file=$srcdir/$file
2567                 v "$CC $CFLAGS $CPPFLAGS $emitbc $file" &
2568         done
2569         wait
2570 else
2571         for file in $SRCS; do
2572                 test $cm = dragonegg && \
2573                     op=`echo x"$file" | sed 's/^x\(.*\)\.c$/\1./'`
2574                 test -f $file || file=$srcdir/$file
2575                 v "$CC $CFLAGS $CPPFLAGS $emitbc $file" || exit 1
2576                 if test $cm = dragonegg; then
2577                         v "mv ${op}s ${op}ll"
2578                         v "llvm-as ${op}ll" || exit 1
2579                 fi
2580         done
2581 fi
2582 case $cm in
2583 dragonegg|llvm)
2584         rmf $tfn.s
2585         v "llvm-link -o - $objs | opt $optflags | llc -o $tfn.s"
2586         ;;
2587 esac
2588 tcfn=$mkshexe
2589 test $cm = combine || v "$CC $CFLAGS $LDFLAGS -o $tcfn $lobjs $LIBS $ccpr"
2590 test -f $tcfn || exit 1
2591 test 1 = $r || v "$NROFF -mdoc <'$srcdir/mksh.1' >$tfn.cat1" || \
2592     rmf $tfn.cat1
2593 test 0 = $eq && v $SIZE $tcfn
2594 i=install
2595 test -f /usr/ucb/$i && i=/usr/ucb/$i
2596 test 1 = $eq && e=:
2597 $e
2598 $e Installing the shell:
2599 $e "# $i -c -s -o root -g bin -m 555 $tfn /bin/$tfn"
2600 if test $legacy = 0; then
2601         $e "# grep -x /bin/$tfn /etc/shells >/dev/null || echo /bin/$tfn >>/etc/shells"
2602         $e "# $i -c -o root -g bin -m 444 dot.mkshrc /usr/share/doc/mksh/examples/"
2603 fi
2604 $e
2605 $e Installing the manual:
2606 if test -f $tfn.cat1; then
2607         $e "# $i -c -o root -g bin -m 444 $tfn.cat1" \
2608             "/usr/share/man/cat1/$tfn.0"
2609         $e or
2610 fi
2611 $e "# $i -c -o root -g bin -m 444 $tfn.1 /usr/share/man/man1/$tfn.1"
2612 $e
2613 $e Run the regression test suite: ./test.sh
2614 $e Please also read the sample file dot.mkshrc and the fine manual.
2615 exit 0
2616
2617 : <<'EOD'
2618
2619 === Environment used ===
2620
2621 ==== build environment ====
2622 AWK                             default: awk
2623 CC                              default: cc
2624 CFLAGS                          if empty, defaults to -xO2 or +O2
2625                                 or -O3 -qstrict or -O2, per compiler
2626 CPPFLAGS                        default empty
2627 LDFLAGS                         default empty; added before sources
2628 LDSTATIC                        set this to '-static'; default unset
2629 LIBS                            default empty; added after sources
2630                                 [Interix] default: -lcrypt (XXX still needed?)
2631 NOWARN                          -Wno-error or similar
2632 NROFF                           default: nroff
2633 TARGET_OS                       default: $(uname -s || uname)
2634 TARGET_OSREV                    [QNX] default: $(uname -r)
2635
2636 ==== feature selectors ====
2637 USE_PRINTF_BUILTIN              1 to include (unsupported) printf(1) as builtin
2638 ===== general format =====
2639 HAVE_STRLEN                     ac_test
2640 HAVE_STRING_H                   ac_header
2641 HAVE_CAN_FSTACKPROTECTORALL     ac_flags
2642
2643 ==== cpp definitions ====
2644 DEBUG                           dont use in production, wants gcc, implies:
2645 DEBUG_LEAKS                     enable freeing resources before exiting
2646 MKSHRC_PATH                     "~/.mkshrc" (do not change)
2647 MKSH_A4PB                       force use of arc4random_pushb
2648 MKSH_ASSUME_UTF8                (0=disabled, 1=enabled; default: unset)
2649 MKSH_BINSHPOSIX                 if */sh or */-sh, enable set -o posix
2650 MKSH_BINSHREDUCED               if */sh or */-sh, enable set -o sh
2651 MKSH_CLRTOEOL_STRING            "\033[K"
2652 MKSH_CLS_STRING                 "\033[;H\033[J"
2653 MKSH_CONSERVATIVE_FDS           fd 0-9 for scripts, shell only up to 31
2654 MKSH_DEFAULT_EXECSHELL          "/bin/sh" (do not change)
2655 MKSH_DEFAULT_PROFILEDIR         "/etc" (do not change)
2656 MKSH_DEFAULT_TMPDIR             "/tmp" (do not change)
2657 MKSH_DISABLE_DEPRECATED         disable code paths scheduled for later removal
2658 MKSH_DISABLE_EXPERIMENTAL       disable code not yet comfy for (LTS) snapshots
2659 MKSH_DISABLE_TTY_WARNING        shut up warning about ctty if OS cant be fixed
2660 MKSH_DONT_EMIT_IDSTRING         omit RCS IDs from binary
2661 MKSH_MIDNIGHTBSD01ASH_COMPAT    set -o sh: additional compatibility quirk
2662 MKSH_NOPROSPECTOFWORK           disable jobs, co-processes, etc. (do not use)
2663 MKSH_NOPWNAM                    skip PAM calls, for -static on glibc or Solaris
2664 MKSH_NO_CMDLINE_EDITING         disable command line editing code entirely
2665 MKSH_NO_DEPRECATED_WARNING      omit warning when deprecated stuff is run
2666 MKSH_NO_LIMITS                  omit ulimit code
2667 MKSH_NO_SIGSETJMP               define if sigsetjmp is broken or not available
2668 MKSH_NO_SIGSUSPEND              use sigprocmask+pause instead of sigsuspend
2669 MKSH_SMALL                      omit some code, optimise hard for size (slower)
2670 MKSH_SMALL_BUT_FAST             disable some hard-for-size optim. (modern sys.)
2671 MKSH_S_NOVI=1                   disable Vi editing mode (default if MKSH_SMALL)
2672 MKSH_TYPEDEF_SIG_ATOMIC_T       define to e.g. 'int' if sig_atomic_t is missing
2673 MKSH_TYPEDEF_SSIZE_T            define to e.g. 'long' if your OS has no ssize_t
2674 MKSH_UNEMPLOYED                 disable job control (but not jobs/co-processes)
2675
2676 === generic installation instructions ===
2677
2678 Set CC and possibly CFLAGS, CPPFLAGS, LDFLAGS, LIBS. If cross-compiling,
2679 also set TARGET_OS. To disable tests, set e.g. HAVE_STRLCPY=0; to enable
2680 them, set to a value other than 0 or 1. Ensure /bin/ed is installed. For
2681 MKSH_SMALL but with Vi mode, add -DMKSH_S_NOVI=0 to CPPFLAGS as well.
2682
2683 Normally, the following command is what you want to run, then:
2684 $ (sh Build.sh -r -c lto && ./test.sh -f) 2>&1 | tee log
2685
2686 Copy dot.mkshrc to /etc/skel/.mkshrc; install mksh into $prefix/bin; or
2687 /bin; install the manpage, if omitting the -r flag a catmanpage is made
2688 using $NROFF. Consider using a forward script as /etc/skel/.mkshrc like
2689 http://anonscm.debian.org/cgit/collab-maint/mksh.git/plain/debian/.mkshrc
2690 and put dot.mkshrc as /etc/mkshrc so users need not keep up their HOME.
2691
2692 You may also want to install the lksh binary (also as /bin/sh) built by:
2693 $ CPPFLAGS="$CPPFLAGS -DMKSH_BINSHPOSIX" sh Build.sh -L -r -c lto
2694
2695 EOD