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