summaryrefslogtreecommitdiff
path: root/alpine/alpine.c
blob: cb752603247e05e72f11aaf2a86a04328114c67d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
#if !defined(lint) && !defined(DOS)
static char rcsid[] = "$Id: alpine.c 1266 2009-07-14 18:39:12Z hubert@u.washington.edu $";
#endif

/*
 * ========================================================================
 * Copyright 2013-2021 Eduardo Chappa
 * Copyright 2006-2008 University of Washington
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * ========================================================================
 */

#include "headers.h"

#include "../pith/newmail.h"
#include "../pith/init.h"
#include "../pith/sort.h"
#include "../pith/options.h"
#include "../pith/list.h"
#include "../pith/conf.h"
#include "../pith/body.h"

#include "osdep/debuging.h"
#include "osdep/termout.gen.h"
#include "osdep/chnge_pw.h"

#include "alpine.h"
#include "mailindx.h"
#include "mailcmd.h"
#include "addrbook.h"
#include "reply.h"
#include "arg.h"
#include "keymenu.h"
#include "status.h"
#include "context.h"
#include "mailview.h"
#include "imap.h"
#include "xoauth2conf.h"
#include "radio.h"
#include "folder.h"
#include "send.h"
#include "help.h"
#include "titlebar.h"
#include "takeaddr.h"
#include "dispfilt.h"
#include "init.h"
#include "remote.h"
#include "pattern.h"
#include "setup.h"
#include "newuser.h"
#include "adrbkcmd.h"
#include "signal.h"
#include "kblock.h"
#include "ldapconf.h"
#include "roleconf.h"
#include "colorconf.h"
#include "print.h"
#include "after.h"
#include "smime.h"
#include "newmail.h"
#include "xoauth2conf.h"
#ifndef _WINDOWS
#include "../pico/osdep/raw.h"	/* for STD*_FD */
#endif


#define	PIPED_FD	5			/* Some innocuous desc	    */


/* look for my_timer_period in pico directory for an explanation */
int my_timer_period = ((IDLE_TIMEOUT + 1)*1000);

/* byte count used by our gets routine to keep track */
static unsigned long gets_bytes;


/*
 * Internal prototypes
 */
void     convert_args_to_utf8(struct pine *, ARGDATA_S *);
void	 preopen_stayopen_folders(void);
int	 read_stdin_char(char *);
void	 main_redrawer(void);
void     show_main_screen(struct pine *, int, OtherMenu, struct key_menu *, int, Pos *);
void	 do_menu(int, Pos *, struct key_menu *);
int	 choose_setup_cmd(int, MSGNO_S *, SCROLL_S *);
int	 setup_menu(struct pine *);
void	 do_setup_task(int);
void	 queue_init_errors(struct pine *);
void	 process_init_cmds(struct pine *, char **);
void	 goodnight_gracey(struct pine *, int);
void	 pine_read_progress(GETS_DATA *, unsigned long);
int	 remote_pinerc_failure(void);
void	 dump_supported_options(void);
int      prune_folders_ok(void);
void     free_alpine_module_globals(void);
#ifdef	WIN32
char	*pine_user_callback(void);
#endif
#ifdef	_WINDOWS
int	 fkey_mode_callback(int, long);
void	 imap_telemetry_on(void);
void	 imap_telemetry_off(void);
char	*pcpine_help_main(char *);
int	 pcpine_main_cursor(int, long);
#define  main app_main
#endif


typedef struct setup_return_val {
    int cmd;
    int exc;
}SRV_S;


/*
 * strlen of longest label from keymenu, of labels corresponding to
 * commands in the middle of the screen.  9 is length of ListFldrs
 */
#define LONGEST_LABEL 9  /* length of longest label from keymenu */

#define EDIT_EXCEPTION (0x100)


static int   in_panic   = 0;


/*----------------------------------------------------------------------
     main routine -- entry point

  Args: argv, argc -- The command line arguments


 Initialize pine, parse arguments and so on

 If there is a user address on the command line go into send mode and exit,
 otherwise loop executing the various screens in Alpine.

 NOTE: The Windows port def's this to "app_main"
  ----*/

