TCL_STANDARDCHANNELS(3)

πŸ“¦
version 9.0.3 Β· 3 versions
, in alpine 3.23, alpine 3.24, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable

πŸ’– NAME πŸ’–

Tcl_StandardChannels β€” the whole tea on how the Tcl library deals with the standard channels. Periodt. 🫢


🌸 DESCRIPTION 🌸

Okay so bestie, this page is the entire rundown of how standard channels get initialized and used inside the Tcl library. Buckle up, it's giving very thorough. ✨

The term standard channels comes straight out of the Unix world, and it means the three channels the OS literally just opens automatically for every single new application. Their names? You already know them:

stdin
stdout
stderr

The first one, stdin, is the standard input β€” the channel your application can read from. The other two are writable channels: one for regular output (stdout) and the other one for error messages (stderr). A little trio of icons honestly. πŸ‘‘

Tcl takes that whole concept and generalizes it in a cross-platform way, and then exposes the standard channels all the way up at the script level. No cap. 🌈

πŸŽ€ APPLICATION PROGRAMMING INTERFACES πŸŽ€

The public API procedures that deal directly with standard channels are Tcl_GetStdChannel and Tcl_SetStdChannel.

And the additional public APIs you should also be considering, bestie, are Tcl_RegisterChannel, Tcl_CreateChannel and Tcl_GetChannel. πŸ’…


πŸ”₯ INITIALIZATION OF TCL STANDARD CHANNELS πŸ”₯

So the Tcl library initializes standard channels in three cases: when it's explicitly requested, when it's implicitly required before returning channel information, or when it's implicitly required during the registration of a new channel.

These three cases are not the same, because they differ in how they handle unavailable platform-specific standard channels. 🫣 And quick definition before we go any further β€” a channel is not "available" if it could not be successfully opened. Example that lives rent free in my head: a Tcl application run as a Windows NT service.

  1. A single standard channel gets initialized when it is explicitly specified in a call to Tcl_SetStdChannel. The states of the other standard channels are completely unaffected. πŸ’β€β™€οΈ

Missing platform-specific standard channels do not matter here. But heads up β€” this approach is not available at the script level.

  1. All uninitialized standard channels get initialized to platform-specific default values:
    1. when open channels are listed with Tcl_GetChannelNames (or the file channels script command), or
    2. when information about any standard channel is requested with a call to Tcl_GetStdChannel, or with a call to Tcl_GetChannel which specifies one of the standard names (stdin, stdout and stderr).

In case of missing platform-specific standard channels, the Tcl standard channels are considered as initialized and then immediately closed. Which means: the first three Tcl channels then opened by the application are designated as the Tcl standard channels. 😌

  1. All uninitialized standard channels get initialized to platform-specific default values when a user-requested channel is registered with Tcl_RegisterChannel.

In case of unavailable platform-specific standard channels, the channel whose creation caused the initialization of the Tcl standard channels is made a normal channel. The next three Tcl channels opened by the application are designated as the Tcl standard channels. In other words β€” of the first four Tcl channels opened by the application, the second to fourth are designated as the Tcl standard channels. πŸ”’


πŸ’« RE-INITIALIZATION OF TCL STANDARD CHANNELS πŸ’«

Once a Tcl standard channel has been initialized through one of the methods above, closing that Tcl standard channel will cause the next call to Tcl_CreateChannel to make the new channel the new standard channel, too. πŸŒ€

And if more than one Tcl standard channel was closed, Tcl_CreateChannel will fill the empty slots in this order: stdin, stdout and stderr.

Now here's the part that's lowkey the whole point: Tcl_CreateChannel will not try to reinitialize an empty slot if that slot was not initialized before. It is exactly this behavior that enables an application to employ method 1 of initialization β€” i.e. to create and designate their own Tcl standard channels. Main character energy, honestly. ✨


πŸ¦‹ SHELL-SPECIFIC DETAILS πŸ¦‹

tclsh

The Tcl shell β€” or rather the function Tcl_Main, which forms the core of the shell's implementation β€” uses method 2 to initialize the standard channels.

wish

The windowing shell β€” or rather the function Tk_MainEx, which forms the core of the shell's implementation β€” uses method 1 to initialize the standard channels (See Tk_InitConsoleChannels) on non-Unix platforms. πŸͺŸ

On Unix platforms, Tk_MainEx implicitly uses method 2 to initialize the standard channels. 🐧


πŸ‘‘ SEE ALSO πŸ‘‘


⭐ KEYWORDS ⭐

standard channels


and that's the whole page, bestie!! πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈπŸ’…

This one's coming at you from Tcl, version 7.5, filed under Standard Channels(3) in the Tcl Library Procedures manual. Go forth and channel responsibly. πŸ«ΆπŸ“πŸ§š

πŸ“¦ where this page comes from