1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#          Copyright (c) 1989-2011 AT&T Intellectual Property          #
5#                      and is licensed under the                       #
6#                 Eclipse Public License, Version 1.0                  #
7#                    by AT&T Intellectual Property                     #
8#                                                                      #
9#                A copy of the License is available at                 #
10#          http://www.eclipse.org/org/documents/epl-v10.html           #
11#         (with md5 checksum b35adb5213ca9657e911e9befb180842)         #
12#                                                                      #
13#              Information and Software Systems Research               #
14#                            AT&T Research                             #
15#                           Florham Park NJ                            #
16#                                                                      #
17#                 Glenn Fowler <gsf@research.att.com>                  #
18#                  David Korn <dgk@research.att.com>                   #
19#                   Eduardo Krell <ekrell@adexus.cl>                   #
20#                                                                      #
21########################################################################
22: generate the 3d syscall features
23: include sys.tab
24#
25# this is the worst iffe script, we promise
26#
27# @(#)syscall.sh (AT&T Research) 2011-12-06
28#
29eval $1
30shell=
31( typeset -u u=a && test "A" = "$u" ) >/dev/null 2>&1 && shell=u$shell
32( integer n=1+1 && test "2" = "$n" ) >/dev/null 2>&1 && shell=n$shell
33( x=a.b.c && test "a" = "${x%%.*}" ) >/dev/null 2>&1 && shell=e$shell
34shift
35case $1 in
36"")	set /dev/null ;;
37esac
38input=$1
39comma=","
40nl="
41"
42tmpsuf="i z m d r f u s n b g t e"
43tmpall=
44for i in $tmpsuf
45do	tmpall=$tmpall$i
46done
47tmpall="$tmp.[$tmpall]"
48rm -f $tmpall
49sys=
50fun=
51stdc=
52sym=
53vers=
54weak=
55case $shell in
56*n*)	integer inx ;;
57esac
58inx=0
59A_OUTPUT=1
60A_INPUT=2
61A_ACCESS=3
62A_MODE=4
63A_NUMBER=5
64A_OPEN=6
65A_POINTER=7
66A_SIZE=8
67A_STRING=9
68A_VECTOR=A
69echo "
70#pragma prototyped"
71echo "
72#if defined(header)
73#include \"${tmp}s.h\"
74#endif
75extern
76#if defined(__cplusplus)
77\"C\"
78#endif
79int syscall();
80static int ((*i)())=syscall;
81main() { return(i==0); }" > $tmp.c
82
83SYS_PREFIX=
84SYS_HEADER=
85SYS_CALL=
86for f in syscall.h sys/syscall.h
87do	echo "
88#include <$f>
89int x;" > ${tmp}s.c
90	if	$cc -c ${tmp}s.c </dev/null >/dev/null
91	then	SYS_HEADER=$f
92		echo "
93#include <$f>
94" >> $tmp.f
95		echo "#include <sys/syscall.h>
96#ifdef SYS_exit
97int i;
98#else
99(
100#endif" > ${tmp}s.c
101		if	$cc -c ${tmp}s.c
102		then	l=fun
103			echo "int ${l}() { return 0; }" > ${tmp}s.c
104			if	$cc -c ${tmp}s.c </dev/null >/dev/null
105			then	SYS_CALL=syscall
106				s=`nm ${tmp}s.o | sed -e /${l}/!d -e 's/.*[^_]\(_*'$l'[0-9]*\).*/\1/'`
107				case $s in
108				_*)	SYS_PREFIX=_ ;;
109				esac
110			fi
111		fi
112		break
113	fi
114done
115
116while	:
117do	echo '#include <sys/types.h>
118#include <dlfcn.h>
119#ifdef RTLD_LAZY
120main()
121{
122	dlsym(dlopen("/lib/libc.so", RTLD_LAZY), "open");
123	return(0);
124}
125#else
126(
127#endif' > ${tmp}h.c
128	if	rm -f $tmp.x && {
129			$cc -o $tmp.x ${tmp}h.c -ldl </dev/null >/dev/null ||
130			$cc -o $tmp.x ${tmp}h.c </dev/null >/dev/null
131		} &&
132		$executable $tmp.x
133	then	sys=dynamic
134		echo "#define _dynamic_syscall		1" >> $tmp.m
135		lib=
136		for d in /lib /usr/lib /shlib /usr/shlib
137		do	for s in "*.*" "*.[!a]*"
138			do	for b in libc
139				do	for i in $d/$b.$s
140					do	if	test -f $i
141						then	lib=$i
142						fi
143					done
144					case $lib in
145					?*)	break 3 ;;
146					esac
147				done
148			done
149		done
150		case $lib in
151		*.[0-9]*.[0-9]*)
152			i=`echo $lib | sed 's,\([^0-9]*[0-9]*\).*,\1,`
153			if	test -f $i
154			then	lib=$i
155			fi
156			;;
157		esac
158		# some run time linkers barf with /lib/xxx if
159		# /usr/lib/xxx is there
160		case $lib in
161		/usr*)	;;
162		*)	if	test -f /usr$lib
163			then	lib=/usr$lib
164			fi
165			;;
166		esac
167		case $lib in
168		"")	lib=/lib/libc.so.1 ;;
169		esac
170		echo '#include <sys/types.h>
171#include <dlfcn.h>
172#ifdef RTLD_NEXT
173main()
174{
175	return 0;
176}
177#else
178(
179#endif' > ${tmp}h.c
180		if	rm -f $tmp.x && {
181			$cc -o $tmp.x ${tmp}h.c -ldl </dev/null >/dev/null ||
182			$cc -o $tmp.x ${tmp}h.c </dev/null >/dev/null
183			} &&
184			$executable $tmp.x
185		then	weak=1
186		fi
187		break
188	fi
189	if	rm -f $tmp.x &&
190		$cc $static -o $tmp.x $tmp.c </dev/null >/dev/null &&
191		$executable $tmp.x
192	then	case $SYS_HEADER in
193		?*)	echo "
194#include <$f>
195SYS" > ${tmp}s.c
196			;;
197		esac
198		inc="#if defined(__STDPP__directive) && defined(__STDPP__hide)
199__STDPP__directive pragma pp:hide syscall
200#else
201#define syscall        ______syscall
202#endif
203#include <$SYS_HEADER>
204#if defined(__STDPP__directive) && defined(__STDPP__hide)
205__STDPP__directive pragma pp:nohide syscall
206#else
207#undef  syscall
208#endif"
209		{
210		case $SYS_HEADER in
211		?*)	echo "$inc" ;;
212		*)	echo "#define SYS_exit 1" ;;
213		esac
214		echo "main() { syscall(SYS_exit, 0); exit(1); }"
215		} > ${tmp}m.c
216		if	rm -f ${tmp}m.x &&
217			$cc $static -o ${tmp}m.x ${tmp}m.c </dev/null >/dev/null &&
218			./${tmp}m.x
219		then	case $SYS_HEADER in
220			?*)	echo "$inc" ;;
221			esac
222			rm -f $tmp.x
223			if	rm -f $tmp.x &&
224				$cc -Dsyscall=swapon $static -o $tmp.x $tmp.c </dev/null >/dev/null &&
225				$executable $tmp.x
226			then	sys=bsd
227			else	sys=att
228			fi
229			echo "#define _static_syscall		1" >> $tmp.m
230			break
231		fi
232	fi
233	sys=mangle
234	echo "#define _mangle_syscall		1" >> $tmp.m
235	if	LD_PRELOAD=/dev/null ${_ld_preload_test-cat} </dev/null >/dev/null
236	then	echo "
237static int _lib_mangle_lib_;" >> $tmp.m
238	fi
239	break
240done
241echo iffe: test: type=$sys header=$SYS_HEADER ${SYS_CALL:+call=$SYS_CALL} ${SYS_PREFIX:+prefix=$SYS_PREFIX} >&$stderr
242echo "#if SYSTRACE3D
243
244/* output */
245
246#define A_OUTPUT	0x$A_OUTPUT
247
248/* input only */
249
250#define A_INPUT		0x$A_INPUT
251#define A_ACCESS	0x$A_ACCESS
252#define A_MODE		0x$A_MODE
253#define A_NUMBER	0x$A_NUMBER
254#define A_OPEN		0x$A_OPEN
255#define A_POINTER	0x$A_POINTER
256#define A_SIZE		0x$A_SIZE
257#define A_STRING	0x$A_STRING
258#define A_VECTOR	0x$A_VECTOR
259
260typedef struct
261{
262	Sysfunc_t	func;
263	short		index;
264	short		nov;
265	unsigned short	call;
266	unsigned short	args;
267	const char*	name;
268	unsigned char	type[8];
269	unsigned long	count;
270	unsigned long	megs;
271	unsigned long	units;
272	Sysfunc_t	last;
273	unsigned short	active;
274} Systrace_t;
275" > $tmp.b
276
277echo "static Systrace_t	sys_trace[] =
278{
279" > $tmp.t
280case $sys in
281*)	SYSCALL3D=1 ;;
282esac
283for i in $cc
284do	case $i in
285	-D*)	case $shell in
286		*e*)	v=${i#-D}
287			v=${v%%=*}
288			;;
289		*)	v=`echo '' $i | sed -e 's/^ -D//' -e 's/=.*//'`
290			;;
291		esac
292		eval $v=1
293		;;
294	esac
295done
296exec <$input
297ifi=0
298ifs=${IFS-'
299	 '}
300while	:
301do	IFS="	"
302	read line || break
303	set $line
304	IFS=$ifs
305	case $1 in
306	\#*)	continue ;;
307	"")	break ;;
308	"if")	shift
309		eval $*
310		case $? in
311		0)	ifi="0 $ifi" ;;
312		*)	ifi="1 $ifi" ;;
313		esac
314		continue
315		;;
316	"elif")	case $ifi in
317		0)	echo "$0: no if for elif" >&$stderr
318			break
319			;;
320		esac
321		case $ifi in
322		1*)	shift
323			eval $*
324			case $? in
325			0)	;;
326			*)	continue ;;
327			esac
328			i=0
329			;;
330		*)	i=2
331			;;
332		esac
333		set $ifi
334		shift
335		ifi="$i $ifi"
336		;;
337	"else")	case $ifi in
338		0)	echo "$0: no if for else" >&$stderr
339			break
340			;;
341		esac
342		case $ifi in
343		1*)	i=0 ;;
344		*)	i=2 ;;
345		esac
346		set $ifi
347		shift
348		ifi="$i $ifi"
349		;;
350	"fi")	case $ifi in
351		0)	echo "$0: no if for fi" >&$stderr
352			break
353			;;
354		esac
355		set $ifi
356		shift
357		ifi=$*
358		continue
359		;;
360	esac
361	case $ifi in
362	0*)	;;
363	*)	continue ;;
364	esac
365	l=$1
366	q=$2
367	r=$3
368	p=$4
369	shift 4
370	echo iffe: test: system call $l >&$stderr
371	case $r in
372	"bool")	cast="(" tsac="<0?-1:0)" r=int ;;
373	"int")	cast= tsac= ;;
374	"DIR*")	cast="(DIR$r)" tsac= ;;
375	*)	cast="($r)" tsac= ;;
376	esac
377	case $shell in
378	*u*)	typeset -u u=$l ;;
379	*)	u=`echo $l | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ` ;;
380	esac
381	w=$l
382	case $l in
383	_*)	case $shell in
384		*e*)	v=${u#?} ;;
385		*)	v=`echo $u | sed 's/.//'` ;;
386		esac
387		echo "#define $v			$u" >> $tmp.r
388		m=$l
389		echo "#undef	$l	/*MANGLENAME*/" >> $tmp.z
390		;;
391	*)	U=$u
392		m=_$l
393		for i in __libc_ _ __
394		do	if	rm -f $tmp.x &&
395				$cc -Dsyscall=${i}$l $static -o $tmp.x $tmp.c </dev/null >/dev/null &&
396				$executable $tmp.x
397			then	case $sys:$l in
398				*:creat|*:creat[0-9]*)	# sol6.sun4
399					;;
400				*:open|*:open[0-9]*)	# sol6.sun4
401					;;
402				dynamic:*)
403					# test if the _ version is a weak ref
404					echo "#include <sys/types.h>
405#include <dlfcn.h>
406main()
407{
408	void*	p;
409	void*	u;
410	void*	r;
411	p = dlopen(\"$lib\", 1);
412	u = dlsym(p, \"${i}$l\");
413	r = dlsym(p, \"$l\");
414	return(u == r);
415}" > ${tmp}h.c
416					if	rm -f $tmp.x && {
417							$cc -o $tmp.x ${tmp}h.c -ldl </dev/null >/dev/null ||
418							$cc -o $tmp.x ${tmp}h.c </dev/null >/dev/null
419						} &&
420						./$tmp.x
421					then	break
422					fi
423					;;
424				esac
425				w=${i}$l
426				case $shell in
427				*u*)	typeset -u I=$i ;;
428				*)	I=`echo $i | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ` ;;
429				esac
430				{
431				echo "#undef  $u"
432				echo "#define $u			${I}$u"
433				} >> $tmp.r
434				u=${I}$u
435				break
436			fi
437		done
438		{
439		echo "#undef	$l	/*MANGLENAME*/"
440		case $w in
441		$l)	;;
442		*)	echo "#undef	$w" ;;
443		esac
444		} >> $tmp.z
445		;;
446	esac
447	case $p in
448	void)		b=0 a= ;;
449	*,*,*,*,*,*)	b=6 a=a,b,c,d,e,f ;;
450	*,*,*,*,*)	b=5 a=a,b,c,d,e ;;
451	*,*,*,*)	b=4 a=a,b,c,d ;;
452	*,*,*)		b=3 a=a,b,c ;;
453	*,*)		b=2 a=a,b ;;
454	*)		b=1 a=a ;;
455	esac
456	org=$p
457	A=$a
458	B=$b
459	alt=
460	def=
461	inc=
462	ind=
463	map=
464	num=
465	opt=
466	ref=
467	ver=
468	VER=
469	while	:
470	do	case $1 in
471		alt*)	alt=$2
472			shift 2
473			;;
474		num*)	shift
475			;;
476		var*)	var=$2
477			shift 2
478			set sys/types.h $1
479			{
480			for i
481			do	echo "#include <$i>"
482			done
483			echo "#ifdef __STDC__
484extern $r $l($var);
485#endif"
486			} > ${tmp}h.c
487			if	$cc -c ${tmp}h.c </dev/null >/dev/null
488			then	inc="$inc $*"
489				p=$var
490				case $p in
491				void)		b=0 a= ;;
492				*,*,*,*,*,*)	b=6 a=a,b,c,d,e,f ;;
493				*,*,*,*,*)	b=5 a=a,b,c,d,e ;;
494				*,*,*,*)	b=4 a=a,b,c,d ;;
495				*,*,*)		b=3 a=a,b,c ;;
496				*,*)		b=2 a=a,b ;;
497				*)		b=1 a=a ;;
498				esac
499				org=$p
500				A=$a
501				B=$b
502			fi
503			echo "#define ARG3D$m		$B" >> $tmp.f
504			break
505			;;
506		ver*)	VER=1
507			case $2 in
508			-)	ref= ;;
509			*)	ref=$2 ;;
510			esac
511			shift 2
512			case $l in
513			[fl]*)	case $shell in
514				*u*)	j=${l#?}
515					i=${l%$j}
516					xxx=_${i}x${j}
517					;;
518				*)	xxx=`echo $l | sed -e 's/\(.\)\(.*\)/_\1x\2/'`
519					;;
520				esac
521				;;
522			*)	xxx=_x${l}
523				;;
524			esac
525			for i in sys/types.h $ref
526			do	echo "#include <$i>"
527			done > ${tmp}h.c
528			i=
529			for j in `$cc -E ${tmp}h.c | sed -e '/^#[line 	]*1[ 	]/!d' -e 's/.*"\(.*\)".*/\1/'`
530			do	test -f "$j" && i="$i $j"
531			done
532			ver=`sed -e "/define[ 	]*[A-Z_0-9]*64_VER/!d" -e 's/.*define[ 	]*\([A-Z_0-9][A-Z_0-9]*\).*/\1/' $i </dev/null | sort -u`
533			ver64="$ver64 $ver"
534			ver=`sed -e "/${xxx}.*_VER/!d" -e 's/.*([ 	]*\([A-Z_0-9][A-Z_0-9]*\).*/\1/' $i </dev/null | sort -u`
535			case $l:$ver in
536			*:*STAT*)
537				;;
538			*stat:*)for j in _SCO_STAT_VER
539				do if grep '[ 	]_SCO_STAT_VER[ 	]' $i >/dev/null
540				   then	ver=$j; break
541				   fi
542				done
543				;;
544			esac
545			if	grep "[^a-zA-Z_0-9]_$xxx" $i >/dev/null
546			then	xxx=_$xxx
547			fi
548			;;
549		*.h)	inc="$inc $1"
550			shift
551			;;
552		[0-9]*)	case $sys in
553			bsd)	case $2 in
554				[0-9]*)	num=$2 ;;
555				*)	num=$1 ;;
556				esac
557				;;
558			*)	num=$1
559				;;
560			esac
561			case $2 in
562			[0-9]*)	shift 2 ;;
563			*)	shift ;;
564			esac
565			;;
566		"")	break
567			;;
568		*)	case $# in
569			[012])	break ;;
570			*)	shift 2 ;;
571			esac
572			;;
573		esac
574	done
575	case $num:$sys in
576	:dynamic)	num=1 ;;
577	esac
578	call=
579	case $inc in
580	?*)	for i in sys/types.h $inc
581		do	echo "#include <$i>"
582		done > ${tmp}s.h
583		opt="$opt -Dheader"
584		;;
585	esac
586	case $VER:$ver in
587	*:?*)	if	rm -f $tmp.x &&
588			$cc -Dsyscall=$xxx $static -o $tmp.x $tmp.c </dev/null >/dev/null &&
589			$executable $tmp.x
590		then	# hack test for last arg indirect in xxx version
591			case $stdc in
592			"")	{
593				echo "extern int foo(const char*);"
594				echo "extern int foo(const char**);"
595				} > ${tmp}h.c
596				if	$cc -c ${tmp}h.c </dev/null >/dev/null
597				then	stdc=0
598				else	stdc=1
599				fi
600				;;
601			esac
602			ra=$a
603			va=$a
604			case $stdc in
605			1)	{
606				for i in sys/types.h $ref
607				do	echo "#include <$i>"
608				done
609				echo "#ifdef INDIRECT"
610				echo "#define REFERENCE *"
611				echo "#else"
612				echo "#define REFERENCE"
613				echo "#endif"
614				echo "extern $r $xxx(const int, $p REFERENCE);"
615				} > ${tmp}h.c
616				if	$cc -c ${tmp}h.c </dev/null >/dev/null
617				then	: ok
618				elif	$cc -c -DINDIRECT ${tmp}h.c </dev/null >/dev/null
619				then	ind="*"
620					case $shell in
621					*e*)	i=${a%?}
622						ra="${i}&${a#$i}"
623						va="${i}*${a#$i}"
624						;;
625					*)	ra=`echo $a | sed -e 's/\(.\)$/\&\1'`
626						va=`echo $a | sed -e 's/\(.\)$/\*\1'`
627						;;
628					esac
629				fi
630				;;
631			esac
632			case $shell in
633			*u*)	typeset -u y=$xxx ;;
634			*)	y=`echo $xxx | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ` ;;
635			esac
636			case $sys in
637			dynamic|mangle)
638				echo "extern $r		$y(const int, $p$ind);" >> $tmp.e
639				;;
640			esac
641			{
642			echo "#define SYS3D$m		$inx"
643			echo "#ifndef SYS$m"
644			echo "#define SYS$m		(-1)"
645			echo "#endif"
646			echo "#ifndef SYS$xxx"
647			echo "#define SYS$xxx		(-1)"
648			echo "#endif"
649			echo "#define SYSNOV$xxx	SYS$m"
650			} >> $tmp.f
651			case $shell in
652			*n*)	((inx=inx+1)) ;;
653			*)	inx=`expr $inx + 1` ;;
654			esac
655			case $ver in
656			*[mM][kK][nN][oO][dD]*)	verid=_MKNOD_VER ;;
657			*[sS][tT][aA][tT]*)	verid=_STAT_VER ;;
658			*)			verid=$ver ;;
659			esac
660			{
661			case $y in
662			__*)	case $shell in
663				*e*)	Y=${y#__} ;;
664				*)	Y=`echo $y | sed -e 's/__//'` ;;
665				esac
666				echo "#undef  $Y"
667				echo "#define $Y	$y"
668				;;
669			_*)	case $shell in
670				*e*)	Y=${y#_} ;;
671				*)	Y=`echo $y | sed -e 's/_//'` ;;
672				esac
673				echo "#undef  $Y"
674				echo "#define $Y	$y"
675				;;
676			esac
677			echo "#undef  $u"
678			echo "#define $u($a)	$y(_3D${verid}${ra:+,}$ra)"
679			echo "#undef  $y"
680			echo "#define $y(v${a:+,}$a)		${cast}SYSCALL(SYS3D$m,v${a:+,}$a)${tsac}"
681			} >> $tmp.u
682			case $p in
683			void)	c=1
684				set $r int
685				;;
686			*)	IFS=$comma
687				set int${comma}$p
688				c=$#
689				set $r${comma}int${comma}$p$ind
690				IFS=$ifs
691				;;
692			esac
693			d=
694			s=
695			for i in 0 1 2 3 4 5
696			do	case $# in
697				0)	break ;;
698				esac
699				case $1 in
700				*"mode_t")
701					d=${d}${s}A_MODE
702					;;
703				*"size_t")
704					d=${d}${s}A_SIZE
705					;;
706				*"char* const*"|*"char**")
707					d=${d}${s}A_VECTOR
708					;;
709				*"char*")
710					case $r:$2 in
711					*"ssize_t":*"size_t")
712						d=${d}${s}A_OUTPUT,A_SIZE
713						shift
714						;;
715					*)	d=${d}${s}A_STRING
716						;;
717					esac
718					;;
719				*"const void*")
720					d=${d}${s}A_INPUT
721					;;
722				*"void*")
723					d=${d}${s}A_OUTPUT
724					;;
725				*"struct stat*"|*"int*")
726					d=${d}${s}A_OUTPUT
727					;;
728				*"...")	case $l in
729					*open|*open64)	d=${d}${s}A_MODE ;;
730					*)		d=${d}${s}A_POINTER ;;
731					esac
732					;;
733				*"[]"|*"*")
734					d=${d}${s}A_POINTER
735					;;
736				*)	d=${d}${s}A_NUMBER
737					;;
738				esac
739				s=,
740				shift
741			done
742			case $sys in
743			dynamic)x="0, "
744				;;
745			mangle)	x="$u, "
746				echo "#undef $u
747extern long $u();" >> $tmp.g
748				;;
749			*)	x=
750				;;
751			esac
752			echo iffe: test: $l "=>" $xxx >&$stderr
753			echo "{ ${x}SYS$xxx, SYSNOV$xxx, MSG_CALL(MSG_$q), $c, \"$xxx\", { $d } }," >> $tmp.t
754			# NOTE: no varargs here
755			IFS=$comma
756			set $p
757			IFS=$ifs
758			d=
759			f=
760			o=
761			for i in a b c d e f
762			do	case $# in
763				0)	break ;;
764				1)	e=$ind ;;
765				*)	e= ;;
766				esac
767				case $1 in
768				*\[\])	case $shell in
769					*e*)	k=${1%\[\]} ;;
770					*)	k=`echo $1 | sed 's/\[\]\$//'` ;;
771					esac
772					i="$i"'[]'
773					;;
774				*)	k=$1
775					;;
776				esac
777				case $k in
778				*char*)	n=path ;;
779				*dev_t)	n=dev ;;
780				*int*)	n=fd ;;
781				*mode_t*)n=mode ;;
782				*stat*)	n=st ;;
783				*)	n="<$k>${i}" ;;
784				esac
785				case $f in
786				'')	f=$n ;;
787				*)	f=$f,$n ;;
788				esac
789				case $d in
790				"")	d="$k$e $n"
791					o="$k$e $n;"
792					;;
793				*)	d="$d, $k$e $n"
794					o="$o $k$e $n;"
795					;;
796				esac
797				shift
798			done
799			echo "#define ${U}3D		_3d_$m" >> $tmp.d
800			echo "#define ${l}3d($a)	$xxx __PARAM__((const int _3d_ver, $d),(_3d_ver${f:+,} $f)) __OTORP__(int _3d_ver; $o)" >> $tmp.d
801			eval v='$'_INIT$ver
802			case $v in
803			"")	eval _INIT$ver=1
804				vers="$vers $ver"
805				;;
806			esac
807			case $sym in
808			"")	sym="$xxx" fun="$xxx()" ;;
809			*)	sym="$sym,$nl	$xxx" fun="$fun,$nl	$xxx()" ;;
810			esac
811			eval _SYS_${l}_VER=_3D$verid
812			continue
813		fi
814		;;
815	1:*)	case $SYS_CALL in
816		?*)	echo "#include <${SYS_HEADER}>
817#ifdef SYS_${l}
818(
819#endif
820#include <sys/types.h>
821#include <sys/stat.h>
822extern int ${l}();
823int fun() { struct stat st; return ${l}(0,&st); }" > ${tmp}s.c
824			if	$cc -c ${tmp}s.c >/dev/null 2>&1
825			then	s=`nm ${tmp}s.o | sed -e /$l/!d -e 's/.*[^_]\('${SYS_PREFIX}'_*'${l}'[0-9]*\).*/\1/'`
826				case $s in
827				$l)	;;
828				*)	echo "#include <sys/syscall.h>
829#ifndef SYS_${s}
830(
831#else
832int i;
833#endif" > ${tmp}s.c
834					if	$cc -c ${tmp}s.c >/dev/null 2>&1
835					then	def=1
836						map=$s
837						echo "#define SYS_${l}	SYS_${s}" >> $tmp.f
838						echo iffe: test: $l "=>" $s >&$stderr
839					fi
840					;;
841				esac
842			else	def=1
843			fi
844			;;
845		esac
846		;;
847	esac
848	if	rm -f $tmp.x &&
849		$cc -Dsyscall=$l $opt $static -o $tmp.x $tmp.c </dev/null >/dev/null &&
850		$executable $tmp.x
851	then	case $sys in
852		att|bsd|dynamic|mangle)
853			case $SYS_HEADER in
854			"")	case $num in
855				"")	;;
856				0)	echo "#define $u		$l" >> $tmp.r
857					;;
858				*)	case $sys in
859					att|bsd)	echo "#define SYS$m		$num" >> $tmp.s ;;
860					esac
861					call=sys
862					;;
863				esac
864				;;
865			*)	case `$cc -E -DSYS=SYS$m ${tmp}s.c </dev/null | grep SYS$m` in
866				"")	call=sys
867					;;
868				*)	case $num in
869					[0-9]*)	call=nosys ;;
870					*)	call=nolib ;;
871					esac
872					;;
873				esac
874				;;
875			esac
876			;;
877		*)	echo "#define _error_$u		1" >> $tmp.m
878			;;
879		esac
880	else	case $num in
881		[0-9]*)	call=nosys ;;
882		*)	call=nolib ;;
883		esac
884	fi
885	case $call in
886	nolib|nosys)
887		{
888		echo "#ifndef SYS$m"
889		echo "#define SYS$m	(-1)"
890		echo "#endif"
891		} >> $tmp.f
892		;;
893	*)	case $def:$SYS_HEADER in
894		:?*)	echo "#include <${SYS_HEADER}>
895#ifdef SYS${m}
896(
897#else
898int i;
899#endif" > ${tmp}s.c
900			if	$cc -c ${tmp}s.c >/dev/null 2>&1
901			then	echo "#define SYS$m		(-1)" >> $tmp.f
902			fi
903			;;
904		esac
905		;;
906	esac
907	case $call in
908	nolib|nosys)
909		echo "#define _nosys$m		1" >> $tmp.u
910		;;
911	esac
912	case $call in
913	nolib)	;;
914	nosys)	case $alt in
915		"")
916			echo "#define $u($a)		NOSYS()" >> $tmp.u
917			echo "#define _stub_$w		1" >> $tmp.n
918			w=_no_$w
919			;;
920		*)	case $shell in
921			*u*)	typeset -u y=$alt ;;
922			*)	y=`echo $alt | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ` ;;
923			esac
924			echo "#define $u			$y" >> $tmp.r
925			;;
926		esac
927		;;
928	sys)	{
929		echo "#define SYS3D$m		$inx"
930		case $sys in
931		dynamic|mangle)
932			echo "#ifndef SYS$m"
933			echo "#define SYS$m		(-1)"
934			echo "#endif"
935			;;
936		esac
937		} >> $tmp.f
938		case $shell in
939		*n*)	((inx=inx+1)) ;;
940		*)	inx=`expr $inx + 1` ;;
941		esac
942		{
943		echo "#undef  $u"
944		echo "#define $u($a)		${cast}SYSCALL(SYS3D$m${A:+,}$A)${tsac}"
945		} >> $tmp.u
946		case $p in
947		void)	set $r
948			c=0
949			;;
950		*)	IFS=$comma
951			set $p
952			c=$#
953			set $r${comma}$p
954			IFS=$ifs
955			;;
956		esac
957		d=
958		s=
959		for i in 0 1 2 3 4 5
960		do	case $# in
961			0)	break ;;
962			esac
963			case $1 in
964			*"mode_t")
965				d=${d}${s}A_MODE
966				;;
967			*"size_t")
968				d=${d}${s}A_SIZE
969				;;
970			*"char* const*"|*"char**")
971				d=${d}${s}A_VECTOR
972				;;
973			*"char*")
974				case $r:$2 in
975				*"ssize_t":*"size_t")
976					d=${d}${s}A_OUTPUT,A_SIZE
977					shift
978					;;
979				*)	d=${d}${s}A_STRING
980					;;
981				esac
982				;;
983			*"const void*")
984				d=${d}${s}A_INPUT
985				;;
986			*"void*")
987				d=${d}${s}A_OUTPUT
988				;;
989			*"struct stat*"|*"int*")
990				d=${d}${s}A_OUTPUT
991				;;
992			*"...")	case $l in
993				*open)	d=${d}${s}A_MODE ;;
994				*)	d=${d}${s}A_POINTER ;;
995				esac
996				;;
997			*"[]"|*"*")
998				d=${d}${s}A_POINTER
999				;;
1000			*)	d=${d}${s}A_NUMBER
1001				;;
1002			esac
1003			s=,
1004			shift
1005		done
1006		case $sys in
1007		mangle)	x="$u, "
1008			echo "#undef $u
1009extern long $u();" >> $tmp.g
1010			;;
1011		*)	x="0, "
1012			;;
1013		esac
1014		i=$l
1015		case $i in
1016		_exit)	{
1017			echo "static int hit;"
1018			echo "void _exit(code) int code; { if (hit++) abort(); __exit(code); }"
1019			echo "main() { return 0; }"
1020			} > ${tmp}h.c
1021			$cc -o ${tmp}h.x ${tmp}h.c && ./${tmp}h.x && i=_$i
1022			;;
1023		*)	case $map:$weak in
1024			?*:*)	i=$map ;;
1025			:?*)	i=$w ;;
1026			esac
1027			;;
1028		esac
1029		echo "{ ${x}SYS$m, (-1), MSG_CALL(MSG_$q), $c, \"$i\", { $d } }," >> $tmp.t
1030		;;
1031	*)	case $num in
1032		?*)	echo "extern $r			$u($p);" >> $tmp.e ;;
1033		esac
1034		;;
1035	esac
1036	i=
1037	case $call in
1038	nolib)	;;
1039	nosys)	;;
1040	*)	case $l:$w in
1041		_*)	;;
1042		*:_*)	i=1 ;;
1043		esac
1044		;;
1045	esac
1046	case $i in
1047	"")	echo "#define ${l}3d		$w" >> $tmp.d
1048		;;
1049	*)	case $p in
1050		void)	d=void o= va= vo=
1051			;;
1052		*)	IFS=$comma
1053			set $p
1054			IFS=$ifs
1055			d=
1056			o=
1057			va=$a
1058			vo=
1059			vx=
1060			for i in a b c d e f
1061			do	case $# in
1062				0)	break ;;
1063				esac
1064				case $1 in
1065				*...)	d="$d, ..."
1066					va=va_alist
1067					case $l in
1068					fcntl)	t='void*' ;;
1069					*)	t=int ;;
1070					esac
1071					vo="__OTORP__($o) $t $i; va_list ap; __VA_START__(ap,$vx); __OTORP__("
1072					o=va_dcl
1073					IFS=$comma
1074					set $p
1075					IFS=$ifs
1076					for i in a b c d e f
1077					do	case $1 in
1078						*...)	break ;;
1079						esac
1080						vo="$vo$i = va_arg(ap, $1);"
1081						shift
1082					done
1083					case $l in
1084					fcntl)	vo="$vo) $i = va_arg(ap, void*); va_end(ap);" ;;
1085					*)	vo="$vo) $i = va_arg(ap, int); va_end(ap);" ;;
1086					esac
1087					break
1088					;;
1089				*\[\])	case $shell in
1090					*e*)	k=${1%\[\]} ;;
1091					*)	k=`echo $1 | sed 's/\[\]\$//'` ;;
1092					esac
1093					i="$i"'[]'
1094					;;
1095				*)	k=$1
1096					;;
1097				esac
1098				vx=$i
1099				case $d in
1100				"")	d="$k $i"
1101					o="$k $i;"
1102					;;
1103				*)	d="$d, $k $i"
1104					o="$o $k $i;"
1105					;;
1106				esac
1107				shift
1108			done
1109			;;
1110		esac
1111		case $o in
1112		?*)	o="__OTORP__($o) " ;;
1113		esac
1114		case $r in
1115		void)	ft='*' fr= ;;
1116		*)	ft= fr='return ' ;;
1117		esac
1118		z="$r $w"
1119		echo "#define ${U}3D		${z}" >> $tmp.d
1120		W=
1121		case $w in
1122		__libc_*) W=" $r __$l __PARAM__(($d),($va)) $o{$vo $fr$w($a); }" ;;
1123		__*) W=" $r _$l __PARAM__(($d),($va)) $o{$vo $fr$w($a); }" ;;
1124		esac
1125		echo "#define ${l}3d		${ft}_3d_stub${m} = 0; extern __MANGLE__ $r $w __PROTO__(($p)); $r $l __PARAM__(($d),($va)) $o{$vo $fr$w($a); }$W $z" >> $tmp.d
1126		;;
1127	esac
1128	case $call:$sym in
1129	nolib:*);;
1130	nosys:*);;
1131	*:)	sym="$w" fun="$w()" ;;
1132	*)	sym="$sym,$nl	$w" fun="$fun,$nl	$w()" ;;
1133	esac
1134done
1135IFS=$ifs
1136if	test -f $tmp.t
1137then	{
1138	echo "};
1139"
1140	case $sys in
1141	dynamic|mangle)
1142		echo "#define SYSCALL3D(z,a,b,c,d,e,f)	(*sys_trace[z].func)(a,b,c,d,e,f)"
1143		case $sys in
1144		dynamic|mangle)
1145			echo "#undef	fstat"
1146			case $_SYS_fstat_VER in
1147			"")	echo "#define fstat(a,b)	SYSCALL3D(SYS3D_fstat,a,b,0,0,0,0)" ;;
1148			*)	echo "#define fstat(a,b)	SYSCALL3D(SYS3D_fstat,$_SYS_fstat_VER,a,b,0,0,0)" ;;
1149			esac
1150			echo "#undef	write"
1151			echo "#define write(a,b,c)	SYSCALL3D(SYS3D_write,a,b,c,0,0,0)"
1152			;;
1153		esac
1154		;;
1155	esac
1156echo "
1157#else
1158"
1159echo "
1160#define SYSCALL		syscall3d
1161
1162extern long	syscall3d(int, ...);
1163
1164#endif"
1165	} >> $tmp.t
1166else	echo "#undef	SYSTRACE3D" > $tmp.t
1167fi
1168echo "
1169#endif" >> $tmp.e
1170echo "
1171#ifdef	SYSENT3D
1172
1173extern int
1174	$fun;
1175
1176int			(*_3d_sysent[])() = {
1177	$sym
1178};
1179
1180#else
1181
1182extern int		(*_3d_sysent[])();
1183#define PULL_IN_3D		int (*pull_in_3d)() = _3d_sysent[0];
1184" >> $tmp.m
1185for ver in $vers
1186do	vercall=
1187	case $ver in
1188	*[mM][kK][nN][oO][dD]*)	vercall=MKNOD ;;
1189	*[sS][tT][aA][tT]*)	vercall=STAT ;;
1190	esac
1191	case $vercall in
1192	?*)	echo "#define _3D_${vercall}_VER	${ver}"
1193		for alt in $ver64
1194		do	case $alt in
1195			*${vercall}64*) echo "#define _3D_${vercall}64_VER	${alt}" ;;
1196			esac
1197		done
1198		;;
1199	esac
1200done >> $tmp.f
1201for i in $tmpsuf
1202do	if	test -f $tmp.$i
1203	then	echo
1204		cat $tmp.$i
1205	fi
1206done
1207echo
1208