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.
46 #define _ISOC99_SOURCE /* for LLONG_MAX primarily... */
58 #ifdef __UCLIBC_HAS_WCHAR__
59 #include <bits/uClibc_uwchar.h>
62 #endif /* __UCLIBC_HAS_WCHAR__ */
70 #ifdef __UCLIBC_HAS_THREADS__
71 #include <stdio_ext.h>
73 #endif /* __UCLIBC_HAS_THREADS__ */
75 #ifdef __UCLIBC_HAS_FLOATS__
77 #include <bits/uClibc_fpmax.h>
78 #endif /* __UCLIBC_HAS_FLOATS__ */
80 #ifdef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
82 /* only emit this once */
83 #warning Forcing undef of __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__ until implemented!
85 #undef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
88 #undef __STDIO_HAS_VSSCANF
89 #if defined(__STDIO_BUFFERS) || !defined(__UCLIBC_HAS_WCHAR__) || defined(__UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__)
90 #define __STDIO_HAS_VSSCANF 1
92 #if !defined(__STDIO_BUFFERS) && !defined(__UCLIBC_HAS_WCHAR__)
95 unsigned char *bufread; /* pointer to 1 past end of buffer */
96 unsigned char *bufpos;
102 extern void _store_inttype(void *dest, int desttype, uintmax_t val);
104 #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
106 extern unsigned long long
107 _stdlib_strto_ll(register const char * __restrict str,
108 char ** __restrict endptr, int base, int sflag);
109 #if (ULLONG_MAX == UINTMAX_MAX)
110 #define STRTOUIM(s,e,b,sf) _stdlib_strto_ll(s,e,b,sf)
113 #else /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
116 _stdlib_strto_l(register const char * __restrict str,
117 char ** __restrict endptr, int base, int sflag);
119 #if (ULONG_MAX == UINTMAX_MAX)
120 #define STRTOUIM(s,e,b,sf) _stdlib_strto_l(s,e,b,sf)
123 #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
126 #error STRTOUIM conversion function is undefined!
129 /**********************************************************************/
131 /* The standards require EOF < 0. */
133 #define __isdigit_char_or_EOF(C) __isdigit_char((C))
135 #define __isdigit_char_or_EOF(C) __isdigit_int((C))
138 /**********************************************************************/
141 int fscanf(FILE * __restrict stream, const char * __restrict format, ...)
146 va_start(arg, format);
147 rv = vfscanf(stream, format, arg);
152 libc_hidden_def(fscanf)
155 /**********************************************************************/
158 int scanf(const char * __restrict format, ...)
163 va_start(arg, format);
164 rv = vfscanf(stdin, format, arg);
171 /**********************************************************************/
174 #ifdef __STDIO_HAS_VSSCANF
176 int sscanf(const char * __restrict str, const char * __restrict format, ...)
181 va_start(arg, format);
182 rv = vsscanf(str, format, arg);
187 libc_hidden_def(sscanf)
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);
201 libc_hidden_def(vscanf)
204 /**********************************************************************/
207 #ifdef __UCLIBC_MJN3_ONLY__
208 #warning WISHLIST: Implement vsscanf for non-buf and no custom stream case.
209 #endif /* __UCLIBC_MJN3_ONLY__ */
211 #ifdef __STDIO_BUFFERS
213 int vsscanf(__const char *sp, __const char *fmt, va_list ap)
217 /* __STDIO_STREAM_RESET_GCS(&f); */
218 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
219 f.__cookie = &(f.__filedes);
221 f.__gcs.write = NULL;
223 f.__gcs.close = NULL;
226 f.__filedes = __STDIO_STREAM_FAKE_VSSCANF_FILEDES;
227 f.__modeflags = (__FLAG_NARROW|__FLAG_READONLY|__FLAG_READING);
229 #ifdef __UCLIBC_HAS_WCHAR__
230 f.__ungot_width[0] = 0;
232 #ifdef __STDIO_MBSTATE
233 __INIT_MBSTATE(&(f.__state));
236 #ifdef __UCLIBC_HAS_THREADS__
237 f.__user_locking = 1; /* Set user locking. */
238 STDIO_INIT_MUTEX(f.__lock);
242 /* Set these last since __bufgetc initialization depends on
243 * __user_locking and only gets set if user locking is on. */
245 f.__bufpos = (unsigned char *) ((void *) sp);
247 f.__bufend = f.__bufstart + strlen(sp);
248 __STDIO_STREAM_ENABLE_GETC(&f);
249 __STDIO_STREAM_DISABLE_PUTC(&f);
251 return vfscanf(&f, fmt, ap);
253 libc_hidden_def(vsscanf)
255 #elif !defined(__UCLIBC_HAS_WCHAR__)
257 int vsscanf(__const char *sp, __const char *fmt, va_list ap)
261 f.bufpos = (unsigned char *) ((void *) sp);
262 f.bufread = f.bufpos + strlen(sp);
264 /* __STDIO_STREAM_RESET_GCS(&f.f); */
265 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
266 f.f.__cookie = &(f.f.__filedes);
267 f.f.__gcs.read = NULL;
268 f.f.__gcs.write = NULL;
269 f.f.__gcs.seek = NULL;
270 f.f.__gcs.close = NULL;
273 f.f.__filedes = __STDIO_STREAM_FAKE_VSSCANF_FILEDES_NB;
274 f.f.__modeflags = (__FLAG_NARROW|__FLAG_READONLY|__FLAG_READING);
276 /* #ifdef __UCLIBC_HAS_WCHAR__ */
277 /* f.f.__ungot_width[0] = 0; */
279 #ifdef __STDIO_MBSTATE
280 #error __STDIO_MBSTATE is defined!
281 /* __INIT_MBSTATE(&(f.f.__state)); */
284 #ifdef __UCLIBC_HAS_THREADS__
285 f.f.__user_locking = 1; /* Set user locking. */
286 STDIO_INIT_MUTEX(f.f.__lock);
288 f.f.__nextopen = NULL;
290 return vfscanf(&f.f, fmt, ap);
292 libc_hidden_def(vsscanf)
294 #elif defined(__UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__)
296 int vsscanf(__const char *sp, __const char *fmt, va_list ap)
301 if ((f = fmemopen((char *)sp, strlen(sp), "r")) != NULL) {
302 rv = vfscanf(f, fmt, ap);
308 libc_hidden_def(vsscanf)
311 #warning Skipping vsscanf since no buffering, no custom streams, and wchar enabled!
312 #ifdef __STDIO_HAS_VSSCANF
313 #error WHOA! __STDIO_HAS_VSSCANF is defined!
318 /**********************************************************************/
321 int fwscanf(FILE * __restrict stream, const wchar_t * __restrict format, ...)
326 va_start(arg, format);
327 rv = vfwscanf(stream, format, arg);
334 /**********************************************************************/
337 int wscanf(const wchar_t * __restrict format, ...)
342 va_start(arg, format);
343 rv = vfwscanf(stdin, format, arg);
350 /**********************************************************************/
353 #ifdef __STDIO_BUFFERS
355 int swscanf(const wchar_t * __restrict str, const wchar_t * __restrict format,
361 va_start(arg, format);
362 rv = vswscanf(str, format, arg);
367 #else /* __STDIO_BUFFERS */
368 #warning Skipping swscanf since no buffering!
369 #endif /* __STDIO_BUFFERS */
372 /**********************************************************************/
375 int vwscanf(const wchar_t * __restrict format, va_list arg)
377 return vfwscanf(stdin, format, arg);
381 /**********************************************************************/
384 #ifdef __STDIO_BUFFERS
386 int vswscanf(const wchar_t * __restrict str, const wchar_t * __restrict format,
392 f.__bufpos = (char *) str;
394 f.__bufend = (char *)(str + wcslen(str));
395 __STDIO_STREAM_DISABLE_GETC(&f);
396 __STDIO_STREAM_DISABLE_PUTC(&f);
398 /* __STDIO_STREAM_RESET_GCS(&f); */
399 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
400 f.__cookie = &(f.__filedes);
402 f.__gcs.write = NULL;
404 f.__gcs.close = NULL;
407 f.__filedes = __STDIO_STREAM_FAKE_VSWSCANF_FILEDES;
408 f.__modeflags = (__FLAG_WIDE|__FLAG_READONLY|__FLAG_READING);
410 #ifdef __UCLIBC_HAS_WCHAR__
411 f.__ungot_width[0] = 0;
412 #endif /* __UCLIBC_HAS_WCHAR__ */
413 #ifdef __STDIO_MBSTATE
414 __INIT_MBSTATE(&(f.__state));
415 #endif /* __STDIO_MBSTATE */
417 #ifdef __UCLIBC_HAS_THREADS__
418 f.__user_locking = 1; /* Set user locking. */
419 STDIO_INIT_MUTEX(f.__lock);
423 return vfwscanf(&f, format, arg);
425 libc_hidden_def(vswscanf)
426 #else /* __STDIO_BUFFERS */
427 #warning Skipping vswscanf since no buffering!
428 #endif /* __STDIO_BUFFERS */
431 /**********************************************************************/
432 /**********************************************************************/
436 /* float layout 0123456789012345678901 repeat n for "l[" */
437 #define SPEC_CHARS "npxXoudifFeEgGaACSncs["
438 /* npxXoudif eEgG CS cs[ */
440 /* NOTE: Ordering is important! In particular, CONV_LEFTBRACKET
441 * must immediately precede CONV_c. */
446 CONV_x, CONV_X, CONV_o, CONV_u, CONV_d, CONV_i,
447 CONV_f, CONV_F, CONV_e, CONV_E, CONV_g, CONV_G, CONV_a, CONV_A,
448 CONV_C, CONV_S, CONV_LEFTBRACKET, CONV_c, CONV_s, CONV_leftbracket,
449 CONV_percent, CONV_whitespace /* not in SPEC_* and no flags */
452 #ifdef __UCLIBC_HAS_FLOATS__
453 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
454 /* p x X o u d i f F e E g G a A */
455 #define SPEC_BASE { 16, 16, 16, 8, 10, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
457 /* p x X o u d i f F e E g G a A */
458 #define SPEC_BASE { 16, 16, 16, 8, 10, 10, 0, 10, 10, 10, 10, 10, 10, 10, 10 }
460 #else /* __UCLIBC_HAS_FLOATS__ */
461 /* p x X o u d i f F e E g G a A */
462 #define SPEC_BASE { 16, 16, 16, 8, 10, 10, 0 }
463 #endif /* __UCLIBC_HAS_FLOATS__ */
465 #ifdef __UCLIBC_MJN3_ONLY__
468 #warning CONSIDER: Add a '0' flag to eat 0 padding when grouping?
470 #endif /* __UCLIBC_MJN3_ONLY__ */
472 #define SPEC_FLAGS "*'I"
475 FLAG_SURPRESS = 0x10, /* MUST BE 1ST!! See DO_FLAGS. */
476 FLAG_THOUSANDS = 0x20,
477 FLAG_I18N = 0x40, /* only works for d, i, u */
478 FLAG_MALLOC = 0x80, /* only works for s, S, and [ (and l[)*/
482 #define SPEC_RANGES { CONV_n, CONV_p, CONV_i, CONV_A, \
483 CONV_C, CONV_LEFTBRACKET, \
484 CONV_c, CONV_leftbracket }
486 /* Note: We treat L and ll as synonymous... for ints and floats. */
488 #define SPEC_ALLOWED_FLAGS { \
489 /* n */ (0x0f|FLAG_SURPRESS), \
490 /* p */ ( 0|FLAG_SURPRESS), \
491 /* oxXudi */ (0x0f|FLAG_SURPRESS|FLAG_THOUSANDS|FLAG_I18N), \
492 /* fFeEgGaA */ (0x0c|FLAG_SURPRESS|FLAG_THOUSANDS|FLAG_I18N), \
493 /* C */ ( 0|FLAG_SURPRESS), \
494 /* S and l[ */ ( 0|FLAG_SURPRESS|FLAG_MALLOC), \
495 /* c */ (0x04|FLAG_SURPRESS), \
496 /* s and [ */ (0x04|FLAG_SURPRESS|FLAG_MALLOC), \
500 /**********************************************************************/
502 * In order to ease translation to what arginfo and _print_info._flags expect,
503 * we map: 0:int 1:char 2:longlong 4:long 8:short
504 * and then _flags |= (((q << 7) + q) & 0x701) and argtype |= (_flags & 0x701)
507 /* TODO -- Fix the table below to take into account stdint.h. */
508 /* #ifndef LLONG_MAX */
509 /* #error fix QUAL_CHARS for no long long! Affects 'L', 'j', 'q', 'll'. */
511 /* #if LLONG_MAX != INTMAX_MAX */
512 /* #error fix QUAL_CHARS intmax_t entry 'j'! */
517 #error PDS already defined!
520 #error SS already defined!
523 #error IMS already defined!
526 #if PTRDIFF_MAX == INT_MAX
528 #elif PTRDIFF_MAX == LONG_MAX
530 #elif defined(LLONG_MAX) && (PTRDIFF_MAX == LLONG_MAX)
533 #error fix QUAL_CHARS ptrdiff_t entry 't'!
536 #if SIZE_MAX == UINT_MAX
538 #elif SIZE_MAX == ULONG_MAX
540 #elif defined(LLONG_MAX) && (SIZE_MAX == ULLONG_MAX)
543 #error fix QUAL_CHARS size_t entries 'z', 'Z'!
546 #if INTMAX_MAX == INT_MAX
548 #elif INTMAX_MAX == LONG_MAX
550 #elif defined(LLONG_MAX) && (INTMAX_MAX == LLONG_MAX)
553 #error fix QUAL_CHARS ptrdiff_t entry 't'!
556 #define QUAL_CHARS { \
557 /* j:(u)intmax_t z:(s)size_t t:ptrdiff_t \0:int q:long_long */ \
558 'h', 'l', 'L', 'j', 'z', 't', 'q', 0, \
559 2, 4, 8, IMS, SS, PDS, 8, 0, /* TODO -- fix!!! */ \
564 /**********************************************************************/
567 /* FIXME: "warning: the right operand of ">" changes sign when promoted" */
569 #error Unfortunately, we currently need wint_t to be able to store EOF. Sorry.
573 #define Wchar wchar_t
574 #define Wuchar __uwchar_t
575 #define ISSPACE(C) iswspace((C))
576 #define VFSCANF vfwscanf
577 #define GETC(SC) (SC)->sc_getc((SC))
579 typedef unsigned char __uchar_t;
583 #define Wuchar __uchar_t
584 #define ISSPACE(C) isspace((C))
585 #define VFSCANF vfscanf
586 #ifdef __UCLIBC_HAS_WCHAR__
587 #define GETC(SC) (SC)->sc_getc((SC))
588 #else /* __UCLIBC_HAS_WCHAR__ */
589 #define GETC(SC) getc_unlocked((SC)->fp)
590 #endif /* __UCLIBC_HAS_WCHAR__ */
600 #ifdef __UCLIBC_HAS_WCHAR__
601 wchar_t app_ungot; /* Match FILE struct member type. */
602 unsigned char ungot_wchar_width;
603 #else /* __UCLIBC_HAS_WCHAR__ */
604 unsigned char app_ungot; /* Match FILE struct member type. */
605 #endif /* __UCLIBC_HAS_WCHAR__ */
609 #ifdef __UCLIBC_HAS_WCHAR__
610 char ungot_wflag; /* vfwscanf */
611 char mb_fail; /* vfscanf */
612 mbstate_t mbstate; /* vfscanf */
614 wint_t ungot_wchar; /* to support __scan_getc */
615 int (*sc_getc)(struct scan_cookie *);
616 #endif /* __UCLIBC_HAS_WCHAR__ */
618 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
619 const char *grouping;
620 const unsigned char *thousands_sep;
622 #ifdef __UCLIBC_HAS_WCHAR__
623 wchar_t thousands_sep_wc;
624 #endif /* __UCLIBC_HAS_WCHAR__ */
625 #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
627 #ifdef __UCLIBC_HAS_FLOATS__
628 const unsigned char *decpt;
630 #ifdef __UCLIBC_HAS_WCHAR__
632 #endif /* __UCLIBC_HAS_WCHAR__ */
633 const unsigned char *fake_decpt;
634 #endif /* __UCLIBC_HAS_FLOATS__ */
639 #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
641 #warning NL_ARGMAX > 10, and space is allocated on the stack for positional args.
643 void *pos_args[NL_ARGMAX];
644 int num_pos_args; /* Must start at -1. */
646 #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
648 const unsigned char *fmt;
649 int cnt, dataargtype, conv_num, max_width;
650 unsigned char store, flags;
651 } psfs_t; /* parse scanf format state */
654 /**********************************************************************/
655 /**********************************************************************/
657 extern void __init_scan_cookie(register struct scan_cookie *sc,
658 register FILE *fp) attribute_hidden;
659 extern int __scan_getc(register struct scan_cookie *sc) attribute_hidden;
660 extern void __scan_ungetc(register struct scan_cookie *sc) attribute_hidden;
662 #ifdef __UCLIBC_HAS_FLOATS__
663 extern int __scan_strtold(long double *ld, struct scan_cookie *sc);
664 #endif /* __UCLIBC_HAS_FLOATS__ */
666 extern int __psfs_parse_spec(psfs_t *psfs) attribute_hidden;
667 extern int __psfs_do_numeric(psfs_t *psfs, struct scan_cookie *sc) attribute_hidden;
669 /**********************************************************************/
670 #ifdef L___scan_cookie
672 #ifdef __UCLIBC_MJN3_ONLY__
673 #warning TODO: Remove dependence on decpt_str and fake_decpt in stub locale mode.
675 #ifndef __UCLIBC_HAS_LOCALE__
676 static const char decpt_str[] = ".";
679 void attribute_hidden __init_scan_cookie(register struct scan_cookie *sc,
685 sc->app_ungot = ((fp->__modeflags & __FLAG_UNGOT) ? fp->__ungot[1] : 0);
686 #ifdef __UCLIBC_HAS_WCHAR__
687 sc->ungot_wflag = 0; /* vfwscanf */
689 #endif /* __UCLIBC_HAS_WCHAR__ */
691 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
692 if (*(sc->grouping = __UCLIBC_CURLOCALE->grouping)) {
693 sc->thousands_sep = (const unsigned char *) __UCLIBC_CURLOCALE->thousands_sep;
694 sc->tslen = __UCLIBC_CURLOCALE->thousands_sep_len;
695 #ifdef __UCLIBC_HAS_WCHAR__
696 sc->thousands_sep_wc = __UCLIBC_CURLOCALE->thousands_sep_wc;
697 #endif /* __UCLIBC_HAS_WCHAR__ */
699 #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
701 #ifdef __UCLIBC_HAS_FLOATS__
702 #ifdef __UCLIBC_HAS_LOCALE__
703 sc->decpt = (const unsigned char *) __UCLIBC_CURLOCALE->decimal_point;
704 sc->decpt_len = __UCLIBC_CURLOCALE->decimal_point_len;
705 #else /* __UCLIBC_HAS_LOCALE__ */
706 sc->fake_decpt = sc->decpt = (unsigned char *) decpt_str;
708 #endif /* __UCLIBC_HAS_LOCALE__ */
709 #ifdef __UCLIBC_HAS_WCHAR__
710 #ifdef __UCLIBC_HAS_LOCALE__
711 sc->decpt_wc = __UCLIBC_CURLOCALE->decimal_point_wc;
715 #endif /* __UCLIBC_HAS_WCHAR__ */
716 #endif /* __UCLIBC_HAS_FLOATS__ */
720 int attribute_hidden __scan_getc(register struct scan_cookie *sc)
724 #ifdef __UCLIBC_HAS_WCHAR__
725 assert(!sc->mb_fail);
726 #endif /* __UCLIBC_HAS_WCHAR__ */
730 if (--sc->width < 0) {
735 if (sc->ungot_flag == 0) {
736 #if !defined(__STDIO_BUFFERS) && !defined(__UCLIBC_HAS_WCHAR__)
737 if (!__STDIO_STREAM_IS_FAKE_VSSCANF_NB(sc->fp)) {
740 __FILE_vsscanf *fv = (__FILE_vsscanf *)(sc->fp);
741 if (fv->bufpos < fv->bufread) {
745 sc->fp->__modeflags |= __FLAG_EOF;
753 if ((c = GETC(sc)) == EOF) {
760 assert(sc->ungot_flag == 1);
765 return sc->cc = sc->ungot_char;
768 void attribute_hidden __scan_ungetc(register struct scan_cookie *sc)
771 if (sc->ungot_flag == 2) { /* last was EOF */
773 sc->cc = sc->ungot_char;
774 } else if (sc->ungot_flag == 0) {
783 /**********************************************************************/
784 #ifdef L___psfs_parse_spec
787 static const unsigned char spec_flags[] = SPEC_FLAGS;
788 #endif /* SPEC_FLAGS */
789 static const unsigned char spec_chars[] = SPEC_CHARS;
790 static const unsigned char qual_chars[] = QUAL_CHARS;
791 static const unsigned char spec_ranges[] = SPEC_RANGES;
792 static const unsigned short spec_allowed[] = SPEC_ALLOWED_FLAGS;
794 int attribute_hidden __psfs_parse_spec(register psfs_t *psfs)
796 const unsigned char *p;
797 const unsigned char *fmt0 = psfs->fmt;
802 #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
803 unsigned char fail = 0;
805 i = 0; /* Do this here to avoid a warning. */
807 if (!__isdigit_char(*psfs->fmt)) { /* Not a positional arg. */
812 /* parse the positional arg (or width) value */
814 if (i <= ((INT_MAX - 9)/10)) {
815 i = (i * 10) + (*psfs->fmt++ - '0');
817 } while (__isdigit_char(*psfs->fmt));
819 if (*psfs->fmt != '$') { /* This is a max field width. */
820 if (psfs->num_pos_args >= 0) { /* Already saw a pos arg! */
824 psfs->num_pos_args = -2;
827 ++psfs->fmt; /* Advance past '$'. */
828 #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
830 #if defined(SPEC_FLAGS) || (defined(NL_ARGMAX) && (NL_ARGMAX > 0))
832 #endif /* defined(SPEC_FLAGS) || (defined(NL_ARGMAX) && (NL_ARGMAX > 0)) */
837 if (*p == *psfs->fmt) {
845 if (psfs->flags & FLAG_SURPRESS) { /* Suppress assignment. */
849 #else /* SPEC_FLAGS */
850 if (*psfs->fmt == '*') { /* Suppress assignment. */
855 #endif /* SPEC_FLAGS */
858 #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
860 /* Must be a non-positional arg */
861 if (psfs->num_pos_args >= 0) { /* Already saw a pos arg! */
864 psfs->num_pos_args = -2;
866 if ((psfs->num_pos_args == -2) || (((unsigned int)(--i)) >= NL_ARGMAX)) {
867 /* Already saw a non-pos arg or (0-based) num too large. */
870 psfs->cur_pos_arg = i;
872 #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
875 for (i = 0 ; __isdigit_char(*psfs->fmt) ; ) {
876 if (i <= ((INT_MAX - 9)/10)) {
877 i = (i * 10) + (*psfs->fmt++ - '0');
882 #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
884 #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
887 if (*psfs->fmt == *p) {
892 if ((p - qual_chars < 2) && (*psfs->fmt == *p)) {
893 p += ((sizeof(qual_chars)-2) / 2);
896 psfs->dataargtype = ((int)(p[(sizeof(qual_chars)-2) / 2])) << 8;
898 #ifdef __UCLIBC_MJN3_ONLY__
899 #warning CONSIDER: Should we validate that psfs->max_width > 0 in __psfs_parse_spec()? It would avoid whitespace consumption...
900 #warning CONSIDER: Should INT_MAX be a valid width (%c/%C)? See __psfs_parse_spec().
901 #endif /* __UCLIBC_MJN3_ONLY__ */
905 if (*psfs->fmt == *p) {
906 int p_m_spec_chars = p - spec_chars;
908 #ifdef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
909 #error implement gnu a flag
911 && ((psfs->fmt[1] == '[') || ((psfs->fmt[1]|0x20) == 's'))
912 ) { /* Assumes ascii for 's' and 'S' test. */
913 psfs->flags |= FLAG_MALLOC;
916 continue; /* The related conversions follow 'a'. */
918 #endif /* __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__ */
920 for (p = spec_ranges; p_m_spec_chars > *p ; ++p) {}
921 if (((psfs->dataargtype >> 8) | psfs->flags)
922 & ~spec_allowed[(int)(p - spec_ranges)]
927 if ((p_m_spec_chars >= CONV_c)
928 && (psfs->dataargtype & PA_FLAG_LONG)) {
929 p_m_spec_chars -= 3; /* lc -> C, ls -> S, l[ -> ?? */
932 psfs->conv_num = p_m_spec_chars;
933 return psfs->fmt - fmt0;
946 /**********************************************************************/
947 #if defined(L_vfscanf) || defined(L_vfwscanf)
949 #ifdef __UCLIBC_HAS_WCHAR__
951 static int sc_getc(register struct scan_cookie *sc)
953 return (getc_unlocked)(sc->fp); /* Disable the macro. */
956 static int scan_getwc(register struct scan_cookie *sc)
963 if (--sc->width < 0) {
968 width = sc->width; /* Preserve width. */
969 sc->width = INT_MAX; /* MB_CUR_MAX can invoke a function. */
971 assert(!sc->mb_fail);
974 while (__scan_getc(sc) >= 0) {
977 r = mbrtowc(wc, b, 1, &sc->mbstate);
978 if (((ssize_t) r) >= 0) { /* Successful completion of a wc. */
981 } else if (r == ((size_t) -2)) {
982 /* Potentially valid but incomplete. */
988 if (r == ((size_t)(-3))) { /* EOF or ERROR on first read */
992 /* If we reach here, either r == ((size_t)-1) and
993 * mbrtowc set errno to EILSEQ, or r == ((size_t)-2)
994 * and stream is in an error state or at EOF with a
995 * partially complete wchar. */
996 __set_errno(EILSEQ); /* In case of incomplete conversion. */
1001 sc->width = width; /* Restore width. */
1003 return (int)((ssize_t) r);
1006 #endif /* L_vfscanf */
1010 /* This gets called by __scan_getc. __scan_getc is called by vfwscanf
1011 * when the next wide char is expected to be valid ascii (digits).
1013 static int sc_getc(register struct scan_cookie *sc)
1017 if (__STDIO_STREAM_IS_FAKE_VSWSCANF(sc->fp)) {
1018 if (sc->fp->__bufpos < sc->fp->__bufend) {
1019 wc = *((wchar_t *)(sc->fp->__bufpos));
1020 sc->fp->__bufpos += sizeof(wchar_t);
1022 sc->fp->__modeflags |= __FLAG_EOF;
1025 } else if ((wc = fgetwc_unlocked(sc->fp)) == WEOF) {
1029 sc->ungot_wflag = 1;
1030 sc->ungot_wchar = wc;
1031 sc->ungot_wchar_width = sc->fp->__ungot_width[0];
1033 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
1034 if (wc == sc->thousands_sep_wc) {
1037 #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
1038 #ifdef __UCLIBC_HAS_FLOATS__
1039 if (wc == sc->decpt_wc) {
1042 #endif /* __UCLIBC_HAS_FLOATS__ */
1043 sc->wc = sc->ungot_char = wc;
1048 static int scan_getwc(register struct scan_cookie *sc)
1054 if (--sc->width < 0) {
1055 sc->ungot_flag |= 2;
1059 if (sc->ungot_flag == 0) {
1060 if (__STDIO_STREAM_IS_FAKE_VSWSCANF(sc->fp)) {
1061 if (sc->fp->__bufpos < sc->fp->__bufend) {
1062 wc = *((wchar_t *)(sc->fp->__bufpos));
1063 sc->fp->__bufpos += sizeof(wchar_t);
1065 sc->ungot_flag |= 2;
1068 } else if ((wc = fgetwc_unlocked(sc->fp)) == WEOF) {
1069 sc->ungot_flag |= 2;
1072 sc->ungot_wflag = 1;
1073 sc->ungot_char = wc;
1074 sc->ungot_wchar_width = sc->fp->__ungot_width[0];
1076 assert(sc->ungot_flag == 1);
1081 sc->wc = sc->ungot_char;
1087 #endif /* L_vfwscanf */
1088 #endif /* __UCLIBC_HAS_WCHAR__ */
1090 static __inline void kill_scan_cookie(register struct scan_cookie *sc)
1094 if (sc->ungot_flag & 1) {
1095 #if !defined(__STDIO_BUFFERS) && !defined(__UCLIBC_HAS_WCHAR__)
1096 if (!__STDIO_STREAM_IS_FAKE_VSSCANF_NB(sc->fp)) {
1097 ungetc(sc->ungot_char, sc->fp);
1100 ungetc(sc->ungot_char, sc->fp);
1102 /* Deal with distiction between user and scanf ungots. */
1103 if (sc->nread == 0) { /* Only one char was read... app ungot? */
1104 sc->fp->__ungot[1] = sc->app_ungot; /* restore ungot state. */
1106 sc->fp->__ungot[1] = 0;
1112 if ((sc->ungot_flag & 1) && (sc->ungot_wflag & 1)
1113 && !__STDIO_STREAM_IS_FAKE_VSWSCANF(sc->fp)
1114 && (sc->fp->__state.__mask == 0)
1116 ungetwc(sc->ungot_char, sc->fp);
1117 /* Deal with distiction between user and scanf ungots. */
1118 if (sc->nread == 0) { /* Only one char was read... app ungot? */
1119 sc->fp->__ungot[1] = sc->app_ungot; /* restore ungot state. */
1121 sc->fp->__ungot[1] = 0;
1123 sc->fp->__ungot_width[1] = sc->ungot_wchar_width;
1130 int VFSCANF (FILE *__restrict fp, const Wchar *__restrict format, va_list arg)
1138 #endif /* L_vfwscanf */
1140 #if defined(__UCLIBC_HAS_LOCALE__) && !defined(L_vfwscanf) || !defined(L_vfscanf)
1144 struct scan_cookie sc;
1148 #ifdef __UCLIBC_MJN3_ONLY__
1149 #warning TODO: Fix MAX_DIGITS. We do not do binary, so...!
1151 #define MAX_DIGITS 65 /* Allow one leading 0. */
1152 unsigned char buf[MAX_DIGITS+2];
1154 unsigned char scanset[UCHAR_MAX + 1];
1155 unsigned char invert; /* Careful! Meaning changes. */
1156 #endif /* L_vfscanf */
1158 unsigned char zero_conversions = 1;
1159 __STDIO_AUTO_THREADLOCK_VAR;
1161 #ifdef __UCLIBC_MJN3_ONLY__
1162 #warning TODO: Make checking of the format string in C locale an option.
1164 /* To support old programs, don't check mb validity if in C locale. */
1165 #if defined(__UCLIBC_HAS_LOCALE__) && !defined(L_vfwscanf)
1166 /* ANSI/ISO C99 requires format string to be a valid multibyte string
1167 * beginning and ending in its initial shift state. */
1168 if (__UCLIBC_CURLOCALE->encoding != __ctype_encoding_7_bit) {
1169 const char *p = format;
1170 mbstate.__mask = 0; /* Initialize the mbstate. */
1171 if (mbsrtowcs(NULL, &p, SIZE_MAX, &mbstate) == ((size_t)(-1))) {
1172 __set_errno(EINVAL); /* Format string is invalid. */
1176 #endif /* defined(__UCLIBC_HAS_LOCALE__) && !defined(L_vfwscanf) */
1178 #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
1179 psfs.num_pos_args = -1; /* Must start at -1. */
1180 /* Initialize positional arg ptrs to NULL. */
1181 memset(psfs.pos_args, 0, sizeof(psfs.pos_args));
1182 #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
1184 __STDIO_AUTO_THREADLOCK(fp);
1186 __STDIO_STREAM_VALIDATE(fp);
1188 __init_scan_cookie(&sc,fp);
1189 #ifdef __UCLIBC_HAS_WCHAR__
1190 sc.sc_getc = sc_getc;
1191 sc.ungot_wchar_width = sc.fp->__ungot_width[1];
1195 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
1197 sc.thousands_sep = (const unsigned char *) ",";
1200 #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
1202 #ifdef __UCLIBC_HAS_FLOATS__
1203 sc.fake_decpt = (const unsigned char *) ".";
1204 #endif /* __UCLIBC_HAS_FLOATS__ */
1206 #else /* L_vfwscanf */
1208 #ifdef __UCLIBC_HAS_FLOATS__
1209 sc.fake_decpt = sc.decpt;
1210 #endif /* __UCLIBC_HAS_FLOATS__ */
1212 #endif /* L_vfwscanf */
1214 #endif /* __UCLIBC_HAS_WCHAR__ */
1217 /* Note: If we ever wanted to support non-nice codesets, we
1218 * would really need to do a mb->wc conversion here in the
1219 * vfscanf case. Related changes would have to be made in
1220 * the code that follows... basicly wherever fmt appears. */
1221 for (fmt = (const Wuchar *) format ; *fmt ; /* ++fmt */) {
1226 psfs.cur_ptr = NULL; /* Debugging aid. */
1230 sc.ungot_flag &= 1; /* Clear (possible fake) EOF. */
1231 sc.width = psfs.max_width = INT_MAX;
1233 /* Note: According to the standards, vfscanf does use isspace
1234 * here. So, if we did a mb->wc conversion, we would have to do
1236 * ((((__uwchar_t)wc) < UCHAR_MAX) && isspace(wc))
1237 * because wc might not be in the allowed domain. */
1238 if (ISSPACE(*fmt)) {
1241 } while (ISSPACE(*fmt));
1243 psfs.conv_num = CONV_whitespace;
1247 if (*fmt == '%') { /* Conversion specification. */
1248 if (*++fmt == '%') { /* Remember, '%' eats whitespace too. */
1249 /* Note: The standard says no conversion occurs.
1250 * So do not reset zero_conversions flag. */
1251 psfs.conv_num = CONV_percent;
1258 #else /* L_vfscanf */
1260 const __uwchar_t *wf = fmt;
1263 while (*wf && __isascii(*wf) && (b < buf + sizeof(buf) - 1)) {
1266 #ifdef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
1267 #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.
1268 if ((*b == 'a') && ((*wf == '[') || ((*wf|0x20) == 's'))) {
1269 goto DONE; /* Spec was excessively long. */
1271 #endif /* __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__ */
1273 if (b == buf) { /* Bad conversion specifier! */
1277 #endif /* L_vfscanf */
1278 if ((i = __psfs_parse_spec(&psfs)) < 0) { /* Bad conversion specifier! */
1284 #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
1285 if (psfs.num_pos_args == -2) {
1286 psfs.cur_ptr = va_arg(arg, void *);
1288 while (psfs.cur_pos_arg > psfs.num_pos_args) {
1289 psfs.pos_args[++psfs.num_pos_args] = va_arg(arg, void *);
1291 psfs.cur_ptr = psfs.pos_args[psfs.cur_pos_arg];
1293 #else /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
1294 psfs.cur_ptr = va_arg(arg, void *);
1295 #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
1299 /* First, consume white-space if not n, c, [, C, or l[. */
1300 if ((((1L << CONV_n)|(1L << CONV_C)|(1L << CONV_c)
1301 |(1L << CONV_LEFTBRACKET)|(1L << CONV_leftbracket))
1302 & (1L << psfs.conv_num)) == 0
1305 while ((__scan_getc(&sc) >= 0)
1308 #else /* L_vfscanf */
1310 #endif /* L_vfscanf */
1313 if (psfs.conv_num == CONV_whitespace) {
1318 sc.width = psfs.max_width; /* Now limit the max width. */
1320 if (sc.width == 0) { /* 0 width is forbidden. */
1325 if (psfs.conv_num == CONV_percent) {
1329 if (psfs.conv_num == CONV_n) {
1330 #ifdef __UCLIBC_MJN3_ONLY__
1331 #warning CONSIDER: Should %n count as a conversion as far as EOF return value?
1333 /* zero_conversions = 0; */
1335 _store_inttype(psfs.cur_ptr, psfs.dataargtype,
1336 (uintmax_t) sc.nread);
1341 if (psfs.conv_num <= CONV_A) { /* pointer, integer, or float spec */
1342 int r = __psfs_do_numeric(&psfs, &sc);
1344 if (sc.ungot_wflag == 1) { /* fix up '?', '.', and ',' hacks */
1345 sc.cc = sc.ungot_char = sc.ungot_wchar;
1348 if (r != -1) { /* Either success or a matching failure. */
1349 zero_conversions = 0;
1357 /* Do string conversions here since they are not common code. */
1363 #ifdef __UCLIBC_HAS_WCHAR__
1364 (psfs.conv_num >= CONV_LEFTBRACKET)
1365 #else /* __UCLIBC_HAS_WCHAR__ */
1366 (psfs.conv_num >= CONV_c)
1367 #endif /* __UCLIBC_HAS_WCHAR__ */
1369 b = (psfs.store ? ((unsigned char *) psfs.cur_ptr) : buf);
1372 if (psfs.conv_num == CONV_c) {
1373 if (sc.width == INT_MAX) {
1377 while (__scan_getc(&sc) >= 0) {
1378 zero_conversions = 0;
1383 if (sc.width > 0) { /* Failed to read all required. */
1386 psfs.cnt += psfs.store;
1390 if (psfs.conv_num == CONV_s) {
1391 /* Yes, believe it or not, a %s conversion can store nuls. */
1392 while ((__scan_getc(&sc) >= 0) && !isspace(sc.cc)) {
1393 zero_conversions = 0;
1399 #ifdef __UCLIBC_HAS_WCHAR__
1400 assert((psfs.conv_num == CONV_LEFTBRACKET) || \
1401 (psfs.conv_num == CONV_leftbracket));
1402 #else /* __UCLIBC_HAS_WCHAR__ */
1403 assert((psfs.conv_num == CONV_leftbracket));
1404 #endif /* __UCLIBC_HAS_WCHAR__ */
1408 if (*++fmt == '^') {
1412 memset(scanset, invert, sizeof(scanset));
1416 scanset[(int)(']')] = invert;
1420 while (*fmt != ']') {
1421 if (!*fmt) { /* No closing ']'. */
1424 if ((*fmt == '-') && (fmt[1] != ']')
1425 && (fmt[-1] < fmt[1]) /* sorted? */
1429 /* Note: scanset[i] should already have been done
1430 * in the previous iteration. */
1432 scanset[++i] = invert;
1434 /* Safe to fall through, and a bit smaller. */
1437 scanset[(int) *fmt] = invert;
1441 #ifdef __UCLIBC_HAS_WCHAR__
1442 if (psfs.conv_num == CONV_LEFTBRACKET) {
1443 goto DO_LEFTBRACKET;
1445 #endif /* __UCLIBC_HAS_WCHAR__ */
1448 while (__scan_getc(&sc) >= 0) {
1449 zero_conversions = 0;
1450 if (!scanset[sc.cc]) {
1458 /* Common tail for processing of %s and %[. */
1461 if (fail) { /* nothing stored! */
1464 *b = 0; /* Nul-terminate string. */
1465 psfs.cnt += psfs.store;
1469 #ifdef __UCLIBC_HAS_WCHAR__
1470 DO_LEFTBRACKET: /* Need to do common wide init. */
1471 if (psfs.conv_num >= CONV_C) {
1475 sc.mbstate.__mask = 0;
1477 wb = (psfs.store ? ((wchar_t *) psfs.cur_ptr) : wbuf);
1480 if (psfs.conv_num == CONV_C) {
1481 if (sc.width == INT_MAX) {
1485 while (scan_getwc(&sc) >= 0) {
1486 zero_conversions = 0;
1487 assert(sc.width >= 0);
1493 if (sc.width > 0) { /* Failed to read all required. */
1496 psfs.cnt += psfs.store;
1501 if (psfs.conv_num == CONV_S) {
1502 /* Yes, believe it or not, a %s conversion can store nuls. */
1503 while (scan_getwc(&sc) >= 0) {
1504 zero_conversions = 0;
1505 if ((((__uwchar_t)(sc.wc)) <= UCHAR_MAX) && isspace(sc.wc)) {
1513 assert(psfs.conv_num == CONV_LEFTBRACKET);
1515 while (scan_getwc(&sc) >= 0) {
1516 zero_conversions = 0;
1517 if (((__uwchar_t) sc.wc) <= UCHAR_MAX) {
1518 if (!scanset[sc.wc]) {
1521 } else if (invert) {
1529 /* Common tail for processing of %ls and %l[. */
1532 if (fail || sc.mb_fail) { /* Nothing stored or mb error. */
1535 *wb = 0; /* Nul-terminate string. */
1536 psfs.cnt += psfs.store;
1541 #endif /* __UCLIBC_HAS_WCHAR__ */
1542 #else /* L_vfscanf */
1544 if (psfs.conv_num >= CONV_C) {
1547 if (psfs.conv_num >= CONV_c) {
1548 mbstate.__mask = 0; /* Initialize the mbstate. */
1550 b = (unsigned char *) psfs.cur_ptr;
1554 wb = (wchar_t *) psfs.cur_ptr;
1560 if ((psfs.conv_num == CONV_C) || (psfs.conv_num == CONV_c)) {
1561 if (sc.width == INT_MAX) {
1565 while (scan_getwc(&sc) >= 0) {
1566 zero_conversions = 0;
1567 if (psfs.conv_num == CONV_C) {
1571 i = wcrtomb((char*) b, sc.wc, &mbstate);
1572 if (i < 0) { /* Conversion failure. */
1581 if (sc.width > 0) { /* Failed to read all required. */
1584 psfs.cnt += psfs.store;
1588 if ((psfs.conv_num == CONV_S) || (psfs.conv_num == CONV_s)) {
1589 /* Yes, believe it or not, a %s conversion can store nuls. */
1590 while (scan_getwc(&sc) >= 0) {
1591 zero_conversions = 0;
1592 if (iswspace(sc.wc)) {
1595 if (psfs.conv_num == CONV_S) {
1599 i = wcrtomb((char*) b, sc.wc, &mbstate);
1600 if (i < 0) { /* Conversion failure. */
1612 unsigned char invert = 0;
1614 assert((psfs.conv_num == CONV_LEFTBRACKET)
1615 || (psfs.conv_num == CONV_leftbracket));
1617 if (*++fmt == '^') {
1621 sss = (const wchar_t *) fmt;
1625 while (*fmt != ']') {
1626 if (!*fmt) { /* No closing ']'. */
1629 if ((*fmt == '-') && (fmt[1] != ']')
1630 && (fmt[-1] < fmt[1]) /* sorted? */
1636 /* Ok... a valid scanset spec. */
1638 while (scan_getwc(&sc) >= 0) {
1639 zero_conversions = 0;
1641 do { /* We know sss < fmt. */
1642 if (*ssp == '-') { /* possible range... */
1643 /* Note: We accept a-c-e (ordered) as
1644 * equivalent to a-e. */
1646 if ((++ssp < (const wchar_t *) fmt)
1647 && (ssp[-2] < *ssp) /* sorted? */
1649 if ((sc.wc >= ssp[-2])
1650 && (sc.wc <= *ssp)) {
1653 continue; /* not in range */
1655 --ssp; /* oops... '-' at end, so back up */
1657 /* false alarm... a literal '-' */
1659 if (sc.wc == *ssp) { /* Matched literal char. */
1662 } while (++ssp < (const wchar_t *) fmt);
1664 if ((ssp == (const wchar_t *) fmt) ^ invert) {
1665 /* no match and not inverting
1666 * or match and inverting */
1669 if (psfs.conv_num == CONV_LEFTBRACKET) {
1673 i = wcrtomb((char*) b, sc.wc, &mbstate);
1674 if (i < 0) { /* Conversion failure. */
1684 /* Common tail for processing of %s and %[. */
1687 if (fail) { /* nothing stored! */
1690 *wb = 0; /* Nul-terminate string. */
1692 psfs.cnt += psfs.store;
1696 #endif /* L_vfscanf */
1700 } /* conversion specification */
1703 if (__scan_getc(&sc) != *fmt) {
1706 #endif /* L_vfwscanf */
1713 if (__FERROR_UNLOCKED(fp)) {
1719 if (__FERROR_UNLOCKED(fp) || (*fmt && zero_conversions && __FEOF_UNLOCKED(fp))) {
1720 psfs.cnt = EOF; /* Yes, vfwscanf also returns EOF. */
1723 kill_scan_cookie(&sc);
1725 __STDIO_STREAM_VALIDATE(fp);
1727 __STDIO_AUTO_THREADUNLOCK(fp);
1731 libc_hidden_def(VFSCANF)
1733 /**********************************************************************/
1734 #ifdef L___psfs_do_numeric
1736 static const unsigned char spec_base[] = SPEC_BASE;
1737 static const unsigned char nil_string[] = "(nil)";
1739 int attribute_hidden __psfs_do_numeric(psfs_t *psfs, struct scan_cookie *sc)
1742 const unsigned char *p;
1744 #ifdef __UCLIBC_HAS_FLOATS__
1747 #ifdef __UCLIBC_MJN3_ONLY__
1748 #warning TODO: Fix MAX_DIGITS. We do not do binary, so...!
1749 #warning TODO: Fix buf!
1751 #define MAX_DIGITS 65 /* Allow one leading 0. */
1752 unsigned char buf[MAX_DIGITS+2+ 100];
1753 unsigned char usflag, base;
1754 unsigned char nonzero = 0;
1755 unsigned char seendigit = 0;
1757 #ifdef __UCLIBC_MJN3_ONLY__
1758 #warning CONSIDER: What should be returned for an invalid conversion specifier?
1760 #ifndef __UCLIBC_HAS_FLOATS__
1761 if (psfs->conv_num > CONV_i) { /* floating point */
1766 base = spec_base[psfs->conv_num - CONV_p];
1767 usflag = (psfs->conv_num <= CONV_u); /* (1)0 if (un)signed */
1771 if (psfs->conv_num == CONV_p) { /* Pointer */
1774 if ((__scan_getc(sc) < 0) || (*p != sc->cc)) {
1776 if (p > nil_string) {
1777 /* We matched at least the '(' so even if we
1778 * are at eof, we can not match a pointer. */
1779 return -2; /* Matching failure */
1783 if (!*++p) { /* Matched (nil), so no unget necessary. */
1786 _store_inttype(psfs->cur_ptr, psfs->dataargtype,
1793 #ifdef __UCLIBC_MJN3_ONLY__
1794 #warning CONSIDER: Should we require a 0x prefix and disallow +/- for pointer %p?
1795 #endif /* __UCLIBC_MJN3_ONLY__ */
1800 return -1; /* Input failure (nothing read yet). */
1803 if ((sc->cc == '+') || (sc->cc == '-')) { /* Handle leading sign.*/
1808 if ((base & 0xef) == 0) { /* 0xef is ~16, so 16 or 0. */
1809 if (sc->cc == '0') { /* Possibly set base and handle prefix. */
1811 if ((sc->cc|0x20) == 'x') { /* Assumes ascii.. x or X. */
1812 if (__scan_getc(sc) < 0) {
1813 /* Either EOF or error (including wc outside char range).
1814 * If EOF or error, this is a matching failure (we read 0x).
1815 * If wc outside char range, this is also a matching failure.
1816 * Hence, we do an unget (although not really necessary here
1818 goto DONE_DO_UNGET; /* matching failure */
1820 base = 16; /* Base 16 for sure now. */
1821 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
1822 /* The prefix is required for hexadecimal floats. */
1825 #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
1826 } else { /* oops... back up */
1828 sc->cc = '0'; /* NASTY HACK! */
1830 base = (base >> 1) + 8; /* 0->8, 16->16. no 'if' */
1831 #ifdef __UCLIBC_HAS_FLOATS__
1832 if (psfs->conv_num > CONV_i) { /* floating point */
1842 /***************** digit grouping **********************/
1843 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
1845 if ((psfs->flags & FLAG_THOUSANDS) && (base == 10)
1846 && *(p = (const unsigned char *) sc->grouping)
1849 int nblk1, nblk2, nbmax, lastblock, pass, i;
1852 #ifdef __UCLIBC_MJN3_ONLY__
1853 #warning CONSIDER: Should we initalize the grouping blocks in __init_scan_cookie()?
1854 #endif /* __UCLIBC_MJN3_ONLY__ */
1855 nbmax = nblk2 = nblk1 = *p;
1858 if (nbmax < nblk2) {
1864 /* Note: for printf, if 0 and \' flags appear then
1865 * grouping is done before 0-padding. Should we
1866 * strip leading 0's first? Or add a 0 flag? */
1868 /* For vfwscanf, sc_getc translates, so the value of sc->cc is
1869 * either EOF or a char. */
1871 if (!__isdigit_char_or_EOF(sc->cc)) { /* No starting digit! */
1872 #ifdef __UCLIBC_HAS_FLOATS__
1873 if (psfs->conv_num > CONV_i) { /* floating point */
1874 goto NO_STARTING_DIGIT;
1880 if (sc->cc == '0') {
1882 *b++ = '0'; /* Store the first 0. */
1883 #ifdef __UCLIBC_MJN3_ONLY__
1884 #warning CONSIDER: Should leading 0s be skipped before digit grouping? (printf 0 pad)
1885 #endif /* __UCLIBC_MJN3_ONLY__ */
1887 do { /* But ignore all subsequent 0s. */
1889 } while (sc->cc == '0');
1896 while (__isdigit_char_or_EOF(sc->cc)) {
1898 if (i == nbmax) { /* too many digits for a block */
1899 #ifdef __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__
1900 if (!pass) { /* treat as nongrouped */
1904 goto DO_TRIM_LEADING_ZEROS;
1907 if (nbmax > nblk1) {
1908 goto DONE_DO_UNGET; /* matching failure */
1910 goto DONE_GROUPING_DO_UNGET; /* nbmax == nblk1 */
1914 if (nonzero || (sc->cc != '0')) {
1915 if (b < buf + MAX_DIGITS) {
1918 #ifdef __UCLIBC_HAS_FLOATS__
1928 if (i) { /* we saw digits digits */
1929 if ((i == nblk2) || ((i < nblk2) && !pass)) {
1930 /* (possible) outer grp */
1931 p = sc->thousands_sep;
1932 if (*p == sc->cc) { /* first byte matches... */
1933 /* so check if grouping mb char */
1934 /* Since 1st matched, either match or fail now
1935 * unless EOF (yuk) */
1938 if (!*++p) { /* is a grouping mb char */
1947 /* bad grouping mb char! */
1949 if ((sc->cc >= 0) || (p > sc->thousands_sep + 1)) {
1950 #ifdef __UCLIBC_HAS_FLOATS__
1951 /* We failed to match a thousep mb char, and
1952 * we've read too much to recover. But if
1953 * this is a floating point conversion and
1954 * the initial portion of the decpt mb char
1955 * matches, then we may still be able to
1957 int k = p - sc->thousands_sep - 1;
1959 if ((psfs->conv_num > CONV_i) /* float conversion */
1960 && (!pass || (i == nblk1)) /* possible last */
1961 && !memcmp(sc->thousands_sep, sc->fake_decpt, k)
1962 /* and prefix matched, so could be decpt */
1965 p = sc->fake_decpt + k;
1968 strcpy((char*) b, (char*) sc->decpt);
1979 #endif /* __UCLIBC_HAS_FLOATS__ */
1982 /* was EOF and 1st, so recoverable. */
1985 if ((i == nblk1) || ((i < nblk1) && !pass)) {
1986 /* got an inner group */
1987 goto DONE_GROUPING_DO_UNGET;
1989 goto DONE_DO_UNGET; /* Matching failure. */
1997 assert(0); /* Should never get here. */
2000 #endif /***************** digit grouping **********************/
2002 /* Not grouping so first trim all but one leading 0. */
2003 #ifdef __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__
2004 DO_TRIM_LEADING_ZEROS:
2005 #endif /* __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__ */
2006 if (sc->cc == '0') {
2008 *b++ = '0'; /* Store the first 0. */
2009 do { /* But ignore all subsequent 0s. */
2011 } while (sc->cc == '0');
2014 #ifdef __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__
2016 #endif /* __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__ */
2017 /* At this point, we're ready to start reading digits. */
2019 #define valid_digit(cc,base) (isxdigit(cc) && ((base == 16) || (cc - '0' < base)))
2021 while (valid_digit(sc->cc,base)) { /* Now for significant digits.*/
2022 if (b - buf < MAX_DIGITS) {
2023 nonzero = seendigit = 1; /* Set nonzero too 0s trimmed above. */
2025 #ifdef __UCLIBC_HAS_FLOATS__
2033 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
2034 DONE_GROUPING_DO_UNGET:
2035 #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
2036 if (psfs->conv_num <= CONV_i) { /* integer conversion */
2038 *b = 0; /* null-terminate */
2040 goto DONE; /* No digits! */
2047 _store_inttype(psfs->cur_ptr, psfs->dataargtype,
2048 (uintmax_t) STRTOUIM((char *) buf, NULL, base, 1-usflag));
2053 #ifdef __UCLIBC_HAS_FLOATS__
2055 /* At this point, we have everything left of the decimal point or exponent. */
2056 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
2062 strcpy((char *) b, (char *) sc->decpt);
2067 if (p > sc->fake_decpt) {
2068 goto DONE_DO_UNGET; /* matching failure (read some of decpt) */
2070 goto DO_DIGIT_CHECK;
2076 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
2080 if (sc->cc == '0') {
2081 assert(exp_adjust == 0);
2088 } while (sc->cc == '0');
2092 while (valid_digit(sc->cc,base)) { /* Process fractional digits.*/
2093 if (b - buf < MAX_DIGITS) {
2101 /* Hmm... no decimal point. */
2103 static const unsigned char nan_inf_str[] = "an\0nfinity";
2105 if (base == 16) { /* We had a prefix, but no digits! */
2106 goto DONE_DO_UNGET; /* matching failure */
2109 /* Avoid tolower problems for INFINITY in the tr_TR locale. (yuk)*/
2111 #define TOLOWER(C) ((C)|0x20)
2113 switch (TOLOWER(sc->cc)) {
2115 p = nan_inf_str + 3;
2121 /* No digits and not inf or nan. */
2129 if (TOLOWER(sc->cc) == *p) {
2134 if (!*p || (p == nan_inf_str + 5)) { /* match nan/infinity or inf */
2137 /* Unrecoverable. Even if on 1st char, we had no digits. */
2142 /* If we get here, we had some digits. */
2145 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
2146 ((base == 16) && (((sc->cc)|0x20) == 'p')) ||
2148 (((sc->cc)|0x20) == 'e')
2149 ) { /* Process an exponent. */
2154 goto DONE_DO_UNGET; /* matching failure.. no exponent digits */
2157 if ((sc->cc == '+') || (sc->cc == '-')) { /* Signed exponent? */
2162 #ifdef __UCLIBC_MJN3_ONLY__
2163 #warning TODO: Fix MAX_EXP_DIGITS!
2165 #define MAX_EXP_DIGITS 20
2170 if (sc->cc == '0') {
2175 } while (sc->cc == '0');
2178 while (__isdigit_char_or_EOF(sc->cc)) { /* Exponent digits (base 10).*/
2179 if (seendigit < MAX_EXP_DIGITS) {
2186 if (!seendigit) { /* No digits. Unrecoverable. */
2197 x = __strtofpmax((char *) buf, &e, exp_adjust);
2200 if (psfs->dataargtype & PA_FLAG_LONG_LONG) {
2201 *((long double *)psfs->cur_ptr) = (long double) x;
2202 } else if (psfs->dataargtype & PA_FLAG_LONG) {
2203 *((double *)psfs->cur_ptr) = (double) x;
2205 *((float *)psfs->cur_ptr) = (float) x;
2212 #endif /* __UCLIBC_HAS_FLOATS__ */
2217 return -2; /* Matching failure. */
2221 /**********************************************************************/