File: VT102.pm

package info (click to toggle)
libterm-vt102-perl 0.91-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 272 kB
  • ctags: 96
  • sloc: perl: 2,471; makefile: 15
file content (2248 lines) | stat: -rw-r--r-- 72,765 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
# Term::VT102 - module for VT102 emulation in Perl
#
# Copyright (C) Andrew Wood
# NO WARRANTY - see COPYING.
#

package Term::VT102;

use strict;

BEGIN {
	use Exporter ();
	use vars qw($VERSION @ISA);

	$VERSION = '0.91';

	@ISA = qw(Exporter);
}


# Return the packed version of a set of attributes fg, bg, bo, fa, st, ul,
# bl, rv.
#
sub attr_pack {
	shift if ref($_[0]); # called in object context, ditch the object
	my ($fg, $bg, $bo, $fa, $st, $ul, $bl, $rv) = @_;
	my $num = 0;

	$num = ($fg & 7)
	  | (($bg & 7) << 4)
	  | ($bo << 8)
	  | ($fa << 9)
	  | ($st << 10)
	  | ($ul << 11)
	  | ($bl << 12)
	  | ($rv << 13);
	return pack ('S', $num);
}


# Return the unpacked version of a packed attribute.
#
sub attr_unpack {
	shift if ref($_[0]); # called in object context, ditch the object
	my $data = shift;
	my ($num, $fg, $bg, $bo, $fa, $st, $ul, $bl, $rv);

	$num = unpack ('S', $data);

	($fg, $bg, $bo, $fa, $st, $ul, $bl, $rv) = (
	  $num & 7,
	  ($num >> 4) & 7,
	  ($num >> 8) & 1,
	  ($num >> 9) & 1,
	  ($num >> 10) & 1,
	  ($num >> 11) & 1,
	  ($num >> 12) & 1,
	  ($num >> 13) & 1
	);

	return ($fg, $bg, $bo, $fa, $st, $ul, $bl, $rv);
}


# Default attribute set (in both packed and unpacked flavors)
#
use constant DEFAULT_ATTR => (7, 0, 0, 0, 0, 0, 0, 0);
use constant DEFAULT_ATTR_PACKED => attr_pack(&DEFAULT_ATTR);


