sfvscanf.c revision 1
1N/A/***********************************************************************
1N/A* *
1N/A* This software is part of the ast package *
1N/A* Copyright (c) 1985-2011 AT&T Intellectual Property *
1N/A* and is licensed under the *
1N/A* Common Public License, Version 1.0 *
1N/A* by AT&T Intellectual Property *
1N/A* *
1N/A* A copy of the License is available at *
1N/A* http://www.opensource.org/licenses/cpl1.0.txt *
1N/A* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
1N/A* *
1N/A* Information and Software Systems Research *
1N/A* AT&T Research *
1N/A* Florham Park NJ *
1N/A* *
1N/A* Glenn Fowler <gsf@research.att.com> *
1N/A* David Korn <dgk@research.att.com> *
1N/A* Phong Vo <kpv@research.att.com> *
1N/A* *
1N/A***********************************************************************/
1N/A#include "sfhdr.h"
1N/A
1N/A/* The main engine for reading formatted data
1N/A**
1N/A** Written by Kiem-Phong Vo.
1N/A*/
1N/A
1N/A#define MAXWIDTH (int)(((uint)~0)>>1) /* max amount to scan */
1N/A
1N/A/*
1N/A * pull in a private strtold()
1N/A */
1N/A
1N/A#include "sfstrtof.h"
1N/A
1N/A/* refresh stream buffer - taking care of unseekable/share streams too */
1N/A#if __STD_C
1N/Astatic void _sfbuf(Sfio_t* f, int* peek)
1N/A#else
1N/Astatic void _sfbuf(f, peek)
1N/ASfio_t* f;
1N/Aint* peek;
1N/A#endif
1N/A{
1N/A if(f->next >= f->endb)
1N/A { if(*peek) /* try peeking for a share stream if possible */
1N/A { f->mode |= SF_RV;
1N/A if(SFFILBUF(f,-1) > 0)
1N/A { f->mode |= SF_PEEK;
1N/A return;
1N/A }
1N/A *peek = 0; /* can't peek, back to normal reads */
1N/A }
1N/A (void)SFFILBUF(f,-1);
1N/A }
1N/A}
1N/A
1N/A/* buffer used during scanning of a double value or a multi-byte
1N/A character. the fields mirror certain local variables in sfvscanf. */
1N/Atypedef struct _scan_s
1N/A{ int error; /* get set by _sfdscan if no value specified */
1N/A int inp; /* last input character read */
1N/A int width; /* field width */
1N/A Sfio_t *f; /* stream being scanned */
1N/A uchar *d, *endd, *data; /* local buffering system */
1N/A int peek; /* != 0 if unseekable/share stream */
1N/A int n_input;/* number of input bytes processed */
1N/A} Scan_t;
1N/A
1N/A/* ds != 0 for scanning double values */
1N/A#define SCinit(sc,ds) ((sc)->inp = (sc)->error = -1, (sc)->f = f, \
1N/A ((sc)->width = (ds) ? width : -1), \
1N/A (sc)->d = d, (sc)->endd = endd, (sc)->data = data, \
1N/A (sc)->peek = peek, (sc)->n_input = n_input)
1N/A#define SCend(sc,ds) (inp = (sc)->inp, f = (sc)->f, \
1N/A (width = (ds) ? (sc)->width : width), \
1N/A d = (sc)->d, endd = (sc)->endd, data = (sc)->data, \
1N/A peek = (sc)->peek, n_input = (sc)->n_input)
1N/A
1N/A#if __STD_C
1N/Astatic int _scgetc(void* arg, int flag)
1N/A#else
1N/Astatic int _scgetc(arg, flag)
1N/Avoid* arg;
1N/Aint flag;
1N/A#endif
1N/A{
1N/A Scan_t *sc = (Scan_t*)arg;
1N/A
1N/A if (flag)
1N/A { sc->error = flag;
1N/A return 0;
1N/A }
1N/A
1N/A /* if width >= 0, do not allow to exceed width number of bytes */
1N/A if(sc->width == 0)
1N/A { sc->inp = -1;
1N/A return 0;
1N/A }
1N/A
1N/A if(sc->d >= sc->endd) /* refresh local buffer */
1N/A { sc->n_input += sc->d - sc->data;
1N/A if(sc->peek)
1N/A SFREAD(sc->f, sc->data, sc->d - sc->data);
1N/A else sc->f->next = sc->d;
1N/A
1N/A _sfbuf(sc->f, &sc->peek);
1N/A sc->data = sc->d = sc->f->next;
1N/A sc->endd = sc->f->endb;
1N/A
1N/A if(sc->d >= sc->endd)
1N/A { sc->inp = -1;
1N/A return 0;
1N/A }
1N/A }
1N/A
1N/A if((sc->width -= 1) >= 0) /* from _sfdscan */
1N/A return (sc->inp = (int)(*sc->d++));
1N/A else return ((int)(*sc->d++));
1N/A}
1N/A
1N/A/* structure to match characters in a character class */
1N/Atypedef struct _accept_s
1N/A{ char ok[SF_MAXCHAR+1];
1N/A int yes;
1N/A char *form, *endf;
1N/A#if _has_multibyte
1N/A wchar_t wc;
1N/A#endif
1N/A} Accept_t;
1N/A
1N/A#if __STD_C
1N/Astatic char* _sfsetclass(const char* form, Accept_t* ac, int flags)
1N/A#else
1N/Astatic char* _sfsetclass(form, ac, flags)
1N/Achar* form; /* format string */
1N/AAccept_t* ac; /* values of accepted characters */
1N/Aint flags; /* SFFMT_LONG for wchar_t */
1N/A#endif
1N/A{
1N/A int c, endc, n;
1N/A#if _has_multibyte
1N/A SFMBDCL(mbs)
1N/A#endif
1N/A
1N/A if(*form == '^') /* complementing this set */
1N/A { ac->yes = 0;
1N/A form += 1;
1N/A }
1N/A else ac->yes = 1;
1N/A
1N/A for(c = 0; c <= SF_MAXCHAR; ++c)
1N/A ac->ok[c] = !ac->yes;
1N/A
1N/A if(*form == ']' || *form == '-') /* special first char */
1N/A { ac->ok[*form] = ac->yes;
1N/A form += 1;
1N/A }
1N/A ac->form = (char*)form;
1N/A
1N/A if(flags&SFFMT_LONG)
1N/A SFMBCLR(&mbs);
1N/A for(n = 1; *form != ']'; form += n)
1N/A { if((c = *((uchar*)form)) == 0)
1N/A return NIL(char*);
1N/A
1N/A if(*(form+1) == '-')
1N/A { endc = *((uchar*)(form+2));
1N/A#if _has_multibyte
1N/A if(c >= 128 || endc >= 128 ) /* range must be ascii */
1N/A goto one_char;
1N/A#endif
1N/A for(; c <= endc; ++c)
1N/A ac->ok[c] = ac->yes;
1N/A n = 3;
1N/A }
1N/A else
1N/A { one_char:
1N/A#if _has_multibyte /* true multi-byte chars must be checked differently */
1N/A if((flags&SFFMT_LONG) && (n = (int)SFMBLEN(form,&mbs)) <= 0)
1N/A return NIL(char*);
1N/A if(n == 1)
1N/A#endif
1N/A ac->ok[c] = ac->yes;
1N/A }
1N/A }
1N/A
1N/A ac->endf = (char*)form;
1N/A return (char*)(form+1);
1N/A}
1N/A
1N/A#if _has_multibyte
1N/A#if __STD_C
1N/Astatic int _sfwaccept(wchar_t wc, Accept_t* ac)
1N/A#else
1N/Astatic int _sfwaccept(wc, ac)
1N/Awchar_t wc;
1N/AAccept_t* ac;
1N/A#endif
1N/A{
1N/A int endc, c, n;
1N/A wchar_t fwc;
1N/A char *form = ac->form;
1N/A SFMBDCL(mbs)
1N/A
1N/A SFMBCLR(&mbs);
1N/A for(n = 1; *form != ']'; form += n)
1N/A { if((c = *((uchar*)form)) == 0)
1N/A return 0;
1N/A
1N/A if(*(form+1) == '-')
1N/A { endc = *((uchar*)(form+2));
1N/A if(c >= 128 || endc >= 128 ) /* range must be ascii */
1N/A goto one_char;
1N/A n = 3;
1N/A }
1N/A else
1N/A { one_char:
1N/A if((n = mbrtowc(&fwc, form, ac->endf-form, &mbs)) > 1 &&
1N/A wc == fwc )
1N/A return ac->yes;
1N/A }
1N/A }
1N/A
1N/A return !ac->yes;
1N/A}
1N/A
1N/A#if _has_multibyte == 1
1N/A#define SFgetwc(sc,wc,fmt,ac,mbs) _sfgetwc(sc,wc,fmt,ac,(Void_t*)(mbs))
1N/A#else
1N/A#define SFgetwc(sc,wc,fmt,ac,mbs) _sfgetwc(sc,wc,fmt,ac,NIL(Void_t*))
1N/A#endif
1N/A
1N/A#if __STD_C
1N/Astatic int _sfgetwc(Scan_t* sc, wchar_t* wc, int fmt, Accept_t* ac, Void_t *mbs)
1N/A#else
1N/Astatic int _sfgetwc(sc, wc, fmt, ac, mbs)
1N/AScan_t* sc; /* the scanning handle */
1N/Awchar_t* wc; /* to return a scanned wchar_t */
1N/Aint fmt; /* %s, %c, %[ */
1N/AAccept_t* ac; /* accept handle for %[ */
1N/AVoid_t* mbs; /* multibyte parsing state */
1N/A#endif
1N/A{
1N/A int n, v;
1N/A char b[16]; /* assuming that SFMBMAX <= 16! */
1N/A
1N/A /* shift left data so that there will be more room to back up on error.
1N/A this won't help streams with small buffers - c'est la vie! */
1N/A if(sc->d > sc->f->data && (n = sc->endd - sc->d) > 0 && n < SFMBMAX)
1N/A { memcpy(sc->f->data, sc->d, n);
1N/A if(sc->f->endr == sc->f->endb)
1N/A sc->f->endr = sc->f->data+n;
1N/A if(sc->f->endw == sc->f->endb)
1N/A sc->f->endw = sc->f->data+n;
1N/A sc->f->endb = sc->f->data+n;
1N/A sc->d = sc->data = sc->f->data;
1N/A sc->endd = sc->f->endb;
1N/A if(!mbs) sc->f->endb = sc->endd; /* stop cc's "unused mbs" warning */
1N/A }
1N/A
1N/A for(n = 0; n < SFMBMAX; )
1N/A { if((v = _scgetc((Void_t*)sc, 0)) <= 0)
1N/A goto no_match;
1N/A else b[n++] = v;
1N/A
1N/A if(mbrtowc(wc, b, n, (mbstate_t*)mbs) == (size_t)(-1))
1N/A goto no_match; /* malformed multi-byte char */
1N/A else
1N/A { /* multi-byte char converted successfully */
1N/A if(fmt == 'c')
1N/A return 1;
1N/A else if(fmt == 's')
1N/A { if(n > 1 || (n == 1 && !isspace(b[0]) ) )
1N/A return 1;
1N/A else goto no_match;
1N/A }
1N/A else if(fmt == '[')
1N/A { if((n == 1 && ac->ok[b[0]]) ||
1N/A (n > 1 && _sfwaccept(*wc,ac)) )
1N/A return 1;
1N/A else goto no_match;
1N/A }
1N/A else /* if(fmt == '1') match a single wchar_t */
1N/A { if(*wc == ac->wc)
1N/A return 1;
1N/A else goto no_match;
1N/A }
1N/A }
1N/A }
1N/A
1N/Ano_match: /* this unget is lossy on a stream with small buffer */
1N/A if((sc->d -= n) < sc->data)
1N/A sc->d = sc->data;
1N/A return 0;
1N/A}
1N/A#endif /*_has_multibyte*/
1N/A
1N/A
1N/A#if __STD_C
1N/Aint sfvscanf(Sfio_t* f, reg const char* form, va_list args)
1N/A#else
1N/Aint sfvscanf(f,form,args)
1N/ASfio_t* f; /* file to be scanned */
1N/Areg char* form; /* scanning format */
1N/Ava_list args;
1N/A#endif
1N/A{
1N/A reg int inp, shift, base, width;
1N/A ssize_t size;
1N/A int fmt, flags, dot, n_assign, v, n, n_input;
1N/A char *sp;
1N/A
1N/A Accept_t acc;
1N/A
1N/A Argv_t argv;
1N/A Sffmt_t *ft;
1N/A Fmt_t *fm, *fmstk;
1N/A
1N/A Fmtpos_t* fp;
1N/A char *oform;
1N/A va_list oargs;
1N/A int argp, argn;
1N/A
1N/A int decimal = 0, thousand = 0;
1N/A
1N/A#if _has_multibyte
1N/A wchar_t wc;
1N/A SFMBDCL(fmbs)
1N/A SFMBDCL(mbs)
1N/A#endif
1N/A
1N/A Void_t* value; /* location to assign scanned value */
1N/A char* t_str;
1N/A ssize_t n_str;
1N/A
1N/A /* local buffering system */
1N/A Scan_t scd;
1N/A uchar *d, *endd, *data;
1N/A int peek;
1N/A#define SFbuf(f) (_sfbuf(f,&peek), (data = d = f->next), (endd = f->endb) )
1N/A#define SFlen(f) (d - data)
1N/A#define SFinit(f) ((peek = f->extent < 0 && (f->flags&SF_SHARE)), SFbuf(f) )
1N/A#define SFend(f) ((n_input += SFlen(f)), \
1N/A (peek ? SFREAD(f,(Void_t*)data,SFlen(f)) : ((f->next = d),0)) )
1N/A#define SFgetc(f,c) ((c) = (d < endd || (SFend(f), SFbuf(f), d < endd)) ? \
1N/A (int)(*d++) : -1 )
1N/A#define SFungetc(f,c) (d -= 1)
1N/A
1N/A SFMTXDECL(f);
1N/A
1N/A SFCVINIT(); /* initialize conversion tables */
1N/A
1N/A SFMTXENTER(f,-1);
1N/A
1N/A if(!form || f->mode != SF_READ && _sfmode(f,SF_READ,0) < 0)
1N/A SFMTXRETURN(f, -1);
1N/A SFLOCK(f,0);
1N/A
1N/A SFinit(f); /* initialize local buffering system */
1N/A
1N/A n_assign = n_input = 0; inp = -1;
1N/A
1N/A fmstk = NIL(Fmt_t*);
1N/A ft = NIL(Sffmt_t*);
1N/A
1N/A fp = NIL(Fmtpos_t*);
1N/A argn = -1;
1N/A oform = (char*)form;
1N/A va_copy(oargs,args);
1N/A
1N/A SFSETLOCALE(&decimal, &thousand);
1N/A
1N/Aloop_fmt:
1N/A SFMBCLR(&fmbs);
1N/A while((fmt = *form++))
1N/A { if(fmt != '%')
1N/A { if(isspace(fmt))
1N/A { if(fmt != '\n' || !(f->flags&SF_LINE))
1N/A fmt = -1;
1N/A for(;;)
1N/A { if(SFgetc(f,inp) < 0 || inp == fmt)
1N/A goto loop_fmt;
1N/A else if(!isspace(inp))
1N/A { SFungetc(f,inp);
1N/A goto loop_fmt;
1N/A }
1N/A }
1N/A }
1N/A else
1N/A { match_1:
1N/A#if _has_multibyte
1N/A if((n = (int)mbrtowc(&wc,form-1,SFMBMAX,&fmbs)) <= 0)
1N/A goto pop_fmt;
1N/A if(n > 1)
1N/A { acc.wc = wc;
1N/A SCinit(&scd,0); SFMBCLR(&mbs);
1N/A v = SFgetwc(&scd, &wc, '1', &acc, &mbs);
1N/A SCend(&scd,0);
1N/A if(v == 0)
1N/A goto pop_fmt;
1N/A form += n-1;
1N/A }
1N/A else
1N/A#endif
1N/A if(SFgetc(f,inp) != fmt)
1N/A { if(inp < 0)
1N/A goto done;
1N/A SFungetc(f,inp);
1N/A goto pop_fmt;
1N/A }
1N/A }
1N/A continue;
1N/A }
1N/A
1N/A if(*form == '%')
1N/A { form += 1;
1N/A do SFgetc(f,inp); while(isspace(inp)); /* skip starting blanks */
1N/A SFungetc(f,inp);
1N/A goto match_1;
1N/A }
1N/A
1N/A if(*form == '\0')
1N/A goto pop_fmt;
1N/A
1N/A if(*form == '*')
1N/A { flags = SFFMT_SKIP;
1N/A form += 1;
1N/A }
1N/A else flags = 0;
1N/A
1N/A /* matching some pattern */
1N/A base = 10; size = -1;
1N/A width = dot = 0;
1N/A t_str = NIL(char*); n_str = 0;
1N/A value = NIL(Void_t*);
1N/A argp = -1;
1N/A
1N/A loop_flags: /* LOOP FOR FLAGS, WIDTH, BASE, TYPE */
1N/A switch((fmt = *form++) )
1N/A {
1N/A case LEFTP : /* get the type which is enclosed in balanced () */
1N/A t_str = (char*)form;
1N/A for(v = 1;;)
1N/A { switch(*form++)
1N/A {
1N/A case 0 : /* not balanceable, retract */
1N/A form = t_str;
1N/A t_str = NIL(char*);
1N/A n_str = 0;
1N/A goto loop_flags;
1N/A case LEFTP : /* increasing nested level */
1N/A v += 1;
1N/A continue;
1N/A case RIGHTP : /* decreasing nested level */
1N/A if((v -= 1) != 0)
1N/A continue;
1N/A if(*t_str != '*' )
1N/A n_str = (form-1) - t_str;
1N/A else
1N/A { t_str = (*_Sffmtintf)(t_str+1,&n);
1N/A if(*t_str == '$')
1N/A { if(!fp &&
1N/A !(fp = (*_Sffmtposf)(f,oform,oargs,ft,1)) )
1N/A goto pop_fmt;
1N/A n = FP_SET(n,argn);
1N/A }
1N/A else n = FP_SET(-1,argn);
1N/A
1N/A if(fp)
1N/A { t_str = fp[n].argv.s;
1N/A n_str = fp[n].ft.size;
1N/A }
1N/A else if(ft && ft->extf )
1N/A { FMTSET(ft, form,args,
1N/A LEFTP, 0, 0, 0,0,0,
1N/A NIL(char*),0);
1N/A n = (*ft->extf)
1N/A (f,(Void_t*)&argv,ft);
1N/A if(n < 0)
1N/A goto pop_fmt;
1N/A if(!(ft->flags&SFFMT_VALUE) )
1N/A goto t_arg;
1N/A if((t_str = argv.s) &&
1N/A (n_str = (int)ft->size) < 0)
1N/A n_str = strlen(t_str);
1N/A }
1N/A else
1N/A { t_arg:
1N/A if((t_str = va_arg(args,char*)) )
1N/A n_str = strlen(t_str);
1N/A }
1N/A }
1N/A goto loop_flags;
1N/A }
1N/A }
1N/A
1N/A case '#' : /* alternative format */
1N/A flags |= SFFMT_ALTER;
1N/A goto loop_flags;
1N/A
1N/A case '.' : /* width & base */
1N/A dot += 1;
1N/A if(isdigit(*form))
1N/A { fmt = *form++;
1N/A goto dot_size;
1N/A }
1N/A else if(*form == '*')
1N/A { form = (*_Sffmtintf)(form+1,&n);
1N/A if(*form == '$')
1N/A { form += 1;
1N/A if(!fp &&
1N/A !(fp = (*_Sffmtposf)(f,oform,oargs,ft,1)) )
1N/A goto pop_fmt;
1N/A n = FP_SET(n,argn);
1N/A }
1N/A else n = FP_SET(-1,argn);
1N/A
1N/A if(fp)
1N/A v = fp[n].argv.i;
1N/A else if(ft && ft->extf )
1N/A { FMTSET(ft, form,args, '.',dot, 0, 0,0,0,
1N/A NIL(char*), 0);
1N/A if((*ft->extf)(f, (Void_t*)(&argv), ft) < 0)
1N/A goto pop_fmt;
1N/A if(ft->flags&SFFMT_VALUE)
1N/A v = argv.i;
1N/A else v = (dot <= 2) ? va_arg(args,int) : 0;
1N/A }
1N/A else v = (dot <= 2) ? va_arg(args,int) : 0;
1N/A if(v < 0)
1N/A v = 0;
1N/A goto dot_set;
1N/A }
1N/A else goto loop_flags;
1N/A
1N/A case '0' : case '1' : case '2' : case '3' : case '4' :
1N/A case '5' : case '6' : case '7' : case '8' : case '9' :
1N/A dot_size :
1N/A for(v = fmt-'0'; isdigit(*form); ++form)
1N/A v = v*10 + (*form - '0');
1N/A
1N/A if(*form == '$')
1N/A { form += 1;
1N/A if(!fp && !(fp = (*_Sffmtposf)(f,oform,oargs,ft,1)) )
1N/A goto pop_fmt;
1N/A argp = v-1;
1N/A goto loop_flags;
1N/A }
1N/A
1N/A dot_set :
1N/A if(dot == 0 || dot == 1)
1N/A width = v;
1N/A else if(dot == 2)
1N/A base = v;
1N/A goto loop_flags;
1N/A
1N/A case 'I' : /* object size */
1N/A size = -1; flags = (flags & ~SFFMT_TYPES) | SFFMT_IFLAG;
1N/A if(isdigit(*form))
1N/A { for(size = 0, n = *form; isdigit(n); n = *++form)
1N/A size = size*10 + (n - '0');
1N/A }
1N/A else if(*form == '*')
1N/A { form = (*_Sffmtintf)(form+1,&n);
1N/A if(*form == '$')
1N/A { form += 1;
1N/A if(!fp &&
1N/A !(fp = (*_Sffmtposf)(f,oform,oargs,ft,1)))
1N/A goto pop_fmt;
1N/A n = FP_SET(n,argn);
1N/A }
1N/A else n = FP_SET(-1,argn);
1N/A
1N/A if(fp) /* use position list */
1N/A size = fp[n].argv.i;
1N/A else if(ft && ft->extf )
1N/A { FMTSET(ft, form,args, 'I',sizeof(int), 0, 0,0,0,
1N/A NIL(char*), 0);
1N/A if((*ft->extf)(f, (Void_t*)(&argv), ft) < 0)
1N/A goto pop_fmt;
1N/A if(ft->flags&SFFMT_VALUE)
1N/A size = argv.i;
1N/A else size = va_arg(args,int);
1N/A }
1N/A else size = va_arg(args,int);
1N/A }
1N/A goto loop_flags;
1N/A
1N/A case 'l' :
1N/A size = -1; flags &= ~SFFMT_TYPES;
1N/A if(*form == 'l')
1N/A { form += 1;
1N/A flags |= SFFMT_LLONG;
1N/A }
1N/A else flags |= SFFMT_LONG;
1N/A goto loop_flags;
1N/A case 'h' :
1N/A size = -1; flags &= ~SFFMT_TYPES;
1N/A if(*form == 'h')
1N/A { form += 1;
1N/A flags |= SFFMT_SSHORT;
1N/A }
1N/A else flags |= SFFMT_SHORT;
1N/A goto loop_flags;
1N/A case 'L' :
1N/A size = -1; flags = (flags&~SFFMT_TYPES) | SFFMT_LDOUBLE;
1N/A goto loop_flags;
1N/A case 'j' :
1N/A size = -1; flags = (flags&~SFFMT_TYPES) | SFFMT_JFLAG;
1N/A goto loop_flags;
1N/A case 'z' :
1N/A size = -1; flags = (flags&~SFFMT_TYPES) | SFFMT_ZFLAG;
1N/A goto loop_flags;
1N/A case 't' :
1N/A size = -1; flags = (flags&~SFFMT_TYPES) | SFFMT_TFLAG;
1N/A goto loop_flags;
1N/A case QUOTE :
1N/A if(thousand > 0)
1N/A flags |= SFFMT_THOUSAND;
1N/A goto loop_flags;
1N/A }
1N/A
1N/A /* set object size for scalars */
1N/A if(flags & SFFMT_TYPES)
1N/A { if((_Sftype[fmt]&(SFFMT_INT|SFFMT_UINT)) || fmt == 'n')
1N/A { if(flags&SFFMT_LONG)
1N/A size = sizeof(long);
1N/A else if(flags&SFFMT_SHORT)
1N/A size = sizeof(short);
1N/A else if(flags&SFFMT_SSHORT)
1N/A size = sizeof(char);
1N/A else if(flags&SFFMT_TFLAG)
1N/A size = sizeof(ptrdiff_t);
1N/A else if(flags&SFFMT_ZFLAG)
1N/A size = sizeof(size_t);
1N/A else if(flags&(SFFMT_LLONG|SFFMT_JFLAG) )
1N/A size = sizeof(Sflong_t);
1N/A else if(flags&SFFMT_IFLAG)
1N/A { if(size <= 0 ||
1N/A size == sizeof(Sflong_t)*CHAR_BIT )
1N/A size = sizeof(Sflong_t);
1N/A }
1N/A else if(size < 0)
1N/A size = sizeof(int);
1N/A }
1N/A else if(_Sftype[fmt]&SFFMT_FLOAT)
1N/A { if(flags&(SFFMT_LONG|SFFMT_LLONG))
1N/A size = sizeof(double);
1N/A else if(flags&SFFMT_LDOUBLE)
1N/A size = sizeof(Sfdouble_t);
1N/A else if(flags&SFFMT_IFLAG)
1N/A { if(size <= 0)
1N/A size = sizeof(Sfdouble_t);
1N/A }
1N/A else if(size < 0)
1N/A size = sizeof(float);
1N/A }
1N/A else if(_Sftype[fmt]&SFFMT_CHAR)
1N/A {
1N/A#if _has_multibyte
1N/A if((flags&SFFMT_LONG) || fmt == 'C')
1N/A { size = sizeof(wchar_t) > sizeof(int) ?
1N/A sizeof(wchar_t) : sizeof(int);
1N/A } else
1N/A#endif
1N/A if(size < 0)
1N/A size = sizeof(int);
1N/A }
1N/A }
1N/A
1N/A argp = FP_SET(argp,argn);
1N/A if(fp)
1N/A { if(!(fp[argp].ft.flags&SFFMT_SKIP) )
1N/A { n_assign += 1;
1N/A value = fp[argp].argv.vp;
1N/A size = fp[argp].ft.size;
1N/A if(ft && ft->extf && fp[argp].ft.fmt != fp[argp].fmt)
1N/A fmt = fp[argp].ft.fmt;
1N/A }
1N/A else flags |= SFFMT_SKIP;
1N/A }
1N/A else if(ft && ft->extf)
1N/A { FMTSET(ft, form,args, fmt, size,flags, width,0,base, t_str,n_str);
1N/A SFend(f); SFOPEN(f,0);
1N/A v = (*ft->extf)(f, (Void_t*)&argv, ft);
1N/A SFLOCK(f,0); SFbuf(f);
1N/A
1N/A if(v < 0)
1N/A goto pop_fmt;
1N/A else if(v > 0) /* extf comsumed v input bytes */
1N/A { n_input += v;
1N/A if(!(ft->flags&SFFMT_SKIP) )
1N/A n_assign += 1;
1N/A continue;
1N/A }
1N/A else /* if(v == 0): extf did not use input stream */
1N/A { FMTGET(ft, form,args, fmt, size, flags, width,n,base);
1N/A
1N/A if((ft->flags&SFFMT_VALUE) && !(ft->flags&SFFMT_SKIP) )
1N/A value = argv.vp;
1N/A }
1N/A }
1N/A
1N/A if(_Sftype[fmt] == 0) /* unknown pattern */
1N/A goto pop_fmt;
1N/A
1N/A if(fmt == '!')
1N/A { if(!fp)
1N/A fp = (*_Sffmtposf)(f,oform,oargs,ft,1);
1N/A else goto pop_fmt;
1N/A
1N/A if(!(argv.ft = va_arg(args,Sffmt_t*)) )
1N/A continue;
1N/A if(!argv.ft->form && ft ) /* change extension functions */
1N/A { if(ft->eventf &&
1N/A (*ft->eventf)(f,SF_DPOP,(Void_t*)form,ft) < 0)
1N/A continue;
1N/A fmstk->ft = ft = argv.ft;
1N/A }
1N/A else /* stack a new environment */
1N/A { if(!(fm = (Fmt_t*)malloc(sizeof(Fmt_t))) )
1N/A goto done;
1N/A
1N/A ft = fm->ft = argv.ft;
1N/A SFMBSET(ft->mbs, &fmbs);
1N/A if(ft->form)
1N/A { fm->form = (char*)form; SFMBCPY(&fm->mbs,&fmbs);
1N/A va_copy(fm->args,args);
1N/A
1N/A fm->oform = oform;
1N/A va_copy(fm->oargs,oargs);
1N/A fm->argn = argn;
1N/A fm->fp = fp;
1N/A
1N/A form = ft->form; SFMBCLR(ft->mbs);
1N/A va_copy(args,ft->args);
1N/A argn = -1;
1N/A fp = NIL(Fmtpos_t*);
1N/A oform = (char*)form;
1N/A va_copy(oargs,args);
1N/A }
1N/A else fm->form = NIL(char*);
1N/A
1N/A fm->eventf = ft->eventf;
1N/A fm->next = fmstk;
1N/A fmstk = fm;
1N/A }
1N/A continue;
1N/A }
1N/A
1N/A /* get the address to assign value */
1N/A if(!value && !(flags&SFFMT_SKIP) )
1N/A value = va_arg(args,Void_t*);
1N/A
1N/A if(fmt == 'n') /* return length of consumed input */
1N/A {
1N/A#if !_ast_intmax_long
1N/A if(size == sizeof(Sflong_t) )
1N/A *((Sflong_t*)value) = (Sflong_t)(n_input+SFlen(f));
1N/A else
1N/A#endif
1N/A if(size == sizeof(long) )
1N/A *((long*)value) = (long)(n_input+SFlen(f));
1N/A else if(size == sizeof(short) )
1N/A *((short*)value) = (short)(n_input+SFlen(f));
1N/A else if(size == sizeof(uchar))
1N/A *((uchar*)value) = (uchar)(n_input+SFlen(f));
1N/A else *((int*)value) = (int)(n_input+SFlen(f));
1N/A continue;
1N/A }
1N/A
1N/A /* if get here, start scanning input */
1N/A if(width == 0)
1N/A width = fmt == 'c' ? 1 : MAXWIDTH;
1N/A
1N/A /* define the first input character */
1N/A if(fmt == 'c' || fmt == '[' || fmt == 'C' )
1N/A SFgetc(f,inp);
1N/A else
1N/A { do { SFgetc(f,inp); }
1N/A while(isspace(inp)); /* skip starting blanks */
1N/A }
1N/A if(inp < 0)
1N/A goto done;
1N/A
1N/A if(_Sftype[fmt] == SFFMT_FLOAT)
1N/A { SFungetc(f,inp); SCinit(&scd,1);
1N/A argv.ld = _sfdscan((Void_t*)(&scd), _scgetc);
1N/A SCend(&scd,1);
1N/A
1N/A if(scd.error >= 0)
1N/A { if(inp >= 0)
1N/A SFungetc(f, inp);
1N/A goto pop_fmt;
1N/A }
1N/A
1N/A if(value)
1N/A {
1N/A#if !_ast_fltmax_double
1N/A if(size == sizeof(Sfdouble_t))
1N/A *((Sfdouble_t*)value) = argv.ld;
1N/A else
1N/A#endif
1N/A if(size == sizeof(double))
1N/A *((double*)value) = (double)argv.ld;
1N/A else *((float*)value) = (float)argv.ld;
1N/A
1N/A n_assign += 1;
1N/A }
1N/A }
1N/A else if(_Sftype[fmt] == SFFMT_UINT || fmt == 'p')
1N/A { if(inp == '-')
1N/A { SFungetc(f,inp);
1N/A goto pop_fmt;
1N/A }
1N/A else goto int_cvt;
1N/A }
1N/A else if(_Sftype[fmt] == SFFMT_INT)
1N/A { int_cvt:
1N/A if(inp == '-' || inp == '+')
1N/A { if(inp == '-')
1N/A flags |= SFFMT_MINUS;
1N/A while(--width > 0 && SFgetc(f,inp) >= 0)
1N/A if(!isspace(inp))
1N/A break;
1N/A }
1N/A if(inp < 0)
1N/A goto done;
1N/A
1N/A if(fmt == 'o')
1N/A base = 8;
1N/A else if(fmt == 'x' || fmt == 'X' || fmt == 'p')
1N/A base = 16;
1N/A else if(fmt == 'i' && inp == '0') /* self-described data */
1N/A { base = 8;
1N/A if(width > 1) /* peek to see if it's a base-16 */
1N/A { if(SFgetc(f,inp) >= 0)
1N/A { if(inp == 'x' || inp == 'X')
1N/A base = 16;
1N/A SFungetc(f,inp);
1N/A }
1N/A inp = '0';
1N/A }
1N/A }
1N/A
1N/A /* now convert */
1N/A argv.lu = 0;
1N/A if(base == 16)
1N/A { sp = (char*)_Sfcv36;
1N/A shift = 4;
1N/A if(sp[inp] >= 16)
1N/A { SFungetc(f,inp);
1N/A goto pop_fmt;
1N/A }
1N/A if(inp == '0' && --width > 0)
1N/A { /* skip leading 0x or 0X */
1N/A if(SFgetc(f,inp) >= 0 &&
1N/A (inp == 'x' || inp == 'X') && --width > 0)
1N/A SFgetc(f,inp);
1N/A }
1N/A if(inp >= 0 && sp[inp] < 16)
1N/A goto base_shift;
1N/A }
1N/A else if(base == 10)
1N/A { for(n = v = 0;; )
1N/A { /* fast base 10 conversion */
1N/A#define TEN(x) (((x) << 3) + ((x) << 1) )
1N/A if (inp >= '0' && inp <= '9')
1N/A { argv.lu = TEN(argv.lu) + (inp-'0');
1N/A n += 1;
1N/A }
1N/A else if(inp == thousand)
1N/A { if((v && n != 3) || (!v && n > 3) )
1N/A break;
1N/A v = 1; n = 0;
1N/A }
1N/A else break;
1N/A if((width -= 1) <= 0 || SFgetc(f,inp) < 0)
1N/A break;
1N/A }
1N/A if (!n && !v)
1N/A { SFungetc(f,inp);
1N/A goto pop_fmt;
1N/A }
1N/A
1N/A if(fmt == 'i' && inp == '#' && !(flags&SFFMT_ALTER) )
1N/A { base = (int)argv.lu;
1N/A if(base < 2 || base > SF_RADIX)
1N/A goto pop_fmt;
1N/A argv.lu = 0;
1N/A sp = (char*)(base <= 36 ? _Sfcv36 : _Sfcv64);
1N/A if(--width > 0 &&
1N/A SFgetc(f,inp) >= 0 && sp[inp] < base)
1N/A goto base_conv;
1N/A }
1N/A }
1N/A else
1N/A { /* other bases */
1N/A sp = (char*)(base <= 36 ? _Sfcv36 : _Sfcv64);
1N/A if(base < 2 || base > SF_RADIX || sp[inp] >= base)
1N/A { SFungetc(f,inp);
1N/A goto pop_fmt;
1N/A }
1N/A
1N/A base_conv: /* check for power of 2 conversions */
1N/A if((base & ~(base-1)) == base)
1N/A { if(base < 8)
1N/A shift = base < 4 ? 1 : 2;
1N/A else if(base < 32)
1N/A shift = base < 16 ? 3 : 4;
1N/A else shift = base < 64 ? 5 : 6;
1N/A
1N/A base_shift: do
1N/A { argv.lu = (argv.lu << shift) + sp[inp];
1N/A } while(--width > 0 &&
1N/A SFgetc(f,inp) >= 0 && sp[inp] < base);
1N/A }
1N/A else
1N/A { do
1N/A { argv.lu = (argv.lu * base) + sp[inp];
1N/A } while(--width > 0 &&
1N/A SFgetc(f,inp) >= 0 && sp[inp] < base);
1N/A }
1N/A }
1N/A
1N/A if(flags&SFFMT_MINUS)
1N/A argv.ll = -argv.ll;
1N/A
1N/A if(value)
1N/A { n_assign += 1;
1N/A
1N/A if(fmt == 'p')
1N/A#if _more_void_int
1N/A *((Void_t**)value) = (Void_t*)((ulong)argv.lu);
1N/A#else
1N/A *((Void_t**)value) = (Void_t*)((uint)argv.lu);
1N/A#endif
1N/A#if !_ast_intmax_long
1N/A else if(size == sizeof(Sflong_t))
1N/A *((Sflong_t*)value) = argv.ll;
1N/A#endif
1N/A else if(size == sizeof(long))
1N/A { if(fmt == 'd' || fmt == 'i')
1N/A *((long*)value) = (long)argv.ll;
1N/A else *((ulong*)value) = (ulong)argv.lu;
1N/A }
1N/A else if(size == sizeof(short))
1N/A { if(fmt == 'd' || fmt == 'i')
1N/A *((short*)value) = (short)argv.ll;
1N/A else *((ushort*)value) = (ushort)argv.lu;
1N/A }
1N/A else if(size == sizeof(char) )
1N/A { if(fmt == 'd' || fmt == 'i')
1N/A *((char*)value) = (char)argv.ll;
1N/A else *((uchar*)value) = (uchar)argv.lu;
1N/A }
1N/A else
1N/A { if(fmt == 'd' || fmt == 'i')
1N/A *((int*)value) = (int)argv.ll;
1N/A else *((uint*)value) = (uint)argv.lu;
1N/A }
1N/A }
1N/A }
1N/A else if(fmt == 'C' || fmt == 'S')
1N/A { fmt = fmt == 'C' ? 'c' : 's';
1N/A flags = (flags & ~SFFMT_TYPES) | SFFMT_LONG;
1N/A goto do_string;
1N/A }
1N/A else if(fmt == 's' || fmt == 'c' || fmt == '[' )
1N/A { do_string:
1N/A if(value)
1N/A { if(size < 0)
1N/A size = MAXWIDTH;
1N/A if(fmt != 'c')
1N/A size -= 1;
1N/A#if _has_multibyte
1N/A if(flags&SFFMT_LONG)
1N/A argv.ws = (wchar_t*)value;
1N/A else
1N/A#endif
1N/A argv.s = (char*)value;
1N/A }
1N/A else size = 0;
1N/A
1N/A if(fmt == '[' && !(form = _sfsetclass(form,&acc,flags)) )
1N/A { SFungetc(f,inp);
1N/A goto pop_fmt;
1N/A }
1N/A
1N/A n = 0; /* count number of scanned characters */
1N/A#if _has_multibyte
1N/A if(flags&SFFMT_LONG)
1N/A { SFungetc(f,inp); SCinit(&scd,0); SFMBCLR(&mbs);
1N/A for(; width > 0; --width)
1N/A { if(SFgetwc(&scd,&wc,fmt,&acc,&mbs) == 0)
1N/A break;
1N/A if((n += 1) <= size)
1N/A *argv.ws++ = wc;
1N/A }
1N/A SCend(&scd,0);
1N/A }
1N/A else
1N/A#endif
1N/A
1N/A if(fmt == 's')
1N/A { do
1N/A { if(isspace(inp))
1N/A break;
1N/A if((n += 1) <= size)
1N/A *argv.s++ = inp;
1N/A } while(--width > 0 && SFgetc(f,inp) >= 0);
1N/A }
1N/A else if(fmt == 'c')
1N/A { do
1N/A { if((n += 1) <= size)
1N/A *argv.s++ = inp;
1N/A } while(--width > 0 && SFgetc(f,inp) >= 0);
1N/A }
1N/A else /* if(fmt == '[') */
1N/A { do
1N/A { if(!acc.ok[inp])
1N/A { if(n > 0 || (flags&SFFMT_ALTER) )
1N/A break;
1N/A else
1N/A { SFungetc(f,inp);
1N/A goto pop_fmt;
1N/A }
1N/A }
1N/A if((n += 1) <= size)
1N/A *argv.s++ = inp;
1N/A } while(--width > 0 && SFgetc(f,inp) >= 0);
1N/A }
1N/A
1N/A if(value && (n > 0 || fmt == '[') )
1N/A { n_assign += 1;
1N/A if(fmt != 'c' && size >= 0)
1N/A {
1N/A#if _has_multibyte
1N/A if(flags&SFFMT_LONG)
1N/A *argv.ws = 0;
1N/A else
1N/A#endif
1N/A *argv.s = 0;
1N/A }
1N/A }
1N/A }
1N/A
1N/A if(width > 0 && inp >= 0)
1N/A SFungetc(f,inp);
1N/A }
1N/A
1N/Apop_fmt:
1N/A if(fp)
1N/A { free(fp);
1N/A fp = NIL(Fmtpos_t*);
1N/A }
1N/A while((fm = fmstk) ) /* pop the format stack and continue */
1N/A { if(fm->eventf)
1N/A { if(!form || !form[0])
1N/A (*fm->eventf)(f,SF_FINAL,NIL(Void_t*),ft);
1N/A else if((*fm->eventf)(f,SF_DPOP,(Void_t*)form,ft) < 0)
1N/A goto loop_fmt;
1N/A }
1N/A
1N/A fmstk = fm->next;
1N/A if((form = fm->form) )
1N/A { SFMBCPY(&fmbs,&fm->mbs);
1N/A va_copy(args, fm->args);
1N/A oform = fm->oform;
1N/A va_copy(oargs,fm->oargs);
1N/A argn = fm->argn;
1N/A fp = fm->fp;
1N/A }
1N/A ft = fm->ft;
1N/A free(fm);
1N/A if(form && form[0])
1N/A goto loop_fmt;
1N/A }
1N/A
1N/Adone:
1N/A if(fp)
1N/A free(fp);
1N/A while((fm = fmstk) )
1N/A { if(fm->eventf)
1N/A (*fm->eventf)(f,SF_FINAL,NIL(Void_t*),fm->ft);
1N/A fmstk = fm->next;
1N/A free(fm);
1N/A }
1N/A
1N/A SFend(f);
1N/A
1N/A SFOPEN(f,0);
1N/A
1N/A if(n_assign == 0 && inp < 0)
1N/A n_assign = -1;
1N/A
1N/A SFMTXRETURN(f,n_assign);
1N/A}