Tcl_OpenFileChannel, Tcl_OpenCommandChannel, Tcl_Close, Tcl_Read, Tcl_Gets, Tcl_Write, Tcl_Flush, Tcl_Seek, Tcl_Tell, Tcl_Eof, Tcl_InputBlocked, Tcl_GetChannelOption, Tcl_SetChannelOption - buffered I/O facilities using channels
typedef ... Tcl_Channel;
Tcl_OpenFileChannel(interp, fileName, mode, permissions)
Tcl_OpenCommandChannel(interp, argc, argv, flags)
Tcl_MakeFileChannel(inOsFile, outOsFile, readOrWrite)
Tcl_GetChannel(interp, channelName, modePtr)
Tcl_Read(channel, buf, toRead)
Tcl_Write(channel, buf, toWrite)
Tcl_Seek(channel, offset, seekMode)
Tcl_GetChannelOption(channel, optionName, optionValue)
Tcl_SetChannelOption(interp, channel, optionName, newValue)
Tcl_Interp *interp (in) Used for error reporting and to look up a channel registered in it.
ClientData outOsFile (in) Operating system specific handle for output to a file.
The procedures described in this manual entry comprise the C APIs of the generic layer of the channel architecture. For a description of the channel driver architecture and how to implement channel drivers for new types of channels, see the manual entry for Tcl_CreateChannel.
Tcl_OpenFileChannel opens a file specified by fileName and returns a channel handle that can be used to perform input and output on the file. This API is modelled after the fopen procedure of the Unix standard I/O library. The syntax and meaning of all arguments is similar to those given in the Tcl open command when opening a file. If an error occurs while opening the channel, Tcl_OpenFileChannel returns NULL and records a POSIX error code that can be retrieved with Tcl_GetErrno. In addition, if interp is non-NULL, Tcl_OpenFileChannel leaves an error message in interp>result after any error.
Tcl_OpenCommandChannel provides a C-level interface to the functions of the exec and open commands. It creates a sequence of subprocesses specified by the argv and argc arguments and returns a channel that can be used to communicate with these subprocesses. The flags argument indicates what sort of communication will exist with the command pipeline.
If the TCL_STDIN flag is set then the standard input for the first subprocess will be tied to the channel: writing to the channel will provide input to the subprocess. If TCL_STDIN is not set, then standard input for the first subprocess will be the same as this application's standard input. If TCL_STDOUT is set then standard output from the last subprocess can be read from the channel; otherwise it goes to this application's standard output. If TCL_STDERR is set, standard error output for all subprocesses is returned to the channel and results in an error when the channel is closed; otherwise it goes to this application's standard error. If TCL_ENFORCE_MODE is not set, then argc and argv can redirect the stdio handles to override TCL_STDIN, TCL_STDOUT, and TCL_STDERR; if it is set, then it is an error for argc and argv to override stdio channels for which TCL_STDIN, TCL_STDOUT, and TCL_STDERR have been set.
If an error occurs while opening the channel, Tcl_OpenCommandChannel returns NULL and records a POSIX error code that can be retrieved with Tcl_GetErrno. In addition, Tcl_OpenCommandChannel leaves an error message in interp->result if interp is not NULL.
Tcl_MakeFileChannel makes a Tcl_Channel from an existing, platform-specific, file handle.
Tcl_GetChannel returns a channel given the channelName used to create it with Tcl_CreateChannel and a pointer to a Tcl interpreter in interp. If a channel by that name is not registered in that interpreter, the procedure returns NULL. If the mode argument is not NULL, it points at an integer variable that will receive an OR-ed combination of TCL_READABLE and TCL_WRITABLE describing whether the channel is open for reading and writing.
Tcl_RegisterChannel adds a channel to the set of channels accessible in interp. After this call, Tcl programs executing in that interpreter can refer to the channel in input or output operations using the name given in the call to Tcl_CreateChannel. After this call the channel becomes the property of the interpreter. The caller should not call Tcl_Close for the channel; the channel will be closed automatically when it is unregistered from the interpreter. Furthermore, it is not generally safe to reference the channel anymore, since it could be deleted at any time by a close command in the interpreter.
Tcl_UnregisterChannel removes a channel from the set of channels accessible in interp. After this call, Tcl programs will no longer be able to use the channel's name to refer to the channel in that interpreter. If this operation removed the last registration of the channel in any interpreter, the channel is also closed and destroyed.
Tcl_Close destroys the channel channel, which must denote a currently open channel. The channel should not be registered in any interpreter when Tcl_Close is called; see the manual entry for Tcl_CreateChannel for a description of Tcl_RegisterChannel and Tcl_UnregisterChannel. Buffered output is flushed to the channel's output device prior to destroying the channel, and any buffered input is discarded. If this is a blocking channel, the call does not return until all buffered data is successfully sent to the channel's output device. If this is a nonblocking channel and there is buffered output that cannot be written without blocking, the call returns immediately; output is flushed in the background and the channel will be closed once all of the buffered data has been output. In this case errors during flushing are not reported.
If the channel was closed successfully, Tcl_Close returns TCL_OK. If an error occurs, Tcl_Close returns TCL_ERROR and records a POSIX error code that can be retrieved with Tcl_GetErrno. If the channel is being closed synchronously and an error occurs during closing of the channel and interp is not NULL, an error message is left in interp->result.
Note: it is not safe to call Tcl_Close on a channel that has been registered in an interpreter using Tcl_RegisterChannel; see the documentation for Tcl_RegisterChannel for details.
Tcl_Read consumes up to toRead bytes of data from channel and stores it at buf. The return value of Tcl_Read is the number of characters written at buf. The buffer produced by Tcl_Read is not NULL terminated. Its contents are valid from the zeroth position up to and excluding the position indicated by the return value. If an error occurs, the return value is -1 and Tcl_Read records a POSIX error code that can be retrieved with Tcl_GetErrno.
The return value may be smaller than the value of toRead, indicating that less data than requested was available, also called a short read. In blocking mode, this can only happen on an end-of-file. In nonblocking mode, a short read can also occur if there is not enough input currently available: Tcl_Read returns a short count rather than waiting for more data.
If the channel is in blocking mode, a return value of zero indicates an end of file condition. If the channel is in nonblocking mode, a return value of zero indicates either that no input is currently available or an end of file condition. Use Tcl_Eof and Tcl_InputBlocked to tell which of these conditions actually occurred.
Tcl_Read translates platform-specific end-of-line representations into the canonical \n internal representation according to the current end-of-line recognition mode. Endof-line recognition and the various platform-specific modes are described in the manual entry for the Tcl fconfigure command.
Tcl_Gets reads a line of input from a channel and appends all of the characters of the line except for the terminating end-of-line character(s) to the dynamic string given by dsPtr. The end-of-line character(s) are read and discarded.
If a line was successfully read, the return value is greater than or equal to zero, and it indicates the number of characters stored in the dynamic string. If an error occurs, Tcl_Gets returns -1 and records a POSIX error code that can be retrieved with Tcl_GetErrno. Tcl_Gets also returns -1 if the end of the file is reached; the Tcl_Eof procedure can be used to distinguish an error from an end-of-file condition.
If the channel is in nonblocking mode, the return value can also be -1 if no data was available or the data that was available did not contain an end-of-line character. When -1 is returned, the Tcl_InputBlocked procedure may be invoked to determine if the channel is blocked because of input unavailability.
Tcl_Write accepts toWrite bytes of data at buf for output on channel. This data may not appear on the output device immediately. If the data should appear immediately, call Tcl_Flush after the call to Tcl_Write, or set the -buffering option on the channel to none. If you wish the data to appear as soon as an end of line is accepted for output, set the -buffering option on the channel to line mode.
The toWrite argument specifies how many bytes of data are provided in the buf argument. If it is negative, Tcl_Write expects the data to be NULL terminated and it outputs everything up to the NULL.
The return value of Tcl_Write is a count of how many characters were accepted for output to the channel. This is either equal to toWrite or -1 to indicate that an error occurred. If an error occurs, Tcl_Write also records a POSIX error code that may be retrieved with Tcl_GetErrno.
Newline characters in the output data are translated to platform-specific end-of-line sequences according to the -translation option for the channel.
Tcl_Flush causes all of the buffered output data for channel to be written to its underlying file or device as soon as possible. If the channel is in blocking mode, the call does not return until all the buffered data has been sent to the channel or some error occurred. The call returns immediately if the channel is nonblocking; it starts a background flush that will write the buffered data to the channel eventually, as fast as the channel is able to absorb it.
The return value is normally TCL_OK. If an error occurs, Tcl_Flush returns TCL_ERROR and records a POSIX error code that can be retrieved with Tcl_GetErrno.
Tcl_Seek moves the access point in channel where subsequent data will be read or written. Buffered output is flushed to the channel and buffered input is discarded, prior to the seek operation.
Tcl_Seek normally returns the new access point. If an error occurs, Tcl_Seek returns -1 and records a POSIX error code that can be retrieved with Tcl_GetErrno. After an error, the access point may or may not have been moved.
Tcl_Tell returns the current access point for a channel. The returned value is -1 if the channel does not support seeking.
Tcl_GetChannelOption retrieves, in dsPtr, the value of one of the options currently in effect for a channel, or a list of all options and their values. The channel argument identifies the channel for which to query an option or retrieve all options and their values. If optionName is not NULL, it is the name of the option to query; the option's value is copied to the Tcl dynamic string denoted by optionValue. If optionName is NULL, the function stores an alternating list of option names and their values in optionValue, using a series of calls to Tcl_DStringAppendElement. The various preexisting options and their possible values are described in the manual entry for the Tcl fconfigure command. Other options can be added by each channel type. These channel type specific options are described in the manual entry for the Tcl command that creates a channel of that type; for example, the additional options for TCP based channels are described in the manual entry for the Tcl socket command. The procedure normally returns TCL_OK. If an error occurs, it returns TCL_ERROR and calls Tcl_SetErrno to store an appropriate POSIX error code.
Tcl_SetChannelOption sets a new value for an option on chan_nel. OptionName is the option to set and newValue is the value to set. The procedure normally returns TCL_OK. If an error occurs, it returns TCL_ERROR; in addition, if interp is non-NULL, Tcl_SetChannelOption leaves an error message in interp->result.
Tcl_Eof returns a nonzero value if channel encountered an end of file during the last input operation.
Tcl_InputBlocked returns a nonzero value if channel is in nonblocking mode and the last input operation returned less data than requested because there was insufficient data available. The call always returns zero if the channel is in blocking mode.
Tcl_InputBuffered returns the number of bytes of input currently buffered in the internal buffers for a channel. If the channel is not open for reading, this function always returns zero.
DString(3), fconfigure(n), filename(n), fopen(2), Tcl_CreateChannel(3)
access point, blocking, buffered I/O, channel, channel driver, end of file, flush, input, nonblocking, output, read, seek, write
Table of Contents