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, see
15 * <http://www.gnu.org/licenses/>.
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.
57 #ifdef __UCLIBC_HAS_WCHAR__
58 #include <bits/uClibc_uwchar.h>
61 #endif /* __UCLIBC_HAS_WCHAR__ */
69 #ifdef __UCLIBC_HAS_THREADS__
70 #include <stdio_ext.h>
72 #endif /* __UCLIBC_HAS_THREADS__ */
74 #ifdef __UCLIBC_HAS_FLOATS__
76 #include <bits/uClibc_fpmax.h>
77 #endif /* __UCLIBC_HAS_FLOATS__ */
79 #undef __STDIO_HAS_VSSCANF
80 #if defined(__STDIO_BUFFERS) || !defined(__UCLIBC_HAS_WCHAR__) || defined(__UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__)
81 #define __STDIO_HAS_VSSCANF 1
83 #if !defined(__STDIO_BUFFERS) && !defined(__UCLIBC_HAS_WCHAR__)
86 unsigned char *bufread
; /* pointer to 1 past end of buffer */
87 unsigned char *bufpos
;
93 #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
95 extern unsigned long long
96 _stdlib_strto_ll(register const char * __restrict str
,
97 char ** __restrict endptr
, int base
, int sflag
);
98 #if (ULLONG_MAX == UINTMAX_MAX)
99 #define STRTOUIM(s,e,b,sf) _stdlib_strto_ll(s,e,b,sf)
102 #else /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
105 _stdlib_strto_l(register const char * __restrict str
,
106 char ** __restrict endptr
, int base
, int sflag
);
108 #if (ULONG_MAX == UINTMAX_MAX)
109 #define STRTOUIM(s,e,b,sf) _stdlib_strto_l(s,e,b,sf)
112 #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
115 #error STRTOUIM conversion function is undefined!
118 /**********************************************************************/
120 /* The standards require EOF < 0. */
122 #define __isdigit_char_or_EOF(C) __isdigit_char((C))
124 #define __isdigit_char_or_EOF(C) __isdigit_int((C))
127 /**********************************************************************/
130 int fscanf(FILE * __restrict stream
, const char * __restrict format
, ...)
135 va_start(arg
, format
);
136 rv
= vfscanf(stream
, format
, arg
);
141 libc_hidden_def(fscanf
)
144 /**********************************************************************/
147 int scanf(const char * __restrict format
, ...)
152 va_start(arg
, format
);
153 rv
= vfscanf(stdin
, format
, arg
);
160 /**********************************************************************/
163 #ifdef __STDIO_HAS_VSSCANF
165 int sscanf(const char * __restrict str
, const char * __restrict format
, ...)
170 va_start(arg
, format
);
171 rv
= vsscanf(str
, format
, arg
);
176 libc_hidden_def(sscanf
)
178 #else /* __STDIO_HAS_VSSCANF */
179 #warning Skipping sscanf since no vsscanf!
180 #endif /* __STDIO_HAS_VSSCANF */
183 /**********************************************************************/
186 int vscanf(const char * __restrict format
, va_list arg
)
188 return vfscanf(stdin
, format
, arg
);
192 /**********************************************************************/
195 #ifdef __UCLIBC_MJN3_ONLY__
196 #warning WISHLIST: Implement vsscanf for non-buf and no custom stream case.
197 #endif /* __UCLIBC_MJN3_ONLY__ */
199 #ifdef __STDIO_BUFFERS
201 int vsscanf(const char *sp
, const char *fmt
, va_list ap
)
205 /* __STDIO_STREAM_RESET_GCS(&f); */
206 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
207 f
.__cookie
= &(f
.__filedes
);
209 f
.__gcs
.write
= NULL
;
211 f
.__gcs
.close
= NULL
;
214 f
.__filedes
= __STDIO_STREAM_FAKE_VSSCANF_FILEDES
;
215 f
.__modeflags
= (__FLAG_NARROW
|__FLAG_READONLY
|__FLAG_READING
);
217 #ifdef __UCLIBC_HAS_WCHAR__
218 f
.__ungot_width
[0] = 0;
220 #ifdef __STDIO_MBSTATE
221 __INIT_MBSTATE(&(f
.__state
));
224 #ifdef __UCLIBC_HAS_THREADS__
225 f
.__user_locking
= 1; /* Set user locking. */
226 STDIO_INIT_MUTEX(f
.__lock
);
230 /* Set these last since __bufgetc initialization depends on
231 * __user_locking and only gets set if user locking is on. */
233 f
.__bufpos
= (unsigned char *) ((void *) sp
);
235 f
.__bufend
= f
.__bufstart
+ strlen(sp
);
236 __STDIO_STREAM_ENABLE_GETC(&f
);
237 __STDIO_STREAM_DISABLE_PUTC(&f
);
239 return vfscanf(&f
, fmt
, ap
);
241 libc_hidden_def(vsscanf
)
243 #elif !defined(__UCLIBC_HAS_WCHAR__)
245 int vsscanf(const char *sp
, const char *fmt
, va_list ap
)
249 f
.bufpos
= (unsigned char *) ((void *) sp
);
250 f
.bufread
= f
.bufpos
+ strlen(sp
);
252 /* __STDIO_STREAM_RESET_GCS(&f.f); */
253 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
254 f
.f
.__cookie
= &(f
.f
.__filedes
);
255 f
.f
.__gcs
.read
= NULL
;
256 f
.f
.__gcs
.write
= NULL
;
257 f
.f
.__gcs
.seek
= NULL
;
258 f
.f
.__gcs
.close
= NULL
;
261 f
.f
.__filedes
= __STDIO_STREAM_FAKE_VSSCANF_FILEDES_NB
;
262 f
.f
.__modeflags
= (__FLAG_NARROW
|__FLAG_READONLY
|__FLAG_READING
);
264 /* #ifdef __UCLIBC_HAS_WCHAR__ */
265 /* f.f.__ungot_width[0] = 0; */
267 #ifdef __STDIO_MBSTATE
268 #error __STDIO_MBSTATE is defined!
269 /* __INIT_MBSTATE(&(f.f.__state)); */
272 #ifdef __UCLIBC_HAS_THREADS__
273 f
.f
.__user_locking
= 1; /* Set user locking. */
274 STDIO_INIT_MUTEX(f
.f
.__lock
);
276 f
.f
.__nextopen
= NULL
;
278 return vfscanf(&f
.f
, fmt
, ap
);
280 libc_hidden_def(vsscanf
)
282 #elif defined(__UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__)
284 int vsscanf(const char *sp
, const char *fmt
, va_list ap
)
289 if ((f
= fmemopen((char *)sp
, strlen(sp
), "r")) != NULL
) {
290 rv
= vfscanf(f
, fmt
, ap
);
296 libc_hidden_def(vsscanf
)
299 #warning Skipping vsscanf since no buffering, no custom streams, and wchar enabled!
300 #ifdef __STDIO_HAS_VSSCANF
301 #error WHOA! __STDIO_HAS_VSSCANF is defined!
306 /**********************************************************************/
309 int fwscanf(FILE * __restrict stream
, const wchar_t * __restrict format
, ...)
314 va_start(arg
, format
);
315 rv
= vfwscanf(stream
, format
, arg
);
322 /**********************************************************************/
325 int wscanf(const wchar_t * __restrict format
, ...)
330 va_start(arg
, format
);
331 rv
= vfwscanf(stdin
, format
, arg
);
338 /**********************************************************************/
341 #ifdef __STDIO_BUFFERS
343 int swscanf(const wchar_t * __restrict str
, const wchar_t * __restrict format
,
349 va_start(arg
, format
);
350 rv
= vswscanf(str
, format
, arg
);
355 #else /* __STDIO_BUFFERS */
356 #warning Skipping swscanf since no buffering!
357 #endif /* __STDIO_BUFFERS */
360 /**********************************************************************/
363 int vwscanf(const wchar_t * __restrict format
, va_list arg
)
365 return vfwscanf(stdin
, format
, arg
);
369 /**********************************************************************/
372 #ifdef __STDIO_BUFFERS
374 int vswscanf(const wchar_t * __restrict str
, const wchar_t * __restrict format
,
380 f
.__bufpos
= (unsigned char *) str
;
382 f
.__bufend
= (unsigned char *)(str
+ wcslen(str
));
383 __STDIO_STREAM_DISABLE_GETC(&f
);
384 __STDIO_STREAM_DISABLE_PUTC(&f
);
386 /* __STDIO_STREAM_RESET_GCS(&f); */
387 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
388 f
.__cookie
= &(f
.__filedes
);
390 f
.__gcs
.write
= NULL
;
392 f
.__gcs
.close
= NULL
;
395 f
.__filedes
= __STDIO_STREAM_FAKE_VSWSCANF_FILEDES
;
396 f
.__modeflags
= (__FLAG_WIDE
|__FLAG_READONLY
|__FLAG_READING
);
398 #ifdef __UCLIBC_HAS_WCHAR__
399 f
.__ungot_width
[0] = 0;
400 #endif /* __UCLIBC_HAS_WCHAR__ */
401 #ifdef __STDIO_MBSTATE
402 __INIT_MBSTATE(&(f
.__state
));
403 #endif /* __STDIO_MBSTATE */
405 #ifdef __UCLIBC_HAS_THREADS__
406 f
.__user_locking
= 1; /* Set user locking. */
407 STDIO_INIT_MUTEX(f
.__lock
);
411 return vfwscanf(&f
, format
, arg
);
413 libc_hidden_def(vswscanf
)
414 #else /* __STDIO_BUFFERS */
415 #warning Skipping vswscanf since no buffering!
416 #endif /* __STDIO_BUFFERS */
419 /**********************************************************************/
420 /**********************************************************************/
424 /* float layout 0123456789012345678901 repeat n for "l[" */
425 #define SPEC_CHARS "npxXoudifFeEgGaACSnmcs["
426 /* npxXoudif eEgG CS cs[ */
427 /* NOTE: the 'm' flag must come before any convs that support it */
429 /* NOTE: Ordering is important! The CONV_{C,S,LEFTBRACKET} must map
430 simply to their lowercase equivalents. */
435 CONV_x
, CONV_X
, CONV_o
, CONV_u
, CONV_d
, CONV_i
,
436 CONV_f
, CONV_F
, CONV_e
, CONV_E
, CONV_g
, CONV_G
, CONV_a
, CONV_A
,
437 CONV_C
, CONV_S
, CONV_LEFTBRACKET
, CONV_m
, CONV_c
, CONV_s
, CONV_leftbracket
,
438 CONV_percent
, CONV_whitespace
/* not in SPEC_* and no flags */
441 #ifdef __UCLIBC_HAS_FLOATS__
442 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
443 /* p x X o u d i f F e E g G a A */
444 #define SPEC_BASE { 16, 16, 16, 8, 10, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
446 /* p x X o u d i f F e E g G a A */
447 #define SPEC_BASE { 16, 16, 16, 8, 10, 10, 0, 10, 10, 10, 10, 10, 10, 10, 10 }
449 #else /* __UCLIBC_HAS_FLOATS__ */
450 /* p x X o u d i f F e E g G a A */
451 #define SPEC_BASE { 16, 16, 16, 8, 10, 10, 0 }
452 #endif /* __UCLIBC_HAS_FLOATS__ */
454 #ifdef __UCLIBC_MJN3_ONLY__
457 #warning CONSIDER: Add a '0' flag to eat 0 padding when grouping?
459 #endif /* __UCLIBC_MJN3_ONLY__ */
461 #define SPEC_FLAGS "*'I"
464 FLAG_SURPRESS
= 0x10, /* MUST BE 1ST!! See DO_FLAGS. */
465 FLAG_THOUSANDS
= 0x20,
466 FLAG_I18N
= 0x40, /* only works for d, i, u */
467 FLAG_MALLOC
= 0x80, /* only works for c, s, S, and [ (and l[)*/
471 #define SPEC_RANGES { CONV_n, CONV_p, CONV_i, CONV_A, \
472 CONV_C, CONV_LEFTBRACKET, \
473 CONV_c, CONV_leftbracket }
475 /* Note: We treat L and ll as synonymous... for ints and floats. */
477 #define SPEC_ALLOWED_FLAGS { \
478 /* n */ (0x0f|FLAG_SURPRESS), \
479 /* p */ ( 0|FLAG_SURPRESS), \
480 /* oxXudi */ (0x0f|FLAG_SURPRESS|FLAG_THOUSANDS|FLAG_I18N), \
481 /* fFeEgGaA */ (0x0c|FLAG_SURPRESS|FLAG_THOUSANDS|FLAG_I18N), \
482 /* C */ ( 0|FLAG_SURPRESS), \
483 /* S and l[ */ ( 0|FLAG_SURPRESS|FLAG_MALLOC), \
484 /* c */ (0x04|FLAG_SURPRESS|FLAG_MALLOC), \
485 /* s and [ */ (0x04|FLAG_SURPRESS|FLAG_MALLOC), \
489 /**********************************************************************/
491 * In order to ease translation to what arginfo and _print_info._flags expect,
492 * we map: 0:int 1:char 2:longlong 4:long 8:short
493 * and then _flags |= (((q << 7) + q) & 0x701) and argtype |= (_flags & 0x701)
496 /* TODO -- Fix the table below to take into account stdint.h. */
497 /* #ifndef LLONG_MAX */
498 /* #error fix QUAL_CHARS for no long long! Affects 'L', 'j', 'q', 'll'. */
500 /* #if LLONG_MAX != INTMAX_MAX */
501 /* #error fix QUAL_CHARS intmax_t entry 'j'! */
506 #error PDS already defined!
509 #error SS already defined!
512 #error IMS already defined!
515 #if PTRDIFF_MAX == INT_MAX
517 #elif PTRDIFF_MAX == LONG_MAX
519 #elif defined(LLONG_MAX) && (PTRDIFF_MAX == LLONG_MAX)
522 #error fix QUAL_CHARS ptrdiff_t entry 't'!
525 #if SIZE_MAX == UINT_MAX
527 #elif SIZE_MAX == ULONG_MAX
529 #elif defined(LLONG_MAX) && (SIZE_MAX == ULLONG_MAX)
532 #error fix QUAL_CHARS size_t entries 'z', 'Z'!
535 #if INTMAX_MAX == INT_MAX
537 #elif INTMAX_MAX == LONG_MAX
539 #elif defined(LLONG_MAX) && (INTMAX_MAX == LLONG_MAX)
542 #error fix QUAL_CHARS intmax_t entry 'j'!
545 #define QUAL_CHARS { \
546 /* j:(u)intmax_t z:(s)size_t t:ptrdiff_t \0:int q:long_long */ \
547 'h', 'l', 'L', 'j', 'z', 't', 'q', 0, \
548 2, 4, 8, IMS, SS, PDS, 8, 0, /* TODO -- fix!!! */ \
553 /**********************************************************************/
556 /* FIXME: "warning: the right operand of ">" changes sign when promoted" */
558 #error Unfortunately, we currently need wint_t to be able to store EOF. Sorry.
562 #define Wchar wchar_t
563 #define Wuchar __uwchar_t
564 #define ISSPACE(C) iswspace((C))
565 #define VFSCANF vfwscanf
566 #define GETC(SC) (SC)->sc_getc((SC))
568 typedef unsigned char __uchar_t
;
572 #define Wuchar __uchar_t
573 #define ISSPACE(C) isspace((C))
574 #define VFSCANF vfscanf
575 #ifdef __UCLIBC_HAS_WCHAR__
576 #define GETC(SC) (SC)->sc_getc((SC))
577 #else /* __UCLIBC_HAS_WCHAR__ */
578 #define GETC(SC) getc_unlocked((SC)->fp)
579 #endif /* __UCLIBC_HAS_WCHAR__ */
589 #ifdef __UCLIBC_HAS_WCHAR__
590 wchar_t app_ungot
; /* Match FILE struct member type. */
591 unsigned char ungot_wchar_width
;
592 #else /* __UCLIBC_HAS_WCHAR__ */
593 unsigned char app_ungot
; /* Match FILE struct member type. */
594 #endif /* __UCLIBC_HAS_WCHAR__ */
598 #ifdef __UCLIBC_HAS_WCHAR__
599 char ungot_wflag
; /* vfwscanf */
600 char mb_fail
; /* vfscanf */
601 mbstate_t mbstate
; /* vfscanf */
603 wint_t ungot_wchar
; /* to support __scan_getc */
604 int (*sc_getc
)(struct scan_cookie
*);
605 #endif /* __UCLIBC_HAS_WCHAR__ */
607 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
608 const char *grouping
;
609 const unsigned char *thousands_sep
;
611 #ifdef __UCLIBC_HAS_WCHAR__
612 wchar_t thousands_sep_wc
;
613 #endif /* __UCLIBC_HAS_WCHAR__ */
614 #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
616 #ifdef __UCLIBC_HAS_FLOATS__
617 const unsigned char *decpt
;
619 #ifdef __UCLIBC_HAS_WCHAR__
621 #endif /* __UCLIBC_HAS_WCHAR__ */
622 const unsigned char *fake_decpt
;
623 #endif /* __UCLIBC_HAS_FLOATS__ */
628 #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
630 #warning NL_ARGMAX > 10, and space is allocated on the stack for positional args.
632 void *pos_args
[NL_ARGMAX
];
633 int num_pos_args
; /* Must start at -1. */
635 #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
637 const unsigned char *fmt
;
638 int cnt
, dataargtype
, conv_num
, max_width
;
639 unsigned char store
, flags
;
640 } psfs_t
; /* parse scanf format state */
643 /**********************************************************************/
644 /**********************************************************************/
646 extern void __init_scan_cookie(register struct scan_cookie
*sc
,
647 register FILE *fp
) attribute_hidden
;
648 extern int __scan_getc(register struct scan_cookie
*sc
) attribute_hidden
;
649 extern void __scan_ungetc(register struct scan_cookie
*sc
) attribute_hidden
;
651 #ifdef __UCLIBC_HAS_FLOATS__
652 extern int __scan_strtold(long double *ld
, struct scan_cookie
*sc
);
653 #endif /* __UCLIBC_HAS_FLOATS__ */
655 extern int __psfs_parse_spec(psfs_t
*psfs
) attribute_hidden
;
656 extern int __psfs_do_numeric(psfs_t
*psfs
, struct scan_cookie
*sc
) attribute_hidden
;
658 /**********************************************************************/
659 #ifdef L___scan_cookie
661 #ifdef __UCLIBC_MJN3_ONLY__
662 #warning TODO: Remove dependence on decpt_str and fake_decpt in stub locale mode.
664 #ifndef __UCLIBC_HAS_LOCALE__
665 static const char decpt_str
[] = ".";
668 void attribute_hidden
__init_scan_cookie(register struct scan_cookie
*sc
,
674 sc
->app_ungot
= ((fp
->__modeflags
& __FLAG_UNGOT
) ? fp
->__ungot
[1] : 0);
675 #ifdef __UCLIBC_HAS_WCHAR__
676 sc
->ungot_wflag
= 0; /* vfwscanf */
678 #endif /* __UCLIBC_HAS_WCHAR__ */
680 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
681 if (*(sc
->grouping
= __UCLIBC_CURLOCALE
->grouping
)) {
682 sc
->thousands_sep
= (const unsigned char *) __UCLIBC_CURLOCALE
->thousands_sep
;
683 sc
->tslen
= __UCLIBC_CURLOCALE
->thousands_sep_len
;
684 #ifdef __UCLIBC_HAS_WCHAR__
685 sc
->thousands_sep_wc
= __UCLIBC_CURLOCALE
->thousands_sep_wc
;
686 #endif /* __UCLIBC_HAS_WCHAR__ */
688 #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
690 #ifdef __UCLIBC_HAS_FLOATS__
691 #ifdef __UCLIBC_HAS_LOCALE__
692 sc
->decpt
= (const unsigned char *) __UCLIBC_CURLOCALE
->decimal_point
;
693 sc
->decpt_len
= __UCLIBC_CURLOCALE
->decimal_point_len
;
694 #else /* __UCLIBC_HAS_LOCALE__ */
695 sc
->fake_decpt
= sc
->decpt
= (unsigned char *) decpt_str
;
697 #endif /* __UCLIBC_HAS_LOCALE__ */
698 #ifdef __UCLIBC_HAS_WCHAR__
699 #ifdef __UCLIBC_HAS_LOCALE__
700 sc
->decpt_wc
= __UCLIBC_CURLOCALE
->decimal_point_wc
;
704 #endif /* __UCLIBC_HAS_WCHAR__ */
705 #endif /* __UCLIBC_HAS_FLOATS__ */
709 int attribute_hidden
__scan_getc(register struct scan_cookie
*sc
)
713 #ifdef __UCLIBC_HAS_WCHAR__
714 assert(!sc
->mb_fail
);
715 #endif /* __UCLIBC_HAS_WCHAR__ */
719 if (--sc
->width
< 0) {
724 if (sc
->ungot_flag
== 0) {
725 #if !defined(__STDIO_BUFFERS) && !defined(__UCLIBC_HAS_WCHAR__)
726 if (!__STDIO_STREAM_IS_FAKE_VSSCANF_NB(sc
->fp
)) {
729 __FILE_vsscanf
*fv
= (__FILE_vsscanf
*)(sc
->fp
);
730 if (fv
->bufpos
< fv
->bufread
) {
734 sc
->fp
->__modeflags
|= __FLAG_EOF
;
742 if ((c
= GETC(sc
)) == EOF
) {
749 assert(sc
->ungot_flag
== 1);
754 return sc
->cc
= sc
->ungot_char
;
757 void attribute_hidden
__scan_ungetc(register struct scan_cookie
*sc
)
760 if (sc
->ungot_flag
== 2) { /* last was EOF */
762 sc
->cc
= sc
->ungot_char
;
763 } else if (sc
->ungot_flag
== 0) {
772 /**********************************************************************/
773 #ifdef L___psfs_parse_spec
776 static const unsigned char spec_flags
[] = SPEC_FLAGS
;
777 #endif /* SPEC_FLAGS */
778 static const unsigned char spec_chars
[] = SPEC_CHARS
;
779 static const unsigned char qual_chars
[] = QUAL_CHARS
;
780 static const unsigned char spec_ranges
[] = SPEC_RANGES
;
781 static const unsigned short spec_allowed
[] = SPEC_ALLOWED_FLAGS
;
783 int attribute_hidden
__psfs_parse_spec(register psfs_t
*psfs
)
785 const unsigned char *p
;
786 const unsigned char *fmt0
= psfs
->fmt
;
791 #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
792 unsigned char fail
= 0;
794 i
= 0; /* Do this here to avoid a warning. */
796 if (!__isdigit_char(*psfs
->fmt
)) { /* Not a positional arg. */
801 /* parse the positional arg (or width) value */
803 if (i
<= ((INT_MAX
- 9)/10)) {
804 i
= (i
* 10) + (*psfs
->fmt
++ - '0');
806 } while (__isdigit_char(*psfs
->fmt
));
808 if (*psfs
->fmt
!= '$') { /* This is a max field width. */
809 if (psfs
->num_pos_args
>= 0) { /* Already saw a pos arg! */
813 psfs
->num_pos_args
= -2;
816 ++psfs
->fmt
; /* Advance past '$'. */
817 #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
819 #if defined(SPEC_FLAGS) || (defined(NL_ARGMAX) && (NL_ARGMAX > 0))
821 #endif /* defined(SPEC_FLAGS) || (defined(NL_ARGMAX) && (NL_ARGMAX > 0)) */
826 if (*p
== *psfs
->fmt
) {
834 if (psfs
->flags
& FLAG_SURPRESS
) { /* Suppress assignment. */
838 #else /* SPEC_FLAGS */
839 if (*psfs
->fmt
== '*') { /* Suppress assignment. */
844 #endif /* SPEC_FLAGS */
847 #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
849 /* Must be a non-positional arg */
850 if (psfs
->num_pos_args
>= 0) { /* Already saw a pos arg! */
853 psfs
->num_pos_args
= -2;
855 if ((psfs
->num_pos_args
== -2) || (((unsigned int)(--i
)) >= NL_ARGMAX
)) {
856 /* Already saw a non-pos arg or (0-based) num too large. */
859 psfs
->cur_pos_arg
= i
;
861 #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
864 for (i
= 0 ; __isdigit_char(*psfs
->fmt
) ; ) {
865 if (i
<= ((INT_MAX
- 9)/10)) {
866 i
= (i
* 10) + (*psfs
->fmt
++ - '0');
871 #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
873 #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
876 if (*psfs
->fmt
== *p
) {
881 if ((p
- qual_chars
< 2) && (*psfs
->fmt
== *p
)) {
882 p
+= ((sizeof(qual_chars
)-2) / 2);
885 psfs
->dataargtype
= ((int)(p
[(sizeof(qual_chars
)-2) / 2])) << 8;
887 #ifdef __UCLIBC_MJN3_ONLY__
888 #warning CONSIDER: Should we validate that psfs->max_width > 0 in __psfs_parse_spec()? It would avoid whitespace consumption...
889 #warning CONSIDER: Should INT_MAX be a valid width (%c/%C)? See __psfs_parse_spec().
890 #endif /* __UCLIBC_MJN3_ONLY__ */
894 if (*psfs
->fmt
== *p
) {
895 int p_m_spec_chars
= p
- spec_chars
;
898 (psfs
->fmt
[1] == '[' || psfs
->fmt
[1] == 'c' ||
899 /* Assumes ascii for 's' and 'S' test. */
900 (psfs
->fmt
[1] | 0x20) == 's'))
903 psfs
->flags
|= FLAG_MALLOC
;
906 continue; /* The related conversions follow 'm'. */
909 for (p
= spec_ranges
; p_m_spec_chars
> *p
; ++p
) {}
910 if (((psfs
->dataargtype
>> 8) | psfs
->flags
)
911 & ~spec_allowed
[(int)(p
- spec_ranges
)]
916 if (p_m_spec_chars
== CONV_p
) {
917 /* a pointer has the same size as 'long int' */
918 psfs
->dataargtype
= PA_FLAG_LONG
;
919 } else if ((p_m_spec_chars
>= CONV_c
)
920 && (psfs
->dataargtype
& PA_FLAG_LONG
)) {
921 p_m_spec_chars
-= CONV_c
- CONV_C
; /* lc -> C, ls -> S, l[ -> ?? */
924 psfs
->conv_num
= p_m_spec_chars
;
925 return psfs
->fmt
- fmt0
;
938 /**********************************************************************/
939 #if defined(L_vfscanf) || defined(L_vfwscanf)
941 #ifdef __UCLIBC_HAS_WCHAR__
943 static int sc_getc(register struct scan_cookie
*sc
)
945 return (getc_unlocked
)(sc
->fp
); /* Disable the macro. */
948 static int scan_getwc(register struct scan_cookie
*sc
)
955 if (--sc
->width
< 0) {
960 width
= sc
->width
; /* Preserve width. */
961 sc
->width
= INT_MAX
; /* MB_CUR_MAX can invoke a function. */
963 assert(!sc
->mb_fail
);
966 while (__scan_getc(sc
) >= 0) {
969 r
= mbrtowc(wc
, b
, 1, &sc
->mbstate
);
970 if (((ssize_t
) r
) >= 0) { /* Successful completion of a wc. */
973 } else if (r
== ((size_t) -2)) {
974 /* Potentially valid but incomplete. */
980 if (r
== ((size_t)(-3))) { /* EOF or ERROR on first read */
984 /* If we reach here, either r == ((size_t)-1) and
985 * mbrtowc set errno to EILSEQ, or r == ((size_t)-2)
986 * and stream is in an error state or at EOF with a
987 * partially complete wchar. */
988 __set_errno(EILSEQ
); /* In case of incomplete conversion. */
993 sc
->width
= width
; /* Restore width. */
995 return (int)((ssize_t
) r
);
998 #endif /* L_vfscanf */
1002 /* This gets called by __scan_getc. __scan_getc is called by vfwscanf
1003 * when the next wide char is expected to be valid ascii (digits).
1005 static int sc_getc(register struct scan_cookie
*sc
)
1009 if (__STDIO_STREAM_IS_FAKE_VSWSCANF(sc
->fp
)) {
1010 if (sc
->fp
->__bufpos
< sc
->fp
->__bufend
) {
1011 wc
= *((wchar_t *)(sc
->fp
->__bufpos
));
1012 sc
->fp
->__bufpos
+= sizeof(wchar_t);
1014 sc
->fp
->__modeflags
|= __FLAG_EOF
;
1017 } else if ((wc
= fgetwc_unlocked(sc
->fp
)) == WEOF
) {
1021 sc
->ungot_wflag
= 1;
1022 sc
->ungot_wchar
= wc
;
1023 sc
->ungot_wchar_width
= sc
->fp
->__ungot_width
[0];
1025 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
1026 if (wc
== sc
->thousands_sep_wc
) {
1029 #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
1030 #ifdef __UCLIBC_HAS_FLOATS__
1031 if (wc
== sc
->decpt_wc
) {
1034 #endif /* __UCLIBC_HAS_FLOATS__ */
1035 sc
->wc
= sc
->ungot_char
= wc
;
1040 static int scan_getwc(register struct scan_cookie
*sc
)
1046 if (--sc
->width
< 0) {
1047 sc
->ungot_flag
|= 2;
1051 if (sc
->ungot_flag
== 0) {
1052 if (__STDIO_STREAM_IS_FAKE_VSWSCANF(sc
->fp
)) {
1053 if (sc
->fp
->__bufpos
< sc
->fp
->__bufend
) {
1054 wc
= *((wchar_t *)(sc
->fp
->__bufpos
));
1055 sc
->fp
->__bufpos
+= sizeof(wchar_t);
1057 sc
->ungot_flag
|= 2;
1060 } else if ((wc
= fgetwc_unlocked(sc
->fp
)) == WEOF
) {
1061 sc
->ungot_flag
|= 2;
1064 sc
->ungot_wflag
= 1;
1065 sc
->ungot_char
= wc
;
1066 sc
->ungot_wchar_width
= sc
->fp
->__ungot_width
[0];
1068 assert(sc
->ungot_flag
== 1);
1073 sc
->wc
= sc
->ungot_char
;
1079 #endif /* L_vfwscanf */
1080 #endif /* __UCLIBC_HAS_WCHAR__ */
1082 static __inline
void kill_scan_cookie(register struct scan_cookie
*sc
)
1086 if (sc
->ungot_flag
& 1) {
1087 #if !defined(__STDIO_BUFFERS) && !defined(__UCLIBC_HAS_WCHAR__)
1088 if (!__STDIO_STREAM_IS_FAKE_VSSCANF_NB(sc
->fp
)) {
1089 ungetc(sc
->ungot_char
, sc
->fp
);
1092 ungetc(sc
->ungot_char
, sc
->fp
);
1094 /* Deal with distiction between user and scanf ungots. */
1095 if (sc
->nread
== 0) { /* Only one char was read... app ungot? */
1096 sc
->fp
->__ungot
[1] = sc
->app_ungot
; /* restore ungot state. */
1098 sc
->fp
->__ungot
[1] = 0;
1104 if ((sc
->ungot_flag
& 1) && (sc
->ungot_wflag
& 1)
1105 && !__STDIO_STREAM_IS_FAKE_VSWSCANF(sc
->fp
)
1106 && (sc
->fp
->__state
.__mask
== 0)
1108 ungetwc(sc
->ungot_char
, sc
->fp
);
1109 /* Deal with distiction between user and scanf ungots. */
1110 if (sc
->nread
== 0) { /* Only one char was read... app ungot? */
1111 sc
->fp
->__ungot
[1] = sc
->app_ungot
; /* restore ungot state. */
1113 sc
->fp
->__ungot
[1] = 0;
1115 sc
->fp
->__ungot_width
[1] = sc
->ungot_wchar_width
;
1122 int VFSCANF (FILE *__restrict fp
, const Wchar
*__restrict format
, va_list arg
)
1130 #endif /* L_vfwscanf */
1132 #if defined(__UCLIBC_HAS_LOCALE__) && !defined(L_vfwscanf) || !defined(L_vfscanf)
1136 struct scan_cookie sc
;
1140 #ifdef __UCLIBC_MJN3_ONLY__
1141 #warning TODO: Fix MAX_DIGITS. We do not do binary, so...!
1143 #define MAX_DIGITS 65 /* Allow one leading 0. */
1144 unsigned char buf
[MAX_DIGITS
+2];
1146 unsigned char scanset
[UCHAR_MAX
+ 1];
1147 unsigned char invert
; /* Careful! Meaning changes. */
1148 #endif /* L_vfscanf */
1150 unsigned char zero_conversions
= 1;
1151 __STDIO_AUTO_THREADLOCK_VAR
;
1153 #ifdef __UCLIBC_MJN3_ONLY__
1154 #warning TODO: Make checking of the format string in C locale an option.
1156 /* To support old programs, don't check mb validity if in C locale. */
1157 #if defined(__UCLIBC_HAS_LOCALE__) && !defined(L_vfwscanf)
1158 /* ANSI/ISO C99 requires format string to be a valid multibyte string
1159 * beginning and ending in its initial shift state. */
1160 if (__UCLIBC_CURLOCALE
->encoding
!= __ctype_encoding_7_bit
) {
1161 const char *p
= format
;
1162 mbstate
.__mask
= 0; /* Initialize the mbstate. */
1163 if (mbsrtowcs(NULL
, &p
, SIZE_MAX
, &mbstate
) == ((size_t)(-1))) {
1164 __set_errno(EINVAL
); /* Format string is invalid. */
1168 #endif /* defined(__UCLIBC_HAS_LOCALE__) && !defined(L_vfwscanf) */
1170 #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
1171 psfs
.num_pos_args
= -1; /* Must start at -1. */
1172 /* Initialize positional arg ptrs to NULL. */
1173 memset(psfs
.pos_args
, 0, sizeof(psfs
.pos_args
));
1174 #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
1176 __STDIO_AUTO_THREADLOCK(fp
);
1178 __STDIO_STREAM_VALIDATE(fp
);
1180 __init_scan_cookie(&sc
,fp
);
1181 #ifdef __UCLIBC_HAS_WCHAR__
1182 sc
.sc_getc
= sc_getc
;
1183 sc
.ungot_wchar_width
= sc
.fp
->__ungot_width
[1];
1187 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
1189 sc
.thousands_sep
= (const unsigned char *) ",";
1192 #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
1194 #ifdef __UCLIBC_HAS_FLOATS__
1195 sc
.fake_decpt
= (const unsigned char *) ".";
1196 #endif /* __UCLIBC_HAS_FLOATS__ */
1198 #else /* L_vfwscanf */
1200 #ifdef __UCLIBC_HAS_FLOATS__
1201 sc
.fake_decpt
= sc
.decpt
;
1202 #endif /* __UCLIBC_HAS_FLOATS__ */
1204 #endif /* L_vfwscanf */
1206 #endif /* __UCLIBC_HAS_WCHAR__ */
1209 /* Note: If we ever wanted to support non-nice codesets, we
1210 * would really need to do a mb->wc conversion here in the
1211 * vfscanf case. Related changes would have to be made in
1212 * the code that follows... basicly wherever fmt appears. */
1213 for (fmt
= (const Wuchar
*) format
; *fmt
; /* ++fmt */) {
1218 psfs
.cur_ptr
= NULL
; /* Debugging aid. */
1222 sc
.ungot_flag
&= 1; /* Clear (possible fake) EOF. */
1223 sc
.width
= psfs
.max_width
= INT_MAX
;
1225 /* Note: According to the standards, vfscanf does use isspace
1226 * here. So, if we did a mb->wc conversion, we would have to do
1228 * ((((__uwchar_t)wc) < UCHAR_MAX) && isspace(wc))
1229 * because wc might not be in the allowed domain. */
1230 if (ISSPACE(*fmt
)) {
1233 } while (ISSPACE(*fmt
));
1235 psfs
.conv_num
= CONV_whitespace
;
1239 if (*fmt
== '%') { /* Conversion specification. */
1240 if (*++fmt
== '%') { /* Remember, '%' eats whitespace too. */
1241 /* Note: The standard says no conversion occurs.
1242 * So do not reset zero_conversions flag. */
1243 psfs
.conv_num
= CONV_percent
;
1250 #else /* L_vfscanf */
1252 const __uwchar_t
*wf
= fmt
;
1255 while (*wf
&& __isascii(*wf
) && (b
< buf
+ sizeof(buf
) - 1)) {
1259 if (b
== buf
) { /* Bad conversion specifier! */
1263 #endif /* L_vfscanf */
1264 if ((i
= __psfs_parse_spec(&psfs
)) < 0) { /* Bad conversion specifier! */
1270 #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
1271 if (psfs
.num_pos_args
== -2) {
1272 psfs
.cur_ptr
= va_arg(arg
, void *);
1274 while (psfs
.cur_pos_arg
> psfs
.num_pos_args
) {
1275 psfs
.pos_args
[++psfs
.num_pos_args
] = va_arg(arg
, void *);
1277 psfs
.cur_ptr
= psfs
.pos_args
[psfs
.cur_pos_arg
];
1279 #else /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
1280 psfs
.cur_ptr
= va_arg(arg
, void *);
1281 #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
1285 /* First, consume white-space if not n, c, [, C, or l[. */
1286 if ((((1L << CONV_n
)|(1L << CONV_C
)|(1L << CONV_c
)
1287 |(1L << CONV_LEFTBRACKET
)|(1L << CONV_leftbracket
))
1288 & (1L << psfs
.conv_num
)) == 0
1291 while ((__scan_getc(&sc
) >= 0)
1294 #else /* L_vfscanf */
1296 #endif /* L_vfscanf */
1299 if (psfs
.conv_num
== CONV_whitespace
) {
1304 sc
.width
= psfs
.max_width
; /* Now limit the max width. */
1306 if (sc
.width
== 0) { /* 0 width is forbidden. */
1311 if (psfs
.conv_num
== CONV_percent
) {
1315 if (psfs
.conv_num
== CONV_n
) {
1316 #ifdef __UCLIBC_MJN3_ONLY__
1317 #warning CONSIDER: Should %n count as a conversion as far as EOF return value?
1319 /* zero_conversions = 0; */
1321 _store_inttype(psfs
.cur_ptr
, psfs
.dataargtype
,
1322 (uintmax_t) sc
.nread
);
1327 if (psfs
.conv_num
<= CONV_A
) { /* pointer, integer, or float spec */
1328 int r
= __psfs_do_numeric(&psfs
, &sc
);
1330 if (sc
.ungot_wflag
== 1) { /* fix up '?', '.', and ',' hacks */
1331 sc
.cc
= sc
.ungot_char
= sc
.ungot_wchar
;
1334 if (r
!= -1) { /* Either success or a matching failure. */
1335 zero_conversions
= 0;
1343 /* Do string conversions here since they are not common code. */
1349 #ifdef __UCLIBC_HAS_WCHAR__
1350 (psfs
.conv_num
>= CONV_LEFTBRACKET
)
1351 #else /* __UCLIBC_HAS_WCHAR__ */
1352 (psfs
.conv_num
>= CONV_c
)
1353 #endif /* __UCLIBC_HAS_WCHAR__ */
1355 b
= (psfs
.store
? ((unsigned char *) psfs
.cur_ptr
) : buf
);
1358 if (psfs
.conv_num
== CONV_c
) {
1359 if (sc
.width
== INT_MAX
) {
1363 while (__scan_getc(&sc
) >= 0) {
1364 zero_conversions
= 0;
1369 if (sc
.width
> 0) { /* Failed to read all required. */
1372 psfs
.cnt
+= psfs
.store
;
1376 if (psfs
.conv_num
== CONV_s
) {
1377 /* We might have to handle the allocation ourselves */
1379 /* With 'm', we actually got a pointer to a pointer */
1380 unsigned char **ptr
= (void *)b
;
1383 if (psfs
.flags
& FLAG_MALLOC
) {
1389 /* Yes, believe it or not, a %s conversion can store nuls. */
1390 while ((__scan_getc(&sc
) >= 0) && !isspace(sc
.cc
)) {
1391 zero_conversions
= 0;
1393 /* Pick a size that won't trigger a lot of
1394 * mallocs early on ... */
1396 b
= realloc(b
, len
+ 1);
1403 if (psfs
.flags
& FLAG_MALLOC
)
1405 /* The code below takes care of terminating NUL */
1408 #ifdef __UCLIBC_HAS_WCHAR__
1409 assert((psfs
.conv_num
== CONV_LEFTBRACKET
) || \
1410 (psfs
.conv_num
== CONV_leftbracket
));
1411 #else /* __UCLIBC_HAS_WCHAR__ */
1412 assert((psfs
.conv_num
== CONV_leftbracket
));
1413 #endif /* __UCLIBC_HAS_WCHAR__ */
1417 if (*++fmt
== '^') {
1421 memset(scanset
, invert
, sizeof(scanset
));
1425 scanset
[(int)(']')] = invert
;
1429 while (*fmt
!= ']') {
1430 if (!*fmt
) { /* No closing ']'. */
1433 if ((*fmt
== '-') && (fmt
[1] != ']')
1434 && (fmt
[-1] < fmt
[1]) /* sorted? */
1438 /* Note: scanset[i] should already have been done
1439 * in the previous iteration. */
1441 scanset
[++i
] = invert
;
1443 /* Safe to fall through, and a bit smaller. */
1446 scanset
[(int) *fmt
] = invert
;
1450 #ifdef __UCLIBC_HAS_WCHAR__
1451 if (psfs
.conv_num
== CONV_LEFTBRACKET
) {
1452 goto DO_LEFTBRACKET
;
1454 #endif /* __UCLIBC_HAS_WCHAR__ */
1457 while (__scan_getc(&sc
) >= 0) {
1458 zero_conversions
= 0;
1459 if (!scanset
[sc
.cc
]) {
1467 /* Common tail for processing of %s and %[. */
1470 if (fail
) { /* nothing stored! */
1473 *b
= 0; /* Nul-terminate string. */
1474 psfs
.cnt
+= psfs
.store
;
1478 #ifdef __UCLIBC_HAS_WCHAR__
1479 DO_LEFTBRACKET
: /* Need to do common wide init. */
1480 if (psfs
.conv_num
>= CONV_C
) {
1484 sc
.mbstate
.__mask
= 0;
1486 wb
= (psfs
.store
? ((wchar_t *) psfs
.cur_ptr
) : wbuf
);
1489 if (psfs
.conv_num
== CONV_C
) {
1490 if (sc
.width
== INT_MAX
) {
1494 while (scan_getwc(&sc
) >= 0) {
1495 zero_conversions
= 0;
1496 assert(sc
.width
>= 0);
1502 if (sc
.width
> 0) { /* Failed to read all required. */
1505 psfs
.cnt
+= psfs
.store
;
1510 if (psfs
.conv_num
== CONV_S
) {
1511 /* Yes, believe it or not, a %s conversion can store nuls. */
1512 while (scan_getwc(&sc
) >= 0) {
1513 zero_conversions
= 0;
1514 if ((((__uwchar_t
)(sc
.wc
)) <= UCHAR_MAX
) && isspace(sc
.wc
)) {
1522 assert(psfs
.conv_num
== CONV_LEFTBRACKET
);
1524 while (scan_getwc(&sc
) >= 0) {
1525 zero_conversions
= 0;
1526 if (((__uwchar_t
) sc
.wc
) <= UCHAR_MAX
) {
1527 if (!scanset
[sc
.wc
]) {
1530 } else if (invert
) {
1538 /* Common tail for processing of %ls and %l[. */
1541 if (fail
|| sc
.mb_fail
) { /* Nothing stored or mb error. */
1544 *wb
= 0; /* Nul-terminate string. */
1545 psfs
.cnt
+= psfs
.store
;
1550 #endif /* __UCLIBC_HAS_WCHAR__ */
1551 #else /* L_vfscanf */
1553 if (psfs
.conv_num
>= CONV_C
) {
1556 if (psfs
.conv_num
>= CONV_c
) {
1557 mbstate
.__mask
= 0; /* Initialize the mbstate. */
1559 b
= (unsigned char *) psfs
.cur_ptr
;
1563 wb
= (wchar_t *) psfs
.cur_ptr
;
1569 if ((psfs
.conv_num
== CONV_C
) || (psfs
.conv_num
== CONV_c
)) {
1570 if (sc
.width
== INT_MAX
) {
1574 while (scan_getwc(&sc
) >= 0) {
1575 zero_conversions
= 0;
1576 if (psfs
.conv_num
== CONV_C
) {
1580 i
= wcrtomb((char*) b
, sc
.wc
, &mbstate
);
1581 if (i
< 0) { /* Conversion failure. */
1590 if (sc
.width
> 0) { /* Failed to read all required. */
1593 psfs
.cnt
+= psfs
.store
;
1597 if ((psfs
.conv_num
== CONV_S
) || (psfs
.conv_num
== CONV_s
)) {
1598 /* Yes, believe it or not, a %s conversion can store nuls. */
1599 while (scan_getwc(&sc
) >= 0) {
1600 zero_conversions
= 0;
1601 if (iswspace(sc
.wc
)) {
1604 if (psfs
.conv_num
== CONV_S
) {
1608 i
= wcrtomb((char*) b
, sc
.wc
, &mbstate
);
1609 if (i
< 0) { /* Conversion failure. */
1621 unsigned char invert
= 0;
1623 assert((psfs
.conv_num
== CONV_LEFTBRACKET
)
1624 || (psfs
.conv_num
== CONV_leftbracket
));
1626 if (*++fmt
== '^') {
1630 sss
= (const wchar_t *) fmt
;
1634 while (*fmt
!= ']') {
1635 if (!*fmt
) { /* No closing ']'. */
1638 if ((*fmt
== '-') && (fmt
[1] != ']')
1639 && (fmt
[-1] < fmt
[1]) /* sorted? */
1645 /* Ok... a valid scanset spec. */
1647 while (scan_getwc(&sc
) >= 0) {
1648 zero_conversions
= 0;
1650 do { /* We know sss < fmt. */
1651 if (*ssp
== '-') { /* possible range... */
1652 /* Note: We accept a-c-e (ordered) as
1653 * equivalent to a-e. */
1655 if ((++ssp
< (const wchar_t *) fmt
)
1656 && (ssp
[-2] < *ssp
) /* sorted? */
1658 if ((sc
.wc
>= ssp
[-2])
1659 && (sc
.wc
<= *ssp
)) {
1662 continue; /* not in range */
1664 --ssp
; /* oops... '-' at end, so back up */
1666 /* false alarm... a literal '-' */
1668 if (sc
.wc
== *ssp
) { /* Matched literal char. */
1671 } while (++ssp
< (const wchar_t *) fmt
);
1673 if ((ssp
== (const wchar_t *) fmt
) ^ invert
) {
1674 /* no match and not inverting
1675 * or match and inverting */
1678 if (psfs
.conv_num
== CONV_LEFTBRACKET
) {
1682 i
= wcrtomb((char*) b
, sc
.wc
, &mbstate
);
1683 if (i
< 0) { /* Conversion failure. */
1693 /* Common tail for processing of %s and %[. */
1696 if (fail
) { /* nothing stored! */
1699 *wb
= 0; /* Nul-terminate string. */
1701 psfs
.cnt
+= psfs
.store
;
1705 #endif /* L_vfscanf */
1709 } /* conversion specification */
1712 if (__scan_getc(&sc
) != *fmt
) {
1715 #endif /* L_vfwscanf */
1722 if (__FERROR_UNLOCKED(fp
)) {
1728 if (__FERROR_UNLOCKED(fp
) || (*fmt
&& zero_conversions
&& __FEOF_UNLOCKED(fp
))) {
1729 psfs
.cnt
= EOF
; /* Yes, vfwscanf also returns EOF. */
1732 kill_scan_cookie(&sc
);
1734 __STDIO_STREAM_VALIDATE(fp
);
1736 __STDIO_AUTO_THREADUNLOCK(fp
);
1740 libc_hidden_def(VFSCANF
)
1742 /**********************************************************************/
1743 #ifdef L___psfs_do_numeric
1745 static const unsigned char spec_base
[] = SPEC_BASE
;
1746 static const unsigned char nil_string
[] = "(nil)";
1748 int attribute_hidden
__psfs_do_numeric(psfs_t
*psfs
, struct scan_cookie
*sc
)
1751 const unsigned char *p
;
1753 #ifdef __UCLIBC_HAS_FLOATS__
1756 #ifdef __UCLIBC_MJN3_ONLY__
1757 #warning TODO: Fix MAX_DIGITS. We do not do binary, so...!
1758 #warning TODO: Fix buf!
1760 #define MAX_DIGITS 65 /* Allow one leading 0. */
1761 unsigned char buf
[MAX_DIGITS
+2+ 100];
1762 unsigned char usflag
, base
;
1763 unsigned char nonzero
= 0;
1764 unsigned char seendigit
= 0;
1766 #ifdef __UCLIBC_MJN3_ONLY__
1767 #warning CONSIDER: What should be returned for an invalid conversion specifier?
1769 #ifndef __UCLIBC_HAS_FLOATS__
1770 if (psfs
->conv_num
> CONV_i
) { /* floating point */
1775 base
= spec_base
[psfs
->conv_num
- CONV_p
];
1776 usflag
= (psfs
->conv_num
<= CONV_u
); /* (1)0 if (un)signed */
1780 if (psfs
->conv_num
== CONV_p
) { /* Pointer */
1783 if ((__scan_getc(sc
) < 0) || (*p
!= sc
->cc
)) {
1785 if (p
> nil_string
) {
1786 /* We matched at least the '(' so even if we
1787 * are at eof, we can not match a pointer. */
1788 return -2; /* Matching failure */
1792 if (!*++p
) { /* Matched (nil), so no unget necessary. */
1795 _store_inttype(psfs
->cur_ptr
, psfs
->dataargtype
,
1802 #ifdef __UCLIBC_MJN3_ONLY__
1803 #warning CONSIDER: Should we require a 0x prefix and disallow +/- for pointer %p?
1804 #endif /* __UCLIBC_MJN3_ONLY__ */
1809 return -1; /* Input failure (nothing read yet). */
1812 if ((sc
->cc
== '+') || (sc
->cc
== '-')) { /* Handle leading sign.*/
1817 if ((base
& 0xef) == 0) { /* 0xef is ~16, so 16 or 0. */
1818 if (sc
->cc
== '0') { /* Possibly set base and handle prefix. */
1820 if ((sc
->cc
|0x20) == 'x') { /* Assumes ascii.. x or X. */
1821 if (__scan_getc(sc
) < 0) {
1822 /* Either EOF or error (including wc outside char range).
1823 * If EOF or error, this is a matching failure (we read 0x).
1824 * If wc outside char range, this is also a matching failure.
1825 * Hence, we do an unget (although not really necessary here
1827 goto DONE_DO_UNGET
; /* matching failure */
1829 base
= 16; /* Base 16 for sure now. */
1830 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
1831 /* The prefix is required for hexadecimal floats. */
1834 #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
1835 } else { /* oops... back up */
1837 sc
->cc
= '0'; /* NASTY HACK! */
1839 base
= (base
>> 1) + 8; /* 0->8, 16->16. no 'if' */
1840 #ifdef __UCLIBC_HAS_FLOATS__
1841 if (psfs
->conv_num
> CONV_i
) { /* floating point */
1851 /***************** digit grouping **********************/
1852 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
1854 if ((psfs
->flags
& FLAG_THOUSANDS
) && (base
== 10)
1855 && *(p
= (const unsigned char *) sc
->grouping
)
1858 int nblk1
, nblk2
, nbmax
, lastblock
, pass
, i
;
1861 #ifdef __UCLIBC_MJN3_ONLY__
1862 #warning CONSIDER: Should we initalize the grouping blocks in __init_scan_cookie()?
1863 #endif /* __UCLIBC_MJN3_ONLY__ */
1864 nbmax
= nblk2
= nblk1
= *p
;
1867 if (nbmax
< nblk2
) {
1873 /* Note: for printf, if 0 and \' flags appear then
1874 * grouping is done before 0-padding. Should we
1875 * strip leading 0's first? Or add a 0 flag? */
1877 /* For vfwscanf, sc_getc translates, so the value of sc->cc is
1878 * either EOF or a char. */
1880 if (!__isdigit_char_or_EOF(sc
->cc
)) { /* No starting digit! */
1881 #ifdef __UCLIBC_HAS_FLOATS__
1882 if (psfs
->conv_num
> CONV_i
) { /* floating point */
1883 goto NO_STARTING_DIGIT
;
1889 if (sc
->cc
== '0') {
1891 *b
++ = '0'; /* Store the first 0. */
1892 #ifdef __UCLIBC_MJN3_ONLY__
1893 #warning CONSIDER: Should leading 0s be skipped before digit grouping? (printf 0 pad)
1894 #endif /* __UCLIBC_MJN3_ONLY__ */
1896 do { /* But ignore all subsequent 0s. */
1898 } while (sc
->cc
== '0');
1905 while (__isdigit_char_or_EOF(sc
->cc
)) {
1907 if (i
== nbmax
) { /* too many digits for a block */
1908 #ifdef __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__
1909 if (!pass
) { /* treat as nongrouped */
1913 goto DO_TRIM_LEADING_ZEROS
;
1916 if (nbmax
> nblk1
) {
1917 goto DONE_DO_UNGET
; /* matching failure */
1919 goto DONE_GROUPING_DO_UNGET
; /* nbmax == nblk1 */
1923 if (nonzero
|| (sc
->cc
!= '0')) {
1924 if (b
< buf
+ MAX_DIGITS
) {
1927 #ifdef __UCLIBC_HAS_FLOATS__
1937 if (i
) { /* we saw digits digits */
1938 if ((i
== nblk2
) || ((i
< nblk2
) && !pass
)) {
1939 /* (possible) outer grp */
1940 p
= sc
->thousands_sep
;
1941 if (*p
== sc
->cc
) { /* first byte matches... */
1942 /* so check if grouping mb char */
1943 /* Since 1st matched, either match or fail now
1944 * unless EOF (yuk) */
1947 if (!*++p
) { /* is a grouping mb char */
1956 /* bad grouping mb char! */
1958 if ((sc
->cc
>= 0) || (p
> sc
->thousands_sep
+ 1)) {
1959 #ifdef __UCLIBC_HAS_FLOATS__
1960 /* We failed to match a thousep mb char, and
1961 * we've read too much to recover. But if
1962 * this is a floating point conversion and
1963 * the initial portion of the decpt mb char
1964 * matches, then we may still be able to
1966 int k
= p
- sc
->thousands_sep
- 1;
1968 if ((psfs
->conv_num
> CONV_i
) /* float conversion */
1969 && (!pass
|| (i
== nblk1
)) /* possible last */
1970 && !memcmp(sc
->thousands_sep
, sc
->fake_decpt
, k
)
1971 /* and prefix matched, so could be decpt */
1974 p
= sc
->fake_decpt
+ k
;
1977 strcpy((char*) b
, (char*) sc
->decpt
);
1988 #endif /* __UCLIBC_HAS_FLOATS__ */
1991 /* was EOF and 1st, so recoverable. */
1994 if ((i
== nblk1
) || ((i
< nblk1
) && !pass
)) {
1995 /* got an inner group */
1996 goto DONE_GROUPING_DO_UNGET
;
1998 goto DONE_DO_UNGET
; /* Matching failure. */
2006 assert(0); /* Should never get here. */
2009 #endif /***************** digit grouping **********************/
2011 /* Not grouping so first trim all but one leading 0. */
2012 #ifdef __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__
2013 DO_TRIM_LEADING_ZEROS
:
2014 #endif /* __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__ */
2015 if (sc
->cc
== '0') {
2017 *b
++ = '0'; /* Store the first 0. */
2018 do { /* But ignore all subsequent 0s. */
2020 } while (sc
->cc
== '0');
2023 #ifdef __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__
2025 #endif /* __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__ */
2026 /* At this point, we're ready to start reading digits. */
2028 #define valid_digit(cc,base) (isxdigit(cc) && ((base == 16) || (cc - '0' < base)))
2030 while (valid_digit(sc
->cc
,base
)) { /* Now for significant digits.*/
2031 if (b
- buf
< MAX_DIGITS
) {
2032 nonzero
= seendigit
= 1; /* Set nonzero too 0s trimmed above. */
2034 #ifdef __UCLIBC_HAS_FLOATS__
2042 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
2043 DONE_GROUPING_DO_UNGET
:
2044 #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
2045 if (psfs
->conv_num
<= CONV_i
) { /* integer conversion */
2047 *b
= 0; /* null-terminate */
2049 goto DONE
; /* No digits! */
2056 _store_inttype(psfs
->cur_ptr
, psfs
->dataargtype
,
2057 (uintmax_t) STRTOUIM((char *) buf
, NULL
, base
, 1-usflag
));
2062 #ifdef __UCLIBC_HAS_FLOATS__
2064 /* At this point, we have everything left of the decimal point or exponent. */
2065 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
2071 strcpy((char *) b
, (char *) sc
->decpt
);
2076 if (p
> sc
->fake_decpt
) {
2077 goto DONE_DO_UNGET
; /* matching failure (read some of decpt) */
2079 goto DO_DIGIT_CHECK
;
2085 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
2089 if (sc
->cc
== '0') {
2090 assert(exp_adjust
== 0);
2097 } while (sc
->cc
== '0');
2101 while (valid_digit(sc
->cc
,base
)) { /* Process fractional digits.*/
2102 if (b
- buf
< MAX_DIGITS
) {
2110 /* Hmm... no decimal point. */
2112 static const unsigned char nan_inf_str
[] = "an\0nfinity";
2114 if (base
== 16) { /* We had a prefix, but no digits! */
2115 goto DONE_DO_UNGET
; /* matching failure */
2118 /* Avoid tolower problems for INFINITY in the tr_TR locale. (yuk)*/
2120 #define TOLOWER(C) ((C)|0x20)
2122 switch (TOLOWER(sc
->cc
)) {
2124 p
= nan_inf_str
+ 3;
2130 /* No digits and not inf or nan. */
2138 if (TOLOWER(sc
->cc
) == *p
) {
2143 if (!*p
|| (p
== nan_inf_str
+ 5)) { /* match nan/infinity or inf */
2146 /* Unrecoverable. Even if on 1st char, we had no digits. */
2151 /* If we get here, we had some digits. */
2154 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
2155 ((base
== 16) && (((sc
->cc
)|0x20) == 'p')) ||
2157 (((sc
->cc
)|0x20) == 'e')
2158 ) { /* Process an exponent. */
2163 goto DONE_DO_UNGET
; /* matching failure.. no exponent digits */
2166 if ((sc
->cc
== '+') || (sc
->cc
== '-')) { /* Signed exponent? */
2171 #ifdef __UCLIBC_MJN3_ONLY__
2172 #warning TODO: Fix MAX_EXP_DIGITS!
2174 #define MAX_EXP_DIGITS 20
2179 if (sc
->cc
== '0') {
2184 } while (sc
->cc
== '0');
2187 while (__isdigit_char_or_EOF(sc
->cc
)) { /* Exponent digits (base 10).*/
2188 if (seendigit
< MAX_EXP_DIGITS
) {
2195 if (!seendigit
) { /* No digits. Unrecoverable. */
2206 x
= __strtofpmax((char *) buf
, &e
, exp_adjust
);
2209 if (psfs
->dataargtype
& PA_FLAG_LONG_LONG
) {
2210 *((long double *)psfs
->cur_ptr
) = (long double) x
;
2211 } else if (psfs
->dataargtype
& PA_FLAG_LONG
) {
2212 *((double *)psfs
->cur_ptr
) = (double) x
;
2214 *((float *)psfs
->cur_ptr
) = (float) x
;
2221 #endif /* __UCLIBC_HAS_FLOATS__ */
2226 return -2; /* Matching failure. */
2230 /**********************************************************************/