Tcl_CreateChannel(3)

πŸ’– Tcl_CreateChannel(3) β€” Tcl Library Procedures πŸ’–

okay bestie, welcome to the Tcl library procedures page where we literally create and manipulate channels. it's giving backend energy, it's giving file descriptors, it's giving main character I/O. let's go ✨


✨ NAME ✨

so this page covers a whole friend group of procedures: 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_ChannelClose2Proc, Tcl_ChannelInputProc, Tcl_ChannelOutputProc, 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 β€” procedures for creating and manipulating channels πŸ’…


✨ SYNOPSIS ✨

#include <tcl.h>

Tcl_Channel
Tcl_CreateChannel(typePtr, channelName, instanceData, mask)

void *
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                                                                      2
Tcl_RemoveChannelMode(interp, channel, mode)                             2

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)

Tcl_CutChannel(channel)

Tcl_SpliceChannel(channel)

Tcl_ClearChannelHandlers(channel)

int
Tcl_ChannelBuffered(channel)

const char *
Tcl_ChannelName(typePtr)

Tcl_ChannelTypeVersion
Tcl_ChannelVersion(typePtr)

Tcl_DriverBlockModeProc *
Tcl_ChannelBlockModeProc(typePtr)

Tcl_DriverClose2Proc *
Tcl_ChannelClose2Proc(typePtr)

Tcl_DriverInputProc *
Tcl_ChannelInputProc(typePtr)

