summaryrefslogtreecommitdiffstats
path: root/firmware/common/vuprintf.c
blob: fb053ae7da11d4a7a16a2b8108be457fe448940c (plain)
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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2002 by Gary Czvitkovicz
 * Copyright (C) 2017 by Michael A. Sevakis
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include <sys/types.h>
#include <limits.h>
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include "system.h"
#include "vuprintf.h"
#include "ap_int.h"

#ifndef BOOTLOADER

/* Only the Quake plugin needs float formatting */
#if defined(HAVE_LCD_COLOR)  && \
    (!defined(LCD_STRIDEFORMAT) || (LCD_STRIDEFORMAT != VERTICAL_STRIDE)) && \
    (PLUGIN_BUFFER_SIZE > 0x14000) && (CONFIG_PLATFORM & PLATFORM_NATIVE) && defined(CPU_ARM)
/* turn everything on */
#define FMT_LENMOD      (0xffffffff)
#define FMT_RADIX       (0xffffffff)
#endif

#endif

/* these are the defaults if no other preference is given */
#ifndef FMT_LENMOD
#define FMT_LENMOD      (FMT_LENMOD_l  | \
                         FMT_LENMOD_z)
#endif /* FMT_LENMOD */

#ifndef FMT_RADIX
#define FMT_RADIX       (FMT_RADIX_c | \
                         FMT_RADIX_d | \
                         FMT_RADIX_p | \
                         FMT_RADIX_s | \
                         FMT_RADIX_u | \
                         FMT_RADIX_x)
#endif /* FMT_RADIX */

/** Length modifier and radix flags **/

/* compulsory length modifiers: NONE
 * however a compatible 'l' or 'll' must be defined if another requires it */
#define FMT_LENMOD_h    0x001  /* signed/unsigned short (%h<radix>) */
#define FMT_LENMOD_hh   0x002  /* signed/unsigned char (%hh<radix>) */
#define FMT_LENMOD_j    0x004  /* intmax_t/uintmax_t (%j<radix>) */
#define FMT_LENMOD_l    0x008  /* signed/unsigned long (%l<radix>) */
#define FMT_LENMOD_ll   0x010  /* signed/unsigned long long (%ll<radix>) */
#define FMT_LENMOD_t    0x020  /* signed/unsigned ptrdiff_t (%t<radix>) */
#define FMT_LENMOD_z    0x040  /* size_t/ssize_t (%z<radix>) */
#if 0
#define FMT_LENMOD_L    0x080  /* long double (instead of double) */
#else
#define FMT_LENMOD_L    0x000
#endif

/* compulsory radixes: c, d, i, u, s */
#define FMT_RADIX_c     0x001  /* single character (%c) */
#define FMT_RADIX_d     0x002  /* signed integer type, decimal (%d %i) */
#define FMT_RADIX_n     0x004  /* bytes output so far (%n) */
#define FMT_RADIX_o     0x008  /* unsigned integer type, octal (%o) */
#define FMT_RADIX_p     0x010  /* pointer (%p %P) */
#define FMT_RADIX_s     0x020  /* string (%s) */
#define FMT_RADIX_u     0x040  /* unsigned integer type, decimal (%u) */
#define FMT_RADIX_x     0x080  /* unsigned integer type, hex (%x %X) */
#define FMT_RADIX_a     0x100  /* hex floating point "[-]0xh.hhhhp±d" */
#define FMT_RADIX_e     0x200  /* floating point with exponent "[-]d.ddde±dd" */
#define FMT_RADIX_f     0x400  /* floating point "[-]ddd.ddd" */
#define FMT_RADIX_g     0x800  /* floating point exponent or decimal depending
                                  upon value and precision */

/* TODO: 'a' 'A' */
#define FMT_RADIX_floats (FMT_RADIX_e|FMT_RADIX_f|FMT_RADIX_g)

#if (FMT_RADIX & FMT_RADIX_floats)
/* Assumes IEEE 754 double-precision, native-endian; replace to parse and init
   for some other format */
#define parse_double            parse_ieee754_double
#define init_double_chunks      init_ieee754_double_chunks
#define format_double_int10     format_ap_int10
#define format_double_frac10    format_ap_frac10
#endif

/* avoid defining redundant functions if two or more types can use the same
 * something not getting a macro means it gets assigned its own value and
 * formatter */

/* l */
#if LONG_MIN == INT_MIN && LONG_MAX == INT_MAX
#define val_ld          val_d
#define format_ld       format_d
#define branch_fmt_ld   branch_fmt_d
#elif !(FMT_LENMOD & FMT_LENMOD_l) /* unique */
#define val_ld
#endif /* LONG_ */

#if ULONG_MAX == UINT_MAX
#define val_lu          val_u
#define format_lu       format_u
#define branch_fmt_lu   branch_fmt_u
#elif !(FMT_LENMOD & FMT_LENMOD_l) /* unique */
#define val_lu
#endif /* ULONG_ */

/* ll */
#if LLONG_MIN == INT_MIN && LLONG_MAX == INT_MAX
#define val_lld         val_d
#define format_lld      format_d
#define branch_fmt_lld  branch_fmt_d
#elif LLONG_MIN == LONG_MIN && LLONG_MAX == LONG_MAX
#define val_lld         val_ld
#define format_lld      format_ld
#define branch_fmt_lld  branch_fmt_ld
#elif !(FMT_LENMOD & FMT_LENMOD_ll) /* unique */
#define val_lld
#endif /* LLONG_ */

#if ULLONG_MAX == UINT_MAX
#define val_llu         val_u
#define format_llu      format_u
#define branch_fmt_llu  branch_fmt_u
#elif ULLONG_MAX == ULONG_MAX
#define val_llu         val_lu
#define format_llu      format_lu
#define branch_fmt_llu  branch_fmt_lu
#elif !(FMT_LENMOD & FMT_LENMOD_ll) /* unique */
#define val_llu
#endif /* ULLONG_ */

/* char/short parameter type promotions */
#define SCHAR_INT_ARG   int
#define UCHAR_INT_ARG   int
#define SSHRT_INT_ARG   int
#if USHRT_MAX == UINT_MAX
#define USHRT_INT_ARG   unsigned int
#else
#define USHRT_INT_ARG   int
#endif

/* some macros to have conditional work inside macros */
#if (FMT_LENMOD & FMT_LENMOD_l)
#define IF_FMT_LENMOD_l(...)    __VA_ARGS__
#else
#define IF_FMT_LENMOD_l(...)
#endif

#if (FMT_LENMOD & FMT_LENMOD_ll)
#define IF_FMT_LENMOD_ll(...)   __VA_ARGS__
#else
#define IF_FMT_LENMOD_ll(...)
#endif

#if (FMT_RADIX & FMT_RADIX_o)
#define IF_FMT_RADIX_o(...)     __VA_ARGS__
#else
#define IF_FMT_RADIX_o(...)
#endif

#if (FMT_RADIX & FMT_RADIX_x)
#define IF_FMT_RADIX_x(...)     __VA_ARGS__
#else
#define IF_FMT_RADIX_x(...)
#endif

/* synthesize multicharacter constant */
#define LENMOD2(cv, ch) \
    (((cv) << CHAR_BIT) | (ch))

#define LENMOD_NONE 0

#if (FMT_LENMOD & FMT_LENMOD_h)
#define LENMOD_h    'h'
#endif
#if (FMT_LENMOD & FMT_LENMOD_hh)
#define LENMOD_hh   LENMOD2('h', 'h') /* 'hh' */
#endif
#if (FMT_LENMOD & FMT_LENMOD_j)
#define LENMOD_j    'j'
#endif
#if (FMT_LENMOD & FMT_LENMOD_l)
#define LENMOD_l    'l'
#endif
#if (FMT_LENMOD & FMT_LENMOD_ll)
#undef FMT_MAX_L
#define LENMOD_ll   LENMOD2('l', 'l') /* 'll' */
#endif
#if (FMT_LENMOD & FMT_LENMOD_t)
#define LENMOD_t    't'
#endif
#if (FMT_LENMOD & FMT_LENMOD_z)
#define LENMOD_z    'z'
#endif

/* select type-compatible length modifier
 * (a bit hacky; it should be range-based) */
#define LENMOD_INTCOMPAT_SEL(type, signd) \
    ({  int __lenmod;                                                 \
        size_t __size = sizeof (type);                                \
        if (__size == ((signd) ? sizeof (int) :                       \
                                 sizeof (unsigned int))) {            \
            __lenmod = LENMOD_NONE;                                   \
        }                                                             \
        else if (__size == ((signd) ? sizeof (long) :                 \
                                      sizeof (unsigned long))) {      \
            IF_FMT_LENMOD_l(__lenmod = LENMOD_l;)                     \
        }                                                             \
        else if (__size == ((signd) ? sizeof (long long) :            \
                                      sizeof (unsigned long long))) { \
            IF_FMT_LENMOD_ll(__lenmod = LENMOD_ll;)                   \
        }                                                             \
        __lenmod; })

/* call formatting function for the compatible integer type */
#define LENMOD_INTCOMPAT_CALL(inteqv, val, fmt_buf, x, signd) \
    ({  const char *__buf;                          \
        switch (inteqv) {                           \
        case LENMOD_NONE:                           \
            __buf = (signd) ?                       \
                format_d((val), (fmt_buf), (x)) :   \
                format_u((val), (fmt_buf), (x));    \
            break;                                  \
        IF_FMT_LENMOD_l(                            \
        case LENMOD_l:                              \
            __buf = (signd) ?                       \
                format_ld((val), (fmt_buf), (x)) :  \
                format_lu((val), (fmt_buf), (x));   \
            break;                                  \
        )                                           \
        IF_FMT_LENMOD_ll(                           \
        case LENMOD_ll:                             \
            __buf = (signd) ?                       \
                format_lld((val), (fmt_buf), (x)) : \
                format_llu((val), (fmt_buf), (x));  \
            break;                                  \
        )                                           \
        }                                           \
        __buf;                                      \
    })

/* execute formatting branch for the compatible integer type */
#define LENMOD_INTCOMPAT_BRANCH(inteqv, val, signd)  \
    ({  switch (inteqv) {                       \
        case LENMOD_NONE:                       \
            if (signd) {                        \
                val_d = (val);                  \
                goto branch_fmt_d;              \
            }                                   \
            else {                              \
                val_u = (val);                  \
                goto branch_fmt_u;              \
            }                                   \
        IF_FMT_LENMOD_l(                        \
        case LENMOD_l:                          \
            if (signd) {                        \
                val_ld = (val);                 \
                goto branch_fmt_ld;             \
            }                                   \
            else {                              \
                val_lu = (val);                 \
                goto branch_fmt_lu;             \
            }                                   \
        )                                       \
        IF_FMT_LENMOD_ll(                       \
        case LENMOD_ll:                         \
            if (signd) {                        \
                val_lld = (val);                \
                goto branch_fmt_lld;            \
            }                                   \
            else {                              \
                val_llu = (val);                \
                goto branch_fmt_llu;            \
            }                                   \
        )                                       \
        }                                       \
    })

