1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
231
  
232
  
233
  
234
  
235
  
236
  
237
  
238
  
239
  
240
  
241
  
242
  
243
  
244
  
245
  
246
  
247
  
248
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
259
  
260
  
261
  
262
  
263
  
264
  
265
  
266
  
267
  
268
  
269
  
270
  
271
  
272
  
273
  
274
  
275
  
276
  
277
  
278
  
279
  
280
  
281
  
282
  
283
  
284
  
285
  
286
  
287
  
288
  
289
  
290
  
291
  
292
  
293
  
294
  
295
  
296
  
297
  
298
  
299
  
300
  
301
  
302
  
303
  
304
  
305
  
306
  
307
  
308
  
309
  
310
  
311
  
312
  
313
  
314
  
315
  
316
  
317
  
318
  
319
  
320
  
321
  
322
  
323
  
324
  
325
  
326
  
327
  
328
  
329
  
330
  
331
  
332
  
333
  
334
  
335
  
336
  
337
  
338
  
339
  
340
  
341
  
342
  
343
  
344
  
345
  
346
  
347
  
348
  
349
  
350
  
351
  
352
  
353
  
354
  
355
  
356
  
357
  
358
  
359
  
360
  
361
  
362
  
363
  
364
  
365
  
366
  
367
  
368
  
369
  
370
  
371
  
372
  
373
  
374
  
375
  
376
  
377
  
378
  
379
  
380
  
381
  
382
  
383
  
384
  
385
  
386
  
387
  
388
  
389
  
390
  
391
  
392
  
393
  
394
  
395
  
396
  
397
  
398
  
399
  
400
  
401
  
402
  
403
  
404
  
405
  
406
  
407
  
408
  
409
  
410
  
411
  
412
  
413
  
414
  
415
  
416
  
417
  
418
  
419
  
420
  
421
  
422
  
423
  
424
  
425
  
426
  
427
  
428
  
429
  
430
  
431
  
432
  
433
  
434
  
435
  
436
  
437
  
438
  
439
  
440
  
441
  
442
  
443
  
444
  
445
  
446
  
447
  
448
  
449
  
450
  
451
  
452
  
453
  
454
  
455
  
456
  
457
  
458
  
459
  
460
  
461
  
462
  
463
  
464
  
465
  
466
  
467
  
468
  
469
  
470
  
471
  
472
  
473
  
474
  
475
  
476
  
477
  
478
  
479
  
480
  
481
  
482
  
483
  
484
  
485
  
486
  
487
  
488
  
489
  
490
  
491
  
492
  
493
  
494
  
495
  
496
  
497
  
498
  
499
  
500
  
501
  
502
  
503
  
504
  
505
  
506
  
507
  
508
  
509
  
510
  
511
  
512
  
513
  
514
  
515
  
516
  
517
  
518
  
519
  
520
  
521
  
522
  
523
  
524
  
525
  
526
  
527
  
528
  
529
  
530
  
531
  
532
  
533
  
534
  
535
  
536
  
537
  
538
  
539
  
540
  
541
  
542
  
543
  
544
  
545
  
546
  
547
  
548
  
549
  
550
  
551
  
552
  
553
  
554
  
555
  
556
  
557
  
558
  
559
  
560
  
561
  
562
  
563
  
564
  
565
  
566
  
567
  
568
  
569
  
570
  
571
  
572
  
573
  
574
  
575
  
576
  
577
  
578
  
579
  
580
  
581
  
582
  
583
  
584
  
585
  
586
  
587
  
588
  
589
  
590
  
591
  
592
  
593
  
594
  
595
  
596
  
597
  
598
  
599
  
600
  
601
  
602
  
603
  
604
  
605
  
606
  
607
  
608
  
609
  
610
  
611
  
612
  
613
  
614
  
615
  
616
  
617
  
618
  
619
  
620
  
621
  
622
  
623
  
624
  
625
  
626
  
627
  
628
  
629
  
630
  
631
  
632
  
633
  
634
  
635
  
636
  
637
  
638
  
639
  
640
  
641
  
642
  
643
  
644
  
645
  
646
  
647
  
648
  
649
  
650
  
651
  
652
  
653
  
654
  
655
  
656
  
657
  
658
  
659
  
660
  
661
  
662
  
663
  
664
  
665
  
666
  
667
  
668
  
669
  
670
  
671
  
672
  
673
  
674
  
675
  
676
  
677
  
678
  
679
  
680
  
681
  
682
  
683
  
684
  
685
  
686
  
687
  
688
  
689
  
690
  
691
  
692
  
693
  
694
  
695
  
696
  
697
  
698
  
699
  
700
  
701
  
702
  
703
  
704
  
705
  
706
  
707
  
708
  
709
  
710
  
711
  
712
  
713
  
714
  
715
  
716
  
717
  
718
  
719
  
720
  
721
  
722
  
723
  
724
  
725
  
726
  
727
  
728
  
729
  
730
  
731
  
732
  
733
  
734
  
735
  
736
  
737
  
738
  
739
  
740
  
741
  
742
  
743
  
744
  
745
  
746
  
747
  
748
  
749
  
750
  
751
  
752
  
753
  
754
  
755
  
756
  
757
  
758
  
759
  
760
  
761
  
762
  
763
  
764
  
765
  
766
  
767
  
768
  
769
  
770
  
771
  
772
  
773
  
774
  
775
  
776
  
777
  
778
  
779
  
780
  
781
  
782
  
783
  
784
  
785
  
786
  
787
  
788
  
789
  
790
  
791
  
792
  
793
  
794
  
795
  
796
  
797
  
798
  
799
  
800
  
801
  
802
  
803
  
804
  
805
  
806
  
807
  
808
  
809
  
810
  
811
  
812
  
813
  
814
  
815
  
816
  
817
  
818
  
819
  
820
  
821
  
822
  
823
  
824
  
825
  
826
  
827
  
828
  
829
  
830
  
831
  
832
  
833
  
834
  
835
  
836
  
837
  
838
  
839
  
840
  
841
  
842
  
843
  
844
  
845
  
846
  
847
  
848
  
849
  
850
  
851
  
852
  
853
  
854
  
855
  
856
  
857
  
858
  
859
  
860
  
861
  
862
  
863
  
864
  
865
  
866
  
867
  
868
  
869
  
870
  
871
  
872
  
873
  
874
  
875
  
876
  
877
  
878
  
879
  
880
  
881
  
882
  
883
  
884
  
885
  
886
  
887
  
888
  
889
  
890
  
891
  
892
  
893
  
894
  
895
  
896
  
897
  
898
  
899
  
900
  
901
  
902
  
903
  
904
  
905
  
906
  
907
  
908
  
909
  
910
  
911
  
912
  
913
  
914
  
915
  
916
  
917
  
918
  
919
  
920
  
921
  
922
  
923
  
924
  
925
  
926
  
927
  
928
  
929
  
930
  
931
  
932
  
933
  
934
  
935
  
936
  
937
  
938
  
939
  
940
  
941
  
942
  
943
  
944
  
945
  
946
  
947
  
948
  
949
  
950
  
951
  
952
  
953
  
954
  
955
  
956
  
957
  
958
  
959
  
960
  
961
  
962
  
963
  
964
  
965
  
966
  
967
  
968
  
969
  
970
  
971
  
972
  
973
  
974
  
975
  
976
  
977
  
978
  
979
  
980
  
981
  
982
  
983
  
984
  
985
  
986
  
987
  
988
  
989
  
990
  
991
  
992
  
993
  
994
  
995
  
996
  
997
  
998
  
999
  
1000
  
1001
  
1002
  
1003
  
1004
  
1005
  
1006
  
1007
  
1008
  
1009
  
1010
  
1011
  
1012
  
1013
  
1014
  
1015
  
1016
  
1017
  
1018
  
1019
  
1020
  
1021
  
1022
  
1023
  
1024
  
1025
  
1026
  
1027
  
1028
  
1029
  
1030
  
1031
  
1032
  
1033
  
1034
  
1035
  
1036
  
1037
  
1038
  
1039
  
1040
  
1041
  
1042
  
1043
  
1044
  
1045
  
1046
  
1047
  
1048
  
1049
  
1050
  
1051
  
1052
  
1053
  
1054
  
1055
  
1056
  
1057
  
1058
  
1059
  
1060
  
1061
  
1062
  
1063
  
1064
  
1065
  
1066
  
1067
  
1068
  
1069
  
1070
  
1071
  
1072
  
1073
  
1074
  
1075
  
1076
  
1077
  
1078
  
1079
  
1080
  
1081
  
1082
  
1083
  
1084
  
1085
  
1086
  
1087
  
1088
  
1089
  
1090
  
1091
  
1092
  
1093
  
1094
  
1095
  
1096
  
1097
  
1098
  
1099
  
1100
  
1101
  
1102
  
1103
  
1104
  
1105
  
1106
  
1107
  
1108
  
1109
  
1110
  
1111
  
1112
  
1113
  
1114
  
1115
  
1116
  
1117
  
1118
  
1119
  
1120
  
1121
  
1122
  
1123
  
1124
  
1125
  
1126
  
1127
  
1128
  
1129
  
1130
  
1131
  
1132
  
1133
  
1134
  
1135
  
1136
  
1137
  
1138
  
1139
  
1140
  
1141
  
1142
  
1143
  
1144
  
1145
  
1146
  
1147
  
1148
  
1149
  
1150
  
1151
  
1152
  
1153
  
1154
  
1155
  
1156
  
1157
  
1158
  
1159
  
1160
  
1161
  
1162
  
1163
  
1164
  
1165
  
1166
  
1167
  
1168
  
1169
  
1170
  
1171
  
1172
  
1173
  
1174
  
1175
  
1176
  
1177
  
1178
  
1179
  
1180
  
1181
  
1182
  
1183
  
1184
  
1185
  
1186
  
1187
  
1188
  
1189
  
1190
  
1191
  
1192
  
1193
  
1194
  
1195
  
1196
  
1197
  
1198
  
1199
  
1200
  
1201
  
1202
  
1203
  
1204
  
1205
  
1206
  
1207
  
1208
  
1209
  
1210
  
1211
  
1212
  
1213
  
1214
  
1215
  
1216
  
1217
  
1218
  
1219
  
1220
  
1221
  
1222
  
1223
  
1224
  
1225
  
1226
  
1227
  
1228
  
1229
  
1230
  
1231
  
1232
  
1233
  
1234
  
1235
  
1236
  
1237
  
1238
  
1239
  
1240
  
1241
  
1242
  
1243
  
1244
  
1245
  
1246
  
1247
  
1248
  
1249
  
1250
  
1251
  
1252
  
1253
  
1254
  
1255
  
1256
  
1257
  
1258
  
1259
  
1260
  
1261
  
1262
  
1263
  
1264
  
1265
  
1266
  
1267
  
1268
  
1269
  
1270
  
1271
  
1272
  
1273
  
1274
  
1275
  
1276
  
1277
  
1278
  
1279
  
1280
  
1281
  
1282
  
1283
  
1284
  
1285
  
1286
  
1287
  
1288
  
1289
  
1290
  
1291
  
1292
  
1293
  
1294
  
1295
  
1296
  
1297
  
1298
  
1299
  
1300
  
1301
  
1302
  
1303
  
1304
  
1305
  
1306
  
1307
  
1308
  
1309
  
1310
  
1311
  
1312
  
1313
  
1314
  
1315
  
1316
  
1317
  
1318
  
1319
  
1320
  
1321
  
1322
  
1323
  
1324
  
1325
  
1326
  
1327
  
1328
  
1329
  
1330
  
1331
  
1332
  
1333
  
1334
  
1335
  
1336
  
1337
  
1338
  
1339
  
1340
  
1341
  
1342
  
1343
  
1344
  
1345
  
1346
  
1347
  
1348
  
1349
  
1350
  
1351
  
1352
  
1353
  
1354
  
1355
  
1356
  
1357
  
1358
  
1359
  
1360
  
1361
  
1362
  
1363
  
1364
  
1365
  
1366
  
1367
  
1368
  
1369
  
1370
  
1371
  
1372
  
1373
  
1374
  
1375
  
1376
  
1377
  
1378
  
1379
  
1380
  
1381
  
1382
  
1383
  
1384
  
1385
  
1386
  
1387
  
1388
  
1389
  
1390
  
1391
  
1392
  
1393
  
1394
  
1395
  
1396
  
1397
  
1398
  
1399
  
1400
  
1401
  
1402
  
1403
  
1404
  
1405
  
1406
  
1407
  
1408
  
1409
  
1410
  
1411
  
1412
  
1413
  
1414
  
1415
  
1416
  
1417
  
1418
  
1419
  
1420
  
1421
  
1422
  
1423
  
1424
  
1425
  
1426
  
1427
  
1428
  
1429
  
1430
  
1431
  
1432
  
1433
  
1434
  
1435
  
1436
  
1437
  
1438
  
1439
  
1440
  
1441
  
1442
  
1443
  
1444
  
1445
  
1446
  
1447
  
1448
  
1449
  
1450
  
1451
  
1452
  
1453
  
1454
  
1455
  
1456
  
1457
  
1458
  
1459
  
1460
  
1461
  
1462
  
1463
  
1464
  
1465
  
1466
  
1467
  
1468
  
1469
  
1470
  
1471
  
1472
  
1473
  
1474
  
1475
  
1476
  
1477
  
1478
  
1479
  
1480
  
1481
  
1482
  
1483
  
1484
  
1485
  
1486
  
1487
  
1488
  
1489
  
1490
  
1491
  
1492
  
1493
  
1494
  
1495
  
1496
  
1497
  
1498
  
1499
  
1500
  
1501
  
1502
  
1503
  
1504
  
1505
  
1506
  
1507
  
1508
  
1509
  
1510
  
1511
  
1512
  
1513
  
1514
  
1515
  
1516
  
1517
  
1518
  
1519
  
1520
  
1521
  
1522
  
1523
  
1524
  
1525
  
1526
  
1527
  
1528
  
1529
  
1530
  
1531
  
1532
  
1533
  
1534
  
1535
  
1536
  
1537
  
1538
  
1539
  
1540
  
1541
  
1542
  
1543
  
1544
  
1545
  
1546
  
1547
  
1548
  
1549
  
1550
  
1551
  
1552
  
1553
  
1554
  
1555
  
1556
  
1557
  
1558
  
1559
  
1560
  
1561
  
1562
  
1563
  
1564
  
1565
  
1566
  
1567
  
1568
  
1569
  
1570
  
1571
  
1572
  
1573
  
1574
  
1575
  
1576
  
1577
  
1578
  
1579
  
1580
  
1581
  
1582
  
1583
  
1584
  
1585
  
1586
  
1587
  
1588
  
1589
  
1590
  
1591
  
1592
  
1593
  
1594
  
1595
  
1596
  
1597
  
1598
  
1599
  
1600
  
1601
  
1602
  
1603
  
1604
  
1605
  
1606
  
1607
  
1608
  
1609
  
1610
  
1611
  
1612
  
1613
  
1614
  
1615
  
1616
  
1617
  
1618
  
1619
  
1620
  
1621
  
1622
  
1623
  
1624
  
1625
  
1626
  
1627
  
1628
  
1629
  
1630
  
1631
  
1632
  
1633
  
1634
  
1635
  
1636
  
1637
  
1638
  
1639
  
1640
  
1641
  
1642
  
1643
  
1644
  
1645
  
1646
  
1647
  
1648
  
1649
  
1650
  
1651
  
1652
  
1653
  
1654
  
1655
  
1656
  
1657
  
1658
  
1659
  
1660
  
1661
  
1662
  
1663
  
1664
  
1665
  
1666
  
1667
  
1668
  
1669
  
1670
  
1671
  
1672
  
1673
  
1674
  
1675
  
1676
  
1677
  
1678
  
1679
  
1680
  
1681
  
1682
  
1683
  
1684
  
1685
  
1686
  
1687
  
1688
  
1689
  
1690
  
1691
  
1692
  
1693
  
1694
  
1695
  
1696
  
1697
  
1698
  
1699
  
1700
  
1701
  
1702
  
1703
  
1704
  
1705
  
1706
  
1707
  
1708
  
1709
  
1710
  
1711
  
1712
  
1713
  
1714
  
1715
  
1716
  
1717
  
1718
  
1719
  
1720
  
1721
  
1722
  
1723
  
1724
  
1725
  
1726
  
1727
  
1728
  
1729
  
1730
  
1731
  
1732
  
1733
  
1734
  
1735
  
1736
  
1737
  
1738
  
1739
  
1740
  
1741
  
