aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/tads/tads2/output.cpp
blob: 8b9fd749254587e5d16a1be335c93be54d587b68 (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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "glk/tads/tads2/character_map.h"
#include "glk/tads/tads2/debug.h"
#include "glk/tads/tads2/error.h"
#include "glk/tads/tads2/memory_cache.h"
#include "glk/tads/tads2/os.h"
#include "glk/tads/tads2/run.h"
#include "glk/tads/tads2/text_io.h"
#include "glk/tads/tads2/vocabulary.h"
#include "glk/tads/os_glk.h"

namespace Glk {
namespace TADS {
namespace TADS2 {

/* Forward declarations */
struct out_stream_info;

/* 
 *   use our own isxxx - anything outside the US ASCII range is not reliably
 *   classifiable by the normal C isxxx routines 
 */
#define outissp(c) (((uchar)(c)) <= 127 && Common::isSpace((uchar)(c)))
#define outisal(c) (((uchar)(c)) <= 127 && Common::isAlpha((uchar)(c)))
#define outisdg(c) (((uchar)(c)) <= 127 && Common::isDigit((uchar)(c)))
#define outisup(c) (((uchar)(c)) <= 127 && Common::isUpper((uchar)(c)))
#define outislo(c) (((uchar)(c)) <= 127 && Common::isLower((uchar)(c)))


/*
 *   Turn on formatter-level MORE mode, EXCEPT under any of the following
 *   conditions:
 *   
 *   - this is a MAC OS port
 *.  - this is an HTML TADS interpreter
 *.  - USE_OS_LINEWRAP is defined
 *   
 *   Formatter-level MORE mode and formatter-level line wrapping go together;
 *   you can't have one without the other.  So, if USE_OS_LINEWRAP is
 *   defined, we must also use OS-level MORE mode, which means we don't want
 *   formatter-level MORE mode.
 *   
 *   For historical reasons, we check specifically for MAC_OS.  This was the
 *   first platform for which OS-level MORE mode and OS-level line wrapping
 *   were invented; at the time, we foolishly failed to anticipate that more
 *   platforms might eventually come along with the same needs, so we coded a
 *   test for MAC_OS rather than some more abstract marker.  For
 *   compatibility, we retain this specific test.
 *   
 *   USE_OS_LINEWRAP is intended as the more abstract marker we should
 *   originally have used.  A port should #define USE_OS_LINEWRAP in its
 *   system-specific os_xxx.h header to turn on OS-level line wrapping and
 *   OS-level MORE mode.  Ports should avoid adding new #ifndef tests for
 *   specific platforms here; we've only retained the MAC_OS test because we
 *   don't want to break the existing MAC_OS port.  
 */
#ifndef MAC_OS
# ifndef USE_HTML
#  ifndef USE_OS_LINEWRAP
#   define USE_MORE                   /* activate formatter-level more-mode */
#  endif /* USE_OS_LINEWRAP */
# endif /* USE_HTML */
#endif /* MAC_OS */

/* 
 *   In HTML mode, don't use MORE mode.  Note that we explicitly turn MORE
 *   mode OFF, even though we won't have turned it on above, because it might
 *   have been turned on by an os_xxx.h header.  This is here for historical
 *   reasons; in particular, some of the HTML interpreter builds include
 *   headers that were originally written for the normal builds for those
 *   same platforms, and those original headers explicitly #define USE_MORE
 *   somewhere.  So, to be absolutely sure we get it right here, we have to
 *   explicitly turn off USE_MORE when compiling for HTML mode.  
 */
#ifdef USE_HTML
# ifdef USE_MORE
#  undef USE_MORE
# endif
#endif

/*
 *   QSPACE is the special character for a quoted space (internally, the
 *   sequence "\ " (backslash-space) is converted to QSPACE).  It must not
 *   be any printable character.  The value here may need to be changed in
 *   the extremely unlikely event that TADS is ever ported to an EBCDIC
 *   machine.
 */
#define QSPACE 26

/*
 *   QTAB is a special hard tab character indicator.  We use this when we
 *   need to generate a hard tab to send to the underlying output layer
 *   (in particular, we use this to send hard tabs to the HTML formatter
 *   when we're in HTML mode).  
 */
#define QTAB 25


/* maximum width of the display */
#define MAXWIDTH  OS_MAXWIDTH


/* ------------------------------------------------------------------------ */
/*
 *   Globals and statics.  These should really be moved into a context
 *   structure, so that the output formatter subsystem could be shared
 *   among multiple clients.  For now, there's no practical problem using
 *   statics, because we only need a single output subsystem at one time.  
 */

/* current script (command input) file */
extern osfildef *scrfp;

/*
 *   This should be TRUE if the output should have two spaces after a
 *   period (or other such punctuation. It should generally be TRUE for
 *   fixed-width fonts, and FALSE for proportional fonts.  
 */
static int doublespace = 1;

/*
 *   Log file handle and name.  If we're copying output to a log file,
 *   these will tell us about the file.  
 */
osfildef *logfp;
static char logfname[OSFNMAX];

/* flag indicating whether output has occurred since last check */
static uchar outcnt;

/* flag indicating whether hidden output has occurred */
static uchar hidout;

/* flag indicating whether to show (TRUE) or hide (FALSE) output */
static uchar outflag;

/* flag indicating whether output is hidden for debugging purposes */
int dbghid;

/*
 *   Current recursion level in formatter invocation
 */
static int G_recurse = 0;

/* active stream in current recursion level */
static out_stream_info *G_cur_stream;

/* watchpoint mode flag */
static uchar outwxflag;

/*
 *   User filter function.  When this function is set, we'll invoke this
 *   function for each string that's displayed through the output
 *   formatter.
 */
static objnum G_user_filter = MCMONINV;


/* ------------------------------------------------------------------------ */
/* 
 *   Hack to run with TADS 2.0 with minimal reworking.  Rather than using
 *   an allocated output layer context, store our subsystem context
 *   information in some statics.  This is less clean than using a real
 *   context, but doesn't create any practical problems as we don't need
 *   to share the output formatter subsystem among multiple simultaneous
 *   callers.  
 */
static runcxdef *runctx;                               /* execution context */
static uchar    *fmsbase;                        /* format string area base */
static uchar    *fmstop;                          /* format string area top */
static objnum    cmdActor;                                 /* current actor */

/* forward declarations of static functions */
static void outstring_stream(out_stream_info *stream, char *s);
static void outchar_noxlat_stream(out_stream_info *stream, char c);
static char out_parse_entity(char *outbuf, size_t outbuf_size,
                             char **sp, size_t *slenp);


/* ------------------------------------------------------------------------ */
/*
 *   HTML lexical analysis mode
 */
#define HTML_MODE_NORMAL  0                    /* normal text, not in a tag */
#define HTML_MODE_TAG     1                         /* parsing inside a tag */
#define HTML_MODE_SQUOTE  2           /* in a single-quoted string in a tag */
#define HTML_MODE_DQUOTE  3           /* in a double-quoted string in a tag */

/*
 *   HTML parsing mode flag for <BR> tags.  We defer these until we've
 *   read the full tag in order to obey an HEIGHT attribute we find.  When
 *   we encounter a <BR>, we figure out whether we think we'll need a
 *   flush or a blank line; if we find a HEIGHT attribute, we may change
 *   this opinion.  
 */
#define HTML_DEFER_BR_NONE   0                           /* no pending <BR> */
#define HTML_DEFER_BR_FLUSH  1                   /* only need an outflush() */
#define HTML_DEFER_BR_BLANK  2                        /* need an outblank() */

/*
 *   If we're compiling for an HTML-enabled underlying output subsystem,
 *   we want to call the underlying OS layer when switching in and out of
 *   HTML mode.  If the underlying system doesn't process HTML, we don't
 *   need to let it know anything about HTML mode. 
 */
#ifdef USE_HTML
# define out_start_html(stream) os_start_html()
# define out_end_html(stream)   os_end_html()
#else
# define out_start_html(stream)
# define out_end_html(stream)
#endif


/* ------------------------------------------------------------------------ */
/*
 *   Output formatter stream state structure.  This structure encapsulates
 *   the state of an individual output stream.  
 */
struct out_stream_info {
    /* low-level display routine (va_list version) */
    void (*do_print)(out_stream_info *stream, const char *str);
    
    /* current line position and output column */
    uchar linepos;
    uchar linecol;

    /* number of lines on the screen (since last MORE prompt) */
    int linecnt;

    /* output buffer */
    char linebuf[MAXWIDTH];

    /* 
     *   attribute buffer - we keep one attribute entry for each character in
     *   the line buffer 
     */
    int attrbuf[MAXWIDTH];

    /* current attribute for text we're buffering into linebuf */
    int cur_attr;

    /* last attribute we wrote to the osifc layer */
    int os_attr;

    /* CAPS mode - next character output is converted to upper-case */
    uchar capsflag;

    /* NOCAPS mode - next character output is converted to lower-case */
    uchar nocapsflag;

    /* ALLCAPS mode - all characters output are converted to upper-case */
    uchar allcapsflag;

    /* capture information */
    mcmcxdef *capture_ctx;           /* memory context to use for capturing */
    mcmon     capture_obj;                /* object holding captured output */
    uint      capture_ofs;                /* write offset in capture object */
    int       capturing;                 /* true -> we are capturing output */

    /* "preview" state for line flushing */
    int preview;

    /* flag indicating that we just flushed a new line */
    int just_did_nl;

    /* this output stream uses "MORE" mode */
    int use_more_mode;

    /* 
     *   This output stream uses OS-level line wrapping - if this is set,
     *   the output formatter will not insert a newline at the end of a
     *   line that it's flushing for word wrapping, but will instead let
     *   the underlying OS display layer handle the wrapping. 
     */
    int os_line_wrap;

    /*
     *   Flag indicating that the underlying output system wants to
     *   receive its output as HTML.
     *   
     *   If this is true, we'll pass through HTML to the underlying output
     *   system, and in addition generate HTML sequences for certain
     *   TADS-native escapes (for example, we'll convert the "\n" sequence
     *   to a <BR> sequence).
     *   
     *   If this is false, we'll do just the opposite: we'll remove HTML
     *   from the output stream and convert it into normal text sequences. 
     */
    int html_target;

    /*
     *   Flag indicating that the target uses plain text.  If this flag is
     *   set, we won't add the OS escape codes for highlighted characters. 
     */
    int plain_text_target;
    
    /* 
     *   Flag indicating that the caller is displaying HTML.  We always
     *   start off in text mode; the client can switch to HTML mode by
     *   displaying a special escape sequence, and can switch back to text
     *   mode by displaying a separate special escape sequence.  
     */
    int html_mode;

    /* current lexical analysis mode */
    unsigned int html_mode_flag;

    /* <BR> defer mode */
    unsigned int html_defer_br;

    /* 
     *   HTML "ignore" mode - we suppress all output when parsing the
     *   contents of a <TITLE> or <ABOUTBOX> tag 
     */
    int html_in_ignore;

    /*
     *   HTML <TITLE> mode - when we're in this mode, we're gathering the
     *   title (i.e., we're inside a <TITLE> tag's contents).  We'll copy
     *   characters to the title buffer rather than the normal output
     *   buffer, and then call os_set_title() when we reach the </TITLE>
     *   tag.  
     */
    int html_in_title;

    /* buffer for the title */
    char html_title_buf[256];

    /* pointer to next available character in title buffer */
    char *html_title_ptr;

    /* quoting level */
    int html_quote_level;

    /* PRE nesting level */
    int html_pre_level;

    /*
     *   Parsing mode flag for ALT attributes.  If we're parsing a tag
     *   that allows ALT, such as IMG or SOUND, we'll set this flag, then
     *   insert the ALT text if we encounter it during parsing.  
     */
    int html_allow_alt;
};

/*
 *   Default output converter.  This is the output converter for the
 *   standard display.  Functions in the public interface that do not
 *   specify an output converter will use this converter by default.  
 */
static out_stream_info G_std_disp;

/*
 *   Log file converter.  This is the output converter for a log file.
 *   Whenever we open a log file, we'll initialize this converter; as we
 *   display text to the main display, we'll also copy it to the log file.
 *   
 *   We maintain an entire separate conversion context for the log file,
 *   so that we can perform a different set of conversions on it.  We may
 *   want, for example, to pass HTML text through to the OS display
 *   subsystem (this is the case for the HTML-enabled interpreter), but
 *   we'll still want to convert log file output to text.  By keeping a
 *   separate display context for the log file, we can format output to
 *   the log file using an entirely different style than we do for the
 *   display. 
 */
static out_stream_info G_log_disp;


/* ------------------------------------------------------------------------ */
/*
 *   low-level output handlers for the standard display and log file 
 */

/* standard display printer */
static void do_std_print(out_stream_info *stream, const char *str)
{
    VARUSED(stream);
    
    /* display the text through the OS layer */
    os_printz(str);
}

/* log file printer */
static void do_log_print(out_stream_info *stream, const char *str)
{
    VARUSED(stream);

    /* display to the log file */
    if (logfp != 0 && G_os_moremode)
    {
        os_fprintz(logfp, str);
        osfflush(logfp);
    }
}


/* ------------------------------------------------------------------------ */
/* 
 *   initialize a generic output formatter state structure 
 */
static void out_state_init(out_stream_info *stream)
{
    /* start out at the first column */
    stream->linepos = 0;
    stream->linecol = 0;
    stream->linebuf[0] = '\0';

    /* set normal text attributes */
    stream->cur_attr = 0;
    stream->os_attr = 0;

    /* start out at the first line */
    stream->linecnt = 0;

    /* we're not in either "caps", "nocaps", or "allcaps" mode yet */
    stream->capsflag = stream->nocapsflag = stream->allcapsflag = FALSE;

    /* we're not capturing yet */
    stream->capturing = FALSE;
    stream->capture_obj = MCMONINV;

    /* we aren't previewing a line yet */
    stream->preview = 0;

    /* we haven't flushed a new line yet */
    stream->just_did_nl = FALSE;

    /* presume this stream does not use "MORE" mode */
    stream->use_more_mode = FALSE;

    /* presume this stream uses formatter-level line wrapping */
    stream->os_line_wrap = FALSE;

    /* assume that the underlying system is not HTML-enabled */
    stream->html_target = FALSE;

    /* presume this target accepts OS highlighting sequences */
    stream->plain_text_target = FALSE;

    /* start out in text mode */
    stream->html_mode = FALSE;

    /* start out in "normal" lexical state */
    stream->html_mode_flag = HTML_MODE_NORMAL;

    /* not in an ignored tag yet */
    stream->html_in_ignore = FALSE;

    /* not in title mode yet */
    stream->html_in_title = FALSE;

    /* not yet deferring line breaks */
    stream->html_defer_br = HTML_DEFER_BR_NONE;

    /* not yet in quotes */
    stream->html_quote_level = 0;

    /* not yet in a PRE block */
    stream->html_pre_level = 0;

    /* not in an ALT tag yet */
    stream->html_allow_alt = FALSE;
}


/* ------------------------------------------------------------------------ */
/*
 *   initialize a standard display stream 
 */
static void out_init_std(out_stream_info *stream)
{
    /* there's no user output filter function yet */
    out_set_filter(MCMONINV);
    
    /* initialize the basic stream state */
    out_state_init(stream);

    /* set up the low-level output routine */
    G_std_disp.do_print = do_std_print;

#ifdef USE_MORE
    /* 
     *   We're compiled for MORE mode, and we're not compiling for an
     *   underlying HTML formatting layer, so use MORE mode for the
     *   standard display stream.  
     */
    stream->use_more_mode = TRUE;
#else
    /*
     *   We're compiled for OS-layer (or HTML-layer) MORE handling.  For
     *   this case, use OS-layer (or HTML-layer) line wrapping as well.  
     */
    stream->os_line_wrap = TRUE;
#endif

#ifdef USE_HTML
    /* 
     *   if we're compiled for HTML mode, set the standard output stream
     *   so that it knows it has an HTML target - this will ensure that
     *   HTML tags are passed through to the underlying stream, and that
     *   we generate HTML equivalents for our own control sequences 
     */
    stream->html_target = TRUE;
#endif
}

/*
 *   initialize a standard log file stream 
 */
static void out_init_log(out_stream_info *stream)
{
    /* initialize the basic stream state */
    out_state_init(stream);

    /* set up the low-level output routine */
    stream->do_print = do_log_print;

    /* use plain text in the log file stream */
    stream->plain_text_target = TRUE;
}



/* ------------------------------------------------------------------------ */
/* 
 *   table of '&' character name sequences 
 */
struct amp_tbl_t {
    /* entity name */
    const char *cname;

    /* HTML Unicode character value */
    uint        html_cval;

    /* native character set expansion */
    char       *expan;
};

/*
 *   HTML entity mapping table.  When we're in non-HTML mode, we keep our
 *   own expansion table so that we can map HTML entity names into the
 *   local character set.
 *   
 *   The entries in this table must be in sorted order (by HTML entity
 *   name), because we use a binary search to find an entity name in the
 *   table.  
 */
static struct amp_tbl_t amp_tbl[] = {
    { "AElig", 198, 0 },
    { "Aacute", 193, 0 },
    { "Abreve", 258, 0 },
    { "Acirc", 194, 0 },
    { "Agrave", 192, 0 },
    { "Alpha", 913, 0 },
    { "Aogon", 260, 0 },
    { "Aring", 197, 0 },
    { "Atilde", 195, 0 },
    { "Auml", 196, 0 },
    { "Beta", 914, 0 },
    { "Cacute", 262, 0 },
    { "Ccaron", 268, 0 },
    { "Ccedil", 199, 0 },
    { "Chi", 935, 0 },
    { "Dagger", 8225, 0 },
    { "Dcaron", 270, 0 },
    { "Delta", 916, 0 },
    { "Dstrok", 272, 0 },
    { "ETH", 208, 0 },
    { "Eacute", 201, 0 },
    { "Ecaron", 282, 0 },
    { "Ecirc", 202, 0 },
    { "Egrave", 200, 0 },
    { "Eogon", 280, 0 },
    { "Epsilon", 917, 0 },
    { "Eta", 919, 0 },
    { "Euml", 203, 0 },
    { "Gamma", 915, 0 },
    { "Iacute", 205, 0 },
    { "Icirc", 206, 0 },
    { "Igrave", 204, 0 },
    { "Iota", 921, 0 },
    { "Iuml", 207, 0 },
    { "Kappa", 922, 0 },
    { "Lacute", 313, 0 },
    { "Lambda", 923, 0 },
    { "Lcaron", 317, 0 },
    { "Lstrok", 321, 0 },
    { "Mu", 924, 0 },
    { "Nacute", 323, 0 },
    { "Ncaron", 327, 0 },
    { "Ntilde", 209, 0 },
    { "Nu", 925, 0 },
    { "OElig", 338, 0 },
    { "Oacute", 211, 0 },
    { "Ocirc", 212, 0 },
    { "Odblac", 336, 0 },
    { "Ograve", 210, 0 },
    { "Omega", 937, 0 },
    { "Omicron", 927, 0 },
    { "Oslash", 216, 0 },
    { "Otilde", 213, 0 },
    { "Ouml", 214, 0 },
    { "Phi", 934, 0 },
    { "Pi", 928, 0 },
    { "Prime", 8243, 0 },
    { "Psi", 936, 0 },
    { "Racute", 340, 0 },
    { "Rcaron", 344, 0 },
    { "Rho", 929, 0 },
    { "Sacute", 346, 0 },
    { "Scaron", 352, 0 },
    { "Scedil", 350, 0 },
    { "Sigma", 931, 0 },
    { "THORN", 222, 0 },
    { "Tau", 932, 0 },
    { "Tcaron", 356, 0 },
    { "Tcedil", 354, 0 },
    { "Theta", 920, 0 },
    { "Uacute", 218, 0 },
    { "Ucirc", 219, 0 },
    { "Udblac", 368, 0 },
    { "Ugrave", 217, 0 },
    { "Upsilon", 933, 0 },
    { "Uring", 366, 0 },
    { "Uuml", 220, 0 },
    { "Xi", 926, 0 },
    { "Yacute", 221, 0 },
    { "Yuml", 376, 0 },
    { "Zacute", 377, 0 },
    { "Zcaron", 381, 0 },
    { "Zdot", 379, 0 },
    { "Zeta", 918, 0 },
    { "aacute", 225, 0 },
    { "abreve", 259, 0 },
    { "acirc", 226, 0 },
    { "acute", 180, 0 },
    { "aelig", 230, 0 },
    { "agrave", 224, 0 },
    { "alefsym", 8501, 0 },
    { "alpha", 945, 0 },
    { "amp", '&', 0 },
    { "and", 8743, 0 },
    { "ang", 8736, 0 },
    { "aogon", 261, 0 },
    { "aring", 229, 0 },
    { "asymp", 8776, 0 },
    { "atilde", 227, 0 },
    { "auml", 228, 0 },
    { "bdquo", 8222, 0 },
    { "beta", 946, 0 },
    { "breve", 728, 0 },
    { "brvbar", 166, 0 },
    { "bull", 8226, 0 },
    { "cacute", 263, 0 },
    { "cap", 8745, 0 },
    { "caron", 711, 0 },
    { "ccaron", 269, 0 },
    { "ccedil", 231, 0 },
    { "cedil", 184, 0 },
    { "cent", 162, 0 },
    { "chi", 967, 0 },
    { "circ", 710, 0 },
    { "clubs", 9827, 0 },
    { "cong", 8773, 0 },
    { "copy", 169, 0 },
    { "crarr", 8629, 0 },
    { "cup", 8746, 0 },
    { "curren", 164, 0 },
    { "dArr", 8659, 0 },
    { "dagger", 8224, 0 },
    { "darr", 8595, 0 },
    { "dblac", 733, 0 },
    { "dcaron", 271, 0 },
    { "deg", 176, 0 },
    { "delta", 948, 0 },
    { "diams", 9830, 0 },
    { "divide", 247, 0 },
    { "dot", 729, 0 },
    { "dstrok", 273, 0 },
    { "eacute", 233, 0 },
    { "ecaron", 283, 0 },
    { "ecirc", 234, 0 },
    { "egrave", 232, 0 },
    { "emdash", 8212, 0 },
    { "empty", 8709, 0 },
    { "endash", 8211, 0 },
    { "eogon", 281, 0 },
    { "epsilon", 949, 0 },
    { "equiv", 8801, 0 },
    { "eta", 951, 0 },
    { "eth", 240, 0 },
    { "euml", 235, 0 },
    { "exist", 8707, 0 },
    { "fnof", 402, 0 },
    { "forall", 8704, 0 },
    { "frac12", 189, 0 },
    { "frac14", 188, 0 },
    { "frac34", 190, 0 },
    { "frasl", 8260, 0 },
    { "gamma", 947, 0 },
    { "ge", 8805, 0 },
    { "gt", '>', 0 },
    { "hArr", 8660, 0 },
    { "harr", 8596, 0 },
    { "hearts", 9829, 0 },
    { "hellip", 8230, 0 },
    { "iacute", 237, 0 },
    { "icirc", 238, 0 },
    { "iexcl", 161, 0 },
    { "igrave", 236, 0 },
    { "image", 8465, 0 },
    { "infin", 8734, 0 },
    { "int", 8747, 0 },
    { "iota", 953, 0 },
    { "iquest", 191, 0 },
    { "isin", 8712, 0 },
    { "iuml", 239, 0 },
    { "kappa", 954, 0 },
    { "lArr", 8656, 0 },
    { "lacute", 314, 0 },
    { "lambda", 955, 0 },
    { "lang", 9001, 0 },
    { "laquo", 171, 0 },
    { "larr", 8592, 0 },
    { "lcaron", 318, 0 },
    { "lceil", 8968, 0 },
    { "ldq", 8220, 0 },
    { "ldquo", 8220, 0 },
    { "le", 8804, 0 },
    { "lfloor", 8970, 0 },
    { "lowast", 8727, 0 },
    { "loz", 9674, 0 },
    { "lsaquo", 8249, 0 },
    { "lsq", 8216, 0 },
    { "lsquo", 8216, 0 },
    { "lstrok", 322, 0 },
    { "lt", '<', 0 },
    { "macr", 175, 0 },
    { "mdash", 8212, 0 },
    { "micro", 181, 0 },
    { "middot", 183, 0 },
    { "minus", 8722, 0 },
    { "mu", 956, 0 },
    { "nabla", 8711, 0 },
    { "nacute", 324, 0 },
    { "nbsp", QSPACE, 0 },
    { "ncaron", 328, 0 },
    { "ndash", 8211, 0 },
    { "ne", 8800, 0 },
    { "ni", 8715, 0 },
    { "not", 172, 0 },
    { "notin", 8713, 0 },
    { "nsub", 8836, 0 },
    { "ntilde", 241, 0 },
    { "nu", 957, 0 },
    { "oacute", 243, 0 },
    { "ocirc", 244, 0 },
    { "odblac", 337, 0 },
    { "oelig", 339, 0 },
    { "ogon", 731, 0 },
    { "ograve", 242, 0 },
    { "oline", 8254, 0 },
    { "omega", 969, 0 },
    { "omicron", 959, 0 },
    { "oplus", 8853, 0 },
    { "or", 8744, 0 },
    { "ordf", 170, 0 },
    { "ordm", 186, 0 },
    { "oslash", 248, 0 },
    { "otilde", 245, 0 },
    { "otimes", 8855, 0 },
    { "ouml", 246, 0 },
    { "para", 182, 0 },
    { "part", 8706, 0 },
    { "permil", 8240, 0 },
    { "perp", 8869, 0 },
    { "phi", 966, 0 },
    { "pi", 960, 0 },
    { "piv", 982, 0 },
    { "plusmn", 177, 0 },
    { "pound", 163, 0 },
    { "prime", 8242, 0 },
    { "prod", 8719, 0 },
    { "prop", 8733, 0 },
    { "psi", 968, 0 },
    { "quot", '"', 0 },
    { "rArr", 8658, 0 },
    { "racute", 341, 0 },
    { "radic", 8730, 0 },
    { "rang", 9002, 0 },
    { "raquo", 187, 0 },
    { "rarr", 8594, 0 },
    { "rcaron", 345, 0 },
    { "rceil", 8969, 0 },
    { "rdq", 8221, 0 },
    { "rdquo", 8221, 0 },
    { "real", 8476, 0 },
    { "reg", 174, 0 },
    { "rfloor", 8971, 0 },
    { "rho", 961, 0 },
    { "rsaquo", 8250, 0 },
    { "rsq", 8217, 0 },
    { "rsquo", 8217, 0 },
    { "sacute", 347, 0 },
    { "sbquo", 8218, 0 },
    { "scaron", 353, 0 },
    { "scedil", 351, 0 },
    { "sdot", 8901, 0 },
    { "sect", 167, 0 },
    { "shy", 173, 0 },
    { "sigma", 963, 0 },
    { "sigmaf", 962, 0 },
    { "sim", 8764, 0 },
    { "spades", 9824, 0 },
    { "sub", 8834, 0 },
    { "sube", 8838, 0 },
    { "sum", 8721, 0 },
    { "sup", 8835, 0 },
    { "sup1", 185, 0 },
    { "sup2", 178, 0 },
    { "sup3", 179, 0 },
    { "supe", 8839, 0 },
    { "szlig", 223, 0 },
    { "tau", 964, 0 },
    { "tcaron", 357, 0 },
    { "tcedil", 355, 0 },
    { "there4", 8756, 0 },
    { "theta", 952, 0 },
    { "thetasym", 977, 0 },
    { "thorn", 254, 0 },
    { "thorn", 254, 0 },
    { "tilde", 732, 0 },
    { "times", 215, 0 },
    { "trade", 8482, 0 },
    { "uArr", 8657, 0 },
    { "uacute", 250, 0 },
    { "uarr", 8593, 0 },
    { "ucirc", 251, 0 },
    { "udblac", 369, 0 },
    { "ugrave", 249, 0 },
    { "uml", 168, 0 },
    { "upsih", 978, 0 },
    { "upsilon", 965, 0 },
    { "uring", 367, 0 },
    { "uuml", 252, 0 },
    { "weierp", 8472, 0 },
    { "xi", 958, 0 },
    { "yacute", 253, 0 },
    { "yen", 165, 0 },
    { "yuml", 255, 0 },
    { "zacute", 378, 0 },
    { "zcaron", 382, 0 },
    { "zdot", 380, 0 },
    { "zeta", 950, 0 }
};


/* ------------------------------------------------------------------------ */
/*
 *   turn on CAPS mode for a stream 
 */
static void outcaps_stream(out_stream_info *stream)
{
    /* turn on CAPS mode */
    stream->capsflag = TRUE;

    /* turn off NOCAPS and ALLCAPS mode */
    stream->nocapsflag = FALSE;
    stream->allcapsflag = FALSE;
}

/*
 *   turn on NOCAPS mode for a stream 
 */
static void outnocaps_stream(out_stream_info *stream)
{
    /* turn on NOCAPS mode */
    stream->nocapsflag = TRUE;

    /* turn off CAPS and ALLCAPS mode */
    stream->capsflag = FALSE;
    stream->allcapsflag = FALSE;
}

/*
 *   turn on or off ALLCAPS mode for a stream 
 */
static void outallcaps_stream(out_stream_info *stream, int all_caps)
{
    /* set the ALLCAPS flag */
    stream->allcapsflag = all_caps;

    /* clear the CAPS and NOCAPS flags */
    stream->capsflag = FALSE;
    stream->nocapsflag = FALSE;
}

/* ------------------------------------------------------------------------ */
/*
 *   write a string to a stream 
 */
static void stream_print(out_stream_info *stream, char *str)
{
    /* call the stream's do_print method */
    (*stream->do_print)(stream, str);
}

/*
 *   Write out a line 
 */
static void t_outline(out_stream_info *stream, int nl,
                      const char *txt, const int *attr)
{
    extern int scrquiet;

    /* 
     *   Check the "script quiet" mode - this indicates that we're reading
     *   a script and not echoing output to the display.  If this mode is
     *   on, and we're writing to the display, suppress this write.  If
     *   the mode is off, or we're writing to another stream (such as the
     *   log file), show the output as normal.  
     */
    if (!scrquiet || stream != &G_std_disp)
    {
        size_t i;
        char buf[MAXWIDTH];
        char *dst;
        
        /*
         *   Check to see if we've reached the end of the screen, and if
         *   so run the MORE prompt.  Note that we don't make this check
         *   at all if USE_MORE is undefined, since this means that the OS
         *   layer code is taking responsibility for pagination issues.
         *   We also don't display a MORE prompt when reading from a
         *   script file.
         *   
         *   Note that we suppress the MORE prompt if nl == 0, since this
         *   is used to flush a partial line of text without starting a
         *   new line (for example, when displaying a prompt where the
         *   input will appear on the same line following the prompt).
         *   
         *   Skip the MORE prompt if this stream doesn't use it.  
         */
        if (stream->use_more_mode
            && scrfp == 0
            && G_os_moremode
            && nl != 0 && nl != 4
            && stream->linecnt++ >= G_os_pagelength)
        {
            /* display the MORE prompt */
            out_more_prompt();
        }

        /*
         *   Display the text.  Run through the text in pieces; each time the
         *   attributes change, set attributes at the osifc level. 
         */
        for (i = 0, dst = buf ; txt[i] != '\0' ; ++i)
        {
            /* if the attribute is changing, notify osifc */
            if (attr != 0 && attr[i] != stream->os_attr)
            {
                /* flush the preceding text */
                if (dst != buf)
                {
                    *dst = '\0';
                    stream_print(stream, buf);
                }

                /* set the new attribute */
                os_set_text_attr(attr[i]);

                /* remember this as the last OS attribute */
                stream->os_attr = attr[i];

                /* start with a fresh buffer */
                dst = buf;
            }

            /* buffer this character */
            *dst++ = txt[i];
        }

        /* flush the last chunk of text */
        if (dst != buf)
        {
            *dst = '\0';
            stream_print(stream, buf);
        }
    }
}

/* ------------------------------------------------------------------------ */
/*
 *   Flush the current line to the display.  The 'nl' argument specifies
 *   what kind of flushing to do:
 *   
 *   0: flush the current line but do not start a new line; more text will
 *   follow on the current line.  This is used, for example, to flush text
 *   after displaying a prompt and before waiting for user input.
 *   
 *   1: flush the line and start a new line.
 *   
 *   2: flush the line as though starting a new line, but don't add an
 *   actual newline character to the output, since the underlying OS
 *   display code will handle this.  Instead, add a space after the line.
 *   (This differs from mode 0 in that mode 0 shouldn't add anything at
 *   all after the line.)
 *   
 *   3: "preview" mode.  Flush the line, but do not start a new line, and
 *   retain the current text in the buffer.  This is used for systems that
 *   handle the line wrapping in the underlying system code to flush a
 *   partially filled line that will need to be flushed again later.
 *   
 *   4: same as mode 0, but used for internal buffer flushes only.  Do not
 *   involve the underlying OS layer in this type of flush - simply flush
 *   our buffers with no separation.  
 */

/* flush a given output stream */
static void outflushn_stream(out_stream_info *stream, int nl)
{
    int i;

    /* null-terminate the current output line buffer */
    stream->linebuf[stream->linepos] = '\0';

    /* note the position of the last character to display */
    i = stream->linepos - 1;

    /* if we're adding anything, remove trailing spaces */
    if (nl != 0 && nl != 4)
    {
        /* look for last non-space character */
        for ( ; i >= 0 && outissp(stream->linebuf[i]) ; --i) ;
    }

    /* check the output mode */
    if (nl == 3)
    {
        /* 
         *   this is the special "preview" mode -- only display the part
         *   that we haven't already previewed for this same line
         */
        if (i + 1 > stream->preview)
        {
            /* write out the line */
            t_outline(stream, 0, &stream->linebuf[stream->preview],
                      &stream->attrbuf[stream->preview]);

            /* skip past the part we wrote */
            stream->preview += strlen(&stream->linebuf[stream->preview]);
        }
    }
    else
    {
        char *suffix = nullptr; /* extra text to add after the flushed text */
        int   countnl = 0;         /* true if line counts for [more] paging */

        /* null-terminate the buffer at the current position */
        stream->linebuf[++i] = '\0';

        /* check the mode */
        switch(nl)
        {
        case 0:
        case 3:
        case 4:
            /* no newline - just flush out what we have with no suffix */
            suffix = 0;
            break;

        case 1:
            /* 
             *   Add a newline.  If there's nothing in the current line,
             *   or we just wrote out a newline, do not add an extra
             *   newline.  Keep all newlines in PRE mode.
             */
            if (stream->linecol != 0 || !stream->just_did_nl
                || stream->html_pre_level != 0)
            {
                /* add a newline after the text */
                suffix = "\n";
                
                /* count the line in the page size */
                countnl = 1;
            }
            else
            {
                /* don't add a newline */
                suffix = 0;
            }
            break;

        case 2:
            /* 
             *   we're going to depend on the underlying OS output layer
             *   to do line breaking, so don't add a newline, but do add a
             *   space, so that the underlying OS layer knows we have a
             *   word break here 
             */
            suffix = " ";
            break;
        }

        /* 
         *   display the line, as long as we have something buffered to
         *   display; even if we don't, display it if our column is
         *   non-zero and we didn't just do a newline, since this must
         *   mean that we've flushed a partial line and are just now doing
         *   the newline 
         */
        if (stream->linebuf[stream->preview] != '\0'
            || (stream->linecol != 0 && !stream->just_did_nl)
            || stream->html_pre_level > 0)
        {
            /* write it out */
            t_outline(stream, countnl, &stream->linebuf[stream->preview],
                      &stream->attrbuf[stream->preview]);

            /* write the suffix, if any */
            if (suffix != 0)
                t_outline(stream, 0, suffix, 0);
        }

        /* generate an HTML line break if necessary */
        if (nl == 1 && stream->html_mode && stream->html_target)
            t_outline(stream, 0, "<BR HEIGHT=0>", 0);

        if (nl == 0)
        {
            /* we're not displaying a newline, so flush what we have */
            os_flush();
        }
        else
        {
            /* we displayed a newline, so reset the column position */
            stream->linecol = 0;
        }

        /* reset the line output buffer position */
        stream->linepos = stream->preview = 0;

        /* 
         *   If we just output a newline, note it.  If we didn't just
         *   output a newline, but we did write out anything else, note
         *   that we're no longer at the start of a line on the underlying
         *   output device.  
         */
        if (nl == 1)
            stream->just_did_nl = TRUE;
        else if (stream->linebuf[stream->preview] != '\0')
            stream->just_did_nl = FALSE;
    }

    /* 
     *   If the osifc-level attributes don't match the current attributes,
     *   bring the osifc layer up to date.  This is necessary in cases where
     *   we set attributes immediately before asking for input - we
     *   essentially need to flush the attributes without flushing any text.
     */
    if (stream->cur_attr != stream->os_attr)
    {
        /* set the osifc attributes */
        os_set_text_attr(stream->cur_attr);

        /* remember the new attributes as the current osifc attributes */
        stream->os_attr = stream->cur_attr;
    }
}

/* ------------------------------------------------------------------------ */
/*
 *   Determine if we're showing output.  Returns true if output should be
 *   displayed, false if it should be suppressed.  We'll note the output
 *   for hidden display accounting as needed. 
 */
static int out_is_hidden()
{
    /* check the output flag */
    if (!outflag)
    {
        /* trace the hidden output if desired */
        if (dbghid && !hidout)
            trchid();

        /* note the hidden output */
        hidout = 1;

        /* 
         *   unless we're showing hidden text in the debugger, we're
         *   suppressing output, so return true 
         */
        if (!dbghid)
            return TRUE;
    }

    /* we're not suppressing output */
    return FALSE;
}

/* ------------------------------------------------------------------------ */
/*
 *   Display a blank line to the given stream 
 */
static void outblank_stream(out_stream_info *stream)
{
    /* flush the stream */
    outflushn_stream(stream, 1);

    /* if generating for an HTML display target, add an HTML line break */
    if (stream->html_mode && stream->html_target)
        outstring_stream(stream, "<BR>");

    /* write out the newline */
    t_outline(stream, 1, "\n", 0);
}

/* ------------------------------------------------------------------------ */
/*
 *   Generate a tab for a "\t" sequence in the game text.
 *   
 *   Standard (non-HTML) version: we'll generate enough spaces to take us
 *   to the next tab stop.
 *   
 *   HTML version: if we're in native HTML mode, we'll just generate a
 *   <TAB MULTIPLE=4>; if we're not in HTML mode, we'll generate a hard
 *   tab character, which the HTML formatter will interpret as a <TAB
 *   MULTIPLE=4>.  
 */
static void outtab_stream(out_stream_info *stream)
{
    /* check to see what the underlying system is expecting */
    if (stream->html_target)
    {
        /* the underlying system is HTML - check for HTML mode */
        if (stream->html_mode)
        {
            /* we're in HTML mode, so use the HTML <TAB> tag */
            outstring_stream(stream, "<TAB MULTIPLE=4>");
        }
        else
        {
            /* we're not in HTML mode, so generate a hard tab character */
            outchar_noxlat_stream(stream, QTAB);
        }
    }
    else
    {
        int maxcol;

        /* 
         *   We're not in HTML mode - expand the tab with spaces.  Figure
         *   the maximum column: if we're doing our own line wrapping, never
         *   go beyond the actual display width. 
         */
        maxcol = (stream->os_line_wrap ? OS_MAXWIDTH : G_os_linewidth);

        /* add the spaces */
        do
        {
            stream->attrbuf[stream->linepos] = stream->cur_attr;
            stream->linebuf[stream->linepos++] = ' ';
            ++(stream->linecol);
        } while (((stream->linecol + 1) & 3) != 0
                 && stream->linecol < maxcol);
    }
}


/* ------------------------------------------------------------------------ */
/*
 *   Flush a line 
 */
static void out_flushline(out_stream_info *stream, int padding)
{
    /* 
     *   check to see if we're using the underlying display layer's line
     *   wrapping 
     */
    if (stream->os_line_wrap)
    {
        /*
         *   In the HTML version, we don't need the normal *MORE*
         *   processing, since the HTML layer will handle that.
         *   Furthermore, we don't need to provide actual newline breaks
         *   -- that happens after the HTML is parsed, so we don't have
         *   enough information here to figure out actual line breaks.
         *   So, we'll just flush out our buffer whenever it fills up, and
         *   suppress newlines.
         *   
         *   Similarly, if we have OS-level MORE processing, don't try to
         *   figure out where the line breaks go -- just flush our buffer
         *   without a trailing newline whenever the buffer is full, and
         *   let the OS layer worry about formatting lines and paragraphs.
         *   
         *   If we're using padding, use mode 2.  If we don't want padding
         *   (which is the case if we completely fill up the buffer
         *   without finding any word breaks), write out in mode 0, which
         *   just flushes the buffer exactly like it is.  
         */
        outflushn_stream(stream, padding ? 2 : 4);
    }
    else
    {
        /*
         *   Normal mode - we process the *MORE* prompt ourselves, and we
         *   are responsible for figuring out where the actual line breaks
         *   go.  Use outflush() to generate an actual newline whenever we
         *   flush out our buffer.  
         */
        outflushn_stream(stream, 1);
    }
}


/* ------------------------------------------------------------------------ */
/*
 *   Write a character to an output stream without translation
 */
static void outchar_noxlat_stream(out_stream_info *stream, char c)
{
    int  i;
    int  qspace;
    
    /* check for the special quoted space character */
    if (c == QSPACE)
    {
        /* it's a quoted space - note it and convert it to a regular space */
        qspace = 1;
        c = ' ';
    }
    else if (c == QTAB)
    {
        /* it's a hard tab - convert it to an ordinary tab */
        c = '\t';
        qspace = 0;
    }
    else
    {
        /* translate any whitespace character to a regular space character */
        if (outissp(c))
            c = ' ';
        
        /* it's not a quoted space */
        qspace = 0;
    }

    /* check for the caps/nocaps flags */
    if ((stream->capsflag || stream->allcapsflag) && outisal(c))
    {
        /* capsflag is set, so capitalize this character */
        if (outislo(c))
            c = toupper(c);

        /* okay, we've capitalized something; clear flag */
        stream->capsflag = 0;
    }
    else if (stream->nocapsflag && outisal(c))
    {
        /* nocapsflag is set, so minisculize this character */
        if (outisup(c))
            c = tolower(c);

        /* clear the flag now that we've done the job */
        stream->nocapsflag = 0;
    }

    /* if in capture mode, simply capture the character */
    if (stream->capturing)
    {
        uchar *p;

        /* if we have a valid capture object, copy to it */
        if (stream->capture_obj != MCMONINV)
        {
            /* lock the object holding the captured text */
            p = mcmlck(stream->capture_ctx, stream->capture_obj);
            
            /* make sure the capture object is big enough */
            if (mcmobjsiz(stream->capture_ctx, stream->capture_obj)
                <= stream->capture_ofs)
            {
                /* expand the object by another 256 bytes */
                p = mcmrealo(stream->capture_ctx, stream->capture_obj,
                             (ushort)(stream->capture_ofs + 256));
            }
            
            /* add this character */
            *(p + stream->capture_ofs++) = c;
            
            /* unlock the capture object */
            mcmtch(stream->capture_ctx, stream->capture_obj);
            mcmunlck(stream->capture_ctx, stream->capture_obj);
        }

        /*
         *   we're done - we don't want to actually display the character
         *   while capturing 
         */
        return;
    }

    /* add the character to out output buffer, flushing as needed */
    if (stream->linecol + 1 < G_os_linewidth)
    {
        /* 
         *   there's room for this character, so add it to the buffer 
         */
        
        /* ignore non-quoted space at start of line outside of PRE */
        if (outissp(c) && c != '\t' && stream->linecol == 0 && !qspace
            && stream->html_pre_level == 0)
            return;

        /* is this a non-quoted space not at the start of the line? */
        if (outissp(c) && c != '\t' && stream->linecol != 0 && !qspace
            && stream->html_pre_level == 0)
        {
            int  pos1 = stream->linepos - 1;
            char p = stream->linebuf[pos1];     /* check previous character */

            /* ignore repeated spaces - collapse into a single space */
            if (outissp(p))
                return;

            /*
             *   Certain punctuation requires a double space: a period, a
             *   question mark, an exclamation mark, or a colon; or any of
             *   these characters followed by any number of single and/or
             *   double quotes.  First, scan back to before any quotes, if
             *   are on one now, then check the preceding character; if
             *   it's one of the punctuation marks requiring a double
             *   space, add this space a second time.  (In addition to
             *   scanning back past quotes, scan past parentheses,
             *   brackets, and braces.)  Don't double the spacing if we're
             *   not in the normal doublespace mode; some people may
             *   prefer single spacing after punctuation, so we make this
             *   a run-time option.  
             */
            if (doublespace)
            {
                /* find the previous relevant punctuation character */
                while (pos1 &&
                       (p == '"' || p == '\'' || p == ')' || p == ']'
                        || p == '}'))
                {
                    p = stream->linebuf[--pos1];
                }
                if ( p == '.' || p == '?' || p == '!' || p == ':' )
                {
                    /* a double-space is required after this character */
                    stream->attrbuf[stream->linepos] = stream->cur_attr;
                    stream->linebuf[stream->linepos++] = c;
                    ++(stream->linecol);
                }
            }
        }

        /* add this character to the buffer */
        stream->attrbuf[stream->linepos] = stream->cur_attr;
        stream->linebuf[stream->linepos++] = c;

        /* advance the output column position */
        ++(stream->linecol);
        return;
    }

    /*
     *   The line would overflow if this character were added.  Find the
     *   most recent word break, and output the line up to the previous
     *   word.  Note that if we're trying to output a space, we'll just
     *   add it to the line buffer.  If the last character of the line
     *   buffer is already a space, we won't do anything right now.  
     */
    if (outissp(c) && c != '\t' && !qspace)
    {
        /* this is a space, so we're at a word break */
        if (stream->linebuf[stream->linepos - 1] != ' ')
        {
            stream->attrbuf[stream->linepos] = stream->cur_attr;
            stream->linebuf[stream->linepos++] = ' ';
        }
        return;
    }
    
    /*
     *   Find the most recent word break: look for a space or dash, starting
     *   at the end of the line.  
     *   
     *   If we're about to write a hyphen, we want to skip all contiguous
     *   hyphens, because we want to keep them together as a single
     *   punctuation mark; then keep going in the normal manner, which will
     *   keep the hyphens plus the word they're attached to together as a
     *   single unit.  If spaces precede the sequence of hyphens, include
     *   the prior word as well.  
     */
    i = stream->linepos - 1;
    if (c == '-')
    {
        /* skip any contiguous hyphens at the end of the line */
        for ( ; i >= 0 && stream->linebuf[i] == '-' ; --i) ;
        
        /* skip any spaces preceding the sequence of hyphens */
        for ( ; i >= 0 && outissp(stream->linebuf[i]) ; --i) ;
    }

    /* 
     *   Now find the preceding space.  If we're doing our own wrapping
     *   (i.e., we're not using OS line wrapping), then look for the
     *   nearest hyphen as well. 
     */
    for ( ; i >= 0 && !outissp(stream->linebuf[i])
          && !(!stream->os_line_wrap && stream->linebuf[i] == '-') ; --i) ;

    /* check to see if we found a good place to break */
    if (i < 0)
    {
        /* 
         *   we didn't find any good place to break - flush the entire
         *   line as-is, breaking arbitrarily in the middle of a word 
         */
        out_flushline(stream, FALSE);

        /* 
         *   we've completely cleared out the line buffer, so reset all of
         *   the line buffer counters 
         */
        stream->linepos = 0;
        stream->linecol = 0;
        stream->linebuf[0] = '\0';
    }
    else
    {
        char brkchar;
        char tmpbuf[MAXWIDTH];
        int tmpattr[MAXWIDTH];
        size_t tmpcnt;

        /* remember the word-break character */        
        brkchar = stream->linebuf[i];

        /* null-terminate the line buffer */        
        stream->linebuf[stream->linepos] = '\0';

        /* the next line starts after the break - save a copy */
        tmpcnt = strlen(&stream->linebuf[i+1]);
        memcpy(tmpbuf, &stream->linebuf[i+1], tmpcnt + 1);
        memcpy(tmpattr, &stream->attrbuf[i+1], tmpcnt * sizeof(tmpattr[0]));

        /* 
         *   terminate the buffer at the space or after the hyphen,
         *   depending on where we broke 
         */
        if (outissp(brkchar))
            stream->linebuf[i] = '\0';
        else
            stream->linebuf[i+1] = '\0';

        /* write out everything up to the word break */
        out_flushline(stream, TRUE);

        /* copy the next line into line buffer */
        memcpy(stream->linebuf, tmpbuf, tmpcnt + 1);
        memcpy(stream->attrbuf, tmpattr, tmpcnt * sizeof(tmpattr[0]));
        stream->linepos = tmpcnt;

        /* 
         *   figure what column we're now in - count all of the printable
         *   characters in the new line 
         */
        for (stream->linecol = 0, i = 0 ; i < stream->linepos ; ++i)
        {
            /* if it's printable, count it */
            if (((unsigned char)stream->linebuf[i]) >= 26)
                ++(stream->linecol);
        }
    }
    
    /* add the new character to buffer */
    stream->attrbuf[stream->linepos] = stream->cur_attr;
    stream->linebuf[stream->linepos++] = c;

    /* advance the column counter */
    ++(stream->linecol);
}

/* ------------------------------------------------------------------------ */
/*
 *   Write out a character, translating to the local system character set
 *   from the game's internal character set. 
 */
static void outchar_stream(out_stream_info *stream, char c)
{
    outchar_noxlat_stream(stream, cmap_i2n(c));
}

/* 
 *   write out a string, translating to the local system character set 
 */
static void outstring_stream(out_stream_info *stream, char *s)
{
    /* write out each character in the string */
    for ( ; *s ; ++s)
        outchar_stream(stream, *s);
}

/* 
 *   write out a string without translation 
 */
static void outstring_noxlat_stream(out_stream_info *stream, char *s)
{
    for ( ; *s ; ++s)
        outchar_noxlat_stream(stream, *s);
}


/* ------------------------------------------------------------------------ */
/*
 *   Write out an HTML character value, translating to the local character
 *   set.  
 */
static void outchar_html_stream(out_stream_info *stream,
                                unsigned int htmlchar)
{
    amp_tbl_t *ampptr;

    /* 
     *   search for a mapping entry for this entity, in case it's defined
     *   in an external mapping file 
     */
    for (ampptr = amp_tbl ;
         ampptr < amp_tbl + sizeof(amp_tbl)/sizeof(amp_tbl[0]) ; ++ampptr)
    {
        /* if this is the one, stop looking */
        if (ampptr->html_cval == htmlchar)
            break;
    }

    /* 
     *   If we found a mapping table entry, and the entry has an expansion
     *   from the external character mapping table file, use the external
     *   expansion; otherwise, use the default expansion.  
     */
    if (ampptr >= amp_tbl + sizeof(amp_tbl)/sizeof(amp_tbl[0])
        || ampptr->expan == 0)
    {
        char xlat_buf[50];

        /* 
         *   there's no external mapping table file expansion -- use the
         *   default OS mapping routine 
         */
        os_xlat_html4(htmlchar, xlat_buf, sizeof(xlat_buf));
        outstring_noxlat_stream(stream, xlat_buf);
    }
    else
    {
        /* 
         *   use the explicit mapping from the mapping table file 
         */
        outstring_noxlat_stream(stream, ampptr->expan);
    }
}


/* ------------------------------------------------------------------------ */
/*
 *   Enter a recursion level.  Returns TRUE if the caller should proceed
 *   with the operation, FALSE if not.
 *   
 *   If we're making a recursive call, thereby re-entering the formatter,
 *   and this stream is not the same as the enclosing stream, we want to
 *   ignore this call and suppress any output to this stream, so we'll
 *   return FALSE.  
 */
static int out_push_stream(out_stream_info *stream)
{
    /* 
     *   if we're already in the formatter, and the new stream doesn't
     *   match the enclosing recursion level's stream, tell the caller to
     *   abort the operation 
     */
    if (G_recurse != 0 && G_cur_stream != stream)
        return FALSE;
    
    /* note the active stream */
    G_cur_stream = stream;

    /* count the entry */
    ++G_recurse;

    /* tell the caller to proceed */
    return TRUE;
}

/*
 *   Leave a recursion level 
 */
static void out_pop_stream()
{
    /* count the exit */
    --G_recurse;
}

/* ------------------------------------------------------------------------ */
/*
 *   nextout() returns the next character in a string, and updates the
 *   string pointer and remaining length.  Returns zero if no more
 *   characters are available in the string.  
 */
/* static char nextout(char **s, uint *len); */
#define nextout(s, len) ((char)(*(len) == 0 ? 0 : (--(*(len)), *((*(s))++))))


/* ------------------------------------------------------------------------ */
/*
 *   Get the next character, writing the previous character to the given
 *   output stream if it's not null. 
 */
static char nextout_copy(char **s, size_t *slen,
                         char prv, out_stream_info *stream)
{
    /* if there's a stream, write the previous character to the stream */
    if (stream != 0)
        outchar_stream(stream, prv);

    /* return the next character */
    return nextout(s, slen);
}

/* ------------------------------------------------------------------------ */
/*
 *   Read an HTML tag, for our primitive mini-parser.  If 'stream' is not
 *   null, we'll copy each character we read to the output stream.  Returns
 *   the next character after the tag name.
 */
static char read_tag(char *dst, size_t dstlen, int *is_end_tag,
                     char **s, size_t *slen, out_stream_info *stream)
{
    char c;
    
    /* skip the opening '<' */
    c = nextout_copy(s, slen, '<', stream);

    /* skip spaces */
    while (outissp(c))
        c = nextout_copy(s, slen, c, stream);

    /* note if this is a closing tag */
    if (c == '/' || c == '\\')
    {
        /* it's an end tag - note it and skip the slash */
        *is_end_tag = TRUE;
        c = nextout_copy(s, slen, c, stream);

        /* skip yet more spaces */
        while (outissp(c))
            c = nextout_copy(s, slen, c, stream);
    }
    else
        *is_end_tag = FALSE;
    
    /* 
     *   find the end of the tag name - the tag continues to the next space,
     *   '>', or end of line 
     */
    for ( ; c != '\0' && !outissp(c) && c != '>' ;
         c = nextout_copy(s, slen, c, stream))
    {
        /* add this to the tag buffer if it fits */
        if (dstlen > 1)
        {
            *dst++ = c;
            --dstlen;
        }
    }
    
    /* null-terminate the tag name */
    if (dstlen > 0)
        *dst = '\0';

    /* return the next character */
    return c;
}


/* ------------------------------------------------------------------------ */
/*
 *   display a string of a given length to a given stream 
 */
static int outformatlen_stream(out_stream_info *stream,
                               char *s, size_t slen)
{
    char     c;
    int      done = 0;
    char     fmsbuf[40];       /* space for constructing translation string */
    uint     fmslen;
    char    *f = 0;
    char    *f1;
    int      infmt = 0;

    /* 
     *   This routine can recurse because of format strings ("%xxx%"
     *   sequences).  When we recurse, we want to ensure that the
     *   recursion is directed to the original stream only.  So, note the
     *   current stream statically in case we re-enter the formatter. 
     */
    if (!out_push_stream(stream))
        return 0;

    /* get the first character */
    c = nextout(&s, &slen);

    /* if we have anything to show, show it */
    while (c != '\0')
    {
        /* check if we're collecting translation string */
        if (infmt)
        {
            /*
             *   if the string is too long for our buffer, or we've come
             *   across a backslash (illegal in a format string), or we've
             *   come across an HTML-significant character ('&' or '<') in
             *   HTML mode, we must have a stray percent sign; dump the
             *   whole string so far and act as though we have no format
             *   string 
             */
            if (c == '\\'
                || f == &fmsbuf[sizeof(fmsbuf)]
                || (stream->html_mode && (c == '<' || c == '&')))
            {
                outchar_stream(stream, '%');
                for (f1 = fmsbuf ; f1 < f ; ++f1)
                    outchar_stream(stream, *f1);
                infmt = 0;

                /* process this character again */
                continue;
            }
            else if (c == '%' && f == fmsbuf)       /* double percent sign? */
            {
                outchar_stream(stream, '%');       /* send out a single '%' */
                infmt = 0;       /* no longer processing translation string */
            }
            else if (c == '%')   /* found end of string? translate it if so */
            {
                uchar *fms;
                int    initcap = FALSE;
                int    allcaps = FALSE;
                char   fmsbuf_srch[sizeof(fmsbuf)];

                /* null-terminate the string */
                *f = '\0';

                /* check for an init cap */
                if (outisup(fmsbuf[0]))
                {
                    /* 
                     *   note the initial capital, so that we follow the
                     *   original capitalization in the substituted string 
                     */
                    initcap = TRUE;

                    /* 
                     *   if the second letter is capitalized as well,
                     *   capitalize the entire substituted string 
                     */
                    if (fmsbuf[1] != '\0' && outisup(fmsbuf[1]))
                    {
                        /* use all caps */
                        allcaps = TRUE;
                    }
                }

                /* convert the entire string to lower case for searching */
                strcpy(fmsbuf_srch, fmsbuf);
                os_strlwr(fmsbuf_srch);
                
                /* find the string in the format string table */
                fmslen = strlen(fmsbuf_srch);
                for (fms = fmsbase ; fms < fmstop ; )
                {
                    uint propnum;
                    uint len;

                    /* get the information on this entry */
                    propnum = osrp2(fms);
                    len = osrp2(fms + 2) - 2;

                    /* check for a match */
                    if (len == fmslen &&
                        !memcmp(fms + 4, fmsbuf_srch, (size_t)len))
                    {
                        int old_all_caps;

                        /* note the current ALLCAPS mode */
                        old_all_caps = stream->allcapsflag;
                        
                        /* 
                         *   we have a match - set the appropriate
                         *   capitalization mode 
                         */
                        if (allcaps)
                            outallcaps_stream(stream, TRUE);
                        else if (initcap)
                            outcaps_stream(stream);

                        /* 
                         *   evaluate the associated property to generate
                         *   the substitution text 
                         */
                        runppr(runctx, cmdActor, (prpnum)propnum, 0);

                        /* turn off ALLCAPS mode */
                        outallcaps_stream(stream, old_all_caps);

                        /* no need to look any further */
                        break;
                    }

                    /* move on to next formatstring if not yet found */
                    fms += len + 4;
                }

                /* if we can't find it, dump the format string as-is */
                if (fms == fmstop)
                {
                    outchar_stream(stream, '%');
                    for (f1 = fmsbuf ; f1 < f ; ++f1)
                        outchar_stream(stream, *f1);
                    outchar_stream(stream, '%');
                }

                /* no longer reading format string */
                infmt = 0;
            }
            else
            {
                /* copy this character of the format string */
                *f++ = c;
            }

            /* move on to the next character and continue scanning */
            c = nextout(&s, &slen);
            continue;
        }
        
        /*
         *   If we're parsing HTML here, and we're inside a tag, skip
         *   characters until we reach the end of the tag.  
         */
        if (stream->html_mode_flag != HTML_MODE_NORMAL)
        {
            switch(stream->html_mode_flag)
            {
            case HTML_MODE_TAG:
                /* 
                 *   keep skipping up to the closing '>', but note when we
                 *   enter any quoted section 
                 */
                switch(c)
                {
                case '>':
                    /* we've reached the end of the tag */
                    stream->html_mode_flag = HTML_MODE_NORMAL;

                    /* if we have a deferred <BR>, process it now */
                    switch(stream->html_defer_br)
                    {
                    case HTML_DEFER_BR_NONE:
                        /* no deferred <BR> */
                        break;

                    case HTML_DEFER_BR_FLUSH:
                        outflushn_stream(stream, 1);
                        break;

                    case HTML_DEFER_BR_BLANK:
                        outblank_stream(stream);
                        break;
                    }

                    /* no more deferred <BR> pending */
                    stream->html_defer_br = HTML_DEFER_BR_NONE;

                    /* no more ALT attribute allowed */
                    stream->html_allow_alt = FALSE;
                    break;

                case '"':
                    /* enter a double-quoted string */
                    stream->html_mode_flag = HTML_MODE_DQUOTE;
                    break;

                case '\'':
                    /* enter a single-quoted string */
                    stream->html_mode_flag = HTML_MODE_SQUOTE;
                    break;

                default:
                    /* if it's alphabetic, note the attribute name */
                    if (outisal(c))
                    {
                        char attrname[128];
                        char attrval[256];
                        char *dst;

                        /* gather up the attribute name */
                        for (dst = attrname ;
                             dst + 1 < attrname + sizeof(attrname) ; )
                        {
                            /* store this character */
                            *dst++ = c;

                            /* get the next character */
                            c = nextout(&s, &slen);

                            /* if it's not alphanumeric, stop scanning */
                            if (!outisal(c) && !outisdg(c))
                                break;
                        }

                        /* null-terminate the result */
                        *dst++ = '\0';

                        /* gather the value if present */
                        if (c == '=')
                        {
                            char qu;
                            
                            /* skip the '=' */
                            c = nextout(&s, &slen);

                            /* if we have a quote, so note */
                            if (c == '"' || c == '\'')
                            {
                                /* remember the quote */
                                qu = c;

                                /* skip it */
                                c = nextout(&s, &slen);
                            }
                            else
                            {
                                /* no quote */
                                qu = 0;
                            }

                            /* read the value */
                            for (dst = attrval ;
                                 dst + 1 < attrval + sizeof(attrval) ; )
                            {
                                /* store this character */
                                *dst++ = c;

                                /* read the next one */
                                c = nextout(&s, &slen);
                                if (c == '\0')
                                {
                                    /* 
                                     *   we've reached the end of the
                                     *   string, and we're still inside
                                     *   this attribute - abandon the
                                     *   attribute but note that we're
                                     *   inside a quoted string if
                                     *   necessary 
                                     */
                                    if (qu == '"')
                                        stream->html_mode_flag =
                                            HTML_MODE_DQUOTE;
                                    else if (qu == '\'')
                                        stream->html_mode_flag =
                                            HTML_MODE_SQUOTE;
                                    else
                                        stream->html_mode_flag
                                            = HTML_MODE_TAG;

                                    /* stop scanning the string */
                                    break;
                                }

                                /* 
                                 *   if we're looking for a quote, check
                                 *   for the closing quote; otherwise,
                                 *   check for alphanumerics 
                                 */
                                if (qu != 0)
                                {
                                    /* if this is our quote, stop scanning */
                                    if (c == qu)
                                        break;
                                }
                                else
                                {
                                    /* if it's non-alphanumeric, we're done */
                                    if (!outisal(c) && !outisdg(c))
                                        break;
                                }
                            }

                            /* skip the closing quote, if necessary */
                            if (qu != 0 && c == qu)
                                c = nextout(&s, &slen);

                            /* null-terminate the value string */
                            *dst = '\0';
                        }
                        else
                        {
                            /* no value */
                            attrval[0] = '\0';
                        }

                        /* 
                         *   see if we recognize it, and it's meaningful
                         *   in the context of the current tag 
                         */
                        if (!scumm_stricmp(attrname, "height")
                            && stream->html_defer_br != HTML_DEFER_BR_NONE)
                        {
                            int ht;
                                
                            /*
                             *   If the height is zero, always treat this
                             *   as a non-blanking flush.  If it's one,
                             *   treat it as we originally planned to.  If
                             *   it's greater than one, add n blank lines.
                             */
                            ht = atoi(attrval);
                            if (ht == 0)
                            {
                                /* always use non-blanking flush */
                                stream->html_defer_br = HTML_DEFER_BR_FLUSH;
                            }
                            else if (ht == 1)
                            {
                                /* keep original setting */
                            }
                            else
                            {
                                for ( ; ht > 0 ; --ht)
                                    outblank_stream(stream);
                            }
                        }
                        else if (!scumm_stricmp(attrname, "alt")
                                 && !stream->html_in_ignore
                                 && stream->html_allow_alt)
                        {
                            /* write out the ALT string */
                            outstring_stream(stream, attrval);
                        }

                        /* 
                         *   since we already read the next character,
                         *   simply loop back immediately 
                         */
                        continue;
                    }
                    break;
                }
                break;

            case HTML_MODE_DQUOTE:
                /* if we've reached the closing quote, return to tag state */
                if (c == '"')
                    stream->html_mode_flag = HTML_MODE_TAG;
                break;

            case HTML_MODE_SQUOTE:
                /* if we've reached the closing quote, return to tag state */
                if (c == '\'')
                    stream->html_mode_flag = HTML_MODE_TAG;
                break;
            }

            /* 
             *   move on to the next character, and start over with the
             *   new character 
             */
            c = nextout(&s, &slen);
            continue;
        }

        /*
         *   If we're in a title, and this isn't the start of a new tag,
         *   skip the character - we suppress all regular text output
         *   inside a <TITLE> ... </TITLE> sequence. 
         */
        if (stream->html_in_ignore && c != '<')
        {
            /* check for entities */
            char cbuf[50];
            if (c == '&')
            {
                /* translate the entity */
                c = out_parse_entity(cbuf, sizeof(cbuf), &s, &slen);
            }
            else
            {
                /* it's an ordinary character - copy it out literally */
                cbuf[0] = c;
                cbuf[1] = '\0';

                /* get the next character */
                c = nextout(&s, &slen);
            }

            /* 
             *   if we're gathering a title, and there's room in the title
             *   buffer for more (always leaving room for a null
             *   terminator), add this to the title buffer 
             */
            if (stream->html_in_title)
            {
                char *cbp;
                for (cbp = cbuf ; *cbp != '\0' ; ++cbp)
                {
                    /* if there's room, add it */
                    if (stream->html_title_ptr + 1 <
                        stream->html_title_buf
                        + sizeof(stream->html_title_buf))
                        *stream->html_title_ptr++ = *cbp;
                }
            }

            /* don't display anything in an ignore section */
            continue;
        }
        
        if ( c == '%' )                              /* translation string? */
        {
            infmt = 1;
            f = fmsbuf;
        }
        else if ( c == '\\' )                       /* special escape code? */
        {
            c = nextout(&s, &slen);
            
            if (stream->capturing && c != '^' && c != 'v' && c != '\0')
            {
                outchar_stream(stream, '\\');
                outchar_stream(stream, c);

                /* keep the \- and also put out the next two chars */
                if (c == '-')
                {
                    outchar_stream(stream, nextout(&s, &slen));
                    outchar_stream(stream, nextout(&s, &slen));
                }
            }
            else
            {
                switch(c)
                {
                case 'H':                                /* HTML mode entry */
                    /* turn on HTML mode in the renderer */
                    switch(c = nextout(&s, &slen))
                    {
                    case '-':
                        /* if we have an HTML target, notify it */
                        if (stream->html_target)
                        {
                            /* flush its stream */
                            outflushn_stream(stream, 0);

                            /* tell the OS layer to switch to normal mode */
                            out_end_html(stream);
                        }

                        /* switch to normal mode */
                        stream->html_mode = FALSE;
                        break;

                    case '+':
                    default:
                        /* if we have an HTML target, notify it */
                        if (stream->html_target)
                        {
                            /* flush the underlying stream */
                            outflushn_stream(stream, 0);

                            /* tell the OS layer to switch to HTML mode */
                            out_start_html(stream);
                        }
                        
                        /* switch to HTML mode */
                        stream->html_mode = TRUE;

                        /* 
                         *   if the character wasn't a "+", it's not part
                         *   of the "\H" sequence, so display it normally 
                         */
                        if (c != '+' && c != 0)
                            outchar_stream(stream, c);
                        break;
                    }

                    /* this sequence doesn't result in any actual output */
                    break;

                case 'n':                                       /* newline? */
                    outflushn_stream(stream, 1);        /* yes, output line */
                    break;
                    
                case 't':                                           /* tab? */
                    outtab_stream(stream);
                    break;
                    
                case 'b':                                    /* blank line? */
                    outblank_stream(stream);
                    break;
                    
                case '\0':                               /* line ends here? */
                    done = 1;
                    break;

                case ' ':                                   /* quoted space */
                    if (stream->html_target && stream->html_mode)
                    {
                        /* 
                         *   we're generating for an HTML target and we're
                         *   in HTML mode - generate the HTML non-breaking
                         *   space 
                         */
                        outstring_stream(stream, "&nbsp;");
                    }
                    else
                    {
                        /* 
                         *   we're not in HTML mode - generate our
                         *   internal quoted space character 
                         */
                        outchar_stream(stream, QSPACE);
                    }
                    break;

                case '^':                      /* capitalize next character */
                    stream->capsflag = 1;
                    stream->nocapsflag = 0;
                    break;

                case 'v':
                    stream->nocapsflag = 1;
                    stream->capsflag = 0;
                    break;

                case '(':
                    /* generate HTML if in the appropriate mode */
                    if (stream->html_mode && stream->html_target)
                    {
                        /* send HTML to the renderer */
                        outstring_stream(stream, "<B>");
                    }
                    else
                    {
                        /* turn on the 'hilite' attribute */
                        stream->cur_attr |= OS_ATTR_HILITE;
                    }
                    break;

                case ')':
                    /* generate HTML if in the appropriate mode */
                    if (stream->html_mode && stream->html_target)
                    {
                        /* send HTML to the renderer */
                        outstring_stream(stream, "</B>");
                    }
                    else
                    {
                        /* turn off the 'hilite' attribute */
                        stream->cur_attr &= ~OS_ATTR_HILITE;
                    }
                    break;

                case '-':
                    outchar_stream(stream, nextout(&s, &slen));
                    outchar_stream(stream, nextout(&s, &slen));
                    break;
                    
                default:                 /* just pass invalid escapes as-is */
                    outchar_stream(stream, c);
                    break;
                }
            }
        }
        else if (!stream->html_target
                 && stream->html_mode
                 && (c == '<' || c == '&'))
        {
            /*
             *   We're in HTML mode, but the underlying target does not
             *   accept HTML sequences.  It appears we're at the start of
             *   an "&" entity or a tag sequence, so parse it, remove it,
             *   and replace it (if possible) with a text-only equivalent. 
             */
            if (c == '<')
            {
                /* read the tag */
                char tagbuf[50];
                int is_end_tag;
                c = read_tag(tagbuf, sizeof(tagbuf), &is_end_tag,
                             &s, &slen, 0);

                /*
                 *   Check to see if we recognize the tag.  We only
                 *   recognize a few simple tags that map easily to
                 *   character mode. 
                 */
                if (!scumm_stricmp(tagbuf, "br"))
                {
                    /* 
                     *   line break - if there's anything buffered up,
                     *   just flush the current line, otherwise write out
                     *   a blank line 
                     */
                    if (stream->html_in_ignore)
                        /* suppress breaks in ignore mode */;
                    else if (stream->linepos != 0)
                        stream->html_defer_br = HTML_DEFER_BR_FLUSH;
                    else
                        stream->html_defer_br = HTML_DEFER_BR_BLANK;
                }
                else if (!scumm_stricmp(tagbuf, "b")
                         || !scumm_stricmp(tagbuf, "i")
                         || !scumm_stricmp(tagbuf, "em")
                         || !scumm_stricmp(tagbuf, "strong"))
                {
                    int attr;
                    
                    /* choose the attribute flag */
                    switch (tagbuf[0])
                    {
                    case 'b':
                    case 'B':
                        attr = OS_ATTR_BOLD;
                        break;

                    case 'i':
                    case 'I':
                        attr = OS_ATTR_ITALIC;
                        break;

                    case 'e':
                    case 'E':
                        attr = OS_ATTR_EM;
                        break;

                    case 's':
                    case 'S':
                        attr = OS_ATTR_STRONG;
                        break;
                    }
                    
                    /* bold on/off - send out appropriate os-layer code */
                    if (stream->html_in_ignore)
                    {
                        /* suppress any change in 'ignore' mode */
                    }
                    else if (!is_end_tag)
                    {
                        /* turn on the selected attribute */
                        stream->cur_attr |= attr;
                    }
                    else
                    {
                        /* turn off the selected attribute */
                        stream->cur_attr &= ~attr;
                    }
                }
                else if (!scumm_stricmp(tagbuf, "p"))
                {
                    /* paragraph - send out a blank line */
                    if (!stream->html_in_ignore)
                        outblank_stream(stream);
                }
                else if (!scumm_stricmp(tagbuf, "tab"))
                {
                    /* tab - send out a \t */
                    if (!stream->html_in_ignore)
                        outtab_stream(stream);
                }
                else if (!scumm_stricmp(tagbuf, "img") || !scumm_stricmp(tagbuf, "sound"))
                {
                    /* IMG and SOUND - allow ALT attributes */
                    stream->html_allow_alt = TRUE;
                }
                else if (!scumm_stricmp(tagbuf, "hr"))
                {
                    int rem;
                    
                    if (!stream->html_in_ignore)
                    {
                        /* start a new line */
                        outflushn_stream(stream, 1);

                        /* write out underscores to the display width */
                        for (rem = G_os_linewidth - 1 ; rem > 0 ; )
                        {
                            char dashbuf[100];
                            int cur;
                            
                            /* do as much as we can on this pass */
                            cur = rem;
                            if ((size_t)cur > sizeof(dashbuf) - 1)
                                cur = sizeof(dashbuf) - 1;
                            
                            /* do a buffer-full of dashes */
                            memset(dashbuf, '_', cur);
                            dashbuf[cur] = '\0';
                            outstring_stream(stream, dashbuf);
                            
                            /* deduct this from the total */
                            rem -= cur;
                        }

                        /* put a blank line after the underscores */
                        outblank_stream(stream);
                    }
                }
                else if (!scumm_stricmp(tagbuf, "q"))
                {
                    unsigned int htmlchar;

                    if (!stream->html_in_ignore)
                    {
                        /* if it's an open quote, increment the level */
                        if (!is_end_tag)
                            ++(stream->html_quote_level);
                        
                        /* add the open quote */
                        htmlchar =
                            (!is_end_tag
                             ? ((stream->html_quote_level & 1) == 1
                                ? 8220 : 8216)
                             : ((stream->html_quote_level & 1) == 1
                                ? 8221 : 8217));
                        
                        /* 
                         *   write out the HTML character, translated to
                         *   the local character set 
                         */
                        outchar_html_stream(stream, htmlchar);
                        
                        /* if it's a close quote, decrement the level */
                        if (is_end_tag)
                            --(stream->html_quote_level);
                    }
                }
                else if (!scumm_stricmp(tagbuf, "title"))
                {
                    /* 
                     *   Turn ignore mode on or off as appropriate, and
                     *   turn on or off title mode as well.
                     */
                    if (is_end_tag)
                    {
                        /* 
                         *   note that we're leaving an ignore section and
                         *   a title section 
                         */
                        --(stream->html_in_ignore);
                        --(stream->html_in_title);

                        /* 
                         *   if we're no longer in a title, call the OS
                         *   layer to tell it the title string, in case it
                         *   wants to change the window title or otherwise
                         *   make use of the title 
                         */
                        if (stream->html_in_title == 0)
                        {
                            /* null-terminate the title string */
                            *stream->html_title_ptr = '\0';
                            
                            /* tell the OS about the title */
                            os_set_title(stream->html_title_buf);
                        }
                    }
                    else
                    {
                        /* 
                         *   if we aren't already in a title, set up to
                         *   capture the title into the title buffer 
                         */
                        if (!stream->html_in_title)
                            stream->html_title_ptr = stream->html_title_buf;

                        /* 
                         *   note that we're in a title and in an ignore
                         *   section, since nothing within gets displayed 
                         */
                        ++(stream->html_in_ignore);
                        ++(stream->html_in_title);
                    }
                }
                else if (!scumm_stricmp(tagbuf, "aboutbox"))
                {
                    /* turn ignore mode on or off as appropriate */
                    if (is_end_tag)
                        --(stream->html_in_ignore);
                    else
                        ++(stream->html_in_ignore);
                }
                else if (!scumm_stricmp(tagbuf, "pre"))
                {
                    /* count the nesting level if starting PRE mode */
                    if (!is_end_tag)
                        stream->html_pre_level += 1;

                    /* surround the PRE block with line breaks */
                    outblank_stream(stream);

                    /* count the nesting level if ending PRE mode */
                    if (is_end_tag && stream->html_pre_level != 0)
                        stream->html_pre_level -= 1;
                }

                /* suppress everything up to the next '>' */
                stream->html_mode_flag = HTML_MODE_TAG;

                /* 
                 *   continue with the current character; since we're in
                 *   html tag mode, we'll skip everything until we get to
                 *   the closing '>' 
                 */
                continue;
            }
            else if (c == '&')
            {
                /* parse it */
                char  xlat_buf[50];
                c = out_parse_entity(xlat_buf, sizeof(xlat_buf), &s, &slen);

                /* write it out (we've already translated it) */
                outstring_noxlat_stream(stream, xlat_buf);
                
                /* proceed with the next character */
                continue;
            }
        }
        else if (stream->html_target && stream->html_mode && c == '<')
        {
            /*
             *   We're in HTML mode, and we have an underlying HTML target.
             *   We don't need to do much HTML interpretation at this level.
             *   However, we do need to keep track of when we're in a PRE
             *   block, so that we can pass whitespaces and newlines through
             *   to the underlying HTML engine without filtering when we're
             *   in preformatted text. 
             */
            char tagbuf[50];
            int is_end_tag;
            c = read_tag(tagbuf, sizeof(tagbuf), &is_end_tag,
                         &s, &slen, stream);

            /* check for special tags */
            if (!scumm_stricmp(tagbuf, "pre"))
            {
                /* count the nesting level */
                if (!is_end_tag)
                    stream->html_pre_level += 1;
                else if (is_end_tag && stream->html_pre_level != 0)
                    stream->html_pre_level -= 1;
            }

            /* copy the last character after the tag to the stream */
            outchar_stream(stream, c);
        }
        else
        {
            /* normal character */
            outchar_stream(stream, c);
        }

        /* move on to the next character, unless we're finished */
        if (done)
            c = '\0';
        else
            c = nextout(&s, &slen);
    }

    /* if we ended up inside what looked like a format string, dump string */
    if (infmt)
    {
        outchar_stream(stream, '%');
        for (f1 = fmsbuf ; f1 < f ; ++f1)
            outchar_stream(stream, *f1);
    }

    /* exit a recursion level */
    out_pop_stream();

    /* success */
    return 0;
}

/* ------------------------------------------------------------------------ */
/*
 *   Parse an HTML entity markup 
 */
static char out_parse_entity(char *outbuf, size_t outbuf_size,
                             char **sp, size_t *slenp)
{
    char  ampbuf[10];
    char *dst;
    char *orig_s;
    size_t orig_slen;
    const amp_tbl_t *ampptr;
    size_t lo, hi, cur;
    char  c;

    /* 
     *   remember where the part after the '&' begins, so we can come back
     *   here later if necessary 
     */
    orig_s = *sp;
    orig_slen = *slenp;

    /* get the character after the ampersand */
    c = nextout(sp, slenp);

    /* if it's numeric, parse the number */
    if (c == '#')
    {
        uint val;
        
        /* skip the '#' */
        c = nextout(sp, slenp);

        /* check for hex */
        if (c == 'x' || c == 'X')
        {
            /* skip the 'x' */
            c = nextout(sp, slenp);

            /* read the hex number */
            for (val = 0 ; Common::isXDigit((uchar)c) ; c = nextout(sp, slenp))
            {
                /* accumulate the current digit into the value */
                val *= 16;
                if (outisdg(c))
                    val += c - '0';
                else if (c >= 'a' && c <= 'f')
                    val += c - 'a' + 10;
                else
                    val += c - 'A' + 10;
            }
        }
        else
        {
            /* read the number */
            for (val = 0 ; outisdg(c) ; c = nextout(sp, slenp))
            {
                /* accumulate the current digit into the value */
                val *= 10;
                val += c - '0';
            }
        }
        
        /* if we found a ';' at the end, skip it */
        if (c == ';')
            c = nextout(sp, slenp);
        
        /* translate the character into the output buffer */
        os_xlat_html4(val, outbuf, outbuf_size);
        
        /* we're done with this character */
        return c;
    }

    /*
     *   Parse the sequence after the '&'.  Parse up to the closing
     *   semicolon, or any non-alphanumeric, or until we fill up the buffer.
     */
    for (dst = ampbuf ;
         c != '\0' && (outisdg(c) || outisal(c))
             && dst < ampbuf + sizeof(ampbuf) - 1 ;
         *dst++ = c, c = nextout(sp, slenp)) ;
    
    /* null-terminate the name */
    *dst = '\0';
    
    /* do a binary search for the name */
    lo = 0;
    hi = sizeof(amp_tbl)/sizeof(amp_tbl[0]) - 1;
    for (;;)
    {
        int diff;
        
        /* if we've converged, look no further */
        if (lo > hi || lo >= sizeof(amp_tbl)/sizeof(amp_tbl[0]))
        {
            ampptr = 0;
            break;
        }
        
        /* split the difference */
        cur = lo + (hi - lo)/2;
        ampptr = &amp_tbl[cur];
        
        /* see where we are relative to the target item */
        diff = strcmp(ampptr->cname, ampbuf);
        if (diff == 0)
        {
            /* this is it */
            break;
        }
        else if (diff > 0)
        {
            /* make sure we don't go off the end */
            if (cur == hi && cur == 0)
            {
                /* we've failed to find it */
                ampptr = 0;
                break;
            }
            
            /* this one is too high - check the lower half */
            hi = (cur == hi ? hi - 1 : cur);
        }
        else
        {
            /* this one is too low - check the upper half */
            lo = (cur == lo ? lo + 1 : cur);
        }
    }
    
    /* skip to the appropriate next character */
    if (c == ';')
    {
        /* name ended with semicolon - skip the semicolon */
        c = nextout(sp, slenp);
    }
    else if (ampptr != 0)
    {
        int skipcnt;
        
        /* found the name - skip its exact length */
        skipcnt = strlen(ampptr->cname);
        for (*sp = orig_s, *slenp = orig_slen ; skipcnt != 0 ;
             c = nextout(sp, slenp), --skipcnt) ;
    }
    
    /* if we found the entry, write out the character */
    if (ampptr != 0)
    {
        /* 
         *   if this one has an external mapping table entry, use the mapping
         *   table entry; otherwise, use the default OS routine mapping 
         */
        if (ampptr->expan != 0)
        {
            /* 
             *   we have an explicit expansion from the mapping table file -
             *   use it 
             */
            size_t copylen = strlen(ampptr->expan);
            if (copylen > outbuf_size - 1)
                copylen = outbuf_size - 1;

            memcpy(outbuf, ampptr->expan, copylen);
            outbuf[copylen] = '\0';
        }
        else
        {
            /* 
             *   there's no mapping table expansion - use the default OS code
             *   expansion 
             */
            os_xlat_html4(ampptr->html_cval, outbuf, outbuf_size);
        }
    }
    else
    {
        /* 
         *   didn't find it - output the '&' literally, then back up and
         *   output the entire sequence following 
         */
        *sp = orig_s;
        *slenp = orig_slen;
        c = nextout(sp, slenp);

        /* fill in the '&' return value */
        outbuf[0] = '&';
        outbuf[1] = '\0';
    }

    /* return the next character */
    return c;
}
                      


/* ------------------------------------------------------------------------ */
/*
 *   Initialize the output formatter 
 */
void out_init()
{
    /* not yet hiding output */
    outflag = 1;
    outcnt = 0;
    hidout = 0;

    /* initialize the standard display stream */
    out_init_std(&G_std_disp);

    /* initialize the log file stream */
    out_init_log(&G_log_disp);
}


/* ------------------------------------------------------------------------ */
/* 
 *   initialize the property translation table 
 */
void tiosetfmt(tiocxdef *ctx, runcxdef *rctx, uchar *fbase, uint flen)
{
    VARUSED(ctx);
    fmsbase = fbase;
    fmstop = fbase + flen;
    runctx = rctx;
}


/* ------------------------------------------------------------------------ */
/*
 *   Map an HTML entity to a local character value.  The character table
 *   reader will call this routine during initialization if it finds HTML
 *   entities in the mapping table file.  We'll remember these mappings
 *   for use in translating HTML entities to the local character set.
 *   
 *   Note that the standard run-time can only display a single character
 *   set, so every HTML entity that we display must be mapped to the
 *   single active native character set.  
 */
void tio_set_html_expansion(unsigned int html_char_val,
                            const char *expansion, size_t expansion_len)
{
    amp_tbl_t *p;

    /* find the character value */
    for (p = amp_tbl ;
         p < amp_tbl + sizeof(amp_tbl)/sizeof(amp_tbl[0]) ; ++p)
    {
        /* if this is the one, store it */
        if (p->html_cval == html_char_val)
        {
            /* allocate space for it */
            p->expan = (char *)osmalloc(expansion_len + 1);

            /* save it */
            memcpy(p->expan, expansion, expansion_len);
            p->expan[expansion_len] = '\0';

            /* no need to look any further */
            return;
        }
    }
}


/* ------------------------------------------------------------------------ */
/*
 *   Write out a c-style (null-terminated) string.
 */
int outformat(char *s)
{
    return outformatlen(s, strlen(s));
}


/* ------------------------------------------------------------------------ */
/*
 *   This routine sends out a string, one character at a time (via outchar).
 *   Escape codes ('\n', and so forth) are handled here.
 */
int outformatlen(char *s, uint slen)
{
    char     c;
    uint     orig_slen;
    char    *orig_s;
    int      ret;
    int      called_filter;

    /* presume we'll return success */
    ret = 0;

    /* presume we won't call the filter function */
    called_filter = FALSE;

    /* if there's a user filter function to invoke, call it */
    if (G_user_filter != MCMONINV)
    {
        /* push the string */
        runpstr(runctx, s, slen, 1);

        /* call the filter */
        runfn(runctx, G_user_filter, 1);

        /* 
         *   note that we called the filter, so that we'll remove the
         *   result of the filter from the stack before we return 
         */
        called_filter = TRUE;

        /* if the result is a string, use it in place of the original text */
        if (runtostyp(runctx) == DAT_SSTRING)
        {
            runsdef val;
            uchar *p;

            /* pop the value */
            runpop(runctx, &val);

            /* 
             *   get the text from the string, and use it as a replacement
             *   for the original string 
             */
            p = val.runsv.runsvstr;
            slen = osrp2(p) - 2;
            s = (char *)(p + 2);

            /* 
             *   push the string back onto the stack - this will ensure
             *   that the string stays referenced while we're working, so
             *   that the garbage collector won't delete it 
             */
            runrepush(runctx, &val);
        }
    }

    /* remember the original string, before we scan the first character */
    orig_s = s;
    orig_slen = slen;

    /* get the first character to display */
    c = nextout(&s, &slen);

    /* if the string is non-empty, note that we've displayed something */
    if (c != 0)
        outcnt = 1;

    /* check to see if we're hiding output */
    if (out_is_hidden())
        goto done;

    /* if the debugger is showing watchpoints, suppress all output */
    if (outwxflag)
        goto done;

    /* display the string */
    ret = outformatlen_stream(&G_std_disp, orig_s, orig_slen);

    /* if there's a log file, write to the log file as well */
    if (logfp != 0)
    {
        outformatlen_stream(&G_log_disp, orig_s, orig_slen);
        osfflush(logfp);
    }

done:
    /* if we called the filter, remove the result from the stack */
    if (called_filter)
        rundisc(runctx);

    /* return the result from displaying to the screen */
    return ret;
}

/* ------------------------------------------------------------------------ */
/*
 *   Display a blank line 
 */
void outblank()
{
    /* note that we've displayed something */
    outcnt = 1;

    /* check to see if we're hiding output */
    if (out_is_hidden())
        return;

    /* generate the newline to the standard display */
    outblank_stream(&G_std_disp);

    /* if we're logging, generate the newline to the log file as well */
    if (logfp != 0)
    {
        outblank_stream(&G_log_disp);
        osfflush(logfp);
    }
}


/* ------------------------------------------------------------------------ */
/*
 *   outcaps() - sets an internal flag which makes the next letter output
 *   a capital, whether it came in that way or not.  Set the same state in
 *   both formatters (standard and log).  
 */
void outcaps(void)
{
    outcaps_stream(&G_std_disp);
    outcaps_stream(&G_log_disp);
}

/*
 *   outnocaps() - sets the next letter to a miniscule, whether it came in
 *   that way or not.  
 */
void outnocaps(void)
{
    outnocaps_stream(&G_std_disp);
    outnocaps_stream(&G_log_disp);
}

/* ------------------------------------------------------------------------ */
/*
 *   Open a log file 
 */
int tiologopn(tiocxdef *ctx, char *fn)
{
    /* if there's an old log file, close it */
    if (tiologcls(ctx))
        return 1;

    /* save the filename for later */
    strcpy(logfname, fn);

    /* open the new file */
    logfp = osfopwt(fn, OSFTLOG);

    /* 
     *   Reset the log file's output formatter state, since we're opening
     *   a new file.  
     */
    out_init_log(&G_log_disp);

    /* 
     *   Set the log file's HTML source mode flag to the same value as is
     *   currently being used in the main display stream, so that it will
     *   interpret source markups the same way that the display stream is
     *   going to.  
     */
    G_log_disp.html_mode = G_std_disp.html_mode;

    /* return 0 on success, non-zero on failure */
    return (logfp == 0);
}

/*
 *   Close the log file 
 */
int tiologcls(tiocxdef *ctx)
{
    /* if we have a file, close it */
    if (logfp != 0)
    {
        /* close the handle */
        osfcls(logfp);

        /* set the system file type to "log file" */
        os_settype(logfname, OSFTLOG);

        /* forget about our log file handle */
        logfp = 0;
    }

    /* success */
    return 0;
}

/* ------------------------------------------------------------------------ */
/*
 *   Write text explicitly to the log file.  This can be used to add
 *   special text (such as prompt text) that would normally be suppressed
 *   from the log file.  When more mode is turned off, we don't
 *   automatically copy text to the log file; any text that the caller
 *   knows should be in the log file during times when more mode is turned
 *   off can be explicitly added with this function.
 *   
 *   If nl is true, we'll add a newline at the end of this text.  The
 *   caller should not include any newlines in the text being displayed
 *   here.  
 */
void out_logfile_print(char *txt, int nl)
{
    /* if there's no log file, there's nothing to do */
    if (logfp == 0)
        return;

    /* add the text */
    os_fprintz(logfp, txt);

    /* add a newline if desired */
    if (nl)
    {
        /* add a normal newline */
        os_fprintz(logfp, "\n");

        /* if the logfile is an html target, write an HTML line break */
        if (G_log_disp.html_target && G_log_disp.html_mode)
            os_fprintz(logfp, "<BR HEIGHT=0>\n");
    }

    /* flush the output */
    osfflush(logfp);
}

/* ------------------------------------------------------------------------ */
/*
 *   Set the current MORE mode 
 */
int setmore(int state)
{
    int oldstate = G_os_moremode;
    
    G_os_moremode = state;
    return oldstate;
}

/* ------------------------------------------------------------------------ */
/*
 *   Run the MORE prompt.  If the output layer takes responsibility for
 *   pagination issues (i.e., USE_MORE is defined), we'll simply display
 *   the prompt and wait for input.  Otherwise, the OS layer controls the
 *   MORE prompt, so we'll call the OS-layer function to display the
 *   prompt.  
 */
void out_more_prompt()
{
#ifdef USE_MORE
    /*
     *   USE_MORE defined - we take responsibility for pagination.  Show
     *   our default MORE prompt and wait for a keystroke.  
     */

    int done;
	int next_page = FALSE;

    /* display the "MORE" prompt */
    os_printz("[More]");
    os_flush();

    /* wait for an acceptable keystroke */
    for (done = FALSE ; !done ; )
    {
        os_event_info_t evt;
        
        /* get an event */
        switch(os_get_event(0, FALSE, &evt))
        {
        case OS_EVT_KEY:
            switch(evt.key[0])
            {
            case ' ':
                /* stop waiting, show one page */
                done = TRUE;
                next_page = TRUE;
                break;
                
            case '\r':
            case '\n':
                /* stop waiting, show one line */
                done = TRUE;
                next_page = FALSE;
                break;

            default:
                /* ignore any other keystrokes */
                break;
            }
            break;

        case OS_EVT_EOF:
            /* end of file - there's nothing to wait for now */
            done = TRUE;
            next_page = TRUE;

            /* don't use more prompts any more, as the user can't respond */
            G_os_moremode = FALSE;
            break;

        default:
            /* ignore other events */
            break;
        }
    }

    /* 
     *   Remove the prompt from the screen by backing up and overwriting
     *   it with spaces.  (Note that this assumes that we're running in
     *   some kind of terminal or character mode with a fixed-pitch font;
     *   if that's not the case, the OS layer should be taking
     *   responsibility for pagination anyway, so this code shouldn't be
     *   in use in the first place.)  
     */
    os_printz("\r      \r");

    /* 
     *   if they pressed the space key, it means that we should show an
     *   entire new page, so reset the line count to zero; otherwise,
     *   we'll want to display another MORE prompt at the very next line,
     *   so leave the line count alone 
     */
    if (next_page)
        G_std_disp.linecnt = 0;

#else /* USE_MORE */

    /*
     *   USE_MORE is undefined - this means that the OS layer is taking
     *   all responsibility for pagination.  We must ask the OS layer to
     *   display the MORE prompt, because we can't make any assumptions
     *   about what the prompt looks like.  
     */

    os_more_prompt();

#endif /* USE_MORE */
}

/* ------------------------------------------------------------------------ */
/*
 *   reset output 
 */
void outreset(void)
{
    G_std_disp.linecnt = 0;
}

/* ------------------------------------------------------------------------ */
/*
 *   Determine if HTML mode is active.  Returns true if so, false if not.
 *   Note that this merely indicates whether an "\H+" sequence is
 *   currently active -- this will return true after an "\H+" sequence,
 *   even on text-only interpreters.  
 */
int tio_is_html_mode()
{
    /* return the current HTML mode flag for the standard display stream */
    return G_std_disp.html_mode;
}


/* ------------------------------------------------------------------------ */
/*
 *   Capture routines.  Capture affects only the standard display output
 *   stream; there's no need to capture information redundantly in the log
 *   file stream.  
 */

/*
 *   Begin/end capturing 
 */
void tiocapture(tiocxdef *tioctx, mcmcxdef *memctx, int flag)
{
    if (flag)
    {
        /* create a new object if necessary */
        if (G_std_disp.capture_obj == MCMONINV)
        {
            mcmalo(memctx, 256, &G_std_disp.capture_obj);
            mcmunlck(memctx, G_std_disp.capture_obj);
        }

        /* remember the memory context */
        G_std_disp.capture_ctx = memctx;
    }

    /* 
     *   remember capture status in the standard output stream as well as
     *   the log stream 
     */
    G_std_disp.capturing = flag;
    G_log_disp.capturing = flag;
}

/* clear all captured output */
void tioclrcapture(tiocxdef *tioctx)
{
    G_std_disp.capture_ofs = 0;
}

/* clear captured output back to a given size */
void tiopopcapture(tiocxdef *tioctx, uint orig_size)
{
    G_std_disp.capture_ofs = orig_size;
}

/* get the object handle of the captured output */
mcmon tiogetcapture(tiocxdef *ctx)
{
    return G_std_disp.capture_obj;
}

/* get the amount of text captured */
uint tiocapturesize(tiocxdef *ctx)
{
    return G_std_disp.capture_ofs;
}

/* ------------------------------------------------------------------------ */
/* 
 *   set the current actor 
 */
void tiosetactor(tiocxdef *ctx, objnum actor)
{
    VARUSED(ctx);
    cmdActor = actor;
}

/*
 *   get the current actor 
 */
objnum tiogetactor(tiocxdef *ctx)
{
    VARUSED(ctx);
    return cmdActor;
}
          
/* ------------------------------------------------------------------------ */
/*
 *   Flush the output line.  We'll write to both the standard display and
 *   the log file, as needed.  
 */
void outflushn(int nl)
{
    /* flush the display stream */
    outflushn_stream(&G_std_disp, nl);

    /* flush the log stream, if we have an open log file */
    if (logfp != 0)
    {
        outflushn_stream(&G_log_disp, nl);
        osfflush(logfp);
    }
}

/*
 *   flush the current line, and start a new line 
 */
void outflush(void)
{
    /* use the common flushing routine in mode 1 (regular newline) */
    outflushn(1);
}

/* ------------------------------------------------------------------------ */
/*
 *   Hidden text routines
 */

/* 
 *   outhide - hide output in the standard display stream 
 */
void outhide(void)
{
    outflag = 0;
    outcnt = 0;
    hidout = 0;
}

/*
 *   Check output status.  Indicate whether output is currently hidden,
 *   and whether any hidden output has occurred.  
 */
void outstat(int *hidden, int *output_occurred)
{
    *hidden = !outflag;
    *output_occurred = outcnt;
}

/* set the flag to indicate that output has occurred */
void outsethidden(void)
{
    outcnt = 1;
    hidout = 1;
}

/*
 *   outshow() - turns output back on, and returns TRUE (1) if any output
 *   has occurred since the last outshow(), FALSE (0) otherwise.
 */
int outshow(void)
{
    /* turn output back on */
    outflag = 1;

    /* if we're debugging, note the end of hidden output */
    if (dbghid && hidout)
    {
        hidout = 0;
        trcsho();
    }

    /* return the flag indicating whether hidden output occurred */
    return outcnt;
}

/* ------------------------------------------------------------------------ */
/*
 *   start/end watchpoint evaluation - suppress all dstring output 
 */
void outwx(int flag)
{
    outwxflag = flag;
}


/* ------------------------------------------------------------------------ */
/*
 *   Set the user filter function.  Setting this to MCMONINV clears the
 *   filter. 
 */
void out_set_filter(objnum filter_fn)
{
    /* remember the filter function */
    G_user_filter = filter_fn;
}

/* ------------------------------------------------------------------------ */
/*
 *   Set the double-space mode 
 */
void out_set_doublespace(int dbl)
{
    /* remember the new setting */
    doublespace = dbl;
}

int tio_askfile(const char *prompt, char *reply, int replen, int prompt_type, os_filetype_t file_type) {
	// let the OS layer handle it
	return os_askfile(prompt, reply, replen, prompt_type, file_type);
}

int tio_input_dialog(int icon_id, const char *prompt, int standard_button_set,
	const char **buttons, int button_count,
	int default_index, int cancel_index) {
	// call the OS implementation
	return os_input_dialog(icon_id, prompt, standard_button_set,
		buttons, button_count,
		default_index, cancel_index);
}

void outfmt(tiocxdef *ctx, uchar *txt) {
    uint len;

    VARUSED(ctx);

    /* read the length prefix */
    len = osrp2(txt) - 2;
    txt += 2;

    /* write out the string */
    tioputslen(ctx, (char *)txt, len);
}

} // End of namespace TADS2
} // End of namespace TADS
} // End of namespace Glk