2002-04-26 05:16:26 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
|
|
|
/* MLTk, Tcl/Tk interface of Objective Caml */
|
|
|
|
/* */
|
|
|
|
/* Francois Rouaix, Francois Pessaux, Jun Furuse and Pierre Weis */
|
|
|
|
/* projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* Jacques Garrigue, Kyoto University RIMS */
|
|
|
|
/* */
|
|
|
|
/* Copyright 2002 Institut National de Recherche en Informatique et */
|
|
|
|
/* en Automatique and Kyoto University. All rights reserved. */
|
|
|
|
/* This file is distributed under the terms of the GNU Library */
|
|
|
|
/* General Public License, with the special exception on linking */
|
|
|
|
/* described in file LICENSE found in the Objective Caml source tree. */
|
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
1999-12-16 04:25:11 -08:00
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
1999-11-30 06:59:39 -08:00
|
|
|
/* Alternative to tkwait variable */
|
|
|
|
#include <string.h>
|
|
|
|
#include <tcl.h>
|
|
|
|
#include <tk.h>
|
2000-04-17 01:55:44 -07:00
|
|
|
#include <mlvalues.h>
|
|
|
|
#include <memory.h>
|
|
|
|
#include <alloc.h>
|
|
|
|
#include <callback.h>
|
1999-11-30 06:59:39 -08:00
|
|
|
#include "camltk.h"
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value camltk_getvar(value var)
|
1999-11-30 06:59:39 -08:00
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
char *stable_var = NULL;
|
|
|
|
CheckInit();
|
|
|
|
|
|
|
|
stable_var = string_to_c(var);
|
|
|
|
s = Tcl_GetVar(cltclinterp,stable_var,
|
|
|
|
TCL_GLOBAL_ONLY|TCL_LEAVE_ERR_MSG);
|
|
|
|
stat_free(stable_var);
|
|
|
|
|
|
|
|
if (s == NULL)
|
|
|
|
tk_error(cltclinterp->result);
|
|
|
|
else
|
2002-04-26 05:16:26 -07:00
|
|
|
return(tcl_string_to_caml(s));
|
1999-11-30 06:59:39 -08:00
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value camltk_setvar(value var, value contents)
|
1999-11-30 06:59:39 -08:00
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
char *stable_var = NULL;
|
2002-04-26 05:16:26 -07:00
|
|
|
char *utf_contents;
|
1999-11-30 06:59:39 -08:00
|
|
|
CheckInit();
|
|
|
|
|
|
|
|
/* SetVar makes a copy of the contents. */
|
|
|
|
/* In case we have write traces in Caml, it's better to make sure that
|
|
|
|
var doesn't move... */
|
|
|
|
stable_var = string_to_c(var);
|
2002-04-26 05:16:26 -07:00
|
|
|
utf_contents = caml_string_to_tcl(contents);
|
|
|
|
s = Tcl_SetVar(cltclinterp,stable_var, utf_contents,
|
1999-11-30 06:59:39 -08:00
|
|
|
TCL_GLOBAL_ONLY|TCL_LEAVE_ERR_MSG);
|
|
|
|
stat_free(stable_var);
|
2002-04-26 05:16:26 -07:00
|
|
|
if( s == utf_contents ){
|
|
|
|
tk_error("camltk_setvar: Tcl_SetVar returned strange result. Call the author of mlTk!");
|
|
|
|
}
|
|
|
|
stat_free(utf_contents);
|
1999-11-30 06:59:39 -08:00
|
|
|
|
|
|
|
if (s == NULL)
|
|
|
|
tk_error(cltclinterp->result);
|
|
|
|
else
|
|
|
|
return(Val_unit);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* The appropriate type is
|
|
|
|
typedef char *(Tcl_VarTraceProc) _ANSI_ARGS_((ClientData clientData,
|
|
|
|
Tcl_Interp *interp, char *part1, char *part2, int flags));
|
|
|
|
*/
|
2002-04-26 05:16:26 -07:00
|
|
|
static char * tracevar(clientdata, interp, name1, name2, flags)
|
|
|
|
ClientData clientdata;
|
|
|
|
Tcl_Interp *interp; /* Interpreter containing variable. */
|
|
|
|
char *name1; /* Name of variable. */
|
|
|
|
char *name2; /* Second part of variable name. */
|
|
|
|
int flags; /* Information about what happened. */
|
1999-11-30 06:59:39 -08:00
|
|
|
{
|
|
|
|
Tcl_UntraceVar2(interp, name1, name2,
|
|
|
|
TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
|
|
|
|
tracevar, clientdata);
|
|
|
|
callback2(*handler_code,Val_int(clientdata),Val_unit);
|
|
|
|
return (char *)NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sets up a callback upon modification of a variable */
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value camltk_trace_var(value var, value cbid)
|
1999-11-30 06:59:39 -08:00
|
|
|
{
|
|
|
|
char *cvar = NULL;
|
|
|
|
|
|
|
|
CheckInit();
|
|
|
|
/* Make a copy of var, since Tcl will modify it in place, and we
|
|
|
|
* don't trust that much what it will do here
|
|
|
|
*/
|
|
|
|
cvar = string_to_c(var);
|
|
|
|
if (Tcl_TraceVar(cltclinterp, cvar,
|
|
|
|
TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
|
|
|
|
tracevar,
|
|
|
|
(ClientData) (Long_val(cbid)))
|
|
|
|
!= TCL_OK) {
|
|
|
|
stat_free(cvar);
|
|
|
|
tk_error(cltclinterp->result);
|
|
|
|
};
|
|
|
|
stat_free(cvar);
|
|
|
|
return Val_unit;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value camltk_untrace_var(value var, value cbid)
|
1999-11-30 06:59:39 -08:00
|
|
|
{
|
|
|
|
char *cvar = NULL;
|
|
|
|
|
|
|
|
CheckInit();
|
|
|
|
/* Make a copy of var, since Tcl will modify it in place, and we
|
|
|
|
* don't trust that much what it will do here
|
|
|
|
*/
|
|
|
|
cvar = string_to_c(var);
|
|
|
|
Tcl_UntraceVar(cltclinterp, cvar,
|
|
|
|
TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
|
|
|
|
tracevar,
|
|
|
|
(ClientData) (Long_val(cbid)));
|
|
|
|
stat_free(cvar);
|
|
|
|
return Val_unit;
|
|
|
|
}
|