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
  
/* 
|| This file is part of Pike. For copyright information see COPYRIGHT. 
|| Pike is distributed under GPL, LGPL and MPL. See the file COPYING 
|| for more information. 
*/ 
 
/* 
  Pike Sprintf v2.0 By Fredrik Hubinette (Profezzorn@nannymud) 
  Should be reasonably compatible and somewhat faster than v1.05+ of Lynscar's 
  sprintf. It requires the buffering function provided in buffer.h 
   Fail-safe memory-leak-protection is implemented through a stack that can 
   be deallocated at any time. If something fails horribly this stack will be 
  deallocated at next call of sprintf. Most operators doesn't need this 
  feature though as they allocate their buffers with alloca() or simply use 
  pointers into other strings. 
  It also has a lot more features. 
 
 Ideas yet to be implemented: 
   Line-break with fill? Lower-case? Upper case? Capitalize? 
   Replace? Justify on decimal point? 
   Change european/american decimal notation? 
   nroff-format? Crack root password and erase all disks? 
   Print-optimize? (space to tab, strip trailing spaces) 
   '>' Kill this field in all lines but the first one 
 
 Examples: 
 
   A short 'people' function (without sort) 
 
   sprintf("%{%-14s %2d %-30s %s\n%}\n",map(users(),lambda(object x) 
       { 
           return ({x->query_real_name(), 
                x->query_level(), 
                query_ip_name(x), 
                file_name(environment(x)) 
              }); 
       })) 
 
 
   an 'ls' 
   sprintf("%-#*{%s\n%}\n",width,get_dir(dir)); 
 
   debug-dump 
   sprintf("%78*O",width,foo); 
 
   A newspaper 
   sprintf("%-=*s %-=*s\n",width/2,article1,width/2,article2); 
 
   A 'dotted-line' pricelist row 
   sprintf("%'.'-10s.%'.'4d\n",item,cost); 
 
*/ 
 
/*! @decl string sprintf(strict_sprintf_format format, sprintf_args ... args) 
 *! 
 *!   Print formated output to string. 
 *! 
 *!   The @[format] string is a string containing a description of how to 
 *!   output the data in @[args]. This string should generally speaking 
 *!   have one @tt{%@i{<modifiers>@}@i{<operator>@}@} format specifier 
 *!   (examples: @tt{%s@}, @tt{%0d@}, @tt{%-=20s@}) for each of the arguments. 
 *! 
 *!   The following modifiers are supported: 
 *!   @int 
 *!     @value '0' 
 *!       Zero pad numbers (implies right justification). 
 *!     @value '!' 
 *!       Toggle truncation. 
 *!     @value ' ' 
 *!       Pad positive integers with a space. 
 *!     @value '+' 
 *!       Pad positive integers with a plus sign. 
 *!     @value '-' 
 *!       Left adjust within field size (default is right). 
 *!     @value '|' 
 *!       Centered within field size. 
 *!     @value '=' 
 *!       Column mode if strings are greater than field width. Breaks 
 *!       between words (possibly skipping or adding spaces). Can not be 
 *!       used together with @expr{'/'@}. 
 *!     @value '/' 
 *!       Column mode with rough line break (break at exactly field width 
 *!       instead of between words). Can not be used together with @expr{'='@}. 
 *!     @value '#' 
 *!       Table mode, print a list of @expr{'\n'@} separated words 
 *!       (top-to-bottom order). 
 *!     @value '$' 
 *!       Inverse table mode (left-to-right order). 
 *!     @value 'n' 
 *!     @value ':n' 
 *!       (Where n is a number or *) field width specifier. 
 *!     @value '.n' 
 *!       Precision specifier. 
 *!     @value ';n' 
 *!       Column width specifier. 
 *!     @value '*' 
 *!       If n is a @tt{*@} then next argument is used for precision/field 
 *!       size. The argument may either be an integer, or a modifier mapping 
 *!       as received by @[lfun::_sprintf()]: 
 *!       @mapping 
 *!         @member int|void "precision" 
 *!           Precision. 
 *!         @member int(0..)|void "width" 
 *!           Field width. 
 *!         @member int(0..1)|void "flag_left" 
 *!           Indicates that the output should be left-aligned. 
 *!         @member int(0..)|void "indent" 
 *!           Indentation level in @tt{%O@}-mode. 
 *!       @endmapping 
 *!     @value "'" 
 *!       Set a pad string. @tt{'@} cannot be a part of the pad string (yet). 
 *!     @value '~' 
 *!       Get pad string from argument list. 
 *!     @value '<' 
 *!       Use same argument again. 
 *!     @value '^' 
 *!       Repeat this on every line produced. 
 *!     @value '@' 
 *!       Repeat this format for each element in the argument array. 
 *!     @value '>' 
 *!       Put the string at the bottom end of column instead of top. 
 *!     @value '_' 
 *!       Set width to the length of data. 
 *!     @value '[n]' 
 *!       Select argument number @tt{@i{n@}@}. Use @tt{*@} to use the next 
 *!       argument as selector. The arguments are numbered starting from 
 *!       @expr{0@} (zero) for the first argument after the @[format]. 
 *!       Note that this only affects where the current operand is fetched. 
 *!   @endint 
 *! 
 *!   The following operators are supported: 
 *!   @int 
 *!     @value '%' 
 *!       Percent. 
 *!     @value 'b' 
 *!       Signed binary integer. 
 *!     @value 'd' 
 *!       Signed decimal integer. 
 *!     @value 'u' 
 *!       Unsigned decimal integer. 
 *!     @value 'o' 
 *!       Signed octal integer. 
 *!     @value 'x' 
 *!       Lowercase signed hexadecimal integer. 
 *!     @value 'X' 
 *!       Uppercase signed hexadecimal integer. 
 *!     @value 'c' 
 *!       Character. If a fieldsize has been specified this will output 
 *!       the low-order bytes of the integer in network (big endian) byte 
 *!       order. To get little endian byte order, negate the field size. 
 *!     @value 'f' 
 *!       Float. (Locale dependent formatting.) 
 *!     @value 'g' 
 *!       Heuristically chosen representation of float. 
 *!       (Locale dependent formatting.) 
 *!     @value 'G' 
 *!       Like @tt{%g@}, but uses uppercase @tt{E@} for exponent. 
 *!     @value 'e' 
 *!       Exponential notation float. (Locale dependent output.) 
 *!     @value 'E' 
 *!       Like @tt{%e@}, but uses uppercase @tt{E@} for exponent. 
 *!     @value 'F' 
 *!       Binary IEEE representation of float (@tt{%4F@} gives 
 *!       single precision, @tt{%8F@} gives double precision) 
 *!       in network (big endian) byte order. To get little endian 
 *!       byte order, negate the field size. 
 *!     @value 's' 
 *!       String. 
 *!     @value 'q' 
 *!       Quoted string. Escapes all control and non-8-bit characters, 
 *!       as well as the quote characters @tt{'\\'@} and @tt{'\"'@}. 
 *!     @value 'O' 
 *!       Any value, debug style. Do not rely on the exact formatting; 
 *!       how the result looks can vary depending on locale, phase of 
 *!       the moon or anything else the @[lfun::_sprintf()] method 
 *!       implementor wanted for debugging. 
 *!     @value 'p' 
 *!       Hexadecimal representation of the memory address of the object. 
 *!       Integers and floats have no address, and are printed as themselves. 
 *!     @value 'H' 
 *!       Binary Hollerith string. Equivalent to @expr{sprintf("%c%s", 
 *!       strlen(str), str)@}. Arguments (such as width etc) adjust the 
 *!       length-part of the format. Requires 8-bit strings. 
 *!     @value 'n' 
 *!       No argument. Same as @expr{"%s"@} with an empty string as argument. 
 *!       Note: Does take an argument array (but ignores its content) 
 *!       if the modifier @expr{'@@'@} is active. 
 *!     @value 't' 
 *!       Type of the argument. 
 *!     @value '{' 
 *!     @value '}' 
 *!       Perform the enclosed format for every element of the argument array. 
 *!   @endint 
 *! 
 *!   Most modifiers and operators are combinable in any fashion, but some 
 *!   combinations may render strange results. 
 *! 
 *!   If an argument is an object that implements @[lfun::_sprintf()], that 
 *!   callback will be called with the operator as the first argument, and 
 *!   the current modifiers as the second. The callback is expected to return 
 *!   a string. 
 *! 
 *! @note 
 *!   sprintf-style formatting is applied by many formatting functions, such 
 *!   @[write()] and @[werror()]. It is also possible to get sprintf-style 
 *!   compile-time argument checking by using the type-attributes 
 *!   @[sprintf_format] or @[strict_sprintf_format] in combination 
 *!   with @[sprintf_args]. 
 *! 
 *! @note 
 *!   The @expr{'q'@} operator was added in Pike 7.7. 
 *! 
 *! @note 
 *!   Support for specifying modifiers via a mapping was added in Pike 7.8. 
 *!   This support can be tested for with the constant 
 *!   @[String.__HAVE_SPRINTF_STAR_MAPPING__]. 
 *! 
 *! @note 
 *!   Support for specifying little endian byte order to @expr{'F'@} 
 *!   was added in Pike 7.8. This support can be tested for with the 
 *!   constant @[String.__HAVE_SPRINTF_NEGATIVE_F__]. 
 *! 
 *! @example 
 *! @code 
 *! Pike v7.8 release 263 running Hilfe v3.5 (Incremental Pike Frontend) 
 *! > sprintf("The unicode character %c has character code %04X.", 'A', 'A'); 
 *! (1) Result: "The unicode character A has character code 0041." 
 *! > sprintf("#%@@02X is the HTML code for purple.", Image.Color.purple->rgb()); 
 *! (2) Result: "#A020F0 is the HTML code for purple." 
 *! > int n=4711; 
 *! > sprintf("%d = hexadecimal %x = octal %o = %b binary", n, n, n, n); 
 *! (3) Result: "4711 = hexadecimal 1267 = octal 11147 = 1001001100111 binary" 
 *! > write(#"Formatting examples: 
 *! Left adjusted  [%-10d] 
 *! Centered       [%|10d] 
 *! Right adjusted [%10d] 
 *! Zero padded    [%010d] 
 *! ", n, n, n, n); 
 *! Formatting examples: 
 *! Left adjusted  [4711      ] 
 *! Centered       [   4711   ] 
 *! Right adjusted [      4711] 
 *! Zero padded    [0000004711] 
 *! (5) Result: 142 
 *! int screen_width=70; 
 *! > write("%-=*s\n", screen_width, 
 *! >> "This will wordwrap the specified string within the "+ 
 *! >> "specified field size, this is useful say, if you let "+ 
 *! >> "users specify their screen size, then the room "+ 
 *! >> "descriptions will automagically word-wrap as appropriate.\n"+ 
 *! >> "slosh-n's will of course force a new-line when needed.\n"); 
 *! This will wordwrap the specified string within the specified field 
 *! size, this is useful say, if you let users specify their screen size, 
 *! then the room descriptions will automagically word-wrap as 
 *! appropriate. 
 *! slosh-n's will of course force a new-line when needed. 
 *! (6) Result: 355 
 *! > write("%-=*s %-=*s\n", screen_width/2, 
 *! >> "Two columns next to each other (any number of columns will "+ 
 *! >> "of course work) independantly word-wrapped, can be useful.", 
 *! >> screen_width/2-1, 
 *! >> "The - is to specify justification, this is in addherence "+ 
 *! >> "to std sprintf which defaults to right-justification, "+ 
 *! >> "this version also supports centre and right justification."); 
 *! Two columns next to each other (any The - is to specify justification, 
 *! number of columns will of course    this is in addherence to std 
 *! work) independantly word-wrapped,   sprintf which defaults to 
 *! can be useful.                      right-justification, this version 
 *!                                     also supports centre and right 
 *!                                     justification. 
 *! (7) Result: 426 
 *! > write("%-$*s\n", screen_width, 
 *! >> "Given a\nlist of\nslosh-n\nseparated\n'words',\nthis option\n"+ 
 *! >> "creates a\ntable out\nof them\nthe number of\ncolumns\n"+ 
 *! >> "be forced\nby specifying a\npresision.\nThe most obvious\n"+ 
 *! >> "use is for\nformatted\nls output."); 
 *! Given a          list of          slosh-n 
 *! separated        'words',         this option 
 *! creates a        table out        of them 
 *! the number of    columns          be forced 
 *! by specifying a  presision.       The most obvious 
 *! use is for       formatted        ls output. 
 *! (8) Result: 312 
 *! > write("%-#*s\n", screen_width, 
 *! >> "Given a\nlist of\nslosh-n\nseparated\n'words',\nthis option\n"+ 
 *! >> "creates a\ntable out\nof them\nthe number of\ncolumns\n"+ 
 *! >> "be forced\nby specifying a\npresision.\nThe most obvious\n"+ 
 *! >> "use is for\nformatted\nls output."); 
 *! Given a          creates a        by specifying a 
 *! list of          table out        presision. 
 *! slosh-n          of them          The most obvious 
 *! separated        the number of    use is for 
 *! 'words',         columns          formatted 
 *! this option      be forced        ls output. 
 *! (9) Result: 312 
 *! > sample = ([ "align":"left", "valign":"middle" ]); 
 *! (10) Result: ([ @xml{/@}* 2 elements *@xml{/@} 
 *!          "align":"left", 
 *!          "valign":"middle" 
 *!        ]) 
 *! > write("<td%{ %s='%s'%}>\n", (array)sample); 
 *! <td valign='middle' align='left'> 
 *! (11) Result: 34 
 *! >  write("Of course all the simple printf options "+ 
 *! >> "are supported:\n %s: %d %x %o %c\n", 
 *! >> "65 as decimal, hex, octal and a char", 
 *! >> 65, 65, 65, 65); 
 *! Of course all the simple printf options are supported: 
 *!  65 as decimal, hex, octal and a char: 65 41 101 A 
 *! (12) Result: 106 
 *! > write("%[0]d, %[0]x, %[0]X, %[0]o, %[0]c\n", 75); 
 *! 75, 4b, 4B, 113, K 
 *! (13) Result: 19 
 *! > write("%|*s\n",screen_width, "THE END"); 
 *!                                THE END 
 *! (14) Result: 71 
 *! > write("%|*s\n", ([ "width":screen_width ]), "ALTERNATIVE END"); 
 *!                            ALTERNATIVE END 
 *! (15) Result: 71 
 *! @endcode 
 *! 
 *! @seealso 
 *!   @[lfun::_sprintf()], @[strict_sprintf_format], @[sprintf_format], 
 *!   @[sprintf_args], @[String.__HAVE_SPRINTF_STAR_MAPPING__], 
 *!   @[String.__HAVE_SPRINTF_NEGATIVE_F__]. 
 */ 
