SYNOPSIS
#include <tcl.h>
Tcl_Channel Tcl_CreateChannel(typePtr, channelName, instanceData, mask)
ClientData Tcl_GetChannelInstanceData(channel)
Tcl_ChannelType * Tcl_GetChannelType(channel)
char * Tcl_GetChannelName(channel)
int Tcl_GetChannelHandle(channel, direction, handlePtr)
int Tcl_GetChannelBufferSize(channel)
Tcl_SetChannelBufferSize(channel, size)
Tcl_NotifyChannel(channel, mask)
int Tcl_BadChannelOption(interp, optionName, optionList)
char * Tcl_ChannelName(typePtr)
Tcl_ChannelTypeVersion Tcl_ChannelVersion(typePtr)
Tcl_DriverBlockModeProc * Tcl_ChannelBlockModeProc(typePtr)
Tcl_DriverCloseProc * Tcl_ChannelCloseProc(typePtr)
Tcl_DriverClose2Proc * Tcl_ChannelClose2Proc(typePtr)
Tcl_DriverInputProc * Tcl_ChannelInputProc(typePtr)
Tcl_DriverOutputProc * Tcl_ChannelOutputProc(typePtr)
Tcl_DriverSeekProc * Tcl_ChannelSeekProc(typePtr)
Tcl_DriverSetOptionProc * Tcl_ChannelSetOptionProc(typePtr)
Tcl_DriverGetOptionProc * Tcl_ChannelGetOptionProc(typePtr)
Tcl_DriverWatchProc * Tcl_ChannelWatchProc(typePtr)
Tcl_DriverGetHandleProc * Tcl_ChannelGetHandleProc(typePtr)
Tcl_DriverFlushProc * Tcl_ChannelFlushProc(typePtr)
Tcl_DriverHandlerProc * Tcl_ChannelHandlerProc(typePtr)
ARGUMENTS
- Tcl_ChannelType *typePtr (in)
-
Points to a structure containing the addresses of procedures that can be called to perform I/O and other functions on the channel.
- char *channelName (in)
-
The name of this channel, such as file3; must not be in use by any other channel. Can be NULL, in which case the channel is created without a name.
- ClientData instanceData (in)
-
Arbitrary one-word value to be associated with this channel. This value is passed to procedures in typePtr when they are invoked.
- int mask (in)
-
OR-ed combination of TCL_READABLE and TCL_WRITABLE to indicate whether a channel is readable and writable.
- Tcl_Channel channel (in)
-
The channel to operate on.
- int direction (in)
-
TCL_READABLE means the input handle is wanted; TCL_WRITABLE means the output handle is wanted.
- ClientData *handlePtr (out)
-
Points to the location where the desired OS-specific handle should be stored.
- Tcl_EolTranslation transMode (in)
-
The translation mode; one of the constants TCL_TRANSLATE_AUTO, TCL_TRANSLATE_CR, TCL_TRANSLATE_LF and TCL_TRANSLATE_CRLF.
- int size (in)
-
The size, in bytes, of buffers to allocate in this channel.
- int mask (in)
-
An OR-ed combination of TCL_READABLE, TCL_WRITABLE and TCL_EXCEPTION that indicates events that have occurred on this channel.
- Tcl_Interp *interp (in)
-
Current interpreter. (can be NULL)
- char *optionName (in)
-
Name of the invalid option.
- char *optionList (in)
-
Specific options list (space separated words, without "-") to append to the standard generic options list. Can be NULL for generic options error message only.
DESCRIPTION
Tcl uses a two-layered channel architecture. It provides a generic upper
layer to enable C and Tcl programs to perform input and output using the
same APIs for a variety of files, devices, sockets etc. The generic C APIs
are described in the reference page for
The lower layer provides type-specific channel drivers for each type of device supported on each platform. This reference page describes the C APIs used to communicate between the generic layer and the type-specific channel drivers. It also explains how new types of channels can be added by providing new channel drivers.
Channel drivers consist of a number of components: First, each channel driver provides a Tcl_ChannelType structure containing pointers to functions implementing the various operations used by the generic layer to communicate with the channel driver. The Tcl_ChannelType structure and the functions referenced by it are described in the section TCL_CHANNELTYPE, below.
Second, channel drivers usually provide a Tcl command to create instances of that type of channel. For example, the Tcl open command creates channels that use the file and command channel drivers, and the Tcl socket command creates channels that use TCP sockets for network communication.
Third, a channel driver optionally provides a C function to open
channel instances of that type. For example,
To add a new type of channel you must implement a C API or a Tcl command
that opens a channel by invoking
TCL_CHANNELTYPE
A channel driver provides a Tcl_ChannelType structure that contains pointers to functions that implement the various operations on a channel; these operations are invoked as needed by the generic layer. The structure was versioned starting in Tcl 8.3.2/8.4 to correct a problem with stacked channel drivers. See the OLD_CHANNEL section below for details about the old structure.
The Tcl_ChannelType structure contains the following fields:
typedef struct Tcl_ChannelType { char *typeName; Tcl_ChannelTypeVersion version; Tcl_DriverCloseProc *closeProc; Tcl_DriverInputProc *inputProc; Tcl_DriverOutputProc *outputProc; Tcl_DriverSeekProc *seekProc; Tcl_DriverSetOptionProc *setOptionProc; Tcl_DriverGetOptionProc *getOptionProc; Tcl_DriverWatchProc *watchProc; Tcl_DriverGetHandleProc *getHandleProc; Tcl_DriverClose2Proc *close2Proc; Tcl_DriverBlockModeProc *blockModeProc; Tcl_DriverFlushProc *flushProc; Tcl_DriverHandlerProc *handlerProc; } Tcl_ChannelType;
The driver must provide implementations for all functions except blockModeProc, seekProc, setOptionProc, getOptionProc, and close2Proc, which may be specified as NULL. Other functions that can not be implemented for this type of device should return EINVAL when invoked to indicate that they are not implemented, except in the case of flushProc and handlerProc, which should specified as NULL if not otherwise defined.
The user should only use the above structure for Tcl_ChannelType
instantiation. When referencing fields in a Tcl_ChannelType
structure, the following functions should be used to obtain the values:
The change to the structures was made in such a way that standard channel types are binary compatible. However, channel types that use stacked channels (ie: TLS, Trf) have new versions to correspond to the above change since the previous code for stacked channels had problems.
TYPENAME
The typeName field contains a null-terminated string that identifies the type of the device implemented by this driver, for example, file or socket.
This value can be retried with
VERSION
The version field should be set to TCL_CHANNEL_VERSION_2. If it is not set to this value TCL_CHANNEL_VERSION_2, then this Tcl_ChannelType is assumed to have the older structure. See OLD_CHANNEL for more details. While Tcl will recognize and function with either structure, stacked channels must be of the newer style to function correctly.
This value can be retried with
BLOCKMODEPROC
The blockModeProc field contains the address of a function called by the generic layer to set blocking and nonblocking mode on the device. BlockModeProc should match the following prototype:
typedef int Tcl_DriverBlockModeProc( ClientData instanceData, int mode);
The instanceData is the same as the value passed to
If the operation is successful, the function can modify the supplied instanceData to record that the channel entered blocking or nonblocking mode and to implement the blocking or nonblocking behavior. For some device types, the blocking and nonblocking behavior can be implemented by the underlying operating system; for other device types, the behavior must be emulated in the channel driver.
This value can be retried with
CLOSEPROC AND CLOSE2PROC
The closeProc field contains the address of a function called by the generic layer to clean up driver-related information when the channel is closed. CloseProc must match the following prototype:
typedef int Tcl_DriverCloseProc( ClientData instanceData, Tcl_Interp *interp);
The instanceData argument is the same as the value provided to
Alternatively, channels that support closing the read and write sides independently may set closeProc to TCL_CLOSE2PROC and set close2Proc to the address of a function that matches the following prototype:
typedef int Tcl_DriverClose2Proc( ClientData instanceData, Tcl_Interp *interp, int flags);
The close2Proc will be called with flags set to an OR'ed combination of TCL_CLOSE_READ or TCL_CLOSE_WRITE to indicate that the driver should close the read and/or write side of the channel. The channel driver may be invoked to perform additional operations on the channel after close2Proc is called to close one or both sides of the channel. If flags is 0 (zero), the driver should close the channel in the manner described above for closeProc. No further operations will be invoked on this instance after close2Proc is called with all flags cleared. In all cases, the close2Proc function should return zero if the close operation was successful; otherwise it should return a nonzero POSIX error code. In addition, if an error occurs and interp is not NULL, the procedure should store an error message in the interpreter's result.
These value can be retried with
INPUTPROC
The inputProc field contains the address of a function called by the generic layer to read data from the file or device and store it in an internal buffer. InputProc must match the following prototype:
typedef int Tcl_DriverInputProc( ClientData instanceData, char *buf, int bufSize, int *errorCodePtr);
InstanceData is the same as the value passed to
The errorCodePtr argument points to an integer variable provided by the generic layer. If an error occurs, the function should set the variable to a POSIX error code that identifies the error that occurred.
The function should read data from the input device encapsulated by the channel and store it at buf. On success, the function should return a nonnegative integer indicating how many bytes were read from the input device and stored at buf. On error, the function should return -1. If an error occurs after some data has been read from the device, that data is lost.
If inputProc can determine that the input device has some data available but less than requested by the bufSize argument, the function should only attempt to read as much data as is available and return without blocking. If the input device has no data available whatsoever and the channel is in nonblocking mode, the function should return an EAGAIN error. If the input device has no data available whatsoever and the channel is in blocking mode, the function should block for the shortest possible time until at least one byte of data can be read from the device; then, it should return as much data as it can read without blocking.
This value can be retried with
OUTPUTPROC
The outputProc field contains the address of a function called by the generic layer to transfer data from an internal buffer to the output device. OutputProc must match the following prototype:
typedef int Tcl_DriverOutputProc( ClientData instanceData, char *buf, int toWrite, int *errorCodePtr);
InstanceData is the same as the value passed to
The errorCodePtr argument points to an integer variable provided by the generic layer. If an error occurs, the function should set this variable to a POSIX error code that identifies the error.
The function should write the data at buf to the output device encapsulated by the channel. On success, the function should return a nonnegative integer indicating how many bytes were written to the output device. The return value is normally the same as toWrite, but may be less in some cases such as if the output operation is interrupted by a signal. If an error occurs the function should return -1. In case of error, some data may have been written to the device.
If the channel is nonblocking and the output device is unable to absorb any data whatsoever, the function should return -1 with an EAGAIN error without writing any data.
This value can be retried with
SEEKPROC
The seekProc field contains the address of a function called by the generic layer to move the access point at which subsequent input or output operations will be applied. SeekProc must match the following prototype:
typedef int Tcl_DriverSeekProc( ClientData instanceData, long offset, int seekMode, int *errorCodePtr);
The instanceData argument is the same as the value given to
The errorCodePtr argument points to an integer variable provided by the generic layer for returning errno values from the function. The function should set this variable to a POSIX error code if an error occurs. The function should store an EINVAL error code if the channel type does not implement seeking.
The return value is the new access point or -1 in case of error. If an error occurred, the function should not move the access point.
This value can be retried with
SETOPTIONPROC
The setOptionProc field contains the address of a function called by the generic layer to set a channel type specific option on a channel. setOptionProc must match the following prototype:
typedef int Tcl_DriverSetOptionProc( ClientData instanceData, Tcl_Interp *interp, char *optionName, char *optionValue);
optionName is the name of an option to set, and
optionValue is
the new value for that option, as a string. The instanceData
is the
same as the value given to
Some options are handled by the generic code and this function is never
called to set them, for example,
If the option value is successfully modified to the new value, the function
returns TCL_OK.
It should call
This value can be retried with
GETOPTIONPROC
The getOptionProc field contains the address of a function called by the generic layer to get the value of a channel type specific option on a channel. getOptionProc must match the following prototype:
typedef int Tcl_DriverGetOptionProc( ClientData instanceData, Tcl_Interp *interp, char *optionName, Tcl_DString *dsPtr);
OptionName is the name of an option supported by this type of
channel. If the option name is not NULL, the function stores its
current
value, as a string, in the Tcl dynamic string dsPtr.
If optionName is NULL, the function stores in
dsPtr an
alternating list of all supported options and their current values.
On success, the function returns TCL_OK.
It should call
Some options are handled by the generic code and this function is never
called to retrieve their value, for example,
This value can be retried with
WATCHPROC
The watchProc field contains the address of a function called by the generic layer to initialize the event notification mechanism to notice events of interest on this channel. WatchProc should match the following prototype:
typedef void Tcl_DriverWatchProc( ClientData instanceData, int mask);
The instanceData is the same as the value passed to
The function should initialize device type specific mechanisms to
notice when an event of interest is present on the channel. When one
or more of the designated events occurs on the channel, the channel
driver is responsible for calling
This value can be retried with
GETHANDLEPROC
The getHandleProc field contains the address of a function called by the generic layer to retrieve a device-specific handle from the channel. GetHandleProc should match the following prototype:
typedef int Tcl_DriverGetHandleProc( ClientData instanceData, int direction, ClientData *handlePtr);
InstanceData is the same as the value passed to
If the channel implementation has device-specific handles, the function should retrieve the appropriate handle associated with the channel, according the direction argument. The handle should be stored in the location referred to by handlePtr, and TCL_OK should be returned. If the channel is not open for the specified direction, or if the channel implementation does not use device handles, the function should return TCL_ERROR.
This value can be retried with
FLUSHPROC
The flushProc field is currently reserved for future use. It should be set to NULL. FlushProc should match the following prototype:
typedef int Tcl_DriverFlushProc( ClientData instanceData);
This value can be retried with
HANDLERPROC
The handlerProc field contains the address of a function called by the generic layer to notify the channel that an event occurred. It should be defined for stacked channel drivers that wish to be notified of events that occur on the underlying (stacked) channel. HandlerProc should match the following prototype:
typedef int Tcl_DriverHandlerProc( ClientData instanceData, int interestMask);
InstanceData is the same as the value passed to
This value can be retried with
TCL_BADCHANNELOPTION
This procedure generates a "bad option" error message in an (optional) interpreter. It is used by channel drivers when a invalid Set/Get option is requested. Its purpose is to concatenate the generic options list to the specific ones and factorize the generic options error message string.
It always return TCL_ERROR
An error message is generated in interp's result object to indicate that a command was invoked with the a bad option The message has the form
bad option "blah": should be one of <...generic options...>+<...specific options...> so you get for instance: bad option "-blah": should be one of -blocking, -buffering, -buffersize, -eofchar, -translation, -peername, or -sockname when called with optionList="peername sockname"
blah is the optionName argument and <specific options> is a space separated list of specific option words. The function takes good care of inserting minus signs before each option, commas after, and an or before the last option.
OLD_CHANNEL
The original (version 8.3.1 and earlier) Tcl_ChannelType structure contains the following fields:
typedef struct Tcl_ChannelType { char *typeName; Tcl_DriverBlockModeProc *blockModeProc; Tcl_DriverCloseProc *closeProc; Tcl_DriverInputProc *inputProc; Tcl_DriverOutputProc *outputProc; Tcl_DriverSeekProc *seekProc; Tcl_DriverSetOptionProc *setOptionProc; Tcl_DriverGetOptionProc *getOptionProc; Tcl_DriverWatchProc *watchProc; Tcl_DriverGetHandleProc *getHandleProc; Tcl_DriverClose2Proc *close2Proc; } Tcl_ChannelType;
It is still possible to create channel with the above structure. The internal channel code will determine the version. It is imperative to use the new Tcl_ChannelType structure if you are creating a stacked channel driver, due to problems with the earlier stacked channel implementation (in 8.2.0 to 8.3.1).
PORTABILITY
Windows 8.1. Windows Server 2012 R2. Windows 10. Windows Server 2016. Windows Server 2019. Windows 11. Windows Server 2022.
AVAILABILITY
PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition
SEE ALSO
- Functions:
Tcl_Close() ,Tcl_OpenFileChannel() ,Tcl_QueueEvent() ,Tcl_SetErrno() ,Tcl_StackChannel()
PTC MKS Toolkit 10.4 Documentation Build 39.