summaryrefslogtreecommitdiff
path: root/src/libsystemd-terminal/term-screen.c
blob: 0e38ff41c63f5b5278979a5109620ab1e7b5be49 (plain)
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
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/

/***
  This file is part of systemd.

  Copyright (C) 2014 David Herrmann <dh.herrmann@gmail.com>

  systemd is free software; you can redistribute it and/or modify it
  under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation; either version 2.1 of the License, or
  (at your option) any later version.

  systemd is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/

/*
 * Terminal Screens
 * The term_screen layer implements the terminal-side. It handles all commands
 * returned by the seq-parser and applies them to its own pages.
 *
 * While there are a lot of legacy control-sequences, we only support a small
 * subset. There is no reason to implement unused codes like horizontal
 * scrolling.
 * If you implement new commands, make sure to document them properly.
 *
 * Standards:
 *   ECMA-48
 *   ANSI X3.64
 *   ISO/IEC 6429
 * References:
 *   http://www.vt100.net/emu/ctrlseq_dec.html
 *   http://www.vt100.net/docs/vt100-ug/chapter3.html
 *   http://www.vt100.net/docs/vt510-rm/chapter4
 *   http://www.vt100.net/docs/vt510-rm/contents
 *   http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
 *   ASCII
 *   http://en.wikipedia.org/wiki/C0_and_C1_control_codes
 *   https://en.wikipedia.org/wiki/ANSI_color
 */

#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <xkbcommon/xkbcommon-keysyms.h>
#include "macro.h"
#include "term-internal.h"
#include "util.h"
#include "utf8.h"

int term_screen_new(term_screen **out, term_screen_write_fn write_fn, void *write_fn_data, term_screen_cmd_fn cmd_fn, void *cmd_fn_data) {
        _cleanup_(term_screen_unrefp) term_screen *screen = NULL;
        int r;

        assert_return(out, -EINVAL);

        screen = new0(term_screen, 1);
        if (!screen)
                return -ENOMEM;

        screen->ref = 1;
        screen->age = 1;
        screen->write_fn = write_fn;
        screen->write_fn_data = write_fn_data;
        screen->cmd_fn = cmd_fn;
        screen->cmd_fn_data = cmd_fn_data;
        screen->flags = TERM_FLAG_7BIT_MODE;
        screen->conformance_level = TERM_CONFORMANCE_LEVEL_VT400;
        screen->g0 = &term_unicode_lower;
        screen->g1 = &term_unicode_upper;
        screen->g2 = &term_unicode_lower;
        screen->g3 = &term_unicode_upper;
        screen->state.gl = &screen->g0;
        screen->state.gr = &screen->g1;
        screen->saved = screen->state;
        screen->saved_alt = screen->saved;

        r = term_page_new(&screen->page_main);
        if (r < 0)
                return r;

        r = term_page_new(&screen->page_alt);
        if (r < 0)
                return r;

        r = term_parser_new(&screen->parser, false);
        if (r < 0)
                return r;

        r = term_history_new(&screen->history_main);
        if (r < 0)
                return r;

        screen->page = screen->page_main;
        screen->history = screen->history_main;

        *out = screen;
        screen = NULL;
        return 0;
}

term_screen *term_screen_ref(term_screen *screen) {
        if (!screen)
                return NULL;

        assert_return(screen->ref > 0, NULL);

        ++screen->ref;
        return screen;
}

term_screen *term_screen_unref(term_screen *screen) {
        if (!screen)
                return NULL;

        assert_return(screen->ref > 0, NULL);

        if (--screen->ref)
                return NULL;

        free(screen->answerback);
        free(screen->tabs);
        term_history_free(screen->history_main);
        term_page_free(screen->page_alt);
        term_page_free(screen->page_main);
        term_parser_free(screen->parser);
        free(screen);

        return NULL;
}

/*
 * Write-Helpers
 * Unfortunately, 7bit/8bit compat mode requires us to send C1 controls encoded
 * as 7bit if asked by the application. This is really used in the wild, so we
 * cannot fall back to "always 7bit".
 * screen_write() is the underlying backend which forwards any writes to the
 * users's callback. It's the users responsibility to buffer these and write
 * them out once their call to term_screen_feed_*() returns.
 * The SEQ_WRITE() and SEQ_WRITE_KEY() macros allow constructing C0/C1 sequences
 * directly in the code-base without requiring any intermediate buffer during
 * runtime.
 */

#define C0_CSI "\e["
#define C1_CSI "\x9b"

#define SEQ(_screen, _prefix_esc, _c0, _c1, _seq) \
                (((_screen)->flags & TERM_FLAG_7BIT_MODE) ? \
                        ((_prefix_esc) ? ("\e" _c0 _seq) : (_c0 _seq)) : \
                        ((_prefix_esc) ? ("\e" _c1 _seq) : (_c1 _seq)))

#define SEQ_SIZE(_screen, _prefix_esc, _c0, _c1, _seq) \
                (((_screen)->flags & TERM_FLAG_7BIT_MODE) ? \
                        ((_prefix_esc) ? sizeof("\e" _c0 _seq) : sizeof(_c0 _seq)) : \
                        ((_prefix_esc) ? sizeof("\e" _c1 _seq) : sizeof(_c1 _seq)))

#define SEQ_WRITE_KEY(_screen, _prefix_esc, _c0, _c1, _seq) \
                screen_write((_screen), \
                             SEQ((_screen), (_prefix_esc), \
                                 _c0, _c1, _seq), \
                             SEQ_SIZE((_screen), (_prefix_esc), \
                                     _c0, _c1, _seq) - 1)

#define SEQ_WRITE(_screen, _c0, _c1, _seq) \
                SEQ_WRITE_KEY((_screen), false, _c0, _c1, _seq)

static int screen_write(term_screen *screen, const void *buf, size_t len) {
        if (len < 1 || !screen->write_fn)
                return 0;

        return screen->write_fn(screen, screen->write_fn_data, buf, len);
}

/*
 * Command Forwarding
 * Some commands cannot be handled by the screen-layer directly. Those are
 * forwarded to the command-handler of the caller. This is rarely used and can
 * safely be set to NULL.
 */

static int screen_forward(term_screen *screen, unsigned int cmd, const term_seq *seq) {
        if (!screen->cmd_fn)
                return 0;

        return screen->cmd_fn(screen, screen->cmd_fn_data, cmd, seq);
}

/*
 * Screen Helpers
 * These helpers implement common-operations like cursor-handler and more, which
 * are used by several command dispatchers.
 */

static unsigned int screen_clamp_x(term_screen *screen, unsigned int x) {
        if (x >= screen->page->width)
                return (screen->page->width > 0) ? screen->page->width - 1 : 0;

        return x;
}

static unsigned int screen_clamp_y(term_screen *screen, unsigned int y) {
        if (y >= screen->page->height)
                return (screen->page->height > 0) ? screen->page->height - 1 : 0;

        return y;
}

static bool screen_tab_is_set(term_screen *screen, unsigned int pos) {
        if (pos >= screen->page->width)
                return false;

        return screen->tabs[pos / 8] & (1 << (pos % 8));
}

static inline void screen_age_cursor(term_screen *screen) {
        term_cell *cell;

        cell = term_page_get_cell(screen->page, screen->state.cursor_x, screen->state.cursor_y);
        if (cell)
                cell->age = screen->age;
}

static void screen_cursor_clear_wrap(term_screen *screen) {
        screen->flags &= ~TERM_FLAG_PENDING_WRAP;
}

static void screen_cursor_set(term_screen *screen, unsigned int x, unsigned int y) {
        x = screen_clamp_x(screen, x);
        y = screen_clamp_y(screen, y);

        if (x == screen->state.cursor_x && y == screen->state.cursor_y)
                return;

        if (!(screen->flags & TERM_FLAG_HIDE_CURSOR))
                screen_age_cursor(screen);

        screen->state.cursor_x = x;
        screen->state.cursor_y = y;

        if (!(screen->flags & TERM_FLAG_HIDE_CURSOR))
                screen_age_cursor(screen);
}

static void screen_cursor_set_rel(term_screen *screen, unsigned int x, unsigned int y) {
        if (screen->state.origin_mode) {
                x = screen_clamp_x(screen, x);
                y = screen_clamp_x(screen, y) + screen->page->scroll_idx;

                if (y >= screen->page->scroll_idx + screen->page->scroll_num) {
                        y = screen->page->scroll_idx + screen->page->scroll_num;
                        if (screen->page->scroll_num > 0)
                                y -= 1;
                }
        }

        screen_cursor_set(screen, x, y);
}

static void screen_cursor_left(term_screen *screen, unsigned int num) {
        if (num > screen->state.cursor_x)
                num = screen->state.cursor_x;

        screen_cursor_set(screen, screen->state.cursor_x - num, screen->state.cursor_y);
}

static void screen_cursor_left_tab(term_screen *screen, unsigned int num) {
        unsigned int i;

        i = screen->state.cursor_x;
        while (i > 0 && num > 0) {
                if (screen_tab_is_set(screen, --i))
                        --num;
        }

        screen_cursor_set(screen, i, screen->state.cursor_y);
}

static void screen_cursor_right(term_screen *screen, unsigned int num) {
        if (num > screen->page->width)
                num = screen->page->width;

        screen_cursor_set(screen, screen->state.cursor_x + num, screen->state.cursor_y);
}

static void screen_cursor_right_tab(term_screen *screen, unsigned int num) {
        unsigned int i;

        i = screen->state.cursor_x;
        while (i + 1 < screen->page->width && num > 0) {
                if (screen_tab_is_set(screen, ++i))
                        --num;
        }

        screen_cursor_set(screen, i, screen->state.cursor_y);
}

static void screen_cursor_up(term_screen *screen, unsigned int num, bool scroll) {
        unsigned int max;

        if (screen->state.cursor_y < screen->page->scroll_idx) {
                if (num > screen->state.cursor_y)
                        num = screen->state.cursor_y;

                screen_cursor_set(screen, screen->state.cursor_x, screen->state.cursor_y - num);
        } else {
                max = screen->state.cursor_y - screen->page->scroll_idx;
                if (num > max) {
                        if (num < 1)
                                return;

                        if (!(screen->flags & TERM_FLAG_HIDE_CURSOR))
                                screen_age_cursor(screen);

                        if (scroll)
                                term_page_scroll_down(screen->page, num - max, &screen->state.attr, screen->age, NULL);

                        screen->state.cursor_y = screen->page->scroll_idx;

                        if (!(screen->flags & TERM_FLAG_HIDE_CURSOR))
                                screen_age_cursor(screen);
                } else {
                        screen_cursor_set(screen, screen->state.cursor_x, screen->state.cursor_y - num);
                }
        }
}

static void screen_cursor_down(term_screen *screen, unsigned int num, bool scroll) {
        unsigned int max;

        if (screen->state.cursor_y >= screen->page->scroll_idx + screen->page->scroll_num) {
                if (num > screen->page->height)
                        num = screen->page->height;

                screen_cursor_set(screen, screen->state.cursor_x, screen->state.cursor_y - num);
        } else {
                max = screen->page->scroll_idx + screen->page->scroll_num - 1 - screen->state.cursor_y;
                if (num > max) {
                        if (num < 1)
                                return;

                        if (!(screen->flags & TERM_FLAG_HIDE_CURSOR))
                                screen_age_cursor(screen);

                        if (scroll)
                                term_page_scroll_up(screen->page, num - max, &screen->state.attr, screen->age, screen->history);

                        screen->state.cursor_y = screen->page->scroll_idx + screen->page->scroll_num - 1;

                        if (!(screen->flags & TERM_FLAG_HIDE_CURSOR))
                                screen_age_cursor(screen);
                } else {
                        screen_cursor_set(screen, screen->state.cursor_x, screen->state.cursor_y + num);
                }
        }
}

static void screen_save_state(term_screen *screen, term_state *where) {
        *where = screen->state;
}

static void screen_restore_state(term_screen *screen, term_state *from) {
        screen_cursor_set(screen, from->cursor_x, from->cursor_y);
        screen->state = *from;
}

static void screen_reset_page(term_screen *screen, term_page *page) {
        term_page_set_scroll_region(page, 0, page->height);
        term_page_erase(page, 0, 0, page->width, page->height, &screen->state.attr, screen->age, false);
}

static void screen_change_alt(term_screen *screen, bool set) {
        if (set) {
                screen->page = screen->page_alt;
                screen->history = NULL;
        } else {
                screen->page = screen->page_main;
                screen->history = screen->history_main;
        }

        screen->page->age = screen->age;
}

static inline void set_reset(term_screen *screen, unsigned int flag, bool set) {
        if (set)
                screen->flags |= flag;
        else
                screen->flags &= ~flag;
}

static void screen_mode_change_ansi(term_screen *screen, unsigned mode, bool set) {
        switch (mode) {
        case 20:
                /*
                 * LNM: line-feed/new-line mode
                 * TODO
                 */
                set_reset(screen, TERM_FLAG_NEWLINE_MODE, set);

                break;
        default:
                log_debug("terminal: failed to %s unknown ANSI mode %u", set ? "set" : "unset", mode);
        }
}

static void screen_mode_change_dec(term_screen *screen, unsigned int mode, bool set) {
        switch (mode) {
        case 1:
                /*
                 * DECCKM: cursor-keys
                 * TODO
                 */
                set_reset(screen, TERM_FLAG_CURSOR_KEYS, set);

                break;
        case 6:
                /*
                 * DECOM: origin-mode
                 * TODO
                 */
                screen->state.origin_mode = set;

                break;
        case 7:
                /*
                 * DECAWN: auto-wrap mode
                 * TODO
                 */
                screen->state.auto_wrap = set;

                break;
        case 25:
                /*
                 * DECTCEM: text-cursor-enable
                 * TODO
                 */
                set_reset(screen, TERM_FLAG_HIDE_CURSOR, !set);
                screen_age_cursor(screen);

                break;
        case 47:
                /*
                 * XTERM-ASB: alternate-screen-buffer
                 * This enables/disables the alternate screen-buffer.
                 * It effectively saves the current page content and
                 * allows you to restore it when changing to the
                 * original screen-buffer again.
                 */
                screen_change_alt(screen, set);

                break;
        case 1047:
                /*
                 * XTERM-ASBPE: alternate-screen-buffer-post-erase
                 * This is the same as XTERM-ASB but erases the
                 * alternate screen-buffer before switching back to the
                 * original buffer. Use it to discard any data on the
                 * alternate screen buffer when done.
                 */
                if (!set)
                        screen_reset_page(screen, screen->page_alt);

                screen_change_alt(screen, set);

                break;
        case 1048:
                /*
                 * XTERM-ASBCS: alternate-screen-buffer-cursor-state
                 * This has the same effect as DECSC/DECRC, but uses a
                 * separate state buffer. It is usually used in
                 * combination with alternate screen buffers to provide
                 * stacked state storage.
                 */
                if (set)
                        screen_save_state(screen, &screen->saved_alt);
                else
                        screen_restore_state(screen, &screen->saved_alt);

                break;
        case 1049:
                /*
                 * XTERM-ASBX: alternate-screen-buffer-extended
                 * This combines XTERM-ASBPE and XTERM-ASBCS somewhat.
                 * When enabling, state is saved, alternate screen
                 * buffer is activated and cleared.
                 * When disabled, alternate screen buffer is cleared,
                 * then normal screen buffer is enabled and state is
                 * restored.
                 */
                if (set)
                        screen_save_state(screen, &screen->saved_alt);

                screen_reset_page(screen, screen->page_alt);
                screen_change_alt(screen, set);

                if (!set)
                        screen_restore_state(screen, &screen->saved_alt);

                break;
        default:
                log_debug("terminal: failed to %s unknown DEC mode %u", set ? "set" : "unset", mode);
        }
}