#include "global.h" 
#include "pike_error.h" 
#include "array.h" 
#include "svalue.h" 
#include "stralloc.h" 
#include "sprintf.h" 
#include "buffer.h" 
#include "pike_compiler.h" 
#include "pike_types.h" 
#include "constants.h" 
#include "interpret.h" 
#include "pike_memory.h" 
#include "pike_macros.h" 
#include "object.h" 
#include "bignum.h" 
#include "mapping.h" 
#include "builtin_functions.h" 
#include "operators.h" 
#include "opcodes.h" 
#include "cyclic.h" 
#include "pike_float.h" 
#include "stack_allocator.h" 
#include <ctype.h> 
#include "module_support.h" 
#include "bitvector.h" 
 
#define RETURN_SHARED_STRING 
 
#define SPRINTF_UNDECIDED -1027 
 
struct format_info 
{ 
  char *fi_free_string; 
  struct pike_string *to_free_string; 
  PCHARP b;             /* Buffer to format */ 
  ptrdiff_t len;        /* Buffer length */ 
  ptrdiff_t width;      /* Field width (handled by fix_field) */ 
  int precision;    /* Field precision (handled by 'boduxXefgEGsq') */ 
  PCHARP pad_string;    /* Padding (handled by fix_field) */ 
  ptrdiff_t pad_length; /* Padding length (handled by fix_field) */ 
  int column_entries;       /* Number of column entries (handled by do_one) */ 
  short flags; 
  char pos_pad;             /* Positive padding (handled by fix_field) */ 
  int column_width; 
  ptrdiff_t column_modulo; 
}; 
 
struct format_stack 
{ 
  struct stack_allocator a; 
  struct format_info *fsp; 
  struct format_info *format_info_stack; 
  size_t size; 
}; 
 
#define FIELD_LEFT    (1<<0) 
#define FIELD_CENTER        (1<<1) 
#define PAD_POSITIVE        (1<<2) 
#define LINEBREAK   (1<<3) 
#define COLUMN_MODE (1<<4) 
#define ZERO_PAD    (1<<5) 
#define ROUGH_LINEBREAK     (1<<6) 
#define DO_TRUNC    (1<<7) 
#define REPEAT              (1<<8) 
#define SNURKEL             (1<<9) 
#define INVERSE_COLUMN_MODE (1<<10) 
#define MULTI_LINE  (1<<11) 
#define WIDTH_OF_DATA       (1<<12) 
#define MULTI_LINE_BREAK (1<<13) 
 
#define MULTILINE (LINEBREAK | COLUMN_MODE | ROUGH_LINEBREAK | \ 
                   INVERSE_COLUMN_MODE | MULTI_LINE | REPEAT) 
 
 
/* Generate binary IEEE strings on a machine which uses a different kind 
   of floating point internally */ 
 
#if !defined(NEED_CUSTOM_IEEE) && (SIZEOF_FLOAT_TYPE > 4) 
#define NEED_CUSTOM_IEEE 
#endif 
 
#ifdef NEED_CUSTOM_IEEE 
 
#ifndef HAVE_FPCLASS 
#ifdef HAVE_FP_CLASS_D 
#define fpclass fp_class_d 
#define FP_NZERO FP_NEG_ZERO 
#define FP_PZERO FP_POS_ZERO 
#define FP_NINF FP_NEG_INF 
#define FP_PINF FP_POS_INF 
#define FP_NNORM FP_NEG_NORM 
#define FP_PNORM FP_POS_NORM 
#define FP_NDENORM FP_NEG_DENORM 
#define FP_PDENORM FP_POS_DENORM 
#define HAVE_FPCLASS 
#endif 
#endif 
 
static void low_write_IEEE_float(char *b, double d, int sz) 
{ 
  int maxexp; 
  unsigned INT32 maxf; 
  int s = 0, e = -1; 
  unsigned INT32 f = 0, extra_f=0; 
 
  if(sz==4) { 
    maxexp = 255; 
    maxf   = 0x7fffff; 
  } else { 
    maxexp = 2047; 
    maxf   = 0x0fffff; /* This is just the high part of the mantissa... */ 
  } 
 
#ifdef HAVE_FPCLASS 
  switch(fpclass(d)) { 
  case FP_SNAN: 
    e = maxexp; f = 2; break; 
  case FP_QNAN: 
    e = maxexp; f = maxf; break; 
  case FP_NINF: 
    s = 1; /* FALLTHRU */ 
  case FP_PINF: 
    e = maxexp; break; 
  case FP_NZERO: 
    s = 1; /* FALLTHRU */ 
  case FP_PZERO: 
    e = 0; break; 
  case FP_NNORM: 
  case FP_NDENORM: 
    s = 1; d = fabs(d); break; 
  case FP_PNORM: 
  case FP_PDENORM: 
    break; 
  default: 
    if(d<0.0) { 
      s = 1; 
      d = fabs(d); 
    } 
    break; 
  } 
#else 
  if(PIKE_ISINF(d)) 
    e = maxexp; 
  else if(PIKE_ISNAN(d)) 
  { 
    e = maxexp; 
    f = maxf; 
  } 
#ifdef HAVE_ISZERO 
  else if(iszero(d)) 
    e = 0; 
#endif 
 
#ifdef HAVE_SIGNBIT 
  if((s = signbit(d))) 
    d = fabs(d); 
#else 
  if(d<0.0) { 
    s = 1; 
    d = fabs(d); 
  } 
#endif 
#endif 
 
  if(e<0) { 
    d = frexp(d, &e); 
    if(d == 1.0) { 
      d=0.5; 
      e++; 
    } 
    if(d == 0.0) { 
      e = 0; 
      f = 0; 
    } else if(sz==4) { 
      e += 126; 
      d *= 16777216.0; 
      if(e<=0) { 
        d = ldexp(d, e-1); 
        e = 0; 
      } 
      f = ((INT32)floor(d))&maxf; 
    } else { 
      double d2; 
      e += 1022; 
      d *= 2097152.0; 
      if(e<=0) { 
        d = ldexp(d, e-1); 
        e = 0; 
      } 
      d2 = floor(d); 
      f = ((INT32)d2)&maxf; 
      d -= d2; 
      d += 1.0; 
      extra_f = (unsigned INT32)(floor(d * 4294967296.0)-4294967296.0); 
    } 
 
    if(e>=maxexp) { 
      e = maxexp; 
      f = extra_f = 0; 
    } 
  } 
 
  if(sz==4) { 
    b[0] = (s? 128:0)|((e&0xfe)>>1); 
    b[1] = ((e&1)<<7)|((f&0x7f0000)>>16); 
    b[2] = (f&0xff00)>>8; 
    b[3] = f&0xff; 
  } else { 
    b[0] = (s? 128:0)|((e&0x7f0)>>4); 
    b[1] = ((e&0xf)<<4)|((f&0x0f0000)>>16); 
    b[2] = (f&0xff00)>>8; 
    b[3] = f&0xff; 
    b[4] = (extra_f&0xff000000)>>24; 
    b[5] = (extra_f&0xff0000)>>16; 
    b[6] = (extra_f&0xff00)>>8; 
    b[7] = extra_f&0xff; 
  } 
} 
#endif 
 
/* Position a string inside a field with fill */ 
 
static void fix_field(struct string_builder *r, 
                             PCHARP b, 
                             ptrdiff_t len, 
                             int flags, 
                             ptrdiff_t width, 
                             PCHARP pad_string, 
                             ptrdiff_t pad_length, 
                             char pos_pad) 
{ 
  ptrdiff_t e; 
  if(!width || width==SPRINTF_UNDECIDED) 
  { 
    if(pos_pad && EXTRACT_PCHARP(b)!='-') string_builder_putchar(r,pos_pad); 
    string_builder_append(r,b,len); 
    return; 
  } 
 
  if(!(flags & DO_TRUNC) && len+(pos_pad && EXTRACT_PCHARP(b)!='-')>=width) 
  { 
    if(pos_pad && EXTRACT_PCHARP(b)!='-') string_builder_putchar(r,pos_pad); 
    string_builder_append(r,b,len); 
    return; 
  } 
 
  if (flags & (ZERO_PAD|FIELD_CENTER|FIELD_LEFT)) { 
    /* Some flag is set. */ 
 
    if(flags & ZERO_PAD)            /* zero pad is kind of special... */ 
    { 
      if(EXTRACT_PCHARP(b)=='-') 
      { 
        string_builder_putchar(r,'-'); 
        INC_PCHARP(b,1); 
        len--; 
        width--; 
      }else{ 
        if(pos_pad) 
        { 
          string_builder_putchar(r,pos_pad); 
          width--; 
        } 
      } 
#if 1 
      string_builder_fill(r,width-len,MKPCHARP("0",0),1,0); 
#else 
      for(;width>len;width--) string_builder_putchar(r,'0'); 
#endif 
      string_builder_append(r,b,len); 
      return; 
    } 
 
    if (flags & (FIELD_CENTER|FIELD_LEFT)) { 
      ptrdiff_t d=0; 
 
      if(flags & FIELD_CENTER) 
      { 
        e=len; 
        if(pos_pad && EXTRACT_PCHARP(b)!='-') e++; 
        e=(width-e)/2; 
        if(e>0) 
        { 
          string_builder_fill(r, e, pad_string, pad_length, 0); 
          width-=e; 
        } 
      } 
 
      /* Left adjust */ 
      if(pos_pad && EXTRACT_PCHARP(b)!='-') 
      { 
        string_builder_putchar(r,pos_pad); 
        width--; 
        d++; 
      } 
 
#if 1 
      len = MINIMUM(width, len); 
      if (len) { 
        d += len; 
        string_builder_append(r, b, len); 
        width -= len; 
      } 
#else /* 0 */ 
      d+=MINIMUM(width,len); 
      while(len && width) 
      { 
        string_builder_putchar(r,EXTRACT_PCHARP(b)); 
        INC_PCHARP(b,1); 
        len--; 
        width--; 
      } 
#endif /* 1 */ 
 
      if(width>0) 
      { 
        d%=pad_length; 
        string_builder_fill(r, width, pad_string, pad_length, d); 
      } 
 
      return; 
    } 
  } 
 
  /* Right-justification */ 
 
  if(pos_pad && EXTRACT_PCHARP(b)!='-') len++; 
  e=width-len; 
  if(e>0) 
  { 
    string_builder_fill(r, e, pad_string, pad_length, 0); 
    width-=e; 
  } 
 
  if(pos_pad && EXTRACT_PCHARP(b)!='-' && len==width) 
  { 
    string_builder_putchar(r,pos_pad); 
    len--; 
    width--; 
  } 
  INC_PCHARP(b,len-width); 
  string_builder_append(r,b,width); 
} 
 