# Constructor function.
#
sub new {
	my ($proto, %init) = @_;
	my $class = ref ($proto) || $proto;
	my $self = {};

	$self->{'_ctlseq'} = { (  # control characters
	  "\000" => 'NUL',            # ignored
	  "\005" => 'ENQ',            # trigger answerback message
	  "\007" => 'BEL',            # beep
	  "\010" => 'BS',             # backspace one column
	  "\011" => 'HT',             # horizontal tab to next tab stop
	  "\012" => 'LF',             # line feed
	  "\013" => 'VT',             # line feed
	  "\014" => 'FF',             # line feed
	  "\015" => 'CR',             # carriage return
	  "\016" => 'SO',             # activate G1 character set & newline
	  "\017" => 'SI',             # activate G0 character set
	  "\021" => 'XON',            # resume transmission
	  "\023" => 'XOFF',           # stop transmission, ignore characters
	  "\030" => 'CAN',            # interrupt escape sequence
	  "\032" => 'SUB',            # interrupt escape sequence
	  "\033" => 'ESC',            # start escape sequence
	  "\177" => 'DEL',            # ignored
	  "\233" => 'CSI'             # equivalent to ESC [
	) };

	$self->{'_escseq'} = { (  # escape sequences
	   'c' => 'RIS',              # reset
	   'D' => 'IND',              # line feed
	   'E' => 'NEL',              # newline
	   'H' => 'HTS',              # set tab stop at current column
	   'M' => 'RI',               # reverse line feed
	   'Z' => 'DECID',            # DEC private ID; return ESC [ ? 6 c (VT102)
	   '7' => 'DECSC',            # save state (position, charset, attributes)
	   '8' => 'DECRC',            # restore most recently saved state
	   '[' => 'CSI',              # control sequence introducer
	  '[[' => 'IGN',              # ignored control sequence
	  '%@' => 'CSDFL',            # select default charset (ISO646/8859-1)
	  '%G' => 'CSUTF8',           # select UTF-8
	  '%8' => 'CSUTF8',           # select UTF-8 (obsolete)
	  '#8' => 'DECALN',           # DEC alignment test - fill screen with E's
	  '(8' => 'G0DFL',            # G0 charset = default mapping (ISO8859-1)
	  '(0' => 'G0GFX',            # G0 charset = VT100 graphics mapping
	  '(U' => 'G0ROM',            # G0 charset = null mapping (straight to ROM)
	  '(K' => 'G0USR',            # G0 charset = user defined mapping
	  '(B' => 'G0TXT',            # G0 charset = ASCII mapping
	  ')8' => 'G1DFL',            # G1 charset = default mapping (ISO8859-1)
	  ')0' => 'G1GFX',            # G1 charset = VT100 graphics mapping
	  ')U' => 'G1ROM',            # G1 charset = null mapping (straight to ROM)
	  ')K' => 'G1USR',            # G1 charset = user defined mapping
	  ')B' => 'G1TXT',            # G1 charset = ASCII mapping
	  '*8' => 'G2DFL',            # G2 charset = default mapping (ISO8859-1)
	  '*0' => 'G2GFX',            # G2 charset = VT100 graphics mapping
	  '*U' => 'G2ROM',            # G2 charset = null mapping (straight to ROM)
	  '*K' => 'G2USR',            # G2 charset = user defined mapping
	  '+8' => 'G3DFL',            # G3 charset = default mapping (ISO8859-1)
	  '+0' => 'G3GFX',            # G3 charset = VT100 graphics mapping
	  '+U' => 'G3ROM',            # G3 charset = null mapping (straight to ROM)
	  '+K' => 'G3USR',            # G3 charset = user defined mapping
	   '>' => 'DECPNM',           # set numeric keypad mode
	   '=' => 'DECPAM',           # set application keypad mode
	   'N' => 'SS2',              # select G2 charset for next char only
	   'O' => 'SS3',              # select G3 charset for next char only
	   'P' => 'DCS',              # device control string (ended by ST)
	   'X' => 'SOS',              # start of string
	   '^' => 'PM',               # privacy message (ended by ST)
	   '_' => 'APC',              # application program command (ended by ST)
	  "\\" => 'ST',               # string terminator
	   'n' => 'LS2',              # invoke G2 charset
	   'o' => 'LS3',              # invoke G3 charset
	   '|' => 'LS3R',             # invoke G3 charset as GR
	   '}' => 'LS2R',             # invoke G2 charset as GR
	   '~' => 'LS1R',             # invoke G1 charset as GR
	   ']' => 'OSC',              # operating system command
	   'g' => 'BEL',              # alternate BEL
	) };

	$self->{'_csiseq'} = { (  # ECMA-48 CSI sequences
	  '[' => 'IGN',               # ignored control sequence
	  '@' => 'ICH',               # insert blank characters
	  'A' => 'CUU',               # move cursor up
	  'B' => 'CUD',               # move cursor down
	  'C' => 'CUF',               # move cursor right
	  'D' => 'CUB',               # move cursor left
	  'E' => 'CNL',               # move cursor down and to column 1
	  'F' => 'CPL',               # move cursor up and to column 1
	  'G' => 'CHA',               # move cursor to column in current row
	  'H' => 'CUP',               # move cursor to row, column
	  'J' => 'ED',                # erase display
	  'K' => 'EL',                # erase line
	  'L' => 'IL',                # insert blank lines
	  'M' => 'DL',                # delete lines
	  'P' => 'DCH',               # delete characters on current line
	  'X' => 'ECH',               # erase characters on current line
	  'a' => 'HPR',               # move cursor right
	  'c' => 'DA',                # return ESC [ ? 6 c (VT102)
	  'd' => 'VPA',               # move to row (current column)
	  'e' => 'VPR',               # move cursor down
	  'f' => 'HVP',               # move cursor to row, column
	  'g' => 'TBC',               # clear tab stop (CSI 3 g = clear all stops)
	  'h' => 'SM',                # set mode
	  'l' => 'RM',                # reset mode
	  'm' => 'SGR',               # set graphic rendition
	  'n' => 'DSR',               # device status report
	  'q' => 'DECLL',             # set keyboard LEDs
	  'r' => 'DECSTBM',           # set scrolling region to (top, bottom) rows
	  's' => 'CUPSV',             # save cursor position
	  'u' => 'CUPRS',             # restore cursor position
	  '`' => 'HPA'                # move cursor to column in current row
	) };

	$self->{'_modeseq'} = { ( # ANSI/DEC specified modes for SM/RM
	                           # ANSI Specified Modes
	    '0'   => 'IGN',           # Error (Ignored)
	    '1'   => 'GATM',          # guarded-area transfer mode (ignored)
	    '2'   => 'KAM',           # keyboard action mode (always reset)
	    '3'   => 'CRM',           # control representation mode (always reset)
	    '4'   => 'IRM',           # insertion/replacement mode (always reset)
	    '5'   => 'SRTM',          # status-reporting transfer mode
	    '6'   => 'ERM',           # erasure mode (always set)
	    '7'   => 'VEM',           # vertical editing mode (ignored)
	   '10'   => 'HEM',           # horizontal editing mode
	   '11'   => 'PUM',           # positioning unit mode
	   '12'   => 'SRM',           # send/receive mode (echo on/off)
	   '13'   => 'FEAM',          # format effector action mode
	   '14'   => 'FETM',          # format effector transfer mode
	   '15'   => 'MATM',          # multiple area transfer mode
	   '16'   => 'TTM',           # transfer termination mode
	   '17'   => 'SATM',          # selected area transfer mode
	   '18'   => 'TSM',           # tabulation stop mode
	   '19'   => 'EBM',           # editing boundary mode
	   '20'   => 'LNM',           # Line Feed / New Line Mode
	                           # DEC Private Modes
	   '?0'   => 'IGN',           # Error (Ignored)
	   '?1'   => 'DECCKM',        # Cursorkeys application (set); Cursorkeys normal (reset)
	   '?2'   => 'DECANM',        # ANSI (set); VT52 (reset)
	   '?3'   => 'DECCOLM',       # 132 columns (set); 80 columns (reset)
	   '?4'   => 'DECSCLM',       # Jump scroll (set); Smooth scroll (reset)
	   '?5'   => 'DECSCNM',       # Reverse screen (set); Normal screen (reset)
	   '?6'   => 'DECOM',         # Sets relative coordinates (set); Sets absolute coordinates (reset)
	   '?7'   => 'DECAWM',        # Auto Wrap
	   '?8'   => 'DECARM',        # Auto Repeat
	   '?9'   => 'DECINLM',       # Interlace
	  '?18'   => 'DECPFF',        # Send FF to printer after print screen (set); No char after PS (reset)
	  '?19'   => 'DECPEX',        # Print screen: prints full screen (set); prints scroll region (reset)
	  '?25'   => 'DECTCEM',       # Cursor on (set); Cursor off (reset)
	) };

	$self->{'_funcs'} = { (     # supported character sequences
	       'BS' => \&_code_BS,    # backspace one column
	       'CR' => \&_code_CR,    # carriage return
	       'DA' => \&_code_DA,    # return ESC [ ? 6 c (VT102)
	       'DL' => \&_code_DL,    # delete lines
	       'ED' => \&_code_ED,    # erase display
	       'EL' => \&_code_EL,    # erase line
	       'FF' => \&_code_LF,    # line feed
	       'HT' => \&_code_HT,    # horizontal tab to next tab stop
	       'IL' => \&_code_IL,    # insert blank lines
	       'LF' => \&_code_LF,    # line feed
	       'PM' => \&_code_PM,    # privacy message (ended by ST)
	       'RI' => \&_code_RI,    # reverse line feed
	       'RM' => \&_code_RM,    # reset mode
	       'SI' => undef,         # activate G0 character set
	       'SM' => \&_code_SM,    # set mode
	       'SO' => undef,         # activate G1 character set & CR
	       'ST' => undef,         # string terminator
	       'VT' => \&_code_LF,    # line feed
	      'APC' => \&_code_APC,   # application program command (ended by ST)
	      'BEL' => \&_code_BEL,   # beep
	      'CAN' => \&_code_CAN,   # interrupt escape sequence
	      'CHA' => \&_code_CHA,   # move cursor to column in current row
	      'CNL' => \&_code_CNL,   # move cursor down and to column 1
	      'CPL' => \&_code_CPL,   # move cursor up and to column 1
	      'CRM' => undef,         # control representation mode
	      'CSI' => \&_code_CSI,   # equivalent to ESC [
	      'CUB' => \&_code_CUB,   # move cursor left
	      'CUD' => \&_code_CUD,   # move cursor down
	      'CUF' => \&_code_CUF,   # move cursor right
	      'CUP' => \&_code_CUP,   # move cursor to row, column
	      'CUU' => \&_code_CUU,   # move cursor up
	      'DCH' => \&_code_DCH,   # delete characters on current line
	      'DCS' => \&_code_DCS,   # device control string (ended by ST)
	      'DEL' => \&_code_IGN,   # ignored
	      'DSR' => \&_code_DSR,   # device status report
	      'EBM' => undef,         # editing boundary mode
	      'ECH' => \&_code_ECH,   # erase characters on current line
	      'ENQ' => undef,         # trigger answerback message
	      'ERM' => undef,         # erasure mode
	      'ESC' => \&_code_ESC,   # start escape sequence
	      'HEM' => undef,         # horizontal editing mode
	      'HPA' => \&_code_CHA,   # move cursor to column in current row
	      'HPR' => \&_code_CUF,   # move cursor right
	      'HTS' => \&_code_HTS,   # set tab stop at current column
	      'HVP' => \&_code_CUP,   # move cursor to row, column
	      'ICH' => \&_code_ICH,   # insert blank characters
	      'IGN' => \&_code_IGN,   # ignored control sequence
	      'IND' => \&_code_LF,    # line feed
	      'IRM' => undef,         # insert/replace mode
	      'KAM' => undef,         # keyboard action mode
	      'LNM' => undef,         # line feed / newline mode
	      'LS2' => undef,         # invoke G2 charset
	      'LS3' => undef,         # invoke G3 charset
	      'NEL' => \&_code_NEL,   # newline
	      'NUL' => \&_code_IGN,   # ignored
	      'OSC' => \&_code_OSC,   # operating system command
	      'PUM' => undef,         # positioning unit mode
	      'RIS' => \&_code_RIS,   # reset
	      'SGR' => \&_code_SGR,   # set graphic rendition
	      'SOS' => undef,         # start of string
	      'SRM' => undef,         # send/receive mode (echo on/off)
	      'SS2' => undef,         # select G2 charset for next char only
	      'SS3' => undef,         # select G3 charset for next char only
	      'SUB' => \&_code_CAN,   # interrupt escape sequence
	      'TBC' => \&_code_TBC,   # clear tab stop (CSI 3 g = clear all stops)
	      'TSM' => undef,         # tabulation stop mode
	      'TTM' => undef,         # transfer termination mode
	      'VEM' => undef,         # vertical editing mode
	      'VPA' => \&_code_VPA,   # move to row (current column)
	      'VPR' => \&_code_CUD,   # move cursor down
	      'XON' => \&_code_XON,   # resume transmission
	     'FEAM' => undef,         # format effector action mode
	     'FETM' => undef,         # format effector transfer mode
	     'GATM' => undef,         # guarded-area transfer mode
	     'LS1R' => undef,         # invoke G1 charset as GR
	     'LS2R' => undef,         # invoke G2 charset as GR
	     'LS3R' => undef,         # invoke G3 charset as GR
	     'MATM' => undef,         # multiple area transfer mode
	     'SATM' => undef,         # selected area transfer mode
	     'SRTM' => undef,         # status-reporting transfer mode
	     'XOFF' => \&_code_XOFF,  # stop transmission, ignore characters
	    'CSDFL' => undef,         # select default charset (ISO646/8859-1)
	    'CUPRS' => \&_code_CUPRS, # restore cursor position
	    'CUPSV' => \&_code_CUPSV, # save cursor position
	    'DECID' => \&_code_DA,    # DEC private ID; return ESC [ ? 6 c (VT102)
	    'DECLL' => undef,         # set keyboard LEDs
	    'DECOM' => undef,         # relative/absolute coordinate mode
	    'DECRC' => \&_code_DECRC, # restore most recently saved state
	    'DECSC' => \&_code_DECSC, # save state (position, charset, attributes)
	    'G0DFL' => undef,         # G0 charset = default mapping (ISO8859-1)
	    'G0GFX' => undef,         # G0 charset = VT100 graphics mapping
	    'G0ROM' => undef,         # G0 charset = null mapping (straight to ROM)
	    'G0TXT' => undef,         # G0 charset = ASCII mapping
	    'G0USR' => undef,         # G0 charset = user defined mapping
	    'G1DFL' => undef,         # G1 charset = default mapping (ISO8859-1)
	    'G1GFX' => undef,         # G1 charset = VT100 graphics mapping
	    'G1ROM' => undef,         # G1 charset = null mapping (straight to ROM)
	    'G1TXT' => undef,         # G1 charset = ASCII mapping
	    'G1USR' => undef,         # G1 charset = user defined mapping
	    'G2DFL' => undef,         # G2 charset = default mapping (ISO8859-1)
	    'G2GFX' => undef,         # G2 charset = VT100 graphics mapping
	    'G2ROM' => undef,         # G2 charset = null mapping (straight to ROM)
	    'G2USR' => undef,         # G2 charset = user defined mapping
	    'G3DFL' => undef,         # G3 charset = default mapping (ISO8859-1)
	    'G3GFX' => undef,         # G3 charset = VT100 graphics mapping
	    'G3ROM' => undef,         # G3 charset = null mapping (straight to ROM)
	    'G3USR' => undef,         # G3 charset = user defined mapping
	   'CSUTF8' => undef,         # select UTF-8 (obsolete)
	   'DECALN' => \&_code_DECALN,# DEC alignment test - fill screen with E's
	   'DECANM' => undef,         # ANSI/VT52 mode
	   'DECARM' => undef,         # auto repeat mode
	   'DECAWM' => undef,         # auto wrap mode
	   'DECCKM' => undef,         # cursor key mode
	   'DECPAM' => undef,         # set application keypad mode
	   'DECPEX' => undef,         # print screen / scrolling region
	   'DECPFF' => undef,         # sent FF after print screen, or not
	   'DECPNM' => undef,         # set numeric keypad mode
	  'DECCOLM' => undef,         # 132 column mode
	  'DECINLM' => undef,         # interlace mode
	  'DECSCLM' => undef,         # jump/smooth scroll mode
	  'DECSCNM' => undef,         # reverse/normal screen mode
	  'DECSTBM' => \&_code_DECSTBM, # set scrolling region
	  'DECTCEM' => \&_code_DECTCEM, # Cursor on (set); Cursor off (reset)
	) };

	$self->{'_callbacks'} = { (   # available callbacks
	  'BELL'              => undef,   # bell character received
	  'CLEAR'             => undef,   # screen cleared
	  'OUTPUT'            => undef,   # data to be sent back to originator
	  'ROWCHANGE'         => undef,   # screen row changed
	  'SCROLL_DOWN'       => undef,   # text about to move up (par=top row)
	  'SCROLL_UP'         => undef,   # text about to move down (par=bott.)
	  'UNKNOWN'           => undef,   # unknown character / sequence
	  'STRING'            => undef,   # string received
	  'XICONNAME'         => undef,   # xterm icon name changed
	  'XWINTITLE'         => undef,   # xterm window title changed
	  'LINEFEED'          => undef,   # line feed about to be processed
	) };

	$self->{'_callbackarg'} = { () }; # stored arguments for callbacks

	$self->{'_decsc'} = [ () ];       # saved state for DECSC/DECRC
	$self->{'_cupsv'} = [ () ];       # saved state for CUPSV/CUPRS
	$self->{'_xon'} = 1;              # state is XON (characters accepted)

	$self->{'cols'} = 80;     # default: 80 columns
	$self->{'rows'} = 24;     # default: 24 rows

	$self->{'_tabstops'} = [];        # tab stops

	$self->{'cols'} = $init{'cols'}
	  if ((defined $init{'cols'}) && ($init{'cols'} > 0));
	$self->{'rows'} = $init{'rows'}
	  if ((defined $init{'rows'}) && ($init{'rows'} > 0));

	bless ($self, $class);

	$self->reset ();

	return $self;
}


# Call a callback function with the given parameters.
#
sub callback_call {
	my ($self, $callback, $par1, $par2) = (@_);
	my ($func, $arg);

	$func = $self->{'_callbacks'}->{$callback};
	return if (not defined $func);

	$arg = $self->{'_callbackarg'}->{$callback};

	&{$func} ($self, $callback, $par1, $par2, $arg);
}


# Set a callback function.
#
sub callback_set {
	my ($self, $callback, $ref, $arg) = (@_);
	$self->{'_callbacks'}->{$callback} = $ref;
	$self->{'_callbackarg'}->{$callback} = $arg;
}


