dtksh/sfio: resolved a naming collision on FreeBSD 11
[oweals/cde.git] / cde / programs / dtksh / ksh93 / ship / shipfeature.sh
1 # $XConsortium: shipfeature.sh /main/2 1995/07/19 17:09:46 drk $
2 :
3 # Glenn Fowler & Phong Vo
4 # AT&T Bell Laboratories
5 #
6 # C feature test support
7 # @(#)feature (AT&T Bell Laboratories) 03/01/94
8 #
9 # NOTE: .exe a.out suffix and [\\/] in path patterns for dos/nt
10
11 command=feature
12 version=03/01/94
13
14 binding="-Bdynamic -Bstatic -Wl,-ashared -Wl,-aarchive -call_shared -non_shared"
15 occ=cc
16 debug=
17 defhdr=
18 deflib=
19 executable='test -x'
20 gothdr=
21 gotlib=
22 dir=FEATURE
23 in=
24 out=
25 pragma=
26 case $RANDOM in
27 $RANDOM)shell=bsh
28         $executable . 2>/dev/null || executable='test -r'
29         ;;
30 *)      shell=ksh
31         ;;
32 esac
33 static=.
34 statictest=
35 case $COTEMP in
36 "")     case $HOSTNAME in
37         ""|?|??|???|????|????)
38                 tmp=${HOSTNAME}
39                 ;;
40         *)      case $shell in
41                 ksh)    tmp=${HOSTNAME%${HOSTNAME#????}} ;;
42                 *)      eval `echo $HOSTNAME | sed 's/\(....\).*/tmp=\1/'` ;;
43                 esac
44                 ;;
45         esac
46         tmp=${tmp}$$
47         ;;
48 *)      tmp=$COTEMP
49         ;;
50 esac
51 case $tmp in
52 ??????????*)
53         case $shell in
54         ksh)    tmp=${tmp%${tmp#?????????}} ;;
55         *)      eval `echo $tmp | sed 's/\(.........\).*/tmp=\1/'` ;;
56         esac
57         ;;
58 ?????????)
59         ;;
60 ????????)
61         tmp=F$tmp
62         ;;
63 esac
64 tmp=./$tmp
65
66 # standard error to /dev/null unless debugging
67 # standard output to the current output file
68 #
69 #       stdout  original standard output
70 #       stderr  original standard error
71 #       nullin  /dev/null input
72 #       nullout /dev/null output
73
74 stdout=5 stderr=6 nullin=7 nullout=8
75 eval "exec $nullin>/dev/null $nullout>/dev/null $stdout>&1 $stderr>&2 2>&$nullout"
76
77 # options -- `-' for output to stdout otherwise usage
78
79 while   :
80 do      case $# in
81         0)      in=-
82                 break
83                 ;;
84         esac
85         case $1 in
86         -)      out=-
87                 ;;
88         -*)     echo "Usage: $command [-] [op [arg ... [-l* *.a *.sh * ...]] [: op ...]" >&$stderr
89                 exit 2
90                 ;;
91         *)      break
92                 ;;
93         esac
94         shift
95 done
96
97 # tmp files cleaned up on exit
98 # status: 0:success 1:failure 2:interrupt
99
100 status=2
101 trap "rm -f $tmp*.*; exit \$status" 0 1 2
102
103 # loop on op [ arg [ ... ] ] [ : op [ arg [ ... ] ] ]
104
105 cur=.
106 while   :
107 do      case $in in
108         "")     case $# in
109                 0)      set set out + ;;
110                 esac
111                 ;;
112         *)      read lin || lin="set out +"
113                 set x $lin
114                 shift
115                 ;;
116         esac
117
118         # check if "run xxx" is equivalent to "set in xxx"
119
120         case $1 in
121         run)    case $shell in
122                 ksh)    x=${2##*[\\/]}
123                         ;;
124                 *)      case $2 in
125                         */*)    x=`echo $2 | sed -e 's,.*[\\\\/],,'` ;;
126                         *)      x=$2 ;;
127                         esac
128                         ;;
129                 esac
130                 case $x in
131                 *.*)    ;;
132                 *)      set set in $2 ;;
133                 esac
134                 ;;
135         esac
136
137         # set drops out early
138
139         case $1 in
140         ""|\#*) continue
141                 ;;
142         set)    shift
143                 case $1 in
144                 ""|\#*) op=
145                         ;;
146                 *)      op=$1
147                         shift
148                         arg=
149                         while   :
150                         do      case $# in
151                                 0)      break ;;
152                                 esac
153                                 case $1 in
154                                 *" "*)  shift
155                                         continue
156                                         ;;
157                                 ""|\#*) break
158                                         ;;
159                                 :)      shift
160                                         break
161                                         ;;
162                                 esac
163                                 case $arg in
164                                 "")     arg=$1 ;;
165                                 *)      arg="$arg $1" ;;
166                                 esac
167                                 shift
168                         done
169                         ;;
170                 esac
171                 case $op in
172                 cc)     occ=
173                         for x in $arg
174                         do      case $occ in
175                                 "")     case $x in
176                                         *=*)    case $shell in
177                                                 ksh)    export $x
178                                                         ;;
179                                                 *)      eval $x
180                                                         export `echo $x | sed -e 's/=.*//'`
181                                                         ;;
182                                                 esac
183                                                 ;;
184                                         *)      occ=$x
185                                                 ;;
186                                         esac
187                                         ;;
188                                 *)      occ="$occ $x"
189                                         ;;
190                                 esac
191                         done
192                         continue
193                         ;;
194                 debug)  case $arg in
195                         0)      exec 2>&$nullout
196                                 set -
197                                 debug=
198                                 ;;
199                         ""|1)   exec 2>&$stderr
200                                 set -
201                                 debug=
202                                 ;;
203                         2)      exec 2>&$stderr
204                                 case $shell in
205                                 ksh)    PS4="${PS4%+*([         ])}+\$LINENO+ "
206                                 esac
207                                 set -x
208                                 debug=-x
209                                 ;;
210                         *)      echo "$command: $arg: debug levels are 0, 1, 2" >&$stderr
211                                 ;;
212                         esac
213                         continue
214                         ;;
215                 "in")   case $arg in
216                         "")     in=-
217                                 ;;
218                         *)      in=$arg
219                                 if      test ! -f $in
220                                 then    echo "$command: $in: not found" >&$stderr
221                                         exit 1
222                                 fi
223                                 exec < $in
224                                 case $out in
225                                 "")     case $in in
226                                         *[.\\/]*)
227                                                 case $shell in
228                                                 ksh)    out=${in##*[\\/]}
229                                                         out=${out%.*}
230                                                         ;;
231                                                 *)      eval `echo $in | sed -e 's,.*[\\\\/],,' -e 's/\.[^.]*//' -e 's/^/out=/'`
232                                                         ;;
233                                                 esac
234                                                 ;;
235                                         *)      out=$in
236                                                 ;;
237                                         esac
238                                         ;;
239                                 esac
240                                 ;;
241                         esac
242                         continue
243                         ;;
244                 nodebug)exec 2>&$nullout
245                         set -
246                         continue
247                         ;;
248                 out)    out=$arg
249                         defhdr=
250                         deflib=
251                         gothdr=
252                         gotlib=
253                         ;;
254                 prototyped|noprototyped)
255                         pragma="$pragma $op"
256                         ;;
257                 pragma) pragma="$pragma $arg"
258                         ;;
259                 shell)  shell=$arg
260                         continue
261                         ;;
262                 static) static=$arg
263                         continue
264                         ;;
265                 *)      echo "$command: $op: unkown option" >&$stderr
266                         exit 1
267                         ;;
268                 esac
269                 ;;
270         *)      case $1 in
271                 *\{)    op=-
272                         ;;
273                 *)      op=$1
274                         shift
275                         ;;
276                 esac
277                 arg=
278                 cc=$occ
279                 fail=
280                 hdr=
281                 lib=
282                 lst=hdr
283                 opt=
284                 pass=
285                 run=
286                 src=
287                 tst=
288                 case $# in
289                 0)      ;;
290                 *)      case $1 in
291                         \#*)    set x
292                                 shift
293                                 ;;
294                         *)      case $op in
295                                 ref)    ;;
296                                 *)      case $1 in
297                                         *\{)    arg=-
298                                                 ;;
299                                         *)      arg=$1
300                                                 shift
301                                         esac
302                                         ;;
303                                 esac
304                                 ;;
305                         esac
306                         while   :
307                         do      case $# in
308                                 0)      break ;;
309                                 esac
310                                 case $1 in
311                                 "")     ;;
312                                 \#*)    set x
313                                         ;;
314                                 [a-z]*\{)
315                                         v=$1
316                                         x=
317                                         shift
318                                         while   :
319                                         do      case $# in
320                                                 0)      case $in in
321                                                         "")     echo "$command: missing }end" >&$stderr
322                                                                 exit 1
323                                                                 ;;
324                                                         esac
325                                                         while   :
326                                                         do      if      read o lin
327                                                                 then    case $o in
328                                                                         \}end)  set $o $lin
329                                                                                 break 2
330                                                                                 ;;
331                                                                         ' '*|'  '*)
332                                                                                 set '' $o
333                                                                                 o=$2
334                                                                                 ;;
335                                                                         esac
336                                                                 
337                                                                         case $lin in
338                                                                         "")     x="$x
339 $o" ;;
340                                                                         *)      x="$x
341 $o $lin" ;;
342                                                                         esac
343                                                                 else    echo "$command: missing }end" >&$stderr
344                                                                         exit 1
345                                                                 fi
346                                                         done
347                                                         ;;
348                                                 esac
349                                                 case $1 in
350                                                 \}end)  break
351                                                         ;;
352                                                 *)      x="$x
353 $1"
354                                                         ;;
355                                                 esac
356                                                 shift
357                                         done
358                                         case $v in
359                                         fail\{) fail=$x ;;
360                                         pass\{) pass=$x ;;
361                                         test\{) tst=$x ;;
362                                         *)      src=$x run=$v ;;
363                                         esac
364                                         ;;
365                                 :)      shift
366                                         break
367                                         ;;
368                                 *[\(\)\{\}\ \   ]*)
369                                         tst="$tst $1"
370                                         ;;
371                                 -)      case $lst in
372                                         hdr)    hdr="$hdr $1" ;;
373                                         lib)    lib="$lib $1" ;;
374                                         esac
375                                         ;;
376                                 -l*)    lib="$lib $1"
377                                         lst=lib
378                                         ;;
379                                 -*|+*)  case $op in
380                                         ref)    occ="$occ $1" ;;
381                                         run)    opt="$opt $1" ;;
382                                         *)      cc="$cc $1" ;;
383                                         esac
384                                         ;;
385                                 *.a)    lib="$lib $1"
386                                         lst=lib
387                                         ;;
388                                 *[.\\/]*)
389                                         hdr="$hdr $1"
390                                         lst=hdr
391                                         ;;
392                                 *)      tst="$tst $1"
393                                         ;;
394                                 esac
395                                 shift
396                         done
397                         ;;
398                 esac
399                 ;;
400         esac
401
402         # check the candidate headers
403
404         case $hdr in
405         ?*)     z=$hdr
406                 hdr=
407                 hit=0
408                 for x in $z
409                 do      case $x in
410                         -)      case $hit in
411                                 0)      hit=1 ;;
412                                 1)      ;;
413                                 *)      break ;;
414                                 esac
415                                 continue
416                                 ;;
417                         *.h)    echo "#include \"$x\"" > $tmp.c
418                                 $cc -E $tmp.c <&$nullin >&$nullout || continue
419                                 ;;
420                         *)      test -r $x || continue
421                                 ;;
422                         esac
423                         hdr="$hdr $x"
424                         case $hit in
425                         0)      ;;
426                         1)      hit=2 ;;
427                         esac
428                 done
429                 ;;
430         esac
431
432         # check the candidate libraries
433
434         case $lib in
435         ?*)     z=$lib
436                 e=
437                 lib=
438                 echo "main(){return(0);}" > $tmp.c
439                 for x in $z
440                 do      case $x in
441                         -)      case $lib in
442                                 "")     continue ;;
443                                 esac
444                                 break
445                                 ;;
446                         esac
447                         while   :
448                         do      rm -f $tmp.exe
449                                 if      $cc -o $tmp.exe $tmp.c $e $x <&$nullin >&$nullout
450                                 then    lib="$lib $e $x"
451                                         e=
452                                         break
453                                 else    case $e in
454                                         "")     e=$x
455                                                 break
456                                                 ;;
457                                         *)      e=
458                                                 ;;
459                                         esac
460                                 fi
461                         done
462                 done
463                 ;;
464         esac
465
466         # precheck some more ops
467
468         case $op in
469         ref)    deflib="$deflib $lib"
470                 defhdr="$defhdr $hdr"
471                 continue
472                 ;;
473         esac
474         ifs=$IFS
475         IFS=,
476         eval op=\"$op\"
477         eval arg=\"$arg\"
478         IFS=$ifs
479
480         # check for op aliases
481
482         x=
483         for o in $op
484         do      case $o in
485                 def|default)    x="$x cmd hdr key lib mth sys typ" ;;
486                 *)              x="$x $o" ;;
487                 esac
488         done
489
490         # loop on the ops o and args a
491
492         for o in $x
493         do      for a in $arg
494                 do      user=
495                         case $a in
496                         *[.\\/]*)
497                                 case $o in
498                                 hdr|sys)
499                                         case $a in
500                                         *[\\/]*) x=/ ;;
501                                         *)       x=. ;;
502                                         esac
503                                         case $shell in
504                                         ksh)    p=${a%%${x}*}
505                                                 v=${a##*${x}}
506                                                 ;;
507                                         *)      eval `echo $a | sed -e "s/\\(.*\\)\\\\${x}\\(.*\\)/p=\\1 v=\\2/"`
508                                                 ;;
509                                         esac
510                                         ;;
511                                 *)      case $shell in
512                                         ksh)    p=${a%.*}
513                                                 p=${p##*[\\/]}
514                                                 v=${a##*.}
515                                                 v=${v##*[\\/]}
516                                                 ;;
517                                         *)      eval `echo $a | sed -e 's,.*[\\\\/],,' -e 's/\(.*\)\.\(.*\)/p=\1 v=\2/'`
518                                                 ;;
519                                         esac
520                                         ;;
521                                 esac
522                                 f=${p}/${v}
523                                 case $o in
524                                 run)    v=$p
525                                         p=
526                                         m=_${v}
527                                         ;;
528                                 mem)    m=_${v}_${p}
529                                         ;;
530                                 *)      m=_${p}_${v}
531                                         ;;
532                                 esac
533                                 ;;
534                         *)      p=
535                                 v=$a
536                                 f=$a
537                                 m=_${v}
538                                 ;;
539                         esac
540
541                         # check output redirection
542
543                         case $out in
544                         $cur)   ;;
545                         *)      case $cur in
546                                 $a|$v)  ;;
547                                 *)      case $cur in
548                                         .)      ;;
549                                         -)      echo "#endif"
550                                                 ;;
551                                         *)      echo "#endif"
552                                                 exec >&-
553                                                 case $cur in
554                                                 *[\\/]*|*.h)    x=$cur ;;
555                                                 *)              x=$dir/$cur ;;
556                                                 esac
557                                                 proto -r $tmp.h
558                                                 sed 's,/\*[^/]*\*/, ,g' $tmp.h > $tmp.c
559                                                 sed 's,/\*[^/]*\*/, ,g' $x > $tmp.t
560                                                 if      cmp -s $tmp.c $tmp.t
561                                                 then    rm -f $tmp.h
562                                                 else    case $x in
563                                                         ${dir}[\\/]$cur)        test -d $dir || mkdir $dir || exit 1 ;;
564                                                         esac
565                                                         mv $tmp.h $x
566                                                 fi
567                                                 ;;
568                                         esac
569                                         case $out in
570                                         +)      case $status in
571                                                 1)      ;;
572                                                 *)      status=0 ;;
573                                                 esac
574                                                 exit $status
575                                                 ;;
576                                         -)      eval "exec >&$stdout"
577                                                 ;;
578                                         *)      exec >$tmp.h
579                                                 ;;
580                                         esac
581                                         case $out in
582                                         "")     case $a in
583                                                 *[\\/]*|???????????????*) cur=$v ;;
584                                                 *)                      cur=$a ;;
585                                                 esac
586                                                 ;;
587                                         *)      cur=$out
588                                                 ;;
589                                         esac
590                                         case $in in
591                                         ""|-|+) case $o in
592                                                 run)    x=" from $a" ;;
593                                                 *)      x= ;;
594                                                 esac
595                                                 ;;
596                                         *)      x=" from $in"
597                                                 ;;
598                                         esac
599                                         echo "/* : : generated$x by $command version $version : : */"
600                                         for x in $pragma
601                                         do      echo "#pragma $x"
602                                         done
603                                         case $out in
604                                         ""|-|+) x=$m
605                                                 ;;
606                                         *.*)    case $shell in
607                                                 ksh)    i=$out
608                                                         x=_
609                                                         while   :
610                                                         do      case $i in
611                                                                 *.*)    x=$x${i%%.*}_
612                                                                         i=${i#*.}
613                                                                         ;;
614                                                                 *)      x=$x$i
615                                                                         break
616                                                                         ;;
617                                                                 esac
618                                                         done
619                                                         ;;
620                                                 *)      eval `echo $in | sed -e 's,\.,_,g' -e 's/^/x=/'`
621                                                         ;;
622                                                 esac
623                                                 ;;
624                                         *)      x=_$out
625                                                 ;;
626                                         esac
627                                         case $x in
628                                         *-*)    echo "#if 1"
629                                                 ;;
630                                         *)      case $x in
631                                                 _cmd|_lib)
632                                                         case $shell in
633                                                         ksh)    c=${PWD##*[\\/]}
634                                                                 c=${c#lib}
635                                                                 c=${c%%.*}
636                                                                 x=${x}_${c}
637                                                                 ;;
638                                                         *)      x=${x}_`echo $x | sed -e 's,.*[\\\\/],,' -e 's,^lib,,' -e 's,\..*,,'`
639                                                                 ;;
640                                                         esac
641                                                         ;;
642                                                 esac
643                                                 x=`echo $x | sed -e 's/[^a-zA-Z0-9_]/_/g'`
644                                                 echo "#ifndef _def${x}"
645                                                 echo "#define _def${x}  1"
646                                                 ;;
647                                         esac
648                                         ;;
649                                 esac
650                                 ;;
651                         esac
652
653                         # set up the candidate include list
654
655                         inc=
656                         for x in $defhdr $hdr
657                         do      case $x in
658                                 *.h)    case $shell in
659                                         ksh)    c=${x##*[\\/]}
660                                                 c=${c%%.*}
661                                                 case $x in
662                                                 */*)    c=${x%%[\\/]*}_${c} ;;
663                                                 esac
664                                                 ;;
665                                         *)      eval `echo $x | sed -e 's,^\([^\\\\/]*\).*[\\\\/]\([^\\\\/]*\)\$,\1_\2,' -e 's/\..*//' -e 's/^/c=/'`
666                                                 ;;
667                                         esac
668                                         case " $gothdr " in
669                                         *" $x "*)
670                                                 ;;
671                                         *)      gothdr="$gothdr $x"
672                                                 echo "#define _inc_${c} 1       /* candidate header <$x> found */"
673                                                 ;;
674                                         esac
675                                         inc="$inc
676 #include <$x>"
677                                         ;;
678                                 esac
679                         done
680
681                         # set up the candidate lib list
682
683                         for x in $lib $deflib
684                         do      case " $gotlib " in
685                                 *" $x "*)
686                                         ;;
687                                 *)      gotlib="$gotlib $x"
688                                         echo "/* candidate library $x found */"
689                                         ;;
690                                 esac
691                         done
692
693                         # src overrides builtin test
694
695                         case $o in
696                         tst)    ;;
697                         *)      m=_${o}${m}
698                                 ;;
699                         esac
700                         pre="#undef $v"
701                         case $src in
702                         ?*)     echo "$inc
703 $src" > $tmp.c
704                                 e=
705                                 case $run in
706                                 cat*|nocat*)
707                                         echo "$src"
708                                         ;;
709                                 run*|norun*)
710                                         (eval "$src") <&$nullin || e=1
711                                         ;;
712                                 mac*|nomac*)
713                                         if      $cc -E $tmp.c <&$nullin >$tmp.i
714                                         then    sed -e '/<<[    ]*".*"[         ]*>>/!d' -e 's/<<[      ]*"//g' -e 's/"[        ]*>>//g' $tmp.i
715                                         else    e=1
716                                         fi
717                                         ;;
718                                 p*|nop*)$cc -DTEST=$p -DID=$v -E $tmp.c <&$nullin >&$nullout || e=1
719                                         ;;
720                                 c*|noc*)$cc -DTEST=$p -DID=$v -c $tmp.c <&$nullin >&$nullout || e=1
721                                         ;;
722                                 *)      rm -f $tmp.exe
723                                         if      $cc -DTEST=$p -DID=$v -o $tmp.exe $tmp.c $lib $deflib <&$nullin >&$nullout && $executable $tmp.exe
724                                         then    case $run in
725                                                 l*|nol*);;
726                                                 o*|noo*)$tmp.exe <&$nullin || e=1 ;;
727                                                 *)      $tmp.exe <&$nullin >&$nullout || e=1 ;;
728                                                 esac
729                                         else    e=1
730                                         fi
731                                         ;;
732                                 esac
733                                 case $run in
734                                 no*)    case $e in
735                                         "")     e=1 ;;
736                                         *)      e= ;;
737                                         esac
738                                         ;;
739                                 esac
740                                 case $e in
741                                 "")     case $m in
742                                         *-*)    ;;
743                                         *)      echo "#define $m        1       /* ${run}} passed */" ;;
744                                         esac
745                                         (eval "$pass") <&$nullin
746                                         ;;
747                                 *)      (eval "$fail") <&$nullin
748                                         ;;
749                                 esac
750                                 continue
751                                 ;;
752                         esac
753
754                         # initialize common builtin state
755
756                         case $o in
757                         dat|lib|mth|run)
758                                 case $statictest in
759                                 "")     statictest=FoobaR
760                                         echo "#if __cplusplus
761 extern \"C\" {
762 #endif
763 extern int $statictest();
764 #if __cplusplus
765 }
766 #endif
767 static int ((*i)())=$statictest;main(){return(i==0);}" > $tmp.c
768                                         rm -f $tmp.exe
769                                         if      $cc -o $tmp.exe $tmp.c <&$nullin >&$nullout && $executable $tmp.exe
770                                         then    case $static in
771                                                 .)      static=
772                                                         echo '#include <stdio.h>
773 main(){printf("hello");return(0);}' > $tmp.c
774                                                         rm -f $tmp.exe
775                                                         if      $cc -c $tmp.c <&$nullin >&$nullout && $cc -o $tmp.exe $tmp.o <&$nullin >&$nullout 2>$tmp.e && $executable $tmp.exe
776                                                         then    e=`wc -l $tmp.e`
777                                                                 set x x $binding
778                                                                 while   :
779                                                                 do      shift
780                                                                         shift
781                                                                         case $# in
782                                                                         0)      break ;;
783                                                                         esac
784                                                                         rm -f $tmp.exe
785                                                                         $cc -o $tmp.exe $1 $tmp.o <&$nullin >&$nullout 2>$tmp.e && $executable $tmp.exe || continue
786                                                                         case `wc -l $tmp.e` in
787                                                                         $e)     ;;
788                                                                         *)      continue ;;
789                                                                         esac
790                                                                         d=`ls -s $tmp.exe`
791                                                                         rm -f $tmp.exe
792                                                                         $cc -o $tmp.exe $2 $tmp.o <&$nullin >&$nullout 2>$tmp.e && $executable $tmp.exe || continue
793                                                                         case `wc -l $tmp.e` in
794                                                                         $e)     ;;
795                                                                         *)      continue ;;
796                                                                         esac
797                                                                         case `ls -s $tmp.exe` in
798                                                                         $d)     ;;
799                                                                         *)      static=$2
800                                                                                 set x
801                                                                                 shift
802                                                                                 break
803                                                                                 ;;
804                                                                         esac
805                                                                 done
806                                                         fi
807                                                         ;;
808                                                 esac
809                                         else    static=
810                                         fi
811                                         ;;
812                                 esac
813                                 ;;
814                         esac
815
816                         # builtin tests
817
818                         case $o in
819                         cmd)    case $p in
820                                 ?*)     continue ;;
821                                 esac
822                                 k=
823                                 for j in "" usr
824                                 do      case $j in
825                                         "")     d= s= ;;
826                                         *)      d=/$j s=_$j ;;
827                                         esac
828                                         for i in bin etc ucb
829                                         do      if      test -f $d/$i/$a
830                                                 then    case $k in
831                                                         "")     k=1
832                                                                 echo "#define $m        1       /* $a in ?(/usr)/(bin|etc|ucb) */"
833                                                                 ;;
834                                                         esac
835                                                         echo "#define ${s}_${i}_${v}    1       /* $d/$i/$a found */"
836                                                 fi
837                                         done
838                                 done
839                                 ;;
840                         dat)    case $p in
841                                 ?*)     continue ;;
842                                 esac
843                                 {
844                                 echo "$pre"
845                                 case $inc in
846                                 ?*)     echo "$inc"
847                                         ;;
848                                 *)      echo "#if __cplusplus
849 extern \"C\" {
850 #endif
851 extern int $v;
852 #if __cplusplus
853 }
854 #endif"
855                                         ;;
856                                 esac
857                                 echo "main(){char* i = (char*)&$v; return i!=0;}"
858                                 } > $tmp.c
859                                 $cc -c $tmp.c <&$nullin >&$nullout &&
860                                 rm -f $tmp.exe
861                                 if      $cc $static -o $tmp.exe $tmp.o $lib $deflib <&$nullin >&$nullout && $executable $tmp.exe
862                                 then    echo "#define $m        1       /* $v in default lib(s) */"
863                                 fi
864                                 ;;
865                         hdr|lcl|sys)
866                                 case $p in
867                                 lcl)    eval p='$'_lcl_$v
868                                         case $p in
869                                         ?*)     continue ;;
870                                         esac
871                                         eval _lcl_$v=1
872                                         p=
873                                         f=$v
874                                         ;;
875                                 *)      case $o in
876                                         hdr)    echo "#include <$f.h>" > $tmp.c
877                                                 $cc -E $tmp.c <&$nullin >&$nullout &&
878                                                 echo "#define $m        1       /* #include <$f.h> ok */"
879                                                 ;;
880                                         sys)    echo "#include <sys/$f.h>" > $tmp.c
881                                                 $cc -E $tmp.c <&$nullin >&$nullout &&
882                                                 echo "#define $m        1       /* #include <sys/$f.h> ok */"
883                                                 ;;
884                                         esac
885                                         continue
886                                         ;;
887                                 esac
888                                 case $p in
889                                 ?*)     x="$p\\$v" ;;
890                                 *)      x=$f ;;
891                                 esac
892                                 case $f in
893                                 *[\\/]*)g=$f ;;
894                                 *)      g="$f sys/$f" ;;
895                                 esac
896                                 for f in $g
897                                 do      echo "$pre
898 $inc
899 #include <$f.h>" > $tmp.c
900                                         if      $cc -E $tmp.c <&$nullin >$tmp.i
901                                         then    i=`sed -e '/^#[line     ]*1[    ][      ]*"[\\\\/].*[\\\\/]'$x'\.h"/!d' -e '/[\\\\/]sys[\\\\/]'$x'\.h"/d' -e s'/.*"\(.*\)".*/\1/' $tmp.i`
902                                                 for i in $i
903                                                 do      break
904                                                 done
905                                         else    i=
906                                         fi
907                                         case $i in
908                                         [\\/]*) echo "#if defined(__STDPP__directive)"
909                                                 echo "__STDPP__directive pragma pp:hosted"
910                                                 echo "#endif"
911                                                 echo "#include \"$i\"   /* untrusted local <$f.h> */"
912                                                 echo "#undef    $m"
913                                                 echo "#define $m        1"
914                                                 break
915                                                 ;;
916                                         *)      echo "/* no local <$f.h> */"
917                                                 ;;
918                                         esac
919                                 done
920                                 ;;
921                         key)    case $p in
922                                 ?*)     continue ;;
923                                 esac
924                                 echo "$pre
925 int f(){int $v = 1;return($v);}" > $tmp.c
926                                 $cc -c $tmp.c <&$nullin >&$nullout ||
927                                 echo "#define $m        1       /* $v is a reserved keyword */"
928                                 ;;
929                         lib|mth)case $p in
930                                 ?*)     continue ;;
931                                 esac
932                                 echo "$pre
933 $inc
934 #if __cplusplus
935 extern \"C\" {
936 #endif
937 extern int $v();
938 #if __cplusplus
939 }
940 #endif
941 static int ((*i)())=$v;main(){return(i==0);}" > $tmp.c
942                                 $cc -c $tmp.c <&$nullin >&$nullout &&
943                                 rm -f $tmp.exe
944                                 if      $cc $static -o $tmp.exe $tmp.o $lib $deflib <&$nullin >&$nullout && $executable $tmp.exe
945                                 then    case $o in
946                                         lib)    echo "#define $m        1       /* $v() in default lib(s) */" ;;
947                                         esac
948                                 else    case $o in
949                                         mth)    rm -f $tmp.exe
950                                                 $cc $static -o $tmp.exe $tmp.o -lm <&$nullin >&$nullout &&
951                                                 $executable $tmp.exe &&
952                                                 echo "#define $m        1       /* $v() in math lib */"
953                                                 ;;
954                                         esac
955                                 fi
956                                 ;;
957                         mac)    case $p in
958                                 ?*)     continue ;;
959                                 esac
960                                 echo "$pre
961 $inc
962 #ifdef $v
963 '#define $m     1       /* $v is a macro */'
964 #endif" > $tmp.c
965                                 $cc -E $tmp.c <&$nullin | sed -e "/^'#define/!d" -e "s/'//g"
966                                 ;;
967                         mem)    case $p in
968                                 ?*)     for i in "" "struct "
969                                         do      echo "$pre
970 $inc
971 static $i$p i;
972 int n = sizeof(i.$v);" > $tmp.c
973                                                 $cc -c $tmp.c <&$nullin >&$nullout &&
974                                                 echo "#define $m        1       /* $v is member of $i$p */" &&
975                                                 break
976                                         done
977                                         ;;
978                                 *)      echo "$command: $o: <struct>.<member> expected" >&$stderr
979                                         status=1
980                                         ;;
981                                 esac
982                                 ;;
983                         nop)    ;;
984                         one)    for i in $a $hdr
985                                 do      x="#include <$i>"
986                                         echo "$x" > $tmp.c
987                                         if      $cc -E $tmp.c <&$nullin >&$nullout
988                                         then    echo "$x"
989                                                 break
990                                         fi
991                                 done
992                                 ;;
993                         out)    ;;
994                         run)    if      test ! -f $a
995                                 then    echo "$command: $a: not found" >&$stderr
996                                         exit 1
997                                 fi
998                                 case $a in
999                                 *.c)    rm -f $tmp.exe
1000                                         cp $a $tmp.c
1001                                         $cc -o $tmp.exe $tmp.c $lib $deflib <&$nullin >&$nullout &&
1002                                         $executable $tmp.exe &&
1003                                         $tmp.exe $opt <&$nullin
1004                                         ;;
1005                                 *.sh)   ${SHELL-sh} $debug $a "cc='$cc' executable='$executable' id='$m' static='$static' tmp='$tmp'" $opt $hdr <&$nullin
1006                                         ;;
1007                                 *)      false
1008                                         ;;
1009                                 esac
1010                                 case $? in
1011                                 0)      ;;
1012                                 *)      echo "$command: $a: cannot run" >&$stderr
1013                                         exit 1
1014                                         ;;
1015                                 esac
1016                                 ;;
1017                         sym)    case $tst in
1018                                 "")     x=$v ;;
1019                                 *)      x=$tst ;;
1020                                 esac
1021                                 echo "$pre
1022 $inc
1023 '=' $x '='" > $tmp.c
1024                                 $cc -E $tmp.c <&$nullin \
1025                                 | sed \
1026                                         -e "/'='/!d" \
1027                                         -e "s/'='//g" \
1028                                         -e 's/[         ]//g' \
1029                                         -e 's/((([^()]*)))->/->/g' \
1030                                         -e 's/(([^()]*))->/->/g' \
1031                                         -e 's/([^()]*)->/->/g' \
1032                                         -e 's/\([a-zA-Z_][a-zA-Z_0-9]*\)\[/\
1033 ary \1[/g' \
1034                                         -e 's/\([a-zA-Z_][a-zA-Z_0-9]*\)(/\
1035 fun \1[/g' \
1036                                         -e 's/\*->\([a-zA-Z_]\)/->\
1037 ptr \1/g' \
1038                                         -e 's/->\([a-zA-Z_]\)/->\
1039 reg \1/g' \
1040                                         -e "/^$v\$/d" \
1041                                         -e 's/^[a-zA-Z_][a-zA-Z_0-9]*$/\
1042 nam &/g' \
1043                                 | sed \
1044                                         -e '/^... /!d' \
1045                                 | sort \
1046                                         -u \
1047                                 | sed \
1048                                         -e 's/\(...\) \([a-zA-Z_][a-zA-Z_0-9]*\).*/#ifndef _\1_'$v'\
1049 #define _\1_'$v' \2\
1050 #define _\1_'$v'_str "\2"\
1051 #endif/'
1052                                 ;;
1053                         typ)    case $p in
1054                                 "")     x= ;;
1055                                 *)      x="$p " ;;
1056                                 esac
1057                                 case $typ in
1058                                 "")     typ="
1059 #include <sys/types.h>"
1060                                         c=
1061                                         h=
1062                                         for i in time "" times
1063                                         do      case $i in
1064                                                 "")     c=sys_
1065                                                         h=sys/
1066                                                         continue
1067                                                         ;;
1068                                                 esac
1069                                                 c=${c}$i
1070                                                 h=${h}$i.h
1071                                                 t="$typ
1072 #include <$h>"
1073                                                 echo "$t" > $tmp.c
1074                                                 if      $cc -c $tmp.c <&$nullin >&$nullout
1075                                                 then    typ="$t"
1076                                                         echo "#define _inc_${c} 1       /* candidate header <$h> found */"
1077                                                 fi
1078                                                 c=
1079                                                 h=
1080                                         done
1081                                         t=
1082                                         for i in stddef stdlib
1083                                         do      u="$t
1084 #include <$i.h>"
1085                                                 echo "$u$typ" > $tmp.c
1086                                                 if      $cc -c $tmp.c <&$nullin >&$nullout
1087                                                 then    t="$u"
1088                                                         echo "#define _inc_$i   1       /* candidate header <$i.h> found */"
1089                                                 else    break
1090                                                 fi
1091                                         done
1092                                         typ="$t$typ"
1093                                         ;;
1094                                 esac
1095                                 {
1096                                 case $p in
1097                                 long)   echo "$pre
1098 $typ$inc
1099 static $x$v i;
1100 $x$v f() {
1101 $x$v v; v = i;"
1102                                         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;
1103 return v; }"
1104                                         ;;
1105                                 *)      echo "$pre
1106 $typ$inc
1107 struct xxx { $x$v mem; };
1108 static struct xxx v;
1109 struct xxx* f() { return &v; }"
1110                                         ;;
1111                                 esac
1112                                 echo "main() { f(); return 0; }"
1113                                 } > $tmp.c
1114                                 rm -f $tmp.exe
1115                                 if      $cc -o $tmp.exe $tmp.c <&$nullin >&$nullout &&
1116                                 $executable $tmp.exe
1117                                 then    echo "#define $m        1       /* $x$v is a type */"
1118                                         user=$pass
1119                                 else    user=$fail
1120                                 fi
1121                                 ;;
1122                         *)      echo "$command: $o: unknown feature test" >&$stderr
1123                                 status=1
1124                                 ;;
1125                         esac
1126                         case $user in
1127                         ?*)     (eval "$user") <&$nullin ;;
1128                         esac
1129                 done
1130         done
1131 done