in src/generic/varchannel.c [102:186]
int varchannelOutputProc(ClientData clientData,
TCLCONST char *buf, int toWrite, int *errorCodePtr)
{
VarChannel *varChannel = NULL;
int res = -1;
int destLen = 0;
char *dest = NULL;
int bytesConv = 0;
Tcl_Obj *tmp = NULL;
int isNew = 0;
Tcl_Obj *var = NULL;
/* sanity */
if ((clientData == NULL) || (buf == NULL))
return res;
/* get interna */
varChannel = (VarChannel *) clientData;
if (varChannel->varName == NULL)
return -1;
/* --------------------------------------------------------------------------
* get var
* ----------------------------------------------------------------------- */
var = Web_GetOrCreateGlobalVar(varChannel->interp,
varChannel->varName, &isNew);
if (var == NULL)
return -1;
if (isNew) {
varChannel->readCursor = 0;
Tcl_DecrRefCount(var);
}
/* the conversion
*
* what we get from Tcl is the string in the encoding of this channel.
* Since we are writing to a variable, we need to back-translate
* the string to the encding currently in use by Tcl.
*/
/* convert to UTF */
destLen = (toWrite + 1) * TCL_UTF_MAX + 1;
dest = Tcl_Alloc(destLen);
if (dest == NULL)
return -1;
res = Tcl_ExternalToUtf(NULL, NULL, buf, toWrite, 0, NULL, dest, destLen,
NULL, &bytesConv, NULL);
if (res != TCL_OK) {
Tcl_Free(dest);
return -1;
}
/* now put UTF into Tcl_object */
tmp = Tcl_NewStringObj(dest, bytesConv);
if (tmp == NULL) {
Tcl_Free(dest);
return -1;
}
Tcl_IncrRefCount(tmp);
/* and append it to the global var */
var = Tcl_ObjSetVar2(varChannel->interp,
varChannel->varName,
NULL,
tmp,
TCL_GLOBAL_ONLY | TCL_APPEND_VALUE |
TCL_LEAVE_ERR_MSG);
/* cleanup */
Tcl_Free(dest);
Tcl_DecrRefCount(tmp);
if (var == NULL)
return -1;
return toWrite;
}