int
main(int argc, char **argv)
{
    ARGDATA_S	 args;
    int		 rv;
    long	 rvl;
    struct pine *pine_state;
    gf_io_t	 stdin_getc = NULL;
    char        *args_for_debug = NULL, *init_pinerc_debugging = NULL;

    /*----------------------------------------------------------------------
          Set up buffering and some data structures
      ----------------------------------------------------------------------*/

    pine_state = new_pine_struct();
    ps_global  = pine_state;

    /*
     * fill in optional pith-offered behavior hooks
     */
    pith_opt_read_msg_prompt	   = read_msg_prompt;
    pith_opt_paint_index_hline	   = paint_index_hline;
    pith_opt_rfc2369_editorial	   = rfc2369_editorial;
    pith_opt_condense_thread_cue   = condensed_thread_cue;
    pith_opt_truncate_sfstr        = truncate_subj_and_from_strings;
    pith_opt_save_and_restore	   = save_and_restore;
    pith_opt_newmail_announce	   = newmail_status_message;
    pith_opt_newmail_check_cue	   = newmail_check_cue;
    pith_opt_checkpoint_cue	   = newmail_check_point_cue;
    pith_opt_icon_text		   = icon_text;
    pith_opt_rd_metadata_name	   = rd_metadata_name;
    pith_opt_remote_pinerc_failure = remote_pinerc_failure;
    pith_opt_reopen_folder	   = ask_mailbox_reopen;
    pith_opt_expunge_prompt	   = expunge_prompt;
    pith_opt_begin_closing	   = expunge_and_close_begins;
    pith_opt_replyto_prompt	   = reply_using_replyto_query;
    pith_opt_reply_to_all_prompt   = reply_to_all_query;
    pith_opt_save_create_prompt	   = create_for_save_prompt;
    pith_opt_daemon_confirm	   = confirm_daemon_send;
    pith_opt_save_size_changed_prompt = save_size_changed_prompt;
    pith_opt_save_index_state	   = setup_index_state;
    pith_opt_filter_pattern_cmd	   = pattern_filter_command;
    pith_opt_get_signature_file	   = get_signature_file;
    pith_opt_pretty_var_name	   = pretty_var_name;
    pith_opt_pretty_feature_name   = pretty_feature_name;
    pith_opt_closing_stream        = titlebar_stream_closing;
    pith_opt_current_expunged	   = mm_expunged_current;
#ifdef	SMIME
    pith_opt_smime_get_passphrase  = smime_get_passphrase;
    pith_smime_import_certificate  = smime_import_certificate;
    pith_smime_enter_password	   = alpine_get_password;
    pith_smime_confirm_save	   = alpine_smime_confirm_save;
#endif
#ifdef	ENABLE_LDAP
    pith_opt_save_ldap_entry       = save_ldap_entry;
#endif

    status_message_lock_init();
    inverse_itokens();

#if	HAVE_SRANDOM
    /*
     * Seed the random number generator with the date & pid.  Random 
     * numbers are used for new mail notification and bug report id's
     */
    srandom(getpid() + time(0));
#endif

    /* need home directory early */
    get_user_info(&ps_global->ui);

    if(!(pine_state->home_dir = our_getenv("HOME")))
      pine_state->home_dir = cpystr(ps_global->ui.homedir);

#ifdef _WINDOWS
    {
	char *p;

	/* normalize path delimiters */
	for(p = pine_state->home_dir; p = strchr(p, '/'); p++)
	  *p='\\';
    }
#endif /* _WINDOWS */

#ifdef DEBUG
    {   size_t len = 0;
	int   i;
	char *p;
	char *no_args = " <no args>";

	for(i = 0; i < argc; i++)
	  len += (strlen(argv[i] ? argv[i] : "")+3);
	
	if(argc == 1)
	  len += strlen(no_args);
	
	p = args_for_debug = (char *)fs_get((len+2) * sizeof(char));
	*p++ = '\n';
	*p = '\0';

	for(i = 0; i < argc; i++){
	    snprintf(p, len+2-(p-args_for_debug), "%s\"%s\"", i ? " " : "", argv[i] ? argv[i] : "");
	    args_for_debug[len+2-1] = '\0';
	    p += strlen(p);
	}
	
	if(argc == 1){
	    strncat(args_for_debug, no_args, len+2-strlen(args_for_debug)-1);
	    args_for_debug[len+2-1] = '\0';
	}
    }
#endif

    /*----------------------------------------------------------------------
           Parse arguments and initialize debugging
      ----------------------------------------------------------------------*/
    pine_args(pine_state, argc, argv, &args);

#ifndef	_WINDOWS
    if(!isatty(0)){
	/*
	 * monkey with descriptors so our normal tty i/o routines don't
	 * choke...
	 */
	dup2(STDIN_FD, PIPED_FD);	/* redirected stdin to new desc */
	dup2(STDERR_FD, STDIN_FD);	/* rebind stdin to the tty	*/
	stdin_getc = read_stdin_char;
	if(stdin_getc){
	  if(args.action == aaURL){
	     display_args_err(
  "Cannot read stdin when using -url\nFor mailto URLs, use \'body=\' instead", 
	     NULL, 1);
	     args_help();
	     exit(-1);
	  } else if (args.action == aaFolder){
	     display_args_err("Cannot take input from pipe when opening a folder", NULL, 1);
	     args_help();
	     exit(-1);
	  }
	}
    }

#else /* _WINDOWS */
    /*
     * We now have enough information to do some of the basic registry settings.
     */
    if(ps_global->update_registry != UREG_NEVER_SET){
	mswin_reg(MSWR_OP_SET
		  | ((ps_global->update_registry == UREG_ALWAYS_SET)
		     ? MSWR_OP_FORCE : 0),
		  MSWR_PINE_DIR, ps_global->pine_dir, (size_t)NULL);
	mswin_reg(MSWR_OP_SET
		  | ((ps_global->update_registry == UREG_ALWAYS_SET)
		     ? MSWR_OP_FORCE : 0),
		  MSWR_PINE_EXE, ps_global->pine_name, (size_t)NULL);
    }

#endif /* _WINDOWS */

    if(ps_global->convert_sigs &&
       (!ps_global->pinerc || !ps_global->pinerc[0])){
	fprintf(stderr, "Use -p <pinerc> with -convert_sigs\n");
	exit(-1);
    }

    /* Windows has its own functions to determine width of a character
     * in the screen, so this is not necessary to do in Window, and
     * using pith_ucs4width does not produce the correct result
     */
#if  !defined(_WINDOWS) && defined(LC_CTYPE)
    { char *s;
      if((s = setlocale(LC_CTYPE, "")) != NULL
	&& strlen(s) >= 5
	&& !strucmp(s+strlen(s)-5, "UTF-8"))
        mail_parameters(NULL, SET_UCS4WIDTH, (void *) pith_ucs4width);
    }
#endif /* !_WINDOWS && LC_CTYPE */
    mail_parameters(NULL, SET_QUOTA, (void *) pine_parse_quota);
    /* set some default timeouts in case pinerc is remote */
    mail_parameters(NULL, SET_OPENTIMEOUT, (void *)(long)30);
    mail_parameters(NULL, SET_READTIMEOUT, (void *)(long)15);
    mail_parameters(NULL, SET_TIMEOUT, (void *) pine_tcptimeout);
    /* could be TO_BAIL_THRESHOLD, 15 seems more appropriate for now */
    pine_state->tcp_query_timeout = 15;

    mail_parameters(NULL, SET_SENDCOMMAND, (void *) pine_imap_cmd_happened);
    mail_parameters(NULL, SET_FREESTREAMSPAREP, (void *) sp_free_callback);
    mail_parameters(NULL, SET_FREEELTSPAREP,    (void *) free_pine_elt);
    mail_parameters(NULL, SET_FREEBODYSPAREP,   (void *) free_body_sparep);
    mail_parameters(NULL, SET_OA2CLIENTGETACCESSCODE, (void *) oauth2_get_access_code);
    mail_parameters(NULL, SET_OA2CLIENTINFO, (void *) oauth2_get_client_info);
    mail_parameters(NULL, SET_OA2DEVICEINFO, (void *) oauth2_set_device_info);

    init_pinerc(pine_state, &init_pinerc_debugging);

#ifdef DEBUG
    /* Since this is specific debugging we don't mind if the
       ifdef is the type of system.
     */
#if defined(HAVE_SMALLOC) || defined(NXT)
    if(ps_global->debug_malloc)
      malloc_debug(ps_global->debug_malloc);
#endif
#ifdef	CSRIMALLOC
    if(ps_global->debug_malloc)
      mal_debug(ps_global->debug_malloc);
#endif

    if(!ps_global->convert_sigs
#ifdef _WINDOWS
       && !ps_global->install_flag
#endif /* _WINDOWS */
	)
      init_debug();

    if(args_for_debug){
	dprint((0, " %s (PID=%ld)\n\n", args_for_debug,
	       (long) getpid()));
	fs_give((void **)&args_for_debug);
    }

    {
	char *env_to_free;
	if((env_to_free = our_getenv("HOME")) != NULL){
	    dprint((2, "Setting home dir from $HOME: \"%s\"\n",
		    env_to_free));
	    fs_give((void **)&env_to_free);
	}
	else{
	    dprint((2, "Setting home dir: \"%s\"\n",
		    pine_state->home_dir ? pine_state->home_dir : "<?>"));
	}
    }

    /* Watch out. Sensitive information in debug file. */
    if(ps_global->debug_imap > 4)
      mail_parameters(NULL, SET_DEBUGSENSITIVE, (void *) TRUE);

#ifndef DEBUGJOURNAL
    if(ps_global->debug_tcp)
#endif
      mail_parameters(NULL, SET_TCPDEBUG, (void *) TRUE);

#ifndef DEBUGJOURNAL
    if(ps_global->debug_http)
#endif
      mail_parameters(NULL, SET_HTTPDEBUG, (void *) TRUE);

#ifdef	_WINDOWS
    mswin_setdebug(debug, debugfile);
    mswin_setdebugoncallback (imap_telemetry_on);
    mswin_setdebugoffcallback (imap_telemetry_off);
    mswin_enableimaptelemetry(ps_global->debug_imap != 0);
#endif
#endif  /* DEBUG */

#ifdef	_WINDOWS
    mswin_setsortcallback(index_sort_callback);
    mswin_setflagcallback(flag_callback);
    mswin_sethdrmodecallback(header_mode_callback);
    mswin_setselectedcallback(any_selected_callback);
    mswin_setzoomodecallback(zoom_mode_callback);
    mswin_setfkeymodecallback(fkey_mode_callback);
#endif

    /*------- Set up c-client drivers -------*/ 
#include "../c-client/linkage.c"

    /*------- ... then tune the drivers just installed -------*/ 
#ifdef	_WINDOWS
    if(_tgetenv(TEXT("HOME")))
      mail_parameters(NULL, SET_HOMEDIR, (void *) pine_state->home_dir);

    mail_parameters(NULL, SET_USERPROMPT, (void *) pine_user_callback);

    /*
     * Sniff the environment for timezone offset.  We need to do this
     * here since Windows needs help figuring out UTC, and will adjust
     * what time() returns based on TZ.  THIS WILL SCREW US because
     * we use time() differences to manage status messages.  So, if 
     * rfc822_date, which calls localtime() and thus needs tzset(),
     * is called while a status message is displayed, it's possible
     * for time() to return a time *before* what we remember as the
     * time we put the status message on the display.  Sheesh.
     */
    tzset();
#else /* !_WINDOWS */
    /*
     * We used to let c-client do this for us automatically, but it declines
     * to do so for root. This forces c-client to establish an environment,
     * even if the uid is 0.
     */
    env_init(ps_global->ui.login, ps_global->ui.homedir);

    /*
     * Install callback to let us know the progress of network reads...
     */
    (void) mail_parameters(NULL, SET_READPROGRESS, (void *)pine_read_progress);
#endif /* !_WINDOWS */

    /*
     * Install callback to handle certificate validation failures,
     * allowing the user to continue if they wish.
     */
    mail_parameters(NULL, SET_SSLCERTIFICATEQUERY, (void *) pine_sslcertquery);
    mail_parameters(NULL, SET_SSLFAILURE, (void *) pine_sslfailure);

    if(init_pinerc_debugging){
        dprint((2, "%s", init_pinerc_debugging));
	fs_give((void **)&init_pinerc_debugging);
    }

    /*
     * Initial allocation of array of stream pool pointers.
     * We do this before init_vars so that we can re-use streams used for
     * remote config files. These sizes may get changed later.
     */
    ps_global->s_pool.max_remstream  = 2;
    dprint((9,
	"Setting initial max_remstream to %d for remote config re-use\n",
	ps_global->s_pool.max_remstream));

    init_vars(pine_state, process_init_cmds);

#if !defined(_WINDOWS) || defined(WINDOWS_UNIXSSL_CERTS)
    set_system_certs_path(pine_state);
    set_system_certs_container(pine_state);
    set_user_certs_path(pine_state);
    set_user_certs_container(pine_state);
    mail_parameters(NULL, SET_SSLCIPHERS, (void *) pine_state->VAR_SSLCIPHERS);
#endif

#ifdef SMIME
    if(F_ON(F_DONT_DO_SMIME, ps_global))
      smime_deinit();
#endif /* SMIME */

#ifdef	ENABLE_NLS
    /*
     * LC_CTYPE is already set from the set_collation call above.
     *
     * We can't use gettext calls before we do this stuff so it doesn't
     * help to translate strings that come before this in the program.
     * Maybe we could rearrange things to accommodate that.
     */
    setlocale(LC_MESSAGES, "");
    bindtextdomain(PACKAGE, LOCALEDIR);
    bind_textdomain_codeset(PACKAGE, "UTF-8");
    textdomain(PACKAGE);
#endif	/* ENABLE_NLS */

    convert_args_to_utf8(pine_state, &args);

    if(args.action == aaFolder){
	pine_state->beginning_of_month = first_run_of_month();
	pine_state->beginning_of_year = first_run_of_year();
    }

    /* Set up optional for user-defined display filtering */
    pine_state->tools.display_filter	     = dfilter;
    pine_state->tools.display_filter_trigger = dfilter_trigger;

#ifdef _WINDOWS
    if(ps_global->install_flag){
	init_install_get_vars();

	if(ps_global->prc)
	  free_pinerc_s(&ps_global->prc);

	exit(0);
    }
#endif

    if(ps_global->convert_sigs){
	if(convert_sigs_to_literal(ps_global, 0) == -1){
	    /* TRANSLATORS: sigs refers to signatures, which the user was trying to convert */
	    fprintf(stderr, _("trouble converting sigs\n"));
	    exit(-1);
	}

	if(ps_global->prc){
	    if(ps_global->prc->outstanding_pinerc_changes)
	      write_pinerc(ps_global, Main, WRP_NONE);

	    free_pinerc_s(&pine_state->prc);
	}

	exit(0);
    }

    /*
     * Set up a c-client read timeout and timeout handler.  In general,
     * it shouldn't happen, but a server crash or dead link can cause
     * pine to appear wedged if we don't set this up...
     */
    rv = 30;
    if(pine_state->VAR_TCPOPENTIMEO)
      (void)SVAR_TCP_OPEN(pine_state, rv, tmp_20k_buf, SIZEOF_20KBUF);
    mail_parameters(NULL, SET_OPENTIMEOUT, (void *)(long)rv);

    rv = 15;
    if(pine_state->VAR_TCPREADWARNTIMEO)
      (void)SVAR_TCP_READWARN(pine_state, rv, tmp_20k_buf, SIZEOF_20KBUF);
    mail_parameters(NULL, SET_READTIMEOUT, (void *)(long)rv);

    rv = 0;
    if(pine_state->VAR_TCPWRITEWARNTIMEO){
	if(!SVAR_TCP_WRITEWARN(pine_state, rv, tmp_20k_buf, SIZEOF_20KBUF))
	  if(rv == 0 || rv > 4)				/* making sure */
	    mail_parameters(NULL, SET_WRITETIMEOUT, (void *)(long)rv);
    }

    mail_parameters(NULL, SET_TIMEOUT, (void *) pine_tcptimeout);

    rv = 15;
    if(pine_state->VAR_RSHOPENTIMEO){
	if(!SVAR_RSH_OPEN(pine_state, rv, tmp_20k_buf, SIZEOF_20KBUF))
	  if(rv == 0 || rv > 4)				/* making sure */
	    mail_parameters(NULL, SET_RSHTIMEOUT, (void *)(long)rv);
    }

    rv = 15;
    if(pine_state->VAR_SSHOPENTIMEO){
	if(!SVAR_SSH_OPEN(pine_state, rv, tmp_20k_buf, SIZEOF_20KBUF))
	  if(rv == 0 || rv > 4)				/* making sure */
	    mail_parameters(NULL, SET_SSHTIMEOUT, (void *)(long)rv);
    }

    rvl = 60L;
    if(pine_state->VAR_MAILDROPCHECK){
	if(!SVAR_MAILDCHK(pine_state, rvl, tmp_20k_buf, SIZEOF_20KBUF)){
	    if(rvl == 0L)
	      rvl = (60L * 60L * 24L * 100L);	/* 100 days */

	    if(rvl >= 60L)			/* making sure */
	      mail_parameters(NULL, SET_SNARFINTERVAL, (void *) rvl);
	}
    }

    /*
     * Lookups of long login names which don't exist are very slow in aix.
     * This would normally get set in system-wide config if not needed.
     */
    if(F_ON(F_DISABLE_SHARED_NAMESPACES, ps_global))
      mail_parameters(NULL, SET_DISABLEAUTOSHAREDNS, (void *) TRUE);

    if(F_ON(F_HIDE_NNTP_PATH, ps_global))
      mail_parameters(NULL, SET_NNTPHIDEPATH, (void *) TRUE);

    if(F_ON(F_MAILDROPS_PRESERVE_STATE, ps_global))
      mail_parameters(NULL, SET_SNARFPRESERVE, (void *) TRUE);

    rvl = 0L;
    if(pine_state->VAR_NNTPRANGE){
	if(!SVAR_NNTPRANGE(pine_state, rvl, tmp_20k_buf, SIZEOF_20KBUF))
	  if(rvl > 0L)
	    mail_parameters(NULL, SET_NNTPRANGE, (void *) rvl);
    }

    /*
     * Tell c-client not to be so aggressive about uid mappings
     */
    mail_parameters(NULL, SET_UIDLOOKAHEAD, (void *) 20);

    /*
     * Setup referral handling
     */
    mail_parameters(NULL, SET_IMAPREFERRAL, (void *) imap_referral);
    mail_parameters(NULL, SET_MAILPROXYCOPY, (void *) imap_proxycopy);

    /*
     * Setup multiple newsrc transition
     */
    mail_parameters(NULL, SET_NEWSRCQUERY, (void *) pine_newsrcquery);

    /*
     * Disable some drivers if requested.
     */
    if(ps_global->VAR_DISABLE_DRIVERS &&
       ps_global->VAR_DISABLE_DRIVERS[0] &&
       ps_global->VAR_DISABLE_DRIVERS[0][0]){
	char **t;

	for(t = ps_global->VAR_DISABLE_DRIVERS; t[0] && t[0][0]; t++)
	  if(mail_parameters(NULL, DISABLE_DRIVER, (void *)(*t))){
	      dprint((2, "Disabled mail driver \"%s\"\n", *t));
	  }
	  else{
	      snprintf(tmp_20k_buf, SIZEOF_20KBUF,
		     _("Failed to disable mail driver \"%s\": name not found"),
		      *t);
	      tmp_20k_buf[SIZEOF_20KBUF-1] = '\0';
	      init_error(ps_global, SM_ORDER | SM_DING, 3, 5, tmp_20k_buf);
	  }
    }

    /*
     * Disable some authenticators if requested.
     */
    if(ps_global->VAR_DISABLE_AUTHS &&
       ps_global->VAR_DISABLE_AUTHS[0] &&
       ps_global->VAR_DISABLE_AUTHS[0][0]){
	char **t;

	for(t = ps_global->VAR_DISABLE_AUTHS; t[0] && t[0][0]; t++)
	  if(mail_parameters(NULL, DISABLE_AUTHENTICATOR, (void *)(*t))){
	      dprint((2,"Disabled SASL authenticator \"%s\"\n", *t));
	  }
	  else{
	      snprintf(tmp_20k_buf, SIZEOF_20KBUF,
	      _("Failed to disable SASL authenticator \"%s\": name not found"),
		      *t);
	      tmp_20k_buf[SIZEOF_20KBUF-1] = '\0';
	      init_error(ps_global, SM_ORDER | SM_DING, 3, 5, tmp_20k_buf);
	  }
    }

    if(ps_global->VAR_ENCRYPTION_RANGE
	&& ps_global->VAR_ENCRYPTION_RANGE[0]){
	char *min_s, *max_s, *s;
	int   min_v, max_v;

	if((s = strchr(ps_global->VAR_ENCRYPTION_RANGE, ',')) == NULL){
	   snprintf(tmp_20k_buf, SIZEOF_20KBUF,
	     _("Bad encryption range: \"%s\": resetting to default"),
	      ps_global->VAR_ENCRYPTION_RANGE);
	   tmp_20k_buf[SIZEOF_20KBUF-1] = '\0';
	   init_error(ps_global, SM_ORDER | SM_DING, 3, 5, tmp_20k_buf);
	   fs_give((void **) &ps_global->VAR_ENCRYPTION_RANGE);
	   ps_global->VAR_ENCRYPTION_RANGE = cpystr(DF_ENCRYPTION_RANGE);
	   s = strchr(ps_global->VAR_ENCRYPTION_RANGE, ','); /* try again */
	}

	if(s == NULL){
	   snprintf(tmp_20k_buf, SIZEOF_20KBUF,
	     _("Bad default encryption range: \"%s\""), 
		ps_global->VAR_ENCRYPTION_RANGE);
	   tmp_20k_buf[SIZEOF_20KBUF-1] = '\0';
	   init_error(ps_global, SM_ORDER | SM_DING, 3, 5, tmp_20k_buf);
	}
	else {
	   *s = ' ';
	   get_pair(ps_global->VAR_ENCRYPTION_RANGE, &min_s, &max_s, 1, 0);
	   *s = ',';

	   min_v = pith_ssl_encryption_version(min_s);
	   max_v = pith_ssl_encryption_version(max_s);

	   if(min_v < 0 || max_v < 0){
	      snprintf(tmp_20k_buf, SIZEOF_20KBUF,
		_("Bad encryption range: \"%s\": resetting to default"),
	           ps_global->VAR_ENCRYPTION_RANGE);
	      tmp_20k_buf[SIZEOF_20KBUF-1] = '\0';
	      init_error(ps_global, SM_ORDER | SM_DING, 3, 5, tmp_20k_buf);
	      min_v = max_v = 0;
	   }

	   if(min_v > max_v){
	      int bubble;
	      snprintf(tmp_20k_buf, SIZEOF_20KBUF,
		_("Minimum encryption protocol (%s) bigger than maximum value (%s). Reversing..."),
		   min_s, max_s);
	      tmp_20k_buf[SIZEOF_20KBUF-1] = '\0';
	      init_error(ps_global, SM_ORDER | SM_DING, 3, 5, tmp_20k_buf);
	      bubble = min_v;
	      min_v = max_v;
	      max_v = bubble;
	   }

	   if(max_v > 0 && max_v < (long) pith_ssl_encryption_version("tls1")){
	      strncpy(tmp_20k_buf, _("Security alert: SSL maximum encryption version was set to SSLv3."), SIZEOF_20KBUF);
	      tmp_20k_buf[SIZEOF_20KBUF-1] = '\0';
	      init_error(ps_global, SM_ORDER | SM_DING, 3, 5, tmp_20k_buf);
	   } 

	   mail_parameters(NULL, SET_ENCRYPTION_RANGE_MIN, (void *) &min_v);
           mail_parameters(NULL, SET_ENCRYPTION_RANGE_MAX, (void *) &max_v);
	}
    }


    /*
     * setup alternative authentication driver preference for IMAP opens
     */
    if(F_ON(F_PREFER_ALT_AUTH, ps_global))
      mail_parameters(NULL, SET_IMAPTRYALT, (void *) TRUE);

    /*
     * Install handler to let us know about potential delays
     */
    (void) mail_parameters(NULL, SET_BLOCKNOTIFY, (void *) pine_block_notify);

    if(ps_global->dump_supported_options){
	dump_supported_options();
	exit(0);
    }

    /*
     * Install extra headers to fetch along with all the other stuff
     * mail_fetch_structure and mail_fetch_overview requests.
     */
    calc_extra_hdrs();
    if(get_extra_hdrs())
      (void) mail_parameters(NULL, SET_IMAPEXTRAHEADERS,
			     (void *) get_extra_hdrs());

    if(init_username(pine_state) < 0){
        fprintf(stderr, _("Who are you? (Unable to look up login name)\n"));
	exit(-1);
    }

    if(init_userdir(pine_state) < 0)
      exit(-1);

    if(init_hostname(pine_state) < 0)
      exit(-1);
    
    /*
     * Verify mail dir if we're not in send only mode...
     */
    if(args.action == aaFolder && init_mail_dir(pine_state) < 0)
      exit(-1);

    init_signals();

    /*--- input side ---*/
    if(init_tty_driver(pine_state)){
#ifndef _WINDOWS	/* always succeeds under _WINDOWS */
        fprintf(stderr, _("Can't access terminal or input is not a terminal. Redirection of\nstandard input is not allowed. For example \"pine < file\" doesn't work.\n%c"), BELL);
        exit(-1);
#endif /* !_WINDOWS */
    }
        

    /*--- output side ---*/
    rv = config_screen(&(pine_state->ttyo));
#ifndef _WINDOWS	/* always succeeds under _WINDOWS */
    if(rv){
        switch(rv){
          case -1:
	    printf(_("Terminal type (environment variable TERM) not set.\n"));
            break;
          case -2:
	    printf(_("Terminal type \"%s\" is unknown.\n"), getenv("TERM"));
            break;
          case -3:
            printf(_("Can't open terminal capabilities database.\n"));
            break;
          case -4:
            printf(_("Your terminal, of type \"%s\", is lacking functions needed to run alpine.\n"), getenv("TERM"));
            break;
        }

        printf("\r");
        end_tty_driver(pine_state);
        exit(-1);
    }
#endif /* !_WINDOWS */

    if(F_ON(F_BLANK_KEYMENU,ps_global))
      FOOTER_ROWS(ps_global) = 1;

    init_screen();
    init_keyboard(pine_state->orig_use_fkeys);
    strncpy(pine_state->inbox_name, INBOX_NAME,
	    sizeof(pine_state->inbox_name)-1);
    init_folders(pine_state);		/* digest folder spec's */

    pine_state->in_init_seq = 0;	/* so output (& ClearScreen) show up */
    pine_state->dont_use_init_cmds = 1;	/* don't use up initial_commands yet */
    ClearScreen();

    /* initialize titlebar in case we use it */
    set_titlebar("", NULL, NULL, NULL, NULL, 0, FolderName, 0, 0, NULL);

    /*
     * Prep storage object driver for PicoText 
     */
    so_register_external_driver(pine_pico_get, pine_pico_give, pine_pico_writec, pine_pico_readc, 
				pine_pico_puts, pine_pico_seek, NULL, NULL);

#ifdef	DEBUG
    if(ps_global->debug_imap > 4 || debug > 9 || ps_global->debug_http > 0){
	q_status_message(SM_ORDER | SM_DING, 5, 9,
	      _("Warning: sensitive authentication data included in debug file"));
	flush_status_messages(0);
    }
#endif

    if(args.action == aaPrcCopy || args.action == aaAbookCopy){
	int   exit_val = -1;
	char *err_msg = NULL;

	/*
	 * Don't translate these into UTF-8 because we'll be using them
	 * before we translate next time. User should use ascii.
	 */
	if(args.data.copy.local && args.data.copy.remote){
	    switch(args.action){
	      case aaPrcCopy:
		exit_val = copy_pinerc(args.data.copy.local,
				       args.data.copy.remote, &err_msg);
		break;

	      case aaAbookCopy:
		exit_val = copy_abook(args.data.copy.local,
				      args.data.copy.remote, &err_msg);
		break;

	      default:
		break;
	    }
	}
	if(err_msg){
	  q_status_message(SM_ORDER | SM_DING, 3, 4, err_msg);
	  fs_give((void **)&err_msg);
	}
	goodnight_gracey(pine_state, exit_val);
    }

    if(args.action == aaFolder
       && (pine_state->first_time_user || pine_state->show_new_version)){
	pine_state->mangled_header = 1;
	show_main_screen(pine_state, 0, FirstMenu, &main_keymenu, 0,
			 (Pos *) NULL);
	new_user_or_version(pine_state);
	ClearScreen();
    }
    
    /* put back in case we need to suppress output */
    pine_state->in_init_seq = pine_state->save_in_init_seq;

    /* queue any init errors so they get displayed in a screen below */
    queue_init_errors(ps_global);

    /* "Page" the given file? */
    if(args.action == aaMore){
	int dice = 1, redir = 0;

	if(pine_state->in_init_seq){
	    pine_state->in_init_seq = pine_state->save_in_init_seq = 0;
	    clear_cursor_pos();
	    if(pine_state->free_initial_cmds)
	      fs_give((void **)&(pine_state->free_initial_cmds));

	    pine_state->initial_cmds = NULL;
	}

	/*======= Requested that we simply page the given file =======*/
	if(args.data.file){		/* Open the requested file... */
	    SourceType  src;
	    STORE_S    *store = NULL;
	    char       *decode_error = NULL;
	    char       filename[MAILTMPLEN];

	    if(args.data.file[0] == '\0'){
		HelpType help = NO_HELP;

		pine_state->mangled_footer = 1;
		filename[0] = '\0';
    		while(1){
		    int flags = OE_APPEND_CURRENT;

        	    rv = optionally_enter(filename, -FOOTER_ROWS(pine_state),
					  0, sizeof(filename),
					  /* TRANSLATORS: file is computer data */
					  _("File to open : "),
					  NULL, help, &flags);
        	    if(rv == 3){
			help = (help == NO_HELP) ? h_no_F_arg : NO_HELP;
			continue;
		    }

        	    if(rv != 4)
		      break;
    		}

    		if(rv == 1){
		    q_status_message(SM_ORDER, 0, 2, _("Cancelled"));
		    goodnight_gracey(pine_state, -1);
		} 

		if(*filename){
		    removing_trailing_white_space(filename);
		    removing_leading_white_space(filename);
		    if(is_absolute_path(filename))
		      fnexpand(filename, sizeof(filename));

		    args.data.file = filename;
    		}

		if(!*filename){
					  /* TRANSLATORS: file is computer data */
		    q_status_message(SM_ORDER, 0, 2 ,_("No file to open"));
		    goodnight_gracey(pine_state, -1);
		} 
	    }

	    if(stdin_getc){
		redir++;
		src = CharStar;
		if(isatty(0) && (store = so_get(src, NULL, EDIT_ACCESS))){
		    gf_io_t pc;

		    gf_set_so_writec(&pc, store);
		    gf_filter_init();
		    if((decode_error = gf_pipe(stdin_getc, pc)) != NULL){
			dice = 0;
			q_status_message1(SM_ORDER, 3, 4,
					  _("Problem reading standard input: %s"),
					  decode_error);
		    }

		    gf_clear_so_writec(store);
		}
		else
		  dice = 0;
	    }
	    else{
		src = FileStar;
		strncpy(ps_global->cur_folder, args.data.file,
			sizeof(ps_global->cur_folder)-1);
		ps_global->cur_folder[sizeof(ps_global->cur_folder)-1] = '\0';
		if(!(store = so_get(src, args.data.file, READ_ACCESS|READ_FROM_LOCALE)))
		  dice = 0;
	    }

	    if(dice){
		SCROLL_S sargs;

		memset(&sargs, 0, sizeof(SCROLL_S));
		sargs.text.text = so_text(store);
		sargs.text.src  = src;
		/* TRANSLATORS: file is computer file being read by user */
		sargs.text.desc = _("file");
		/* TRANSLATORS: this is in the title bar at top of screen */
		sargs.bar.title = _("FILE VIEW");
		sargs.bar.style = FileTextPercent;
		sargs.keys.menu = &simple_file_keymenu;
		setbitmap(sargs.keys.bitmap);
		scrolltool(&sargs);

		printf("\n\n");
		so_give(&store);
	    }
	}

	if(!dice){
	    q_status_message2(SM_ORDER, 3, 4,
		_("Can't display \"%s\": %s"),
		 (redir) ? _("Standard Input") 
			 : args.data.file ? args.data.file : "NULL",
		 error_description(errno));
	}

	goodnight_gracey(pine_state, 0);
    }
    else if(args.action == aaMail || (stdin_getc && (args.action != aaURL))){
        /*======= address on command line/send one message mode ============*/
        char	   *to = NULL, *error = NULL, *addr = NULL;
        int	    len, good_addr = 1;
	int	    exit_val = 0;
	BUILDER_ARG fcc;

	if(pine_state->in_init_seq){
	    pine_state->in_init_seq = pine_state->save_in_init_seq = 0;
	    clear_cursor_pos();
	    if(pine_state->free_initial_cmds)
	      fs_give((void **) &(pine_state->free_initial_cmds));

	    pine_state->initial_cmds = NULL;
	}

        /*----- Format the To: line with commas for the composer ---*/
	if(args.data.mail.addrlist){
	    STRLIST_S *p;

	    for(p = args.data.mail.addrlist, len = 0; p; p = p->next)
	      len += strlen(p->name) + 2;

	    to = (char *) fs_get((len + 5) * sizeof(char));
	    for(p = args.data.mail.addrlist, *to = '\0'; p; p = p->next){
		if(*to){
		    strncat(to, ", ", len+5-strlen(to)-1);
		    to[len+5-1] = '\0';
		}

		strncat(to, p->name, len+5-strlen(to)-1);
		to[len+5-1] = '\0';
	    }

	    memset((void *)&fcc, 0, sizeof(BUILDER_ARG));
	    dprint((2, "building addr: -->%s<--\n", to ? to : "?"));
	    good_addr = (build_address(to, &addr, &error, &fcc, NULL) >= 0);
	    dprint((2, "mailing to: -->%s<--\n", addr ? addr : "?"));
	    free_strlist(&args.data.mail.addrlist);
	}
	else
	  memset(&fcc, 0, sizeof(fcc));

	if(good_addr){
	    compose_mail(addr, fcc.tptr, NULL,
			 args.data.mail.attachlist, stdin_getc);
	}
	else{
	    /* TRANSLATORS: refers to bad email address */
	    q_status_message1(SM_ORDER, 3, 4, _("Bad address: %s"), error);
	    exit_val = -1;
	}

	if(addr)
	  fs_give((void **) &addr);

	if(fcc.tptr)
	  fs_give((void **) &fcc.tptr);

	if(args.data.mail.attachlist)
	  free_attachment_list(&args.data.mail.attachlist);

	if(to)
	  fs_give((void **) &to);

	if(error)
	  fs_give((void **) &error);

	goodnight_gracey(pine_state, exit_val);
    }
    else{
	char             int_mail[MAXPATH+1];
        struct key_menu *km = &main_keymenu;

        /*========== Normal pine mail reading mode ==========*/
            
        pine_state->mail_stream    = NULL;
        pine_state->mangled_screen = 1;

	if(args.action == aaURL){
	    url_tool_t f;

	    if(pine_state->in_init_seq){
		pine_state->in_init_seq = pine_state->save_in_init_seq = 0;
		clear_cursor_pos();
		if(pine_state->free_initial_cmds)
		  fs_give((void **) &(pine_state->free_initial_cmds));
		pine_state->initial_cmds = NULL;
	    }
	    if((f = url_local_handler(args.url)) != NULL){
		if(args.data.mail.attachlist){
		    if(f == url_local_mailto){
			if(!(url_local_mailto_and_atts(args.url,
					args.data.mail.attachlist)
			     && pine_state->next_screen))
			  free_attachment_list(&args.data.mail.attachlist);
			goodnight_gracey(pine_state, 0);
		    }
		    else {
			q_status_message(SM_ORDER | SM_DING, 3, 4,
			 _("Only mailto URLs are allowed with file attachments"));
			goodnight_gracey(pine_state, -1);	/* no return */
		    }
		}
		else if(!((*f)(args.url) && pine_state->next_screen))
		  goodnight_gracey(pine_state, 0);	/* no return */
	    }
	    else{
		q_status_message1(SM_ORDER | SM_DING, 3, 4,
				  _("Unrecognized URL \"%s\""), args.url);
		goodnight_gracey(pine_state, -1);	/* no return */
	    }
	}
	else if(!pine_state->start_in_index){
	    /* flash message about executing initial commands */
	    if(pine_state->in_init_seq){
	        pine_state->in_init_seq    = 0;
		clear_cursor_pos();
		pine_state->mangled_header = 1;
		pine_state->mangled_footer = 1;
		pine_state->mangled_screen = 0;
		/* show that this is Alpine */
		show_main_screen(pine_state, 0, FirstMenu, km, 0, (Pos *)NULL);
		pine_state->mangled_screen = 1;
		pine_state->painted_footer_on_startup = 1;
		if(MIN(4, pine_state->ttyo->screen_rows - 4) > 1){
		  char buf1[6*MAX_SCREEN_COLS+1];
		  char buf2[6*MAX_SCREEN_COLS+1];
		  int  wid;

		  /* TRANSLATORS: Initial Keystroke List is the literal name of an option */
		  strncpy(buf1, _("Executing Initial Keystroke List......"), sizeof(buf1));
		  buf1[sizeof(buf1)-1] = '\0';
		  wid = utf8_width(buf1);
		  if(wid > ps_global->ttyo->screen_cols){
		    utf8_pad_to_width(buf2, buf1, sizeof(buf2), ps_global->ttyo->screen_cols, 1);
	            PutLine0(MIN(4, pine_state->ttyo->screen_rows - 4), 0, buf2);
		  }
		  else{
	            PutLine0(MIN(4, pine_state->ttyo->screen_rows - 4),
		      MAX(MIN(11, pine_state->ttyo->screen_cols - wid), 0), buf1);
		  }
		}

	        pine_state->in_init_seq = 1;
	    }
	    else{
                show_main_screen(pine_state, 0, FirstMenu, km, 0, (Pos *)NULL);
		pine_state->painted_body_on_startup   = 1;
		pine_state->painted_footer_on_startup = 1;
	    }
        }
	else{
	    /* cancel any initial commands, overridden by cmd line */
	    if(pine_state->in_init_seq){
		pine_state->in_init_seq      = 0;
		pine_state->save_in_init_seq = 0;
		clear_cursor_pos();
		if(pine_state->initial_cmds){
		    if(pine_state->free_initial_cmds)
		      fs_give((void **)&(pine_state->free_initial_cmds));

		    pine_state->initial_cmds = NULL;
		}

		F_SET(F_USE_FK,pine_state, pine_state->orig_use_fkeys);
	    }

            (void) do_index_border(pine_state->context_current,
				   pine_state->cur_folder,
				   pine_state->mail_stream,
				   pine_state->msgmap, MsgIndex, NULL,
				   INDX_CLEAR|INDX_HEADER|INDX_FOOTER);
	    pine_state->painted_footer_on_startup = 1;
	    if(MIN(4, pine_state->ttyo->screen_rows - 4) > 1){
	      char buf1[6*MAX_SCREEN_COLS+1];
	      char buf2[6*MAX_SCREEN_COLS+1];
	      int  wid;

	      strncpy(buf1, _("Please wait, opening mail folder......"), sizeof(buf1));
	      buf1[sizeof(buf1)-1] = '\0';
	      wid = utf8_width(buf1);
	      if(wid > ps_global->ttyo->screen_cols){
		utf8_pad_to_width(buf2, buf1, sizeof(buf2), ps_global->ttyo->screen_cols, 1);
		PutLine0(MIN(4, pine_state->ttyo->screen_rows - 4), 0, buf2);
	      }
	      else{
		PutLine0(MIN(4, pine_state->ttyo->screen_rows - 4),
		  MAX(MIN(11, pine_state->ttyo->screen_cols - wid), 0), buf1);
	      }
	    }
        }

        fflush(stdout);

#if !defined(_WINDOWS) && !defined(LEAVEOUTFIFO)
	if(ps_global->VAR_FIFOPATH && ps_global->VAR_FIFOPATH[0])
	  init_newmailfifo(ps_global->VAR_FIFOPATH);
#endif

	if(pine_state->in_init_seq){
	    pine_state->in_init_seq = 0;
	    clear_cursor_pos();
	}

        if(args.action == aaFolder && args.data.folder){
	    CONTEXT_S *cntxt = NULL, *tc = NULL;
	    char       foldername[MAILTMPLEN];
	    int        notrealinbox = 0;

	    if(args.data.folder[0] == '\0'){
		char *fldr;
		unsigned save_def_goto_rule;

		foldername[0] = '\0';
		save_def_goto_rule = pine_state->goto_default_rule;
		pine_state->goto_default_rule = GOTO_FIRST_CLCTN;
		tc = default_save_context(pine_state->context_list);
		fldr = broach_folder(-FOOTER_ROWS(pine_state), 1, &notrealinbox, &tc);
		pine_state->goto_default_rule = save_def_goto_rule;
		if(fldr){
		    strncpy(foldername, fldr, sizeof(foldername)-1);
		    foldername[sizeof(foldername)-1] = '\0';
		}

		if(*foldername){
		    removing_trailing_white_space(foldername);
		    removing_leading_white_space(foldername);
		    args.data.folder = cpystr(foldername);
    		}

		if(!*foldername){
		    q_status_message(SM_ORDER, 0, 2 ,_("No folder to open"));
		    goodnight_gracey(pine_state, -1);
		} 
	    }

	    if(tc)
	      cntxt = tc;
	    else if((rv = pine_state->init_context) < 0)
	      /*
	       * As with almost all the folder vars in the pinerc,
	       * we subvert the collection "breakout" here if the
	       * folder name given looks like an absolute path on
	       * this system...
	       */
	      cntxt = (is_absolute_path(args.data.folder))
			? NULL : pine_state->context_current;
	    else if(rv == 0)
	      cntxt = NULL;
	    else
	      for(cntxt = pine_state->context_list;
		  rv > 1 && cntxt->next;
		  rv--, cntxt = cntxt->next)
		;

	    if(pine_state && pine_state->ttyo){
		blank_keymenu(pine_state->ttyo->screen_rows - 2, 0);
		pine_state->painted_footer_on_startup = 0;
		pine_state->mangled_footer = 1;
	    }

	    if(args.data.folder && *args.data.folder 
		&& !strucmp(args.data.folder, ps_global->inbox_name)
		&& cntxt != ps_global->context_list)
		notrealinbox = 1;

            if(do_broach_folder(args.data.folder, cntxt, NULL, notrealinbox ? 0L : DB_INBOXWOCNTXT) <= 0){
		q_status_message1(SM_ORDER, 3, 4,
		    _("Unable to open folder \"%s\""), args.data.folder);

		fs_give((void **) &args.data.folder);

		goodnight_gracey(pine_state, -1);
	    }
	}
	else if(args.action == aaFolder){
#ifdef _WINDOWS
            /*
	     * need to ask for the inbox name if no default under DOS
	     * since there is no "inbox"
	     */

	    if(!pine_state->VAR_INBOX_PATH || !pine_state->VAR_INBOX_PATH[0]
	       || strucmp(pine_state->VAR_INBOX_PATH, "inbox") == 0){
		HelpType help = NO_HELP;
		static   ESCKEY_S ekey[] = {{ctrl(T), 2, "^T", "To Fldrs"},
					  {-1, 0, NULL, NULL}};

		pine_state->mangled_footer = 1;
		int_mail[0] = '\0';
    		while(1){
		    int flags = OE_APPEND_CURRENT;

        	    rv = optionally_enter(int_mail, -FOOTER_ROWS(pine_state),
				      0, sizeof(int_mail),
				      _("No inbox!  Folder to open as inbox : "),
				      /* ekey */ NULL, help, &flags);
        	    if(rv == 3){
			help = (help == NO_HELP) ? h_sticky_inbox : NO_HELP;
			continue;
		    }

        	    if(rv != 4)
		      break;
    		}

    		if(rv == 1){
		    q_status_message(SM_ORDER, 0, 2 ,_("Folder open cancelled"));
		    rv = 0;		/* reset rv */
		} 
		else if(rv == 2){
                    show_main_screen(pine_state,0,FirstMenu,km,0,(Pos *)NULL);
		}

		if(*int_mail){
		    removing_trailing_white_space(int_mail);
		    removing_leading_white_space(int_mail);
		    if((!pine_state->VAR_INBOX_PATH 
			|| strucmp(pine_state->VAR_INBOX_PATH, "inbox") == 0)
		     /* TRANSLATORS: Inbox-Path and PINERC are literal, not to be translated */
		     && want_to(_("Preserve folder as \"Inbox-Path\" in PINERC"), 
				'y', 'n', NO_HELP, WT_NORM) == 'y'){
			set_variable(V_INBOX_PATH, int_mail, 1, 1, Main);
		    }
		    else{
			if(pine_state->VAR_INBOX_PATH)
			  fs_give((void **)&pine_state->VAR_INBOX_PATH);

			pine_state->VAR_INBOX_PATH = cpystr(int_mail);
		    }

		    if(pine_state && pine_state->ttyo){
			blank_keymenu(pine_state->ttyo->screen_rows - 2, 0);
			pine_state->painted_footer_on_startup = 0;
			pine_state->mangled_footer = 1;
		    }

		    do_broach_folder(pine_state->inbox_name, 
				     pine_state->context_list, NULL, DB_INBOXWOCNTXT);
    		}
		else
		  q_status_message(SM_ORDER, 0, 2 ,_("No folder opened"));

	    }
	    else

#endif /* _WINDOWS */
	    if(F_ON(F_PREOPEN_STAYOPENS, ps_global))
	      preopen_stayopen_folders();

	    if(pine_state && pine_state->ttyo){
		blank_keymenu(pine_state->ttyo->screen_rows - 2, 0);
		pine_state->painted_footer_on_startup = 0;
		pine_state->mangled_footer = 1;
	    }

	    /* open inbox */
            do_broach_folder(pine_state->inbox_name,
			     pine_state->context_list, NULL, DB_INBOXWOCNTXT);
        }

        if(pine_state->mangled_footer)
	  pine_state->painted_footer_on_startup = 0;

        if(args.action == aaFolder
	   && pine_state->mail_stream
	   && expire_sent_mail())
	  pine_state->painted_footer_on_startup = 0;

	/*
	 * Initialize the defaults.  Initializing here means that
	 * if they're remote, the user isn't prompted for an imap login
	 * before the display's drawn, AND there's the chance that
	 * we can climb onto the already opened folder's stream...
	 */
	if(ps_global->first_time_user)
	  init_save_defaults();	/* initialize default save folders */

	build_path(int_mail,
		   ps_global->VAR_OPER_DIR ? ps_global->VAR_OPER_DIR
					   : pine_state->home_dir,
		   INTERRUPTED_MAIL, sizeof(int_mail));
	if(args.action == aaFolder
	   && (folder_exists(NULL, int_mail) & FEX_ISFILE))
	  q_status_message(SM_ORDER | SM_DING, 4, 5, 
		       _("Use Compose command to continue interrupted message."));

	if(args.action == aaFolder && args.data.folder)
	  fs_give((void **) &args.data.folder);

#if defined(USE_QUOTAS)
	{
	    long q;
	    int  over;
	    q = disk_quota(pine_state->home_dir, &over);
	    if(q > 0 && over){
		q_status_message2(SM_ASYNC | SM_DING, 4, 5,
		      _("WARNING! Over your disk quota by %s bytes (%s)"),
			      comatose(q),byte_string(q));
	    }
	}
#endif

	pine_state->in_init_seq = pine_state->save_in_init_seq;
	pine_state->dont_use_init_cmds = 0;
	clear_cursor_pos();

	if(pine_state->give_fixed_warning)
	  q_status_message(SM_ASYNC, 0, 10,
/* TRANSLATORS: config is an abbreviation for configuration */
_("Note: some of your config options conflict with site policy and are ignored"));

	if(!prune_folders_ok())
	  q_status_message(SM_ASYNC, 0, 10, 
			   /* TRANSLATORS: Pruned-Folders is literal */
			   _("Note: ignoring Pruned-Folders outside of default collection for saves"));
	
	if(get_input_timeout() == 0 &&
	   ps_global->VAR_INBOX_PATH &&
	   ps_global->VAR_INBOX_PATH[0] == '{')
	  q_status_message(SM_ASYNC, 0, 10,
_("Note: Mail-Check-Interval=0 may cause IMAP server connection to time out"));

#ifdef _WINDOWS
	mswin_setnewmailwidth(ps_global->nmw_width);
#endif


        /*-------------------------------------------------------------------
                         Loop executing the commands
    
            This is done like this so that one command screen can cause
            another one to execute it with out going through the main menu. 
          ------------------------------------------------------------------*/
	if(!pine_state->next_screen)
	  pine_state->next_screen = pine_state->start_in_index
				      ? mail_index_screen : main_menu_screen;
        while(1){
            if(pine_state->next_screen == SCREEN_FUN_NULL) 
              pine_state->next_screen = main_menu_screen;

            (*(pine_state->next_screen))(pine_state);
        }
    }

    exit(0);
}


