1 """Pseudo terminal utilities."""
3 # Bugs: No signal handling. Doesn't set slave termios and window size.
4 # Only tested on Linux.
5 # See: W. Richard Stevens. 1992. Advanced Programming in the
6 # UNIX Environment. Chapter 19.
7 # Author: Steen Lumholt -- with additions by Guido.
9 from select
import select
13 __all__
= ["openpty","fork","spawn"]
22 """openpty() -> (master_fd, slave_fd)
23 Open a pty master/slave pair, using os.openpty() if possible."""
27 except (AttributeError, OSError):
29 master_fd
, slave_name
= _open_terminal()
30 slave_fd
= slave_open(slave_name
)
31 return master_fd
, slave_fd
34 """master_open() -> (master_fd, slave_name)
35 Open a pty master and return the fd, and the filename of the slave end.
36 Deprecated, use openpty() instead."""
39 master_fd
, slave_fd
= os
.openpty()
40 except (AttributeError, OSError):
43 slave_name
= os
.ttyname(slave_fd
)
45 return master_fd
, slave_name
47 return _open_terminal()
50 """Open pty master and return (master_fd, tty_name).
51 SGI and generic BSD version, for when openpty() fails."""
58 tty_name
, master_fd
= sgi
._getpty
(os
.O_RDWR
, 0o666, 0)
59 except IOError as msg
:
61 return master_fd
, tty_name
62 for x
in 'pqrstuvwxyzPQRST':
63 for y
in '0123456789abcdef':
64 pty_name
= '/dev/pty' + x
+ y
66 fd
= os
.open(pty_name
, os
.O_RDWR
)
69 return (fd
, '/dev/tty' + x
+ y
)
70 raise os
.error('out of pty devices')
72 def slave_open(tty_name
):
73 """slave_open(tty_name) -> slave_fd
74 Open the pty slave and acquire the controlling terminal, returning
75 opened filedescriptor.
76 Deprecated, use openpty() instead."""
78 result
= os
.open(tty_name
, os
.O_RDWR
)
80 from fcntl
import ioctl
, I_PUSH
84 ioctl(result
, I_PUSH
, "ptem")
85 ioctl(result
, I_PUSH
, "ldterm")
91 """fork() -> (pid, master_fd)
92 Fork and make the child a session leader with a controlling terminal."""
95 pid
, fd
= os
.forkpty()
96 except (AttributeError, OSError):
103 # os.forkpty() already set us session leader
107 master_fd
, slave_fd
= openpty()
110 # Establish a new session.
114 # Slave becomes stdin/stdout/stderr of child.
115 os
.dup2(slave_fd
, STDIN_FILENO
)
116 os
.dup2(slave_fd
, STDOUT_FILENO
)
117 os
.dup2(slave_fd
, STDERR_FILENO
)
118 if (slave_fd
> STDERR_FILENO
):
121 # Explicitly open the tty to make it become a controlling tty.
122 tmp_fd
= os
.open(os
.ttyname(STDOUT_FILENO
), os
.O_RDWR
)
127 # Parent and child process.
128 return pid
, master_fd
130 def _writen(fd
, data
):
131 """Write all the data to a descriptor."""
133 n
= os
.write(fd
, data
)
137 """Default read function."""
138 return os
.read(fd
, 1024)
140 def _copy(master_fd
, master_read
=_read
, stdin_read
=_read
):
143 pty master -> standard output (master_read)
144 standard input -> pty master (stdin_read)"""
146 rfds
, wfds
, xfds
= select(
147 [master_fd
, STDIN_FILENO
], [], [])
148 if master_fd
in rfds
:
149 data
= master_read(master_fd
)
150 os
.write(STDOUT_FILENO
, data
)
151 if STDIN_FILENO
in rfds
:
152 data
= stdin_read(STDIN_FILENO
)
153 _writen(master_fd
, data
)
155 def spawn(argv
, master_read
=_read
, stdin_read
=_read
):
156 """Create a spawned process."""
157 if type(argv
) == type(''):
159 pid
, master_fd
= fork()
161 os
.execlp(argv
[0], *argv
)
163 mode
= tty
.tcgetattr(STDIN_FILENO
)
164 tty
.setraw(STDIN_FILENO
)
166 except tty
.error
: # This is the same as termios.error
169 _copy(master_fd
, master_read
, stdin_read
)
170 except (IOError, OSError):
172 tty
.tcsetattr(STDIN_FILENO
, tty
.TCSAFLUSH
, mode
)