nxu/term.py

52 lines
1.5 KiB
Python
Executable File

#!/usr/bin/python
#
# ./term.py term.rom /bin/bash
from select import select
from subprocess import Popen, PIPE, STDOUT
import sys
# currently unbuffered
#
# TODO: figure out a more reasonable buffering strategy
def run(args):
return Popen(args, stdin=PIPE, stdout=PIPE, stderr=STDOUT, bufsize=0)
def main():
args = sys.argv[1:]
if len(args) < 2:
print('usage: %s ROM SHELL [ ARGS... ]')
print('')
print('execute the given uxn terminal program (ROM)')
print('and start a shell process (SHELL + ARGS).')
print('')
print('example: %s term.rom /bin/bash -i' % sys.argv[0])
sys.exit(1)
term = run(['uxnemu', args[0]])
term_out = term.stdout.fileno()
shell = run(args[1:])
shell_out = shell.stdout.fileno()
while shell.poll() is None and term.poll() is None:
# wait 100ms to see if anything is ready to write
ns, _, _ = select([term_out, shell_out], [], [], 0.1)
for n in ns:
if n == term_out:
shell.stdin.write(term.stdout.read(1))
shell.stdin.flush()
elif n == shell_out:
term.stdin.write(shell.stdout.read(1))
term.stdin.flush()
else:
raise Exception('unexpected fileno: %d (expected %d or %d)' % (n, term_out, shell_out))
if shell.poll() is None:
shell.kill()
if term.poll() is None:
term.kill()
if __name__ == "__main__":
main()