Description

Easy Pseudo-Terminal Interface

Author

Alex Shinn

Version

Usage

(require-extension pty)

Download

pty.egg

Documentation

PTY allows you to run sub-processes in pseudo-terminals and access their input and output from the controlling program as regular Chicken ports. This is useful for a variety of purposes, such as:

In general these cannot be done with regular sub-process ports.

High-Level Interface

procedure: (call-with-pty-process-io command proc [name [width [height]]])

Call and return the result of proc which should be a procedure of three arguments: the input, output and PID of the sub-process command running in a new PTY. Ensures the sub-process is terminated on completion. name, width and height are optional settings for the new PTY. command may optionally be a list specifying the arglist to the sub-process, or a single string which is split on whitespace.

The ports generated only block the current thread, and char-ready? only returns #t if some input is already available. The eof-object is returned only when the sub-process terminates.

NOTE: Prior to login of, and after completion of the sub-process, anything written to the output port will pass directly through to the input port, which is almost certainly not what you want. To avoid premature writes, you should read once first. To avoid writing after the process has completed, don't write after you've read a single eof-object, or alternately you can check manually with process-alive?.

procedure: (with-pty-process-io command proc [name [width [height]]])

As above, except current input and output ports are bound to the output and input of command respectively, and proc only takes one argument, the PID.

Low-Level Interface

procedure: (open-pty-process command [name [width [height]]])
procedure: (open-pty [name [width [height]]])
procedure: (login-tty fd)
procedure: (process-alive? pid)
procedure: (fcntl-ref fd)
procedure: (fcntl-set! fd arg)
procedure: (file-select-one fd)
procedure: (file-read/maybe fd buf len)
procedure: (open-file-io/non-blocking fd [process-alive? (-> boolean)])

Example


(call-with-pty-process-io "ssh myfirewall"
  (lambda (in out pid)
    (peek-char in)
    (display (gui-get-password) out)
    (newline out)
    (read-line in)
    (display "dhclient 2>/dev/null && echo OK || echo FAIL\n" out)
    (unless (equal? "OK" (read-line in))
      (error "couldn't launch dhclient")))

License

Copyright (c) 2006 Alex Shinn
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.