annotate lib/printf-args.c @ 276:23034db7dd8a 2.5

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