1742
  
1743
  
1744
  
1745
  
1746
  
1747
  
1748
  
1749
  
1750
  
1751
  
1752
  
1753
  
1754
  
1755
  
1756
  
1757
  
1758
  
1759
  
1760
  
1761
  
1762
  
1763
  
1764
  
1765
  
1766
  
1767
  
1768
  
1769
  
1770
  
1771
  
1772
  
1773
  
1774
  
1775
  
1776
  
1777
  
1778
  
1779
  
1780
  
1781
  
1782
  
1783
  
1784
  
1785
  
1786
  
1787
  
1788
  
1789
  
1790
  
1791
  
1792
  
1793
  
1794
  
1795
  
1796
  
1797
  
1798
  
1799
  
1800
  
1801
  
1802
  
1803
  
1804
  
1805
  
1806
  
1807
  
1808
  
1809
  
1810
  
1811
  
1812
  
1813
  
1814
  
1815
  
1816
  
1817
  
1818
  
1819
  
1820
  
1821
  
1822
  
1823
  
1824
  
1825
  
1826
  
1827
  
1828
  
1829
  
1830
  
1831
  
1832
  
1833
  
1834
  
1835
  
1836
  
1837
  
1838
  
1839
  
1840
  
1841
  
1842
  
1843
  
1844
  
1845
  
1846
  
1847
  
1848
  
1849
  
1850
  
1851
  
1852
  
1853
  
1854
  
1855
  
1856
  
1857
  
1858
  
1859
  
1860
  
1861
  
1862
  
1863
  
1864
  
1865
  
1866
  
1867
  
1868
  
1869
  
1870
  
1871
  
1872
  
1873
  
1874
  
1875
  
1876
  
1877
  
1878
  
1879
  
1880
  
1881
  
1882
  
1883
  
1884
  
1885
  
1886
  
1887
  
1888
  
1889
  
1890
  
1891
  
1892
  
1893
  
1894
  
1895
  
1896
  
1897
  
1898
  
1899
  
1900
  
1901
  
1902
  
1903
  
1904
  
1905
  
1906
  
1907
  
1908
  
1909
  
1910
  
1911
  
1912
  
1913
  
1914
  
1915
  
1916
  
1917
  
1918
  
1919
  
1920
  
1921
  
1922
  
1923
  
1924
  
1925
  
1926
  
1927
  
1928
  
1929
  
1930
  
1931
  
1932
  
1933
  
1934
  
1935
  
1936
  
1937
  
1938
  
1939
  
1940
  
1941
  
1942
  
1943
  
1944
  
1945
  
1946
  
1947
  
1948
  
1949
  
1950
  
1951
  
1952
  
1953
  
1954
  
1955
  
1956
  
1957
  
1958
  
1959
  
1960
  
1961
  
1962
  
1963
  
1964
  
1965
  
1966
  
1967
  
1968
  
1969
  
1970
  
1971
  
1972
  
1973
  
1974
  
1975
  
1976
  
1977
  
1978
  
1979
  
1980
  
1981
  
1982
  
1983
  
1984
  
1985
  
1986
  
1987
  
1988
  
1989
  
1990
  
1991
  
1992
  
1993
  
1994
  
1995
  
1996
  
1997
  
1998
  
1999
  
2000
  
2001
  
2002
  
2003
  
2004
  
2005
  
2006
  
2007
  
2008
  
2009
  
2010
  
2011
  
2012
  
2013
  
2014
  
2015
  
2016
  
2017
  
2018
  
2019
  
2020
  
2021
  
2022
  
2023
  
2024
  
2025
  
2026
  
2027
  
2028
  
2029
  
2030
  
2031
  
2032
  
2033
  
2034
  
2035
  
2036
  
2037
  
2038
  
2039
  
2040
  
2041
  
2042
  
2043
  
2044
  
2045
  
2046
  
2047
  
2048
  
2049
  
2050
  
2051
  
2052
  
2053
  
2054
  
2055
  
2056
  
2057
  
2058
  
2059
  
2060
  
2061
  
2062
  
2063
  
2064
  
2065
  
2066
  
2067
  
2068
  
2069
  
2070
  
2071
  
2072
  
2073
  
2074
  
2075
  
2076
  
2077
  
2078
  
2079
  
2080
  
2081
  
2082
  
2083
  
2084
  
2085
  
2086
  
2087
  
2088
  
2089
  
2090
  
2091
  
2092
  
2093
  
2094
  
2095
  
2096
  
2097
  
2098
  
2099
  
2100
  
2101
  
2102
  
2103
  
2104
  
2105
  
2106
  
2107
  
2108
  
2109
  
2110
  
2111
  
2112
  
2113
  
2114
  
2115
  
2116
  
2117
  
2118
  
2119
  
2120
  
2121
  
2122
  
2123
  
2124
  
2125
  
2126
  
2127
  
2128
  
2129
  
2130
  
2131
  
2132
  
2133
  
2134
  
2135
  
2136
  
2137
  
2138
  
2139
  
2140
  
2141
  
2142
  
2143
  
2144
  
2145
  
2146
  
2147
  
2148
  
2149
  
2150
  
2151
  
2152
  
2153
  
2154
  
2155
  
2156
  
2157
  
2158
  
2159
  
2160
  
2161
  
2162
  
2163
  
2164
  
2165
  
2166
  
2167
  
2168
  
2169
  
2170
  
2171
  
2172
  
2173
  
2174
  
2175
  
2176
  
2177
  
2178
  
2179
  
2180
  
2181
  
2182
  
2183
  
2184
  
2185
  
2186
  
2187
  
2188
  
2189
  
2190
  
2191
  
2192
  
2193
  
2194
  
2195
  
2196
  
2197
  
2198
  
2199
  
2200
  
2201
  
2202
  
2203
  
2204
  
2205
  
2206
  
2207
  
2208
  
2209
  
2210
  
2211
  
2212
  
2213
  
2214
  
2215
  
2216
  
2217
  
2218
  
2219
  
2220
  
2221
  
2222
  
2223
  
2224
  
2225
  
2226
  
2227
  
2228
  
2229
  
2230
  
2231
  
2232
  
2233
  
2234
  
2235
  
2236
  
2237
  
2238
  
2239
  
2240
  
2241
  
2242
  
2243
  
2244
  
2245
  
2246
  
2247
  
2248
  
2249
  
2250
  
2251
  
2252
  
2253
  
2254
  
2255
  
2256
  
2257
  
2258
  
2259
  
2260
  
2261
  
2262
  
2263
  
2264
  
2265
  
2266
  
2267
  
2268
  
2269
  
2270
  
2271
  
2272
  
2273
  
2274
  
2275
  
2276
  
2277
  
2278
  
2279
  
2280
  
2281
  
2282
  
2283
  
2284
  
2285
  
2286
  
2287
  
2288
  
2289
  
2290
  
2291
  
2292
  
2293
  
2294
  
2295
  
2296
  
2297
  
2298
  
2299
  
2300
  
2301
  
2302
  
2303
  
2304
  
2305
  
2306
  
2307
  
2308
  
2309
  
2310
  
2311
  
2312
  
2313
  
2314
  
2315
  
2316
  
2317
  
2318
  
2319
  
2320
  
2321
  
2322
  
2323
  
2324
  
2325
  
2326
  
2327
  
2328
  
2329
  
2330
  
2331
  
2332
  
2333
  
2334
  
2335
  
2336
  
2337
  
2338
  
2339
  
2340
  
2341
  
2342
  
2343
  
2344
  
2345
  
2346
  
2347
  
2348
  
2349
  
2350
  
2351
  
2352
  
2353
  
2354
  
2355
  
2356
  
2357
  
2358
  
2359
  
2360
  
2361
  
2362
  
2363
  
2364
  
2365
  
2366
  
2367
  
2368
  
2369
  
2370
  
2371
  
2372
  
2373
  
2374
  
2375
  
2376
  
2377
  
2378
  
2379
  
2380
  
2381
  
2382
  
2383
  
2384
  
2385
  
2386
  
2387
  
2388
  
2389
  
2390
  
2391
  
2392
  
2393
  
2394
  
2395
  
2396
  
2397
  
2398
  
2399
  
2400
  
2401
  
2402
  
2403
  
2404
  
2405
  
2406
  
2407
  
2408
  
2409
  
2410
  
2411
  
2412
  
2413
  
2414
  
2415
  
2416
  
2417
  
2418
  
2419
  
2420
  
2421
  
2422
  
2423
  
2424
  
2425
  
2426
  
2427
  
2428
  
2429
  
2430
  
2431
  
2432
  
2433
  
2434
  
2435
  
2436
  
2437
  
2438
  
2439
  
2440
  
2441
  
2442
  
2443
  
2444
  
2445
  
2446
  
2447
  
2448
  
2449
  
2450
  
2451
  
2452
  
2453
  
2454
  
2455
  
2456
  
2457
  
2458
  
2459
  
2460
  
2461
  
2462
  
2463
  
2464
  
2465
  
2466
  
2467
  
2468
  
2469
  
2470
  
2471
  
2472
  
2473
  
2474
  
2475
  
2476
  
2477
  
2478
  
2479
  
2480
  
2481
  
2482
  
2483
  
2484
  
2485
  
2486
  
2487
  
2488
  
2489
  
2490
  
2491
  
2492
  
2493
  
2494
  
2495
  
2496
  
2497
  
2498
  
2499
  
2500
  
2501
  
2502
  
2503
  
2504
  
2505
  
2506
  
2507
  
2508
  
2509
  
2510
  
2511
  
2512
  
2513
  
2514
  
2515
  
2516
  
2517
  
2518
  
2519
  
2520
  
2521
  
2522
  
2523
  
2524
  
2525
  
2526
  
2527
  
2528
  
2529
  
2530
  
2531
  
2532
  
2533
  
2534
  
2535
  
2536
  
2537
  
2538
  
2539
  
2540
  
2541
  
2542
  
2543
  
2544
  
2545
  
2546
  
2547
  
2548
  
2549
  
2550
  
2551
  
2552
  
2553
  
2554
  
2555
  
2556
  
2557
  
2558
  
2559
  
2560
  
2561
  
2562
  
2563
  
2564
  
2565
  
2566
  
2567
  
2568
  
2569
  
2570
  
2571
  
2572
  
2573
  
2574
  
2575
  
2576
  
2577
  
2578
  
2579
  
2580
  
2581
  
2582
  
2583
  
2584
  
2585
  
2586
  
2587
  
2588
  
2589
  
2590
  
2591
  
2592
  
2593
  
2594
  
2595
  
2596
  
2597
  
2598
  
2599
  
2600
  
2601
  
2602
  
2603
  
2604
  
2605
  
2606
  
2607
  
2608
  
2609
  
2610
  
2611
  
2612
  
2613
  
2614
  
2615
  
2616
  
2617
  
2618
  
2619
  
2620
  
2621
  
2622
  
2623
  
2624
  
2625
  
2626
  
2627
  
2628
  
2629
  
2630
  
2631
  
2632
  
2633
  
2634
  
2635
  
2636
  
2637
  
2638
  
2639
  
2640
  
2641
  
2642
  
2643
  
2644
  
2645
  
2646
  
2647
  
2648
  
2649
  
2650
  
2651
  
2652
  
2653
  
2654
  
2655
  
2656
  
2657
  
2658
  
2659
  
2660
  
2661
  
2662
  
2663
  
2664
  
2665
  
2666
  
2667
  
2668
  
2669
  
2670
  
2671
  
2672
  
2673
  
2674
  
2675
  
2676
  
2677
  
2678
  
2679
  
2680
  
2681
  
2682
  
2683
  
2684
  
2685
  
2686
  
2687
  
2688
  
2689
  
2690
  
2691
  
2692
  
2693
  
2694
  
2695
  
2696
  
2697
  
2698
  
2699
  
2700
  
2701
  
2702
  
2703
  
2704
  
2705
  
2706
  
2707
  
2708
  
2709
  
2710
  
2711
  
2712
  
2713
  
2714
  
2715
  
2716
  
2717
  
2718
  
2719
  
2720
  
2721
  
2722
  
2723
  
2724
  
2725
  
2726
  
2727
  
2728
  
2729
  
2730
  
2731
  
2732
  
2733
  
2734
  
2735
  
2736
  
2737
  
2738
  
2739
  
2740
  
2741
  
2742
  
2743
  
2744
  
2745
  
2746
  
2747
  
2748
  
2749
  
2750
  
2751
  
2752
  
2753
  
2754
  
2755
  
2756
  
2757
  
2758
  
2759
  
2760
  
2761
  
2762
  
2763
  
2764
  
2765
  
2766
  
2767
  
2768
  
2769
  
2770
  
2771
  
2772
  
2773
  
2774
  
2775
  
2776
  
2777
  
2778
  
2779
  
2780
  
2781
  
2782
  
2783
  
2784
  
2785
  
2786
  
2787
  
2788
  
2789
  
2790
  
2791
  
2792
  
2793
  
2794
  
2795
  
2796
  
2797
  
2798
  
2799
  
2800
  
2801
  
2802
  
2803
  
2804
  
2805
  
2806
  
2807
  
2808
  
2809
  
2810
  
2811
  
2812
  
2813
  
2814
  
2815
  
2816
  
2817
  
2818
  
2819
  
2820
  
2821
  
2822
  
2823
  
2824
  
2825
  
2826
  
2827
  
2828
  
2829
  
2830
  
2831
  
2832
  
2833
  
2834
  
2835
  
2836
  
2837
  
2838
  
2839
  
2840
  
2841
  
2842
  
2843
  
2844
  
2845
  
2846
  
2847
  
2848
  
2849
  
2850
  
2851
  
2852
  
2853
  
2854
  
2855
  
2856
  
2857
  
2858
  
2859
  
2860
  
2861
  
2862
  
2863
  
2864
  
2865
  
2866
  
2867
  
2868
  
2869
  
2870
  
2871
  
2872
  
2873
  
2874
  
2875
  
2876
  
2877
  
2878
  
2879
  
2880
  
2881
  
2882
  
2883
  
2884
  
2885
  
2886
  
2887
  
2888
  
2889
  
2890
  
2891
  
2892
  
2893
  
2894
  
2895
  
2896
  
2897
  
2898
  
2899
  
2900
  
2901
  
2902
  
2903
  
2904
  
2905
  
2906
  
2907
  
2908
  
2909
  
2910
  
2911
  
2912
  
2913
  
2914
  
2915
  
2916
  
2917
  
2918
  
2919
  
2920
  
2921
  
2922
  
2923
  
2924
  
2925
  
2926
  
2927
  
2928
  
2929
  
2930
  
2931
  
2932
  
2933
  
2934
  
2935
  
2936
  
2937
  
2938
  
2939
  
2940
  
2941
  
2942
  
2943
  
2944
  
2945
  
2946
  
2947
  
2948
  
2949
  
2950
  
2951
  
2952
  
2953
  
2954
  
2955
  
2956
  
2957
  
2958
  
2959
  
2960
  
2961
  
2962
  
2963
  
2964
  
2965
  
2966
  
2967
  
2968
  
2969
  
2970
  
2971
  
2972
  
2973
  
2974
  
2975
  
2976
  
2977
  
2978
  
2979
  
2980
  
2981
  
2982
  
2983
  
2984
  
2985
  
2986
  
2987
  
2988
  
2989
  
2990
  
2991
  
2992
  
2993
  
2994
  
2995
  
2996
  
2997
  
2998
  
2999
  
3000
  
3001
  
3002
  
3003
  
3004
  
3005
  
3006
  
3007
  
3008
  
3009
  
3010
  
3011
  
3012
  
3013
  
3014
  
3015
  
3016
  
3017
  
3018
  
3019
  
3020
  
3021
  
3022
  
3023
  
3024
  
3025
  
3026
  
3027
  
3028
  
3029
  
3030
  
3031
  
3032
  
3033
  
3034
  
3035
  
3036
  
3037
  
3038
  
3039
  
3040
  
3041
  
3042
  
3043
  
3044
  
3045
  
3046
  
3047
  
3048
  
3049
  
3050
  
3051
  
3052
  
3053
  
3054
  
3055
  
3056
  
3057
  
3058
  
3059
  
3060
  
3061
  
3062
  
3063
  
3064
  
3065
  
3066
  
3067
  
3068
  
3069
  
3070
  
3071
  
3072
  
3073
  
3074
  
3075
  
3076
  
3077
  
3078
  
3079
  
3080
  
3081
  
3082
  
3083
  
3084
  
3085
  
3086
  
3087
  
3088
  
3089
  
3090
  
