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
  
/* -*- c -*- 
|| 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: builtin.cmod,v 1.131 2003/04/07 17:28:54 nilsson Exp $ 
*/ 
 
#include "global.h" 
#include "interpret.h" 
#include "svalue.h" 
#include "opcodes.h" 
#include "pike_macros.h" 
#include "object.h" 
#include "program.h" 
#include "array.h" 
#include "pike_error.h" 
#include "constants.h" 
#include "mapping.h" 
#include "stralloc.h" 
#include "multiset.h" 
#include "pike_types.h" 
#include "pike_memory.h" 
#include "threads.h" 
#include <math.h> 
#include <ctype.h> 
#include "module_support.h" 
#include "cyclic.h" 
#include "bignum.h" 
#include "main.h" 
#include "operators.h" 
#include "builtin_functions.h" 
#include "fsort.h" 
#include "port.h" 
#include "gc.h" 
#include <assert.h> 
 
/*! @decl array(array(int|string)) describe_program(program p) 
 *! @belongs Debug 
 *! 
 *! Debug function for showing the symbol table of a program. 
 */ 
PIKEFUN array(array(int|string)) _describe_program(mixed x) 
     efun; 
{ 
  struct program *p; 
  struct array *res; 
  int i; 
 
  if (!(p = program_from_svalue(Pike_sp - args))) 
    SIMPLE_BAD_ARG_ERROR("_describe_program", 1, "program"); 
 
  for (i=0; i < (int)p->num_identifier_references;i++) { 
    struct reference *ref = p->identifier_references + i; 
    struct identifier *id = ID_FROM_PTR(p, ref); 
    struct inherit *inh = INHERIT_FROM_PTR(p, ref); 
    push_int(ref->id_flags); 
    ref_push_string(id->name); 
    push_int(id->identifier_flags); 
    if (id->identifier_flags & IDENTIFIER_C_FUNCTION) { 
      push_int(-2); 
    } else { 
      push_int(id->func.offset); 
    } 
    push_int(ref->inherit_offset); 
    push_int(inh->inherit_level); 
    f_aggregate(6); 
  } 
  f_aggregate(p->num_identifier_references); 
  dmalloc_touch_svalue(Pike_sp-1); 
  res = Pike_sp[-1].u.array; 
  Pike_sp--; 
  pop_n_elems(args); 
  push_array(res); 
} 
 
/*! @decl string basetype(mixed x) 
 *! 
 *! Same as sprintf("%t",x); 
 *! 
 *! @seealso 
 *! @[sprintf()] 
 */ 
PIKEFUN string basetype(mixed x) 
  efun; 
  optflags OPT_TRY_OPTIMIZE; 
{ 
  int t=x->type; 
  if(x->type == T_OBJECT && x->u.object->prog) 
  { 
    ptrdiff_t fun=FIND_LFUN(x->u.object->prog, LFUN__SPRINTF); 
    if(fun != -1) 
    { 
      push_int('t'); 
      f_aggregate_mapping(0); 
      apply_low(x->u.object, fun, 2); 
      if(Pike_sp[-1].type == T_STRING) 
      { 
        stack_swap(); 
        pop_stack(); 
        return; 
      } else if (UNSAFE_IS_ZERO(Pike_sp-1)) { 
        pop_n_elems(2); 
        push_constant_text("object"); 
        return; 
      } else { 
        Pike_error("Non-string returned from _sprintf()\n"); 
      } 
    } 
  } 
  pop_stack(); 
  switch(t) 
  { 
    case T_ARRAY: push_constant_text("array"); break; 
    case T_FLOAT: push_constant_text("float"); break; 
    case T_FUNCTION: push_constant_text("function"); break; 
    case T_INT: push_constant_text("int"); break; 
    case T_MAPPING: push_constant_text("mapping"); break; 
    case T_MULTISET: push_constant_text("multiset"); break; 
    case T_OBJECT: push_constant_text("object"); break; 
    case T_PROGRAM: push_constant_text("program"); break; 
    case T_STRING: push_constant_text("string"); break; 
    case T_TYPE: push_constant_text("type"); break; 
    case T_ZERO: push_constant_text("zero"); break; 
    case T_VOID: push_constant_text("void"); break; 
      /* The following are internal and shouldn't be applicable in normal use. */ 
    case T_SVALUE_PTR: push_constant_text("svalue_ptr"); break; 
    case T_OBJ_INDEX: push_constant_text("obj_index"); break; 
    case T_MAPPING_DATA: push_constant_text("mapping_data"); break; 
    case T_PIKE_FRAME: push_constant_text("pike_frame"); break; 
    case T_MULTISET_DATA: push_constant_text("multiset_data"); break; 
    default: push_constant_text("unknown"); break; 
  } 
} 
 
 
/*! @decl string int2char(int x) 
 *! @appears String.int2char 
 *! 
 *! Same as sprintf("%c",x); 
 *! 
 *! @seealso 
 *! @[sprintf()] 
 */ 
PIKEFUN string int2char(int|object x) 
  efun; 
  optflags OPT_TRY_OPTIMIZE; 
{ 
  int c; 
  if(x->type == T_OBJECT && x->u.object->prog) 
  { 
    ptrdiff_t fun=FIND_LFUN(x->u.object->prog, LFUN__SPRINTF); 
    if(fun != -1) 
    { 
      push_int('c'); 
      f_aggregate_mapping(0); 
      apply_low(x->u.object, fun, 2); 
      if(Pike_sp[-1].type == T_STRING) 
      { 
        stack_swap(); 
        pop_stack(); 
        return; 
      } 
      Pike_error("Non-string returned from _sprintf()\n"); 
    } 
  } 
  if(x->type != T_INT) 
    SIMPLE_BAD_ARG_ERROR("int2char", 1, "int"); 
 
  c=x->u.integer; 
 
  if(c>=0 && c<256) 
  { 
    struct pike_string *s; 
    s=begin_shared_string(1); 
    s->str[0]=c; 
    RETURN end_shared_string(s); 
  }else{ 
    struct string_builder tmp; 
    init_string_builder(&tmp,0); 
    string_builder_putchar(&tmp, c); 
    RETURN finish_string_builder(&tmp); 
  } 
} 
 
/*! @decl string int2hex(int x) 
 *! @appears String.int2hex 
 *! 
 *! Same as sprintf("%x",x); 
 *! 
 *! @seealso 
 *! @[sprintf()] 
 */ 
PIKEFUN string int2hex(int|object x) 
  efun; 
  optflags OPT_TRY_OPTIMIZE; 
{ 
  INT_TYPE c; 
  unsigned INT_TYPE n; 
  int len; 
  struct pike_string *s; 
 
  if(x->type == T_OBJECT && x->u.object->prog) 
  { 
    ptrdiff_t fun=FIND_LFUN(x->u.object->prog, LFUN__SPRINTF); 
    if(fun != -1) 
    { 
      push_int('x'); 
      f_aggregate_mapping(0); 
      apply_low(x->u.object, fun, 2); 
      if(Pike_sp[-1].type == T_STRING) 
      { 
        stack_swap(); 
        pop_stack(); 
        return; 
      } 
      Pike_error("Non-string returned from _sprintf()\n"); 
    } 
  } 
  if(x->type != T_INT) 
    SIMPLE_BAD_ARG_ERROR("int2hex", 1, "int"); 
 
  c=x->u.integer; 
 
  len=1; 
  if(c<0) { 
    len++; 
    n=(-c)&((unsigned INT_TYPE)(-1)); 
  }else{ 
    n=c; 
  } 
  while(n>65535) { n>>=16; len+=4; } 
  while(n>15) { n>>=4; len++; } 
   
  s=begin_shared_string(len); 
  c=x->u.integer; 
  if(!c) 
  { 
    s->str[0]='0'; 
  }else{ 
    if(c<0) 
    { 
      s->str[0]='-'; 
      n=(-c)&((unsigned INT_TYPE)(-1)); 
    }else{ 
      n=c; 
    } 
    while(len && n) 
    { 
      s->str[--len]="0123456789abcdef"[n&0xf]; 
      n>>=4; 
    } 
  } 
  RETURN end_shared_string(s); 
} 
 
/*! @decl array column(array data, mixed index) 
 *! 
 *! Extract a column from a two-dimensional array. 
 *! 
 *! This function is exactly equivalent to: 
 *! @code 
 *! map(@[data], lambda(mixed x,mixed y) { return x[y]; }, @[index]) 
 *! @endcode 
 *! 
 *! Except of course it is a lot shorter and faster. 
 *! That is, it indices every index in the array data on the value of 
 *! the argument index and returns an array with the results. 
 *! 
 *! @seealso 
 *! @[rows()] 
 */ 
PIKEFUN array column(array data, mixed index) 
  efun; 
  optflags OPT_TRY_OPTIMIZE; 
{ 
  INT32 e; 
  struct array *a; 
 
  DECLARE_CYCLIC(); 
 
  /* Optimization */ 
  if(data->refs == 1) 
  { 
    /* An array with one ref cannot possibly be cyclic */ 
    struct svalue sval; 
    data->type_field = BIT_MIXED | BIT_UNFINISHED; 
    for(e=0;e<data->size;e++) 
    { 
      index_no_free(&sval, ITEM(data)+e, index); 
      free_svalue(ITEM(data)+e); 
      ITEM(data)[e]=sval; 
    } 
    pop_stack(); 
    return; 
  } 
 
  if((a=(struct array *)BEGIN_CYCLIC(data,0))) 
  { 
    add_ref(a); 
  }else{ 
    push_array(a=allocate_array(data->size)); 
    SET_CYCLIC_RET(a); 
 
    for(e=0;e<a->size;e++) 
      index_no_free(ITEM(a)+e, ITEM(data)+e, index); 
 
    dmalloc_touch_svalue(Pike_sp-1); 
    Pike_sp--; 
  } 
  END_CYCLIC(); 
  RETURN a; 
} 
 
/*! @decl multiset mkmultiset(array a) 
 *! 
 *! This function creates a multiset from an array. 
 *! 
 *! @seealso 
 *! @[aggregate_multiset()] 
 *! 
 */ 
PIKEFUN multiset(1) mkmultiset(array(1=mixed) a) 
  efun; 
  optflags OPT_TRY_OPTIMIZE|OPT_EXTERNAL_DEPEND; 
{ 
  RETURN mkmultiset(a); 
} 
 
/*! @decl int trace(int level, void|string facility, void|int all_threads) 
 *! 
 *! This function changes the trace level for the subsystem identified 
 *! by @[facility] to @[level]. If @[facility] is zero or left out, it 
 *! changes the global trace level which affects all subsystems. 
 *! 
 *! Enabling tracing causes messages to be printed to stderr. A higher 
 *! trace level includes the output from all lower levels. The lowest 
 *! level is zero which disables all trace messages. 
 *! 
 *! See the @tt{-t@} command-line option for more information. 
 *! 
 *! @param level 
 *!   If @[facility] is specified then there is typically only one 
 *!   trace level for it, i.e. it's an on-or-off toggle. The global 
 *!   trace levels, when @[facility] isn't specified, are: 
 *! 
 *!   @int 
 *!   @value 1 
 *!     Trace calls to Pike functions and garbage collector runs. 
 *!   @value 2 
 *!     Trace calls to builtin functions. 
 *!   @value 3 
 *!     Trace every interpreted opcode. 
 *!   @value 4 
 *!     Also trace the opcode arguments. 
 *!   @endint 
 *! 
 *! @param facility 
 *!   Valid facilities are: 
 *! 
 *!   @string 
 *!   @value "gc" 
 *!     Trace the start and end of each run of the garbage collector. 
 *!     The setting is never thread local. 
 *!   @endstring 
 *! 
 *! @param all_threads 
 *!   Trace levels are normally thread local, so changes affect only 
 *!   the current thread. To change the level in all threads, pass a 
 *!   nonzero value in this argument. 
 *! 
 *! @returns 
 *!   The old trace level in the current thread is returned. 
 */ 
