source: project/wiki/eggref/4/pty @ 23993

Last change on this file since 23993 was 23993, checked in by svnwiki, 10 years ago

Anonymous wiki edit for IP [219.89.162.195]: Fix procedure listing: "(" was used instead of "<", minor typo

File size: 4.3 KB
Line 
1== pty
2
3Easy Pseudo-Terminal Interface
4     
5=== Author
6
7Alex Shinn
8
9=== Documentation
10
11PTY allows you to run sub-processes in pseudo-terminals
12and access their input and output from the controlling
13program as regular Chicken ports.  This is useful for
14a variety of purposes, such as:
15
16* Run a remote session via ssh or rlogin using a user-supplied password.
17* Automate some or all of a terminal game or program (such as nethack or vi).
18* Run fsck, answering "yes" or "no" based on pre-determined criteria.
19* Implement an xterm program.
20
21In general these cannot be done with regular sub-process ports.
22
23==== High-Level Interface
24
25<procedure>(call-with-pty-process-io command proc [name [width [height]]])</procedure>
26
27Call and return the result of {{proc}} which should be a procedure of
28three arguments: the input, output and PID of the sub-process
29{{command}} running in a new PTY. Ensures the sub-process is
30terminated on completion.
31
32{{name}}, {{width}} and {{height}} are optional settings for the new
33PTY.
34
35{{command}} may optionally be a list specifying the arglist to the
36sub-process, or a single string which is split on whitespace.
37
38The ports generated only block the current thread, and {{char-ready?}}
39only returns #t if some input is already available.  The eof-object is
40returned only when the sub-process terminates.
41
42'''NOTE:''' Prior to login of, and after completion of the
43sub-process, anything written to the output port will pass directly
44through to the input port, which is almost certainly not what you
45want.  To avoid premature writes, you should read once first.  To
46avoid writing after the process has completed, don't write after
47you've read a single eof-object, or alternately you can check manually
48with {{process-alive?}}.
49
50<procedure>(with-pty-process-io command proc [name [width [height]]])</procedure>
51
52As above, except current input and output ports are bound to the
53output and input of {{command}} respectively, and {{proc}} only takes
54one argument, the PID.
55
56=== Low-Level Interface
57
58<procedure>(open-pty-process command [name [width [height]]])</procedure>
59<procedure>(open-pty [name [width [height]]])</procedure>
60<procedure>(login-tty fd)</procedure>
61<procedure>(process-alive? pid)</procedure>
62<procedure>(fcntl-ref fd)</procedure>
63<procedure>(fcntl-set! fd arg)</procedure>
64<procedure>(file-select-one fd)</procedure>
65<procedure>(file-read/maybe fd buf len)</procedure>
66<procedure>(open-file-io/non-blocking fd [process-alive? (-> boolean)])</procedure>
67
68=== Example
69
70<enscript highlight="scheme">
71(call-with-pty-process-io "ssh myfirewall"
72  (lambda (in out pid)
73    (peek-char in)
74    (display (gui-get-password) out)
75    (newline out)
76    (read-line in)
77    (display "dhclient 2>/dev/null && echo OK || echo FAIL\n" out)
78    (unless (equal? "OK" (read-line in))
79      (error "couldn't launch dhclient")))
80</enscript>
81
82=== Changelog
83
84* 1.3 setup script fixes (felix)
85* 1.1 MacOS X compile [Kon Lovett]
86* 1.0 Initial release
87
88=== License
89
90  Copyright (c) 2006 Alex Shinn
91  All rights reserved.
92 
93  Redistribution and use in source and binary forms, with or without
94  modification, are permitted provided that the following conditions
95  are met:
96  1. Redistributions of source code must retain the above copyright
97     notice, this list of conditions and the following disclaimer.
98  2. Redistributions in binary form must reproduce the above copyright
99     notice, this list of conditions and the following disclaimer in the
100     documentation and/or other materials provided with the distribution.
101  3. The name of the author may not be used to endorse or promote products
102     derived from this software without specific prior written permission.
103 
104  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
105  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
106  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
107  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
108  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
109  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
110  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
111  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
112  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
113  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
Note: See TracBrowser for help on using the repository browser.