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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 # include "wprintf-parse.h"
26 # include "printf-parse.h"
29 /* Get size_t, NULL. */
33 #include <sys/types.h>
36 #if HAVE_STDINT_H_WITH_UINTMAX
39 #if HAVE_INTTYPES_H_WITH_UINTMAX
40 # include <inttypes.h>
43 /* malloc(), realloc(), free(). */
46 /* Checked size_t computations. */
50 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
54 # define PRINTF_PARSE wprintf_parse
55 # define CHAR_T wchar_t
56 # define DIRECTIVE wchar_t_directive
57 # define DIRECTIVES wchar_t_directives
59 # define PRINTF_PARSE printf_parse
61 # define DIRECTIVE char_directive
62 # define DIRECTIVES char_directives
69 PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a)
71 const CHAR_T *cp = format; /* pointer into format */
72 ssize_t arg_posn = 0; /* number of regular arguments consumed */
73 size_t d_allocated; /* allocated elements of d->dir */
74 size_t a_allocated; /* allocated elements of a->arg */
75 size_t max_width_length = 0;
76 size_t max_precision_length = 0;
80 d->dir = malloc (d_allocated * sizeof (DIRECTIVE));
89 #define REGISTER_ARG(_index_,_type_) \
91 size_t n = (_index_); \
92 if (n >= a_allocated) \
97 a_allocated = xtimes (a_allocated, 2); \
98 if (a_allocated <= n) \
99 a_allocated = xsum (n, 1); \
100 memory_size = xtimes (a_allocated, sizeof (argument)); \
101 if (size_overflow_p (memory_size)) \
102 /* Overflow, would lead to out of memory. */ \
105 ? realloc (a->arg, memory_size) \
106 : malloc (memory_size)); \
107 if (memory == NULL) \
108 /* Out of memory. */ \
112 while (a->count <= n) \
113 a->arg[a->count++].type = TYPE_NONE; \
114 if (a->arg[n].type == TYPE_NONE) \
115 a->arg[n].type = (_type_); \
116 else if (a->arg[n].type != (_type_)) \
117 /* Ambiguous type for positional argument. */ \
126 ssize_t arg_index = -1;
127 DIRECTIVE *dp = &d->dir[d->count];/* pointer to next directive */
129 /* Initialize the next directive. */
130 dp->dir_start = cp - 1;
132 dp->width_start = NULL;
133 dp->width_end = NULL;
134 dp->width_arg_index = -1;
135 dp->precision_start = NULL;
136 dp->precision_end = NULL;
137 dp->precision_arg_index = -1;
140 /* Test for positional argument. */
141 if (*cp >= '0' && *cp <= '9')
145 for (np = cp; *np >= '0' && *np <= '9'; np++)
151 for (np = cp; *np >= '0' && *np <= '9'; np++)
152 n = xsum (xtimes (n, 10), *np - '0');
154 /* Positional argument 0. */
156 if (size_overflow_p (n) || n - 1 > SSIZE_MAX)
157 /* n too large, would lead to out of memory later. */
164 /* Read the flags. */
169 dp->flags |= FLAG_GROUP;
174 dp->flags |= FLAG_LEFT;
179 dp->flags |= FLAG_SHOWSIGN;
184 dp->flags |= FLAG_SPACE;
189 dp->flags |= FLAG_ALT;
194 dp->flags |= FLAG_ZERO;
201 /* Parse the field width. */
204 dp->width_start = cp;
207 if (max_width_length < 1)
208 max_width_length = 1;
210 /* Test for positional argument. */
211 if (*cp >= '0' && *cp <= '9')
215 for (np = cp; *np >= '0' && *np <= '9'; np++)
221 for (np = cp; *np >= '0' && *np <= '9'; np++)
222 n = xsum (xtimes (n, 10), *np - '0');
224 /* Positional argument 0. */
226 if (size_overflow_p (n) || n - 1 > SSIZE_MAX)
227 /* n too large, would lead to out of memory later. */
229 dp->width_arg_index = n - 1;
233 if (dp->width_arg_index < 0)
235 dp->width_arg_index = arg_posn++;
236 if (dp->width_arg_index < 0)
237 /* arg_posn wrapped around at SSIZE_MAX. */
240 REGISTER_ARG (dp->width_arg_index, TYPE_INT);
242 else if (*cp >= '0' && *cp <= '9')
246 dp->width_start = cp;
247 for (; *cp >= '0' && *cp <= '9'; cp++)
250 width_length = dp->width_end - dp->width_start;
251 if (max_width_length < width_length)
252 max_width_length = width_length;
255 /* Parse the precision. */
261 dp->precision_start = cp - 1;
263 dp->precision_end = cp;
264 if (max_precision_length < 2)
265 max_precision_length = 2;
267 /* Test for positional argument. */
268 if (*cp >= '0' && *cp <= '9')
272 for (np = cp; *np >= '0' && *np <= '9'; np++)
278 for (np = cp; *np >= '0' && *np <= '9'; np++)
279 n = xsum (xtimes (n, 10), *np - '0');
281 /* Positional argument 0. */
283 if (size_overflow_p (n) || n - 1 > SSIZE_MAX)
284 /* n too large, would lead to out of memory
287 dp->precision_arg_index = n - 1;
291 if (dp->precision_arg_index < 0)
293 dp->precision_arg_index = arg_posn++;
294 if (dp->precision_arg_index < 0)
295 /* arg_posn wrapped around at SSIZE_MAX. */
298 REGISTER_ARG (dp->precision_arg_index, TYPE_INT);
302 size_t precision_length;
304 dp->precision_start = cp - 1;
305 for (; *cp >= '0' && *cp <= '9'; cp++)
307 dp->precision_end = cp;
308 precision_length = dp->precision_end - dp->precision_start;
309 if (max_precision_length < precision_length)
310 max_precision_length = precision_length;
317 /* Parse argument type/size specifiers. */
325 flags |= (1 << (flags & 1));
341 if (sizeof (intmax_t) > sizeof (long))
343 /* intmax_t = long long */
346 else if (sizeof (intmax_t) > sizeof (int))
348 /* intmax_t = long */
354 else if (*cp == 'z' || *cp == 'Z')
356 /* 'z' is standardized in ISO C 99, but glibc uses 'Z'
357 because the warning facility in gcc-2.95.2 understands
358 only 'Z' (see gcc-2.95.2/gcc/c-common.c:1784). */
359 if (sizeof (size_t) > sizeof (long))
361 /* size_t = long long */
364 else if (sizeof (size_t) > sizeof (int))
373 if (sizeof (ptrdiff_t) > sizeof (long))
375 /* ptrdiff_t = long long */
378 else if (sizeof (ptrdiff_t) > sizeof (int))
380 /* ptrdiff_t = long */
389 /* Read the conversion character. */
394 #ifdef HAVE_LONG_LONG
395 if (flags >= 16 || (flags & 4))
396 type = TYPE_LONGLONGINT;
408 case 'o': case 'u': case 'x': case 'X':
409 #ifdef HAVE_LONG_LONG
410 if (flags >= 16 || (flags & 4))
411 type = TYPE_ULONGLONGINT;
415 type = TYPE_ULONGINT;
423 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
425 #ifdef HAVE_LONG_DOUBLE
426 if (flags >= 16 || (flags & 4))
427 type = TYPE_LONGDOUBLE;
435 type = TYPE_WIDE_CHAR;
444 type = TYPE_WIDE_CHAR;
451 type = TYPE_WIDE_STRING;
460 type = TYPE_WIDE_STRING;
468 #ifdef HAVE_LONG_LONG
469 if (flags >= 16 || (flags & 4))
470 type = TYPE_COUNT_LONGLONGINT_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 < 0)
496 dp->arg_index = arg_posn++;
497 if (dp->arg_index < 0)
498 /* arg_posn wrapped around at SSIZE_MAX. */
501 REGISTER_ARG (dp->arg_index, type);
508 if (d->count >= d_allocated)
513 d_allocated = xtimes (d_allocated, 2);
514 if (size_overflow_p (d_allocated))
515 /* Overflow, would lead to out of memory. */
517 memory_size = xtimes (d_allocated, sizeof (DIRECTIVE));
518 if (size_overflow_p (memory_size))
519 /* Overflow, would lead to out of memory. */
521 memory = realloc (d->dir, memory_size);
529 d->dir[d->count].dir_start = cp;
531 d->max_width_length = max_width_length;
532 d->max_precision_length = max_precision_length;