/*
 * The arguments need to be converted to UTF-8 for our internal use.
 * Not all arguments are converted because some are used before we
 * are able to do the conversion, like the pinerc name.
 */
void
convert_args_to_utf8(struct pine *ps, ARGDATA_S *args)
{
    char  *fromcharset = NULL;
    char  *conv;

    if(args){
	if(ps->keyboard_charmap && strucmp(ps->keyboard_charmap, "UTF-8")
	   &&  strucmp(ps->keyboard_charmap, "US-ASCII"))
	  fromcharset = ps->keyboard_charmap;
	else if(ps->display_charmap && strucmp(ps->display_charmap, "UTF-8")
	   &&  strucmp(ps->display_charmap, "US-ASCII"))
	  fromcharset = ps->display_charmap;
#ifndef	_WINDOWS
	else if(ps->VAR_OLD_CHAR_SET && strucmp(ps->VAR_OLD_CHAR_SET, "UTF-8")
	   &&  strucmp(ps->VAR_OLD_CHAR_SET, "US-ASCII"))
	  fromcharset = ps->VAR_OLD_CHAR_SET;
#endif	/* ! _WINDOWS */

	if(args->action == aaURL && args->url){
	    conv = convert_to_utf8(args->url, fromcharset, 0);
	    if(conv){
		fs_give((void **) &args->url);
		args->url = conv;
	    }
	}

	if(args->action == aaFolder && args->data.folder){
	    conv = convert_to_utf8(args->data.folder, fromcharset, 0);
	    if(conv){
		fs_give((void **) &args->data.folder);
		args->data.folder = conv;
	    }
	}

	if(args->action == aaMore && args->data.file){
	    conv = convert_to_utf8(args->data.file, fromcharset, 0);
	    if(conv){
		fs_give((void **) &args->data.file);
		args->data.file = conv;
	    }
	}

	if(args->action == aaURL || args->action == aaMail){
	    if(args->data.mail.addrlist){
		STRLIST_S *p;

		for(p = args->data.mail.addrlist; p; p=p->next){
		    if(p->name){
			conv = convert_to_utf8(p->name, fromcharset, 0);
			if(conv){
			    fs_give((void **) &p->name);
			    p->name = conv;
			}
		    }
		}
	    }

	    if(args->data.mail.attachlist){
		PATMT *p;

		for(p = args->data.mail.attachlist; p; p=p->next){
		    if(p->filename){
			conv = convert_to_utf8(p->filename, fromcharset, 0);
			if(conv){
			    fs_give((void **) &p->filename);
			    p->filename = conv;
			}
		    }
		}
	    }
	}
    }
}


