for review: mdb options for I/O redirection

Fergus Henderson fjh at cs.mu.OZ.AU
Fri Dec 18 17:50:28 AEDT 1998


scripts/mdb.in:
	Add new options `--tty', `--window', and `--window-cmd',
	for redirecting I/O to a different tty or to a new window.

Index: scripts/mdb.in
===================================================================
RCS file: /home/mercury1/repository/mercury/scripts/mdb.in,v
retrieving revision 1.2
diff -u -r1.2 mdb.in
--- mdb.in	1998/11/13 09:31:10	1.2
+++ mdb.in	1998/12/18 06:48:52
@@ -1,41 +1,170 @@
 #!/bin/sh
-#---------------------------------------------------------------------------#
+#-----------------------------------------------------------------------------#
 # Copyright (C) 1998 The University of Melbourne.
 # This file may only be copied under the terms of the GNU General
 # Public License - see the file COPYING in the Mercury distribution.
-#---------------------------------------------------------------------------#
+#-----------------------------------------------------------------------------#
 #
 # IMPORTANT: the manpage is produced automatically from this help
 # message, so if you change the help message, don't forget to check
 # that the manpage still looks OK.
 Help="\
 Name:	mdb - Mercury debugger
-Usage:	mdb <executable> [<options>]...
+Usage:	mdb [<options>] <executable> [<args>]...
 Description:
-	The arguments of this command form a command line.
-	If the executable named by this command line is a Mercury program
-	compiled with debugging enabled (e.g. via the \`--debug' option),
-	or if it invokes such a program, then mdb will cause the program
-	to be executed under the supervision of the Mercury internal debugger.
-	Otherwise, mdb will execute the command line as if the mdb prefix
-	weren't there.
+	\`mdb' invokes the specified command \`<executable> <args>...'
+	with Mercury debugging enabled.  If that command is a Mercury
+	program that was compiled with debugging enabled (e.g. using
+	the \`--debug' option), or if that command invokes such a
+	program, then mdb will cause the program to be executed under
+	the supervision of the Mercury internal debugger.  Otherwise,
+	mdb will execute the command line as if the mdb prefix weren't
+	there.
+
+	By default, all the output of the debugger and the output of the
+	program being debugged will be interleaved on the same terminal
+	window.  This can be avoided using the \`--tty' or \`--window'
+	options described below.
+
+Options:
+	-t <file-name>, --tty <file-name>
+		Redirect all of the I/O for the debugger to the device
+		specified by <file-name>.  The I/O for the program
+		being debugged will not be redirected.
+
+	-w, --window
+		Run the program in a new window, with the program's I/O
+		going to that window, but with mdb's I/O going to the
+		current terminal.
+
+	-c <window-command>, --window-command <window-command>
+		Specify the command used by the \`--window' option for
+		executing a command in a new window.  The default such
+		command is \`xterm -e'.
+
 Environment variables:
 	MERCURY_OPTIONS, MERCURY_DEBUGGER_INIT.
 "
 
+tty=
+window=false
+window_cmd="xterm -e"
+
+#-----------------------------------------------------------------------------#
+#
+# process the command line options
+#
+
 case $# in
-	0)	echo "Usage: mdb <executable> [<arg> ...]" 1>&2
-		exit 1;;
+	0)	echo "Usage: mdb [<options>] <executable> [<arg> ...]" 1>&2
+		exit 1 ;;
+esac
+
+while : ; do
+	case "$1" in
+		--help)
+			echo "$Help"
+			exit 0 ;;
+		-t|--tty)
+			tty="$2";
+			shift; shift ;;
+		-t*)
+			tty="` expr $1 : '-t\(.*\)' `"
+			shift ;;
+		-w|--window)
+			window=true
+			shift ;;
+		-w-|--no-window)
+			window=false
+			shift ;;
+		-c|--window-command)
+			window_cmd="$2";
+			shift; shift ;;
+		--)
+			shift; break ;;
+		-*)
+			echo "$0: unknown option \`$1'" 1>&2
+			exit 1 ;;
+		*)
+			break ;;
+	esac
+done
+
+#-----------------------------------------------------------------------------#
+#
+# Figure out how we should invoke the command
+#
+
+invoke_cmd=
+case "$window" in true)
+	invoke_cmd="$window_cmd"
+	#
+	# If windowing is enabled, check that DISPLAY is set, and if not,
+	# issue a warning message.  This is needed because the default error
+	# message from xterm is very poor.
+	#
+	case "$DISPLAY" in "")
+	    echo "$0: warning: environment variable \`DISPLAY' not set" 1>&2 ;;
+	esac ;;
 esac
 
-case $1 in
-	--help)
-		echo "$Help"
-		exit 0;;
+#-----------------------------------------------------------------------------#
+#
+# Figure out if/how we should redirect the mdb I/O streams
+#
+
+case "$tty" in
+    "")
+	case "$window" in
+	    true)
+		#
+		# On Linux, we can use special files in /proc
+		# that refer to the file descriptors for a
+		# particular process.
+		#
+		stdin=/proc/$$/fd/0
+		stdout=/proc/$$/fd/1
+		stderr=/proc/$$/fd/2
+		if [ -f $stdin -a -f $stdout -a -f $stderr ]; then
+			redirect_opts="
+				--mdb-in $stdin
+				--mdb-out $stdout
+				--mdb-err $stderr
+			"
+		else
+			# In the general case, we can use the `tty' command.
+			# But that will only work if we're actually running
+			# on a terminal.
+			tty="`tty`"
+			case "$tty" in ""|"not a tty")
+				echo "$0: standard input stream is not a tty" 1>&2
+				exit 1
+				;;
+			esac
+			redirect_opts="--mdb-tty $tty"
+		fi
+		;;
+	    false)
+		redirect_opts=""
+		;;
+	esac ;;
+    *)
+	redirect_opts="--mdb-tty $tty"
+	;;
 esac
 
-MERCURY_OPTIONS="$MERCURY_OPTIONS -Di"
+#-----------------------------------------------------------------------------#
+#
+# Set the environment variables used by the Mercury runtime to the
+# the appropriate values to enabled debugging and to redirect mdb I/O,
+# and then finally use $invoke_cmd to invoke the command.
+#
+
+enable_mdb_opt="-Di"
+MERCURY_OPTIONS="$MERCURY_OPTIONS $redirect_opts $enable_mdb_opt"
 export MERCURY_OPTIONS
 MERCURY_DEBUGGER_INIT=${MERCURY_DEBUGGER_INIT- at DEFAULT_MERCURY_DEBUGGER_INIT_DIR@/mdbrc}
 export MERCURY_DEBUGGER_INIT
-exec "$@"
+exec $invoke_cmd "$@"
+
+#-----------------------------------------------------------------------------#

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "Binaries may die
WWW: <http://www.cs.mu.oz.au/~fjh>  |   but source code lives forever"
PGP: finger fjh at 128.250.37.3        |     -- leaked Microsoft memo.



More information about the developers mailing list