Posted to tcl by de at Sat Apr 01 00:02:10 GMT 2023view pretty

#include <tcl.h>

#if defined(Tcl_Size)
#  define domLength Tcl_Size
#  define STUB_VERSION "8.6"
#else
#  define domLength int
#  define STUB_VERSION "9"
#endif

static int rdeCmd(
    void *clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[]
    )
{
    /* With:
       Tcl_Size len;
       or
       size_t len;
       this would work as expected. */
    domLength len;
    
    if (objc != 2) {
        Tcl_WrongNumArgs (interp, 1, objv, "string");
        return TCL_ERROR;
    }
    Tcl_GetStringFromObj (objv[1], &len);
    Tcl_SetObjResult (interp, Tcl_NewWideIntObj(len));
    return TCL_OK;
}

int Rde_Init(Tcl_Interp *interp) {
    if (Tcl_InitStubs(interp, STUB_VERSION, 0) == NULL) {
        return TCL_ERROR;
    }
    Tcl_CreateObjCommand(interp, "rde", rdeCmd, NULL, NULL);
    return TCL_OK;
}

/*

Build with a sh script as:
  
TCL=/usr/local/tcl/9

rm -f rde.o rde.so
gcc -DUSE_TCL_STUBS -I${TCL}/include -fPIC -o rde.o -c rde.c
gcc -L${TCL}/lib -shared -o rde.so rde.o -ltclstub9.0

Test script:

load ./rde.so
puts [rde [string repeat abc 1000000000]]

*/