Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 2 | |
| 3 | import os |
| 4 | import sys |
| 5 | import time |
| 6 | import select |
| 7 | import fcntl |
| 8 | import termios |
| 9 | import readline |
| 10 | import signal |
| 11 | |
| 12 | def nonblockingfd(fd): |
| 13 | fcntl.fcntl(fd, fcntl.F_SETFL, fcntl.fcntl(fd, fcntl.F_GETFL) | os.O_NONBLOCK) |
| 14 | |
| 15 | def echonocbreak(fd): |
| 16 | old = termios.tcgetattr(fd) |
| 17 | old[3] = old[3] | termios.ECHO | termios.ICANON |
| 18 | termios.tcsetattr(fd, termios.TCSADRAIN, old) |
| 19 | |
| 20 | def cbreaknoecho(fd): |
| 21 | old = termios.tcgetattr(fd) |
| 22 | old[3] = old[3] &~ termios.ECHO &~ termios.ICANON |
| 23 | termios.tcsetattr(fd, termios.TCSADRAIN, old) |
| 24 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 25 | if len(sys.argv) != 3 or sys.argv[1] in ('-h', '--help'): |
| 26 | print('oepydevshell-internal.py: error: the following arguments are required: pty, pid\n' |
| 27 | 'Usage: oepydevshell-internal.py pty pid\n\n' |
| 28 | 'OpenEmbedded oepydevshell-internal.py - internal script called from meta/classes/devshell.bbclass\n\n' |
| 29 | 'arguments:\n' |
| 30 | ' pty pty device name\n' |
| 31 | ' pid parent process id\n\n' |
| 32 | 'options:\n' |
| 33 | ' -h, --help show this help message and exit\n') |
| 34 | sys.exit(2) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 35 | |
| 36 | pty = open(sys.argv[1], "w+b", 0) |
| 37 | parent = int(sys.argv[2]) |
| 38 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 39 | nonblockingfd(pty) |
| 40 | nonblockingfd(sys.stdin) |
| 41 | |
| 42 | |
| 43 | histfile = os.path.expanduser("~/.oedevpyshell-history") |
| 44 | readline.parse_and_bind("tab: complete") |
| 45 | try: |
| 46 | readline.read_history_file(histfile) |
| 47 | except IOError: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 48 | pass |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 49 | |
| 50 | try: |
| 51 | |
| 52 | i = "" |
| 53 | o = "" |
| 54 | # Need cbreak/noecho whilst in select so we trigger on any keypress |
| 55 | cbreaknoecho(sys.stdin.fileno()) |
| 56 | # Send our PID to the other end so they can kill us. |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 57 | pty.write(str(os.getpid()).encode('utf-8') + b"\n") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 58 | while True: |
| 59 | try: |
| 60 | writers = [] |
| 61 | if i: |
| 62 | writers.append(sys.stdout) |
| 63 | (ready, _, _) = select.select([pty, sys.stdin], writers , [], 0) |
| 64 | try: |
| 65 | if pty in ready: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 66 | i = i + pty.read().decode('utf-8') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 67 | if i: |
| 68 | # Write a page at a time to avoid overflowing output |
| 69 | # d.keys() is a good way to do that |
| 70 | sys.stdout.write(i[:4096]) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 71 | sys.stdout.flush() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 72 | i = i[4096:] |
| 73 | if sys.stdin in ready: |
| 74 | echonocbreak(sys.stdin.fileno()) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 75 | o = input().encode('utf-8') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 76 | cbreaknoecho(sys.stdin.fileno()) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 77 | pty.write(o + b"\n") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 78 | except (IOError, OSError) as e: |
| 79 | if e.errno == 11: |
| 80 | continue |
| 81 | if e.errno == 5: |
| 82 | sys.exit(0) |
| 83 | raise |
| 84 | except EOFError: |
| 85 | sys.exit(0) |
| 86 | except KeyboardInterrupt: |
| 87 | os.kill(parent, signal.SIGINT) |
| 88 | |
| 89 | except SystemExit: |
| 90 | pass |
| 91 | except Exception as e: |
| 92 | import traceback |
| 93 | print("Exception in oepydehshell-internal: " + str(e)) |
| 94 | traceback.print_exc() |
| 95 | time.sleep(5) |
| 96 | finally: |
| 97 | readline.write_history_file(histfile) |