1mNAME0m
       fileevent  -  Execute  a  script  when  a  channel  becomes readable or
       writable

1mSYNOPSIS0m
       1mfileevent 4m22mchannelId24m 1mreadable 22m?4mscript24m?

       1mfileevent 4m22mchannelId24m 1mwritable 22m?4mscript24m?


1mDESCRIPTION0m
       This command is used to create 4mfile24m 4mevent24m 4mhandlers24m.  A file event  han-
       dler  is a binding between a channel and a script, such that the script
       is evaluated whenever the channel becomes readable or  writable.   File
       event handlers are most commonly used to allow data to be received from
       another process on an event-driven basis, so that the receiver can con-
       tinue  to  interact with the user while waiting for the data to arrive.
       If an application invokes 1mgets 22mor 1mread 22mon a blocking channel when there
       is  no  input  data  available, the process will block; until the input
       data arrives, it will not be able to service other events, so  it  will
       appear  to  the user to ``freeze up''.  With 1mfileevent22m, the process can
       tell when data is present and only invoke 1mgets 22mor 1mread 22mwhen they  won't
       block.

       The  4mchannelId24m argument to 1mfileevent 22mrefers to an open channel, such as
       the return value from a previous 1mopen 22mor 1msocket 22mcommand.  If the 4mscript0m
       argument  is  specified,  then  1mfileevent  22mcreates a new event handler:
       4mscript24m will be evaluated  whenever  the  channel  becomes  readable  or
       writable (depending on the second argument to 1mfileevent22m).  In this case
       1mfileevent 22mreturns an empty string.  The  1mreadable  22mand  1mwritable  22mevent
       handlers  for  a  file  are independent, and may be created and deleted
       separately.  However, there  may  be  at  most  one  1mreadable  22mand  one
       1mwritable 22mhandler for a file at a given time in a given interpreter.  If
       1mfileevent 22mis called when the specified handler already  exists  in  the
       invoking interpreter, the new script replaces the old one.

       If  the 4mscript24m argument is not specified, 1mfileevent 22mreturns the current
       script for 4mchannelId24m, or an empty string if  there  is  none.   If  the
       4mscript24m  argument is specified as an empty string then the event handler
       is deleted, so that no script will be invoked.  A file event handler is
       also deleted automatically whenever its channel is closed or its inter-
       preter is deleted.

       A channel is considered to be readable if there is unread  data  avail-
       able  on  the  underlying  device.   A channel is also considered to be
       readable if there is unread data in an input buffer, except in the spe-
       cial  case where the most recent attempt to read from the channel was a
       1mgets 22mcall that could not find a complete  line  in  the  input  buffer.
       This  feature  allows a file to be read a line at a time in nonblocking
       mode using events.  A channel is also considered to be readable  if  an
       end  of  file  or  error condition is present on the underlying file or
       device.  It is important for 4mscript24m to check for these  conditions  and
       handle  them  appropriately;  for example, if there is no special check
       for end of file, an infinite loop may occur where 4mscript24m reads no data,
       returns, and is immediately invoked again.

       A channel is considered to be writable if at least one byte of data can
       be written to the underlying file or device without blocking, or if  an
       error condition is present on the underlying file or device.

       Event-driven  I/O  works  best  for channels that have been placed into
       nonblocking mode with the 1mfconfigure 22mcommand.  In blocking mode, a 1mputs0m
       command  may block if you give it more data than the underlying file or
       device can accept, and a 1mgets 22mor 1mread 22mcommand will block if you attempt
       to read more data than is ready;  no events will be processed while the
       commands block.  In nonblocking mode 1mputs22m, 1mread22m, and 1mgets 22mnever  block.
       See  the  documentation  for the individual commands for information on
       how they handle blocking and nonblocking channels.

       The script for a file event is executed at global  level  (outside  the
       context of any Tcl procedure) in the interpreter in which the 1mfileevent0m
       command was invoked.  If an error occurs  while  executing  the  script
       then  the  1mbgerror 22mmechanism is used to report the error.  In addition,
       the file event handler is deleted if it ever returns an error;  this is
       done in order to prevent infinite loops due to buggy handlers.


1mCREDITS0m
       1mfileevent 22mis based on the 1maddinput 22mcommand created by Mark Diekhans.


1mSEE ALSO0m
       bgerror(n), fconfigure(n), gets(n), puts(n), read(n)


1mKEYWORDS0m
       asynchronous  I/O, blocking, channel, event handler, nonblocking, read-