/* map a character according to current GL and GR maps */
static uint32_t screen_map(term_screen *screen, uint32_t val) {
        uint32_t nval = -1U;

        /* 32 and 127 always map to identity. 160 and 255 map to identity iff a
         * 96 character set is loaded into GR. Values above 255 always map to
         * identity. */
        switch (val) {
        case 33 ... 126:
                if (screen->state.glt) {
                        nval = (**screen->state.glt)[val - 32];
                        screen->state.glt = NULL;
                } else {
                        nval = (**screen->state.gl)[val - 32];
                }
                break;
        case 160 ... 255:
                if (screen->state.grt) {
                        nval = (**screen->state.grt)[val - 160];
                        screen->state.grt = NULL;
                } else {
                        nval = (**screen->state.gr)[val - 160];
                }
                break;
        }

        return (nval == -1U) ? val : nval;
}

/*
 * Command Handlers
 * This is the unofficial documentation of all the TERM_CMD_* definitions. Each
 * handled command has a separate function with an extensive comment on the
 * semantics of the command.
 * Note that many semantics are unknown and need to be verified. This is mostly
 * about error-handling, though. Applications rarely rely on those features.
 */

static int screen_DA1(term_screen *screen, const term_seq *seq);
static int screen_LF(term_screen *screen, const term_seq *seq);

static int screen_GRAPHIC(term_screen *screen, const term_seq *seq) {
        term_char_t ch = TERM_CHAR_NULL;

        if (screen->state.cursor_x + 1 == screen->page->width
            && screen->flags & TERM_FLAG_PENDING_WRAP
            && screen->state.auto_wrap) {
                screen_cursor_down(screen, 1, true);
                screen_cursor_set(screen, 0, screen->state.cursor_y);
        }

        screen_cursor_clear_wrap(screen);

        ch = term_char_merge(ch, screen_map(screen, seq->terminator));
        term_page_write(screen->page, screen->state.cursor_x, screen->state.cursor_y, ch, 1, &screen->state.attr, screen->age, false);

        if (screen->state.cursor_x + 1 == screen->page->width)
                screen->flags |= TERM_FLAG_PENDING_WRAP;
        else
                screen_cursor_right(screen, 1);

        return 0;
}

static int screen_BEL(term_screen *screen, const term_seq *seq) {
        /*
         * BEL - sound bell tone
         * This command should trigger an acoustic bell. Usually, this is
         * forwarded directly to the pcspkr. However, bells have become quite
         * uncommon and annoying, so we're not implementing them here. Instead,
         * it's one of the commands we forward to the caller.
         */

        return screen_forward(screen, TERM_CMD_BEL, seq);
}

static int screen_BS(term_screen *screen, const term_seq *seq) {
        /*
         * BS - backspace
         * Move cursor one cell to the left. If already at the left margin,
         * nothing happens.
         */

        screen_cursor_clear_wrap(screen);
        screen_cursor_left(screen, 1);
        return 0;
}

static int screen_CBT(term_screen *screen, const term_seq *seq) {
        /*
         * CBT - cursor-backward-tabulation
         * Move the cursor @args[0] tabs backwards (to the left). The
         * current cursor cell, in case it's a tab, is not counted.
         * Furthermore, the cursor cannot be moved beyond position 0 and
         * it will stop there.
         *
         * Defaults:
         *   args[0]: 1
         */

        unsigned int num = 1;

        if (seq->args[0] > 0)
                num = seq->args[0];

        screen_cursor_clear_wrap(screen);
        screen_cursor_left_tab(screen, num);

        return 0;
}

static int screen_CHA(term_screen *screen, const term_seq *seq) {
        /*
         * CHA - cursor-horizontal-absolute
         * Move the cursor to position @args[0] in the current line. The
         * cursor cannot be moved beyond the rightmost cell and will stop
         * there.
         *
         * Defaults:
         *   args[0]: 1
         */

        unsigned int pos = 1;

        if (seq->args[0] > 0)
                pos = seq->args[0];

        screen_cursor_clear_wrap(screen);
        screen_cursor_set(screen, pos - 1, screen->state.cursor_y);

        return 0;
}

static int screen_CHT(term_screen *screen, const term_seq *seq) {
        /*
         * CHT - cursor-horizontal-forward-tabulation
         * Move the cursor @args[0] tabs forward (to the right). The
         * current cursor cell, in case it's a tab, is not counted.
         * Furthermore, the cursor cannot be moved beyond the rightmost cell
         * and will stop there.
         *
         * Defaults:
         *   args[0]: 1
         */

        unsigned int num = 1;

        if (seq->args[0] > 0)
                num = seq->args[0];

        screen_cursor_clear_wrap(screen);
        screen_cursor_right_tab(screen, num);

        return 0;
}

static int screen_CNL(term_screen *screen, const term_seq *seq) {
        /*
         * CNL - cursor-next-line
         * Move the cursor @args[0] lines down.
         *
         * TODO: Does this stop at the bottom or cause a scroll-up?
         *
         * Defaults:
         *   args[0]: 1
         */

        unsigned int num = 1;

        if (seq->args[0] > 0)
                num = seq->args[0];

        screen_cursor_clear_wrap(screen);
        screen_cursor_down(screen, num, false);

        return 0;
}

static int screen_CPL(term_screen *screen, const term_seq *seq) {
        /*
         * CPL - cursor-preceding-line
         * Move the cursor @args[0] lines up.
         *
         * TODO: Does this stop at the top or cause a scroll-up?
         *
         * Defaults:
         *   args[0]: 1
         */

        unsigned int num = 1;

        if (seq->args[0] > 0)
                num = seq->args[0];

        screen_cursor_clear_wrap(screen);
        screen_cursor_up(screen, num, false);

        return 0;
}

static int screen_CR(term_screen *screen, const term_seq *seq) {
        /*
         * CR - carriage-return
         * Move the cursor to the left margin on the current line.
         */

        screen_cursor_clear_wrap(screen);
        screen_cursor_set(screen, 0, screen->state.cursor_y);

        return 0;
}

static int screen_CUB(term_screen *screen, const term_seq *seq) {
        /*
         * CUB - cursor-backward
         * Move the cursor @args[0] positions to the left. The cursor stops
         * at the left-most position.
         *
         * Defaults:
         *   args[0]: 1
         */

        unsigned int num = 1;

        if (seq->args[0] > 0)
                num = seq->args[0];

        screen_cursor_clear_wrap(screen);
        screen_cursor_left(screen, num);

        return 0;
}

static int screen_CUD(term_screen *screen, const term_seq *seq) {
        /*
         * CUD - cursor-down
         * Move the cursor @args[0] positions down. The cursor stops at the
         * bottom margin. If it was already moved further, it stops at the
         * bottom line.
         *
         * Defaults:
         *   args[0]: 1
         */

        unsigned int num = 1;

        if (seq->args[0] > 0)
                num = seq->args[0];

        screen_cursor_clear_wrap(screen);
        screen_cursor_down(screen, num, false);

        return 0;
}

static int screen_CUF(term_screen *screen, const term_seq *seq) {
        /*
         * CUF -cursor-forward
         * Move the cursor @args[0] positions to the right. The cursor stops
         * at the right-most position.
         *
         * Defaults:
         *   args[0]: 1
         */

        unsigned int num = 1;

        if (seq->args[0] > 0)
                num = seq->args[0];

        screen_cursor_clear_wrap(screen);
        screen_cursor_right(screen, num);

        return 0;
}

static int screen_CUP(term_screen *screen, const term_seq *seq) {
        /*
         * CUP - cursor-position
         * Moves the cursor to position @args[1] x @args[0]. If either is 0, it
         * is treated as 1. The positions are subject to the origin-mode and
         * clamped to the addressable with/height.
         *
         * Defaults:
         *   args[0]: 1
         *   args[1]: 1
         */

        unsigned int x = 1, y = 1;

        if (seq->args[0] > 0)
                y = seq->args[0];
        if (seq->args[1] > 0)
                x = seq->args[1];

        screen_cursor_clear_wrap(screen);
        screen_cursor_set_rel(screen, x - 1, y - 1);

        return 0;
}

static int screen_CUU(term_screen *screen, const term_seq *seq) {
        /*
         * CUU - cursor-up
         * Move the cursor @args[0] positions up. The cursor stops at the
         * top margin. If it was already moved further, it stops at the
         * top line.
         *
         * Defaults:
         *   args[0]: 1
         *
         */

        unsigned int num = 1;

        if (seq->args[0] > 0)
                num = seq->args[0];

        screen_cursor_clear_wrap(screen);
        screen_cursor_up(screen, num, false);

        return 0;
}

static int screen_DA1(term_screen *screen, const term_seq *seq) {
        /*
         * DA1 - primary-device-attributes
         * The primary DA asks for basic terminal features. We simply return
         * a hard-coded list of features we implement.
         * Note that the primary DA asks for supported features, not currently
         * enabled features.
         *
         * The terminal's answer is:
         *   ^[ ? 64 ; ARGS c
         * The first argument, 64, is fixed and denotes a VT420, the last
         * DEC-term that extended this number.
         * All following arguments denote supported features. Note
         * that at most 15 features can be sent (max CSI args). It is safe to
         * send more, but clients might not be able to parse them. This is a
         * client's problem and we shouldn't care. There is no other way to
         * send those feature lists, so we have to extend them beyond 15 in
         * those cases.
         *
         * Known modes:
         *    1: 132 column mode
         *       The 132 column mode is supported by the terminal.
         *    2: printer port
         *       A priner-port is supported and can be addressed via
         *       control-codes.
         *    3: ReGIS graphics
         *       Support for ReGIS graphics is available. The ReGIS routines
         *       provide the "remote graphics instruction set" and allow basic
         *       vector-rendering.
         *    4: sixel
         *       Support of Sixel graphics is available. This provides access
         *       to the sixel bitmap routines.
         *    6: selective erase
         *       The terminal supports DECSCA and related selective-erase
         *       functions. This allows to protect specific cells from being
         *       erased, if specified.
         *    7: soft character set (DRCS)
         *       TODO: ?
         *    8: user-defined keys (UDKs)
         *       TODO: ?
         *    9: national-replacement character sets (NRCS)
         *       National-replacement character-sets are available.
         *   12: Yugoslavian (SCS)
         *       TODO: ?
         *   15: technical character set
         *       The DEC technical-character-set is available.
         *   18: windowing capability
         *       TODO: ?
         *   21: horizontal scrolling
         *       TODO: ?
         *   22: ANSII color
         *       TODO: ?
         *   23: Greek
         *       TODO: ?
         *   24: Turkish
         *       TODO: ?
         *   29: ANSI text locator
         *       TODO: ?
         *   42: ISO Latin-2 character set
         *       TODO: ?
         *   44: PCTerm
         *       TODO: ?
         *   45: soft keymap
         *       TODO: ?
         *   46: ASCII emulation
         *       TODO: ?
         */

        return SEQ_WRITE(screen, C0_CSI, C1_CSI, "?64;1;6;9;15c");
}

static int screen_DA2(term_screen *screen, const term_seq *seq) {
        /*
         * DA2 - secondary-device-attributes
         * The secondary DA asks for the terminal-ID, firmware versions and
         * other non-primary attributes. All these values are
         * informational-only and should not be used by the host to detect
         * terminal features.
         *
         * The terminal's response is:
         *   ^[ > 61 ; FIRMWARE ; KEYBOARD c
         * whereas 65 is fixed for VT525 terminals, the last terminal-line that
         * increased this number. FIRMWARE is the firmware
         * version encoded as major/minor (20 == 2.0) and KEYBOARD is 0 for STD
         * keyboard and 1 for PC keyboards.
         *
         * We replace the firmware-version with the systemd-version so clients
         * can decode it again.
         */

        return SEQ_WRITE(screen, C0_CSI, C1_CSI, ">65;" PACKAGE_VERSION ";1c");
}

static int screen_DA3(term_screen *screen, const term_seq *seq) {
        /*
         * DA3 - tertiary-device-attributes
         * The tertiary DA is used to query the terminal-ID.
         *
         * The terminal's response is:
         *   ^P ! | XX AA BB CC ^\
         * whereas all four parameters are hexadecimal-encoded pairs. XX
         * denotes the manufacturing site, AA BB CC is the terminal's ID.
         */

        /* we do not support tertiary DAs */
        return 0;
}

static int screen_DC1(term_screen *screen, const term_seq *seq) {
        /*
         * DC1 - device-control-1 or XON
         * This clears any previous XOFF and resumes terminal-transmission.
         */

        /* we do not support XON */
        return 0;
}

static int screen_DC3(term_screen *screen, const term_seq *seq) {
        /*
         * DC3 - device-control-3 or XOFF
         * Stops terminal transmission. No further characters are sent until
         * an XON is received.
         */

        /* we do not support XOFF */
        return 0;
}

static int screen_DCH(term_screen *screen, const term_seq *seq) {
        /*
         * DCH - delete-character
         * This deletes @argv[0] characters at the current cursor position. As
         * characters are deleted, the remaining characters between the cursor
         * and right margin move to the left. Character attributes move with the
         * characters. The terminal adds blank spaces with no visual character
         * attributes at the right margin. DCH has no effect outside the
         * scrolling margins.
         *
         * Defaults:
         *   args[0]: 1
         */

        unsigned int num = 1;

        if (seq->args[0] > 0)
                num = seq->args[0];

        screen_cursor_clear_wrap(screen);
        term_page_delete_cells(screen->page, screen->state.cursor_x, screen->state.cursor_y, num, &screen->state.attr, screen->age);

        return 0;
}