# Reset the terminal to "power-on" values.
#
sub reset {
	my $self = shift;
	my ($a, $b, $i);

	$self->{'x'} = 1;                     # default X position: 1
	$self->{'y'} = 1;                     # default Y position: 1

	$self->{'attr'} = DEFAULT_ATTR_PACKED;

	$self->{'ti'} = '';                   # default: blank window title
	$self->{'ic'} = '';                   # default: blank icon title

	$self->{'srt'} = 1;                   # scrolling region top: row 1
	$self->{'srb'} = $self->{'rows'};     # scrolling region bottom

	$self->{'opts'} = {};                 # blank all options
	$self->{'opts'}->{'LINEWRAP'} = 0;    # line wrapping off
	$self->{'opts'}->{'LFTOCRLF'} = 0;    # don't map LF -> CRLF
	$self->{'opts'}->{'IGNOREXOFF'} = 1;  # ignore XON/XOFF by default

	$self->{'scrt'} = [ () ];             # blank screen text
	$self->{'scra'} = [ () ];             # blank screen attributes

	$a = "\000" x $self->{'cols'};        # set text to NUL
	$b = $self->{'attr'} x $self->{'cols'}; # set attributes to default

	foreach $i (1 .. $self->{'rows'}) {
		($self->{'scrt'}->[$i], $self->{'scra'}->[$i]) = ($a, $b);
	}

	$self->{'_tabstops'} = [];            # reset tab stops
	for ($i = 1; $i < $self->{'cols'}; $i += 8) {
		$self->{'_tabstops'}->[$i] = 1;
	}

	$self->{'_buf'} = undef;              # blank the esc-sequence buffer
	$self->{'_inesc'} = '';               # not in any escape sequence
	$self->{'_xon'} = 1;                  # state is XON (chars accepted)

	$self->{'cursor'} = 1;                # turn cursor on
}


# Resize the terminal.
#
sub resize {
	my $self = shift;
	my $cols = shift;
	my $rows = shift;

	$self->callback_call ('CLEAR', 0, 0);

	$self->{'cols'} = $cols;
	$self->{'rows'} = $rows;

	$self->reset ();
}


# Return the package version.
#
sub version {
	return $VERSION;
}


# Return the current number of columns.
#
sub cols {
	my $self = shift;
	return $self->{'cols'};
}


# Return the current number of rows.
#
sub rows {
	my $self = shift;
	return $self->{'rows'};
}


# Return the current terminal size.
#
sub size {
	my $self = shift;
	return ( $self->{'cols'}, $self->{'rows'} );
}


# Return the current cursor X co-ordinate.
#
sub x {
	my $self = shift;
	return $self->{'x'};
}


# Return the current cursor Y co-ordinate.
#
sub y {
	my $self = shift;
	return $self->{'y'};
}


# Return the current cursor state (1=on, 0=off).
#
sub cursor {
	my $self = shift;
	return $self->{'cursor'};
}


# Return the current xterm title text.
#
sub xtitle {
	my $self = shift;
	return $self->{'ti'};
}


# Return the current xterm icon text.
#
sub xicon {
	my $self = shift;
	return $self->{'ic'};
}


# Return the current terminal status.
#
sub status {
	my $self = shift;

	return (
	  $self->{'x'},               # cursor X position
	  $self->{'y'},               # cursor Y position
	  $self->{'attr'},            # packed attributes
	  $self->{'ti'},              # xterm title text
	  $self->{'ic'}               # xterm icon text
	);
}


# Process the given string, updating the terminal object and calling any
# necessary callbacks on the way.
#
sub process {
	my $self = shift;
	my ($string) = @_;

	return if (not defined $string);

	while (length $string > 0) {
		if (defined $self->{'_buf'}) {        # in escape sequence
			if ($string =~ s/^(.)//s) {
				my $ch = $1;
				if ($ch =~ /[\x00-\x1F]/s) {
					$self->_process_ctl ($ch);
				} else {
					$self->{'_buf'} .= $ch;
					$self->_process_escseq ();
				}
			}
		} else {                              # not in escape sequence
			if ($string =~
			    s/^([^\x00-\x1F\x7F\x9B]+)//s) {
				$self->_process_text ($1);
			} elsif ($string =~ s/^(.)//s) {
				$self->_process_ctl ($1);
			}
		}
	}
}


# Return the current value of the given option, or undef if it doesn't exist.
#
sub option_read {
	my $self = shift;
	my ($option) = @_;

	return undef if (not defined $option);
	return $self->{'opts'}->{$option};
}


# Set the value of the given option to the given value, returning the old
# value or undef if an invalid option was given.
#
sub option_set {
	my $self = shift;
	my ($option, $value) = @_;
	my $prev;

	return undef if (not defined $option);
	return undef if (not defined $value);
	return undef if (not defined $self->{'opts'}->{$option});

	$prev = $self->{'opts'}->{$option};
	$self->{'opts'}->{$option} = $value;
	return $prev;
}


# Return the attributes of the given row, or undef if out of range.
#
sub row_attr {
	my $self = shift;
	my ($row, $startcol, $endcol) = @_;
	my ($data);

	return undef if ($row < 1);
	return undef if ($row > $self->{'rows'});

	$data = $self->{'scra'}->[$row];

	if (defined $startcol && defined $endcol) {
		$data = substr (
		  $data,
		  ($startcol - 1) * 2,
		  (($endcol - $startcol) + 1) * 2
		);
	}

	return $data;
}


# Return the textual contents of the given row, or undef if out of range.
#
sub row_text {
	my $self = shift;
	my ($row, $startcol, $endcol) = @_;
	my $text;

	return undef if ($row < 1);
	return undef if ($row > $self->{'rows'});

	$text = $self->{'scrt'}->[$row];

	if (defined $startcol && defined $endcol) {
		$text = substr (
		  $text,
		  $startcol - 1,
		  ($endcol - $startcol) + 1
		);
	}

	return $text;
}


# Return the textual contents of the given row, or undef if out of range,
# with unused characters represented as a space instead of \0.
#
sub row_plaintext {
	my $self = shift;
	my ($row, $startcol, $endcol) = @_;
	my $text;

	return undef if ($row < 1);
	return undef if ($row > $self->{'rows'});

	$text = $self->{'scrt'}->[$row];
	$text =~ s/\0/ /g;

	if (defined $startcol && defined $endcol) {
		$text = substr (
		  $text,
		  $startcol - 1,
		  ($endcol - $startcol) + 1
		);
	}

	return $text;
}


# Return a set of SGR escape sequences that will change colours and
# attributes from "source" to "dest" (packed attributes).
#
sub sgr_change {
	shift if ref($_[0]);
	my ($source, $dest) = @_;
	my ($out, %off, %on) = ('', (), ());

	$source = DEFAULT_ATTR_PACKED if (not defined $source);
	$dest = DEFAULT_ATTR_PACKED if (not defined $dest);

	return '' if ($source eq $dest);
	return "\e[m" if ($dest eq DEFAULT_ATTR_PACKED);

	my ($sfg, $sbg, $sbo, $sfa, $sst, $sul, $sbl, $srv) = attr_unpack ($source);
	my ($dfg, $dbg, $dbo, $dfa, $dst, $dul, $dbl, $drv) = attr_unpack ($dest);

	if (($sfg != $dfg) || ($sbg != $dbg)) {
		$out .= sprintf ("\e[m\e[3%d;4%dm", $dfg, $dbg);
		($sbo, $sfa, $sst, $sul, $sbl, $srv) = (0, 0, 0, 0, 0, 0);
	}

	if (($sbo > $dbo) || ($sfa > $dfa)) {
		$off{'22'} = 1;
		($sbo, $sfa) = (0, 0);
	}
	$off{'24'} = 1 if ($sul > $dul);
	$off{'25'} = 1 if ($sbl > $dbl);
	$off{'27'} = 1 if ($srv > $drv);

	if (scalar keys %off > 2) {
		$out .= "\e[m";
		($sbo, $sfa, $sst, $sul, $sbl, $srv) = (0, 0, 0, 0, 0, 0);
	} elsif (scalar keys %off > 0) {
		$out .= "\e[" . join (';', keys %off) . "m";
	}

	$on{'1'} = 1 if ($dbo > $sbo);
	$on{'2'} = 1 if (($dfa > $sfa) && !($dbo > $sbo));
	$on{'4'} = 1 if ($dul > $sul);
	$on{'5'} = 1 if ($dbl > $sbl);
	$on{'7'} = 1 if ($drv > $srv);

	$out .= "\e[" . join (';', keys %on) . "m" if (scalar keys %on > 0);

	return $out;
}


# Return the textual contents of the given row, or undef if out of range,
# with unused characters represented as a space instead of \0, and any
# colour or attribute changes expressed by the relevant SGR escape
# sequences.
#
sub row_sgrtext {
	my ($self, $row, $startcol, $endcol) = @_;
	my ($row_text, $row_attr, $text, $char, $attr_cur, $attr_next);

	return undef if ($row < 1);
	return undef if ($row > $self->{'rows'});

	$startcol = 1 if (not defined $startcol);
	$endcol = $self->{'cols'} if (not defined $endcol);

	return undef if (($startcol < 1) || ($startcol > $self->{'cols'}));
	return undef if (($endcol < 1) || ($endcol > $self->{'cols'}));
	return undef if ($endcol < $startcol);

	$row_text = $self->{'scrt'}->[$row];
	$row_attr = $self->{'scra'}->[$row];

	$text = '';
	$attr_cur = DEFAULT_ATTR_PACKED;

	for (; $startcol <= $endcol; $startcol++) {
		$char = substr ($row_text, $startcol - 1, 1);
		$char =~ s/\0/ /g;
		$char = ' ' if ($char !~ /./);
		$attr_next = substr ($row_attr, ($startcol - 1) * 2, 2);
		$text .= $self->sgr_change ($attr_cur, $attr_next) . $char;
		$attr_cur = $attr_next;
	}

	$attr_next = DEFAULT_ATTR_PACKED;
	$text .= $self->sgr_change ($attr_cur, $attr_next);

	return $text;
}


