#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)vfscanf.c 8.1 (Berkeley) 6/4/93";
#endif
#include <sys/cdefs.h>
__FBSDID("$FreeBSD: src/lib/libc/stdio/vfscanf.c,v 1.43 2009/01/19 06:19:51 das Exp $");
#include "xlocale_private.h"
#include "namespace.h"
#include <ctype.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdarg.h>
#include <string.h>
#include <wchar.h>
#include <wctype.h>
#include <pthread.h>
#include "un-namespace.h"
#include "collate.h"
#include "libc_private.h"
#include "local.h"
#ifndef NO_FLOATING_POINT
#include <locale.h>
#endif
#define BUF 513
#define LONG 0x01
#define LONGDBL 0x02
#define SHORT 0x04
#define SUPPRESS 0x08
#define POINTER 0x10
#define NOSKIP 0x20
#define LONGLONG 0x400
#define INTMAXT 0x800
#define PTRDIFFT 0x1000
#define SIZET 0x2000
#define SHORTSHORT 0x4000
#define UNSIGNED 0x8000
#define SIGNOK 0x40
#define NDIGITS 0x80
#define PFXOK 0x100
#define NZDIGITS 0x200
#define HAVESIGN 0x10000
#define CT_CHAR 0
#define CT_CCL 1
#define CT_STRING 2
#define CT_INT 3
#define CT_FLOAT 4
static const u_char *__sccl(char *, const u_char *, locale_t);
#ifndef NO_FLOATING_POINT
static int parsefloat(FILE *, char **, size_t, locale_t);
#endif
__weak_reference(__vfscanf, vfscanf);
int
__vfscanf(FILE * __restrict fp, char const * __restrict fmt0, va_list ap)
{
int ret;
FLOCKFILE(fp);
ret = __svfscanf_l(fp, __current_locale(), fmt0, ap);
FUNLOCKFILE(fp);
return (ret);
}
int
vfscanf_l(FILE * __restrict fp, locale_t loc, char const * __restrict fmt0, va_list ap)
{
int ret;
NORMALIZE_LOCALE(loc);
FLOCKFILE(fp);
ret = __svfscanf_l(fp, loc, fmt0, ap);
FUNLOCKFILE(fp);
return (ret);
}
__private_extern__ int
__svfscanf_l(FILE * __restrict fp, locale_t loc, const char * __restrict fmt0, va_list ap)
{
const u_char *fmt = (const u_char *)fmt0;
int c;
size_t width;
char *p;
int n;
int flags;
char *p0;
int nassigned;
int nread;
int base;
char ccltab[256];
char buf[BUF];
wchar_t *wcp;
size_t nconv;
int index;
va_list ap_orig;
static const mbstate_t initial;
mbstate_t mbs;
int mb_cur_max;
static short basefix[17] =
{ 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
NORMALIZE_LOCALE(loc);
mb_cur_max = MB_CUR_MAX_L(loc);
ORIENT(fp, -1);
nassigned = 0;
nread = 0;
va_copy(ap_orig, ap);
for (;;) {
c = *fmt++;
if (c == 0)
return (nassigned);
if (isspace_l(c, loc)) {
while ((fp->_r > 0 || __srefill(fp) == 0) && isspace_l(*fp->_p, loc))
nread++, fp->_r--, fp->_p++;
continue;
}
if (c != '%') {
if (fp->_r <= 0 && __srefill(fp))
goto input_failure;
goto literal;
}
width = 0;
flags = 0;
again: c = *fmt++;
switch (c) {
case '%':
for(;;) {
if (fp->_r <= 0 && __srefill(fp))
goto input_failure;
if (!isspace_l(*fp->_p, loc))
break;
nread++;
fp->_r--;
fp->_p++;
}
literal:
if (*fp->_p != c)
goto match_failure;
fp->_r--, fp->_p++;
nread++;
continue;
case '$':
index = width;
if (index < 1 || index > NL_ARGMAX || fmt[-3] != '%') {
goto input_failure;
}
width = 0;
va_end(ap);
va_copy(ap, ap_orig);
for (; index > 1; index--) {
va_arg(ap, void*);
}
goto again;
case '*':
flags |= SUPPRESS;
goto again;
case 'j':
flags |= INTMAXT;
goto again;
case 'l':
if (flags & LONG) {
flags &= ~LONG;
flags |= LONGLONG;
} else
flags |= LONG;
goto again;
case 'q':
flags |= LONGLONG;
goto again;
case 't':
flags |= PTRDIFFT;
goto again;
case 'z':
flags |= SIZET;
goto again;
case 'L':
flags |= LONGDBL;
goto again;
case 'h':
if (flags & SHORT) {
flags &= ~SHORT;
flags |= SHORTSHORT;
} else
flags |= SHORT;
goto again;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
width = width * 10 + c - '0';
goto again;
case 'd':
c = CT_INT;
base = 10;
break;
case 'i':
c = CT_INT;
base = 0;
break;
case 'o':
c = CT_INT;
flags |= UNSIGNED;
base = 8;
break;
case 'u':
c = CT_INT;
flags |= UNSIGNED;
base = 10;
break;
case 'X':
case 'x':
flags |= PFXOK;
c = CT_INT;
flags |= UNSIGNED;
base = 16;
break;
#ifndef NO_FLOATING_POINT
case 'A': case 'E': case 'F': case 'G':
case 'a': case 'e': case 'f': case 'g':
c = CT_FLOAT;
break;
#endif
case 'S':
flags |= LONG;
case 's':
c = CT_STRING;
break;
case '[':
fmt = __sccl(ccltab, fmt, loc);
flags |= NOSKIP;
c = CT_CCL;
break;
case 'C':
flags |= LONG;
case 'c':
flags |= NOSKIP;
c = CT_CHAR;
break;
case 'p':
flags |= POINTER | PFXOK;
c = CT_INT;
flags |= UNSIGNED;
base = 16;
break;
case 'n':
{
void *ptr = va_arg(ap, void *);
if ((ptr == NULL) || (flags & SUPPRESS))
continue;
else if (flags & SHORTSHORT)
*(char *)ptr = nread;
else if (flags & SHORT)
*(short *)ptr = nread;
else if (flags & LONG)
*(long *)ptr = nread;
else if (flags & LONGLONG)
*(long long *)ptr = nread;
else if (flags & INTMAXT)
*(intmax_t *)ptr = nread;
else if (flags & SIZET)
*(size_t *)ptr = nread;
else if (flags & PTRDIFFT)
*(ptrdiff_t *)ptr = nread;
else
*(int *)ptr = nread;
continue;
}
default:
goto match_failure;
case '\0':
return (EOF);
}
if (fp->_r <= 0 && __srefill(fp))
goto input_failure;
if ((flags & NOSKIP) == 0) {
while (isspace_l(*fp->_p, loc)) {
nread++;
if (--fp->_r > 0)
fp->_p++;
else if (__srefill(fp))
goto input_failure;
}
}
switch (c) {
case CT_CHAR:
if (width == 0)
width = 1;
if (flags & LONG) {
if ((flags & SUPPRESS) == 0)
wcp = va_arg(ap, wchar_t *);
else
wcp = NULL;
n = 0;
while (width != 0) {
if (n == mb_cur_max) {
fp->_flags |= __SERR;
goto input_failure;
}
buf[n++] = *fp->_p;
fp->_p++;
fp->_r--;
mbs = initial;
nconv = mbrtowc_l(wcp, buf, n, &mbs, loc);
if (nconv == (size_t)-1) {
fp->_flags |= __SERR;
goto input_failure;
}
if (nconv == 0 && !(flags & SUPPRESS))
*wcp = L'\0';
if (nconv != (size_t)-2) {
nread += n;
width--;
if (!(flags & SUPPRESS))
wcp++;
n = 0;
}
if (fp->_r <= 0 && __srefill(fp)) {
if (n != 0) {
fp->_flags |= __SERR;
goto input_failure;
}
break;
}
}
if (!(flags & SUPPRESS))
nassigned++;
} else if (flags & SUPPRESS) {
size_t sum = 0;
for (;;) {
if ((n = fp->_r) < width) {
sum += n;
width -= n;
fp->_p += n;
if (__srefill(fp)) {
if (sum == 0)
goto input_failure;
break;
}
} else {
sum += width;
fp->_r -= width;
fp->_p += width;
break;
}
}
nread += sum;
} else {
size_t r = __fread((void *)va_arg(ap, char *), 1,
width, fp);
if (r == 0)
goto input_failure;
nread += r;
nassigned++;
}
break;
case CT_CCL:
if (width == 0)
width = (size_t)~0;
if (flags & LONG) {
wchar_t twc;
int nchars;
if ((flags & SUPPRESS) == 0)
wcp = va_arg(ap, wchar_t *);
else
wcp = &twc;
n = 0;
nchars = 0;
while (width != 0) {
if (n == mb_cur_max) {
fp->_flags |= __SERR;
goto input_failure;
}
buf[n++] = *fp->_p;
fp->_p++;
fp->_r--;
mbs = initial;
nconv = mbrtowc_l(wcp, buf, n, &mbs, loc);
if (nconv == (size_t)-1) {
fp->_flags |= __SERR;
goto input_failure;
}
if (nconv == 0)
*wcp = L'\0';
if (nconv != (size_t)-2) {
if (wctob_l(*wcp, loc) != EOF &&
!ccltab[wctob_l(*wcp, loc)]) {
while (n != 0) {
n--;
__ungetc(buf[n],
fp);
}
break;
}
nread += n;
width--;
if (!(flags & SUPPRESS))
wcp++;
nchars++;
n = 0;
}
if (fp->_r <= 0 && __srefill(fp)) {
if (n != 0) {
fp->_flags |= __SERR;
goto input_failure;
}
break;
}
}
if (n != 0) {
fp->_flags |= __SERR;
goto input_failure;
}
n = nchars;
if (n == 0)
goto match_failure;
if (!(flags & SUPPRESS)) {
*wcp = L'\0';
nassigned++;
}
} else if (flags & SUPPRESS) {
n = 0;
while (ccltab[*fp->_p]) {
n++, fp->_r--, fp->_p++;
if (--width == 0)
break;
if (fp->_r <= 0 && __srefill(fp)) {
if (n == 0)
goto input_failure;
break;
}
}
if (n == 0)
goto match_failure;
} else {
p0 = p = va_arg(ap, char *);
while (ccltab[*fp->_p]) {
fp->_r--;
*p++ = *fp->_p++;
if (--width == 0)
break;
if (fp->_r <= 0 && __srefill(fp)) {
if (p == p0)
goto input_failure;
break;
}
}
n = p - p0;
if (n == 0)
goto match_failure;
*p = 0;
nassigned++;
}
nread += n;
break;
case CT_STRING:
if (width == 0)
width = (size_t)~0;
if (flags & LONG) {
wchar_t twc;
if ((flags & SUPPRESS) == 0)
wcp = va_arg(ap, wchar_t *);
else
wcp = &twc;
n = 0;
while (width != 0) {
if (n == mb_cur_max) {
fp->_flags |= __SERR;
goto input_failure;
}
buf[n++] = *fp->_p;
fp->_p++;
fp->_r--;
mbs = initial;
nconv = mbrtowc_l(wcp, buf, n, &mbs, loc);
if (nconv == (size_t)-1) {
fp->_flags |= __SERR;
goto input_failure;
}
if (nconv == 0)
*wcp = L'\0';
if (nconv != (size_t)-2) {
if (iswspace_l(*wcp, loc)) {
while (n != 0) {
n--;
__ungetc(buf[n],
fp);
}
break;
}
nread += n;
width--;
if (!(flags & SUPPRESS))
wcp++;
n = 0;
}
if (fp->_r <= 0 && __srefill(fp)) {
if (n != 0) {
fp->_flags |= __SERR;
goto input_failure;
}
break;
}
}
if (!(flags & SUPPRESS)) {
*wcp = L'\0';
nassigned++;
}
} else if (flags & SUPPRESS) {
n = 0;
while (!isspace_l(*fp->_p, loc)) {
n++, fp->_r--, fp->_p++;
if (--width == 0)
break;
if (fp->_r <= 0 && __srefill(fp))
break;
}
nread += n;
} else {
p0 = p = va_arg(ap, char *);
while (!isspace_l(*fp->_p, loc)) {
fp->_r--;
*p++ = *fp->_p++;
if (--width == 0)
break;
if (fp->_r <= 0 && __srefill(fp))
break;
}
*p = 0;
nread += p - p0;
nassigned++;
}
continue;
case CT_INT:
#ifdef hardway
if (width == 0 || width > sizeof(buf) - 1)
width = sizeof(buf) - 1;
#else
if (--width > sizeof(buf) - 2)
width = sizeof(buf) - 2;
width++;
#endif
flags |= SIGNOK | NDIGITS | NZDIGITS;
for (p = buf; width; width--) {
c = *fp->_p;
switch (c) {
case '0':
if (base == 0) {
base = 8;
flags |= PFXOK;
}
if (flags & NZDIGITS)
flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
else
flags &= ~(SIGNOK|PFXOK|NDIGITS);
goto ok;
case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
base = basefix[base];
flags &= ~(SIGNOK | PFXOK | NDIGITS);
goto ok;
case '8': case '9':
base = basefix[base];
if (base <= 8)
break;
flags &= ~(SIGNOK | PFXOK | NDIGITS);
goto ok;
case 'A': case 'B': case 'C':
case 'D': case 'E': case 'F':
case 'a': case 'b': case 'c':
case 'd': case 'e': case 'f':
if (base <= 10)
break;
flags &= ~(SIGNOK | PFXOK | NDIGITS);
goto ok;
case '+': case '-':
if (flags & SIGNOK) {
flags &= ~SIGNOK;
flags |= HAVESIGN;
goto ok;
}
break;
case 'x': case 'X':
if (flags & PFXOK && p ==
buf + 1 + !!(flags & HAVESIGN)) {
base = 16;
flags &= ~PFXOK;
goto ok;
}
break;
}
break;
ok:
*p++ = c;
if (--fp->_r > 0)
fp->_p++;
else if (__srefill(fp))
break;
}
if (flags & NDIGITS) {
if (p > buf)
(void) __ungetc(*(u_char *)--p, fp);
goto match_failure;
}
c = ((u_char *)p)[-1];
if (c == 'x' || c == 'X') {
--p;
(void) __ungetc(c, fp);
}
if ((flags & SUPPRESS) == 0) {
uintmax_t res;
*p = 0;
if ((flags & UNSIGNED) == 0)
res = strtoimax_l(buf, (char **)NULL, base, loc);
else
res = strtoumax_l(buf, (char **)NULL, base, loc);
if (flags & POINTER)
*va_arg(ap, void **) =
(void *)(uintptr_t)res;
else if (flags & SHORTSHORT)
*va_arg(ap, char *) = res;
else if (flags & SHORT)
*va_arg(ap, short *) = res;
else if (flags & LONG)
*va_arg(ap, long *) = res;
else if (flags & LONGLONG)
*va_arg(ap, long long *) = res;
else if (flags & INTMAXT)
*va_arg(ap, intmax_t *) = res;
else if (flags & PTRDIFFT)
*va_arg(ap, ptrdiff_t *) = res;
else if (flags & SIZET)
*va_arg(ap, size_t *) = res;
else
*va_arg(ap, int *) = res;
nassigned++;
}
nread += p - buf;
break;
#ifndef NO_FLOATING_POINT
case CT_FLOAT:
{
char *pbuf;
if ((width = parsefloat(fp, &pbuf, width, loc)) == 0)
goto match_failure;
if ((flags & SUPPRESS) == 0) {
if (flags & LONGDBL) {
long double res = strtold_l(pbuf, &p, loc);
*va_arg(ap, long double *) = res;
} else if (flags & LONG) {
double res = strtod_l(pbuf, &p, loc);
*va_arg(ap, double *) = res;
} else {
float res = strtof_l(pbuf, &p, loc);
*va_arg(ap, float *) = res;
}
nassigned++;
}
nread += width;
break;
}
#endif
}
}
input_failure:
return (nassigned ? nassigned : EOF);
match_failure:
return (nassigned);
}
int
__svfscanf(FILE * __restrict fp, const char * __restrict fmt0, va_list ap)
{
return __svfscanf_l(fp, __current_locale(), fmt0, ap);
}
static const u_char *
__sccl(tab, fmt, loc)
char *tab;
const u_char *fmt;
locale_t loc;
{
int c, n, v, i;
c = *fmt++;
if (c == '^') {
v = 1;
c = *fmt++;
} else
v = 0;
(void) memset(tab, v, 256);
if (c == 0)
return (fmt - 1);
v = 1 - v;
for (;;) {
tab[c] = v;
doswitch:
n = *fmt++;
switch (n) {
case 0:
return (fmt - 1);
case '-':
{
n = *fmt;
if (n == ']'
|| (loc->__collate_load_error ? n < c :
__collate_range_cmp (n, c, loc) < 0
)
) {
c = '-';
break;
}
fmt++;
if (loc->__collate_load_error) {
do {
tab[++c] = v;
} while (c < n);
} else {
for (i = 0; i < 256; i ++)
if ( __collate_range_cmp (c, i, loc) < 0
&& __collate_range_cmp (i, n, loc) <= 0
)
tab[i] = v;
}
#if 1
c = n;
goto doswitch;
#else
c = *fmt++;
if (c == 0)
return (fmt - 1);
if (c == ']')
return (fmt);
#endif
break;
}
case ']':
return (fmt);
default:
c = n;
break;
}
}
}
#ifndef NO_FLOATING_POINT
#ifdef BUILDING_VARIANT
extern char *__parsefloat_buf(size_t s);
#else
__private_extern__ char *
__parsefloat_buf(size_t s)
{
char *b;
static pthread_key_t parsefloat_tsd_key = (pthread_key_t)-1;
static pthread_mutex_t parsefloat_tsd_lock = PTHREAD_MUTEX_INITIALIZER;
static size_t bsiz = 0;
if (parsefloat_tsd_key == (pthread_key_t)-1) {
pthread_mutex_lock(&parsefloat_tsd_lock);
if (parsefloat_tsd_key == (pthread_key_t)-1) {
parsefloat_tsd_key = __LIBC_PTHREAD_KEY_PARSEFLOAT;
pthread_key_init_np(parsefloat_tsd_key, free);
}
pthread_mutex_unlock(&parsefloat_tsd_lock);
}
if ((b = (char *)pthread_getspecific(parsefloat_tsd_key)) == NULL) {
bsiz = s > BUF ? s : BUF;
b = (char *)malloc(bsiz);
if (b == NULL) {
bsiz = 0;
return NULL;
}
pthread_setspecific(parsefloat_tsd_key, b);
return b;
}
if (s > bsiz) {
b = (char *)reallocf(b, s);
pthread_setspecific(parsefloat_tsd_key, b);
if (b == NULL) {
bsiz = 0;
return NULL;
}
bsiz = s;
}
return b;
}
#endif
static int
parsefloat(FILE *fp, char **buf, size_t width, locale_t loc)
{
char *commit, *p;
int infnanpos = 0, decptpos = 0;
enum {
S_START, S_GOTSIGN, S_INF, S_NAN, S_DONE, S_MAYBEHEX,
S_DIGITS, S_DECPT, S_FRAC, S_EXP, S_EXPDIGITS
} state = S_START;
unsigned char c;
const char *decpt = localeconv_l(loc)->decimal_point;
_Bool gotmantdig = 0, ishex = 0;
char *b;
char *e;
size_t s;
s = (width == 0 ? BUF : (width + 1));
if ((b = __parsefloat_buf(s)) == NULL) {
*buf = NULL;
return 0;
}
e = b + (s - 1);
commit = b - 1;
for (p = b; width == 0 || p < e; ) {
c = *fp->_p;
reswitch:
switch (state) {
case S_START:
state = S_GOTSIGN;
if (c == '-' || c == '+')
break;
else
goto reswitch;
case S_GOTSIGN:
switch (c) {
case '0':
state = S_MAYBEHEX;
commit = p;
break;
case 'I':
case 'i':
state = S_INF;
break;
case 'N':
case 'n':
state = S_NAN;
break;
default:
state = S_DIGITS;
goto reswitch;
}
break;
case S_INF:
if (infnanpos > 6 ||
(c != "nfinity"[infnanpos] &&
c != "NFINITY"[infnanpos]))
goto parsedone;
if (infnanpos == 1 || infnanpos == 6)
commit = p;
infnanpos++;
break;
case S_NAN:
switch (infnanpos) {
case 0:
if (c != 'A' && c != 'a')
goto parsedone;
break;
case 1:
if (c != 'N' && c != 'n')
goto parsedone;
else
commit = p;
break;
case 2:
if (c != '(')
goto parsedone;
break;
default:
if (c == ')') {
commit = p;
state = S_DONE;
} else if (!isalnum_l(c, loc) && c != '_')
goto parsedone;
break;
}
infnanpos++;
break;
case S_DONE:
goto parsedone;
case S_MAYBEHEX:
state = S_DIGITS;
if (c == 'X' || c == 'x') {
ishex = 1;
break;
} else {
gotmantdig = 1;
goto reswitch;
}
case S_DIGITS:
if ((ishex && isxdigit_l(c, loc)) || isdigit_l(c, loc)) {
gotmantdig = 1;
commit = p;
break;
} else {
state = S_DECPT;
goto reswitch;
}
case S_DECPT:
if (c == decpt[decptpos]) {
if (decpt[++decptpos] == '\0') {
state = S_FRAC;
if (gotmantdig)
commit = p;
}
break;
} else if (!decptpos) {
state = S_FRAC;
goto reswitch;
} else {
goto parsedone;
}
case S_FRAC:
if (((c == 'E' || c == 'e') && !ishex) ||
((c == 'P' || c == 'p') && ishex)) {
if (!gotmantdig)
goto parsedone;
else
state = S_EXP;
} else if ((ishex && isxdigit_l(c, loc)) || isdigit_l(c, loc)) {
commit = p;
gotmantdig = 1;
} else
goto parsedone;
break;
case S_EXP:
state = S_EXPDIGITS;
if (c == '-' || c == '+')
break;
else
goto reswitch;
case S_EXPDIGITS:
if (isdigit_l(c, loc))
commit = p;
else
goto parsedone;
break;
default:
LIBC_ABORT("unknown state %d", state);
}
if (p >= e) {
ssize_t diff = (p - b);
ssize_t com = (commit - b);
s += BUF;
b = __parsefloat_buf(s);
if (b == NULL) {
*buf = NULL;
return 0;
}
e = b + (s - 1);
p = b + diff;
commit = b + com;
}
*p++ = c;
if (--fp->_r > 0)
fp->_p++;
else if (__srefill(fp))
break;
}
parsedone:
while (commit < --p)
__ungetc(*(u_char *)p, fp);
*++commit = '\0';
*buf = b;
return (commit - b);
}
#endif