Tcl_GetStdChannel(3)
β¨ NAME β¨
Tcl_GetStdChannel, Tcl_SetStdChannel β the procedures for grabbing the standard channels and swapping them out π
This is the Tcl Library Procedures page, bestie, and yes we're doing the whole thing π«Ά
πΈ SYNOPSIS πΈ
#include <tcl.h>
Tcl_Channel
Tcl_GetStdChannel(type)
Tcl_SetStdChannel(channel, type)
π ARGUMENTS π
int type(in) β the identifier for whichever standard channel you're trying to retrieve or mess with. It must be one ofTCL_STDIN,TCL_STDOUT, orTCL_STDERR. No other values, okay? Not negotiable π₯Tcl_Channel channel(in) β the channel that becomes the new value of the standard channel you picked.
π DESCRIPTION π
So Tcl has three special channels that all the I/O-ish commands fall back on when you don't name a channel yourself. Iconic behaviour, honestly.
- The standard input channel is named
stdinand it's whatreadandgetsuse. - The standard output channel is named
stdoutand it's whatputsuses. - The standard error channel is named
stderrand it's the one for reporting errors.
And on top of that? The standard channels get inherited by any child processes created with exec or open when there aren't any other redirections in play. The vibes pass down, periodt.
Okay but here's the tea: the standard channels are actually aliases for other normal channels. It's giving understudy energy. To find out which channel is currently playing the role, call Tcl_GetStdChannel with one of TCL_STDIN, TCL_STDOUT, or TCL_STDERR as the type. What you get back is either a valid channel or NULL β and NULL is a whole mood, it means nothing's there π
You can also cast a new channel in the role: call Tcl_SetStdChannel with your new channel β or NULL β in the channel argument, for the standard channel named by type. And if that channel later gets closed by a call to Tcl_Close, the matching standard channel automatically flips itself to NULL. Self-cleaning, we love to see it β¨
Now for the messy part π If you pass a non-NULLvalue forchanneltoTcl_SetStdChannel, then you should pass that same value toTcl_RegisterChannel, like this:
Tcl_RegisterChannel(NULL, channel);
This is a workaround for a misfeature in Tcl_SetStdChannel: it fails to do some reference counting housekeeping. And no, that misfeature cannot be corrected without contradicting the assumptions of some existing code that calls Tcl_SetStdChannel. So we live with it. Character development, I guess π
More lore: if Tcl_GetStdChannel gets called before Tcl_SetStdChannel, Tcl will construct a brand new channel to wrap the appropriate platform-specific standard file handle. But if Tcl_SetStdChannel is called before Tcl_GetStdChannel, then that default channel will not be created. Order matters, bestie, always has π
If one of the standard channels is set to NULL β whether by calling Tcl_SetStdChannel with a NULL channel argument, or by calling Tcl_Close on the channel β then the next call to Tcl_CreateChannel automatically sets that standard channel to the newly created channel. Chef's kiss π§
And if more than one standard channel is NULL? Then they get assigned in this exact order:
- standard input first
- standard output next
- standard error last
No queue-jumping π
Oh and if you want the full general treatise about standard channels and how the Tcl library behaves around them, go read Tcl_StandardChannels. It's the deep lore π¦
π SEE ALSO π
Tcl_Close(3), Tcl_CreateChannel(3), Tcl_Main(3), tclsh(1)
β KEYWORDS β
standard channel, standard input, standard output, standard error
and that's the whole page, bestie π This was the Tcl_GetStdChannel(3) page from Tcl 7.5 β mwah, go forth and channel responsibly ππ«Ά
π¦ where this page comes from
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- nixpkgs unstable: tcl 9.1.0
- Copyright (c) 1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms