1 /* Formatted output to strings.
2 Copyright (C) 1999-2000, 2002-2003, 2006-2015 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 2, 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, see <http://www.gnu.org/licenses/>. */
17 /* This file can be parametrized with the following macros:
18 CHAR_T The element type of the format string.
19 CHAR_T_ONLY_ASCII Set to 1 to enable verification that all characters
20 in the format string are ASCII.
21 DIRECTIVE Structure denoting a format directive.
23 DIRECTIVES Structure denoting the set of format directives of a
24 format string. Depends on CHAR_T.
25 PRINTF_PARSE Function that parses a format string.
27 STATIC Set to 'static' to declare the function static.
28 ENABLE_UNISTDIO Set to 1 to enable the unistdio extensions. */
38 # include "printf-parse.h"
43 /* Default parameters. */
45 # define PRINTF_PARSE printf_parse
47 # define DIRECTIVE char_directive
48 # define DIRECTIVES char_directives
51 /* Get size_t, NULL. */
55 #if defined IN_LIBINTL || defined IN_LIBASPRINTF
56 # if HAVE_STDINT_H_WITH_UINTMAX
59 # if HAVE_INTTYPES_H_WITH_UINTMAX
60 # include <inttypes.h>
63 # if !defined (_MSC_VER) || (_MSC_VER >= 1600)
66 typedef signed __int64
intmax_t;
70 /* malloc(), realloc(), free(). */
88 PRINTF_PARSE (const CHAR_T
*format
, DIRECTIVES
*d
, arguments
*a
)
90 const CHAR_T
*cp
= format
; /* pointer into format */
91 size_t arg_posn
= 0; /* number of regular arguments consumed */
92 size_t d_allocated
; /* allocated elements of d->dir */
93 size_t a_allocated
; /* allocated elements of a->arg */
94 size_t max_width_length
= 0;
95 size_t max_precision_length
= 0;
98 d_allocated
= N_DIRECT_ALLOC_DIRECTIVES
;
99 d
->dir
= d
->direct_alloc_dir
;
102 a_allocated
= N_DIRECT_ALLOC_ARGUMENTS
;
103 a
->arg
= a
->direct_alloc_arg
;
105 #define REGISTER_ARG(_index_,_type_) \
107 size_t n = (_index_); \
108 if (n >= a_allocated) \
110 size_t memory_size; \
113 a_allocated = xtimes (a_allocated, 2); \
114 if (a_allocated <= n) \
115 a_allocated = xsum (n, 1); \
116 memory_size = xtimes (a_allocated, sizeof (argument)); \
117 if (size_overflow_p (memory_size)) \
118 /* Overflow, would lead to out of memory. */ \
119 goto out_of_memory; \
120 memory = (argument *) (a->arg != a->direct_alloc_arg \
121 ? realloc (a->arg, memory_size) \
122 : malloc (memory_size)); \
123 if (memory == NULL) \
124 /* Out of memory. */ \
125 goto out_of_memory; \
126 if (a->arg == a->direct_alloc_arg) \
127 memcpy (memory, a->arg, a->count * sizeof (argument)); \
130 while (a->count <= n) \
131 a->arg[a->count++].type = TYPE_NONE; \
132 if (a->arg[n].type == TYPE_NONE) \
133 a->arg[n].type = (_type_); \
134 else if (a->arg[n].type != (_type_)) \
135 /* Ambiguous type for positional argument. */ \
144 size_t arg_index
= ARG_NONE
;
145 DIRECTIVE
*dp
= &d
->dir
[d
->count
]; /* pointer to next directive */
147 /* Initialize the next directive. */
148 dp
->dir_start
= cp
- 1;
150 dp
->width_start
= NULL
;
151 dp
->width_end
= NULL
;
152 dp
->width_arg_index
= ARG_NONE
;
153 dp
->precision_start
= NULL
;
154 dp
->precision_end
= NULL
;
155 dp
->precision_arg_index
= ARG_NONE
;
156 dp
->arg_index
= ARG_NONE
;
158 /* Test for positional argument. */
159 if (*cp
>= '0' && *cp
<= '9')
163 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
169 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
170 n
= xsum (xtimes (n
, 10), *np
- '0');
172 /* Positional argument 0. */
174 if (size_overflow_p (n
))
175 /* n too large, would lead to out of memory later. */
182 /* Read the flags. */
187 dp
->flags
|= FLAG_GROUP
;
192 dp
->flags
|= FLAG_LEFT
;
197 dp
->flags
|= FLAG_SHOWSIGN
;
202 dp
->flags
|= FLAG_SPACE
;
207 dp
->flags
|= FLAG_ALT
;
212 dp
->flags
|= FLAG_ZERO
;
215 #if __GLIBC__ >= 2 && !defined __UCLIBC__
218 dp
->flags
|= FLAG_LOCALIZED
;
226 /* Parse the field width. */
229 dp
->width_start
= cp
;
232 if (max_width_length
< 1)
233 max_width_length
= 1;
235 /* Test for positional argument. */
236 if (*cp
>= '0' && *cp
<= '9')
240 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
246 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
247 n
= xsum (xtimes (n
, 10), *np
- '0');
249 /* Positional argument 0. */
251 if (size_overflow_p (n
))
252 /* n too large, would lead to out of memory later. */
254 dp
->width_arg_index
= n
- 1;
258 if (dp
->width_arg_index
== ARG_NONE
)
260 dp
->width_arg_index
= arg_posn
++;
261 if (dp
->width_arg_index
== ARG_NONE
)
262 /* arg_posn wrapped around. */
265 REGISTER_ARG (dp
->width_arg_index
, TYPE_INT
);
267 else if (*cp
>= '0' && *cp
<= '9')
271 dp
->width_start
= cp
;
272 for (; *cp
>= '0' && *cp
<= '9'; cp
++)
275 width_length
= dp
->width_end
- dp
->width_start
;
276 if (max_width_length
< width_length
)
277 max_width_length
= width_length
;
280 /* Parse the precision. */
286 dp
->precision_start
= cp
- 1;
288 dp
->precision_end
= cp
;
289 if (max_precision_length
< 2)
290 max_precision_length
= 2;
292 /* Test for positional argument. */
293 if (*cp
>= '0' && *cp
<= '9')
297 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
303 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
304 n
= xsum (xtimes (n
, 10), *np
- '0');
306 /* Positional argument 0. */
308 if (size_overflow_p (n
))
309 /* n too large, would lead to out of memory
312 dp
->precision_arg_index
= n
- 1;
316 if (dp
->precision_arg_index
== ARG_NONE
)
318 dp
->precision_arg_index
= arg_posn
++;
319 if (dp
->precision_arg_index
== ARG_NONE
)
320 /* arg_posn wrapped around. */
323 REGISTER_ARG (dp
->precision_arg_index
, TYPE_INT
);
327 size_t precision_length
;
329 dp
->precision_start
= cp
- 1;
330 for (; *cp
>= '0' && *cp
<= '9'; cp
++)
332 dp
->precision_end
= cp
;
333 precision_length
= dp
->precision_end
- dp
->precision_start
;
334 if (max_precision_length
< precision_length
)
335 max_precision_length
= precision_length
;
342 /* Parse argument type/size specifiers. */
350 flags
|= (1 << (flags
& 1));
365 if (sizeof (intmax_t) > sizeof (long))
367 /* intmax_t = long long */
370 else if (sizeof (intmax_t) > sizeof (int))
372 /* intmax_t = long */
377 else if (*cp
== 'z' || *cp
== 'Z')
379 /* 'z' is standardized in ISO C 99, but glibc uses 'Z'
380 because the warning facility in gcc-2.95.2 understands
381 only 'Z' (see gcc-2.95.2/gcc/c-common.c:1784). */
382 if (sizeof (size_t) > sizeof (long))
384 /* size_t = long long */
387 else if (sizeof (size_t) > sizeof (int))
396 if (sizeof (ptrdiff_t) > sizeof (long))
398 /* ptrdiff_t = long long */
401 else if (sizeof (ptrdiff_t) > sizeof (int))
403 /* ptrdiff_t = long */
408 #if defined __APPLE__ && defined __MACH__
409 /* On Mac OS X 10.3, PRIdMAX is defined as "qd".
410 We cannot change it to "lld" because PRIdMAX must also
411 be understood by the system's printf routines. */
414 if (64 / 8 > sizeof (long))
416 /* int64_t = long long */
427 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
428 /* On native Windows, PRIdMAX is defined as "I64d".
429 We cannot change it to "lld" because PRIdMAX must also
430 be understood by the system's printf routines. */
431 else if (*cp
== 'I' && cp
[1] == '6' && cp
[2] == '4')
433 if (64 / 8 > sizeof (long))
435 /* __int64 = long long */
450 /* Read the conversion character. */
456 /* If 'long long' exists and is larger than 'long': */
457 if (flags
>= 16 || (flags
& 4))
458 type
= TYPE_LONGLONGINT
;
461 /* If 'long long' exists and is the same as 'long', we parse
462 "lld" into TYPE_LONGINT. */
472 case 'o': case 'u': case 'x': case 'X':
474 /* If 'long long' exists and is larger than 'long': */
475 if (flags
>= 16 || (flags
& 4))
476 type
= TYPE_ULONGLONGINT
;
479 /* If 'unsigned long long' exists and is the same as
480 'unsigned long', we parse "llu" into TYPE_ULONGINT. */
482 type
= TYPE_ULONGINT
;
490 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
492 if (flags
>= 16 || (flags
& 4))
493 type
= TYPE_LONGDOUBLE
;
500 type
= TYPE_WIDE_CHAR
;
509 type
= TYPE_WIDE_CHAR
;
516 type
= TYPE_WIDE_STRING
;
525 type
= TYPE_WIDE_STRING
;
534 /* If 'long long' exists and is larger than 'long': */
535 if (flags
>= 16 || (flags
& 4))
536 type
= TYPE_COUNT_LONGLONGINT_POINTER
;
539 /* If 'long long' exists and is the same as 'long', we parse
540 "lln" into TYPE_COUNT_LONGINT_POINTER. */
542 type
= TYPE_COUNT_LONGINT_POINTER
;
544 type
= TYPE_COUNT_SCHAR_POINTER
;
546 type
= TYPE_COUNT_SHORT_POINTER
;
548 type
= TYPE_COUNT_INT_POINTER
;
551 /* The unistdio extensions. */
554 type
= TYPE_U32_STRING
;
556 type
= TYPE_U16_STRING
;
558 type
= TYPE_U8_STRING
;
565 /* Unknown conversion character. */
570 if (type
!= TYPE_NONE
)
572 dp
->arg_index
= arg_index
;
573 if (dp
->arg_index
== ARG_NONE
)
575 dp
->arg_index
= arg_posn
++;
576 if (dp
->arg_index
== ARG_NONE
)
577 /* arg_posn wrapped around. */
580 REGISTER_ARG (dp
->arg_index
, type
);
587 if (d
->count
>= d_allocated
)
592 d_allocated
= xtimes (d_allocated
, 2);
593 memory_size
= xtimes (d_allocated
, sizeof (DIRECTIVE
));
594 if (size_overflow_p (memory_size
))
595 /* Overflow, would lead to out of memory. */
597 memory
= (DIRECTIVE
*) (d
->dir
!= d
->direct_alloc_dir
598 ? realloc (d
->dir
, memory_size
)
599 : malloc (memory_size
));
603 if (d
->dir
== d
->direct_alloc_dir
)
604 memcpy (memory
, d
->dir
, d
->count
* sizeof (DIRECTIVE
));
608 #if CHAR_T_ONLY_ASCII
609 else if (!c_isascii (c
))
611 /* Non-ASCII character. Not supported. */
616 d
->dir
[d
->count
].dir_start
= cp
;
618 d
->max_width_length
= max_width_length
;
619 d
->max_precision_length
= max_precision_length
;
623 if (a
->arg
!= a
->direct_alloc_arg
)
625 if (d
->dir
!= d
->direct_alloc_dir
)
631 if (a
->arg
!= a
->direct_alloc_arg
)
633 if (d
->dir
!= d
->direct_alloc_dir
)
642 #undef CHAR_T_ONLY_ASCII