# Process a string of plain text, with no special characters in it.
#
sub _process_text {
	my $self = shift;
	my ($text) = @_;
	my ($width, $segment);

	return if ($self->{'_xon'} == 0);

	$width = ($self->{'cols'} + 1) - $self->{'x'};

	if ($self->{'opts'}->{'LINEWRAP'} == 0) {     # no line wrap - truncate
		return if ($width < 1);
		$text = substr ($text, 0, $width);
		substr (
		  $self->{'scrt'}->[$self->{'y'}], $self->{'x'} - 1,
		  length $text
		) = $text;
		substr (
		  $self->{'scra'}->[$self->{'y'}], 2 * ($self->{'x'} - 1),
		  2 * (length $text)
		) = $self->{'attr'} x (length $text);
		$self->{'x'} += length $text;
		$self->{'x'} = $self->{'cols'}
		  if ($self->{'x'} > $self->{'cols'});
		$self->callback_call ('ROWCHANGE', $self->{'y'}, 0);
		return;
	}

	while (length $text > 0) {                    # line wrapping enabled
		if ($width > 0) {
			$segment = substr ($text, 0, $width, '');
			substr (
			  $self->{'scrt'}->[$self->{'y'}], $self->{'x'} - 1,
			  length $segment
			) = $segment;
			substr (
			  $self->{'scra'}->[$self->{'y'}],
			  2 * ($self->{'x'} - 1),
			  2 * (length $segment)
			) = $self->{'attr'} x (length $segment);
			$self->{'x'} += length $segment;
		} else {
			if ($self->{'x'} > $self->{'cols'}) {       # wrap to next line
				$self->callback_call ('ROWCHANGE', $self->{'y'}, 0);
				$self->callback_call ('LINEFEED', $self->{'y'}, 0);
				$self->{'x'} = 1;
				$self->_move_down;
			}
		}
		$width = ($self->{'cols'} + 1) - $self->{'x'};
	}

	$self->callback_call ('ROWCHANGE', $self->{'y'}, 0);
}


# Process a control character.
#
sub _process_ctl {
	my $self = shift;
	my $ctl = shift;
	my ($name, $func);

	$name = $self->{'_ctlseq'}->{$ctl};
	return if (not defined $name);        # ignore unknown characters

	# If we're in XOFF mode, ignore anything other than XON
	#
	if ($self->{'_xon'} == 0) {
		return if ($name ne 'XON');
	}

	$func = $self->{'_funcs'}->{$name};
	if (not defined $func) {              # do nothing if unsupported
		$self->callback_call ('UNKNOWN', $name, $ctl);
	} else {                              # call handler function
		&{$func} ($self, $name);
	}
}


# Check the escape-sequence buffer, and process it if necessary.
#
sub _process_escseq {
	my $self = shift;
	my ($prefix, $suffix, $func, $name, $dat);
	my @params;

	return if (not defined $self->{'_buf'});
	return if (length $self->{'_buf'} < 1);
	return if ($self->{'_xon'} == 0);

	if ($self->{'_inesc'} eq 'OSC') {                 # in OSC sequence
		if (
		  $self->{'_buf'} =~ /^0;([^\007]*)(?:\007|\033\\)/
		) {                                         # icon & window
			$dat = $1;
			$self->callback_call ('XWINTITLE', $dat, 0);
			$self->callback_call ('XICONNAME', $dat, 0);
			$self->{'ic'} = $dat;
			$self->{'ti'} = $dat;
			$self->{'_buf'} = undef;
			$self->{'_inesc'} = '';
		} elsif (
		  $self->{'_buf'} =~ /^1;([^\007]*)(?:\007|\033\\)/
		) {                                         # set icon name
			$dat = $1;
			$self->callback_call ('XICONNAME', $dat, 0);
			$self->{'ic'} = $dat;
			$self->{'_buf'} = undef;
			$self->{'_inesc'} = '';
		} elsif (
		  $self->{'_buf'} =~ /^2;([^\007]*)(?:\007|\033\\)/
		) {                                         # set window title
			$dat = $1;
			$self->callback_call ('XWINTITLE', $dat, 0);
			$self->{'ti'} = $dat;
			$self->{'_buf'} = undef;
			$self->{'_inesc'} = '';
		} elsif (
		  $self->{'_buf'} =~ /^\d+;([^\007]*)(?:\007|\033\\)/
		) {                                         # unknown OSC
			$self->callback_call (
			  'UNKNOWN', 'OSC', "\033]" . $self->{'_buf'}
			);
			$self->{'_buf'} = undef;
			$self->{'_inesc'} = '';
		} elsif (
		  length $self->{'_buf'} > 1024
		) {                                         # OSC too long
			$self->callback_call (
			  'UNKNOWN', 'OSC', "\033]" . $self->{'_buf'}
			);
			$self->{'_buf'} = undef;
			$self->{'_inesc'} = '';
		}
	} elsif ($self->{'_inesc'} eq 'CSI') {            # in CSI sequence
		foreach $suffix (keys %{$self->{'_csiseq'}}) {
			next if (length $self->{'_buf'} < length $suffix);
			next if (
			  substr (
			    $self->{'_buf'},
			    (length $self->{'_buf'}) - (length $suffix),
			    length $suffix
			  ) ne $suffix
			);
			$self->{'_buf'} = substr (
			  $self->{'_buf'},
			  0,
			  (length $self->{'_buf'}) - (length $suffix)
			);
			$name = $self->{'_csiseq'}->{$suffix};
			$func = $self->{'_funcs'}->{$name};
			if (not defined $func) {        # unsupported sequence
				$self->callback_call (
				  'UNKNOWN',
				  $name,
				  "\033[" . $self->{'_buf'} . $suffix
				);
				$self->{'_buf'} = undef;
				$self->{'_inesc'} = '';
				return;
			}
			@params = split (';', $self->{'_buf'});
			$self->{'_buf'} = undef;
			$self->{'_inesc'} = '';
			&{$func} ($self, @params);
			return;
		}
		if (
		  length $self->{'_buf'} > 64
		) {                           # abort CSI sequence if too long
			$self->callback_call (
			  'UNKNOWN', 'CSI', "\033[" . $self->{'_buf'}
			);
			$self->{'_buf'} = undef;
			$self->{'_inesc'} = '';
		}
	} elsif ($self->{'_inesc'} =~ /_ST$/) {
		if ($self->{'_buf'} =~ s/\033\\$//) {
			$self->{'_inesc'} =~ s/_ST$//;
			$self->callback_call (
			  'STRING', $self->{'_inesc'}, $self->{'_buf'}
			);
			$self->{'_buf'} = undef;
			$self->{'_inesc'} = '';
			$self->{'_buf'} = undef;
			$self->{'_inesc'} = '';
		} elsif (
		  length $self->{'_buf'} > 1024
		) {                                         # string too long
			$self->{'_inesc'} =~ s/_ST$//;
			$self->callback_call (
			  'STRING', $self->{'_inesc'}, $self->{'_buf'}
			);
			$self->{'_buf'} = undef;
			$self->{'_inesc'} = '';
		}
	} else {                                      # in ESC sequence
		foreach $prefix (
		  keys %{$self->{'_escseq'}}
		) {
			next if (
			  substr ($self->{'_buf'}, 0, length $prefix)
			  ne $prefix
			);
			$name = $self->{'_escseq'}->{$prefix};
			$func = $self->{'_funcs'}->{$name};
			if (not defined $func) {        # unsupported sequence
				$self->callback_call (
				  'UNKNOWN',
				  $name,
				  "\033" . $self->{'_buf'}
				);
				$self->{'_buf'} = undef;
				$self->{'_inesc'} = '';
				return;
			}
			$self->{'_buf'} = undef;
			$self->{'_inesc'} = '';
			&{$func} ($self);
			return;
		}
		if (
		  length $self->{'_buf'} > 8
		) {                           # abort ESC sequence if too long
			$self->callback_call (
			  'UNKNOWN',
			  'ESC',
			  "\033" . $self->{'_buf'}
			);
			$self->{'_buf'} = undef;
			$self->{'_inesc'} = '';
		}
	}
}


# Scroll the scrolling region up such that the text in the scrolling region
# moves down, by the given number of lines.
#
sub _scroll_up {
	my $self = shift;
	my $lines = shift;
	my ($attr, $a, $b, $i);

	return if ($lines < 1);

	$self->callback_call ('SCROLL_UP', $self->{'srb'}, $lines);

	for ($i = $self->{'srb'}; $i >= ($self->{'srt'} + $lines); $i --) {
		$self->{'scrt'}->[$i] = $self->{'scrt'}->[$i - $lines];
		$self->{'scra'}->[$i] = $self->{'scra'}->[$i - $lines];
	}

	$a = "\000" x $self->{'cols'};           # set text to NUL
	$attr = DEFAULT_ATTR_PACKED;
	$b = $attr x $self->{'cols'};            # set attributes to default

	for (
	  $i = $self->{'srt'};
	  ($i <= $self->{'srb'}) && ($i < ($self->{'srt'} + $lines));
	  $i ++
	) {
		$self->{'scrt'}->[$i] = $a;      # blank new lines
		$self->{'scra'}->[$i] = $b;      # wipe attributes of new lines
	}
}


# Scroll the scrolling region down such that the text in the scrolling region
# moves up, by the given number of lines.
#
sub _scroll_down {
	my $self = shift;
	my $lines = shift;
	my ($a, $b, $i, $attr);

	$self->callback_call ('SCROLL_DOWN', $self->{'srt'}, $lines);

	for ($i = $self->{'srt'}; $i <= ($self->{'srb'} - $lines); $i ++) {
		$self->{'scrt'}->[$i] = $self->{'scrt'}->[$i + $lines];
		$self->{'scra'}->[$i] = $self->{'scra'}->[$i + $lines];
	}

	$a = "\000" x $self->{'cols'};           # set text to NUL
	$attr = DEFAULT_ATTR_PACKED;
	$b = $attr x $self->{'cols'};            # set attributes to default

	for (
	  $i = $self->{'srb'};
	  ($i >= $self->{'srt'}) && ($i > ($self->{'srb'} - $lines));
	  $i --
	) {
		$self->{'scrt'}->[$i] = $a;      # blank new lines
		$self->{'scra'}->[$i] = $b;      # wipe attributes of new lines
	}
}


# Move the cursor up the given number of lines, without triggering a GOTO callback, taking scrolling into account.
#
sub _move_up {
	my $self = shift;
	my $num = shift;
	$num = 1 if (not defined $num);
	$num = 1 if ($num < 1);
	$self->{'y'} -= $num;
	return if ($self->{'y'} >= $self->{'srt'});
	$self->_scroll_up ($self->{'srt'} - $self->{'y'});            # scroll
	$self->{'y'} = $self->{'srt'};
}


# Move the cursor down the given number of lines, without triggering a GOTO
# callback, taking scrolling into account.
#
sub _move_down {
	my $self = shift;
	my $num = shift;
	$num = 1 if (not defined $num);
	$num = 1 if ($num < 1);
	$self->{'y'} += $num;
	return if ($self->{'y'} <= $self->{'srb'});
	$self->_scroll_down ($self->{'y'} - $self->{'srb'});          # scroll
	$self->{'y'} = $self->{'srb'};
}


