Tizen 2.1 base
[platform/upstream/hplip.git] / base / pexpect.py
1 """Pexpect is a Python module for spawning child applications and controlling
2 them automatically. Pexpect can be used for automating interactive applications
3 such as ssh, ftp, passwd, telnet, etc. It can be used to a automate setup
4 scripts for duplicating software package installations on different servers. It
5 can be used for automated software testing. Pexpect is in the spirit of Don
6 Libes' Expect, but Pexpect is pure Python. Other Expect-like modules for Python
7 require TCL and Expect or require C extensions to be compiled. Pexpect does not
8 use C, Expect, or TCL extensions. It should work on any platform that supports
9 the standard Python pty module. The Pexpect interface focuses on ease of use so
10 that simple tasks are easy.
11
12 There are two main interfaces to Pexpect -- the function, run() and the class,
13 spawn. You can call the run() function to execute a command and return the
14 output. This is a handy replacement for os.system().
15
16 For example:
17     pexpect.run('ls -la')
18
19 The more powerful interface is the spawn class. You can use this to spawn an
20 external child command and then interact with the child by sending lines and
21 expecting responses.
22
23 For example:
24     child = pexpect.spawn('scp foo myname@host.example.com:.')
25     child.expect ('Password:')
26     child.sendline (mypassword)
27
28 This works even for commands that ask for passwords or other input outside of
29 the normal stdio streams.
30
31 Credits:
32 Noah Spurrier, Richard Holden, Marco Molteni, Kimberley Burchett, Robert Stone,
33 Hartmut Goebel, Chad Schroeder, Erick Tryzelaar, Dave Kirby, Ids vander Molen,
34 George Todd, Noel Taylor, Nicolas D. Cesar, Alexander Gattin,
35 Geoffrey Marshall, Francisco Lourenco, Glen Mabey, Karthik Gurusamy,
36 Fernando Perez 
37 (Let me know if I forgot anyone.)
38
39 Free, open source, and all that good stuff.
40
41 Permission is hereby granted, free of charge, to any person obtaining a copy of
42 this software and associated documentation files (the "Software"), to deal in
43 the Software without restriction, including without limitation the rights to
44 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
45 of the Software, and to permit persons to whom the Software is furnished to do
46 so, subject to the following conditions:
47
48 The above copyright notice and this permission notice shall be included in all
49 copies or substantial portions of the Software.
50
51 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
52 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
53 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
54 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
55 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
56 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
57 SOFTWARE.
58
59 Pexpect Copyright (c) 2006 Noah Spurrier
60 http://pexpect.sourceforge.net/
61
62 $Revision: 1.2 $
63 $Date: 2007/01/11 20:51:46 $
64 """
65 try:
66     import os
67     import sys
68     import time
69     import select
70     import string
71     import re
72     import struct
73     import resource
74     import types
75     import pty
76     import tty
77     import termios
78     import fcntl
79     import errno
80     import traceback
81     import signal
82 except ImportError, e:
83     raise ImportError (str(e) + """
84 A critical module was not found. Probably this operating system does not support it.
85 Pexpect is intended for UNIX-like operating systems.""")
86
87 __version__ = '2.1'
88 __revision__ = '$Revision: 1.2 $'
89 __all__ = ['ExceptionPexpect', 'EOF', 'TIMEOUT', 'spawn', 'run', 'which', 'split_command_line',
90     '__version__', '__revision__']
91
92 # Exception classes used by this module.
93 class ExceptionPexpect(Exception):
94     """Base class for all exceptions raised by this module.
95     """
96     def __init__(self, value):
97         self.value = value
98     def __str__(self):
99         return str(self.value)
100     def get_trace(self):
101         """This returns an abbreviated stack trace with lines that only concern the caller.
102         In other words, the stack trace inside the Pexpect module is not included.
103         """
104         tblist = traceback.extract_tb(sys.exc_info()[2])
105         tblist = filter(self.__filter_not_pexpect, tblist)
106         tblist = traceback.format_list(tblist)
107         return ''.join(tblist)
108     def __filter_not_pexpect(self, trace_list_item):
109         if trace_list_item[0].find('pexpect.py') == -1:
110             return True
111         else:
112             return False
113 class EOF(ExceptionPexpect):
114     """Raised when EOF is read from a child.
115     """
116 class TIMEOUT(ExceptionPexpect):
117     """Raised when a read time exceeds the timeout.
118     """
119
120 def run (command, timeout=-1, withexitstatus=False, events=None, extra_args=None, logfile=None):
121     """This function runs the given command; waits for it to finish;
122     then returns all output as a string. STDERR is included in output.
123     If the full path to the command is not given then the path is searched.
124
125     Note that lines are terminated by CR/LF (\\r\\n) combination
126     even on UNIX-like systems because this is the standard for pseudo ttys.
127     If you set withexitstatus to true, then run will return a tuple of
128     (command_output, exitstatus). If withexitstatus is false then this
129     returns just command_output.
130
131     The run() function can often be used instead of creating a spawn instance.
132     For example, the following code uses spawn:
133         from pexpect import *
134         child = spawn('scp foo myname@host.example.com:.')
135         child.expect ('(?i)password')
136         child.sendline (mypassword)
137     The previous code can be replace with the following, which you may
138     or may not find simpler:
139         from pexpect import *
140         run ('scp foo myname@host.example.com:.', events={'(?i)password': mypassword})
141
142     Examples:
143     Start the apache daemon on the local machine:
144         from pexpect import *
145         run ("/usr/local/apache/bin/apachectl start")
146     Check in a file using SVN:
147         from pexpect import *
148         run ("svn ci -m 'automatic commit' my_file.py")
149     Run a command and capture exit status:
150         from pexpect import *
151         (command_output, exitstatus) = run ('ls -l /bin', withexitstatus=1)
152
153     Tricky Examples:   
154     The following will run SSH and execute 'ls -l' on the remote machine.
155     The password 'secret' will be sent if the '(?i)password' pattern is ever seen.
156         run ("ssh username@machine.example.com 'ls -l'", events={'(?i)password':'secret\n'})
157
158     This will start mencoder to rip a video from DVD. This will also display
159     progress ticks every 5 seconds as it runs.
160         from pexpect import *
161         def print_ticks(d):
162             print d['event_count'],
163         run ("mencoder dvd://1 -o video.avi -oac copy -ovc copy", events={TIMEOUT:print_ticks}, timeout=5)
164
165     The 'events' argument should be a dictionary of patterns and responses.
166     Whenever one of the patterns is seen in the command out
167     run() will send the associated response string. Note that you should
168     put newlines in your string if Enter is necessary.
169     The responses may also contain callback functions.
170     Any callback is function that takes a dictionary as an argument.
171     The dictionary contains all the locals from the run() function, so
172     you can access the child spawn object or any other variable defined
173     in run() (event_count, child, and extra_args are the most useful).
174     A callback may return True to stop the current run process otherwise
175     run() continues until the next event.
176     A callback may also return a string which will be sent to the child.
177     'extra_args' is not used by directly run(). It provides a way to pass data to
178     a callback function through run() through the locals dictionary passed to a callback.
179     """
180     if timeout == -1:
181         child = spawn(command, maxread=2000, logfile=logfile)
182     else:
183         child = spawn(command, timeout=timeout, maxread=2000, logfile=logfile)
184     if events is not None:
185         patterns = events.keys()
186         responses = events.values()
187     else:
188         patterns=None # We assume that EOF or TIMEOUT will save us.
189         responses=None
190     child_result_list = []
191     event_count = 0
192     while 1:
193         try:
194             index = child.expect (patterns)
195             if type(child.after) is types.StringType:
196                 child_result_list.append(child.before + child.after)
197             else: # child.after may have been a TIMEOUT or EOF, so don't cat those.
198                 child_result_list.append(child.before)
199             if type(responses[index]) is types.StringType:
200                 child.send(responses[index])
201             elif type(responses[index]) is types.FunctionType:
202                 callback_result = responses[index](locals())
203                 sys.stdout.flush()
204                 if type(callback_result) is types.StringType:
205                     child.send(callback_result)
206                 elif callback_result:
207                     break
208             else:
209                 raise TypeError ('The callback must be a string or function type.')
210             event_count = event_count + 1
211         except TIMEOUT, e:
212             child_result_list.append(child.before)
213             break
214         except EOF, e:
215             child_result_list.append(child.before)
216             break
217     child_result = ''.join(child_result_list)
218     if withexitstatus:
219         child.close()
220         return (child_result, child.exitstatus)
221     else:
222         return child_result
223
224 class spawn (object):
225     """This is the main class interface for Pexpect.
226     Use this class to start and control child applications.
227     """
228
229     def __init__(self, command, args=[], timeout=30, maxread=2000, searchwindowsize=None, logfile=None, env=None):
230         """This is the constructor. The command parameter may be a string
231         that includes a command and any arguments to the command. For example:
232             p = pexpect.spawn ('/usr/bin/ftp')
233             p = pexpect.spawn ('/usr/bin/ssh user@example.com')
234             p = pexpect.spawn ('ls -latr /tmp')
235         You may also construct it with a list of arguments like so:
236             p = pexpect.spawn ('/usr/bin/ftp', [])
237             p = pexpect.spawn ('/usr/bin/ssh', ['user@example.com'])
238             p = pexpect.spawn ('ls', ['-latr', '/tmp'])
239         After this the child application will be created and
240         will be ready to talk to. For normal use, see expect() and 
241         send() and sendline().
242
243         The maxread attribute sets the read buffer size.
244         This is maximum number of bytes that Pexpect will try to read
245         from a TTY at one time.
246         Seeting the maxread size to 1 will turn off buffering.
247         Setting the maxread value higher may help performance in cases
248         where large amounts of output are read back from the child.
249         This feature is useful in conjunction with searchwindowsize.
250
251         The searchwindowsize attribute sets the how far back in
252         the incomming seach buffer Pexpect will search for pattern matches.
253         Every time Pexpect reads some data from the child it will append the data to
254         the incomming buffer. The default is to search from the beginning of the
255         imcomming buffer each time new data is read from the child.
256         But this is very inefficient if you are running a command that
257         generates a large amount of data where you want to match
258         The searchwindowsize does not effect the size of the incomming data buffer.
259         You will still have access to the full buffer after expect() returns.
260
261         The logfile member turns on or off logging.
262         All input and output will be copied to the given file object.
263         Set logfile to None to stop logging. This is the default.
264         Set logfile to sys.stdout to echo everything to standard output.
265         The logfile is flushed after each write.
266         Example 1:
267             child = pexpect.spawn('some_command')
268             fout = file('mylog.txt','w')
269             child.logfile = fout
270         Example 2:
271             child = pexpect.spawn('some_command')
272             child.logfile = sys.stdout
273
274         The delaybeforesend helps overcome a weird behavior that many users were experiencing.
275         The typical problem was that a user would expect() a "Password:" prompt and
276         then immediately call sendline() to send the password. The user would then
277         see that their password was echoed back to them. Passwords don't
278         normally echo. The problem is caused by the fact that most applications
279         print out the "Password" prompt and then turn off stdin echo, but if you
280         send your password before the application turned off echo, then you get
281         your password echoed. Normally this wouldn't be a problem when interacting
282         with a human at a real heyboard. If you introduce a slight delay just before 
283         writing then this seems to clear up the problem. This was such a common problem 
284         for many users that I decided that the default pexpect behavior
285         should be to sleep just before writing to the child application.
286         1/10th of a second (100 ms) seems to be enough to clear up the problem.
287         You can set delaybeforesend to 0 to return to the old behavior.
288
289         Note that spawn is clever about finding commands on your path.
290         It uses the same logic that "which" uses to find executables.
291
292         If you wish to get the exit status of the child you must call
293         the close() method. The exit or signal status of the child will be
294         stored in self.exitstatus or self.signalstatus.
295         If the child exited normally then exitstatus will store the exit return code and
296         signalstatus will be None.
297         If the child was terminated abnormally with a signal then signalstatus will store
298         the signal value and exitstatus will be None.
299         If you need more detail you can also read the self.status member which stores
300         the status returned by os.waitpid. You can interpret this using
301         os.WIFEXITED/os.WEXITSTATUS or os.WIFSIGNALED/os.TERMSIG.
302         """
303         self.STDIN_FILENO = pty.STDIN_FILENO
304         self.STDOUT_FILENO = pty.STDOUT_FILENO
305         self.STDERR_FILENO = pty.STDERR_FILENO
306         self.stdin = sys.stdin
307         self.stdout = sys.stdout
308         self.stderr = sys.stderr
309
310         self.patterns = None
311         self.ignorecase = False
312         self.before = None
313         self.after = None
314         self.match = None
315         self.match_index = None
316         self.terminated = True
317         self.exitstatus = None
318         self.signalstatus = None
319         self.status = None # status returned by os.waitpid 
320         self.flag_eof = False
321         self.pid = None
322         self.child_fd = -1 # initially closed
323         self.timeout = timeout
324         self.delimiter = EOF
325         self.logfile = logfile    
326         self.maxread = maxread # Max bytes to read at one time into buffer.
327         self.buffer = '' # This is the read buffer. See maxread.
328         self.searchwindowsize = searchwindowsize # Anything before searchwindowsize point is preserved, but not searched.
329         self.delaybeforesend = 0.1 # Sets sleep time used just before sending data to child.
330         self.delayafterclose = 0.1 # Sets delay in close() method to allow kernel time to update process status.
331         self.delayafterterminate = 0.1 # Sets delay in terminate() method to allow kernel time to update process status.
332         self.softspace = False # File-like object.
333         self.name = '<' + repr(self) + '>' # File-like object.
334         self.encoding = None # File-like object.
335         self.closed = True # File-like object.
336         self.env = env
337         self.__irix_hack = sys.platform.lower().find('irix') >= 0 # This flags if we are running on irix
338         self.use_native_pty_fork = not (sys.platform.lower().find('solaris') >= 0) # Solaris uses internal __fork_pty(). All other use pty.fork().
339
340         # allow dummy instances for subclasses that may not use command or args.
341         if command is None:
342             self.command = None
343             self.args = None
344             self.name = '<pexpect factory incomplete>'
345             return
346
347         # If command is an int type then it may represent a file descriptor.
348         if type(command) == type(0):
349             raise ExceptionPexpect ('Command is an int type. If this is a file descriptor then maybe you want to use fdpexpect.fdspawn which takes an existing file descriptor instead of a command string.')
350
351         if type (args) != type([]):
352             raise TypeError ('The argument, args, must be a list.')
353
354         if args == []:
355             self.args = split_command_line(command)
356             self.command = self.args[0]
357         else:
358             self.args = args[:] # work with a copy
359             self.args.insert (0, command)
360             self.command = command
361
362         command_with_path = which(self.command)
363         if command_with_path is None:
364             raise ExceptionPexpect ('The command was not found or was not executable: %s.' % self.command)
365         self.command = command_with_path
366         self.args[0] = self.command
367
368         self.name = '<' + ' '.join (self.args) + '>'
369         self.__spawn()
370
371     def __del__(self):
372         """This makes sure that no system resources are left open.
373         Python only garbage collects Python objects. OS file descriptors
374         are not Python objects, so they must be handled explicitly.
375         If the child file descriptor was opened outside of this class
376         (passed to the constructor) then this does not close it.
377         """
378         if not self.closed:
379             self.close()
380
381     def __str__(self):
382         """This returns the current state of the pexpect object as a string.
383         """
384         s = []
385         s.append(repr(self))
386         s.append('version: ' + __version__ + ' (' + __revision__ + ')')
387         s.append('command: ' + str(self.command))
388         s.append('args: ' + str(self.args))
389         if self.patterns is None:
390             s.append('patterns: None')
391         else:
392             s.append('patterns:')
393             for p in self.patterns:
394                 if type(p) is type(re.compile('')):
395                     s.append('    ' + str(p.pattern))
396                 else:
397                     s.append('    ' + str(p))
398         s.append('buffer (last 100 chars): ' + str(self.buffer)[-100:])
399         s.append('before (last 100 chars): ' + str(self.before)[-100:])
400         s.append('after: ' + str(self.after))
401         s.append('match: ' + str(self.match))
402         s.append('match_index: ' + str(self.match_index))
403         s.append('exitstatus: ' + str(self.exitstatus))
404         s.append('flag_eof: ' + str(self.flag_eof))
405         s.append('pid: ' + str(self.pid))
406         s.append('child_fd: ' + str(self.child_fd))
407         s.append('closed: ' + str(self.closed))
408         s.append('timeout: ' + str(self.timeout))
409         s.append('delimiter: ' + str(self.delimiter))
410         s.append('logfile: ' + str(self.logfile))
411         s.append('maxread: ' + str(self.maxread))
412         s.append('ignorecase: ' + str(self.ignorecase))
413         s.append('searchwindowsize: ' + str(self.searchwindowsize))
414         s.append('delaybeforesend: ' + str(self.delaybeforesend))
415         s.append('delayafterclose: ' + str(self.delayafterclose))
416         s.append('delayafterterminate: ' + str(self.delayafterterminate))
417         return '\n'.join(s)
418
419     def __spawn(self):
420         """This starts the given command in a child process.
421         This does all the fork/exec type of stuff for a pty.
422         This is called by __init__. 
423         """
424         # The pid and child_fd of this object get set by this method.
425         # Note that it is difficult for this method to fail.
426         # You cannot detect if the child process cannot start.
427         # So the only way you can tell if the child process started
428         # or not is to try to read from the file descriptor. If you get
429         # EOF immediately then it means that the child is already dead.
430         # That may not necessarily be bad because you may haved spawned a child
431         # that performs some task; creates no stdout output; and then dies.
432
433         assert self.pid is None, 'The pid member should be None.'
434         assert self.command is not None, 'The command member should not be None.'
435
436         if self.use_native_pty_fork:
437             try:
438                 self.pid, self.child_fd = pty.fork()
439             except OSError, e:
440                 raise ExceptionPexpect('Error! pty.fork() failed: ' + str(e))
441         else: # Use internal __fork_pty
442             self.pid, self.child_fd = self.__fork_pty() 
443
444         if self.pid == 0: # Child
445             try: 
446                 self.child_fd = sys.stdout.fileno() # used by setwinsize()
447                 self.setwinsize(24, 80)
448             except: 
449                 # Some platforms do not like setwinsize (Cygwin).
450                 # This will cause problem when running applications that
451                 # are very picky about window size.
452                 # This is a serious limitation, but not a show stopper.
453                 pass
454             # Do not allow child to inherit open file descriptors from parent.
455             max_fd = resource.getrlimit(resource.RLIMIT_NOFILE)[0]
456             for i in range (3, max_fd):
457                 try:
458                     os.close (i)
459                 except OSError:
460                     pass
461
462             # I don't know why this works, but ignoring SIGHUP fixes a
463             # problem when trying to start a Java daemon with sudo
464             # (specifically, Tomcat).
465             signal.signal(signal.SIGHUP, signal.SIG_IGN)
466
467             if self.env is None:
468                 os.execv(self.command, self.args)
469             else:
470                 os.execvpe(self.command, self.args, self.env)
471
472         # Parent
473         self.terminated = False
474         self.closed = False
475
476     def __fork_pty(self):
477         """This implements a substitute for the forkpty system call.
478         This should be more portable than the pty.fork() function.
479         Specifically, this should work on Solaris.
480
481         Modified 10.06.05 by Geoff Marshall:
482             Implemented __fork_pty() method to resolve the issue with Python's 
483             pty.fork() not supporting Solaris, particularly ssh.
484         Based on patch to posixmodule.c authored by Noah Spurrier:
485             http://mail.python.org/pipermail/python-dev/2003-May/035281.html
486         """
487         parent_fd, child_fd = os.openpty()
488         if parent_fd < 0 or child_fd < 0:
489             raise ExceptionPexpect, "Error! Could not open pty with os.openpty()."
490
491         pid = os.fork()
492         if pid < 0:
493             raise ExceptionPexpect, "Error! Failed os.fork()."
494         elif pid == 0:
495             # Child.
496             os.close(parent_fd)
497             self.__pty_make_controlling_tty(child_fd)
498
499             os.dup2(child_fd, 0)
500             os.dup2(child_fd, 1)
501             os.dup2(child_fd, 2)
502
503             if child_fd > 2:
504                 os.close(child_fd)
505         else:
506             # Parent.
507             os.close(child_fd)
508
509         return pid, parent_fd
510
511     def __pty_make_controlling_tty(self, tty_fd):
512         """This makes the pseudo-terminal the controlling tty.
513         This should be more portable than the pty.fork() function.
514         Specifically, this should work on Solaris.
515         """
516         child_name = os.ttyname(tty_fd)
517
518         # Disconnect from controlling tty if still connected.
519         fd = os.open("/dev/tty", os.O_RDWR | os.O_NOCTTY);
520         if fd >= 0:
521             os.close(fd)
522
523         os.setsid()
524
525         # Verify we are disconnected from controlling tty
526         try:
527             fd = os.open("/dev/tty", os.O_RDWR | os.O_NOCTTY);
528             if fd >= 0:
529                 os.close(fd)
530                 raise ExceptionPexpect, "Error! We are not disconnected from a controlling tty."
531         except:
532             # Good! We are disconnected from a controlling tty.
533             pass
534
535         # Verify we can open child pty.
536         fd = os.open(child_name, os.O_RDWR);
537         if fd < 0:
538             raise ExceptionPexpect, "Error! Could not open child pty, " + child_name
539         else:
540             os.close(fd)
541
542         # Verify we now have a controlling tty.
543         fd = os.open("/dev/tty", os.O_WRONLY)
544         if fd < 0:
545             raise ExceptionPexpect, "Error! Could not open controlling tty, /dev/tty"
546         else:
547             os.close(fd)
548
549     def fileno (self):   # File-like object.
550         """This returns the file descriptor of the pty for the child.
551         """
552         return self.child_fd
553
554     def close (self, force=True):   # File-like object.
555         """This closes the connection with the child application.
556         Note that calling close() more than once is valid.
557         This emulates standard Python behavior with files.
558         Set force to True if you want to make sure that the child is terminated
559         (SIGKILL is sent if the child ignores SIGHUP and SIGINT).
560         """
561         if not self.closed:
562             self.flush()
563             os.close (self.child_fd)
564             self.child_fd = -1
565             self.closed = True
566             time.sleep(self.delayafterclose) # Give kernel time to update process status.
567             if self.isalive():
568                 if not self.terminate(force):
569                     raise ExceptionPexpect ('close() could not terminate the child using terminate()')
570
571     def flush (self):   # File-like object.
572         """This does nothing. It is here to support the interface for a File-like object.
573         """
574         pass
575
576     def isatty (self):   # File-like object.
577         """This returns True if the file descriptor is open and connected to a tty(-like) device, else False.
578         """
579         return os.isatty(self.child_fd)
580
581     def setecho (self, state):
582         """This sets the terminal echo mode on or off.
583         Note that anything the child sent before the echo will be lost, so
584         you should be sure that your input buffer is empty before you setecho.
585         For example, the following will work as expected.
586             p = pexpect.spawn('cat')
587             p.sendline ('1234') # We will see this twice (once from tty echo and again from cat).
588             p.expect (['1234'])
589             p.expect (['1234'])
590             p.setecho(False) # Turn off tty echo
591             p.sendline ('abcd') # We will set this only once (echoed by cat).
592             p.sendline ('wxyz') # We will set this only once (echoed by cat)
593             p.expect (['abcd'])
594             p.expect (['wxyz'])
595         The following WILL NOT WORK because the lines sent before the setecho
596         will be lost:
597             p = pexpect.spawn('cat')
598             p.sendline ('1234') # We will see this twice (once from tty echo and again from cat).
599             p.setecho(False) # Turn off tty echo
600             p.sendline ('abcd') # We will set this only once (echoed by cat).
601             p.sendline ('wxyz') # We will set this only once (echoed by cat)
602             p.expect (['1234'])
603             p.expect (['1234'])
604             p.expect (['abcd'])
605             p.expect (['wxyz'])
606         """
607         self.child_fd
608         new = termios.tcgetattr(self.child_fd)
609         if state:
610             new[3] = new[3] | termios.ECHO
611         else:
612             new[3] = new[3] & ~termios.ECHO
613         # I tried TCSADRAIN and TCSAFLUSH, but these were inconsistent
614         # and blocked on some platforms. TCSADRAIN is probably ideal if it worked.
615         termios.tcsetattr(self.child_fd, termios.TCSANOW, new)
616
617     def read_nonblocking (self, size = 1, timeout = -1):
618         """This reads at most size characters from the child application.
619         It includes a timeout. If the read does not complete within the
620         timeout period then a TIMEOUT exception is raised.
621         If the end of file is read then an EOF exception will be raised.
622         If a log file was set using setlog() then all data will
623         also be written to the log file.
624
625         If timeout==None then the read may block indefinitely.
626         If timeout==-1 then the self.timeout value is used.
627         If timeout==0 then the child is polled and 
628             if there was no data immediately ready then this will raise a TIMEOUT exception.
629
630         The "timeout" refers only to the amount of time to read at least one character.
631         This is not effected by the 'size' parameter, so if you call
632         read_nonblocking(size=100, timeout=30) and only one character is
633         available right away then one character will be returned immediately. 
634         It will not wait for 30 seconds for another 99 characters to come in.
635
636         This is a wrapper around os.read().
637         It uses select.select() to implement a timeout. 
638         """
639         if self.closed:
640             raise ValueError ('I/O operation on closed file in read_nonblocking().')
641
642         if timeout == -1:
643             timeout = self.timeout
644
645         # Note that some systems such as Solaris do not give an EOF when
646         # the child dies. In fact, you can still try to read
647         # from the child_fd -- it will block forever or until TIMEOUT.
648         # For this case, I test isalive() before doing any reading.
649         # If isalive() is false, then I pretend that this is the same as EOF.
650         if not self.isalive():
651             r,w,e = self.__select([self.child_fd], [], [], 0) # timeout of 0 means "poll"
652             if not r:
653                 self.flag_eof = True
654                 raise EOF ('End Of File (EOF) in read_nonblocking(). Braindead platform.')
655         elif self.__irix_hack:
656             # This is a hack for Irix. It seems that Irix requires a long delay before checking isalive.
657             # This adds a 2 second delay, but only when the child is terminated.
658             r, w, e = self.__select([self.child_fd], [], [], 2)
659             if not r and not self.isalive():
660                 self.flag_eof = True
661                 raise EOF ('End Of File (EOF) in read_nonblocking(). Pokey platform.')
662
663         r,w,e = self.__select([self.child_fd], [], [], timeout)
664
665         if not r:
666             if not self.isalive():
667                 # Some platforms, such as Irix, will claim that their processes are alive;
668                 # then timeout on the select; and then finally admit that they are not alive.
669                 self.flag_eof = True
670                 raise EOF ('End of File (EOF) in read_nonblocking(). Very pokey platform.')
671             else:
672                 raise TIMEOUT ('Timeout exceeded in read_nonblocking().')
673
674         if self.child_fd in r:
675             try:
676                 s = os.read(self.child_fd, size)
677             except OSError, e: # Linux does this
678                 self.flag_eof = True
679                 raise EOF ('End Of File (EOF) in read_nonblocking(). Exception style platform.')
680             if s == '': # BSD style
681                 self.flag_eof = True
682                 raise EOF ('End Of File (EOF) in read_nonblocking(). Empty string style platform.')
683
684             if self.logfile is not None:
685                 self.logfile.write (s)
686                 self.logfile.flush()
687
688             return s
689
690         raise ExceptionPexpect ('Reached an unexpected state in read_nonblocking().')
691
692     def read (self, size = -1):   # File-like object.
693         """This reads at most "size" bytes from the file 
694         (less if the read hits EOF before obtaining size bytes). 
695         If the size argument is negative or omitted, 
696         read all data until EOF is reached. 
697         The bytes are returned as a string object. 
698         An empty string is returned when EOF is encountered immediately.
699         """
700         if size == 0:
701             return ''
702         if size < 0:
703             self.expect (self.delimiter) # delimiter default is EOF
704             return self.before
705
706         # I could have done this more directly by not using expect(), but
707         # I deliberately decided to couple read() to expect() so that
708         # I would catch any bugs early and ensure consistant behavior.
709         # It's a little less efficient, but there is less for me to
710         # worry about if I have to later modify read() or expect().
711         # Note, it's OK if size==-1 in the regex. That just means it
712         # will never match anything in which case we stop only on EOF.
713         cre = re.compile('.{%d}' % size, re.DOTALL) 
714         index = self.expect ([cre, self.delimiter]) # delimiter default is EOF
715         if index == 0:
716             return self.after ### self.before should be ''. Should I assert this?
717         return self.before
718
719     def readline (self, size = -1):    # File-like object.
720         """This reads and returns one entire line. A trailing newline is kept in
721         the string, but may be absent when a file ends with an incomplete line. 
722         Note: This readline() looks for a \\r\\n pair even on UNIX because
723         this is what the pseudo tty device returns. So contrary to what you
724         may expect you will receive the newline as \\r\\n.
725         An empty string is returned when EOF is hit immediately.
726         Currently, the size agument is mostly ignored, so this behavior is not
727         standard for a file-like object. If size is 0 then an empty string
728         is returned.
729         """
730         if size == 0:
731             return ''
732         index = self.expect (['\r\n', self.delimiter]) # delimiter default is EOF
733         if index == 0:
734             return self.before + '\r\n'
735         else:
736             return self.before
737
738     def __iter__ (self):    # File-like object.
739         """This is to support iterators over a file-like object.
740         """
741         return self
742
743     def next (self):    # File-like object.
744         """This is to support iterators over a file-like object.
745         """
746         result = self.readline()
747         if result == "":
748             raise StopIteration
749         return result
750
751     def readlines (self, sizehint = -1):    # File-like object.
752         """This reads until EOF using readline() and returns a list containing 
753         the lines thus read. The optional "sizehint" argument is ignored.
754         """        
755         lines = []
756         while True:
757             line = self.readline()
758             if not line:
759                 break
760             lines.append(line)
761         return lines
762
763     def write(self, str):   # File-like object.
764         """This is similar to send() except that there is no return value.
765         """
766         self.send (str)
767
768     def writelines (self, sequence):   # File-like object.
769         """This calls write() for each element in the sequence.
770         The sequence can be any iterable object producing strings, 
771         typically a list of strings. This does not add line separators
772         There is no return value.
773         """
774         for str in sequence:
775             self.write (str)
776
777     def send(self, str):
778         """This sends a string to the child process.
779         This returns the number of bytes written.
780         If a log file was set then the data is also written to the log.
781         """
782         time.sleep(self.delaybeforesend)
783         if self.logfile is not None:
784             self.logfile.write (str)
785             self.logfile.flush()
786         c = os.write(self.child_fd, str)
787         return c
788
789     def sendline(self, str=''):
790         """This is like send(), but it adds a line feed (os.linesep).
791         This returns the number of bytes written.
792         """
793         n = self.send(str)
794         n = n + self.send (os.linesep)
795         return n
796
797     def sendeof(self):
798         """This sends an EOF to the child.
799         This sends a character which causes the pending parent output
800         buffer to be sent to the waiting child program without
801         waiting for end-of-line. If it is the first character of the
802         line, the read() in the user program returns 0, which
803         signifies end-of-file. This means to work as expected 
804         a sendeof() has to be called at the begining of a line. 
805         This method does not send a newline. It is the responsibility
806         of the caller to ensure the eof is sent at the beginning of a line.
807         """
808         fd = sys.stdin.fileno()
809         old = termios.tcgetattr(fd) # remember current state
810         new = termios.tcgetattr(fd)
811         new[3] = new[3] | termios.ICANON # ICANON must be set to recognize EOF
812         try: # use try/finally to ensure state gets restored
813             termios.tcsetattr(fd, termios.TCSADRAIN, new)
814             if 'CEOF' in dir(termios):
815                 os.write (self.child_fd, '%c' % termios.CEOF)
816             else:
817                 os.write (self.child_fd, '%c' % 4) # Silly platform does not define CEOF so assume CTRL-D
818         finally: # restore state
819             termios.tcsetattr(fd, termios.TCSADRAIN, old)
820
821     def eof (self):
822         """This returns True if the EOF exception was ever raised.
823         """
824         return self.flag_eof
825
826     def terminate(self, force=False):
827         """This forces a child process to terminate.
828         It starts nicely with SIGHUP and SIGINT. If "force" is True then
829         moves onto SIGKILL.
830         This returns True if the child was terminated.
831         This returns False if the child could not be terminated.
832         """
833         if not self.isalive():
834             return True
835         self.kill(signal.SIGHUP)
836         time.sleep(self.delayafterterminate)
837         if not self.isalive():
838             return True
839         self.kill(signal.SIGCONT)
840         time.sleep(self.delayafterterminate)
841         if not self.isalive():
842             return True
843         self.kill(signal.SIGINT)
844         time.sleep(self.delayafterterminate)
845         if not self.isalive():
846             return True
847         if force:
848             self.kill(signal.SIGKILL)
849             time.sleep(self.delayafterterminate)
850             if not self.isalive():
851                 return True
852             else:
853                 return False
854         return False
855         #raise ExceptionPexpect ('terminate() could not terminate child process. Try terminate(force=True)?')
856
857     def wait(self):
858         """This waits until the child exits. This is a blocking call.
859             This will not read any data from the child, so this will block forever
860             if the child has unread output and has terminated. In other words, the child
861             may have printed output then called exit(); but, technically, the child is
862             still alive until its output is read.
863         """
864         if self.isalive():
865             pid, status = os.waitpid(self.pid, 0)
866         else:
867             raise ExceptionPexpect ('Cannot wait for dead child process.')
868         self.exitstatus = os.WEXITSTATUS(status)
869         if os.WIFEXITED (status):
870             self.status = status
871             self.exitstatus = os.WEXITSTATUS(status)
872             self.signalstatus = None
873             self.terminated = True
874         elif os.WIFSIGNALED (status):
875             self.status = status
876             self.exitstatus = None
877             self.signalstatus = os.WTERMSIG(status)
878             self.terminated = True
879         elif os.WIFSTOPPED (status):
880             raise ExceptionPexpect ('Wait was called for a child process that is stopped. This is not supported. Is some other process attempting job control with our child pid?')
881         return self.exitstatus
882
883     def isalive(self):
884         """This tests if the child process is running or not.
885         This is non-blocking. If the child was terminated then this
886         will read the exitstatus or signalstatus of the child.
887         This returns True if the child process appears to be running or False if not.
888         It can take literally SECONDS for Solaris to return the right status.
889         """
890         if self.terminated:
891             return False
892
893         if self.flag_eof:
894             # This is for Linux, which requires the blocking form of waitpid to get
895             # status of a defunct process. This is super-lame. The flag_eof would have
896             # been set in read_nonblocking(), so this should be safe.
897             waitpid_options = 0
898         else:
899             waitpid_options = os.WNOHANG
900
901         try:
902             pid, status = os.waitpid(self.pid, waitpid_options)
903         except OSError, e: # No child processes
904             if e[0] == errno.ECHILD:
905                 raise ExceptionPexpect ('isalive() encountered condition where "terminated" is 0, but there was no child process. Did someone else call waitpid() on our process?')
906             else:
907                 raise e
908
909         # I have to do this twice for Solaris. I can't even believe that I figured this out...
910         # If waitpid() returns 0 it means that no child process wishes to
911         # report, and the value of status is undefined.
912         if pid == 0:
913             try:
914                 pid, status = os.waitpid(self.pid, waitpid_options) ### os.WNOHANG) # Solaris!
915             except OSError, e: # This should never happen...
916                 if e[0] == errno.ECHILD:
917                     raise ExceptionPexpect ('isalive() encountered condition that should never happen. There was no child process. Did someone else call waitpid() on our process?')
918                 else:
919                     raise e
920
921             # If pid is still 0 after two calls to waitpid() then
922             # the process really is alive. This seems to work on all platforms, except
923             # for Irix which seems to require a blocking call on waitpid or select, so I let read_nonblocking
924             # take care of this situation (unfortunately, this requires waiting through the timeout).
925             if pid == 0:
926                 return True
927
928         if pid == 0:
929             return True
930
931         if os.WIFEXITED (status):
932             self.status = status
933             self.exitstatus = os.WEXITSTATUS(status)
934             self.signalstatus = None
935             self.terminated = True
936         elif os.WIFSIGNALED (status):
937             self.status = status
938             self.exitstatus = None
939             self.signalstatus = os.WTERMSIG(status)
940             self.terminated = True
941         elif os.WIFSTOPPED (status):
942             raise ExceptionPexpect ('isalive() encountered condition where child process is stopped. This is not supported. Is some other process attempting job control with our child pid?')
943         return False
944
945     def kill(self, sig):
946         """This sends the given signal to the child application.
947         In keeping with UNIX tradition it has a misleading name.
948         It does not necessarily kill the child unless
949         you send the right signal.
950         """
951         # Same as os.kill, but the pid is given for you.
952         if self.isalive():
953             os.kill(self.pid, sig)
954
955     def compile_pattern_list(self, patterns):
956         """This compiles a pattern-string or a list of pattern-strings.
957         Patterns must be a StringType, EOF, TIMEOUT, SRE_Pattern, or 
958         a list of those. Patterns may also be None which results in
959         an empty list.
960
961         This is used by expect() when calling expect_list().
962         Thus expect() is nothing more than::
963              cpl = self.compile_pattern_list(pl)
964              return self.expect_list(clp, timeout)
965
966         If you are using expect() within a loop it may be more
967         efficient to compile the patterns first and then call expect_list().
968         This avoid calls in a loop to compile_pattern_list():
969              cpl = self.compile_pattern_list(my_pattern)
970              while some_condition:
971                 ...
972                 i = self.expect_list(clp, timeout)
973                 ...
974         """
975         if patterns is None:
976             return []
977         if type(patterns) is not types.ListType:
978             patterns = [patterns]
979
980         compile_flags = re.DOTALL # Allow dot to match \n
981         if self.ignorecase:
982             compile_flags = compile_flags | re.IGNORECASE
983         compiled_pattern_list = []
984         for p in patterns:
985             if type(p) is types.StringType:
986                 compiled_pattern_list.append(re.compile(p, compile_flags))
987             elif p is EOF:
988                 compiled_pattern_list.append(EOF)
989             elif p is TIMEOUT:
990                 compiled_pattern_list.append(TIMEOUT)
991             elif type(p) is type(re.compile('')):
992                 compiled_pattern_list.append(p)
993             else:
994                 raise TypeError ('Argument must be one of StringType, EOF, TIMEOUT, SRE_Pattern, or a list of those type. %s' % str(type(p)))
995
996         return compiled_pattern_list
997
998     def expect(self, pattern, timeout = -1, searchwindowsize=None):
999
1000         """This seeks through the stream until a pattern is matched.
1001         The pattern is overloaded and may take several types including a list.
1002         The pattern can be a StringType, EOF, a compiled re, or a list of
1003         those types. Strings will be compiled to re types. This returns the
1004         index into the pattern list. If the pattern was not a list this
1005         returns index 0 on a successful match. This may raise exceptions for
1006         EOF or TIMEOUT. To avoid the EOF or TIMEOUT exceptions add
1007         EOF or TIMEOUT to the pattern list.
1008
1009         After a match is found the instance attributes
1010         'before', 'after' and 'match' will be set.
1011         You can see all the data read before the match in 'before'.
1012         You can see the data that was matched in 'after'.
1013         The re.MatchObject used in the re match will be in 'match'.
1014         If an error occured then 'before' will be set to all the
1015         data read so far and 'after' and 'match' will be None.
1016
1017         If timeout is -1 then timeout will be set to the self.timeout value.
1018
1019         Note: A list entry may be EOF or TIMEOUT instead of a string.
1020         This will catch these exceptions and return the index
1021         of the list entry instead of raising the exception.
1022         The attribute 'after' will be set to the exception type.
1023         The attribute 'match' will be None.
1024         This allows you to write code like this:
1025                 index = p.expect (['good', 'bad', pexpect.EOF, pexpect.TIMEOUT])
1026                 if index == 0:
1027                     do_something()
1028                 elif index == 1:
1029                     do_something_else()
1030                 elif index == 2:
1031                     do_some_other_thing()
1032                 elif index == 3:
1033                     do_something_completely_different()
1034         instead of code like this:
1035                 try:
1036                     index = p.expect (['good', 'bad'])
1037                     if index == 0:
1038                         do_something()
1039                     elif index == 1:
1040                         do_something_else()
1041                 except EOF:
1042                     do_some_other_thing()
1043                 except TIMEOUT:
1044                     do_something_completely_different()
1045         These two forms are equivalent. It all depends on what you want.
1046         You can also just expect the EOF if you are waiting for all output
1047         of a child to finish. For example:
1048                 p = pexpect.spawn('/bin/ls')
1049                 p.expect (pexpect.EOF)
1050                 print p.before
1051
1052         If you are trying to optimize for speed then see expect_list().
1053         """
1054         compiled_pattern_list = self.compile_pattern_list(pattern)
1055         return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)
1056
1057     def expect_list(self, pattern_list, timeout = -1, searchwindowsize = -1):
1058         """This takes a list of compiled regular expressions and returns 
1059         the index into the pattern_list that matched the child output.
1060         The list may also contain EOF or TIMEOUT (which are not
1061         compiled regular expressions). This method is similar to
1062         the expect() method except that expect_list() does not
1063         recompile the pattern list on every call.
1064         This may help if you are trying to optimize for speed, otherwise
1065         just use the expect() method.  This is called by expect().
1066         If timeout==-1 then the self.timeout value is used.
1067         If searchwindowsize==-1 then the self.searchwindowsize value is used.
1068         """
1069
1070         self.patterns = pattern_list
1071
1072         if timeout == -1:
1073             timeout = self.timeout
1074         if timeout is not None:
1075             end_time = time.time() + timeout 
1076         if searchwindowsize == -1:
1077             searchwindowsize = self.searchwindowsize
1078
1079         try:
1080             incoming = self.buffer
1081             while True: # Keep reading until exception or return.
1082                 # Sequence through the list of patterns looking for a match.
1083                 first_match = -1
1084                 for cre in pattern_list:
1085                     if cre is EOF or cre is TIMEOUT: 
1086                         continue # The patterns for PexpectExceptions are handled differently.
1087                     if searchwindowsize is None: # search everything
1088                         match = cre.search(incoming)
1089                     else:
1090                         startpos = max(0, len(incoming) - searchwindowsize)
1091                         match = cre.search(incoming, startpos)
1092                     if match is None:
1093                         continue
1094                     if first_match > match.start() or first_match == -1:
1095                         first_match = match.start()
1096                         self.match = match
1097                         self.match_index = pattern_list.index(cre)
1098                 if first_match > -1:
1099                     self.buffer = incoming[self.match.end() : ]
1100                     self.before = incoming[ : self.match.start()]
1101                     self.after = incoming[self.match.start() : self.match.end()]
1102                     return self.match_index
1103                 # No match at this point
1104                 if timeout < 0 and timeout is not None:
1105                     raise TIMEOUT ('Timeout exceeded in expect_list().')
1106                 # Still have time left, so read more data
1107                 c = self.read_nonblocking (self.maxread, timeout)
1108                 time.sleep (0.0001)
1109                 incoming = incoming + c
1110                 if timeout is not None:
1111                     timeout = end_time - time.time()
1112         except EOF, e:
1113             self.buffer = ''
1114             self.before = incoming
1115             self.after = EOF
1116             if EOF in pattern_list:
1117                 self.match = EOF
1118                 self.match_index = pattern_list.index(EOF)
1119                 return self.match_index
1120             else:
1121                 self.match = None
1122                 self.match_index = None
1123                 raise EOF (str(e) + '\n' + str(self))
1124         except TIMEOUT, e:
1125             self.before = incoming
1126             self.after = TIMEOUT
1127             if TIMEOUT in pattern_list:
1128                 self.match = TIMEOUT
1129                 self.match_index = pattern_list.index(TIMEOUT)
1130                 return self.match_index
1131             else:
1132                 self.match = None
1133                 self.match_index = None
1134                 raise TIMEOUT (str(e) + '\n' + str(self))
1135         except Exception:
1136             self.before = incoming
1137             self.after = None
1138             self.match = None
1139             self.match_index = None
1140             raise
1141
1142     def getwinsize(self):
1143         """This returns the terminal window size of the child tty.
1144         The return value is a tuple of (rows, cols).
1145         """
1146         if 'TIOCGWINSZ' in dir(termios):
1147             TIOCGWINSZ = termios.TIOCGWINSZ
1148         else:
1149             TIOCGWINSZ = 1074295912L # assume if not defined
1150         s = struct.pack('HHHH', 0, 0, 0, 0)
1151         x = fcntl.ioctl(self.fileno(), TIOCGWINSZ, s)
1152         return struct.unpack('HHHH', x)[0:2]
1153
1154     def setwinsize(self, r, c):
1155         """This sets the terminal window size of the child tty.
1156         This will cause a SIGWINCH signal to be sent to the child.
1157         This does not change the physical window size.
1158         It changes the size reported to TTY-aware applications like
1159         vi or curses -- applications that respond to the SIGWINCH signal.
1160         """
1161         # Check for buggy platforms. Some Python versions on some platforms
1162         # (notably OSF1 Alpha and RedHat 7.1) truncate the value for
1163         # termios.TIOCSWINSZ. It is not clear why this happens.
1164         # These platforms don't seem to handle the signed int very well;
1165         # yet other platforms like OpenBSD have a large negative value for
1166         # TIOCSWINSZ and they don't have a truncate problem.
1167         # Newer versions of Linux have totally different values for TIOCSWINSZ.
1168         # Note that this fix is a hack.
1169         if 'TIOCSWINSZ' in dir(termios):
1170             TIOCSWINSZ = termios.TIOCSWINSZ
1171         else:
1172             TIOCSWINSZ = -2146929561
1173         if TIOCSWINSZ == 2148037735L: # L is not required in Python >= 2.2.
1174             TIOCSWINSZ = -2146929561 # Same bits, but with sign.
1175         # Note, assume ws_xpixel and ws_ypixel are zero.
1176         s = struct.pack('HHHH', r, c, 0, 0)
1177         fcntl.ioctl(self.fileno(), TIOCSWINSZ, s)
1178
1179     def interact(self, escape_character = chr(29), input_filter = None, output_filter = None):
1180         """This gives control of the child process to the interactive user
1181         (the human at the keyboard).
1182         Keystrokes are sent to the child process, and the stdout and stderr
1183         output of the child process is printed.
1184         This simply echos the child stdout and child stderr to the real
1185         stdout and it echos the real stdin to the child stdin.
1186         When the user types the escape_character this method will stop.
1187         The default for escape_character is ^]. This should not be confused
1188         with ASCII 27 -- the ESC character. ASCII 29 was chosen
1189         for historical merit because this is the character used
1190         by 'telnet' as the escape character. The escape_character will
1191         not be sent to the child process.
1192
1193         You may pass in optional input and output filter functions.
1194         These functions should take a string and return a string.
1195         The output_filter will be passed all the output from the child process.
1196         The input_filter will be passed all the keyboard input from the user.
1197         The input_filter is run BEFORE the check for the escape_character.
1198
1199         Note that if you change the window size of the parent
1200         the SIGWINCH signal will not be passed through to the child.
1201         If you want the child window size to change when the parent's
1202         window size changes then do something like the following example:
1203             import pexpect, struct, fcntl, termios, signal, sys
1204             def sigwinch_passthrough (sig, data):
1205                 s = struct.pack("HHHH", 0, 0, 0, 0)
1206                 a = struct.unpack('hhhh', fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ , s))
1207                 global p
1208                 p.setwinsize(a[0],a[1])
1209             p = pexpect.spawn('/bin/bash') # Note this is global and used in sigwinch_passthrough.
1210             signal.signal(signal.SIGWINCH, sigwinch_passthrough)
1211             p.interact()
1212         """
1213         # Flush the buffer.
1214         self.stdout.write (self.buffer)
1215         self.stdout.flush()
1216         self.buffer = ''
1217         mode = tty.tcgetattr(self.STDIN_FILENO)
1218         tty.setraw(self.STDIN_FILENO)
1219         try:
1220             self.__interact_copy(escape_character, input_filter, output_filter)
1221         finally:
1222             tty.tcsetattr(self.STDIN_FILENO, tty.TCSAFLUSH, mode)
1223
1224     def __interact_writen(self, fd, data):
1225         """This is used by the interact() method.
1226         """
1227         while data != '' and self.isalive():
1228             n = os.write(fd, data)
1229             data = data[n:]
1230     def __interact_read(self, fd):
1231         """This is used by the interact() method.
1232         """
1233         return os.read(fd, 1000)
1234     def __interact_copy(self, escape_character = None, input_filter = None, output_filter = None):
1235         """This is used by the interact() method.
1236         """
1237         while self.isalive():
1238             r,w,e = self.__select([self.child_fd, self.STDIN_FILENO], [], [])
1239             if self.child_fd in r:
1240                 data = self.__interact_read(self.child_fd)
1241                 if output_filter: data = output_filter(data)
1242                 if self.logfile is not None:
1243                     self.logfile.write (data)
1244                     self.logfile.flush()
1245                 os.write(self.STDOUT_FILENO, data)
1246             if self.STDIN_FILENO in r:
1247                 data = self.__interact_read(self.STDIN_FILENO)
1248                 if input_filter: data = input_filter(data)
1249                 i = data.rfind(escape_character)
1250                 if i != -1:
1251                     data = data[:i]
1252                     self.__interact_writen(self.child_fd, data)
1253                     break
1254                 self.__interact_writen(self.child_fd, data)
1255     def __select (self, iwtd, owtd, ewtd, timeout=None):
1256         """This is a wrapper around select.select() that ignores signals.
1257         If select.select raises a select.error exception and errno is an EINTR error then
1258         it is ignored. Mainly this is used to ignore sigwinch (terminal resize).
1259         """
1260         # if select() is interrupted by a signal (errno==EINTR) then
1261         # we loop back and enter the select() again.
1262         if timeout is not None:
1263             end_time = time.time() + timeout 
1264         while True:
1265             try:
1266                 return select.select (iwtd, owtd, ewtd, timeout)
1267             except select.error, e:
1268                 if e[0] == errno.EINTR:
1269                     # if we loop back we have to subtract the amount of time we already waited.
1270                     if timeout is not None:
1271                         timeout = end_time - time.time()
1272                         if timeout < 0:
1273                             return ([],[],[])
1274                 else: # something else caused the select.error, so this really is an exception
1275                     raise
1276
1277 ##############################################################################
1278 # The following methods are no longer supported or allowed..                
1279     def setmaxread (self, maxread):
1280         """This method is no longer supported or allowed.
1281         I don't like getters and setters without a good reason.
1282         """
1283         raise ExceptionPexpect ('This method is no longer supported or allowed. Just assign a value to the maxread member variable.')
1284     def expect_exact (self, pattern_list, timeout = -1):
1285         """This method is no longer supported or allowed.
1286         It was too hard to maintain and keep it up to date with expect_list.
1287         Few people used this method. Most people favored reliability over speed.
1288         The implementation is left in comments in case anyone needs to hack this
1289         feature back into their copy.
1290         If someone wants to diff this with expect_list and make them work
1291         nearly the same then I will consider adding this make in.
1292         """
1293         raise ExceptionPexpect ('This method is no longer supported or allowed.')
1294     def setlog (self, fileobject):
1295         """This method is no longer supported or allowed.
1296         """
1297         raise ExceptionPexpect ('This method is no longer supported or allowed. Just assign a value to the logfile member variable.')
1298
1299 ##############################################################################
1300 # End of spawn class
1301 ##############################################################################
1302
1303 def which (filename):
1304     """This takes a given filename; tries to find it in the environment path; 
1305     then checks if it is executable.
1306     This returns the full path to the filename if found and executable.
1307     Otherwise this returns None.
1308     """
1309     # Special case where filename already contains a path.
1310     if os.path.dirname(filename) != '':
1311         if os.access (filename, os.X_OK):
1312             return filename
1313
1314     if not os.environ.has_key('PATH') or os.environ['PATH'] == '':
1315         p = os.defpath
1316     else:
1317         p = os.environ['PATH']
1318
1319     # Oddly enough this was the one line that made Pexpect
1320     # incompatible with Python 1.5.2.
1321     #pathlist = p.split (os.pathsep) 
1322     pathlist = string.split (p, os.pathsep)
1323
1324     for path in pathlist:
1325         f = os.path.join(path, filename)
1326         if os.access(f, os.X_OK):
1327             return f
1328     return None
1329
1330 def split_command_line(command_line):
1331     """This splits a command line into a list of arguments.
1332     It splits arguments on spaces, but handles
1333     embedded quotes, doublequotes, and escaped characters.
1334     It's impossible to do this with a regular expression, so
1335     I wrote a little state machine to parse the command line.
1336     """
1337     arg_list = []
1338     arg = ''
1339
1340     # Constants to name the states we can be in.
1341     state_basic = 0
1342     state_esc = 1
1343     state_singlequote = 2
1344     state_doublequote = 3
1345     state_whitespace = 4 # The state of consuming whitespace between commands.
1346     state = state_basic
1347
1348     for c in command_line:
1349         if state == state_basic or state == state_whitespace:
1350             if c == '\\': # Escape the next character
1351                 state = state_esc
1352             elif c == r"'": # Handle single quote
1353                 state = state_singlequote
1354             elif c == r'"': # Handle double quote
1355                 state = state_doublequote
1356             elif c.isspace():
1357                 # Add arg to arg_list if we aren't in the middle of whitespace.
1358                 if state == state_whitespace:
1359                     None # Do nothing.
1360                 else:
1361                     arg_list.append(arg)
1362                     arg = ''
1363                     state = state_whitespace
1364             else:
1365                 arg = arg + c
1366                 state = state_basic
1367         elif state == state_esc:
1368             arg = arg + c
1369             state = state_basic
1370         elif state == state_singlequote:
1371             if c == r"'":
1372                 state = state_basic
1373             else:
1374                 arg = arg + c
1375         elif state == state_doublequote:
1376             if c == r'"':
1377                 state = state_basic
1378             else:
1379                 arg = arg + c
1380
1381     if arg != '':
1382         arg_list.append(arg)
1383     return arg_list
1384