xref: /freebsd/libexec/rc/debug.sh (revision 7e1c014a9e3a632099efd1fa34a583b627b71bb3)
1aa3b7a2fSSimon J. Gerraty:
2aa3b7a2fSSimon J. Gerraty# SPDX-License-Identifier: BSD-2-Clause
3aa3b7a2fSSimon J. Gerraty
4aa3b7a2fSSimon J. Gerraty# NAME:
5aa3b7a2fSSimon J. Gerraty#	debug.sh - selectively debug scripts
6aa3b7a2fSSimon J. Gerraty#
7aa3b7a2fSSimon J. Gerraty# SYNOPSIS:
8aa3b7a2fSSimon J. Gerraty#	$_DEBUG_SH . debug.sh
9aa3b7a2fSSimon J. Gerraty#	DebugOn [-eo] "tag" ...
10aa3b7a2fSSimon J. Gerraty#	DebugOff [-eo] [rc="rc"] "tag" ...
11aa3b7a2fSSimon J. Gerraty#	Debugging
12aa3b7a2fSSimon J. Gerraty#	DebugEcho ...
13aa3b7a2fSSimon J. Gerraty#	DebugLog ...
14aa3b7a2fSSimon J. Gerraty#	DebugShell "tag" ...
15aa3b7a2fSSimon J. Gerraty#	DebugTrace ...
16aa3b7a2fSSimon J. Gerraty#	Debug "tag" ...
17aa3b7a2fSSimon J. Gerraty#
18aa3b7a2fSSimon J. Gerraty#	$DEBUG_SKIP echo skipped when Debug "tag" is true.
19aa3b7a2fSSimon J. Gerraty#	$DEBUG_DO echo only done when Debug "tag" is true.
20aa3b7a2fSSimon J. Gerraty#
21aa3b7a2fSSimon J. Gerraty# DESCRIPTION:
22aa3b7a2fSSimon J. Gerraty#	debug.sh provides the following functions to facilitate
23aa3b7a2fSSimon J. Gerraty#	flexible run-time tracing of complicated shell scripts.
24aa3b7a2fSSimon J. Gerraty#
25aa3b7a2fSSimon J. Gerraty#	DebugOn turns tracing on if any "tag" is found in "DEBUG_SH".
26aa3b7a2fSSimon J. Gerraty#	It turns tracing off if "!tag" is found in "DEBUG_SH".
27aa3b7a2fSSimon J. Gerraty#	It also sets "DEBUG_ON" to the "tag" that caused tracing to be
28aa3b7a2fSSimon J. Gerraty#	enabled, or "DEBUG_OFF" if we matched "!tag".
29aa3b7a2fSSimon J. Gerraty#	If '-e' option given returns 1 if no "tag" matched.
30aa3b7a2fSSimon J. Gerraty#	If the '-o' flag is given, tracing is turned off unless there
31aa3b7a2fSSimon J. Gerraty#	was a matched "tag", useful for functions too noisy to tace.
32aa3b7a2fSSimon J. Gerraty#
33aa3b7a2fSSimon J. Gerraty#	DebugOff turns tracing on if any "tag" matches "DEBUG_OFF" or
34aa3b7a2fSSimon J. Gerraty#	off if any "tag" matches "DEBUG_ON". This allows nested
35aa3b7a2fSSimon J. Gerraty#	functions to not interfere with each other.
36aa3b7a2fSSimon J. Gerraty#
37aa3b7a2fSSimon J. Gerraty#	DebugOff accepts but ignores the '-e' and '-o' options.
38aa3b7a2fSSimon J. Gerraty#	The optional "rc" value will be returned rather than the
39aa3b7a2fSSimon J. Gerraty#	default of 0. Thus if DebugOff is the last operation in a
40aa3b7a2fSSimon J. Gerraty#	function, "rc" will be the return code of that function.
41aa3b7a2fSSimon J. Gerraty#
42aa3b7a2fSSimon J. Gerraty#	DebugEcho is just shorthand for:
43aa3b7a2fSSimon J. Gerraty#.nf
44aa3b7a2fSSimon J. Gerraty#	$DEBUG_DO echo "$@"
45aa3b7a2fSSimon J. Gerraty#.fi
46aa3b7a2fSSimon J. Gerraty#
47aa3b7a2fSSimon J. Gerraty#	Debugging returns true if tracing is enabled.
48aa3b7a2fSSimon J. Gerraty#	It is useful for bounding complex debug actions, rather than
49aa3b7a2fSSimon J. Gerraty#	using lots of "DEBUG_DO" lines.
50aa3b7a2fSSimon J. Gerraty#
51aa3b7a2fSSimon J. Gerraty#	DebugShell runs an interactive shell if any "tag" is found in
52aa3b7a2fSSimon J. Gerraty#	"DEBUG_INTERACTIVE", and there is a tty available.
53aa3b7a2fSSimon J. Gerraty#	The shell used is defined by "DEBUG_SHELL" or "SHELL" and
54aa3b7a2fSSimon J. Gerraty#	defaults to '/bin/sh'.
55aa3b7a2fSSimon J. Gerraty#
56aa3b7a2fSSimon J. Gerraty#	Debug calls DebugOn and if that does not turn tracing on, it
57aa3b7a2fSSimon J. Gerraty#	calls DebugOff to turn it off.
58aa3b7a2fSSimon J. Gerraty#
59aa3b7a2fSSimon J. Gerraty#	The variables "DEBUG_SKIP" and "DEBUG_DO" are set so as to
60aa3b7a2fSSimon J. Gerraty#	enable/disable code that should be skipped/run when debugging
61aa3b7a2fSSimon J. Gerraty#	is turned on. "DEBUGGING" is the same as "DEBUG_SKIP" for
62aa3b7a2fSSimon J. Gerraty#	backwards compatability.
63aa3b7a2fSSimon J. Gerraty#
64aa3b7a2fSSimon J. Gerraty#	The use of $_DEBUG_SH is to prevent multiple inclusion, though
65aa3b7a2fSSimon J. Gerraty#	it does no harm in this case.
66aa3b7a2fSSimon J. Gerraty#
67aa3b7a2fSSimon J. Gerraty# BUGS:
68aa3b7a2fSSimon J. Gerraty#	Does not work with some versions of ksh.
69aa3b7a2fSSimon J. Gerraty#	If a function turns tracing on, ksh turns it off when the
70aa3b7a2fSSimon J. Gerraty#	function returns - useless.
71aa3b7a2fSSimon J. Gerraty#	PD ksh works ok ;-)
72aa3b7a2fSSimon J. Gerraty#
73aa3b7a2fSSimon J. Gerraty# AUTHOR:
74aa3b7a2fSSimon J. Gerraty#	Simon J. Gerraty <sjg@crufty.net>
75aa3b7a2fSSimon J. Gerraty
76aa3b7a2fSSimon J. Gerraty# RCSid:
77*7e1c014aSSimon J. Gerraty#	$Id: debug.sh,v 1.40 2024/09/09 20:06:00 sjg Exp $
78aa3b7a2fSSimon J. Gerraty#
79aa3b7a2fSSimon J. Gerraty#	@(#) Copyright (c) 1994-2024 Simon J. Gerraty
80aa3b7a2fSSimon J. Gerraty#
81aa3b7a2fSSimon J. Gerraty#	This file is provided in the hope that it will
82aa3b7a2fSSimon J. Gerraty#	be of use.  There is absolutely NO WARRANTY.
83aa3b7a2fSSimon J. Gerraty#	Permission to copy, redistribute or otherwise
84aa3b7a2fSSimon J. Gerraty#	use this file is hereby granted provided that
85aa3b7a2fSSimon J. Gerraty#	the above copyright notice and this notice are
86aa3b7a2fSSimon J. Gerraty#	left intact.
87aa3b7a2fSSimon J. Gerraty#
88aa3b7a2fSSimon J. Gerraty#	Please send copies of changes and bug-fixes to:
89aa3b7a2fSSimon J. Gerraty#	sjg@crufty.net
90aa3b7a2fSSimon J. Gerraty#
91aa3b7a2fSSimon J. Gerraty
92aa3b7a2fSSimon J. Gerraty_DEBUG_SH=:
93aa3b7a2fSSimon J. Gerraty
94aa3b7a2fSSimon J. GerratyMyname=${Myname:-`basename $0 .sh`}
95aa3b7a2fSSimon J. Gerraty
96*7e1c014aSSimon J. Gerraty# We want to use local if we can
97*7e1c014aSSimon J. Gerraty# if isposix-shell.sh has been sourced isPOSIX_SHELL will be set
98*7e1c014aSSimon J. Gerraty# as will local
99*7e1c014aSSimon J. Gerratycase "$local" in
100*7e1c014aSSimon J. Gerratylocal|:) ;;
101*7e1c014aSSimon J. Gerraty*)
102*7e1c014aSSimon J. Gerraty	if (echo ${PATH%:*}) > /dev/null 2>&1; then
103*7e1c014aSSimon J. Gerraty		local=local
104*7e1c014aSSimon J. Gerraty	else
105*7e1c014aSSimon J. Gerraty		local=:
106*7e1c014aSSimon J. Gerraty	fi
107*7e1c014aSSimon J. Gerraty	;;
108*7e1c014aSSimon J. Gerratyesac
109*7e1c014aSSimon J. Gerraty
110aa3b7a2fSSimon J. GerratyDEBUGGING=
111aa3b7a2fSSimon J. GerratyDEBUG_DO=:
112aa3b7a2fSSimon J. GerratyDEBUG_SKIP=
113aa3b7a2fSSimon J. Gerratyexport DEBUGGING DEBUG_DO DEBUG_SKIP
114aa3b7a2fSSimon J. Gerraty
115*7e1c014aSSimon J. Gerraty##
116*7e1c014aSSimon J. Gerraty# _debugOn match first
117*7e1c014aSSimon J. Gerraty#
118*7e1c014aSSimon J. Gerraty# Actually turn on tracing, set $DEBUG_ON=$match
119*7e1c014aSSimon J. Gerraty#
120*7e1c014aSSimon J. Gerraty# If we have included hooks.sh $_HOOKS_SH will be set
121*7e1c014aSSimon J. Gerraty# and if $first (the first arg to DebugOn) is suitable as a variable
122*7e1c014aSSimon J. Gerraty# name we will run ${first}_debugOn_hooks.
123*7e1c014aSSimon J. Gerraty#
124*7e1c014aSSimon J. Gerraty# We disable tracing for hooks_run itself but functions can trace
125*7e1c014aSSimon J. Gerraty# if they want based on DEBUG_DO
126*7e1c014aSSimon J. Gerraty#
127aa3b7a2fSSimon J. Gerraty_debugOn() {
128aa3b7a2fSSimon J. Gerraty	DEBUG_OFF=
129aa3b7a2fSSimon J. Gerraty	DEBUG_DO=
130aa3b7a2fSSimon J. Gerraty	DEBUG_SKIP=:
131aa3b7a2fSSimon J. Gerraty	DEBUG_X=-x
132aa3b7a2fSSimon J. Gerraty	set -x
133aa3b7a2fSSimon J. Gerraty	DEBUG_ON=$1
134*7e1c014aSSimon J. Gerraty	case "$_HOOKS_SH,$2" in
135*7e1c014aSSimon J. Gerraty	,*|:,|:,*[${CASE_CLASS_NEG:-!}A-Za-z0-9_]*) ;;
136*7e1c014aSSimon J. Gerraty	*)	# avoid noise from hooks_run
137*7e1c014aSSimon J. Gerraty		set +x
138*7e1c014aSSimon J. Gerraty		hooks_run ${2}_debugOn_hooks
139*7e1c014aSSimon J. Gerraty		set -x
140*7e1c014aSSimon J. Gerraty		;;
141*7e1c014aSSimon J. Gerraty	esac
142aa3b7a2fSSimon J. Gerraty}
143aa3b7a2fSSimon J. Gerraty
144*7e1c014aSSimon J. Gerraty##
145*7e1c014aSSimon J. Gerraty# _debugOff match $DEBUG_ON $first
146*7e1c014aSSimon J. Gerraty#
147*7e1c014aSSimon J. Gerraty# Actually turn off tracing, set $DEBUG_OFF=$match
148*7e1c014aSSimon J. Gerraty#
149*7e1c014aSSimon J. Gerraty# If we have included hooks.sh $_HOOKS_SH will be set
150*7e1c014aSSimon J. Gerraty# and if $first (the first arg to DebugOff) is suitable as a variable
151*7e1c014aSSimon J. Gerraty# name we will run ${first}_debugOff_hooks.
152*7e1c014aSSimon J. Gerraty#
153*7e1c014aSSimon J. Gerraty# We do hooks_run after turning off tracing, but before resetting
154*7e1c014aSSimon J. Gerraty# DEBUG_DO so functions can trace if they want
155*7e1c014aSSimon J. Gerraty#
156aa3b7a2fSSimon J. Gerraty_debugOff() {
157aa3b7a2fSSimon J. Gerraty	DEBUG_OFF=$1
158aa3b7a2fSSimon J. Gerraty	set +x
159*7e1c014aSSimon J. Gerraty	case "$_HOOKS_SH,$3" in
160*7e1c014aSSimon J. Gerraty	,*|:,|:,*[${CASE_CLASS_NEG:-!}A-Za-z0-9_]*) ;;
161*7e1c014aSSimon J. Gerraty	*)	hooks_run ${3}_debugOff_hooks;;
162*7e1c014aSSimon J. Gerraty	esac
163*7e1c014aSSimon J. Gerraty	set +x			# just to be sure
164aa3b7a2fSSimon J. Gerraty	DEBUG_ON=$2
165aa3b7a2fSSimon J. Gerraty	DEBUG_DO=:
166aa3b7a2fSSimon J. Gerraty	DEBUG_SKIP=
167aa3b7a2fSSimon J. Gerraty	DEBUG_X=
168aa3b7a2fSSimon J. Gerraty}
169aa3b7a2fSSimon J. Gerraty
170aa3b7a2fSSimon J. GerratyDebugEcho() {
171aa3b7a2fSSimon J. Gerraty	$DEBUG_DO echo "$@"
172aa3b7a2fSSimon J. Gerraty}
173aa3b7a2fSSimon J. Gerraty
174*7e1c014aSSimon J. Gerraty##
175*7e1c014aSSimon J. Gerraty# Debugging
176*7e1c014aSSimon J. Gerraty#
177*7e1c014aSSimon J. Gerraty# return 0 if we are debugging.
178*7e1c014aSSimon J. Gerraty#
179aa3b7a2fSSimon J. GerratyDebugging() {
180aa3b7a2fSSimon J. Gerraty	test "$DEBUG_SKIP"
181aa3b7a2fSSimon J. Gerraty}
182aa3b7a2fSSimon J. Gerraty
183*7e1c014aSSimon J. Gerraty##
184*7e1c014aSSimon J. Gerraty# DebugLog message
185*7e1c014aSSimon J. Gerraty#
186*7e1c014aSSimon J. Gerraty# Outout message with timestamp if we are debugging
187*7e1c014aSSimon J. Gerraty#
188aa3b7a2fSSimon J. GerratyDebugLog() {
189aa3b7a2fSSimon J. Gerraty	$DEBUG_SKIP return 0
190aa3b7a2fSSimon J. Gerraty	echo `date '+@ %s [%Y-%m-%d %H:%M:%S %Z]'` "$@"
191aa3b7a2fSSimon J. Gerraty}
192aa3b7a2fSSimon J. Gerraty
193*7e1c014aSSimon J. Gerraty##
194*7e1c014aSSimon J. Gerraty# DebugTrace message
195*7e1c014aSSimon J. Gerraty#
196*7e1c014aSSimon J. Gerraty# Something hard to miss when wading through huge -x output
197*7e1c014aSSimon J. Gerraty#
198aa3b7a2fSSimon J. GerratyDebugTrace() {
199aa3b7a2fSSimon J. Gerraty	$DEBUG_SKIP return 0
200aa3b7a2fSSimon J. Gerraty	set +x
201aa3b7a2fSSimon J. Gerraty	echo "@ ==================== [ $DEBUG_ON ] ===================="
202aa3b7a2fSSimon J. Gerraty	DebugLog "$@"
203aa3b7a2fSSimon J. Gerraty	echo "@ ==================== [ $DEBUG_ON ] ===================="
204aa3b7a2fSSimon J. Gerraty	set -x
205aa3b7a2fSSimon J. Gerraty}
206aa3b7a2fSSimon J. Gerraty
207*7e1c014aSSimon J. Gerraty##
208*7e1c014aSSimon J. Gerraty# DebugOn [-e] [-o] match ...
209*7e1c014aSSimon J. Gerraty#
210*7e1c014aSSimon J. Gerraty# Turn on debugging if any $match is found in $DEBUG_SH.
211*7e1c014aSSimon J. Gerraty#
212aa3b7a2fSSimon J. GerratyDebugOn() {
213*7e1c014aSSimon J. Gerraty	eval ${local:-:} _e _match _off _rc
214aa3b7a2fSSimon J. Gerraty	_rc=0			# avoid problems with set -e
215aa3b7a2fSSimon J. Gerraty	_off=:
216aa3b7a2fSSimon J. Gerraty	while :
217aa3b7a2fSSimon J. Gerraty	do
218aa3b7a2fSSimon J. Gerraty		case "$1" in
219aa3b7a2fSSimon J. Gerraty		-e) _rc=1; shift;; # caller ok with return 1
220aa3b7a2fSSimon J. Gerraty		-o) _off=; shift;; # off unless we have a match
221aa3b7a2fSSimon J. Gerraty		*) break;;
222aa3b7a2fSSimon J. Gerraty		esac
223aa3b7a2fSSimon J. Gerraty	done
224aa3b7a2fSSimon J. Gerraty	case ",${DEBUG_SH:-$DEBUG}," in
225aa3b7a2fSSimon J. Gerraty	,,)	return $_rc;;
226aa3b7a2fSSimon J. Gerraty	*,[Dd]ebug,*) ;;
227aa3b7a2fSSimon J. Gerraty	*) $DEBUG_DO set +x;;		# reduce the noise
228aa3b7a2fSSimon J. Gerraty	esac
229aa3b7a2fSSimon J. Gerraty	_match=
230aa3b7a2fSSimon J. Gerraty	# if debugging is off because of a !e
231aa3b7a2fSSimon J. Gerraty	# don't add 'all' to the On list.
232aa3b7a2fSSimon J. Gerraty	case "$_off$DEBUG_OFF" in
233aa3b7a2fSSimon J. Gerraty	:)	_e=all;;
234aa3b7a2fSSimon J. Gerraty	*)	_e=;;
235aa3b7a2fSSimon J. Gerraty	esac
236aa3b7a2fSSimon J. Gerraty	for _e in ${*:-$Myname} $_e
237aa3b7a2fSSimon J. Gerraty	do
238aa3b7a2fSSimon J. Gerraty		: $_e in ,${DEBUG_SH:-$DEBUG},
239aa3b7a2fSSimon J. Gerraty		case ",${DEBUG_SH:-$DEBUG}," in
240aa3b7a2fSSimon J. Gerraty		*,!$_e,*|*,!$Myname:$_e,*)
241aa3b7a2fSSimon J. Gerraty			# only turn it off if it was on
242aa3b7a2fSSimon J. Gerraty			_rc=0
243*7e1c014aSSimon J. Gerraty			$DEBUG_DO _debugOff $_e $DEBUG_ON $1
244aa3b7a2fSSimon J. Gerraty			break
245aa3b7a2fSSimon J. Gerraty			;;
246aa3b7a2fSSimon J. Gerraty		*,$_e,*|*,$Myname:$_e,*)
247aa3b7a2fSSimon J. Gerraty			# only turn it on if it was off
248aa3b7a2fSSimon J. Gerraty			_rc=0
249aa3b7a2fSSimon J. Gerraty			_match=$_e
250*7e1c014aSSimon J. Gerraty			$DEBUG_SKIP _debugOn $_e $1
251aa3b7a2fSSimon J. Gerraty			break
252aa3b7a2fSSimon J. Gerraty			;;
253aa3b7a2fSSimon J. Gerraty		esac
254aa3b7a2fSSimon J. Gerraty	done
255aa3b7a2fSSimon J. Gerraty	if test -z "$_off$_match"; then
256aa3b7a2fSSimon J. Gerraty		# off unless explicit match, but
257aa3b7a2fSSimon J. Gerraty		# only turn it off if it was on
258*7e1c014aSSimon J. Gerraty		$DEBUG_DO _debugOff $_e $DEBUG_ON $1
259aa3b7a2fSSimon J. Gerraty	fi
260aa3b7a2fSSimon J. Gerraty	DEBUGGING=$DEBUG_SKIP	# backwards compatability
261aa3b7a2fSSimon J. Gerraty	$DEBUG_DO set -x	# back on if needed
262aa3b7a2fSSimon J. Gerraty	$DEBUG_DO set -x	# make sure we see it in trace
263aa3b7a2fSSimon J. Gerraty	return $_rc
264aa3b7a2fSSimon J. Gerraty}
265aa3b7a2fSSimon J. Gerraty
266*7e1c014aSSimon J. Gerraty##
267*7e1c014aSSimon J. Gerraty# DebugOff [-e] [-o] [rc=$?] match ...
268*7e1c014aSSimon J. Gerraty#
269aa3b7a2fSSimon J. Gerraty# Only turn debugging off if one of our args was the reason it
270aa3b7a2fSSimon J. Gerraty# was turned on.
271*7e1c014aSSimon J. Gerraty#
272aa3b7a2fSSimon J. Gerraty# We normally return 0, but caller can pass rc=$? as first arg
273aa3b7a2fSSimon J. Gerraty# so that we preserve the status of last statement.
274*7e1c014aSSimon J. Gerraty#
275*7e1c014aSSimon J. Gerraty# The options '-e' and '-o' are ignored, they just make it easier to
276*7e1c014aSSimon J. Gerraty# keep DebugOn and DebugOff lines in sync.
277*7e1c014aSSimon J. Gerraty#
278aa3b7a2fSSimon J. GerratyDebugOff() {
279*7e1c014aSSimon J. Gerraty	eval ${local:-:} _e _rc
280aa3b7a2fSSimon J. Gerraty	case ",${DEBUG_SH:-$DEBUG}," in
281aa3b7a2fSSimon J. Gerraty	*,[Dd]ebug,*) ;;
282aa3b7a2fSSimon J. Gerraty	*) $DEBUG_DO set +x;;		# reduce the noise
283aa3b7a2fSSimon J. Gerraty	esac
284aa3b7a2fSSimon J. Gerraty	_rc=0			# always happy
285aa3b7a2fSSimon J. Gerraty	while :
286aa3b7a2fSSimon J. Gerraty	do
287aa3b7a2fSSimon J. Gerraty		case "$1" in
288aa3b7a2fSSimon J. Gerraty		-[eo]) shift;;	# ignore it
289aa3b7a2fSSimon J. Gerraty		rc=*) eval "_$1"; shift;;
290aa3b7a2fSSimon J. Gerraty		*) break;;
291aa3b7a2fSSimon J. Gerraty		esac
292aa3b7a2fSSimon J. Gerraty	done
293aa3b7a2fSSimon J. Gerraty	for _e in $*
294aa3b7a2fSSimon J. Gerraty	do
295aa3b7a2fSSimon J. Gerraty		: $_e==$DEBUG_OFF DEBUG_OFF
296aa3b7a2fSSimon J. Gerraty		case "$DEBUG_OFF" in
297aa3b7a2fSSimon J. Gerraty		"")	break;;
298*7e1c014aSSimon J. Gerraty		$_e)	_debugOn $DEBUG_ON $1; return $_rc;;
299aa3b7a2fSSimon J. Gerraty		esac
300aa3b7a2fSSimon J. Gerraty	done
301aa3b7a2fSSimon J. Gerraty	for _e in $*
302aa3b7a2fSSimon J. Gerraty	do
303aa3b7a2fSSimon J. Gerraty		: $_e==$DEBUG_ON DEBUG_ON
304aa3b7a2fSSimon J. Gerraty		case "$DEBUG_ON" in
305aa3b7a2fSSimon J. Gerraty		"")	break;;
306*7e1c014aSSimon J. Gerraty		$_e)	_debugOff "" "" $1; return $_rc;;
307aa3b7a2fSSimon J. Gerraty		esac
308aa3b7a2fSSimon J. Gerraty	done
309aa3b7a2fSSimon J. Gerraty	DEBUGGING=$DEBUG_SKIP	# backwards compatability
310aa3b7a2fSSimon J. Gerraty	$DEBUG_DO set -x	# back on if needed
311aa3b7a2fSSimon J. Gerraty	$DEBUG_DO set -x	# make sure we see it in trace
312aa3b7a2fSSimon J. Gerraty	return $_rc
313aa3b7a2fSSimon J. Gerraty}
314aa3b7a2fSSimon J. Gerraty
315aa3b7a2fSSimon J. Gerraty_TTY=${_TTY:-`test -t 0 && tty`}; export _TTY
316aa3b7a2fSSimon J. Gerraty
317aa3b7a2fSSimon J. Gerraty# override this if you like
318aa3b7a2fSSimon J. Gerraty_debugShell() {
319*7e1c014aSSimon J. Gerraty	test "x$_TTY" != x || return 0
320aa3b7a2fSSimon J. Gerraty	{
321aa3b7a2fSSimon J. Gerraty		echo DebugShell "$@"
322aa3b7a2fSSimon J. Gerraty		echo "Type 'exit' to continue..."
323aa3b7a2fSSimon J. Gerraty	} > $_TTY
324aa3b7a2fSSimon J. Gerraty	${DEBUG_SHELL:-${SHELL:-/bin/sh}} < $_TTY > $_TTY 2>&1
325aa3b7a2fSSimon J. Gerraty}
326aa3b7a2fSSimon J. Gerraty
327aa3b7a2fSSimon J. Gerraty# Run an interactive shell if appropriate
328aa3b7a2fSSimon J. Gerraty# Note: you can use $DEBUG_SKIP DebugShell ... to skip unless debugOn
329aa3b7a2fSSimon J. GerratyDebugShell() {
330*7e1c014aSSimon J. Gerraty	eval ${local:-:} _e
331aa3b7a2fSSimon J. Gerraty	case "$_TTY%${DEBUG_INTERACTIVE}" in
332aa3b7a2fSSimon J. Gerraty	*%|%*) return 0;;	# no tty or no spec
333aa3b7a2fSSimon J. Gerraty	esac
334aa3b7a2fSSimon J. Gerraty	for _e in ${*:-$Myname} all
335aa3b7a2fSSimon J. Gerraty	do
336aa3b7a2fSSimon J. Gerraty		case ",${DEBUG_INTERACTIVE}," in
337aa3b7a2fSSimon J. Gerraty		*,!$_e,*|*,!$Myname:$_e,*)
338aa3b7a2fSSimon J. Gerraty			return 0
339aa3b7a2fSSimon J. Gerraty			;;
340aa3b7a2fSSimon J. Gerraty		*,$_e,*|*,$Myname:$_e,*)
341aa3b7a2fSSimon J. Gerraty			# Provide clues as to why/where
342aa3b7a2fSSimon J. Gerraty			_debugShell "$_e: $@"
343aa3b7a2fSSimon J. Gerraty			return $?
344aa3b7a2fSSimon J. Gerraty			;;
345aa3b7a2fSSimon J. Gerraty		esac
346aa3b7a2fSSimon J. Gerraty	done
347aa3b7a2fSSimon J. Gerraty	return 0
348aa3b7a2fSSimon J. Gerraty}
349aa3b7a2fSSimon J. Gerraty
350aa3b7a2fSSimon J. Gerraty# For backwards compatability
351aa3b7a2fSSimon J. GerratyDebug() {
352aa3b7a2fSSimon J. Gerraty	case "${DEBUG_SH:-$DEBUG}" in
353aa3b7a2fSSimon J. Gerraty	"")	;;
354aa3b7a2fSSimon J. Gerraty	*)	DEBUG_ON=${DEBUG_ON:-_Debug}
355aa3b7a2fSSimon J. Gerraty		DebugOn -e $* || DebugOff $DEBUG_LAST
356aa3b7a2fSSimon J. Gerraty		DEBUGGING=$DEBUG_SKIP
357aa3b7a2fSSimon J. Gerraty		;;
358aa3b7a2fSSimon J. Gerraty	esac
359aa3b7a2fSSimon J. Gerraty}
360