sub _code_BEL {                         # beep
	my $self = shift;
	if ((defined $self->{'_buf'}) && ($self->{'_inesc'} eq 'OSC')) {
		# CSI OSC can be terminated with a BEL
		$self->{'_buf'} .= "\007";
		$self->_process_escseq ();
	} else {
		$self->callback_call ('BELL', 0, 0);
	}
}

sub _code_BS {                          # move left 1 character
	my $self = shift;
	$self->{'x'} --;
	$self->{'x'} = 1 if ($self->{'x'} < 1);
}

sub _code_CAN {                         # cancel escape sequence
	my $self = shift;
	$self->{'_inesc'} = '';
	$self->{'_buf'} = undef;
}

sub _code_TBC {                         # clear tab stop (CSI 3 g = clear all stops)
	my $self = shift;
	my $num = shift;
	if ((defined $num) && ($num eq '3')) {
		$self->{'_tabstops'} = [];
	} else {
		$self->{'_tabstops'}->[$self->{'x'}] = undef;
	}
}

sub _code_CHA {                         # move to column in current row
	my $self = shift;
	my $col = shift;
	$col = 1 if (not defined $col);
	return if ($self->{'x'} == $col);
	$self->callback_call ('GOTO', $col, $self->{'y'});
	$self->{'x'} = $col;
	$self->{'x'} = 1 if ($self->{'x'} < 1);
	$self->{'x'} = $self->{'cols'} if ($self->{'x'} > $self->{'cols'});
}

sub _code_CNL {                         # move cursor down and to column 1
	my $self = shift;
	my $num = shift;
	$num = 1 if (not defined $num);
	$self->callback_call ('GOTO', 1, $self->{'y'} + $num);
	$self->{'x'} = 1;
	$self->_move_down ($num);
}

sub _code_CPL {                         # move cursor up and to column 1
	my $self = shift;
	my $num = shift;
	$num = 1 if (not defined $num);
	$self->callback_call ('GOTO', $self->{'x'}, $self->{'y'} - $num);
	$self->{'x'} = 1;
	$self->_move_up ($num);
}

sub _code_CR {                          # carriage return
	my $self = shift;
	$self->{'x'} = 1;
}

sub _code_CSI {                         # ESC [
	my $self = shift;
	$self->{'_buf'} = '';                         # restart ESC buffering
	$self->{'_inesc'} = 'CSI';                    # ...for a CSI, not an ESC
}

sub _code_CUB {                         # move cursor left
	my $self = shift;
	my $num = shift;
	$num = 1 if (not defined $num);
	$num = 1 if ($num < 1);
	$self->callback_call ('GOTO', $self->{'x'} - $num, $self->{'y'});
	$self->{'x'} -= $num;
	$self->{'x'} = 1 if ($self->{'x'} < 1);
}

sub _code_CUD {                         # move cursor down
	my $self = shift;
	my $num = shift;
	$num = 1 if (not defined $num);
	$num = 1 if ($num < 1);
	$self->callback_call ('GOTO', $self->{'x'}, $self->{'y'} + $num);
	$self->_move_down ($num);
}

sub _code_CUF {                         # move cursor right
	my $self = shift;
	my $num = shift;
	$num = 1 if (not defined $num);
	$num = 1 if ($num < 1);
	$self->callback_call ('GOTO', $self->{'x'} + $num, $self->{'y'});
	$self->{'x'} += $num;
	$self->{'x'} = $self->{'cols'} if ($self->{'x'} > $self->{'cols'});
}

sub _code_CUP {                         # move cursor to row, column
	my $self = shift;
	my ($row, $col) = (@_);
	$row = 1 if (not defined $row);
	$col = 1 if (not defined $col);
	$row = 1 if ($row < 1);
	$col = 1 if ($col < 1);
	$row = $self->{'rows'} if ($row > $self->{'rows'});
	$col = $self->{'cols'} if ($col > $self->{'cols'});
	$self->callback_call ('GOTO', $col, $row);
	$self->{'x'} = $col;
	$self->{'y'} = $row;
}

sub _code_RI {                          # reverse line feed
	my $self = shift;
	$self->callback_call ('GOTO', $self->{'x'}, $self->{'y'} - 1);
	$self->_move_up;
}

sub _code_CUU {                         # move cursor up
	my $self = shift;
	my $num = shift;
	$num = 1 if (not defined $num);
	$num = 1 if ($num < 1);
	$self->callback_call ('GOTO', $self->{'x'}, $self->{'y'} - $num);
	$self->_move_up ($num);
}

sub _code_DA {                          # return ESC [ ? 6 c (VT102)
	my $self = shift;
	$self->callback_call ('OUTPUT', "\033[?6c", 0);
}

sub _code_DCH {                         # delete characters on current line
	my $self = shift;
	my $num = shift;
	my ($width, $todel, $line, $lsub, $rsub, $attr);

	$num = 1 if (not defined $num);
	$num = 1 if ($num < 1);

	$width = $self->{'cols'} + 1 - $self->{'x'};
	$todel = $num;
	$todel = $width if ($todel > $width);

	$line = $self->{'scrt'}->[$self->{'y'}];
	($lsub, $rsub) = ("", "");
	$lsub = substr ($line, 0, $self->{'x'} - 1) if ($self->{'x'} > 1);
	$rsub = substr ($line, $self->{'x'} - 1 + $todel);
	$self->{'scrt'}->[$self->{'y'}] = $lsub . $rsub . ("\0" x $todel);

	$attr = DEFAULT_ATTR_PACKED;
	$line = $self->{'scra'}->[$self->{'y'}];
	($lsub, $rsub) = ("", "");
	$lsub = substr ($line, 0, 2 * ($self->{'x'} - 1)) if ($self->{'x'} > 1);
	$rsub = substr ($line, 2 * ($self->{'x'} - 1 + $todel));
	$self->{'scra'}->[$self->{'y'}] = $lsub . $rsub . ($attr x $todel);

	$self->callback_call ('ROWCHANGE', $self->{'y'}, 0);
}

sub _code_DCS {                         # device control string (ignored)
	my $self = shift;
	$self->{'_buf'} = '';
	$self->{'_inesc'} = 'DCS_ST';
}

sub _code_DECSTBM {                     # set scrolling region
	my $self = shift;
	my ($top, $bottom) = (@_);
	$top = 1 if (not defined $top);
	$bottom = $self->{'rows'} if (not defined $bottom);
	$top = 1 if ($top < 1);
	$bottom = 1 if ($bottom < 1);
	$top = $self->{'rows'} if ($top > $self->{'rows'});
	$bottom = $self->{'rows'} if ($bottom > $self->{'rows'});
	if ($bottom < $top) {
		my $a = $bottom;
		$bottom = $top;
		$top = $a;
	}
	$self->{'srt'} = $top;
	$self->{'srb'} = $bottom;
}

sub _code_DECTCEM {                     # Cursor on (set); Cursor off (reset)
	my $self = shift;
	$self->{'cursor'} = shift;
}

sub _code_IGN {                         # ignored control sequence
}

sub _code_DL {                          # delete lines
	my $self = shift;
	my $lines = shift;
	my ($attr, $scrb, $row);

	$lines = 1 if (not defined $lines);
	$lines = 1 if ($lines < 1);

	$attr = DEFAULT_ATTR_PACKED;

	$scrb = $self->{'srb'};
	$scrb = $self->{'rows'} if ($self->{'y'} > $self->{'srb'});
	$scrb = $self->{'srt'} - 1 if ($self->{'y'} < $self->{'srt'});

	for ($row = $self->{'y'}; $row <= ($scrb - $lines); $row ++) {
		$self->{'scrt'}->[$row] = $self->{'scrt'}->[$row + $lines];
		$self->{'scra'}->[$row] = $self->{'scra'}->[$row + $lines];
		$self->callback_call ('ROWCHANGE', $row, 0);
	}

	for (
	  $row = $scrb;
	  ($row > ($scrb - $lines)) && ($row >= ($self->{'y'}));
	  $row --
	) {
		$self->{'scrt'}->[$row] = "\000" x $self->{'cols'};
		$self->{'scra'}->[$row] = $attr x $self->{'cols'};
		$self->callback_call ('ROWCHANGE', $row, 0);
	}
}

sub _code_DSR {                         # device status report
	my $self = shift;
	my $num = shift;
	$num = 5 if (not defined $num);
	if ($num == 6) {                        # CPR - cursor position report
		$self->callback_call (
		  'OUTPUT', "\e[" . $self->{'y'} . ";" . $self->{'x'} . "R", 0
		);
	} elsif ($num == 5) {                   # DSR - reply ESC [ 0 n
		$self->callback_call ('OUTPUT', "\e[0n", 0);
	}
}

sub _code_ECH {                         # erase characters on current line
	my $self = shift;
	my $num = shift;
	my ($width, $todel, $line, $lsub, $rsub, $attr);

	$num = 1 if (not defined $num);
	$num = 1 if ($num < 1);

	$width = $self->{'cols'} + 1 - $self->{'x'};
	$todel = $num;
	$todel = $width if ($todel > $width);

	$line = $self->{'scrt'}->[$self->{'y'}];
	($lsub, $rsub) = ("", "");
	$lsub = substr ($line, 0, $self->{'x'} - 1) if ($self->{'x'} > 1);
	$rsub = substr ($line, $self->{'x'} - 1 + $todel);
	$self->{'scrt'}->[$self->{'y'}] = $lsub . ("\0" x $todel) . $rsub;

	$attr = DEFAULT_ATTR_PACKED;


	$line = $self->{'scra'}->[$self->{'y'}];
	($lsub, $rsub) = ("", "");
	$lsub = substr ($line, 0, 2 * ($self->{'x'} - 1)) if ($self->{'x'} > 1);
	$rsub = substr ($line, 2 * ($self->{'x'} - 1 + $todel));
	$self->{'scra'}->[$self->{'y'}] = $lsub . ($attr x $todel) . $rsub;

	$self->callback_call ('ROWCHANGE', $self->{'y'}, 0);
}