void
preopen_stayopen_folders(void)
{
    char  **open_these;

    for(open_these = ps_global->VAR_PERMLOCKED;
	open_these && *open_these; open_these++)
      (void) do_broach_folder(*open_these, ps_global->context_list,
			      NULL, DB_NOVISIT);
}


/*
 * read_stdin_char - simple function to return a character from
 *		     redirected stdin
 */
int
read_stdin_char(char *c)
{
    int rv;
    
    /* it'd probably be a good idea to fix this to pre-read blocks */
    while(1){
	rv = read(PIPED_FD, c, 1);
	if(rv < 0){
	    if(errno == EINTR){
		dprint((2, "read_stdin_char: read interrupted, restarting\n"));
		continue;
	    }
	    else
	      dprint((1, "read_stdin_char: read FAILED: %s\n",
			 error_description(errno)));
	}
	break;
    }
    return(rv);
}


/* this default is from the array of structs below */
#define DEFAULT_MENU_ITEM ((unsigned) 3)	/* LIST FOLDERS */
#define ABOOK_MENU_ITEM ((unsigned) 4)		/* ADDRESS BOOK */
#define MAX_MENU_ITEM ((unsigned) 6)
/*
 * Skip this many spaces between rows of main menu screen.
 * We have    MAX_MENU_ITEM+1 = # of commands in menu
 *            1               = copyright line
 *            MAX_MENU_ITEM   = rows between commands
 *            1               = extra row above commands
 *            1               = row between commands and copyright
 *
 * To make it simple, if there is enough room for all of that include all the
 * extra space, if not, cut it all out.
 */
#define MNSKIP(X) (((HEADER_ROWS(X)+FOOTER_ROWS(X)+(MAX_MENU_ITEM+1)+1+MAX_MENU_ITEM+1+1) <= (X)->ttyo->screen_rows) ? 1 : 0)

static unsigned menu_index = DEFAULT_MENU_ITEM;

/*
 * One of these for each line that gets printed in the middle of the
 * screen in the main menu.
 */
static struct menu_key {
    char         *key_and_name,
		 *news_addition;
    int		  key_index;	  /* index into keymenu array for this cmd */
} mkeys[] = {
    /*
     * TRANSLATORS: These next few are headings on the Main alpine menu.
     * It's nice if the dashes can be made to line up vertically.
     */
    {N_(" %s     HELP               -  Get help using Alpine"),
     NULL, MAIN_HELP_KEY},
    {N_(" %s     COMPOSE MESSAGE    -  Compose and send%s a message"),
     /* TRANSLATORS: We think of sending an email message or posting a news message.
        The message is shown as Compose and send/post a message */
     N_("/post"), MAIN_COMPOSE_KEY},
    {N_(" %s     MESSAGE INDEX      -  View messages in current folder"),
     NULL, MAIN_INDEX_KEY},
    {N_(" %s     FOLDER LIST        -  Select a folder%s to view"),
     /* TRANSLATORS: When news is supported the message above becomes
        Select a folder OR news group to view */
     N_(" OR news group"), MAIN_FOLDER_KEY},
    {N_(" %s     ADDRESS BOOK       -  Update address book"),
     NULL, MAIN_ADDRESS_KEY},
    {N_(" %s     SETUP              -  Configure Alpine Options"),
     NULL, MAIN_SETUP_KEY},
    /* TRANSLATORS: final Main menu line */
    {N_(" %s     QUIT               -  Leave the Alpine program"),
     NULL, MAIN_QUIT_KEY}
};



/*----------------------------------------------------------------------
      display main menu and execute main menu commands

    Args: The usual pine structure

  Result: main menu commands are executed


              M A I N   M E N U    S C R E E N

   Paint the main menu on the screen, get the commands and either execute
the function or pass back the name of the function to execute for the menu
selection. Only simple functions that always return here can be executed
here.

This functions handling of new mail, redrawing, errors and such can 
serve as a template for the other screen that do much the same thing.

There is a loop that fetches and executes commands until a command to leave
this screen is given. Then the name of the next screen to display is
stored in next_screen member of the structure and this function is exited
with a return.

First a check for new mail is performed. This might involve reading the new
mail into the inbox which might then cause the screen to be repainted.

Then the general screen painting is done. This is usually controlled
by a few flags and some other position variables. If they change they
tell this part of the code what to repaint. This will include cursor
motion and so on.
  ----*/
