Debian Codegraph @

doc/refchan.n

raw
1 '\"
2 '\" Copyright (c) 2006 Andreas Kupries <[email protected]>
3 '\"
4 '\" See the file "license.terms" for information on usage and redistribution
5 '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
6 '\"
7 .TH refchan n 8.5 Tcl "Tcl Built-In Commands"
8 .so man.macros
9 .BS
10 .\" Note: do not modify the .SH NAME line immediately below!
11 .SH NAME
12 refchan \- command handler API of reflected channels
13 .SH SYNOPSIS
14 \fBcmdPrefix \fIoption\fR ?\fIarg arg ...\fR?
15 .BE
16 .SH DESCRIPTION
17 .PP
18 The Tcl-level handler for a reflected channel has to be a command with
19 subcommands (termed an \fIensemble\fR, as it is a command such as that
20 created by \fBnamespace ensemble\fR \fBcreate\fR, though the implementation
21 of handlers for reflected channel \fIis not\fR tied to \fBnamespace
22 ensemble\fRs in any way; see \fBEXAMPLE\fR below for how to build an
23 \fBoo::class\fR that supports the API). Note that \fIcmdPrefix\fR is whatever was
24 specified in the call to \fBchan create\fR, and may consist of
25 multiple arguments; this will be expanded to multiple words in place
26 of the prefix.
27 .PP
28 Of all the possible subcommands, the handler \fImust\fR support
29 \fBinitialize\fR, \fBfinalize\fR, and \fBwatch\fR. Support for the
30 other subcommands is optional.
31 .SS "MANDATORY SUBCOMMANDS"
32 .TP
33 \fIcmdPrefix \fBinitialize \fIchannelId mode\fR
34 .
35 An invocation of this subcommand will be the first call the
36 \fIcmdPrefix\fR will receive for the specified new \fIchannelId\fR. It
37 is the responsibility of this subcommand to set up any internal data
38 structures required to keep track of the channel and its state.
39 .RS
40 .PP
41 The return value of the method has to be a list containing the names
42 of all subcommands supported by the \fIcmdPrefix\fR. This also tells
43 the Tcl core which version of the API for reflected channels is used by
44 this command handler.
45 .PP
46 Any error thrown by the method will abort the creation of the channel
47 and no channel will be created. The thrown error will appear as error
48 thrown by \fBchan create\fR. Any exception other than an \fBerror\fR
49 (e.g.,\ \fBbreak\fR, etc.) is treated as (and converted to) an error.
50 .PP
51 \fBNote:\fR If the creation of the channel was aborted due to failures
52 here, then the \fBfinalize\fR subcommand will not be called.
53 .PP
54 The \fImode\fR argument tells the handler whether the channel was
55 opened for reading, writing, or both. It is a list containing any of
56 the strings \fBread\fR or \fBwrite\fR. The list may be empty, but
57 will usually contain at least one element.
58 .PP
59 The subcommand must throw an error if the chosen mode is not
60 supported by the \fIcmdPrefix\fR.
61 .RE
62 .TP
63 \fIcmdPrefix \fBfinalize \fIchannelId\fR
64 .
65 An invocation of this subcommand will be the last call the
66 \fIcmdPrefix\fR will receive for the specified \fIchannelId\fR. It will
67 be generated just before the destruction of the data structures of the
68 channel held by the Tcl core. The command handler \fImust not\fR
69 access the \fIchannelId\fR anymore in no way. Upon this subcommand being
70 called, any internal resources allocated to this channel must be
71 cleaned up.
72 .RS
73 .PP
74 The return value of this subcommand is ignored.
75 .PP
76 If the subcommand throws an error the command which caused its
77 invocation (usually \fBchan close\fR) will appear to have thrown this
78 error. Any exception beyond \fBerror\fR (e.g.,\ \fBbreak\fR, etc.) is
79 treated as (and converted to) an error.
80 .PP
81 This subcommand is not invoked if the creation of the channel was
82 aborted during \fBinitialize\fR (See above).
83 .RE
84 .TP
85 \fIcmdPrefix \fBwatch \fIchannelId eventspec\fR
86 .
87 This subcommand notifies the \fIcmdPrefix\fR that the specified
88 \fIchannelId\fR is interested in the events listed in the
89 \fIeventspec\fR. This argument is a list containing any of \fBread\fR
90 and \fBwrite\fR. The list may be empty, which signals that the
91 channel does not wish to be notified of any events. In that situation,
92 the handler should disable event generation completely.
93 .RS
94 .PP
95 \fBWarning:\fR Any return value of the subcommand is ignored. This
96 includes all errors thrown by the subcommand, \fBbreak\fR, \fBcontinue\fR, and
97 custom return codes.
98 .PP
99 This subcommand interacts with \fBchan postevent\fR. Trying to post an
100 event which was not listed in the last call to \fBwatch\fR will cause
101 \fBchan postevent\fR to throw an error.
102 .RE
103 .SS "OPTIONAL SUBCOMMANDS"
104 .TP
105 \fIcmdPrefix \fBread \fIchannelId count\fR
106 .
107 This \fIoptional\fR subcommand is called when the user requests data from the
108 channel \fIchannelId\fR. \fIcount\fR specifies how many \fIbytes\fR have been
109 requested. If the subcommand is not supported then it is not possible to read
110 from the channel handled by the command.
111 .RS
112 .PP
113 The return value of this subcommand is taken as the requested data
114 \fIbytes\fR. If the returned data contains more bytes than requested,
115 an error will be signaled and later thrown by the command which
116 performed the read (usually \fBgets\fR or \fBread\fR). However,
117 returning fewer bytes than requested is acceptable.
118 .PP
119 Note that returning nothing (0 bytes) is a signal to the higher layers
120 that \fBEOF\fR has been reached on the channel. To signal that the
121 channel is out of data right now, but has not yet reached \fBEOF\fR,
122 it is necessary to throw the error "EAGAIN", i.e. to either
123 .PP
124 .CS
125 return -code error EAGAIN
126 .CE
127 or
128 .CS
129 error EAGAIN
130 .CE
131 .PP
132 For extensibility any error whose value is a negative integer number
133 will cause the higher layers to set the C-level variable "\fBerrno\fR"
134 to the absolute value of this number, signaling a system error.
135 However, note that the exact mapping between these error numbers and
136 their meanings is operating system dependent.
137 .PP
138 For example, while on Linux both
139 .PP
140 .CS
141 return -code error -11
142 .CE
143 and
144 .CS
145 error -11
146 .CE
147 .PP
148 are equivalent to the examples above, using the more readable string "EAGAIN",
149 this is not true for BSD, where the equivalent number is -35.
150 .PP
151 The symbolic string however is the same across systems, and internally
152 translated to the correct number. No other error value has such a mapping
153 to a symbolic string.
154 .PP
155 If the subcommand throws any other error, the command which caused its
156 invocation (usually \fBgets\fR, or \fBread\fR) will appear to have
157 thrown this error. Any exception beyond \fBerror\fR, (e.g.,\ \fBbreak\fR,
158 etc.) is treated as and converted to an error.
159 .RE
160 .TP
161 \fIcmdPrefix \fBwrite \fIchannelId data\fR
162 .
163 This \fIoptional\fR subcommand is called when the user writes data to
164 the channel \fIchannelId\fR. The \fIdata\fR argument contains \fIbytes\fR, not
165 characters. Any type of transformation (EOL, encoding) configured for
166 the channel has already been applied at this point. If this subcommand
167 is not supported then it is not possible to write to the channel
168 handled by the command.
169 .RS
170 .PP
171 The return value of the subcommand is taken as the number of bytes
172 written by the channel. Anything non-numeric will cause an error to be
173 signaled and later thrown by the command which performed the write. A
174 negative value implies that the write failed. Returning a value
175 greater than the number of bytes given to the handler, or zero, is
176 forbidden and will cause the Tcl core to throw an error.
177 .PP
178 To signal that the channel is not able to accept data for writing
179 right now, it is necessary to throw the error "EAGAIN", i.e. to either
180 .PP
181 .CS
182 return -code error EAGAIN
183 .CE
184 or
185 .CS
186 error EAGAIN
187 .CE
188 .PP
189 For extensibility any error whose value is a negative integer number
190 will cause the higher layers to set the C-level variable "\fBerrno\fR"
191 to the absolute value of this number, signaling a system error.
192 However, note that the exact mapping between these error numbers and
193 their meanings is operating system dependent.
194 .PP
195 For example, while on Linux both
196 .PP
197 .CS
198 return -code error -11
199 .CE
200 and
201 .CS
202 error -11
203 .CE
204 .PP
205 are equivalent to the examples above, using the more readable string "EAGAIN",
206 this is not true for BSD, where the equivalent number is -35.
207 .PP
208 The symbolic string however is the same across systems, and internally
209 translated to the correct number. No other error value has such a mapping
210 to a symbolic string.
211 .PP
212 If the subcommand throws any other error the command which caused its
213 invocation (usually \fBputs\fR) will appear to have thrown this error.
214 Any exception beyond \fBerror\fR (e.g.,\ \fBbreak\fR, etc.) is treated
215 as and converted to an error.
216 .RE
217 .TP
218 \fIcmdPrefix \fBseek \fIchannelId offset base\fR
219 .
220 This \fIoptional\fR subcommand is responsible for the handling of
221 \fBchan seek\fR and \fBchan tell\fR requests on the channel
222 \fIchannelId\fR. If it is not supported then seeking will not be possible for
223 the channel.
224 .RS
225 .PP
226 The \fIbase\fR argument is the same as the equivalent argument of the
227 builtin \fBchan seek\fR, namely:
228 .TP 10
229 \fBstart\fR
230 .
231 Seeking is relative to the beginning of the channel.
232 .TP 10
233 \fBcurrent\fR
234 .
235 Seeking is relative to the current seek position.
236 .TP 10
237 \fBend\fR
238 .
239 Seeking is relative to the end of the channel.
240 .PP
241 The \fIoffset\fR is an integer number specifying the amount of
242 \fBbytes\fR to seek forward or backward. A positive number should seek
243 forward, and a negative number should seek backward.
244 A channel may provide only limited seeking. For example sockets can
245 seek forward, but not backward.
246 .PP
247 The return value of the subcommand is taken as the (new) location of
248 the channel, counted from the start. This has to be an integer number
249 greater than or equal to zero.
250 If the subcommand throws an error the command which caused its
251 invocation (usually \fBchan seek\fR, or \fBchan tell\fR) will appear to have
252 thrown this error. Any exception beyond \fBerror\fR (e.g.,\ \fBbreak\fR,
253 etc.) is treated as and converted to an error.
254 .PP
255 The offset/base combination of 0/\fBcurrent\fR signals a \fBchan tell\fR
256 request, i.e.,\ seek nothing relative to the current location, making
257 the new location identical to the current one, which is then returned.
258 .RE
259 .TP
260 \fIcmdPrefix \fBconfigure \fIchannelId option value\fR
261 .
262 This \fIoptional\fR subcommand is for setting the type-specific options of
263 channel \fIchannelId\fR. The \fIoption\fR argument indicates the option to be
264 written, and the \fIvalue\fR argument indicates the value to set the option to.
265 .RS
266 .PP
267 This subcommand will never try to update more than one option at a
268 time; that is behavior implemented in the Tcl channel core.
269 .PP
270 The return value of the subcommand is ignored.
271 .PP
272 If the subcommand throws an error the command which performed the
273 (re)configuration or query (usually \fBfconfigure\fR or
274 \fBchan configure\fR) will appear to have thrown this error. Any exception
275 beyond \fBerror\fR (e.g.,\ \fBbreak\fR, etc.) is treated as and
276 converted to an error.
277 .RE
278 .TP
279 \fIcmdPrefix \fBcget \fIchannelId option\fR
280 .
281 This \fIoptional\fR subcommand is used when reading a single type-specific
282 option of channel \fIchannelId\fR. If this subcommand is supported then the
283 subcommand \fBcgetall\fR must be supported as well.
284 .RS
285 .PP
286 The subcommand should return the value of the specified \fIoption\fR.
287 .PP
288 If the subcommand throws an error, the command which performed the
289 (re)configuration or query (usually \fBfconfigure\fR or \fBchan configure\fR)
290 will appear to have thrown this error. Any exception beyond \fIerror\fR
291 (e.g.,\ \fBbreak\fR, etc.) is treated as and converted to an error.
292 .RE
293 .TP
294 \fIcmdPrefix \fBcgetall \fIchannelId\fR
295 .
296 This \fIoptional\fR subcommand is used for reading all type-specific options
297 of channel \fIchannelId\fR. If this subcommand is supported then the
298 subcommand \fBcget\fR has to be supported as well.
299 .RS
300 .PP
301 The subcommand should return a list of all options and their values.
302 This list must have an even number of elements.
303 .PP
304 If the subcommand throws an error the command which performed the
305 (re)configuration or query (usually \fBfconfigure\fR or \fBchan configure\fR)
306 will appear to have thrown this error. Any exception beyond \fBerror\fR
307 (e.g.,\ \fBbreak\fR, etc.) is treated as and converted to an error.
308 .RE
309 .TP
310 \fIcmdPrefix \fBblocking \fIchannelId mode\fR
311 .
312 This \fIoptional\fR subcommand handles changes to the blocking mode of the
313 channel \fIchannelId\fR. The \fImode\fR is a boolean flag. A true value means
314 that the channel has to be set to blocking, and a false value means that the
315 channel should be non-blocking.
316 .RS
317 .PP
318 The return value of the subcommand is ignored.
319 .PP
320 If the subcommand throws an error the command which caused its
321 invocation (usually \fBfconfigure\fR or \fBchan configure\fR) will appear to
322 have thrown this error. Any exception beyond \fBerror\fR (e.g.,\ \fBbreak\fR,
323 etc.) is treated as and converted to an error.
324 .RE
325 .SH NOTES
326 Some of the functions supported in channels defined in Tcl's C
327 interface are not available to channels reflected to the Tcl level.
328 .PP
329 The function \fBTcl_DriverGetHandleProc\fR is not supported;
330 i.e.,\ reflected channels do not have OS specific handles.
331 .PP
332 The function \fBTcl_DriverHandlerProc\fR is not supported. This driver
333 function is relevant only for stacked channels, i.e.,\ transformations.
334 Reflected channels are always base channels, not transformations.
335 .PP
336 The function \fBTcl_DriverFlushProc\fR is not supported. This is
337 because the current generic I/O layer of Tcl does not use this
338 function anywhere at all. Therefore support at the Tcl level makes no
339 sense either. This may be altered in the future (through extending the
340 API defined here and changing its version number) should the function
341 be used at some time in the future.
342 .SH EXAMPLE
343 .PP
344 This demonstrates how to make a channel that reads from a string.
345 .PP
346 .CS
347 oo::class create stringchan {
348 variable data pos
349 constructor {string {encoding {}}} {
350 if {$encoding eq ""} {set encoding [encoding system]}
351 set data [encoding convertto $encoding $string]
352 set pos 0
353 }
354
355 method \fBinitialize\fR {ch mode} {
356 return "initialize finalize watch read seek"
357 }
358 method \fBfinalize\fR {ch} {
359 my destroy
360 }
361 method \fBwatch\fR {ch events} {
362 # Must be present but we ignore it because we do not
363 # post any events
364 }
365
366 # Must be present on a readable channel
367 method \fBread\fR {ch count} {
368 set d [string range $data $pos [expr {$pos+$count-1}]]
369 incr pos [string length $d]
370 return $d
371 }
372
373 # This method is optional, but useful for the example below
374 method \fBseek\fR {ch offset base} {
375 switch $base {
376 start {
377 set pos $offset
378 }
379 current {
380 incr pos $offset
381 }
382 end {
383 set pos [string length $data]
384 incr pos $offset
385 }
386 }
387 if {$pos < 0} {
388 set pos 0
389 } elseif {$pos > [string length $data]} {
390 set pos [string length $data]
391 }
392 return $pos
393 }
394 }
395
396 # Now we create an instance...
397 set string "The quick brown fox jumps over the lazy dog.\\n"
398 set ch [\fBchan create\fR read [stringchan new $string]]
399
400 puts [gets $ch]; # Prints the whole string
401
402 seek $ch -5 end;
403 puts [read $ch]; # Prints just the last word
404 .CE
405 .SH "SEE ALSO"
406 chan(n), transchan(n)
407 .SH KEYWORDS
408 API, channel, ensemble, prefix, reflection
409 '\" Local Variables:
410 '\" mode: nroff
411 '\" End: