xref: /titanic_50/usr/src/lib/libshell/common/features/math.sh (revision 64cfc8ed0c5da83f827c424588bfe709e3fb5a7a)
1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#           Copyright (c) 1982-2007 AT&T Knowledge Ventures            #
5#                      and is licensed under the                       #
6#                  Common Public License, Version 1.0                  #
7#                      by AT&T Knowledge Ventures                      #
8#                                                                      #
9#                A copy of the License is available at                 #
10#            http://www.opensource.org/licenses/cpl1.0.txt             #
11#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
12#                                                                      #
13#              Information and Software Systems Research               #
14#                            AT&T Research                             #
15#                           Florham Park NJ                            #
16#                                                                      #
17#                  David Korn <dgk@research.att.com>                   #
18#                                                                      #
19########################################################################
20: generate the ksh math builtin table
21: include math.tab
22
23# @(#)math.sh (AT&T Research) 2007-02-02
24
25command=$0
26iffeflags="-n -v -F ast_standards.h"
27iffehdrs="math.h ieeefp.h"
28iffelibs="-lm"
29table=/dev/null
30
31eval $1
32shift
33table=$1
34
35names=
36tests=
37
38: read the table
39
40exec < $table
41while	read type args name aka comment
42do	case $type in
43	[fi])	names="$names $name"
44		tests="$tests,$name,${name}l"
45		eval TYPE_$name=$type ARGS_$name=$args AKA_$name=$aka
46		;;
47	esac
48done
49
50: check the math library
51
52eval `iffe $iffeflags -c "$cc" - typ long.double : lib $tests $iffehdrs $iffelibs 2>&$stderr`
53lib=
54for name in $names
55do	eval x='$'_lib_${name}l y='$'_lib_${name}
56	case $x in
57	1)	lib="$lib,${name}l" ;;
58	esac
59	case $y in
60	1)	case $x in
61		'')	lib="$lib,${name}" ;;
62		esac
63		;;
64	esac
65done
66eval `iffe $iffeflags -c "$cc" - dat,npt,mac $lib $iffehdrs $iffelibs 2>&$stderr`
67
68cat <<!
69#pragma prototyped
70
71/* : : generated by $command from $table : : */
72
73typedef Sfdouble_t (*Math_f)(Sfdouble_t,...);
74
75!
76echo "#include <ast_standards.h>"
77echo "#include <math.h>"
78case $_hdr_ieeefp in
791)	echo "#include <ieeefp.h>"
80	echo
81	;;
82esac
83
84: generate the intercept functions and table entries
85
86nl='
87'
88ht='	'
89tab=
90for name in $names
91do	eval x='$'_lib_${name}l y='$'_lib_${name} r='$'TYPE_${name} a='$'ARGS_${name} aka='$'AKA_${name}
92	case $x:$y in
93	1:*)	f=${name}l
94		t=Sfdouble_t
95		local=
96		;;
97	*:1)	f=${name}
98		t=double
99		local=$_typ_long_double
100		;;
101	*)	continue
102		;;
103	esac
104	eval n='$'_npt_$f m='$'_mac_$f d='$'_dat_$f
105	case $r in
106	i)	L=int r=int R=1 ;;
107	*)	L=Sfdouble_t r=$t R=0 ;;
108	esac
109	case $d:$m:$n in
110	1:*:*|*:1:*)
111		;;
112	*:*:1)	code="extern $r $f("
113		sep=
114		for p in 1 2 3 4 5 6 7
115		do	code="$code${sep}$t"
116			case $a in
117			$p)	break ;;
118			esac
119			sep=","
120		done
121		code="$code);"
122		echo "$code"
123		;;
124	esac
125	case $local:$m:$n:$d in
126	1:*:*:*|*:1:*:*|*:*::)
127		args=
128		code="static $L local_$f("
129		sep=
130		for p in 1 2 3 4 5 6 7 8 9
131		do	args="$args${sep}a$p"
132			code="$code${sep}Sfdouble_t a$p"
133			case $a in
134			$p)	break ;;
135			esac
136			sep=","
137		done
138		code="$code){return $f($args);}"
139		echo "$code"
140		f=local_$f
141		;;
142	esac
143	for x in $name $aka
144	do	tab="$tab$nl$ht\"\\0${R}${a}${x}\",$ht(Math_f)$f,"
145	done
146done
147tab="$tab$nl$ht\"\",$ht$ht(Math_f)0"
148
149cat <<!
150
151/*
152 * first byte is two-digit octal number.  Last digit is number of args
153 * first digit is 0 if return value is double, 1 for integer
154 */
155const struct mathtab shtab_math[] =
156{$tab
157};
158!
159