Tcl8.6/Tk8.6 Documentation > Tcl C API, version 8.6.8 > StdChannels
Tcl/Tk Applications | Tcl Commands | Tk Commands | [incr Tcl] Package Commands | SQLite3 Package Commands | TDBC Package Commands | tdbc::mysql Package Commands | tdbc::odbc Package Commands | tdbc::postgres Package Commands | tdbc::sqlite3 Package Commands | Thread Package Commands | Tcl C API | Tk C API | [incr Tcl] Package C API | TDBC Package C API
- Tcl_StandardChannels — How the Tcl library deals with the standard channels
- APPLICATION PROGRAMMING INTERFACES
- INITIALIZATION OF TCL STANDARD CHANNELS
- RE-INITIALIZATION OF TCL STANDARD CHANNELS
- SHELL-SPECIFIC DETAILS
- SEE ALSO
The term standard channels comes out of the Unix world and refers to the three channels automatically opened by the OS for each new application. They are stdin, stdout and stderr. The first is the standard input an application can read from, the other two refer to writable channels, one for regular output and the other for error messages.
Tcl generalizes this concept in a cross-platform way and exposes standard channels to the script level.Tcl_GetStdChannel and Tcl_SetStdChannel. Additional public APIs to consider are Tcl_RegisterChannel, Tcl_CreateChannel and Tcl_GetChannel.
These cases differ in how they handle unavailable platform- specific standard channels. (A channel is not “available” if it could not be successfully opened; for example, in a Tcl application run as a Windows NT service.)
A single standard channel is initialized when it is explicitly
specified in a call to Tcl_SetStdChannel. The states of the
other standard channels are unaffected.
Missing platform-specific standard channels do not matter here. This approach is not available at the script level.
All uninitialized standard channels are initialized to
platform-specific default values:
- when open channels are listed with Tcl_GetChannelNames (or the file channels script command), or
- 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. This means that the first three Tcl channels then opened by the application are designated as the Tcl standard channels.
- All uninitialized standard channels are 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.Tcl_CreateChannel to make the new channel the new standard channel, too. If more than one Tcl standard channel was closed Tcl_CreateChannel will fill the empty slots in the order stdin, stdout and stderr.
Tcl_CreateChannel will not try to reinitialize an empty slot if that slot was not initialized before. It is this behavior which enables an application to employ method 1 of initialization, i.e. to create and designate their own Tcl standard channels.Tcl_Main, which forms the core of the shell's implementation) uses method 2 to initialize the standard channels. Tk_InitConsoleChannels) on non-Unix platforms. On Unix platforms, Tk_MainEx implicitly uses method 2 to initialize the standard channels. Tcl_CreateChannel, Tcl_RegisterChannel, Tcl_GetChannel, Tcl_GetStdChannel, Tcl_SetStdChannel, Tk_InitConsoleChannels, tclsh, wish, Tcl_Main, Tk_MainEx standard channels