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 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)
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
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:
if p_out in r:
if debug:
print('pre p_out')
try:
msgBytes = p_out.read(65536)
msgBytes = os.read(p_out.fileno(), 4096)
#p_out.read(4096)
except (EOFError, OSError):
sys.exit(0)
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
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)