3
# openipmi GUI command window handling
5
# Author: MontaVista Software, Inc.
6
# Corey Minyard <minyard@mvista.com>
9
# Copyright 2006 MontaVista Software Inc.
11
# This program is free software; you can redistribute it and/or
12
# modify it under the terms of the GNU Lesser General Public License
13
# as published by the Free Software Foundation; either version 2 of
14
# the License, or (at your option) any later version.
17
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
18
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
23
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
25
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
26
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
# You should have received a copy of the GNU Lesser General Public
29
# License along with this program; if not, write to the Free
30
# Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35
import xml.dom.minidom
41
class CommandWindow(Tix.ScrolledText):
42
def __init__(self, parent, ui):
44
Tix.ScrolledText.__init__(self, parent)
48
self.max_history = 100
49
self.text.bind("<Key>", self.HandleChar)
50
self.text.bind("<Control-Key>", self.HandleCtrlChar)
51
self.text.insert("end", "> ")
54
for cmd in init_history:
55
self.history.append(cmd[1])
58
self.history.append("")
60
self.currhist = self.lasthist
62
self.cmdlang = OpenIPMI.alloc_cmdlang(self)
64
self.cmd_in_progress = False
66
self.bind("<Destroy>", self.OnDestroy)
68
OpenIPMI.set_cmdlang_global_err_handler(self)
71
def global_cmdlang_err(self, objstr, location, errstr, errval):
72
log = "Global cmdlang err: " + errstr;
73
if (len(location) > 0) or (len(objstr) > 0):
74
log += " at " + objstr + "(" + location + ")"
76
log += ": " + errstr + " (" + str(errval) + ")"
80
def OnDestroy(self, event):
84
def cmdlang_down(self, cmdlang):
88
def cmdlang_up(self, cmdlang):
89
if (self.indent >= 2):
94
def HandleNewLines(self):
95
lastline = int(self.text.index("end").split(".")[0]) - 1
96
while (lastline > self.max_lines):
97
self.delete("1.0", "2.0")
98
lastline = int(self.text.index("end").split(".")[0]) - 1
102
def InsertString(self, string):
103
(lastrow, lastcol) = self.text.index("end").split(".")
104
lastrow = str(int(lastrow)-1)
105
self.text.insert(lastrow + ".0", string)
106
self.HandleNewLines()
107
self.text.see("insert")
110
def cmdlang_done(self, cmdlang):
111
err = cmdlang.get_err()
113
errtext = cmdlang.get_errstr()
114
objstr = cmdlang.get_objstr()
115
location = cmdlang.get_location()
116
if (location == None):
120
str = ("error: %s: %s (0x%x, %s)\n"
121
% (location, errtext, err,
122
OpenIPMI.get_error_string(err)))
125
str = ("error: %s %s: %s (0x%x, %s)\n"
126
% (location, objstr, errtext, err,
127
OpenIPMI.get_error_string(err)))
129
self.InsertString(str)
131
self.cmd_in_progress = False
132
self.text.insert("end", "> ")
135
def cmdlang_out(self, cmdlang, name, value):
136
if (cmdlang.is_help()):
137
self.InsertString("%*s%s %s\n" % (self.indent, "", name, value))
140
self.InsertString("%*s%s: %s\n" % (self.indent, "", name, value))
144
def cmdlang_out_binary(self, cmdlang, name, value):
145
self.InsertString("%*s%s: %s\n" % (self.indent, "", name, str(value)))
148
def cmdlang_out_unicode(self, cmdlang, name, value):
149
self.InsertString("%*s%s:U: %s\n" % (self.indent, "", name, str(value)))
152
def HandleNewHistory(self):
153
self.history.append("")
154
if (self.lasthist >= self.max_history):
162
def HandleCtrlChar(self, event):
163
# This is here to catch the control characters and pass them
164
# on so HandleChar() doesn't trap and throw them away.
167
def HandleChar(self, event):
169
if ((key == "Backspace") or (key == "Delete")):
170
# A key that will result in a backspace. Make sure it
171
# only occurs on the last line and not in the prompt area.
172
if (self.cmd_in_progress):
174
(lastrow, lastcol) = self.text.index("end").split(".")
175
lastrow = str(int(lastrow)-1)
176
(currrow, currcol) = self.text.index("insert").split(".")
177
if ((lastrow != currrow) or (col <= 2)):
178
# Ignore the keypress
181
elif (key == "Return"):
182
# Enter the command...
183
if (self.cmd_in_progress):
185
(lastrow, lastcol) = self.text.index("end").split(".")
186
lastrow = str(int(lastrow)-1)
187
(currrow, currcol) = self.text.index("insert").split(".")
188
if ((lastrow != currrow) or (int(currcol) <= 2)):
189
# Ignore the keypress
192
command = self.text.get(lastrow + ".2", lastrow + ".end")
193
self.HandleNewLines();
195
self.text.insert("end", "\n")
196
self.history[self.lasthist] = command
197
self.HandleNewHistory()
198
self.cmdlang.handle(str(command))
201
self.text.insert("end", "\n> ")
203
self.text.mark_set("insert", "end")
204
self.currhist = self.lasthist
205
self.text.see("insert")
209
if (self.cmd_in_progress):
211
if (self.currhist == 0):
213
(lastrow, lastcol) = self.text.index("end").split(".")
214
lastrow = str(int(lastrow)-1)
215
if (self.currhist == self.lasthist):
216
command = self.text.get(lastrow + ".2", lastrow + ".end")
217
self.history[self.lasthist] = command
219
self.text.delete(lastrow + ".2", lastrow + ".end")
221
self.text.insert(lastrow + ".2", self.history[self.currhist])
223
elif (key == "Down"):
224
if (self.cmd_in_progress):
227
if (self.currhist == self.lasthist):
229
(lastrow, lastcol) = self.text.index("end").split(".")
230
lastrow = str(int(lastrow)-1)
231
self.text.delete(lastrow + ".2", lastrow + ".end")
233
self.text.insert(lastrow + ".2", self.history[self.currhist])
235
elif (len(event.char) == 1) and (event.char < chr(255)):
236
# A key that will result in text addition. Make sure it
237
# only occurs on the last line and not in the prompt area.
238
if (self.cmd_in_progress):
240
(lastrow, lastcol) = self.text.index("end").split(".")
241
lastrow = str(int(lastrow)-1)
242
(currrow, currcol) = self.text.index("insert").split(".")
243
if ((lastrow != currrow) or (int(currcol) < 2)):
244
# Ignore the keypress
247
elif ((key == "Left") or (key == "Right") or
249
(key == "End") or (key == "Home") or
250
(key == "Prior") or (key == "Next")):
260
return cmp(a[0], b[0])
262
def _HistorySave(file):
263
if (not init_history):
265
domimpl = xml.dom.getDOMImplementation()
266
doc = domimpl.createDocument(None, "IPMIHistory", None)
267
main = doc.documentElement
269
for cmd in init_history:
271
helem = doc.createElement("hval")
272
helem.setAttribute("idx", str(i))
273
helem.setAttribute("val", cmd)
274
main.appendChild(helem)
280
doc.writexml(f, indent='', addindent='\t', newl='\n')
285
def _HistoryRestore(file):
287
doc = xml.dom.minidom.parse(file).documentElement
290
for c in doc.childNodes:
291
if (c.nodeType == c.ELEMENT_NODE):
293
idx = int(c.getAttribute("idx"))
294
val = c.getAttribute("val")
295
init_history.append( (idx, val) )
301
init_history.sort(cmphist)