1 /* Decomposed printf argument list.
2 Copyright (C) 1999, 2002-2003, 2005-2007 Free Software Foundation, Inc.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
8
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 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17 /* This file can be parametrized with the following macros:
18 ENABLE_UNISTDIO Set to 1 to enable the unistdio extensions.
19 PRINTF_FETCHARGS Name of the function to be defined.
20 STATIC Set to 'static' to declare the function static. */
21
22 #ifndef PRINTF_FETCHARGS
23 # include <config.h>
24 #endif
25
26 /* Specification. */
27 #ifndef PRINTF_FETCHARGS
28 # include "printf-args.h"
29 #endif
30
31 #ifdef STATIC
32 STATIC
33 #endif
34 int
PRINTF_FETCHARGS(va_list args,arguments * a)35 PRINTF_FETCHARGS (va_list args, arguments *a)
36 {
37 size_t i;
38 argument *ap;
39
40 for (i = 0, ap = &a->arg[0]; i < a->count; i++, ap++)
41 switch (ap->type)
42 {
43 case TYPE_SCHAR:
44 ap->a.a_schar = va_arg (args, /*signed char*/ int);
45 break;
46 case TYPE_UCHAR:
47 ap->a.a_uchar = va_arg (args, /*unsigned char*/ int);
48 break;
49 case TYPE_SHORT:
50 ap->a.a_short = va_arg (args, /*short*/ int);
51 break;
52 case TYPE_USHORT:
53 ap->a.a_ushort = va_arg (args, /*unsigned short*/ int);
54 break;
55 case TYPE_INT:
56 ap->a.a_int = va_arg (args, int);
57 break;
58 case TYPE_UINT:
59 ap->a.a_uint = va_arg (args, unsigned int);
60 break;
61 case TYPE_LONGINT:
62 ap->a.a_longint = va_arg (args, long int);
63 break;
64 case TYPE_ULONGINT:
65 ap->a.a_ulongint = va_arg (args, unsigned long int);
66 break;
67 #if HAVE_LONG_LONG_INT
68 case TYPE_LONGLONGINT:
69 ap->a.a_longlongint = va_arg (args, long long int);
70 break;
71 case TYPE_ULONGLONGINT:
72 ap->a.a_ulonglongint = va_arg (args, unsigned long long int);
73 break;
74 #endif
75 case TYPE_DOUBLE:
76 ap->a.a_double = va_arg (args, double);
77 break;
78 case TYPE_LONGDOUBLE:
79 ap->a.a_longdouble = va_arg (args, long double);
80 break;
81 case TYPE_CHAR:
82 ap->a.a_char = va_arg (args, int);
83 break;
84 #if HAVE_WINT_T
85 case TYPE_WIDE_CHAR:
86 /* Although ISO C 99 7.24.1.(2) says that wint_t is "unchanged by
87 default argument promotions", this is not the case in mingw32,
88 where wint_t is 'unsigned short'. */
89 ap->a.a_wide_char =
90 (sizeof (wint_t) < sizeof (int)
91 ? va_arg (args, int)
92 : va_arg (args, wint_t));
93 break;
94 #endif
95 case TYPE_STRING:
96 ap->a.a_string = va_arg (args, const char *);
97 /* A null pointer is an invalid argument for "%s", but in practice
98 it occurs quite frequently in printf statements that produce
99 debug output. Use a fallback in this case. */
100 if (ap->a.a_string == NULL)
101 ap->a.a_string = "(NULL)";
102 break;
103 #if HAVE_WCHAR_T
104 case TYPE_WIDE_STRING:
105 ap->a.a_wide_string = va_arg (args, const wchar_t *);
106 /* A null pointer is an invalid argument for "%ls", but in practice
107 it occurs quite frequently in printf statements that produce
108 debug output. Use a fallback in this case. */
109 if (ap->a.a_wide_string == NULL)
110 {
111 static const wchar_t wide_null_string[] =
112 {
113 (wchar_t)'(',
114 (wchar_t)'N', (wchar_t)'U', (wchar_t)'L', (wchar_t)'L',
115 (wchar_t)')',
116 (wchar_t)0
117 };
118 ap->a.a_wide_string = wide_null_string;
119 }
120 break;
121 #endif
122 case TYPE_POINTER:
123 ap->a.a_pointer = va_arg (args, void *);
124 break;
125 case TYPE_COUNT_SCHAR_POINTER:
126 ap->a.a_count_schar_pointer = va_arg (args, signed char *);
127 break;
128 case TYPE_COUNT_SHORT_POINTER:
129 ap->a.a_count_short_pointer = va_arg (args, short *);
130 break;
131 case TYPE_COUNT_INT_POINTER:
132 ap->a.a_count_int_pointer = va_arg (args, int *);
133 break;
134 case TYPE_COUNT_LONGINT_POINTER:
135 ap->a.a_count_longint_pointer = va_arg (args, long int *);
136 break;
137 #if HAVE_LONG_LONG_INT
138 case TYPE_COUNT_LONGLONGINT_POINTER:
139 ap->a.a_count_longlongint_pointer = va_arg (args, long long int *);
140 break;
141 #endif
142 #if ENABLE_UNISTDIO
143 /* The unistdio extensions. */
144 case TYPE_U8_STRING:
145 ap->a.a_u8_string = va_arg (args, const uint8_t *);
146 /* A null pointer is an invalid argument for "%U", but in practice
147 it occurs quite frequently in printf statements that produce
148 debug output. Use a fallback in this case. */
149 if (ap->a.a_u8_string == NULL)
150 {
151 static const uint8_t u8_null_string[] =
152 { '(', 'N', 'U', 'L', 'L', ')', 0 };
153 ap->a.a_u8_string = u8_null_string;
154 }
155 break;
156 case TYPE_U16_STRING:
157 ap->a.a_u16_string = va_arg (args, const uint16_t *);
158 /* A null pointer is an invalid argument for "%lU", but in practice
159 it occurs quite frequently in printf statements that produce
160 debug output. Use a fallback in this case. */
161 if (ap->a.a_u16_string == NULL)
162 {
163 static const uint16_t u16_null_string[] =
164 { '(', 'N', 'U', 'L', 'L', ')', 0 };
165 ap->a.a_u16_string = u16_null_string;
166 }
167 break;
168 case TYPE_U32_STRING:
169 ap->a.a_u32_string = va_arg (args, const uint32_t *);
170 /* A null pointer is an invalid argument for "%llU", but in practice
171 it occurs quite frequently in printf statements that produce
172 debug output. Use a fallback in this case. */
173 if (ap->a.a_u32_string == NULL)
174 {
175 static const uint32_t u32_null_string[] =
176 { '(', 'N', 'U', 'L', 'L', ')', 0 };
177 ap->a.a_u32_string = u32_null_string;
178 }
179 break;
180 #endif
181 default:
182 /* Unknown type. */
183 return -1;
184 }
185 return 0;
186 }
187