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. */
22 # include "wprintf-parse.h"
24 # include "printf-parse.h"
27 /* Get size_t, NULL. */
32 # if HAVE_STDINT_H_WITH_UINTMAX
35 # if HAVE_INTTYPES_H_WITH_UINTMAX
36 # include <inttypes.h>
42 /* malloc(), realloc(), free(). */
45 /* Checked size_t computations. */
49 # define PRINTF_PARSE wprintf_parse
50 # define CHAR_T wchar_t
51 # define DIRECTIVE wchar_t_directive
52 # define DIRECTIVES wchar_t_directives
54 # define PRINTF_PARSE printf_parse
56 # define DIRECTIVE char_directive
57 # define DIRECTIVES char_directives
64 PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a)
66 const CHAR_T *cp = format; /* pointer into format */
67 size_t arg_posn = 0; /* number of regular arguments consumed */
68 size_t d_allocated; /* allocated elements of d->dir */
69 size_t a_allocated; /* allocated elements of a->arg */
70 size_t max_width_length = 0;
71 size_t max_precision_length = 0;
75 d->dir = (DIRECTIVE *) malloc (d_allocated * sizeof (DIRECTIVE));
84 #define REGISTER_ARG(_index_,_type_) \
86 size_t n = (_index_); \
87 if (n >= a_allocated) \
92 a_allocated = xtimes (a_allocated, 2); \
93 if (a_allocated <= n) \
94 a_allocated = xsum (n, 1); \
95 memory_size = xtimes (a_allocated, sizeof (argument)); \
96 if (size_overflow_p (memory_size)) \
97 /* Overflow, would lead to out of memory. */ \
99 memory = (argument *) (a->arg \
100 ? realloc (a->arg, memory_size) \
101 : malloc (memory_size)); \
102 if (memory == NULL) \
103 /* Out of memory. */ \
107 while (a->count <= n) \
108 a->arg[a->count++].type = TYPE_NONE; \
109 if (a->arg[n].type == TYPE_NONE) \
110 a->arg[n].type = (_type_); \
111 else if (a->arg[n].type != (_type_)) \
112 /* Ambiguous type for positional argument. */ \
121 size_t arg_index = ARG_NONE;
122 DIRECTIVE *dp = &d->dir[d->count];/* pointer to next directive */
124 /* Initialize the next directive. */
125 dp->dir_start = cp - 1;
127 dp->width_start = NULL;
128 dp->width_end = NULL;
129 dp->width_arg_index = ARG_NONE;
130 dp->precision_start = NULL;
131 dp->precision_end = NULL;
132 dp->precision_arg_index = ARG_NONE;
133 dp->arg_index = ARG_NONE;
135 /* Test for positional argument. */
136 if (*cp >= '0' && *cp <= '9')
140 for (np = cp; *np >= '0' && *np <= '9'; np++)
146 for (np = cp; *np >= '0' && *np <= '9'; np++)
147 n = xsum (xtimes (n, 10), *np - '0');
149 /* Positional argument 0. */
151 if (size_overflow_p (n))
152 /* n too large, would lead to out of memory later. */
159 /* Read the flags. */
164 dp->flags |= FLAG_GROUP;
169 dp->flags |= FLAG_LEFT;
174 dp->flags |= FLAG_SHOWSIGN;
179 dp->flags |= FLAG_SPACE;
184 dp->flags |= FLAG_ALT;
189 dp->flags |= FLAG_ZERO;
196 /* Parse the field width. */
199 dp->width_start = cp;
202 if (max_width_length < 1)
203 max_width_length = 1;
205 /* Test for positional argument. */
206 if (*cp >= '0' && *cp <= '9')
210 for (np = cp; *np >= '0' && *np <= '9'; np++)
216 for (np = cp; *np >= '0' && *np <= '9'; np++)
217 n = xsum (xtimes (n, 10), *np - '0');
219 /* Positional argument 0. */
221 if (size_overflow_p (n))
222 /* n too large, would lead to out of memory later. */
224 dp->width_arg_index = n - 1;
228 if (dp->width_arg_index == ARG_NONE)
230 dp->width_arg_index = arg_posn++;
231 if (dp->width_arg_index == ARG_NONE)
232 /* arg_posn wrapped around. */
235 REGISTER_ARG (dp->width_arg_index, TYPE_INT);
237 else if (*cp >= '0' && *cp <= '9')
241 dp->width_start = cp;
242 for (; *cp >= '0' && *cp <= '9'; cp++)
245 width_length = dp->width_end - dp->width_start;
246 if (max_width_length < width_length)
247 max_width_length = width_length;
250 /* Parse the precision. */
256 dp->precision_start = cp - 1;
258 dp->precision_end = cp;
259 if (max_precision_length < 2)
260 max_precision_length = 2;
262 /* Test for positional argument. */
263 if (*cp >= '0' && *cp <= '9')
267 for (np = cp; *np >= '0' && *np <= '9'; np++)
273 for (np = cp; *np >= '0' && *np <= '9'; np++)
274 n = xsum (xtimes (n, 10), *np - '0');
276 /* Positional argument 0. */
278 if (size_overflow_p (n))
279 /* n too large, would lead to out of memory
282 dp->precision_arg_index = n - 1;
286 if (dp->precision_arg_index == ARG_NONE)
288 dp->precision_arg_index = arg_posn++;
289 if (dp->precision_arg_index == ARG_NONE)
290 /* arg_posn wrapped around. */
293 REGISTER_ARG (dp->precision_arg_index, TYPE_INT);
297 size_t precision_length;
299 dp->precision_start = cp - 1;
300 for (; *cp >= '0' && *cp <= '9'; cp++)
302 dp->precision_end = cp;
303 precision_length = dp->precision_end - dp->precision_start;
304 if (max_precision_length < precision_length)
305 max_precision_length = precision_length;
312 /* Parse argument type/size specifiers. */
320 flags |= (1 << (flags & 1));
335 if (sizeof (intmax_t) > sizeof (long))
337 /* intmax_t = long long */
340 else if (sizeof (intmax_t) > sizeof (int))
342 /* 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 #if HAVE_LONG_LONG_INT
388 /* If 'long long' exists and is larger than 'long': */
389 if (flags >= 16 || (flags & 4))
390 type = TYPE_LONGLONGINT;
393 /* If 'long long' exists and is the same as 'long', we parse
394 "lld" into TYPE_LONGINT. */
404 case 'o': case 'u': case 'x': case 'X':
405 #if HAVE_LONG_LONG_INT
406 /* If 'long long' exists and is larger than 'long': */
407 if (flags >= 16 || (flags & 4))
408 type = TYPE_ULONGLONGINT;
411 /* If 'unsigned long long' exists and is the same as
412 'unsigned long', we parse "llu" into TYPE_ULONGINT. */
414 type = TYPE_ULONGINT;
422 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
425 if (flags >= 16 || (flags & 4))
426 type = TYPE_LONGDOUBLE;
434 type = TYPE_WIDE_CHAR;
443 type = TYPE_WIDE_CHAR;
450 type = TYPE_WIDE_STRING;
459 type = TYPE_WIDE_STRING;
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_COUNT_LONGLONGINT_POINTER;
473 /* If 'long long' exists and is the same as 'long', we parse
474 "lln" into TYPE_COUNT_LONGINT_POINTER. */
476 type = TYPE_COUNT_LONGINT_POINTER;
478 type = TYPE_COUNT_SCHAR_POINTER;
480 type = TYPE_COUNT_SHORT_POINTER;
482 type = TYPE_COUNT_INT_POINTER;
488 /* Unknown conversion character. */
493 if (type != TYPE_NONE)
495 dp->arg_index = arg_index;
496 if (dp->arg_index == ARG_NONE)
498 dp->arg_index = arg_posn++;
499 if (dp->arg_index == ARG_NONE)
500 /* arg_posn wrapped around. */
503 REGISTER_ARG (dp->arg_index, type);
510 if (d->count >= d_allocated)
515 d_allocated = xtimes (d_allocated, 2);
516 memory_size = xtimes (d_allocated, sizeof (DIRECTIVE));
517 if (size_overflow_p (memory_size))
518 /* Overflow, would lead to out of memory. */
520 memory = (DIRECTIVE *) realloc (d->dir, memory_size);
528 d->dir[d->count].dir_start = cp;
530 d->max_width_length = max_width_length;
531 d->max_precision_length = max_precision_length;