dp_filehandler - to handle file descriptor conditions
dp_isready - Check if a socket has data pending
dp_update - Process pending events and/or when-idle handlers
dp_after - Execute a command after a certain amount of time has elapsed
dp_whenidle - Execute a command when the system is idle
dp_waitvariable - Wait for a variable to be modified

dp_filehandler sockId ?mode command?
dp_isready sockId
dp_update ?idletasks?
dp_after ms ?arg1 arg2 arg3 ...?
dp_whenidle command ?arg1 arg2 arg3 ...?
dp_waitvariable variable

The dp_isready command checks whether sockId is readable or writeable using a select(2) system call. It returns a list of up two boolean values (i.e., 1 or 0). The first value specifies whether sockId is readable, and the second value specifies wheth- er sockId is writeable. For example, the tcl expression
if [lindex [dp_isready file3] 1] {MyOutput file3}
calls MyOutput if file3 is writeable.

The dp_update command is equivalent to the Tk "update" command, except that it operates even if a connection to the X server is not present. See the Tk documentation on "update" for an expla- nation of it's usage.

The dp_after command is equivalent to the Tk "after" command, ex- cept that it operates even if a connection to the X server is not present. See the Tk documentation on "after" for an explanation of it's usage.

The dp_whenidle command arranges for the specified Tcl/Tk command to be evaluated whenever the system is about to go to sleep wait- ing for an event to occur.

The dp_waitvariable command is equivalent to the Tk "tkwait vari- able" command, except that it operates even if a connection to the X server is not present. See the Tk documentation on "tkwait" for an explanation of it's usage.

The dp_filehander command allows one to specify a Tcl/Tk command which will be evaluated whenever the file descriptor represented by sockId is readable, writable, and/or has an exceptional condi- tion pending. Command will be invoked with two arguments append- ed: a single character indicating the mode ('r', 'w', or 'e'), and the sockId.

A sockId is an identifier which represents a file descriptor. It is identical to the identifier manipulated by the Tcl-DP (Tcl Distributed Programming) network connection management commands (dp_connect, dp_shutdown, etc.). SockId's are also manipulated by the read, eof, gets, puts, and close file management commands of Tcl.

The mode argument indicates the situations when the command will be evaluated. It may be any combination of the following values:

If the mode and command arguments are not specified, then any previously specified command for the given sockId is deleted. Specifying a new command using the dp_filehandler command will also delete any previously specified command for a sockId.

The following file handlers create a telnet like interface to the sendmail process (the smtp port) running on the local host. The command "say" sends the string passed to it to sendmail. The re- ply is automatically printed on stdout by the get_remote pro- cedure.

proc get_remote {mode fid} {
    # Exit if remote connection closed on us.
    if {[gets $fid input] == -1} {
        puts stdout "Connection closed by foreign host."
        exit
    }
    puts stdout $input
}

proc say {args} {
    global remote
    puts $remote $args;
}

set remote [lindex [dp_connect localhost smtp] 0]
dp_filehandler $remote r get_remote

Pekka Nikander, Telecom Finland (Pekka.Nikander@ajk.tele.fi)
Extended by Tim MacKenzie (tym@dibbler.cs.monash.edu.au)
Further enhancements by Brian Smith (bsmith@cs.berkeley.edu)
and Steve Yen (syen@cs.berkeley.edu)