-
Notifications
You must be signed in to change notification settings - Fork 2
/
tff.py
1445 lines (1196 loc) · 45.3 KB
/
tff.py
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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# ***** BEGIN LICENSE BLOCK *****
# Copyright (C) 2012-2014, Hayaki Saito
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
# ***** END LICENSE BLOCK *****
__author__ = "Hayaki Saito (user@zuse.jp)"
__version__ = "0.2.10"
__license__ = "MIT"
signature = 'b87c36758a4c3d666c74490b383f483b'
import sys
import os
import termios
import pty
import signal
import fcntl
import struct
import select
import errno
import codecs
import threading
import logging
_BUFFER_SIZE = 8192
_ESC_TIMEOUT = 0.5 # sec
###############################################################################
#
# Exceptions
#
class NotHandledException(Exception):
''' thrown when an unknown seqnence is detected '''
def __init__(self, value):
"""
>>> e = NotHandledException("test1")
>>> e.value
'test1'
"""
self.value = value
def __str__(self):
"""
>>> e = NotHandledException("test2")
>>> e.value
'test2'
"""
return repr(self.value)
class ParseException(Exception):
''' thrown when a parse error is detected '''
def __init__(self, value):
"""
>>> e = ParseException("test2")
>>> e.value
'test2'
"""
self.value = value
def __str__(self):
"""
>>> e = ParseException("test2")
>>> e.value
'test2'
"""
return repr(self.value)
###############################################################################
#
# interfaces
#
# - EventObserver
# - Scanner
# - OutputStream
# - Parser
# - PTY
#
class EventObserver:
''' adapt to event driven ECMA-35/48 parser model '''
def handle_start(self, context):
raise NotImplementedError("EventObserver::handle_start")
def handle_end(self, context):
raise NotImplementedError("EventObserver::handle_end")
def handle_csi(self, context, params, intermediate, final):
raise NotImplementedError("EventObserver::handle_csi")
def handle_esc(self, context, prefix, final):
raise NotImplementedError("EventObserver::handle_esc")
def handle_ss2(self, context, final):
raise NotImplementedError("EventObserver::handle_ss2")
def handle_ss3(self, context, final):
raise NotImplementedError("EventObserver::handle_ss3")
def handle_control_string(self, context, prefix, value):
raise NotImplementedError("EventObserver::handle_control_string")
def handle_char(self, context, c):
raise NotImplementedError("EventObserver::handle_char")
def handle_invalid(self, context, seq):
raise NotImplementedError("EventObserver::handle_invalid")
def handle_draw(self, context):
raise NotImplementedError("EventObserver::handle_draw")
def handle_resize(self, context, row, col):
raise NotImplementedError("EventObserver::handle_resize")
class Scanner:
''' forward input iterator '''
def __iter__(self):
raise NotImplementedError("Scanner::__iter__")
# deprecated
def assign(self, value, termenc):
raise NotImplementedError("Scanner::assign")
def continuous_assign(self, value, termenc):
raise NotImplementedError("Scanner::continuous_assign")
class OutputStream:
''' abstruct TTY output stream '''
def write(self, c):
raise NotImplementedError("OutputStream::write")
def flush(self):
raise NotImplementedError("OutputStream::flush")
class EventDispatcher:
''' Dispatch interface of terminal sequence event oriented parser '''
def dispatch_esc(self, prefix, final):
raise NotImplementedError("EventDispatcher::dispatch_esc")
def dispatch_csi(self, prefix, params, final):
raise NotImplementedError("EventDispatcher::dispatch_csi")
def dispatch_control_string(self, prefix, value):
raise NotImplementedError("EventDispatcher::dispatch_control_string")
def dispatch_char(self, c):
raise NotImplementedError("EventDispatcher::dispatch_char")
class Parser:
''' abstruct Parser '''
def parse(self, context):
raise NotImplementedError("Parser::parse")
class PTY:
''' abstruct PTY device '''
def fitsize(self):
raise NotImplementedError("PTY::fitsize")
def resize(self, height, width):
raise NotImplementedError("PTY::resize")
def read(self):
raise NotImplementedError("PTY::read")
def write(self, data):
raise NotImplementedError("PTY::write")
def xon(self):
raise NotImplementedError("PTY::xon")
def xoff(self):
raise NotImplementedError("PTY::xoff")
def drive(self):
raise NotImplementedError("PTY::drive")
###############################################################################
#
# Simple Parser implementation
#
class SimpleParser(Parser):
''' simple parser, don't parse ESC/CSI/string seqneces '''
class _MockContext:
def __init__(self):
self.output = []
def __iter__(self):
for i in [1, 2, 3, 4, 5]:
yield i
def dispatch_char(self, c):
self.output.append(c)
def parse(self, context):
"""
>>> parser = SimpleParser()
>>> context = SimpleParser._MockContext()
>>> parser.parse(context)
>>> context.output
[1, 2, 3, 4, 5]
"""
for c in context:
context.dispatch_char(c)
###############################################################################
#
# Default Parser implementation
#
_STATE_GROUND = 0
_STATE_ESC = 1
_STATE_ESC_INTERMEDIATE = 2
_STATE_CSI_PARAMETER = 3
_STATE_CSI_INTERMEDIATE = 4
_STATE_SS2 = 6
_STATE_SS3 = 7
_STATE_OSC = 8
_STATE_OSC_ESC = 9
_STATE_STR = 10
_STATE_STR_ESC = 11
class _MockHandler:
def handle_csi(self, context, parameter, intermediate, final):
print (parameter, intermediate, final)
def handle_esc(self, context, intermediate, final):
print (intermediate, final)
def handle_control_string(self, context, prefix, value):
print (prefix, value)
def handle_char(self, context, c):
print (c)
class DefaultParser(Parser):
''' parse ESC/CSI/string seqneces '''
def __init__(self):
self.reset()
def init(self, context):
self.__context = context
def state_is_esc(self):
return self.__state != _STATE_GROUND
def flush(self):
pbytes = self.__pbytes
ibytes = self.__ibytes
state = self.__state
context = self.__context
if state == _STATE_ESC:
context.dispatch_char(0x1b)
elif state == _STATE_ESC_INTERMEDIATE:
context.dispatch_invalid([0x1b] + ibytes)
elif state == _STATE_CSI_INTERMEDIATE:
context.dispatch_invalid([0x1b, 0x5b] + ibytes)
elif state == _STATE_CSI_PARAMETER:
context.dispatch_invalid([0x1b, 0x5b] + ibytes + pbytes)
def reset(self):
self.__state = _STATE_GROUND
self.__pbytes = []
self.__ibytes = []
def parse(self, data):
context = self.__context
context.assign(data)
pbytes = self.__pbytes
ibytes = self.__ibytes
state = self.__state
for c in context:
if state == _STATE_GROUND:
if c == 0x1b: # ESC
ibytes = []
state = _STATE_ESC
else: # control character
context.dispatch_char(c)
elif state == _STATE_ESC:
#
# - ISO-6429 independent escape sequense
#
# ESC F
#
# - ISO-2022 designation sequence
#
# ESC I ... I F
#
if c == 0x5b: # [
pbytes = []
state = _STATE_CSI_PARAMETER
elif c == 0x5d: # ]
pbytes = [c]
state = _STATE_OSC
elif c == 0x4e: # N
state = _STATE_SS2
elif c == 0x4f: # O
state = _STATE_SS3
elif c == 0x50 or c == 0x58 or c == 0x5e or c == 0x5f:
# P(DCS) or X(SOS) or ^(PM) or _(APC)
pbytes = [c]
state = _STATE_STR
elif c < 0x20: # control character
if c == 0x1b: # ESC
seq = [0x1b]
context.dispatch_invalid(seq)
ibytes = []
state = _STATE_ESC
elif c == 0x18 or c == 0x1a:
seq = [0x1b]
context.dispatch_invalid(seq)
context.dispatch_char(c)
state = _STATE_GROUND
else:
context.dispatch_char(c)
elif c <= 0x2f: # SP to /
ibytes.append(c)
state = _STATE_ESC_INTERMEDIATE
elif c <= 0x7e: # ~
context.dispatch_esc(ibytes, c)
state = _STATE_GROUND
elif c == 0x7f: # control character
context.dispatch_char(c)
else:
seq = [0x1b, c]
context.dispatch_invalid(seq)
state = _STATE_GROUND
elif state == _STATE_CSI_PARAMETER:
# parse control sequence
#
# CSI P ... P I ... I F
# ^
if c > 0x7e:
if c == 0x7f: # control character
context.dispatch_char(c)
else:
seq = [0x1b, 0x5b] + pbytes
context.dispatch_invalid(seq)
state = _STATE_GROUND
elif c > 0x3f: # Final byte, @ to ~
context.dispatch_csi(pbytes, ibytes, c)
state = _STATE_GROUND
elif c > 0x2f: # parameter, 0 to ?
pbytes.append(c)
elif c > 0x1f: # intermediate, SP to /
ibytes.append(c)
state = _STATE_CSI_INTERMEDIATE
# control chars
elif c == 0x1b: # ESC
seq = [0x1b, 0x5b] + pbytes
context.dispatch_invalid(seq)
ibytes = []
state = _STATE_ESC
elif c == 0x18 or c == 0x1a: # CAN, SUB
seq = [0x1b, 0x5b] + pbytes
context.dispatch_invalid(seq)
context.dispatch_char(c)
state = _STATE_GROUND
else:
context.dispatch_char(c)
elif state == _STATE_CSI_INTERMEDIATE:
# parse control sequence
#
# CSI P ... P I ... I F
# ^
if c > 0x7e:
if c == 0x7f: # control character
context.dispatch_char(c)
else:
seq = [0x1b, 0x5b] + pbytes + ibytes
context.dispatch_invalid(seq)
state = _STATE_GROUND
elif c > 0x3f: # Final byte, @ to ~
context.dispatch_csi(pbytes, ibytes, c)
state = _STATE_GROUND
elif c > 0x2f:
seq = [0x1b, 0x5b] + pbytes + ibytes + [c]
context.dispatch_invalid(seq)
state = _STATE_GROUND
elif c > 0x1f: # intermediate, SP to /
ibytes.append(c)
state = _STATE_CSI_INTERMEDIATE
# control chars
elif c == 0x1b: # ESC
seq = [0x1b, 0x5b] + pbytes + ibytes
context.dispatch_invalid(seq)
ibytes = []
state = _STATE_ESC
elif c == 0x18 or c == 0x1a:
seq = [0x1b, 0x5b] + pbytes + ibytes
context.dispatch_invalid(seq)
context.dispatch_char(c)
state = _STATE_GROUND
else:
context.dispatch_char(c)
elif state == _STATE_ESC_INTERMEDIATE:
if c > 0x7e:
if c == 0x7f: # control character
context.dispatch_char(c)
else:
seq = [0x1b] + ibytes + [c]
context.dispatch_invalid(seq)
state = _STATE_GROUND
elif c > 0x2f: # 0 to ~, Final byte
context.dispatch_esc(ibytes, c)
state = _STATE_GROUND
elif c > 0x1f: # SP to /
ibytes.append(c)
state = _STATE_ESC_INTERMEDIATE
elif c == 0x1b: # ESC
seq = [0x1b] + ibytes
context.dispatch_invalid(seq)
ibytes = []
state = _STATE_ESC
elif c == 0x18 or c == 0x1a:
seq = [0x1b] + ibytes
context.dispatch_invalid(seq)
context.dispatch_char(c)
state = _STATE_GROUND
else:
context.dispatch_char(c)
elif state == _STATE_OSC:
# parse control string
if c == 0x07:
context.dispatch_control_string(pbytes[0], ibytes)
state = _STATE_GROUND
elif c < 0x08:
seq = [0x1b] + pbytes + ibytes + [c]
context.dispatch_invalid(seq)
state = _STATE_GROUND
elif c < 0x0e:
ibytes.append(c)
elif c == 0x1b:
state = _STATE_OSC_ESC
elif c < 0x20:
seq = [0x1b] + pbytes + ibytes + [c]
context.dispatch_invalid(seq)
state = _STATE_GROUND
else:
ibytes.append(c)
elif state == _STATE_STR:
# parse control string
# 00/08 - 00/13, 02/00 - 07/14
#
if c < 0x08:
seq = [0x1b] + pbytes + ibytes + [c]
context.dispatch_invalid(seq)
state = _STATE_GROUND
elif c < 0x0e:
ibytes.append(c)
elif c == 0x1b:
state = _STATE_STR_ESC
elif c < 0x20:
seq = [0x1b] + pbytes + ibytes + [c]
context.dispatch_invalid(seq)
state = _STATE_GROUND
else:
ibytes.append(c)
elif state == _STATE_OSC_ESC:
# parse control string
if c == 0x5c:
context.dispatch_control_string(pbytes[0], ibytes)
state = _STATE_GROUND
else:
seq = [0x1b] + pbytes + ibytes + [0x1b, c]
context.dispatch_invalid(seq)
state = _STATE_GROUND
elif state == _STATE_STR_ESC:
# parse control string
# 00/08 - 00/13, 02/00 - 07/14
#
if c == 0x5c:
context.dispatch_control_string(pbytes[0], ibytes)
state = _STATE_GROUND
else:
seq = [0x1b] + pbytes + ibytes + [0x1b, c]
context.dispatch_invalid(seq)
state = _STATE_GROUND
elif state == _STATE_SS3:
if c < 0x20: # control character
if c == 0x1b: # ESC
seq = [0x1b, 0x4f]
context.dispatch_invalid(seq)
ibytes = []
state = _STATE_ESC
elif c == 0x18 or c == 0x1a:
seq = [0x1b, 0x4f]
context.dispatch_invalid(seq)
context.dispatch_char(c)
state = _STATE_GROUND
else:
context.dispatch_char(c)
elif c < 0x7f:
context.dispatch_ss3(c)
state = _STATE_GROUND
else:
seq = [0x1b, 0x4f]
context.dispatch_invalid(seq)
context.dispatch_char(c)
elif state == _STATE_SS2:
if c < 0x20: # control character
if c == 0x1b: # ESC
seq = [0x1b, 0x4e]
context.dispatch_invalid(seq)
ibytes = []
state = _STATE_ESC
elif c == 0x18 or c == 0x1a:
seq = [0x1b, 0x4e]
context.dispatch_invalid(seq)
context.dispatch_char(c)
state = _STATE_GROUND
else:
context.dispatch_char(c)
elif c < 0x7f:
context.dispatch_ss2(c)
state = _STATE_GROUND
else:
seq = [0x1b, 0x4f]
context.dispatch_invalid(seq)
context.dispatch_char(c)
self.__pbytes = pbytes
self.__ibytes = ibytes
self.__state = state
###############################################################################
#
# Scanner implementation
#
class DefaultScanner(Scanner):
''' scan input stream and iterate UCS code points '''
def __init__(self, ucs4=True, termenc=None):
"""
>>> scanner = DefaultScanner()
>>> scanner._ucs4
True
"""
self._data = None
self._ucs4 = ucs4
if termenc:
self._decoder = codecs.getincrementaldecoder(termenc)(errors='replace')
self._termenc = termenc
else:
self._decoder = None
self._termenc = None
# deprecated
def assign(self, value, termenc):
"""
>>> scanner = DefaultScanner()
>>> scanner.assign("01234", "ascii")
>>> scanner._data
u'01234'
"""
if self._termenc != termenc:
self._decoder = codecs.getincrementaldecoder(termenc)(errors='replace')
self._termenc = termenc
self._data = self._decoder.decode(value)
def continuous_assign(self, value):
"""
>>> scanner = DefaultScanner(termenc="utf-8")
>>> scanner.continuous_assign("01234")
>>> scanner._data
u'01234'
"""
self._data = self._decoder.decode(value)
def __iter__(self):
"""
>>> scanner = DefaultScanner()
>>> scanner.assign("abcde", "UTF-8")
>>> print [ c for c in scanner ]
[97, 98, 99, 100, 101]
"""
if self._ucs4:
c1 = 0
for x in self._data:
c = ord(x)
if c >= 0xd800 and c <= 0xdbff:
c1 = c - 0xd800
continue
elif c1 != 0 and c >= 0xdc00 and c <= 0xdfff:
c = 0x10000 + ((c1 << 10) | (c - 0xdc00))
c1 = 0
yield c
else:
for x in self._data:
yield ord(x)
###############################################################################
#
# Handler implementation
#
class DefaultHandler(EventObserver):
''' default handler, pass through all ESC/CSI/string seqnceses '''
def __init__(self):
pass
# EventObserver
def handle_start(self, context):
pass
def handle_end(self, context):
pass
def handle_esc(self, context, intermediate, final):
return False
def handle_csi(self, context, parameter, intermediate, final):
return False
def handle_ss2(self, context, final):
return False
def handle_ss3(self, context, final):
return False
def handle_control_string(self, context, prefix, value):
return False
def handle_char(self, context, c):
return False
def handle_invalid(self, context, seq):
return False
def handle_draw(self, context):
pass
def handle_resize(self, context, row, col):
pass
###############################################################################
#
# Multiplexer implementation
#
class FilterMultiplexer(EventObserver):
def __init__(self, lhs, rhs):
self.__lhs = lhs
self.__rhs = rhs
def get_lhs(self):
return self.__lhs
def get_rhs(self):
return self.__rhs
def handle_start(self, context):
handled_lhs = self.__lhs.handle_start(context)
handled_rhs = self.__rhs.handle_start(context)
return handled_lhs and handled_rhs
def handle_end(self, context):
handled_lhs = self.__lhs.handle_end(context)
handled_rhs = self.__rhs.handle_end(context)
return handled_lhs and handled_rhs
def handle_flush(self, context):
handled_lhs = self.__lhs.handle_flush(context)
handled_rhs = self.__rhs.handle_flush(context)
return handled_lhs and handled_rhs
def handle_csi(self, context, params, intermediate, final):
handled_lhs = self.__lhs.handle_csi(context, params,
intermediate, final)
handled_rhs = self.__rhs.handle_csi(context, params,
intermediate, final)
return handled_lhs and handled_rhs
def handle_esc(self, context, intermediate, final):
handled_lhs = self.__lhs.handle_esc(context, intermediate, final)
handled_rhs = self.__rhs.handle_esc(context, intermediate, final)
return handled_lhs and handled_rhs
def handle_ss2(self, context, final):
handled_lhs = self.__lhs.handle_ss2(context, final)
handled_rhs = self.__rhs.handle_ss2(context, final)
return handled_lhs and handled_rhs
def handle_ss3(self, context, final):
handled_lhs = self.__lhs.handle_ss3(context, final)
handled_rhs = self.__rhs.handle_ss3(context, final)
return handled_lhs and handled_rhs
def handle_control_string(self, context, prefix, value):
handled_lhs = self.__lhs.handle_control_string(context, prefix, value)
handled_rhs = self.__rhs.handle_control_string(context, prefix, value)
return handled_lhs and handled_rhs
def handle_char(self, context, c):
handled_lhs = self.__lhs.handle_char(context, c)
handled_rhs = self.__rhs.handle_char(context, c)
return handled_lhs and handled_rhs
def handle_invalid(self, context, seq):
handled_lhs = self.__lhs.handle_invalid(context, seq)
handled_rhs = self.__rhs.handle_invalid(context, seq)
return handled_lhs and handled_rhs
def handle_draw(self, context):
handled_lhs = self.__lhs.handle_draw(context)
handled_rhs = self.__rhs.handle_draw(context)
return handled_lhs and handled_rhs
def handle_resize(self, context, row, col):
handled_lhs = self.__lhs.handle_resize(context, row, col)
handled_rhs = self.__rhs.handle_resize(context, row, col)
return handled_lhs and handled_rhs
###############################################################################
#
# Dispatcher implementation
#
class ParseContext(OutputStream, EventDispatcher):
def __init__(self,
output,
termenc='UTF-8',
scanner=DefaultScanner(),
handler=DefaultHandler(),
buffering=False):
self.__termenc = termenc
self.__scanner = scanner
self.__handler = handler
self._c1 = 0
if buffering:
try:
from cStringIO import StringIO
self._output = codecs.getwriter(termenc)(StringIO())
except ImportError:
try:
from StringIO import StringIO
self._output = codecs.getwriter(termenc)(StringIO())
except ImportError:
from io import StringIO
self._output = codecs.getwriter(termenc)(StringIO())
else:
self._output = codecs.getwriter(termenc)(output)
self._target_output = output
self._buffering = buffering
def __iter__(self):
return self.__scanner.__iter__()
def assign(self, data):
self.__scanner.assign(data, self.__termenc)
if self._buffering:
self._output.truncate(0)
def sethandler(self, handler):
self.__handler = handler
def putu(self, data):
self._output.write(data)
def puts(self, data):
self._target_output.write(data)
def put(self, c):
if c < 0x80:
self._output.write(chr(c))
elif c < 0xd800:
self._output.write(unichr(c))
elif c < 0xdc00:
self._c1 = c
elif c < 0xe000:
self._output.write(unichr(self._c1) + unichr(c))
elif c < 0x10000:
self._output.write(unichr(c))
else: # c > 0x10000
c -= 0x10000
c1 = (c >> 10) + 0xd800
c2 = (c & 0x3ff) + 0xdc00
self._output.write(unichr(c1) + unichr(c2))
# obsoluted!!
def writestring(self, data):
try:
self._target_output.write(data)
except Exception:
self._output.write(data)
# OutputStream
# obsoluted!!
def write(self, c):
self.put(c)
def flush(self):
if self._buffering:
self._target_output.write(self._output)
try:
self._target_output.flush()
except IOError:
pass
# EventDispatcher
def dispatch_esc(self, intermediate, final):
if not self.__handler.handle_esc(self, intermediate, final):
self.put(0x1b) # ESC
for c in intermediate:
self.put(c)
self.put(final)
def dispatch_csi(self, parameter, intermediate, final):
if not self.__handler.handle_csi(self, parameter, intermediate, final):
self.put(0x1b) # ESC
self.put(0x5b) # [
for c in parameter:
self.put(c)
for c in intermediate:
self.put(c)
self.put(final)
def dispatch_ss2(self, final):
if not self.__handler.handle_ss2(self, final):
self.put(0x1b) # ESC
self.put(0x4e) # N
self.put(final)
def dispatch_ss3(self, final):
if not self.__handler.handle_ss3(self, final):
self.put(0x1b) # ESC
self.put(0x4f) # O
self.put(final)
def dispatch_control_string(self, prefix, value):
if not self.__handler.handle_control_string(self, prefix, value):
self.put(0x1b) # ESC
self.put(prefix)
for c in value:
self.put(c)
self.put(0x1b) # ESC
self.put(0x5c) # \
def dispatch_char(self, c):
if not self.__handler.handle_char(self, c):
self.put(c)
def dispatch_invalid(self, seq):
if not self.__handler.handle_invalid(self, seq):
for c in seq:
self.put(c)
###############################################################################
#
# DefaultPTY
#
class DefaultPTY(PTY):
def __init__(self, term, lang, command, stdin, row=None, col=None):
self._stdin_fileno = stdin.fileno()
backup = termios.tcgetattr(self._stdin_fileno)
self._backup_termios = backup
pid, master = pty.fork()
if not pid:
os.environ['TERM'] = term
os.environ['LANG'] = lang
os.execlp('/bin/sh', '/bin/sh', '-c', 'exec %s' % command)
self.__setupterm(self._stdin_fileno)
self.pid = pid
self._master = master
if row and col:
self.resize(row, col)
def close(self):
#self.restore_term()
try:
os.close(self._master)
except OSError, e:
logging.exception(e)
logging.info("DefaultPTY.close: master=%d" % self._master)
def restore_term(self):
termios.tcsetattr(self._stdin_fileno,
termios.TCSANOW,
self._backup_termios)
def __setupterm(self, fd):
term = termios.tcgetattr(fd)
## c_iflag
#IUTF8 = 16384
term[0] &= ~(termios.IGNBRK
| termios.BRKINT
| termios.PARMRK
| termios.ISTRIP
| termios.INLCR
| termios.IGNCR
| termios.ICRNL
| termios.IXON)
term[1] &= ~(termios.OPOST
| termios.ONLCR)
# c_cflag
c_cflag = term[2]
c_cflag &= ~(termios.CSIZE | termios.PARENB)
c_cflag |= termios.CS8
term[2] = c_cflag
## c_lflag
c_lflag = term[3]
c_lflag &= ~(termios.ECHO
| termios.ECHONL
| termios.ICANON
| termios.ISIG
| termios.IEXTEN)
term[3] = c_lflag
# c_cc
# this PTY is jast a filter, so it must not fire signals
vdisable = os.fpathconf(self._stdin_fileno, 'PC_VDISABLE')
VDSUSP = 11
c_cc = term[6]
c_cc[termios.VEOF] = vdisable # Ctrl-D
c_cc[termios.VINTR] = vdisable # Ctrl-C
c_cc[termios.VREPRINT] = vdisable # Ctrl-R
c_cc[termios.VSTART] = vdisable # Ctrl-Q
c_cc[termios.VSTOP] = vdisable # Ctrl-S
c_cc[termios.VLNEXT] = vdisable # Ctrl-V