PIKEFUN int trace(int level, void|string facility, void|int all_threads) 
  efun; 
  optflags OPT_SIDE_EFFECT; 
{ 
  INT32 old_level; 
  if (facility) { 
    struct pike_string *gc_str; 
    if (facility->type != T_STRING) 
      SIMPLE_BAD_ARG_ERROR("trace", 2, "void|string"); 
    MAKE_CONST_STRING(gc_str, "gc"); 
    if (facility->u.string == gc_str) { 
      old_level = gc_trace; 
      gc_trace = level; 
    } 
    else { 
      bad_arg_error("trace", Pike_sp-args, args, 2, 
                   "trace facility identifier", Pike_sp-args+1, 
                   "Bad argument 2 to trace(). Unknown trace facility."); 
    } 
  } 
  else { 
    old_level = Pike_interpreter.trace_level; 
    gc_trace = level; 
#ifdef PIKE_THREADS 
    if (!all_threads || UNSAFE_IS_ZERO (all_threads)) 
      Pike_interpreter.trace_level = level; 
    else { 
      struct thread_state *s; 
      FOR_EACH_THREAD(s, s->state.trace_level = level); 
    } 
#else 
    Pike_interpreter.trace_level = level; 
#endif 
  } 
  RETURN old_level; 
} 
 
/*! @decl mapping(string:float) gc_parameters (void|mapping(string:mixed) params) 
 *! @belongs Pike 
 *! 
 *! Set and get various parameters that control the operation of the 
 *! garbage collector. The passed mapping contains the parameters to 
 *! set. If a parameter is missing from the mapping, the current value 
 *! will be filled in instead. The same mapping is returned. Thus an 
 *! empty mapping, or no argument at all, causes a mapping with all 
 *! current settings to be returned. 
 *! 
 *! The following parameters are recognized: 
 *! 
 *! @mapping 
 *! @member int "enabled" 
 *!   If this is 1 then the gc is enabled as usual. If it's 0 then all 
 *!   automatically scheduled gc runs are disabled and the parameters 
 *!   below have no effect, but explicit runs through the @[gc] 
 *!   function still works as usual. If it's -1 then the gc is 
 *!   completely disabled so that even explicit @[gc] calls won't do 
 *!   anything. 
 *! @member float "garbage_ratio_low" 
 *!   As long as the gc time is less than gc_time_ratio, aim to run 
 *!   the gc approximately every time the ratio between the garbage 
 *!   and the total amount of allocated things is this. 
 *! @member float "time_ratio" 
 *!   When more than this fraction of the cpu time is spent in the gc, 
 *!   aim for gc_garbage_ratio_high instead of gc_garbage_ratio_low. 
 *! @member float "garbage_ratio_high" 
 *!   Upper limit for the garbage ratio - run the gc as often as it 
 *!   takes to keep it below this. 
 *! @member float "average_slowness" 
 *!   When predicting the next gc interval, use a decaying average 
 *!   with this slowness factor. It should be a value between 0.0 and 
 *!   1.0 that specifies the weight to give to the old average value. 
 *!   The remaining weight up to 1.0 is given to the last reading. 
 *! @endmapping 
 *! 
 *! @seealso 
 *!   @[gc], @[Debug.gc_status] 
 */ 
PIKEFUN mapping(string:mixed) gc_parameters (void|mapping(string:mixed) params) 
  errname Pike.gc_parameters; 
  optflags OPT_SIDE_EFFECT; 
{ 
  struct pike_string *str; 
  struct svalue *set; 
  struct svalue get; 
 
  if (!params) { 
    push_mapping (allocate_mapping (5)); 
    params = Pike_sp - 1; 
  } 
  else if (params->type != T_MAPPING) 
    SIMPLE_BAD_ARG_ERROR ("Pike.gc_parameters", 1, "void|mapping"); 
 
#define HANDLE_PARAM(NAME, CHECK_AND_SET, GET) do {                     \ 
    MAKE_CONST_STRING (str, NAME);                                      \ 
    if ((set = low_mapping_string_lookup (params->u.mapping, str))) {   \ 
      CHECK_AND_SET;                                                    \ 
    }                                                                   \ 
    else {                                                              \ 
      GET;                                                              \ 
      mapping_string_insert (params->u.mapping, str, &get);             \ 
    }                                                                   \ 
  } while (0) 
 
#define HANDLE_FLOAT_FACTOR(NAME, VAR)                                  \ 
  HANDLE_PARAM (NAME, {                                                 \ 
      if (set->type != T_FLOAT ||                                       \ 
          set->u.float_number < 0.0 || set->u.float_number > 1.0)       \ 
        SIMPLE_BAD_ARG_ERROR ("Pike.gc_parameters", 1,                  \ 
                              "float between 0.0 and 1.0 for " NAME);   \ 
      VAR = set->u.float_number;                                        \ 
    }, {                                                                \ 
      get.type = T_FLOAT;                                               \ 
      get.u.float_number = VAR;                                         \ 
    }); 
 
  HANDLE_PARAM ("enabled", { 
      if (set->type != T_INT || set->u.integer < -1 || set->u.integer > 1) 
        SIMPLE_BAD_ARG_ERROR ("Pike.gc_parameters", 1, 
                              "integer in the range -1..1 for 'enabled'"); 
      if (gc_enabled != set->u.integer) { 
        if (gc_enabled > 0) 
          gc_enabled = set->u.integer; 
        else { 
          gc_enabled = 1; 
          if (alloc_threshold == GC_MAX_ALLOC_THRESHOLD) 
            alloc_threshold = GC_MIN_ALLOC_THRESHOLD; 
        } 
      } 
    }, { 
      get.type = T_INT; 
      get.u.integer = gc_enabled; 
    }); 
  HANDLE_FLOAT_FACTOR ("garbage_ratio_low", gc_garbage_ratio_low); 
  HANDLE_FLOAT_FACTOR ("time_ratio", gc_time_ratio); 
  HANDLE_FLOAT_FACTOR ("garbage_ratio_high", gc_garbage_ratio_high); 
  HANDLE_FLOAT_FACTOR ("average_slowness", gc_average_slowness); 
 
#undef HANDLE_PARAM 
#undef HANDLE_FLOAT_FACTOR 
 
  REF_RETURN params->u.mapping; 
} 
 
/*! @decl string ctime(int timestamp) 
 *! 
 *! Convert the output from a previous call to @[time()] into a readable 
 *! string containing the current year, month, day and time. 
 *! 
 *! Like @[localtime], this function might throw an error if the 
 *! ctime(2) call failed on the system. It's platform dependent what 
 *! time ranges that function can handle, e.g. Windows doesn't handle 
 *! a negative @[timestamp]. 
 *! 
 *! @seealso 
 *! @[time()], @[localtime()], @[mktime()], @[gmtime()] 
 */ 
PIKEFUN string ctime(int timestamp) 
  efun; 
  optflags OPT_TRY_OPTIMIZE; 
{ 
  time_t i=(time_t)timestamp; 
  char *s = ctime (&i); 
  if (!s) Pike_error ("ctime() on this system cannot handle " 
                      "the timestamp %ld.\n", (long) i); 
  RETURN make_shared_string(s); 
} 
 
/*! @decl mapping mkmapping(array ind, array val) 
 *! 
 *! Make a mapping from two arrays. 
 *! 
 *! Makes a mapping @[ind[x]]:@[val[x]], @tt{0 <= x < sizeof(ind)@}. 
 *! 
 *! @[ind] and @[val] must have the same size. 
 *! 
 *! This is the inverse operation of @[indices()] and @[values()]. 
 *! 
 *! @seealso 
 *! @[indices()], @[values()] 
 */ 
PIKEFUN mapping(1:2) mkmapping(array(1=mixed) ind, array(2=mixed) val) 
  efun; 
  optflags OPT_TRY_OPTIMIZE|OPT_EXTERNAL_DEPEND; 
{ 
  if(ind->size != val->size) 
    bad_arg_error("mkmapping", Pike_sp-args, args, 2, "array", Pike_sp+1-args, 
                  "mkmapping called on arrays of different sizes (%d != %d)\n", 
                  ind->size, val->size); 
 
  RETURN mkmapping(ind, val); 
} 
 
/*! @decl int count(string haystack, string needle) 
 *! @belongs String 
 *! 
 *! Count the number of non-overlapping times the string @[needle] occurrs 
 *! in the string @[haystack]. 
 *! 
 *! @seealso 
 *! @[search()], @[`/()] 
 */ 
PIKEFUN int string_count(string haystack, string needle) 
  errname String.count; 
  optflags OPT_TRY_OPTIMIZE; 
{ 
   ptrdiff_t c = 0; 
   ptrdiff_t i, j; 
 
   switch (needle->len) 
   { 
     case 0: 
       switch (haystack->len) 
       { 
         case 0: c=1; break; /* "" appears one time in "" */ 
         case 1: c=0; break; /* "" doesn't appear in "x" */ 
         default: c=haystack->len-1; /* one time between each character */ 
       } 
       break; 
     case 1: 
       /* maybe optimize? */ 
     default: 
       for (i=0; i<haystack->len; i++) 
       { 
         j=string_search(haystack,needle,i); 
         if (j==-1) break; 
         i=j+needle->len-1; 
         c++; 
       } 
       break; 
   } 
   RETURN DO_NOT_WARN((INT_TYPE)c); 
} 
 
/*! @decl string trim_whites (string s) 
 *! @belongs String 
 *! 
 *! Trim leading and trailing spaces and tabs from the string @[s]. 
 */ 
PIKEFUN string string_trim_whites (string s) 
  errname String.trim_whites; 
  optflags OPT_TRY_OPTIMIZE; 
{ 
  ptrdiff_t start = 0, end = s->len; 
  int chr; 
  switch (s->size_shift) { 
#define DO_IT(TYPE)                                                     \ 
    {                                                                   \ 
      for (; start < s->len; start++) {                                 \ 
        chr = ((TYPE *) s->str)[start];                                 \ 
        if (chr != ' ' && chr != '\t') break;                           \ 
      }                                                                 \ 
      while (--end > start) {                                           \ 
        chr = ((TYPE *) s->str)[end];                                   \ 
        if (chr != ' ' && chr != '\t') break;                           \ 
      }                                                                 \ 
    } 
    case 0: DO_IT (p_wchar0); break; 
    case 1: DO_IT (p_wchar1); break; 
    case 2: DO_IT (p_wchar2); break; 
#undef DO_IT 
  } 
  RETURN string_slice (s, start, end + 1 - start); 
} 
 
