Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/CrtChannel.3 @ 42

Last change on this file since 42 was 25, checked in by landauf, 17 years ago

added tcl to libs

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