xref: /illumos-gate/usr/src/contrib/ast/src/cmd/ksh93/mamexec (revision b30d193948be5a7794d7ae3ba0ed9c2f72c88e0f)
1*b30d1939SAndy Fiddaman:
2*b30d1939SAndy Fiddaman#
3*b30d1939SAndy Fiddaman# Glenn Fowler
4*b30d1939SAndy Fiddaman# AT&T Bell Laboratories
5*b30d1939SAndy Fiddaman#
6*b30d1939SAndy Fiddaman# make abstract machine executor with state
7*b30d1939SAndy Fiddaman#
8*b30d1939SAndy Fiddaman# @(#)mamexec (gsf@research.att.com) 07/17/94
9*b30d1939SAndy Fiddaman#
10*b30d1939SAndy Fiddaman# mamexec [ -dfins ] [ target ... ] < mam-file
11*b30d1939SAndy Fiddaman#
12*b30d1939SAndy Fiddaman# NOTE: variables defined in this script may conflict with
13*b30d1939SAndy Fiddaman#	mam-file variables
14*b30d1939SAndy Fiddaman#
15*b30d1939SAndy Fiddaman
16*b30d1939SAndy Fiddaman_command_=mamexec
17*b30d1939SAndy Fiddaman_debug_=:
18*b30d1939SAndy Fiddaman_diff_=.
19*b30d1939SAndy Fiddaman_error_='exit 1'
20*b30d1939SAndy Fiddaman_exec_=eval
21*b30d1939SAndy Fiddaman_force_=
22*b30d1939SAndy Fiddaman_list_=
23*b30d1939SAndy Fiddaman_same_=.
24*b30d1939SAndy Fiddaman_set_=
25*b30d1939SAndy Fiddaman_silent_=
26*b30d1939SAndy Fiddaman_state_list_=MAMlist
27*b30d1939SAndy Fiddaman_state_time_=MAMtime
28*b30d1939SAndy Fiddaman_tmp_=/tmp/mam.$$.mam
29*b30d1939SAndy Fiddaman
30*b30d1939SAndy Fiddamantrap 'rm -f $_tmp_' 0
31*b30d1939SAndy Fiddamantrap 'exit 2' 1 2
32*b30d1939SAndy Fiddamanwhile	:
33*b30d1939SAndy Fiddamando	case $# in
34*b30d1939SAndy Fiddaman	0)	break ;;
35*b30d1939SAndy Fiddaman	esac
36*b30d1939SAndy Fiddaman	case $1 in
37*b30d1939SAndy Fiddaman	--)	shift
38*b30d1939SAndy Fiddaman		break
39*b30d1939SAndy Fiddaman		;;
40*b30d1939SAndy Fiddaman	-*)	case $1 in
41*b30d1939SAndy Fiddaman		-*[abceghjklmopqrtuvwxyz_A-Z0-9]*) # [!...] not portable
42*b30d1939SAndy Fiddaman			echo "Usage: $_command_ [ -dfins ] [ target ... ] < mam-file" >&2; exit 2
43*b30d1939SAndy Fiddaman			;;
44*b30d1939SAndy Fiddaman		*)	case $1 in
45*b30d1939SAndy Fiddaman			*d*)	_debug_="eval echo $_command_: debug: >&2" ;;
46*b30d1939SAndy Fiddaman			esac
47*b30d1939SAndy Fiddaman			case $1 in
48*b30d1939SAndy Fiddaman			*f*)	_force_=1 ;;
49*b30d1939SAndy Fiddaman			esac
50*b30d1939SAndy Fiddaman			case $1 in
51*b30d1939SAndy Fiddaman			*i*)	_error_=: ;;
52*b30d1939SAndy Fiddaman			esac
53*b30d1939SAndy Fiddaman			case $1 in
54*b30d1939SAndy Fiddaman			*n*)	_exec_=echo ;;
55*b30d1939SAndy Fiddaman			esac
56*b30d1939SAndy Fiddaman			case $1 in
57*b30d1939SAndy Fiddaman			*s*)	_silent_=1 ;;
58*b30d1939SAndy Fiddaman			esac
59*b30d1939SAndy Fiddaman			;;
60*b30d1939SAndy Fiddaman		esac
61*b30d1939SAndy Fiddaman		;;
62*b30d1939SAndy Fiddaman	*)	break
63*b30d1939SAndy Fiddaman		;;
64*b30d1939SAndy Fiddaman	esac
65*b30d1939SAndy Fiddaman	shift
66*b30d1939SAndy Fiddamandone
67*b30d1939SAndy Fiddaman_select_=
68*b30d1939SAndy Fiddamanwhile	:
69*b30d1939SAndy Fiddamando	case $# in
70*b30d1939SAndy Fiddaman	0)	break ;;
71*b30d1939SAndy Fiddaman	esac
72*b30d1939SAndy Fiddaman	case $1 in
73*b30d1939SAndy Fiddaman	*=*)	eval $1
74*b30d1939SAndy Fiddaman		;;
75*b30d1939SAndy Fiddaman	*)	case $_select_ in
76*b30d1939SAndy Fiddaman		"")	_select_=$1 ;;
77*b30d1939SAndy Fiddaman		*)	_select_="$_select_|$1" ;;
78*b30d1939SAndy Fiddaman		esac
79*b30d1939SAndy Fiddaman		;;
80*b30d1939SAndy Fiddaman	esac
81*b30d1939SAndy Fiddaman	shift
82*b30d1939SAndy Fiddamandone
83*b30d1939SAndy Fiddamancase $_select_ in
84*b30d1939SAndy Fiddaman"")	_select_="*" ;;
85*b30d1939SAndy Fiddamanesac
86*b30d1939SAndy Fiddaman(set -e; false || true) && _set_=e || echo $_command_: command errors ignored because of shell botch >&2
87*b30d1939SAndy Fiddamancase $_silent_ in
88*b30d1939SAndy Fiddaman"")	_set_=x$_set_
89*b30d1939SAndy Fiddaman	;;
90*b30d1939SAndy Fiddaman*)	case $_exec_ in
91*b30d1939SAndy Fiddaman	"echo")	_exec_=: ;;
92*b30d1939SAndy Fiddaman	esac
93*b30d1939SAndy Fiddaman	;;
94*b30d1939SAndy Fiddamanesac
95*b30d1939SAndy Fiddamancase $_exec_ in
96*b30d1939SAndy Fiddaman"eval")	_begin_="("
97*b30d1939SAndy Fiddaman	_end_=") </dev/null"
98*b30d1939SAndy Fiddaman	case $_set_ in
99*b30d1939SAndy Fiddaman	?*)	_set_="set -$_set_;" ;;
100*b30d1939SAndy Fiddaman	esac
101*b30d1939SAndy Fiddaman	;;
102*b30d1939SAndy Fiddaman*)	_set_=
103*b30d1939SAndy Fiddaman	;;
104*b30d1939SAndy Fiddamanesac
105*b30d1939SAndy Fiddaman
106*b30d1939SAndy Fiddamanif	test -f $_state_list_ -a -f $_state_time_
107*b30d1939SAndy Fiddamanthen	mamstate $_state_list_ < $_state_list_ | sort > $_tmp_
108*b30d1939SAndy Fiddaman	for _i_ in `comm -12 $_state_time_ $_tmp_ | sed 's/ .*//'`
109*b30d1939SAndy Fiddaman	do	case $_same_ in
110*b30d1939SAndy Fiddaman		.)	_same_=$_i_ ;;
111*b30d1939SAndy Fiddaman		*)	_same_="$_same_|$_i_" ;;
112*b30d1939SAndy Fiddaman		esac
113*b30d1939SAndy Fiddaman	done
114*b30d1939SAndy Fiddamanfi
115*b30d1939SAndy Fiddaman
116*b30d1939SAndy Fiddaman_index_=_
117*b30d1939SAndy Fiddaman_match_=
118*b30d1939SAndy Fiddamancase `(echo ok | (read -r a; echo $a) 2>/dev/null)` in
119*b30d1939SAndy Fiddamanok)	_read_='read -r'
120*b30d1939SAndy Fiddaman	;;
121*b30d1939SAndy Fiddaman*)	# read strips \ -- thanks a lot
122*b30d1939SAndy Fiddaman	# tmp file avoids char at a time read
123*b30d1939SAndy Fiddaman	_read_=read
124*b30d1939SAndy Fiddaman	sed 's/\\/\\\\/g' > $_tmp_
125*b30d1939SAndy Fiddaman	exec < $_tmp_
126*b30d1939SAndy Fiddaman	rm -f $_tmp_
127*b30d1939SAndy Fiddaman	;;
128*b30d1939SAndy Fiddamanesac
129*b30d1939SAndy Fiddaman_old_=1
130*b30d1939SAndy Fiddaman_ifs_=$IFS
131*b30d1939SAndy Fiddamanwhile	IFS=' '; $_read_ _op_ _arg_ _val_
132*b30d1939SAndy Fiddamando	IFS=$_ifs_
133*b30d1939SAndy Fiddaman	case $_op_ in
134*b30d1939SAndy Fiddaman	"note")	continue
135*b30d1939SAndy Fiddaman		;;
136*b30d1939SAndy Fiddaman	"info")	case $_arg_ in
137*b30d1939SAndy Fiddaman		"mam")	_old_= ;;
138*b30d1939SAndy Fiddaman		esac
139*b30d1939SAndy Fiddaman		continue
140*b30d1939SAndy Fiddaman		;;
141*b30d1939SAndy Fiddaman	"setv") eval _data_='$'$_arg_
142*b30d1939SAndy Fiddaman		case $_index_:$_data_ in
143*b30d1939SAndy Fiddaman		__*:*|*:)
144*b30d1939SAndy Fiddaman			case $_exec_ in
145*b30d1939SAndy Fiddaman			"eval")	;;
146*b30d1939SAndy Fiddaman			*)	echo "$_arg_=$_val_" ;;
147*b30d1939SAndy Fiddaman			esac
148*b30d1939SAndy Fiddaman			eval $_arg_="$_val_" "</dev/null"
149*b30d1939SAndy Fiddaman			;;
150*b30d1939SAndy Fiddaman		esac
151*b30d1939SAndy Fiddaman		case $_arg_:$mam_cc_L in
152*b30d1939SAndy Fiddaman		"CC:")	(
153*b30d1939SAndy Fiddaman			set -
154*b30d1939SAndy Fiddaman			mkdir /tmp/mam$$
155*b30d1939SAndy Fiddaman			cd /tmp/mam$$
156*b30d1939SAndy Fiddaman			echo 'main(){return 0;}' > main.c
157*b30d1939SAndy Fiddaman			code=1
158*b30d1939SAndy Fiddaman			if	$CC -c main.c 2>/dev/null
159*b30d1939SAndy Fiddaman			then	if	$CC -L. main.o -lc 2>/dev/null
160*b30d1939SAndy Fiddaman				then	$CC -L. main.o -lc > libc.a 2>/dev/null || code=0
161*b30d1939SAndy Fiddaman				fi
162*b30d1939SAndy Fiddaman			fi
163*b30d1939SAndy Fiddaman			cd /tmp
164*b30d1939SAndy Fiddaman			rm -rf /tmp/mam$$
165*b30d1939SAndy Fiddaman			exit $code
166*b30d1939SAndy Fiddaman			) </dev/null && mam_cc_L=' '
167*b30d1939SAndy Fiddaman			;;
168*b30d1939SAndy Fiddaman		esac
169*b30d1939SAndy Fiddaman		continue
170*b30d1939SAndy Fiddaman		;;
171*b30d1939SAndy Fiddaman	"make")	eval _name_$_index_=$_name_
172*b30d1939SAndy Fiddaman		eval _prev_$_index_=$_prev_
173*b30d1939SAndy Fiddaman		eval _cmds_$_index_='"$_cmds_"'
174*b30d1939SAndy Fiddaman		eval _attr_$_index_=$_attr_
175*b30d1939SAndy Fiddaman		eval _name_=$_arg_
176*b30d1939SAndy Fiddaman		_prev_=$_index_
177*b30d1939SAndy Fiddaman		case " $_val_ " in
178*b30d1939SAndy Fiddaman		*" metarule "*)	_attr_=m$_attr_ ;;
179*b30d1939SAndy Fiddaman		esac
180*b30d1939SAndy Fiddaman		_cmds_=
181*b30d1939SAndy Fiddaman		eval "	case \"$_name_\" in
182*b30d1939SAndy Fiddaman			$_select_)
183*b30d1939SAndy Fiddaman				case \"$_select_\" in
184*b30d1939SAndy Fiddaman				\"*\")	_select_=$_name_ ;;
185*b30d1939SAndy Fiddaman				esac
186*b30d1939SAndy Fiddaman				_match_=1
187*b30d1939SAndy Fiddaman				_attr_=x$_attr_
188*b30d1939SAndy Fiddaman				;;
189*b30d1939SAndy Fiddaman			esac"
190*b30d1939SAndy Fiddaman		case $_force_ in
191*b30d1939SAndy Fiddaman		"")	eval "	case \"$_name_\" in
192*b30d1939SAndy Fiddaman				$_diff_)_attr_=u$_attr_ ;;
193*b30d1939SAndy Fiddaman				$_same_);;
194*b30d1939SAndy Fiddaman				*)	_attr_=u$_attr_ ;;
195*b30d1939SAndy Fiddaman				esac"
196*b30d1939SAndy Fiddaman			;;
197*b30d1939SAndy Fiddaman		*)	_attr_=u$_attr_
198*b30d1939SAndy Fiddaman			;;
199*b30d1939SAndy Fiddaman		esac
200*b30d1939SAndy Fiddaman		case $_attr_ in
201*b30d1939SAndy Fiddaman		*u*)	case $_diff_ in
202*b30d1939SAndy Fiddaman			.)	_diff_=$_name_ ;;
203*b30d1939SAndy Fiddaman			*)	_diff_="$_diff_|$_name_" ;;
204*b30d1939SAndy Fiddaman			esac
205*b30d1939SAndy Fiddaman			;;
206*b30d1939SAndy Fiddaman		esac
207*b30d1939SAndy Fiddaman		_index_=_$_index_
208*b30d1939SAndy Fiddaman		eval _name_$_index_=$_name_
209*b30d1939SAndy Fiddaman		eval _prev_$_index_=$_prev_
210*b30d1939SAndy Fiddaman		eval _cmds_$_index_=$_cmds_
211*b30d1939SAndy Fiddaman		eval _attr_$_index_=$_attr_
212*b30d1939SAndy Fiddaman		eval _list_='"'"$_list_"'
213*b30d1939SAndy Fiddaman'$_name_'"'
214*b30d1939SAndy Fiddaman		continue
215*b30d1939SAndy Fiddaman		;;
216*b30d1939SAndy Fiddaman	"prev")	case $_force_ in
217*b30d1939SAndy Fiddaman		"")	eval "	case \"$_arg_\" in
218*b30d1939SAndy Fiddaman				$_diff_)_attr_=u$_attr_ ;;
219*b30d1939SAndy Fiddaman				$_same_)	;;
220*b30d1939SAndy Fiddaman				*)	_attr_=u$_attr_ ;;
221*b30d1939SAndy Fiddaman				esac"
222*b30d1939SAndy Fiddaman			;;
223*b30d1939SAndy Fiddaman		*)	_attr_=u$_attr_
224*b30d1939SAndy Fiddaman			;;
225*b30d1939SAndy Fiddaman		esac
226*b30d1939SAndy Fiddaman		continue
227*b30d1939SAndy Fiddaman		;;
228*b30d1939SAndy Fiddaman	esac
229*b30d1939SAndy Fiddaman	case $_index_ in
230*b30d1939SAndy Fiddaman	_)	echo $_op_: missing make op >&2; continue ;;
231*b30d1939SAndy Fiddaman	esac
232*b30d1939SAndy Fiddaman	case $_op_ in
233*b30d1939SAndy Fiddaman	"attr")	case $_val_ in
234*b30d1939SAndy Fiddaman		"meta"|"suff")	_attr_=m$_attr_ ;;
235*b30d1939SAndy Fiddaman		esac
236*b30d1939SAndy Fiddaman		;;
237*b30d1939SAndy Fiddaman	"exec"|"....")
238*b30d1939SAndy Fiddaman		case $_old_ in
239*b30d1939SAndy Fiddaman		"")	_arg_=$_val_
240*b30d1939SAndy Fiddaman			;;
241*b30d1939SAndy Fiddaman		*)	case $_val_ in
242*b30d1939SAndy Fiddaman			?*)	_arg_="$_arg_ $_val_" ;;
243*b30d1939SAndy Fiddaman			esac
244*b30d1939SAndy Fiddaman			;;
245*b30d1939SAndy Fiddaman		esac
246*b30d1939SAndy Fiddaman		case $_cmds_ in
247*b30d1939SAndy Fiddaman		"")	_cmds_=$_arg_
248*b30d1939SAndy Fiddaman			;;
249*b30d1939SAndy Fiddaman		*)	_cmds_="$_cmds_
250*b30d1939SAndy Fiddaman$_arg_"
251*b30d1939SAndy Fiddaman			;;
252*b30d1939SAndy Fiddaman		esac
253*b30d1939SAndy Fiddaman		;;
254*b30d1939SAndy Fiddaman	"done")	eval _arg_=$_arg_
255*b30d1939SAndy Fiddaman		_prop_=
256*b30d1939SAndy Fiddaman		case $_arg_ in
257*b30d1939SAndy Fiddaman		$_name_)case $_attr_ in
258*b30d1939SAndy Fiddaman			*m*)	;;
259*b30d1939SAndy Fiddaman			*x*u*|*u*x*)
260*b30d1939SAndy Fiddaman				case $_cmds_ in
261*b30d1939SAndy Fiddaman				"")	case $_attr_ in
262*b30d1939SAndy Fiddaman					*u*)	_prop_=u ;;
263*b30d1939SAndy Fiddaman					esac
264*b30d1939SAndy Fiddaman					;;
265*b30d1939SAndy Fiddaman				*)	$_exec_ "$_begin_$_set_$_cmds_$_end_" ||
266*b30d1939SAndy Fiddaman					{
267*b30d1939SAndy Fiddaman					_code_=$?
268*b30d1939SAndy Fiddaman					case $_set_ in
269*b30d1939SAndy Fiddaman					*-*e*)	;;
270*b30d1939SAndy Fiddaman					*)	case $_cmds_ in
271*b30d1939SAndy Fiddaman						*if*then*fi*|"||")	_code_=0 ;;
272*b30d1939SAndy Fiddaman						esac
273*b30d1939SAndy Fiddaman						;;
274*b30d1939SAndy Fiddaman					esac
275*b30d1939SAndy Fiddaman					case $_code_ in
276*b30d1939SAndy Fiddaman					0)	;;
277*b30d1939SAndy Fiddaman					*)	echo "*** exit code $_code_ making $_name_" >&2
278*b30d1939SAndy Fiddaman						$_error_
279*b30d1939SAndy Fiddaman						;;
280*b30d1939SAndy Fiddaman					esac
281*b30d1939SAndy Fiddaman					}
282*b30d1939SAndy Fiddaman					_prop_=u
283*b30d1939SAndy Fiddaman					;;
284*b30d1939SAndy Fiddaman				esac
285*b30d1939SAndy Fiddaman				;;
286*b30d1939SAndy Fiddaman			esac
287*b30d1939SAndy Fiddaman			_index_=$_prev_
288*b30d1939SAndy Fiddaman			eval _name_='$'_name_$_index_
289*b30d1939SAndy Fiddaman			eval _prev_='$'_prev_$_index_
290*b30d1939SAndy Fiddaman			eval _cmds_='$'_cmds_$_index_
291*b30d1939SAndy Fiddaman			eval _attr_=$_prop_'$'_attr_$_index_
292*b30d1939SAndy Fiddaman			;;
293*b30d1939SAndy Fiddaman		*)	echo $_val_: $_op_ $_name_ expected >&2
294*b30d1939SAndy Fiddaman			;;
295*b30d1939SAndy Fiddaman		esac
296*b30d1939SAndy Fiddaman		;;
297*b30d1939SAndy Fiddaman	esac
298*b30d1939SAndy Fiddamandone
299*b30d1939SAndy FiddamanIFS=$_ifs_
300*b30d1939SAndy Fiddamancase $_match_ in
301*b30d1939SAndy Fiddaman"")	echo "$_command_: don't know how to make $_select_" >&2; $_error_ ;;
302*b30d1939SAndy Fiddamanesac
303*b30d1939SAndy Fiddamancase $_exec_ in
304*b30d1939SAndy Fiddaman"eval")	echo "$_list_" > $_state_list_
305*b30d1939SAndy Fiddaman	mamstate $_state_list_ < $_state_list_ | sort > $_state_time_
306*b30d1939SAndy Fiddaman	;;
307*b30d1939SAndy Fiddamanesac
308