void
main_menu_screen(struct pine *pine_state)
{
    UCS             ch;
    int		    cmd, just_a_navigate_cmd, setup_command, km_popped;
    int             notrealinbox;
    char            *new_folder, *utf8str;
    CONTEXT_S       *tc;
    struct key_menu *km;
    OtherMenu        what;
    Pos              curs_pos;

    ps_global                 = pine_state;
    just_a_navigate_cmd       = 0;
    km_popped		      = 0;
    menu_index = DEFAULT_MENU_ITEM;
    what                      = FirstMenu;  /* which keymenu to display */
    ch                        = 'x'; /* For display_message 1st time through */
    pine_state->next_screen   = SCREEN_FUN_NULL;
    pine_state->prev_screen   = main_menu_screen;
    curs_pos.row = pine_state->ttyo->screen_rows-FOOTER_ROWS(pine_state);
    curs_pos.col = 0;
    km		 = &main_keymenu;

    mailcap_free(); /* free resources we won't be using for a while */

    if(!pine_state->painted_body_on_startup 
       && !pine_state->painted_footer_on_startup){
	pine_state->mangled_screen = 1;
    }

    dprint((1, "\n\n    ---- MAIN_MENU_SCREEN ----\n"));

    while(1){
	if(km_popped){
	    km_popped--;
	    if(km_popped == 0){
		clearfooter(pine_state);
		pine_state->mangled_body = 1;
	    }
	}

	/*
	 * fix up redrawer just in case some submenu caused it to get
	 * reassigned...
	 */
	pine_state->redrawer = main_redrawer;

	/*----------- Check for new mail -----------*/
        if(new_mail(0, NM_TIMING(ch), NM_STATUS_MSG | NM_DEFER_SORT) >= 0)
          pine_state->mangled_header = 1;

        if(streams_died())
          pine_state->mangled_header = 1;

        show_main_screen(pine_state, just_a_navigate_cmd, what, km,
			 km_popped, &curs_pos);
        just_a_navigate_cmd = 0;
	what = SameMenu;

	/*---- This displays new mail notification, or errors ---*/
	if(km_popped){
	    FOOTER_ROWS(pine_state) = 3;
	    mark_status_dirty();
	}

        display_message(ch);
	if(km_popped){
	    FOOTER_ROWS(pine_state) = 1;
	    mark_status_dirty();
	}

	if(F_OFF(F_SHOW_CURSOR, ps_global)){
	    curs_pos.row =pine_state->ttyo->screen_rows-FOOTER_ROWS(pine_state);
	    curs_pos.col =0;
	}

        MoveCursor(curs_pos.row, curs_pos.col);

        /*------ Read the command from the keyboard ----*/      
#ifdef	MOUSE
	mouse_in_content(KEY_MOUSE, -1, -1, 0, 0);
	register_mfunc(mouse_in_content, HEADER_ROWS(pine_state), 0,
		    pine_state->ttyo->screen_rows-(FOOTER_ROWS(pine_state)+1),
		       pine_state->ttyo->screen_cols);
#endif
#if defined(DOS) || defined(OS2)
	/*
	 * AND pre-build header lines.  This works just fine under
	 * DOS since we wait for characters in a loop. Something will
         * will have to change under UNIX if we want to do the same.
	 */
	/* while_waiting = build_header_cache; */
#ifdef	_WINDOWS
	mswin_sethelptextcallback(pcpine_help_main);
	mswin_mousetrackcallback(pcpine_main_cursor);
#endif
#endif
	ch = READ_COMMAND(&utf8str);
#ifdef	MOUSE
	clear_mfunc(mouse_in_content);
#endif
#if defined(DOS) || defined(OS2)
/*	while_waiting = NULL; */
#ifdef	_WINDOWS
	mswin_sethelptextcallback(NULL);
	mswin_mousetrackcallback(NULL);
#endif
#endif

	/* No matter what, Quit here always works */
	if(ch == 'q' || ch == 'Q'){
	    cmd = MC_QUIT;
	}
#ifdef	DEBUG
	else if(debug && ch && ch < 0x80 && strchr("123456789", ch)){
	    int olddebug;

	    olddebug = debug;
	    debug = ch - '0';
	    if(debug > 7)
	      ps_global->debug_timestamp = 1;
	    else
	      ps_global->debug_timestamp = 0;

	    if(debug > 7)
	      ps_global->debug_imap = 4;
	    else if(debug > 6)
	      ps_global->debug_imap = 3;
	    else if(debug > 4)
	      ps_global->debug_imap = 2;
	    else if(debug > 2)
	      ps_global->debug_imap = 1;
	    else
	      ps_global->debug_imap = 0;

	    if(ps_global->mail_stream){
		if(ps_global->debug_imap > 0){
		    mail_debug(ps_global->mail_stream);
#ifdef	_WINDOWS
		    mswin_enableimaptelemetry(TRUE);
#endif
		}
		else{
		    mail_nodebug(ps_global->mail_stream);
#ifdef	_WINDOWS
		    mswin_enableimaptelemetry(FALSE);
#endif
		}
	    }

	    if(debug > 7 && olddebug <= 7)
	      mail_parameters(NULL, SET_TCPDEBUG, (void *) TRUE);
	    else if(debug <= 7 && olddebug > 7 && !ps_global->debugmem)
	      mail_parameters(NULL, SET_TCPDEBUG, (void *) FALSE);

	    if(debug > 7 && olddebug <= 7)
	      mail_parameters(NULL, SET_HTTPDEBUG, (void *) TRUE);
	    else if(debug <= 7 && olddebug > 7 && !ps_global->debugmem)
	      mail_parameters(NULL, SET_HTTPDEBUG, (void *) FALSE);

	    dprint((1, "*** Debug level set to %d ***\n", debug));
	    if(debugfile)
	      fflush(debugfile);

	    q_status_message1(SM_ORDER, 0, 1, _("Debug level set to %s"),
			      int2string(debug));
	    continue;
	}
#endif	/* DEBUG */
	else{
	    cmd = menu_command(ch, km);

	    if(km_popped)
	      switch(cmd){
		case MC_NONE :
		case MC_OTHER :
		case MC_RESIZE :
		case MC_REPAINT :
		  km_popped++;
		  break;

		default:
		  clearfooter(pine_state);
		  break;
	      }
	}

	/*------ Execute the command ------*/
	switch (cmd){
help_case :
	    /*------ HELP ------*/
	  case MC_HELP :

	    if(FOOTER_ROWS(pine_state) == 1 && km_popped == 0){
		km_popped = 2;
		pine_state->mangled_footer = 1;
	    }
	    else{
		/* TRANSLATORS: This is a screen title */
		helper(main_menu_tx, _("HELP FOR MAIN MENU"), 0);
		pine_state->mangled_screen = 1;
	    }

	    break;


	    /*---------- display other key bindings ------*/
	  case MC_OTHER :
	    if(ch == 'o')
	      warn_other_cmds();

	    what = NextMenu;
	    pine_state->mangled_footer = 1;
	    break;


	    /*---------- Previous item in menu ----------*/
	  case MC_PREVITEM :
	    if(menu_index > 0) {
		menu_index--;
		pine_state->mangled_body = 1;
		if(km->which == 0)
		  pine_state->mangled_footer = 1;

		just_a_navigate_cmd++;
	    }
	    else
	      /* TRANSLATORS: list refers to list of commands in main menu */
	      q_status_message(SM_ORDER, 0, 2, _("Already at top of list"));

	    break;


	    /*---------- Next item in menu ----------*/
	  case MC_NEXTITEM :
	    if(menu_index < MAX_MENU_ITEM){
		menu_index++;
		pine_state->mangled_body = 1;
		if(km->which == 0)
		  pine_state->mangled_footer = 1;

		just_a_navigate_cmd++;
	    }
	    else
	      q_status_message(SM_ORDER, 0, 2, _("Already at bottom of list"));

	    break;


	    /*---------- Release Notes ----------*/
	  case MC_RELNOTES :
	    /* TRANSLATORS: This is a screen title */
	    helper(h_news, _("ALPINE RELEASE NOTES"), 0);
	    pine_state->mangled_screen = 1;
	    break;


#ifdef KEYBOARD_LOCK
	    /*---------- Keyboard lock ----------*/
	  case MC_KBLOCK :
	    (void) lock_keyboard();
	    pine_state->mangled_screen = 1;
	    break;
#endif /* KEYBOARD_LOCK */


	    /*---------- Quit pine ----------*/
	  case MC_QUIT :
	    pine_state->next_screen = quit_screen;
	    return;

  
	    /*---------- Go to composer ----------*/
	  case MC_COMPOSE :
	    pine_state->next_screen = compose_screen;
	    return;

  
	    /*---- Go to alternate composer ------*/
	  case MC_ROLE :
	    pine_state->next_screen = alt_compose_screen;
	    return;

  
	    /*---------- Top of Folder list ----------*/
	  case MC_COLLECTIONS : 
	    pine_state->next_screen = folder_screen;
	    return;


	    /*---------- Goto new folder ----------*/
	  case MC_GOTO :
	    tc = ps_global->context_current;
	    new_folder = broach_folder(-FOOTER_ROWS(pine_state), 1, &notrealinbox, &tc);
	    if(new_folder)
	      visit_folder(ps_global, new_folder, tc, NULL, notrealinbox ? 0L : DB_INBOXWOCNTXT);

	    return;


	    /*---------- Go to index ----------*/
	  case MC_INDEX :
	    if(THREADING()
	       && sp_viewing_a_thread(pine_state->mail_stream)
	       && unview_thread(pine_state, pine_state->mail_stream,
				pine_state->msgmap)){
		pine_state->view_skipped_index = 0;
		pine_state->mangled_screen = 1;
	    }

	    pine_state->next_screen = mail_index_screen;
	    return;


	    /*---------- Review Status Messages ----------*/
	  case MC_JOURNAL :
	    review_messages();
	    pine_state->mangled_screen = 1;
	    break;


	    /*---------- Setup mini menu ----------*/
	  case MC_SETUP :
setup_case :
	    setup_command = setup_menu(pine_state);
	    pine_state->mangled_footer = 1;
	    do_setup_task(setup_command);
	    if(ps_global->next_screen != main_menu_screen)
	      return;

	    break;


	    /*---------- Go to address book ----------*/
	  case MC_ADDRBOOK :
	    pine_state->next_screen = addr_book_screen;
	    return;


	    /*------ Repaint the works -------*/
	  case MC_RESIZE :
          case MC_REPAINT :
	    ClearScreen();
	    pine_state->mangled_screen = 1;
	    break;

  
#ifdef	MOUSE
	    /*------- Mouse event ------*/
	  case MC_MOUSE :
	    {   
		MOUSEPRESS mp;
		unsigned ndmi;
		struct pine *ps = pine_state;

		mouse_get_last (NULL, &mp);

#ifdef	_WINDOWS
		if(mp.button == M_BUTTON_RIGHT){
		    if(!mp.doubleclick){
			static MPopup main_popup[] = {
			    {tQueue, {"Folder List", lNormal}, {'L'}},
			    {tQueue, {"Message Index", lNormal}, {'I'}},
			    {tSeparator},
			    {tQueue, {"Address Book", lNormal}, {'A'}},
			    {tQueue, {"Setup Options", lNormal}, {'S'}},
			    {tTail}
			};

			mswin_popup(main_popup);
		    }
		}
		else {
#endif
		    if (mp.row >= (HEADER_ROWS(ps) + MNSKIP(ps)))
		      ndmi = (mp.row+1 - HEADER_ROWS(ps) - (MNSKIP(ps)+1))/(MNSKIP(ps)+1);

		    if (mp.row >= (HEADER_ROWS(ps) + MNSKIP(ps))
			&& !(MNSKIP(ps) && (mp.row+1) & 0x01)
			&& ndmi <= MAX_MENU_ITEM
			&& FOOTER_ROWS(ps) + (ndmi+1)*(MNSKIP(ps)+1)
			    + MNSKIP(ps) + FOOTER_ROWS(ps) <= ps->ttyo->screen_rows){
			if(mp.doubleclick){
			    switch(ndmi){	/* fake main_screen request */
			      case 0 :
				goto help_case;

			      case 1 :
				pine_state->next_screen = compose_screen;
				return;

			      case 2 :
				pine_state->next_screen = mail_index_screen;
				return;

			      case 3 :
				pine_state->next_screen = folder_screen;
				return;

			      case 4 :
				pine_state->next_screen = addr_book_screen;
				return;

			      case 5 :
				goto setup_case;

			      case 6 :
				pine_state->next_screen = quit_screen;
				return;

			      default:			/* no op */
				break;
			    }
			}
			else{
			    menu_index = ndmi;
			    pine_state->mangled_body = 1;
			    if(km->which == 0)
			      pine_state->mangled_footer = 1;

			    just_a_navigate_cmd++;
			}
		    }
#ifdef	_WINDOWS
		}
#endif
	    }

	    break;
#endif


	    /*------ Input timeout ------*/
	  case MC_NONE :
            break;	/* noop for timeout loop mail check */


	    /*------ Bogus Input ------*/
          case MC_UNKNOWN :
	    if(ch == 'm' || ch == 'M'){
		q_status_message(SM_ORDER, 0, 1, "Already in Main Menu");
		break;
	    }

	  default:
	    bogus_command(ch, F_ON(F_USE_FK,pine_state) ? "F1" : "?");
	    break;

	  case MC_UTF8:
	    bogus_utf8_command(utf8str, F_ON(F_USE_FK, pine_state) ? "F1" : "?");
	    break;
	 } /* the switch */
    } /* the BIG while loop! */
}


/*----------------------------------------------------------------------
    Re-Draw the main menu

    Args: none.

  Result: main menu is re-displayed
  ----*/
void
main_redrawer(void)
{
    struct key_menu *km = &main_keymenu;

    ps_global->mangled_screen = 1;
    show_main_screen(ps_global, 0, FirstMenu, km, 0, (Pos *)NULL);
}

	
/*----------------------------------------------------------------------
         Draw the main menu

    Args: pine_state - the usual struct
	  quick_draw - tells do_menu() it can skip some drawing
	  what       - tells which section of keymenu to draw
	  km         - the keymenu
	  cursor_pos - returns a good position for the cursor to be located

  Result: main menu is displayed
  ----*/
void
show_main_screen(struct pine *ps, int quick_draw, OtherMenu what,
		 struct key_menu *km, int km_popped, Pos *cursor_pos)
{
    if(ps->painted_body_on_startup || ps->painted_footer_on_startup){
	ps->mangled_screen = 0;		/* only worry about it here */
	ps->mangled_header = 1;		/* we have to redo header */
	if(!ps->painted_body_on_startup)
	  ps->mangled_body = 1;		/* make sure to paint body*/

	if(!ps->painted_footer_on_startup)
	  ps->mangled_footer = 1;	/* make sure to paint footer*/

	ps->painted_body_on_startup   = 0;
        ps->painted_footer_on_startup = 0;
    }

    if(ps->mangled_screen){
	ps->mangled_header = 1;
	ps->mangled_body   = 1;
	ps->mangled_footer = 1;
	ps->mangled_screen = 0;
    }

#ifdef _WINDOWS
    /* Reset the scroll range.  Main screen never scrolls. */
    scroll_setrange (0L, 0L);
    mswin_beginupdate();
#endif

    /* paint the titlebar if needed */
    if(ps->mangled_header){
	/* TRANSLATORS: screen title */
	set_titlebar(_("MAIN MENU"), ps->mail_stream, ps->context_current,
		     ps->cur_folder, ps->msgmap, 1, FolderName, 0, 0, NULL);
	ps->mangled_header = 0;
    }

    /* paint the body if needed */
    if(ps->mangled_body){
	if(!quick_draw)
	  ClearBody();

	do_menu(quick_draw, cursor_pos, km);
	ps->mangled_body = 0;
    }

    /* paint the keymenu if needed */
    if(km && ps->mangled_footer){
	static char label[LONGEST_LABEL + 2 + 1], /* label + brackets + \0 */
		    name[8];
	bitmap_t    bitmap;

	setbitmap(bitmap);

#ifdef KEYBOARD_LOCK
	if(ps_global->restricted || F_ON(F_DISABLE_KBLOCK_CMD,ps_global))
#endif
	  clrbitn(MAIN_KBLOCK_KEY, bitmap);

	menu_clear_binding(km, '>');
	menu_clear_binding(km, '.');
	menu_clear_binding(km, KEY_RIGHT);
	menu_clear_binding(km, ctrl('M'));
	menu_clear_binding(km, ctrl('J'));
	km->keys[MAIN_DEFAULT_KEY].bind
				 = km->keys[mkeys[menu_index].key_index].bind;
	km->keys[MAIN_DEFAULT_KEY].label
				 = km->keys[mkeys[menu_index].key_index].label;

	/* put brackets around the default action */
	snprintf(label, sizeof(label), "[%s]", km->keys[mkeys[menu_index].key_index].label);
	label[sizeof(label)-1] = '\0';
	strncpy(name, ">", sizeof(name));
	name[sizeof(name)-1] = '\0';
	km->keys[MAIN_DEFAULT_KEY].label = label;
	km->keys[MAIN_DEFAULT_KEY].name = name;
	menu_add_binding(km, '>', km->keys[MAIN_DEFAULT_KEY].bind.cmd);
	menu_add_binding(km, '.', km->keys[MAIN_DEFAULT_KEY].bind.cmd);
	menu_add_binding(km, ctrl('M'), km->keys[MAIN_DEFAULT_KEY].bind.cmd);
	menu_add_binding(km, ctrl('J'), km->keys[MAIN_DEFAULT_KEY].bind.cmd);

	if(F_ON(F_ARROW_NAV,ps_global))
	  menu_add_binding(km, KEY_RIGHT, km->keys[MAIN_DEFAULT_KEY].bind.cmd);

	if(km_popped){
	    FOOTER_ROWS(ps) = 3;
	    clearfooter(ps);
	}

	draw_keymenu(km, bitmap, ps_global->ttyo->screen_cols,
		     1-FOOTER_ROWS(ps_global), 0, what);
	ps->mangled_footer = 0;
	if(km_popped){
	    FOOTER_ROWS(ps) = 1;
	    mark_keymenu_dirty();
	}
    }

#ifdef _WINDOWS
    mswin_endupdate();
#endif
}


