aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/oepydevshell-internal.py
blob: a22bec3365f6036936f17ecf0fa228156c9868c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
.highlight .kt { color: #66d9ef } /* Keyword.Type */
.highlight .ld { color: #e6db74 } /* Literal.Date */
.highlight .m { color: #ae81ff } /* Literal.Number */
.highlight .s { color: #e6db74 } /* Literal.String */
.highlight .na { color: #a6e22e } /* Name.Attribute */
.highlight .nb { color: #f8f8f2 } /* Name.Builtin */
.highlight .nc { color: #a6e22e } /* Name.Class */
.highlight .no { color: #66d9ef } /* Name.Constant */
.highlight .nd { color: #a6e22e } /* Name.Decorator */
.highlight .ni { color: #f8f8f2 } /* Name.Entity */
.highlight .ne { color: #a6e22e } /* Name.Exception */
.highlight .nf
#!/usr/bin/env python3

import os
import sys
import time
import select
import fcntl
import termios
import readline
import signal

def nonblockingfd(fd):
    fcntl.fcntl(fd, fcntl.F_SETFL, fcntl.fcntl(fd, fcntl.F_GETFL) | os.O_NONBLOCK)

def echonocbreak(fd):
    old = termios.tcgetattr(fd)
    old[3] = old[3] | termios.ECHO | termios.ICANON
    termios.tcsetattr(fd, termios.TCSADRAIN, old)

def cbreaknoecho(fd):
    old = termios.tcgetattr(fd)
    old[3] = old[3] &~ termios.ECHO &~ termios.ICANON
    termios.tcsetattr(fd, termios.TCSADRAIN, old)

if len(sys.argv) != 3:
    print("Incorrect parameters")
    sys.exit(1)

pty = open(sys.argv[1], "w+b", 0)
parent = int(sys.argv[2])

nonblockingfd(pty)
nonblockingfd(sys.stdin)


histfile = os.path.expanduser("~/.oedevpyshell-history")
readline.parse_and_bind("tab: complete")
try:
    readline.read_history_file(histfile)
except IOError:
    pass 

try:

    i = ""
    o = ""
    # Need cbreak/noecho whilst in select so we trigger on any keypress
    cbreaknoecho(sys.stdin.fileno())
    # Send our PID to the other end so they can kill us.
    pty.write(str(os.getpid()).encode('utf-8') + b"\n")
    while True:
        try:
            writers = []
            if i:
                writers.append(sys.stdout)
            (ready, _, _) = select.select([pty, sys.stdin], writers , [], 0)
            try:
                if pty in ready:
                    i = i + pty.read().decode('utf-8')
                if i:
                    # Write a page at a time to avoid overflowing output 
                    # d.keys() is a good way to do that
                    sys.stdout.write(i[:4096])
                    sys.stdout.flush()
                    i = i[4096:]
                if sys.stdin in ready:
                    echonocbreak(sys.stdin.fileno())
                    o = input().encode('utf-8')
                    cbreaknoecho(sys.stdin.fileno())
                    pty.write(o + b"\n")
            except (IOError, OSError) as e:
                if e.errno == 11:
                    continue
                if e.errno == 5:
                    sys.exit(0)
                raise
            except EOFError:
                sys.exit(0)
        except KeyboardInterrupt:
            os.kill(parent, signal.SIGINT)

except SystemExit:
    pass
except Exception as e:
    import traceback
    print("Exception in oepydehshell-internal: " + str(e))
    traceback.print_exc()
    time.sleep(5)
finally:
    readline.write_history_file(histfile)