File: master.pike.in

package info (click to toggle)
pike7.8 7.8.700-7~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 68,572 kB
  • sloc: ansic: 252,113; xml: 36,537; makefile: 4,216; sh: 2,879; lisp: 655; asm: 591; objc: 212; pascal: 157; sed: 34
file content (5800 lines) | stat: -rw-r--r-- 160,565 bytes parent folder | download
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
// -*- Pike -*-
//
// Master Control Program for Pike.
//
// This file is part of Pike. For copyright information see COPYRIGHT.
// Pike is distributed under GPL, LGPL and MPL. See the file COPYING
// for more information.
//
// $Id: 311ee057e2112636b21c9ebcbc259db496d15234 $

#pike __REAL_VERSION__
//#pragma strict_types

//! @appears predef::MasterObject
//!
//! Master control program for Pike.
//!
//! @seealso
//!   @[predef::master()], @[predef::replace_master()]

// --- Some configurable parameters

#define PIKE_AUTORELOAD
#define GETCWD_CACHE
//#define FILE_STAT_CACHE

// This define is searched and replaced by bin/install.pike.
#undef PIKE_MODULE_RELOC

#ifndef PIKE_WARNINGS
#define PIKE_WARNINGS 1
#endif /* PIKE_WARNINGS */


// --- Global constants and variables

// Used by describe_backtrace() et al.
#if !defined(BT_MAX_STRING_LEN) || (BT_MAX_STRING_LEN <= 0)
#undef BT_MAX_STRING_LEN
#define BT_MAX_STRING_LEN	200
#endif /* !defined(BT_MAX_STRING_LEN) || (BT_MAX_STRING_LEN <= 0) */
constant bt_max_string_len = BT_MAX_STRING_LEN;

//! @decl constant bt_max_string_len = 200
//! This constant contains the maximum length of a function entry in a
//! backtrace. Defaults to 200 if no BT_MAX_STRING_LEN define has been
//! given.

// Enables the out of date warning in low_findprog().
#ifndef OUT_OF_DATE_WARNING
#define OUT_OF_DATE_WARNING 1
#endif /* OUT_OF_DATE_WARNING */
constant out_of_date_warning = OUT_OF_DATE_WARNING;

// FIXME: PATH_SEPARATOR and UPDIR should probably be exported,
//        or at least some functions that use them should be.
//        cf Tools.Shoot.runpike.
//	/grubba 2004-04-11
#if defined(__NT__) || defined(__amigaos__)
#define PATH_SEPARATOR ";"
#else
#define PATH_SEPARATOR ":"
#endif

#ifdef __amigaos__
#define UPDIR "/"
#else
#define UPDIR "../"
#endif

//! @decl constant out_of_date_warning = 1
//! Should Pike complain about out of date compiled files.
//! 1 means yes and 0 means no. Controlled by the OUT_OF_DATE_WARNING
//! define.

//! If not zero compilation warnings will be written out on stderr.
int want_warnings = PIKE_WARNINGS;

//!
int compat_major=-1;

//!
int compat_minor=-1;

//!
int show_if_constant_errors = 0;

int is_pike_master = 0;
// This integer variable should exist in any object that aspires to be
// the master. It gets set to 1 when the master is installed, and is
// therefore set in any object that is or has been the master. That
// makes the Encoder class encode references to the master and all
// ex-masters as references to the current master object.

// ---  Functions begin here.

// Have to access some stuff without going through the resolver.
private object(_static_modules.Builtin) Builtin = _static_modules.Builtin();
private constant Files = _static_modules.files;

#define Stat Files.Stat
#define capitalize(X) (upper_case((X)[..0])+(X)[1..])
#define trim_all_whites(X) (Builtin.string_trim_all_whites (X))

private function write = Files()->_stdout->write;
private function werror = Files()->_stderr->write;

// Tell Pike.count_memory this is global.
constant pike_cycle_depth = 0;

#ifdef RESOLV_DEBUG

#if constant (thread_local)
protected object resolv_msg_depth = thread_local();
// NOTE: May be used before __INIT has completed.
#define GET_RESOLV_MSG_DEPTH (resolv_msg_depth && resolv_msg_depth->get())
#define INC_RESOLV_MSG_DEPTH() (resolv_msg_depth && resolv_msg_depth->set (resolv_msg_depth->get() + 1))
#define DEC_RESOLV_MSG_DEPTH() (resolv_msg_depth && resolv_msg_depth->set (resolv_msg_depth->get() - 1))
#else
protected int resolv_msg_depth;
#define GET_RESOLV_MSG_DEPTH resolv_msg_depth
#define INC_RESOLV_MSG_DEPTH() (++resolv_msg_depth)
#define DEC_RESOLV_MSG_DEPTH() (--resolv_msg_depth)
#endif

void resolv_debug (sprintf_format fmt, sprintf_args... args)
{
  string pad = "  " * GET_RESOLV_MSG_DEPTH;
  if (sizeof (args)) fmt = sprintf (fmt, @args);
  if (fmt[-1] == '\n')
    fmt = pad + replace (fmt[..<1], "\n", "\n" + pad) + "\n";
  else
    fmt = pad + replace (fmt, "\n", "\n" + pad);
  if (!werror) werror = Files()->_stderr->write;
  werror (fmt);
}

#else  // !RESOLV_DEBUG
#define INC_RESOLV_MSG_DEPTH() 0
#define DEC_RESOLV_MSG_DEPTH() 0
#define resolv_debug(X...) do {} while (0)
#endif	// !RESOLV_DEBUG

constant no_value = (<>);
constant NoValue = typeof (no_value);

// Some API compatibility stuff.

//! Pike 0.5 master compatibility interface.
//!
//! Most of the interface is implemented via mixin,
//! or overloading by more recent masters.
//!
//! This interface is used for compatibility with
//! all pikes until Pike 0.5.
//!
//! @deprecated predef::MasterObject
//!
//! @seealso
//!   @[get_compat_master()], @[master()], @[predef::MasterObject]
protected class Pike_0_5_master
{
  string describe_backtrace(array(mixed) trace);
  object low_cast_to_object(string oname, string current_file);
  extern array(string) pike_include_path;
  string pike_library_path =
#if "#lib_prefix#"[0]!='#'
    "#lib_prefix#"
#else
    dirname(__FILE__)
#endif
    ;
  extern array(string) pike_module_path;
  extern array(string) pike_program_path;
#ifdef GETCWD_CACHE
  extern string current_path;
  int cd(string s);
  string getcwd();
#endif
  string combine_path_with_cwd(string path);
#ifdef FILE_STAT_CACHE
  extern int invalidate_time;
  extern mapping(string:multiset(string)) dir_cache;
#endif
  local array(mixed) master_file_stat(string x)
  {
    Stat st = global::master_file_stat(x);
    return st && (array)st;
  }

  //! @decl mapping(string:array(string)) environment
  //!
  //! Mapping containing the environment variables.
  //!
  //! The mapping currently has the following structure:
  //! @mapping
  //!   @member array(string) index
  //!      Note that the index is @[lower_case()]'d on NT.
  //!      @array
  //!         @elem string varname
  //!            Variable name with case intact.
  //!         @elem string value
  //!            Variable value.
  //!      @endarray
  //! @endmapping
  //!
  //! @note
  //!   This mapping should not be accessed directly; use @[getenv()]
  //!   and @[putenv()] instead. This mapping is not publicly
  //!   accessible in pikes newer than 7.6.
  //!
  //! @note
  //!   This mapping is not compatible with @[Process.create_process()];
  //!   use the mapping returned from calling @[getenv()] without arguments
  //!   instead.
  //!
  //! @bugs
  //!   This mapping is not the real environment; it is just a copy of
  //!   the environment made at startup. Pike does attempt to keep
  //!   track of changes in the mapping and to reflect them in the
  //!   real environment, but avoid accessing this mapping if at all
  //!   possible.

  string|mapping(string:string) getenv(string|void s);
  void putenv(string|void varname, string|void value);

  // compat_environment is the mapping returned by `environment
  // (if any).
  // compat_environment_copy is used to keep track of any changes
  // performed destructively on the compat_environment mapping.
  // Both should be zero if not in use.
  protected mapping(string:array(string)) compat_environment;
  protected mapping(string:array(string)) compat_environment_copy;

#pragma no_deprecation_warnings
  local __deprecated__(mapping(string:array(string))) `environment()
  {
    if (compat_environment) return compat_environment;
    compat_environment_copy = ([]);
#ifdef __NT__
    // Can't use the cached environment returned by getenv(), since
    // variable names have been lowercased there.
    foreach((array(array(string)))Builtin._getenv(), array(string) pair) {
      compat_environment_copy[lower_case(pair[0])] = pair;
    }
#else
    foreach((array(array(string)))getenv(), array(string) pair) {
      compat_environment_copy[pair[0]] = pair;
    }
#endif
    return compat_environment = copy_value(compat_environment_copy);
  }

  local void `environment=(__deprecated__(mapping(string:array(string)))
			   new_env)
  {
    compat_environment = new_env;
    if (!new_env)
      compat_environment_copy = 0;
    else if (!compat_environment_copy)
      compat_environment_copy = ([]);
  }
#pragma deprecation_warnings

  void add_include_path(string tmp);
  void remove_include_path(string tmp);
  void add_module_path(string tmp);
  void remove_module_path(string tmp);
  void add_program_path(string tmp);
  void remove_program_path(string tmp);
  mapping(string:program|NoValue) programs;
  program cast_to_program(string pname, string current_file);
  void handle_error(array(mixed) trace);

  //! Make a new instance of a class.
  //!
  //! @note
  //!   This function should not be used. It is here for
  //!   compatibility reasons only.
  local __deprecated__ object new(mixed prog, mixed ... args)
  {
    if(stringp(prog))
      prog=cast_to_program(prog,backtrace()[-2][0]);
    return prog(@args);
  }

  void create();
  program handle_inherit(string pname, string current_file);
  extern mapping(program:object) objects;
  object low_cast_to_object(string oname, string current_file);
  object cast_to_object(string oname, string current_file);
  class dirnode {};
  object findmodule(string fullname);
  local protected object Pike_0_5_compat_handler;
  local mixed resolv(string identifier, string|void current_file)
  {
    if (!Pike_0_5_compat_handler) {
      Pike_0_5_compat_handler = global::get_compilation_handler(0, 5);
    }
    return Pike_0_5_compat_handler->resolv(identifier, current_file);
  }
  extern string _master_file_name;
  void _main(array(string) orig_argv, array(string) env);
  extern mixed inhibit_compile_errors;
  void set_inhibit_compile_errors(mixed f);
  string trim_file_name(string s);
  void compile_error(string file,int line,string err);
  string handle_include(string f, string current_file, int local_include);
  local __deprecated__ string stupid_describe(mixed m)
  {
    switch(string typ=sprintf("%t",m))
    {
    case "int":
    case "float":
      return (string)m;

    case "string":
      if(sizeof(m) < BT_MAX_STRING_LEN)
      {
	string t = sprintf("%O", m);
	if (sizeof(t) < (BT_MAX_STRING_LEN + 2)) {
	  return t;
	}
	t = 0;
      }

    case "array":
    case "mapping":
    case "multiset":
      return typ+"["+sizeof(m)+"]";

    default:
      return typ;
    }
  }

  string describe_backtrace(array(mixed) trace);

  object get_compat_master(int major, int minor)
  {
    // 0.0 - 0.5
    if (!major && (minor < 6))
      return this_program::this;
    return get_compat_master(major, minor);
  }

  /* Missing symbols:
   *
   * __INIT
   * __lambda_30	(Alias for mkmapping().)
   */
}

//! Pike 0.6 master compatibility interface.
//!
//! Most of the interface is implemented via mixin,
//! or overloading by more recent masters.
//!
//! This interface is used for compatibility with
//! Pike 0.6.
//!
//! @deprecated predef::MasterObject
//!
//! @seealso
//!   @[get_compat_master()], @[master()], @[predef::MasterObject]
protected class Pike_0_6_master
{
  inherit Pike_0_5_master;
  int is_absolute_path(string p);
  array(string) explode_path(string p);
  string dirname(string x);
  string basename(string x);
  object low_cast_to_object(string oname, string current_file);
#pragma no_deprecation_warnings
  private __deprecated__(string) pike_library_path =
    (__deprecated__(string))Pike_0_5_master::pike_library_path;
#pragma deprecation_warnings
  extern int want_warnings;
  program compile_string(string data, void|string name);
  program compile_file(string file);

#if constant(_static_modules.Builtin.mutex)
  extern object compilation_mutex;
#endif

  local constant mkmultiset = predef::mkmultiset;
  local __deprecated__(function) clone = new;
  constant master_efuns = ({});
  class joinnode {};
  extern mapping(string:mixed) fc;
  mixed handle_import(string what, string|void current_file);
  local protected object Pike_0_6_compat_handler;
  local mixed resolv(string identifier, string|void current_file)
  {
    if (!Pike_0_6_compat_handler) {
      Pike_0_6_compat_handler = global::get_compilation_handler(0, 6);
    }
    return Pike_0_6_compat_handler->resolv(identifier, current_file);
  }
  extern string _pike_file_name;
  void compile_warning(string file,int line,string err);
  string read_include(string f);

  string describe_program(program p);
  string describe_backtrace(array(mixed) trace);
  class Codec {};

  object get_compat_master(int major, int minor)
  {
    if (!major && (minor < 6))
      return Pike_0_5_master::get_compat_master(major, minor);
    // 0.6
    if (!major && (minor < 7))
      return this_program::this;
    return get_compat_master(major, minor);
  }

  /* Missing symbols:
   *
   * __INIT
   */
}

//! Pike 7.0 master compatibility interface.
//!
//! Most of the interface is implemented via mixin,
//! or overloading by more recent masters.
//!
//! This interface is used for compatibility with
//! Pike 0.7 through 7.0.
//!
//! @deprecated predef::MasterObject
//!
//! @seealso
//!   @[get_compat_master()], @[master()], @[predef::MasterObject]
class Pike_7_0_master
{
  inherit Pike_0_6_master;
  constant bt_max_string_len = 1;
  constant out_of_date_warning = 1;
#ifdef PIKE_FAKEROOT
  extern object o;
  string fakeroot(string s);
#endif
#ifdef PIKE_AUTORELOAD
  extern int autoreload_on;
  extern int newest;
  extern mapping(string:int) load_time;
#endif
  string master_read_file();
  string normalize_path(string X);
  array(string) query_precompiled_names(string fname);
  program cast_to_program(string pname, string current_file,
			  object|void handler);
  void handle_error(array(mixed)|object trace);
  protected private constant mkmultiset = mkmultiset;
  program handle_inherit(string pname, string current_file, object|void handler);
  mixed handle_import(string what, string|void current_file, object|void handler);
  mixed resolv_base(string identifier, string|void current_file);

  // FIXME: Not in 7.7!
  extern mapping resolv_cache;
  local protected object Pike_7_0_compat_handler;
  local mixed resolv(string identifier, string|void current_file)
  {
    if (!Pike_7_0_compat_handler) {
      Pike_7_0_compat_handler = global::get_compilation_handler(7, 0);
    }
    return Pike_7_0_compat_handler->resolv(identifier, current_file);
  }
  mixed get_inhibit_compile_errors();
  string decode_charset(string data, string charset);
  local __deprecated__(int) clipped=0;
  local __deprecated__(int) canclip=0;
#pragma no_deprecation_warnings
  local __deprecated__ string stupid_describe(mixed m, int maxlen)
  {
    string typ;
    if (catch (typ=sprintf("%t",m)))
      typ = "object";		// Object with a broken _sprintf(), probably.
    switch(typ)
    {
    case "int":
    case "float":
      return (string)m;
      
    case "string":
      canclip++;
      if(sizeof(m) < maxlen)
      {
	string t = sprintf("%O", m);
	if (sizeof(t) < (maxlen + 2)) {
	  return t;
	}
	t = 0;
      }
      clipped++;
      if(maxlen>10)
      {
	return sprintf("%O+[%d]",m[..maxlen-5],sizeof(m)-(maxlen-5));
      }else{
	return "string["+sizeof(m)+"]";
      }
      
    case "array":
      if(!sizeof(m)) return "({})";
      if(maxlen<5)
      {
	clipped++;
	return "array["+sizeof(m)+"]";
      }
      canclip++;
      return "({" + stupid_describe_comma_list(m,maxlen-2) +"})";
      
    case "mapping":
      if(!sizeof(m)) return "([])";
      return "mapping["+sizeof(m)+"]";
      
    case "multiset":
      if(!sizeof(m)) return "(<>)";
      return "multiset["+sizeof(m)+"]";
      
    case "function":
      if(string tmp=describe_program(m)) return tmp;
      if(object o=function_object(m))
	return (describe_object(o)||"")+"->"+function_name(m);
      else {
	string tmp;
	if (catch (tmp = function_name(m)))
	  // The function object has probably been destructed.
	  return "function";
	return tmp || "function";
      }

    case "program":
      if(string tmp=describe_program(m)) return tmp;
      return typ;

    default:
      if (objectp(m))
	if(string tmp=describe_object(m)) return tmp;
      return typ;
    }
  }
  local __deprecated__ string stupid_describe_comma_list(array x, int maxlen)
  {
    string ret="";

    if(!sizeof(x)) return "";
    if(maxlen<0) return ",,,"+sizeof(x);

    int clip=min(maxlen/2,sizeof(x));
    int len=maxlen;
    int done=0;

//  int loopcount=0;

    while(1)
    {
//    if(loopcount>10000) werror("len=%d\n",len);
      array(string) z=allocate(clip);
      array(int) isclipped=allocate(clip);
      array(int) clippable=allocate(clip);
      for(int e=0;e<clip;e++)
      {
	clipped=0;
	canclip=0;
	z[e]=stupid_describe(x[e],len);
	isclipped[e]=clipped;
	clippable[e]=canclip;
      }

      while(1)
      {
//      if(loopcount>10000)  werror("clip=%d maxlen=%d\n",clip,maxlen);
	string ret = z[..clip-1]*",";
//      if(loopcount>10000)  werror("sizeof(ret)=%d z=%O isclipped=%O done=%d\n",sizeof(ret),z[..clip-1],isclipped[..clip-1],done);
	if(done || sizeof(ret)<=maxlen+1)
	{
	  int tmp=sizeof(x)-clip-1;
//        if(loopcount>10000) werror("CLIPPED::::: %O\n",isclipped);
	  clipped=`+(0,@isclipped);
	  if(tmp>=0)
	  {
	    clipped++;
	    ret+=",,,"+tmp;
	  }
	  canclip++;
	  return ret;
	}

	int last_newlen=len;
	int newlen;
	int clipsuggest;
	while(1)
	{
//        if(loopcount++ > 20000) return "";
//        if(!(loopcount & 0xfff)) werror("GNORK\n");
	  int smallsize=0;
	  int num_large=0;
	  clipsuggest=0;

	  for(int e=0;e<clip;e++)
	  {
//          if(loopcount>10000) werror("sizeof(z[%d])=%d  len=%d\n",e,sizeof(z[e]),len);

	    if((sizeof(z[e])>=last_newlen || isclipped[e]) && clippable[e])
	      num_large++;
	    else
	      smallsize+=sizeof(z[e]);

	    if(num_large * 15 + smallsize < maxlen) clipsuggest=e+1;
	  }
        
//        if(loopcount>10000) werror("num_large=%d  maxlen=%d  smallsize=%d clippsuggest=%d\n",num_large,maxlen,smallsize,clipsuggest);
	  newlen=num_large ? (maxlen-smallsize)/num_large : 0;
       
//        if(loopcount>10000) werror("newlen=%d\n",newlen);

	  if(newlen<8 || newlen >= last_newlen) break;
	  last_newlen=newlen;
//        if(loopcount>10000) werror("len decreased, retrying.\n");
	}

	if(newlen < 8 && clip)
	{
	  clip-= (clip/4) || 1;
	  if(clip > clipsuggest) clip=clipsuggest;
//        if(loopcount>10000) werror("clip decreased, retrying.\n");
	}else{
	  len=newlen;
	  done++;
	  break;
	}
      }
    }

    return ret;
  }
#pragma deprecation_warnings

  string describe_object(object o);
  string describe_backtrace(array(mixed) trace, void|int linewidth);
  string describe_error(mixed trace);

  object get_compat_master(int major, int minor)
  {
    if (!major && (minor < 7))
      return Pike_0_6_master::get_compat_master(major, minor);
    // 0.7 - 7.0
    if ((major < 7) || ((major == 7) && !minor))
      return this_program::this;
    return get_compat_master(major, minor);
  }

  /* No missing symbols. */
}

//! Pike 7.2 master compatibility interface.
//!
//! Most of the interface is implemented via mixin,
//! or overloading by more recent masters.
//!
//! This interface is used for compatibility with
//! Pike 7.1 and 7.2.
//!
//! @deprecated predef::MasterObject
//!
//! @seealso
//!   @[get_compat_master()], @[master()], @[predef::MasterObject]
protected class Pike_7_2_master
{
  inherit Pike_7_0_master;
#ifdef PIKE_MODULE_RELOC
  string relocate_module(string s);
  string unrelocate_module(string s);
#endif
  extern int compat_major;
  extern int compat_minor;
  Stat master_file_stat(string x);
  object low_cast_to_object(string oname, string current_file,
			    object|void current_handler);
  object findmodule(string fullname, object|void handler);
  extern multiset no_resolv;
  extern string ver;
  mapping get_default_module();
  local protected object Pike_7_2_compat_handler;
  local mixed resolv(string identifier, string|void current_file)
  {
    if (!Pike_7_2_compat_handler) {
      Pike_7_2_compat_handler = global::get_compilation_handler(7, 2);
    }
    return Pike_7_2_compat_handler->resolv(identifier, current_file);
  }
  void runtime_warning(string where, string what, mixed ... args);
  protected int clipped;
  protected int canclip;
  protected string stupid_describe(mixed m, int maxlen);
  protected string stupid_describe_comma_list(array x, int maxlen);
  class Describer {};
  string describe_function(function f);
  class CompatResolver {};
  int(0..1) asyncp();
  class Version {};
  extern object currentversion;
  extern mapping(object:object) compat_handler_cache;
  object get_compilation_handler(int major, int minor);
  string _sprintf(int|void t);
  object get_compat_master(int major, int minor)
  {
    if ((major < 7) || ((major == 7) && (minor < 1)))
      return Pike_7_0_master::get_compat_master(major, minor);
    // 7.1 & 7.2
    if ((major == 7) && (minor < 3))
      return this_program::this;
    return get_compat_master(major, minor);
  }

  /* No missing symbols. */
}

//! Pike 7.4 master compatibility interface.
//!
//! Most of the interface is implemented via mixin,
//! or overloading by more recent masters.
//!
//! This interface is used for compatibility with
//! Pike 7.3 and 7.4.
//!
//! @deprecated predef::MasterObject
//!
//! @seealso
//!   @[get_compat_master()], @[master()], @[predef::MasterObject]
protected class Pike_7_4_master
{
  inherit Pike_7_2_master;
#ifdef RESOLV_DEBUG
  void resolv_debug (sprintf_format fmt, sprintf_args... args);
#endif
  void error(sprintf_format f, sprintf_args ... args);
#ifdef GETCWD_CACHE
  protected extern string current_path;
#endif
  constant no_value = (<>);
  constant NoValue = typeof (no_value);
  string programs_reverse_lookup (program prog);
  program objects_reverse_lookup (object obj);
  string fc_reverse_lookup (object obj);
  // Hide clone() and new().
  private local __deprecated__ object new(mixed prog, mixed ... args){}
  private local __deprecated__(function) clone = new;
  void unregister(program p);
  program low_cast_to_program(string pname,
			      string current_file,
			      object|void handler,
			      void|int mkobj);
  extern string include_prefix;
  extern mapping(string:string) predefines;
  // FIXME: Not in 7.7!
  extern CompatResolver parent_resolver;
  void add_predefine (string name, string value);
  void remove_predefine (string name);
  mapping get_predefines();
#if constant(thread_create)
  object backend_thread();
#endif
  function(string:string) set_trim_file_name_callback(function(string:string) s);
  int compile_exception (array|object trace);
  string program_path_to_name ( string path,
				void|string module_prefix,
				void|string module_suffix,
				void|string object_suffix );
  string describe_module(object|program mod, array(object)|void ret_obj);
  string describe_program(program|function p);
  class Encoder {};
  class Decoder {};
  extern mapping(string:Codec) codecs;
  Codec get_codec(string|void fname, int|void mkobj);

  // The following come from the inherit of Codec. */
  // Codec::Encoder:
  extern mixed encoded;
  string|array nameof (mixed what, void|array(object) module_object);
  mixed encode_object(object x);
  // Codec::Decoder:
  extern string fname;
  extern int mkobj;
  object __register_new_program(program p);
  object objectof (string|array what);
  function functionof (string|array what);
  program programof (string|array what);
  void decode_object(object o, mixed data);

  string _sprintf(int t);
  local protected object Pike_7_4_compat_handler;
  local mixed resolv(string identifier, string|void current_file)
  {
    if (!Pike_7_4_compat_handler) {
      Pike_7_4_compat_handler = global::get_compilation_handler(7, 4);
    }
    return Pike_7_4_compat_handler->resolv(identifier, current_file);
  }
  object get_compat_master(int major, int minor)
  {
    if ((major < 7) || ((major == 7) && (minor < 3)))
      return Pike_7_2_master::get_compat_master(major, minor);
    // 7.3 & 7.4
    if ((major == 7) && (minor < 5))
      return this_program::this;
    return get_compat_master(major, minor);
  }

  /* No missing symbols. */
}

//! Pike 7.6 master compatibility interface.
//!
//! Most of the interface is implemented via mixin,
//! or overloading by more recent masters.
//!
//! This interface is used for compatibility with
//! Pike 7.5 and 7.6.
//!
//! @deprecated predef::MasterObject
//!
//! @seealso
//!   @[get_compat_master()], @[master()], @[predef::MasterObject]
protected class Pike_7_6_master
{
  inherit Pike_7_4_master;

  extern int show_if_constant_errors;
  protected void create();
  extern mapping(string:string) initial_predefines;
  extern joinnode root_module;
  extern mapping(object:joinnode) handler_root_modules;
  extern array(string) system_module_path;
  protected extern CompatResolver parent_resolver;
  protected extern mapping resolv_cache;
  extern CompatResolver fallback_resolver;
  joinnode get_root_module(object|void current_handler);
  class Pike06Resolver {}
  local protected object Pike_7_6_compat_handler;
  local mixed resolv_or_error(string identifier, string|void current_file,
			      void|object current_handler)
  {
    if (!Pike_7_6_compat_handler) {
      Pike_7_6_compat_handler = global::get_compilation_handler(7, 6);
    }
    return Pike_7_6_compat_handler->resolv_or_error(identifier, current_file,
						    current_handler);
  }
  local mixed resolv(string identifier, string|void current_file)
  {
    if (!Pike_7_6_compat_handler) {
      Pike_7_6_compat_handler = global::get_compilation_handler(7, 6);
    }
    return Pike_7_6_compat_handler->resolv(identifier, current_file);
  }
  array get_backtrace (object|array err);
  object get_compat_master(int major, int minor)
  {
    if ((major < 7) || ((major == 7) && (minor < 5)))
      return Pike_7_4_master::get_compat_master(major, minor);
    // 7.5 & 7.6
    if ((major == 7) && (minor < 7))
      return this_program::this;
    return get_compat_master(major, minor);
  }

  /* No missing symbols. */
}

//! Namespaces for compat masters.
//!
//! This inherit is used to provide compatibility namespaces
//! for @[get_compat_master()].
//!
//! @seealso
//!   @[get_compat_master()]
protected inherit Pike_7_6_master;

//! @appears error
//! Throws an error. A more readable version of the code
//! @expr{throw( ({ sprintf(f, @@args), backtrace() }) )@}.
void error(sprintf_format f, sprintf_args ... args) {
  if (sizeof(args)) f = sprintf(f, @args);
  throw( ({ f, backtrace()[..<1] }) );
}

// FIXME: Should the pikeroot-things be private?
#ifdef PIKE_FAKEROOT
object o;
string fakeroot(string s)
{
  string tmp1=combine_path_with_cwd(s);
#ifdef PIKE_FAKEROOT_OMIT
  foreach(PIKE_FAKEROOT_OMIT/PATH_SEPARATOR, string x)
    if(glob(x,tmp1))
      return s;
#endif
  return PIKE_FAKEROOT+tmp1;
}
#else
#define fakeroot(X) X
#endif // PIKE_FAKEROOT

#ifdef PIKE_MODULE_RELOC
string relocate_module(string s)
{
  if(s == "/${PIKE_MODULE_PATH}" || has_prefix (s, "/${PIKE_MODULE_PATH}/")) {
    string tmp = s[21..];
    foreach(pike_module_path, string path) {
      string s2 = fakeroot(sizeof(tmp)? combine_path(path, tmp) : path);
      if(master_file_stat(s2))
	return s2;
    }
  }
  return fakeroot(s);
}

string unrelocate_module(string s)
{
  if(s == "/${PIKE_MODULE_PATH}" || has_prefix (s, "/${PIKE_MODULE_PATH}/"))
    return s;

  foreach(pike_module_path, string path)
    if(s == path)
      return "/${PIKE_MODULE_PATH}";
    else {
      path = combine_path(path, "");
      if(has_prefix (s, path))
	return "/${PIKE_MODULE_PATH}/"+s[sizeof(path)..];
    }

  /* This is necessary to find compat modules... */
  foreach(pike_module_path, string path) {
    path = combine_path(path, UPDIR, "");
    if(has_prefix (s, path))
      return "/${PIKE_MODULE_PATH}/"+UPDIR+s[sizeof(path)..];
  }

  return s;
}

#ifdef fakeroot
#undef fakeroot
#endif
#define fakeroot relocate_module
#endif // PIKE_MODULE_RELOC


//! @appears is_absolute_path
//! Check if a path @[p] is fully qualified (ie not relative).
//!
//! @returns
//! Returns 1 if the path is absolute, 0 otherwise.
int is_absolute_path(string p)
{
#ifdef __amigaos__
#define IS_ABSOLUTE_PATH(X) (search((X),":")>0)
  return IS_ABSOLUTE_PATH(p);
#else
#ifdef __NT__
  p=replace(p,"\\","/");
  if(sscanf(p,"%[a-zA-Z]:%*c",string s)==2 && sizeof(s)==1)
    return 1;
#define IS_ABSOLUTE_PATH is_absolute_path
#else
#define IS_ABSOLUTE_PATH(X) has_prefix((X),"/")
#endif
  return has_prefix(p,"/");
#endif
}

#ifdef __NT__
#define EXPLODE_PATH(X) (replace((X),"\\","/")/"/")
#else
#define EXPLODE_PATH(X) ((X)/"/")
#endif

//! @appears explode_path
//! Split a path @[p] into its components.
//!
//! This function divides a path into its components. This might seem like
//! it could be done by dividing the string on <tt>"/"</tt>, but that will
//! not work on some operating systems.  To turn the components back into
//! a path again, use @[combine_path()].
//!
array(string) explode_path(string p)
{
#ifdef __amigaos__
  int colon = search(reverse(p), ":");
  if(colon >= 0)
    return ({ p[..<colon] }) + explode_path(p[<colon+1..]);
  array(string) r = p/"/";
  return replace(r[..<1], "", "/")+r[<0..];
#else
  array(string) r = EXPLODE_PATH(p);
  if(r[0] == "" && sizeof(p))
    r[0] = "/";
  return r;
#endif
}

//! @appears dirname
//! Returns all but the last segment of a path. Some example inputs and
//! outputs:
//!
//! @xml{<matrix>
//! <r><c><b>Expression</b></c><c><b>Value</b></c></r>
//! <r><c>dirname("/a/b")</c><c>"/a"</c></r>
//! <r><c>dirname("/a/")</c><c>"/a"</c></r>
//! <r><c>dirname("/a")</c><c>"/"</c></r>
//! <r><c>dirname("/")</c><c>"/"</c></r>
//! <r><c>dirname("")</c><c>""</c></r>
//! </matrix>@}
//!
//! @seealso
//! @[basename()], @[explode_path()]
string dirname(string x)
{
  if(x=="") return "";
#ifdef __amigaos__
  array(string) tmp=x/":";
  array(string) tmp2=tmp[-1]/"/";
  tmp[-1]=tmp2[..<1]*"/";
  if(sizeof(tmp2) >= 2 && tmp2[-2]=="") tmp[-1]+="/";
  return tmp*":";
#else
  array(string) tmp=EXPLODE_PATH(x);
  if(x[0]=='/' && sizeof(tmp)<3) return "/";
  return tmp[..<1]*"/";
#endif
}

//! @appears basename
//! Returns the last segment of a path.
//!
//! @seealso
//! @[dirname()], @[explode_path()]
string basename(string x)
{
#ifdef __amigaos__
  return ((x/":")[-1]/"/")[-1];
#define BASENAME(X) ((((X)/":")[-1]/"/")[-1])
#else
  array(string) tmp=EXPLODE_PATH(x);
  return tmp[-1];
#define BASENAME(X) (EXPLODE_PATH(X)[-1])
#endif
}

#ifdef PIKE_AUTORELOAD

int autoreload_on;
int newest;

#define AUTORELOAD_BEGIN() \
    int ___newest=newest;  \
    newest=0

#define AUTORELOAD_CHECK_FILE(X) do {					\
    if(autoreload_on)							\
      if(Stat s=master_file_stat(X))					\
	if(s->mtime>newest) newest=[int]s->mtime;			\
  } while(0)

#define AUTORELOAD_FINISH(VAR, CACHE, FILE)				\
  if(autoreload_on) {							\
    mixed val = CACHE[FILE];						\
    if(!zero_type (val) && val != no_value &&				\
       newest <= load_time[FILE]) {					\
      VAR = val;							\
    }									\
  }									\
  load_time[FILE] = newest;						\
  if(___newest > newest) newest=___newest;


mapping(string:int) load_time=([]);
#else

#define AUTORELOAD_CHECK_FILE(X)
#define AUTORELOAD_BEGIN()
#define AUTORELOAD_FINISH(VAR,CACHE,FILE)

#endif // PIKE_AUTORELOAD

//! @appears compile_string
//! Compile the Pike code in the string @[source] into a program.
//! If @[filename] is not specified, it will default to @expr{"-"@}.
//!
//! Functionally equal to @expr{@[compile](@[cpp](@[source], @[filename]))@}.
//!
//! @seealso
//! @[compile()], @[cpp()], @[compile_file()]
//!
program compile_string(string source, void|string filename,
		       object|void handler,
		       void|program p,
		       void|object o,
		       void|int _show_if_constant_errors)
{
  program ret = compile(cpp(source, filename||"-", 1, handler,
		     compat_major, compat_minor,
		     (zero_type(_show_if_constant_errors)?
		      show_if_constant_errors:
		      _show_if_constant_errors)),
		 handler,
		 compat_major,
		 compat_minor,
		 p,
		 o);
  if (source_cache)
    source_cache[ret] = source;
  return ret;
}

//!
string master_read_file(string file)
{
  object o=Files()->Fd();
  if( ([function(string, string : int)]o->open)(fakeroot(file),"r") )
    return ([function(void : string)]o->read)();
  return 0;
}

#ifdef GETCWD_CACHE
protected string current_path;
int cd(string s)
{
  current_path=0;
  return predef::cd(s);
}

string getcwd()
{
  return current_path || (current_path=predef::getcwd());
}
#endif // GETCWD_CACHE

string combine_path_with_cwd(string ... paths)
{
  return combine_path(IS_ABSOLUTE_PATH(paths[0])?"":getcwd(),@paths);
}

#ifdef FILE_STAT_CACHE

#define FILE_STAT_CACHE_TIME 20

int invalidate_time;
mapping(string:multiset(string)) dir_cache = ([]);


array(string) master_get_dir(string|void x)
{
  return get_dir(x);
}

Stat master_file_stat(string x)
{
  string dir = combine_path_with_cwd(x);
  string file = BASENAME(dir);
  dir = dirname(dir);

  if(time() > invalidate_time)
  {
    dir_cache = ([]);
    invalidate_time = time()+FILE_STAT_CACHE_TIME;
  }

  multiset(string) d = dir_cache[dir];
  if( zero_type(d) )
  {
    array(string) tmp = master_get_dir(dir);
    if(tmp)
    {
#ifdef __NT__
      tmp = map(tmp, lower_case);
#endif
      d = dir_cache[dir] = (multiset)tmp;
    }
    else
      dir_cache[dir]=0;
  }

#ifdef __NT__
  file = lower_case(file);
#endif
  if(d && !d[file]) return 0;

  return predef::file_stat(x);
}
#else
constant master_file_stat = predef::file_stat;
constant master_get_dir = predef::get_dir;
#endif // FILE_STAT_CACHE


protected mapping(string:string) environment;

#ifdef __NT__
protected void set_lc_env (mapping(string:string) env)
{
  environment = ([]);
  foreach (env; string var; string val)
    environment[lower_case (var)] = val;
}
#endif

//! @decl string getenv (string varname, void|int force_update)
//! @decl mapping(string:string) getenv (void|int force_update)
//!
//! Queries the environment variables. The first variant returns the
//! value of a specific variable or zero if it doesn't exist in the
//! environment. The second variant returns the whole environment as a
//! mapping. Destructive operations on the mapping will not affect the
//! internal environment representation.
//!
//! A cached copy of the real environment is kept to make this
//! function quicker. If the optional flag @[force_update] is nonzero
//! then the real environment is queried and the cache is updated from
//! it. That can be necessary if the environment changes through other
//! means than @[putenv], typically from a C-level library.
//!
//! Variable names and values cannot be wide strings nor contain
//! @expr{'\0'@} characters. Variable names also cannot contain
//! @expr{'='@} characters.
//!
//! @note
//!   On NT the environment variable name is case insensitive.
//!
//! @seealso
//!   @[putenv()]
string|mapping(string:string) getenv (void|int|string varname,
				      void|int force_update)
{
  // Variants doesn't seem to work well yet.
  if (stringp (varname)) {
    if (!environment || force_update) {
#ifdef __NT__
      set_lc_env (Builtin._getenv());
#else
      environment = Builtin._getenv();
#endif
      // Kill the compat environment if forced.
      compat_environment = compat_environment_copy = 0;
    }

#ifdef __NT__
    varname = lower_case(varname);
#endif

    if (compat_environment) {
      array(string) res;
      if (!equal(res = compat_environment[varname],
		 compat_environment_copy[varname])) {
	// Something has messed with the compat environment mapping.
	putenv(varname, res && res[1]);
      }
    }

    return environment[varname];
  }

  else {
    force_update = varname;

    mapping(string:string) res;

    if (force_update) {
      res = Builtin._getenv();
#ifdef __NT__
      set_lc_env (res);
#else
      environment = res + ([]);
#endif
      // Kill the compat environment if forced.
      compat_environment = compat_environment_copy = 0;
    }

    else {
      if (compat_environment &&
	  !equal(compat_environment, compat_environment_copy)) {
	foreach(compat_environment; varname; array(string) pair) {
	  if (!equal(pair, compat_environment_copy[varname])) {
	    putenv(pair[0], pair[1]);
	  }
	}
	foreach(compat_environment_copy; varname; array(string) pair) {
	  if (!compat_environment[varname]) {
	    putenv(pair[0]);
	  }
	}
      }
#ifdef __NT__
      // Can't use the cached environment since variable names have been
      // lowercased there.
      res = Builtin._getenv();
      if (!environment) set_lc_env (res);
#else
      if (!environment) environment = Builtin._getenv();
      res = environment + ([]);
#endif
    }

    return res;
  }
}

void putenv (string varname, void|string value)
//! Sets the environment variable @[varname] to @[value].
//!
//! If @[value] is omitted or zero, the environment variable
//! @[varname] is removed.
//!
//! @[varname] and @[value] cannot be wide strings nor contain
//! @expr{'\0'@} characters. @[varname] also cannot contain
//! @expr{'='@} characters.
//!
//! @note
//!   On NT the environment variable name is case insensitive.
//!
//! @seealso
//! @[getenv()]
//!
{
  Builtin._putenv (varname, value);
  if (compat_environment) {
    string lvarname = varname;
#ifdef __NT__
    lvarname = lower_case(varname);
#endif
    if (value) {
      compat_environment[lvarname] =
	(compat_environment_copy[lvarname] = ({ varname, value })) + ({});
    } else {
      m_delete(compat_environment, lvarname);
      m_delete(compat_environment_copy, lvarname);
    }
  }
  if (environment) {
#ifdef __NT__
    varname = lower_case (varname);
#endif
    if (value) environment[varname] = value;
    else m_delete (environment, varname);
  }
}


//! @appears compile_file
//! Compile the Pike code contained in the file @[filename] into a program.
//!
//! This function will compile the file @[filename] to a Pike program that can
//! later be instantiated. It is the same as doing
//! @expr{@[compile_string](@[Stdio.read_file](@[filename]), @[filename])@}.
//!
//! @seealso
//! @[compile()], @[compile_string()], @[cpp()]
//!
program compile_file(string filename,
		     object|void handler,
		     void|program p,
		     void|object o)
{
  AUTORELOAD_CHECK_FILE(filename);
  return compile(cpp(master_read_file(filename),
		     filename,
		     1,
		     handler,
		     compat_major,
		     compat_minor),
		 handler,
		 compat_major,
		 compat_minor,
		 p,
		 o);
}


//! @appears normalize_path
//! Replaces "\" with "/" if runing on MS Windows. It is
//! adviced to use @[System.normalize_path] instead.
string normalize_path( string path )
{
#ifndef __NT__
  return path;
#else
  return replace(path,"\\","/");
#endif
}

//! Mapping containing the cache of currently compiled files.
//!
//! This mapping currently has the following structure:
//! @mapping
//!    @member program filename
//! @endmapping
//! The filename path separator is / on both NT and UNIX.
//!
//! @note
//!   Special cases: The current master program is available under the
//!   name @expr{"/master"@}, and the program containing the @[main]
//!   function under @expr{"/main"@}.
mapping(string:program|NoValue) programs=(["/master":this_program]);
mapping(program:object) documentation = ([]);
mapping(program:string) source_cache;

mapping (program:object|NoValue) objects=([
  this_program : this,
  object_program(_static_modules): _static_modules,
]);

mapping(string:object|NoValue) fc=([]);

// Note: It's assumed that the mappings above never decrease in size
// unless the reverse mappings above also are updated. no_value should
// otherwise be used for entries that should be considered removed.

// The reverse mapping for objects isn't only for speed; search()
// doesn't work reliably there since it calls `==.
protected mapping(program:string) rev_programs = ([]);
protected mapping(object:program) rev_objects = ([]);
protected mapping(mixed:string) rev_fc = ([]);

string programs_reverse_lookup (program prog)
//! Returns the path for @[prog] in @[programs], if it got any.
{
  // When running with trace, this function can get called
  // before __INIT has completed.
  if (!rev_programs) return UNDEFINED;
  if (sizeof (rev_programs) < sizeof (programs)) {
    foreach (programs; string path; program|NoValue prog)
      if (prog == no_value)
	m_delete (programs, path);
      else
	rev_programs[prog] = path;
  }
  return rev_programs[prog];
}

program objects_reverse_lookup (object obj)
//! Returns the program for @[obj], if known to the master.
{
  if (sizeof (rev_objects) < sizeof (objects)) {
    foreach (objects; program prog; object|NoValue obj)
      if (obj == no_value)
	m_delete (rev_objects, obj);
      else
	rev_objects[obj] = prog;
  }
  return rev_objects[obj];
}

string fc_reverse_lookup (object obj)
//! Returns the path for @[obj] in @[fc], if it got any.
{
  if (sizeof (rev_fc) < sizeof (fc)) {
    foreach (fc; string path; mixed obj)
      if (obj == no_value)
	m_delete (fc, obj);
      else
	rev_fc[obj] = path;
  }
  return rev_fc[obj];
}

array(string) query_precompiled_names(string fname)
{
  // Filenames of potential precompiled files in priority order.
#ifdef PRECOMPILED_SEARCH_MORE
  // Search for precompiled files in all module directories, not just
  // in the one where the source file is. This is useful when running
  // pike directly from the build directory.
  fname = fakeroot (fname);
  // FIXME: Not sure if this works correctly with the fakeroot and
  // module relocation stuff.
  foreach (pike_module_path, string path)
    if (has_prefix (fname, path))
      return map (pike_module_path, `+, "/", fname[sizeof (path)..], ".o");
#endif
  return ({ fname + ".o" });
}

protected class CompileCallbackError
{
  inherit _static_modules.Builtin.GenericError;
  constant is_generic_error = 1;
  constant is_compile_callback_error = 1;
  constant is_cpp_or_compilation_error = 1;
}

protected void compile_cb_error (string msg, mixed ... args)
// Use this to throw errors that should be converted to plain compile
// error messages, without backtraces being reported by
// compile_exception.
{
  if (sizeof (args)) msg = sprintf (msg, @args);
  throw (CompileCallbackError (msg, backtrace()[..<1]));
}

protected void compile_cb_rethrow (object|array err)
// Use this to rethrow errors that should be converted to plain
// compile error messages, without backtraces being reported by
// compile_exception.
{
  array bt;
  if (array|object e = catch (bt = get_backtrace (err)))
    handle_error (e);
  throw (CompileCallbackError (call_describe_error (err), bt));
}

protected void call_compile_warning (object handler, string file,
				     string msg, mixed ... args)
{
  if (sizeof (args)) msg = sprintf (msg, @args);
  msg = trim_all_whites (msg);
  if (handler && handler->compile_warning)
    handler->compile_warning (file, 0, msg);
  else if (master()->compile_warning)
    master()->compile_warning (file, 0, msg);
  else
    compile_warning (file, 0, msg);
}

#if constant(_static_modules.Builtin.mutex)
#define THREADED
_static_modules.Builtin.mutex compilation_mutex = Builtin.mutex();
#endif

#ifdef __NT__
#define FIX_CASE(X)	lower_case(X)
#else
#define FIX_CASE(X)	(X)
#endif /* __NT__ */

protected string base_from_filename(string fname)
{
  string low_name = FIX_CASE(fname);
  if (has_prefix(low_name, ".#")) return 0;
  if (has_suffix(low_name, ".pike") ||
      has_suffix(low_name, ".pmod")) {
    return fname[..<5];
  }
  if (has_suffix(low_name, ".so")) {
    return fname[..<3];
  }
  return 0;
}

protected int prio_from_filename(string fname)
{
  fname = FIX_CASE(fname);
  if (has_suffix(fname, ".pmod")) return 3;
  if (has_suffix(fname, ".so")) return 2;
  if (has_suffix(fname, ".pike")) return 1;

  // FIXME: Warn here?
  return 0;
}

//! Find the files in which @[mod] is defined, as they may be hidden away in 
//! joinnodes and dirnodes
//!
//! @param mod
//!   The module we are looking for.
//!
//! @returns
//!   An array of strings with filenames.
//!   (one for each file in a joinnode, or just one otherwise)
array(string) module_defined(object|program mod)
{
  array files = ({});
  if (programp(mod))
    return ({ Builtin.program_defined([program]mod) });

  array mods;
  if (mod->is_resolv_joinnode)
    mods = mod->joined_modules; 
  else
    mods = ({ mod });

  foreach (mods;; object mod)
  {
    if (mod->is_resolv_dirnode)
      files += ({ Builtin.program_defined(object_program(mod->module)) });
    else
      files += ({ Builtin.program_defined(object_program(mod)) });
  }
  return files;
}

//! Enable caching of sources from compile_string()
void enable_source_cache()
{
  if (!source_cache)
    source_cache = ([]);
}

//! Show documentation for the item @[obj]
//!
//! @param obj
//!   The object for which the documentation should be shown
//!
//! @returns
//!   an AutoDoc object
object show_doc(program|object|function obj)
{ 
  object doc_extractor = main_resolv("Tools.AutoDoc.PikeExtractor.extractClass"); 
  string child;
  program prog;

  if (programp(obj))
    prog = obj;
  if (functionp(obj))
  {
    prog = function_program(obj);
    child = ((describe_function(obj)||"")/"->")[-1];
  }
  if (objectp(obj))
  {
    if (obj->is_resolv_joinnode)
      obj = obj->joined_modules[0]; // FIXME: check for multiples
    if (obj->is_resolv_dirnode)
      prog = object_program(obj->module);
    else
      prog = object_program(obj);
  }


  if (prog && !documentation[prog] && doc_extractor)
  {
    string source;
    if (source_cache && source_cache[prog])
      source = source_cache[prog];
    else
    {
      array sourceref = array_sscanf(Builtin.program_defined(prog), 
                                     "%s%[:]%[0-9]");
      source = master_read_file(sourceref[0]);
      if (sizeof(sourceref[1]) && sizeof(sourceref[2]))
      {
        if (programp(prog))
          child = ((describe_program(prog)||"")/".")[-1];
      }
    }

    if (source)
    {
      catch
      {
        documentation[prog] = doc_extractor(source, sprintf("%O", prog));
      };
      //FIXME: handle this error somehow
    }
  }

  if (documentation[prog])
  { 
    if (child)
      return documentation[prog]->findObject(child)||documentation[prog]->findChild(child);
    else
      return documentation[prog];
  }
}

// Make it possible to customize the backtrace formatting.
protected string call_describe_backtrace(array(mixed) trace,
					 void|int linewidth)
{
  return (all_constants()["describe_backtrace"]||describe_backtrace)
    (trace, linewidth);
}

protected string call_describe_error(mixed /* object|array */ err)
{
  return (all_constants()["describe_error"]||describe_error)(err);
}

protected program low_findprog(string pname,
			       string ext,
			       object|void handler,
			       void|int mkobj)
{
  program ret;
  Stat s;
  string fname=pname+ext;

  resolv_debug("low_findprog(%O, %O, %O, %O)\n",
	       pname, ext, handler, mkobj);

#ifdef THREADED
  object key;
  // The catch is needed, since we might be called in a context when
  // threads are disabled. (compile() disables threads).
  mixed err = catch {
    key=compilation_mutex->lock(2);
  };
  if (err && !has_suffix (err[0], "when threads are disabled!"))
    throw (err);
#endif

#ifdef PIKE_MODULE_RELOC
  fname = unrelocate_module(fname);
#endif

#ifdef __NT__
  // Ugly kluge to work better with cygwin32 "/X:/" paths.
  if(getenv("OSTYPE")=="cygwin32")
  {
    string tmp=fname[..1];
    if((tmp=="//" || tmp=="\\\\") && (fname[3]=='/' || fname[3]=='\\'))
    {
      if(!master_file_stat(fname))
      {
	fname=fname[2..2]+":"+fname[3..];
      }
    }
  }
#endif

  if( (s=master_file_stat(fakeroot(fname))) && s->isreg )
  {
#ifdef PIKE_AUTORELOAD
    if(!autoreload_on || load_time[fname] >= s->mtime)
#endif
    {
      if(!zero_type (ret=programs[fname]) && ret != no_value) {
	resolv_debug ("low_findprog %s: returning cached (no autoreload)\n", fname);
	return ret;
      }
    }

    AUTORELOAD_BEGIN();

#ifdef PIKE_AUTORELOAD
    if (load_time[fname] >= s->mtime)
      if (!zero_type (ret=programs[fname]) && ret != no_value) {
	resolv_debug ("low_findprog %s: returning cached (autoreload)\n", fname);
	return ret;
      }
#endif

    switch(ext)
    {
    case "":
    case ".pike":
      foreach(query_precompiled_names(fname), string oname) {
	if(Stat s2=master_file_stat(fakeroot(oname)))
	{
	  if(s2->isreg && s2->mtime >= s->mtime)
	  {
	    mixed err=catch {
	      object|program decoded;
	      AUTORELOAD_CHECK_FILE(oname);
	      resolv_debug ("low_findprog %s: decoding dumped\n", fname);
	      INC_RESOLV_MSG_DEPTH();
	      decoded = decode_value(master_read_file(oname),
				     (handler && handler->get_codec ||
				      get_codec)(fname, mkobj, handler));
	      DEC_RESOLV_MSG_DEPTH();
	      resolv_debug ("low_findprog %s: dump decode ok\n", fname);
	      if (decoded && decoded->this_program_does_not_exist) {
		resolv_debug ("low_findprog %s: program claims not to exist\n",
			      fname);
		return programs[fname] = 0;
	      }
	      else {
		if (objectp(decoded)) {
		  resolv_debug("low_findprog %s: decoded object %O\n",
			       fname, decoded);
		  objects[ret = object_program(decoded)] = decoded;
		} else {
		  ret = decoded;
		}
		resolv_debug("low_findprog %s: returning %O\n", fname, ret);
		return programs[fname]=ret;
	      }
	    };
	    DEC_RESOLV_MSG_DEPTH();
	    resolv_debug ("low_findprog %s: dump decode failed\n", fname);
	    programs[fname] = no_value;
	    call_compile_warning (handler, oname,
				  "Decode failed: " +
				  call_describe_error(err));
	    // handle_error(err);
	  } else if (out_of_date_warning) {
	    call_compile_warning (handler, oname,
				  "Compiled file is out of date");
	  }
	}
      }

      resolv_debug ("low_findprog %s: compiling, mkobj: %O\n", fname, mkobj);
      INC_RESOLV_MSG_DEPTH();
      programs[fname]=ret=__empty_program(0, fname);
      AUTORELOAD_CHECK_FILE (fname);
      string src;
      if (array|object err = catch (src = master_read_file (fname))) {
	DEC_RESOLV_MSG_DEPTH();
	resolv_debug ("low_findprog %s: failed to read file\n", fname);
	objects[ret] = no_value;
	ret=programs[fname]=0;	// Negative cache.
	compile_cb_rethrow (err);
      }
      if ( mixed e=catch {
	  ret=compile_string(src, fname, handler,
			     ret,
			     mkobj? (objects[ret]=__null_program()) : 0);
	} )
      {
	DEC_RESOLV_MSG_DEPTH();
	resolv_debug ("low_findprog %s: compilation failed\n", fname);
	objects[ret] = no_value;
	ret=programs[fname]=0;	// Negative cache.
        throw(e);
      }
      DEC_RESOLV_MSG_DEPTH();
      resolv_debug ("low_findprog %s: compilation ok\n", fname);
      break;

#if constant(load_module)
    case ".so":
      if (fname == "") {
	werror( "low_findprog(%O, %O) => load_module(\"\")\n"
		"%s\n", pname, ext, call_describe_backtrace(backtrace()) );
      }

      if (array|object err = catch (ret = load_module(fakeroot(fname)))) {
	DEC_RESOLV_MSG_DEPTH();
	resolv_debug ("low_findprog %s: failed to load binary\n", fname);
	objects[ret] = no_value;
	ret=programs[fname]=0;	// Negative cache.
	if (objectp (err) && err->is_module_load_error)
	  // Do not treat errors from dlopen(3) as exceptions since in
	  // a dist we can have .so files that are dynamically linked
	  // against libraries that don't exist on the system, and in
	  // that case we should just treat the module as nonexisting.
	  //
	  // What we really want is to do this only for errors that
	  // are due to nonexisting files, but the error reporting
	  // from dlerror(3) doesn't allow us to tell those from other
	  // errors.
	  call_compile_warning (handler, fname,
				"Failed to load library: %s\n", err->reason);
	else
	  compile_cb_rethrow (err);
      }
      else
	resolv_debug ("low_findprog %s: loaded binary\n", fname);
#endif /* load_module */
    }

    AUTORELOAD_FINISH(ret,programs,fname);

    if (ret && ret->this_program_does_not_exist) {
      resolv_debug ("low_findprog %s: program says it doesn't exist\n", fname);
      return programs[fname] = 0;
    }
    else {
      resolv_debug("low_findprog %s: returning %O\n", fname, ret);
      return programs[fname]=ret;
    }
  }

  resolv_debug ("low_findprog %s: file not found\n", fname);
  return 0;
}

//
// This function is called by the compiler when a delayed compilation
// error occurs in the given program. It should remove all references
// to the program so that it can be freed.
//
void unregister(program p)
{
  // werror("Unregistering %O...\n", p);
  if(string fname=rev_programs[p] || search(programs,p)) {
    resolv_debug("unregister %s\n", fname);
    if (m_delete (rev_programs, p))
      m_delete (programs, fname);
    else
      programs[fname] = no_value;

    fname = dirname (fname);
    object n;
    if ( fname!="" && objectp (n = fc[fname]) )
      if (n->is_resolv_dirnode || n->is_resolv_joinnode)
	n->delete_value (p);
  }

  object o = m_delete(objects, p);
  if (objectp (o)) {
    m_delete(rev_objects, o);
  }

  foreach (fc; string name; mixed mod)
    if (objectp(mod) && object_program(mod) == p)
      if (m_delete (rev_fc, mod))
	m_delete (fc, name);
      else
	fc[name] = no_value;

  // FIXME: Delete from caches in dirnodes and joinnodes.
}

protected program findprog(string pname,
			   string ext,
			   object|void handler,
			   void|int mkobj)
{
  switch(ext)
  {
  case ".pike":
  case ".so":
    return low_findprog(pname,ext,handler, mkobj);

  default:
    pname+=ext;
    return
      low_findprog(pname,"", handler, mkobj) ||
      low_findprog(pname,".pike", handler, mkobj) ||
      low_findprog(pname,".so", handler, mkobj);
  }
}

program low_cast_to_program(string pname,
			    string current_file,
			    object|void handler,
			    void|int mkobj)
{
  string ext;
  string nname;

  //werror("low_cast_to_program(%O, %O, %O, %O)\n",
  //	 pname, current_file, handler, mkobj);

  if(sscanf(reverse(BASENAME(pname)),"%s.%s",ext, nname))
  {
    ext="."+reverse(ext);
    pname=pname[..<sizeof(ext)];
  }
  else {
    ext="";
  }

  if(IS_ABSOLUTE_PATH(pname))
  {
    program|NoValue prog = programs[pname];
    if ((!zero_type(prog)) && (prog != no_value))
    { 
      return prog;
    }
    pname=combine_path("",pname);
    return findprog(pname,ext,handler,mkobj);
  }
  else {
    string cwd;
    if(current_file)
    {
      cwd=dirname(current_file);
    }
    else {
      cwd=getcwd();
    }

    if(program ret=findprog(combine_path(cwd,pname),ext,handler,mkobj))
      return ret;

    foreach(pike_program_path, string path)
      if(program ret=findprog(combine_path(path,pname),ext,handler,mkobj))
	return ret;

    return 0;
  }
}


//! This function is called when the driver wants to cast a string
//! to a program, this might be because of an explicit cast, an inherit
//! or a implict cast. In the future it might receive more arguments,
//! to aid the master finding the right program.
program cast_to_program(string pname,
			string current_file, 
			object|void handler)
{
  resolv_debug ("cast_to_program(%O, %O)\n", pname, current_file);
  INC_RESOLV_MSG_DEPTH();
  program ret = low_cast_to_program(pname, current_file, handler);
  DEC_RESOLV_MSG_DEPTH();
  resolv_debug ("cast_to_program(%O, %O) => %O\n", pname, current_file, ret);
  if (programp (ret)) return ret;
  error("Cast %O to program failed%s.\n",
	pname,
	(current_file && current_file!="-") ? sprintf(" in %O",current_file) : "");
}


//! This function is called when an error occurs that is not caught
//! with catch().
void handle_error(array|object trace)
{
  // NB: Use predef::trace() to modify trace level here.
  // predef::trace(2);
  if(mixed x=catch {
    werror(call_describe_backtrace(trace));
  }) {
    // One reason for this might be too little stack space, which
    // easily can occur for "out of stack" errors. It should help to
    // tune up the STACK_MARGIN values in interpret.c then.
    werror("Error in handle_error in master object:\n");
    if(catch {
      // NB: Splited werror calls to retain some information
      //     even if/when werror throws.
      catch {
	if (catch {
	  string msg = [string]x[0];
	  array bt = [array]x[1];
	  werror("%s", msg);
	  werror("%O\n", bt);
	}) {
	  werror("%O\n", x);
	}
      };
      werror("Original error:\n"
	     "%O\n", trace);
    }) {
      werror("sprintf() failed to write error.\n");
    }
  }
  // predef::trace(0);
}

/* This array contains the names of the functions
 * that a replacing master-object may want to override.
 */
constant master_efuns = ({
  "error",
  "basename",
  "dirname",
  "is_absolute_path",
  "explode_path",

  "compile_string",
  "compile_file",
  "add_include_path",
  "remove_include_path",
  "add_module_path",
  "remove_module_path",
  "add_program_path",
  "remove_program_path",
  "describe_backtrace",
  "describe_error",
  "get_backtrace",
  "normalize_path",
  "bool",
  "true",
  "false",
  "getenv",
  "putenv",

#ifdef GETCWD_CACHE
  "cd",
  "getcwd",
#endif
});

enum bool { false=0, true=1 };

//! Prefix for Pike-related C header files.
string include_prefix;

//! Prefix for autodoc files.
string doc_prefix;

//! Flags suitable for use when compiling Pike C modules
string cflags;

//! Flags suitable for use when linking Pike C modules
string ldflags; // Not yet used

//! @decl int strlen(string|multiset|array|mapping|object thing)
//! @appears strlen
//! Alias for @[sizeof].
//! @deprecated sizeof

//! @decl int write(string fmt, mixed ... args)
//! @appears write
//! Writes a string on stdout. Works just like @[Stdio.File.write]
//! on @[Stdio.stdout].

//! @decl int werror(string fmt, mixed ... args)
//! @appears werror
//! Writes a string on stderr. Works just like @[Stdio.File.write]
//! on @[Stdio.stderr].

/* Note that create is called before add_precompiled_program
 */
protected void create()
{
  foreach(master_efuns, string e)
    if (!zero_type(this[e]))
      add_constant(e, this[e]);
    else
      error("Function %O is missing from master.pike.\n", e);

  add_constant("__dirnode", dirnode);
  add_constant("__joinnode", joinnode);

  add_constant("strlen", sizeof);
  add_constant("write", write);
  add_constant("werror", werror);
  // To make it possible to overload get_dir and file_stat later on.
  // It's not possible to replace efuns with normal functions in .o-files

  add_constant("get_dir", master_get_dir );
  add_constant("file_stat", lambda( string f, int|void d ) { return file_stat(f,d);} );

#define CO(X)  add_constant(#X,Builtin.__backend->X)
  CO(call_out);
  CO(_do_call_outs);
  CO(find_call_out);
  CO(remove_call_out);
  CO(call_out_info);

#if "#share_prefix#"[0]!='#'
  // add path for architecture-independant files
  add_include_path("#share_prefix#/include");
  add_module_path("#share_prefix#/modules");
#endif

#if "#lib_prefix#"[0]!='#'
  // add path for architecture-dependant files
  add_include_path("#lib_prefix#/include");
  add_module_path("#lib_prefix#/modules");
#endif

  // Debian paths
  add_include_path("/usr/local/lib/pike"+__REAL_MAJOR__+"."+__REAL_MINOR__+"/include");
  add_module_path("/usr/local/lib/pike"+__REAL_MAJOR__+"."+__REAL_MINOR__+"/modules");

#if "#cflags# "[0]!='#'
  cflags = "#cflags#";
#endif

#if "#ldflags# "[0]!='#'
  ldflags = "#ldflags#";
#endif

#if "#include_prefix#"[0]!='#'
  include_prefix = "#include_prefix#";
  cflags = (cflags || "") + " -I" + dirname(include_prefix);
#endif

#if "#doc_prefix#"[0]!='#'
  doc_prefix = "#doc_prefix#";
#endif

#if constant(__embedded_resource_directory)
  // for use with embedded interpreters
  // add path for architecture-dependant files
  add_include_path(__embedded_resource_directory + "/lib/include");
  add_module_path(__embedded_resource_directory + "/lib/modules");
  add_module_path(__embedded_resource_directory + "/" + replace(uname()->machine, " ", "_") + "/modules");

#endif

  system_module_path=pike_module_path;
}


//! This function is called whenever a inherit is called for.
//! It is supposed to return the program to inherit.
//! The first argument is the argument given to inherit, and the second
//! is the file name of the program currently compiling. Note that the
//! file name can be changed with #line, or set by compile_string, so
//! it can not be 100% trusted to be a filename.
//! previous_object(), can be virtually anything in this function, as it
//! is called from the compiler.
program handle_inherit(string pname, string current_file, object|void handler)
{
  resolv_debug ("handle_inherit(%O, %O)\n", pname, current_file);
  INC_RESOLV_MSG_DEPTH();
  program ret = cast_to_program(pname, current_file, handler);
  DEC_RESOLV_MSG_DEPTH();
  resolv_debug ("handle_inherit(%O, %O) => %O\n", pname, current_file, ret);
  return ret;
}

object low_cast_to_object(string oname, string current_file,
			  object|void current_handler)
{
  program p;
  object o;

  p = low_cast_to_program(oname, current_file, current_handler, 1);
  if(!p) return 0;
  // NB: p might be a function in a fake_object...
  if(!objectp (o=objects[p])) o=objects[p]=p();
  return o;
}

//! This function is called when the drivers wants to cast a string
//! to an object because of an implict or explicit cast. This function
//! may also receive more arguments in the future.
object cast_to_object(string oname, string current_file,
		      object|void current_handler)
{
  resolv_debug ("cast_to_object(%O, %O)\n", oname, current_file);
  INC_RESOLV_MSG_DEPTH();
  object o = low_cast_to_object(oname, current_file, current_handler);
  DEC_RESOLV_MSG_DEPTH();
  resolv_debug ("cast_to_object(%O, %O) => %O\n", oname, current_file, o);
  if (objectp (o)) return o;
  error("Cast %O to object failed%s.\n",
	oname,
	(current_file && current_file!="-") ? sprintf(" in %O",current_file) : "");
}

// Marker used for negative caching in module caches.
// FIXME: Won't this cause problems when inheriting "/master"?
protected class ZERO_TYPE {};

protected object Unicode;

//! Module node representing a single directory.
//!
//! @seealso
//!   @[joinnode]
class dirnode
{
  string dirname;
  object|void compilation_handler;
  constant is_resolv_dirnode = 1;
  // objectp() is intentionally not used on the module object, to
  // allow a module to deny its own existence with `!.
  mixed module;
  mapping(string:mixed) cache=([]);
  mapping(string:array(string)) file_paths = ([]);

#ifdef __NT__
#define FIX_CASE(X)	lower_case(X)
#else
#define FIX_CASE(X)	(X)
#endif /* __NT__ */

  protected string base_from_filename(string fname)
  {
    string low_name = FIX_CASE(fname);
    catch {
      // FIXME: Warn on failure?
      low_name = utf8_to_string(low_name);
      if (Builtin.string_width(low_name) > 8) {
	// We might need to normalize the string (cf MacOS X).

	// Load the Unicode module if it hasn't already been loaded.
	if (!Unicode) {
	  Unicode = resolv("Unicode");
	}
	low_name = Unicode.normalize(low_name, "NFC");
      }
    };
    if (has_prefix(low_name, ".#")) return 0;
    if (has_suffix(low_name, ".pike") ||
	has_suffix(low_name, ".pmod")) {
      return fname[..<5];
    }
    if (has_suffix(low_name, ".so")) {
      return fname[..<3];
    }
    return 0;
  }

  protected int prio_from_filename(string fname)
  {
    fname = FIX_CASE(fname);
    if (has_suffix(fname, ".pmod")) return 3;
    if (has_suffix(fname, ".so")) return 2;
    if (has_suffix(fname, ".pike")) return 1;

    // FIXME: Warn here?
    return 0;
  }

  protected void create(string d, object|void h)
  {
    resolv_debug ("dirnode(%O,%O) created\n",d,h);
    dirname=d;
    compilation_handler=h;
    fc[dirname]=this;
    array(string) files = sort(master_get_dir(d)||({}));
    if (!sizeof(d)) return;
    array(string) bases = map(files, base_from_filename);
    files = filter(files, bases);
    bases = filter(bases, bases);
    resolv_debug("dirnode(%O,%O) got %d files.\n",
		 d, h, sizeof(bases));
    if (!sizeof(files)) return;

    foreach(files; int no; string fname) {
      fname = combine_path(dirname, fname);
      string base = bases[no];
      if (base == "module") {
	// We need a module_checker.
	module = module_checker();
      }
      array(string) paths = file_paths[base];
      if (!paths) {
	// New entry.
	file_paths[base] = ({ fname });
	continue;
      }

      // Multiple files. Order according to prio_from_filename().
      // Insert sort. Worst case is 3 filenames.
      int prio = prio_from_filename(fname);
      int index;
      foreach(paths; index; string other_fname) {
	if (prio_from_filename(other_fname) <= prio) break;
      }
      file_paths[base] = paths[..index-1] + ({ fname }) + paths[index..];
    }
  }

  class module_checker
  {
    int `!()
    {
      resolv_debug ("dirnode(%O)->module_checker()->`!()\n",dirname);
      INC_RESOLV_MSG_DEPTH();

      if (mixed err = catch { 
	// Look up module.
	if (module = cache["module"] || low_ind("module", 1)) {
	  /* This allows for `[] to have side effects first time
	   * it is called. (Specifically, the Calendar module uses
	   * this).
	   */
	  cache=([]);
	  _cache_full=0;
	}
	DEC_RESOLV_MSG_DEPTH();
	resolv_debug("dirnode(%O)->module_checker()->`!() => %s\n",
		     dirname, !module ? "doesn't exist" : "exists");
	return !module;
      }) {
	//werror ("findmodule error: " + call_describe_backtrace (err));

	// findmodule() failed. This can occur due to circularities
	// between encode_value()'ed programs.
	// The error will then typically be:
	// "Cannot call functions in unfinished objects."

	// Pretend not to exist for now...
	DEC_RESOLV_MSG_DEPTH();
	resolv_debug("dirnode(%O)->module_checker()->`!() => failure, doesn't exist\n",
		     dirname);
	return 1;
      }
    }

    mixed `[](string index)
      {
	resolv_debug ("dirnode(%O)->module_checker()[%O] => %O\n",
		      dirname, index, module && module[index]);
	return module && module[index];
      }
    array(string) _indices() { if(module) return indices(module); }
    array _values() { if(module) return values(module); }
  }

  protected mixed low_ind(string index, int(0..1)|void set_module)
  {
    array(string) paths;

    if (!(paths = file_paths[index])) {
      DEC_RESOLV_MSG_DEPTH();
      resolv_debug("dirnode(%O)->ind(%O) => no file match\n",
		   dirname, index);
      return UNDEFINED;
    }

    foreach(paths, string fname) {
      resolv_debug("dirnode(%O)->ind(%O) Trying file %O...\n",
		   dirname, index, fname);
      Stat stat = master_file_stat(fakeroot(fname));
      if (!stat) {
	resolv_debug("dirnode(%O)->ind(%O) file %O disappeared!\n",
		     dirname, index, fname);
	continue;
      }
      if (has_suffix(fname, ".pmod")) {
	if (stat->isdir) {
	  if (dirnode n = fc[fname]) {
	    // Avoid duplicate dirnodes for the same dirs. This can
	    // happen if the master is replaced, e.g. with master_76
	    // in 7.6/modules/__default.pmod.
	    resolv_debug("dirnode(%O)->ind(%O) => found subdirectory %O, "
			 "returning old dirnode\n", dirname, index, fname);
	    return n;
	  }
	  resolv_debug("dirnode(%O)->ind(%O) => found subdirectory %O, "
		       "creating new dirnode\n", dirname, index, fname);
	  return fc[fname] = dirnode(fname, compilation_handler);
	}
	resolv_debug("dirnode(%O)->ind(%O) casting (object)%O\n",
		     dirname, index, fname);
	// FIXME: cast_to_program() and cast_to_object()
	//        have lots of overhead to guess the proper
	//        filename. This overhead isn't needed in
	//        our cases, so we could make do with
	//        low_findprog() and the caches.
	mixed ret;
	if (ret = catch {
	    if (objectp(ret = low_cast_to_object(fname, 0, compilation_handler))) {
	      // This assignment is needed for eg the Calendar module.
	      if (set_module) module = ret;
	      if(mixed tmp=ret->_module_value) ret=tmp;
	      DEC_RESOLV_MSG_DEPTH();
	      resolv_debug("dirnode(%O)->ind(%O) => found submodule %O:%O\n",
			   dirname, index, fname, ret);
	      return ret;
	    }
	  }) {
	  resolv_debug("dirnode(%O)->ind(%O) ==> Cast to object failed: %s\n",
		       dirname, index, call_describe_backtrace(ret));
	}
      } else {
	resolv_debug("dirnode(%O)->ind(%O) casting (program)%O\n",
		     dirname, index, fname);
	program|object ret;
	if (ret = low_cast_to_program(fname, 0, compilation_handler)) {
	  DEC_RESOLV_MSG_DEPTH();
	  resolv_debug("dirnode(%O)->ind(%O) => found subprogram %O:%O\n",
		       dirname, index, fname, ret);
#if constant(load_module)
	  if (has_suffix(fname, ".so")) {
	    // This is compatible with 7.4 behaviour.
	    if (!ret->_module_value) {
	      object o;
	      // NB: p might be a function in a fake_object...
	      if(!objectp (o=objects[ret])) o=objects[ret]=ret();
	      ret = o;
	    }
	    if(mixed tmp=ret->_module_value) ret=tmp;
	  }
#endif
	  return ret;
	}
      }
      resolv_debug("dirnode(%O)->ind(%O) => failure for file %O\n",
		   dirname, index, fname);
    }

    resolv_debug("dirnode(%O)->ind(%O) => UNDEFINED\n",
		 dirname, index);
    return UNDEFINED;
  }

  protected mixed ind(string index)
  {
    resolv_debug ("dirnode(%O)->ind(%O)\n", dirname, index);
    INC_RESOLV_MSG_DEPTH();

    if (_cache_full) {
      DEC_RESOLV_MSG_DEPTH();
      resolv_debug("dirnode(%O)->ind(%O) => cache_full %O\n",
		   dirname, index, cache[index]);
      return cache[index];
    }

    if(module)
    {
      mixed o;
//      _describe(module);
      if(!zero_type(o=module[index]))
      {
	DEC_RESOLV_MSG_DEPTH();
	resolv_debug ("dirnode(%O)->ind(%O) => found %O\n",
		      dirname, index, o);
	return o;
      }
      resolv_debug ("dirnode(%O)->ind(%O) => not found in module\n",
		    dirname, index);
    }
    else
      resolv_debug ("dirnode(%O)->ind(%O) => no module\n", dirname, index);

    return low_ind(index);
  }

  mixed `[](string index)
  {
    mixed ret;
#ifdef MODULE_TRACE
    werror("%*nDirnode(%O) cache[%O] ?????\n",
	   sizeof(backtrace()),dirname,index);
#endif
    if(!zero_type(ret=cache[index]))
    {
#ifdef MODULE_TRACE
      werror("%*nDirnode(%O) cache[%O] => %O%s\n",
	     sizeof(backtrace()),dirname,index, ret,
	     (ret != ZERO_TYPE)?"":" (zero_type)");
#endif
      if (ret != ZERO_TYPE) return ret;
#ifdef MODULE_TRACE
      werror("%*nDirnode(%O) ZERO_TYPE!\n",
	     sizeof(backtrace()),dirname);
#endif
      return UNDEFINED;
    }
    ret=ind(index);

    // We might have gotten placeholder objects in the first pass
    // which must not be cached to the second.
    if(ret == predef::__placeholder_object) {
#ifdef MODULE_TRACE
      werror("%*nDirnode(%O) PLACE_HOLDER.\n",
	     sizeof(backtrace()),dirname);
#endif
      return ret;
    }

    cache[index] = zero_type(ret) ? ZERO_TYPE : ret;
    return ret;
  }

  mixed safe_index(string index)
  {
    mixed err;
    resolv_debug ("dirnode(%O):     %O...\n", dirname, index);
    if (err = catch { return `[](index); }) {
      call_compile_warning (compilation_handler,
			    dirname+"."+fname,
			    "Compilation failed: " +
			    call_describe_error(err));
    }
    return UNDEFINED;
  }

  protected int(0..1) _cache_full;
  void fill_cache()
  {
#if 0
    werror(call_describe_backtrace(({ sprintf("Filling cache in dirnode %O\n",
					      dirname),
				      backtrace() })));
#endif
    if (_cache_full) {
      return;
    }

    resolv_debug ("dirnode(%O) => Filling cache...\n", dirname);

    // NOTE: We rely on side effects in `[]() and safe_index()
    //       to fill the cache.

    // Why shouldn't thrown errors be propagated here? /mast
    if (module) {
      resolv_debug("dirnode(%O): module: %O, indices:%{%O, %}\n",
		   dirname, module, indices(module));
      map(indices(module), safe_index);
    }

    map(indices(file_paths), safe_index);
    _cache_full = (object_program(module) != __null_program);
    resolv_debug ("dirnode(%O) => Cache %s.\n", dirname,
		  _cache_full?"full":"partially filled");
  }

  protected array(string) _indices()
  {
    fill_cache();
    // Note: Cannot index cache at all here to filter out the
    // ZERO_TYPE values since that can change the order in the
    // mapping, and _indices() has to return the elements in the same
    // order as a nearby _values() call.
    return filter (indices (cache), map (values (cache), `!=, ZERO_TYPE));
  }

  protected array(mixed) _values()
  {
    fill_cache();
    return values(cache) - ({ZERO_TYPE});
  }

  void delete_value (mixed val)
  {
    if (string name = search (cache, val)) {
      m_delete (cache, name);
      _cache_full = 0;
    }
  }

  protected int(0..) _sizeof() {
    return sizeof(_values());
  }

  protected string _sprintf(int as)
  {
    return as=='O' && sprintf("master()->dirnode(%O:%O)",
			      dirname, module && module);
  }
}

//! Module node holding possibly multiple directories,
//! and optionally falling back to another level.
//!
//! @seealso
//!   @[dirnode]
class joinnode
{
  constant is_resolv_joinnode = 1;
  array(object|mapping) joined_modules;
  mapping(string:mixed) cache=([]);

  object compilation_handler;

  // NOTE: Uses the empty mapping as the default fallback
  // for simplified code.
  joinnode|mapping(mixed:int(0..0)) fallback_module = ([]);

  string _sprintf(int as)
  {
    return as=='O' && sprintf("master()->joinnode(%O)",joined_modules);
  }

  protected void create(array(object|mapping) _joined_modules,
			object|void _compilation_handler,
			joinnode|void _fallback_module)
  {
    joined_modules = _joined_modules;
    compilation_handler = _compilation_handler;
    fallback_module = _fallback_module || ([]);
    resolv_debug ("joinnode(%O) created\n", joined_modules);
  }

  void add_path(string path)
  {
    path = combine_path(getcwd(), path);
    dirnode node = fc[path] ||
      (fc[path] = dirnode(path, compilation_handler));
    if (sizeof(joined_modules) &&
	joined_modules[0] == node) return;
    joined_modules = ({ node }) + (joined_modules - ({ node }));
    cache = ([]);
  }

  void rem_path(string path)
  {
    path = combine_path(getcwd(), path);
    joined_modules = filter(joined_modules,
			    lambda(dirnode node) {
			      return !objectp(node) ||
				!node->is_resolv_dirnode ||
				(node->dirname != path);
			    });
    cache = ([]);
  }

  protected mixed ind(string index)
  {
    resolv_debug ("joinnode(%O)->ind(%O)\n", joined_modules, index);
    INC_RESOLV_MSG_DEPTH();

    array(mixed) res = ({});
    foreach(joined_modules, object|mapping o) 
    {
      mixed ret;
      if (!zero_type(ret = o[index])) 
      {
	if (objectp(ret) &&
	    (ret->is_resolv_dirnode || ret->is_resolv_joinnode))
        {
	  // Only join directorynodes (or joinnodes).
	  res += ({ ret });
	} else {
	  DEC_RESOLV_MSG_DEPTH();
	  resolv_debug ("joinnode(%O)->ind(%O) => found %O\n",
			joined_modules, index, ret);
	  return (ret);
	}
      }
    }

    if (sizeof(res)) {
      DEC_RESOLV_MSG_DEPTH();
      resolv_debug("joinnode(%O)->ind(%O) => new joinnode, fallback: %O\n",
		   joined_modules, index, fallback_module[index]);
      return joinnode(res, compilation_handler, fallback_module[index]);
    }

    DEC_RESOLV_MSG_DEPTH();
    resolv_debug ("joinnode(%O)->ind(%O) => not found. Trying fallback %O\n",
		  joined_modules, index, fallback_module);
    return fallback_module[index];
  }

  mixed `[](string index)
  {
    mixed ret;
    if (!zero_type(ret = cache[index])) {
      if (ret != ZERO_TYPE) {
	return ret;
      }
      return UNDEFINED;
    }
    ret = ind(index);

    // We might have gotten placeholder objects in the first pass
    // which must not be cached to the second.
    if(ret == predef::__placeholder_object) return ret;

    if (zero_type(ret)) {
      cache[index] = ZERO_TYPE;
    } else {
      cache[index] = ret;
    }
    return ret;
  }

  protected int _cache_full;

  void fill_cache()
  {
#if 0
    werror(call_describe_backtrace(({ "Filling cache in joinnode\n",
				      backtrace() })));
#endif
    if (_cache_full) {
      return;
    }
    foreach(joined_modules, object|mapping|program o) {
      foreach(indices(o), string index) {
	if (zero_type(cache[index])) {
	  `[](index);
	}
      }
    }
    foreach(indices(fallback_module), string index) {
      `[](index);
    }
    _cache_full = 1;
  }

  array(string) _indices()
  {
    fill_cache();
    // Note: Cannot index cache at all here to filter out the
    // ZERO_TYPE values since that can change the order in the
    // mapping, and _indices() has to return the elements in the same
    // order as a nearby _values() call.
    return filter (indices (cache), map (values (cache), `!=, ZERO_TYPE));
  }

  array(mixed) _values()
  {
    fill_cache();
    return values(cache) - ({ZERO_TYPE});
  }

  void delete_value (mixed val)
  {
    if (string name = search (cache, val))
      m_delete (cache, name);
    for (int i = 0; i < sizeof (joined_modules); i++) {
      object|mapping|program o = joined_modules[i];
      if (o == val) {
	joined_modules = joined_modules[..i - 1] + joined_modules[i + 1..];
	i--;
      }
      else if (objectp (o) && (o->is_resolv_dirnode || o->is_resolv_joinnode))
	o->delete_value (val);
      else if (string name = mappingp (o) && search (o, val))
	m_delete (o, name);
    }
  }

  int `== (mixed other)
  {
    return objectp (other) && (other->is_resolv_joinnode == 1) &&
      equal (mkmultiset (joined_modules), mkmultiset (other->joined_modules));
  }

  array(object) _encode()
  {
    return joined_modules;
  }

  void _decode (array(object) joined_modules)
  {
    this_program::joined_modules = joined_modules;
  }
}

joinnode handle_import(string path, string|void current_file,
		       object|void current_handler)
{
#ifdef __amigaos__
  if(path == ".")
    path = "";
#endif
  if(current_file)
  {
    path = combine_path_with_cwd(dirname(current_file), path);
  } else {
    path = combine_path_with_cwd(path);
  }

  // FIXME: Need caching!!!
#if 0
  // FIXME: This caching strategy could be improved,
  //        since it ignores module_nodes from the
  //        ordinary module tree.

  if (module_node_cache[current_handler]) {
    if (module_node_cache[current_handler][path]) {
      return module_node_cache[current_handler][path];
    }
  } else {
    module_node_cache[current_handler] = ([]);
  }
  module_node node = module_node_cache[current_handler][path] =
    module_node("import::"+path, 0, current_handler);
#endif /* 0 */
  joinnode node = joinnode(({}), current_handler);
#ifdef PIKE_MODULE_RELOC
  // If we have PIKE_MODULE_RELOC enabled, 
  // we might need to map to multiple directories.
  if(path == "/${PIKE_MODULE_PATH}" ||
     has_prefix(path, "/${PIKE_MODULE_PATH}/")) {
    string tmp = path[21..];
    foreach(pike_module_path, string prefix) {
      node->add_path(sizeof(tmp)? combine_path(prefix, tmp) : prefix);
    }
  } else
#endif /* PIKE_MODULE_RELOC */
    node->add_path(path);
  return node;
}

program|object findmodule(string fullname, object|void handler)
{
  program|object o;

  resolv_debug ("findmodule(%O)\n", fullname);
  if(!zero_type(o=fc[fullname]) && o != no_value)
  {
    if (objectp(o) || programp(o) || o != 0) {
      resolv_debug ("findmodule(%O) => found %O (cached)\n", fullname, o);
      return o;
    }
    resolv_debug ("findmodule(%O) => not found (cached)\n", fullname);
    return UNDEFINED;
  }

  if(Stat stat=master_file_stat(fakeroot(fullname)))
  {
    if(stat->isdir)
    {
      resolv_debug ("findmodule(%O) => new dirnode\n", fullname);
      return fc[fullname] = dirnode(fullname, handler);
    }
#if constant (load_module)
    else if (has_suffix (fullname, ".so")) {
      o = fc[fullname] = low_cast_to_object(fullname, "/.", handler);
      DEC_RESOLV_MSG_DEPTH();
      resolv_debug ("findmodule(%O) => got .so object %O\n", fullname, o);
      return o;
    }
#endif
  }

  INC_RESOLV_MSG_DEPTH();

  if(objectp (o = low_cast_to_object(fullname, "/.", handler))) {
    DEC_RESOLV_MSG_DEPTH();
    resolv_debug ("findmodule(%O) => got object %O\n", fullname, o);
    return fc[fullname]=o;
  }

  if (programp (o = low_cast_to_program(fullname, "/.", handler))) {
    DEC_RESOLV_MSG_DEPTH();
    resolv_debug ("findmodule(%O) => got .pike program %O\n", fullname, o);
    return fc[fullname] = o;
  }

  DEC_RESOLV_MSG_DEPTH();
  resolv_debug ("findmodule(%O) => not found\n", fullname);
  return fc[fullname] = 0;
}

#if 0
mixed handle_import(string what, string|void current_file, object|void handler)
{
  string path;
  if(current_file)
  {
    path = combine_path_with_cwd(dirname(current_file), what);
  } else {
    path = combine_path_with_cwd(what);
  }

#if 0
  // If we can't cache the dirnode when we got a handler, then
  // findmodule has to be broken too. Good caching is necessary for
  // module dumping. /mast
  if (handler) {
    resolv_debug ("handle_import(%O, %O, %O) => new dirnode with handler\n",
		  what, current_file, handler);
    return dirnode(path, handler);
  }
#endif

  if(objectp (fc[path])) {
    resolv_debug ("handle_import(%O, %O) => found %O (cached)\n",
		  what, current_file, fc[path]);
    return fc[path];
  }
  resolv_debug ("handle_import(%O, %O) => new dirnode\n", what, current_file);
#ifdef PIKE_MODULE_RELOC
  // If we have PIKE_MODULE_RELOC enabled, 
  // we might need to map to a join node.
  // FIXME: Ought to use the non-relocate_module() fakeroot().
  if(path == "/${PIKE_MODULE_PATH}" ||
     has_prefix(path, "/${PIKE_MODULE_PATH}/")) {
    string tmp = path[21..];
    array(dirnode) dirnodes = ({});
    foreach(pike_module_path, string prefix) {
      string s2 = fakeroot(sizeof(tmp)? combine_path(prefix, tmp) : prefix);
      if(master_file_stat(s2))
	dirnodes += ({ dirnode(s2, handler) });
    }
    resolv_debug("handle_import(%O, %O) => Found %d dirnodes\n",
		 what, current_file, sizeof(dirnodes));
    if (sizeof(dirnodes) > 1) return fc[path] = joinnode(dirnodes);
    if (sizeof(dirnodes)) return fc[path] = dirnodes[0];
    return UNDEFINED;
  }
#endif /* PIKE_MODULE_RELOC */
  return fc[path] = dirnode(fakeroot(path), handler);
}
#endif /* 0 */


multiset no_resolv = (<>);

//! Resolver of symbols not located in the program being compiled.
class CompatResolver
{
  //! Join node of the root modules for this resolver.
  joinnode root_module = joinnode(({instantiate_static_modules(predef::_static_modules)}));

  //! Lookup from handler module to corresponding root_module.
  mapping(object:joinnode) handler_root_modules = ([]);

  //! The pike system module path, not including any set by the user.
  array(string) system_module_path=({});

  //! The complete module search path
  array(string) pike_module_path=({});

  //! The complete include search path
  array(string) pike_include_path=({});

  //! The complete program search path
  array(string) pike_program_path=({});

  mapping(string:string) predefines = master()->initial_predefines;
  string ver;

  //! If we fail to resolv, try the fallback.
  //!
  //! Typical configuration:
  //! @pre{0.6->7.0->7.2-> ... ->master@}
  CompatResolver fallback_resolver;

  //! The CompatResolver is initialized with a value that can be
  //! casted into a "%d.%d" string, e.g. a version object.
  //!
  //! It can also optionally be initialized with a fallback resolver.
  protected void create(mixed version, CompatResolver|void fallback_resolver)
  {
    resolv_debug("CompatResolver(%O, %O)\n", version, fallback_resolver);
    ver=(string)version;
#if 0
    if (version) {
      root_module->symbol = ver + "::";
    }
#endif
    if (CompatResolver::fallback_resolver = fallback_resolver) {
      root_module->fallback_module = fallback_resolver->root_module;
    }
    predefines = initial_predefines;
  }

  //! Add a directory to search for include files.
  //!
  //! This is the same as the command line option @tt{-I@}.
  //!
  //! @note
  //! Note that the added directory will only be searched when using
  //! < > to quote the included file.
  //!
  //! @seealso
  //! @[remove_include_path()]
  //!
  void add_include_path(string tmp)
    {
      tmp=normalize_path(combine_path_with_cwd(tmp));
      pike_include_path-=({tmp});
      pike_include_path=({tmp})+pike_include_path;
    }

  //! Remove a directory to search for include files.
  //!
  //! This function performs the reverse operation of @[add_include_path()].
  //!
  //! @seealso
  //! @[add_include_path()]
  //!
  void remove_include_path(string tmp)
    {
      tmp=normalize_path(combine_path_with_cwd(tmp));
      pike_include_path-=({tmp});
    }

  //! Add a directory to search for modules.
  //!
  //! This is the same as the command line option @tt{-M@}.
  //!
  //! @seealso
  //! @[remove_module_path()]
  //!
  void add_module_path(string tmp)
    {
      tmp=normalize_path(combine_path_with_cwd(tmp));
      root_module->add_path(tmp);
      pike_module_path = ({ tmp }) + (pike_module_path - ({ tmp }));
    }

  //! Remove a directory to search for modules.
  //!
  //! This function performs the reverse operation of @[add_module_path()].
  //!
  //! @seealso
  //! @[add_module_path()]
  //!
  void remove_module_path(string tmp)
    {
      tmp=normalize_path(combine_path_with_cwd(tmp));
      root_module->rem_path(tmp);
      pike_module_path -= ({ tmp });
    }

  //! Add a directory to search for programs.
  //!
  //! This is the same as the command line option @tt{-P@}.
  //!
  //! @seealso
  //! @[remove_program_path()]
  //!
  void add_program_path(string tmp)
    {
      tmp=normalize_path(combine_path_with_cwd(tmp));
      pike_program_path-=({tmp});
      pike_program_path=({tmp})+pike_program_path;
    }

  //! Remove a directory to search for programs.
  //!
  //! This function performs the reverse operation of @[add_program_path()].
  //!
  //! @seealso
  //! @[add_program_path()]
  //!
  void remove_program_path(string tmp)
    {
      tmp=normalize_path(combine_path_with_cwd(tmp));
      pike_program_path-=({tmp});
    }

  //! Add a define (without arguments) which will be implicitly
  //! defined in @[cpp] calls.
  void add_predefine (string name, string value)
  {
    predefines[name] = value;
  }

  //! Remove a define from the set that are implicitly defined in
  //! @[cpp] calls.
  void remove_predefine (string name)
  {
    m_delete (predefines, name);
  }

  //! Returns a mapping with the current predefines.
  mapping get_predefines()
  {
    return predefines;
  }

  //! Instantiate static modules in the same way that dynamic modules
  //! are instantiated.
  protected mapping(string:mixed) instantiate_static_modules(object|mapping static_modules)
  {
    mapping(string:mixed) res = ([]), joins = ([]);
    foreach(indices(static_modules), string name) {
      mixed val = static_modules[name];
      if (!val->_module_value)
	val = val();
      if(mixed tmp=val->_module_value) val=tmp;
      if(!has_value(name, '.'))
	res[name] = val;
      else {
	mapping(string:mixed) level = joins;
	string pfx;
	while(2 == sscanf(name, "%s.%s", pfx, name))
	  level = (level[pfx] || (level[pfx] = ([])));
	level[name] = val;
      }
    }
    joinnode joinify(mapping m)
    {
      foreach(m; string n; mixed v)
	if(mappingp(v))
	  m[n]=joinify(v);
      return joinnode(({m}));
    };
    foreach(joins; string n; mixed v) {
      if(mappingp(v))
	v = joinify(v);
      if(res[n])
	res[n] = joinnode(({res[n], v}));
      else
	res[n] = v;
    }
    return res;
  }

  //!
  mapping get_default_module()
    {
      resolv_debug ("%O->get_default_module()\n", this);

      /* This is an ugly kluge to avoid an infinite recursion.
       * The infinite recursion occurs because this function is
       * called for every file when the compat_major/minor is set.
       * This kluge could cause problems with threads if the
       * compiler was threaded. -Hubbe
       */
      int saved_compat_minor=compat_minor;
      int saved_compat_major=compat_major;
      compat_minor=-1;
      compat_major=-1;

      mixed x;
      mixed err =catch {
	if(resolv("__default") && (x=resolv("__default.all_constants")))
	  x=x();
      };

      compat_major=saved_compat_major;
      compat_minor=saved_compat_minor;
      if(err) throw(err);
      return x;
    }

  // _static_modules -- default for global::
  // current_handler->get_default_module()->_static_modules

  joinnode get_root_module(object|void current_handler)
  {
    if (!root_module) {
      error("get_root_module(%O): No default root module!\n",
	    current_handler);
    }
    if (!current_handler) return root_module;
    joinnode node = handler_root_modules[current_handler];
    if (node) return node;

    // Check for _static_modules.
    mixed static_modules = _static_modules;
    if (current_handler->get_default_module) {
      mapping(string:mixed) default_module =
	current_handler->get_default_module();
      if (default_module) {
	static_modules = default_module["_static_modules"] || ([]);
      }
    }

    node = joinnode(({ instantiate_static_modules(static_modules),
		       // Copy relevant stuff from the root module.
		       @filter(root_module->joined_modules,
			       lambda(mixed x) {
				 return objectp(x) && x->is_resolv_dirnode;
			       }) }),
		    current_handler,
		    root_module->fallback_module);

    // FIXME: Is this needed?
    // Kluge to get _static_modules to work at top level.
    node->cache->_static_modules = static_modules;

    return node;
  }

  //!
  mixed resolv_base(string identifier, string|void current_file,
		    object|void current_handler)
  {
    //      werror("Resolv_base(%O)\n",identifier);
    return get_root_module(current_handler)[identifier];
  }

  //! Same as @[resolv], but throws an error instead of returning
  //! @[UNDEFINED] if the resolv failed.
  mixed resolv_or_error(string identifier, string|void current_file,
			void|object current_handler)
  {
    mixed res = resolv(identifier, current_file, current_handler);
    if(zero_type(res)) error("Could not resolve %s.\n", identifier);
    return res;
  }

  //!
  mixed resolv(string identifier, string|void current_file,
	       object|void current_handler)
  {
    resolv_debug("resolv(%O, %O)\n",identifier, current_file);
    INC_RESOLV_MSG_DEPTH();

    // FIXME: Support having the cache in the handler?
    if( no_resolv[ identifier ] ) {
      DEC_RESOLV_MSG_DEPTH();
      resolv_debug("resolv(%O, %O) => excluded\n",identifier, current_file);
      return UNDEFINED;
    }

    if (current_file && !stringp(current_file)) {
      error("resolv(%O, %O, %O): current_file is not a string!\n",
	    identifier, current_file, current_handler);
    }

    array(string) tmp = identifier/"::";
    mixed ret;
    if (sizeof(tmp) > 1) {
      string scope = tmp[0];
      tmp = tmp[1]/".";
      switch(scope) {
      case "predef":
	ret = all_constants();
	break;
      default:
	if (sscanf(scope, "%d.%d%*s", int major, int minor) == 3) {
	  // Versioned identifier.
	  ret = get_compilation_handler(major, minor);
	  if (ret) {
	    mixed mod = ret->get_default_module();
	    if (!zero_type(mod = mod[tmp[0]])) {
	      ret = mod;
	    } else {
	      ret = ret->resolv(tmp[0]);
	    }
	    tmp = tmp[1..];
	    break;
	  }
	}
	error("resolv(%O, %O, %O): Unsupported scope: %O!\n",
	      identifier, current_file, current_handler, scope);
      }
    } else {
      tmp = identifier/".";
      ret = resolv_base(tmp[0], current_file, current_handler);
      tmp = tmp[1..];
    }
    foreach(tmp,string index) {
      resolv_debug("indexing %O with %O...\n",
		   ret, index);
      resolv_debug("indices(%O): %O\n", ret, indices(ret));
      if (zero_type(ret)) break;
      ret = ret[index];
    }
    DEC_RESOLV_MSG_DEPTH();
#ifdef RESOLV_DEBUG
    if (zero_type (ret))
      resolv_debug("resolv(%O, %O) => not found\n",identifier, current_file);
    else
      resolv_debug("resolv(%O, %O) => found %O\n",identifier, current_file, ret);
#endif /* RESOLV_DEBUG */
    return ret;
  }


  //! This function is called whenever an #include directive is
  //! encountered. It receives the argument for #include and should
  //! return the file name of the file to include
  string handle_include(string f,
			string current_file,
			int local_include)
  {
    if(local_include)
    {
      if(IS_ABSOLUTE_PATH(f)) return combine_path(f);
      return combine_path_with_cwd(dirname(current_file), f);
    }
    else
    {
      foreach(pike_include_path, string path)
      {
	path=combine_path(path,f);
	if(master_file_stat(fakeroot(path)))
	  return path;
      }
      if (fallback_resolver) {
	return fallback_resolver->handle_include(f, current_file,
						 local_include);
      }
    }
    // Failed.
    return 0;
  }

  //!
  string read_include(string f)
  {
    AUTORELOAD_CHECK_FILE(f);
    if (array|object err = catch {
	return master_read_file (f);
      })
      compile_cb_rethrow (err);
  }

  string _sprintf(int t)
  {
    return t=='O' && sprintf("CompatResolver(%O)",ver);
  }
}

inherit CompatResolver;

//!
class Pike06Resolver
{
  inherit CompatResolver;

  //! In Pike 0.6 the current directory was implicitly searched.
  mixed resolv_base(string identifier, string|void current_file,
		    object|void current_handler)
  {
    if (current_file) {
      joinnode node = handle_import(".", current_file, current_handler);
      return node[identifier] ||
	::resolv_base(identifier, current_file, current_handler);
    }
    return ::resolv_base(identifier, current_file, current_handler);
  }
}

//! These are useful if you want to start other Pike processes
//! with the same options as this one was started with.
string _pike_file_name;
string _master_file_name;

// Gets set to 1 if we're in async-mode (script->main() returned <0)
private int(0..1) _async=0;

//! Returns 1 if were in async-mode, e.g. if the main method has
//! returned a negative number.
int(0..1) asyncp() {
  return _async;
}

#if constant(thread_create)
// this must be done in __init if someone inherits the master
protected object _backend_thread=this_thread();

//! The backend_thread() function is useful to determine if you are
//! the backend thread - important when doing async/sync protocols.
//! This method is only available if thread_create is present.
object backend_thread()
{
   return _backend_thread;
}
#endif


mapping(string:string) initial_predefines = ([]);

protected mixed main_resolv(string sym, CompatResolver|void resolver) {
  mixed v = (resolver||this)->resolv(sym);
  if(!v)
    error("Could not resolve %s. "
	  "(Perhaps the installed pike tree has been moved.)\n", sym);
  return v;
};

//! This function is called when all the driver is done with all setup
//! of modules, efuns, tables etc. etc. and is ready to start executing
//! _real_ programs. It receives the arguments not meant for the driver.
void _main(array(string) orig_argv)
{
  array(string) argv=copy_value(orig_argv);
  int debug,trace,run_tool;
  object tmp;
  string postparseaction=0;

  predefines = initial_predefines =
    Builtin._take_over_initial_predefines();
  _pike_file_name = orig_argv[0];
  _master_file_name = __FILE__;
#if constant(thread_create)
  _backend_thread = this_thread();
#endif

#ifndef NOT_INSTALLED
  {
    array parts = (getenv("PIKE_INCLUDE_PATH")||"")/PATH_SEPARATOR-({""});
    int i = sizeof(parts);
    while(i) add_include_path(parts[--i]);

    parts = (getenv("PIKE_PROGRAM_PATH")||"")/PATH_SEPARATOR-({""});
    i = sizeof(parts);
    while(i) add_program_path(parts[--i]);

    parts = (getenv("PIKE_MODULE_PATH")||"")/PATH_SEPARATOR-({""});
    i = sizeof(parts);
    while(i) add_module_path(parts[--i]);
  }
#endif

  // Some configure scripts depends on this format.
  string format_paths() {
    return  ("master.pike...: " + (_master_file_name || __FILE__) + "\n"
	     "Module path...: " + pike_module_path*"\n"
	     "                " + "\n"
	     "Include path..: " + pike_include_path*"\n"
	     "                " + "\n"
	     "Program path..: " + pike_program_path*"\n"
	     "                " + "\n");
  };

  Version cur_compat_ver;

  if(sizeof(argv)>1 && sizeof(argv[1]) && argv[1][0]=='-')
  {
    array q;
    tmp = main_resolv( "Getopt" );

    int NO_ARG = tmp->NO_ARG;
    int MAY_HAVE_ARG = tmp->MAY_HAVE_ARG;
    int HAS_ARG = tmp->HAS_ARG;

    q=tmp->find_all_options(argv,({
      ({"compat_version", HAS_ARG, ({"-V", "--compat"}), 0, 0}),
      ({"version",        NO_ARG,  ({"-v", "--version"}), 0, 0}),
      ({"dumpversion",    NO_ARG,  ({"--dumpversion"}), 0, 0}),
      ({"help",           MAY_HAVE_ARG, ({"-h", "--help"}), 0, 0}),
      ({"features",       NO_ARG,  ({"--features"}), 0, 0}),
      ({"info",           NO_ARG,  ({"--info"}), 0, 0}),
      ({"execute",        HAS_ARG, ({"-e", "--execute"}), 0, 0}),
      ({"debug_without",  HAS_ARG, ({"--debug-without"}), 0, 0}),
      ({"preprocess",     HAS_ARG, ({"-E", "--preprocess"}), 0, 0}),
      ({"modpath",        HAS_ARG, ({"-M", "--module-path"}), 0, 0}),
      ({"ipath",          HAS_ARG, ({"-I", "--include-path"}), 0, 0}),
      ({"ppath",          HAS_ARG, ({"-P", "--program-path"}), 0, 0}),
      ({"showpaths",      MAY_HAVE_ARG, ({"--show-paths"}), 0, 0}),
      ({"warnings",       NO_ARG,  ({"-w", "--warnings"}), 0, 0}),
      ({"nowarnings",     NO_ARG,  ({"-W", "--woff", "--no-warnings"}), 0, 0}),
      ({"autoreload",     NO_ARG,  ({"--autoreload"}), 0, 0}),
      ({"master",         HAS_ARG, ({"-m"}), 0, 0}),
      ({"compiler_trace", NO_ARG,  ({"--compiler-trace"}), 0, 0}),
      ({"assembler_debug",MAY_HAVE_ARG, ({"--assembler-debug"}), 0, 0}),
      ({"optimizer_debug",MAY_HAVE_ARG, ({"--optimizer-debug"}), 0, 0}),
      ({"debug",          MAY_HAVE_ARG, ({"--debug"}), 0, 1}),
      ({"trace",          MAY_HAVE_ARG, ({"--trace"}), 0, 1}),
      ({"ignore",         MAY_HAVE_ARG, ({"-Dqdatplr"}), 0, 1}),
      ({"ignore",         HAS_ARG, ({"-s"}), 0, 0}),
      ({"run_tool",       NO_ARG,  ({"-x"}), 0, 0}),
      ({"show_cpp_warn",  NO_ARG,  ({"--show-all-cpp-warnings","--picky-cpp"}), 0, 0}),
    }), 1);

    /* Parse -M and -I backwards */
    for(int i=sizeof(q)-1; i>=0; i--)
    {
      switch(q[i][0])
      {
	case "compat_version":
	  sscanf(q[i][1],"%d.%d",compat_major,compat_minor);
	  break;

#ifdef PIKE_AUTORELOAD
      case "autoreload":
	autoreload_on++;
	break;
#endif

      case "debug_without":
	// FIXME: Disable loading of dumped modules?
        foreach( q[i][1]/",", string feature )
        {
          switch( feature )
          {
           case "ttf":
             no_resolv[ "_Image_TTF" ] = 1;
             break;
           case "zlib":
             no_resolv[ "Gz" ] = 1;
             break;
           case "unisys":
             no_resolv[ "_Image_GIF" ] = 1;
             no_resolv[ "_Image_TIFF" ] = 1;
             break;
           case "threads":
             // not really 100% correct, but good enough for most things.
             no_resolv[ "Thread" ] = 1;
             add_constant( "thread_create" );
             break;
           default:
             no_resolv[ feature ] = 1;
             break;
          }
        }
        break;

      case "debug":
	debug+=(int)q[i][1];
	break;

#if constant(_compiler_trace)
      case "compiler_trace":
	_compiler_trace(1);
	break;
#endif /* constant(_compiler_trace) */

#if constant(_assembler_debug)
      case "assembler_debug":
	_assembler_debug((int)q[i][1]);
	break;
#endif /* constant(_assembler_debug) */

#if constant(_optimizer_debug)
      case "optimizer_debug":
	_optimizer_debug((int)q[i][1]);
	break;
#endif /* constant(_optimizer_debug) */

      case "trace":
	trace+=(int)q[i][1];
	break;

      case "modpath":
	add_module_path(q[i][1]);
	break;

      case "ipath":
	add_include_path(q[i][1]);
	break;

      case "ppath":
	add_program_path(q[i][1]);
	break;

      case "warnings":
	want_warnings++;
	break;

      case "nowarnings":
	want_warnings--;
	break;

      case "master":
	_master_file_name = q[i][1];
	break;

      case "run_tool":
	run_tool = 1;
	break;

      case "show_cpp_warn":
	show_if_constant_errors = 1;
	break;
      }
    }

    cur_compat_ver = Version (compat_major, compat_minor);
    if (compat_major != -1) {
      object compat_master = get_compat_master (compat_major, compat_minor);

      if (cur_compat_ver <= Version (7, 6)) {
	mapping(string:array(string)) compat_env = ([]);
	foreach (Builtin._getenv(); string var; string val) {
#ifdef __NT__
	  compat_env[lower_case (var)] = ({var, val});
#else
	  compat_env[var] = ({var, val});
#endif
	}
	compat_master->environment = compat_env;
      }
    }

    foreach(q, array opts)
    {
      switch(opts[0])
      {
      case "dumpversion":
	write("%d.%d.%d\n", __REAL_MAJOR__, __REAL_MINOR__, __REAL_BUILD__);
        exit(0);

      case "version":
        exit(0, string_to_utf8(version() + " Copyright  1994-2012 Linkping University\n"
            "Pike comes with ABSOLUTELY NO WARRANTY; This is free software and you are\n"
            "welcome to redistribute it under certain conditions; read the files\n"
            "COPYING and COPYRIGHT in the Pike distribution for more details.\n"));

      case "help":
	exit( 0, main_resolv("Tools.MasterHelp")->do_help(opts[1]) );

      case "features":
	postparseaction="features";
	break;

      case "info":
	postparseaction="info";
	break;

      case "showpaths":
        if( stringp(opts[1]) )
        {
          switch(opts[1])
          {
          case "master":
            write( (_master_file_name || __FILE__)+"\n" );
            break;

          case "module":
            write( (pike_module_path * ":")+"\n" );
            break;

          case "include":
            write( (pike_include_path * ":")+"\n" );
            break;

          case "program":
            write( (pike_program_path * ":")+"\n" );
            break;

          default:
            exit(1, "Unknown path type %s\n", opts[1]);
          }
          exit(0);
        }

	exit(0, format_paths());

      case "execute":
#ifdef __AUTO_BIGNUM__
	main_resolv( "Gmp.bignum" );
#endif /* __AUTO_BIGNUM__ */

	random_seed((time() ^ (getpid()<<8)));
	argv = tmp->get_args(argv,1);

	program prog;
	mixed compile_err = catch {;
	  if(cur_compat_ver <= Version(7,4))
	    prog = compile_string(
	      "mixed create(int argc, array(string) argv,array(string) env){"+
	      opts[1]+";}");
	  else if (intp (opts[1]))
	    prog = compile_string ("mixed run() {}");
	  else {
	    string code = opts[1];
	    while(sscanf(code, "%sCHAR(%1s)%s", code, string c, string rest)==3)
	      code += c[0] + rest;
	    if (cur_compat_ver <= Version (7, 6))
	      prog = compile_string(
		"#define NOT(X) !(X)\n"
		"mixed run(int argc, array(string) argv,"
		"mapping(string:string) env){"+
		code+";}");
	    else
	      prog = compile_string(
		"#define NOT(X) !(X)\n"
		"mixed run(int argc, array(string) argv){" + code + ";}");
	  }
	  };

	if (compile_err) {
	  if (compile_err->is_cpp_or_compilation_error) {
	    // Don't clutter the output with the backtrace for
	    // compilation errors.
	    exit (20, call_describe_error (compile_err));
	  }
	  else throw (compile_err);
	}

#if constant(_debug)
	if(debug) _debug(debug);
#endif
	if(trace) trace = predef::trace(trace);
	mixed ret;
	mixed err = catch {
	    // One reason for this catch is to get a new call to
	    // eval_instruction in interpret.c so that the debug and
	    // trace levels set above take effect in the bytecode
	    // evaluator.
	    if(cur_compat_ver <= Version(7,4))
	      prog (sizeof(argv),argv,getenv());
	    else if (cur_compat_ver <= Version (7, 6))
	      ret = prog()->run(sizeof(argv),argv,getenv());
	    else
	      ret = prog()->run(sizeof(argv),argv);
	  };
	predef::trace(trace);
	if (err) {
	  handle_error (err);
	  ret = 10;
	}
	if(stringp(ret)) {
	  write(ret);
	  if(ret[-1]!='\n') write("\n");
	}
	if(!intp(ret) || ret<0) ret=0;
	exit(ret);

      case "preprocess":
#ifdef __AUTO_BIGNUM__
	main_resolv( "Gmp.bignum" );
#endif /* __AUTO_BIGNUM__ */
	write(cpp(master_read_file(opts[1]),opts[1]));
	exit(0);
      }
    }

    argv = tmp->get_args(argv,1);
  }
  else
    cur_compat_ver = Version (compat_major, compat_minor);

  switch (postparseaction)
  {
     case "features":
       write( main_resolv( "Tools.Install.features" )()*"\n"+"\n" );
       exit(0);

     case "info":
       write("Software......Pike\n"
	     "Version......."+version()+"\n"
	     "WWW...........http://pike.ida.liu.se/\n"
	     "\n"
	     "pike binary..."+_pike_file_name+"\n"+
	      format_paths() + "\n"
	     "Features......"+
	     main_resolv( "Tools.Install.features" )()*"\n              "+
	     "\n");
	exit(0);
  }

#ifdef __AUTO_BIGNUM__
  main_resolv( "Gmp.bignum" );
#endif /* __AUTO_BIGNUM__ */

  random_seed(time() ^ (getpid()<<8));

  if(sizeof(argv)==1)
  {
    if(run_tool) {
      werror("Pike -x specificed without tool name.\n"
	     "Available tools:\n");
      mapping t = ([]);
      int i;
      object ts = main_resolv("Tools.Standalone",
			      get_compilation_handler(compat_major,
						      compat_minor));
      foreach (indices(ts), string s) {
	mixed val = ts[s];
	if (programp (val)) {
	  object o = val();
	  if(!o->main || !o->description) continue;
	  t[s] = o->description;
	  i = max(i, sizeof(s));
	}
      }
      foreach(sort(indices(t)), string s)
	werror(" %-"+i+"s %s\n", s, t[s]);
      exit(1);
    }
    main_resolv("Tools.Hilfe",
		get_compilation_handler(compat_major,
					compat_minor))->StdinHilfe();
    exit(0);
  }
  else
    argv=argv[1..];

  program prog;

  if(run_tool) {
    mixed err = catch {
      prog = main_resolv("Tools.Standalone." + argv[0],
			 get_compilation_handler(compat_major, compat_minor));
    };

    if (err)
      exit(1, "Pike: Failed to load tool %s:\n"
           "%s\n", argv[0],
           stringp(err[0])?err[0]:call_describe_backtrace(err));

    argv[0] = search(programs, prog) || argv[0];
  } else {
    argv[0]=combine_path_with_cwd(argv[0]);

    mixed err = catch {
      prog=(program)argv[0];
    };

    if (err) {
      string fn = argv[0];
      if( !file_stat(fn) )
      {
        if( file_stat(fn+".pike") )
          fn += ".pike";
        else
          exit(1, "Could not find file %O.\n", fn);
      }
      if( !file_stat(fn)->isreg )
	exit(1, "File %O is not a regular file.\n", fn);
      if( !master_read_file(fn) )
	exit(1, "File %O is not readable. %s.\n",
	     fn, strerror(errno()));
      if (objectp (err) && err->is_cpp_or_compilation_error)
	exit(1, "Pike: Failed to compile script.\n");
      else
	exit(1, "Pike: Failed to compile script:\n"
	     "%s", call_describe_backtrace(err));
    }

    // Don't list the program with its real path in the programs
    // mapping, so that reverse lookups (typically by the codec)
    // always find the canonical "/main" instead.
    programs[argv[0]] = no_value;
  }

  programs["/main"] = prog;

  // FIXME: Can this occur at all?
  if(!prog)
    error("Pike: Couldn't find script to execute\n(%O)\n", argv[0]);

#if constant(_debug)
  if(debug) _debug(debug);
#endif
  if(trace) trace = predef::trace(trace);
  mixed ret;
  mixed err = catch {
      // The main reason for this catch is actually to get a new call
      // to eval_instruction in interpret.c so that the debug and
      // trace levels set above take effect in the bytecode evaluator.
      // Note that it won't work if there's another catch surrounding
      // this one.
      object script;
      if(cur_compat_ver <= Version(7,4)) {
	script=prog();
      }
      else {
	script=prog(argv);
      }
      if(!script->main)
	error("Error: %s has no main().\n", argv[0]);
      if (cur_compat_ver <= Version (7, 6))
	ret=script->main(sizeof(argv),argv,getenv());
      else
	ret=script->main(sizeof(argv),argv);
    };
  // Disable tracing.
  trace = predef::trace(trace);
  if (err) {
    handle_error (err);
    ret = 10;
  }
  if(!intp(ret))
    exit(10, "Error: Non-integer value %O returned from main.\n", ret);

  if(ret >=0) exit([int]ret);
  _async=1;

  // Reenable tracing.
  trace = predef::trace(trace);
  while(1)
  {
    mixed err=catch 
    {
      while(1)
	Builtin.__backend(3600.0);
    };
    master()->handle_error(err);
  }
}

#if constant(thread_local)
object inhibit_compile_errors = thread_local();

void set_inhibit_compile_errors(mixed f)
{
  inhibit_compile_errors->set(f);
}

mixed get_inhibit_compile_errors()
{
  return inhibit_compile_errors->get();
}
#else /* !constant(thread_local) */
mixed inhibit_compile_errors;

void set_inhibit_compile_errors(mixed f)
{
  inhibit_compile_errors=f;
}

mixed get_inhibit_compile_errors()
{
  return inhibit_compile_errors;
}
#endif /* constant(thread_local) */

protected private function(string:string) _trim_file_name_cb=0;
string trim_file_name(string s)
{
#ifdef PIKE_MODULE_RELOC
  s = relocate_module(s);
#endif
  if(getenv("LONG_PIKE_ERRORS")) return s;
  if(getenv("SHORT_PIKE_ERRORS")) return BASENAME(s);

  if (_trim_file_name_cb) return _trim_file_name_cb(s);

  /* getcwd() can fail, but since this is called from handle_error(),
   * we don't want to fail, so we don't care about that.
   */
  catch {
    string cwd=getcwd();
    if (sizeof(cwd) && (cwd[-1] != '/')) {
      cwd += "/";
    }
    if(has_prefix (s, cwd)) return s[sizeof(cwd)..];
  };
  return s;
}

function(string:string) set_trim_file_name_callback(function(string:string) s)
{
   function(string:string) f=_trim_file_name_cb;
   _trim_file_name_cb=s;
   return f;
}


//! This function is called whenever a compile error occurs. @[line]
//! is zero for errors that aren't associated with any specific line.
//! @[err] is not newline terminated.
void compile_error(string file,int line,string err)
{
  mixed val;
  if(! (val = get_inhibit_compile_errors() ))
  {
    werror( "%s:%s:%s\n",trim_file_name(file),
	    line?(string)line:"-",err );
  }
  else if(objectp(val) ||
	  programp(val) ||
	  functionp(val))
  {
    if (objectp(val) && val->compile_error) {
      val->compile_error(file, line, err);
    } else if (callablep(val)) {
      val(file, line, err);
    }
  }
}


//! This function is called whenever a compile warning occurs. @[line]
//! is zero for warnings that aren't associated with any specific
//! line. @[err] is not newline terminated.
void compile_warning(string file,int line,string err)
{
  mixed val;

  if(!(val = get_inhibit_compile_errors() ))
  {
    if(want_warnings)
      werror( "%s:%s: Warning: %s\n",trim_file_name(file),
	      line?(string)line:"-",err );
  }
  else if (objectp(val) && val->compile_warning) {
    ([function(string,int,string:void)]([object]val)
     ->compile_warning)(file, line, err);
  }
}


//! This function is called when an exception is caught during
//! compilation. Its message is also reported to @[compile_error] if
//! this function returns zero.
int compile_exception (array|object trace)
{
  if (objectp (trace) && ([object]trace)->is_cpp_or_compilation_error)
    // Errors thrown by cpp(), compile() or a compile callback should
    // be reported as a normal compile error, so let the caller do
    // just that.
    return 0;
  if (mixed val = get_inhibit_compile_errors()) {
    if (objectp(val) && ([object]val)->compile_exception)
      return ([function(object:int)]([object]val)
	      ->compile_exception)([object]trace);
  }
  else {
    handle_error (trace);
    return 1;
  }
  return 0;
}


//! Called for every runtime warning. The first argument identifies
//! where the warning comes from, the second identifies the specific
//! message, and the rest depends on that. See code below for currently
//! implemented warnings.
void runtime_warning (string where, string what, mixed... args)
{
  if (want_warnings)
    switch (where + "." + what) {
      case "gc.bad_cycle":
	// args[0] is an array containing the objects in the cycle
	// which aren't destructed and have destroy() functions.
#if 0
	// Ignore this warning for now since we do not yet have a weak
	// modifier, so it can't be avoided in a reasonable way.
	werror ("GC warning: Garbing cycle where destroy() will be called "
		"in arbitrary order:\n%{            %s\n%}",
		sprintf("%O", args[0][*]));
#endif
	break;

      default:
	werror ("%s warning: %s %O\n", capitalize (where), what, args);
    }
}


protected object Charset;

//! This function is called by cpp() when it wants to do
//! character code conversion.
string decode_charset(string data, string charset)
{
  if (!Charset) {
    object mod = [object]resolv("Locale");

    Charset = [object](mod && mod["Charset"]);
    if (!Charset)
      compile_cb_error("Cannot handle charset - no Locale.Charset module found.");
  }

  if (mixed err = catch {
      object decoder = ([function(string:object)]Charset.decoder)(charset);
      return ([function(void:string)]([function(string:object)]decoder->
				      feed)(data)->drain)();
    })
    compile_cb_rethrow (err);
}


class Describer
{
  int clipped=0;
  int canclip=0;
  mapping(mixed:int|string) ident = ([]);
  int identcount = 0;

  void identify_parts (mixed stuff)
  {
    // Use an array as stack here instead of recursing directly; we
    // might be pressed for stack space if the backtrace being
    // described is a stack overflow.
    array identify_stack = ({stuff});
    while (sizeof (identify_stack)) {
      stuff = identify_stack[-1];
      identify_stack = identify_stack[..<1];
      if (!intp(ident[stuff])) continue;	// Already identified.
      if (objectp (stuff) || functionp (stuff) || programp (stuff))
	ident[stuff]++;
      else if (arrayp (stuff)) {
	if (!ident[stuff]++)
	  identify_stack += stuff;
      }
      else if (multisetp (stuff)) {
	if (!ident[stuff]++)
	  identify_stack += indices([multiset]stuff);
      }
      else if (mappingp (stuff)) {
	if (!ident[stuff]++)
	  identify_stack += indices([mapping]stuff) + values([mapping]stuff);
      }
    }
  }

  string describe_string (string m, int maxlen)
  {
    canclip++;
    if(sizeof(m) < maxlen)
    {
      string t = sprintf("%q", m);
      if (sizeof(t) < (maxlen + 2))
	return t;
      t = 0;
    }
    clipped++;
    if(maxlen>10)
      return sprintf("%q+[%d]",m[..maxlen-5],sizeof(m)-(maxlen-5));

    return "string["+sizeof(m)+"]";
  }

  string describe_array (array m, int maxlen)
  {
    if(!sizeof(m)) return "({})";
    else {
      if(maxlen<5)
      {
	clipped++;
	return "array["+sizeof(m)+"]";
      }
      else {
	canclip++;
	return "({" + describe_comma_list(m,maxlen-2) +"})";
      }
    }
  }

  string describe_mapping (mapping m, int maxlen)
  {
    if(!sizeof(m)) return "([])";
    else return "mapping["+sizeof(m)+"]";
  }

  string describe_multiset (multiset m, int maxlen)
  {
    if(!sizeof(m)) return "(<>)";
    else return "multiset["+sizeof(m)+"]";
  }

  string describe (mixed m, int maxlen)
  {
    catch {
      if (stringp (ident[m])) return [string]ident[m];
      else if (intp (ident[m]) && ident[m] > 1)
	ident[m] = "@" + identcount++;
    };

    string res;
    if (catch (res=sprintf("%t",m)))
      res = "object";		// Object with a broken _sprintf(), probably.
    switch(res)
    {
    case "int":
      if (!m && zero_type (m) == 1)
	return "UNDEFINED";
    case "float":
      return (string)m;
    case "string":
      return describe_string ([string]m, maxlen);
    case "array":
      res = describe_array ([array]m, maxlen);
      break;
    case "mapping":
      res = describe_mapping ([mapping]m, maxlen);
      break;
    case "multiset":
      res = describe_multiset ([multiset]m, maxlen);
      break;
    case "function":
      if (string tmp=describe_function([function]m)) res = tmp;
      break;
    case "program":
      if(string tmp=describe_program([program]m)) res = tmp;
      break;
    default:
      /* object or type. */
      if (catch {
	if(string tmp=sprintf("%O", m)) res = tmp;
      }) {
	// Extra paranoia case.
	res = sprintf("Instance of %O", _typeof(m));
      }
      break;
    }
    if (stringp(ident[m]))
      return ident[m] + "=" + res;
    return res;
  }

  string describe_comma_list(array x, int maxlen)
  {
    string ret="";

    if(!sizeof(x)) return "";
    if(maxlen<0) return ",,,"+sizeof(x);

    int clip=min(maxlen/2,sizeof(x));
    int len=maxlen;
    int done=0;

    while(1)
    {
      array(string) z=allocate(clip);
      array(int) isclipped=allocate(clip);
      array(int) clippable=allocate(clip);
      for(int e=0;e<clip;e++)
      {
	clipped=0;
	canclip=0;
	z[e]=describe(x[e],len);
	isclipped[e]=clipped;
	clippable[e]=canclip;
      }

      while(1)
      {
	string ret = z[..clip-1]*",";
	if(done || sizeof(ret)<=maxlen+1)
	{
	  int tmp=sizeof(x)-clip-1;
	  clipped=`+(0,@isclipped);
	  if(tmp>=0)
	  {
	    clipped++;
	    ret+=",,,"+tmp;
	  }
	  canclip++;
	  return ret;
	}

	int last_newlen=len;
	int newlen;
	int clipsuggest;
	while(1)
	{
	  int smallsize=0;
	  int num_large=0;
	  clipsuggest=0;

	  for(int e=0;e<clip;e++)
	  {
	    if((sizeof(z[e])>=last_newlen || isclipped[e]) && clippable[e])
	      num_large++;
	    else
	      smallsize+=sizeof(z[e]);

	    if(num_large * 15 + smallsize < maxlen) clipsuggest=e+1;
	  }

	  newlen=num_large ? (maxlen-smallsize)/num_large : 0;

	  if(newlen<8 || newlen >= last_newlen) break;
	  last_newlen=newlen;
	}

	if(newlen < 8 && clip)
	{
	  clip-= (clip/4) || 1;
	  if(clip > clipsuggest) clip=clipsuggest;
	}else{
	  len=newlen;
	  done++;
	  break;
	}
      }
    }

    return ret;
  }
}


string program_path_to_name ( string path,
			      void|string module_prefix,
			      void|string module_suffix,
			      void|string object_suffix )
//! Converts a module path on the form @expr{"Foo.pmod/Bar.pmod"@} or
//! @expr{"/path/to/pike/lib/modules/Foo.pmod/Bar.pmod"@} to a module
//! identifier on the form @expr{"Foo.Bar"@}.
//!
//! If @[module_prefix] or @[module_suffix] are given, they are
//! prepended and appended, respectively, to the returned string if
//! it's a module file (i.e. ends with @expr{".pmod"@} or
//! @expr{".so"@}). If @[object_suffix] is given, it's appended to the
//! returned string if it's an object file (i.e. ends with
//! @expr{".pike"@}).
{
  if (path == "/master") return "master" + (object_suffix || "");

  array(string) sort_paths_by_length(array(string) paths)
  {
    sort(map(paths, sizeof), paths);
    return reverse(paths);
  };

  string prefix = "";

  array(Version) versions = reverse(sort(indices(compat_handler_cache)));
 find_prefix:
  foreach(versions, Version version) {
    CompatResolver r = compat_handler_cache[version];
    if (!r) continue;	// Protection against the gc...

    foreach(sort_paths_by_length(map(r->pike_module_path - ({""}),
				     lambda(string s) {
				       if (s[-1] == '/') return s;
				       return s+"/";
				     })),
	    string path_prefix) {
      if (has_prefix(path, path_prefix)) {
	path = path[sizeof(path_prefix)..];
	if (version != currentversion) {
	  prefix = ((string)version) + "::";
	}
	break find_prefix;
      }
    }
  }

#if 0
  // This seems broken. Why should the current directory or the
  // setting of SHORT_PIKE_ERRORS etc affect the module identifiers?
  // /mast
  path = trim_file_name(path);
#endif

  string modname = replace(path, ".pmod/", ".");
  if(search(modname, "/")<0) path=modname;

  path = prefix + path;

  if (has_suffix(path, ".module.pmod")) {
    return (module_prefix || "") + path[..<12] + (module_suffix || "");
  }
  if (has_suffix(path, ".pmod")) {
    return (module_prefix || "") + path[..<5] + (module_suffix || "");
  }
  if (has_suffix(path, ".so")) {
    return (module_prefix || "") + path[..<3] + (module_suffix || "");
  }
  if (has_suffix(path, ".pike")) {
    return path[..<5] + (object_suffix || "");
  }
  return path + (object_suffix || "");
}


//! Describe the path to the module @[mod].
//!
//! @param mod
//!   If @[mod] is a program, attempt to describe the path
//!   to a clone of @[mod].
//!
//! @param ret_obj
//!   If an instance of @[mod] is found, it will be returned
//!   by changing element @expr{0@} of @[ret_obj].
//!
//! @returns
//!   The a description of the path.
//!
//! @note
//!   The returned description will end with a proper indexing method
//!   currently either @expr{"."@} or @expr{"->"@}.
string describe_module(object|program mod, array(object)|void ret_obj)
{
  // Note: mod might be a bignum object; objectp won't work right for
  // our purposes. object_program returns zero for non-objects, so we
  // use it instead.
  program parent_fun = object_program(mod);
  if (parent_fun) {
    if (ret_obj) ret_obj[0] = mod;
  } else if (programp (mod)) {
    parent_fun = mod;
    // When running with debug we might be called before __INIT, so
    // we have to check if objects exists before we use it.
    if (objects && objectp (mod = objects[parent_fun]) && ret_obj)
      ret_obj[0] = mod;
  }
  else
    return "";			// efun

  if (mod) {
    catch {
      string res = sprintf("%O", mod);
      if (res != "object" && res != "")
	return (objectp (objects[parent_fun]) && programs["/master"] != parent_fun?
		res+".":res+"->");
    };
    string res = search(all_constants(), mod);
    if (res) return res;
  }
  if (!object_program(parent_fun)) {
    // We might be a top-level entity.
    if (string path = programs_reverse_lookup (parent_fun))
      return program_path_to_name(path, "", ".", "()->");
  }
  // Begin by describing our parent.
  array(object) parent_obj = ({ 0 });
  string res = describe_module(function_object(parent_fun)||
			       function_program(parent_fun)||
			       object_program(parent_fun),
			       parent_obj);
  // werror("So far: %O parent_obj:%O\n", res, parent_obj);
  object|program parent =
    object_program (parent_obj[0]) ? parent_obj[0] : object_program(parent_fun);
  if (mod && (object_program (parent) || parent)) {
    // Object identified.
    catch {
      // Check if we're an object in parent.
      int i = search(values(parent), mod);
      if (i >= 0) {
	return res + [string]indices(parent)[i] + ".";
      }
    };
  }

  // We're cloned from something in parent.
  if (string fun_name = function_name(parent_fun)) {
    return res + fun_name + "()->";
  }

  // No such luck.
  // Try identifying a clone of ourselves.
  if (!mod && (object_program (parent) || parent)) {
    catch {
      // Check if there's a clone of parent_fun in parent_obj.
      int i;
      array(mixed) val = values(parent);
      array(string) ind = [array(string)]indices(parent);
      for (i=0; i < sizeof(val); i++) {
	if (object_program(val[i]) && object_program(val[i]) == parent_fun) {
	  return res + ind[i] + ".";
	}
      }
    };
  }

  // We're really out of luck here...
  return res + (describe_program(parent_fun)||"unknown_program") + "()->";
}

//!
string describe_object(object o)
{
  string s;
  if(zero_type (o)) return 0;	// Destructed.

  // Handled by the search of all_constants() below.
  // if (o == _static_modules) return "_static_modules";

  program|function(mixed...:void|object) parent_fun = object_program(o);

  /* Constant object? */
  catch {
    object|program parent_obj =
      (function_object(parent_fun) || function_program(parent_fun));

    if (objectp (parent_obj) || parent_obj) {
      /* Check if we have a constant object. */
      object tmp = objects[parent_obj];
      if (objectp (tmp)) parent_obj = tmp;

      /* Try finding ourselves in parent_obj. */
      int i = search(values(parent_obj), o);
      if (i >= 0) {
	s = [string]indices(parent_obj)[i];
	return describe_module(parent_obj) + s;
      }
    }
  };
  if ((s = search(all_constants(), o))) return s;
  // When running with RESOLV_DEBUG this function may
  // get called before objects has been initialized.
  if(objects && objectp (objects[parent_fun]))
    if ((s = programs_reverse_lookup (parent_fun)) &&
	(s=program_path_to_name(s, "", "", "()")))
      return s;
  /* Try identifying the program. */
  if(( s=describe_program(parent_fun) ))
    return s+"()";

  return 0;
}

//!
string describe_program(program|function p)
{
  string s;
  if(!p) return 0;

  if (p == object_program (_static_modules))
    return "object_program(_static_modules)";

  if(programp(p) &&
     (s = programs_reverse_lookup ([program] p)) &&
     (s=program_path_to_name(s, "object_program(", ")", "")))
    return s;

  if(object|program tmp=(function_object(p) || function_program(p))) {
    if(s = function_name(p))
    {
      return describe_module(tmp) + s;
    }
  }

  if(s=Builtin.program_defined(p))
    return BASENAME(s);

  return search(all_constants(), p);
}

//!
string describe_function (function f)
{
  if (!f) return 0;

  string name;

  if (name = search(all_constants(), f)) return name;

  if(string s = programs_reverse_lookup (f))
  {
    if(has_suffix(s, ".pmod"))
      name = BASENAME(s[..<5]);
    else
      name = trim_file_name(s);
  }
  else 
    if (catch (name = function_name (f))) name = "function";

  object o = function_object([function(mixed...:void|mixed)]f);
  if(object_program (o)) { // Check if it's an object in a way that
			   // (hopefully) doesn't call any functions
			   // in it (neither `== nor `!).
    string s;
    if (!catch (s = sprintf("%O",o)) && s != "object")
      return s+"->"+name;
  }
  return name;
}

/* It is possible that this should be a real efun,
 * it is currently used by handle_error to convert a backtrace to a
 * readable message.
 */

//! @appears describe_backtrace
//! Return a readable message that describes where the backtrace
//! @[trace] was made (by @[backtrace]).
//!
//! It may also be an error object or array (typically caught by a
//! @[catch]), in which case the error message also is included in the
//! description.
//!
//! @seealso
//! @[backtrace()], @[describe_error()], @[catch()], @[throw()]
//!
string describe_backtrace(mixed trace, void|int linewidth)
{
  int e;
  string ret;
  int backtrace_len=((int)getenv("PIKE_BACKTRACE_LEN")) || bt_max_string_len;

  if(!linewidth)
  {
    linewidth=99999;
    catch 
    {
      linewidth=[int]Files()->_stdin->tcgetattr()->columns;
    };
    if(linewidth<10) linewidth=99999;
  }

  // Note: Partial code duplication in describe_error and get_backtrace.

  if (objectp(trace) && ([object]trace)->is_generic_error) {
    object err_obj = [object] trace;
    if (mixed err = catch {

	if (functionp (err_obj->message))
	  ret = err_obj->message();
	else if (zero_type (ret = err_obj->error_message))
	  // For compatibility with error objects trying to behave
	  // like arrays.
	  ret = err_obj[0];
	if (!ret)
	  ret = "";
	else if (!stringp (ret))
	  ret = sprintf ("<Message in %O is %t, expected string>\n",
			 err_obj, ret);

	if (functionp (err_obj->backtrace))
	  trace = err_obj->backtrace();
	else if (zero_type (trace = err_obj->error_backtrace))
	  // For compatibility with error objects trying to behave
	  // like arrays.
	  trace = err_obj[1];
	if (!trace)
	  return ret + "<No backtrace>\n";
	else if (!arrayp (trace))
	  return sprintf ("%s<Backtrace in %O is %t, expected array>\n",
			  ret, err_obj, trace);

      })
      return sprintf ("<Failed to index backtrace object %O: %s>\n",
		      err_obj, trim_all_whites (call_describe_error (err)));
  }

  else if (arrayp(trace)) {
    if (sizeof([array]trace)==2 && stringp(ret = ([array]trace)[0])) {
      trace = ([array] trace)[1];
      if(!trace)
	return ret + "<No backtrace>\n";
      else if (!arrayp (trace))
	return sprintf ("%s<Backtrace in error array is %t, expected array>\n",
			ret, trace);
    }
    else
      ret = "";
  }

  else {
#if constant(_gdb_breakpoint)
    _gdb_breakpoint();
#endif
    return sprintf ("<Invalid backtrace/error container: %O>\n"
		    "%s\n", trace, call_describe_backtrace(backtrace()));
  }

  {
    Describer desc = Describer();
    array trace = [array]trace;

    int end = 0;
    if( (sizeof(trace)>1) &&
	arrayp(trace[0]) &&
	(sizeof([array]trace[0]) > 2) &&
	(([array]trace[0])[2] == _main))
      end = 1;

    if( end==1 && (sizeof(trace)>2) &&
	arrayp(trace[1]) && (sizeof([array]trace[1])>2) &&
	(([array]trace[1])[2] == main_resolv) &&
	arrayp(trace[-1]) && (sizeof([array]trace[-1])>2) &&
	(([array]trace[-1]))[2] == compile_string )
      end = sizeof(trace);

    mapping(string:int) prev_pos = ([]);
    array(string) frames = ({});
    int loop_start = 0, loop_next, loops;

    for(e = sizeof(trace)-1; e>=end; e--)
    {
      mixed tmp;
      string row;
      if (array err=[array]catch {
	tmp = trace[e];
	if(stringp(tmp))
	{
	  row=[string]tmp;
	}
	else if(arrayp(tmp))
	{
	  if(sprintf("%t",tmp)=="object") {
	    // tmp is backtrace_frame
	    desc->identify_parts( tmp->args );
	  }
	  else
	    desc->identify_parts( tmp );
	  array tmp = [array]tmp;
	  string pos;
	  if(sizeof(tmp)>=2 && stringp(tmp[0])) {
	    if (intp(tmp[1])) {
	      pos=trim_file_name([string]tmp[0])+":"+(string)tmp[1];
	    } else {
	      pos = sprintf("%s:Bad line %t",
			    trim_file_name([string]tmp[0]), tmp[1]);
	    }
	  }else{
	    string desc="Unknown program";
	    if(sizeof(tmp)>=3 && functionp(tmp[2]))
	    {
	      catch 
              {
		if(mixed tmp=function_object([function(mixed...:
						       void|mixed)]tmp[2]))
		  if(tmp=object_program(tmp))
		    if(tmp=describe_program([program]tmp))
		      desc=[string]tmp;
	      };
	    }
	    pos=desc;
	  }

	  string data;

	  if(sizeof(tmp)>=3)
	  {
	    if(functionp(tmp[2])) {
	      data = describe_function ([function]tmp[2]);
	    }
	    else if (stringp(tmp[2])) {
	      data = [string]tmp[2];
	    } else
	      data ="unknown function";

	    data+="("+
	      desc->describe_comma_list(tmp[3..], backtrace_len)+
	    ")";

	    if(sizeof(pos)+sizeof(data) < linewidth-4)
	    {
	      row=sprintf("%s: %s",pos,data);
	    }else{
	      row=sprintf("%s:\n%s",pos,sprintf("    %*-/s",linewidth-6,data));
	    }
	  } else {
	    row = pos;
	  }
	}
	else
	{
	  if (tmp) {
	    if (catch (row = sprintf("%O", tmp)))
	      row = describe_program(object_program(tmp)) + " with broken _sprintf()";
	  } else {
	    row = "Destructed object";
	  }
	}
      }) {
  	row = sprintf("Error indexing backtrace line %d: %s (%O)!", e, err[0], err[1]);
      }

      int dup_frame;
      if (!zero_type(dup_frame = prev_pos[row])) {
	dup_frame -= sizeof(frames);
	if (!loop_start) {
	  loop_start = dup_frame;
	  loop_next = dup_frame + 1;
	  loops = 0;
	  continue;
	} else {
	  int new_loop = 0;
	  if (!loop_next) loop_next = loop_start, new_loop = 1;
	  if (dup_frame == loop_next++) {
	    loops += new_loop;
	    continue;
	  }
	}
      }
      prev_pos[row] = sizeof(frames);

      if (loop_start) {
	array(string) tail;
	if (!loop_next) tail = ({}), loops++;
	else tail = frames[loop_start + sizeof(frames) ..
			  loop_next - 1 + sizeof(frames)];
	if (loops)
	  frames += ({sprintf ("... last %d frames above repeated %d times ...\n",
			       -loop_start, loops)});
	frames += tail;
	prev_pos = ([]);
	loop_start = 0;
      }

      frames += ({row + "\n"});
    }

    if (loop_start) {
      // Want tail to contain a full loop rather than being empty; it
      // looks odd when the repeat message ends the backtrace.
      array(string) tail = frames[loop_start + sizeof(frames) ..
				  loop_next - 1 + sizeof(frames)];
      if (loops)
	frames += ({sprintf("... last %d frames above repeated %d times ...\n",
			     -loop_start, loops)});
      frames += tail;
    }

    ret += frames * "";
  }

  return ret;
}

//! @appears describe_error
//!
//! Return the error message from an error object or array (typically
//! caught by a @[catch]). The type of the error is checked, hence
//! @[err] is declared as @expr{mixed@} and not @expr{object|array@}.
//!
//! If an error message couldn't be obtained, a fallback message
//! describing the failure is returned. No errors due to incorrectness
//! in @[err] are thrown.
//!
//! @seealso
//! @[describe_backtrace()], @[get_backtrace]
//!
string describe_error (mixed /* object|array */ err)
{
  mixed msg;

  // Note: Partial code duplication in describe_backtrace and get_backtrace.

  if (objectp(err) && ([object]err)->is_generic_error) {
    object err_obj = [object] err;
    if (mixed err = catch {

	if (functionp (err_obj->message))
	  msg = err_obj->message();
	else if (zero_type (msg = err_obj->error_message))
	  // For compatibility with error objects trying to behave
	  // like arrays.
	  msg = err_obj[0];

	if (stringp (msg))
	  return msg;
	else if (!msg)
	  return "<No error message>\n";
	else
	  return sprintf ("<Message in %O is %t, expected string>\n",
			  err_obj, msg);

      })
      return sprintf ("<Failed to index error object %O: %s>\n",
		      err_obj, trim_all_whites (call_describe_error (err)));
  }

  else if (arrayp(err) && sizeof([array]err)==2 &&
	   (!(msg = ([array]err)[0]) || stringp (msg)))
    return [string] msg || "<No error message>\n";

  else
    return sprintf ("<Invalid error container: %O>\n", err);
}

//! @appears get_backtrace
//!
//! Return the backtrace array from an error object or array
//! (typically caught by a @[catch]), or zero if there is none. Errors
//! are thrown on if there are problems retrieving the backtrace.
//!
//! @seealso
//! @[describe_backtrace()], @[describe_error()]
//!
array get_backtrace (object|array err)
{
  array bt;

  // Note: Partial code duplication in describe_backtrace and describe_error.

  if (objectp(err) && ([object]err)->is_generic_error) {
    object err_obj = [object] err;

    if (functionp (err_obj->backtrace))
      bt = err_obj->backtrace();
    else if (zero_type (bt = err_obj->error_backtrace))
      // For compatibility with error objects trying to behave like
      // arrays.
      bt = err_obj[1];

    if (bt && !arrayp (bt))
      error ("Backtrace in %O is %t, expected array.\n", err_obj, bt);
  }

  else if (arrayp(err) && sizeof([array]err)==2 &&
	   (!(bt = ([array]err)[1]) || arrayp (bt)))
    {}

  else if (err)
    error ("Invalid error container: %O\n", err);

  return bt;
}


#ifdef ENCODE_DEBUG
#  define ENC_MSG(X...) do werror (X); while (0)
#  define ENC_RETURN(val) do {						\
  mixed _v__ = (val);							\
  werror ("  returned %s\n",						\
	  zero_type (_v__) ? "UNDEFINED" :				\
	  sprintf ("%O", _v__));					\
  return _v__;								\
} while (0)
#else
#  define ENC_MSG(X...) do {} while (0)
#  define ENC_RETURN(val) do return (val); while (0)
#endif

#ifdef DECODE_DEBUG
#  define DEC_MSG(X...) do werror (X); while (0)
#  define DEC_RETURN(val) do {						\
  mixed _v__ = (val);							\
  werror ("  returned %s\n",						\
	  zero_type (_v__) ? "UNDEFINED" :				\
	  sprintf ("%O", _v__));					\
  return _v__;								\
} while (0)
#else
#  define DEC_MSG(X...) do {} while (0)
#  define DEC_RETURN(val) do return (val); while (0)
#endif

class Encoder
//! @appears Pike.Encoder
//!
//! Codec for use with @[encode_value]. It understands all the
//! standard references to builtin functions, pike modules, and the
//! main program script.
//!
//! The format of the produced identifiers are documented here to
//! allow extension of this class:
//!
//! The produced names are either strings or arrays. The string
//! variant specifies the thing to look up according to the first
//! character:
//!
//! 'c'   Look up in all_constants().
//! 's'   Look up in _static_modules.
//! 'r'   Look up with resolv().
//! 'p'   Look up in programs.
//! 'o'   Look up in programs, then look up the result in objects.
//! 'f'   Look up in fc.
//!
//! In the array format, the first element is a string as above and
//! the rest specify a series of things to do with the result:
//!
//! A string   Look up this string in the result.
//! 'm'        Get module object in dirnode.
//! 'p'        Do object_program(result).
//!
//! All lowercase letters and the symbols ':', '/' and '.' are
//! reserved for internal use in both cases where characters are used
//! above.
{
  mixed encoded;

  protected mapping(mixed:string) rev_constants = ([]);
  protected mapping(mixed:string) rev_static_modules = ([]);

  protected array find_index (object|program parent, mixed child,
			      array(object) module_object,
			      int|void try)
  {
    array id;

  find_id: {
      array inds = indices (parent), vals = values (parent);
      int i = search (vals, child);
      if (i >= 0 && parent[inds[i]] == child) {
	id = ({inds[i]});
	ENC_MSG ("  found as parent value with index %O\n", id[0]);
      }

      else {
	// Try again with the programs of the objects in parent, since
	// it's common that only objects and not their programs are
	// accessible in modules.
	foreach (vals; i; mixed val)
	  if (objectp (val) && child == object_program (val) &&
	      val == parent[inds[i]]) {
	    if (module_object) {
	      module_object[0] = val;
	      id = ({inds[i]});
	    }
	    else
	      id = ({inds[i], 'p'});
	    ENC_MSG ("  found as program of parent value object %O with index %O\n",
		     val, id[0]);
	    break find_id;
	  }

	if (try) {
	  ENC_MSG("Cannot find %O in %O.\n", child, parent);
	  return UNDEFINED;
	}
	error ("Cannot find %O in %O.\n", child, parent);
      }
    }

    if (!stringp (id[0])) {
      if (try) {
	ENC_MSG("Got nonstring index %O for %O in %O.\n", id[0], child, parent);
	return UNDEFINED;
      }
      error ("Got nonstring index %O for %O in %O.\n", id[0], child, parent);
    }

    return id;
  }

  protected string|array compare_resolved (string name, mixed what,
					   mixed resolved,
					   array(object) module_object)
  {
    array append;

  compare: {
      if (resolved == what) {
	ENC_MSG ("  compare_resolved: %O is %O\n", what, resolved);
	/* No need for anything advanced. resolv() does the job. */
	return "r" + name;
      }

      if (objectp (resolved)) {
	if (object_program (resolved) == what) {
	  ENC_MSG ("  compare_resolved: %O is program of %O\n", what, resolved);
	  append = ({'p'});
	  break compare;
	}

	if (resolved->is_resolv_dirnode)
	  if (resolved->module == what) {
	    ENC_MSG ("  compare_resolved: %O is dirnode module of %O\n", what, resolved);
	    append = ({'m'});
	    resolved = resolved->module;
	    break compare;
	  }
	  else if (object_program (resolved->module) == what) {
	    ENC_MSG ("  compare_resolved: %O is program of dirnode module of %O\n",
		     what, resolved);
	    append = ({'m', 'p'});
	    break compare;
	  }
	  else
	    ENC_MSG ("  compare_resolved: %O is different from dirnode module %O\n",
		     what, resolved->module);

#if 0
	// This is only safe if the joinnode modules don't conflict,
	// and we don't know that.
	if (resolved->is_resolv_joinnode) {
	  ENC_MSG ("  compare_resolved: searching for %O in joinnode %O\n",
		   what, resolved);
	  foreach (resolved->joined_modules, mixed part)
	    if (string|array name = compare_resolved (name, what, part,
						      module_object)) {
	      if (module_object) module_object[0] = resolved;
	      return name;
	    }
	}
#endif
      }

      ENC_MSG ("  compare_resolved: %O is different from %O\n", what, resolved);
      return 0;
    }

    name = "r" + name;
    string|array res = name;

    if (append)
      if (module_object) {
	// The caller is going to do subindexing. In both the 'p' and
	// 'm' cases it's better to do that from the original
	// object/dirnode, so just drop the suffixes.
	module_object[0] = resolved;
	return res;
      }
      else
	return (arrayp (res) ? res : ({res})) + append;
    else
      return res;
  }

  string|array nameof (mixed what, void|array(object) module_object)
  //! When @[module_object] is set and the name would end with an
  //! @expr{object_program@} step (i.e. @expr{'p'@}), then drop that
  //! step so that the name corresponds to the object instead.
  //! @expr{@[module_object][0]@} will receive the found object.
  {
    ENC_MSG ("nameof (%t %O)\n", what, what);

    if (what == encoded) {
      ENC_MSG ("  got the thing to encode - encoding recursively\n");
      return UNDEFINED;
    }

    if (string id = rev_constants[what]) ENC_RETURN (id);
    if (string id = rev_static_modules[what]) ENC_RETURN (id);

    if (objectp (what)) {

      if (what->is_resolv_dirnode) {
	ENC_MSG ("  is a dirnode\n");
	string name = program_path_to_name (what->dirname);
	if (string|array ref = compare_resolved (name, what, resolv (name),
						 module_object))
	  ENC_RETURN (ref);
      }

      else if (what->is_resolv_joinnode) {
	ENC_MSG ("  is a joinnode\n");
	object modules = Builtin.array_iterator (what->joined_modules);
	object|mapping value;
      check_dirnode:
	if (modules && objectp (value = modules->value()) &&
	    value->is_resolv_dirnode) {
	  string name = program_path_to_name (value->dirname);
	  modules += 1;
	  foreach (modules;; value)
	    if (!objectp (value) || !value->is_resolv_dirnode ||
		program_path_to_name (value->dirname) != name)
	      break check_dirnode;
	  ENC_MSG ("  joinnode has consistent name %O\n", name);
	  if (string|array ref = compare_resolved (name, what, resolv (name),
						   module_object))
	    ENC_RETURN (ref);
	}
      }

      else if (what->is_pike_master) {
	ENC_MSG ("  is a master object\n");
	ENC_RETURN ("o/master");
      }

      program prog;
      if ((prog = objects_reverse_lookup (what)))
	ENC_MSG ("  found program in objects: %O\n", prog);
#if 0
      else if ((prog = object_program (what)))
	ENC_MSG ("  got program of object: %O\n", prog);
#endif

      if (prog) {
	if (prog == encoded) ENC_RETURN ("o");
	if (string path = programs_reverse_lookup (prog)) {
	  ENC_MSG ("  found path in programs: %O\n", path);
	  string name = program_path_to_name (path);
	  ENC_MSG ("  program name: %O\n", name);
	  if (string|array ref = compare_resolved (name,
						   what->_module_value || what,
						   resolv (name), module_object))
	    ENC_RETURN (ref);
	  else {
	    ENC_MSG ("  Warning: Failed to resolve; encoding path\n");
#ifdef PIKE_MODULE_RELOC
	    ENC_RETURN ("o" + unrelocate_module (path));
#else
	    ENC_RETURN ("o" + path);
#endif
	  }
	}
	if (prog == object_program(_static_modules)) {
	  ENC_MSG ("  got _static_modules.\n");
	  ENC_RETURN ("c" + "_static_modules");
	}
      }

      if (string path = fc_reverse_lookup (what)) {
	ENC_MSG ("  found path in fc: %O\n", path);
	string name = program_path_to_name (path);
	if (string|array ref = compare_resolved (name, what, resolv (name),
						 module_object))
	  ENC_RETURN (ref);
	else {
	    ENC_MSG ("  Warning: Failed to resolve; encoding path\n");
#ifdef PIKE_MODULE_RELOC
	  ENC_RETURN ("f" + unrelocate_module (path));
#else
	  ENC_RETURN ("f" + path);
#endif
	}
      }

      if (what->_encode) {
	ENC_MSG ("  object got _encode function - encoding recursively\n");
	return UNDEFINED;
      }

      if (function|program prog = object_program (what)) {
	ENC_MSG ("  got program of object: %O\n", prog);
	object|program parent;
	if (!(parent = function_object (prog) || function_program (prog))) {
	  // Check if prog is in a module directory.
	  if (string path = programs_reverse_lookup (prog)) {
	    path = combine_path(path, "..");
	    ENC_MSG ("  found parent path in programs: %O\n", path);
	    parent = fc[path];
	  }
	}
	if (parent) {
	  ENC_MSG ("  got parent of program: %O\n", parent);
	  // We're going to subindex the parent so we ask for the
	  // module object and not the program. That since we'll
	  // always be able to do a better job if we base the indexing
	  // on objects.
	  array parent_object = ({0});
	  string|array parent_name = nameof (parent, parent_object);
	  if (!parent_name) {
	    ENC_MSG ("  inside the thing to encode - encoding recursively\n");
	    return UNDEFINED;
	  }
	  else {
	    ENC_MSG("  parent has name: %O\n", parent_name);
	    if (objectp (parent_object[0])) parent = parent_object[0];
	    array id = find_index (parent, what, module_object);
	    if ((equal(id, ({"_module_value"}))) ||
		(equal(id, ({ "__default" })) && has_suffix(parent_name, "::")))
	      ENC_RETURN (parent_name);
	    else
	      ENC_RETURN ((arrayp (parent_name) ? parent_name : ({parent_name})) + id);
	  }
	}
      }

      error ("Failed to find name of unencodable object %O.\n", what);
    }

    if (programp (what) || functionp (what)) {
      if (string path = programs_reverse_lookup (what)) {
	ENC_MSG ("  found path in programs: %O\n", path);
	string name = program_path_to_name (path);
	if (string|array ref = compare_resolved (name, what, resolv (name),
						 module_object))
	  ENC_RETURN (ref);
	else {
	    ENC_MSG ("  Warning: Failed to resolve; encoding path\n");
#ifdef PIKE_MODULE_RELOC
	  ENC_RETURN ("p" + unrelocate_module (path));
#else
	  ENC_RETURN ("p" + path);
#endif
	}
      }

      if (object|program parent = function_object (what) || function_program (what)) {
	ENC_MSG ("  got parent: %O\n", parent);
	if (!objectp (parent)) {
	  object parent_obj = objects[parent];
	  if (objectp (parent_obj)) {
	    ENC_MSG ("  found object for parent program in objects: %O\n", parent_obj);
	    parent = parent_obj;
	  }
	}

	array parent_object = ({0});
	string|array parent_name = nameof (parent, parent_object);
	if (!parent_name) {
	  ENC_MSG ("  inside the thing to encode - encoding recursively\n");
	  return UNDEFINED;
	}

	else {
	  ENC_MSG("  parent has name: %O\n", parent_name);
	  if (objectp (parent_object[0])) parent = parent_object[0];
	  if (parent["_module_value"] == what &&
	      objects_reverse_lookup (parent)) {
	    ENC_MSG ("  found as _module_value of parent module\n");
	    ENC_RETURN (parent_name);
	  }
	  else {
	    string|array id = function_name ([program]what);
	    if (stringp (id) && parent[id] == what) {
	      ENC_MSG ("  found function name in parent: %O\n", id);
	      id = ({id});
	    }
	    else {
	      if (stringp(parent_name) &&
		  has_suffix(parent_name, "::__default") &&
		  parent->all_constants) {
		if (id = find_index(parent->all_constants(), what,
				    module_object, 1)) {
		  ENC_MSG("  found in all_constants() for %O: %O\n",
			  parent, id);
		  ENC_RETURN(({parent_name[..sizeof(parent_name)-
					   (1+sizeof("__default"))] + id[0]}) +
			     id[1..]);
		}
	      }
	      id = find_index (parent, what, module_object);
	    }
	    if (equal (id, ({"_module_value"})))
	      ENC_RETURN (parent_name);
	    else
	      ENC_RETURN ((arrayp (parent_name) ? parent_name : ({parent_name})) + id);
	  }
	}
      }

      error ("Failed to find name of %t %O.\n", what, what);
    }

    // FIXME: Should have a reverse mapping of constants in modules;
    // it can potentially be large mappings and stuff that we encode
    // here. They can go stale too.

    ENC_MSG ("  encoding recursively\n");
    return ([])[0];
  }

  mixed encode_object(object x)
  {
    DEC_MSG ("encode_object (%O)\n", x);
    if(!x->_encode) {
      error ("Cannot encode object %O without _encode function.\n", x);
      // return ({});
    }
    DEC_RETURN (([function]x->_encode)());
  }

  protected void create (void|mixed encoded)
  //! Creates an encoder instance. If @[encoded] is specified, it's
  //! encoded instead of being reverse resolved to a name. That's
  //! necessary to encode programs.
  {
    this_program::encoded = encoded;

    foreach (all_constants(); string var; mixed val)
      rev_constants[val] = "c" + var;

    // FIXME: We don't have any good way to do reverse lookups on
    // things that are assigned to module variables. Here we use
    // rev_constants only to find the most vital ones.
    {
      object val_module = resolv ("Val");
      foreach (indices (val_module), string var) {
	mixed val = val_module[var];
	if (!programp (val) && !functionp (val))
	  // The classes in Val.pmod are irrelevant since the current
	  // values might come from other implementations. Besides,
	  // they're covered by the ordinary module lookup code.
	  rev_constants[val] = "rVal." + var;
      }
    }

    rev_static_modules =
      mkmapping (values (_static_modules),
		 map (indices (_static_modules),
		      lambda (string name) {return "s" + name;}));

#if 0
    // This looks flawed; when the decoder looks it up, it'll get the
    // module and not its program. /mast
    foreach (rev_static_modules; mixed module; string name) {
      if (objectp(module)) {
	program p = object_program(module);
	if (!rev_static_modules[p]) {
	  // Some people inherit modules...
	  rev_static_modules[p] = "s" + name;
	}
      }
    }
#endif
  }
}

class Decoder (void|string fname, void|int mkobj, void|object handler)
//! @appears Pike.Decoder
//!
//! Codec for use with @[decode_value]. This is the decoder
//! corresponding to @[Encoder]. See that one for more details.
{
  protected int unregistered = 1;

  object __register_new_program(program p)
  {
    DEC_MSG ("__register_new_program (%O)\n", p);
    if(unregistered && fname)
    {
      unregistered = 0;
      resolv_debug("register %s\n", fname);
      programs[fname]=p;
      if (mkobj)
	DEC_RETURN (objectp (objects[p]) ? objects[p] : (objects[p]=__null_program()));
    }
    DEC_RETURN (0);
  }

  protected mixed thingof (string|array what)
  {
    mixed res;
    array sublist;
    if (arrayp (what)) sublist = [array]what, what = [array|string]sublist[0];

    switch (what[0]) {
      case 'c':
	if (zero_type (res = all_constants()[what[1..]]))
	  error ("Cannot find global constant %O.\n", what[1..]);
	break;
      case 's':
	if (zero_type (res = _static_modules[what[1..]]))
	  error ("Cannot find %O in _static_modules.\n", what[1..]);
	break;
      case 'r':
	if (zero_type (res = resolv ([string]what[1..], fname, handler)))
	  error ("Cannot resolve %O.\n", what[1..]);
	break;
      case 'p':
	if (!(res = low_cast_to_program ([string]what[1..], fname, handler)))
	  error ("Cannot find program for %O.\n", what[1..]);
	break;
      case 'o':
	if (!objectp(res = low_cast_to_object([string]what[1..], fname, handler)))
	  error ("Cannot find object for %O.\n", what[1..]);
	break;
      case 'f':
	if (!objectp (res = findmodule ([string]what[1..], handler)))
	  error ("Cannot find module for %O.\n", what[1..]);
	break;
    }

    DEC_MSG ("  got %O\n", res);

    if (sublist) {
      mixed subres = res;
      for (int i = 1; i < sizeof (sublist); i++) {
	mixed op = sublist[i];
	if (stringp (op)) {
	  if (!programp (subres) && !objectp (subres) && !mappingp (subres))
	    error ("Cannot subindex %O%{[%O]%} since it's a %t.\n",
		   res, sublist[1..i-1], subres);
	  if (zero_type (subres = ([mapping]subres)[op]))
	    error ("Cannot find %O in %O%{[%O]%}.\n",
		   op, res, sublist[1..i-1]);
	  DEC_MSG ("  indexed with %O: %O\n", op, subres);
	}
	else switch (op) {
	  case 'm':
	    if (objectp (subres) && ([object]subres)->is_resolv_joinnode) {
	      dirnode found;
	      foreach (([object(joinnode)]subres)->joined_modules,
		       object|mapping part)
		if (objectp (part) && part->is_resolv_dirnode && part->module) {
		  if (found)
		    error ("There are ambiguous module objects in %O.\n",
			   subres);
		  else
		    found = [object(dirnode)]part;
		}
	      if (found) subres = found;
	    }

	    if (objectp (subres) && ([object]subres)->is_resolv_dirnode) {
	      if (([object]subres)->module) {
		subres = ([object]subres)->module;
		DEC_MSG ("  got dirnode module %O\n", subres);
	      }
	      else
		error ("Cannot find module object in dirnode %O.\n", subres);
	    }
	    else
	      error ("Cannot get module object in thing that isn't "
		     "a dirnode or unambiguous joinnode: %O\n", subres);
	    break;

	  case 'p':
	    subres = object_program (subres);
	    DEC_MSG ("  got object_program %O\n", subres);
	    break;

	  default:
	    error ("Unknown sublist operation %O in %O\n", op, what);
	}
      }
      res = subres;
    }

    return res;
  }

  object objectof (string|array what)
  {
    DEC_MSG ("objectof (%O)\n", what);
    if (!what) {
      // This is necessary for compatibility with 7.2 encoded values:
      // If an object was fed to encode_value there and there was no
      // codec then a zero would be encoded silently since the failed
      // call to nameof was ignored. decode_value would likewise
      // silently ignore the failed call objectof(0) and a zero would
      // be decoded. Now we supply a fairly capable codec which is
      // used by default and we therefore get these objectof(0) calls
      // here. So if we throw an error we can't decode values which
      // 7.2 would encode and decode without a codec (albeit partly
      // incorrectly). So just print a sulky warning and continue.. :P
      DEC_MSG ("Warning: Decoded broken object identifier to zero.\n");
      DEC_RETURN (0);
    }
    DEC_RETURN ([object] thingof (what));
  }

  function functionof (string|array what)
  {
    DEC_MSG ("functionof (%O)\n", what);
    DEC_RETURN ([function] thingof (what));
  }

  program programof (string|array what)
  {
    DEC_MSG ("programof (%O)\n", what);
    DEC_RETURN ([program] thingof (what));
  }

  //! Restore the state of an encoded object.
  //!
  //! @param o
  //!   Object to modify.
  //!
  //! @param data
  //!   State information from @[Encoder()->encode_object()].
  //!
  //! The default implementation calls @expr{o->_decode(data)@}
  //! if the object has an @expr{_decode()@}, otherwise if
  //! @[data] is an array, returns it to indicate that @[lfun::create()]
  //! should be called.
  //!
  //! @note
  //!   This function is called @b{before@} @[lfun::create()]
  //!   in the object has been called, but after @[lfun::__INIT()]
  //!   has been called.
  //!
  //! @returns
  //!   Returns an array to indicate to the caller that
  //!   @[lfun::create()] should be called with the elements
  //!   of the array as arguments.
  //!
  //!   Returns @expr{0@} (zero) to inhibit calling of @[lfun::create()].
  //!
  //! @seealso
  //!   @[Encoder()->encode_object()]
  array(mixed) decode_object(object o, mixed data)
  {
    DEC_MSG ("decode_object (object(%O), %O)\n", object_program (o), data);
    if(!o->_decode) {
      if (!arrayp(data)) {
	error ("Cannot decode object(%O) without _decode function.\n",
	       object_program (o));
      }
      // Try calling lfun::create().
      return data;
    }
    ([function(mixed:void)]o->_decode)(data);
    return 0;
  }
}

// Note: This mapping is not for caching but for coping with recursion
// problems by returning the same codec for the same file. As a cache
// it's really pointless.
protected mapping(string:Decoder) codecs = set_weak_flag (([]), 1);

Decoder get_codec(string|void fname, int|void mkobj, void|object handler)
{
  if (handler)
    // Decoders are currently not reused in this case since it's
    // tricky to set up a weak multilevel mapping for lookup. This is
    // seldom a problem since explicit handlers are fairly rare. If it
    // is then the handler can always override this function.
    return Decoder (fname, mkobj, handler);
  string key = fname + "\0" + mkobj;
  if (codecs[key]) return codecs[key];
  return codecs[key] = Decoder(fname, mkobj);
}

class Codec
//! @appears Pike.Codec
//!
//! @[Encoder] and @[Decoder] rolled into one. This is for mainly
//! compatibility; there's typically no use combining encoding and
//! decoding into the same object.
{
  inherit Encoder;
  inherit Decoder;

  protected void create (void|mixed encoded)
  //! The optional argument is the thing to encode; it's passed on to
  //! @[Encoder].
  {
    Encoder::create (encoded);
  }
}

// The master acts as the default codec.
inherit Codec;


//! Contains version information about a Pike version.
class Version
{

  //! The major and minor parts of the version.
  int major;
  int minor;

  //! @decl void create(int major, int minor)
  //! Set the version in the object.
  protected void create(int maj, int min)
  {
    if(maj==-1) maj = __REAL_MAJOR__;
    if(min==-1) min = __REAL_MINOR__;
    major = maj;
    minor = min;
  }

#define CMP(X)  ((major - ([object]X)->major) || (minor - ([object]X)->minor))

  //! Methods define so that version objects
  //! can be compared and ordered.
  int `<(mixed v) { return objectp(v) && CMP(v) < 0; }
  int `>(mixed v) { return objectp(v) && CMP(v) > 0; }
  int `==(mixed v) { return objectp(v) && CMP(v)== 0; }
  int __hash() { return major * 4711 + minor ; }

  string _sprintf(int t) {
    switch(t) {
    case 's': return sprintf("%d.%d",major,minor);
    case 'O': return sprintf("%O(%s)", this_program, this);
    }
  }

  //! The version object can be casted into a string.
  mixed cast(string type)
    {
      switch(type)
      {
	case "string":
	  return sprintf("%d.%d",major,minor);
      }
    }
}

//! Version information about the current Pike version.
Version currentversion = Version(__REAL_MAJOR__, __REAL_MINOR__);

mapping(Version:CompatResolver) compat_handler_cache = ([
  currentversion:this_object(),
]);

CompatResolver get_compilation_handler(int major, int minor)
{
  Version v=Version(major,minor);

  if(v > currentversion)
  {
    /* Do we want to make an error if major.minor > __MAJOR__.__MINOR ?
     *
     * No; it's useful for referring to the next version when writing
     * compat modules.
     */
    return 0;
  }

  CompatResolver ret;

  if(!zero_type(ret=compat_handler_cache[v])) return ret;

  array(string) files;
  array(Version) available=({});

#if "#share_prefix#"[0]!='#'
  if (!(files = master_get_dir("#share_prefix#"))) {
    werror ("Error listing directory %O: %s\n",
	    "#share_prefix#", strerror (errno()));
    files = ({});
  }
  foreach(files, string ver)
    {
      if(sscanf(ver,"%d.%d",int maj, int min))
      {
	Version x=Version(maj, min) ;
	if(x >= v)
	  available|=({ x });
      }
    }
#endif

#if "#lib_prefix#"[0]!='#'
  if (!(files = master_get_dir("#lib_prefix#"))) {
    werror ("Error listing directory %O: %s\n",
	    "#lib_prefix#", strerror (errno()));
    files = ({});
  }
  foreach(files, string ver)
    {
      if(sscanf(ver,"%d.%d",int maj, int min))
      {
	Version x=Version(maj, min) ;
	if(x >= v)
	  available|=({ x });
      }
    }
#endif

  sort(available);

#ifndef RESOLVER_HACK
  /* We need to define RESOLVER_HACK when we add
   * version-specific stuff in the CompatResolver.
   * As long as all the compatibility is done in the
   * module directories, RESOLVER_HACK can be undefined
   */

  /* No compat needed */
  if(!sizeof(available))
  {
    compat_handler_cache[v]=0;
    return 0;
  }

  /* Same as available[0] */
  if(ret=compat_handler_cache[available[0]])
    return compat_handler_cache[v]=ret;
#endif

  // The root resolver is this object.
  ret = this;

  foreach(reverse(available), Version tmp)
    {
      CompatResolver compat_handler = compat_handler_cache[tmp];
      if (!compat_handler) {
	// Create a new compat handler, that
	// falls back to the successor version.
	if (tmp <= Version(0, 6)) {
	  compat_handler = Pike06Resolver(tmp, ret);
	} else {
	  compat_handler = CompatResolver(tmp, ret);
	}

	string base;
#if "#lib_prefix#"[0]!='#'
	base=combine_path("#lib_prefix#",sprintf("%s",tmp));
	compat_handler->add_module_path(combine_path(base,"modules"));
	compat_handler->add_include_path(combine_path(base,"include"));
#endif

#if "#share_prefix#"[0]!='#'
	base=combine_path("#share_prefix#",sprintf("%s",tmp));
	compat_handler->add_module_path(combine_path(base,"modules"));
	compat_handler->add_include_path(combine_path(base,"include"));
#endif

	// Kludge to avoid(?) recursive compilation problems. It was
	// observed with a pike program containing
	//
	// #if constant (__builtin.security)
	// #endif
	//
	// when using -V7.4. There was a cycle between resolving
	// lib/modules/__builtin.pmod and lib/7.4/modules/__default.pmod.
	compat_handler->get_default_module();

#ifndef RESOLVER_HACK
	ret = compat_handler_cache[tmp] = compat_handler;
#endif
      } else {
	ret = compat_handler;
      }
    }

  // Note: May duplicate the assignment above.
  compat_handler_cache[v] = ret;

  return ret;
}

string _sprintf(int t)
{
  // NOTE: The ||'O' is for Pike 7.2 compat only.
  switch(t||'O') {
    // FIXME: What good is this lying for 't'? The master is still an
    // object.. /mast
  case 't': return "master";
  case 'O': return "master()";
  }
}

//! Return a master object compatible with the specified version of Pike.
//!
//! This function is used to implement the various compatibility versions
//! of @[master()].
//!
//! @seealso
//!   @[get_compilation_handler()], @[master()]
object get_compat_master(int major, int minor)
{
  if ((major < 7) || ((major == 7) && (minor < 7)))
    return Pike_7_6_master::get_compat_master(major, minor);
  // 7.7 and later.
  return this;
}