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
;
209 #if __GLIBC__ >= 2 && !defined __UCLIBC__
212 dp
->flags
|= FLAG_LOCALIZED
;
220 /* Parse the field width. */
223 dp
->width_start
= cp
;
226 if (max_width_length
< 1)
227 max_width_length
= 1;
229 /* Test for positional argument. */
230 if (*cp
>= '0' && *cp
<= '9')
234 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
240 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
241 n
= xsum (xtimes (n
, 10), *np
- '0');
243 /* Positional argument 0. */
245 if (size_overflow_p (n
))
246 /* n too large, would lead to out of memory later. */
248 dp
->width_arg_index
= n
- 1;
252 if (dp
->width_arg_index
== ARG_NONE
)
254 dp
->width_arg_index
= arg_posn
++;
255 if (dp
->width_arg_index
== ARG_NONE
)
256 /* arg_posn wrapped around. */
259 REGISTER_ARG (dp
->width_arg_index
, TYPE_INT
);
261 else if (*cp
>= '0' && *cp
<= '9')
265 dp
->width_start
= cp
;
266 for (; *cp
>= '0' && *cp
<= '9'; cp
++)
269 width_length
= dp
->width_end
- dp
->width_start
;
270 if (max_width_length
< width_length
)
271 max_width_length
= width_length
;
274 /* Parse the precision. */
280 dp
->precision_start
= cp
- 1;
282 dp
->precision_end
= cp
;
283 if (max_precision_length
< 2)
284 max_precision_length
= 2;
286 /* Test for positional argument. */
287 if (*cp
>= '0' && *cp
<= '9')
291 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
297 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
298 n
= xsum (xtimes (n
, 10), *np
- '0');
300 /* Positional argument 0. */
302 if (size_overflow_p (n
))
303 /* n too large, would lead to out of memory
306 dp
->precision_arg_index
= n
- 1;
310 if (dp
->precision_arg_index
== ARG_NONE
)
312 dp
->precision_arg_index
= arg_posn
++;
313 if (dp
->precision_arg_index
== ARG_NONE
)
314 /* arg_posn wrapped around. */
317 REGISTER_ARG (dp
->precision_arg_index
, TYPE_INT
);
321 size_t precision_length
;
323 dp
->precision_start
= cp
- 1;
324 for (; *cp
>= '0' && *cp
<= '9'; cp
++)
326 dp
->precision_end
= cp
;
327 precision_length
= dp
->precision_end
- dp
->precision_start
;
328 if (max_precision_length
< precision_length
)
329 max_precision_length
= precision_length
;
336 /* Parse argument type/size specifiers. */
344 flags
|= (1 << (flags
& 1));
359 if (sizeof (intmax_t) > sizeof (long))
361 /* intmax_t = long long */
364 else if (sizeof (intmax_t) > sizeof (int))
366 /* intmax_t = long */
371 else if (*cp
== 'z' || *cp
== 'Z')
373 /* 'z' is standardized in ISO C 99, but glibc uses 'Z'
374 because the warning facility in gcc-2.95.2 understands
375 only 'Z' (see gcc-2.95.2/gcc/c-common.c:1784). */
376 if (sizeof (size_t) > sizeof (long))
378 /* size_t = long long */
381 else if (sizeof (size_t) > sizeof (int))
390 if (sizeof (ptrdiff_t) > sizeof (long))
392 /* ptrdiff_t = long long */
395 else if (sizeof (ptrdiff_t) > sizeof (int))
397 /* ptrdiff_t = long */
402 #if defined __APPLE__ && defined __MACH__
403 /* On MacOS X 10.3, PRIdMAX is defined as "qd".
404 We cannot change it to "lld" because PRIdMAX must also
405 be understood by the system's printf routines. */
408 if (64 / 8 > sizeof (long))
410 /* int64_t = long long */
421 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
422 /* On native Win32, PRIdMAX is defined as "I64d".
423 We cannot change it to "lld" because PRIdMAX must also
424 be understood by the system's printf routines. */
425 else if (*cp
== 'I' && cp
[1] == '6' && cp
[2] == '4')
427 if (64 / 8 > sizeof (long))
429 /* __int64 = long long */
444 /* Read the conversion character. */
449 #if HAVE_LONG_LONG_INT
450 /* If 'long long' exists and is larger than 'long': */
451 if (flags
>= 16 || (flags
& 4))
452 type
= TYPE_LONGLONGINT
;
455 /* If 'long long' exists and is the same as 'long', we parse
456 "lld" into TYPE_LONGINT. */
466 case 'o': case 'u': case 'x': case 'X':
467 #if HAVE_LONG_LONG_INT
468 /* If 'long long' exists and is larger than 'long': */
469 if (flags
>= 16 || (flags
& 4))
470 type
= TYPE_ULONGLONGINT
;
473 /* If 'unsigned long long' exists and is the same as
474 'unsigned long', we parse "llu" into TYPE_ULONGINT. */
476 type
= TYPE_ULONGINT
;
484 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
486 if (flags
>= 16 || (flags
& 4))
487 type
= TYPE_LONGDOUBLE
;
494 type
= TYPE_WIDE_CHAR
;
503 type
= TYPE_WIDE_CHAR
;
510 type
= TYPE_WIDE_STRING
;
519 type
= TYPE_WIDE_STRING
;
527 #if HAVE_LONG_LONG_INT
528 /* If 'long long' exists and is larger than 'long': */
529 if (flags
>= 16 || (flags
& 4))
530 type
= TYPE_COUNT_LONGLONGINT_POINTER
;
533 /* If 'long long' exists and is the same as 'long', we parse
534 "lln" into TYPE_COUNT_LONGINT_POINTER. */
536 type
= TYPE_COUNT_LONGINT_POINTER
;
538 type
= TYPE_COUNT_SCHAR_POINTER
;
540 type
= TYPE_COUNT_SHORT_POINTER
;
542 type
= TYPE_COUNT_INT_POINTER
;
545 /* The unistdio extensions. */
548 type
= TYPE_U32_STRING
;
550 type
= TYPE_U16_STRING
;
552 type
= TYPE_U8_STRING
;
559 /* Unknown conversion character. */
564 if (type
!= TYPE_NONE
)
566 dp
->arg_index
= arg_index
;
567 if (dp
->arg_index
== ARG_NONE
)
569 dp
->arg_index
= arg_posn
++;
570 if (dp
->arg_index
== ARG_NONE
)
571 /* arg_posn wrapped around. */
574 REGISTER_ARG (dp
->arg_index
, type
);
581 if (d
->count
>= d_allocated
)
586 d_allocated
= xtimes (d_allocated
, 2);
587 memory_size
= xtimes (d_allocated
, sizeof (DIRECTIVE
));
588 if (size_overflow_p (memory_size
))
589 /* Overflow, would lead to out of memory. */
591 memory
= (DIRECTIVE
*) realloc (d
->dir
, memory_size
);
598 #if CHAR_T_ONLY_ASCII
599 else if (!c_isascii (c
))
601 /* Non-ASCII character. Not supported. */
606 d
->dir
[d
->count
].dir_start
= cp
;
608 d
->max_width_length
= max_width_length
;
609 d
->max_precision_length
= max_precision_length
;
633 #undef CHAR_T_ONLY_ASCII