static void free_sprintf_strings(struct format_stack *fs) 
{ 
  struct format_info * f, * fend; 
  f = fs->fsp; 
  fend = fs->format_info_stack; 
  for(;f>=fend;f--) 
  { 
    if(f->fi_free_string) free(f->fi_free_string); 
#ifdef PIKE_DEBUG 
    f->fi_free_string=0; 
#endif 
    if(f->to_free_string) free_string(f->to_free_string); 
#ifdef PIKE_DEBUG 
    f->to_free_string=0; 
#endif 
  } 
  fs->fsp = f; 
} 
 
static void sprintf_error(struct format_stack *fs, 
                          char *s,...) ATTRIBUTE((noinline,noreturn,format (printf, 2, 3))); 
static void sprintf_error(struct format_stack *fs, 
                          char *s,...) 
{ 
  char buf[100]; 
  va_list args; 
  va_start(args,s); 
  free_sprintf_strings(fs); 
 
  sprintf(buf,"sprintf: %s",s); 
  va_error(buf,args); 
  va_end(args); 
} 
 
static void mapping_to_format_info(struct mapping * m, struct format_info * fsp, int * indent) { 
  struct svalue *sval; 
 
  if ((sval = simple_mapping_string_lookup(m, "precision")) && 
      (TYPEOF(*sval) == T_INT)) { 
    fsp->precision = sval->u.integer; 
  } 
  if ((sval = simple_mapping_string_lookup(m, "width")) && 
      (TYPEOF(*sval) == T_INT) && (sval->u.integer >= 0)) { 
    fsp->width = sval->u.integer; 
  } 
  if ((sval = simple_mapping_string_lookup(m, "flag_left")) && 
      (TYPEOF(*sval) == T_INT)) { 
    if (sval->u.integer) { 
      fsp->flags |= FIELD_LEFT; 
    } else { 
      fsp->flags &= ~FIELD_LEFT; 
    } 
  } 
  if ((sval = simple_mapping_string_lookup(m, "indent")) && 
      (TYPEOF(*sval) == T_INT) && (sval->u.integer >= 0)) { 
    *indent = sval->u.integer; 
  } 
} 
 
static int call_object_sprintf(int mode, struct object * o, ptrdiff_t fun, struct format_stack * fs) { 
  int n = 0; 
  struct format_info *fsp = fs->fsp; 
  DECLARE_CYCLIC(); 
 
  if (BEGIN_CYCLIC(o, fun)) { 
      END_CYCLIC(); 
      return 0; 
  } 
 
  push_int(mode); 
 
  if (fsp->precision!=SPRINTF_UNDECIDED) 
  { 
     push_constant_text("precision"); 
     push_int(fsp->precision); 
     n+=2; 
  } 
  if (fsp->width!=SPRINTF_UNDECIDED) 
  { 
     push_constant_text("width"); 
     push_int64(fsp->width); 
     n+=2; 
  } 
  if ((fsp->flags&FIELD_LEFT)) 
  { 
     push_constant_text("flag_left"); 
     push_int(1); 
     n+=2; 
  } 
  f_aggregate_mapping(n); 
 
  SET_CYCLIC_RET(1); 
 
  apply_low(o, fun, 2); 
 
  END_CYCLIC(); 
 
  if(TYPEOF(Pike_sp[-1]) == T_STRING) 
  { 
    DO_IF_DEBUG( if(fsp->to_free_string) 
                 Pike_fatal("OOps in sprintfn"); ) 
    fsp->to_free_string = (--Pike_sp)->u.string; 
 
    fsp->b = MKPCHARP_STR(fsp->to_free_string); 
    fsp->len = fsp->to_free_string->len; 
    return 1; 
  } 
 
  if (!SAFE_IS_ZERO(Pike_sp-1)) 
    sprintf_error(fs,"(object) returned illegal value from _sprintf()\n"); 
  pop_stack(); 
 
  return 0; 
} 
 
/* This is called once for every '%' on every output line 
 * it takes care of linebreak and column mode. It returns 1 
 * if there is more for next line. 
 */ 
 
static int do_one(struct format_stack *fs, 
                  struct string_builder *r, 
                  struct format_info *f) 
{ 
  PCHARP rest; 
  ptrdiff_t e, d; 
 
  rest.ptr=0; 
  if(f->flags & (LINEBREAK|ROUGH_LINEBREAK)) 
  { 
    ptrdiff_t lastspace = -1; 
 
    /* Format info widths are used as follows: 
     * 
     * column_width  Position where the string will be broken. 
     *               Defaults to same as width. 
     * 
     * precision     Maximum number of input characters per 
     *               (unbroken) line. Defaults to unlimited. (TODO) 
     * 
     * width         Minimum width to pad broken lines to. 
     *               Defaults to no padding. 
     */ 
    if(f->column_width <= 0) 
      f->column_width = f->width; 
    if(f->column_width <= 0) 
      sprintf_error(fs, "Must have a field width for linebreak.\n"); 
 
    for(e = 0; e < f->len && e <= f->column_width; e++) 
    { 
      switch(INDEX_PCHARP(f->b,e)) 
      { 
        case '\n': 
          lastspace = e; 
          break; 
 
        case ' ': 
          if(f->flags & LINEBREAK) 
          { 
            lastspace = e; 
          } 
          /* FALLTHRU */ 
 
        default: 
          continue; 
      } 
      break; 
    } 
 
    if (lastspace == -1) { 
      /* No suitable whitespace break point found. 
       * Perform a rough break at f->column_width. 
       */ 
      if (e <= f->column_width) { 
        lastspace = e; 
      } else { 
        lastspace = f->column_width; 
      } 
      rest = ADD_PCHARP(f->b, lastspace); 
    } else { 
      /* Break at the found whitespace, and advance past it. */ 
      rest = ADD_PCHARP(f->b, lastspace + 1); 
    } 
 
    fix_field(r, 
              f->b, 
              lastspace, 
              f->flags, 
              f->width, 
              f->pad_string, 
              f->pad_length, 
              f->pos_pad); 
  } 
  else if(f->flags & INVERSE_COLUMN_MODE) 
  { 
    if(f->width==SPRINTF_UNDECIDED) 
      sprintf_error(fs, "Must have field width for column mode.\n"); 
    e=f->width/(f->column_width+1); 
    if(!f->column_width || e<1) e=1; 
 
    rest=f->b; 
    for(d=0;INDEX_PCHARP(rest,d) && e;d++) 
    { 
 
      while(INDEX_PCHARP(rest,d) && INDEX_PCHARP(rest,d)!='\n') 
        d++; 
 
      fix_field(r, 
                rest, 
                d, 
                f->flags, 
                f->column_width, 
                f->pad_string, 
                f->pad_length, 
                f->pos_pad); 
 
      e--; 
      INC_PCHARP(rest,d); 
      d=-1; 
      if(EXTRACT_PCHARP(rest)) INC_PCHARP(rest,1); 
    } 
  } 
  else if(f->flags & COLUMN_MODE) 
  { 
    ptrdiff_t mod; 
    ptrdiff_t col; 
    PCHARP end; 
 
    if(f->width==SPRINTF_UNDECIDED) 
      sprintf_error(fs, "Must have field width for column mode.\n"); 
    mod=f->column_modulo; 
    col=f->width/(f->column_width+1); 
    if(!f->column_width || col<1) col=1; 
    rest=f->b; 
    end=ADD_PCHARP(rest,f->len); 
 
    for(d=0;rest.ptr && d<col;d++) 
    { 
 
      /* Find end of entry */ 
      for(e=0;COMPARE_PCHARP(ADD_PCHARP(rest, e),<,end) && 
            INDEX_PCHARP(rest,e)!='\n';e++); 
 
      fix_field(r,rest,e,f->flags,f->column_width, 
                f->pad_string,f->pad_length,f->pos_pad); 
 
      f->column_entries--; 
 
      /* Advance to after entry */ 
      INC_PCHARP(rest,e); 
      if(!COMPARE_PCHARP(rest,<,end)) break; 
      INC_PCHARP(rest,1); 
 
      for(e=1;e<mod;e++) 
      { 
        PCHARP s=MEMCHR_PCHARP(rest,'\n',SUBTRACT_PCHARP(end,rest)); 
        if(s.ptr) 
        { 
          rest=ADD_PCHARP(s,1); 
        }else{ 
          rest.ptr=0; 
          break; 
        } 
      } 
    } 
    if(f->column_entries>0) 
    { 
      for(rest=f->b;COMPARE_PCHARP(rest,<,end) && 
            EXTRACT_PCHARP(rest)!='\n';INC_PCHARP(rest,1)); 
      if(COMPARE_PCHARP(rest,<,end)) INC_PCHARP(rest,1); 
    }else{ 
      rest.ptr=0; 
    } 
  } 
  else 
  { 
    fix_field(r,f->b,f->len,f->flags,f->width, 
              f->pad_string,f->pad_length,f->pos_pad); 
  } 
 
  if(f->flags & REPEAT) return 0; 
  if(rest.ptr) 
  { 
    f->len-=SUBTRACT_PCHARP(rest,f->b); 
    f->b=rest; 
  }else{ 
    f->len=0; 
    f->b=MKPCHARP("",0); 
  } 
  return f->len>0; 
} 
 
#define POP_ARGUMENT() do { \ 
    if (arg) arg = NULL;      \ 
    else if (argument < num_arg) {\ 
        lastarg=argp+(argument++); \ 
    } \ 
    else sprintf_error(fs, "Too few arguments to sprintf.\n"); \ 
} while (0) 
 
#define GET_SVALUE(VAR) \ 
  if(arg) \ 
  { \ 
    VAR=arg; \ 
    arg=0; \ 
  }else{ \ 
    if(argument >= num_arg) \ 
    { \ 
      sprintf_error(fs, "Too few arguments to sprintf.\n"); \ 
      UNREACHABLE(break); \ 
    } \ 
    VAR=lastarg=argp+(argument++); \ 
  } 
 
#define PEEK_SVALUE(VAR) \ 
  if(arg) \ 
  { \ 
    VAR=arg; \ 
  }else{ \ 
    if(argument >= num_arg) \ 
    { \ 
      sprintf_error(fs, "Too few arguments to sprintf.\n"); \ 
      UNREACHABLE(break); \ 
    } \ 
    VAR=argp+argument; \ 
  } 
 
#define GET(VAR,PIKE_TYPE,TYPE_NAME,EXTENSION) \ 
  { \ 
    struct svalue *tmp_; \ 
    GET_SVALUE(tmp_); \ 
    if(TYPEOF(*tmp_) != PIKE_TYPE) \ 
    { \ 
      sprintf_error(fs, "Wrong type for argument %d: expected %s, got %s.\n",argument+1,TYPE_NAME, \ 
                    get_name_of_type(TYPEOF(*tmp_))); \ 
      UNREACHABLE(break); \ 
    } \ 
    VAR=tmp_->u.EXTENSION; \ 
  } 
 
#define GET_INT(VAR) GET(VAR,T_INT,"integer",integer) 
#define GET_STRING(VAR) GET(VAR,T_STRING,"string",string) 
#define GET_FLOAT(VAR) GET(VAR,T_FLOAT,"float",float_number) 
#define GET_ARRAY(VAR) GET(VAR,T_ARRAY,"array",array) 
#define GET_OBJECT(VAR) GET(VAR,T_OBJECT,"object",object) 
 
 
/* This is the main pike_sprintf function, note that it calls itself 
 * recursively during the '%{ %}' parsing. The string is stored in 
 * the buffer in save_objectII.c 
 */ 
