Standard Channels

Standard Channels




Tcl_StandardChannels − How the Tcl library deals with the standard channels ______________


This page explains the initialization and use of standard channels in the Tcl library.

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.

The public API procedures dealing directly with standard channels are Tcl_GetStdChannel and Tcl_SetStdChannel. Additional public APIs to consider are Tcl_RegisterChannel, Tcl_CreateChannel and Tcl_GetChannel.


Standard channels are initialized by the Tcl library in three cases: when explicitly requested, when implicitly required before returning channel information, or when implicitly required during registration of a new channel.

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.


Once a Tcl standard channel is initialized through one of the methods above, closing this Tcl standard channel will cause the next call to 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.


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.

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.


Tcl_CreateChannel(3), Tcl_RegisterChannel(3), Tcl_GetChannel(3), Tcl_GetStdChannel(3), Tcl_SetStdChannel(3), Tk_InitConsoleChannels(3), tclsh(1), wish(1), Tcl_Main(3), Tk_MainEx(3)


standard channels


Personal Opportunity - Free software gives you access to billions of dollars of software at no cost. Use this software for your business, personal use or to develop a profitable skill. Access to source code provides access to a level of capabilities/information that companies protect though copyrights. Open source is a core component of the Internet and it is available to you. Leverage the billions of dollars in resources and capabilities to build a career, establish a business or change the world. The potential is endless for those who understand the opportunity.

Business Opportunity - Goldman Sachs, IBM and countless large corporations are leveraging open source to reduce costs, develop products and increase their bottom lines. Learn what these companies know about open source and how open source can give you the advantage.

Free Software

Free Software provides computer programs and capabilities at no cost but more importantly, it provides the freedom to run, edit, contribute to, and share the software. The importance of free software is a matter of access, not price. Software at no cost is a benefit but ownership rights to the software and source code is far more significant.

Free Office Software - The Libre Office suite provides top desktop productivity tools for free. This includes, a word processor, spreadsheet, presentation engine, drawing and flowcharting, database and math applications. Libre Office is available for Linux or Windows.

Free Books

The Free Books Library is a collection of thousands of the most popular public domain books in an online readable format. The collection includes great classical literature and more recent works where the U.S. copyright has expired. These books are yours to read and use without restrictions.

Source Code - Want to change a program or know how it works? Open Source provides the source code for its programs so that anyone can use, modify or learn how to write those programs themselves. Visit the GNU source code repositories to download the source.


Study at Harvard, Stanford or MIT - Open edX provides free online courses from Harvard, MIT, Columbia, UC Berkeley and other top Universities. Hundreds of courses for almost all major subjects and course levels. Open edx also offers some paid courses and selected certifications.

Linux Manual Pages - A man or manual page is a form of software documentation found on Linux/Unix operating systems. Topics covered include computer programs (including library and system calls), formal standards and conventions, and even abstract concepts.