#define CONVERT_RADIX_10_SIGN(val, fmt_buf, p, signchar, type) \
    do {                                        \
        if (val) {                              \
            unsigned type v;                    \
                                                \
            if (val < 0) {                      \
                v = (typeof (v))-(val + 1) + 1; \
                signchar = '-';                 \
            }                                   \
            else {                              \
                v = val;                        \
            }                                   \
                                                \
            do {                                \
                *--p = (v % 10) + '0';          \
                v /= 10;                        \
            } while (v);                        \
        }                                       \
                                                \
        if (signchar) {                         \
            p[-1] = signchar;                   \
            fmt_buf->length = 1;                \
            break;                              \
        }                                       \
                                                \
        fmt_buf->length = 0;                    \
    } while (0)

#define CONVERT_RADIX_8(val, fmt_buf, p) \
    do {                                        \
        if (val) {                              \
            typeof (val) v = val;               \
                                                \
            do {                                \
                *--p = (v % 010) + '0';         \
                v /= 010;                       \
            } while (v);                        \
        }                                       \
                                                \
        if (fmt_buf->length) {                  \
            *--p = '0';                         \
            fmt_buf->length = 0;                \
        }                                       \
    } while (0)

#define CONVERT_RADIX_10(val, fmt_buf, p) \
    do {                                        \
        if (val) {                              \
            typeof (val) v = val;               \
                                                \
            do {                                \
                *--p = (v % 10) + '0';          \
                v /= 10;                        \
            } while (v);                        \
        }                                       \
                                                \
        fmt_buf->length = 0;                    \
    } while (0)

#define CONVERT_RADIX_16(val, fmt_buf, p, x) \
    do {                                        \
        if (val) {                              \
            const int h = x - 'X' - 0xA         \
                            + 'A' - '0';        \
            typeof (val) v = val;               \
                                                \
            do {                                \
                unsigned int d = v % 0x10;      \
                if (d >= 0xA) {                 \
                    d += h;                     \
                }                               \
                *--p = d + '0';                 \
                v /= 0x10;                      \
            } while (v);                        \
                                                \
            if (fmt_buf->length) {              \
                p[-1] = x;                      \
                p[-2] = '0';                    \
                fmt_buf->length = 2;            \
                break;                          \
            }                                   \
        }                                       \
                                                \
        fmt_buf->length = 0;                    \
    } while (0)

#define CONVERT_RADIX_NOSIGN(val, fmt_buf, p, x) \
    switch (x)                                  \
    {                                           \
    IF_FMT_RADIX_o( case 'o':                   \
        CONVERT_RADIX_8(val, fmt_buf, p);       \
        break; )                                \
    case 'u':                                   \
        CONVERT_RADIX_10(val, fmt_buf, p);      \
        break;                                  \
    IF_FMT_RADIX_x( default:                    \
        CONVERT_RADIX_16(val, fmt_buf, p, x);   \
        break; )                                \
    }

struct fmt_buf {
    const char *fmt_start; /* second character of formatter after '%' */
    size_t length;         /* length of formatted text (non-numeric)
                              or prefix (numeric) */
    char buf[24];          /* work buffer */
    char bufend[1];        /* buffer end marker and guard '0' */
#if (FMT_RADIX & FMT_RADIX_floats)
    int lenmod;
    int radixchar;
    int signchar;
    int alignchar;
    int width;
    int precision;
    char *p;
#endif
};

#define PUSHCHAR(ch) \
    ({  int __rc = push(userp, (ch)); \
        count += __rc >= 0;           \
        if (__rc <= 0) {              \
            goto done;                \
        } })

/* %d %i */
static inline const char * format_d(int val,
                                    struct fmt_buf *fmt_buf,
                                    int signchar)
{
    char *p = fmt_buf->bufend;
    CONVERT_RADIX_10_SIGN(val, fmt_buf, p, signchar, int);
    return p;
}

/* %o %u %x %X */
static inline const char * format_u(unsigned int val,
                                    struct fmt_buf *fmt_buf,
                                    int radixchar)
{
    char *p = fmt_buf->bufend;
    CONVERT_RADIX_NOSIGN(val, fmt_buf, p, radixchar);
    return p;
}

#if (FMT_LENMOD & FMT_LENMOD_l)
#ifndef format_ld
/* %ld %li */
static inline const char * format_ld(long val,
                                     struct fmt_buf *fmt_buf,
                                     int signchar)
{
    char *p = fmt_buf->bufend;
    CONVERT_RADIX_10_SIGN(val, fmt_buf, p, signchar, long);
    return p;
}
#endif /* format_ld */

#ifndef format_lu
/* %lo %lu %lx %lX */
static inline const char * format_lu(unsigned long val,
                                     struct fmt_buf *fmt_buf,
                                     int radixchar)
{
    char *p = fmt_buf->bufend;
    CONVERT_RADIX_NOSIGN(val, fmt_buf, p, radixchar);
    return p;
}
#endif /* format_lu */
#endif /* FMT_LENMOD_l */

