scanf.c 52 KB

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