-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterm.py
More file actions
129 lines (113 loc) · 3.97 KB
/
term.py
File metadata and controls
129 lines (113 loc) · 3.97 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python
import sys
import subprocess
import select
import fcntl
from collections import defaultdict
import data
import multiprocessing
import os
import io
import time
import struct
import outputparser
import terminalrenderer
class AppWrapper(object):
def __init__(self, execPath):
self.execPath = execPath
def start(self, readPipe, processError=None):
self.process = multiprocessing.Process(target=self._run, kwargs = {"readPipe":readPipe,
"processError":processError})
self.process.start()
print >>sys.stderr, "AppWrapper: %d", self.process.pid
def stop(self):
self.process.terminate()
self.process.join()
def _run(self, readPipe, processError):
df = subprocess.Popen(self.execPath, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
df_fd = df.stdout.fileno()
df_fl = fcntl.fcntl(df_fd, fcntl.F_GETFL)
fcntl.fcntl(df_fd, fcntl.F_SETFL, df_fl | os.O_NONBLOCK)
n = 0
f = open("f.log","w")
print os.getpid(), sys.stdin.fileno()
epoll = select.poll()
STDIN = io.FileIO(name=0, closefd=False)
epoll.register(STDIN, select.POLLIN)
epoll.register(df_fd, select.POLLIN)
try:
t0 = time.time()
prevTs = 0
while True:
n += 1
ret = epoll.poll(1000)
if not ret:
os.write(readPipe, outputparser.FLUSH_COMMAND)
for (sock, event) in ret:
if sock == STDIN.fileno():
d = STDIN.read()
if d != "":
df.stdin.write(d)
elif sock == df_fd:
d = os.read(df_fd, 1000)
if len(d) == 0:
f.close()
print 'CLOSING'
return
ts = int(time.time() - t0) * 1000
if ts != prevTs:
ts_pack = struct.pack("I", ts)
ts_pack = ts_pack.replace("\x1b", "\x1a") #Can't send the escape char do to how the parser works.
print >>sys.stderr, ts, ts_pack
f.write("\x1bD%s%s" % (ts_pack, d))
prevTs = ts
else:
f.write(d)
os.write(readPipe, d)
except Exception as e:
print e
pass
f.write(str(e))
finally:
print "EXITTT"
os.write(readPipe, "\d")
f.close()
#readPipe.close()
os.close(readPipe)
print
if __name__ == "__main__":
from graphics import *
fd = sys.stdin.fileno()
fl = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
appWrapper = AppWrapper("df_linux/df")
#appWrapper = AppWrapper("telnet towel.blinkenlights.nl")
outputParserServer = outputparser.OutputParserServer(data.escape_code_map)
(outPipe, inPipe) = outputParserServer.getPipes()
outputParserServer.start()
appWrapper.start(outPipe)
import termModel
#def __init__(self, glyphManager, gfxTerm, charGroup):
glyphManager = GlyphManager()
glyphManager.loadSprite(GlyphManager.VT220_GLYPH_FILENAME, 10, 8)
g = GfxTerm()
group = g.getSpriteGroup()
gfxCharFactory = GfxCharFactory(glyphManager, g, group)
t = termModel.Terminal(gfxCharFactory, 25, 80)
g.setTermModel(t)
t.setDisplay(g)
while True:
print '1',
cmd = inPipe.recv()
g.sink()
print '2',
if cmd == "QUIT":
break
if cmd:
print '3',
r = t.render(cmd)
if r:
print "[","]"
print "GOODBYE"
appWrapper.stop()
outputParserServer.stop()