Update terminalManagement

This commit is contained in:
chrys87 2017-08-22 16:01:27 +02:00 committed by GitHub
parent aab718855f
commit ec048d2ea0

View File

@ -3,11 +3,12 @@ import struct
import sys import sys
import pty import pty
import tty import tty
import termios
import shlex import shlex
import signal import signal
import select import select
import pyte import pyte
import time
class Terminal: class Terminal:
def __init__(self, columns, lines, p_in): def __init__(self, columns, lines, p_in):
@ -31,7 +32,7 @@ class Terminal:
return {"c": (cursor.x, cursor.y), "lines": lines} return {"c": (cursor.x, cursor.y), "lines": lines}
def open_terminal(command="bash -i", columns=80, lines=24): def open_terminal(command="bash", columns=80, lines=24):
p_pid, master_fd = pty.fork() p_pid, master_fd = pty.fork()
if p_pid == 0: # Child. if p_pid == 0: # Child.
argv = shlex.split(command) argv = shlex.split(command)
@ -54,38 +55,45 @@ def convert_to_text(dump):
def HandleTerminal(): def HandleTerminal():
debug = False
running = True
attr = termios.tcgetattr(sys.stdin.fileno())
try: try:
terminal, p_pid, p_out = open_terminal()
signal_pipe = os.pipe()
Running = True
tty.setraw(0) tty.setraw(0)
while Running: terminal, p_pid, p_out = open_terminal()
try: termios.tcdrain(p_pid)
r, w, x = select.select([sys.stdin, p_out,signal_pipe[0]],[],[],0) termios.tcdrain(0)
while running:
r, w, x = select.select([sys.stdin, p_out],[],[],0)
if r == []: if r == []:
continue continue
for fd in r: if p_out in r:
if fd == signal_pipe[0]: if debug:
msgBytes = os.read(signal_pipe[0], 1) print('pre p_out')
if fd == sys.stdin:
msgBytes = os.read(sys.stdin.fileno(), 65536)
terminal.feed(msgBytes)
p_out.write(msgBytes)
if fd == p_out:
try: try:
msgBytes = p_out.read(65536) msgBytes = os.read(p_out.fileno(), 4096)
#p_out.read(4096)
except (EOFError, OSError): except (EOFError, OSError):
sys.exit(0) running = False
#sys.exit(0)
terminal.feed(msgBytes) terminal.feed(msgBytes)
os.write(sys.stdout.fileno(), msgBytes) os.write(sys.stdout.fileno(), msgBytes)
#print(terminal.screen.display) #print(terminal.screen.display)
if debug:
print('after p_out')
if sys.stdin in r:
if debug:
print('pre stdin')
msgBytes = os.read(sys.stdin.fileno(), 4096)
terminal.feed(msgBytes)
p_out.write(msgBytes)
if debug:
print('after stdin')
except Exception as e: # Process died? except Exception as e: # Process died?
print(e) print(e)
Running = False running = False
except Exception as e: # Process died?
print(e)
Running = False
finally: finally:
termios.tcsetattr(0, termios.TCSADRAIN, attr)
p_out.close() p_out.close()
os.kill(p_pid, signal.SIGTERM) os.kill(p_pid, signal.SIGTERM)