summaryrefslogtreecommitdiffstats
path: root/common/estream-printf.c
blob: 3955a01cbf3ec5cbb457258e798577e7a9e14dca (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
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
/* estream-printf.c - Versatile mostly C-99 compliant printf formatting
 * Copyright (C) 2007, 2008, 2009, 2010 g10 Code GmbH
 *
 * This file is part of Libestream.
 *
 * Libestream 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.
 *
 * Libestream is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Libestream; if not, see <http://www.gnu.org/licenses/>.
 *
 * ALTERNATIVELY, Libestream may be distributed under the terms of the
 * following license, in which case the provisions of this license are
 * required INSTEAD OF the GNU General Public License. If you wish to
 * allow use of your version of this file only under the terms of the
 * GNU General Public License, and not to allow others to use your
 * version of this file under the terms of the following license,
 * indicate your decision by deleting this paragraph and the license
 * below.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, and the entire permission notice in its entirety,
 *    including the disclaimer of warranties.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote
 *    products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*  Required autoconf tests:

    AC_TYPE_LONG_LONG_INT            defines HAVE_LONG_LONG_INT
    AC_TYPE_LONG_DOUBLE              defines HAVE_LONG_DOUBLE
    AC_TYPE_INTMAX_T                 defines HAVE_INTMAX_T
    AC_TYPE_UINTMAX_T                defines HAVE_UINTMAX_T
    AC_CHECK_TYPES([ptrdiff_t])      defines HAVE_PTRDIFF_T
    AC_CHECK_SIZEOF([unsigned long]) defines SIZEOF_UNSIGNED_LONG
    AC_CHECK_SIZEOF([void *])        defines SIZEOF_VOID_P
                                             HAVE_LANGINFO_THOUSANDS_SEP

    Note that the file estream.m4 provides the autoconf macro
    ESTREAM_PRINTF_INIT which runs all required checks.
    See estream-printf.h for ways to tune this code.

  Missing stuff:  wchar and wint_t
                  thousands_sep in pr_float.

*/

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#if defined(_WIN32) && !defined(HAVE_W32_SYSTEM)
# define HAVE_W32_SYSTEM 1
# if defined(__MINGW32CE__) && !defined (HAVE_W32CE_SYSTEM)
#  define HAVE_W32CE_SYSTEM
# endif
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include <errno.h>
#include <stddef.h>
#include <assert.h>
#if defined(HAVE_INTMAX_T) || defined(HAVE_UINTMAX_T)
# ifdef HAVE_STDINT_H
#  include <stdint.h>
# endif 
#endif
#ifdef HAVE_LANGINFO_THOUSANDS_SEP
#include <langinfo.h>
#endif
#ifdef HAVE_W32CE_SYSTEM
#include <gpg-error.h>  /* ERRNO replacement.  */
#endif
#ifdef _ESTREAM_PRINTF_EXTRA_INCLUDE
# include _ESTREAM_PRINTF_EXTRA_INCLUDE
#endif
#include "estream-printf.h"

/* #define DEBUG 1 */


/* Allow redefinition of asprintf used malloc functions.  */
#if defined(_ESTREAM_PRINTF_MALLOC)
#define my_printf_malloc(a) _ESTREAM_PRINTF_MALLOC((a))  
#else
#define my_printf_malloc(a) malloc((a))
#endif
#if defined(_ESTREAM_PRINTF_FREE)
#define my_printf_free(a)   _ESTREAM_PRINTF_FREE((a))  
#else
#define my_printf_free(a)   free((a))
#endif

/* A wrapper to set ERRNO.  */
#ifdef HAVE_W32CE_SYSTEM
# define _set_errno(a)  gpg_err_set_errno ((a))
#else
# define _set_errno(a)  do { errno = (a); } while (0)
#endif


/* Calculate array dimension.  */
#ifndef DIM
#define DIM(array) (sizeof (array) / sizeof (*array))
#endif


/* We allow for that many args without requiring malloced memory. */
#define DEFAULT_MAX_ARGSPECS  5

/* We allow for that many values without requiring malloced memory. */
#define DEFAULT_MAX_VALUES  8

/* We allocate this many new array argspec elements each time.  */
#define ARGSPECS_BUMP_VALUE   10

/* Special values for the field width and the precision.  */
#define NO_FIELD_VALUE   (-1)
#define STAR_FIELD_VALUE (-2)

/* Bit valuues used for the conversion flags. */
#define FLAG_GROUPING   1
#define FLAG_LEFT_JUST  2
#define FLAG_PLUS_SIGN  4
#define FLAG_SPACE_PLUS 8
#define FLAG_ALT_CONV   16
#define FLAG_ZERO_PAD   32

/* Constants used the length modifiers.  */
typedef enum
  {
    LENMOD_NONE = 0,
    LENMOD_CHAR,     /* "hh" */
    LENMOD_SHORT,    /* "h"  */
    LENMOD_LONG,     /* "l"  */
    LENMOD_LONGLONG, /* "ll" */
    LENMOD_INTMAX,   /* "j"  */
    LENMOD_SIZET,    /* "z"  */
    LENMOD_PTRDIFF,  /* "t"  */
    LENMOD_LONGDBL   /* "L"  */
  } lenmod_t;

/* All the conversion specifiers.  */
typedef enum
  {
    CONSPEC_UNKNOWN = 0,
    CONSPEC_DECIMAL,
    CONSPEC_OCTAL,
    CONSPEC_UNSIGNED,
    CONSPEC_HEX,
    CONSPEC_HEX_UP,
    CONSPEC_FLOAT,
    CONSPEC_FLOAT_UP,
    CONSPEC_EXP,
    CONSPEC_EXP_UP,
    CONSPEC_F_OR_G,
    CONSPEC_F_OR_G_UP,
    CONSPEC_HEX_EXP,
    CONSPEC_HEX_EXP_UP,
    CONSPEC_CHAR,
    CONSPEC_STRING,
    CONSPEC_POINTER,
    CONSPEC_STRERROR,
    CONSPEC_BYTES_SO_FAR
  } conspec_t;


/* Constants describing all the suppoorted types.  Note that we list
   all the types we know about even if certain types are not available
   on this system. */
typedef enum
  {
    VALTYPE_UNSUPPORTED = 0,  /* Artificial type for error detection.  */
    VALTYPE_CHAR,
    VALTYPE_SCHAR,
    VALTYPE_UCHAR,
    VALTYPE_SHORT,
    VALTYPE_USHORT,
    VALTYPE_INT,
    VALTYPE_UINT,
    VALTYPE_LONG,
    VALTYPE_ULONG,
    VALTYPE_LONGLONG,
    VALTYPE_ULONGLONG,
    VALTYPE_DOUBLE,
    VALTYPE_LONGDOUBLE,
    VALTYPE_STRING,
    VALTYPE_INTMAX,
    VALTYPE_UINTMAX,
    VALTYPE_SIZE,
    VALTYPE_PTRDIFF,
    VALTYPE_POINTER,
    VALTYPE_CHAR_PTR,
    VALTYPE_SCHAR_PTR,
    VALTYPE_SHORT_PTR,
    VALTYPE_INT_PTR,
    VALTYPE_LONG_PTR,
    VALTYPE_LONGLONG_PTR,
    VALTYPE_INTMAX_PTR,
    VALTYPE_SIZE_PTR,
    VALTYPE_PTRDIFF_PTR
  } valtype_t;


/* A union used to store the actual values. */
typedef union 
{
  char a_char;
  signed char a_schar;
  unsigned char a_uchar;
  short a_short;
  unsigned short a_ushort;
  int a_int;
  unsigned int a_uint;
  long int a_long;
  unsigned long int a_ulong;
#ifdef HAVE_LONG_LONG_INT
  long long int a_longlong;
  unsigned long long int a_ulonglong;
#endif
  double a_double;
#ifdef HAVE_LONG_DOUBLE
  long double a_longdouble;
#endif
  const char *a_string;
#ifdef HAVE_INTMAX_T
  intmax_t a_intmax;
#endif
#ifdef HAVE_UINTMAX_T
  intmax_t a_uintmax;
#endif
  size_t a_size;
#ifdef HAVE_PTRDIFF_T
  ptrdiff_t a_ptrdiff;
#endif
  void *a_void_ptr;
  char *a_char_ptr;
  signed char *a_schar_ptr;
  short *a_short_ptr;
  int  *a_int_ptr;
  long *a_long_ptr;
#ifdef HAVE_LONG_LONG_INT
  long long int *a_longlong_ptr;
#endif
#ifdef HAVE_INTMAX_T
  intmax_t *a_intmax_ptr;
#endif
  size_t *a_size_ptr;
#ifdef HAVE_PTRDIFF_T
  ptrdiff_t *a_ptrdiff_ptr;
#endif
} value_t;

/* An object used to keep track of a format option and arguments. */
struct argspec_s
{
  size_t length;       /* The length of these args including the percent.  */
  unsigned int flags;  /* The conversion flags (bits defined by FLAG_foo).  */
  int width;           /* The field width.  */
  int precision;       /* The precision.  */
  lenmod_t lenmod;     /* The length modifier.  */
  conspec_t conspec;   /* The conversion specifier.  */
  int arg_pos;         /* The position of the argument.  This one may
                          be -1 to indicate that no value is expected
                          (e.g. for "%m").  */
  int width_pos;       /* The position of the argument for a field
                          width star's value. 0 for not used.  */
  int precision_pos;   /* The position of the argument for the a
                          precision star's value.  0 for not used. */
  valtype_t vt;        /* The type of the corresponding argument.  */
};
typedef struct argspec_s *argspec_t;

/* An object to build up a table of values and their types.  */
struct valueitem_s
{
  valtype_t vt;  /* The type of the value.  */
  value_t value; /* The value.  */
};
typedef struct valueitem_s *valueitem_t;


#ifdef DEBUG
static void
dump_argspecs (argspec_t arg, size_t argcount)
{
  int idx;

  for (idx=0; argcount; argcount--, arg++, idx++)
    fprintf (stderr, 
             "%2d: len=%u flags=%u width=%d prec=%d mod=%d "
             "con=%d vt=%d pos=%d-%d-%d\n",
             idx,
             (unsigned int)arg->length,
             arg->flags,
             arg->width,
             arg->precision,
             arg->lenmod,
             arg->conspec,
             arg->vt,
             arg->arg_pos,
             arg->width_pos,
             arg->precision_pos);
}
#endif /*DEBUG*/


/* Set the vt field for ARG.  */
static void
compute_type (argspec_t arg)
{
  switch (arg->conspec)
    {
    case CONSPEC_UNKNOWN: 
      arg->vt = VALTYPE_UNSUPPORTED; 
      break;

    case CONSPEC_DECIMAL:
      switch (arg->lenmod)
        {
        case LENMOD_CHAR: arg->vt = VALTYPE_SCHAR; break;
        case LENMOD_SHORT: arg->vt = VALTYPE_SHORT; break;
        case LENMOD_LONG: arg->vt = VALTYPE_LONG; break;
        case LENMOD_LONGLONG: arg->vt = VALTYPE_LONGLONG; break;
        case LENMOD_INTMAX: arg->vt = VALTYPE_INTMAX; break;
        case LENMOD_SIZET: arg->vt = VALTYPE_SIZE; break;   
        case LENMOD_PTRDIFF: arg->vt = VALTYPE_PTRDIFF; break;
        default: arg->vt = VALTYPE_INT; break;
        }
      break;

    case CONSPEC_OCTAL:
    case CONSPEC_UNSIGNED:
    case CONSPEC_HEX:
    case CONSPEC_HEX_UP:
      switch (arg->lenmod)
        {
        case LENMOD_CHAR: arg->vt = VALTYPE_UCHAR; break;
        case LENMOD_SHORT: arg->vt = VALTYPE_USHORT; break;
        case LENMOD_LONG: arg->vt = VALTYPE_ULONG; break;
        case LENMOD_LONGLONG: arg->vt = VALTYPE_ULONGLONG; break;
        case LENMOD_INTMAX: arg->vt = VALTYPE_UINTMAX; break;
        case LENMOD_SIZET: arg->vt = VALTYPE_SIZE; break;   
        case LENMOD_PTRDIFF: arg->vt = VALTYPE_PTRDIFF; break;
        default: arg->vt = VALTYPE_UINT; break;
        }
      break;
      
    case CONSPEC_FLOAT:
    case CONSPEC_FLOAT_UP:
    case CONSPEC_EXP:
    case CONSPEC_EXP_UP:
    case CONSPEC_F_OR_G:
    case CONSPEC_F_OR_G_UP:
    case CONSPEC_HEX_EXP:
    case CONSPEC_HEX_EXP_UP:
      switch (arg->lenmod)
        {
        case LENMOD_LONGDBL: arg->vt = VALTYPE_LONGDOUBLE; break;
        case LENMOD_LONG: arg->vt = VALTYPE_DOUBLE; break;
        default: arg->vt = VALTYPE_DOUBLE; break;
        }
      break;
      
    case CONSPEC_CHAR:
      arg->vt = VALTYPE_INT; 
      break;

    case CONSPEC_STRING:
      arg->vt = VALTYPE_STRING;
      break;

    case CONSPEC_POINTER:
      arg->vt = VALTYPE_POINTER;
      break;

    case CONSPEC_STRERROR:
      arg->vt = VALTYPE_STRING;
      break;

    case CONSPEC_BYTES_SO_FAR:
      switch (arg->lenmod)
        {
        case LENMOD_CHAR: arg->vt = VALTYPE_SCHAR_PTR; break;
        case LENMOD_SHORT: arg->vt = VALTYPE_SHORT_PTR; break;
        case LENMOD_LONG: arg->vt = VALTYPE_LONG_PTR; break;
        case LENMOD_LONGLONG: arg->vt = VALTYPE_LONGLONG_PTR; break;
        case LENMOD_INTMAX: arg->vt = VALTYPE_INTMAX_PTR; break;
        case LENMOD_SIZET: arg->vt = VALTYPE_SIZE_PTR; break;   
        case LENMOD_PTRDIFF: arg->vt = VALTYPE_PTRDIFF_PTR; break;
        default: arg->vt = VALTYPE_INT_PTR; break;
        }
      break;
      
    }
}



/* Parse the FORMAT string and populate the specification array stored
   at the address ARGSPECS_ADDR.  The caller has provided enough space
   to store up to MAX_ARGSPECS in that buffer.  The function may
   however ignore the provided buffer and malloc a larger one.  On
   success the addrrss of that larger buffer will be stored at
   ARGSPECS_ADDR.  The actual number of specifications will be
   returned at R_ARGSPECS_COUNT. */
static int
parse_format (const char *format,
              argspec_t *argspecs_addr, size_t max_argspecs,
              size_t *r_argspecs_count)
{
  const char *s;
  argspec_t argspecs = *argspecs_addr;
  argspec_t arg;
  size_t argcount = 0;

  if (!format)
    goto leave_einval;
      
  for (; *format; format++)
    {
      unsigned int flags;
      int width, precision;
      lenmod_t lenmod;
      conspec_t conspec;
      int arg_pos, width_pos, precision_pos;
      
      if (*format != '%')
        continue;
      s = ++format;
      if (!*s)
        goto leave_einval;
      if (*s == '%')
        continue; /* Just a quoted percent.  */

      /* First check whether there is a positional argument.  */
      arg_pos = 0; /* No positional argument given.  */
      if (*s >= '1' && *s <= '9')
        {
          const char *save_s = s;
          
          arg_pos = (*s++ - '0');
          for (; *s >= '0' && *s <= '9'; s++)
            arg_pos = 10*arg_pos + (*s - '0');
          if (arg_pos < 0)
            goto leave_einval; /* Overflow during conversion.  */
          if (*s == '$')
            s++;
          else
            {
              arg_pos = 0;
              s = save_s;
            }
        }
         
      /* Parse the flags.  */
      flags = 0;
      for ( ; *s; s++)
        {
          switch (*s)
            {
            case '\'': flags |= FLAG_GROUPING; break;
            case '-': flags |= FLAG_LEFT_JUST; break;
            case '+': flags |= FLAG_PLUS_SIGN; break;
            case ' ': flags |= FLAG_SPACE_PLUS; break;
            case '#': flags |= FLAG_ALT_CONV; break;
            case '0': flags |= FLAG_ZERO_PAD; break;
            default:
              goto flags_parsed;
            }
        }
    flags_parsed:
      
      /* Parse the field width.  */
      width_pos = 0;
      if (*s == '*')
        {
          width = STAR_FIELD_VALUE;
          s++;
          /* If we have a positional argument, another one might also
             be used to give the position of the star's value. */
          if (arg_pos && *s >= '1' && *s <= '9')
            {
              width_pos = (*s++ - '0');
              for (; *s >= '0' && *s <= '9'; s++)
                width_pos = 10*width_pos + (*s - '0');
              if (width_pos < 1)
                goto leave_einval; /* Overflow during conversion.  */
              if (*s != '$')
                goto leave_einval; /* Not followed by $.  */
              s++;
            }
        }
      else if ( *s >= '0' && *s <= '9')
        {
          width = (*s++ - '0');
          for (; *s >= '0' && *s <= '9'; s++)
            {
              if (!width && *s == '0')
                goto leave_einval; /* Leading zeroes are not allowed.
                                      Fixme: check what other
                                      implementations do. */
              width = 10*width + (*s - '0');
            }
          if (width < 0)
            goto leave_einval; /* Overflow during conversion.  */
        }
      else
        width = NO_FIELD_VALUE;

      /* Parse the precision.  */
      precision_pos = 0;
      precision = NO_FIELD_VALUE;
      if (*s == '.')
        {
          int ignore_value = (s[1] == '-');

          s++;
          if (*s == '*')
            {
              precision = STAR_FIELD_VALUE;
              s++;
              /* If we have a positional argument, another one might also
                 be used to give the position of the star's value. */
              if (arg_pos && *s >= '1' && *s <= '9')
                {
                  precision_pos = (*s++ - '0');
                  for (; *s >= '0' && *s <= '9'; s++)
                    precision_pos = 10*precision_pos + (*s - '0');
                  if (precision_pos < 1)
                    goto leave_einval; /* Overflow during conversion.  */
                  if (*s != '$')
                    goto leave_einval; /* Not followed by $.  */
                  s++;
                }
            }
          else if ( *s >= '0' && *s <= '9')
            {
              precision = (*s++ - '0');
              for (; *s >= '0' && *s <= '9'; s++)
                {
                  if (!precision && *s == '0')
                    goto leave_einval; /* Leading zeroes are not allowed.
                                          Fixme: check what other
                                          implementations do. */
                  precision = 10*precision + (*s - '0');
                }
              if (precision < 0)
                goto leave_einval; /* Overflow during conversion.  */
            }
          else
            precision = 0;
          if (ignore_value)
            precision = NO_FIELD_VALUE;
        }
      
      /* Parse the length modifiers.  */
      switch (*s)
        {
        case 'h': 
          if (s[1] == 'h')
            {
              lenmod = LENMOD_CHAR;
              s++;
            }
          else
            lenmod = LENMOD_SHORT;
          s++;
          break;
        case 'l':
          if (s[1] == 'l')
            {
              lenmod = LENMOD_LONGLONG;
              s++;
            }
          else
            lenmod = LENMOD_LONG;
          s++;
          break;
        case 'j': lenmod = LENMOD_INTMAX; s++; break;
        case 'z': lenmod = LENMOD_SIZET; s++; break;
        case 't': lenmod = LENMOD_PTRDIFF; s++; break;
        case 'L': lenmod = LENMOD_LONGDBL; s++; break;
        default:  lenmod = LENMOD_NONE; break;
        }
      
      /* Parse the conversion specifier.  */
      switch (*s)
        {
        case 'd':
        case 'i': conspec = CONSPEC_DECIMAL; break;
        case 'o': conspec = CONSPEC_OCTAL; break;
        case 'u': conspec = CONSPEC_UNSIGNED; break;
        case 'x': conspec = CONSPEC_HEX; break;
        case 'X': conspec = CONSPEC_HEX_UP; break;
        case 'f': conspec = CONSPEC_FLOAT; break;
        case 'F': conspec = CONSPEC_FLOAT_UP; break;
        case 'e': conspec = CONSPEC_EXP; break;
        case 'E': conspec = CONSPEC_EXP_UP; break;
        case 'g': conspec = CONSPEC_F_OR_G; break;
        case 'G': conspec = CONSPEC_F_OR_G_UP; break;
        case 'a': conspec = CONSPEC_HEX_EXP; break;
        case 'A': conspec = CONSPEC_HEX_EXP_UP; break;
        case 'c': conspec = CONSPEC_CHAR; break;
        case 's': conspec = CONSPEC_STRING; break;
        case 'p': conspec = CONSPEC_POINTER; break;
        case 'n': conspec = CONSPEC_BYTES_SO_FAR; break;
        case 'C': conspec = CONSPEC_CHAR; lenmod = LENMOD_LONG; break;
        case 'S': conspec = CONSPEC_STRING; lenmod = LENMOD_LONG; break;
        case 'm': conspec = CONSPEC_STRERROR; arg_pos = -1; break;
        default: conspec = CONSPEC_UNKNOWN;
        }

      /* Save the args. */
      if (argcount >= max_argspecs)
        {
          /* We either need to allocate a new array instead of the
             caller provided one or realloc the array.  Instead of
             using realloc we allocate a new one and release the
             original one then. */
          size_t n, newmax;
          argspec_t newarg;

          newmax = max_argspecs + ARGSPECS_BUMP_VALUE;
          if (newmax <= max_argspecs)
            goto leave_einval;  /* Too many arguments. */
          newarg = calloc (newmax, sizeof *newarg);
          if (!newarg)
            goto leave;
          for (n=0; n < argcount; n++)
            newarg[n] = argspecs[n];
          if (argspecs != *argspecs_addr)
            free (argspecs);
          argspecs = newarg;
          max_argspecs = newmax;
        }

      arg = argspecs + argcount;
      arg->length = s - format + 2;
      arg->flags = flags;
      arg->width = width;
      arg->precision = precision;
      arg->lenmod = lenmod;
      arg->conspec = conspec;
      arg->arg_pos = arg_pos;
      arg->width_pos = width_pos;
      arg->precision_pos = precision_pos;
      compute_type (arg);
      argcount++;
      format = s;
    }

  *argspecs_addr = argspecs;
  *r_argspecs_count = argcount;
  return 0; /* Success.  */
  
 leave_einval:
  _set_errno (EINVAL);
 leave:
  if (argspecs != *argspecs_addr)
    free (argspecs);
  *argspecs_addr = NULL;
  return -1;
}


/* This function reads all the values as specified by VALUETABLE into
   VALUETABLE.  The values are expected in VAARGS.  The function
   returns -1 if a specified type is not supported. */
static int
read_values (valueitem_t valuetable, size_t valuetable_len, va_list vaargs)
{
  int validx;

  for (validx=0; validx < valuetable_len; validx++)
    {
      value_t *value = &valuetable[validx].value;
      valtype_t vt = valuetable[validx].vt;

      switch (vt)
        {
        case VALTYPE_CHAR: value->a_char = va_arg (vaargs, int); break;
        case VALTYPE_CHAR_PTR:
          value->a_char_ptr = va_arg (vaargs, char *);
          break;
        case VALTYPE_SCHAR: value->a_schar = va_arg (vaargs, int); break;
        case VALTYPE_SCHAR_PTR: 
          value->a_schar_ptr = va_arg (vaargs, signed char *); 
          break;
        case VALTYPE_UCHAR: value->a_uchar = va_arg (vaargs, int); break;
        case VALTYPE_SHORT: value->a_short = va_arg (vaargs, int); break;
        case VALTYPE_USHORT: value->a_ushort = va_arg (vaargs, int); break;
        case VALTYPE_SHORT_PTR: 
          value->a_short_ptr = va_arg (vaargs, short *); 
          break;
        case VALTYPE_INT:
          value->a_int = va_arg (vaargs, int);
          break;
        case VALTYPE_INT_PTR:
          value->a_int_ptr = va_arg (vaargs, int *);
          break;
        case VALTYPE_UINT:
          value->a_uint = va_arg (vaargs, unsigned int);
          break;
        case VALTYPE_LONG:
          value->a_long = va_arg (vaargs, long);
          break;
        case VALTYPE_ULONG: 
          value->a_ulong = va_arg (vaargs, unsigned long);
          break;
        case VALTYPE_LONG_PTR: 
          value->a_long_ptr = va_arg (vaargs, long *); 
          break;
#ifdef HAVE_LONG_LONG_INT
        case VALTYPE_LONGLONG:
          value->a_longlong = va_arg (vaargs, long long int);
          break;
        case VALTYPE_ULONGLONG: 
          value->a_ulonglong = va_arg (vaargs, unsigned long long int); 
          break;
        case VALTYPE_LONGLONG_PTR: 
          value->a_longlong_ptr = va_arg (vaargs, long long *);
          break;
#endif
        case VALTYPE_DOUBLE:
          value->a_double = va_arg (vaargs, double);
          break;
#ifdef HAVE_LONG_DOUBLE
        case VALTYPE_LONGDOUBLE:
          value->a_longdouble = va_arg (vaargs, long double);
          break;
#endif
        case VALTYPE_STRING:
          value->a_string = va_arg (vaargs, const char *);
          break;
        case VALTYPE_POINTER: 
          value->a_void_ptr = va_arg (vaargs, void *);
          break;
#ifdef HAVE_INTMAX_T
        case VALTYPE_INTMAX:
          value->a_intmax = va_arg (vaargs, intmax_t);
          break;
        case VALTYPE_INTMAX_PTR: 
          value->a_intmax_ptr = va_arg (vaargs, intmax_t *); 
          break;
#endif
#ifdef HAVE_UINTMAX_T
        case VALTYPE_UINTMAX: 
          value->a_uintmax = va_arg (vaargs, uintmax_t); 
          break;
#endif
        case VALTYPE_SIZE:
          value->a_size = va_arg (vaargs, size_t);
          break;
        case VALTYPE_SIZE_PTR: 
          value->a_size_ptr = va_arg (vaargs, size_t *); 
          break;
#ifdef HAVE_PTRDIFF_T
        case VALTYPE_PTRDIFF:
          value->a_ptrdiff = va_arg (vaargs, ptrdiff_t); 
          break;
        case VALTYPE_PTRDIFF_PTR:
          value->a_ptrdiff_ptr = va_arg (vaargs, ptrdiff_t *);
          break;
#endif
        default: /* Unsupported type.  */
          return -1;
        }
    }
  return 0;
}



/* Output COUNT padding characters PADCHAR and update NBYTES by the
   number of bytes actually written.  */
static int
pad_out (estream_printf_out_t outfnc, void *outfncarg,
         int padchar, int count, size_t *nbytes)
{
  char buf[32];
  size_t n;
  int rc;

  while (count > 0)
    {
      n = (count <= sizeof buf)? count : sizeof buf;
      memset (buf, padchar, n);
      rc = outfnc (outfncarg, buf, n);
      if (rc)
        return rc;
      *nbytes += n;
      count -= n;
    }
  
  return 0;
}


/* "d,i,o,u,x,X" formatting.  OUTFNC and OUTFNCARG describes the
   output routine, ARG gives the argument description and VALUE the
   actual value (its type is available through arg->vt).  */
static int
pr_integer (estream_printf_out_t outfnc, void *outfncarg,
            argspec_t arg, value_t value, size_t *nbytes)
{
  int rc;
#ifdef HAVE_LONG_LONG_INT
  unsigned long long aulong;
#else
  unsigned long aulong;
#endif
  char numbuf[100];
  char *p, *pend;
  size_t n;
  char signchar = 0;
  int n_prec;  /* Number of extra precision digits required.  */
  int n_extra; /* Extra number of prefix or sign characters.  */

  if (arg->conspec == CONSPEC_DECIMAL)
    {
#ifdef HAVE_LONG_LONG_INT
      long long along;
#else
      long along;
#endif

      switch (arg->vt)
        {
        case VALTYPE_SHORT: along = value.a_short; break;
        case VALTYPE_INT: along = value.a_int; break;
        case VALTYPE_LONG: along = value.a_long; break;  
#ifdef HAVE_LONG_LONG_INT
        case VALTYPE_LONGLONG: along = value.a_longlong; break;  
        case VALTYPE_SIZE: along = value.a_size; break;  
# ifdef HAVE_INTMAX_T
        case VALTYPE_INTMAX: along = value.a_intmax; break;  
# endif
# ifdef HAVE_PTRDIFF_T
        case VALTYPE_PTRDIFF: along = value.a_ptrdiff; break;  
# endif
#endif /*HAVE_LONG_LONG_INT*/
        default: 
          return -1;
        }
      if (along < 0)
        {
          aulong = -along;
          signchar = '-';
        }
      else
        aulong = along;
    }
  else
    {
      switch (arg->vt)
        {
        case VALTYPE_USHORT: aulong = value.a_ushort; break;
        case VALTYPE_UINT: aulong = value.a_uint; break;
        case VALTYPE_ULONG: aulong = value.a_ulong; break;  
#ifdef HAVE_LONG_LONG_INT
        case VALTYPE_ULONGLONG: aulong = value.a_ulonglong; break;  
        case VALTYPE_SIZE: aulong = value.a_size; break;  
# ifdef HAVE_UINTMAX_T
        case VALTYPE_UINTMAX: aulong = value.a_uintmax; break;  
# endif
# ifdef HAVE_PTRDIFF_T
        case VALTYPE_PTRDIFF: aulong = value.a_ptrdiff; break;  
# endif
#endif /*HAVE_LONG_LONG_INT*/
        default: 
          return -1;
        }
    }

  if (signchar == '-')
    ;
  else if ((arg->flags & FLAG_PLUS_SIGN))
    signchar = '+';
  else if ((arg->flags & FLAG_SPACE_PLUS))
    signchar = ' ';

  n_extra = !!signchar;

  /* We build the string up backwards.  */
  p = pend = numbuf + DIM(numbuf);
  if ((!aulong && !arg->precision))
    ;
  else if (arg->conspec == CONSPEC_DECIMAL
           || arg->conspec == CONSPEC_UNSIGNED)
    {
      int grouping = -1;
      const char * grouping_string =
#ifdef HAVE_LANGINFO_THOUSANDS_SEP
        nl_langinfo(THOUSANDS_SEP);
#else
        "'";
#endif

      do
        {
          if ((arg->flags & FLAG_GROUPING) 
              && (++grouping == 3) && *grouping_string)
            {
              *--p = *grouping_string;
              grouping = 0;
            }
          *--p = '0' + (aulong % 10);
          aulong /= 10;
        }
      while (aulong);
    }
  else if (arg->conspec == CONSPEC_OCTAL)
    {
      do
        {
          *--p = '0' + (aulong % 8);
          aulong /= 8;
        }
      while (aulong);
      if ((arg->flags & FLAG_ALT_CONV) && *p != '0')
        *--p = '0';
    }
  else /* HEX or HEXUP */
    {
      const char *digits = ((arg->conspec == CONSPEC_HEX)
                            ? "0123456789abcdef" : "0123456789ABCDEF");
      do
        {
          *--p = digits[(aulong % 16)];
          aulong /= 16;
        }
      while (aulong);
      if ((arg->flags & FLAG_ALT_CONV))
        n_extra += 2;
    }
  
  n = pend - p;

  if ((arg->flags & FLAG_ZERO_PAD)
      && arg->precision == NO_FIELD_VALUE && !(arg->flags & FLAG_LEFT_JUST)
      && n && arg->width - n_extra > n )
    n_prec = arg->width - n_extra - n;
  else if (arg->precision > 0 && arg->precision > n)
    n_prec = arg->precision - n;
  else
    n_prec = 0;

  if (!(arg->flags & FLAG_LEFT_JUST)
      && arg->width >= 0 && arg->width - n_extra > n
      && arg->width - n_extra - n >= n_prec )
    {
      rc = pad_out (outfnc, outfncarg, ' ',
                    arg->width - n_extra - n - n_prec, nbytes);
      if (rc)
        return rc;
    }

  if (signchar)
    {
      rc = outfnc (outfncarg, &signchar, 1);
      if (rc)
        return rc;
      *nbytes += 1;
    }

  if ((arg->flags & FLAG_ALT_CONV)
      && (arg->conspec == CONSPEC_HEX || arg->conspec == CONSPEC_HEX_UP))
    {
      rc = outfnc (outfncarg, arg->conspec == CONSPEC_HEX? "0x": "0X", 2);
      if (rc)
        return rc;
      *nbytes += 2;
    }

  if (n_prec)
    {
      rc = pad_out (outfnc, outfncarg, '0', n_prec, nbytes);
      if (rc)
        return rc;
    }
      
  rc = outfnc (outfncarg, p, pend - p);
  if (rc)
    return rc;
  *nbytes += pend - p;

  if ((arg->flags & FLAG_LEFT_JUST)
      && arg->width >= 0 && arg->width - n_extra - n_prec > n)
    {
      rc = pad_out (outfnc, outfncarg, ' ',
                    arg->width - n_extra - n_prec - n, nbytes);
      if (rc)
        return rc;
    }

  return 0;
}


/* "e,E,f,F,g,G,a,A" formatting.  OUTFNC and OUTFNCARG describes the
   output routine, ARG gives the argument description and VALUE the
   actual value (its type is available through arg->vt).  For
   portability reasons sprintf is used for the actual formatting.
   This is useful because sprint is the only standard function to
   convert a floating number into its ascii representation.  To avoid
   using malloc we just pass the precision to sprintf and do the final
   formatting with our own code.  */
static int
pr_float (estream_printf_out_t outfnc, void *outfncarg,
          argspec_t arg, value_t value, size_t *nbytes)
{
  int rc;
#ifdef HAVE_LONG_DOUBLE
  long double adblfloat = 0; /* Just to please gcc.  */
  int use_dbl = 0;
#endif
  double afloat;
  char numbuf[350];
  char formatstr[20];
  char *p, *pend;
  size_t n;
  char signchar = 0;
  int n_extra;  /* Extra number of prefix or sign characters.  */

  switch (arg->vt)
    {
    case VALTYPE_DOUBLE: afloat = value.a_double; break;
#ifdef HAVE_LONG_DOUBLE
    case VALTYPE_LONGDOUBLE:
      afloat = 0;  /* Just to please gcc.  */
      adblfloat = value.a_longdouble;
      use_dbl=1; break;
#endif
    default: 
      return -1;
    }

  /* We build the string using sprint.  */
  p = formatstr + sizeof formatstr;
  *--p = 0;
  switch (arg->conspec)
    {
    case CONSPEC_FLOAT:      *--p = 'f'; break;
    case CONSPEC_FLOAT_UP:   *--p = 'F'; break;
    case CONSPEC_EXP:        *--p = 'e'; break;
    case CONSPEC_EXP_UP:     *--p = 'E'; break;
    case CONSPEC_F_OR_G:     *--p = 'g'; break;
    case CONSPEC_F_OR_G_UP:  *--p = 'G'; break;
    case CONSPEC_HEX_EXP:    *--p = 'a'; break;
    case CONSPEC_HEX_EXP_UP: *--p = 'A'; break;
    default:
      return -1; /* Actually a bug.  */
    }
#ifdef HAVE_LONG_DOUBLE
  if (use_dbl)
    *--p = 'L';
#endif
  if (arg->precision != NO_FIELD_VALUE)
    {
      /* Limit it to a meaningful value so that even a stupid sprintf
         won't overflow our buffer.  */
      n = arg->precision <= 100? arg->precision : 100;
      do
        {
          *--p = '0' + (n % 10);
          n /= 10;
        }
      while (n);
      *--p = '.';
    }
  if ((arg->flags & FLAG_ALT_CONV))
    *--p = '#';
  *--p = '%';
#ifdef HAVE_LONG_DOUBLE
  if (use_dbl)
    sprintf (numbuf, p, adblfloat);
  else
#endif /*HAVE_LONG_DOUBLE*/
    sprintf (numbuf, p, afloat);
  p = numbuf;
  n = strlen (numbuf);
  pend = p + n;

  if (*p =='-')
    {
      signchar = '-';
      p++;
      n--;
    }
  else if ((arg->flags & FLAG_PLUS_SIGN))
    signchar = '+';
  else if ((arg->flags & FLAG_SPACE_PLUS))
    signchar = ' ';

  n_extra = !!signchar;

  if (!(arg->flags & FLAG_LEFT_JUST)
      && arg->width >= 0 && arg->width - n_extra > n)
    {
      rc = pad_out (outfnc, outfncarg, ' ', arg->width - n_extra - n, nbytes);
      if (rc)
        return rc;
    }

  if (signchar)
    {
      rc = outfnc (outfncarg, &signchar, 1);
      if (rc)
        return rc;
      *nbytes += 1;
    }

  rc = outfnc (outfncarg, p, pend - p);
  if (rc)
    return rc;
  *nbytes += pend - p;

  if ((arg->flags & FLAG_LEFT_JUST)
      && arg->width >= 0 && arg->width - n_extra > n)
    {
      rc = pad_out (outfnc, outfncarg, ' ', arg->width - n_extra - n, nbytes);
      if (rc)
        return rc;
    }

  return 0;
}


/* "c" formatting.  */
static int
pr_char (estream_printf_out_t outfnc, void *outfncarg,
            argspec_t arg, value_t value, size_t *nbytes)
{
  int rc;
  char buf[1];

  if (arg->vt != VALTYPE_INT)
    return -1;
  buf[0] = (unsigned int)value.a_int;
  rc = outfnc (outfncarg, buf, 1);
  if(rc)
    return rc;
  *nbytes += 1;
  
  return 0;
}


/* "s" formatting.  */
static int
pr_string (estream_printf_out_t outfnc, void *outfncarg,
            argspec_t arg, value_t value, size_t *nbytes)
{
  int rc;
  size_t n;
  const char *string, *s;

  if (arg->vt != VALTYPE_STRING)
    return -1;
  string = value.a_string;
  if (!string)
    string = "(null)";
  if (arg->precision >= 0)
    {
      for (n=0,s=string; *s && n < arg->precision; s++) 
        n++;
    }
  else
    n = strlen (string);

  if (!(arg->flags & FLAG_LEFT_JUST)
      && arg->width >= 0 && arg->width > n )
    {
      rc = pad_out (outfnc, outfncarg, ' ', arg->width - n, nbytes);
      if (rc)
        return rc;
    }

  rc = outfnc (outfncarg, string, n);
  if (rc)
    return rc;
  *nbytes += n;

  if ((arg->flags & FLAG_LEFT_JUST)
      && arg->width >= 0 && arg->width > n)
    {
      rc = pad_out (outfnc, outfncarg, ' ', arg->width - n, nbytes);
      if (rc)
        return rc;
    }
  
  return 0;
}


/* "p" formatting.  */
static int
pr_pointer (estream_printf_out_t outfnc, void *outfncarg,
            argspec_t arg, value_t value, size_t *nbytes)
{
  int rc;
#ifdef HAVE_LONG_LONG_INT
  unsigned long long aulong;
#else
  unsigned long aulong;
#endif
  char numbuf[100];
  char *p, *pend;

  if (arg->vt != VALTYPE_POINTER)
    return -1;
  /* We assume that a pointer can be converted to an unsigned long.
     That is not correct for a 64 bit Windows, but then we assume that
     long long is supported and usable for storing a pointer.  */
#if defined(HAVE_LONG_LONG_INT) && (SIZEOF_UNSIGNED_LONG < SIZEOF_VOID_P)
  aulong = (unsigned long long)value.a_void_ptr;
#else
  aulong = (unsigned long)value.a_void_ptr;
#endif

  p = pend = numbuf + DIM(numbuf);
  do
    {
      *--p = "0123456789abcdefx"[(aulong % 16)];
      aulong /= 16;
    }
  while (aulong);
  while ((pend-p) < 2*sizeof (aulong))
    *--p = '0';
  *--p = 'x';
  *--p = '0';
  
  rc = outfnc (outfncarg, p, pend - p);
  if (rc)
    return rc;
  *nbytes += pend - p;

  return 0;
}

/* "n" pesudo format operation.  */
static int
pr_bytes_so_far (estream_printf_out_t outfnc, void *outfncarg,
                 argspec_t arg, value_t value, size_t *nbytes)
{
  (void)outfnc;
  (void)outfncarg;

  switch (arg->vt)
    {
    case VALTYPE_SCHAR_PTR: 
      *value.a_schar_ptr = (signed char)(unsigned int)(*nbytes); 
      break;
    case VALTYPE_SHORT_PTR:  
      *value.a_short_ptr = (short)(unsigned int)(*nbytes);
      break;
    case VALTYPE_LONG_PTR:     
      *value.a_long_ptr = (long)(*nbytes); 
      break;
#ifdef HAVE_LONG_LONG_INT
    case VALTYPE_LONGLONG_PTR:
      *value.a_longlong_ptr = (long long)(*nbytes);
      break;
#endif
#ifdef HAVE_INTMAX_T
    case VALTYPE_INTMAX_PTR:   
      *value.a_intmax_ptr = (intmax_t)(*nbytes);
      break;
#endif
    case VALTYPE_SIZE_PTR:
      *value.a_size_ptr = (*nbytes); 
      break;
#ifdef HAVE_PTRDIFF_T
    case VALTYPE_PTRDIFF_PTR:
      *value.a_ptrdiff_ptr = (ptrdiff_t)(*nbytes);
      break;
#endif
    case VALTYPE_INT_PTR:
      *value.a_int_ptr = (int)(*nbytes);
      break;
    default:
      return -1; /* An unsupported type has been used.  */
    }

  return 0;
}



/* Run the actual formatting.  OUTFNC and OUTFNCARG are the output
   functions.  FORMAT is format string ARGSPECS is the parsed format
   string, ARGSPECS_LEN the number of items in ARGSPECS.  VALUETABLE
   holds the values and may be directly addressed using the position
   arguments given by ARGSPECS.  MYERRNO is used for the "%m"
   conversion. NBYTES well be updated to reflect the number of bytes
   send to the output function. */ 
static int 
do_format (estream_printf_out_t outfnc, void *outfncarg,
           const char *format, argspec_t argspecs, size_t argspecs_len,
           valueitem_t valuetable, int myerrno, size_t *nbytes)
{
  int rc = 0;
  const char *s;
  argspec_t arg = argspecs;
  int argidx = 0; /* Only used for assertion.  */
  size_t n;
  value_t value;

  s = format;
  while ( *s )
    {
      if (*s != '%')
        {
          s++;
          continue;
        }
      if (s != format)
        {
          rc = outfnc (outfncarg, format, (n=s-format));
          if (rc)
            return rc;
          *nbytes += n;
        }
      if (s[1] == '%')
        {
          /* Note that this code ignores one trailing percent escape -
             this is however okay as the args parser must have
             detected this already.  */
          rc = outfnc (outfncarg, s, 1);
          if (rc)
            return rc;
          *nbytes += 1;
          s += 2;
          format = s;
          continue;
        }

      /* Save the next start.  */
      s += arg->length;
      format = s;
 
      assert (argidx < argspecs_len);
      argidx++;

      /* Apply indirect field width and precision values.  */
      if (arg->width == STAR_FIELD_VALUE)
        {
          assert (valuetable[arg->width_pos-1].vt == VALTYPE_INT);
          arg->width = valuetable[arg->width_pos-1].value.a_int;
          if (arg->width < 0)
            {
              arg->width = -arg->width;
              arg->flags |= FLAG_LEFT_JUST;
            }
        }
      if (arg->precision == STAR_FIELD_VALUE)
        {
          assert (valuetable[arg->precision_pos-1].vt == VALTYPE_INT);
          arg->precision = valuetable[arg->precision_pos-1].value.a_int;
          if (arg->precision < 0)
            arg->precision = NO_FIELD_VALUE;
        }

      if (arg->arg_pos == -1 && arg->conspec == CONSPEC_STRERROR)
        value.a_string = strerror (myerrno);
      else
        {
          assert (arg->vt == valuetable[arg->arg_pos-1].vt);
          value = valuetable[arg->arg_pos-1].value;
        }

      switch (arg->conspec)
        {
        case CONSPEC_UNKNOWN: assert (!"bug"); break;

        case CONSPEC_DECIMAL:
        case CONSPEC_UNSIGNED:
        case CONSPEC_OCTAL:
        case CONSPEC_HEX:
        case CONSPEC_HEX_UP:
          rc = pr_integer (outfnc, outfncarg, arg, value, nbytes);
          break;
        case CONSPEC_FLOAT:
        case CONSPEC_FLOAT_UP:
        case CONSPEC_EXP:
        case CONSPEC_EXP_UP:
        case CONSPEC_F_OR_G:
        case CONSPEC_F_OR_G_UP:
        case CONSPEC_HEX_EXP:
        case CONSPEC_HEX_EXP_UP:
          rc = pr_float (outfnc, outfncarg, arg, value, nbytes);
          break;
        case CONSPEC_CHAR:
          rc = pr_char (outfnc, outfncarg, arg, value, nbytes);
          break;
        case CONSPEC_STRING:
        case CONSPEC_STRERROR:
          rc = pr_string (outfnc, outfncarg, arg, value, nbytes);
          break;
        case CONSPEC_POINTER:
          rc = pr_pointer (outfnc, outfncarg, arg, value, nbytes);
          break;
        case CONSPEC_BYTES_SO_FAR:
          rc = pr_bytes_so_far (outfnc, outfncarg, arg, value, nbytes);
          break;
        }
      if (rc)
        return rc;
      arg++;    
    }
  
  /* Print out any trailing stuff. */
  n = s - format;
  rc = n? outfnc (outfncarg, format, n) : 0;
  if (!rc)
    *nbytes += n;

  return rc;
}




/* The versatile printf formatting routine.  It expects a callback
   function OUTFNC and an opaque argument OUTFNCARG used for actual
   output of the formatted stuff.  FORMAT is the format specification
   and VAARGS a variable argumemt list matching the arguments of
   FORMAT.  */
int 
estream_format (estream_printf_out_t outfnc,
                void *outfncarg,
                const char *format, va_list vaargs)
{
  /* Buffer to hold the argspecs and a pointer to it.*/
  struct argspec_s argspecs_buffer[DEFAULT_MAX_ARGSPECS];
  argspec_t argspecs = argspecs_buffer;
  size_t argspecs_len;  /* Number of specifications in ARGSPECS.  */

  /* Buffer to hold the description for the values.  */
  struct valueitem_s valuetable_buffer[DEFAULT_MAX_VALUES];
  valueitem_t valuetable = valuetable_buffer;

  int rc;     /* Return code. */
  size_t argidx; /* Used to index the argspecs array.  */
  size_t validx; /* Used to index the valuetable.  */
  int max_pos;/* Highest argument position.  */

  size_t nbytes = 0; /* Keep track of the number of bytes passed to
                        the output function.  */

  int myerrno = errno; /* Save the errno for use with "%m". */


  /* Parse the arguments to come up with descriptive list.  We can't
     do this on the fly because we need to support positional
     arguments. */
  rc = parse_format (format, &argspecs, DIM(argspecs_buffer), &argspecs_len);
  if (rc)
    goto leave;

  /* Check that all ARG_POS fields are set.  */
  for (argidx=0,max_pos=0; argidx < argspecs_len; argidx++)
    {
      if (argspecs[argidx].arg_pos != -1 
          && argspecs[argidx].arg_pos > max_pos)
        max_pos = argspecs[argidx].arg_pos;
      if (argspecs[argidx].width_pos > max_pos)
        max_pos = argspecs[argidx].width_pos;
      if (argspecs[argidx].precision_pos > max_pos)
        max_pos = argspecs[argidx].precision_pos;
    }
  if (!max_pos)
    {
      /* Fill in all the positions.  */
      for (argidx=0; argidx < argspecs_len; argidx++)
        {
          if (argspecs[argidx].width == STAR_FIELD_VALUE)
            argspecs[argidx].width_pos = ++max_pos;
          if (argspecs[argidx].precision == STAR_FIELD_VALUE)
            argspecs[argidx].precision_pos = ++max_pos;
          if (argspecs[argidx].arg_pos != -1 )
            argspecs[argidx].arg_pos = ++max_pos;
        }
    }
  else
    {
      /* Check that they are all filled.   More test are done later.  */
      for (argidx=0; argidx < argspecs_len; argidx++)
        {
          if (!argspecs[argidx].arg_pos
              || (argspecs[argidx].width == STAR_FIELD_VALUE
                  && !argspecs[argidx].width_pos)
              || (argspecs[argidx].precision == STAR_FIELD_VALUE
                  && !argspecs[argidx].precision_pos))
            goto leave_einval;
        }
    }
  /* Check that there is no overflow in max_pos and that it has a
     reasonable length.  There may never be more elements than the
     number of characters in FORMAT.  */
  if (max_pos < 0 || max_pos >= strlen (format))
    goto leave_einval;

#ifdef DEBUG
    dump_argspecs (argspecs, argspecs_len);
#endif

  /* Allocate a table to hold the values.  If it is small enough we
     use a stack allocated buffer.  */
  if (max_pos > DIM(valuetable_buffer))
    {
      valuetable = calloc (max_pos, sizeof *valuetable);
      if (!valuetable)
        goto leave_error;
    }
  else
    {
      for (validx=0; validx < DIM(valuetable_buffer); validx++)
        valuetable[validx].vt = VALTYPE_UNSUPPORTED;
    }
  for (argidx=0; argidx < argspecs_len; argidx++)
    {
      if (argspecs[argidx].arg_pos != - 1)
        {
          validx = argspecs[argidx].arg_pos - 1;
          if (valuetable[validx].vt)
            goto leave_einval; /* Already defined. */
          valuetable[validx].vt = argspecs[argidx].vt;
        }
      if (argspecs[argidx].width == STAR_FIELD_VALUE)
        {
          validx = argspecs[argidx].width_pos - 1;
          if (valuetable[validx].vt)
            goto leave_einval; /* Already defined.  */
          valuetable[validx].vt = VALTYPE_INT;
        }
      if (argspecs[argidx].precision == STAR_FIELD_VALUE)
        {
          validx = argspecs[argidx].precision_pos - 1;
          if (valuetable[validx].vt)
            goto leave_einval; /* Already defined.  */
          valuetable[validx].vt = VALTYPE_INT;
        }
    }
  
  /* Read all the arguments.  This will error out for unsupported
     types and for not given positional arguments. */
  rc = read_values (valuetable, max_pos, vaargs);
  if (rc)
    goto leave_einval;  
  
/*   for (validx=0; validx < max_pos; validx++) */
/*     fprintf (stderr, "%2d: vt=%d\n", validx, valuetable[validx].vt); */

  /* Everything has been collected, go ahead with the formatting.  */
  rc = do_format (outfnc, outfncarg, format,
                  argspecs, argspecs_len, valuetable, myerrno, &nbytes);

  goto leave;
  
 leave_einval:
  _set_errno (EINVAL);
 leave_error:
  rc = -1;
 leave:
  if (valuetable != valuetable_buffer)
    free (valuetable);
  if (argspecs != argspecs_buffer)
    free (argspecs);
  return rc;
}




/* A simple output handler utilizing stdio.  */
static int
plain_stdio_out (void *outfncarg, const char *buf, size_t buflen)
{
  FILE *fp = (FILE*)outfncarg;

  if ( fwrite (buf, buflen, 1, fp) != 1 )
    return -1;
  return 0;
}


/* A replacement for printf.  */
int
estream_printf (const char *format, ...)
{
  int rc;
  va_list arg_ptr;
  
  va_start (arg_ptr, format);
  rc = estream_format (plain_stdio_out, stderr, format, arg_ptr);
  va_end (arg_ptr);
  
  return rc;
}

/* A replacement for fprintf.  */
int
estream_fprintf (FILE *fp, const char *format, ...)
{
  int rc;
  va_list arg_ptr;
  
  va_start (arg_ptr, format);
  rc = estream_format (plain_stdio_out, fp, format, arg_ptr);
  va_end (arg_ptr);
  
  return rc;
}

/* A replacement for vfprintf.  */
int 
estream_vfprintf (FILE *fp, const char *format, va_list arg_ptr)
{
  return estream_format (plain_stdio_out, fp, format, arg_ptr);
}



/* Communication object used between estream_snprintf and
   fixed_buffer_out.  */
struct fixed_buffer_parm_s
{
  size_t size;    /* Size of the buffer.  */
  size_t count;   /* Number of bytes requested for output.  */
  size_t used;    /* Used size of the buffer.  */
  char *buffer;   /* Provided buffer.  */
};

/* A simple malloced buffer output handler.  */
static int
fixed_buffer_out (void *outfncarg, const char *buf, size_t buflen)
{
  struct fixed_buffer_parm_s *parm = outfncarg;

  parm->count += buflen;

  if (!parm->buffer)
    ;
  else if (parm->used + buflen < parm->size)
    {
      /* Handle the common case that everything fits into the buffer
         separately.  */
      memcpy (parm->buffer + parm->used, buf, buflen);
      parm->used += buflen;
    }
  else
    {
      /* The slow version of above.  */
      for ( ;buflen && parm->used < parm->size; buflen--)
        parm->buffer[parm->used++] = *buf++;
    }

  return 0;
}


/* A replacement for vsnprintf. */
int 
estream_vsnprintf (char *buf, size_t bufsize,
                   const char *format, va_list arg_ptr)
{
  struct fixed_buffer_parm_s parm;
  int rc;

  parm.size = bufsize;
  parm.count = 0;
  parm.used = 0;
  parm.buffer = bufsize?buf:NULL;
  rc = estream_format (fixed_buffer_out, &parm, format, arg_ptr);
  if (!rc)
    rc = fixed_buffer_out (&parm, "", 1); /* Print terminating Nul.  */
  if (rc == -1)
    return -1;
  if (bufsize && buf && parm.size && parm.count >= parm.size)
    buf[parm.size-1] = 0;

  parm.count--; /* Do not count the trailing nul.  */
  return (int)parm.count; /* Return number of bytes which would have
                             been written.  */
}

/* A replacement for snprintf.  */
int 
estream_snprintf (char *buf, size_t bufsize, const char *format, ...)
{
  int rc;
  va_list arg_ptr;

  va_start (arg_ptr, format);
  rc = estream_vsnprintf (buf, bufsize, format, arg_ptr);
  va_end (arg_ptr);
    
  return rc;
}



/* Communication object used between estream_asprintf and
   dynamic_buffer_out.  */
struct dynamic_buffer_parm_s
{
  int error_flag; /* Internal helper.  */ 
  size_t alloced; /* Allocated size of the buffer.  */
  size_t used;    /* Used size of the buffer.  */
  char *buffer;   /* Malloced buffer.  */
};

/* A simple malloced buffer output handler.  */
static int
dynamic_buffer_out (void *outfncarg, const char *buf, size_t buflen)
{
  struct dynamic_buffer_parm_s *parm = outfncarg;

  if (parm->error_flag)
    {
      /* Just in case some formatting routine did not checked for an
         error. */
      _set_errno (parm->error_flag);
      return -1;
    }

  if (parm->used + buflen >= parm->alloced)
    {
      char *p;
      
      parm->alloced += buflen + 512;
      p = realloc (parm->buffer, parm->alloced);
      if (!p)
        {
          parm->error_flag = errno ? errno : ENOMEM;
          /* Wipe out what we already accumulated.  This is useful in
             case sensitive data is formated.  */
          memset (parm->buffer, 0, parm->used);
          return -1;
        }
      parm->buffer = p;
    }
  memcpy (parm->buffer + parm->used, buf, buflen);
  parm->used += buflen;

  return 0;
}


/* A replacement for vasprintf.  As with the BSD of vasprintf version -1
   will be returned on error and NULL stored at BUFP.  On success the
   number of bytes printed will be returned. */
int 
estream_vasprintf (char **bufp, const char *format, va_list arg_ptr)
{
  struct dynamic_buffer_parm_s parm;
  int rc;

  parm.error_flag = 0;
  parm.alloced = 512;
  parm.used = 0;
  parm.buffer = my_printf_malloc (parm.alloced);
  if (!parm.buffer)
    {
      *bufp = NULL;
      return -1;
    }
  
  rc = estream_format (dynamic_buffer_out, &parm, format, arg_ptr);
  if (!rc)
    rc = dynamic_buffer_out (&parm, "", 1); /* Print terminating Nul.  */
  /* Fixme: Should we shrink the resulting buffer?  */
  if (rc != -1 && parm.error_flag)
    {
      rc = -1;
      _set_errno (parm.error_flag);
    }
  if (rc == -1)
    {
      memset (parm.buffer, 0, parm.used);
      my_printf_free (parm.buffer);
      *bufp = NULL;
      return -1;
    }
  assert (parm.used);   /* We have at least the terminating Nul.  */
  *bufp = parm.buffer;
  return parm.used - 1; /* Do not include that Nul. */
}

/* A replacement for asprintf.  As with the BSD of asprintf version -1
   will be returned on error and NULL stored at BUFP.  On success the
   number of bytes printed will be returned. */
int 
estream_asprintf (char **bufp, const char *format, ...)
{
  int rc;
  va_list arg_ptr;

  va_start (arg_ptr, format);
  rc = estream_vasprintf (bufp, format, arg_ptr);
  va_end (arg_ptr);
    
  return rc;
}