diff options
Diffstat (limited to 'scripts/oepydevshell-internal.py')
| -rwxr-xr-x | scripts/oepydevshell-internal.py | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/scripts/oepydevshell-internal.py b/scripts/oepydevshell-internal.py new file mode 100755 index 0000000000..f7b2e4e0bf --- /dev/null +++ b/scripts/oepydevshell-internal.py | |||
| @@ -0,0 +1,92 @@ | |||
| 1 | #!/usr/bin/env python | ||
| 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 | |||
| 25 | if len(sys.argv) != 3: | ||
| 26 | print("Incorrect parameters") | ||
| 27 | sys.exit(1) | ||
| 28 | |||
| 29 | pty = open(sys.argv[1], "w+b", 0) | ||
| 30 | parent = int(sys.argv[2]) | ||
| 31 | |||
| 32 | # Don't buffer output by line endings | ||
| 33 | sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) | ||
| 34 | sys.stdin = os.fdopen(sys.stdin.fileno(), 'r', 0) | ||
| 35 | nonblockingfd(pty) | ||
| 36 | nonblockingfd(sys.stdin) | ||
| 37 | |||
| 38 | |||
| 39 | histfile = os.path.expanduser("~/.oedevpyshell-history") | ||
| 40 | readline.parse_and_bind("tab: complete") | ||
| 41 | try: | ||
| 42 | readline.read_history_file(histfile) | ||
| 43 | except IOError: | ||
| 44 | pass | ||
| 45 | |||
| 46 | try: | ||
| 47 | |||
| 48 | i = "" | ||
| 49 | o = "" | ||
| 50 | # Need cbreak/noecho whilst in select so we trigger on any keypress | ||
| 51 | cbreaknoecho(sys.stdin.fileno()) | ||
| 52 | # Send our PID to the other end so they can kill us. | ||
| 53 | pty.write(str(os.getpid()) + "\n") | ||
| 54 | while True: | ||
| 55 | try: | ||
| 56 | writers = [] | ||
| 57 | if i: | ||
| 58 | writers.append(sys.stdout) | ||
| 59 | (ready, _, _) = select.select([pty, sys.stdin], writers , [], 0) | ||
| 60 | try: | ||
| 61 | if pty in ready: | ||
| 62 | i = i + pty.read() | ||
| 63 | if i: | ||
| 64 | # Write a page at a time to avoid overflowing output | ||
| 65 | # d.keys() is a good way to do that | ||
| 66 | sys.stdout.write(i[:4096]) | ||
| 67 | i = i[4096:] | ||
| 68 | if sys.stdin in ready: | ||
| 69 | echonocbreak(sys.stdin.fileno()) | ||
| 70 | o = raw_input() | ||
| 71 | cbreaknoecho(sys.stdin.fileno()) | ||
| 72 | pty.write(o + "\n") | ||
| 73 | except (IOError, OSError) as e: | ||
| 74 | if e.errno == 11: | ||
| 75 | continue | ||
| 76 | if e.errno == 5: | ||
| 77 | sys.exit(0) | ||
| 78 | raise | ||
| 79 | except EOFError: | ||
| 80 | sys.exit(0) | ||
| 81 | except KeyboardInterrupt: | ||
| 82 | os.kill(parent, signal.SIGINT) | ||
| 83 | |||
| 84 | except SystemExit: | ||
| 85 | pass | ||
| 86 | except Exception as e: | ||
| 87 | import traceback | ||
| 88 | print("Exception in oepydehshell-internal: " + str(e)) | ||
| 89 | traceback.print_exc() | ||
| 90 | time.sleep(5) | ||
| 91 | finally: | ||
| 92 | readline.write_history_file(histfile) | ||
