For Programmers: Free Programming Magazines  


Home > Archive > Tcl > February 2005 > redirect stdout problem









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author redirect stdout problem
Dan

2005-02-25, 3:59 am

I am trying to redirect stdout from the TCL command line such that
output from system commands like "ls" are redirected to an internal C
function. I based my code on the tkConsole style code which reroutes
stdout to a tk widget. I think it should work, but when I put in a
system command (like ls), I get the message:

channel "console1" wasn't opened for writing

After trying several variations on the code, I'm quite stuck. Any
suggestions would be greatly appreciated.

Here are the important sections of the code:

First I define a channel type:
static Tcl_ChannelType consoleChannelType = {
"console", /* Type name. */
NULL, /* version */
ConsoleClose, /* Close proc. */
ConsoleInput, /* Input proc. */
ConsoleOutput, /* Output proc. */
NULL, /* S proc. */
NULL, /* Set option proc. */
NULL, /* Get option proc. */
ConsoleWatch, /* Watch for events on console. */
ConsoleHandle, /* Get a handle from the device. */
NULL, /* Close proc. if device supports
closing r/w independently */
NULL /* Set blocking mode for the raw
channel. */
};


To redirect stdout:

void cliRedirectOutput(Tcl_Interp *interp)

{
Tcl_Channel consoleChannel;
consoleChannel = Tcl_CreateChannel(&consoleChannelType, "console1",
(ClientData) TCL_STDOUT,
TCL_WRITABLE);
if (consoleChannel != NULL) {
Tcl_SetChannelOption(NULL, consoleChannel, "-translation",
"lf");
Tcl_SetChannelOption(NULL, consoleChannel, "-buffering",
"none");
Tcl_SetChannelOption(NULL, consoleChannel,"-blocking", "off");
Tcl_SetChannelOption(NULL, consoleChannel,"-encoding",
"utf-8");
Tcl_SetChannelOption(NULL, consoleChannel,"-drawing", "raw");
}
Tcl_SetStdChannel(consoleChannel, TCL_STDOUT);
return;
}

I expect an "ls" command to call the ConsoleOutput function, which
does nothing but return toWrite at this stage (eventually I would pass
the contents of buf to another function):

static int ConsoleOutput(ClientData instanceData, char* buf, int
toWrite, int *errorCode)
{
*errorCode = 0;
Tcl_SetErrno(0);
return toWrite;
}

Dan

2005-02-25, 9:00 pm

OK, to explain a bit further....
First, this application is in Linux and will never move to the Windows
world.

What we have is an EDA application with a command-line interface and a
GUI interface. We intercept our custom commands associated with
operating the tool.
We assume any commmands other than ours are system level commands like
"ls" or "cd" and let them pass on to the operating system (our
customers expect this behaviour as it is standard).

For the command-line interface, this works fine because the command
prompt is in the same xterm used to start the program. If I do "ls",
the text shows up in the xterm.

The problem is with the GUI interface. We are using a Java GUI which
interfaces to the C code program using JNI. I can route text from the
C code to a text pane in the Java GUI for our custom commands because
we have JNI routines set up for that. But commands like "ls" cause the
listing to occur in the original command shell, totally bypassing the
text widget in the Java GUI. If I can route stdout to a C function,
then I can pass the text on to the Java GUI. I think the approach
above should work, but so far it does not...

Steve Bold

2005-02-25, 9:00 pm

You may wish to take a look at the 'unknown' command, usually implemented in init.tcl.
In my installation it contains code:

set redir ""
if {[string equal [info commands console] ""]} {
set redir ">&@stdout <@stdin"
}

Your fix might be as simple as adding a dummy 'console' command to make Tcl think it's dealing with a Tk
based wish console.

Steve.

"Dan" <dblanks@rio-da.com> wrote in message news:1109361400.521453.19110@g14g2000cwa.googlegroups.com...
> OK, to explain a bit further....
> First, this application is in Linux and will never move to the Windows
> world.
>
> What we have is an EDA application with a command-line interface and a
> GUI interface. We intercept our custom commands associated with
> operating the tool.
> We assume any commmands other than ours are system level commands like
> "ls" or "cd" and let them pass on to the operating system (our
> customers expect this behaviour as it is standard).
>
> For the command-line interface, this works fine because the command
> prompt is in the same xterm used to start the program. If I do "ls",
> the text shows up in the xterm.
>
> The problem is with the GUI interface. We are using a Java GUI which
> interfaces to the C code program using JNI. I can route text from the
> C code to a text pane in the Java GUI for our custom commands because
> we have JNI routines set up for that. But commands like "ls" cause the
> listing to occur in the original command shell, totally bypassing the
> text widget in the Java GUI. If I can route stdout to a C function,
> then I can pass the text on to the Java GUI. I think the approach
> above should work, but so far it does not...
>



Dan

2005-02-26, 3:59 am

OK I'll give that a try on Monday...

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com