_scanf.c 54 KB

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