/*! @decl string trim_all_whites (string s) 
 *! @belongs String 
 *! 
 *! Trim leading and trailing white spaces characters (space, tab, 
 *! newline and carriage return) from the string @[s]. 
 */ 
PIKEFUN string string_trim_all_whites (string s) 
  errname String.trim_all_whites; 
  optflags OPT_TRY_OPTIMIZE; 
{ 
  ptrdiff_t start = 0, end = s->len; 
  int chr; 
  switch (s->size_shift) { 
#define DO_IT(TYPE)                                                     \ 
    {                                                                   \ 
      for (; start < s->len; start++) {                                 \ 
        chr = ((TYPE *) s->str)[start];                                 \ 
        if (chr != ' ' && chr != '\t' && chr != '\n' && chr != '\r')    \ 
          break;                                                        \ 
      }                                                                 \ 
      while (--end > start) {                                           \ 
        chr = ((TYPE *) s->str)[end];                                   \ 
        if (chr != ' ' && chr != '\t' && chr != '\n' && chr != '\r')    \ 
          break;                                                        \ 
      }                                                                 \ 
    } 
    case 0: DO_IT (p_wchar0); break; 
    case 1: DO_IT (p_wchar1); break; 
    case 2: DO_IT (p_wchar2); break; 
#undef DO_IT 
  } 
  RETURN string_slice (s, start, end + 1 - start); 
} 
 
/*! @decl int implements(program prog, program api) 
 *! @belongs Program 
 *! 
 *! Returns 1 if @[prog] implements @[api]. 
 */ 
PIKEFUN int program_implements(program prog, program api) 
  errname Program.implements; 
  optflags OPT_TRY_OPTIMIZE; 
{ 
  RETURN implements(prog, api); 
} 
 
/*! @decl int inherits(program child, program parent) 
 *! @belongs Program 
 *! 
 *! Returns 1 if @[child] has inherited @[parent]. 
 */ 
PIKEFUN int program_inherits(program parent, program child) 
  errname Program.inherits; 
  optflags OPT_TRY_OPTIMIZE; 
{ 
  RETURN low_get_storage(parent, child) != -1; 
} 
 
/*! @decl string defined(program p) 
 *! @belongs Program 
 *! 
 *! Returns a string with filename and linenumber describing where 
 *! the program @[p] was defined. 
 *! 
 *! The returned string is of the format @expr{"filename:linenumber"@}. 
 *! 
 *! If it cannot be determined where the program was defined, @expr{0@} 
 *! (zero) will be returned. 
 */ 
PIKEFUN string program_defined(program p) 
  errname Program.defined; 
  optflags OPT_TRY_OPTIMIZE; 
{ 
  INT32 line; 
  struct pike_string *tmp = low_get_program_line(p, &line); 
 
  pop_n_elems(args); 
 
  if (tmp) { 
    push_string(tmp); 
    if(line >= 1) 
    { 
      push_constant_text(":"); 
      push_int(line); 
      f_add(3); 
    } 
  } 
  else 
    push_int(0); 
} 
 
/*! @decl int(8..8)|int(16..16)|int(32..32) width(string s) 
 *! @belongs String 
 *! 
 *! Returns the width of a string. 
 *! 
 *! Three return values are possible: 
 *! @int 
 *!   @value 8 
 *!     The string @[s] only contains characters <= 255. 
 *!   @value 16 
 *!     The string @[s] only contains characters <= 65535. 
 *!   @value 32 
 *!     The string @[s] contains characters >= 65536. 
 *! @endint 
 */ 
PIKEFUN int(8 .. 8)|int(16 .. 16)|int(32 .. 32) string_width(string s) 
  errname String.width; 
  optflags OPT_TRY_OPTIMIZE; 
{ 
  RETURN 8 * (1 << s->size_shift); 
} 
 
/*! @decl mixed m_delete(object|mapping map, mixed index) 
 *! 
 *! If @[map] is an object that implements @[lfun::_m_delete()], 
 *! that function will be called with @[index] as its single argument. 
 *! 
 *! Otherwise if @[map] is a mapping the entry with index @[index] 
 *! will be removed from @[map] destructively. 
 *! 
 *! If the mapping does not have an entry with index @[index], nothing is done. 
 *! 
 *! @returns 
 *!   The value that was removed will be returned. 
 *! 
 *! @note 
 *!   Note that @[m_delete()] changes @[map] destructively. 
 *! 
 *! @seealso 
 *!   @[mappingp()] 
 */ 
PIKEFUN mixed m_delete(object|mapping map, mixed index) 
  efun; 
  optflags OPT_SIDE_EFFECT; 
{ 
  /*FIXME: Should be 
   *     type function(mapping(1=mixed:2=mixed),1:2)| 
   *     function(object,mixed:mixed); 
   * 
   *     or similar 
   */ 
  if( map->type == T_MAPPING ) 
  { 
    struct svalue s; 
    map_delete_no_free(map->u.mapping, index, &s); 
    pop_n_elems(args); 
    *Pike_sp=s; 
    Pike_sp++; 
    dmalloc_touch_svalue(Pike_sp-1); 
  } 
  else if (map->type == T_OBJECT && map->u.object->prog) 
  { 
    int id = FIND_LFUN(map->u.object->prog, LFUN__M_DELETE); 
 
    if( id == -1 ) 
      SIMPLE_BAD_ARG_ERROR("m_delete", 1, "object containing the _m_delete method"); 
     
    apply_low( map->u.object, id, 1 ); 
    stack_swap(); 
    pop_stack(); 
  } else { 
    SIMPLE_BAD_ARG_ERROR("m_delete", 1, "object|mapping"); 
  } 
} 
 
/*! @decl int get_weak_flag(array|mapping|multiset m) 
 *! 
 *! Returns the weak flag settings for @[m]. It's a combination of 
 *! @[Pike.WEAK_INDICES] and @[Pike.WEAK_VALUES]. 
 */ 
PIKEFUN int get_weak_flag(array m) 
  efun; 
  optflags OPT_EXTERNAL_DEPEND; 
{ 
  RETURN (m->flags & ARRAY_WEAK_FLAG) ? PIKE_WEAK_VALUES : 0; 
} 
 
PIKEFUN int get_weak_flag(mapping m) 
{ 
  RETURN  mapping_get_flags(m) & MAPPING_WEAK; 
} 
 
PIKEFUN int get_weak_flag(multiset m) 
{ 
#ifdef PIKE_NEW_MULTISETS 
  RETURN multiset_get_flags(m) & MULTISET_WEAK; 
#else 
  RETURN (m->ind->flags & (ARRAY_WEAK_FLAG|ARRAY_WEAK_SHRINK)) ? 
        PIKE_WEAK_INDICES : 0; 
#endif 
} 
 
PIKEFUN program __empty_program(int|void line, string|void file) 
  efun; 
  optflags OPT_EXTERNAL_DEPEND; 
{ 
  if (line && line->type != T_INT) 
    SIMPLE_BAD_ARG_ERROR("__empty_program", 1, "int|void"); 
  else if (file && file->type != T_STRING) 
    SIMPLE_BAD_ARG_ERROR("__empty_program", 2, "string|void"); 
  else { 
    struct program *prog = low_allocate_program(); 
    if (file) ext_store_program_line (prog, line->u.integer, file->u.string); 
    RETURN prog; 
  } 
} 
 
/*! @decl string function_name(function f) 
 *! 
 *! Return the name of the function @[f]. 
 *! 
 *! If @[f] is a global function defined in the runtime @expr{0@} 
 *! (zero) will be returned. 
 *! 
 *! @seealso 
 *! @[function_object()] 
 */ 
PIKEFUN string function_name(program|function func) 
  efun; 
  optflags OPT_TRY_OPTIMIZE; 
{ 
  switch(func->type) 
  { 
    default: 
      SIMPLE_BAD_ARG_ERROR("function_name", 1, "function|program"); 
      return; /* NOTREACHED */ 
         
    case PIKE_T_PROGRAM: 
    { 
      struct program *p=func->u.program; 
 
      if(p->parent) 
      { 
        int e; 
        p=p->parent; 
        /* search constants in parent for this 
         * program... 
         */ 
         
        for(e = p->num_identifier_references; e--; ) 
        { 
          struct identifier *id; 
          if (p->identifier_references[e].id_flags & ID_HIDDEN) 
            continue; 
 
          id = ID_FROM_INT(p, e); 
          if (IDENTIFIER_IS_CONSTANT(id->identifier_flags) && 
              is_eq( & PROG_FROM_INT(p, e)->constants[id->func.offset].sval, 
                     func)) 
            REF_RETURN id->name; 
        } 
#ifdef PIKE_DEBUG 
        if (d_flag>5) { 
          fprintf(stderr, 
                  "Failed to find symbol for program 0x%08p\n" 
                  "Parent program info:\n", 
                  func->u.program); 
          dump_program_tables(func->u.program->parent, 0); 
        } 
#endif 
      } 
      break; 
    } 
 
    case PIKE_T_FUNCTION: 
      if(func->subtype == FUNCTION_BUILTIN) break; 
      if(!func->u.object->prog) 
        bad_arg_error("function_name", Pike_sp-args, args, 1, 
                      "function", Pike_sp-args, 
                      "Destructed object.\n"); 
      if(func->u.object->prog == pike_trampoline_program) 
      { 
        struct pike_trampoline *t; 
        t=((struct pike_trampoline *)func->u.object->storage); 
        if(t->frame->current_object->prog) 
          REF_RETURN ID_FROM_INT(t->frame->current_object->prog, 
                                 t->func)->name; 
      } 
       
      REF_RETURN ID_FROM_INT(func->u.object->prog, func->subtype)->name; 
  } 
  pop_n_elems(args); 
  push_int(0); 
} 
 
/*! @decl object function_object(function f) 
 *! 
 *!   Return the object the function @[f] is in. 
 *! 
 *!   If @[f] is a global function defined in the runtime @expr{0@} 
 *!   (zero) will be returned. 
 *! 
 *!   Zero will also be returned if @[f] is a constant in the 
 *!   parent class. In that case @[function_program()] can be 
 *!   used to get the parent program. 
 *! 
 *! @seealso 
 *!   @[function_name()], @[function_program()] 
 */ 
PIKEFUN object function_object(function|program func) 
  efun; 
  optflags OPT_TRY_OPTIMIZE; 
  type function(function:object); 
{ 
  switch(func->type) 
  { 
    case PIKE_T_PROGRAM: 
      break; 
       
    case PIKE_T_FUNCTION: 
      if(func->subtype == FUNCTION_BUILTIN) break; 
      if(func->u.object->prog == pike_trampoline_program) 
      { 
        struct object *o; 
        o=((struct pike_trampoline *)func->u.object->storage)->frame->current_object; 
        add_ref(o); 
        pop_n_elems(args); 
        push_object(o); 
        return; 
      } 
      func->type=T_OBJECT; 
      return; 
 
 
    default: 
      SIMPLE_BAD_ARG_ERROR("function_object",1,"function"); 
  } 
  pop_n_elems(args); 
  push_int(0); 
} 
 