static void low_pike_sprintf(struct format_stack *fs, 
                             struct string_builder *r, 
                             PCHARP format, 
                             ptrdiff_t format_len, 
                             struct svalue *argp, 
                             ptrdiff_t num_arg, 
                             int nosnurkel) 
{ 
  int argument=0; 
  int tmp,setwhat,d,e,indent; 
  char buffer[140]; 
  ptrdiff_t start; 
  struct format_info *f, *fsp; 
  FLOAT_TYPE tf; 
  struct svalue *arg=0;     /* pushback argument */ 
  struct svalue *lastarg=0; 
 
  PCHARP a,begin; 
  PCHARP format_end=ADD_PCHARP(format,format_len); 
 
  check_c_stack(500); 
 
  start=fs->fsp - fs->format_info_stack; 
  for(a=format;COMPARE_PCHARP(a,<,format_end);INC_PCHARP(a,1)) 
  { 
    int num_snurkel; 
 
    if(fs->fsp + 1 == fs->format_info_stack + fs->size) { 
      struct format_info * new; 
      ptrdiff_t diff; 
      fs->size *= 2; 
      new = realloc(fs->format_info_stack, fs->size * sizeof(struct format_info)); 
      if (!new) sprintf_error(fs, "Cannot allocate more sprintf stack.\n"); 
      diff = new - fs->format_info_stack; 
      fs->fsp = new + fs->size/2; 
      fs->format_info_stack = new; 
    } else fs->fsp++; 
#ifdef PIKE_DEBUG 
    if(fs->fsp < fs->format_info_stack) 
      Pike_fatal("sprintf: fs->fsp out of bounds.\n"); 
#endif 
    fsp = fs->fsp; 
    fsp->pad_string=MKPCHARP(" ",0); 
    fsp->pad_length=1; 
    fsp->fi_free_string=0; 
    fsp->to_free_string=0; 
    fsp->column_width=0; 
    fsp->pos_pad = 0; 
    fsp->flags = 0; 
    fsp->width=fsp->precision=SPRINTF_UNDECIDED; 
 
    if(EXTRACT_PCHARP(a)!='%') 
    { 
      for(e=0;INDEX_PCHARP(a,e)!='%' && 
            COMPARE_PCHARP(ADD_PCHARP(a,e),<,format_end);e++); 
      fsp->b=a; 
      fsp->len=e; 
      fsp->width=e; 
      INC_PCHARP(a,e-1); 
      continue; 
    } 
    num_snurkel=0; 
    arg=NULL; 
    setwhat=0; 
    begin=a; 
    indent = 0; 
 
    for(INC_PCHARP(a,1);;INC_PCHARP(a,1)) 
    { 
      int mode = EXTRACT_PCHARP(a); 
 
      switch (mode) { 
      case '{': 
      case 'n': 
      case 't': 
      case 'c': 
      case 'H': 
      case 'b': 
      case 'o': 
      case 'd': 
      case 'u': 
      case 'x': 
      case 'X': 
      case 'e': 
      case 'f': 
      case 'g': 
      case 'E': 
      case 'G': 
      case 'F': 
      case 'O': 
      case 'p': 
      case 's': 
      case 'q': 
        if(UNLIKELY(fsp->flags & SNURKEL)) 
        { 
          ONERROR _e; 
          struct array *_v; 
          struct string_builder _b; 
          init_string_builder(&_b,0); 
          SET_ONERROR(_e, free_string_builder, &_b); 
          GET_ARRAY(_v); 
          for(tmp=0;tmp<_v->size;tmp++) 
          { 
            struct svalue *save_sp=Pike_sp; 
            array_index_no_free(Pike_sp,_v,tmp); 
            Pike_sp++; 
            low_pike_sprintf(fs, &_b,begin,SUBTRACT_PCHARP(a,begin)+1, 
                             Pike_sp-1,1,nosnurkel+1); 
            fsp = fs->fsp; 
            if(save_sp < Pike_sp) pop_stack(); 
          } 
          fsp->b=MKPCHARP_STR(_b.s); 
          fsp->len=_b.s->len; 
          fsp->to_free_string=_b.s; 
          fsp->pad_string=MKPCHARP(" ",0); 
          fsp->pad_length=1; 
          fsp->column_width=0; 
          fsp->pos_pad=0; 
          fsp->flags=0; 
          fsp->width=fsp->precision=SPRINTF_UNDECIDED; 
          UNSET_ONERROR(_e); 
          break; 
        } 
      default: 
        goto cont_1; 
      } 
      break; 
 
cont_1: 
 
      switch (mode) { 
      case 't': 
      case 'c': 
      case 'H': 
      case 'b': 
      case 'o': 
      case 'd': 
      case 'u': 
      case 'x': 
      case 'X': 
      case 'e': 
      case 'f': 
      case 'g': 
      case 'E': 
      case 'G': 
      case 'F': 
        /* Its necessary to use CHECK_OBJECT_SPRINTF() here, 
           because describe_svalue encodes \t and others 
           when returned by _sprintf */ 
      case 'O': 
      case 's': 
      case 'q': 
        { 
          struct svalue *sv; 
          PEEK_SVALUE(sv); 
          if(TYPEOF(*sv) == T_OBJECT && sv->u.object->prog) 
          { 
            ptrdiff_t fun=FIND_LFUN(sv->u.object->prog, LFUN__SPRINTF); 
            if (fun != -1) { 
              if (call_object_sprintf(mode, sv->u.object, fun, fs)) { 
                POP_ARGUMENT(); 
                break; 
              } 
            } 
          } 
        } 
        /* FALLTHRU */ 
 
      default: 
        goto cont_2; 
      } 
      break; 
 
cont_2: 
 
      switch(mode) 
      { 
      default: 
        if(mode < 256 && isprint(mode)) 
        { 
          sprintf_error(fs, "Error in format string, %c is not a format.\n", 
                        mode); 
        }else{ 
          sprintf_error(fs,"Error in format string, U%08x is not a format.\n", 
                        mode); 
        } 
        break; 
 
      /* First the modifiers */ 
      case '0': 
         if (setwhat<2) 
         { 
            fsp->flags|=ZERO_PAD; 
            continue; 
         } 
         /* FALLTHRU */ 
      case '1': case '2': case '3': 
      case '4': case '5': case '6': 
      case '7': case '8': case '9': 
        tmp=STRTOL_PCHARP(a,&a,10); 
        INC_PCHARP(a,-1); 
        goto got_arg; 
 
      case '*': 
        { 
          struct svalue *sval; 
          struct mapping *m; 
          GET_SVALUE(sval); 
          if (TYPEOF(*sval) == T_INT) { 
            tmp = sval->u.integer; 
            goto got_arg; 
          } else if (TYPEOF(*sval) != T_MAPPING) { 
            sprintf_error(fs, "Wrong type for argument %d: " 
                          "expected %s, got %s.\n", 
                          argument+1, "int|mapping(string:int)", 
                          get_name_of_type(TYPEOF(*sval))); 
          } 
          mapping_to_format_info(sval->u.mapping, fsp, &indent); 
          continue; 
        } 
 
      got_arg: 
        switch(setwhat) 
        { 
        case 0: 
        case 1: 
          if(tmp < 0) sprintf_error(fs, "Illegal width %d.\n", tmp); 
          fsp->width=tmp; 
          if (!setwhat) break; 
          /* FALLTHRU */ 
        case 2: fsp->precision=tmp; break; 
        case 3: fsp->column_width=tmp; break; 
        case 4: fsp->precision=-tmp; break; 
        } 
        continue; 
 
      case ';': setwhat=3; continue; 
      case '.': setwhat=2; continue; 
      case ':': setwhat=1; continue; 
 
      case '=': fsp->flags|=LINEBREAK; 
        if (fsp->flags & ROUGH_LINEBREAK) 
          sprintf_error(fs, 
                        "Combining modifiers '=' and '/' is not allowed.\n"); 
        continue; 
      case '/': fsp->flags|=ROUGH_LINEBREAK; 
        if (fsp->flags & LINEBREAK) 
          sprintf_error(fs, 
                        "Combining modifiers '=' and '/' is not allowed.\n"); 
        continue; 
      case '#': fsp->flags|=COLUMN_MODE; continue; 
      case '$': fsp->flags|=INVERSE_COLUMN_MODE; continue; 
 
      case '-': 
        if(setwhat==2) 
          setwhat=4; 
        else 
          fsp->flags|=FIELD_LEFT; 
        continue; 
      case '|': fsp->flags|=FIELD_CENTER; continue; 
      case ' ': fsp->pos_pad=' '; continue; 
      case '+': fsp->pos_pad='+'; continue; 
      case '!': fsp->flags^=DO_TRUNC; continue; 
      case '^': fsp->flags|=REPEAT; continue; 
      case '>': fsp->flags|=MULTI_LINE_BREAK; continue; 
      case '_': fsp->flags|=WIDTH_OF_DATA; continue; 
      case '@': 
        if(++num_snurkel > nosnurkel) 
          fsp->flags|=SNURKEL; 
        continue; 
 
      case '\'': 
        tmp=0; 
        for(INC_PCHARP(a,1);INDEX_PCHARP(a,tmp)!='\'';tmp++) 
        { 
#if 0 
        fprintf(stderr, "Sprinf-glop: %d (%c)\n", 
                  INDEX_PCHARP(a,tmp), INDEX_PCHARP(a,tmp)); 
#endif 
        if(COMPARE_PCHARP(ADD_PCHARP(a, tmp),>=,format_end)) 
            sprintf_error(fs, "Unfinished pad string in format string.\n"); 
        } 
        if(tmp) 
        { 
          fsp->pad_string=a; 
          fsp->pad_length=tmp; 
        } 
        INC_PCHARP(a,tmp); 
        continue; 
 
      case '~': 
      { 
        struct pike_string *s; 
        GET_STRING(s); 
        if (s->len) { 
          fsp->pad_string=MKPCHARP_STR(s); 
          fsp->pad_length=s->len; 
        } 
        continue; 
      } 
 
      case '<': 
        if(!lastarg) 
          sprintf_error(fs, "No last argument.\n"); 
        arg=lastarg; 
        continue; 
 
      case '[': 
        INC_PCHARP(a,1); 
        if(EXTRACT_PCHARP(a)=='*') { 
          GET_INT(tmp); 
          INC_PCHARP(a,1); 
        } else 
          tmp=STRTOL_PCHARP(a,&a,10); 
        if(EXTRACT_PCHARP(a)!=']') 
          sprintf_error(fs, "Expected ] in format string, not %c.\n", 
                        EXTRACT_PCHARP(a)); 
        if(tmp >= num_arg) 
          sprintf_error(fs, "Not enough arguments to [%d].\n",tmp); 
        arg = argp+tmp; 
        continue; 
 
        /* now the real operators */ 
 
      case '{': 
      { 
        struct array *w; 
        struct string_builder b; 
        for(e=1,tmp=1;tmp;e++) 
        { 
          if (!INDEX_PCHARP(a,e) && 
              !COMPARE_PCHARP(ADD_PCHARP(a,e),<,format_end)) { 
            sprintf_error(fs, "Missing %%} in format string.\n"); 
            UNREACHABLE(break); 
          } else if(INDEX_PCHARP(a,e)=='%') { 
            switch(INDEX_PCHARP(a,e+1)) 
            { 
            case '%': e++; break; 
            case '}': tmp--; break; 
            case '{': tmp++; break; 
            } 
          } 
        } 
 
        GET_ARRAY(w); 
        if(!w->size) 
        { 
          fsp->b=MKPCHARP("",0); 
          fsp->len=0; 
        }else{ 
          ONERROR err; 
          init_string_builder(&b,0); 
          SET_ONERROR(err,free_string_builder,&b); 
          for(tmp=0;tmp<w->size;tmp++) 
          { 
            struct svalue *s; 
            union anything *q; 
 
/*        check_threads_etc(); */ 
            q=low_array_get_item_ptr(w,tmp,T_ARRAY); 
            s=Pike_sp; 
            if(q) 
            { 
              add_ref(q->array); 
              push_array_items(q->array); 
            }else{ 
              array_index_no_free(Pike_sp,w,tmp); 
              Pike_sp++; 
            } 
            low_pike_sprintf(fs, &b,ADD_PCHARP(a,1),e-2,s,Pike_sp-s,0); 
            fsp = fs->fsp; 
            pop_n_elems(Pike_sp-s); 
          } 
#ifdef PIKE_DEBUG 
        if(fs->fsp < fs->format_info_stack) 
            Pike_fatal("sprintf: fs->fsp out of bounds.\n"); 
#endif 
        fsp->b=MKPCHARP_STR(b.s); 
          fsp->len=b.s->len; 
          fsp->to_free_string=b.s; 
          UNSET_ONERROR(err); 
        } 
 
        INC_PCHARP(a,e); 
        break; 
      } 
 
      case '%': 
        fsp->b=MKPCHARP("%",0); 
        fsp->len=fsp->width=1; 
        break; 
 
      case 'n': 
        fsp->b=MKPCHARP("",0); 
        fsp->len=0; 
        break; 
 
      case 't': 
      { 
        struct svalue *t; 
        GET_SVALUE(t); 
        fsp->b = MKPCHARP(get_name_of_type(TYPEOF(*t)),0); 
        fsp->len=strlen((char *)fsp->b.ptr); 
        break; 
      } 
 
      case 'c': 
      { 
        INT_TYPE tmp; 
        ptrdiff_t l,n; 
        char *x; 
        if(fsp->width == SPRINTF_UNDECIDED) 
        { 
          GET_INT(tmp); 
          x=(char *)sa_alloc(&fs->a, 4); 
          if(tmp<256) fsp->b=MKPCHARP(x,0); 
          else if(tmp<65536) fsp->b=MKPCHARP(x,1); 
          else  fsp->b=MKPCHARP(x,2); 
          SET_INDEX_PCHARP(fsp->b,0,tmp); 
          fsp->len=1; 
        } 
        else if ( (fsp->flags&FIELD_LEFT) ) 
        { 
          l=1; 
          if(fsp->width > 0) l=fsp->width; 
          x=(char *)sa_alloc(&fs->a, l); 
          fsp->b=MKPCHARP(x,0); 
          fsp->len=l; 
          GET_INT(tmp); 
          n=0; 
          while(n<l) 
          { 
            x[n++]=tmp & 0xff; 
            tmp>>=8; 
          } 
        } 
        else 
        { 
          l=1; 
          if(fsp->width > 0) l=fsp->width; 
          x=(char *)sa_alloc(&fs->a, l); 
          fsp->b=MKPCHARP(x,0); 
          fsp->len=l; 
          GET_INT(tmp); 
          while(--l>=0) 
          { 
            x[l]=tmp & 0xff; 
            tmp>>=8; 
          } 
        } 
        break; 
      } 
 
      case 'H': 
      { 
        struct string_builder buf; 
        struct pike_string *s; 
        ptrdiff_t tmp; 
        ptrdiff_t l,n; 
        char *x; 
 
        GET_STRING(s); 
        if( s->size_shift ) 
          sprintf_error(fs, "%%H requires all characters in the string " 
                        "to be at most eight bits large\n"); 
 
        tmp = s->len; 
        l=1; 
        if(fsp->width > 0) 
          l=fsp->width; 
        else if(fsp->flags&ZERO_PAD) 
          sprintf_error(fs, "Length of string to %%H is 0.\n"); 
 
        /* Note: The >>-operator performs an implicit % (sizeof(tmp)*8) 
         *       on the shift operand on some architectures. */ 
        if( (l < (ptrdiff_t) sizeof(tmp)) && (tmp>>(l*8))) 
          sprintf_error(fs, "Length of string to %%%"PRINTPTRDIFFT"dH " 
                        "too large.\n", l); 
 
 
        x=(char *)sa_alloc(&fs->a, l); 
        fsp->b=MKPCHARP(x,0); 
        fsp->len=l; 
 
        if ( (fsp->flags&FIELD_LEFT) ) 
        { 
          n=0; 
          while(n<l) 
          { 
            x[n++]=tmp & 0xff; 
            tmp>>=8; 
          } 
        } 
        else 
        { 
          while(--l>=0) 
          { 
            x[l]=tmp & 0xff; 
            tmp>>=8; 
          } 
        } 
 
        init_string_builder_alloc(&buf, s->len+fsp->len, 0); 
        string_builder_append(&buf,fsp->b,fsp->len); 
        string_builder_shared_strcat(&buf,s); 
 
        fsp->b = MKPCHARP_STR(buf.s); 
        fsp->len = buf.s->len; 
        buf.s->len = buf.malloced; 
        fsp->to_free_string = buf.s; 
        break; 
      } 
 
      case 'x': 
      case 'X': 
      { 
        struct svalue *v; 
        PEEK_SVALUE(v); 
        if(TYPEOF(*v)==T_STRING) 
        { 
          struct pike_string *str; 
          push_svalue(v); 
          if( mode=='X' ) 
          { 
            push_int(1); 
            f_string2hex(2); 
          } 
          else 
            f_string2hex(1); 
 
          str = Pike_sp[-1].u.string; 
          fsp->b = MKPCHARP_STR(str); 
          fsp->len = str->len; 
          fsp->to_free_string = str; 
 
          add_ref(str); 
          pop_stack(); 
          POP_ARGUMENT(); 
          break; 
        } 
      } 
      /* FALLTHRU */ 
      case 'b': 
      case 'o': 
      case 'd': 
      case 'u': 
      { 
        int base = 0, mask_size = 0; 
       char *x; 
        INT_TYPE val; 
 
        GET_INT(val); 
 
        if(fsp->precision != SPRINTF_UNDECIDED && fsp->precision > 0) 
          mask_size = fsp->precision; 
 
        x=(char *)sa_alloc(&fs->a, sizeof(val)*CHAR_BIT + 4 + mask_size); 
        fsp->b=MKPCHARP(x,0); 
 
        switch(mode) 
        { 
          case 'b': base = 1; break; 
          case 'o': base = 3; break; 
          case 'x': base = 4; break; 
          case 'X': base = 4; break; 
        } 
 
        if(base) 
        { 
          char *p = x; 
          ptrdiff_t l; 
 
          if(mask_size || val>=0) 
          { 
            do { 
              if((*p++ = '0'|(val&((1<<base)-1)))>'9') 
                p[-1] += (mode=='X'? 'A'-'9'-1 : 'a'-'9'-1); 
              val >>= base; 
            } while(--mask_size && val); 
            l = p-x; 
          } 
          else 
          { 
            *p++ = '-'; 
            val = -val; 
            do { 
              if((*p++ = '0'|(val&((1<<base)-1)))>'9') 
                p[-1] += (mode=='X'? 'A'-'9'-1 : 'a'-'9'-1); 
              val = ((unsigned INT_TYPE)val) >> base; 
            } while(val); 
            l = p-x-1; 
          } 
          *p = '\0'; 
          while(l>1) { 
            char t = p[-l]; 
            p[-l] = p[-1]; 
            p[-1] = t; 
            --p; 
            l -= 2; 
          } 
        } 
        else if(mode == 'u') 
          sprintf(x, "%"PRINTPIKEINT"u", (unsigned INT_TYPE) val); 
        else 
          sprintf(x, "%"PRINTPIKEINT"d", val); 
 
        fsp->len=strlen(x); 
        break; 
      } 
 
      case 'e': 
      case 'f': 
      case 'g': 
      case 'E': 
      case 'G': 
      { 
        char *x; 
        GET_FLOAT(tf); 
 
        /* Special casing for infinity and not a number, 
         * since many libc's forget about them... 
         */ 
        if (PIKE_ISNAN(tf)) { 
          /* NaN */ 
          fsp->b = MKPCHARP("nan", 0); 
          fsp->len = 3; 
          break; 
        } else if (PIKE_ISINF(tf)) { 
          /* Infinity. */ 
          if (tf > 0.0) { 
            fsp->b = MKPCHARP("inf", 0); 
            fsp->len = 3; 
          } else { 
            fsp->b = MKPCHARP("-inf", 0); 
            fsp->len = 4; 
          } 
          break; 
        } 
 
        if (fsp->precision==SPRINTF_UNDECIDED) fsp->precision=3; 
 
        /* FIXME: The constant (320) is good for IEEE double precision 
         * float, but will definitely fail for bigger precision! --aldem 
         */ 
        x=(char *)xalloc(320+MAXIMUM(fsp->precision,3)); 
        fsp->fi_free_string=x; 
        fsp->b=MKPCHARP(x,0); 
        sprintf(buffer,"%%*.*"PRINTPIKEFLOAT"%c", mode); 
 
        if(fsp->precision<0) { 
#if SIZEOF_FLOAT_TYPE > SIZEOF_DOUBLE 
        FLOAT_TYPE m=powl(10.0, (double)fsp->precision); 
          tf = rintl(tf*m)/m; 
#else 
        double m=pow(10.0, (double)fsp->precision); 
          tf = rint(tf*m)/m; 
#endif 
      } else if (fsp->precision==0) { 
#if SIZEOF_FLOAT_TYPE > SIZEOF_DOUBLE 
        tf = rintl(tf); 
#else 
        tf = rint(tf); 
#endif 
        } 
 
        debug_malloc_touch(x); 
        sprintf(x,buffer,1,fsp->precision<0?0:fsp->precision,tf); 
        debug_malloc_touch(x); 
        fsp->len=strlen(x); 
 
        /* Make sure that the last digits really are zero. */ 
        if(fsp->precision<0) 
        { 
          ptrdiff_t i, j; 
          /* Find the ending of the number.  Yes, this can be made 
             simpler now when the alignment bug for floats is fixed. */ 
          for(i=fsp->len-1; i>=0; i--) 
            if('0'<=x[i] && x[i]<='9') 
            { 
              i+=fsp->precision+1; 
              if(i>=0 && '0'<=x[i] && x[i]<='9') 
                for(j=0; j<-fsp->precision; j++) 
                  x[i+j]='0'; 
              break; 
            } 
        } 
        break; 
      } 
 
      case 'F': 
      { 
        ptrdiff_t l; 
        char *x; 
        l=4; 
        if(fsp->width > 0) l=fsp->width; 
        if(l != 4 && l != 8) 
          sprintf_error(fs, "Invalid IEEE width %ld.\n", (long)l); 
        x=(char *)sa_alloc(&fs->a, l); 
        fsp->b=MKPCHARP(x,0); 
        fsp->len=l; 
        GET_FLOAT(tf); 
        switch(l) { 
        case 4: 
          { 
            float f = (float)tf; 
#if SIZEOF_FLOAT_TYPE > 4 
          /* Some paranoia in case libc doesn't handle 
             * conversion to denormalized floats. */ 
            if ((f != 0.0) || (tf == 0.0)) { 
#endif 
#ifdef FLOAT_IS_IEEE_BIG 
            memcpy(x, &f, 4); 
#elif defined(FLOAT_IS_IEEE_LITTLE) 
            x[0] = ((char *)&f)[3]; 
              x[1] = ((char *)&f)[2]; 
              x[2] = ((char *)&f)[1]; 
              x[3] = ((char *)&f)[0]; 
#else 
            low_write_IEEE_float(x, tf, 4); 
#endif /* IEEE */ 
#if SIZEOF_FLOAT_TYPE > 4 
            break; 
            } 
            low_write_IEEE_float(x, tf, 4); 
#endif 
        } 
          break; 
        case 8: 
          { 
#ifdef DOUBLE_IS_IEEE_BIG 
          double d = (double)tf; 
            memcpy(x, &d, 8); 
#elif defined(DOUBLE_IS_IEEE_LITTLE) 
          double d = (double)tf; 
            x[0] = ((char *)&d)[7]; 
            x[1] = ((char *)&d)[6]; 
            x[2] = ((char *)&d)[5]; 
            x[3] = ((char *)&d)[4]; 
            x[4] = ((char *)&d)[3]; 
            x[5] = ((char *)&d)[2]; 
            x[6] = ((char *)&d)[1]; 
            x[7] = ((char *)&d)[0]; 
#else 
          low_write_IEEE_float(x, tf, 8); 
#endif 
        } 
        } 
        if (fsp->flags & FIELD_LEFT) { 
          /* Reverse the byte order. */ 
          int i; 
          char c; 
          l--; 
          for (i=0; i < (l-i); i++) { 
            c = x[i]; 
            x[i] = x[l-i]; 
            x[l-i] = c; 
          } 
        } 
        break; 
      } 
 
      case 'O': 
      { 
        struct svalue *t; 
        GET_SVALUE(t); 
        { 
          struct byte_buffer buf = BUFFER_INIT(); 
 
          describe_svalue(&buf,t,indent,0); 
          fsp->b=MKPCHARP(buffer_ptr(&buf),0); 
          fsp->len=buffer_content_length(&buf); 
          fsp->fi_free_string=buffer_ptr(&buf); 
          break; 
        } 
      } 
 
      case 'p': 
      { 
        struct svalue *t; 
        struct pike_string *str; 
        GET_SVALUE(t); 
        if (TYPEOF(*t) == T_INT) { 
          push_string(make_shared_binary_string((void *)&t->u.integer, 
                                                sizeof(INT_TYPE))); 
        } else if (TYPEOF(*t) == T_FLOAT) { 
          push_string(make_shared_binary_string((void *)&t->u.float_number, 
                                                sizeof(FLOAT_TYPE))); 
        } else { 
          push_string(make_shared_binary_string((void *)&t->u.refs, sizeof(void *))); 
        } 
        push_int((PIKE_BYTEORDER == 1234)?2:0); 
        f_string2hex(2); 
 
        str = Pike_sp[-1].u.string; 
        fsp->b = MKPCHARP_STR(str); 
        fsp->len = str->len; 
        fsp->to_free_string = str; 
 
        add_ref(str); 
        pop_stack(); 
        break; 
      } 
 
      case 's': 
      { 
        struct pike_string *s; 
        GET_STRING(s); 
        fsp->b=MKPCHARP_STR(s); 
        fsp->len=s->len; 
        if(fsp->precision != SPRINTF_UNDECIDED && fsp->precision < fsp->len) 
          fsp->len = (fsp->precision < 0 ? 0 : fsp->precision); 
        break; 
      } 
 
      case 'q': 
      { 
        struct string_builder buf; 
        struct pike_string *s; 
 
        GET_STRING(s); 
 
        init_string_builder_alloc(&buf, s->len+2, 0); 
        string_builder_putchar(&buf, '"'); 
        string_builder_quote_string(&buf, s, 0, 
                                    (fsp->precision == SPRINTF_UNDECIDED)? 
                                    0x7fffffff:fsp->precision, 
                                    QUOTE_NO_STRING_CONCAT); 
        string_builder_putchar(&buf, '"'); 
 
        fsp->b = MKPCHARP_STR(buf.s); 
        fsp->len = buf.s->len; 
        /* NOTE: We need to do this since we're not 
         *       using free_string_builder(). */ 
        buf.s->len = buf.malloced; 
        fsp->to_free_string = buf.s; 
        break; 
      } 
      } 
      break; 
    } 
  } 
 
  for(f=fs->fsp;f>fs->format_info_stack + start;f--) 
  { 
    if(f->flags & WIDTH_OF_DATA) 
      f->width=f->len; 
 
    if(((f->flags & INVERSE_COLUMN_MODE) && !f->column_width) || 
       (f->flags & COLUMN_MODE)) 
    { 
      int max_len,nr; 
      ptrdiff_t columns; 
      tmp=1; 
      for(max_len=nr=e=0;e<f->len;e++) 
      { 
        if(INDEX_PCHARP(f->b,e)=='\n') 
        { 
          nr++; 
          if(max_len<tmp) max_len=tmp; 
          tmp=0; 
        } 
        tmp++; 
      } 
      nr++; 
      if(max_len<tmp) 
        max_len=tmp; 
      if(!f->column_width) 
        f->column_width=max_len; 
      f->column_entries=nr; 
      columns=f->width/(f->column_width+1); 
 
      if(f->column_width<1 || columns<1) 
        columns=1; 
      f->column_modulo=(nr+columns-1)/columns; 
    } 
  } 
 
  /* Here we do some DWIM */ 
  for(f=fs->fsp-1;f>fs->format_info_stack + start;f--) 
  { 
    if((f[1].flags & MULTILINE) && 
       !(f[0].flags & (MULTILINE|MULTI_LINE_BREAK))) 
    { 
      if(! MEMCHR_PCHARP(f->b, '\n', f->len).ptr ) f->flags|=MULTI_LINE; 
    } 
  } 
 
  for(f=fs->format_info_stack + start+1;f<=fs->fsp;) 
  { 
    for(;f<=fs->fsp && !(f->flags&MULTILINE);f++) 
      do_one(fs, r, f); 
 
    do { 
      d=0; 
      for(e=0;f+e<=fs->fsp && (f[e].flags & MULTILINE);e++) 
        d |= do_one(fs, r, f+e); 
      if(d) 
        string_builder_putchar(r,'\n'); 
    } while(d); 
 
    for(;f<=fs->fsp && (f->flags&MULTILINE); f++); 
  } 
 
  f = fs->fsp; 
  fsp = fs->format_info_stack + start; 
  while(f > fsp) 
  { 
#ifdef PIKE_DEBUG 
    if(f < fs->format_info_stack) 
      Pike_fatal("sprintf: fsp out of bounds.\n"); 
#endif 
 
    if(f->fi_free_string) 
      free(f->fi_free_string); 
#ifdef PIKE_DEBUG 
    f->fi_free_string=0; 
#endif 
 
    if(f->to_free_string) 
      free_string(f->to_free_string); 
#ifdef PIKE_DEBUG 
    f->to_free_string=0; 
#endif 
 
    f--; 
  } 
  fs->fsp = f; 
} 
 
