File: _term.py

package info (click to toggle)
openipmi 2.0.7-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 10,200 kB
  • ctags: 14,662
  • sloc: ansic: 126,919; sh: 9,454; python: 6,885; perl: 5,838; makefile: 507
file content (711 lines) | stat: -rw-r--r-- 23,938 bytes parent folder | download
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
# _term.py
#
# openipmi generic terminal handling for SoL
#
# Author: MontaVista Software, Inc.
#         Corey Minyard <minyard@mvista.com>
#         source@mvista.com
#
# Copyright 2006 MontaVista Software Inc.
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public License
#  as published by the Free Software Foundation; either version 2 of
#  the License, or (at your option) any later version.
#
#
#  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
#  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
#  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
#  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
#  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
#  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this program; if not, write to the Free
#  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

black = 0
red = 1
green = 2
yellow = 3
blue = 4
magenta = 5
cyan = 6
white = 7

BOLD = 1
UNDERLINE = 2
INVERSE = 4
BLINK = 8
CONCEALED = 16

class TerminalEmulator:
    def __init__(self):
        self.buf = [ ]
        # Mode is a 2-d array of [ cflags, bg color, fg color ]
        self.modes = [ ]
        # Style is (fg, bg, flags)
        for i in range(0, 24):
            self.buf.append([ ])
            self.modes.append([ ])
            for j in range(0, 80):
                self.buf[i].append(" ")
                self.modes[i].append( [0, 0, 7] )
                pass
            pass
        self.cflags = 0
        self.bg_color = black
        self.fg_color = white
        self.x = 0
        self.y = 0
        self.height = 24
        self.width = 80
        self.InputHandler = self.Input0
        self.parms = None
        self.saved_pos = [1, 1]
        self.scroll_region = [0, self.height-1]
        self.keypad_alt = False
        return

    def check_scroll_down(self):
        redraw = False
        starty = self.GetStartY()
        endy = self.GetEndY()
        if (self.y == endy):
            old = self.buf[starty]
            del(self.buf[starty])
            self.buf.insert(endy, old)
            old = self.modes[starty]
            del(self.modes[starty])
            self.modes.insert(endy, old)
            for j in range(0, self.width):
                self.buf[endy][j] = " "
                self.modes[endy][j][0] = 0
                self.modes[endy][j][1] = black
                self.modes[endy][j][2] = white
                pass
            pass
            self.ScrollLines(starty, endy)
        else:
            self.y += 1
            pass
        return

    def check_scroll_up(self):
        starty = self.GetStartY()
        endy = self.GetEndY()
        if (self.y == starty):
            old = self.buf[endy]
            del(self.buf[endy])
            self.buf.insert(starty, old)
            for j in range(0, self.width):
                self.buf[starty][j] = ' '
                self.modes[starty][j][0] = 0
                self.modes[starty][j][1] = black
                self.modes[starty][j][2] = white
                pass
            pass
            self.ScrollLinesUp(starty, endy)
        else:
            self.y -= 1
            pass
        return

    def output_at(self, x, y, slen):
        s = ""
        i = 0
        last_mode = [ 0, 0, 7 ]
        lastx = 0
        while (i < slen):
            if (len(s) > 0):
                if (last_mode != self.modes[y][x+i]):
                    # Character mode change, output what we have and switch.
                    self.DrawText(last_mode[2], last_mode[1], last_mode[0],
                                  x, y, s)
                    s = ""
                    pass
                pass
            else:
                last_mode = self.modes[y][x+i]
                lastx = x + i
                pass
            s += self.buf[y][x+i]
            i += 1
            pass
        self.DrawText(last_mode[2], last_mode[1], last_mode[0],
                      lastx, y, s)
        return

    def handle_cursor(self):
        if (self.x == self.width):
            xpos = self.x - 1
        else:
            xpos = self.x
            pass
        mode = self.modes[self.y][xpos]
        self.DrawCursor(mode[2], mode[1], mode[0], xpos, self.y,
                        self.buf[self.y][xpos])
        return

    def restore_cursor(self):
        if (self.x == self.width):
            self.output_at(self.x-1, self.y, 1)
            pass
        else:
            self.output_at(self.x, self.y, 1)
            pass
        return
        
    def output_str(self, s):
        if (len(s) == 0):
            return
        do_scroll = False
        if (self.x == self.width):
            # Cursor is at the end of the line, redraw the char where
            # the cursor is sitting.
            self.restore_cursor()
            self.x = 0;
            self.check_scroll_down()
            pass
        while ((self.x + len(s)) > self.width):
            # The string exceeds the line length, do it in pieces
            outlen = self.width - self.x
            do_scroll = True
            self.buf[self.y][self.x:self.x+outlen] = s[0:outlen]
            self.output_at(self.x, self.y, outlen)
            self.x = 0;
            s = s[outlen:]
            self.check_scroll_down()
            pass
        outlen = len(s)
        for i in range(0, outlen):
            self.buf[self.y][self.x+i] = s[i]
        for i in range(self.x, self.x+outlen):
            self.modes[self.y][i][0] = self.cflags
            self.modes[self.y][i][1] = self.bg_color
            self.modes[self.y][i][2] = self.fg_color
            pass
        self.output_at(self.x, self.y, outlen)
        self.x += outlen
        self.handle_cursor()
        return

    def GetParm(self, n, default = 1):
        if (self.parms == None):
            return default
        elif (len(self.parms) <= n):
            return default
        return self.parms[n]

    # Get the current upper bound of the cursor, used for scroll regions.
    def GetStartY(self):
        if ((self.y >= self.scroll_region[0])
            and (self.y <= self.scroll_region[1])):
            return self.scroll_region[0]
        return 0

    # Get the current lower bound of the cursor, used for scroll regions.
    def GetEndY(self):
        if ((self.y >= self.scroll_region[0])
            and (self.y <= self.scroll_region[1])):
            return self.scroll_region[1]
        return self.height - 1

    # After '\e['
    def Input2(self, c, s):
        if (c == 'A'): # Up
            count = self.GetParm(0)
            starty = self.GetStartY()
            self.restore_cursor()
            if (count > (self.y - starty)):
                self.y = starty
            else:
                self.y -= count
                pass
            self.handle_cursor();
            pass
        elif (c == 'B'): # Down
            count = self.GetParm(0)
            endy = self.GetEndY()
            self.restore_cursor()
            if (count > (endy - self.y)):
                self.y = endy
            else:
                self.y += count
                pass
            self.handle_cursor();
            pass
        elif (c == 'C'): # Right
            count = self.GetParm(0)
            self.restore_cursor()
            if (count > (self.width - self.x - 1)):
                self.x = self.width - 1
            else:
                self.x += count
                pass
            self.handle_cursor();
            pass
        elif (c == 'D'): # Right
            count = self.GetParm(0)
            self.restore_cursor()
            if (count > self.x):
                self.x = 0
            else:
                self.x -= count
                pass
            self.handle_cursor();
            pass
        elif (c == '?'): # FIXME: Not sure what this does
            return "" # Stay in Input2
        elif ((c >= '0') and (c <= '9')):
            if (self.parms == None):
                self.parms = [ int(c) ]
            else:
                currparm = len(self.parms) - 1
                self.parms[currparm] *= 10
                self.parms[currparm] += int(c)
                pass
            return "" # Stay in Input2
        elif (c == ';'): # Next parm
            if (self.parms == None):
                self.parms = [ 0, 0 ]
            else:
                self.parms.append(0)
                pass
            return "" # Stay in Input2
        elif (c == 'r'): # Scroll region
            y1 = self.GetParm(0, -1)
            y2 = self.GetParm(1, -1)
            if ((y1 == -1) or (y2 == -1)):
                if ((y1 == -1) and (y2 == -1)):
                    self.scroll_region[0] = 0
                    self.scroll_region[1] = self.height - 1
                    pass
                pass
            elif ((y1 > y2) or (y1 < 1) or (y2 < 1)
                  or (y1 >= (self.height+1)) or (y2 >= (self.height+1))):
                # Bogus values, just ignre them.
                pass
            else:
                self.scroll_region[0] = y1 - 1
                self.scroll_region[1] = y2 - 1
                pass
            pass
        elif ((c == 'H') or (c == 'f')): # Move to position specified
            y = self.GetParm(0)
            x = self.GetParm(1)
            if (x < 1):
                x = 1
            elif (x > self.width):
                x = self.width
                pass
            self.restore_cursor()
            self.x = x - 1
            self.y = y - 1
            self.handle_cursor();
            pass
        elif (c == 's'): # save cursor position
            self.saved_pos[0] = self.x
            self.saved_pos[1] = self.y
            pass
        elif (c == 'u'): # Restore cursor position
            self.restore_cursor()
            self.x = self.saved_pos[0]
            self.y = self.saved_pos[1]
            self.handle_cursor();
            pass
        elif (c == 'J'): # Clear screen area
            mode = self.GetParm(0, -1)
            if (mode == -1):
                starty = self.y
                length = self.height - self.y
                pass
            elif (mode == 1):
                starty = 0
                length = self.y + 1
                pass
            elif (mode == 2):
                starty = 0
                length = self.height
                pass
            else:
                starty = 0
                length = 0
                pass
            for y in range(starty, starty + length):
                for x in range(0, self.width):
                    self.buf[y][x] = " "
                    self.modes[y][x][0] = 0
                    self.modes[y][x][1] = black
                    self.modes[y][x][2] = white
                    pass
                self.output_at(0, y, self.width)
                pass
            self.handle_cursor();
            pass
        elif (c == 'K'): # Clear line
            mode = self.GetParm(0, 0)
            y = self.y
            if (mode == 0): # To end of line
                startx = self.x
                length = self.width - self.x
                pass
            elif (mode == 1): # To start of line
                startx = 0
                length = self.x + 1
                pass
            elif (mode == 2): # Whole line
                startx = 0
                length = self.width
                pass
            else: # Ignore
                startx = 0
                length = 0
                pass
            for x in range(startx, startx+length):
                self.buf[y][x] = " "
                self.modes[y][x][0] = 0
                self.modes[y][x][1] = black
                self.modes[y][x][2] = white
                pass
            self.output_at(startx, y, length)
            self.handle_cursor();
        elif (c == 'm'): # Graphics mode
            i = 0
            val = self.GetParm(i, 0)
            while (val != -1):
                if (val == 0):
                    self.cflags = 0
                    self.bg_color = black
                    self.fg_color = white
                elif (val == 1):
                    self.cflags |= BOLD
                elif (val == 4):
                    self.cflags |= UNDERLINE
                elif (val == 5):
                    self.cflags |= BLINK
                elif (val == 7):
                    self.cflags |= INVERSE
                elif (val == 8):
                    self.cflags |= CONCEALED
                elif ((val >= 30) and (val <= 37)):
                    self.fg_color = val - 30
                elif ((val >= 40) and (val <= 47)):
                    self.bg_color = val - 40
                    pass
                i += 1
                val = self.GetParm(i, -1)
                pass
            pass
        elif (c == 'g'):
             # FIXME: \e[2g means clear tabs, so does 3g, 0g or just g
             # means current tab
            pass
        elif (c == 'P'): # delete parm characters (1 default)
            count = self.GetParm(0, 1)
            if (count > (self.width - self.x)):
                count = self.width - self.x
                pass
            y = self.y
            x = self.x
            for i in range(0, count):
                del self.buf[y][x]
                self.buf[y].append(' ')
                old = self.modes[y][x]
                del self.modes[y][x]
                old[0] = 0
                old[1] = black
                old[2] = white
                self.modes[y].append(old)
                pass
            self.DeleteChars(x, y, count)
            self.handle_cursor()
            pass
        elif (c == 'M'): # delete parm lines (1 default)
            count = self.GetParm(0, 1)
            if (count > (self.height - self.y)):
                count = self.height - self.y
                pass
            for i in range(0, count):
                old = self.buf[self.y]
                del self.buf[self.y]
                self.buf.append(old)
                old = self.modes[self.y]
                del self.modes[self.y]
                self.modes.append(old)
                for j in range(0, self.width):
                    self.buf[self.height-1][j] = ' '
                    self.modes[self.height-1][j][0] = 0
                    self.modes[self.height-1][j][1] = black
                    self.modes[self.height-1][j][2] = white
                    pass
                self.ScrollLines(self.y, self.height-1)
                pass
            self.handle_cursor()
            pass
        elif (c == 'L'): # insert parm lines (1 default)
            self.restore_cursor()
            count = self.GetParm(0, 1)
            if (count > (self.height - self.y)):
                count = self.height - self.y
                pass
            for i in range(0, count):
                old = self.buf[self.height-1]
                del self.buf[self.height-1]
                self.buf.insert(self.y, old)
                for j in range(0, self.width):
                    self.buf[self.y][j] = ' '
                    self.modes[self.y][j][0] = 0
                    self.modes[self.y][j][1] = black
                    self.modes[self.y][j][2] = white
                    pass
                self.ScrollLinesUp(self.y, self.height-1)
                pass
            self.handle_cursor()
            pass
        elif (c == 'Z'): # Move to previous tab stop
            self.restore_cursor()
            self.x = ((self.x-1) / 8) * 8
            self.handle_cursor();
            pass
        elif (c == '@'): # insert parm chars (1 default)
            self.restore_cursor()
            count = self.GetParm(0, 1)
            if (count > (self.width - self.x)):
                count = self.width - self.x
                pass
            y = self.y
            x = self.x
            for i in range(0, count):
                del self.buf[y][self.width-1]
                old = self.modes[y][self.width-1]
                del self.modes[y][self.width-1]
                old[0] = 0
                old[1] = black
                old[2] = white
                self.buf[y].insert(self.x, ' ')
                self.modes[y].insert(self.x, old)
                pass
            self.InsertChars(x, y, count)
            self.handle_cursor()
            pass
        elif (c == 'S'): # scroll forward parm lines (1 default)
            self.restore_cursor()
            count = self.GetParm(0, 1)
            if (count > self.height):
                count = self.height
                pass
            for i in range(0, count):
                old = self.buf[0]
                del self.buf[0]
                self.buf.append(old)
                old = self.modes[0]
                del self.modes[0]
                self.modes.append(old)
                for j in range(0, self.width):
                    self.buf[self.height-1][j] = ' '
                    self.modes[self.height-1][j][0] = 0
                    self.modes[self.height-1][j][1] = black
                    self.modes[self.height-1][j][2] = white
                    pass
                self.ScrollLines(0, self.height-1)
                pass
            self.handle_cursor()
            pass
        elif (c == 'T'): # scroll back parm lines (1 default)
            self.restore_cursor()
            count = self.GetParm(0, 1)
            if (count > self.height):
                count = self.height
                pass
            for i in range(0, count):
                old = self.buf[self.height-1]
                del self.buf[self.height-1]
                self.buf.insert(0, old)
                old = self.modes[self.height-1]
                del self.modes[self.height-1]
                self.modes.insert(0, old)
                for j in range(0, self.width):
                    self.buf[0][j] = ' '
                    self.modes[0][j][0] = 0
                    self.modes[0][j][1] = black
                    self.modes[0][j][2] = white
                    pass
                self.ScrollLinesUp(0, self.height-1)
                pass
            self.handle_cursor()
            pass
        elif (c == 'G'): # Move cursor to column parm (1 default)
            self.restore_cursor()
            pos = self.GetParm(0, 1)
            if (pos > self.width):
                pos = self.width
                pass
            elif (pos < 1):
                return ""
            self.x = pos - 1
            self.handle_cursor()
            pass
        elif (c == 'd'): # Move cursor to line parm (1 default)
            self.restore_cursor()
            pos = self.GetParm(0, 1)
            if (pos > self.height):
                pos = self.height
                pass
            elif (pos < 1):
                return ""
            self.y = pos - 1
            self.handle_cursor()
            pass
        elif (c == 'X'): # erase parm characters (1 default) (no cursor move)
            count = self.GetParm(0, 1)
            if (count > (self.height - self.y)):
                count = self.height - self.y
                pass
            for i in range(0, count):
                self.buf[self.y][self.x+i] = " "
                self.modes[self.y][self.x+i][0] = 0
                self.modes[self.y][self.x+i][1] = black
                self.modes[self.y][self.x+i][2] = white
                pass
            self.output_at(self.x, self.y, count)
            self.handle_cursor()
            pass
        elif (c == 'c'): # Identify terminal
            # Identify ourself as "linux"
            self.HandleTerminalOutput("\x1b[?62;9;c")
            pass
        self.InputHandler = self.Input0
        return ""

    # After an escape
    def Input1(self, c, s):
        if (c == '['):
            self.InputHandler = self.Input2
            return ""
        elif (c == 'D'): # Scroll down
            self.restore_cursor()
            self.check_scroll_down()
            self.handle_cursor();
        elif (c == 'M'): # Scroll up
            self.restore_cursor()
            self.check_scroll_up()
            self.handle_cursor();
            pass
        elif (c == 'H'): # FIXME: Set tabulator stop in all rows at current column
            pass
        elif (c == 'c'): # reset terminal
            for y in range(0, self.height):
                for x in range(0, self.width):
                    self.buf[y][x] = " "
                    self.modes[y][x][0] = 0
                    self.modes[y][x][1] = black
                    self.modes[y][x][2] = white
                    pass
                self.output_at(0, y, self.width)
                pass
            self.x = 0
            self.y = 0
            self.cflags = 0
            self.bg_color = black
            self.fg_color = white
            self.handle_cursor();
            pass
        elif ((c >= '0') and (c <= '9')):
            if (self.parms == None):
                self.parms = [ int(c) ]
            else:
                currparm = len(self.parms) - 1
                self.parms[currparm] *= 10
                self.parms[currparm] += int(c)
                pass
            return "" # Stay in Input1
        elif (c == 'n'): # Terminal state
            op = self.GetParm(0, 0)
            if (op == 5): # Requesting terminal status
                self.HandleTerminalOutput("\x1b0n") # We are ok
            elif (op == 6): # Current cursor position
                self.HandleTerminalOutput("\x1b%d;%dR" % (self.y+1, self.x+1))
            pass
        elif (c == '='): # alternate keypad mode
            self.keypat_alt = True
            pass
        elif (c == '>'): # alternate keypad mode off
            self.keypat_alt = False
            pass
        self.InputHandler = self.Input0
        return ""

    # "normal" input mode
    def Input0(self, c, s):
        if ((c >= ' ') and (c <= '~')):
            return s + c
        else:
            self.output_str(s)
            if (c == '\n'):
                self.restore_cursor()
                self.check_scroll_down()
                self.handle_cursor();
                pass
            elif (c == '\r'):
                self.restore_cursor()
                self.x = 0
                self.handle_cursor();
                pass
            elif (c == '\t'):
                self.restore_cursor()
                if (self.x >= self.width-8):
                    self.x = self.width-1
                else:
                    self.x = ((self.x / 8) * 8) + 8
                    pass
                self.handle_cursor();
                pass
            elif (c == '\007'): #bell
                self.Bell()
            elif (c == '\010'): #backspace
                if (self.x > 0):
                    self.restore_cursor()
                    self.x -= 1
                    self.handle_cursor();
                    pass
                pass
            elif (c == '\x1b'):
                self.parms = None
                self.InputHandler = self.Input1
                pass
            pass
        return ""
    
    def ProcessInput(self, data):
        s = ""
        for c in data:
            s = self.InputHandler(c, s)
            pass
        self.output_str(s)
        return

    def ResizeTerminal(self, w, h):
        return

    def Width(self):
        return self.width

    def Height(self):
        return self.height

    def Update(self):
        return

    def Reset(self):
        return
    
    pass