Tcl_CreateChannel(3)

πŸ“¦
version 8.6.17 Β· 2 versions
  • 8.6.17 Β· alpine 3.22, alpine 3.23, alpine 3.24, arch rolling, centos-stream 10, opensuse-leap 16.0 (latest)
  • 8.6.10 Β· centos-stream 9
, in alpine 3.22, alpine 3.23, alpine 3.24, arch rolling, centos-stream 10, opensuse-leap 16.0

πŸ’… Tcl_CreateChannel(3) πŸ’…

okay bestie, welcome to Tcl_CreateChannel(3) from the Tcl Library Procedures section β€” this is the group chat for creating and wrangling channels, and every single one of these functions is invited. no cap, the whole squad is here. 🫢


🌸 NAME 🌸

these are procedures for creating and manipulating channels, and here's the full cast of characters β€” every single one:

  • Tcl_CreateChannel
  • Tcl_GetChannelInstanceData
  • Tcl_GetChannelType
  • Tcl_GetChannelName
  • Tcl_GetChannelHandle
  • Tcl_GetChannelMode
  • Tcl_GetChannelBufferSize
  • Tcl_SetChannelBufferSize
  • Tcl_NotifyChannel
  • Tcl_BadChannelOption
  • Tcl_ChannelName
  • Tcl_ChannelVersion
  • Tcl_ChannelBlockModeProc
  • Tcl_ChannelCloseProc
  • Tcl_ChannelClose2Proc
  • Tcl_ChannelInputProc
  • Tcl_ChannelOutputProc
  • Tcl_ChannelSeekProc
  • Tcl_ChannelWideSeekProc
  • Tcl_ChannelTruncateProc
  • Tcl_ChannelSetOptionProc
  • Tcl_ChannelGetOptionProc
  • Tcl_ChannelWatchProc
  • Tcl_ChannelGetHandleProc
  • Tcl_ChannelFlushProc
  • Tcl_ChannelHandlerProc
  • Tcl_ChannelThreadActionProc
  • Tcl_IsChannelShared
  • Tcl_IsChannelRegistered
  • Tcl_CutChannel
  • Tcl_SpliceChannel
  • Tcl_IsChannelExisting
  • Tcl_ClearChannelHandlers
  • Tcl_GetChannelThread
  • Tcl_ChannelBuffered

✨ SYNOPSIS ✨

#include <tcl.h>

Tcl_Channel
Tcl_CreateChannel(typePtr, channelName, instanceData, mask)

ClientData
Tcl_GetChannelInstanceData(channel)

const Tcl_ChannelType *
Tcl_GetChannelType(channel)

const char *
Tcl_GetChannelName(channel)

int
Tcl_GetChannelHandle(channel, direction, handlePtr)

Tcl_ThreadId
Tcl_GetChannelThread(channel)

int
Tcl_GetChannelMode(channel)

int
Tcl_GetChannelBufferSize(channel)

Tcl_SetChannelBufferSize(channel, size)

Tcl_NotifyChannel(channel, mask)

int
Tcl_BadChannelOption(interp, optionName, optionList)

int
Tcl_IsChannelShared(channel)

int
Tcl_IsChannelRegistered(interp, channel)

int
Tcl_IsChannelExisting(channelName)

void
Tcl_CutChannel(channel)

void
Tcl_SpliceChannel(channel)

void
Tcl_ClearChannelHandlers(channel)

int
Tcl_ChannelBuffered(channel)

const 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_DriverWideSeekProc *
Tcl_ChannelWideSeekProc(typePtr)

Tcl_DriverThreadActionProc *
Tcl_ChannelThreadActionProc(typePtr)

