using existing tcl c procedures in tcl script
How to use Tcl_ParseCommand or list of Tcl C procedures already available under "http://tmml.sourceforge.net开发者_JAVA百科/doc/tcl/". Do i need to write wrapper c procedure and init procedure for each of these command?
-Prasath
The easiest way to access the parser commands Tcl_ParseCommand etc. might be the tclparser extension (e.g. the (orphaned) tclparser package in Debian/Ubuntu or build yourself from the sources http://wiki.tcl.tk/1660).
If you need to handle other Tcl C-library commands as well, you should have a look at how to write a Tcl extension in general. A lot of examples and advice for that can be found at: http://wiki.tcl.tk/1191
Yes, you do need to write a wrapper C-implemented command to use those. However, there are a number of neat tools to make that easier. In particular, critcl is interesting choice, which you might use like this (very noddy example):
package require critcl
critcl::cproc foo {Tcl_Interp* interp Tcl_Obj* argObj} ok {
int nObjs;
Tcl_Obj **objs;
if (Tcl_ListObjGetElements(interp, argObj, &nObjs, &objs) != TCL_OK)
return TCL_ERROR;
if (nObjs != 2) {
Tcl_AppendResult(interp, "need list of length 2", NULL);
return TCL_ERROR;
}
Tcl_SetObjResult(interp, Tcl_NewBooleanObj(
!strcmp(Tcl_GetString(objs[0]), Tcl_GetString(objs[1]))));
return TCL_OK;
}
puts [foo {a a}]
The useful documentation is on the Tcler's Wiki.
Note that for Tcl_ParseCommand
in particular, you're better off using the tclparser package as schlenk mentions. That's because the result of that function (as an out parameter) is significantly non-trivial; it needs quite a lot of converting to turn into the sort of thing that works as a Tcl value.
精彩评论