/*! @decl program function_program(function|program f) 
 *! 
 *!   Return the program the function @[f] is in. 
 *! 
 *!   If @[f] is a global function defined in the runtime @expr{0@} 
 *!   (zero) will be returned. 
 *! 
 *! @seealso 
 *!   @[function_name()], @[function_object()] 
 */ 
PIKEFUN program function_program(program|function func) 
  efun; 
  optflags OPT_TRY_OPTIMIZE; 
{ 
  switch(func->type) 
  { 
    case PIKE_T_PROGRAM: 
    { 
      struct program *p; 
      if(!(p=func->u.program->parent)) break; 
      add_ref(p); 
      free_program(func->u.program); 
      func->u.program=p; 
      return; 
    } 
       
    case PIKE_T_FUNCTION: 
      { 
        struct program *p; 
        if(func->subtype == FUNCTION_BUILTIN) break; 
        p = func->u.object->prog; 
        if(p == pike_trampoline_program) 
        { 
          p = ((struct pike_trampoline *)func->u.object->storage)-> 
            frame->current_object->prog; 
        } 
        if (p) { 
          ref_push_program(p); 
          stack_pop_n_elems_keep_top(args); 
          return; 
        } 
      } 
      break; 
 
    default: 
      SIMPLE_BAD_ARG_ERROR("function_program", 1, "function"); 
  } 
  pop_n_elems(args); 
  push_int(0); 
} 
 
 
 
/*! @decl int random(int max) 
 *! 
 *!   This function returns a random number in the range 0 - @[max]-1. 
 *! 
 *! @seealso 
 *!   @[random_seed()] 
 */ 
 
 
PIKEFUN mixed random(object o) 
  efun; 
  optflags OPT_TRY_OPTIMIZE|OPT_EXTERNAL_DEPEND; 
{ 
  apply(o,"_random",0); 
  stack_swap(); 
  pop_stack(); 
} 
 
PIKEFUN int random(int i) 
{ 
  if(i <= 0) RETURN 0; 
  RETURN  my_rand() % i; 
}       
 
PIKEFUN float random(float f) 
{ 
  if(f<=0.0) RETURN 0.0; 
#define N 1048576 
  RETURN f * (my_rand()%N/((float)N)) + 
    f * (my_rand()%N/( ((float)N) * ((float)N) )); 
                    
} 
 
PIKEFUN mixed random(array a) 
{ 
  if(!a->size) 
    SIMPLE_BAD_ARG_ERROR("random", 1, "array with elements in it"); 
  push_svalue(a->item + (my_rand() % a->size)); 
  stack_swap(); 
  pop_stack(); 
} 
 
PIKEFUN mixed random(multiset m) 
{ 
  if(multiset_is_empty (m)) 
    SIMPLE_BAD_ARG_ERROR("random", 1, "multiset with elements in it"); 
#ifdef PIKE_NEW_MULTISETS 
  if (multiset_indval (m)) { 
    ptrdiff_t nodepos = multiset_get_nth (m, my_rand() % multiset_sizeof (m)); 
    push_multiset_index (m, nodepos); 
    push_multiset_value (m, nodepos); 
    sub_msnode_ref (m); 
    f_aggregate (2); 
  } 
  else { 
    push_multiset_index (m, multiset_get_nth (m, my_rand() % multiset_sizeof (m))); 
    sub_msnode_ref (m); 
  } 
#else 
  push_svalue(m->ind->item + (my_rand() % m->ind->size)); 
#endif 
  stack_swap(); 
  pop_stack(); 
} 
 
PIKEFUN array random(mapping m) 
{ 
  struct mapping_data *md=m->data; 
  size_t bucket, count; 
  struct keypair *k; 
   
  if(!m_sizeof(m)) 
    SIMPLE_BAD_ARG_ERROR("random", 1, "mapping with elements in it"); 
   
  /* Find a random, nonempty bucket */ 
  bucket=my_rand() % md->hashsize; 
  while(! md->hash[bucket] ) 
    if(++bucket > (size_t)md->hashsize) 
      bucket=0; 
   
  /* Count entries in bucket */ 
  count=0; 
  for(k=md->hash[bucket];k;k=k->next) count++; 
   
  /* Select a random entry in this bucket */ 
  count = my_rand() % count; 
  k=md->hash[bucket]; 
  while(count-- > 0) k=k->next; 
   
  /* Push result and return */ 
  push_svalue(&k->ind); 
  push_svalue(&k->val); 
  f_aggregate(2); 
  stack_swap(); 
  pop_stack(); 
} 
 
/* 
 * Backtrace handling. 
 */ 
 
/*! @module Pike 
 */ 
 
/*! @class BacktraceFrame 
 */ 
 
PIKECLASS backtrace_frame 
{ 
  PIKEVAR mixed fun; 
  PIKEVAR array args; 
 
  /* These are cleared when filename and lineno has been initialized 
   * from them. */ 
  CVAR struct program *prog;    /* FIXME: Ought to be a private pikevar... */ 
  CVAR PIKE_OPCODE_T *pc; 
 
  /* These two are considered to be uninitialized from prog, pc and 
   * fun as long as lineno == -1. */ 
  CVAR struct pike_string *filename; 
  CVAR INT32 lineno; 
 
  INIT 
  { 
    THIS->fun.type = T_INT; 
    THIS->fun.u.integer = 0; 
    THIS->prog = NULL; 
    THIS->pc = NULL; 
    THIS->lineno = -1; 
    THIS->args = NULL; 
    THIS->filename = NULL; 
  } 
 
  EXIT 
  { 
    if (THIS->prog) { 
      free_program(THIS->prog); 
      THIS->prog = NULL; 
    } 
    if (THIS->args) { 
      free_array(THIS->args); 
      THIS->args = NULL; 
    } 
    if (THIS->filename) { 
      free_string(THIS->filename); 
      THIS->filename = NULL; 
    } 
    THIS->pc = NULL; 
    THIS->lineno = -1; 
    free_svalue(&THIS->fun); 
    THIS->fun.type = T_INT; 
    THIS->fun.u.integer = 0; 
  } 
 
  PIKEFUN int(0..1) _is_type(string t) 
  { 
    INT_TYPE res = (t == findstring("array")); 
    pop_n_elems(args); 
    push_int(res); 
  } 
 
  static void fill_in_file_and_line() 
  { 
    struct pike_string *file; 
    assert (THIS->lineno == -1); 
 
    if (THIS->pc && THIS->prog) { 
      file = low_get_line(THIS->pc, THIS->prog, &THIS->lineno); 
      THIS->pc = NULL; 
    } 
    else if (THIS->fun.type == PIKE_T_FUNCTION) 
      file = low_get_function_line (THIS->fun.u.object, THIS->fun.subtype, 
                                    &THIS->lineno); 
    else if (THIS->prog) 
      file = low_get_program_line (THIS->prog, &THIS->lineno); 
 
    if (!THIS->filename) THIS->filename = file; 
    else free_string (file); 
 
    if (THIS->prog) { 
      free_program(THIS->prog); 
      THIS->prog = NULL; 
    } 
  } 
 
  PIKEFUN string _sprintf(int c, mapping|void opts) 
  { 
    pop_n_elems(args); 
 
    if (c != 'O') { 
      push_undefined (); 
      return; 
    } 
 
    push_text("backtrace_frame("); 
 
    if (THIS->lineno == -1) fill_in_file_and_line(); 
 
    if (THIS->filename) { 
      ref_push_string(THIS->filename); 
      push_text(":"); 
      push_int(THIS->lineno); 
      push_text(", "); 
      f_add(4); 
    } else { 
      push_text("Unknown file, "); 
    } 
    if (THIS->fun.type == PIKE_T_FUNCTION) { 
      if (THIS->fun.u.object->prog) { 
        push_svalue(&THIS->fun); 
        f_function_name(1); 
        push_text("(), "); 
        f_add(2); 
      } else { 
        free_svalue(&THIS->fun); 
        THIS->fun.type = PIKE_T_INT; 
        THIS->fun.u.integer = 0; 
        THIS->fun.subtype = NUMBER_DESTRUCTED; 
        push_text("destructed_function(), ");    
      } 
    } else { 
      push_text("destructed_function(), "); 
    } 
 
    if (THIS->args) { 
      push_text("Args: "); 
      push_int(THIS->args->size); 
      f_add(2); 
    } else { 
      push_text("No args"); 
    } 
    push_text(")"); 
    f_add(5); 
  } 
 
  PIKEFUN int _sizeof() 
  { 
    if (THIS->args) { 
      push_int(THIS->args->size + 3); 
    } else { 
      push_int(3); 
    } 
  } 
 
  PIKEFUN mixed `[](int index, int|void end_or_none) 
  { 
    INT_TYPE end = index; 
    INT32 numargs = 0; 
    INT32 i; 
 
    if (THIS->args) { 
      numargs = THIS->args->size; 
    } 
 
    numargs += 3; 
 
    if (!end_or_none) { 
      if (index < 0) { 
        index_error("pike_frame->`[]", Pike_sp-args, args, NULL, Pike_sp-args, 
                    "Indexing with negative index (%"PRINTPIKEINT"d)\n", index); 
      } else if (index >= numargs) { 
        index_error("pike_frame->`[]", Pike_sp-args, args, NULL, Pike_sp-args, 
                    "Indexing with too large index (%"PRINTPIKEINT"d)\n", index); 
      } 
    } else { 
      if (end_or_none->type != PIKE_T_INT) { 
        SIMPLE_BAD_ARG_ERROR("`[]",2,"int|void"); 
      } 
      end = end_or_none->u.integer; 
    } 
 
    pop_n_elems(args); 
 
    if (end_or_none) { 
      if ((end < 0) || (end < index) || (index >= numargs)) { 
        f_aggregate(0); 
        return; 
      } 
 
      if (end >= numargs) { 
        end = numargs-1; 
      } 
    } 
 
    for (i = index; i <= end; i++) { 
      switch(i) { 
      case 0:   /* Filename */ 
        if (THIS->lineno == -1) fill_in_file_and_line(); 
        if (THIS->filename) { 
          ref_push_string(THIS->filename); 
        } else { 
          push_int(0); 
        } 
        break; 
      case 1:   /* Linenumber */ 
        if (THIS->lineno == -1) fill_in_file_and_line(); 
        push_int(THIS->lineno); 
        break; 
      case 2:   /* Function */ 
        push_svalue(&THIS->fun); 
        break; 
      default:  /* Arguments */ 
        { 
          if ((i > 2) && (THIS->args) && (i-3 < THIS->args->size)) { 
            push_svalue(THIS->args->item + (i - 3)); 
            break; 
          } 
          bad_arg_error("backtrace_frame->`[]", Pike_sp-args, args, 1, 
                        "int(0..)", Pike_sp-args, 
                        "Bad argument 1 to backtrace_frame->`[](): " 
                        "Expected int(0..%d)\n", 
                        numargs + 2); 
        } 
        /* NOT_REACHED */ 
        break; 
      } 
    } 
    if (end_or_none) { 
      f_aggregate(1 + end - index); 
    } 
  } 
 
  PIKEFUN mixed `[]=(int index, mixed value) 
  { 
    INT32 numargs = 0; 
 
    if (THIS->args) { 
      numargs = THIS->args->size; 
    } 
 
    numargs += 3; 
 
    if ((index < -numargs) || (index >= numargs)) { 
        index_error("pike_frame->`[]=", Pike_sp-args, args, NULL, Pike_sp-args, 
                    "Index %"PRINTPIKEINT"d is out of array range 0 - %d,\n", 
                    index, numargs-1); 
    } else if (index < 0) { 
      index += numargs; 
    } 
 
    if (args > 2) { 
      pop_n_elems(args - 2); 
      args = 2; 
    } 
 
    switch(index) { 
    case 0:     /* Filename */ 
      if (THIS->lineno == -1) fill_in_file_and_line(); 
      if (value->type != PIKE_T_STRING) { 
        if ((value->type != PIKE_T_INT) || 
            (value->u.integer)) { 
          SIMPLE_BAD_ARG_ERROR("backtrace_frame->`[]=", 2, 
                               "string|int(0..0)"); 
        } 
        if (THIS->filename) { 
          free_string(THIS->filename); 
          THIS->filename = NULL; 
        } 
      } else { 
        if (THIS->filename) { 
          free_string(THIS->filename); 
          THIS->filename = NULL; 
        } 
        copy_shared_string(THIS->filename, value->u.string); 
      } 
      break; 
 
    case 1:     /* Linenumber */ 
      if (THIS->lineno == -1) fill_in_file_and_line(); 
      if (value->type != PIKE_T_INT) { 
        SIMPLE_BAD_ARG_ERROR("backtrace_frame->`[]=", 2, "int(1..)"); 
      } 
      THIS->lineno = value->u.integer; 
      break; 
 
    case 2:     /* Function */ 
      if (THIS->lineno == -1) fill_in_file_and_line(); 
      assign_svalue(&THIS->fun, value); 
      break; 
    default:    /* Arguments */ 
      assign_svalue(THIS->args->item + index - 3, value); 
      break; 
    } 
    stack_swap(); 
    pop_stack(); 
  } 
 
}; 
 
