rewriting DbgTerminal.contents_changed. Much better now
[pygdb.git] / DbgTerminal.py
1 #!/usr/bin/python
2 #shuber, 2008-06-04
3
4 __author__ = "shuber"
5
6
7 import gtk
8 import os
9 import pango
10 import pty
11 import string
12 import sys
13 import time
14 import threading
15 import vte
16
17 import ClientIOTerminal
18
19
20
21 class DbgTerminal (vte.Terminal):
22
23 def __init__(self, clientCmd):
24
25 vte.Terminal.__init__(self)
26
27 #Set members
28 self.childpid = None
29 self.history = [""]
30 self.isactive = True
31 self.lastc, self.lastr = 0,0
32
33 #Start debugger
34 self.clientCmd = clientCmd
35 #Open pseudo-terminal where to-be-debugged process reads/writes to
36 self.client_ptymaster, self.client_ptyslave = pty.openpty()
37
38 #Set up terminal window and initialize debugger
39 self.connect("cursor-moved", self.contents_changed)
40 self.connect("child-exited", quitHandler)
41
42 #font description
43 fontdesc = pango.FontDescription("monospace 9")
44 self.set_font(fontdesc)
45
46
47 def initialize(self):
48 self.childpid = self.fork_command( self.getCommand(), self.getArgv())
49 self.waitForActivation(0)
50 self.setPty(self.client_ptyslave)
51
52 def stopDbg(self):
53
54 if self.childpid != None:
55 #9=KILL, 15=TERM
56 os.kill(self.childpid, 15);
57 self.childpid = None
58
59
60
61 def contents_changed(self, term):
62 assert( self.getHistoryLen()>0 )
63
64 c,r = term.get_cursor_position()
65 text = self.get_text_range(self.lastr,self.lastc,r,c-1,lambda *w:True)
66 self.lastc, self.lastr = c,r
67
68 #Remove annoying \n at the end
69 assert(text[-1] == "\n")
70 text = text[:-1]
71
72 #Get the lines and remove empty lines
73 lines = string.split(text, "\n")
74
75 #Remove the incomplete line
76 self.history[-1] += lines[0]
77 self.history += lines[1:]
78
79
80 def waitForNewline(self):
81 l = self.getHistoryLen()
82 while not self.getHistoryLen() > l:
83 gtk.main_iteration()
84
85 def getHistoryLen(self):
86 return len(self.history)
87
88 def waitForRx(self, rx, start):
89 curr = start
90 while True:
91 assert( curr>=start )
92 for no in range(curr, self.getHistoryLen()):
93 line = self.history[no]
94 if rx.search(line):
95 return no, line
96
97 #Do not forget the last line
98 curr = max(start,self.getHistoryLen()-1)
99 gtk.main_iteration()
100
101
102 def getCommand(self):
103 return self.getArgv()[0];
104
105 def getArgv(self):
106 raise NotImplementedError()
107
108 def setPty(self, pty):
109 raise NotImplementedError()
110
111 def setRun(self):
112 raise NotImplementedError()
113
114 def setContinue(self):
115 raise NotImplementedError()
116
117 def setStepover(self):
118 raise NotImplementedError()
119
120 def setStepin(self):
121 raise NotImplementedError()
122
123 def setQuit(self):
124 raise NotImplementedError()
125
126 def setBreakpoint(self, file, lineno, condition=False):
127 raise NotImplementedError()
128
129 def delBreakpoint(self, breakpoint):
130 raise NotImplementedError()
131
132 def getExpression(self, expr):
133 raise NotImplementedError()
134
135 def waitForActivation(self, his):
136 raise NotImplementedError()
137
138 def setActive(self, isactive):
139 self.isactive = isactive
140
141 def isActive(self):
142 return self.isactive
143
144
145
146 def quitHandler(*w):
147 try:
148 gtk.main_quit()
149 except:
150 pass
151
152
153
154 class DbgWindow (gtk.Window):
155
156 clientIOWnd = None
157
158
159 def __init__(self, terminal):
160
161 #Set up some members
162 self.terminal = terminal
163
164 #Set up GTK stuff
165 gtk.Window.__init__(self)
166 self.connect("destroy", quitHandler)
167
168 #Set title and add terminal
169 self.set_title("Debugger I/O")
170 self.terminal.history = []
171 self.terminal.history_length = 5
172 self.add(self.terminal)
173
174 #Show the window
175 self.show_all()
176
177 def toggleClientIOWindow(self):
178 if not self.clientIOWnd:
179 self.clientIOWnd = ClientIOTerminal.ClientIOWindow(self, \
180 self.terminal.client_ptymaster)
181 else:
182 self.clientIOWnd.destroy()
183 self.clientIOWnd = None
184
185 def isClientIOWindowExisting(self):
186 return self.clientIOWnd != None
187
188
189