static int screen_DECALN(term_screen *screen, const term_seq *seq) {
        /*
         * DECALN - screen-alignment-pattern
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECANM(term_screen *screen, const term_seq *seq) {
        /*
         * DECANM - ansi-mode
         * Set the terminal into VT52 compatibility mode. Control sequences
         * overlap with regular sequences so we have to detect them early before
         * dispatching them.
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECBI(term_screen *screen, const term_seq *seq) {
        /*
         * DECBI - back-index
         * This control function moves the cursor backward one column. If the
         * cursor is at the left margin, then all screen data within the margin
         * moves one column to the right. The column that shifted past the right
         * margin is lost.
         * DECBI adds a new column at the left margin with no visual attributes.
         * DECBI does not affect the margins. If the cursor is beyond the
         * left-margin at the left border, then the terminal ignores DECBI.
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECCARA(term_screen *screen, const term_seq *seq) {
        /*
         * DECCARA - change-attributes-in-rectangular-area
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECCRA(term_screen *screen, const term_seq *seq) {
        /*
         * DECCRA - copy-rectangular-area
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECDC(term_screen *screen, const term_seq *seq) {
        /*
         * DECDC - delete-column
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECDHL_BH(term_screen *screen, const term_seq *seq) {
        /*
         * DECDHL_BH - double-width-double-height-line: bottom half
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECDHL_TH(term_screen *screen, const term_seq *seq) {
        /*
         * DECDHL_TH - double-width-double-height-line: top half
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECDWL(term_screen *screen, const term_seq *seq) {
        /*
         * DECDWL - double-width-single-height-line
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECEFR(term_screen *screen, const term_seq *seq) {
        /*
         * DECEFR - enable-filter-rectangle
         * Defines the coordinates of a filter rectangle (top, left, bottom,
         * right as @args[0] to @args[3]) and activates it.
         * Anytime the locator is detected outside of the filter rectangle, an
         * outside rectangle event is generated and the rectangle is disabled.
         * Filter rectangles are always treated as "one-shot" events. Any
         * parameters that are omitted default to the current locator position.
         * If all parameters are omitted, any locator motion will be reported.
         * DECELR always cancels any prevous rectangle definition.
         *
         * The locator is usually associated with the mouse-cursor, but based
         * on cells instead of pixels. See DECELR how to initialize and enable
         * it. DECELR can also enable pixel-mode instead of cell-mode.
         *
         * TODO: implement
         */

        return 0;
}

static int screen_DECELF(term_screen *screen, const term_seq *seq) {
        /*
         * DECELF - enable-local-functions
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECELR(term_screen *screen, const term_seq *seq) {
        /*
         * DECELR - enable-locator-reporting
         * This changes the locator-reporting mode. @args[0] specifies the mode
         * to set, 0 disables locator-reporting, 1 enables it continuously, 2
         * enables it for a single report. @args[1] specifies the
         * precision-mode. 0 and 2 set the reporting to cell-precision, 1 sets
         * pixel-precision.
         *
         * Defaults:
         *   args[0]: 0
         *   args[1]: 0
         *
         * TODO: implement
         */

        return 0;
}

static int screen_DECERA(term_screen *screen, const term_seq *seq) {
        /*
         * DECERA - erase-rectangular-area
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECFI(term_screen *screen, const term_seq *seq) {
        /*
         * DECFI - forward-index
         * This control function moves the cursor forward one column. If the
         * cursor is at the right margin, then all screen data within the
         * margins moves one column to the left. The column shifted past the
         * left margin is lost.
         * DECFI adds a new column at the right margin, with no visual
         * attributes. DECFI does not affect margins. If the cursor is beyond
         * the right margin at the border of the page when the terminal
         * receives DECFI, then the terminal ignores DECFI.
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECFRA(term_screen *screen, const term_seq *seq) {
        /*
         * DECFRA - fill-rectangular-area
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECIC(term_screen *screen, const term_seq *seq) {
        /*
         * DECIC - insert-column
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECID(term_screen *screen, const term_seq *seq) {
        /*
         * DECID - return-terminal-id
         * This is an obsolete form of TERM_CMD_DA1.
         */

        return screen_DA1(screen, seq);
}

static int screen_DECINVM(term_screen *screen, const term_seq *seq) {
        /*
         * DECINVM - invoke-macro
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECKBD(term_screen *screen, const term_seq *seq) {
        /*
         * DECKBD - keyboard-language-selection
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECKPAM(term_screen *screen, const term_seq *seq) {
        /*
         * DECKPAM - keypad-application-mode
         * Enables the keypad-application mode. If enabled, the keypad sends
         * special characters instead of the printed characters. This way,
         * applications can detect whether a numeric key was pressed on the
         * top-row or on the keypad.
         * Default is keypad-numeric-mode.
         */

        screen->flags |= TERM_FLAG_KEYPAD_MODE;

        return 0;
}

static int screen_DECKPNM(term_screen *screen, const term_seq *seq) {
        /*
         * DECKPNM - keypad-numeric-mode
         * This disables the keypad-application-mode (DECKPAM) and returns to
         * the keypad-numeric-mode. Keypresses on the keypad generate the same
         * sequences as corresponding keypresses on the main keyboard.
         * Default is keypad-numeric-mode.
         */

        screen->flags &= ~TERM_FLAG_KEYPAD_MODE;

        return 0;
}

static int screen_DECLFKC(term_screen *screen, const term_seq *seq) {
        /*
         * DECLFKC - local-function-key-control
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECLL(term_screen *screen, const term_seq *seq) {
        /*
         * DECLL - load-leds
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECLTOD(term_screen *screen, const term_seq *seq) {
        /*
         * DECLTOD - load-time-of-day
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECPCTERM(term_screen *screen, const term_seq *seq) {
        /*
         * DECPCTERM - pcterm-mode
         * This enters/exits the PCTerm mode. Default mode is VT-mode. It can
         * also select parameters for scancode/keycode mappings in SCO mode.
         *
         * Definitely not worth implementing. Lets kill PCTerm/SCO modes!
         */

        return 0;
}

static int screen_DECPKA(term_screen *screen, const term_seq *seq) {
        /*
         * DECPKA - program-key-action
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECPKFMR(term_screen *screen, const term_seq *seq) {
        /*
         * DECPKFMR - program-key-free-memory-report
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECRARA(term_screen *screen, const term_seq *seq) {
        /*
         * DECRARA - reverse-attributes-in-rectangular-area
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECRC(term_screen *screen, const term_seq *seq) {
        /*
         * DECRC - restore-cursor
         * Restores the terminal to the state saved by the save cursor (DECSC)
         * function. This includes more than just the cursor-position.
         *
         * If nothing was saved by DECSC, then DECRC performs the following
         * actions:
         *   * Moves the cursor to the home position (upper left of screen).
         *   * Resets origin mode (DECOM).
         *   * Turns all character attributes off (normal setting).
         *   * Maps the ASCII character set into GL, and the DEC Supplemental
         *     Graphic set into GR.
         *
         * The terminal maintains a separate DECSC buffer for the main display
         * and the status line. This feature lets you save a separate operating
         * state for the main display and the status line.
         */

        screen_restore_state(screen, &screen->saved);

        return 0;
}

static int screen_DECREQTPARM(term_screen *screen, const term_seq *seq) {
        /*
         * DECREQTPARM - request-terminal-parameters
         * The sequence DECREPTPARM is sent by the terminal controller to notify
         * the host of the status of selected terminal parameters. The status
         * sequence may be sent when requested by the host or at the terminal's
         * discretion. DECREPTPARM is sent upon receipt of a DECREQTPARM.
         *
         * If @args[0] is 0, this marks a request and the terminal is allowed
         * to send DECREPTPARM messages without request. If it is 1, the same
         * applies but the terminal should no longer send DECREPTPARM
         * unrequested.
         * 2 and 3 mark a report, but 3 is only used if the terminal answers as
         * an explicit request with @args[0] == 1.
         *
         * The other arguments are ignored in requests, but have the following
         * meaning in responses:
         *   args[1]: 1=no-parity-set 4=parity-set-and-odd 5=parity-set-and-even
         *   args[2]: 1=8bits-per-char 2=7bits-per-char
         *   args[3]: transmission-speed
         *   args[4]: receive-speed
         *   args[5]: 1=bit-rate-multiplier-is-16
         *   args[6]: This value communicates the four switch values in block 5
         *            of SETUP B, which are only visible to the user when an STP
         *            option is installed. These bits may be assigned for an STP
         *            device. The four bits are a decimal-encoded binary number.
         *            Value between 0-15.
         *
         * The transmission/receive speeds have mappings for number => bits/s
         * which are quite weird. Examples are: 96->3600, 112->9600, 120->19200
         *
         * Defaults:
         *   args[0]: 0
         */

        if (seq->n_args < 1 || seq->args[0] == 0) {
                screen->flags &= ~TERM_FLAG_INHIBIT_TPARM;
                return SEQ_WRITE(screen, C0_CSI, C1_CSI, "2;1;1;120;120;1;0x");
        } else if (seq->args[0] == 1) {
                screen->flags |= TERM_FLAG_INHIBIT_TPARM;
                return SEQ_WRITE(screen, C0_CSI, C1_CSI, "3;1;1;120;120;1;0x");
        } else {
                return 0;
        }
}

static int screen_DECRPKT(term_screen *screen, const term_seq *seq) {
        /*
         * DECRPKT - report-key-type
         * Response to DECRQKT, we can safely ignore it as we're the one sending
         * it to the host.
         */

        return 0;
}

static int screen_DECRQCRA(term_screen *screen, const term_seq *seq) {
        /*
         * DECRQCRA - request-checksum-of-rectangular-area
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECRQDE(term_screen *screen, const term_seq *seq) {
        /*
         * DECRQDE - request-display-extent
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECRQKT(term_screen *screen, const term_seq *seq) {
        /*
         * DECRQKT - request-key-type
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECRQLP(term_screen *screen, const term_seq *seq) {
        /*
         * DECRQLP - request-locator-position
         * See DECELR for locator-information.
         *
         * TODO: document and implement
         */

        return 0;
}

static int screen_DECRQM_ANSI(term_screen *screen, const term_seq *seq) {
        /*
         * DECRQM_ANSI - request-mode-ansi
         * The host sends this control function to find out if a particular mode
         * is set or reset. The terminal responds with a report mode function.
         * @args[0] contains the mode to query.
         *
         * Response is DECRPM with the first argument set to the mode that was
         * queried, second argument is 0 if mode is invalid, 1 if mode is set,
         * 2 if mode is not set (reset), 3 if mode is permanently set and 4 if
         * mode is permanently not set (reset):
         *   ANSI: ^[ MODE ; VALUE $ y
         *   DEC:  ^[ ? MODE ; VALUE $ y
         *
         * TODO: implement
         */

        return 0;
}

static int screen_DECRQM_DEC(term_screen *screen, const term_seq *seq) {
        /*
         * DECRQM_DEC - request-mode-dec
         * Same as DECRQM_ANSI but for DEC modes.
         *
         * TODO: implement
         */

        return 0;
}

static int screen_DECRQPKFM(term_screen *screen, const term_seq *seq) {
        /*
         * DECRQPKFM - request-program-key-free-memory
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECRQPSR(term_screen *screen, const term_seq *seq) {
        /*
         * DECRQPSR - request-presentation-state-report
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECRQTSR(term_screen *screen, const term_seq *seq) {
        /*
         * DECRQTSR - request-terminal-state-report
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECRQUPSS(term_screen *screen, const term_seq *seq) {
        /*
         * DECRQUPSS - request-user-preferred-supplemental-set
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECSACE(term_screen *screen, const term_seq *seq) {
        /*
         * DECSACE - select-attribute-change-extent
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECSASD(term_screen *screen, const term_seq *seq) {
        /*
         * DECSASD - select-active-status-display
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECSC(term_screen *screen, const term_seq *seq) {
        /*
         * DECSC - save-cursor
         * Save cursor and terminal state so it can be restored later on.
         * Saves the following items in the terminal's memory:
         *   * Cursor position
         *   * Character attributes set by the SGR command
         *   * Character sets (G0, G1, G2, or G3) currently in GL and GR
         *   * Wrap flag (autowrap or no autowrap)
         *   * State of origin mode (DECOM)
         *   * Selective erase attribute
         *   * Any single shift 2 (SS2) or single shift 3 (SS3) functions sent
         */

        screen_save_state(screen, &screen->saved);

        return 0;
}

static int screen_DECSCA(term_screen *screen, const term_seq *seq) {
        /*
         * DECSCA - select-character-protection-attribute
         * Defines the characters that come after it as erasable or not erasable
         * from the screen. The selective erase control functions (DECSED and
         * DECSEL) can only erase characters defined as erasable.
         *
         * @args[0] specifies the new mode. 0 and 2 mark any following character
         * as erasable, 1 marks it as not erasable.
         *
         * Defaults:
         *   args[0]: 0
         */

        unsigned int mode = 0;

        if (seq->args[0] > 0)
                mode = seq->args[0];

        switch (mode) {
        case 0:
        case 2:
                screen->state.attr.protect = 0;
                break;
        case 1:
                screen->state.attr.protect = 1;
                break;
        }

        return 0;
}

static int screen_DECSCL(term_screen *screen, const term_seq *seq) {
        /*
         * DECSCL - select-conformance-level
         * Select the terminal's operating level. The factory default is
         * level 4 (VT Level 4 mode, 7-bit controls).
         * When you change the conformance level, the terminal performs a hard
         * reset (RIS).
         *
         * @args[0] defines the conformance-level, valid values are:
         *   61: Level 1 (VT100)
         *   62: Level 2 (VT200)
         *   63: Level 3 (VT300)
         *   64: Level 4 (VT400)
         * @args[1] defines the 8bit-mode, valid values are:
         *    0: 8-bit controls
         *    1: 7-bit controls
         *    2: 8-bit controls (same as 0)
         *
         * If @args[0] is 61, then @args[1] is ignored and 7bit controls are
         * enforced.
         *
         * Defaults:
         *   args[0]: 64
         *   args[1]: 0
         */

        unsigned int level = 64, bit = 0;

        if (seq->n_args > 0) {
                level = seq->args[0];
                if (seq->n_args > 1)
                        bit = seq->args[1];
        }

        term_screen_hard_reset(screen);

        switch (level) {
        case 61:
                screen->conformance_level = TERM_CONFORMANCE_LEVEL_VT100;
                screen->flags |= TERM_FLAG_7BIT_MODE;
                break;
        case 62 ... 69:
                screen->conformance_level = TERM_CONFORMANCE_LEVEL_VT400;
                if (bit == 1)
                        screen->flags |= TERM_FLAG_7BIT_MODE;
                else
                        screen->flags &= ~TERM_FLAG_7BIT_MODE;
                break;
        }

        return 0;
}