/*! @endclass 
 */ 
 
/*! @endmodule 
 */ 
 
void low_backtrace(struct Pike_interpreter *i) 
{ 
  struct svalue *stack_top = i->stack_pointer; 
  struct pike_frame *f, *of = 0; 
  int size = 0; 
  struct array *res = NULL; 
 
  for (f = i->frame_pointer; f; f = f->next) { 
    size++; 
  } 
 
  res = allocate_array_no_init(size, 0); 
  push_array(res); 
 
  for (f = i->frame_pointer; f && size; f = (of = f)->next) { 
    struct object *o = low_clone(backtrace_frame_program); 
    struct backtrace_frame_struct *bf; 
    struct identifier *function = NULL; 
 
    call_c_initializers(o); 
 
    size--; 
 
    res->item[size].u.object = o; 
    res->item[size].type = PIKE_T_OBJECT; 
    res->item[size].subtype = 0; 
 
    bf = OBJ2_BACKTRACE_FRAME(o); 
 
    if ((bf->prog = f->context.prog)) { 
      add_ref(bf->prog); 
      if (f->pc) { 
        /* f->pc is the return address (i.e the next opcode) when 
         * inside a call, otherwise it points to the current 
         * opcode. */ 
        if (of) 
          bf->pc = f->pc - 1; 
        else 
          bf->pc = f->pc; 
      } 
    } 
 
    if ((bf->fun.u.object = f->current_object) && 
        (bf->fun.u.object->prog)) { 
      add_ref(bf->fun.u.object); 
      bf->fun.subtype = f->fun; 
      bf->fun.type = PIKE_T_FUNCTION; 
      function = ID_FROM_INT(f->current_object->prog, f->fun); 
    } else { 
      bf->fun.u.integer = 0; 
      bf->fun.subtype = NUMBER_DESTRUCTED; 
      bf->fun.type = PIKE_T_INT; 
    } 
 
    if (f->locals) { 
      INT32 numargs = DO_NOT_WARN((INT32) MINIMUM(f->num_args, 
                                                  stack_top - f->locals)); 
      INT32 varargs = 0; 
 
      if(of) { 
        /* f->num_args can be too large, so this is necessary for some 
         * reason. I don't know why. /mast */ 
        numargs = DO_NOT_WARN((INT32)MINIMUM(f->num_args,of->locals - f->locals)); 
      } 
 
      numargs = MAXIMUM(numargs, 0); 
 
      /* Handle varargs... */ 
      if (function && (function->identifier_flags & IDENTIFIER_VARARGS) && 
          (f->locals + numargs < stack_top) && 
          (f->locals[numargs].type == T_ARRAY)) { 
        varargs = f->locals[numargs].u.array->size; 
      } 
 
      if (numargs + varargs) { 
        bf->args = allocate_array_no_init(numargs + varargs, 0); 
        assign_svalues_no_free(bf->args->item, f->locals, numargs, BIT_MIXED); 
        if (varargs) { 
          assign_svalues_no_free(bf->args->item + numargs, 
                                 f->locals[numargs].u.array->item, 
                                 varargs, BIT_MIXED); 
        } 
      } 
    } 
  } 
  /* NOTE: res has already been pushed on the stack. */ 
} 
 
/*! @decl array(Pike.BacktraceFrame) backtrace() 
 *! 
 *!   FIXME: This documentation is not up to date! 
 *! 
 *!   Get a description of the current call stack. 
 *! 
 *!   The description is returned as an array with one entry for each call 
 *!   frame on the stack. 
 *! 
 *!   Each entry has this format: 
 *!   @array 
 *!     @elem string file 
 *!       A string with the filename if known, else zero. 
 *!     @elem int line 
 *!       An integer containing the linenumber if known, else zero. 
 *!     @elem function fun 
 *!       The function that was called at this level. 
 *!     @elem mixed|void ... args 
 *!       The arguments that the function was called with. 
 *!   @endarray 
 *! 
 *!   The current call frame will be last in the array. 
 *! 
 *! @note 
 *!   Please note that the frame order may be reversed in a later version 
 *!   (than 7.1) of Pike to accommodate for deferred backtraces. 
 *! 
 *!   Note that the arguments reported in the backtrace are the current 
 *!   values of the variables, and not the ones that were at call-time. 
 *!   This can be used to hide sensitive information from backtraces 
 *!   (eg passwords). 
 *! 
 *! @seealso 
 *!   @[catch()], @[throw()] 
 */ 
PMOD_EXPORT 
PIKEFUN array(mixed) backtrace() 
     efun; 
     optflags OPT_EXTERNAL_DEPEND; 
{ 
  low_backtrace(& Pike_interpreter); 
} 
 
#define INITIAL_BUF_LEN 4096 
 
/*! @module String 
 */ 
 
/*! @class Buffer 
 *!    A buffer, used for building strings. It's 
 *!    conceptually similar to a string, but you can only @[add] 
 *!    strings to it, and you can only @[get] the value from it once. 
 *! 
 *!    There is a reason for those seemingly rather odd limitations, 
 *!    it makes it possible to do some optimizations that really speed 
 *!    things up. 
 *! 
 *!    You do not need to use this class unless you add very many 
 *!    strings together, or very large strings. 
 *! 
 *! @example 
 *!    For the fastest possible operation, write your code like this: 
 *! 
 *! @code 
 *! String.Buffer b = String.Buffer( ); 
 *! 
 *! function add = b->add; 
 *! 
 *! .. call add several times in code ... 
 *! 
 *! string result = b->get(); // also clears the buffer 
 *! @endcode 
 */ 
PIKECLASS Buffer 
{ 
  CVAR struct string_builder str; 
  CVAR int initial; 
   
  void f_Buffer_get_copy( INT32 args ); 
  void f_Buffer_get( INT32 args ); 
  void f_Buffer_add( INT32 args ); 
 
 
  /*! @decl void create(int initial_size) 
   *! 
   *!   Initializes a new buffer. 
   *! 
   *!   If no @[initial_size] is specified, 256 is used. If you 
   *!   know approximately how big the buffer will be, you can optimize 
   *!   the operation of @[add()] (slightly) by passing the size to this 
   *!   function. 
   */ 
  PIKEFUN void create( int|void size ) 
  { 
    struct Buffer_struct *str = THIS; 
    if( args ) 
      str->initial = MAXIMUM( size->u.integer, 512 ); 
    else 
    { 
      str->initial = 256; 
      push_int(0); 
    } 
  } 
 
  PIKEFUN string _sprintf( int flag, mapping flags ) 
  { 
    switch( flag ) 
    { 
      case 'O': 
        { 
          struct pike_string *res; 
          struct Buffer_struct *str = THIS; 
          push_text( "Buffer(%d /* %d */)" ); 
          if( str->str.s ) 
          { 
            push_int(str->str.s->len); 
            push_int(str->str.malloced); 
          } 
          else 
          { 
            push_int( 0 ); 
            push_int( 0 ); 
          } 
          f_sprintf( 3 ); 
          dmalloc_touch_svalue(Pike_sp-1); 
          res = Pike_sp[-1].u.string; 
          Pike_sp--; 
          RETURN res; 
        } 
 
      case 's': 
        { 
          pop_n_elems( args ); 
          if( Pike_fp->current_object->refs != 1 ) 
            f_Buffer_get_copy( 0 ); 
          else 
            f_Buffer_get( 0 ); 
        } 
        return; 
         
      case 't': 
        RETURN make_shared_binary_string("Buffer",6); 
    } 
    pop_n_elems( args ); 
    push_int( 0 ); 
    Pike_sp[-1].subtype = 1; 
  } 
 
  PIKEFUN mixed cast( string type ) 
  { 
    struct pike_string *string_t; 
    struct pike_string *int_t; 
    MAKE_CONST_STRING( string_t, "string" ); 
    MAKE_CONST_STRING( int_t, "int" ); 
 
    if( type == string_t ) 
    { 
      pop_n_elems( args ); 
      if( Pike_fp->current_object->refs != 1 ) 
        f_Buffer_get_copy( 0 ); 
      else 
        f_Buffer_get( 0 ); 
      return; 
    } 
 
    if( type == int_t ) 
    { 
      struct Buffer_struct *str = THIS; 
      pop_stack(); 
      if( Pike_fp->current_object->refs != 1 ) 
        f_Buffer_get_copy( 0 ); 
      else 
        f_Buffer_get( 0 ); 
      o_cast_to_int( ); 
      return; 
    } 
    Pike_error("Cannot cast to %s\n", type->str ); 
  } 
   
  PIKEFUN object `+( string what ) 
  { 
    struct Buffer_struct *str = THIS, *str2; 
    struct object *res = fast_clone_object( Buffer_program ); 
    str2 = OBJ2_BUFFER( res ); 
    str2->initial = str->initial; 
    if( str->str.s ) 
      init_string_builder_copy (&str2->str, &str->str); 
    apply( res, "add", 1 ); 
    RETURN res; 
  } 
 
  PIKEFUN object `+=( string what ) 
  { 
    f_Buffer_add( 1 ); 
    REF_RETURN Pike_fp->current_object; 
  } 
 
  /*! @decl int add(string ... data) 
   *! 
   *!   Adds @[data] to the buffer. Returns the size of the buffer. 
   *! 
   */ 
  PIKEFUN int add( string ... arg1 ) 
  { 
    struct Buffer_struct *str = THIS; 
    int j; 
 
    if (!str->str.s && args) { 
      int sum = 0; 
      int shift = 0; 
      for (j=0; j < args; j++) { 
        struct pike_string *a = Pike_sp[j-args].u.string; 
        sum += a->len; 
        shift |= a->size_shift; 
      } 
      if (sum < str->initial) { 
        sum = str->initial; 
      } 
      shift = shift & ~(shift >> 1); 
      init_string_builder_alloc(&str->str, sum, shift); 
      /* We know it will be a string that really is this wide. */ 
      str->str.known_shift = shift; 
    } 
 
    for( j = 0; j<args; j++ ) 
    { 
      struct pike_string *a = Pike_sp[j-args].u.string; 
      string_builder_shared_strcat( &str->str, a ); 
    } 
 
    if (str->str.s) { 
      RETURN str->str.s->len; 
    } else { 
      RETURN 0; 
    } 
  } 
 
  /*! @decl string get_copy() 
   *! 
   *!   Get the data from the buffer. Significantly slower than @[get], 
   *!   but does not clear the buffer. 
   */ 
  PIKEFUN string get_copy() 
  { 
    struct pike_string *str = THIS->str.s; 
    if( str ) 
    { 
      ptrdiff_t len = str->len; 
      if( len > 0 ) 
      { 
        char *d = (char *)str->str; 
        switch( str->size_shift ) 
        { 
          case 0: 
            RETURN make_shared_binary_string0((p_wchar0 *)d,len); 
            break; 
          case 1: 
            RETURN make_shared_binary_string1((p_wchar1 *)d,len>>1); 
            break; 
          case 2: 
            RETURN make_shared_binary_string2((p_wchar2 *)d,len>>2); 
            break; 
        } 
      } 
    } 
    push_text(""); 
    return; 
  } 
 
  /*! @decl string get() 
   *! 
   *!   Get the data from the buffer. 
   *! 
   *! @note 
   *!   This will clear the data in the buffer 
   */ 
  PIKEFUN string get( ) 
  { 
    struct Buffer_struct *str = THIS; 
    if( str->str.s ) 
    { 
      struct pike_string *s = finish_string_builder( &str->str ); 
      str->str.malloced = 0; 
      str->str.s = 0; 
      RETURN s; 
    } 
    pop_n_elems(args); 
    push_text(""); 
    return; 
  } 
 
  /*! @decl int _sizeof() 
   *! 
   *!   Returns the size of the buffer. 
   */ 
  PIKEFUN int _sizeof() 
  { 
    struct Buffer_struct *str = THIS; 
    RETURN str->str.s ? str->str.s->len : 0; 
  } 
 
  INIT 
    { 
      struct Buffer_struct *str = THIS; 
      MEMSET( str, 0, sizeof( *str ) ); 
    } 
 
  EXIT 
    { 
      struct Buffer_struct *str = THIS; 
      if( str->str.s ) 
        free_string_builder( &str->str ); 
    } 
} 
 
