1 --- vfwprintf.c.orig 2004-11-25 11:38:36.000000000 -0800
2 +++ vfwprintf.c 2005-02-24 15:17:14.000000000 -0800
5 __FBSDID("$FreeBSD: src/lib/libc/stdio/vfwprintf.c,v 1.23 2004/08/26 06:25:28 des Exp $");
7 +#include "xlocale_private.h"
10 * Actual wprintf innards.
17 #include "un-namespace.h"
19 #include "libc_private.h"
24 +#include <machine/cpu_capabilities.h>
26 +#define VECTORTYPE vector unsigned char
37 + VECTORTYPE vectorarg;
38 + unsigned char vuchararg[16];
39 + signed char vchararg[16];
40 + unsigned short vushortarg[8];
41 + signed short vshortarg[8];
42 + unsigned int vuintarg[4];
43 + signed int vintarg[4];
50 T_LONG, T_U_LONG, TP_LONG, T_LLONG, T_U_LLONG, TP_LLONG,
51 T_PTRDIFFT, TP_PTRDIFFT, T_SIZET, TP_SIZET,
52 T_INTMAXT, T_UINTMAXT, TP_INTMAXT, TP_VOID, TP_CHAR, TP_SCHAR,
54 + T_DOUBLE, T_LONG_DOUBLE, T_WINT, TP_WCHAR, T_VECTOR
55 +#else /* ! ALTIVEC */
56 T_DOUBLE, T_LONG_DOUBLE, T_WINT, TP_WCHAR
60 -static int __sbprintf(FILE *, const wchar_t *, va_list);
61 -static wint_t __xfputwc(wchar_t, FILE *);
62 +static int __sbprintf(FILE *, locale_t, const wchar_t *, va_list);
63 +static wint_t __xfputwc(wchar_t, FILE *, locale_t);
64 static wchar_t *__ujtoa(uintmax_t, wchar_t *, int, int, const char *, int,
66 static wchar_t *__ultoa(u_long, wchar_t *, int, int, const char *, int,
68 -static wchar_t *__mbsconv(char *, int);
69 +static wchar_t *__mbsconv(char *, int, locale_t);
70 static void __find_arguments(const wchar_t *, 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 * Helper function for `fprintf to unbuffered unix file': creates a
108 * temporary buffer. We only work on write-only files; this avoids
109 * worries about ungetc buffers and so forth.
112 -__sbprintf(FILE *fp, const wchar_t *fmt, va_list ap)
113 +__sbprintf(FILE *fp, locale_t loc, const wchar_t *fmt, va_list ap)
118 fake._lbfsize = 0; /* not actually used, but Just In Case */
120 /* do the work, then copy any error status */
121 - ret = __vfwprintf(&fake, fmt, ap);
122 + ret = __vfwprintf(&fake, loc, fmt, ap);
123 if (ret >= 0 && __fflush(&fake))
125 if (fake._flags & __SERR)
127 * File must already be locked.
130 -__xfputwc(wchar_t wc, FILE *fp)
131 +__xfputwc(wchar_t wc, FILE *fp, locale_t loc)
133 static const mbstate_t initial;
135 @@ -167,10 +223,10 @@
138 if ((fp->_flags & __SSTR) == 0)
139 - return (__fputwc(wc, fp));
140 + return (__fputwc(wc, fp, loc));
143 - if ((len = wcrtomb(buf, wc, &mbs)) == (size_t)-1) {
144 + if ((len = wcrtomb_l(buf, wc, &mbs, loc)) == (size_t)-1) {
145 fp->_flags |= __SERR;
148 @@ -350,13 +406,14 @@
149 * that the multibyte char. string ends in a null character.
152 -__mbsconv(char *mbsarg, int prec)
153 +__mbsconv(char *mbsarg, int prec, locale_t loc)
155 static const mbstate_t initial;
157 wchar_t *convbuf, *wcp;
159 size_t insize, nchars, nconv;
160 + int mb_cur_max = MB_CUR_MAX_L(loc);
167 while (nchars != (size_t)prec) {
168 - nconv = mbrlen(p, MB_CUR_MAX, &mbs);
169 + nconv = mbrlen_l(p, mb_cur_max, &mbs, loc);
170 if (nconv == 0 || nconv == (size_t)-1 ||
176 while (insize != 0) {
177 - nconv = mbrtowc(wcp, p, insize, &mbs);
178 + nconv = mbrtowc_l(wcp, p, insize, &mbs, loc);
179 if (nconv == 0 || nconv == (size_t)-1 || nconv == (size_t)-2)
186 - ret = __vfwprintf(fp, fmt0, ap);
187 + ret = __vfwprintf(fp, __current_locale(), fmt0, ap);
193 +vfwprintf_l(FILE * __restrict fp, locale_t loc, const wchar_t * __restrict fmt0,
199 + NORMALIZE_LOCALE(loc);
201 + ret = __vfwprintf(fp, loc, fmt0, ap);
205 @@ -474,12 +545,15 @@
206 #define PTRDIFFT 0x800 /* ptrdiff_t */
207 #define INTMAXT 0x1000 /* intmax_t */
208 #define CHARINT 0x2000 /* print char using int format */
210 +#define VECTOR 0x4000 /* Altivec vector */
211 +#endif /* ALTIVEC */
214 * Non-MT-safe version
217 -__vfwprintf(FILE *fp, const wchar_t *fmt0, va_list ap)
218 +__private_extern__ int
219 +__vfwprintf(FILE *fp, locale_t loc, const wchar_t *fmt0, va_list ap)
221 wchar_t *fmt; /* format string */
222 wchar_t ch; /* character from fmt */
224 int nseps; /* number of group separators with ' */
225 int nrepeats; /* number of repeats of the last group */
228 + union arg vval; /* Vector argument. */
229 + wchar_t *pct; /* Pointer to '%' at beginning of specifier. */
230 + wchar_t vsep; /* Vector separator character. */
232 u_long ulval; /* integer arguments %[diouxX] */
233 uintmax_t ujval; /* %j, %ll, %q, %t, %z integers */
234 int base; /* base for [diouxX] conversion */
237 #define PRINT(ptr, len) do { \
238 for (n3 = 0; n3 < (len); n3++) \
239 - __xfputwc((ptr)[n3], fp); \
240 + __xfputwc((ptr)[n3], fp, loc); \
242 #define PAD(howmany, with) do { \
243 if ((n = (howmany)) > 0) { \
248 - * Get the argument indexed by nextarg. If the argument table is
249 - * built, use it to get the argument. If its not, get the next
250 - * argument (and arguments must be gotten sequentially).
252 -#define GETARG(type) \
253 - ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
254 - (nextarg++, va_arg(ap, type)))
257 * To extend shorts properly, we need both signed and unsigned
258 * argument extraction methods.
260 @@ -640,21 +710,22 @@
261 val = GETARG (int); \
265 thousands_sep = '\0';
267 #ifndef NO_FLOATING_POINT
268 - decimal_point = localeconv()->decimal_point;
269 + decimal_point = localeconv_l(loc)->decimal_point;
272 /* sorry, fwprintf(read_only_file, L"") returns WEOF, not 0 */
273 - if (prepwrite(fp) != 0)
274 + if (prepwrite(fp) != 0) {
279 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
280 if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
282 - return (__sbprintf(fp, fmt0, ap));
283 + return (__sbprintf(fp, loc, fmt0, ap));
285 fmt = (wchar_t *)fmt0;
293 +#endif /* ALTIVEC */
294 fmt++; /* skip over '%' */
302 + vsep = 'X'; /* Illegal value, changed to defaults later. */
303 +#endif /* ALTIVEC */
306 reswitch: switch (ch) {
312 + case ',': case ';': case ':': case '_':
315 +#endif /* ALTIVEC */
318 * ``A negative field width argument is taken as a
323 - thousands_sep = *(localeconv()->thousands_sep);
324 - grouping = localeconv()->grouping;
325 + thousands_sep = *(localeconv_l(loc)->thousands_sep);
326 + grouping = localeconv_l(loc)->grouping;
329 if ((ch = *fmt++) == '*') {
330 @@ -796,10 +878,16 @@
335 + if (flags & VECTOR) {
339 +#endif /* ALTIVEC */
341 *(cp = buf) = (wchar_t)GETARG(wint_t);
343 - *(cp = buf) = (wchar_t)btowc(GETARG(int));
344 + *(cp = buf) = (wchar_t)btowc_l(GETARG(int), loc);
353 + if (flags & VECTOR) {
357 +#endif /* ALTIVEC */
358 if (flags & INTMAX_SIZE) {
360 if ((intmax_t)ujval < 0) {
366 + fparg.dbl = GETARG(double);
368 + __hdtoa(fparg.dbl, xdigs, prec,
369 + &expt, &signflag, &dtoaend);
370 +#else /* !LDBL_COMPAT */
371 if (flags & LONGDBL) {
372 fparg.ldbl = GETARG(long double);
375 __hdtoa(fparg.dbl, xdigs, prec,
376 &expt, &signflag, &dtoaend);
378 +#endif /* LDBL_COMPAT */
380 prec = dtoaend - dtoaresult;
385 ndig = dtoaend - dtoaresult;
386 - cp = convbuf = __mbsconv(dtoaresult, -1);
387 + cp = convbuf = __mbsconv(dtoaresult, -1, loc);
388 freedtoa(dtoaresult);
391 @@ -868,10 +969,24 @@
396 + if (flags & VECTOR) {
401 +#endif /* ALTIVEC */
407 + if (flags & VECTOR) {
412 +#endif /* ALTIVEC */
413 expchar = ch - ('g' - 'e');
421 + fparg.dbl = GETARG(double);
423 + dtoa(fparg.dbl, expchar ? 2 : 3, prec,
424 + &expt, &signflag, &dtoaend);
427 +#else /* !LDBL_COMPAT */
428 if (flags & LONGDBL) {
429 fparg.ldbl = GETARG(long double);
435 +#endif /* LDBL_COMPAT */
436 ndig = dtoaend - dtoaresult;
437 - cp = convbuf = __mbsconv(dtoaresult, -1);
438 + cp = convbuf = __mbsconv(dtoaresult, -1, loc);
439 freedtoa(dtoaresult);
442 @@ -989,6 +1113,12 @@
447 + if (flags & VECTOR) {
451 +#endif /* ALTIVEC */
452 if (flags & INTMAX_SIZE)
455 @@ -1003,6 +1133,12 @@
460 + if (flags & VECTOR) {
464 +#endif /* ALTIVEC */
465 ujval = (uintmax_t)(uintptr_t)GETARG(void *);
468 @@ -1024,7 +1160,7 @@
469 if ((mbp = GETARG(char *)) == NULL)
472 - convbuf = __mbsconv(mbp, prec);
473 + convbuf = __mbsconv(mbp, prec, loc);
474 if (convbuf == NULL) {
475 fp->_flags |= __SERR;
477 @@ -1055,6 +1191,12 @@
482 + if (flags & VECTOR) {
486 +#endif /* ALTIVEC */
487 if (flags & INTMAX_SIZE)
490 @@ -1067,6 +1209,12 @@
495 + if (flags & VECTOR) {
499 +#endif /* ALTIVEC */
500 if (flags & INTMAX_SIZE)
503 @@ -1111,6 +1259,14 @@
504 if (size > BUF) /* should never happen */
514 +#endif /* ALTIVEC */
515 default: /* "%?" prints ?, unless ? is NUL */
518 @@ -1122,6 +1278,183 @@
523 + if (flags & VECTOR) {
525 + * Do the minimum amount of work necessary to construct
526 + * a format specifier that can be used to recursively
527 + * call vfprintf() for each element in the vector.
529 + int i, j; /* Counter. */
530 + int vcnt; /* Number of elements in vector. */
531 + char *vfmt; /* Pointer to format specifier. */
532 + char vfmt_buf[32]; /* Static buffer for format spec. */
533 + int vwidth = 0; /* Width specified via '*'. */
534 + int vprec = 0; /* Precision specified via '*'. */
535 + union { /* Element. */
539 + char *vstr; /* Used for asprintf(). */
540 + int vlen; /* Length returned by asprintf(). */
543 + * Set vfmt. If vfmt_buf may not be big enough,
544 + * malloc() space, taking care to free it later.
546 + if (&fmt[-1] - pct < sizeof(vfmt_buf))
549 + vfmt = (char *)malloc(&fmt[-1] - pct + 1);
551 + /* Set the separator character, if not specified. */
559 + /* Create the format specifier. */
560 + for (i = j = 0; i < &fmt[-1] - pct; i++) {
562 + case ',': case ';': case ':': case '_':
563 + case 'v': case 'h': case 'l':
567 + if (pct[i - 1] != '.')
573 + vfmt[j++] = pct[i];
578 + * Determine the number of elements in the vector and
579 + * finish up the format specifier.
581 + if (flags & SHORTINT) {
585 + } else if (flags & LONGINT) {
602 + * The default case should never
619 +/* Get a vector element. */
620 +#define VPRINT(cnt, ind, args...) do { \
621 + if (flags & FPT) { \
622 + velm.f = vval.vfloatarg[ind]; \
623 + vlen = asprintf_l(&vstr, loc, vfmt , ## args, velm.f); \
627 + /* The default case should never happen. */ \
629 + velm.i = (unsigned)vval.vintarg[ind]; \
632 + velm.i = (unsigned short)vval.vshortarg[ind]; \
635 + velm.i = (unsigned char)vval.vchararg[ind]; \
638 + vlen = asprintf_l(&vstr, loc, vfmt , ## args, velm.i); \
641 + PRINT(vstr, vlen); \
645 + /* Actually print. */
648 + /* First element. */
650 + for (i = 1; i < vcnt; i++) {
658 + /* First element. */
659 + VPRINT(vcnt, 0, prec);
660 + for (i = 1; i < vcnt; i++) {
665 + VPRINT(vcnt, i, prec);
670 + /* First element. */
671 + VPRINT(vcnt, 0, width);
672 + for (i = 1; i < vcnt; i++) {
677 + VPRINT(vcnt, i, width);
680 + /* First element. */
681 + VPRINT(vcnt, 0, width, prec);
682 + for (i = 1; i < vcnt; i++) {
687 + VPRINT(vcnt, i, width, prec);
693 + if (vfmt != vfmt_buf)
698 +#endif /* ALTIVEC */
700 * All reasonable formats wind up here. At this point, `cp'
701 * points to a string which (if not flags&LADJUST) should be
702 @@ -1401,6 +1734,11 @@
707 + if (flags & VECTOR)
710 +#endif /* ALTIVEC */
714 @@ -1418,6 +1756,11 @@
719 + if (flags & VECTOR)
722 +#endif /* ALTIVEC */
724 ADDTYPE(T_LONG_DOUBLE);
726 @@ -1446,9 +1789,19 @@
731 + if (flags & VECTOR)
734 +#endif /* ALTIVEC */
739 + if (flags & VECTOR)
742 +#endif /* ALTIVEC */
746 @@ -1466,6 +1819,11 @@
751 + if (flags & VECTOR)
754 +#endif /* ALTIVEC */
757 default: /* "%?" prints ?, unless ? is NUL */
758 @@ -1532,7 +1890,7 @@
759 (*argtable) [n].sizearg = va_arg (ap, size_t);
762 - (*argtable) [n].psizearg = va_arg (ap, ssize_t *);
763 + (*argtable) [n].psizearg = va_arg (ap, size_t *);
766 (*argtable) [n].intmaxarg = va_arg (ap, intmax_t);
767 @@ -1551,6 +1909,12 @@
768 (*argtable) [n].longdoublearg = va_arg (ap, long double);
774 + ap = getvec( &((*argtable) [n]), NULL, 0, ap );
776 +#endif /* ALTIVEC */
778 (*argtable) [n].pchararg = va_arg (ap, char *);