static int screen_DECSCP(term_screen *screen, const term_seq *seq) {
        /*
         * DECSCP - select-communication-port
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECSCPP(term_screen *screen, const term_seq *seq) {
        /*
         * DECSCPP - select-columns-per-page
         * Select columns per page. The number of rows is unaffected by this.
         * @args[0] selectes the number of columns (width), DEC only defines 80
         * and 132, but we allow any integer here. 0 is equivalent to 80.
         * Page content is *not* cleared and the cursor is left untouched.
         * However, if the page is reduced in width and the cursor would be
         * outside the visible region, it's set to the right border. Newly added
         * cells are cleared. No data is retained outside the visible region.
         *
         * Defaults:
         *   args[0]: 0
         *
         * TODO: implement
         */

        return 0;
}

static int screen_DECSCS(term_screen *screen, const term_seq *seq) {
        /*
         * DECSCS - select-communication-speed
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECSCUSR(term_screen *screen, const term_seq *seq) {
        /*
         * DECSCUSR - set-cursor-style
         * This changes the style of the cursor. @args[0] can be one of:
         *   0, 1: blinking block
         *      2: steady block
         *      3: blinking underline
         *      4: steady underline
         * Changing this setting does _not_ affect the cursor visibility itself.
         * Use DECTCEM for that.
         *
         * Defaults:
         *   args[0]: 0
         *
         * TODO: implement
         */

        return 0;
}

static int screen_DECSDDT(term_screen *screen, const term_seq *seq) {
        /*
         * DECSDDT - select-disconnect-delay-time
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECSDPT(term_screen *screen, const term_seq *seq) {
        /*
         * DECSDPT - select-digital-printed-data-type
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECSED(term_screen *screen, const term_seq *seq) {
        /*
         * DECSED - selective-erase-in-display
         * This control function erases some or all of the erasable characters
         * in the display. DECSED can only erase characters defined as erasable
         * by the DECSCA control function. DECSED works inside or outside the
         * scrolling margins.
         *
         * @args[0] defines which regions are erased. If it is 0, all cells from
         * the cursor (inclusive) till the end of the display are erase. If it
         * is 1, all cells from the start of the display till the cursor
         * (inclusive) are erased. If it is 2, all cells are erased.
         *
         * Defaults:
         *   args[0]: 0
         */

        unsigned int mode = 0;

        if (seq->args[0] > 0)
                mode = seq->args[0];

        switch (mode) {
        case 0:
                term_page_erase(screen->page,
                                screen->state.cursor_x, screen->state.cursor_y,
                                screen->page->width, screen->page->height,
                                &screen->state.attr, screen->age, true);
                break;
        case 1:
                term_page_erase(screen->page,
                                0, 0,
                                screen->state.cursor_x, screen->state.cursor_y,
                                &screen->state.attr, screen->age, true);
                break;
        case 2:
                term_page_erase(screen->page,
                                0, 0,
                                screen->page->width, screen->page->height,
                                &screen->state.attr, screen->age, true);
                break;
        }

        return 0;
}

static int screen_DECSEL(term_screen *screen, const term_seq *seq) {
        /*
         * DECSEL - selective-erase-in-line
         * This control function erases some or all of the erasable characters
         * in a single line of text. DECSEL erases only those characters defined
         * as erasable by the DECSCA control function. DECSEL works inside or
         * outside the scrolling margins.
         *
         * @args[0] defines the region to be erased. If it is 0, all cells from
         * the cursor (inclusive) till the end of the line are erase. If it is
         * 1, all cells from the start of the line till the cursor (inclusive)
         * are erased. If it is 2, the whole line of the cursor is erased.
         *
         * Defaults:
         *   args[0]: 0
         */

        unsigned int mode = 0;

        if (seq->args[0] > 0)
                mode = seq->args[0];

        switch (mode) {
        case 0:
                term_page_erase(screen->page,
                                screen->state.cursor_x, screen->state.cursor_y,
                                screen->page->width, screen->state.cursor_y,
                                &screen->state.attr, screen->age, true);
                break;
        case 1:
                term_page_erase(screen->page,
                                0, screen->state.cursor_y,
                                screen->state.cursor_x, screen->state.cursor_y,
                                &screen->state.attr, screen->age, true);
                break;
        case 2:
                term_page_erase(screen->page,
                                0, screen->state.cursor_y,
                                screen->page->width, screen->state.cursor_y,
                                &screen->state.attr, screen->age, true);
                break;
        }

        return 0;
}

static int screen_DECSERA(term_screen *screen, const term_seq *seq) {
        /*
         * DECSERA - selective-erase-rectangular-area
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECSFC(term_screen *screen, const term_seq *seq) {
        /*
         * DECSFC - select-flow-control
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECSKCV(term_screen *screen, const term_seq *seq) {
        /*
         * DECSKCV - set-key-click-volume
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECSLCK(term_screen *screen, const term_seq *seq) {
        /*
         * DECSLCK - set-lock-key-style
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECSLE(term_screen *screen, const term_seq *seq) {
        /*
         * DECSLE - select-locator-events
         *
         * TODO: implement
         */

        return 0;
}

static int screen_DECSLPP(term_screen *screen, const term_seq *seq) {
        /*
         * DECSLPP - set-lines-per-page
         * Set the number of lines used for the page. @args[0] specifies the
         * number of lines to be used. DEC only allows a limited number of
         * choices, however, we allow all integers. 0 is equivalent to 24.
         *
         * Defaults:
         *   args[0]: 0
         *
         * TODO: implement
         */

        return 0;
}

static int screen_DECSLRM_OR_SC(term_screen *screen, const term_seq *seq) {
        /*
         * DECSLRM_OR_SC - set-left-and-right-margins or save-cursor
         *
         * TODO: Detect save-cursor and run it. DECSLRM is not worth
         *       implementing.
         */

        return 0;
}

static int screen_DECSMBV(term_screen *screen, const term_seq *seq) {
        /*
         * DECSMBV - set-margin-bell-volume
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECSMKR(term_screen *screen, const term_seq *seq) {
        /*
         * DECSMKR - select-modifier-key-reporting
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECSNLS(term_screen *screen, const term_seq *seq) {
        /*
         * DECSNLS - set-lines-per-screen
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECSPP(term_screen *screen, const term_seq *seq) {
        /*
         * DECSPP - set-port-parameter
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECSPPCS(term_screen *screen, const term_seq *seq) {
        /*
         * DECSPPCS - select-pro-printer-character-set
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECSPRTT(term_screen *screen, const term_seq *seq) {
        /*
         * DECSPRTT - select-printer-type
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECSR(term_screen *screen, const term_seq *seq) {
        /*
         * DECSR - secure-reset
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECSRFR(term_screen *screen, const term_seq *seq) {
        /*
         * DECSRFR - select-refresh-rate
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECSSCLS(term_screen *screen, const term_seq *seq) {
        /*
         * DECSSCLS - set-scroll-speed
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECSSDT(term_screen *screen, const term_seq *seq) {
        /*
         * DECSSDT - select-status-display-line-type
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECSSL(term_screen *screen, const term_seq *seq) {
        /*
         * DECSSL - select-setup-language
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECST8C(term_screen *screen, const term_seq *seq) {
        /*
         * DECST8C - set-tab-at-every-8-columns
         * Clear the tab-ruler and reset it to a tab at every 8th column,
         * starting at 9 (though, setting a tab at 1 is fine as it has no
         * effect).
         */

        unsigned int i;

        for (i = 0; i < screen->page->width; i += 8)
                screen->tabs[i / 8] = 0x1;

        return 0;
}

static int screen_DECSTBM(term_screen *screen, const term_seq *seq) {
        /*
         * DECSTBM - set-top-and-bottom-margins
         * This control function sets the top and bottom margins for the current
         * page. You cannot perform scrolling outside the margins.
         *
         * @args[0] defines the top margin, @args[1] defines the bottom margin.
         * The bottom margin must be lower than the top-margin.
         *
         * This call resets the cursor position to 0/0 of the page.
         *
         * Defaults:
         *   args[0]: 1
         *   args[1]: last page-line
         */

        unsigned int top, bottom;

        top = 1;
        bottom = screen->page->height;

        if (seq->args[0] > 0)
                top = seq->args[0];
        if (seq->args[1] > 0)
                bottom = seq->args[1];

        if (top > screen->page->height)
                top = screen->page->height;
        if (bottom > screen->page->height)
                bottom = screen->page->height;

        if (top >= bottom || top > screen->page->height || bottom > screen->page->height) {
                top = 1;
                bottom = screen->page->height;
        }

        term_page_set_scroll_region(screen->page, top - 1, bottom - top + 1);
        screen_cursor_clear_wrap(screen);
        screen_cursor_set(screen, 0, 0);

        return 0;
}

static int screen_DECSTR(term_screen *screen, const term_seq *seq) {
        /*
         * DECSTR - soft-terminal-reset
         * Perform a soft reset to the default values.
         */

        term_screen_soft_reset(screen);

        return 0;
}

static int screen_DECSTRL(term_screen *screen, const term_seq *seq) {
        /*
         * DECSTRL - set-transmit-rate-limit
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECSWBV(term_screen *screen, const term_seq *seq) {
        /*
         * DECSWBV - set-warning-bell-volume
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECSWL(term_screen *screen, const term_seq *seq) {
        /*
         * DECSWL - single-width-single-height-line
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECTID(term_screen *screen, const term_seq *seq) {
        /*
         * DECTID - select-terminal-id
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECTME(term_screen *screen, const term_seq *seq) {
        /*
         * DECTME - terminal-mode-emulation
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DECTST(term_screen *screen, const term_seq *seq) {
        /*
         * DECTST - invoke-confidence-test
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_DL(term_screen *screen, const term_seq *seq) {
        /*
         * DL - delete-line
         * This control function deletes one or more lines in the scrolling
         * region, starting with the line that has the cursor. @args[0] defines
         * the number of lines to delete. 0 is treated the same as 1.
         * As lines are deleted, lines below the cursor and in the scrolling
         * region move up. The terminal adds blank lines with no visual
         * character attributes at the bottom of the scrolling region. If it is
         * greater than the number of lines remaining on the page, DL deletes
         * only the remaining lines. DL has no effect outside the scrolling
         * margins.
         *
         * Defaults:
         *   args[0]: 1
         */

        unsigned int num = 1;

        if (seq->args[0] > 0)
                num = seq->args[0];

        term_page_delete_lines(screen->page, screen->state.cursor_y, num, &screen->state.attr, screen->age);

        return 0;
}

static int screen_DSR_ANSI(term_screen *screen, const term_seq *seq) {
        /*
         * DSR_ANSI - device-status-report-ansi
         *
         * TODO: implement
         */

        return 0;
}

static int screen_DSR_DEC(term_screen *screen, const term_seq *seq) {
        /*
         * DSR_DEC - device-status-report-dec
         *
         * TODO: implement
         */

        return 0;
}

static int screen_ECH(term_screen *screen, const term_seq *seq) {
        /*
         * ECH - erase-character
         * This control function erases one or more characters, from the cursor
         * position to the right. ECH clears character attributes from erased
         * character positions. ECH works inside or outside the scrolling
         * margins.
         * @args[0] defines the number of characters to erase. 0 is treated the
         * same as 1.
         *
         * Defaults:
         *   args[0]: 1
         */

        unsigned int num = 1;

        if (seq->args[0] > 0)
                num = seq->args[0];

        term_page_erase(screen->page,
                        screen->state.cursor_x, screen->state.cursor_y,
                        screen->state.cursor_x + num, screen->state.cursor_y,
                        &screen->state.attr, screen->age, false);

        return 0;
}

static int screen_ED(term_screen *screen, const term_seq *seq) {
        /*
         * ED - erase-in-display
         * This control function erases characters from part or all of the
         * display. When you erase complete lines, they become single-height,
         * single-width lines, with all visual character attributes cleared. ED
         * works inside or outside the scrolling margins.
         *
         * @args[0] defines the region to erase. 0 means from cursor (inclusive)
         * till the end of the screen. 1 means from the start of the screen till
         * the cursor (inclusive) and 2 means the whole screen.
         *
         * Defaults:
         *   args[0]: 0
         */

        unsigned int mode = 0;

        if (seq->args[0] > 0)
                mode = seq->args[0];

        switch (mode) {
        case 0:
                term_page_erase(screen->page,
                                screen->state.cursor_x, screen->state.cursor_y,
                                screen->page->width, screen->page->height,
                                &screen->state.attr, screen->age, false);
                break;
        case 1:
                term_page_erase(screen->page,
                                0, 0,
                                screen->state.cursor_x, screen->state.cursor_y,
                                &screen->state.attr, screen->age, false);
                break;
        case 2:
                term_page_erase(screen->page,
                                0, 0,
                                screen->page->width, screen->page->height,
                                &screen->state.attr, screen->age, false);
                break;
        }

        return 0;
}

static int screen_EL(term_screen *screen, const term_seq *seq) {
        /*
         * EL - erase-in-line
         * This control function erases characters on the line that has the
         * cursor. EL clears all character attributes from erased character
         * positions. EL works inside or outside the scrolling margins.
         *
         * @args[0] defines the region to erase. 0 means from cursor (inclusive)
         * till the end of the line. 1 means from the start of the line till the
         * cursor (inclusive) and 2 means the whole line.
         *
         * Defaults:
         *   args[0]: 0
         */

        unsigned int mode = 0;

        if (seq->args[0] > 0)
                mode = seq->args[0];

        switch (mode) {
        case 0:
                term_page_erase(screen->page,
                                screen->state.cursor_x, screen->state.cursor_y,
                                screen->page->width, screen->state.cursor_y,
                                &screen->state.attr, screen->age, false);
                break;
        case 1:
                term_page_erase(screen->page,
                                0, screen->state.cursor_y,
                                screen->state.cursor_x, screen->state.cursor_y,
                                &screen->state.attr, screen->age, false);
                break;
        case 2:
                term_page_erase(screen->page,
                                0, screen->state.cursor_y,
                                screen->page->width, screen->state.cursor_y,
                                &screen->state.attr, screen->age, false);
                break;
        }

        return 0;
}

static int screen_ENQ(term_screen *screen, const term_seq *seq) {
        /*
         * ENQ - enquiry
         * Transmit the answerback-string. If none is set, do nothing.
         */

        if (screen->answerback)
                return screen_write(screen, screen->answerback, strlen(screen->answerback));

        return 0;
}

static int screen_EPA(term_screen *screen, const term_seq *seq) {
        /*
         * EPA - end-of-guarded-area
         *
         * TODO: What is this?
         */

        return 0;
}

static int screen_FF(term_screen *screen, const term_seq *seq) {
        /*
         * FF - form-feed
         * This causes the cursor to jump to the next line. It is treated the
         * same as LF.
         */

        return screen_LF(screen, seq);
}