static void free_f_sprintf_data (struct format_stack *fs) 
{ 
  free_sprintf_strings (fs); 
  stack_alloc_destroy(&fs->a); 
  free (fs->format_info_stack); 
} 
 
/* The efun */ 
void low_f_sprintf(INT32 args, struct string_builder *r) 
{ 
  ONERROR uwp; 
  struct pike_string *ret; 
  struct svalue *argp; 
  struct format_stack fs; 
 
  argp=Pike_sp-args; 
 
  if(TYPEOF(argp[0]) != T_STRING) { 
    if (TYPEOF(argp[0]) == T_OBJECT) { 
      /* Try checking if we can cast it to a string... */ 
      ref_push_object(argp[0].u.object); 
      o_cast(string_type_string, PIKE_T_STRING); 
      if (TYPEOF(Pike_sp[-1]) != T_STRING) { 
        /* We don't accept objects... */ 
        Pike_error("Cast to string failed.\n"); 
      } 
      /* Replace the original object with the new string. */ 
      assign_svalue(argp, Pike_sp-1); 
      /* Clean up the stack. */ 
      pop_stack(); 
    } else { 
      SIMPLE_ARG_TYPE_ERROR("sprintf", 1, "string|object"); 
    } 
  } 
 
  fs.size = round_up32(args*2); 
  stack_alloc_init(&fs.a, 128); /* this should scale with fs.size */ 
  fs.format_info_stack = xalloc(fs.size*sizeof(struct format_info)); 
  fs.fsp = fs.format_info_stack - 1; 
 
  SET_ONERROR(uwp, free_f_sprintf_data, &fs); 
  low_pike_sprintf(&fs, 
                   r, 
                   MKPCHARP_STR(argp->u.string), 
                   argp->u.string->len, 
                   argp+1, 
                   args-1, 
                   0); 
  UNSET_ONERROR(uwp); 
  stack_alloc_destroy(&fs.a); 
  free (fs.format_info_stack); 
} 
 
