/*
* tclLink.c --
*
* This file implements linked variables (a C variable that is
* tied to a Tcl variable). The idea of linked variables was
* first suggested by Andreas Stolcke and this implementation is
* based heavily on a prototype implementation provided by
* him.
*
* Copyright (c) 1993 The Regents of the University of California.
* Copyright (c) 1994-1996 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* SCCS: @(#) tclLink.c 1.13 96/08/09 16:23:34
*/
#include "tclInt.h"
/*
* For each linked variable there is a data structure of the following
* type, which describes the link and is the clientData for the trace
* set on the Tcl variable.
*/
typedef struct Link {
* is needed during trace callbacks, since
* the actual variable may be aliased at
* that time via upvar. */
union {
int i;
double d;
* avoid string conversions. */
* for definitions. */
} Link;
/*
* Definitions for flag bits:
* LINK_READ_ONLY - 1 means errors should be generated if Tcl
* script attempts to write variable.
* LINK_BEING_UPDATED - 1 means that a call to Tcl_UpdateLinkedVar
* is in progress for this variable, so
* trace callbacks on the variable should
* be ignored.
*/
/*
* Forward references to procedures defined later in this file:
*/
int flags));
char *buffer));
/*
*----------------------------------------------------------------------
*
* Tcl_LinkVar --
*
* Link a C variable to a Tcl variable so that changes to either
* one causes the other to change.
*
* Results:
* The return value is TCL_OK if everything went well or TCL_ERROR
* if an error occurred (interp->result is also set after errors).
*
* Side effects:
* The value at *addr is linked to the Tcl variable "varName",
* using "type" to convert between string values for Tcl and
* binary values for *addr.
*
*----------------------------------------------------------------------
*/
int
char *varName; /* Name of a global variable in interp. */
char *addr; /* Address of a C variable to be linked
* to varName. */
int type; /* Type of C variable: TCL_LINK_INT, etc.
* Also may have TCL_LINK_READ_ONLY
* OR'ed in. */
{
int code;
if (type & TCL_LINK_READ_ONLY) {
} else {
}
return TCL_ERROR;
}
(ClientData) linkPtr);
}
return code;
}
/*
*----------------------------------------------------------------------
*
* Tcl_UnlinkVar --
*
* Destroy the link between a Tcl variable and a C variable.
*
* Results:
* None.
*
* Side effects:
* If "varName" was previously linked to a C variable, the link
* is broken to make the variable independent. If there was no
* previous link for "varName" then nothing happens.
*
*----------------------------------------------------------------------
*/
void
char *varName; /* Global variable in interp to unlink. */
{
return;
}
}
/*
*----------------------------------------------------------------------
*
* Tcl_UpdateLinkedVar --
*
* This procedure is invoked after a linked variable has been
* changed by C code. It updates the Tcl variable so that
* traces on the variable will trigger.
*
* Results:
* None.
*
* Side effects:
* The Tcl variable "varName" is updated from its C value,
* causing traces on the variable to trigger.
*
*----------------------------------------------------------------------
*/
void
char *varName; /* Name of global variable that is linked. */
{
int savedFlag;
return;
}
}
/*
*----------------------------------------------------------------------
*
* LinkTraceProc --
*
* This procedure is invoked when a linked Tcl variable is read,
* written, or unset from Tcl. It's responsible for keeping the
* C variable in sync with the Tcl variable.
*
* Results:
* If all goes well, NULL is returned; otherwise an error message
* is returned.
*
* Side effects:
* The C variable may be updated to make it consistent with the
* Tcl variable, or the Tcl variable may be overwritten to reject
* a modification.
*
*----------------------------------------------------------------------
*/
static char *
char *name1; /* First part of variable name. */
char *name2; /* Second part of variable name. */
int flags; /* Miscellaneous additional information. */
{
int changed;
/*
* If the variable is being unset, then just re-create it (with a
* trace) unless the whole interpreter is going away.
*/
if (flags & TCL_TRACE_UNSETS) {
if (flags & TCL_INTERP_DESTROYED) {
} else if (flags & TCL_TRACE_DESTROYED) {
}
return NULL;
}
/*
* If we were invoked because of a call to Tcl_UpdateLinkedVar, then
* don't do anything at all. In particular, we don't want to get
* upset that the variable is being modified, even if it is
* supposed to be read-only.
*/
return NULL;
}
/*
* For read accesses, update the Tcl variable if the C variable
* has changed since the last time we updated the Tcl variable.
*/
if (flags & TCL_TRACE_READS) {
case TCL_LINK_INT:
case TCL_LINK_BOOLEAN:
break;
case TCL_LINK_DOUBLE:
break;
case TCL_LINK_STRING:
changed = 1;
break;
default:
return "internal error: bad linked variable type";
}
if (changed) {
}
return NULL;
}
/*
* For writes, first make sure that the variable is writable. Then
* convert the Tcl value to C if possible. If the variable isn't
* writable or can't be converted, then restore the varaible's old
* value and return an error. Another tricky thing: we have to save
* and restore the interpreter's result, since the variable access
* could occur when the result has been partially set.
*/
return "linked variable is read-only";
}
/*
* This shouldn't ever happen.
*/
return "internal error: linked variable couldn't be read";
}
case TCL_LINK_INT:
return "variable must have integer value";
}
break;
case TCL_LINK_DOUBLE:
!= TCL_OK) {
return "variable must have real value";
}
break;
case TCL_LINK_BOOLEAN:
!= TCL_OK) {
return "variable must have boolean value";
}
break;
case TCL_LINK_STRING:
}
break;
default:
return "internal error: bad linked variable type";
}
return NULL;
}
/*
*----------------------------------------------------------------------
*
* StringValue --
*
* Converts the value of a C variable to a string for use in a
* Tcl variable to which it is linked.
*
* Results:
* The return value is a pointer
to a string that represents
* the value of the C variable given by linkPtr.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static char *
char *buffer; /* Small buffer to use for converting
* values. Must have TCL_DOUBLE_SPACE
* bytes or more. */
{
char *p;
case TCL_LINK_INT:
return buffer;
case TCL_LINK_DOUBLE:
return buffer;
case TCL_LINK_BOOLEAN:
return "1";
}
return "0";
case TCL_LINK_STRING:
if (p == NULL) {
return "NULL";
}
return p;
}
/*
* This code only gets executed if the link type is unknown
* (shouldn't ever happen).
*/
return "??";
}