<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://community.activestate.com" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
 <title>ActiveState Community Site - Trying to intercept stdout and stdin while a script is running - Comments</title>
 <link>http://community.activestate.com/forum-topic/trying-intercept-stdout-and-stdin-while-script-running</link>
 <description>Comments for &quot;Trying to intercept stdout and stdin while a script is running&quot;</description>
 <language>en</language>
<item>
 <title>channel encoding output</title>
 <link>http://community.activestate.com/forum-topic/trying-intercept-stdout-and-stdin-while-script-running#comment-3888</link>
 <description>&lt;p&gt;It looks like you 8.5.0 output is Tcl_UniChar-sized data or somehow output as UCS-2 (unicode).  You are getting your regular ASCII data in short (2-byte) sized output.&lt;/p&gt;
</description>
 <pubDate>Mon, 14 Jan 2008 14:06:37 -0600</pubDate>
 <dc:creator>jeffh</dc:creator>
 <guid isPermaLink="false">comment 3888 at http://community.activestate.com</guid>
</item>
<item>
 <title>Trying to intercept stdout and stdin while a script is running</title>
 <link>http://community.activestate.com/forum-topic/trying-intercept-stdout-and-stdin-while-script-running</link>
 <description>&lt;p&gt;I am creating an application in C that is designed to run various Tcl scripts, but I need to intercept the output to the console as it is running.  I also need to automatically fill any requests for input automatically (depending on options) or have the user enter input from the console window.  I began to develop my application testing with Tcl 8.4.16, but have run into problems when using Tcl 8.5.0.  I am not sure if I am using the appropriate methods to do this, but I have not been able to find many examples on this type of application.&lt;/p&gt;
&lt;p&gt;Is there anything I am doing wrong or should be doing differently below?&lt;/p&gt;
&lt;p&gt;I have simplified my application to the following (this works fine in Tcl 8.4.16):&lt;/p&gt;
&lt;pre class=&quot;geshifilter&quot;&gt;Tcl_DriverInputProc * inProc;
Tcl_DriverOutputProc * outProc;

int OutputProc(ClientData instanceData, const char * buf, int toWrite, int * errorCodePtr)
{
	//do something here
	return outProc(instanceData, buf, toWrite, errorCodePtr);
}

int InputProc(ClientData instanceData, char *buf, int bufSize, int *errorCodePtr)
{
	//do something here
	return inProc(instanceData, buf, bufSize, errorCodePtr);
}

int _tmain(int argc, char* argv[])
{

	Tcl_FindExecutable(argv[0]);
	Tcl_Interp * interp = Tcl_CreateInterp();

	//get stdin channel
	Tcl_Channel stdinChan = Tcl_GetStdChannel(TCL_STDIN);
	Tcl_ChannelType * chanType = Tcl_GetChannelType(stdinChan);
	//save the old input procedure so I can use it later
	inProc = chanType-&amp;gt;inputProc;
	//assign my input procedure to it
	chanType-&amp;gt;inputProc = &amp;InputProc;

	//get stdout channel
	Tcl_Channel stdoutChan = Tcl_GetStdChannel(TCL_STDOUT);
	Tcl_ChannelType * stdoutType = Tcl_GetChannelType(stdoutChan);
	//save the old output procedure so I can use it later
	outProc = stdoutType-&amp;gt;outputProc;
	//assign my output procedure to it
	stdoutType-&amp;gt;outputProc = &amp;OutputProc;

	//evaluate a script
	int result = Tcl_Eval(interp, &quot;gets stdin x&quot;);

	//get the variable out
	const char * x  = Tcl_GetVar(interp, &quot;x&quot;, 0);

	//output to console
	result = Tcl_Eval(interp, &quot;puts $x&quot;);

	return 0;
}&lt;/pre&gt;&lt;p&gt;
This code by itself works fine in both versions of Tcl.  However, there is a slightly different behavior when I&#039;m using 8.4.16 vs. 8.5.0.  If I set a breakpoint and look at the buf variable in the OutputProc, I get the following differences when I enter &quot;abc&quot; from the console:&lt;/p&gt;
&lt;p&gt;8.4.16:  buf = 61 62 63 0d 0a   (&quot;abc\r\n&quot;)&lt;br /&gt;
8.5.0:   buf = 61 00 62 00 63 00 0d 00 0a 00&lt;/p&gt;
&lt;p&gt;Does anyone know what these extra characters are?&lt;/p&gt;
&lt;p&gt;Similarly, when I examine the contents of buf on the InputProc after getting values, I notice the same things.&lt;/p&gt;
&lt;p&gt;I&#039;m open to suggestions.&lt;/p&gt;
</description>
 <comments>http://community.activestate.com/forum-topic/trying-intercept-stdout-and-stdin-while-script-running#comments</comments>
 <category domain="http://community.activestate.com/forums/activetcl-support-0">ActiveTcl support</category>
 <category domain="http://community.activestate.com/os/windows">Windows</category>
 <pubDate>Mon, 14 Jan 2008 12:00:36 -0600</pubDate>
 <dc:creator>apiscitello</dc:creator>
 <guid isPermaLink="false">1818 at http://community.activestate.com</guid>
</item>
</channel>
</rss>