#if (FMT_LENMOD & FMT_LENMOD_ll)
#ifndef format_lld
/* %lld %lli */
static inline const char * format_lld(long long val,
                                      struct fmt_buf *fmt_buf,
                                      int signchar)
{
    char *p = fmt_buf->bufend;
    CONVERT_RADIX_10_SIGN(val, fmt_buf, p, signchar, long long);
    return p;
}
#endif /* format_lld */

#ifndef format_llu
/* %llo %llu %llx %llX */
static inline const char * format_llu(unsigned long long val,
                                      struct fmt_buf *fmt_buf,
                                      int radixchar)
{
    char *p = fmt_buf->bufend;
    CONVERT_RADIX_NOSIGN(val, fmt_buf, p, radixchar);
    return p;
}
#endif /* format_llu */
#endif /* FMT_LENMOD_ll */

/* %c */
static inline const char * format_c(int c,
                                    struct fmt_buf *fmt_buf,
                                    int lenmod)
{
    if (lenmod != LENMOD_NONE) {
        return NULL; /* wchar_t support for now */
    }

    char *p = fmt_buf->bufend;
    fmt_buf->length = 1;
    *--p = (unsigned char)c;
    return p;
}

/* %s */
static inline const char * format_s(const void *str,
                                    struct fmt_buf *fmt_buf,
                                    int precision,
                                    int lenmod)
{
    if (lenmod != LENMOD_NONE) {
        return NULL; /* wchar_t support for now */
    }

    const char *s = str;
    size_t len;
    /* string length may be specified by precision instead of \0-
       terminated; however, don't go past a \0 if one is there */
    if (precision >= 0) {
        const char *nil = memchr(s, '\0', (size_t) precision);

        if (nil != NULL && (nil - s) < precision)
            len = nil - s;
        else
            len = precision;
    } 
    else
        len = strlen(s);

    fmt_buf->length = len;
    return s;
}

#if (FMT_RADIX & FMT_RADIX_n)
/* %n */
static inline bool format_n(void *np,
                            int count,
                            int lenmod)
{
    if (lenmod != LENMOD_NONE) {
        return false; /* int only for now */
    }

    *(int *)np = count;
    return true;
}
#endif /* FMT_RADIX_n */

#if (FMT_RADIX & FMT_RADIX_p)
/* %p %P */
static inline const char * format_p(const void *p,
                                    struct fmt_buf *fmt_buf,
                                    int radixchar,
                                    bool *numericp)
{
    if (p) {
        /* format as %#x or %#X */
        *numericp = true;
        radixchar -= 'P' - 'X';
        fmt_buf->length = 2;
        return LENMOD_INTCOMPAT_CALL(LENMOD_INTCOMPAT_SEL(uintptr_t, false),
                                     (uintptr_t)p, fmt_buf, radixchar, false);
    }
    else {
        /* format as %s */
        fmt_buf->length = 5;
        return "(nil)";
    }
}
#endif /* FMT_RADIX_p */

#if (FMT_RADIX & FMT_RADIX_floats)
/* find out how many uint32_t chunks need to be allocated, if any
 * if none are needed, finish the init for the number here */
static long parse_ieee754_double(double f,
                                 struct ap_int *ia,
                                 struct ap_int *fa,
                                 struct fmt_buf *fmt_buf)
{
    long rc = 0;

    union {
        double   f;
        uint64_t f64;
    } u = { .f = f };

    int e = ((int)(u.f64 >> 52) & 0x7ff) - 1023; /* -1023..1024 */
    uint64_t mantissa = u.f64 & 0x000fffffffffffffull;

    if (u.f64 >> 63) {
        fmt_buf->signchar = '-';
    }

    if (LIKELY(e >= -8 && e <= 63)) { /* -8 to +63 */
        /* integer, fraction and manipulations fit in uint64_t */
        mantissa |= 0x0010000000000000ull;
        ia->numchunks = 0;
        ia->shift     = 0;
        fa->numchunks = 0;

        if (e < 0) {            /* -8 to -1 - fraction */
            long fracbits = 52 - e;
            /* int - none */
            ia->len   = 0;
            ia->val   = 0;
            /* frac */
            fa->len   = fracbits - __builtin_ctzll(mantissa);
            fa->shift = fracbits;
            fa->val   = mantissa;
        }
        else if (e <= 51) {     /* 0 to +51 - integer|fraction */
            long fracbits = 52 - e;
            /* int */
            ia->len   = base10exp(e) + 2; /* go up + possibly 1 longer */
            ia->val   = mantissa >> fracbits;
            /* frac */
            fa->shift = fracbits;
            fa->val   = mantissa ^ (ia->val << fracbits);
            fa->len   = fa->val ? fracbits - __builtin_ctzll(mantissa) : 0;
        }
        else {                  /* +52 to +63 - integer */
            /* int */
            ia->len   = base10exp(e) + 2;
            ia->val   = mantissa << (e - 52);
            /* frac - none */
            fa->len   = 0;
            fa->shift = 0;
            fa->val   = 0;
        }
    }
    else if (e < 0) {         /* -1023 to -9 - fraction */
        /* int - none */
        ia->numchunks = 0;
        ia->len       = 0;
        ia->shift     = 0;
        ia->val       = 0;
        /* frac - left-justify on bit 31 of the chunk of the MSb */
        if (e >= -1022) {       /* normal */
            mantissa |= 0x0010000000000000ull;
        }
        else {                  /* subnormal (including zero) */
            e = -1022;
        }

        if (mantissa) {
            long fracbits = 52 - e;
            fa->len       = fracbits - __builtin_ctzll(mantissa);
            fa->shift     = 31 - ((51 - e) % 32);
            fa->val       = mantissa;
            fa->basechunk = (fa->shift + 52) / 32;
            fa->numchunks = (51 - e + fa->shift) / 32 + 1;
            rc = fa->numchunks;
        }
        else {                  /* zero */
            fa->numchunks = 0;
            fa->len       = 0;
            fa->shift     = 0;
            fa->val       = 0;
        }
    }
    else if (e <= 1023) {       /* +64 to +1023 - integer */
        /* int - right-justify on bit 0 of the first chunk */
        ia->val       = mantissa | 0x0010000000000000ull;
        ia->len       = base10exp(e) + 2;
        ia->shift     = (e - 52) % 32;
        ia->basechunk = e / 32;
        ia->numchunks = ia->basechunk + 1;
        rc = ia->numchunks;
        /* frac - none */
        fa->numchunks = 0;
        fa->len       = 0;
        fa->shift     = 0;
        fa->val       = 0;
    }
    else {                      /* +1024: INF, NAN */
        rc = -1 - !!mantissa;
    }

