1 --- vfprintf.c.orig 2004-11-25 11:38:35.000000000 -0800
2 +++ vfprintf.c 2005-02-24 15:16:20.000000000 -0800
5 __FBSDID("$FreeBSD: src/lib/libc/stdio/vfprintf.c,v 1.68 2004/08/26 06:25:28 des Exp $");
7 +#include "xlocale_private.h"
10 * Actual printf innards.
19 #include "un-namespace.h"
25 +#include <machine/cpu_capabilities.h>
27 +#define VECTORTYPE vector unsigned char
38 + VECTORTYPE vectorarg;
39 + unsigned char vuchararg[16];
40 + signed char vchararg[16];
41 + unsigned short vushortarg[8];
42 + signed short vshortarg[8];
43 + unsigned int vuintarg[4];
44 + signed int vintarg[4];
51 T_LONG, T_U_LONG, TP_LONG, T_LLONG, T_U_LLONG, TP_LLONG,
52 T_PTRDIFFT, TP_PTRDIFFT, T_SIZET, TP_SIZET,
53 T_INTMAXT, T_UINTMAXT, TP_INTMAXT, TP_VOID, TP_CHAR, TP_SCHAR,
55 + T_DOUBLE, T_LONG_DOUBLE, T_WINT, TP_WCHAR, T_VECTOR
56 +#else /* ! ALTIVEC */
57 T_DOUBLE, T_LONG_DOUBLE, T_WINT, TP_WCHAR
61 static int __sprint(FILE *, struct __suio *);
62 -static int __sbprintf(FILE *, const char *, va_list) __printflike(2, 0);
63 +static int __sbprintf(FILE *, locale_t, const char *, va_list) __printflike(3, 0);
64 static char *__ujtoa(uintmax_t, char *, int, int, const char *, int, char,
66 static char *__ultoa(u_long, char *, int, int, const char *, int, char,
68 -static char *__wcsconv(wchar_t *, int);
69 +static char *__wcsconv(wchar_t *, int, locale_t);
70 static void __find_arguments(const char *, va_list, union arg **);
71 static void __grow_type_table(int, enum typeid **, int *);
74 + * Get the argument indexed by nextarg. If the argument table is
75 + * built, use it to get the argument. If its not, get the next
76 + * argument (and arguments must be gotten sequentially).
78 +#define GETARG(type) \
79 + ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
80 + (nextarg++, va_arg(ap, type)))
83 +#define hasAltivec (_cpu_capabilities & kHasAltivec)
84 +/*-----------------------------------------------------------------------
85 + * getvec() must be a real subroutine. If it is a #define, then __vfprintf()
86 + * would have its calling sequence changed by Altivec so that a non-Altivec
87 + * processor would crash on illegal instruction. By isolating the calling
88 + * sequence in getvec(), __vprintf() is callable by a non-Altivec processor.
89 + *-----------------------------------------------------------------------*/
90 +static va_list getvec(union arg *, const union arg *, int, va_list) __attribute__((noinline));
93 +getvec(union arg *dst, const union arg *argtable, int nextarg, va_list ap)
95 + dst->vectorarg = GETARG(VECTORTYPE);
99 +#define SETVEC(dst) \
101 + ap = getvec(&dst, argtable, nextarg, ap); \
104 +#endif /* ALTIVEC */
107 * Flush out all the vectors defined by the given uio,
108 * then reset it so that it can be reused.
110 * worries about ungetc buffers and so forth.
113 -__sbprintf(FILE *fp, const char *fmt, va_list ap)
114 +__sbprintf(FILE *fp, locale_t loc, const char *fmt, va_list ap)
119 fake._lbfsize = 0; /* not actually used, but Just In Case */
121 /* do the work, then copy any error status */
122 - ret = __vfprintf(&fake, fmt, ap);
123 + ret = __vfprintf(&fake, loc, fmt, ap);
124 if (ret >= 0 && __fflush(&fake))
126 if (fake._flags & __SERR)
128 * that the wide char. string ends in a null character.
131 -__wcsconv(wchar_t *wcsarg, int prec)
132 +__wcsconv(wchar_t *wcsarg, int prec, locale_t loc)
134 static const mbstate_t initial;
140 - clen = wcrtomb(buf, *p++, &mbs);
141 + clen = wcrtomb_l(buf, *p++, &mbs, loc);
142 if (clen == 0 || clen == (size_t)-1 ||
143 nbytes + clen > prec)
149 - nbytes = wcsrtombs(NULL, (const wchar_t **)&p, 0, &mbs);
150 + nbytes = wcsrtombs_l(NULL, (const wchar_t **)&p, 0, &mbs, loc);
151 if (nbytes == (size_t)-1)
157 while (mbp - convbuf < nbytes) {
158 - clen = wcrtomb(mbp, *p++, &mbs);
159 + clen = wcrtomb_l(mbp, *p++, &mbs, loc);
160 if (clen == 0 || clen == (size_t)-1)
167 - ret = __vfprintf(fp, fmt0, ap);
168 + ret = __vfprintf(fp, __current_locale(), fmt0, ap);
174 +vfprintf_l(FILE * __restrict fp, locale_t loc, const char * __restrict fmt0,
180 + NORMALIZE_LOCALE(loc);
182 + ret = __vfprintf(fp, loc, fmt0, ap);
186 @@ -451,12 +521,15 @@
187 #define PTRDIFFT 0x800 /* ptrdiff_t */
188 #define INTMAXT 0x1000 /* intmax_t */
189 #define CHARINT 0x2000 /* print char using int format */
191 +#define VECTOR 0x4000 /* Altivec vector */
192 +#endif /* ALTIVEC */
195 * Non-MT-safe version
198 -__vfprintf(FILE *fp, const char *fmt0, va_list ap)
199 +__private_extern__ int
200 +__vfprintf(FILE *fp, locale_t loc, const char *fmt0, va_list ap)
202 char *fmt; /* format string */
203 int ch; /* character from fmt */
205 int nseps; /* number of group separators with ' */
206 int nrepeats; /* number of repeats of the last group */
209 + union arg vval; /* Vector argument. */
210 + char *pct; /* Pointer to '%' at beginning of specifier. */
211 + char vsep; /* Vector separator character. */
213 u_long ulval; /* integer arguments %[diouxX] */
214 uintmax_t ujval; /* %j, %ll, %q, %t, %z integers */
215 int base; /* base for [diouxX] conversion */
220 - * Get the argument indexed by nextarg. If the argument table is
221 - * built, use it to get the argument. If its not, get the next
222 - * argument (and arguments must be gotten sequentially).
224 -#define GETARG(type) \
225 - ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
226 - (nextarg++, va_arg(ap, type)))
229 * To extend shorts properly, we need both signed and unsigned
230 * argument extraction methods.
232 @@ -633,22 +702,23 @@
233 val = GETARG (int); \
237 thousands_sep = '\0';
240 #ifndef NO_FLOATING_POINT
242 - decimal_point = localeconv()->decimal_point;
243 + decimal_point = localeconv_l(loc)->decimal_point;
245 /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
246 - if (prepwrite(fp) != 0)
247 + if (prepwrite(fp) != 0) {
252 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
253 if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
255 - return (__sbprintf(fp, fmt0, ap));
256 + return (__sbprintf(fp, loc, fmt0, ap));
266 +#endif /* ALTIVEC */
267 fmt++; /* skip over '%' */
275 + vsep = 'X'; /* Illegal value, changed to defaults later. */
276 +#endif /* ALTIVEC */
279 reswitch: switch (ch) {
285 + case ',': case ';': case ':': case '_':
288 +#endif /* ALTIVEC */
291 * ``A negative field width argument is taken as a
296 - thousands_sep = *(localeconv()->thousands_sep);
297 - grouping = localeconv()->grouping;
298 + thousands_sep = *(localeconv_l(loc)->thousands_sep);
299 + grouping = localeconv_l(loc)->grouping;
302 if ((ch = *fmt++) == '*') {
303 @@ -793,14 +874,20 @@
308 + if (flags & VECTOR) {
312 +#endif /* ALTIVEC */
313 if (flags & LONGINT) {
314 static const mbstate_t initial;
319 - mbseqlen = wcrtomb(cp = buf,
320 - (wchar_t)GETARG(wint_t), &mbs);
321 + mbseqlen = wcrtomb_l(cp = buf,
322 + (wchar_t)GETARG(wint_t), &mbs, loc);
323 if (mbseqlen == (size_t)-1) {
324 fp->_flags |= __SERR;
331 + if (flags & VECTOR) {
335 +#endif /* ALTIVEC */
336 if (flags & INTMAX_SIZE) {
338 if ((intmax_t)ujval < 0) {
340 #ifndef NO_FLOATING_POINT
344 + if (flags & VECTOR) {
349 +#endif /* ALTIVEC */
355 if (dtoaresult != NULL)
356 freedtoa(dtoaresult);
358 + fparg.dbl = GETARG(double);
360 + __hdtoa(fparg.dbl, xdigs, prec,
361 + &expt, &signflag, &dtoaend);
362 +#else /* !LDBL_COMPAT */
363 if (flags & LONGDBL) {
364 fparg.ldbl = GETARG(long double);
367 __hdtoa(fparg.dbl, xdigs, prec,
368 &expt, &signflag, &dtoaend);
370 +#endif /* LDBL_COMPAT */
379 + if (flags & VECTOR) {
384 +#endif /* ALTIVEC */
386 if (prec < 0) /* account for digit before decpt */
388 @@ -874,10 +988,24 @@
393 + if (flags & VECTOR) {
398 +#endif /* ALTIVEC */
404 + if (flags & VECTOR) {
409 +#endif /* ALTIVEC */
410 expchar = ch - ('g' - 'e');
413 @@ -886,6 +1014,14 @@
415 if (dtoaresult != NULL)
416 freedtoa(dtoaresult);
418 + fparg.dbl = GETARG(double);
420 + dtoa(fparg.dbl, expchar ? 2 : 3, prec,
421 + &expt, &signflag, &dtoaend);
424 +#else /* !LDBL_COMPAT */
425 if (flags & LONGDBL) {
426 fparg.ldbl = GETARG(long double);
432 +#endif /* LDBL_COMPAT */
436 @@ -993,6 +1130,12 @@
441 + if (flags & VECTOR) {
445 +#endif /* ALTIVEC */
446 if (flags & INTMAX_SIZE)
449 @@ -1007,6 +1150,12 @@
454 + if (flags & VECTOR) {
458 +#endif /* ALTIVEC */
459 ujval = (uintmax_t)(uintptr_t)GETARG(void *);
462 @@ -1025,7 +1174,7 @@
463 if ((wcp = GETARG(wchar_t *)) == NULL)
466 - convbuf = __wcsconv(wcp, prec);
467 + convbuf = __wcsconv(wcp, prec, loc);
468 if (convbuf == NULL) {
469 fp->_flags |= __SERR;
471 @@ -1056,6 +1205,12 @@
476 + if (flags & VECTOR) {
480 +#endif /* ALTIVEC */
481 if (flags & INTMAX_SIZE)
484 @@ -1068,6 +1223,12 @@
489 + if (flags & VECTOR) {
493 +#endif /* ALTIVEC */
494 if (flags & INTMAX_SIZE)
497 @@ -1112,6 +1273,14 @@
498 if (size > BUF) /* should never happen */
508 +#endif /* ALTIVEC */
509 default: /* "%?" prints ?, unless ? is NUL */
512 @@ -1123,6 +1292,184 @@
517 + if (flags & VECTOR) {
519 + * Do the minimum amount of work necessary to construct
520 + * a format specifier that can be used to recursively
521 + * call vfprintf() for each element in the vector.
523 + int i, j; /* Counter. */
524 + int vcnt; /* Number of elements in vector. */
525 + char *vfmt; /* Pointer to format specifier. */
526 + char vfmt_buf[32]; /* Static buffer for format spec. */
527 + int vwidth = 0; /* Width specified via '*'. */
528 + int vprec = 0; /* Precision specified via '*'. */
529 + union { /* Element. */
533 + char *vstr; /* Used for asprintf(). */
534 + int vlen; /* Length returned by asprintf(). */
537 + * Set vfmt. If vfmt_buf may not be big enough,
538 + * malloc() space, taking care to free it later.
540 + if (&fmt[-1] - pct < sizeof(vfmt_buf))
543 + vfmt = (char *)malloc(&fmt[-1] - pct + 1);
545 + /* Set the separator character, if not specified. */
553 + /* Create the format specifier. */
554 + for (i = j = 0; i < &fmt[-1] - pct; i++) {
556 + case ',': case ';': case ':': case '_':
557 + case 'v': case 'h': case 'l':
561 + if (pct[i - 1] != '.')
567 + vfmt[j++] = pct[i];
572 + * Determine the number of elements in the vector and
573 + * finish up the format specifier.
575 + if (flags & SHORTINT) {
579 + } else if (flags & LONGINT) {
596 + * The default case should never
613 +/* Get a vector element. */
614 +#define VPRINT(cnt, ind, args...) do { \
615 + if (flags & FPT) { \
616 + velm.f = vval.vfloatarg[ind]; \
617 + vlen = asprintf_l(&vstr, loc, vfmt , ## args, velm.f); \
621 + /* The default case should never happen. */ \
623 + velm.i = (unsigned)vval.vintarg[ind]; \
626 + velm.i = (unsigned short)vval.vshortarg[ind]; \
629 + velm.i = (unsigned char)vval.vchararg[ind]; \
632 + vlen = asprintf_l(&vstr, loc, vfmt , ## args, velm.i); \
635 + PRINT(vstr, vlen); \
640 + /* Actually print. */
643 + /* First element. */
645 + for (i = 1; i < vcnt; i++) {
653 + /* First element. */
654 + VPRINT(vcnt, 0, prec);
655 + for (i = 1; i < vcnt; i++) {
660 + VPRINT(vcnt, i, prec);
665 + /* First element. */
666 + VPRINT(vcnt, 0, width);
667 + for (i = 1; i < vcnt; i++) {
672 + VPRINT(vcnt, i, width);
675 + /* First element. */
676 + VPRINT(vcnt, 0, width, prec);
677 + for (i = 1; i < vcnt; i++) {
682 + VPRINT(vcnt, i, width, prec);
688 + if (vfmt != vfmt_buf)
693 +#endif /* ALTIVEC */
695 * All reasonable formats wind up here. At this point, `cp'
696 * points to a string which (if not flags&LADJUST) should be
697 @@ -1406,6 +1753,11 @@
702 + if (flags & VECTOR)
705 +#endif /* ALTIVEC */
709 @@ -1413,6 +1765,11 @@
714 + if (flags & VECTOR)
720 #ifndef NO_FLOATING_POINT
721 @@ -1423,6 +1780,11 @@
726 + if (flags & VECTOR)
729 +#endif /* ALTIVEC */
731 ADDTYPE(T_LONG_DOUBLE);
733 @@ -1451,9 +1813,19 @@
738 + if (flags & VECTOR)
741 +#endif /* ALTIVEC */
746 + if (flags & VECTOR)
749 +#endif /* ALTIVEC */
753 @@ -1471,6 +1843,11 @@
758 + if (flags & VECTOR)
761 +#endif /* ALTIVEC */
764 default: /* "%?" prints ?, unless ? is NUL */
765 @@ -1537,7 +1914,7 @@
766 (*argtable) [n].sizearg = va_arg (ap, size_t);
769 - (*argtable) [n].psizearg = va_arg (ap, ssize_t *);
770 + (*argtable) [n].psizearg = va_arg (ap, size_t *);
773 (*argtable) [n].intmaxarg = va_arg (ap, intmax_t);
774 @@ -1556,6 +1933,12 @@
775 (*argtable) [n].longdoublearg = va_arg (ap, long double);
781 + ap = getvec( &((*argtable) [n]), NULL, 0, ap );
783 +#endif /* ALTIVEC */
785 (*argtable) [n].pchararg = va_arg (ap, char *);