I'm assuming that your existing C program is a command-line driven application. In that case, it is quite possible (and frequently done) to create a Tcl/Tk GUI "wrapper" for your application. The wrapper would be responsible for managing the GUI and for marshalling communications with the existing C application.
In a situation like this,
exec is probably not powerful enough of a tool to get the job done. The Tcl
exec command runs another application and, optionally, captures its output for use by your Tcl script. However,
exec blocks (waits) until the application exits; it can't perform sophisticated interaction with the application, which is what I suspect you'll require.
Tcl has a more elegant mechanism for interprocess communication: command pipelines. In addition to opening simple files, Tcl's
open command allows you to start another application and connect to its standard input and and standard output channels through pipes. The syntax is simple:
Code:
set fid [open "| myprog" r+]
Now you can use
puts to write to the program's standard input, and
gets to read from its standard output.
That's the simple part. The difficult part is to solve the various buffering and blocking problems that can quickly arise. In general, especially with a GUI Tcl program, you'll want to set up event-driven I/O with the pipe using the
fileevent command. For more information on all of this, there are several pages on the Tcl'ers Wiki (
you'll probably want to start with
On Unix, the
Expect extension is another possibility, that allows your Tcl script to
spawn another application under its control,
send messages to the application, and
expect responses. You can get links to more information about Expect from the Tcl'ers Wiki at
In general, Tcl is excellent in situations like this for creating wrappers to other applications; it's much simpler than doing so in almost any other language. However, the issues involved are subtle and complex. It's easy to get stuck if you don't have a good understanding of what's going on between the applications and how the communication is being managed. If this is your first experience with Tcl/Tk programming, be prepared for a challenge because you're getting into more advanced Tcl programming techniques. I hope that the links I've provided help get you started. And, to get a little plug in here, you can always check out the Tcl training and consulting my company offers. In addition to our basic Tcl and Tk courses, we have a 1-day course on Interprocess Communication and a 2-day couse on Expect that addresses exactly these types of issues.
Good luck! - Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax