Debian Codegraph @

doc/README

raw
1 README
2
3 lpty
4
5 a library that allows lua to start processes and control them via pty
6
7 Author: Gunnar Zötl , 2010-2013.
8 Released under MIT/X11 license. See file LICENSE for details.
9
10 Introduction
11
12 This is a simple interface to pty functionality, providing the ability
13 to fork a process and run it under pty control. It does not try to
14 mimic the posix API but instead focuses on the function of running and
15 controlling a program. The interface is very bare bones, no additional
16 functionality is provided, especially nothing like expect, which should
17 be a different package.
18
19 This has been developed on Linux and tested on MacOS X. It should
20 compile and run on any platform supporting the Unix 98 interface to
21 pty's. Support for additional Unixen might follow in the future,
22 depending on demand (especially my own ;) ). No support for windows is
23 planned, as ptys are a Unix thing. It might work with cygwin.
24
25 Installing
26
27 This uses only stuff that comes with your system, so you do not need to
28 install any additional libraries to satisfy any dependencies.
29
30 There are 2 options for building and installing this module, the first
31 and most obvious being luarocks. Normally, calling:
32 sudo luarocks install lpty
33
34 or when you have an unpacked source folder:
35 sudo luarocks make
36
37 should do the right thing.
38
39 There is also a Makefile in the distribution, that has been created for
40 use with Linux and MacOS X. It may work for other platforms, or you may
41 have to edit it by hand. Calling:
42 make && make install
43
44 will build the module and install it to some standard location for your
45 version of lua.
46
47 Using
48
49 Load the module with:
50 lpty = require "lpty"
51
52 Note: since version 1.0, lpty no longer registers a global table lpty
53 with lua.
54
55 Constructor
56
57 pty = lpty.new([options])
58 Creates and fully initializes the master side of a pty, and
59 returns an object to interact with the pty. Options for the pty
60 may be passed as key - value pairs in a table. These options
61 are:
62
63 throw_errors
64 If true, errors are thrown, otherwise they are returned as
65 a standard nil, "error message" pair. Default is false.
66
67 no_local_echo
68 If true, stuff sent to the parent side of the pty will not
69 be echoed back to the parent side, otherwise it will be
70 echoed back. Default is false.
71 Beware that this may not always work as expected.
72
73 use_path
74 If true, the pty:startproc() method for the created pty
75 will search for an executable file if the specified
76 filename does not contain a slash (/) character. The
77 search path is the path specified in the parents
78 environment by the PATH variable, no default will be used.
79 If false, the path to the executable must be explicitly
80 specified. Default is true.
81
82 If you do not want to pass any options, you may omit the options
83 table altogether.
84
85 Process handling
86
87 pty:startproc(command, arg1, arg2, ...)
88 Starts a process with the slave side of the pty as its
89 controlling terminal. The child processes stdin, stdout and
90 stderr are set to use the slave side of th pty. The command and
91 its arguments must be separate arguments to the startproc
92 method. If the pty has been created with use_path=true (the
93 default), then this uses the PATH environment variable of the
94 calling process (the one using lpty) to find any executables, so
95 in that case specifying a full path to the command is not always
96 necessary. Returns true if the call to fork() was successful. If
97 anything goes wrong whilst starting the new process, an error is
98 signalled (that is, thrown or returned as two values nil, "error
99 message", depending on the setting of the throw_errors flag.
100
101 If there is already an active process attached to the pty, the
102 new process is not started and the method returns false.
103
104 The pty:startproc() method can not check whether running the
105 command in the child process or basically anything that happens
106 in the child process after the fork is successful. If
107 pty:startproc() returns true, this means that setting up the
108 client side of the tty and fork()ing went well and yielded a
109 running child process. You may want to check wether it is still
110 running when you need it using the pty:hasproc() method,
111 especially if you're dealing with an interactive program. If the
112 program the child was supposed to start is not running when you
113 expect it to, you will have to call the pty:exitstatus() method
114 and maybe even check the contents of the pty for the reason.
115
116 pty:endproc([kill])
117 Terminates the process running with the pty as its controlling
118 terminal. If the optional parameter kill is false or omitted,
119 send the child process a SIGTERM, otherwise send a SIGKILL. It
120 is not an error to call this on a pty object that has no active
121 child processes. You do not need to call it when you have
122 already terminated the child process using more civilized means
123 (like, for example, sending it a quit command it understands).
124 After the child process has been terminated, a new one can be
125 started using the same PTY.
126
127 pty:hasproc()
128 returns true if the pty has an active child process, false if
129 not (none has been created, the child has terminated, ...).
130
131 pty:exitstatus()
132 Determines the reason why a previous child process has
133 terminated. Returns a the kind of termination and the code.
134 Possible combinations are:
135
136 false, nil
137 if the pty has an active child process or never had one,
138
139 'exit', exitcode
140 if the child process has exited normally,
141
142 'sig', signum
143 if the child process was terminated by a signal,
144
145 'unk', 0
146 if the reason for the death of the child process can not
147 be determined any more.
148
149 The pty:exitstatus() method works by having a fixed length
150 ringbuffer, in which the signal handler responsible for reaping
151 child processes places what waitpid() returns. Because of the
152 fixed size, when you create a lot of processes, the ring buffer
153 might wrap before you retrieve the exit status of your process,
154 in which case the 'unk', 0 values will be returned, 'unk'
155 meaning "unknown". If you hit this a lot, you may want to check
156 the exit status earlier, or if that does not help, change the
157 value for EXITSTATUS_BUFSIZ in lpty.c.
158
159 Process Environments
160
161 pty:getenviron()
162 Returns a table containing the environment a child process of
163 the pty will be executed in. If no custom environment has been
164 set using the pty:setenviron() method, this will be the
165 environment the parent process is running in.
166
167 pty:setenviron(env)
168 Sets a custom environment for the child process to be executed
169 within the pty. The argument must be a table containing a
170 complete environment for the child process. If you want to
171 modify the current environment, obtain it with the
172 pty:getenviron() method, modify it and pass it into this method
173 as a new environment.
174
175 Calling this function with nil as its argument resets the
176 environment to the environment the parent process is running in.
177
178 Communication
179
180 pty:readok([timeout])
181 returns true if the pty has data available to be read, false
182 otherwise. If a timeout value is specified, the call will wait
183 at most that amount of seconds for data to become available to
184 read. No value is equivalent to a timeout of 0 seconds.
185
186 pty:read([timeout])
187 reads data from the pty. This is a blocking read, so you can
188 specify an optional timeout in seconds after which the read
189 should return to the caller whether there was data or not. If
190 the timeout is omitted, the read will block until there is
191 something to read. Returns the data read, or nil if the request
192 timed out or was interrupted (failed with EINTR or ECHILD). If
193 an error occurred during the read attempt, it is signalled to
194 lua.
195
196 Note: unless you specified no_local_echo=true when creating the
197 pty, this reads data output from the client process as well as
198 data sent to the client process from the controlling skript.
199
200 The pty:read() method reads its data into a buffer. The size of
201 this buffer determines how much data can be returned from an
202 individual read. It is set by a #define in the module source and
203 by default is set to 4096. Considering the C-typical terminating
204 zero, this will leave a maximum of 4095 bytes to be read in one
205 go. In order to fetch all available data, just loop while
206 pty:readok() returns true and concatenate the results.
207
208 pty:sendok([timeout])
209 returns true if the pty can accept data sent to it, false
210 otherwise. If a timeout value is specified, the call will wait
211 at most that many seconds for the pty to accept data, No value
212 is equivalent to a timeout of 0 seconds.
213
214 pty:send(data [, timeout])
215 sends data to the pty. This will then be available to the child
216 process on its stdin. As sending data may block, there is an
217 optional argument for a timeout in seconds on the send
218 operation. If the timeout argument is omitted, pty:send() will
219 block until the pty can accept the data. Returns the number of
220 bytes written, or nil if the send timed out or was interrupted
221 (failed with EINTR or ECHILD). If an error occurred during the
222 attempt to write it is signalled to lua.
223
224 The pty:send() method may not send all data you gave it in one
225 go. For small amounts of data this is quite improbable, but in
226 industrial strength applications you should probably check the
227 return value of the pty:send() method against the length of the
228 data you intended to send.
229
230 pty:flush([mode])
231 flushes data from the pty. mode is an optional string argument.
232 If mode is "i", flush data received but not read. If mode is
233 "o", flush data written but not transmitted. If mode is anything
234 else or omitted, flush both sides.
235
236 Note that the mode "o" is hardly ever useful, because when
237 send() returns, the data it claims to have sent will already be
238 transmitted to the client side.
239
240 Information
241
242 pty:ttyname()
243 return the tty name of the slave side of the pty.
244
245 pty:getfd()
246 return the file handle of the master side of the pty. This is
247 intended for use with luasocket's select() or luaposix's poll()
248 and similar functions.
249
250 Additional Details
251
252 The local echo thing
253
254 As written in the description of the no_local_echo flag, this may not
255 always work as expected. Programs are free to set their terminals to
256 whatever settings they like, and indeed some programs do, or even
257 completely roll their own local echo policy. The most notable piece of
258 code to do this is Gnu readline, though it seems that this does not
259 happen for all versions of it. On Mac OS X, it certainly does, whereas
260 on my Ubuntu Box it does not. So if you write something using lpty that
261 is supposed to be used on different unixen, you better not rely on
262 no_local_echo to do what you think it should.
263
264 Communication and process handling
265
266 If you have a pty without a running child process, you can still write
267 to it. Anything you write to the pty will be available as input to a
268 child process that you start later.
269
270 You can also read from a pty without a child process, but only what you
271 have written to it - unless you specified no_local_echo=true for pty
272 creation, in which case there is nothing to read from it. This means
273 that if you (or the last active child process) have nothing written to
274 it, a read with timeout will always time out, and a read without
275 timeout will block. Also, if you read stuff back from the master side
276 of a pty without a child, that you have written there, it will not be
277 removed from a subsequent child process's input.
278
279 Starting a process takes some time, so after you have called
280 pty:startproc() it may take a while for data to become available from
281 the child process. So don't be surprised if a pty:readok() fails
282 directly after a pty:startproc().
283
284 Similar things are true for calls to pty:hasproc(). If you call
285 pty:hasproc() directly after pty:startproc(), chances are it will
286 return true whatever the outcome of starting the child process was,
287 because it just may not have had enough time to crash. Calling
288 pty:hasproc() will yield a much saner result if you do it after, for
289 example, a pty:read() timed out when you didn't expect it to, or
290 similar occurrences.
291
292 Garbage disposal
293
294 When a lpty object is garbage collected, its master side pty handle is
295 closed, and if it has a child process, that is terminated with a
296 SIGKILL. So it is reasonably safe to let pty's with active processes
297 become garbage, it just isn't very friendly towards the child
298 processes.
299
300 Example
301
302 A very simple example for lpty follows. In a real application you
303 should probably do some checking for errors and stuff output by your
304 child process. This uses bc to compute the sum of 2 numbers:
305 lpty = require "lpty" p = lpty.new() p:startproc("lua") p:read() --
306 skip startup message p:send("=111+234\n") r = p:read() print("Result is
307 "..r) p:send("os.exit()\n") -- terminate lua all friendly-like
308 print("Done.")
309
310 Also, see the samples folder in the distribution archive.
311
312 References
313
314 Read up on ptys on your local friendly linux system:
315 /www.kernel.org/doc/man-pages/online/pages/man7/pty.7.html>man 7 pty
316 and friends. This link is linux specific, but as lpty is implemented
317 using Unix 98 pseudo ttys, which Linux is using, the information is
318 probably valid whereever lpty runs.