_scanf.c 54 KB

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