-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathvprintf.h
427 lines (405 loc) · 14.4 KB
/
vprintf.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// Copyright (c) 2015-2016 Nuxi, https://nuxi.nl/
//
// SPDX-License-Identifier: BSD-2-Clause
#include <common/float10.h>
#include <common/float16.h>
#include <common/locale.h>
#include <common/mbstate.h>
#include <common/numeric_grouping.h>
#include <common/stdio.h>
#include <assert.h>
#include <errno.h>
#include <fenv.h>
#include <limits.h>
#include <locale.h>
#include <math.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wchar.h>
#if WIDE
typedef wchar_t char_t;
#else
typedef char char_t;
#endif
#include <common/vprintscanf.h>
#define VASPRINTF 1
#define VDPRINTF 2
#define VFPRINTF 3
#define VSNPRINTF 4
#define GET_ARG_SINT_LM(lm, index) \
((lm) == LM_SHORT_SHORT \
? (signed char)GET_ARG_SINT_T(int, index) \
: (lm) == LM_SHORT \
? (signed short)GET_ARG_SINT_T(int, index) \
: (lm) == LM_LONG ? GET_ARG_SINT_T(signed long, index) \
: (lm) == LM_LONG_LONG \
? GET_ARG_SINT_T(signed long long, index) \
: GET_ARG_SINT_T(signed int, index))
#define GET_ARG_UINT_LM(lm, index) \
((lm) == LM_SHORT_SHORT \
? (unsigned char)GET_ARG_UINT_T(int, index) \
: (lm) == LM_SHORT \
? (unsigned short)GET_ARG_UINT_T(int, index) \
: (lm) == LM_LONG \
? GET_ARG_UINT_T(unsigned long, index) \
: (lm) == LM_LONG_LONG \
? GET_ARG_UINT_T(unsigned long long, index) \
: GET_ARG_UINT_T(unsigned int, index))
#define GET_ARG_FLOAT_LM(lm, index) \
((lm) == LM_LONG_DOUBLE ? GET_ARG_FLOAT_T(long double, index) \
: GET_ARG_FLOAT_T(double, index))
// Scans through a format string and determines whether the format uses
// numbered arguments. If so, it returns the highest numbered argument used.
// This can be used to allocate space to store the numbered arguments.
static size_t get_numarg_max(const char_t *format) {
size_t max_numarg = 0;
while (*format != '\0') {
if (*format++ == '%') {
// Numbered argument value.
bool got_numarg = false;
{
size_t numarg = get_numarg(&format);
if (numarg > 0)
got_numarg = true;
if (max_numarg < numarg)
max_numarg = numarg;
}
// Minimum field width and precision.
while (strchr("diouxXfFeEgGaAcspCS%m", *format) == NULL) {
if (*format++ == '*') {
size_t numarg = get_numarg(&format);
if (numarg == 0)
return 0;
if (max_numarg < numarg)
max_numarg = numarg;
}
}
// Non-numbered argument encountered.
if (*format != '%' && *format != 'm' && !got_numarg)
return 0;
}
}
return max_numarg;
}
struct numarg_type {
enum { K_SINT, K_UINT, K_POINTER, K_FLOAT } kind;
enum length_modifier modifier;
};
// Scans through a format string and determines the types of the
// numbered arguments. These tyes can then be used by
// get_numarg_values() to extract the arguments of the call iteratively
// and storing them in an array, so they can be addressed randomly.
static void get_numarg_types(const char_t *format,
struct numarg_type *numarg_types) {
while (*format != '\0') {
if (*format++ == '%') {
size_t arg_value = get_numarg(&format);
#if WIDE
// Skip flags.
format += wcsspn(format, L"'-+ #0");
#else
format += strspn(format, "'-+ #0");
#endif
// Minimum field width.
if (*format == '*') {
++format;
size_t arg_field_width = get_numarg(&format) - 1;
numarg_types[arg_field_width].modifier = LM_DEFAULT;
numarg_types[arg_field_width].kind = K_SINT;
} else {
get_number(&format);
}
// Precision.
if (*format == '.') {
++format;
if (*format == '*') {
++format;
size_t arg_precision = get_numarg(&format) - 1;
numarg_types[arg_precision].modifier = LM_DEFAULT;
numarg_types[arg_precision].kind = K_SINT;
} else {
get_number(&format);
}
}
// Not a numbered argument.
char_t specifier = *format++;
if (arg_value-- == 0)
continue;
// Length modifier.
enum length_modifier length = get_length_modifier(&format);
numarg_types[arg_value].modifier = length;
// Conversion specifiers.
switch (specifier) {
case 'd':
case 'i': {
// Signed decimal integer.
numarg_types[arg_value].kind = K_SINT;
break;
}
case 'o':
case 'u':
case 'x':
case 'X': {
// Unsigned integer types.
numarg_types[arg_value].kind = K_UINT;
break;
}
case 'f':
case 'F':
case 'e':
case 'E':
case 'g':
case 'G':
case 'a':
case 'A': {
// Floating point types.
numarg_types[arg_value].kind = K_FLOAT;
break;
}
case 'c': {
// Character.
if (length == LM_LONG) {
numarg_types[arg_value].kind = K_SINT;
numarg_types[arg_value].modifier = LM_WCHAR;
} else {
numarg_types[arg_value].kind = K_UINT;
numarg_types[arg_value].modifier = LM_CHAR;
}
break;
}
case 'C': {
// Wide character.
numarg_types[arg_value].kind = K_SINT;
numarg_types[arg_value].modifier = LM_WCHAR;
break;
}
case 's':
case 'p':
case 'S': {
// Pointers.
numarg_types[arg_value].kind = K_POINTER;
break;
}
}
}
}
}
union numarg_value {
intmax_t v_sint;
uintmax_t v_uint;
const void *v_pointer;
long double v_float;
};
// Extracts all the arguments in a va_list and stores them in an array
// that can be accessed randomly. It uses the typing information from
// get_numarg_types() to extract values elements with the right size and
// alignment from the va_list.
static void get_numarg_values(size_t numarg_max,
struct numarg_type *numarg_types,
union numarg_value *numarg_values, va_list ap) {
#define GET_ARG_SINT_T(type, index) va_arg(ap, type)
#define GET_ARG_UINT_T(type, index) va_arg(ap, type)
#define GET_ARG_POINTER_T(type, index) va_arg(ap, const type *)
#define GET_ARG_FLOAT_T(type, index) va_arg(ap, type)
for (size_t i = 0; i < numarg_max; ++i) {
switch (numarg_types[i].kind) {
case K_SINT:
numarg_values[i].v_sint = GET_ARG_SINT_LM(numarg_types[i].modifier, i);
break;
case K_UINT:
numarg_values[i].v_uint = GET_ARG_UINT_LM(numarg_types[i].modifier, i);
break;
case K_POINTER:
numarg_values[i].v_pointer = GET_ARG_POINTER_T(void, i);
break;
case K_FLOAT:
numarg_values[i].v_float =
GET_ARG_FLOAT_LM(numarg_types[i].modifier, i);
break;
}
}
#undef GET_ARG_FLOAT_T
#undef GET_ARG_SINT_T
#undef GET_ARG_UINT_T
#undef GET_ARG_POINTER_T
}
#if STYLE == VASPRINTF
int NAME(char_t **s, locale_t locale, const char_t *format, va_list ap) {
// Already preallocate a buffer of 16 bytes.
size_t resultlen = 16 / sizeof(char_t);
char_t *result = malloc(resultlen * sizeof(char_t));
if (result == NULL)
return -1;
size_t resultstored = 0;
#define PUTCHAR(c) \
do { \
if (resultstored == resultlen) { \
resultlen *= 2; \
char_t *newresult = realloc(result, resultlen * sizeof(char_t)); \
if (newresult == NULL) { \
free(result); \
return -1; \
} \
result = newresult; \
} \
result[resultstored++] = (c); \
} while (0)
#elif STYLE == VDPRINTF
int NAME(int fd, locale_t locale, const char_t *format, va_list ap) {
char result[BUFSIZ];
size_t resultstored = 0;
size_t resultwritten = 0;
#if WIDE
static const mbstate_t initial_mbstate;
mbstate_t resultmbs = initial_mbstate;
#define PUTCHAR(c) \
do { \
size_t k = wcrtomb_l(result + resultstored, c, &resultmbs, locale); \
if (k == (size_t)-1) \
return -1; \
resultstored += k; \
while (resultstored > sizeof(result) - MB_LEN_MAX) { \
ssize_t l = write(fd, result, resultstored); \
if (l == -1) \
return -1; \
memmove(result, result + l, resultstored - l); \
resultstored -= l; \
} \
++resultwritten; \
} while (0)
#else
#define PUTCHAR(c) \
do { \
result[resultstored++] = (c); \
while (resultstored == sizeof(result)) { \
ssize_t l = write(fd, result, sizeof(result)); \
if (l == -1) \
return -1; \
memmove(result, result + l, resultstored - l); \
resultstored -= l; \
} \
++resultwritten; \
} while (0)
#endif
#elif STYLE == VFPRINTF
int NAME(FILE *stream, locale_t locale, const char_t *format, va_list ap) {
size_t resultwritten = 0;
flockfile_orientation(stream, WIDE ? 1 : -1);
#if WIDE
#define PUTCHAR(c) \
do { \
if (putwc_unlocked(c, stream) == WEOF) { \
funlockfile(stream); \
return -1; \
} \
++resultwritten; \
} while (0)
#else
#define PUTCHAR(c) \
do { \
if (putc_unlocked(c, stream) == EOF) { \
funlockfile(stream); \
return -1; \
} \
++resultwritten; \
} while (0)
#endif
#elif STYLE == VSNPRINTF
int NAME(char_t *s, size_t n, locale_t locale, const char_t *format,
va_list ap) {
size_t resultstored = 0;
#define PUTCHAR(c) \
do { \
char_t ch = (c); \
if (resultstored + 1 < n) \
s[resultstored] = ch; \
++resultstored; \
} while (0)
#else
#error "Unknown style"
#endif
// Save current errno for %m.
int saved_errno = errno;
size_t numarg_max = get_numarg_max(format);
if (numarg_max > 0) {
// Numbered arguments require us to construct a table with all
// argument values explicitly, as va_lists cannot be accessed
// randomly. Furthermore, we cannot iterate over the va_list without
// knowing the exact type of the parameters.
union numarg_value numarg_values[numarg_max];
{
// Start out by inferring the type of all the parameters.
struct numarg_type numarg_types[numarg_max];
get_numarg_types(format, numarg_types);
// Now iterate over the va_list to extract the values using the
// proper type.
get_numarg_values(numarg_max, numarg_types, numarg_values, ap);
}
#define PARSE_ARGNUM(field) size_t field = get_numarg(&format) - 1
#define GET_ARG_SINT_T(type, index) ((type)numarg_values[index].v_sint)
#define GET_ARG_UINT_T(type, index) ((type)numarg_values[index].v_uint)
#define GET_ARG_POINTER_T(type, index) \
((const type *)numarg_values[index].v_pointer)
#define GET_ARG_FLOAT_T(type, index) ((type)numarg_values[index].v_float)
#define LABEL(n) n##_1
#include "vprintf_body.h"
#undef PARSE_ARGNUM
#undef GET_ARG_SINT_T
#undef GET_ARG_UINT_T
#undef GET_ARG_POINTER_T
#undef GET_ARG_FLOAT_T
#undef LABEL
} else {
#define PARSE_ARGNUM(field)
#define GET_ARG_SINT_T(type, index) va_arg(ap, type)
#define GET_ARG_UINT_T(type, index) va_arg(ap, type)
#define GET_ARG_POINTER_T(type, index) va_arg(ap, const type *)
#define GET_ARG_FLOAT_T(type, index) va_arg(ap, type)
#define LABEL(n) n##_2
#include "vprintf_body.h"
#undef PARSE_ARGNUM
#undef GET_ARG_SINT_T
#undef GET_ARG_UINT_T
#undef GET_ARG_POINTER_T
#undef GET_ARG_FLOAT_T
#undef LABEL
}
#if STYLE == VASPRINTF
// Nul-terminate the buffer.
PUTCHAR('\0');
*s = result;
return resultstored - 1;
bad:
free(result);
return -1;
#elif STYLE == VDPRINTF
while (resultstored > 0) {
ssize_t l = write(fd, result, resultstored);
if (l == -1)
return -1;
memmove(result, result + l, resultstored - l);
resultstored -= l;
}
return resultwritten;
bad:
return -1;
#elif STYLE == VFPRINTF
funlockfile(stream);
return resultwritten;
bad:
funlockfile(stream);
return -1;
#elif STYLE == VSNPRINTF
// Nul-terminate the buffer, if provided.
if (n > 0)
s[resultstored < n - 1 ? resultstored : n - 1] = '\0';
return resultstored;
bad:
return -1;
#endif
}