1Tcl_CreateChannel(3) Tcl Library Procedures Tcl_CreateChannel(3)
2
3
4
5______________________________________________________________________________
6
8 Tcl_CreateChannel, Tcl_GetChannelInstanceData, Tcl_GetChannelType,
9 Tcl_GetChannelName, Tcl_GetChannelHandle, Tcl_GetChannelMode,
10 Tcl_GetChannelBufferSize, Tcl_SetChannelBufferSize, Tcl_NotifyChannel,
11 Tcl_BadChannelOption, Tcl_ChannelName, Tcl_ChannelVersion, Tcl_Channel‐
12 BlockModeProc, Tcl_ChannelCloseProc, Tcl_ChannelClose2Proc, Tcl_Chan‐
13 nelInputProc, Tcl_ChannelOutputProc, Tcl_ChannelSeekProc, Tcl_Channel‐
14 WideSeekProc, Tcl_ChannelSetOptionProc, Tcl_ChannelGetOptionProc,
15 Tcl_ChannelWatchProc, Tcl_ChannelGetHandleProc, Tcl_ChannelFlushProc,
16 Tcl_ChannelHandlerProc, Tcl_ChannelThreadActionProc, Tcl_IsChan‐
17 nelShared, Tcl_IsChannelRegistered, Tcl_CutChannel, Tcl_SpliceChannel,
18 Tcl_IsChannelExisting, Tcl_ClearChannelHandlers, Tcl_GetChannelThread,
19 Tcl_ChannelBuffered - procedures for creating and manipulating channels
20
22 #include <tcl.h>
23
24 Tcl_Channel
25 Tcl_CreateChannel(typePtr, channelName, instanceData, mask)
26
27 ClientData
28 Tcl_GetChannelInstanceData(channel)
29
30 Tcl_ChannelType *
31 Tcl_GetChannelType(channel)
32
33 CONST char *
34 Tcl_GetChannelName(channel)
35
36 int
37 Tcl_GetChannelHandle(channel, direction, handlePtr)
38
39 Tcl_ThreadId │
40 Tcl_GetChannelThread(channel) │
41
42 int
43 Tcl_GetChannelMode(channel)
44
45 int
46 Tcl_GetChannelBufferSize(channel)
47
48 Tcl_SetChannelBufferSize(channel, size)
49
50 Tcl_NotifyChannel(channel, mask)
51
52 int
53 Tcl_BadChannelOption(interp, optionName, optionList)
54
55 int │
56 Tcl_IsChannelShared(channel) │
57
58 int │
59 Tcl_IsChannelRegistered(interp, channel) │
60
61 int │
62 Tcl_IsChannelExisting(channelName) │
63
64 void │
65 Tcl_CutChannel(channel) │
66
67 void │
68 Tcl_SpliceChannel(channel) │
69
70 void │
71 Tcl_ClearChannelHandlers(channel) │
72
73 int
74 Tcl_ChannelBuffered(channel)
75
76 CONST char *
77 Tcl_ChannelName(typePtr)
78
79 Tcl_ChannelTypeVersion
80 Tcl_ChannelVersion(typePtr)
81
82 Tcl_DriverBlockModeProc *
83 Tcl_ChannelBlockModeProc(typePtr)
84
85 Tcl_DriverCloseProc *
86 Tcl_ChannelCloseProc(typePtr)
87
88 Tcl_DriverClose2Proc *
89 Tcl_ChannelClose2Proc(typePtr)
90
91 Tcl_DriverInputProc *
92 Tcl_ChannelInputProc(typePtr)
93
94 Tcl_DriverOutputProc *
95 Tcl_ChannelOutputProc(typePtr)
96
97 Tcl_DriverSeekProc *
98 Tcl_ChannelSeekProc(typePtr)
99
100 Tcl_DriverWideSeekProc * │
101 Tcl_ChannelWideSeekProc(typePtr) │
102
103 Tcl_DriverThreadActionProc * │
104 Tcl_ChannelThreadActionProc(typePtr) │
105
106 Tcl_DriverSetOptionProc *
107 Tcl_ChannelSetOptionProc(typePtr)
108
109 Tcl_DriverGetOptionProc *
110 Tcl_ChannelGetOptionProc(typePtr)
111
112 Tcl_DriverWatchProc *
113 Tcl_ChannelWatchProc(typePtr)
114
115 Tcl_DriverGetHandleProc *
116 Tcl_ChannelGetHandleProc(typePtr)
117
118 Tcl_DriverFlushProc *
119 Tcl_ChannelFlushProc(typePtr)
120
121 Tcl_DriverHandlerProc *
122 Tcl_ChannelHandlerProc(typePtr)
123
124
126 Tcl_ChannelType *typePtr (in) Points to a structure con‐
127 taining the addresses of
128 procedures that can be
129 called to perform I/O and
130 other functions on the chan‐
131 nel.
132
133 CONST char *channelName (in) The name of this channel,
134 such as file3; must not be
135 in use by any other channel.
136 Can be NULL, in which case
137 the channel is created with‐
138 out a name.
139
140 ClientData instanceData (in) Arbitrary one-word value to
141 be associated with this
142 channel. This value is
143 passed to procedures in
144 typePtr when they are
145 invoked.
146
147 int mask (in) OR-ed combination of
148 TCL_READABLE and
149 TCL_WRITABLE to indicate
150 whether a channel is read‐
151 able and writable.
152
153 Tcl_Channel channel (in) The channel to operate on.
154
155 int direction (in) TCL_READABLE means the input
156 handle is wanted;
157 TCL_WRITABLE means the out‐
158 put handle is wanted.
159
160 ClientData *handlePtr (out) Points to the location where
161 the desired OS-specific han‐
162 dle should be stored.
163
164 int size (in) The size, in bytes, of buf‐
165 fers to allocate in this
166 channel.
167
168 int mask (in) An OR-ed combination of
169 TCL_READABLE, TCL_WRITABLE
170 and TCL_EXCEPTION that indi‐
171 cates events that have
172 occurred on this channel.
173
174 Tcl_Interp *interp (in) Current interpreter. (can be
175 NULL)
176
177 CONST char *optionName (in) Name of the invalid option.
178
179 CONST char *optionList (in) Specific options list (space
180 separated words, without
181 "-") to append to the stan‐
182 dard generic options list.
183 Can be NULL for generic
184 options error message only.
185
186_________________________________________________________________
187
188
190 Tcl uses a two-layered channel architecture. It provides a generic
191 upper layer to enable C and Tcl programs to perform input and output
192 using the same APIs for a variety of files, devices, sockets etc. The
193 generic C APIs are described in the manual entry for Tcl_OpenFileChan‐
194 nel.
195
196 The lower layer provides type-specific channel drivers for each type of
197 device supported on each platform. This manual entry describes the C
198 APIs used to communicate between the generic layer and the type-spe‐
199 cific channel drivers. It also explains how new types of channels can
200 be added by providing new channel drivers.
201
202 Channel drivers consist of a number of components: First, each channel
203 driver provides a Tcl_ChannelType structure containing pointers to
204 functions implementing the various operations used by the generic layer
205 to communicate with the channel driver. The Tcl_ChannelType structure
206 and the functions referenced by it are described in the section
207 TCL_CHANNELTYPE, below.
208
209 Second, channel drivers usually provide a Tcl command to create
210 instances of that type of channel. For example, the Tcl open command
211 creates channels that use the file and command channel drivers, and the
212 Tcl socket command creates channels that use TCP sockets for network
213 communication.
214
215 Third, a channel driver optionally provides a C function to open chan‐
216 nel instances of that type. For example, Tcl_OpenFileChannel opens a
217 channel that uses the file channel driver, and Tcl_OpenTcpClient opens
218 a channel that uses the TCP network protocol. These creation functions
219 typically use Tcl_CreateChannel internally to open the channel.
220
221 To add a new type of channel you must implement a C API or a Tcl com‐
222 mand that opens a channel by invoking Tcl_CreateChannel. When your
223 driver calls Tcl_CreateChannel it passes in a Tcl_ChannelType structure
224 describing the driver's I/O procedures. The generic layer will then
225 invoke the functions referenced in that structure to perform operations
226 on the channel.
227
228 Tcl_CreateChannel opens a new channel and associates the supplied type‐
229 Ptr and instanceData with it. The channel is opened in the mode indi‐
230 cated by mask. For a discussion of channel drivers, their operations
231 and the Tcl_ChannelType structure, see the section TCL_CHANNELTYPE,
232 below.
233
234 Tcl_CreateChannel interacts with the code managing the standard chan‐
235 nels. Once a standard channel was initialized either through a call to
236 Tcl_GetStdChannel or a call to Tcl_SetStdChannel closing this standard
237 channel will cause the next call to Tcl_CreateChannel to make the new
238 channel the new standard channel too. See Tcl_StandardChannels for a
239 general treatise about standard channels and the behaviour of the Tcl
240 library with regard to them.
241
242 Tcl_GetChannelInstanceData returns the instance data associated with
243 the channel in channel. This is the same as the instanceData argument
244 in the call to Tcl_CreateChannel that created this channel.
245
246 Tcl_GetChannelType returns a pointer to the Tcl_ChannelType structure
247 used by the channel in the channel argument. This is the same as the
248 typePtr argument in the call to Tcl_CreateChannel that created this
249 channel.
250
251 Tcl_GetChannelName returns a string containing the name associated with
252 the channel, or NULL if the channelName argument to Tcl_CreateChannel
253 was NULL.
254
255 Tcl_GetChannelHandle places the OS-specific device handle associated
256 with channel for the given direction in the location specified by han‐
257 dlePtr and returns TCL_OK. If the channel does not have a device han‐
258 dle for the specified direction, then TCL_ERROR is returned instead.
259 Different channel drivers will return different types of handle. Refer
260 to the manual entries for each driver to determine what type of handle
261 is returned.
262
263 Tcl_GetChannelThread returns the id of the thread currently managing │
264 the specified channel. This allows channel drivers to send their file │
265 events to the correct event queue even for a multi-threaded core.
266
267 Tcl_GetChannelMode returns an OR-ed combination of TCL_READABLE and
268 TCL_WRITABLE, indicating whether the channel is open for input and out‐
269 put.
270
271 Tcl_GetChannelBufferSize returns the size, in bytes, of buffers allo‐
272 cated to store input or output in channel. If the value was not set by
273 a previous call to Tcl_SetChannelBufferSize, described below, then the
274 default value of 4096 is returned.
275
276 Tcl_SetChannelBufferSize sets the size, in bytes, of buffers that will
277 be allocated in subsequent operations on the channel to store input or
278 output. The size argument should be between ten and one million, allow‐
279 ing buffers of ten bytes to one million bytes. If size is outside this
280 range, Tcl_SetChannelBufferSize sets the buffer size to 4096.
281
282 Tcl_NotifyChannel is called by a channel driver to indicate to the
283 generic layer that the events specified by mask have occurred on the
284 channel. Channel drivers are responsible for invoking this function
285 whenever the channel handlers need to be called for the channel. See
286 WATCHPROC below for more details.
287
288 Tcl_BadChannelOption is called from driver specific set or get option
289 procs to generate a complete error message.
290
291 Tcl_ChannelBuffered returns the number of bytes of input currently
292 buffered in the internal buffer (push back area) of the channel itself.
293 It does not report about the data in the overall buffers for the stack
294 of channels the supplied channel is part of.
295
296 Tcl_IsChannelShared checks the refcount of the specified channel and │
297 returns whether the channel was shared among multiple interpreters │
298 (result == 1) or not (result == 0). │
299
300 Tcl_IsChannelRegistered checks whether the specified channel is regis‐ │
301 tered in the given interpreter (result == 1) or not (result == 0). │
302
303 Tcl_IsChannelExisting checks whether a channel with the specified name │
304 is registered in the (thread)-global list of all channels (result == 1) │
305 or not (result == 0). │
306
307 Tcl_CutChannel removes the specified channel from the (thread)global │
308 list of all channels (of the current thread). Application to a channel │
309 still registered in some interpreter is not allowed. Also notifies the │
310 driver if the Tcl_ChannelType version is TCL_CHANNEL_VERSION_4 (or │
311 higher), and Tcl_DriverThreadActionProc is defined for it.
312
313 Tcl_SpliceChannel adds the specified channel to the (thread)global list
314 of all channels (of the current thread). Application to a channel reg‐
315 istered in some interpreter is not allowed. Also notifies the driver │
316 if the Tcl_ChannelType version is TCL_CHANNEL_VERSION_4 (or higher), │
317 and Tcl_DriverThreadActionProc is defined for it.
318
319 Tcl_ClearChannelHandlers removes all channelhandlers and event scripts
320 associated with the specified channel, thus shutting down all event
321 processing for this channel.
322
323
325 A channel driver provides a Tcl_ChannelType structure that contains
326 pointers to functions that implement the various operations on a chan‐
327 nel; these operations are invoked as needed by the generic layer. The
328 structure was versioned starting in Tcl 8.3.2/8.4 to correct a problem
329 with stacked channel drivers. See the OLD CHANNEL TYPES section below
330 for details about the old structure.
331
332 The Tcl_ChannelType structure contains the following fields:
333 typedef struct Tcl_ChannelType {
334 char *typeName;
335 Tcl_ChannelTypeVersion version;
336 Tcl_DriverCloseProc *closeProc;
337 Tcl_DriverInputProc *inputProc;
338 Tcl_DriverOutputProc *outputProc;
339 Tcl_DriverSeekProc *seekProc;
340 Tcl_DriverSetOptionProc *setOptionProc;
341 Tcl_DriverGetOptionProc *getOptionProc;
342 Tcl_DriverWatchProc *watchProc;
343 Tcl_DriverGetHandleProc *getHandleProc;
344 Tcl_DriverClose2Proc *close2Proc;
345 Tcl_DriverBlockModeProc *blockModeProc;
346 Tcl_DriverFlushProc *flushProc;
347 Tcl_DriverHandlerProc *handlerProc;
348 Tcl_DriverWideSeekProc *wideSeekProc;
349 Tcl_DriverThreadActionProc *threadActionProc;
350 } Tcl_ChannelType;
351
352 It is not necessary to provide implementations for all channel opera‐
353 tions. Those which are not necessary may be set to NULL in the struct:
354 blockModeProc, seekProc, setOptionProc, getOptionProc, and close2Proc,
355 in addition to flushProc, handlerProc, and threadActionProc. Other
356 functions that cannot be implemented in a meaningful way should return
357 EINVAL when called, to indicate that the operations they represent are
358 not available. Also note that wideSeekProc can be NULL if seekProc is.
359
360 The user should only use the above structure for Tcl_ChannelType
361 instantiation. When referencing fields in a Tcl_ChannelType structure,
362 the following functions should be used to obtain the values: Tcl_Chan‐
363 nelName, Tcl_ChannelVersion, Tcl_ChannelBlockModeProc, Tcl_Channel‐
364 CloseProc, Tcl_ChannelClose2Proc, Tcl_ChannelInputProc, Tcl_ChannelOut‐
365 putProc, Tcl_ChannelSeekProc, Tcl_ChannelWideSeekProc, Tcl_Chan‐ │
366 nelThreadActionProc, Tcl_ChannelSetOptionProc, Tcl_ChannelGetOption‐
367 Proc, Tcl_ChannelWatchProc, Tcl_ChannelGetHandleProc, Tcl_ChannelFlush‐
368 Proc, or Tcl_ChannelHandlerProc.
369
370 The change to the structures was made in such a way that standard chan‐
371 nel types are binary compatible. However, channel types that use
372 stacked channels (ie: TLS, Trf) have new versions to correspond to the
373 above change since the previous code for stacked channels had problems.
374
375
377 The typeName field contains a null-terminated string that identifies
378 the type of the device implemented by this driver, e.g. file or
379 socket.
380
381 This value can be retrieved with Tcl_ChannelName, which returns a
382 pointer to the string.
383
384
386 The version field should be set to the version of the structure that
387 you require. TCL_CHANNEL_VERSION_2 is the minimum recommended. │
388 TCL_CHANNEL_VERSION_3 must be set to specifiy the wideSeekProc member. │
389 TCL_CHANNEL_VERSION_4 must be set to specifiy the threadActionProc mem‐ │
390 ber (includes wideSeekProc). If it is not set to any of these, then
391 this Tcl_ChannelType is assumed to have the original structure. See
392 OLD CHANNEL TYPES for more details. While Tcl will recognize and func‐
393 tion with either structures, stacked channels must be of at least
394 TCL_CHANNEL_VERSION_2 to function correctly.
395
396 This value can be retrieved with Tcl_ChannelVersion, which returns one │
397 of TCL_CHANNEL_VERSION_4, TCL_CHANNEL_VERSION_3, TCL_CHANNEL_VERSION_2,
398 or TCL_CHANNEL_VERSION_1.
399
400
402 The blockModeProc field contains the address of a function called by
403 the generic layer to set blocking and nonblocking mode on the device.
404 BlockModeProc should match the following prototype:
405
406 typedef int Tcl_DriverBlockModeProc(
407 ClientData instanceData,
408 int mode);
409
410 The instanceData is the same as the value passed to Tcl_CreateChannel
411 when this channel was created. The mode argument is either
412 TCL_MODE_BLOCKING or TCL_MODE_NONBLOCKING to set the device into block‐
413 ing or nonblocking mode. The function should return zero if the opera‐
414 tion was successful, or a nonzero POSIX error code if the operation
415 failed.
416
417 If the operation is successful, the function can modify the supplied
418 instanceData to record that the channel entered blocking or nonblocking
419 mode and to implement the blocking or nonblocking behavior. For some
420 device types, the blocking and nonblocking behavior can be implemented
421 by the underlying operating system; for other device types, the behav‐
422 ior must be emulated in the channel driver.
423
424 This value can be retrieved with Tcl_ChannelBlockModeProc, which
425 returns a pointer to the function.
426
427 A channel driver not supplying a blockModeProc has to be very, very
428 careful. It has to tell the generic layer exactly which blocking mode
429 is acceptable to it, and should this also document for the user so that
430 the blocking mode of the channel is not changed to an inacceptable
431 value. Any confusion here may lead the interpreter into a (spurious and
432 difficult to find) deadlock.
433
434
435
437 The closeProc field contains the address of a function called by the
438 generic layer to clean up driver-related information when the channel
439 is closed. CloseProc must match the following prototype:
440
441 typedef int Tcl_DriverCloseProc(
442 ClientData instanceData,
443 Tcl_Interp *interp);
444
445 The instanceData argument is the same as the value provided to Tcl_Cre‐
446 ateChannel when the channel was created. The function should release
447 any storage maintained by the channel driver for this channel, and
448 close the input and output devices encapsulated by this channel. All
449 queued output will have been flushed to the device before this function
450 is called, and no further driver operations will be invoked on this
451 instance after calling the closeProc. If the close operation is suc‐
452 cessful, the procedure should return zero; otherwise it should return a
453 nonzero POSIX error code. In addition, if an error occurs and interp is
454 not NULL, the procedure should store an error message in the inter‐
455 preter's result.
456
457 Alternatively, channels that support closing the read and write sides
458 independently may set closeProc to TCL_CLOSE2PROC and set close2Proc to
459 the address of a function that matches the following prototype:
460
461 typedef int Tcl_DriverClose2Proc(
462 ClientData instanceData,
463 Tcl_Interp *interp,
464 int flags);
465
466 The close2Proc will be called with flags set to an OR'ed combination of
467 TCL_CLOSE_READ or TCL_CLOSE_WRITE to indicate that the driver should
468 close the read and/or write side of the channel. The channel driver
469 may be invoked to perform additional operations on the channel after
470 close2Proc is called to close one or both sides of the channel. If
471 flags is 0 (zero), the driver should close the channel in the manner
472 described above for closeProc. No further operations will be invoked
473 on this instance after close2Proc is called with all flags cleared. In
474 all cases, the close2Proc function should return zero if the close
475 operation was successful; otherwise it should return a nonzero POSIX
476 error code. In addition, if an error occurs and interp is not NULL, the
477 procedure should store an error message in the interpreter's result.
478
479 These value can be retrieved with Tcl_ChannelCloseProc or Tcl_Channel‐
480 Close2Proc, which returns a pointer to the respective function.
481
482
484 The inputProc field contains the address of a function called by the
485 generic layer to read data from the file or device and store it in an
486 internal buffer. InputProc must match the following prototype:
487
488 typedef int Tcl_DriverInputProc(
489 ClientData instanceData,
490 char *buf,
491 int bufSize,
492 int *errorCodePtr);
493
494 InstanceData is the same as the value passed to Tcl_CreateChannel when
495 the channel was created. The buf argument points to an array of bytes
496 in which to store input from the device, and the bufSize argument indi‐
497 cates how many bytes are available at buf.
498
499 The errorCodePtr argument points to an integer variable provided by the
500 generic layer. If an error occurs, the function should set the variable
501 to a POSIX error code that identifies the error that occurred.
502
503 The function should read data from the input device encapsulated by the
504 channel and store it at buf. On success, the function should return a
505 nonnegative integer indicating how many bytes were read from the input
506 device and stored at buf. On error, the function should return -1. If
507 an error occurs after some data has been read from the device, that
508 data is lost.
509
510 If inputProc can determine that the input device has some data avail‐
511 able but less than requested by the bufSize argument, the function
512 should only attempt to read as much data as is available and return
513 without blocking. If the input device has no data available whatsoever
514 and the channel is in nonblocking mode, the function should return an
515 EAGAIN error. If the input device has no data available whatsoever and
516 the channel is in blocking mode, the function should block for the
517 shortest possible time until at least one byte of data can be read from
518 the device; then, it should return as much data as it can read without
519 blocking.
520
521 This value can be retrieved with Tcl_ChannelInputProc, which returns a
522 pointer to the function.
523
524
526 The outputProc field contains the address of a function called by the
527 generic layer to transfer data from an internal buffer to the output
528 device. OutputProc must match the following prototype:
529
530 typedef int Tcl_DriverOutputProc(
531 ClientData instanceData,
532 CONST char *buf,
533 int toWrite,
534 int *errorCodePtr);
535
536 InstanceData is the same as the value passed to Tcl_CreateChannel when
537 the channel was created. The buf argument contains an array of bytes to
538 be written to the device, and the toWrite argument indicates how many
539 bytes are to be written from the buf argument.
540
541 The errorCodePtr argument points to an integer variable provided by the
542 generic layer. If an error occurs, the function should set this vari‐
543 able to a POSIX error code that identifies the error.
544
545 The function should write the data at buf to the output device encapsu‐
546 lated by the channel. On success, the function should return a nonnega‐
547 tive integer indicating how many bytes were written to the output
548 device. The return value is normally the same as toWrite, but may be
549 less in some cases such as if the output operation is interrupted by a
550 signal. If an error occurs the function should return -1. In case of
551 error, some data may have been written to the device.
552
553 If the channel is nonblocking and the output device is unable to absorb
554 any data whatsoever, the function should return -1 with an EAGAIN error
555 without writing any data.
556
557 This value can be retrieved with Tcl_ChannelOutputProc, which returns a
558 pointer to the function.
559
560
562 The seekProc field contains the address of a function called by the
563 generic layer to move the access point at which subsequent input or
564 output operations will be applied. SeekProc must match the following
565 prototype:
566
567 typedef int Tcl_DriverSeekProc(
568 ClientData instanceData,
569 long offset,
570 int seekMode,
571 int *errorCodePtr);
572
573 The instanceData argument is the same as the value given to Tcl_Create‐
574 Channel when this channel was created. Offset and seekMode have the
575 same meaning as for the Tcl_Seek procedure (described in the manual
576 entry for Tcl_OpenFileChannel).
577
578 The errorCodePtr argument points to an integer variable provided by the
579 generic layer for returning errno values from the function. The func‐
580 tion should set this variable to a POSIX error code if an error occurs.
581 The function should store an EINVAL error code if the channel type does
582 not implement seeking.
583
584 The return value is the new access point or -1 in case of error. If an
585 error occurred, the function should not move the access point.
586
587 If there is a non-NULL seekProc field, the wideSeekProc field may con‐ │
588 tain the address of an alternative function to use which handles wide │
589 (i.e. larger than 32-bit) offsets, so allowing seeks within files │
590 larger than 2GB. The wideSeekProc will be called in preference to the │
591 seekProc, but both must be defined if the wideSeekProc is defined. │
592 WideSeekProc must match the following prototype: │
593
594 typedef Tcl_WideInt Tcl_DriverWideSeekProc( │
595 ClientData instanceData, │
596 Tcl_WideInt offset, │
597 int seekMode, │
598 int *errorCodePtr); │
599
600 The arguments and return values mean the same thing as with seekProc │
601 above, except that the type of offsets and the return type are differ‐ │
602 ent. │
603
604 The seekProc value can be retrieved with Tcl_ChannelSeekProc, which │
605 returns a pointer to the function, and similarly the wideSeekProc can │
606 be retrieved with Tcl_ChannelWideSeekProc.
607
608
610 The setOptionProc field contains the address of a function called by
611 the generic layer to set a channel type specific option on a channel.
612 setOptionProc must match the following prototype:
613
614 typedef int Tcl_DriverSetOptionProc(
615 ClientData instanceData,
616 Tcl_Interp *interp,
617 CONST char *optionName,
618 CONST char *newValue);
619
620 optionName is the name of an option to set, and newValue is the new
621 value for that option, as a string. The instanceData is the same as the
622 value given to Tcl_CreateChannel when this channel was created. The
623 function should do whatever channel type specific action is required to
624 implement the new value of the option.
625
626 Some options are handled by the generic code and this function is never
627 called to set them, e.g. -blockmode. Other options are specific to each
628 channel type and the setOptionProc procedure of the channel driver will
629 get called to implement them. The setOptionProc field can be NULL,
630 which indicates that this channel type supports no type specific
631 options.
632
633 If the option value is successfully modified to the new value, the
634 function returns TCL_OK. It should call Tcl_BadChannelOption which
635 itself returns TCL_ERROR if the optionName is unrecognized. If new‐
636 Value specifies a value for the option that is not supported or if a
637 system call error occurs, the function should leave an error message in
638 the result field of interp if interp is not NULL. The function should
639 also call Tcl_SetErrno to store an appropriate POSIX error code.
640
641 This value can be retrieved with Tcl_ChannelSetOptionProc, which
642 returns a pointer to the function.
643
644
646 The getOptionProc field contains the address of a function called by
647 the generic layer to get the value of a channel type specific option on
648 a channel. getOptionProc must match the following prototype:
649
650 typedef int Tcl_DriverGetOptionProc(
651 ClientData instanceData,
652 Tcl_Interp *interp,
653 CONST char *optionName,
654 Tcl_DString *optionValue);
655
656 OptionName is the name of an option supported by this type of channel.
657 If the option name is not NULL, the function stores its current value,
658 as a string, in the Tcl dynamic string optionValue. If optionName is
659 NULL, the function stores in optionValue an alternating list of all
660 supported options and their current values. On success, the function
661 returns TCL_OK. It should call Tcl_BadChannelOption which itself
662 returns TCL_ERROR if the optionName is unrecognized. If a system call
663 error occurs, the function should leave an error message in the result
664 of interp if interp is not NULL. The function should also call
665 Tcl_SetErrno to store an appropriate POSIX error code.
666
667 Some options are handled by the generic code and this function is never
668 called to retrieve their value, e.g. -blockmode. Other options are spe‐
669 cific to each channel type and the getOptionProc procedure of the chan‐
670 nel driver will get called to implement them. The getOptionProc field
671 can be NULL, which indicates that this channel type supports no type
672 specific options.
673
674 This value can be retrieved with Tcl_ChannelGetOptionProc, which
675 returns a pointer to the function.
676
677
679 The watchProc field contains the address of a function called by the
680 generic layer to initialize the event notification mechanism to notice
681 events of interest on this channel. WatchProc should match the follow‐
682 ing prototype:
683
684 typedef void Tcl_DriverWatchProc(
685 ClientData instanceData,
686 int mask);
687
688 The instanceData is the same as the value passed to Tcl_CreateChannel
689 when this channel was created. The mask argument is an OR-ed combina‐
690 tion of TCL_READABLE, TCL_WRITABLE and TCL_EXCEPTION; it indicates
691 events the caller is interested in noticing on this channel.
692
693 The function should initialize device type specific mechanisms to
694 notice when an event of interest is present on the channel. When one
695 or more of the designated events occurs on the channel, the channel
696 driver is responsible for calling Tcl_NotifyChannel to inform the
697 generic channel module. The driver should take care not to starve
698 other channel drivers or sources of callbacks by invoking Tcl_Notify‐
699 Channel too frequently. Fairness can be insured by using the Tcl event
700 queue to allow the channel event to be scheduled in sequence with other
701 events. See the description of Tcl_QueueEvent for details on how to
702 queue an event.
703
704 This value can be retrieved with Tcl_ChannelWatchProc, which returns a
705 pointer to the function.
706
707
709 The getHandleProc field contains the address of a function called by
710 the generic layer to retrieve a device-specific handle from the chan‐
711 nel. GetHandleProc should match the following prototype:
712
713 typedef int Tcl_DriverGetHandleProc(
714 ClientData instanceData,
715 int direction,
716 ClientData *handlePtr);
717
718 InstanceData is the same as the value passed to Tcl_CreateChannel when
719 this channel was created. The direction argument is either TCL_READABLE
720 to retrieve the handle used for input, or TCL_WRITABLE to retrieve the
721 handle used for output.
722
723 If the channel implementation has device-specific handles, the function
724 should retrieve the appropriate handle associated with the channel,
725 according the direction argument. The handle should be stored in the
726 location referred to by handlePtr, and TCL_OK should be returned. If
727 the channel is not open for the specified direction, or if the channel
728 implementation does not use device handles, the function should return
729 TCL_ERROR.
730
731 This value can be retrieved with Tcl_ChannelGetHandleProc, which
732 returns a pointer to the function.
733
734
736 The flushProc field is currently reserved for future use. It should be
737 set to NULL. FlushProc should match the following prototype:
738
739 typedef int Tcl_DriverFlushProc(
740 ClientData instanceData);
741
742 This value can be retrieved with Tcl_ChannelFlushProc, which returns a
743 pointer to the function.
744
745
747 The handlerProc field contains the address of a function called by the
748 generic layer to notify the channel that an event occurred. It should
749 be defined for stacked channel drivers that wish to be notified of
750 events that occur on the underlying (stacked) channel. HandlerProc
751 should match the following prototype:
752
753 typedef int Tcl_DriverHandlerProc(
754 ClientData instanceData,
755 int interestMask);
756
757 InstanceData is the same as the value passed to Tcl_CreateChannel when
758 this channel was created. The interestMask is an OR-ed combination of
759 TCL_READABLE or TCL_WRITABLE; it indicates what type of event occurred
760 on this channel.
761
762 This value can be retrieved with Tcl_ChannelHandlerProc, which returns
763 a pointer to the function.
764
765
767 The threadActionProc field contains the address of the function called │
768 by the generic layer when a channel is created, closed, or going to │
769 move to a different thread, i.e. whenever thread-specific driver state │
770 might have to initialized or updated. It can be NULL. The action │
771 TCL_CHANNEL_THREAD_REMOVE is used to notify the driver that it should │
772 update or remove any thread-specific data it might be maintaining for │
773 the channel. │
774
775 The action TCL_CHANNEL_THREAD_INSERT is used to notify the driver that │
776 it should update or initialize any thread-specific data it might be │
777 maintaining using the calling thread as the associate. See Tcl_CutChan‐ │
778 nel and Tcl_SpliceChannel for more detail. │
779
780 typedef void Tcl_DriverThreadActionProc( │
781 ClientData instanceData, │
782 int action); │
783
784 InstanceData is the same as the value passed to Tcl_CreateChannel when │
785 this channel was created. │
786
787 These values can be retrieved with Tcl_ChannelThreadActionProc, which │
788 returns a pointer to the function.
789
790
792 This procedure generates a "bad option" error message in an (optional)
793 interpreter. It is used by channel drivers when a invalid Set/Get
794 option is requested. Its purpose is to concatenate the generic options
795 list to the specific ones and factorize the generic options error mes‐
796 sage string.
797
798 It always return TCL_ERROR
799
800 An error message is generated in interp's result object to indicate
801 that a command was invoked with the a bad option The message has the
802 form
803 bad option "blah": should be one of
804 <...generic options...>+<...specific options...>
805 so you get for instance:
806 bad option "-blah": should be one of -blocking,
807 -buffering, -buffersize, -eofchar, -translation,
808 -peername, or -sockname
809 when called with optionList="peername sockname"
810 ``blah'' is the optionName argument and ``<specific options>'' is a
811 space separated list of specific option words. The function takes good
812 care of inserting minus signs before each option, commas after, and an
813 ``or'' before the last option.
814
815
817 The original (8.3.1 and below) Tcl_ChannelType structure contains the
818 following fields:
819
820 typedef struct Tcl_ChannelType {
821 char *typeName;
822 Tcl_DriverBlockModeProc *blockModeProc;
823 Tcl_DriverCloseProc *closeProc;
824 Tcl_DriverInputProc *inputProc;
825 Tcl_DriverOutputProc *outputProc;
826 Tcl_DriverSeekProc *seekProc;
827 Tcl_DriverSetOptionProc *setOptionProc;
828 Tcl_DriverGetOptionProc *getOptionProc;
829 Tcl_DriverWatchProc *watchProc;
830 Tcl_DriverGetHandleProc *getHandleProc;
831 Tcl_DriverClose2Proc *close2Proc;
832 } Tcl_ChannelType;
833
834 It is still possible to create channel with the above structure. The
835 internal channel code will determine the version. It is imperative to
836 use the new Tcl_ChannelType structure if you are creating a stacked
837 channel driver, due to problems with the earlier stacked channel imple‐
838 mentation (in 8.2.0 to 8.3.1).
839
840 Prior to 8.4.0 (i.e. during the later releases of 8.3 and early part of │
841 the 8.4 development cycle) the Tcl_ChannelType structure contained the │
842 following fields: │
843
844 typedef struct Tcl_ChannelType { │
845 char *typeName; │
846 Tcl_ChannelTypeVersion version; │
847 Tcl_DriverCloseProc *closeProc; │
848 Tcl_DriverInputProc *inputProc; │
849 Tcl_DriverOutputProc *outputProc; │
850 Tcl_DriverSeekProc *seekProc; │
851 Tcl_DriverSetOptionProc *setOptionProc; │
852 Tcl_DriverGetOptionProc *getOptionProc; │
853 Tcl_DriverWatchProc *watchProc; │
854 Tcl_DriverGetHandleProc *getHandleProc; │
855 Tcl_DriverClose2Proc *close2Proc; │
856 Tcl_DriverBlockModeProc *blockModeProc; │
857 Tcl_DriverFlushProc *flushProc; │
858 Tcl_DriverHandlerProc *handlerProc; │
859 } Tcl_ChannelType; │
860
861 When the above structure is registered as a channel type, the version │
862 field should always be TCL_CHANNEL_VERSION_2.
863
864
866 Tcl_Close(3), Tcl_OpenFileChannel(3), Tcl_SetErrno(3),
867 Tcl_QueueEvent(3), Tcl_StackChannel(3), Tcl_GetStdChannel(3)
868
869
871 blocking, channel driver, channel registration, channel type, nonblock‐
872 ing
873
874
875
876Tcl 8.4 Tcl_CreateChannel(3)