home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / tcl / tcl7.0b1 / tclVar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-01  |  65.2 KB  |  2,360 lines

  1. /* 
  2.  * tclVar.c --
  3.  *
  4.  *    This file contains routines that implement Tcl variables
  5.  *    (both scalars and arrays).
  6.  *
  7.  *    The implementation of arrays is modelled after an initial
  8.  *    implementation by Mark Diekhans and Karl Lehenbauer.
  9.  *
  10.  * Copyright (c) 1987-1993 The Regents of the University of California.
  11.  * All rights reserved.
  12.  *
  13.  * Permission is hereby granted, without written agreement and without
  14.  * license or royalty fees, to use, copy, modify, and distribute this
  15.  * software and its documentation for any purpose, provided that the
  16.  * above copyright notice and the following two paragraphs appear in
  17.  * all copies of this software.
  18.  * 
  19.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  20.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  21.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  22.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23.  *
  24.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  25.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  26.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  27.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  28.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  29.  */
  30.  
  31. #ifndef lint
  32. static char rcsid[] = "$Header: /user6/ouster/tcl/RCS/tclVar.c,v 1.40 93/07/01 15:25:58 ouster Exp $ SPRITE (Berkeley)";
  33. #endif
  34.  
  35. #include "tclInt.h"
  36.  
  37. /*
  38.  * The strings below are used to indicate what went wrong when a
  39.  * variable access is denied.
  40.  */
  41.  
  42. static char *noSuchVar =    "no such variable";
  43. static char *isArray =        "variable is array";
  44. static char *needArray =    "variable isn't array";
  45. static char *noSuchElement =    "no such element in array";
  46. static char *danglingUpvar =    "upvar refers to element in deleted array";
  47.  
  48. /*
  49.  * Creation flag values passed in to LookupVar:
  50.  *
  51.  * CRT_PART1 -        1 means create hash table entry for part 1 of
  52.  *            name, if it doesn't already exist.  0 means
  53.  *            return an error if it doesn't exist.
  54.  * CRT_PART2 -        1 means create hash table entry for part 2 of
  55.  *            name, if it doesn't already exist.  0 means
  56.  *            return an error if it doesn't exist.
  57.  */
  58.  
  59. #define CRT_PART1    1
  60. #define CRT_PART2    2
  61.  
  62. /*
  63.  * Forward references to procedures defined later in this file:
  64.  */
  65.  
  66. static  char *        CallTraces _ANSI_ARGS_((Interp *iPtr, Var *arrayPtr,
  67.                 Var *varPtr, char *part1, char *part2,
  68.                 int flags));
  69. static void        CleanupVar _ANSI_ARGS_((Var *varPtr, Var *arrayPtr));
  70. static void        DeleteSearches _ANSI_ARGS_((Var *arrayVarPtr));
  71. static void        DeleteArray _ANSI_ARGS_((Interp *iPtr, char *arrayName,
  72.                 Var *varPtr, int flags));
  73. static Var *        LookupVar _ANSI_ARGS_((Tcl_Interp *interp, char *part1,
  74.                 char *part2, int flags, char *msg, int create,
  75.                 Var **arrayPtrPtr));
  76. static int        MakeUpvar _ANSI_ARGS_((Interp *iPtr,
  77.                 CallFrame *framePtr, char *otherP1,
  78.                 char *otherP2, char *myName));
  79. static Var *        NewVar _ANSI_ARGS_((void));
  80. static ArraySearch *    ParseSearchId _ANSI_ARGS_((Tcl_Interp *interp,
  81.                 Var *varPtr, char *varName, char *string));
  82. static void        VarErrMsg _ANSI_ARGS_((Tcl_Interp *interp,
  83.                 char *part1, char *part2, char *operation,
  84.                 char *reason));
  85.  
  86. /*
  87.  *----------------------------------------------------------------------
  88.  *
  89.  * LookupVar --
  90.  *
  91.  *    This procedure is used by virtually all of the variable
  92.  *    code to locate a variable given its name(s).
  93.  *
  94.  * Results:
  95.  *    The return value is a pointer to the variable indicated by
  96.  *    part1 and part2, or NULL if the variable couldn't be found.
  97.  *    If the variable is found, *arrayPtrPtr is filled in with
  98.  *    the address of the array that contains the variable (or NULL
  99.  *    if the variable is a scalar).  Note:  it's possible that the
  100.  *    variable returned may be VAR_UNDEFINED, even if CRT_PART1 and
  101.  *    CRT_PART2 are specified (these only cause the hash table entry
  102.  *    and/or array to be created).
  103.  *
  104.  * Side effects:
  105.  *    None.
  106.  *
  107.  *----------------------------------------------------------------------
  108.  */
  109.  
  110. static Var *
  111. LookupVar(interp, part1, part2, flags, msg, create, arrayPtrPtr)
  112.     Tcl_Interp *interp;        /* Interpreter to use for lookup. */
  113.     char *part1;        /* If part2 is NULL, this is name of scalar
  114.                  * variable.  Otherwise it is name of array. */
  115.     char *part2;        /* Name of an element within array, or NULL. */
  116.     int flags;            /* Only the TCL_GLOBAL_ONLY and
  117.                  * TCL_LEAVE_ERR_MSG bits matter. */
  118.     char *msg;            /* Verb to use in error messages, e.g.
  119.                  * "read" or "set".  Only needed if
  120.                  * TCL_LEAVE_ERR_MSG is set in flags. */
  121.     int create;            /* OR'ed combination of CRT_PART1 and
  122.                  * CRT_PART2.  Tells which entries to create
  123.                  * if they don't already exist. */
  124.     Var **arrayPtrPtr;        /* If part2 is non-NULL, *arrayPtrPtr gets
  125.                  * filled in with address of array variable. */
  126. {
  127.     Interp *iPtr = (Interp *) interp;
  128.     Tcl_HashTable *tablePtr;
  129.     Tcl_HashEntry *hPtr;
  130.     Var *varPtr;
  131.     int new;
  132.  
  133.     /*
  134.      * Lookup part1.
  135.      */
  136.  
  137.     *arrayPtrPtr = NULL;
  138.     if ((flags & TCL_GLOBAL_ONLY) || (iPtr->varFramePtr == NULL)) {
  139.     tablePtr = &iPtr->globalTable;
  140.     } else {
  141.     tablePtr = &iPtr->varFramePtr->varTable;
  142.     }
  143.     if (create & CRT_PART1) {
  144.     hPtr = Tcl_CreateHashEntry(tablePtr, part1, &new);
  145.     if (new) {
  146.         varPtr = NewVar();
  147.         Tcl_SetHashValue(hPtr, varPtr);
  148.         varPtr->hPtr = hPtr;
  149.     }
  150.     } else {
  151.     hPtr = Tcl_FindHashEntry(tablePtr, part1);
  152.     if (hPtr == NULL) {
  153.         if (flags & TCL_LEAVE_ERR_MSG) {
  154.         VarErrMsg(interp, part1, part2, msg, noSuchVar);
  155.         }
  156.         return NULL;
  157.     }
  158.     }
  159.     varPtr = (Var *) Tcl_GetHashValue(hPtr);
  160.     if (varPtr->flags & VAR_UPVAR) {
  161.     varPtr = varPtr->value.upvarPtr;
  162.     }
  163.  
  164.     if (part2 == NULL) {
  165.     return varPtr;
  166.     }
  167.  
  168.     /*
  169.      * We're dealing with an array element, so make sure the variable
  170.      * is an array and lookup the element (create it if desired).
  171.      */
  172.  
  173.     if (varPtr->flags & VAR_UNDEFINED) {
  174.     if (!(create & CRT_PART1)) {
  175.         if (flags & TCL_LEAVE_ERR_MSG) {
  176.         VarErrMsg(interp, part1, part2, msg, noSuchVar);
  177.         }
  178.         return NULL;
  179.     }
  180.     varPtr->flags = VAR_ARRAY;
  181.     varPtr->value.tablePtr = (Tcl_HashTable *)
  182.         ckalloc(sizeof(Tcl_HashTable));
  183.     Tcl_InitHashTable(varPtr->value.tablePtr, TCL_STRING_KEYS);
  184.     } else if (!(varPtr->flags & VAR_ARRAY)) {
  185.     if (flags & TCL_LEAVE_ERR_MSG) {
  186.         VarErrMsg(interp, part1, part2, msg, needArray);
  187.     }
  188.     return NULL;
  189.     }
  190.     *arrayPtrPtr = varPtr;
  191.     if (create & CRT_PART2) {
  192.     hPtr = Tcl_CreateHashEntry(varPtr->value.tablePtr, part2, &new);
  193.     if (new) {
  194.         if (varPtr->searchPtr != NULL) {
  195.         DeleteSearches(varPtr);
  196.         }
  197.         varPtr = NewVar();
  198.         Tcl_SetHashValue(hPtr, varPtr);
  199.         varPtr->hPtr = hPtr;
  200.     }
  201.     } else {
  202.     hPtr = Tcl_FindHashEntry(varPtr->value.tablePtr, part2);
  203.     if (hPtr == NULL) {
  204.         if (flags & TCL_LEAVE_ERR_MSG) {
  205.         VarErrMsg(interp, part1, part2, msg, noSuchElement);
  206.         }
  207.         return NULL;
  208.     }
  209.     }
  210.     return (Var *) Tcl_GetHashValue(hPtr);
  211. }
  212.  
  213. /*
  214.  *----------------------------------------------------------------------
  215.  *
  216.  * Tcl_GetVar --
  217.  *
  218.  *    Return the value of a Tcl variable.
  219.  *
  220.  * Results:
  221.  *    The return value points to the current value of varName.  If
  222.  *    the variable is not defined or can't be read because of a clash
  223.  *    in array usage then a NULL pointer is returned and an error
  224.  *    message is left in interp->result if the TCL_LEAVE_ERR_MSG
  225.  *    flag is set.  Note:  the return value is only valid up until
  226.  *    the next call to Tcl_SetVar or Tcl_SetVar2;  if you depend on
  227.  *    the value lasting longer than that, then make yourself a private
  228.  *    copy.
  229.  *
  230.  * Side effects:
  231.  *    None.
  232.  *
  233.  *----------------------------------------------------------------------
  234.  */
  235.  
  236. char *
  237. Tcl_GetVar(interp, varName, flags)
  238.     Tcl_Interp *interp;        /* Command interpreter in which varName is
  239.                  * to be looked up. */
  240.     char *varName;        /* Name of a variable in interp. */
  241.     int flags;            /* OR-ed combination of TCL_GLOBAL_ONLY
  242.                  * or TCL_LEAVE_ERR_MSG bits. */
  243. {
  244.     register char *p;
  245.  
  246.     /*
  247.      * If varName refers to an array (it ends with a parenthesized
  248.      * element name), then handle it specially.
  249.      */
  250.  
  251.     for (p = varName; *p != '\0'; p++) {
  252.     if (*p == '(') {
  253.         char *result;
  254.         char *open = p;
  255.  
  256.         do {
  257.         p++;
  258.         } while (*p != '\0');
  259.         p--;
  260.         if (*p != ')') {
  261.         goto scalar;
  262.         }
  263.         *open = '\0';
  264.         *p = '\0';
  265.         result = Tcl_GetVar2(interp, varName, open+1, flags);
  266.         *open = '(';
  267.         *p = ')';
  268.         return result;
  269.     }
  270.     }
  271.  
  272.     scalar:
  273.     return Tcl_GetVar2(interp, varName, (char *) NULL, flags);
  274. }
  275.  
  276. /*
  277.  *----------------------------------------------------------------------
  278.  *
  279.  * Tcl_GetVar2 --
  280.  *
  281.  *    Return the value of a Tcl variable, given a two-part name
  282.  *    consisting of array name and element within array.
  283.  *
  284.  * Results:
  285.  *    The return value points to the current value of the variable
  286.  *    given by part1 and part2.  If the specified variable doesn't
  287.  *    exist, or if there is a clash in array usage, then NULL is
  288.  *    returned and a message will be left in interp->result if the
  289.  *    TCL_LEAVE_ERR_MSG flag is set.  Note:  the return value is
  290.  *    only valid up until the next call to Tcl_SetVar or Tcl_SetVar2;
  291.  *    if you depend on the value lasting longer than that, then make
  292.  *    yourself a private copy.
  293.  *
  294.  * Side effects:
  295.  *    None.
  296.  *
  297.  *----------------------------------------------------------------------
  298.  */
  299.  
  300. char *
  301. Tcl_GetVar2(interp, part1, part2, flags)
  302.     Tcl_Interp *interp;        /* Command interpreter in which variable is
  303.                  * to be looked up. */
  304.     char *part1;        /* Name of array (if part2 is NULL) or
  305.                  * name of variable. */
  306.     char *part2;        /* If non-null, gives name of element in
  307.                  * array. */
  308.     int flags;            /* OR-ed combination of TCL_GLOBAL_ONLY
  309.                  * or TCL_LEAVE_ERR_MSG bits. */
  310. {
  311.     Var *varPtr, *arrayPtr;
  312.     Interp *iPtr = (Interp *) interp;
  313.  
  314.     varPtr = LookupVar(interp, part1, part2, flags, "read", CRT_PART2,
  315.         &arrayPtr);
  316.     if (varPtr == NULL) {
  317.     return NULL;
  318.     }
  319.  
  320.     /*
  321.      * Invoke any traces that have been set for the variable.
  322.      */
  323.  
  324.     if ((varPtr->tracePtr != NULL)
  325.         || ((arrayPtr != NULL) && (arrayPtr->tracePtr != NULL))) {
  326.     char *msg;
  327.  
  328.     msg = CallTraces(iPtr, arrayPtr, varPtr, part1, part2,
  329.         (flags & TCL_GLOBAL_ONLY) | TCL_TRACE_READS);
  330.     if (msg != NULL) {
  331.         VarErrMsg(interp, part1, part2, "read", msg);
  332.         goto cleanup;
  333.     }
  334.     }
  335.     if (!(varPtr->flags & (VAR_UNDEFINED|VAR_UPVAR|VAR_ARRAY))) {
  336.     return varPtr->value.string;
  337.     }
  338.     if (flags & TCL_LEAVE_ERR_MSG) {
  339.     char *msg;
  340.  
  341.     if ((varPtr->flags & VAR_UNDEFINED) && (arrayPtr != NULL)
  342.         && !(arrayPtr->flags & VAR_UNDEFINED)) {
  343.         msg = noSuchElement;
  344.     } else {
  345.         msg = noSuchVar;
  346.     }
  347.     VarErrMsg(interp, part1, part2, "read", msg);
  348.     }
  349.  
  350.     /*
  351.      * If the variable doesn't exist anymore and no-one's using it,
  352.      * then free up the relevant structures and hash table entries.
  353.      */
  354.  
  355.     cleanup:
  356.     if (varPtr->flags & VAR_UNDEFINED) {
  357.     CleanupVar(varPtr, arrayPtr);
  358.     }
  359.     return NULL;
  360. }
  361.  
  362. /*
  363.  *----------------------------------------------------------------------
  364.  *
  365.  * Tcl_SetVar --
  366.  *
  367.  *    Change the value of a variable.
  368.  *
  369.  * Results:
  370.  *    Returns a pointer to the malloc'ed string holding the new
  371.  *    value of the variable.  The caller should not modify this
  372.  *    string.  If the write operation was disallowed then NULL
  373.  *    is returned;  if the TCL_LEAVE_ERR_MSG flag is set, then
  374.  *    an explanatory message will be left in interp->result.
  375.  *
  376.  * Side effects:
  377.  *    If varName is defined as a local or global variable in interp,
  378.  *    its value is changed to newValue.  If varName isn't currently
  379.  *    defined, then a new global variable by that name is created.
  380.  *
  381.  *----------------------------------------------------------------------
  382.  */
  383.  
  384. char *
  385. Tcl_SetVar(interp, varName, newValue, flags)
  386.     Tcl_Interp *interp;        /* Command interpreter in which varName is
  387.                  * to be looked up. */
  388.     char *varName;        /* Name of a variable in interp. */
  389.     char *newValue;        /* New value for varName. */
  390.     int flags;            /* Various flags that tell how to set value:
  391.                  * any of TCL_GLOBAL_ONLY, TCL_APPEND_VALUE,
  392.                  * TCL_LIST_ELEMENT, or TCL_LEAVE_ERR_MSG. */
  393. {
  394.     register char *p;
  395.  
  396.     /*
  397.      * If varName refers to an array (it ends with a parenthesized
  398.      * element name), then handle it specially.
  399.      */
  400.  
  401.     for (p = varName; *p != '\0'; p++) {
  402.     if (*p == '(') {
  403.         char *result;
  404.         char *open = p;
  405.  
  406.         do {
  407.         p++;
  408.         } while (*p != '\0');
  409.         p--;
  410.         if (*p != ')') {
  411.         goto scalar;
  412.         }
  413.         *open = '\0';
  414.         *p = '\0';
  415.         result = Tcl_SetVar2(interp, varName, open+1, newValue, flags);
  416.         *open = '(';
  417.         *p = ')';
  418.         return result;
  419.     }
  420.     }
  421.  
  422.     scalar:
  423.     return Tcl_SetVar2(interp, varName, (char *) NULL, newValue, flags);
  424. }
  425.  
  426. /*
  427.  *----------------------------------------------------------------------
  428.  *
  429.  * Tcl_SetVar2 --
  430.  *
  431.  *    Given a two-part variable name, which may refer either to a
  432.  *    scalar variable or an element of an array, change the value
  433.  *    of the variable.  If the named scalar or array or element
  434.  *    doesn't exist then create one.
  435.  *
  436.  * Results:
  437.  *    Returns a pointer to the malloc'ed string holding the new
  438.  *    value of the variable.  The caller should not modify this
  439.  *    string.  If the write operation was disallowed because an
  440.  *    array was expected but not found (or vice versa), then NULL
  441.  *    is returned;  if the TCL_LEAVE_ERR_MSG flag is set, then
  442.  *    an explanatory message will be left in interp->result.
  443.  *
  444.  * Side effects:
  445.  *    The value of the given variable is set.  If either the array
  446.  *    or the entry didn't exist then a new one is created.
  447.  *
  448.  *----------------------------------------------------------------------
  449.  */
  450.  
  451. char *
  452. Tcl_SetVar2(interp, part1, part2, newValue, flags)
  453.     Tcl_Interp *interp;        /* Command interpreter in which variable is
  454.                  * to be looked up. */
  455.     char *part1;        /* If part2 is NULL, this is name of scalar
  456.                  * variable.  Otherwise it is name of array. */
  457.     char *part2;        /* Name of an element within array, or NULL. */
  458.     char *newValue;        /* New value for variable. */
  459.     int flags;            /* Various flags that tell how to set value:
  460.                  * any of TCL_GLOBAL_ONLY, TCL_APPEND_VALUE,
  461.                  * TCL_LIST_ELEMENT, or TCL_LEAVE_ERR_MSG . */
  462. {
  463.     register Var *varPtr;
  464.     register Interp *iPtr = (Interp *) interp;
  465.     int length, listFlags;
  466.     Var *arrayPtr;
  467.     char *result;
  468.  
  469.     varPtr = LookupVar(interp, part1, part2, flags, "set", CRT_PART1|CRT_PART2,
  470.         &arrayPtr);
  471.     if (varPtr == NULL) {
  472.     return NULL;
  473.     }
  474.  
  475.     /*
  476.      * If the variable's hPtr field is NULL, it means that this is an
  477.      * upvar to an array element where the array was deleted, leaving
  478.      * the element dangling at the end of the upvar.  Generate an error
  479.      * (allowing the variable to be reset would screw up our storage
  480.      * allocation and is meaningless anyway).
  481.      */
  482.  
  483.     if (varPtr->hPtr == NULL) {
  484.     if (flags & TCL_LEAVE_ERR_MSG) {
  485.         VarErrMsg(interp, part1, part2, "set", danglingUpvar);
  486.     }
  487.     return NULL;
  488.     }
  489.  
  490.     /*
  491.      * Compute how many total bytes will be needed for the
  492.      * variable (leave space for a separating space between list
  493.      * elements).
  494.      */
  495.  
  496.     if (flags & TCL_LIST_ELEMENT) {
  497.     length = Tcl_ScanElement(newValue, &listFlags) + 1;
  498.     } else {
  499.     length = strlen(newValue);
  500.     }
  501.  
  502.     /*
  503.      * Clear the variable's current value unless this is an
  504.      * append operation.
  505.      */
  506.  
  507.     if (varPtr->flags & VAR_ARRAY) {
  508.     if (flags & TCL_LEAVE_ERR_MSG) {
  509.         VarErrMsg(interp, part1, part2, "set", isArray);
  510.     }
  511.     return NULL;
  512.     }
  513.     if (!(flags & TCL_APPEND_VALUE) || (varPtr->flags & VAR_UNDEFINED)) {
  514.     varPtr->valueLength = 0;
  515.     }
  516.  
  517.     /*
  518.      * Compute how many total bytes will be needed for the variable's
  519.      * new value (leave space for a separating space between list
  520.      * elements).  Allocate new space for the value if needed.
  521.      */
  522.  
  523.     if (flags & TCL_LIST_ELEMENT) {
  524.     length = Tcl_ScanElement(newValue, &listFlags) + 1;
  525.     } else {
  526.     length = strlen(newValue);
  527.     }
  528.     length += varPtr->valueLength;
  529.     if (length >= varPtr->valueSpace) {
  530.     char *newValue;
  531.     int newSize;
  532.  
  533.     newSize = 2*varPtr->valueSpace;
  534.     if (newSize <= length) {
  535.         newSize = length + 1;
  536.     }
  537.     if (newSize < 24) {
  538.         /*
  539.          * Don't waste time with teensy-tiny variables;  we'll
  540.          * just end up expanding them later.
  541.          */
  542.  
  543.         newSize = 24;
  544.     }
  545.     newValue = ckalloc((unsigned) newSize);
  546.     if (varPtr->valueSpace > 0) {
  547.         strcpy(newValue, varPtr->value.string);
  548.         ckfree(varPtr->value.string);
  549.     }
  550.     varPtr->valueSpace = newSize;
  551.     varPtr->value.string = newValue;
  552.     }
  553.  
  554.     /*
  555.      * Append the new value to the variable, either as a list
  556.      * element or as a string.
  557.      */
  558.  
  559.     if (flags & TCL_LIST_ELEMENT) {
  560.     char *dst = varPtr->value.string + varPtr->valueLength;
  561.  
  562.     if ((varPtr->valueLength > 0) && ((dst[-1] != '{')
  563.         || ((varPtr->valueLength > 1) && (dst[-2] != ' ')))) {
  564.         *dst = ' ';
  565.         dst++;
  566.         varPtr->valueLength++;
  567.     }
  568.     varPtr->valueLength += Tcl_ConvertElement(newValue, dst, listFlags);
  569.     } else {
  570.     strcpy(varPtr->value.string + varPtr->valueLength, newValue);
  571.     varPtr->valueLength = length;
  572.     }
  573.     varPtr->flags &= ~VAR_UNDEFINED;
  574.  
  575.     /*
  576.      * Invoke any write traces for the variable.
  577.      */
  578.  
  579.     if ((varPtr->tracePtr != NULL)
  580.         || ((arrayPtr != NULL) && (arrayPtr->tracePtr != NULL))) {
  581.     char *msg;
  582.  
  583.     msg = CallTraces(iPtr, arrayPtr, varPtr, part1, part2,
  584.         (flags & TCL_GLOBAL_ONLY) | TCL_TRACE_WRITES);
  585.     if (msg != NULL) {
  586.         VarErrMsg(interp, part1, part2, "set", msg);
  587.         result = NULL;
  588.         goto cleanup;
  589.     }
  590.     }
  591.  
  592.     /*
  593.      * If the variable was changed in some gross way by a trace (e.g.
  594.      * it was unset and then recreated as an array) then just return
  595.      * an empty string;  otherwise return the variable's current
  596.      * value.
  597.      */
  598.  
  599.     if (!(varPtr->flags & (VAR_UNDEFINED|VAR_UPVAR|VAR_ARRAY))) {
  600.     return varPtr->value.string;
  601.     }
  602.     result = "";
  603.  
  604.     /*
  605.      * If the variable doesn't exist anymore and no-one's using it,
  606.      * then free up the relevant structures and hash table entries.
  607.      */
  608.  
  609.     cleanup:
  610.     if (varPtr->flags & VAR_UNDEFINED) {
  611.     CleanupVar(varPtr, arrayPtr);
  612.     }
  613.     return result;
  614. }
  615.  
  616. /*
  617.  *----------------------------------------------------------------------
  618.  *
  619.  * Tcl_UnsetVar --
  620.  *
  621.  *    Delete a variable, so that it may not be accessed anymore.
  622.  *
  623.  * Results:
  624.  *    Returns TCL_OK if the variable was successfully deleted, TCL_ERROR
  625.  *    if the variable can't be unset.  In the event of an error,
  626.  *    if the TCL_LEAVE_ERR_MSG flag is set then an error message
  627.  *    is left in interp->result.
  628.  *
  629.  * Side effects:
  630.  *    If varName is defined as a local or global variable in interp,
  631.  *    it is deleted.
  632.  *
  633.  *----------------------------------------------------------------------
  634.  */
  635.  
  636. int
  637. Tcl_UnsetVar(interp, varName, flags)
  638.     Tcl_Interp *interp;        /* Command interpreter in which varName is
  639.                  * to be looked up. */
  640.     char *varName;        /* Name of a variable in interp.  May be
  641.                  * either a scalar name or an array name
  642.                  * or an element in an array. */
  643.     int flags;            /* OR-ed combination of any of
  644.                  * TCL_GLOBAL_ONLY or TCL_LEAVE_ERR_MSG. */
  645. {
  646.     register char *p;
  647.     int result;
  648.  
  649.     /*
  650.      * Figure out whether this is an array reference, then call
  651.      * Tcl_UnsetVar2 to do all the real work.
  652.      */
  653.  
  654.     for (p = varName; *p != '\0'; p++) {
  655.     if (*p == '(') {
  656.         char *open = p;
  657.  
  658.         do {
  659.         p++;
  660.         } while (*p != '\0');
  661.         p--;
  662.         if (*p != ')') {
  663.         goto scalar;
  664.         }
  665.         *open = '\0';
  666.         *p = '\0';
  667.         result = Tcl_UnsetVar2(interp, varName, open+1, flags);
  668.         *open = '(';
  669.         *p = ')';
  670.         return result;
  671.     }
  672.     }
  673.  
  674.     scalar:
  675.     return Tcl_UnsetVar2(interp, varName, (char *) NULL, flags);
  676. }
  677.  
  678. /*
  679.  *----------------------------------------------------------------------
  680.  *
  681.  * Tcl_UnsetVar2 --
  682.  *
  683.  *    Delete a variable, given a 2-part name.
  684.  *
  685.  * Results:
  686.  *    Returns TCL_OK if the variable was successfully deleted, TCL_ERROR
  687.  *    if the variable can't be unset.  In the event of an error,
  688.  *    if the TCL_LEAVE_ERR_MSG flag is set then an error message
  689.  *    is left in interp->result.
  690.  *
  691.  * Side effects:
  692.  *    If part1 and part2 indicate a local or global variable in interp,
  693.  *    it is deleted.  If part1 is an array name and part2 is NULL, then
  694.  *    the whole array is deleted.
  695.  *
  696.  *----------------------------------------------------------------------
  697.  */
  698.  
  699. int
  700. Tcl_UnsetVar2(interp, part1, part2, flags)
  701.     Tcl_Interp *interp;        /* Command interpreter in which varName is
  702.                  * to be looked up. */
  703.     char *part1;        /* Name of variable or array. */
  704.     char *part2;        /* Name of element within array or NULL. */
  705.     int flags;            /* OR-ed combination of any of
  706.                  * TCL_GLOBAL_ONLY or TCL_LEAVE_ERR_MSG. */
  707. {
  708.     Var *varPtr, dummyVar;
  709.     Interp *iPtr = (Interp *) interp;
  710.     Var *arrayPtr;
  711.     ActiveVarTrace *activePtr;
  712.     int result;
  713.  
  714.     varPtr = LookupVar(interp, part1, part2, flags, "unset", 0,  &arrayPtr);
  715.     if (varPtr == NULL) {
  716.     return TCL_ERROR;
  717.     }
  718.     result = (varPtr->flags & VAR_UNDEFINED) ? TCL_ERROR : TCL_OK;
  719.  
  720.     if ((part2 != NULL) && (arrayPtr->searchPtr != NULL)) {
  721.     DeleteSearches(arrayPtr);
  722.     }
  723.  
  724.     /*
  725.      * The code below is tricky, because of the possibility that
  726.      * a trace procedure might try to access a variable being
  727.      * deleted.  To handle this situation gracefully, do things
  728.      * in three steps:
  729.      * 1. Copy the contents of the variable to a dummy variable
  730.      *    structure, and mark the original structure as undefined.
  731.      * 2. Invoke traces and clean up the variable, using the copy.
  732.      * 3. If at the end of this the original variable is still
  733.      *    undefined and has no outstanding references, then delete
  734.      *      it (but it could have gotten recreated by a trace).
  735.      */
  736.  
  737.     dummyVar = *varPtr;
  738.     varPtr->valueSpace = 0;
  739.     varPtr->flags = VAR_UNDEFINED;
  740.     varPtr->tracePtr = NULL;
  741.  
  742.     /*
  743.      * Call trace procedures for the variable being deleted and delete
  744.      * its traces.  Be sure to abort any other traces for the variable
  745.      * that are still pending.  Special tricks:
  746.      * 1. Increment varPtr's refCount around this:  CallTraces will
  747.      *    use dummyVar so it won't increment varPtr's refCount.
  748.      * 2. Turn off the VAR_TRACE_ACTIVE flag in dummyVar: we want to
  749.      *    call unset traces even if other traces are pending.
  750.      */
  751.  
  752.     if ((dummyVar.tracePtr != NULL)
  753.         || ((arrayPtr != NULL) && (arrayPtr->tracePtr != NULL))) {
  754.     varPtr->refCount++;
  755.     dummyVar.flags &= ~VAR_TRACE_ACTIVE;
  756.     (void) CallTraces(iPtr, arrayPtr, &dummyVar, part1, part2,
  757.         (flags & TCL_GLOBAL_ONLY) | TCL_TRACE_UNSETS);
  758.     while (dummyVar.tracePtr != NULL) {
  759.         VarTrace *tracePtr = dummyVar.tracePtr;
  760.         dummyVar.tracePtr = tracePtr->nextPtr;
  761.         ckfree((char *) tracePtr);
  762.     }
  763.     for (activePtr = iPtr->activeTracePtr; activePtr != NULL;
  764.         activePtr = activePtr->nextPtr) {
  765.         if (activePtr->varPtr == varPtr) {
  766.         activePtr->nextTracePtr = NULL;
  767.         }
  768.     }
  769.     varPtr->refCount--;
  770.     }
  771.  
  772.     /*
  773.      * If the variable is an array, delete all of its elements.  This
  774.      * must be done after calling the traces on the array, above (that's
  775.      * the way traces are defined).
  776.      */
  777.  
  778.     if (dummyVar.flags & VAR_ARRAY) {
  779.     DeleteArray(iPtr, part1, &dummyVar,
  780.         (flags & TCL_GLOBAL_ONLY) | TCL_TRACE_UNSETS);
  781.     }
  782.     if (dummyVar.valueSpace > 0) {
  783.     ckfree(dummyVar.value.string);
  784.     }
  785.     if (result == TCL_ERROR) {
  786.     if (flags & TCL_LEAVE_ERR_MSG) {
  787.         VarErrMsg(interp, part1, part2, "unset", 
  788.             (part2 == NULL) ? noSuchVar : noSuchElement);
  789.     }
  790.     }
  791.  
  792.     /*
  793.      * Finally, if the variable is truly not in use then free up its
  794.      * record and remove it from the hash table.
  795.      */
  796.  
  797.     CleanupVar(varPtr, arrayPtr);
  798.     return result;
  799. }
  800.  
  801. /*
  802.  *----------------------------------------------------------------------
  803.  *
  804.  * Tcl_TraceVar --
  805.  *
  806.  *    Arrange for reads and/or writes to a variable to cause a
  807.  *    procedure to be invoked, which can monitor the operations
  808.  *    and/or change their actions.
  809.  *
  810.  * Results:
  811.  *    A standard Tcl return value.
  812.  *
  813.  * Side effects:
  814.  *    A trace is set up on the variable given by varName, such that
  815.  *    future references to the variable will be intermediated by
  816.  *    proc.  See the manual entry for complete details on the calling
  817.  *    sequence for proc.
  818.  *
  819.  *----------------------------------------------------------------------
  820.  */
  821.  
  822. int
  823. Tcl_TraceVar(interp, varName, flags, proc, clientData)
  824.     Tcl_Interp *interp;        /* Interpreter in which variable is
  825.                  * to be traced. */
  826.     char *varName;        /* Name of variable;  may end with "(index)"
  827.                  * to signify an array reference. */
  828.     int flags;            /* OR-ed collection of bits, including any
  829.                  * of TCL_TRACE_READS, TCL_TRACE_WRITES,
  830.                  * TCL_TRACE_UNSETS, and TCL_GLOBAL_ONLY. */
  831.     Tcl_VarTraceProc *proc;    /* Procedure to call when specified ops are
  832.                  * invoked upon varName. */
  833.     ClientData clientData;    /* Arbitrary argument to pass to proc. */
  834. {
  835.     register char *p;
  836.  
  837.     /*
  838.      * If varName refers to an array (it ends with a parenthesized
  839.      * element name), then handle it specially.
  840.      */
  841.  
  842.     for (p = varName; *p != '\0'; p++) {
  843.     if (*p == '(') {
  844.         int result;
  845.         char *open = p;
  846.  
  847.         do {
  848.         p++;
  849.         } while (*p != '\0');
  850.         p--;
  851.         if (*p != ')') {
  852.         goto scalar;
  853.         }
  854.         *open = '\0';
  855.         *p = '\0';
  856.         result = Tcl_TraceVar2(interp, varName, open+1, flags,
  857.             proc, clientData);
  858.         *open = '(';
  859.         *p = ')';
  860.         return result;
  861.     }
  862.     }
  863.  
  864.     scalar:
  865.     return Tcl_TraceVar2(interp, varName, (char *) NULL, flags,
  866.         proc, clientData);
  867. }
  868.  
  869. /*
  870.  *----------------------------------------------------------------------
  871.  *
  872.  * Tcl_TraceVar2 --
  873.  *
  874.  *    Arrange for reads and/or writes to a variable to cause a
  875.  *    procedure to be invoked, which can monitor the operations
  876.  *    and/or change their actions.
  877.  *
  878.  * Results:
  879.  *    A standard Tcl return value.
  880.  *
  881.  * Side effects:
  882.  *    A trace is set up on the variable given by part1 and part2, such
  883.  *    that future references to the variable will be intermediated by
  884.  *    proc.  See the manual entry for complete details on the calling
  885.  *    sequence for proc.
  886.  *
  887.  *----------------------------------------------------------------------
  888.  */
  889.  
  890. int
  891. Tcl_TraceVar2(interp, part1, part2, flags, proc, clientData)
  892.     Tcl_Interp *interp;        /* Interpreter in which variable is
  893.                  * to be traced. */
  894.     char *part1;        /* Name of scalar variable or array. */
  895.     char *part2;        /* Name of element within array;  NULL means
  896.                  * trace applies to scalar variable or array
  897.                  * as-a-whole. */
  898.     int flags;            /* OR-ed collection of bits, including any
  899.                  * of TCL_TRACE_READS, TCL_TRACE_WRITES,
  900.                  * TCL_TRACE_UNSETS, and TCL_GLOBAL_ONLY. */
  901.     Tcl_VarTraceProc *proc;    /* Procedure to call when specified ops are
  902.                  * invoked upon varName. */
  903.     ClientData clientData;    /* Arbitrary argument to pass to proc. */
  904. {
  905.     Var *varPtr, *arrayPtr;
  906.     register VarTrace *tracePtr;
  907.  
  908.     varPtr = LookupVar(interp, part1, part2, (flags | TCL_LEAVE_ERR_MSG),
  909.         "trace", CRT_PART1|CRT_PART2, &arrayPtr);
  910.     if (varPtr == NULL) {
  911.     return TCL_ERROR;
  912.     }
  913.  
  914.     /*
  915.      * Set up trace information.
  916.      */
  917.  
  918.     tracePtr = (VarTrace *) ckalloc(sizeof(VarTrace));
  919.     tracePtr->traceProc = proc;
  920.     tracePtr->clientData = clientData;
  921.     tracePtr->flags = flags &
  922.         (TCL_TRACE_READS|TCL_TRACE_WRITES|TCL_TRACE_UNSETS);
  923.     tracePtr->nextPtr = varPtr->tracePtr;
  924.     varPtr->tracePtr = tracePtr;
  925.     return TCL_OK;
  926. }
  927.  
  928. /*
  929.  *----------------------------------------------------------------------
  930.  *
  931.  * Tcl_UntraceVar --
  932.  *
  933.  *    Remove a previously-created trace for a variable.
  934.  *
  935.  * Results:
  936.  *    None.
  937.  *
  938.  * Side effects:
  939.  *    If there exists a trace for the variable given by varName
  940.  *    with the given flags, proc, and clientData, then that trace
  941.  *    is removed.
  942.  *
  943.  *----------------------------------------------------------------------
  944.  */
  945.  
  946. void
  947. Tcl_UntraceVar(interp, varName, flags, proc, clientData)
  948.     Tcl_Interp *interp;        /* Interpreter containing traced variable. */
  949.     char *varName;        /* Name of variable;  may end with "(index)"
  950.                  * to signify an array reference. */
  951.     int flags;            /* OR-ed collection of bits describing
  952.                  * current trace, including any of
  953.                  * TCL_TRACE_READS, TCL_TRACE_WRITES,
  954.                  * TCL_TRACE_UNSETS, and TCL_GLOBAL_ONLY. */
  955.     Tcl_VarTraceProc *proc;    /* Procedure assocated with trace. */
  956.     ClientData clientData;    /* Arbitrary argument to pass to proc. */
  957. {
  958.     register char *p;
  959.  
  960.     /*
  961.      * If varName refers to an array (it ends with a parenthesized
  962.      * element name), then handle it specially.
  963.      */
  964.  
  965.     for (p = varName; *p != '\0'; p++) {
  966.     if (*p == '(') {
  967.         char *open = p;
  968.  
  969.         do {
  970.         p++;
  971.         } while (*p != '\0');
  972.         p--;
  973.         if (*p != ')') {
  974.         goto scalar;
  975.         }
  976.         *open = '\0';
  977.         *p = '\0';
  978.         Tcl_UntraceVar2(interp, varName, open+1, flags, proc, clientData);
  979.         *open = '(';
  980.         *p = ')';
  981.         return;
  982.     }
  983.     }
  984.  
  985.     scalar:
  986.     Tcl_UntraceVar2(interp, varName, (char *) NULL, flags, proc, clientData);
  987. }
  988.  
  989. /*
  990.  *----------------------------------------------------------------------
  991.  *
  992.  * Tcl_UntraceVar2 --
  993.  *
  994.  *    Remove a previously-created trace for a variable.
  995.  *
  996.  * Results:
  997.  *    None.
  998.  *
  999.  * Side effects:
  1000.  *    If there exists a trace for the variable given by part1
  1001.  *    and part2 with the given flags, proc, and clientData, then
  1002.  *    that trace is removed.
  1003.  *
  1004.  *----------------------------------------------------------------------
  1005.  */
  1006.  
  1007. void
  1008. Tcl_UntraceVar2(interp, part1, part2, flags, proc, clientData)
  1009.     Tcl_Interp *interp;        /* Interpreter containing traced variable. */
  1010.     char *part1;        /* Name of variable or array. */
  1011.     char *part2;        /* Name of element within array;  NULL means
  1012.                  * trace applies to scalar variable or array
  1013.                  * as-a-whole. */
  1014.     int flags;            /* OR-ed collection of bits describing
  1015.                  * current trace, including any of
  1016.                  * TCL_TRACE_READS, TCL_TRACE_WRITES,
  1017.                  * TCL_TRACE_UNSETS, and TCL_GLOBAL_ONLY. */
  1018.     Tcl_VarTraceProc *proc;    /* Procedure assocated with trace. */
  1019.     ClientData clientData;    /* Arbitrary argument to pass to proc. */
  1020. {
  1021.     register VarTrace *tracePtr;
  1022.     VarTrace *prevPtr;
  1023.     Var *varPtr, *arrayPtr;
  1024.     Interp *iPtr = (Interp *) interp;
  1025.     ActiveVarTrace *activePtr;
  1026.  
  1027.     varPtr = LookupVar(interp, part1, part2, flags & TCL_GLOBAL_ONLY,
  1028.         (char *) NULL, 0, &arrayPtr);
  1029.     if (varPtr == NULL) {
  1030.     return;
  1031.     }
  1032.  
  1033.     flags &= (TCL_TRACE_READS | TCL_TRACE_WRITES | TCL_TRACE_UNSETS);
  1034.     for (tracePtr = varPtr->tracePtr, prevPtr = NULL; ;
  1035.         prevPtr = tracePtr, tracePtr = tracePtr->nextPtr) {
  1036.     if (tracePtr == NULL) {
  1037.         return;
  1038.     }
  1039.     if ((tracePtr->traceProc == proc) && (tracePtr->flags == flags)
  1040.         && (tracePtr->clientData == clientData)) {
  1041.         break;
  1042.     }
  1043.     }
  1044.  
  1045.     /*
  1046.      * The code below makes it possible to delete traces while traces
  1047.      * are active:  it makes sure that the deleted trace won't be
  1048.      * processed by CallTraces.
  1049.      */
  1050.  
  1051.     for (activePtr = iPtr->activeTracePtr; activePtr != NULL;
  1052.         activePtr = activePtr->nextPtr) {
  1053.     if (activePtr->nextTracePtr == tracePtr) {
  1054.         activePtr->nextTracePtr = tracePtr->nextPtr;
  1055.     }
  1056.     }
  1057.     if (prevPtr == NULL) {
  1058.     varPtr->tracePtr = tracePtr->nextPtr;
  1059.     } else {
  1060.     prevPtr->nextPtr = tracePtr->nextPtr;
  1061.     }
  1062.     ckfree((char *) tracePtr);
  1063.  
  1064.     /*
  1065.      * If this is the last trace on the variable, and the variable is
  1066.      * unset and unused, then free up the variable.
  1067.      */
  1068.  
  1069.     if (varPtr->flags & VAR_UNDEFINED) {
  1070.     CleanupVar(varPtr, (Var *) NULL);
  1071.     }
  1072. }
  1073.  
  1074. /*
  1075.  *----------------------------------------------------------------------
  1076.  *
  1077.  * Tcl_VarTraceInfo --
  1078.  *
  1079.  *    Return the clientData value associated with a trace on a
  1080.  *    variable.  This procedure can also be used to step through
  1081.  *    all of the traces on a particular variable that have the
  1082.  *    same trace procedure.
  1083.  *
  1084.  * Results:
  1085.  *    The return value is the clientData value associated with
  1086.  *    a trace on the given variable.  Information will only be
  1087.  *    returned for a trace with proc as trace procedure.  If
  1088.  *    the clientData argument is NULL then the first such trace is
  1089.  *    returned;  otherwise, the next relevant one after the one
  1090.  *    given by clientData will be returned.  If the variable
  1091.  *    doesn't exist, or if there are no (more) traces for it,
  1092.  *    then NULL is returned.
  1093.  *
  1094.  * Side effects:
  1095.  *    None.
  1096.  *
  1097.  *----------------------------------------------------------------------
  1098.  */
  1099.  
  1100. ClientData
  1101. Tcl_VarTraceInfo(interp, varName, flags, proc, prevClientData)
  1102.     Tcl_Interp *interp;        /* Interpreter containing variable. */
  1103.     char *varName;        /* Name of variable;  may end with "(index)"
  1104.                  * to signify an array reference. */
  1105.     int flags;            /* 0 or TCL_GLOBAL_ONLY. */
  1106.     Tcl_VarTraceProc *proc;    /* Procedure assocated with trace. */
  1107.     ClientData prevClientData;    /* If non-NULL, gives last value returned
  1108.                  * by this procedure, so this call will
  1109.                  * return the next trace after that one.
  1110.                  * If NULL, this call will return the
  1111.                  * first trace. */
  1112. {
  1113.     register char *p;
  1114.  
  1115.     /*
  1116.      * If varName refers to an array (it ends with a parenthesized
  1117.      * element name), then handle it specially.
  1118.      */
  1119.  
  1120.     for (p = varName; *p != '\0'; p++) {
  1121.     if (*p == '(') {
  1122.         ClientData result;
  1123.         char *open = p;
  1124.  
  1125.         do {
  1126.         p++;
  1127.         } while (*p != '\0');
  1128.         p--;
  1129.         if (*p != ')') {
  1130.         goto scalar;
  1131.         }
  1132.         *open = '\0';
  1133.         *p = '\0';
  1134.         result = Tcl_VarTraceInfo2(interp, varName, open+1, flags, proc,
  1135.         prevClientData);
  1136.         *open = '(';
  1137.         *p = ')';
  1138.         return result;
  1139.     }
  1140.     }
  1141.  
  1142.     scalar:
  1143.     return Tcl_VarTraceInfo2(interp, varName, (char *) NULL, flags, proc,
  1144.         prevClientData);
  1145. }
  1146.  
  1147. /*
  1148.  *----------------------------------------------------------------------
  1149.  *
  1150.  * Tcl_VarTraceInfo2 --
  1151.  *
  1152.  *    Same as Tcl_VarTraceInfo, except takes name in two pieces
  1153.  *    instead of one.
  1154.  *
  1155.  * Results:
  1156.  *    Same as Tcl_VarTraceInfo.
  1157.  *
  1158.  * Side effects:
  1159.  *    None.
  1160.  *
  1161.  *----------------------------------------------------------------------
  1162.  */
  1163.  
  1164. ClientData
  1165. Tcl_VarTraceInfo2(interp, part1, part2, flags, proc, prevClientData)
  1166.     Tcl_Interp *interp;        /* Interpreter containing variable. */
  1167.     char *part1;        /* Name of variable or array. */
  1168.     char *part2;        /* Name of element within array;  NULL means
  1169.                  * trace applies to scalar variable or array
  1170.                  * as-a-whole. */
  1171.     int flags;            /* 0 or TCL_GLOBAL_ONLY. */
  1172.     Tcl_VarTraceProc *proc;    /* Procedure assocated with trace. */
  1173.     ClientData prevClientData;    /* If non-NULL, gives last value returned
  1174.                  * by this procedure, so this call will
  1175.                  * return the next trace after that one.
  1176.                  * If NULL, this call will return the
  1177.                  * first trace. */
  1178. {
  1179.     register VarTrace *tracePtr;
  1180.     Var *varPtr, *arrayPtr;
  1181.  
  1182.     varPtr = LookupVar(interp, part1, part2, flags & TCL_GLOBAL_ONLY,
  1183.         (char *) NULL, 0, &arrayPtr);
  1184.     if (varPtr == NULL) {
  1185.     return NULL;
  1186.     }
  1187.  
  1188.     /*
  1189.      * Find the relevant trace, if any, and return its clientData.
  1190.      */
  1191.  
  1192.     tracePtr = varPtr->tracePtr;
  1193.     if (prevClientData != NULL) {
  1194.     for ( ; tracePtr != NULL; tracePtr = tracePtr->nextPtr) {
  1195.         if ((tracePtr->clientData == prevClientData)
  1196.             && (tracePtr->traceProc == proc)) {
  1197.         tracePtr = tracePtr->nextPtr;
  1198.         break;
  1199.         }
  1200.     }
  1201.     }
  1202.     for ( ; tracePtr != NULL; tracePtr = tracePtr->nextPtr) {
  1203.     if (tracePtr->traceProc == proc) {
  1204.         return tracePtr->clientData;
  1205.     }
  1206.     }
  1207.     return NULL;
  1208. }
  1209.  
  1210. /*
  1211.  *----------------------------------------------------------------------
  1212.  *
  1213.  * Tcl_SetCmd --
  1214.  *
  1215.  *    This procedure is invoked to process the "set" Tcl command.
  1216.  *    See the user documentation for details on what it does.
  1217.  *
  1218.  * Results:
  1219.  *    A standard Tcl result value.
  1220.  *
  1221.  * Side effects:
  1222.  *    A variable's value may be changed.
  1223.  *
  1224.  *----------------------------------------------------------------------
  1225.  */
  1226.  
  1227.     /* ARGSUSED */
  1228. int
  1229. Tcl_SetCmd(dummy, interp, argc, argv)
  1230.     ClientData dummy;            /* Not used. */
  1231.     register Tcl_Interp *interp;    /* Current interpreter. */
  1232.     int argc;                /* Number of arguments. */
  1233.     char **argv;            /* Argument strings. */
  1234. {
  1235.     if (argc == 2) {
  1236.     char *value;
  1237.  
  1238.     value = Tcl_GetVar(interp, argv[1], TCL_LEAVE_ERR_MSG);
  1239.     if (value == NULL) {
  1240.         return TCL_ERROR;
  1241.     }
  1242.     interp->result = value;
  1243.     return TCL_OK;
  1244.     } else if (argc == 3) {
  1245.     char *result;
  1246.  
  1247.     result = Tcl_SetVar(interp, argv[1], argv[2], TCL_LEAVE_ERR_MSG);
  1248.     if (result == NULL) {
  1249.         return TCL_ERROR;
  1250.     }
  1251.     interp->result = result;
  1252.     return TCL_OK;
  1253.     } else {
  1254.     Tcl_AppendResult(interp, "wrong # args: should be \"",
  1255.         argv[0], " varName ?newValue?\"", (char *) NULL);
  1256.     return TCL_ERROR;
  1257.     }
  1258. }
  1259.  
  1260. /*
  1261.  *----------------------------------------------------------------------
  1262.  *
  1263.  * Tcl_UnsetCmd --
  1264.  *
  1265.  *    This procedure is invoked to process the "unset" Tcl command.
  1266.  *    See the user documentation for details on what it does.
  1267.  *
  1268.  * Results:
  1269.  *    A standard Tcl result value.
  1270.  *
  1271.  * Side effects:
  1272.  *    See the user documentation.
  1273.  *
  1274.  *----------------------------------------------------------------------
  1275.  */
  1276.  
  1277.     /* ARGSUSED */
  1278. int
  1279. Tcl_UnsetCmd(dummy, interp, argc, argv)
  1280.     ClientData dummy;            /* Not used. */
  1281.     register Tcl_Interp *interp;    /* Current interpreter. */
  1282.     int argc;                /* Number of arguments. */
  1283.     char **argv;            /* Argument strings. */
  1284. {
  1285.     int i;
  1286.  
  1287.     if (argc < 2) {
  1288.     Tcl_AppendResult(interp, "wrong # args: should be \"",
  1289.         argv[0], " varName ?varName ...?\"", (char *) NULL);
  1290.     return TCL_ERROR;
  1291.     }
  1292.     for (i = 1; i < argc; i++) {
  1293.     if (Tcl_UnsetVar(interp, argv[i], TCL_LEAVE_ERR_MSG) != TCL_OK) {
  1294.         return TCL_ERROR;
  1295.     }
  1296.     }
  1297.     return TCL_OK;
  1298. }
  1299.  
  1300. /*
  1301.  *----------------------------------------------------------------------
  1302.  *
  1303.  * Tcl_AppendCmd --
  1304.  *
  1305.  *    This procedure is invoked to process the "append" Tcl command.
  1306.  *    See the user documentation for details on what it does.
  1307.  *
  1308.  * Results:
  1309.  *    A standard Tcl result value.
  1310.  *
  1311.  * Side effects:
  1312.  *    A variable's value may be changed.
  1313.  *
  1314.  *----------------------------------------------------------------------
  1315.  */
  1316.  
  1317.     /* ARGSUSED */
  1318. int
  1319. Tcl_AppendCmd(dummy, interp, argc, argv)
  1320.     ClientData dummy;            /* Not used. */
  1321.     register Tcl_Interp *interp;    /* Current interpreter. */
  1322.     int argc;                /* Number of arguments. */
  1323.     char **argv;            /* Argument strings. */
  1324. {
  1325.     int i;
  1326.     char *result = NULL;        /* (Initialization only needed to keep
  1327.                      * the compiler from complaining) */
  1328.  
  1329.     if (argc < 3) {
  1330.     Tcl_AppendResult(interp, "wrong # args: should be \"",
  1331.         argv[0], " varName value ?value ...?\"", (char *) NULL);
  1332.     return TCL_ERROR;
  1333.     }
  1334.  
  1335.     for (i = 2; i < argc; i++) {
  1336.     result = Tcl_SetVar(interp, argv[1], argv[i],
  1337.         TCL_APPEND_VALUE|TCL_LEAVE_ERR_MSG);
  1338.     if (result == NULL) {
  1339.         return TCL_ERROR;
  1340.     }
  1341.     }
  1342.     interp->result = result;
  1343.     return TCL_OK;
  1344. }
  1345.  
  1346. /*
  1347.  *----------------------------------------------------------------------
  1348.  *
  1349.  * Tcl_LappendCmd --
  1350.  *
  1351.  *    This procedure is invoked to process the "lappend" Tcl command.
  1352.  *    See the user documentation for details on what it does.
  1353.  *
  1354.  * Results:
  1355.  *    A standard Tcl result value.
  1356.  *
  1357.  * Side effects:
  1358.  *    A variable's value may be changed.
  1359.  *
  1360.  *----------------------------------------------------------------------
  1361.  */
  1362.  
  1363.     /* ARGSUSED */
  1364. int
  1365. Tcl_LappendCmd(dummy, interp, argc, argv)
  1366.     ClientData dummy;            /* Not used. */
  1367.     register Tcl_Interp *interp;    /* Current interpreter. */
  1368.     int argc;                /* Number of arguments. */
  1369.     char **argv;            /* Argument strings. */
  1370. {
  1371.     int i;
  1372.     char *result = NULL;        /* (Initialization only needed to keep
  1373.                      * the compiler from complaining) */
  1374.  
  1375.     if (argc < 3) {
  1376.     Tcl_AppendResult(interp, "wrong # args: should be \"",
  1377.         argv[0], " varName value ?value ...?\"", (char *) NULL);
  1378.     return TCL_ERROR;
  1379.     }
  1380.  
  1381.     for (i = 2; i < argc; i++) {
  1382.     result = Tcl_SetVar(interp, argv[1], argv[i],
  1383.         TCL_APPEND_VALUE|TCL_LIST_ELEMENT|TCL_LEAVE_ERR_MSG);
  1384.     if (result == NULL) {
  1385.         return TCL_ERROR;
  1386.     }
  1387.     }
  1388.     interp->result = result;
  1389.     return TCL_OK;
  1390. }
  1391.  
  1392. /*
  1393.  *----------------------------------------------------------------------
  1394.  *
  1395.  * Tcl_ArrayCmd --
  1396.  *
  1397.  *    This procedure is invoked to process the "array" Tcl command.
  1398.  *    See the user documentation for details on what it does.
  1399.  *
  1400.  * Results:
  1401.  *    A standard Tcl result value.
  1402.  *
  1403.  * Side effects:
  1404.  *    See the user documentation.
  1405.  *
  1406.  *----------------------------------------------------------------------
  1407.  */
  1408.  
  1409.     /* ARGSUSED */
  1410. int
  1411. Tcl_ArrayCmd(dummy, interp, argc, argv)
  1412.     ClientData dummy;            /* Not used. */
  1413.     register Tcl_Interp *interp;    /* Current interpreter. */
  1414.     int argc;                /* Number of arguments. */
  1415.     char **argv;            /* Argument strings. */
  1416. {
  1417.     int length;
  1418.     char c;
  1419.     Var *varPtr;
  1420.     Tcl_HashEntry *hPtr;
  1421.     Interp *iPtr = (Interp *) interp;
  1422.  
  1423.     if (argc < 3) {
  1424.     Tcl_AppendResult(interp, "wrong # args: should be \"",
  1425.         argv[0], " option arrayName ?arg ...?\"", (char *) NULL);
  1426.     return TCL_ERROR;
  1427.     }
  1428.  
  1429.     /*
  1430.      * Locate the array variable (and it better be an array).
  1431.      */
  1432.  
  1433.     if (iPtr->varFramePtr == NULL) {
  1434.     hPtr = Tcl_FindHashEntry(&iPtr->globalTable, argv[2]);
  1435.     } else {
  1436.     hPtr = Tcl_FindHashEntry(&iPtr->varFramePtr->varTable, argv[2]);
  1437.     }
  1438.     if (hPtr == NULL) {
  1439.     notArray:
  1440.     Tcl_AppendResult(interp, "\"", argv[2], "\" isn't an array",
  1441.         (char *) NULL);
  1442.     return TCL_ERROR;
  1443.     }
  1444.     varPtr = (Var *) Tcl_GetHashValue(hPtr);
  1445.     if (varPtr->flags & VAR_UPVAR) {
  1446.     varPtr = varPtr->value.upvarPtr;
  1447.     }
  1448.     if (!(varPtr->flags & VAR_ARRAY)) {
  1449.     goto notArray;
  1450.     }
  1451.  
  1452.     /*
  1453.      * Dispatch based on the option.
  1454.      */
  1455.  
  1456.     c = argv[1][0];
  1457.     length = strlen(argv[1]);
  1458.     if ((c == 'a') && (strncmp(argv[1], "anymore", length) == 0)) {
  1459.     ArraySearch *searchPtr;
  1460.  
  1461.     if (argc != 4) {
  1462.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  1463.             argv[0], " anymore arrayName searchId\"", (char *) NULL);
  1464.         return TCL_ERROR;
  1465.     }
  1466.     searchPtr = ParseSearchId(interp, varPtr, argv[2], argv[3]);
  1467.     if (searchPtr == NULL) {
  1468.         return TCL_ERROR;
  1469.     }
  1470.     while (1) {
  1471.         Var *varPtr2;
  1472.  
  1473.         if (searchPtr->nextEntry != NULL) {
  1474.         varPtr2 = (Var *) Tcl_GetHashValue(searchPtr->nextEntry);
  1475.         if (!(varPtr2->flags & VAR_UNDEFINED)) {
  1476.             break;
  1477.         }
  1478.         }
  1479.         searchPtr->nextEntry = Tcl_NextHashEntry(&searchPtr->search);
  1480.         if (searchPtr->nextEntry == NULL) {
  1481.         interp->result = "0";
  1482.         return TCL_OK;
  1483.         }
  1484.     }
  1485.     interp->result = "1";
  1486.     return TCL_OK;
  1487.     } else if ((c == 'd') && (strncmp(argv[1], "donesearch", length) == 0)) {
  1488.     ArraySearch *searchPtr, *prevPtr;
  1489.  
  1490.     if (argc != 4) {
  1491.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  1492.             argv[0], " donesearch arrayName searchId\"", (char *) NULL);
  1493.         return TCL_ERROR;
  1494.     }
  1495.     searchPtr = ParseSearchId(interp, varPtr, argv[2], argv[3]);
  1496.     if (searchPtr == NULL) {
  1497.         return TCL_ERROR;
  1498.     }
  1499.     if (varPtr->searchPtr == searchPtr) {
  1500.         varPtr->searchPtr = searchPtr->nextPtr;
  1501.     } else {
  1502.         for (prevPtr = varPtr->searchPtr; ; prevPtr = prevPtr->nextPtr) {
  1503.         if (prevPtr->nextPtr == searchPtr) {
  1504.             prevPtr->nextPtr = searchPtr->nextPtr;
  1505.             break;
  1506.         }
  1507.         }
  1508.     }
  1509.     ckfree((char *) searchPtr);
  1510.     } else if ((c == 'n') && (strncmp(argv[1], "names", length) == 0)
  1511.         && (length >= 2)) {
  1512.     Tcl_HashSearch search;
  1513.     Var *varPtr2;
  1514.  
  1515.     if (argc != 3) {
  1516.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  1517.             argv[0], " names arrayName\"", (char *) NULL);
  1518.         return TCL_ERROR;
  1519.     }
  1520.     for (hPtr = Tcl_FirstHashEntry(varPtr->value.tablePtr, &search);
  1521.         hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
  1522.         varPtr2 = (Var *) Tcl_GetHashValue(hPtr);
  1523.         if (varPtr2->flags & VAR_UNDEFINED) {
  1524.         continue;
  1525.         }
  1526.         Tcl_AppendElement(interp,
  1527.             Tcl_GetHashKey(varPtr->value.tablePtr, hPtr));
  1528.     }
  1529.     } else if ((c == 'n') && (strncmp(argv[1], "nextelement", length) == 0)
  1530.         && (length >= 2)) {
  1531.     ArraySearch *searchPtr;
  1532.     Tcl_HashEntry *hPtr;
  1533.  
  1534.     if (argc != 4) {
  1535.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  1536.             argv[0], " nextelement arrayName searchId\"",
  1537.             (char *) NULL);
  1538.         return TCL_ERROR;
  1539.     }
  1540.     searchPtr = ParseSearchId(interp, varPtr, argv[2], argv[3]);
  1541.     if (searchPtr == NULL) {
  1542.         return TCL_ERROR;
  1543.     }
  1544.     while (1) {
  1545.         Var *varPtr2;
  1546.  
  1547.         hPtr = searchPtr->nextEntry;
  1548.         if (hPtr == NULL) {
  1549.         hPtr = Tcl_NextHashEntry(&searchPtr->search);
  1550.         if (hPtr == NULL) {
  1551.             return TCL_OK;
  1552.         }
  1553.         } else {
  1554.         searchPtr->nextEntry = NULL;
  1555.         }
  1556.         varPtr2 = (Var *) Tcl_GetHashValue(hPtr);
  1557.         if (!(varPtr2->flags & VAR_UNDEFINED)) {
  1558.         break;
  1559.         }
  1560.     }
  1561.     interp->result = Tcl_GetHashKey(varPtr->value.tablePtr, hPtr);
  1562.     } else if ((c == 's') && (strncmp(argv[1], "size", length) == 0)
  1563.         && (length >= 2)) {
  1564.     Tcl_HashSearch search;
  1565.     Var *varPtr2;
  1566.     int size;
  1567.  
  1568.     if (argc != 3) {
  1569.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  1570.             argv[0], " size arrayName\"", (char *) NULL);
  1571.         return TCL_ERROR;
  1572.     }
  1573.     size = 0;
  1574.     for (hPtr = Tcl_FirstHashEntry(varPtr->value.tablePtr, &search);
  1575.         hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
  1576.         varPtr2 = (Var *) Tcl_GetHashValue(hPtr);
  1577.         if (varPtr2->flags & VAR_UNDEFINED) {
  1578.         continue;
  1579.         }
  1580.         size++;
  1581.     }
  1582.     sprintf(interp->result, "%d", size);
  1583.     } else if ((c == 's') && (strncmp(argv[1], "startsearch", length) == 0)
  1584.         && (length >= 2)) {
  1585.     ArraySearch *searchPtr;
  1586.  
  1587.     if (argc != 3) {
  1588.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  1589.             argv[0], " startsearch arrayName\"", (char *) NULL);
  1590.         return TCL_ERROR;
  1591.     }
  1592.     searchPtr = (ArraySearch *) ckalloc(sizeof(ArraySearch));
  1593.     if (varPtr->searchPtr == NULL) {
  1594.         searchPtr->id = 1;
  1595.         Tcl_AppendResult(interp, "s-1-", argv[2], (char *) NULL);
  1596.     } else {
  1597.         char string[20];
  1598.  
  1599.         searchPtr->id = varPtr->searchPtr->id + 1;
  1600.         sprintf(string, "%d", searchPtr->id);
  1601.         Tcl_AppendResult(interp, "s-", string, "-", argv[2],
  1602.             (char *) NULL);
  1603.     }
  1604.     searchPtr->varPtr = varPtr;
  1605.     searchPtr->nextEntry = Tcl_FirstHashEntry(varPtr->value.tablePtr,
  1606.         &searchPtr->search);
  1607.     searchPtr->nextPtr = varPtr->searchPtr;
  1608.     varPtr->searchPtr = searchPtr;
  1609.     } else {
  1610.     Tcl_AppendResult(interp, "bad option \"", argv[1],
  1611.         "\": should be anymore, donesearch, names, nextelement, ",
  1612.         "size, or startsearch", (char *) NULL);
  1613.     return TCL_ERROR;
  1614.     }
  1615.     return TCL_OK;
  1616. }
  1617.  
  1618. /*
  1619.  *----------------------------------------------------------------------
  1620.  *
  1621.  * MakeUpvar --
  1622.  *
  1623.  *    This procedure does all of the work of the "global" and "upvar"
  1624.  *    commands.
  1625.  *
  1626.  * Results:
  1627.  *    A standard Tcl completion code.  If an error occurs then an
  1628.  *    error message is left in iPtr->result.
  1629.  *
  1630.  * Side effects:
  1631.  *    The variable given by myName is linked to the variable in
  1632.  *    framePtr given by otherP1 and otherP2, so that references to
  1633.  *    myName are redirected to the other variable like a symbolic
  1634. *    link.
  1635.  *
  1636.  *----------------------------------------------------------------------
  1637.  */
  1638.  
  1639. static int
  1640. MakeUpvar(iPtr, framePtr, otherP1, otherP2, myName)
  1641.     Interp *iPtr;        /* Interpreter containing variables.  Used
  1642.                  * for error messages, too. */
  1643.     CallFrame *framePtr;    /* Call frame containing "other" variable.
  1644.                  * NULL means use global context. */
  1645.     char *otherP1, *otherP2;    /* Two-part name of variable in framePtr. */
  1646.     char *myName;        /* Name of variable in local table, which
  1647.                  * will refer to otherP1/P2.  Must be a
  1648.                  * scalar. */
  1649. {
  1650.     Tcl_HashEntry *hPtr;
  1651.     Var *otherPtr, *varPtr, *arrayPtr;
  1652.     CallFrame *savedFramePtr;
  1653.     int new;
  1654.  
  1655.     /*
  1656.      * In order to use LookupVar to find "other", temporarily replace
  1657.      * the current frame pointer in the interpreter.
  1658.      */
  1659.  
  1660.     savedFramePtr = iPtr->varFramePtr;
  1661.     iPtr->varFramePtr = framePtr;
  1662.     otherPtr = LookupVar((Tcl_Interp *) iPtr, otherP1, otherP2,
  1663.         TCL_LEAVE_ERR_MSG, "access", CRT_PART1|CRT_PART2, &arrayPtr);
  1664.     iPtr->varFramePtr = savedFramePtr;
  1665.     if (otherPtr == NULL) {
  1666.     return TCL_ERROR;
  1667.     }
  1668.     hPtr = Tcl_CreateHashEntry(&iPtr->varFramePtr->varTable, myName, &new);
  1669.     if (new) {
  1670.     varPtr = NewVar();
  1671.     Tcl_SetHashValue(hPtr, varPtr);
  1672.     varPtr->hPtr = hPtr;
  1673.     } else {
  1674.     /*
  1675.      * The variable already exists.  If it's not an upvar then it's
  1676.      * an error.  If it is an upvar, then just disconnect it from the
  1677.      * thing it currently refers to.
  1678.      */
  1679.  
  1680.     varPtr = (Var *) Tcl_GetHashValue(hPtr);
  1681.     if (varPtr->flags & VAR_UPVAR) {
  1682.         Var *upvarPtr;
  1683.  
  1684.         upvarPtr = varPtr->value.upvarPtr;
  1685.         if (upvarPtr == otherPtr) {
  1686.         return TCL_OK;
  1687.         }
  1688.         upvarPtr->refCount--;
  1689.         if (upvarPtr->flags & VAR_UNDEFINED) {
  1690.         CleanupVar(upvarPtr, (Var *) NULL);
  1691.         }
  1692.     } else if (!(varPtr->flags & VAR_UNDEFINED)) {
  1693.         Tcl_AppendResult((Tcl_Interp *) iPtr, "variable \"", myName,
  1694.         "\" already exists", (char *) NULL);
  1695.         return TCL_ERROR;
  1696.     }
  1697.     }
  1698.     varPtr->flags = (varPtr->flags & ~VAR_UNDEFINED) | VAR_UPVAR;
  1699.     varPtr->value.upvarPtr = otherPtr;
  1700.     otherPtr->refCount++;
  1701.     return TCL_OK;
  1702. }
  1703.  
  1704. /*
  1705.  *----------------------------------------------------------------------
  1706.  *
  1707.  * Tcl_GlobalCmd --
  1708.  *
  1709.  *    This procedure is invoked to process the "global" Tcl command.
  1710.  *    See the user documentation for details on what it does.
  1711.  *
  1712.  * Results:
  1713.  *    A standard Tcl result value.
  1714.  *
  1715.  * Side effects:
  1716.  *    See the user documentation.
  1717.  *
  1718.  *----------------------------------------------------------------------
  1719.  */
  1720.  
  1721.     /* ARGSUSED */
  1722. int
  1723. Tcl_GlobalCmd(dummy, interp, argc, argv)
  1724.     ClientData dummy;            /* Not used. */
  1725.     Tcl_Interp *interp;            /* Current interpreter. */
  1726.     int argc;                /* Number of arguments. */
  1727.     char **argv;            /* Argument strings. */
  1728. {
  1729.     register Interp *iPtr = (Interp *) interp;
  1730.  
  1731.     if (argc < 2) {
  1732.     Tcl_AppendResult((Tcl_Interp *) iPtr, "wrong # args: should be \"",
  1733.         argv[0], " varName ?varName ...?\"", (char *) NULL);
  1734.     return TCL_ERROR;
  1735.     }
  1736.     if (iPtr->varFramePtr == NULL) {
  1737.     return TCL_OK;
  1738.     }
  1739.  
  1740.     for (argc--, argv++; argc > 0; argc--, argv++) {
  1741.     if (MakeUpvar(iPtr, (CallFrame *) NULL, *argv, (char *) NULL, *argv)
  1742.         != TCL_OK) {
  1743.         return TCL_ERROR;
  1744.     }
  1745.     }
  1746.     return TCL_OK;
  1747. }
  1748.  
  1749. /*
  1750.  *----------------------------------------------------------------------
  1751.  *
  1752.  * Tcl_UpvarCmd --
  1753.  *
  1754.  *    This procedure is invoked to process the "upvar" Tcl command.
  1755.  *    See the user documentation for details on what it does.
  1756.  *
  1757.  * Results:
  1758.  *    A standard Tcl result value.
  1759.  *
  1760.  * Side effects:
  1761.  *    See the user documentation.
  1762.  *
  1763.  *----------------------------------------------------------------------
  1764.  */
  1765.  
  1766.     /* ARGSUSED */
  1767. int
  1768. Tcl_UpvarCmd(dummy, interp, argc, argv)
  1769.     ClientData dummy;            /* Not used. */
  1770.     Tcl_Interp *interp;            /* Current interpreter. */
  1771.     int argc;                /* Number of arguments. */
  1772.     char **argv;            /* Argument strings. */
  1773. {
  1774.     register Interp *iPtr = (Interp *) interp;
  1775.     int result;
  1776.     CallFrame *framePtr;
  1777.     register char *p;
  1778.  
  1779.     if (argc < 3) {
  1780.     upvarSyntax:
  1781.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1782.         " ?level? otherVar localVar ?otherVar localVar ...?\"",
  1783.         (char *) NULL);
  1784.     return TCL_ERROR;
  1785.     }
  1786.  
  1787.     /*
  1788.      * Find the hash table containing the variable being referenced.
  1789.      */
  1790.  
  1791.     result = TclGetFrame(interp, argv[1], &framePtr);
  1792.     if (result == -1) {
  1793.     return TCL_ERROR;
  1794.     }
  1795.     argc -= result+1;
  1796.     if ((argc & 1) != 0) {
  1797.     goto upvarSyntax;
  1798.     }
  1799.     argv += result+1;
  1800.  
  1801.     /*
  1802.      * Iterate over all the pairs of (other variable, local variable)
  1803.      * names.  For each pair, divide the other variable name into two
  1804.      * parts, then call MakeUpvar to do all the work of creating linking
  1805.      * it to the local variable.
  1806.      */
  1807.  
  1808.     for ( ; argc > 0; argc -= 2, argv += 2) {
  1809.     for (p = argv[0]; *p != 0; p++) {
  1810.         if (*p == '(') {
  1811.         char *open = p;
  1812.  
  1813.         do {
  1814.             p++;
  1815.         } while (*p != '\0');
  1816.         p--;
  1817.         if (*p != ')') {
  1818.             goto scalar;
  1819.         }
  1820.         *open = '\0';
  1821.         *p = '\0';
  1822.         result = MakeUpvar(iPtr, framePtr, argv[0], open+1, argv[1]);
  1823.         *open = '(';
  1824.         *p = ')';
  1825.         goto checkResult;
  1826.         }
  1827.     }
  1828.     scalar:
  1829.     result = MakeUpvar(iPtr, framePtr, argv[0], (char *) NULL, argv[1]);
  1830.  
  1831.     checkResult:
  1832.     if (result != TCL_OK) {
  1833.         return TCL_ERROR;
  1834.     }
  1835.     }
  1836.     return TCL_OK;
  1837. }
  1838.  
  1839. /*
  1840.  *----------------------------------------------------------------------
  1841.  *
  1842.  * CallTraces --
  1843.  *
  1844.  *    This procedure is invoked to find and invoke relevant
  1845.  *    trace procedures associated with a particular operation on
  1846.  *    a variable.  This procedure invokes traces both on the
  1847.  *    variable and on its containing array (where relevant).
  1848.  *
  1849.  * Results:
  1850.  *    The return value is NULL if no trace procedures were invoked, or
  1851.  *    if all the invoked trace procedures returned successfully.
  1852.  *    The return value is non-zero if a trace procedure returned an
  1853.  *    error (in this case no more trace procedures were invoked after
  1854.  *    the error was returned).  In this case the return value is a
  1855.  *    pointer to a static string describing the error.
  1856.  *
  1857.  * Side effects:
  1858.  *    Almost anything can happen, depending on trace;  this procedure
  1859.  *    itself doesn't have any side effects.
  1860.  *
  1861.  *----------------------------------------------------------------------
  1862.  */
  1863.  
  1864. static char *
  1865. CallTraces(iPtr, arrayPtr, varPtr, part1, part2, flags)
  1866.     Interp *iPtr;            /* Interpreter containing variable. */
  1867.     register Var *arrayPtr;        /* Pointer to array variable that
  1868.                      * contains the variable, or NULL if
  1869.                      * the variable isn't an element of an
  1870.                      * array. */
  1871.     Var *varPtr;            /* Variable whose traces are to be
  1872.                      * invoked. */
  1873.     char *part1, *part2;        /* Variable's two-part name. */
  1874.     int flags;                /* Flags to pass to trace procedures:
  1875.                      * indicates what's happening to
  1876.                      * variable, plus other stuff like
  1877.                      * TCL_GLOBAL_ONLY and
  1878.                      * TCL_INTERP_DESTROYED. */
  1879. {
  1880.     register VarTrace *tracePtr;
  1881.     ActiveVarTrace active;
  1882.     char *result;
  1883.  
  1884.     /*
  1885.      * If there are already similar trace procedures active for the
  1886.      * variable, don't call them again.
  1887.      */
  1888.  
  1889.     if (varPtr->flags & VAR_TRACE_ACTIVE) {
  1890.     return NULL;
  1891.     }
  1892.     varPtr->flags |= VAR_TRACE_ACTIVE;
  1893.     varPtr->refCount++;
  1894.  
  1895.     /*
  1896.      * Invoke traces on the array containing the variable, if relevant.
  1897.      */
  1898.  
  1899.     result = NULL;
  1900.     active.nextPtr = iPtr->activeTracePtr;
  1901.     iPtr->activeTracePtr = &active;
  1902.     if (arrayPtr != NULL) {
  1903.     arrayPtr->refCount++;
  1904.     active.varPtr = arrayPtr;
  1905.     for (tracePtr = arrayPtr->tracePtr;  tracePtr != NULL;
  1906.         tracePtr = active.nextTracePtr) {
  1907.         active.nextTracePtr = tracePtr->nextPtr;
  1908.         if (!(tracePtr->flags & flags)) {
  1909.         continue;
  1910.         }
  1911.         result = (*tracePtr->traceProc)(tracePtr->clientData,
  1912.             (Tcl_Interp *) iPtr, part1, part2, flags);
  1913.         if (result != NULL) {
  1914.         if (flags & TCL_TRACE_UNSETS) {
  1915.             result = NULL;
  1916.         } else {
  1917.             goto done;
  1918.         }
  1919.         }
  1920.     }
  1921.     }
  1922.  
  1923.     /*
  1924.      * Invoke traces on the variable itself.
  1925.      */
  1926.  
  1927.     if (flags & TCL_TRACE_UNSETS) {
  1928.     flags |= TCL_TRACE_DESTROYED;
  1929.     }
  1930.     active.varPtr = varPtr;
  1931.     for (tracePtr = varPtr->tracePtr; tracePtr != NULL;
  1932.         tracePtr = active.nextTracePtr) {
  1933.     active.nextTracePtr = tracePtr->nextPtr;
  1934.     if (!(tracePtr->flags & flags)) {
  1935.         continue;
  1936.     }
  1937.     result = (*tracePtr->traceProc)(tracePtr->clientData,
  1938.         (Tcl_Interp *) iPtr, part1, part2, flags);
  1939.     if (result != NULL) {
  1940.         if (flags & TCL_TRACE_UNSETS) {
  1941.         result = NULL;
  1942.         } else {
  1943.         goto done;
  1944.         }
  1945.     }
  1946.     }
  1947.  
  1948.     /*
  1949.      * Restore the variable's flags, remove the record of our active
  1950.      * traces, and then return.
  1951.      */
  1952.  
  1953.     done:
  1954.     if (arrayPtr != NULL) {
  1955.     arrayPtr->refCount--;
  1956.     }
  1957.     varPtr->flags &= ~VAR_TRACE_ACTIVE;
  1958.     varPtr->refCount--;
  1959.     iPtr->activeTracePtr = active.nextPtr;
  1960.     return result;
  1961. }
  1962.  
  1963. /*
  1964.  *----------------------------------------------------------------------
  1965.  *
  1966.  * NewVar --
  1967.  *
  1968.  *    Create a new variable with a given amount of storage
  1969.  *    space.
  1970.  *
  1971.  * Results:
  1972.  *    The return value is a pointer to the new variable structure.
  1973.  *    The variable will not be part of any hash table yet.  Its
  1974.  *    initial value is empty.
  1975.  *
  1976.  * Side effects:
  1977.  *    Storage gets allocated.
  1978.  *
  1979.  *----------------------------------------------------------------------
  1980.  */
  1981.  
  1982. static Var *
  1983. NewVar()
  1984. {
  1985.     register Var *varPtr;
  1986.  
  1987.     varPtr = (Var *) ckalloc(sizeof(Var));
  1988.     varPtr->valueLength = 0;
  1989.     varPtr->valueSpace = 0;
  1990.     varPtr->value.string = NULL;
  1991.     varPtr->hPtr = NULL;
  1992.     varPtr->refCount = 0;
  1993.     varPtr->tracePtr = NULL;
  1994.     varPtr->searchPtr = NULL;
  1995.     varPtr->flags = VAR_UNDEFINED;
  1996.     return varPtr;
  1997. }
  1998.  
  1999. /*
  2000.  *----------------------------------------------------------------------
  2001.  *
  2002.  * ParseSearchId --
  2003.  *
  2004.  *    This procedure translates from a string to a pointer to an
  2005.  *    active array search (if there is one that matches the string).
  2006.  *
  2007.  * Results:
  2008.  *    The return value is a pointer to the array search indicated
  2009.  *    by string, or NULL if there isn't one.  If NULL is returned,
  2010.  *    interp->result contains an error message.
  2011.  *
  2012.  * Side effects:
  2013.  *    None.
  2014.  *
  2015.  *----------------------------------------------------------------------
  2016.  */
  2017.  
  2018. static ArraySearch *
  2019. ParseSearchId(interp, varPtr, varName, string)
  2020.     Tcl_Interp *interp;        /* Interpreter containing variable. */
  2021.     Var *varPtr;        /* Array variable search is for. */
  2022.     char *varName;        /* Name of array variable that search is
  2023.                  * supposed to be for. */
  2024.     char *string;        /* String containing id of search.  Must have
  2025.                  * form "search-num-var" where "num" is a
  2026.                  * decimal number and "var" is a variable
  2027.                  * name. */
  2028. {
  2029.     char *end;
  2030.     int id;
  2031.     ArraySearch *searchPtr;
  2032.  
  2033.     /*
  2034.      * Parse the id into the three parts separated by dashes.
  2035.      */
  2036.  
  2037.     if ((string[0] != 's') || (string[1] != '-')) {
  2038.     syntax:
  2039.     Tcl_AppendResult(interp, "illegal search identifier \"", string,
  2040.         "\"", (char *) NULL);
  2041.     return NULL;
  2042.     }
  2043.     id = strtoul(string+2, &end, 10);
  2044.     if ((end == (string+2)) || (*end != '-')) {
  2045.     goto syntax;
  2046.     }
  2047.     if (strcmp(end+1, varName) != 0) {
  2048.     Tcl_AppendResult(interp, "search identifier \"", string,
  2049.         "\" isn't for variable \"", varName, "\"", (char *) NULL);
  2050.     return NULL;
  2051.     }
  2052.  
  2053.     /*
  2054.      * Search through the list of active searches on the interpreter
  2055.      * to see if the desired one exists.
  2056.      */
  2057.  
  2058.     for (searchPtr = varPtr->searchPtr; searchPtr != NULL;
  2059.         searchPtr = searchPtr->nextPtr) {
  2060.     if (searchPtr->id == id) {
  2061.         return searchPtr;
  2062.     }
  2063.     }
  2064.     Tcl_AppendResult(interp, "couldn't find search \"", string, "\"",
  2065.         (char *) NULL);
  2066.     return NULL;
  2067. }
  2068.  
  2069. /*
  2070.  *----------------------------------------------------------------------
  2071.  *
  2072.  * DeleteSearches --
  2073.  *
  2074.  *    This procedure is called to free up all of the searches
  2075.  *    associated with an array variable.
  2076.  *
  2077.  * Results:
  2078.  *    None.
  2079.  *
  2080.  * Side effects:
  2081.  *    Memory is released to the storage allocator.
  2082.  *
  2083.  *----------------------------------------------------------------------
  2084.  */
  2085.  
  2086. static void
  2087. DeleteSearches(arrayVarPtr)
  2088.     register Var *arrayVarPtr;        /* Variable whose searches are
  2089.                      * to be deleted. */
  2090. {
  2091.     ArraySearch *searchPtr;
  2092.  
  2093.     while (arrayVarPtr->searchPtr != NULL) {
  2094.     searchPtr = arrayVarPtr->searchPtr;
  2095.     arrayVarPtr->searchPtr = searchPtr->nextPtr;
  2096.     ckfree((char *) searchPtr);
  2097.     }
  2098. }
  2099.  
  2100. /*
  2101.  *----------------------------------------------------------------------
  2102.  *
  2103.  * TclDeleteVars --
  2104.  *
  2105.  *    This procedure is called to recycle all the storage space
  2106.  *    associated with a table of variables.  For this procedure
  2107.  *    to work correctly, it must not be possible for any of the
  2108.  *    variable in the table to be accessed from Tcl commands
  2109.  *    (e.g. from trace procedures).
  2110.  *
  2111.  * Results:
  2112.  *    None.
  2113.  *
  2114.  * Side effects:
  2115.  *    Variables are deleted and trace procedures are invoked, if
  2116.  *    any are declared.
  2117.  *
  2118.  *----------------------------------------------------------------------
  2119.  */
  2120.  
  2121. void
  2122. TclDeleteVars(iPtr, tablePtr)
  2123.     Interp *iPtr;        /* Interpreter to which variables belong. */
  2124.     Tcl_HashTable *tablePtr;    /* Hash table containing variables to
  2125.                  * delete. */
  2126. {
  2127.     Tcl_HashSearch search;
  2128.     Tcl_HashEntry *hPtr;
  2129.     register Var *varPtr;
  2130.     Var *upvarPtr;
  2131.     int flags;
  2132.     ActiveVarTrace *activePtr;
  2133.  
  2134.     flags = TCL_TRACE_UNSETS;
  2135.     if (tablePtr == &iPtr->globalTable) {
  2136.     flags |= TCL_INTERP_DESTROYED | TCL_GLOBAL_ONLY;
  2137.     }
  2138.     for (hPtr = Tcl_FirstHashEntry(tablePtr, &search); hPtr != NULL;
  2139.         hPtr = Tcl_NextHashEntry(&search)) {
  2140.     varPtr = (Var *) Tcl_GetHashValue(hPtr);
  2141.  
  2142.     /*
  2143.      * For global/upvar variables referenced in procedures, decrement
  2144.      * the reference count on the variable referred to, and free up
  2145.      * the referenced variable if it's no longer needed.
  2146.      */
  2147.  
  2148.     if (varPtr->flags & VAR_UPVAR) {
  2149.         upvarPtr = varPtr->value.upvarPtr;
  2150.         upvarPtr->refCount--;
  2151.         if (upvarPtr->flags & VAR_UNDEFINED) {
  2152.         CleanupVar(upvarPtr, (Var *) NULL);
  2153.         }
  2154.     }
  2155.  
  2156.     /*
  2157.      * Invoke traces on the variable that is being deleted, then
  2158.      * free up the variable's space (no need to free the hash entry
  2159.      * here, unless we're dealing with a global variable:  the
  2160.      * hash entries will be deleted automatically when the whole
  2161.      * table is deleted).
  2162.      */
  2163.  
  2164.     if (varPtr->tracePtr != NULL) {
  2165.         (void) CallTraces(iPtr, (Var *) NULL, varPtr,
  2166.             Tcl_GetHashKey(tablePtr, hPtr), (char *) NULL, flags);
  2167.         while (varPtr->tracePtr != NULL) {
  2168.         VarTrace *tracePtr = varPtr->tracePtr;
  2169.         varPtr->tracePtr = tracePtr->nextPtr;
  2170.         ckfree((char *) tracePtr);
  2171.         }
  2172.         for (activePtr = iPtr->activeTracePtr; activePtr != NULL;
  2173.             activePtr = activePtr->nextPtr) {
  2174.         if (activePtr->varPtr == varPtr) {
  2175.             activePtr->nextTracePtr = NULL;
  2176.         }
  2177.         }
  2178.     }
  2179.     if (varPtr->flags & VAR_ARRAY) {
  2180.         DeleteArray(iPtr, Tcl_GetHashKey(tablePtr, hPtr), varPtr, flags);
  2181.     }
  2182.     if (varPtr->valueSpace > 0) {
  2183.         /*
  2184.          * SPECIAL TRICK:  it's possible that the interpreter's result
  2185.          * currently points to this variable (for example, a "set" or
  2186.          * "lappend" command was the last command in a procedure that's
  2187.          * being returned from).  If this is the case, then just pass
  2188.          * ownership of the value string to the Tcl interpreter.
  2189.          */
  2190.  
  2191.         if (iPtr->result == varPtr->value.string) {
  2192.         iPtr->freeProc = (Tcl_FreeProc *) free;
  2193.         } else {
  2194.         ckfree(varPtr->value.string);
  2195.         }
  2196.         varPtr->valueSpace = 0;
  2197.     }
  2198.     varPtr->hPtr = NULL;
  2199.     varPtr->tracePtr = NULL;
  2200.     varPtr->flags = VAR_UNDEFINED;
  2201.     if (varPtr->refCount == 0) {
  2202.         ckfree((char *) varPtr);
  2203.     }
  2204.     }
  2205.     Tcl_DeleteHashTable(tablePtr);
  2206. }
  2207.  
  2208. /*
  2209.  *----------------------------------------------------------------------
  2210.  *
  2211.  * DeleteArray --
  2212.  *
  2213.  *    This procedure is called to free up everything in an array
  2214.  *    variable.  It's the caller's responsibility to make sure
  2215.  *    that the array is no longer accessible before this procedure
  2216.  *    is called.
  2217.  *
  2218.  * Results:
  2219.  *    None.
  2220.  *
  2221.  * Side effects:
  2222.  *    All storage associated with varPtr's array elements is deleted
  2223.  *    (including the hash table).  Delete trace procedures for
  2224.  *    array elements are invoked.
  2225.  *
  2226.  *----------------------------------------------------------------------
  2227.  */
  2228.  
  2229. static void
  2230. DeleteArray(iPtr, arrayName, varPtr, flags)
  2231.     Interp *iPtr;            /* Interpreter containing array. */
  2232.     char *arrayName;            /* Name of array (used for trace
  2233.                      * callbacks). */
  2234.     Var *varPtr;            /* Pointer to variable structure. */
  2235.     int flags;                /* Flags to pass to CallTraces:
  2236.                      * TCL_TRACE_UNSETS and sometimes
  2237.                      * TCL_INTERP_DESTROYED and/or
  2238.                      * TCL_GLOBAL_ONLY. */
  2239. {
  2240.     Tcl_HashSearch search;
  2241.     register Tcl_HashEntry *hPtr;
  2242.     register Var *elPtr;
  2243.     ActiveVarTrace *activePtr;
  2244.  
  2245.     DeleteSearches(varPtr);
  2246.     for (hPtr = Tcl_FirstHashEntry(varPtr->value.tablePtr, &search);
  2247.         hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
  2248.     elPtr = (Var *) Tcl_GetHashValue(hPtr);
  2249.     if (elPtr->valueSpace != 0) {
  2250.         ckfree(elPtr->value.string);
  2251.         elPtr->valueSpace = 0;
  2252.     }
  2253.     elPtr->hPtr = NULL;
  2254.     if (elPtr->tracePtr != NULL) {
  2255.         elPtr->flags &= ~VAR_TRACE_ACTIVE;
  2256.         (void) CallTraces(iPtr, (Var *) NULL, elPtr, arrayName,
  2257.             Tcl_GetHashKey(varPtr->value.tablePtr, hPtr), flags);
  2258.         while (elPtr->tracePtr != NULL) {
  2259.         VarTrace *tracePtr = elPtr->tracePtr;
  2260.         elPtr->tracePtr = tracePtr->nextPtr;
  2261.         ckfree((char *) tracePtr);
  2262.         }
  2263.         for (activePtr = iPtr->activeTracePtr; activePtr != NULL;
  2264.             activePtr = activePtr->nextPtr) {
  2265.         if (activePtr->varPtr == elPtr) {
  2266.             activePtr->nextTracePtr = NULL;
  2267.         }
  2268.         }
  2269.     }
  2270.     elPtr->flags = VAR_UNDEFINED;
  2271.     if (elPtr->refCount == 0) {
  2272.         ckfree((char *) elPtr);
  2273.     }
  2274.     }
  2275.     Tcl_DeleteHashTable(varPtr->value.tablePtr);
  2276.     ckfree((char *) varPtr->value.tablePtr);
  2277. }
  2278.  
  2279. /*
  2280.  *----------------------------------------------------------------------
  2281.  *
  2282.  * CleanupVar --
  2283.  *
  2284.  *    This procedure is called when it looks like it may be OK
  2285.  *    to free up the variable's record and hash table entry, and
  2286.  *    those of its containing parent.  It's called, for example,
  2287.  *    when a trace on a variable deletes the variable.
  2288.  *
  2289.  * Results:
  2290.  *    None.
  2291.  *
  2292.  * Side effects:
  2293.  *    If the variable (or its containing array) really is dead then
  2294.  *    its record, and possibly its hash table entry, gets freed up.
  2295.  *
  2296.  *----------------------------------------------------------------------
  2297.  */
  2298.  
  2299. static void
  2300. CleanupVar(varPtr, arrayPtr)
  2301.     Var *varPtr;        /* Pointer to variable that may be a
  2302.                  * candidate for being expunged. */
  2303.     Var *arrayPtr;        /* Array that contains the variable, or
  2304.                  * NULL if this variable isn't an array
  2305.                  * element. */
  2306. {
  2307.     if ((varPtr->flags & VAR_UNDEFINED) && (varPtr->refCount == 0)
  2308.         && (varPtr->tracePtr == NULL)) {
  2309.     if (varPtr->hPtr != NULL) {
  2310.         Tcl_DeleteHashEntry(varPtr->hPtr);
  2311.     }
  2312.     ckfree((char *) varPtr);
  2313.     }
  2314.     if (arrayPtr != NULL) {
  2315.     if ((arrayPtr->flags & VAR_UNDEFINED) && (arrayPtr->refCount == 0)
  2316.         && (arrayPtr->tracePtr == NULL)) {
  2317.         if (arrayPtr->hPtr != NULL) {
  2318.         Tcl_DeleteHashEntry(arrayPtr->hPtr);
  2319.         }
  2320.         ckfree((char *) arrayPtr);
  2321.     }
  2322.     }
  2323.     return;
  2324. }
  2325.  
  2326. /*
  2327.  *----------------------------------------------------------------------
  2328.  *
  2329.  * VarErrMsg --
  2330.  *
  2331.  *    Generate a reasonable error message describing why a variable
  2332.  *    operation failed.
  2333.  *
  2334.  * Results:
  2335.  *    None.
  2336.  *
  2337.  * Side effects:
  2338.  *    Interp->result is reset to hold a message identifying the
  2339.  *    variable given by part1 and part2 and describing why the
  2340.  *    variable operation failed.
  2341.  *
  2342.  *----------------------------------------------------------------------
  2343.  */
  2344.  
  2345. static void
  2346. VarErrMsg(interp, part1, part2, operation, reason)
  2347.     Tcl_Interp *interp;        /* Interpreter in which to record message. */
  2348.     char *part1, *part2;    /* Variable's two-part name. */
  2349.     char *operation;        /* String describing operation that failed,
  2350.                  * e.g. "read", "set", or "unset". */
  2351.     char *reason;        /* String describing why operation failed. */
  2352. {
  2353.     Tcl_ResetResult(interp);
  2354.     Tcl_AppendResult(interp, "can't ", operation, " \"", part1, (char *) NULL);
  2355.     if (part2 != NULL) {
  2356.     Tcl_AppendResult(interp, "(", part2, ")", (char *) NULL);
  2357.     }
  2358.     Tcl_AppendResult(interp, "\": ", reason, (char *) NULL);
  2359. }
  2360.