Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A "console" REPL that runs while the event loop is alive

73 views
Skip to first unread message

Jonathan Bromley

unread,
Jun 21, 2013, 10:32:06 AM6/21/13
to
I have a number of Tcl-only (no Tk) event driven scripts where I
like to have the usual Tcl command line continue to be available
while the event loop is running, providing me with a neat way
to debug and inspect the running event-driven script.

For years now I've done this using a simple bit of code, shown
at the end of this post. But it has a big limitation: because it
makes stdin nonblocking, trying to do [gets stdin] at my new
"console" will always return immediately with an empty string.
I'd like to be able to fix this, so that - in effect - I have
a regular synchronous Tcl console running in the foreground on
stdin/stdout, but my event driven program running in the
background even when the "console" is stalled at a [gets stdin].

I took a look at the Tk console code, which clearly gets this
right already, but it's all in C and I was hoping for a pure
Tcl solution.

I guess I need to replace [gets] - but the more of that sort of
thing I do, the more I feel I'm reinventing wheels. So I wondered
if anyone has already done something similar and had some ideas
to share.

I poked around the Tcl Wiki but couldn't find anything (that
doesn't mean there isn't anything, of course).

Thanks in advance - Jonathan.

# simple machinery for running Tcl console in background
# while the event loop is running. Multi-line
# Tcl commands are incrementally built up in
# global variable "cmd".

# this proc will finally stop the event loop
proc bg_exit {} {set ::wait_var 0; return {}}

# call this proc to set up the "background console"
# and enter the event loop - use it as a replacement
# for [vwait whatever]
proc bg_console {} {
# Set up nonblocking stdin
fconfigure stdin -blocking 0
fileevent stdin readable console_input
# Make the [exit] command behave reasonably.
# It shuts down the event loop and returns control
# to the regular Tcl command line.
rename exit exit_from_tclsh
rename bg_exit exit
# start the event loop and provide first prompt
console_prompt
vwait ::wait_var
# We came out of the event loop. Put things back.
rename exit bg_exit
rename exit_from_tclsh exit
fileevent stdin readable {}
fconfigure stdin -blocking 1
puts stdout ">>>>> back to foreground <<<<<"
}

# Provide a characteristic prompt, with a variant
# for continuation lines
proc console_prompt {{cont 0}} {
if {$cont} {
puts -nonewline stdout "+++>> "
} else {
puts -nonewline stdout "bg-console>> "
}
flush stdout
}

# This proc is invoked when there's new keystrokes at stdin
proc console_input {} {
global cmd
if {[gets stdin line] >= 0} {
# there's a full line of text: process it
if {[info complete [append cmd $line\n]]} {
catch {uplevel #0 $cmd} msg
if {[string length $msg]} {puts $msg}
set cmd {}
}
console_prompt [expr {[string length $cmd] > 0}]
}
}

--
Jonathan Bromley

pal...@yahoo.com

unread,
Jun 21, 2013, 12:10:38 PM6/21/13
to
There are several examples on the wiki - http://wiki.tcl.tk/24060 or http://wiki.tcl.tk/1968 (the Discussion section) have multiple implementations.

/Ashok

EL

unread,
Jun 21, 2013, 2:15:20 PM6/21/13
to
Am 21.06.13 16:32, schrieb Jonathan Bromley:
> I have a number of Tcl-only (no Tk) event driven scripts where I
> like to have the usual Tcl command line continue to be available
> while the event loop is running, providing me with a neat way
> to debug and inspect the running event-driven script.

I think Tloona's built in console does that. Just checked:

% proc t args {set y 0; vwait x}
% t

% info vars
args y
% info level
1

Might be a starting point for you. The relevant code is in tmw-console
in the src/ directory (here of course Itcl/Itk based and requiring Tk).
In fact it is an extended version of the great small console example in
"Practical Programming Tcl/Tk".

Here's where you find the program:
http://sourceforge.net/projects/tloona/

--
EL

0 new messages