Tcl/Tk 烹调书 - 增加扩展


目的

本章描述如何建造包含你自己选择的 Tcl/Tk 库的 Tcl 解释器。如果没有一个集成版本的 tclshwish,对于每个你要使用的扩展,你将运行特定的 tclshwish,它的名字通常用某个 Tcl/Tk 扩展的名字作为为前导。这些解释器是为特定的扩展而编译的。否则将不能识别 Tcl/Tk 扩展的命令。象我们在第6章中已经见到的那样,Tcl/Tk 提供了包含 Tcl/Tk 扩展库并建立一个集成版本的模版。

你需要

你需要下载,解包(untar)并建造你打算集成的扩展的源代码。

注意如果你打算使用 itcl 和 C++ 作为你的应用编程接口的话,你需要同在 itcl 发布中 Tcl 和 Tk 库一起编译你的扩展。为此首先要建造 itcl。

端盘子

为所有 Tcl/Tk 扩展库,如 BLT 和 Expect,建立一个集成的 Tcl_AppInit。

菜谱

这个脚本的模板是在 Tcl/Tk 源代码发布中的 tkAppInit.c(在放置 Tk 源代码的目录下查找 - 象 ~pdsrc/TclTk/tk4.0 一类的东西)。复制一份 tkAppInit.c 到你的工作目录中。重命名为 STclInit.c (在 ~cookbook/code/ch9 下的源代码中还有一个 Makefile 模版)。

在下面的脚本中,使用粗体字来突出在模版代码中定制部分:

STclInit.c


#ifndef lint
static char sccsid[] = "@(#) tkAppInit.c 1.12 94/12/17 16:30:56";
#endif /* not lint */
#include "tcl.h"
#include "tk.h"

extern char *exp_argv0; /* For expect */

/*
 * The following variable is a special hack that is needed in order for
 * Sun shared libraries to be used for Tcl.
 */

#ifdef NEED_MATHERR
extern int matherr();
int *tclDummyMathPtr = (int *) matherr;
#endif
/*
 *----------------------------------------------------------------------
 *
 * Tcl_AppInit --
 *
 *	This procedure performs application-specific initialization.
 *	Most applications, especially those that incorporate additional
 *	packages, will have their own version of this procedure.
 *
 * Results:
 *	Returns a standard Tcl completion code, and leaves an error
 *	message in interp->result if an error occurs.
 *
 * Side effects:
 *	Depends on the startup script.
 *
 *----------------------------------------------------------------------
 */


int
Tcl_AppInit(interp)
    Tcl_Interp *interp;		/* Interpreter for application. */
{
    
Tk_Window mainwin;

    mainwin = Tk_MainWindow(interp);

    if (Tcl_Init(interp) == TCL_ERROR) {
	return TCL_ERROR;
    }
    if (Tk_Init(interp) == TCL_ERROR) {
	return TCL_ERROR;
    }

    /*
     * Call the init procedures for included packages.  Each call should
     * look like this:
     *
     * if (Mod_Init(interp) == TCL_ERROR) {
     *     return TCL_ERROR;
     * }
     *
     * where "Mod" is the name of the module.
     */

if (Blt_Init(interp) == TCL_ERROR) {
          return TCL_ERROR;
      }
if (Exp_Init(interp) == TCL_ERROR) {
        return TCL_ERROR;
     }

    /*
     * Call Tcl_CreateCommand for application-specific commands, if
     * they weren't already created by the init procedures called above.
     */

    /*
     * Specify a user-specific startup file to invoke if the application
     * is run interactively.  Typically the startup file is "~/.apprc"
     * where "app" is the name of the application.  If this line is deleted
     * then no user-specific startup file will be run under any conditions.
     */

	tcl_RcFileName ="~/.stclrc";

        return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * main --
 *
 *	This is the main program for the application.
 *
 * Results:
 *	None: Tk_Main never returns here, so this procedure never
 *	returns either.
 *
 * Side effects:
 *	Whatever the application does.
 *
 *----------------------------------------------------------------------
 */

int
main(argc, argv)
    int argc;			/* Number of command-line arguments. */
    char **argv;		/* Values of command-line arguments. */
{
    Tk_Main(argc, argv, Tcl_AppInit);
    return 0;			/* Needed only to prevent compiler warning. */
}


在上面的代码中,在第一段粗体字之前的的那段注释给出了调用 Tcl/Tk 扩展模块的初始化过程的准确的 Tcl 语法。随后的部分包含了 BLT_Init (用于有众多(mega)组件和命令的 BLT) 和 Exp_Init(用于 Expect)。

一个叫 Make_stcl 的 Makefile 模版在 ~cookbook/code/ch9 中。复制这个 makefile 模版,为你的站点(site)定制它并通过键入下面的命令行来编译 STclInit.c:



		make -f Make_stcl

最终的可执行文件叫 stcl。你可以交互的调用它。结果类似于(wish shell 被放置到调用它的窗口中来在一张图片中捕获两个窗口):

你可以使用这个 shell 来执行 Expect 和/或 BLT 命令。在下一章中给出两个非常简单的应用,一个给 Expect 另一个给 BLT 图表(graph)组件。