sub _code_ED {                          # erase display
	my $self = shift;
	my $num = shift;
	my ($row, $attr);

	$num = 0 if (not defined $num);

	$attr = DEFAULT_ATTR_PACKED;

	# Wipe-cursor-to-end is the same as clear-whole-screen if cursor at top left
	#
	$num = 2 if (($num == 0) && ($self->{'x'} == 1) && ($self->{'y'} == 1));

	if ($num == 0) {                              # 0 = cursor to end
		$self->{'scrt'}->[$self->{'y'}] =
		  substr (
		    $self->{'scrt'}->[$self->{'y'}],
		    0,
		    $self->{'x'} - 1
		  ) . ("\0" x ($self->{'cols'} + 1 - $self->{'x'}));
		$self->{'scra'}->[$self->{'y'}] =
		  substr (
		    $self->{'scra'}->[$self->{'y'}],
		    0,
		    2 * ($self->{'x'} - 1)
		  ) . ($attr x ($self->{'cols'} + 1 - $self->{'x'}));
		$self->callback_call ('ROWCHANGE', $self->{'y'}, 0);
		for (
		  $row = $self->{'y'} + 1;
		  $row <= $self->{'rows'};
		  $row ++
		) {
			$self->{'scrt'}->[$row] = "\0" x $self->{'cols'};
			$self->{'scra'}->[$row] = $attr x $self->{'cols'};
			$self->callback_call ('ROWCHANGE', $row, 0);
		}
	} elsif ($num == 1) {                         # 1 = start to cursor
		for (
		  $row = 1;
		  $row < $self->{'y'};
		  $row ++
		) {
			$self->{'scrt'}->[$row] = "\0" x $self->{'cols'};
			$self->{'scra'}->[$row] = $attr x $self->{'cols'};
			$self->callback_call ('ROWCHANGE', $row, 0);
		}
		$self->{'scrt'}->[$self->{'y'}] =
		  ("\0" x $self->{'x'}) .
		  substr ($self->{'scrt'}->[$self->{'y'}], $self->{'x'});
		$self->{'scra'}->[$self->{'y'}] =
		  ($attr x $self->{'x'}) .
		  substr ($self->{'scra'}->[$self->{'y'}], 2 * $self->{'x'});
		$self->callback_call ('ROWCHANGE', $self->{'y'}, 0);
	} else {                                      # 2 = whole display
		$self->callback_call ('CLEAR', 0, 0);
		for ($row = 1; $row <= $self->{'rows'}; $row ++) {
			$self->{'scrt'}->[$row] = "\0" x $self->{'cols'};
			$self->{'scra'}->[$row] = $attr x $self->{'cols'};
		}
	}
}

sub _code_EL {                          # erase line
	my $self = shift;
	my $num = shift;
	my $attr;

	$num = 0 if (not defined $num);

	$attr = DEFAULT_ATTR_PACKED;

	if ($num == 0) {                            # 0 = cursor to end of line
		$self->{'scrt'}->[$self->{'y'}] =
		  substr (
		    $self->{'scrt'}->[$self->{'y'}],
		    0,
		    $self->{'x'} - 1
		  ) . ("\0" x ($self->{'cols'} + 1 - $self->{'x'}));
		$self->{'scra'}->[$self->{'y'}] =
		  substr (
		    $self->{'scra'}->[$self->{'y'}],
		    0,
		    2 * ($self->{'x'} - 1)
		  ) . ($attr x ($self->{'cols'} + 1 - $self->{'x'}));
		$self->callback_call ('ROWCHANGE', $self->{'y'}, 0);
	} elsif ($num == 1) {                       # 1 = start of line to cursor
		$self->{'scrt'}->[$self->{'y'}] =
		  ("\0" x $self->{'x'}) .
		  substr ($self->{'scrt'}->[$self->{'y'}], $self->{'x'});
		$self->{'scra'}->[$self->{'y'}] =
		  ($attr x $self->{'x'}) .
		  substr ($self->{'scra'}->[$self->{'y'}], 2 * $self->{'x'});
		$self->callback_call ('ROWCHANGE', $self->{'y'}, 0);
	} else {                                      # 2 = whole line
		$self->{'scrt'}->[$self->{'y'}] = "\0" x $self->{'cols'};
		$self->{'scra'}->[$self->{'y'}] = $attr x $self->{'cols'};
		$self->callback_call ('ROWCHANGE', $self->{'y'}, 0);
	}
}

sub _code_ESC {                         # start escape sequence
	my $self = shift;
	if ((defined $self->{'_buf'}) && ($self->{'_inesc'} =~ /OSC|_ST/)) {
		# Some sequences are terminated with an ST
		$self->{'_buf'} .= "\033";
		$self->_process_escseq ();
		return;
	}
	$self->{'_buf'} = '';                         # set ESC buffer
	$self->{'_inesc'} = 'ESC';                    # ...for ESC, not CSI
}

sub _code_LF {                          # line feed
	my $self = shift;
	$self->_code_CR ()                            # cursor to start of line
	  if ($self->{'opts'}->{'LFTOCRLF'} != 0);
	$self->callback_call ('LINEFEED', $self->{'y'}, 0);
	$self->_move_down ();
}

sub _code_NEL {                         # newline
	my $self = shift;
	$self->_code_CR ();                           # cursor always to start
	$self->_code_LF ();                           # standard line feed
}

sub _code_HT {                          # horizontal tab to next tab stop
	my $self = shift;
	my ($newx, $spaces, $width);

	if (
	  ($self->{'opts'}->{'LINEWRAP'} != 0)
	  && ($self->{'x'} >= $self->{'cols'})
	) {
		$self->callback_call ('LINEFEED', $self->{'y'}, 0);
		$self->{'x'} = 1;
		$self->_move_down;
	}

	$newx = $self->{'x'} + 1;
	while ($newx < $self->{'cols'} && not $self->{'_tabstops'}->[$newx]) {
		$newx++;
	}

	$width = ($self->{'cols'} + 1) - $self->{'x'};
	$spaces = $newx - $self->{'x'};
	$spaces = $width + 1 if ($spaces > $width);

	if ($spaces > 0) {
		$self->{'x'} += $spaces;
		$self->{'x'} = $self->{'cols'} if ($self->{'x'} > $self->{'cols'});
	}
}

sub _code_HTS {                         # set tab stop at current column
	my $self = shift;
	$self->{'_tabstops'}->[$self->{'x'}] = 1;
}

sub _code_ICH {                         # insert blank characters
	my $self = shift;
	my $num = shift;
	my ($width, $toins, $line, $lsub, $rsub, $attr);

	$num = 1 if (not defined $num);
	$num = 1 if ($num < 1);

	$width = $self->{'cols'} + 1 - $self->{'x'};
	$toins = $num;
	$toins = $width if ($toins > $width);

	$line = $self->{'scrt'}->[$self->{'y'}];
	($lsub, $rsub) = ("", "");
	$lsub = substr ($line, 0, $self->{'x'} - 1) if ($self->{'x'} > 1);
	$rsub = substr ($line, $self->{'x'} - 1, $width - $toins);
	$self->{'scrt'}->[$self->{'y'}] = $lsub . ("\0" x $toins) . $rsub;

	$attr = DEFAULT_ATTR_PACKED;
	$line = $self->{'scra'}->[$self->{'y'}];
	($lsub, $rsub) = ("", "");
	$lsub = substr ($line, 0, 2 * ($self->{'x'} - 1)) if ($self->{'x'} > 1);
	$rsub = substr ($line, 2 * ($self->{'x'} - 1), 2 * ($width - $toins));
	$self->{'scra'}->[$self->{'y'}] = $lsub . ($attr x $toins) . $rsub;

	$self->callback_call ('ROWCHANGE', $self->{'y'}, 0);
}

sub _code_IL {                          # insert blank lines
	my $self = shift;
	my $lines = shift;
	my ($attr, $scrb, $row);

	$lines = 1 if (not defined $lines);
	$lines = 1 if ($lines < 1);

	$attr = DEFAULT_ATTR_PACKED;

	$scrb = $self->{'srb'};
	$scrb = $self->{'rows'} if ($self->{'y'} > $self->{'srb'});
	$scrb = $self->{'srt'} - 1 if ($self->{'y'} < $self->{'srt'});

	for ($row = $scrb; $row >= ($self->{'y'} + $lines); $row --) {
		$self->{'scrt'}->[$row] = $self->{'scrt'}->[$row - $lines];
		$self->{'scra'}->[$row] = $self->{'scra'}->[$row - $lines];
		$self->callback_call ('ROWCHANGE', $row, 0);
	}

	for (
	  $row = $self->{'y'};
	  ($row <= $scrb) && ($row < ($self->{'y'} + $lines));
	  $row ++
	) {
		$self->{'scrt'}->[$row] = "\000" x $self->{'cols'};
		$self->{'scra'}->[$row] = $attr x $self->{'cols'};
		$self->callback_call ('ROWCHANGE', $row, 0);
	}
}

sub _code_PM {                          # privacy message (ignored)
	my $self = shift;
	$self->{'_buf'} = '';
	$self->{'_inesc'} = 'PM_ST';
}

sub _code_APC {                         # application program command (ignored)
	my $self = shift;
	$self->{'_buf'} = '';
	$self->{'_inesc'} = 'APC_ST';
}

sub _code_OSC {                         # operating system command
	my $self = shift;
	$self->{'_buf'} = '';                         # restart buffering
	$self->{'_inesc'} = 'OSC';                    # ...for OSC, not ESC or CSI
}

sub _code_RIS {                         # reset
	my $self = shift;
	$self->reset ();
}

sub _toggle_mode {                      # set/reset modes
	my $self = shift;
	my ($flag, @modes) = @_;

	foreach my $mode (@modes) {
		my $name = $self->{'_modeseq'}->{$mode};
		my $func = undef;
		$func = $self->{'_funcs'}->{$name} if (defined $name);
		if (not defined $func) {                # unsupported seq.
			$self->callback_call (
			  'UNKNOWN',
			  $name,
			  "\033[${mode}" . ($flag ? "h" : "l")
			);
			$self->{'_buf'} = undef;
			$self->{'_inesc'} = '';
			return;
		}
		$self->{'_buf'} = undef;
		$self->{'_inesc'} = '';
		&{$func} ($self, $flag);
		return;
	}
}

sub _code_RM {                          # reset mode
	my $self = shift;
	$self->_toggle_mode(0, @_);
}

sub _code_SM {                          # set mode
	my $self = shift;
	$self->_toggle_mode(1, @_);
}