Tcl_DriverTruncateProc *
Tcl_ChannelTruncateProc(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 πŸŽ€

okay so here's the vibe check on every parameter β€” keep these straight, bestie, because the details matter πŸ’–

  • const 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. like, it's the instruction manual for how this channel does its thing.
  • const char *channelName (in) β€” the name of this channel, such as file3; it must not be in use by any other channel. can be NULL, in which case the channel is created without a name. but if the created channel is assigned to one of the standard channels (stdin, stdout or stderr), the assigned channel name will be the name of the standard channel. iykyk.
  • 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. it's giving little sidekick that tags along everywhere.
  • int mask (in) β€” OR-ed combination of TCL_READABLE and TCL_WRITABLE to indicate whether a channel is readable and writable. yes bestie, both flags can show up to the party at once.
  • Tcl_Channel channel (in) β€” the channel to operate on. the main character, literally.
  • int direction (in) β€” TCL_READABLE means the input handle is wanted; TCL_WRITABLE means the output handle is wanted. pick your lane πŸ’…
  • ClientData *handlePtr (out) β€” points to the location where the desired OS-specific handle should be stored. that's where the answer gets dropped off.
  • int size (in) β€” the size, in bytes, of buffers to allocate in this channel. buffer math, but make it cute.
  • int mask (in) β€” an OR-ed combination of TCL_READABLE, TCL_WRITABLE and TCL_EXCEPTION that indicates events that have occurred on this channel. yeah, there are two different mask parameters in this section β€” iconic, honestly.
  • Tcl_Interp *interp (in) β€” current interpreter. (can be NULL). so lowkey optional sometimes, but you still gotta keep track.
  • const char *optionName (in) β€” name of the invalid option. yeah, we're calling out the problem child by name.
  • const 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. very "just here for the generic error" energy.

🌸 DESCRIPTION 🌸

okay bestie, so Tcl uses a two-layered channel architecture and honestly that's the whole vibe of this page. πŸ’…

It gives you a generic upper layer so that C programs and Tcl programs can do input and output using the same APIs for a whole bunch of stuff β€” files, devices, sockets, etc. The generic C APIs live in the manual entry for Tcl_OpenFileChannel (go read that one too, iykyk).

The lower layer is where it gets type-specific: it gives you channel drivers for each type of device supported on each platform. This manual entry is all about the C APIs used to talk between the generic layer and the type-specific channel drivers. It also explains how you can add brand new types of channels by writing new channel drivers. ✨


So a channel driver is made of a few components:

  1. First, every channel driver provides a Tcl_ChannelType structure stuffed with pointers to functions that implement the various operations the generic layer uses to talk to the channel driver. That structure and the functions it references get their own section β€” TCL_CHANNELTYPE, right below. πŸ‘‘
  2. Second, channel drivers usually give you 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. 🫢
  3. Third, a channel driver optionally provides a C function to open channel instances of that type. For example, Tcl_OpenFileChannel opens a channel that uses the file channel driver, and Tcl_OpenTcpClient opens a channel that uses the TCP network protocol. These creation functions typically use Tcl_CreateChannel internally to open the channel.

So like β€” to add a new type of channel you must implement a C API or a Tcl command that opens a channel by invoking Tcl_CreateChannel. When your driver calls Tcl_CreateChannel it passes in a Tcl_ChannelType structure describing the driver's I/O procedures. The generic layer will then invoke the functions referenced in that structure to do operations on the channel. no cap. πŸ”₯


Tcl_CreateChannel opens a new channel and associates the supplied typePtr and instanceData with it. The channel is opened in the mode indicated by mask. For the whole discussion of channel drivers, their operations and the Tcl_ChannelType structure, see the TCL_CHANNELTYPE section below. πŸ’–

Tcl_CreateChannel also interacts with the code managing the standard channels. Once a standard channel was initialized β€” either through a call to Tcl_GetStdChannel or a call to Tcl_SetStdChannel β€” closing this standard channel will cause the next call to Tcl_CreateChannel to make the new channel the new standard channel too. See Tcl_StandardChannels for a general treatise about standard channels and how the Tcl library behaves with regard to them. πŸŽ€


Here's the function-by-function rundown, get comfy:

  • Tcl_GetChannelInstanceData β€” returns the instance data associated with the channel in channel. This is the same as the instanceData argument in the call to Tcl_CreateChannel that created this channel.
  • Tcl_GetChannelType β€” returns a pointer to the Tcl_ChannelType structure used by the channel in the channel argument. Same as the typePtr argument in the call to Tcl_CreateChannel that created this channel.
  • Tcl_GetChannelName β€” returns a string containing the name associated with the channel, or NULL if the channelName argument to Tcl_CreateChannel was NULL.
  • Tcl_GetChannelHandle β€” places the OS-specific device handle associated with channel for the given direction in the location specified by handlePtr, and returns TCL_OK. If the channel does not have a device handle for the specified direction, then TCL_ERROR is returned instead. Different channel drivers will return different types of handle β€” refer to the manual entries for each driver to figure out what type of handle you're getting. πŸ™ˆ
  • Tcl_GetChannelThread β€” returns the id of the thread currently managing the specified channel. This lets channel drivers send their file events to the correct event queue even for a multi-threaded core. 🧡
  • Tcl_GetChannelMode β€” returns an OR-ed combination of TCL_READABLE and TCL_WRITABLE, indicating whether the channel is open for input and output.
  • Tcl_GetChannelBufferSize β€” returns the size, in bytes, of buffers allocated to store input or output in channel. If the value was not set by a previous call to Tcl_SetChannelBufferSize (described below), then the default value of 4096 is returned.
  • Tcl_SetChannelBufferSize β€” sets the size, in bytes, of buffers that will be allocated in subsequent operations on the channel to store input or output. The size argument should be between one and one million, allowing buffers of one byte to one million bytes. If size is outside this range, Tcl_SetChannelBufferSize sets the buffer size to 4096. πŸ’€ (yes, it just silently picks 4096, that's the rule)
  • Tcl_NotifyChannel β€” called by a channel driver to tell the generic layer that the events specified by mask have occurred on the channel. Channel drivers are responsible for invoking this function whenever the channel handlers need to be called for the channel (or other pending tasks like a write flush should be performed). See WATCHPROC below for more details.
  • Tcl_BadChannelOption β€” called from driver specific setOptionProc or getOptionProc to generate a complete error message.
  • Tcl_ChannelBuffered β€” returns the number of bytes of input currently buffered in the internal buffer (the push back area) of the channel itself. It does not report about the data in the overall buffers for the stack of channels the supplied channel is part of. ⚠️
  • Tcl_IsChannelShared β€” checks the refcount of the specified channel and returns whether the channel was shared among multiple interpreters (result == 1) or not (result == 0).
  • Tcl_IsChannelRegistered β€” checks whether the specified channel is registered in the given interpreter (result == 1) or not (result == 0).
  • Tcl_IsChannelExisting β€” checks whether a channel with the specified name is registered in the (thread)-global list of all channels (result == 1) or not (result == 0).
  • Tcl_CutChannel β€” removes the specified channel from the (thread)global list of all channels (of the current thread). Application to a channel still registered in some interpreter is not allowed. Also notifies the driver if the Tcl_ChannelType version is TCL_CHANNEL_VERSION_4 (or higher), and Tcl_DriverThreadActionProc is defined for it.
  • Tcl_SpliceChannel β€” adds the specified channel to the (thread)global list of all channels (of the current thread). Application to a channel registered in some interpreter is not allowed. Also notifies the driver if the Tcl_ChannelType version is TCL_CHANNEL_VERSION_4 (or higher), and Tcl_DriverThreadActionProc is defined for it.
  • Tcl_ClearChannelHandlers β€” removes all channel handlers and event scripts associated with the specified channel, thus shutting down all event processing for this channel. πŸ›‘

πŸ‘‘ TCL_CHANNELTYPE πŸ‘‘

So a channel driver provides a Tcl_ChannelType structure full of pointers to functions that implement the various operations on a channel β€” the generic layer invokes these as needed. The structure got versioned starting in Tcl 8.3.2/8.4 to fix a problem with stacked channel drivers. See the OLD CHANNEL TYPES section below for the tea on the old structure. β˜•

The Tcl_ChannelType structure contains these fields, exactly:

typedef struct Tcl_ChannelType {
        const 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_DriverWideSeekProc *wideSeekProc;
        Tcl_DriverThreadActionProc *threadActionProc;
        Tcl_DriverTruncateProc *truncateProc;
} Tcl_ChannelType;

You do not have to provide implementations for all channel operations, bestie, breathe. πŸ’– Those which aren't necessary may be set to NULL in the struct: blockModeProc, seekProc, setOptionProc, getOptionProc, getHandleProc, and close2Proc, in addition to flushProc, handlerProc, threadActionProc, and truncateProc. Other functions that cannot be implemented in a meaningful way should return EINVAL when called, to indicate that the operations they represent are not available. Also note that wideSeekProc can be NULL if seekProc is. πŸ¦‹

The user should only use the above structure for Tcl_ChannelType instantiation. πŸ‘€

When you're referencing fields in a Tcl_ChannelType structure, use these functions to obtain the values:

Tcl_ChannelName, Tcl_ChannelVersion, Tcl_ChannelBlockModeProc, Tcl_ChannelCloseProc, Tcl_ChannelClose2Proc, Tcl_ChannelInputProc, Tcl_ChannelOutputProc, Tcl_ChannelSeekProc, Tcl_ChannelWideSeekProc, Tcl_ChannelThreadActionProc, Tcl_ChannelTruncateProc, Tcl_ChannelSetOptionProc, Tcl_ChannelGetOptionProc, Tcl_ChannelWatchProc, Tcl_ChannelGetHandleProc, Tcl_ChannelFlushProc, or Tcl_ChannelHandlerProc.

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 (i.e. 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, e.g. file or socket. πŸ“

This value can be retrieved with Tcl_ChannelName, which returns a pointer to the string.

⭐ VERSION

The version field should be set to the version of the structure that you require. TCL_CHANNEL_VERSION_2 is the minimum recommended. TCL_CHANNEL_VERSION_3 must be set to specify the wideSeekProc member. TCL_CHANNEL_VERSION_4 must be set to specify the threadActionProc member (includes wideSeekProc). TCL_CHANNEL_VERSION_5 must be set to specify the truncateProc members (includes wideSeekProc and threadActionProc). If it is not set to any of these, then this Tcl_ChannelType is assumed to have the original structure β€” see OLD CHANNEL TYPES for more details. And like, while Tcl will recognize and function with either structures, stacked channels must be of at least TCL_CHANNEL_VERSION_2 to function correctly. πŸ”₯

This value can be retrieved with Tcl_ChannelVersion, which returns one of TCL_CHANNEL_VERSION_5, TCL_CHANNEL_VERSION_4, TCL_CHANNEL_VERSION_3, TCL_CHANNEL_VERSION_2 or TCL_CHANNEL_VERSION_1.

πŸ’… BLOCKMODEPROC

So the blockModeProc field is literally the address of a function that the generic layer calls to flip the device between blocking and nonblocking mode. BlockModeProc has to match this prototype, no cap:

typedef int Tcl_DriverBlockModeProc(
        ClientData instanceData,
        int mode);

The instanceData is the exact same value you handed to Tcl_CreateChannel when this channel was born. The mode argument is either TCL_MODE_BLOCKING or TCL_MODE_NONBLOCKING, and it's telling the device which mode to go into. Your function should return zero if the operation slayed, or a nonzero POSIX error code if it flopped. ✨

Okay and if the operation is successful, the function can modify the supplied instanceData to record that the channel entered blocking or nonblocking mode, and to actually implement that blocking or nonblocking behavior. For some device types, the underlying operating system can handle the blocking/nonblocking thing for you; for other device types, you have to emulate the behavior inside the channel driver itself. iykyk.

You can grab this value with Tcl_ChannelBlockModeProc, which returns a pointer to the function. 🫢

⚠️ A channel driver that does not supply a blockModeProc has to be very, very careful. It has to tell the generic layer exactly which blocking mode is acceptable to it, and should this also document for the user so that the blocking mode of the channel is not changed to an unacceptable value. Any confusion here may lead the interpreter into a (spurious and difficult to find) deadlock. πŸ’€ Like, genuinely β€” deadlock. Not cute.

πŸŽ€ CLOSEPROC AND CLOSE2PROC

The closeProc field holds the address of a function that the generic layer calls to clean up all the driver-related info when the channel gets closed. CloseProc must match this prototype:

typedef int Tcl_DriverCloseProc(
        ClientData instanceData,
        Tcl_Interp *interp);

The instanceData argument is the same value you gave to Tcl_CreateChannel when the channel was created. The function should release any storage that the channel driver is holding onto for this channel, and close the input and output devices encapsulated by this channel. Heads up: all queued output will have already been flushed to the device before this function gets called, and no further driver operations will be invoked on this instance after you call closeProc. If the close worked, the procedure should return zero; otherwise it returns a nonzero POSIX error code. Also, if an error occurs and interp is not NULL, the procedure should store an error message in the interpreter's result. 🌸

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 matching this prototype:

typedef int Tcl_DriverClose2Proc(
        ClientData instanceData,
        Tcl_Interp *interp,
        int flags);

The close2Proc gets called with flags set to an OR'ed combination of TCL_CLOSE_READ or TCL_CLOSE_WRITE, which is the driver's cue to 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. If flags is 0 (zero), the driver should close the channel in the manner described above for closeProc. And 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. Plus, if an error occurs and interp is not NULL, the procedure should store an error message in the interpreter's result. πŸ‘‘

The closeProc and close2Proc values can be retrieved with Tcl_ChannelCloseProc or Tcl_ChannelClose2Proc, which return a pointer to the respective function.


πŸ¦‹ INPUTPROC

The inputProc field is the address of a function that the generic layer calls to read data from the file or device and stash it in an internal buffer. InputProc must match this prototype:

typedef int Tcl_DriverInputProc(
        ClientData instanceData,
        char *buf,
        int bufSize,
        int *errorCodePtr);

InstanceData is the same as the value passed to Tcl_CreateChannel when the channel was created. The buf argument points to an array of bytes where you're gonna store input from the device, and bufSize tells you how many bytes are available at buf. πŸ“

The errorCodePtr argument points to an integer variable provided by the generic layer. If an error goes down, the function should set that variable to a POSIX error code identifying what happened.

So the function should read data from the input device encapsulated by the channel and store it at buf. On success, return a nonnegative integer saying how many bytes were read from the input device and stored at buf. On error, return -1. And real talk: if an error occurs after some data has already been read from the device, that data is lost. 😭

If inputProc can tell that the input device has some data available but less than what bufSize requested, the function should only attempt to read as much data as is available and return without blocking. If the input device has literally no data available whatsoever and the channel is in nonblocking mode, the function should return an EAGAIN error. And 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. Iconic behavior, honestly.

This value can be retrieved with Tcl_ChannelInputProc, which returns a pointer to the function.


πŸ’œ OUTPUTPROC

The outputProc field is the address of a function the generic layer calls to transfer data from an internal buffer out to the output device. OutputProc must match this prototype:

typedef int Tcl_DriverOutputProc(
        ClientData instanceData,
        const char *buf,
        int toWrite,
        int *errorCodePtr);

InstanceData is the same as the value passed to Tcl_CreateChannel when the channel was created. The buf argument contains an array of bytes to be written to the device, and toWrite says how many bytes from buf are getting written. ✨

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, return a nonnegative integer indicating how many bytes were written to the output device. Normally that return value is the same as toWrite, but it may be less in some cases β€” like if the output operation gets interrupted by a signal. If an error occurs the function should return -1. And in case of error, some data may have already 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 retrieved with Tcl_ChannelOutputProc, which returns a pointer to the function.


πŸ”₯ SEEKPROC AND WIDESEEKPROC

The seekProc field is the address of a function the generic layer calls to move the access point where subsequent input or output operations will be applied. SeekProc must match this prototype:

typedef int Tcl_DriverSeekProc(
        ClientData instanceData,
        long offset,
        int seekMode,
        int *errorCodePtr);

The instanceData argument is the same as the value given to Tcl_CreateChannel when this channel was created. Offset and seekMode have the same meaning as for the Tcl_Seek procedure (described in the manual entry for Tcl_OpenFileChannel).

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. And if the channel type doesn't implement seeking, the function should store an EINVAL error code. 🫢

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. Periodt.

If there's a non-NULL seekProc field, the wideSeekProc field may contain the address of an alternative function to use which handles wide (i.e. larger than 32-bit) offsets β€” so allowing seeks within files larger than 2GB. The wideSeekProc will be called in preference to the seekProc, but both must be defined if the wideSeekProc is defined. WideSeekProc must match this prototype:

typedef Tcl_WideInt Tcl_DriverWideSeekProc(
        ClientData instanceData,
        Tcl_WideInt offset,
        int seekMode,
        int *errorCodePtr);

The arguments and return values mean the same thing as with seekProc above, except that the type of offsets and the return type are different. πŸ’…

The seekProc value can be retrieved with Tcl_ChannelSeekProc, which returns a pointer to the function, and similarly the wideSeekProc can be retrieved with Tcl_ChannelWideSeekProc.


🌈 SETOPTIONPROC

The setOptionProc field is the address of a function the generic layer calls to set a channel type specific option on a channel. setOptionProc must match this prototype:

typedef int Tcl_DriverSetOptionProc(
        ClientData instanceData,
        Tcl_Interp *interp,
        const char *optionName,
        const char *newValue);

optionName is the name of an option to set, and newValue is the new value for that option, as a string. The instanceData is the same as the value given to Tcl_CreateChannel when this channel was created. The function should do whatever channel type specific action is required to implement the new value of the option. πŸŽ€

Some options are handled by the generic code and this function is never called to set them, e.g. -blockmode. Other options are specific to each channel type, and the setOptionProc procedure of the channel driver will get called to implement them. The setOptionProc field can be NULL, which indicates that this channel type supports no type specific options. ⭐

If the option value gets successfully changed to the new value, the function returns TCL_OK. It should call Tcl_BadChannelOption, which itself returns TCL_ERROR, if the optionName isn't recognized. If newValue asks for a value the option just doesn't support, or if a system call error happens, the function should leave an error message in the result of interp if interp is not NULL. It should also call Tcl_SetErrno to store an appropriate POSIX error code.

You can grab this value with Tcl_ChannelSetOptionProc, which returns a pointer to the function. 🫢

GETOPTIONPROC

The getOptionProc field holds the address of a function that the generic layer calls to get the value of a channel-type-specific option on a channel. getOptionProc has to match this prototype exactly:

typedef int Tcl_DriverGetOptionProc(
        ClientData instanceData,
        Tcl_Interp *interp,
        const char *optionName,
        Tcl_DString *optionValue);

optionName is the name of an option supported by this type of channel. If optionName is not NULL, the function stashes its current value β€” as a string β€” in the Tcl dynamic string optionValue. If optionName is NULL, the function stashes in optionValue an alternating list of all supported options and their current values. On success, the function returns TCL_OK. It should call Tcl_BadChannelOption, which itself returns TCL_ERROR, if optionName is unrecognized. If a system call error occurs, the function should leave an error message in the result of interp if interp is not NULL. It should also call Tcl_SetErrno to store an appropriate POSIX error code.

Some options are handled by the generic code and this function never even gets called to retrieve their value β€” -blockmode, for example. Other options are specific to each channel type, and the getOptionProc procedure of the channel driver gets called to implement them. The getOptionProc field can be NULL, which means this channel type supports no type-specific options, bestie, none at all. 🌸

You can grab this value with Tcl_ChannelGetOptionProc, which returns a pointer to the function.

WATCHPROC

The watchProc field holds the address of a function the generic layer calls to set up the event notification mechanism so it notices events of interest on this channel. watchProc should match this prototype:

typedef void Tcl_DriverWatchProc(
        ClientData instanceData,
        int mask);

The instanceData is the same value that got passed to Tcl_CreateChannel back when this channel was created. The mask argument is an OR-ed combination of TCL_READABLE, TCL_WRITABLE and TCL_EXCEPTION β€” it tells you which events the caller cares about noticing on this channel.

The function should set up device-type-specific mechanisms to notice when an event of interest is present on the channel. When one or more of the designated events happens on the channel, the channel driver is responsible for calling Tcl_NotifyChannel to inform the generic channel module. The driver should be careful not to starve other channel drivers or sources of callbacks by invoking Tcl_NotifyChannel too frequently β€” fairness can be insured by using the Tcl event queue so the channel event gets scheduled in sequence with other events. Peep the description of Tcl_QueueEvent for the deets on how to queue an event. ✨

You can grab this value with Tcl_ChannelWatchProc, which returns a pointer to the function.

GETHANDLEPROC

The getHandleProc field holds the address of a function the generic layer calls to retrieve a device-specific handle from the channel. GetHandleProc should match this prototype:

typedef int Tcl_DriverGetHandleProc(
        ClientData instanceData,
        int direction,
        ClientData *handlePtr);

instanceData is the same value that got passed to Tcl_CreateChannel when this channel was created. The direction argument is either TCL_READABLE, to retrieve the handle used for input, or TCL_WRITABLE, to retrieve the handle used for output.

If the channel implementation has device-specific handles, the function should retrieve the appropriate handle associated with the channel, according to the direction argument. The handle should be stored in the location handlePtr refers to, and TCL_OK should be returned. If the channel isn't open for the specified direction, or if the channel implementation doesn't use device handles, the function should return TCL_ERROR.

You can grab this value with Tcl_ChannelGetHandleProc, which returns a pointer to the function.

FLUSHPROC

The flushProc field is currently reserved for future use. Set it to NULL. Like, that's the whole vibe of it, okay? FlushProc should match this prototype:

typedef int Tcl_DriverFlushProc(
        ClientData instanceData);

You can grab this value with Tcl_ChannelFlushProc, which returns a pointer to the function.

HANDLERPROC

The handlerProc field holds the address of a function the generic layer calls to notify the channel that an event occurred. It should be defined for stacked channel drivers that want to be notified of events happening on the underlying (stacked) channel. HandlerProc should match this prototype:

typedef int Tcl_DriverHandlerProc(
        ClientData instanceData,
        int interestMask);

instanceData is the same value that got passed to Tcl_CreateChannel when this channel was created. The interestMask is an OR-ed combination of TCL_READABLE or TCL_WRITABLE; it tells you what type of event occurred on this channel. πŸ‘‘

You can grab this value with Tcl_ChannelHandlerProc, which returns a pointer to the function.

THREADACTIONPROC

The threadActionProc field holds the address of the function the generic layer calls when a channel is created, closed, or about to move to a different thread β€” basically whenever thread-specific driver state might have to be initialized or updated. It can be NULL. The action TCL_CHANNEL_THREAD_REMOVE is used to notify the driver that it should update or remove any thread-specific data it might be maintaining for the channel.

The action TCL_CHANNEL_THREAD_INSERT is used to notify the driver that it should update or initialize any thread-specific data it might be maintaining, using the calling thread as the associate. See Tcl_CutChannel and Tcl_SpliceChannel for more detail. 🧚

typedef void Tcl_DriverThreadActionProc(
        ClientData instanceData,
        int action);

instanceData is the same value that got passed to Tcl_CreateChannel when this channel was created.

These values can be retrieved with Tcl_ChannelThreadActionProc, which returns a pointer to the function.

TRUNCATEPROC

The truncateProc field holds the address of the function the generic layer calls when a channel is truncated to some length. It can be NULL. πŸ“

typedef int Tcl_DriverTruncateProc(
        ClientData instanceData,
        Tcl_WideInt length);

instanceData is the same value that got passed to Tcl_CreateChannel when this channel was created, and length is the new length of the underlying file, which should not be negative. The result should be 0 on success, or an errno code (suitable for use with Tcl_SetErrno) on failure.

These values can be retrieved with Tcl_ChannelTruncateProc, which returns a pointer to the function.


πŸ’€ TCL_BADCHANNELOPTION πŸ’€

This procedure generates a "bad option" error message in an (optional) interpreter. Channel drivers use it when an invalid Set/Get option is requested. Its whole purpose is to concatenate the generic options list onto the specific ones and factorize the generic options error message string. Efficiency queen behavior, honestly.

It always returns TCL_ERROR.

An error message gets generated in interp's result value to indicate that a command was invoked with a bad option. The message has this 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 equal to "peername sockname".

"blah" is the optionName argument and "<specific options>" is a space-separated list of specific option words. The function takes very good care of inserting minus signs before each option, commas after, and an "or" before the last option. It's giving attention to detail. πŸ’…


🌈 OLD CHANNEL TYPES 🌈

The original (8.3.1 and below) Tcl_ChannelType structure contains these fields:

typedef struct Tcl_ChannelType {
    const 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's still possible to create a channel with the above structure β€” the internal channel code will figure out the version on its own. But it is imperative to use the new Tcl_ChannelType structure if you're creating a stacked channel driver, because of problems with the earlier stacked channel implementation (in 8.2.0 to 8.3.1). Like, do not do it, bestie, that way lies pain. 😭

Prior to 8.4.0 (i.e. during the later releases of 8.3 and the early part of the 8.4 development cycle) the Tcl_ChannelType structure contained these fields:

typedef struct Tcl_ChannelType {
    const 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_DriverTruncateProc *truncateProc;
} Tcl_ChannelType;

When the above structure gets registered as a channel type, the version field should always be TCL_CHANNEL_VERSION_2. No exceptions, periodt.


πŸ‘€ SEE ALSO πŸ‘€

Tcl_Close(3), Tcl_OpenFileChannel(3), Tcl_SetErrno(3), Tcl_QueueEvent(3), Tcl_StackChannel(3), Tcl_GetStdChannel(3)

πŸ”‘ KEYWORDS πŸ”‘

Okay bestie, here's the lil tag cloud so you can find this page again when you forget everything (no judgement, it happens πŸ’–):

  • blocking
  • channel driver
  • channel registration
  • channel type
  • nonblocking

That's the whole vibe check β€” those are the search terms, fr fr. Type any of 'em into your man game and this page will come running πŸƒβ€β™€οΈπŸ’¨


And that's a wrap on Tcl_CreateChannel(3), bestie!! You made it all the way through πŸ’…βœ¨ Go forth and register channels like the icon you are. πŸŽ€

This lil masterpiece lives in the Tcl manual, version 8.4, filed under Tcl_CreateChannel(3) β€” dated however long ago Tcl 8.4 was a thing (spoiler: a minute ago in Unix years 🫢). Copyright and license deets belong to the Tcl source distribution, so go peep the original page if you need the legal fine print. Mwah! πŸ’œ

πŸ“¦ where this page comes from