/*----------------------------------------------------------------------
         Actually display the main menu

    Args: quick_draw - just a next or prev command was typed so we only have
		       to redraw the highlighting
          cursor_pos - a place to return a good value for cursor location

  Result: Main menu is displayed
  ---*/
void
do_menu(int quick_draw, Pos *cursor_pos, struct key_menu *km)
{
    struct pine *ps = ps_global;
    int  dline, indent, longest = 0, cmd;
    char buf[4*MAX_SCREEN_COLS+1];
    char buf2[4*MAX_SCREEN_COLS+1];
    static int last_inverse = -1;
    Pos pos;

    /* find the longest command */
    for(cmd = 0; cmd < sizeof(mkeys)/(sizeof(mkeys[1])); cmd++){
	memset((void *) buf, ' ', sizeof(buf));
        snprintf(buf, sizeof(buf), mkeys[cmd].key_and_name[0] ? _(mkeys[cmd].key_and_name) : "",
		(F_OFF(F_USE_FK,ps)
		 && km->keys[mkeys[cmd].key_index].name)
		   ? km->keys[mkeys[cmd].key_index].name : "",
		(ps->VAR_NEWS_SPEC && mkeys[cmd].news_addition && mkeys[cmd].news_addition[0])
		  ? _(mkeys[cmd].news_addition) : "");
	buf[sizeof(buf)-1] = '\0';

	if(longest < (indent = utf8_width(buf)))
	  longest = indent;
    }

    indent = MAX(((ps->ttyo->screen_cols - longest)/2) - 1, 0);

    dline = HEADER_ROWS(ps) + MNSKIP(ps);
    for(cmd = 0; cmd < sizeof(mkeys)/(sizeof(mkeys[1])); cmd++){
	/* leave room for copyright and footer */
	if(dline + MNSKIP(ps) + 1 + FOOTER_ROWS(ps) >= ps->ttyo->screen_rows)
	  break;

	if(quick_draw && !(cmd == last_inverse || cmd == menu_index)){
	    dline += (1 + MNSKIP(ps));
	    continue;
	}

	if(cmd == menu_index)
	  StartInverse();

	memset((void *) buf, ' ', sizeof(buf));
        snprintf(buf, sizeof(buf), mkeys[cmd].key_and_name[0] ? _(mkeys[cmd].key_and_name) : "",
		(F_OFF(F_USE_FK,ps)
		 && km->keys[mkeys[cmd].key_index].name)
		   ? km->keys[mkeys[cmd].key_index].name : "",
		(ps->VAR_NEWS_SPEC && mkeys[cmd].news_addition && mkeys[cmd].news_addition[0])
		  ? _(mkeys[cmd].news_addition) : "");
	buf[sizeof(buf)-1] = '\0';

	utf8_pad_to_width(buf2, buf, sizeof(buf2),
			  MIN(ps->ttyo->screen_cols-indent,longest+1), 1);
	pos.row = dline++;
	pos.col = indent;
        PutLine0(pos.row, pos.col, buf2);

	if(MNSKIP(ps))
	  dline++;

	if(cmd == menu_index){
	    if(cursor_pos){
		cursor_pos->row = pos.row;
		/* 6 is 1 for the letter plus 5 spaces */
		cursor_pos->col = pos.col + 6;
		if(F_OFF(F_USE_FK,ps))
		  cursor_pos->col++;

		cursor_pos->col = MIN(cursor_pos->col, ps->ttyo->screen_cols);
	    }

	    EndInverse();
	}
    }


    last_inverse = menu_index;

    if(!quick_draw && FOOTER_ROWS(ps)+1 < ps->ttyo->screen_rows){
	utf8_to_width(buf2, LEGAL_NOTICE, sizeof(buf2),
		      ps->ttyo->screen_cols-3, NULL);
	PutLine0(ps->ttyo->screen_rows - (FOOTER_ROWS(ps)+1),
		 MAX(0, ((ps->ttyo->screen_cols-utf8_width(buf2))/2)),
		 buf2);
    }

    fflush(stdout);
}


int
choose_setup_cmd(int cmd, MSGNO_S *msgmap, SCROLL_S *sparms)
{
    int rv = 1;
    SRV_S *srv;

    if(!(srv = (SRV_S *)sparms->proc.data.p)){
	sparms->proc.data.p = (SRV_S *)fs_get(sizeof(*srv));
	srv = (SRV_S *)sparms->proc.data.p;
	memset(srv, 0, sizeof(*srv));
    }

    ps_global->next_screen = SCREEN_FUN_NULL;

    switch(cmd){
      case MC_PRINTER :
	srv->cmd = 'p';
	break;

      case MC_PASSWD :
	srv->cmd = 'n';
	break;

      case MC_CONFIG :
	srv->cmd = 'c';
	break;

      case MC_XOAUTH2 :
	srv->cmd = 'u';
	break;

      case MC_SIG :
	srv->cmd = 's';
	break;

      case MC_ABOOKS :
	srv->cmd = 'a';
	break;

      case MC_CLISTS :
	srv->cmd = 'l';
	break;

      case MC_RULES :
	srv->cmd = 'r';
	break;

      case MC_DIRECTORY :
	srv->cmd = 'd';
	break;

      case MC_KOLOR :
	srv->cmd = 'k';
	break;

      case MC_REMOTE :
	srv->cmd = 'z';
	break;

      case MC_SECURITY :	/* S/MIME setup screen */
	srv->cmd = 'm';
	break;

      case MC_EXCEPT :
	srv->exc = !srv->exc;
	menu_clear_binding(sparms->keys.menu, 'x');
	if(srv->exc){
	  if(sparms->bar.title) fs_give((void **)&sparms->bar.title);
	  /* TRANSLATORS: screen title */
	  sparms->bar.title = cpystr(_("SETUP EXCEPTIONS"));
	  ps_global->mangled_header = 1;
	  /* TRANSLATORS: The reason the X is upper case in eXceptions
	     is because the command key is X. It isn't necessary, just
	     nice if it works. */
	  menu_init_binding(sparms->keys.menu, 'x', MC_EXCEPT, "X",
			    N_("not eXceptions"), SETUP_EXCEPT);
	}
	else{
	  if(sparms->bar.title) fs_give((void **)&sparms->bar.title);
	  /* TRANSLATORS: screen title */
	  sparms->bar.title = cpystr(_("SETUP"));
	  ps_global->mangled_header = 1;
	  menu_init_binding(sparms->keys.menu, 'x', MC_EXCEPT, "X",
			    N_("eXceptions"), SETUP_EXCEPT);
	}

	if(sparms->keys.menu->which == 1)
	  ps_global->mangled_footer = 1;

	rv = 0;
	break;

      case MC_NO_EXCEPT :
#if defined(DOS) || defined(OS2)
        q_status_message(SM_ORDER, 0, 2, _("Need argument \"-x <except_config>\" or \"PINERCEX\" file to use eXceptions"));
#else
        q_status_message(SM_ORDER, 0, 2, _("Need argument \"-x <except_config>\" or \".pinercex\" file to use eXceptions"));
#endif
	rv = 0;
	break;

      default:
	alpine_panic("Unexpected command in choose_setup_cmd");
	break;
    }

    return(rv);
}


int
setup_menu(struct pine *ps)
{
    int         ret = 0, exceptions = 0;
    int         printer = 0, passwd = 0, config = 0, sig = 0, dir = 0, smime = 0, exc = 0;
    SCROLL_S	sargs;
    SRV_S      *srv;
    STORE_S    *store;

    if(!(store = so_get(CharStar, NULL, EDIT_ACCESS))){
	q_status_message(SM_ORDER | SM_DING, 3, 3, _("Error allocating space."));
	return(ret);
    }

#if	!defined(DOS)
    if(!ps_global->vars[V_PRINTER].is_fixed)	/* printer can be changed */
      printer++;
#endif

#ifdef	PASSWD_PROG
    if(F_OFF(F_DISABLE_PASSWORD_CMD,ps_global))	/* password is allowed */
      passwd++;
#endif

    if(F_OFF(F_DISABLE_CONFIG_SCREEN,ps_global))	/* config allowed */
      config++;

    if(F_OFF(F_DISABLE_SIGEDIT_CMD,ps_global))	/* .sig editing is allowed */
      sig++;

#ifdef	ENABLE_LDAP
    dir++;
#endif

#ifdef	SMIME
    smime++;
#endif

    if(ps_global->post_prc)
      exc++;

    /* TRANSLATORS: starting here we have a whole screen of help text */
    so_puts(store, _("This is the Setup screen for Alpine. Choose from the following commands:\n"));

    so_puts(store, "\n");
    so_puts(store, _("(E) Exit Setup:\n"));
    so_puts(store, _("    This puts you back at the Main Menu.\n"));

    if(exc){
	so_puts(store, "\n");
	so_puts(store, _("(X) eXceptions:\n"));
	so_puts(store, _("    This command is different from the rest. It is not actually a command\n"));
	so_puts(store, _("    itself. Instead, it is a toggle which modifies the behavior of the\n"));
	so_puts(store, _("    other commands. You toggle Exceptions editing on and off with this\n"));
	so_puts(store, _("    command. When it is off you will be editing (changing) your regular\n"));
	so_puts(store, _("    configuration file. When it is on you will be editing your exceptions\n"));
	so_puts(store, _("    configuration file. For example, you might want to type the command \n"));
	so_puts(store, _("    \"eXceptions\" followed by \"Kolor\" to setup different screen colors\n"));
	so_puts(store, _("    on a particular platform.\n"));
	so_puts(store, _("    (Note: this command does not show up on the keymenu at the bottom of\n"));
	so_puts(store, _("    the screen unless you press \"O\" for \"Other Commands\" --but you don't\n"));
	so_puts(store, _("    need to press the \"O\" in order to invoke the command.)\n"));
    }

    if(printer){
	so_puts(store, "\n");
	so_puts(store, _("(P) Printer:\n"));
	so_puts(store, _("    Allows you to set a default printer and to define custom\n"));
	so_puts(store, _("    print commands.\n"));
    }

    if(passwd){
	so_puts(store, "\n");
	so_puts(store, _("(N) Newpassword:\n"));
	so_puts(store, _("    Change your password.\n"));
    }

    if(config){
	so_puts(store, "\n");
	so_puts(store, _("(C) Config:\n"));
	so_puts(store, _("    Allows you to set or unset many features of Alpine.\n"));
	so_puts(store, _("    You may also set the values of many options with this command.\n"));
    }

    if(sig){
	so_puts(store, "\n");
	so_puts(store, _("(S) Signature:\n"));
	so_puts(store, _("    Enter or edit a custom signature which will\n"));
	so_puts(store, _("    be included with each new message you send.\n"));
    }

    so_puts(store, "\n");
    so_puts(store, _("(A) AddressBooks:\n"));
    so_puts(store, _("    Define a non-default address book.\n"));

    so_puts(store, "\n");
    so_puts(store, _("(L) collectionLists:\n"));
    so_puts(store, _("    You may define groups of folders to help you better organize your mail.\n"));

    so_puts(store, "\n");
    so_puts(store, _("(R) Rules:\n"));
    so_puts(store, _("    This has up to six sub-categories: Roles, Index Colors, Filters,\n"));
    so_puts(store, _("    SetScores, Search, and Other. If the Index Colors option is\n"));
    so_puts(store, _("    missing you may turn it on (if possible) with Setup/Kolor.\n"));
    so_puts(store, _("    If Roles is missing it has probably been administratively disabled.\n"));

    if(dir){
	so_puts(store, "\n");
	so_puts(store, _("(D) Directory:\n"));
	so_puts(store, _("    Define an LDAP Directory server for Alpine's use. A directory server is\n"));
	so_puts(store, _("    similar to an address book, but it is usually maintained by an\n"));
	so_puts(store, _("    organization. It is similar to a telephone directory.\n"));
    }

    so_puts(store, "\n");
    so_puts(store, _("(K) Kolor:\n"));
    so_puts(store, _("    Set custom colors for various parts of the Alpine screens. For example, the\n"));
    so_puts(store, _("    command key labels, the titlebar at the top of each page, and quoted\n"));
    so_puts(store, _("    sections of messages you are viewing.\n"));

    if(smime){
	so_puts(store, "\n");
	so_puts(store, _("(M) S/MIME:\n"));
	so_puts(store, _("    Setup for using S/MIME to verify signed messages, decrypt\n"));
	so_puts(store, _("    encrypted messages, and to sign or encrypt outgoing messages.\n"));
    }

    so_puts(store, "\n");
    so_puts(store, _("(U) xoaUth2:\n"));
    so_puts(store, _("    Set client-id and client-secret to use the XOAUTH2\n"));
    so_puts(store, _("    authenticator.\n"));

    so_puts(store, "\n");
    so_puts(store, _("(Z) RemoteConfigSetup:\n"));
    so_puts(store, _("    This is a command you will probably only want to use once, if at all.\n"));
    so_puts(store, _("    It helps you transfer your Alpine configuration data to an IMAP server,\n"));
    so_puts(store, _("    where it will be accessible from any of the computers you read mail\n"));
    so_puts(store, _("    from (using Alpine). The idea behind a remote configuration is that you\n"));
    so_puts(store, _("    can change your configuration in one place and have that change show\n"));
    so_puts(store, _("    up on all of the computers you use.\n"));
    so_puts(store, _("    (Note: this command does not show up on the keymenu at the bottom of\n"));
    so_puts(store, _("    the screen unless you press \"O\" for \"Other Commands\" --but you don't\n"));
    so_puts(store, _("    need to press the \"O\" in order to invoke the command.)\n"));

    /* put this down here for people who don't have exceptions */
    if(!exc){
	so_puts(store, "\n");
	so_puts(store, _("(X) eXceptions:\n"));
	so_puts(store, _("    This command is different from the rest. It is not actually a command\n"));
	so_puts(store, _("    itself. Instead, it is a toggle which modifies the behavior of the\n"));
	so_puts(store, _("    other commands. You toggle Exceptions editing on and off with this\n"));
	so_puts(store, _("    command. When it is off you will be editing (changing) your regular\n"));
	so_puts(store, _("    configuration file. When it is on you will be editing your exceptions\n"));
	so_puts(store, _("    configuration file. For example, you might want to type the command \n"));
	so_puts(store, _("    \"eXceptions\" followed by \"Kolor\" to setup different screen colors\n"));
	so_puts(store, _("    on a particular platform.\n"));
	so_puts(store, _("    (Note: this command does not do anything unless you have a configuration\n"));
	so_puts(store, _("    with exceptions enabled (you don't have that). Common ways to enable an\n"));
	so_puts(store, _("    exceptions config are the command line argument \"-x <exception_config>\";\n"));
	so_puts(store, _("    or the existence of the file \".pinercex\" for Unix Alpine, or \"PINERCEX\")\n"));
	so_puts(store, _("    for PC-Alpine.)\n"));
	so_puts(store, _("    (Another note: this command does not show up on the keymenu at the bottom\n"));
	so_puts(store, _("    of the screen unless you press \"O\" for \"Other Commands\" --but you\n"));
	so_puts(store, _("    don't need to press the \"O\" in order to invoke the command.)\n"));
    }

    memset(&sargs, 0, sizeof(SCROLL_S));
    sargs.text.text   = so_text(store);
    sargs.text.src    = CharStar;
    sargs.text.desc   = _("Information About Setup Command");
    sargs.bar.title   = cpystr(_("SETUP"));
    sargs.proc.tool   = choose_setup_cmd;
    sargs.help.text   = NO_HELP;
    sargs.help.title  = NULL;
    sargs.keys.menu   = &choose_setup_keymenu;
    sargs.keys.menu->how_many = 2;

    setbitmap(sargs.keys.bitmap);
    if(!printer)
      clrbitn(SETUP_PRINTER, sargs.keys.bitmap);

    if(!passwd)
      clrbitn(SETUP_PASSWD, sargs.keys.bitmap);

    if(!config)
      clrbitn(SETUP_CONFIG, sargs.keys.bitmap);

    if(!sig)
      clrbitn(SETUP_SIG, sargs.keys.bitmap);

    if(!dir)
      clrbitn(SETUP_DIRECTORY, sargs.keys.bitmap);

    if(!smime)
      clrbitn(SETUP_SMIME, sargs.keys.bitmap);

    if(exc)
      menu_init_binding(sargs.keys.menu, 'x', MC_EXCEPT, "X",
			N_("eXceptions"), SETUP_EXCEPT);
    else
      menu_init_binding(sargs.keys.menu, 'x', MC_NO_EXCEPT, "X",
			N_("eXceptions"), SETUP_EXCEPT);


    scrolltool(&sargs);

    ps->mangled_screen = 1;

    srv = (SRV_S *)sargs.proc.data.p;

    exceptions = srv ? srv->exc : 0;

    so_give(&store);

    if(sargs.bar.title) fs_give((void**)&sargs.bar.title);
    if(srv){
	ret = srv->cmd;
	fs_give((void **)&sargs.proc.data.p);
    }
    else
      ret = 'e';

    return(ret | (exceptions ? EDIT_EXCEPTION : 0));
}