void f_sprintf(INT32 args) 
{ 
  ONERROR uwp; 
  struct string_builder r; 
  SET_ONERROR(uwp, free_string_builder, &r); 
  init_string_builder(&r,0); 
  low_f_sprintf(args, &r); 
  UNSET_ONERROR(uwp); 
  pop_n_elems(args); 
  push_string(finish_string_builder(&r)); 
} 
 
#define PSAT_INVALID  1 
#define PSAT_MARKER 2 
#define PSAT_SMARKER        4 
 
/* Push the types corresponding to the %-directives in the format string. 
 * 
 *   severity is the severity level if any syntax errors 
 *            are encountered in the format string. 
 * 
 * Returns -1 on syntax error. 
 *         LSB 1 on unhandled (ie position-dependent args). 
 *         LSB 0 on success. 
 * 
 *         PSAT_MARKER is set if the marker is in use. 
 * 
 *         *min_charp is <= *max_charp if a static character range 
 *         has been detected (eg constant string segment, etc). 
 */ 
static int push_sprintf_argument_types(PCHARP format, 
                                       ptrdiff_t format_len, 
                                       int ret, 
                                       int int_marker, 
                                       int str_marker, 
                                       p_wchar2 *min_charp, 
                                       p_wchar2 *max_charp, 
                                       int severity) 
{ 
  int tmp, setwhat, e; 
  int uses_marker = 0; 
  struct svalue *arg=0;     /* pushback argument */ 
  struct svalue *lastarg=0; 
 
  PCHARP a,begin; 
  PCHARP format_end=ADD_PCHARP(format,format_len); 
 
  p_wchar2 min_char = *min_charp; 
  p_wchar2 max_char = *max_charp; 
 
  check_c_stack(500); 
 
  /* if (num_arg < 0) num_arg = MAX_INT32; */ 
 
  for(a=format;COMPARE_PCHARP(a,<,format_end);INC_PCHARP(a,1)) 
  { 
    int num_snurkel, column; 
    ptrdiff_t width = 0; 
    p_wchar2 c; 
    int uses_tilde = 0; 
 
    if(EXTRACT_PCHARP(a)!='%') 
    { 
      for(e=0;(c = INDEX_PCHARP(a,e)) != '%' && 
            COMPARE_PCHARP(ADD_PCHARP(a,e),<,format_end);e++) { 
        if (c < min_char) min_char = c; 
        if (c > max_char) max_char = c; 
      } 
      INC_PCHARP(a,e-1); 
      continue; 
    } 
    num_snurkel=0; 
    column=0; 
    arg=NULL; 
    setwhat=0; 
    begin=a; 
 
    for(INC_PCHARP(a,1);;INC_PCHARP(a,1)) 
    { 
      switch(c = EXTRACT_PCHARP(a)) 
      { 
      default: 
        if(c < 256 && isprint(c)) 
        { 
          yyreport(severity, type_check_system_string, 
                   0, "Error in format string, %c is not a format.", 
                   c); 
        }else{ 
          yyreport(severity, type_check_system_string, 
                   0, "Error in format string, U%08x is not a format.", 
                   c); 
        } 
        ret = -1; 
        num_snurkel = 0; 
        break; 
 
      /* First the modifiers */ 
      case '*': 
        if (setwhat < 2) { 
          push_int_type(0, MAX_INT32); 
          if (uses_tilde) { 
            max_char = 0x7fffffff; 
            min_char = -0x80000000; 
          } else { 
            if (' ' < min_char) min_char = ' '; 
            if (' ' > max_char) max_char = ' '; 
          } 
        } else { 
          push_int_type(MIN_INT32, MAX_INT32); 
        } 
        /* Allow a mapping in all cases. */ 
        push_int_type(MIN_INT32, MAX_INT32); 
        push_unlimited_array_type(T_STRING); 
        push_int_type(MIN_INT32, MAX_INT32); 
        push_reverse_type(T_MAPPING); 
        push_type(T_OR); 
        continue; 
 
      case '0': 
        if (setwhat<2) continue; 
        /* FALLTHRU */ 
      case '1': case '2': case '3': 
      case '4': case '5': case '6': 
      case '7': case '8': case '9': 
        tmp=STRTOL_PCHARP(a,&a,10); 
        INC_PCHARP(a,-1); 
        switch(setwhat) 
        { 
        case 0: case 1: 
          if(tmp < 0) { 
            yyreport(severity, type_check_system_string, 
                     0, "Illegal width %d.", tmp); 
            ret = -1; 
          } 
          width = tmp; 
          if (width) { 
            if (uses_tilde) { 
              max_char = 0x7fffffff; 
              min_char = -0x80000000; 
            } else { 
              if (' ' < min_char) min_char = ' '; 
              if (' ' > max_char) max_char = ' '; 
            } 
          } 
        /* FALLTHRU */ 
        case 2: case 3: case 4: break; 
        } 
        setwhat++; 
        continue; 
 
      case ';': setwhat=3; continue; 
      case '.': setwhat=2; continue; 
      case ':': setwhat=1; continue; 
      case '-': 
        if(setwhat==2) setwhat=4; 
        continue; 
 
      case '/': 
        column |= ROUGH_LINEBREAK; 
        if( column & LINEBREAK ) { 
          yyreport(severity, type_check_system_string, 
                   0, "Can not use both the modifiers / and =."); 
          ret = -1; 
        } 
        continue; 
 
      case '=': 
        column |= LINEBREAK; 
        if( column & ROUGH_LINEBREAK ) { 
          yyreport(severity, type_check_system_string, 
                   0, "Can not use both the modifiers / and =."); 
          ret = -1; 
        } 
        if ('\n' < min_char) min_char = '\n'; 
        if (' ' > max_char) max_char = ' '; 
        continue; 
 
      case '#': case '$': case '>': 
        if ('\n' < min_char) min_char = '\n'; 
        /* FALLTHRU */ 
      case '|': case ' ': case '+': 
        if (' ' > max_char) max_char = ' '; 
        if (' ' < min_char) min_char = ' '; 
        /* FALLTHRU */ 
      case '!': case '^': case '_': 
        continue; 
 
      case '@': 
        ++num_snurkel; 
        continue; 
 
      case '\'': 
        tmp=0; 
        for(INC_PCHARP(a,1); 
            COMPARE_PCHARP(ADD_PCHARP(a, tmp),<,format_end) 
              && (c = INDEX_PCHARP(a,tmp)) != '\'';tmp++) { 
          if (c < min_char) min_char = c; 
          if (c > max_char) max_char = c; 
        } 
 
        if (COMPARE_PCHARP(ADD_PCHARP(a, tmp),<,format_end)) { 
            INC_PCHARP(a,tmp); 
            continue; 
        } else { 
            INC_PCHARP(a,tmp); 
            yyreport(severity, type_check_system_string, 
                     0, "Unfinished pad string in format string."); 
            ret = -1; 
            break; 
        } 
 
      case '~': 
      { 
        push_finished_type(int_type_string); 
        if (int_marker) { 
          push_assign_type(int_marker); 
          uses_marker = 1; 
        } 
        push_unlimited_array_type(T_STRING); 
        uses_tilde = 1; 
        continue; 
      } 
 
      case '<': 
        /* FIXME: !!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ 
        ret |= PSAT_INVALID;    /* FAILURE! */ 
#if 0 
      if(!lastarg) { 
          yyreport(severity, type_check_system_string, 
                   0, "No last argument."); 
          ret = -1; 
        } 
        arg=lastarg; 
#endif /* 0 */ 
      continue; 
 
      case '[': 
        /* FIXME: !!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ 
        ret |= PSAT_INVALID;    /* FAILURE! */ 
        INC_PCHARP(a,1); 
        if(EXTRACT_PCHARP(a)=='*') { 
          push_int_type(0, /*num_arg*/ MAX_INT32); 
          INC_PCHARP(a,1); 
        } 
        else 
          STRTOL_PCHARP(a,&a,10); 
        if((c = EXTRACT_PCHARP(a))!=']')  { 
          yyreport(severity, type_check_system_string, 
                   0, "Expected ] in format string, not %c.", 
                   c); 
          ret = -1; 
        } 
        continue; 
 
        /* now the real operators */ 
 
      case '{': 
      { 
        ptrdiff_t cnt; 
        for(e=1,tmp=1;tmp;e++) 
        { 
          if (!INDEX_PCHARP(a,e) && 
              !COMPARE_PCHARP(ADD_PCHARP(a,e),<,format_end)) { 
            yyreport(severity, type_check_system_string, 
                     0, "Missing %%} in format string."); 
            ret = -1; 
            break; 
          } else if((c = INDEX_PCHARP(a,e)) == '%') { 
            switch(INDEX_PCHARP(a,e+1)) 
            { 
            case '%': e++; break; 
            case '}': tmp--; break; 
            case '{': tmp++; break; 
            } 
          } else { 
            if (c < min_char) min_char = c; 
            if (c > max_char) max_char = c; 
          } 
        } 
 
        type_stack_mark(); 
        /* Note: No need to check the return value, since we 
         *       simply or all the types together. Thus the 
         *       argument order isn't significant. 
         * 
         * ... Unless there was a syntax error... 
         */ 
        ret = push_sprintf_argument_types(ADD_PCHARP(a,1), e-2, ret, 
                                          int_marker, str_marker, 
                                          &min_char, &max_char, 
                                          severity); 
        /* Join the argument types for our array. */ 
        push_type(PIKE_T_ZERO); 
        for (cnt = pop_stack_mark(); cnt > 1; cnt--) { 
          push_type(T_OR); 
        } 
        push_finished_type(peek_type_stack());  /* dup() */ 
        push_unlimited_array_type(PIKE_T_ARRAY); 
        push_type(T_OR); 
 
        push_unlimited_array_type(PIKE_T_ARRAY); 
 
        INC_PCHARP(a,e); 
        break; 
      } 
 
      case '%': 
        num_snurkel = 0; 
        if ('%' < min_char) min_char = '%'; 
        if ('%' > max_char) max_char = '%'; 
        break; 
 
      case 'n': 
        if (num_snurkel) { 
          /* NOTE: Does take an argument if '@' is active! */ 
          push_type(PIKE_T_ZERO); 
        } 
        break; 
 
      case 't': 
      { 
        push_type(T_MIXED); 
 
        /* Lower-case ASCII */ 
        if ('a' < min_char) min_char = 'a'; 
        if ('z' > max_char) max_char = 'z'; 
        break; 
      } 
 
      case 'c': 
      { 
        push_object_type(0, 0); 
        push_int_type(MIN_INT32, MAX_INT32); 
        push_type(T_OR); 
        if (!width) { 
          if (int_marker) { 
            push_assign_type(int_marker); 
            ret |= PSAT_MARKER; 
          } 
        } else { 
          if (min_char > 0) min_char = 0; 
          if (max_char < 255) max_char = 255; 
        } 
        break; 
      } 
 
      case 'x': 
        if ('f' > max_char) max_char = 'f'; 
        /* FALLTHRU */ 
      case 'X': 
        if ('F' > max_char) max_char = 'F'; 
        if ('+' < min_char) min_char = '+'; 
        push_int_type(0, 255); 
        push_unlimited_array_type(T_STRING); 
        push_object_type(0, 0); 
        push_type(T_OR); 
        push_int_type(MIN_INT32, MAX_INT32); 
        push_type(T_OR); 
        break; 
 
      case 'd': 
      case 'u': 
        if ('9' > max_char) max_char = '9'; 
        /* FALLTHRU */ 
      case 'o': 
        if ('7' > max_char) max_char = '7'; 
        /* FALLTHRU */ 
      case 'b': 
        if ('1' > max_char) max_char = '1'; 
        if ('+' < min_char) min_char = '+'; 
      { 
        push_object_type(0, 0); 
        push_int_type(MIN_INT32, MAX_INT32); 
        push_type(T_OR); 
        break; 
      } 
 
      case 'e': 
      case 'f': 
      case 'g': 
      case 'E': 
      case 'G': 
      { 
        push_object_type(0, 0); 
        push_type(PIKE_T_FLOAT); 
        push_type(T_OR); 
        if ('e' > max_char) max_char = 'e'; 
        if ('+' < min_char) min_char = '+'; 
        break; 
      } 
 
      case 'F': 
      { 
        push_object_type(0, 0); 
        push_type(PIKE_T_FLOAT); 
        push_type(T_OR); 
        if (min_char > 0) min_char = 0; 
        if (max_char < 255) max_char = 255; 
        break; 
      }  
 
      case 'O': 
      { 
        push_type(T_MIXED); 
        max_char = 0x7fffffff; 
        min_char = -0x80000000; 
        break; 
      } 
 
      case 'p': 
      { 
        push_type(T_MIXED); 
        break; 
      } 
      case 'H': 
      { 
        push_object_type(0, 0); 
        push_int_type(0, 255); 
        push_unlimited_array_type(T_STRING); 
        push_type(T_OR); 
        if (min_char > 0) min_char = 0; 
        if (max_char < 255) max_char = 255; 
        break; 
      } 
 
      case 'q': 
      { 
        push_object_type(0, 0); 
        push_finished_type(int_type_string); 
        push_unlimited_array_type(T_STRING); 
        push_type(T_OR); 
        if (min_char > 32) min_char = 32;   // Control characters and 
        if (max_char < 255) max_char = 255; // non-8-bit are escaped. 
        break; 
      } 
 
      case 's': 
      { 
        push_object_type(0, 0); 
        push_finished_type(int_type_string); 
        push_unlimited_array_type(T_STRING); 
        push_type(T_OR); 
        if (str_marker) { 
          push_assign_type(str_marker); 
        } 
        ret |= PSAT_SMARKER; 
        break; 
      } 
 
      } 
      break; 
    } 
    while (num_snurkel--) push_unlimited_array_type(T_ARRAY); 
  } 
 
  *min_charp = min_char; 
  *max_charp = max_char; 
  return ret; 
} 
 
