I am trying to put together a Tcl extension that, among other things,
constructs and calls a Tcl proc via Tcl_DString and Tcl_VarEval. The
procedure is supposed to take a single parameter.
The very odd thing I'm seeing is that the procedure is called correctly
if the parameter is greater than a certain length--about nine or ten
characters, if I am observing correctly. If the parameter is smaller
than this, it is omitted entirely, and the interp returns an error of
"wrong # args".
Here are the relevant parts of my C code:
long size = AEGetDescDataSize(&directParameter);
Tcl_DStringInit(&as_arg);
if (size) {
UInt8 paramstring[size];
AEGetDescData(&directParameter, paramstring, size);
AEDisposeDesc(&directParameter);
/*Need to convert paramstring to something Tcl can handle
gracefully. Direct conversion of AE params to char does not seem to work
well, produces garbage.*/
CFStringRef arg = CFStringCreateWithBytes(0, paramstring,
sizeof(paramstring), kCFStringEncodingUTF8, false);
int len;
len = CFStringGetMaximumSizeForEncoding(CFStringGetLength(arg),
kCFStringEncodingUTF8);
Tcl_DStringSetLength(&as_arg, len);
Tcl_DStringValue(&as_arg) = CFStringGetCStringPtr(arg,
kCFStringEncodingUTF8);
CFRelease(arg);
} else {
Tcl_DStringValue(&as_arg) = " ";
}
---
char *applescript_tcl_cmd;
/* Convert the string reference into a C string. */
applescript_tcl_cmd = CFStringGetCStringPtr(cfcmd,
kCFStringEncodingUTF8);
/* Eval the Tcl command. */
Tcl_VarEval(AS_Interp, applescript_tcl_cmd, " ",
Tcl_DStringValue(&as_arg), NULL);
---
In the code above, the CFStringRef bits are just conversions between
various string formats on the Mac on the way to passing Tcl a buffer it
can understand.
If I pass a value of "Archive::Tar" to Tcl_DstringValue(&as_arg), my
code runs correctly. If I pass a smaller string, such as "open," I get
the "wrong # args" error.
I would understand if the arg were truncated in some way that I would
need to increase the size of the string buffer, but it is baffling to me
why it is being cut out altogether.
Is anyone familiar with situations where args are not truncated but
omitted together? I realize Tcl_VarEval is deprecated but it is perfect
for this use case. If someone can suggest a different way to call a Tcl
proc with a single arg from C, constructed with Tcl_DStrings, I am also
happy to try that out.
Advice is appreciated.
--Kevin
--
Kevin Walzer
Code by Kevin/Mobile Code by Kevin
http://www.codebykevin.com
http://www.wtmobilesoftware.com