nxu/term.py

47 lines
1.4 KiB
Python
Raw Normal View History

2023-01-21 15:41:27 -05:00
#!/usr/bin/python
from select import select
from subprocess import Popen, PIPE, STDOUT
import sys
# TODO: byte-by-byte I/O is painful. figure out better buffering
# that doesn't break everything
# currently line-buffered
def run(args):
#return Popen(args, stdin=PIPE, stdout=PIPE, stderr=STDOUT, bufsize=1, universal_newlines=True)
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')
print('')
print('execute the given uxn terminal program (ROM)')
print('and start a shell process (SHELL).')
sys.exit(1)
rom = args[0]
shell = args[1]
term = run(['uxnemu', rom])
term_out = term.stdout.fileno()
shell = run([shell])
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 __name__ == "__main__":
main()