1 /* Formatted output to strings.
2 Copyright (C) 1999-2000, 2002-2003, 2006-2007 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, 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. */
53 # if HAVE_STDINT_H_WITH_UINTMAX
56 # if HAVE_INTTYPES_H_WITH_UINTMAX
57 # include <inttypes.h>
63 /* malloc(), realloc(), free(). */
66 /* Checked size_t computations. */
78 PRINTF_PARSE (const CHAR_T
*format
, DIRECTIVES
*d
, arguments
*a
)
80 const CHAR_T
*cp
= format
; /* pointer into format */
81 size_t arg_posn
= 0; /* number of regular arguments consumed */
82 size_t d_allocated
; /* allocated elements of d->dir */
83 size_t a_allocated
; /* allocated elements of a->arg */
84 size_t max_width_length
= 0;
85 size_t max_precision_length
= 0;
89 d
->dir
= (DIRECTIVE
*) malloc (d_allocated
* sizeof (DIRECTIVE
));
98 #define REGISTER_ARG(_index_,_type_) \
100 size_t n = (_index_); \
101 if (n >= a_allocated) \
103 size_t memory_size; \
106 a_allocated = xtimes (a_allocated, 2); \
107 if (a_allocated <= n) \
108 a_allocated = xsum (n, 1); \
109 memory_size = xtimes (a_allocated, sizeof (argument)); \
110 if (size_overflow_p (memory_size)) \
111 /* Overflow, would lead to out of memory. */ \
113 memory = (argument *) (a->arg \
114 ? realloc (a->arg, memory_size) \
115 : malloc (memory_size)); \
116 if (memory == NULL) \
117 /* Out of memory. */ \
121 while (a->count <= n) \
122 a->arg[a->count++].type = TYPE_NONE; \
123 if (a->arg[n].type == TYPE_NONE) \
124 a->arg[n].type = (_type_); \
125 else if (a->arg[n].type != (_type_)) \
126 /* Ambiguous type for positional argument. */ \
135 size_t arg_index
= ARG_NONE
;
136 DIRECTIVE
*dp
= &d
->dir
[d
->count
]; /* pointer to next directive */
138 /* Initialize the next directive. */
139 dp
->dir_start
= cp
- 1;
141 dp
->width_start
= NULL
;
142 dp
->width_end
= NULL
;
143 dp
->width_arg_index
= ARG_NONE
;
144 dp
->precision_start
= NULL
;
145 dp
->precision_end
= NULL
;
146 dp
->precision_arg_index
= ARG_NONE
;
147 dp
->arg_index
= ARG_NONE
;
149 /* Test for positional argument. */
150 if (*cp
>= '0' && *cp
<= '9')
154 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
160 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
161 n
= xsum (xtimes (n
, 10), *np
- '0');
163 /* Positional argument 0. */
165 if (size_overflow_p (n
))
166 /* n too large, would lead to out of memory later. */
173 /* Read the flags. */
178 dp
->flags
|= FLAG_GROUP
;
183 dp
->flags
|= FLAG_LEFT
;
188 dp
->flags
|= FLAG_SHOWSIGN
;
193 dp
->flags
|= FLAG_SPACE
;
198 dp
->flags
|= FLAG_ALT
;
203 dp
->flags
|= FLAG_ZERO
;
210 /* Parse the field width. */
213 dp
->width_start
= cp
;
216 if (max_width_length
< 1)
217 max_width_length
= 1;
219 /* Test for positional argument. */
220 if (*cp
>= '0' && *cp
<= '9')
224 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
230 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
231 n
= xsum (xtimes (n
, 10), *np
- '0');
233 /* Positional argument 0. */
235 if (size_overflow_p (n
))
236 /* n too large, would lead to out of memory later. */
238 dp
->width_arg_index
= n
- 1;
242 if (dp
->width_arg_index
== ARG_NONE
)
244 dp
->width_arg_index
= arg_posn
++;
245 if (dp
->width_arg_index
== ARG_NONE
)
246 /* arg_posn wrapped around. */
249 REGISTER_ARG (dp
->width_arg_index
, TYPE_INT
);
251 else if (*cp
>= '0' && *cp
<= '9')
255 dp
->width_start
= cp
;
256 for (; *cp
>= '0' && *cp
<= '9'; cp
++)
259 width_length
= dp
->width_end
- dp
->width_start
;
260 if (max_width_length
< width_length
)
261 max_width_length
= width_length
;
264 /* Parse the precision. */
270 dp
->precision_start
= cp
- 1;
272 dp
->precision_end
= cp
;
273 if (max_precision_length
< 2)
274 max_precision_length
= 2;
276 /* Test for positional argument. */
277 if (*cp
>= '0' && *cp
<= '9')
281 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
287 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
288 n
= xsum (xtimes (n
, 10), *np
- '0');
290 /* Positional argument 0. */
292 if (size_overflow_p (n
))
293 /* n too large, would lead to out of memory
296 dp
->precision_arg_index
= n
- 1;
300 if (dp
->precision_arg_index
== ARG_NONE
)
302 dp
->precision_arg_index
= arg_posn
++;
303 if (dp
->precision_arg_index
== ARG_NONE
)
304 /* arg_posn wrapped around. */
307 REGISTER_ARG (dp
->precision_arg_index
, TYPE_INT
);
311 size_t precision_length
;
313 dp
->precision_start
= cp
- 1;
314 for (; *cp
>= '0' && *cp
<= '9'; cp
++)
316 dp
->precision_end
= cp
;
317 precision_length
= dp
->precision_end
- dp
->precision_start
;
318 if (max_precision_length
< precision_length
)
319 max_precision_length
= precision_length
;
326 /* Parse argument type/size specifiers. */
334 flags
|= (1 << (flags
& 1));
349 if (sizeof (intmax_t) > sizeof (long))
351 /* intmax_t = long long */
354 else if (sizeof (intmax_t) > sizeof (int))
356 /* intmax_t = long */
361 else if (*cp
== 'z' || *cp
== 'Z')
363 /* 'z' is standardized in ISO C 99, but glibc uses 'Z'
364 because the warning facility in gcc-2.95.2 understands
365 only 'Z' (see gcc-2.95.2/gcc/c-common.c:1784). */
366 if (sizeof (size_t) > sizeof (long))
368 /* size_t = long long */
371 else if (sizeof (size_t) > sizeof (int))
380 if (sizeof (ptrdiff_t) > sizeof (long))
382 /* ptrdiff_t = long long */
385 else if (sizeof (ptrdiff_t) > sizeof (int))
387 /* ptrdiff_t = long */
396 /* Read the conversion character. */
401 #if HAVE_LONG_LONG_INT
402 /* If 'long long' exists and is larger than 'long': */
403 if (flags
>= 16 || (flags
& 4))
404 type
= TYPE_LONGLONGINT
;
407 /* If 'long long' exists and is the same as 'long', we parse
408 "lld" into TYPE_LONGINT. */
418 case 'o': case 'u': case 'x': case 'X':
419 #if HAVE_LONG_LONG_INT
420 /* If 'long long' exists and is larger than 'long': */
421 if (flags
>= 16 || (flags
& 4))
422 type
= TYPE_ULONGLONGINT
;
425 /* If 'unsigned long long' exists and is the same as
426 'unsigned long', we parse "llu" into TYPE_ULONGINT. */
428 type
= TYPE_ULONGINT
;
436 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
438 if (flags
>= 16 || (flags
& 4))
439 type
= TYPE_LONGDOUBLE
;
446 type
= TYPE_WIDE_CHAR
;
455 type
= TYPE_WIDE_CHAR
;
462 type
= TYPE_WIDE_STRING
;
471 type
= TYPE_WIDE_STRING
;
479 #if HAVE_LONG_LONG_INT
480 /* If 'long long' exists and is larger than 'long': */
481 if (flags
>= 16 || (flags
& 4))
482 type
= TYPE_COUNT_LONGLONGINT_POINTER
;
485 /* If 'long long' exists and is the same as 'long', we parse
486 "lln" into TYPE_COUNT_LONGINT_POINTER. */
488 type
= TYPE_COUNT_LONGINT_POINTER
;
490 type
= TYPE_COUNT_SCHAR_POINTER
;
492 type
= TYPE_COUNT_SHORT_POINTER
;
494 type
= TYPE_COUNT_INT_POINTER
;
497 /* The unistdio extensions. */
500 type
= TYPE_U32_STRING
;
502 type
= TYPE_U16_STRING
;
504 type
= TYPE_U8_STRING
;
511 /* Unknown conversion character. */
516 if (type
!= TYPE_NONE
)
518 dp
->arg_index
= arg_index
;
519 if (dp
->arg_index
== ARG_NONE
)
521 dp
->arg_index
= arg_posn
++;
522 if (dp
->arg_index
== ARG_NONE
)
523 /* arg_posn wrapped around. */
526 REGISTER_ARG (dp
->arg_index
, type
);
533 if (d
->count
>= d_allocated
)
538 d_allocated
= xtimes (d_allocated
, 2);
539 memory_size
= xtimes (d_allocated
, sizeof (DIRECTIVE
));
540 if (size_overflow_p (memory_size
))
541 /* Overflow, would lead to out of memory. */
543 memory
= (DIRECTIVE
*) realloc (d
->dir
, memory_size
);
550 #if CHAR_T_ONLY_ASCII
551 else if (!c_isascii (c
))
553 /* Non-ASCII character. Not supported. */
558 d
->dir
[d
->count
].dir_start
= cp
;
560 d
->max_width_length
= max_width_length
;
561 d
->max_precision_length
= max_precision_length
;
575 #undef CHAR_T_ONLY_ASCII