Version 1 of Wrapping external commands with Tcl

Updated 2003-01-27 13:39:01

Many times people want to do things like run a program feeding it information, or doing some setup, then running a command and doing something with its output, or writing a Tk wrapper for a text only command.

Here's an example of wrapping the bc arbitrary precision arithmetic language calculator in Tcl. This might provide one with longer precision math without implementing one of the extensions discussed in [add wiki page titles here].

 #! /usr/tcl84/bin/tclsh
 # Program: bcfe
 # Date: Jan 27, 2003
 # Version: 1.0


 set fe [open "|/bin/bc" "r+"]
 fconfigure $fe -buffering none

 puts "Enter expression:"
 set ex 1
 puts -nonewline "bc $ex> "
 flush stdout
 while { [gets stdin input] >= 0 } {
        if { $input == "quit" } {
                break
        }
        puts $fe "$input"
        flush $fe
        incr ex
        gets $fe newans
        puts "Answer is: $newans"
        puts -nonewline "bc $ex> "
        flush stdout
 }

Category Application