sub _code_SGR {                         # set graphic rendition
	my $self = shift;
	my (@parms) = (@_);
	my ($val, $fg, $bg, $bo, $fa, $st, $ul, $bl, $rv);

	($fg, $bg, $bo, $fa, $st, $ul, $bl, $rv) =
	  $self->attr_unpack ($self->{'attr'});

	@parms = (0) if ($#parms < 0);                # ESC [ m = ESC [ 0 m

	while (defined ($val = shift @parms)) {
		if ($val == 0) {                     # reset all attributes
			($fg, $bg, $bo, $fa, $st, $ul, $bl, $rv) = DEFAULT_ATTR;
		} elsif ($val == 1) {                # bold ON
			($bo, $fa) = (1, 0);
		} elsif ($val == 2) {                # faint ON
			($bo, $fa) = (0, 1);
		} elsif ($val == 4) {                # underline ON
			$ul = 1;
		} elsif ($val == 5) {                # blink ON
			$bl = 1;
		} elsif ($val == 7) {                # reverse video ON
			$rv = 1;
		} elsif ($val == 21) {               # normal intensity
			($bo, $fa) = (0, 0);
		} elsif ($val == 22) {               # normal intensity
			($bo, $fa) = (0, 0);
		} elsif ($val == 24) {               # underline OFF
			$ul = 0;
		} elsif ($val == 25) {               # blink OFF
			$bl = 0;
		} elsif ($val == 27) {               # reverse video OFF
			$rv = 0;
		} elsif (($val >= 30) && ($val <= 37)) {# set foreground colour
			$fg = $val - 30;
		} elsif ($val == 38) {               # underline on, default fg
			($ul, $fg) = (1, 7);
		} elsif ($val == 39) {               # underline off, default fg
			($ul, $fg) = (0, 7);
		} elsif (($val >= 40) && ($val <= 47)) {# set background colour
			$bg = $val - 40;
		} elsif ($val == 49) {               # default background
			$bg = 0;
		}
	}

	$self->{'attr'} = $self->attr_pack ($fg, $bg, $bo, $fa, $st, $ul, $bl, $rv);
}

sub _code_VPA {                         # move to row (current column)
	my $self = shift;
	my $row = shift;
	$row = 1 if (not defined $row);
	return if ($self->{'y'} == $row);
	$self->{'y'} = $row;
	$self->{'y'} = 1 if ($self->{'y'} < 1);
	$self->{'y'} = $self->{'rows'} if ($self->{'y'} > $self->{'rows'});
}

sub _code_DECALN {                      # fill screen with E's
	my $self = shift;
	my ($row, $attr);

	$attr = DEFAULT_ATTR_PACKED;

	for ($row = 1; $row <= $self->{'rows'}; $row ++) {
		$self->{'scrt'}->[$row] = 'E' x $self->{'cols'};
		$self->{'scra'}->[$row] = $attr x $self->{'cols'};
		$self->callback_call ('ROWCHANGE', $self->{'y'}, 0);
	}

	$self->{'x'} = 1;
	$self->{'y'} = 1;
}

sub _code_DECSC {                       # save state
	my $self = shift;
	my @state;

	@state = @{$self->{'_decsc'}};
	push (
	  @state,
	  [
	    $self->{'x'},
	    $self->{'y'},
	    $self->{'attr'},
	    $self->{'ti'},
	    $self->{'ic'},
	    $self->{'cursor'}
	  ]
	);
	$self->{'_decsc'} = [ @state ];
}

sub _code_DECRC {                       # restore most recently saved state
	my $self = shift;
	my @state;
	my $ref;

	@state = @{$self->{'_decsc'}};
	return if ($#state < 0);

	$ref = pop @state;

	(
	  $self->{'x'},
	  $self->{'y'},
	  $self->{'attr'},
	  $self->{'ti'},
	  $self->{'ic'},
	  $self->{'cursor'}
	) = @$ref;

	$self->{'_decsc'} = [ @state ];
}

sub _code_CUPSV {                       # save cursor position
	my $self = shift;
	my @state;

	@state = @{$self->{'_cupsv'}};
	push (
	  @state,
	  [
	    $self->{'x'},
	    $self->{'y'}
	  ]
	);
	$self->{'_cupsv'} = [ @state ];
}

sub _code_CUPRS {                       # restore cursor position
	my $self = shift;
	my @state;
	my $ref;

	@state = @{$self->{'_cupsv'}};
	return if ($#state < 0);

	$ref = pop @state;

	(
	  $self->{'x'},
	  $self->{'y'}
	) = @$ref;

	$self->{'_cupsv'} = [ @state ];
}

sub _code_XON {                         # resume character processing
	my $self = shift;
	$self->{'_xon'} = 1;
}

sub _code_XOFF {                        # stop character processing
	my $self = shift;
	return if ($self->{'opts'}->{'IGNOREXOFF'});
	$self->{'_xon'} = 0;
}

1;
__END__

=head1 NAME

Term::VT102 - a class to emulate a DEC VT102 terminal

=head1 SYNOPSIS

  use Term::VT102;

  my $vt = Term::VT102->new ('cols' => 80, 'rows' => 24);
  while (<>) { $vt->process ($_); }

=head1 DESCRIPTION

The VT102 class provides emulation of most of the functions of a DEC VT102
terminal.  Once initialised, data passed to a VT102 object is processed and the
in-memory "screen" modified accordingly.  This "screen" can be interrogated by
the external program in a variety of ways.

This allows your program to interface with full-screen console programs by
running them in a subprocess and passing their output to a VT102 class.  You
can then see what the application has written on the screen by querying the
class appropriately.

=head1 OPTIONS

Setting B<cols> or B<rows> in the B<new()> hash allows you to change
the size of the terminal being emulated.  If you do not specify a size, the
default is 80 columns by 24 rows.

After initialisation, you can read and set the following terminal options
using the B<option_read()> and B<option_set()> methods:

  LINEWRAP      line wrapping; 1=on, 0=off. Default is OFF.
  LFTOCRLF      treat LF (\n) as CRLF (\r\n); 1=on, 0=off. Default OFF.
  IGNOREXOFF    ignore XON/XOFF characters; 1=on (ignore). Default ON.

=head1 METHODS

The following methods are provided:

=over 4

=item B<attr_pack> (I<$fg>,I<$bg>,I<$bo>,I<$fa>,I<$st>,I<$ul>,I<$bl>,I<$rv>)

Returns the packed version of the given attribute settings, which are given in
the same order as returned by B<attr_unpack>.  The packed version will be a
binary string not longer than 2 bytes.

=item B<attr_unpack> (I<$data>)

Returns a list of the contents of the given packed attribute settings, of the
form (I<$fg>,I<$bg>,I<$bo>,I<$fa>,I<$st>,I<$ul>,I<$bl>,I<$rv>).

I<$fg> and I<$bg> are the ANSI foreground and background text colours, and
I<$bo>, I<$fa>, I<$st>, I<$ul>, I<$bl>, and I<$rv> are flags (1 = on,
0 = off) for bold, faint, standout, underline, blink and reverse respectively.

=item B<callback_call> (I<$name>, I<$par1>, I<$par2>)

Calls the callback I<$name> (eg B<'ROWCHANGE'>) with parameters
I<$par1> and I<$par2>, as if the VT102 module had called it.
Does nothing if that callback has not been set with
B<callback_set ()>.

=item B<callback_set> (I<$callback>, I<$ref>, I<$private>)

Sets the callback I<callback> to function reference I<ref> with
private data I<$private>.

See the section on B<CALLBACKS> below.

=item B<new> (I<%config>)

Returns a new VT102 object with options specified in I<%config> (see the
B<OPTIONS> section for details).

=item B<option_read> (I<$option>)

Returns the current value of terminal option I<$option> (see B<OPTIONS> for
details), or I<undef> if that option does not exist.  Note that you cannot
read the terminal size with this call; use B<size()> for that.

=item B<option_set> (I<$option>, I<$value>)

Sets the current value of terminal option I<$option> to I<$value>, returning
the old value or I<undef> if no such terminal option exists or you have
specified an undefined I<$value>.  Note that you cannot resize the terminal
with this call; use B<resize()> for that.

=item B<process> (I<$string>)

Processes the string I<$string> (which can be zero-length), updating the
VT102 object accordingly and calling any necessary callbacks on the way.

=item B<resize> (I<$cols>, I<$rows>)

Resizes the VT102 terminal to I<cols> columns by I<rows> rows,
eg B<$vt->>B<resize (80, 24)>.  The virtual screen is cleared first.

=item B<reset> ()

Resets the object to its "power-on" state.

=item B<row_attr> (I<$row>, [I<$startcol>, I<$endcol>])

Returns the attributes for row I<$row> (or I<undef> if out of range) as
a string of packed attributes, each character cell's attributes being 2
bytes long.  To unpack the attributes for a given cell, use B<substr()>,
eg B<$attr=substr($row,4,2)> would set I<$attr> to the attributes for cell
3 (steps of 2: 0 .. 2 .. 4, so 4 means the 3rd character).  You would then
use the B<attr_unpack()> method to unpack that character cell's attributes.

If I<$startcol> and I<$endcol> are defined, only returns the part of the row
between columns I<$startcol> and I<$endcol> inclusive instead of the whole row.

=item B<row_text> (I<$row>, [I<$startcol>, I<$endcol>])

Returns the textual contents of row I<$row> (or I<undef> if out of range), with
totally unused characters being represented as NULL (\0).  If I<$startcol> and
I<$endcol> are defined, only returns the part of the row between columns
I<$startcol> and I<$endcol> inclusive instead of the whole row.

=item B<row_plaintext> (I<$row>, [I<$startcol>, I<$endcol>])

Returns the textual contents of row I<$row> (or I<undef> if out of range),
with unused characters being represented as spaces.  If I<$startcol> and
I<$endcol> are defined, only returns the part of the row between columns
I<$startcol> and I<$endcol> inclusive instead of the whole row.

=item B<row_sgrtext> (I<$row>, [I<$startcol>, I<$endcol>])

Returns the textual contents of row I<$row> (or I<undef> if out of range),
with unused characters being represented as spaces, and ANSI/ECMA-48 escape
sequences (CSI SGR) used to set the colours and attributes as appropriate.
If I<$startcol> and I<$endcol> are defined, only returns the part of the row
between columns I<$startcol> and I<$endcol> inclusive instead of the whole
row.

Use B<row_sgrtext> to get a row if you want to output it to a real terminal
and preserve all colours, bold, etc.

=item B<sgr_change> (I<$source>, I<$dest>)

Returns a string containing ANSI/ECMA-48 escape sequences to change colours
and attributes from I<$source> to I<$dest>, which are both packed attributes
(see B<attr_pack>).  This is used internally by B<row_sgrtext>.

=item B<cols> ()

Return the number of columns in the VT102 object.

=item B<rows> ()

Return the number of rows in the VT102 object.

=item B<size> ()

Return a pair of values (I<columns>,I<rows>) denoting the size of the terminal
in the VT102 object.

=item B<x> ()

Return the current cursor X co-ordinate (1 being leftmost).

B<Note:> It is possible for the current X co-ordinate to be 1 more than the
number of columns. This happens when the end of a row is reached such that
the next character would wrap on to the next row.

=item B<y> ()

Return the current cursor Y co-ordinate (1 being topmost).

=item B<cursor> ()

Return the current cursor state (1 being on, 0 being off).

=item B<xtitle> ()

Return the current xterm window title.

=item B<xicon> ()

Return the current xterm window icon name.

=item B<status> ()

Return a list of values
(I<$x>,I<$y>,I<$attr>,I<$ti>,I<$ic>), where I<$x> and I<$y> are the cursor
co-ordinates (1,1 = top left), I<$attr> is a packed version of the current
attributes (see B<attr_unpack>), I<$ti> is the xterm window title, and
I<$ic> is the xterm window icon name.

=item B<version> ()

Return the version of the VT102 module being used.

=back

=head1 CALLBACKS

Callbacks are the processing loop's way of letting your main program know
that something has happened.  They are called while in a B<process()> loop.

To specify a callback, use the B<callback_set> interface, giving a reference
to the function to call.  Your function should take five scalar arguments:
the VT102 object being processed, the name of the callback, and two
arguments whose value depends on the callback, as shown below.  The final
argument is the private data scalar you passed when you called
B<callback_set>.

The name of the callback is passed to the callback function so that you can
have one function to handle all callbacks if you wish.

Available callback names are:

  BELL          BEL (beep, \007) character received
  CLEAR         screen about to be cleared
  OUTPUT        data (arg1) to be sent back to data source
  ROWCHANGE     screen row (row number is argument 1) content has changed
  SCROLL_DOWN   about to scroll down (arg1=top row, arg2=num to scroll)
  SCROLL_UP     about to scroll up (ditto)
  UNKNOWN       unknown/unsupported code (arg1=name, arg2=code/sequence)
  STRING        string received (arg1=source, eg PM, APC, arg2=string)
  XICONNAME     xterm icon name to be changed to arg1
  XWINTITLE     xterm title name to be changed to arg1
  LINEFEED      line feed about to be processed (arg1=row)
  GOTO          cursor about to be moved (args=new pos)

Note that the wording of the above is significant in terms of exactly
B<when> the callback is called. For instance, B<CLEAR> is called just
before the screen is cleared, whereas B<ROWCHANGE> is called I<after>
the given row has been changed.

A good callback handler for B<OUTPUT> is to simply B<syswrite()> argument 1
to your data source - eg if you're reading from a telnet session, write
that argument straight to it.  It is used for cursor position request
responses and suchlike.

Note that B<SCROLL_DOWN> is called when scrolling down, so text is about to
move UP the screen; I<arg1> will be the row number of the bottom of the
scrolling region, and I<arg2> will be the number of rows to be scrolled.
Likewise, B<SCROLL_UP> is called when text is about to move down; I<arg1>
will be the row number of the top of the scrolling region.

The B<STRING> callback is called for escape sequences that contain a string
that would otherwise be ignored, such as DSC, PM, and APC. The first
argument is the escape sequence that contained the string, such as DSC, and
the second argument is the string itself. This callback doesn't get called
for OSC strings.

The B<LINEFEED> callback can be thought of as "line completed", it's called
when LF, NEL or IND are about to be processed or just before a line wraps,
so it generally indicates that an application has finished updating a
particular line on the screen.  Handy for scrollback buffer processing.

The B<GOTO> callback is only called just before the cursor is explicitly
moved, by one of CUU, CUD, VPR, CUF, HPR, CUB, CNL, CPL, CHA, HPA, CUP, HVP.
The parameters give the destination column and row, without taking scrolling
and boundaries into account.

Finally, note that B<ROWCHANGE> is only triggered when text is being entered;
screen scrolling or screen clearance does not trigger it, that would
trigger a B<SCROLL_DOWN> or B<SCROLL_UP> or B<CLEAR>.  Line or character
insertion or deletion will cause one or more B<ROWCHANGE> callbacks, however.

=head1 SUPPORTED CODES

The following sequences are supported:

   007 (BEL)   beep
   010 (BS)    backspace
   011 (HT)    horizontal tab to next tab stop
   012 (LF)    line feed
   013 (VT)    line feed
   014 (FF)    line feed
   015 (CR)    carriage return
   021 (XON)   resume transmission (only if option IGNOREXOFF is cleared)
   023 (XOFF)  stop transmission (only if option IGNOREXOFF is cleared)
   030 (CAN)   interrupt escape sequence
   032 (SUB)   interrupt escape sequence
   033 (ESC)   start escape sequence
   177 (DEL)   ignored
   233 (CSI)   same as ESC [

   ESC 7 (DECSC)   save state
   ESC 8 (DECRC)   restore most recently saved state
   ESC H (HTS)     set tab stop at current column
   ESC g           visual beep - treated as BEL

   ESC # 8 (DECALN)  DEC screen alignment test - fill screen with E's

   CSI @ (ICH)     insert blank characters
   CSI A (CUU)     move cursor up
   CSI B (CUD)     move cursor down
   CSI C (CUF)     move cursor right
   CSI D (CUB)     move cursor left
   CSI E (CNL)     move cursor down and to column 1
   CSI F (CPL)     move cursor up and to column 1
   CSI G (CHA)     move cursor to column in current row
   CSI H (CUP)     move cursor to row, column
   CSI J (ED)      erase display
   CSI K (EL)      erase line
   CSI L (IL)      insert blank lines
   CSI M (DL)      delete lines
   CSI P (DCH)     delete characters on current line
   CSI X (ECH)     erase characters on current line
   CSI a (HPR)     move cursor right
   CSI c (DA)      return ESC [ ? 6 c (VT102)
   CSI d (VPA)     move to row (current column)
   CSI e (VPR)     move cursor down
   CSI f (HVP)     move cursor to row, column
   CSI m (SGR)     set graphic rendition
   CSI n (DSR)     device status report
   CSI r (DECSTBM) set scrolling region to (top, bottom) rows
   CSI s (CUPSV)   save cursor position
   CSI u (CUPRS)   restore cursor position
   CSI ` (HPA)     move cursor to column in current row
   CSI g (TBC)     clear tab stop (CSI 3 g = clear all stops)

=head1 LIMITATIONS

Unknown escape sequences and control characters are ignored.  All escape
sequences pertaining to character sets are ignored.

The following known control characters / sequences are ignored:

   005 (ENQ)   trigger answerback message
   016 (SO)    activate G1 charset, carriage return
   017 (SI)    activate G0 charset

The following known escape sequences are ignored:

   ESC %@ (CSDFL)    select default charset (ISO646/8859-1)
   ESC %G (CSUTF8)   select UTF-8
   ESC %8 (CSUTF8)   select UTF-8 (obsolete)
   ESC (8 (G0DFL)    G0 charset = default mapping (ISO8859-1)
   ESC (0 (G0GFX)    G0 charset = VT100 graphics mapping
   ESC (U (G0ROM)    G0 charset = null mapping (straight to ROM)
   ESC (K (G0USR)    G0 charset = user defined mapping
   ESC (B (G0TXT)    G0 charset = ASCII mapping
   ESC )8 (G1DFL)    G1 charset = default mapping (ISO8859-1)
   ESC )0 (G1GFX)    G1 charset = VT100 graphics mapping
   ESC )U (G1ROM)    G1 charset = null mapping (straight to ROM)
   ESC )K (G1USR)    G1 charset = user defined mapping
   ESC )B (G1TXT)    G1 charset = ASCII mapping
   ESC *8 (G2DFL)    G2 charset = default mapping (ISO8859-1)
   ESC *0 (G2GFX)    G2 charset = VT100 graphics mapping
   ESC *U (G2ROM)    G2 charset = null mapping (straight to ROM)
   ESC *K (G2USR)    G2 charset = user defined mapping
   ESC +8 (G3DFL)    G3 charset = default mapping (ISO8859-1)
   ESC +0 (G3GFX)    G3 charset = VT100 graphics mapping
   ESC +U (G3ROM)    G3 charset = null mapping (straight to ROM)
   ESC +K (G3USR)    G3 charset = user defined mapping
   ESC >  (DECPNM)   set numeric keypad mode
   ESC =  (DECPAM)   set application keypad mode
   ESC N  (SS2)      select G2 charset for next char only
   ESC O  (SS3)      select G3 charset for next char only
   ESC P  (DCS)      device control string (ended by ST)
   ESC X  (SOS)      start of string
   ESC ^  (PM)       privacy message (ended by ST)
   ESC _  (APC)      application program command (ended by ST)
   ESC \  (ST)       string terminator
   ESC n  (LS2)      invoke G2 charset
   ESC o  (LS3)      invoke G3 charset
   ESC |  (LS3R)     invoke G3 charset as GR
   ESC }  (LS2R)     invoke G2 charset as GR
   ESC ~  (LS1R)     invoke G1 charset as GR

The following known CSI (ESC [) sequences are ignored:

   CSI q (DECLL)   set keyboard LEDs

The following known CSI (ESC [) sequences are only partially supported:

   CSI h (SM)      set mode (only support CSI ? 25 h, cursor on/off)
   CSI l (RM)      reset mode (as above)

=head1 EXAMPLES

For some examples, including how to interface Term::VT102 with Net::Telnet
or a command such as SSH, please see the B<examples/> directory in the
distribution.

=head1 AUTHORS

Copyright (C) 2003 Andrew Wood C<E<lt>andrew dot wood at ivarch dot comE<gt>>.
Distributed under the terms of the Artistic License 2.0.

Credit is also due to:

  Charles Harker <CHarker at interland.com>
    - reported and helped to diagnose a bug in the handling of TABs

  Steve van der Burg <steve.vanderburg at lhsc.on.ca>
    - supplied basis for an example script using Net::Telnet

  Chris R. Donnelly <cdonnelly at digitalmotorworks.com>
    - added support for DECTCEM, partial support for SM/RM

  Paul L. Stoddard
    - reported a possible bug in cursor movement handling

  Joerg Walter
    - provided a patch for Unicode handling

=head1 THINGS TO WATCH OUT FOR

Make sure that your code understands NULL (\000) - you will get this in
strings where nothing has been printed on the screen.  For instance, the
sequence "12\e[C34" ("12", "CUF (move right)", "34") you might think would
yield the string "12 34", but in fact it can also yield "12\00034" - that
is, "12" followed by a zero byte followed by "34".  This is because the
screen's contents defaults to zeroes, not spaces.

To avoid that, use B<row_plaintext>, which will convert NULLs to spaces,
instead of B<row_text>.

Different types of terminal disagree on certain corner cases. For example,
B<gnome-terminal> and B<screen> handle TAB stops and TABbing past the end of
the screen in slightly different ways. Term::VT102 is closer to B<screen> in
the way it handles this sort of thing.

=head1 SEE ALSO

B<console_codes>(4), B<IO::Pty>(3)

=cut

# EOF