3091
  
3092
  
3093
  
3094
  
3095
  
3096
  
3097
  
3098
  
3099
  
3100
  
3101
  
3102
  
3103
  
3104
  
3105
  
3106
  
3107
  
3108
  
3109
  
3110
  
3111
  
3112
  
3113
  
3114
  
3115
  
3116
  
3117
  
3118
  
3119
  
3120
  
3121
  
3122
  
3123
  
3124
  
3125
  
3126
  
3127
  
3128
  
3129
  
3130
  
3131
  
3132
  
3133
  
3134
  
3135
  
3136
  
3137
  
3138
  
3139
  
3140
  
3141
  
3142
  
3143
  
3144
  
3145
  
3146
  
3147
  
3148
  
3149
  
3150
  
3151
  
3152
  
3153
  
3154
  
3155
  
3156
  
3157
  
3158
  
3159
  
3160
  
3161
  
3162
  
3163
  
3164
  
3165
  
3166
  
3167
  
3168
  
3169
  
3170
  
3171
  
3172
  
3173
  
3174
  
3175
  
3176
  
3177
  
3178
  
3179
  
3180
  
3181
  
3182
  
3183
  
3184
  
3185
  
3186
  
3187
  
3188
  
3189
  
3190
  
3191
  
3192
  
3193
  
3194
  
3195
  
3196
  
3197
  
3198
  
3199
  
3200
  
3201
  
3202
  
3203
  
3204
  
3205
  
3206
  
3207
  
3208
  
3209
  
3210
  
3211
  
3212
  
3213
  
3214
  
3215
  
3216
  
3217
  
3218
  
3219
  
3220
  
3221
  
3222
  
3223
  
3224
  
#pike __REAL_VERSION__ 
 
// 
// Incremental Pike Evaluator 
// 
 
constant hilfe_todo = #"List of known Hilfe bugs/room for improvements: 
 
- Hilfe can not handle enums. 
- Hilfe can not handle typedefs. 
- Hilfe can not handle implicit lambdas. 
- Hilfe can not handle unnamed classes. 
- Hilfe can not handle named lambdas. 
- Hilfe should possibly handle imports better, e.g. overwrite the 
  local variables/constants/functions/programs. 
- Add some better multiline edit support. 
- Improve doc command to get documentation from c-code. 
"; 
 
// The Big To Do: 
// The parser part of Hilfe should really be redesigned once again. 
// The user input is first run through Parser.Pike.split and output 
// as a token stream. This stream is fed into a streaming parser which 
// then relocates the variables and outputs expression objects with 
// evaluation destinations already assigned. Note that the streaming 
// parser can not start on the next expression before the previous 
// expression has been evaluated, because the variable table might not 
// be up to date. 
 
//! Abstract class for Hilfe commands. 
class Command { 
 
  //! Returns a one line description of the command. This help should 
  //! be shorter than 54 characters. 
  string help(string what); 
 
  //! A more elaborate documentation of the command. This should be 
  //! less than 68 characters per line. 
  string doc(string what, string with) { return help(what); } 
 
  //! The actual command callback. Messages to the user should be 
  //! written out by using the safe_write method in the @[Evaluator] 
  //! object. 
  void exec(Evaluator e, string line, array(string) words, 
            array(string) tokens); 
} 
 
 
// 
// Built in commands 
// 
 
//! Variable reset command. Put ___Hilfe->commands->reset = 
//! Tools.Hilfe.CommandReset(); in your .hilferc to have this command 
//! defined when you open Hilfe. 
class CommandReset { 
  inherit Command; 
  string help(string what) { return "Undefines the given symbol."; } 
  string doc(string what, string with) { 
    return "Undefines any variable, constant, function or program, " 
      "specified by\nname. Example: \"reset tmp\"\n"; 
  } 
 
  void exec(Evaluator e, string line, array(string) words, 
            array(string) tokens) { 
    string n = sizeof(words)>1 && words[1]; 
    if(!n) { 
      e->safe_write("No symbol given as argument to reset.\n"); 
      return; 
    } 
    if(!has_index(e->variables, n) && !has_index(e->constants, n) && 
       !has_index(e->functions, n) && !has_index(e->programs, n)) { 
      e->safe_write("Symbol %O not defined.\n", n); 
      return; 
    } 
 
    m_delete(e->variables, n); 
    m_delete(e->types, n); 
    m_delete(e->constants, n); 
    m_delete(e->functions, n); 
    m_delete(e->programs, n); 
  } 
} 
 
//! Helper function that formats a time span in nanoseconds to 
//! something more human readable (ns, ms or s). 
string format_hr_time(int i) { 
  if(i<1000) return i+"ns"; 
  if(i<1000000) return sprintf("%.2fms", i/1000.0); 
  return sprintf("%.2fs", i/1000000.0); 
} 
 
protected class CommandSet { 
  inherit Command; 
 
  string help(string what) { return "Change Hilfe settings."; } 
  string doc(string what, string with) { 
    if(with=="format") 
      return documentation_set_format; 
    return documentation_set; 
  } 
 
  protected void bench_reswrite(function(string, mixed ... : int) w, 
                              string sres, int num, mixed res, 
                              int last_compile_time, int last_eval_time) { 
    if(!sres) 
      w( "Compilation: %s, Execution: %s\n", 
         format_hr_time(last_compile_time), 
         format_hr_time(last_eval_time) ); 
 
    else 
      w( "Result %d: %s\nCompilation: %s, Execution: %s\n", 
         num, replace(sres, "\n", "\n        "+(" "*sizeof(""+num))), 
         format_hr_time(last_compile_time), 
         format_hr_time(last_eval_time) ); 
  } 
 
  protected class Reswriter (string format) { 
    void `()(function(string, mixed ... : int) w, string sres, int num, 
             mixed res, int last_compile_time, int last_eval_time) { 
      mixed err = catch { 
        if(!sres) 
          w("Ok.\n"); 
        else 
          w(format, sres, num, res, 
            format_hr_time(last_compile_time), 
            format_hr_time(last_eval_time), 
            last_compile_time, last_eval_time); 
      }; 
      if(err) 
        w("Hilfe Error: Could not format result.\n%s\n", 
          describe_backtrace(err)); 
    } 
  } 
 
  protected class Intwriter (string type, function fallback) { 
    protected void `()(function(string, mixed ... : int) w, string sres, 
                       int num, mixed res, int last_compile_time, 
                       int last_eval_time) { 
      if(res && intp(res)) { 
        if(type=="b") { 
          string s = sprintf("%b", res); 
          s = "0"*(8-sizeof(s)%8) + s; 
          w( "0b" + s/8*" " + "\n" ); 
        } 
        else 
          w(type+"\n", res); 
      } 
      else 
        fallback(w, sres, num, res, last_compile_time, last_eval_time); 
    } 
  } 
 
  protected array my_indices(string|mapping|multiset|object|program in) { 
    if(objectp(in) || programp(in)) 
      return sort(indices(in)); 
    return indices(in); 
  } 
 
  void exec(Evaluator e, string line, array(string) words, 
            array(string) tokens) { 
 
    line = sizeof(words)>1 && words[1]; 
    function(array(string)|string, mixed ... : void) write = e->safe_write; 
 
    if(!line) { 
      write("No setting to change given.\n"); 
      return; 
    } 
 
    int(0..1) arg_check(string arg) { 
      if(line!=arg) return 0; 
      if(sizeof(words)<3) { 
        write("Insufficient number of arguments to set %s.\n", line); 
        line = ""; 
        return 0; 
      } 
      return 1; 
    }; 
 
    if(words[1]=="hedda") { 
      mapping(string:string) vars = ([ 
        "foo":"mixed", 
        "bar":"mixed", 
        "i":"int", 
        "s":"string", 
        "f":"float", 
        "m":"mapping", 
        "a":"array" 
      ]); 
      mapping vals = ([     "s":"", "f":0.0, "m":([]), "a":({}) ]); 
      foreach(vars; string name; string type) 
        if(!e->variables[name]) { 
          e->variables[name] = vals[name]; 
          e->types[name] = type; 
        } 
      e->functions->indices = my_indices; 
      return; 
    } 
 
    if(arg_check("warnings")) { 
      switch( words[2] ) { 
      case "strict": 
        e->warnings = 1; 
        e->strict_types = 1; 
        return; 
      case "on": 
        e->warnings = 1; 
        e->strict_types = 0; 
        return; 
      case "off": 
      default: 
        e->warnings = 0; 
        e->strict_types = 1; 
        return; 
      } 
    } 
 
    if(arg_check("trace")) { 
      e->trace_level = (int)words[2]; 
      return; 
    } 
 
    if(arg_check("assembler_debug")) { 
#if constant(Debug.assembler_debug) 
      e->assembler_debug_level = (int)words[2]; 
#else 
      write("Assembler debug not available.\n"); 
#endif 
      return; 
    } 
 
    if(arg_check("compiler_trace")) { 
#if constant(Debug.compiler_trace) 
      e->compiler_trace_level = (int)words[2]; 
#else 
      write("Compiler trace not available.\n"); 
#endif 
      return; 
    } 
 
    if(arg_check("debug")) { 
#if constant(_debug) 
      e->debug_level = (int)words[2]; 
#else 
      write("Debug not available.\n"); 
#endif 
      return; 
    } 
 
    if(arg_check("history")) { 
      e->history->set_maxsize((int)words[2]); 
      return; 
    } 
 
    if(arg_check("format")) { 
      switch(words[2]) { 
      case "default": 
        e->reswrite = e->std_reswrite; 
        return; 
      case "bench": 
        e->reswrite = bench_reswrite; 
        return; 
      case "bin": 
        e->reswrite = Intwriter("b", e->std_reswrite); 
        return; 
      case "oct": 
        e->reswrite = Intwriter("0%o", e->std_reswrite); 
        return; 
      case "hex": 
        e->reswrite = Intwriter("0x%x", e->std_reswrite); 
        return; 
      case "sprintf": 
        string f; 
        foreach(tokens, string token) 
          if(token[0]=='"') f=token; 
        if(!f) 
          write("No formatting string given.\n"); 
        else { 
          // FIXME: We should do a real string compilation. 
          f = replace(f, ([ "\\n":"\n", "\\\"":"\"" ]) ); 
          e->reswrite = Reswriter(f[1..<1]); 
        } 
        return; 
      } 
      write("No result presentation format %O defined.\n", words[2]); 
      return; 
    } 
 
    if(line=="") return; 
    write("No setting named %O exists.\n", line); 
    // FIXME: Allow user to change prompt (>/>>)? 
  } 
} 
 
protected class CommandExit { 
  inherit Command; 
  string help(string what) { return "Exit Hilfe."; } 
 
  void exec(Evaluator e, string line, array(string) words, 
            array(string) tokens) { 
    if (e->readline) { 
      e->readline->get_history()->pop(line); 
      e->save_history(); 
    } 
    e->safe_write("Exiting.\n"); 
    destruct(e); 
    exit(0); 
  } 
} 
 
protected class CommandDoc { 
  inherit Command; 
  string help(string what) 
  { 
    return #"Show documentation for pike modules and classes. 
Documentation for the symbol before the cursor is also accessible 
by pressing F1."; 
  } 
 
  void exec(Evaluator e, string line, array(string) words, 
            array(string) tokens) 
  { 
    if (sizeof(tokens) > 3) 
      output_doc(e, Parser.Pike.group(tokens[2..])); 
  } 
 
  void output_doc(Evaluator e, array(array(string)|string) tokens) 
  { 
    object|function module; 
    string type; 
    array rest; 
    object docs; 
 
    if (!sizeof(tokens)) 
      return; 
    if (tokens == ({ "write", "\n\n" })) 
      module = Stdio.stdout->write; 
    else 
      [module, rest, type] = resolv(e, tokens); 
 
    if (!module) 
      return; 
    if (type != "autodoc") 
      docs = master()->show_doc(module); 
    else 
      docs = module; 
 
    object child; 
    if (docs?->documentation) 
    { 
      if (docs->objects && sizeof(docs->objects)) 
        e->safe_write("\n%{%s\n%}\n", docs->objects->print()); 
      e->safe_write(docs->documentation->text+"\n"); 
    } 
    else if (docs && (child=docs->findObject("create")) && child->documentation) 
    { 
      e->safe_write("\n%{%s\n%}\n", replace(child->objects->print()[*], ([ "create":(tokens[2..]-({"\n\n"}))*"", " | ":"|" ]) )); 
      e->safe_write(child->documentation->text+"\n"); 
    } 
    else if (docs && sizeof(docs->docGroups)) 
      e->safe_write("\n%{%s\n%}\n", Array.flatten(docs->docGroups->objects->print())); 
    else if (objectp(module)) 
      e->safe_write("\n%{%s\n%}\n", indices(module)); 
    else if (functionp(module)) 
      e->safe_write("\n%O\n", _typeof(module)); 
    else 
      e->safe_write("Documentation not found!\n"); 
  } 
} 
 
protected class CommandHelp { 
  inherit Command; 
  string help(string what) { return "Show help text."; } 
 