Tcl_DriverOutputProc *
Tcl_ChannelOutputProc(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 ✨

  • 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. it's the channel's whole personality in a struct, no cap.
  • 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. 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. so don't try to steal a name, bestie.
  • void *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 like the channel's emotional support pointer 🫢.
  • int mask (in) β€” OR-ed combination of TCL_READABLE and TCL_WRITABLE to indicate whether a channel is readable and writable. yes, it's the vibe check for I/O direction.
  • Tcl_Channel channel (in) β€” the channel to operate on. that's it. the main character.
  • int direction (in) β€” TCL_READABLE means the input handle is wanted; TCL_WRITABLE means the output handle is wanted. pick your fighter.
  • void **handlePtr (out) β€” points to the location where the desired OS-specific handle should be stored. it's an out param, so it gets filled in. very demure, very mindful.
  • Tcl_Size size (in) β€” the size, in bytes, of buffers to allocate in this channel. buffer size but make it fashion.
  • int mask (in) β€” an OR-ed combination of TCL_READABLE, TCL_WRITABLE and TCL_EXCEPTION that indicates events that have occurred on this channel. (yes, there are two mask arguments on this page, different contexts, keep up πŸ’…)
  • Tcl_Interp *interp (in) β€” current interpreter. (can be NULL). it's the Tcl interpreter context.
  • const char *optionName (in) β€” name of the invalid option. for when someone tries to be messy with options.
  • 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. so if you're extra, you can add your own options to the error message.

🌈 DESCRIPTION 🌈

okay so bestie, Tcl is giving two-layered channel architecture realness. πŸ’… It provides a generic upper layer so C and Tcl programs can do input and output using the same APIs for a whole buffet of files, devices, sockets, etc. The generic C APIs are described in the manual entry for Tcl_OpenFileChannel.

The lower layer? That's where the type-specific channel drivers live, one for each type of device supported on each platform. This manual entry 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 up: 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, 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.

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 perform 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 a discussion of channel drivers, their operations and the Tcl_ChannelType structure, see the section TCL_CHANNELTYPE, below.

Tcl_CreateChannel 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 the behavior of the Tcl library with regard to them.


  • 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. This is the 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 determine what type of handle is returned. πŸ’…
  • Tcl_GetChannelThread β€” returns the id of the thread currently managing the specified channel. This allows channel drivers to 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_RemoveChannelMode β€” removes an access privilege from the channel, either TCL_READABLE or TCL_WRITABLE, and returns a regular Tcl result code, TCL_OK, or TCL_ERROR. The function throws an error if either an invalid mode is specified or the result of the removal would be an inaccessible channel. In that case an error message is left in the interp argument, if not NULL. 😭
  • 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.
  • Tcl_NotifyChannel β€” is called by a channel driver to indicate to 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 β€” is 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 (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 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 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 ✨

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 TYPES section below for details about the old structure.

The Tcl_ChannelType structure contains the following fields:

              typedef struct {
                      const char *typeName;
                      Tcl_ChannelTypeVersion version;
                      void *closeProc; /* Not used any more*/
                      Tcl_DriverInputProc *inputProc;
                      Tcl_DriverOutputProc *outputProc;
                      void *seekProc; /* Not used any more */
                      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;

It is not necessary to provide implementations for all channel operations. Those which are not 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 referencing fields in a Tcl_ChannelType structure, the following functions should be used to obtain the values: Tcl_ChannelName, Tcl_ChannelVersion, Tcl_ChannelBlockModeProc, Tcl_ChannelClose2Proc, Tcl_ChannelInputProc, Tcl_ChannelOutputProc, 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_5 is the minimum supported.

This value can be retrieved with Tcl_ChannelVersion.

πŸŽ€ 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(
                      void *instanceData,
                      int mode);

Okay so real talk, bestie β€” the instanceData? It's literally the exact same value you passed to Tcl_CreateChannel when this channel got born. And mode is either TCL_MODE_BLOCKING or TCL_MODE_NONBLOCKING, which is just your way of telling the device "hey bestie, we're going blocking" or "nope, nonblocking era." 🫢

Return zero if it worked, or a nonzero POSIX error code if it flopped. Simple. Iconic.

And here's the cute part: if it did work, you're allowed to go ahead and modify that instanceData you were handed, to record that the channel switched into blocking-or-nonblocking mode and to actually implement that behavior. ✨ For some device types the underlying operating system handles all of it β€” the blocking/nonblocking behavior is just… free. πŸŽ€ For other device types, nope, the channel driver has to emulate the whole vibe itself. 😭

You can grab this value back with Tcl_ChannelBlockModeProc, which hands you a pointer to the function. πŸ‘‘

🚨 WARNING BESTIE 🚨 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 it should also document that for the user, so nobody goes and changes the blocking mode of the channel to something unacceptable. Any confusion here may lead the interpreter into a spurious and difficult-to-find deadlock. And not the cute kind. πŸ’€

🌸 CLOSE2PROC 🌸

The close2Proc field holds the address of a function the generic layer calls to clean up all the driver-related stuff when the channel gets closed. It's giving goodbye, but make it tidy. 🧹

Close2Proc must match this prototype, character for character:

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

If flags is 0, then instanceData is the same value you gave to Tcl_CreateChannel back when the channel was created. πŸ’– Your function should release any storage the channel driver was holding for this channel, and close the input and output devices that this channel encapsulates. All queued output will already have been flushed to the device before this function gets called β€” and after closeProc is called, no further driver operations will be invoked on this instance. If the close worked out, return zero. If not, return a nonzero POSIX error code. Also β€” and this part matters β€” if an error happens and interp is not NULL, the procedure should store an error message in the interpreter's result. πŸ“

OR, plot twist πŸŒ€: channels that support closing the read and write sides independently may accept flag values other than 0. In that case close2Proc gets called with flags set to an OR'ed combination of TCL_CLOSE_READ or TCL_CLOSE_WRITE, which tells the driver "close the read side and/or the write side, bestie." The channel driver may then be invoked to perform additional operations on the channel after close2Proc is called, to close one or both sides of it. In all cases, close2Proc should return zero if the close operation was successful; otherwise it should return a nonzero POSIX error code. And again: if an error occurs and interp is not NULL, store an error message in the interpreter's result. πŸ’…

You can retrieve this close2Proc value with Tcl_ChannelClose2Proc, which returns a pointer to the function. 🌈


πŸ¦‹ INPUTPROC πŸ¦‹

The inputProc field holds the address of a function the generic layer calls to read data from the file or device and stash it in an internal buffer. It's literally the receiving texts of the channel world. πŸ“₯

InputProc must match this prototype:

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

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

The errorCodePtr argument points to an integer variable that the generic layer provides. If an error happens, set that variable to a POSIX error code identifying the error that occurred. πŸ‘€

Your function should read data from the input device this channel encapsulates and store it at buf. On success, return a nonnegative integer saying how many bytes got read from the input device and stored at buf. On error, return -1. And here's the sad part 😭 β€” if an error occurs after some data has already been read from the device, that data is lost. Gone. No cap.

If inputProc can figure out that the input device has some data available but less than what bufSize asked for, the function should only try 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, 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 return as much data as it can read without blocking. ⏳

Retrieve this value with Tcl_ChannelInputProc, which returns a pointer to the function. ✨


πŸ’œ OUTPUTPROC πŸ’œ

The outputProc field holds the address of a function the generic layer calls to move data from an internal buffer out to the output device. This one's the sending half of the relationship, fr fr. πŸ“€

OutputProc must match this prototype:

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

InstanceData is the same value you passed to Tcl_CreateChannel when the channel was created. The buf argument holds an array of bytes to be written to the device, and toWrite says how many bytes are to be written from buf. πŸ‘‘

The errorCodePtr argument points to an integer variable provided by the generic layer. If an error occurs, set this variable to a POSIX error code identifying that error. 🚩

Your function should write the data at buf to the output device encapsulated by the channel. On success, return a nonnegative integer saying how many bytes were written to the output device. The return value is normally 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, return -1. And heads up: in case of error, some data may have already been written to the device. So like, it's not a clean rollback situation. πŸ™ˆ

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. Periodt. πŸ’…

Retrieve this value with Tcl_ChannelOutputProc, which returns a pointer to the function. 🌸


⭐ WIDESEEKPROC ⭐

The wideSeekProc field holds the address of a function the generic layer calls to move the access point β€” the spot where all your subsequent input and output operations are gonna happen. Think of it as scrolling to a specific timestamp in the group chat. πŸ“

WideSeekProc must match this prototype:

       typedef long long Tcl_DriverWideSeekProc(
               void *instanceData,
               long long offset,
               int seekMode,
               int *errorCodePtr);

The instanceData argument is the same value given to Tcl_CreateChannel when this channel was created. Offset and seekMode mean the same thing they mean 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. Set this variable to a POSIX error code if an error occurs. And if the channel type doesn't implement seeking at all? Store an EINVAL error code. 🚫

The return value is the new access point, or -1 in case of error. And if an error occurred, the function should not move the access point. Stay put, bestie. βœ‹

The wideSseekProc value can be retrieved with Tcl_ChannelWideSeekProc, which returns a pointer to the function. πŸ¦‹


🌈 SETOPTIONPROC 🌈

The setOptionProc field holds the address of a function the generic layer calls to set a channel-type-specific option on a channel. Custom settings, but make it your settings. πŸŽ›οΈ

setOptionProc must match this prototype:

       typedef int Tcl_DriverSetOptionProc(
               void *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 value given to Tcl_CreateChannel when this channel was created. Your 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 β€” for example, -blockmode. Other options are specific to each channel type, and the setOptionProc procedure of the channel driver gets called to implement those. The setOptionProc field can be NULL, which means this channel type supports no type-specific options at all. And that's valid. 🫢

If the option value is successfully modified to the new value, the function returns TCL_OK. It should call Tcl_BadChannelOption β€” which itself returns TCL_ERROR β€” if the optionName is unrecognized. If newValue specifies a value for the option that isn't supported, or if a system call error occurs, the function should leave an error message in the result of interp if interp is not NULL. The function should also call Tcl_SetErrno to store an appropriate POSIX error code. πŸ“Œ

Retrieve this value with Tcl_ChannelSetOptionProc, which returns a pointer to the function. ✨


πŸŽ€ GETOPTIONPROC πŸŽ€

The getOptionProc field holds the address of a function the generic layer calls to get the value of a channel-type-specific option on a channel. It's the reading half of the setOptionProc situationship. πŸ‘€

getOptionProc must match this prototype:

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

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 optionValue. πŸ’œ If optionName is NULL, the function stores 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 the 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. The function should also call Tcl_SetErrno to store an appropriate POSIX error code. 🌟

okay so some options are handled by the generic code, and this function never even gets called to fetch their value β€” -blockmode is the classic example, bestie, it just exists without asking the driver anything. Other options are specific to each channel type, and the getOptionProc procedure of the channel driver gets called to implement those. And plot twist: the getOptionProc field can be NULL, which is the driver's way of saying "this channel type supports no type specific options, don't @ me."

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 that the generic layer calls to set up the whole event-notification situation so it can notice events of interest on this channel. WatchProc should match this prototype, no cap:

typedef void Tcl_DriverWatchProc(
        void *instanceData,
        int mask);

The instanceData is literally 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's telling you which events the caller is interested in noticing on this channel.

So the function should initialize device type specific mechanisms to notice when an event of interest is present on the channel. And when one or more of those designated events pops off, the channel driver is the one responsible for calling Tcl_NotifyChannel to inform the generic channel module. πŸ’…

Careful though: the driver should take care 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 to allow the channel event to be scheduled in sequence with other events. See the description of Tcl_QueueEvent for the details on how to queue an event.

You can retrieve 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 yank a device-specific handle out of the channel. GetHandleProc should match this prototype:

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

InstanceData is the same value 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. Pick your lane.

If the channel implementation has device-specific handles, the function should retrieve the appropriate handle associated with the channel, according to the direction argument. That handle should be stored in the location referred to by handlePtr, and TCL_OK should be returned. But if the channel is not open for the specified direction, or if the channel implementation doesn't use device handles at all, the function should return TCL_ERROR. periodt.

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


✨ FLUSHPROC ✨

The flushProc field is currently reserved for future use β€” it's giving "coming soon, don't worry about it." It should be set to NULL. FlushProc should match this prototype:

typedef int Tcl_DriverFlushProc(
        void *instanceData);

You can retrieve 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 wish to be notified of events that occur on the underlying (stacked) channel. HandlerProc should match this prototype:

typedef int Tcl_DriverHandlerProc(
        void *instanceData,
        int interestMask);

InstanceData is the same value passed to Tcl_CreateChannel when this channel was created. The interestMask is an OR-ed combination of TCL_READABLE or TCL_WRITABLE β€” it indicates what type of event occurred on this channel. πŸ“

You can retrieve 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 going to move to a different thread β€” i.e. whenever thread-specific driver state might have to be initialized or updated. It can be NULL, bestie, no pressure.

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(
        void *instanceData,
        int action);

InstanceData is the same value 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(
        void *instanceData,
        long long length);

InstanceData is the same value 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. It's used by channel drivers when an invalid Set/Get option is requested. Its whole purpose? To concatenate the generic options list to the specific ones and factorize the generic options error message string. Very organized of it, honestly.

It always returns TCL_ERROR. Always. No exceptions, babes.

An error message is 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 good care of inserting minus signs before each option, commas after, and an β€œor” before the last option. It ate and left no crumbs. πŸ‘‘


🌈 SEE ALSO 🌈


πŸŽ€ KEYWORDS πŸŽ€

blocking, channel driver, channel registration, channel type, nonblocking


and that's a wrap on the whole thing, bestie!! 🫢 This page is from Tcl version 8.4, dated with the Tcl_CreateChannel(3) title, and honestly? iconic behavior. Go forth and register those channels. πŸ’–βœ¨πŸ¦‹

πŸ“¦ where this page comes from