Debian Codegraph @

doc/CrtChannel.3

raw
1 '\"
2 '\" Copyright (c) 1996-1997 Sun Microsystems, Inc.
3 '\" Copyright (c) 1997-2000 Ajuba Solutions.
4 '\"
5 '\" See the file "license.terms" for information on usage and redistribution
6 '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
7 .TH Tcl_CreateChannel 3 8.4 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_CreateChannel, Tcl_GetChannelInstanceData, Tcl_GetChannelType, Tcl_GetChannelName, Tcl_GetChannelHandle, Tcl_GetChannelMode, Tcl_GetChannelBufferSize, Tcl_SetChannelBufferSize, Tcl_NotifyChannel, Tcl_BadChannelOption, Tcl_ChannelName, Tcl_ChannelVersion, Tcl_ChannelBlockModeProc, Tcl_ChannelCloseProc, Tcl_ChannelClose2Proc, Tcl_ChannelInputProc, Tcl_ChannelOutputProc, Tcl_ChannelSeekProc, Tcl_ChannelWideSeekProc, Tcl_ChannelTruncateProc, Tcl_ChannelSetOptionProc, Tcl_ChannelGetOptionProc, Tcl_ChannelWatchProc, Tcl_ChannelGetHandleProc, Tcl_ChannelFlushProc, Tcl_ChannelHandlerProc, Tcl_ChannelThreadActionProc, Tcl_IsChannelShared, Tcl_IsChannelRegistered, Tcl_CutChannel, Tcl_SpliceChannel, Tcl_IsChannelExisting, Tcl_ClearChannelHandlers, Tcl_GetChannelThread, Tcl_ChannelBuffered \- procedures for creating and manipulating channels
13 .SH SYNOPSIS
14 .nf
15 \fB#include <tcl.h>\fR
16 .sp
17 Tcl_Channel
18 \fBTcl_CreateChannel\fR(\fItypePtr, channelName, instanceData, mask\fR)
19 .sp
20 ClientData
21 \fBTcl_GetChannelInstanceData\fR(\fIchannel\fR)
22 .sp
23 const Tcl_ChannelType *
24 \fBTcl_GetChannelType\fR(\fIchannel\fR)
25 .sp
26 const char *
27 \fBTcl_GetChannelName\fR(\fIchannel\fR)
28 .sp
29 int
30 \fBTcl_GetChannelHandle\fR(\fIchannel, direction, handlePtr\fR)
31 .sp
32 Tcl_ThreadId
33 \fBTcl_GetChannelThread\fR(\fIchannel\fR)
34 .sp
35 int
36 \fBTcl_GetChannelMode\fR(\fIchannel\fR)
37 .sp
38 int
39 \fBTcl_GetChannelBufferSize\fR(\fIchannel\fR)
40 .sp
41 \fBTcl_SetChannelBufferSize\fR(\fIchannel, size\fR)
42 .sp
43 \fBTcl_NotifyChannel\fR(\fIchannel, mask\fR)
44 .sp
45 int
46 \fBTcl_BadChannelOption\fR(\fIinterp, optionName, optionList\fR)
47 .sp
48 int
49 \fBTcl_IsChannelShared\fR(\fIchannel\fR)
50 .sp
51 int
52 \fBTcl_IsChannelRegistered\fR(\fIinterp, channel\fR)
53 .sp
54 int
55 \fBTcl_IsChannelExisting\fR(\fIchannelName\fR)
56 .sp
57 void
58 \fBTcl_CutChannel\fR(\fIchannel\fR)
59 .sp
60 void
61 \fBTcl_SpliceChannel\fR(\fIchannel\fR)
62 .sp
63 void
64 \fBTcl_ClearChannelHandlers\fR(\fIchannel\fR)
65 .sp
66 int
67 \fBTcl_ChannelBuffered\fR(\fIchannel\fR)
68 .sp
69 const char *
70 \fBTcl_ChannelName\fR(\fItypePtr\fR)
71 .sp
72 Tcl_ChannelTypeVersion
73 \fBTcl_ChannelVersion\fR(\fItypePtr\fR)
74 .sp
75 Tcl_DriverBlockModeProc *
76 \fBTcl_ChannelBlockModeProc\fR(\fItypePtr\fR)
77 .sp
78 Tcl_DriverCloseProc *
79 \fBTcl_ChannelCloseProc\fR(\fItypePtr\fR)
80 .sp
81 Tcl_DriverClose2Proc *
82 \fBTcl_ChannelClose2Proc\fR(\fItypePtr\fR)
83 .sp
84 Tcl_DriverInputProc *
85 \fBTcl_ChannelInputProc\fR(\fItypePtr\fR)
86 .sp
87 Tcl_DriverOutputProc *
88 \fBTcl_ChannelOutputProc\fR(\fItypePtr\fR)
89 .sp
90 Tcl_DriverSeekProc *
91 \fBTcl_ChannelSeekProc\fR(\fItypePtr\fR)
92 .sp
93 Tcl_DriverWideSeekProc *
94 \fBTcl_ChannelWideSeekProc\fR(\fItypePtr\fR)
95 .sp
96 Tcl_DriverThreadActionProc *
97 \fBTcl_ChannelThreadActionProc\fR(\fItypePtr\fR)
98 .sp
99 Tcl_DriverTruncateProc *
100 \fBTcl_ChannelTruncateProc\fR(\fItypePtr\fR)
101 .sp
102 Tcl_DriverSetOptionProc *
103 \fBTcl_ChannelSetOptionProc\fR(\fItypePtr\fR)
104 .sp
105 Tcl_DriverGetOptionProc *
106 \fBTcl_ChannelGetOptionProc\fR(\fItypePtr\fR)
107 .sp
108 Tcl_DriverWatchProc *
109 \fBTcl_ChannelWatchProc\fR(\fItypePtr\fR)
110 .sp
111 Tcl_DriverGetHandleProc *
112 \fBTcl_ChannelGetHandleProc\fR(\fItypePtr\fR)
113 .sp
114 Tcl_DriverFlushProc *
115 \fBTcl_ChannelFlushProc\fR(\fItypePtr\fR)
116 .sp
117 Tcl_DriverHandlerProc *
118 \fBTcl_ChannelHandlerProc\fR(\fItypePtr\fR)
119 .sp
120 .SH ARGUMENTS
121 .AS "const Tcl_ChannelType" *channelName
122 .AP "const Tcl_ChannelType" *typePtr in
123 Points to a structure containing the addresses of procedures that
124 can be called to perform I/O and other functions on the channel.
125 .AP "const char" *channelName in
126 The name of this channel, such as \fBfile3\fR; must not be in use
127 by any other channel. Can be NULL, in which case the channel is
128 created without a name. If the created channel is assigned to one
129 of the standard channels (\fBstdin\fR, \fBstdout\fR or \fBstderr\fR),
130 the assigned channel name will be the name of the standard channel.
131 .AP ClientData instanceData in
132 Arbitrary one-word value to be associated with this channel. This
133 value is passed to procedures in \fItypePtr\fR when they are invoked.
134 .AP int mask in
135 OR-ed combination of \fBTCL_READABLE\fR and \fBTCL_WRITABLE\fR to indicate
136 whether a channel is readable and writable.
137 .AP Tcl_Channel channel in
138 The channel to operate on.
139 .AP int direction in
140 \fBTCL_READABLE\fR means the input handle is wanted; \fBTCL_WRITABLE\fR
141 means the output handle is wanted.
142 .AP ClientData *handlePtr out
143 Points to the location where the desired OS-specific handle should be
144 stored.
145 .AP int size in
146 The size, in bytes, of buffers to allocate in this channel.
147 .AP int mask in
148 An OR-ed combination of \fBTCL_READABLE\fR, \fBTCL_WRITABLE\fR
149 and \fBTCL_EXCEPTION\fR that indicates events that have occurred on
150 this channel.
151 .AP Tcl_Interp *interp in
152 Current interpreter. (can be NULL)
153 .AP "const char" *optionName in
154 Name of the invalid option.
155 .AP "const char" *optionList in
156 Specific options list (space separated words, without
157 .QW \- )
158 to append to the standard generic options list.
159 Can be NULL for generic options error message only.
160 .BE
161 .SH DESCRIPTION
162 .PP
163 Tcl uses a two-layered channel architecture. It provides a generic upper
164 layer to enable C and Tcl programs to perform input and output using the
165 same APIs for a variety of files, devices, sockets etc. The generic C APIs
166 are described in the manual entry for \fBTcl_OpenFileChannel\fR.
167 .PP
168 The lower layer provides type-specific channel drivers for each type
169 of device supported on each platform. This manual entry describes the
170 C APIs used to communicate between the generic layer and the
171 type-specific channel drivers. It also explains how new types of
172 channels can be added by providing new channel drivers.
173 .PP
174 Channel drivers consist of a number of components: First, each channel
175 driver provides a \fBTcl_ChannelType\fR structure containing pointers to
176 functions implementing the various operations used by the generic layer to
177 communicate with the channel driver. The \fBTcl_ChannelType\fR structure
178 and the functions referenced by it are described in the section
179 \fBTCL_CHANNELTYPE\fR, below.
180 .PP
181 Second, channel drivers usually provide a Tcl command to create
182 instances of that type of channel. For example, the Tcl \fBopen\fR
183 command creates channels that use the file and command channel
184 drivers, and the Tcl \fBsocket\fR command creates channels that use
185 TCP sockets for network communication.
186 .PP
187 Third, a channel driver optionally provides a C function to open
188 channel instances of that type. For example, \fBTcl_OpenFileChannel\fR
189 opens a channel that uses the file channel driver, and
190 \fBTcl_OpenTcpClient\fR opens a channel that uses the TCP network
191 protocol. These creation functions typically use
192 \fBTcl_CreateChannel\fR internally to open the channel.
193 .PP
194 To add a new type of channel you must implement a C API or a Tcl command
195 that opens a channel by invoking \fBTcl_CreateChannel\fR.
196 When your driver calls \fBTcl_CreateChannel\fR it passes in
197 a \fBTcl_ChannelType\fR structure describing the driver's I/O
198 procedures.
199 The generic layer will then invoke the functions referenced in that
200 structure to perform operations on the channel.
201 .PP
202 \fBTcl_CreateChannel\fR opens a new channel and associates the supplied
203 \fItypePtr\fR and \fIinstanceData\fR with it. The channel is opened in the
204 mode indicated by \fImask\fR.
205 For a discussion of channel drivers, their operations and the
206 \fBTcl_ChannelType\fR structure, see the section \fBTCL_CHANNELTYPE\fR, below.
207 .PP
208 \fBTcl_CreateChannel\fR interacts with the code managing the standard
209 channels. Once a standard channel was initialized either through a
210 call to \fBTcl_GetStdChannel\fR or a call to \fBTcl_SetStdChannel\fR
211 closing this standard channel will cause the next call to
212 \fBTcl_CreateChannel\fR to make the new channel the new standard
213 channel too. See \fBTcl_StandardChannels\fR for a general treatise
214 about standard channels and the behavior of the Tcl library with
215 regard to them.
216 .PP
217 \fBTcl_GetChannelInstanceData\fR returns the instance data associated with
218 the channel in \fIchannel\fR. This is the same as the \fIinstanceData\fR
219 argument in the call to \fBTcl_CreateChannel\fR that created this channel.
220 .PP
221 \fBTcl_GetChannelType\fR returns a pointer to the \fBTcl_ChannelType\fR
222 structure used by the channel in the \fIchannel\fR argument. This is
223 the same as the \fItypePtr\fR argument in the call to
224 \fBTcl_CreateChannel\fR that created this channel.
225 .PP
226 \fBTcl_GetChannelName\fR returns a string containing the name associated
227 with the channel, or NULL if the \fIchannelName\fR argument to
228 \fBTcl_CreateChannel\fR was NULL.
229 .PP
230 \fBTcl_GetChannelHandle\fR places the OS-specific device handle
231 associated with \fIchannel\fR for the given \fIdirection\fR in the
232 location specified by \fIhandlePtr\fR and returns \fBTCL_OK\fR. If
233 the channel does not have a device handle for the specified direction,
234 then \fBTCL_ERROR\fR is returned instead. Different channel drivers
235 will return different types of handle. Refer to the manual entries
236 for each driver to determine what type of handle is returned.
237 .PP
238 \fBTcl_GetChannelThread\fR returns the id of the thread currently managing
239 the specified \fIchannel\fR. This allows channel drivers to send their file
240 events to the correct event queue even for a multi-threaded core.
241 .PP
242 \fBTcl_GetChannelMode\fR returns an OR-ed combination of \fBTCL_READABLE\fR
243 and \fBTCL_WRITABLE\fR, indicating whether the channel is open for input
244 and output.
245 .PP
246 \fBTcl_GetChannelBufferSize\fR returns the size, in bytes, of buffers
247 allocated to store input or output in \fIchannel\fR. If the value was not set
248 by a previous call to \fBTcl_SetChannelBufferSize\fR, described below, then
249 the default value of 4096 is returned.
250 .PP
251 \fBTcl_SetChannelBufferSize\fR sets the size, in bytes, of buffers that
252 will be allocated in subsequent operations on the channel to store input or
253 output. The \fIsize\fR argument should be between one and one million,
254 allowing buffers of one byte to one million bytes. If \fIsize\fR is
255 outside this range, \fBTcl_SetChannelBufferSize\fR sets the buffer size to
256 4096.
257 .PP
258 \fBTcl_NotifyChannel\fR is called by a channel driver to indicate to
259 the generic layer that the events specified by \fImask\fR have
260 occurred on the channel. Channel drivers are responsible for invoking
261 this function whenever the channel handlers need to be called for the
262 channel (or other pending tasks like a write flush should be performed).
263 See \fBWATCHPROC\fR below for more details.
264 .PP
265 \fBTcl_BadChannelOption\fR is called from driver specific
266 \fIsetOptionProc\fR or \fIgetOptionProc\fR to generate a complete
267 error message.
268 .PP
269 \fBTcl_ChannelBuffered\fR returns the number of bytes of input
270 currently buffered in the internal buffer (push back area) of the
271 channel itself. It does not report about the data in the overall
272 buffers for the stack of channels the supplied channel is part of.
273 .PP
274 \fBTcl_IsChannelShared\fR checks the refcount of the specified
275 \fIchannel\fR and returns whether the \fIchannel\fR was shared among
276 multiple interpreters (result == 1) or not (result == 0).
277 .PP
278 \fBTcl_IsChannelRegistered\fR checks whether the specified \fIchannel\fR is
279 registered in the given \fIinterp\fRreter (result == 1) or not
280 (result == 0).
281 .PP
282 \fBTcl_IsChannelExisting\fR checks whether a channel with the specified
283 name is registered in the (thread)-global list of all channels (result
284 == 1) or not (result == 0).
285 .PP
286 \fBTcl_CutChannel\fR removes the specified \fIchannel\fR from the
287 (thread)global list of all channels (of the current thread).
288 Application to a channel still registered in some interpreter
289 is not allowed.
290 Also notifies the driver if the \fBTcl_ChannelType\fR version is
291 \fBTCL_CHANNEL_VERSION_4\fR (or higher), and
292 \fBTcl_DriverThreadActionProc\fR is defined for it.
293 .PP
294 \fBTcl_SpliceChannel\fR adds the specified \fIchannel\fR to the
295 (thread)global list of all channels (of the current thread).
296 Application to a channel registered in some interpreter is not allowed.
297 Also notifies the driver if the \fBTcl_ChannelType\fR version is
298 \fBTCL_CHANNEL_VERSION_4\fR (or higher), and
299 \fBTcl_DriverThreadActionProc\fR is defined for it.
300 .PP
301 \fBTcl_ClearChannelHandlers\fR removes all channel handlers and event
302 scripts associated with the specified \fIchannel\fR, thus shutting
303 down all event processing for this channel.
304 .SH TCL_CHANNELTYPE
305 .PP
306 A channel driver provides a \fBTcl_ChannelType\fR structure that contains
307 pointers to functions that implement the various operations on a channel;
308 these operations are invoked as needed by the generic layer. The structure
309 was versioned starting in Tcl 8.3.2/8.4 to correct a problem with stacked
310 channel drivers. See the \fBOLD CHANNEL TYPES\fR section below for
311 details about the old structure.
312 .PP
313 The \fBTcl_ChannelType\fR structure contains the following fields:
314 .PP
315 .CS
316 typedef struct Tcl_ChannelType {
317 const char *\fItypeName\fR;
318 Tcl_ChannelTypeVersion \fIversion\fR;
319 Tcl_DriverCloseProc *\fIcloseProc\fR;
320 Tcl_DriverInputProc *\fIinputProc\fR;
321 Tcl_DriverOutputProc *\fIoutputProc\fR;
322 Tcl_DriverSeekProc *\fIseekProc\fR;
323 Tcl_DriverSetOptionProc *\fIsetOptionProc\fR;
324 Tcl_DriverGetOptionProc *\fIgetOptionProc\fR;
325 Tcl_DriverWatchProc *\fIwatchProc\fR;
326 Tcl_DriverGetHandleProc *\fIgetHandleProc\fR;
327 Tcl_DriverClose2Proc *\fIclose2Proc\fR;
328 Tcl_DriverBlockModeProc *\fIblockModeProc\fR;
329 Tcl_DriverFlushProc *\fIflushProc\fR;
330 Tcl_DriverHandlerProc *\fIhandlerProc\fR;
331 Tcl_DriverWideSeekProc *\fIwideSeekProc\fR;
332 Tcl_DriverThreadActionProc *\fIthreadActionProc\fR;
333 Tcl_DriverTruncateProc *\fItruncateProc\fR;
334 } \fBTcl_ChannelType\fR;
335 .CE
336 .PP
337 It is not necessary to provide implementations for all channel
338 operations. Those which are not necessary may be set to NULL in the
339 struct: \fIblockModeProc\fR, \fIseekProc\fR, \fIsetOptionProc\fR,
340 \fIgetOptionProc\fR, \fIgetHandleProc\fR, and \fIclose2Proc\fR, in addition to
341 \fIflushProc\fR, \fIhandlerProc\fR, \fIthreadActionProc\fR, and
342 \fItruncateProc\fR. Other functions that cannot be implemented in a
343 meaningful way should return \fBEINVAL\fR when called, to indicate
344 that the operations they represent are not available. Also note that
345 \fIwideSeekProc\fR can be NULL if \fIseekProc\fR is.
346 .PP
347 The user should only use the above structure for \fBTcl_ChannelType\fR
348 instantiation. When referencing fields in a \fBTcl_ChannelType\fR
349 structure, the following functions should be used to obtain the values:
350 \fBTcl_ChannelName\fR, \fBTcl_ChannelVersion\fR,
351 \fBTcl_ChannelBlockModeProc\fR, \fBTcl_ChannelCloseProc\fR,
352 \fBTcl_ChannelClose2Proc\fR, \fBTcl_ChannelInputProc\fR,
353 \fBTcl_ChannelOutputProc\fR, \fBTcl_ChannelSeekProc\fR,
354 \fBTcl_ChannelWideSeekProc\fR, \fBTcl_ChannelThreadActionProc\fR,
355 \fBTcl_ChannelTruncateProc\fR,
356 \fBTcl_ChannelSetOptionProc\fR, \fBTcl_ChannelGetOptionProc\fR,
357 \fBTcl_ChannelWatchProc\fR, \fBTcl_ChannelGetHandleProc\fR,
358 \fBTcl_ChannelFlushProc\fR, or \fBTcl_ChannelHandlerProc\fR.
359 .PP
360 The change to the structures was made in such a way that standard channel
361 types are binary compatible. However, channel types that use stacked
362 channels (i.e. TLS, Trf) have new versions to correspond to the above change
363 since the previous code for stacked channels had problems.
364 .SS TYPENAME
365 .PP
366 The \fItypeName\fR field contains a null-terminated string that
367 identifies the type of the device implemented by this driver, e.g.
368 \fBfile\fR or \fBsocket\fR.
369 .PP
370 This value can be retrieved with \fBTcl_ChannelName\fR, which returns
371 a pointer to the string.
372 .SS VERSION
373 .PP
374
375 The \fIversion\fR field should be set to the version of the structure
376 that you require. \fBTCL_CHANNEL_VERSION_2\fR is the minimum recommended.
377 \fBTCL_CHANNEL_VERSION_3\fR must be set to specify the \fIwideSeekProc\fR member.
378 \fBTCL_CHANNEL_VERSION_4\fR must be set to specify the \fIthreadActionProc\fR member
379 (includes \fIwideSeekProc\fR).
380 \fBTCL_CHANNEL_VERSION_5\fR must be set to specify the
381 \fItruncateProc\fR members (includes
382 \fIwideSeekProc\fR and \fIthreadActionProc\fR).
383 If it is not set to any of these, then this
384 \fBTcl_ChannelType\fR is assumed to have the original structure. See
385 \fBOLD CHANNEL TYPES\fR for more details. While Tcl will recognize
386 and function with either structures, stacked channels must be of at
387 least \fBTCL_CHANNEL_VERSION_2\fR to function correctly.
388 .PP
389 This value can be retrieved with \fBTcl_ChannelVersion\fR, which returns
390 one of
391 \fBTCL_CHANNEL_VERSION_5\fR,
392 \fBTCL_CHANNEL_VERSION_4\fR,
393 \fBTCL_CHANNEL_VERSION_3\fR,
394 \fBTCL_CHANNEL_VERSION_2\fR or \fBTCL_CHANNEL_VERSION_1\fR.
395 .SS BLOCKMODEPROC
396 .PP
397 The \fIblockModeProc\fR field contains the address of a function called by
398 the generic layer to set blocking and nonblocking mode on the device.
399 \fIBlockModeProc\fR should match the following prototype:
400 .PP
401 .CS
402 typedef int \fBTcl_DriverBlockModeProc\fR(
403 ClientData \fIinstanceData\fR,
404 int \fImode\fR);
405 .CE
406 .PP
407 The \fIinstanceData\fR is the same as the value passed to
408 \fBTcl_CreateChannel\fR when this channel was created. The \fImode\fR
409 argument is either \fBTCL_MODE_BLOCKING\fR or \fBTCL_MODE_NONBLOCKING\fR to
410 set the device into blocking or nonblocking mode. The function should
411 return zero if the operation was successful, or a nonzero POSIX error code
412 if the operation failed.
413 .PP
414 If the operation is successful, the function can modify the supplied
415 \fIinstanceData\fR to record that the channel entered blocking or
416 nonblocking mode and to implement the blocking or nonblocking behavior.
417 For some device types, the blocking and nonblocking behavior can be
418 implemented by the underlying operating system; for other device types, the
419 behavior must be emulated in the channel driver.
420 .PP
421 This value can be retrieved with \fBTcl_ChannelBlockModeProc\fR, which returns
422 a pointer to the function.
423 .PP
424 A channel driver \fBnot\fR supplying a \fIblockModeProc\fR has to be
425 very, very careful. It has to tell the generic layer exactly which
426 blocking mode is acceptable to it, and should this also document for
427 the user so that the blocking mode of the channel is not changed to an
428 unacceptable value. Any confusion here may lead the interpreter into a
429 (spurious and difficult to find) deadlock.
430 .SS "CLOSEPROC AND CLOSE2PROC"
431 .PP
432 The \fIcloseProc\fR field contains the address of a function called by the
433 generic layer to clean up driver-related information when the channel is
434 closed. \fICloseProc\fR must match the following prototype:
435 .PP
436 .CS
437 typedef int \fBTcl_DriverCloseProc\fR(
438 ClientData \fIinstanceData\fR,
439 Tcl_Interp *\fIinterp\fR);
440 .CE
441 .PP
442 The \fIinstanceData\fR argument is the same as the value provided to
443 \fBTcl_CreateChannel\fR when the channel was created. The function should
444 release any storage maintained by the channel driver for this channel, and
445 close the input and output devices encapsulated by this channel. All queued
446 output will have been flushed to the device before this function is called,
447 and no further driver operations will be invoked on this instance after
448 calling the \fIcloseProc\fR. If the close operation is successful, the
449 procedure should return zero; otherwise it should return a nonzero POSIX
450 error code. In addition, if an error occurs and \fIinterp\fR is not NULL,
451 the procedure should store an error message in the interpreter's result.
452 .PP
453 Alternatively, channels that support closing the read and write sides
454 independently may set \fIcloseProc\fR to \fBTCL_CLOSE2PROC\fR and set
455 \fIclose2Proc\fR to the address of a function that matches the
456 following prototype:
457 .PP
458 .CS
459 typedef int \fBTcl_DriverClose2Proc\fR(
460 ClientData \fIinstanceData\fR,
461 Tcl_Interp *\fIinterp\fR,
462 int \fIflags\fR);
463 .CE
464 .PP
465 The \fIclose2Proc\fR will be called with \fIflags\fR set to an OR'ed
466 combination of \fBTCL_CLOSE_READ\fR or \fBTCL_CLOSE_WRITE\fR to
467 indicate that the driver should close the read and/or write side of
468 the channel. The channel driver may be invoked to perform
469 additional operations on the channel after \fIclose2Proc\fR is
470 called to close one or both sides of the channel. If \fIflags\fR is
471 \fB0\fR (zero), the driver should close the channel in the manner
472 described above for \fIcloseProc\fR. No further operations will be
473 invoked on this instance after \fIclose2Proc\fR is called with all
474 flags cleared. In all cases, the \fIclose2Proc\fR function should
475 return zero if the close operation was successful; otherwise it should
476 return a nonzero POSIX error code. In addition, if an error occurs and
477 \fIinterp\fR is not NULL, the procedure should store an error message
478 in the interpreter's result.
479 .PP
480 The \fIcloseProc\fR and \fIclose2Proc\fR values can be retrieved with
481 \fBTcl_ChannelCloseProc\fR or \fBTcl_ChannelClose2Proc\fR, which
482 return a pointer to the respective function.
483 .SS INPUTPROC
484 .PP
485 The \fIinputProc\fR field contains the address of a function called by the
486 generic layer to read data from the file or device and store it in an
487 internal buffer. \fIInputProc\fR must match the following prototype:
488 .PP
489 .CS
490 typedef int \fBTcl_DriverInputProc\fR(
491 ClientData \fIinstanceData\fR,
492 char *\fIbuf\fR,
493 int \fIbufSize\fR,
494 int *\fIerrorCodePtr\fR);
495 .CE
496 .PP
497 \fIInstanceData\fR is the same as the value passed to
498 \fBTcl_CreateChannel\fR when the channel was created. The \fIbuf\fR
499 argument points to an array of bytes in which to store input from the
500 device, and the \fIbufSize\fR argument indicates how many bytes are
501 available at \fIbuf\fR.
502 .PP
503 The \fIerrorCodePtr\fR argument points to an integer variable provided by
504 the generic layer. If an error occurs, the function should set the variable
505 to a POSIX error code that identifies the error that occurred.
506 .PP
507 The function should read data from the input device encapsulated by the
508 channel and store it at \fIbuf\fR. On success, the function should return
509 a nonnegative integer indicating how many bytes were read from the input
510 device and stored at \fIbuf\fR. On error, the function should return -1. If
511 an error occurs after some data has been read from the device, that data is
512 lost.
513 .PP
514 If \fIinputProc\fR can determine that the input device has some data
515 available but less than requested by the \fIbufSize\fR argument, the
516 function should only attempt to read as much data as is available and
517 return without blocking. If the input device has no data available
518 whatsoever and the channel is in nonblocking mode, the function should
519 return an \fBEAGAIN\fR error. If the input device has no data available
520 whatsoever and the channel is in blocking mode, the function should block
521 for the shortest possible time until at least one byte of data can be read
522 from the device; then, it should return as much data as it can read without
523 blocking.
524 .PP
525 This value can be retrieved with \fBTcl_ChannelInputProc\fR, which returns
526 a pointer to the function.
527 .SS OUTPUTPROC
528 .PP
529 The \fIoutputProc\fR field contains the address of a function called by the
530 generic layer to transfer data from an internal buffer to the output device.
531 \fIOutputProc\fR must match the following prototype:
532 .PP
533 .CS
534 typedef int \fBTcl_DriverOutputProc\fR(
535 ClientData \fIinstanceData\fR,
536 const char *\fIbuf\fR,
537 int \fItoWrite\fR,
538 int *\fIerrorCodePtr\fR);
539 .CE
540 .PP
541 \fIInstanceData\fR is the same as the value passed to
542 \fBTcl_CreateChannel\fR when the channel was created. The \fIbuf\fR
543 argument contains an array of bytes to be written to the device, and the
544 \fItoWrite\fR argument indicates how many bytes are to be written from the
545 \fIbuf\fR argument.
546 .PP
547 The \fIerrorCodePtr\fR argument points to an integer variable provided by
548 the generic layer. If an error occurs, the function should set this
549 variable to a POSIX error code that identifies the error.
550 .PP
551 The function should write the data at \fIbuf\fR to the output device
552 encapsulated by the channel. On success, the function should return a
553 nonnegative integer indicating how many bytes were written to the output
554 device. The return value is normally the same as \fItoWrite\fR, but may be
555 less in some cases such as if the output operation is interrupted by a
556 signal. If an error occurs the function should return -1. In case of
557 error, some data may have been written to the device.
558 .PP
559 If the channel is nonblocking and the output device is unable to absorb any
560 data whatsoever, the function should return -1 with an \fBEAGAIN\fR error
561 without writing any data.
562 .PP
563 This value can be retrieved with \fBTcl_ChannelOutputProc\fR, which returns
564 a pointer to the function.
565 .SS "SEEKPROC AND WIDESEEKPROC"
566 .PP
567 The \fIseekProc\fR field contains the address of a function called by the
568 generic layer to move the access point at which subsequent input or output
569 operations will be applied. \fISeekProc\fR must match the following
570 prototype:
571 .PP
572 .CS
573 typedef int \fBTcl_DriverSeekProc\fR(
574 ClientData \fIinstanceData\fR,
575 long \fIoffset\fR,
576 int \fIseekMode\fR,
577 int *\fIerrorCodePtr\fR);
578 .CE
579 .PP
580 The \fIinstanceData\fR argument is the same as the value given to
581 \fBTcl_CreateChannel\fR when this channel was created. \fIOffset\fR and
582 \fIseekMode\fR have the same meaning as for the \fBTcl_Seek\fR
583 procedure (described in the manual entry for \fBTcl_OpenFileChannel\fR).
584 .PP
585 The \fIerrorCodePtr\fR argument points to an integer variable provided by
586 the generic layer for returning \fBerrno\fR values from the function. The
587 function should set this variable to a POSIX error code if an error occurs.
588 The function should store an \fBEINVAL\fR error code if the channel type
589 does not implement seeking.
590 .PP
591 The return value is the new access point or -1 in case of error. If an
592 error occurred, the function should not move the access point.
593 .PP
594 If there is a non-NULL \fIseekProc\fR field, the \fIwideSeekProc\fR
595 field may contain the address of an alternative function to use which
596 handles wide (i.e. larger than 32-bit) offsets, so allowing seeks
597 within files larger than 2GB. The \fIwideSeekProc\fR will be called
598 in preference to the \fIseekProc\fR, but both must be defined if the
599 \fIwideSeekProc\fR is defined. \fIWideSeekProc\fR must match the
600 following prototype:
601 .PP
602 .CS
603 typedef Tcl_WideInt \fBTcl_DriverWideSeekProc\fR(
604 ClientData \fIinstanceData\fR,
605 Tcl_WideInt \fIoffset\fR,
606 int \fIseekMode\fR,
607 int *\fIerrorCodePtr\fR);
608 .CE
609 .PP
610 The arguments and return values mean the same thing as with
611 \fIseekProc\fR above, except that the type of offsets and the return
612 type are different.
613 .PP
614 The \fIseekProc\fR value can be retrieved with
615 \fBTcl_ChannelSeekProc\fR, which returns a pointer to the function,
616 and similarly the \fIwideSeekProc\fR can be retrieved with
617 \fBTcl_ChannelWideSeekProc\fR.
618 .SS SETOPTIONPROC
619 .PP
620 The \fIsetOptionProc\fR field contains the address of a function called by
621 the generic layer to set a channel type specific option on a channel.
622 \fIsetOptionProc\fR must match the following prototype:
623 .PP
624 .CS
625 typedef int \fBTcl_DriverSetOptionProc\fR(
626 ClientData \fIinstanceData\fR,
627 Tcl_Interp *\fIinterp\fR,
628 const char *\fIoptionName\fR,
629 const char *\fInewValue\fR);
630 .CE
631 .PP
632 \fIoptionName\fR is the name of an option to set, and \fInewValue\fR is
633 the new value for that option, as a string. The \fIinstanceData\fR is the
634 same as the value given to \fBTcl_CreateChannel\fR when this channel was
635 created. The function should do whatever channel type specific action is
636 required to implement the new value of the option.
637 .PP
638 Some options are handled by the generic code and this function is never
639 called to set them, e.g. \fB\-blockmode\fR. Other options are specific to
640 each channel type and the \fIsetOptionProc\fR procedure of the channel
641 driver will get called to implement them. The \fIsetOptionProc\fR field can
642 be NULL, which indicates that this channel type supports no type specific
643 options.
644 .PP
645 If the option value is successfully modified to the new value, the function
646 returns \fBTCL_OK\fR.
647 It should call \fBTcl_BadChannelOption\fR which itself returns
648 \fBTCL_ERROR\fR if the \fIoptionName\fR is
649 unrecognized.
650 If \fInewValue\fR specifies a value for the option that
651 is not supported or if a system call error occurs,
652 the function should leave an error message in the result
653 of \fIinterp\fR if \fIinterp\fR is not NULL. The
654 function should also call \fBTcl_SetErrno\fR to store an appropriate POSIX
655 error code.
656 .PP
657 This value can be retrieved with \fBTcl_ChannelSetOptionProc\fR, which returns
658 a pointer to the function.
659 .SS GETOPTIONPROC
660 .PP
661 The \fIgetOptionProc\fR field contains the address of a function called by
662 the generic layer to get the value of a channel type specific option on a
663 channel. \fIgetOptionProc\fR must match the following prototype:
664 .PP
665 .CS
666 typedef int \fBTcl_DriverGetOptionProc\fR(
667 ClientData \fIinstanceData\fR,
668 Tcl_Interp *\fIinterp\fR,
669 const char *\fIoptionName\fR,
670 Tcl_DString *\fIoptionValue\fR);
671 .CE
672 .PP
673 \fIOptionName\fR is the name of an option supported by this type of
674 channel. If the option name is not NULL, the function stores its current
675 value, as a string, in the Tcl dynamic string \fIoptionValue\fR.
676 If \fIoptionName\fR is NULL, the function stores in \fIoptionValue\fR an
677 alternating list of all supported options and their current values.
678 On success, the function returns \fBTCL_OK\fR.
679 It should call \fBTcl_BadChannelOption\fR which itself returns
680 \fBTCL_ERROR\fR if the \fIoptionName\fR is
681 unrecognized. If a system call error occurs,
682 the function should leave an error message in the
683 result of \fIinterp\fR if \fIinterp\fR is not NULL. The
684 function should also call \fBTcl_SetErrno\fR to store an appropriate POSIX
685 error code.
686 .PP
687 Some options are handled by the generic code and this function is never
688 called to retrieve their value, e.g. \fB\-blockmode\fR. Other options are
689 specific to each channel type and the \fIgetOptionProc\fR procedure of the
690 channel driver will get called to implement them. The \fIgetOptionProc\fR
691 field can be NULL, which indicates that this channel type supports no type
692 specific options.
693 .PP
694 This value can be retrieved with \fBTcl_ChannelGetOptionProc\fR, which returns
695 a pointer to the function.
696 .SS WATCHPROC
697 .PP
698 The \fIwatchProc\fR field contains the address of a function called
699 by the generic layer to initialize the event notification mechanism to
700 notice events of interest on this channel.
701 \fIWatchProc\fR should match the following prototype:
702 .PP
703 .CS
704 typedef void \fBTcl_DriverWatchProc\fR(
705 ClientData \fIinstanceData\fR,
706 int \fImask\fR);
707 .CE
708 .PP
709 The \fIinstanceData\fR is the same as the value passed to
710 \fBTcl_CreateChannel\fR when this channel was created. The \fImask\fR
711 argument is an OR-ed combination of \fBTCL_READABLE\fR, \fBTCL_WRITABLE\fR
712 and \fBTCL_EXCEPTION\fR; it indicates events the caller is interested in
713 noticing on this channel.
714 .PP
715 The function should initialize device type specific mechanisms to
716 notice when an event of interest is present on the channel. When one
717 or more of the designated events occurs on the channel, the channel
718 driver is responsible for calling \fBTcl_NotifyChannel\fR to inform
719 the generic channel module. The driver should take care not to starve
720 other channel drivers or sources of callbacks by invoking
721 Tcl_NotifyChannel too frequently. Fairness can be insured by using
722 the Tcl event queue to allow the channel event to be scheduled in sequence
723 with other events. See the description of \fBTcl_QueueEvent\fR for
724 details on how to queue an event.
725 .PP
726 This value can be retrieved with \fBTcl_ChannelWatchProc\fR, which returns
727 a pointer to the function.
728 .SS GETHANDLEPROC
729 .PP
730 The \fIgetHandleProc\fR field contains the address of a function called by
731 the generic layer to retrieve a device-specific handle from the channel.
732 \fIGetHandleProc\fR should match the following prototype:
733 .PP
734 .CS
735 typedef int \fBTcl_DriverGetHandleProc\fR(
736 ClientData \fIinstanceData\fR,
737 int \fIdirection\fR,
738 ClientData *\fIhandlePtr\fR);
739 .CE
740 .PP
741 \fIInstanceData\fR is the same as the value passed to
742 \fBTcl_CreateChannel\fR when this channel was created. The \fIdirection\fR
743 argument is either \fBTCL_READABLE\fR to retrieve the handle used
744 for input, or \fBTCL_WRITABLE\fR to retrieve the handle used for
745 output.
746 .PP
747 If the channel implementation has device-specific handles, the
748 function should retrieve the appropriate handle associated with the
749 channel, according the \fIdirection\fR argument. The handle should be
750 stored in the location referred to by \fIhandlePtr\fR, and
751 \fBTCL_OK\fR should be returned. If the channel is not open for the
752 specified direction, or if the channel implementation does not use
753 device handles, the function should return \fBTCL_ERROR\fR.
754 .PP
755 This value can be retrieved with \fBTcl_ChannelGetHandleProc\fR, which returns
756 a pointer to the function.
757 .SS FLUSHPROC
758 .PP
759 The \fIflushProc\fR field is currently reserved for future use.
760 It should be set to NULL.
761 \fIFlushProc\fR should match the following prototype:
762 .PP
763 .CS
764 typedef int \fBTcl_DriverFlushProc\fR(
765 ClientData \fIinstanceData\fR);
766 .CE
767 .PP
768 This value can be retrieved with \fBTcl_ChannelFlushProc\fR, which returns
769 a pointer to the function.
770 .SS HANDLERPROC
771 .PP
772 The \fIhandlerProc\fR field contains the address of a function called by
773 the generic layer to notify the channel that an event occurred. It should
774 be defined for stacked channel drivers that wish to be notified of events
775 that occur on the underlying (stacked) channel.
776 \fIHandlerProc\fR should match the following prototype:
777 .PP
778 .CS
779 typedef int \fBTcl_DriverHandlerProc\fR(
780 ClientData \fIinstanceData\fR,
781 int \fIinterestMask\fR);
782 .CE
783 .PP
784 \fIInstanceData\fR is the same as the value passed to \fBTcl_CreateChannel\fR
785 when this channel was created. The \fIinterestMask\fR is an OR-ed
786 combination of \fBTCL_READABLE\fR or \fBTCL_WRITABLE\fR; it indicates what
787 type of event occurred on this channel.
788 .PP
789 This value can be retrieved with \fBTcl_ChannelHandlerProc\fR, which returns
790 a pointer to the function.
791
792 .SS "THREADACTIONPROC"
793 .PP
794 The \fIthreadActionProc\fR field contains the address of the function
795 called by the generic layer when a channel is created, closed, or
796 going to move to a different thread, i.e. whenever thread-specific
797 driver state might have to initialized or updated. It can be NULL.
798 The action \fITCL_CHANNEL_THREAD_REMOVE\fR is used to notify the
799 driver that it should update or remove any thread-specific data it
800 might be maintaining for the channel.
801 .PP
802 The action \fITCL_CHANNEL_THREAD_INSERT\fR is used to notify the
803 driver that it should update or initialize any thread-specific data it
804 might be maintaining using the calling thread as the associate. See
805 \fBTcl_CutChannel\fR and \fBTcl_SpliceChannel\fR for more detail.
806 .PP
807 .CS
808 typedef void \fBTcl_DriverThreadActionProc\fR(
809 ClientData \fIinstanceData\fR,
810 int \fIaction\fR);
811 .CE
812 .PP
813 \fIInstanceData\fR is the same as the value passed to
814 \fBTcl_CreateChannel\fR when this channel was created.
815 .PP
816 These values can be retrieved with \fBTcl_ChannelThreadActionProc\fR,
817 which returns a pointer to the function.
818 .SS "TRUNCATEPROC"
819 .PP
820 The \fItruncateProc\fR field contains the address of the function
821 called by the generic layer when a channel is truncated to some
822 length. It can be NULL.
823 .PP
824 .CS
825 typedef int \fBTcl_DriverTruncateProc\fR(
826 ClientData \fIinstanceData\fR,
827 Tcl_WideInt \fIlength\fR);
828 .CE
829 .PP
830 \fIInstanceData\fR is the same as the value passed to
831 \fBTcl_CreateChannel\fR when this channel was created, and
832 \fIlength\fR is the new length of the underlying file, which should
833 not be negative. The result should be 0 on success or an errno code
834 (suitable for use with \fBTcl_SetErrno\fR) on failure.
835 .PP
836 These values can be retrieved with \fBTcl_ChannelTruncateProc\fR,
837 which returns a pointer to the function.
838 .SH TCL_BADCHANNELOPTION
839 .PP
840 This procedure generates a
841 .QW "bad option"
842 error message in an
843 (optional) interpreter. It is used by channel drivers when
844 an invalid Set/Get option is requested. Its purpose is to concatenate
845 the generic options list to the specific ones and factorize
846 the generic options error message string.
847 .PP
848 It always returns \fBTCL_ERROR\fR
849 .PP
850 An error message is generated in \fIinterp\fR's result value to
851 indicate that a command was invoked with a bad option.
852 The message has the form
853 .CS
854 bad option "blah": should be one of
855 <...generic options...>+<...specific options...>
856 .CE
857 so you get for instance:
858 .CS
859 bad option "-blah": should be one of -blocking,
860 -buffering, -buffersize, -eofchar, -translation,
861 -peername, or -sockname
862 .CE
863 when called with \fIoptionList\fR equal to
864 .QW "peername sockname"
865 .PP
866 .QW blah
867 is the \fIoptionName\fR argument and
868 .QW "<specific options>"
869 is a space separated list of specific option words.
870 The function takes good care of inserting minus signs before
871 each option, commas after, and an
872 .QW or
873 before the last option.
874 .SH "OLD CHANNEL TYPES"
875 The original (8.3.1 and below) \fBTcl_ChannelType\fR structure contains
876 the following fields:
877 .PP
878 .CS
879 typedef struct Tcl_ChannelType {
880 const char *\fItypeName\fR;
881 Tcl_DriverBlockModeProc *\fIblockModeProc\fR;
882 Tcl_DriverCloseProc *\fIcloseProc\fR;
883 Tcl_DriverInputProc *\fIinputProc\fR;
884 Tcl_DriverOutputProc *\fIoutputProc\fR;
885 Tcl_DriverSeekProc *\fIseekProc\fR;
886 Tcl_DriverSetOptionProc *\fIsetOptionProc\fR;
887 Tcl_DriverGetOptionProc *\fIgetOptionProc\fR;
888 Tcl_DriverWatchProc *\fIwatchProc\fR;
889 Tcl_DriverGetHandleProc *\fIgetHandleProc\fR;
890 Tcl_DriverClose2Proc *\fIclose2Proc\fR;
891 } \fBTcl_ChannelType\fR;
892 .CE
893 .PP
894 It is still possible to create channel with the above structure. The
895 internal channel code will determine the version. It is imperative to use
896 the new \fBTcl_ChannelType\fR structure if you are creating a stacked
897 channel driver, due to problems with the earlier stacked channel
898 implementation (in 8.2.0 to 8.3.1).
899 .PP
900 Prior to 8.4.0 (i.e. during the later releases of 8.3 and early part
901 of the 8.4 development cycle) the \fBTcl_ChannelType\fR structure
902 contained the following fields:
903 .PP
904 .CS
905 typedef struct Tcl_ChannelType {
906 const char *\fItypeName\fR;
907 Tcl_ChannelTypeVersion \fIversion\fR;
908 Tcl_DriverCloseProc *\fIcloseProc\fR;
909 Tcl_DriverInputProc *\fIinputProc\fR;
910 Tcl_DriverOutputProc *\fIoutputProc\fR;
911 Tcl_DriverSeekProc *\fIseekProc\fR;
912 Tcl_DriverSetOptionProc *\fIsetOptionProc\fR;
913 Tcl_DriverGetOptionProc *\fIgetOptionProc\fR;
914 Tcl_DriverWatchProc *\fIwatchProc\fR;
915 Tcl_DriverGetHandleProc *\fIgetHandleProc\fR;
916 Tcl_DriverClose2Proc *\fIclose2Proc\fR;
917 Tcl_DriverBlockModeProc *\fIblockModeProc\fR;
918 Tcl_DriverFlushProc *\fIflushProc\fR;
919 Tcl_DriverHandlerProc *\fIhandlerProc\fR;
920 Tcl_DriverTruncateProc *\fItruncateProc\fR;
921 } \fBTcl_ChannelType\fR;
922 .CE
923 .PP
924 When the above structure is registered as a channel type, the
925 \fIversion\fR field should always be \fBTCL_CHANNEL_VERSION_2\fR.
926 .SH "SEE ALSO"
927 Tcl_Close(3), Tcl_OpenFileChannel(3), Tcl_SetErrno(3), Tcl_QueueEvent(3), Tcl_StackChannel(3), Tcl_GetStdChannel(3)
928 .SH KEYWORDS
929 blocking, channel driver, channel registration, channel type, nonblocking