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