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