static node *optimize_sprintf(node *n) 
{ 
  node **arg0 = my_get_arg(&_CDR(n), 0); 
  node **arg1 = my_get_arg(&_CDR(n), 1); 
  node *ret = NULL; 
  int num_args=count_args(CDR(n)); 
  if(arg0 && 
     (*arg0)->token == F_CONSTANT && 
     TYPEOF((*arg0)->u.sval) == T_STRING) 
  { 
    /* First argument is a constant string. */ 
    struct pike_string *fmt = (*arg0)->u.sval.u.string; 
 
    if(arg1 && num_args == 2 && 
       fmt->size_shift == 0 && fmt->len == 2 && STR0(fmt)[0]=='%') 
    { 
      /* First argument is a two character format string. */ 
      switch(STR0(fmt)[1]) 
      { 
      case 'c': 
        ADD_NODE_REF2(*arg1, 
                      ret = mkefuncallnode("int2char",*arg1); 
          ); 
        return ret; 
 
      case 't': 
        ADD_NODE_REF2(*arg1, 
                      ret = mkefuncallnode("basetype",*arg1); 
          ); 
        return ret; 
 
      case 'x': 
        switch( TYPEOF((*arg1)->u.sval) ) 
        { 
        case T_STRING: 
          ADD_NODE_REF2(*arg1, 
                        ret = mkefuncallnode("string2hex",*arg1); 
          ); 
          break; 
        case T_INT: 
          ADD_NODE_REF2(*arg1, 
                        ret = mkefuncallnode("int2hex",*arg1); 
          ); 
          break; 
        } 
        return ret; 
      case '%': 
        { 
          /* FIXME: This code can be removed when the generic 
           *        argument check is in place. 
           */ 
          struct pike_string *percent_string; 
          /* yywarning("Ignoring second argument to sprintf."); */ 
          MAKE_CONST_STRING(percent_string, "%"); 
          ADD_NODE_REF2(*arg1, 
                        ret = mknode(F_COMMA_EXPR, *arg1, 
                                     mkstrnode(percent_string))); 
          return ret; 
        } 
 
      default: break; 
      } 
    } 
  } 
  /* FIXME: Convert into compile_sprintf(args[0])->format(@args[1..])? */ 
  return ret; 
} 
 
/*! @decl type(mixed) __handle_sprintf_format(string attr, string fmt, @ 
 *!                                           type arg_type, type cont_type, @ 
 *!                                           mapping state) 
 *! 
 *!   Type attribute handler for @expr{"sprintf_format"@}. 
 *! 
 *! @param attr 
 *!   Attribute to handle, either @expr{"sprintf_format"@} 
 *!   or @expr{"strict_sprintf_format"@}. 
 *! 
 *! @param fmt 
 *!   Sprintf-style formatting string to generate type information from. 
 *! 
 *! @param arg_type 
 *!   Declared type of the @[fmt] argument (typically @expr{string@}). 
 *! 
 *! @param cont_type 
 *!   Continuation function type after the @[fmt] argument. This is 
 *!   scanned for the type attribute @expr{"sprintf_args"@} to 
 *!   determine where the remaining arguments to @[sprintf()] will 
 *!   come from. 
 *! 
 *! @param state 
 *!   State mapping. 
 *! 
 *! This function is typically called from 
 *! @[PikeCompiler()->apply_attribute_constant()] and is used to perform 
 *! stricter compile-time argument checking of @[sprintf()]-style functions. 
 *! 
 *! It currently implements two operating modes depending on the value of 
 *! @[attr]: 
 *! @string 
 *!   @value "strict_sprintf_format" 
 *!     The formatting string @[fmt] is known to always be passed to 
 *!     @[sprintf()]. 
 *!   @value "sprintf_format" 
 *!     The formatting string @[fmt] is passed to @[sprintf()] only 
 *!     if there are @expr{"sprintf_args"@}. 
 *! @endstring 
 *! 
 *! @returns 
 *!   Returns @[cont_type] with @expr{"sprintf_args"@} replaced by the 
 *!   arguments required by the @[fmt] formatting string, and 
 *!   @expr{"sprintf_result"@} replaced by the resulting string type. 
 *! 
 *! @seealso 
 *!   @[PikeCompiler()->apply_attribute_constant()], @[sprintf()] 
 */ 
