1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
|
/*
* $LynxId: LYUtils.c,v 1.292 2018/05/15 21:20:52 tom Exp $
*/
#include <HTUtils.h>
#include <HTTCP.h>
#include <HTParse.h>
#include <HTAccess.h>
#include <HTCJK.h>
#include <HTAlert.h>
#if defined(__MINGW32__)
extern int kbhit(void); /* FIXME: use conio.h */
#undef UNIX
#elif defined(_WINDOWS)
#ifdef DONT_USE_GETTEXT
#undef gettext
#elif defined(HAVE_GETTEXT)
#undef gettext
#define gettext conio_gettext
#else
#undef gettext
#endif
#include <conio.h>
#ifdef DONT_USE_GETTEXT
#define gettext(s) s
#elif defined(HAVE_GETTEXT)
#undef gettext
#ifdef _INTL_REDIRECT_MACROS
#define gettext libintl_gettext /* restore definition from libintl.h */
#endif
#else
#undef gettext
#define gettext(s) s
#endif
#if !defined(kbhit) && defined(_WCONIO_DEFINED)
#define kbhit() _kbhit() /* reasonably recent conio.h */
#endif
#elif defined(__minix)
#include <termios.h> /* for struct winsize */
#endif /* __MINGW32__ */
#include <LYCurses.h>
#include <LYHistory.h>
#include <LYStrings.h>
#include <LYGlobalDefs.h>
#include <LYUtils.h>
#include <LYSignal.h>
#include <GridText.h>
#include <LYClean.h>
#include <LYCharSets.h>
#include <LYCharUtils.h>
#include <LYMainLoop.h>
#include <LYKeymap.h>
#ifdef __DJGPP__
#include <go32.h>
#include <sys/exceptn.h>
#endif /* __DJGPP__ */
#ifndef NO_GROUPS
#include <HTFile.h>
#endif
#ifdef _WINDOWS /* 1998/04/30 (Thu) 19:04:25 */
#define GETPID() (unsigned) (getpid() & 0xffff)
#else
#define GETPID() (unsigned) getpid()
#endif /* _WINDOWS */
#ifdef FNAMES_8_3
#define PID_FMT "%04x"
#else
#define PID_FMT "%u"
#endif
#ifdef DJGPP_KEYHANDLER
#include <bios.h>
#endif /* DJGPP_KEYHANDLER */
#ifdef __EMX__
# define BOOLEAN OS2_BOOLEAN /* Conflicts, but is used */
# undef HT_ERROR /* Conflicts too */
# define INCL_PM /* I want some PM functions.. */
# define INCL_DOSPROCESS /* TIB PIB. */
# include <os2.h>
# undef BOOLEAN
#endif
#ifdef VMS
#include <descrip.h>
#include <libclidef.h>
#include <lib$routines.h>
#endif /* VMS */
#ifdef HAVE_UTMP
#include <pwd.h>
#ifdef UTMPX_FOR_UTMP
#include <utmpx.h>
#define utmp utmpx
#ifdef UTMPX_FILE
#ifdef UTMP_FILE
#undef UTMP_FILE
#endif /* UTMP_FILE */
#define UTMP_FILE UTMPX_FILE
#else
#ifdef __UTMPX_FILE
#define UTMP_FILE __UTMPX_FILE /* at least in OS/390 S/390 -- gil -- 2100 */
#else
#ifndef UTMP_FILE
#define UTMP_FILE "/var/adm/utmpx" /* Digital Unix 4.0 */
#endif
#endif
#endif /* UTMPX_FILE */
#else
#include <utmp.h>
#endif /* UTMPX_FOR_UTMP */
#endif /* HAVE_UTMP */
#ifdef NEED_PTEM_H
/* they neglected to define struct winsize in termios.h -- it's only in
* termio.h and ptem.h (the former conflicts with other definitions).
*/
#include <sys/stream.h>
#include <sys/ptem.h>
#endif
#include <LYLeaks.h>
#ifdef USE_COLOR_STYLE
#include <AttrList.h>
#include <LYHash.h>
#include <LYStyle.h>
#endif
#ifdef SVR4_BSDSELECT
extern int BSDselect(int nfds, fd_set * readfds, fd_set * writefds,
fd_set * exceptfds, struct timeval *timeout);
#ifdef select
#undef select
#endif /* select */
#define select BSDselect
#ifdef SOCKS
#ifdef Rselect
#undef Rselect
#endif /* Rselect */
#define Rselect BSDselect
#endif /* SOCKS */
#endif /* SVR4_BSDSELECT */
#ifdef __DJGPP__
#undef select /* defined to select_s in www_tcp.h */
#endif
#ifndef UTMP_FILE
#if defined(__FreeBSD__) || defined(__bsdi__)
#define UTMP_FILE _PATH_UTMP
#else
#define UTMP_FILE "/etc/utmp"
#endif /* __FreeBSD__ || __bsdi__ */
#endif /* !UTMP_FILE */
/*
* experimental - make temporary filenames random to make the scheme less
* obvious. However, as noted by KW, there are instances (such as the
* 'O'ption page, for which Lynx will store a temporary filename even when
* it no longer applies, since it will reuse that filename at a later time.
*/
#ifdef USE_RAND_TEMPNAME
#if defined(LYNX_RAND_MAX)
#define HAVE_RAND_TEMPNAME 1
#define MAX_TEMPNAME 10000
#ifndef BITS_PER_CHAR
#define BITS_PER_CHAR 8
#endif
#endif
#endif
#define COPY_COMMAND "%s %s %s"
static HTList *localhost_aliases = NULL; /* Hosts to treat as local */
static char *HomeDir = NULL; /* HOME directory */
HTList *sug_filenames = NULL; /* Suggested filenames */
/*
* Maintain a list of all of the temp-files we create so that we can remove
* them during the cleanup.
*/
typedef struct _LYTemp {
struct _LYTemp *next;
char *name;
BOOLEAN outs;
FILE *file;
} LY_TEMP;
static LY_TEMP *ly_temp;
static LY_TEMP *FindTempfileByName(const char *name)
{
LY_TEMP *p;
for (p = ly_temp; p != 0; p = p->next) {
if (!strcmp(p->name, name)) {
break;
}
}
return p;
}
static LY_TEMP *FindTempfileByFP(FILE *fp)
{
LY_TEMP *p;
for (p = ly_temp; p != 0; p = p->next) {
if (p->file == fp) {
break;
}
}
return p;
}
#if defined(_WIN32)
/*
* Use RegQueryValueExA() rather than RegQueryValueEx() for compatibility
* with non-Unicode winvile
*/
static int w32_get_reg_sz(HKEY hkey, const char *name, char *value, unsigned length)
{
int result;
DWORD dwSzBuffer = length;
CTRACE((tfp, "w32_get_reg_sz(%s)\n", name));
result = RegQueryValueExA(hkey,
name,
NULL,
NULL,
(LPBYTE) value,
&dwSzBuffer);
if (result == ERROR_SUCCESS) {
value[dwSzBuffer] = 0;
CTRACE((tfp, "->%s\n", value));
}
return result;
}
static char *w32_get_shell_folder(const char *name)
{
static HKEY rootkey = HKEY_CURRENT_USER;
char *result = 0;
HKEY hkey;
char buffer[LY_MAXPATH];
if (RegOpenKeyEx(rootkey,
W32_STRING("Software"
"\\Microsoft"
"\\Windows"
"\\CurrentVersion"
"\\Explorer"
"\\Shell Folders"),
0,
KEY_READ,
&hkey) == ERROR_SUCCESS) {
if (w32_get_reg_sz(hkey, name, buffer, sizeof(buffer)) == ERROR_SUCCESS) {
result = strdup(buffer);
(void) RegCloseKey(hkey);
}
(void) RegCloseKey(hkey);
}
return non_empty(result) ? result : 0;
}
#endif
/*
* Get an environment variable, rejecting empty strings
*/
char *LYGetEnv(const char *name)
{
char *result = getenv(name);
#if defined(_WIN32)
if (result == 0) {
static HKEY rootkeys[] =
{HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE};
int j;
HKEY hkey;
char buffer[256];
for (j = 0; j < (int) TABLESIZE(rootkeys); ++j) {
if (RegOpenKeyEx(rootkeys[j],
LYNX_SUBKEY W32_STRING("\\Environment"),
0,
KEY_READ,
&hkey) == ERROR_SUCCESS) {
if (w32_get_reg_sz(hkey, name, buffer, sizeof(buffer)) == ERROR_SUCCESS) {
result = strdup(buffer);
(void) RegCloseKey(hkey);
break;
}
(void) RegCloseKey(hkey);
}
}
}
#endif
return non_empty(result) ? result : 0;
}
/*
* ascii versions of locale sensitive functions needed because in
* Turkish locales tolower("I") is not "i". That's fatal for case
* sensitive operations with charset names, HTML tags etc.
*/
#ifdef USE_ASCII_CTYPES
int ascii_tolower(int i)
{
if (91 > i && i > 64)
return (i + 32);
else
return i;
}
int ascii_toupper(int i)
{
if (123 > i && i > 96)
return (i - 32);
else
return i;
}
int ascii_isupper(int i)
{
if (91 > i && i > 64)
return 1;
else
return 0;
}
#endif /* USE_ASCII_CTYPES */
/*
* Check for UTF-8 data, returning the length past the first character.
* Return zero if we found an ordinary character rather than UTF-8.
*/
size_t utf8_length(int utf_flag,
const char *data)
{
size_t utf_extra = 0;
if (utf_flag && is8bits(*data)) {
if ((*data & 0xe0) == 0xc0) {
utf_extra = 1;
} else if ((*data & 0xf0) == 0xe0) {
utf_extra = 2;
} else if ((*data & 0xf8) == 0xf0) {
utf_extra = 3;
} else if ((*data & 0xfc) == 0xf8) {
utf_extra = 4;
} else if ((*data & 0xfe) == 0xfc) {
utf_extra = 5;
} else {
/*
* Garbage.
*/
utf_extra = 0;
}
if (strlen(data + 1) < utf_extra) {
/*
* Shouldn't happen.
*/
utf_extra = 0;
}
}
return utf_extra;
}
/*
* Free storage used for the link-highlighting.
*/
void LYFreeHilites(int first, int last)
{
int i;
for (i = first; i < last; i++) {
LYSetHilite(i, NULL);
FREE(links[i].lname);
}
}
#define LXP (links[cur].lx)
#define LYP (links[cur].ly)
/*
* Set the initial highlight information for a given link.
*/
void LYSetHilite(int cur,
const char *text)
{
links[cur].list.hl_base.hl_text = DeConst(text);
links[cur].list.hl_len = (short) ((text != NULL) ? 1 : 0);
FREE(links[cur].list.hl_info);
}
/*
* Add highlight information for the next line of a link.
*/
void LYAddHilite(int cur,
char *text,
int x)
{
HiliteList *list = &(links[cur].list);
HiliteInfo *have = list->hl_info;
size_t need = (unsigned) (list->hl_len - 1);
size_t want;
list->hl_len = (short) (list->hl_len + 1);
want = (size_t) list->hl_len;
if (have != NULL) {
have = typeRealloc(HiliteInfo, have, want);
} else {
have = typeMallocn(HiliteInfo, want);
}
list->hl_info = have;
have[need].hl_text = text;
have[need].hl_x = (short) x;
}
/*
* Get the highlight text, counting from zero.
*/
const char *LYGetHiliteStr(int cur,
int count)
{
const char *result;
if (count >= links[cur].list.hl_len)
result = NULL;
else if (count > 0)
result = links[cur].list.hl_info[count - 1].hl_text;
else
result = links[cur].list.hl_base.hl_text;
return result;
}
/*
* Get the X-ordinate at which to draw the corresponding highlight-text
*/
int LYGetHilitePos(int cur,
int count)
{
int result;
if (count >= links[cur].list.hl_len)
result = -1;
else if (count > 0)
result = links[cur].list.hl_info[count - 1].hl_x;
else
result = LXP;
return result;
}
#ifdef SHOW_WHEREIS_TARGETS
#define SKIP_GLYPHS(theFlag, theData, theOffset) \
(theFlag \
? LYmbcs_skip_glyphs(theData, (theOffset), theFlag) \
: (theData + (theOffset)))
/*
* If we have an emphasized WHEREIS hit in the highlighted text, restore the
* emphasis. Note that we never emphasize the first and last characters of the
* highlighted text when we are making the link current, so the link attributes
* for the current link will persist at the beginning and end, providing an
* indication to the user that it has been made current. Also note that we use
* HText_getFirstTargetInLine() to determine if there's a hit in the HText
* structure line containing the link, and if so, get back a copy of the line
* starting at that first hit (which might be before or after our link), and
* with all IsSpecial characters stripped, so we don't need to deal with them
* here. -FM
*/
static BOOL show_whereis_targets(int flag,
int cur,
int count,
const char *target,
int TargetEmphasisON,
int utf_flag)
{
const char *mydata = NULL;
const char *cp;
char *theData = NULL;
char buffer[MAX_LINE];
char tmp[7];
int HitOffset;
int LenNeeded;
int Offset;
int tLen;
tmp[0] = tmp[1] = tmp[2] = '\0';
if (non_empty(target)
&& (links[cur].type & WWW_LINK_TYPE)
&& non_empty(LYGetHiliteStr(cur, count))
&& LYP + count < display_lines
&& HText_getFirstTargetInLine(HTMainText,
links[cur].anchor_line_num + count,
utf_flag,
&Offset,
&tLen,
&theData,
target)) {
int itmp, written, len, y, offset;
const char *data;
int tlen = (int) strlen(target);
int hlen, hLen;
int hLine = LYP + count;
int hoffset = LYGetHilitePos(cur, count);
size_t utf_extra = 0;
/*
* Copy into the buffer only what will fit up to the right border of
* the screen. -FM
*/
LYmbcsstrncpy(buffer,
NonNull(LYGetHiliteStr(cur, count)),
(int) (sizeof(buffer) - 1),
(LYcolLimit - LYGetHilitePos(cur, count)),
utf_flag);
hlen = (int) strlen(buffer);
hLen = ((IS_CJK_TTY || utf_flag) ?
LYmbcsstrlen(buffer, utf_flag, YES) : hlen);
/*
* Break out if the first hit in the line starts after this link. -FM
*/
if (Offset < (hoffset + hLen)) {
/*
* Recursively skip hits that end before this link, and break out
* if there is no hit beyond those. -FM
*/
mydata = theData;
while ((Offset < hoffset) &&
((Offset + tLen) <= hoffset)) {
data = (mydata + tlen);
offset = (Offset + tLen);
if (((cp = LYno_attr_mb_strstr(data,
target,
utf_flag, YES,
&HitOffset,
&LenNeeded)) != NULL)
&& (offset + LenNeeded) < LYcols) {
mydata = cp;
Offset = (offset + HitOffset);
} else {
goto highlight_search_done;
}
}
data = buffer;
offset = hoffset;
/*
* If the hit starts before the hightext, and ends in or beyond the
* hightext, restore the emphasis, skipping the first and last
* characters of the hightext if we're making the link current.
* -FM
*/
if (offset >= 0 &&
(Offset < offset) &&
((Offset + tLen) > offset)) {
itmp = 0;
written = 0;
len = (tlen - (offset - Offset));
/*
* Go to the start of the hightext and handle its first
* character. -FM
*/
LYmove(hLine, offset);
tmp[0] = data[itmp];
utf_extra = utf8_length(utf_flag, data + itmp);
if (utf_extra) {
LYStrNCpy(&tmp[1], &data[itmp + 1], utf_extra);
itmp += (int) utf_extra;
/*
* Start emphasis immediately if we are making the link
* non-current. -FM
*/
if (flag != TRUE) {
LYstartTargetEmphasis();
TargetEmphasisON = TRUE;
LYaddstr(tmp);
} else {
LYmove(hLine, (offset + 1));
}
tmp[1] = '\0';
written += (int) (utf_extra + 1);
} else if (IS_CJK_TTY && is8bits(tmp[0])) {
/*
* For CJK strings, by Masanobu Kimura.
*/
tmp[1] = data[++itmp];
/*
* Start emphasis immediately if we are making the link
* non-current. -FM
*/
if (flag != TRUE) {
LYstartTargetEmphasis();
TargetEmphasisON = TRUE;
LYaddstr(tmp);
} else {
LYmove(hLine, (offset + 1));
}
tmp[1] = '\0';
written += 2;
} else {
/*
* Start emphasis immediately if we are making the link
* non-current. -FM
*/
if (flag != TRUE) {
LYstartTargetEmphasis();
TargetEmphasisON = TRUE;
LYaddstr(tmp);
} else {
LYmove(hLine, (offset + 1));
}
written++;
}
itmp++;
/*
* Start emphasis after the first character if we are making
* the link current and this is not the last character. -FM
*/
if (!TargetEmphasisON &&
data[itmp] != '\0') {
LYstartTargetEmphasis();
TargetEmphasisON = TRUE;
}
/*
* Handle the remaining characters. -FM
*/
for (;
written < len && (tmp[0] = data[itmp]) != '\0';
itmp++) {
/*
* Print all the other target chars, except the last
* character if it is also the last character of hightext
* and we are making the link current. -FM
*/
utf_extra = utf8_length(utf_flag, data + itmp);
if (utf_extra) {
LYStrNCpy(&tmp[1], &data[itmp + 1], utf_extra);
itmp += (int) utf_extra;
/*
* Make sure we don't restore emphasis to the last
* character of hightext if we are making the link
* current. -FM
*/
if (flag == TRUE && data[(itmp + 1)] == '\0') {
LYstopTargetEmphasis();
TargetEmphasisON = FALSE;
LYGetYX(y, offset);
(void) y;
LYmove(hLine, (offset + 1));
} else {
LYaddstr(tmp);
}
tmp[1] = '\0';
written += (int) (utf_extra + 1);
} else if (IS_CJK_TTY && is8bits(tmp[0])) {
/*
* For CJK strings, by Masanobu Kimura.
*/
tmp[1] = data[++itmp];
/*
* Make sure we don't restore emphasis to the last
* character of hightext if we are making the link
* current. -FM
*/
if (flag == TRUE && data[(itmp + 1)] == '\0') {
LYstopTargetEmphasis();
TargetEmphasisON = FALSE;
LYGetYX(y, offset);
LYmove(hLine, (offset + 1));
} else {
LYaddstr(tmp);
}
tmp[1] = '\0';
written += 2;
} else {
/*
* Make sure we don't restore emphasis to the last
* character of hightext if we are making the link
* current. -FM
*/
if (flag == TRUE && data[(itmp + 1)] == '\0') {
LYstopTargetEmphasis();
TargetEmphasisON = FALSE;
LYGetYX(y, offset);
LYmove(hLine, (offset + 1));
} else {
LYaddstr(tmp);
}
written++;
}
}
/*
* Stop the emphasis if we haven't already, then reset the
* offset to our current position in the line, and if that is
* beyond the link, or or we are making the link current and it
* is the last character of the hightext, we are done. -FM
*/
if (TargetEmphasisON) {
LYstopTargetEmphasis();
TargetEmphasisON = FALSE;
}
LYGetYX(y, offset);
if (offset < (hoffset + (flag == TRUE ? (hLen - 1) : hLen))
/*
* See if we have another hit that starts within the
* hightext. -FM
*/
&& ((cp =
LYno_attr_mb_strstr(data = SKIP_GLYPHS(utf_flag,
mydata,
offset - Offset),
target,
utf_flag, YES,
&HitOffset,
&LenNeeded)) != NULL)
&& (offset + LenNeeded) < LYcols
/*
* If the hit starts after the end of the hightext, or we
* are making the link current and the hit starts at its
* last character, we are done. -FM
*/
&& (HitOffset + offset) <
(hoffset +
(flag == TRUE ? (hLen - 1) : hLen))) {
/*
* Set up the data and offset for the hit, and let the code
* for within hightext hits handle it. -FM
*/
mydata = cp;
Offset = (offset + HitOffset);
data = buffer;
offset = hoffset;
goto highlight_hit_within_hightext;
}
goto highlight_search_done;
}
highlight_hit_within_hightext:
/*
* If we get to here, the hit starts within the hightext. If we
* are making the link current and it's the last character in the
* hightext, we are done. Otherwise, move there and start
* restoring the emphasis. -FM
*/
if ((Offset - offset) <= (flag == TRUE ? (hLen - 1) : hLen)) {
data = SKIP_GLYPHS(utf_flag, data, Offset - offset);
if (utf_flag) {
LYrefresh();
}
offset = Offset;
itmp = 0;
written = 0;
len = tlen;
/*
* Go to the start of the hit and handle its first character.
* -FM
*/
LYmove(hLine, offset);
tmp[0] = data[itmp];
utf_extra = utf8_length(utf_flag, data + itmp);
if (utf_extra) {
LYStrNCpy(&tmp[1], &data[itmp + 1], utf_extra);
itmp += (int) utf_extra;
/*
* Start emphasis immediately if we are making the link
* non-current, or we are making it current but this is not
* the first or last character of the hightext. -FM
*/
if (flag != TRUE ||
(offset > hoffset && data[itmp + 1] != '\0')) {
LYstartTargetEmphasis();
TargetEmphasisON = TRUE;
LYaddstr(tmp);
} else {
LYmove(hLine, (offset + 1));
}
tmp[1] = '\0';
written += (int) (utf_extra + 1);
} else if (IS_CJK_TTY && is8bits(tmp[0])) {
/*
* For CJK strings, by Masanobu Kimura.
*/
tmp[1] = data[++itmp];
/*
* Start emphasis immediately if we are making the link
* non-current, or we are making it current but this is not
* the first or last character of the hightext. -FM
*/
if (flag != TRUE ||
(offset > hoffset && data[itmp + 1] != '\0')) {
LYstartTargetEmphasis();
TargetEmphasisON = TRUE;
LYaddstr(tmp);
} else {
LYmove(hLine, (offset + 2));
}
tmp[1] = '\0';
written += 2;
} else {
/*
* Start emphasis immediately if we are making the link
* non-current, or we are making it current but this is not
* the first or last character of the hightext. -FM
*/
if (flag != TRUE ||
(offset > hoffset && data[itmp + 1] != '\0')) {
LYstartTargetEmphasis();
TargetEmphasisON = TRUE;
LYaddstr(tmp);
} else {
LYmove(hLine, (offset + 1));
}
written++;
}
itmp++;
/*
* Start emphasis after the first character if we are making
* the link current and this is not the last character. -FM
*/
if (!TargetEmphasisON &&
data[itmp] != '\0') {
LYstartTargetEmphasis();
TargetEmphasisON = TRUE;
}
for (;
written < len && (tmp[0] = data[itmp]) != '\0';
itmp++) {
/*
* Print all the other target chars, except the last
* character if it is also the last character of hightext
* and we are making the link current. -FM
*/
utf_extra = utf8_length(utf_flag, data + itmp);
if (utf_extra) {
LYStrNCpy(&tmp[1], &data[itmp + 1], utf_extra);
itmp += (int) utf_extra;
/*
* Make sure we don't restore emphasis to the last
* character of hightext if we are making the link
* current. -FM
*/
if (flag == TRUE && data[(itmp + 1)] == '\0') {
LYstopTargetEmphasis();
TargetEmphasisON = FALSE;
LYGetYX(y, offset);
LYmove(hLine, (offset + 1));
} else {
LYaddstr(tmp);
}
tmp[1] = '\0';
written += (int) (utf_extra + 1);
} else if (IS_CJK_TTY && is8bits(tmp[0])) {
/*
* For CJK strings, by Masanobu Kimura.
*/
tmp[1] = data[++itmp];
/*
* Make sure we don't restore emphasis to the last
* character of hightext if we are making the link
* current. -FM
*/
if (flag == TRUE && data[(itmp + 1)] == '\0') {
LYstopTargetEmphasis();
TargetEmphasisON = FALSE;
LYGetYX(y, offset);
LYmove(hLine, (offset + 1));
} else {
LYaddstr(tmp);
}
tmp[1] = '\0';
written += 2;
} else {
/*
* Make sure we don't restore emphasis to the last
* character of hightext if we are making the link
* current. -FM
*/
if (flag == TRUE && data[(itmp + 1)] == '\0') {
LYstopTargetEmphasis();
TargetEmphasisON = FALSE;
LYGetYX(y, offset);
LYmove(hLine, (offset + 1));
} else {
LYaddstr(tmp);
}
written++;
}
}
/*
* Stop the emphasis if we haven't already, then reset the
* offset to our current position in the line, and if that is
* beyond the link, or we are making the link current and it is
* the last character in the hightext, we are done. -FM
*/
if (TargetEmphasisON) {
LYstopTargetEmphasis();
TargetEmphasisON = FALSE;
}
LYGetYX(y, offset);
if (offset < (hoffset + (flag == TRUE ? (hLen - 1) : hLen))
/*
* See if we have another hit that starts within the
* hightext. -FM
*/
&& ((cp =
LYno_attr_mb_strstr(data = SKIP_GLYPHS(utf_flag,
mydata,
offset - Offset),
target,
utf_flag, YES,
&HitOffset,
&LenNeeded)) != NULL)
&& (offset + LenNeeded) < LYcols
/*
* If the hit starts after the end of the hightext, or we
* are making the link current and the hit starts at its
* last character, we are done. -FM
*/
&& (HitOffset + offset) <
(hoffset + (flag == TRUE ? (hLen - 1) : hLen))) {
/*
* If the target extends beyond our buffer, emphasize
* everything in the hightext starting at this hit.
* Otherwise, set up the data and offsets, and loop back.
* -FM
*/
if ((HitOffset + (offset + tLen)) >= (hoffset + hLen)) {
offset = (HitOffset + offset);
data = SKIP_GLYPHS(utf_flag, mydata, offset - hoffset);
if (utf_flag) {
LYrefresh();
}
LYmove(hLine, offset);
itmp = 0;
written = 0;
len = (int) strlen(data);
/*
* Turn the emphasis back on. -FM
*/
LYstartTargetEmphasis();
TargetEmphasisON = TRUE;
for (;
written < len && (tmp[0] = data[itmp]) != '\0';
itmp++) {
/*
* Print all the other target chars, except the
* last character if it is also the last character
* of hightext and we are making the link current.
* -FM
*/
utf_extra = utf8_length(utf_flag, data + itmp);
if (utf_extra) {
LYStrNCpy(&tmp[1], &data[itmp + 1], utf_extra);
itmp += (int) utf_extra;
/*
* Make sure we don't restore emphasis to the
* last character of hightext if we are making
* the link current. -FM
*/
if (flag == TRUE && data[(itmp + 1)] == '\0') {
LYstopTargetEmphasis();
TargetEmphasisON = FALSE;
LYGetYX(y, offset);
LYmove(hLine, (offset + 1));
} else {
LYaddstr(tmp);
}
tmp[1] = '\0';
written += (int) (utf_extra + 1);
} else if (IS_CJK_TTY && is8bits(tmp[0])) {
/*
* For CJK strings, by Masanobu Kimura.
*/
tmp[1] = data[++itmp];
/*
* Make sure we don't restore emphasis to the
* last character of hightext if we are making
* the link current. -FM
*/
if (flag == TRUE && data[(itmp + 1)] == '\0') {
LYstopTargetEmphasis();
TargetEmphasisON = FALSE;
} else {
LYaddstr(tmp);
}
tmp[1] = '\0';
written += 2;
} else {
/*
* Make sure we don't restore emphasis to the
* last character of hightext if we are making
* the link current. -FM
*/
if (flag == TRUE && data[(itmp + 1)] == '\0') {
LYstopTargetEmphasis();
TargetEmphasisON = FALSE;
} else {
LYaddstr(tmp);
}
written++;
}
}
/*
* Turn off the emphasis if we haven't already, and
* then we're done. -FM
*/
if (TargetEmphasisON) {
LYstopTargetEmphasis();
}
} else {
mydata = cp;
Offset = (offset + HitOffset);
data = buffer;
offset = hoffset;
goto highlight_hit_within_hightext;
}
}
}
}
}
highlight_search_done:
FREE(theData);
return (BOOLEAN) TargetEmphasisON;
}
#endif /* SHOW_WHEREIS_TARGETS */
#ifdef USE_COLOR_STYLE
static int find_cached_style(int cur,
int flag)
{
int s = s_alink;
#ifdef TEXTFIELDS_MAY_NEED_ACTIVATION
if (textfields_need_activation
&& links[cur].type == WWW_FORM_LINK_TYPE
&& F_TEXTLIKE(links[cur].l_form->type))
s = s_curedit;
#endif
if (flag != TRUE) {
int x;
/*
* This is where we try to restore the original style when a link is
* unhighlighted. The cached styles array saves the original style
* just for this case. If it doesn't have a color change saved at just
* the right position, we look at preceding positions in the same line
* until we find one.
*/
if (ValidCachedStyle(LYP, LXP)) {
CTRACE2(TRACE_STYLE,
(tfp, "STYLE.highlight.off: cached style @(%d,%d): ",
LYP, LXP));
s = (int) GetCachedStyle(LYP, LXP);
if (s == 0) {
for (x = LXP - 1; x >= 0; x--) {
s = (int) GetCachedStyle(LYP, x);
if (s != 0) {
SetCachedStyle(LYP, LXP, (unsigned) s);
CTRACE2(TRACE_STYLE,
(tfp, "found %d, x_offset=%d.\n", s, x - LXP));
break;
}
}
if (s == 0) {
CTRACE2(TRACE_STYLE, (tfp, "not found, assume <a>.\n"));
s = s_a;
}
} else {
CTRACE2(TRACE_STYLE, (tfp, "found %d.\n", s));
}
} else {
CTRACE2(TRACE_STYLE,
(tfp, "STYLE.highlight.off: can't use cache.\n"));
s = s_a;
}
} else {
CTRACE2(TRACE_STYLE, (tfp, "STYLE.highlight.on: @(%d,%d).\n", LYP, LXP));
}
return s;
}
#endif /* USE_COLOR_STYLE */
/*
* Highlight (or unhighlight) a given link.
*/
void LYhighlight(int flag,
int cur,
const char *target)
{
char buffer[MAX_LINE];
int i;
int hi_count;
int hi_offset;
int title_adjust = (no_title ? -TITLE_LINES : 0);
char tmp[7];
const char *hi_string;
#ifdef SHOW_WHEREIS_TARGETS
BOOL TargetEmphasisON = FALSE;
BOOL target1_drawn = NO;
#endif
BOOL utf_flag = (BOOL) IS_UTF8_TTY;
BOOL hl1_drawn = NO;
tmp[0] = tmp[1] = tmp[2] = '\0';
/*
* Bugs in the history code might cause -1 to be sent for cur, which yields
* a crash when LYStrNCpy() is called with a nonsense pointer. As far as I
* know, such bugs have been squashed, but if they should reappear, this
* works around them. -FM
*/
if (cur < 0) {
CTRACE((tfp, "LYhighlight cur %d (bug workaround)\n", cur));
cur = 0;
}
CTRACE((tfp, "LYhighlight at(%2d,%2d) %s %d [%d]:%s\n",
links[cur].ly, links[cur].lx,
(flag
? "on"
: "off"),
cur,
links[cur].anchor_number,
NONNULL(target)));
#if defined(TEXTFIELDS_MAY_NEED_ACTIVATION) && defined(INACTIVE_INPUT_STYLE_VH)
if (flag == FALSE)
textinput_redrawn = FALSE;
#endif
if (nlinks > 0) {
#ifdef USE_COLOR_STYLE
if (flag == TRUE || links[cur].type == WWW_FORM_LINK_TYPE) {
LYmove(LYP + title_adjust, LXP);
LynxChangeStyle(find_cached_style(cur, flag), STACK_ON);
}
#else
if (links[cur].type == WWW_FORM_LINK_TYPE
|| LYGetHiliteStr(cur, 0) == NULL) {
LYMoveToLink(cur, target, NULL,
flag, links[cur].inUnderline, utf_flag);
lynx_start_link_color(flag == TRUE, links[cur].inUnderline);
} else {
LYMoveToLink(cur, target, LYGetHiliteStr(cur, 0),
flag, links[cur].inUnderline, utf_flag);
hl1_drawn = YES;
#ifdef SHOW_WHEREIS_TARGETS
target1_drawn = YES;
#endif
}
#endif
if (links[cur].type == WWW_FORM_LINK_TYPE) {
int len;
int avail_space = (LYcolLimit - LXP) + (LYcolLimit * (LYlines - LYP));
const char *text = LYGetHiliteStr(cur, 0);
if (text == 0)
text = "";
if (avail_space > links[cur].l_form->size)
avail_space = links[cur].l_form->size;
len = (int) (LYmbcs_skip_cells(text, avail_space, utf_flag) - text);
LYwaddnstr(LYwin, text, (size_t) len);
while (len++ < avail_space)
LYaddch('_');
#ifdef USE_COLOR_STYLE
} else if (flag == FALSE) {
redraw_lines_of_link(cur);
CTRACE2(TRACE_STYLE,
(tfp, "STYLE.highlight.off: NOFIX branch @(%d,%d).\n",
LYP, LXP));
#endif
} else if (!hl1_drawn) {
/*
* Copy into the buffer only what will fit within the width of the
* screen.
*/
LYmbcsstrncpy(buffer,
NonNull(LYGetHiliteStr(cur, 0)),
(int) (sizeof(buffer) - 1),
(LYcolLimit - LXP),
utf_flag);
LYaddstr(buffer);
}
/*
* Display a second line as well.
*/
for (hi_count = 1;
(hi_string = LYGetHiliteStr(cur, hi_count)) != NULL
&& LYP + hi_count <= display_lines;
++hi_count) {
int row = LYP + hi_count + title_adjust;
hi_offset = LYGetHilitePos(cur, hi_count);
if (hi_offset < 0)
continue;
lynx_stop_link_color(flag == TRUE, links[cur].inUnderline);
LYmove(row, hi_offset);
#ifdef USE_COLOR_STYLE
CTRACE2(TRACE_STYLE,
(tfp, "STYLE.highlight.line2: @(%d,%d), style=%d.\n",
row, hi_offset,
flag == TRUE ? s_alink : s_a));
LynxChangeStyle(flag == TRUE ? s_alink : s_a, ABS_ON);
#else
lynx_start_link_color(flag == TRUE, links[cur].inUnderline);
#endif
for (i = 0; (tmp[0] = hi_string[i]) != '\0'
&& (i + hi_offset) < LYcols; i++) {
if (!IsSpecialAttrChar(hi_string[i])) {
/*
* For CJK strings, by Masanobu Kimura.
*/
if (IS_CJK_TTY && is8bits(tmp[0])) {
tmp[1] = hi_string[++i];
LYaddstr(tmp);
tmp[1] = '\0';
} else {
LYaddstr(tmp);
}
}
}
}
lynx_stop_link_color(flag == TRUE, links[cur].inUnderline);
#ifdef SHOW_WHEREIS_TARGETS
for (hi_count = target1_drawn ? 1 : 0;
LYGetHiliteStr(cur, hi_count) != NULL;
hi_count++) {
TargetEmphasisON = show_whereis_targets(flag,
cur,
hi_count,
target,
TargetEmphasisON,
utf_flag);
}
if (!LYShowCursor)
/*
* Get cursor out of the way.
*/
LYHideCursor();
else
#endif /* SHOW_WHEREIS_TARGETS */
/*
* Never hide the cursor if there's no FANCY CURSES or SLANG.
*/
LYmove(LYP + title_adjust, ((LXP > 0) ? (LXP - 1) : 0));
if (flag)
LYrefresh();
}
return;
}
/*
* free_and_clear will free a pointer if it is non-zero and then set it to
* zero.
*/
void free_and_clear(char **pointer)
{
if (*pointer) {
FREE(*pointer);
*pointer = 0;
}
return;
}
/*
* Convert single or serial newlines to single spaces throughout a string
* (ignore newlines if the preceding character is a space) and convert tabs to
* single spaces. Don't ignore any explicit tabs or spaces if the condense
* argument is FALSE, otherwise, condense any serial spaces or tabs to one
* space. - FM
*/
void convert_to_spaces(char *string,
int condense)
{
char *s = string;
char *ns;
BOOL last_is_space = FALSE;
if (!s)
return;
s = LYSkipNonBlanks(s);
ns = s;
while (*s) {
switch (*s) {
case ' ':
case '\t':
if (!(condense && last_is_space))
*(ns++) = ' ';
last_is_space = TRUE;
break;
case '\r':
case '\n':
if (!last_is_space) {
*(ns++) = ' ';
last_is_space = TRUE;
}
break;
default:
*(ns++) = *s;
last_is_space = FALSE;
break;
}
s++;
}
*ns = '\0';
return;
}
/*
* Strip trailing slashes from directory paths.
*/
char *strip_trailing_slash(char *dirname)
{
int i;
i = (int) strlen(dirname) - 1;
while (i >= 0 && dirname[i] == '/')
dirname[i--] = '\0';
return (dirname);
}
/*
* Remove most blanks, but restore one trailing blank to make prompts nicer.
*/
static void remove_most_blanks(char *buffer)
{
int length = (int) strlen(buffer);
BOOL trailing = (BOOL) ((length != 0) && (buffer[length - 1] == ' '));
LYReduceBlanks(buffer);
if (trailing)
strcat(buffer, " ");
}
/*
* Display (or hide) the status line.
*/
BOOLEAN mustshow = FALSE;
void statusline(const char *text)
{
char buffer[MAX_LINE];
unsigned char *temp = NULL;
int max_length, len, i, j;
int at_lineno;
unsigned char k;
char *p;
char text_buff[MAX_LINE];
if (text == NULL)
return;
/*
* Don't print statusline messages if dumping to stdout.
*/
if (dump_output_immediately)
return;
/*
* Don't print statusline message if turned off.
*/
if (mustshow != TRUE) {
if (no_statusline == TRUE) {
return;
}
}
mustshow = FALSE;
/* "LYNXDOWNLOAD://Method=-1/File=%s/SugFile=%s%s\">Save to disk</a>\n" */
LYStrNCpy(text_buff, text, sizeof(text_buff) - 1);
p = StrChr(text_buff, '\n');
if (p)
*p = '\0';
/*
* Deal with any CJK escape sequences and Kanji if we have a CJK character
* set selected, otherwise, strip any escapes. Also, make sure text is not
* longer than the statusline window. - FM
*/
max_length = (((LYcolLimit - 1) < (int) sizeof(buffer))
? (LYcolLimit - 1)
: (int) sizeof(buffer) - 1);
if ((text_buff[0] != '\0') &&
(LYHaveCJKCharacterSet)) {
/*
* Translate or filter any escape sequences. - FM
*/
if ((temp = typecallocn(unsigned char, strlen(text_buff) + 1)) == NULL)
outofmem(__FILE__, "statusline");
if (kanji_code == EUC) {
TO_EUC((const unsigned char *) text_buff, temp);
} else if (kanji_code == SJIS) {
#ifdef KANJI_CODE_OVERRIDE
if (!LYRawMode || last_kcode == SJIS)
strcpy(temp, text_buff);
else
TO_SJIS((const unsigned char *) text_buff, temp);
#else
strcpy((char *) temp, text_buff);
#endif
} else {
for (i = 0, j = 0; text_buff[i]; i++) {
if (text_buff[i] != CH_ESC) { /* S/390 -- gil -- 2119 */
temp[j++] = UCH(text_buff[i]);
}
}
temp[j] = '\0';
}
/*
* Deal with any newlines or tabs in the string. - FM
*/
remove_most_blanks((char *) temp);
/*
* Handle the Kanji, making sure the text is not longer than the
* statusline window. - FM
*/
for (i = 0, j = 0, len = 0, k = '\0';
temp[i] != '\0' && len < max_length; i++) {
if (k != '\0') {
buffer[j++] = (char) k;
buffer[j++] = (char) temp[i];
k = '\0';
len += 2;
} else if ((temp[i] & 0200) != 0) {
k = temp[i];
} else {
buffer[j++] = (char) temp[i];
len++;
}
}
buffer[j] = '\0';
FREE(temp);
} else {
/*
* Deal with any newlines or tabs in the string. - FM
*/
remove_most_blanks(text_buff);
#ifdef WIDEC_CURSES
len = (int) strlen(text_buff);
if (len >= (int) (sizeof(buffer) - 1))
len = (int) (sizeof(buffer) - 1);
LYStrNCpy(buffer, text_buff, len);
/* FIXME: a binary search might be faster */
while (len > 0 && LYstrExtent(buffer, len, len) > max_length)
buffer[--len] = '\0';
#else
/*
* Strip any escapes, and shorten text if necessary. Note that we
* don't deal with the possibility of UTF-8 characters in the string.
* This is unlikely, but if strings with such characters are used in
* LYMessages_en.h, a compilation symbol of HAVE_UTF8_STATUSLINES could
* be added there, and code added here for determining the displayed
* string length, as we do above for CJK. - FM
*/
for (i = 0, len = 0; text_buff[i] != '\0' && len < max_length; i++) {
if (text_buff[i] != CH_ESC) { /* S/390 -- gil -- 2119 */
buffer[len++] = text_buff[i];
}
}
buffer[len] = '\0';
#endif
}
/*
* Move to the desired statusline window and output the text highlighted.
* - FM
*/
if (LYStatusLine >= 0) {
if (LYStatusLine < LYlines - 1) {
at_lineno = LYStatusLine;
} else {
at_lineno = LYlines - 1;
}
} else if (user_mode == NOVICE_MODE) {
at_lineno = LYlines - 3;
} else {
at_lineno = LYlines - 1;
}
LYmove(at_lineno, 0);
LYclrtoeol();
if (buffer[0] != '\0') {
BOOLEAN has_CJK = FALSE;
if (IS_CJK_TTY) {
for (i = 0; buffer[i] != '\0'; i++) {
if (buffer[i] & 0x80) {
has_CJK = TRUE;
break;
}
}
}
if (has_CJK
#ifdef HAVE_UTF8_STATUSLINES
|| IS_UTF8_TTY
#endif
) {
LYrefresh();
}
#ifndef USE_COLOR_STYLE
lynx_start_status_color();
LYaddstr(buffer);
lynx_stop_status_color();
#else
/* draw the status bar in the STATUS style */
{
int y, x;
int a = ((StrNCmp(buffer, ALERT_FORMAT, ALERT_PREFIX_LEN)
|| !hashStyles[s_alert].used)
? s_status
: s_alert);
LynxChangeStyle(a, STACK_ON);
LYaddstr(buffer);
wbkgdset(LYwin,
((lynx_has_color && LYShowColor >= SHOW_COLOR_ON)
? (chtype) hashStyles[a].color
: A_NORMAL) | ' ');
LYGetYX(y, x);
(void) x;
if (y == at_lineno) {
LYclrtoeol();
}
if (!(lynx_has_color && LYShowColor >= SHOW_COLOR_ON))
wbkgdset(LYwin, A_NORMAL | ' ');
else if (s_normal != NOSTYLE)
wbkgdset(LYwin, (chtype) (hashStyles[s_normal].color | ' '));
else
wbkgdset(LYwin, (chtype) (displayStyles[DSTYLE_NORMAL].color | ' '));
LynxChangeStyle(a, STACK_OFF);
}
#endif
}
LYrefresh();
return;
}
static const char *novice_lines(int lineno)
{
switch (lineno) {
case 0:
return NOVICE_LINE_TWO_A;
case 1:
return NOVICE_LINE_TWO_B;
case 2:
return NOVICE_LINE_TWO_C;
default:
return "";
}
}
static int lineno = 0;
void toggle_novice_line(void)
{
lineno++;
if (*novice_lines(lineno) == '\0')
lineno = 0;
return;
}
void noviceline(int more_flag GCC_UNUSED)
{
if (dump_output_immediately)
return;
LYmove(LYlines - 2, 0);
LYclrtoeol();
LYaddstr(NOVICE_LINE_ONE);
LYParkCursor();
#if defined(DIRED_SUPPORT ) && defined(OK_OVERRIDE)
if (lynx_edit_mode && !no_dired_support)
LYaddstr(DIRED_NOVICELINE);
else
#endif /* DIRED_SUPPORT && OK_OVERRIDE */
if (LYUseNoviceLineTwo)
LYaddstr(NOVICE_LINE_TWO);
else
LYaddstr(novice_lines(lineno));
LYrefresh();
return;
}
/*
* If the standard input is not a tty, and Lynx is really reading from the
* standard input, attempt to reopen it, pointing to a real tty. Normally
* this would happen if the user pipes data to Lynx and wants to run
* interactively after that.
*
* Returns:
* 1 if successfully reopened
* -1 if we cannot reopen
* 0 if we do not have to reopen
*/
int LYReopenInput(void)
{
int result = 0;
int fd;
if ((fd = fileno(stdin)) == 0
&& !isatty(fd)
&& LYConsoleInputFD(FALSE) == fd) {
const char *term_name = NULL;
int new_fd = -1;
#ifdef HAVE_TTYNAME
if (isatty(fileno(stdout)) &&
(term_name = ttyname(fileno(stdout))) != NULL)
new_fd = open(term_name, O_RDONLY);
if (new_fd == -1 &&
isatty(fileno(stderr)) &&
(term_name = ttyname(fileno(stderr))) != NULL)
new_fd = open(term_name, O_RDONLY);
#endif
#ifdef HAVE_CTERMID
if (new_fd == -1 &&
(term_name = ctermid(NULL)) != NULL)
new_fd = open(term_name, O_RDONLY);
#endif
#ifdef TTY_DEVICE
if (new_fd == -1)
new_fd = open(term_name = TTY_DEVICE, O_RDONLY);
#endif
CTRACE((tfp, "LYReopenInput open(%s) returned %d.\n", term_name, new_fd));
if (new_fd >= 0) {
FILE *frp;
close(new_fd);
frp = freopen(term_name, "r", stdin);
CTRACE((tfp,
"LYReopenInput freopen(%s,\"r\",stdin) returned %p, stdin is now %p with fd %d.\n",
term_name, (void *) frp, (void *) stdin, fileno(stdin)));
result = 1;
} else {
result = -1;
}
}
return result;
}
/*
* Returns the file descriptor from which keyboard input is expected, or INVSOC
* (-1) if not available. If need_selectable is true, returns non-INVSOC fd
* only if select() is possible - actually, currently only checks if fd is
* connected to a tty. - kw
*/
int LYConsoleInputFD(int need_selectable)
{
int fd = INVSOC;
#ifdef USE_SLANG
if (!LYCursesON)
fd = fileno(stdin);
#if ((SLANG_VERSION >= 9919) && defined(REAL_UNIX_SYSTEM) && !defined(__CYGWIN__))
/* SLang_TT_Read_FD introduced in slang 0.99.19, from its changelog:
* SLang_TT_Read_FD variable is now available for unix. This is the file
* descriptor used by SLang_getkey. */
else
fd = SLang_TT_Read_FD;
#endif /* SLANG_VERSION >= 9919 */
#else /* !USE_SLANG */
fd = fileno(stdin);
#endif /* !USE_SLANG */
if (need_selectable && fd != INVSOC) {
if (isatty(fd)) {
return fd;
} else {
return INVSOC;
}
}
return fd;
}
static int fake_zap = 0;
void LYFakeZap(int set)
{
if (set && fake_zap < 1) {
CTRACE((tfp, "\r *** Set simulated 'Z'"));
if (fake_zap)
CTRACE((tfp, ", %d pending", fake_zap));
CTRACE((tfp, " ***\n"));
fake_zap++;
} else if (!set && fake_zap) {
CTRACE((tfp, "\r *** Unset simulated 'Z'"));
CTRACE((tfp, ", %d pending", fake_zap));
CTRACE((tfp, " ***\n"));
fake_zap = 0;
}
}
static int DontCheck(void)
{
static time_t last;
time_t next;
/** Curses or slang setup was not invoked **/
if (dump_output_immediately)
return (TRUE);
if (LYHaveCmdScript()) /* we may be running from a script */
return (TRUE);
if (LYNoZapKey)
return (TRUE);
/*
* Avoid checking interrupts more than one per second, since it is a slow
* and expensive operation - TD
*/
#ifdef HAVE_GETTIMEOFDAY
#undef timezone /* U/Win defines a conflicting macro */
{
struct timeval tv;
gettimeofday(&tv, (struct timezone *) 0);
next = (tv.tv_sec * 10);
next += (tv.tv_usec / 100000L); /* 0.1 seconds is a compromise */
}
#else
next = time((time_t *) 0);
#endif
if (next == last)
return (TRUE);
last = next;
return FALSE;
}
int HTCheckForInterrupt(void)
{
int c;
int cmd;
if (fake_zap > 0) {
fake_zap--;
CTRACE((tfp, "\r *** Got simulated 'Z' ***\n"));
CTRACE_FLUSH(tfp);
CTRACE_SLEEP(AlertSecs);
return ((int) TRUE);
}
/** Curses or slang setup was not invoked **/
if (DontCheck())
return ((int) FALSE);
#ifndef VMS /* UNIX stuff: */
#if !defined(_WINDOWS) || defined(__MINGW32__)
/*
* First, check if there is a character.
*/
#ifdef USE_SLANG
/** No keystroke was entered
Note that this isn't taking possible SOCKSification
and the socks_flag into account, and may fail on the
slang library's select() when SOCKSified. - FM **/
#ifdef DJGPP_KEYHANDLER
if (0 == _bios_keybrd(_NKEYBRD_READY))
return (FALSE);
#else
if (0 == SLang_input_pending(0))
return (FALSE);
#endif /* DJGPP_KEYHANDLER */
#else /* Unix curses: */
{
struct timeval socket_timeout;
int ret = 0;
fd_set readfds;
socket_timeout.tv_sec = 0;
socket_timeout.tv_usec = 0;
FD_ZERO(&readfds);
FD_SET(0, &readfds);
#ifdef SOCKS
if (socks_flag)
ret = Rselect(1, &readfds, NULL, NULL, &socket_timeout);
else
#endif /* SOCKS */
ret = select(1, &readfds, NULL, NULL, &socket_timeout);
/** Suspended? **/
if ((ret == -1) && (SOCKET_ERRNO == EINTR))
return ((int) FALSE);
/** No keystroke was entered? **/
if (!FD_ISSET(0, &readfds))
return ((int) FALSE);
}
#endif /* USE_SLANG */
#endif /* !_WINDOWS */
/*
* Now, read the character.
*/
#if defined(USE_CURSES_NODELAY)
nodelay(LYwin, TRUE);
c = LYgetch();
nodelay(LYwin, FALSE);
#elif defined(USE_SLANG) && defined(_WINDOWS)
if (!SLang_input_pending(0))
return ((int) FALSE);
c = LYgetch();
#else
c = LYgetch();
#endif
#else /* VMS: */
extern int typeahead(void);
/** Control-C or Control-Y and a 'N'o reply to exit query **/
if (HadVMSInterrupt) {
HadVMSInterrupt = FALSE;
return ((int) TRUE);
}
c = typeahead();
#endif /* !VMS */
/*
* 'c' contains whatever character we're able to read from keyboard
*/
/** Keyboard 'Z' or 'z', or Control-G or Control-C **/
if (LYCharIsINTERRUPT(c))
return ((int) TRUE);
/* There is a subset of mainloop() actions available at this stage: no new
* getfile() cycle is possible until the previous finished. Currently we
* have scrolling in partial mode, toggling of trace log, and pasting.
* User search now in progress...
*/
cmd = (LKC_TO_LAC(keymap, c));
switch (cmd) {
case LYK_TRACE_TOGGLE: /* Toggle TRACE mode. */
handle_LYK_TRACE_TOGGLE();
break;
#ifdef CAN_CUT_AND_PASTE
case LYK_TO_CLIPBOARD:{ /* ^S */
const char *s = LYDownLoadAddress();
if (!s || !*s || put_clip(s))
HTInfoMsg(gettext("Copy to clipboard failed."));
else
HTInfoMsg(gettext("Download document URL put to clipboard."));
break;
}
#endif /* defined CAN_CUT_AND_PASTE */
default:
#ifdef DISP_PARTIAL
/* OK, we got several lines from new document and want to scroll... */
if (display_partial && (NumOfLines_partial > 2)) {
BOOLEAN do_refresh;
int res;
int Newline_partial = LYGetNewline();
switch (cmd) {
case LYK_WHEREIS: /* search within the document */
case LYK_NEXT: /* search for the next occurrence in the document */
case LYK_PREV: /* search for the previous occurrence in the document */
handle_LYK_WHEREIS(cmd, &do_refresh);
if (www_search_result != -1) {
Newline_partial = www_search_result;
www_search_result = -1; /* reset */
}
break;
case LYK_FASTBACKW_LINK:
if (Newline_partial <= (display_lines) + 1) {
Newline_partial -= display_lines;
} else if ((res =
HTGetLinkOrFieldStart(-1,
&Newline_partial, NULL,
-1, TRUE)) == LINK_LINE_FOUND) {
Newline_partial++;
} else if (res == LINK_DO_ARROWUP) {
Newline_partial -= display_lines;
}
break;
case LYK_FASTFORW_LINK:
if (HText_canScrollDown()) {
/* This is not an exact science... - kw */
if (HTGetLinkOrFieldStart(HText_LinksInLines(HTMainText,
Newline_partial,
display_lines)
- 1,
&Newline_partial, NULL,
1, TRUE) == LINK_LINE_FOUND) {
Newline_partial++;
}
}
break;
case LYK_PREV_PAGE:
if (Newline_partial > 1)
Newline_partial -= display_lines;
break;
case LYK_NEXT_PAGE:
if (HText_canScrollDown())
Newline_partial += display_lines;
break;
case LYK_UP_HALF:
if (Newline_partial > 1)
Newline_partial -= (display_lines / 2);
break;
case LYK_DOWN_HALF:
if (HText_canScrollDown())
Newline_partial += (display_lines / 2);
break;
case LYK_UP_TWO:
if (Newline_partial > 1)
Newline_partial -= 2;
break;
case LYK_DOWN_TWO:
if (HText_canScrollDown())
Newline_partial += 2;
break;
case LYK_HOME:
if (Newline_partial > 1)
Newline_partial = 1;
break;
case LYK_END:
if (HText_canScrollDown())
Newline_partial = HText_getNumOfLines() - display_lines + 1;
/* calculate for "current" bottom value */
break;
case LYK_REFRESH:
break;
default:
/** Other or no keystrokes **/
return ((int) FALSE);
} /* end switch */
if (Newline_partial < 1)
Newline_partial = 1;
if (LYMainLoop_pageDisplay(Newline_partial))
NumOfLines_partial = HText_getNumOfLines();
}
#endif /* DISP_PARTIAL */
break;
} /* end switch */
/** Other or no keystrokes **/
return ((int) FALSE);
}
/*
* Check if the given filename looks like it's an absolute pathname, i.e.,
* references a directory.
*/
BOOLEAN LYisAbsPath(const char *path)
{
BOOLEAN result = FALSE;
if (non_empty(path)) {
#ifdef VMS
result = TRUE;
#else
#if defined(USE_DOS_DRIVES)
result = (BOOLEAN) (LYIsPathSep(path[0])
|| (LYIsDosDrive(path)
&& LYIsPathSep(path[2])));
#else
result = (BOOLEAN) (LYIsPathSep(path[0]));
#endif /* USE_DOS_DRIVES */
#endif
}
return result;
}
/*
* Check if the given filename is the root path, e.g., "/" on Unix.
*/
BOOLEAN LYisRootPath(const char *path)
{
#if defined(USE_DOS_DRIVES)
if (strlen(path) == 3
&& LYIsDosDrive(path)
&& LYIsPathSep(path[2]))
return TRUE;
#endif
return (BOOL) ((strlen(path) == 1) && LYIsPathSep(path[0]));
}
/*
* A file URL for a remote host is an obsolete ftp URL.
* Return YES only if we're certain it's a local file. - FM
*/
BOOLEAN LYisLocalFile(const char *filename)
{
char *host = NULL;
char *acc_method = NULL;
char *cp;
if (!filename)
return NO;
if (!(host = HTParse(filename, "", PARSE_HOST)))
return NO;
if (!*host) {
FREE(host);
return NO;
}
if ((cp = StrChr(host, ':')) != NULL)
*cp = '\0';
if ((acc_method = HTParse(filename, "", PARSE_ACCESS))) {
if (0 == strcmp("file", acc_method) &&
(0 == strcmp(host, "localhost") ||
LYSameFilename(host, HTHostName()))) {
FREE(host);
FREE(acc_method);
return YES;
}
}
FREE(host);
FREE(acc_method);
return NO;
}
/*
* Utility for checking URLs with a host field. Return YES only if we're
* certain it's the local host. - FM
*/
BOOLEAN LYisLocalHost(const char *filename)
{
char *host = NULL;
char *cp;
if (!filename)
return NO;
if (!(host = HTParse(filename, "", PARSE_HOST)))
return NO;
if (!*host) {
FREE(host);
return NO;
}
if ((cp = StrChr(host, ':')) != NULL)
*cp = '\0';
if ((LYSameFilename(host, "localhost") ||
LYSameFilename(host, LYHostName) ||
LYSameFilename(host, HTHostName()))) {
FREE(host);
return YES;
}
FREE(host);
return NO;
}
/*
* Free an HTList that contains strings.
*/
void LYFreeStringList(HTList *list)
{
if (list != NULL) {
char *argument;
HTList *cur = list;
while (NULL != (argument = (char *) HTList_nextObject(cur))) {
FREE(argument);
}
HTList_delete(list);
}
}
/*
* Utility for freeing the list of local host aliases. - FM
*/
void LYLocalhostAliases_free(void)
{
LYFreeStringList(localhost_aliases);
localhost_aliases = NULL;
}
/*
* Utility for listing hosts to be treated as local aliases. - FM
*/
void LYAddLocalhostAlias(char *alias)
{
char *LocalAlias = NULL;
if (!non_empty(alias))
return;
if (!localhost_aliases) {
localhost_aliases = HTList_new();
#ifdef LY_FIND_LEAKS
atexit(LYLocalhostAliases_free);
#endif
}
StrAllocCopy(LocalAlias, alias);
HTList_addObject(localhost_aliases, LocalAlias);
return;
}
/*
* Utility for checking URLs with a host field. Return YES only if we've
* listed the host as a local alias. - FM
*/
BOOLEAN LYisLocalAlias(const char *filename)
{
char *host = NULL;
char *alias;
char *cp;
HTList *cur = localhost_aliases;
if (!cur || !filename)
return NO;
if (!(host = HTParse(filename, "", PARSE_HOST)))
return NO;
if (!(*host)) {
FREE(host);
return NO;
}
if ((cp = StrChr(host, ':')) != NULL)
*cp = '\0';
while (NULL != (alias = (char *) HTList_nextObject(cur))) {
if (LYSameFilename(host, alias)) {
FREE(host);
return YES;
}
}
FREE(host);
return NO;
}
/*
* This function checks for a URL with an unknown scheme,
* but for which proxying has been set up, and if so,
* returns PROXY_URL_TYPE. - FM
*
* If a colon is present but the string segment which
* precedes it is not being proxied, and we can be sure
* that what follows the colon is not a port field,
* it returns UNKNOWN_URL_TYPE. Otherwise, it returns
* 0 (not a URL). - FM
*/
UrlTypes LYCheckForProxyURL(char *filename)
{
char *cp = filename;
char *cp1;
char *cp2 = NULL;
/*
* Don't crash on an empty argument.
*/
if (isEmpty(cp))
return (NOT_A_URL_TYPE);
/* kill beginning spaces */
cp = LYSkipBlanks(cp);
/*
* Check for a colon, and if present,
* see if we have proxying set up.
*/
if ((cp1 = StrChr((cp + 1), ':')) != NULL) {
if ((cp2 = StrChr((cp + 1), '/')) != NULL && cp2 < cp1)
return (NOT_A_URL_TYPE);
*cp1 = '\0';
cp2 = NULL;
StrAllocCopy(cp2, cp);
*cp1 = ':';
StrAllocCat(cp2, "_proxy");
if (LYGetEnv(cp2) != NULL) {
FREE(cp2);
return (PROXY_URL_TYPE);
}
FREE(cp2);
#if defined (USE_DOS_DRIVES)
if (LYIsDosDrive(cp))
return (NOT_A_URL_TYPE);
#endif
cp1++;
if (!*cp) {
return (NOT_A_URL_TYPE);
} else if (isdigit(UCH(*cp1))) {
while (*cp1 && isdigit(UCH(*cp1)))
cp1++;
if (*cp1 && !LYIsHtmlSep(*cp1))
return (UNKNOWN_URL_TYPE);
} else {
return (UNKNOWN_URL_TYPE);
}
}
return (NOT_A_URL_TYPE);
}
/*
* Compare a "type:" string, replacing it by the comparison-string if it
* matches (and return true in that case).
*/
static BOOLEAN compare_type(char *tst,
const char *cmp,
size_t len)
{
if (!strncasecomp(tst, cmp, (int) len)) {
if (StrNCmp(tst, cmp, len)) {
size_t i;
for (i = 0; i < len; i++)
tst[i] = cmp[i];
}
return TRUE;
}
return FALSE;
}
#define CompareType(tst,cmp,len) compare_type((tst),(cmp),(size_t)(len))
#define DoubleHtmlSep(s) (LYIsHtmlSep((s)[0]) && LYIsHtmlSep((s)[1]))
#define compare_two(tst,cmp,len,limit) \
((len + 2) <= limit \
&& DoubleHtmlSep(tst + len) \
&& CompareType(tst, cmp, len))
/*
* Must recognize a URL and return the type. If recognized, based on a
* case-insensitive analysis of the scheme field, ensures that the scheme field
* has the expected case.
*
* Returns 0 (not a URL) for a NULL argument, one which lacks a colon.
*
* Chains to LYCheckForProxyURL() if a colon is present but the type is not
* recognized.
*/
UrlTypes is_url(char *filename)
{
char *cp = filename;
char *cp1;
UrlTypes result = NOT_A_URL_TYPE;
int limit;
/*
* Don't crash on an empty argument.
*/
if (isEmpty(cp))
return (result);
/*
* Can't be a URL if it lacks a colon and if it starts with '[' it's
* probably IPv6 adress.
*/
if (NULL == StrChr(cp, ':') || cp[0] == '[')
return (result);
/*
* Kill beginning spaces.
*/
cp = LYSkipBlanks(cp);
/*
* Can't be a URL if it starts with a slash. So return immediately for
* this common case, also to avoid false positives if there was a colon
* later in the string. Also can't be a URL if it starts with a colon. -
* KW
*/
if (*cp == ':' || LYIsHtmlSep(*cp)) {
result = NOT_A_URL_TYPE;
} else {
limit = (int) strlen(cp);
switch (*cp) {
case 'L':
case 'l':
/*
* Lynx internal pages ("LYNXfoo:" or "lynxfoo:") start with 'l' or
* 'L', other URLs aren't.
*/
if (CompareType(cp, STR_LYNXEXEC, LEN_LYNXEXEC)) {
/*
* Special External Lynx type to handle execution of commands
* or scripts which require a pause to read the screen upon
* completion.
*/
result = LYNXEXEC_URL_TYPE;
} else if (CompareType(cp, STR_LYNXPROG, LEN_LYNXPROG)) {
/*
* Special External Lynx type to handle execution of commands,
* scripts or programs which do not require a pause to read
* screen upon completion.
*/
result = LYNXPROG_URL_TYPE;
} else if (CompareType(cp, STR_LYNXCGI, LEN_LYNXCGI)) {
result = LYNXCGI_URL_TYPE;
} else if (CompareType(cp, STR_LYNXPRINT, LEN_LYNXPRINT)) {
result = LYNXPRINT_URL_TYPE;
} else if (CompareType(cp, STR_LYNXOPTIONS, LEN_LYNXOPTIONS)) {
result = LYNXOPTIONS_URL_TYPE;
} else if (CompareType(cp, STR_LYNXCFG, LEN_LYNXCFG)) {
result = LYNXCFG_URL_TYPE;
} else if (CompareType(cp, STR_LYNXMESSAGES, LEN_LYNXMESSAGES)) {
result = LYNXMESSAGES_URL_TYPE;
} else if (CompareType(cp, STR_LYNXCFLAGS, LEN_LYNXCFLAGS)) {
result = LYNXCOMPILE_OPTS_URL_TYPE;
} else if (CompareType(cp, STR_LYNXDOWNLOAD, LEN_LYNXDOWNLOAD)) {
result = LYNXDOWNLOAD_URL_TYPE;
} else if (CompareType(cp, STR_LYNXDIRED, LEN_LYNXDIRED)) {
result = LYNXDIRED_URL_TYPE;
} else if (CompareType(cp, STR_LYNXEDITMAP, LEN_LYNXEDITMAP)) {
result = LYNXEDITMAP_URL_TYPE;
} else if (CompareType(cp, STR_LYNXHIST, LEN_LYNXHIST)) {
result = LYNXHIST_URL_TYPE;
#ifdef USE_CACHEJAR
} else if (CompareType(cp, STR_LYNXCACHE, LEN_LYNXCACHE)) {
result = LYNXCACHE_URL_TYPE;
#endif
} else if (CompareType(cp, STR_LYNXKEYMAP, LEN_LYNXKEYMAP)) {
result = LYNXKEYMAP_URL_TYPE;
} else if (CompareType(cp, STR_LYNXIMGMAP, LEN_LYNXIMGMAP)) {
/* force lower/uppercase of next part */
(void) is_url(&cp[LEN_LYNXIMGMAP]);
result = LYNXIMGMAP_URL_TYPE;
} else if (CompareType(cp, STR_LYNXCOOKIE, LEN_LYNXCOOKIE)) {
result = LYNXCOOKIE_URL_TYPE;
}
break;
#ifndef DISABLE_NEWS
/*
* NEWSfoo: schemes -
*/
case 'N':
case 'n':
if (CompareType(cp, STR_NEWS_URL, LEN_NEWS_URL)) {
result = NEWS_URL_TYPE;
} else if (CompareType(cp, STR_NNTP_URL, LEN_NNTP_URL)) {
result = NNTP_URL_TYPE;
} else if (CompareType(cp, "newspost:", 9)) {
/*
* Special Lynx type to handle news posts.
*/
result = NEWSPOST_URL_TYPE;
} else if (CompareType(cp, "newsreply:", 10)) {
/*
* Special Lynx type to handle news replies (followups).
*/
result = NEWSREPLY_URL_TYPE;
}
break;
/*
* SNEWSfoo: schemes -
*/
case 'S':
case 's':
if (CompareType(cp, STR_SNEWS_URL, LEN_SNEWS_URL)) {
result = SNEWS_URL_TYPE;
} else if (CompareType(cp, "snewspost:", 10)) {
/*
* Special Lynx type to handle snews posts.
*/
result = NEWSPOST_URL_TYPE;
} else if (CompareType(cp, "snewsreply:", 11)) {
/*
* Special Lynx type to handle snews replies (followups).
*/
result = NEWSREPLY_URL_TYPE;
}
break;
#endif
case 'M':
case 'm':
if (CompareType(cp, STR_MAILTO_URL, LEN_MAILTO_URL)) {
result = MAILTO_URL_TYPE;
}
break;
case 'F':
case 'f':
if (CompareType(cp, STR_FILE_URL, LEN_FILE_URL)) {
if (LYisLocalFile(cp)) {
result = FILE_URL_TYPE;
} else if (DoubleHtmlSep(cp + LEN_FILE_URL)) {
result = FTP_URL_TYPE;
}
}
#ifndef DISABLE_FTP
else if (compare_two(cp, STR_FTP_URL, LEN_FTP_URL, limit)) {
result = FTP_URL_TYPE;
}
#endif
#ifndef DISABLE_FINGER
else if (compare_two(cp, STR_FINGER_URL, LEN_FINGER_URL, limit)) {
result = FINGER_URL_TYPE;
}
#endif
break;
case 'B':
case 'b':
#ifndef DISABLE_BIBP
if (CompareType(cp, STR_BIBP_URL, LEN_BIBP_URL)) {
result = BIBP_URL_TYPE;
}
#endif
break;
case 'D':
case 'd':
if (CompareType(cp, "data:", 5)) {
result = DATA_URL_TYPE;
}
break;
default:
if (limit >= 3
&& ((cp1 = StrChr(cp + 3, ':')) == NULL
|| !DoubleHtmlSep(cp1 + 1))) {
/*
* If it doesn't contain "://", and it's not one of the the
* above, it can't be a URL with a scheme we know, so check if
* it's an unknown scheme for which proxying has been set up.
* - FM
*/
if (cp1 != NULL
&& (cp1 - cp) > 1 /* exclude DOS-style device:/path */
&& LYisAbsPath(cp1 + 1)) {
result = NCFTP_URL_TYPE;
}
} else {
switch (*cp) {
case 'H':
case 'h':
if (CompareType(cp, STR_HTTP_URL, LEN_HTTP_URL)) {
result = HTTP_URL_TYPE;
} else if (CompareType(cp, STR_HTTPS_URL, LEN_HTTPS_URL)) {
result = HTTPS_URL_TYPE;
}
break;
#ifndef DISABLE_GOPHER
case 'G':
case 'g':
if (CompareType(cp, STR_GOPHER_URL, LEN_GOPHER_URL)) {
if (strlen(cp) >= 11
&& (cp1 = StrChr(cp + 11, '/')) != NULL) {
if (TOUPPER(*(cp1 + 1)) == 'H' || *(cp1 + 1) == 'w')
/* if this is a gopher html type */
result = HTML_GOPHER_URL_TYPE;
else if (*(cp1 + 1) == 'T' || *(cp1 + 1) == '8')
result = TELNET_GOPHER_URL_TYPE;
else if (*(cp1 + 1) == '7')
result = INDEX_GOPHER_URL_TYPE;
else
result = GOPHER_URL_TYPE;
} else {
result = GOPHER_URL_TYPE;
}
}
break;
#endif
case 'W':
case 'w':
if (CompareType(cp, STR_WAIS_URL, LEN_WAIS_URL)) {
result = WAIS_URL_TYPE;
}
break;
case 'T':
case 't':
if (CompareType(cp, STR_TELNET_URL, LEN_TELNET_URL)) {
result = TELNET_URL_TYPE;
} else if (CompareType(cp, STR_TN3270_URL, LEN_TN3270_URL)) {
result = TN3270_URL_TYPE;
}
break;
case 'R':
case 'r':
if (CompareType(cp, STR_RLOGIN_URL, LEN_RLOGIN_URL)) {
result = RLOGIN_URL_TYPE;
}
break;
case 'C':
case 'c':
if (CompareType(cp, STR_CSO_URL, LEN_CSO_URL)) {
result = CSO_URL_TYPE;
}
break;
case 'A':
case 'a':
if (CompareType(cp, "afs:", 4)) {
result = AFS_URL_TYPE;
}
break;
case 'P':
case 'p':
if (CompareType(cp, "prospero:", 9)) {
result = PROSPERO_URL_TYPE;
}
break;
}
}
}
/*
* Check if it is an unknown scheme for which proxying has been set up.
*/
if (result == NOT_A_URL_TYPE)
result = LYCheckForProxyURL(filename);
}
return result;
}
/*
* Sometimes it is just expected that curses is on when an alert or other
* statusline message needs to be shown and we are not just dumping
* immediately. Calling this will 'fix' it, but may not always be appropriate.
* - kw
*/
void LYFixCursesOn(const char *reason)
{
if (dump_output_immediately || LYCursesON)
return;
if (reason) {
CTRACE((tfp, "Forcing curses on to %s\n", reason));
}
start_curses();
}
/*
* Most protocol modules called through HTLoad* expect that curses is on unless
* dump_output_immediately is set, so that statusline messages can be shown.
* Some protocols expect the opposite, namely telnet and friends. This
* function should be called after the 'physical' URL for accessing addr has
* been established. It does the right thing to the degree that curses is
* turned on for known problem cases. In any normal circumstances this should
* never apply, but proxying or rule substitution is not prevented for
* telnet-like URLs, and this 'fix' avoids some crashes that can otherwise
* occur. - kw
*/
BOOLEAN LYFixCursesOnForAccess(const char *addr,
const char *physical)
{
/*
* If curses is off when maybe it shouldn't...
*/
if (!dump_output_immediately && !LYCursesON && physical) {
char *cp1;
/*
* If requested resource wants to be accessed with curses off, and
* getfile() would indeed have turned curses off for it...
*/
if (strstr(addr, "://") != NULL &&
(isTELNET_URL(addr) ||
isRLOGIN_URL(addr) ||
isTN3270_URL(addr) ||
(!isGOPHER_URL(addr) &&
(cp1 = StrChr(addr + 11, '/')) != NULL &&
(*(cp1 + 1) == 'T' || *(cp1 + 1) == '8')))) {
/*
* If actual access that will be done is ok with curses off, then
* do nothing special, else force curses on. - kw
*/
if (!isTELNET_URL(physical) &&
!isRLOGIN_URL(physical) &&
!isTN3270_URL(physical)) {
start_curses();
HTAlert(gettext("Unexpected access protocol for this URL scheme."));
return TRUE;
}
}
}
return FALSE;
}
/*
* Determine whether we allow HEAD and related flags for a URL. - kw
*/
BOOLEAN LYCanDoHEAD(const char *address)
{
char *temp0 = NULL;
int isurl;
if (!non_empty(address))
return FALSE;
if (!StrNCmp(address, "http", 4))
return TRUE;
/* Make copy for is_url() since caller may not care for case changes */
StrAllocCopy(temp0, address);
isurl = is_url(temp0);
if (!isurl) {
FREE(temp0);
return FALSE;
}
if (isurl == LYNXCGI_URL_TYPE) {
FREE(temp0);
#if defined(LYNXCGI_LINKS) && !defined(VMS)
return TRUE;
#else
return FALSE;
#endif
}
/*
* The idea of the following is to allow HEAD for news URLs that identify
* single articles, not those that identify ranges of articles or groups or
* a list of groups. - kw
*/
if (isurl == NEWS_URL_TYPE || isurl == NNTP_URL_TYPE) {
char *temp = HTParse(address, "", PARSE_PATH);
char *cp = strrchr(temp, '/');
if (StrChr((cp ? cp : temp), '@') != NULL) {
FREE(temp0);
FREE(temp);
return TRUE;
}
if (cp && isdigit(UCH(cp[1])) && StrChr(cp, '-') == NULL) {
FREE(temp0);
FREE(temp);
return TRUE;
}
FREE(temp);
}
#define ALLOW_PROXY_HEAD
/* If defined, also allow head requests for URLs proxied through the "http" or
* "lynxcgi" protocols, which understand HEAD. Only the proxy environment
* variables are checked, not the HTRules system. - kw
*/
#ifdef ALLOW_PROXY_HEAD
if (isurl != FILE_URL_TYPE) {
char *acc_method = HTParse(temp0, "", PARSE_ACCESS);
if (non_empty(acc_method)) {
char *proxy;
StrAllocCat(acc_method, "_proxy");
proxy = LYGetEnv(acc_method);
if (proxy && (isHTTP_URL(proxy) ||
isLYNXCGI(proxy)) &&
!override_proxy(temp0)) {
FREE(temp0);
FREE(acc_method);
return TRUE;
}
}
FREE(acc_method);
}
#endif /* ALLOW_PROXY_HEAD */
FREE(temp0);
return FALSE;
}
/*
* Close an input file.
*/
BOOLEAN LYCloseInput(FILE *fp)
{
int result = FALSE;
if (fp != 0) {
int err = ferror(fp);
LY_TEMP *p = FindTempfileByFP(fp);
fclose(fp);
if (p != 0) {
p->file = 0;
}
if (!err) {
result = TRUE;
}
}
return (BOOLEAN) result;
}
/*
* Close an output file, reporting any problems with writing to it.
*/
BOOLEAN LYCloseOutput(FILE *fp)
{
int result = FALSE;
if (fp != 0) {
int err = ferror(fp);
LY_TEMP *p = FindTempfileByFP(fp);
fclose(fp);
if (p != 0) {
p->file = 0;
}
if (!err) {
result = TRUE;
}
}
if (!result) {
HTAlert(CANNOT_WRITE_TO_FILE);
}
return (BOOLEAN) result;
}
/*
* Test if we'll be able to write a file. If not, warn the user.
*/
BOOLEAN LYCanWriteFile(const char *filename)
{
BOOLEAN result = FALSE;
if (LYCloseOutput(fopen(filename, "w"))) {
if (remove(filename) == 0) {
result = TRUE;
}
} else {
_statusline(NEW_FILENAME_PROMPT);
}
return result;
}
/*
* Test if we'll be able to read a file.
*/
BOOLEAN LYCanReadFile(const char *filename)
{
FILE *fp;
BOOLEAN result = FALSE;
if (non_empty(filename)) {
if ((fp = fopen(filename, "r")) != 0) {
result = LYCloseInput(fp);
}
}
return result;
}
char *LYFindConfigFile(const char *nominal, const char *dftfile)
{
char *result = 0;
char *path = 0;
char *head = 0;
char *leaf;
char *item;
if (non_empty(nominal)) {
StrAllocCopy(result, nominal);
/*
* Look for it in as-is - first expanding any tilde.
*/
LYTildeExpand(&result, TRUE);
if (!LYCanReadFile(result)) {
const char *cfg_path;
char *list = 0;
BOOLEAN found = FALSE;
/*
* Now try in the config-path.
*/
if ((cfg_path = LYGetEnv("LYNX_CFG_PATH")) == NULL)
cfg_path = LYNX_CFG_PATH;
StrAllocCopy(list, cfg_path);
path = list;
while ((item = LYstrsep(&path, PATH_SEPARATOR)) != 0) {
if (isEmpty(item))
continue;
FREE(result);
HTSprintf0(&result, "%s%s%s", item, FILE_SEPARATOR, nominal);
LYTildeExpand(&result, TRUE);
if (LYCanReadFile(result)) {
found = TRUE;
break;
}
}
FREE(list);
if (!found) {
/*
* If not found, try finding it in the same directory as the
* compiled-in location of the default file.
*/
StrAllocCopy(head, dftfile);
if (strcmp(nominal, dftfile) &&
(leaf = LYPathLeaf(head)) != head) {
head[leaf - head] = '\0';
StrAllocCopy(result, head);
StrAllocCat(result, nominal);
if (!LYCanReadFile(result)) {
FREE(result);
}
}
#ifdef USE_PROGRAM_DIR
else {
/*
* Finally, try in the same directory as the executable.
*/
StrAllocCopy(result, program_dir);
LYAddPathSep(&result);
StrAllocCat(result, nominal);
LYTildeExpand(&result, TRUE);
if (!LYCanReadFile(result)) {
FREE(result);
}
}
#endif
}
}
}
FREE(head);
return result;
}
/*
* Remove backslashes from any string.
*/
void remove_backslashes(char *buf)
{
char *cp;
for (cp = buf; *cp != '\0'; cp++) {
if (*cp != '\\') { /* don't print slashes */
*buf = *cp;
buf++;
} else if (*cp == '\\' && /* print one slash if there */
*(cp + 1) == '\\') { /* are two in a row */
*buf = *cp;
buf++;
}
}
*buf = '\0';
return;
}
/*
* Checks to see if the current process is attached via a terminal in the local
* domain.
*/
BOOLEAN inlocaldomain(void)
{
BOOLEAN result = TRUE;
#ifdef HAVE_UTMP
int n;
FILE *fp;
struct utmp me;
char *cp, *mytty = NULL;
if ((cp = ttyname(0))) {
mytty = cp;
if (!strncmp(mytty, "/dev/", 5)) {
mytty += 5; /* pty's can be like "pts/0" in utmp */
} else {
if ((mytty = LYLastPathSep(cp)) != 0)
++mytty;
}
}
result = FALSE;
if (mytty && (fp = fopen(UTMP_FILE, "r")) != NULL) {
size_t ulen = strlen(mytty);
if (ulen > sizeof(me.ut_line))
ulen = sizeof(me.ut_line);
do {
n = (int) fread((char *) &me, sizeof(struct utmp), (size_t) 1, fp);
if (n <= 0)
break;
} while (memcmp(me.ut_line, mytty, ulen));
(void) LYCloseInput(fp);
if (n > 0) {
for (ulen = 0; ulen < sizeof(me.ut_host); ++ulen) {
if (me.ut_host[ulen] == '\0')
break;
}
if (ulen > strlen(LYLocalDomain) &&
STREQ(LYLocalDomain,
me.ut_host + ulen - strlen(LYLocalDomain))) {
result = TRUE;
}
#ifdef LINUX
/* Linux fix to check for local user. J.Cullen 11Jul94 */
else if (strlen(me.ut_host) == 0) {
result = TRUE;
}
#endif /* LINUX */
}
} else {
CTRACE((tfp,
"Could not get ttyname (returned %s) or open UTMP file %s\n",
NONNULL(cp), UTMP_FILE));
}
#else
CTRACE((tfp, "LYUtils: inlocaldomain() not supported.\n"));
#endif /* HAVE_UTMP */
return (result);
}
#ifdef HAVE_SIGACTION
/*
* An extended alternative for calling signal(), sets some flags for signal
* handler as we want them if that functionality is available. (We don't
* return anything from this function since the return value would currently be
* ignored anyway.) - kw
*/
void LYExtSignal(int sig,
LYSigHandlerFunc_t *handler)
{
#ifdef SIGWINCH
/* add more cases to if(condition) if required... */
if (sig == SIGWINCH && LYNonRestartingSIGWINCH) {
struct sigaction act;
act.sa_handler = handler;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(sig, &act, NULL);
} else
#endif /* defined(SIGWINCH) */
signal(sig, handler);
}
#endif /* HAVE_SIGACTION */
#if defined(SIGTSTP) && !defined(USE_SLANG)
#ifdef HAVE_SIGACTION
/*
* For switching a signal's handling between SIG_DFL and something (possibly)
* different that may have been set up by lynx code or e.g. by curses library.
* Uses sigaction to preserve / restore as much state as possible.
*
* Second arg is where to save or restore from.
*
* Third arg to_dfl specifies what to do:
* 1 Save current state in where, set handling to SIG_DFL
* 0 Restore current state to previously saved one in where
*
* Currently only used for SIGTSTP without SLANG, to prevent (n)curses signal
* handler from running while lynx is waiting in system() for an interactive
* command like an editor. - kw
*/
static BOOLEAN LYToggleSigDfl(int sig,
struct sigaction *where,
int to_dfl)
{
int rv = -1;
struct sigaction oact;
if (to_dfl == 1) {
rv = sigaction(sig, NULL, &oact);
if (rv == 0) {
if (oact.sa_handler != SIG_DFL) {
oact.sa_handler = SIG_DFL;
rv = sigaction(sig, &oact, where);
} else if (where) {
memcpy(where, &oact, sizeof(oact));
rv = 0;
}
}
} else {
rv = sigaction(sig, where, NULL);
}
if (rv != 0) {
CTRACE((tfp, "Error in LYToggleSigDfl: %s\n", LYStrerror(errno)));
return FALSE;
} else
return TRUE;
}
#endif /* HAVE_SIGACTION */
#endif /* SIGTSTP && !USE_SLANG */
/**************
* This bit of code catches window size change signals
*/
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
/* For systems that have both, but both can't be included, duh (or neither) */
/* FIXME: this whole chunk may be redundant */
#ifdef TERMIO_AND_CURSES
# ifdef TERMIO_AND_TERMIOS
# include <termio.h>
# else
# ifdef HAVE_TERMIOS_H
# include <termios.h>
# else
# ifdef HAVE_TERMIO_H
# include <termio.h>
# endif /* HAVE_TERMIO_H */
# endif /* HAVE_TERMIOS_H */
# endif /* TERMIO_AND_TERMIOS */
#endif /* TERMIO_AND_CURSES */
void size_change(int sig GCC_UNUSED)
{
int old_lines = LYlines;
int old_cols = LYcols;
#ifdef USE_SLANG
#if defined(VMS) || defined(UNIX)
SLtt_get_screen_size();
#endif /* VMS || UNIX */
LYlines = SLtt_Screen_Rows;
LYcols = SLtt_Screen_Cols;
if (sig == 0)
/*
* Called from start_curses().
*/
return;
#else /* Curses: */
#ifdef HAVE_SIZECHANGE
#ifdef TIOCGSIZE
struct ttysize win;
#else
#ifdef TIOCGWINSZ
struct winsize win;
#endif /* TIOCGWINSZ */
#endif /* TIOCGSIZE */
#ifdef TIOCGSIZE
if (ioctl(0, TIOCGSIZE, &win) == 0) {
if (win.ts_lines != 0) {
LYlines = win.ts_lines;
}
if (win.ts_cols != 0) {
LYcols = win.ts_cols;
}
}
#else
#ifdef TIOCGWINSZ
if (ioctl(0, (long) TIOCGWINSZ, &win) == 0) {
if (win.ws_row != 0) {
LYlines = win.ws_row;
}
if (win.ws_col != 0) {
LYcols = win.ws_col;
}
}
#endif /* TIOCGWINSZ */
#endif /* TIOCGSIZE */
#endif /* HAVE_SIZECHANGE */
#ifdef __EMX__
{
int scrsize[2];
_scrsize(scrsize);
LYcols = scrsize[0];
LYlines = scrsize[1];
}
#endif
if (LYlines <= 0)
LYlines = DFT_ROWS;
if (LYcols <= 0)
LYcols = DFT_COLS;
#endif /* USE_SLANG */
/*
* Check if the screen size has actually changed. - AJL
*/
if (LYlines != old_lines || LYcols != old_cols) {
recent_sizechange = TRUE;
CTRACE((tfp, "Window size changed from (%d,%d) to (%d,%d)\n",
old_lines, old_cols, LYlines, LYcols));
#if defined(CAN_SWITCH_DISPLAY_CHARSET) && defined(CAN_AUTODETECT_DISPLAY_CHARSET)
/* May need to reload the font due to different char-box size */
if (current_char_set != auto_display_charset)
Switch_Display_Charset(current_char_set, SWITCH_DISPLAY_CHARSET_RESIZE);
#endif
}
#ifdef SIGWINCH
LYExtSignal(SIGWINCH, size_change);
#endif /* SIGWINCH */
return;
}
/*
* Utility for freeing the list of previous suggested filenames. - FM
*/
void HTSugFilenames_free(void)
{
LYFreeStringList(sug_filenames);
sug_filenames = NULL;
}
/*
* Utility for listing suggested filenames, making any repeated filenames the
* most current in the list. - FM
*/
void HTAddSugFilename(char *fname)
{
char *tmp = NULL;
char *old;
HTList *cur;
if (!non_empty(fname))
return;
StrAllocCopy(tmp, fname);
if (!sug_filenames) {
sug_filenames = HTList_new();
#ifdef LY_FIND_LEAKS
atexit(HTSugFilenames_free);
#endif
HTList_addObject(sug_filenames, tmp);
return;
}
cur = sug_filenames;
while (NULL != (old = (char *) HTList_nextObject(cur))) {
if (!strcmp(old, tmp)) {
HTList_removeObject(sug_filenames, old);
FREE(old);
break;
}
}
HTList_addObject(sug_filenames, tmp);
return;
}
/*
* CHANGE_SUG_FILENAME -- Foteos Macrides 29-Dec-1993 Upgraded for use with
* Lynx2.2 - FM 17-Jan-1994
*/
void change_sug_filename(char *fname)
{
const char *cp2;
char *temp = 0, *cp, *cp1, *end;
#ifdef VMS
char *dot;
int j, k;
#endif /* VMS */
/*
* Establish the current end of fname.
*/
end = fname + strlen(fname);
/*
* Unescape fname.
*/
HTUnEscape(fname);
/*
* Rename any temporary files.
*/
cp2 = wwwName(lynx_temp_space);
if (LYIsHtmlSep(*cp2)) {
HTSprintf0(&temp, "file://localhost%s" PID_FMT, cp2, GETPID());
} else {
HTSprintf0(&temp, "file://localhost/%s" PID_FMT, cp2, GETPID());
}
if (!StrNCmp(fname, temp, strlen(temp))) {
if ((cp = strrchr(fname, '.')) != 0) {
if (strlen(cp) > (strlen(temp) - 4))
cp = NULL;
}
StrAllocCopy(temp, NonNull(cp));
sprintf(fname, "temp%.*s", LY_MAXPATH - 10, temp);
}
FREE(temp);
if (fname[strlen(fname) - 1] == '/')
/*
* Hmm... we have a directory name. It is annoying to see a
* scheme+host+path name as a suggested one, let's remove the
* last_slash and go ahead like we have a file name. - LP
*/
fname[strlen(fname) - 1] = '\0';
/*
* Remove everything up the the last_slash if there is one.
*/
if ((cp = strrchr(fname, '/')) != NULL && strlen(cp) > 1) {
cp1 = fname;
/*
* Go past the slash.
*/
cp++;
for (; *cp != '\0'; cp++, cp1++) {
*cp1 = *cp;
}
*cp1 = '\0';
}
#ifdef _WINDOWS /* 1998/05/05 (Tue) 10:08:05 */
if ((cp = strrchr(fname, '=')) != NULL && strlen(cp) > 1) {
cp1 = fname;
/*
* Go past the '='.
*/
cp++;
for (; *cp != '\0'; cp++, cp1++) {
*cp1 = *cp;
}
*cp1 = '\0';
}
#endif
/*
* Trim off date-size suffix, if present.
*/
if ((*(end - 1) == ']') && ((cp = strrchr(fname, '[')) != NULL) &&
(cp > fname) && *(--cp) == ' ') {
while (*cp == ' ') {
*(cp--) = '\0';
}
}
#ifdef VMS
/*
* Trim off VMS device and/or directory specs, if present.
*/
if ((cp = StrChr(fname, '[')) != NULL &&
(cp1 = strrchr(cp, ']')) != NULL && strlen(cp1) > 1) {
cp1++;
for (cp = fname; *cp1 != '\0'; cp1++) {
*(cp++) = *cp1;
}
*cp = '\0';
}
/*
* Replace illegal or problem characters.
*/
dot = fname + strlen(fname);
for (cp = fname; cp < dot; cp++) {
/*
* Replace with underscores.
*/
if (*cp == ' ' || *cp == '/' || *cp == ':' ||
*cp == '[' || *cp == ']' || *cp == '&') {
*cp = '_';
/*
* Replace with dashes.
*/
} else if (*cp == '!' || *cp == '?' || *cp == '\'' ||
*cp == ',' || *cp == ':' || *cp == '"' ||
*cp == '+' || *cp == '@' || *cp == '\\' ||
*cp == '(' || *cp == ')' || *cp == '=' ||
*cp == '<' || *cp == '>' || *cp == '#' ||
*cp == '%' || *cp == '*' || *cp == '`' ||
*cp == '~' || *cp == '^' || *cp == '|' ||
*cp < ' ' || (UCH(*cp)) > 126) {
*cp = '-';
}
}
/*
* Collapse any serial underscores.
*/
cp = fname + 1;
j = 0;
while (cp < dot) {
if (fname[j] == '_' && *cp == '_') {
cp++;
} else {
fname[++j] = *cp++;
}
}
fname[++j] = '\0';
/*
* Collapse any serial dashes.
*/
dot = fname + (strlen(fname));
cp = fname + 1;
j = 0;
while (cp < dot) {
if (fname[j] == '-' && *cp == '-') {
cp++;
} else {
fname[++j] = *cp++;
}
}
fname[++j] = '\0';
/*
* Trim any trailing or leading underscores or dashes.
*/
cp = fname + (strlen(fname)) - 1;
while (*cp == '_' || *cp == '-') {
*cp-- = '\0';
}
if (fname[0] == '_' || fname[0] == '-') {
dot = fname + (strlen(fname));
cp = fname;
while ((*cp == '_' || *cp == '-') && cp < dot) {
cp++;
}
j = 0;
while (cp < dot) {
fname[j++] = *cp++;
}
fname[j] = '\0';
}
/*
* Replace all but the last period with _'s, or second to last if last is
* followed by a terminal Z or z, or GZ or gz,
* e.g., convert foo.tar.Z to foo.tar_Z
* or, convert foo.tar.gz to foo.tar-gz
*/
j = strlen(fname) - 1;
if ((dot = strrchr(fname, '.')) != NULL) {
if (TOUPPER(fname[j]) == 'Z') {
if ((fname[j - 1] == '.') &&
(((cp = StrChr(fname, '.')) != NULL) && cp < dot)) {
*dot = '_';
dot = strrchr(fname, '.');
} else if (((TOUPPER(fname[j - 1]) == 'G') &&
fname[j - 2] == '.') &&
(((cp = StrChr(fname, '.')) != NULL) && cp < dot)) {
*dot = '-';
dot = strrchr(fname, '.');
}
}
cp = fname;
while ((cp = StrChr(cp, '.')) != NULL && cp < dot) {
*cp = '_';
}
/*
* But if the root is > 39 characters, move the period appropriately to
* the left.
*/
while (dot - fname > 39) {
*dot = '\0';
if ((cp = strrchr(fname, '_')) != NULL) {
*cp = '.';
*dot = '_';
} else if ((cp = strrchr(fname, '-')) != NULL) {
*cp = '.';
*dot = '_';
} else if (*(dot + 1) == '\0') {
j = strlen(fname);
while (j > 39) {
fname[j] = fname[j - 1];
j--;
}
fname[j] = '.';
} else {
*dot = '.';
j = 39;
k = 0;
while (dot[k] != '\0') {
fname[j++] = dot[k++];
}
fname[j] = '\0';
}
dot = strrchr(fname, '.');
}
/*
* Make sure the extension is < 40 characters.
*/
if ((fname + strlen(fname) - dot) > 39) {
*(dot + 40) = '\0';
}
/*
* Trim trailing dashes or underscores.
*/
j = (strlen(fname) - 1);
while (fname[j] == '_' || fname[j] == '-') {
fname[j--] = '\0';
}
} else {
/*
* No period, so put one on the end, or after the 39th character,
* trimming trailing dashes or underscores.
*/
if (strlen(fname) > 39) {
fname[39] = '\0';
}
j = (strlen(fname) - 1);
while ((fname[j] == '_') || (fname[j] == '-')) {
j--;
}
fname[++j] = '.';
fname[++j] = '\0';
}
#else /* Not VMS (UNIX): */
/*
* Replace problem characters.
*/
for (cp = fname; *cp != '\0'; cp++) {
switch (*cp) {
case '\'':
case '"':
case '/':
case ' ':
*cp = '-';
}
}
#endif /* VMS (UNIX) */
/*
* Make sure the rest of the original string in nulled.
*/
cp = fname + strlen(fname);
while (cp < end) {
*cp++ = '\0';
}
return;
}
/*
* Construct a temporary-filename. Assumes result is LY_MAXPATH chars long.
*/
static int fmt_tempname(char *result,
const char *prefix,
const char *suffix)
{
int code;
#ifdef HAVE_RAND_TEMPNAME
#define SIZE_TEMPNAME ((MAX_TEMPNAME / BITS_PER_CHAR) + 1)
static BOOL first = TRUE;
static int names_used = 0;
static unsigned char used_tempname[SIZE_TEMPNAME];
unsigned offset, mask;
#endif
static unsigned counter;
char leaf[LY_MAXPATH];
if (prefix == 0)
prefix = "";
if (suffix == 0)
suffix = "";
/*
* Prefer a random value rather than a counter.
*/
#ifdef HAVE_RAND_TEMPNAME
if (first) {
lynx_srand((unsigned) ((long) time((time_t *) NULL) + (long) result));
first = FALSE;
}
/* We don't really need all of the bits from rand(). The high-order bits
* are the more-random portion in any case, but limiting the width of the
* generated name is done partly to avoid problems on systems that may not
* support long filenames.
*/
counter = MAX_TEMPNAME;
if (names_used < MAX_TEMPNAME) {
long get_rand = lynx_rand();
long max_rand = LYNX_RAND_MAX;
counter = (unsigned) (((float) MAX_TEMPNAME * (float) get_rand) /
(float) max_rand + 1);
/*
* Avoid reusing a temporary name, since there are places in the code
* which can refer to a temporary filename even after it has been
* closed and removed from the filesystem.
*/
do {
counter %= MAX_TEMPNAME;
offset = counter / BITS_PER_CHAR;
mask = (unsigned) (1 << (counter % BITS_PER_CHAR));
if ((used_tempname[offset] & mask) == 0) {
names_used++;
used_tempname[offset] |= UCH(mask);
break;
}
} while ((used_tempname[offset] & mask) == 0);
}
if (names_used >= MAX_TEMPNAME)
HTAlert(gettext("Too many tempfiles"));
#else
counter++;
#endif
#ifdef FNAMES_8_3
/*
* The 'lynx_temp_space' string ends with a '/' or '\\', so we only have to
* limit the length of the leaf. As received (e.g., from HTCompressed),
* the suffix may contain more than a ".htm", e.g., "-txt.gz", so we trim
* off from the filename portion to make room.
*/
sprintf(leaf, PID_FMT PID_FMT, counter, GETPID());
if (strlen(leaf) > 8)
leaf[8] = 0;
if (strlen(suffix) > 4 || *suffix != '.') {
const char *tail = StrChr(suffix, '.');
if (tail == 0)
tail = suffix + strlen(suffix);
if (8 - (tail - suffix) >= 0)
leaf[8 - (tail - suffix)] = 0;
}
strcat(leaf, suffix);
#else
sprintf(leaf, "L" PID_FMT "-%uTMP%s", GETPID(), counter, suffix);
#endif
/*
* Someone could have configured the temporary pathname to be too long.
*/
if ((strlen(prefix) + strlen(leaf)) < LY_MAXPATH) {
sprintf(result, "%s%s", prefix, leaf);
code = TRUE;
} else {
sprintf(result, "%.*s", LY_MAXPATH - 1, leaf);
code = FALSE;
}
CTRACE((tfp, "-> '%s'\n", result));
return (code);
}
/*
* Convert 4, 6, 2, 8 to left, right, down, up, etc.
*/
int number2arrows(int number)
{
switch (number) {
case '1':
number = END_KEY;
break;
case '2':
number = DNARROW_KEY;
break;
case '3':
number = PGDOWN_KEY;
break;
case '4':
number = LTARROW_KEY;
break;
case '5':
number = DO_NOTHING;
break;
case '6':
number = RTARROW_KEY;
break;
case '7':
number = HOME_KEY;
break;
case '8':
number = UPARROW_KEY;
break;
case '9':
number = PGUP_KEY;
break;
}
return (number);
}
/*
* parse_restrictions takes a string of comma-separated restrictions and sets
* the corresponding flags to restrict the facilities available.
*/
/* The first two are special: we want to record whether "default" or "all"
* restrictions were applied, in addition to the detailed effects of those
* options. - kw
*/
/* skip the special flags when processing "all" and "default": */
#define N_SPECIAL_RESTRICT_OPTIONS 2
/* *INDENT-OFF* */
static const struct {
const char *name;
BOOLEAN *flag;
BOOLEAN can;
} restrictions[] = {
{ "default", &had_restrictions_default, TRUE },
{ "all", &had_restrictions_all, TRUE },
{ "inside_telnet", &no_inside_telnet, CAN_ANONYMOUS_INSIDE_DOMAIN_TELNET },
{ "outside_telnet", &no_outside_telnet, CAN_ANONYMOUS_OUTSIDE_DOMAIN_TELNET },
{ "telnet_port", &no_telnet_port, CAN_ANONYMOUS_GOTO_TELNET_PORT },
{ "inside_ftp", &no_inside_ftp, CAN_ANONYMOUS_INSIDE_DOMAIN_FTP },
{ "outside_ftp", &no_outside_ftp, CAN_ANONYMOUS_OUTSIDE_DOMAIN_FTP },
{ "inside_rlogin", &no_inside_rlogin, CAN_ANONYMOUS_INSIDE_DOMAIN_RLOGIN },
{ "outside_rlogin", &no_outside_rlogin, CAN_ANONYMOUS_OUTSIDE_DOMAIN_RLOGIN },
{ "suspend", &no_suspend, FALSE },
{ "editor", &no_editor, FALSE },
{ "shell", &no_shell, FALSE },
{ "bookmark", &no_bookmark, FALSE },
{ "multibook", &no_multibook, FALSE },
{ "bookmark_exec", &no_bookmark_exec, FALSE },
{ "option_save", &no_option_save, FALSE },
{ "print", &no_print, CAN_ANONYMOUS_PRINT },
{ "download", &no_download, FALSE },
{ "disk_save", &no_disk_save, FALSE },
#if defined(EXEC_LINKS) || defined(EXEC_SCRIPTS)
{ "exec", &no_exec, LOCAL_EXECUTION_LINKS_ALWAYS_OFF_FOR_ANONYMOUS },
#endif
{ "lynxcgi", &no_lynxcgi, FALSE },
{ "exec_frozen", &exec_frozen, FALSE },
{ "goto", &no_goto, CAN_ANONYMOUS_GOTO },
{ "jump", &no_jump, CAN_ANONYMOUS_JUMP },
{ "file_url", &no_file_url, FALSE },
#ifndef DISABLE_NEWS
{ "news_post", &no_newspost, FALSE },
{ "inside_news", &no_inside_news, CAN_ANONYMOUS_INSIDE_DOMAIN_READ_NEWS },
{ "outside_news", &no_outside_news, CAN_ANONYMOUS_OUTSIDE_DOMAIN_READ_NEWS },
#endif
{ "mail", &no_mail, CAN_ANONYMOUS_MAIL },
{ "dotfiles", &no_dotfiles, FALSE },
{ "useragent", &no_useragent, FALSE },
#ifdef SUPPORT_CHDIR
{ "chdir", &no_chdir, FALSE },
#endif
#ifdef DIRED_SUPPORT
{ "dired_support", &no_dired_support, FALSE },
#ifdef OK_PERMIT
{ "change_exec_perms", &no_change_exec_perms, FALSE },
#endif /* OK_PERMIT */
#endif /* DIRED_SUPPORT */
#ifdef USE_EXTERNALS
{ "externals", &no_externals, FALSE },
#endif
{ "lynxcfg_info", &no_lynxcfg_info, CAN_ANONYMOUS_VIEW_LYNXCFG_INFO },
#ifndef NO_CONFIG_INFO
{ "lynxcfg_xinfo", &no_lynxcfg_xinfo, CAN_ANONYMOUS_VIEW_LYNXCFG_EXTENDED_INFO },
#ifdef HAVE_CONFIG_H
{ "compileopts_info", &no_compileopts_info, CAN_ANONYMOUS_VIEW_COMPILEOPTS_INFO },
#endif
#endif
/* put "goto" restrictions on the end, since they are a refinement */
#ifndef DISABLE_BIBP
{ "goto_bibp", &no_goto_bibp, CAN_ANONYMOUS_GOTO_BIBP },
#endif
#ifdef HAVE_CONFIG_H
#ifndef NO_CONFIG_INFO
{ "goto_configinfo", &no_goto_configinfo, CAN_ANONYMOUS_GOTO_CONFIGINFO },
#endif
#endif
{ "goto_cso", &no_goto_cso, CAN_ANONYMOUS_GOTO_CSO },
{ "goto_file", &no_goto_file, CAN_ANONYMOUS_GOTO_FILE },
#ifndef DISABLE_FINGER
{ "goto_finger", &no_goto_finger, CAN_ANONYMOUS_GOTO_FINGER },
#endif
{ "goto_ftp", &no_goto_ftp, CAN_ANONYMOUS_GOTO_FTP },
#ifndef DISABLE_GOPHER
{ "goto_gopher", &no_goto_gopher, CAN_ANONYMOUS_GOTO_GOPHER },
#endif
{ "goto_http", &no_goto_http, CAN_ANONYMOUS_GOTO_HTTP },
{ "goto_https", &no_goto_https, CAN_ANONYMOUS_GOTO_HTTPS },
{ "goto_lynxcgi", &no_goto_lynxcgi, CAN_ANONYMOUS_GOTO_LYNXCGI },
{ "goto_lynxexec", &no_goto_lynxexec, CAN_ANONYMOUS_GOTO_LYNXEXEC },
{ "goto_lynxprog", &no_goto_lynxprog, CAN_ANONYMOUS_GOTO_LYNXPROG },
{ "goto_mailto", &no_goto_mailto, CAN_ANONYMOUS_GOTO_MAILTO },
#ifndef DISABLE_NEWS
{ "goto_news", &no_goto_news, CAN_ANONYMOUS_GOTO_NEWS },
{ "goto_nntp", &no_goto_nntp, CAN_ANONYMOUS_GOTO_NNTP },
#endif
{ "goto_rlogin", &no_goto_rlogin, CAN_ANONYMOUS_GOTO_RLOGIN },
#ifndef DISABLE_NEWS
{ "goto_snews", &no_goto_snews, CAN_ANONYMOUS_GOTO_SNEWS },
#endif
{ "goto_telnet", &no_goto_telnet, CAN_ANONYMOUS_GOTO_TELNET },
{ "goto_tn3270", &no_goto_tn3270, CAN_ANONYMOUS_GOTO_TN3270 },
{ "goto_wais", &no_goto_wais, CAN_ANONYMOUS_GOTO_WAIS },
};
/* *INDENT-ON* */
/* This will make no difference between '-' and '_'. It does only in/equality
* compare. It assumes that p2 can't contain dashes, but p1 can. This
* function is also used (if macro OPTNAME_ALLOW_DASHES doesn't have value of
* zero) for compare of commandline options -VH
*/
BOOL strn_dash_equ(const char *p1,
const char *p2,
int len)
{
while (len--) {
if (!*p2)
return 0; /* canonical name is shorter */
switch (*p1) {
case 0:
return 0;
case '-':
case '_':
if (*p2 != '_')
return 0;
else
break;
default:
if (*p1 != *p2)
return 0;
}
++p1;
++p2;
}
return 1;
}
/* Uncomment following lines to allow only exact string matching */
/* #define RESTRICT_NM_ALLOW_DASHES 0 */
#ifndef RESTRICT_NM_ALLOW_DASHES
# define RESTRICT_NM_ALLOW_DASHES 1
#endif
#if RESTRICT_NM_ALLOW_DASHES
# define RESTRICT_NM_EQU(a,b,len) strn_dash_equ(a,b,len)
#else
# define RESTRICT_NM_EQU(a,b,len) STRNEQ(a,b,len)
#endif
/*
* Returns the inx'th name from the restrictions table, or null if inx is
* out of range.
*/
const char *index_to_restriction(unsigned inx)
{
if (inx < TABLESIZE(restrictions))
return restrictions[inx].name;
return NULL;
}
/*
* Returns the value TRUE/FALSE of a given restriction, or -1 if it is not
* one that we recognize.
*/
int find_restriction(const char *name,
int len)
{
unsigned i;
if (len < 0)
len = (int) strlen(name);
for (i = 0; i < TABLESIZE(restrictions); i++) {
if (RESTRICT_NM_EQU(name, restrictions[i].name, len)) {
return (*restrictions[i].flag);
}
}
return -1;
}
void parse_restrictions(const char *s)
{
const char *p;
const char *word;
unsigned i;
BOOLEAN found;
p = s;
while (*p) {
p = LYSkipCBlanks(p);
if (*p == '\0')
break;
word = p;
while (*p != ',' && *p != '\0')
p++;
found = FALSE;
if (RESTRICT_NM_EQU(word, "all", (int) (p - word))) {
found = TRUE;
for (i = N_SPECIAL_RESTRICT_OPTIONS;
i < TABLESIZE(restrictions);
i++)
*(restrictions[i].flag) = TRUE;
} else if (RESTRICT_NM_EQU(word, "default", (int) (p - word))) {
found = TRUE;
for (i = N_SPECIAL_RESTRICT_OPTIONS;
i < TABLESIZE(restrictions);
i++)
*(restrictions[i].flag) = (BOOLEAN) !restrictions[i].can;
} else {
for (i = 0; i < TABLESIZE(restrictions); i++) {
if (RESTRICT_NM_EQU(word, restrictions[i].name, (int) (p - word))) {
*(restrictions[i].flag) = TRUE;
found = TRUE;
break;
}
}
}
if (!found) {
printf("%s: %.*s\n", gettext("unknown restriction"),
(int) (p - word), word);
exit_immediately(EXIT_FAILURE);
}
if (*p)
p++;
}
/*
* If shell is restricted, set restrictions on related topics.
*/
if (no_shell) {
no_goto_lynxexec = TRUE;
no_goto_lynxprog = TRUE;
no_goto_lynxcgi = TRUE;
#ifdef EXEC_LINKS
local_exec_on_local_files = TRUE;
#endif
}
}
void print_restrictions_to_fd(FILE *fp)
{
unsigned i, count = 0;
for (i = 0; i < TABLESIZE(restrictions); i++) {
if (*(restrictions[i].flag) == TRUE) {
count++;
}
}
if (!count) {
fprintf(fp, gettext("No restrictions set.\n"));
return;
}
fprintf(fp, gettext("Restrictions set:\n"));
for (i = 0; i < TABLESIZE(restrictions); i++) {
if (*(restrictions[i].flag) == TRUE) {
/* if "goto" is restricted, don't bother tell about its
* refinements
*/
if (StrNCmp(restrictions[i].name, "goto_", 5)
|| !no_goto)
fprintf(fp, " %s\n", restrictions[i].name);
}
}
}
#ifdef VMS
#include <jpidef.h>
#include <maildef.h>
#include <starlet.h>
typedef struct _VMSMailItemList {
short buffer_length;
short item_code;
void *buffer_address;
long *return_length_address;
} VMSMailItemList;
void LYCheckMail(void)
{
static BOOL firsttime = TRUE, failure = FALSE;
static char user[13], dir[252];
static long userlen = 0, dirlen;
static time_t lastcheck = 0;
time_t now;
static short new, lastcount;
long ucontext = 0, status;
short flags = MAIL$M_NEWMSG;
/* *INDENT-OFF* */
VMSMailItemList
null_list[] = {{0,0,0,0}},
jpi_list[] = {{sizeof(user) - 1,JPI$_USERNAME,(void *)user,&userlen},
{0,0,0,0}},
uilist[] = {{0,MAIL$_USER_USERNAME,0,0},
{0,0,0,0}},
uolist[] = {{sizeof(new),MAIL$_USER_NEW_MESSAGES,&new,0},
{sizeof(dir),MAIL$_USER_FULL_DIRECTORY,dir,&dirlen},
{0,0,0,0}};
/* *INDENT-ON* */
extern long mail$user_begin();
extern long mail$user_get_info();
extern long mail$user_end();
if (failure)
return;
if (firsttime) {
firsttime = FALSE;
/* Get the username. */
status = sys$getjpiw(0, 0, 0, jpi_list, 0, 0, 0);
if (!(status & 1)) {
failure = TRUE;
return;
}
user[userlen] = '\0';
LYTrimTrailing(user);
}
/* Minimum report interval is 60 sec. */
time(&now);
if (now - lastcheck < 60)
return;
lastcheck = now;
/* Get the current newmail count. */
status = mail$user_begin(&ucontext, null_list, null_list);
if (!(status & 1)) {
failure = TRUE;
return;
}
uilist[0].buffer_length = strlen(user);
uilist[0].buffer_address = user;
status = mail$user_get_info(&ucontext, uilist, uolist);
if (!(status & 1)) {
failure = TRUE;
return;
}
/* Should we report anything to the user? */
if (new > 0) {
if (lastcount == 0)
/* Have newmail at startup of Lynx. */
HTUserMsg(HAVE_UNREAD_MAIL_MSG);
else if (new > lastcount)
/* Have additional mail since last report. */
HTUserMsg(HAVE_NEW_MAIL_MSG);
lastcount = new;
return;
}
lastcount = new;
/* Clear the context */
mail$user_end((long *) &ucontext, null_list, null_list);
return;
}
#else
void LYCheckMail(void)
{
static BOOL firsttime = TRUE;
static char *mf;
static time_t lastcheck;
static time_t lasttime;
static long lastsize;
time_t now;
struct stat st;
if (firsttime) {
mf = LYGetEnv("MAIL");
firsttime = FALSE;
time(&lasttime);
}
if (mf == NULL)
return;
time(&now);
if (now - lastcheck < 60)
return;
lastcheck = now;
if ((stat(mf, &st) < 0)
|| !S_ISREG(st.st_mode)) {
mf = NULL;
return;
}
if (st.st_size > 0) {
if (((lasttime != st.st_mtime) && (st.st_mtime > st.st_atime))
|| ((lastsize != 0) && (st.st_size > lastsize)))
HTUserMsg(HAVE_NEW_MAIL_MSG);
else if (lastsize == 0)
HTUserMsg(HAVE_MAIL_MSG);
}
lastsize = (long) st.st_size;
lasttime = st.st_mtime;
return;
}
#endif /* VMS */
/*
* This function ensures that an href will be
* converted to a fully resolved, absolute URL,
* with guessing of the host or expansions of
* lead tildes via LYConvertToURL() if needed,
* and tweaking/simplifying via HTParse(). It
* is used for LynxHome, startfile, homepage,
* and 'g'oto entries, after they have been
* passed to LYFillLocalFileURL(). - FM
* Such URLs have no `base' reference to which they
* could be resolved. LYLegitimizeHREF could not be used.
*/
void LYEnsureAbsoluteURL(char **href,
const char *name,
int fixit)
{
char *temp = NULL;
if (isEmpty(*href))
return;
/*
* Check whether to fill in localhost. - FM
*/
LYFillLocalFileURL(href, "file://localhost");
/*
* If it is not a URL then make it one.
*/
if (!strcasecomp(*href, STR_NEWS_URL)) {
StrAllocCat(*href, "*");
} else if (!strcasecomp(*href, STR_SNEWS_URL)) {
StrAllocCat(*href, "/*");
}
if (!is_url(*href)) {
CTRACE((tfp, "%s%s'%s' is not a URL\n",
NonNull(name), (name ? " " : ""), *href));
LYConvertToURL(href, fixit);
}
temp = HTParse(*href, "", PARSE_ALL);
if (non_empty(temp))
StrAllocCopy(*href, temp);
FREE(temp);
}
static const char *default_scheme = "http://";
static const char *guess_scheme(const char *url)
{
const char *scheme = NULL;
if (LYGuessScheme && non_empty(url)) {
if (0 == strncasecomp(url, "www.", 4)) {
scheme = "http://";
} else if (0 == strncasecomp(url, "ftp.", 4)) {
scheme = "ftp://";
} else if (0 == strncasecomp(url, "gopher.", 7)) {
scheme = "gopher://";
} else if (0 == strncasecomp(url, "wais.", 5)) {
scheme = "wais://";
} else if (0 == strncasecomp(url, "cso.", 4) ||
0 == strncasecomp(url, "ns.", 3) ||
0 == strncasecomp(url, "ph.", 3)) {
scheme = "cso://";
} else if (0 == strncasecomp(url, "finger.", 7)) {
scheme = "finger://";
} else if (0 == strncasecomp(url, "news.", 5)) {
scheme = "news://";
} else if (0 == strncasecomp(url, "nntp.", 5)) {
scheme = "nntp://";
}
}
CTRACE((tfp, "guess_scheme(%s) -> '%s'\n", NonNull(url), NonNull(scheme)));
return scheme;
}
/*
* This function rewrites and reallocates a previously allocated string that
* begins with an Internet host name so that the string begins with its guess
* of the scheme based on the first field of the host name, or the default
* scheme if no guess was made.
*/
static void LYAddSchemeForURL(char **AllocatedString)
{
if (non_empty(*AllocatedString)) {
char *Str = NULL;
const char *scheme = guess_scheme(*AllocatedString);
if (scheme == NULL)
scheme = default_scheme;
StrAllocCopy(Str, scheme);
StrAllocCat(Str, *AllocatedString);
StrAllocCopy(*AllocatedString, Str);
FREE(Str);
}
}
/*
* This function rewrites and reallocates a previously allocated string so that
* the first element is a confirmed Internet host, and returns TRUE, otherwise
* it does not modify the string and returns FALSE.
*
* It first tries the element as is, then, if the element does not end with a
* dot, it adds prefixes from the (comma separated) prefix list argument, and,
* if the element does not begin with a dot, suffixes from the (comma
* separated) suffix list arguments (e.g., www.host.com, then www.host,edu,
* then www.host.net, then www.host.org).
*
* The remaining path, if one is present, will be appended to the expanded
* host.
*
* It also takes into account whether a colon is in the element or suffix, and
* includes that and what follows as a port field for the expanded host field
* (e.g, wfbr:8002/dir/lynx should yield www.wfbr.edu:8002/dir/lynx).
*/
static BOOLEAN LYExpandHostForURL(char **AllocatedString,
char *prefix_list,
char *suffix_list)
{
char *DomainPrefix = NULL;
const char *StartP, *EndP;
char *DomainSuffix = NULL;
const char *StartS, *EndS;
char *Str = NULL, *StrColon = NULL, *MsgStr = NULL;
char *Host = NULL, *HostColon = NULL, *host = NULL;
char *Path = NULL;
char *Fragment = NULL;
BOOLEAN GotHost = FALSE;
BOOLEAN Startup = (BOOL) (helpfilepath == NULL);
/*
* If it's a NULL or zero-length string, or if it begins with a slash or
* hash, don't continue pointlessly. - FM
*/
if (isEmpty(*AllocatedString) ||
*AllocatedString[0] == '/' ||
*AllocatedString[0] == '#') {
return GotHost;
}
/*
* If it's a partial or relative path, don't continue pointlessly. - FM
*/
if (!StrNCmp(*AllocatedString, "..", 2) ||
!StrNCmp(*AllocatedString, "./", 2)) {
return GotHost;
}
/*
* Make a clean copy of the string, and trim off the path if one is
* present, but save the information so we can restore the path after
* filling in the Host[:port] field. - FM
*/
StrAllocCopy(Str, *AllocatedString);
if ((Path = StrChr(Str, '/')) != NULL) {
/*
* Have a path. Any fragment should already be included in Path. - FM
*/
*Path = '\0';
} else {
/*
* No path, so check for a fragment and trim that, to be restored after
* filling in the Host[:port] field. - FM
*/
Fragment = trimPoundSelector(Str);
}
/*
* If the potential host string has a colon, assume it begins a port field,
* and trim it off, but save the information so we can restore the port
* field after filling in the host field. - FM
*/
if ((StrColon = strrchr(Str, ':')) != NULL &&
isdigit(UCH(StrColon[1])) && StrChr(StrColon, ']') == NULL) {
if (StrColon == Str) {
goto cleanup;
}
*StrColon = '\0';
}
/*
* Do a DNS test on the potential host field as presently trimmed. - FM
*/
StrAllocCopy(host, Str);
strip_userid(host, FALSE);
HTUnEscape(host);
if (LYCursesON) {
StrAllocCopy(MsgStr, WWW_FIND_MESSAGE);
StrAllocCat(MsgStr, host);
StrAllocCat(MsgStr, FIRST_SEGMENT);
HTProgress(MsgStr);
} else if (Startup && !dump_output_immediately) {
fprintf(stdout, "%s '%s'%s\r\n", WWW_FIND_MESSAGE, host, FIRST_SEGMENT);
}
#ifdef INET6
if (HTCheckAddrInfo(host, 80))
#else
if (LYCheckHostByName(host))
#endif /* INET6 */
{
/*
* Clear any residual interrupt. - FM
*/
if (LYCursesON && HTCheckForInterrupt()) {
CTRACE((tfp,
"LYExpandHostForURL: Ignoring interrupt because '%s' resolved.\n",
host));
}
/*
* Return success. - FM
*/
GotHost = TRUE;
goto cleanup;
} else if (LYCursesON && (lynx_nsl_status == HT_INTERRUPTED)) {
/*
* Give the user chance to interrupt lookup cycles. - KW & FM
*/
CTRACE((tfp,
"LYExpandHostForURL: Interrupted while '%s' failed to resolve.\n",
host));
/*
* Return failure. - FM
*/
goto cleanup;
}
/*
* Set the first prefix, making it a zero-length string if the list is NULL
* or if the potential host field ends with a dot. - FM
*/
StartP = ((prefix_list && Str[strlen(Str) - 1] != '.')
? prefix_list
: "");
/*
* If we have a prefix, but the allocated string is one of the common host
* prefixes, make our prefix a zero-length string. - FM
*/
if (*StartP && *StartP != '.') {
if (guess_scheme(*AllocatedString) != NULL) {
StartP = "";
}
}
while ((*StartP) && (WHITE(*StartP) || *StartP == ',')) {
StartP++; /* Skip whitespace and separators */
}
EndP = StartP;
while (*EndP && !WHITE(*EndP) && *EndP != ',') {
EndP++; /* Find separator */
}
StrAllocCopy(DomainPrefix, StartP);
DomainPrefix[EndP - StartP] = '\0';
/*
* Test each prefix with each suffix. - FM
*/
do {
/*
* Set the first suffix, making it a zero-length string if the list is
* NULL or if the potential host field begins with a dot. - FM
*/
StartS = ((suffix_list && *Str != '.')
? suffix_list
: "");
while ((*StartS) && (WHITE(*StartS) || *StartS == ',')) {
StartS++; /* Skip whitespace and separators */
}
EndS = StartS;
while (*EndS && !WHITE(*EndS) && *EndS != ',') {
EndS++; /* Find separator */
}
StrAllocCopy(DomainSuffix, StartS);
DomainSuffix[EndS - StartS] = '\0';
/*
* Create domain names and do DNS tests. - FM
*/
do {
StrAllocCopy(Host, DomainPrefix);
StrAllocCat(Host, ((*Str == '.') ? (Str + 1) : Str));
if (Host[strlen(Host) - 1] == '.') {
Host[strlen(Host) - 1] = '\0';
}
StrAllocCat(Host, DomainSuffix);
if ((HostColon = strrchr(Host, ':')) != NULL &&
isdigit(UCH(HostColon[1]))) {
*HostColon = '\0';
}
StrAllocCopy(host, Host);
HTUnEscape(host);
if (LYCursesON) {
StrAllocCopy(MsgStr, WWW_FIND_MESSAGE);
StrAllocCat(MsgStr, host);
StrAllocCat(MsgStr, GUESSING_SEGMENT);
HTProgress(MsgStr);
} else if (Startup && !dump_output_immediately) {
fprintf(stdout, "%s '%s'%s\n", WWW_FIND_MESSAGE, host, GUESSING_SEGMENT);
}
GotHost = LYCheckHostByName(host);
if (HostColon != NULL) {
*HostColon = ':';
}
if (GotHost == FALSE) {
/*
* Give the user chance to interrupt lookup cycles. - KW
*/
if (LYCursesON && (lynx_nsl_status == HT_INTERRUPTED)) {
CTRACE((tfp,
"LYExpandHostForURL: Interrupted while '%s' failed to resolve.\n",
host));
goto cleanup; /* We didn't find a valid name. */
}
/*
* Advance to the next suffix, or end of suffix list. - FM
*/
StartS = ((*EndS == '\0') ? EndS : (EndS + 1));
while ((*StartS) && (WHITE(*StartS) || *StartS == ',')) {
StartS++; /* Skip whitespace and separators */
}
EndS = StartS;
while (*EndS && !WHITE(*EndS) && *EndS != ',') {
EndS++; /* Find separator */
}
LYStrNCpy(DomainSuffix, StartS, (EndS - StartS));
}
} while ((GotHost == FALSE) && (*DomainSuffix != '\0'));
if (GotHost == FALSE) {
/*
* Advance to the next prefix, or end of prefix list. - FM
*/
StartP = ((*EndP == '\0') ? EndP : (EndP + 1));
while ((*StartP) && (WHITE(*StartP) || *StartP == ',')) {
StartP++; /* Skip whitespace and separators */
}
EndP = StartP;
while (*EndP && !WHITE(*EndP) && *EndP != ',') {
EndP++; /* Find separator */
}
LYStrNCpy(DomainPrefix, StartP, (EndP - StartP));
}
} while ((GotHost == FALSE) && (*DomainPrefix != '\0'));
/*
* If a test passed, restore the port field if we had one and there is no
* colon in the expanded host, and the path if we had one, and reallocate
* the original string with the expanded Host[:port] field included. - FM
*/
if (GotHost) {
if (StrColon && StrChr(Host, ':') == NULL) {
*StrColon = ':';
StrAllocCat(Host, StrColon);
}
if (Path) {
*Path = '/';
StrAllocCat(Host, Path);
} else if (Fragment) {
StrAllocCat(Host, "/");
restorePoundSelector(Fragment);
StrAllocCat(Host, Fragment);
}
StrAllocCopy(*AllocatedString, Host);
}
/*
* Clear any residual interrupt. - FM
*/
if (LYCursesON && HTCheckForInterrupt()) {
CTRACE((tfp,
"LYExpandHostForURL: Ignoring interrupt because '%s' %s.\n",
host,
(GotHost ? "resolved" : "timed out")));
}
/*
* Clean up and return the last test result. - FM
*/
cleanup:
FREE(DomainPrefix);
FREE(DomainSuffix);
FREE(Str);
FREE(MsgStr);
FREE(Host);
FREE(host);
return GotHost;
}
/*
* Rewrite and reallocate a previously allocated string as a file URL if the
* string resolves to a file or directory on the local system, otherwise as an
* http URL. - FM
*/
void LYConvertToURL(char **AllocatedString,
int fixit)
{
char *old_string = *AllocatedString;
char *temp = NULL;
char *cp = NULL;
#ifndef VMS
struct stat st;
#endif /* !VMS */
if (isEmpty(old_string))
return;
#if defined(USE_DOS_DRIVES)
{
char *cp_url = *AllocatedString;
for (; *cp_url != '\0'; cp_url++)
if (*cp_url == '\\')
*cp_url = '/';
cp_url--;
if (LYIsDosDrive(*AllocatedString) && *cp_url == ':')
LYAddPathSep(AllocatedString);
}
#endif /* USE_DOS_DRIVES */
*AllocatedString = NULL; /* so StrAllocCopy doesn't free it */
StrAllocCopy(*AllocatedString, "file://localhost");
if (*old_string != '/') {
char *fragment = NULL;
#if defined(USE_DOS_DRIVES)
StrAllocCat(*AllocatedString, "/");
#endif /* USE_DOS_DRIVES */
#ifdef VMS
/*
* Not a SHELL pathspec. Get the full VMS spec and convert it.
*/
char *cur_dir = NULL;
static char url_file[LY_MAXPATH], file_name[LY_MAXPATH], dir_name[LY_MAXPATH];
unsigned long context = 0;
$DESCRIPTOR(url_file_dsc, url_file);
$DESCRIPTOR(file_name_dsc, file_name);
if (LYIsTilde(*old_string)) {
/*
* On VMS, we'll accept '~' on the command line as Home_Dir(), and
* assume the rest of the path, if any, has SHELL syntax.
*/
StrAllocCat(*AllocatedString, HTVMS_wwwName(Home_Dir()));
if ((cp = StrChr(old_string, '/')) != NULL) {
/*
* Append rest of path, if present, skipping "user" if "~user"
* was entered, simplifying, and eliminating any residual
* relative elements. - FM
*/
StrAllocCopy(temp, cp);
LYTrimRelFromAbsPath(temp);
StrAllocCat(*AllocatedString, temp);
FREE(temp);
}
goto have_VMS_URL;
} else {
fragment = trimPoundSelector(old_string);
LYStrNCpy(url_file, old_string, sizeof(url_file) - 1);
}
url_file_dsc.dsc$w_length = (short) strlen(url_file);
if (1 & lib$find_file(&url_file_dsc, &file_name_dsc, &context,
0, 0, 0, 0)) {
/*
* We found the file. Convert to a URL pathspec.
*/
if ((cp = StrChr(file_name, ';')) != NULL) {
*cp = '\0';
}
LYLowerCase(file_name);
StrAllocCat(*AllocatedString, HTVMS_wwwName(file_name));
if ((cp = StrChr(old_string, ';')) != NULL) {
StrAllocCat(*AllocatedString, cp);
}
if (fragment != NULL) {
restorePoundSelector(fragment);
StrAllocCat(*AllocatedString, fragment);
fragment = NULL;
}
} else if ((NULL != getcwd(dir_name, sizeof(dir_name) - 1, 0)) &&
0 == chdir(old_string)) {
/*
* Probably a directory. Try converting that.
*/
StrAllocCopy(cur_dir, dir_name);
restorePoundSelector(fragment);
if (NULL != getcwd(dir_name, sizeof(dir_name) - 1, 0)) {
/*
* Yup, we got it!
*/
LYLowerCase(dir_name);
StrAllocCat(*AllocatedString, dir_name);
if (fragment != NULL) {
StrAllocCat(*AllocatedString, fragment);
fragment = NULL;
}
} else {
/*
* Nope. Assume it's an http URL with the "http://" defaulted,
* if we can't rule out a bad VMS path.
*/
fragment = NULL;
if (StrChr(old_string, '[') ||
((cp = StrChr(old_string, ':')) != NULL &&
!isdigit(UCH(cp[1]))) ||
!LYExpandHostForURL(&old_string,
URLDomainPrefixes,
URLDomainSuffixes)) {
/*
* Probably a bad VMS path (but can't be sure). Use
* original pathspec for the error message that will
* result.
*/
sprintf(url_file, "/%.*s", sizeof(url_file) - 2, old_string);
CTRACE((tfp,
"Can't find '%s' Will assume it's a bad path.\n",
old_string));
StrAllocCat(*AllocatedString, url_file);
} else {
LYAddSchemeForURL(&old_string);
StrAllocCopy(*AllocatedString, old_string);
}
}
} else {
/*
* Nothing found. Assume it's an http URL with the "http://"
* defaulted, if we can't rule out a bad VMS path.
*/
restorePoundSelector(fragment);
fragment = NULL;
if (StrChr(old_string, '[') ||
((cp = StrChr(old_string, ':')) != NULL &&
!isdigit(UCH(cp[1]))) ||
!LYExpandHostForURL(&old_string,
URLDomainPrefixes,
URLDomainSuffixes)) {
/*
* Probably a bad VMS path (but can't be sure). Use original
* pathspec for the error message that will result.
*/
sprintf(url_file, "/%.*s", sizeof(url_file) - 2, old_string);
CTRACE((tfp, "Can't find '%s' Will assume it's a bad path.\n",
old_string));
StrAllocCat(*AllocatedString, url_file);
} else {
LYAddSchemeForURL(&old_string);
StrAllocCopy(*AllocatedString, old_string);
}
}
lib$find_file_end(&context);
FREE(cur_dir);
have_VMS_URL:
CTRACE((tfp, "Trying: '%s'\n", *AllocatedString));
#else /* not VMS: */
#if defined(USE_DOS_DRIVES)
#ifdef _WINDOWS
if (*old_string == '.') {
char fullpath[MAX_PATH + 1];
char *filepart = NULL;
DWORD chk;
chk = GetFullPathNameA(old_string, MAX_PATH + 1,
fullpath, &filepart);
if (chk != 0) {
StrAllocCopy(temp, wwwName(fullpath));
StrAllocCat(*AllocatedString, temp);
FREE(temp);
CTRACE((tfp, "Converted '%s' to '%s'\n",
old_string, *AllocatedString));
} else {
StrAllocCat(*AllocatedString, old_string);
}
}
#else
if (strlen(old_string) == 1 && *old_string == '.') {
/*
* They want .
*/
char curdir[LY_MAXPATH];
StrAllocCopy(temp, wwwName(Current_Dir(curdir)));
StrAllocCat(*AllocatedString, temp);
FREE(temp);
CTRACE((tfp, "Converted '%s' to '%s'\n",
old_string, *AllocatedString));
}
#endif
else
#endif /* USE_DOS_DRIVES */
if (LYIsTilde(*old_string)) {
char *his_home = NULL;
StrAllocCopy(his_home, old_string);
LYTildeExpand(&his_home, FALSE);
StrAllocCat(*AllocatedString, his_home);
FREE(his_home);
CTRACE((tfp, "Converted '%s' to '%s'\n",
old_string, *AllocatedString));
} else {
/*
* Create a full path to the current default directory.
*/
char curdir[LY_MAXPATH];
char *temp2 = NULL;
BOOL is_local = FALSE;
Current_Dir(curdir);
/*
* Concatenate and simplify, trimming any residual relative
* elements. - FM
*/
#if defined (USE_DOS_DRIVES)
if (old_string[1] != ':' && old_string[1] != '|') {
StrAllocCopy(temp, wwwName(curdir));
LYAddHtmlSep(&temp);
LYStrNCpy(curdir, temp, (sizeof(curdir) - 1));
StrAllocCat(temp, old_string);
} else {
curdir[0] = '\0';
/* 1998/01/13 (Tue) 12:24:33 */
if (old_string[1] == '|')
old_string[1] = ':';
StrAllocCopy(temp, old_string);
if (strlen(temp) == 2 && LYIsDosDrive(temp))
LYAddPathSep(&temp);
}
#else
StrAllocCopy(temp, curdir);
StrAllocCat(temp, "/");
StrAllocCat(temp, old_string);
#endif /* USE_DOS_DRIVES */
LYTrimRelFromAbsPath(temp);
CTRACE((tfp, "Converted '%s' to '%s'\n", old_string, temp));
if ((stat(temp, &st) > -1) ||
LYCanReadFile(temp)) {
/*
* It is a subdirectory or file on the local system.
*/
#if defined (USE_DOS_DRIVES)
/* Don't want to see DOS local paths like c: escaped */
/* especially when we really have file://localhost/ */
/* at the beginning. To avoid any confusion we allow */
/* escaping the path if URL specials % or # present. */
if (StrChr(temp, '#') == NULL && StrChr(temp, '%') == NULL)
StrAllocCopy(cp, temp);
else
cp = HTEscape(temp, URL_PATH);
#else
cp = HTEscape(temp, URL_PATH);
#endif /* USE_DOS_DRIVES */
StrAllocCat(*AllocatedString, cp);
FREE(cp);
CTRACE((tfp, "Converted '%s' to '%s'\n",
old_string, *AllocatedString));
is_local = TRUE;
} else {
char *cp2 = NULL;
StrAllocCopy(temp2, curdir);
LYAddPathSep(&temp2);
StrAllocCopy(cp, old_string);
fragment = trimPoundSelector(cp);
HTUnEscape(cp); /* unescape given path without fragment */
StrAllocCat(temp2, cp); /* append to current dir */
StrAllocCopy(cp2, temp2); /* keep a copy in cp2 */
LYTrimRelFromAbsPath(temp2);
#ifdef WIN_EX /* 1998/07/31 (Fri) 09:09:03 */
HTUnEscape(temp2); /* for LFN */
#endif
if (strcmp(temp2, temp) != 0 &&
((stat(temp2, &st) > -1) ||
LYCanReadFile(temp2))) {
/*
* It is a subdirectory or file on the local system with
* escaped characters and/or a fragment to be appended to
* the URL. - FM
*/
FREE(temp);
if (strcmp(cp2, temp2) == 0) {
/*
* LYTrimRelFromAbsPath did nothing, use old_string as
* given. - kw
*/
temp = HTEscape(curdir, URL_PATH);
LYAddHtmlSep(&temp);
StrAllocCat(temp, old_string);
} else {
temp = HTEscape(temp2, URL_PATH);
if (fragment != NULL) {
restorePoundSelector(fragment);
StrAllocCat(temp, fragment);
}
}
StrAllocCat(*AllocatedString, temp);
CTRACE((tfp, "Converted '%s' to '%s'\n",
old_string, *AllocatedString));
is_local = TRUE;
} else if (StrChr(curdir, '#') != NULL ||
StrChr(curdir, '%') != NULL) {
/*
* If PWD has some unusual characters, construct a filename
* in temp where those are escaped. This is mostly to
* prevent this function from returning with some weird URL
* if the LYExpandHostForURL tests further down fail. - kw
*/
FREE(temp);
if (strcmp(cp2, temp2) == 0) {
/*
* LYTrimRelFromAbsPath did nothing, use old_string as
* given. - kw
*/
temp = HTEscape(curdir, URL_PATH);
LYAddHtmlSep(&temp);
StrAllocCat(temp, old_string);
} else {
temp = HTEscape(temp2, URL_PATH);
if (fragment != NULL) {
restorePoundSelector(fragment);
StrAllocCat(temp, fragment);
}
}
}
FREE(cp);
FREE(cp2);
}
if (is_local == FALSE) {
/*
* It's not an accessible subdirectory or file on the local
* system, so assume it's a URL request and guess the scheme
* with "http://" as the default.
*/
CTRACE((tfp, "Can't stat() or fopen() '%s'\n",
temp2 ? temp2 : temp));
#ifdef WIN_EX /* 1998/01/13 (Tue) 09:07:37 */
{
const char *p, *q;
char buff[LY_MAXPATH + 128];
p = Home_Dir();
q = temp2 ? temp2 : temp;
if (strlen(q) == 3 && LYIsDosDrive(q)) {
sprintf(buff,
"'%s' not exist, Goto LynxHome '%s'.", q, p);
_statusline(buff);
LYSleepAlert();
FREE(temp);
StrAllocCat(*AllocatedString, p);
goto Retry;
}
}
#endif
if (LYExpandHostForURL(&old_string,
URLDomainPrefixes,
URLDomainSuffixes)) {
LYAddSchemeForURL(&old_string);
StrAllocCopy(*AllocatedString, old_string);
} else if (fixit) {
StrAllocCopy(*AllocatedString, old_string);
} else {
/* Return file URL for the file that does not exist */
StrAllocCat(*AllocatedString, temp);
}
#ifdef WIN_EX
Retry:
#endif
CTRACE((tfp, "Trying: '%s'\n", *AllocatedString));
}
FREE(temp);
FREE(temp2);
}
#endif /* VMS */
} else {
/*
* Path begins with a slash. Simplify and use it.
*/
if (old_string[1] == '\0') {
/*
* Request for root. Respect it on Unix, but on VMS we treat that
* as a listing of the login directory. - FM
*/
#ifdef VMS
StrAllocCat(*AllocatedString, HTVMS_wwwName(Home_Dir()));
#else
StrAllocCat(*AllocatedString, "/");
} else if ((stat(old_string, &st) > -1) ||
LYCanReadFile(old_string)) {
/*
* It is an absolute directory or file on the local system. - KW
*/
StrAllocCopy(temp, old_string);
LYTrimRelFromAbsPath(temp);
CTRACE((tfp, "Converted '%s' to '%s'\n", old_string, temp));
cp = HTEscape(temp, URL_PATH);
StrAllocCat(*AllocatedString, cp);
FREE(cp);
FREE(temp);
CTRACE((tfp, "Converted '%s' to '%s'\n",
old_string, *AllocatedString));
#endif /* VMS */
} else if (LYIsTilde(old_string[1])) {
/*
* Has a Home_Dir() reference. Handle it as if there weren't a
* lead slash. - FM
*/
StrAllocCat(*AllocatedString, wwwName(Home_Dir()));
if ((cp = StrChr((old_string + 1), '/')) != NULL) {
/*
* Append rest of path, if present, skipping "user" if "~user"
* was entered, simplifying, and eliminating any residual
* relative elements. - FM
*/
StrAllocCopy(temp, cp);
LYTrimRelFromAbsPath(temp);
StrAllocCat(*AllocatedString, temp);
FREE(temp);
}
} else {
/*
* Normal absolute path. Simplify, trim any residual relative
* elements, and append it. - FM
*/
StrAllocCopy(temp, old_string);
LYTrimRelFromAbsPath(temp);
StrAllocCat(*AllocatedString, temp);
FREE(temp);
}
CTRACE((tfp, "Converted '%s' to '%s'\n",
old_string, *AllocatedString));
}
FREE(old_string);
/* Pause so we can read the messages before invoking curses */
CTRACE_SLEEP(AlertSecs);
}
#if defined(_WINDOWS) /* 1998/06/23 (Tue) 16:45:20 */
int win32_check_interrupt(void)
{
int c;
if (kbhit()) {
c = LYgetch();
/** Keyboard 'Z' or 'z', or Control-G or Control-C **/
if (LYCharIsINTERRUPT(c) || c == 0x1b) {
return TRUE;
}
}
return FALSE;
}
#if (!defined(__MINGW32__) && !defined(sleep)) || (defined(__MINGW32__) && !defined(HAVE_SLEEP))
void sleep(unsigned sec)
{
unsigned int i, j;
for (j = 0; j < sec; j++) {
for (i = 0; i < 10; i++) {
Sleep(100);
if (kbhit()) {
(void) LYgetch();
return;
}
}
}
}
#endif /* !__MINGW32__ */
#endif /* _WINDOWS */
/*
* This function expects an absolute Unix or VMS SHELL path spec as an
* allocated string, simplifies it, and trims out any residual relative
* elements. It also checks whether the path had a terminal slash, and if it
* didn't, makes sure that the simplified path doesn't either. If it's a
* directory, our convention is to exclude "Up to parent" links when a terminal
* slash is present. - FM
*/
void LYTrimRelFromAbsPath(char *path)
{
char *cp;
int i;
BOOL TerminalSlash;
/*
* Make sure we have a pointer to an absolute path. - FM
*/
if (path == NULL || !LYIsPathSep(*path))
return;
/*
* Check whether the path has a terminal slash. - FM
*/
TerminalSlash = (BOOL) (LYIsPathSep(path[(strlen(path) - 1)]));
/*
* Simplify the path and then do any necessary trimming. - FM
*/
HTSimplify(path);
cp = path;
while (cp[1] == '.') {
if (cp[2] == '\0') {
/*
* Eliminate trailing dot. - FM
*/
cp[1] = '\0';
} else if (LYIsPathSep(cp[2])) {
/*
* Skip over the "/." of a "/./". - FM
*/
cp += 2;
} else if (cp[2] == '.' && cp[3] == '\0') {
/*
* Eliminate trailing dotdot. - FM
*/
cp[1] = '\0';
} else if (cp[2] == '.' && cp[3] == '/') {
/*
* Skip over the "/.." of a "/../". - FM
*/
cp += 3;
} else {
/*
* Done trimming. - FM
*/
break;
}
}
/*
* Load any shifts into path, and eliminate any terminal slash created by
* HTSimplify() or our walk, but not present originally. - FM
*/
if (cp > path) {
for (i = 0; cp[i] != '\0'; i++)
path[i] = cp[i];
path[i] = '\0';
}
if (TerminalSlash == FALSE) {
LYTrimPathSep(path);
}
}
/*
* Example Client-Side Include interface.
*
* This is called from SGML.c and simply returns markup for reporting the URL
* of the document being loaded if a comment begins with "<!--#lynxCSI". The
* markup will be included as if it were in the document. Move this function
* to a separate module for doing this kind of thing seriously, someday. - FM
*/
void LYDoCSI(char *url,
const char *comment,
char **csi)
{
const char *cp = comment;
if (cp == NULL)
return;
if (StrNCmp(cp, "!--#", 4))
return;
cp += 4;
if (!strncasecomp(cp, "lynxCSI", 7)) {
StrAllocCat(*csi, "\n<p align=\"center\">URL: ");
StrAllocCat(*csi, url);
StrAllocCat(*csi, "</p>\n\n");
}
return;
}
#ifdef VMS
/*
* Define_VMSLogical -- Fote Macrides 04-Apr-1995
* Define VMS logicals in the process table.
*/
void Define_VMSLogical(char *LogicalName,
char *LogicalValue)
{
$DESCRIPTOR(lname, "");
$DESCRIPTOR(lvalue, "");
$DESCRIPTOR(ltable, "LNM$PROCESS");
if (isEmpty(LogicalName))
return;
lname.dsc$w_length = strlen(LogicalName);
lname.dsc$a_pointer = LogicalName;
if (isEmpty(LogicalValue)) {
lib$delete_logical(&lname, <able);
return;
}
lvalue.dsc$w_length = strlen(LogicalValue);
lvalue.dsc$a_pointer = LogicalValue;
lib$set_logical(&lname, &lvalue, <able, 0, 0);
return;
}
#endif /* VMS */
#ifdef LY_FIND_LEAKS
static void LYHomeDir_free(void)
{
FREE(HomeDir);
}
#endif /* LY_FIND_LEAKS */
char *Current_Dir(char *pathname)
{
char *result;
#ifdef HAVE_GETCWD
result = getcwd(pathname, (size_t) LY_MAXPATH);
#else
result = getwd(pathname);
#endif /* NO_GETCWD */
if (result == 0)
strcpy(pathname, ".");
return pathname;
}
/*
* Verify that the given path refers to an existing directory, returning the
* string if the directory exists. If not, return null.
*/
static char *CheckDir(char *path)
{
struct stat stat_info;
if (!LYisAbsPath(path)
|| (HTStat(path, &stat_info) < 0
|| !S_ISDIR(stat_info.st_mode))) {
path = NULL;
}
CTRACE((tfp, "CheckDir(%s) %s\n", path, path ? "OK" : "ERR"));
return path;
}
/*
* Lookup various possibilities for $HOME, and check that the directory exists.
*/
static char *HomeEnv(void)
{
char *result = CheckDir(LYGetEnv("HOME"));
#if defined (USE_DOS_DRIVES) && defined(_WIN32)
if (result == 0) {
char *head;
char *leaf;
static char *temp = NULL;
result = w32_get_shell_folder("Personal");
if (result == 0) {
/* Windows Vista/7 */
if ((head = LYGetEnv("USERPROFILE")) != 0) {
HTSprintf0(&temp, "%s%sDocuments", head, FILE_SEPARATOR);
result = CheckDir(temp);
if (result == 0) {
/* Windows 2000 */
HTSprintf0(&temp, "%s%sMy Documents", head, FILE_SEPARATOR);
result = CheckDir(temp);
}
}
}
/* NT4 */
if (result == 0) {
if ((head = LYGetEnv("HOMEDRIVE")) != 0) {
if ((leaf = LYGetEnv("HOMEPATH")) != 0) {
HTSprintf0(&temp, "%s%s%s", head, FILE_SEPARATOR, leaf);
result = CheckDir(temp);
}
}
}
/* General M$ */
#ifdef USE_PROGRAM_DIR
if (result == 0)
result = CheckDir(program_dir);
#endif
if (result == 0)
result = CheckDir(LYGetEnv("TEMP"));
if (result == 0)
result = CheckDir(LYGetEnv("TMP"));
if (result == 0) {
if ((head = LYGetEnv("SystemDrive")) != 0) {
HTSprintf0(&temp, "%s%s", head, FILE_SEPARATOR);
result = CheckDir(temp);
}
}
if (result == 0)
result = CheckDir("C:" FILE_SEPARATOR);
}
#endif
return result;
}
const char *Home_Dir(void)
{
static const char *homedir = NULL;
char *cp = NULL;
if (homedir == NULL) {
if ((cp = HomeEnv()) == NULL) {
#ifdef VMS
if ((cp = LYGetEnv("SYS$LOGIN")) == NULL
&& (cp = LYGetEnv("SYS$SCRATCH")) == NULL) {
cp = "sys$scratch:";
}
StrAllocCopy(HomeDir, cp);
#else
#ifdef UNIX
#ifdef HAVE_UTMP
/*
* One could use getlogin() and getpwnam() here instead.
*/
struct passwd *pw = getpwuid(geteuid());
if (pw && pw->pw_dir) {
StrAllocCopy(HomeDir, pw->pw_dir);
} else
#endif
{
/*
* Use /tmp; it should be writable.
*/
StrAllocCopy(HomeDir, "/tmp");
}
#endif
#endif /* VMS */
} else {
StrAllocCopy(HomeDir, cp);
}
homedir = (const char *) HomeDir;
#ifdef LY_FIND_LEAKS
atexit(LYHomeDir_free);
#endif
}
if (homedir == NULL) {
printf("%s\n", gettext("Cannot find HOME directory"));
exit_immediately(EXIT_FAILURE);
}
return homedir;
}
/*
* Return a pointer to the final leaf of the given pathname, If no pathname
* separators are found, returns the original pathname. The leaf may be
* empty.
*/
char *LYPathLeaf(char *pathname)
{
char *leaf;
#ifdef UNIX
if ((leaf = strrchr(pathname, '/')) != 0) {
leaf++;
}
#else
#ifdef VMS
if ((leaf = strrchr(pathname, ']')) == 0)
leaf = strrchr(pathname, ':');
if (leaf != 0)
leaf++;
#else
int n;
for (leaf = 0, n = (int) strlen(pathname) - 1; n >= 0; n--) {
if (StrChr("\\/:", pathname[n]) != 0) {
leaf = pathname + n + 1;
break;
}
}
#endif
#endif
return (leaf != 0) ? leaf : pathname;
}
/*
* This function checks the acceptability of file paths that are intended to be
* off the home directory. The file path should be passed in fbuffer, together
* with the size of the buffer. The function simplifies the file path, and if
* it is acceptable, loads it into fbuffer and returns TRUE. Otherwise, it
* does not modify fbuffer and returns FALSE. If a subdirectory is present and
* the path does not begin with "./", that is prefixed to make the situation
* clear. - FM
*/
BOOLEAN LYPathOffHomeOK(char *fbuffer,
size_t fbuffer_size)
{
char *file = NULL;
char *cp, *cp1;
/*
* Make sure we have an fbuffer and a string in it. - FM
*/
if (fbuffer_size < 2 || isEmpty(fbuffer)) {
return (FALSE);
}
StrAllocCopy(file, fbuffer);
cp = file;
/*
* Check for an inappropriate reference to the home directory, and correct
* it if we can. - FM
*/
#ifdef VMS
if (!strncasecomp(cp, "sys$login", 9)) {
if (*(cp + 9) == '\0') {
/*
* Reject "sys$login". - FM
*/
FREE(file);
return (FALSE);
}
if (*(cp + 9) == ':') {
cp += 10;
if (*cp == '\0') {
/*
* Reject "sys$login:". Otherwise, we have converted
* "sys$login:file" to "file", or have left a strange path for
* VMS as it was originally. - FM
*/
FREE(file);
return (FALSE);
}
}
}
#endif /* VMS */
if (LYIsTilde(cp[0])) {
if (LYIsPathSep(cp[1])) {
if (cp[2] != '\0') {
if (StrChr((cp + 2), '/') != NULL) {
/*
* Convert "~/subdir(s)/file" to "./subdir(s)/file". - FM
*/
*cp = '.';
} else {
/*
* Convert "~/file" to "file". - FM
*/
cp += 2;
}
} else {
/*
* Reject "~/". - FM
*/
FREE(file);
return (FALSE);
}
} else if ((*(cp + 1) != '\0') &&
(cp1 = StrChr((cp + 1), '/')) != NULL) {
cp = (cp1 - 1);
if (*(cp + 2) != '\0') {
if (StrChr((cp + 2), '/') != NULL) {
/*
* Convert "~user/subdir(s)/file" to "./subdir(s)/file".
* If user is someone else, we covered a spoof. Otherwise,
* we simplified. - FM
*/
*cp = '.';
} else {
/*
* Convert "~user/file" to "file". - FM
*/
cp += 2;
}
} else {
/*
* Reject "~user/". - FM
*/
FREE(file);
return (FALSE);
}
} else {
/*
* Reject "~user". - FM
*/
FREE(file);
return (FALSE);
}
}
#ifdef VMS
/*
* Check for VMS path specs, and reject if still present. - FM
*/
if (StrChr(cp, ':') != NULL || StrChr(cp, ']') != NULL) {
FREE(file);
return (FALSE);
}
#endif /* VMS */
/*
* Check for a URL or absolute path, and reject if present. - FM
*/
if (is_url(cp) || LYIsPathSep(*cp)) {
FREE(file);
return (FALSE);
}
/*
* Simplify it. - FM
*/
HTSimplify(cp);
/*
* Check if it has a pointless "./". - FM
*/
if (!StrNCmp(cp, "./", 2)) {
if (StrChr((cp + 2), '/') == NULL) {
cp += 2;
}
}
/*
* Check for spoofing. - FM
*/
if (*cp == '\0'
|| LYIsPathSep(*cp)
|| LYIsPathSep(cp[(strlen(cp) - 1)])
|| strstr(cp, "..") != NULL
|| !strcmp(cp, ".")) {
FREE(file);
return (FALSE);
}
/*
* Load what we have at this point into fbuffer, trimming if too long, and
* claim it's OK. - FM
*/
if (fbuffer_size > 3 && StrNCmp(cp, "./", 2) && StrChr(cp, '/')) {
/*
* We have a subdirectory and no lead "./", so prefix it to make the
* situation clear. - FM
*/
strcpy(fbuffer, "./");
if (strlen(cp) > (fbuffer_size - 3))
cp[(fbuffer_size - 3)] = '\0';
strcat(fbuffer, cp);
} else {
if (strlen(cp) > (fbuffer_size - 1))
cp[(fbuffer_size - 1)] = '\0';
strcpy(fbuffer, cp);
}
FREE(file);
return (TRUE);
}
/*
* Search for a leading tilde, optionally embedded. If found, return a pointer
* to the tilde. If not found, return the original parameter.
*/
static char *FindLeadingTilde(char *pathname, int embedded)
{
char *result = pathname;
if (pathname != NULL) {
if (embedded) {
while (pathname[0] != '\0') {
if (LYIsPathSep(pathname[0])) {
if (LYIsTilde(pathname[1])) {
++pathname;
break;
}
}
++pathname;
}
}
if (LYIsTilde(*pathname))
result = pathname;
}
return result;
}
/*
* Convert a non-absolute path to one which is off the home directory. Expand
* tildes as a side-effect. Return a pointer to the converted result.
*/
char *LYAbsOrHomePath(char **fname)
{
if (*fname && !LYisAbsPath(*fname)) {
if (LYIsTilde((*fname)[0])) {
LYTildeExpand(fname, FALSE);
} else {
char temp[LY_MAXPATH];
LYAddPathToHome(temp, sizeof(temp), *fname);
StrAllocCopy(*fname, temp);
}
}
return *fname;
}
/*
* Expand a "leading" tilde into the user's home directory in WWW format. If
* "embedded" is true, allow that "leading" tilde to follow a path separator.
*/
char *LYTildeExpand(char **pathname,
int embedded)
{
char *temp = FindLeadingTilde(*pathname, embedded);
if (LYIsTilde(temp[0])) {
CTRACE((tfp, "LYTildeExpand %s\n", *pathname));
if (LYIsPathSep(temp[1])) {
char *first = NULL;
char *second = NULL;
StrAllocCopy(first, *pathname);
first[temp - *pathname] = '\0';
StrAllocCopy(second, temp + 2);
StrAllocCopy(*pathname, first);
StrAllocCat(*pathname, wwwName(Home_Dir()));
LYAddPathSep(pathname);
StrAllocCat(*pathname, second);
FREE(first);
FREE(second);
} else if (temp[1] == '\0') {
StrAllocCopy(*pathname, wwwName(Home_Dir()));
#ifndef NOUSERS
} else {
char *save;
char saved = '\0';
struct passwd *pw;
for (save = temp; *save != '\0'; ++save) {
if (LYIsPathSep(*save)) {
saved = *save;
*save = '\0';
break;
}
}
pw = getpwnam(temp + 1);
*save = saved;
if (pw != 0 && non_empty(pw->pw_dir)) {
temp = NULL;
StrAllocCopy(temp, save);
StrAllocCopy(*pathname, pw->pw_dir);
StrAllocCat(*pathname, temp);
FREE(temp);
}
#endif
}
CTRACE((tfp, "expanded path %s\n", *pathname));
}
return *pathname;
}
/*
* This function appends fname to the home path and returns the full path and
* filename. The fname string can be just a filename (e.g.,
* "lynx_bookmarks.html"), or include a subdirectory off the home directory, in
* which case fname should begin with "./" (e.g., ./BM/lynx_bookmarks.html) Use
* LYPathOffHomeOK() to check and/or fix up fname before calling this function.
* On VMS, the resultant full path and filename are converted to VMS syntax. -
* FM
*/
void LYAddPathToHome(char *fbuffer,
size_t fbuffer_size,
const char *fname)
{
char *home = NULL;
const char *file = fname;
int len;
/*
* Make sure we have a buffer. - FM
*/
if (!fbuffer)
return;
if (fbuffer_size < 2) {
fbuffer[0] = '\0';
return;
}
fbuffer[(fbuffer_size - 1)] = '\0';
/*
* Make sure we have a file name. - FM
*/
if (!file)
file = "";
/*
* Set up home string and length. - FM
*/
StrAllocCopy(home, Home_Dir());
#ifdef VMS
#define NO_HOMEPATH "Error:"
#else
#define NO_HOMEPATH "/error"
#endif /* VMS */
if (!non_empty(home))
/*
* Home_Dir() has a bug if this ever happens. - FM
*/
StrAllocCopy(home, NO_HOMEPATH);
len = (int) fbuffer_size - ((int) strlen(home) + 1);
if (len <= 0) {
/*
* Buffer is smaller than or only big enough for the home path. Load
* what fits of the home path and return. This will fail, but we need
* something in the buffer. - FM
*/
LYStrNCpy(fbuffer, home, (fbuffer_size - 1));
FREE(home);
return;
}
#ifdef VMS
/*
* Check whether we have a subdirectory path or just a filename. - FM
*/
if (!StrNCmp(file, "./", 2)) {
/*
* We have a subdirectory path. - FM
*/
if (home[strlen(home) - 1] == ']') {
/*
* We got the home directory, so convert it to SHELL syntax and
* append subdirectory path, then convert that to VMS syntax. - FM
*/
char *temp = NULL;
HTSprintf0(&temp, "%s%s", HTVMS_wwwName(home), (file + 1));
sprintf(fbuffer, "%.*s",
(fbuffer_size - 1), HTVMS_name("", temp));
FREE(temp);
} else {
/*
* This will fail, but we need something in the buffer. - FM
*/
sprintf(fbuffer, "%s%.*s", home, len, file);
}
} else {
/*
* We have a file in the home directory. - FM
*/
sprintf(fbuffer, "%s%.*s", home, len, file);
}
#else
/*
* Check whether we have a subdirectory path or just a filename. - FM
*/
sprintf(fbuffer, "%s/%.*s", home, len,
(StrNCmp(file, "./", 2) ? file : (file + 2)));
#endif /* VMS */
FREE(home);
}
/*
* Given a filename, concatenate it to the save-space pathname, unless it is
* an absolute pathname. If there is no save-space defined, use the home
* directory. Return a new string with the result.
*/
char *LYAddPathToSave(char *fname)
{
char *result = NULL;
if (LYisAbsPath(fname)) {
StrAllocCopy(result, fname);
} else {
if (non_empty(lynx_save_space)) {
StrAllocCopy(result, lynx_save_space);
} else {
char temp[LY_MAXPATH];
LYAddPathToHome(temp, sizeof(temp), fname);
StrAllocCopy(result, temp);
}
}
return result;
}
#if !defined(HAVE_PUTENV) && !defined(_WINDOWS)
/*
* No putenv on the NeXT so we use this code instead!
*/
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#if defined(STDC_HEADERS) || defined(USG)
#include <string.h>
#else /* Not (STDC_HEADERS or USG): */
#include <strings.h>
#endif /* STDC_HEADERS or USG */
#ifndef NULL
#define NULL 0
#endif /* !NULL */
extern char **environ;
/*
* Put STRING, which is of the form "NAME=VALUE", in the environment.
*/
int putenv(const char *string)
{
char *name_end = StrChr(string, '=');
register size_t size;
register char **ep;
if (name_end == NULL) {
/* Remove the variable from the environment. */
size = strlen(string);
for (ep = environ; *ep != NULL; ++ep)
if (!StrNCmp(*ep, string, size) && (*ep)[size] == '=') {
while (ep[1] != NULL) {
ep[0] = ep[1];
++ep;
}
*ep = NULL;
return 0;
}
}
size = 0;
for (ep = environ; *ep != NULL; ++ep)
if (!StrNCmp(*ep, string, name_end - string) &&
(*ep)[name_end - string] == '=')
break;
else
++size;
if (*ep == NULL) {
static char **last_environ = NULL;
char **new_environ = (char **) malloc((size + 2) * sizeof(char *));
if (new_environ == NULL)
return -1;
(void) memcpy((char *) new_environ, (char *) environ, size * sizeof(char *));
new_environ[size] = (char *) string;
new_environ[size + 1] = NULL;
if (last_environ != NULL)
FREE(last_environ);
last_environ = new_environ;
environ = new_environ;
} else
*ep = (char *) string;
return 0;
}
#endif /* !HAVE_PUTENV */
#ifdef NEED_REMOVE
int remove(char *name)
{
return unlink(name);
}
#endif
#if defined(MULTI_USER_UNIX)
#if defined(HAVE_LSTAT) && defined(S_IFLNK)
/*
* If IsOurFile() is checking a symbolic link, ensure that the target
* points to the user's file as well.
*/
static BOOL IsOurSymlink(const char *name)
{
BOOL result = FALSE;
size_t size = LY_MAXPATH;
size_t used;
char *buffer = typeMallocn(char, (unsigned) size);
char *check;
if (buffer != 0) {
while ((used = (size_t) readlink(name, buffer, (size - 1))) == size - 1) {
check = typeRealloc(char, buffer, (unsigned) (size *= 2));
if (check == 0)
break;
buffer = check;
}
if (buffer != 0) {
if ((int) used > 0) {
buffer[used] = '\0';
} else {
FREE(buffer);
}
}
}
if (buffer != 0) {
if (!LYisAbsPath(buffer)) {
char *cutoff = LYLastPathSep(name);
char *clone = NULL;
if (cutoff != 0) {
HTSprintf0(&clone, "%.*s%s%s",
(int) (cutoff - name),
name, FILE_SEPARATOR, buffer);
FREE(buffer);
buffer = clone;
}
}
CTRACE2(TRACE_CFG, (tfp, "IsOurSymlink(%s -> %s)\n", name, buffer));
result = IsOurFile(buffer);
FREE(buffer);
}
return result;
}
#endif
/*
* Verify if this is really a file which is not accessed by a symbolic link,
* except for the special case of its directory being pointed to by a link from
* a directory owned by root and not writable by other users.
*/
BOOL IsOurFile(const char *name)
{
BOOL result = FALSE;
struct stat data;
if (!LYIsTilde(name[0])
&& lstat(name, &data) == 0
&& ((S_ISREG(data.st_mode)
&& (data.st_mode & (S_IWOTH | S_IWGRP)) == 0
&& data.st_uid == getuid())
#if defined(HAVE_LSTAT) && defined(S_IFLNK)
|| (S_ISLNK(data.st_mode) && IsOurSymlink(name))
#endif
)) {
int linked = FALSE;
/*
* ( If this is not a single-user system, the other user is presumed by
* some people busy trying to use a symlink attack on our files ;-)
*/
#if defined(HAVE_LSTAT)
char *path = 0;
char *leaf;
StrAllocCopy(path, name);
do {
if ((leaf = LYPathLeaf(path)) != path)
*--leaf = '\0'; /* write a null on the '/' */
if (lstat(*path ? path : "/", &data) != 0) {
break;
}
/*
* If we find a symbolic link, it has to be in a directory that's
* protected. Otherwise someone could have switched it to point
* to one of the real user's files.
*/
if (S_ISLNK(data.st_mode)) {
linked = TRUE; /* could be link-to-link; doesn't matter */
} else if (S_ISDIR(data.st_mode)) {
if (linked) {
linked = FALSE;
/*
* We assume that a properly-configured system has the
* unwritable directories owned by root. This is not
* necessarily so (bin, news, etc., may), but the only
* uid we can count on is 0. It would be nice to add a
* check for the gid also, but that wouldn't be
* portable.
*
* Likewise, the t-bit would be nice to rely upon. However
* it is marked as an extension in POSIX, rather than
* required.
*/
if (data.st_uid != 0
|| (data.st_mode & S_IWOTH) != 0) {
linked = TRUE; /* force an error-return */
break;
}
}
} else if (linked) {
break;
}
} while (leaf != path);
FREE(path);
#endif
result = (BOOLEAN) !linked;
}
CTRACE2(TRACE_CFG, (tfp, "IsOurFile(%s) %d\n", name, result));
return result;
}
/*
* Open a file that we don't want other users to see.
*/
static FILE *OpenHiddenFile(const char *name, const char *mode)
{
FILE *fp = 0;
struct stat data;
BOOLEAN binary = (BOOLEAN) (StrChr(mode, 'b') != 0);
#if defined(O_CREAT) && defined(O_EXCL) /* we have fcntl.h or kindred? */
/*
* This is the preferred method for creating new files, since it ensures
* that no one has an existing file or link that they happen to own.
*/
if (*mode == 'w') {
int fd = open(name, O_CREAT | O_EXCL | O_WRONLY, HIDE_CHMOD);
if (fd < 0
&& errno == EEXIST
&& IsOurFile(name)) {
if (remove(name) == 0) {
/* FIXME: there's a race at this point if directory is open */
fd = open(name, O_CREAT | O_EXCL | O_WRONLY, HIDE_CHMOD);
}
}
if (fd >= 0) {
#if defined(O_BINARY) && defined(__CYGWIN__)
if (binary)
setmode(fd, O_BINARY);
#endif
fp = fdopen(fd, mode);
}
} else
#endif
if (*mode == 'a') {
if (IsOurFile(name)
&& chmod(name, HIDE_CHMOD) == 0)
fp = fopen(name, mode);
else if (lstat(name, &data) != 0)
fp = OpenHiddenFile(name, binary ? BIN_W : TXT_W);
/*
* This is less stringent, but reasonably portable. For new files, the
* umask will suffice; however if the file already exists we'll change
* permissions first, before opening it. If the chmod fails because of
* some reason other than a non-existent file, there's no point in trying
* to open it.
*
* This won't work properly if the user is root, since the chmod succeeds.
*/
} else if (*mode != 'a') {
mode_t save = umask(HIDE_UMASK);
if (chmod(name, HIDE_CHMOD) == 0 || errno == ENOENT)
fp = fopen(name, mode);
(void) umask(save);
}
return fp;
}
#else
#define OpenHiddenFile(name, mode) fopen(name, mode)
#endif /* MULTI_USER_UNIX */
FILE *LYNewBinFile(const char *name)
{
#ifdef VMS
FILE *fp = fopen(name, BIN_W, "mbc=32");
(void) chmod(name, HIDE_CHMOD);
#else
FILE *fp = OpenHiddenFile(name, BIN_W);
#endif
return fp;
}
FILE *LYNewTxtFile(const char *name)
{
FILE *fp;
#ifdef VMS
fp = fopen(name, TXT_W, "shr=get");
(void) chmod(name, HIDE_CHMOD);
#else
SetDefaultMode(O_TEXT);
fp = OpenHiddenFile(name, TXT_W);
SetDefaultMode(O_BINARY);
#endif
return fp;
}
FILE *LYAppendToTxtFile(const char *name)
{
FILE *fp;
#ifdef VMS
fp = fopen(name, TXT_A, "shr=get");
(void) chmod(name, HIDE_CHMOD);
#else
SetDefaultMode(O_TEXT);
fp = OpenHiddenFile(name, TXT_A);
SetDefaultMode(O_BINARY);
#endif
return fp;
}
#if defined(MULTI_USER_UNIX)
/*
* Restore normal permissions to a copy of a file that we have created with
* temp file restricted permissions. The normal umask should apply for user
* files. - kw
*/
void LYRelaxFilePermissions(const char *name)
{
mode_t mode;
struct stat stat_buf;
if (stat(name, &stat_buf) == 0 &&
S_ISREG(stat_buf.st_mode) &&
(mode = (stat_buf.st_mode & 0777)) == HIDE_CHMOD) {
/*
* It looks plausible that this is a file we created with temp file
* paranoid permissions (and the umask wasn't even more restrictive
* when it was copied). - kw
*/
mode_t save = umask(HIDE_UMASK);
mode = (mode_t) (((mode & 0700) | 0066) & ~save);
(void) umask(save);
(void) chmod(name, mode);
}
}
#endif
/*
* Check if the given anchor has an associated file-cache.
*/
BOOLEAN LYCachedTemp(char *target,
char **cached)
{
BOOLEAN result = FALSE;
if (*cached) {
LYStrNCpy(target, *cached, LY_MAXPATH);
FREE(*cached);
if (LYCanReadFile(target)) {
if (remove(target) != 0) {
CTRACE((tfp, "cannot remove %s\n", target));
}
}
result = TRUE;
}
return result;
}
#ifndef HAVE_MKDTEMP
#define mkdtemp(path) ((mktemp(path) != 0) && (mkdir(path, 0700) == 0))
#endif
/*
* Open a temp-file, ensuring that it is unique, and not readable by other
* users.
*
* The mode can be one of: "w", "a", "wb".
*/
FILE *LYOpenTemp(char *result,
const char *suffix,
const char *mode)
{
FILE *fp = 0;
BOOL txt = TRUE;
char wrt = 'r';
LY_TEMP *p;
CTRACE((tfp, "LYOpenTemp(,%s,%s)\n", suffix, mode));
if (result == 0)
return 0;
while (*mode != '\0') {
switch (*mode++) {
case 'w':
wrt = 'w';
break;
case 'a':
wrt = 'a';
break;
case 'b':
txt = FALSE;
break;
default:
CTRACE((tfp, "%s @%d: BUG\n", __FILE__, __LINE__));
return 0;
}
}
/*
* Verify if the given space looks secure enough. Otherwise, make a
* secure subdirectory of that.
*/
#if defined(MULTI_USER_UNIX) && (defined(HAVE_MKTEMP) || defined(HAVE_MKDTEMP))
if (lynx_temp_subspace == 0) {
BOOL make_it = FALSE;
struct stat sb;
if (lstat(lynx_temp_space, &sb) == 0
&& S_ISDIR(sb.st_mode)) {
if (sb.st_uid != getuid()
|| (sb.st_mode & (S_IWOTH | S_IWGRP)) != 0) {
make_it = TRUE;
CTRACE((tfp,
"lynx_temp_space is not our directory %s owner %d mode %03o\n",
lynx_temp_space, (int) sb.st_uid, (int) sb.st_mode & 0777));
}
} else {
make_it = TRUE;
CTRACE((tfp, "lynx_temp_space is not a directory %s\n", lynx_temp_space));
}
if (make_it) {
mode_t old_mask = umask(HIDE_UMASK);
StrAllocCat(lynx_temp_space, "lynxXXXXXXXXXX");
if (mkdtemp(lynx_temp_space) == 0) {
printf("%s: %s\n", lynx_temp_space, LYStrerror(errno));
exit_immediately(EXIT_FAILURE);
}
(void) umask(old_mask);
lynx_temp_subspace = 1;
StrAllocCat(lynx_temp_space, "/");
CTRACE((tfp, "made subdirectory %s\n", lynx_temp_space));
} else {
lynx_temp_subspace = -1;
}
}
#endif
do {
if (!fmt_tempname(result, lynx_temp_space, suffix))
return 0;
if (txt) {
switch (wrt) {
case 'w':
fp = LYNewTxtFile(result);
break;
case 'a':
fp = LYAppendToTxtFile(result);
break;
}
} else {
fp = LYNewBinFile(result);
}
/*
* If we get a failure to make a temporary file, don't bother to try a
* different name unless the failure was because the file already
* exists.
*/
#ifdef EEXIST /* FIXME (need a better test) in fcntl.h or unistd.h */
if ((fp == 0) && (errno != EEXIST)) {
CTRACE((tfp, "... LYOpenTemp(%s) failed: %s\n",
result, LYStrerror(errno)));
return 0;
}
#endif
} while (fp == 0);
if ((p = typecalloc(LY_TEMP)) != 0) {
p->next = ly_temp;
StrAllocCopy((p->name), result);
p->file = fp;
p->outs = (BOOLEAN) (wrt != 'r');
ly_temp = p;
} else {
outofmem(__FILE__, "LYOpenTemp");
}
CTRACE((tfp, "... LYOpenTemp(%s)\n", result));
return fp;
}
/*
* Reopen a temporary file
*/
FILE *LYReopenTemp(char *name)
{
LY_TEMP *p;
FILE *fp = 0;
LYCloseTemp(name);
if ((p = FindTempfileByName(name)) != 0) {
fp = p->file = LYAppendToTxtFile(name);
}
return fp;
}
/*
* Open a temp-file for writing, possibly re-using a previously used
* name and file.
* If a non-empty fname is given, it is reused if it indicates a file
* previously registered as a temp file and, in case the file still
* exists, if it looks like we can write to it safely. Otherwise a
* new temp file (with new name) will be generated and returned in fname.
*
* File permissions are set so that the file is not readable by unprivileged
* other users.
*
* Suffix is only used if fname is not being reused.
* The mode should be "w", others are possible (they may be passed on)
* but probably don't make sense. - kw
*/
FILE *LYOpenTempRewrite(char *fname,
const char *suffix,
const char *mode)
{
FILE *fp = 0;
BOOL txt = TRUE;
char wrt = 'r';
BOOL registered = NO;
BOOL writable_exists = NO;
BOOL is_ours = NO;
BOOL still_open = NO;
LY_TEMP *p;
struct stat stat_buf;
CTRACE((tfp, "LYOpenTempRewrite(%s,%s,%s)\n", fname, suffix, mode));
if (*fname == '\0') /* first time, no filename yet */
return (LYOpenTemp(fname, suffix, mode));
if ((p = FindTempfileByName(fname)) != 0) {
registered = YES;
if (p->file != 0)
still_open = YES;
CTRACE((tfp, "...used before%s\n", still_open ? ", still open!" : "."));
}
if (registered) {
#ifndef NO_GROUPS
writable_exists = HTEditable(fname); /* existing, can write */
#define CTRACE_EXISTS "exists and is writable, "
#else
writable_exists = (BOOL) (stat(fname, &stat_buf) == 0); /* existing, assume can write */
#define CTRACE_EXISTS "exists, "
#endif
if (writable_exists) {
is_ours = IsOurFile(fname);
}
CTRACE((tfp, "...%s%s\n",
writable_exists ? CTRACE_EXISTS : "",
is_ours ? "is our file." : "is NOT our file."));
}
/*
* Note that in cases where LYOpenTemp is called as fallback below, we
* don't call LYRemoveTemp first. That may be appropriate in some cases,
* but not trying to remove a weird existing file seems safer and could
* help diagnose an unusual situation. (They may be removed anyway later.)
*/
if (still_open) {
/*
* This should probably not happen. Make a new one.
*/
return (LYOpenTemp(fname, suffix, mode));
} else if (!registered) {
/*
* Not registered. It should have been registered at one point though,
* otherwise we wouldn't be called like this.
*/
return (LYOpenTemp(fname, suffix, mode));
} else if (writable_exists && !is_ours) {
/*
* File exists, writable if we checked, but something is wrong with it.
*/
return (LYOpenTemp(fname, suffix, mode));
#ifndef NO_GROUPS
} else if (!is_ours && (lstat(fname, &stat_buf) == 0)) {
/*
* Exists but not writable, and something is wrong with it.
*/
return (LYOpenTemp(fname, suffix, mode));
#endif
}
while (*mode != '\0') {
switch (*mode++) {
case 'w':
wrt = 'w';
break;
case 'a':
wrt = 'a';
break;
case 'b':
txt = FALSE;
break;
default:
CTRACE((tfp, "%s @%d: BUG\n", __FILE__, __LINE__));
return fp;
}
}
if (is_ours) {
/*
* Yes, it exists, is writable if we checked, and everything looks ok
* so far. This should be the most regular case. - kw
*/
#ifdef HAVE_TRUNCATE
if (txt == TRUE) { /* limitation of LYReopenTemp. shrug */
/*
* We truncate and then append, this avoids having a small window
* in which the file doesn't exist. - kw
*/
if (truncate(fname, (off_t) 0) != 0) {
CTRACE((tfp, "... truncate(%s,0) failed: %s\n",
fname, LYStrerror(errno)));
return (LYOpenTemp(fname, suffix, mode));
} else {
return (LYReopenTemp(fname));
}
}
#endif
remove(fname);
}
/* We come here in two cases: either the file existed and was ours and we
* just got rid of it. Or the file did and does not exist, but is
* registered as a temp file. It must have been removed by some means
* other than LYRemoveTemp. In both cases, reuse the name! - kw
*/
if (txt) {
switch (wrt) {
case 'w':
fp = LYNewTxtFile(fname);
break;
case 'a':
fp = LYAppendToTxtFile(fname);
break;
}
} else {
fp = LYNewBinFile(fname);
}
p->file = fp;
CTRACE((tfp, "... LYOpenTempRewrite(%s), %s\n", fname,
(fp) ? "ok" : "failed"));
/*
* We could fall back to trying LYOpenTemp() here in case of failure.
* After all the checks already done above a filure here should be pretty
* unusual though, so maybe it's better to let the user notice that
* something went wrong, and not try to fix it up. - kw
*/
return fp;
}
/*
* Special case of LYOpenTemp, used for manipulating bookmark file, i.e., with
* renaming.
*/
FILE *LYOpenScratch(char *result,
const char *prefix)
{
FILE *fp;
LY_TEMP *p;
if (!fmt_tempname(result, prefix, HTML_SUFFIX))
return 0;
if ((fp = LYNewTxtFile(result)) != 0) {
if ((p = typecalloc(LY_TEMP)) != 0) {
p->next = ly_temp;
StrAllocCopy((p->name), result);
p->file = fp;
ly_temp = p;
} else {
outofmem(__FILE__, "LYOpenScratch");
}
}
CTRACE((tfp, "LYOpenScratch(%s)\n", result));
return fp;
}
static void LY_close_temp(LY_TEMP * p)
{
if (p->file != 0) {
if (p->outs) {
LYCloseOutput(p->file);
} else {
LYCloseInput(p->file);
}
p->file = 0;
}
}
/*
* Close a temp-file, given its name
*/
void LYCloseTemp(char *name)
{
LY_TEMP *p;
CTRACE((tfp, "LYCloseTemp(%s)\n", name));
if ((p = FindTempfileByName(name)) != 0) {
CTRACE((tfp, "...LYCloseTemp(%s)%s\n", name,
(p->file != 0) ? ", closed" : ""));
LY_close_temp(p);
}
}
/*
* Close a temp-file, given its file-pointer
*/
void LYCloseTempFP(FILE *fp)
{
LY_TEMP *p;
CTRACE((tfp, "LYCloseTempFP\n"));
if ((p = FindTempfileByFP(fp)) != 0) {
LY_close_temp(p);
CTRACE((tfp, "...LYCloseTempFP(%s)\n", p->name));
}
}
/*
* Close a temp-file, removing it.
*/
int LYRemoveTemp(char *name)
{
LY_TEMP *p, *q;
int code = -1;
if (non_empty(name)) {
CTRACE((tfp, "LYRemoveTemp(%s)\n", name));
for (p = ly_temp, q = 0; p != 0; q = p, p = p->next) {
if (!strcmp(name, p->name)) {
if (q != 0) {
q->next = p->next;
} else {
ly_temp = p->next;
}
LY_close_temp(p);
code = HTSYS_remove(name);
CTRACE((tfp, "...LYRemoveTemp done(%d)%s\n", code,
(p->file != 0) ? ", closed" : ""));
CTRACE_FLUSH(tfp);
FREE(p->name);
FREE(p);
break;
}
}
}
return code;
}
/*
* Remove all of the temp-files. Note that this assumes that they are closed,
* since some systems will not allow us to remove a file which is open.
*/
void LYCleanupTemp(void)
{
while (ly_temp != 0) {
(void) LYRemoveTemp(ly_temp->name);
}
#if defined(MULTI_USER_UNIX)
if (lynx_temp_subspace > 0) {
char result[LY_MAXPATH];
LYStrNCpy(result, lynx_temp_space, sizeof(result) - 1);
LYTrimPathSep(result);
CTRACE((tfp, "LYCleanupTemp removing %s\n", result));
rmdir(result);
lynx_temp_subspace = -1;
}
#endif
}
/*
* We renamed a temporary file. Keep track so we can remove it on exit.
*/
void LYRenamedTemp(char *oldname,
char *newname)
{
LY_TEMP *p;
CTRACE((tfp, "LYRenamedTemp(old=%s, new=%s)\n", oldname, newname));
if ((p = FindTempfileByName(oldname)) != 0) {
StrAllocCopy((p->name), newname);
}
}
#ifndef DISABLE_BIBP
/*
* Check that bibhost defines the BibP icon.
*/
void LYCheckBibHost(void)
{
DocAddress bibhostIcon;
BOOLEAN saveFlag;
bibhostIcon.address = NULL;
StrAllocCopy(bibhostIcon.address, BibP_bibhost);
StrAllocCat(bibhostIcon.address, "bibp1.0/bibpicon.jpg");
bibhostIcon.post_data = NULL;
bibhostIcon.post_content_type = NULL;
bibhostIcon.bookmark = FALSE;
bibhostIcon.isHEAD = FALSE;
bibhostIcon.safe = FALSE;
saveFlag = traversal;
traversal = TRUE; /* Hack to force error response. */
BibP_bibhost_available = (BOOLEAN) (HTLoadAbsolute(&bibhostIcon) == YES);
traversal = saveFlag;
BibP_bibhost_checked = TRUE;
}
#endif /* !DISABLE_BIBP */
/*
* Management of User Interface Pages. - kw
*
* These are mostly temp files. Pages which can be recognized by their special
* URL (after having been loaded) need not be tracked here.
*
* First some private stuff:
*/
typedef struct uipage_entry {
UIP_t type;
unsigned flags;
char *url;
HTList *alturls;
char *file;
} uip_entry;
#define UIP_F_MULTI 0x0001 /* flag: track multiple instances */
#define UIP_F_LIMIT 0x0002 /* flag: limit size of alturls list */
#define UIP_F_LMULTI (UIP_F_MULTI | UIP_F_LIMIT)
/* *INDENT-OFF* */
static uip_entry ly_uip[] =
{
{ UIP_HISTORY , UIP_F_LMULTI, NULL, NULL, NULL }
, { UIP_DOWNLOAD_OPTIONS , 0 , NULL, NULL, NULL }
, { UIP_PRINT_OPTIONS , 0 , NULL, NULL, NULL }
, { UIP_SHOWINFO , UIP_F_LMULTI, NULL, NULL, NULL }
, { UIP_LIST_PAGE , UIP_F_LMULTI, NULL, NULL, NULL }
, { UIP_VLINKS , UIP_F_LMULTI, NULL, NULL, NULL }
#if !defined(NO_OPTION_FORMS)
, { UIP_OPTIONS_MENU , UIP_F_LMULTI, NULL, NULL, NULL }
#endif
#ifdef DIRED_SUPPORT
, { UIP_DIRED_MENU , 0 , NULL, NULL, NULL }
, { UIP_PERMIT_OPTIONS , 0 , NULL, NULL, NULL }
, { UIP_UPLOAD_OPTIONS , UIP_F_LMULTI, NULL, NULL, NULL }
#endif
#ifdef USE_ADDRLIST_PAGE
, { UIP_ADDRLIST_PAGE , UIP_F_LMULTI, NULL, NULL, NULL }
#endif
, { UIP_LYNXCFG , UIP_F_LMULTI, NULL, NULL, NULL }
#if !defined(NO_CONFIG_INFO)
, { UIP_CONFIG_DEF , UIP_F_LMULTI, NULL, NULL, NULL }
#endif
/* The following are not generated tempfiles: */
, { UIP_TRACELOG , 0 , NULL, NULL, NULL }
#if defined(DIRED_SUPPORT) && defined(OK_INSTALL)
, { UIP_INSTALL , 0 , NULL, NULL, NULL }
#endif
};
/* *INDENT-ON* */
/* Public entry points for User Interface Page management: */
BOOL LYIsUIPage3(const char *url,
UIP_t type,
int flagparam)
{
unsigned int i;
size_t l;
BOOL result = NO;
if (url) {
for (i = 0; i < TABLESIZE(ly_uip); i++) {
if (ly_uip[i].type == type) {
if (!ly_uip[i].url) {
break;
} else if ((flagparam & UIP_P_FRAG) ?
(!StrNCmp(ly_uip[i].url, url, (l = strlen(ly_uip[i].url)))
&& (url[l] == '\0' || url[l] == '#')) :
!strcmp(ly_uip[i].url, url)) {
result = YES;
} else if (ly_uip[i].flags & UIP_F_MULTI) {
char *p;
HTList *l0 = ly_uip[i].alturls;
while ((p = (char *) HTList_nextObject(l0)) != NULL) {
if ((flagparam & UIP_P_FRAG)
? (!StrNCmp(p, url, (l = strlen(p))) &&
(url[l] == '\0' || url[l] == '#'))
: !strcmp(p, url)) {
result = YES;
break;
}
}
}
break;
}
}
}
return result;
}
void LYRegisterUIPage(const char *url,
UIP_t type)
{
unsigned int i;
for (i = 0; i < TABLESIZE(ly_uip); i++) {
if (ly_uip[i].type == type) {
if (ly_uip[i].url && url &&
!strcmp(ly_uip[i].url, url)) {
} else if (!ly_uip[i].url || !url ||
!(ly_uip[i].flags & UIP_F_MULTI)) {
StrAllocCopy(ly_uip[i].url, url);
} else {
char *p;
int n = 0;
HTList *l0 = ly_uip[i].alturls;
while ((p = (char *) HTList_nextObject(l0)) != NULL) {
if (!strcmp(p, url))
return;
if (!strcmp(p, ly_uip[i].url)) {
StrAllocCopy(ly_uip[i].url, url);
return;
}
n++;
}
if (!ly_uip[i].alturls)
ly_uip[i].alturls = HTList_new();
if (n >= HTCacheSize && (ly_uip[i].flags & UIP_F_LIMIT))
HTList_removeFirstObject(ly_uip[i].alturls);
HTList_addObject(ly_uip[i].alturls, ly_uip[i].url);
ly_uip[i].url = NULL;
StrAllocCopy(ly_uip[i].url, url);
}
return;
}
}
}
void LYUIPages_free(void)
{
unsigned int i;
for (i = 0; i < TABLESIZE(ly_uip); i++) {
FREE(ly_uip[i].url);
FREE(ly_uip[i].file);
LYFreeStringList(ly_uip[i].alturls);
ly_uip[i].alturls = NULL;
}
}
/*
* Convert local pathname to www name
* (do not bother about file://localhost prefix at this point).
*/
const char *wwwName(const char *pathname)
{
const char *cp = NULL;
#if defined(USE_DOS_DRIVES)
cp = HTDOS_wwwName(pathname);
#else
#ifdef VMS
cp = HTVMS_wwwName(pathname);
#else
cp = pathname;
#endif /* VMS */
#endif
return cp;
}
/*
* Given a user-specified filename, e.g., for download or print, validate and
* expand it. Expand home-directory expressions in the given string. Only
* allow pipes if the user can spawn shell commands.
*/
BOOLEAN LYValidateFilename(bstring **result,
bstring **given)
{
BOOLEAN code = TRUE;
char *cp = NULL;
/*
* Cancel if the user entered "/dev/null" on Unix, or an "nl:" path on VMS.
* - FM
*/
if (LYIsNullDevice((*given)->str)) {
/* just ignore it */
code = FALSE;
#ifdef HAVE_POPEN
} else if (LYIsPipeCommand((*given)->str)) {
if (no_shell) {
HTUserMsg(SPAWNING_DISABLED);
code = FALSE;
} else {
BStrCopy(*result, (*given));
}
#endif
} else {
if (FindLeadingTilde((*given)->str, TRUE) != 0) {
char *cp1 = NULL;
StrAllocCopy(cp1, (*given)->str);
LYTildeExpand(&cp1, TRUE);
BStrCopy0(*result, cp1);
BStrCopy0(*given, cp1);
FREE(cp1);
}
#ifdef VMS
if (StrChr((*given)->str, '/') != NULL) {
BStrCopy0(*result, HTVMS_name("", (*given)->str));
BStrCopy(*given, *result);
}
if ((*given)->str[0] != '/'
&& StrChr((*given)->str, ':') == NULL) {
BStrCopy0(*result, "sys$disk:");
if (StrChr((*given)->str, ']') == NULL)
BStrCat0(*result, "[]");
BStrCat(*result, (*given));
} else {
BStrCopy(*result, (*given));
}
#else
#ifndef __EMX__
if (!LYisAbsPath((*given)->str)) {
#if defined(__DJGPP__) || defined(_WINDOWS)
if (StrChr((*result)->str, ':') != NULL)
cp = NULL;
else
#endif /* __DJGPP__ || _WINDOWS */
{
#ifdef SUPPORT_CHDIR
static char buf[LY_MAXPATH];
cp = Current_Dir(buf);
#else
cp = original_dir;
#endif
}
} else
#endif /* __EMX__ */
cp = NULL;
if (cp) {
LYTrimPathSep(cp);
BStrCopy0(*result, cp);
BStrCat0(*result, "/");
} else {
BStrCopy0(*result, "");
}
if (code) {
cp = HTSYS_name((*given)->str);
BStrCat0(*result, cp);
}
#endif /* VMS */
}
return code;
}
/*
* Given a valid filename, check if it exists. If so, we'll have to worry
* about overwriting it.
*
* Returns:
* 'Y' (yes/success)
* 'N' (no/retry)
* 3 (cancel)
*/
int LYValidateOutput(char *filename)
{
int c;
CTRACE((tfp, "LYValidateOutput '%s'\n", filename));
/*
* Assume we can write to a pipe
*/
#ifdef HAVE_POPEN
if (LYIsPipeCommand(filename))
return 'Y';
#endif
if (no_dotfiles || !show_dotfiles) {
if (*LYPathLeaf(filename) == '.') {
HTAlert(FILENAME_CANNOT_BE_DOT);
return 'N';
}
}
/*
* See if it already exists.
*/
if (LYCanReadFile(filename)) {
#ifdef VMS
c = HTConfirm(FILE_EXISTS_HPROMPT);
#else
c = HTConfirm(FILE_EXISTS_OPROMPT);
#endif /* VMS */
if (HTLastConfirmCancelled()) {
HTInfoMsg(SAVE_REQUEST_CANCELLED);
return 3;
} else if (c == NO) {
return 'N';
}
} else if (!LYCanWriteFile(filename)) {
return 'N';
}
return 'Y';
}
/*
* Convert a local filename to a URL
*/
void LYLocalFileToURL(char **target,
const char *source)
{
const char *leaf;
StrAllocCopy(*target, "file://localhost");
leaf = wwwName(source);
if (!LYisAbsPath(source)) {
char temp[LY_MAXPATH];
Current_Dir(temp);
if (!LYIsHtmlSep(*temp))
LYAddHtmlSep(target);
StrAllocCat(*target, temp);
}
if (leaf && !LYIsHtmlSep(*leaf))
LYAddHtmlSep(target);
StrAllocCat(*target, leaf);
}
#define MY_DOCTYPE "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n"
#define PUT_STRING(buf) (*(target)->isa->put_string)(target, buf)
/*
* Like WriteInternalTitle, used for writing title on pages constructed via
* streams.
*/
void WriteStreamTitle(HTStream *target, const char *Title)
{
char *buf = 0;
PUT_STRING(MY_DOCTYPE);
PUT_STRING("<html>\n<head>\n");
LYAddMETAcharsetToStream(target, -1);
HTSprintf0(&buf, "<title>%s</title>\n</head>\n<body>\n", Title);
PUT_STRING(buf);
FREE(buf);
}
/*
* Open a temporary file for internal-pages, optionally reusing an existing
* filename.
*/
FILE *InternalPageFP(char *filename,
int reuse_flag)
{
FILE *fp;
if (LYReuseTempfiles && reuse_flag) {
fp = LYOpenTempRewrite(filename, HTML_SUFFIX, BIN_W);
} else {
(void) LYRemoveTemp(filename);
fp = LYOpenTemp(filename, HTML_SUFFIX, BIN_W);
}
if (fp == NULL) {
HTAlert(CANNOT_OPEN_TEMP);
}
return fp;
}
/*
* This part is shared by all internal pages.
*/
void WriteInternalTitle(FILE *fp0, const char *Title)
{
fprintf(fp0, MY_DOCTYPE);
fprintf(fp0, "<html>\n<head>\n");
LYAddMETAcharsetToFD(fp0, -1);
if (LYIsListpageTitle(Title)) {
if (StrChr(HTLoadedDocumentURL(), '"') == NULL) {
char *Address = NULL;
/*
* Insert a BASE tag so there is some way to relate the List Page
* file to its underlying document after we are done. It won't be
* actually used for resolving relative URLs. - kw
*/
StrAllocCopy(Address, HTLoadedDocumentURL());
LYEntify(&Address, FALSE);
fprintf(fp0, "<base href=\"%s\">\n", Address);
FREE(Address);
}
}
fprintf(fp0, "<title>%s</title>\n</head>\n<body>\n", Title);
}
/*
* This is used to start most internal pages, except for special cases where
* the embedded HREF's in the title differ.
*/
void BeginInternalPage(FILE *fp0, const char *Title,
const char *HelpURL)
{
WriteInternalTitle(fp0, Title);
if ((user_mode == NOVICE_MODE)
&& LYwouldPush(Title, NULL)
&& (HelpURL != 0)) {
fprintf(fp0, "<h1>%s (%s%s%s), <a href=\"%s%s\">help</a></h1>\n",
Title, LYNX_NAME, VERSION_SEGMENT, LYNX_VERSION,
helpfilepath, HelpURL);
} else {
fprintf(fp0, "<h1>%s (%s%s%s)</h1>\n",
Title, LYNX_NAME, VERSION_SEGMENT, LYNX_VERSION);
}
}
void EndInternalPage(FILE *fp0)
{
fprintf(fp0, "</body>\n</html>");
}
char *trimPoundSelector(char *address)
{
char *pound = findPoundSelector(address);
if (pound != 0)
*pound = '\0';
return pound;
}
/*
* Trim a trailing path-separator to avoid confusing other programs when we concatenate
* to it. This only applies to local filesystems.
*/
void LYTrimPathSep(char *path)
{
size_t len;
if (path != 0
&& (len = strlen(path)) != 0
&& LYIsPathSep(path[len - 1]))
path[len - 1] = 0;
}
/*
* Add a trailing path-separator to avoid confusing other programs when we concatenate
* to it. This only applies to local filesystems.
*/
void LYAddPathSep(char **path)
{
size_t len;
char *temp;
if ((path != 0)
&& ((temp = *path) != 0)
&& (len = strlen(temp)) != 0
&& !LYIsPathSep(temp[len - 1])) {
StrAllocCat(*path, FILE_SEPARATOR);
}
}
/*
* Add a trailing path-separator to avoid confusing other programs when we concatenate
* to it. This only applies to local filesystems.
*/
void LYAddPathSep0(char *path)
{
size_t len;
if ((path != 0)
&& (len = strlen(path)) != 0
&& (len < LY_MAXPATH - 2)
&& !LYIsPathSep(path[len - 1])) {
strcat(path, FILE_SEPARATOR);
}
}
/*
* Check if a given string contains a path separator
*/
char *LYLastPathSep(const char *path)
{
char *result;
#if defined(USE_DOS_DRIVES)
if ((result = strrchr(path, '\\')) == 0)
result = strrchr(path, '/');
#else
result = strrchr(path, '/');
#endif
return result;
}
/*
* Trim a trailing path-separator to avoid confusing other programs when we concatenate
* to it. This only applies to HTML paths.
*/
void LYTrimHtmlSep(char *path)
{
size_t len;
if (path != 0
&& (len = strlen(path)) != 0
&& LYIsHtmlSep(path[len - 1]))
path[len - 1] = 0;
}
/*
* Add a trailing path-separator to avoid confusing other programs when we concatenate
* to it. This only applies to HTML paths.
*/
void LYAddHtmlSep(char **path)
{
size_t len;
char *temp;
if ((path != 0)
&& ((temp = *path) != 0)
&& (len = strlen(temp)) != 0
&& !LYIsHtmlSep(temp[len - 1])) {
StrAllocCat(*path, "/");
}
}
/*
* Add a trailing path-separator to avoid confusing other programs when we concatenate
* to it. This only applies to HTML paths.
*/
void LYAddHtmlSep0(char *path)
{
size_t len;
if ((path != 0)
&& (len = strlen(path)) != 0
&& (len < LY_MAXPATH - 2)
&& !LYIsHtmlSep(path[len - 1])) {
strcat(path, "/");
}
}
/*
* Rename a file
*/
int LYRenameFile(char *src,
char *dst)
{
#ifdef _WINDOWS
/*
* If dest_file exists prior to calling rename(), rename() will fail on Windows platforms.
* https://www.securecoding.cert.org/confluence/display/c/FIO10-C.+Take+care+when+using+the+rename%28%29+function
*/
struct stat st;
if (stat(dst, &st) == 0) {
unlink(dst);
}
#endif
return rename(src, dst);
}
/*
* Copy a file
*/
int LYCopyFile(char *src,
char *dst)
{
int code;
const char *program;
if ((program = HTGetProgramPath(ppCOPY)) != NULL) {
char *the_command = 0;
HTAddParam(&the_command, COPY_COMMAND, 1, program);
HTAddParam(&the_command, COPY_COMMAND, 2, src);
HTAddParam(&the_command, COPY_COMMAND, 3, dst);
HTEndParam(&the_command, COPY_COMMAND, 3);
CTRACE((tfp, "command: %s\n", the_command));
stop_curses();
code = LYSystem(the_command);
start_curses();
FREE(the_command);
} else {
FILE *fin, *fout;
unsigned char buff[BUFSIZ];
size_t len;
code = EOF;
if ((fin = fopen(src, BIN_R)) != 0) {
if ((fout = fopen(dst, BIN_W)) != 0) {
code = 0;
while ((len = fread(buff, (size_t) 1, sizeof(buff), fin)) != 0) {
if (fwrite(buff, (size_t) 1, len, fout) < len
|| ferror(fout)) {
code = EOF;
break;
}
}
LYCloseOutput(fout);
}
LYCloseInput(fin);
}
CTRACE((tfp, "builtin copy ->%d\n\tsource=%s\n\ttarget=%s\n",
code, src, dst));
}
if (code) {
HTAlert(CANNOT_WRITE_TO_FILE);
}
return code;
}
#ifdef __DJGPP__
static char *escape_backslashes(char *source)
{
char *result = 0;
int count = 0;
int n;
for (n = 0; source[n] != '\0'; ++n) {
if (source[n] == '\\')
++count;
}
if (count != 0) {
result = malloc(count + n + 1);
if (result != 0) {
int ch;
char *target = result;
while ((ch = *source++) != '\0') {
if (ch == '\\')
*target++ = ch;
*target++ = ch;
}
*target = '\0';
}
}
return result;
}
#endif /* __DJGPP__ */
/*
* Invoke a shell command, return nonzero on error.
*/
int LYSystem(char *command)
{
int code;
int do_free = 0;
#if defined(HAVE_SIGACTION) && defined(SIGTSTP) && !defined(USE_SLANG)
struct sigaction saved_sigtstp_act;
BOOLEAN sigtstp_saved = FALSE;
#endif
int saved_errno = 0;
#ifdef __EMX__
int scrsize[4];
#endif
fflush(stdout);
fflush(stderr);
CTRACE((tfp, "LYSystem(%s)\n", command));
CTRACE_FLUSH(tfp);
#ifdef __DJGPP__
__djgpp_set_ctrl_c(0);
_go32_want_ctrl_break(1);
#endif /* __DJGPP__ */
#ifdef VMS
code = DCLsystem(command);
#else
# ifdef __EMX__ /* FIXME: Should be LY_CONVERT_SLASH? */
/* Configure writes commands which contain direct slashes.
Native command-(non)-shell will not tolerate this. */
{
char *space = command, *slash = command;
_scrsize(scrsize);
while (*space && *space != ' ' && *space != '\t')
space++;
while (slash < space && *slash != '/')
slash++;
if (slash != space) {
char *old = command;
command = NULL;
StrAllocCopy(command, old);
do_free = 1;
slash = (slash - old) + command - 1;
space = (space - old) + command;
while (++slash < space)
if (*slash == '/')
*slash = '\\';
}
}
# endif
/*
* This chunk of code does not work, for two reasons:
* a) the Cygwin system() function edits out the backslashes
* b) it does not account for more than one parameter, e.g., +number
*/
#if defined(__CYGWIN__) && defined(DOSPATH) /* 1999/02/26 (Fri) */
{
char cmd[LY_MAXPATH];
char win32_name[LY_MAXPATH];
char new_cmd[LY_MAXPATH];
char new_command[LY_MAXPATH * 2 + 10];
char *p, *q;
p = command;
q = cmd;
while (*p) {
if (*p == ' ')
break;
else
*q = *p;
p++;
q++;
}
*q = '\0';
if (cmd[0] == '/')
cygwin_conv_to_full_posix_path(cmd, new_cmd);
else
strcpy(new_cmd, cmd);
while (*p == ' ')
p++;
if (StrChr(p, '\\') == NULL) {
/* for Windows Application */
cygwin_conv_to_full_win32_path(p, win32_name);
sprintf(new_command, "%.*s \"%.*s\"",
LY_MAXPATH, new_cmd, LY_MAXPATH, win32_name);
} else {
/* for DOS like editor */
q = win32_name;
while (*p) {
if (*p == '\\') {
if (*(p + 1) == '\\')
p++;
}
*q = *p;
q++, p++;
}
*q = '\0';
sprintf(new_command, "%.*s %.*s", LY_MAXPATH, new_cmd, LY_MAXPATH, win32_name);
}
command = new_command;
}
#endif
#ifdef __DJGPP__
if (dj_is_bash) {
char *new_command = escape_backslashes(command);
if (new_command != 0) {
if (do_free)
free(command);
command = new_command;
}
}
#endif /* __DJGPP__ */
#ifdef _WIN_CC
code = exec_command(command, TRUE); /* Wait exec */
#else /* !_WIN_CC */
#ifdef SIGPIPE
if (restore_sigpipe_for_children)
signal(SIGPIPE, SIG_DFL); /* Some commands expect the default */
#endif
#if defined(HAVE_SIGACTION) && defined(SIGTSTP) && !defined(USE_SLANG)
if (!dump_output_immediately && !LYCursesON && !no_suspend)
sigtstp_saved = LYToggleSigDfl(SIGTSTP, &saved_sigtstp_act, 1);
#endif
code = system(command);
saved_errno = errno;
#if defined(HAVE_SIGACTION) && defined(SIGTSTP) && !defined(USE_SLANG)
if (sigtstp_saved)
LYToggleSigDfl(SIGTSTP, &saved_sigtstp_act, 0);
#endif
#ifdef SIGPIPE
if (restore_sigpipe_for_children)
signal(SIGPIPE, SIG_IGN); /* Ignore it again - kw */
#endif
#endif
#endif
#ifdef __DJGPP__
__djgpp_set_ctrl_c(1);
_go32_want_ctrl_break(0);
#endif /* __DJGPP__ */
fflush(stdout);
fflush(stderr);
if (do_free)
FREE(command);
#if !defined(UCX) || !defined(VAXC) /* errno not modifiable ?? */
set_errno(saved_errno); /* may have been clobbered */
#endif
#ifdef __EMX__ /* Check whether the screen size changed */
size_change(0);
#endif
return code;
}
/*
* Return a string which can be used in LYSystem() for spawning a subshell
*/
#if defined(__CYGWIN__) /* 1999/02/26 (Fri) */
int Cygwin_Shell(void)
{
char *shell;
int code;
STARTUPINFO startUpInfo;
PROCESS_INFORMATION procInfo;
SECURITY_ATTRIBUTES sa;
/* Set up security attributes to allow inheritance of the file handle */
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = 0;
sa.bInheritHandle = TRUE;
/* Init a startup structure */
GetStartupInfo(&startUpInfo);
shell = LYGetEnv("COMSPEC");
/* Create the child process, specifying
inherited handles. Pass the value of the
handle as a command line parameter */
code = 0;
if (shell) {
code = CreateProcess(0, shell, 0, 0,
TRUE, 0,
0, 0, &startUpInfo, &procInfo);
if (!code) {
printf("shell = [%s], code = %ld\n", shell, (long) GetLastError());
}
/* wait for the child to return (this is not a requirement
since the child is its own independent process) */
WaitForSingleObject(procInfo.hProcess, INFINITE);
}
return code;
}
#endif
#ifdef WIN_EX
/*
* Quote the path to make it safe for shell command processing.
* We always quote it not only includes spaces in it.
* At least we should quote paths which include "&".
*/
char *quote_pathname(char *pathname)
{
char *result = NULL;
HTSprintf0(&result, "\"%s\"", pathname);
return result;
}
#endif
const char *LYSysShell(void)
{
const char *shell = 0;
#ifdef DOSPATH
#ifdef WIN_EX
shell = LYGetEnv("SHELL");
if (shell) {
if (access(shell, 0) != 0)
shell = LYGetEnv("COMSPEC");
} else {
shell = LYGetEnv("COMSPEC");
}
if (shell == NULL) {
if (system_is_NT)
shell = "cmd.exe";
else
shell = "command.com";
}
#else
shell = LYGetEnv("SHELL");
if (shell == NULL) {
shell = LYGetEnv("COMSPEC");
}
if (shell == NULL) {
shell = "command.com";
}
#endif /* WIN_EX */
#else
#ifdef __EMX__
if (LYGetEnv("SHELL") != NULL) {
shell = LYGetEnv("SHELL");
} else {
shell = (LYGetEnv("COMSPEC") == NULL) ? "cmd.exe" : LYGetEnv("COMSPEC");
}
#else
#ifdef VMS
shell = "";
#else
shell = "exec $SHELL";
#endif /* __EMX__ */
#endif /* VMS */
#endif /* DOSPATH */
return shell;
}
#ifdef VMS
#define DISPLAY "DECW$DISPLAY"
#else
#define DISPLAY "DISPLAY"
#endif /* VMS */
/*
* Return the X-Window $DISPLAY string if it is nonnull/nonempty
*/
char *LYgetXDisplay(void)
{
return LYGetEnv(DISPLAY);
}
/*
* Set the value of the X-Window $DISPLAY variable (yes it leaks memory, but
* that is putenv's fault).
*/
void LYsetXDisplay(char *new_display)
{
if (new_display != 0) {
#ifdef VMS
LYUpperCase(new_display);
Define_VMSLogical(DISPLAY, new_display);
#else
static char *display_putenv_command;
display_putenv_command = NULL; /* yes, this is a leak - cannot fix */
HTSprintf0(&display_putenv_command, "DISPLAY=%s", new_display);
putenv(display_putenv_command);
#endif /* VMS */
if ((new_display = LYgetXDisplay()) != 0) {
StrAllocCopy(x_display, new_display);
}
}
}
#ifdef CAN_CUT_AND_PASTE
#ifdef __EMX__
static int proc_type = -1;
static PPIB pib;
static HAB hab;
static HMQ hmq;
static void morph_PM(void)
{
PTIB tib;
int first = 0;
if (proc_type == -1) {
DosGetInfoBlocks(&tib, &pib);
proc_type = pib->pib_ultype;
first = 1;
}
if (pib->pib_ultype != 3) /* 2 is VIO */
pib->pib_ultype = 3; /* 3 is PM */
if (first)
hab = WinInitialize(0);
/* 64 messages if before OS/2 3.0, ignored otherwise */
hmq = WinCreateMsgQueue(hab, 64);
WinCancelShutdown(hmq, 1); /* Do not inform us on shutdown */
}
static void unmorph_PM(void)
{
WinDestroyMsgQueue(hmq);
pib->pib_ultype = proc_type;
}
int size_clip(void)
{
return 8192;
}
/* Code partially stolen from FED editor. */
int put_clip(const char *s)
{
int sz = strlen(s) + 1;
int ret = EOF, nl = 0;
char *pByte = 0, *s1 = s, c, *t;
while ((c = *s1++)) {
if (c == '\r' && *s1 == '\n')
s1++;
else if (c == '\n')
nl++;
}
if (DosAllocSharedMem((PPVOID) & pByte, 0, sz + nl,
PAG_WRITE | PAG_COMMIT | OBJ_GIVEABLE | OBJ_GETTABLE))
return ret;
if (!nl)
memcpy(pByte, s, sz);
else {
t = pByte;
while ((c = *t++ = *s++))
if (c == '\n' && (t == pByte + 1 || t[-2] != '\r'))
t[-1] = '\r', *t++ = '\n';
}
morph_PM();
if (!hab)
goto fail;
WinOpenClipbrd(hab);
WinEmptyClipbrd(hab);
if (WinSetClipbrdData(hab, (ULONG) pByte, CF_TEXT, CFI_POINTER))
ret = 0;
WinCloseClipbrd(hab);
unmorph_PM();
if (ret == 0)
return 0;
fail:
DosFreeMem((PPVOID) & pByte);
return EOF;
}
static int clip_open;
/* get_clip_grab() returns a pointer to the string in the system area.
get_clip_release() should be called ASAP after this. */
char *get_clip_grab(void)
{
char *ClipData;
ULONG ulFormat;
int sz;
morph_PM();
if (!hab)
return 0;
if (clip_open)
get_clip_release();
WinQueryClipbrdFmtInfo(hab, CF_TEXT, &ulFormat);
if (ulFormat != CFI_POINTER) {
unmorph_PM();
return 0;
}
WinOpenClipbrd(hab);
clip_open = 1;
ClipData = (char *) WinQueryClipbrdData(hab, CF_TEXT);
sz = strlen(ClipData);
if (!ClipData || !sz) {
get_clip_release();
return 0;
}
return ClipData;
}
void get_clip_release(void)
{
if (!clip_open)
return;
WinCloseClipbrd(hab);
clip_open = 0;
unmorph_PM();
}
#else /* !( defined __EMX__ ) */
# if !defined(WIN_EX) && defined(HAVE_POPEN)
static FILE *paste_handle = 0;
static char *paste_buf = NULL;
void get_clip_release(void)
{
if (paste_handle != 0)
pclose(paste_handle);
if (paste_buf)
FREE(paste_buf);
}
static int clip_grab(void)
{
char *cmd = LYGetEnv("RL_PASTE_CMD");
if (paste_handle)
pclose(paste_handle);
if (!cmd)
return 0;
paste_handle = popen(cmd, TXT_R);
if (!paste_handle)
return 0;
return 1;
}
#define PASTE_BUFFER 1008
#define CF_TEXT 0 /* Not used */
char *get_clip_grab(void)
{
int len;
unsigned size = PASTE_BUFFER;
int off = 0;
if (!clip_grab())
return NULL;
if (!paste_handle)
return NULL;
if (paste_buf)
FREE(paste_buf);
paste_buf = typeMallocn(char, PASTE_BUFFER);
while (1) {
len = (int) fread(paste_buf + off,
(size_t) 1,
(size_t) PASTE_BUFFER - 1,
paste_handle);
paste_buf[off + len] = '\0';
if (len < PASTE_BUFFER - 1)
break;
if (StrChr(paste_buf + off, '\r')
|| StrChr(paste_buf + off, '\n'))
break;
paste_buf = typeRealloc(char, paste_buf, size += PASTE_BUFFER - 1);
off += len;
}
return paste_buf;
}
int put_clip(const char *s)
{
char *cmd = LYGetEnv("RL_CLCOPY_CMD");
FILE *fh;
size_t l = strlen(s), res;
if (!cmd)
return -1;
fh = popen(cmd, TXT_W);
if (!fh)
return -1;
res = fwrite(s, (size_t) 1, l, fh);
if (pclose(fh) != 0 || res != l)
return -1;
return 0;
}
# endif /* !defined(WIN_EX) && defined(HAVE_POPEN) */
#endif /* __EMX__ */
/*
* Sleep for a number of milli-sec.
*/
void LYmsec_delay(unsigned msec)
{
#if defined(_WINDOWS)
Sleep(msec);
#elif defined(HAVE_NAPMS)
napms((int) msec);
#elif defined(DJGPP) || defined(HAVE_USLEEP)
usleep(1000 * msec);
#else
struct timeval tv;
unsigned long usec = 1000UL * msec;
tv.tv_sec = usec / 1000000UL;
tv.tv_usec = usec % 1000000UL;
select(0, NULL, NULL, NULL, &tv);
#endif
}
#if defined(WIN_EX) /* 1997/10/16 (Thu) 20:13:28 */
int put_clip(const char *szBuffer)
{
HANDLE hWnd;
HANDLE m_hLogData;
LPTSTR pLogData;
HANDLE hClip;
int len;
if (szBuffer == NULL)
return EOF;
len = (int) strlen(szBuffer);
if (len == 0)
return EOF;
else
len++;
m_hLogData = GlobalAlloc(GHND, len);
if (m_hLogData == NULL) {
return EOF;
}
hWnd = NULL;
if (!OpenClipboard(hWnd)) {
return EOF;
}
/* Remove the current Clipboard contents */
if (!EmptyClipboard()) {
GlobalFree(m_hLogData);
return EOF;
}
/* Lock the global memory while we write to it. */
pLogData = (LPTSTR) GlobalLock(m_hLogData);
lstrcpy((LPTSTR) pLogData, szBuffer);
GlobalUnlock(m_hLogData);
/* If there were any lines at all then copy them to clipboard. */
hClip = SetClipboardData(CF_TEXT, m_hLogData);
if (!hClip) {
/* If we couldn't clip the data then free the global handle. */
GlobalFree(m_hLogData);
}
CloseClipboard();
return 0;
}
static HANDLE m_hLogData;
static int m_locked;
/* get_clip_grab() returns a pointer to the string in the system area.
get_clip_release() should be called ASAP after this. */
char *get_clip_grab()
{
HANDLE hWnd;
LPTSTR pLogData;
hWnd = NULL;
if (!OpenClipboard(hWnd)) {
return 0;
}
m_hLogData = GetClipboardData(CF_TEXT);
if (m_hLogData == NULL) {
CloseClipboard();
m_locked = 0;
return 0;
}
pLogData = (LPTSTR) GlobalLock(m_hLogData);
m_locked = 1;
return pLogData;
}
void get_clip_release()
{
if (!m_locked)
return;
GlobalUnlock(m_hLogData);
CloseClipboard();
m_locked = 0;
}
#endif /* WIN_EX */
#endif /* CAN_CUT_AND_PASTE */
#if defined(WIN_EX)
#ifndef WSABASEERR
#define WSABASEERR 10000
#endif
#ifdef ENABLE_IPV6
#define WSOCK_NAME "ws2_32"
#else
#define WSOCK_NAME "wsock32"
#endif
/*
* Description: the windows32 version of perror()
*
* Returns: a pointer to a static error
*
* Notes/Dependencies: I got this from
* comp.os.ms-windows.programmer.win32
*/
char *w32_strerror(DWORD ercode)
{
/* __declspec(thread) necessary if you will use multiple threads */
#if defined(__CYGWIN__) || defined(__MINGW32__)
static char msg_buff[256];
#else
__declspec(thread) static char msg_buff[256];
#endif
HMODULE hModule;
int i, msg_type;
unsigned char *p, *q, tmp_buff[256];
DWORD rc;
hModule = NULL;
msg_type = FORMAT_MESSAGE_FROM_SYSTEM;
/* Fill message buffer with a default message in
* case FormatMessage fails
*/
wsprintf(msg_buff, "Error %ld", ercode);
/*
* Special code for winsock error handling.
*/
if (ercode > WSABASEERR) {
hModule = GetModuleHandle(WSOCK_NAME);
if (hModule)
msg_type = FORMAT_MESSAGE_FROM_HMODULE;
}
/*
* message handling. If not found in module, retry from system.
*/
rc = FormatMessage(msg_type, hModule, ercode, LANG_NEUTRAL,
msg_buff, sizeof(msg_buff), NULL);
if (rc == 0 && msg_type == FORMAT_MESSAGE_FROM_HMODULE) {
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, ercode,
LANG_NEUTRAL, msg_buff, sizeof(msg_buff), NULL);
}
strcpy((char *) tmp_buff, msg_buff);
p = q = tmp_buff;
i = 0;
while (*p) {
if (!(*p == '\n' || *p == '\r'))
msg_buff[i++] = *p;
p++;
}
msg_buff[i] = '\0';
return msg_buff;
}
#endif
#if defined(SYSLOG_REQUESTED_URLS)
/*
* syslog() interface
*/
void LYOpenlog(const char *banner)
{
if (syslog_requested_urls) {
CTRACE((tfp, "LYOpenlog(%s)\n", NONNULL(banner)));
#if defined(DJGPP)
openlog("lynx", LOG_PID | LOG_NDELAY, LOG_LOCAL5);
#else
openlog("lynx", LOG_PID, LOG_LOCAL5);
#endif
if (banner) {
syslog(LOG_INFO, "Session start:%s", banner);
} else {
syslog(LOG_INFO, "Session start");
}
}
}
static BOOLEAN looks_like_password(char *first,
char *last)
{
BOOLEAN result = FALSE;
while (first <= last) {
if (*first == '/'
|| *first == ':') {
result = FALSE;
break;
}
result = TRUE;
first++;
}
return result;
}
void LYSyslog(char *arg)
{
char *colon1;
char *colon2;
char *atsign;
if (syslog_requested_urls) {
CTRACE((tfp, "LYSyslog %s\n", arg));
if (is_url(arg)) { /* proto://user:password@host/path:port */
/* ^this colon */
if ((colon1 = StrChr(arg, ':')) != 0
&& !StrNCmp(colon1, "://", 3)
&& (colon2 = StrChr(colon1 + 3, ':')) != 0
&& (atsign = StrChr(colon1, '@')) != 0
&& (colon2 < atsign)
&& looks_like_password(colon2 + 1, atsign - 1)) {
char *buf = NULL;
StrAllocCopy(buf, arg);
buf[colon2 - arg + 1] = 0;
StrAllocCat(buf, "******");
StrAllocCat(buf, atsign);
syslog(LOG_INFO | LOG_LOCAL5, "%s", buf);
CTRACE((tfp, "...alter %s\n", buf));
FREE(buf);
return;
}
}
syslog(LOG_INFO | LOG_LOCAL5, "%s", NONNULL(arg));
}
}
void LYCloselog(void)
{
if (syslog_requested_urls) {
syslog(LOG_INFO, "Session over");
closelog();
}
}
#endif /* SYSLOG_REQUESTED_URLS */
#if defined(WIN_EX) || defined(__CYGWIN__) /* 2000/03/07 (Tue) 17:17:46 */
#define IS_SEP(p) ((p == '\\') || (p == '/') || (p == ':'))
static char *black_list[] =
{
"con",
"prn",
"clock$",
"config$",
NULL
};
static int is_device(char *fname)
{
HANDLE fileHandle;
DWORD val;
int i;
i = 0;
while (black_list[i] != NULL) {
if (strcasecomp(fname, black_list[i]) == 0) {
return 1; /* device file */
}
i++;
}
fileHandle = CreateFile(fname, 0, 0, 0, OPEN_EXISTING, 0, 0);
if (fileHandle == INVALID_HANDLE_VALUE) {
return 0; /* normal file */
} else {
val = GetFileType(fileHandle);
switch (val) {
case 1:
val = 0;
break;
case 2:
val = 1; /* device file */
break;
default:
val = 0;
break;
}
CloseHandle(fileHandle);
}
return val;
}
static char *device_list[] =
{
"con",
"nul",
"aux",
"prn",
NULL
};
int unsafe_filename(const char *fname)
{
int i, len, sum;
char *cp;
char *save;
i = 0;
while (device_list[i] != NULL) {
if (strcasecomp(fname, device_list[i]) == 0) {
return 0; /* device file (open OK) */
}
i++;
}
save = cp = strdup(fname);
while (*cp) {
if (IS_SJIS_HI1(UCH(*cp)) || IS_SJIS_HI2(UCH(*cp)))
cp += 2; /* KANJI skip */
if (IS_SEP(*cp)) {
*cp = '\0';
}
cp++;
}
sum = 0;
cp = save;
len = (int) strlen(fname);
while (cp < (save + len)) {
if (*cp == '\0') {
cp++;
} else {
char *q;
q = StrChr(cp, '.');
if (q)
*q = '\0';
if (is_device(cp)) {
sum++;
break;
}
if (q)
cp = q + 1;
while (*cp)
cp++;
}
}
free(save);
return (sum != 0);
}
FILE *safe_fopen(const char *fname, const char *mode)
{
if (unsafe_filename(fname)) {
return (FILE *) NULL;
} else {
return fopen(fname, mode);
}
}
#endif /* defined(WIN_EX) || defined(__CYGWIN__) */
|