1
/* -*- buffer-read-only: t -*- vi: set ro: */
2
/* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3
/* Formatted output to strings.
4
Copyright (C) 1999-2000, 2002-2003, 2006-2010 Free Software Foundation, Inc.
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 3, or (at your option)
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License along
17
with this program; if not, write to the Free Software Foundation,
18
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20
/* This file can be parametrized with the following macros:
21
CHAR_T The element type of the format string.
22
CHAR_T_ONLY_ASCII Set to 1 to enable verification that all characters
23
in the format string are ASCII.
24
DIRECTIVE Structure denoting a format directive.
26
DIRECTIVES Structure denoting the set of format directives of a
27
format string. Depends on CHAR_T.
28
PRINTF_PARSE Function that parses a format string.
30
STATIC Set to 'static' to declare the function static.
31
ENABLE_UNISTDIO Set to 1 to enable the unistdio extensions. */
39
# include "printf-parse.h"
42
/* Default parameters. */
44
# define PRINTF_PARSE printf_parse
46
# define DIRECTIVE char_directive
47
# define DIRECTIVES char_directives
50
/* Get size_t, NULL. */
54
#if defined IN_LIBINTL || defined IN_LIBASPRINTF
55
# if HAVE_STDINT_H_WITH_UINTMAX
58
# if HAVE_INTTYPES_H_WITH_UINTMAX
59
# include <inttypes.h>
65
/* malloc(), realloc(), free(). */
71
/* Checked size_t computations. */
83
PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a)
85
const CHAR_T *cp = format; /* pointer into format */
86
size_t arg_posn = 0; /* number of regular arguments consumed */
87
size_t d_allocated; /* allocated elements of d->dir */
88
size_t a_allocated; /* allocated elements of a->arg */
89
size_t max_width_length = 0;
90
size_t max_precision_length = 0;
94
d->dir = (DIRECTIVE *) malloc (d_allocated * sizeof (DIRECTIVE));
103
#define REGISTER_ARG(_index_,_type_) \
105
size_t n = (_index_); \
106
if (n >= a_allocated) \
108
size_t memory_size; \
111
a_allocated = xtimes (a_allocated, 2); \
112
if (a_allocated <= n) \
113
a_allocated = xsum (n, 1); \
114
memory_size = xtimes (a_allocated, sizeof (argument)); \
115
if (size_overflow_p (memory_size)) \
116
/* Overflow, would lead to out of memory. */ \
117
goto out_of_memory; \
118
memory = (argument *) (a->arg \
119
? realloc (a->arg, memory_size) \
120
: malloc (memory_size)); \
121
if (memory == NULL) \
122
/* Out of memory. */ \
123
goto out_of_memory; \
126
while (a->count <= n) \
127
a->arg[a->count++].type = TYPE_NONE; \
128
if (a->arg[n].type == TYPE_NONE) \
129
a->arg[n].type = (_type_); \
130
else if (a->arg[n].type != (_type_)) \
131
/* Ambiguous type for positional argument. */ \
140
size_t arg_index = ARG_NONE;
141
DIRECTIVE *dp = &d->dir[d->count]; /* pointer to next directive */
143
/* Initialize the next directive. */
144
dp->dir_start = cp - 1;
146
dp->width_start = NULL;
147
dp->width_end = NULL;
148
dp->width_arg_index = ARG_NONE;
149
dp->precision_start = NULL;
150
dp->precision_end = NULL;
151
dp->precision_arg_index = ARG_NONE;
152
dp->arg_index = ARG_NONE;
154
/* Test for positional argument. */
155
if (*cp >= '0' && *cp <= '9')
159
for (np = cp; *np >= '0' && *np <= '9'; np++)
165
for (np = cp; *np >= '0' && *np <= '9'; np++)
166
n = xsum (xtimes (n, 10), *np - '0');
168
/* Positional argument 0. */
170
if (size_overflow_p (n))
171
/* n too large, would lead to out of memory later. */
178
/* Read the flags. */
183
dp->flags |= FLAG_GROUP;
188
dp->flags |= FLAG_LEFT;
193
dp->flags |= FLAG_SHOWSIGN;
198
dp->flags |= FLAG_SPACE;
203
dp->flags |= FLAG_ALT;
208
dp->flags |= FLAG_ZERO;
215
/* Parse the field width. */
218
dp->width_start = cp;
221
if (max_width_length < 1)
222
max_width_length = 1;
224
/* Test for positional argument. */
225
if (*cp >= '0' && *cp <= '9')
229
for (np = cp; *np >= '0' && *np <= '9'; np++)
235
for (np = cp; *np >= '0' && *np <= '9'; np++)
236
n = xsum (xtimes (n, 10), *np - '0');
238
/* Positional argument 0. */
240
if (size_overflow_p (n))
241
/* n too large, would lead to out of memory later. */
243
dp->width_arg_index = n - 1;
247
if (dp->width_arg_index == ARG_NONE)
249
dp->width_arg_index = arg_posn++;
250
if (dp->width_arg_index == ARG_NONE)
251
/* arg_posn wrapped around. */
254
REGISTER_ARG (dp->width_arg_index, TYPE_INT);
256
else if (*cp >= '0' && *cp <= '9')
260
dp->width_start = cp;
261
for (; *cp >= '0' && *cp <= '9'; cp++)
264
width_length = dp->width_end - dp->width_start;
265
if (max_width_length < width_length)
266
max_width_length = width_length;
269
/* Parse the precision. */
275
dp->precision_start = cp - 1;
277
dp->precision_end = cp;
278
if (max_precision_length < 2)
279
max_precision_length = 2;
281
/* Test for positional argument. */
282
if (*cp >= '0' && *cp <= '9')
286
for (np = cp; *np >= '0' && *np <= '9'; np++)
292
for (np = cp; *np >= '0' && *np <= '9'; np++)
293
n = xsum (xtimes (n, 10), *np - '0');
295
/* Positional argument 0. */
297
if (size_overflow_p (n))
298
/* n too large, would lead to out of memory
301
dp->precision_arg_index = n - 1;
305
if (dp->precision_arg_index == ARG_NONE)
307
dp->precision_arg_index = arg_posn++;
308
if (dp->precision_arg_index == ARG_NONE)
309
/* arg_posn wrapped around. */
312
REGISTER_ARG (dp->precision_arg_index, TYPE_INT);
316
size_t precision_length;
318
dp->precision_start = cp - 1;
319
for (; *cp >= '0' && *cp <= '9'; cp++)
321
dp->precision_end = cp;
322
precision_length = dp->precision_end - dp->precision_start;
323
if (max_precision_length < precision_length)
324
max_precision_length = precision_length;
331
/* Parse argument type/size specifiers. */
339
flags |= (1 << (flags & 1));
354
if (sizeof (intmax_t) > sizeof (long))
356
/* intmax_t = long long */
359
else if (sizeof (intmax_t) > sizeof (int))
361
/* intmax_t = long */
366
else if (*cp == 'z' || *cp == 'Z')
368
/* 'z' is standardized in ISO C 99, but glibc uses 'Z'
369
because the warning facility in gcc-2.95.2 understands
370
only 'Z' (see gcc-2.95.2/gcc/c-common.c:1784). */
371
if (sizeof (size_t) > sizeof (long))
373
/* size_t = long long */
376
else if (sizeof (size_t) > sizeof (int))
385
if (sizeof (ptrdiff_t) > sizeof (long))
387
/* ptrdiff_t = long long */
390
else if (sizeof (ptrdiff_t) > sizeof (int))
392
/* ptrdiff_t = long */
397
#if defined __APPLE__ && defined __MACH__
398
/* On MacOS X 10.3, PRIdMAX is defined as "qd".
399
We cannot change it to "lld" because PRIdMAX must also
400
be understood by the system's printf routines. */
403
if (64 / 8 > sizeof (long))
405
/* int64_t = long long */
416
#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
417
/* On native Win32, PRIdMAX is defined as "I64d".
418
We cannot change it to "lld" because PRIdMAX must also
419
be understood by the system's printf routines. */
420
else if (*cp == 'I' && cp[1] == '6' && cp[2] == '4')
422
if (64 / 8 > sizeof (long))
424
/* __int64 = long long */
439
/* Read the conversion character. */
444
#if HAVE_LONG_LONG_INT
445
/* If 'long long' exists and is larger than 'long': */
446
if (flags >= 16 || (flags & 4))
447
type = TYPE_LONGLONGINT;
450
/* If 'long long' exists and is the same as 'long', we parse
451
"lld" into TYPE_LONGINT. */
461
case 'o': case 'u': case 'x': case 'X':
462
#if HAVE_LONG_LONG_INT
463
/* If 'long long' exists and is larger than 'long': */
464
if (flags >= 16 || (flags & 4))
465
type = TYPE_ULONGLONGINT;
468
/* If 'unsigned long long' exists and is the same as
469
'unsigned long', we parse "llu" into TYPE_ULONGINT. */
471
type = TYPE_ULONGINT;
479
case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
481
if (flags >= 16 || (flags & 4))
482
type = TYPE_LONGDOUBLE;
489
type = TYPE_WIDE_CHAR;
498
type = TYPE_WIDE_CHAR;
505
type = TYPE_WIDE_STRING;
514
type = TYPE_WIDE_STRING;
522
#if HAVE_LONG_LONG_INT
523
/* If 'long long' exists and is larger than 'long': */
524
if (flags >= 16 || (flags & 4))
525
type = TYPE_COUNT_LONGLONGINT_POINTER;
528
/* If 'long long' exists and is the same as 'long', we parse
529
"lln" into TYPE_COUNT_LONGINT_POINTER. */
531
type = TYPE_COUNT_LONGINT_POINTER;
533
type = TYPE_COUNT_SCHAR_POINTER;
535
type = TYPE_COUNT_SHORT_POINTER;
537
type = TYPE_COUNT_INT_POINTER;
540
/* The unistdio extensions. */
543
type = TYPE_U32_STRING;
545
type = TYPE_U16_STRING;
547
type = TYPE_U8_STRING;
554
/* Unknown conversion character. */
559
if (type != TYPE_NONE)
561
dp->arg_index = arg_index;
562
if (dp->arg_index == ARG_NONE)
564
dp->arg_index = arg_posn++;
565
if (dp->arg_index == ARG_NONE)
566
/* arg_posn wrapped around. */
569
REGISTER_ARG (dp->arg_index, type);
576
if (d->count >= d_allocated)
581
d_allocated = xtimes (d_allocated, 2);
582
memory_size = xtimes (d_allocated, sizeof (DIRECTIVE));
583
if (size_overflow_p (memory_size))
584
/* Overflow, would lead to out of memory. */
586
memory = (DIRECTIVE *) realloc (d->dir, memory_size);
593
#if CHAR_T_ONLY_ASCII
594
else if (!c_isascii (c))
596
/* Non-ASCII character. Not supported. */
601
d->dir[d->count].dir_start = cp;
603
d->max_width_length = max_width_length;
604
d->max_precision_length = max_precision_length;
628
#undef CHAR_T_ONLY_ASCII