/*! @endclass 
 */ 
 
/*! @class Replace 
 */ 
PIKECLASS multi_string_replace 
{ 
  CVAR struct tupel 
  { 
    int prefix; 
    struct pike_string *ind; 
    struct pike_string *val; 
  } *v; 
  CVAR size_t v_sz; 
  CVAR size_t sz; 
  CVAR INT32 set_start[256]; 
  CVAR INT32 set_end[256]; 
 
  static int replace_sortfun(struct tupel *a,struct tupel *b) 
  { 
    return DO_NOT_WARN((int)my_quick_strcmp(a->ind, b->ind)); 
  } 
 
  PIKEFUN void create(array(string)|void from_, array(string)|void to_) 
  { 
    int i; 
    struct array *from; 
    struct array *to; 
    if (!args) { 
      push_int(0); 
      return; 
    } 
    if (!from_ || !to_) { 
      Pike_error("Bad number of arguments to create().\n"); 
    } 
    from = from_->u.array; 
    to = to_->u.array; 
    if (from->size != to->size) { 
      Pike_error("Replace must have equal-sized from and to arrays.\n"); 
    } 
    for (i = 0; i < (int)from->size; i++) { 
      if (from->item[i].type != PIKE_T_STRING) { 
        Pike_error("Replace: from array is not an array(string).\n"); 
      } 
      if (to->item[i].type != PIKE_T_STRING) { 
        Pike_error("Replace: to array is not an array(string).\n"); 
      } 
    } 
    if (THIS->v) { 
      for (i = 0; i < (int)THIS->v_sz; i++) { 
        if (!THIS->v[i].ind) break; 
        free_string(THIS->v[i].ind); 
        THIS->v[i].ind = NULL; 
        free_string(THIS->v[i].val); 
        THIS->v[i].val = NULL;   
      } 
    } 
    if (THIS->v && (THIS->v_sz < (size_t)from->size)) {       
      free(THIS->v); 
      THIS->v = NULL; 
      THIS->v_sz = 0; 
    } 
    if (!THIS->v) { 
      THIS->v = (struct tupel *)xalloc(sizeof(struct tupel) * from->size); 
      THIS->v_sz = from->size; 
    } 
    for (i = 0; i < (int)from->size; i++) { 
      copy_shared_string(THIS->v[i].ind, from->item[i].u.string); 
      copy_shared_string(THIS->v[i].val, to->item[i].u.string); 
      THIS->v[i].prefix = -2;   /* Uninitialized */ 
    } 
    THIS->sz = from->size; 
    fsort((char *)THIS->v, from->size, sizeof(struct tupel), 
          (fsortfun)replace_sortfun); 
 
    MEMSET(THIS->set_start, 0, sizeof(INT32)*256); 
    MEMSET(THIS->set_end, 0, sizeof(INT32)*256); 
 
    for (i = 0; i < (int)from->size; i++) { 
      INT32 x = index_shared_string(THIS->v[from->size-1-i].ind, 0); 
      if ((x >= 0) && (x < 256)) 
        THIS->set_start[x] = from->size-1-i; 
      x = index_shared_string(THIS->v[i].ind, 0); 
      if ((x >= 0) && (x < 256)) 
        THIS->set_end[x] = i+1; 
    } 
    pop_n_elems(args); 
    push_int(0); 
  } 
 
  static int find_longest_prefix(char *str, 
                                 ptrdiff_t len, 
                                 int size_shift, 
                                 struct tupel *v, 
                                 INT32 a, 
                                 INT32 b) 
  { 
    INT32 c,match=-1; 
    ptrdiff_t tmp; 
 
    while(a<b) 
    { 
      c=(a+b)/2; 
       
      tmp=generic_quick_binary_strcmp(v[c].ind->str, 
                                      v[c].ind->len, 
                                      v[c].ind->size_shift, 
                                      str, 
                                      MINIMUM(len,v[c].ind->len), 
                                      size_shift); 
      if(tmp<0) 
      { 
        INT32 match2=find_longest_prefix(str, 
                                         len, 
                                         size_shift, 
                                         v, 
                                         c+1, 
                                         b); 
        if(match2!=-1) return match2; 
 
        while(1) 
        { 
          if(v[c].prefix==-2) 
          { 
            v[c].prefix=find_longest_prefix(v[c].ind->str, 
                                            v[c].ind->len, 
                                            v[c].ind->size_shift, 
                                            v, 
                                            0 /* can this be optimized? */, 
                                            c); 
          } 
          c=v[c].prefix; 
          if(c<a || c<match) return match; 
 
          if(!generic_quick_binary_strcmp(v[c].ind->str, 
                                          v[c].ind->len, 
                                          v[c].ind->size_shift, 
                                          str, 
                                          MINIMUM(len,v[c].ind->len), 
                                          size_shift)) 
            return c; 
        } 
      } 
      else if(tmp>0) 
      { 
        b=c; 
      } 
      else 
      { 
        a=c+1; /* There might still be a better match... */ 
        match=c; 
      } 
    } 
    return match; 
  } 
 
  PIKEFUN string `()(string str) 
  { 
    struct string_builder ret; 
    ptrdiff_t length = str->len; 
    ptrdiff_t s; 
    int *set_start = THIS->set_start; 
    int *set_end = THIS->set_end; 
    struct tupel *v = THIS->v; 
    int num = THIS->sz; 
 
    if (!num) { 
      add_ref(str); 
      RETURN str; 
    } 
 
    init_string_builder(&ret,str->size_shift); 
 
    for(s=0;length > 0;) 
    { 
      INT32 a,b; 
      ptrdiff_t ch; 
 
      ch = index_shared_string(str, s); 
      if((ch >= 0) && (ch < 256)) 
        b = set_end[ch]; 
      else 
        b = num; 
 
      if(b) 
      { 
        if((ch >= 0) && (ch < 256)) 
          a = set_start[ch]; 
        else 
          a = 0; 
 
        a = find_longest_prefix(str->str+(s << str->size_shift), 
                                length, 
                                str->size_shift, 
                                v, a, b); 
 
        if(a!=-1) 
        { 
          ch = v[a].ind->len; 
          if(!ch) ch=1; 
          s += ch; 
          length -= ch; 
          string_builder_shared_strcat(&ret, v[a].val); 
          continue; 
        } 
      } 
      string_builder_putchar(&ret, 
                             DO_NOT_WARN((INT32)ch)); 
      s++; 
      length--; 
    } 
 
    RETURN finish_string_builder(&ret); 
  } 
 
  PIKEFUN array(string) _encode() 
  { 
    size_t i; 
    for (i=0; i < THIS->sz; i++) { 
      ref_push_string(THIS->v[i].ind); 
    } 
    f_aggregate(DO_NOT_WARN((INT32)THIS->sz)); 
    for (i=0; i < THIS->sz; i++) { 
      ref_push_string(THIS->v[i].val); 
    } 
    f_aggregate(DO_NOT_WARN((INT32)THIS->sz)); 
    f_aggregate(2); 
  } 
 
  PIKEFUN void _decode(array(array(string)) encoded) 
  { 
    INT32 i; 
 
    for (i=0; i < encoded->size; i++) { 
      push_svalue(encoded->item + i); 
      stack_swap(); 
    } 
    pop_stack(); 
 
    f_multi_string_replace_create(i); 
  } 
 
  INIT 
  { 
    THIS->v = NULL; 
    THIS->v_sz = 0; 
    THIS->sz = 0; 
  } 
 
  EXIT 
  { 
    if (THIS->v) { 
      int i; 
      for (i = 0; i < (int)THIS->v_sz; i++) { 
        if (!THIS->v[i].ind) break; 
        free_string(THIS->v[i].ind); 
        THIS->v[i].ind = NULL; 
        free_string(THIS->v[i].val); 
        THIS->v[i].val = NULL;   
      } 
      free(THIS->v); 
    } 
    THIS->v = NULL; 
    THIS->v_sz = 0; 
    THIS->sz = 0; 
  } 
} 
 
/*! @endclass 
 */ 
 
/*! @class SingleReplace 
 */ 
PIKECLASS single_string_replace 
{ 
  CVAR SearchMojt mojt; 
  CVAR struct pike_string *del; 
  CVAR struct pike_string *to; 
 
  INIT 
  { 
    THIS->mojt.vtab = NULL; 
    THIS->mojt.data = NULL; 
    THIS->del = NULL; 
    THIS->to = NULL; 
  } 
 
  EXIT 
  { 
    if (THIS->mojt.vtab) { 
      THIS->mojt.vtab->freeme(THIS->mojt.data); 
      THIS->mojt.vtab = NULL; 
      THIS->mojt.data = NULL; 
    } 
    if (THIS->del) { 
      free_string(THIS->del); 
      THIS->del = NULL; 
    } 
    if (THIS->to) { 
      free_string(THIS->to); 
      THIS->to = NULL; 
    } 
  } 
 
  PIKEFUN void create(string|void del_, string|void to_) 
  { 
    struct pike_string *del; 
    struct pike_string *to; 
 
    /* Clean up... */ 
    exit_single_string_replace_struct(); 
     
    if (!del_) return; 
 
    if (!to_) { 
      SIMPLE_BAD_ARG_ERROR("String.SingleReplace->create", 2, "string"); 
    } 
 
    if (del_->u.string == to_->u.string) { 
      /* No-op... */ 
      return; 
    } 
 
    copy_shared_string(THIS->del, del = del_->u.string); 
    copy_shared_string(THIS->to, to = to_->u.string); 
 
    if (del->len) { 
      THIS->mojt = simple_compile_memsearcher(del); 
    } 
  } 
 
  /*** replace function ***/ 
  typedef char *(* replace_searchfunc)(void *,void *,size_t); 
  PIKEFUN string `()(string str) 
  { 
    int shift; 
    struct pike_string *del = THIS->del; 
    struct pike_string *to = THIS->to; 
    struct pike_string *ret = NULL; 
 
    if (!str->len || !del || !to) { 
      /* The result is already on the stack in the correct place... */ 
      return; 
    } 
 
    shift = MAXIMUM(str->size_shift, to->size_shift); 
 
    if (!del->len) { 
      int e, pos; 
      ret = begin_wide_shared_string(str->len + to->len * (str->len-1), 
                                     shift); 
      low_set_index(ret, 0, index_shared_string(str, 0)); 
      for(pos=e=1;e<str->len;e++) 
      { 
        pike_string_cpy(MKPCHARP_STR_OFF(ret,pos), to); 
        pos+=to->len; 
        low_set_index(ret,pos++,index_shared_string(str,e)); 
      } 
    } else { 
      char *s, *end, *tmp; 
      replace_searchfunc f = (replace_searchfunc)0; 
      void *mojt_data = THIS->mojt.data; 
      PCHARP r; 
 
      end = str->str+(str->len<<str->size_shift); 
 
      switch(str->size_shift) 
      { 
      case 0: f = (replace_searchfunc)THIS->mojt.vtab->func0; break; 
      case 1: f = (replace_searchfunc)THIS->mojt.vtab->func1; break; 
      case 2: f = (replace_searchfunc)THIS->mojt.vtab->func2; break; 
#ifdef PIKE_DEBUG 
      default: Pike_fatal("Illegal shift.\n"); 
#endif 
      } 
 
      if(del->len == to->len) 
      { 
        ret = begin_wide_shared_string(str->len, shift); 
      } else { 
        INT32 delimiters = 0; 
 
        s = str->str; 
 
        while((s = f(mojt_data, s, (end-s)>>str->size_shift))) 
        { 
          delimiters++; 
          s += del->len << str->size_shift; 
        } 
 
        if (!delimiters) { 
          /* The result is already on the stack in the correct place... */ 
          return; 
        } 
 
        ret = begin_wide_shared_string(str->len + 
                                       (to->len-del->len)*delimiters, shift); 
      } 
 
      s = str->str; 
      r = MKPCHARP_STR(ret); 
 
      while((tmp = f(mojt_data, s, (end-s)>>str->size_shift))) 
      { 
#ifdef PIKE_DEBUG 
        if(tmp + (del->len << str->size_shift) > end) 
          Pike_fatal("generic_memory_search found a match beyond end of string!\n"); 
#endif 
        generic_memcpy(r,MKPCHARP(s,str->size_shift),(tmp-s)>>str->size_shift); 
        INC_PCHARP(r,(tmp-s)>>str->size_shift); 
        pike_string_cpy(r,to); 
        INC_PCHARP(r,to->len); 
        s=tmp+(del->len << str->size_shift); 
      } 
      generic_memcpy(r,MKPCHARP(s,str->size_shift),(end-s)>>str->size_shift); 
    } 
    RETURN end_shared_string(ret); 
  } 
 
  PIKEFUN array(string) _encode() 
  { 
    if (THIS->del) { 
      ref_push_string(THIS->del); 
      ref_push_string(THIS->to); 
      f_aggregate(2); 
    } else { 
      push_int(0); 
    } 
  } 
 
  PIKEFUN void _decode(array(string)|int(0..0) encoded_) 
  {     
    INT32 i = 0; 
    if (encoded_->type == PIKE_T_ARRAY) { 
      struct array *encoded = encoded_->u.array; 
 
      for (i=0; i < encoded->size; i++) { 
        push_svalue(encoded->item + i); 
        stack_swap(); 
      } 
    } 
    pop_stack(); 
       
    f_single_string_replace_create(i); 
  } 
} 
 
