1 /* Copyright (C) 2002-2004 Manuel Novoa III
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Library General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Library General Public License for more details.
13 * You should have received a copy of the GNU Library General Public
14 * License along with this library; if not, write to the Free
15 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 * New *scanf implementation with lots of bug fixes and *wscanf support.
20 * Also now optionally supports hexadecimal float notation, positional
21 * args, and glibc locale-specific digit grouping. Should now be
22 * standards compliant.
25 * Bug fix: scanf %lc,%ls,%l[ would always set mb_fail on eof or error,
26 * even when just starting a new mb char.
27 * Bug fix: wscanf would incorrectly unget in certain situations.
30 * Bug fix: store flag wasn't respected if no positional args.
31 * Implement vs{n}scanf for the non-buffered stdio no-wchar case.
34 * Bug fix: Fix a problem reported by Atsushi Nemoto <anemo@mba.ocn.ne.jp>
35 * for environments where long and long long are the same.
38 * Ugh... EOF handling by scanf was completely broken. :-( Regretably,
39 * I got my mind fixed in one mode and didn't comply with the standards.
40 * Things should be fixed now, but comparision testing is difficult when
41 * glibc's scanf is broken and they stubbornly refuse to even acknowledge
42 * that it is... even when confronted by specific examples from the C99
43 * standards and from an official C standard defect report.
47 #define _ISOC99_SOURCE /* for LLONG_MAX primarily... */
60 #ifdef __UCLIBC_HAS_WCHAR__
61 #include <bits/uClibc_uwchar.h>
64 #endif /* __UCLIBC_HAS_WCHAR__ */
72 #ifdef __UCLIBC_HAS_THREADS__
73 #include <stdio_ext.h>
75 #endif /* __UCLIBC_HAS_THREADS__ */
77 #ifdef __UCLIBC_HAS_FLOATS__
79 #include <bits/uClibc_fpmax.h>
80 #endif /* __UCLIBC_HAS_FLOATS__ */
82 #ifdef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
84 /* only emit this once */
85 #warning Forcing undef of __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__ until implemented!
87 #undef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
90 #undef __STDIO_HAS_VSSCANF
91 #if defined(__STDIO_BUFFERS) || !defined(__UCLIBC_HAS_WCHAR__) || defined(__UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__)
92 #define __STDIO_HAS_VSSCANF 1
94 #if !defined(__STDIO_BUFFERS) && !defined(__UCLIBC_HAS_WCHAR__)
97 unsigned char *bufread; /* pointer to 1 past end of buffer */
98 unsigned char *bufpos;
104 extern void _store_inttype(void *dest, int desttype, uintmax_t val);
106 #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
108 extern unsigned long long
109 _stdlib_strto_ll(register const char * __restrict str,
110 char ** __restrict endptr, int base, int sflag);
111 #if (ULLONG_MAX == UINTMAX_MAX)
112 #define STRTOUIM(s,e,b,sf) _stdlib_strto_ll(s,e,b,sf)
115 #else /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
118 _stdlib_strto_l(register const char * __restrict str,
119 char ** __restrict endptr, int base, int sflag);
121 #if (ULONG_MAX == UINTMAX_MAX)
122 #define STRTOUIM(s,e,b,sf) _stdlib_strto_l(s,e,b,sf)
125 #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
128 #error STRTOUIM conversion function is undefined!
131 /**********************************************************************/
133 /* The standards require EOF < 0. */
135 #define __isdigit_char_or_EOF(C) __isdigit_char((C))
137 #define __isdigit_char_or_EOF(C) __isdigit_int((C))
140 /**********************************************************************/
143 int fscanf(FILE * __restrict stream, const char * __restrict format, ...)
148 va_start(arg, format);
149 rv = vfscanf(stream, format, arg);
156 /**********************************************************************/
159 int scanf(const char * __restrict format, ...)
164 va_start(arg, format);
165 rv = vfscanf(stdin, format, arg);
172 /**********************************************************************/
175 #ifdef __STDIO_HAS_VSSCANF
177 int sscanf(const char * __restrict str, const char * __restrict format, ...)
182 va_start(arg, format);
183 rv = vsscanf(str, format, arg);
189 #else /* __STDIO_HAS_VSSCANF */
190 #warning Skipping sscanf since no vsscanf!
191 #endif /* __STDIO_HAS_VSSCANF */
194 /**********************************************************************/
197 int vscanf(const char * __restrict format, va_list arg)
199 return vfscanf(stdin, format, arg);
203 /**********************************************************************/
206 #ifdef __UCLIBC_MJN3_ONLY__
207 #warning WISHLIST: Implement vsscanf for non-buf and no custom stream case.
208 #endif /* __UCLIBC_MJN3_ONLY__ */
210 #ifdef __STDIO_BUFFERS
212 int vsscanf(__const char *sp, __const char *fmt, va_list ap)
216 /* __STDIO_STREAM_RESET_GCS(&f); */
217 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
218 f.__cookie = &(f.__filedes);
220 f.__gcs.write = NULL;
222 f.__gcs.close = NULL;
225 f.__filedes = __STDIO_STREAM_FAKE_VSSCANF_FILEDES;
226 f.__modeflags = (__FLAG_NARROW|__FLAG_READONLY|__FLAG_READING);
228 #ifdef __UCLIBC_HAS_WCHAR__
229 f.__ungot_width[0] = 0;
231 #ifdef __STDIO_MBSTATE
232 __INIT_MBSTATE(&(f.__state));
235 #ifdef __UCLIBC_HAS_THREADS__
236 f.__user_locking = 1; /* Set user locking. */
237 __stdio_init_mutex(&f.__lock);
241 /* Set these last since __bufgetc initialization depends on
242 * __user_locking and only gets set if user locking is on. */
244 f.__bufpos = (unsigned char *) ((void *) sp);
246 f.__bufend = f.__bufstart + strlen(sp);
247 __STDIO_STREAM_ENABLE_GETC(&f);
248 __STDIO_STREAM_DISABLE_PUTC(&f);
250 return vfscanf(&f, fmt, ap);
253 #elif !defined(__UCLIBC_HAS_WCHAR__)
255 int vsscanf(__const char *sp, __const char *fmt, va_list ap)
259 f.bufpos = (unsigned char *) ((void *) sp);
260 f.bufread = f.bufpos + strlen(sp);
262 /* __STDIO_STREAM_RESET_GCS(&f.f); */
263 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
264 f.f.__cookie = &(f.f.__filedes);
265 f.f.__gcs.read = NULL;
266 f.f.__gcs.write = NULL;
267 f.f.__gcs.seek = NULL;
268 f.f.__gcs.close = NULL;
271 f.f.__filedes = __STDIO_STREAM_FAKE_VSSCANF_FILEDES_NB;
272 f.f.__modeflags = (__FLAG_NARROW|__FLAG_READONLY|__FLAG_READING);
274 /* #ifdef __UCLIBC_HAS_WCHAR__ */
275 /* f.f.__ungot_width[0] = 0; */
277 #ifdef __STDIO_MBSTATE
278 #error __STDIO_MBSTATE is defined!
279 /* __INIT_MBSTATE(&(f.f.__state)); */
282 #ifdef __UCLIBC_HAS_THREADS__
283 f.f.__user_locking = 1; /* Set user locking. */
284 __stdio_init_mutex(&f.f.__lock);
286 f.f.__nextopen = NULL;
288 return vfscanf(&f.f, fmt, ap);
291 #elif defined(__UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__)
293 int vsscanf(__const char *sp, __const char *fmt, va_list ap)
298 if ((f = fmemopen((char *)sp, strlen(sp), "r")) != NULL) {
299 rv = vfscanf(f, fmt, ap);
307 #warning Skipping vsscanf since no buffering, no custom streams, and wchar enabled!
308 #ifdef __STDIO_HAS_VSSCANF
309 #error WHOA! __STDIO_HAS_VSSCANF is defined!
314 /**********************************************************************/
317 int fwscanf(FILE * __restrict stream, const wchar_t * __restrict format, ...)
322 va_start(arg, format);
323 rv = vfwscanf(stream, format, arg);
330 /**********************************************************************/
333 int wscanf(const wchar_t * __restrict format, ...)
338 va_start(arg, format);
339 rv = vfwscanf(stdin, format, arg);
346 /**********************************************************************/
349 #ifdef __STDIO_BUFFERS
351 int swscanf(const wchar_t * __restrict str, const wchar_t * __restrict format,
357 va_start(arg, format);
358 rv = vswscanf(str, format, arg);
363 #else /* __STDIO_BUFFERS */
364 #warning Skipping swscanf since no buffering!
365 #endif /* __STDIO_BUFFERS */
368 /**********************************************************************/
371 int vwscanf(const wchar_t * __restrict format, va_list arg)
373 return vfwscanf(stdin, format, arg);
377 /**********************************************************************/
380 #ifdef __STDIO_BUFFERS
382 int vswscanf(const wchar_t * __restrict str, const wchar_t * __restrict format,
388 f.__bufpos = (char *) str;
390 f.__bufend = (char *)(str + wcslen(str));
391 __STDIO_STREAM_DISABLE_GETC(&f);
392 __STDIO_STREAM_DISABLE_PUTC(&f);
394 /* __STDIO_STREAM_RESET_GCS(&f); */
395 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
396 f.__cookie = &(f.__filedes);
398 f.__gcs.write = NULL;
400 f.__gcs.close = NULL;
403 f.__filedes = __STDIO_STREAM_FAKE_VSWSCANF_FILEDES;
404 f.__modeflags = (__FLAG_WIDE|__FLAG_READONLY|__FLAG_READING);
406 #ifdef __UCLIBC_HAS_WCHAR__
407 f.__ungot_width[0] = 0;
408 #endif /* __UCLIBC_HAS_WCHAR__ */
409 #ifdef __STDIO_MBSTATE
410 __INIT_MBSTATE(&(f.__state));
411 #endif /* __STDIO_MBSTATE */
413 #ifdef __UCLIBC_HAS_THREADS__
414 f.__user_locking = 1; /* Set user locking. */
415 __stdio_init_mutex(&f.__lock);
419 return vfwscanf(&f, format, arg);
421 #else /* __STDIO_BUFFERS */
422 #warning Skipping vswscanf since no buffering!
423 #endif /* __STDIO_BUFFERS */
426 /**********************************************************************/
427 /**********************************************************************/
431 /* float layout 0123456789012345678901 repeat n for "l[" */
432 #define SPEC_CHARS "npxXoudifFeEgGaACSncs["
433 /* npxXoudif eEgG CS cs[ */
435 /* NOTE: Ordering is important! In particular, CONV_LEFTBRACKET
436 * must immediately precede CONV_c. */
441 CONV_x, CONV_X, CONV_o, CONV_u, CONV_d, CONV_i,
442 CONV_f, CONV_F, CONV_e, CONV_E, CONV_g, CONV_G, CONV_a, CONV_A,
443 CONV_C, CONV_S, CONV_LEFTBRACKET, CONV_c, CONV_s, CONV_leftbracket,
444 CONV_percent, CONV_whitespace /* not in SPEC_* and no flags */
447 #ifdef __UCLIBC_HAS_FLOATS__
448 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
449 /* p x X o u d i f F e E g G a A */
450 #define SPEC_BASE { 16, 16, 16, 8, 10, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
452 /* p x X o u d i f F e E g G a A */
453 #define SPEC_BASE { 16, 16, 16, 8, 10, 10, 0, 10, 10, 10, 10, 10, 10, 10, 10 }
455 #else /* __UCLIBC_HAS_FLOATS__ */
456 /* p x X o u d i f F e E g G a A */
457 #define SPEC_BASE { 16, 16, 16, 8, 10, 10, 0 }
458 #endif /* __UCLIBC_HAS_FLOATS__ */
460 #ifdef __UCLIBC_MJN3_ONLY__
463 #warning CONSIDER: Add a '0' flag to eat 0 padding when grouping?
465 #endif /* __UCLIBC_MJN3_ONLY__ */
467 #define SPEC_FLAGS "*'I"
470 FLAG_SURPRESS = 0x10, /* MUST BE 1ST!! See DO_FLAGS. */
471 FLAG_THOUSANDS = 0x20,
472 FLAG_I18N = 0x40, /* only works for d, i, u */
473 FLAG_MALLOC = 0x80, /* only works for s, S, and [ (and l[)*/
477 #define SPEC_RANGES { CONV_n, CONV_p, CONV_i, CONV_A, \
478 CONV_C, CONV_LEFTBRACKET, \
479 CONV_c, CONV_leftbracket }
481 /* Note: We treat L and ll as synonymous... for ints and floats. */
483 #define SPEC_ALLOWED_FLAGS { \
484 /* n */ (0x0f|FLAG_SURPRESS), \
485 /* p */ ( 0|FLAG_SURPRESS), \
486 /* oxXudi */ (0x0f|FLAG_SURPRESS|FLAG_THOUSANDS|FLAG_I18N), \
487 /* fFeEgGaA */ (0x0c|FLAG_SURPRESS|FLAG_THOUSANDS|FLAG_I18N), \
488 /* C */ ( 0|FLAG_SURPRESS), \
489 /* S and l[ */ ( 0|FLAG_SURPRESS|FLAG_MALLOC), \
490 /* c */ (0x04|FLAG_SURPRESS), \
491 /* s and [ */ (0x04|FLAG_SURPRESS|FLAG_MALLOC), \
495 /**********************************************************************/
497 * In order to ease translation to what arginfo and _print_info._flags expect,
498 * we map: 0:int 1:char 2:longlong 4:long 8:short
499 * and then _flags |= (((q << 7) + q) & 0x701) and argtype |= (_flags & 0x701)
502 /* TODO -- Fix the table below to take into account stdint.h. */
503 /* #ifndef LLONG_MAX */
504 /* #error fix QUAL_CHARS for no long long! Affects 'L', 'j', 'q', 'll'. */
506 /* #if LLONG_MAX != INTMAX_MAX */
507 /* #error fix QUAL_CHARS intmax_t entry 'j'! */
512 #error PDS already defined!
515 #error SS already defined!
518 #error IMS already defined!
521 #if PTRDIFF_MAX == INT_MAX
523 #elif PTRDIFF_MAX == LONG_MAX
525 #elif defined(LLONG_MAX) && (PTRDIFF_MAX == LLONG_MAX)
528 #error fix QUAL_CHARS ptrdiff_t entry 't'!
531 #if SIZE_MAX == UINT_MAX
533 #elif SIZE_MAX == ULONG_MAX
535 #elif defined(LLONG_MAX) && (SIZE_MAX == ULLONG_MAX)
538 #error fix QUAL_CHARS size_t entries 'z', 'Z'!
541 #if INTMAX_MAX == INT_MAX
543 #elif INTMAX_MAX == LONG_MAX
545 #elif defined(LLONG_MAX) && (INTMAX_MAX == LLONG_MAX)
548 #error fix QUAL_CHARS ptrdiff_t entry 't'!
551 #define QUAL_CHARS { \
552 /* j:(u)intmax_t z:(s)size_t t:ptrdiff_t \0:int q:long_long */ \
553 'h', 'l', 'L', 'j', 'z', 't', 'q', 0, \
554 2, 4, 8, IMS, SS, PDS, 8, 0, /* TODO -- fix!!! */\
558 /**********************************************************************/
562 #error Unfortunately, we currently need wint_t to be able to store EOF. Sorry.
566 #define Wchar wchar_t
567 #define Wuchar __uwchar_t
568 #define ISSPACE(C) iswspace((C))
569 #define VFSCANF vfwscanf
570 #define GETC(SC) (SC)->sc_getc((SC))
572 typedef unsigned char __uchar_t;
576 #define Wuchar __uchar_t
577 #define ISSPACE(C) isspace((C))
578 #define VFSCANF vfscanf
579 #ifdef __UCLIBC_HAS_WCHAR__
580 #define GETC(SC) (SC)->sc_getc((SC))
581 #else /* __UCLIBC_HAS_WCHAR__ */
582 #define GETC(SC) getc_unlocked((SC)->fp)
583 #endif /* __UCLIBC_HAS_WCHAR__ */
593 #ifdef __UCLIBC_HAS_WCHAR__
594 wchar_t app_ungot; /* Match FILE struct member type. */
595 unsigned char ungot_wchar_width;
596 #else /* __UCLIBC_HAS_WCHAR__ */
597 unsigned char app_ungot; /* Match FILE struct member type. */
598 #endif /* __UCLIBC_HAS_WCHAR__ */
602 #ifdef __UCLIBC_HAS_WCHAR__
603 char ungot_wflag; /* vfwscanf */
604 char mb_fail; /* vfscanf */
605 mbstate_t mbstate; /* vfscanf */
607 wint_t ungot_wchar; /* to support __scan_getc */
608 int (*sc_getc)(struct scan_cookie *);
609 #endif /* __UCLIBC_HAS_WCHAR__ */
611 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
612 const char *grouping;
613 const unsigned char *thousands_sep;
615 #ifdef __UCLIBC_HAS_WCHAR__
616 wchar_t thousands_sep_wc;
617 #endif /* __UCLIBC_HAS_WCHAR__ */
618 #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
620 #ifdef __UCLIBC_HAS_FLOATS__
621 const unsigned char *decpt;
623 #ifdef __UCLIBC_HAS_WCHAR__
625 #endif /* __UCLIBC_HAS_WCHAR__ */
626 const unsigned char *fake_decpt;
627 #endif /* __UCLIBC_HAS_FLOATS__ */
632 #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
634 #warning NL_ARGMAX > 10, and space is allocated on the stack for positional args.
636 void *pos_args[NL_ARGMAX];
637 int num_pos_args; /* Must start at -1. */
639 #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
641 const unsigned char *fmt;
642 int cnt, dataargtype, conv_num, max_width;
643 unsigned char store, flags;
644 } psfs_t; /* parse scanf format state */
647 /**********************************************************************/
648 /**********************************************************************/
650 extern void __init_scan_cookie(register struct scan_cookie *sc,
651 register FILE *fp) attribute_hidden;
652 extern int __scan_getc(register struct scan_cookie *sc) attribute_hidden;
653 extern void __scan_ungetc(register struct scan_cookie *sc) attribute_hidden;
655 #ifdef __UCLIBC_HAS_FLOATS__
656 extern int __scan_strtold(long double *ld, struct scan_cookie *sc);
657 #endif /* __UCLIBC_HAS_FLOATS__ */
659 extern int __psfs_parse_spec(psfs_t *psfs) attribute_hidden;
660 extern int __psfs_do_numeric(psfs_t *psfs, struct scan_cookie *sc) attribute_hidden;
662 /**********************************************************************/
663 #ifdef L___scan_cookie
665 #ifdef __UCLIBC_MJN3_ONLY__
666 #warning TODO: Remove dependence on decpt_str and fake_decpt in stub locale mode.
668 #ifndef __UCLIBC_HAS_LOCALE__
669 static const char decpt_str[] = ".";
672 void attribute_hidden __init_scan_cookie(register struct scan_cookie *sc,
678 sc->app_ungot = ((fp->__modeflags & __FLAG_UNGOT) ? fp->__ungot[1] : 0);
679 #ifdef __UCLIBC_HAS_WCHAR__
680 sc->ungot_wflag = 0; /* vfwscanf */
682 #endif /* __UCLIBC_HAS_WCHAR__ */
684 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
685 if (*(sc->grouping = __UCLIBC_CURLOCALE_DATA.grouping)) {
686 sc->thousands_sep = __UCLIBC_CURLOCALE_DATA.thousands_sep;
687 sc->tslen = __UCLIBC_CURLOCALE_DATA.thousands_sep_len;
688 #ifdef __UCLIBC_HAS_WCHAR__
689 sc->thousands_sep_wc = __UCLIBC_CURLOCALE_DATA.thousands_sep_wc;
690 #endif /* __UCLIBC_HAS_WCHAR__ */
692 #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
694 #ifdef __UCLIBC_HAS_FLOATS__
695 #ifdef __UCLIBC_HAS_LOCALE__
696 sc->decpt = __UCLIBC_CURLOCALE_DATA.decimal_point;
697 sc->decpt_len = __UCLIBC_CURLOCALE_DATA.decimal_point_len;
698 #else /* __UCLIBC_HAS_LOCALE__ */
699 sc->fake_decpt = sc->decpt = decpt_str;
701 #endif /* __UCLIBC_HAS_LOCALE__ */
702 #ifdef __UCLIBC_HAS_WCHAR__
703 #ifdef __UCLIBC_HAS_LOCALE__
704 sc->decpt_wc = __UCLIBC_CURLOCALE_DATA.decimal_point_wc;
708 #endif /* __UCLIBC_HAS_WCHAR__ */
709 #endif /* __UCLIBC_HAS_FLOATS__ */
713 int attribute_hidden __scan_getc(register struct scan_cookie *sc)
717 #ifdef __UCLIBC_HAS_WCHAR__
718 assert(!sc->mb_fail);
719 #endif /* __UCLIBC_HAS_WCHAR__ */
723 if (--sc->width < 0) {
728 if (sc->ungot_flag == 0) {
729 #if !defined(__STDIO_BUFFERS) && !defined(__UCLIBC_HAS_WCHAR__)
730 if (!__STDIO_STREAM_IS_FAKE_VSSCANF_NB(sc->fp)) {
733 __FILE_vsscanf *fv = (__FILE_vsscanf *)(sc->fp);
734 if (fv->bufpos < fv->bufread) {
738 sc->fp->__modeflags |= __FLAG_EOF;
746 if ((c = GETC(sc)) == EOF) {
753 assert(sc->ungot_flag == 1);
758 return sc->cc = sc->ungot_char;
761 void attribute_hidden __scan_ungetc(register struct scan_cookie *sc)
764 if (sc->ungot_flag == 2) { /* last was EOF */
766 sc->cc = sc->ungot_char;
767 } else if (sc->ungot_flag == 0) {
776 /**********************************************************************/
777 #ifdef L___psfs_parse_spec
780 static const unsigned char spec_flags[] = SPEC_FLAGS;
781 #endif /* SPEC_FLAGS */
782 static const unsigned char spec_chars[] = SPEC_CHARS;
783 static const unsigned char qual_chars[] = QUAL_CHARS;
784 static const unsigned char spec_ranges[] = SPEC_RANGES;
785 static const unsigned short spec_allowed[] = SPEC_ALLOWED_FLAGS;
787 int attribute_hidden __psfs_parse_spec(register psfs_t *psfs)
789 const unsigned char *p;
790 const unsigned char *fmt0 = psfs->fmt;
795 #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
796 unsigned char fail = 0;
798 i = 0; /* Do this here to avoid a warning. */
800 if (!__isdigit_char(*psfs->fmt)) { /* Not a positional arg. */
805 /* parse the positional arg (or width) value */
807 if (i <= ((INT_MAX - 9)/10)) {
808 i = (i * 10) + (*psfs->fmt++ - '0');
810 } while (__isdigit_char(*psfs->fmt));
812 if (*psfs->fmt != '$') { /* This is a max field width. */
813 if (psfs->num_pos_args >= 0) { /* Already saw a pos arg! */
817 psfs->num_pos_args = -2;
820 ++psfs->fmt; /* Advance past '$'. */
821 #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
823 #if defined(SPEC_FLAGS) || (defined(NL_ARGMAX) && (NL_ARGMAX > 0))
825 #endif /* defined(SPEC_FLAGS) || (defined(NL_ARGMAX) && (NL_ARGMAX > 0)) */
830 if (*p == *psfs->fmt) {
838 if (psfs->flags & FLAG_SURPRESS) { /* Suppress assignment. */
842 #else /* SPEC_FLAGS */
843 if (*psfs->fmt == '*') { /* Suppress assignment. */
848 #endif /* SPEC_FLAGS */
851 #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
853 /* Must be a non-positional arg */
854 if (psfs->num_pos_args >= 0) { /* Already saw a pos arg! */
857 psfs->num_pos_args = -2;
859 if ((psfs->num_pos_args == -2) || (((unsigned int)(--i)) >= NL_ARGMAX)) {
860 /* Already saw a non-pos arg or (0-based) num too large. */
863 psfs->cur_pos_arg = i;
865 #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
868 for (i = 0 ; __isdigit_char(*psfs->fmt) ; ) {
869 if (i <= ((INT_MAX - 9)/10)) {
870 i = (i * 10) + (*psfs->fmt++ - '0');
875 #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
877 #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
880 if (*psfs->fmt == *p) {
885 if ((p - qual_chars < 2) && (*psfs->fmt == *p)) {
886 p += ((sizeof(qual_chars)-2) / 2);
889 psfs->dataargtype = ((int)(p[(sizeof(qual_chars)-2) / 2])) << 8;
891 #ifdef __UCLIBC_MJN3_ONLY__
892 #warning CONSIDER: Should we validate that psfs->max_width > 0 in __psfs_parse_spec()? It would avoid whitespace consumption...
893 #warning CONSIDER: Should INT_MAX be a valid width (%c/%C)? See __psfs_parse_spec().
894 #endif /* __UCLIBC_MJN3_ONLY__ */
898 if (*psfs->fmt == *p) {
899 int p_m_spec_chars = p - spec_chars;
901 #ifdef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
902 #error implement gnu a flag
904 && ((psfs->fmt[1] == '[') || ((psfs->fmt[1]|0x20) == 's'))
905 ) { /* Assumes ascii for 's' and 'S' test. */
906 psfs->flags |= FLAG_MALLOC;
909 continue; /* The related conversions follow 'a'. */
911 #endif /* __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__ */
913 for (p = spec_ranges; p_m_spec_chars > *p ; ++p) {}
914 if (((psfs->dataargtype >> 8) | psfs->flags)
915 & ~spec_allowed[(int)(p - spec_ranges)]
920 if ((p_m_spec_chars >= CONV_c)
921 && (psfs->dataargtype & PA_FLAG_LONG)) {
922 p_m_spec_chars -= 3; /* lc -> C, ls -> S, l[ -> ?? */
925 psfs->conv_num = p_m_spec_chars;
926 return psfs->fmt - fmt0;
939 /**********************************************************************/
940 #if defined(L_vfscanf) || defined(L_vfwscanf)
942 #ifdef __UCLIBC_HAS_WCHAR__
944 static int sc_getc(register struct scan_cookie *sc)
946 return (getc_unlocked)(sc->fp); /* Disable the macro. */
949 static int scan_getwc(register struct scan_cookie *sc)
956 if (--sc->width < 0) {
961 width = sc->width; /* Preserve width. */
962 sc->width = INT_MAX; /* MB_CUR_MAX can invoke a function. */
964 assert(!sc->mb_fail);
967 while (__scan_getc(sc) >= 0) {
970 r = mbrtowc(wc, b, 1, &sc->mbstate);
971 if (((ssize_t) r) >= 0) { /* Successful completion of a wc. */
974 } else if (r == ((size_t) -2)) {
975 /* Potentially valid but incomplete. */
981 if (r == ((size_t)(-3))) { /* EOF or ERROR on first read */
985 /* If we reach here, either r == ((size_t)-1) and
986 * mbrtowc set errno to EILSEQ, or r == ((size_t)-2)
987 * and stream is in an error state or at EOF with a
988 * partially complete wchar. */
989 __set_errno(EILSEQ); /* In case of incomplete conversion. */
994 sc->width = width; /* Restore width. */
996 return (int)((ssize_t) r);
999 #endif /* L_vfscanf */
1003 /* This gets called by __scan_getc. __scan_getc is called by vfwscanf
1004 * when the next wide char is expected to be valid ascii (digits).
1006 static int sc_getc(register struct scan_cookie *sc)
1010 if (__STDIO_STREAM_IS_FAKE_VSWSCANF(sc->fp)) {
1011 if (sc->fp->__bufpos < sc->fp->__bufend) {
1012 wc = *((wchar_t *)(sc->fp->__bufpos));
1013 sc->fp->__bufpos += sizeof(wchar_t);
1015 sc->fp->__modeflags |= __FLAG_EOF;
1018 } else if ((wc = fgetwc_unlocked(sc->fp)) == WEOF) {
1022 sc->ungot_wflag = 1;
1023 sc->ungot_wchar = wc;
1024 sc->ungot_wchar_width = sc->fp->__ungot_width[0];
1026 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
1027 if (wc == sc->thousands_sep_wc) {
1030 #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
1031 #ifdef __UCLIBC_HAS_FLOATS__
1032 if (wc == sc->decpt_wc) {
1035 #endif /* __UCLIBC_HAS_FLOATS__ */
1036 if (!__isascii(wc)) {
1039 sc->wc = sc->ungot_char = wc;
1044 static int scan_getwc(register struct scan_cookie *sc)
1050 if (--sc->width < 0) {
1051 sc->ungot_flag |= 2;
1055 if (sc->ungot_flag == 0) {
1056 if (__STDIO_STREAM_IS_FAKE_VSWSCANF(sc->fp)) {
1057 if (sc->fp->__bufpos < sc->fp->__bufend) {
1058 wc = *((wchar_t *)(sc->fp->__bufpos));
1059 sc->fp->__bufpos += sizeof(wchar_t);
1061 sc->ungot_flag |= 2;
1064 } else if ((wc = fgetwc_unlocked(sc->fp)) == WEOF) {
1065 sc->ungot_flag |= 2;
1068 sc->ungot_wflag = 1;
1069 sc->ungot_char = wc;
1070 sc->ungot_wchar_width = sc->fp->__ungot_width[0];
1072 assert(sc->ungot_flag == 1);
1077 sc->wc = sc->ungot_char;
1083 #endif /* L_vfwscanf */
1084 #endif /* __UCLIBC_HAS_WCHAR__ */
1086 static __inline void kill_scan_cookie(register struct scan_cookie *sc)
1090 if (sc->ungot_flag & 1) {
1091 #if !defined(__STDIO_BUFFERS) && !defined(__UCLIBC_HAS_WCHAR__)
1092 if (!__STDIO_STREAM_IS_FAKE_VSSCANF_NB(sc->fp)) {
1093 ungetc(sc->ungot_char, sc->fp);
1096 ungetc(sc->ungot_char, sc->fp);
1098 /* Deal with distiction between user and scanf ungots. */
1099 if (sc->nread == 0) { /* Only one char was read... app ungot? */
1100 sc->fp->__ungot[1] = sc->app_ungot; /* restore ungot state. */
1102 sc->fp->__ungot[1] = 0;
1108 if ((sc->ungot_flag & 1) && (sc->ungot_wflag & 1)
1109 && !__STDIO_STREAM_IS_FAKE_VSWSCANF(sc->fp)
1110 && (sc->fp->__state.__mask == 0)
1112 ungetwc(sc->ungot_char, sc->fp);
1113 /* Deal with distiction between user and scanf ungots. */
1114 if (sc->nread == 0) { /* Only one char was read... app ungot? */
1115 sc->fp->__ungot[1] = sc->app_ungot; /* restore ungot state. */
1117 sc->fp->__ungot[1] = 0;
1119 sc->fp->__ungot_width[1] = sc->ungot_wchar_width;
1126 #ifdef __UCLIBC_HAS_FLOATS__
1127 static const char fake_decpt_str[] = ".";
1129 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
1130 static const char fake_thousands_sep_str[] = ",";
1132 #endif /* L_vfwscanf */
1135 int VFSCANF (FILE *__restrict fp, const Wchar *__restrict format, va_list arg)
1144 #endif /* L_vfwscanf */
1146 #ifdef __UCLIBC_HAS_WCHAR__
1148 #endif /* __UCLIBC_HAS_WCHAR__ */
1150 struct scan_cookie sc;
1155 #ifdef __UCLIBC_MJN3_ONLY__
1156 #warning TODO: Fix MAX_DIGITS. We do not do binary, so...!
1158 #define MAX_DIGITS 65 /* Allow one leading 0. */
1159 unsigned char buf[MAX_DIGITS+2];
1161 unsigned char scanset[UCHAR_MAX + 1];
1162 unsigned char invert; /* Careful! Meaning changes. */
1163 #endif /* L_vfscanf */
1165 unsigned char zero_conversions = 1;
1166 __STDIO_AUTO_THREADLOCK_VAR;
1168 #ifdef __UCLIBC_MJN3_ONLY__
1169 #warning TODO: Make checking of the format string in C locale an option.
1171 /* To support old programs, don't check mb validity if in C locale. */
1172 #if defined(__UCLIBC_HAS_LOCALE__) && !defined(L_vfwscanf)
1173 /* ANSI/ISO C99 requires format string to be a valid multibyte string
1174 * beginning and ending in its initial shift state. */
1175 if (((__UCLIBC_CURLOCALE_DATA).encoding) != __ctype_encoding_7_bit) {
1176 const char *p = format;
1177 mbstate.__mask = 0; /* Initialize the mbstate. */
1178 if (mbsrtowcs(NULL, &p, SIZE_MAX, &mbstate) == ((size_t)(-1))) {
1179 __set_errno(EINVAL); /* Format string is invalid. */
1183 #endif /* defined(__UCLIBC_HAS_LOCALE__) && !defined(L_vfwscanf) */
1185 #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
1186 psfs.num_pos_args = -1; /* Must start at -1. */
1187 /* Initialize positional arg ptrs to NULL. */
1188 memset(psfs.pos_args, 0, sizeof(psfs.pos_args));
1189 #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
1191 __STDIO_AUTO_THREADLOCK(fp);
1193 __STDIO_STREAM_VALIDATE(fp);
1195 __init_scan_cookie(&sc,fp);
1196 #ifdef __UCLIBC_HAS_WCHAR__
1197 sc.sc_getc = sc_getc;
1198 sc.ungot_wchar_width = sc.fp->__ungot_width[1];
1202 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
1204 sc.thousands_sep = fake_thousands_sep_str;
1207 #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
1209 #ifdef __UCLIBC_HAS_FLOATS__
1210 sc.fake_decpt = fake_decpt_str;
1211 #endif /* __UCLIBC_HAS_FLOATS__ */
1213 #else /* L_vfwscanf */
1215 #ifdef __UCLIBC_HAS_FLOATS__
1216 sc.fake_decpt = sc.decpt;
1217 #endif /* __UCLIBC_HAS_FLOATS__ */
1219 #endif /* L_vfwscanf */
1221 #endif /* __UCLIBC_HAS_WCHAR__ */
1224 /* Note: If we ever wanted to support non-nice codesets, we
1225 * would really need to do a mb->wc conversion here in the
1226 * vfscanf case. Related changes would have to be made in
1227 * the code that follows... basicly wherever fmt appears. */
1228 for (fmt = (const Wuchar *) format ; *fmt ; /* ++fmt */) {
1233 psfs.cur_ptr = NULL; /* Debugging aid. */
1237 sc.ungot_flag &= 1; /* Clear (possible fake) EOF. */
1238 sc.width = psfs.max_width = INT_MAX;
1240 /* Note: According to the standards, vfscanf does use isspace
1241 * here. So, if we did a mb->wc conversion, we would have to do
1243 * ((((__uwchar_t)wc) < UCHAR_MAX) && isspace(wc))
1244 * because wc might not be in the allowed domain. */
1245 if (ISSPACE(*fmt)) {
1248 } while (ISSPACE(*fmt));
1250 psfs.conv_num = CONV_whitespace;
1254 if (*fmt == '%') { /* Conversion specification. */
1255 if (*++fmt == '%') { /* Remember, '%' eats whitespace too. */
1256 /* Note: The standard says no conversion occurs.
1257 * So do not reset zero_conversions flag. */
1258 psfs.conv_num = CONV_percent;
1265 #else /* L_vfscanf */
1267 const __uwchar_t *wf = fmt;
1270 while (*wf && __isascii(*wf) && (b < buf + sizeof(buf) - 1)) {
1273 #ifdef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
1274 #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.
1275 if ((*b == 'a') && ((*wf == '[') || ((*wf|0x20) == 's'))) {
1276 goto DONE; /* Spec was excessively long. */
1278 #endif /* __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__ */
1280 if (b == buf) { /* Bad conversion specifier! */
1284 #endif /* L_vfscanf */
1285 if ((i = __psfs_parse_spec(&psfs)) < 0) { /* Bad conversion specifier! */
1291 #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
1292 if (psfs.num_pos_args == -2) {
1293 psfs.cur_ptr = va_arg(arg, void *);
1295 while (psfs.cur_pos_arg > psfs.num_pos_args) {
1296 psfs.pos_args[++psfs.num_pos_args] = va_arg(arg, void *);
1298 psfs.cur_ptr = psfs.pos_args[psfs.cur_pos_arg];
1300 #else /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
1301 psfs.cur_ptr = va_arg(arg, void *);
1302 #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
1306 /* First, consume white-space if not n, c, [, C, or l[. */
1307 if ((((1L << CONV_n)|(1L << CONV_C)|(1L << CONV_c)
1308 |(1L << CONV_LEFTBRACKET)|(1L << CONV_leftbracket))
1309 & (1L << psfs.conv_num)) == 0
1312 while ((__scan_getc(&sc) >= 0)
1315 #else /* L_vfscanf */
1317 #endif /* L_vfscanf */
1320 if (psfs.conv_num == CONV_whitespace) {
1325 sc.width = psfs.max_width; /* Now limit the max width. */
1327 if (sc.width == 0) { /* 0 width is forbidden. */
1332 if (psfs.conv_num == CONV_percent) {
1336 if (psfs.conv_num == CONV_n) {
1337 #ifdef __UCLIBC_MJN3_ONLY__
1338 #warning CONSIDER: Should %n count as a conversion as far as EOF return value?
1340 /* zero_conversions = 0; */
1342 _store_inttype(psfs.cur_ptr, psfs.dataargtype,
1343 (uintmax_t) sc.nread);
1348 if (psfs.conv_num <= CONV_A) { /* pointer, integer, or float spec */
1349 int r = __psfs_do_numeric(&psfs, &sc);
1351 if (sc.ungot_wflag == 1) { /* fix up '?', '.', and ',' hacks */
1352 sc.cc = sc.ungot_char = sc.ungot_wchar;
1355 if (r != -1) { /* Either success or a matching failure. */
1356 zero_conversions = 0;
1364 /* Do string conversions here since they are not common code. */
1370 #ifdef __UCLIBC_HAS_WCHAR__
1371 (psfs.conv_num >= CONV_LEFTBRACKET)
1372 #else /* __UCLIBC_HAS_WCHAR__ */
1373 (psfs.conv_num >= CONV_c)
1374 #endif /* __UCLIBC_HAS_WCHAR__ */
1376 b = (psfs.store ? ((unsigned char *) psfs.cur_ptr) : buf);
1380 if (psfs.conv_num == CONV_c) {
1381 if (sc.width == INT_MAX) {
1385 while (__scan_getc(&sc) >= 0) {
1386 zero_conversions = 0;
1391 if (sc.width > 0) { /* Failed to read all required. */
1394 psfs.cnt += psfs.store;
1398 if (psfs.conv_num == CONV_s) {
1399 /* Yes, believe it or not, a %s conversion can store nuls. */
1400 while ((__scan_getc(&sc) >= 0) && !isspace(sc.cc)) {
1401 zero_conversions = 0;
1407 #ifdef __UCLIBC_HAS_WCHAR__
1408 assert((psfs.conv_num == CONV_LEFTBRACKET) || \
1409 (psfs.conv_num == CONV_leftbracket));
1410 #else /* __UCLIBC_HAS_WCHAR__ */
1411 assert((psfs.conv_num == CONV_leftbracket));
1412 #endif /* __UCLIBC_HAS_WCHAR__ */
1416 if (*++fmt == '^') {
1420 memset(scanset, invert, sizeof(scanset));
1424 scanset[(int)(']')] = invert;
1428 while (*fmt != ']') {
1429 if (!*fmt) { /* No closing ']'. */
1432 if ((*fmt == '-') && (fmt[1] != ']')
1433 && (fmt[-1] < fmt[1]) /* sorted? */
1437 /* Note: scanset[i] should already have been done
1438 * in the previous iteration. */
1440 scanset[++i] = invert;
1442 /* Safe to fall through, and a bit smaller. */
1445 scanset[(int) *fmt] = invert;
1449 #ifdef __UCLIBC_HAS_WCHAR__
1450 if (psfs.conv_num == CONV_LEFTBRACKET) {
1451 goto DO_LEFTBRACKET;
1453 #endif /* __UCLIBC_HAS_WCHAR__ */
1456 while (__scan_getc(&sc) >= 0) {
1457 zero_conversions = 0;
1458 if (!scanset[sc.cc]) {
1466 /* Common tail for processing of %s and %[. */
1469 if (fail) { /* nothing stored! */
1472 *b = 0; /* Nul-terminate string. */
1473 psfs.cnt += psfs.store;
1477 #ifdef __UCLIBC_HAS_WCHAR__
1478 DO_LEFTBRACKET: /* Need to do common wide init. */
1479 if (psfs.conv_num >= CONV_C) {
1483 sc.mbstate.__mask = 0;
1485 wb = (psfs.store ? ((wchar_t *) psfs.cur_ptr) : wbuf);
1488 if (psfs.conv_num == CONV_C) {
1489 if (sc.width == INT_MAX) {
1493 while (scan_getwc(&sc) >= 0) {
1494 zero_conversions = 0;
1495 assert(sc.width >= 0);
1501 if (sc.width > 0) { /* Failed to read all required. */
1504 psfs.cnt += psfs.store;
1509 if (psfs.conv_num == CONV_S) {
1510 /* Yes, believe it or not, a %s conversion can store nuls. */
1511 while (scan_getwc(&sc) >= 0) {
1512 zero_conversions = 0;
1513 if ((((__uwchar_t)(sc.wc)) <= UCHAR_MAX) && isspace(sc.wc)) {
1521 assert(psfs.conv_num == CONV_LEFTBRACKET);
1523 while (scan_getwc(&sc) >= 0) {
1524 zero_conversions = 0;
1525 if (((__uwchar_t) sc.wc) <= UCHAR_MAX) {
1526 if (!scanset[sc.wc]) {
1529 } else if (invert) {
1537 /* Common tail for processing of %ls and %l[. */
1540 if (fail || sc.mb_fail) { /* Nothing stored or mb error. */
1543 *wb = 0; /* Nul-terminate string. */
1544 psfs.cnt += psfs.store;
1549 #endif /* __UCLIBC_HAS_WCHAR__ */
1550 #else /* L_vfscanf */
1552 if (psfs.conv_num >= CONV_C) {
1555 if (psfs.conv_num >= CONV_c) {
1556 mbstate.__mask = 0; /* Initialize the mbstate. */
1558 b = (unsigned char *) psfs.cur_ptr;
1562 wb = (wchar_t *) psfs.cur_ptr;
1568 if ((psfs.conv_num == CONV_C) || (psfs.conv_num == CONV_c)) {
1569 if (sc.width == INT_MAX) {
1573 while (scan_getwc(&sc) >= 0) {
1574 zero_conversions = 0;
1575 if (psfs.conv_num == CONV_C) {
1579 i = wcrtomb(b, sc.wc, &mbstate);
1580 if (i < 0) { /* Conversion failure. */
1589 if (sc.width > 0) { /* Failed to read all required. */
1592 psfs.cnt += psfs.store;
1596 if ((psfs.conv_num == CONV_S) || (psfs.conv_num == CONV_s)) {
1597 /* Yes, believe it or not, a %s conversion can store nuls. */
1598 while (scan_getwc(&sc) >= 0) {
1599 zero_conversions = 0;
1600 if (iswspace(sc.wc)) {
1603 if (psfs.conv_num == CONV_S) {
1607 i = wcrtomb(b, sc.wc, &mbstate);
1608 if (i < 0) { /* Conversion failure. */
1620 unsigned char invert = 0;
1622 assert((psfs.conv_num == CONV_LEFTBRACKET)
1623 || (psfs.conv_num == CONV_leftbracket));
1625 if (*++fmt == '^') {
1629 sss = (const wchar_t *) fmt;
1633 while (*fmt != ']') {
1634 if (!*fmt) { /* No closing ']'. */
1637 if ((*fmt == '-') && (fmt[1] != ']')
1638 && (fmt[-1] < fmt[1]) /* sorted? */
1644 /* Ok... a valid scanset spec. */
1646 while (scan_getwc(&sc) >= 0) {
1647 zero_conversions = 0;
1649 do { /* We know sss < fmt. */
1650 if (*ssp == '-') { /* possible range... */
1651 /* Note: We accept a-c-e (ordered) as
1652 * equivalent to a-e. */
1654 if ((++ssp < (const wchar_t *) fmt)
1655 && (ssp[-2] < *ssp) /* sorted? */
1657 if ((sc.wc >= ssp[-2])
1658 && (sc.wc <= *ssp)) {
1661 continue; /* not in range */
1663 --ssp; /* oops... '-' at end, so back up */
1665 /* false alarm... a literal '-' */
1667 if (sc.wc == *ssp) { /* Matched literal char. */
1670 } while (++ssp < (const wchar_t *) fmt);
1672 if ((ssp == (const wchar_t *) fmt) ^ invert) {
1673 /* no match and not inverting
1674 * or match and inverting */
1677 if (psfs.conv_num == CONV_LEFTBRACKET) {
1681 i = wcrtomb(b, sc.wc, &mbstate);
1682 if (i < 0) { /* Conversion failure. */
1692 /* Common tail for processing of %s and %[. */
1695 if (fail) { /* nothing stored! */
1698 *wb = 0; /* Nul-terminate string. */
1700 psfs.cnt += psfs.store;
1704 #endif /* L_vfscanf */
1708 } /* conversion specification */
1711 if (__scan_getc(&sc) != *fmt) {
1714 #endif /* L_vfwscanf */
1721 if (__FERROR_UNLOCKED(fp)) {
1727 if (__FERROR_UNLOCKED(fp) || (*fmt && zero_conversions && __FEOF_UNLOCKED(fp))) {
1728 psfs.cnt = EOF; /* Yes, vfwscanf also returns EOF. */
1731 kill_scan_cookie(&sc);
1733 __STDIO_STREAM_VALIDATE(fp);
1735 __STDIO_AUTO_THREADUNLOCK(fp);
1740 /**********************************************************************/
1741 #ifdef L___psfs_do_numeric
1743 static const unsigned char spec_base[] = SPEC_BASE;
1744 static const unsigned char nil_string[] = "(nil)";
1746 int attribute_hidden __psfs_do_numeric(psfs_t *psfs, struct scan_cookie *sc)
1749 const unsigned char *p;
1751 #ifdef __UCLIBC_HAS_FLOATS__
1754 #ifdef __UCLIBC_MJN3_ONLY__
1755 #warning TODO: Fix MAX_DIGITS. We do not do binary, so...!
1756 #warning TODO: Fix buf!
1758 #define MAX_DIGITS 65 /* Allow one leading 0. */
1759 unsigned char buf[MAX_DIGITS+2+ 100];
1760 unsigned char usflag, base;
1761 unsigned char nonzero = 0;
1762 unsigned char seendigit = 0;
1765 #ifdef __UCLIBC_MJN3_ONLY__
1766 #warning CONSIDER: What should be returned for an invalid conversion specifier?
1768 #ifndef __UCLIBC_HAS_FLOATS__
1769 if (psfs->conv_num > CONV_i) { /* floating point */
1774 base = spec_base[psfs->conv_num - CONV_p];
1775 usflag = (psfs->conv_num <= CONV_u); /* (1)0 if (un)signed */
1779 if (psfs->conv_num == CONV_p) { /* Pointer */
1782 if ((__scan_getc(sc) < 0) || (*p != sc->cc)) {
1784 if (p > nil_string) {
1785 /* We matched at least the '(' so even if we
1786 * are at eof, we can not match a pointer. */
1787 return -2; /* Matching failure */
1791 if (!*++p) { /* Matched (nil), so no unget necessary. */
1794 _store_inttype(psfs->cur_ptr, psfs->dataargtype,
1801 #ifdef __UCLIBC_MJN3_ONLY__
1802 #warning CONSIDER: Should we require a 0x prefix and disallow +/- for pointer %p?
1803 #endif /* __UCLIBC_MJN3_ONLY__ */
1808 return -1; /* Input failure (nothing read yet). */
1811 if ((sc->cc == '+') || (sc->cc == '-')) { /* Handle leading sign.*/
1816 if ((base & 0xef) == 0) { /* 0xef is ~16, so 16 or 0. */
1817 if (sc->cc == '0') { /* Possibly set base and handle prefix. */
1819 if ((sc->cc|0x20) == 'x') { /* Assumes ascii.. x or X. */
1820 if (__scan_getc(sc) < 0) {
1821 /* Either EOF or error (including wc outside char range).
1822 * If EOF or error, this is a matching failure (we read 0x).
1823 * If wc outside char range, this is also a matching failure.
1824 * Hence, we do an unget (although not really necessary here
1826 goto DONE_DO_UNGET; /* matching failure */
1828 base = 16; /* Base 16 for sure now. */
1829 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
1830 /* The prefix is required for hexadecimal floats. */
1833 #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
1834 } else { /* oops... back up */
1836 sc->cc = '0'; /* NASTY HACK! */
1838 base = (base >> 1) + 8; /* 0->8, 16->16. no 'if' */
1839 #ifdef __UCLIBC_HAS_FLOATS__
1840 if (psfs->conv_num > CONV_i) { /* floating point */
1850 /***************** digit grouping **********************/
1851 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
1853 if ((psfs->flags & FLAG_THOUSANDS) && (base == 10)
1854 && *(p = sc->grouping)
1857 int nblk1, nblk2, nbmax, lastblock, pass, i;
1860 #ifdef __UCLIBC_MJN3_ONLY__
1861 #warning CONSIDER: Should we initalize the grouping blocks in __init_scan_cookie()?
1862 #endif /* __UCLIBC_MJN3_ONLY__ */
1863 nbmax = nblk2 = nblk1 = *p;
1866 if (nbmax < nblk2) {
1872 /* Note: for printf, if 0 and \' flags appear then
1873 * grouping is done before 0-padding. Should we
1874 * strip leading 0's first? Or add a 0 flag? */
1876 /* For vfwscanf, sc_getc translates, so the value of sc->cc is
1877 * either EOF or a char. */
1879 if (!__isdigit_char_or_EOF(sc->cc)) { /* No starting digit! */
1880 #ifdef __UCLIBC_HAS_FLOATS__
1881 if (psfs->conv_num > CONV_i) { /* floating point */
1882 goto NO_STARTING_DIGIT;
1888 if (sc->cc == '0') {
1890 *b++ = '0'; /* Store the first 0. */
1891 #ifdef __UCLIBC_MJN3_ONLY__
1892 #warning CONSIDER: Should leading 0s be skipped before digit grouping? (printf 0 pad)
1893 #endif /* __UCLIBC_MJN3_ONLY__ */
1895 do { /* But ignore all subsequent 0s. */
1897 } while (sc->cc == '0');
1904 while (__isdigit_char_or_EOF(sc->cc)) {
1906 if (i == nbmax) { /* too many digits for a block */
1907 #ifdef __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__
1908 if (!pass) { /* treat as nongrouped */
1912 goto DO_TRIM_LEADING_ZEROS;
1915 if (nbmax > nblk1) {
1916 goto DONE_DO_UNGET; /* matching failure */
1918 goto DONE_GROUPING_DO_UNGET; /* nbmax == nblk1 */
1922 if (nonzero || (sc->cc != '0')) {
1923 if (b < buf + MAX_DIGITS) {
1926 #ifdef __UCLIBC_HAS_FLOATS__
1936 if (i) { /* we saw digits digits */
1937 if ((i == nblk2) || ((i < nblk2) && !pass)) {
1938 /* (possible) outer grp */
1939 p = sc->thousands_sep;
1940 if (*p == sc->cc) { /* first byte matches... */
1941 /* so check if grouping mb char */
1942 /* Since 1st matched, either match or fail now
1943 * unless EOF (yuk) */
1946 if (!*++p) { /* is a grouping mb char */
1955 /* bad grouping mb char! */
1957 if ((sc->cc >= 0) || (p > sc->thousands_sep + 1)) {
1958 #ifdef __UCLIBC_HAS_FLOATS__
1959 /* We failed to match a thousep mb char, and
1960 * we've read too much to recover. But if
1961 * this is a floating point conversion and
1962 * the initial portion of the decpt mb char
1963 * matches, then we may still be able to
1965 int k = p - sc->thousands_sep - 1;
1967 if ((psfs->conv_num > CONV_i) /* float conversion */
1968 && (!pass || (i == nblk1)) /* possible last */
1969 && !memcmp(sc->thousands_sep, sc->fake_decpt, k)
1970 /* and prefix matched, so could be decpt */
1973 p = sc->fake_decpt + k;
1976 strcpy(b, sc->decpt);
1987 #endif /* __UCLIBC_HAS_FLOATS__ */
1990 /* was EOF and 1st, so recoverable. */
1993 if ((i == nblk1) || ((i < nblk1) && !pass)) {
1994 /* got an inner group */
1995 goto DONE_GROUPING_DO_UNGET;
1997 goto DONE_DO_UNGET; /* Matching failure. */
2005 assert(0); /* Should never get here. */
2008 #endif /***************** digit grouping **********************/
2010 /* Not grouping so first trim all but one leading 0. */
2011 #ifdef __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__
2012 DO_TRIM_LEADING_ZEROS:
2013 #endif /* __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__ */
2014 if (sc->cc == '0') {
2016 *b++ = '0'; /* Store the first 0. */
2017 do { /* But ignore all subsequent 0s. */
2019 } while (sc->cc == '0');
2022 #ifdef __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__
2024 #endif /* __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__ */
2025 /* At this point, we're ready to start reading digits. */
2027 #define valid_digit(cc,base) (isxdigit(cc) && ((base == 16) || (cc - '0' < base)))
2029 while (valid_digit(sc->cc,base)) { /* Now for significant digits.*/
2030 if (b - buf < MAX_DIGITS) {
2031 nonzero = seendigit = 1; /* Set nonzero too 0s trimmed above. */
2033 #ifdef __UCLIBC_HAS_FLOATS__
2041 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
2042 DONE_GROUPING_DO_UNGET:
2043 #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
2044 if (psfs->conv_num <= CONV_i) { /* integer conversion */
2046 *b = 0; /* null-terminate */
2048 goto DONE; /* No digits! */
2055 _store_inttype(psfs->cur_ptr, psfs->dataargtype,
2056 (uintmax_t) STRTOUIM(buf, NULL, base, 1-usflag));
2061 #ifdef __UCLIBC_HAS_FLOATS__
2063 /* At this point, we have everything left of the decimal point or exponent. */
2064 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
2070 strcpy(b, sc->decpt);
2075 if (p > sc->fake_decpt) {
2076 goto DONE_DO_UNGET; /* matching failure (read some of decpt) */
2078 goto DO_DIGIT_CHECK;
2084 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
2088 if (sc->cc == '0') {
2089 assert(exp_adjust == 0);
2096 } while (sc->cc == '0');
2100 while (valid_digit(sc->cc,base)) { /* Process fractional digits.*/
2101 if (b - buf < MAX_DIGITS) {
2109 /* Hmm... no decimal point. */
2111 static const unsigned char nan_inf_str[] = "an\0nfinity";
2113 if (base == 16) { /* We had a prefix, but no digits! */
2114 goto DONE_DO_UNGET; /* matching failure */
2117 /* Avoid tolower problems for INFINITY in the tr_TR locale. (yuk)*/
2119 #define TOLOWER(C) ((C)|0x20)
2121 switch (TOLOWER(sc->cc)) {
2123 p = nan_inf_str + 3;
2129 /* No digits and not inf or nan. */
2137 if (TOLOWER(sc->cc) == *p) {
2142 if (!*p || (p == nan_inf_str + 5)) { /* match nan/infinity or inf */
2145 /* Unrecoverable. Even if on 1st char, we had no digits. */
2150 /* If we get here, we had some digits. */
2153 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
2154 ((base == 16) && (((sc->cc)|0x20) == 'p')) ||
2156 (((sc->cc)|0x20) == 'e')
2157 ) { /* Process an exponent. */
2162 goto DONE_DO_UNGET; /* matching failure.. no exponent digits */
2165 if ((sc->cc == '+') || (sc->cc == '-')) { /* Signed exponent? */
2170 #ifdef __UCLIBC_MJN3_ONLY__
2171 #warning TODO: Fix MAX_EXP_DIGITS!
2173 #define MAX_EXP_DIGITS 20
2178 if (sc->cc == '0') {
2183 } while (sc->cc == '0');
2186 while (__isdigit_char_or_EOF(sc->cc)) { /* Exponent digits (base 10).*/
2187 if (seendigit < MAX_EXP_DIGITS) {
2194 if (!seendigit) { /* No digits. Unrecoverable. */
2205 x = __strtofpmax(buf, &e, exp_adjust);
2208 if (psfs->dataargtype & PA_FLAG_LONG_LONG) {
2209 *((long double *)psfs->cur_ptr) = (long double) x;
2210 } else if (psfs->dataargtype & PA_FLAG_LONG) {
2211 *((double *)psfs->cur_ptr) = (double) x;
2213 *((float *)psfs->cur_ptr) = (float) x;
2220 #endif /* __UCLIBC_HAS_FLOATS__ */
2225 return -2; /* Matching failure. */
2229 /**********************************************************************/