Update terminalManagement
This commit is contained in:
parent
aab718855f
commit
ec048d2ea0
@ -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)
|
||||||
if r == []:
|
while running:
|
||||||
continue
|
r, w, x = select.select([sys.stdin, p_out],[],[],0)
|
||||||
for fd in r:
|
if r == []:
|
||||||
if fd == signal_pipe[0]:
|
continue
|
||||||
msgBytes = os.read(signal_pipe[0], 1)
|
if p_out in r:
|
||||||
if fd == sys.stdin:
|
if debug:
|
||||||
msgBytes = os.read(sys.stdin.fileno(), 65536)
|
print('pre p_out')
|
||||||
terminal.feed(msgBytes)
|
try:
|
||||||
p_out.write(msgBytes)
|
msgBytes = os.read(p_out.fileno(), 4096)
|
||||||
if fd == p_out:
|
#p_out.read(4096)
|
||||||
try:
|
except (EOFError, OSError):
|
||||||
msgBytes = p_out.read(65536)
|
running = False
|
||||||
except (EOFError, OSError):
|
#sys.exit(0)
|
||||||
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:
|
||||||
except Exception as e: # Process died?
|
print('after p_out')
|
||||||
print(e)
|
if sys.stdin in r:
|
||||||
Running = False
|
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
|
||||||
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)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user