void f___handle_sprintf_format(INT32 args) 
{ 
  struct pike_type *res; 
  struct pike_type *tmp; 
  struct pike_string *attr; 
  struct pike_string *fmt; 
  int severity = REPORT_ERROR; 
  int found = 0; 
  int fmt_count; 
  int marker; 
  int str_marker = 0; 
  int marker_mask; 
 
  if (args != 5) 
    SIMPLE_WRONG_NUM_ARGS_ERROR("__handle_sprintf_format", 5); 
  if (TYPEOF(Pike_sp[-5]) != PIKE_T_STRING) 
    SIMPLE_ARG_TYPE_ERROR("__handle_sprintf_format", 1, "string"); 
  if (TYPEOF(Pike_sp[-4]) != PIKE_T_STRING) 
    SIMPLE_ARG_TYPE_ERROR("__handle_sprintf_format", 2, "string"); 
  if (TYPEOF(Pike_sp[-3]) != PIKE_T_TYPE) 
    SIMPLE_ARG_TYPE_ERROR("__handle_sprintf_format", 3, "type"); 
  if (TYPEOF(Pike_sp[-2]) != PIKE_T_TYPE) 
    SIMPLE_ARG_TYPE_ERROR("__handle_sprintf_format", 4, "type"); 
  if (TYPEOF(Pike_sp[-1]) != PIKE_T_MAPPING) 
    SIMPLE_ARG_TYPE_ERROR("__handle_sprintf_format", 5, "mapping"); 
 
  tmp = Pike_sp[-2].u.type; 
  if ((tmp->type != PIKE_T_FUNCTION) && (tmp->type != T_MANY)) { 
    SIMPLE_ARG_TYPE_ERROR("__handle_sprintf_format", 4, "type(function)"); 
  } 
 
#if 0 
  fprintf(stderr, "__handle_sprintf_format(\"%s\", \"%s\", ...)\n", 
          Pike_sp[-5].u.string->str, Pike_sp[-4].u.string->str); 
#endif /* 0 */ 
 
  MAKE_CONST_STRING(attr, "sprintf_format"); 
  if (Pike_sp[-5].u.string == attr) { 
    /* Don't complain so loud about syntax errors in 
     * relaxed mode. 
     */ 
    severity = REPORT_NOTICE; 
  } else { 
    MAKE_CONST_STRING(attr, "strict_sprintf_format"); 
    if (Pike_sp[-5].u.string != attr) { 
      Pike_error("Bad argument 1 to __handle_sprintf_format(), expected " 
                 "\"sprintf_format\" or \"strict_sprintf_format\", " 
                 "got \"%S\".\n", Pike_sp[-5].u.string); 
    } 
  } 
 
  /* Allocate a marker for accumulating the result type. */ 
  marker = '0'; 
  marker_mask = PT_FLAG_MARKER_0 | PT_FLAG_ASSIGN_0; 
  while (tmp->flags & marker_mask) { 
    marker++; 
    marker_mask <<= 1; 
  } 
  if (marker > '9') marker = 0; 
  else { 
    str_marker = marker + 1; 
    marker_mask <<= 1; 
    while (tmp->flags & marker_mask) { 
      str_marker++; 
      marker_mask <<= 1; 
    } 
    if (str_marker > '9') str_marker = 0; 
  } 
 
  fmt = Pike_sp[-4].u.string; 
  MAKE_CONST_STRING(attr, "sprintf_args"); 
 
  type_stack_mark(); 
  type_stack_mark(); 
  for (; tmp; tmp = tmp->cdr) { 
    struct pike_type *arg = tmp->car; 
    int array_cnt = 0; 
    while(arg) { 
      switch(arg->type) { 
      case PIKE_T_ATTRIBUTE: 
        if (arg->car == (struct pike_type *)attr) 
          break; 
        /* FALLTHRU */ 
      case PIKE_T_NAME: 
        arg = arg->cdr; 
        continue; 
      case PIKE_T_ARRAY: 
        array_cnt++; 
        arg = arg->car; 
        continue; 
      default: 
        arg = NULL; 
        break; 
      } 
      break; 
    } 
    if (arg) { 
      p_wchar2 min_char = 0x7fffffff; 
      p_wchar2 max_char = -0x80000000; 
      int ret; 
 
      type_stack_mark(); 
      ret = push_sprintf_argument_types(MKPCHARP(fmt->str, fmt->size_shift), 
                                        fmt->len, 0, marker, str_marker, 
                                        &min_char, &max_char, severity); 
      switch(ret) { 
      case PSAT_SMARKER: 
      case PSAT_MARKER|PSAT_SMARKER: 
        if (!str_marker) { 
          min_char = -0x80000000; 
          max_char = 0x7fffffff; 
          ret &= ~PSAT_SMARKER; 
        } 
        /* FALLTHRU */ 
      case PSAT_MARKER: 
        if (!marker && (ret & PSAT_MARKER)) { 
          min_char = -0x80000000; 
          max_char = 0x7fffffff; 
          ret &= ~PSAT_MARKER; 
        } 
        /* FALLTHRU */ 
      case 0: 
        /* Ok. */ 
        if (!array_cnt) { 
          struct pike_type *trailer; 
          MAKE_CONST_STRING(attr, "sprintf_result"); 
          pop_stack_mark(); 
          push_type(T_VOID);    /* No more args */ 
          while (tmp->type == PIKE_T_FUNCTION) { 
            tmp = tmp->cdr; 
          } 
          trailer = tmp->cdr;   /* Return type */ 
          while (trailer->type == PIKE_T_NAME) { 
            trailer = trailer->cdr; 
          } 
          if ((trailer->type == PIKE_T_ATTRIBUTE) && 
              (trailer->car == (struct pike_type *)attr)) { 
            /* Push the derived return type. */ 
            if (min_char <= max_char) { 
              push_int_type(min_char, max_char); 
              if (ret & PSAT_MARKER) { 
                push_type(marker); 
                push_finished_type(int_type_string); 
                push_type(T_AND); 
                push_type(T_OR); 
              } 
            } else if (ret & PSAT_MARKER) { 
              push_type(marker); 
            } else { 
              push_type(PIKE_T_UNKNOWN); 
            } 
            push_unlimited_array_type(PIKE_T_STRING); 
            if (ret & PSAT_SMARKER) { 
              push_finished_type(int_type_string);      /* Len */ 
              push_type(str_marker); 
              push_finished_type(string_type_string); 
              push_type(T_AND); 
              push_type(str_marker); 
              push_finished_type(object_type_string); 
              push_type(T_AND); 
              push_type_operator(PIKE_T_FIND_LFUN, 
                                 (struct pike_type *)(ptrdiff_t)LFUN__SPRINTF); 
              /* FIXME: Need splice apply with mixed. */ 
              push_type_operator(PIKE_T_GET_RETURN, NULL); 
              push_type(T_OR); 
              push_type(PIKE_T_SET_CAR); 
              push_type(T_OR); 
            } 
          } else { 
            push_finished_type(tmp->cdr);       /* Return type */ 
          } 
          push_reverse_type(T_MANY); 
          fmt_count = pop_stack_mark(); 
          while (fmt_count > 1) { 
            push_reverse_type(T_FUNCTION); 
            fmt_count--; 
          } 
          if (severity < REPORT_ERROR) { 
            /* Add the type where the fmt isn't sent to sprintf(). */ 
            type_stack_mark(); 
            for (arg = Pike_sp[-2].u.type; arg != tmp; arg = arg->cdr) { 
              push_finished_type(arg->car); 
            } 
            push_type(T_VOID);                  /* No more args */ 
            push_finished_type(tmp->cdr);       /* Return type */ 
            push_reverse_type(T_MANY); 
            fmt_count = pop_stack_mark(); 
            while (fmt_count > 1) { 
              push_reverse_type(T_FUNCTION); 
              fmt_count--; 
            } 
            push_type(T_OR); 
          } 
          res = pop_unfinished_type(); 
          pop_n_elems(args); 
          push_type_value(res); 
          return; 
        } else { 
          /* Join the argument types into the array. */ 
          push_type(PIKE_T_ZERO); 
          for (fmt_count = pop_stack_mark(); fmt_count > 1; fmt_count--) { 
            push_type(T_OR); 
          } 
          while (array_cnt--) { 
            push_unlimited_array_type(PIKE_T_ARRAY); 
          } 
          if (severity < REPORT_ERROR) { 
            push_type(T_VOID); 
            push_type(T_OR); 
          } 
          found = 1; 
        } 
        break; 
      case -1: 
        /* Syntax error. */ 
        if (severity < REPORT_ERROR) { 
          /* Add the type where the fmt isn't sent to sprintf(). */ 
          type_stack_pop_to_mark(); 
          if (array_cnt--) { 
            push_type(PIKE_T_ZERO);     /* No args */ 
            while (array_cnt--) { 
              push_unlimited_array_type(PIKE_T_ARRAY); 
              push_type(PIKE_T_ZERO); 
              push_type(T_OR); 
            } 
            push_type(T_VOID); 
            push_type(T_OR); 
            push_finished_type(tmp->cdr);       /* Rest type */ 
            push_reverse_type(tmp->type); 
          } else { 
            push_type(T_VOID);  /* No more args */ 
            while (tmp->type == PIKE_T_FUNCTION) { 
              tmp = tmp->cdr; 
            } 
            push_finished_type(tmp->cdr);       /* Return type */ 
            push_reverse_type(T_MANY); 
          } 
          fmt_count = pop_stack_mark(); 
          while (fmt_count > 1) { 
            push_reverse_type(T_FUNCTION); 
            fmt_count--; 
          } 
          res = pop_unfinished_type(); 
          pop_n_elems(args); 
          push_type_value(res); 
          return; 
        } 
        /* FALLTHRU */ 
      default: 
      case PSAT_INVALID: 
      case PSAT_INVALID|PSAT_MARKER: 
        /* There was a position argument or a parse error in strict mode. */ 
        pop_stack_mark(); 
        pop_stack_mark(); 
        type_stack_pop_to_mark(); 
        pop_n_elems(args); 
        if (ret & (PSAT_MARKER|PSAT_SMARKER)) { 
          /* Error or marker that we can't trust. */ 
          push_undefined(); 
        } else { 
          /* Position argument, but we don't need to look at the marker. */ 
          type_stack_mark(); 
          if (min_char <= max_char) { 
            push_int_type(min_char, max_char); 
          } else { 
            push_type(T_ZERO); 
          } 
          push_unlimited_array_type(T_STRING); 
          push_type(T_MIXED); 
          push_type(T_MANY); 
          push_type_value(pop_unfinished_type()); 
        } 
        return; 
      } 
    } else { 
      push_finished_type(tmp->car); 
    } 
    if (tmp->type == T_MANY) { 
      tmp = tmp->cdr; 
      break; 
    } 
  } 
  if (found) { 
    /* Found, but inside an array, so we need to build the function 
     * type here. 
     */ 
    push_finished_type(tmp);    /* Return type. */ 
    push_reverse_type(T_MANY); 
    fmt_count = pop_stack_mark(); 
    while (fmt_count > 1) { 
      push_reverse_type(T_FUNCTION); 
      fmt_count--; 
    } 
    res = pop_unfinished_type(); 
    pop_n_elems(args); 
    push_type_value(res); 
  } else { 
    /* No marker found. */ 
#if 0 
    simple_describe_type(Pike_sp[-2].u.type); 
    fprintf(stderr, " ==> No marker found.\n"); 
#endif /* 0 */ 
    pop_stack_mark(); 
    type_stack_pop_to_mark(); 
    pop_n_elems(args); 
    push_undefined(); 
  } 
} 
 
/*! @decl constant sprintf_format = __attribute__("sprintf_format") 
 *! 
 *!   Type constant used for typing arguments that are optionally 
 *!   sent to @[sprintf()] depending on the presence of extra arguments. 
 *! 
 *! @seealso 
 *!   @[strict_sprintf_format], @[sprintf_args], @[sprintf()] 
 */ 
 
/*! @decl constant strict_sprintf_format = @ 
 *!         __attribute__("strict_sprintf_format") 
 *! 
 *!   Type constant used for typing arguments that are always 
 *!   sent to @[sprintf()] regardless of the presence of extra arguments. 
 *! 
 *! @seealso 
 *!   @[sprintf_format], @[sprintf_args], @[sprintf()] 
 */ 
 
/*! @decl constant sprintf_args = __attribute__("sprintf_args") 
 *! 
 *!   Type constant used for typing extra arguments that are 
 *!   sent to @[sprintf()]. 
 *! 
 *! @seealso 
 *!   @[strict_sprintf_format], @[sprintf_format], @[sprintf()] 
 */ 
 
/*! @decl constant sprintf_result = __attribute__("sprintf_result") 
 *! 
 *!   Type constant used for typing the return value from @[sprintf()]. 
 *! 
 *! @seealso 
 *!   @[strict_sprintf_format], @[sprintf_format], @[sprintf()] 
 */ 
 
/*! @module String 
 */ 
 
/*! @decl constant __HAVE_SPRINTF_STAR_MAPPING__ = 1 
 *! 
 *!   Presence of this symbol indicates that @[sprintf()] supports 
 *!   mappings for the @tt{'*'@}-modifier syntax. 
 *! 
 *! @seealso 
 *!   @[sprintf()], @[lfun::_sprintf()] 
 */ 
 
/*! @decl constant __HAVE_SPRINTF_NEGATIVE_F__ = 1 
 *! 
 *!   Presence of this symbol indicates that @[sprintf()] supports 
 *!   little endian output for the @tt{'F'@}-format specifier. 
 *! 
 *! @seealso 
 *!   @[sprintf()], @[lfun::_sprintf()] 
 */ 
 
/*! @endmodule 
 */ 
 
void init_sprintf(void) 
{ 
  struct pike_string *attr; 
  struct svalue s; 
 
  ADD_EFUN("__handle_sprintf_format", f___handle_sprintf_format, 
           tFunc(tStr tStr tType(tMix) tType(tMix), tType(tMix)), 
           0); 
 
  MAKE_CONST_STRING(attr, "sprintf_format"); 
  SET_SVAL(s, T_TYPE, 0, type, 
           make_pike_type(tAttr("sprintf_format", tOr(tStr, tObj)))); 
  low_add_efun(attr, &s); 
  free_type(s.u.type); 
 
  MAKE_CONST_STRING(attr, "strict_sprintf_format"); 
  SET_SVAL(s, T_TYPE, 0, type, 
           make_pike_type(tAttr("strict_sprintf_format", tOr(tStr, tObj)))); 
  low_add_efun(attr, &s); 
  free_type(s.u.type); 
 
  MAKE_CONST_STRING(attr, "sprintf_args"); 
  s.u.type = make_pike_type(tAttr("sprintf_args", tMix)); 
  low_add_efun(attr, &s); 
  free_type(s.u.type); 
 
  MAKE_CONST_STRING(attr, "sprintf_result"); 
  s.u.type = make_pike_type(tAttr("sprintf_result", tStr)); 
  low_add_efun(attr, &s); 
  free_type(s.u.type); 
 
  /* function(string|object, mixed ... : string) */ 
  ADD_EFUN2("sprintf", 
            f_sprintf, 
            tFuncV(tAttr("strict_sprintf_format", tOr(tStr, tObj)), 
                   tAttr("sprintf_args", tMix), tAttr("sprintf_result", tStr)), 
            OPT_TRY_OPTIMIZE, 
            optimize_sprintf, 
            0); 
} 
 
void exit_sprintf(void) 
{ 
}