0

How to use Tcl_ParseCommand or list of Tcl C procedures already available under "http://tmml.sourceforge.net/doc/tcl/". Do i need to write wrapper c procedure and init procedure for each of these command?

-Prasath

Prasath
  • 595
  • 5
  • 11
  • Your question is a bit vague. To use the Tcl C-API you either build a Tcl extension package or embed a Tcl interpreter into your own C program (have a look at the sources for the tclsh shell). – schlenk Apr 27 '11 at 21:41
  • My purpose is to use the tcl c procedures in my tcl script. could you let me me know how to create an Tcl extension package for these existing c procedure available in Tcl library. i'm not looking to create my own command in c. – Prasath Apr 28 '11 at 03:17

2 Answers2

0

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

schlenk
  • 7,002
  • 1
  • 25
  • 29
  • Thats great. Thanks for the suggestion. Since Tcl c libraries like Tcl_ParseCommand etc are inbuilt, i thought there must a gereric way already available to get this working in tcl scripts without a need to write an extenstion for each c api. It seems i've to extend these c libraries as if i do for new c procedures. (am i correct?). – Prasath Apr 28 '11 at 09:14
  • 1
    Most of the Tcl c api just does the same as simple Tcl commands, so in a way you have direct access to most of them, but there are some parts of the c api that are less useful or needed in the script layer. For example Tcl_Eval is just the same as eval or source or namespace eval or interp eval or one of the other ways to eval a script. In fact there are very few functions not available in a normal script, mostly initialization, memory allocation and other utilities, or deep internals like registering new event sources to the notifier. All the rest is just a Tcl command in disguise. – schlenk Apr 28 '11 at 09:48
  • hmm. ok. the issue is writing c extension for each tcl c libraries is cumbersome for me as i've little knowledge in c. Also its difficult for me to understand how to passs required arguments to the tcl c api from the wrappers and then convert the return values of these c api's back to tcl script. – Prasath Apr 28 '11 at 14:47
0

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.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215