|
@@ -1,130 +1,178 @@
|
|
|
-
|
|
|
-
|
|
|
- * Modified by Manuel Novoa III Mar 13, 2001
|
|
|
- *
|
|
|
- * The vfscanf routine was completely rewritten to add features and remove
|
|
|
- * bugs. The function __strtold, based on my strtod code in stdlib, was
|
|
|
- * added to provide floating point support for the scanf functions.
|
|
|
- *
|
|
|
- * So far they pass the test cases from glibc-2.1.3, except in two instances.
|
|
|
- * In one case, the test appears to be broken. The other case is something
|
|
|
- * I need to research further. This version of scanf assumes it can only
|
|
|
- * peek one character ahead. Apparently, glibc looks further. The difference
|
|
|
- * can be seen when parsing a floating point value in the character
|
|
|
- * sequence "100ergs". glibc is able to back up before the 'e' and return
|
|
|
- * a value of 100, whereas this scanf reports a bad match with the stream
|
|
|
- * pointer at 'r'. A similar situation can also happen when parsing hex
|
|
|
- * values prefixed by 0x or 0X; a failure would occur for "0xg". In order to
|
|
|
- * fix this, I need to rework the "ungetc" machinery in stdio.c again.
|
|
|
- * I do have one reference though, that seems to imply scanf has a single
|
|
|
- * character of lookahead.
|
|
|
- *
|
|
|
- * May 20, 2001
|
|
|
- *
|
|
|
- * Quote from ANSI/ISO C99 standard:
|
|
|
- *
|
|
|
- * fscanf pushes back at most one input character onto the input stream.
|
|
|
- * Therefore, some sequences that are acceptable to strtod, strtol, etc.,
|
|
|
- * are unacceptable to fscanf.
|
|
|
- *
|
|
|
- * So uClibc's *scanf functions conform to the standard, and glibc's
|
|
|
- * implementation doesn't for the "100ergs" case mentioned above.
|
|
|
+
|
|
|
*
|
|
|
- * Sep 6, 2002
|
|
|
- * Patch from Tero_Lyytikäinen <tero@paravant.fi> to fix bug in matchchar case.
|
|
|
+ * This library is free software; you can redistribute it and/or
|
|
|
+ * modify it under the terms of the GNU Library General Public
|
|
|
+ * License as published by the Free Software Foundation; either
|
|
|
+ * version 2 of the License, or (at your option) any later version.
|
|
|
*
|
|
|
- * May 15, 2003
|
|
|
- * Hopefully fix handling of 0 bytes with %s, %c, and %[ specifiers.
|
|
|
+ * This 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
|
|
|
+ * Library General Public License for more details.
|
|
|
*
|
|
|
- * July 17, 2003
|
|
|
- * Bug fix from Peter Kjellerstedt <peter.kjellerstedt@axis.com>. vfscanf was
|
|
|
- * not setting the FILE bufread member to flag the end of the buffer.
|
|
|
- * Also, do not set bufgetc member if getc macro support is disabled.
|
|
|
+ * You should have received a copy of the GNU Library General Public
|
|
|
+ * License along with this library; if not, write to the Free
|
|
|
+ * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
*/
|
|
|
|
|
|
+
|
|
|
+ * New *scanf implementation with lots of bug fixes and *wscanf support.
|
|
|
+ * Also now optionally supports hexadecimal float notation, positional
|
|
|
+ * args, and glibc locale-specific digit grouping. Should now be
|
|
|
+ * standards compliant.
|
|
|
+ */
|
|
|
+
|
|
|
+
|
|
|
#define _ISOC99_SOURCE
|
|
|
#define _GNU_SOURCE
|
|
|
#define _STDIO_UTILITY
|
|
|
+#include <features.h>
|
|
|
#include <stdio.h>
|
|
|
#include <stdlib.h>
|
|
|
#include <unistd.h>
|
|
|
#include <ctype.h>
|
|
|
#include <string.h>
|
|
|
#include <stdarg.h>
|
|
|
+#include <stdint.h>
|
|
|
+#include <errno.h>
|
|
|
+#include <printf.h>
|
|
|
+
|
|
|
+#ifdef __UCLIBC_HAS_WCHAR__
|
|
|
+#include <bits/uClibc_uwchar.h>
|
|
|
+#include <wchar.h>
|
|
|
+#include <wctype.h>
|
|
|
+#endif
|
|
|
+
|
|
|
+#include <langinfo.h>
|
|
|
+#include <locale.h>
|
|
|
+
|
|
|
+#include <assert.h>
|
|
|
+#include <limits.h>
|
|
|
|
|
|
#ifdef __STDIO_THREADSAFE
|
|
|
#include <stdio_ext.h>
|
|
|
#include <pthread.h>
|
|
|
#endif
|
|
|
|
|
|
-#ifdef L_scanf
|
|
|
-#ifdef __STDC__
|
|
|
-int scanf(const char *fmt, ...)
|
|
|
+#ifdef __UCLIBC_HAS_FLOATS__
|
|
|
+#include <float.h>
|
|
|
+#include <bits/uClibc_fpmax.h>
|
|
|
+#endif
|
|
|
+
|
|
|
+#ifdef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
|
|
|
+#ifdef L_vfscanf
|
|
|
+
|
|
|
+#warning Forcing undef of __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__ until implemented!
|
|
|
+#endif
|
|
|
+#undef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
|
|
|
+#endif
|
|
|
+
|
|
|
+extern void _store_inttype(void *dest, int desttype, uintmax_t val);
|
|
|
+
|
|
|
+#ifdef LLONG_MAX
|
|
|
+
|
|
|
+extern unsigned long long
|
|
|
+_stdlib_strto_ll(register const char * __restrict str,
|
|
|
+ char ** __restrict endptr, int base, int sflag);
|
|
|
+#if (ULLONG_MAX == UINTMAX_MAX)
|
|
|
+#define STRTOUIM(s,e,b,sf) _stdlib_strto_ll(s,e,b,sf)
|
|
|
+#endif
|
|
|
+
|
|
|
+#else
|
|
|
+
|
|
|
+extern unsigned long
|
|
|
+_stdlib_strto_l(register const char * __restrict str,
|
|
|
+ char ** __restrict endptr, int base, int sflag);
|
|
|
+
|
|
|
+#if (ULONG_MAX == UINTMAX_MAX)
|
|
|
+#define STRTOUIM(s,e,b,sf) _stdlib_strto_l(s,e,b,sf)
|
|
|
+#endif
|
|
|
+
|
|
|
+#endif
|
|
|
+
|
|
|
+#ifndef STRTOUIM
|
|
|
+#error STRTOUIM conversion function is undefined!
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#if EOF >= CHAR_MIN
|
|
|
+#define __isdigit_char_or_EOF(C) __isdigit_char((C))
|
|
|
#else
|
|
|
-int scanf(fmt, va_alist)
|
|
|
-__const char *fmt;
|
|
|
-va_dcl
|
|
|
+#define __isdigit_char_or_EOF(C) __isdigit_int((C))
|
|
|
#endif
|
|
|
+
|
|
|
+
|
|
|
+#ifdef L_fscanf
|
|
|
+
|
|
|
+int fscanf(FILE * __restrict stream, const char * __restrict format, ...)
|
|
|
{
|
|
|
- va_list ptr;
|
|
|
+ va_list arg;
|
|
|
int rv;
|
|
|
|
|
|
- va_start(ptr, fmt);
|
|
|
- rv = vfscanf(stdin, fmt, ptr);
|
|
|
- va_end(ptr);
|
|
|
+ va_start(arg, format);
|
|
|
+ rv = vfscanf(stream, format, arg);
|
|
|
+ va_end(arg);
|
|
|
+
|
|
|
return rv;
|
|
|
}
|
|
|
-#endif
|
|
|
|
|
|
-#ifdef L_sscanf
|
|
|
-#if !defined(__STDIO_BUFFERS) && !defined(__STDIO_GLIBC_CUSTOM_STREAMS)
|
|
|
-#warning skipping sscanf since no buffering and no custom streams!
|
|
|
-#else
|
|
|
+#endif
|
|
|
+
|
|
|
+#ifdef L_scanf
|
|
|
|
|
|
-int sscanf(const char *sp, const char *fmt, ...)
|
|
|
+int scanf(const char * __restrict format, ...)
|
|
|
{
|
|
|
- va_list ptr;
|
|
|
+ va_list arg;
|
|
|
int rv;
|
|
|
|
|
|
- va_start(ptr, fmt);
|
|
|
- rv = vsscanf(sp, fmt, ptr);
|
|
|
- va_end(ptr);
|
|
|
+ va_start(arg, format);
|
|
|
+ rv = vfscanf(stdin, format, arg);
|
|
|
+ va_end(arg);
|
|
|
+
|
|
|
return rv;
|
|
|
}
|
|
|
|
|
|
#endif
|
|
|
-#endif
|
|
|
+
|
|
|
+#ifdef L_sscanf
|
|
|
|
|
|
-#ifdef L_fscanf
|
|
|
-#ifdef __STDC__
|
|
|
-int fscanf(FILE * fp, const char *fmt, ...)
|
|
|
-#else
|
|
|
-int fscanf(fp, fmt, va_alist)
|
|
|
-FILE *fp;
|
|
|
-__const char *fmt;
|
|
|
-va_dcl
|
|
|
-#endif
|
|
|
+#if defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS)
|
|
|
+
|
|
|
+int sscanf(const char * __restrict str, const char * __restrict format, ...)
|
|
|
{
|
|
|
- va_list ptr;
|
|
|
+ va_list arg;
|
|
|
int rv;
|
|
|
|
|
|
- va_start(ptr, fmt);
|
|
|
- rv = vfscanf(fp, fmt, ptr);
|
|
|
- va_end(ptr);
|
|
|
+ va_start(arg, format);
|
|
|
+ rv = vsscanf(str, format, arg);
|
|
|
+ va_end(arg);
|
|
|
+
|
|
|
return rv;
|
|
|
}
|
|
|
-#endif
|
|
|
|
|
|
+#else
|
|
|
+#warning Skipping sscanf since no buffering and no custom streams!
|
|
|
+#endif
|
|
|
+
|
|
|
+#endif
|
|
|
+
|
|
|
#ifdef L_vscanf
|
|
|
-int vscanf(fmt, ap)
|
|
|
-__const char *fmt;
|
|
|
-va_list ap;
|
|
|
+
|
|
|
+int vscanf(const char * __restrict format, va_list arg)
|
|
|
{
|
|
|
- return vfscanf(stdin, fmt, ap);
|
|
|
+ return vfscanf(stdin, format, arg);
|
|
|
}
|
|
|
-#endif
|
|
|
|
|
|
+#endif
|
|
|
+
|
|
|
#ifdef L_vsscanf
|
|
|
+
|
|
|
+#ifdef __UCLIBC_MJN3_ONLY__
|
|
|
+#warning WISHLIST: Implement vsscanf for non-buffered and no custom stream case.
|
|
|
+#endif
|
|
|
+
|
|
|
#ifdef __STDIO_BUFFERS
|
|
|
int vsscanf(__const char *sp, __const char *fmt, va_list ap)
|
|
|
{
|
|
@@ -165,598 +213,1866 @@ int vsscanf(__const char *sp, __const char *fmt, va_list ap)
|
|
|
return rv;
|
|
|
}
|
|
|
#else
|
|
|
-#warning skipping vsscanf since no buffering and no custom streams!
|
|
|
+#warning Skipping vsscanf since no buffering and no custom streams!
|
|
|
#endif
|
|
|
#endif
|
|
|
+
|
|
|
#endif
|
|
|
+
|
|
|
+#ifdef L_fwscanf
|
|
|
|
|
|
-#ifdef L_vfscanf
|
|
|
+int fwscanf(FILE * __restrict stream, const wchar_t * __restrict format, ...)
|
|
|
+{
|
|
|
+ va_list arg;
|
|
|
+ int rv;
|
|
|
|
|
|
-#include <assert.h>
|
|
|
-#include <ctype.h>
|
|
|
-#include <limits.h>
|
|
|
+ va_start(arg, format);
|
|
|
+ rv = vfwscanf(stream, format, arg);
|
|
|
+ va_end(arg);
|
|
|
+
|
|
|
+ return rv;
|
|
|
+}
|
|
|
+
|
|
|
+#endif
|
|
|
+
|
|
|
+#ifdef L_wscanf
|
|
|
|
|
|
-static int valid_digit(char c, char base)
|
|
|
+int wscanf(const wchar_t * __restrict format, ...)
|
|
|
{
|
|
|
- if (base == 16) {
|
|
|
- return isxdigit(c);
|
|
|
- } else {
|
|
|
- return (__isdigit(c) && (c < '0' + base));
|
|
|
- }
|
|
|
+ va_list arg;
|
|
|
+ int rv;
|
|
|
+
|
|
|
+ va_start(arg, format);
|
|
|
+ rv = vfwscanf(stdin, format, arg);
|
|
|
+ va_end(arg);
|
|
|
+
|
|
|
+ return rv;
|
|
|
}
|
|
|
|
|
|
-extern unsigned long
|
|
|
-_stdlib_strto_l(register const char * __restrict str,
|
|
|
- char ** __restrict endptr, int base, int sflag);
|
|
|
-#ifdef LLONG_MAX
|
|
|
-extern unsigned long long
|
|
|
-_stdlib_strto_ll(register const char * __restrict str,
|
|
|
- char ** __restrict endptr, int base, int sflag);
|
|
|
+#endif
|
|
|
+
|
|
|
+#ifdef L_swscanf
|
|
|
+
|
|
|
+#ifdef __STDIO_BUFFERS
|
|
|
+
|
|
|
+int swscanf(const wchar_t * __restrict str, const wchar_t * __restrict format,
|
|
|
+ ...)
|
|
|
+{
|
|
|
+ va_list arg;
|
|
|
+ int rv;
|
|
|
+
|
|
|
+ va_start(arg, format);
|
|
|
+ rv = vswscanf(str, format, arg);
|
|
|
+ va_end(arg);
|
|
|
+
|
|
|
+ return rv;
|
|
|
+}
|
|
|
+#else
|
|
|
+#warning Skipping swscanf since no buffering!
|
|
|
+#endif
|
|
|
+
|
|
|
+#endif
|
|
|
+
|
|
|
+#ifdef L_vwscanf
|
|
|
+
|
|
|
+int vwscanf(const wchar_t * __restrict format, va_list arg)
|
|
|
+{
|
|
|
+ return vfwscanf(stdin, format, arg);
|
|
|
+}
|
|
|
+
|
|
|
+#endif
|
|
|
+
|
|
|
+#ifdef L_vswscanf
|
|
|
+
|
|
|
+#ifdef __STDIO_BUFFERS
|
|
|
+
|
|
|
+int vswscanf(const wchar_t * __restrict str, const wchar_t * __restrict format,
|
|
|
+ va_list arg)
|
|
|
+{
|
|
|
+ FILE f;
|
|
|
+
|
|
|
+ f.filedes = -3;
|
|
|
+ f.modeflags = (__FLAG_WIDE|__FLAG_READONLY|__FLAG_READING);
|
|
|
+ f.bufpos = (char *) str;
|
|
|
+ f.bufend = (char *)(str + wcslen(str));
|
|
|
+ f.ungot_width[0] = 0;
|
|
|
+#ifdef __STDIO_THREADSAFE
|
|
|
+ f.user_locking = 0;
|
|
|
+ __stdio_init_mutex(&f.lock);
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+ return vfwscanf(&f, format, arg);
|
|
|
+}
|
|
|
+#else
|
|
|
+#warning Skipping vswscanf since no buffering!
|
|
|
+#endif
|
|
|
+
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#define SPEC_CHARS "npxXoudifFeEgGaACSncs["
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * must immediately precede CONV_c. */
|
|
|
+
|
|
|
+enum {
|
|
|
+ CONV_n = 0,
|
|
|
+ CONV_p,
|
|
|
+ CONV_x, CONV_X, CONV_o, CONV_u, CONV_d, CONV_i,
|
|
|
+ CONV_f, CONV_F, CONV_e, CONV_E, CONV_g, CONV_G, CONV_a, CONV_A,
|
|
|
+ CONV_C, CONV_S, CONV_LEFTBRACKET, CONV_c, CONV_s, CONV_leftbracket,
|
|
|
+ CONV_percent, CONV_whitespace
|
|
|
+};
|
|
|
+
|
|
|
+#ifdef __UCLIBC_HAS_FLOATS__
|
|
|
+#ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
|
|
|
+
|
|
|
+#define SPEC_BASE { 16, 16, 16, 8, 10, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
|
|
|
+#else
|
|
|
+
|
|
|
+#define SPEC_BASE { 16, 16, 16, 8, 10, 10, 0, 10, 10, 10, 10, 10, 10, 10, 10 }
|
|
|
+#endif
|
|
|
+#else
|
|
|
+
|
|
|
+#define SPEC_BASE { 16, 16, 16, 8, 10, 10, 0 }
|
|
|
+#endif
|
|
|
+
|
|
|
+#ifdef __UCLIBC_MJN3_ONLY__
|
|
|
+#ifdef L_vfscanf
|
|
|
+
|
|
|
+#warning CONSIDER: Add a '0' flag to eat 0 padding when grouping?
|
|
|
+#endif
|
|
|
+#endif
|
|
|
+
|
|
|
+#define SPEC_FLAGS "*'I"
|
|
|
+
|
|
|
+enum {
|
|
|
+ FLAG_SURPRESS = 0x10,
|
|
|
+ FLAG_THOUSANDS = 0x20,
|
|
|
+ FLAG_I18N = 0x40,
|
|
|
+ FLAG_MALLOC = 0x80,
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
+#define SPEC_RANGES { CONV_n, CONV_p, CONV_i, CONV_A, \
|
|
|
+ CONV_C, CONV_LEFTBRACKET, \
|
|
|
+ CONV_c, CONV_leftbracket }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#define SPEC_ALLOWED_FLAGS { \
|
|
|
+ (0x0f|FLAG_SURPRESS), \
|
|
|
+ ( 0|FLAG_SURPRESS), \
|
|
|
+ (0x0f|FLAG_SURPRESS|FLAG_THOUSANDS|FLAG_I18N), \
|
|
|
+ (0x0c|FLAG_SURPRESS|FLAG_THOUSANDS|FLAG_I18N), \
|
|
|
+ ( 0|FLAG_SURPRESS), \
|
|
|
+ ( 0|FLAG_SURPRESS|FLAG_MALLOC), \
|
|
|
+ (0x04|FLAG_SURPRESS), \
|
|
|
+ (0x04|FLAG_SURPRESS|FLAG_MALLOC), \
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * In order to ease translation to what arginfo and _print_info._flags expect,
|
|
|
+ * we map: 0:int 1:char 2:longlong 4:long 8:short
|
|
|
+ * and then _flags |= (((q << 7) + q) & 0x701) and argtype |= (_flags & 0x701)
|
|
|
+ */
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#ifdef PDS
|
|
|
+#error PDS already defined!
|
|
|
+#endif
|
|
|
+#ifdef SS
|
|
|
+#error SS already defined!
|
|
|
+#endif
|
|
|
+#ifdef IMS
|
|
|
+#error IMS already defined!
|
|
|
+#endif
|
|
|
+
|
|
|
+#if PTRDIFF_MAX == INT_MAX
|
|
|
+#define PDS 0
|
|
|
+#elif PTRDIFF_MAX == LONG_MAX
|
|
|
+#define PDS 4
|
|
|
+#elif defined(LLONG_MAX) && (PTRDIFF_MAX == LLONG_MAX)
|
|
|
+#define PDS 8
|
|
|
+#else
|
|
|
+#error fix QUAL_CHARS ptrdiff_t entry 't'!
|
|
|
+#endif
|
|
|
+
|
|
|
+#if SIZE_MAX == UINT_MAX
|
|
|
+#define SS 0
|
|
|
+#elif SIZE_MAX == ULONG_MAX
|
|
|
+#define SS 4
|
|
|
+#elif defined(LLONG_MAX) && (SIZE_MAX == ULLONG_MAX)
|
|
|
+#define SS 8
|
|
|
+#else
|
|
|
+#error fix QUAL_CHARS size_t entries 'z', 'Z'!
|
|
|
+#endif
|
|
|
+
|
|
|
+#if INTMAX_MAX == INT_MAX
|
|
|
+#define IMS 0
|
|
|
+#elif INTMAX_MAX == LONG_MAX
|
|
|
+#define IMS 4
|
|
|
+#elif defined(LLONG_MAX) && (INTMAX_MAX == LLONG_MAX)
|
|
|
+#define IMS 8
|
|
|
+#else
|
|
|
+#error fix QUAL_CHARS ptrdiff_t entry 't'!
|
|
|
+#endif
|
|
|
+
|
|
|
+#define QUAL_CHARS { \
|
|
|
+ \
|
|
|
+ 'h', 'l', 'L', 'j', 'z', 't', 'q', 0, \
|
|
|
+ 2, 4, 8, IMS, SS, PDS, 8, 0, \
|
|
|
+ 1, 8 }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#ifdef L_vfwscanf
|
|
|
+#if WINT_MIN > EOF
|
|
|
+#error Unfortunately, we currently need wint_t to be able to store EOF. Sorry.
|
|
|
+#endif
|
|
|
+#define W_EOF WEOF
|
|
|
+#define Wint wint_t
|
|
|
+#define Wchar wchar_t
|
|
|
+#define Wuchar __uwchar_t
|
|
|
+#define ISSPACE(C) iswspace((C))
|
|
|
+#define VFSCANF vfwscanf
|
|
|
+#define GETC(SC) (SC)->sc_getc((SC))
|
|
|
+#else
|
|
|
+typedef unsigned char __uchar_t;
|
|
|
+#define W_EOF EOF
|
|
|
+#define Wint int
|
|
|
+#define Wchar char
|
|
|
+#define Wuchar __uchar_t
|
|
|
+#define ISSPACE(C) isspace((C))
|
|
|
+#define VFSCANF vfscanf
|
|
|
+#ifdef __UCLIBC_HAS_WCHAR__
|
|
|
+#define GETC(SC) (SC)->sc_getc((SC))
|
|
|
+#else
|
|
|
+#define GETC(SC) getc_unlocked((SC)->fp)
|
|
|
+#endif
|
|
|
#endif
|
|
|
|
|
|
struct scan_cookie {
|
|
|
+ Wint cc;
|
|
|
+ Wint ungot_char;
|
|
|
FILE *fp;
|
|
|
int nread;
|
|
|
int width;
|
|
|
- int width_flag;
|
|
|
- int ungot_char;
|
|
|
- int ungot_flag;
|
|
|
- int app_ungot;
|
|
|
+
|
|
|
+#ifdef __UCLIBC_HAS_WCHAR__
|
|
|
+ wchar_t app_ungot;
|
|
|
+ unsigned char ungot_wchar_width;
|
|
|
+#else
|
|
|
+ unsigned char app_ungot;
|
|
|
+#endif
|
|
|
+
|
|
|
+ char ungot_flag;
|
|
|
+
|
|
|
+#ifdef __UCLIBC_HAS_WCHAR__
|
|
|
+ char ungot_wflag;
|
|
|
+ char mb_fail;
|
|
|
+ mbstate_t mbstate;
|
|
|
+ wint_t wc;
|
|
|
+ wint_t ungot_wchar;
|
|
|
+ int (*sc_getc)(struct scan_cookie *);
|
|
|
+#endif
|
|
|
+
|
|
|
+#ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
|
|
|
+ const char *grouping;
|
|
|
+ const unsigned char *thousands_sep;
|
|
|
+ int tslen;
|
|
|
+#ifdef __UCLIBC_HAS_WCHAR__
|
|
|
+ wchar_t thousands_sep_wc;
|
|
|
+#endif
|
|
|
+#endif
|
|
|
+
|
|
|
+#ifdef __UCLIBC_HAS_FLOATS__
|
|
|
+ const unsigned char *decpt;
|
|
|
+ int decpt_len;
|
|
|
+#ifdef __UCLIBC_HAS_WCHAR__
|
|
|
+ wchar_t decpt_wc;
|
|
|
+#endif
|
|
|
+ const unsigned char *fake_decpt;
|
|
|
+#endif
|
|
|
+
|
|
|
};
|
|
|
|
|
|
-static const char qual[] = "hl" "Lq";
|
|
|
-
|
|
|
-static const char qsz[] = { -1, 1, 2, 2 };
|
|
|
+typedef struct {
|
|
|
+#if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
|
|
|
+#if NL_ARGMAX > 10
|
|
|
+#warning NL_ARGMAX > 10, and space is allocated on the stack for positional args.
|
|
|
+#endif
|
|
|
+ void *pos_args[NL_ARGMAX];
|
|
|
+ int num_pos_args;
|
|
|
+ int cur_pos_arg;
|
|
|
+#endif
|
|
|
+ void *cur_ptr;
|
|
|
+ const unsigned char *fmt;
|
|
|
+ int cnt, dataargtype, conv_num, max_width;
|
|
|
+ unsigned char store, flags;
|
|
|
+} psfs_t;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+extern void __init_scan_cookie(register struct scan_cookie *sc,
|
|
|
+ register FILE *fp);
|
|
|
+extern int __scan_getc(register struct scan_cookie *sc);
|
|
|
+extern void __scan_ungetc(register struct scan_cookie *sc);
|
|
|
|
|
|
#ifdef __UCLIBC_HAS_FLOATS__
|
|
|
-static int __strtold(long double *ld, struct scan_cookie *sc);
|
|
|
-
|
|
|
-static const char spec[] = "%n[csoupxXidfeEgG";
|
|
|
-#else
|
|
|
-static const char spec[] = "%n[csoupxXid";
|
|
|
+extern int __scan_strtold(long double *ld, struct scan_cookie *sc);
|
|
|
+#endif
|
|
|
+
|
|
|
+extern int __psfs_parse_spec(psfs_t *psfs);
|
|
|
+extern int __psfs_do_numeric(psfs_t *psfs, struct scan_cookie *sc);
|
|
|
+
|
|
|
+
|
|
|
+#ifdef L___scan_cookie
|
|
|
+
|
|
|
+#ifdef __UCLIBC_MJN3_ONLY__
|
|
|
+#warning TODO: Remove dependence on decpt_str and fake_decpt in stub locale mode.
|
|
|
+#endif
|
|
|
+#ifndef __UCLIBC_HAS_LOCALE__
|
|
|
+static const char decpt_str[] = ".";
|
|
|
#endif
|
|
|
-
|
|
|
-static const char radix[] = { 8, 10, 16, 16, 16, 0, 10 };
|
|
|
|
|
|
-static void init_scan_cookie(register struct scan_cookie *sc,
|
|
|
- register FILE *fp)
|
|
|
+void __init_scan_cookie(register struct scan_cookie *sc,
|
|
|
+ register FILE *fp)
|
|
|
{
|
|
|
sc->fp = fp;
|
|
|
sc->nread = 0;
|
|
|
- sc->width_flag = 0;
|
|
|
sc->ungot_flag = 0;
|
|
|
sc->app_ungot = ((fp->modeflags & __MASK_UNGOT) ? fp->ungot[1] : 0);
|
|
|
-}
|
|
|
+#ifdef __UCLIBC_HAS_WCHAR__
|
|
|
+ sc->ungot_wflag = 0;
|
|
|
+ sc->mb_fail = 0;
|
|
|
+#endif
|
|
|
+
|
|
|
+#ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
|
|
|
+ if (*(sc->grouping = __UCLIBC_CURLOCALE_DATA.grouping)) {
|
|
|
+ sc->thousands_sep = __UCLIBC_CURLOCALE_DATA.thousands_sep;
|
|
|
+ sc->tslen = __UCLIBC_CURLOCALE_DATA.thousands_sep_len;
|
|
|
+#ifdef __UCLIBC_HAS_WCHAR__
|
|
|
+ sc->thousands_sep_wc = __UCLIBC_CURLOCALE_DATA.thousands_sep_wc;
|
|
|
+#endif
|
|
|
+ }
|
|
|
+#endif
|
|
|
+
|
|
|
+#ifdef __UCLIBC_HAS_FLOATS__
|
|
|
+#ifdef __UCLIBC_HAS_LOCALE__
|
|
|
+ sc->decpt = __UCLIBC_CURLOCALE_DATA.decimal_point;
|
|
|
+ sc->decpt_len = __UCLIBC_CURLOCALE_DATA.decimal_point_len;
|
|
|
+#else
|
|
|
+ sc->fake_decpt = sc->decpt = decpt_str;
|
|
|
+ sc->decpt_len = 1;
|
|
|
+#endif
|
|
|
+#ifdef __UCLIBC_HAS_WCHAR__
|
|
|
+#ifdef __UCLIBC_HAS_LOCALE__
|
|
|
+ sc->decpt_wc = __UCLIBC_CURLOCALE_DATA.decimal_point_wc;
|
|
|
+#else
|
|
|
+ sc->decpt_wc = '.';
|
|
|
+#endif
|
|
|
+#endif
|
|
|
+#endif
|
|
|
|
|
|
-
|
|
|
+}
|
|
|
|
|
|
-static int scan_getc_nw(register struct scan_cookie *sc)
|
|
|
+int __scan_getc(register struct scan_cookie *sc)
|
|
|
{
|
|
|
+ int c;
|
|
|
+
|
|
|
+#ifdef __UCLIBC_HAS_WCHAR__
|
|
|
+ assert(!sc->mb_fail);
|
|
|
+#endif
|
|
|
+
|
|
|
+ sc->cc = EOF;
|
|
|
+
|
|
|
+ if (--sc->width < 0) {
|
|
|
+ sc->ungot_flag |= 2;
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
if (sc->ungot_flag == 0) {
|
|
|
- sc->ungot_char = getc(sc->fp);
|
|
|
+ if ((c = GETC(sc)) == EOF) {
|
|
|
+ sc->ungot_flag |= 2;
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ sc->ungot_char = c;
|
|
|
} else {
|
|
|
+ assert(sc->ungot_flag == 1);
|
|
|
sc->ungot_flag = 0;
|
|
|
}
|
|
|
- if (sc->ungot_char > 0) {
|
|
|
- ++sc->nread;
|
|
|
- }
|
|
|
- sc->width_flag = 0;
|
|
|
- return sc->ungot_char;
|
|
|
+
|
|
|
+ ++sc->nread;
|
|
|
+ return sc->cc = sc->ungot_char;
|
|
|
}
|
|
|
|
|
|
-static int scan_getc(register struct scan_cookie *sc)
|
|
|
+void __scan_ungetc(register struct scan_cookie *sc)
|
|
|
{
|
|
|
- if (sc->ungot_flag == 0) {
|
|
|
- sc->ungot_char = getc(sc->fp);
|
|
|
- }
|
|
|
- sc->width_flag = 1;
|
|
|
- if (--sc->width < 0) {
|
|
|
+ ++sc->width;
|
|
|
+ if (sc->ungot_flag == 2) {
|
|
|
+ sc->ungot_flag = 0;
|
|
|
+ sc->cc = sc->ungot_char;
|
|
|
+ } else if (sc->ungot_flag == 0) {
|
|
|
sc->ungot_flag = 1;
|
|
|
- return -1;
|
|
|
- }
|
|
|
- sc->ungot_flag = 0;
|
|
|
- if (sc->ungot_char > 0) {
|
|
|
- ++sc->nread;
|
|
|
+ --sc->nread;
|
|
|
+ } else {
|
|
|
+ assert(0);
|
|
|
}
|
|
|
- return sc->ungot_char;
|
|
|
}
|
|
|
|
|
|
-static void scan_ungetc(register struct scan_cookie *sc)
|
|
|
+#endif
|
|
|
+
|
|
|
+#ifdef L___psfs_parse_spec
|
|
|
+
|
|
|
+#ifdef SPEC_FLAGS
|
|
|
+static const unsigned char spec_flags[] = SPEC_FLAGS;
|
|
|
+#endif
|
|
|
+static const unsigned char spec_chars[] = SPEC_CHARS;
|
|
|
+static const unsigned char qual_chars[] = QUAL_CHARS;
|
|
|
+static const unsigned char spec_ranges[] = SPEC_RANGES;
|
|
|
+static const unsigned short spec_allowed[] = SPEC_ALLOWED_FLAGS;
|
|
|
+
|
|
|
+int __psfs_parse_spec(register psfs_t *psfs)
|
|
|
{
|
|
|
- if (sc->ungot_flag != 0) {
|
|
|
- assert(sc->width < 0);
|
|
|
- return;
|
|
|
+ const unsigned char *p;
|
|
|
+ const unsigned char *fmt0 = psfs->fmt;
|
|
|
+ int i;
|
|
|
+#ifdef SPEC_FLAGS
|
|
|
+ int j;
|
|
|
+#endif
|
|
|
+#if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
|
|
|
+ unsigned char fail = 0;
|
|
|
+
|
|
|
+ i = 0;
|
|
|
+
|
|
|
+ if (!__isdigit_char(*psfs->fmt)) {
|
|
|
+ fail = 1;
|
|
|
+ goto DO_FLAGS;
|
|
|
}
|
|
|
- if (sc->width_flag) {
|
|
|
- ++sc->width;
|
|
|
+
|
|
|
+
|
|
|
+ do {
|
|
|
+ if (i <= ((INT_MAX - 9)/10)) {
|
|
|
+ i = (i * 10) + (*psfs->fmt++ - '0');
|
|
|
+ }
|
|
|
+ } while (__isdigit_char(*psfs->fmt));
|
|
|
+
|
|
|
+ if (*psfs->fmt != '$') {
|
|
|
+ if (psfs->num_pos_args >= 0) {
|
|
|
+ goto ERROR_EINVAL;
|
|
|
+ }
|
|
|
+ psfs->max_width = i;
|
|
|
+ psfs->num_pos_args = -2;
|
|
|
+ goto DO_QUALIFIER;
|
|
|
}
|
|
|
- sc->ungot_flag = 1;
|
|
|
- if (sc->ungot_char > 0) {
|
|
|
- --sc->nread;
|
|
|
+ ++psfs->fmt;
|
|
|
+#endif
|
|
|
+
|
|
|
+#if defined(SPEC_FLAGS) || (defined(NL_ARGMAX) && (NL_ARGMAX > 0))
|
|
|
+ DO_FLAGS:
|
|
|
+#endif
|
|
|
+#ifdef SPEC_FLAGS
|
|
|
+ p = spec_flags;
|
|
|
+ j = FLAG_SURPRESS;
|
|
|
+ do {
|
|
|
+ if (*p == *psfs->fmt) {
|
|
|
+ ++psfs->fmt;
|
|
|
+ psfs->flags |= j;
|
|
|
+ goto DO_FLAGS;
|
|
|
+ }
|
|
|
+ j += j;
|
|
|
+ } while (*++p);
|
|
|
+
|
|
|
+ if (psfs->flags & FLAG_SURPRESS) {
|
|
|
+ psfs->store = 0;
|
|
|
+ goto DO_WIDTH;
|
|
|
}
|
|
|
-}
|
|
|
+#else
|
|
|
+ if (*psfs->fmt == '*') {
|
|
|
+ ++psfs->fmt;
|
|
|
+ psfs->store = 0;
|
|
|
+ goto DO_WIDTH;
|
|
|
+ }
|
|
|
+#endif
|
|
|
|
|
|
-static void kill_scan_cookie(register struct scan_cookie *sc)
|
|
|
-{
|
|
|
- if (sc->ungot_flag) {
|
|
|
- ungetc(sc->ungot_char,sc->fp);
|
|
|
-
|
|
|
- if (sc->nread == 0) {
|
|
|
- sc->fp->ungot[1] = sc->app_ungot;
|
|
|
+
|
|
|
+#if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
|
|
|
+ if (fail) {
|
|
|
+
|
|
|
+ if (psfs->num_pos_args >= 0) {
|
|
|
+ goto ERROR_EINVAL;
|
|
|
}
|
|
|
+ psfs->num_pos_args = -2;
|
|
|
+ } else {
|
|
|
+ if ((psfs->num_pos_args == -2) || (((unsigned int)(--i)) >= NL_ARGMAX)) {
|
|
|
+
|
|
|
+ goto ERROR_EINVAL;
|
|
|
+ }
|
|
|
+ psfs->cur_pos_arg = i;
|
|
|
}
|
|
|
-}
|
|
|
+#endif
|
|
|
|
|
|
-int vfscanf(FILE *fp, const char *format, va_list ap)
|
|
|
-{
|
|
|
-#define STRTO_L_(s,e,b,sf) _stdlib_strto_ll(s,e,b,sf)
|
|
|
-#define MAX_DIGITS 64
|
|
|
-#define UV_TYPE unsigned long long
|
|
|
-#define V_TYPE long long
|
|
|
-#ifdef __UCLIBC_HAS_FLOATS__
|
|
|
- long double ld;
|
|
|
-#endif
|
|
|
- UV_TYPE uv;
|
|
|
- struct scan_cookie sc;
|
|
|
- register unsigned const char *fmt;
|
|
|
- const char *p;
|
|
|
- register unsigned char *b;
|
|
|
- void *vp;
|
|
|
- int cc, i, cnt;
|
|
|
- signed char lval;
|
|
|
- unsigned char store, usflag, base, invert, r0, r1;
|
|
|
- unsigned char buf[MAX_DIGITS+2];
|
|
|
- unsigned char scanset[UCHAR_MAX + 1];
|
|
|
+ DO_WIDTH:
|
|
|
+ for (i = 0 ; __isdigit_char(*psfs->fmt) ; ) {
|
|
|
+ if (i <= ((INT_MAX - 9)/10)) {
|
|
|
+ i = (i * 10) + (*psfs->fmt++ - '0');
|
|
|
+ psfs->max_width = i;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- __STDIO_THREADLOCK(fp);
|
|
|
+#if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
|
|
|
+ DO_QUALIFIER:
|
|
|
+#endif
|
|
|
+ p = qual_chars;
|
|
|
+ do {
|
|
|
+ if (*psfs->fmt == *p) {
|
|
|
+ ++psfs->fmt;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ } while (*++p);
|
|
|
+ if ((p - qual_chars < 2) && (*psfs->fmt == *p)) {
|
|
|
+ p += ((sizeof(qual_chars)-2) / 2);
|
|
|
+ ++psfs->fmt;
|
|
|
+ }
|
|
|
+ psfs->dataargtype = ((int)(p[(sizeof(qual_chars)-2) / 2])) << 8;
|
|
|
|
|
|
- init_scan_cookie(&sc,fp);
|
|
|
+#ifdef __UCLIBC_MJN3_ONLY__
|
|
|
+#warning CONSIDER: Should we validate that psfs->max_width > 0 in __psfs_parse_spec()? It would avoid whitespace consumption...
|
|
|
+#warning CONSIDER: Should INT_MAX be a valid width (%c/%C)? See __psfs_parse_spec().
|
|
|
+#endif
|
|
|
|
|
|
- fmt = (unsigned const char *) format;
|
|
|
- cnt = 0;
|
|
|
+ p = spec_chars;
|
|
|
+ do {
|
|
|
+ if (*psfs->fmt == *p) {
|
|
|
+ int p_m_spec_chars = p - spec_chars;
|
|
|
|
|
|
- while (*fmt) {
|
|
|
- store = 1;
|
|
|
- lval = 0;
|
|
|
- sc.width = INT_MAX;
|
|
|
- if (*fmt == '%') {
|
|
|
- ++fmt;
|
|
|
- if (*fmt == '*') {
|
|
|
- store = 0;
|
|
|
- ++fmt;
|
|
|
+#ifdef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
|
|
|
+#error implement gnu a flag
|
|
|
+ if ((*p == 'a')
|
|
|
+ && ((psfs->fmt[1] == '[') || ((psfs->fmt[1]|0x20) == 's'))
|
|
|
+ ) {
|
|
|
+ psfs->flags |= FLAG_MALLOC;
|
|
|
+ ++psfs->fmt;
|
|
|
+ ++p;
|
|
|
+ continue;
|
|
|
}
|
|
|
- for (i = 0 ; __isdigit(*fmt) ; sc.width = i) {
|
|
|
- i = (i * 10) + (*fmt++ - '0');
|
|
|
+#endif
|
|
|
+
|
|
|
+ for (p = spec_ranges; p_m_spec_chars > *p ; ++p) {}
|
|
|
+ if (((psfs->dataargtype >> 8) | psfs->flags)
|
|
|
+ & ~spec_allowed[(int)(p - spec_ranges)]
|
|
|
+ ) {
|
|
|
+ goto ERROR_EINVAL;
|
|
|
}
|
|
|
- for (i = 0 ; i < sizeof(qual) ; i++) {
|
|
|
- if (qual[i] == *fmt) {
|
|
|
- ++fmt;
|
|
|
- lval += qsz[i];
|
|
|
- if ((i < 2) && (qual[i] == *fmt)) {
|
|
|
- ++fmt;
|
|
|
- lval += qsz[i];
|
|
|
+
|
|
|
+ if ((p_m_spec_chars >= CONV_c)
|
|
|
+ && (psfs->dataargtype & PA_FLAG_LONG)) {
|
|
|
+ p_m_spec_chars -= 3;
|
|
|
+ }
|
|
|
+
|
|
|
+ psfs->conv_num = p_m_spec_chars;
|
|
|
+ return psfs->fmt - fmt0;
|
|
|
+ }
|
|
|
+ if (!*++p) {
|
|
|
+ ERROR_EINVAL:
|
|
|
+ __set_errno(EINVAL);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ } while(1);
|
|
|
+
|
|
|
+ assert(0);
|
|
|
+}
|
|
|
+
|
|
|
+#endif
|
|
|
+
|
|
|
+#if defined(L_vfscanf) || defined(L_vfwscanf)
|
|
|
+
|
|
|
+#ifdef __UCLIBC_HAS_WCHAR__
|
|
|
+#ifdef L_vfscanf
|
|
|
+static int sc_getc(register struct scan_cookie *sc)
|
|
|
+{
|
|
|
+ return getc(sc->fp);
|
|
|
+}
|
|
|
+
|
|
|
+static int scan_getwc(register struct scan_cookie *sc)
|
|
|
+{
|
|
|
+ size_t r;
|
|
|
+ int width;
|
|
|
+ wchar_t wc[1];
|
|
|
+ char b[1];
|
|
|
+
|
|
|
+ if (--sc->width < 0) {
|
|
|
+ sc->ungot_flag |= 2;
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ width = sc->width;
|
|
|
+ sc->width = INT_MAX;
|
|
|
+
|
|
|
+ r = (size_t)(-1);
|
|
|
+ while (__scan_getc(sc) >= 0) {
|
|
|
+ *b = sc->cc;
|
|
|
+
|
|
|
+ r = mbrtowc(wc, b, 1, &sc->mbstate);
|
|
|
+ if (((ssize_t) r) >= 0) {
|
|
|
+ sc->wc = *wc;
|
|
|
+ goto SUCCESS;
|
|
|
+ } else if (r == ((size_t) -2)) {
|
|
|
+
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * mbrtowc set errno to EILSEQ, or r == ((size_t)-2)
|
|
|
+ * and stream is in an error state or at EOF with a
|
|
|
+ * partially complete wchar. */
|
|
|
+ __set_errno(EILSEQ);
|
|
|
+ sc->mb_fail = 1;
|
|
|
+
|
|
|
+ SUCCESS:
|
|
|
+ sc->width = width;
|
|
|
+
|
|
|
+ return (int)((ssize_t) r);
|
|
|
+}
|
|
|
+
|
|
|
+#endif
|
|
|
+
|
|
|
+#ifdef L_vfwscanf
|
|
|
+
|
|
|
+
|
|
|
+ * when the next wide char is expected to be valid ascii (digits).
|
|
|
+ */
|
|
|
+static int sc_getc(register struct scan_cookie *sc)
|
|
|
+{
|
|
|
+ wint_t wc;
|
|
|
+
|
|
|
+ if (sc->fp->filedes == -3) {
|
|
|
+ if (sc->fp->bufpos < sc->fp->bufend) {
|
|
|
+ wc = *((wchar_t *)(sc->fp->bufpos));
|
|
|
+ sc->fp->bufpos += sizeof(wchar_t);
|
|
|
+ } else {
|
|
|
+ sc->fp->modeflags |= __FLAG_EOF;
|
|
|
+ return EOF;
|
|
|
+ }
|
|
|
+ } else if ((wc = fgetwc_unlocked(sc->fp)) == WEOF) {
|
|
|
+ return EOF;
|
|
|
+ }
|
|
|
+
|
|
|
+ sc->ungot_wflag = 1;
|
|
|
+ sc->ungot_wchar = wc;
|
|
|
+ sc->ungot_wchar_width = sc->fp->ungot_width[0];
|
|
|
+
|
|
|
+#ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
|
|
|
+ if (wc == sc->thousands_sep_wc) {
|
|
|
+ wc = ',';
|
|
|
+ } else
|
|
|
+#endif
|
|
|
+#ifdef __UCLIBC_HAS_FLOATS__
|
|
|
+ if (wc == sc->decpt_wc) {
|
|
|
+ wc = '.';
|
|
|
+ } else
|
|
|
+#endif
|
|
|
+ if (!__isascii(wc)) {
|
|
|
+ wc = '?';
|
|
|
+ }
|
|
|
+ sc->wc = sc->ungot_char = wc;
|
|
|
+
|
|
|
+ return (int) wc;
|
|
|
+}
|
|
|
+
|
|
|
+static int scan_getwc(register struct scan_cookie *sc)
|
|
|
+{
|
|
|
+ wint_t wc;
|
|
|
+
|
|
|
+ sc->wc = WEOF;
|
|
|
+
|
|
|
+ if (--sc->width < 0) {
|
|
|
+ sc->ungot_flag |= 2;
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (sc->ungot_flag == 0) {
|
|
|
+
|
|
|
+ if (sc->fp->filedes == -3) {
|
|
|
+ if (sc->fp->bufpos < sc->fp->bufend) {
|
|
|
+ wc = *((wchar_t *)(sc->fp->bufpos));
|
|
|
+ sc->fp->bufpos += sizeof(wchar_t);
|
|
|
+ } else {
|
|
|
+ sc->ungot_flag |= 2;
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ } else if ((wc = fgetwc_unlocked(sc->fp)) == WEOF) {
|
|
|
+ sc->ungot_flag |= 2;
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ sc->ungot_wflag = 1;
|
|
|
+ sc->ungot_char = wc;
|
|
|
+ sc->ungot_wchar_width = sc->fp->ungot_width[0];
|
|
|
+ } else {
|
|
|
+ assert(sc->ungot_flag == 1);
|
|
|
+ sc->ungot_flag = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ ++sc->nread;
|
|
|
+ sc->wc = sc->ungot_char;
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+#endif
|
|
|
+#endif
|
|
|
+
|
|
|
+static __inline void kill_scan_cookie(register struct scan_cookie *sc)
|
|
|
+{
|
|
|
+#ifdef L_vfscanf
|
|
|
+
|
|
|
+ if (sc->ungot_flag & 1) {
|
|
|
+ ungetc(sc->ungot_char, sc->fp);
|
|
|
+
|
|
|
+ if (sc->nread == 0) {
|
|
|
+ sc->fp->ungot[1] = sc->app_ungot;
|
|
|
+ } else {
|
|
|
+ sc->fp->ungot[1] = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+#else
|
|
|
+
|
|
|
+ if ((sc->ungot_wflag & 1) && (sc->fp->filedes != -3) && (sc->fp->state.mask == 0)) {
|
|
|
+ ungetwc(sc->ungot_char, sc->fp);
|
|
|
+
|
|
|
+ if (sc->nread == 0) {
|
|
|
+ sc->fp->ungot[1] = sc->app_ungot;
|
|
|
+ } else {
|
|
|
+ sc->fp->ungot[1] = 0;
|
|
|
+ }
|
|
|
+ sc->fp->ungot_width[1] = sc->ungot_wchar_width;
|
|
|
+ }
|
|
|
+
|
|
|
+#endif
|
|
|
+}
|
|
|
+
|
|
|
+#ifdef L_vfwscanf
|
|
|
+#ifdef __UCLIBC_HAS_FLOATS__
|
|
|
+static const char fake_decpt_str[] = ".";
|
|
|
+#endif
|
|
|
+#ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
|
|
|
+static const char fake_thousands_sep_str[] = ",";
|
|
|
+#endif
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+int VFSCANF (FILE *__restrict fp, const Wchar *__restrict format, va_list arg)
|
|
|
+{
|
|
|
+ const Wuchar *fmt;
|
|
|
+ unsigned char *b;
|
|
|
+
|
|
|
+
|
|
|
+#ifdef L_vfwscanf
|
|
|
+ wchar_t wbuf[1];
|
|
|
+ wchar_t *wb;
|
|
|
+#endif
|
|
|
+
|
|
|
+#ifdef __UCLIBC_HAS_WCHAR__
|
|
|
+ mbstate_t mbstate;
|
|
|
+#endif
|
|
|
+
|
|
|
+ struct scan_cookie sc;
|
|
|
+ psfs_t psfs;
|
|
|
+
|
|
|
+ int i;
|
|
|
+
|
|
|
+#warning fix MAX_DIGITS. we do not do binary, so...!
|
|
|
+#define MAX_DIGITS 65
|
|
|
+ unsigned char buf[MAX_DIGITS+2];
|
|
|
+#ifdef L_vfscanf
|
|
|
+ unsigned char scanset[UCHAR_MAX + 1];
|
|
|
+ unsigned char invert;
|
|
|
+#endif
|
|
|
+ unsigned char fail;
|
|
|
+
|
|
|
+#ifdef __UCLIBC_MJN3_ONLY__
|
|
|
+#warning TODO: Make checking of the format string in C locale an option.
|
|
|
+#endif
|
|
|
+
|
|
|
+#if defined(__UCLIBC_HAS_LOCALE__) && !defined(L_vfwscanf)
|
|
|
+
|
|
|
+ * beginning and ending in its initial shift state. */
|
|
|
+ if (((__UCLIBC_CURLOCALE_DATA).encoding) != __ctype_encoding_7_bit) {
|
|
|
+ mbstate.mask = 0;
|
|
|
+ const char *p = format;
|
|
|
+ if (mbsrtowcs(NULL, &p, SIZE_MAX, &mbstate) == ((size_t)(-1))) {
|
|
|
+ __set_errno(EINVAL);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+#endif
|
|
|
+
|
|
|
+#if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
|
|
|
+ psfs.num_pos_args = -1;
|
|
|
+
|
|
|
+ memset(psfs.pos_args, 0, sizeof(psfs.pos_args));
|
|
|
+#endif
|
|
|
+
|
|
|
+ __STDIO_THREADLOCK(fp);
|
|
|
+
|
|
|
+ __init_scan_cookie(&sc,fp);
|
|
|
+#ifdef __UCLIBC_HAS_WCHAR__
|
|
|
+ sc.sc_getc = sc_getc;
|
|
|
+ sc.ungot_wchar_width = sc.fp->ungot_width[1];
|
|
|
+
|
|
|
+#ifdef L_vfwscanf
|
|
|
+
|
|
|
+#ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
|
|
|
+ if (*sc.grouping) {
|
|
|
+ sc.thousands_sep = fake_thousands_sep_str;
|
|
|
+ sc.tslen = 1;
|
|
|
+ }
|
|
|
+#endif
|
|
|
+
|
|
|
+#ifdef __UCLIBC_HAS_FLOATS__
|
|
|
+ sc.fake_decpt = fake_decpt_str;
|
|
|
+#endif
|
|
|
+
|
|
|
+#else
|
|
|
+
|
|
|
+#ifdef __UCLIBC_HAS_FLOATS__
|
|
|
+ sc.fake_decpt = sc.decpt;
|
|
|
+#endif
|
|
|
+
|
|
|
+#endif
|
|
|
+
|
|
|
+#endif
|
|
|
+ psfs.cnt = 0;
|
|
|
+
|
|
|
+
|
|
|
+ * would really need to do a mb->wc conversion here in the
|
|
|
+ * vfscanf case. Related changes would have to be made in
|
|
|
+ * the code that follows... basicly wherever fmt appears. */
|
|
|
+ for (fmt = (const Wuchar *) format ; *fmt ; ) {
|
|
|
+
|
|
|
+ psfs.store = 1;
|
|
|
+ psfs.flags = 0;
|
|
|
+#ifndef NDEBUG
|
|
|
+ psfs.cur_ptr = NULL;
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+ sc.ungot_flag &= 1;
|
|
|
+ sc.width = psfs.max_width = INT_MAX;
|
|
|
+
|
|
|
+
|
|
|
+ * here. So, if we did a mb->wc conversion, we would have to do
|
|
|
+ * something like
|
|
|
+ * ((((__uwchar_t)wc) < UCHAR_MAX) && isspace(wc))
|
|
|
+ * because wc might not be in the allowed domain. */
|
|
|
+ if (ISSPACE(*fmt)) {
|
|
|
+ do {
|
|
|
+ ++fmt;
|
|
|
+ } while (ISSPACE(*fmt));
|
|
|
+ --fmt;
|
|
|
+ psfs.conv_num = CONV_whitespace;
|
|
|
+ goto DO_WHITESPACE;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (*fmt == '%') {
|
|
|
+ if (*++fmt == '%') {
|
|
|
+ psfs.conv_num = CONV_percent;
|
|
|
+ goto DO_CONVERSION;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+#ifdef L_vfscanf
|
|
|
+ psfs.fmt = fmt;
|
|
|
+#else
|
|
|
+ {
|
|
|
+ const __uwchar_t *wf = fmt;
|
|
|
+ psfs.fmt = b = buf;
|
|
|
+
|
|
|
+ while (*wf && __isascii(*wf) && (b < buf + sizeof(buf) - 1)) {
|
|
|
+ *b++ = *wf++;
|
|
|
+ }
|
|
|
+#ifdef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
|
|
|
+#error this is wrong... we need to ched in __psfs_parse_spec instead since this checks last char in buffer and conversion my have stopped before it.
|
|
|
+ if ((*b == 'a') && ((*wf == '[') || ((*wf|0x20) == 's'))) {
|
|
|
+ goto DONE;
|
|
|
+ }
|
|
|
+#endif
|
|
|
+ *b = 0;
|
|
|
+ if (b == buf) {
|
|
|
+ goto DONE;
|
|
|
+ }
|
|
|
+ }
|
|
|
+#endif
|
|
|
+ if ((i = __psfs_parse_spec(&psfs)) < 0) {
|
|
|
+ goto DONE;
|
|
|
+ }
|
|
|
+ fmt += i;
|
|
|
+
|
|
|
+#if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
|
|
|
+ if (psfs.store) {
|
|
|
+ if (psfs.num_pos_args == -2) {
|
|
|
+ psfs.cur_ptr = va_arg(arg, void *);
|
|
|
+ } else {
|
|
|
+ while (psfs.cur_pos_arg > psfs.num_pos_args) {
|
|
|
+ psfs.pos_args[++psfs.num_pos_args] = va_arg(arg, void *);
|
|
|
}
|
|
|
- break;
|
|
|
+ psfs.cur_ptr = psfs.pos_args[psfs.cur_pos_arg];
|
|
|
+ }
|
|
|
+ }
|
|
|
+#else
|
|
|
+ psfs.cur_ptr = va_arg(arg, void *);
|
|
|
+#endif
|
|
|
+
|
|
|
+ DO_CONVERSION:
|
|
|
+
|
|
|
+ if ((((1L << CONV_n)|(1L << CONV_C)|(1L << CONV_c)
|
|
|
+ |(1L << CONV_LEFTBRACKET)|(1L << CONV_leftbracket))
|
|
|
+ & (1L << psfs.conv_num)) == 0
|
|
|
+ ) {
|
|
|
+ DO_WHITESPACE:
|
|
|
+ while ((__scan_getc(&sc) >= 0)
|
|
|
+#ifdef L_vfscanf
|
|
|
+ && isspace(sc.cc)
|
|
|
+#else
|
|
|
+ && iswspace(sc.wc)
|
|
|
+#endif
|
|
|
+ ) {}
|
|
|
+ __scan_ungetc(&sc);
|
|
|
+ if (psfs.conv_num == CONV_whitespace) {
|
|
|
+ goto NEXT_FMT;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ sc.width = psfs.max_width;
|
|
|
+
|
|
|
+ if (sc.width == 0) {
|
|
|
+ goto DONE;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if (psfs.conv_num == CONV_percent) {
|
|
|
+ goto MATCH_CHAR;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (psfs.conv_num == CONV_n) {
|
|
|
+ if (psfs.store) {
|
|
|
+ _store_inttype(psfs.cur_ptr, psfs.dataargtype,
|
|
|
+ (uintmax_t) sc.nread);
|
|
|
}
|
|
|
+ goto NEXT_FMT;
|
|
|
}
|
|
|
- for (p = spec ; *p ; p++) {
|
|
|
- if (*fmt != *p) continue;
|
|
|
- if (p-spec < 1) {
|
|
|
- goto matchchar;
|
|
|
+
|
|
|
+ if (psfs.conv_num <= CONV_A) {
|
|
|
+#ifdef L_vfscanf
|
|
|
+ if (__psfs_do_numeric(&psfs, &sc) < 0) {
|
|
|
+ goto DONE;
|
|
|
}
|
|
|
- if (p-spec < 2) {
|
|
|
- *(va_arg(ap, int *)) = sc.nread;
|
|
|
- scan_getc_nw(&sc);
|
|
|
- goto nextfmt;
|
|
|
+ goto NEXT_FMT;
|
|
|
+#else
|
|
|
+ int r = __psfs_do_numeric(&psfs, &sc);
|
|
|
+ if (sc.ungot_wflag == 1) {
|
|
|
+ sc.cc = sc.ungot_char = sc.ungot_wchar;
|
|
|
}
|
|
|
- if (p-spec > 3) {
|
|
|
- do {
|
|
|
- i = scan_getc_nw(&sc);
|
|
|
- } while (__isspace(i));
|
|
|
- scan_ungetc(&sc);
|
|
|
+ if (r < 0) {
|
|
|
+ goto DONE;
|
|
|
+ }
|
|
|
+ goto NEXT_FMT;
|
|
|
+#endif
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#ifdef L_vfscanf
|
|
|
+
|
|
|
+ if
|
|
|
+#ifdef __UCLIBC_HAS_WCHAR__
|
|
|
+ (psfs.conv_num >= CONV_LEFTBRACKET)
|
|
|
+#else
|
|
|
+ (psfs.conv_num >= CONV_c)
|
|
|
+#endif
|
|
|
+ {
|
|
|
+ b = (psfs.store ? ((unsigned char *) psfs.cur_ptr) : buf);
|
|
|
+ fail = 1;
|
|
|
+
|
|
|
+
|
|
|
+ if (psfs.conv_num == CONV_c) {
|
|
|
+ if (sc.width == INT_MAX) {
|
|
|
+ sc.width = 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ while (__scan_getc(&sc) >= 0) {
|
|
|
+ *b = sc.cc;
|
|
|
+ b += psfs.store;
|
|
|
+ }
|
|
|
+ __scan_ungetc(&sc);
|
|
|
+ if (sc.width > 0) {
|
|
|
+ goto DONE;
|
|
|
+ }
|
|
|
+ psfs.cnt += psfs.store;
|
|
|
+ goto NEXT_FMT;
|
|
|
}
|
|
|
- if (p-spec < 5) {
|
|
|
+
|
|
|
+ if (psfs.conv_num == CONV_s) {
|
|
|
+
|
|
|
+ while ((__scan_getc(&sc) >= 0) && !isspace(sc.cc)) {
|
|
|
+ *b = sc.cc;
|
|
|
+ b += psfs.store;
|
|
|
+ fail = 0;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+#ifdef __UCLIBC_HAS_WCHAR__
|
|
|
+ assert((psfs.conv_num == CONV_LEFTBRACKET) || \
|
|
|
+ (psfs.conv_num == CONV_leftbracket));
|
|
|
+#else
|
|
|
+ assert((psfs.conv_num == CONV_leftbracket));
|
|
|
+#endif
|
|
|
+
|
|
|
invert = 0;
|
|
|
- if (*p == 'c') {
|
|
|
+
|
|
|
+ if (*++fmt == '^') {
|
|
|
+ ++fmt;
|
|
|
invert = 1;
|
|
|
- if (sc.width == INT_MAX) {
|
|
|
- sc.width = 1;
|
|
|
- }
|
|
|
- }
|
|
|
- for (i=0 ; i<= UCHAR_MAX ; i++) {
|
|
|
- scanset[i] = ((*p == 's') ? (__isspace(i) == 0) : 0);
|
|
|
+ }
|
|
|
+ memset(scanset, invert, sizeof(scanset));
|
|
|
+ invert = 1-invert;
|
|
|
+
|
|
|
+ if (*fmt == ']') {
|
|
|
+ scanset[(int)(']')] = invert;
|
|
|
+ ++fmt;
|
|
|
}
|
|
|
- if (*p == '[') {
|
|
|
- if (*++fmt == '^') {
|
|
|
- invert = 1;
|
|
|
- ++fmt;
|
|
|
- }
|
|
|
- if (*fmt == ']') {
|
|
|
- scanset[(int)']'] = 1;
|
|
|
- ++fmt;
|
|
|
+
|
|
|
+ while (*fmt != ']') {
|
|
|
+ if (!*fmt) {
|
|
|
+ goto DONE;
|
|
|
}
|
|
|
- r0 = 0;
|
|
|
- while (*fmt && *fmt !=']') {
|
|
|
- if ((*fmt == '-') && r0 && (fmt[1] != ']')) {
|
|
|
-
|
|
|
- ++fmt;
|
|
|
- if (*fmt < r0) {
|
|
|
- r1 = r0;
|
|
|
- r0 = *fmt;
|
|
|
- } else {
|
|
|
- r1 = *fmt;
|
|
|
- }
|
|
|
- for (i=r0 ; i<= r1 ; i++) {
|
|
|
- scanset[i] = 1;
|
|
|
- }
|
|
|
- r0 = 0;
|
|
|
- } else {
|
|
|
- r0 = *fmt;
|
|
|
- scanset[r0] = 1;
|
|
|
- }
|
|
|
+ if ((*fmt == '-') && (fmt[1] != ']')
|
|
|
+ && (fmt[-1] < fmt[1])
|
|
|
+ ) {
|
|
|
++fmt;
|
|
|
+ i = fmt[-2];
|
|
|
+
|
|
|
+ * in the previous iteration. */
|
|
|
+ do {
|
|
|
+ scanset[++i] = invert;
|
|
|
+ } while (i < *fmt);
|
|
|
+
|
|
|
}
|
|
|
- if (!*fmt) {
|
|
|
- goto done;
|
|
|
- }
|
|
|
+
|
|
|
+ scanset[(int) *fmt] = invert;
|
|
|
+ ++fmt;
|
|
|
}
|
|
|
-
|
|
|
- if (sc.width <= 0) {
|
|
|
- goto done;
|
|
|
+
|
|
|
+#ifdef __UCLIBC_HAS_WCHAR__
|
|
|
+ if (psfs.conv_num == CONV_LEFTBRACKET) {
|
|
|
+ goto DO_LEFTBRACKET;
|
|
|
}
|
|
|
- if (store) {
|
|
|
- b = va_arg(ap, unsigned char *);
|
|
|
- } else {
|
|
|
- b = buf;
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+ while (__scan_getc(&sc) >= 0) {
|
|
|
+ if (!scanset[sc.cc]) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ *b = sc.cc;
|
|
|
+ b += psfs.store;
|
|
|
+ fail = 0;
|
|
|
}
|
|
|
- cc = scan_getc(&sc);
|
|
|
- if (cc < 0) {
|
|
|
- scan_ungetc(&sc);
|
|
|
- goto done;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ __scan_ungetc(&sc);
|
|
|
+ if (fail) {
|
|
|
+ goto DONE;
|
|
|
+ }
|
|
|
+ *b = 0;
|
|
|
+ psfs.cnt += psfs.store;
|
|
|
+ goto NEXT_FMT;
|
|
|
+ }
|
|
|
+
|
|
|
+#ifdef __UCLIBC_HAS_WCHAR__
|
|
|
+ DO_LEFTBRACKET:
|
|
|
+ if (psfs.conv_num >= CONV_C) {
|
|
|
+ wchar_t wbuf[1];
|
|
|
+ wchar_t *wb;
|
|
|
+
|
|
|
+ sc.mbstate.mask = 0;
|
|
|
+
|
|
|
+ wb = (psfs.store ? ((wchar_t *) psfs.cur_ptr) : wbuf);
|
|
|
+ fail = 1;
|
|
|
+
|
|
|
+ if (psfs.conv_num == CONV_C) {
|
|
|
+ if (sc.width == INT_MAX) {
|
|
|
+ sc.width = 1;
|
|
|
}
|
|
|
- if (*p == 'c') {
|
|
|
- goto c_spec;
|
|
|
+
|
|
|
+ while (scan_getwc(&sc) >= 0) {
|
|
|
+ assert(sc.width >= 0);
|
|
|
+ *wb = sc.wc;
|
|
|
+ wb += psfs.store;
|
|
|
}
|
|
|
- i = 0;
|
|
|
- while ((cc>=0) && (scanset[cc] != invert)) {
|
|
|
- c_spec:
|
|
|
- i = 1;
|
|
|
- *b = cc;
|
|
|
- b += store;
|
|
|
- cc = scan_getc(&sc);
|
|
|
+
|
|
|
+ __scan_ungetc(&sc);
|
|
|
+ if (sc.width > 0) {
|
|
|
+ goto DONE;
|
|
|
}
|
|
|
- if (i==0) {
|
|
|
- scan_ungetc(&sc);
|
|
|
- goto done;
|
|
|
+ psfs.cnt += psfs.store;
|
|
|
+ goto NEXT_FMT;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if (psfs.conv_num == CONV_S) {
|
|
|
+
|
|
|
+ while ((scan_getwc(&sc) >= 0)
|
|
|
+ && ((((__uwchar_t)(sc.wc)) > UCHAR_MAX)
|
|
|
+ || !isspace(sc.wc))
|
|
|
+ ) {
|
|
|
+ *wb = sc.wc;
|
|
|
+ wb += psfs.store;
|
|
|
+ fail = 0;
|
|
|
}
|
|
|
- if (*p != 'c') {
|
|
|
- *b = 0;
|
|
|
+ } else {
|
|
|
+ assert(psfs.conv_num == CONV_LEFTBRACKET);
|
|
|
+
|
|
|
+ while (scan_getwc(&sc) >= 0) {
|
|
|
+ if (((__uwchar_t) sc.wc) <= UCHAR_MAX) {
|
|
|
+ if (!scanset[sc.wc]) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ } else if (invert) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ *wb = sc.wc;
|
|
|
+ wb += psfs.store;
|
|
|
+ fail = 0;
|
|
|
}
|
|
|
- cnt += store;
|
|
|
- goto nextfmt;
|
|
|
}
|
|
|
- if (p-spec < 12) {
|
|
|
- if (*p == 'p') {
|
|
|
-
|
|
|
- lval = (sizeof(char *) == sizeof(long));
|
|
|
+
|
|
|
+
|
|
|
+ __scan_ungetc(&sc);
|
|
|
+ if (fail || sc.mb_fail) {
|
|
|
+ goto DONE;
|
|
|
+ }
|
|
|
+ *wb = 0;
|
|
|
+ psfs.cnt += psfs.store;
|
|
|
+ goto NEXT_FMT;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+#endif
|
|
|
+#else
|
|
|
+
|
|
|
+ if (psfs.conv_num >= CONV_C) {
|
|
|
+ b = buf;
|
|
|
+ wb = wbuf;
|
|
|
+ if (psfs.conv_num >= CONV_c) {
|
|
|
+ mbstate.mask = 0;
|
|
|
+ if (psfs.store) {
|
|
|
+ b = (unsigned char *) psfs.cur_ptr;
|
|
|
}
|
|
|
- usflag = ((p-spec) < 10);
|
|
|
- base = radix[(int)(p-spec) - 5];
|
|
|
- b = buf;
|
|
|
- if (sc.width <= 0) {
|
|
|
- goto done;
|
|
|
+ } else {
|
|
|
+ if (psfs.store) {
|
|
|
+ wb = (wchar_t *) psfs.cur_ptr;
|
|
|
}
|
|
|
- cc = scan_getc(&sc);
|
|
|
- if ((cc == '+') || (cc == '-')) {
|
|
|
- *b++ = cc;
|
|
|
- cc = scan_getc(&sc);
|
|
|
+ }
|
|
|
+ fail = 1;
|
|
|
+
|
|
|
+
|
|
|
+ if ((psfs.conv_num == CONV_C) || (psfs.conv_num == CONV_c)) {
|
|
|
+ if (sc.width == INT_MAX) {
|
|
|
+ sc.width = 1;
|
|
|
}
|
|
|
- if (cc == '0') {
|
|
|
- if ((base == 0) || (base == 16)) {
|
|
|
- cc = scan_getc(&sc);
|
|
|
- if ((cc == 'x') || (cc == 'X')) {
|
|
|
-
|
|
|
- base = 16;
|
|
|
- cc = scan_getc(&sc);
|
|
|
- } else {
|
|
|
- scan_ungetc(&sc);
|
|
|
- cc = '0';
|
|
|
- if (base == 0) {
|
|
|
- base = 8;
|
|
|
- }
|
|
|
+
|
|
|
+ while (scan_getwc(&sc) >= 0) {
|
|
|
+ if (psfs.conv_num == CONV_C) {
|
|
|
+ *wb = sc.wc;
|
|
|
+ wb += psfs.store;
|
|
|
+ } else {
|
|
|
+ i = wcrtomb(b, sc.wc, &mbstate);
|
|
|
+ if (i < 0) {
|
|
|
+ goto DONE_DO_UNGET;
|
|
|
+ }
|
|
|
+ if (psfs.store) {
|
|
|
+ b += i;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- if (base == 0) {
|
|
|
- base = 10;
|
|
|
- }
|
|
|
-
|
|
|
- if (cc == '0') {
|
|
|
- *b++ = cc;
|
|
|
- do {
|
|
|
- cc = scan_getc(&sc);
|
|
|
- } while (cc == '0');
|
|
|
+ __scan_ungetc(&sc);
|
|
|
+ if (sc.width > 0) {
|
|
|
+ goto DONE;
|
|
|
}
|
|
|
- while (valid_digit(cc,base)) {
|
|
|
- if (b - buf < MAX_DIGITS) {
|
|
|
- *b++ = cc;
|
|
|
+ psfs.cnt += psfs.store;
|
|
|
+ goto NEXT_FMT;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ((psfs.conv_num == CONV_S) || (psfs.conv_num == CONV_s)) {
|
|
|
+
|
|
|
+ while ((scan_getwc(&sc) >= 0) && !iswspace(sc.wc)) {
|
|
|
+ if (psfs.conv_num == CONV_S) {
|
|
|
+ *wb = sc.wc;
|
|
|
+ wb += psfs.store;
|
|
|
+ } else {
|
|
|
+ i = wcrtomb(b, sc.wc, &mbstate);
|
|
|
+ if (i < 0) {
|
|
|
+ goto DONE_DO_UNGET;
|
|
|
+ }
|
|
|
+ if (psfs.store) {
|
|
|
+ b += i;
|
|
|
+ }
|
|
|
}
|
|
|
- cc = scan_getc(&sc);
|
|
|
+ fail = 0;
|
|
|
}
|
|
|
- *b = 0;
|
|
|
- if ((b == buf) || (*--b == '+') || (*b == '-')) {
|
|
|
- scan_ungetc(&sc);
|
|
|
- goto done;
|
|
|
+ } else {
|
|
|
+ const wchar_t *sss;
|
|
|
+ const wchar_t *ssp;
|
|
|
+ unsigned char invert = 0;
|
|
|
+
|
|
|
+ assert((psfs.conv_num == CONV_LEFTBRACKET)
|
|
|
+ || (psfs.conv_num == CONV_leftbracket));
|
|
|
+
|
|
|
+ if (*++fmt == '^') {
|
|
|
+ ++fmt;
|
|
|
+ invert = 1;
|
|
|
}
|
|
|
- if (store) {
|
|
|
- if (*buf == '-') {
|
|
|
- usflag = 0;
|
|
|
+ sss = (const wchar_t *) fmt;
|
|
|
+ if (*fmt == ']') {
|
|
|
+ ++fmt;
|
|
|
+ }
|
|
|
+ while (*fmt != ']') {
|
|
|
+ if (!*fmt) {
|
|
|
+ goto DONE;
|
|
|
}
|
|
|
- uv = STRTO_L_(buf, NULL, base, 1-usflag);
|
|
|
- vp = va_arg(ap, void *);
|
|
|
- switch (lval) {
|
|
|
- case 2:
|
|
|
- *((unsigned long long *)vp) = uv;
|
|
|
- break;
|
|
|
- case 1:
|
|
|
-#if ULONG_MAX == UINT_MAX
|
|
|
- case 0:
|
|
|
-#endif
|
|
|
- if (usflag) {
|
|
|
- if (uv > ULONG_MAX) {
|
|
|
- uv = ULONG_MAX;
|
|
|
- }
|
|
|
- } else if (((V_TYPE)uv) > LONG_MAX) {
|
|
|
- uv = LONG_MAX;
|
|
|
- } else if (((V_TYPE)uv) < LONG_MIN) {
|
|
|
- uv = (UV_TYPE) LONG_MIN;
|
|
|
- }
|
|
|
- *((unsigned long *)vp) = (unsigned long)uv;
|
|
|
- break;
|
|
|
-#if ULONG_MAX != UINT_MAX
|
|
|
- case 0:
|
|
|
- if (usflag) {
|
|
|
- if (uv > UINT_MAX) {
|
|
|
- uv = UINT_MAX;
|
|
|
- }
|
|
|
- } else if (((V_TYPE)uv) > INT_MAX) {
|
|
|
- uv = INT_MAX;
|
|
|
- } else if (((V_TYPE)uv) < INT_MIN) {
|
|
|
- uv = (UV_TYPE) INT_MIN;
|
|
|
- }
|
|
|
- *((unsigned int *)vp) = (unsigned int)uv;
|
|
|
- break;
|
|
|
-#endif
|
|
|
- case (signed char)(-1):
|
|
|
- if (usflag) {
|
|
|
- if (uv > USHRT_MAX) {
|
|
|
- uv = USHRT_MAX;
|
|
|
- }
|
|
|
- } else if (((V_TYPE)uv) > SHRT_MAX) {
|
|
|
- uv = SHRT_MAX;
|
|
|
- } else if (((V_TYPE)uv) < SHRT_MIN) {
|
|
|
- uv = (UV_TYPE) SHRT_MIN;
|
|
|
- }
|
|
|
- *((unsigned short *)vp) = (unsigned short)uv;
|
|
|
- break;
|
|
|
- case (signed char)(-2):
|
|
|
- if (usflag) {
|
|
|
- if (uv > UCHAR_MAX) {
|
|
|
- uv = UCHAR_MAX;
|
|
|
+ if ((*fmt == '-') && (fmt[1] != ']')
|
|
|
+ && (fmt[-1] < fmt[1])
|
|
|
+ ) {
|
|
|
+ ++fmt;
|
|
|
+ }
|
|
|
+ ++fmt;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ while (scan_getwc(&sc) >= 0) {
|
|
|
+ ssp = sss;
|
|
|
+ do {
|
|
|
+ if (*ssp == '-') {
|
|
|
+
|
|
|
+ * equivalent to a-e. */
|
|
|
+ if (ssp > sss) {
|
|
|
+ if ((++ssp < (const wchar_t *) fmt)
|
|
|
+ && (ssp[-2] < *ssp)
|
|
|
+ ) {
|
|
|
+ if ((sc.wc >= ssp[-2])
|
|
|
+ && (sc.wc <= *ssp)) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ continue;
|
|
|
}
|
|
|
- } else if (((V_TYPE)uv) > CHAR_MAX) {
|
|
|
- uv = CHAR_MAX;
|
|
|
- } else if (((V_TYPE)uv) < CHAR_MIN) {
|
|
|
- uv = (UV_TYPE) CHAR_MIN;
|
|
|
+ --ssp;
|
|
|
}
|
|
|
- *((unsigned char *)vp) = (unsigned char) uv;
|
|
|
+
|
|
|
+ }
|
|
|
+ if (sc.wc == *ssp) {
|
|
|
break;
|
|
|
- default:
|
|
|
- assert(0);
|
|
|
+ }
|
|
|
+ } while (++ssp < (const wchar_t *) fmt);
|
|
|
+
|
|
|
+ if ((ssp == (const wchar_t *) fmt) ^ invert) {
|
|
|
+
|
|
|
+ * or match and inverting */
|
|
|
+ break;
|
|
|
}
|
|
|
- ++cnt;
|
|
|
- }
|
|
|
- goto nextfmt;
|
|
|
- }
|
|
|
-#ifdef __UCLIBC_HAS_FLOATS__
|
|
|
- else {
|
|
|
- if (sc.width <= 0) {
|
|
|
- goto done;
|
|
|
- }
|
|
|
- if (__strtold(&ld, &sc)) {
|
|
|
- if (store) {
|
|
|
- vp = va_arg(ap, void *);
|
|
|
- switch (lval) {
|
|
|
- case 2:
|
|
|
- *((long double *)vp) = ld;
|
|
|
- break;
|
|
|
- case 1:
|
|
|
- *((double *)vp) = (double) ld;
|
|
|
- break;
|
|
|
- case 0:
|
|
|
- *((float *)vp) = (float) ld;
|
|
|
- break;
|
|
|
- default:
|
|
|
- assert(0);
|
|
|
- goto done;
|
|
|
+ if (psfs.conv_num == CONV_LEFTBRACKET) {
|
|
|
+ *wb = sc.wc;
|
|
|
+ wb += psfs.store;
|
|
|
+ } else {
|
|
|
+ i = wcrtomb(b, sc.wc, &mbstate);
|
|
|
+ if (i < 0) {
|
|
|
+ goto DONE_DO_UNGET;
|
|
|
+ }
|
|
|
+ if (psfs.store) {
|
|
|
+ b += i;
|
|
|
}
|
|
|
- ++cnt;
|
|
|
}
|
|
|
- goto nextfmt;
|
|
|
+ fail = 0;
|
|
|
}
|
|
|
}
|
|
|
-#else
|
|
|
- assert(0);
|
|
|
-#endif
|
|
|
- goto done;
|
|
|
- }
|
|
|
-
|
|
|
- goto RETURN_cnt;
|
|
|
- } if (__isspace(*fmt)) {
|
|
|
- do {
|
|
|
- i = scan_getc_nw(&sc);
|
|
|
- } while (__isspace(i));
|
|
|
- } else {
|
|
|
- matchchar:
|
|
|
- if (scan_getc_nw(&sc) != *fmt) {
|
|
|
- scan_ungetc(&sc);
|
|
|
- goto done;
|
|
|
+
|
|
|
+
|
|
|
+ __scan_ungetc(&sc);
|
|
|
+ if (fail) {
|
|
|
+ goto DONE;
|
|
|
+ }
|
|
|
+ *wb = 0;
|
|
|
+ *b = 0;
|
|
|
+ psfs.cnt += psfs.store;
|
|
|
+ goto NEXT_FMT;
|
|
|
}
|
|
|
- scan_getc_nw(&sc);
|
|
|
+
|
|
|
+#endif
|
|
|
+
|
|
|
+ assert(0);
|
|
|
+ goto DONE;
|
|
|
+ }
|
|
|
+
|
|
|
+ MATCH_CHAR:
|
|
|
+ if (__scan_getc(&sc) != *fmt) {
|
|
|
+#ifdef L_vfwscanf
|
|
|
+ DONE_DO_UNGET:
|
|
|
+#endif
|
|
|
+ __scan_ungetc(&sc);
|
|
|
+ goto DONE;
|
|
|
}
|
|
|
- nextfmt:
|
|
|
- scan_ungetc(&sc);
|
|
|
+
|
|
|
+ NEXT_FMT:
|
|
|
++fmt;
|
|
|
}
|
|
|
|
|
|
- done:
|
|
|
- kill_scan_cookie(&sc);
|
|
|
-
|
|
|
- if ((sc.ungot_char <= 0) && (cnt == 0) && (*fmt)) {
|
|
|
- cnt = EOF;
|
|
|
+ DONE:
|
|
|
+ if ((psfs.cnt == 0) && (*fmt) && __FEOF_OR_FERROR(fp)) {
|
|
|
+ psfs.cnt = EOF;
|
|
|
}
|
|
|
|
|
|
- RETURN_cnt:
|
|
|
+ kill_scan_cookie(&sc);
|
|
|
+
|
|
|
+
|
|
|
__STDIO_THREADUNLOCK(fp);
|
|
|
|
|
|
- return (cnt);
|
|
|
+ return psfs.cnt;
|
|
|
}
|
|
|
+#endif
|
|
|
+
|
|
|
+#ifdef L___psfs_do_numeric
|
|
|
+
|
|
|
+static const unsigned char spec_base[] = SPEC_BASE;
|
|
|
+static const unsigned char nil_string[] = "(nil)";
|
|
|
|
|
|
-
|
|
|
+int __psfs_do_numeric(psfs_t *psfs, struct scan_cookie *sc)
|
|
|
+{
|
|
|
+ unsigned char *b;
|
|
|
+ const unsigned char *p;
|
|
|
#ifdef __UCLIBC_HAS_FLOATS__
|
|
|
+ int exp_adjust = 0;
|
|
|
+#endif
|
|
|
+#warning fix MAX_DIGITS. we do not do binary, so...!
|
|
|
+#define MAX_DIGITS 65
|
|
|
+#warning fix buf!
|
|
|
+ unsigned char buf[MAX_DIGITS+2+ 100];
|
|
|
+ unsigned char usflag, base;
|
|
|
+ unsigned char nonzero = 0;
|
|
|
+ unsigned char seendigit = 0;
|
|
|
+
|
|
|
|
|
|
-#include <float.h>
|
|
|
+#ifndef __UCLIBC_HAS_FLOATS__
|
|
|
+ if (psfs->conv_num > CONV_i) {
|
|
|
+ goto DONE;
|
|
|
+ }
|
|
|
+#endif
|
|
|
|
|
|
-#define MAX_SIG_DIGITS 20
|
|
|
-#define MAX_IGNORED_DIGITS 2000
|
|
|
-#define MAX_ALLOWED_EXP (MAX_SIG_DIGITS + MAX_IGNORED_DIGITS + LDBL_MAX_10_EXP)
|
|
|
+ base = spec_base[psfs->conv_num - CONV_p];
|
|
|
+ usflag = (psfs->conv_num <= CONV_u);
|
|
|
+ b = buf;
|
|
|
|
|
|
-#if LDBL_DIG > MAX_SIG_DIGITS
|
|
|
-#error need to adjust MAX_SIG_DIGITS
|
|
|
-#endif
|
|
|
|
|
|
-#include <limits.h>
|
|
|
-#if MAX_ALLOWED_EXP > INT_MAX
|
|
|
-#error size assumption violated for MAX_ALLOWED_EXP
|
|
|
+ if (psfs->conv_num == CONV_p) {
|
|
|
+ p = nil_string;
|
|
|
+ do {
|
|
|
+ if ((__scan_getc(sc) < 0) || (*p != sc->cc)) {
|
|
|
+ __scan_ungetc(sc);
|
|
|
+ if (p > nil_string) {
|
|
|
+
|
|
|
+ * are at eof, we can not match a pointer. */
|
|
|
+ goto DONE;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (!*++p) {
|
|
|
+ if (psfs->store) {
|
|
|
+ ++psfs->cnt;
|
|
|
+ _store_inttype(psfs->cur_ptr, psfs->dataargtype,
|
|
|
+ (uintmax_t) NULL);
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ } while (1);
|
|
|
+
|
|
|
+#ifdef __UCLIBC_MJN3_ONLY__
|
|
|
+#warning CONSIDER: Should we require a 0x prefix and disallow +/- for pointer %p?
|
|
|
+#endif
|
|
|
+ }
|
|
|
+
|
|
|
+ __scan_getc(sc);
|
|
|
+ if ((sc->cc == '+') || (sc->cc == '-')) {
|
|
|
+ *b++ = sc->cc;
|
|
|
+ __scan_getc(sc);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ((base & 0xef) == 0) {
|
|
|
+ if (sc->cc == '0') {
|
|
|
+ __scan_getc(sc);
|
|
|
+ if ((sc->cc|0x20) == 'x') {
|
|
|
+ if ((__scan_getc(sc) < 0)
|
|
|
+#ifdef __UCLIBC_HAS_WCHAR__
|
|
|
+ && !sc->ungot_wflag
|
|
|
+#endif
|
|
|
+ ) {
|
|
|
+
|
|
|
+ * While this looks like I'm 'unget'ing twice,
|
|
|
+ * EOF and end of field are handled specially
|
|
|
+ * by the scan_* funcs. */
|
|
|
+ __scan_ungetc(sc);
|
|
|
+ goto DO_NO_0X;
|
|
|
+ }
|
|
|
+ base = 16;
|
|
|
+#ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
|
|
|
+
|
|
|
+ *b++ = '0';
|
|
|
+ *b++ = 'x';
|
|
|
+#endif
|
|
|
+ } else {
|
|
|
+ DO_NO_0X:
|
|
|
+ __scan_ungetc(sc);
|
|
|
+ sc->cc = '0';
|
|
|
+
|
|
|
+ base = (base >> 1) + 8;
|
|
|
+#ifdef __UCLIBC_HAS_FLOATS__
|
|
|
+ if (psfs->conv_num > CONV_i) {
|
|
|
+ base = 10;
|
|
|
+ }
|
|
|
#endif
|
|
|
+ }
|
|
|
+ } else if (!base) {
|
|
|
+ base = 10;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
-int __strtold(long double *ld, struct scan_cookie *sc)
|
|
|
-{
|
|
|
- long double number;
|
|
|
- long double p10;
|
|
|
- int exponent_power;
|
|
|
- int exponent_temp;
|
|
|
- int negative;
|
|
|
- int num_digits;
|
|
|
- int since_decimal;
|
|
|
- int c;
|
|
|
+
|
|
|
+#ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
|
|
|
+
|
|
|
+ if ((psfs->flags & FLAG_THOUSANDS) && (base == 10)
|
|
|
+ && *(p = sc->grouping)
|
|
|
+ ) {
|
|
|
|
|
|
- c = scan_getc(sc);
|
|
|
-
|
|
|
- negative = 0;
|
|
|
- switch(c) {
|
|
|
- case '-': negative = 1;
|
|
|
- case '+': c = scan_getc(sc);
|
|
|
- }
|
|
|
-
|
|
|
- number = 0.;
|
|
|
- num_digits = -1;
|
|
|
- exponent_power = 0;
|
|
|
- since_decimal = INT_MIN;
|
|
|
-
|
|
|
- LOOP:
|
|
|
- while (__isdigit(c)) {
|
|
|
- ++since_decimal;
|
|
|
- if (num_digits < 0) {
|
|
|
- ++num_digits;
|
|
|
- }
|
|
|
- if (num_digits || (c != '0')) {
|
|
|
- ++num_digits;
|
|
|
- if (num_digits <= MAX_SIG_DIGITS) {
|
|
|
- number = number * 10. + (c - '0');
|
|
|
+ int nblk1, nblk2, nbmax, lastblock, pass, i;
|
|
|
+
|
|
|
+
|
|
|
+#ifdef __UCLIBC_MJN3_ONLY__
|
|
|
+#warning CONSIDER: Should we initalize the grouping blocks in __init_scan_cookie()?
|
|
|
+#endif
|
|
|
+ nbmax = nblk2 = nblk1 = *p;
|
|
|
+ if (*++p) {
|
|
|
+ nblk2 = *p;
|
|
|
+ if (nbmax < nblk2) {
|
|
|
+ nbmax = nblk2;
|
|
|
}
|
|
|
+ assert(!*++p);
|
|
|
}
|
|
|
- c = scan_getc(sc);
|
|
|
- }
|
|
|
-
|
|
|
- if ((c == '.') && (since_decimal < 0)) {
|
|
|
- since_decimal = 0;
|
|
|
- c = scan_getc(sc);
|
|
|
- goto LOOP;
|
|
|
- }
|
|
|
-
|
|
|
- if (num_digits<0) {
|
|
|
- goto FAIL;
|
|
|
- }
|
|
|
-
|
|
|
- if (num_digits > MAX_SIG_DIGITS) {
|
|
|
- exponent_power += num_digits - MAX_SIG_DIGITS;
|
|
|
- }
|
|
|
-
|
|
|
- if (since_decimal >= 0) {
|
|
|
- exponent_power -= since_decimal;
|
|
|
- }
|
|
|
-
|
|
|
- if (negative) {
|
|
|
- number = -number;
|
|
|
- negative = 0;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- if (c == 'e' || c == 'E') {
|
|
|
- c = scan_getc(sc);
|
|
|
- switch(c) {
|
|
|
- case '-': negative = 1;
|
|
|
- case '+': c = scan_getc(sc);
|
|
|
- }
|
|
|
-
|
|
|
- num_digits = 0;
|
|
|
- exponent_temp = 0;
|
|
|
- while (__isdigit(c)) {
|
|
|
- if (exponent_temp < MAX_ALLOWED_EXP) {
|
|
|
- exponent_temp = exponent_temp * 10 + (c - '0');
|
|
|
+
|
|
|
+
|
|
|
+ * grouping is done before 0-padding. Should we
|
|
|
+ * strip leading 0's first? Or add a 0 flag? */
|
|
|
+
|
|
|
+
|
|
|
+ * either EOF or a char. */
|
|
|
+
|
|
|
+ if (!__isdigit_char_or_EOF(sc->cc)) {
|
|
|
+#ifdef __UCLIBC_HAS_FLOATS__
|
|
|
+ if (psfs->conv_num > CONV_i) {
|
|
|
+ goto NO_STARTING_DIGIT;
|
|
|
}
|
|
|
- c = scan_getc(sc);
|
|
|
- ++num_digits;
|
|
|
+#endif
|
|
|
+ goto DONE_DO_UNGET;
|
|
|
}
|
|
|
|
|
|
- if (num_digits == 0) {
|
|
|
- goto FAIL;
|
|
|
- }
|
|
|
- if (negative) {
|
|
|
- exponent_power -= exponent_temp;
|
|
|
- } else {
|
|
|
- exponent_power += exponent_temp;
|
|
|
+ if (sc->cc == '0') {
|
|
|
+ seendigit = 1;
|
|
|
+ *b++ = '0';
|
|
|
+#ifdef __UCLIBC_MJN3_ONLY__
|
|
|
+#warning CONSIDER: Should leading 0s be skipped before digit grouping? (printf 0 pad)
|
|
|
+#endif
|
|
|
+#if 0
|
|
|
+ do {
|
|
|
+ __scan_getc(sc);
|
|
|
+ } while (sc->cc == '0');
|
|
|
+#endif
|
|
|
}
|
|
|
- }
|
|
|
+ pass = 0;
|
|
|
+ lastblock = 0;
|
|
|
+ do {
|
|
|
+ i = 0;
|
|
|
+ while (__isdigit_char_or_EOF(sc->cc)) {
|
|
|
+ seendigit = 1;
|
|
|
+ if (i == nbmax) {
|
|
|
+#ifdef __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__
|
|
|
+ if (!pass) {
|
|
|
+ if (nonzero) {
|
|
|
+ goto DO_NO_GROUP;
|
|
|
+ }
|
|
|
+ goto DO_TRIM_LEADING_ZEROS;
|
|
|
+ }
|
|
|
+#endif
|
|
|
+ if (nbmax > nblk1) {
|
|
|
+ goto DONE_DO_UNGET;
|
|
|
+ }
|
|
|
+ goto DONE_GROUPING_DO_UNGET;
|
|
|
+ }
|
|
|
+ ++i;
|
|
|
+
|
|
|
+ if (nonzero || (sc->cc != '0')) {
|
|
|
+ if (b < buf + MAX_DIGITS) {
|
|
|
+ *b++ = sc->cc;
|
|
|
+ nonzero = 1;
|
|
|
+#ifdef __UCLIBC_HAS_FLOATS__
|
|
|
+ } else {
|
|
|
+ ++exp_adjust;
|
|
|
+#endif
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ __scan_getc(sc);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (i) {
|
|
|
+ if ((i == nblk2) || ((i < nblk2) && !pass)) {
|
|
|
+
|
|
|
+ p = sc->thousands_sep;
|
|
|
+ if (*p == sc->cc) {
|
|
|
+
|
|
|
+
|
|
|
+ * unless EOF (yuk) */
|
|
|
+ __scan_getc(sc);
|
|
|
+ MBG_LOOP:
|
|
|
+ if (!*++p) {
|
|
|
+ lastblock = i;
|
|
|
+ ++pass;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (*p == sc->cc) {
|
|
|
+ __scan_getc(sc);
|
|
|
+ goto MBG_LOOP;
|
|
|
+ }
|
|
|
+
|
|
|
+ __scan_ungetc(sc);
|
|
|
+ if ((sc->cc >= 0) || (p > sc->thousands_sep + 1)) {
|
|
|
+#ifdef __UCLIBC_HAS_FLOATS__
|
|
|
+
|
|
|
+ * we've read too much to recover. But if
|
|
|
+ * this is a floating point conversion and
|
|
|
+ * the initial portion of the decpt mb char
|
|
|
+ * matches, then we may still be able to
|
|
|
+ * recover. */
|
|
|
+ int k = p - sc->thousands_sep - 1;
|
|
|
+
|
|
|
+ if ((psfs->conv_num > CONV_i)
|
|
|
+ && (!pass || (i == nblk1))
|
|
|
+ && !memcmp(sc->thousands_sep, sc->fake_decpt, k)
|
|
|
+
|
|
|
+ ) {
|
|
|
+ __scan_getc(sc);
|
|
|
+ p = sc->fake_decpt + k;
|
|
|
+ do {
|
|
|
+ if (!*++p) {
|
|
|
+ strcpy(b, sc->decpt);
|
|
|
+ b += sc->decpt_len;
|
|
|
+ goto GOT_DECPT;
|
|
|
+ }
|
|
|
+ if (*p != sc->cc) {
|
|
|
+ __scan_ungetc(sc);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ __scan_getc(sc);
|
|
|
+ } while (1);
|
|
|
+ }
|
|
|
+#endif
|
|
|
+ goto DONE;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ((i == nblk1) || ((i < nblk1) && !pass)) {
|
|
|
+
|
|
|
+ goto DONE_GROUPING_DO_UNGET;
|
|
|
+ }
|
|
|
+ if (i > nblk1) {
|
|
|
+
|
|
|
+ if ((i - nblk1) <= (sc->ungot_flag ^ 1)) {
|
|
|
+ assert(sc->cc < 0);
|
|
|
+ --b;
|
|
|
+ goto DO_RECOVER_GROUP;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * So we have an unrecoverable situation. */
|
|
|
+ goto DONE_DO_UNGET;
|
|
|
+ }
|
|
|
+
|
|
|
+ assert(pass);
|
|
|
+
|
|
|
+
|
|
|
+ if ((pass == 1) || (nblk1 == nblk2)) {
|
|
|
+ if (!i && (sc->tslen == 1) && (sc->cc < 0)) {
|
|
|
+
|
|
|
+ DO_RECOVER_GROUP:
|
|
|
+ if (sc->ungot_flag & 2) {
|
|
|
+ __scan_ungetc(sc);
|
|
|
+ }
|
|
|
+ goto DONE_GROUPING_DO_UNGET;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ goto DONE_DO_UNGET;
|
|
|
+ } while (1);
|
|
|
+
|
|
|
+ assert(0);
|
|
|
+ }
|
|
|
+
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+#ifdef __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__
|
|
|
+ DO_TRIM_LEADING_ZEROS:
|
|
|
+#endif
|
|
|
+ if (sc->cc == '0') {
|
|
|
+ seendigit = 1;
|
|
|
+ *b++ = '0';
|
|
|
+ do {
|
|
|
+ __scan_getc(sc);
|
|
|
+ } while (sc->cc == '0');
|
|
|
+ }
|
|
|
+
|
|
|
+#ifdef __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__
|
|
|
+ DO_NO_GROUP:
|
|
|
+#endif
|
|
|
+
|
|
|
|
|
|
- if (number != 0.) {
|
|
|
-
|
|
|
- exponent_temp = exponent_power;
|
|
|
- p10 = 10.;
|
|
|
+#define valid_digit(cc,base) (isxdigit(cc) && ((base == 16) || (cc - '0' < base)))
|
|
|
|
|
|
- if (exponent_temp < 0) {
|
|
|
- exponent_temp = -exponent_temp;
|
|
|
+ while (valid_digit(sc->cc,base)) {
|
|
|
+ if (b - buf < MAX_DIGITS) {
|
|
|
+ nonzero = seendigit = 1;
|
|
|
+ *b++ = sc->cc;
|
|
|
+#ifdef __UCLIBC_HAS_FLOATS__
|
|
|
+ } else {
|
|
|
+ ++exp_adjust;
|
|
|
+#endif
|
|
|
}
|
|
|
+ __scan_getc(sc);
|
|
|
+ }
|
|
|
|
|
|
- while (exponent_temp) {
|
|
|
- if (exponent_temp & 1) {
|
|
|
- if (exponent_power < 0) {
|
|
|
- number /= p10;
|
|
|
- } else {
|
|
|
- number *= p10;
|
|
|
+#ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
|
|
|
+ DONE_GROUPING_DO_UNGET:
|
|
|
+#endif
|
|
|
+ if (psfs->conv_num <= CONV_i) {
|
|
|
+ __scan_ungetc(sc);
|
|
|
+ *b = 0;
|
|
|
+ if (!seendigit) {
|
|
|
+ goto DONE;
|
|
|
+ }
|
|
|
+ if (psfs->store) {
|
|
|
+ if (*buf == '-') {
|
|
|
+ usflag = 0;
|
|
|
+ }
|
|
|
+ ++psfs->cnt;
|
|
|
+ _store_inttype(psfs->cur_ptr, psfs->dataargtype,
|
|
|
+ (uintmax_t) STRTOUIM(buf, NULL, base, 1-usflag));
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+#ifdef __UCLIBC_HAS_FLOATS__
|
|
|
+
|
|
|
+
|
|
|
+#ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
|
|
|
+ NO_STARTING_DIGIT:
|
|
|
+#endif
|
|
|
+ p = sc->fake_decpt;
|
|
|
+ do {
|
|
|
+ if (!*p) {
|
|
|
+ strcpy(b, sc->decpt);
|
|
|
+ b += sc->decpt_len;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (*p != sc->cc) {
|
|
|
+ if (p > sc->fake_decpt) {
|
|
|
+ if ((sc->cc >= 0) && (p > sc->fake_decpt + 1)) {
|
|
|
+ goto DONE_DO_UNGET;
|
|
|
}
|
|
|
+
|
|
|
+ __scan_ungetc(sc);
|
|
|
+
|
|
|
}
|
|
|
- exponent_temp >>= 1;
|
|
|
- p10 *= p10;
|
|
|
+ goto DO_DIGIT_CHECK;
|
|
|
+ }
|
|
|
+ ++p;
|
|
|
+ __scan_getc(sc);
|
|
|
+ } while (1);
|
|
|
+
|
|
|
+#ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
|
|
|
+ GOT_DECPT:
|
|
|
+#endif
|
|
|
+ if (!nonzero) {
|
|
|
+ if (sc->cc == '0') {
|
|
|
+ assert(exp_adjust == 0);
|
|
|
+ *b++ = '0';
|
|
|
+ ++exp_adjust;
|
|
|
+ seendigit = 1;
|
|
|
+ do {
|
|
|
+ --exp_adjust;
|
|
|
+ __scan_getc(sc);
|
|
|
+ } while (sc->cc == '0');
|
|
|
}
|
|
|
}
|
|
|
- *ld = number;
|
|
|
- return 1;
|
|
|
|
|
|
- FAIL:
|
|
|
- scan_ungetc(sc);
|
|
|
- return 0;
|
|
|
-}
|
|
|
+ while (valid_digit(sc->cc,base)) {
|
|
|
+ if (b - buf < MAX_DIGITS) {
|
|
|
+ seendigit = 1;
|
|
|
+ *b++ = sc->cc;
|
|
|
+ }
|
|
|
+ __scan_getc(sc);
|
|
|
+ }
|
|
|
+
|
|
|
+ DO_DIGIT_CHECK:
|
|
|
+
|
|
|
+ if (!seendigit) {
|
|
|
+ static const unsigned char nan_inf_str[] = "an\0nfinity";
|
|
|
+
|
|
|
+ if (base == 16) {
|
|
|
+ goto DONE_DO_UNGET;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+#undef TOLOWER
|
|
|
+#define TOLOWER(C) ((C)|0x20)
|
|
|
+
|
|
|
+ switch (TOLOWER(sc->cc)) {
|
|
|
+ case 'i':
|
|
|
+ p = nan_inf_str + 3;
|
|
|
+ break;
|
|
|
+ case 'n':
|
|
|
+ p = nan_inf_str;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+
|
|
|
+ goto DONE_DO_UNGET;
|
|
|
+ }
|
|
|
+
|
|
|
+ *b++ = sc->cc;
|
|
|
+
|
|
|
+ do {
|
|
|
+ __scan_getc(sc);
|
|
|
+ if (TOLOWER(sc->cc) == *p) {
|
|
|
+ *b++ = sc->cc;
|
|
|
+ ++p;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (!*p || (p == nan_inf_str + 5)) {
|
|
|
+ goto GOT_FLOAT;
|
|
|
+ }
|
|
|
+
|
|
|
+ goto DONE_DO_UNGET;
|
|
|
+ } while (1);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if (
|
|
|
+#ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
|
|
|
+ ((base == 16) && (((sc->cc)|0x20) == 'p')) ||
|
|
|
+#endif
|
|
|
+ (((sc->cc)|0x20) == 'e')
|
|
|
+ ) {
|
|
|
+ *b++ = sc->cc;
|
|
|
+
|
|
|
+ __scan_getc(sc);
|
|
|
+ if (sc->cc < 0) {
|
|
|
+ --b;
|
|
|
+ goto GOT_FLOAT;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ((sc->cc == '+') || (sc->cc == '-')) {
|
|
|
+ *b++ = sc->cc;
|
|
|
+ __scan_getc(sc);
|
|
|
+ }
|
|
|
+
|
|
|
+#warning fix MAX_EXP_DIGITS!
|
|
|
+#define MAX_EXP_DIGITS 20
|
|
|
+ assert(seendigit);
|
|
|
+ seendigit = 0;
|
|
|
+ nonzero = 0;
|
|
|
+
|
|
|
+ if (sc->cc == '0') {
|
|
|
+ seendigit = 1;
|
|
|
+ *b++ = '0';
|
|
|
+ do {
|
|
|
+ __scan_getc(sc);
|
|
|
+ } while (sc->cc == '0');
|
|
|
+ }
|
|
|
+
|
|
|
+ while (__isdigit_char_or_EOF(sc->cc)) {
|
|
|
+ if (seendigit < MAX_EXP_DIGITS) {
|
|
|
+ ++seendigit;
|
|
|
+ *b++ = sc->cc;
|
|
|
+ }
|
|
|
+ __scan_getc(sc);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!seendigit) {
|
|
|
+ goto DONE_DO_UNGET;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ GOT_FLOAT:
|
|
|
+
|
|
|
+ *b = 0;
|
|
|
+ {
|
|
|
+ __fpmax_t x;
|
|
|
+ char *e;
|
|
|
+ x = __strtofpmax(buf, &e, exp_adjust);
|
|
|
+ assert(!*e);
|
|
|
+ if (psfs->store) {
|
|
|
+ if (psfs->dataargtype & PA_FLAG_LONG_LONG) {
|
|
|
+ *((long double *)psfs->cur_ptr) = (long double) x;
|
|
|
+ } else if (psfs->dataargtype & PA_FLAG_LONG) {
|
|
|
+ *((double *)psfs->cur_ptr) = (double) x;
|
|
|
+ } else {
|
|
|
+ *((float *)psfs->cur_ptr) = (float) x;
|
|
|
+ }
|
|
|
+ ++psfs->cnt;
|
|
|
+ }
|
|
|
+ __scan_ungetc(sc);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
#endif
|
|
|
+
|
|
|
+ DONE_DO_UNGET:
|
|
|
+ __scan_ungetc(sc);
|
|
|
+ DONE:
|
|
|
+ return -1;
|
|
|
+
|
|
|
+}
|
|
|
#endif
|
|
|
+
|