    return rc;
}

/* construct the arbitrary-precision value in the provided allocation */
static void init_ieee754_double_chunks(struct ap_int *a,
                                       uint32_t *a_chunks)
{
    long basechunk = a->basechunk;
    long shift = a->shift;
    uint64_t val = a->val;

    a->chunks = a_chunks;

    memset(a_chunks, 0, a->numchunks*sizeof (uint32_t));

    if (shift < 12) {
        a_chunks[basechunk - 1] = val << shift;
        a_chunks[basechunk - 0] = val >> (32 - shift);
    }
    else {
        a_chunks[basechunk - 2] = val << shift;
        a_chunks[basechunk - 1] = val >> (32 - shift);
        a_chunks[basechunk - 0] = val >> (64 - shift);
    }
}

/* format inf, nan strings */
static void format_inf_nan(struct fmt_buf *fmt_buf, long type)
{
    /* certain special values */
    static const char text[2][2][3] =
    {
        { { 'I', 'N', 'F' }, { 'i', 'n', 'f' } },
        { { 'N', 'A', 'N' }, { 'n', 'a', 'n' } },
    };

    char *p = fmt_buf->buf;
    fmt_buf->p = p;
    fmt_buf->length = 3;

    /* they also have a sign */
    if (fmt_buf->signchar) {
        *p++ = fmt_buf->signchar;
        fmt_buf->length++;
    }

    memcpy(p, &text[type][(fmt_buf->radixchar >> 5) & 0x1], 3);
}

