xref: /titanic_41/usr/src/lib/libshell/common/features/math.sh (revision 2b24ab6b3865caeede9eeb9db6b83e1d89dcd1ea)
1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#          Copyright (c) 1982-2008 AT&T Intellectual Property          #
5#                      and is licensed under the                       #
6#                  Common Public License, Version 1.0                  #
7#                    by AT&T Intellectual Property                     #
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) 2008-08-29
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: check long double
39
40eval `iffe $iffeflags -c "$cc" - typ long.double 2>&$stderr`
41
42: read the table
43
44exec < $table
45while	read type args name aka comment
46do	case $type in
47	[fi])	names="$names $name"
48		tests="$tests,$name"
49		case $_typ_long_double in
50		1)	tests="$tests,${name}l" ;;
51		esac
52		eval TYPE_$name=$type ARGS_$name=$args AKA_$name=$aka
53		;;
54	esac
55done
56
57: check the math library
58
59eval `iffe $iffeflags -c "$cc" - lib $tests $iffehdrs $iffelibs 2>&$stderr`
60lib=
61for name in $names
62do	eval x='$'_lib_${name}l y='$'_lib_${name}
63	case $x in
64	1)	lib="$lib,${name}l" ;;
65	esac
66	case $y in
67	1)	case $x in
68		'')	lib="$lib,${name}" ;;
69		esac
70		;;
71	esac
72done
73eval `iffe $iffeflags -c "$cc" - dat,npt,mac $lib $iffehdrs $iffelibs 2>&$stderr`
74
75cat <<!
76#pragma prototyped
77
78/* : : generated by $command from $table : : */
79
80typedef Sfdouble_t (*Math_f)(Sfdouble_t,...);
81
82!
83echo "#include <ast_standards.h>"
84echo "#include <math.h>"
85case $_hdr_ieeefp in
861)	echo "#include <ieeefp.h>"
87	echo
88	;;
89esac
90
91: generate the intercept functions and table entries
92
93nl='
94'
95ht='	'
96tab=
97for name in $names
98do	eval x='$'_lib_${name}l y='$'_lib_${name} r='$'TYPE_${name} a='$'ARGS_${name} aka='$'AKA_${name}
99	case $x:$y in
100	1:*)	f=${name}l
101		t=Sfdouble_t
102		local=
103		;;
104	*:1)	f=${name}
105		t=double
106		local=$_typ_long_double
107		;;
108	*)	continue
109		;;
110	esac
111	eval n='$'_npt_$f m='$'_mac_$f d='$'_dat_$f
112	case $r in
113	i)	L=int r=int R=1 ;;
114	*)	L=Sfdouble_t r=$t R=0 ;;
115	esac
116	case $d:$m:$n in
117	1:*:*|*:1:*)
118		;;
119	*:*:1)	code="extern $r $f("
120		sep=
121		for p in 1 2 3 4 5 6 7
122		do	code="$code${sep}$t"
123			case $a in
124			$p)	break ;;
125			esac
126			sep=","
127		done
128		code="$code);"
129		echo "$code"
130		;;
131	esac
132	case $local:$m:$n:$d in
133	1:*:*:*|*:1:*:*|*:*:1:)
134		args=
135		code="static $L local_$f("
136		sep=
137		for p in 1 2 3 4 5 6 7 8 9
138		do	args="$args${sep}a$p"
139			code="$code${sep}Sfdouble_t a$p"
140			case $a in
141			$p)	break ;;
142			esac
143			sep=","
144		done
145		code="$code){return $f($args);}"
146		echo "$code"
147		f=local_$f
148		;;
149	esac
150	for x in $name $aka
151	do	tab="$tab$nl$ht\"\\0${R}${a}${x}\",$ht(Math_f)$f,"
152	done
153done
154tab="$tab$nl$ht\"\",$ht$ht(Math_f)0"
155
156cat <<!
157
158/*
159 * first byte is two-digit octal number.  Last digit is number of args
160 * first digit is 0 if return value is double, 1 for integer
161 */
162const struct mathtab shtab_math[] =
163{$tab
164};
165!
166