  void exec(Evaluator e, string line, array(string) words, 
            array(string) tokens) { 
    line = words[1..]*" "; 
    function(array(string)|string, mixed ... : void) write = e->safe_write; 
 
    switch(line) { 
 
    case "me more": 
      write( documentation_help_me_more ); 
      return; 
 
    case "hilfe todo": 
      write(hilfe_todo); 
      return; 
 
    case "about hilfe": 
      e->print_version(); 
      return; 
    } 
 
    if(sizeof(words)>1 && e->commands[words[1]]) { 
      string ret = e->commands[words[1]]->doc(words[1], words[2..]*""); 
      if(ret) write(ret+"\n"); 
      return; 
    } 
 
    write("\n"); 
    e->print_version(); 
    write( #"Hilfe is a tool to evaluate Pike code interactively and 
incrementally. Any Pike function, expression or variable declaration 
can be entered at the command line. Tab completion is also available. 
There are also a few extra commands: 
 
"); 
 
    array err = ({}); 
    foreach(sort(indices(e->commands)), string cmd) { 
      string ret; 
      err += ({ catch( ret = e->commands[cmd]->help(cmd) ) }); 
      if(ret) 
        write(" %-10s - %s\n", cmd, ret); 
    } 
 
    write( #" .          - Abort current input batch. 
 
Enter \"help me more\" for further Hilfe help. 
"); 
 
    foreach(err-({0}), mixed err) 
      write(describe_backtrace(err)+"\n\n"); 
  } 
} 
 
protected class CommandDot { 
  inherit Command; 
  string help(string what) { return 0; } 
 
  local protected constant usr_vector_a = ({ 
    89, 111, 117, 32, 97, 114, 101, 32, 105, 110, 115, 105, 100, 101, 32, 97, 
    32, 72, 105, 108, 102, 101, 46, 32, 73, 116, 32, 115, 109, 101, 108, 108, 
    115, 32, 103, 111, 111, 100, 32, 104, 101, 114, 101, 46, 32, 89, 111, 
    117, 32, 115, 101, 101, 32 }); 
  local protected constant usr_vector_b = ({ 
    32, 89, 111, 117, 32, 99, 97, 110, 32, 103, 111, 32, 105, 110, 32, 97, 
    110, 121, 32, 100, 105, 114, 101, 99, 116, 105, 111, 110, 32, 102, 114, 
    111, 109, 32, 104, 101, 114, 101, 46 }); 
  local protected constant usr_vector_c = ({ 
    32, 89, 111, 117, 32, 97, 114, 101, 32, 99, 97, 114, 114, 121, 105, 110, 
    103, 32 }); 
  local protected constant usr_vector_d = usr_vector_c[..8] + ({ 
    101, 109, 112, 116, 121, 32, 104, 97, 110, 100, 101, 100, 46 }); 
 
  protected array(string) thing(array|mapping|object thing, string what, 
                              void|string a, void|string b) { 
    if(!sizeof(thing)) return ({}); 
    return ({ sizeof(thing)+" "+what+(sizeof(thing)==1?(a||""):(b||"s")) }); 
  } 
 
  void exec(Evaluator e, string line, array(string) words, 
            array(string) tokens) { 
    string ret = (string)usr_vector_a; 
 
    array(string) tmp = ({}); 
    tmp += thing(e->imports, "import"); 
    tmp += thing(e->inherits, "inherit"); 
    tmp += thing(e->history, "history entr", "y", "ies"); 
    if(sizeof(tmp)) 
      ret += String.implode_nicely(tmp) + "."; 
    else 
      ret += "nothing."; 
 
    tmp = ({}); 
    tmp += thing(e->variables, "variable"); 
    tmp += thing(e->constants, "constant"); 
    tmp += thing(e->functions, "function"); 
    tmp += thing(e->programs, "program"); 
    if(sizeof(tmp)) 
      ret += (string)usr_vector_c + String.implode_nicely(tmp) + "."; 
    else 
      ret += (string)usr_vector_d; 
 
    ret += (string)usr_vector_b; 
    e->safe_write("%-=67s\n", ret); 
  } 
} 
 
class CommandDump { 
  inherit Command; 
 
  protected function(array(string)|string, mixed ... : int) write; 
 
  string help(string what) { return "Dump variables and other info."; } 
  string doc(string what, string with) { return documentation_dump; } 
 
  protected void wrapper(Evaluator e) { 
    if(!e->last_compiled_expr) { 
      write("No wrapper compiled so far.\n"); 
      return; 
    } 
    write("Last compiled wrapper:\n"); 
    int i; 
    string w = map(e->last_compiled_expr/"\n", 
                   lambda(string in) { 
                     return sprintf("%03d: %s", ++i, in); 
                   })*"\n"; 
    write(w+"\n"); 
  } 
 
  protected string print_mapping(array(string) ind, array val) { 
    int m = max( @filter(map(ind, sizeof), `<, 20), 8 ); 
    foreach(ind; int i; string name) 
      write("%-*s : %s\n", m, name, replace(sprintf("%O", val[i]), "\n", 
                                            "\n"+(" "*m)+"   ")); 
  } 
 
  protected void dump(Evaluator e) { 
    if(sizeof(e->constants)) { 
      write("\nConstants:\n"); 
      array(string) a=indices(e->constants); 
      array b=values(e->constants); 
      sort(a,b); 
      print_mapping(a,b); 
    } 
    if(sizeof(e->variables)) { 
      write("\nVariables:\n"); 
      array(string) a=indices(e->variables); 
      array b=values(e->variables); 
      sort(a,b); 
      a = map(a, lambda(string in) { return e->types[in] + " " + in; } ); 
      print_mapping(a,b); 
    } 
    if(sizeof(e->functions)) { 
      write("\nFunctions:\n"); 
      foreach(sort(indices(e->functions)), string name) 
        write("%s\n", name); 
    } 
    if(sizeof(e->programs)) { 
      write("\nPrograms:\n"); 
      foreach(sort(indices(e->programs)), string name) 
        write("%s\n", name); 
    } 
    if(sizeof(e->inherits)) { 
      write("\nInherits:\n" + e->inherits*"\n" + "\n"); 
    } 
    if(sizeof(e->imports)) { 
      write("\nImports:\n" + e->imports*"\n" + "\n"); 
    } 
  } 
 
  void exec(Evaluator e, string line, array(string) words, 
            array(string) tokens) { 
    write = e->safe_write; 
 
    line = words[1..]*""; 
    switch( line ) { 
    case "wrapper": 
      wrapper(e); 
      return; 
    case "state": 
      write(e->state->status()); 
      return; 
    case "history": 
      write(e->history->status()); 
      return; 
    case "memory": 
      write(Pike.Lazy.Debug.pp_memory_usage()); 
      return; 
    case "": 
      dump(e); 
      return; 
    } 
    write("Unknown dump specifier.\n"); 
    write(doc(0,0)+"\n"); 
  } 
} 
 
protected class CommandHej { 
  inherit Command; 
  string help(string what) { return 0; } 
  void exec(Evaluator e, string line, array(string) words, 
            array(string) tokens) { 
    if(line[0]=='.') e->safe_write( (string)({ 84,106,97,98,97,33,10 }) ); 
  } 
} 
 
protected class CommandNew { 
  inherit Command; 
  string help(string what) { return "Clears the Hilfe state."; } 
  string doc(string what, string with) { return documentation_new; } 
 
  void exec(Evaluator e, string line, array(string) words, 
            array(string) tokens) { 
 
    line = sizeof(words)>1 && words[1]; 
    switch(line) { 
    case "variables": 
      e->types = ([]); 
    case "constants": 
    case "functions": 
    case "programs": 
      e[line] = ([]); 
      return; 
 
    case "imports": 
    case "inherits": 
      e[line] = ({}); 
      return; 
 
    case "history": 
      e->history->flush(); 
      return; 
    } 
 
    if(line) { 
      e->safe_write("Unknown specifier %O.\n", line); 
      return; 
    } 
    e->reset_evaluator(); 
  } 
} 
 
protected class CommandStartStop { 
  inherit Command; 
  SubSystems subsystems = SubSystems(); 
 
  string help(string what) { 
    switch(what){ 
    case "start": 
      return "Start a subsystem."; 
    case "stop": 
      return "Stop a subsystem."; 
    } 
  } 
 
  string doc(string what, string with) { 
    if(what=="start") 
      return subsystems->doc(1); 
    if(what=="stop") 
      return subsystems->doc(0); 
  } 
 
  void exec(Evaluator e, string line, array(string) words, 
            array(string) tokens) { 
    if(sizeof(words)>=2) { 
      subsystems[words[0]](e, words[1], words[1..]); 
      return; 
    } 
    e->safe_write("No subsystem selected. Available subsystems:\n"); 
    e->safe_write("%{   %s\n%}", subsystems->list()); 
  } 
} 
 
 
// 
// Backend subsystem 
// 
 
#if constant(thread_create) 
protected class SubSysBackend { 
  int(0..1) is_running; 
 
  constant startdoc = "backend [once]\n" 
  "\tStarts the backend thread. If \"once\" is specified execution\n" 
  "\twill end at first exception. Can be restarted with \"start backend\".\n"; 
 
  constant stopdoc = "backend\n\tstop the backend thread.\n"; 
 
  void start(Evaluator e, array(string) words){ 
    int(0..1) once = (sizeof(words)>=2 && words[1]=="once"); 
    add_constant("backend_thread", 
                 thread_create(backend_loop, e->safe_write, once)); 
  } 
 
  void stop(Evaluator e, array(string) words){ 
    call_out(throw, 0, 0); 
  } 
 
  int(0..1) runningp() { 
    return is_running; 
  } 
 
  protected void backend_loop(function(string:int) write_err, int(0..1) once){ 
    is_running=1; 
    object backend = Pike.DefaultBackend; 
    mixed err; 
    do { 
      err = catch { 
        while(1) 
          backend(3600.0); 
      }; 
      if(err) 
        write_err(describe_backtrace(err)); 
    } while(!once && err); 
    if(once && err) 
      write_err("Backend done.\r\n"); 
    is_running=0; 
  } 
 
} 
#endif // constant(thread_create) 
 
protected class SubSysLogger { 
 
  constant startdoc = "logging [<filename>]\n" 
  "\tLogs all input and output to a log file. If no file name is \n" 
  "\tspecified logging will be appended to hilfe.log in the current\n" 
  "\twork directory.\n"; 
 
  constant stopdoc = "logging\n\tTurns off logging to file.\n"; 
  int(0..1) running; 
 
  protected class Logger (Evaluator e, Stdio.File logfile) 
  { 
    constant is_logger = 1; 
 
    protected void create() { 
      e->add_input_hook(this); 
      running = 1; 
    } 
 
    protected void _destruct() { 
      e && e->remove_input_hook(this); 
      running = 0; 
    } 
 
    protected int(0..0) `() (string in) { 
      if(!running) return 0; 
      if(catch( logfile->write(in) )) { 
        e->remove_writer(this); 
        e->safe_write("Error writing to log file. Terminating logger.\n"); 
      } 
      return 0; 
    } 
 
  } 
 
  void start(Evaluator e, array(string) words) { 
    string fn = sizeof(words)>1 ? words[1] : "hilfe.log"; 
    Stdio.File logfile = Stdio.File( fn, "wac" ); 
    if(!logfile) { 
      e->safe_write("Hilfe Error: Could not open log file %O.\n", fn); 
      return; 
    } 
    e->safe_write("Logging to log file %O.\n", fn); 
    e->add_writer( Logger(e, logfile) ); 
    e->safe_write("Hilfe logger activated "+ctime(time())); 
  } 
 
  void stop(Evaluator e, void|array(string) words) { 
    running = 0; 
    if(arrayp(e->write)) { 
      foreach([array]e->write, mixed w) 
        if(objectp(w) && ([object]w)->is_logger) 
          e->remove_writer([object(Logger)]w); 
    } 
    else if(objectp(e->writer) && ([object]e->writer)->is_logger) 
      destruct([object]e->writer); 
  } 
 
  int(0..1) runningp() { return running; } 
} 
 
protected class SubSysPhish { 
 
  constant startdoc = "phish\n" 
    "\tStart the Pike Hilfe Shell.\n"; 
  constant stopdoc = "phish\n\tTurns off phish.\n"; 
 
  protected int(0..1) running; 
  protected int(0..1) in_expr; 
  protected Evaluator e; 
 
  int(0..1) runningp() { return running; } 
 
  protected int(0..1) do_cmd(string cmd) { 
    if(in_expr) { 
      return 0; 
    } 
 
    array c = cmd[..<1]/" "; 
    if(e->commands[c[0]] || cmd==".\n") 
      return 0; 
 
    string src = c[0]+"("; 
    array arg = ({}); 
    for(int i=1; i<sizeof(c); i++) { 
      if(c[i][0]=="\"") { 
        string str = ""; 
        do { 
          str += c[i++]; 
        } while(i+1<sizeof(c) && c[i+1][-1]!="\""); 
        arg += ({ str }); 
      } 
      else 
        arg += ({ "\"" + c[i] + "\"" }); 
    } 
    e->add_buffer(src + arg*"," + ");\n"); 
    return 1; 
  } 
 
  void start(Evaluator _e, array(string) words) { 
    if(running) return; 
    e = _e; 
    e->add_input_hook(do_cmd); 
    running = 1; 
  } 
 
  void stop(Evaluator e, void|array(string) words) { 
    e->remove_input_hook(do_cmd); 
    running = 0; 
  } 
} 
 
// 
// Support stuff.. 
// 
 
 
// General subsystem handler class. 
protected class SubSystems { 
  mapping(string:object) subsystems; 
 
  protected void create () { 
    // Register the subsystems here. 
    subsystems = ([ 
#if constant(thread_create) 
      "backend":SubSysBackend(), 
#endif 
      "logging":SubSysLogger(), 
      "phish":SubSysPhish(), 
    ]); 
  } 
 
  array(string) list() { 
    return indices(subsystems); 
  } 
 
  string doc(int(0..1) startp) { 
    array(string) res = ({}); 
    string what = (startp?"startdoc":"stopdoc"); 
    foreach(sort(indices(subsystems)), string name) 
      res += ({ (startp?" start ":" stop ") + 
        replace(subsystems[name][what], "\t", "    ") }); 
    return res*"\n"; 
  } 
 
  void start(Evaluator e, string what, array(string) words){ 
    if(subsystems[what]) { 
 
      if(!subsystems[what]->runningp()) 
        subsystems[what]->start(e, words); 
      else 
        e->safe_write("%s is already running.\n", what); 
 
    } 
    else 
      e->safe_write("No such subsystem (%O).\n", what); 
  } 
 
  void stop(Evaluator e, string what, array(string) words){ 
    if(subsystems[what]) { 
 
      if(subsystems[what]->runningp()) 
        subsystems[what]->stop(e,words); 
      else 
        e->safe_write("%s is not running.\n", what); 
    } 
    else 
      e->safe_write("No such subsystem (%O).\n", what); 
  } 
} 
 
local { 
 
protected constant whitespace = (< ' ', '\n' ,'\r', '\t' >); 
protected constant termblock = (< "catch", "do", "gauge", "lambda", 
                                  "class stop" >); 
protected constant modifier = (< "extern", "final", "inline", "local", 
                                 "optional", "private", "protected", 
                                 "public", "static", "variant", "deprecated" >); 
 
protected constant types = (< "string", "int", "float", "array", "mapping", 
                              "multiset", "mixed", "object", "program", 
                              "function", "void" >); 
 
// infix token may appear between two literals 
protected constant infix = (< "!=", "%", "%=", "&", "&=", "*", "*=", 
                              "+", "+=", ",", "-", "-=", 
                              "->", "->=", "/", "/=", 
                              "<", "<<", "<<=", "<=", "==", 
                              ">", ">=", ">>", ">>=", 
                              "^", "^=", "|", "|=", "~", "~=", 
                              "&&", "||", "=", "..", "**" >); 
 
// before literal but not after 
protected constant prefix = (< "!", "@", "(", "({", "([", "(<", "[", "{", 
                               "<", ">" >); 
 
// after literal but not before 
protected constant postfix = (< ")", "})", "])", ">)", "]", "}" >); 
 
// before or after literal but not between 
protected constant prepostfix = (< "--", "++" >); 
 
// between two expressions 
protected constant seperator = (< "?", ":", ",", ";" >); 
 
protected constant reference = ([ ".":"module", "->":"object", "[":"array" ]); 
 
protected constant group = ([ "(":")", "({":"})", "([":"])", "(<":">)", 
                              "[":"]", "{":"}" ]); 
 
// Symbols not valid in type expressions. 
// All of the above except ".", "|", "&" and "~". 
protected constant notype = (infix+prefix+postfix+prepostfix+seperator) - 
  (< ".", "|", "&", "~" >); 
 
} 
 
string typeof_token(string|array token) 
{ 
  string type; 
  if (arrayp(token)) 
  { 
    if (token[0] == "(" && token[-1] == ")") 
      type = "argumentgroup"; 
    else if (token[0] == "[" && token[-1] == "]") 
      type = "referencegroup"; 
  } 
  else if ( reference[token] ) 
    type = "reference"; 
  else if ( (token[0]==token[-1] && (< '"', '\'' >)[token[0]]) 
            || token == array_sscanf(token, "%[0-9.]")[0] ) 
    type = "literal"; 
  else if (token == array_sscanf(token, "%[a-zA-Z0-9_]")[0] || token[0]=='`') 
    type = "symbol"; 
    // FIXME: handle unicode chars 
  else if (token == array_sscanf(token, "%[ \t\r\n]")[0]) 
    type = "whitespace"; 
  else if (seperator[token]) 
    type = "seperator"; 
  else if (prefix[token]) 
    type = "prefix"; 
  else if (prepostfix[token]) 
    type = "prepostfix"; 
  else if (postfix[token]) 
    type = "postfix"; 
  else if (infix[token]) 
    type = "infix"; 
  else 
    type = "unknown"; 
  return type; 
} 
 
 
 
//! Represents a Pike expression 
class Expression { 
  protected array(string) tokens; 
  protected mapping(int:int) positions; 
  protected array(int) depths; 
  protected multiset(int) sscanf_depths; 
 
  //! @param t 
  //!   An array of Pike tokens. 
  protected void create(array(string) t) { 
    positions = ([]); 
    depths = allocate(sizeof(t)); 
    sscanf_depths = (<>); 
 
    generate_offsets(t); 
    tokens = t; 
  } 
 
  protected void generate_offsets(array t) 
  { 
    int pos = tokens && sizeof(tokens); 
    int depth; 
 
    foreach(t; int i; string t) { 
      if(!whitespace[t[0]]) 
        positions[pos++] = i; 
 
      if(t=="(") 
        depth++; 
      else if(t==")") 
        depth--; 
      depths[i]=depth; 
 
      if(t=="sscanf") sscanf_depths[depth+1]=1; 
    } 
  } 
 
  //! The number of non-whitespace tokens in the expression. 
  int _sizeof() { 
    return sizeof(positions); 
  } 
 
  //! Returns a token or a token range without whitespaces. 
  string `[](int f, void|int t) { 
    if(f>sizeof(positions) || -f>sizeof(positions)) 
      return 0; 
    if(!t) 
      return tokens[positions[f]]; 
    if(t>=sizeof(positions)) 
      t = sizeof(positions)-1; 
 
    // Negative t not boundary checked. 
    return tokens[positions[f]..positions[t]]*""; 
  } 
 
  //! Replaces a token with a new token. 
  string `[]= (int f, string v) { 
    return tokens[positions[f]] = v; 
  } 
 
  //! Return the parenthesis depth at the given position. 
  int depth(int f) { 
    return depths[positions[f]]; 
  } 
 
  //! Returns 1 if the current position is within a sscanf expression. 
  int(0..1) in_sscanf(int f) { 
    return sscanf_depths[depth(f)]; 
  } 
 
  //! See if there are any forbidden modifiers used in the expression, 
  //! e.g. "private int x;" is not valid inside Hilfe. 
  //! @returns 
  //!   Returns an error message as a string if the expression 
  //!   contains a forbidden modifier, otherwise @expr{0@}. 
  string check_modifiers() { 
    foreach(sort(values(positions)), int pos) 
      if(modifier[tokens[pos]]) 
        return "Hilfe Error: Modifier \"" + tokens[pos] + 
          "\" not allowed on top level in Hilfe.\n"; 
      else 
        return 0; 
    return 0; 
  } 
 
  //! Returns the expression verbatim. 
  string code() { 
    return tokens*""; 
  } 
 
  //! Returns at which position the type declaration that begins at 
  //! position @[position] ends. A return value of -1 means that the 
  //! token or tokens from @[position] can not be a type declaration. 
  int(-1..) endoftype(int(-1..) position) { 
    string t = `[](position); 
    if(types[ t ] ) { 
      // We are in a type declaration. 
      position++; 
 
      t = `[](position); 
      if( !(< "(", "|" >)[t] ) 
        return position-1; 
 
      if( t=="(" ) { 
        int plevel; 
        for(; position<sizeof(positions); position++) { 
          t = `[](position); 
          if( t=="(" ) plevel++; 
          if( t==")" ) plevel--; 
          if( !plevel ) { 
            if( `[](position+1)=="|" ) 
              return endoftype(position+2); 
            return position; 
          } 
          // We will not index outside the array, 
          // since "|" can't be the last entry. 
 
          if( t=="|" ) position++; 
        } 
      } 
 
      if( t=="|" ) 
        return endoftype(position+1); 
 
      return position; 
    } 
 
    // Any sequence beginning with any of these can't be 
    // a type declaration. 
    if( (< "break", "continue", "class", "!", "-", 
           "(", "~", "[", "`", "else", ".." >)[ t ] ) 
      return -1; 
    if( t[0]>='0' && t[0]<='9' ) 
      return -1; 
    if( notype[ t ] ) 
      return -1; 
 
    if( t=="." ) position++; 
 
    for(; position<sizeof(positions); position++) { 
      if( notype[ `[](position+1) ] ) 
        return -1; 
      if( `[](position+1)=="." ) { 
        position++; 
        continue; 
      } 
 
      if( (< "|", "&" >)[`[](position+1)] ) { 
        return endoftype(position+2); 
      } 
 
      return position; 
    } 
 
    // Well, we did find a type declaration, but it 
    // wasn't used for anything except perhaps loading 
    // a module into pike, e.g. "_Roxen;" 
    return -1; 
  } 
 
  //! Returns the position of the matching parenthesis of the given 
  //! kind, starting from the given position. The position should be 
  //! the position after the opening parenthesis, or later. Assuming 
  //! balanced code. Returns -1 on failure. 
  int(-1..) find_matching(string token, int(0..)|void pos) { 
    string find = ([ "(":")", "{":"}", "[":"]", 
                     "({":"})", "([":"])", "(<":">)" ])[token]; 
    int plevel=1; 
    while( pos<sizeof(positions) ) { 
      if( `[](pos)==token ) plevel++; 
      if( `[](pos)==find ) plevel--; 
      if(!plevel) return pos; 
      pos++; 
    } 
    return -1; 
  } 
 
  //! Is there a block starting at @[pos]? 
  int(0..1) is_block(int pos) { 
 
    if( (< "for", "foreach", "switch", "while", "lambda", "class", 
           "do", "gauge", "catch" >)[ `[](pos) ] ) 
      return 1; 
 
    if( `[](pos+1)=="{" && `[](pos)!="(") 
      return 1; 
    if( `[](pos+1)!="(" ) 
      return 0; 
    pos = find_matching("(", pos+2); 
    if( pos==-1 ) 
      return 0; 
    if( `[](pos+1)=="{" ) 
      return 1; 
    return 0; 
  } 
 
  //! An Expression object can be cast to an array or a string. In 
  //! both forms all tokens, including white spaces will be returned. 
  protected mixed cast(string to) 
  { 
    switch(to) 
    { 
    case "array": return tokens; 
    case "string": return code(); 
    } 
    return UNDEFINED; 
  } 
 
  protected string _sprintf(int t) { 
    return t=='O' && sprintf("%O(%O)", this_program, tokens); 
  } 
} 
 
//! In every Hilfe object (@[Evaluator]) there is a ParserState object 
//! that manages the current state of the parser. Essentially tokens are 
//! entered in one end and complete expressions are output in the other. 
//! The parser object is accessible as ___Hilfe->state from Hilfe expressions. 
protected class ParserState { 
  protected ADT.Stack pstack = ADT.Stack(); 
  protected constant starts = ([ ")":"(", "}":"{", "]":"[", 
                               ">)":"(<", "})":"({", "])":"([" ]); 
  protected array(string) pipeline = ({ }); 
  protected array(Expression) ready = ({ }); 
  protected string last; 
  protected string block; 
 
  protected mapping low_state = ([]); 
 
  //! Feed more tokens into the state. 
  void feed(array(string) tokens) { 
    foreach(tokens, string token) { 
      // comments 
      if(sizeof(token)>1 && (token[0..1]=="//" || token[0..1]=="/*")) continue; 
 
      pipeline += ({ token }); 
 
      // If we start a block at the uppermost level, what kind of block is it? 
      if(!sizeof(pstack)) 
        switch(token) 
        { 
        case "{": 
          if(!block) 
            block = last; 
          else if(block=="class" && last=="class") 
            block = "class stop"; 
          pstack->push(token); 
          continue; 
 
        case "lambda": 
          block = token; 
          last = token; 
          continue; 
 
        case "class": 
          if(sizeof(pipeline)>1) { 
            // Kludge to get "object foo=class{}();" past the kludge below. 
            block = "class stop"; 
          } 
          else { 
            // Kludge to get "class A {}" to work without semicolon. 
            block = token; 
          } 
          last = token; 
          continue; 
        } 
 
      // Do we begin any kind of parenthesis level? 
      if(token=="(" || token=="{" || token=="[" || 
         token=="(<" || token=="({" || token=="([") { 
        pstack->push(token); 
        continue; 
      } 
 
      // Do we end any kind of parenthesis level? 
      if(token==")" || token=="}" || token=="]" || 
         token==">)" || token=="})" || token=="])" ) { 
        if(!sizeof(pstack)) 
           throw(sprintf("%O end parenthesis without start parenthesis.", 
                         token)); 
        if(pstack->top()!=starts[token]) 
           throw(sprintf("%O end parenthesis does not match closest start " 
                         "parenthesis %O.", 
                         token, pstack->top())); 
        pstack->pop(); 
      } 
 
      // expressions 
      if(token==";" && !sizeof(pstack)) { 
        ready += ({ Expression(pipeline) }); 
        pipeline = ({}); 
        block = 0; 
        continue; 
      } 
 
      // If we end a block at the uppermost level, and it doesn't need a ";", 
      // then we can move out that block of the pipeline. 
      if(token=="}" && !sizeof(pstack) && !termblock[block]) { 
        ready += ({ Expression(pipeline) }); 
        pipeline = ({}); 
        block = 0; 
        continue; 
      } 
 
      // Preprocessor 
      if( token[0]=='#' ) { 
        string tmp = token-" "; 
        if( has_prefix(tmp, "#error") && !sizeof(pstack)) { 
          ready += ({ Expression( pipeline ) }); 
          pipeline = ({}); 
          block = 0; 
          continue; 
        } 
      } 
 
      if(!whitespace[token[0]]) 
        last = token; 
    } 
  } 
 
  //! Read out completed expressions. Returns an array where every element 
  //! is an expression represented as an array of tokens. 
  array(Expression) read() { 
    array(Expression) ret = ({}); 
 
    for(int pos; pos<sizeof(ready); pos++) 
    { 
      Expression expr = ready[pos]; 
      if(expr[0]==";") continue; 
      if( expr[0]=="if" ) 
      { 
        if( pos<sizeof(ready)-1 && ready[pos+1][0]=="else" ) 
          while( pos<sizeof(ready)-1 && ready[pos+1][0]=="else" ) 
            expr = Expression( (array)expr + (array)ready[++pos] ); 
        else 
          expr = Expression( (array)expr+({ " else ___Hilfe->last_else=1;" })); 
      } 
      else if( expr[0]=="else" ) 
        expr = Expression( ({ "if", "(___Hilfe->last_else);" })+(array)expr ); 
 
      ret += ({ expr }); 
    } 
 
    ready = ({}); 
    return ret; 
  } 
 
  protected string caught_error; 
 
  //! Prints out any error that might have occurred while 
  //! @[push_string] was executed. The error will be 
  //! printed with the print function @[w]. 
  void show_error(function(array(string)|string, mixed ... : int) w) { 
    if(!caught_error) return; 
    w("Hilfe Error: %s", caught_error); 
    caught_error = 0; 
  } 
 
  //! Sends the input @[line] to @[Parser.Pike] for tokenization, 
  //! but keeps a state between each call to handle multiline 
  //! /**/ comments and multiline #"" strings. 
  array(string) push_string(string line) { 
    array(string) tokens; 
    array err; 
    if(sizeof(line) && line[0]=='#') { 
      string tmp = line-" "; 
      if( has_prefix(tmp, "#define") || has_prefix(tmp, "#undef") ) { 
        caught_error = "Preprocessor defines not possible inside Hilfe.\n"; 
        return 0; 
      } 
      // FIXME: It should be possible to add charset support, but 
      // would it be worth it? 
      if( has_prefix(tmp, "#charset") ) { 
        caught_error = "Preprocessor charset declaration not possible " 
          "inside Hilfe.\n"; 
        return 0; 
      } 
      // FIXME: We would like #pike to work. 
      if( has_prefix(tmp, "#pike") ) { 
        caught_error = "Version selection does not work inside Hilfe.\n"; 
        return 0; 
      } 
      if( has_prefix(tmp, "#pragma") ){ 
        caught_error = "Pragma does not work inside Hilfe.\n"; 
        return 0; 
      } 
      if( has_prefix(tmp, "#if") || has_prefix(tmp, "#elif") || 
          has_prefix(tmp, "#else") || has_prefix(tmp, "#endif") ) { 
        caught_error = "Preprocess instructions if, elif, ifdef, ifndef " 
          "else and endif not possible in Hilfe.\n"; 
        return 0; 
      } 
    } 
    if(err = catch( tokens = Parser.Pike.split(line, low_state) )) { 
      caught_error = err[0]; 
      return 0; 
    } 
    return tokens; 
  } 
 
  //! Returns true if there is any waiting expression that can be fetched 
  //! with @[read]. 
  int datap() { 
    if(sizeof(pipeline)==1 && whitespace[pipeline[0][0]]) 
      pipeline = ({}); 
    return sizeof(ready); 
  } 
 
  //! Are we in the middle of an expression. Used e.g. for changing the 
  //! Hilfe prompt when entering multiline expressions. 
  int(0..1) finishedp() { 
    if(sizeof(pstack)) return 0; 
    if(low_state->remains) return 0; 
    if(!sizeof(pipeline)) return 1; 
    if(sizeof(pipeline)==1 && whitespace[pipeline[0][0]]) { 
      pipeline = ({}); 
      return 1; 
    } 
    return 0; 
  } 
 
  //! Clear the current state. 
  void flush() { 
    pstack->reset(); 
    pipeline = ({}); 
    ready = ({}); 
    last = 0; 
    block = 0; 
    low_state = ([]); 
  } 
 
  //! Returns the current parser state. Used by "dump state". 
  string status() { 
    string ret = "Current parser state\n"; 
    ret += sprintf("Parenthesis stack: %s\n", pstack->arr[..sizeof(pstack)]*" "); 
    ret += sprintf("Current pipeline: %O\n", pipeline); 
    ret += sprintf("Last token: %O\n", last); 
    ret += sprintf("Current block: %O\n", block); 
    return ret; 
  } 
} 
 
//! In every Hilfe object (@[Evaluator]) there is a HilfeHistory 
//! object that manages the result history. That history object is 
//! accessible both from __ and ___Hilfe->history in Hilfe expressions. 
protected class HilfeHistory { 
 
  inherit ADT.History; 
 
  // Add content overview 
  string status() { 
    string ret = ""; 
    int abs_num = get_first_entry_num(); 
    int rel_num = -_sizeof(); 
    for(abs_num; abs_num<get_latest_entry_num()+1; abs_num++) 
      ret += sprintf(" %2d (%2d) : %s\n", abs_num, rel_num++, 
                     replace(sprintf("%O", `[](abs_num)), "\n", 
                             "\n           ")); 
    ret += sprintf("%d out of %d possible entries used.\n", 
                   _sizeof(), get_maxsize()); 
    return ret; 
  } 
 
  // Give better names in backtraces. 
  protected mixed `[](int i) { 
    mixed ret; 
    array err = catch( ret = ::`[](i) ); 
    if(err) 
      error(err[0]); 
    return ret; 
  } 
 
  // Give the object a better name. 
  protected string _sprintf(int t) { 
    return t=='O' && sprintf("%O(%d/%d)", this_program, 
                             _sizeof(), get_maxsize() ); 
  } 
} 
 
 
// 
// The actual Hilfe 
// 
 
//! This class implements the actual Hilfe interpreter. It is accessible 
//! as ___Hilfe from Hilfe expressions. 
class Evaluator { 
 
  //! This mapping contains the available Hilfe commands, including the 
  //! built in ones (dump, exit, help, new, quit), so it is possible to 
  //! replace or remove them. The name of a command should be 10 
  //! characters or less. 
  mapping(string:Command) commands = ([]); 
 
  //! Keeps the state, e.g. multiline input in process etc. 
  ParserState state = ParserState(); 
 
  //! The locally defined variables (name:value). 
  mapping(string:mixed) variables; 
 
  //! The types of the locally defined variables (name:type). 
  mapping(string:string) types; 
 
  //! The locally defined constants (name:value). 
  mapping(string:mixed) constants; 
 
  //! The locally defined functions (name:value). 
  mapping(string:function) functions; 
 
  //! The locally defined programs (name:value). 
  mapping(string:program) programs; 
 
  //! The current imports. 
  array(string) imports; 
 
  //! The current inherits. 
  array(string) inherits; 
 
  //! The current result history. 
  HilfeHistory history = HilfeHistory(10); 
 
  //! The function to use when writing to the user. 
  array|object|function(string : int(0..)) write; 
 
  //! Adds another output function. 
  void add_writer(object|function(string : int(0..)) new) { 
    if(arrayp(write)) 
      write += ({ new }); 
    else if (write) 
      write = ({ write, new }); 
    else 
      write = new; 
  } 
 
  //! Removes an output function. 
  void remove_writer(object|function old) { 
    if(arrayp(write)) 
      write -= ({ old }); 
    else 
      write = 0; 
  } 
 
  //! An output method that shouldn't crash. 
  int safe_write(array(string)|string in, mixed ... args) { 
    if(!write) return 0; 
    mixed err = catch { 
      if(arrayp(in)) in *= ""; 
      if(sizeof(args)) 
        in = sprintf(in, @args); 
      if (String.width(in) > 8) { 
        // Variable and function names may contain wide characters... 
        // Perform Unicode escaping. 
        in = map(in/"", 
                 lambda(string s) { 
                   if (String.width(s) <= 8) return s; 
                   int c = s[0] & 0xffffffff; 
                   if (c <= 0xffff) return sprintf("\\u%04x", c); 
                   return sprintf("\\U%08x", c); 
                 }) * ""; 
      } 
      int ret = write(in); 
      if(!ret && sizeof(in)) return sizeof(in); 
      return ret; 
    }; 
    catch { 
      write("HilfeError: Error while outputting data.\n"); 
      write(describe_backtrace(err)); 
      return 0; 
    }; 
    werror("HilfeError: Error while outputting data.\n"); 
    return 0; 
  } 
 
  protected array input_hooks = ({}); 
 
  //! Adds a function to the input hook, making 
  //! all user data be fed into the function. 
  //! @seealso 
  //!  @[remove_input_hook] 
  void add_input_hook(function|object new) { 
    input_hooks += ({ new }); 
  } 
 
  //! Removes a function from the input hook. 
  //! @seealso 
  //!  @[add_input_hook] 
  void remove_input_hook(function|object old) { 
    input_hooks -= ({ old }); 
  } 
 
  protected void create() 
  { 
    print_version(); 
    commands->set = CommandSet(); 
    commands->exit = CommandExit(); 
    commands->quit = commands->exit; 
    commands->help = CommandHelp(); 
    commands->doc = CommandDoc(); 
    commands->dump = CommandDump(); 
    commands->new = CommandNew(); 
    commands->hej = CommandHej(); 
    commands->look = CommandDot(); 
    commands->start = CommandStartStop(); 
    commands->stop = commands->start; 
    reset_evaluator(); 
  } 
 
  //! Displays the current version of Hilfe. 
  void print_version() 
  { 
    safe_write(version()+ 
              " running Hilfe v3.5 (Incremental Pike Frontend)\n"); 
    int major = master()->compat_major; 
    int minor = master()->compat_minor; 
    if( major!=-1 || minor!=-1 ) 
      safe_write("(running in Pike %d.%d compat mode)\n", major, minor); 
  } 
 
  //! Clears the current state, history and removes all locally 
  //! defined variables, constants, functions and programs. Removes 
  //! all imports and inherits. It does not reset the command mapping 
  //! nor reevaluate the .hilferc file. 
  void reset_evaluator() { 
    state->flush(); 
    history->flush(); 
    variables = ([]); 
    types = ([]); 
    constants = ([]); 
    functions = ([]); 
    programs = ([]); 
    imports = ({}); 
    inherits = ({}); 
  } 
 
  //! Input a line of text into Hilfe. It checks if @[s] is 
  //! ".", in which case it calls state->flush(). Otherwise 
  //! just calls add_buffer. 
  void add_input_line(string s) 
  { 
    if( sizeof(input_hooks) && `+(@input_hooks(s+"\n")) ) 
      return; 
 
    if(s==".") 
    { 
      state->flush(); 
      return; 
    } 
 
    add_buffer(s); 
  } 
 
  //! Add buffer tokenizes the input string and determines if the 
  //! new line is a Hilfe command. If not, it updates the current 
  //! state with the new tokens and sends any and all complete 
  //! expressions to evaluation in @[parse_expression]. 
  void add_buffer(string s) 
  { 
    // Tokenize the input 
    int(0..1) finished = state->finishedp(); 
    array(string) tokens = state->push_string(s+"\n"); 
    array(string) words = s/" "; 
    string command = words[0]; 
 
    if(finished) 
    { 
      // See if first token is a command and not a defined entity. 
      if(commands[command] && !has_index(constants, command) && 
         !has_index(variables, command) && !has_index(functions, command) && 
         (sizeof(words)==1 || words[1]!=";")) { 
        commands[command]->exec(this, s, words, tokens); 
        return; 
      } 
 
      // See if the command is executed in overridden mode. 
      if(sizeof(command) && command[0]=='.') { 
        command = command[1..]; 
        if(commands[command]) { 
          commands[command]->exec(this, s, words, tokens); 
          return; 
        } 
      } 
    } 
 
    if(!tokens) { 
      state->show_error(safe_write); 
      return; 
    } 
 
    // Push new tokens into our state. 
    mixed err = catch( state->feed(tokens) ); 
    if(err) { 
      if(stringp(err)) 
        safe_write("Hilfe Error: %s\n", err); 
      else 
        safe_write(describe_backtrace(err)); 
      state->flush(); 
    } 
 
    // See if any complete expressions came out on the other side. 
    if(state->datap()) 
      foreach(state->read(), Expression expression) { 
        string ret = parse_expression(expression); 
        if(ret) safe_write(ret); 
      } 
  } 
 
 
  // 
  // 
  // Parser code 
  // 
  // 
 
  protected int(0..1) hilfe_error(mixed err) { 
    if(!err) return 1; 
    mixed err2 = catch { 
      if( (arrayp(err) && sizeof(err)==2 && stringp(err[0])) || 
          (objectp(err) && err->backtrace) ) { 
        array files = map(reverse(err[1]), lambda(mixed in) { 
                                          if(in) return in[0]; 
                                          return 0; 
                                        }); 
        int pos = search(files, "HilfeInput"); 
        safe_write(describe_backtrace( ({ err[0], 
                                          err[1][sizeof(err[1])-pos..] }) )); 
     } else 
        safe_write("Hilfe Error: Unknown format of thrown error " 
                   "(not backtrace).\n(%O)\n", err); 
    }; 
    if(err2) 
      safe_write("Hilfe Error: Error while printing backtrace.\n"); 
    return 0; 
  } 
 
  protected void add_hilfe_constant(string code, string var) { 
    if(object o = hilfe_compile("constant " + code + 
                                ";\nmixed ___HilfeWrapper() { return " + 
                                var + "; }", var)) { 
      hilfe_error( catch( constants[var] = o->___HilfeWrapper() ) ); 
    } 
  } 
 
  protected void add_hilfe_variable(string type, string code, string var) { 
    int(0..1) existed; 
    mixed old_value; 
    if(has_index(variables, var)) { 
      old_value = m_delete(variables, var); 
      existed = 1; 
    } 
 
    object o = hilfe_compile(type + " " + code + 
                             ";\nmixed ___HilfeWrapper() { return " + 
                             var + "; }", var); 
 
    if( o && hilfe_error( catch( variables[var] = 
                                 o->___HilfeWrapper() ) ) ) { 
      types[var] = type; 
    } 
    else if(existed) 
      variables[var] = old_value; 
  } 
 
  protected void add_hilfe_entity(string type, string code, 
                                string var, mapping vtype) { 
    int(0..1) existed; 
    mixed old_value; 
    if(vtype[var]) { 
      old_value = m_delete(vtype, var); 
      existed = 1; 
    } 
 
    object o = hilfe_compile(type + " " + code + 
                             ";\nmixed ___HilfeWrapper() { return " + 
                             var + "; }", var); 
 
    if( o && hilfe_error( catch( vtype[var] = o->___HilfeWrapper() ) ) ) 
      return; 
 
    if(existed) 
      vtype[var] = old_value; 
  } 
 
  // Rewrites "dangerous" tokens (int/string/float-variables) to 
  // operate directly in the variable mapping. It rewrites all other 
  // variables as well, but we didn't have to. 
  protected int rel_parser( Expression expr, multiset(string) symbols, 
                          void|int p ) { 
    int top = !p; 
    while( p<sizeof(expr)) { 
      if( expr->is_block(p) ) { 
        string type = expr[p++]; 
        if(type=="class") top=0; 
        multiset(string) new_scope = symbols+(<>); 
 
        // No () for catch, gauge, do and possibly class. 
        // Get variable names for next scope clobber. 
        if(expr[p]=="(") { 
          p++; 
 
          switch(type) { 
 
          case "foreach": 
            p = relocate(expr, symbols, new_scope, p, ","); 
            if(expr[p]==";") { 
              p++; 
              p = relocate(expr, symbols, new_scope, p); 
            } 
            p++; 
            p = relocate(expr, symbols, new_scope, p); 
            p++; 
            break; 
 
          case "for": 
            p = relocate(expr, symbols, new_scope, p); 
            p++; 
            p = relocate(expr, new_scope, new_scope, p); 
            p++; 
            p = relocate(expr, new_scope, new_scope, p); 
            p++; 
            break; 
 
          // FIXME: Detect named lambdas. 
 
          case "lambda": 
          case "class": // Unnamed class 
          default: // Function definition 
            while(expr[p-1]!=")") { 
              p = relocate(expr, symbols, new_scope, p, ","); 
              p++; 
            } 
            break; 
          } 
 
        } 
        else if( type=="class" && expr[p+1]=="(" ) { 
          p += 2; // Skip class name 
          while(expr[p-1]!=")") { 
            p = relocate(expr, symbols, new_scope, p, ","); 
            p++; 
          } 
        } 
 
        if(expr[p]=="{") { 
          p++; 
          p = rel_parser(expr, new_scope, p); 
          p++; 
          continue; 
        } 
 
        p = relocate(expr, new_scope, 0, p); 
        p++; 
        continue; 
      } 
 
      if(expr[p]=="}") 
        return p; 
 
      // expr is an expression 
      p = relocate(expr, symbols, symbols, p, 0, top); 
      p++; 
    } 
    return p; 
  } 
 
#if 0 
  // Some debug code to intercept calls to relocate. Aspect 
  // Oriented Programming would be handy here... 
  protected int relocate( Expression expr, multiset(string) symbols, 
                        multiset(string) next_symbols, int p, 
                        void|string safe_word, void|int(0..1) top) { 
    int op = p; 
    werror("%O %O %d\n", (symbols?indices(symbols||(<>))*", ":0), 
           (next_symbols?indices(next_symbols||(<>))*", ":0), top ); 
    p = _relocate(expr, symbols, next_symbols, p, safe_word, top); 
    werror(" relocate %O %O\n", op, expr[op..p]); 
    return p; 
  } 
#endif 
 
  protected int relocate( Expression expr, multiset(string) symbols, 
                        multiset(string) next_symbols, int p, 
                        void|string safe_word, void|int(0..1) top) { 
    // Type declaration? 
    int pos = expr->endoftype(p); 
    if(pos>=0) { 
      pos++; 
 
      if( (< ";", ",", "=", ")" >)[expr[pos+1]]) { 
        // We are declaring the variable expr[pos] 
        while(pos<sizeof(expr)) { 
          int from = pos; 
          int plevel; 
          while( !(< ",", ";",")" >)[expr[pos]] || plevel ) { 
            if(expr[pos]=="(" || expr[pos]=="{") plevel++; 
            else if(expr[pos]==")" || expr[pos]=="}") plevel--; 
 
            pos++; 
            if(pos==sizeof(expr)) { 
              // Something went wrong. End relocation completely. 
              werror("Variable declaration detection in relocation broke.\n"); 
              return pos; 
            } 
          } 
 
          // Relocate symbols in the variable assignment. 
          for(int i=from+1; i<pos; i++){ 
 
            string t = expr[i]; 
 
            if(t==".") { 
              i++; 
              continue; 
            } 
 
            if(t=="lambda") { 
              int d = expr->depth(i); 
              do { 
                i++; 
                if( i==pos ) break; 
              } while( expr->depth(++i)>d ); 
              continue; 
            } 
 
            if(expr->in_sscanf(i)) { 
              int nv = expr->endoftype(i); 
              if(nv>=0) { 
                if(top) { 
                  string var = expr[nv+1]; 
                  types[var] = expr[i..nv]; 
                  variables[var] = 0; 
                  symbols[var] = 1; 
                  for(int j=i; j<=nv; j++) 
                    expr[j]=""; 
                  i=nv; 
                  continue; 
                } 
                else { 
                  i=nv+1; 
                  continue; 
                } 
              } 
            } 
 
            if(symbols[t]) { 
              if(types[t]) 
                expr[i] = "(([mapping(string:"+types[expr[i]]+")]" 
                  "(mixed)___hilfe)->" + t + ")"; 
              else 
                expr[i] = "(___hilfe->"+t+")"; 
            } 
          } 
 
          if(next_symbols) 
            next_symbols[expr[from]] = 0; 
          else 
            symbols[expr[from]] = 0; 
 
          if(top) 
            symbols[expr[from]] = 1; 
 
          if( expr[pos]==safe_word || (< ";", ")" >)[expr[pos]] || 
              plevel<0 ) 
            return pos; 
 
          pos++; 
        } 
      } 
      else { 
        // We are declaring a function. Take one step back so that the 
        // function name will be the first token to is_block. 
        pos--; 
      } 
      return pos; 
    } 
 
    int plevel; 
    for( ; p<sizeof(expr); p++) { 
      string t = expr[p]; 
 
      if( (< "(", "{" >)[t] ) { 
        plevel++; 
        continue; 
      } 
 
      if( (< "}", ")" >)[t] ) { 
        if(!plevel) return p; 
        plevel--; 
        continue; 
      } 
 
      if( t=="{" ) 
        error("HilfeError: Error in relocation parser (relocate:'}')"); 
 
      if(t=="lambda") return p-1; 
 
      if( !plevel && (t==safe_word || t==";") ) 
        return p; 
 
      if(expr->in_sscanf(p)) { 
        int nv = expr->endoftype(p); 
        if(nv>=0) { 
          if(top) { 
            string var = expr[nv+1]; 
            types[var] = expr[p..nv]; 
            variables[var] = 0; 
            symbols[var] = 1; 
            for(int j=p; j<=nv; j++) 
              expr[j]=""; 
            p=nv; 
            continue; 
          } 
          else { 
            p=nv+1; 
            continue; 
          } 
        } 
      } 
 
      // Rewrite variable 
      if(symbols[t]) { 
        if(types[expr[p]]) 
          expr[p] = "(([mapping(string:"+types[expr[p]]+")]" 
            "(mixed)___hilfe)->" + expr[p] + ")"; 
        else 
          expr[p] = "(___hilfe->"+expr[p]+")"; 
        continue; 
      } 
 
      // Skip tokens preceded by . or -> 
      if( (< ".", "->" >)[t] ) { 
        p++; 
        continue; 
      } 
    } 
 
    return p; 
  } 
 
  //! Parses a Pike expression. Returns 0 if everything went well, 
  //! or a string with an error message otherwise. 
  string parse_expression(Expression expr) 
  { 
    // Check for modifiers 
    expr->check_modifiers(); 
 
    // Rewrite variables for the Hilfe wrapper 
    rel_parser(expr, (multiset)(indices(variables)) ); 
 
    switch(expr[0]) 
    { 
      case "if": 
      case "else": 
      case "for": 
      case "do": 
      case "while": 
      case "foreach": 
      case "switch": 
        // Value-less statements 
        evaluate(expr->code(), 0); 
        return 0; 
 
      case "inherit": 
      { 
        inherits += ({ expr[1..<1] }); 
        if(!hilfe_compile("")) 
          inherits = inherits[..<1]; 
        return 0; 
      } 
 
      case "import": 
      { 
        imports += ({ expr[1..<1] }); 
        if(!hilfe_compile("")) 
          imports = imports[..<1]; 
        return 0; 
      } 
 
      case "constant": 
      { 
        int pos = 1; 
 
        while(pos<sizeof(expr)) { 
          int from = pos; 
          int plevel; 
          while((expr[pos]!="," && expr[pos]!=";") || plevel) { 
            if(expr[pos]=="(") plevel++; 
            else if(expr[pos]==")") plevel--; 
            pos++; 
            if(pos==sizeof(expr)) 
              return "Hilfe Error: Bug in constant handling. " 
                "Please report this!\n"; 
          } 
          add_hilfe_constant(expr[from..pos-1], expr[from]); 
          pos++; 
        } 
 
        return 0; 
      } 
 
      case "class": 
 
        if(expr[1]=="{") { 
          // Unnamed class ("class {}();"). 
          evaluate("return " + expr->code(), 1); 
        } 
        else 
          add_hilfe_entity("class", expr[1..], expr[1], programs); 
        return 0; 
    } 
 
    int pos = expr->endoftype(0); 
    if(pos>=0) { 
      string type = expr[..pos++]; 
 
      if( (< ";", ",", "=" >)[expr[pos+1]] ) { 
        // We are declaring the variable expr[pos] 
        while(pos<sizeof(expr)) { 
          int from = pos; 
          int plevel; 
          while((expr[pos]!="," && expr[pos]!=";") || plevel) { 
            if(expr[pos]=="(" || expr[pos]=="{") plevel++; 
            else if(expr[pos]==")" || expr[pos]=="}") plevel--; 
            pos++; 
            if(pos==sizeof(expr)) 
              return "Hilfe Error: Bug in variable handling or error " 
                "in variable assignment.\n"; 
          } 
          if(constants[expr[from]]) 
            return "Hilfe Error: \"" + expr[from] + 
              "\" already defined as constant.\n"; 
          add_hilfe_variable(type, expr[from..pos-1], expr[from]); 
          pos++; 
        } 
        return 0; 
      } 
 
      if( expr[pos+1]=="(" ) { 
        // We are defining the function expr[pos] 
        if(constants[expr[pos]]) 
          return "Hilfe Error: \"" + expr[pos] + 
            "\" already defined as constant.\n"; 
        add_hilfe_entity(type, expr[pos..], expr[pos], functions); 
        return 0; 
      } 
    } 
 
    // parse expressions 
    evaluate("return " + expr->code(), 1); 
    return 0; 
  } 
 
 
  // 
  // 
  // Compilation/Evaluation code 
  // 
  // 
 
  //! The last created wrapper in which an expression was evaluated. 
  string last_compiled_expr; 
 
  //! The last compile time; 
  int(0..) last_compile_time; 
 
  //! The last evaluation time; 
  int(0..) last_eval_time; 
 
  //! Strict types? 
  int(0..1) strict_types; 
 
  //! Show warnings? 
  int(0..1) warnings = 1; 
 
  //! The current trace level. 
  int trace_level; 
#if constant(Debug.assembler_debug) 
  //! The current assembler debug level. 
  //! Only available if Pike is compiled with RTL debug. 
  int assembler_debug_level; 
#endif 
#if constant(Debug.compiler_trace) 
  //! The current compiler trace level. 
  //! Only available if Pike is compiled with RTL debug. 
  int compiler_trace_level; 
#endif 
#if constant(_debug) 
  //! The current debug level. 
  //! Only available if Pike is compiled with RTL debug. 
  int debug_level; 
#endif 
 
  //! Should an else expression be carried out? 
  int(0..1) last_else; 
 
  //! The standard @[reswrite] function. 
  void std_reswrite(function w, string sres, int num, mixed res) { 
    if(!sres) 
      w("Ok.\n"); 
    else 
      w( "(%d) Result: %s\n", num, 
         replace(sres, "\n", "\n           "+(" "*sizeof(""+num))) ); 
  } 
 
  //! The function used to write results. 
  //! Gets as arguments in order: The safe_write function 
  //! (function(string, mixed ...:int), the result as a string (string), 
  //! the history entry number (int), the result (mixed), the compilation 
  //! time (int) and the evaluation time (int). If the evaluated expression 
  //! didn't return anything (e.g. a for loop) then 0 will be given as the 
  //! result string. 
  function reswrite = std_reswrite; 
 
 
  protected string hch_errors = ""; 
  protected string hch_warnings = ""; 
 
  protected class HilfeCompileHandler (int stack_level) { 
 
    protected void create() { 
      hch_errors = ""; 
      hch_warnings = ""; 
    } 
 
    mapping(string:mixed) hilfe_symbols; 
 
    mapping(string:mixed) get_default_module() { 
      object compat = get_active_compilation_handler(); 
      if (compat?->get_default_module) { 
        // Support things like @expr{7.4::rusage}. 
        return (compat->get_default_module()||all_constants()) + hilfe_symbols; 
      } 
      return all_constants() + hilfe_symbols; 
    } 
 
    string format(string file, int line, string err) { 
      if(file=="HilfeInput") 
        file = ""; 
      else 
        file = master()->trim_file_name(file)+":"; 
      if(err[-1]!='\n') err += "\n"; 
      string linestr = line?(string)line:"-"; 
      return sprintf(": %s%s: %s", file, linestr, err); 
    } 
 
    void compile_error(string file, int line, string err) { 
      hch_errors += "Compiler Error" + format(file, line, err); 
    } 
 
    void compile_warning(string file, int line, string warn) { 
      hch_warnings += "Compiler Warning" + format(file, line, warn); 
    } 
 
    int compile_exception (object|array trace) 
    { 
      if (objectp (trace) && trace->is_cpp_or_compilation_error) 
        // Backtraces for errors thrown by cpp(), compile() or a 
        // compile callback are normally not interesting. Return zero 
        // to let the caller format an error message instead. 
        return 0; 
 
      catch { 
        trace = ({trace[0], trace[1][stack_level + 1..]}); 
        if (trace[1][0][0] == "Optimizer") 
          // When the compiler evaluates constants there's a 
          // somewhat odd frame "Optimizer:0 0()" at the top. 
          trace[1] = trace[1][1..]; 
      }; 
      hch_errors += "Compiler Exception: " + describe_backtrace (trace); 
      return 1; 
    } 
 
    void show_errors() { 
      if (sizeof(hch_errors)) 
        safe_write(hch_errors); 
    } 
 
    void show_warnings() { 
      if (sizeof(hch_warnings)) 
        safe_write(hch_warnings); 
    } 
  } 
 
  //! Creates a wrapper and compiles the pike code @[f] in it. 
  //! If a new variable is compiled to be tested, its name 
  //! should be given in @[new_var] so that magically defined 
  //! entities can be undefined and a warning printed. 
  object hilfe_compile(string f, void|string new_var) 
  { 
    if(new_var && commands[new_var]) 
      safe_write("Hilfe Warning: Command %O no longer reachable. " 
                 "Use %O instead.\n", 
                 new_var, "."+new_var); 
 
    if(new_var=="___hilfe" || new_var=="___Hilfe" || 
       new_var=="___HilfeWrapper" ) { 
      safe_write("Hilfe Error: Symbol %O must not be defined.\n" 
                "             It is used internally by Hilfe.\n", new_var); 
      return 0; 
    } 
 
    if(new_var=="=") { 
      safe_write("Hilfe Error: No variable name specified.\n"); 
      return 0; 
    } 
 
    mapping symbols = constants + functions + programs; 
 
    if(new_var=="__") 
      safe_write("Hilfe Warning: History variable __ is no " 
                 "longer reachable.\n"); 
    else if(!has_index(symbols, "__") && !has_index(variables, "__")) { 
      symbols["__"] = history; 
    } 
 
    if(new_var=="_") 
      safe_write("Hilfe Warning: History variable _ is no " 
                 "longer reachable.\n"); 
    else if(!has_index(symbols, "_") && !has_index(variables, "_") 
            && sizeof(history)) { 
      symbols["_"] = history[-1]; 
    } 
 
    string prog; 
    int major = master()->compat_major; 
    int minor = master()->compat_minor; 
    if( major!=-1 || minor!=-1 ) 
      prog = sprintf("#pike %d.%d\n", major, minor); 
    else 
      prog = ""; 
 
    if(strict_types) 
      prog += "#pragma strict_types\n"; 
 
    prog += 
      map(inherits, lambda(string f) { return "inherit "+f+";\n"; }) * "" + 
 
      map(imports, lambda(string f) { return "import "+f+";\n"; }) * "" + 
 
      "mapping(string:mixed) ___hilfe = ___Hilfe->variables;\n# 1\n" + f + 
      "\n"; 
 
    HilfeCompileHandler handler = HilfeCompileHandler (sizeof (backtrace())); 
 
    handler->hilfe_symbols = symbols; 
    handler->hilfe_symbols->___Hilfe = this; 
    handler->hilfe_symbols->write = safe_write; 
 
    last_compiled_expr = prog; 
    program p; 
    mixed err; 
 
#if constant(Debug.assembler_debug) 
    if(assembler_debug_level) 
      Debug.assembler_debug(assembler_debug_level); 
#endif 
#if constant(Debug.compiler_trace) 
    if(compiler_trace_level) 
      Debug.compiler_trace(compiler_trace_level); 
#endif 
 
    float compile_time = gauge { 
      err = catch(p=compile_string(prog, "HilfeInput", handler)); 
    }; 
    last_compile_time = (int)(compile_time*1000000); 
 
#if constant(Debug.assembler_debug) 
    Debug.assembler_debug(0); 
#endif 
#if constant(Debug.compiler_trace) 
    Debug.compiler_trace(0); 
#endif 
 
    if(warnings||err) 
      handler->show_warnings(); 
    if(err) { 
      handler->show_errors(); 
      return 0; 
    } 
 
    object o; 
    if(hilfe_error( catch(o=p()) )) 
      return o; 
 
    return 0; 
  } 
 
  //! Compiles the Pike code @[a] and evaluates it by 
  //! calling ___HilfeWrapper in the generated object. 
  //! If @[show_result] is set the result will be displayed 
  //! and the result buffer updated with its value. 
  void evaluate(string a, int(0..1) show_result) 
  { 
    if(trace_level) 
        a = "\ntrace("+trace_level+");\n"+a; 
#if constant(_debug) 
    if(debug_level) 
      a = "\n_debug("+debug_level+");\n" + a; 
#endif 
    a = "mixed ___HilfeWrapper() { " + a + " ; }"; 
 
    last_else = 0; 
    object o; 
    if( o=hilfe_compile(a) ) 
    { 
      mixed res, err; 
      float eval_time = gauge { 
        err = catch { 
          res = o->___HilfeWrapper(); 
          trace(0); 
#if constant(_debug) 
        _debug(0); 
#endif 
      }; 
      }; 
      last_eval_time = (int)(eval_time*1000000); 
 
      if( err || (err=catch(a=sprintf("%O", res))) ) 
      { 
        if(objectp(err) && err->is_generic_error) 
          catch { err = ({ err[0], err[1] }); }; 
 
        if(arrayp(err) && sizeof(err)==2 && arrayp(err[1])) 
        { 
          err[1]=err[1][sizeof(backtrace())..]; 
          safe_write(describe_backtrace(err)); 
        } 
        else 
          safe_write("Hilfe Error: Error in evaluation: %O\n",err); 
      } 
      else { 
        if(show_result) { 
          history->push(res); 
          reswrite( safe_write, a, history->get_latest_entry_num(), res, 
                    last_compile_time, last_eval_time ); 
        } 
        else 
          reswrite( safe_write, 0, history->get_latest_entry_num(), 0, 
                    last_compile_time, last_eval_time ); 
      } 
    } 
  } 
} 
 
mapping base_objects(Evaluator e) 
{ 
  return all_constants() + e->constants + e->variables; 
} 
 
array(object|array(string)) resolv(Evaluator e, array completable, 
                                   void|object base, void|string type) 
{ 
  if (e->variables->DEBUG_COMPLETIONS) 
    e->safe_write("resolv(%O, %O, %O)\n", completable, base, type); 
  if (!sizeof(completable)) 
    return ({ base, completable, type }); 
 
  if (stringp(completable[0]) && 
      completable[0] == array_sscanf(completable[0], "%[ \t\r\n]")[0]) 
    return ({ base, completable[1..], type }); 
 
  if (typeof_token(completable[0]) == "argumentgroup" && type != "autodoc") 
    return resolv(e, completable, master()->show_doc(base), "autodoc"); 
 
  if (!base && completable[0] == ".") 
  { 
    if (sizeof(completable) == 1) 
      return ({ 0, completable, "module" }); 
    else 
    { 
      catch 
      { 
        // quick and dirty attempt to load a local module 
        base=compile_string(sprintf("object o=.%s;", completable[1]), 0)()->o; 
      }; 
      if (!base) 
        return ({ 0, completable, type }); 
      if (objectp(base)) 
        return resolv(e, completable[2..], base, "module"); 
      return resolv(e, completable[2..], base); 
    } 
  } 
 
  if (!base && sizeof(completable) > 1) 
  { 
    if (completable[0] == "master" && sizeof(completable) >=2 
        && typeof_token(completable[1]) == "argumentgroup") 
    { 
      return resolv(e, completable[2..], master(), "object"); 
    } 
    if (base=base_objects(e)[completable[0]]) 
      return resolv(e, completable[1..], base, "object"); 
 
    if (sizeof(completable) > 1 
        && (base=master()->root_module[completable[0]])) 
      return resolv(e, completable[1..], base, "module"); 
    return ({ 0, completable, type }); 
  } 
 
  if (sizeof(completable) > 1) 
  { 
    object newbase; 
    if (reference[completable[0]] && sizeof(completable) > 1) 
      return resolv(e, completable[1..], base); 
    if (type == "autodoc") 
    { 
      if (typeof_token(completable[0]) == "symbol" 
          && (newbase = base->findObject(completable[0]))) 
        return resolv(e, completable[1..], newbase, type); 
      else if (sizeof(completable) > 2 
            && typeof_token(completable[0]) == "argumentgroup" 
            && typeof_token(completable[1]) == "reference") 
        return resolv(e, completable[2..], base, type); 
      else 
        return ({ base, completable, type }); 
    } 
    if (!functionp(base) && (newbase=base[completable[0]]) ) 
      return resolv(e, completable[1..], newbase, type); 
  } 
 
  return ({ base, completable, type }); 
} 
 
// 
// Different wrappers that give the Hilfe a user interface 
// 
 
//! This is a wrapper containing a user interface to the Hilfe @[Evaluator] 
//! so that it can actually be used. This wrapper uses the @[Stdio.Readline] 
//! module to interface with the user. All input history is handled by 
//! that module, and as a consequence loading and saving .hilfe_history is 
//! handled in this class. Also .hilferc is handled by this class. 
class StdinHilfe 
{ 
  inherit Evaluator; 
 
  //! The readline object, 
  Stdio.Readline readline; 
 
  //! Saves the user input history, if possible, when called. 
  void save_history() { 
    catch { 
      if(string home=getenv("HOME")||getenv("USERPROFILE")) { 
        rm(home+"/.hilfe_history~"); 
        if(object f=Stdio.File(home+"/.hilfe_history~","wct")) { 
          f->write(readline->get_history()->encode()); 
          f->close(); 
        } 
        rm(home+"/.hilfe_history"); 
        mv(home+"/.hilfe_history~",home+"/.hilfe_history"); 
#if constant(chmod) 
      chmod(home+"/.hilfe_history", 0600); 
#endif 
      } 
    }; 
  } 
 
  void load_history() { 
    catch { 
      if(string home=getenv("HOME")||getenv("USERPROFILE")) 
        if(string s=Stdio.read_file(home+"/.hilfe_history")) 
          readline->enable_history(s/"\n"); 
    }; 
  } 
 
  void load_hilferc() { 
    if(string home=getenv("HOME")||getenv("USERPROFILE")) 
      if(string s=Stdio.read_file(home+"/.hilferc")) 
        map(s/"\n", add_buffer); 
  } 
 
  void signal_trap(int s) { 
    exit(1); 
  } 
 
  protected int do_write(string a, mixed ... args ) 
  { 
    if( sizeof( args ) ) 
      a = sprintf(a,@args); 
    write(a); 
    return strlen(a); 
  } 
 
  //! Any hilfe statements given in the init array will be executed 
  //! once .hilferc has been executed. 
  protected void create(void|array(string) init) 
  { 
    readline = Stdio.Readline(); 
    write = readline->write; 
    ::create(); 
 
    add_constant("werror",do_write); 
    add_constant("write",do_write); 
 
    load_hilferc(); 
    if(init) map(init, add_buffer); 
 
    load_history(); 
    if(!readline->get_history()) 
      readline->enable_history(512); 
    readline->get_input_controller()->bind("\t", handle_completions); 
    readline->get_input_controller()->bind("\\!k1", handle_doc); 
 
    signal(signum("SIGINT"),signal_trap); 
 
    for(;;) { 
      readline->set_prompt(state->finishedp() ? "> " : ">> "); 
      string s=readline->read(); 
 
      if(!s) 
        break; 
 
      save_history(); 
      add_input_line(s); 
    } 
    write("Terminal closed.\n"); 
    destruct(readline); 
  } 
 
  array get_resolvable(array tokens, void|int debug) 
  { 
    array completable = ({}); 
    string tokentype; 
 
    foreach(reverse(tokens);; string token) 
    { 
      string _tokentype = typeof_token(token); 
 
      if (debug) 
        write(sprintf("%O = %s\n", token, _tokentype)); 
 
      if ( ( _tokentype == "reference" && 
             (!tokentype || tokentype == "symbol")) 
            || (_tokentype == "symbol" && (!tokentype 
                 || (< "reference", "referencegroup", 
                       "argumentgroup" >)[tokentype])) 
            || ( (<"argumentgroup", "referencegroup" >)[_tokentype] 
                 && (!tokentype || tokentype == "reference")) 
         ) 
      { 
        completable += ({ token }); 
        tokentype = _tokentype; 
      } 
      else if (_tokentype == "whitespace") 
        ; 
      else 
        break; 
    } 
 
    // keep the last whitespace 
    if (arrayp(tokens) && sizeof(tokens) && 
        typeof_token(tokens[-1]) == "whitespace") 
      completable = ({ " " }) + completable; 
    return reverse(completable); 
  } 
 
  void handle_doc(string key) 
  { 
    array tokens; 
    string input = readline->gettext()[..readline->getcursorpos()-1]; 
    mixed error = catch 
    { 
      tokens = tokenize(input); 
    }; 
    if (error || !tokens || !sizeof(tokens)) 
      return; 
 
    array completable = get_resolvable(tokens); 
    if (sizeof(completable)) 
      CommandDoc()->output_doc(this, completable); 
  } 
 
  void handle_completions(string key) 
  { 
    mixed old_handler = master()->get_inhibit_compile_errors(); 
    HilfeCompileHandler handler = HilfeCompileHandler(sizeof(backtrace())); 
    master()->set_inhibit_compile_errors(handler); 
 
    array tokens; 
    string input = readline->gettext()[..readline->getcursorpos()-1]; 
    array|string completions; 
 
    mixed error = catch 
    { 
      tokens = tokenize(input); 
    }; 
 
    if(error) 
    { 
      if (objectp(error) && error->is_unterminated_string_error) 
      { 
        error = catch 
        { 
          completions = get_file_completions((input/"\"")[-1]); 
        }; 
      } 
 
      if (error) 
      { 
        if(!objectp(error)) 
          error = Error.mkerror(error); 
        readline->message(sprintf("%s\nAn error occurred, attempting to complete your input!\nPlease include the backtrace above and the line below in your report:\ninput: %s\n", error->describe(), input)); 
        completions = ({}); 
      } 
    } 
 
    if (tokens && !completions) 
    { 
      array completable = get_resolvable(tokens, variables->DEBUG_COMPLETIONS); 
 
      if (completable && sizeof(completable)) 
      { 
        error = catch 
        { 
          completions = get_module_completions(completable); 
        }; 
        error = Error.mkerror(error); 
      } 
      else if (!tokens || !sizeof(tokens)) 
        completions = sort(indices(master()->root_module)) + 
          sort(indices(base_objects(this))); 
        // FIXME: base_objects should not be sorted like this 
 
      if (!completions || !sizeof(completions)) 
      { 
        string token = tokens[-1]; 
        if( sizeof(tokens) >= 2 && typeof_token(token) == "whitespace" ) 
          token = tokens[-2]; 
 
        if (variables->DEBUG_COMPLETIONS) 
          readline->message(sprintf("type: %s\n", typeof_token(token))); 
 
        completions = sort(indices(master()->root_module)) + 
          sort(indices(base_objects(this))); 
        // FIXME: base_objects should not be sorted like this 
 
        switch(typeof_token(token)) 
        { 
          case "symbol": 
          case "literal": 
          case "postfix": 
            completions = (array)(infix+seperator); 
            break; 
          case "prefix": 
          case "infix": 
          case "seperator": 
          default: 
            completions += (array)prefix; 
        } 
        foreach(reverse(tokens);; string token) 
        { 
            if (group[token]) 
            { 
              completions += ({ group[token] }) ; 
              break; 
            } 
        } 
      } 
 
      if (error) 
      { 
        readline->message(sprintf("%s\nAn error occurred, attempting to complete your input!\nPlease include the backtrace above and the lines below in your report:\ninput: %s\ntokens: %O\ncompletable: %O\n", error->describe(), input, tokens, completable, )); 
      } 
      else if (variables->DEBUG_COMPLETIONS) 
        readline->message(sprintf("input: %s\ntokens: %O\ncompletable: %O\ncompletions: %O\n", input, tokens, completable, completions)); 
    } 
 
    handler->show_errors(); 
    handler->show_warnings(); 
    master()->set_inhibit_compile_errors(old_handler); 
 
    if(completions && sizeof(completions)) 
    { 
      if(stringp(completions)) 
        readline->insert(completions, readline->getcursorpos()); 
      else 
        readline->list_completions(completions); 
    } 
  } 
 
  array tokenize(string input) 
  { 
      array tokens = Parser.Pike.split(input); 
      if (variables->DEBUG_COMPLETIONS) 
          readline->message(sprintf("\n\ntokenize(%O): %O\n\n", input, tokens)); 
      // drop the linebreak that split appends 
      if (tokens[-1] == "\n") 
        tokens = tokens[..<1]; 
      else if (tokens[-1][-1] == '\n') 
        tokens[-1] = tokens[-1][..<1]; 
 
      tokens = Parser.Pike.group(tokens); 
      return tokens; 
  } 
 
  array|string get_file_completions(string path) 
  { 
    array files = ({}); 
    if ( (< "", ".", ".." >)[path-"../"] ) 
      files += ({ ".." }); 
 
    if (!sizeof(path) || path[0] != '/') 
      path = "./"+path; 
 
    string dir = dirname(path); 
    string file = basename(path); 
    catch 
    { 
      files += get_dir(dir); 
    }; 
 
    if (!sizeof(files)) 
      return ({}); 
 
    array completions = Array.filter(files, has_prefix, file); 
    string prefix = String.common_prefix(completions)[sizeof(file)..]; 
 
    if (sizeof(prefix)) 
    { 
      return prefix; 
    } 
 
    mapping filetypes = ([ "dir":"/", "lnk":"@", "reg":"" ]); 
 
    if (sizeof(completions) == 1 && file_stat(dir+"/"+completions[0])->isdir ) 
    { 
      return "/"; 
    } 
    else 
    { 
      foreach(completions; int count; string item) 
      { 
        Stdio.Stat stat = file_stat(dir+"/"+item); 
        if (stat) 
          completions[count] += filetypes[stat->type]||""; 
 
        stat = file_stat(dir+"/"+item, 1); 
        if (stat?->type == "lnk") 
          completions[count] += filetypes["lnk"]; 
      } 
      return completions; 
    } 
  } 
 
  array|string get_module_completions(array completable) 
  { 
    array rest = completable; 
    object base; 
    string type; 
    int(0..1) space; 
 
    if (!completable) 
      completable = ({}); 
 
    if (completable[-1]==' ') 
    { 
      space = true; 
      completable = completable[..<1]; 
    } 
 
    if (sizeof(completable) > 1) 
      [base, rest, type] = resolv(this, completable); 
 
    if (variables->DEBUG_COMPLETIONS) 
      safe_write(sprintf("get_module_completions(%O): %O, %O, %O\n", completable, base, rest, type)); 
    array completions = low_get_module_completions(rest, base, type, space); 
    if (sizeof(completions) == 1) 
      return completions[0]; 
    else 
      return completions; 
  } 
 
  mapping reftypes = ([ "module":".", 
                     "object":"->", 
                     "mapping":"->", 
                     "function":"(", 
                     "program":"(", 
                     "method":"(", 
                     "class":"(", 
                   ]); 
 
  array low_get_module_completions(array completable, object base, void|string type, void|int(0..1) space) 
  { 
      if (variables->DEBUG_COMPLETIONS) 
        safe_write(sprintf("low_get_module_completions(%O\n, %O, %O, %O)\n", completable, base, type, space)); 
 
      if (!completable) 
        completable = ({}); 
 
      mapping other = ([]); 
      array modules = ({}); 
      mixed error; 
 
      if (base && !sizeof(completable)) 
      { 
        if (space) 
          return (array)infix; 
        if (type == "autodoc") 
            return ({ reftypes[base->objtype||base->objects[0]->objtype]||"" }); 
        if (objectp(base)) 
          return ({ reftypes[type||"object"] }); 
        if (mappingp(base)) 
          return ({ reftypes->object }); 
        else if(functionp(base)) 
          return ({ reftypes->function }); 
        else if (programp(base)) 
          return ({ reftypes->program }); 
        else 
          return (array)infix; 
      } 
 
      if (!base && sizeof(completable) && completable[0] == ".") 
      { 
          array modules = sort(get_dir(".")); 
          if (sizeof(completable) > 1) 
          { 
            modules = Array.filter(modules, has_prefix, completable[1]); 
            if (sizeof(modules) == 1) 
              return ({ (modules[0]/".")[0][sizeof(completable[1])..] }); 
            string prefix = String.common_prefix(modules)[sizeof(completable[1])..]; 
            if (prefix) 
              return ({ prefix }); 
 
            if (sizeof(completable) == 2) 
              return modules; 
            else 
              return ({}); 
          } 
          else 
            return modules; 
      } 
      else if (!base) 
      { 
          if (type == "autodoc") 
          { 
            if (variables->DEBUG_COMPLETIONS) 
              safe_write("autodoc without base\n"); 
            return ({}); 
          } 
          other = base_objects(this); 
          base = master()->root_module; 
      } 
 
      if (type == "autodoc") 
      { 
        if (base->docGroups) 
          modules = Array.uniq(Array.flatten(base->docGroups->objects->name)); 
        else 
          return ({}); 
      } 
      else 
      { 
        error = catch 
        { 
          modules = sort(indices(base)); 
        }; 
        error = Error.mkerror(error); 
      } 
 
      if (sizeof(other)) 
        modules += indices(other); 
 
      if (sizeof(completable) == 1) 
      { 
          if (type == "autodoc" 
              && typeof_token(completable[0]) == "argumentgroup") 
            if (space) 
              return (array)infix; 
            else 
              return ({ reftypes->object }); 
          if (reference[completable[0]]) 
            return modules; 
          if (!stringp(completable[0])) 
            return ({}); 
 
          // FIXME: handle non-string indices better 
          modules = sort((array(string))modules); 
          modules = Array.filter(modules, has_prefix, completable[0]); 
          string prefix = String.common_prefix(modules); 
          string module; 
 
          if (prefix == completable[0] && sizeof(modules)>1 && (base[prefix]||other[prefix])) 
            return modules + low_get_module_completions(({}), base[prefix]||other[prefix], type, space); 
 
          prefix = prefix[sizeof(completable[0])..]; 
          if (sizeof(prefix)) 
            return ({ prefix }); 
 
          if (sizeof(modules)>1) 
            return modules; 
          else if (!sizeof(modules)) 
            return ({}); 
          else 
          { 
            module = modules[0]; 
            modules = ({}); 
            object thismodule; 
 
            if(other && other[module]) 
            { 
              thismodule = other[module]; 
              type = "object"; 
            } 
            else if (intp(base[module]) || floatp(base[module]) || stringp(base[module]) ) 
              return (array)infix; 
            else 
            { 
              // FIXME: need to check if thismodule is something indexable 
              thismodule = base[module]; 
              if (!type) 
                type = "module"; 
            } 
 
            return low_get_module_completions(({}), thismodule, type, space); 
          } 
      } 
 
      if (completable && sizeof(completable)) 
      { 
          if ( (< "reference", "argumentgroup" >)[typeof_token(completable[0])]) 
            return low_get_module_completions(completable[1..], base, type||reference[completable[0]], space); 
          else 
            safe_write(sprintf("UNHANDLED CASE: completable: %O\nbase: %O\n", completable, base)); 
      } 
 
      return modules; 
  } 
} 
 
//! 
class GenericHilfe 
{ 
  inherit Evaluator; 
 
  //! 
  protected void create(Stdio.FILE in, Stdio.File out) 
  { 
    write=out->write; 
    ::create(); 
 
    while(1) 
    { 
      write(state->finishedp() ? "> " : ">> "); 
      if(string s=in->gets()) 
        add_input_line(s); 
      else { 
        write("Terminal closed.\n"); 
        return; 
      } 
    } 
  } 
} 
 
//! 
class GenericAsyncHilfe (Stdio.File infile, Stdio.File outfile) 
{ 
  inherit Evaluator; 
 
  string outbuffer=""; 
 
  void write_callback() 
  { 
    if (!sizeof(outbuffer)) return; 
    int i=outfile->write(outbuffer); 
    outbuffer=outbuffer[i..]; 
  } 
 
  void send_output(string s, mixed ... args) 
  { 
    outbuffer+=sprintf(s,@args); 
    write_callback(); 
  } 
 
  string inbuffer=""; 
  void read_callback(mixed id, string s) 
  { 
    inbuffer+=s; 
    foreach(inbuffer/"\n",string s) 
      { 
        add_input_line(s); 
        write(state->finishedp() ? "> " : ">> "); 
      } 
  } 
 
  void close_callback() 
  { 
    write("Terminal closed.\n"); 
    destruct(this); 
    destruct(infile); 
    destruct(outfile); 
  } 
 
  protected void create() 
  { 
    infile->set_nonblocking(read_callback, 0, close_callback); 
    outfile->set_write_callback(write_callback); 
 
    write=send_output; 
    ::create(); 
    write(state->finishedp() ? "> " : ">> "); 
  } 
} 
 
int main() { 
  StdinHilfe(); 
  return 0; 
} 
 
 
// 
// Help texts for the built in commands. 
// 
 
constant documentation_set = 
#"Change Hilfe settings. Used as \"set <setting> <parameter>\". 
Available parameters: 
 
assembler_debug 
    Changes the level of assembler debug used when evaluating 
    expressions in Pike. Requires that Pike is compiled with 
    RTL debug. 
 
compiler_trace 
    Changes the level of compiler trace used when evaluating 
    expressions in Pike. Requires that Pike is compiled with 
    RTL debug. 
 
debug 
    Changes the level of debug used when evaluating expressions in 
    Pike. Requires that Pike is compiled with RTL debug. 
 
format 
    Changes the formatting of the result values from evaluated 
    Pike expressions. Enter \"help set format\" for more 
    information. 
 
hedda 
    Initializes some variables for quick access, unless they are 
    already defined. These variables may be added: mixed foo, 
    mixed bar, int i, float f=0.0, mapping m=([]), array a=({}) 
    and string s=\"\". 
 
history 
    Change the maximum number of entries that are kept in the 
    result history. Default is 10. 
 
trace 
     Changes the level of trace used when evaluating expressions 
     in Pike. Possible values are: 
       0 Off 
       1 Calls to Pike functions are printed. 
       2 Calls to buitin functions are printed. 
       3 Every opcode interpreted is printed. 
       4 Arguments to these opcodes are printed as well. 
 
warnings 
    Change the current level of warnings checking. Possible 
    values are: 
       off     No warnings are shown. 
       on      Normal warnings are shown (default). 
       strict  Try a little harder to show warnings. 
"; 
 
constant documentation_set_format = 
#"\"set format\" changes the formatting of the result values from 
evaluated Pike expressions. Currently the following set format 
parameters are available: 
 
default  The normal result formatting. 
bench    A result formatting extended with compilation and 
         evaluation times. 
bin      Returns integers in binary form. 
hex      Returns integers in hexadecimal form. 
oct      Returns integers in octal form. 
sprintf  The result formatting will be decided by the succeeding 
         Pike string. The sprintf will be given the following 
         arguments: 
 
           0  The result as a string. 
           1  The result number in the history. 
           2  The result in its native type. 
           3  The compilation time as a string. 
           4  The evaluation time as a string. 
           5  The compilation time in nanoseconds as an int. 
           6  The evaluation time in nanoseconds as an int. 
 
         Usage examples: 
           set format sprintf \"%s (%[2]t)\\n\" 
           set format sprintf \"%s (%d/%[3]s/%[4]s)\\n\" 
"; 
 
constant documentation_help_me_more = 
#"Some commands have extended help available. This can be displayed by 
typing help followed by the name of the command, e.g. \"help dump\". 
Commands clobbered by e.g. variable declarations can be reached by 
prefixing a dot to the command, e.g. \".exit\". 
 
Tab completion works on modules, global and local symbols and operators. 
Within quotes completion is offered for files and directories. 
 
A history of the last returned results is kept and can be accessed 
from your hilfe expressions with the variable __. You can either 
\"address\" your results with absolute addresses, e.g. __[2] to get 
the second result ever, or with relative addresses, e.g. __[-1] to 
get the last result. The last result is also available in the 
variable _, thus _==__[-1] is true. The magic _ and __ variable can 
be clobbered with local definitions to disable them, e.g. by typing 
\"int _;\". The result history is currently ten entries long. 
 
A history of the 512 last entered lines is kept in Hilfe. You can 
browse this list with your arrow keys up/down. When you exit Hilfe 
your history will be saved in .hilfe_history in the directory set 
in environment variable $HOME or $USERPROFILE. Next time hilfe is 
started the history is imported. 
 
You can put a .hilferc file in the directory set in your 
environment variable $HOME or $USERPROFILE. The contents of this 
file will be evaluated in hilfe during each startup. 
 
Note that there are a few symbols that you can not define, since 
they are used by Hilfe. They are: 
 
___hilfe         A mapping containing all defined symbols. 
___Hilfe         The Hilfe object. 
___HilfeWrapper  A wrapper around the entered expression. 
 
 
Type \"help hilfe todo\" to get a list of known Hilfe bugs/lackings. 
Type \"help about hilfe\" to get an extended version information. 
"; 
 
constant documentation_dump = 
#"dump 
      Shows the currently defined constants, variables, functions 
      and programs. It also lists all active inherits and imports. 
 
dump history 
      Shows all items in the history queue. 
 
dump memory 
      Shows the current memory usage. 
 
dump state 
      Shows the current parser state. Only useful for debugging 
      Hilfe. 
 
dump wrapper 
      Show the latest Hilfe wrapper that the last expression was 
      evaluated in. Useful when debugging Hilfe (i.e. investigating 
      why valid Pike expressions don't compile). 
"; 
 
constant documentation_new = 
#"new 
      Clears the current Hilfe state. This includes the parser 
      state, variables, constants, functions, programs, inherits, 
      imports and the history. It does not include the currently 
      installed commands. Note that code in your .hilferc will not 
      be reevaluated. 
 
new history 
      Remove all history entries from the result history. 
 
new constants 
new functions 
new programs 
new variables 
      Clears all locally defined symbols of the given type. 
 
new imports 
new inherits 
      Removes all imports/inherits made. 
";