1 /* Formatted output to strings.
2 Copyright (C) 1999-2000, 2002-2003, 2006 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. */
22 # include "wprintf-parse.h"
24 # include "printf-parse.h"
27 /* Get size_t, NULL. */
31 #if HAVE_STDINT_H_WITH_UINTMAX
34 #if HAVE_INTTYPES_H_WITH_UINTMAX
35 # include <inttypes.h>
38 /* malloc(), realloc(), free(). */
41 /* Checked size_t computations. */
45 # define PRINTF_PARSE wprintf_parse
46 # define CHAR_T wchar_t
47 # define DIRECTIVE wchar_t_directive
48 # define DIRECTIVES wchar_t_directives
50 # define PRINTF_PARSE printf_parse
52 # define DIRECTIVE char_directive
53 # define DIRECTIVES char_directives
60 PRINTF_PARSE (const CHAR_T
*format
, DIRECTIVES
*d
, arguments
*a
)
62 const CHAR_T
*cp
= format
; /* pointer into format */
63 size_t arg_posn
= 0; /* number of regular arguments consumed */
64 size_t d_allocated
; /* allocated elements of d->dir */
65 size_t a_allocated
; /* allocated elements of a->arg */
66 size_t max_width_length
= 0;
67 size_t max_precision_length
= 0;
71 d
->dir
= (DIRECTIVE
*) malloc (d_allocated
* sizeof (DIRECTIVE
));
80 #define REGISTER_ARG(_index_,_type_) \
82 size_t n = (_index_); \
83 if (n >= a_allocated) \
88 a_allocated = xtimes (a_allocated, 2); \
89 if (a_allocated <= n) \
90 a_allocated = xsum (n, 1); \
91 memory_size = xtimes (a_allocated, sizeof (argument)); \
92 if (size_overflow_p (memory_size)) \
93 /* Overflow, would lead to out of memory. */ \
95 memory = (argument *) (a->arg \
96 ? realloc (a->arg, memory_size) \
97 : malloc (memory_size)); \
99 /* Out of memory. */ \
103 while (a->count <= n) \
104 a->arg[a->count++].type = TYPE_NONE; \
105 if (a->arg[n].type == TYPE_NONE) \
106 a->arg[n].type = (_type_); \
107 else if (a->arg[n].type != (_type_)) \
108 /* Ambiguous type for positional argument. */ \
117 size_t arg_index
= ARG_NONE
;
118 DIRECTIVE
*dp
= &d
->dir
[d
->count
];/* pointer to next directive */
120 /* Initialize the next directive. */
121 dp
->dir_start
= cp
- 1;
123 dp
->width_start
= NULL
;
124 dp
->width_end
= NULL
;
125 dp
->width_arg_index
= ARG_NONE
;
126 dp
->precision_start
= NULL
;
127 dp
->precision_end
= NULL
;
128 dp
->precision_arg_index
= ARG_NONE
;
129 dp
->arg_index
= ARG_NONE
;
131 /* Test for positional argument. */
132 if (*cp
>= '0' && *cp
<= '9')
136 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
142 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
143 n
= xsum (xtimes (n
, 10), *np
- '0');
145 /* Positional argument 0. */
147 if (size_overflow_p (n
))
148 /* n too large, would lead to out of memory later. */
155 /* Read the flags. */
160 dp
->flags
|= FLAG_GROUP
;
165 dp
->flags
|= FLAG_LEFT
;
170 dp
->flags
|= FLAG_SHOWSIGN
;
175 dp
->flags
|= FLAG_SPACE
;
180 dp
->flags
|= FLAG_ALT
;
185 dp
->flags
|= FLAG_ZERO
;
192 /* Parse the field width. */
195 dp
->width_start
= cp
;
198 if (max_width_length
< 1)
199 max_width_length
= 1;
201 /* Test for positional argument. */
202 if (*cp
>= '0' && *cp
<= '9')
206 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
212 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
213 n
= xsum (xtimes (n
, 10), *np
- '0');
215 /* Positional argument 0. */
217 if (size_overflow_p (n
))
218 /* n too large, would lead to out of memory later. */
220 dp
->width_arg_index
= n
- 1;
224 if (dp
->width_arg_index
== ARG_NONE
)
226 dp
->width_arg_index
= arg_posn
++;
227 if (dp
->width_arg_index
== ARG_NONE
)
228 /* arg_posn wrapped around. */
231 REGISTER_ARG (dp
->width_arg_index
, TYPE_INT
);
233 else if (*cp
>= '0' && *cp
<= '9')
237 dp
->width_start
= cp
;
238 for (; *cp
>= '0' && *cp
<= '9'; cp
++)
241 width_length
= dp
->width_end
- dp
->width_start
;
242 if (max_width_length
< width_length
)
243 max_width_length
= width_length
;
246 /* Parse the precision. */
252 dp
->precision_start
= cp
- 1;
254 dp
->precision_end
= cp
;
255 if (max_precision_length
< 2)
256 max_precision_length
= 2;
258 /* Test for positional argument. */
259 if (*cp
>= '0' && *cp
<= '9')
263 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
269 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
270 n
= xsum (xtimes (n
, 10), *np
- '0');
272 /* Positional argument 0. */
274 if (size_overflow_p (n
))
275 /* n too large, would lead to out of memory
278 dp
->precision_arg_index
= n
- 1;
282 if (dp
->precision_arg_index
== ARG_NONE
)
284 dp
->precision_arg_index
= arg_posn
++;
285 if (dp
->precision_arg_index
== ARG_NONE
)
286 /* arg_posn wrapped around. */
289 REGISTER_ARG (dp
->precision_arg_index
, TYPE_INT
);
293 size_t precision_length
;
295 dp
->precision_start
= cp
- 1;
296 for (; *cp
>= '0' && *cp
<= '9'; cp
++)
298 dp
->precision_end
= cp
;
299 precision_length
= dp
->precision_end
- dp
->precision_start
;
300 if (max_precision_length
< precision_length
)
301 max_precision_length
= precision_length
;
308 /* Parse argument type/size specifiers. */
316 flags
|= (1 << (flags
& 1));
332 if (sizeof (intmax_t) > sizeof (long))
334 /* intmax_t = long long */
337 else if (sizeof (intmax_t) > sizeof (int))
339 /* intmax_t = long */
345 else if (*cp
== 'z' || *cp
== 'Z')
347 /* 'z' is standardized in ISO C 99, but glibc uses 'Z'
348 because the warning facility in gcc-2.95.2 understands
349 only 'Z' (see gcc-2.95.2/gcc/c-common.c:1784). */
350 if (sizeof (size_t) > sizeof (long))
352 /* size_t = long long */
355 else if (sizeof (size_t) > sizeof (int))
364 if (sizeof (ptrdiff_t) > sizeof (long))
366 /* ptrdiff_t = long long */
369 else if (sizeof (ptrdiff_t) > sizeof (int))
371 /* ptrdiff_t = long */
380 /* Read the conversion character. */
385 #ifdef HAVE_LONG_LONG_INT
386 /* If 'long long' exists and is larger than 'long': */
387 if (flags
>= 16 || (flags
& 4))
388 type
= TYPE_LONGLONGINT
;
391 /* If 'long long' exists and is the same as 'long', we parse
392 "lld" into TYPE_LONGINT. */
402 case 'o': case 'u': case 'x': case 'X':
403 #ifdef HAVE_LONG_LONG_INT
404 /* If 'long long' exists and is larger than 'long': */
405 if (flags
>= 16 || (flags
& 4))
406 type
= TYPE_ULONGLONGINT
;
409 /* If 'unsigned long long' exists and is the same as
410 'unsigned long', we parse "llu" into TYPE_ULONGINT. */
412 type
= TYPE_ULONGINT
;
420 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
422 #ifdef HAVE_LONG_DOUBLE
423 if (flags
>= 16 || (flags
& 4))
424 type
= TYPE_LONGDOUBLE
;
432 type
= TYPE_WIDE_CHAR
;
441 type
= TYPE_WIDE_CHAR
;
448 type
= TYPE_WIDE_STRING
;
457 type
= TYPE_WIDE_STRING
;
465 #ifdef HAVE_LONG_LONG_INT
466 /* If 'long long' exists and is larger than 'long': */
467 if (flags
>= 16 || (flags
& 4))
468 type
= TYPE_COUNT_LONGLONGINT_POINTER
;
471 /* If 'long long' exists and is the same as 'long', we parse
472 "lln" into TYPE_COUNT_LONGINT_POINTER. */
474 type
= TYPE_COUNT_LONGINT_POINTER
;
476 type
= TYPE_COUNT_SCHAR_POINTER
;
478 type
= TYPE_COUNT_SHORT_POINTER
;
480 type
= TYPE_COUNT_INT_POINTER
;
486 /* Unknown conversion character. */
491 if (type
!= TYPE_NONE
)
493 dp
->arg_index
= arg_index
;
494 if (dp
->arg_index
== ARG_NONE
)
496 dp
->arg_index
= arg_posn
++;
497 if (dp
->arg_index
== ARG_NONE
)
498 /* arg_posn wrapped around. */
501 REGISTER_ARG (dp
->arg_index
, type
);
508 if (d
->count
>= d_allocated
)
513 d_allocated
= xtimes (d_allocated
, 2);
514 memory_size
= xtimes (d_allocated
, sizeof (DIRECTIVE
));
515 if (size_overflow_p (memory_size
))
516 /* Overflow, would lead to out of memory. */
518 memory
= (DIRECTIVE
*) realloc (d
->dir
, memory_size
);
526 d
->dir
[d
->count
].dir_start
= cp
;
528 d
->max_width_length
= max_width_length
;
529 d
->max_precision_length
= max_precision_length
;