/*----------------------------------------------------------------------

Args: command -- command char to perform

  ----*/
void
do_setup_task(int command)
{
    char *err = NULL;
    int   rtype;
    int   edit_exceptions = 0;
    int   do_lit_sig = 0;

    if(command & EDIT_EXCEPTION){
	edit_exceptions = 1;
	command &= ~EDIT_EXCEPTION;
    }

    switch(command) {
        /*----- EDIT SIGNATURE -----*/
      case 's':
	if(ps_global->VAR_LITERAL_SIG)
	  do_lit_sig = 1;
	else {
	    char sig_path[MAXPATH+1];

	    if(!signature_path(ps_global->VAR_SIGNATURE_FILE, sig_path, MAXPATH))
	      do_lit_sig = 1;
	    else if((!IS_REMOTE(ps_global->VAR_SIGNATURE_FILE)
		     && can_access(sig_path, READ_ACCESS) == 0)
		    ||(IS_REMOTE(ps_global->VAR_SIGNATURE_FILE)
		       && (folder_exists(NULL, sig_path) & FEX_ISFILE)))
	      do_lit_sig = 0;
	    else if(!ps_global->vars[V_SIGNATURE_FILE].main_user_val.p
		    && !ps_global->vars[V_SIGNATURE_FILE].cmdline_val.p
		    && !ps_global->vars[V_SIGNATURE_FILE].fixed_val.p)
	      do_lit_sig = 1;
	    else
	      do_lit_sig = 0;
	}

	if(do_lit_sig){
	    char     *result = NULL;
	    char    **apval;
	    EditWhich ew;
	    int       readonly = 0;

	    ew = edit_exceptions ? ps_global->ew_for_except_vars : Main;

	    if(ps_global->restricted)
	      readonly = 1;
	    else switch(ew){
		   case Main:
		     readonly = ps_global->prc->readonly;
		     break;
		   case Post:
		     readonly = ps_global->post_prc->readonly;
		     break;
		   default:
		     break;
	    }

	    if(readonly)
	      err = cpystr(ps_global->restricted
				     ? "Alpine demo can't change config file"
				     : _("Config file not changeable"));

	    if(!err){
		apval = APVAL(&ps_global->vars[V_LITERAL_SIG], ew);
		if(!apval)
		  err = cpystr(_("Problem accessing configuration"));
		else{
		    char *input;

		    input = (char *)fs_get((strlen(*apval ? *apval : "")+1) *
								sizeof(char));
		    input[0] = '\0';
		    cstring_to_string(*apval, input);
		    err = signature_edit_lit(input, &result,
					     _("SIGNATURE EDITOR"),
					     h_composer_sigedit);
		    fs_give((void **)&input);
		}
	    }

	    if(!err){
		char *cstring_version;

		cstring_version = string_to_cstring(result);

		set_variable(V_LITERAL_SIG, cstring_version, 0, 0, ew);

		if(cstring_version)
		  fs_give((void **)&cstring_version);
	    }

	    if(result)
	      fs_give((void **)&result);
	}
	else
	    err = signature_edit(ps_global->VAR_SIGNATURE_FILE,
				 _("SIGNATURE EDITOR"));

	if(err){
	    q_status_message(SM_ORDER, 3, 4, err);
	    fs_give((void **)&err);
	}

	ps_global->mangled_screen = 1;
	break;

        /*----- ADD ADDRESSBOOK ----*/
      case 'a':
	addr_book_config(ps_global, edit_exceptions);
	menu_index = ABOOK_MENU_ITEM;
	ps_global->mangled_screen = 1;
	break;

#ifdef	ENABLE_LDAP
        /*--- ADD DIRECTORY SERVER --*/
      case 'd':
	directory_config(ps_global, edit_exceptions);
	ps_global->mangled_screen = 1;
	break;
#endif

#ifdef	SMIME
        /*--- S/MIME --*/
      case 'm':
	smime_config_screen(ps_global, edit_exceptions);
	ps_global->mangled_screen = 1;
	break;
#endif

        /*----- CONFIGURE OPTIONS -----*/
      case 'c':
	option_screen(ps_global, edit_exceptions);
	ps_global->mangled_screen = 1;
	break;

        /*----- XOAUTH2 CLIENT CONFIGURATION -----*/
      case 'u':
	alpine_xoauth2_configuration(ps_global, edit_exceptions);
	ps_global->mangled_screen = 1;
	break;

        /*----- COLLECTION LIST -----*/
      case 'l':
	folder_config_screen(ps_global, edit_exceptions);
	ps_global->mangled_screen = 1;
	break;

        /*----- RULES -----*/
      case 'r':
	rtype = rule_setup_type(ps_global, RS_RULES | RS_INCFILTNOW,
				_("Type of rule setup : "));
	switch(rtype){
	  case 'r':
	  case 's':
	  case 'i':
	  case 'f':
	  case 'o':
	  case 'c':
	    role_config_screen(ps_global, (rtype == 'r') ? ROLE_DO_ROLES :
					   (rtype == 's') ? ROLE_DO_SCORES :
					    (rtype == 'o') ? ROLE_DO_OTHER :
					     (rtype == 'f') ? ROLE_DO_FILTER :
					      (rtype == 'c') ? ROLE_DO_SRCH :
							        ROLE_DO_INCOLS,
			       edit_exceptions);
	    break;

	  case 'Z':
	    q_status_message(SM_ORDER | SM_DING, 3, 5,
			_("Try turning on color with the Setup/Kolor command."));
	    break;

	  case 'n':
	    role_process_filters();
	    break;

	  default:
	    cmd_cancelled(NULL);
	    break;
	}

	ps_global->mangled_screen = 1;
	break;

        /*----- COLOR -----*/
      case 'k':
	color_config_screen(ps_global, edit_exceptions);
	ps_global->mangled_screen = 1;
	break;

      case 'z':
	convert_to_remote_config(ps_global, edit_exceptions);
	ps_global->mangled_screen = 1;
	break;

        /*----- EXIT -----*/
      case 'e':
        break;

        /*----- NEW PASSWORD -----*/
      case 'n':
#ifdef	PASSWD_PROG
        if(ps_global->restricted){
	    q_status_message(SM_ORDER, 3, 5,
	    "Password change unavailable in restricted demo version of Alpine.");
        }else {
	    change_passwd();
	    ClearScreen();
	    ps_global->mangled_screen = 1;
	}
#else
        q_status_message(SM_ORDER, 0, 5,
		 _("Password changing not configured for this version of Alpine."));
	display_message('x');
#endif	/* DOS */
        break;

#if	!defined(DOS)
        /*----- CHOOSE PRINTER ------*/
      case 'p':
        select_printer(ps_global, edit_exceptions); 
	ps_global->mangled_screen = 1;
        break;
#endif
    }
}


int
rule_setup_type(struct pine *ps, int flags, char *prompt)
{
    ESCKEY_S opts[9];
    int ekey_num = 0, deefault = 0;

    if(flags & RS_INCADDR){
	deefault = 'a';
	opts[ekey_num].ch      = 'a';
	opts[ekey_num].rval    = 'a';
	opts[ekey_num].name    = "A";
	opts[ekey_num++].label = "Addrbook";
    }

  if(flags & RS_RULES){

    if(F_OFF(F_DISABLE_ROLES_SETUP,ps)){ /* roles are allowed */
	if(deefault != 'a')
	  deefault = 'r';

	opts[ekey_num].ch      = 'r';
	opts[ekey_num].rval    = 'r';
	opts[ekey_num].name    = "R";
	opts[ekey_num++].label = "Roles";
    }
    else if(deefault != 'a')
      deefault = 's';

    opts[ekey_num].ch      = 's';
    opts[ekey_num].rval    = 's';
    opts[ekey_num].name    = "S";
    opts[ekey_num++].label = "SetScores";

#ifndef	_WINDOWS
    if(ps->color_style != COL_NONE && pico_hascolor()){
#endif
	if(deefault != 'a')
	  deefault = 'i';

	opts[ekey_num].ch      = 'i';
	opts[ekey_num].rval    = 'i';
	opts[ekey_num].name    = "I";
	opts[ekey_num++].label = "Indexcolor";
#ifndef	_WINDOWS
    }
    else{
	opts[ekey_num].ch      = 'i';
	opts[ekey_num].rval    = 'Z';		/* notice this rval! */
	opts[ekey_num].name    = "I";
	opts[ekey_num++].label = "Indexcolor";
    }
#endif

    opts[ekey_num].ch      = 'f';
    opts[ekey_num].rval    = 'f';
    opts[ekey_num].name    = "F";
    opts[ekey_num++].label = "Filters";

    opts[ekey_num].ch      = 'o';
    opts[ekey_num].rval    = 'o';
    opts[ekey_num].name    = "O";
    opts[ekey_num++].label = "Other";

    opts[ekey_num].ch      = 'c';
    opts[ekey_num].rval    = 'c';
    opts[ekey_num].name    = "C";
    opts[ekey_num++].label = "searCh";

  }

    if(flags & RS_INCEXP){
	opts[ekey_num].ch      = 'e';
	opts[ekey_num].rval    = 'e';
	opts[ekey_num].name    = "E";
	opts[ekey_num++].label = "Export";
    }

    if(flags & RS_INCFILTNOW){
	opts[ekey_num].ch      = 'n';
	opts[ekey_num].rval    = 'n';
	opts[ekey_num].name    = "N";
	opts[ekey_num++].label = "filterNow";
    }

    opts[ekey_num].ch    = -1;

    return(radio_buttons(prompt, -FOOTER_ROWS(ps), opts,
			 deefault, 'x', NO_HELP, RB_NORM));
}



/*
 * Process the command list, changing function key notation into
 * lexical equivalents.
 */
void
process_init_cmds(struct pine *ps, char **list)
{
    char **p;
    int i = 0;
    int j;
    int lpm1;
#define MAX_INIT_CMDS 500
    /* this is just a temporary stack array, the real one is allocated below */
    int i_cmds[MAX_INIT_CMDS];
    int fkeys = 0;
    int not_fkeys = 0;
  
    if(list){
      for(p = list; *p; p++){
	  if(i >= MAX_INIT_CMDS){
	      snprintf(tmp_20k_buf, SIZEOF_20KBUF,
		      "Initial keystroke list too long at \"%s\"", *p);
	      init_error(ps, SM_ORDER | SM_DING, 3, 5, tmp_20k_buf);
	      break;
	  }
	  

	/* regular character commands */
	if(strlen(*p) == 1){
	  i_cmds[i++] = **p;
	  not_fkeys++;
	}

	/* special commands */
	else if(strucmp(*p, "SPACE") == 0)
	  i_cmds[i++] = ' ';
	else if(strucmp(*p, "CR") == 0)
	  i_cmds[i++] = '\n';
	else if(strucmp(*p, "TAB") == 0)
	  i_cmds[i++] = '\t';
	else if(strucmp(*p, "UP") == 0)
	  i_cmds[i++] = KEY_UP;
	else if(strucmp(*p, "DOWN") == 0)
	  i_cmds[i++] = KEY_DOWN;
	else if(strucmp(*p, "LEFT") == 0)
	  i_cmds[i++] = KEY_LEFT;
	else if(strucmp(*p, "RIGHT") == 0)
	  i_cmds[i++] = KEY_RIGHT;

	/* control chars */
	else if(strlen(*p) == 2 && **p == '^')
	  i_cmds[i++] = ctrl(*((*p)+1));

	/* function keys */
	else if(**p == 'F' || **p == 'f'){
	    int v;

	    fkeys++;
	    v = atoi((*p)+1);
	    if(v >= 1 && v <= 12)
	      i_cmds[i++] = PF1 + v - 1;
	    else
	      i_cmds[i++] = KEY_JUNK;
	}

	/* literal string */
	else if(**p == '"' && (*p)[lpm1 = strlen(*p) - 1] == '"'){
	    if(lpm1 + i - 1 > MAX_INIT_CMDS){
		snprintf(tmp_20k_buf, SIZEOF_20KBUF,
			"Initial keystroke list too long, truncated at %s\n", *p);
		init_error(ps, SM_ORDER | SM_DING, 3, 5, tmp_20k_buf);
		break;                   /* Bail out of this loop! */
	    } else
	      for(j = 1; j < lpm1; j++)
		i_cmds[i++] = (*p)[j];
	}
	else {
	    snprintf(tmp_20k_buf,SIZEOF_20KBUF,
		    "Bad initial keystroke \"%.500s\" (missing comma?)", *p);
	    init_error(ps, SM_ORDER | SM_DING, 3, 5, tmp_20k_buf);
	    break;
	}
      }
    }

    /*
     * We don't handle the case where function keys are used to specify the
     * commands but some non-function key input is also required.  For example,
     * you might want to jump to a specific message number and view it
     * on start up.  To do that, you need to use character commands instead
     * of function key commands in the initial-keystroke-list.
     */
    if(fkeys && not_fkeys){
	init_error(ps, SM_ORDER | SM_DING, 3, 5,
"Mixed characters and function keys in \"initial-keystroke-list\", skipping.");
	i = 0;
    }

    if(fkeys && !not_fkeys)
      F_TURN_ON(F_USE_FK,ps);
    if(!fkeys && not_fkeys)
      F_TURN_OFF(F_USE_FK,ps);

    if(i > 0){
	ps->initial_cmds = (int *)fs_get((i+1) * sizeof(int));
	ps->free_initial_cmds = ps->initial_cmds;
	for(j = 0; j < i; j++)
	  ps->initial_cmds[j] = i_cmds[j];

	ps->initial_cmds[i] = 0;
	ps->in_init_seq = ps->save_in_init_seq = 1;
    }
}


