scanf.c 54 KB

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