| 1 |
'\" |
| 2 |
'\" Copyright (c) 1994 The Regents of the University of California. |
| 3 |
'\" Copyright (c) 1994-1996 Sun Microsystems, Inc. |
| 4 |
'\" Copyright (c) 2008 Pat Thoyts |
| 5 |
'\" |
| 6 |
'\" See the file "license.terms" for information on usage and redistribution |
| 7 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 8 |
'\" |
| 9 |
.TH fileevent n 7.5 Tcl "Tcl Built-In Commands" |
| 10 |
.so man.macros |
| 11 |
.BS |
| 12 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 13 |
.SH NAME |
| 14 |
fileevent \- Execute a script when a channel becomes readable or writable |
| 15 |
.SH SYNOPSIS |
| 16 |
\fBfileevent \fIchannelId \fBreadable \fR?\fIscript\fR? |
| 17 |
.sp |
| 18 |
\fBfileevent \fIchannelId \fBwritable \fR?\fIscript\fR? |
| 19 |
.BE |
| 20 |
|
| 21 |
.SH DESCRIPTION |
| 22 |
.PP |
| 23 |
This command is used to create \fIfile event handlers\fR. A file event |
| 24 |
handler is a binding between a channel and a script, such that the script |
| 25 |
is evaluated whenever the channel becomes readable or writable. File event |
| 26 |
handlers are most commonly used to allow data to be received from another |
| 27 |
process on an event-driven basis, so that the receiver can continue to |
| 28 |
interact with the user while waiting for the data to arrive. If an |
| 29 |
application invokes \fBgets\fR or \fBread\fR on a blocking channel when |
| 30 |
there is no input data available, the process will block; until the input |
| 31 |
data arrives, it will not be able to service other events, so it will |
| 32 |
appear to the user to |
| 33 |
.QW "freeze up" . |
| 34 |
With \fBfileevent\fR, the process can |
| 35 |
tell when data is present and only invoke \fBgets\fR or \fBread\fR when |
| 36 |
they will not block. |
| 37 |
.PP |
| 38 |
The \fIchannelId\fR argument to \fBfileevent\fR refers to an open |
| 39 |
channel such as a Tcl standard channel (\fBstdin\fR, \fBstdout\fR, |
| 40 |
or \fBstderr\fR), the return value from an invocation of \fBopen\fR |
| 41 |
or \fBsocket\fR, or the result of a channel creation command provided |
| 42 |
by a Tcl extension. |
| 43 |
.PP |
| 44 |
If the \fIscript\fR argument is specified, then \fBfileevent\fR |
| 45 |
creates a new event handler: \fIscript\fR will be evaluated |
| 46 |
whenever the channel becomes readable or writable (depending on the |
| 47 |
second argument to \fBfileevent\fR). |
| 48 |
In this case \fBfileevent\fR returns an empty string. |
| 49 |
The \fBreadable\fR and \fBwritable\fR event handlers for a file |
| 50 |
are independent, and may be created and deleted separately. |
| 51 |
However, there may be at most one \fBreadable\fR and one \fBwritable\fR |
| 52 |
handler for a file at a given time in a given interpreter. |
| 53 |
If \fBfileevent\fR is called when the specified handler already |
| 54 |
exists in the invoking interpreter, the new script replaces the old one. |
| 55 |
.PP |
| 56 |
If the \fIscript\fR argument is not specified, \fBfileevent\fR |
| 57 |
returns the current script for \fIchannelId\fR, or an empty string |
| 58 |
if there is none. |
| 59 |
If the \fIscript\fR argument is specified as an empty string |
| 60 |
then the event handler is deleted, so that no script will be invoked. |
| 61 |
A file event handler is also deleted automatically whenever |
| 62 |
its channel is closed or its interpreter is deleted. |
| 63 |
.PP |
| 64 |
A channel is considered to be readable if there is unread data |
| 65 |
available on the underlying device. |
| 66 |
A channel is also considered to be readable if there is unread |
| 67 |
data in an input buffer, except in the special case where the |
| 68 |
most recent attempt to read from the channel was a \fBgets\fR |
| 69 |
call that could not find a complete line in the input buffer. |
| 70 |
This feature allows a file to be read a line at a time in nonblocking mode |
| 71 |
using events. |
| 72 |
A channel is also considered to be readable if an end of file or |
| 73 |
error condition is present on the underlying file or device. |
| 74 |
It is important for \fIscript\fR to check for these conditions |
| 75 |
and handle them appropriately; for example, if there is no special |
| 76 |
check for end of file, an infinite loop may occur where \fIscript\fR |
| 77 |
reads no data, returns, and is immediately invoked again. |
| 78 |
.PP |
| 79 |
A channel is considered to be writable if at least one byte of data |
| 80 |
can be written to the underlying file or device without blocking, |
| 81 |
or if an error condition is present on the underlying file or device. |
| 82 |
.PP |
| 83 |
Event-driven I/O works best for channels that have been placed into |
| 84 |
nonblocking mode with the \fBfconfigure\fR command. In blocking mode, |
| 85 |
a \fBputs\fR command may block if you give it more data than the |
| 86 |
underlying file or device can accept, and a \fBgets\fR or \fBread\fR |
| 87 |
command will block if you attempt to read more data than is ready; a |
| 88 |
readable underlying file or device may not even guarantee that a |
| 89 |
blocking [read 1] will succeed (counter-examples being multi-byte |
| 90 |
encodings, compression or encryption transforms ). In all such cases, |
| 91 |
no events will be processed while the commands block. |
| 92 |
.PP |
| 93 |
In nonblocking mode \fBputs\fR, \fBread\fR, and \fBgets\fR never block. |
| 94 |
See the documentation for the individual commands for information |
| 95 |
on how they handle blocking and nonblocking channels. |
| 96 |
.PP |
| 97 |
Testing for the end of file condition should be done after any attempts |
| 98 |
read the channel data. The eof flag is set once an attempt to read the |
| 99 |
end of data has occurred and testing before this read will require an |
| 100 |
additional event to be fired. |
| 101 |
.PP |
| 102 |
The script for a file event is executed at global level (outside the |
| 103 |
context of any Tcl procedure) in the interpreter in which the |
| 104 |
\fBfileevent\fR command was invoked. |
| 105 |
If an error occurs while executing the script then the |
| 106 |
command registered with \fBinterp bgerror\fR is used to report the error. |
| 107 |
In addition, the file event handler is deleted if it ever returns |
| 108 |
an error; this is done in order to prevent infinite loops due to |
| 109 |
buggy handlers. |
| 110 |
.SH EXAMPLE |
| 111 |
.PP |
| 112 |
In this setup \fBGetData\fR will be called with the channel as an |
| 113 |
argument whenever $chan becomes readable. The \fBread\fR call will |
| 114 |
read whatever binary data is currently available without blocking. |
| 115 |
Here the channel has the fileevent removed when an end of file |
| 116 |
occurs to avoid being continually called (see above). Alternatively |
| 117 |
the channel may be closed on this condition. |
| 118 |
.PP |
| 119 |
.CS |
| 120 |
proc GetData {chan} { |
| 121 |
set data [read $chan] |
| 122 |
puts "[string length $data] $data" |
| 123 |
if {[eof $chan]} { |
| 124 |
fileevent $chan readable {} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
fconfigure $chan -blocking 0 -translation binary |
| 129 |
\fBfileevent\fR $chan readable [list GetData $chan] |
| 130 |
.CE |
| 131 |
.PP |
| 132 |
The next example demonstrates use of \fBgets\fR to read line-oriented |
| 133 |
data. |
| 134 |
.PP |
| 135 |
.CS |
| 136 |
proc GetData {chan} { |
| 137 |
if {[gets $chan line] >= 0} { |
| 138 |
puts $line |
| 139 |
} |
| 140 |
if {[eof $chan]} { |
| 141 |
close $chan |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
fconfigure $chan -blocking 0 -buffering line -translation crlf |
| 146 |
\fBfileevent\fR $chan readable [list GetData $chan] |
| 147 |
.CE |
| 148 |
.SH CREDITS |
| 149 |
.PP |
| 150 |
\fBfileevent\fR is based on the \fBaddinput\fR command created |
| 151 |
by Mark Diekhans. |
| 152 |
.SH "SEE ALSO" |
| 153 |
fconfigure(n), gets(n), interp(n), puts(n), read(n), Tcl_StandardChannels(3) |
| 154 |
.SH KEYWORDS |
| 155 |
asynchronous I/O, blocking, channel, event handler, nonblocking, readable, |
| 156 |
script, writable. |