Tcl/Tk Documentation > TclLib > OpenFileChnl
- Tcl_OpenFileChannel, Tcl_OpenCommandChannel, Tcl_MakeFileChannel, Tcl_GetChannel, Tcl_GetChannelNames, Tcl_GetChannelNamesEx, Tcl_RegisterChannel, Tcl_UnregisterChannel, Tcl_DetachChannel, Tcl_IsStandardChannel, Tcl_Close, Tcl_ReadChars, Tcl_Read, Tcl_GetsObj, Tcl_Gets, Tcl_WriteObj, Tcl_WriteChars, Tcl_Write, Tcl_Flush, Tcl_Seek, Tcl_Tell, Tcl_TruncateChannel, Tcl_GetChannelOption, Tcl_SetChannelOption, Tcl_Eof, Tcl_InputBlocked, Tcl_InputBuffered, Tcl_OutputBuffered, Tcl_Ungets, Tcl_ReadRaw, Tcl_WriteRaw - buffered I/O facilities using channels
- TCL_READCHARS AND TCL_READ
- TCL_GETSOBJ AND TCL_GETS
- TCL_WRITECHARS, TCL_WRITEOBJ, AND TCL_WRITE
- PLATFORM ISSUES
- SEE ALSO
Tcl_OpenFileChannel(interp, fileName, mode, permissions)
Tcl_OpenCommandChannel(interp, argc, argv, flags)
Tcl_GetChannel(interp, channelName, modePtr)
Tcl_ReadChars(channel, readObjPtr, charsToRead, appendFlag)
Tcl_Read(channel, readBuf, bytesToRead)
Tcl_Ungets(channel, input, inputLen, addAtEnd)
Tcl_WriteChars(channel, charBuf, bytesToWrite)
Tcl_Write(channel, byteBuf, bytesToWrite)
Tcl_ReadRaw(channel, readBuf, bytesToRead)
Tcl_WriteRaw(channel, byteBuf, bytesToWrite)
Tcl_Seek(channel, offset, seekMode)
Tcl_GetChannelOption(interp, 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.
- const char *fileName (in)
- The name of a local or network file.
- const char *mode (in)
- Specifies how the file is to be accessed. May have any of the values allowed for the mode argument to the Tcl open command.
- int permissions (in)
- POSIX-style permission flags such as 0644. If a new file is created, these permissions will be set on the created file.
- int argc (in)
- The number of elements in argv.
- const char **argv (in)
- Arguments for constructing a command pipeline. These values have the same meaning as the non-switch arguments to the Tcl exec command.
- int flags (in)
- Specifies the disposition of the stdio handles in pipeline: OR-ed combination of TCL_STDIN, TCL_STDOUT, TCL_STDERR, and TCL_ENFORCE_MODE. If TCL_STDIN is set, stdin for the first child in the pipe is the pipe channel, otherwise it is the same as the standard input of the invoking process; likewise for TCL_STDOUT and TCL_STDERR. If TCL_ENFORCE_MODE is not set, then the pipe can redirect stdio handles to override the stdio handles for which TCL_STDIN, TCL_STDOUT and TCL_STDERR have been set. If it is set, then such redirections cause an error.
- ClientData handle (in)
- Operating system specific handle for I/O to a file. For Unix this is a file descriptor, for Windows it is a HANDLE.
- int readOrWrite (in)
- OR-ed combination of TCL_READABLE and TCL_WRITABLE to indicate what operations are valid on handle.
- const char *channelName (in)
- The name of the channel.
- int *modePtr (out)
- Points at an integer variable that will receive an OR-ed combination of TCL_READABLE and TCL_WRITABLE denoting whether the channel is open for reading and writing.
- const char *pattern (in)
- The pattern to match on, passed to Tcl_StringMatch, or NULL.
- Tcl_Channel channel (in)
- A Tcl channel for input or output. Must have been the return value from a procedure such as Tcl_OpenFileChannel.
- Tcl_Obj *readObjPtr (in/out)
- A pointer to a Tcl Object in which to store the characters read from the channel.
- int charsToRead (in)
- The number of characters to read from the channel. If the channel's encoding is binary, this is equivalent to the number of bytes to read from the channel.
- int appendFlag (in)
- If non-zero, data read from the channel will be appended to the object. Otherwise, the data will replace the existing contents of the object.
- char *readBuf (out)
- A buffer in which to store the bytes read from the channel.
- int bytesToRead (in)
- The number of bytes to read from the channel. The buffer readBuf must be large enough to hold this many bytes.
- Tcl_Obj *lineObjPtr (in/out)
- A pointer to a Tcl object in which to store the line read from the channel. The line read will be appended to the current value of the object.
- Tcl_DString *lineRead (in/out)
- A pointer to a Tcl dynamic string in which to store the line read from the channel. Must have been initialized by the caller. The line read will be appended to any data already in the dynamic string.
- const char *input (in)
- The input to add to a channel buffer.
- int inputLen (in)
- Length of the input
- int addAtEnd (in)
- Flag indicating whether the input should be added to the end or beginning of the channel buffer.
- Tcl_Obj *writeObjPtr (in)
- A pointer to a Tcl Object whose contents will be output to the channel.
- const char *charBuf (in)
- A buffer containing the characters to output to the channel.
- const char *byteBuf (in)
- A buffer containing the bytes to output to the channel.
- int bytesToWrite (in)
- The number of bytes to consume from charBuf or byteBuf and output to the channel.
- Tcl_WideInt offset (in)
- How far to move the access point in the channel at which the next input or output operation will be applied, measured in bytes from the position given by seekMode. May be either positive or negative.
- int seekMode (in)
- Relative to which point to seek; used with offset to calculate the new access point for the channel. Legal values are SEEK_SET, SEEK_CUR, and SEEK_END.
- Tcl_WideInt length (in)
- The (non-negative) length to truncate the channel the channel to.
- const char *optionName (in)
- The name of an option applicable to this channel, such as -blocking. May have any of the values accepted by the fconfigure command.
- Tcl_DString *optionValue (in)
- Where to store the value of an option or a list of all options and their values. Must have been initialized by the caller.
- const char *newValue (in)
- New value for the option given by optionName.
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.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's result after any error. As of Tcl 8.4, the object-based API Tcl_FSOpenFileChannel should be used in preference to Tcl_OpenFileChannel wherever possible.
The newly created channel is not registered in the supplied interpreter; to register it, use Tcl_RegisterChannel, described below. If one of the standard channels, stdin, stdout or stderr was previously closed, the act of creating the new channel also assigns it as a replacement for the standard channel.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 the interpreter's result if interp is not NULL.
The newly created channel is not registered in the supplied interpreter; to register it, use Tcl_RegisterChannel, described below. If one of the standard channels, stdin, stdout or stderr was previously closed, the act of creating the new channel also assigns it as a replacement for the standard channel.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 modePtr 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_GetChannelNames and Tcl_GetChannelNamesEx write the names of the registered channels to the interpreter's result as a list object. Tcl_GetChannelNamesEx will filter these names according to the pattern. If pattern is NULL, then it will not do any filtering. The return value is TCL_OK if no errors occurred writing to the result, otherwise it is TCL_ERROR, and the error message is left in the interpreter's result.Tcl_CreateChannel. After this call, the channel becomes the property of the interpreter, and the caller should not call Tcl_Close for the channel; the channel will be closed automatically when it is unregistered from the interpreter.
Code executing outside of any Tcl interpreter can call Tcl_RegisterChannel with interp as NULL, to indicate that it wishes to hold a reference to this channel. Subsequently, the channel can be registered in a Tcl interpreter and it will only be closed when the matching number of calls to Tcl_UnregisterChannel have been made. This allows code executing outside of any interpreter to safely hold a reference to a channel that is also registered in a Tcl interpreter.
This procedure interacts with the code managing the standard channels. If no standard channels were initialized before the first call to Tcl_RegisterChannel, they will get initialized by that call. See Tcl_StandardChannels for a general treatise about standard channels and the behaviour of the Tcl library with regard to them.
Code not associated with a Tcl interpreter can call Tcl_UnregisterChannel with interp as NULL, to indicate to Tcl that it no longer holds a reference to that channel. If this is the last reference to the channel, it will now be closed. Tcl_UnregisterChannel is very similar to Tcl_DetachChannel except that it will also close the channel if no further references to it exist.
Code not associated with a Tcl interpreter can call Tcl_DetachChannel with interp as NULL, to indicate to Tcl that it no longer holds a reference to that channel. If this is the last reference to the channel, unlike Tcl_UnregisterChannel, it will not be closed.
No attempt is made to check whether the given channel or the standard channels are initialized or otherwise valid.
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 the interpreter's result.
Note: it is not safe to call Tcl_Close on a channel that has been registered using Tcl_RegisterChannel; see the documentation for Tcl_RegisterChannel, above, for details. If the channel has ever been given as the chan argument in a call to Tcl_RegisterChannel, you should instead use Tcl_UnregisterChannel, which will internally call Tcl_Close when all calls to Tcl_RegisterChannel have been matched by corresponding calls to Tcl_UnregisterChannel.Tcl_GetErrno.
Setting charsToRead to -1 will cause the command to read all characters currently available (non-blocking) or everything until eof (blocking mode).
The return value may be smaller than the value to read, indicating that less data than requested was available. This is 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_ReadChars 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_ReadChars translates the various end-of-line representations into the canonical \n internal representation according to the current end-of-line recognition mode. End-of-line recognition and the various platform-specific modes are described in the manual entry for the Tcl fconfigure command.
As a performance optimization, when reading from a channel with the encoding binary, the bytes are not converted to UTF-8 as they are read. Instead, they are stored in readObjPtr's internal representation as a byte-array object. The string representation of this object will only be constructed if it is needed (e.g., because of a call to Tcl_GetStringFromObj). In this way, byte-oriented data can be read from a channel, manipulated by calling Tcl_GetByteArrayFromObj and related functions, and then written to a channel without the expense of ever converting to or from UTF-8.
Tcl_Read is similar to Tcl_ReadChars, except that it does not do encoding conversions, regardless of the channel's encoding. It is deprecated and exists for backwards compatibility with non-internationalized Tcl extensions. It consumes bytes from channel and stores them in readBuf, performing end-of-line translations on the way. The return value of Tcl_Read is the number of bytes, up to bytesToRead, written in readBuf. 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.
Tcl_ReadRaw is the same as Tcl_Read but does not compensate for stacking. While Tcl_Read (and the other functions in the API) always get their data from the topmost channel in the stack the supplied channel is part of, Tcl_ReadRaw does not. Thus this function is only usable for transformational channel drivers, i.e. drivers used in the middle of a stack of channels, to move data from the channel below into the transformation.binary, each byte read from the channel is treated as an individual Unicode character. All of the characters of the line except for the terminating end-of-line character(s) are appended to lineObjPtr's string representation. 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 indicates the number of bytes stored in lineObjPtr. If an error occurs, Tcl_GetsObj returns -1 and records a POSIX error code that can be retrieved with Tcl_GetErrno. Tcl_GetsObj 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_Gets is the same as Tcl_GetsObj except the resulting characters are appended to the dynamic string given by lineRead rather than a Tcl object.
Data queued for output may not appear on the output device immediately, due to internal buffering. If the data should appear immediately, call Tcl_Flush after the call to Tcl_WriteChars, or set the -buffering option on the channel to none. If you wish the data to appear as soon as a complete line is accepted for output, set the -buffering option on the channel to line mode.
The return value of Tcl_WriteChars is a count of how many bytes were accepted for output to the channel. This is either greater than zero to indicate success or -1 to indicate that an error occurred. If an error occurs, Tcl_WriteChars 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. This is done even if the channel has no encoding.
Tcl_WriteObj is similar to Tcl_WriteChars except it accepts a Tcl object whose contents will be output to the channel. The UTF-8 characters in writeObjPtr's string representation are converted to the channel's encoding and queued for output to channel. As a performance optimization, when writing to a channel with the encoding binary, UTF-8 characters are not converted as they are written. Instead, the bytes in writeObjPtr's internal representation as a byte-array object are written to the channel. The byte-array representation of the object will be constructed if it is needed. In this way, byte-oriented data can be read from a channel, manipulated by calling Tcl_GetByteArrayFromObj and related functions, and then written to a channel without the expense of ever converting to or from UTF-8.
Tcl_Write is similar to Tcl_WriteChars except that it does not do encoding conversions, regardless of the channel's encoding. It is deprecated and exists for backwards compatibility with non-internationalized Tcl extensions. It accepts bytesToWrite bytes of data at byteBuf and queues them for output to channel. If bytesToWrite is negative, Tcl_Write expects byteBuf to be null-terminated and it outputs everything up to the null.
Tcl_WriteRaw is the same as Tcl_Write but does not compensate for stacking. While Tcl_Write (and the other functions in the API) always feed their input to the topmost channel in the stack the supplied channel is part of, Tcl_WriteRaw does not. Thus this function is only usable for transformational channel drivers, i.e. drivers used in the middle of a stack of channels, to move data from the transformation into the channel below 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 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_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_GetChannelHandle depend on the platform and the channel type. On Unix platforms, the handle is always a Unix file descriptor as returned from the open system call. On Windows platforms, the handle is a file HANDLE when the channel was created with Tcl_OpenFileChannel, Tcl_OpenCommandChannel, or Tcl_MakeFileChannel. Other channel types may return a different type of handle on Windows platforms. fconfigure, filename, fopen, Tcl_CreateChannel access point, blocking, buffered I/O, channel, channel driver, end of file, flush, input, nonblocking, output, read, seek, write