/*! @endclass 
 */ 
 
/*! @class Bootstring 
 *! 
 *! This class implements the "Bootstring" string transcoder described in 
 *! @url{http://www.ietf.org/internet-drafts/draft-ietf-idn-punycode-03.txt@}. 
 */ 
PIKECLASS bootstring 
{ 
  CVAR INT_TYPE base, tmin, tmax, skew, damp; 
  CVAR INT_TYPE initial_bias, initial_n; 
  CVAR p_wchar2 delim; 
  PIKEVAR string digits flags ID_STATIC|ID_PRIVATE; 
 
  static INT_TYPE bootstring_cp_to_digit(p_wchar2 ch) 
  { 
    ptrdiff_t digit = THIS->digits->len; 
    PCHARP digits = MKPCHARP_STR( THIS->digits ); 
    while (digit>=0) 
      if (INDEX_PCHARP( digits, digit ) == ch) 
        return digit; 
      else 
        --digit; 
    return -1; 
  } 
 
  static INT_TYPE bootstring_adapt(INT_TYPE delta, INT_TYPE numpoints, 
                                   int firsttime) 
  { 
    struct bootstring_struct *bs = THIS; 
    INT_TYPE k = 0, b = bs->base; 
    INT_TYPE a = b - bs->tmin; 
    INT_TYPE limit = (a * bs->tmax) >> 1; 
    if (firsttime) 
      delta /= bs->damp; 
    else 
      delta >>= 1; 
    delta += delta / numpoints; 
    while (delta > limit) { 
      delta /= a; 
      k += b; 
    } 
    return k + (a + 1)*delta / (delta + bs->skew); 
  } 
 
  /*! @decl string decode(string s) 
   *! 
   *! Decodes a Bootstring encoded string of "basic" code points back 
   *! to the original string space. 
   */ 
  PIKEFUN string decode(string s) 
  { 
    struct bootstring_struct *bs = THIS; 
    INT_TYPE n = bs->initial_n; 
    INT_TYPE i = 0; 
    INT_TYPE bias = bs->initial_bias; 
    ptrdiff_t pos, input_left; 
    PCHARP input; 
    struct string_builder output; 
    init_string_builder( &output,0 ); 
    input = MKPCHARP_STR( s ); 
    input_left = s->len; 
    for (pos = input_left-1; pos > 0; --pos) 
      if (INDEX_PCHARP( input, pos ) == bs->delim) { 
        string_builder_append( &output, input, pos ); 
        INC_PCHARP( input, pos+1 ); 
        input_left -= pos+1; 
        break; 
      } 
 
    while (input_left > 0) { 
      INT_TYPE oldi = i; 
      INT_TYPE w = 1; 
      INT_TYPE k; 
      for (k=bs->base; ; k+=bs->base) { 
        INT_TYPE digit, t; 
        if (input_left < 1 || 
            (digit = bootstring_cp_to_digit( EXTRACT_PCHARP( input ) )) < 0) { 
          free_string_builder( &output ); 
          Pike_error( "Invalid variable-length integer.\n" ); 
        } 
        INC_PCHARP( input, 1 ); 
        --input_left; 
        i += digit * w; /* fail on overflow... */ 
        if (k <= bias + bs->tmin) 
          t = bs->tmin; 
        else if (k >= bias + bs->tmax) 
          t = bs->tmax; 
        else 
          t = k - bias; 
        if (digit < t) break; 
        w *= (bs->base - t); 
      } 
      bias = bootstring_adapt( i - oldi, output.s->len+1, !oldi ); 
      n += i / (output.s->len+1); 
      i %= output.s->len+1; 
      string_builder_putchar( &output, n ); 
      if (i != output.s->len-1) 
        switch (output.s->size_shift) { 
        case 0: 
          { 
            p_wchar0 *s = STR0(output.s); 
            INT_TYPE p = output.s->len; 
            while (--p>i) 
              s[p] = s[p-1]; 
            s[p] = n; 
          } 
          break; 
        case 1: 
          { 
            p_wchar1 *s = STR1(output.s); 
            INT_TYPE p = output.s->len; 
            while (--p>i) 
              s[p] = s[p-1]; 
            s[p] = n; 
          } 
          break; 
        case 2: 
          { 
            p_wchar2 *s = STR2(output.s); 
            INT_TYPE p = output.s->len; 
            while (--p>i) 
              s[p] = s[p-1]; 
            s[p] = n; 
          } 
          break; 
        default: 
          Pike_fatal("Illegal shift size!\n"); 
        } 
      i++; 
    } 
 
    RETURN finish_string_builder( &output ); 
  } 
 
  /*! @decl string encode(string s) 
   *! 
   *! Encodes a string using Bootstring encoding into a string constisting 
   *! only of "basic" code points (< initial_n). 
   */ 
  PIKEFUN string encode(string s) 
  { 
    struct bootstring_struct *bs = THIS; 
    INT_TYPE n = bs->initial_n; 
    INT_TYPE delta = 0; 
    INT_TYPE bias = bs->initial_bias; 
    INT_TYPE c, h, b = 0; 
    ptrdiff_t pos, input_left; 
    PCHARP input; 
    struct string_builder output; 
    init_string_builder( &output,0 ); 
    input = MKPCHARP_STR( s ); 
    input_left = s->len; 
    for (pos=0; pos<input_left; pos++) 
      if ((c = INDEX_PCHARP( input, pos )) < n) { 
        string_builder_putchar( &output, c ); 
        b++; 
      } 
    if ((h = b)) 
      string_builder_putchar( &output, bs->delim ); 
    while (h < input_left) { 
      INT_TYPE m = -1; 
      for (pos=0; pos<input_left; pos++) 
        if ((c = INDEX_PCHARP( input, pos )) >= n && 
            (m < 0 || c < m)) 
          m = c; 
      delta = delta + (m - n) * (h + 1); /* fail on overflow... */ 
      n = m; 
      for (pos=0; pos<input_left; pos++) 
        if ((c = INDEX_PCHARP( input, pos )) < n) 
          delta++; 
        else if (c == n) { 
          INT_TYPE k, q = delta; 
          for (k=bs->base; ; k+=bs->base) { 
            INT_TYPE t, bt; 
            if (k <= bias + bs->tmin) 
              t = bs->tmin; 
            else if(k >= bias + bs->tmax) 
              t = bs->tmax; 
            else 
              t = k-bias; 
            if (q < t) 
              break; 
            bt = bs->base - t; 
            string_builder_putchar( &output, 
                                    index_shared_string( bs->digits, 
                                                         t + (q-t)%bt ) ); 
            q = (q-t) / bt; 
          } 
          string_builder_putchar( &output, 
                                  index_shared_string( bs->digits, q ) ); 
          bias = bootstring_adapt( delta, h+1, h==b ); 
          delta = 0; 
          h++; 
        } 
      delta++; 
      n++; 
    } 
 
    RETURN finish_string_builder( &output ); 
  } 
 
  /*! @decl void create(int base, int tmin, int tmax, int skew,         @ 
   *!                   int damp, int initial_bias, int initial_n,      @ 
   *!                   int delim, string digits) 
   *! 
   *! Creates a Bootstring transcoder instance using the specified parameters. 
   *! 
   *! @param base 
   *!   The base used by the variable-length integers. 
   *! @param tmin 
   *!   The minimum threshold digit value for the variable-length integers. 
   *!   Must be >=0 and <= tmax. 
   *! @param tmax 
   *!   The maximum threshold digit value for the variable-length integers. 
   *!   Must be <= base-1. 
   *! @param skew 
   *!   The skew term for the bias adapation.  Must be >= 1. 
   *! @param damp 
   *!   The damping factor for the bias adaption.  Must be >= 2. 
   *! @param initial_bias 
   *!   The initial bias for the variable-length integer thresholding. 
   *!   initial_bias % base must be <= base - tmin. 
   *! @param initial_n 
   *!   The first code point outside the "basic" set of code points. 
   *! @param delim 
   *!   The "basic" code point used as the delimiter. 
   *! @param digits 
   *!   The "basic" code points used as digits.  The length of the string 
   *!   should be the same as the base parameter. 
   */ 
  PIKEFUN void create( int base, int tmin, int tmax, 
                       int skew, int damp,  
                       int initial_bias, int initial_n, 
                       int delim, string digits ) 
    flags ID_STATIC; 
  { 
    struct bootstring_struct *bs = THIS; 
    if (base<2) 
      Pike_error("Bogus base\n"); 
    if (tmin<0 || tmax<tmin || base-1<tmax) 
      Pike_error("Parameters violate 0 <= tmin <= tmax <= base-1\n"); 
    if (skew < 1) 
      Pike_error("Parameters violate skew >= 1\n"); 
    if (damp < 2) 
      Pike_error("Parameters violate damp >= 2\n"); 
    if (initial_bias%base > base-tmin) 
      Pike_error("Parameters violate initial_bias%%base <= base-tmin\n"); 
    if (digits->len != base) 
      Pike_error("Length of digits string does not match base.\n"); 
    bs->base = base;  bs->tmin = tmin;  bs->tmax = tmax; 
    bs->skew = skew;  bs->damp = damp; 
    bs->initial_bias = initial_bias;  bs->initial_n = initial_n; 
    bs->delim = delim; 
    if (bs->digits) { 
      free_string( bs->digits ); 
      bs->digits = NULL; 
    } 
    copy_shared_string( bs->digits, digits ); 
  } 
 
} 
 