UCS *
user_wordseps(char **list)
 {
    char **p;
    int i = 0;
    int j;
#define MAX_SEPARATORS 500
    /*
     * This is just a temporary stack array, the real one is allocated below.
     * This is supposed to be way large enough.
     */
    UCS seps[MAX_SEPARATORS+1];
    UCS *u;
    UCS *return_array = NULL;
    size_t l;
  
    seps[0] = '\0';

    if(list){
	for(p = list; *p; p++){
	    if(i >= MAX_SEPARATORS){
		q_status_message(SM_ORDER | SM_DING, 3, 3,
			  "Warning: composer-word-separators list is too long");
		break;
	    }

	    u = utf8_to_ucs4_cpystr(*p);

	    if(u){
		if(ucs4_strlen(u) == 1)
		  seps[i++] = *u;
		else if(*u == '"' && u[l = ucs4_strlen(u) - 1] == '"'){
		    if(l + i - 1 > MAX_SEPARATORS){
			q_status_message(SM_ORDER | SM_DING, 3, 3,
				  "Warning: composer-word-separators list is too long");
			break;                   /* Bail out of this loop! */
		    }
		    else{
			for(j = 1; j < l; j++)
			  seps[i++] = u[j];
		    }
		}
		else{
		    l = ucs4_strlen(u);
		    if(l + i > MAX_SEPARATORS){
			q_status_message(SM_ORDER | SM_DING, 3, 3,
				  "Warning: composer-word-separators list is too long");
			break;                   /* Bail out of this loop! */
		    }
		    else{
			for(j = 0; j < l; j++)
			  seps[i++] = u[j];
		    }
		}

		fs_give((void **) &u);
	    }
	}
    }

    seps[i] = '\0';

    if(i > 0)
      return_array = ucs4_cpystr(seps);

    return(return_array);
}


/*
 * Make sure any errors during initialization get queued for display
 */
void
queue_init_errors(struct pine *ps)
{
    int i;

    if(ps->init_errs){
	for(i = 0; (ps->init_errs)[i].message; i++){
	    q_status_message((ps->init_errs)[i].flags,
			     (ps->init_errs)[i].min_time,
			     (ps->init_errs)[i].max_time,
			     (ps->init_errs)[i].message);
	    fs_give((void **)&(ps->init_errs)[i].message);
	}

	fs_give((void **)&ps->init_errs);
    }
}


/*----------------------------------------------------------------------
          Quit pine if the user wants to 

    Args: The usual pine structure

  Result: User is asked if she wants to quit, if yes then execute quit.

       Q U I T    S C R E E N

Not really a full screen. Just count up deletions and ask if we really
want to quit.
  ----*/
void
quit_screen(struct pine *pine_state)
{
    int quit = 0;

    dprint((1, "\n\n    ---- QUIT SCREEN ----\n"));    

    if(F_ON(F_CHECK_MAIL_ONQUIT,ps_global)
       && pine_state->mail_stream != NULL
       && new_mail(1, VeryBadTime, NM_STATUS_MSG | NM_DEFER_SORT) > 0
       && (quit = want_to(_("Quit even though new mail just arrived"), 'y', 0,
			  NO_HELP, WT_NORM | WT_DING)) != 'y'){
	refresh_sort(pine_state->mail_stream, pine_state->msgmap, SRT_VRB);
        pine_state->next_screen = pine_state->prev_screen;
        return;
    }

    if(quit != 'y'
       && F_OFF(F_QUIT_WO_CONFIRM,pine_state)
       && want_to(_("Really quit Alpine"), 'y', 0, NO_HELP, WT_NORM) != 'y'){
        pine_state->next_screen = pine_state->prev_screen;
        return;
    }

    goodnight_gracey(pine_state, 0);
}


/*----------------------------------------------------------------------
    The nuts and bolts of actually cleaning up and exiting pine

    Args: ps -- the usual pine structure, 
	  exit_val -- what to tell our parent

  Result: This never returns

  ----*/
void
goodnight_gracey(struct pine *pine_state, int exit_val)
{
    int   i, cnt_user_streams = 0;
    char *final_msg = NULL;
    char  msg[MAX_SCREEN_COLS+1];
    char *pf = _("Alpine finished");
    MAILSTREAM *m;
    extern KBESC_T *kbesc;

    dprint((2, "goodnight_gracey:\n"));    

    /* We want to do this here before we close up the streams */
    trim_remote_adrbks();

    for(i = 0; i < ps_global->s_pool.nstream; i++){
	m = ps_global->s_pool.streams[i];
	if(m && sp_flagged(m, SP_LOCKED) && sp_flagged(m, SP_USERFLDR))
	  cnt_user_streams++;
    }

    /* clean up open streams */

    if(pine_state->mail_stream
       && sp_flagged(pine_state->mail_stream, SP_LOCKED)
       && sp_flagged(pine_state->mail_stream, SP_USERFLDR)){
	dprint((5, "goodnight_gracey: close current stream\n"));    
	expunge_and_close(pine_state->mail_stream,
			  (cnt_user_streams <= 1) ? &final_msg : NULL, EC_NONE);
	cnt_user_streams--;
    }

    pine_state->mail_stream = NULL;
    pine_state->redrawer = (void (*)(void))NULL;

    dprint((5,
	    "goodnight_gracey: close other stream pool streams\n"));    
    for(i = 0; i < ps_global->s_pool.nstream; i++){
	m = ps_global->s_pool.streams[i];
        /* 
	 * fix global for functions that depend(ed) on it sort_folder.
	 * Hopefully those will get phased out.
	 */
	ps_global->mail_stream = m;
	if(m && sp_flagged(m, SP_LOCKED) && sp_flagged(m, SP_USERFLDR)
	   && !sp_flagged(m, SP_INBOX)){
	    sp_set_expunge_count(m, 0L);
	    expunge_and_close(m, (cnt_user_streams <= 1) ? &final_msg : NULL,
			      EC_NONE);
	    cnt_user_streams--;
	}
    }

    for(i = 0; i < ps_global->s_pool.nstream; i++){
	m = ps_global->s_pool.streams[i];
        /* 
	 * fix global for functions that depend(ed) on it (sort_folder).
	 * Hopefully those will get phased out.
	 */
	ps_global->mail_stream = m;
	if(m && sp_flagged(m, SP_LOCKED) && sp_flagged(m, SP_USERFLDR)
	   && sp_flagged(m, SP_INBOX)){
	    dprint((5,
		    "goodnight_gracey: close inbox stream stream\n"));    
	    sp_set_expunge_count(m, 0L);
	    expunge_and_close(m, (cnt_user_streams <= 1) ? &final_msg : NULL,
			      EC_NONE);
	    cnt_user_streams--;
	}
    }

#ifdef _WINDOWS
    if(ps_global->ttyo)
      (void)get_windsize(ps_global->ttyo);
#endif

    dprint((7, "goodnight_gracey: close config files\n"));    

    free_pinerc_strings(&pine_state);

    strncpy(msg, pf, sizeof(msg));
    msg[sizeof(msg)-1] = '\0';
    if(final_msg){
	strncat(msg, " -- ", sizeof(msg)-strlen(msg)-1);
	msg[sizeof(msg)-1] = '\0';
	strncat(msg, final_msg, sizeof(msg)-strlen(msg)-1);
	msg[sizeof(msg)-1] = '\0';
	fs_give((void **)&final_msg);
    }

    dprint((7, "goodnight_gracey: sp_end\n"));
    ps_global->noshow_error = 1;
    sp_end();

#ifdef SMIME
    smime_deinit();
#endif

    /* after sp_end, which might call a filter */
    completely_done_with_adrbks();

    dprint((7, "goodnight_gracey: end_screen\n"));
    end_screen(msg, exit_val);
    dprint((7, "goodnight_gracey: end_titlebar\n"));
    end_titlebar();
    dprint((7, "goodnight_gracey: end_keymenu\n"));
    end_keymenu();

    dprint((7, "goodnight_gracey: end_keyboard\n"));
    end_keyboard(F_ON(F_USE_FK,pine_state));
    dprint((7, "goodnight_gracey: end_ttydriver\n"));
    end_tty_driver(pine_state);
#if !defined(DOS) && !defined(OS2)
    kbdestroy(kbesc);
#if !defined(LEAVEOUTFIFO)
    close_newmailfifo();
#endif
#endif
    end_signals(0);
    if(filter_data_file(0))
      our_unlink(filter_data_file(0));

    imap_flush_passwd_cache(TRUE);
    free_newsgrp_cache();
    mailcap_free();
    close_every_pattern();
    free_extra_hdrs();
    free_contexts(&ps_global->context_list);
    free_charsetchecker();
    dprint((7, "goodnight_gracey: free more memory\n"));    
#ifdef	ENABLE_LDAP
    free_saved_query_parameters();
#endif

    html_dir_clean(1);		/* force remove of remaining files */
    free_pine_struct(&pine_state);

    free_histlist();

    free_alpine_module_globals();	/* should we have module globals? */
    free_pith_module_globals();
    free_pico_module_globals();
    free_c_client_module_globals();

#ifdef DEBUG
    if(debugfile){
	if(debug >= 2)
	  fputs("goodnight_gracey finished\n", debugfile);

	fclose(debugfile);
    }
#endif    

    exit(exit_val);
}


/*----------------------------------------------------------------------
  Call back for c-client to feed us back the progress of network reads

  Input: 

 Result: 
  ----*/
void
pine_read_progress(GETS_DATA *md, long unsigned int count)
{
    gets_bytes += count;			/* update counter */
}


/*----------------------------------------------------------------------
  Function to fish the current byte count from a c-client fetch.

  Input: reset -- flag telling us to reset the count

 Result: Returns the number of bytes read by the c-client so far
  ----*/
unsigned long
pine_gets_bytes(int reset)
{
    if(reset)
      gets_bytes = 0L;

    return(gets_bytes);
}


/*----------------------------------------------------------------------
    Panic pine - call on detected programmatic errors to exit pine

   Args: message -- message to record in debug file and to be printed for user

 Result: The various tty modes are restored
         If debugging is active a core dump will be generated
         Exits Alpine

  This is also called from imap routines and fs_get and fs_resize.
  ----*/
void
alpine_panic(char *message)
{
    char buf[256];

    /* global variable in .../pico/edef.h */
    in_panic = 1;

    if(ps_global->ttyo){
	end_screen(NULL, -1);
	end_keyboard(ps_global != NULL ? F_ON(F_USE_FK,ps_global) : 0);
	end_tty_driver(ps_global);
	end_signals(1);
    }
    if(filter_data_file(0))
      our_unlink(filter_data_file(0));

    dprint((1, "\n===========================================\n\n"));
    dprint((1, "   Alpine Panic: %s\n\n", message ? message : "?"));
    dprint((1, "===========================================\n\n"));

    /* intercept c-client "free storage" errors */
    if(strstr(message, "free storage"))
      snprintf(buf, sizeof(buf), _("No more available memory.\nAlpine Exiting"));
    else
      snprintf(buf, sizeof(buf), _("Problem detected: \"%s\".\nAlpine Exiting."), message);

    buf[sizeof(buf)-1] = '\0';

#ifdef _WINDOWS
    /* Put up a message box. */
    mswin_messagebox (buf, 1);
#else
    fprintf(stderr, "\n\n%s\n", buf);
#endif

#ifdef DEBUG
    if(debugfile){
	save_debug_on_crash(debugfile, recent_keystroke);
    }

    coredump();   /*--- If we're debugging get a core dump --*/
#endif

    exit(-1);
    fatal("ffo"); /* BUG -- hack to get fatal out of library in right order*/
}


/*
 * panicking - function to test whether or not we're exiting under stress.
 *             
 */
int
panicking(void)
{
    return(in_panic);
}


/*----------------------------------------------------------------------
    exceptional_exit - called to exit under unusual conditions (with no core)

   Args: message -- message to record in debug file and to be printed for user
	 ev -- exit value

  ----*/
void
exceptional_exit(char *message, int ev)
{
    fprintf(stderr, "%s\n", message);
    exit(ev);
}


/*
 *  PicoText Storage Object Support Routines
 */

STORE_S *
pine_pico_get(void)
{
    return((STORE_S *)pico_get());
}

int
pine_pico_give(STORE_S **sop)
{
    pico_give((void *)sop);
    return(1);
}

int
pine_pico_writec(int c, STORE_S *so)
{
    unsigned char ch = (unsigned char) c;

    return(pico_writec(so->txt, ch, PICOREADC_NONE));
}

int
pine_pico_writec_noucs(int c, STORE_S *so)
{
    unsigned char ch = (unsigned char) c;

    return(pico_writec(so->txt, ch, PICOREADC_NOUCS));
}

int
pine_pico_readc(unsigned char *c, STORE_S *so)
{
    return(pico_readc(so->txt, c, PICOREADC_NONE));
}

int
pine_pico_readc_noucs(unsigned char *c, STORE_S *so)
{
    return(pico_readc(so->txt, c, PICOREADC_NOUCS));
}

int
pine_pico_puts(STORE_S *so, char *s)
{
    return(pico_puts(so->txt, s, PICOREADC_NONE));
}

int
pine_pico_puts_noucs(STORE_S *so, char *s)
{
    return(pico_puts(so->txt, s, PICOREADC_NOUCS));
}

int
pine_pico_seek(STORE_S *so, long pos, int orig)
{
    return(pico_seek((void *)so, pos, orig));
}


int
remote_pinerc_failure(void)
{
#ifdef _WINDOWS
    if(ps_global->install_flag)  /* just exit silently */
      exit(0);
#endif /* _WINDOWS */

    if(ps_global->exit_if_no_pinerc){
	exceptional_exit("Exiting because -bail option is set and config file not readable.", -1);
    }

    if(want_to("Trouble reading remote configuration! Continue anyway ",
	       'n', 'n', NO_HELP, WT_FLUSH_IN) != 'y'){
	return(0);
    }

    return(1);
}


void
dump_supported_options(void)
{
    char **config;

    config = get_supported_options();
    if(config){
	display_args_err(NULL, config, 0);
	free_list_array(&config);
    }
}


/*----------------------------------------------------------------------
     Check pruned-folders for validity, making sure they are in the 
     same context as sent-mail.

  ----*/
int
prune_folders_ok(void)
{
    char **p;

    for(p = ps_global->VAR_PRUNED_FOLDERS; p && *p && **p; p++)
      if(!context_isambig(*p))
	return(0);

    return(1);
}

void  
free_alpine_module_globals(void)
{   
#ifdef  LOCAL_PASSWD_CACHE
    free_passfile_cache();
#endif
    free_message_queue();
    free_titlebar_globals();
}

#ifdef	WIN32
char *
pine_user_callback()
{
    if(ps_global->VAR_USER_ID && ps_global->VAR_USER_ID[0]){
	return(ps_global->VAR_USER_ID);
    }
    else{
	/* SHOULD PROMPT HERE! */
      return(NULL);
    }
}
#endif

#ifdef	_WINDOWS
/*
 * windows callback to get/set function keys mode state
 */
int
fkey_mode_callback(set, args)
    int  set;
    long args;
{
    return(F_ON(F_USE_FK, ps_global) != 0);
}


void
imap_telemetry_on()
{
    if(ps_global->mail_stream)
      mail_debug(ps_global->mail_stream);
}


void
imap_telemetry_off()
{
    if(ps_global->mail_stream)
      mail_nodebug(ps_global->mail_stream);
}


char *
pcpine_help_main(title)
    char *title;
{
    if(title)
      strncpy(title, _("PC-Alpine MAIN MENU Help"), 256);

    return(pcpine_help(main_menu_tx));
}


int
pcpine_main_cursor(col, row)
    int  col;
    long row;
{
    unsigned ndmi;

    if (row >= (HEADER_ROWS(ps_global) + MNSKIP(ps_global)))
      ndmi = (row+1 - HEADER_ROWS(ps_global) - (MNSKIP(ps_global)+1))/(MNSKIP(ps_global)+1);

    if (row >= (HEADER_ROWS(ps_global) + MNSKIP(ps_global))
	&& !(MNSKIP(ps_global) && (row+1) & 0x01)
	&& ndmi <= MAX_MENU_ITEM
	&& FOOTER_ROWS(ps_global) + (ndmi+1)*(MNSKIP(ps_global)+1)
	    + MNSKIP(ps_global) + FOOTER_ROWS(ps_global) <= ps_global->ttyo->screen_rows)
      return(MSWIN_CURSOR_HAND);
    else
      return(MSWIN_CURSOR_ARROW);
}
#endif /* _WINDOWS */