diff --git a/play zone/terminalManagement b/play zone/terminalManagement index f6eae61e..05ea4c78 100644 --- a/play zone/terminalManagement +++ b/play zone/terminalManagement @@ -3,11 +3,12 @@ import struct import sys import pty import tty +import termios import shlex import signal import select - import pyte +import time class Terminal: def __init__(self, columns, lines, p_in): @@ -31,7 +32,7 @@ class Terminal: 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() if p_pid == 0: # Child. argv = shlex.split(command) @@ -54,38 +55,45 @@ def convert_to_text(dump): def HandleTerminal(): + debug = False + running = True + attr = termios.tcgetattr(sys.stdin.fileno()) try: - terminal, p_pid, p_out = open_terminal() - signal_pipe = os.pipe() - Running = True tty.setraw(0) - while Running: - try: - r, w, x = select.select([sys.stdin, p_out,signal_pipe[0]],[],[],0) - if r == []: - continue - for fd in r: - if fd == signal_pipe[0]: - msgBytes = os.read(signal_pipe[0], 1) - if fd == sys.stdin: - msgBytes = os.read(sys.stdin.fileno(), 65536) - terminal.feed(msgBytes) - p_out.write(msgBytes) - if fd == p_out: - try: - msgBytes = p_out.read(65536) - except (EOFError, OSError): - sys.exit(0) - terminal.feed(msgBytes) - os.write(sys.stdout.fileno(), msgBytes) - #print(terminal.screen.display) - except Exception as e: # Process died? - print(e) - Running = False + terminal, p_pid, p_out = open_terminal() + termios.tcdrain(p_pid) + termios.tcdrain(0) + while running: + r, w, x = select.select([sys.stdin, p_out],[],[],0) + if r == []: + continue + if p_out in r: + if debug: + print('pre p_out') + try: + msgBytes = os.read(p_out.fileno(), 4096) + #p_out.read(4096) + except (EOFError, OSError): + running = False + #sys.exit(0) + terminal.feed(msgBytes) + os.write(sys.stdout.fileno(), msgBytes) + #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? print(e) - Running = False + running = False finally: + termios.tcsetattr(0, termios.TCSADRAIN, attr) p_out.close() os.kill(p_pid, signal.SIGTERM)