diff --git a/src/kernel/libroot/posix/glibc/stdio-common/Jamfile b/src/kernel/libroot/posix/glibc/stdio-common/Jamfile index dc04eb9b2c..6ce7344b06 100644 --- a/src/kernel/libroot/posix/glibc/stdio-common/Jamfile +++ b/src/kernel/libroot/posix/glibc/stdio-common/Jamfile @@ -1,9 +1,11 @@ SubDir OBOS_TOP src kernel libroot posix glibc stdio-common ; -SubDirHdrs $(OBOS_TOP) src kernel libroot posix glibc include arch $(OBOS_ARCH) ; -SubDirHdrs $(OBOS_TOP) src kernel libroot posix glibc stdio-common ; -SubDirHdrs $(OBOS_TOP) src kernel libroot posix glibc include ; +SubDirCcFlags -D_GNU_SOURCE -DUSE_IN_LIBIO ; + SubDirHdrs $(OBOS_TOP) src kernel libroot posix glibc libio ; +SubDirHdrs $(OBOS_TOP) src kernel libroot posix glibc include arch $(OBOS_ARCH) ; +SubDirHdrs $(OBOS_TOP) src kernel libroot posix glibc include ; +SubDirHdrs $(OBOS_TOP) src kernel libroot posix glibc stdio-common ; SubDirHdrs $(OBOS_TOP) src kernel libroot posix glibc locale ; SubDirHdrs $(OBOS_TOP) src kernel libroot posix glibc ; @@ -11,7 +13,7 @@ SubDirHdrs $(OBOS_TOP) src kernel libroot posix glibc ; KernelMergeObject posix_gnu_stdio.o : #_common.o : <$(SOURCE_GRIST)>_itoa.c -# <$(SOURCE_GRIST)>_itowa.c + <$(SOURCE_GRIST)>_itowa.c <$(SOURCE_GRIST)>asprintf.c <$(SOURCE_GRIST)>dprintf.c <$(SOURCE_GRIST)>fprintf.c @@ -20,11 +22,12 @@ KernelMergeObject posix_gnu_stdio.o : #_common.o : <$(SOURCE_GRIST)>getw.c <$(SOURCE_GRIST)>itoa-digits.c <$(SOURCE_GRIST)>itoa-udigits.c -# <$(SOURCE_GRIST)>itowa-digits.c + <$(SOURCE_GRIST)>itowa-digits.c <$(SOURCE_GRIST)>perror.c - <$(SOURCE_GRIST)>printf-parse.c + <$(SOURCE_GRIST)>printf-prs.c <$(SOURCE_GRIST)>printf.c <$(SOURCE_GRIST)>printf_fp.c + <$(SOURCE_GRIST)>printf_fphex.c # <$(SOURCE_GRIST)>printf_size.c <$(SOURCE_GRIST)>putw.c <$(SOURCE_GRIST)>reg-printf.c diff --git a/src/kernel/libroot/posix/glibc/stdio-common/printf-parse.h b/src/kernel/libroot/posix/glibc/stdio-common/printf-parse.h index 8fbf8ff178..4f8007121e 100644 --- a/src/kernel/libroot/posix/glibc/stdio-common/printf-parse.h +++ b/src/kernel/libroot/posix/glibc/stdio-common/printf-parse.h @@ -1,5 +1,5 @@ /* Internal header for parsing printf format strings. - Copyright (C) 1995-1999, 2000, 2002 Free Software Foundation, Inc. + Copyright (C) 1995-1999, 2000 Free Software Foundation, Inc. This file is part of th GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -87,27 +87,28 @@ read_int (const UCHAR_T * *pstr) } + /* Find the next spec in FORMAT, or the end of the string. Returns a pointer into FORMAT, to a '%' or a '\0'. */ static inline const UCHAR_T * -//#ifdef COMPILE_WPRINTF +#ifdef COMPILE_WPRINTF find_spec (const UCHAR_T *format) -//#else -//find_spec (const UCHAR_T *format, mbstate_t *ps) -//#endif +#else +find_spec (const UCHAR_T *format, mbstate_t *ps) +#endif { #ifdef COMPILE_WPRINTF return (const UCHAR_T *) __wcschrnul ((const CHAR_T *) format, L'%'); #else while (*format != L_('\0') && *format != L_('%')) { -// int len; + int len; /* Remove any hints of a wrong encoding. */ -// ps->__count = 0; -// if (! isascii (*format) && (len = __mbrlen (format, MB_CUR_MAX, ps)) > 0) -// format += len; -// else + ps->__count = 0; + if (! isascii (*format) && (len = __mbrlen (format, MB_CUR_MAX, ps)) > 0) + format += len; + else ++format; } return format; @@ -116,8 +117,8 @@ find_spec (const UCHAR_T *format) /* These are defined in reg-printf.c. */ -extern printf_arginfo_function **__printf_arginfo_table attribute_hidden; -extern printf_function **__printf_function_table attribute_hidden; +extern printf_arginfo_function *__printf_arginfo_table[]; +extern printf_function **__printf_function_table; /* FORMAT must point to a '%' at the beginning of a spec. Fills in *SPEC @@ -126,13 +127,13 @@ extern printf_function **__printf_function_table attribute_hidden; the number of args consumed by this spec; *MAX_REF_ARG is updated so it remains the highest argument index used. */ static inline size_t -//#ifdef COMPILE_WPRINTF +#ifdef COMPILE_WPRINTF parse_one_spec (const UCHAR_T *format, size_t posn, struct printf_spec *spec, size_t *max_ref_arg) -//#else -//parse_one_spec (const UCHAR_T *format, size_t posn, struct printf_spec *spec, -// size_t *max_ref_arg, mbstate_t *ps) -//#endif +#else +parse_one_spec (const UCHAR_T *format, size_t posn, struct printf_spec *spec, + size_t *max_ref_arg, mbstate_t *ps) +#endif { unsigned int n; size_t nargs = 0; @@ -353,7 +354,7 @@ parse_one_spec (const UCHAR_T *format, size_t posn, struct printf_spec *spec, /* Get the format specification. */ spec->info.spec = (wchar_t) *format++; - if (__builtin_expect (__printf_function_table != NULL, 0) + if (__printf_function_table != NULL && spec->info.spec <= UCHAR_MAX && __printf_arginfo_table[spec->info.spec] != NULL) /* We don't try to get the types for all arguments if the format @@ -442,11 +443,11 @@ parse_one_spec (const UCHAR_T *format, size_t posn, struct printf_spec *spec, { /* Find the next format spec. */ spec->end_of_fmt = format; -//#ifdef COMPILE_WPRINTF +#ifdef COMPILE_WPRINTF spec->next_fmt = find_spec (format); -//#else -// spec->next_fmt = find_spec (format, ps); -//#endif +#else + spec->next_fmt = find_spec (format, ps); +#endif } return nargs; diff --git a/src/kernel/libroot/posix/glibc/stdio-common/printf-prs.c b/src/kernel/libroot/posix/glibc/stdio-common/printf-prs.c new file mode 100644 index 0000000000..ae3fe41403 --- /dev/null +++ b/src/kernel/libroot/posix/glibc/stdio-common/printf-prs.c @@ -0,0 +1,120 @@ +/* Copyright (C) 1991,1992,1995,1996,1999,2000 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#include +#include +#include +#include +#include +#include + +#ifndef COMPILE_WPRINTF +# define CHAR_T char +# define UCHAR_T unsigned char +# define INT_T int +# define L_(Str) Str +# define ISDIGIT(Ch) isdigit (Ch) +# define ISASCII(Ch) isascii (Ch) +# define MBRLEN(Cp, L, St) __mbrlen (Cp, L, St) + +# ifdef USE_IN_LIBIO +# define PUT(F, S, N) _IO_sputn (F, S, N) +# define PAD(Padchar) \ + if (width > 0) \ + done += _IO_padn (s, Padchar, width) +# else +# define PUTC(C, F) putc (C, F) +ssize_t __printf_pad __P ((FILE *, char pad, size_t n)); +# define PAD(Padchar) \ + if (width > 0) \ + { if (__printf_pad (s, Padchar, width) == -1) \ + return -1; else done += width; } +# endif +#else +# define vfprintf vfwprintf +# define CHAR_T wchar_t +# define UCHAR_T uwchar_t +# define INT_T wint_t +# define L_(Str) L##Str +# define ISDIGIT(Ch) iswdigit (Ch) + +# ifdef USE_IN_LIBIO +# define PUT(F, S, N) _IO_sputn (F, S, N) +# define PAD(Padchar) \ + if (width > 0) \ + done += _IO_wpadn (s, Padchar, width) +# else +# define PUTC(C, F) wputc (C, F) +ssize_t __wprintf_pad __P ((FILE *, wchar_t pad, size_t n)); +# define PAD(Padchar) \ + if (width > 0) \ + { if (__wprintf_pad (s, Padchar, width) == -1) \ + return -1; else done += width; } +# endif +#endif + +#include "printf-parse.h" + + +size_t +parse_printf_format (fmt, n, argtypes) + const char *fmt; + size_t n; + int *argtypes; +{ + size_t nargs; /* Number of arguments. */ + size_t max_ref_arg; /* Highest index used in a positional arg. */ + struct printf_spec spec; + mbstate_t mbstate; + + nargs = 0; + max_ref_arg = 0; + + /* Search for format specifications. */ + for (fmt = find_spec (fmt, &mbstate); *fmt != '\0'; fmt = spec.next_fmt) + { + /* Parse this spec. */ + nargs += parse_one_spec (fmt, nargs, &spec, &max_ref_arg, &mbstate); + + /* If the width is determined by an argument this is an int. */ + if (spec.width_arg != -1 && (size_t) spec.width_arg < n) + argtypes[spec.width_arg] = PA_INT; + + /* If the precision is determined by an argument this is an int. */ + if (spec.prec_arg != -1 && (size_t) spec.prec_arg < n) + argtypes[spec.prec_arg] = PA_INT; + + if ((size_t) spec.data_arg < n) + switch (spec.ndata_args) + { + case 0: /* No arguments. */ + break; + case 1: /* One argument; we already have the type. */ + argtypes[spec.data_arg] = spec.data_arg_type; + break; + default: + /* We have more than one argument for this format spec. We must + call the arginfo function again to determine all the types. */ + (void) (*__printf_arginfo_table[spec.info.spec]) + (&spec.info, n - spec.data_arg, &argtypes[spec.data_arg]); + break; + } + } + + return MAX (nargs, max_ref_arg); +} diff --git a/src/kernel/libroot/posix/glibc/stdio-common/printf_fp.c b/src/kernel/libroot/posix/glibc/stdio-common/printf_fp.c index 116334d9af..4c68c5edb3 100644 --- a/src/kernel/libroot/posix/glibc/stdio-common/printf_fp.c +++ b/src/kernel/libroot/posix/glibc/stdio-common/printf_fp.c @@ -1,5 +1,5 @@ /* Floating point output for `printf'. - Copyright (C) 1995-1999, 2000, 2001, 2002 Free Software Foundation, Inc. + Copyright (C) 1995-1999, 2000, 2001 Free Software Foundation, Inc. This file is part of the GNU C Library. Written by Ulrich Drepper , 1995. @@ -21,10 +21,11 @@ /* The gmp headers need some configuration frobs. */ #define HAVE_ALLOCA 1 -#include -// ToDo: implement this function for real -#include -#if 0 +#ifdef USE_IN_LIBIO +# include +#else +# include +#endif #include #include #include @@ -36,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -50,7 +52,7 @@ the GNU I/O library. */ #ifdef USE_IN_LIBIO # define PUT(f, s, n) _IO_sputn (f, s, n) -# define PAD(f, c, n) (wide ? _IO_wpadn (f, c, n) : INTUSE(_IO_padn) (f, c, n)) +# define PAD(f, c, n) (wide ? _IO_wpadn (f, c, n) : _IO_padn (f, c, n)) /* We use this file GNU C library and GNU I/O library. So make names equal. */ # undef putc @@ -118,8 +120,7 @@ ssize_t __printf_pad __P ((FILE *, char pad, int n)); /* In vfprintf.c. */ #define MPN_GE(u,v) \ (u##size > v##size || (u##size == v##size && __mpn_cmp (u, v, u##size) >= 0)) -extern int __isinfl_internal (long double) attribute_hidden; -extern int __isnanl_internal (long double) attribute_hidden; +extern int __isinfl (long double), __isnanl (long double); extern mp_size_t __mpn_extract_double (mp_ptr res_ptr, mp_size_t size, int *expt, int *is_neg, @@ -210,7 +211,8 @@ __printf_fp (FILE *fp, else if (scalesize == 0) { hi = frac[fracsize - 1]; - frac[fracsize - 1] = __mpn_mul_1 (frac, frac, fracsize - 1, 10); + cy = __mpn_mul_1 (frac, frac, fracsize - 1, 10); + frac[fracsize - 1] = cy; } else { @@ -234,11 +236,9 @@ __printf_fp (FILE *fp, } } - { - mp_limb_t _cy = __mpn_mul_1 (frac, frac, fracsize, 10); - if (_cy != 0) - frac[fracsize++] = _cy; - } + cy = __mpn_mul_1 (frac, frac, fracsize, 10); + if (cy != 0) + frac[fracsize++] = cy; } return L'0' + hi; @@ -304,7 +304,7 @@ __printf_fp (FILE *fp, multibyte representation for the thousands separator, we must ensure the wide character thousands separator is available, even if it is fake. */ - thousands_sepwc = (wchar_t)0xfffffffe; + thousands_sepwc = 0xfffffffe; } } else @@ -363,7 +363,6 @@ __printf_fp (FILE *fp, /* Check for special values: not a number or infinity. */ if (__isnan (fpnum.dbl)) { - is_neg = 0; if (isupper (info->spec)) { special = "NAN"; @@ -374,10 +373,10 @@ __printf_fp (FILE *fp, special = "nan"; wspecial = L"nan"; } + is_neg = 0; } else if (__isinf (fpnum.dbl)) { - is_neg = fpnum.dbl < 0; if (isupper (info->spec)) { special = "INF"; @@ -388,6 +387,7 @@ __printf_fp (FILE *fp, special = "inf"; wspecial = L"inf"; } + is_neg = fpnum.dbl < 0; } else { @@ -493,9 +493,6 @@ __printf_fp (FILE *fp, &__tens[powers->arrayoff], tmpsize * sizeof (mp_limb_t)); MPN_ZERO (tmp, _FPIO_CONST_SHIFT); - /* Adjust exponent, as scaleexpo will be this much - bigger too. */ - exponent += _FPIO_CONST_SHIFT * BITS_PER_MP_LIMB; } else #endif @@ -809,12 +806,10 @@ __printf_fp (FILE *fp, dig_max = INT_MAX; /* Unlimited. */ significant = 1; /* Does not matter here. */ } - else if (_tolower (info->spec) == 'f') + else if (info->spec == 'f') { type = 'f'; fracdig_min = fracdig_max = info->prec < 0 ? 6 : info->prec; - dig_max = INT_MAX; /* Unlimited. */ - significant = 1; /* Does not matter here. */ if (expsign == 0) { intdig_max = exponent + 1; @@ -826,6 +821,8 @@ __printf_fp (FILE *fp, intdig_max = 1; chars_needed = 1 + 1 + fracdig_max; } + dig_max = INT_MAX; /* Unlimited. */ + significant = 1; /* Does not matter here. */ } else { @@ -869,7 +866,7 @@ __printf_fp (FILE *fp, it is possible that we need two more characters in front of all the other output. If the amount of memory we have to allocate is too large use `malloc' instead of `alloca'. */ - buffer_malloced = ! __libc_use_alloca (chars_needed * 2 * sizeof (wchar_t)); + buffer_malloced = chars_needed > 5000; if (buffer_malloced) { wbuffer = (wchar_t *) malloc ((2 + chars_needed) * sizeof (wchar_t)); @@ -911,7 +908,7 @@ __printf_fp (FILE *fp, { ++fracdig_no; *wcp = hack_digit (); - if (*wcp++ != L'0') + if (*wcp != L'0') significant = 1; else if (significant == 0) { @@ -919,6 +916,7 @@ __printf_fp (FILE *fp, if (fracdig_min > 0) ++fracdig_min; } + ++wcp; } /* Do rounding. */ @@ -1144,7 +1142,6 @@ __printf_fp (FILE *fp, } return done; } -libc_hidden_def (__printf_fp) /* Return the number of extra grouping characters that will be inserted into a number with INTDIG_MAX integer digits. */ @@ -1230,10 +1227,3 @@ group_number (wchar_t *buf, wchar_t *bufend, unsigned int intdig_no, return bufend + ngroups; } -#endif - -int -__printf_fp(FILE *fp, const struct printf_info *info, const void * const *args) -{ - return fputs("NaN-FIXME", fp); -} diff --git a/src/kernel/libroot/posix/glibc/stdio-common/printf_fphex.c b/src/kernel/libroot/posix/glibc/stdio-common/printf_fphex.c new file mode 100644 index 0000000000..5505d13897 --- /dev/null +++ b/src/kernel/libroot/posix/glibc/stdio-common/printf_fphex.c @@ -0,0 +1,493 @@ +/* Print floating point number in hexadecimal notation according to ISO C99. + Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 1997. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "_itoa.h" +#include "_itowa.h" +#include + +/* #define NDEBUG 1*/ /* Undefine this for debugging assertions. */ +#include + +/* This defines make it possible to use the same code for GNU C library and + the GNU I/O library. */ +#ifdef USE_IN_LIBIO +# include +# define PUT(f, s, n) _IO_sputn (f, s, n) +# define PAD(f, c, n) (wide ? _IO_wpadn (f, c, n) : _IO_padn (f, c, n)) +/* We use this file GNU C library and GNU I/O library. So make + names equal. */ +# undef putc +# define putc(c, f) (wide \ + ? (int)_IO_putwc_unlocked (c, f) : _IO_putc_unlocked (c, f)) +# define size_t _IO_size_t +# define FILE _IO_FILE +#else /* ! USE_IN_LIBIO */ +# define PUT(f, s, n) fwrite (s, 1, n, f) +# define PAD(f, c, n) __printf_pad (f, c, n) +ssize_t __printf_pad __P ((FILE *, char pad, int n)); /* In vfprintf.c. */ +#endif /* USE_IN_LIBIO */ + +/* Macros for doing the actual output. */ + +#define outchar(ch) \ + do \ + { \ + register const int outc = (ch); \ + if (putc (outc, fp) == EOF) \ + return -1; \ + ++done; \ + } while (0) + +#define PRINT(ptr, wptr, len) \ + do \ + { \ + register size_t outlen = (len); \ + if (wide) \ + while (outlen-- > 0) \ + outchar (*wptr++); \ + else \ + while (outlen-- > 0) \ + outchar (*ptr++); \ + } while (0) + +#define PADN(ch, len) \ + do \ + { \ + if (PAD (fp, ch, len) != len) \ + return -1; \ + done += len; \ + } \ + while (0) + +#ifndef MIN +# define MIN(a,b) ((a)<(b)?(a):(b)) +#endif + + +int +__printf_fphex (FILE *fp, + const struct printf_info *info, + const void *const *args) +{ + /* The floating-point value to output. */ + union + { + union ieee754_double dbl; + union ieee854_long_double ldbl; + } + fpnum; + + /* Locale-dependent representation of decimal point. */ + const char *decimal; + wchar_t decimalwc; + + /* "NaN" or "Inf" for the special cases. */ + const char *special = NULL; + const wchar_t *wspecial = NULL; + + /* Buffer for the generated number string for the mantissa. The + maximal size for the mantissa is 128 bits. */ + char numbuf[32]; + char *numstr; + char *numend; + wchar_t wnumbuf[32]; + wchar_t *wnumstr; + wchar_t *wnumend; + int negative; + + /* The maximal exponent of two in decimal notation has 5 digits. */ + char expbuf[5]; + char *expstr; + wchar_t wexpbuf[5]; + wchar_t *wexpstr; + int expnegative; + int exponent; + + /* Non-zero is mantissa is zero. */ + int zero_mantissa; + + /* The leading digit before the decimal point. */ + char leading; + + /* Precision. */ + int precision = info->prec; + + /* Width. */ + int width = info->width; + + /* Number of characters written. */ + int done = 0; + + /* Nonzero if this is output on a wide character stream. */ + int wide = info->wide; + + + /* Figure out the decimal point character. */ + if (info->extra == 0) + { + decimal = _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT); + decimalwc = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_DECIMAL_POINT_WC); + } + else + { + decimal = _NL_CURRENT (LC_MONETARY, MON_DECIMAL_POINT); + decimalwc = _NL_CURRENT_WORD (LC_MONETARY, + _NL_MONETARY_DECIMAL_POINT_WC); + } + /* The decimal point character must never be zero. */ + assert (*decimal != '\0' && decimalwc != L'\0'); + + + /* Fetch the argument value. */ +#ifndef __NO_LONG_DOUBLE_MATH + if (info->is_long_double && sizeof (long double) > sizeof (double)) + { + fpnum.ldbl.d = *(const long double *) args[0]; + + /* Check for special values: not a number or infinity. */ + if (__isnanl (fpnum.ldbl.d)) + { + if (isupper (info->spec)) + { + special = "NAN"; + wspecial = L"NAN"; + } + else + { + special = "nan"; + wspecial = L"nan"; + } + negative = 0; + } + else + { + if (__isinfl (fpnum.ldbl.d)) + { + if (isupper (info->spec)) + { + special = "INF"; + wspecial = L"INF"; + } + else + { + special = "inf"; + wspecial = L"inf"; + } + } + + negative = signbit (fpnum.ldbl.d); + } + } + else +#endif /* no long double */ + { + fpnum.dbl.d = *(const double *) args[0]; + + /* Check for special values: not a number or infinity. */ + if (__isnan (fpnum.dbl.d)) + { + if (isupper (info->spec)) + { + special = "NAN"; + wspecial = L"NAN"; + } + else + { + special = "nan"; + wspecial = L"nan"; + } + negative = 0; + } + else + { + if (__isinf (fpnum.dbl.d)) + { + if (isupper (info->spec)) + { + special = "INF"; + wspecial = L"INF"; + } + else + { + special = "inf"; + wspecial = L"inf"; + } + } + + negative = signbit (fpnum.dbl.d); + } + } + + if (special) + { + int width = info->width; + + if (negative || info->showsign || info->space) + --width; + width -= 3; + + if (!info->left && width > 0) + PADN (' ', width); + + if (negative) + outchar ('-'); + else if (info->showsign) + outchar ('+'); + else if (info->space) + outchar (' '); + + PRINT (special, wspecial, 3); + + if (info->left && width > 0) + PADN (' ', width); + + return done; + } + + if (info->is_long_double == 0 || sizeof (double) == sizeof (long double)) + { + /* We have 52 bits of mantissa plus one implicit digit. Since + 52 bits are representable without rest using hexadecimal + digits we use only the implicit digits for the number before + the decimal point. */ + unsigned long long int num; + + num = (((unsigned long long int) fpnum.dbl.ieee.mantissa0) << 32 + | fpnum.dbl.ieee.mantissa1); + + zero_mantissa = num == 0; + + if (sizeof (unsigned long int) > 6) + { + wnumstr = _itowa_word (num, wnumbuf + (sizeof wnumbuf) / sizeof (wchar_t), 16, + info->spec == 'A'); + numstr = _itoa_word (num, numbuf + sizeof numbuf, 16, + info->spec == 'A'); + } + else + { + wnumstr = _itowa (num, wnumbuf + sizeof wnumbuf / sizeof (wchar_t), 16, + info->spec == 'A'); + numstr = _itoa (num, numbuf + sizeof numbuf, 16, + info->spec == 'A'); + } + + /* Fill with zeroes. */ + while (wnumstr > wnumbuf + (sizeof wnumbuf - 52) / sizeof (wchar_t)) + { + *--wnumstr = L'0'; + *--numstr = '0'; + } + + leading = fpnum.dbl.ieee.exponent == 0 ? '0' : '1'; + + exponent = fpnum.dbl.ieee.exponent; + + if (exponent == 0) + { + if (zero_mantissa) + expnegative = 0; + else + { + /* This is a denormalized number. */ + expnegative = 1; + exponent = IEEE754_DOUBLE_BIAS - 1; + } + } + else if (exponent >= IEEE754_DOUBLE_BIAS) + { + expnegative = 0; + exponent -= IEEE754_DOUBLE_BIAS; + } + else + { + expnegative = 1; + exponent = -(exponent - IEEE754_DOUBLE_BIAS); + } + } +#ifdef PRINT_FPHEX_LONG_DOUBLE + else + PRINT_FPHEX_LONG_DOUBLE; +#endif + + /* Look for trailing zeroes. */ + if (! zero_mantissa) + { + wnumend = wnumbuf + sizeof wnumbuf; + numend = numbuf + sizeof numbuf; + while (wnumend[-1] == L'0') + { + --wnumend; + --numend; + } + + if (precision == -1) + precision = numend - numstr; + else if (precision < numend - numstr + && (numstr[precision] > '8' + || (('A' < '0' || 'a' < '0') + && numstr[precision] < '0') + || (numstr[precision] == '8' + && (precision + 1 < numend - numstr + /* Round to even. */ + || (precision > 0 + && ((numstr[precision - 1] & 1) + ^ (isdigit (numstr[precision - 1]) == 0))) + || (precision == 0 + && ((leading & 1) + ^ (isdigit (leading) == 0))))))) + { + /* Round up. */ + int cnt = precision; + while (--cnt >= 0) + { + char ch = numstr[cnt]; + /* We assume that the digits and the letters are ordered + like in ASCII. This is true for the rest of GNU, too. */ + if (ch == '9') + { + wnumstr[cnt] = (wchar_t) info->spec; + numstr[cnt] = info->spec; /* This is tricky, + think about it! */ + break; + } + else if (tolower (ch) < 'f') + { + ++numstr[cnt]; + ++wnumstr[cnt]; + break; + } + else + { + numstr[cnt] = '0'; + wnumstr[cnt] = L'0'; + } + } + if (cnt < 0) + { + /* The mantissa so far was fff...f Now increment the + leading digit. Here it is again possible that we + get an overflow. */ + if (leading == '9') + leading = info->spec; + else if (tolower (leading) < 'f') + ++leading; + else + { + leading = 1; + if (expnegative) + { + exponent += 4; + if (exponent >= 0) + expnegative = 0; + } + else + exponent += 4; + } + } + } + } + else + { + if (precision == -1) + precision = 0; + numend = numstr; + wnumend = wnumstr; + } + + /* Now we can compute the exponent string. */ + expstr = _itoa_word (exponent, expbuf + sizeof expbuf, 10, 0); + wexpstr = _itowa_word (exponent, + wexpbuf + sizeof wexpbuf / sizeof (wchar_t), 10, 0); + + /* Now we have all information to compute the size. */ + width -= ((negative || info->showsign || info->space) + /* Sign. */ + + 2 + 1 + 0 + precision + 1 + 1 + /* 0x h . hhh P ExpoSign. */ + + ((expbuf + sizeof expbuf) - expstr)); + /* Exponent. */ + + /* Count the decimal point. */ + if (precision > 0 || info->alt) + width -= wide ? 1 : strlen (decimal); + + /* A special case when the mantissa or the precision is zero and the `#' + is not given. In this case we must not print the decimal point. */ + if (precision == 0 && !info->alt) + ++width; /* This nihilates the +1 for the decimal-point + character in the following equation. */ + + if (!info->left && width > 0) + PADN (' ', width); + + if (negative) + outchar ('-'); + else if (info->showsign) + outchar ('+'); + else if (info->space) + outchar (' '); + + outchar ('0'); + if ('X' - 'A' == 'x' - 'a') + outchar (info->spec + ('x' - 'a')); + else + outchar (info->spec == 'A' ? 'X' : 'x'); + outchar (leading); + + if (precision > 0 || info->alt) + { + const wchar_t *wtmp = &decimalwc; + PRINT (decimal, wtmp, wide ? 1 : strlen (decimal)); + } + + if (precision > 0) + { + ssize_t tofill = precision - (numend - numstr); + PRINT (numstr, wnumstr, MIN (numend - numstr, precision)); + if (tofill > 0) + PADN ('0', tofill); + } + + if (info->left && info->pad == '0' && width > 0) + PADN ('0', width); + + if ('P' - 'A' == 'p' - 'a') + outchar (info->spec + ('p' - 'a')); + else + outchar (info->spec == 'A' ? 'P' : 'p'); + + outchar (expnegative ? '-' : '+'); + + PRINT (expstr, wexpstr, (expbuf + sizeof expbuf) - expstr); + + if (info->left && info->pad != '0' && width > 0) + PADN (info->pad, width); + + return done; +} diff --git a/src/kernel/libroot/posix/glibc/stdio-common/vfprintf.c b/src/kernel/libroot/posix/glibc/stdio-common/vfprintf.c index 5f0376058d..febd094729 100644 --- a/src/kernel/libroot/posix/glibc/stdio-common/vfprintf.c +++ b/src/kernel/libroot/posix/glibc/stdio-common/vfprintf.c @@ -1,4 +1,4 @@ -/* Copyright (C) 1991-2002, 2003 Free Software Foundation, Inc. +/* Copyright (C) 1991-1999, 2000, 2001 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -14,11 +14,8 @@ You should have received a copy of the GNU Lesser General Public License along with the GNU C Library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - 02111-1307 USA. -*/ + 02111-1307 USA. */ - -#include #include #include #include @@ -40,99 +37,179 @@ Beside this it is also shared between the normal and wide character implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995. */ -#include -#define PUTC(C, F) _IO_putc_unlocked(C, F) -#define FILE _IO_FILE -#undef BUFSIZ -#define BUFSIZ _IO_BUFSIZ -#define ARGCHECK(S, Format) \ - do { \ - /* Check file argument for consistence. */ \ - CHECK_FILE (S, -1); \ - if (S->_flags & _IO_NO_WRITES) { \ - __set_errno(EBADF); \ - return -1; \ - } \ - if (Format == NULL) { \ - __set_errno(EINVAL); \ - return -1; \ - } \ - } while (0) +#ifdef USE_IN_LIBIO +/* This code is for use in libio. */ +# include +# define FILE _IO_FILE +# undef va_list +# define va_list _IO_va_list +# undef BUFSIZ +# define BUFSIZ _IO_BUFSIZ +# define ARGCHECK(S, Format) \ + do \ + { \ + /* Check file argument for consistence. */ \ + CHECK_FILE (S, -1); \ + if (S->_flags & _IO_NO_WRITES) \ + { \ + __set_errno (EBADF); \ + return -1; \ + } \ + if (Format == NULL) \ + { \ + MAYBE_SET_EINVAL; \ + return -1; \ + } \ + } while (0) +# define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED) -#define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED) +# ifndef COMPILE_WPRINTF +# define vfprintf _IO_vfprintf +# define CHAR_T char +# define UCHAR_T unsigned char +# define INT_T int +# define L_(Str) Str +# define ISDIGIT(Ch) ((unsigned int) ((Ch) - '0') < 10) -#define CHAR_T char -#define UCHAR_T unsigned char -#define INT_T int -#define L_(Str) Str -#define ISDIGIT(Ch) ((unsigned int) ((Ch) - '0') < 10) +# define PUT(F, S, N) _IO_sputn ((F), (S), (N)) +# define PAD(Padchar) \ + if (width > 0) \ + done += _IO_padn (s, (Padchar), width) +# define PUTC(C, F) _IO_putc_unlocked (C, F) +# define ORIENT if (s->_vtable_offset == 0 && _IO_fwide (s, -1) != -1)\ + return -1 +# else +# define vfprintf _IO_vfwprintf +# define CHAR_T wchar_t +/* This is a hack!!! There should be a type uwchar_t. */ +# define UCHAR_T unsigned int /* uwchar_t */ +# define INT_T wint_t +# define L_(Str) L##Str +# define ISDIGIT(Ch) ((unsigned int) ((Ch) - L'0') < 10) -#define PUT(F, S, N) _IO_sputn ((F), (S), (N)) -#define PAD(Padchar) \ - if (width > 0) \ - done += INTUSE(_IO_padn) (s, (Padchar), width) +# include "_itowa.h" + +# define PUT(F, S, N) _IO_sputn ((F), (S), (N)) +# define PAD(Padchar) \ + if (width > 0) \ + done += _IO_wpadn (s, (Padchar), width) +# define PUTC(C, F) _IO_putwc_unlocked (C, F) +# define ORIENT if (_IO_fwide (s, 1) != 1) return -1 + +# define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case) +# define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case) +# undef EOF +# define EOF WEOF +# endif +#else /* ! USE_IN_LIBIO */ +/* This code is for use in the GNU C library. */ +# include +# define ARGCHECK(S, Format) \ + do \ + { \ + /* Check file argument for consistence. */ \ + if (!__validfp (S) || !S->__mode.__write) \ + { \ + __set_errno (EBADF); \ + return -1; \ + } \ + if (Format == NULL) \ + { \ + __set_errno (EINVAL); \ + return -1; \ + } \ + if (!S->__seen) \ + { \ + if (__flshfp (S, EOF) == EOF) \ + return -1; \ + } \ + } \ + while (0) +# define UNBUFFERED_P(s) ((s)->__buffer == NULL) + +# define CHAR_T char +# define UCHAR_T unsigned char +# define INT_T int +# define L_(Str) Str +# define ISDIGIT(Ch) isdigit (Ch) + +# define PUT(F, S, N) fwrite (S, 1, N, F) +ssize_t __printf_pad __P ((FILE *, char pad, size_t n)); +# define PAD(Padchar) \ + if (width > 0) \ + { ssize_t __res = __printf_pad (s, (Padchar), width); \ + if (__res == -1) \ + { \ + done = -1; \ + goto all_done; \ + } \ + done += __res; } +# define PUTC(C, F) putc (C, F) /* XXX These declarations should go as soon as the stdio header files have these prototypes. */ extern void __flockfile (FILE *); extern void __funlockfile (FILE *); +#endif /* USE_IN_LIBIO */ - -//#include "_i18n_number.h" +#include "_i18n_number.h" /* Include the shared code for parsing the format string. */ #include "printf-parse.h" -#define outchar(Ch) \ - do { \ - register const INT_T outc =(Ch); \ - if (PUTC (outc, s) == EOF) { \ - done = -1; \ - goto all_done; \ - } else \ - ++done; \ - } while (0) - -#define outstring(String, Len) \ - do { \ - if ((size_t)PUT(s, (String), (Len)) != (size_t)(Len)) { \ - done = -1; \ - goto all_done; \ - } \ - done += (Len); \ - } while (0) +#define outchar(Ch) \ + do \ + { \ + register const INT_T outc = (Ch); \ + if (PUTC (outc, s) == EOF) \ + { \ + done = -1; \ + goto all_done; \ + } \ + else \ + ++done; \ + } \ + while (0) +#define outstring(String, Len) \ + do \ + { \ + if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len)) \ + { \ + done = -1; \ + goto all_done; \ + } \ + done += (Len); \ + } \ + while (0) /* For handling long_double and longlong we use the same flag. If - * `long' and `long long' are effectively the same type define it to - * zero. - */ + `long' and `long long' are effectively the same type define it to + zero. */ #if LONG_MAX == LONG_LONG_MAX -# define is_longlong 0 +# define is_longlong 0 #else -# define is_longlong is_long_double +# define is_longlong is_long_double #endif /* If `long' and `int' is effectively the same type we don't have to - * handle `long separately. - */ + handle `long separately. */ #if INT_MAX == LONG_MAX -# define is_long_num 0 +# define is_long_num 0 #else -# define is_long_num is_long +# define is_long_num is_long #endif /* Global variables. */ static const CHAR_T null[] = L_("(null)"); -#if 0 + /* Helper function to provide temporary buffering for unbuffered streams. */ static int buffered_vfprintf __P ((FILE *stream, const CHAR_T *fmt, va_list)) internal_function; -#endif /* Handle unknown format specifier. */ static int printf_unknown __P ((FILE *, const struct printf_info *, @@ -148,109 +225,110 @@ static CHAR_T *group_number __P ((CHAR_T *, CHAR_T *, const char *, #endif -static int -__libc_use_alloca(int size) -{ - // ToDo: we don't use alloca() for now - return 0; -} - - -/* The function itself. */ - +/* The function itself. */ int -_IO_vfprintf(FILE *s, const CHAR_T *format, va_list ap) +vfprintf (FILE *s, const CHAR_T *format, va_list ap) { - /* The character used as thousands separator. */ + /* The character used as thousands separator. */ #ifdef COMPILE_WPRINTF - wchar_t thousands_sep = L'\0'; + wchar_t thousands_sep = L'\0'; #else - const char *thousands_sep = NULL; + const char *thousands_sep = NULL; #endif - /* The string describing the size of groups of digits. */ - const char *grouping; + /* The string describing the size of groups of digits. */ + const char *grouping; - /* Place to accumulate the result. */ - int done; + /* Place to accumulate the result. */ + int done; - /* Current character in format string. */ - const UCHAR_T *f; + /* Current character in format string. */ + const UCHAR_T *f; - /* End of leading constant string. */ - const UCHAR_T *lead_str_end; + /* End of leading constant string. */ + const UCHAR_T *lead_str_end; - /* Points to next format specifier. */ - const UCHAR_T *end_of_spec; + /* Points to next format specifier. */ + const UCHAR_T *end_of_spec; - /* Buffer intermediate results. */ - CHAR_T work_buffer[1000]; - CHAR_T *workstart = NULL; - CHAR_T *workend; + /* Buffer intermediate results. */ + CHAR_T work_buffer[1000]; + CHAR_T *workend; - /* We have to save the original argument pointer. */ - va_list ap_save; + /* State for restartable multibyte character handling functions. */ +#ifndef COMPILE_WPRINTF + mbstate_t mbstate; +#endif - /* Count number of specifiers we already processed. */ - int nspecs_done; + /* We have to save the original argument pointer. */ + va_list ap_save; - /* For the %m format we may need the current `errno' value. */ - int save_errno = errno; + /* Count number of specifiers we already processed. */ + int nspecs_done; - /* This table maps a character into a number representing a class. - * In each step there is a destination label for each class. - */ - static const int jump_table[] = { - /* ' ' */ 1, 0, 0, /* '#' */ 4, - 0, /* '%' */ 14, 0, /* '\''*/ 6, - 0, 0, /* '*' */ 7, /* '+' */ 2, - 0, /* '-' */ 3, /* '.' */ 9, 0, - /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8, - /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8, - /* '8' */ 8, /* '9' */ 8, 0, 0, - 0, 0, 0, 0, - 0, /* 'A' */ 25, 0, /* 'C' */ 0/*25*/, - 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19, - 0, /* 'I' */ 28, 0, 0, - /* 'L' */ 12, 0, 0, 0, - 0, 0, 0, /* 'S' */ 21, - 0, 0, 0, 0, - /* 'X' */ 18, 0, /* 'Z' */ 13, 0, - 0, 0, 0, 0, - 0, /* 'a' */ 25, 0, /* 'c' */ 20, - /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19, - /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 27, 0, - /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17, - /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21, - /* 't' */ 26, /* 'u' */ 16, 0, 0, - /* 'x' */ 18, 0, /* 'z' */ 13 - }; + /* For the %m format we may need the current `errno' value. */ + int save_errno = errno; + + + /* This table maps a character into a number representing a + class. In each step there is a destination label for each + class. */ + static const int jump_table[] = + { + /* ' ' */ 1, 0, 0, /* '#' */ 4, + 0, /* '%' */ 14, 0, /* '\''*/ 6, + 0, 0, /* '*' */ 7, /* '+' */ 2, + 0, /* '-' */ 3, /* '.' */ 9, 0, + /* '0' */ 5, /* '1' */ 8, /* '2' */ 8, /* '3' */ 8, + /* '4' */ 8, /* '5' */ 8, /* '6' */ 8, /* '7' */ 8, + /* '8' */ 8, /* '9' */ 8, 0, 0, + 0, 0, 0, 0, + 0, /* 'A' */ 26, 0, /* 'C' */ 25, + 0, /* 'E' */ 19, /* F */ 19, /* 'G' */ 19, + 0, /* 'I' */ 29, 0, 0, + /* 'L' */ 12, 0, 0, 0, + 0, 0, 0, /* 'S' */ 21, + 0, 0, 0, 0, + /* 'X' */ 18, 0, /* 'Z' */ 13, 0, + 0, 0, 0, 0, + 0, /* 'a' */ 26, 0, /* 'c' */ 20, + /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19, + /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28, 0, + /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17, + /* 'p' */ 22, /* 'q' */ 12, 0, /* 's' */ 21, + /* 't' */ 27, /* 'u' */ 16, 0, 0, + /* 'x' */ 18, 0, /* 'z' */ 13 + }; #define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z')) #define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')]) #if defined HAVE_SUBTRACT_LOCAL_LABELS && defined SHARED /* 'int' is enough and it saves some space on 64 bit systems. */ -# define JUMP_TABLE_TYPE const int -# define JUMP(ChExpr, table) \ - do { \ - int offset; \ - void *__unbounded ptr; \ - spec = (ChExpr); \ - offset = NOT_IN_JUMP_RANGE(spec) ? REF(form_unknown) \ - : table[CHAR_CLASS(spec)]; \ - ptr = &&do_form_unknown + offset; \ - goto *ptr; \ - } while (0) +# define JUMP_TABLE_TYPE const int +# define JUMP(ChExpr, table) \ + do \ + { \ + int offset; \ + void *__unbounded ptr; \ + spec = (ChExpr); \ + offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \ + : table[CHAR_CLASS (spec)]; \ + ptr = &&do_form_unknown + offset; \ + goto *ptr; \ + } \ + while (0) #else -# define JUMP_TABLE_TYPE const void *const -# define JUMP(ChExpr, table) \ - do { \ - const void *__unbounded ptr; \ - spec = (ChExpr); \ - ptr = NOT_IN_JUMP_RANGE(spec) ? REF(form_unknown) \ - : table[CHAR_CLASS(spec)]; \ - goto *ptr; \ - } while (0) +# define JUMP_TABLE_TYPE const void *const +# define JUMP(ChExpr, table) \ + do \ + { \ + const void *__unbounded ptr; \ + spec = (ChExpr); \ + ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown) \ + : table[CHAR_CLASS (spec)]; \ + goto *ptr; \ + } \ + while (0) #endif #define STEP0_3_TABLE \ @@ -282,6 +360,7 @@ _IO_vfprintf(FILE *s, const CHAR_T *format, va_list ap) REF (form_pointer), /* for 'p' */ \ REF (form_number), /* for 'n' */ \ REF (form_strerror), /* for 'm' */ \ + REF (form_wcharacter), /* for 'C' */ \ REF (form_floathex), /* for 'A', 'a' */ \ REF (mod_ptrdiff_t), /* for 't' */ \ REF (mod_intmax_t), /* for 'j' */ \ @@ -315,6 +394,7 @@ _IO_vfprintf(FILE *s, const CHAR_T *format, va_list ap) REF (form_pointer), /* for 'p' */ \ REF (form_number), /* for 'n' */ \ REF (form_strerror), /* for 'm' */ \ + REF (form_wcharacter), /* for 'C' */ \ REF (form_floathex), /* for 'A', 'a' */ \ REF (mod_ptrdiff_t), /* for 't' */ \ REF (mod_intmax_t), /* for 'j' */ \ @@ -348,6 +428,7 @@ _IO_vfprintf(FILE *s, const CHAR_T *format, va_list ap) REF (form_pointer), /* for 'p' */ \ REF (form_number), /* for 'n' */ \ REF (form_strerror), /* for 'm' */ \ + REF (form_wcharacter), /* for 'C' */ \ REF (form_floathex), /* for 'A', 'a' */ \ REF (mod_ptrdiff_t), /* for 't' */ \ REF (mod_intmax_t), /* for 'j' */ \ @@ -415,6 +496,7 @@ _IO_vfprintf(FILE *s, const CHAR_T *format, va_list ap) REF (form_pointer), /* for 'p' */ \ REF (form_number), /* for 'n' */ \ REF (form_strerror), /* for 'm' */ \ + REF (form_wcharacter), /* for 'C' */ \ REF (form_floathex), /* for 'A', 'a' */ \ REF (form_unknown), /* for 't' */ \ REF (form_unknown), /* for 'j' */ \ @@ -450,499 +532,761 @@ _IO_vfprintf(FILE *s, const CHAR_T *format, va_list ap) REF (form_pointer), /* for 'p' */ \ REF (form_number), /* for 'n' */ \ REF (form_strerror), /* for 'm' */ \ + REF (form_wcharacter), /* for 'C' */ \ REF (form_floathex), /* for 'A', 'a' */ \ REF (form_unknown), /* for 't' */ \ REF (form_unknown), /* for 'j' */ \ REF (form_unknown) /* for 'I' */ \ } -#define process_arg(fspec) \ - /* Start real work. We know about all flags and modifiers and \ - now process the wanted format specifier. */ \ - LABEL(form_percent): \ - /* Write a literal "%". */ \ - outchar(L_('%')); \ - break; \ - \ - LABEL(form_integer): \ - /* Signed decimal integer. */ \ - base = 10; \ - \ - if (is_longlong) { \ - long long int signed_number; \ - \ - if (fspec == NULL) \ - signed_number = va_arg(ap, long long int); \ - else \ - signed_number = args_value[fspec->data_arg].pa_long_long_int; \ - \ - is_negative = signed_number < 0; \ - number.longlong = is_negative ? (- signed_number) : signed_number; \ - \ - goto LABEL(longlong_number); \ - } else { \ - long int signed_number; \ - \ - if (fspec == NULL) { \ - if (is_long_num) { \ - signed_number = va_arg(ap, long int); \ - } else { \ - /* `char' and `short int' will be promoted to `int'. */ \ - signed_number = va_arg(ap, int); \ - } \ - } else if (is_long_num) \ - signed_number = args_value[fspec->data_arg].pa_long_int; \ - else if (!is_short) \ - signed_number = args_value[fspec->data_arg].pa_int; \ - else \ - signed_number = args_value[fspec->data_arg].pa_short_int; \ - \ - is_negative = signed_number < 0; \ - number.word = is_negative ? (- signed_number) : signed_number; \ - \ - goto LABEL(number); \ - } \ - /* NOTREACHED */ \ - \ - LABEL(form_unsigned): \ - /* Unsigned decimal integer. */ \ - base = 10; \ - goto LABEL(unsigned_number); \ - \ - LABEL(form_octal): \ - /* Unsigned octal integer. */ \ - base = 8; \ - goto LABEL(unsigned_number); \ - \ - LABEL(form_hexa): \ - /* Unsigned hexadecimal integer. */ \ - base = 16; \ - /* Supposed to fall through */ \ - \ - LABEL(unsigned_number): \ - /* Unsigned number of base BASE. */ \ - \ - /* ISO specifies the `+' and ` ' flags only for signed \ - * conversions. \ - */ \ - is_negative = 0; \ - showsign = 0; \ - space = 0; \ - \ - if (is_longlong) { \ - if (fspec == NULL) \ - number.longlong = va_arg(ap, unsigned long long int); \ - else \ - number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \ - \ - LABEL(longlong_number): \ - if (prec < 0) { \ - /* Supply a default precision if none was given. */ \ - prec = 1; \ - } else { \ - /* We have to take care for the '0' flag. If a \ - * precision is given it must be ignored. \ - */ \ - pad = L_(' '); \ - } \ - \ - /* If the precision is 0 and the number is 0 nothing \ - * has to be written for the number, except for the 'o' \ - * format in alternate form. \ - */ \ - if (prec == 0 && number.longlong == 0) { \ - string = workend; \ - if (base == 8 && alt) \ - *--string = L_('0'); \ - } else { \ - /* Put the number in WORK. */ \ - string = _itoa(number.longlong, workend, base, spec == L_('X')); \ - if (group && grouping) \ - string = group_number(string, workend, grouping, thousands_sep); \ - \ - /*if (use_outdigits && base == 10) \ - string = _i18n_number_rewrite(string, workend);*/ \ - } \ - /* Simplify further test for num != 0. */ \ - number.word = number.longlong != 0; \ - } else { \ - if (fspec == NULL) { \ - if (is_long_num) \ - number.word = va_arg (ap, unsigned long int); \ - else if (is_char) \ - number.word = (unsigned char) va_arg (ap, unsigned int); \ - else if (!is_short) \ - number.word = va_arg (ap, unsigned int); \ - else \ - number.word = (unsigned short int) va_arg (ap, unsigned int); \ - } else if (is_long_num) \ - number.word = args_value[fspec->data_arg].pa_u_long_int; \ - else if (is_char) \ - number.word = (unsigned char)args_value[fspec->data_arg].pa_char; \ - else if (!is_short) \ - number.word = args_value[fspec->data_arg].pa_u_int; \ - else \ - number.word = (unsigned short int)args_value[fspec->data_arg].pa_u_short_int; \ - \ - LABEL(number): \ - if (prec < 0) { \ - /* Supply a default precision if none was given. */ \ - prec = 1; \ - } else { \ - /* We have to take care for the '0' flag. If \ - * a precision is given it must be ignored. \ - */ \ - pad = L_(' '); \ - } \ - /* If the precision is 0 and the number is 0 nothing has to \ - * be written for the number, except for the 'o' format in \ - * alternate form. \ - */ \ - if (prec == 0 && number.word == 0) { \ - string = workend; \ - if (base == 8 && alt) \ - *--string = L_('0'); \ - } else { \ - /* Put the number in WORK. */ \ - string = _itoa_word(number.word, workend, base, spec == L_('X')); \ - if (group && grouping) \ - string = group_number(string, workend, grouping, thousands_sep); \ - \ - /*if (use_outdigits && base == 10) \ - string = _i18n_number_rewrite(string, workend);*/ \ - } \ - } \ - \ - if (prec <= workend - string && number.word != 0 \ - && alt && base == 8) \ - /* Add octal marker. */ \ - *--string = L_('0'); \ - \ - prec = MAX(0, prec - (workend - string)); \ - \ - if (!left) { \ - width -= workend - string + prec; \ - \ - if (number.word != 0 && alt && base == 16) { \ - /* Account for 0X hex marker. */ \ - width -= 2; \ - } \ - if (is_negative || showsign || space) \ - --width; \ - \ - if (pad == L_(' ')) { \ - PAD (L_(' ')); \ - width = 0; \ - } \ - \ - if (is_negative) \ - outchar(L_('-')); \ - else if (showsign) \ - outchar(L_('+')); \ - else if (space) \ - outchar(L_(' ')); \ - \ - if (number.word != 0 && alt && base == 16) { \ - outchar(L_('0')); \ - outchar(spec); \ - } \ - \ - width += prec; \ - PAD(L_('0')); \ - \ - outstring(string, workend - string); \ - \ - break; \ - } else { \ - if (is_negative) { \ - outchar(L_('-')); \ - --width; \ - } else if (showsign) { \ - outchar(L_('+')); \ - --width; \ - } else if (space) { \ - outchar (L_(' ')); \ - --width; \ - } \ - \ - if (number.word != 0 && alt && base == 16) { \ - outchar(L_('0')); \ - outchar(spec); \ - width -= 2; \ - } \ - \ - width -= workend - string + prec; \ - \ - if (prec > 0) { \ - int temp = width; \ - width = prec; \ - PAD(L_('0')); \ - width = temp; \ - } \ - \ - outstring(string, workend - string); \ - \ - PAD(L_(' ')); \ - break; \ - } \ - \ - LABEL(form_float): \ - { \ - /* Floating-point number. This is handled by printf_fp.c. */ \ - const void *ptr; \ - int function_done; \ - \ - if (fspec == NULL) { \ - struct printf_info info = { \ - .prec = prec, \ - .width = width, \ - .spec = spec, \ - .is_long_double = is_long_double, \ - .is_short = is_short, \ - .is_long = is_long, \ - .alt = alt, \ - .space = space, \ - .left = left, \ - .showsign = showsign, \ - .group = group, \ - .pad = pad, \ - .extra = 0, \ - .wide = sizeof(CHAR_T) != 1 \ - }; \ - \ - if (is_long_double) \ - the_arg.pa_long_double = va_arg (ap, long double); \ - else \ - the_arg.pa_double = va_arg (ap, double); \ - ptr = (const void *) &the_arg; \ - \ - function_done = -1/*__printf_fp(s, &info, &ptr)*/; \ - } else { \ - ptr = (const void *) &args_value[fspec->data_arg]; \ - \ - function_done = -1/*__printf_fp(s, &fspec->info, &ptr)*/; \ - } \ - \ - if (function_done < 0) { \ - /* Error in print handler. */ \ - done = -1; \ - goto all_done; \ - } \ - \ - done += function_done; \ - } \ - break; \ - \ - LABEL(form_floathex): \ - { \ - /* Floating point number printed as hexadecimal number. */ \ - const void *ptr; \ - int function_done; \ - \ - if (fspec == NULL) { \ - struct printf_info info = { .prec = prec, \ - .width = width, \ - .spec = spec, \ - .is_long_double = is_long_double, \ - .is_short = is_short, \ - .is_long = is_long, \ - .alt = alt, \ - .space = space, \ - .left = left, \ - .showsign = showsign, \ - .group = group, \ - .pad = pad, \ - .extra = 0, \ - .wide = sizeof(CHAR_T) != 1 \ - }; \ - \ - if (is_long_double) \ - the_arg.pa_long_double = va_arg (ap, long double); \ - else \ - the_arg.pa_double = va_arg (ap, double); \ - ptr = (const void *) &the_arg; \ - \ - function_done = -1/*__printf_fphex (s, &info, &ptr)*/; \ - } else { \ - ptr = (const void *) &args_value[fspec->data_arg]; \ - \ - function_done = -1/*__printf_fphex (s, &fspec->info, &ptr)*/; \ - } \ - \ - if (function_done < 0) { \ - /* Error in print handler. */ \ - done = -1; \ - goto all_done; \ - } \ - \ - done += function_done; \ - } \ - break; \ - \ - LABEL(form_pointer): \ - /* Generic pointer. */ \ - { \ - const void *ptr; \ - if (fspec == NULL) \ - ptr = va_arg (ap, void *); \ - else \ - ptr = args_value[fspec->data_arg].pa_pointer; \ - if (ptr != NULL) { \ - /* If the pointer is not NULL, write it as a %#x spec. */ \ - base = 16; \ - number.word = (unsigned long int)ptr; \ - is_negative = 0; \ - alt = 1; \ - group = 0; \ - spec = L_('x'); \ - goto LABEL(number); \ - } else { \ - /* Write "(nil)" for a nil pointer. */ \ - string = (CHAR_T *) L_("(nil)"); \ - /* Make sure the full string "(nil)" is printed. */ \ - if (prec < 5) \ - prec = 5; \ - is_long = 0; /* This is no wide-char string. */ \ - goto LABEL(print_string); \ - } \ - } \ - /* NOTREACHED */ \ - \ - LABEL (form_number): \ - /* Answer the count of characters written. */ \ - if (fspec == NULL) { \ - if (is_longlong) \ - *(long long int *) va_arg (ap, void *) = done; \ - else if (is_long_num) \ - *(long int *) va_arg (ap, void *) = done; \ - else if (is_char) \ - *(char *) va_arg (ap, void *) = done; \ - else if (!is_short) \ - *(int *) va_arg (ap, void *) = done; \ - else \ - *(short int *) va_arg (ap, void *) = done; \ - } else if (is_longlong) \ - *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \ - else if (is_long_num) \ - *(long int *) args_value[fspec->data_arg].pa_pointer = done; \ - else if (is_char) \ - *(char *) args_value[fspec->data_arg].pa_pointer = done; \ - else if (!is_short) \ - *(int *) args_value[fspec->data_arg].pa_pointer = done; \ - else \ - *(short int *) args_value[fspec->data_arg].pa_pointer = done; \ - break; \ - \ - LABEL(form_strerror): \ - /* Print description of error ERRNO. */ \ - string = (CHAR_T *)strerror_r(save_errno, (char *)work_buffer, sizeof(work_buffer)); \ - is_long = 0; /* This is no wide-char string. */ \ - goto LABEL (print_string) -#define process_string_arg(fspec) \ - LABEL(form_character): \ - /* Character. */ \ - --width; /* Account for the character itself. */ \ - if (!left) \ - PAD(' '); \ - if (fspec == NULL) \ - outchar((unsigned char) va_arg (ap, int)); /* Promoted. */ \ - else \ - outchar((unsigned char) args_value[fspec->data_arg].pa_char); \ - if (left) \ - PAD(' '); \ - break; \ - \ - LABEL(form_string): \ - { \ - size_t len; \ - int string_malloced; \ - \ - /* The string argument could in fact be `char *' or \ - * `wchar_t *'. But this should not make a difference here. \ - */ \ - if (fspec == NULL) \ - string = (char *)va_arg(ap, const char *); \ - else \ - string = (char *)args_value[fspec->data_arg].pa_string; \ - \ - /* Entry point for printing other strings. */ \ - LABEL(print_string): \ - string_malloced = 0; \ - if (string == NULL) { \ - /* Write "(null)" if there's space. */ \ - if (prec == -1 || prec >= (int) sizeof (null) - 1) { \ - string = (char *) null; \ - len = sizeof (null) - 1; \ - } else { \ - string = (char *)""; \ - len = 0; \ - } \ - } else { \ - if (prec != -1) \ - len = strnlen(string, prec); \ - else \ - len = strlen(string); \ - } \ - \ - if ((width -= len) < 0) { \ - outstring(string, len); \ - break; \ - } \ - \ - if (!left) \ - PAD(' '); \ - outstring(string, len); \ - if (left) \ - PAD(' '); \ - \ - if (__builtin_expect(string_malloced, 0)) \ - free(string); \ - break; \ - } - /* end of print_string_arg() macro */ +#define process_arg(fspec) \ + /* Start real work. We know about all flags and modifiers and \ + now process the wanted format specifier. */ \ + LABEL (form_percent): \ + /* Write a literal "%". */ \ + outchar (L_('%')); \ + break; \ + \ + LABEL (form_integer): \ + /* Signed decimal integer. */ \ + base = 10; \ + \ + if (is_longlong) \ + { \ + long long int signed_number; \ + \ + if (fspec == NULL) \ + signed_number = va_arg (ap, long long int); \ + else \ + signed_number = args_value[fspec->data_arg].pa_long_long_int; \ + \ + is_negative = signed_number < 0; \ + number.longlong = is_negative ? (- signed_number) : signed_number; \ + \ + goto LABEL (longlong_number); \ + } \ + else \ + { \ + long int signed_number; \ + \ + if (fspec == NULL) \ + { \ + if (is_long_num) \ + signed_number = va_arg (ap, long int); \ + else /* `char' and `short int' will be promoted to `int'. */ \ + signed_number = va_arg (ap, int); \ + } \ + else \ + if (is_long_num) \ + signed_number = args_value[fspec->data_arg].pa_long_int; \ + else \ + signed_number = args_value[fspec->data_arg].pa_int; \ + \ + is_negative = signed_number < 0; \ + number.word = is_negative ? (- signed_number) : signed_number; \ + \ + goto LABEL (number); \ + } \ + /* NOTREACHED */ \ + \ + LABEL (form_unsigned): \ + /* Unsigned decimal integer. */ \ + base = 10; \ + goto LABEL (unsigned_number); \ + /* NOTREACHED */ \ + \ + LABEL (form_octal): \ + /* Unsigned octal integer. */ \ + base = 8; \ + goto LABEL (unsigned_number); \ + /* NOTREACHED */ \ + \ + LABEL (form_hexa): \ + /* Unsigned hexadecimal integer. */ \ + base = 16; \ + \ + LABEL (unsigned_number): /* Unsigned number of base BASE. */ \ + \ + /* ISO specifies the `+' and ` ' flags only for signed \ + conversions. */ \ + is_negative = 0; \ + showsign = 0; \ + space = 0; \ + \ + if (is_longlong) \ + { \ + if (fspec == NULL) \ + number.longlong = va_arg (ap, unsigned long long int); \ + else \ + number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \ + \ + LABEL (longlong_number): \ + if (prec < 0) \ + /* Supply a default precision if none was given. */ \ + prec = 1; \ + else \ + /* We have to take care for the '0' flag. If a precision \ + is given it must be ignored. */ \ + pad = L_(' '); \ + \ + /* If the precision is 0 and the number is 0 nothing has to \ + be written for the number, except for the 'o' format in \ + alternate form. */ \ + if (prec == 0 && number.longlong == 0) \ + { \ + string = workend; \ + if (base == 8 && alt) \ + *--string = L_('0'); \ + } \ + else \ + { \ + /* Put the number in WORK. */ \ + string = _itoa (number.longlong, workend, base, \ + spec == L_('X')); \ + if (group && grouping) \ + string = group_number (string, workend, grouping, \ + thousands_sep); \ + \ + if (use_outdigits && base == 10) \ + string = _i18n_number_rewrite (string, workend); \ + } \ + /* Simplify further test for num != 0. */ \ + number.word = number.longlong != 0; \ + } \ + else \ + { \ + if (fspec == NULL) \ + { \ + if (is_long_num) \ + number.word = va_arg (ap, unsigned long int); \ + else if (is_char) \ + number.word = (unsigned char) va_arg (ap, unsigned int); \ + else if (!is_short) \ + number.word = va_arg (ap, unsigned int); \ + else \ + number.word = (unsigned short int) va_arg (ap, unsigned int); \ + } \ + else \ + if (is_long_num) \ + number.word = args_value[fspec->data_arg].pa_u_long_int; \ + else if (is_char) \ + number.word = (unsigned char) \ + args_value[fspec->data_arg].pa_char; \ + else if (!is_short) \ + number.word = args_value[fspec->data_arg].pa_u_int; \ + else \ + number.word = (unsigned short int) \ + args_value[fspec->data_arg].pa_u_short_int; \ + \ + LABEL (number): \ + if (prec < 0) \ + /* Supply a default precision if none was given. */ \ + prec = 1; \ + else \ + /* We have to take care for the '0' flag. If a precision \ + is given it must be ignored. */ \ + pad = L_(' '); \ + \ + /* If the precision is 0 and the number is 0 nothing has to \ + be written for the number, except for the 'o' format in \ + alternate form. */ \ + if (prec == 0 && number.word == 0) \ + { \ + string = workend; \ + if (base == 8 && alt) \ + *--string = L_('0'); \ + } \ + else \ + { \ + /* Put the number in WORK. */ \ + string = _itoa_word (number.word, workend, base, \ + spec == L_('X')); \ + if (group && grouping) \ + string = group_number (string, workend, grouping, \ + thousands_sep); \ + \ + if (use_outdigits && base == 10) \ + string = _i18n_number_rewrite (string, workend); \ + } \ + } \ + \ + if (prec <= workend - string && number.word != 0 && alt && base == 8) \ + /* Add octal marker. */ \ + *--string = L_('0'); \ + \ + prec = MAX (0, prec - (workend - string)); \ + \ + if (!left) \ + { \ + width -= workend - string + prec; \ + \ + if (number.word != 0 && alt && base == 16) \ + /* Account for 0X hex marker. */ \ + width -= 2; \ + \ + if (is_negative || showsign || space) \ + --width; \ + \ + if (pad == L_(' ')) \ + { \ + PAD (L_(' ')); \ + width = 0; \ + } \ + \ + if (is_negative) \ + outchar (L_('-')); \ + else if (showsign) \ + outchar (L_('+')); \ + else if (space) \ + outchar (L_(' ')); \ + \ + if (number.word != 0 && alt && base == 16) \ + { \ + outchar (L_('0')); \ + outchar (spec); \ + } \ + \ + width += prec; \ + PAD (L_('0')); \ + \ + outstring (string, workend - string); \ + \ + break; \ + } \ + else \ + { \ + if (is_negative) \ + { \ + outchar (L_('-')); \ + --width; \ + } \ + else if (showsign) \ + { \ + outchar (L_('+')); \ + --width; \ + } \ + else if (space) \ + { \ + outchar (L_(' ')); \ + --width; \ + } \ + \ + if (number.word != 0 && alt && base == 16) \ + { \ + outchar (L_('0')); \ + outchar (spec); \ + width -= 2; \ + } \ + \ + width -= workend - string + prec; \ + \ + if (prec > 0) \ + { \ + int temp = width; \ + width = prec; \ + PAD (L_('0'));; \ + width = temp; \ + } \ + \ + outstring (string, workend - string); \ + \ + PAD (L_(' ')); \ + break; \ + } \ + \ + LABEL (form_float): \ + { \ + /* Floating-point number. This is handled by printf_fp.c. */ \ + const void *ptr; \ + int function_done; \ + \ + if (fspec == NULL) \ + { \ + struct printf_info info = { prec: prec, \ + width: width, \ + spec: spec, \ + is_long_double: is_long_double, \ + is_short: is_short, \ + is_long: is_long, \ + alt: alt, \ + space: space, \ + left: left, \ + showsign: showsign, \ + group: group, \ + pad: pad, \ + extra: 0, \ + wide: sizeof (CHAR_T) != 1 }; \ + \ + if (is_long_double) \ + the_arg.pa_long_double = va_arg (ap, long double); \ + else \ + the_arg.pa_double = va_arg (ap, double); \ + ptr = (const void *) &the_arg; \ + \ + function_done = __printf_fp (s, &info, &ptr); \ + } \ + else \ + { \ + ptr = (const void *) &args_value[fspec->data_arg]; \ + \ + function_done = __printf_fp (s, &fspec->info, &ptr); \ + } \ + \ + if (function_done < 0) \ + { \ + /* Error in print handler. */ \ + done = -1; \ + goto all_done; \ + } \ + \ + done += function_done; \ + } \ + break; \ + \ + LABEL (form_floathex): \ + { \ + /* Floating point number printed as hexadecimal number. */ \ + const void *ptr; \ + int function_done; \ + \ + if (fspec == NULL) \ + { \ + struct printf_info info = { prec: prec, \ + width: width, \ + spec: spec, \ + is_long_double: is_long_double, \ + is_short: is_short, \ + is_long: is_long, \ + alt: alt, \ + space: space, \ + left: left, \ + showsign: showsign, \ + group: group, \ + pad: pad, \ + extra: 0, \ + wide: sizeof (CHAR_T) != 1 }; \ + \ + if (is_long_double) \ + the_arg.pa_long_double = va_arg (ap, long double); \ + else \ + the_arg.pa_double = va_arg (ap, double); \ + ptr = (const void *) &the_arg; \ + \ + function_done = __printf_fphex (s, &info, &ptr); \ + } \ + else \ + { \ + ptr = (const void *) &args_value[fspec->data_arg]; \ + \ + function_done = __printf_fphex (s, &fspec->info, &ptr); \ + } \ + \ + if (function_done < 0) \ + { \ + /* Error in print handler. */ \ + done = -1; \ + goto all_done; \ + } \ + \ + done += function_done; \ + } \ + break; \ + \ + LABEL (form_pointer): \ + /* Generic pointer. */ \ + { \ + const void *ptr; \ + if (fspec == NULL) \ + ptr = va_arg (ap, void *); \ + else \ + ptr = args_value[fspec->data_arg].pa_pointer; \ + if (ptr != NULL) \ + { \ + /* If the pointer is not NULL, write it as a %#x spec. */ \ + base = 16; \ + number.word = (unsigned long int) ptr; \ + is_negative = 0; \ + alt = 1; \ + group = 0; \ + spec = L_('x'); \ + goto LABEL (number); \ + } \ + else \ + { \ + /* Write "(nil)" for a nil pointer. */ \ + string = (CHAR_T *) L_("(nil)"); \ + /* Make sure the full string "(nil)" is printed. */ \ + if (prec < 5) \ + prec = 5; \ + is_long = 0; /* This is no wide-char string. */ \ + goto LABEL (print_string); \ + } \ + } \ + /* NOTREACHED */ \ + \ + LABEL (form_number): \ + /* Answer the count of characters written. */ \ + if (fspec == NULL) \ + { \ + if (is_longlong) \ + *(long long int *) va_arg (ap, void *) = done; \ + else if (is_long_num) \ + *(long int *) va_arg (ap, void *) = done; \ + else if (is_char) \ + *(char *) va_arg (ap, void *) = done; \ + else if (is_long_num) \ + *(long int *) va_arg (ap, void *) = done; \ + else if (!is_short) \ + *(int *) va_arg (ap, void *) = done; \ + else \ + *(short int *) va_arg (ap, void *) = done; \ + } \ + else \ + if (is_longlong) \ + *(long long int *) args_value[fspec->data_arg].pa_pointer = done; \ + else if (is_long_num) \ + *(long int *) args_value[fspec->data_arg].pa_pointer = done; \ + else if (is_long_num) \ + *(long int *) args_value[fspec->data_arg].pa_pointer = done; \ + else if (is_char) \ + *(char *) args_value[fspec->data_arg].pa_pointer = done; \ + else if (!is_short) \ + *(int *) args_value[fspec->data_arg].pa_pointer = done; \ + else \ + *(short int *) args_value[fspec->data_arg].pa_pointer = done; \ + break; \ + \ + LABEL (form_strerror): \ + /* Print description of error ERRNO. */ \ + string = \ + (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer, \ + sizeof work_buffer); \ + is_long = 0; /* This is no wide-char string. */ \ + goto LABEL (print_string) - /* Orient the stream. */ -#ifdef ORIENT - ORIENT; +#ifdef COMPILE_WPRINTF +# define process_string_arg(fspec) \ + LABEL (form_character): \ + /* Character. */ \ + if (is_long) \ + goto LABEL (form_wcharacter); \ + --width; /* Account for the character itself. */ \ + if (!left) \ + PAD (L' '); \ + if (fspec == NULL) \ + outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \ + else \ + outchar (__btowc ((unsigned char) \ + args_value[fspec->data_arg].pa_char)); \ + if (left) \ + PAD (L' '); \ + break; \ + \ + LABEL (form_wcharacter): \ + { \ + /* Wide character. */ \ + --width; \ + if (!left) \ + PAD (L' '); \ + if (fspec == NULL) \ + outchar (va_arg (ap, wint_t)); \ + else \ + outchar (args_value[fspec->data_arg].pa_wchar); \ + if (left) \ + PAD (L' '); \ + } \ + break; \ + \ + LABEL (form_string): \ + { \ + size_t len; \ + int string_malloced; \ + \ + /* The string argument could in fact be `char *' or `wchar_t *'. \ + But this should not make a difference here. */ \ + if (fspec == NULL) \ + string = (CHAR_T *) va_arg (ap, const wchar_t *); \ + else \ + string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring; \ + \ + /* Entry point for printing other strings. */ \ + LABEL (print_string): \ + \ + string_malloced = 0; \ + if (string == NULL) \ + { \ + /* Write "(null)" if there's space. */ \ + if (prec == -1 \ + || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1) \ + { \ + string = (CHAR_T *) null; \ + len = (sizeof (null) / sizeof (null[0])) - 1; \ + } \ + else \ + { \ + string = (CHAR_T *) L""; \ + len = 0; \ + } \ + } \ + else if (!is_long && spec != L_('S')) \ + { \ + /* This is complicated. We have to transform the multibyte \ + string into a wide character string. */ \ + const char *mbs = (const char *) string; \ + mbstate_t mbstate; \ + \ + len = prec != -1 ? prec : strlen (mbs); \ + \ + /* Allocate dynamically an array which definitely is long \ + enough for the wide character version. */ \ + if (len < 8192 \ + || ((string = (CHAR_T *) malloc (len * sizeof (wchar_t))) \ + == NULL)) \ + string = (CHAR_T *) alloca (len * sizeof (wchar_t)); \ + else \ + string_malloced = 1; \ + \ + memset (&mbstate, '\0', sizeof (mbstate_t)); \ + len = __mbsrtowcs (string, &mbs, len, &mbstate); \ + if (len == (size_t) -1) \ + { \ + /* Illegal multibyte character. */ \ + done = -1; \ + goto all_done; \ + } \ + } \ + else \ + { \ + if (prec != -1) \ + /* Search for the end of the string, but don't search past \ + the length specified by the precision. */ \ + len = __wcsnlen (string, prec); \ + else \ + len = __wcslen (string); \ + } \ + \ + if ((width -= len) < 0) \ + { \ + outstring (string, len); \ + break; \ + } \ + \ + if (!left) \ + PAD (L' '); \ + outstring (string, len); \ + if (left) \ + PAD (L' '); \ + if (string_malloced) \ + free (string); \ + } \ + break; +#else +# define process_string_arg(fspec) \ + LABEL (form_character): \ + /* Character. */ \ + if (is_long) \ + goto LABEL (form_wcharacter); \ + --width; /* Account for the character itself. */ \ + if (!left) \ + PAD (' '); \ + if (fspec == NULL) \ + outchar ((unsigned char) va_arg (ap, int)); /* Promoted. */ \ + else \ + outchar ((unsigned char) args_value[fspec->data_arg].pa_char); \ + if (left) \ + PAD (' '); \ + break; \ + \ + LABEL (form_wcharacter): \ + { \ + /* Wide character. */ \ + char buf[MB_CUR_MAX]; \ + mbstate_t mbstate; \ + size_t len; \ + \ + memset (&mbstate, '\0', sizeof (mbstate_t)); \ + len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wint_t) \ + : args_value[fspec->data_arg].pa_wchar), \ + &mbstate); \ + if (len == (size_t) -1) \ + { \ + /* Something went wron gduring the conversion. Bail out. */ \ + done = -1; \ + goto all_done; \ + } \ + width -= len; \ + if (!left) \ + PAD (' '); \ + outstring (buf, len); \ + if (left) \ + PAD (' '); \ + } \ + break; \ + \ + LABEL (form_string): \ + { \ + size_t len; \ + int string_malloced; \ + \ + /* The string argument could in fact be `char *' or `wchar_t *'. \ + But this should not make a difference here. */ \ + if (fspec == NULL) \ + string = (char *) va_arg (ap, const char *); \ + else \ + string = (char *) args_value[fspec->data_arg].pa_string; \ + \ + /* Entry point for printing other strings. */ \ + LABEL (print_string): \ + \ + string_malloced = 0; \ + if (string == NULL) \ + { \ + /* Write "(null)" if there's space. */ \ + if (prec == -1 || prec >= (int) sizeof (null) - 1) \ + { \ + string = (char *) null; \ + len = sizeof (null) - 1; \ + } \ + else \ + { \ + string = (char *) ""; \ + len = 0; \ + } \ + } \ + else if (!is_long && spec != L_('S')) \ + { \ + if (prec != -1) \ + { \ + /* Search for the end of the string, but don't search past \ + the length (in bytes) specified by the precision. Also \ + don't use incomplete characters. */ \ + if (_NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_MB_CUR_MAX) == 1) \ + len = __strnlen (string, prec); \ + else \ + { \ + /* In case we have a multibyte character set the \ + situation is more compilcated. We must not copy \ + bytes at the end which form an incomplete character. */\ + wchar_t ignore[prec]; \ + const char *str2 = string; \ + mbstate_t ps; \ + \ + memset (&ps, '\0', sizeof (ps)); \ + if (__mbsnrtowcs (ignore, &str2, prec, prec, &ps) \ + == (size_t) -1) \ + { \ + done = -1; \ + goto all_done; \ + } \ + if (str2 == NULL) \ + len = strlen (string); \ + else \ + len = str2 - string - (ps.__count); \ + } \ + } \ + else \ + len = strlen (string); \ + } \ + else \ + { \ + const wchar_t *s2 = (const wchar_t *) string; \ + mbstate_t mbstate; \ + \ + memset (&mbstate, '\0', sizeof (mbstate_t)); \ + \ + if (prec >= 0) \ + { \ + /* The string `s2' might not be NUL terminated. */ \ + if (prec < 32768 \ + || (string = (char *) malloc (prec)) == NULL) \ + string = (char *) alloca (prec); \ + else \ + string_malloced = 1; \ + len = __wcsrtombs (string, &s2, prec, &mbstate); \ + } \ + else \ + { \ + len = __wcsrtombs (NULL, &s2, 0, &mbstate); \ + if (len != (size_t) -1) \ + { \ + assert (__mbsinit (&mbstate)); \ + s2 = (const wchar_t *) string; \ + if (len + 1 < 32768 \ + || (string = (char *) malloc (len + 1)) == NULL) \ + string = (char *) alloca (len + 1); \ + else \ + string_malloced = 1; \ + (void) __wcsrtombs (string, &s2, len + 1, &mbstate); \ + } \ + } \ + \ + if (len == (size_t) -1) \ + { \ + /* Illegal wide-character string. */ \ + done = -1; \ + goto all_done; \ + } \ + } \ + \ + if ((width -= len) < 0) \ + { \ + outstring (string, len); \ + break; \ + } \ + \ + if (!left) \ + PAD (' '); \ + outstring (string, len); \ + if (left) \ + PAD (' '); \ + if (string_malloced) \ + free (string); \ + } \ + break; #endif - /* Sanity check of arguments. */ - ARGCHECK (s, format); - + /* Orient the stream. */ #ifdef ORIENT - /* Check for correct orientation. */ - if (s->_vtable_offset == 0 && _IO_fwide(s, -1) != -1) -// && _IO_fwide(s, sizeof(CHAR_T) == 1 ? -1 : 1) != (sizeof(CHAR_T) == 1 ? -1 : 1)) - /* The stream is already oriented otherwise. */ - return EOF; + ORIENT; +#endif + + /* Sanity check of arguments. */ + ARGCHECK (s, format); + +#ifdef ORIENT + /* Check for correct orientation. */ + if ( +# ifdef USE_IN_LIBIO + s->_vtable_offset == 0 && +# endif + _IO_fwide (s, sizeof (CHAR_T) == 1 ? -1 : 1) + != (sizeof (CHAR_T) == 1 ? -1 : 1)) + /* The stream is already oriented otherwise. */ + return EOF; #endif -// ToDo: this needs to be tested/rewritten/fixed first! -#if 0 if (UNBUFFERED_P (s)) /* Use a helper function which will allocate a local temporary buffer for the stream and then call us again. */ return buffered_vfprintf (s, format, ap); -#endif - /* Initialize local variables. */ - done = 0; - grouping = (const char *) -1; + /* Initialize local variables. */ + done = 0; + grouping = (const char *) -1; #ifdef __va_copy - /* This macro will be available soon in gcc's . We need it - * since on some systems `va_list' is not an integral type. - */ - __va_copy (ap_save, ap); + /* This macro will be available soon in gcc's . We need it + since on some systems `va_list' is not an integral type. */ + __va_copy (ap_save, ap); #else - ap_save = ap; + ap_save = ap; #endif - nspecs_done = 0; + nspecs_done = 0; -//#ifdef COMPILE_WPRINTF - /* Find the first format specifier. */ - f = lead_str_end = find_spec((const UCHAR_T *) format); -#if 0 +#ifdef COMPILE_WPRINTF + /* Find the first format specifier. */ + f = lead_str_end = find_spec ((const UCHAR_T *) format); +#else /* Put state for processing format string in initial state. */ memset (&mbstate, '\0', sizeof (mbstate_t)); @@ -950,718 +1294,737 @@ _IO_vfprintf(FILE *s, const CHAR_T *format, va_list ap) f = lead_str_end = find_spec (format, &mbstate); #endif - /* Lock stream. */ - _IO_cleanup_region_start((void (*) (void *)) &_IO_funlockfile, s); - _IO_flockfile(s); - - /* Write the literal text before the first format. */ - outstring((const UCHAR_T *)format, lead_str_end - (const UCHAR_T *)format); - - /* If we only have to print a simple string, return now. */ - if (*f == L_('\0')) - goto all_done; - - /* Process whole format string. */ - do { -#if defined HAVE_SUBTRACT_LOCAL_LABELS && defined SHARED -# define REF(Name) &&do_##Name - &&do_form_unknown + /* Lock stream. */ +#ifdef USE_IN_LIBIO + __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, s); + _IO_flockfile (s); #else -# define REF(Name) &&do_##Name + __libc_cleanup_region_start (1, (void (*) (void *)) &__funlockfile, s); + __flockfile (s); +#endif + + /* Write the literal text before the first format. */ + outstring ((const UCHAR_T *) format, + lead_str_end - (const UCHAR_T *) format); + + /* If we only have to print a simple string, return now. */ + if (*f == L_('\0')) + goto all_done; + + /* Process whole format string. */ + do + { +#if defined HAVE_SUBTRACT_LOCAL_LABELS && defined SHARED +# define REF(Name) &&do_##Name - &&do_form_unknown +#else +# define REF(Name) &&do_##Name #endif #define LABEL(Name) do_##Name - STEP0_3_TABLE; - STEP4_TABLE; - union printf_arg *args_value; /* This is not used here but ... */ - int is_negative; /* Flag for negative number. */ - union { - unsigned long long int longlong; - unsigned long int word; - } number; - int base; - union printf_arg the_arg; - CHAR_T *string; /* Pointer to argument string. */ - int alt = 0; /* Alternate format. */ - int space = 0; /* Use space prefix if no sign is needed. */ - int left = 0; /* Left-justify output. */ - int showsign = 0; /* Always begin with plus or minus sign. */ - int group = 0; /* Print numbers according grouping rules. */ - int is_long_double = 0; /* Argument is long double/ long long int. */ - int is_short = 0; /* Argument is short int. */ - int is_long = 0; /* Argument is long int. */ - int is_char = 0; /* Argument is promoted (unsigned) char. */ - int width = 0; /* Width of output; 0 means none specified. */ - int prec = -1; /* Precision of output; -1 means none specified. */ - /* This flag is set by the 'I' modifier and selects the use of the - * `outdigits' as determined by the current locale. - */ - int use_outdigits = 0; - UCHAR_T pad = L_(' ');/* Padding character. */ - CHAR_T spec; + STEP0_3_TABLE; + STEP4_TABLE; - workstart = NULL; - workend = &work_buffer[sizeof(work_buffer) / sizeof(CHAR_T)]; + union printf_arg *args_value; /* This is not used here but ... */ + int is_negative; /* Flag for negative number. */ + union + { + unsigned long long int longlong; + unsigned long int word; + } number; + int base; + union printf_arg the_arg; + CHAR_T *string; /* Pointer to argument string. */ + int alt = 0; /* Alternate format. */ + int space = 0; /* Use space prefix if no sign is needed. */ + int left = 0; /* Left-justify output. */ + int showsign = 0; /* Always begin with plus or minus sign. */ + int group = 0; /* Print numbers according grouping rules. */ + int is_long_double = 0; /* Argument is long double/ long long int. */ + int is_short = 0; /* Argument is short int. */ + int is_long = 0; /* Argument is long int. */ + int is_char = 0; /* Argument is promoted (unsigned) char. */ + int width = 0; /* Width of output; 0 means none specified. */ + int prec = -1; /* Precision of output; -1 means none specified. */ + /* This flag is set by the 'I' modifier and selects the use of the + `outdigits' as determined by the current locale. */ + int use_outdigits = 0; + UCHAR_T pad = L_(' ');/* Padding character. */ + CHAR_T spec; - /* Get current character in format string. */ - JUMP(*++f, step0_jumps); + workend = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)]; - /* ' ' flag. */ - LABEL(flag_space): - space = 1; - JUMP(*++f, step0_jumps); + /* Get current character in format string. */ + JUMP (*++f, step0_jumps); - /* '+' flag. */ - LABEL(flag_plus): - showsign = 1; - JUMP(*++f, step0_jumps); + /* ' ' flag. */ + LABEL (flag_space): + space = 1; + JUMP (*++f, step0_jumps); - /* The '-' flag. */ - LABEL(flag_minus): - left = 1; - pad = L_(' '); - JUMP (*++f, step0_jumps); + /* '+' flag. */ + LABEL (flag_plus): + showsign = 1; + JUMP (*++f, step0_jumps); - /* The '#' flag. */ - LABEL(flag_hash): - alt = 1; - JUMP (*++f, step0_jumps); + /* The '-' flag. */ + LABEL (flag_minus): + left = 1; + pad = L_(' '); + JUMP (*++f, step0_jumps); - /* The '0' flag. */ - LABEL (flag_zero): - if (!left) - pad = L_('0'); - JUMP (*++f, step0_jumps); + /* The '#' flag. */ + LABEL (flag_hash): + alt = 1; + JUMP (*++f, step0_jumps); - /* The '\'' flag. */ - LABEL (flag_quote): - group = 1; + /* The '0' flag. */ + LABEL (flag_zero): + if (!left) + pad = L_('0'); + JUMP (*++f, step0_jumps); - if (grouping == (const char *) -1) { -#ifdef COMPILE_WPRINTF - thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_THOUSANDS_SEP_WC); -#else - thousands_sep = ","; //_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP); -#endif + /* The '\'' flag. */ + LABEL (flag_quote): + group = 1; - grouping = NULL; //_NL_CURRENT (LC_NUMERIC, GROUPING); - if (*grouping == '\0' || *grouping == CHAR_MAX -#ifdef COMPILE_WPRINTF - || thousands_sep == L'\0' -#else - || *thousands_sep == '\0' -#endif - ) - grouping = NULL; - } - JUMP(*++f, step0_jumps); - - LABEL(flag_i18n): - use_outdigits = 1; - JUMP(*++f, step0_jumps); - - /* Get width from argument. */ - LABEL(width_asterics): - { - const UCHAR_T *tmp; /* Temporary value. */ - - tmp = ++f; - if (ISDIGIT (*tmp) && read_int (&tmp) && *tmp == L_('$')) { - /* The width comes from a positional parameter. */ - goto do_positional; - } - - width = va_arg(ap, int); - - /* Negative width means left justified. */ - if (width < 0) { - width = -width; - pad = L_(' '); - left = 1; - } - - if (width + 32 >= (int)(sizeof(work_buffer) / sizeof(work_buffer[0]))) { - /* We have to use a special buffer. The "32" is just a safe - * bet for all the output which is not counted in the width. - */ - if (__libc_use_alloca((width + 32) * sizeof(CHAR_T))) - workend = ((CHAR_T *)alloca((width + 32) * sizeof(CHAR_T)) + (width + 32)); - else { - workstart = (CHAR_T *)malloc((width + 32) * sizeof(CHAR_T)); - if (workstart == NULL) { - done = -1; - goto all_done; - } - workend = workstart + (width + 32); - } - } - } - JUMP(*f, step1_jumps); - - /* Given width in format string. */ - LABEL(width): - width = read_int(&f); - - if (width + 32 >= (int)(sizeof(work_buffer) / sizeof(work_buffer[0]))) { - /* We have to use a special buffer. The "32" is just a safe - * bet for all the output which is not counted in the width. - */ - if (__libc_use_alloca((width + 32) * sizeof(CHAR_T))) - workend = ((CHAR_T *)alloca((width + 32) * sizeof(CHAR_T)) + (width + 32)); - else { - workstart = (CHAR_T *)malloc((width + 32) * sizeof(CHAR_T)); - if (workstart == NULL) { - done = -1; - goto all_done; - } - workend = workstart + (width + 32); - } - } - - if (*f == L_('$')) { - /* Oh, oh. The argument comes from a positional parameter. */ - goto do_positional; - } - - JUMP(*f, step1_jumps); - - LABEL(precision): - ++f; - if (*f == L_('*')) { - const UCHAR_T *tmp; /* Temporary value. */ - - tmp = ++f; - if (ISDIGIT(*tmp) && read_int(&tmp) > 0 && *tmp == L_('$')) { - /* The precision comes from a positional parameter. */ - goto do_positional; - } - - prec = va_arg (ap, int); - - /* If the precision is negative the precision is omitted. */ - if (prec < 0) - prec = -1; - } else if (ISDIGIT(*f)) - prec = read_int(&f); - else - prec = 0; - - if (prec > width && prec + 32 > (int)(sizeof(work_buffer) / sizeof(work_buffer[0]))) { - if (__libc_use_alloca((prec + 32) * sizeof(CHAR_T))) - workend = ((CHAR_T *)alloca((prec + 32) * sizeof(CHAR_T))) + (prec + 32); - else { - workstart = (CHAR_T *)malloc((prec + 32) * sizeof(CHAR_T)); - if (workstart == NULL) { - done = -1; - goto all_done; - } - workend = workstart + (prec + 32); - } - } - JUMP(*f, step2_jumps); - - /* Process 'h' modifier. There might another 'h' following. */ - LABEL(mod_half): - is_short = 1; - JUMP(*++f, step3a_jumps); - - /* Process 'hh' modifier. */ - LABEL(mod_halfhalf): - is_short = 0; - is_char = 1; - JUMP(*++f, step4_jumps); - - /* Process 'l' modifier. There might another 'l' following. */ - LABEL(mod_long): - is_long = 1; - JUMP(*++f, step3b_jumps); - - /* Process 'L', 'q', or 'll' modifier. No other modifier is - * allowed to follow. - */ - LABEL(mod_longlong): - is_long_double = 1; - is_long = 1; - JUMP(*++f, step4_jumps); - - LABEL(mod_size_t): - is_long_double = sizeof(size_t) > sizeof(unsigned long int); - is_long = sizeof(size_t) > sizeof(unsigned int); - JUMP(*++f, step4_jumps); - - LABEL(mod_ptrdiff_t): - is_long_double = sizeof(ptrdiff_t) > sizeof(unsigned long int); - is_long = sizeof(ptrdiff_t) > sizeof(unsigned int); - JUMP(*++f, step4_jumps); - - LABEL(mod_intmax_t): - is_long_double = sizeof(intmax_t) > sizeof(unsigned long int); - is_long = sizeof(intmax_t) > sizeof(unsigned int); - JUMP(*++f, step4_jumps); - - /* Process current format. */ - while (1) { - process_arg(((struct printf_spec *)NULL)); - process_string_arg(((struct printf_spec *) NULL)); - - LABEL (form_unknown): - if (spec == L_('\0')) { - /* The format string ended before the specifier is complete. */ - done = -1; - goto all_done; - } - - /* If we are in the fast loop force entering the complicated one. */ - goto do_positional; - } - - /* The format is correctly handled. */ - ++nspecs_done; - - if (__builtin_expect(workstart != NULL, 0)) - free(workstart); - workstart = NULL; - - /* Look for next format specifier. */ -//#ifdef COMPILE_WPRINTF - f = find_spec((end_of_spec = ++f)); -//#else -// f = find_spec((end_of_spec = ++f), &mbstate); -//#endif - - /* Write the following constant string. */ - outstring(end_of_spec, f - end_of_spec); - } while (*f != L_('\0')); - - /* Unlock stream and return. */ - goto all_done; - -/* Here starts the more complex loop to handle positional parameters. */ -do_positional: + if (grouping == (const char *) -1) { - /* Array with information about the needed arguments. - * This has to be dynamically extensible. - */ - size_t nspecs = 0; - size_t nspecs_max = 32; /* A more or less arbitrary start value. */ - struct printf_spec *specs = alloca(nspecs_max * sizeof(struct printf_spec)); - - /* The number of arguments the format string requests. This will - * determine the size of the array needed to store the argument - * attributes. - */ - size_t nargs = 0; - int *args_type; - union printf_arg *args_value = NULL; - - /* Positional parameters refer to arguments directly. This could - * also determine the maximum number of arguments. Track the - * maximum number. - */ - size_t max_ref_arg = 0; - - /* Just a counter. */ - size_t cnt; - - if (grouping == (const char *)-1) { #ifdef COMPILE_WPRINTF - thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_THOUSANDS_SEP_WC); + thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC, + _NL_NUMERIC_THOUSANDS_SEP_WC); #else - thousands_sep = ","; //_NL_CURRENT(LC_NUMERIC, THOUSANDS_SEP); + thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP); #endif - grouping = NULL; //_NL_CURRENT (LC_NUMERIC, GROUPING); - if (*grouping == '\0' || *grouping == CHAR_MAX) - grouping = NULL; - } + grouping = _NL_CURRENT (LC_NUMERIC, GROUPING); + if (*grouping == '\0' || *grouping == CHAR_MAX +#ifdef COMPILE_WPRINTF + || thousands_sep == L'\0' +#else + || *thousands_sep == '\0' +#endif + ) + grouping = NULL; + } + JUMP (*++f, step0_jumps); - for (f = lead_str_end; *f != L_('\0'); f = specs[nspecs++].next_fmt) { - if (nspecs >= nspecs_max) { - /* Extend the array of format specifiers. */ - struct printf_spec *old = specs; + LABEL (flag_i18n): + use_outdigits = 1; + JUMP (*++f, step0_jumps); - nspecs_max *= 2; - specs = alloca(nspecs_max * sizeof(struct printf_spec)); + /* Get width from argument. */ + LABEL (width_asterics): + { + const UCHAR_T *tmp; /* Temporary value. */ - if (specs == &old[nspecs]) { - /* Stack grows up, OLD was the last thing allocated; - * extend it. - */ - nspecs_max += nspecs_max / 2; - } else { - /* Copy the old array's elements to the new space. */ - memcpy(specs, old, nspecs * sizeof(struct printf_spec)); - if (old == &specs[nspecs]) { - /* Stack grows down, OLD was just below the new - * SPECS. We can use that space when the new - * space runs out. - */ - nspecs_max += nspecs_max / 2; - } - } - } + tmp = ++f; + if (ISDIGIT (*tmp) && read_int (&tmp) && *tmp == L_('$')) + /* The width comes from a positional parameter. */ + goto do_positional; - /* Parse the format specifier. */ -//#ifdef COMPILE_WPRINTF - nargs += parse_one_spec (f, nargs, &specs[nspecs], &max_ref_arg); -//#else -// nargs += parse_one_spec (f, nargs, &specs[nspecs], &max_ref_arg, &mbstate); -//#endif - } + width = va_arg (ap, int); - /* Determine the number of arguments the format string consumes. */ - nargs = MAX(nargs, max_ref_arg); + /* Negative width means left justified. */ + if (width < 0) + { + width = -width; + pad = L_(' '); + left = 1; + } - /* Allocate memory for the argument descriptions. */ - args_type = alloca(nargs * sizeof(int)); - memset(args_type, 0, nargs * sizeof(int)); - args_value = alloca (nargs * sizeof(union printf_arg)); + if (width + 32 >= (int) (sizeof (work_buffer) / sizeof (work_buffer[0]))) + /* We have to use a special buffer. The "32" is just a safe + bet for all the output which is not counted in the width. */ + workend = ((CHAR_T *) alloca ((width + 32) * sizeof (CHAR_T)) + + (width + 32)); + } + JUMP (*f, step1_jumps); - /* XXX Could do sanity check here: If any element in ARGS_TYPE is - * still zero after this loop, format is invalid. For now we - * simply use 0 as the value. - */ + /* Given width in format string. */ + LABEL (width): + width = read_int (&f); - /* Fill in the types of all the arguments. */ - for (cnt = 0; cnt < nspecs; ++cnt) { - /* If the width is determined by an argument this is an int. */ - if (specs[cnt].width_arg != -1) - args_type[specs[cnt].width_arg] = PA_INT; + if (width + 32 >= (int) (sizeof (work_buffer) / sizeof (work_buffer[0]))) + /* We have to use a special buffer. The "32" is just a safe + bet for all the output which is not counted in the width. */ + workend = ((CHAR_T *) alloca ((width + 32) * sizeof (CHAR_T)) + + (width + 32)); + if (*f == L_('$')) + /* Oh, oh. The argument comes from a positional parameter. */ + goto do_positional; + JUMP (*f, step1_jumps); - /* If the precision is determined by an argument this is an int. */ - if (specs[cnt].prec_arg != -1) - args_type[specs[cnt].prec_arg] = PA_INT; + LABEL (precision): + ++f; + if (*f == L_('*')) + { + const UCHAR_T *tmp; /* Temporary value. */ - switch (specs[cnt].ndata_args) { - case 0: /* No arguments. */ - break; - case 1: /* One argument; we already have the type. */ - args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type; - break; - default: - /* We have more than one argument for this format spec. - * We must call the arginfo function again to determine - * all the types. - */ - (*__printf_arginfo_table[specs[cnt].info.spec])(&specs[cnt].info, - specs[cnt].ndata_args, &args_type[specs[cnt].data_arg]); - break; - } - } + tmp = ++f; + if (ISDIGIT (*tmp) && read_int (&tmp) > 0 && *tmp == L_('$')) + /* The precision comes from a positional parameter. */ + goto do_positional; - /* Now we know all the types and the order. Fill in the argument values. */ - for (cnt = 0; cnt < nargs; ++cnt) { - switch (args_type[cnt]) { -#define T(tag, mem, type) \ - case tag: \ - args_value[cnt].mem = va_arg (ap_save, type); \ - break + prec = va_arg (ap, int); - T(PA_CHAR, pa_char, int); /* Promoted. */ - T(PA_WCHAR, pa_wchar, wint_t); - T(PA_INT|PA_FLAG_SHORT, pa_short_int, int); /* Promoted. */ - T(PA_INT, pa_int, int); - T(PA_INT|PA_FLAG_LONG, pa_long_int, long int); - T(PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int); - T(PA_FLOAT, pa_float, double); /* Promoted. */ - T(PA_DOUBLE, pa_double, double); - T(PA_DOUBLE|PA_FLAG_LONG_DOUBLE, pa_long_double, long double); - T(PA_STRING, pa_string, const char *); - T(PA_WSTRING, pa_wstring, const wchar_t *); - T(PA_POINTER, pa_pointer, void *); + /* If the precision is negative the precision is omitted. */ + if (prec < 0) + prec = -1; + } + else if (ISDIGIT (*f)) + prec = read_int (&f); + else + prec = 0; + if (prec > width + && prec + 32 > (int)(sizeof (work_buffer) / sizeof (work_buffer[0]))) + workend = alloca (spec + 32) + (spec + 32); + JUMP (*f, step2_jumps); + + /* Process 'h' modifier. There might another 'h' following. */ + LABEL (mod_half): + is_short = 1; + JUMP (*++f, step3a_jumps); + + /* Process 'hh' modifier. */ + LABEL (mod_halfhalf): + is_short = 0; + is_char = 1; + JUMP (*++f, step4_jumps); + + /* Process 'l' modifier. There might another 'l' following. */ + LABEL (mod_long): + is_long = 1; + JUMP (*++f, step3b_jumps); + + /* Process 'L', 'q', or 'll' modifier. No other modifier is + allowed to follow. */ + LABEL (mod_longlong): + is_long_double = 1; + is_long = 1; + JUMP (*++f, step4_jumps); + + LABEL (mod_size_t): + is_long_double = sizeof (size_t) > sizeof (unsigned long int); + is_long = sizeof (size_t) > sizeof (unsigned int); + JUMP (*++f, step4_jumps); + + LABEL (mod_ptrdiff_t): + is_long_double = sizeof (ptrdiff_t) > sizeof (unsigned long int); + is_long = sizeof (ptrdiff_t) > sizeof (unsigned int); + JUMP (*++f, step4_jumps); + + LABEL (mod_intmax_t): + is_long_double = sizeof (intmax_t) > sizeof (unsigned long int); + is_long = sizeof (intmax_t) > sizeof (unsigned int); + JUMP (*++f, step4_jumps); + + /* Process current format. */ + while (1) + { + process_arg (((struct printf_spec *) NULL)); + process_string_arg (((struct printf_spec *) NULL)); + + LABEL (form_unknown): + if (spec == L_('\0')) + { + /* The format string ended before the specifier is complete. */ + done = -1; + goto all_done; + } + + /* If we are in the fast loop force entering the complicated + one. */ + goto do_positional; + } + + /* The format is correctly handled. */ + ++nspecs_done; + + /* Look for next format specifier. */ +#ifdef COMPILE_WPRINTF + f = find_spec ((end_of_spec = ++f)); +#else + f = find_spec ((end_of_spec = ++f), &mbstate); +#endif + + /* Write the following constant string. */ + outstring (end_of_spec, f - end_of_spec); + } + while (*f != L_('\0')); + + /* Unlock stream and return. */ + goto all_done; + + /* Here starts the more complex loop to handle positional parameters. */ +do_positional: + { + /* Array with information about the needed arguments. This has to + be dynamically extensible. */ + size_t nspecs = 0; + size_t nspecs_max = 32; /* A more or less arbitrary start value. */ + struct printf_spec *specs + = alloca (nspecs_max * sizeof (struct printf_spec)); + + /* The number of arguments the format string requests. This will + determine the size of the array needed to store the argument + attributes. */ + size_t nargs = 0; + int *args_type; + union printf_arg *args_value = NULL; + + /* Positional parameters refer to arguments directly. This could + also determine the maximum number of arguments. Track the + maximum number. */ + size_t max_ref_arg = 0; + + /* Just a counter. */ + size_t cnt; + + + if (grouping == (const char *) -1) + { +#ifdef COMPILE_WPRINTF + thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC, + _NL_NUMERIC_THOUSANDS_SEP_WC); +#else + thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP); +#endif + + grouping = _NL_CURRENT (LC_NUMERIC, GROUPING); + if (*grouping == '\0' || *grouping == CHAR_MAX) + grouping = NULL; + } + + for (f = lead_str_end; *f != L_('\0'); f = specs[nspecs++].next_fmt) + { + if (nspecs >= nspecs_max) + { + /* Extend the array of format specifiers. */ + struct printf_spec *old = specs; + + nspecs_max *= 2; + specs = alloca (nspecs_max * sizeof (struct printf_spec)); + + if (specs == &old[nspecs]) + /* Stack grows up, OLD was the last thing allocated; + extend it. */ + nspecs_max += nspecs_max / 2; + else + { + /* Copy the old array's elements to the new space. */ + memcpy (specs, old, nspecs * sizeof (struct printf_spec)); + if (old == &specs[nspecs]) + /* Stack grows down, OLD was just below the new + SPECS. We can use that space when the new space + runs out. */ + nspecs_max += nspecs_max / 2; + } + } + + /* Parse the format specifier. */ +#ifdef COMPILE_WPRINTF + nargs += parse_one_spec (f, nargs, &specs[nspecs], &max_ref_arg); +#else + nargs += parse_one_spec (f, nargs, &specs[nspecs], &max_ref_arg, + &mbstate); +#endif + } + + /* Determine the number of arguments the format string consumes. */ + nargs = MAX (nargs, max_ref_arg); + + /* Allocate memory for the argument descriptions. */ + args_type = alloca (nargs * sizeof (int)); + memset (args_type, 0, nargs * sizeof (int)); + args_value = alloca (nargs * sizeof (union printf_arg)); + + /* XXX Could do sanity check here: If any element in ARGS_TYPE is + still zero after this loop, format is invalid. For now we + simply use 0 as the value. */ + + /* Fill in the types of all the arguments. */ + for (cnt = 0; cnt < nspecs; ++cnt) + { + /* If the width is determined by an argument this is an int. */ + if (specs[cnt].width_arg != -1) + args_type[specs[cnt].width_arg] = PA_INT; + + /* If the precision is determined by an argument this is an int. */ + if (specs[cnt].prec_arg != -1) + args_type[specs[cnt].prec_arg] = PA_INT; + + switch (specs[cnt].ndata_args) + { + case 0: /* No arguments. */ + break; + case 1: /* One argument; we already have the type. */ + args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type; + break; + default: + /* We have more than one argument for this format spec. + We must call the arginfo function again to determine + all the types. */ + (void) (*__printf_arginfo_table[specs[cnt].info.spec]) + (&specs[cnt].info, + specs[cnt].ndata_args, &args_type[specs[cnt].data_arg]); + break; + } + } + + /* Now we know all the types and the order. Fill in the argument + values. */ + for (cnt = 0; cnt < nargs; ++cnt) + switch (args_type[cnt]) + { +#define T(tag, mem, type) \ + case tag: \ + args_value[cnt].mem = va_arg (ap_save, type); \ + break + + T (PA_CHAR, pa_char, int); /* Promoted. */ + T (PA_WCHAR, pa_wchar, wint_t); + T (PA_INT|PA_FLAG_SHORT, pa_short_int, int); /* Promoted. */ + T (PA_INT, pa_int, int); + T (PA_INT|PA_FLAG_LONG, pa_long_int, long int); + T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int); + T (PA_FLOAT, pa_float, double); /* Promoted. */ + T (PA_DOUBLE, pa_double, double); + T (PA_DOUBLE|PA_FLAG_LONG_DOUBLE, pa_long_double, long double); + T (PA_STRING, pa_string, const char *); + T (PA_WSTRING, pa_wstring, const wchar_t *); + T (PA_POINTER, pa_pointer, void *); #undef T - default: - if ((args_type[cnt] & PA_FLAG_PTR) != 0) - args_value[cnt].pa_pointer = va_arg (ap_save, void *); - else - args_value[cnt].pa_long_double = 0.0; - break; - } - } + default: + if ((args_type[cnt] & PA_FLAG_PTR) != 0) + args_value[cnt].pa_pointer = va_arg (ap_save, void *); + else + args_value[cnt].pa_long_double = 0.0; + break; + } - /* Now walk through all format specifiers and process them. */ - for (; (size_t) nspecs_done < nspecs; ++nspecs_done) { + /* Now walk through all format specifiers and process them. */ + for (; (size_t) nspecs_done < nspecs; ++nspecs_done) + { #undef REF #if defined HAVE_SUBTRACT_LOCAL_LABELS && defined SHARED -# define REF(Name) &&do2_##Name - &&do_form_unknown +# define REF(Name) &&do2_##Name - &&do_form_unknown #else -# define REF(Name) &&do2_##Name +# define REF(Name) &&do2_##Name #endif #undef LABEL #define LABEL(Name) do2_##Name - STEP4_TABLE; + STEP4_TABLE; - int is_negative; - union { - unsigned long long int longlong; - unsigned long int word; - } number; - int base; - union printf_arg the_arg; - CHAR_T *string; /* Pointer to argument string. */ + int is_negative; + union + { + unsigned long long int longlong; + unsigned long int word; + } number; + int base; + union printf_arg the_arg; + CHAR_T *string; /* Pointer to argument string. */ - /* Fill variables from values in struct. */ - int alt = specs[nspecs_done].info.alt; - int space = specs[nspecs_done].info.space; - int left = specs[nspecs_done].info.left; - int showsign = specs[nspecs_done].info.showsign; - int group = specs[nspecs_done].info.group; - int is_long_double = specs[nspecs_done].info.is_long_double; - int is_short = specs[nspecs_done].info.is_short; - int is_char = specs[nspecs_done].info.is_char; - int is_long = specs[nspecs_done].info.is_long; - int width = specs[nspecs_done].info.width; - int prec = specs[nspecs_done].info.prec; - int use_outdigits = specs[nspecs_done].info.i18n; - char pad = specs[nspecs_done].info.pad; - CHAR_T spec = specs[nspecs_done].info.spec; - CHAR_T *workstart = NULL; + /* Fill variables from values in struct. */ + int alt = specs[nspecs_done].info.alt; + int space = specs[nspecs_done].info.space; + int left = specs[nspecs_done].info.left; + int showsign = specs[nspecs_done].info.showsign; + int group = specs[nspecs_done].info.group; + int is_long_double = specs[nspecs_done].info.is_long_double; + int is_short = specs[nspecs_done].info.is_short; + int is_char = specs[nspecs_done].info.is_char; + int is_long = specs[nspecs_done].info.is_long; + int width = specs[nspecs_done].info.width; + int prec = specs[nspecs_done].info.prec; + int use_outdigits = specs[nspecs_done].info.i18n; + char pad = specs[nspecs_done].info.pad; + CHAR_T spec = specs[nspecs_done].info.spec; - /* Fill in last information. */ - if (specs[nspecs_done].width_arg != -1) { - /* Extract the field width from an argument. */ - specs[nspecs_done].info.width = args_value[specs[nspecs_done].width_arg].pa_int; + /* Fill in last information. */ + if (specs[nspecs_done].width_arg != -1) + { + /* Extract the field width from an argument. */ + specs[nspecs_done].info.width = + args_value[specs[nspecs_done].width_arg].pa_int; - if (specs[nspecs_done].info.width < 0) { - /* If the width value is negative left justification is - * selected and the value is taken as being positive. - */ - specs[nspecs_done].info.width *= -1; - left = specs[nspecs_done].info.left = 1; - } - width = specs[nspecs_done].info.width; - } + if (specs[nspecs_done].info.width < 0) + /* If the width value is negative left justification is + selected and the value is taken as being positive. */ + { + specs[nspecs_done].info.width *= -1; + left = specs[nspecs_done].info.left = 1; + } + width = specs[nspecs_done].info.width; + } - if (specs[nspecs_done].prec_arg != -1) { - /* Extract the precision from an argument. */ - specs[nspecs_done].info.prec = args_value[specs[nspecs_done].prec_arg].pa_int; + if (specs[nspecs_done].prec_arg != -1) + { + /* Extract the precision from an argument. */ + specs[nspecs_done].info.prec = + args_value[specs[nspecs_done].prec_arg].pa_int; - if (specs[nspecs_done].info.prec < 0) { - /* If the precision is negative the precision is omitted. */ - specs[nspecs_done].info.prec = -1; - } + if (specs[nspecs_done].info.prec < 0) + /* If the precision is negative the precision is + omitted. */ + specs[nspecs_done].info.prec = -1; - prec = specs[nspecs_done].info.prec; - } + prec = specs[nspecs_done].info.prec; + } - /* Maybe the buffer is too small. */ - if (MAX (prec, width) + 32 > (int)(sizeof(work_buffer) / sizeof(CHAR_T))) { - if (__libc_use_alloca((MAX(prec, width) + 32) * sizeof (CHAR_T))) - workend = ((CHAR_T *)alloca((MAX (prec, width) + 32) * sizeof (CHAR_T)) + (MAX (prec, width) + 32)); - else { - workstart = (CHAR_T *)malloc((MAX (prec, width) + 32) * sizeof (CHAR_T)); - if (workstart == NULL) { - done = -1; - goto all_done; - } - workend = workstart + (MAX(prec, width) + 32); - } - } + /* Maybe the buffer is too small. */ + if (MAX (prec, width) + 32 > (int) (sizeof (work_buffer) / sizeof (CHAR_T))) + workend = ((CHAR_T *) alloca ((MAX (prec, width) + 32) + * sizeof (CHAR_T)) + + (MAX (prec, width) + 32)); - /* Process format specifiers. */ - while (1) { - JUMP(spec, step4_jumps); + /* Process format specifiers. */ + while (1) + { + JUMP (spec, step4_jumps); - process_arg((&specs[nspecs_done])); - process_string_arg((&specs[nspecs_done])); + process_arg ((&specs[nspecs_done])); + process_string_arg ((&specs[nspecs_done])); - LABEL(form_unknown): - { - extern printf_function **__printf_function_table; - int function_done; - printf_function *function; - unsigned int i; - const void **ptr; + LABEL (form_unknown): + { + extern printf_function **__printf_function_table; + int function_done; + printf_function *function; + unsigned int i; + const void **ptr; - function = (__printf_function_table == NULL ? NULL : - __printf_function_table[specs[nspecs_done].info.spec]); + function = + (__printf_function_table == NULL ? NULL : + __printf_function_table[specs[nspecs_done].info.spec]); - if (function == NULL) - function = &printf_unknown; + if (function == NULL) + function = &printf_unknown; - ptr = alloca (specs[nspecs_done].ndata_args * sizeof (const void *)); + ptr = alloca (specs[nspecs_done].ndata_args + * sizeof (const void *)); - /* Fill in an array of pointers to the argument values. */ - for (i = 0; i < specs[nspecs_done].ndata_args; ++i) - ptr[i] = &args_value[specs[nspecs_done].data_arg + i]; + /* Fill in an array of pointers to the argument values. */ + for (i = 0; i < specs[nspecs_done].ndata_args; ++i) + ptr[i] = &args_value[specs[nspecs_done].data_arg + i]; - /* Call the function. */ - function_done = (*function)(s, &specs[nspecs_done].info, ptr); + /* Call the function. */ + function_done = (*function) (s, &specs[nspecs_done].info, ptr); - /* If an error occurred we don't have information about # of chars. */ - if (function_done < 0) { - done = -1; - goto all_done; - } - - done += function_done; - } - break; - } - - if (__builtin_expect(workstart != NULL, 0)) - free(workstart); - workstart = NULL; - - /* Write the following constant string. */ - outstring(specs[nspecs_done].end_of_fmt, - specs[nspecs_done].next_fmt - specs[nspecs_done].end_of_fmt); + /* If an error occurred we don't have information about # + of chars. */ + if (function_done < 0) + { + done = -1; + goto all_done; } - } + + done += function_done; + } + break; + } + + /* Write the following constant string. */ + outstring (specs[nspecs_done].end_of_fmt, + specs[nspecs_done].next_fmt + - specs[nspecs_done].end_of_fmt); + } + } all_done: - if (__builtin_expect(workstart != NULL, 0)) - free(workstart); + /* Unlock the stream. */ +#ifdef USE_IN_LIBIO + _IO_funlockfile (s); +#else + __funlockfile (s); +#endif + __libc_cleanup_region_end (0); - /* Unlock the stream. */ - _IO_funlockfile(s); - _IO_cleanup_region_end(0); - - return done; + return done; } - - -/** Handle an unknown format specifier. This prints out a - * canonicalized representation of the format spec itself. - */ - + +/* Handle an unknown format specifier. This prints out a canonicalized + representation of the format spec itself. */ static int -printf_unknown(FILE *s, const struct printf_info *info, const void *const *args) +printf_unknown (FILE *s, const struct printf_info *info, + const void *const *args) + { - int done = 0; - CHAR_T work_buffer[MAX(info->width, info->spec) + 32]; - CHAR_T *const workend = &work_buffer[sizeof(work_buffer) / sizeof(CHAR_T)]; - register CHAR_T *w; + int done = 0; + CHAR_T work_buffer[MAX (info->width, info->spec) + 32]; + CHAR_T *const workend + = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)]; + register CHAR_T *w; - outchar(L_('%')); + outchar (L_('%')); - if (info->alt) - outchar(L_('#')); - if (info->group) - outchar(L_('\'')); - if (info->showsign) - outchar(L_('+')); - else if (info->space) - outchar(L_(' ')); - if (info->left) - outchar(L_('-')); - if (info->pad == L_('0')) - outchar(L_('0')); - if (info->i18n) - outchar(L_('I')); + if (info->alt) + outchar (L_('#')); + if (info->group) + outchar (L_('\'')); + if (info->showsign) + outchar (L_('+')); + else if (info->space) + outchar (L_(' ')); + if (info->left) + outchar (L_('-')); + if (info->pad == L_('0')) + outchar (L_('0')); + if (info->i18n) + outchar (L_('I')); - if (info->width != 0) { - w = _itoa_word(info->width, workend, 10, 0); - while (w < workend) - outchar(*w++); - } + if (info->width != 0) + { + w = _itoa_word (info->width, workend, 10, 0); + while (w < workend) + outchar (*w++); + } - if (info->prec != -1) { - outchar(L_('.')); - w = _itoa_word(info->prec, workend, 10, 0); - while (w < workend) - outchar (*w++); - } + if (info->prec != -1) + { + outchar (L_('.')); + w = _itoa_word (info->prec, workend, 10, 0); + while (w < workend) + outchar (*w++); + } - if (info->spec != L_('\0')) - outchar(info->spec); + if (info->spec != L_('\0')) + outchar (info->spec); -all_done: - return done; + all_done: + return done; } - - -/** Group the digits according to the grouping rules of the current locale. - * The interpretation of GROUPING is as in `struct lconv' from . - */ - + +/* Group the digits according to the grouping rules of the current locale. + The interpretation of GROUPING is as in `struct lconv' from . */ static CHAR_T * internal_function group_number (CHAR_T *w, CHAR_T *rear_ptr, const char *grouping, #ifdef COMPILE_WPRINTF - wchar_t thousands_sep + wchar_t thousands_sep #else - const char *thousands_sep + const char *thousands_sep #endif - ) + ) { - int len; - CHAR_T *src, *s; + int len; + CHAR_T *src, *s; #ifndef COMPILE_WPRINTF - int tlen = strlen(thousands_sep); + int tlen = strlen (thousands_sep); #endif - /* We treat all negative values like CHAR_MAX. */ + /* We treat all negative values like CHAR_MAX. */ - if (*grouping == CHAR_MAX || *grouping <= 0) - /* No grouping should be done. */ - return w; + if (*grouping == CHAR_MAX || *grouping <= 0) + /* No grouping should be done. */ + return w; - len = *grouping; + len = *grouping; - /* Copy existing string so that nothing gets overwritten. */ - src = (CHAR_T *)alloca((rear_ptr - w) * sizeof(CHAR_T)); - s = (CHAR_T *)__mempcpy(src, w, (rear_ptr - w) * sizeof(CHAR_T)); - w = rear_ptr; + /* Copy existing string so that nothing gets overwritten. */ + src = (CHAR_T *) alloca ((rear_ptr - w) * sizeof (CHAR_T)); + s = (CHAR_T *) __mempcpy (src, w, + (rear_ptr - w) * sizeof (CHAR_T)); + w = rear_ptr; - /* Process all characters in the string. */ - while (s > src) { - *--w = *--s; + /* Process all characters in the string. */ + while (s > src) + { + *--w = *--s; - if (--len == 0 && s > src) { - /* A new group begins. */ + if (--len == 0 && s > src) + { + /* A new group begins. */ #ifdef COMPILE_WPRINTF - *--w = thousands_sep; + *--w = thousands_sep; #else - int cnt = tlen; - do { - *--w = thousands_sep[--cnt]; - } while (cnt > 0); + int cnt = tlen; + do + *--w = thousands_sep[--cnt]; + while (cnt > 0); #endif - len = *grouping++; - if (*grouping == '\0') { - /* The previous grouping repeats ad infinitum. */ - --grouping; - } else if (*grouping == CHAR_MAX + len = *grouping++; + if (*grouping == '\0') + /* The previous grouping repeats ad infinitum. */ + --grouping; + else if (*grouping == CHAR_MAX #if CHAR_MIN < 0 - || *grouping < 0 + || *grouping < 0 #endif - ) { - /* No further grouping to be done. Copy the rest of the number. */ - do { - *--w = *--s; - } while (s > src); - break; - } - } + ) + { + /* No further grouping to be done. + Copy the rest of the number. */ + do + *--w = *--s; + while (s > src); + break; + } } - return w; + } + return w; } - -#if 0 - + +#ifdef USE_IN_LIBIO /* Helper "class" for `fprintf to unbuffered': creates a temporary buffer. */ -struct helper_file { - struct _IO_FILE_plus _f; - _IO_FILE *_put_stream; -#ifdef _IO_MTSAFE_IO - _IO_lock_t lock; +struct helper_file + { + struct _IO_FILE_plus _f; +#ifdef COMPILE_WPRINTF + struct _IO_wide_data _wide_data; #endif -}; - + _IO_FILE *_put_stream; +#ifdef _IO_MTSAFE_IO + _IO_lock_t lock; +#endif + }; static int -_IO_helper_overflow(_IO_FILE *s, int c) +_IO_helper_overflow (_IO_FILE *s, int c) { - _IO_FILE *target = ((struct helper_file*) s)->_put_stream; - int used = s->_IO_write_ptr - s->_IO_write_base; - if (used) { - _IO_size_t written = _IO_sputn(target, s->_IO_write_base, used); - s->_IO_write_ptr -= written; - } - return PUTC(c, s); + _IO_FILE *target = ((struct helper_file*) s)->_put_stream; +#ifdef COMPILE_WPRINTF + int used = s->_wide_data->_IO_write_ptr - s->_wide_data->_IO_write_base; + if (used) + { + _IO_size_t written = _IO_sputn (target, s->_wide_data->_IO_write_base, + used); + s->_wide_data->_IO_write_ptr -= written; + } +#else + int used = s->_IO_write_ptr - s->_IO_write_base; + if (used) + { + _IO_size_t written = _IO_sputn (target, s->_IO_write_base, used); + s->_IO_write_ptr -= written; + } +#endif + return PUTC (c, s); } - +#ifdef COMPILE_WPRINTF +static const struct _IO_jump_t _IO_helper_jumps = +{ + JUMP_INIT_DUMMY, + JUMP_INIT (finish, _IO_wdefault_finish), + JUMP_INIT (overflow, _IO_helper_overflow), + JUMP_INIT (underflow, _IO_default_underflow), + JUMP_INIT (uflow, _IO_default_uflow), + JUMP_INIT (pbackfail, (_IO_pbackfail_t) _IO_wdefault_pbackfail), + JUMP_INIT (xsputn, _IO_wdefault_xsputn), + JUMP_INIT (xsgetn, _IO_wdefault_xsgetn), + JUMP_INIT (seekoff, _IO_default_seekoff), + JUMP_INIT (seekpos, _IO_default_seekpos), + JUMP_INIT (setbuf,(_IO_setbuf_t) _IO_wdefault_setbuf), + JUMP_INIT (sync, _IO_default_sync), + JUMP_INIT (doallocate, _IO_wdefault_doallocate), + JUMP_INIT (read, _IO_default_read), + JUMP_INIT (write, _IO_default_write), + JUMP_INIT (seek, _IO_default_seek), + JUMP_INIT (close, _IO_default_close), + JUMP_INIT (stat, _IO_default_stat) +}; +#else static const struct _IO_jump_t _IO_helper_jumps = { JUMP_INIT_DUMMY, @@ -1683,58 +2046,157 @@ static const struct _IO_jump_t _IO_helper_jumps = JUMP_INIT (close, _IO_default_close), JUMP_INIT (stat, _IO_default_stat) }; - +#endif static int internal_function -buffered_vfprintf(register _IO_FILE *s, const CHAR_T *format, _IO_va_list args) +buffered_vfprintf (register _IO_FILE *s, const CHAR_T *format, + _IO_va_list args) { - char buf[_IO_BUFSIZ]; + CHAR_T buf[_IO_BUFSIZ]; struct helper_file helper; - register _IO_FILE *hp = (_IO_FILE *) &helper; + register _IO_FILE *hp = (_IO_FILE *) &helper._f; int result, to_flush; - /* Initialize helper. */ + /* Orient the stream. */ +#ifdef ORIENT + ORIENT; +#endif + + /* Initialize helper. */ helper._put_stream = s; - hp->_IO_write_base = buf; - hp->_IO_write_ptr = buf; - hp->_IO_write_end = buf + sizeof buf; - hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS; +#ifdef COMPILE_WPRINTF + hp->_wide_data = &helper._wide_data; + _IO_wsetp (hp, buf, buf + sizeof buf / sizeof (CHAR_T)); + hp->_mode = 1; +#else + _IO_setp (hp, buf, buf + sizeof buf); + hp->_mode = -1; +#endif + hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS|_IO_USER_LOCK; #if _IO_JUMPS_OFFSET hp->_vtable_offset = 0; #endif #ifdef _IO_MTSAFE_IO - hp->_lock = &helper.lock; - __libc_lock_init (*(_IO_lock_t *)hp->_lock); + hp->_lock = NULL; #endif -// ToDo: -// _IO_JUMPS (hp) = (struct _IO_jump_t *) &_IO_helper_jumps; + _IO_JUMPS (&helper._f) = (struct _IO_jump_t *) &_IO_helper_jumps; /* Now print to helper instead. */ - result = _IO_vfprintf (hp, format, args); + result = vfprintf (hp, format, args); /* Lock stream. */ -// ToDo: - //__libc_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s); + __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, s); _IO_flockfile (s); /* Now flush anything from the helper to the S. */ +#ifdef COMPILE_WPRINTF + if ((to_flush = (hp->_wide_data->_IO_write_ptr + - hp->_wide_data->_IO_write_base)) > 0) + { + if ((int) _IO_sputn (s, hp->_wide_data->_IO_write_base, to_flush) + != to_flush) + result = -1; + } +#else if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0) { if ((int) _IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush) result = -1; } +#endif /* Unlock the stream. */ _IO_funlockfile (s); -// __libc_cleanup_region_end (0); + __libc_cleanup_region_end (0); return result; } + +#else /* !USE_IN_LIBIO */ + +static int +internal_function +buffered_vfprintf (register FILE *s, const CHAR_T *format, va_list args) +{ + char buf[BUFSIZ]; + int result; + + /* Orient the stream. */ +#ifdef ORIENT + ORIENT; #endif -#ifdef COMPILE_WPRINTF -//weak_alias(_IO_vfwprintf, vfwprintf); + s->__bufp = s->__buffer = buf; + s->__bufsize = sizeof buf; + s->__put_limit = s->__buffer + s->__bufsize; + s->__get_limit = s->__buffer; + + /* Now use buffer to print. */ + result = vfprintf (s, format, args); + + if (fflush (s) == EOF) + result = -1; + s->__buffer = s->__bufp = s->__get_limit = s->__put_limit = NULL; + s->__bufsize = 0; + + return result; +} + +/* Pads string with given number of a specified character. + This code is taken from iopadn.c of the GNU I/O library. */ +#define PADSIZE 16 +static const CHAR_T blanks[PADSIZE] = +{ L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), + L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' '), L_(' ') }; +static const CHAR_T zeroes[PADSIZE] = +{ L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), + L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0'), L_('0') }; + +ssize_t +#ifndef COMPILE_WPRINTF +__printf_pad (FILE *s, char pad, size_t count) #else -weak_alias(_IO_vfprintf, vfprintf); +__wprintf_pad (FILE *s, wchar_t pad, size_t count) +#endif +{ + const CHAR_T *padptr; + register size_t i; + + padptr = pad == L_(' ') ? blanks : zeroes; + + for (i = count; i >= PADSIZE; i -= PADSIZE) + if (PUT (s, padptr, PADSIZE) != PADSIZE) + return -1; + if (i > 0) + if (PUT (s, padptr, i) != i) + return -1; + + return count; +} +#undef PADSIZE +#endif /* USE_IN_LIBIO */ + +#ifdef USE_IN_LIBIO +# undef vfprintf +# ifdef strong_alias +/* This is for glibc. */ +# ifdef COMPILE_WPRINTF +strong_alias (_IO_vfwprintf, __vfwprintf); +weak_alias (_IO_vfwprintf, vfwprintf); +# else +strong_alias (_IO_vfprintf, vfprintf); +# endif +# else +# if defined __ELF__ || defined __GNU_LIBRARY__ +# include +# ifdef weak_alias +# ifdef COMPILE_WPRINTF +weak_alias (_IO_vfwprintf, vfwprintf); +# else +weak_alias (_IO_vfprintf, vfprintf); +# endif +# endif +# endif +# endif #endif diff --git a/src/kernel/libroot/posix/glibc/stdio-common/vfscanf.c b/src/kernel/libroot/posix/glibc/stdio-common/vfscanf.c index 07eb1caf0a..b4b7d4dff1 100644 --- a/src/kernel/libroot/posix/glibc/stdio-common/vfscanf.c +++ b/src/kernel/libroot/posix/glibc/stdio-common/vfscanf.c @@ -1,4 +1,4 @@ -/* Copyright (C) 1991-2002, 2003 Free Software Foundation, Inc. +/* Copyright (C) 1991-1999, 2000, 2001 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -14,9 +14,7 @@ You should have received a copy of the GNU Lesser General Public License along with the GNU C Library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - 02111-1307 USA. -*/ - + 02111-1307 USA. */ #include #include @@ -27,7 +25,10 @@ #include #include #include +#include +#include #include +#include #ifdef __GNUC__ # define HAVE_LONGLONG @@ -74,14 +75,12 @@ # ifdef COMPILE_WSCANF # define ungetc(c, s) ((void) (c == WEOF \ || (--read_in, \ - INTUSE(_IO_sputbackwc) (s, c)))) + _IO_sputbackwc (s, c)))) # define ungetc_not_eof(c, s) ((void) (--read_in, \ - INTUSE(_IO_sputbackwc) (s, c))) -# define inchar() (c == WEOF ? ((errno = inchar_errno), WEOF) \ + _IO_sputbackwc (s, c))) +# define inchar() (c == WEOF ? WEOF \ : ((c = _IO_getwc_unlocked (s)), \ - (void) (c != WEOF \ - ? ++read_in \ - : (size_t) (inchar_errno = errno)), c)) + (void) (c != WEOF && ++read_in), c)) # define MEMCPY(d, s, n) __wmemcpy (d, s, n) # define ISSPACE(Ch) iswspace (Ch) @@ -106,14 +105,12 @@ # else # define ungetc(c, s) ((void) ((int) c == EOF \ || (--read_in, \ - INTUSE(_IO_sputbackc) (s, (unsigned char) c)))) + _IO_sputbackc (s, (unsigned char) c)))) # define ungetc_not_eof(c, s) ((void) (--read_in, \ - INTUSE(_IO_sputbackc) (s, (unsigned char) c))) -# define inchar() (c == EOF ? ((errno = inchar_errno), EOF) \ + _IO_sputbackc (s, (unsigned char) c))) +# define inchar() (c == EOF ? EOF \ : ((c = _IO_getc_unlocked (s)), \ - (void) (c != EOF \ - ? ++read_in \ - : (size_t) (inchar_errno = errno)), c)) + (void) (c != EOF && ++read_in), c)) # define MEMCPY(d, s, n) memcpy (d, s, n) # define ISSPACE(Ch) isspace (Ch) # define ISDIGIT(Ch) isdigit (Ch) @@ -275,8 +272,6 @@ __vfscanf (FILE *s, const char *format, va_list argptr) register int width; /* Maximum field width. */ register int flags; /* Modifiers for current format element. */ - /* Errno of last failed inchar call. */ - int inchar_errno = 0; /* Status for reading F-P nums. */ char got_dot, got_e, negative; /* If a [...] is a [^...]. */ @@ -299,7 +294,8 @@ __vfscanf (FILE *s, const char *format, va_list argptr) #else const char *thousands; #endif - + /* State for the conversions. */ + mbstate_t state; /* Integral holding variables. */ union { @@ -310,6 +306,7 @@ __vfscanf (FILE *s, const char *format, va_list argptr) } num; /* Character-buffer pointer. */ char *str = NULL; + wchar_t *wstr = NULL; char **strptr = NULL; ssize_t strsize = 0; /* We must not react on white spaces immediately because they can @@ -354,13 +351,13 @@ __vfscanf (FILE *s, const char *format, va_list argptr) #ifdef COMPILE_WSCANF decimal = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_DECIMAL_POINT_WC); #else - decimal = "."; //_NL_CURRENT (LC_NUMERIC, DECIMAL_POINT); + decimal = _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT); #endif /* Figure out the thousands separator character. */ #ifdef COMPILE_WSCANF thousands = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_THOUSANDS_SEP_WC); #else - thousands = ","; //_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP); + thousands = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP); if (*thousands == '\0') thousands = NULL; #endif @@ -369,6 +366,11 @@ __vfscanf (FILE *s, const char *format, va_list argptr) LOCK_STREAM (s); +#ifndef COMPILE_WSCANF + /* From now on we use `state' to convert the format string. */ + memset (&state, '\0', sizeof (state)); +#endif + /* Run through the format string. */ while (*f != '\0') { @@ -406,6 +408,30 @@ __vfscanf (FILE *s, const char *format, va_list argptr) # endif #endif +#ifndef COMPILE_WSCANF + if (!isascii ((unsigned char) *f)) + { + /* Non-ASCII, may be a multibyte. */ + int len = __mbrlen (f, strlen (f), &state); + if (len > 0) + { + do + { + c = inchar (); + if (c == EOF) + input_error (); + else if (c != (unsigned char) *f++) + { + ungetc_not_eof (c, s); + conv_error (); + } + } + while (--len > 0); + continue; + } + } +#endif + fc = *f++; if (fc != '%') { @@ -667,6 +693,26 @@ __vfscanf (FILE *s, const char *format, va_list argptr) if (width == -1) width = 1; +#ifdef COMPILE_WSCANF + /* We have to convert the wide character(s) into multibyte + characters and store the result. */ + memset (&state, '\0', sizeof (state)); + + do + { + size_t n; + + n = __wcrtomb (!(flags & SUPPRESS) ? str : NULL, c, &state); + if (n == (size_t) -1) + /* No valid wide character. */ + input_error (); + + /* Increment the output pointer. Even if we don't + write anything. */ + str += n; + } + while (--width > 0 && inchar () != EOF); +#else if (!(flags & SUPPRESS)) { do @@ -675,13 +721,85 @@ __vfscanf (FILE *s, const char *format, va_list argptr) } else while (--width > 0 && inchar () != EOF); +#endif if (!(flags & SUPPRESS)) ++done; break; } - // ToDo: check if we ever get here! + /* FALLTHROUGH */ + case L_('C'): + if (!(flags & SUPPRESS)) + { + wstr = ARG (wchar_t *); + if (wstr == NULL) + conv_error (); + } + + c = inchar (); + if (c == EOF) + input_error (); + +#ifdef COMPILE_WSCANF + /* Just store the incoming wide characters. */ + if (!(flags & SUPPRESS)) + { + do + *wstr++ = c; + while (--width > 0 && inchar () != EOF); + } + else + while (--width > 0 && inchar () != EOF); +#else + { + /* We have to convert the multibyte input sequence to wide + characters. */ + char buf[1]; + mbstate_t cstate; + + memset (&cstate, '\0', sizeof (cstate)); + + do + { + /* This is what we present the mbrtowc function first. */ + buf[0] = c; + + while (1) + { + size_t n; + + n = __mbrtowc (!(flags & SUPPRESS) ? wstr : NULL, + buf, 1, &cstate); + + if (n == (size_t) -2) + { + /* Possibly correct character, just not enough + input. */ + if (inchar () == EOF) + encode_error (); + + buf[0] = c; + continue; + } + + if (n != 1) + encode_error (); + + /* We have a match. */ + break; + } + + /* Advance the result pointer. */ + ++wstr; + } + while (--width > 0 && inchar () != EOF); + } +#endif + + if (!(flags & SUPPRESS)) + ++done; + break; case L_('s'): /* Read a string. */ @@ -712,6 +830,10 @@ __vfscanf (FILE *s, const char *format, va_list argptr) if (c == EOF) input_error (); +#ifdef COMPILE_WSCANF + memset (&state, '\0', sizeof (state)); +#endif + do { if (ISSPACE (c)) @@ -720,6 +842,61 @@ __vfscanf (FILE *s, const char *format, va_list argptr) break; } +#ifdef COMPILE_WSCANF + /* This is quite complicated. We have to convert the + wide characters into multibyte characters and then + store them. */ + { + size_t n; + + if (!(flags & SUPPRESS) && (flags & MALLOC) + && str + MB_CUR_MAX >= *strptr + strsize) + { + /* We have to enlarge the buffer if the `a' flag + was given. */ + size_t strleng = str - *strptr; + char *newstr; + + newstr = (char *) realloc (*strptr, strsize * 2); + if (newstr == NULL) + { + /* Can't allocate that much. Last-ditch + effort. */ + newstr = (char *) realloc (*strptr, + strleng + MB_CUR_MAX); + if (newstr == NULL) + { + /* We lose. Oh well. Terminate the + string and stop converting, + so at least we don't skip any input. */ + ((char *) (*strptr))[strleng] = '\0'; + ++done; + conv_error (); + } + else + { + *strptr = newstr; + str = newstr + strleng; + strsize = strleng + MB_CUR_MAX; + } + } + else + { + *strptr = newstr; + str = newstr + strleng; + strsize *= 2; + } + } + + n = __wcrtomb (!(flags & SUPPRESS) ? str : NULL, c, + &state); + if (n == (size_t) -1) + encode_error (); + + assert (n <= MB_CUR_MAX); + str += n; + } +#else /* This is easy. */ if (!(flags & SUPPRESS)) { @@ -758,11 +935,44 @@ __vfscanf (FILE *s, const char *format, va_list argptr) } } } +#endif } while ((width <= 0 || --width > 0) && inchar () != EOF); if (!(flags & SUPPRESS)) { +#ifdef COMPILE_WSCANF + /* We have to emit the code to get into the initial + state. */ + char buf[MB_LEN_MAX]; + size_t n = __wcrtomb (buf, L'\0', &state); + if (n > 0 && (flags & MALLOC) + && str + n >= *strptr + strsize) + { + /* Enlarge the buffer. */ + size_t strleng = str - *strptr; + char *newstr; + + newstr = (char *) realloc (*strptr, strleng + n + 1); + if (newstr == NULL) + { + /* We lose. Oh well. Terminate the string + and stop converting, so at least we don't + skip any input. */ + ((char *) (*strptr))[strleng] = '\0'; + ++done; + conv_error (); + } + else + { + *strptr = newstr; + str = newstr + strleng; + strsize = strleng + n + 1; + } + } + + str = __mempcpy (str, buf, n); +#endif *str++ = '\0'; if ((flags & MALLOC) && str - *strptr != strsize) @@ -777,8 +987,166 @@ __vfscanf (FILE *s, const char *format, va_list argptr) break; } /* FALLTHROUGH */ - // ToDo: check if we ever got here! - break; + + case L_('S'): + { +#ifndef COMPILE_WSCANF + mbstate_t cstate; +#endif + + /* Wide character string. */ + STRING_ARG (wstr, wchar_t); + + c = inchar (); + if (c == EOF) + input_error (); + +#ifndef COMPILE_WSCANF + memset (&cstate, '\0', sizeof (cstate)); +#endif + + do + { + if (ISSPACE (c)) + { + ungetc_not_eof (c, s); + break; + } + +#ifdef COMPILE_WSCANF + /* This is easy. */ + if (!(flags & SUPPRESS)) + { + *wstr++ = c; + if ((flags & MALLOC) + && wstr == (wchar_t *) *strptr + strsize) + { + /* Enlarge the buffer. */ + wstr = (wchar_t *) realloc (*strptr, + (2 * strsize) + * sizeof (wchar_t)); + if (wstr == NULL) + { + /* Can't allocate that much. Last-ditch + effort. */ + wstr = (wchar_t *) realloc (*strptr, + (strsize + 1) + * sizeof (wchar_t)); + if (wstr == NULL) + { + /* We lose. Oh well. Terminate the string + and stop converting, so at least we don't + skip any input. */ + ((wchar_t *) (*strptr))[strsize - 1] = L'\0'; + ++done; + conv_error (); + } + else + { + *strptr = (char *) wstr; + wstr += strsize; + ++strsize; + } + } + else + { + *strptr = (char *) wstr; + wstr += strsize; + strsize *= 2; + } + } + } +#else + { + char buf[1]; + + buf[0] = c; + + while (1) + { + size_t n; + + n = __mbrtowc (!(flags & SUPPRESS) ? wstr : NULL, + buf, 1, &cstate); + + if (n == (size_t) -2) + { + /* Possibly correct character, just not enough + input. */ + if (inchar () == EOF) + encode_error (); + + buf[0] = c; + continue; + } + + if (n != 1) + encode_error (); + + /* We have a match. */ + ++wstr; + break; + } + + if (!(flags & SUPPRESS) && (flags & MALLOC) + && wstr == (wchar_t *) *strptr + strsize) + { + /* Enlarge the buffer. */ + wstr = (wchar_t *) realloc (*strptr, + (2 * strsize + * sizeof (wchar_t))); + if (wstr == NULL) + { + /* Can't allocate that much. Last-ditch effort. */ + wstr = (wchar_t *) realloc (*strptr, + ((strsize + 1) + * sizeof (wchar_t))); + if (wstr == NULL) + { + /* We lose. Oh well. Terminate the + string and stop converting, so at + least we don't skip any input. */ + ((wchar_t *) (*strptr))[strsize - 1] = L'\0'; + ++done; + conv_error (); + } + else + { + *strptr = (char *) wstr; + wstr += strsize; + ++strsize; + } + } + else + { + *strptr = (char *) wstr; + wstr += strsize; + strsize *= 2; + } + } + } +#endif + } + while ((width <= 0 || --width > 0) && inchar () != EOF); + + if (!(flags & SUPPRESS)) + { + *wstr++ = L'\0'; + + if ((flags & MALLOC) && wstr - (wchar_t *) *strptr != strsize) + { + wchar_t *cp = (wchar_t *) realloc (*strptr, + ((wstr + - (wchar_t *) *strptr) + * sizeof(wchar_t))); + if (cp != NULL) + *strptr = (char *) cp; + } + + ++done; + } + } + break; case L_('x'): /* Hexadecimal integer. */ case L_('X'): /* Ditto. */ @@ -846,7 +1214,6 @@ __vfscanf (FILE *s, const char *format, va_list argptr) if (base == 0) base = 10; -#if 0 if (base == 10 && (flags & I18N) != 0) { int from_level; @@ -1060,7 +1427,6 @@ __vfscanf (FILE *s, const char *format, va_list argptr) } } else -#endif /* Read the number into workspace. */ while (c != EOF && width != 0) { @@ -1500,19 +1866,19 @@ __vfscanf (FILE *s, const char *format, va_list argptr) ADDW (L_('\0')); if (flags & LONGDBL) { - long double d = 42.0; //__strtold_internal (wp, &tw, flags & GROUP); + long double d = __strtold_internal (wp, &tw, flags & GROUP); if (!(flags & SUPPRESS) && tw != wp) *ARG (long double *) = negative ? -d : d; } else if (flags & LONG) { - double d = 42.0; //__strtod_internal (wp, &tw, flags & GROUP); + double d = __strtod_internal (wp, &tw, flags & GROUP); if (!(flags & SUPPRESS) && tw != wp) *ARG (double *) = negative ? -d : d; } else { - float d = 42.0f; //__strtof_internal (wp, &tw, flags & GROUP); + float d = __strtof_internal (wp, &tw, flags & GROUP); if (!(flags & SUPPRESS) && tw != wp) *ARG (float *) = negative ? -d : d; } @@ -1525,6 +1891,9 @@ __vfscanf (FILE *s, const char *format, va_list argptr) break; case L_('['): /* Character class. */ + if (flags & LONG) + STRING_ARG (wstr, wchar_t); + else STRING_ARG (str, char); if (*f == L_('^')) @@ -1541,6 +1910,23 @@ __vfscanf (FILE *s, const char *format, va_list argptr) a very high value to make the algorithm easier. */ width = INT_MAX; +#ifdef COMPILE_WSCANF + /* Find the beginning and the end of the scanlist. We are not + creating a lookup table since it would have to be too large. + Instead we search each time through the string. This is not + a constant lookup time but who uses this feature deserves to + be punished. */ + tw = (wchar_t *) f; /* Marks the beginning. */ + + if (*f == L']') + ++f; + + while ((fc = *f++) != L'\0' && fc != L']'); + + if (fc == L'\0') + conv_error (); + wp = (wchar_t *) f - 1; +#else /* Fill WP with byte flags indexed by character. We will use this flag map for matching input characters. */ if (wpmax < UCHAR_MAX + 1) @@ -1575,19 +1961,335 @@ __vfscanf (FILE *s, const char *format, va_list argptr) if (fc == '\0') conv_error(); +#endif -#if 0 if (flags & LONG) { + size_t now = read_in; +#ifdef COMPILE_WSCANF + if (inchar () == WEOF) + input_error (); + + do + { + wchar_t *runp; + + /* Test whether it's in the scanlist. */ + runp = tw; + while (runp < wp) + { + if (runp[0] == L'-' && runp[1] != '\0' && runp + 1 != wp + && runp != tw + && (unsigned int) runp[-1] <= (unsigned int) runp[1]) + { + /* Match against all characters in between the + first and last character of the sequence. */ + wchar_t wc; + + for (wc = runp[-1] + 1; wc <= runp[1]; ++wc) + if (wc == c) + break; + + if (wc <= runp[1] && !not_in) + break; + if (wc <= runp[1] && not_in) + { + /* The current character is not in the + scanset. */ + ungetwc (c, s); + goto out; + } + + runp += 2; + } + else + { + if (*runp == c && !not_in) + break; + if (*runp == c && not_in) + { + ungetwc (c, s); + goto out; + } + + ++runp; + } + } + + if (runp == wp && !not_in) + { + ungetwc (c, s); + goto out; + } + + if (!(flags & SUPPRESS)) + { + *wstr++ = c; + + if ((flags & MALLOC) + && wstr == (wchar_t *) *strptr + strsize) + { + /* Enlarge the buffer. */ + wstr = (wchar_t *) realloc (*strptr, + (2 * strsize) + * sizeof (wchar_t)); + if (wstr == NULL) + { + /* Can't allocate that much. Last-ditch + effort. */ + wstr = (wchar_t *) + realloc (*strptr, (strsize + 1) + * sizeof (wchar_t)); + if (wstr == NULL) + { + /* We lose. Oh well. Terminate the string + and stop converting, so at least we don't + skip any input. */ + ((wchar_t *) (*strptr))[strsize - 1] = L'\0'; + ++done; + conv_error (); + } + else + { + *strptr = (char *) wstr; + wstr += strsize; + ++strsize; + } + } + else + { + *strptr = (char *) wstr; + wstr += strsize; + strsize *= 2; + } + } + } + } + while (--width > 0 && inchar () != WEOF); + out: +#else + char buf[MB_LEN_MAX]; + size_t cnt = 0; + mbstate_t cstate; + + if (inchar () == EOF) + input_error (); + + memset (&cstate, '\0', sizeof (cstate)); + + do + { + if (wp[c] == not_in) + { + ungetc_not_eof (c, s); + break; + } + + /* This is easy. */ + if (!(flags & SUPPRESS)) + { + size_t n; + + /* Convert it into a wide character. */ + buf[0] = c; + n = __mbrtowc (wstr, buf, 1, &cstate); + + if (n == (size_t) -2) + { + /* Possibly correct character, just not enough + input. */ + ++cnt; + assert (cnt < MB_CUR_MAX); + continue; + } + + ++wstr; + if ((flags & MALLOC) + && wstr == (wchar_t *) *strptr + strsize) + { + /* Enlarge the buffer. */ + wstr = (wchar_t *) realloc (*strptr, + (2 * strsize + * sizeof (wchar_t))); + if (wstr == NULL) + { + /* Can't allocate that much. Last-ditch + effort. */ + wstr = (wchar_t *) + realloc (*strptr, ((strsize + 1) + * sizeof (wchar_t))); + if (wstr == NULL) + { + /* We lose. Oh well. Terminate the + string and stop converting, + so at least we don't skip any input. */ + ((wchar_t *) (*strptr))[strsize - 1] = L'\0'; + ++done; + conv_error (); + } + else + { + *strptr = (char *) wstr; + wstr += strsize; + ++strsize; + } + } + else + { + *strptr = (char *) wstr; + wstr += strsize; + strsize *= 2; + } + } + } + + if (--width <= 0) + break; + } + while (inchar () != EOF); + + if (cnt != 0) + /* We stopped in the middle of recognizing another + character. That's a problem. */ + encode_error (); +#endif + + if (now == read_in) + /* We haven't succesfully read any character. */ + conv_error (); + + if (!(flags & SUPPRESS)) + { + *wstr++ = L'\0'; + + if ((flags & MALLOC) + && wstr - (wchar_t *) *strptr != strsize) + { + wchar_t *cp = (wchar_t *) + realloc (*strptr, ((wstr - (wchar_t *) *strptr) + * sizeof(wchar_t))); + if (cp != NULL) + *strptr = (char *) cp; + } + + ++done; + } } else -#endif { size_t now = read_in; if (inchar () == EOF) input_error (); +#ifdef COMPILE_WSCANF + + memset (&state, '\0', sizeof (state)); + + do + { + wchar_t *runp; + size_t n; + + /* Test whether it's in the scanlist. */ + runp = tw; + while (runp < wp) + { + if (runp[0] == L'-' && runp[1] != '\0' && runp + 1 != wp + && runp != tw + && (unsigned int) runp[-1] <= (unsigned int) runp[1]) + { + /* Match against all characters in between the + first and last character of the sequence. */ + wchar_t wc; + + for (wc = runp[-1] + 1; wc <= runp[1]; ++wc) + if (wc == c) + break; + + if (wc <= runp[1] && !not_in) + break; + if (wc <= runp[1] && not_in) + { + /* The current character is not in the + scanset. */ + ungetwc (c, s); + goto out2; + } + + runp += 2; + } + else + { + if (*runp == c && !not_in) + break; + if (*runp == c && not_in) + { + ungetwc (c, s); + goto out2; + } + + ++runp; + } + } + + if (runp == wp && !not_in) + { + ungetwc (c, s); + goto out2; + } + + if (!(flags & SUPPRESS)) + { + if ((flags & MALLOC) + && str + MB_CUR_MAX >= *strptr + strsize) + { + /* Enlarge the buffer. */ + size_t strleng = str - *strptr; + char *newstr; + + newstr = (char *) realloc (*strptr, 2 * strsize); + if (newstr == NULL) + { + /* Can't allocate that much. Last-ditch + effort. */ + newstr = (char *) realloc (*strptr, + strleng + MB_CUR_MAX); + if (newstr == NULL) + { + /* We lose. Oh well. Terminate the string + and stop converting, so at least we don't + skip any input. */ + ((char *) (*strptr))[strleng] = '\0'; + ++done; + conv_error (); + } + else + { + *strptr = newstr; + str = newstr + strleng; + strsize = strleng + MB_CUR_MAX; + } + } + else + { + *strptr = newstr; + str = newstr + strleng; + strsize *= 2; + } + } + } + + n = __wcrtomb (!(flags & SUPPRESS) ? str : NULL, c, &state); + if (n == (size_t) -1) + encode_error (); + + assert (n <= MB_CUR_MAX); + str += n; + } + while (--width > 0 && inchar () != WEOF); + out2: +#else do { if (wp[c] == not_in) @@ -1636,6 +2338,7 @@ __vfscanf (FILE *s, const char *format, va_list argptr) } } while (--width > 0 && inchar () != EOF); +#endif if (now == read_in) /* We haven't succesfully read any character. */ @@ -1643,6 +2346,38 @@ __vfscanf (FILE *s, const char *format, va_list argptr) if (!(flags & SUPPRESS)) { +#ifdef COMPILE_WSCANF + /* We have to emit the code to get into the initial + state. */ + char buf[MB_LEN_MAX]; + size_t n = __wcrtomb (buf, L'\0', &state); + if (n > 0 && (flags & MALLOC) + && str + n >= *strptr + strsize) + { + /* Enlarge the buffer. */ + size_t strleng = str - *strptr; + char *newstr; + + newstr = (char *) realloc (*strptr, strleng + n + 1); + if (newstr == NULL) + { + /* We lose. Oh well. Terminate the string + and stop converting, so at least we don't + skip any input. */ + ((char *) (*strptr))[strleng] = '\0'; + ++done; + conv_error (); + } + else + { + *strptr = newstr; + str = newstr + strleng; + strsize = strleng + n + 1; + } + } + + str = __mempcpy (str, buf, n); +#endif *str++ = '\0'; if ((flags & MALLOC) && str - *strptr != strsize) @@ -1700,9 +2435,8 @@ __vfwscanf (FILE *s, const wchar_t *format, va_list argptr) int __vfscanf (FILE *s, const char *format, va_list argptr) { - return INTUSE(_IO_vfscanf) (s, format, argptr, NULL); + return _IO_vfscanf (s, format, argptr, NULL); } -libc_hidden_def (__vfscanf) # endif #endif @@ -1710,5 +2444,4 @@ libc_hidden_def (__vfscanf) weak_alias (__vfwscanf, vfwscanf) #else weak_alias (__vfscanf, vfscanf) -INTDEF(_IO_vfscanf) #endif