home *** CD-ROM | disk | FTP | other *** search
/ Ultra Pack / UltraComputing Partner Applications.iso / SunLabs / tclTK / src / tcl7.4 / tclTest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-08  |  26.5 KB  |  934 lines

  1. /* 
  2.  * tclTest.c --
  3.  *
  4.  *    This file contains C command procedures for a bunch of additional
  5.  *    Tcl commands that are used for testing out Tcl's C interfaces.
  6.  *    These commands are not normally included in Tcl applications;
  7.  *    they're only used for testing.
  8.  *
  9.  * Copyright (c) 1993-1994 The Regents of the University of California.
  10.  * Copyright (c) 1994-1995 Sun Microsystems, Inc.
  11.  *
  12.  * See the file "license.terms" for information on usage and redistribution
  13.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  14.  */
  15.  
  16. #ifndef lint
  17. static char sccsid[] = "@(#) tclTest.c 1.25 95/06/08 10:55:56";
  18. #endif /* not lint */
  19.  
  20. #include "tclInt.h"
  21. #include "tclPort.h"
  22.  
  23. /*
  24.  * The following variable is a special hack that is needed in order for
  25.  * Sun shared libraries to be used for Tcl.
  26.  */
  27.  
  28. extern int matherr();
  29. int *tclDummyMathPtr = (int *) matherr;
  30.  
  31. /*
  32.  * Dynamic string shared by TestdcallCmd and DelCallbackProc;  used
  33.  * to collect the results of the various deletion callbacks.
  34.  */
  35.  
  36. static Tcl_DString delString;
  37. static Tcl_Interp *delInterp;
  38.  
  39. /*
  40.  * One of the following structures exists for each asynchronous
  41.  * handler created by the "testasync" command".
  42.  */
  43.  
  44. typedef struct TestAsyncHandler {
  45.     int id;                /* Identifier for this handler. */
  46.     Tcl_AsyncHandler handler;        /* Tcl's token for the handler. */
  47.     char *command;            /* Command to invoke when the
  48.                      * handler is invoked. */
  49.     struct TestAsyncHandler *nextPtr;    /* Next is list of handlers. */
  50. } TestAsyncHandler;
  51.  
  52. static TestAsyncHandler *firstHandler = NULL;
  53.  
  54. /*
  55.  * The variable below is a token for an asynchronous handler for
  56.  * interrupt signals, or NULL if none exists.
  57.  */
  58.  
  59. static Tcl_AsyncHandler intHandler;
  60.  
  61. /*
  62.  * The dynamic string below is used by the "testdstring" command
  63.  * to test the dynamic string facilities.
  64.  */
  65.  
  66. static Tcl_DString dstring;
  67.  
  68. /*
  69.  * Forward declarations for procedures defined later in this file:
  70.  */
  71.  
  72. static int        AsyncHandlerProc _ANSI_ARGS_((ClientData clientData,
  73.                 Tcl_Interp *interp, int code));
  74. static void        CmdDelProc1 _ANSI_ARGS_((ClientData clientData));
  75. static void        CmdDelProc2 _ANSI_ARGS_((ClientData clientData));
  76. static int        CmdProc1 _ANSI_ARGS_((ClientData clientData,
  77.                 Tcl_Interp *interp, int argc, char **argv));
  78. static int        CmdProc2 _ANSI_ARGS_((ClientData clientData,
  79.                 Tcl_Interp *interp, int argc, char **argv));
  80. static void        DelCallbackProc _ANSI_ARGS_((ClientData clientData,
  81.                 Tcl_Interp *interp));
  82. static int        IntHandlerProc _ANSI_ARGS_((ClientData clientData,
  83.                 Tcl_Interp *interp, int code));
  84. static void        IntProc();
  85. static void        SpecialFree _ANSI_ARGS_((char *blockPtr));
  86. static int        TestasyncCmd _ANSI_ARGS_((ClientData dummy,
  87.                 Tcl_Interp *interp, int argc, char **argv));
  88. static int        TestcmdinfoCmd _ANSI_ARGS_((ClientData dummy,
  89.                 Tcl_Interp *interp, int argc, char **argv));
  90. static int        TestcmdtokenCmd _ANSI_ARGS_((ClientData dummy,
  91.                 Tcl_Interp *interp, int argc, char **argv));
  92. static int        TestdcallCmd _ANSI_ARGS_((ClientData dummy,
  93.                 Tcl_Interp *interp, int argc, char **argv));
  94. static int        TestdstringCmd _ANSI_ARGS_((ClientData dummy,
  95.                 Tcl_Interp *interp, int argc, char **argv));
  96. static int        TestlinkCmd _ANSI_ARGS_((ClientData dummy,
  97.                 Tcl_Interp *interp, int argc, char **argv));
  98. static int        TestMathFunc _ANSI_ARGS_((ClientData clientData,
  99.                 Tcl_Interp *interp, Tcl_Value *args,
  100.                 Tcl_Value *resultPtr));
  101. static int        TestupvarCmd _ANSI_ARGS_((ClientData dummy,
  102.                 Tcl_Interp *interp, int argc, char **argv));
  103.  
  104. /*
  105.  *----------------------------------------------------------------------
  106.  *
  107.  * main --
  108.  *
  109.  *    This is the main program for the application.
  110.  *
  111.  * Results:
  112.  *    None: Tcl_Main never returns here, so this procedure never
  113.  *    returns either.
  114.  *
  115.  * Side effects:
  116.  *    Whatever the application does.
  117.  *
  118.  *----------------------------------------------------------------------
  119.  */
  120.  
  121. int
  122. main(argc, argv)
  123.     int argc;            /* Number of command-line arguments. */
  124.     char **argv;        /* Values of command-line arguments. */
  125. {
  126.     Tcl_Main(argc, argv, Tcl_AppInit);
  127.     return 0;            /* Needed only to prevent compiler warning. */
  128. }
  129.  
  130. /*
  131.  *----------------------------------------------------------------------
  132.  *
  133.  * Tcl_AppInit --
  134.  *
  135.  *    This procedure performs application-specific initialization.
  136.  *    Most applications, especially those that incorporate additional
  137.  *    packages, will have their own version of this procedure.
  138.  *
  139.  * Results:
  140.  *    Returns a standard Tcl completion code, and leaves an error
  141.  *    message in interp->result if an error occurs.
  142.  *
  143.  * Side effects:
  144.  *    Depends on the startup script.
  145.  *
  146.  *----------------------------------------------------------------------
  147.  */
  148.  
  149. int
  150. Tcl_AppInit(interp)
  151.     Tcl_Interp *interp;        /* Interpreter for application. */
  152. {
  153.     /*
  154.      * Call the init procedures for included packages.  Each call should
  155.      * look like this:
  156.      *
  157.      * if (Mod_Init(interp) == TCL_ERROR) {
  158.      *     return TCL_ERROR;
  159.      * }
  160.      *
  161.      * where "Mod" is the name of the module.
  162.      */
  163.  
  164.     if (Tcl_Init(interp) == TCL_ERROR) {
  165.     return TCL_ERROR;
  166.     }
  167.  
  168.     /*
  169.      * Create additional commands and math functions for testing Tcl.
  170.      */
  171.  
  172.     Tcl_CreateCommand(interp, "testasync", TestasyncCmd, (ClientData) 0,
  173.         (Tcl_CmdDeleteProc *) NULL);
  174.     Tcl_CreateCommand(interp, "testcmdtoken", TestcmdtokenCmd, (ClientData) 0,
  175.         (Tcl_CmdDeleteProc *) NULL);
  176.     Tcl_CreateCommand(interp, "testcmdinfo", TestcmdinfoCmd, (ClientData) 0,
  177.         (Tcl_CmdDeleteProc *) NULL);
  178.     Tcl_CreateCommand(interp, "testdcall", TestdcallCmd, (ClientData) 0,
  179.         (Tcl_CmdDeleteProc *) NULL);
  180.     Tcl_DStringInit(&dstring);
  181.     Tcl_CreateCommand(interp, "testdstring", TestdstringCmd, (ClientData) 0,
  182.         (Tcl_CmdDeleteProc *) NULL);
  183.     Tcl_CreateCommand(interp, "testlink", TestlinkCmd, (ClientData) 0,
  184.         (Tcl_CmdDeleteProc *) NULL);
  185.     Tcl_CreateCommand(interp, "testupvar", TestupvarCmd, (ClientData) 0,
  186.         (Tcl_CmdDeleteProc *) NULL);
  187.     Tcl_CreateMathFunc(interp, "T1", 0, (Tcl_ValueType *) NULL, TestMathFunc,
  188.         (ClientData) 123);
  189.     Tcl_CreateMathFunc(interp, "T2", 0, (Tcl_ValueType *) NULL, TestMathFunc,
  190.         (ClientData) 345);
  191.  
  192.     /*
  193.      * Specify a user-specific startup file to invoke if the application
  194.      * is run interactively.  If this line is deleted then no user-specific
  195.      * startup file will be run under any conditions.
  196.      */
  197.  
  198.     tcl_RcFileName = "~/.tclshrc";
  199.     return TCL_OK;
  200. }
  201.  
  202. /*
  203.  *----------------------------------------------------------------------
  204.  *
  205.  * TestasyncCmd --
  206.  *
  207.  *    This procedure implements the "testasync" command.  It is used
  208.  *    to test the asynchronous handler facilities of Tcl.
  209.  *
  210.  * Results:
  211.  *    A standard Tcl result.
  212.  *
  213.  * Side effects:
  214.  *    Creates, deletes, and invokes handlers.
  215.  *
  216.  *----------------------------------------------------------------------
  217.  */
  218.  
  219.     /* ARGSUSED */
  220. static int
  221. TestasyncCmd(dummy, interp, argc, argv)
  222.     ClientData dummy;            /* Not used. */
  223.     Tcl_Interp *interp;            /* Current interpreter. */
  224.     int argc;                /* Number of arguments. */
  225.     char **argv;            /* Argument strings. */
  226. {
  227.     TestAsyncHandler *asyncPtr, *prevPtr;
  228.     int id, code;
  229.     static int nextId = 1;
  230.  
  231.     if (argc < 2) {
  232.     wrongNumArgs:
  233.     interp->result = "wrong # args";
  234.     return TCL_ERROR;
  235.     }
  236.     if (strcmp(argv[1], "create") == 0) {
  237.     if (argc != 3) {
  238.         goto wrongNumArgs;
  239.     }
  240.     asyncPtr = (TestAsyncHandler *) ckalloc(sizeof(TestAsyncHandler));
  241.     asyncPtr->id = nextId;
  242.     nextId++;
  243.     asyncPtr->handler = Tcl_AsyncCreate(AsyncHandlerProc,
  244.         (ClientData) asyncPtr);
  245.     asyncPtr->command = ckalloc((unsigned) (strlen(argv[2]) + 1));
  246.     strcpy(asyncPtr->command, argv[2]);
  247.     asyncPtr->nextPtr = firstHandler;
  248.     firstHandler = asyncPtr;
  249.     sprintf(interp->result, "%d", asyncPtr->id);
  250.     } else if (strcmp(argv[1], "delete") == 0) {
  251.     if (argc == 2) {
  252.         while (firstHandler != NULL) {
  253.         asyncPtr = firstHandler;
  254.         firstHandler = asyncPtr->nextPtr;
  255.         Tcl_AsyncDelete(asyncPtr->handler);
  256.         ckfree(asyncPtr->command);
  257.         ckfree((char *) asyncPtr);
  258.         }
  259.         return TCL_OK;
  260.     }
  261.     if (argc != 3) {
  262.         goto wrongNumArgs;
  263.     }
  264.     if (Tcl_GetInt(interp, argv[2], &id) != TCL_OK) {
  265.         return TCL_ERROR;
  266.     }
  267.     for (prevPtr = NULL, asyncPtr = firstHandler; asyncPtr != NULL;
  268.         prevPtr = asyncPtr, asyncPtr = asyncPtr->nextPtr) {
  269.         if (asyncPtr->id != id) {
  270.         continue;
  271.         }
  272.         if (prevPtr == NULL) {
  273.         firstHandler = asyncPtr->nextPtr;
  274.         } else {
  275.         prevPtr->nextPtr = asyncPtr->nextPtr;
  276.         }
  277.         Tcl_AsyncDelete(asyncPtr->handler);
  278.         ckfree(asyncPtr->command);
  279.         ckfree((char *) asyncPtr);
  280.         break;
  281.     }
  282.     } else if (strcmp(argv[1], "int") == 0) {
  283.     if (argc != 2) {
  284.         goto wrongNumArgs;
  285.     }
  286.     intHandler = Tcl_AsyncCreate(IntHandlerProc, (ClientData) interp);
  287.     signal(SIGINT, IntProc);
  288.     } else if (strcmp(argv[1], "mark") == 0) {
  289.     if (argc != 5) {
  290.         goto wrongNumArgs;
  291.     }
  292.     if ((Tcl_GetInt(interp, argv[2], &id) != TCL_OK)
  293.         || (Tcl_GetInt(interp, argv[4], &code) != TCL_OK)) {
  294.         return TCL_ERROR;
  295.     }
  296.     for (asyncPtr = firstHandler; asyncPtr != NULL;
  297.         asyncPtr = asyncPtr->nextPtr) {
  298.         if (asyncPtr->id == id) {
  299.         Tcl_AsyncMark(asyncPtr->handler);
  300.         break;
  301.         }
  302.     }
  303.     Tcl_SetResult(interp, argv[3], TCL_VOLATILE);
  304.     return code;
  305.     } else {
  306.     Tcl_AppendResult(interp, "bad option \"", argv[1],
  307.         "\": must be create, delete, int, or mark",
  308.         (char *) NULL);
  309.     return TCL_ERROR;
  310.     }
  311.     return TCL_OK;
  312. }
  313.  
  314. static int
  315. AsyncHandlerProc(clientData, interp, code)
  316.     ClientData clientData;    /* Pointer to TestAsyncHandler structure. */
  317.     Tcl_Interp *interp;        /* Interpreter in which command was
  318.                  * executed, or NULL. */
  319.     int code;            /* Current return code from command. */
  320. {
  321.     TestAsyncHandler *asyncPtr = (TestAsyncHandler *) clientData;
  322.     char *listArgv[4];
  323.     char string[20], *cmd;
  324.  
  325.     sprintf(string, "%d", code);
  326.     listArgv[0] = asyncPtr->command;
  327.     listArgv[1] = interp->result;
  328.     listArgv[2] = string;
  329.     listArgv[3] = NULL;
  330.     cmd = Tcl_Merge(3, listArgv);
  331.     code = Tcl_Eval(interp, cmd);
  332.     ckfree(cmd);
  333.     return code;
  334. }
  335.  
  336. static void
  337. IntProc()
  338. {
  339.     Tcl_AsyncMark(intHandler);
  340. }
  341.  
  342. static int
  343. IntHandlerProc(clientData, interp, code)
  344.     ClientData clientData;    /* Interpreter in which to invoke command. */
  345.     Tcl_Interp *interp;        /* Interpreter in which command was
  346.                  * executed, or NULL. */
  347.     int code;            /* Current return code from command. */
  348. {
  349.     char *listArgv[4];
  350.     char string[20], *cmd;
  351.  
  352.     interp = (Tcl_Interp *) clientData;
  353.     listArgv[0] = Tcl_GetVar(interp, "sigIntCmd", TCL_GLOBAL_ONLY);
  354.     if (listArgv[0] == NULL) {
  355.     return code;
  356.     }
  357.     listArgv[1] = interp->result;
  358.     sprintf(string, "%d", code);
  359.     listArgv[2] = string;
  360.     listArgv[3] = NULL;
  361.     cmd = Tcl_Merge(3, listArgv);
  362.     code = Tcl_Eval(interp, cmd);
  363.     ckfree(cmd);
  364.     return code;
  365. }
  366.  
  367. /*
  368.  *----------------------------------------------------------------------
  369.  *
  370.  * TestdcallCmd --
  371.  *
  372.  *    This procedure implements the "testdcall" command.  It is used
  373.  *    to test Tcl_CallWhenDeleted.
  374.  *
  375.  * Results:
  376.  *    A standard Tcl result.
  377.  *
  378.  * Side effects:
  379.  *    Creates and deletes interpreters.
  380.  *
  381.  *----------------------------------------------------------------------
  382.  */
  383.  
  384.     /* ARGSUSED */
  385. static int
  386. TestdcallCmd(dummy, interp, argc, argv)
  387.     ClientData dummy;            /* Not used. */
  388.     Tcl_Interp *interp;            /* Current interpreter. */
  389.     int argc;                /* Number of arguments. */
  390.     char **argv;            /* Argument strings. */
  391. {
  392.     int i, id;
  393.  
  394.     delInterp = Tcl_CreateInterp();
  395.     Tcl_DStringInit(&delString);
  396.     for (i = 1; i < argc; i++) {
  397.     if (Tcl_GetInt(interp, argv[i], &id) != TCL_OK) {
  398.         return TCL_ERROR;
  399.     }
  400.     if (id < 0) {
  401.         Tcl_DontCallWhenDeleted(delInterp, DelCallbackProc,
  402.             (ClientData) (-id));
  403.     } else {
  404.         Tcl_CallWhenDeleted(delInterp, DelCallbackProc,
  405.             (ClientData) id);
  406.     }
  407.     }
  408.     Tcl_DeleteInterp(delInterp);
  409.     Tcl_DStringResult(interp, &delString);
  410.     return TCL_OK;
  411. }
  412.  
  413. /*
  414.  * The deletion callback used by TestdcallCmd:
  415.  */
  416.  
  417. static void
  418. DelCallbackProc(clientData, interp)
  419.     ClientData clientData;        /* Numerical value to append to
  420.                      * delString. */
  421.     Tcl_Interp *interp;            /* Interpreter being deleted. */
  422. {
  423.     int id = (int) clientData;
  424.     char buffer[10];
  425.  
  426.     sprintf(buffer, "%d", id);
  427.     Tcl_DStringAppendElement(&delString, buffer);
  428.     if (interp != delInterp) {
  429.     Tcl_DStringAppendElement(&delString, "bogus interpreter argument!");
  430.     }
  431. }
  432.  
  433. /*
  434.  *----------------------------------------------------------------------
  435.  *
  436.  * TestcmdinfoCmd --
  437.  *
  438.  *    This procedure implements the "testcmdinfo" command.  It is used
  439.  *    to test Tcl_GetCommandInfo, Tcl_SetCommandInfo, and command creation
  440.  *    and deletion.
  441.  *
  442.  * Results:
  443.  *    A standard Tcl result.
  444.  *
  445.  * Side effects:
  446.  *    Creates and deletes various commands and modifies their data.
  447.  *
  448.  *----------------------------------------------------------------------
  449.  */
  450.  
  451.     /* ARGSUSED */
  452. static int
  453. TestcmdinfoCmd(dummy, interp, argc, argv)
  454.     ClientData dummy;            /* Not used. */
  455.     Tcl_Interp *interp;            /* Current interpreter. */
  456.     int argc;                /* Number of arguments. */
  457.     char **argv;            /* Argument strings. */
  458. {
  459.     Tcl_CmdInfo info;
  460.  
  461.     if (argc != 3) {
  462.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  463.         " option cmdName\"", (char *) NULL);
  464.     return TCL_ERROR;
  465.     }
  466.     if (strcmp(argv[1], "create") == 0) {
  467.     Tcl_CreateCommand(interp, argv[2], CmdProc1, (ClientData) "original",
  468.         CmdDelProc1);
  469.     } else if (strcmp(argv[1], "delete") == 0) {
  470.     Tcl_DStringInit(&delString);
  471.     Tcl_DeleteCommand(interp, argv[2]);
  472.     Tcl_DStringResult(interp, &delString);
  473.     } else if (strcmp(argv[1], "get") == 0) {
  474.     if (Tcl_GetCommandInfo(interp, argv[2], &info) ==0) {
  475.         interp->result = "??";
  476.         return TCL_OK;
  477.     }
  478.     if (info.proc == CmdProc1) {
  479.         Tcl_AppendResult(interp, "CmdProc1", " ",
  480.             (char *) info.clientData, (char *) NULL);
  481.     } else if (info.proc == CmdProc2) {
  482.         Tcl_AppendResult(interp, "CmdProc2", " ",
  483.             (char *) info.clientData, (char *) NULL);
  484.     } else {
  485.         Tcl_AppendResult(interp, "unknown", (char *) NULL);
  486.     }
  487.     if (info.deleteProc == CmdDelProc1) {
  488.         Tcl_AppendResult(interp, " CmdDelProc1", " ",
  489.             (char *) info.deleteData, (char *) NULL);
  490.     } else if (info.deleteProc == CmdDelProc2) {
  491.         Tcl_AppendResult(interp, " CmdDelProc2", " ",
  492.             (char *) info.deleteData, (char *) NULL);
  493.     } else {
  494.         Tcl_AppendResult(interp, " unknown", (char *) NULL);
  495.     }
  496.     } else if (strcmp(argv[1], "modify") == 0) {
  497.     info.proc = CmdProc2;
  498.     info.clientData = (ClientData) "new_command_data";
  499.     info.deleteProc = CmdDelProc2;
  500.     info.deleteData = (ClientData) "new_delete_data";
  501.     if (Tcl_SetCommandInfo(interp, argv[2], &info) == 0) {
  502.         interp->result = "0";
  503.     } else {
  504.         interp->result = "1";
  505.     }
  506.     } else {
  507.     Tcl_AppendResult(interp, "bad option \"", argv[1],
  508.         "\": must be create, delete, get, or modify",
  509.         (char *) NULL);
  510.     return TCL_ERROR;
  511.     }
  512.     return TCL_OK;
  513. }
  514.  
  515.     /*ARGSUSED*/
  516. static int
  517. CmdProc1(clientData, interp, argc, argv)
  518.     ClientData clientData;        /* String to return. */
  519.     Tcl_Interp *interp;            /* Current interpreter. */
  520.     int argc;                /* Number of arguments. */
  521.     char **argv;            /* Argument strings. */
  522. {
  523.     Tcl_AppendResult(interp, "CmdProc1 ", (char *) clientData,
  524.         (char *) NULL);
  525.     return TCL_OK;
  526. }
  527.  
  528.     /*ARGSUSED*/
  529. static int
  530. CmdProc2(clientData, interp, argc, argv)
  531.     ClientData clientData;        /* String to return. */
  532.     Tcl_Interp *interp;            /* Current interpreter. */
  533.     int argc;                /* Number of arguments. */
  534.     char **argv;            /* Argument strings. */
  535. {
  536.     Tcl_AppendResult(interp, "CmdProc2 ", (char *) clientData,
  537.         (char *) NULL);
  538.     return TCL_OK;
  539. }
  540.  
  541. static void
  542. CmdDelProc1(clientData)
  543.     ClientData clientData;        /* String to save. */
  544. {
  545.     Tcl_DStringInit(&delString);
  546.     Tcl_DStringAppend(&delString, "CmdDelProc1 ", -1);
  547.     Tcl_DStringAppend(&delString, (char *) clientData, -1);
  548. }
  549.  
  550. static void
  551. CmdDelProc2(clientData)
  552.     ClientData clientData;        /* String to save. */
  553. {
  554.     Tcl_DStringInit(&delString);
  555.     Tcl_DStringAppend(&delString, "CmdDelProc2 ", -1);
  556.     Tcl_DStringAppend(&delString, (char *) clientData, -1);
  557. }
  558.  
  559. /*
  560.  *----------------------------------------------------------------------
  561.  *
  562.  * TestcmdtokenCmd --
  563.  *
  564.  *    This procedure implements the "testcmdtoken" command.  It is used
  565.  *    to test Tcl_Command tokens and Tcl_GetCommandName.
  566.  *
  567.  * Results:
  568.  *    A standard Tcl result.
  569.  *
  570.  * Side effects:
  571.  *    Creates and deletes various commands and modifies their data.
  572.  *
  573.  *----------------------------------------------------------------------
  574.  */
  575.  
  576.     /* ARGSUSED */
  577. static int
  578. TestcmdtokenCmd(dummy, interp, argc, argv)
  579.     ClientData dummy;            /* Not used. */
  580.     Tcl_Interp *interp;            /* Current interpreter. */
  581.     int argc;                /* Number of arguments. */
  582.     char **argv;            /* Argument strings. */
  583. {
  584.     Tcl_Command token;
  585.     long int l;
  586.  
  587.     if (argc != 3) {
  588.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  589.         " option arg\"", (char *) NULL);
  590.     return TCL_ERROR;
  591.     }
  592.     if (strcmp(argv[1], "create") == 0) {
  593.     token = Tcl_CreateCommand(interp, argv[2], CmdProc1,
  594.         (ClientData) "original", (Tcl_CmdDeleteProc *) NULL);
  595.     sprintf(interp->result, "%lx", (long int) token);
  596.     } else if (strcmp(argv[1], "name") == 0) {
  597.     if (sscanf(argv[2], "%lx", &l) != 1) {
  598.         Tcl_AppendResult(interp, "bad command token \"", argv[2],
  599.             "\"", (char *) NULL);
  600.         return TCL_ERROR;
  601.     }
  602.     interp->result = Tcl_GetCommandName(interp, (Tcl_Command) l);
  603.     } else {
  604.     Tcl_AppendResult(interp, "bad option \"", argv[1],
  605.         "\": must be create or name", (char *) NULL);
  606.     return TCL_ERROR;
  607.     }
  608.     return TCL_OK;
  609. }
  610.  
  611. /*
  612.  *----------------------------------------------------------------------
  613.  *
  614.  * TestdstringCmd --
  615.  *
  616.  *    This procedure implements the "testdstring" command.  It is used
  617.  *    to test the dynamic string facilities of Tcl.
  618.  *
  619.  * Results:
  620.  *    A standard Tcl result.
  621.  *
  622.  * Side effects:
  623.  *    Creates, deletes, and invokes handlers.
  624.  *
  625.  *----------------------------------------------------------------------
  626.  */
  627.  
  628.     /* ARGSUSED */
  629. static int
  630. TestdstringCmd(dummy, interp, argc, argv)
  631.     ClientData dummy;            /* Not used. */
  632.     Tcl_Interp *interp;            /* Current interpreter. */
  633.     int argc;                /* Number of arguments. */
  634.     char **argv;            /* Argument strings. */
  635. {
  636.     int count;
  637.  
  638.     if (argc < 2) {
  639.     wrongNumArgs:
  640.     interp->result = "wrong # args";
  641.     return TCL_ERROR;
  642.     }
  643.     if (strcmp(argv[1], "append") == 0) {
  644.     if (argc != 4) {
  645.         goto wrongNumArgs;
  646.     }
  647.     if (Tcl_GetInt(interp, argv[3], &count) != TCL_OK) {
  648.         return TCL_ERROR;
  649.     }
  650.     Tcl_DStringAppend(&dstring, argv[2], count);
  651.     } else if (strcmp(argv[1], "element") == 0) {
  652.     if (argc != 3) {
  653.         goto wrongNumArgs;
  654.     }
  655.     Tcl_DStringAppendElement(&dstring, argv[2]);
  656.     } else if (strcmp(argv[1], "end") == 0) {
  657.     if (argc != 2) {
  658.         goto wrongNumArgs;
  659.     }
  660.     Tcl_DStringEndSublist(&dstring);
  661.     } else if (strcmp(argv[1], "free") == 0) {
  662.     if (argc != 2) {
  663.         goto wrongNumArgs;
  664.     }
  665.     Tcl_DStringFree(&dstring);
  666.     } else if (strcmp(argv[1], "get") == 0) {
  667.     if (argc != 2) {
  668.         goto wrongNumArgs;
  669.     }
  670.     interp->result = Tcl_DStringValue(&dstring);
  671.     } else if (strcmp(argv[1], "gresult") == 0) {
  672.     if (argc != 3) {
  673.         goto wrongNumArgs;
  674.     }
  675.     if (strcmp(argv[2], "staticsmall") == 0) {
  676.         interp->result = "short";
  677.     } else if (strcmp(argv[2], "staticlarge") == 0) {
  678.         interp->result = "first0 first1 first2 first3 first4 first5 first6 first7 first8 first9\nsecond0 second1 second2 second3 second4 second5 second6 second7 second8 second9\nthird0 third1 third2 third3 third4 third5 third6 third7 third8 third9\nfourth0 fourth1 fourth2 fourth3 fourth4 fourth5 fourth6 fourth7 fourth8 fourth9\nfifth0 fifth1 fifth2 fifth3 fifth4 fifth5 fifth6 fifth7 fifth8 fifth9\nsixth0 sixth1 sixth2 sixth3 sixth4 sixth5 sixth6 sixth7 sixth8 sixth9\nseventh0 seventh1 seventh2 seventh3 seventh4 seventh5 seventh6 seventh7 seventh8 seventh9\n";
  679.     } else if (strcmp(argv[2], "free") == 0) {
  680.         interp->result = ckalloc(100);
  681.         interp->freeProc = (Tcl_FreeProc *) free;
  682.         strcpy(interp->result, "This is a malloc-ed string");
  683.     } else if (strcmp(argv[2], "special") == 0) {
  684.         interp->result = ckalloc(100);
  685.         interp->result += 4;
  686.         interp->freeProc = SpecialFree;
  687.         strcpy(interp->result, "This is a specially-allocated string");
  688.     } else {
  689.         Tcl_AppendResult(interp, "bad gresult option \"", argv[2],
  690.             "\": must be staticsmall, staticlarge, free, or special",
  691.             (char *) NULL);
  692.         return TCL_ERROR;
  693.     }
  694.     Tcl_DStringGetResult(interp, &dstring);
  695.     } else if (strcmp(argv[1], "length") == 0) {
  696.     if (argc != 2) {
  697.         goto wrongNumArgs;
  698.     }
  699.     sprintf(interp->result, "%d", Tcl_DStringLength(&dstring));
  700.     } else if (strcmp(argv[1], "result") == 0) {
  701.     if (argc != 2) {
  702.         goto wrongNumArgs;
  703.     }
  704.     Tcl_DStringResult(interp, &dstring);
  705.     } else if (strcmp(argv[1], "trunc") == 0) {
  706.     if (argc != 3) {
  707.         goto wrongNumArgs;
  708.     }
  709.     if (Tcl_GetInt(interp, argv[2], &count) != TCL_OK) {
  710.         return TCL_ERROR;
  711.     }
  712.     Tcl_DStringTrunc(&dstring, count);
  713.     } else if (strcmp(argv[1], "start") == 0) {
  714.     if (argc != 2) {
  715.         goto wrongNumArgs;
  716.     }
  717.     Tcl_DStringStartSublist(&dstring);
  718.     } else {
  719.     Tcl_AppendResult(interp, "bad option \"", argv[1],
  720.         "\": must be append, element, end, free, get, length, ",
  721.         "result, trunc, or start", (char *) NULL);
  722.     return TCL_ERROR;
  723.     }
  724.     return TCL_OK;
  725. }
  726.  
  727. /*
  728.  * The procedure below is used as a special freeProc to test how well
  729.  * Tcl_DStringGetResult handles freeProc's other than free.
  730.  */
  731.  
  732. static void SpecialFree(blockPtr)
  733.     char *blockPtr;            /* Block to free. */
  734. {
  735.     ckfree(blockPtr - 4);
  736. }
  737.  
  738. /*
  739.  *----------------------------------------------------------------------
  740.  *
  741.  * TestlinkCmd --
  742.  *
  743.  *    This procedure implements the "testlink" command.  It is used
  744.  *    to test Tcl_LinkVar and related library procedures.
  745.  *
  746.  * Results:
  747.  *    A standard Tcl result.
  748.  *
  749.  * Side effects:
  750.  *    Creates and deletes various variable links, plus returns
  751.  *    values of the linked variables.
  752.  *
  753.  *----------------------------------------------------------------------
  754.  */
  755.  
  756.     /* ARGSUSED */
  757. static int
  758. TestlinkCmd(dummy, interp, argc, argv)
  759.     ClientData dummy;            /* Not used. */
  760.     Tcl_Interp *interp;            /* Current interpreter. */
  761.     int argc;                /* Number of arguments. */
  762.     char **argv;            /* Argument strings. */
  763. {
  764.     static int intVar = 43;
  765.     static int boolVar = 4;
  766.     static double realVar = 1.23;
  767.     static char *stringVar = NULL;
  768.     char buffer[TCL_DOUBLE_SPACE];
  769.     int writable, flag;
  770.  
  771.     if (argc < 2) {
  772.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  773.         " option ?arg arg arg?\"", (char *) NULL);
  774.     return TCL_ERROR;
  775.     }
  776.     if (strcmp(argv[1], "create") == 0) {
  777.     if (Tcl_GetBoolean(interp, argv[2], &writable) != TCL_OK) {
  778.         return TCL_ERROR;
  779.     }
  780.     flag = (writable != 0) ? 0 : TCL_LINK_READ_ONLY;
  781.     if (Tcl_LinkVar(interp, "int", (char *) &intVar,
  782.         TCL_LINK_INT | flag) != TCL_OK) {
  783.         return TCL_ERROR;
  784.     }
  785.     if (Tcl_GetBoolean(interp, argv[3], &writable) != TCL_OK) {
  786.         return TCL_ERROR;
  787.     }
  788.     flag = (writable != 0) ? 0 : TCL_LINK_READ_ONLY;
  789.     if (Tcl_LinkVar(interp, "real", (char *) &realVar,
  790.         TCL_LINK_DOUBLE | flag) != TCL_OK) {
  791.         return TCL_ERROR;
  792.     }
  793.     if (Tcl_GetBoolean(interp, argv[4], &writable) != TCL_OK) {
  794.         return TCL_ERROR;
  795.     }
  796.     flag = (writable != 0) ? 0 : TCL_LINK_READ_ONLY;
  797.     if (Tcl_LinkVar(interp, "bool", (char *) &boolVar,
  798.         TCL_LINK_BOOLEAN | flag) != TCL_OK) {
  799.         return TCL_ERROR;
  800.     }
  801.     if (Tcl_GetBoolean(interp, argv[5], &writable) != TCL_OK) {
  802.         return TCL_ERROR;
  803.     }
  804.     flag = (writable != 0) ? 0 : TCL_LINK_READ_ONLY;
  805.     if (Tcl_LinkVar(interp, "string", (char *) &stringVar,
  806.         TCL_LINK_STRING | flag) != TCL_OK) {
  807.         return TCL_ERROR;
  808.     }
  809.     } else if (strcmp(argv[1], "delete") == 0) {
  810.     Tcl_UnlinkVar(interp, "int");
  811.     Tcl_UnlinkVar(interp, "real");
  812.     Tcl_UnlinkVar(interp, "bool");
  813.     Tcl_UnlinkVar(interp, "string");
  814.     } else if (strcmp(argv[1], "get") == 0) {
  815.     sprintf(buffer, "%d", intVar);
  816.     Tcl_AppendElement(interp, buffer);
  817.     Tcl_PrintDouble(interp, realVar, buffer);
  818.     Tcl_AppendElement(interp, buffer);
  819.     sprintf(buffer, "%d", boolVar);
  820.     Tcl_AppendElement(interp, buffer);
  821.     Tcl_AppendElement(interp, (stringVar == NULL) ? "-" : stringVar);
  822.     } else if (strcmp(argv[1], "set") == 0) {
  823.     if (argc != 6) {
  824.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  825.         argv[0], " ", argv[1],
  826.         "intValue realValue boolValue stringValue\"", (char *) NULL);
  827.         return TCL_ERROR;
  828.     }
  829.     if (argv[2][0] != 0) {
  830.         if (Tcl_GetInt(interp, argv[2], &intVar) != TCL_OK) {
  831.         return TCL_ERROR;
  832.         }
  833.     }
  834.     if (argv[3][0] != 0) {
  835.         if (Tcl_GetDouble(interp, argv[3], &realVar) != TCL_OK) {
  836.         return TCL_ERROR;
  837.         }
  838.     }
  839.     if (argv[4][0] != 0) {
  840.         if (Tcl_GetInt(interp, argv[4], &boolVar) != TCL_OK) {
  841.         return TCL_ERROR;
  842.         }
  843.     }
  844.     if (argv[5][0] != 0) {
  845.         if (stringVar != NULL) {
  846.         ckfree(stringVar);
  847.         }
  848.         if (strcmp(argv[5], "-") == 0) {
  849.         stringVar = NULL;
  850.         } else {
  851.         stringVar = ckalloc((unsigned) (strlen(argv[5]) + 1));
  852.         strcpy(stringVar, argv[5]);
  853.         }
  854.     }
  855.     } else {
  856.     Tcl_AppendResult(interp, "bad option \"", argv[1],
  857.         "\": should be create, delete, get, or set",
  858.         (char *) NULL);
  859.     return TCL_ERROR;
  860.     }
  861.     return TCL_OK;
  862. }
  863.  
  864. /*
  865.  *----------------------------------------------------------------------
  866.  *
  867.  * TestMathFunc --
  868.  *
  869.  *    This is a user-defined math procedure to test out math procedures
  870.  *    with no arguments.
  871.  *
  872.  * Results:
  873.  *    A normal Tcl completion code.
  874.  *
  875.  * Side effects:
  876.  *    None.
  877.  *
  878.  *----------------------------------------------------------------------
  879.  */
  880.  
  881.     /* ARGSUSED */
  882. static int
  883. TestMathFunc(clientData, interp, args, resultPtr)
  884.     ClientData clientData;        /* Integer value to return. */
  885.     Tcl_Interp *interp;            /* Not used. */
  886.     Tcl_Value *args;            /* Not used. */
  887.     Tcl_Value *resultPtr;        /* Where to store result. */
  888. {
  889.     resultPtr->type = TCL_INT;
  890.     resultPtr->intValue = (int) clientData;
  891.     return TCL_OK;
  892. }
  893.  
  894. /*
  895.  *----------------------------------------------------------------------
  896.  *
  897.  * TestupvarCmd --
  898.  *
  899.  *    This procedure implements the "testupvar2" command.  It is used
  900.  *    to test Tcl_UpVar and Tcl_UpVar2.
  901.  *
  902.  * Results:
  903.  *    A standard Tcl result.
  904.  *
  905.  * Side effects:
  906.  *    Creates or modifies an "upvar" reference.
  907.  *
  908.  *----------------------------------------------------------------------
  909.  */
  910.  
  911.     /* ARGSUSED */
  912. static int
  913. TestupvarCmd(dummy, interp, argc, argv)
  914.     ClientData dummy;            /* Not used. */
  915.     Tcl_Interp *interp;            /* Current interpreter. */
  916.     int argc;                /* Number of arguments. */
  917.     char **argv;            /* Argument strings. */
  918. {
  919.     if ((argc != 5) && (argc != 6)) {
  920.     Tcl_AppendResult(interp, "wrong # arguments: should be \"",
  921.         argv[0], " level name ?name2? dest global\"", (char *) NULL);
  922.     return TCL_ERROR;
  923.     }
  924.  
  925.     if (argc == 5) {
  926.     return Tcl_UpVar(interp, argv[1], argv[2], argv[3],
  927.         (strcmp(argv[4], "global") == 0) ? TCL_GLOBAL_ONLY : 0);
  928.     } else {
  929.     return Tcl_UpVar2(interp, argv[1], argv[2], 
  930.         (argv[3][0] == 0) ? (char *) NULL : argv[3], argv[4],
  931.         (strcmp(argv[5], "global") == 0) ? TCL_GLOBAL_ONLY : 0);
  932.     }
  933. }
  934.