tools/llvm: Do not build with symbols
[minix3.git] / lib / libc / stdio / vfwscanf.c
blob4b38ea9d6bae729b8b9c3feda4f16cf7eeb4a2c6
1 /* $NetBSD: vfwscanf.c,v 1.11 2013/09/23 12:41:37 pooka Exp $ */
3 /*-
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Chris Torek.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
39 #include <sys/cdefs.h>
40 #if defined(LIBC_SCCS) && !defined(lint)
41 #if 0
42 static char sccsid[] = "@(#)ftell.c 8.2 (Berkeley) 5/4/95";
43 __FBSDID("$FreeBSD: src/lib/libc/stdio/vfwscanf.c,v 1.12 2004/05/02 20:13:29 obrien Exp $");
44 #else
45 __RCSID("$NetBSD: vfwscanf.c,v 1.11 2013/09/23 12:41:37 pooka Exp $");
46 #endif
47 #endif /* LIBC_SCCS and not lint */
49 #include "namespace.h"
50 #include <ctype.h>
51 #include <inttypes.h>
52 #include <assert.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <stddef.h>
56 #include <stdarg.h>
57 #include <string.h>
58 #include <limits.h>
59 #include <wchar.h>
60 #include <wctype.h>
62 #include "reentrant.h"
63 #include "local.h"
65 #include <locale.h>
66 #include "setlocale_local.h"
68 #define BUF 513 /* Maximum length of numeric string. */
71 * Flags used during conversion.
73 #define LONG 0x01 /* l: long or double */
74 #define LONGDBL 0x02 /* L: long double */
75 #define SHORT 0x04 /* h: short */
76 #define SUPPRESS 0x08 /* *: suppress assignment */
77 #define POINTER 0x10 /* p: void * (as hex) */
78 #define NOSKIP 0x20 /* [ or c: do not skip blanks */
79 #define LONGLONG 0x400 /* ll: quad_t (+ deprecated q: quad) */
80 #define INTMAXT 0x800 /* j: intmax_t */
81 #define PTRDIFFT 0x1000 /* t: ptrdiff_t */
82 #define SIZET 0x2000 /* z: size_t */
83 #define SHORTSHORT 0x4000 /* hh: char */
84 #define UNSIGNED 0x8000 /* %[oupxX] conversions */
87 * The following are used in integral conversions only:
88 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS
90 #define SIGNOK 0x40 /* +/- is (still) legal */
91 #define NDIGITS 0x80 /* no digits detected */
92 #define PFXOK 0x100 /* 0x prefix is (still) legal */
93 #define NZDIGITS 0x200 /* no zero digits detected */
94 #define HAVESIGN 0x10000 /* sign detected */
97 * Conversion types.
99 #define CT_CHAR 0 /* %c conversion */
100 #define CT_CCL 1 /* %[...] conversion */
101 #define CT_STRING 2 /* %s conversion */
102 #define CT_INT 3 /* %[dioupxX] conversion */
103 #define CT_FLOAT 4 /* %[efgEFG] conversion */
105 #ifndef NO_FLOATING_POINT
106 static int parsefloat(FILE *, wchar_t *, wchar_t *, locale_t);
107 #endif
109 #define INCCL(_c) \
110 (cclcompl ? (wmemchr(ccls, (_c), (size_t)(ccle - ccls)) == NULL) : \
111 (wmemchr(ccls, (_c), (size_t)(ccle - ccls)) != NULL))
114 * MT-safe version.
117 vfwscanf(FILE * __restrict fp, const wchar_t * __restrict fmt, va_list ap)
119 return vfwscanf_l(fp, _current_locale(), fmt, ap);
123 vfwscanf_l(FILE * __restrict fp, locale_t loc, const wchar_t * __restrict fmt,
124 va_list ap)
126 int ret;
128 FLOCKFILE(fp);
129 _SET_ORIENTATION(fp, 1);
130 ret = __vfwscanf_unlocked_l(fp, loc, fmt, ap);
131 FUNLOCKFILE(fp);
132 return ret;
135 #define SCANF_SKIP_SPACE() \
136 do { \
137 wint_t tc; \
139 while ((tc = __fgetwc_unlock(fp)) != WEOF && iswspace_l(tc, loc)) \
140 continue; \
141 if (tc != WEOF) \
142 ungetwc(tc, fp); \
143 } while (/*CONSTCOND*/ 0)
146 * Non-MT-safe version.
149 __vfwscanf_unlocked_l(FILE * __restrict fp, locale_t loc,
150 const wchar_t * __restrict fmt, va_list ap)
152 wint_t c; /* character from format, or conversion */
153 size_t width; /* field width, or 0 */
154 wchar_t *p; /* points into all kinds of strings */
155 int n; /* handy integer */
156 int flags; /* flags as defined above */
157 wchar_t *p0; /* saves original value of p when necessary */
158 int nassigned; /* number of fields assigned */
159 int nconversions; /* number of conversions */
160 size_t nread; /* number of characters consumed from fp */
161 int base; /* base argument to conversion function */
162 wchar_t buf[BUF]; /* buffer for numeric conversions */
163 const wchar_t *ccls; /* character class start */
164 const wchar_t *ccle; /* character class end */
165 int cclcompl; /* ccl is complemented? */
166 wint_t wi; /* handy wint_t */
167 char *mbp; /* multibyte string pointer for %c %s %[ */
168 size_t nconv; /* number of bytes in mb. conversion */
169 static const mbstate_t initial;
170 mbstate_t mbs;
171 char mbbuf[MB_LEN_MAX]; /* temporary mb. character buffer */
172 /* `basefix' is used to avoid `if' tests in the integer scanner */
173 static short basefix[17] =
174 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
176 nassigned = 0;
177 nconversions = 0;
178 nread = 0;
179 ccls = ccle = NULL;
180 base = 0;
181 cclcompl = 0;
182 mbp = NULL;
183 for (;;) {
184 c = *fmt++;
185 if (c == 0)
186 return nassigned;
187 if (iswspace_l(c, loc)) {
188 while ((c = __fgetwc_unlock(fp)) != WEOF &&
189 iswspace_l(c, loc))
191 if (c != WEOF)
192 ungetwc(c, fp);
193 continue;
195 if (c != '%')
196 goto literal;
197 width = 0;
198 flags = 0;
200 * switch on the format. continue if done;
201 * break once format type is derived.
203 again: c = *fmt++;
204 switch (c) {
205 case '%':
206 SCANF_SKIP_SPACE();
207 literal:
208 if ((wi = __fgetwc_unlock(fp)) == WEOF)
209 goto input_failure;
210 if (wi != c) {
211 ungetwc(wi, fp);
212 goto input_failure;
214 nread++;
215 continue;
217 case '*':
218 flags |= SUPPRESS;
219 goto again;
220 case 'j':
221 flags |= INTMAXT;
222 goto again;
223 case 'l':
224 if (flags & LONG) {
225 flags &= ~LONG;
226 flags |= LONGLONG;
227 } else
228 flags |= LONG;
229 goto again;
230 case 'q':
231 flags |= LONGLONG; /* not quite */
232 goto again;
233 case 't':
234 flags |= PTRDIFFT;
235 goto again;
236 case 'z':
237 flags |= SIZET;
238 goto again;
239 case 'L':
240 flags |= LONGDBL;
241 goto again;
242 case 'h':
243 if (flags & SHORT) {
244 flags &= ~SHORT;
245 flags |= SHORTSHORT;
246 } else
247 flags |= SHORT;
248 goto again;
250 case '0': case '1': case '2': case '3': case '4':
251 case '5': case '6': case '7': case '8': case '9':
252 width = width * 10 + c - '0';
253 goto again;
256 * Conversions.
258 case 'd':
259 c = CT_INT;
260 base = 10;
261 break;
263 case 'i':
264 c = CT_INT;
265 base = 0;
266 break;
268 case 'o':
269 c = CT_INT;
270 flags |= UNSIGNED;
271 base = 8;
272 break;
274 case 'u':
275 c = CT_INT;
276 flags |= UNSIGNED;
277 base = 10;
278 break;
280 case 'X':
281 case 'x':
282 flags |= PFXOK; /* enable 0x prefixing */
283 c = CT_INT;
284 flags |= UNSIGNED;
285 base = 16;
286 break;
288 #ifndef NO_FLOATING_POINT
289 case 'A': case 'E': case 'F': case 'G':
290 case 'a': case 'e': case 'f': case 'g':
291 c = CT_FLOAT;
292 break;
293 #endif
295 case 'S':
296 flags |= LONG;
297 /* FALLTHROUGH */
298 case 's':
299 c = CT_STRING;
300 break;
302 case '[':
303 ccls = fmt;
304 if (*fmt == '^') {
305 cclcompl = 1;
306 fmt++;
307 } else
308 cclcompl = 0;
309 if (*fmt == ']')
310 fmt++;
311 while (*fmt != '\0' && *fmt != ']')
312 fmt++;
313 ccle = fmt;
314 fmt++;
315 flags |= NOSKIP;
316 c = CT_CCL;
317 break;
319 case 'C':
320 flags |= LONG;
321 /* FALLTHROUGH */
322 case 'c':
323 flags |= NOSKIP;
324 c = CT_CHAR;
325 break;
327 case 'p': /* pointer format is like hex */
328 flags |= POINTER | PFXOK;
329 c = CT_INT; /* assumes sizeof(uintmax_t) */
330 flags |= UNSIGNED; /* >= sizeof(uintptr_t) */
331 base = 16;
332 break;
334 case 'n':
335 nconversions++;
336 if (flags & SUPPRESS) /* ??? */
337 continue;
338 if (flags & SHORTSHORT)
339 *va_arg(ap, char *) = (char)nread;
340 else if (flags & SHORT)
341 *va_arg(ap, short *) = (short)nread;
342 else if (flags & LONG)
343 *va_arg(ap, long *) = nread;
344 else if (flags & LONGLONG)
345 *va_arg(ap, quad_t *) = nread;
346 else if (flags & INTMAXT)
347 *va_arg(ap, intmax_t *) = nread;
348 else if (flags & SIZET)
349 *va_arg(ap, size_t *) = nread;
350 else if (flags & PTRDIFFT)
351 *va_arg(ap, ptrdiff_t *) = nread;
352 else
353 *va_arg(ap, int *) = (int)nread;
354 continue;
356 default:
357 goto match_failure;
360 * Disgusting backwards compatibility hack. XXX
362 case '\0': /* compat */
363 return EOF;
367 * Consume leading white space, except for formats
368 * that suppress this.
370 if ((flags & NOSKIP) == 0) {
371 while ((wi = __fgetwc_unlock(fp)) != WEOF &&
372 iswspace_l(wi, loc))
373 nread++;
374 if (wi == WEOF)
375 goto input_failure;
376 ungetwc(wi, fp);
380 * Do the conversion.
382 switch (c) {
384 case CT_CHAR:
385 /* scan arbitrary characters (sets NOSKIP) */
386 if (width == 0)
387 width = 1;
388 if (flags & LONG) {
389 if (!(flags & SUPPRESS))
390 p = va_arg(ap, wchar_t *);
391 n = 0;
392 while (width-- != 0 &&
393 (wi = __fgetwc_unlock(fp)) != WEOF) {
394 if (!(flags & SUPPRESS))
395 *p++ = (wchar_t)wi;
396 n++;
398 if (n == 0)
399 goto input_failure;
400 nread += n;
401 if (!(flags & SUPPRESS))
402 nassigned++;
403 } else {
404 if (!(flags & SUPPRESS))
405 mbp = va_arg(ap, char *);
406 n = 0;
407 mbs = initial;
408 while (width != 0 &&
409 (wi = __fgetwc_unlock(fp)) != WEOF) {
410 if (width >= MB_CUR_MAX_L(loc) &&
411 !(flags & SUPPRESS)) {
412 nconv = wcrtomb_l(mbp, wi,
413 &mbs, loc);
414 if (nconv == (size_t)-1)
415 goto input_failure;
416 } else {
417 nconv = wcrtomb_l(mbbuf, wi,
418 &mbs, loc);
419 if (nconv == (size_t)-1)
420 goto input_failure;
421 if (nconv > width) {
422 ungetwc(wi, fp);
423 break;
425 if (!(flags & SUPPRESS))
426 memcpy(mbp, mbbuf,
427 nconv);
429 if (!(flags & SUPPRESS))
430 mbp += nconv;
431 width -= nconv;
432 n++;
434 if (n == 0)
435 goto input_failure;
436 nread += n;
437 if (!(flags & SUPPRESS))
438 nassigned++;
440 nconversions++;
441 break;
443 case CT_CCL:
444 /* scan a (nonempty) character class (sets NOSKIP) */
445 if (width == 0)
446 width = (size_t)~0; /* `infinity' */
447 /* take only those things in the class */
448 if ((flags & SUPPRESS) && (flags & LONG)) {
449 n = 0;
450 while ((wi = __fgetwc_unlock(fp)) != WEOF &&
451 width-- != 0 && INCCL(wi))
452 n++;
453 if (wi != WEOF)
454 ungetwc(wi, fp);
455 if (n == 0)
456 goto match_failure;
457 } else if (flags & LONG) {
458 p0 = p = va_arg(ap, wchar_t *);
459 while ((wi = __fgetwc_unlock(fp)) != WEOF &&
460 width-- != 0 && INCCL(wi))
461 *p++ = (wchar_t)wi;
462 if (wi != WEOF)
463 ungetwc(wi, fp);
464 _DIAGASSERT(__type_fit(int, p - p0));
465 n = (int)(p - p0);
466 if (n == 0)
467 goto match_failure;
468 *p = 0;
469 nassigned++;
470 } else {
471 if (!(flags & SUPPRESS))
472 mbp = va_arg(ap, char *);
473 n = 0;
474 mbs = initial;
475 while ((wi = __fgetwc_unlock(fp)) != WEOF &&
476 width != 0 && INCCL(wi)) {
477 if (width >= MB_CUR_MAX_L(loc) &&
478 !(flags & SUPPRESS)) {
479 nconv = wcrtomb_l(mbp, wi,
480 &mbs, loc);
481 if (nconv == (size_t)-1)
482 goto input_failure;
483 } else {
484 nconv = wcrtomb_l(mbbuf, wi,
485 &mbs, loc);
486 if (nconv == (size_t)-1)
487 goto input_failure;
488 if (nconv > width)
489 break;
490 if (!(flags & SUPPRESS))
491 memcpy(mbp, mbbuf,
492 nconv);
494 if (!(flags & SUPPRESS))
495 mbp += nconv;
496 width -= nconv;
497 n++;
499 if (wi != WEOF)
500 ungetwc(wi, fp);
501 if (!(flags & SUPPRESS)) {
502 *mbp = 0;
503 nassigned++;
506 nread += n;
507 nconversions++;
508 break;
510 case CT_STRING:
511 /* like CCL, but zero-length string OK, & no NOSKIP */
512 if (width == 0)
513 width = (size_t)~0;
514 if ((flags & SUPPRESS) && (flags & LONG)) {
515 while ((wi = __fgetwc_unlock(fp)) != WEOF &&
516 width-- != 0 &&
517 !iswspace_l(wi, loc))
518 nread++;
519 if (wi != WEOF)
520 ungetwc(wi, fp);
521 } else if (flags & LONG) {
522 p0 = p = va_arg(ap, wchar_t *);
523 while ((wi = __fgetwc_unlock(fp)) != WEOF &&
524 width-- != 0 &&
525 !iswspace_l(wi, loc)) {
526 *p++ = (wchar_t)wi;
527 nread++;
529 if (wi != WEOF)
530 ungetwc(wi, fp);
531 *p = '\0';
532 nassigned++;
533 } else {
534 if (!(flags & SUPPRESS))
535 mbp = va_arg(ap, char *);
536 mbs = initial;
537 while ((wi = __fgetwc_unlock(fp)) != WEOF &&
538 width != 0 &&
539 !iswspace_l(wi, loc)) {
540 if (width >= MB_CUR_MAX_L(loc) &&
541 !(flags & SUPPRESS)) {
542 nconv = wcrtomb_l(mbp, wi,
543 &mbs, loc);
544 if (nconv == (size_t)-1)
545 goto input_failure;
546 } else {
547 nconv = wcrtomb_l(mbbuf, wi,
548 &mbs, loc);
549 if (nconv == (size_t)-1)
550 goto input_failure;
551 if (nconv > width)
552 break;
553 if (!(flags & SUPPRESS))
554 memcpy(mbp, mbbuf,
555 nconv);
557 if (!(flags & SUPPRESS))
558 mbp += nconv;
559 width -= nconv;
560 nread++;
562 if (wi != WEOF)
563 ungetwc(wi, fp);
564 if (!(flags & SUPPRESS)) {
565 *mbp = 0;
566 nassigned++;
569 nconversions++;
570 continue;
572 case CT_INT:
573 /* scan an integer as if by the conversion function */
574 if (width == 0 || width > sizeof(buf) /
575 sizeof(*buf) - 1)
576 width = sizeof(buf) / sizeof(*buf) - 1;
577 flags |= SIGNOK | NDIGITS | NZDIGITS;
578 for (p = buf; width; width--) {
579 c = __fgetwc_unlock(fp);
581 * Switch on the character; `goto ok'
582 * if we accept it as a part of number.
584 switch (c) {
587 * The digit 0 is always legal, but is
588 * special. For %i conversions, if no
589 * digits (zero or nonzero) have been
590 * scanned (only signs), we will have
591 * base==0. In that case, we should set
592 * it to 8 and enable 0x prefixing.
593 * Also, if we have not scanned zero digits
594 * before this, do not turn off prefixing
595 * (someone else will turn it off if we
596 * have scanned any nonzero digits).
598 case '0':
599 if (base == 0) {
600 base = 8;
601 flags |= PFXOK;
603 if (flags & NZDIGITS)
604 flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
605 else
606 flags &= ~(SIGNOK|PFXOK|NDIGITS);
607 goto ok;
609 /* 1 through 7 always legal */
610 case '1': case '2': case '3':
611 case '4': case '5': case '6': case '7':
612 base = basefix[base];
613 flags &= ~(SIGNOK | PFXOK | NDIGITS);
614 goto ok;
616 /* digits 8 and 9 ok iff decimal or hex */
617 case '8': case '9':
618 base = basefix[base];
619 if (base <= 8)
620 break; /* not legal here */
621 flags &= ~(SIGNOK | PFXOK | NDIGITS);
622 goto ok;
624 /* letters ok iff hex */
625 case 'A': case 'B': case 'C':
626 case 'D': case 'E': case 'F':
627 case 'a': case 'b': case 'c':
628 case 'd': case 'e': case 'f':
629 /* no need to fix base here */
630 if (base <= 10)
631 break; /* not legal here */
632 flags &= ~(SIGNOK | PFXOK | NDIGITS);
633 goto ok;
635 /* sign ok only as first character */
636 case '+': case '-':
637 if (flags & SIGNOK) {
638 flags &= ~SIGNOK;
639 flags |= HAVESIGN;
640 goto ok;
642 break;
645 * x ok iff flag still set & 2nd char (or
646 * 3rd char if we have a sign).
648 case 'x': case 'X':
649 if (flags & PFXOK && p ==
650 buf + 1 + !!(flags & HAVESIGN)) {
651 base = 16; /* if %i */
652 flags &= ~PFXOK;
653 goto ok;
655 break;
659 * If we got here, c is not a legal character
660 * for a number. Stop accumulating digits.
662 if (c != WEOF)
663 ungetwc(c, fp);
664 break;
667 * c is legal: store it and look at the next.
669 *p++ = (wchar_t)c;
672 * If we had only a sign, it is no good; push
673 * back the sign. If the number ends in `x',
674 * it was [sign] '0' 'x', so push back the x
675 * and treat it as [sign] '0'.
677 if (flags & NDIGITS) {
678 if (p > buf)
679 ungetwc(*--p, fp);
680 goto match_failure;
682 c = p[-1];
683 if (c == 'x' || c == 'X') {
684 --p;
685 ungetwc(c, fp);
687 if ((flags & SUPPRESS) == 0) {
688 uintmax_t res;
690 *p = 0;
691 if ((flags & UNSIGNED) == 0)
692 res = wcstoimax_l(buf, NULL, base, loc);
693 else
694 res = wcstoumax_l(buf, NULL, base, loc);
695 if (flags & POINTER)
696 *va_arg(ap, void **) =
697 (void *)(uintptr_t)res;
698 else if (flags & SHORTSHORT)
699 *va_arg(ap, char *) = (char)res;
700 else if (flags & SHORT)
701 *va_arg(ap, short *) = (short)res;
702 else if (flags & LONG)
703 *va_arg(ap, long *) = (long)res;
704 else if (flags & LONGLONG)
705 *va_arg(ap, quad_t *) = res;
706 else if (flags & INTMAXT)
707 *va_arg(ap, intmax_t *) = res;
708 else if (flags & PTRDIFFT)
709 *va_arg(ap, ptrdiff_t *) = (ptrdiff_t)res;
710 else if (flags & SIZET)
711 *va_arg(ap, size_t *) = (size_t)res;
712 else
713 *va_arg(ap, int *) = (int)res;
714 nassigned++;
716 _DIAGASSERT(__type_fit(int, p - buf));
717 nread += (int)(p - buf);
718 nconversions++;
719 break;
721 #ifndef NO_FLOATING_POINT
722 case CT_FLOAT:
723 /* scan a floating point number as if by strtod */
724 if (width == 0 || width > sizeof(buf) /
725 sizeof(*buf) - 1)
726 width = sizeof(buf) / sizeof(*buf) - 1;
727 if ((width = parsefloat(fp, buf, buf + width, loc)) == 0)
728 goto match_failure;
729 if ((flags & SUPPRESS) == 0) {
730 if (flags & LONGDBL) {
731 long double res = wcstold_l(buf, &p,
732 loc);
733 *va_arg(ap, long double *) = res;
734 } else
735 if (flags & LONG) {
736 double res = wcstod_l(buf, &p, loc);
737 *va_arg(ap, double *) = res;
738 } else {
739 float res = wcstof_l(buf, &p, loc);
740 *va_arg(ap, float *) = res;
742 #ifdef DEBUG
743 if (p - buf != (ptrdiff_t)width)
744 abort();
745 #endif
746 nassigned++;
748 nread += width;
749 nconversions++;
750 break;
751 #endif /* !NO_FLOATING_POINT */
754 input_failure:
755 return nconversions != 0 ? nassigned : EOF;
756 match_failure:
757 return nassigned;
760 #ifndef NO_FLOATING_POINT
761 static int
762 parsefloat(FILE *fp, wchar_t *buf, wchar_t *end, locale_t loc)
764 wchar_t *commit, *p;
765 int infnanpos = 0;
766 enum {
767 S_START, S_GOTSIGN, S_INF, S_NAN, S_MAYBEHEX,
768 S_DIGITS, S_FRAC, S_EXP, S_EXPDIGITS
769 } state = S_START;
770 wchar_t c;
771 wchar_t decpt = (wchar_t)(unsigned char)*localeconv_l(loc)->decimal_point;
772 int gotmantdig = 0, ishex = 0;
775 * We set commit = p whenever the string we have read so far
776 * constitutes a valid representation of a floating point
777 * number by itself. At some point, the parse will complete
778 * or fail, and we will ungetc() back to the last commit point.
779 * To ensure that the file offset gets updated properly, it is
780 * always necessary to read at least one character that doesn't
781 * match; thus, we can't short-circuit "infinity" or "nan(...)".
783 commit = buf - 1;
784 c = WEOF;
785 for (p = buf; p < end; ) {
786 if ((c = __fgetwc_unlock(fp)) == WEOF)
787 break;
788 reswitch:
789 switch (state) {
790 case S_START:
791 state = S_GOTSIGN;
792 if (c == '-' || c == '+')
793 break;
794 else
795 goto reswitch;
796 case S_GOTSIGN:
797 switch (c) {
798 case '0':
799 state = S_MAYBEHEX;
800 commit = p;
801 break;
802 case 'I':
803 case 'i':
804 state = S_INF;
805 break;
806 case 'N':
807 case 'n':
808 state = S_NAN;
809 break;
810 default:
811 state = S_DIGITS;
812 goto reswitch;
814 break;
815 case S_INF:
816 if (infnanpos > 6 ||
817 (c != "nfinity"[infnanpos] &&
818 c != "NFINITY"[infnanpos]))
819 goto parsedone;
820 if (infnanpos == 1 || infnanpos == 6)
821 commit = p; /* inf or infinity */
822 infnanpos++;
823 break;
824 case S_NAN:
825 switch (infnanpos) {
826 case -1: /* XXX kludge to deal with nan(...) */
827 goto parsedone;
828 case 0:
829 if (c != 'A' && c != 'a')
830 goto parsedone;
831 break;
832 case 1:
833 if (c != 'N' && c != 'n')
834 goto parsedone;
835 else
836 commit = p;
837 break;
838 case 2:
839 if (c != '(')
840 goto parsedone;
841 break;
842 default:
843 if (c == ')') {
844 commit = p;
845 infnanpos = -2;
846 } else if (!iswalnum_l(c, loc) && c != '_')
847 goto parsedone;
848 break;
850 infnanpos++;
851 break;
852 case S_MAYBEHEX:
853 state = S_DIGITS;
854 if (c == 'X' || c == 'x') {
855 ishex = 1;
856 break;
857 } else { /* we saw a '0', but no 'x' */
858 gotmantdig = 1;
859 goto reswitch;
861 case S_DIGITS:
862 if ((ishex && iswxdigit_l(c, loc)) ||
863 iswdigit_l(c, loc))
864 gotmantdig = 1;
865 else {
866 state = S_FRAC;
867 if (c != decpt)
868 goto reswitch;
870 if (gotmantdig)
871 commit = p;
872 break;
873 case S_FRAC:
874 if (((c == 'E' || c == 'e') && !ishex) ||
875 ((c == 'P' || c == 'p') && ishex)) {
876 if (!gotmantdig)
877 goto parsedone;
878 else
879 state = S_EXP;
880 } else if ((ishex && iswxdigit_l(c, loc)) ||
881 iswdigit_l(c, loc)) {
882 commit = p;
883 gotmantdig = 1;
884 } else
885 goto parsedone;
886 break;
887 case S_EXP:
888 state = S_EXPDIGITS;
889 if (c == '-' || c == '+')
890 break;
891 else
892 goto reswitch;
893 case S_EXPDIGITS:
894 if (iswdigit_l(c, loc))
895 commit = p;
896 else
897 goto parsedone;
898 break;
899 default:
900 abort();
902 *p++ = c;
903 c = WEOF;
906 parsedone:
907 if (c != WEOF)
908 ungetwc(c, fp);
909 while (commit < --p)
910 ungetwc(*p, fp);
911 *++commit = '\0';
912 _DIAGASSERT(__type_fit(int, commit - buf));
913 return (int)(commit - buf);
915 #endif