]>
Commit | Line | Data |
---|---|---|
a04e7405 | 1 | /* Internal header for parsing printf format strings. |
04277e02 | 2 | Copyright (C) 1995-2019 Free Software Foundation, Inc. |
2c6fe0bd | 3 | This file is part of th GNU C Library. |
a04e7405 | 4 | |
2c6fe0bd | 5 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
a04e7405 | 9 | |
2c6fe0bd UD |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
a04e7405 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
a04e7405 | 18 | |
a04e7405 | 19 | #include <printf.h> |
e852e889 | 20 | #include <stdint.h> |
a04e7405 | 21 | #include <stddef.h> |
c06b7169 UD |
22 | #include <string.h> |
23 | #include <wchar.h> | |
a04e7405 | 24 | |
a04e7405 RM |
25 | |
26 | struct printf_spec | |
27 | { | |
77a58cad | 28 | /* Information parsed from the format spec. */ |
a04e7405 RM |
29 | struct printf_info info; |
30 | ||
31 | /* Pointers into the format string for the end of this format | |
32 | spec and the next (or to the end of the string if no more). */ | |
d64b6ad0 | 33 | const UCHAR_T *end_of_fmt, *next_fmt; |
a04e7405 RM |
34 | |
35 | /* Position of arguments for precision and width, or -1 if `info' has | |
36 | the constant value. */ | |
37 | int prec_arg, width_arg; | |
38 | ||
39 | int data_arg; /* Position of data argument. */ | |
40 | int data_arg_type; /* Type of first argument. */ | |
41 | /* Number of arguments consumed by this format specifier. */ | |
42 | size_t ndata_args; | |
9d26efa9 UD |
43 | /* Size of the parameter for PA_USER type. */ |
44 | int size; | |
a04e7405 RM |
45 | }; |
46 | ||
47 | ||
48 | /* The various kinds off arguments that can be passed to printf. */ | |
49 | union printf_arg | |
50 | { | |
2c6fe0bd | 51 | wchar_t pa_wchar; |
a04e7405 RM |
52 | int pa_int; |
53 | long int pa_long_int; | |
54 | long long int pa_long_long_int; | |
a04e7405 RM |
55 | unsigned int pa_u_int; |
56 | unsigned long int pa_u_long_int; | |
57 | unsigned long long int pa_u_long_long_int; | |
a04e7405 RM |
58 | double pa_double; |
59 | long double pa_long_double; | |
1626f499 GG |
60 | #if __HAVE_FLOAT128_UNLIKE_LDBL |
61 | _Float128 pa_float128; | |
62 | #endif | |
a04e7405 | 63 | const char *pa_string; |
2c6fe0bd | 64 | const wchar_t *pa_wstring; |
a04e7405 | 65 | void *pa_pointer; |
9d26efa9 | 66 | void *pa_user; |
a04e7405 RM |
67 | }; |
68 | ||
69 | ||
9c7ff11a | 70 | #ifndef DONT_NEED_READ_INT |
a04e7405 RM |
71 | /* Read a simple integer from a string and update the string pointer. |
72 | It is assumed that the first character is a digit. */ | |
135ffda8 | 73 | static int |
299a95b9 | 74 | read_int (const UCHAR_T * *pstr) |
a04e7405 | 75 | { |
135ffda8 | 76 | int retval = **pstr - L_('0'); |
a04e7405 | 77 | |
299a95b9 | 78 | while (ISDIGIT (*++(*pstr))) |
135ffda8 DM |
79 | if (retval >= 0) |
80 | { | |
81 | if (INT_MAX / 10 < retval) | |
82 | retval = -1; | |
83 | else | |
84 | { | |
85 | int digit = **pstr - L_('0'); | |
86 | ||
87 | retval *= 10; | |
88 | if (INT_MAX - digit < retval) | |
89 | retval = -1; | |
90 | else | |
91 | retval += digit; | |
92 | } | |
93 | } | |
a04e7405 RM |
94 | |
95 | return retval; | |
96 | } | |
d64b6ad0 | 97 | #endif |
a04e7405 RM |
98 | |
99 | ||
3e5f5557 | 100 | /* These are defined in reg-printf.c. */ |
9d26efa9 | 101 | extern printf_arginfo_size_function **__printf_arginfo_table attribute_hidden; |
6455d255 | 102 | extern printf_function **__printf_function_table attribute_hidden; |
9d26efa9 | 103 | extern printf_va_arg_function **__printf_va_arg_table attribute_hidden; |
a04e7405 RM |
104 | |
105 | ||
9c7ff11a UD |
106 | /* Find the next spec in FORMAT, or the end of the string. Returns |
107 | a pointer into FORMAT, to a '%' or a '\0'. */ | |
c06b7169 UD |
108 | __extern_always_inline const unsigned char * |
109 | __find_specmb (const unsigned char *format) | |
110 | { | |
111 | return (const unsigned char *) __strchrnul ((const char *) format, '%'); | |
112 | } | |
9c7ff11a | 113 | |
c06b7169 UD |
114 | __extern_always_inline const unsigned int * |
115 | __find_specwc (const unsigned int *format) | |
116 | { | |
117 | return (const unsigned int *) __wcschrnul ((const wchar_t *) format, L'%'); | |
118 | } | |
9c7ff11a UD |
119 | |
120 | ||
a04e7405 RM |
121 | /* FORMAT must point to a '%' at the beginning of a spec. Fills in *SPEC |
122 | with the parsed details. POSN is the number of arguments already | |
123 | consumed. At most MAXTYPES - POSN types are filled in TYPES. Return | |
124 | the number of args consumed by this spec; *MAX_REF_ARG is updated so it | |
125 | remains the highest argument index used. */ | |
9c7ff11a UD |
126 | extern size_t __parse_one_specmb (const unsigned char *format, size_t posn, |
127 | struct printf_spec *spec, | |
c06b7169 | 128 | size_t *max_ref_arg) attribute_hidden; |
9c7ff11a UD |
129 | |
130 | extern size_t __parse_one_specwc (const unsigned int *format, size_t posn, | |
131 | struct printf_spec *spec, | |
132 | size_t *max_ref_arg) attribute_hidden; | |
9d26efa9 UD |
133 | |
134 | ||
135 | ||
136 | /* This variable is defined in reg-modifier.c. */ | |
137 | struct printf_modifier_record; | |
138 | extern struct printf_modifier_record **__printf_modifier_table | |
139 | attribute_hidden; | |
140 | ||
141 | /* Handle registered modifiers. */ | |
142 | extern int __handle_registered_modifier_mb (const unsigned char **format, | |
143 | struct printf_info *info) | |
144 | attribute_hidden; | |
145 | extern int __handle_registered_modifier_wc (const unsigned int **format, | |
146 | struct printf_info *info) | |
147 | attribute_hidden; |