nxu/term.py

52 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:
2023-01-21 16:07:56 -05:00
print('usage: %s ROM SHELL [ ARGS... ]')
2023-01-21 15:41:27 -05:00
print('')
print('execute the given uxn terminal program (ROM)')
2023-01-21 16:07:56 -05:00
print('and start a shell process (SHELL + ARGS).')
2023-01-21 15:58:02 -05:00
print('')
2023-01-21 16:07:56 -05:00
print('example: %s term.rom /bin/bash -i' % sys.argv[0])
2023-01-21 15:41:27 -05:00
sys.exit(1)
2023-01-21 16:07:56 -05:00
term = run(['uxnemu', args[0]])
2023-01-21 15:41:27 -05:00
term_out = term.stdout.fileno()
2023-01-21 16:07:56 -05:00
shell = run(args[1:])
2023-01-21 15:41:27 -05:00
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()