OSDN Git Service

f38e72b5c84e9aaa284fd2cfb50ed1e1502d5036
[uclinux-h8/uClibc.git] / libc / stdio / _scanf.c
1 /*  Copyright (C) 2002-2004     Manuel Novoa III
2  *
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.
7  *
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.
12  *
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.
16  */
17
18 /* Aug 1, 2003
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.
23  *
24  * Aug 18, 2003
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.
28  *
29  * Sep 5, 2003
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.
32  *
33  * Sep 13, 2003
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.
36  *
37  * Sep 21, 2003
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.
44  */
45
46 #define _ISOC99_SOURCE                  /* for LLONG_MAX primarily... */
47 #include <features.h>
48 #include "_stdio.h"
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <ctype.h>
52 #include <string.h>
53 #include <stdarg.h>
54 #include <stdint.h>
55 #include <errno.h>
56 #include <printf.h>
57
58 #ifdef __UCLIBC_HAS_WCHAR__
59 #include <bits/uClibc_uwchar.h>
60 #include <wchar.h>
61 #include <wctype.h>
62 #endif /* __UCLIBC_HAS_WCHAR__ */
63
64 #include <langinfo.h>
65 #include <locale.h>
66
67 #include <assert.h>
68 #include <limits.h>
69
70 #ifdef __UCLIBC_HAS_THREADS__
71 #include <stdio_ext.h>
72 #include <pthread.h>
73 #endif /* __UCLIBC_HAS_THREADS__ */
74
75 #ifdef __UCLIBC_HAS_FLOATS__
76 #include <float.h>
77 #include <bits/uClibc_fpmax.h>
78 #endif /* __UCLIBC_HAS_FLOATS__ */
79
80 #ifdef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
81 #ifdef L_vfscanf
82 /* only emit this once */
83 #warning Forcing undef of __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__ until implemented!
84 #endif
85 #undef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
86 #endif
87
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
91
92 #if !defined(__STDIO_BUFFERS) && !defined(__UCLIBC_HAS_WCHAR__)
93 typedef struct {
94         FILE f;
95         unsigned char *bufread;         /* pointer to 1 past end of buffer */
96         unsigned char *bufpos;
97 } __FILE_vsscanf;
98 #endif
99
100 #endif
101
102 extern void _store_inttype(void *dest, int desttype, uintmax_t val);
103
104 #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
105
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)
111 #endif
112
113 #else  /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
114
115 extern unsigned long
116 _stdlib_strto_l(register const char * __restrict str,
117                                 char ** __restrict endptr, int base, int sflag);
118
119 #if (ULONG_MAX == UINTMAX_MAX)
120 #define STRTOUIM(s,e,b,sf) _stdlib_strto_l(s,e,b,sf)
121 #endif
122
123 #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
124
125 #ifndef STRTOUIM
126 #error STRTOUIM conversion function is undefined!
127 #endif
128
129 /**********************************************************************/
130
131 /* The standards require EOF < 0. */
132 #if EOF >= CHAR_MIN
133 #define __isdigit_char_or_EOF(C)   __isdigit_char((C))
134 #else
135 #define __isdigit_char_or_EOF(C)   __isdigit_int((C))
136 #endif
137
138 /**********************************************************************/
139 #ifdef L_fscanf
140
141 int fscanf(FILE * __restrict stream, const char * __restrict format, ...)
142 {
143         va_list arg;
144         int rv;
145
146         va_start(arg, format);
147         rv = vfscanf(stream, format, arg);
148         va_end(arg);
149
150         return rv;
151 }
152 libc_hidden_def(fscanf)
153
154 #endif
155 /**********************************************************************/
156 #ifdef L_scanf
157
158 int scanf(const char * __restrict format, ...)
159 {
160         va_list arg;
161         int rv;
162
163         va_start(arg, format);
164         rv = vfscanf(stdin, format, arg);
165         va_end(arg);
166
167         return rv;
168 }
169
170 #endif
171 /**********************************************************************/
172 #ifdef L_sscanf
173
174 #ifdef __STDIO_HAS_VSSCANF
175
176 int sscanf(const char * __restrict str, const char * __restrict format, ...)
177 {
178         va_list arg;
179         int rv;
180
181         va_start(arg, format);
182         rv = vsscanf(str, format, arg);
183         va_end(arg);
184
185         return rv;
186 }
187 libc_hidden_def(sscanf)
188
189 #else  /* __STDIO_HAS_VSSCANF */
190 #warning Skipping sscanf since no vsscanf!
191 #endif /* __STDIO_HAS_VSSCANF */
192
193 #endif
194 /**********************************************************************/
195 #ifdef L_vscanf
196
197 int vscanf(const char * __restrict format, va_list arg)
198 {
199         return vfscanf(stdin, format, arg);
200 }
201
202 #endif
203 /**********************************************************************/
204 #ifdef L_vsscanf
205
206 #ifdef __UCLIBC_MJN3_ONLY__
207 #warning WISHLIST: Implement vsscanf for non-buf and no custom stream case.
208 #endif /* __UCLIBC_MJN3_ONLY__ */
209
210 #ifdef __STDIO_BUFFERS
211
212 int vsscanf(__const char *sp, __const char *fmt, va_list ap)
213 {
214         FILE f;
215
216 /*      __STDIO_STREAM_RESET_GCS(&f); */
217 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
218         f.__cookie = &(f.__filedes);
219         f.__gcs.read = NULL;
220         f.__gcs.write = NULL;
221         f.__gcs.seek = NULL;
222         f.__gcs.close = NULL;
223 #endif
224
225         f.__filedes = __STDIO_STREAM_FAKE_VSSCANF_FILEDES;
226         f.__modeflags = (__FLAG_NARROW|__FLAG_READONLY|__FLAG_READING);
227
228 #ifdef __UCLIBC_HAS_WCHAR__
229         f.__ungot_width[0] = 0;
230 #endif
231 #ifdef __STDIO_MBSTATE
232         __INIT_MBSTATE(&(f.__state));
233 #endif
234
235 #ifdef __UCLIBC_HAS_THREADS__
236         f.__user_locking = 1;           /* Set user locking. */
237         STDIO_INIT_MUTEX(f.__lock);
238 #endif
239         f.__nextopen = NULL;
240
241         /* Set these last since __bufgetc initialization depends on
242          * __user_locking and only gets set if user locking is on. */
243         f.__bufstart =
244         f.__bufpos = (unsigned char *) ((void *) sp);
245         f.__bufread =
246         f.__bufend = f.__bufstart + strlen(sp);
247         __STDIO_STREAM_ENABLE_GETC(&f);
248         __STDIO_STREAM_DISABLE_PUTC(&f);
249
250         return vfscanf(&f, fmt, ap);
251 }
252 libc_hidden_def(vsscanf)
253
254 #elif !defined(__UCLIBC_HAS_WCHAR__)
255
256 int vsscanf(__const char *sp, __const char *fmt, va_list ap)
257 {
258         __FILE_vsscanf f;
259
260         f.bufpos = (unsigned char *) ((void *) sp);
261         f.bufread = f.bufpos + strlen(sp);
262
263 /*      __STDIO_STREAM_RESET_GCS(&f.f); */
264 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
265         f.f.__cookie = &(f.f.__filedes);
266         f.f.__gcs.read = NULL;
267         f.f.__gcs.write = NULL;
268         f.f.__gcs.seek = NULL;
269         f.f.__gcs.close = NULL;
270 #endif
271
272         f.f.__filedes = __STDIO_STREAM_FAKE_VSSCANF_FILEDES_NB;
273         f.f.__modeflags = (__FLAG_NARROW|__FLAG_READONLY|__FLAG_READING);
274
275 /* #ifdef __UCLIBC_HAS_WCHAR__ */
276 /*      f.f.__ungot_width[0] = 0; */
277 /* #endif */
278 #ifdef __STDIO_MBSTATE
279 #error __STDIO_MBSTATE is defined!
280 /*      __INIT_MBSTATE(&(f.f.__state)); */
281 #endif
282
283 #ifdef __UCLIBC_HAS_THREADS__
284         f.f.__user_locking = 1;         /* Set user locking. */
285         STDIO_INIT_MUTEX(f.f.__lock);
286 #endif
287         f.f.__nextopen = NULL;
288
289         return vfscanf(&f.f, fmt, ap);
290 }
291 libc_hidden_def(vsscanf)
292
293 #elif defined(__UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__)
294
295 int vsscanf(__const char *sp, __const char *fmt, va_list ap)
296 {
297         FILE *f;
298         int rv = EOF;
299
300         if ((f = fmemopen((char *)sp, strlen(sp), "r")) != NULL) {
301                 rv = vfscanf(f, fmt, ap);
302                 fclose(f);
303         }
304
305         return rv;
306 }
307 libc_hidden_def(vsscanf)
308
309 #else
310 #warning Skipping vsscanf since no buffering, no custom streams, and wchar enabled!
311 #ifdef __STDIO_HAS_VSSCANF
312 #error WHOA! __STDIO_HAS_VSSCANF is defined!
313 #endif
314 #endif
315
316 #endif
317 /**********************************************************************/
318 #ifdef L_fwscanf
319
320 int fwscanf(FILE * __restrict stream, const wchar_t * __restrict format, ...)
321 {
322         va_list arg;
323         int rv;
324
325         va_start(arg, format);
326         rv = vfwscanf(stream, format, arg);
327         va_end(arg);
328
329         return rv;
330 }
331
332 #endif
333 /**********************************************************************/
334 #ifdef L_wscanf
335
336 int wscanf(const wchar_t * __restrict format, ...)
337 {
338         va_list arg;
339         int rv;
340
341         va_start(arg, format);
342         rv = vfwscanf(stdin, format, arg);
343         va_end(arg);
344
345         return rv;
346 }
347
348 #endif
349 /**********************************************************************/
350 #ifdef L_swscanf
351
352 #ifdef __STDIO_BUFFERS
353
354 int swscanf(const wchar_t * __restrict str, const wchar_t * __restrict format,
355                    ...)
356 {
357         va_list arg;
358         int rv;
359
360         va_start(arg, format);
361         rv = vswscanf(str, format, arg);
362         va_end(arg);
363
364         return rv;
365 }
366 #else  /* __STDIO_BUFFERS */
367 #warning Skipping swscanf since no buffering!
368 #endif /* __STDIO_BUFFERS */
369
370 #endif
371 /**********************************************************************/
372 #ifdef L_vwscanf
373
374 int vwscanf(const wchar_t * __restrict format, va_list arg)
375 {
376         return vfwscanf(stdin, format, arg);
377 }
378
379 #endif
380 /**********************************************************************/
381 #ifdef L_vswscanf
382
383 #ifdef __STDIO_BUFFERS
384
385 int vswscanf(const wchar_t * __restrict str, const wchar_t * __restrict format,
386                         va_list arg)
387 {
388         FILE f;
389
390         f.__bufstart =
391         f.__bufpos = (char *) str;
392         f.__bufread =
393         f.__bufend = (char *)(str + wcslen(str));
394         __STDIO_STREAM_DISABLE_GETC(&f);
395         __STDIO_STREAM_DISABLE_PUTC(&f);
396
397 /*      __STDIO_STREAM_RESET_GCS(&f); */
398 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
399         f.__cookie = &(f.__filedes);
400         f.__gcs.read = NULL;
401         f.__gcs.write = NULL;
402         f.__gcs.seek = NULL;
403         f.__gcs.close = NULL;
404 #endif
405
406         f.__filedes = __STDIO_STREAM_FAKE_VSWSCANF_FILEDES;
407         f.__modeflags = (__FLAG_WIDE|__FLAG_READONLY|__FLAG_READING);
408
409 #ifdef __UCLIBC_HAS_WCHAR__
410         f.__ungot_width[0] = 0;
411 #endif /* __UCLIBC_HAS_WCHAR__ */
412 #ifdef __STDIO_MBSTATE
413         __INIT_MBSTATE(&(f.__state));
414 #endif /* __STDIO_MBSTATE */
415
416 #ifdef __UCLIBC_HAS_THREADS__
417         f.__user_locking = 1;           /* Set user locking. */
418         STDIO_INIT_MUTEX(f.__lock);
419 #endif
420         f.__nextopen = NULL;
421
422         return vfwscanf(&f, format, arg);
423 }
424 libc_hidden_def(vswscanf)
425 #else  /* __STDIO_BUFFERS */
426 #warning Skipping vswscanf since no buffering!
427 #endif /* __STDIO_BUFFERS */
428
429 #endif
430 /**********************************************************************/
431 /**********************************************************************/
432
433
434
435 /* float layout          0123456789012345678901  repeat n for "l[" */
436 #define SPEC_CHARS              "npxXoudifFeEgGaACSncs["
437 /*                       npxXoudif eEgG  CS cs[ */
438
439 /* NOTE: Ordering is important!  In particular, CONV_LEFTBRACKET
440  * must immediately precede CONV_c. */
441
442 enum {
443         CONV_n = 0,
444         CONV_p,
445         CONV_x, CONV_X, CONV_o, CONV_u, CONV_d, CONV_i,
446         CONV_f, CONV_F, CONV_e, CONV_E, CONV_g, CONV_G, CONV_a, CONV_A,
447         CONV_C, CONV_S, CONV_LEFTBRACKET, CONV_c, CONV_s, CONV_leftbracket,
448         CONV_percent, CONV_whitespace /* not in SPEC_* and no flags */
449 };
450
451 #ifdef __UCLIBC_HAS_FLOATS__
452 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
453 /*                         p   x   X  o   u   d   i   f   F   e   E   g   G   a   A */
454 #define SPEC_BASE               { 16, 16, 16, 8, 10, 10,  0,  0,  0,  0,  0,  0,  0,  0,  0 }
455 #else
456 /*                         p   x   X  o   u   d   i   f   F   e   E   g   G   a   A */
457 #define SPEC_BASE               { 16, 16, 16, 8, 10, 10,  0, 10, 10, 10, 10, 10, 10, 10, 10 }
458 #endif
459 #else  /* __UCLIBC_HAS_FLOATS__ */
460 /*                         p   x   X  o   u   d   i   f   F   e   E   g   G   a   A */
461 #define SPEC_BASE               { 16, 16, 16, 8, 10, 10,  0 }
462 #endif /* __UCLIBC_HAS_FLOATS__ */
463
464 #ifdef __UCLIBC_MJN3_ONLY__
465 #ifdef L_vfscanf
466 /* emit once */
467 #warning CONSIDER: Add a '0' flag to eat 0 padding when grouping?
468 #endif
469 #endif /* __UCLIBC_MJN3_ONLY__ */
470
471 #define SPEC_FLAGS              "*'I"
472
473 enum {
474         FLAG_SURPRESS   =   0x10,       /* MUST BE 1ST!!  See DO_FLAGS. */
475         FLAG_THOUSANDS  =       0x20,
476         FLAG_I18N               =       0x40,   /* only works for d, i, u */
477         FLAG_MALLOC     =   0x80,       /* only works for s, S, and [ (and l[)*/
478 };
479
480
481 #define SPEC_RANGES             { CONV_n, CONV_p, CONV_i, CONV_A, \
482                                                   CONV_C, CONV_LEFTBRACKET, \
483                                                   CONV_c, CONV_leftbracket }
484
485 /* Note: We treat L and ll as synonymous... for ints and floats. */
486
487 #define SPEC_ALLOWED_FLAGS              { \
488         /* n */                 (0x0f|FLAG_SURPRESS), \
489         /* p */                 (   0|FLAG_SURPRESS), \
490         /* oxXudi */    (0x0f|FLAG_SURPRESS|FLAG_THOUSANDS|FLAG_I18N), \
491         /* fFeEgGaA */  (0x0c|FLAG_SURPRESS|FLAG_THOUSANDS|FLAG_I18N), \
492         /* C */                 (   0|FLAG_SURPRESS), \
493         /* S and l[ */  (   0|FLAG_SURPRESS|FLAG_MALLOC), \
494         /* c */                 (0x04|FLAG_SURPRESS), \
495         /* s and [ */   (0x04|FLAG_SURPRESS|FLAG_MALLOC), \
496 }
497
498
499 /**********************************************************************/
500 /*
501  * In order to ease translation to what arginfo and _print_info._flags expect,
502  * we map:  0:int  1:char  2:longlong 4:long  8:short
503  * and then _flags |= (((q << 7) + q) & 0x701) and argtype |= (_flags & 0x701)
504  */
505
506 /* TODO -- Fix the table below to take into account stdint.h. */
507 /*  #ifndef LLONG_MAX */
508 /*  #error fix QUAL_CHARS for no long long!  Affects 'L', 'j', 'q', 'll'. */
509 /*  #else */
510 /*  #if LLONG_MAX != INTMAX_MAX */
511 /*  #error fix QUAL_CHARS intmax_t entry 'j'! */
512 /*  #endif */
513 /*  #endif */
514
515 #ifdef PDS
516 #error PDS already defined!
517 #endif
518 #ifdef SS
519 #error SS already defined!
520 #endif
521 #ifdef IMS
522 #error IMS already defined!
523 #endif
524
525 #if PTRDIFF_MAX == INT_MAX
526 #define PDS             0
527 #elif PTRDIFF_MAX == LONG_MAX
528 #define PDS             4
529 #elif defined(LLONG_MAX) && (PTRDIFF_MAX == LLONG_MAX)
530 #define PDS             8
531 #else
532 #error fix QUAL_CHARS ptrdiff_t entry 't'!
533 #endif
534
535 #if SIZE_MAX == UINT_MAX
536 #define SS              0
537 #elif SIZE_MAX == ULONG_MAX
538 #define SS              4
539 #elif defined(LLONG_MAX) && (SIZE_MAX == ULLONG_MAX)
540 #define SS              8
541 #else
542 #error fix QUAL_CHARS size_t entries 'z', 'Z'!
543 #endif
544
545 #if INTMAX_MAX == INT_MAX
546 #define IMS             0
547 #elif INTMAX_MAX == LONG_MAX
548 #define IMS             4
549 #elif defined(LLONG_MAX) && (INTMAX_MAX == LLONG_MAX)
550 #define IMS             8
551 #else
552 #error fix QUAL_CHARS intmax_t entry 'j'!
553 #endif
554
555 #define QUAL_CHARS              { \
556         /* j:(u)intmax_t z:(s)size_t  t:ptrdiff_t  \0:int  q:long_long */ \
557         'h',   'l',  'L',  'j',  'z',  't',  'q', 0, \
558          2,     4,    8,  IMS,   SS,  PDS,    8,  0, /* TODO -- fix!!! */ \
559          1,     8 \
560 }
561
562
563 /**********************************************************************/
564
565 #ifdef L_vfwscanf
566 /* FIXME: "warning: the right operand of ">" changes sign when promoted" */
567 #if WINT_MIN > EOF
568 #error Unfortunately, we currently need wint_t to be able to store EOF.  Sorry.
569 #endif
570 #define W_EOF WEOF
571 #define Wint wint_t
572 #define Wchar wchar_t
573 #define Wuchar __uwchar_t
574 #define ISSPACE(C) iswspace((C))
575 #define VFSCANF vfwscanf
576 #define GETC(SC) (SC)->sc_getc((SC))
577 #else
578 typedef unsigned char __uchar_t;
579 #define W_EOF EOF
580 #define Wint int
581 #define Wchar char
582 #define Wuchar __uchar_t
583 #define ISSPACE(C) isspace((C))
584 #define VFSCANF vfscanf
585 #ifdef __UCLIBC_HAS_WCHAR__
586 #define GETC(SC) (SC)->sc_getc((SC))
587 #else  /* __UCLIBC_HAS_WCHAR__ */
588 #define GETC(SC) getc_unlocked((SC)->fp)
589 #endif /* __UCLIBC_HAS_WCHAR__ */
590 #endif
591
592 struct scan_cookie {
593         Wint cc;
594         Wint ungot_char;
595         FILE *fp;
596         int nread;
597         int width;
598
599 #ifdef __UCLIBC_HAS_WCHAR__
600         wchar_t app_ungot;                      /* Match FILE struct member type. */
601         unsigned char ungot_wchar_width;
602 #else  /* __UCLIBC_HAS_WCHAR__ */
603         unsigned char app_ungot;        /* Match FILE struct member type. */
604 #endif /* __UCLIBC_HAS_WCHAR__ */
605
606         char ungot_flag;
607
608 #ifdef __UCLIBC_HAS_WCHAR__
609         char ungot_wflag;                       /* vfwscanf */
610         char mb_fail;                           /* vfscanf */
611         mbstate_t mbstate;                      /* vfscanf */
612         wint_t wc;
613         wint_t ungot_wchar;                     /* to support __scan_getc */
614         int (*sc_getc)(struct scan_cookie *);
615 #endif /* __UCLIBC_HAS_WCHAR__ */
616
617 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
618         const char *grouping;
619         const unsigned char *thousands_sep;
620         int tslen;
621 #ifdef __UCLIBC_HAS_WCHAR__
622         wchar_t thousands_sep_wc;
623 #endif /* __UCLIBC_HAS_WCHAR__ */
624 #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
625
626 #ifdef __UCLIBC_HAS_FLOATS__
627         const unsigned char *decpt;
628         int decpt_len;
629 #ifdef __UCLIBC_HAS_WCHAR__
630         wchar_t decpt_wc;
631 #endif /* __UCLIBC_HAS_WCHAR__ */
632         const unsigned char *fake_decpt;
633 #endif /* __UCLIBC_HAS_FLOATS__ */
634
635 };
636
637 typedef struct {
638 #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
639 #if NL_ARGMAX > 10
640 #warning NL_ARGMAX > 10, and space is allocated on the stack for positional args.
641 #endif
642         void *pos_args[NL_ARGMAX];
643         int num_pos_args;               /* Must start at -1. */
644         int cur_pos_arg;
645 #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
646         void *cur_ptr;
647         const unsigned char *fmt;
648         int cnt, dataargtype, conv_num, max_width;
649         unsigned char store, flags;
650 } psfs_t;                                               /* parse scanf format state */
651
652
653 /**********************************************************************/
654 /**********************************************************************/
655
656 extern void __init_scan_cookie(register struct scan_cookie *sc,
657                                                            register FILE *fp) attribute_hidden;
658 extern int __scan_getc(register struct scan_cookie *sc) attribute_hidden;
659 extern void __scan_ungetc(register struct scan_cookie *sc) attribute_hidden;
660
661 #ifdef __UCLIBC_HAS_FLOATS__
662 extern int __scan_strtold(long double *ld, struct scan_cookie *sc);
663 #endif /* __UCLIBC_HAS_FLOATS__ */
664
665 extern int __psfs_parse_spec(psfs_t *psfs) attribute_hidden;
666 extern int __psfs_do_numeric(psfs_t *psfs, struct scan_cookie *sc) attribute_hidden;
667
668 /**********************************************************************/
669 #ifdef L___scan_cookie
670
671 #ifdef __UCLIBC_MJN3_ONLY__
672 #warning TODO: Remove dependence on decpt_str and fake_decpt in stub locale mode.
673 #endif
674 #ifndef __UCLIBC_HAS_LOCALE__
675 static const char decpt_str[] = ".";
676 #endif
677
678 void attribute_hidden __init_scan_cookie(register struct scan_cookie *sc,
679                                                 register FILE *fp)
680 {
681         sc->fp = fp;
682         sc->nread = 0;
683         sc->ungot_flag = 0;
684         sc->app_ungot = ((fp->__modeflags & __FLAG_UNGOT) ? fp->__ungot[1] : 0);
685 #ifdef __UCLIBC_HAS_WCHAR__
686         sc->ungot_wflag = 0;            /* vfwscanf */
687         sc->mb_fail = 0;
688 #endif /* __UCLIBC_HAS_WCHAR__ */
689
690 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
691         if (*(sc->grouping = __UCLIBC_CURLOCALE->grouping)) {
692                 sc->thousands_sep = (const unsigned char *) __UCLIBC_CURLOCALE->thousands_sep;
693                 sc->tslen = __UCLIBC_CURLOCALE->thousands_sep_len;
694 #ifdef __UCLIBC_HAS_WCHAR__
695                 sc->thousands_sep_wc = __UCLIBC_CURLOCALE->thousands_sep_wc;
696 #endif /* __UCLIBC_HAS_WCHAR__ */
697         }
698 #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
699
700 #ifdef __UCLIBC_HAS_FLOATS__
701 #ifdef __UCLIBC_HAS_LOCALE__
702         sc->decpt = (const unsigned char *) __UCLIBC_CURLOCALE->decimal_point;
703         sc->decpt_len = __UCLIBC_CURLOCALE->decimal_point_len;
704 #else  /* __UCLIBC_HAS_LOCALE__ */
705         sc->fake_decpt = sc->decpt = (unsigned char *) decpt_str;
706         sc->decpt_len = 1;
707 #endif /* __UCLIBC_HAS_LOCALE__ */
708 #ifdef __UCLIBC_HAS_WCHAR__
709 #ifdef __UCLIBC_HAS_LOCALE__
710         sc->decpt_wc = __UCLIBC_CURLOCALE->decimal_point_wc;
711 #else
712         sc->decpt_wc = '.';
713 #endif
714 #endif /* __UCLIBC_HAS_WCHAR__ */
715 #endif /* __UCLIBC_HAS_FLOATS__ */
716
717 }
718
719 int attribute_hidden __scan_getc(register struct scan_cookie *sc)
720 {
721         int c;
722
723 #ifdef __UCLIBC_HAS_WCHAR__
724         assert(!sc->mb_fail);
725 #endif /* __UCLIBC_HAS_WCHAR__ */
726
727         sc->cc = EOF;
728
729         if (--sc->width < 0) {
730                 sc->ungot_flag |= 2;
731                 return -1;
732         }
733
734         if (sc->ungot_flag == 0) {
735 #if !defined(__STDIO_BUFFERS) && !defined(__UCLIBC_HAS_WCHAR__)
736                 if (!__STDIO_STREAM_IS_FAKE_VSSCANF_NB(sc->fp)) {
737                         c = GETC(sc);
738                 } else {
739                         __FILE_vsscanf *fv = (__FILE_vsscanf *)(sc->fp);
740                         if (fv->bufpos < fv->bufread) {
741                                 c = *fv->bufpos++;
742                         } else {
743                                 c = EOF;
744                                 sc->fp->__modeflags |= __FLAG_EOF;
745                         }
746                 }
747                 if (c == EOF) {
748                         sc->ungot_flag |= 2;
749                         return -1;
750                 }
751 #else
752                 if ((c = GETC(sc)) == EOF) {
753                         sc->ungot_flag |= 2;
754                         return -1;
755                 }
756 #endif
757                 sc->ungot_char = c;
758         } else {
759                 assert(sc->ungot_flag == 1);
760                 sc->ungot_flag = 0;
761         }
762
763         ++sc->nread;
764         return sc->cc = sc->ungot_char;
765 }
766
767 void attribute_hidden __scan_ungetc(register struct scan_cookie *sc)
768 {
769         ++sc->width;
770         if (sc->ungot_flag == 2) {      /* last was EOF */
771                 sc->ungot_flag = 0;
772                 sc->cc = sc->ungot_char;
773         } else if (sc->ungot_flag == 0) {
774                 sc->ungot_flag = 1;
775                 --sc->nread;
776         } else {
777                 assert(0);
778         }
779 }
780
781 #endif
782 /**********************************************************************/
783 #ifdef L___psfs_parse_spec
784
785 #ifdef SPEC_FLAGS
786 static const unsigned char spec_flags[] = SPEC_FLAGS;
787 #endif /* SPEC_FLAGS */
788 static const unsigned char spec_chars[] = SPEC_CHARS;
789 static const unsigned char qual_chars[] = QUAL_CHARS;
790 static const unsigned char spec_ranges[] = SPEC_RANGES;
791 static const unsigned short spec_allowed[] = SPEC_ALLOWED_FLAGS;
792
793 int attribute_hidden __psfs_parse_spec(register psfs_t *psfs)
794 {
795         const unsigned char *p;
796         const unsigned char *fmt0 = psfs->fmt;
797         int i;
798 #ifdef SPEC_FLAGS
799         int j;
800 #endif
801 #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
802         unsigned char fail = 0;
803
804         i = 0;                                          /* Do this here to avoid a warning. */
805
806         if (!__isdigit_char(*psfs->fmt)) { /* Not a positional arg. */
807                 fail = 1;
808                 goto DO_FLAGS;
809         }
810
811         /* parse the positional arg (or width) value */
812         do {
813                 if (i <= ((INT_MAX - 9)/10)) {
814                         i = (i * 10) + (*psfs->fmt++ - '0');
815                 }
816         } while (__isdigit_char(*psfs->fmt));
817
818         if (*psfs->fmt != '$') { /* This is a max field width. */
819                 if (psfs->num_pos_args >= 0) { /* Already saw a pos arg! */
820                         goto ERROR_EINVAL;
821                 }
822                 psfs->max_width = i;
823                 psfs->num_pos_args = -2;
824                 goto DO_QUALIFIER;
825         }
826         ++psfs->fmt;                    /* Advance past '$'. */
827 #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
828
829 #if defined(SPEC_FLAGS) || (defined(NL_ARGMAX) && (NL_ARGMAX > 0))
830  DO_FLAGS:
831 #endif /* defined(SPEC_FLAGS) || (defined(NL_ARGMAX) && (NL_ARGMAX > 0)) */
832 #ifdef SPEC_FLAGS
833         p = spec_flags;
834         j = FLAG_SURPRESS;
835         do {
836                 if (*p == *psfs->fmt) {
837                         ++psfs->fmt;
838                         psfs->flags |= j;
839                         goto DO_FLAGS;
840                 }
841                 j += j;
842         } while (*++p);
843
844         if (psfs->flags & FLAG_SURPRESS) { /* Suppress assignment. */
845                 psfs->store = 0;
846                 goto DO_WIDTH;
847         }
848 #else  /* SPEC_FLAGS */
849         if (*psfs->fmt == '*') {        /* Suppress assignment. */
850                 ++psfs->fmt;
851                 psfs->store = 0;
852                 goto DO_WIDTH;
853         }
854 #endif /* SPEC_FLAGS */
855
856
857 #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
858         if (fail) {
859                 /* Must be a non-positional arg */
860                 if (psfs->num_pos_args >= 0) { /* Already saw a pos arg! */
861                         goto ERROR_EINVAL;
862                 }
863                 psfs->num_pos_args = -2;
864         } else {
865                 if ((psfs->num_pos_args == -2) || (((unsigned int)(--i)) >= NL_ARGMAX)) {
866                         /* Already saw a non-pos arg or (0-based) num too large. */
867                         goto ERROR_EINVAL;
868                 }
869                 psfs->cur_pos_arg = i;
870         }
871 #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
872
873  DO_WIDTH:
874         for (i = 0 ; __isdigit_char(*psfs->fmt) ; ) {
875                 if (i <= ((INT_MAX - 9)/10)) {
876                         i = (i * 10) + (*psfs->fmt++ - '0');
877                         psfs->max_width = i;
878                 }
879         }
880
881 #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
882  DO_QUALIFIER:
883 #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
884         p = qual_chars;
885         do {
886                 if (*psfs->fmt == *p) {
887                         ++psfs->fmt;
888                         break;
889                 }
890         } while (*++p);
891         if ((p - qual_chars < 2) && (*psfs->fmt == *p)) {
892                 p += ((sizeof(qual_chars)-2) / 2);
893                 ++psfs->fmt;
894         }
895         psfs->dataargtype = ((int)(p[(sizeof(qual_chars)-2) / 2])) << 8;
896
897 #ifdef __UCLIBC_MJN3_ONLY__
898 #warning CONSIDER: Should we validate that psfs->max_width > 0 in __psfs_parse_spec()?  It would avoid whitespace consumption...
899 #warning CONSIDER: Should INT_MAX be a valid width (%c/%C)?  See __psfs_parse_spec().
900 #endif /* __UCLIBC_MJN3_ONLY__ */
901
902         p = spec_chars;
903         do {
904                 if (*psfs->fmt == *p) {
905                         int p_m_spec_chars = p - spec_chars;
906
907 #ifdef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
908 #error implement gnu a flag
909                         if ((*p == 'a')
910                                 && ((psfs->fmt[1] == '[') || ((psfs->fmt[1]|0x20) == 's'))
911                                 ) {             /* Assumes ascii for 's' and 'S' test. */
912                                 psfs->flags |= FLAG_MALLOC;
913                                 ++psfs->fmt;
914                                 ++p;
915                                 continue; /* The related conversions follow 'a'. */
916                         }
917 #endif /* __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__ */
918
919                         for (p = spec_ranges; p_m_spec_chars > *p ; ++p) {}
920                         if (((psfs->dataargtype >> 8) | psfs->flags)
921                                 & ~spec_allowed[(int)(p - spec_ranges)]
922                                 ) {
923                                 goto ERROR_EINVAL;
924                         }
925
926                         if (p_m_spec_chars == CONV_p) {
927                                 /* a pointer has the same size as 'long int'  */
928                                 psfs->dataargtype = PA_FLAG_LONG;
929                         } else if ((p_m_spec_chars >= CONV_c)
930                                 && (psfs->dataargtype & PA_FLAG_LONG)) {
931                                 p_m_spec_chars -= 3; /* lc -> C, ls -> S, l[ -> ?? */
932                         }
933
934                         psfs->conv_num = p_m_spec_chars;
935                         return psfs->fmt - fmt0;
936                 }
937                 if (!*++p) {
938                 ERROR_EINVAL:
939                         __set_errno(EINVAL);
940                         return -1;
941                 }
942         } while(1);
943
944         assert(0);
945 }
946
947 #endif
948 /**********************************************************************/
949 #if defined(L_vfscanf) || defined(L_vfwscanf)
950
951 #ifdef __UCLIBC_HAS_WCHAR__
952 #ifdef L_vfscanf
953 static int sc_getc(register struct scan_cookie *sc)
954 {
955         return (getc_unlocked)(sc->fp); /* Disable the macro. */
956 }
957
958 static int scan_getwc(register struct scan_cookie *sc)
959 {
960         size_t r;
961         int width;
962         wchar_t wc[1];
963         char b[1];
964
965         if (--sc->width < 0) {
966                 sc->ungot_flag |= 2;
967                 return -1;
968         }
969
970         width = sc->width;                      /* Preserve width. */
971         sc->width = INT_MAX;            /* MB_CUR_MAX can invoke a function. */
972
973         assert(!sc->mb_fail);
974
975         r = (size_t)(-3);
976         while (__scan_getc(sc) >= 0) {
977                 *b = sc->cc;
978
979                 r = mbrtowc(wc, b, 1, &sc->mbstate);
980                 if (((ssize_t) r) >= 0) { /* Successful completion of a wc. */
981                         sc->wc = *wc;
982                         goto SUCCESS;
983                 } else if (r == ((size_t) -2)) {
984                         /* Potentially valid but incomplete. */
985                         continue;
986                 }
987                 break;
988         }
989
990         if (r == ((size_t)(-3))) {      /* EOF or ERROR on first read */
991                 sc->wc = WEOF;
992                 r = (size_t)(-1);
993         } else {
994                 /* If we reach here, either r == ((size_t)-1) and
995                  * mbrtowc set errno to EILSEQ, or r == ((size_t)-2)
996                  * and stream is in an error state or at EOF with a
997                  * partially complete wchar. */
998                 __set_errno(EILSEQ);            /* In case of incomplete conversion. */
999                 sc->mb_fail = 1;
1000         }
1001
1002  SUCCESS:
1003         sc->width = width;                      /* Restore width. */
1004
1005         return (int)((ssize_t) r);
1006 }
1007
1008 #endif /* L_vfscanf */
1009
1010 #ifdef L_vfwscanf
1011
1012 /* This gets called by __scan_getc.  __scan_getc is called by vfwscanf
1013  * when the next wide char is expected to be valid ascii (digits).
1014  */
1015 static int sc_getc(register struct scan_cookie *sc)
1016 {
1017         wint_t wc;
1018
1019         if (__STDIO_STREAM_IS_FAKE_VSWSCANF(sc->fp)) {
1020                 if (sc->fp->__bufpos < sc->fp->__bufend) {
1021                         wc = *((wchar_t *)(sc->fp->__bufpos));
1022                         sc->fp->__bufpos += sizeof(wchar_t);
1023                 } else {
1024                         sc->fp->__modeflags |= __FLAG_EOF;
1025                         return EOF;
1026                 }
1027         } else if ((wc = fgetwc_unlocked(sc->fp)) == WEOF) {
1028                 return EOF;
1029         }
1030
1031         sc->ungot_wflag = 1;
1032         sc->ungot_wchar = wc;
1033         sc->ungot_wchar_width = sc->fp->__ungot_width[0];
1034
1035 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
1036         if (wc == sc->thousands_sep_wc) {
1037                 wc = ',';
1038         } else
1039 #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
1040 #ifdef __UCLIBC_HAS_FLOATS__
1041         if (wc == sc->decpt_wc) {
1042                 wc = '.';
1043         } else
1044 #endif /* __UCLIBC_HAS_FLOATS__ */
1045         sc->wc = sc->ungot_char = wc;
1046
1047         return (int) wc;
1048 }
1049
1050 static int scan_getwc(register struct scan_cookie *sc)
1051 {
1052         wint_t wc;
1053
1054         sc->wc = WEOF;
1055
1056         if (--sc->width < 0) {
1057                 sc->ungot_flag |= 2;
1058                 return -1;
1059         }
1060
1061         if (sc->ungot_flag == 0) {
1062                 if (__STDIO_STREAM_IS_FAKE_VSWSCANF(sc->fp)) {
1063                         if (sc->fp->__bufpos < sc->fp->__bufend) {
1064                                 wc = *((wchar_t *)(sc->fp->__bufpos));
1065                                 sc->fp->__bufpos += sizeof(wchar_t);
1066                         } else {
1067                                 sc->ungot_flag |= 2;
1068                                 return -1;
1069                         }
1070                 } else if ((wc = fgetwc_unlocked(sc->fp)) == WEOF) {
1071                         sc->ungot_flag |= 2;
1072                         return -1;
1073                 }
1074                 sc->ungot_wflag = 1;
1075                 sc->ungot_char = wc;
1076                 sc->ungot_wchar_width = sc->fp->__ungot_width[0];
1077         } else {
1078                 assert(sc->ungot_flag == 1);
1079                 sc->ungot_flag = 0;
1080         }
1081
1082         ++sc->nread;
1083         sc->wc = sc->ungot_char;
1084
1085         return 0;
1086 }
1087
1088
1089 #endif /* L_vfwscanf */
1090 #endif /* __UCLIBC_HAS_WCHAR__ */
1091
1092 static __inline void kill_scan_cookie(register struct scan_cookie *sc)
1093 {
1094 #ifdef L_vfscanf
1095
1096         if (sc->ungot_flag & 1) {
1097 #if !defined(__STDIO_BUFFERS) && !defined(__UCLIBC_HAS_WCHAR__)
1098                 if (!__STDIO_STREAM_IS_FAKE_VSSCANF_NB(sc->fp)) {
1099                         ungetc(sc->ungot_char, sc->fp);
1100                 }
1101 #else
1102                 ungetc(sc->ungot_char, sc->fp);
1103 #endif
1104                 /* Deal with distiction between user and scanf ungots. */
1105                 if (sc->nread == 0) {   /* Only one char was read... app ungot? */
1106                         sc->fp->__ungot[1] = sc->app_ungot; /* restore ungot state. */
1107                 } else {
1108                         sc->fp->__ungot[1] = 0;
1109                 }
1110         }
1111
1112 #else
1113
1114         if ((sc->ungot_flag & 1) && (sc->ungot_wflag & 1)
1115                 && !__STDIO_STREAM_IS_FAKE_VSWSCANF(sc->fp)
1116                 && (sc->fp->__state.__mask == 0)
1117                 ) {
1118                 ungetwc(sc->ungot_char, sc->fp);
1119                 /* Deal with distiction between user and scanf ungots. */
1120                 if (sc->nread == 0) {   /* Only one char was read... app ungot? */
1121                         sc->fp->__ungot[1] = sc->app_ungot; /* restore ungot state. */
1122                 } else {
1123                         sc->fp->__ungot[1] = 0;
1124                 }
1125                 sc->fp->__ungot_width[1] = sc->ungot_wchar_width;
1126         }
1127
1128 #endif
1129 }
1130
1131
1132 int VFSCANF (FILE *__restrict fp, const Wchar *__restrict format, va_list arg)
1133 {
1134         const Wuchar *fmt;
1135         unsigned char *b;
1136
1137 #ifdef L_vfwscanf
1138         wchar_t wbuf[1];
1139         wchar_t *wb;
1140 #endif /* L_vfwscanf */
1141
1142 #if defined(__UCLIBC_HAS_LOCALE__) && !defined(L_vfwscanf) || !defined(L_vfscanf)
1143         mbstate_t mbstate;
1144 #endif
1145
1146         struct scan_cookie sc;
1147         psfs_t psfs;
1148         int i;
1149
1150 #ifdef __UCLIBC_MJN3_ONLY__
1151 #warning TODO: Fix MAX_DIGITS.  We do not do binary, so...!
1152 #endif
1153 #define MAX_DIGITS 65                   /* Allow one leading 0. */
1154         unsigned char buf[MAX_DIGITS+2];
1155 #ifdef L_vfscanf
1156         unsigned char scanset[UCHAR_MAX + 1];
1157         unsigned char invert;           /* Careful!  Meaning changes. */
1158 #endif /* L_vfscanf */
1159         unsigned char fail;
1160         unsigned char zero_conversions = 1;
1161         __STDIO_AUTO_THREADLOCK_VAR;
1162
1163 #ifdef __UCLIBC_MJN3_ONLY__
1164 #warning TODO: Make checking of the format string in C locale an option.
1165 #endif
1166         /* To support old programs, don't check mb validity if in C locale. */
1167 #if defined(__UCLIBC_HAS_LOCALE__) && !defined(L_vfwscanf)
1168         /* ANSI/ISO C99 requires format string to be a valid multibyte string
1169          * beginning and ending in its initial shift state. */
1170         if (__UCLIBC_CURLOCALE->encoding != __ctype_encoding_7_bit) {
1171                 const char *p = format;
1172                 mbstate.__mask = 0;             /* Initialize the mbstate. */
1173                 if (mbsrtowcs(NULL, &p, SIZE_MAX, &mbstate) == ((size_t)(-1))) {
1174                         __set_errno(EINVAL); /* Format string is invalid. */
1175                         return 0;
1176                 }
1177         }
1178 #endif /* defined(__UCLIBC_HAS_LOCALE__) && !defined(L_vfwscanf) */
1179
1180 #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
1181         psfs.num_pos_args = -1;         /* Must start at -1. */
1182         /* Initialize positional arg ptrs to NULL. */
1183         memset(psfs.pos_args, 0, sizeof(psfs.pos_args));
1184 #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
1185
1186         __STDIO_AUTO_THREADLOCK(fp);
1187
1188         __STDIO_STREAM_VALIDATE(fp);
1189
1190         __init_scan_cookie(&sc,fp);
1191 #ifdef __UCLIBC_HAS_WCHAR__
1192         sc.sc_getc = sc_getc;
1193         sc.ungot_wchar_width = sc.fp->__ungot_width[1];
1194
1195 #ifdef L_vfwscanf
1196
1197 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
1198         if (*sc.grouping) {
1199                 sc.thousands_sep = (const unsigned char *) ",";
1200                 sc.tslen = 1;
1201         }
1202 #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
1203
1204 #ifdef __UCLIBC_HAS_FLOATS__
1205         sc.fake_decpt = (const unsigned char *) ".";
1206 #endif /* __UCLIBC_HAS_FLOATS__ */
1207
1208 #else  /* L_vfwscanf */
1209
1210 #ifdef __UCLIBC_HAS_FLOATS__
1211         sc.fake_decpt = sc.decpt;
1212 #endif /* __UCLIBC_HAS_FLOATS__ */
1213
1214 #endif /* L_vfwscanf */
1215
1216 #endif /* __UCLIBC_HAS_WCHAR__ */
1217         psfs.cnt = 0;
1218
1219         /* Note: If we ever wanted to support non-nice codesets, we
1220          * would really need to do a mb->wc conversion here in the
1221          * vfscanf case.  Related changes would have to be made in
1222          * the code that follows... basicly wherever fmt appears. */
1223         for (fmt = (const Wuchar *) format ; *fmt ; /* ++fmt */) {
1224
1225                 psfs.store = 1;
1226                 psfs.flags = 0;
1227 #ifndef NDEBUG
1228                 psfs.cur_ptr = NULL;    /* Debugging aid. */
1229 #endif /* NDEBUG */
1230
1231
1232                 sc.ungot_flag &= 1;             /* Clear (possible fake) EOF. */
1233                 sc.width = psfs.max_width = INT_MAX;
1234
1235                 /* Note: According to the standards, vfscanf does use isspace
1236                  * here. So, if we did a mb->wc conversion, we would have to do
1237                  * something like
1238                  *      ((((__uwchar_t)wc) < UCHAR_MAX) && isspace(wc))
1239                  * because wc might not be in the allowed domain. */
1240                 if (ISSPACE(*fmt)) {
1241                         do {
1242                                 ++fmt;
1243                         } while (ISSPACE(*fmt));
1244                         --fmt;
1245                         psfs.conv_num = CONV_whitespace;
1246                         goto DO_WHITESPACE;
1247                 }
1248
1249                 if (*fmt == '%') {              /* Conversion specification. */
1250                         if (*++fmt == '%') { /* Remember, '%' eats whitespace too. */
1251                                 /* Note: The standard says no conversion occurs.
1252                                  * So do not reset zero_conversions flag. */
1253                                 psfs.conv_num = CONV_percent;
1254                                 goto DO_CONVERSION;
1255                         }
1256
1257
1258 #ifdef L_vfscanf
1259                         psfs.fmt = fmt;
1260 #else  /* L_vfscanf */
1261                         {
1262                                 const __uwchar_t *wf = fmt;
1263                                 psfs.fmt = b = buf;
1264
1265                                 while (*wf && __isascii(*wf) && (b < buf + sizeof(buf) - 1)) {
1266                                         *b++ = *wf++;
1267                                 }
1268 #ifdef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
1269 #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.
1270                                 if ((*b == 'a') && ((*wf == '[') || ((*wf|0x20) == 's'))) {
1271                                         goto DONE;      /* Spec was excessively long. */
1272                                 }
1273 #endif /* __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__ */
1274                                 *b = 0;
1275                                 if (b == buf) { /* Bad conversion specifier! */
1276                                         goto DONE;
1277                                 }
1278                         }
1279 #endif /* L_vfscanf */
1280                         if ((i = __psfs_parse_spec(&psfs)) < 0) { /* Bad conversion specifier! */
1281                                 goto DONE;
1282                         }
1283                         fmt += i;
1284
1285                         if (psfs.store) {
1286 #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
1287                                 if (psfs.num_pos_args == -2) {
1288                                         psfs.cur_ptr = va_arg(arg, void *);
1289                                 } else {
1290                                         while (psfs.cur_pos_arg > psfs.num_pos_args) {
1291                                                 psfs.pos_args[++psfs.num_pos_args] = va_arg(arg, void *);
1292                                         }
1293                                         psfs.cur_ptr = psfs.pos_args[psfs.cur_pos_arg];
1294                                 }
1295 #else  /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
1296                                 psfs.cur_ptr = va_arg(arg, void *);
1297 #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
1298                         }
1299
1300                 DO_CONVERSION:
1301                         /* First, consume white-space if not n, c, [, C, or l[. */
1302                         if ((((1L << CONV_n)|(1L << CONV_C)|(1L << CONV_c)
1303                                  |(1L << CONV_LEFTBRACKET)|(1L << CONV_leftbracket))
1304                                  & (1L << psfs.conv_num)) == 0
1305                                 ) {
1306                         DO_WHITESPACE:
1307                                 while ((__scan_getc(&sc) >= 0)
1308 #ifdef L_vfscanf
1309                                            && isspace(sc.cc)
1310 #else  /* L_vfscanf */
1311                                            && iswspace(sc.wc)
1312 #endif /* L_vfscanf */
1313                                            ) {}
1314                                 __scan_ungetc(&sc);
1315                                 if (psfs.conv_num == CONV_whitespace) {
1316                                         goto NEXT_FMT;
1317                                 }
1318                         }
1319
1320                         sc.width = psfs.max_width; /* Now limit the max width. */
1321
1322                         if (sc.width == 0) { /* 0 width is forbidden. */
1323                                 goto DONE;
1324                         }
1325
1326
1327                         if (psfs.conv_num == CONV_percent) {
1328                                 goto MATCH_CHAR;
1329                         }
1330
1331                         if (psfs.conv_num == CONV_n) {
1332 #ifdef __UCLIBC_MJN3_ONLY__
1333 #warning CONSIDER: Should %n count as a conversion as far as EOF return value?
1334 #endif
1335 /*                              zero_conversions = 0; */
1336                                 if (psfs.store) {
1337                                         _store_inttype(psfs.cur_ptr, psfs.dataargtype,
1338                                                                    (uintmax_t) sc.nread);
1339                                 }
1340                                 goto NEXT_FMT;
1341                         }
1342
1343                         if (psfs.conv_num <= CONV_A) { /* pointer, integer, or float spec */
1344                                 int r = __psfs_do_numeric(&psfs, &sc);
1345 #ifndef L_vfscanf
1346                                 if (sc.ungot_wflag == 1) {      /* fix up  '?', '.', and ',' hacks */
1347                                         sc.cc = sc.ungot_char = sc.ungot_wchar;
1348                                 }
1349 #endif
1350                                 if (r != -1) {  /* Either success or a matching failure. */
1351                                         zero_conversions = 0;
1352                                 }
1353                                 if (r < 0) {
1354                                         goto DONE;
1355                                 }
1356                                 goto NEXT_FMT;
1357                         }
1358
1359                         /* Do string conversions here since they are not common code. */
1360
1361
1362 #ifdef L_vfscanf
1363
1364                         if
1365 #ifdef __UCLIBC_HAS_WCHAR__
1366                                 (psfs.conv_num >= CONV_LEFTBRACKET)
1367 #else  /* __UCLIBC_HAS_WCHAR__ */
1368                                 (psfs.conv_num >= CONV_c)
1369 #endif /* __UCLIBC_HAS_WCHAR__ */
1370                         {
1371                                 b = (psfs.store ? ((unsigned char *) psfs.cur_ptr) : buf);
1372                                 fail = 1;
1373
1374                                 if (psfs.conv_num == CONV_c) {
1375                                         if (sc.width == INT_MAX) {
1376                                                 sc.width = 1;
1377                                         }
1378
1379                                         while (__scan_getc(&sc) >= 0) {
1380                                                 zero_conversions = 0;
1381                                                 *b = sc.cc;
1382                                                 b += psfs.store;
1383                                         }
1384                                         __scan_ungetc(&sc);
1385                                         if (sc.width > 0) {     /* Failed to read all required. */
1386                                                 goto DONE;
1387                                         }
1388                                         psfs.cnt += psfs.store;
1389                                         goto NEXT_FMT;
1390                                 }
1391
1392                                 if (psfs.conv_num == CONV_s) {
1393                                         /* Yes, believe it or not, a %s conversion can store nuls. */
1394                                         while ((__scan_getc(&sc) >= 0) && !isspace(sc.cc)) {
1395                                                 zero_conversions = 0;
1396                                                 *b = sc.cc;
1397                                                 b += psfs.store;
1398                                                 fail = 0;
1399                                         }
1400                                 } else {
1401 #ifdef __UCLIBC_HAS_WCHAR__
1402                                         assert((psfs.conv_num == CONV_LEFTBRACKET) || \
1403                                                    (psfs.conv_num == CONV_leftbracket));
1404 #else /* __UCLIBC_HAS_WCHAR__ */
1405                                         assert((psfs.conv_num == CONV_leftbracket));
1406 #endif /* __UCLIBC_HAS_WCHAR__ */
1407
1408                                         invert = 0;
1409
1410                                         if (*++fmt == '^') {
1411                                                 ++fmt;
1412                                                 invert = 1;
1413                                         }
1414                                         memset(scanset, invert, sizeof(scanset));
1415                                         invert = 1-invert;
1416
1417                                         if (*fmt == ']') {
1418                                                 scanset[(int)(']')] = invert;
1419                                                 ++fmt;
1420                                         }
1421
1422                                         while (*fmt != ']') {
1423                                                 if (!*fmt) { /* No closing ']'. */
1424                                                         goto DONE;
1425                                                 }
1426                                                 if ((*fmt == '-') && (fmt[1] != ']')
1427                                                         && (fmt[-1] < fmt[1]) /* sorted? */
1428                                                         ) {     /* range */
1429                                                         ++fmt;
1430                                                         i = fmt[-2];
1431                                                         /* Note: scanset[i] should already have been done
1432                                                          * in the previous iteration. */
1433                                                         do {
1434                                                                 scanset[++i] = invert;
1435                                                         } while (i < *fmt);
1436                                                         /* Safe to fall through, and a bit smaller. */
1437                                                 }
1438                                                 /* literal char */
1439                                                 scanset[(int) *fmt] = invert;
1440                                                 ++fmt;
1441                                         }
1442
1443 #ifdef __UCLIBC_HAS_WCHAR__
1444                                         if (psfs.conv_num == CONV_LEFTBRACKET) {
1445                                                 goto DO_LEFTBRACKET;
1446                                         }
1447 #endif /* __UCLIBC_HAS_WCHAR__ */
1448
1449
1450                                         while (__scan_getc(&sc) >= 0) {
1451                                                 zero_conversions = 0;
1452                                                 if (!scanset[sc.cc]) {
1453                                                         break;
1454                                                 }
1455                                                 *b = sc.cc;
1456                                                 b += psfs.store;
1457                                                 fail = 0;
1458                                         }
1459                                 }
1460                                 /* Common tail for processing of %s and %[. */
1461
1462                                 __scan_ungetc(&sc);
1463                                 if (fail) {     /* nothing stored! */
1464                                         goto DONE;
1465                                 }
1466                                 *b = 0;         /* Nul-terminate string. */
1467                                 psfs.cnt += psfs.store;
1468                                 goto NEXT_FMT;
1469                         }
1470
1471 #ifdef __UCLIBC_HAS_WCHAR__
1472                 DO_LEFTBRACKET:                 /* Need to do common wide init. */
1473                         if (psfs.conv_num >= CONV_C) {
1474                                 wchar_t wbuf[1];
1475                                 wchar_t *wb;
1476
1477                                 sc.mbstate.__mask = 0;
1478
1479                                 wb = (psfs.store ? ((wchar_t *) psfs.cur_ptr) : wbuf);
1480                                 fail = 1;
1481
1482                                 if (psfs.conv_num == CONV_C) {
1483                                         if (sc.width == INT_MAX) {
1484                                                 sc.width = 1;
1485                                         }
1486
1487                                         while (scan_getwc(&sc) >= 0) {
1488                                                 zero_conversions = 0;
1489                                                 assert(sc.width >= 0);
1490                                                 *wb = sc.wc;
1491                                                 wb += psfs.store;
1492                                         }
1493
1494                                         __scan_ungetc(&sc);
1495                                         if (sc.width > 0) {     /* Failed to read all required. */
1496                                                 goto DONE;
1497                                         }
1498                                         psfs.cnt += psfs.store;
1499                                         goto NEXT_FMT;
1500                                 }
1501
1502
1503                                 if (psfs.conv_num == CONV_S) {
1504                                         /* Yes, believe it or not, a %s conversion can store nuls. */
1505                                         while (scan_getwc(&sc) >= 0) {
1506                                                 zero_conversions = 0;
1507                                                 if ((((__uwchar_t)(sc.wc)) <= UCHAR_MAX) && isspace(sc.wc)) {
1508                                                         break;
1509                                                 }
1510                                                 *wb = sc.wc;
1511                                                 wb += psfs.store;
1512                                                 fail = 0;
1513                                         }
1514                                 } else {
1515                                         assert(psfs.conv_num == CONV_LEFTBRACKET);
1516
1517                                         while (scan_getwc(&sc) >= 0) {
1518                                                 zero_conversions = 0;
1519                                                 if (((__uwchar_t) sc.wc) <= UCHAR_MAX) {
1520                                                         if (!scanset[sc.wc]) {
1521                                                                 break;
1522                                                         }
1523                                                 } else if (invert) {
1524                                                         break;
1525                                                 }
1526                                                 *wb = sc.wc;
1527                                                 wb += psfs.store;
1528                                                 fail = 0;
1529                                         }
1530                                 }
1531                                 /* Common tail for processing of %ls and %l[. */
1532
1533                                 __scan_ungetc(&sc);
1534                                 if (fail || sc.mb_fail) { /* Nothing stored or mb error. */
1535                                         goto DONE;
1536                                 }
1537                                 *wb = 0;                /* Nul-terminate string. */
1538                                 psfs.cnt += psfs.store;
1539                                 goto NEXT_FMT;
1540
1541                         }
1542
1543 #endif /* __UCLIBC_HAS_WCHAR__ */
1544 #else  /* L_vfscanf */
1545
1546                         if (psfs.conv_num >= CONV_C) {
1547                                 b = buf;
1548                                 wb = wbuf;
1549                                 if (psfs.conv_num >= CONV_c) {
1550                                         mbstate.__mask = 0;             /* Initialize the mbstate. */
1551                                         if (psfs.store) {
1552                                                 b = (unsigned char *) psfs.cur_ptr;
1553                                         }
1554                                 } else {
1555                                         if (psfs.store) {
1556                                                 wb = (wchar_t *) psfs.cur_ptr;
1557                                         }
1558                                 }
1559                                 fail = 1;
1560
1561
1562                                 if ((psfs.conv_num == CONV_C) || (psfs.conv_num == CONV_c)) {
1563                                         if (sc.width == INT_MAX) {
1564                                                 sc.width = 1;
1565                                         }
1566
1567                                         while (scan_getwc(&sc) >= 0) {
1568                                                 zero_conversions = 0;
1569                                                 if (psfs.conv_num == CONV_C) {
1570                                                         *wb = sc.wc;
1571                                                         wb += psfs.store;
1572                                                 } else {
1573                                                         i = wcrtomb((char*) b, sc.wc, &mbstate);
1574                                                         if (i < 0) { /* Conversion failure. */
1575                                                                 goto DONE_DO_UNGET;
1576                                                         }
1577                                                         if (psfs.store) {
1578                                                                 b += i;
1579                                                         }
1580                                                 }
1581                                         }
1582                                         __scan_ungetc(&sc);
1583                                         if (sc.width > 0) {     /* Failed to read all required. */
1584                                                 goto DONE;
1585                                         }
1586                                         psfs.cnt += psfs.store;
1587                                         goto NEXT_FMT;
1588                                 }
1589
1590                                 if ((psfs.conv_num == CONV_S) || (psfs.conv_num == CONV_s)) {
1591                                         /* Yes, believe it or not, a %s conversion can store nuls. */
1592                                         while (scan_getwc(&sc) >= 0) {
1593                                                 zero_conversions = 0;
1594                                                 if  (iswspace(sc.wc)) {
1595                                                         break;
1596                                                 }
1597                                                 if (psfs.conv_num == CONV_S) {
1598                                                         *wb = sc.wc;
1599                                                         wb += psfs.store;
1600                                                 } else {
1601                                                         i = wcrtomb((char*) b, sc.wc, &mbstate);
1602                                                         if (i < 0) { /* Conversion failure. */
1603                                                                 goto DONE_DO_UNGET;
1604                                                         }
1605                                                         if (psfs.store) {
1606                                                                 b += i;
1607                                                         }
1608                                                 }
1609                                                 fail = 0;
1610                                         }
1611                                 } else {
1612                                         const wchar_t *sss;
1613                                         const wchar_t *ssp;
1614                                         unsigned char invert = 0;
1615
1616                                         assert((psfs.conv_num == CONV_LEFTBRACKET)
1617                                                    || (psfs.conv_num == CONV_leftbracket));
1618
1619                                         if (*++fmt == '^') {
1620                                                 ++fmt;
1621                                                 invert = 1;
1622                                         }
1623                                         sss = (const wchar_t *) fmt;
1624                                         if (*fmt == ']') {
1625                                                 ++fmt;
1626                                         }
1627                                         while (*fmt != ']') {
1628                                                 if (!*fmt) { /* No closing ']'. */
1629                                                         goto DONE;
1630                                                 }
1631                                                 if ((*fmt == '-') && (fmt[1] != ']')
1632                                                         && (fmt[-1] < fmt[1]) /* sorted? */
1633                                                         ) {     /* range */
1634                                                         ++fmt;
1635                                                 }
1636                                                 ++fmt;
1637                                         }
1638                                         /* Ok... a valid scanset spec. */
1639
1640                                         while (scan_getwc(&sc) >= 0) {
1641                                                 zero_conversions = 0;
1642                                                 ssp = sss;
1643                                                 do {    /* We know sss < fmt. */
1644                                                         if (*ssp == '-') { /* possible range... */
1645                                                                 /* Note: We accept a-c-e (ordered) as
1646                                                                  * equivalent to a-e. */
1647                                                                 if (ssp > sss) {
1648                                                                         if ((++ssp < (const wchar_t *) fmt)
1649                                                                                 && (ssp[-2] < *ssp)     /* sorted? */
1650                                                                                 ) { /* yes */
1651                                                                                 if ((sc.wc >= ssp[-2])
1652                                                                                         && (sc.wc <= *ssp)) {
1653                                                                                         break;
1654                                                                                 }
1655                                                                                 continue; /* not in range */
1656                                                                         }
1657                                                                         --ssp; /* oops... '-' at end, so back up */
1658                                                                 }
1659                                                                 /* false alarm... a literal '-' */
1660                                                         }
1661                                                         if (sc.wc == *ssp) { /* Matched literal char. */
1662                                                                 break;
1663                                                         }
1664                                                 } while (++ssp < (const wchar_t *) fmt);
1665
1666                                                 if ((ssp == (const wchar_t *) fmt) ^ invert) {
1667                                                         /* no match and not inverting
1668                                                          * or match and inverting */
1669                                                         break;
1670                                                 }
1671                                                 if (psfs.conv_num == CONV_LEFTBRACKET) {
1672                                                         *wb = sc.wc;
1673                                                         wb += psfs.store;
1674                                                 } else {
1675                                                         i = wcrtomb((char*) b, sc.wc, &mbstate);
1676                                                         if (i < 0) { /* Conversion failure. */
1677                                                                 goto DONE_DO_UNGET;
1678                                                         }
1679                                                         if (psfs.store) {
1680                                                                 b += i;
1681                                                         }
1682                                                 }
1683                                                 fail = 0;
1684                                         }
1685                                 }
1686                                 /* Common tail for processing of %s and %[. */
1687
1688                                 __scan_ungetc(&sc);
1689                                 if (fail) {     /* nothing stored! */
1690                                         goto DONE;
1691                                 }
1692                                 *wb = 0;                /* Nul-terminate string. */
1693                                 *b = 0;
1694                                 psfs.cnt += psfs.store;
1695                                 goto NEXT_FMT;
1696                         }
1697
1698 #endif /* L_vfscanf */
1699
1700                         assert(0);
1701                         goto DONE;
1702                 } /* conversion specification */
1703
1704         MATCH_CHAR:
1705                 if (__scan_getc(&sc) != *fmt) {
1706 #ifdef L_vfwscanf
1707                 DONE_DO_UNGET:
1708 #endif /* L_vfwscanf */
1709                         __scan_ungetc(&sc);
1710                         goto DONE;
1711                 }
1712
1713         NEXT_FMT:
1714                 ++fmt;
1715                 if (__FERROR_UNLOCKED(fp)) {
1716                         break;
1717                 }
1718         }
1719
1720  DONE:
1721         if (__FERROR_UNLOCKED(fp) || (*fmt && zero_conversions && __FEOF_UNLOCKED(fp))) {
1722                 psfs.cnt = EOF;                 /* Yes, vfwscanf also returns EOF. */
1723         }
1724
1725         kill_scan_cookie(&sc);
1726
1727         __STDIO_STREAM_VALIDATE(fp);
1728
1729         __STDIO_AUTO_THREADUNLOCK(fp);
1730
1731         return psfs.cnt;
1732 }
1733 libc_hidden_def(VFSCANF)
1734 #endif
1735 /**********************************************************************/
1736 #ifdef L___psfs_do_numeric
1737
1738 static const unsigned char spec_base[] = SPEC_BASE;
1739 static const unsigned char nil_string[] = "(nil)";
1740
1741 int attribute_hidden __psfs_do_numeric(psfs_t *psfs, struct scan_cookie *sc)
1742 {
1743         unsigned char *b;
1744         const unsigned char *p;
1745
1746 #ifdef __UCLIBC_HAS_FLOATS__
1747         int exp_adjust = 0;
1748 #endif
1749 #ifdef __UCLIBC_MJN3_ONLY__
1750 #warning TODO: Fix MAX_DIGITS.  We do not do binary, so...!
1751 #warning TODO: Fix buf!
1752 #endif
1753 #define MAX_DIGITS 65                   /* Allow one leading 0. */
1754         unsigned char buf[MAX_DIGITS+2+ 100];
1755         unsigned char usflag, base;
1756         unsigned char nonzero = 0;
1757         unsigned char seendigit = 0;
1758
1759 #ifdef __UCLIBC_MJN3_ONLY__
1760 #warning CONSIDER: What should be returned for an invalid conversion specifier?
1761 #endif
1762 #ifndef __UCLIBC_HAS_FLOATS__
1763         if (psfs->conv_num > CONV_i) { /* floating point */
1764                 goto DONE;
1765         }
1766 #endif
1767
1768         base = spec_base[psfs->conv_num - CONV_p];
1769         usflag = (psfs->conv_num <= CONV_u); /* (1)0 if (un)signed */
1770         b = buf;
1771
1772
1773         if (psfs->conv_num == CONV_p) { /* Pointer */
1774                 p = nil_string;
1775                 do {
1776                         if ((__scan_getc(sc) < 0) || (*p != sc->cc)) {
1777                                 __scan_ungetc(sc);
1778                                 if (p > nil_string) {
1779                                         /* We matched at least the '(' so even if we
1780                                          * are at eof,  we can not match a pointer. */
1781                                         return -2;      /* Matching failure */
1782                                 }
1783                                 break;
1784                         }
1785                         if (!*++p) {   /* Matched (nil), so no unget necessary. */
1786                                 if (psfs->store) {
1787                                         ++psfs->cnt;
1788                                         _store_inttype(psfs->cur_ptr, psfs->dataargtype,
1789                                                                    (uintmax_t)0);
1790                                 }
1791                                 return 0;
1792                         }
1793                 } while (1);
1794
1795 #ifdef __UCLIBC_MJN3_ONLY__
1796 #warning CONSIDER: Should we require a 0x prefix and disallow +/- for pointer %p?
1797 #endif /*  __UCLIBC_MJN3_ONLY__ */
1798         }
1799
1800         __scan_getc(sc);
1801         if (sc->cc < 0) {
1802                 return -1;                              /* Input failure (nothing read yet). */
1803         }
1804
1805         if ((sc->cc == '+') || (sc->cc == '-')) { /* Handle leading sign.*/
1806                 *b++ = sc->cc;
1807                 __scan_getc(sc);
1808         }
1809
1810         if ((base & 0xef) == 0) { /* 0xef is ~16, so 16 or 0. */
1811                 if (sc->cc == '0') {    /* Possibly set base and handle prefix. */
1812                         __scan_getc(sc);
1813                         if ((sc->cc|0x20) == 'x') { /* Assumes ascii.. x or X. */
1814                                 if (__scan_getc(sc) < 0) {
1815                                         /* Either EOF or error (including wc outside char range).
1816                                          * If EOF or error, this is a matching failure (we read 0x).
1817                                          * If wc outside char range, this is also a matching failure.
1818                                          * Hence, we do an unget (although not really necessary here
1819                                          * and fail. */
1820                                         goto DONE_DO_UNGET;     /* matching failure */
1821                                 }
1822                                 base = 16; /* Base 16 for sure now. */
1823 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
1824                                 /* The prefix is required for hexadecimal floats. */
1825                                 *b++ = '0';
1826                                 *b++ = 'x';
1827 #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
1828                         } else { /* oops... back up */
1829                                 __scan_ungetc(sc);
1830                                 sc->cc = '0';   /* NASTY HACK! */
1831
1832                                 base = (base >> 1) + 8; /* 0->8, 16->16.  no 'if' */
1833 #ifdef __UCLIBC_HAS_FLOATS__
1834                                 if (psfs->conv_num > CONV_i) { /* floating point */
1835                                         base = 10;
1836                                 }
1837 #endif
1838                         }
1839                 } else if (!base) {
1840                         base = 10;
1841                 }
1842         }
1843
1844         /***************** digit grouping **********************/
1845 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
1846
1847         if ((psfs->flags & FLAG_THOUSANDS) && (base == 10)
1848                 && *(p = (const unsigned char *) sc->grouping)
1849                 ) {
1850
1851                 int nblk1, nblk2, nbmax, lastblock, pass, i;
1852
1853
1854 #ifdef __UCLIBC_MJN3_ONLY__
1855 #warning CONSIDER: Should we initalize the grouping blocks in __init_scan_cookie()?
1856 #endif /*  __UCLIBC_MJN3_ONLY__ */
1857                 nbmax = nblk2 = nblk1 = *p;
1858                 if (*++p) {
1859                         nblk2 = *p;
1860                         if (nbmax < nblk2) {
1861                                 nbmax = nblk2;
1862                         }
1863                         assert(!p[1]);
1864                 }
1865
1866                 /* Note: for printf, if 0 and \' flags appear then
1867                  * grouping is done before 0-padding.  Should we
1868                  * strip leading 0's first?  Or add a 0 flag? */
1869
1870                 /* For vfwscanf, sc_getc translates, so the value of sc->cc is
1871                  * either EOF or a char. */
1872
1873                 if (!__isdigit_char_or_EOF(sc->cc)) { /* No starting digit! */
1874 #ifdef __UCLIBC_HAS_FLOATS__
1875                         if (psfs->conv_num > CONV_i) { /* floating point */
1876                                 goto NO_STARTING_DIGIT;
1877                         }
1878 #endif
1879                         goto DONE_DO_UNGET;
1880                 }
1881
1882                 if (sc->cc == '0') {
1883                         seendigit = 1;
1884                         *b++ = '0';                     /* Store the first 0. */
1885 #ifdef __UCLIBC_MJN3_ONLY__
1886 #warning CONSIDER: Should leading 0s be skipped before digit grouping? (printf 0 pad)
1887 #endif /*  __UCLIBC_MJN3_ONLY__ */
1888 #if 0
1889                         do {                            /* But ignore all subsequent 0s. */
1890                                 __scan_getc(sc);
1891                         } while (sc->cc == '0');
1892 #endif
1893                 }
1894                 pass = 0;
1895                 lastblock = 0;
1896                 do {
1897                         i = 0;
1898                         while (__isdigit_char_or_EOF(sc->cc)) {
1899                                 seendigit = 1;
1900                                 if (i == nbmax) { /* too many digits for a block */
1901 #ifdef __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__
1902                                         if (!pass) { /* treat as nongrouped */
1903                                                 if (nonzero) {
1904                                                         goto DO_NO_GROUP;
1905                                                 }
1906                                                 goto DO_TRIM_LEADING_ZEROS;
1907                                         }
1908 #endif
1909                                         if (nbmax > nblk1) {
1910                                                 goto DONE_DO_UNGET;     /* matching failure */
1911                                         }
1912                                         goto DONE_GROUPING_DO_UNGET; /* nbmax == nblk1 */
1913                                 }
1914                                 ++i;
1915
1916                                 if (nonzero || (sc->cc != '0')) {
1917                                         if (b < buf + MAX_DIGITS) {
1918                                                 *b++ = sc->cc;
1919                                                 nonzero = 1;
1920 #ifdef __UCLIBC_HAS_FLOATS__
1921                                         } else {
1922                                                 ++exp_adjust;
1923 #endif
1924                                         }
1925                                 }
1926
1927                                 __scan_getc(sc);
1928                         }
1929
1930                         if (i) {                        /* we saw digits digits */
1931                                 if ((i == nblk2) || ((i < nblk2) && !pass)) {
1932                                         /* (possible) outer grp */
1933                                         p = sc->thousands_sep;
1934                                         if (*p == sc->cc) {     /* first byte matches... */
1935                                                 /* so check if grouping mb char */
1936                                                 /* Since 1st matched, either match or fail now
1937                                                  * unless EOF (yuk) */
1938                                                 __scan_getc(sc);
1939                                         MBG_LOOP:
1940                                                 if (!*++p) { /* is a grouping mb char */
1941                                                         lastblock = i;
1942                                                         ++pass;
1943                                                         continue;
1944                                                 }
1945                                                 if (*p == sc->cc) {
1946                                                         __scan_getc(sc);
1947                                                         goto MBG_LOOP;
1948                                                 }
1949                                                 /* bad grouping mb char! */
1950                                                 __scan_ungetc(sc);
1951                                                 if ((sc->cc >= 0) || (p > sc->thousands_sep + 1)) {
1952 #ifdef __UCLIBC_HAS_FLOATS__
1953                                                         /* We failed to match a thousep mb char, and
1954                                                          * we've read too much to recover.  But if
1955                                                          * this is a floating point conversion and
1956                                                          * the initial portion of the decpt mb char
1957                                                          * matches, then we may still be able to
1958                                                          * recover. */
1959                                                         int k = p - sc->thousands_sep - 1;
1960
1961                                                         if ((psfs->conv_num > CONV_i) /* float conversion */
1962                                                                 && (!pass || (i == nblk1)) /* possible last */
1963                                                                 && !memcmp(sc->thousands_sep, sc->fake_decpt, k)
1964                                                                 /* and prefix matched, so could be decpt */
1965                                                                 ) {
1966                                                                 __scan_getc(sc);
1967                                                                 p = sc->fake_decpt + k;
1968                                                                 do {
1969                                                                         if (!*++p) {
1970                                                                                 strcpy((char*) b, (char*) sc->decpt);
1971                                                                                 b += sc->decpt_len;
1972                                                                                 goto GOT_DECPT;
1973                                                                         }
1974                                                                         if (*p != sc->cc) {
1975                                                                                 __scan_ungetc(sc);
1976                                                                                 break; /* failed */
1977                                                                         }
1978                                                                         __scan_getc(sc);
1979                                                                 } while (1);
1980                                                         }
1981 #endif /* __UCLIBC_HAS_FLOATS__ */
1982                                                         goto DONE;
1983                                                 }
1984                                                 /* was EOF and 1st, so recoverable. */
1985                                         }
1986                                 }
1987                                 if ((i == nblk1) || ((i < nblk1) && !pass)) {
1988                                         /* got an inner group */
1989                                         goto DONE_GROUPING_DO_UNGET;
1990                                 }
1991                                 goto DONE_DO_UNGET;     /* Matching failure. */
1992                         } /* i != 0 */
1993
1994                         assert(pass);
1995
1996                         goto DONE_DO_UNGET;
1997                 } while (1);
1998
1999                 assert(0);                              /* Should never get here. */
2000         }
2001
2002 #endif /***************** digit grouping **********************/
2003
2004         /* Not grouping so first trim all but one leading 0. */
2005 #ifdef __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__
2006         DO_TRIM_LEADING_ZEROS:
2007 #endif /* __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__ */
2008         if (sc->cc == '0') {
2009                 seendigit = 1;
2010                 *b++ = '0';                             /* Store the first 0. */
2011                 do {                                    /* But ignore all subsequent 0s. */
2012                         __scan_getc(sc);
2013                 } while (sc->cc == '0');
2014         }
2015
2016 #ifdef __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__
2017  DO_NO_GROUP:
2018 #endif /* __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__ */
2019         /* At this point, we're ready to start reading digits. */
2020
2021 #define valid_digit(cc,base) (isxdigit(cc) && ((base == 16) || (cc - '0' < base)))
2022
2023         while (valid_digit(sc->cc,base)) { /* Now for significant digits.*/
2024                 if (b - buf < MAX_DIGITS) {
2025                         nonzero = seendigit = 1; /* Set nonzero too 0s trimmed above. */
2026                         *b++ = sc->cc;
2027 #ifdef __UCLIBC_HAS_FLOATS__
2028                 } else {
2029                         ++exp_adjust;
2030 #endif
2031                 }
2032                 __scan_getc(sc);
2033         }
2034
2035 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
2036  DONE_GROUPING_DO_UNGET:
2037 #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
2038         if (psfs->conv_num <= CONV_i) { /* integer conversion */
2039                 __scan_ungetc(sc);
2040                 *b = 0;                                         /* null-terminate */
2041                 if (!seendigit) {
2042                         goto DONE;                              /* No digits! */
2043                 }
2044                 if (psfs->store) {
2045                         if (*buf == '-') {
2046                                 usflag = 0;
2047                         }
2048                         ++psfs->cnt;
2049                         _store_inttype(psfs->cur_ptr, psfs->dataargtype,
2050                                                    (uintmax_t) STRTOUIM((char *) buf, NULL, base, 1-usflag));
2051                 }
2052                 return 0;
2053         }
2054
2055 #ifdef __UCLIBC_HAS_FLOATS__
2056
2057         /* At this point, we have everything left of the decimal point or exponent. */
2058 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
2059  NO_STARTING_DIGIT:
2060 #endif
2061         p = sc->fake_decpt;
2062         do {
2063                 if (!*p) {
2064                         strcpy((char *) b, (char *) sc->decpt);
2065                         b += sc->decpt_len;
2066                         break;
2067                 }
2068                 if (*p != sc->cc) {
2069                         if (p > sc->fake_decpt) {
2070                                 goto DONE_DO_UNGET;     /* matching failure (read some of decpt) */
2071                         }
2072                         goto DO_DIGIT_CHECK;
2073                 }
2074                 ++p;
2075                 __scan_getc(sc);
2076         } while (1);
2077
2078 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
2079  GOT_DECPT:
2080 #endif
2081         if (!nonzero) {
2082                 if (sc->cc == '0') {
2083                         assert(exp_adjust == 0);
2084                         *b++ = '0';
2085                         ++exp_adjust;
2086                         seendigit = 1;
2087                         do {
2088                                 --exp_adjust;
2089                                 __scan_getc(sc);
2090                         } while (sc->cc == '0');
2091                 }
2092         }
2093
2094         while (valid_digit(sc->cc,base)) { /* Process fractional digits.*/
2095                 if (b - buf < MAX_DIGITS) {
2096                         seendigit = 1;
2097                         *b++ = sc->cc;
2098                 }
2099                 __scan_getc(sc);
2100         }
2101
2102  DO_DIGIT_CHECK:
2103         /* Hmm... no decimal point.   */
2104         if (!seendigit) {
2105                 static const unsigned char nan_inf_str[] = "an\0nfinity";
2106
2107                 if (base == 16) {               /* We had a prefix, but no digits! */
2108                         goto DONE_DO_UNGET;     /* matching failure */
2109                 }
2110
2111                 /* Avoid tolower problems for INFINITY in the tr_TR locale. (yuk)*/
2112 #undef TOLOWER
2113 #define TOLOWER(C)     ((C)|0x20)
2114
2115                 switch (TOLOWER(sc->cc)) {
2116                         case 'i':
2117                                 p = nan_inf_str + 3;
2118                                 break;
2119                         case 'n':
2120                                 p = nan_inf_str;
2121                                 break;
2122                         default:
2123                                 /* No digits and not inf or nan. */
2124                                 goto DONE_DO_UNGET;
2125                 }
2126
2127                 *b++ = sc->cc;
2128
2129                 do {
2130                         __scan_getc(sc);
2131                         if (TOLOWER(sc->cc) == *p) {
2132                                 *b++ = sc->cc;
2133                                 ++p;
2134                                 continue;
2135                         }
2136                         if (!*p || (p == nan_inf_str + 5)) { /* match nan/infinity or inf */
2137                                 goto GOT_FLOAT;
2138                         }
2139                         /* Unrecoverable.  Even if on 1st char, we had no digits. */
2140                         goto DONE_DO_UNGET;
2141                 } while (1);
2142         }
2143
2144         /* If we get here, we had some digits. */
2145
2146         if (
2147 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
2148                 ((base == 16) && (((sc->cc)|0x20) == 'p')) ||
2149 #endif
2150                 (((sc->cc)|0x20) == 'e')
2151                 ) {                                             /* Process an exponent. */
2152                 *b++ = sc->cc;
2153
2154                 __scan_getc(sc);
2155                 if (sc->cc < 0) {
2156                         goto DONE_DO_UNGET;     /* matching failure.. no exponent digits */
2157                 }
2158
2159                 if ((sc->cc == '+') || (sc->cc == '-')) { /* Signed exponent? */
2160                         *b++ = sc->cc;
2161                         __scan_getc(sc);
2162                 }
2163
2164 #ifdef __UCLIBC_MJN3_ONLY__
2165 #warning TODO: Fix MAX_EXP_DIGITS!
2166 #endif
2167 #define MAX_EXP_DIGITS 20
2168                 assert(seendigit);
2169                 seendigit = 0;
2170                 nonzero = 0;
2171
2172                 if (sc->cc == '0') {
2173                         seendigit = 1;
2174                         *b++ = '0';
2175                         do {
2176                                 __scan_getc(sc);
2177                         } while (sc->cc == '0');
2178                 }
2179
2180                 while (__isdigit_char_or_EOF(sc->cc)) { /* Exponent digits (base 10).*/
2181                         if (seendigit < MAX_EXP_DIGITS) {
2182                                 ++seendigit;
2183                                 *b++ = sc->cc;
2184                         }
2185                         __scan_getc(sc);
2186                 }
2187
2188                 if (!seendigit) {               /* No digits.  Unrecoverable. */
2189                         goto DONE_DO_UNGET;
2190                 }
2191         }
2192
2193
2194  GOT_FLOAT:
2195         *b = 0;
2196         {
2197                 __fpmax_t x;
2198                 char *e;
2199                 x = __strtofpmax((char *) buf, &e, exp_adjust);
2200                 assert(!*e);
2201                 if (psfs->store) {
2202                         if (psfs->dataargtype & PA_FLAG_LONG_LONG) {
2203                                 *((long double *)psfs->cur_ptr) = (long double) x;
2204                         } else if (psfs->dataargtype & PA_FLAG_LONG) {
2205                                 *((double *)psfs->cur_ptr) = (double) x;
2206                         } else {
2207                                 *((float *)psfs->cur_ptr) = (float) x;
2208                         }
2209                         ++psfs->cnt;
2210                 }
2211                 __scan_ungetc(sc);
2212                 return 0;
2213         }
2214 #endif /* __UCLIBC_HAS_FLOATS__ */
2215
2216  DONE_DO_UNGET:
2217         __scan_ungetc(sc);
2218  DONE:
2219         return -2;                                      /* Matching failure. */
2220
2221 }
2222 #endif
2223 /**********************************************************************/