static int screen_HPA(term_screen *screen, const term_seq *seq) {
        /*
         * HPA - horizontal-position-absolute
         * HPA causes the active position to be moved to the n-th horizontal
         * position of the active line. If an attempt is made to move the active
         * position past the last position on the line, then the active position
         * stops at the last position on the line.
         *
         * @args[0] defines the horizontal position. 0 is treated as 1.
         *
         * Defaults:
         *   args[0]: 1
         */

        unsigned int num = 1;

        if (seq->args[0] > 0)
                num = seq->args[0];

        screen_cursor_clear_wrap(screen);
        screen_cursor_set(screen, num - 1, screen->state.cursor_y);

        return 0;
}

static int screen_HPR(term_screen *screen, const term_seq *seq) {
        /*
         * HPR - horizontal-position-relative
         * HPR causes the active position to be moved to the n-th following
         * horizontal position of the active line. If an attempt is made to move
         * the active position past the last position on the line, then the
         * active position stops at the last position on the line.
         *
         * @args[0] defines the horizontal position. 0 is treated as 1.
         *
         * Defaults:
         *   args[0]: 1
         */

        unsigned int num = 1;

        if (seq->args[0] > 0)
                num = seq->args[0];

        screen_cursor_clear_wrap(screen);
        screen_cursor_right(screen, num);

        return 0;
}

static int screen_HT(term_screen *screen, const term_seq *seq) {
        /*
         * HT - horizontal-tab
         * Moves the cursor to the next tab stop. If there are no more tab
         * stops, the cursor moves to the right margin. HT does not cause text
         * to auto wrap.
         */

        screen_cursor_clear_wrap(screen);
        screen_cursor_right_tab(screen, 1);

        return 0;
}

static int screen_HTS(term_screen *screen, const term_seq *seq) {
        /*
         * HTS - horizontal-tab-set
         * HTS sets a horizontal tab stop at the column position indicated by
         * the value of the active column when the terminal receives an HTS.
         *
         * Executing an HTS does not effect the other horizontal tab stop
         * settings.
         */

        unsigned int pos;

        pos = screen->state.cursor_x;
        if (screen->page->width > 0)
                screen->tabs[pos / 8] |= 1U << (pos % 8);

        return 0;
}

static int screen_HVP(term_screen *screen, const term_seq *seq) {
        /*
         * HVP - horizontal-and-vertical-position
         * This control function works the same as the cursor position (CUP)
         * function. Origin mode (DECOM) selects line numbering and the ability
         * to move the cursor into margins.
         *
         * Defaults:
         *   args[0]: 1
         *   args[1]: 1
         */

        return screen_CUP(screen, seq);
}

static int screen_ICH(term_screen *screen, const term_seq *seq) {
        /*
         * ICH - insert-character
         * This control function inserts one or more space (SP) characters
         * starting at the cursor position. @args[0] is the number of characters
         * to insert. 0 is treated as 1.
         *
         * The ICH sequence inserts blank characters with the normal
         * character attribute. The cursor remains at the beginning of the blank
         * characters. Text between the cursor and right margin moves to the
         * right. Characters scrolled past the right margin are lost. ICH has no
         * effect outside the scrolling margins.
         *
         * Defaults:
         *   args[0]: 1
         */

        unsigned int num = 1;

        if (seq->args[0] > 0)
                num = seq->args[0];

        screen_cursor_clear_wrap(screen);
        term_page_insert_cells(screen->page, screen->state.cursor_x, screen->state.cursor_y, num, &screen->state.attr, screen->age);

        return 0;
}

static int screen_IL(term_screen *screen, const term_seq *seq) {
        /*
         * IL - insert-line
         * This control function inserts one or more blank lines, starting at
         * the cursor. @args[0] is the number of lines to insert. 0 is treated
         * as 1.
         *
         * As lines are inserted, lines below the cursor and in the scrolling
         * region move down. Lines scrolled off the page are lost. IL has no
         * effect outside the page margins.
         *
         * Defaults:
         *   args[0]: 1
         */

        unsigned int num = 1;

        if (seq->args[0] > 0)
                num = seq->args[0];

        screen_cursor_clear_wrap(screen);
        term_page_insert_lines(screen->page, screen->state.cursor_y, num, &screen->state.attr, screen->age);

        return 0;
}

static int screen_IND(term_screen *screen, const term_seq *seq) {
        /*
         * IND - index
         * IND moves the cursor down one line in the same column. If the cursor
         * is at the bottom margin, then the screen performs a scroll-up.
         */

        screen_cursor_down(screen, 1, true);

        return 0;
}

static int screen_LF(term_screen *screen, const term_seq *seq) {
        /*
         * LF - line-feed
         * Causes a line feed or a new line operation, depending on the setting
         * of line feed/new line mode.
         */

        screen_cursor_down(screen, 1, true);
        if (screen->flags & TERM_FLAG_NEWLINE_MODE)
                screen_cursor_left(screen, screen->state.cursor_x);

        return 0;
}

static int screen_LS1R(term_screen *screen, const term_seq *seq) {
        /*
         * LS1R - locking-shift-1-right
         * Map G1 into GR.
         */

        screen->state.gr = &screen->g1;

        return 0;
}

static int screen_LS2(term_screen *screen, const term_seq *seq) {
        /*
         * LS2 - locking-shift-2
         * Map G2 into GL.
         */

        screen->state.gl = &screen->g2;

        return 0;
}

static int screen_LS2R(term_screen *screen, const term_seq *seq) {
        /*
         * LS2R - locking-shift-2-right
         * Map G2 into GR.
         */

        screen->state.gr = &screen->g2;

        return 0;
}

static int screen_LS3(term_screen *screen, const term_seq *seq) {
        /*
         * LS3 - locking-shift-3
         * Map G3 into GL.
         */

        screen->state.gl = &screen->g3;

        return 0;
}

static int screen_LS3R(term_screen *screen, const term_seq *seq) {
        /*
         * LS3R - locking-shift-3-right
         * Map G3 into GR.
         */

        screen->state.gr = &screen->g3;

        return 0;
}

static int screen_MC_ANSI(term_screen *screen, const term_seq *seq) {
        /*
         * MC_ANSI - media-copy-ansi
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_MC_DEC(term_screen *screen, const term_seq *seq) {
        /*
         * MC_DEC - media-copy-dec
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_NEL(term_screen *screen, const term_seq *seq) {
        /*
         * NEL - next-line
         * Moves cursor to first position on next line. If cursor is at bottom
         * margin, then screen performs a scroll-up.
         */

        screen_cursor_clear_wrap(screen);
        screen_cursor_down(screen, 1, true);
        screen_cursor_set(screen, 0, screen->state.cursor_y);

        return 0;
}

static int screen_NP(term_screen *screen, const term_seq *seq) {
        /*
         * NP - next-page
         * This control function moves the cursor forward to the home position
         * on one of the following pages in page memory. If there is only one
         * page, then the terminal ignores NP.
         * If NP tries to move the cursor past the last page in memory, then the
         * cursor stops at the last page.
         *
         * @args[0] defines the number of pages to forward. 0 is treated as 1.
         *
         * Defaults:
         *   args[0]: 1
         *
         * Probably not worth implementing. We only support a single page.
         */

        return 0;
}

static int screen_NULL(term_screen *screen, const term_seq *seq) {
        /*
         * NULL - null
         * The NULL operation does nothing. ASCII NULL is always ignored.
         */

        return 0;
}

static int screen_PP(term_screen *screen, const term_seq *seq) {
        /*
         * PP - preceding-page
         * This control function moves the cursor backward to the home position
         * on one of the preceding pages in page memory. If there is only one
         * page, then the terminal ignores PP.
         * If PP tries to move the cursor back farther than the first page in
         * memory, then the cursor stops at the first page.
         *
         * @args[0] defines the number of pages to go backwards. 0 is treated
         * as 1.
         *
         * Defaults:
         *   args[0]: 1
         *
         * Probably not worth implementing. We only support a single page.
         */

        return 0;
}

static int screen_PPA(term_screen *screen, const term_seq *seq) {
        /*
         * PPA - page-position-absolute
         * This control function can move the cursor to the corresponding row
         * and column on any page in page memory. You select the page by its
         * number. If there is only one page, then the terminal ignores PPA.
         *
         * @args[0] is the number of the page to move the cursor to. If it is
         * greater than the number of the last page in memory, then the cursor
         * stops at the last page. If it is less than the number of the first
         * page, then the cursor stops at the first page.
         *
         * Defaults:
         *   args[0]: 1
         *
         * Probably not worth implementing. We only support a single page.
         */

        return 0;
}

static int screen_PPB(term_screen *screen, const term_seq *seq) {
        /*
         * PPB - page-position-backward
         * This control function moves the cursor backward to the corresponding
         * row and column on one of the preceding pages in page memory. If there
         * is only one page, then the terminal ignores PPB.
         *
         * @args[0] indicates the number of pages to move the cursor backward.
         * If it tries to move the cursor back farther than the first page in
         * memory, then the cursor stops at the first page. 0 is treated as 1.
         *
         * Defaults:
         *   args[0]: 1
         *
         * Probably not worth implementing. We only support a single page.
         */

        return 0;
}

static int screen_PPR(term_screen *screen, const term_seq *seq) {
        /*
         * PPR - page-position-relative
         * This control function moves the cursor forward to the corresponding
         * row and column on one of the following pages in page memory. If there
         * is only one page, then the terminal ignores PPR.
         *
         * @args[0] indicates how many pages to move the cursor forward. If it
         * tries to move the cursor beyond the last page in memory, then the
         * cursor stops at the last page. 0 is treated as 1.
         *
         * Defaults:
         *   args[0]: 1
         *
         * Probably not worth implementing. We only support a single page.
         */

        return 0;
}

static int screen_RC(term_screen *screen, const term_seq *seq) {
        /*
         * RC - restore-cursor
         */

        return screen_DECRC(screen, seq);
}

static int screen_REP(term_screen *screen, const term_seq *seq) {
        /*
         * REP - repeat
         * Repeat the preceding graphics-character the given number of times.
         * @args[0] specifies how often it shall be repeated. 0 is treated as 1.
         *
         * Defaults:
         *   args[0]: 1
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_RI(term_screen *screen, const term_seq *seq) {
        /*
         * RI - reverse-index
         * Moves the cursor up one line in the same column. If the cursor is at
         * the top margin, the page scrolls down.
         */

        screen_cursor_up(screen, 1, true);

        return 0;
}

static int screen_RIS(term_screen *screen, const term_seq *seq) {
        /*
         * RIS - reset-to-initial-state
         * This control function causes a nonvolatile memory (NVR) recall to
         * occur. RIS replaces all set-up features with their saved settings.
         *
         * The terminal stores these saved settings in NVR memory. The saved
         * setting for a feature is the same as the factory-default setting,
         * unless you saved a new setting.
         */

        term_screen_hard_reset(screen);

        return 0;
}

static int screen_RM_ANSI(term_screen *screen, const term_seq *seq) {
        /*
         * RM_ANSI - reset-mode-ansi
         *
         * TODO: implement (see VT510rm manual)
         */

        unsigned int i;

        for (i = 0; i < seq->n_args; ++i)
                screen_mode_change_ansi(screen, seq->args[i], false);

        return 0;
}

static int screen_RM_DEC(term_screen *screen, const term_seq *seq) {
        /*
         * RM_DEC - reset-mode-dec
         * This is the same as RM_ANSI but for DEC modes.
         */

        unsigned int i;

        for (i = 0; i < seq->n_args; ++i)
                screen_mode_change_dec(screen, seq->args[i], false);

        return 0;
}

static int screen_S7C1T(term_screen *screen, const term_seq *seq) {
        /*
         * S7C1T - set-7bit-c1-terminal
         * This causes the terminal to start sending C1 controls as 7bit
         * sequences instead of 8bit C1 controls.
         * This is ignored if the terminal is below level-2 emulation mode
         * (VT100 and below), the terminal already sends 7bit controls then.
         */

        if (screen->conformance_level > TERM_CONFORMANCE_LEVEL_VT100)
                screen->flags |= TERM_FLAG_7BIT_MODE;

        return 0;
}

static int screen_S8C1T(term_screen *screen, const term_seq *seq) {
        /*
         * S8C1T - set-8bit-c1-terminal
         * This causes the terminal to start sending C1 controls as 8bit C1
         * control instead of 7bit sequences.
         * This is ignored if the terminal is below level-2 emulation mode
         * (VT100 and below). The terminal always sends 7bit controls in those
         * modes.
         */

        if (screen->conformance_level > TERM_CONFORMANCE_LEVEL_VT100)
                screen->flags &= ~TERM_FLAG_7BIT_MODE;

        return 0;
}

static int screen_SCS(term_screen *screen, const term_seq *seq) {
        /*
         * SCS - select-character-set
         * Designate character sets to G-sets. The mapping from intermediates
         * and terminal characters in the escape sequence to G-sets and
         * character-sets is non-trivial and implemented separately. See there
         * for more information.
         * This call simply sets the selected G-set to the desired
         * character-set.
         */

        term_charset *cs = NULL;

        /* TODO: support more of them? */
        switch (seq->charset) {
        case TERM_CHARSET_ISO_LATIN1_SUPPLEMENTAL:
        case TERM_CHARSET_ISO_LATIN2_SUPPLEMENTAL:
        case TERM_CHARSET_ISO_LATIN5_SUPPLEMENTAL:
        case TERM_CHARSET_ISO_GREEK_SUPPLEMENTAL:
        case TERM_CHARSET_ISO_HEBREW_SUPPLEMENTAL:
        case TERM_CHARSET_ISO_LATIN_CYRILLIC:
                break;

        case TERM_CHARSET_DEC_SPECIAL_GRAPHIC:
                cs = &term_dec_special_graphics;
                break;
        case TERM_CHARSET_DEC_SUPPLEMENTAL:
                cs = &term_dec_supplemental_graphics;
                break;
        case TERM_CHARSET_DEC_TECHNICAL:
        case TERM_CHARSET_CYRILLIC_DEC:
        case TERM_CHARSET_DUTCH_NRCS:
        case TERM_CHARSET_FINNISH_NRCS:
        case TERM_CHARSET_FRENCH_NRCS:
        case TERM_CHARSET_FRENCH_CANADIAN_NRCS:
        case TERM_CHARSET_GERMAN_NRCS:
        case TERM_CHARSET_GREEK_DEC:
        case TERM_CHARSET_GREEK_NRCS:
        case TERM_CHARSET_HEBREW_DEC:
        case TERM_CHARSET_HEBREW_NRCS:
        case TERM_CHARSET_ITALIAN_NRCS:
        case TERM_CHARSET_NORWEGIAN_DANISH_NRCS:
        case TERM_CHARSET_PORTUGUESE_NRCS:
        case TERM_CHARSET_RUSSIAN_NRCS:
        case TERM_CHARSET_SCS_NRCS:
        case TERM_CHARSET_SPANISH_NRCS:
        case TERM_CHARSET_SWEDISH_NRCS:
        case TERM_CHARSET_SWISS_NRCS:
        case TERM_CHARSET_TURKISH_DEC:
        case TERM_CHARSET_TURKISH_NRCS:
                break;

        case TERM_CHARSET_USERPREF_SUPPLEMENTAL:
                break;
        }

        if (seq->intermediates & TERM_SEQ_FLAG_POPEN)
                screen->g0 = cs ? : &term_unicode_lower;
        else if (seq->intermediates & TERM_SEQ_FLAG_PCLOSE)
                screen->g1 = cs ? : &term_unicode_upper;
        else if (seq->intermediates & TERM_SEQ_FLAG_MULT)
                screen->g2 = cs ? : &term_unicode_lower;
        else if (seq->intermediates & TERM_SEQ_FLAG_PLUS)
                screen->g3 = cs ? : &term_unicode_upper;
        else if (seq->intermediates & TERM_SEQ_FLAG_MINUS)
                screen->g1 = cs ? : &term_unicode_upper;
        else if (seq->intermediates & TERM_SEQ_FLAG_DOT)
                screen->g2 = cs ? : &term_unicode_lower;
        else if (seq->intermediates & TERM_SEQ_FLAG_SLASH)
                screen->g3 = cs ? : &term_unicode_upper;

        return 0;
}

