1 /* Formatted output to strings.
2 Copyright (C) 1999-2000, 2002-2003 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. */
24 # include "wprintf-parse.h"
26 # include "printf-parse.h"
29 /* Get size_t, NULL. */
33 #if HAVE_STDINT_H_WITH_UINTMAX
36 #if HAVE_INTTYPES_H_WITH_UINTMAX
37 # include <inttypes.h>
40 /* malloc(), realloc(), free(). */
43 /* Checked size_t computations. */
47 # define PRINTF_PARSE wprintf_parse
48 # define CHAR_T wchar_t
49 # define DIRECTIVE wchar_t_directive
50 # define DIRECTIVES wchar_t_directives
52 # define PRINTF_PARSE printf_parse
54 # define DIRECTIVE char_directive
55 # define DIRECTIVES char_directives
62 PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a)
64 const CHAR_T *cp = format; /* pointer into format */
65 size_t arg_posn = 0; /* number of regular arguments consumed */
66 size_t d_allocated; /* allocated elements of d->dir */
67 size_t a_allocated; /* allocated elements of a->arg */
68 size_t max_width_length = 0;
69 size_t max_precision_length = 0;
73 d->dir = malloc (d_allocated * sizeof (DIRECTIVE));
82 #define REGISTER_ARG(_index_,_type_) \
84 size_t n = (_index_); \
85 if (n >= a_allocated) \
90 a_allocated = xtimes (a_allocated, 2); \
91 if (a_allocated <= n) \
92 a_allocated = xsum (n, 1); \
93 memory_size = xtimes (a_allocated, sizeof (argument)); \
94 if (size_overflow_p (memory_size)) \
95 /* Overflow, would lead to out of memory. */ \
98 ? realloc (a->arg, memory_size) \
99 : malloc (memory_size)); \
100 if (memory == NULL) \
101 /* Out of memory. */ \
105 while (a->count <= n) \
106 a->arg[a->count++].type = TYPE_NONE; \
107 if (a->arg[n].type == TYPE_NONE) \
108 a->arg[n].type = (_type_); \
109 else if (a->arg[n].type != (_type_)) \
110 /* Ambiguous type for positional argument. */ \
119 size_t arg_index = ARG_NONE;
120 DIRECTIVE *dp = &d->dir[d->count];/* pointer to next directive */
122 /* Initialize the next directive. */
123 dp->dir_start = cp - 1;
125 dp->width_start = NULL;
126 dp->width_end = NULL;
127 dp->width_arg_index = ARG_NONE;
128 dp->precision_start = NULL;
129 dp->precision_end = NULL;
130 dp->precision_arg_index = ARG_NONE;
131 dp->arg_index = ARG_NONE;
133 /* Test for positional argument. */
134 if (*cp >= '0' && *cp <= '9')
138 for (np = cp; *np >= '0' && *np <= '9'; np++)
144 for (np = cp; *np >= '0' && *np <= '9'; np++)
145 n = xsum (xtimes (n, 10), *np - '0');
147 /* Positional argument 0. */
149 if (size_overflow_p (n))
150 /* n too large, would lead to out of memory later. */
157 /* Read the flags. */
162 dp->flags |= FLAG_GROUP;
167 dp->flags |= FLAG_LEFT;
172 dp->flags |= FLAG_SHOWSIGN;
177 dp->flags |= FLAG_SPACE;
182 dp->flags |= FLAG_ALT;
187 dp->flags |= FLAG_ZERO;
194 /* Parse the field width. */
197 dp->width_start = cp;
200 if (max_width_length < 1)
201 max_width_length = 1;
203 /* Test for positional argument. */
204 if (*cp >= '0' && *cp <= '9')
208 for (np = cp; *np >= '0' && *np <= '9'; np++)
214 for (np = cp; *np >= '0' && *np <= '9'; np++)
215 n = xsum (xtimes (n, 10), *np - '0');
217 /* Positional argument 0. */
219 if (size_overflow_p (n))
220 /* n too large, would lead to out of memory later. */
222 dp->width_arg_index = n - 1;
226 if (dp->width_arg_index == ARG_NONE)
228 dp->width_arg_index = arg_posn++;
229 if (dp->width_arg_index == ARG_NONE)
230 /* arg_posn wrapped around. */
233 REGISTER_ARG (dp->width_arg_index, TYPE_INT);
235 else if (*cp >= '0' && *cp <= '9')
239 dp->width_start = cp;
240 for (; *cp >= '0' && *cp <= '9'; cp++)
243 width_length = dp->width_end - dp->width_start;
244 if (max_width_length < width_length)
245 max_width_length = width_length;
248 /* Parse the precision. */
254 dp->precision_start = cp - 1;
256 dp->precision_end = cp;
257 if (max_precision_length < 2)
258 max_precision_length = 2;
260 /* Test for positional argument. */
261 if (*cp >= '0' && *cp <= '9')
265 for (np = cp; *np >= '0' && *np <= '9'; np++)
271 for (np = cp; *np >= '0' && *np <= '9'; np++)
272 n = xsum (xtimes (n, 10), *np - '0');
274 /* Positional argument 0. */
276 if (size_overflow_p (n))
277 /* n too large, would lead to out of memory
280 dp->precision_arg_index = n - 1;
284 if (dp->precision_arg_index == ARG_NONE)
286 dp->precision_arg_index = arg_posn++;
287 if (dp->precision_arg_index == ARG_NONE)
288 /* arg_posn wrapped around. */
291 REGISTER_ARG (dp->precision_arg_index, TYPE_INT);
295 size_t precision_length;
297 dp->precision_start = cp - 1;
298 for (; *cp >= '0' && *cp <= '9'; cp++)
300 dp->precision_end = cp;
301 precision_length = dp->precision_end - dp->precision_start;
302 if (max_precision_length < precision_length)
303 max_precision_length = precision_length;
310 /* Parse argument type/size specifiers. */
318 flags |= (1 << (flags & 1));
334 if (sizeof (intmax_t) > sizeof (long))
336 /* intmax_t = long long */
339 else if (sizeof (intmax_t) > sizeof (int))
341 /* intmax_t = long */
347 else if (*cp == 'z' || *cp == 'Z')
349 /* 'z' is standardized in ISO C 99, but glibc uses 'Z'
350 because the warning facility in gcc-2.95.2 understands
351 only 'Z' (see gcc-2.95.2/gcc/c-common.c:1784). */
352 if (sizeof (size_t) > sizeof (long))
354 /* size_t = long long */
357 else if (sizeof (size_t) > sizeof (int))
366 if (sizeof (ptrdiff_t) > sizeof (long))
368 /* ptrdiff_t = long long */
371 else if (sizeof (ptrdiff_t) > sizeof (int))
373 /* ptrdiff_t = long */
382 /* Read the conversion character. */
387 #ifdef HAVE_LONG_LONG
388 if (flags >= 16 || (flags & 4))
389 type = TYPE_LONGLONGINT;
401 case 'o': case 'u': case 'x': case 'X':
402 #ifdef HAVE_LONG_LONG
403 if (flags >= 16 || (flags & 4))
404 type = TYPE_ULONGLONGINT;
408 type = TYPE_ULONGINT;
416 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
418 #ifdef HAVE_LONG_DOUBLE
419 if (flags >= 16 || (flags & 4))
420 type = TYPE_LONGDOUBLE;
428 type = TYPE_WIDE_CHAR;
437 type = TYPE_WIDE_CHAR;
444 type = TYPE_WIDE_STRING;
453 type = TYPE_WIDE_STRING;
461 #ifdef HAVE_LONG_LONG
462 if (flags >= 16 || (flags & 4))
463 type = TYPE_COUNT_LONGLONGINT_POINTER;
467 type = TYPE_COUNT_LONGINT_POINTER;
469 type = TYPE_COUNT_SCHAR_POINTER;
471 type = TYPE_COUNT_SHORT_POINTER;
473 type = TYPE_COUNT_INT_POINTER;
479 /* Unknown conversion character. */
484 if (type != TYPE_NONE)
486 dp->arg_index = arg_index;
487 if (dp->arg_index == ARG_NONE)
489 dp->arg_index = arg_posn++;
490 if (dp->arg_index == ARG_NONE)
491 /* arg_posn wrapped around. */
494 REGISTER_ARG (dp->arg_index, type);
501 if (d->count >= d_allocated)
506 d_allocated = xtimes (d_allocated, 2);
507 memory_size = xtimes (d_allocated, sizeof (DIRECTIVE));
508 if (size_overflow_p (memory_size))
509 /* Overflow, would lead to out of memory. */
511 memory = realloc (d->dir, memory_size);
519 d->dir[d->count].dir_start = cp;
521 d->max_width_length = max_width_length;
522 d->max_precision_length = max_precision_length;