_scanf.c 54 KB

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