1 |
578 |
markom |
'\"
|
2 |
|
|
'\" Copyright (c) 1994 The Regents of the University of California.
|
3 |
|
|
'\" Copyright (c) 1994-1996 Sun Microsystems, Inc.
|
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: fileevent.n,v 1.1.1.1 2002-01-16 10:25:24 markom Exp $
|
9 |
|
|
'\"
|
10 |
|
|
.so man.macros
|
11 |
|
|
.TH fileevent n 7.5 Tcl "Tcl Built-In Commands"
|
12 |
|
|
.BS
|
13 |
|
|
'\" Note: do not modify the .SH NAME line immediately below!
|
14 |
|
|
.SH NAME
|
15 |
|
|
fileevent \- Execute a script when a channel becomes readable or writable
|
16 |
|
|
.SH SYNOPSIS
|
17 |
|
|
\fBfileevent \fIchannelId \fBreadable \fR?\fIscript\fR?
|
18 |
|
|
.sp
|
19 |
|
|
\fBfileevent \fIchannelId \fBwritable \fR?\fIscript\fR?
|
20 |
|
|
.BE
|
21 |
|
|
|
22 |
|
|
.SH DESCRIPTION
|
23 |
|
|
.PP
|
24 |
|
|
This command is used to create \fIfile event handlers\fR. A file event
|
25 |
|
|
handler is a binding between a channel and a script, such that the script
|
26 |
|
|
is evaluated whenever the channel becomes readable or writable. File event
|
27 |
|
|
handlers are most commonly used to allow data to be received from another
|
28 |
|
|
process on an event-driven basis, so that the receiver can continue to
|
29 |
|
|
interact with the user while waiting for the data to arrive. If an
|
30 |
|
|
application invokes \fBgets\fR or \fBread\fR on a blocking channel when
|
31 |
|
|
there is no input data available, the process will block; until the input
|
32 |
|
|
data arrives, it will not be able to service other events, so it will
|
33 |
|
|
appear to the user to ``freeze up''. With \fBfileevent\fR, the process can
|
34 |
|
|
tell when data is present and only invoke \fBgets\fR or \fBread\fR when
|
35 |
|
|
they won't block.
|
36 |
|
|
.PP
|
37 |
|
|
The \fIchannelId\fR argument to \fBfileevent\fR refers to an open channel,
|
38 |
|
|
such as the return value from a previous \fBopen\fR or \fBsocket\fR
|
39 |
|
|
command.
|
40 |
|
|
If the \fIscript\fR argument is specified, then \fBfileevent\fR
|
41 |
|
|
creates a new event handler: \fIscript\fR will be evaluated
|
42 |
|
|
whenever the channel becomes readable or writable (depending on the
|
43 |
|
|
second argument to \fBfileevent\fR).
|
44 |
|
|
In this case \fBfileevent\fR returns an empty string.
|
45 |
|
|
The \fBreadable\fR and \fBwritable\fR event handlers for a file
|
46 |
|
|
are independent, and may be created and deleted separately.
|
47 |
|
|
However, there may be at most one \fBreadable\fR and one \fBwritable\fR
|
48 |
|
|
handler for a file at a given time in a given interpreter.
|
49 |
|
|
If \fBfileevent\fR is called when the specified handler already
|
50 |
|
|
exists in the invoking interpreter, the new script replaces the old one.
|
51 |
|
|
.PP
|
52 |
|
|
If the \fIscript\fR argument is not specified, \fBfileevent\fR
|
53 |
|
|
returns the current script for \fIchannelId\fR, or an empty string
|
54 |
|
|
if there is none.
|
55 |
|
|
If the \fIscript\fR argument is specified as an empty string
|
56 |
|
|
then the event handler is deleted, so that no script will be invoked.
|
57 |
|
|
A file event handler is also deleted automatically whenever
|
58 |
|
|
its channel is closed or its interpreter is deleted.
|
59 |
|
|
.PP
|
60 |
|
|
A channel is considered to be readable if there is unread data
|
61 |
|
|
available on the underlying device.
|
62 |
|
|
A channel is also considered to be readable if there is unread
|
63 |
|
|
data in an input buffer, except in the special case where the
|
64 |
|
|
most recent attempt to read from the channel was a \fBgets\fR
|
65 |
|
|
call that could not find a complete line in the input buffer.
|
66 |
|
|
This feature allows a file to be read a line at a time in nonblocking mode
|
67 |
|
|
using events.
|
68 |
|
|
A channel is also considered to be readable if an end of file or
|
69 |
|
|
error condition is present on the underlying file or device.
|
70 |
|
|
It is important for \fIscript\fR to check for these conditions
|
71 |
|
|
and handle them appropriately; for example, if there is no special
|
72 |
|
|
check for end of file, an infinite loop may occur where \fIscript\fR
|
73 |
|
|
reads no data, returns, and is immediately invoked again.
|
74 |
|
|
.PP
|
75 |
|
|
A channel is considered to be writable if at least one byte of data
|
76 |
|
|
can be written to the underlying file or device without blocking,
|
77 |
|
|
or if an error condition is present on the underlying file or device.
|
78 |
|
|
.PP
|
79 |
|
|
Event-driven I/O works best for channels that have been
|
80 |
|
|
placed into nonblocking mode with the \fBfconfigure\fR command.
|
81 |
|
|
In blocking mode, a \fBputs\fR command may block if you give it
|
82 |
|
|
more data than the underlying file or device can accept, and a
|
83 |
|
|
\fBgets\fR or \fBread\fR command will block if you attempt to read
|
84 |
|
|
more data than is ready; no events will be processed while the
|
85 |
|
|
commands block.
|
86 |
|
|
In nonblocking mode \fBputs\fR, \fBread\fR, and \fBgets\fR never block.
|
87 |
|
|
See the documentation for the individual commands for information
|
88 |
|
|
on how they handle blocking and nonblocking channels.
|
89 |
|
|
.PP
|
90 |
|
|
The script for a file event is executed at global level (outside the
|
91 |
|
|
context of any Tcl procedure) in the interpreter in which the
|
92 |
|
|
\fBfileevent\fR command was invoked.
|
93 |
|
|
If an error occurs while executing the script then the
|
94 |
|
|
\fBbgerror\fR mechanism is used to report the error.
|
95 |
|
|
In addition, the file event handler is deleted if it ever returns
|
96 |
|
|
an error; this is done in order to prevent infinite loops due to
|
97 |
|
|
buggy handlers.
|
98 |
|
|
|
99 |
|
|
.SH CREDITS
|
100 |
|
|
.PP
|
101 |
|
|
\fBfileevent\fR is based on the \fBaddinput\fR command created
|
102 |
|
|
by Mark Diekhans.
|
103 |
|
|
|
104 |
|
|
.SH "SEE ALSO"
|
105 |
|
|
bgerror, fconfigure, gets, puts, read
|
106 |
|
|
|
107 |
|
|
.SH KEYWORDS
|
108 |
|
|
asynchronous I/O, blocking, channel, event handler, nonblocking, readable,
|
109 |
|
|
script, writable.
|