/*! @endclass 
 */ 
 
/*! @endmodule 
 */ 
 
/*! @module System 
 */ 
 
/*! @class Time 
 *! 
 *! The current time as a structure containing a sec and a usec 
 *! member. 
 */ 
PIKECLASS Time 
{ 
  CVAR int hard_update; 
   
  /*! @decl int sec; 
   *! @decl int usec; 
   *! 
   *!   The number of seconds and microseconds since the epoch and the 
   *!   last whole second, respectively. (See also @[predef::time()]) 
   *! 
   *!   Please note that these variables will continually update when 
   *!   they are requested, there is no need to create new Time() 
   *!   objects. 
   */ 
 
  /*! @decl int usec_full; 
   *! 
   *!   The number of microseconds since the epoch. Please note that 
   *!   pike has to have been compiled with bignum support for this 
   *!   variable to contain sensible values. 
   */ 
 
  PIKEFUN int `->( string x ) 
  { 
    extern struct timeval current_time; 
    struct pike_string *usec; 
    struct pike_string *sec; 
 
    MAKE_CONST_STRING( sec, "sec" ); 
    MAKE_CONST_STRING( usec, "usec" ); 
 
    if( !x ) 
      RETURN 0; 
 
    if( THIS->hard_update ) 
      GETTIMEOFDAY( &current_time ); 
 
    if( x == usec ) 
      RETURN current_time.tv_usec; 
    if( x == sec ) 
      RETURN current_time.tv_sec; 
 
#ifdef AUTO_BIGNUM 
    pop_stack(); 
    push_int( current_time.tv_sec ); 
    push_int( 1000000 ); 
    f_multiply( 2 ); 
    push_int( current_time.tv_usec ); 
    f_add( 2 ); 
    return; 
#else 
    RETURN (current_time.tv_sec * 1000000 + current_time.tv_usec); 
#endif 
  } 
   
  PIKEFUN int `[]( string x ) 
  { 
    f_Time_cq__backtick_2D_3E( args ); 
  } 
 
  /*! @decl static void create( int fast ); 
   *! 
   *! If fast is true, do not request a new time from the system, 
   *! instead use the global current time variable. 
   *! 
   *! This will only work in callbacks, but can save significant amounts 
   *! of CPU. 
   */ 
  PIKEFUN void create( int|void fast ) 
  { 
    THIS->hard_update = !fast; 
  } 
} 
 
/*! @endclass 
 */ 
 
/*! @class Timer 
 */ 
PIKECLASS Timer 
{ 
  CVAR struct timeval last_time; 
  CVAR int hard_update; 
   
  /*! @decl float peek() 
   *!   Return the time in seconds since the last time @[get] was called. 
   */ 
  PIKEFUN float peek( ) 
  { 
    extern struct timeval current_time; 
    FLOAT_TYPE res; 
    if( THIS->hard_update ) 
      GETTIMEOFDAY( &current_time ); 
    res = current_time.tv_sec-THIS->last_time.tv_sec + 
      (current_time.tv_usec-THIS->last_time.tv_usec)/1000000.0; 
    RETURN res; 
  } 
 
  /*! @decl float get() 
   *!   Return the time in seconds since the last time get was called. 
   *!   The first time this method is called the time since the object 
   *!   was created is returned instead. 
   */ 
  PIKEFUN float get( ) 
  { 
    extern struct timeval current_time; 
    f_Timer_peek( 0 ); 
    THIS->last_time = current_time; 
    return; 
  } 
 
  /*! @decl static void create( int|void fast ) 
   *!   Create a new timer object. The timer keeps track of relative time 
   *!   with sub-second precision. 
   *!    
   *!   If fast is specified, the timer will not do system calls to get 
   *!   the current time but instead use the one maintained by pike. This 
   *!   will result in faster but somewhat more inexact timekeeping.  
   *!   Also, if your program never utilizes the pike event loop the pike 
   *!   maintained current time never change. 
   */ 
  PIKEFUN void create( int|void fast ) 
  { 
    extern struct timeval current_time; 
    THIS->hard_update = !fast; 
    if( THIS->hard_update ) 
      GETTIMEOFDAY( &current_time ); 
    THIS->last_time = current_time;  
  } 
} 
 
/*! @endclass 
 */ 
 
/*! @endmodule 
 */ 
 
 
PIKECLASS automap_marker 
{ 
  PIKEVAR array arg; 
  PIKEVAR int depth; 
 
  PIKEFUN void create(array a, int d) 
    { 
      if(THIS->arg) free_array(THIS->arg); 
      add_ref(THIS->arg=a); 
      THIS->depth=d; 
    } 
 
  PIKEFUN string _sprintf(int mode, mapping flags) 
    { 
      pop_n_elems(args); 
      if (mode != 'O') { 
        push_undefined (); 
        return; 
      } 
      push_text("%O%*'[*]'n"); 
      if(THIS->arg) 
        ref_push_array(THIS->arg); 
      else 
        push_int(0); 
      push_int(THIS->depth*3); 
      f_sprintf(3); 
    } 
} 
 
 
static void low_automap(int d, 
                        int depth, 
                        struct svalue *fun, 
                        struct svalue *real_args, 
                        INT32 args) 
{ 
  INT32 x,e,tmp,size=0x7fffffff; 
  struct svalue *tmpargs=Pike_sp - args; 
  struct array *ret; 
 
  for(e=0;e<args;e++) 
  { 
    if(real_args[e].type==T_OBJECT && 
       real_args[e].u.object->prog == automap_marker_program && 
       OBJ2_AUTOMAP_MARKER(real_args[e].u.object)->depth >= d) 
    { 
      if(tmpargs[e].type != T_ARRAY) 
        index_error("__automap__", 
                    Pike_sp-args, 
                    args, 
                    tmpargs, 
                    NULL, 
                    "Automap on non-array.\n"); 
      tmp=tmpargs[e].u.array->size; 
      if(tmp < size) 
        size=tmp; 
    } 
  } 
 
#ifdef PIKE_DEBUG 
   if(size == 0x7fffffff) 
    Pike_fatal("No automap markers found in low_automap\n"); 
#endif 
 
  push_array(ret=allocate_array(size)); 
 
  for(x=0;x<size;x++) 
  { 
    for(e=0;e<args;e++) 
    { 
      if(real_args[e].type==T_OBJECT && 
         real_args[e].u.object->prog == automap_marker_program && 
         OBJ2_AUTOMAP_MARKER(real_args[e].u.object)->depth >= d) 
      { 
#ifdef PIKE_DEBUG 
        if(x >= tmpargs[e].u.array->size) 
          Pike_fatal("low_automap failed to determine size!\n"); 
#endif 
        push_svalue(ITEM(tmpargs[e].u.array)+x); 
      }else{ 
        push_svalue(tmpargs+e); 
      } 
    } 
     
    if(d == depth) 
      apply_svalue(fun,args); 
    else 
      low_automap(d+1,depth,fun,real_args,args); 
    dmalloc_touch_svalue(Pike_sp-1); 
    ITEM(ret)[x]=*--Pike_sp; 
  } 
  stack_unlink(args); 
} 
 
 
PIKEFUN array __automap__(mixed fun, mixed ... tmpargs) 
  efun; 
{ 
  int e,depth=-1; 
  check_stack(args); 
 
  for(e=0;e<args-1;e++) 
  { 
    if(tmpargs[e].type==T_OBJECT && 
       tmpargs[e].u.object->prog == automap_marker_program) 
    { 
      int tmp=OBJ2_AUTOMAP_MARKER(tmpargs[e].u.object)->depth; 
      if(tmp > depth) depth=tmp; 
      ref_push_array(OBJ2_AUTOMAP_MARKER(tmpargs[e].u.object)->arg); 
    }else{ 
      push_svalue(tmpargs+e); 
    } 
  } 
  check_stack(depth * (args+1)); 
  low_automap(1,depth,fun,tmpargs,args-1); 
  stack_unlink(args); 
} 
 
void init_builtin(void) 
{ 
INIT 
} 
 
void exit_builtin(void) 
{ 
EXIT 
}