/* %e %E %f %F %g %G */
static int format_double_radix(double f,
                               struct fmt_buf *fmt_buf,
                               vuprintf_push_cb push,
                               void *userp)
{
    struct ap_int ia, fa;
    long rc = parse_double(f, &ia, &fa, fmt_buf);

    if (UNLIKELY(rc < 0)) {
        format_inf_nan(fmt_buf, -rc - 1);
        return 0;
    }

    int count = 0;

    /* default precision is 6 for all formats */
    int prec_rem = fmt_buf->precision < 0 ? 6 : fmt_buf->precision;

    int exp = exp;
    int explen = 0;

    switch (fmt_buf->radixchar & 0x3)
    {
    case 3: /* %g, %G */
        fmt_buf->precision = prec_rem;
        if (prec_rem) {
            prec_rem--;
        }
    case 1: /* %e, %E */
        explen = 2;
        break;
    default:
        break;
    }

    if (rc > 0 && ia.numchunks > 0) {
        /* large integer required */
        init_double_chunks(&ia, alloca(rc*sizeof(*ia.chunks)));
        rc = 0;
    }

    const int bufoffs = 6; /* log rollover + round rollover + leading zeros (%g) */
    long f_prec = MIN(fa.len, prec_rem + 1);
    char buf[bufoffs + ia.len + f_prec + 1];
    char *p_last  = &buf[bufoffs + ia.len];
    char *p_dec   = p_last;
    char *p_first = format_double_int10(&ia, p_last);

    if (explen) {
        if (!ia.val && fa.len) {
            p_first = p_last = &buf[bufoffs];
            f_prec = -f_prec - 1; /* no lead zeros */
        }
        else { /* handles 0e+0 too */
            exp = ia.len - 1;

            if (exp) {
                prec_rem -= exp;

                if (prec_rem < 0) {
                    p_last += prec_rem + 1;
                    f_prec = 0;
                }
                else {
                    f_prec = MIN(fa.len, prec_rem + 1);
                }
            }
        }

        p_dec = p_first + 1;
    }

    if (f_prec) {
        if (rc > 0) {
            /* large integer required */
            init_double_chunks(&fa, alloca(rc*sizeof(*fa.chunks)));
        }

        p_last = format_double_frac10(&fa, p_last, f_prec);

        if (f_prec < 0) {
            f_prec = -f_prec - 1;
            exp = f_prec - fa.len;
        }

        prec_rem -= f_prec;
    }

    if (prec_rem < 0) {
        prec_rem = 0;
        p_last--;

        if (round_number_string10(p_last, p_last - p_first)) {
            /* carried left */
            p_first--;

            if (explen) {
                /* slide everything left by 1 */
                exp++;
                p_dec--;
                p_last--;
            }
        }
    }

    if (explen) {
        if ((fmt_buf->radixchar & 0x3) == 0x3) { /* g, G */
            /* 'g' is some weird crap */
            /* now that the final exponent is known and everything rounded,
               it is possible to decide whether to format similarly to
               'e' or 'f' */
            if (fmt_buf->precision > exp && exp >= -4) { /* P > X >= -4 */
                if (exp >= 0) {
                    /* integer digits will be in the buffer */
                    p_dec = p_first + exp + 1;
                }
                else {
                    /* we didn't keep leading zeros and need to regenerate
                       them; space was reserved just in case */
                    p_first = memset(p_dec + exp - 1, '0', -exp);
                    p_dec = p_first + 1;
                }

                /* suppress exponent */
                explen = 0;
            }

            if (!fmt_buf->length) {
                /* strip any trailing zeros from the fraction */
                while (p_last > p_dec && p_last[-1] == '0') {
                    p_last--;
                }

                /* suppress trailing precision fill */
                prec_rem = 0;
            }
        }

        if (explen) {
            /* build exponent string: 'e±dd' */
            char *p = fmt_buf->bufend;
            int signchar = '+';

            if (exp < 0) {
                signchar = '-';
                exp = -exp;
            }

            while (exp || explen < 4) {
                *--p = exp % 10 + '0';
                exp /= 10;
                explen++;
            }

            *--p = signchar;
            *--p = fmt_buf->radixchar & ~0x2;
        }
    }

    int width = fmt_buf->width;
    int point = p_last > p_dec || prec_rem || fmt_buf->length;
    int length = p_last - p_first + !!fmt_buf->signchar + point + explen;

    if (width) {
        if (width - length <= prec_rem) {
            width = 0;
        }
        else {
            width -= length + prec_rem;
        }
    }

    rc = -1;

    /* left padding */
    if (fmt_buf->alignchar > '0') {
        /* space-padded width -- before sign */
        while (width > 0) {
            PUSHCHAR(' ');
            width--;
        }
    }

    if (fmt_buf->signchar) {
        PUSHCHAR(fmt_buf->signchar);
    }

    if (fmt_buf->alignchar == '0') {
        /* zero-padded width -- after sign */
        while (width > 0) {
            PUSHCHAR('0');
            width--;
        }
    }

    /* integer part */
    while (p_first < p_dec) {
        PUSHCHAR(*p_first++);
    }

    /* decimal point */
    if (point) {
        PUSHCHAR('.');
    }

    /* fractional part */
    while (p_first < p_last) {
        PUSHCHAR(*p_first++);
    }

    /* precision 0-padding */
    while (prec_rem > 0) {
        PUSHCHAR('0');
        prec_rem--;
    }

    /* exponent */
    if (explen > 0) {
        char *p = fmt_buf->bufend;
        while (explen > 0) {
            PUSHCHAR(p[-explen--]);
        }
    }

    /* right padding */
    while (width > 0) {
        PUSHCHAR(' ');
        width--;
    }

    rc = 1;
done:
    fmt_buf->length = count;
    return rc;
}
#endif /* FMT_RADIX_floats */

/* parse fixed width or precision field */
static const char * parse_number_spec(const char *fmt,
                                      int ch,
                                      int *out)
{
    int i = ch - '0';

    while (1) {
        ch = *fmt - '0';

        if (ch < 0 || ch > 9 || i > INT_MAX / 10 ||
            (i == INT_MAX / 10 && ch > INT_MAX % 10)) {
            break;
        }

        i = i * 10 + ch;
        fmt++;
    }

    *out = i;
    return fmt;
}

