| 1 |
'\" |
| 2 |
'\" Copyright (c) 1996-1997 Sun Microsystems, Inc. |
| 3 |
'\" |
| 4 |
'\" See the file "license.terms" for information on usage and redistribution |
| 5 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 6 |
'\" |
| 7 |
.TH Tcl_OpenFileChannel 3 8.3 Tcl "Tcl Library Procedures" |
| 8 |
.so man.macros |
| 9 |
.BS |
| 10 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 11 |
.SH NAME |
| 12 |
Tcl_OpenFileChannel, Tcl_OpenCommandChannel, Tcl_MakeFileChannel, Tcl_GetChannel, Tcl_GetChannelNames, Tcl_GetChannelNamesEx, Tcl_RegisterChannel, Tcl_UnregisterChannel, Tcl_DetachChannel, Tcl_IsStandardChannel, Tcl_Close, Tcl_CloseEx, 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 |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
\fB#include <tcl.h>\fR |
| 16 |
.sp |
| 17 |
Tcl_Channel |
| 18 |
\fBTcl_OpenFileChannel\fR(\fIinterp, fileName, mode, permissions\fR) |
| 19 |
.sp |
| 20 |
Tcl_Channel |
| 21 |
\fBTcl_OpenCommandChannel\fR(\fIinterp, argc, argv, flags\fR) |
| 22 |
.sp |
| 23 |
Tcl_Channel |
| 24 |
\fBTcl_MakeFileChannel\fR(\fIhandle, readOrWrite\fR) |
| 25 |
.sp |
| 26 |
Tcl_Channel |
| 27 |
\fBTcl_GetChannel\fR(\fIinterp, channelName, modePtr\fR) |
| 28 |
.sp |
| 29 |
int |
| 30 |
\fBTcl_GetChannelNames\fR(\fIinterp\fR) |
| 31 |
.sp |
| 32 |
int |
| 33 |
\fBTcl_GetChannelNamesEx\fR(\fIinterp, pattern\fR) |
| 34 |
.sp |
| 35 |
void |
| 36 |
\fBTcl_RegisterChannel\fR(\fIinterp, channel\fR) |
| 37 |
.sp |
| 38 |
int |
| 39 |
\fBTcl_UnregisterChannel\fR(\fIinterp, channel\fR) |
| 40 |
.sp |
| 41 |
int |
| 42 |
\fBTcl_DetachChannel\fR(\fIinterp, channel\fR) |
| 43 |
.sp |
| 44 |
int |
| 45 |
\fBTcl_IsStandardChannel\fR(\fIchannel\fR) |
| 46 |
.sp |
| 47 |
int |
| 48 |
\fBTcl_Close\fR(\fIinterp, channel\fR) |
| 49 |
.sp |
| 50 |
int |
| 51 |
\fBTcl_CloseEx\fR(\fIinterp, channel, closeFlags\fR) |
| 52 |
.sp |
| 53 |
int |
| 54 |
\fBTcl_ReadChars\fR(\fIchannel, readObjPtr, charsToRead, appendFlag\fR) |
| 55 |
.sp |
| 56 |
int |
| 57 |
\fBTcl_Read\fR(\fIchannel, readBuf, bytesToRead\fR) |
| 58 |
.sp |
| 59 |
int |
| 60 |
\fBTcl_GetsObj\fR(\fIchannel, lineObjPtr\fR) |
| 61 |
.sp |
| 62 |
int |
| 63 |
\fBTcl_Gets\fR(\fIchannel, lineRead\fR) |
| 64 |
.sp |
| 65 |
int |
| 66 |
\fBTcl_Ungets\fR(\fIchannel, input, inputLen, addAtEnd\fR) |
| 67 |
.sp |
| 68 |
int |
| 69 |
\fBTcl_WriteObj\fR(\fIchannel, writeObjPtr\fR) |
| 70 |
.sp |
| 71 |
int |
| 72 |
\fBTcl_WriteChars\fR(\fIchannel, charBuf, bytesToWrite\fR) |
| 73 |
.sp |
| 74 |
int |
| 75 |
\fBTcl_Write\fR(\fIchannel, byteBuf, bytesToWrite\fR) |
| 76 |
.sp |
| 77 |
int |
| 78 |
\fBTcl_ReadRaw\fR(\fIchannel, readBuf, bytesToRead\fR) |
| 79 |
.sp |
| 80 |
int |
| 81 |
\fBTcl_WriteRaw\fR(\fIchannel, byteBuf, bytesToWrite\fR) |
| 82 |
.sp |
| 83 |
int |
| 84 |
\fBTcl_Eof\fR(\fIchannel\fR) |
| 85 |
.sp |
| 86 |
int |
| 87 |
\fBTcl_Flush\fR(\fIchannel\fR) |
| 88 |
.sp |
| 89 |
int |
| 90 |
\fBTcl_InputBlocked\fR(\fIchannel\fR) |
| 91 |
.sp |
| 92 |
int |
| 93 |
\fBTcl_InputBuffered\fR(\fIchannel\fR) |
| 94 |
.sp |
| 95 |
int |
| 96 |
\fBTcl_OutputBuffered\fR(\fIchannel\fR) |
| 97 |
.sp |
| 98 |
Tcl_WideInt |
| 99 |
\fBTcl_Seek\fR(\fIchannel, offset, seekMode\fR) |
| 100 |
.sp |
| 101 |
Tcl_WideInt |
| 102 |
\fBTcl_Tell\fR(\fIchannel\fR) |
| 103 |
.sp |
| 104 |
int |
| 105 |
\fBTcl_TruncateChannel\fR(\fIchannel, length\fR) |
| 106 |
.sp |
| 107 |
int |
| 108 |
\fBTcl_GetChannelOption\fR(\fIinterp, channel, optionName, optionValue\fR) |
| 109 |
.sp |
| 110 |
int |
| 111 |
\fBTcl_SetChannelOption\fR(\fIinterp, channel, optionName, newValue\fR) |
| 112 |
.sp |
| 113 |
.SH ARGUMENTS |
| 114 |
.AS Tcl_DString *channelName in/out |
| 115 |
.AP Tcl_Interp *interp in |
| 116 |
Used for error reporting and to look up a channel registered in it. |
| 117 |
.AP "const char" *fileName in |
| 118 |
The name of a local or network file. |
| 119 |
.AP "const char" *mode in |
| 120 |
Specifies how the file is to be accessed. May have any of the values |
| 121 |
allowed for the \fImode\fR argument to the Tcl \fBopen\fR command. |
| 122 |
.AP int permissions in |
| 123 |
POSIX-style permission flags such as 0644. If a new file is created, these |
| 124 |
permissions will be set on the created file. |
| 125 |
.AP int argc in |
| 126 |
The number of elements in \fIargv\fR. |
| 127 |
.AP "const char" **argv in |
| 128 |
Arguments for constructing a command pipeline. These values have the same |
| 129 |
meaning as the non-switch arguments to the Tcl \fBexec\fR command. |
| 130 |
.AP int flags in |
| 131 |
Specifies the disposition of the stdio handles in pipeline: OR-ed |
| 132 |
combination of \fBTCL_STDIN\fR, \fBTCL_STDOUT\fR, \fBTCL_STDERR\fR, and |
| 133 |
\fBTCL_ENFORCE_MODE\fR. If \fBTCL_STDIN\fR is set, stdin for the first child |
| 134 |
in the pipe is the pipe channel, otherwise it is the same as the standard |
| 135 |
input of the invoking process; likewise for \fBTCL_STDOUT\fR and |
| 136 |
\fBTCL_STDERR\fR. If \fBTCL_ENFORCE_MODE\fR is not set, then the pipe can |
| 137 |
redirect stdio handles to override the stdio handles for which |
| 138 |
\fBTCL_STDIN\fR, \fBTCL_STDOUT\fR and \fBTCL_STDERR\fR have been set. If it |
| 139 |
is set, then such redirections cause an error. |
| 140 |
.AP ClientData handle in |
| 141 |
Operating system specific handle for I/O to a file. For Unix this is a |
| 142 |
file descriptor, for Windows it is a HANDLE. |
| 143 |
.AP int readOrWrite in |
| 144 |
OR-ed combination of \fBTCL_READABLE\fR and \fBTCL_WRITABLE\fR to indicate |
| 145 |
what operations are valid on \fIhandle\fR. |
| 146 |
.AP "const char" *channelName in |
| 147 |
The name of the channel. |
| 148 |
.AP int *modePtr out |
| 149 |
Points at an integer variable that will receive an OR-ed combination of |
| 150 |
\fBTCL_READABLE\fR and \fBTCL_WRITABLE\fR denoting whether the channel is |
| 151 |
open for reading and writing. |
| 152 |
.AP "const char" *pattern in |
| 153 |
The pattern to match on, passed to Tcl_StringMatch, or NULL. |
| 154 |
.AP Tcl_Channel channel in |
| 155 |
A Tcl channel for input or output. Must have been the return value |
| 156 |
from a procedure such as \fBTcl_OpenFileChannel\fR. |
| 157 |
.AP Tcl_Obj *readObjPtr in/out |
| 158 |
A pointer to a Tcl value in which to store the characters read from the |
| 159 |
channel. |
| 160 |
.AP int charsToRead in |
| 161 |
The number of characters to read from the channel. If the channel's encoding |
| 162 |
is \fBbinary\fR, this is equivalent to the number of bytes to read from the |
| 163 |
channel. |
| 164 |
.AP int closeFlags in |
| 165 |
If \fB0\fR, the channel is closed in both directions. If \fBTCL_CLOSE_READ\fR, |
| 166 |
the channel is only closed for reading. If \fBTCL_CLOSE_WRITE\fR, the channel |
| 167 |
is only closed for writing. These flags must not be combined. |
| 168 |
.AP int appendFlag in |
| 169 |
If non-zero, data read from the channel will be appended to the value. |
| 170 |
Otherwise, the data will replace the existing contents of the value. |
| 171 |
.AP char *readBuf out |
| 172 |
A buffer in which to store the bytes read from the channel. |
| 173 |
.AP int bytesToRead in |
| 174 |
The number of bytes to read from the channel. The buffer \fIreadBuf\fR must |
| 175 |
be large enough to hold this many bytes. |
| 176 |
.AP Tcl_Obj *lineObjPtr in/out |
| 177 |
A pointer to a Tcl value in which to store the line read from the |
| 178 |
channel. The line read will be appended to the current value of the |
| 179 |
value. |
| 180 |
.AP Tcl_DString *lineRead in/out |
| 181 |
A pointer to a Tcl dynamic string in which to store the line read from the |
| 182 |
channel. Must have been initialized by the caller. The line read will be |
| 183 |
appended to any data already in the dynamic string. |
| 184 |
.AP "const char" *input in |
| 185 |
The input to add to a channel buffer. |
| 186 |
.AP int inputLen in |
| 187 |
Length of the input |
| 188 |
.AP int addAtEnd in |
| 189 |
Flag indicating whether the input should be added to the end or |
| 190 |
beginning of the channel buffer. |
| 191 |
.AP Tcl_Obj *writeObjPtr in |
| 192 |
A pointer to a Tcl value whose contents will be output to the channel. |
| 193 |
.AP "const char" *charBuf in |
| 194 |
A buffer containing the characters to output to the channel. |
| 195 |
.AP "const char" *byteBuf in |
| 196 |
A buffer containing the bytes to output to the channel. |
| 197 |
.AP int bytesToWrite in |
| 198 |
The number of bytes to consume from \fIcharBuf\fR or \fIbyteBuf\fR and |
| 199 |
output to the channel. |
| 200 |
.AP Tcl_WideInt offset in |
| 201 |
How far to move the access point in the channel at which the next input or |
| 202 |
output operation will be applied, measured in bytes from the position |
| 203 |
given by \fIseekMode\fR. May be either positive or negative. |
| 204 |
.AP int seekMode in |
| 205 |
Relative to which point to seek; used with \fIoffset\fR to calculate the new |
| 206 |
access point for the channel. Legal values are \fBSEEK_SET\fR, |
| 207 |
\fBSEEK_CUR\fR, and \fBSEEK_END\fR. |
| 208 |
.AP Tcl_WideInt length in |
| 209 |
The (non-negative) length to truncate the channel the channel to. |
| 210 |
.AP "const char" *optionName in |
| 211 |
The name of an option applicable to this channel, such as \fB\-blocking\fR. |
| 212 |
May have any of the values accepted by the \fBfconfigure\fR command. |
| 213 |
.AP Tcl_DString *optionValue in |
| 214 |
Where to store the value of an option or a list of all options and their |
| 215 |
values. Must have been initialized by the caller. |
| 216 |
.AP "const char" *newValue in |
| 217 |
New value for the option given by \fIoptionName\fR. |
| 218 |
.BE |
| 219 |
.SH DESCRIPTION |
| 220 |
.PP |
| 221 |
The Tcl channel mechanism provides a device-independent and |
| 222 |
platform-independent mechanism for performing buffered input |
| 223 |
and output operations on a variety of file, socket, and device |
| 224 |
types. |
| 225 |
The channel mechanism is extensible to new channel types, by |
| 226 |
providing a low-level channel driver for the new type; the channel driver |
| 227 |
interface is described in the manual entry for \fBTcl_CreateChannel\fR. The |
| 228 |
channel mechanism provides a buffering scheme modeled after |
| 229 |
Unix's standard I/O, and it also allows for nonblocking I/O on |
| 230 |
channels. |
| 231 |
.PP |
| 232 |
The procedures described in this manual entry comprise the C APIs of the |
| 233 |
generic layer of the channel architecture. For a description of the channel |
| 234 |
driver architecture and how to implement channel drivers for new types of |
| 235 |
channels, see the manual entry for \fBTcl_CreateChannel\fR. |
| 236 |
.SH TCL_OPENFILECHANNEL |
| 237 |
.PP |
| 238 |
\fBTcl_OpenFileChannel\fR opens a file specified by \fIfileName\fR and |
| 239 |
returns a channel handle that can be used to perform input and output on |
| 240 |
the file. This API is modeled after the \fBfopen\fR procedure of |
| 241 |
the Unix standard I/O library. |
| 242 |
The syntax and meaning of all arguments is similar to those |
| 243 |
given in the Tcl \fBopen\fR command when opening a file. |
| 244 |
If an error occurs while opening the channel, \fBTcl_OpenFileChannel\fR |
| 245 |
returns NULL and records a POSIX error code that can be |
| 246 |
retrieved with \fBTcl_GetErrno\fR. |
| 247 |
In addition, if \fIinterp\fR is non-NULL, \fBTcl_OpenFileChannel\fR |
| 248 |
leaves an error message in \fIinterp\fR's result after any error. |
| 249 |
As of Tcl 8.4, the value-based API \fBTcl_FSOpenFileChannel\fR should |
| 250 |
be used in preference to \fBTcl_OpenFileChannel\fR wherever possible. |
| 251 |
.PP |
| 252 |
The newly created channel is not registered in the supplied interpreter; to |
| 253 |
register it, use \fBTcl_RegisterChannel\fR, described below. |
| 254 |
If one of the standard channels, \fBstdin\fR, \fBstdout\fR or \fBstderr\fR was |
| 255 |
previously closed, the act of creating the new channel also assigns it as a |
| 256 |
replacement for the standard channel. |
| 257 |
.SH TCL_OPENCOMMANDCHANNEL |
| 258 |
.PP |
| 259 |
\fBTcl_OpenCommandChannel\fR provides a C-level interface to the |
| 260 |
functions of the \fBexec\fR and \fBopen\fR commands. |
| 261 |
It creates a sequence of subprocesses specified |
| 262 |
by the \fIargv\fR and \fIargc\fR arguments and returns a channel that can |
| 263 |
be used to communicate with these subprocesses. |
| 264 |
The \fIflags\fR argument indicates what sort of communication will |
| 265 |
exist with the command pipeline. |
| 266 |
.PP |
| 267 |
If the \fBTCL_STDIN\fR flag is set then the standard input for the |
| 268 |
first subprocess will be tied to the channel: writing to the channel |
| 269 |
will provide input to the subprocess. If \fBTCL_STDIN\fR is not set, |
| 270 |
then standard input for the first subprocess will be the same as this |
| 271 |
application's standard input. If \fBTCL_STDOUT\fR is set then |
| 272 |
standard output from the last subprocess can be read from the channel; |
| 273 |
otherwise it goes to this application's standard output. If |
| 274 |
\fBTCL_STDERR\fR is set, standard error output for all subprocesses is |
| 275 |
returned to the channel and results in an error when the channel is |
| 276 |
closed; otherwise it goes to this application's standard error. If |
| 277 |
\fBTCL_ENFORCE_MODE\fR is not set, then \fIargc\fR and \fIargv\fR can |
| 278 |
redirect the stdio handles to override \fBTCL_STDIN\fR, |
| 279 |
\fBTCL_STDOUT\fR, and \fBTCL_STDERR\fR; if it is set, then it is an |
| 280 |
error for argc and argv to override stdio channels for which |
| 281 |
\fBTCL_STDIN\fR, \fBTCL_STDOUT\fR, and \fBTCL_STDERR\fR have been set. |
| 282 |
.PP |
| 283 |
If an error occurs while opening the channel, \fBTcl_OpenCommandChannel\fR |
| 284 |
returns NULL and records a POSIX error code that can be retrieved with |
| 285 |
\fBTcl_GetErrno\fR. |
| 286 |
In addition, \fBTcl_OpenCommandChannel\fR leaves an error message in |
| 287 |
the interpreter's result. \fIinterp\fR cannot be NULL. |
| 288 |
.PP |
| 289 |
The newly created channel is not registered in the supplied interpreter; to |
| 290 |
register it, use \fBTcl_RegisterChannel\fR, described below. |
| 291 |
If one of the standard channels, \fBstdin\fR, \fBstdout\fR or \fBstderr\fR was |
| 292 |
previously closed, the act of creating the new channel also assigns it as a |
| 293 |
replacement for the standard channel. |
| 294 |
.SH TCL_MAKEFILECHANNEL |
| 295 |
.PP |
| 296 |
\fBTcl_MakeFileChannel\fR makes a \fBTcl_Channel\fR from an existing, |
| 297 |
platform-specific, file handle. |
| 298 |
The newly created channel is not registered in the supplied interpreter; to |
| 299 |
register it, use \fBTcl_RegisterChannel\fR, described below. |
| 300 |
If one of the standard channels, \fBstdin\fR, \fBstdout\fR or \fBstderr\fR was |
| 301 |
previously closed, the act of creating the new channel also assigns it as a |
| 302 |
replacement for the standard channel. |
| 303 |
.SH TCL_GETCHANNEL |
| 304 |
.PP |
| 305 |
\fBTcl_GetChannel\fR returns a channel given the \fIchannelName\fR used to |
| 306 |
create it with \fBTcl_CreateChannel\fR and a pointer to a Tcl interpreter in |
| 307 |
\fIinterp\fR. If a channel by that name is not registered in that interpreter, |
| 308 |
the procedure returns NULL. If the \fImodePtr\fR argument is not NULL, it |
| 309 |
points at an integer variable that will receive an OR-ed combination of |
| 310 |
\fBTCL_READABLE\fR and \fBTCL_WRITABLE\fR describing whether the channel is |
| 311 |
open for reading and writing. |
| 312 |
.PP |
| 313 |
\fBTcl_GetChannelNames\fR and \fBTcl_GetChannelNamesEx\fR write the |
| 314 |
names of the registered channels to the interpreter's result as a |
| 315 |
list value. \fBTcl_GetChannelNamesEx\fR will filter these names |
| 316 |
according to the \fIpattern\fR. If \fIpattern\fR is NULL, then it |
| 317 |
will not do any filtering. The return value is \fBTCL_OK\fR if no |
| 318 |
errors occurred writing to the result, otherwise it is \fBTCL_ERROR\fR, |
| 319 |
and the error message is left in the interpreter's result. |
| 320 |
.SH TCL_REGISTERCHANNEL |
| 321 |
.PP |
| 322 |
\fBTcl_RegisterChannel\fR adds a channel to the set of channels accessible |
| 323 |
in \fIinterp\fR. After this call, Tcl programs executing in that |
| 324 |
interpreter can refer to the channel in input or output operations using |
| 325 |
the name given in the call to \fBTcl_CreateChannel\fR. After this call, |
| 326 |
the channel becomes the property of the interpreter, and the caller should |
| 327 |
not call \fBTcl_Close\fR for the channel; the channel will be closed |
| 328 |
automatically when it is unregistered from the interpreter. |
| 329 |
.PP |
| 330 |
Code executing outside of any Tcl interpreter can call |
| 331 |
\fBTcl_RegisterChannel\fR with \fIinterp\fR as NULL, to indicate that it |
| 332 |
wishes to hold a reference to this channel. Subsequently, the channel can |
| 333 |
be registered in a Tcl interpreter and it will only be closed when the |
| 334 |
matching number of calls to \fBTcl_UnregisterChannel\fR have been made. |
| 335 |
This allows code executing outside of any interpreter to safely hold a |
| 336 |
reference to a channel that is also registered in a Tcl interpreter. |
| 337 |
.PP |
| 338 |
This procedure interacts with the code managing the standard |
| 339 |
channels. If no standard channels were initialized before the first |
| 340 |
call to \fBTcl_RegisterChannel\fR, they will get initialized by that |
| 341 |
call. See \fBTcl_StandardChannels\fR for a general treatise about |
| 342 |
standard channels and the behavior of the Tcl library with regard to |
| 343 |
them. |
| 344 |
.SH TCL_UNREGISTERCHANNEL |
| 345 |
.PP |
| 346 |
\fBTcl_UnregisterChannel\fR removes a channel from the set of channels |
| 347 |
accessible in \fIinterp\fR. After this call, Tcl programs will no longer be |
| 348 |
able to use the channel's name to refer to the channel in that interpreter. |
| 349 |
If this operation removed the last registration of the channel in any |
| 350 |
interpreter, the channel is also closed and destroyed. |
| 351 |
.PP |
| 352 |
Code not associated with a Tcl interpreter can call |
| 353 |
\fBTcl_UnregisterChannel\fR with \fIinterp\fR as NULL, to indicate to Tcl |
| 354 |
that it no longer holds a reference to that channel. If this is the last |
| 355 |
reference to the channel, it will now be closed. \fBTcl_UnregisterChannel\fR |
| 356 |
is very similar to \fBTcl_DetachChannel\fR except that it will also |
| 357 |
close the channel if no further references to it exist. |
| 358 |
.SH TCL_DETACHCHANNEL |
| 359 |
.PP |
| 360 |
\fBTcl_DetachChannel\fR removes a channel from the set of channels |
| 361 |
accessible in \fIinterp\fR. After this call, Tcl programs will no longer be |
| 362 |
able to use the channel's name to refer to the channel in that interpreter. |
| 363 |
Beyond that, this command has no further effect. It cannot be used on |
| 364 |
the standard channels (\fBstdout\fR, \fBstderr\fR, \fBstdin\fR), and will return |
| 365 |
\fBTCL_ERROR\fR if passed one of those channels. |
| 366 |
.PP |
| 367 |
Code not associated with a Tcl interpreter can call |
| 368 |
\fBTcl_DetachChannel\fR with \fIinterp\fR as NULL, to indicate to Tcl |
| 369 |
that it no longer holds a reference to that channel. If this is the last |
| 370 |
reference to the channel, unlike \fBTcl_UnregisterChannel\fR, |
| 371 |
it will not be closed. |
| 372 |
.SH TCL_ISSTANDARDCHANNEL |
| 373 |
.PP |
| 374 |
\fBTcl_IsStandardChannel\fR tests whether a channel is one of the |
| 375 |
three standard channels, \fBstdin\fR, \fBstdout\fR or \fBstderr\fR. |
| 376 |
If so, it returns 1, otherwise 0. |
| 377 |
.PP |
| 378 |
No attempt is made to check whether the given channel or the standard |
| 379 |
channels are initialized or otherwise valid. |
| 380 |
.SH "TCL_CLOSE AND TCL_CLOSEEX" |
| 381 |
.PP |
| 382 |
\fBTcl_Close\fR destroys the channel \fIchannel\fR, which must denote a |
| 383 |
currently open channel. The channel should not be registered in any |
| 384 |
interpreter when \fBTcl_Close\fR is called. Buffered output is flushed to |
| 385 |
the channel's output device prior to destroying the channel, and any |
| 386 |
buffered input is discarded. If this is a blocking channel, the call does |
| 387 |
not return until all buffered data is successfully sent to the channel's |
| 388 |
output device. If this is a nonblocking channel and there is buffered |
| 389 |
output that cannot be written without blocking, the call returns |
| 390 |
immediately; output is flushed in the background and the channel will be |
| 391 |
closed once all of the buffered data has been output. In this case errors |
| 392 |
during flushing are not reported. |
| 393 |
.PP |
| 394 |
\fBTcl_CloseEx\fR allows for both full closing and half-closing of channels |
| 395 |
depending on its \fBcloseFlags\fR parameter. See the description of the |
| 396 |
parameter above. It is an error to attempt to close the channel for |
| 397 |
a direction for which it is not open. The channel is destroyed only when |
| 398 |
it has been closed for both reading and writing. Only socket and command |
| 399 |
pipe channels support half-closing. |
| 400 |
.PP |
| 401 |
If the channel was closed successfully, \fBTcl_Close\fR and \fBTcl_CloseEx\fR |
| 402 |
return \fBTCL_OK\fR. If an error occurs, they return \fBTCL_ERROR\fR and record |
| 403 |
a POSIX error code that can be retrieved with \fBTcl_GetErrno\fR. |
| 404 |
If the channel is being closed synchronously and an error occurs during |
| 405 |
closing of the channel and \fIinterp\fR is not NULL, an error message is |
| 406 |
left in the interpreter's result. |
| 407 |
.PP |
| 408 |
Note: it is not safe to call the channel closing functions on a channel |
| 409 |
that has been registered using \fBTcl_RegisterChannel\fR; see the documentation |
| 410 |
for \fBTcl_RegisterChannel\fR, above, for details. If the channel has ever |
| 411 |
been given as the \fBchan\fR argument in a call to |
| 412 |
\fBTcl_RegisterChannel\fR, you should instead use |
| 413 |
\fBTcl_UnregisterChannel\fR, which will internally call \fBTcl_Close\fR |
| 414 |
when all calls to \fBTcl_RegisterChannel\fR have been matched by |
| 415 |
corresponding calls to \fBTcl_UnregisterChannel\fR. |
| 416 |
.SH "TCL_READCHARS AND TCL_READ" |
| 417 |
.PP |
| 418 |
\fBTcl_ReadChars\fR consumes bytes from \fIchannel\fR, converting the bytes |
| 419 |
to UTF-8 based on the channel's encoding and storing the produced data in |
| 420 |
\fIreadObjPtr\fR's string representation. The return value of |
| 421 |
\fBTcl_ReadChars\fR is the number of characters, up to \fIcharsToRead\fR, |
| 422 |
that were stored in \fIreadObjPtr\fR. If an error occurs while reading, the |
| 423 |
return value is \-1 and \fBTcl_ReadChars\fR records a POSIX error code that |
| 424 |
can be retrieved with \fBTcl_GetErrno\fR. |
| 425 |
.PP |
| 426 |
Setting \fIcharsToRead\fR to \fB\-1\fR will cause the command to read |
| 427 |
all characters currently available (non-blocking) or everything until |
| 428 |
eof (blocking mode). |
| 429 |
.PP |
| 430 |
The return value may be smaller than the value to read, indicating that less |
| 431 |
data than requested was available. This is called a \fIshort read\fR. In |
| 432 |
blocking mode, this can only happen on an end-of-file. In nonblocking mode, |
| 433 |
a short read can also occur if there is not enough input currently |
| 434 |
available: \fBTcl_ReadChars\fR returns a short count rather than waiting |
| 435 |
for more data. |
| 436 |
.PP |
| 437 |
If the channel is in blocking mode, a return value of zero indicates an |
| 438 |
end-of-file condition. If the channel is in nonblocking mode, a return |
| 439 |
value of zero indicates either that no input is currently available or an |
| 440 |
end-of-file condition. Use \fBTcl_Eof\fR and \fBTcl_InputBlocked\fR to tell |
| 441 |
which of these conditions actually occurred. |
| 442 |
.PP |
| 443 |
\fBTcl_ReadChars\fR translates the various end-of-line representations into |
| 444 |
the canonical \fB\en\fR internal representation according to the current |
| 445 |
end-of-line recognition mode. End-of-line recognition and the various |
| 446 |
platform-specific modes are described in the manual entry for the Tcl |
| 447 |
\fBfconfigure\fR command. |
| 448 |
.PP |
| 449 |
As a performance optimization, when reading from a channel with the encoding |
| 450 |
\fBbinary\fR, the bytes are not converted to UTF-8 as they are read. |
| 451 |
Instead, they are stored in \fIreadObjPtr\fR's internal representation as a |
| 452 |
byte-array value. The string representation of this value will only be |
| 453 |
constructed if it is needed (e.g., because of a call to |
| 454 |
\fBTcl_GetStringFromObj\fR). In this way, byte-oriented data can be read |
| 455 |
from a channel, manipulated by calling \fBTcl_GetByteArrayFromObj\fR and |
| 456 |
related functions, and then written to a channel without the expense of ever |
| 457 |
converting to or from UTF-8. |
| 458 |
.PP |
| 459 |
\fBTcl_Read\fR is similar to \fBTcl_ReadChars\fR, except that it does not do |
| 460 |
encoding conversions, regardless of the channel's encoding. It is deprecated |
| 461 |
and exists for backwards compatibility with non-internationalized Tcl |
| 462 |
extensions. It consumes bytes from \fIchannel\fR and stores them in |
| 463 |
\fIreadBuf\fR, performing end-of-line translations on the way. The return value |
| 464 |
of \fBTcl_Read\fR is the number of bytes, up to \fIbytesToRead\fR, written in |
| 465 |
\fIreadBuf\fR. The buffer produced by \fBTcl_Read\fR is not null-terminated. |
| 466 |
Its contents are valid from the zeroth position up to and excluding the |
| 467 |
position indicated by the return value. |
| 468 |
.PP |
| 469 |
\fBTcl_ReadRaw\fR is the same as \fBTcl_Read\fR but does not |
| 470 |
compensate for stacking. While \fBTcl_Read\fR (and the other functions |
| 471 |
in the API) always get their data from the topmost channel in the |
| 472 |
stack the supplied channel is part of, \fBTcl_ReadRaw\fR does |
| 473 |
not. Thus this function is \fBonly\fR usable for transformational |
| 474 |
channel drivers, i.e. drivers used in the middle of a stack of |
| 475 |
channels, to move data from the channel below into the transformation. |
| 476 |
.SH "TCL_GETSOBJ AND TCL_GETS" |
| 477 |
.PP |
| 478 |
\fBTcl_GetsObj\fR consumes bytes from \fIchannel\fR, converting the bytes to |
| 479 |
UTF-8 based on the channel's encoding, until a full line of input has been |
| 480 |
seen. If the channel's encoding is \fBbinary\fR, each byte read from the |
| 481 |
channel is treated as an individual Unicode character. All of the |
| 482 |
characters of the line except for the terminating end-of-line character(s) |
| 483 |
are appended to \fIlineObjPtr\fR's string representation. The end-of-line |
| 484 |
character(s) are read and discarded. |
| 485 |
.PP |
| 486 |
If a line was successfully read, the return value is greater than or equal |
| 487 |
to zero and indicates the number of bytes stored in \fIlineObjPtr\fR. If an |
| 488 |
error occurs, \fBTcl_GetsObj\fR returns \-1 and records a POSIX error code |
| 489 |
that can be retrieved with \fBTcl_GetErrno\fR. \fBTcl_GetsObj\fR also |
| 490 |
returns \-1 if the end of the file is reached; the \fBTcl_Eof\fR procedure |
| 491 |
can be used to distinguish an error from an end-of-file condition. |
| 492 |
.PP |
| 493 |
If the channel is in nonblocking mode, the return value can also be \-1 if |
| 494 |
no data was available or the data that was available did not contain an |
| 495 |
end-of-line character. When \-1 is returned, the \fBTcl_InputBlocked\fR |
| 496 |
procedure may be invoked to determine if the channel is blocked because |
| 497 |
of input unavailability. |
| 498 |
.PP |
| 499 |
\fBTcl_Gets\fR is the same as \fBTcl_GetsObj\fR except the resulting |
| 500 |
characters are appended to the dynamic string given by |
| 501 |
\fIlineRead\fR rather than a Tcl value. |
| 502 |
.SH "TCL_UNGETS" |
| 503 |
.PP |
| 504 |
\fBTcl_Ungets\fR is used to add data to the input queue of a channel, |
| 505 |
at either the head or tail of the queue. The pointer \fIinput\fR points |
| 506 |
to the data that is to be added. The length of the input to add is given |
| 507 |
by \fIinputLen\fR. A non-zero value of \fIaddAtEnd\fR indicates that the |
| 508 |
data is to be added at the end of queue; otherwise it will be added at the |
| 509 |
head of the queue. If \fIchannel\fR has a |
| 510 |
.QW sticky |
| 511 |
EOF set, no data will be |
| 512 |
added to the input queue. \fBTcl_Ungets\fR returns \fIinputLen\fR or |
| 513 |
\-1 if an error occurs. |
| 514 |
.SH "TCL_WRITECHARS, TCL_WRITEOBJ, AND TCL_WRITE" |
| 515 |
.PP |
| 516 |
\fBTcl_WriteChars\fR accepts \fIbytesToWrite\fR bytes of character data at |
| 517 |
\fIcharBuf\fR. The UTF-8 characters in the buffer are converted to the |
| 518 |
channel's encoding and queued for output to \fIchannel\fR. If |
| 519 |
\fIbytesToWrite\fR is negative, \fBTcl_WriteChars\fR expects \fIcharBuf\fR |
| 520 |
to be null-terminated and it outputs everything up to the null. |
| 521 |
.PP |
| 522 |
Data queued for output may not appear on the output device immediately, due |
| 523 |
to internal buffering. If the data should appear immediately, call |
| 524 |
\fBTcl_Flush\fR after the call to \fBTcl_WriteChars\fR, or set the |
| 525 |
\fB\-buffering\fR option on the channel to \fBnone\fR. If you wish the data |
| 526 |
to appear as soon as a complete line is accepted for output, set the |
| 527 |
\fB\-buffering\fR option on the channel to \fBline\fR mode. |
| 528 |
.PP |
| 529 |
The return value of \fBTcl_WriteChars\fR is a count of how many bytes were |
| 530 |
accepted for output to the channel. This is either greater than zero to |
| 531 |
indicate success or \-1 to indicate that an error occurred. If an error |
| 532 |
occurs, \fBTcl_WriteChars\fR records a POSIX error code that may be |
| 533 |
retrieved with \fBTcl_GetErrno\fR. |
| 534 |
.PP |
| 535 |
Newline characters in the output data are translated to platform-specific |
| 536 |
end-of-line sequences according to the \fB\-translation\fR option for the |
| 537 |
channel. This is done even if the channel has no encoding. |
| 538 |
.PP |
| 539 |
\fBTcl_WriteObj\fR is similar to \fBTcl_WriteChars\fR except it |
| 540 |
accepts a Tcl value whose contents will be output to the channel. The |
| 541 |
UTF-8 characters in \fIwriteObjPtr\fR's string representation are converted |
| 542 |
to the channel's encoding and queued for output to \fIchannel\fR. |
| 543 |
As a performance optimization, when writing to a channel with the encoding |
| 544 |
\fBbinary\fR, UTF-8 characters are not converted as they are written. |
| 545 |
Instead, the bytes in \fIwriteObjPtr\fR's internal representation as a |
| 546 |
byte-array value are written to the channel. The byte-array representation |
| 547 |
of the value will be constructed if it is needed. In this way, |
| 548 |
byte-oriented data can be read from a channel, manipulated by calling |
| 549 |
\fBTcl_GetByteArrayFromObj\fR and related functions, and then written to a |
| 550 |
channel without the expense of ever converting to or from UTF-8. |
| 551 |
.PP |
| 552 |
\fBTcl_Write\fR is similar to \fBTcl_WriteChars\fR except that it does not do |
| 553 |
encoding conversions, regardless of the channel's encoding. It is |
| 554 |
deprecated and exists for backwards compatibility with non-internationalized |
| 555 |
Tcl extensions. It accepts \fIbytesToWrite\fR bytes of data at |
| 556 |
\fIbyteBuf\fR and queues them for output to \fIchannel\fR. If |
| 557 |
\fIbytesToWrite\fR is negative, \fBTcl_Write\fR expects \fIbyteBuf\fR to be |
| 558 |
null-terminated and it outputs everything up to the null. |
| 559 |
.PP |
| 560 |
\fBTcl_WriteRaw\fR is the same as \fBTcl_Write\fR but does not |
| 561 |
compensate for stacking. While \fBTcl_Write\fR (and the other |
| 562 |
functions in the API) always feed their input to the topmost channel |
| 563 |
in the stack the supplied channel is part of, \fBTcl_WriteRaw\fR does |
| 564 |
not. Thus this function is \fBonly\fR usable for transformational |
| 565 |
channel drivers, i.e. drivers used in the middle of a stack of |
| 566 |
channels, to move data from the transformation into the channel below |
| 567 |
it. |
| 568 |
.SH TCL_FLUSH |
| 569 |
.PP |
| 570 |
\fBTcl_Flush\fR causes all of the buffered output data for \fIchannel\fR |
| 571 |
to be written to its underlying file or device as soon as possible. |
| 572 |
If the channel is in blocking mode, the call does not return until |
| 573 |
all the buffered data has been sent to the channel or some error occurred. |
| 574 |
The call returns immediately if the channel is nonblocking; it starts |
| 575 |
a background flush that will write the buffered data to the channel |
| 576 |
eventually, as fast as the channel is able to absorb it. |
| 577 |
.PP |
| 578 |
The return value is normally \fBTCL_OK\fR. |
| 579 |
If an error occurs, \fBTcl_Flush\fR returns \fBTCL_ERROR\fR and |
| 580 |
records a POSIX error code that can be retrieved with \fBTcl_GetErrno\fR. |
| 581 |
.SH TCL_SEEK |
| 582 |
.PP |
| 583 |
\fBTcl_Seek\fR moves the access point in \fIchannel\fR where subsequent |
| 584 |
data will be read or written. Buffered output is flushed to the channel and |
| 585 |
buffered input is discarded, prior to the seek operation. |
| 586 |
.PP |
| 587 |
\fBTcl_Seek\fR normally returns the new access point. |
| 588 |
If an error occurs, \fBTcl_Seek\fR returns \-1 and records a POSIX error |
| 589 |
code that can be retrieved with \fBTcl_GetErrno\fR. |
| 590 |
After an error, the access point may or may not have been moved. |
| 591 |
.SH TCL_TELL |
| 592 |
.PP |
| 593 |
\fBTcl_Tell\fR returns the current access point for a channel. The returned |
| 594 |
value is \-1 if the channel does not support seeking. |
| 595 |
.SH TCL_TRUNCATECHANNEL |
| 596 |
.PP |
| 597 |
\fBTcl_TruncateChannel\fR truncates the file underlying \fIchannel\fR |
| 598 |
to a given \fIlength\fR of bytes. It returns \fBTCL_OK\fR if the |
| 599 |
operation succeeded, and \fBTCL_ERROR\fR otherwise. |
| 600 |
.SH TCL_GETCHANNELOPTION |
| 601 |
.PP |
| 602 |
\fBTcl_GetChannelOption\fR retrieves, in \fIoptionValue\fR, the value of one of |
| 603 |
the options currently in effect for a channel, or a list of all options and |
| 604 |
their values. The \fIchannel\fR argument identifies the channel for which |
| 605 |
to query an option or retrieve all options and their values. |
| 606 |
If \fIoptionName\fR is not NULL, it is the name of the |
| 607 |
option to query; the option's value is copied to the Tcl dynamic string |
| 608 |
denoted by \fIoptionValue\fR. If |
| 609 |
\fIoptionName\fR is NULL, the function stores an alternating list of option |
| 610 |
names and their values in \fIoptionValue\fR, using a series of calls to |
| 611 |
\fBTcl_DStringAppendElement\fR. The various preexisting options and |
| 612 |
their possible values are described in the manual entry for the Tcl |
| 613 |
\fBfconfigure\fR command. Other options can be added by each channel type. |
| 614 |
These channel type specific options are described in the manual entry for |
| 615 |
the Tcl command that creates a channel of that type; for example, the |
| 616 |
additional options for TCP-based channels are described in the manual entry |
| 617 |
for the Tcl \fBsocket\fR command. |
| 618 |
The procedure normally returns \fBTCL_OK\fR. If an error occurs, it returns |
| 619 |
\fBTCL_ERROR\fR and calls \fBTcl_SetErrno\fR to store an appropriate POSIX |
| 620 |
error code. |
| 621 |
.SH TCL_SETCHANNELOPTION |
| 622 |
.PP |
| 623 |
\fBTcl_SetChannelOption\fR sets a new value \fInewValue\fR |
| 624 |
for an option \fIoptionName\fR on \fIchannel\fR. |
| 625 |
The procedure normally returns \fBTCL_OK\fR. If an error occurs, |
| 626 |
it returns \fBTCL_ERROR\fR; in addition, if \fIinterp\fR is non-NULL, |
| 627 |
\fBTcl_SetChannelOption\fR leaves an error message in the interpreter's result. |
| 628 |
.SH TCL_EOF |
| 629 |
.PP |
| 630 |
\fBTcl_Eof\fR returns a nonzero value if \fIchannel\fR encountered |
| 631 |
an end of file during the last input operation. |
| 632 |
.SH TCL_INPUTBLOCKED |
| 633 |
.PP |
| 634 |
\fBTcl_InputBlocked\fR returns a nonzero value if \fIchannel\fR is in |
| 635 |
nonblocking mode and the last input operation returned less data than |
| 636 |
requested because there was insufficient data available. |
| 637 |
The call always returns zero if the channel is in blocking mode. |
| 638 |
.SH TCL_INPUTBUFFERED |
| 639 |
.PP |
| 640 |
\fBTcl_InputBuffered\fR returns the number of bytes of input currently |
| 641 |
buffered in the internal buffers for a channel. If the channel is not open |
| 642 |
for reading, this function always returns zero. |
| 643 |
.SH TCL_OUTPUTBUFFERED |
| 644 |
.PP |
| 645 |
\fBTcl_OutputBuffered\fR returns the number of bytes of output |
| 646 |
currently buffered in the internal buffers for a channel. If the |
| 647 |
channel is not open for writing, this function always returns zero. |
| 648 |
.SH "PLATFORM ISSUES" |
| 649 |
.PP |
| 650 |
The handles returned from \fBTcl_GetChannelHandle\fR depend on the |
| 651 |
platform and the channel type. On Unix platforms, the handle is |
| 652 |
always a Unix file descriptor as returned from the \fBopen\fR system |
| 653 |
call. On Windows platforms, the handle is a file \fBHANDLE\fR when |
| 654 |
the channel was created with \fBTcl_OpenFileChannel\fR, |
| 655 |
\fBTcl_OpenCommandChannel\fR, or \fBTcl_MakeFileChannel\fR. Other |
| 656 |
channel types may return a different type of handle on Windows |
| 657 |
platforms. |
| 658 |
.SH "SEE ALSO" |
| 659 |
DString(3), fconfigure(n), filename(n), fopen(3), Tcl_CreateChannel(3) |
| 660 |
.SH KEYWORDS |
| 661 |
access point, blocking, buffered I/O, channel, channel driver, end of file, |
| 662 |
flush, input, nonblocking, output, read, seek, write |