static int screen_SD(term_screen *screen, const term_seq *seq) {
        /*
         * SD - scroll-down
         * This control function moves the user window down a specified number
         * of lines in page memory.
         * @args[0] is the number of lines to move the
         * user window up in page memory. New lines appear at the top of the
         * display. Old lines disappear at the bottom of the display. You
         * cannot pan past the top margin of the current page. 0 is treated
         * as 1.
         *
         * Defaults:
         *   args[0]: 1
         */

        unsigned int num = 1;

        if (seq->args[0] > 0)
                num = seq->args[0];

        term_page_scroll_down(screen->page, num, &screen->state.attr, screen->age, NULL);

        return 0;
}

static int screen_SGR(term_screen *screen, const term_seq *seq) {
        /*
         * SGR - select-graphics-rendition
         */

        term_color *dst;
        unsigned int i, code;
        int v;

        if (seq->n_args < 1) {
                zero(screen->state.attr);
                return 0;
        }

        for (i = 0; i < seq->n_args; ++i) {
                v = seq->args[i];
                switch (v) {
                case 1:
                        screen->state.attr.bold = 1;
                        break;
                case 3:
                        screen->state.attr.italic = 1;
                        break;
                case 4:
                        screen->state.attr.underline = 1;
                        break;
                case 5:
                        screen->state.attr.blink = 1;
                        break;
                case 7:
                        screen->state.attr.inverse = 1;
                        break;
                case 8:
                        screen->state.attr.hidden = 1;
                        break;
                case 22:
                        screen->state.attr.bold = 0;
                        break;
                case 23:
                        screen->state.attr.italic = 0;
                        break;
                case 24:
                        screen->state.attr.underline = 0;
                        break;
                case 25:
                        screen->state.attr.blink = 0;
                        break;
                case 27:
                        screen->state.attr.inverse = 0;
                        break;
                case 28:
                        screen->state.attr.hidden = 0;
                        break;
                case 30 ... 37:
                        screen->state.attr.fg.ccode = v - 30 + TERM_CCODE_BLACK;
                        break;
                case 39:
                        screen->state.attr.fg.ccode = 0;
                        break;
                case 40 ... 47:
                        screen->state.attr.bg.ccode = v - 40 + TERM_CCODE_BLACK;
                        break;
                case 49:
                        screen->state.attr.bg.ccode = 0;
                        break;
                case 90 ... 97:
                        screen->state.attr.fg.ccode = v - 90 + TERM_CCODE_LIGHT_BLACK;
                        break;
                case 100 ... 107:
                        screen->state.attr.bg.ccode = v - 100 + TERM_CCODE_LIGHT_BLACK;
                        break;
                case 38:
                        /* fallthrough */
                case 48:

                        if (v == 38)
                                dst = &screen->state.attr.fg;
                        else
                                dst = &screen->state.attr.bg;

                        ++i;
                        if (i >= seq->n_args)
                                break;

                        switch (seq->args[i]) {
                        case 2:
                                /* 24bit-color support */

                                i += 3;
                                if (i >= seq->n_args)
                                        break;

                                dst->ccode = TERM_CCODE_RGB;
                                dst->red = (seq->args[i - 2] >= 0) ? seq->args[i - 2] : 0;
                                dst->green = (seq->args[i - 1] >= 0) ? seq->args[i - 1] : 0;
                                dst->blue = (seq->args[i] >= 0) ? seq->args[i] : 0;

                                break;
                        case 5:
                                /* 256-color support */

                                ++i;
                                if (i >= seq->n_args || seq->args[i] < 0)
                                        break;

                                dst->ccode = TERM_CCODE_256;
                                code = seq->args[i];
                                dst->c256 = code < 256 ? code : 0;

                                break;
                        }

                        break;
                case -1:
                        /* fallthrough */
                case 0:
                        zero(screen->state.attr);
                        break;
                }
        }

        return 0;
}

static int screen_SI(term_screen *screen, const term_seq *seq) {
        /*
         * SI - shift-in
         * Map G0 into GL.
         */

        screen->state.gl = &screen->g0;

        return 0;
}

static int screen_SM_ANSI(term_screen *screen, const term_seq *seq) {
        /*
         * SM_ANSI - set-mode-ansi
         *
         * TODO: implement
         */

        unsigned int i;

        for (i = 0; i < seq->n_args; ++i)
                screen_mode_change_ansi(screen, seq->args[i], true);

        return 0;
}

static int screen_SM_DEC(term_screen *screen, const term_seq *seq) {
        /*
         * SM_DEC - set-mode-dec
         * This is the same as SM_ANSI but for DEC modes.
         */

        unsigned int i;

        for (i = 0; i < seq->n_args; ++i)
                screen_mode_change_dec(screen, seq->args[i], true);

        return 0;
}

static int screen_SO(term_screen *screen, const term_seq *seq) {
        /*
         * SO - shift-out
         * Map G1 into GL.
         */

        screen->state.gl = &screen->g1;

        return 0;
}

static int screen_SPA(term_screen *screen, const term_seq *seq) {
        /*
         * SPA - start-of-protected-area
         *
         * TODO: What is this?
         */

        return 0;
}

static int screen_SS2(term_screen *screen, const term_seq *seq) {
        /*
         * SS2 - single-shift-2
         * Temporarily map G2 into GL for the next graphics character.
         */

        screen->state.glt = &screen->g2;

        return 0;
}

static int screen_SS3(term_screen *screen, const term_seq *seq) {
        /*
         * SS3 - single-shift-3
         * Temporarily map G3 into GL for the next graphics character
         */

        screen->state.glt = &screen->g3;

        return 0;
}

static int screen_ST(term_screen *screen, const term_seq *seq) {
        /*
         * ST - string-terminator
         * The string-terminator is usually part of control-sequences and
         * handled by the parser. In all other situations it is silently
         * ignored.
         */

        return 0;
}

static int screen_SU(term_screen *screen, const term_seq *seq) {
        /*
         * SU - scroll-up
         * This control function moves the user window up a specified number of
         * lines in page memory.
         * @args[0] is the number of lines to move the
         * user window down in page memory. New lines appear at the bottom of
         * the display. Old lines disappear at the top of the display. You
         * cannot pan past the bottom margin of the current page. 0 is treated
         * as 1.
         *
         * Defaults:
         *   args[0]: 1
         */

        unsigned int num = 1;

        if (seq->args[0] > 0)
                num = seq->args[0];

        term_page_scroll_up(screen->page, num, &screen->state.attr, screen->age, screen->history);

        return 0;
}

static int screen_SUB(term_screen *screen, const term_seq *seq) {
        /*
         * SUB - substitute
         * Cancel the current control-sequence and print a replacement
         * character. Our parser already handles this so all we have to do is
         * print the replacement character.
         */

        static const term_seq rep = {
                .type = TERM_SEQ_GRAPHIC,
                .command = TERM_CMD_GRAPHIC,
                .terminator = 0xfffd,
        };

        return screen_GRAPHIC(screen, &rep);
}

static int screen_TBC(term_screen *screen, const term_seq *seq) {
        /*
         * TBC - tab-clear
         * This clears tab-stops. If @args[0] is 0, the tab-stop at the current
         * cursor position is cleared. If it is 3, all tab stops are cleared.
         *
         * Defaults:
         *   args[0]: 0
         */

        unsigned int mode = 0, pos;

        if (seq->args[0] > 0)
                mode = seq->args[0];

        switch (mode) {
        case 0:
                pos = screen->state.cursor_x;
                if (screen->page->width > 0)
                        screen->tabs[pos / 8] &= ~(1U << (pos % 8));
                break;
        case 3:
                if (screen->page->width > 0)
                        memzero(screen->tabs, (screen->page->width + 7) / 8);
                break;
        }

        return 0;
}

static int screen_VPA(term_screen *screen, const term_seq *seq) {
        /*
         * VPA - vertical-line-position-absolute
         * VPA causes the active position to be moved to the corresponding
         * horizontal position. @args[0] specifies the line to jump to. If an
         * attempt is made to move the active position below the last line, then
         * the active position stops on the last line. 0 is treated as 1.
         *
         * Defaults:
         *   args[0]: 1
         */

        unsigned int pos = 1;

        if (seq->args[0] > 0)
                pos = seq->args[0];

        screen_cursor_clear_wrap(screen);
        screen_cursor_set_rel(screen, screen->state.cursor_x, pos - 1);

        return 0;
}

static int screen_VPR(term_screen *screen, const term_seq *seq) {
        /*
         * VPR - vertical-line-position-relative
         * VPR causes the active position to be moved to the corresponding
         * horizontal position. @args[0] specifies the number of lines to jump
         * down relative to the current cursor position. If an attempt is made
         * to move the active position below the last line, the active position
         * stops at the last line. 0 is treated as 1.
         *
         * Defaults:
         *   args[0]: 1
         */

        unsigned int num = 1;

        if (seq->args[0] > 0)
                num = seq->args[0];

        screen_cursor_clear_wrap(screen);
        screen_cursor_down(screen, num, false);

        return 0;
}

static int screen_VT(term_screen *screen, const term_seq *seq) {
        /*
         * VT - vertical-tab
         * This causes a vertical jump by one line. Terminals treat it exactly
         * the same as LF.
         */

        return screen_LF(screen, seq);
}

static int screen_XTERM_CLLHP(term_screen *screen, const term_seq *seq) {
        /*
         * XTERM_CLLHP - xterm-cursor-lower-left-hp-bugfix
         * Move the cursor to the lower-left corner of the page. This is an HP
         * bugfix by xterm.
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_XTERM_IHMT(term_screen *screen, const term_seq *seq) {
        /*
         * XTERM_IHMT - xterm-initiate-highlight-mouse-tracking
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_XTERM_MLHP(term_screen *screen, const term_seq *seq) {
        /*
         * XTERM_MLHP - xterm-memory-lock-hp-bugfix
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_XTERM_MUHP(term_screen *screen, const term_seq *seq) {
        /*
         * XTERM_MUHP - xterm-memory-unlock-hp-bugfix
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_XTERM_RPM(term_screen *screen, const term_seq *seq) {
        /*
         * XTERM_RPM - xterm-restore-private-mode
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_XTERM_RRV(term_screen *screen, const term_seq *seq) {
        /*
         * XTERM_RRV - xterm-reset-resource-value
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_XTERM_RTM(term_screen *screen, const term_seq *seq) {
        /*
         * XTERM_RTM - xterm-reset-title-mode
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_XTERM_SACL1(term_screen *screen, const term_seq *seq) {
        /*
         * XTERM_SACL1 - xterm-set-ansi-conformance-level-1
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_XTERM_SACL2(term_screen *screen, const term_seq *seq) {
        /*
         * XTERM_SACL2 - xterm-set-ansi-conformance-level-2
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_XTERM_SACL3(term_screen *screen, const term_seq *seq) {
        /*
         * XTERM_SACL3 - xterm-set-ansi-conformance-level-3
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_XTERM_SDCS(term_screen *screen, const term_seq *seq) {
        /*
         * XTERM_SDCS - xterm-set-default-character-set
         * Select the default character set. We treat this the same as UTF-8 as
         * this is our default character set. As we always use UTF-8, this
         * becomes as no-op.
         */

        return 0;
}

static int screen_XTERM_SGFX(term_screen *screen, const term_seq *seq) {
        /*
         * XTERM_SGFX - xterm-sixel-graphics
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_XTERM_SPM(term_screen *screen, const term_seq *seq) {
        /*
         * XTERM_SPM - xterm-set-private-mode
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_XTERM_SRV(term_screen *screen, const term_seq *seq) {
        /*
         * XTERM_SRV - xterm-set-resource-value
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_XTERM_STM(term_screen *screen, const term_seq *seq) {
        /*
         * XTERM_STM - xterm-set-title-mode
         *
         * Probably not worth implementing.
         */

        return 0;
}

static int screen_XTERM_SUCS(term_screen *screen, const term_seq *seq) {
        /*
         * XTERM_SUCS - xterm-select-utf8-character-set
         * Select UTF-8 as character set. This is our default on only character
         * set. Hence, this is a no-op.
         */

        return 0;
}

static int screen_XTERM_WM(term_screen *screen, const term_seq *seq) {
        /*
         * XTERM_WM - xterm-window-management
         *
         * Probably not worth implementing.
         */

        return 0;
}

/*
 * Feeding data
 * The screen_feed_*() handlers take data from the user and feed it into the
 * screen. Once the parser has detected a sequence, we parse the command-type
 * and forward it to the command-dispatchers.
 */

