FTP Client in TCL-TK
Volume Number: 14
Issue Number: 2
Column Tag: Alternate Environments
An FTP Fetch Client in Tcl/Tk
by Bruce O'Neel, Laurel MD
A light introduction to this powerful, multi-platform
scripting language
Overview
Tcl/Tk (pronounced "tickle tee-kay") is a scripting language written by Dr. John
Ousterhout while he was a professor at the University of California at Berkeley. Tcl
can either be a standalone shell where you issue commands (like those of unix or the
MPW shell), or it can be a library which you embed into your compiled program and
use to issue commands. Tk is an extension to Tcl which provides graphical interface Tcl
commands enabling you to write event driven programs with graphical interfaces.
Tcl/Tk has been very popular in the unix world for a long time and has recently been
ported to Mac OS and Win95/NT. As of version 8.0 of Tcl/Tk, the Mac OS and
Win95/NT ports have a native look and feel on their respective platforms. This article
is going to provide a brief overview of Tcl/Tk and then present a demonstration Tcl/Tk
program to fetch files using FTP.
Tcl/Tk Overview
Why is Tcl/Tk interesting? First, it is a dynamic scripting language. At run-time
your scripts are byte compiled and run. You can get the names of procedures and
variables at run-time; you can define new commands and new control statements at
run-time; you can load new source code at run-time; and you can extend Tcl with your
own shared libraries at run-time. Second, you can produce Mac like interfaces using
the native port of Tk and you can do this quickly and interactively. Think of it as rapid
prototyping for the Mac in a free language. Third, you can write scripts which can be
moved unchanged from Mac OS to Win95/NT and most unix variants. Finally, you can
easily write extensions to Tcl in any compiled language on the Mac, and they can either
call and be called by C or produce shared libraries. These extensions also can be
cross-platform if written to be portable. As an example, a group of people at NASA's
Goddard Space Flight Center have written an extension to Tcl which reads and writes a
file format called FITS used in astronomy
ftp://legacy.gsfc.nasa.gov/software/ftools/release/other/fitstclmac-src.sit.hqx.
There are a few notes on Tcl's syntax that will make reading the code easier. First,
remember that Tcl works by string substitution and that, from your point of view,
everything is a string. $varname means look up the value that is currently assigned to
a variable and put that string in place of $varname. [command arg arg] means execute
what ever is between the square brackets and substitute the value in place of
[command arg arg]. Finally, curly braces are used around parts of code you want to
execute later and defer evaluation until sometime in the future.
An FTP Client
I thought that a good demo of Tcl/Tk for the Mac would be an FTP client. Now, I didn't
want to rewrite Fetch or Anarchie, but, I did want a useful example. The example
program works but there are many features left for the reader to complete and the
sample probably won't work unless you FTP to a unix system. One develops a lot of
respect for Anarchie or Fetch when you try to repeat their author's work.
So, even though this is just a simple example, what made it good for Tcl/Tk? First, it
was quick and easy to write. I took about 4-6 hours to write most of the code, with a
little bit of time to clean things up for publication. Second, the resulting executable is
small at around 27 Kbytes and the UI is very Mac like. Third the same source worked
on more than one system. I was also able to run this on a unix system pretty much
unchanged for additional testing and on the unix system it looked like I was running a
Motif application. Finally I wanted a GUI and TCP/IP sockets in my program and Tcl/Tk
has all of this easily built in, debugged, and well documented. Plus, you can experiment
interactively with your code rather than compile, link, run, crash, debug,and edit as
you must normally do.
There are two downsides to Mac Tcl/Tk applications. The first is that you have to
install Tcl/Tk. The small application depends on some shared libraries, but, you could
avoid the need to already have installed Tcl/Tk by using the non-shared version. The
second downside is that the current version requires quite a bit of memory. The default
is 4mb but you might have to bump this up if your programs crash. Many crashes are
caused by running out of memory.
Displaying aWindow
The first thing the user sees when they start the program is a dialog produced by the
new_connection proc, listed below. The dialog looks like
Figure 1. Open Connection Dialog.
Because Tcl/Tk is interactive, you could download it from http://sunscript.sun.com
and type in each following command and watch what happens as you go. This is a very
quick way to learn how Tcl/Tk works.
new_connection
This is the main dialog the user interacts with and an example of
Tcl/Tk
programming. This asks the user for their hostname, username
(optional),
password (optional), and directory to connect to. When they click the
connect button, it brings up a directory list of that directory.
# Procedure to open a new connection.
proc new_connection {} {

# so we can access the global variable FTP
global FTP
# This sets the variable named t to the result of the
# toplevel command
# toplevel, like all Tk Widget creation commands returns
# the name of the widget,
# .new_connection in this case, as it's result.
set t [toplevel .new_connection -menu .menubar]
wm title $t "Open Connection

# create a text label
label $t.title -text "Open a new FTP connection
# grid is a geometry manager. This puts the title on the
# screen.
grid $t.title -columnspan 2
label $t.hostl -text "Hostname:
# associate the variable FTP(hostname) with a text entry
# area on the screen.
# note that there is not $ before FTP(hostname)
entry $t.hoste -textvariable FTP(hostname)
grid $t.hostl $t.hoste
label $t.userl -text "Username:
entry $t.usere -textvariable FTP(username)
grid $t.userl $t.usere

label $t.passl -text "Password:
# -show * echos * rather than the user's keystrokes
entry $t.passe -textvariable FTP(password) -show *
grid $t.passl $t.passe

label $t.dirl -text "Directory:
entry $t.dire -textvariable FTP(directory)

# create a button which when it runs the command up_dir
button $t.dirup -text "Up" -command "up_dir
grid $t.dirl $t.dire $t.dirup

# put up two radio buttons to set datamode. Tied together
# by the -variable option.
radiobutton $t.binary -variable FTP(mode) -text Binary
-value Binary
radiobutton $t.ascii -variable FTP(mode) -text Ascii
-value Ascii
label $t.datamode -text "Data Mode:
grid $t.datamode $t.binary $t.ascii

# frames hold things
frame $t.direc
label $t.direc.title -text "Remote Directory
# pack is another geometry manager and puts the title at
# the top of this frame
pack $t.direc.title -side top
# the following three commands set up a text box and two
# scroll bars
set FTP(listbox) [listbox $t.direc.list
-xscrollcommand [list $t.direc.xscroll set]
-yscrollcommand [list $t.direc.yscroll set]]
scrollbar $t.direc.xscroll -orient horizontal
-command [list $t.direc.list xview]
scrollbar $t.direc.yscroll -orient vertical
-command [list $t.direc.list yview]