nxu/term.py

54 lines
1.5 KiB
Python
Raw Normal View History

2023-01-21 15:41:27 -05:00
#!/usr/bin/python
2023-01-21 15:58:02 -05:00
#
# ./term.py term.rom /bin/bash
2023-01-21 15:41:27 -05:00
from select import select
from subprocess import Popen, PIPE, STDOUT
import sys
2023-01-21 15:58:02 -05:00
# currently unbuffered
#
# TODO: figure out a more reasonable buffering strategy
2023-01-21 15:41:27 -05:00
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')
print('')
print('execute the given uxn terminal program (ROM)')
print('and start a shell process (SHELL).')
2023-01-21 15:58:02 -05:00
print('')
print('example: %s term.rom /bin/bash' % sys.argv[0])
2023-01-21 15:41:27 -05:00
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))
2023-01-21 15:58:02 -05:00
if shell.poll() is None:
shell.kill()
if term.poll() is None:
term.kill()
2023-01-21 15:41:27 -05:00
if __name__ == "__main__":
main()