static int screen_feed_cmd(term_screen *screen, const term_seq *seq) {
        switch (seq->command) {
        case TERM_CMD_GRAPHIC:
                return screen_GRAPHIC(screen, seq);
        case TERM_CMD_BEL:
                return screen_BEL(screen, seq);
        case TERM_CMD_BS:
                return screen_BS(screen, seq);
        case TERM_CMD_CBT:
                return screen_CBT(screen, seq);
        case TERM_CMD_CHA:
                return screen_CHA(screen, seq);
        case TERM_CMD_CHT:
                return screen_CHT(screen, seq);
        case TERM_CMD_CNL:
                return screen_CNL(screen, seq);
        case TERM_CMD_CPL:
                return screen_CPL(screen, seq);
        case TERM_CMD_CR:
                return screen_CR(screen, seq);
        case TERM_CMD_CUB:
                return screen_CUB(screen, seq);
        case TERM_CMD_CUD:
                return screen_CUD(screen, seq);
        case TERM_CMD_CUF:
                return screen_CUF(screen, seq);
        case TERM_CMD_CUP:
                return screen_CUP(screen, seq);
        case TERM_CMD_CUU:
                return screen_CUU(screen, seq);
        case TERM_CMD_DA1:
                return screen_DA1(screen, seq);
        case TERM_CMD_DA2:
                return screen_DA2(screen, seq);
        case TERM_CMD_DA3:
                return screen_DA3(screen, seq);
        case TERM_CMD_DC1:
                return screen_DC1(screen, seq);
        case TERM_CMD_DC3:
                return screen_DC3(screen, seq);
        case TERM_CMD_DCH:
                return screen_DCH(screen, seq);
        case TERM_CMD_DECALN:
                return screen_DECALN(screen, seq);
        case TERM_CMD_DECANM:
                return screen_DECANM(screen, seq);
        case TERM_CMD_DECBI:
                return screen_DECBI(screen, seq);
        case TERM_CMD_DECCARA:
                return screen_DECCARA(screen, seq);
        case TERM_CMD_DECCRA:
                return screen_DECCRA(screen, seq);
        case TERM_CMD_DECDC:
                return screen_DECDC(screen, seq);
        case TERM_CMD_DECDHL_BH:
                return screen_DECDHL_BH(screen, seq);
        case TERM_CMD_DECDHL_TH:
                return screen_DECDHL_TH(screen, seq);
        case TERM_CMD_DECDWL:
                return screen_DECDWL(screen, seq);
        case TERM_CMD_DECEFR:
                return screen_DECEFR(screen, seq);
        case TERM_CMD_DECELF:
                return screen_DECELF(screen, seq);
        case TERM_CMD_DECELR:
                return screen_DECELR(screen, seq);
        case TERM_CMD_DECERA:
                return screen_DECERA(screen, seq);
        case TERM_CMD_DECFI:
                return screen_DECFI(screen, seq);
        case TERM_CMD_DECFRA:
                return screen_DECFRA(screen, seq);
        case TERM_CMD_DECIC:
                return screen_DECIC(screen, seq);
        case TERM_CMD_DECID:
                return screen_DECID(screen, seq);
        case TERM_CMD_DECINVM:
                return screen_DECINVM(screen, seq);
        case TERM_CMD_DECKBD:
                return screen_DECKBD(screen, seq);
        case TERM_CMD_DECKPAM:
                return screen_DECKPAM(screen, seq);
        case TERM_CMD_DECKPNM:
                return screen_DECKPNM(screen, seq);
        case TERM_CMD_DECLFKC:
                return screen_DECLFKC(screen, seq);
        case TERM_CMD_DECLL:
                return screen_DECLL(screen, seq);
        case TERM_CMD_DECLTOD:
                return screen_DECLTOD(screen, seq);
        case TERM_CMD_DECPCTERM:
                return screen_DECPCTERM(screen, seq);
        case TERM_CMD_DECPKA:
                return screen_DECPKA(screen, seq);
        case TERM_CMD_DECPKFMR:
                return screen_DECPKFMR(screen, seq);
        case TERM_CMD_DECRARA:
                return screen_DECRARA(screen, seq);
        case TERM_CMD_DECRC:
                return screen_DECRC(screen, seq);
        case TERM_CMD_DECREQTPARM:
                return screen_DECREQTPARM(screen, seq);
        case TERM_CMD_DECRPKT:
                return screen_DECRPKT(screen, seq);
        case TERM_CMD_DECRQCRA:
                return screen_DECRQCRA(screen, seq);
        case TERM_CMD_DECRQDE:
                return screen_DECRQDE(screen, seq);
        case TERM_CMD_DECRQKT:
                return screen_DECRQKT(screen, seq);
        case TERM_CMD_DECRQLP:
                return screen_DECRQLP(screen, seq);
        case TERM_CMD_DECRQM_ANSI:
                return screen_DECRQM_ANSI(screen, seq);
        case TERM_CMD_DECRQM_DEC:
                return screen_DECRQM_DEC(screen, seq);
        case TERM_CMD_DECRQPKFM:
                return screen_DECRQPKFM(screen, seq);
        case TERM_CMD_DECRQPSR:
                return screen_DECRQPSR(screen, seq);
        case TERM_CMD_DECRQTSR:
                return screen_DECRQTSR(screen, seq);
        case TERM_CMD_DECRQUPSS:
                return screen_DECRQUPSS(screen, seq);
        case TERM_CMD_DECSACE:
                return screen_DECSACE(screen, seq);
        case TERM_CMD_DECSASD:
                return screen_DECSASD(screen, seq);
        case TERM_CMD_DECSC:
                return screen_DECSC(screen, seq);
        case TERM_CMD_DECSCA:
                return screen_DECSCA(screen, seq);
        case TERM_CMD_DECSCL:
                return screen_DECSCL(screen, seq);
        case TERM_CMD_DECSCP:
                return screen_DECSCP(screen, seq);
        case TERM_CMD_DECSCPP:
                return screen_DECSCPP(screen, seq);
        case TERM_CMD_DECSCS:
                return screen_DECSCS(screen, seq);
        case TERM_CMD_DECSCUSR:
                return screen_DECSCUSR(screen, seq);
        case TERM_CMD_DECSDDT:
                return screen_DECSDDT(screen, seq);
        case TERM_CMD_DECSDPT:
                return screen_DECSDPT(screen, seq);
        case TERM_CMD_DECSED:
                return screen_DECSED(screen, seq);
        case TERM_CMD_DECSEL:
                return screen_DECSEL(screen, seq);
        case TERM_CMD_DECSERA:
                return screen_DECSERA(screen, seq);
        case TERM_CMD_DECSFC:
                return screen_DECSFC(screen, seq);
        case TERM_CMD_DECSKCV:
                return screen_DECSKCV(screen, seq);
        case TERM_CMD_DECSLCK:
                return screen_DECSLCK(screen, seq);
        case TERM_CMD_DECSLE:
                return screen_DECSLE(screen, seq);
        case TERM_CMD_DECSLPP:
                return screen_DECSLPP(screen, seq);
        case TERM_CMD_DECSLRM_OR_SC:
                return screen_DECSLRM_OR_SC(screen, seq);
        case TERM_CMD_DECSMBV:
                return screen_DECSMBV(screen, seq);
        case TERM_CMD_DECSMKR:
                return screen_DECSMKR(screen, seq);
        case TERM_CMD_DECSNLS:
                return screen_DECSNLS(screen, seq);
        case TERM_CMD_DECSPP:
                return screen_DECSPP(screen, seq);
        case TERM_CMD_DECSPPCS:
                return screen_DECSPPCS(screen, seq);
        case TERM_CMD_DECSPRTT:
                return screen_DECSPRTT(screen, seq);
        case TERM_CMD_DECSR:
                return screen_DECSR(screen, seq);
        case TERM_CMD_DECSRFR:
                return screen_DECSRFR(screen, seq);
        case TERM_CMD_DECSSCLS:
                return screen_DECSSCLS(screen, seq);
        case TERM_CMD_DECSSDT:
                return screen_DECSSDT(screen, seq);
        case TERM_CMD_DECSSL:
                return screen_DECSSL(screen, seq);
        case TERM_CMD_DECST8C:
                return screen_DECST8C(screen, seq);
        case TERM_CMD_DECSTBM:
                return screen_DECSTBM(screen, seq);
        case TERM_CMD_DECSTR:
                return screen_DECSTR(screen, seq);
        case TERM_CMD_DECSTRL:
                return screen_DECSTRL(screen, seq);
        case TERM_CMD_DECSWBV:
                return screen_DECSWBV(screen, seq);
        case TERM_CMD_DECSWL:
                return screen_DECSWL(screen, seq);
        case TERM_CMD_DECTID:
                return screen_DECTID(screen, seq);
        case TERM_CMD_DECTME:
                return screen_DECTME(screen, seq);
        case TERM_CMD_DECTST:
                return screen_DECTST(screen, seq);
        case TERM_CMD_DL:
                return screen_DL(screen, seq);
        case TERM_CMD_DSR_ANSI:
                return screen_DSR_ANSI(screen, seq);
        case TERM_CMD_DSR_DEC:
                return screen_DSR_DEC(screen, seq);
        case TERM_CMD_ECH:
                return screen_ECH(screen, seq);
        case TERM_CMD_ED:
                return screen_ED(screen, seq);
        case TERM_CMD_EL:
                return screen_EL(screen, seq);
        case TERM_CMD_ENQ:
                return screen_ENQ(screen, seq);
        case TERM_CMD_EPA:
                return screen_EPA(screen, seq);
        case TERM_CMD_FF:
                return screen_FF(screen, seq);
        case TERM_CMD_HPA:
                return screen_HPA(screen, seq);
        case TERM_CMD_HPR:
                return screen_HPR(screen, seq);
        case TERM_CMD_HT:
                return screen_HT(screen, seq);
        case TERM_CMD_HTS:
                return screen_HTS(screen, seq);
        case TERM_CMD_HVP:
                return screen_HVP(screen, seq);
        case TERM_CMD_ICH:
                return screen_ICH(screen, seq);
        case TERM_CMD_IL:
                return screen_IL(screen, seq);
        case TERM_CMD_IND:
                return screen_IND(screen, seq);
        case TERM_CMD_LF:
                return screen_LF(screen, seq);
        case TERM_CMD_LS1R:
                return screen_LS1R(screen, seq);
        case TERM_CMD_LS2:
                return screen_LS2(screen, seq);
        case TERM_CMD_LS2R:
                return screen_LS2R(screen, seq);
        case TERM_CMD_LS3:
                return screen_LS3(screen, seq);
        case TERM_CMD_LS3R:
                return screen_LS3R(screen, seq);
        case TERM_CMD_MC_ANSI:
                return screen_MC_ANSI(screen, seq);
        case TERM_CMD_MC_DEC:
                return screen_MC_DEC(screen, seq);
        case TERM_CMD_NEL:
                return screen_NEL(screen, seq);
        case TERM_CMD_NP:
                return screen_NP(screen, seq);
        case TERM_CMD_NULL:
                return screen_NULL(screen, seq);
        case TERM_CMD_PP:
                return screen_PP(screen, seq);
        case TERM_CMD_PPA:
                return screen_PPA(screen, seq);
        case TERM_CMD_PPB:
                return screen_PPB(screen, seq);
        case TERM_CMD_PPR:
                return screen_PPR(screen, seq);
        case TERM_CMD_RC:
                return screen_RC(screen, seq);
        case TERM_CMD_REP:
                return screen_REP(screen, seq);
        case TERM_CMD_RI:
                return screen_RI(screen, seq);
        case TERM_CMD_RIS:
                return screen_RIS(screen, seq);
        case TERM_CMD_RM_ANSI:
                return screen_RM_ANSI(screen, seq);
        case TERM_CMD_RM_DEC:
                return screen_RM_DEC(screen, seq);
        case TERM_CMD_S7C1T:
                return screen_S7C1T(screen, seq);
        case TERM_CMD_S8C1T:
                return screen_S8C1T(screen, seq);
        case TERM_CMD_SCS:
                return screen_SCS(screen, seq);
        case TERM_CMD_SD:
                return screen_SD(screen, seq);
        case TERM_CMD_SGR:
                return screen_SGR(screen, seq);
        case TERM_CMD_SI:
                return screen_SI(screen, seq);
        case TERM_CMD_SM_ANSI:
                return screen_SM_ANSI(screen, seq);
        case TERM_CMD_SM_DEC:
                return screen_SM_DEC(screen, seq);
        case TERM_CMD_SO:
                return screen_SO(screen, seq);
        case TERM_CMD_SPA:
                return screen_SPA(screen, seq);
        case TERM_CMD_SS2:
                return screen_SS2(screen, seq);
        case TERM_CMD_SS3:
                return screen_SS3(screen, seq);
        case TERM_CMD_ST:
                return screen_ST(screen, seq);
        case TERM_CMD_SU:
                return screen_SU(screen, seq);
        case TERM_CMD_SUB:
                return screen_SUB(screen, seq);
        case TERM_CMD_TBC:
                return screen_TBC(screen, seq);
        case TERM_CMD_VPA:
                return screen_VPA(screen, seq);
        case TERM_CMD_VPR:
                return screen_VPR(screen, seq);
        case TERM_CMD_VT:
                return screen_VT(screen, seq);
        case TERM_CMD_XTERM_CLLHP:
                return screen_XTERM_CLLHP(screen, seq);
        case TERM_CMD_XTERM_IHMT:
                return screen_XTERM_IHMT(screen, seq);
        case TERM_CMD_XTERM_MLHP:
                return screen_XTERM_MLHP(screen, seq);
        case TERM_CMD_XTERM_MUHP:
                return screen_XTERM_MUHP(screen, seq);
        case TERM_CMD_XTERM_RPM:
                return screen_XTERM_RPM(screen, seq);
        case TERM_CMD_XTERM_RRV:
                return screen_XTERM_RRV(screen, seq);
        case TERM_CMD_XTERM_RTM:
                return screen_XTERM_RTM(screen, seq);
        case TERM_CMD_XTERM_SACL1:
                return screen_XTERM_SACL1(screen, seq);
        case TERM_CMD_XTERM_SACL2:
                return screen_XTERM_SACL2(screen, seq);
        case TERM_CMD_XTERM_SACL3:
                return screen_XTERM_SACL3(screen, seq);
        case TERM_CMD_XTERM_SDCS:
                return screen_XTERM_SDCS(screen, seq);
        case TERM_CMD_XTERM_SGFX:
                return screen_XTERM_SGFX(screen, seq);
        case TERM_CMD_XTERM_SPM:
                return screen_XTERM_SPM(screen, seq);
        case TERM_CMD_XTERM_SRV:
                return screen_XTERM_SRV(screen, seq);
        case TERM_CMD_XTERM_STM:
                return screen_XTERM_STM(screen, seq);
        case TERM_CMD_XTERM_SUCS:
                return screen_XTERM_SUCS(screen, seq);
        case TERM_CMD_XTERM_WM:
                return screen_XTERM_WM(screen, seq);
        }

        return 0;
}

unsigned int term_screen_get_width(term_screen *screen) {
        assert_return(screen, -EINVAL);

        return screen->page->width;
}

unsigned int term_screen_get_height(term_screen *screen) {
        assert_return(screen, -EINVAL);

        return screen->page->height;
}

uint64_t term_screen_get_age(term_screen *screen) {
        assert_return(screen, 0);

        return screen->age;
}

int term_screen_feed_text(term_screen *screen, const uint8_t *in, size_t size) {
        uint32_t *ucs4_str;
        size_t i, j, ucs4_len;
        const term_seq *seq;
        int r;

        assert_return(screen, -EINVAL);

        ++screen->age;

        /* Feed bytes into utf8 decoder and handle parsed ucs4 chars. We always
         * treat data as UTF-8, but the parser makes sure to fall back to raw
         * 8bit mode if the stream is not valid UTF-8. This should be more than
         * enough to support old 7bit/8bit modes. */
        for (i = 0; i < size; ++i) {
                ucs4_len = term_utf8_decode(&screen->utf8, &ucs4_str, in[i]);
                for (j = 0; j < ucs4_len; ++j) {
                        r = term_parser_feed(screen->parser, &seq, ucs4_str[j]);
                        if (r < 0) {
                                return r;
                        } else if (r != TERM_SEQ_NONE) {
                                r = screen_feed_cmd(screen, seq);
                                if (r < 0)
                                        return r;
                        }
                }
        }

        return 0;
}

