1
/* Formatted output to strings.
2
Copyright (C) 1999-2000, 2002-2003, 2006-2010 Free Software Foundation, Inc.
4
This program is free software; you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation; either version 3, or (at your option)
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License along
15
with this program; if not, write to the Free Software Foundation,
16
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18
/* This file can be parametrized with the following macros:
19
CHAR_T The element type of the format string.
20
CHAR_T_ONLY_ASCII Set to 1 to enable verification that all characters
21
in the format string are ASCII.
22
DIRECTIVE Structure denoting a format directive.
24
DIRECTIVES Structure denoting the set of format directives of a
25
format string. Depends on CHAR_T.
26
PRINTF_PARSE Function that parses a format string.
28
STATIC Set to 'static' to declare the function static.
29
ENABLE_UNISTDIO Set to 1 to enable the unistdio extensions. */
37
# include "printf-parse.h"
40
/* Default parameters. */
42
# define PRINTF_PARSE printf_parse
44
# define DIRECTIVE char_directive
45
# define DIRECTIVES char_directives
48
/* Get size_t, NULL. */
52
#if defined IN_LIBINTL || defined IN_LIBASPRINTF
53
# if HAVE_STDINT_H_WITH_UINTMAX
56
# if HAVE_INTTYPES_H_WITH_UINTMAX
57
# include <inttypes.h>
63
/* malloc(), realloc(), free(). */
69
/* Checked size_t computations. */
81
PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a)
83
const CHAR_T *cp = format; /* pointer into format */
84
size_t arg_posn = 0; /* number of regular arguments consumed */
85
size_t d_allocated; /* allocated elements of d->dir */
86
size_t a_allocated; /* allocated elements of a->arg */
87
size_t max_width_length = 0;
88
size_t max_precision_length = 0;
92
d->dir = (DIRECTIVE *) malloc (d_allocated * sizeof (DIRECTIVE));
101
#define REGISTER_ARG(_index_,_type_) \
103
size_t n = (_index_); \
104
if (n >= a_allocated) \
106
size_t memory_size; \
109
a_allocated = xtimes (a_allocated, 2); \
110
if (a_allocated <= n) \
111
a_allocated = xsum (n, 1); \
112
memory_size = xtimes (a_allocated, sizeof (argument)); \
113
if (size_overflow_p (memory_size)) \
114
/* Overflow, would lead to out of memory. */ \
115
goto out_of_memory; \
116
memory = (argument *) (a->arg \
117
? realloc (a->arg, memory_size) \
118
: malloc (memory_size)); \
119
if (memory == NULL) \
120
/* Out of memory. */ \
121
goto out_of_memory; \
124
while (a->count <= n) \
125
a->arg[a->count++].type = TYPE_NONE; \
126
if (a->arg[n].type == TYPE_NONE) \
127
a->arg[n].type = (_type_); \
128
else if (a->arg[n].type != (_type_)) \
129
/* Ambiguous type for positional argument. */ \
138
size_t arg_index = ARG_NONE;
139
DIRECTIVE *dp = &d->dir[d->count]; /* pointer to next directive */
141
/* Initialize the next directive. */
142
dp->dir_start = cp - 1;
144
dp->width_start = NULL;
145
dp->width_end = NULL;
146
dp->width_arg_index = ARG_NONE;
147
dp->precision_start = NULL;
148
dp->precision_end = NULL;
149
dp->precision_arg_index = ARG_NONE;
150
dp->arg_index = ARG_NONE;
152
/* Test for positional argument. */
153
if (*cp >= '0' && *cp <= '9')
157
for (np = cp; *np >= '0' && *np <= '9'; np++)
163
for (np = cp; *np >= '0' && *np <= '9'; np++)
164
n = xsum (xtimes (n, 10), *np - '0');
166
/* Positional argument 0. */
168
if (size_overflow_p (n))
169
/* n too large, would lead to out of memory later. */
176
/* Read the flags. */
181
dp->flags |= FLAG_GROUP;
186
dp->flags |= FLAG_LEFT;
191
dp->flags |= FLAG_SHOWSIGN;
196
dp->flags |= FLAG_SPACE;
201
dp->flags |= FLAG_ALT;
206
dp->flags |= FLAG_ZERO;
213
/* Parse the field width. */
216
dp->width_start = cp;
219
if (max_width_length < 1)
220
max_width_length = 1;
222
/* Test for positional argument. */
223
if (*cp >= '0' && *cp <= '9')
227
for (np = cp; *np >= '0' && *np <= '9'; np++)
233
for (np = cp; *np >= '0' && *np <= '9'; np++)
234
n = xsum (xtimes (n, 10), *np - '0');
236
/* Positional argument 0. */
238
if (size_overflow_p (n))
239
/* n too large, would lead to out of memory later. */
241
dp->width_arg_index = n - 1;
245
if (dp->width_arg_index == ARG_NONE)
247
dp->width_arg_index = arg_posn++;
248
if (dp->width_arg_index == ARG_NONE)
249
/* arg_posn wrapped around. */
252
REGISTER_ARG (dp->width_arg_index, TYPE_INT);
254
else if (*cp >= '0' && *cp <= '9')
258
dp->width_start = cp;
259
for (; *cp >= '0' && *cp <= '9'; cp++)
262
width_length = dp->width_end - dp->width_start;
263
if (max_width_length < width_length)
264
max_width_length = width_length;
267
/* Parse the precision. */
273
dp->precision_start = cp - 1;
275
dp->precision_end = cp;
276
if (max_precision_length < 2)
277
max_precision_length = 2;
279
/* Test for positional argument. */
280
if (*cp >= '0' && *cp <= '9')
284
for (np = cp; *np >= '0' && *np <= '9'; np++)
290
for (np = cp; *np >= '0' && *np <= '9'; np++)
291
n = xsum (xtimes (n, 10), *np - '0');
293
/* Positional argument 0. */
295
if (size_overflow_p (n))
296
/* n too large, would lead to out of memory
299
dp->precision_arg_index = n - 1;
303
if (dp->precision_arg_index == ARG_NONE)
305
dp->precision_arg_index = arg_posn++;
306
if (dp->precision_arg_index == ARG_NONE)
307
/* arg_posn wrapped around. */
310
REGISTER_ARG (dp->precision_arg_index, TYPE_INT);
314
size_t precision_length;
316
dp->precision_start = cp - 1;
317
for (; *cp >= '0' && *cp <= '9'; cp++)
319
dp->precision_end = cp;
320
precision_length = dp->precision_end - dp->precision_start;
321
if (max_precision_length < precision_length)
322
max_precision_length = precision_length;
329
/* Parse argument type/size specifiers. */
337
flags |= (1 << (flags & 1));
352
if (sizeof (intmax_t) > sizeof (long))
354
/* intmax_t = long long */
357
else if (sizeof (intmax_t) > sizeof (int))
359
/* intmax_t = long */
364
else if (*cp == 'z' || *cp == 'Z')
366
/* 'z' is standardized in ISO C 99, but glibc uses 'Z'
367
because the warning facility in gcc-2.95.2 understands
368
only 'Z' (see gcc-2.95.2/gcc/c-common.c:1784). */
369
if (sizeof (size_t) > sizeof (long))
371
/* size_t = long long */
374
else if (sizeof (size_t) > sizeof (int))
383
if (sizeof (ptrdiff_t) > sizeof (long))
385
/* ptrdiff_t = long long */
388
else if (sizeof (ptrdiff_t) > sizeof (int))
390
/* ptrdiff_t = long */
395
#if defined __APPLE__ && defined __MACH__
396
/* On MacOS X 10.3, PRIdMAX is defined as "qd".
397
We cannot change it to "lld" because PRIdMAX must also
398
be understood by the system's printf routines. */
401
if (64 / 8 > sizeof (long))
403
/* int64_t = long long */
414
#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
415
/* On native Win32, PRIdMAX is defined as "I64d".
416
We cannot change it to "lld" because PRIdMAX must also
417
be understood by the system's printf routines. */
418
else if (*cp == 'I' && cp[1] == '6' && cp[2] == '4')
420
if (64 / 8 > sizeof (long))
422
/* __int64 = long long */
437
/* Read the conversion character. */
442
#if HAVE_LONG_LONG_INT
443
/* If 'long long' exists and is larger than 'long': */
444
if (flags >= 16 || (flags & 4))
445
type = TYPE_LONGLONGINT;
448
/* If 'long long' exists and is the same as 'long', we parse
449
"lld" into TYPE_LONGINT. */
459
case 'o': case 'u': case 'x': case 'X':
460
#if HAVE_LONG_LONG_INT
461
/* If 'long long' exists and is larger than 'long': */
462
if (flags >= 16 || (flags & 4))
463
type = TYPE_ULONGLONGINT;
466
/* If 'unsigned long long' exists and is the same as
467
'unsigned long', we parse "llu" into TYPE_ULONGINT. */
469
type = TYPE_ULONGINT;
477
case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
479
if (flags >= 16 || (flags & 4))
480
type = TYPE_LONGDOUBLE;
487
type = TYPE_WIDE_CHAR;
496
type = TYPE_WIDE_CHAR;
503
type = TYPE_WIDE_STRING;
512
type = TYPE_WIDE_STRING;
520
#if HAVE_LONG_LONG_INT
521
/* If 'long long' exists and is larger than 'long': */
522
if (flags >= 16 || (flags & 4))
523
type = TYPE_COUNT_LONGLONGINT_POINTER;
526
/* If 'long long' exists and is the same as 'long', we parse
527
"lln" into TYPE_COUNT_LONGINT_POINTER. */
529
type = TYPE_COUNT_LONGINT_POINTER;
531
type = TYPE_COUNT_SCHAR_POINTER;
533
type = TYPE_COUNT_SHORT_POINTER;
535
type = TYPE_COUNT_INT_POINTER;
538
/* The unistdio extensions. */
541
type = TYPE_U32_STRING;
543
type = TYPE_U16_STRING;
545
type = TYPE_U8_STRING;
552
/* Unknown conversion character. */
557
if (type != TYPE_NONE)
559
dp->arg_index = arg_index;
560
if (dp->arg_index == ARG_NONE)
562
dp->arg_index = arg_posn++;
563
if (dp->arg_index == ARG_NONE)
564
/* arg_posn wrapped around. */
567
REGISTER_ARG (dp->arg_index, type);
574
if (d->count >= d_allocated)
579
d_allocated = xtimes (d_allocated, 2);
580
memory_size = xtimes (d_allocated, sizeof (DIRECTIVE));
581
if (size_overflow_p (memory_size))
582
/* Overflow, would lead to out of memory. */
584
memory = (DIRECTIVE *) realloc (d->dir, memory_size);
591
#if CHAR_T_ONLY_ASCII
592
else if (!c_isascii (c))
594
/* Non-ASCII character. Not supported. */
599
d->dir[d->count].dir_start = cp;
601
d->max_width_length = max_width_length;
602
d->max_precision_length = max_precision_length;
626
#undef CHAR_T_ONLY_ASCII