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: |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 66 | readdata = pty.read() |
| 67 | if readdata: |
| 68 | i = i + readdata.decode('utf-8') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 69 | if i: |
| 70 | # Write a page at a time to avoid overflowing output |
| 71 | # d.keys() is a good way to do that |
| 72 | sys.stdout.write(i[:4096]) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 73 | sys.stdout.flush() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 74 | i = i[4096:] |
| 75 | if sys.stdin in ready: |
| 76 | echonocbreak(sys.stdin.fileno()) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 77 | o = input().encode('utf-8') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 78 | cbreaknoecho(sys.stdin.fileno()) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 79 | pty.write(o + b"\n") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 80 | except (IOError, OSError) as e: |
| 81 | if e.errno == 11: |
| 82 | continue |
| 83 | if e.errno == 5: |
| 84 | sys.exit(0) |
| 85 | raise |
| 86 | except EOFError: |
| 87 | sys.exit(0) |
| 88 | except KeyboardInterrupt: |
| 89 | os.kill(parent, signal.SIGINT) |
| 90 | |
| 91 | except SystemExit: |
| 92 | pass |
| 93 | except Exception as e: |
| 94 | import traceback |
| 95 | print("Exception in oepydehshell-internal: " + str(e)) |
| 96 | traceback.print_exc() |
| 97 | time.sleep(5) |
| 98 | finally: |
| 99 | readline.write_history_file(histfile) |