static char *screen_map_key(term_screen *screen,
                            char *p,
                            const uint32_t *keysyms,
                            size_t n_syms,
                            uint32_t ascii,
                            const uint32_t *ucs4,
                            unsigned int mods) {
        char ch, ch2, ch_mods;
        uint32_t v;
        size_t i;

        /* TODO: All these key-mappings need to be verified. Public information
         * on those mappings is pretty scarce and every emulator seems to do it
         * slightly differently.
         * A lot of mappings are also missing. */

        if (n_syms < 1)
                return p;

        if (n_syms == 1)
                v = keysyms[0];
        else
                v = XKB_KEY_NoSymbol;

        /* In some mappings, the modifiers are encoded as CSI parameters. The
         * encoding is rather arbitrary, but seems to work. */
        ch_mods = 0;
        switch (mods & (TERM_KBDMOD_SHIFT | TERM_KBDMOD_ALT | TERM_KBDMOD_CTRL)) {
        case TERM_KBDMOD_SHIFT:
                ch_mods = '2';
                break;
        case TERM_KBDMOD_ALT:
                ch_mods = '3';
                break;
        case TERM_KBDMOD_SHIFT | TERM_KBDMOD_ALT:
                ch_mods = '4';
                break;
        case TERM_KBDMOD_CTRL:
                ch_mods = '5';
                break;
        case TERM_KBDMOD_CTRL | TERM_KBDMOD_SHIFT:
                ch_mods = '6';
                break;
        case TERM_KBDMOD_CTRL | TERM_KBDMOD_ALT:
                ch_mods = '7';
                break;
        case TERM_KBDMOD_CTRL | TERM_KBDMOD_SHIFT | TERM_KBDMOD_ALT:
                ch_mods = '8';
                break;
        }

        /* A user might actually use multiple layouts for keyboard
         * input. @keysyms[0] contains the actual keysym that the user
         * used. But if this keysym is not in the ascii range, the
         * input handler does check all other layouts that the user
         * specified whether one of them maps the key to some ASCII
         * keysym and provides this via @ascii. We always use the real
         * keysym except when handling CTRL+<XY> shortcuts we use the
         * ascii keysym. This is for compatibility to xterm et. al. so
         * ctrl+c always works regardless of the currently active
         * keyboard layout. But if no ascii-sym is found, we still use
         * the real keysym. */
        if (ascii == XKB_KEY_NoSymbol)
                ascii = v;

        /* map CTRL+<ascii> */
        if (mods & TERM_KBDMOD_CTRL) {
                switch (ascii) {
                case 0x60 ... 0x7e:
                        /* Right hand side is mapped to the left and then
                         * treated equally. Fall through to left-hand side.. */
                        ascii -= 0x20;
                case 0x20 ... 0x5f:
                        /* Printable ASCII is mapped 1-1 in XKB and in
                         * combination with CTRL bit 7 is flipped. This
                         * is equivalent to the caret-notation. */
                        *p++ = ascii ^ 0x40;
                        return p;
                }
        }

        /* map cursor keys */
        ch = 0;
        switch (v) {
        case XKB_KEY_Up:
                ch = 'A';
                break;
        case XKB_KEY_Down:
                ch = 'B';
                break;
        case XKB_KEY_Right:
                ch = 'C';
                break;
        case XKB_KEY_Left:
                ch = 'D';
                break;
        case XKB_KEY_Home:
                ch = 'H';
                break;
        case XKB_KEY_End:
                ch = 'F';
                break;
        }
        if (ch) {
                *p++ = 0x1b;
                if (screen->flags & TERM_FLAG_CURSOR_KEYS)
                        *p++ = 'O';
                else
                        *p++ = '[';
                if (ch_mods) {
                        *p++ = '1';
                        *p++ = ';';
                        *p++ = ch_mods;
                }
                *p++ = ch;
                return p;
        }

        /* map action keys */
        ch = 0;
        switch (v) {
        case XKB_KEY_Find:
                ch = '1';
                break;
        case XKB_KEY_Insert:
                ch = '2';
                break;
        case XKB_KEY_Delete:
                ch = '3';
                break;
        case XKB_KEY_Select:
                ch = '4';
                break;
        case XKB_KEY_Page_Up:
                ch = '5';
                break;
        case XKB_KEY_Page_Down:
                ch = '6';
                break;
        }
        if (ch) {
                *p++ = 0x1b;
                *p++ = '[';
                *p++ = ch;
                if (ch_mods) {
                        *p++ = ';';
                        *p++ = ch_mods;
                }
                *p++ = '~';
                return p;
        }

        /* map lower function keys */
        ch = 0;
        switch (v) {
        case XKB_KEY_F1:
                ch = 'P';
                break;
        case XKB_KEY_F2:
                ch = 'Q';
                break;
        case XKB_KEY_F3:
                ch = 'R';
                break;
        case XKB_KEY_F4:
                ch = 'S';
                break;
        }
        if (ch) {
                if (ch_mods) {
                        *p++ = 0x1b;
                        *p++ = '[';
                        *p++ = '1';
                        *p++ = ';';
                        *p++ = ch_mods;
                        *p++ = ch;
                } else {
                        *p++ = 0x1b;
                        *p++ = 'O';
                        *p++ = ch;
                }

                return p;
        }

        /* map upper function keys */
        ch = 0;
        ch2 = 0;
        switch (v) {
        case XKB_KEY_F5:
                ch = '1';
                ch2 = '5';
                break;
        case XKB_KEY_F6:
                ch = '1';
                ch2 = '7';
                break;
        case XKB_KEY_F7:
                ch = '1';
                ch2 = '8';
                break;
        case XKB_KEY_F8:
                ch = '1';
                ch2 = '9';
                break;
        case XKB_KEY_F9:
                ch = '2';
                ch2 = '0';
                break;
        case XKB_KEY_F10:
                ch = '2';
                ch2 = '1';
                break;
        case XKB_KEY_F11:
                ch = '2';
                ch2 = '2';
                break;
        case XKB_KEY_F12:
                ch = '2';
                ch2 = '3';
                break;
        }
        if (ch) {
                *p++ = 0x1b;
                *p++ = '[';
                *p++ = ch;
                if (ch2)
                        *p++ = ch2;
                if (ch_mods) {
                        *p++ = ';';
                        *p++ = ch_mods;
                }
                *p++ = '~';
                return p;
        }

        /* map special keys */
        switch (v) {
        case 0xff08: /* XKB_KEY_BackSpace */
        case 0xff09: /* XKB_KEY_Tab */
        case 0xff0a: /* XKB_KEY_Linefeed */
        case 0xff0b: /* XKB_KEY_Clear */
        case 0xff15: /* XKB_KEY_Sys_Req */
        case 0xff1b: /* XKB_KEY_Escape */
        case 0xffff: /* XKB_KEY_Delete */
                *p++ = v - 0xff00;
                return p;
        case 0xff13: /* XKB_KEY_Pause */
                /* TODO: What should we do with this key?
                 * Sending XOFF is awful as there is no simple
                 * way on modern keyboards to send XON again.
                 * If someone wants this, we can re-eanble
                 * optionally. */
                return p;
        case 0xff14: /* XKB_KEY_Scroll_Lock */
                /* TODO: What should we do on scroll-lock?
                 * Sending 0x14 is what the specs say but it is
                 * not used today the way most users would
                 * expect so we disable it. If someone wants
                 * this, we can re-enable it (optionally). */
                return p;
        case XKB_KEY_Return:
                *p++ = 0x0d;
                if (screen->flags & TERM_FLAG_NEWLINE_MODE)
                        *p++ = 0x0a;
                return p;
        case XKB_KEY_ISO_Left_Tab:
                *p++ = 0x09;
                return p;
        }

        /* map unicode keys */
        for (i = 0; i < n_syms; ++i)
                p += utf8_encode_unichar(p, ucs4[i]);

        return p;
}

int term_screen_feed_keyboard(term_screen *screen,
                              const uint32_t *keysyms,
                              size_t n_syms,
                              uint32_t ascii,
                              const uint32_t *ucs4,
                              unsigned int mods) {
        _cleanup_free_ char *dyn = NULL;
        static const size_t padding = 1;
        char buf[128], *start, *p;

        assert_return(screen, -EINVAL);

        /* allocate buffer if too small */
        start = buf;
        if (4 * n_syms + padding > sizeof(buf)) {
                dyn = malloc(4 * n_syms + padding);
                if (!dyn)
                        return -ENOMEM;

                start = dyn;
        }

        /* reserve prefix space */
        start += padding;
        p = start;

        p = screen_map_key(screen, p, keysyms, n_syms, ascii, ucs4, mods);
        if (!p || p - start < 1)
                return 0;

        /* The ALT modifier causes ESC to be prepended to any key-stroke. We
         * already accounted for that buffer space above, so simply prepend it
         * here.
         * TODO: is altSendsEscape a suitable default? What are the semantics
         * exactly? Is it used in C0/C1 conversion? Is it prepended if there
         * already is an escape character? */
        if (mods & TERM_KBDMOD_ALT && *start != 0x1b)
                *--start = 0x1b;

        /* turn C0 into C1 */
        if (!(screen->flags & TERM_FLAG_7BIT_MODE) && p - start >= 2)
                if (start[0] == 0x1b && start[1] >= 0x40 && start[1] <= 0x5f)
                        *++start ^= 0x40;

        return screen_write(screen, start, p - start);
}

int term_screen_resize(term_screen *screen, unsigned int x, unsigned int y) {
        unsigned int i;
        uint8_t *t;
        int r;

        assert_return(screen, -EINVAL);

        r = term_page_reserve(screen->page_main, x, y, &screen->state.attr, screen->age);
        if (r < 0)
                return r;

        r = term_page_reserve(screen->page_alt, x, y, &screen->state.attr, screen->age);
        if (r < 0)
                return r;

        if (x > screen->n_tabs) {
                t = realloc(screen->tabs, (x + 7) / 8);
                if (!t)
                        return -ENOMEM;

                screen->tabs = t;
                screen->n_tabs = x;
        }

        for (i = (screen->page->width + 7) / 8 * 8; i < x; i += 8)
                screen->tabs[i / 8] = 0x1;

        term_page_resize(screen->page_main, x, y, &screen->state.attr, screen->age, screen->history);
        term_page_resize(screen->page_alt, x, y, &screen->state.attr, screen->age, NULL);

        screen->state.cursor_x = screen_clamp_x(screen, screen->state.cursor_x);
        screen->state.cursor_y = screen_clamp_x(screen, screen->state.cursor_y);
        screen_cursor_clear_wrap(screen);

        return 0;
}

void term_screen_soft_reset(term_screen *screen) {
        unsigned int i;

        assert(screen);

        screen->g0 = &term_unicode_lower;
        screen->g1 = &term_unicode_upper;
        screen->g2 = &term_unicode_lower;
        screen->g3 = &term_unicode_upper;
        screen->state.attr = screen->default_attr;
        screen->state.gl = &screen->g0;
        screen->state.gr = &screen->g1;
        screen->state.glt = NULL;
        screen->state.grt = NULL;
        screen->state.auto_wrap = 0;
        screen->state.origin_mode = 0;

        screen->saved = screen->state;
        screen->saved.cursor_x = 0;
        screen->saved.cursor_y = 0;
        screen->saved_alt = screen->saved;

        screen->page = screen->page_main;
        screen->history = screen->history_main;
        screen->flags = TERM_FLAG_7BIT_MODE;
        screen->conformance_level = TERM_CONFORMANCE_LEVEL_VT400;

        for (i = 0; i < screen->page->width; i += 8)
                screen->tabs[i / 8] = 0x1;

        term_page_set_scroll_region(screen->page_main, 0, screen->page->height);
        term_page_set_scroll_region(screen->page_alt, 0, screen->page->height);
}

void term_screen_hard_reset(term_screen *screen) {
        assert(screen);

        term_screen_soft_reset(screen);
        zero(screen->utf8);
        screen->state.cursor_x = 0;
        screen->state.cursor_y = 0;
        term_page_erase(screen->page_main, 0, 0, screen->page->width, screen->page->height, &screen->state.attr, screen->age, false);
        term_page_erase(screen->page_alt, 0, 0, screen->page->width, screen->page->height, &screen->state.attr, screen->age, false);
}

int term_screen_set_answerback(term_screen *screen, const char *answerback) {
        char *t = NULL;

        assert_return(screen, -EINVAL);

        if (answerback) {
                t = strdup(answerback);
                if (!t)
                        return -ENOMEM;
        }

        free(screen->answerback);
        screen->answerback = t;

        return 0;
}

int term_screen_draw(term_screen *screen,
                     int (*draw_fn) (term_screen *screen,
                                     void *userdata,
                                     unsigned int x,
                                     unsigned int y,
                                     const term_attr *attr,
                                     const uint32_t *ch,
                                     size_t n_ch,
                                     unsigned int ch_width),
                     void *userdata,
                     uint64_t *fb_age) {
        uint64_t cell_age, line_age, age = 0;
        term_charbuf_t ch_buf;
        const uint32_t *ch_str;
        unsigned int i, j, cw;
        term_page *page;
        term_line *line;
        term_cell *cell;
        size_t ch_n;
        int r;

        assert(screen);
        assert(draw_fn);

        if (fb_age)
                age = *fb_age;

        page = screen->page;

        for (j = 0; j < page->height; ++j) {
                line = page->lines[j];
                line_age = MAX(line->age, page->age);

                for (i = 0; i < page->width; ++i) {
                        term_attr attr;

                        cell = &line->cells[i];
                        cell_age = MAX(cell->age, line_age);

                        if (age != 0 && cell_age <= age)
                                continue;

                        ch_str = term_char_resolve(cell->ch, &ch_n, &ch_buf);

                        /* Character-width of 0 is used for cleared cells.
                         * Always treat this as single-cell character, so
                         * renderers can assume ch_width is set properpy. */
                        cw = MAX(cell->cwidth, 1U);

                        attr = cell->attr;
                        if (i == screen->state.cursor_x && j == screen->state.cursor_y &&
                            !(screen->flags & TERM_FLAG_HIDE_CURSOR))
                                attr.inverse ^= 1;

                        r = draw_fn(screen,
                                    userdata,
                                    i,
                                    j,
                                    &attr,
                                    ch_str,
                                    ch_n,
                                    cw);
                        if (r != 0)
                                return r;
                }
        }

        if (fb_age)
                *fb_age = screen->age;

        return 0;
}