home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume25 / tcl / part31 < prev    next >
Encoding:
Text File  |  1991-11-15  |  43.3 KB  |  826 lines

  1. Newsgroups: comp.sources.misc
  2. From: karl@sugar.neosoft.com (Karl Lehenbauer)
  3. Subject:  v25i099:  tcl - tool command language, version 6.1, Part31/33
  4. Message-ID: <1991Nov15.230100.22349@sparky.imd.sterling.com>
  5. X-Md4-Signature: 18b6bb80ea9b129780668efa8b4edd19
  6. Date: Fri, 15 Nov 1991 23:01:00 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: karl@sugar.neosoft.com (Karl Lehenbauer)
  10. Posting-number: Volume 25, Issue 99
  11. Archive-name: tcl/part31
  12. Environment: UNIX
  13.  
  14. #! /bin/sh
  15. # This is a shell archive.  Remove anything before this line, then unpack
  16. # it by saving it into a file and typing "sh file".  To overwrite existing
  17. # files, type "sh file -c".  You can also feed this as standard input via
  18. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  19. # will see the following message at the end:
  20. #        "End of archive 31 (of 33)."
  21. # Contents:  tcl6.1/doc/usenix.text
  22. # Wrapped by karl@one on Tue Nov 12 19:44:33 1991
  23. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  24. if test -f 'tcl6.1/doc/usenix.text' -a "${1}" != "-c" ; then 
  25.   echo shar: Will not clobber existing file \"'tcl6.1/doc/usenix.text'\"
  26. else
  27. echo shar: Extracting \"'tcl6.1/doc/usenix.text'\" \(41391 characters\)
  28. sed "s/^X//" >'tcl6.1/doc/usenix.text' <<'END_OF_FILE'
  29. XTcl: An Embeddable Command Language
  30. XJohn K. Ousterhout
  31. XComputer Science Division
  32. XElectrical Engineering and Computer Sciences
  33. XUniversity of California at Berkeley
  34. XBerkeley, CA 94720
  35. Xouster@sprite.berkeley.edu
  36. X
  37. XABSTRACT
  38. X
  39. XTcl is an interpreter for a tool command language.  It consists of a library
  40. Xpackage that is embedded in tools (such as editors, debuggers, etc.) as the
  41. Xbasic command interpreter.  Tcl provides (a) a parser for a simple textual
  42. Xcommand language, (b) a collection of built-in utility commands, and (c)
  43. Xa C interface that tools use to augment the built-in commands with
  44. Xtool-specific commands.  Tcl is particularly attractive when integrated with
  45. Xthe widget library of a window system: it increases the programmability of
  46. Xthe widgets by providing mechanisms for variables, procedures, expressions,
  47. Xetc; it allows users to program both the appearance and the actions of
  48. Xwidgets; and it offers a simple but powerful communication mechanism between
  49. Xinteractive programs.
  50. X
  51. XThe work described here was supported in part by the National Science
  52. XFoundation under Grant ECS-8351961.
  53. X
  54. X
  55. X1.  Introduction
  56. X
  57. XTcl stands for ``tool command language''.  It consists of a library package
  58. Xthat programs can use as the basis for their command languages.  The
  59. Xdevelopment of Tcl was motivated by two observations.  The first observation
  60. Xis that a general-purpose programmable command language amplifies the power
  61. Xof a tool by allowing users to write programs in the command language in
  62. Xorder to extend the tool's built-in facilities.  Among the best-known
  63. Xexamples of powerful command languages are those of the UNIX shells [5]
  64. Xand the Emacs editor [8].  In each case a computing environment of unusual
  65. Xpower has arisen, in large part because of the availability of a pro-
  66. Xgrammable command language.
  67. X
  68. XThe second motivating observation is that the number of interactive
  69. Xapplications is increasing.  In the timesharing environments of the late
  70. X1970's and early 1980's almost all programs were batch-oriented.  They
  71. Xwere typically invoked using an interactive command shell.  Besides the
  72. Xshell, only a few other programs needed to be interactive, such as editors
  73. Xand mailers.  In contrast, the personal workstations used today, with their
  74. Xraster displays and mice, encourage a different system structure where a
  75. Xlarge number of programs are interactive and the most common style of
  76. Xinteraction is to manipulate individual applications directly with a mouse.
  77. XFurthermore, the large displays available today make it possible for many
  78. Xinteractive applications to be active at once, whereas this was not practical
  79. Xwith the smaller screens of ten years ago.
  80. X
  81. XUnfortunately, few of today's interactive applications have the power of the
  82. Xshell or Emacs command languages.  Where good command languages exist, they
  83. Xtend to be tied to specific programs.  Each new interactive application 
  84. Xrequires a new command language to be developed.  In most cases application
  85. Xprogrammers do not have the time or inclination to implement a general-purpose
  86. Xfacility (particularly if the application itself is simple), so the resulting
  87. Xcommand languages tend to have insufficient power and clumsy syntax.
  88. X
  89. XTcl is an application-independent command language.  It exists as a
  90. XC library package that can be used in many different programs.  The
  91. XTcl library provides a parser for a simple but fully programmable command
  92. Xlanguage.  The library also implements a collection of built-in commands
  93. Xthat provide general-purpose programming constructs such as variables,
  94. Xlists, expressions, conditionals, looping, and procedures.  Individual
  95. Xapplication programs extend the basic Tcl language with application-specific
  96. Xcommands.  The Tcl library also provides a set of utility routines to simplify
  97. Xthe implementation of tool-specific commands.
  98. X
  99. XI believe that Tcl is particularly useful in a windowing environment, and that
  100. Xit provides two advantages.  First, it can be used as a general-purpose
  101. Xmechanism for programming the interfaces of applications.  If a tool is based
  102. Xon Tcl, then it should be relatively easy to modify the application's user
  103. Xinterface and to extend the interface with new commands.  Second, and more
  104. Ximportant, Tcl provides a uniform framework for communication between tools.
  105. XIf used uniformly in all tools, Tcl will make it possible for tools to work
  106. Xtogether more gracefully than is possible today.  
  107. X
  108. XThe rest of this paper is organized as follows.  Section 2 describes the
  109. XTcl language as seen by users.  Section 3 discusses how Tcl is used in
  110. Xapplications, including the C-language interface between application
  111. Xprograms and the Tcl library.  Section 4 describes how Tcl can be used
  112. Xin a windowing environment to customize interface actions and appearances.
  113. XSection 5 shows how Tcl can be used as a vehicle for communication between
  114. Xapplications, and why this is important.  Section 6 presents the status
  115. Xof the Tcl implementation and some preliminary performance measurements.
  116. XSection 7 compares Tcl to Lisp, Emacs, and NeWS, and Section 8 concludes
  117. Xthe paper.
  118. X
  119. X2.  The Tcl Language
  120. X
  121. XIn a sense, the syntax of the Tcl language is unimportant: any programming
  122. Xlanguage, whether it is C [6], Forth [4], Lisp [1], or Postscript [2],
  123. Xcould provide many of the same programmability and communication advantages
  124. Xas Tcl.  This suggests that the best implementation approach is to borrow
  125. Xan existing language and concentrate on providing a convenient framework for
  126. Xthe use of that language.  However, the environment for an embeddable command
  127. Xlanguage presents an unusual set of constraints on the language, which are
  128. Xdescribed below.  I eventually decided that a new language designed from
  129. Xscratch could probably meet the constraints with less implementation effort
  130. Xthan any existing language.
  131. X
  132. XTcl is unusual because it presents two different interfaces: a textual
  133. Xinterface to users who issue Tcl commands, and a procedural interface to the
  134. Xapplications in which it is embedded.  Each of these interfaces must be
  135. Xsimple, powerful, and efficient.  There were four major factors in the
  136. Xlanguage design: 
  137. X
  138. X[1] The language is for commands.
  139. X
  140. XAlmost all Tcl ``programs'' will be short, many only one line long.  Most
  141. Xprograms will be typed in, executed once or perhaps a few times, and then
  142. Xdiscarded.  This suggests that the language should have a simple syntax so
  143. Xthat it is easy to type commands.  Most existing programming languages have
  144. Xcomplex syntax; the syntax is helpful when writing long programs but would
  145. Xbe clumsy if used for a command language.
  146. X
  147. X[2] The language must be programmable.
  148. X
  149. XIt should contain general programming constructs such as variables,
  150. Xprocedures, conditionals, and loops, so that users can extend the built-in
  151. Xcommand set by writing Tcl procedures.  Extensibility also argues for
  152. Xa simple syntax: this makes it easier for Tcl programs to generate other
  153. XTcl programs.
  154. X
  155. X[3] The language must permit a simple and efficient interpreter.
  156. X
  157. XFor the Tcl library to be included in many small programs, particularly
  158. Xon machines without shared-library facilities, the interpreter must not
  159. Xoccupy much memory.  The mechanism for interpreting Tcl commands must be
  160. Xfast enough to be usable for events that occur hundreds of times a second,
  161. Xsuch as mouse motion.
  162. X
  163. X[4] The language must permit a simple interface to C applications.
  164. X
  165. XIt must be easy for C applications to invoke the interpreter and easy
  166. Xfor them to extend the built-in commands with application-specific
  167. Xcommands.  This factor was one of the reasons why I decided not to
  168. Xuse Lisp as the command language: Lisp's basic data types and storage
  169. Xmanagement mechanisms are so different than those of C that it would
  170. Xbe difficult to build a clean and simple interface between them.
  171. X
  172. XFor Tcl I used a data type (string) that is natural to C.
  173. X
  174. X2.1.  Tcl Language Syntax
  175. X
  176. XTcl's basic syntax is similar to that of the UNIX shells: a command consists
  177. Xof one or more fields separated spaces or tabs.  The first field is the name
  178. Xof a command, which may be either a built-in command, an application-specific
  179. Xcommand, or a procedure consisting of a sequence of Tcl commands.  Fields
  180. Xafter the first one are passed to the command as arguments.  Newline
  181. Xcharacters are used as command separators, just as in the UNIX shells, and
  182. Xsemi-colons may be used to separate commands on the same line.  Unlike the
  183. XUNIX shells, each Tcl command returns a string result, or the empty string
  184. Xif a return value isn't appropriate.
  185. X
  186. XThere are four additional syntactic constructs in Tcl, which give the language
  187. Xa Lisp-like flavor.  Curly braces are used to group complex arguments; they
  188. Xact as nestable quote characters.  If the first character of an argument is a
  189. Xopen brace, then the argument is not terminated by white space.  Instead, it
  190. Xis terminated by the matching close brace.  The argument passed to the command
  191. Xconsists of everything between the braces, with the enclosing braces stripped
  192. Xoff.  For example, the command
  193. X
  194. X    set a {dog cat {horse cow mule} bear}
  195. X
  196. Xwill receive two arguments: ``a'' and ``dog cat {horse cow mule} bear''.
  197. XThis particular command will set the variable
  198. X    a
  199. Xto a string equal to the second argument.  If an argument is enclosed
  200. Xin braces, then none of the other substitutions described below is made
  201. Xon the argument.  One of the most common uses of braces is to specify a
  202. XTcl subprogram as an argument to a Tcl command.
  203. X
  204. XThe second syntactic construct in Tcl is square brackets, which are used to
  205. Xinvoke command substitution.  If an open bracket appears in an argument, then
  206. Xeverything from the open bracket up to the matching close bracket is treated
  207. Xas a command and executed recursively by the Tcl interpreter.  The result of
  208. Xthe command is then substituted into the argument in place of the bracketed
  209. Xstring.  For example, consider the command
  210. X
  211. X    set a [format {Santa Claus is %s years old} 99]
  212. X
  213. XThe format command does printf-like formatting and returns the string
  214. X``Santa Claus is 99 years old'', which is then passed to set and assigned
  215. Xto variable a.
  216. X
  217. XThe third syntactic construct is the dollar sign, which is used for variable
  218. Xsubstitution.  If it appears in an argument then the following characters are
  219. Xtreated as a variable name; the contents of the variable are substituted into
  220. Xthe argument in place of the dollar sign and name.  For example, the commands
  221. X
  222. Xset b 99
  223. Xset a [format {Santa Claus is %s years old} $b]
  224. X
  225. Xresult in the same final value for a as the single command in the previous
  226. Xparagraph.  Variable substitution isn't strictly necessary since there are 
  227. Xother ways to achieve the same effect, but it reduces typing.
  228. X
  229. XThe last syntactic construct is the backslash character, which may be used
  230. Xto insert special characters into arguments, such as curly braces or
  231. Xnon-printing characters.
  232. X
  233. X2.2.  Data Types
  234. X
  235. XThere is only one type of data in Tcl: strings.  All commands, arguments
  236. Xto commands, results returned by commands, and variable values are ASCII
  237. Xstrings.  The use of strings throughout Tcl makes it easy to pass information
  238. Xback and forth between Tcl library procedures and C code in the enclosing
  239. Xapplication.  It also makes it easier to pass Tcl-related information back
  240. Xand forth between machines of different types.
  241. X
  242. XAlthough everything in Tcl is a string, many commands expect their string
  243. Xarguments to have particular formats.  There are three particularly common
  244. Xformats for strings: lists, expressions, and commands.  A list is just a
  245. Xstring containing one or more fields separated by white space, similar to
  246. Xa command.  Curly braces may be used to enclose complex list elements; these
  247. Xcomplex list elements are often lists in their own right, as in Lisp.  For
  248. Xexample, the string
  249. X
  250. X    dog cat {horse cow mule} bear
  251. X
  252. Xis a list with four elements, the third of which is a list with three
  253. Xelements.  Tcl provides commands for a number of list-manipulation operations,
  254. Xsuch as creating lists, extracting elements, and computing list lengths.  
  255. X
  256. XThe second common form for a string is a numeric expression.  Tcl 
  257. Xexpressions have the same operators and precedence as expressions in C.
  258. XThe "expr" Tcl command evaluates a string as an expression and returns the
  259. Xresult (as a string, of course).  For example, the command
  260. X
  261. Xexpr {($a < $b) || ($c != 0)}
  262. X
  263. Xreturns ``0'' if the numeric value of variable a is less than that of
  264. Xvariable b, or if variable c is not zero; otherwise it returns
  265. X``0''.  Several other commands, such as "if" and "for", expect one or more 
  266. Xof their arguments to be expressions.
  267. X
  268. XThe third common interpretation of strings is as commands (or sequences of
  269. Xcommands).  Arguments of this form are used in Tcl commands that implement
  270. Xcontrol structures.  For example, consider the following command:
  271. X
  272. Xif {$a < $b} {
  273. X    set tmp $a
  274. X    set a $b
  275. X    set b $tmp
  276. X}
  277. X
  278. XThe "if" command receives two arguments here, each of which is delimited
  279. Xby curly braces.  "If" is a built-in command that evaluates its first
  280. Xargument as an expression; if the result is non-zero, "if" executes its
  281. Xsecond argument as a Tcl command.  This particular command swaps the
  282. Xvalues of the variables "a" and "b" if "a" is less than "b".
  283. X
  284. XTcl also allows users to define command procedures written in the Tcl
  285. Xlanguage.  I will refer to these procedures as tclproc's, in order to
  286. Xdistinguish them from other procedures written in C.  The "proc" built-in
  287. Xcommand is used to create a tclproc.  For example, here is a Tcl command
  288. Xthat defines a recursive factorial procedure:
  289. X
  290. Xproc fac x {
  291. X    if {$x == 1} {return 1}
  292. X    return [expr {$x * [fac [expr $x-1]]}]
  293. X}
  294. X
  295. XThe "proc" command takes three arguments: a name for the new tclproc, a
  296. Xlist of variable names (in this case the list has only a single element,
  297. X"x"), and a Tcl command that comprises the body of the tclproc.  Once
  298. Xthis proc command has been executed, "fac" may be invoked just like any
  299. Xother Tcl command.  For example 
  300. X
  301. X    fac 4
  302. X    
  303. Xwill return the string ``24''.  
  304. X
  305. XFigure 1 lists all of the built-in Tcl commands in groups.  In addition to
  306. Xthe commands already mentioned, Tcl provides commands for manipulating
  307. Xstrings (comparison, matching, and printf/scanf-like operations), commands
  308. Xfor manipulating files and file names, and a command to fork a subprocess
  309. Xand return the subprocess's standard output as result.  The built-in Tcl
  310. Xcommands provide a simple but complete programming language.  The built-in
  311. Xfacilities may be extended in three ways: by writing tclprocs; by invoking
  312. Xother programs as subprocesses; or by defining new commands with C
  313. Xprocedures as described in the next section.
  314. X
  315. X3.  Embedding Tcl in Applications
  316. X
  317. XAlthough the built-in Tcl commands could conceivably be used as a
  318. Xstand-alone programming system, Tcl is really intended to be embedded
  319. Xin application programs.  I have built several application programs using
  320. XTcl, one of which is a mouse-based editor for X called "mx".  In the rest
  321. Xof the paper I will use examples from mx to illustrate how Tcl interacts
  322. Xwith its enclosing application.  
  323. X
  324. XAn application using Tcl extends the built-in commands with a few
  325. Xadditional commands related to that particular application.  For
  326. Xexample, a clock program might provide additional commands to control
  327. Xhow the clock is displayed and to set alarms; the mx editor provides
  328. Xadditional commands to read a file from disk, display it in a window,
  329. Xselect and modify ranges of bytes, and write the modified file back to
  330. Xdisk.  An application programmer need only write the application-specific
  331. Xcommands; the built-in commands provide programmability and extensibility
  332. X``for free''.  To users, the application-specific commands appear the same
  333. Xas the built-in commands.  
  334. X
  335. XFigure 2 shows the relationship between Tcl and the rest of an application.
  336. XTcl is a C library package that is linked with the application.  The Tcl
  337. Xlibrary includes a parser for the Tcl language, procedures to execute the
  338. Xbuilt-in commands, and a set of utility procedures for things like expression
  339. Xevaluation and list management.  The parser includes an extension interface
  340. Xthat may be used to extend the language's command set.
  341. X
  342. XTo use Tcl, an application first creates an object called an "interpreter",
  343. Xusing the following library procedure:
  344. X
  345. X    Tcl_Interp *Tcl_CreateInterp()
  346. X
  347. XAn interpreter consists of a set of commands, a set of variable bindings,
  348. Xand a command execution state.  It is the basic unit manipulated by most
  349. Xof the Tcl library procedures.
  350. X
  351. XSimple applications will use only a single interpreter, while more complex
  352. Xapplications may use multiple interpreters for different purposes.  For
  353. Xexample, mx uses one interpreter for each window on the screen.
  354. X
  355. XThe Tcl library provides a parser for the Tcl language, a set of built-in
  356. Xcommands, and several utility procedures.  The application provides 
  357. Xapplication-specific commands plus procedures to collect commands for
  358. Xexecution.  The commands are parsed by Tcl and then passed to relevant
  359. Xcommand procedures (either in Tcl or in the application) for execution.
  360. X
  361. XOnce an application has created an interpreter, it calls the
  362. XTcl_CreateCommand procedure to extend the interpreter with
  363. Xapplication-specific commands:
  364. X
  365. Xtypedef int Tcl_CmdProc((ClientData) clientData, Tcl_Interp *interp,
  366. X    int argc, char *argv[]);
  367. X
  368. XTcl_CreateCommand(Tcl_Interp *interp, char *name, Tcl_CmdProc proc,
  369. X    ClientData clientData)
  370. X
  371. XEach call to Tcl_CreateCommand associates a particular command name
  372. X(name) with a procedure that implements that command (proc) and an
  373. Xarbitrary single-word value to pass to that procedure (clientData).
  374. X
  375. XAfter creating application-specific commands, the application enters
  376. Xa main loop that collects commands and passes them to the Tcl_Eval
  377. Xprocedure for execution:
  378. X
  379. X    int Tcl_Eval(Tcl_Interp *interp, char *cmd)
  380. X
  381. XIn the simplest form, an application might simply read commands from the
  382. Xterminal or from a file.  In the mx editor Tcl commands are associated
  383. Xwith events such as keystrokes, mouse buttons, or menu activations; each
  384. Xtime an event occurs, the corresponding Tcl command is passed to Tcl_Eval.
  385. X
  386. XThe Tcl_Eval procedure parses its cmd argument into fields, looks up the
  387. Xcommand name in the table of those associated with the interpreter, and
  388. Xinvokes the command procedure associated with that command.  All command
  389. Xprocedures, whether built-in or application-specific, are called in the
  390. Xsame way, as described in the typedef for Tcl_CmdProc above.
  391. XA command procedure is passed an array of strings describing the command's
  392. Xarguments (argc and argv) plus the clientData value that was associated
  393. Xwith the command when it was created.  ClientData is typically a pointer
  394. Xto an application-specific structure containing information needed to
  395. Xexecute the command.  For example, in mx the clientData argument points
  396. Xto a per-window data structure describing the file being edited and the
  397. Xwindow it is displayed in.
  398. X
  399. XControl mechanisms like "if" and "for" are implemented with recursive
  400. Xcalls to Tcl_Eval.  For example, the command procedure for the "if"
  401. Xcommand evaluates its first argument as an expression; if the result
  402. Xis non-zero, then it calls Tcl_Eval recursively to execute its second
  403. Xargument as a Tcl command.  During the execution of that command, Tcl_Eval
  404. Xmay be called recursively again, and so on.  Tcl_Eval also calls itself
  405. Xrecursively to execute bracketed commands that appear in arguments.
  406. X
  407. X
  408. XEven tclprocs such as fac use this same basic mechanism.
  409. XWhen the "proc" command is invoked to create "fac", the proc command
  410. Xprocedure creates a new command by calling Tcl_CreateCommand as
  411. Xillustrated in Figure 3.  The new command has the name "fac".  Its
  412. Xcommand procedure ("proc" in the call to Tcl_CreateCommand) is a
  413. Xspecial Tcl library procedure called "InterpProc", and its clientData
  414. Xis a pointer to a structure describing the tclproc.  This structure
  415. Xcontains, among other things, a copy of the body of the tclproc (the
  416. Xthird argument to the proc command).  When the fac command is invoked,
  417. XTcl_Eval calls InterpProc, which in turn calls Tcl_Eval to execute the
  418. Xbody of the tclproc.  There is some additional code required to associate
  419. Xthe argument of the fac command (which is passed to InterpProc in its argv
  420. Xarray) with the "x" variable used inside fac's body, and to support variables
  421. Xwith local scope, but much of the mechanism for tclprocs is the same as that
  422. Xfor any other Tcl command.
  423. X
  424. XThe creation and execution of a tclproc (a procedure written in Tcl):
  425. X(a) the proc command is invoked, e.g.  to create the fac procedure; (b)
  426. Xthe Tcl parser invokes the command procedure associated with proc; (c)
  427. Xthe proc command procedure creates a data structure to hold the Tcl
  428. Xcommand that is fac's body; (d) fac is registered as a new Tcl command,
  429. Xwith InterpProc as its command procedure; (e) fac is invoked as a Tcl
  430. Xcommand; (f) the Tcl parser invokes InterpProc as the command procedure
  431. Xfor fac; (g) InterpProc retrieves the body of fac from the data structure;
  432. Xand (h) the Tcl commands in fac's body are passed back to the Tcl parser
  433. Xfor execution.
  434. X
  435. XA Tcl command procedure returns two results to Tcl_Eval: an integer return
  436. Xcode and a string.  The return code is returned as the procedure's result,
  437. Xand the string is stored in the interpreter, from which it can be retrieved
  438. Xlater.  Tcl_Eval returns the same code and string to its caller.
  439. X
  440. XTable I summarizes the return codes and strings.
  441. X
  442. XNormally the return code is TCL_OK and the string contains the result of
  443. Xthe command.  If an error occurs in executing a command, then the return
  444. Xcode will be TCL_ERROR and the string will describe the error condition.
  445. XWhen TCL_ERROR is returned (or any value other than TCL_OK), the normal
  446. Xaction is for nested command procedures to return the same code and string
  447. Xto their callers, unwinding all pending command executions until eventually
  448. Xthe return code and string are returned by the top-level call to Tcl_Eval.
  449. XAt this point the application will normally display the error message for
  450. Xthe user by printing it on the terminal or displaying it in a notifier
  451. Xwindow.  
  452. X
  453. XReturn codes other than TCL_OK or TCL_ERROR cause partial unwinding.  For
  454. Xexample, the break command returns a TCL_BREAK code.  This causes nested
  455. Xcommand executions to be unwound until a nested "for" or "foreach" command
  456. Xis reached.  When a "for" or "foreach" command invokes Tcl_Eval recursively,
  457. Xit checks specially for the TCL_BREAK result.  When this occurs the "for" or
  458. X"foreach" command terminates the loop, but it doesn't return the TCL_BREAK
  459. Xcode to its caller.  Instead it returns TCL_OK.  Thus no higher levels of
  460. Xexecution are aborted.  The TCL_CONTINUE return code is also handled by the 
  461. X"for" and "foreach" commands (they go on to the next loop iteration) and
  462. XTCL_RETURN is handled by the InterpProc procedure.  Only a few command
  463. Xprocedures, like "break" and "for", know anything about special return codes
  464. Xsuch as TCL_BREAK; other command procedures simply abort whenever they see any
  465. Xreturn code other than TCL_OK.
  466. X
  467. XThe "catch" command may be used to prevent complete unwinding on TCL_ERROR
  468. Xreturns.  Catch takes an argument that is a Tcl command to execute.  It
  469. Xpasses the command to Tcl_Eval for execution, but always returns TCL_OK.
  470. XIf an error occurs in the command, catch's command procedure detects the
  471. XTCL_ERROR return value from Tcl_Eval, saves information about the error
  472. Xin Tcl variables, and then returns TCL_OK to its caller.  In almost all
  473. Xcases I think the best response to an error is to abort all command 
  474. Xinvocations and notify the user; catch is provided for those few occasions
  475. Xwhere an error is expected and can be handled without aborting.
  476. X
  477. X4.  Tcl and Window Applications
  478. X
  479. XAn embeddable command language like Tcl offers particular advantages in
  480. Xa windowing environment.  This is partly because there are many interactive
  481. Xprograms in a windowing environment (hence many places to use a command
  482. Xlanguage) and partly because configurability is important in today's
  483. Xwindowing environments and a language like Tcl provides the flexibility
  484. Xto reconfigure.
  485. X
  486. XTcl can be used for two purposes in a window application: to configure the
  487. Xapplication's interface actions, and to configure the application's
  488. Xinterface appearance.  These two purposes are discussed in the paragraphs
  489. Xbelow.
  490. X
  491. XThe first use of Tcl is for interface actions.  Ideally, each event that
  492. Xhas any importance to the application should be bound to a Tcl command.
  493. XEach keystroke, each mouse motion or mouse button press (or release), and
  494. Xeach menu entry should be associated with a Tcl command.  When the event
  495. Xoccurs, it is first mapped to its Tcl command and then executed by passing
  496. Xthe command to Tcl_Eval.  The application should not take any actions
  497. Xdirectly; all actions should first pass through Tcl.  Furthermore, the 
  498. Xapplication should provide Tcl commands that allow the user to change the
  499. XTcl command associated with any event.  
  500. X
  501. XIn interactive windowing applications, the use of Tcl will probably not be
  502. Xvisible to beginning users: they will manipulate the applications using
  503. Xbuttons, menus, and other interface components.  However, if Tcl is used as
  504. Xan intermediary for all interface actions then two advantages accrue.  First,
  505. Xit becomes possible to write Tcl programs to reconfigure the interface.
  506. XFor example, users will be able to rebind keystrokes, change mouse buttons,
  507. Xor replace an existing operation with a more complex one specified as a set of
  508. XTcl commands or tclprocs.  The second advantage is that this approach forces
  509. Xall of the application's functionality to be accessible through Tcl: anything
  510. Xthat can be invoked with the mouse or keyboard can also be invoked with Tcl
  511. Xprograms.  This makes it possible to write tclprocs that simulate the actions
  512. Xof the program, or that compose the program's basic actions into more powerful
  513. Xactions.  It also permits interactive sessions to be recorded and replayed as
  514. Xa sequence of Tcl commands (see Section 5).
  515. X
  516. XThe second use for Tcl in a window application is to configure the appearance
  517. Xof the application.  All of the application's interface components 
  518. X(``widgets'' in X terminology), such as labels, buttons, text entries, menus,
  519. Xand scrollbars, should be configured using Tcl commands.  For example, in
  520. Xthe case of a button the application (or the button widget code) should provide
  521. XTcl commands to change the button's size and location, its text, its colors,
  522. Xand the action (a Tcl command, of course) to invoke when the button is
  523. Xactivated.  This makes it possible for users to write Tcl programs to
  524. Xpersonalize the layout and appearance of the applications they use.  The most
  525. Xcommon use of such reconfigurability would probably be in Tcl command files
  526. Xread by programs automatically when they start execution.  However, the
  527. XTcl commands could also be used to change an application's appearance while
  528. Xit is running, if that should prove useful.
  529. X
  530. XIf Tcl is used as described above, then it could serve as a specification 
  531. Xlanguage for user interfaces.  User interface editors could be written to 
  532. Xdisplay widgets and let users re-arrange them and configure attributes such
  533. Xas colors and associated Tcl commands.  The interface editor could then 
  534. Xoutput information about the interface as a Tcl command file to be read by 
  535. Xthe application when it starts up.
  536. XSome current interface editors output C code which must then be compiled
  537. Xinto the application [7]; unfortunately this approach requires an
  538. Xapplication to be recompiled in order to change its interface (or,
  539. Xalternatively, it requires a dynamic-code-loading facility).  If Tcl
  540. Xwere used as the interface specification language then no recompilation
  541. Xwould be necessary and a single application binary could support many
  542. Xdifferent interfaces.
  543. X
  544. X5.  Communication Between Applications
  545. X
  546. XThe advantages of an embedded command language like Tcl become even
  547. Xgreater if all of the tools in an environment are based on the same
  548. Xlanguage.  First, users need only learn one basic command language;
  549. Xto move from one application to another they need only learn the
  550. X(few?) application-specific commands for the new application.  Second,
  551. Xgeneric interface editors become possible, as described in the previous
  552. Xsection.  Third, and most important in my view, Tcl can provide a means
  553. Xof communication between applications.
  554. X
  555. XI have implemented a communication mechanism for X11 in the form of an
  556. Xadditional Tcl command called "send".  For send to work, each Tcl
  557. Xinterpreter associated with an X11 application is given a textual name,
  558. Xsuch as "xmh" for an X mail handler or mx.foo.c for a window in which
  559. Xmx is displaying a file named foo.c.  The send command takes two arguments:
  560. Xthe name of an interpreter and a Tcl command to execute in that interpreter.
  561. X"Send" arranges for the command to be passed to the process containing the
  562. Xnamed interpreter; the command is executed by that interpreter and the
  563. Xresults (return code and string) are returned to the application that
  564. Xissued the "send" command.
  565. X
  566. XThe X11 implementation of send uses a special property attached to the
  567. Xroot window.  The property stores the names of all the interpreters plus
  568. Xa window identifier for each interpreter.  A command is sent to an interpreter
  569. Xby appending it to a particular property in the interpreter's associated
  570. Xwindow.  The property change is detected by the process that owns the
  571. Xinterpreter; it reads the property, executes the command, and appends
  572. Xresult information onto a property associated with the sending application.
  573. XFinally, the sending application detects this change of property, reads
  574. Xthe result information, and returns it as the result of the send command.
  575. X
  576. XThe send command provides a powerful way for one application to control
  577. Xanother.  For example, a debugger could send commands to an editor to
  578. Xhighlight the current source line as it single-steps through a program.
  579. XOr, a user interface editor could use send to manipulate an application's
  580. Xinterface directly: rather than modifying a dummy version of the
  581. Xapplication's interface displayed by the interface editor, the interface
  582. Xeditor could use send to modify the interface of a ``live'' application,
  583. Xwhile also saving the configuration for a configuration file.  This would
  584. Xallow an interface designer to try out the look and feel of a new interface
  585. Xincrementally as changes are made to the interface.
  586. X
  587. XAnother example of using send is for changing user preferences.  If one
  588. Xuser walks up to a display that has been configured for some other user,
  589. Xthe new user could run a program that finds out about all the existing
  590. Xapplications on the screen (by querying the property that contains their
  591. Xnames), reads the new user's configuration file for each application, and
  592. Xsends commands to that application to reconfigure it for the new user's
  593. Xpreferences.  When the old user returns, he or she could invoke the same
  594. Xprogram to restore the original preferences.
  595. X
  596. X"Send" could also be used to record interactive sessions involving multiple
  597. Xapplications and then replay the sessions later (e.g.  for demonstration
  598. Xpurposes).  This would require an additional Tcl command called trace;
  599. Xtrace would take a single argument (a Tcl command string) and cause that
  600. Xcommand string to be executed before each other command was executed in
  601. Xthat interpreter.  Within a single application, trace could be used to record
  602. Xeach Tcl command before it is executed, so that the commands could be replayed
  603. Xlater.  In a multi-application environment, a recorder program could be built
  604. Xusing send.  The recorder sends a trace command to each application to be
  605. Xrecorded.  The trace command arranges for information to be sent back
  606. Xto the recorder about each command executed in that application.  The
  607. Xrecorder then logs information about which applications executed which
  608. Xcommands.  The recorder can reexecute the commands by "send"-ing them
  609. Xback to the applications again.  The trace command does not yet exist
  610. Xin Tcl, but it could easily be added.
  611. X
  612. XSend provides a much more powerful mechanism for communication between
  613. Xapplications than is available today.  The only easy-to-use form of
  614. Xcommunication for today's applications is the selection or cut buffer:
  615. Xa single string of text that may be set by one application and read by
  616. Xanother.  Send provides a more general form of communication akin to
  617. Xremote procedure call [3].  If all of an application's functionality is
  618. Xmade available through Tcl, as described in Section 4, then send makes
  619. Xall of each application's functionality available to other applications
  620. Xas well.
  621. X
  622. XIf Tcl (and send) were to become widely used in window applications, I
  623. Xbelieve that a better kind of interactive environment would arise,
  624. Xconsisting of a large number of small specialized applications rather
  625. Xthan a few monolithic ones.  Today's applications cannot communicate
  626. Xwith each other very well, so each application must incorporate all
  627. Xthe functionality that it needs.  For example, some window-based debuggers
  628. Xcontain built-in text editors so that they can highlight the current
  629. Xpoint of execution.  With Tcl and send, the debugger and the editor could
  630. Xbe distinct programs, with each sending commands to the other as necessary.
  631. XIdeally, monolithic applications could be replaced by lots of small
  632. Xapplications that work together in exciting new ways, just as the UNIX
  633. Xshells allowed lots of small text processing applications to be combined
  634. Xtogether.  I think that Tcl, or some other language like it, will provide
  635. Xthe glue that binds together the windowing applications of the 1990's.
  636. X
  637. X6.  Status and Performance
  638. X
  639. XThe Tcl language was designed in the fall of 1987 and implemented in the
  640. Xwinter of 1988.  In the spring of 1988 I incorporated Tcl into the mx
  641. Xeditor (which already existed, but with an inferior command language),
  642. Xand also into a companion terminal emulator called Tx.  Both of these
  643. Xprograms have been in use by a small user community at Berkeley for
  644. Xthe last year and a half.  All of the Tcl language facilities exist as
  645. Xdescribed above, except that the send command is still in prototype form
  646. Xand trace hasn't been implemented.  Some of the features described in
  647. XSection 4, such as menu and keystroke bindings, are implemented in mx, but
  648. Xin an ad hoc fashion: Tcl is not yet integrated with a widget set.  I am
  649. Xcurrently building a new toolkit and widget set that is based entirely on
  650. XTcl.  When it is completed, I expect it to provide all of the features
  651. Xdescribed in Section 4.  As of this writing, the implementation has barely
  652. Xbegun.
  653. X
  654. XTable II shows how long it takes Tcl to execute various commands on two
  655. Xdifferent workstations.  On Sun-3 workstations, the average time for simple
  656. Xcommands is about 500 microseconds, while on DECstation 3100's the average
  657. Xtime per command is about 160 microseconds.  Although mx does not currently
  658. Xuse a Tcl command for each mouse motion event, the times in Table II suggest
  659. Xthat this would be possible, even on Sun-3 workstations, without significant
  660. Xdegradation of response.  For example, if mouse motion events occur 100 times
  661. Xper second, the Tcl overhead for dispatching one command per event will
  662. Xconsume only about 1-2% of a Sun-3 processor.
  663. XFor the ways in which Tcl is currently used (keystroke and menu bindings
  664. Xconsisting of a few commands), there are no noticeable delays associated
  665. Xwith Tcl.  For application-specific commands such as those for the mx editor,
  666. Xthe time to execute the command is much greater than the time required by
  667. XTcl to parse it and call the command procedure.  
  668. X
  669. XThe Tcl library is small enough to be used in a wide variety of programs, even
  670. Xon systems without mechanisms for sharing libraries.  The Tcl code consists of 
  671. Xabout 7000 lines of C code (about half of which is comments).  When compiled
  672. Xfor a Motorola 68000, it generates about 27000 bytes of object code.
  673. X
  674. X7.  Comparisons
  675. X
  676. XThe Tcl language has quite a bit of surface similarity to Lisp, except
  677. Xthat Tcl uses curly braces or brackets instead of parentheses and no braces
  678. Xare needed around the outermost level of a command.  The greatest difference
  679. Xbetween Tcl and Lisp is that Lisp evaluates arguments by default, whereas
  680. Xin Tcl arguments are not evaluated unless surrounded by brackets.  This
  681. Xmeans that more typing effort is required in Tcl if an argument is to be
  682. Xevaluated, and more typing effort is required in Lisp if an argument is
  683. Xto be quoted (not evaluated).
  684. XIt appeared to me that no-evaluation is usually the desired result in
  685. Xarguments to a command language, so I made this the default in Tcl.
  686. XTcl also has fewer data types than Lisp; this was done in order to simplify
  687. Xthe interface between the Tcl library and an enclosing C application.  
  688. X
  689. XThe Emacs editor is similar to Tcl in that it provides a framework that
  690. Xcan be used to control many different application programs.  For example,
  691. Xsubprocesses can be run in Emacs windows and users can write Emacs command
  692. Xscripts that (a) generate command sequences for input to the applications
  693. Xand (b) re-format the output of applications.  This allows users to embellish
  694. Xthe basic facilities of applications, edit their output, and so on.
  695. X
  696. XThe difference between Emacs and Tcl is that the programmability is
  697. Xcentralized in Emacs: applications cannot talk to each other unless Emacs
  698. Xacts as intermediary (e.g.  to set up a new communication mechanism between
  699. Xtwo applications, code must be written in Emacs to pass information back
  700. Xand forth between the applications).  The Tcl approach is decentralized:
  701. Xeach application has its own command interpreter and applications may
  702. Xcommunicate directly with each other.
  703. X
  704. XLastly, it is interesting to compare Tcl to NeWS [9], a window system that
  705. Xis based on the Postscript language.  NeWS allows applications to down-load
  706. Xa window server in order to change the user interface and modify other
  707. Xaspects of the system.  In a sense, this is similar to the "send" command in
  708. XTcl, in that applications may send programs to the server for execution.
  709. XHowever, the NeWS mechanism is less general than Tcl: NeWS applications
  710. Xgenerate Postscript programs as output but they do not necessarily respond
  711. Xto Postscript programs as input.  In other words, NeWS applications can
  712. Xaffect each others' interfaces, by controlling the server, but they cannot
  713. Xdirectly invoke each others' application-specific operations as they can
  714. Xwith Tcl.
  715. X
  716. XTo summarize, the Tcl approach is less centralized than either the Emacs
  717. Xor NeWS approaches.  For a windowing environment with large numbers of
  718. Xindependent tools, I think the decentralized approach makes sense.
  719. XIn fairness to Emacs, it's important to point out that Emacs wasn't designed
  720. Xfor this environment, and that Emacs works quite nicely in the environment
  721. Xfor which it was designed (ASCII terminals with batch-style applications).
  722. XIt's also worth noting that direct communication between applications was
  723. Xnot an explicit goal of the NeWS system design.
  724. X
  725. X8.  Conclusions
  726. X
  727. XI think that Tcl could improve our interactive environments in three general
  728. Xways.  First, Tcl can be used to improve individual tools by providing them
  729. Xwith a programmable command language; this allows users to customize tools
  730. Xand extend their functionality.  Second, Tcl can provide a uniform command 
  731. Xlanguage across a range of tools; this makes it easier for users to program 
  732. Xthe tools and also allows tool-independent facilities to be built, such as 
  733. Xinterface editors.  Third, Tcl provides a mechanism for tools to control
  734. Xeach other; this encourages a more modular approach to windowing applications
  735. Xand makes it possible to re-use old applications in new ways.  In my opinion
  736. Xthe third benefit is potentially the most important.  
  737. X
  738. XMy experiences with Tcl so far are positive but limited.  Tcl needs a larger
  739. Xuser community and a more complete integration into a windowing toolkit before
  740. Xit can be fully evaluated.  The Tcl library source code is currently available
  741. Xto the public in a free, unlicensed form, and I hope to produce a Tcl-based
  742. Xtoolkit in the near future.
  743. X
  744. X9.  Acknowledgments
  745. X
  746. XThe members of the Sprite project acted as guinea pigs for the editor and
  747. Xterminal emulator based on Tcl; without their help the language would
  748. Xnot have evolved to its current state.  Fred Douglis, John Hartman,
  749. XKen Shirriff, and Brent Welch provided helpful comments that improved the
  750. Xpresentation of this paper.
  751. X
  752. X10.  References
  753. X
  754. X[1] Abelson, H.  and Sussman, G.J.
  755. X    Structure and Interpretation of Computer Programs,
  756. X    MIT Press, Cambridge, MA, 1985.
  757. X
  758. X[2] Adobe Systems, Inc.
  759. X    Postscript Language Tutorial and Cookbook,
  760. X    Addison-Wesley, Reading, MA, 1985.
  761. X
  762. X[3] Birrell, A.  and Nelson, B.  
  763. X    ``Implementing Remote Procedure Calls.''
  764. X    ACM Transactions on Computer Systems,
  765. X    Vol.  2, No.  1, February 1986, pp.  39-59.
  766. X
  767. X[4] Brodie, L.
  768. X    Starting FORTH: An Introduction to the FORTH Language and
  769. X        Operating System for Beginners and Professionals,
  770. X    Prentice Hall, Englewood Cliffs, NJ, 1981.
  771. X
  772. X[5] Kernighan, B.W.  and Pike, R.
  773. X    The UNIX Programming Environment,
  774. X    Prentice Hall, Englewood Cliffs, NJ, 1984.
  775. X
  776. X[6] Kernighan, B.W.  and Ritchie, D.M.
  777. X    The C Programming Language,
  778. X    Second Edition,
  779. X    Prentice Hall, Englewood Cliffs, NJ, 1988.
  780. X
  781. X[7] Mackey, K., Downs, M., Duffy, J., and Leege, J.
  782. X    ``An Interactive Interface Builder for Use with Ada Programs,''
  783. X    Xhibition Conference Proceedings, 1989.
  784. X
  785. X[8] Stallman, R.
  786. X    GNU Emacs Manual,
  787. X    Fourth Edition,
  788. X    Version 17,
  789. X    February 1986.
  790. X
  791. X[9] Sun Microsystems, Inc.
  792. X    NeWS Technical Overview,
  793. X    Sun Microsystems, Inc.
  794. X    PN 800-1498-05, 1987.
  795. X
  796. END_OF_FILE
  797. if test 41391 -ne `wc -c <'tcl6.1/doc/usenix.text'`; then
  798.     echo shar: \"'tcl6.1/doc/usenix.text'\" unpacked with wrong size!
  799. fi
  800. # end of 'tcl6.1/doc/usenix.text'
  801. fi
  802. echo shar: End of archive 31 \(of 33\).
  803. cp /dev/null ark31isdone
  804. MISSING=""
  805. for I in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 ; do
  806.     if test ! -f ark${I}isdone ; then
  807.     MISSING="${MISSING} ${I}"
  808.     fi
  809. done
  810. if test "${MISSING}" = "" ; then
  811.     echo You have unpacked all 33 archives.
  812.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  813. else
  814.     echo You still need to unpack the following archives:
  815.     echo "        " ${MISSING}
  816. fi
  817. ##  End of shell archive.
  818. exit 0
  819.  
  820. exit 0 # Just in case...
  821. -- 
  822. Kent Landfield                   INTERNET: kent@sparky.IMD.Sterling.COM
  823. Sterling Software, IMD           UUCP:     uunet!sparky!kent
  824. Phone:    (402) 291-8300         FAX:      (402) 291-4362
  825. Please send comp.sources.misc-related mail to kent@uunet.uu.net.
  826.