int vuprintf(vuprintf_push_cb push, /* call 'push()' for each output letter */
             void *userp,
             const char *fmt,
             va_list ap)
{
    int count = 0;
    int ch;

    /* macrofied identifiers share a variable with another */
    unsigned int val_d;
    unsigned int val_u;
    #ifndef val_ld
    unsigned long val_ld;
    #endif
    #ifndef val_lu
    unsigned long val_lu;
    #endif
    #ifndef val_lld
    unsigned long long val_lld;
    #endif
    #ifndef val_llu
    unsigned long long val_llu;
    #endif

    struct fmt_buf fmt_buf;
    fmt_buf.bufend[0] = '0';

    while (1) {
        while (1) {
            if ((ch = *fmt++) == '\0') {
                goto done;
            }

            if (ch == '%' && (ch = *fmt++) != '%') {
                break;
            }

            PUSHCHAR(ch);
        }

        /* set to defaults */
        fmt_buf.fmt_start = fmt;

        int signchar = 0;
        unsigned int width = 0;
        int lenmod = LENMOD_NONE;
        size_t length = 0;
        size_t pfxlen = 0;
        bool numeric = false;
        int alignchar = '0' + 1;
        int precision = -1;
        const char *buf = NULL;

        /*** flags ***/
        while (1) {
            switch (ch)
            {
            case ' ':   /* <space> before non-negative value (signed conversion) */
            case '+':   /* '+' before non-negative value (signed conversion) */
                /* '+' overrides ' ' */
                if (ch > signchar) {
                    signchar = ch;
                }
                break;
            case '-':   /* left-justify in field */
            case '0':   /* zero-pad to fill field */
                /* '-' overrides '0' */
                if (ch < alignchar) {
                    alignchar = ch;
                }
                break;
            case '#':   /* number prefix (nonzero %o:'0' %x/%X:'0x') */
                /* indicate; formatter updates with actual length */
                pfxlen = 1;
                break;
            #if 0
            case '\'':  /* digit grouping (non-monetary) */
                break;
            #endif
            default:
                goto flags_done;
            }

            ch = *fmt++;
        }
   flags_done:

        /*** width ***/
        if (ch == '*') {
            /* variable width */
            int w = va_arg(ap, int);
            if (w < 0) {
                /* negative width is width with implied '-' */
                width = (unsigned int)-(w + 1) + 1;
                alignchar = '-';
            }
            else {
                width = w;
            }

            ch = *fmt++;
        }
        else if (ch >= '1' && ch <= '9') {
            /* fixed width */
            fmt = parse_number_spec(fmt, ch, &width);
            ch = *fmt++;
        }

        /*** precision ***/
        if (ch == '.') {
            ch = *fmt++;

            if (ch == '*') {
                /* variable precision; negative precision is ignored */
                precision = va_arg (ap, int);
                ch = *fmt++;
            }
            else if (ch >= '0' && ch <= '9') {
                /* fixed precision */
                fmt = parse_number_spec(fmt, ch, &precision);
                ch = *fmt++;
            }
        }

        /*** length modifier ***/
        #if FMT_LENMOD
        switch (ch)
        {
        #if (FMT_LENMOD & (FMT_LENMOD_h | FMT_LENMOD_hh))
        case 'h':
        #endif
        #if (FMT_LENMOD & FMT_LENMOD_j)
        case 'j':
        #endif
        #if (FMT_LENMOD & (FMT_LENMOD_l | FMT_LENMOD_ll))
        case 'l':
        #endif
        #if (FMT_LENMOD & FMT_LENMOD_t)
        case 't':
        #endif
        #if (FMT_LENMOD & FMT_LENMOD_z)
        case 'z':
        #endif
        #if (FMT_LENMOD & FMT_LENMOD_L)
        case 'L':
        #endif
            lenmod = ch;
            ch = *fmt++;
        #if (FMT_LENMOD & (FMT_LENMOD_hh | FMT_LENMOD_ll))
            /* doesn't matter if jj, tt or zz happen; they will be rejected
               by the radix handler */
            if (ch == lenmod) {
                lenmod = LENMOD2(lenmod, ch);
                ch = *fmt++;
            }
        #endif
        }
        #endif /* FMT_LENMOD */

        /*** radix ***/
        switch (ch)
        {
            /** non-numeric **/
        case 'c':
            buf = format_c(va_arg(ap, int),  &fmt_buf, lenmod);
            break;
        #if (FMT_RADIX & FMT_RADIX_n)
        case 'n':
            if (format_n(va_arg(ap, void *), count, lenmod)) {
                continue; /* no output */
            }
            break;
        #endif
        case 's':
            buf = format_s(va_arg(ap, const void *), &fmt_buf,
                           precision, lenmod);
            break;

            /** non-integer **/
        #if (FMT_RADIX & FMT_RADIX_p)
        case 'p':
        case 'P':
            buf = format_p(va_arg(ap, void *), &fmt_buf, ch,
                           &numeric);
            break;
        #endif

        #if (FMT_RADIX & FMT_RADIX_floats)
        /* any floats gets all of them (except with 'L' and %a, %A for now) */
        case 'e':
        case 'E':
        case 'f':
        case 'F':
        case 'g':
        case 'G':
            /* LENMOD_L isn't supported for now and will be rejected automatically */

            /* floating point has very different spec interpretations to other
               formats and requires special handling */
            fmt_buf.length    = pfxlen;
            fmt_buf.lenmod    = lenmod;
            fmt_buf.radixchar = ch;
            fmt_buf.signchar  = signchar;
            fmt_buf.alignchar = alignchar;
            fmt_buf.width     = width;
            fmt_buf.precision = precision;

            ch = format_double_radix(va_arg(ap, double), &fmt_buf, push, userp);
            if (ch) {
                count += fmt_buf.length;
                if (ch > 0) {
                    continue;
                }

                goto done;
            }

            buf = fmt_buf.p;
            break;
        #endif

            /** signed integer **/
        case 'd':
        case 'i':
            fmt_buf.length = pfxlen;

            switch (lenmod)
            {
            case LENMOD_NONE:
                val_d = va_arg(ap, signed int);
                goto branch_fmt_d;
            #if (FMT_LENMOD & FMT_LENMOD_h)
            case LENMOD_h:
                val_d = (signed short)va_arg(ap, SSHRT_INT_ARG);
                goto branch_fmt_d;
            #endif
            #if (FMT_LENMOD & FMT_LENMOD_hh)
            case LENMOD_hh:
                val_d = (signed char)va_arg(ap, SCHAR_INT_ARG);
                goto branch_fmt_d;
            #endif
            #if (FMT_LENMOD & FMT_LENMOD_j)
            case LENMOD_j:
                LENMOD_INTCOMPAT_BRANCH(LENMOD_INTCOMPAT_SEL(intmax_t, true),
                                        va_arg(ap, intmax_t), true);
            #endif
            #if (FMT_LENMOD & FMT_LENMOD_l)
            case LENMOD_l:
                val_ld = va_arg(ap, signed long);
                goto branch_fmt_ld;
            #endif
            #if (FMT_LENMOD & FMT_LENMOD_ll)
            case LENMOD_ll:
                val_lld = va_arg(ap, signed long long);
                goto branch_fmt_lld;
            #endif
            #if (FMT_LENMOD & FMT_LENMOD_t)
            case LENMOD_t:
                LENMOD_INTCOMPAT_BRANCH(LENMOD_INTCOMPAT_SEL(ptrdiff_t, true),
                                        va_arg(ap, ptrdiff_t), true);
            #endif
            #if (FMT_LENMOD & FMT_LENMOD_z)
            case LENMOD_z:
                LENMOD_INTCOMPAT_BRANCH(LENMOD_INTCOMPAT_SEL(ssize_t, true),
                                        va_arg(ap, ssize_t), true);
            #endif
            }

            /* macrofied labels share a formatter with another */
            if (0) {
            branch_fmt_d:
                buf = format_d(val_d, &fmt_buf, signchar);
            } else if (0) {
            #ifndef val_ld
            branch_fmt_ld:
                buf = format_ld(val_ld, &fmt_buf, signchar);
            #endif
            } else if (0) {
            #ifndef val_lld
            branch_fmt_lld:
                buf = format_lld(val_lld, &fmt_buf, signchar);
            #endif
            }

            numeric = true;
            break;

            /** unsigned integer **/
        #if (FMT_RADIX & FMT_RADIX_o)
        case 'o':
        #endif
        case 'u':
        #if (FMT_RADIX & FMT_RADIX_x)
        case 'x':
        case 'X':
        #endif
            fmt_buf.length = pfxlen;

            switch (lenmod)
            {
            case LENMOD_NONE:
                val_u = va_arg(ap, unsigned int);
                goto branch_fmt_u;
            #if (FMT_LENMOD & FMT_LENMOD_h)
            case LENMOD_h:
                val_u = (unsigned short)va_arg(ap, USHRT_INT_ARG);
                goto branch_fmt_u;
            #endif
            #if (FMT_LENMOD & FMT_LENMOD_hh)
            case LENMOD_hh:
                val_u = (unsigned char)va_arg(ap, UCHAR_INT_ARG);
                goto branch_fmt_u;
            #endif
            #if (FMT_LENMOD & FMT_LENMOD_j)
            case LENMOD_j:
                LENMOD_INTCOMPAT_BRANCH(LENMOD_INTCOMPAT_SEL(uintmax_t, false),
                                        va_arg(ap, uintmax_t), false);
            #endif
            #if (FMT_LENMOD & FMT_LENMOD_l)
            case LENMOD_l:
                val_lu = va_arg(ap, unsigned long);
                goto branch_fmt_lu;
            #endif
            #if (FMT_LENMOD & FMT_LENMOD_ll)
            case LENMOD_ll:
                val_llu = va_arg(ap, unsigned long long);
                goto branch_fmt_llu;
            #endif
            #if (FMT_LENMOD & (FMT_LENMOD_t | FMT_LENMOD_z))
            /* format "uptrdiff_t" as size_t (unless it becomes standard) */
            #if (FMT_LENMOD & FMT_LENMOD_t)
            case LENMOD_t:
            #endif
            #if (FMT_LENMOD & FMT_LENMOD_z)
            case LENMOD_z:
            #endif
                LENMOD_INTCOMPAT_BRANCH(LENMOD_INTCOMPAT_SEL(size_t, false),
                                        va_arg(ap, size_t), false);
            #endif
            }

            /* macrofied labels share a formatter with another */
            if (0) {
            branch_fmt_u:
                buf = format_u(val_u, &fmt_buf, ch);
            } else if (0) {
            #ifndef val_lu
            branch_fmt_lu:
                buf = format_lu(val_lu, &fmt_buf, ch);
            #endif
            } else if (0) {
            #ifndef val_llu
            branch_fmt_llu:
                buf = format_llu(val_llu, &fmt_buf, ch);
            #endif
            }

            numeric = true;
            break;
        }

        if (buf) {
            /** padding **/
            if (numeric) {
                /* numeric formats into fmt_buf.buf */
                pfxlen = fmt_buf.length;
                length = fmt_buf.bufend - buf;

                size_t size = pfxlen + length;

                if (precision >= 0) {
                    /* explicit precision */
                    precision -= (int)length;

                    if (precision > 0) {
                        size += precision;
                    }

                    width -= MIN(width, size);
                }
                else {
                    /* default precision */
                    if (!length) {
                        length = 1;
                        size++;
                    }

                    width -= MIN(width, size);

                    if (alignchar == '0') {
                        /* width zero-fill */
                        precision = width;
                        width = 0;
                    }
                }
            }
            else {
                /* non-numeric: supress prefix and precision; keep length and
                   width */
                pfxlen = 0;
                precision = 0;
                length = fmt_buf.length;
                width -= MIN(width, length);
            }
        }
        else {
            /* format not accepted; print it literally */
            buf = fmt_buf.fmt_start - 2;
            length = fmt - buf;
            width = 0;
            pfxlen = 0;
            precision = 0;
        }

        /** push all the stuff **/

        if (alignchar != '-') {
            /* left padding */
            while (width > 0) {
                PUSHCHAR(' ');
                width--;
            }
        }

        /* prefix */
        while (pfxlen > 0) {
            PUSHCHAR(buf[-pfxlen]);
            pfxlen--;
        }

        /* 0-padding */
        while (precision > 0) {
            PUSHCHAR('0');
            precision--;
        }

        /* field */
        while (length > 0) {
            PUSHCHAR(*buf++);
            length--;
        }

        /* right padding */
        while (width > 0) {
            PUSHCHAR(' ');
            width--;
        }
    }

done:
    return count;
}