1 /* Formatted output to strings.
2 Copyright (C) 1999-2000, 2002-2003, 2006-2008 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published
6 by 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 GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 /* This file can be parametrized with the following macros:
20 CHAR_T The element type of the format string.
21 CHAR_T_ONLY_ASCII Set to 1 to enable verification that all characters
22 in the format string are ASCII.
23 DIRECTIVE Structure denoting a format directive.
25 DIRECTIVES Structure denoting the set of format directives of a
26 format string. Depends on CHAR_T.
27 PRINTF_PARSE Function that parses a format string.
29 STATIC Set to 'static' to declare the function static.
30 ENABLE_UNISTDIO Set to 1 to enable the unistdio extensions. */
38 # include "printf-parse.h"
41 /* Default parameters. */
43 # define PRINTF_PARSE printf_parse
45 # define DIRECTIVE char_directive
46 # define DIRECTIVES char_directives
49 /* Get size_t, NULL. */
53 #if defined IN_LIBINTL || defined IN_LIBASPRINTF
54 # if HAVE_STDINT_H_WITH_UINTMAX
57 # if HAVE_INTTYPES_H_WITH_UINTMAX
58 # include <inttypes.h>
64 /* malloc(), realloc(), free(). */
70 /* Checked size_t computations. */
82 PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a)
84 const CHAR_T *cp = format; /* pointer into format */
85 size_t arg_posn = 0; /* number of regular arguments consumed */
86 size_t d_allocated; /* allocated elements of d->dir */
87 size_t a_allocated; /* allocated elements of a->arg */
88 size_t max_width_length = 0;
89 size_t max_precision_length = 0;
93 d->dir = (DIRECTIVE *) malloc (d_allocated * sizeof (DIRECTIVE));
102 #define REGISTER_ARG(_index_,_type_) \
104 size_t n = (_index_); \
105 if (n >= a_allocated) \
107 size_t memory_size; \
110 a_allocated = xtimes (a_allocated, 2); \
111 if (a_allocated <= n) \
112 a_allocated = xsum (n, 1); \
113 memory_size = xtimes (a_allocated, sizeof (argument)); \
114 if (size_overflow_p (memory_size)) \
115 /* Overflow, would lead to out of memory. */ \
116 goto out_of_memory; \
117 memory = (argument *) (a->arg \
118 ? realloc (a->arg, memory_size) \
119 : malloc (memory_size)); \
120 if (memory == NULL) \
121 /* Out of memory. */ \
122 goto out_of_memory; \
125 while (a->count <= n) \
126 a->arg[a->count++].type = TYPE_NONE; \
127 if (a->arg[n].type == TYPE_NONE) \
128 a->arg[n].type = (_type_); \
129 else if (a->arg[n].type != (_type_)) \
130 /* Ambiguous type for positional argument. */ \
139 size_t arg_index = ARG_NONE;
140 DIRECTIVE *dp = &d->dir[d->count]; /* pointer to next directive */
142 /* Initialize the next directive. */
143 dp->dir_start = cp - 1;
145 dp->width_start = NULL;
146 dp->width_end = NULL;
147 dp->width_arg_index = ARG_NONE;
148 dp->precision_start = NULL;
149 dp->precision_end = NULL;
150 dp->precision_arg_index = ARG_NONE;
151 dp->arg_index = ARG_NONE;
153 /* Test for positional argument. */
154 if (*cp >= '0' && *cp <= '9')
158 for (np = cp; *np >= '0' && *np <= '9'; np++)
164 for (np = cp; *np >= '0' && *np <= '9'; np++)
165 n = xsum (xtimes (n, 10), *np - '0');
167 /* Positional argument 0. */
169 if (size_overflow_p (n))
170 /* n too large, would lead to out of memory later. */
177 /* Read the flags. */
182 dp->flags |= FLAG_GROUP;
187 dp->flags |= FLAG_LEFT;
192 dp->flags |= FLAG_SHOWSIGN;
197 dp->flags |= FLAG_SPACE;
202 dp->flags |= FLAG_ALT;
207 dp->flags |= FLAG_ZERO;
214 /* Parse the field width. */
217 dp->width_start = cp;
220 if (max_width_length < 1)
221 max_width_length = 1;
223 /* Test for positional argument. */
224 if (*cp >= '0' && *cp <= '9')
228 for (np = cp; *np >= '0' && *np <= '9'; np++)
234 for (np = cp; *np >= '0' && *np <= '9'; np++)
235 n = xsum (xtimes (n, 10), *np - '0');
237 /* Positional argument 0. */
239 if (size_overflow_p (n))
240 /* n too large, would lead to out of memory later. */
242 dp->width_arg_index = n - 1;
246 if (dp->width_arg_index == ARG_NONE)
248 dp->width_arg_index = arg_posn++;
249 if (dp->width_arg_index == ARG_NONE)
250 /* arg_posn wrapped around. */
253 REGISTER_ARG (dp->width_arg_index, TYPE_INT);
255 else if (*cp >= '0' && *cp <= '9')
259 dp->width_start = cp;
260 for (; *cp >= '0' && *cp <= '9'; cp++)
263 width_length = dp->width_end - dp->width_start;
264 if (max_width_length < width_length)
265 max_width_length = width_length;
268 /* Parse the precision. */
274 dp->precision_start = cp - 1;
276 dp->precision_end = cp;
277 if (max_precision_length < 2)
278 max_precision_length = 2;
280 /* Test for positional argument. */
281 if (*cp >= '0' && *cp <= '9')
285 for (np = cp; *np >= '0' && *np <= '9'; np++)
291 for (np = cp; *np >= '0' && *np <= '9'; np++)
292 n = xsum (xtimes (n, 10), *np - '0');
294 /* Positional argument 0. */
296 if (size_overflow_p (n))
297 /* n too large, would lead to out of memory
300 dp->precision_arg_index = n - 1;
304 if (dp->precision_arg_index == ARG_NONE)
306 dp->precision_arg_index = arg_posn++;
307 if (dp->precision_arg_index == ARG_NONE)
308 /* arg_posn wrapped around. */
311 REGISTER_ARG (dp->precision_arg_index, TYPE_INT);
315 size_t precision_length;
317 dp->precision_start = cp - 1;
318 for (; *cp >= '0' && *cp <= '9'; cp++)
320 dp->precision_end = cp;
321 precision_length = dp->precision_end - dp->precision_start;
322 if (max_precision_length < precision_length)
323 max_precision_length = precision_length;
330 /* Parse argument type/size specifiers. */
338 flags |= (1 << (flags & 1));
353 if (sizeof (intmax_t) > sizeof (long))
355 /* intmax_t = long long */
358 else if (sizeof (intmax_t) > sizeof (int))
360 /* intmax_t = long */
365 else if (*cp == 'z' || *cp == 'Z')
367 /* 'z' is standardized in ISO C 99, but glibc uses 'Z'
368 because the warning facility in gcc-2.95.2 understands
369 only 'Z' (see gcc-2.95.2/gcc/c-common.c:1784). */
370 if (sizeof (size_t) > sizeof (long))
372 /* size_t = long long */
375 else if (sizeof (size_t) > sizeof (int))
384 if (sizeof (ptrdiff_t) > sizeof (long))
386 /* ptrdiff_t = long long */
389 else if (sizeof (ptrdiff_t) > sizeof (int))
391 /* ptrdiff_t = long */
396 #if defined __APPLE__ && defined __MACH__
397 /* On MacOS X 10.3, PRIdMAX is defined as "qd".
398 We cannot change it to "lld" because PRIdMAX must also
399 be understood by the system's printf routines. */
402 if (64 / 8 > sizeof (long))
404 /* int64_t = long long */
415 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
416 /* On native Win32, PRIdMAX is defined as "I64d".
417 We cannot change it to "lld" because PRIdMAX must also
418 be understood by the system's printf routines. */
419 else if (*cp == 'I' && cp[1] == '6' && cp[2] == '4')
421 if (64 / 8 > sizeof (long))
423 /* __int64 = long long */
438 /* Read the conversion character. */
443 #if HAVE_LONG_LONG_INT
444 /* If 'long long' exists and is larger than 'long': */
445 if (flags >= 16 || (flags & 4))
446 type = TYPE_LONGLONGINT;
449 /* If 'long long' exists and is the same as 'long', we parse
450 "lld" into TYPE_LONGINT. */
460 case 'o': case 'u': case 'x': case 'X':
461 #if HAVE_LONG_LONG_INT
462 /* If 'long long' exists and is larger than 'long': */
463 if (flags >= 16 || (flags & 4))
464 type = TYPE_ULONGLONGINT;
467 /* If 'unsigned long long' exists and is the same as
468 'unsigned long', we parse "llu" into TYPE_ULONGINT. */
470 type = TYPE_ULONGINT;
478 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
480 if (flags >= 16 || (flags & 4))
481 type = TYPE_LONGDOUBLE;
488 type = TYPE_WIDE_CHAR;
497 type = TYPE_WIDE_CHAR;
504 type = TYPE_WIDE_STRING;
513 type = TYPE_WIDE_STRING;
521 #if HAVE_LONG_LONG_INT
522 /* If 'long long' exists and is larger than 'long': */
523 if (flags >= 16 || (flags & 4))
524 type = TYPE_COUNT_LONGLONGINT_POINTER;
527 /* If 'long long' exists and is the same as 'long', we parse
528 "lln" into TYPE_COUNT_LONGINT_POINTER. */
530 type = TYPE_COUNT_LONGINT_POINTER;
532 type = TYPE_COUNT_SCHAR_POINTER;
534 type = TYPE_COUNT_SHORT_POINTER;
536 type = TYPE_COUNT_INT_POINTER;
539 /* The unistdio extensions. */
542 type = TYPE_U32_STRING;
544 type = TYPE_U16_STRING;
546 type = TYPE_U8_STRING;
553 /* Unknown conversion character. */
558 if (type != TYPE_NONE)
560 dp->arg_index = arg_index;
561 if (dp->arg_index == ARG_NONE)
563 dp->arg_index = arg_posn++;
564 if (dp->arg_index == ARG_NONE)
565 /* arg_posn wrapped around. */
568 REGISTER_ARG (dp->arg_index, type);
575 if (d->count >= d_allocated)
580 d_allocated = xtimes (d_allocated, 2);
581 memory_size = xtimes (d_allocated, sizeof (DIRECTIVE));
582 if (size_overflow_p (memory_size))
583 /* Overflow, would lead to out of memory. */
585 memory = (DIRECTIVE *) realloc (d->dir, memory_size);
592 #if CHAR_T_ONLY_ASCII
593 else if (!c_isascii (c))
595 /* Non-ASCII character. Not supported. */
600 d->dir[d->count].dir_start = cp;
602 d->max_width_length = max_width_length;
603 d->max_precision_length = max_precision_length;
627 #undef CHAR_T_ONLY_ASCII