1 : $XConsortium: shipiffe.sh /main/3 1996/05/10 16:41:50 drk $
3 # Glenn Fowler & Phong Vo
4 # AT&T Bell Laboratories
6 # test if feature exists
8 # @(#)iffe (AT&T Bell Laboratories) 05/09/95
10 # NOTE: .exe a.out suffix and [\\/] in path patterns for dos/nt
15 binding="-Bdynamic -Bstatic -Wl,-ashared -Wl,-aarchive -call_shared -non_shared"
37 $executable . 2>/dev/null || executable='test -r'
47 ""|?|??|???|????|????)
51 ksh) tmp=${HOSTNAME%${HOSTNAME#????}} ;;
52 *) eval `echo $HOSTNAME | sed 's/\(....\).*/tmp=\1/'` ;;
64 ksh) tmp=${tmp%${tmp#?????????}} ;;
65 *) eval `echo $tmp | sed 's/\(.........\).*/tmp=\1/'` ;;
76 # standard error to /dev/null unless debugging
77 # standard output to the current output file
79 # stdout original standard output
80 # stderr original standard error
81 # nullin /dev/null input
82 # nullout /dev/null output
84 stdin=4 stdout=5 stderr=6 nullin=7 nullout=8
85 eval "exec $stdin<&0 $nullin>/dev/null $nullout>/dev/null $stdout>&1 $stderr>&2 2>&$nullout"
87 # options -- `-' for output to stdout otherwise usage
98 -*) echo "Usage: $command [-] [op [arg ... [-l* *.a *.sh * ...]] [: op ...]" >&$stderr
107 # tmp files cleaned up on exit
108 # status: 0:success 1:failure 2:interrupt
111 trap "rm -f $tmp*.*; exit \$status" 0 1 2
113 # standard header for c source
115 std='#if defined(__STDC__) || defined(__cplusplus) || defined(c_plusplus)
124 #if defined(__cplusplus)
125 #define _BEGIN_EXTERNS_ extern "C" {
126 #define _END_EXTERNS_ }
128 #define _BEGIN_EXTERNS_
129 #define _END_EXTERNS_
131 #define _NIL_(x) ((x)0)'
133 # loop on op [ arg [ ... ] ] [ : op [ arg [ ... ] ] ]
142 *) read lin || lin="set out +"
148 # check if "run xxx" is equivalent to "set in xxx"
155 */*) x=`echo $2 | sed -e 's,.*[\\\\/],,'` ;;
167 # set drops out early
210 export `echo $x | sed -e 's/=.*//'`
228 ""|1) exec 2>&$stderr
233 ksh) PS4="${PS4%+*([ ])}+\$LINENO+ "
237 *) echo "$command: $arg: debug levels are 0, 1, 2" >&$stderr
247 then echo "$command: $in: not found" >&$stderr
255 ksh) out=${in##*[\\/]}
258 *) eval `echo $in | sed -e 's,.*[\\\\/],,' -e 's/\.[^.]*//' -e 's/^/out=/'`
271 nodebug)exec 2>&$nullout
285 prototyped|noprototyped)
288 pragma) pragma="$pragma $arg"
296 *) echo "$command: $op: unkown option" >&$stderr
351 menu\{) sep=" $nl " ;;
357 "") echo "$command: missing }end" >&$stderr
370 else echo "$command: missing }end" >&$stderr
387 nofail\{) pass=$x v=pass\{ ;;
388 nopass\{) fail=$x v=fail\{ ;;
391 prompt\{) prompt=$x ;;
403 hdr) hdr="$hdr $1" ;;
404 lib) lib="$lib $1" ;;
411 ref) cc="$cc $1" occ="$occ $1" ;;
412 run) opt="$opt $1" ;;
433 # check for interactive queries
447 echo "$prompt" >&$stdout
458 $nl) shift; continue ;;
489 echo " $p)$t" >&$stdout
490 eval p$n='"$p"' v$n='"$v"'
498 ksh) read -u$stdin val ;;
499 *) val=`exec <&$stdin; read val; echo val` ;;
511 eval p='$'p$i v='$'v$i
527 *) ( eval "$test" ) <&$stdin >&$stdout 2>&$stderr && break ;;
531 echo "$command: invalid input" >&$stderr
539 # check the candidate headers
554 *.h) case " $gothdr " in
560 *) echo "#include <$x>" > $tmp.c
561 if $cc -E $tmp.c <&$nullin >&$nullout
562 then gothdr="$gothdr + $x"
563 else gothdr="$gothdr - $x"
569 *) test -r $x || continue
581 # check the candidate libraries
587 echo "main(){return(0);}" > $tmp.c
610 if $cc -o $tmp.exe $tmp.c $p <&$nullin >&$nullout
611 then gotlib="$gotlib + $p"
615 else gotlib="$gotlib - $p"
631 # precheck some more ops
634 ref) deflib="$deflib $lib"
635 defhdr="$defhdr $hdr"
644 # check for op aliases
649 def|default) x="$x cmd dat hdr key lib mth sys typ" ;;
654 # loop on the ops o and args a
671 *) eval `echo $a | sed -e "s/\\(.*\\)\\\\${x}\\(.*\\)/p=\\1 v=\\2/"`
687 *) eval `echo $a | sed -e 's,.*[\\\\/],,' -e 's/\(.*\)\.\(.*\)/p=\1 v=\2/'`
711 # check output redirection
728 *[\\/]*|*.h) x=$cur ;;
733 sed 's,/\*[^/]*\*/, ,g' $tmp.h > $tmp.c
734 sed 's,/\*[^/]*\*/, ,g' $x > $tmp.t
736 *) sed '/^#/d' $tmp.h > $tmp.c
737 sed '/^#/d' $x > $tmp.t
740 if cmp -s $tmp.c $tmp.t
743 ${dir}[\\/]$cur) test -d $dir || mkdir $dir || exit 1 ;;
756 -) eval "exec >&$stdout"
763 *[\\/]*|???????????????*) cur=$v ;;
780 C) echo "/* : : generated$x by $command version $version : : */"
801 *) eval `echo $in | sed -e 's,\.,_,g' -e 's/^/x=/'`
811 *) x=`pwd | sed -e 's,.*[\\\\/],,' -e 's,\..*,,' -e 's,^lib,,' -e 's,^,'${x}_',' -e 's,[^a-zA-Z0-9_],_,g'`
812 echo "#ifndef _def${x}"
813 echo "#define _def${x} 1"
817 *) echo "# : : generated$x by $command version $version : : #"
825 # set up the candidate include list
828 for x in $defhdr $hdr
834 */*) c=${x%%[\\/]*}_${c} ;;
837 *) eval `echo $x | sed -e 's,^\([^\\\\/]*\).*[\\\\/]\([^\\\\/]*\)\$,\1_\2,' -e 's/\..*//' -e 's/^/c=/'`
847 *) puthdr="$puthdr $c"
848 usr="$usr$nl#define $c 1"
849 echo "#define $c 1 /* #include <$x> ok */"
858 # set up the candidate lib list
860 for x in $lib $deflib
861 do case " $putlib " in
864 *) putlib="$putlib $x"
865 echo "/* candidate library $x found */"
870 # src overrides builtin test
890 (eval "$src") <&$nullin || e=1
893 if $cc -E $tmp.c <&$nullin >$tmp.i
894 then sed -e '/<<[ ]*".*"[ ]*>>/!d' -e 's/<<[ ]*"//g' -e 's/"[ ]*>>//g' $tmp.i
898 p*|nop*)$cc -DTEST=$p -DID=$v -E $tmp.c <&$nullin >&$nullout || e=1
900 c*|noc*)$cc -DTEST=$p -DID=$v -c $tmp.c <&$nullin >&$nullout || e=1
903 if $cc -DTEST=$p -DID=$v -o $tmp.exe $tmp.c $lib $deflib <&$nullin >&$nullout && $executable $tmp.exe
906 o*|noo*)$tmp.exe <&$nullin || e=1 ;;
907 *) $tmp.exe <&$nullin >&$nullout || e=1 ;;
923 *) usr="$usr$nl#define $m 1"
925 "") echo "#define $m 1 /* ${run}} passed */" ;;
926 *) echo "#define $m 1 /*"$note" */" ;;
930 (eval "$pass") <&$nullin
932 *) (eval "$fail") <&$nullin
939 # initialize common builtin state
944 "") statictest=FoobaR
948 extern int $statictest;
950 main(){char* i = (char*)&$statictest; return i!=0;}" > $tmp.c
952 if $cc -o $tmp.exe $tmp.c <&$nullin >&$nullout && $executable $tmp.exe
955 echo '#include <stdio.h>
956 main(){printf("hello");return(0);}' > $tmp.c
958 if $cc -c $tmp.c <&$nullin >&$nullout && $cc -o $tmp.exe $tmp.o <&$nullin >&$nullout 2>$tmp.e && $executable $tmp.exe
959 then e=`wc -l $tmp.e`
968 $cc -o $tmp.exe $1 $tmp.o <&$nullin >&$nullout 2>$tmp.e && $executable $tmp.exe || continue
969 case `wc -l $tmp.e` in
975 $cc -o $tmp.exe $2 $tmp.o <&$nullin >&$nullout 2>$tmp.e && $executable $tmp.exe || continue
976 case `wc -l $tmp.e` in
980 case `ls -s $tmp.exe` in
1011 for i in bin etc ucb
1012 do if test -f $d/$i/$a
1015 usr="$usr$nl#define $m 1"
1016 echo "#define $m 1 /* $a in ?(/usr)/(bin|etc|ucb) */"
1020 usr="$usr$nl#define $c 1"
1021 echo "#define $c 1 /* $d/$i/$a found */"
1036 *) echo "_BEGIN_EXTERNS_
1041 echo "main(){char* i = (char*)&$v; return i!=0;}"
1043 $cc -c $tmp.c <&$nullin >&$nullout &&
1045 if $cc $static -o $tmp.exe $tmp.o $lib $deflib <&$nullin >&$nullout && $executable $tmp.exe
1046 then usr="$usr$nl#define $m 1"
1047 echo "#define $m 1 /* $v in default lib(s) */"
1055 lcl) eval p='$'_lcl_$v
1063 *) case " $puthdr " in
1075 *) echo "#include <$x>" > $tmp.c
1076 if $cc -E $tmp.c <&$nullin >&$nullout
1077 then gothdr="$gothdr + $x"
1078 usr="$usr$nl#define $m 1"
1079 echo "#define $m 1 /* #include <$x> ok */"
1080 else gothdr="$gothdr - $x"
1100 #include <$f.h>" > $tmp.c
1103 *) e='-e /[\\\\/]sys[\\\\/]'$x'\.h"/d' ;;
1105 if $cc -E $tmp.c <&$nullin >$tmp.i
1106 then i=`sed -e '/^#[line ]*[0-9][0-9]*[ ][ ]*"[\\\\/].*[\\\\/]'$x'\.h"/!d' $e -e '/\/bits\//d' -e s'/.*"\(.*\)".*/\1/' -e 's,\\\\,/,g' -e 's,///*,/,g' $tmp.i | sort -u`
1113 [\\/]*) echo "#if defined(__STDPP__directive)"
1114 echo "__STDPP__directive pragma pp:hosted"
1116 echo "#include \"$i\" /* untrusted local <$f.h> */"
1118 usr="$usr$nl#define $m 1"
1122 *) echo "/* no local <$f.h> */"
1131 int f(){int $v = 1;return($v);}" > $tmp.c
1132 $cc -c $tmp.c <&$nullin >&$nullout ||
1134 usr="$usr$nl#define $m 1"
1135 echo "#define $m 1 /* $v is a reserved keyword */"
1148 static int ((*i)())=$v;main(){return(i==0);}" > $tmp.c
1149 $cc -c $tmp.c <&$nullin >&$nullout &&
1151 if $cc $static -o $tmp.exe $tmp.o $lib $deflib <&$nullin >&$nullout && $executable $tmp.exe
1153 lib) usr="$usr$nl#define $m 1"
1154 echo "#define $m 1 /* $v() in default lib(s) */" ;;
1158 $cc $static -o $tmp.exe $tmp.o -lm <&$nullin >&$nullout &&
1159 $executable $tmp.exe &&
1161 usr="$usr$nl#define $m 1"
1162 echo "#define $m 1 /* $v() in math lib */"
1174 '#define $m 1 /* $v is a macro */'
1176 $cc -E $tmp.c <&$nullin | sed -e "/#define/!d" -e "s/'//g" -e "s/^[ ][ ]*//"
1179 ?*) for i in "" "struct "
1183 int n = sizeof(i.$v);" > $tmp.c
1184 if $cc -c $tmp.c <&$nullin >&$nullout
1185 then usr="$usr$nl#define $m 1"
1186 echo "#define $m 1 /* $v is member of $i$p */"
1191 *) echo "$command: $o: <struct>.<member> expected" >&$stderr
1197 one) for i in $a $hdr
1198 do x="#include <$i>"
1205 *) echo "$x" > $tmp.c
1206 if $cc -E $tmp.c <&$nullin >&$nullout
1207 then gothdr="$gothdr + $x"
1208 else gothdr="$gothdr - $x"
1218 *) echo "$one" > $tmp.c
1219 if $cc -E $tmp.c <&$nullin >$tmp.i
1222 *[\\/]*) c=`echo $c | sed 's,[\\\\/],[/],g'` ;;
1224 case `sed -e '/^#[line ]*1[ ][ ]*"[\\\\/].*[\\\\/]'$c'"/!d' $tmp.i` in
1236 run) if test ! -r $a
1237 then echo "$command: $a: not found" >&$stderr
1243 $cc -o $tmp.exe $tmp.c $lib $deflib <&$nullin >&$stderr 2>&$stderr &&
1244 $executable $tmp.exe &&
1245 $tmp.exe $opt <&$nullin
1248 set "cc='$cc' executable='$executable' id='$m' static='$static' tmp='$tmp'" $opt $hdr $test
1257 *) echo "$command: $a: cannot run" >&$stderr
1268 '=' $x '='" > $tmp.c
1269 $cc -E $tmp.c <&$nullin \
1274 -e 's/((([^()]*)))->/->/g' \
1275 -e 's/(([^()]*))->/->/g' \
1276 -e 's/([^()]*)->/->/g' \
1277 -e 's/\([a-zA-Z_][a-zA-Z_0-9]*\)\[/\
1279 -e 's/\([a-zA-Z_][a-zA-Z_0-9]*\)(/\
1281 -e 's/\*->\([a-zA-Z_]\)/->\
1283 -e 's/->\([a-zA-Z_]\)/->\
1286 -e 's/^[a-zA-Z_][a-zA-Z_0-9]*$/\
1293 -e 's/\(...\) \([a-zA-Z_][a-zA-Z_0-9]*\).*/#ifndef _\1_'$v'\
1294 #define _\1_'$v' \2\
1295 #define _\1_'$v'_str "\2"\
1304 #include <sys/types.h>"
1307 for i in time "" times
1323 *) echo "$t" > $tmp.c
1324 if $cc -c $tmp.c <&$nullin >&$nullout
1325 then gothdr="$gothdr + $f"
1326 else gothdr="$gothdr - $f"
1336 *) puthdr="$puthdr $c"
1337 usr="$usr$nl#define $c 1"
1338 echo "#define $c 1 /* #include <$f> ok */"
1343 for i in stddef stdlib
1353 *) echo "$u$typ" > $tmp.c
1354 if $cc -c $tmp.c <&$nullin >&$nullout
1355 then gothdr="$gothdr + $f"
1356 else gothdr="$gothdr - $f"
1366 *) puthdr="$puthdr $c"
1367 usr="$usr$nl#define $c 1"
1368 echo "#define $c 1 /* #include <$f> ok */"
1382 echo "v <<= 4; i = v >> 2; i = v * i; i = i / v; v = v + i; i = i - v; i = 10; i = v % i; i |= v; v ^= i; i = 123; v &= i;
1387 struct xxx { $x$v mem; };
1388 static struct xxx v;
1389 struct xxx* f() { return &v; }"
1392 echo "main() { f(); return 0; }"
1395 if $cc -o $tmp.exe $tmp.c $lib $deflib <&$nullin >&$nullout &&
1396 $executable $tmp.exe
1397 then usr="$usr$nl#define $m 1"
1398 echo "#define $m 1 /* $x$v is a type */"
1404 \"*\") echo $arg=\'$val\' ;;
1405 *) echo $arg=\"$val\" ;;
1410 *) echo "$command: $o: unknown feature test" >&$stderr
1415 ?*) (eval "$user") <&$nullin ;;