_scanf.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229
  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. b = (psfs.store ? ((unsigned char *) psfs.cur_ptr) : buf);
  1132. fail = 1;
  1133. if (psfs.conv_num == CONV_c) {
  1134. if (sc.width == INT_MAX) {
  1135. sc.width = 1;
  1136. }
  1137. while (__scan_getc(&sc) >= 0) {
  1138. zero_conversions = 0;
  1139. *b = sc.cc;
  1140. b += psfs.store;
  1141. }
  1142. __scan_ungetc(&sc);
  1143. if (sc.width > 0) { /* Failed to read all required. */
  1144. goto DONE;
  1145. }
  1146. psfs.cnt += psfs.store;
  1147. goto NEXT_FMT;
  1148. }
  1149. if (psfs.conv_num == CONV_s) {
  1150. /* We might have to handle the allocation ourselves */
  1151. int len;
  1152. /* With 'm', we actually got a pointer to a pointer */
  1153. unsigned char **ptr = (void *)b;
  1154. i = 0;
  1155. if (psfs.flags & FLAG_MALLOC) {
  1156. len = 0;
  1157. b = NULL;
  1158. } else
  1159. len = -1;
  1160. /* Yes, believe it or not, a %s conversion can store nuls. */
  1161. while ((__scan_getc(&sc) >= 0) && !isspace(sc.cc)) {
  1162. zero_conversions = 0;
  1163. if (i == len) {
  1164. /* Pick a size that won't trigger a lot of
  1165. * mallocs early on ... */
  1166. len += 256;
  1167. b = realloc(b, len + 1);
  1168. }
  1169. b[i] = sc.cc;
  1170. i += psfs.store;
  1171. fail = 0;
  1172. }
  1173. if (psfs.flags & FLAG_MALLOC)
  1174. *ptr = b;
  1175. /* The code below takes care of terminating NUL */
  1176. b += i;
  1177. } else {
  1178. #ifdef __UCLIBC_HAS_WCHAR__
  1179. assert((psfs.conv_num == CONV_LEFTBRACKET) || \
  1180. (psfs.conv_num == CONV_leftbracket));
  1181. #else /* __UCLIBC_HAS_WCHAR__ */
  1182. assert((psfs.conv_num == CONV_leftbracket));
  1183. #endif /* __UCLIBC_HAS_WCHAR__ */
  1184. invert = 0;
  1185. if (*++fmt == '^') {
  1186. ++fmt;
  1187. invert = 1;
  1188. }
  1189. memset(scanset, invert, sizeof(scanset));
  1190. invert = 1-invert;
  1191. if (*fmt == ']') {
  1192. scanset[(int)(']')] = invert;
  1193. ++fmt;
  1194. }
  1195. while (*fmt != ']') {
  1196. if (!*fmt) { /* No closing ']'. */
  1197. goto DONE;
  1198. }
  1199. if ((*fmt == '-') && (fmt[1] != ']')
  1200. && (fmt[-1] < fmt[1]) /* sorted? */
  1201. ) { /* range */
  1202. ++fmt;
  1203. i = fmt[-2];
  1204. /* Note: scanset[i] should already have been done
  1205. * in the previous iteration. */
  1206. do {
  1207. scanset[++i] = invert;
  1208. } while (i < *fmt);
  1209. /* Safe to fall through, and a bit smaller. */
  1210. }
  1211. /* literal char */
  1212. scanset[(int) *fmt] = invert;
  1213. ++fmt;
  1214. }
  1215. #ifdef __UCLIBC_HAS_WCHAR__
  1216. if (psfs.conv_num == CONV_LEFTBRACKET) {
  1217. goto DO_LEFTBRACKET;
  1218. }
  1219. #endif /* __UCLIBC_HAS_WCHAR__ */
  1220. while (__scan_getc(&sc) >= 0) {
  1221. zero_conversions = 0;
  1222. if (!scanset[sc.cc]) {
  1223. break;
  1224. }
  1225. *b = sc.cc;
  1226. b += psfs.store;
  1227. fail = 0;
  1228. }
  1229. }
  1230. /* Common tail for processing of %s and %[. */
  1231. __scan_ungetc(&sc);
  1232. if (fail) { /* nothing stored! */
  1233. goto DONE;
  1234. }
  1235. *b = 0; /* Nul-terminate string. */
  1236. psfs.cnt += psfs.store;
  1237. goto NEXT_FMT;
  1238. }
  1239. #ifdef __UCLIBC_HAS_WCHAR__
  1240. DO_LEFTBRACKET: /* Need to do common wide init. */
  1241. if (psfs.conv_num >= CONV_C) {
  1242. wchar_t wbuf[1];
  1243. wchar_t *wb;
  1244. sc.mbstate.__mask = 0;
  1245. wb = (psfs.store ? ((wchar_t *) psfs.cur_ptr) : wbuf);
  1246. fail = 1;
  1247. if (psfs.conv_num == CONV_C) {
  1248. if (sc.width == INT_MAX) {
  1249. sc.width = 1;
  1250. }
  1251. while (scan_getwc(&sc) >= 0) {
  1252. zero_conversions = 0;
  1253. assert(sc.width >= 0);
  1254. *wb = sc.wc;
  1255. wb += psfs.store;
  1256. }
  1257. __scan_ungetc(&sc);
  1258. if (sc.width > 0) { /* Failed to read all required. */
  1259. goto DONE;
  1260. }
  1261. psfs.cnt += psfs.store;
  1262. goto NEXT_FMT;
  1263. }
  1264. if (psfs.conv_num == CONV_S) {
  1265. /* Yes, believe it or not, a %s conversion can store nuls. */
  1266. while (scan_getwc(&sc) >= 0) {
  1267. zero_conversions = 0;
  1268. if ((((__uwchar_t)(sc.wc)) <= UCHAR_MAX) && isspace(sc.wc)) {
  1269. break;
  1270. }
  1271. *wb = sc.wc;
  1272. wb += psfs.store;
  1273. fail = 0;
  1274. }
  1275. } else {
  1276. assert(psfs.conv_num == CONV_LEFTBRACKET);
  1277. while (scan_getwc(&sc) >= 0) {
  1278. zero_conversions = 0;
  1279. if (((__uwchar_t) sc.wc) <= UCHAR_MAX) {
  1280. if (!scanset[sc.wc]) {
  1281. break;
  1282. }
  1283. } else if (invert) {
  1284. break;
  1285. }
  1286. *wb = sc.wc;
  1287. wb += psfs.store;
  1288. fail = 0;
  1289. }
  1290. }
  1291. /* Common tail for processing of %ls and %l[. */
  1292. __scan_ungetc(&sc);
  1293. if (fail || sc.mb_fail) { /* Nothing stored or mb error. */
  1294. goto DONE;
  1295. }
  1296. *wb = 0; /* Nul-terminate string. */
  1297. psfs.cnt += psfs.store;
  1298. goto NEXT_FMT;
  1299. }
  1300. #endif /* __UCLIBC_HAS_WCHAR__ */
  1301. #else /* L_vfscanf */
  1302. if (psfs.conv_num >= CONV_C) {
  1303. b = buf;
  1304. wb = wbuf;
  1305. if (psfs.conv_num >= CONV_c) {
  1306. mbstate.__mask = 0; /* Initialize the mbstate. */
  1307. if (psfs.store) {
  1308. b = (unsigned char *) psfs.cur_ptr;
  1309. }
  1310. } else {
  1311. if (psfs.store) {
  1312. wb = (wchar_t *) psfs.cur_ptr;
  1313. }
  1314. }
  1315. fail = 1;
  1316. if ((psfs.conv_num == CONV_C) || (psfs.conv_num == CONV_c)) {
  1317. if (sc.width == INT_MAX) {
  1318. sc.width = 1;
  1319. }
  1320. while (scan_getwc(&sc) >= 0) {
  1321. zero_conversions = 0;
  1322. if (psfs.conv_num == CONV_C) {
  1323. *wb = sc.wc;
  1324. wb += psfs.store;
  1325. } else {
  1326. i = wcrtomb((char*) b, sc.wc, &mbstate);
  1327. if (i < 0) { /* Conversion failure. */
  1328. goto DONE_DO_UNGET;
  1329. }
  1330. if (psfs.store) {
  1331. b += i;
  1332. }
  1333. }
  1334. }
  1335. __scan_ungetc(&sc);
  1336. if (sc.width > 0) { /* Failed to read all required. */
  1337. goto DONE;
  1338. }
  1339. psfs.cnt += psfs.store;
  1340. goto NEXT_FMT;
  1341. }
  1342. if ((psfs.conv_num == CONV_S) || (psfs.conv_num == CONV_s)) {
  1343. /* Yes, believe it or not, a %s conversion can store nuls. */
  1344. while (scan_getwc(&sc) >= 0) {
  1345. zero_conversions = 0;
  1346. if (iswspace(sc.wc)) {
  1347. break;
  1348. }
  1349. if (psfs.conv_num == CONV_S) {
  1350. *wb = sc.wc;
  1351. wb += psfs.store;
  1352. } else {
  1353. i = wcrtomb((char*) b, sc.wc, &mbstate);
  1354. if (i < 0) { /* Conversion failure. */
  1355. goto DONE_DO_UNGET;
  1356. }
  1357. if (psfs.store) {
  1358. b += i;
  1359. }
  1360. }
  1361. fail = 0;
  1362. }
  1363. } else {
  1364. const wchar_t *sss;
  1365. const wchar_t *ssp;
  1366. unsigned char invert = 0;
  1367. assert((psfs.conv_num == CONV_LEFTBRACKET)
  1368. || (psfs.conv_num == CONV_leftbracket));
  1369. if (*++fmt == '^') {
  1370. ++fmt;
  1371. invert = 1;
  1372. }
  1373. sss = (const wchar_t *) fmt;
  1374. if (*fmt == ']') {
  1375. ++fmt;
  1376. }
  1377. while (*fmt != ']') {
  1378. if (!*fmt) { /* No closing ']'. */
  1379. goto DONE;
  1380. }
  1381. if ((*fmt == '-') && (fmt[1] != ']')
  1382. && (fmt[-1] < fmt[1]) /* sorted? */
  1383. ) { /* range */
  1384. ++fmt;
  1385. }
  1386. ++fmt;
  1387. }
  1388. /* Ok... a valid scanset spec. */
  1389. while (scan_getwc(&sc) >= 0) {
  1390. zero_conversions = 0;
  1391. ssp = sss;
  1392. do { /* We know sss < fmt. */
  1393. if (*ssp == '-') { /* possible range... */
  1394. /* Note: We accept a-c-e (ordered) as
  1395. * equivalent to a-e. */
  1396. if (ssp > sss) {
  1397. if ((++ssp < (const wchar_t *) fmt)
  1398. && (ssp[-2] < *ssp) /* sorted? */
  1399. ) { /* yes */
  1400. if ((sc.wc >= ssp[-2])
  1401. && (sc.wc <= *ssp)) {
  1402. break;
  1403. }
  1404. continue; /* not in range */
  1405. }
  1406. --ssp; /* oops... '-' at end, so back up */
  1407. }
  1408. /* false alarm... a literal '-' */
  1409. }
  1410. if (sc.wc == *ssp) { /* Matched literal char. */
  1411. break;
  1412. }
  1413. } while (++ssp < (const wchar_t *) fmt);
  1414. if ((ssp == (const wchar_t *) fmt) ^ invert) {
  1415. /* no match and not inverting
  1416. * or match and inverting */
  1417. break;
  1418. }
  1419. if (psfs.conv_num == CONV_LEFTBRACKET) {
  1420. *wb = sc.wc;
  1421. wb += psfs.store;
  1422. } else {
  1423. i = wcrtomb((char*) b, sc.wc, &mbstate);
  1424. if (i < 0) { /* Conversion failure. */
  1425. goto DONE_DO_UNGET;
  1426. }
  1427. if (psfs.store) {
  1428. b += i;
  1429. }
  1430. }
  1431. fail = 0;
  1432. }
  1433. }
  1434. /* Common tail for processing of %s and %[. */
  1435. __scan_ungetc(&sc);
  1436. if (fail) { /* nothing stored! */
  1437. goto DONE;
  1438. }
  1439. *wb = 0; /* Nul-terminate string. */
  1440. *b = 0;
  1441. psfs.cnt += psfs.store;
  1442. goto NEXT_FMT;
  1443. }
  1444. #endif /* L_vfscanf */
  1445. assert(0);
  1446. goto DONE;
  1447. } /* conversion specification */
  1448. MATCH_CHAR:
  1449. if (__scan_getc(&sc) != *fmt) {
  1450. #ifdef L_vfwscanf
  1451. DONE_DO_UNGET:
  1452. #endif /* L_vfwscanf */
  1453. __scan_ungetc(&sc);
  1454. goto DONE;
  1455. }
  1456. NEXT_FMT:
  1457. ++fmt;
  1458. if (__FERROR_UNLOCKED(fp)) {
  1459. break;
  1460. }
  1461. }
  1462. DONE:
  1463. if (__FERROR_UNLOCKED(fp) || (*fmt && zero_conversions && __FEOF_UNLOCKED(fp))) {
  1464. psfs.cnt = EOF; /* Yes, vfwscanf also returns EOF. */
  1465. }
  1466. kill_scan_cookie(&sc);
  1467. __STDIO_STREAM_VALIDATE(fp);
  1468. __STDIO_AUTO_THREADUNLOCK(fp);
  1469. return psfs.cnt;
  1470. }
  1471. libc_hidden_def(VFSCANF)
  1472. #endif
  1473. /**********************************************************************/
  1474. #ifdef L___psfs_do_numeric
  1475. static const unsigned char spec_base[] = SPEC_BASE;
  1476. static const unsigned char nil_string[] = "(nil)";
  1477. int attribute_hidden __psfs_do_numeric(psfs_t *psfs, struct scan_cookie *sc)
  1478. {
  1479. unsigned char *b;
  1480. const unsigned char *p;
  1481. #ifdef __UCLIBC_HAS_FLOATS__
  1482. int exp_adjust = 0;
  1483. #endif
  1484. #ifdef __UCLIBC_MJN3_ONLY__
  1485. #warning TODO: Fix MAX_DIGITS. We do not do binary, so...!
  1486. #warning TODO: Fix buf!
  1487. #endif
  1488. #define MAX_DIGITS 65 /* Allow one leading 0. */
  1489. unsigned char buf[MAX_DIGITS+2+ 100];
  1490. unsigned char usflag, base;
  1491. unsigned char nonzero = 0;
  1492. unsigned char seendigit = 0;
  1493. #ifdef __UCLIBC_MJN3_ONLY__
  1494. #warning CONSIDER: What should be returned for an invalid conversion specifier?
  1495. #endif
  1496. #ifndef __UCLIBC_HAS_FLOATS__
  1497. if (psfs->conv_num > CONV_i) { /* floating point */
  1498. goto DONE;
  1499. }
  1500. #endif
  1501. base = spec_base[psfs->conv_num - CONV_p];
  1502. usflag = (psfs->conv_num <= CONV_u); /* (1)0 if (un)signed */
  1503. b = buf;
  1504. if (psfs->conv_num == CONV_p) { /* Pointer */
  1505. p = nil_string;
  1506. do {
  1507. if ((__scan_getc(sc) < 0) || (*p != sc->cc)) {
  1508. __scan_ungetc(sc);
  1509. if (p > nil_string) {
  1510. /* We matched at least the '(' so even if we
  1511. * are at eof, we can not match a pointer. */
  1512. return -2; /* Matching failure */
  1513. }
  1514. break;
  1515. }
  1516. if (!*++p) { /* Matched (nil), so no unget necessary. */
  1517. if (psfs->store) {
  1518. ++psfs->cnt;
  1519. _store_inttype(psfs->cur_ptr, psfs->dataargtype,
  1520. (uintmax_t)0);
  1521. }
  1522. return 0;
  1523. }
  1524. } while (1);
  1525. #ifdef __UCLIBC_MJN3_ONLY__
  1526. #warning CONSIDER: Should we require a 0x prefix and disallow +/- for pointer %p?
  1527. #endif /* __UCLIBC_MJN3_ONLY__ */
  1528. }
  1529. __scan_getc(sc);
  1530. if (sc->cc < 0) {
  1531. return -1; /* Input failure (nothing read yet). */
  1532. }
  1533. if ((sc->cc == '+') || (sc->cc == '-')) { /* Handle leading sign.*/
  1534. *b++ = sc->cc;
  1535. __scan_getc(sc);
  1536. }
  1537. if ((base & 0xef) == 0) { /* 0xef is ~16, so 16 or 0. */
  1538. if (sc->cc == '0') { /* Possibly set base and handle prefix. */
  1539. __scan_getc(sc);
  1540. if ((sc->cc|0x20) == 'x') { /* Assumes ascii.. x or X. */
  1541. if (__scan_getc(sc) < 0) {
  1542. /* Either EOF or error (including wc outside char range).
  1543. * If EOF or error, this is a matching failure (we read 0x).
  1544. * If wc outside char range, this is also a matching failure.
  1545. * Hence, we do an unget (although not really necessary here
  1546. * and fail. */
  1547. goto DONE_DO_UNGET; /* matching failure */
  1548. }
  1549. base = 16; /* Base 16 for sure now. */
  1550. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  1551. /* The prefix is required for hexadecimal floats. */
  1552. *b++ = '0';
  1553. *b++ = 'x';
  1554. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  1555. } else { /* oops... back up */
  1556. __scan_ungetc(sc);
  1557. sc->cc = '0'; /* NASTY HACK! */
  1558. base = (base >> 1) + 8; /* 0->8, 16->16. no 'if' */
  1559. #ifdef __UCLIBC_HAS_FLOATS__
  1560. if (psfs->conv_num > CONV_i) { /* floating point */
  1561. base = 10;
  1562. }
  1563. #endif
  1564. }
  1565. } else if (!base) {
  1566. base = 10;
  1567. }
  1568. }
  1569. /***************** digit grouping **********************/
  1570. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  1571. if ((psfs->flags & FLAG_THOUSANDS) && (base == 10)
  1572. && *(p = (const unsigned char *) sc->grouping)
  1573. ) {
  1574. int nblk1, nblk2, nbmax, lastblock, pass, i;
  1575. #ifdef __UCLIBC_MJN3_ONLY__
  1576. #warning CONSIDER: Should we initalize the grouping blocks in __init_scan_cookie()?
  1577. #endif /* __UCLIBC_MJN3_ONLY__ */
  1578. nbmax = nblk2 = nblk1 = *p;
  1579. if (*++p) {
  1580. nblk2 = *p;
  1581. if (nbmax < nblk2) {
  1582. nbmax = nblk2;
  1583. }
  1584. assert(!p[1]);
  1585. }
  1586. /* Note: for printf, if 0 and \' flags appear then
  1587. * grouping is done before 0-padding. Should we
  1588. * strip leading 0's first? Or add a 0 flag? */
  1589. /* For vfwscanf, sc_getc translates, so the value of sc->cc is
  1590. * either EOF or a char. */
  1591. if (!__isdigit_char_or_EOF(sc->cc)) { /* No starting digit! */
  1592. #ifdef __UCLIBC_HAS_FLOATS__
  1593. if (psfs->conv_num > CONV_i) { /* floating point */
  1594. goto NO_STARTING_DIGIT;
  1595. }
  1596. #endif
  1597. goto DONE_DO_UNGET;
  1598. }
  1599. if (sc->cc == '0') {
  1600. seendigit = 1;
  1601. *b++ = '0'; /* Store the first 0. */
  1602. #ifdef __UCLIBC_MJN3_ONLY__
  1603. #warning CONSIDER: Should leading 0s be skipped before digit grouping? (printf 0 pad)
  1604. #endif /* __UCLIBC_MJN3_ONLY__ */
  1605. #if 0
  1606. do { /* But ignore all subsequent 0s. */
  1607. __scan_getc(sc);
  1608. } while (sc->cc == '0');
  1609. #endif
  1610. }
  1611. pass = 0;
  1612. lastblock = 0;
  1613. do {
  1614. i = 0;
  1615. while (__isdigit_char_or_EOF(sc->cc)) {
  1616. seendigit = 1;
  1617. if (i == nbmax) { /* too many digits for a block */
  1618. #ifdef __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__
  1619. if (!pass) { /* treat as nongrouped */
  1620. if (nonzero) {
  1621. goto DO_NO_GROUP;
  1622. }
  1623. goto DO_TRIM_LEADING_ZEROS;
  1624. }
  1625. #endif
  1626. if (nbmax > nblk1) {
  1627. goto DONE_DO_UNGET; /* matching failure */
  1628. }
  1629. goto DONE_GROUPING_DO_UNGET; /* nbmax == nblk1 */
  1630. }
  1631. ++i;
  1632. if (nonzero || (sc->cc != '0')) {
  1633. if (b < buf + MAX_DIGITS) {
  1634. *b++ = sc->cc;
  1635. nonzero = 1;
  1636. #ifdef __UCLIBC_HAS_FLOATS__
  1637. } else {
  1638. ++exp_adjust;
  1639. #endif
  1640. }
  1641. }
  1642. __scan_getc(sc);
  1643. }
  1644. if (i) { /* we saw digits digits */
  1645. if ((i == nblk2) || ((i < nblk2) && !pass)) {
  1646. /* (possible) outer grp */
  1647. p = sc->thousands_sep;
  1648. if (*p == sc->cc) { /* first byte matches... */
  1649. /* so check if grouping mb char */
  1650. /* Since 1st matched, either match or fail now
  1651. * unless EOF (yuk) */
  1652. __scan_getc(sc);
  1653. MBG_LOOP:
  1654. if (!*++p) { /* is a grouping mb char */
  1655. lastblock = i;
  1656. ++pass;
  1657. continue;
  1658. }
  1659. if (*p == sc->cc) {
  1660. __scan_getc(sc);
  1661. goto MBG_LOOP;
  1662. }
  1663. /* bad grouping mb char! */
  1664. __scan_ungetc(sc);
  1665. if ((sc->cc >= 0) || (p > sc->thousands_sep + 1)) {
  1666. #ifdef __UCLIBC_HAS_FLOATS__
  1667. /* We failed to match a thousep mb char, and
  1668. * we've read too much to recover. But if
  1669. * this is a floating point conversion and
  1670. * the initial portion of the decpt mb char
  1671. * matches, then we may still be able to
  1672. * recover. */
  1673. int k = p - sc->thousands_sep - 1;
  1674. if ((psfs->conv_num > CONV_i) /* float conversion */
  1675. && (!pass || (i == nblk1)) /* possible last */
  1676. && !memcmp(sc->thousands_sep, sc->fake_decpt, k)
  1677. /* and prefix matched, so could be decpt */
  1678. ) {
  1679. __scan_getc(sc);
  1680. p = sc->fake_decpt + k;
  1681. do {
  1682. if (!*++p) {
  1683. strcpy((char*) b, (char*) sc->decpt);
  1684. b += sc->decpt_len;
  1685. goto GOT_DECPT;
  1686. }
  1687. if (*p != sc->cc) {
  1688. __scan_ungetc(sc);
  1689. break; /* failed */
  1690. }
  1691. __scan_getc(sc);
  1692. } while (1);
  1693. }
  1694. #endif /* __UCLIBC_HAS_FLOATS__ */
  1695. goto DONE;
  1696. }
  1697. /* was EOF and 1st, so recoverable. */
  1698. }
  1699. }
  1700. if ((i == nblk1) || ((i < nblk1) && !pass)) {
  1701. /* got an inner group */
  1702. goto DONE_GROUPING_DO_UNGET;
  1703. }
  1704. goto DONE_DO_UNGET; /* Matching failure. */
  1705. } /* i != 0 */
  1706. assert(pass);
  1707. goto DONE_DO_UNGET;
  1708. } while (1);
  1709. assert(0); /* Should never get here. */
  1710. }
  1711. #endif /***************** digit grouping **********************/
  1712. /* Not grouping so first trim all but one leading 0. */
  1713. #ifdef __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__
  1714. DO_TRIM_LEADING_ZEROS:
  1715. #endif /* __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__ */
  1716. if (sc->cc == '0') {
  1717. seendigit = 1;
  1718. *b++ = '0'; /* Store the first 0. */
  1719. do { /* But ignore all subsequent 0s. */
  1720. __scan_getc(sc);
  1721. } while (sc->cc == '0');
  1722. }
  1723. #ifdef __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__
  1724. DO_NO_GROUP:
  1725. #endif /* __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__ */
  1726. /* At this point, we're ready to start reading digits. */
  1727. #define valid_digit(cc,base) (isxdigit(cc) && ((base == 16) || (cc - '0' < base)))
  1728. while (valid_digit(sc->cc,base)) { /* Now for significant digits.*/
  1729. if (b - buf < MAX_DIGITS) {
  1730. nonzero = seendigit = 1; /* Set nonzero too 0s trimmed above. */
  1731. *b++ = sc->cc;
  1732. #ifdef __UCLIBC_HAS_FLOATS__
  1733. } else {
  1734. ++exp_adjust;
  1735. #endif
  1736. }
  1737. __scan_getc(sc);
  1738. }
  1739. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  1740. DONE_GROUPING_DO_UNGET:
  1741. #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
  1742. if (psfs->conv_num <= CONV_i) { /* integer conversion */
  1743. __scan_ungetc(sc);
  1744. *b = 0; /* null-terminate */
  1745. if (!seendigit) {
  1746. goto DONE; /* No digits! */
  1747. }
  1748. if (psfs->store) {
  1749. if (*buf == '-') {
  1750. usflag = 0;
  1751. }
  1752. ++psfs->cnt;
  1753. _store_inttype(psfs->cur_ptr, psfs->dataargtype,
  1754. (uintmax_t) STRTOUIM((char *) buf, NULL, base, 1-usflag));
  1755. }
  1756. return 0;
  1757. }
  1758. #ifdef __UCLIBC_HAS_FLOATS__
  1759. /* At this point, we have everything left of the decimal point or exponent. */
  1760. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  1761. NO_STARTING_DIGIT:
  1762. #endif
  1763. p = sc->fake_decpt;
  1764. do {
  1765. if (!*p) {
  1766. strcpy((char *) b, (char *) sc->decpt);
  1767. b += sc->decpt_len;
  1768. break;
  1769. }
  1770. if (*p != sc->cc) {
  1771. if (p > sc->fake_decpt) {
  1772. goto DONE_DO_UNGET; /* matching failure (read some of decpt) */
  1773. }
  1774. goto DO_DIGIT_CHECK;
  1775. }
  1776. ++p;
  1777. __scan_getc(sc);
  1778. } while (1);
  1779. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  1780. GOT_DECPT:
  1781. #endif
  1782. if (!nonzero) {
  1783. if (sc->cc == '0') {
  1784. assert(exp_adjust == 0);
  1785. *b++ = '0';
  1786. ++exp_adjust;
  1787. seendigit = 1;
  1788. do {
  1789. --exp_adjust;
  1790. __scan_getc(sc);
  1791. } while (sc->cc == '0');
  1792. }
  1793. }
  1794. while (valid_digit(sc->cc,base)) { /* Process fractional digits.*/
  1795. if (b - buf < MAX_DIGITS) {
  1796. seendigit = 1;
  1797. *b++ = sc->cc;
  1798. }
  1799. __scan_getc(sc);
  1800. }
  1801. DO_DIGIT_CHECK:
  1802. /* Hmm... no decimal point. */
  1803. if (!seendigit) {
  1804. static const unsigned char nan_inf_str[] = "an\0nfinity";
  1805. if (base == 16) { /* We had a prefix, but no digits! */
  1806. goto DONE_DO_UNGET; /* matching failure */
  1807. }
  1808. /* Avoid tolower problems for INFINITY in the tr_TR locale. (yuk)*/
  1809. #undef TOLOWER
  1810. #define TOLOWER(C) ((C)|0x20)
  1811. switch (TOLOWER(sc->cc)) {
  1812. case 'i':
  1813. p = nan_inf_str + 3;
  1814. break;
  1815. case 'n':
  1816. p = nan_inf_str;
  1817. break;
  1818. default:
  1819. /* No digits and not inf or nan. */
  1820. goto DONE_DO_UNGET;
  1821. }
  1822. *b++ = sc->cc;
  1823. do {
  1824. __scan_getc(sc);
  1825. if (TOLOWER(sc->cc) == *p) {
  1826. *b++ = sc->cc;
  1827. ++p;
  1828. continue;
  1829. }
  1830. if (!*p || (p == nan_inf_str + 5)) { /* match nan/infinity or inf */
  1831. goto GOT_FLOAT;
  1832. }
  1833. /* Unrecoverable. Even if on 1st char, we had no digits. */
  1834. goto DONE_DO_UNGET;
  1835. } while (1);
  1836. }
  1837. /* If we get here, we had some digits. */
  1838. if (
  1839. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  1840. ((base == 16) && (((sc->cc)|0x20) == 'p')) ||
  1841. #endif
  1842. (((sc->cc)|0x20) == 'e')
  1843. ) { /* Process an exponent. */
  1844. *b++ = sc->cc;
  1845. __scan_getc(sc);
  1846. if (sc->cc < 0) {
  1847. goto DONE_DO_UNGET; /* matching failure.. no exponent digits */
  1848. }
  1849. if ((sc->cc == '+') || (sc->cc == '-')) { /* Signed exponent? */
  1850. *b++ = sc->cc;
  1851. __scan_getc(sc);
  1852. }
  1853. #ifdef __UCLIBC_MJN3_ONLY__
  1854. #warning TODO: Fix MAX_EXP_DIGITS!
  1855. #endif
  1856. #define MAX_EXP_DIGITS 20
  1857. assert(seendigit);
  1858. seendigit = 0;
  1859. nonzero = 0;
  1860. if (sc->cc == '0') {
  1861. seendigit = 1;
  1862. *b++ = '0';
  1863. do {
  1864. __scan_getc(sc);
  1865. } while (sc->cc == '0');
  1866. }
  1867. while (__isdigit_char_or_EOF(sc->cc)) { /* Exponent digits (base 10).*/
  1868. if (seendigit < MAX_EXP_DIGITS) {
  1869. ++seendigit;
  1870. *b++ = sc->cc;
  1871. }
  1872. __scan_getc(sc);
  1873. }
  1874. if (!seendigit) { /* No digits. Unrecoverable. */
  1875. goto DONE_DO_UNGET;
  1876. }
  1877. }
  1878. GOT_FLOAT:
  1879. *b = 0;
  1880. {
  1881. __fpmax_t x;
  1882. char *e;
  1883. x = __strtofpmax((char *) buf, &e, exp_adjust);
  1884. assert(!*e);
  1885. if (psfs->store) {
  1886. if (psfs->dataargtype & PA_FLAG_LONG_LONG) {
  1887. *((long double *)psfs->cur_ptr) = (long double) x;
  1888. } else if (psfs->dataargtype & PA_FLAG_LONG) {
  1889. *((double *)psfs->cur_ptr) = (double) x;
  1890. } else {
  1891. *((float *)psfs->cur_ptr) = (float) x;
  1892. }
  1893. ++psfs->cnt;
  1894. }
  1895. __scan_ungetc(sc);
  1896. return 0;
  1897. }
  1898. #endif /* __UCLIBC_HAS_FLOATS__ */
  1899. DONE_DO_UNGET:
  1900. __scan_ungetc(sc);
  1901. DONE:
  1902. return -2; /* Matching failure. */
  1903. }
  1904. #endif
  1905. /**********************************************************************/