scanf.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229
  1. /* Copyright (C) 2002-2004 Manuel Novoa III
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Library General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2 of the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Library General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Library General Public
  14. * License along with this library; if not, 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. #define _GNU_SOURCE
  46. #include <features.h>
  47. #include "_stdio.h"
  48. #include <stdlib.h>
  49. #include <unistd.h>
  50. #include <ctype.h>
  51. #include <string.h>
  52. #include <stdarg.h>
  53. #include <stdint.h>
  54. #include <errno.h>
  55. #include <printf.h>
  56. #ifdef __UCLIBC_HAS_WCHAR__
  57. #include <bits/uClibc_uwchar.h>
  58. #include <wchar.h>
  59. #include <wctype.h>
  60. #endif /* __UCLIBC_HAS_WCHAR__ */
  61. #include <langinfo.h>
  62. #include <locale.h>
  63. #include <assert.h>
  64. #include <limits.h>
  65. #ifdef __UCLIBC_HAS_THREADS__
  66. #include <stdio_ext.h>
  67. #include <pthread.h>
  68. #endif /* __UCLIBC_HAS_THREADS__ */
  69. #ifdef __UCLIBC_HAS_FLOATS__
  70. #include <float.h>
  71. #include <bits/uClibc_fpmax.h>
  72. #endif /* __UCLIBC_HAS_FLOATS__ */
  73. #ifdef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
  74. #ifdef L_vfscanf
  75. /* only emit this once */
  76. #warning Forcing undef of __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__ until implemented!
  77. #endif
  78. #undef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
  79. #endif
  80. #undef __STDIO_HAS_VSSCANF
  81. #if defined(__STDIO_BUFFERS) || !defined(__UCLIBC_HAS_WCHAR__) || defined(__UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__)
  82. #define __STDIO_HAS_VSSCANF 1
  83. #if !defined(__STDIO_BUFFERS) && !defined(__UCLIBC_HAS_WCHAR__)
  84. typedef struct {
  85. FILE f;
  86. unsigned char *bufread; /* pointer to 1 past end of buffer */
  87. unsigned char *bufpos;
  88. } __FILE_vsscanf;
  89. #endif
  90. #endif
  91. extern void _store_inttype(void *dest, int desttype, uintmax_t val);
  92. #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
  93. extern unsigned long long
  94. _stdlib_strto_ll(register const char * __restrict str,
  95. char ** __restrict endptr, int base, int sflag);
  96. #if (ULLONG_MAX == UINTMAX_MAX)
  97. #define STRTOUIM(s,e,b,sf) _stdlib_strto_ll(s,e,b,sf)
  98. #endif
  99. #else /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
  100. extern unsigned long
  101. _stdlib_strto_l(register const char * __restrict str,
  102. char ** __restrict endptr, int base, int sflag);
  103. #if (ULONG_MAX == UINTMAX_MAX)
  104. #define STRTOUIM(s,e,b,sf) _stdlib_strto_l(s,e,b,sf)
  105. #endif
  106. #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
  107. #ifndef STRTOUIM
  108. #error STRTOUIM conversion function is undefined!
  109. #endif
  110. /**********************************************************************/
  111. /* The standards require EOF < 0. */
  112. #if EOF >= CHAR_MIN
  113. #define __isdigit_char_or_EOF(C) __isdigit_char((C))
  114. #else
  115. #define __isdigit_char_or_EOF(C) __isdigit_int((C))
  116. #endif
  117. /**********************************************************************/
  118. #ifdef L_fscanf
  119. int fscanf(FILE * __restrict stream, const char * __restrict format, ...)
  120. {
  121. va_list arg;
  122. int rv;
  123. va_start(arg, format);
  124. rv = vfscanf(stream, format, arg);
  125. va_end(arg);
  126. return rv;
  127. }
  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. #else /* __STDIO_HAS_VSSCANF */
  154. #warning Skipping sscanf since no vsscanf!
  155. #endif /* __STDIO_HAS_VSSCANF */
  156. #endif
  157. /**********************************************************************/
  158. #ifdef L_vscanf
  159. int vscanf(const char * __restrict format, va_list arg)
  160. {
  161. return vfscanf(stdin, format, arg);
  162. }
  163. #endif
  164. /**********************************************************************/
  165. #ifdef L_vsscanf
  166. #ifdef __UCLIBC_MJN3_ONLY__
  167. #warning WISHLIST: Implement vsscanf for non-buf and no custom stream case.
  168. #endif /* __UCLIBC_MJN3_ONLY__ */
  169. #ifdef __STDIO_BUFFERS
  170. int vsscanf(__const char *sp, __const char *fmt, va_list ap)
  171. {
  172. FILE f;
  173. /* __STDIO_STREAM_RESET_GCS(&f); */
  174. #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
  175. f.__cookie = &(f.__filedes);
  176. f.__gcs.read = NULL;
  177. f.__gcs.write = NULL;
  178. f.__gcs.seek = NULL;
  179. f.__gcs.close = NULL;
  180. #endif
  181. f.__filedes = __STDIO_STREAM_FAKE_VSSCANF_FILEDES;
  182. f.__modeflags = (__FLAG_NARROW|__FLAG_READONLY|__FLAG_READING);
  183. #ifdef __UCLIBC_HAS_WCHAR__
  184. f.__ungot_width[0] = 0;
  185. #endif
  186. #ifdef __STDIO_MBSTATE
  187. __INIT_MBSTATE(&(f.__state));
  188. #endif
  189. #ifdef __UCLIBC_HAS_THREADS__
  190. f.__user_locking = 1; /* Set user locking. */
  191. __stdio_init_mutex(&f.__lock);
  192. #endif
  193. f.__nextopen = NULL;
  194. /* Set these last since __bufgetc initialization depends on
  195. * __user_locking and only gets set if user locking is on. */
  196. f.__bufstart =
  197. f.__bufpos = (unsigned char *) ((void *) sp);
  198. f.__bufread =
  199. f.__bufend = f.__bufstart + strlen(sp);
  200. __STDIO_STREAM_ENABLE_GETC(&f);
  201. __STDIO_STREAM_DISABLE_PUTC(&f);
  202. return vfscanf(&f, fmt, ap);
  203. }
  204. #elif !defined(__UCLIBC_HAS_WCHAR__)
  205. int vsscanf(__const char *sp, __const char *fmt, va_list ap)
  206. {
  207. __FILE_vsscanf f;
  208. f.bufpos = (unsigned char *) ((void *) sp);
  209. f.bufread = f.bufpos + strlen(sp);
  210. /* __STDIO_STREAM_RESET_GCS(&f.f); */
  211. #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
  212. f.f.__cookie = &(f.f.__filedes);
  213. f.f.__gcs.read = NULL;
  214. f.f.__gcs.write = NULL;
  215. f.f.__gcs.seek = NULL;
  216. f.f.__gcs.close = NULL;
  217. #endif
  218. f.f.__filedes = __STDIO_STREAM_FAKE_VSSCANF_FILEDES_NB;
  219. f.f.__modeflags = (__FLAG_NARROW|__FLAG_READONLY|__FLAG_READING);
  220. /* #ifdef __UCLIBC_HAS_WCHAR__ */
  221. /* f.f.__ungot_width[0] = 0; */
  222. /* #endif */
  223. #ifdef __STDIO_MBSTATE
  224. #error __STDIO_MBSTATE is defined!
  225. /* __INIT_MBSTATE(&(f.f.__state)); */
  226. #endif
  227. #ifdef __UCLIBC_HAS_THREADS__
  228. f.f.__user_locking = 1; /* Set user locking. */
  229. __stdio_init_mutex(&f.f.__lock);
  230. #endif
  231. f.f.__nextopen = NULL;
  232. return vfscanf(&f.f, fmt, ap);
  233. }
  234. #elif defined(__UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__)
  235. int vsscanf(__const char *sp, __const char *fmt, va_list ap)
  236. {
  237. FILE *f;
  238. int rv = EOF;
  239. if ((f = fmemopen((char *)sp, strlen(sp), "r")) != NULL) {
  240. rv = vfscanf(f, fmt, ap);
  241. fclose(f);
  242. }
  243. return rv;
  244. }
  245. #else
  246. #warning Skipping vsscanf since no buffering, no custom streams, and wchar enabled!
  247. #ifdef __STDIO_HAS_VSSCANF
  248. #error WHOA! __STDIO_HAS_VSSCANF is defined!
  249. #endif
  250. #endif
  251. #endif
  252. /**********************************************************************/
  253. #ifdef L_fwscanf
  254. int fwscanf(FILE * __restrict stream, const wchar_t * __restrict format, ...)
  255. {
  256. va_list arg;
  257. int rv;
  258. va_start(arg, format);
  259. rv = vfwscanf(stream, format, arg);
  260. va_end(arg);
  261. return rv;
  262. }
  263. #endif
  264. /**********************************************************************/
  265. #ifdef L_wscanf
  266. int wscanf(const wchar_t * __restrict format, ...)
  267. {
  268. va_list arg;
  269. int rv;
  270. va_start(arg, format);
  271. rv = vfwscanf(stdin, format, arg);
  272. va_end(arg);
  273. return rv;
  274. }
  275. #endif
  276. /**********************************************************************/
  277. #ifdef L_swscanf
  278. #ifdef __STDIO_BUFFERS
  279. int swscanf(const wchar_t * __restrict str, const wchar_t * __restrict format,
  280. ...)
  281. {
  282. va_list arg;
  283. int rv;
  284. va_start(arg, format);
  285. rv = vswscanf(str, format, arg);
  286. va_end(arg);
  287. return rv;
  288. }
  289. #else /* __STDIO_BUFFERS */
  290. #warning Skipping swscanf since no buffering!
  291. #endif /* __STDIO_BUFFERS */
  292. #endif
  293. /**********************************************************************/
  294. #ifdef L_vwscanf
  295. int vwscanf(const wchar_t * __restrict format, va_list arg)
  296. {
  297. return vfwscanf(stdin, format, arg);
  298. }
  299. #endif
  300. /**********************************************************************/
  301. #ifdef L_vswscanf
  302. #ifdef __STDIO_BUFFERS
  303. int vswscanf(const wchar_t * __restrict str, const wchar_t * __restrict format,
  304. va_list arg)
  305. {
  306. FILE f;
  307. f.__bufstart =
  308. f.__bufpos = (char *) str;
  309. f.__bufread =
  310. f.__bufend = (char *)(str + wcslen(str));
  311. __STDIO_STREAM_DISABLE_GETC(&f);
  312. __STDIO_STREAM_DISABLE_PUTC(&f);
  313. /* __STDIO_STREAM_RESET_GCS(&f); */
  314. #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
  315. f.__cookie = &(f.__filedes);
  316. f.__gcs.read = NULL;
  317. f.__gcs.write = NULL;
  318. f.__gcs.seek = NULL;
  319. f.__gcs.close = NULL;
  320. #endif
  321. f.__filedes = __STDIO_STREAM_FAKE_VSWSCANF_FILEDES;
  322. f.__modeflags = (__FLAG_WIDE|__FLAG_READONLY|__FLAG_READING);
  323. #ifdef __UCLIBC_HAS_WCHAR__
  324. f.__ungot_width[0] = 0;
  325. #endif /* __UCLIBC_HAS_WCHAR__ */
  326. #ifdef __STDIO_MBSTATE
  327. __INIT_MBSTATE(&(f.__state));
  328. #endif /* __STDIO_MBSTATE */
  329. #ifdef __UCLIBC_HAS_THREADS__
  330. f.__user_locking = 1; /* Set user locking. */
  331. __stdio_init_mutex(&f.__lock);
  332. #endif
  333. f.__nextopen = NULL;
  334. return vfwscanf(&f, format, arg);
  335. }
  336. #else /* __STDIO_BUFFERS */
  337. #warning Skipping vswscanf since no buffering!
  338. #endif /* __STDIO_BUFFERS */
  339. #endif
  340. /**********************************************************************/
  341. /**********************************************************************/
  342. /* float layout 0123456789012345678901 repeat n for "l[" */
  343. #define SPEC_CHARS "npxXoudifFeEgGaACSncs["
  344. /* npxXoudif eEgG CS cs[ */
  345. /* NOTE: Ordering is important! In particular, CONV_LEFTBRACKET
  346. * must immediately precede CONV_c. */
  347. enum {
  348. CONV_n = 0,
  349. CONV_p,
  350. CONV_x, CONV_X, CONV_o, CONV_u, CONV_d, CONV_i,
  351. CONV_f, CONV_F, CONV_e, CONV_E, CONV_g, CONV_G, CONV_a, CONV_A,
  352. CONV_C, CONV_S, CONV_LEFTBRACKET, CONV_c, CONV_s, CONV_leftbracket,
  353. CONV_percent, CONV_whitespace /* not in SPEC_* and no flags */
  354. };
  355. #ifdef __UCLIBC_HAS_FLOATS__
  356. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  357. /* p x X o u d i f F e E g G a A */
  358. #define SPEC_BASE { 16, 16, 16, 8, 10, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
  359. #else
  360. /* p x X o u d i f F e E g G a A */
  361. #define SPEC_BASE { 16, 16, 16, 8, 10, 10, 0, 10, 10, 10, 10, 10, 10, 10, 10 }
  362. #endif
  363. #else /* __UCLIBC_HAS_FLOATS__ */
  364. /* p x X o u d i f F e E g G a A */
  365. #define SPEC_BASE { 16, 16, 16, 8, 10, 10, 0 }
  366. #endif /* __UCLIBC_HAS_FLOATS__ */
  367. #ifdef __UCLIBC_MJN3_ONLY__
  368. #ifdef L_vfscanf
  369. /* emit once */
  370. #warning CONSIDER: Add a '0' flag to eat 0 padding when grouping?
  371. #endif
  372. #endif /* __UCLIBC_MJN3_ONLY__ */
  373. #define SPEC_FLAGS "*'I"
  374. enum {
  375. FLAG_SURPRESS = 0x10, /* MUST BE 1ST!! See DO_FLAGS. */
  376. FLAG_THOUSANDS = 0x20,
  377. FLAG_I18N = 0x40, /* only works for d, i, u */
  378. FLAG_MALLOC = 0x80, /* only works for s, S, and [ (and l[)*/
  379. };
  380. #define SPEC_RANGES { CONV_n, CONV_p, CONV_i, CONV_A, \
  381. CONV_C, CONV_LEFTBRACKET, \
  382. CONV_c, CONV_leftbracket }
  383. /* Note: We treat L and ll as synonymous... for ints and floats. */
  384. #define SPEC_ALLOWED_FLAGS { \
  385. /* n */ (0x0f|FLAG_SURPRESS), \
  386. /* p */ ( 0|FLAG_SURPRESS), \
  387. /* oxXudi */ (0x0f|FLAG_SURPRESS|FLAG_THOUSANDS|FLAG_I18N), \
  388. /* fFeEgGaA */ (0x0c|FLAG_SURPRESS|FLAG_THOUSANDS|FLAG_I18N), \
  389. /* C */ ( 0|FLAG_SURPRESS), \
  390. /* S and l[ */ ( 0|FLAG_SURPRESS|FLAG_MALLOC), \
  391. /* c */ (0x04|FLAG_SURPRESS), \
  392. /* s and [ */ (0x04|FLAG_SURPRESS|FLAG_MALLOC), \
  393. }
  394. /**********************************************************************/
  395. /*
  396. * In order to ease translation to what arginfo and _print_info._flags expect,
  397. * we map: 0:int 1:char 2:longlong 4:long 8:short
  398. * and then _flags |= (((q << 7) + q) & 0x701) and argtype |= (_flags & 0x701)
  399. */
  400. /* TODO -- Fix the table below to take into account stdint.h. */
  401. /* #ifndef LLONG_MAX */
  402. /* #error fix QUAL_CHARS for no long long! Affects 'L', 'j', 'q', 'll'. */
  403. /* #else */
  404. /* #if LLONG_MAX != INTMAX_MAX */
  405. /* #error fix QUAL_CHARS intmax_t entry 'j'! */
  406. /* #endif */
  407. /* #endif */
  408. #ifdef PDS
  409. #error PDS already defined!
  410. #endif
  411. #ifdef SS
  412. #error SS already defined!
  413. #endif
  414. #ifdef IMS
  415. #error IMS already defined!
  416. #endif
  417. #if PTRDIFF_MAX == INT_MAX
  418. #define PDS 0
  419. #elif PTRDIFF_MAX == LONG_MAX
  420. #define PDS 4
  421. #elif defined(LLONG_MAX) && (PTRDIFF_MAX == LLONG_MAX)
  422. #define PDS 8
  423. #else
  424. #error fix QUAL_CHARS ptrdiff_t entry 't'!
  425. #endif
  426. #if SIZE_MAX == UINT_MAX
  427. #define SS 0
  428. #elif SIZE_MAX == ULONG_MAX
  429. #define SS 4
  430. #elif defined(LLONG_MAX) && (SIZE_MAX == ULLONG_MAX)
  431. #define SS 8
  432. #else
  433. #error fix QUAL_CHARS size_t entries 'z', 'Z'!
  434. #endif
  435. #if INTMAX_MAX == INT_MAX
  436. #define IMS 0
  437. #elif INTMAX_MAX == LONG_MAX
  438. #define IMS 4
  439. #elif defined(LLONG_MAX) && (INTMAX_MAX == LLONG_MAX)
  440. #define IMS 8
  441. #else
  442. #error fix QUAL_CHARS ptrdiff_t entry 't'!
  443. #endif
  444. #define QUAL_CHARS { \
  445. /* j:(u)intmax_t z:(s)size_t t:ptrdiff_t \0:int q:long_long */ \
  446. 'h', 'l', 'L', 'j', 'z', 't', 'q', 0, \
  447. 2, 4, 8, IMS, SS, PDS, 8, 0, /* TODO -- fix!!! */\
  448. 1, 8 }
  449. /**********************************************************************/
  450. #ifdef L_vfwscanf
  451. #if WINT_MIN > EOF
  452. #error Unfortunately, we currently need wint_t to be able to store EOF. Sorry.
  453. #endif
  454. #define W_EOF WEOF
  455. #define Wint wint_t
  456. #define Wchar wchar_t
  457. #define Wuchar __uwchar_t
  458. #define ISSPACE(C) iswspace((C))
  459. #define VFSCANF vfwscanf
  460. #define GETC(SC) (SC)->sc_getc((SC))
  461. #else
  462. typedef unsigned char __uchar_t;
  463. #define W_EOF EOF
  464. #define Wint int
  465. #define Wchar char
  466. #define Wuchar __uchar_t
  467. #define ISSPACE(C) isspace((C))
  468. #define VFSCANF vfscanf
  469. #ifdef __UCLIBC_HAS_WCHAR__
  470. #define GETC(SC) (SC)->sc_getc((SC))
  471. #else /* __UCLIBC_HAS_WCHAR__ */
  472. #define GETC(SC) getc_unlocked((SC)->fp)
  473. #endif /* __UCLIBC_HAS_WCHAR__ */
  474. #endif
  475. struct scan_cookie {
  476. Wint cc;
  477. Wint ungot_char;
  478. FILE *fp;
  479. int nread;
  480. int width;
  481. #ifdef __UCLIBC_HAS_WCHAR__
  482. wchar_t app_ungot; /* Match FILE struct member type. */
  483. unsigned char ungot_wchar_width;
  484. #else /* __UCLIBC_HAS_WCHAR__ */
  485. unsigned char app_ungot; /* Match FILE struct member type. */
  486. #endif /* __UCLIBC_HAS_WCHAR__ */
  487. char ungot_flag;
  488. #ifdef __UCLIBC_HAS_WCHAR__
  489. char ungot_wflag; /* vfwscanf */
  490. char mb_fail; /* vfscanf */
  491. mbstate_t mbstate; /* vfscanf */
  492. wint_t wc;
  493. wint_t ungot_wchar; /* to support __scan_getc */
  494. int (*sc_getc)(struct scan_cookie *);
  495. #endif /* __UCLIBC_HAS_WCHAR__ */
  496. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  497. const char *grouping;
  498. const unsigned char *thousands_sep;
  499. int tslen;
  500. #ifdef __UCLIBC_HAS_WCHAR__
  501. wchar_t thousands_sep_wc;
  502. #endif /* __UCLIBC_HAS_WCHAR__ */
  503. #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
  504. #ifdef __UCLIBC_HAS_FLOATS__
  505. const unsigned char *decpt;
  506. int decpt_len;
  507. #ifdef __UCLIBC_HAS_WCHAR__
  508. wchar_t decpt_wc;
  509. #endif /* __UCLIBC_HAS_WCHAR__ */
  510. const unsigned char *fake_decpt;
  511. #endif /* __UCLIBC_HAS_FLOATS__ */
  512. };
  513. typedef struct {
  514. #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
  515. #if NL_ARGMAX > 10
  516. #warning NL_ARGMAX > 10, and space is allocated on the stack for positional args.
  517. #endif
  518. void *pos_args[NL_ARGMAX];
  519. int num_pos_args; /* Must start at -1. */
  520. int cur_pos_arg;
  521. #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
  522. void *cur_ptr;
  523. const unsigned char *fmt;
  524. int cnt, dataargtype, conv_num, max_width;
  525. unsigned char store, flags;
  526. } psfs_t; /* parse scanf format state */
  527. /**********************************************************************/
  528. /**********************************************************************/
  529. extern void __init_scan_cookie(register struct scan_cookie *sc,
  530. register FILE *fp) attribute_hidden;
  531. extern int __scan_getc(register struct scan_cookie *sc) attribute_hidden;
  532. extern void __scan_ungetc(register struct scan_cookie *sc) attribute_hidden;
  533. #ifdef __UCLIBC_HAS_FLOATS__
  534. extern int __scan_strtold(long double *ld, struct scan_cookie *sc);
  535. #endif /* __UCLIBC_HAS_FLOATS__ */
  536. extern int __psfs_parse_spec(psfs_t *psfs) attribute_hidden;
  537. extern int __psfs_do_numeric(psfs_t *psfs, struct scan_cookie *sc) attribute_hidden;
  538. /**********************************************************************/
  539. #ifdef L___scan_cookie
  540. #ifdef __UCLIBC_MJN3_ONLY__
  541. #warning TODO: Remove dependence on decpt_str and fake_decpt in stub locale mode.
  542. #endif
  543. #ifndef __UCLIBC_HAS_LOCALE__
  544. static const char decpt_str[] = ".";
  545. #endif
  546. void attribute_hidden __init_scan_cookie(register struct scan_cookie *sc,
  547. register FILE *fp)
  548. {
  549. sc->fp = fp;
  550. sc->nread = 0;
  551. sc->ungot_flag = 0;
  552. sc->app_ungot = ((fp->__modeflags & __FLAG_UNGOT) ? fp->__ungot[1] : 0);
  553. #ifdef __UCLIBC_HAS_WCHAR__
  554. sc->ungot_wflag = 0; /* vfwscanf */
  555. sc->mb_fail = 0;
  556. #endif /* __UCLIBC_HAS_WCHAR__ */
  557. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  558. if (*(sc->grouping = __UCLIBC_CURLOCALE_DATA.grouping)) {
  559. sc->thousands_sep = __UCLIBC_CURLOCALE_DATA.thousands_sep;
  560. sc->tslen = __UCLIBC_CURLOCALE_DATA.thousands_sep_len;
  561. #ifdef __UCLIBC_HAS_WCHAR__
  562. sc->thousands_sep_wc = __UCLIBC_CURLOCALE_DATA.thousands_sep_wc;
  563. #endif /* __UCLIBC_HAS_WCHAR__ */
  564. }
  565. #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
  566. #ifdef __UCLIBC_HAS_FLOATS__
  567. #ifdef __UCLIBC_HAS_LOCALE__
  568. sc->decpt = __UCLIBC_CURLOCALE_DATA.decimal_point;
  569. sc->decpt_len = __UCLIBC_CURLOCALE_DATA.decimal_point_len;
  570. #else /* __UCLIBC_HAS_LOCALE__ */
  571. sc->fake_decpt = sc->decpt = decpt_str;
  572. sc->decpt_len = 1;
  573. #endif /* __UCLIBC_HAS_LOCALE__ */
  574. #ifdef __UCLIBC_HAS_WCHAR__
  575. #ifdef __UCLIBC_HAS_LOCALE__
  576. sc->decpt_wc = __UCLIBC_CURLOCALE_DATA.decimal_point_wc;
  577. #else
  578. sc->decpt_wc = '.';
  579. #endif
  580. #endif /* __UCLIBC_HAS_WCHAR__ */
  581. #endif /* __UCLIBC_HAS_FLOATS__ */
  582. }
  583. int attribute_hidden __scan_getc(register struct scan_cookie *sc)
  584. {
  585. int c;
  586. #ifdef __UCLIBC_HAS_WCHAR__
  587. assert(!sc->mb_fail);
  588. #endif /* __UCLIBC_HAS_WCHAR__ */
  589. sc->cc = EOF;
  590. if (--sc->width < 0) {
  591. sc->ungot_flag |= 2;
  592. return -1;
  593. }
  594. if (sc->ungot_flag == 0) {
  595. #if !defined(__STDIO_BUFFERS) && !defined(__UCLIBC_HAS_WCHAR__)
  596. if (!__STDIO_STREAM_IS_FAKE_VSSCANF_NB(sc->fp)) {
  597. c = GETC(sc);
  598. } else {
  599. __FILE_vsscanf *fv = (__FILE_vsscanf *)(sc->fp);
  600. if (fv->bufpos < fv->bufread) {
  601. c = *fv->bufpos++;
  602. } else {
  603. c = EOF;
  604. sc->fp->__modeflags |= __FLAG_EOF;
  605. }
  606. }
  607. if (c == EOF) {
  608. sc->ungot_flag |= 2;
  609. return -1;
  610. }
  611. #else
  612. if ((c = GETC(sc)) == EOF) {
  613. sc->ungot_flag |= 2;
  614. return -1;
  615. }
  616. #endif
  617. sc->ungot_char = c;
  618. } else {
  619. assert(sc->ungot_flag == 1);
  620. sc->ungot_flag = 0;
  621. }
  622. ++sc->nread;
  623. return sc->cc = sc->ungot_char;
  624. }
  625. void attribute_hidden __scan_ungetc(register struct scan_cookie *sc)
  626. {
  627. ++sc->width;
  628. if (sc->ungot_flag == 2) { /* last was EOF */
  629. sc->ungot_flag = 0;
  630. sc->cc = sc->ungot_char;
  631. } else if (sc->ungot_flag == 0) {
  632. sc->ungot_flag = 1;
  633. --sc->nread;
  634. } else {
  635. assert(0);
  636. }
  637. }
  638. #endif
  639. /**********************************************************************/
  640. #ifdef L___psfs_parse_spec
  641. #ifdef SPEC_FLAGS
  642. static const unsigned char spec_flags[] = SPEC_FLAGS;
  643. #endif /* SPEC_FLAGS */
  644. static const unsigned char spec_chars[] = SPEC_CHARS;
  645. static const unsigned char qual_chars[] = QUAL_CHARS;
  646. static const unsigned char spec_ranges[] = SPEC_RANGES;
  647. static const unsigned short spec_allowed[] = SPEC_ALLOWED_FLAGS;
  648. int attribute_hidden __psfs_parse_spec(register psfs_t *psfs)
  649. {
  650. const unsigned char *p;
  651. const unsigned char *fmt0 = psfs->fmt;
  652. int i;
  653. #ifdef SPEC_FLAGS
  654. int j;
  655. #endif
  656. #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
  657. unsigned char fail = 0;
  658. i = 0; /* Do this here to avoid a warning. */
  659. if (!__isdigit_char(*psfs->fmt)) { /* Not a positional arg. */
  660. fail = 1;
  661. goto DO_FLAGS;
  662. }
  663. /* parse the positional arg (or width) value */
  664. do {
  665. if (i <= ((INT_MAX - 9)/10)) {
  666. i = (i * 10) + (*psfs->fmt++ - '0');
  667. }
  668. } while (__isdigit_char(*psfs->fmt));
  669. if (*psfs->fmt != '$') { /* This is a max field width. */
  670. if (psfs->num_pos_args >= 0) { /* Already saw a pos arg! */
  671. goto ERROR_EINVAL;
  672. }
  673. psfs->max_width = i;
  674. psfs->num_pos_args = -2;
  675. goto DO_QUALIFIER;
  676. }
  677. ++psfs->fmt; /* Advance past '$'. */
  678. #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
  679. #if defined(SPEC_FLAGS) || (defined(NL_ARGMAX) && (NL_ARGMAX > 0))
  680. DO_FLAGS:
  681. #endif /* defined(SPEC_FLAGS) || (defined(NL_ARGMAX) && (NL_ARGMAX > 0)) */
  682. #ifdef SPEC_FLAGS
  683. p = spec_flags;
  684. j = FLAG_SURPRESS;
  685. do {
  686. if (*p == *psfs->fmt) {
  687. ++psfs->fmt;
  688. psfs->flags |= j;
  689. goto DO_FLAGS;
  690. }
  691. j += j;
  692. } while (*++p);
  693. if (psfs->flags & FLAG_SURPRESS) { /* Suppress assignment. */
  694. psfs->store = 0;
  695. goto DO_WIDTH;
  696. }
  697. #else /* SPEC_FLAGS */
  698. if (*psfs->fmt == '*') { /* Suppress assignment. */
  699. ++psfs->fmt;
  700. psfs->store = 0;
  701. goto DO_WIDTH;
  702. }
  703. #endif /* SPEC_FLAGS */
  704. #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
  705. if (fail) {
  706. /* Must be a non-positional arg */
  707. if (psfs->num_pos_args >= 0) { /* Already saw a pos arg! */
  708. goto ERROR_EINVAL;
  709. }
  710. psfs->num_pos_args = -2;
  711. } else {
  712. if ((psfs->num_pos_args == -2) || (((unsigned int)(--i)) >= NL_ARGMAX)) {
  713. /* Already saw a non-pos arg or (0-based) num too large. */
  714. goto ERROR_EINVAL;
  715. }
  716. psfs->cur_pos_arg = i;
  717. }
  718. #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
  719. DO_WIDTH:
  720. for (i = 0 ; __isdigit_char(*psfs->fmt) ; ) {
  721. if (i <= ((INT_MAX - 9)/10)) {
  722. i = (i * 10) + (*psfs->fmt++ - '0');
  723. psfs->max_width = i;
  724. }
  725. }
  726. #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
  727. DO_QUALIFIER:
  728. #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
  729. p = qual_chars;
  730. do {
  731. if (*psfs->fmt == *p) {
  732. ++psfs->fmt;
  733. break;
  734. }
  735. } while (*++p);
  736. if ((p - qual_chars < 2) && (*psfs->fmt == *p)) {
  737. p += ((sizeof(qual_chars)-2) / 2);
  738. ++psfs->fmt;
  739. }
  740. psfs->dataargtype = ((int)(p[(sizeof(qual_chars)-2) / 2])) << 8;
  741. #ifdef __UCLIBC_MJN3_ONLY__
  742. #warning CONSIDER: Should we validate that psfs->max_width > 0 in __psfs_parse_spec()? It would avoid whitespace consumption...
  743. #warning CONSIDER: Should INT_MAX be a valid width (%c/%C)? See __psfs_parse_spec().
  744. #endif /* __UCLIBC_MJN3_ONLY__ */
  745. p = spec_chars;
  746. do {
  747. if (*psfs->fmt == *p) {
  748. int p_m_spec_chars = p - spec_chars;
  749. #ifdef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
  750. #error implement gnu a flag
  751. if ((*p == 'a')
  752. && ((psfs->fmt[1] == '[') || ((psfs->fmt[1]|0x20) == 's'))
  753. ) { /* Assumes ascii for 's' and 'S' test. */
  754. psfs->flags |= FLAG_MALLOC;
  755. ++psfs->fmt;
  756. ++p;
  757. continue; /* The related conversions follow 'a'. */
  758. }
  759. #endif /* __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__ */
  760. for (p = spec_ranges; p_m_spec_chars > *p ; ++p) {}
  761. if (((psfs->dataargtype >> 8) | psfs->flags)
  762. & ~spec_allowed[(int)(p - spec_ranges)]
  763. ) {
  764. goto ERROR_EINVAL;
  765. }
  766. if ((p_m_spec_chars >= CONV_c)
  767. && (psfs->dataargtype & PA_FLAG_LONG)) {
  768. p_m_spec_chars -= 3; /* lc -> C, ls -> S, l[ -> ?? */
  769. }
  770. psfs->conv_num = p_m_spec_chars;
  771. return psfs->fmt - fmt0;
  772. }
  773. if (!*++p) {
  774. ERROR_EINVAL:
  775. __set_errno(EINVAL);
  776. return -1;
  777. }
  778. } while(1);
  779. assert(0);
  780. }
  781. #endif
  782. /**********************************************************************/
  783. #if defined(L_vfscanf) || defined(L_vfwscanf)
  784. #ifdef __UCLIBC_HAS_WCHAR__
  785. #ifdef L_vfscanf
  786. static int sc_getc(register struct scan_cookie *sc)
  787. {
  788. return (getc_unlocked)(sc->fp); /* Disable the macro. */
  789. }
  790. static int scan_getwc(register struct scan_cookie *sc)
  791. {
  792. size_t r;
  793. int width;
  794. wchar_t wc[1];
  795. char b[1];
  796. if (--sc->width < 0) {
  797. sc->ungot_flag |= 2;
  798. return -1;
  799. }
  800. width = sc->width; /* Preserve width. */
  801. sc->width = INT_MAX; /* MB_CUR_MAX can invoke a function. */
  802. assert(!sc->mb_fail);
  803. r = (size_t)(-3);
  804. while (__scan_getc(sc) >= 0) {
  805. *b = sc->cc;
  806. r = mbrtowc(wc, b, 1, &sc->mbstate);
  807. if (((ssize_t) r) >= 0) { /* Successful completion of a wc. */
  808. sc->wc = *wc;
  809. goto SUCCESS;
  810. } else if (r == ((size_t) -2)) {
  811. /* Potentially valid but incomplete. */
  812. continue;
  813. }
  814. break;
  815. }
  816. if (r == ((size_t)(-3))) { /* EOF or ERROR on first read */
  817. sc->wc = WEOF;
  818. r = (size_t)(-1);
  819. } else {
  820. /* If we reach here, either r == ((size_t)-1) and
  821. * mbrtowc set errno to EILSEQ, or r == ((size_t)-2)
  822. * and stream is in an error state or at EOF with a
  823. * partially complete wchar. */
  824. __set_errno(EILSEQ); /* In case of incomplete conversion. */
  825. sc->mb_fail = 1;
  826. }
  827. SUCCESS:
  828. sc->width = width; /* Restore width. */
  829. return (int)((ssize_t) r);
  830. }
  831. #endif /* L_vfscanf */
  832. #ifdef L_vfwscanf
  833. /* This gets called by __scan_getc. __scan_getc is called by vfwscanf
  834. * when the next wide char is expected to be valid ascii (digits).
  835. */
  836. static int sc_getc(register struct scan_cookie *sc)
  837. {
  838. wint_t wc;
  839. if (__STDIO_STREAM_IS_FAKE_VSWSCANF(sc->fp)) {
  840. if (sc->fp->__bufpos < sc->fp->__bufend) {
  841. wc = *((wchar_t *)(sc->fp->__bufpos));
  842. sc->fp->__bufpos += sizeof(wchar_t);
  843. } else {
  844. sc->fp->__modeflags |= __FLAG_EOF;
  845. return EOF;
  846. }
  847. } else if ((wc = __fgetwc_unlocked(sc->fp)) == WEOF) {
  848. return EOF;
  849. }
  850. sc->ungot_wflag = 1;
  851. sc->ungot_wchar = wc;
  852. sc->ungot_wchar_width = sc->fp->__ungot_width[0];
  853. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  854. if (wc == sc->thousands_sep_wc) {
  855. wc = ',';
  856. } else
  857. #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
  858. #ifdef __UCLIBC_HAS_FLOATS__
  859. if (wc == sc->decpt_wc) {
  860. wc = '.';
  861. } else
  862. #endif /* __UCLIBC_HAS_FLOATS__ */
  863. if (!__isascii(wc)) {
  864. wc = '?';
  865. }
  866. sc->wc = sc->ungot_char = wc;
  867. return (int) wc;
  868. }
  869. static int scan_getwc(register struct scan_cookie *sc)
  870. {
  871. wint_t wc;
  872. sc->wc = WEOF;
  873. if (--sc->width < 0) {
  874. sc->ungot_flag |= 2;
  875. return -1;
  876. }
  877. if (sc->ungot_flag == 0) {
  878. if (__STDIO_STREAM_IS_FAKE_VSWSCANF(sc->fp)) {
  879. if (sc->fp->__bufpos < sc->fp->__bufend) {
  880. wc = *((wchar_t *)(sc->fp->__bufpos));
  881. sc->fp->__bufpos += sizeof(wchar_t);
  882. } else {
  883. sc->ungot_flag |= 2;
  884. return -1;
  885. }
  886. } else if ((wc = __fgetwc_unlocked(sc->fp)) == WEOF) {
  887. sc->ungot_flag |= 2;
  888. return -1;
  889. }
  890. sc->ungot_wflag = 1;
  891. sc->ungot_char = wc;
  892. sc->ungot_wchar_width = sc->fp->__ungot_width[0];
  893. } else {
  894. assert(sc->ungot_flag == 1);
  895. sc->ungot_flag = 0;
  896. }
  897. ++sc->nread;
  898. sc->wc = sc->ungot_char;
  899. return 0;
  900. }
  901. #endif /* L_vfwscanf */
  902. #endif /* __UCLIBC_HAS_WCHAR__ */
  903. static __inline void kill_scan_cookie(register struct scan_cookie *sc)
  904. {
  905. #ifdef L_vfscanf
  906. if (sc->ungot_flag & 1) {
  907. #if !defined(__STDIO_BUFFERS) && !defined(__UCLIBC_HAS_WCHAR__)
  908. if (!__STDIO_STREAM_IS_FAKE_VSSCANF_NB(sc->fp)) {
  909. ungetc(sc->ungot_char, sc->fp);
  910. }
  911. #else
  912. ungetc(sc->ungot_char, sc->fp);
  913. #endif
  914. /* Deal with distiction between user and scanf ungots. */
  915. if (sc->nread == 0) { /* Only one char was read... app ungot? */
  916. sc->fp->__ungot[1] = sc->app_ungot; /* restore ungot state. */
  917. } else {
  918. sc->fp->__ungot[1] = 0;
  919. }
  920. }
  921. #else
  922. if ((sc->ungot_flag & 1) && (sc->ungot_wflag & 1)
  923. && !__STDIO_STREAM_IS_FAKE_VSWSCANF(sc->fp)
  924. && (sc->fp->__state.__mask == 0)
  925. ) {
  926. ungetwc(sc->ungot_char, sc->fp);
  927. /* Deal with distiction between user and scanf ungots. */
  928. if (sc->nread == 0) { /* Only one char was read... app ungot? */
  929. sc->fp->__ungot[1] = sc->app_ungot; /* restore ungot state. */
  930. } else {
  931. sc->fp->__ungot[1] = 0;
  932. }
  933. sc->fp->__ungot_width[1] = sc->ungot_wchar_width;
  934. }
  935. #endif
  936. }
  937. #ifdef L_vfwscanf
  938. #ifdef __UCLIBC_HAS_FLOATS__
  939. static const char fake_decpt_str[] = ".";
  940. #endif
  941. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  942. static const char fake_thousands_sep_str[] = ",";
  943. #endif
  944. #endif /* L_vfwscanf */
  945. int VFSCANF (FILE *__restrict fp, const Wchar *__restrict format, va_list arg)
  946. {
  947. const Wuchar *fmt;
  948. unsigned char *b;
  949. #ifdef L_vfwscanf
  950. wchar_t wbuf[1];
  951. wchar_t *wb;
  952. #endif /* L_vfwscanf */
  953. #ifdef __UCLIBC_HAS_WCHAR__
  954. mbstate_t mbstate;
  955. #endif /* __UCLIBC_HAS_WCHAR__ */
  956. struct scan_cookie sc;
  957. psfs_t psfs;
  958. int i;
  959. #ifdef __UCLIBC_MJN3_ONLY__
  960. #warning TODO: Fix MAX_DIGITS. We do not do binary, so...!
  961. #endif
  962. #define MAX_DIGITS 65 /* Allow one leading 0. */
  963. unsigned char buf[MAX_DIGITS+2];
  964. #ifdef L_vfscanf
  965. unsigned char scanset[UCHAR_MAX + 1];
  966. unsigned char invert; /* Careful! Meaning changes. */
  967. #endif /* L_vfscanf */
  968. unsigned char fail;
  969. unsigned char zero_conversions = 1;
  970. __STDIO_AUTO_THREADLOCK_VAR;
  971. #ifdef __UCLIBC_MJN3_ONLY__
  972. #warning TODO: Make checking of the format string in C locale an option.
  973. #endif
  974. /* To support old programs, don't check mb validity if in C locale. */
  975. #if defined(__UCLIBC_HAS_LOCALE__) && !defined(L_vfwscanf)
  976. /* ANSI/ISO C99 requires format string to be a valid multibyte string
  977. * beginning and ending in its initial shift state. */
  978. if (((__UCLIBC_CURLOCALE_DATA).encoding) != __ctype_encoding_7_bit) {
  979. const char *p = format;
  980. mbstate.__mask = 0; /* Initialize the mbstate. */
  981. if (mbsrtowcs(NULL, &p, SIZE_MAX, &mbstate) == ((size_t)(-1))) {
  982. __set_errno(EINVAL); /* Format string is invalid. */
  983. return 0;
  984. }
  985. }
  986. #endif /* defined(__UCLIBC_HAS_LOCALE__) && !defined(L_vfwscanf) */
  987. #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
  988. psfs.num_pos_args = -1; /* Must start at -1. */
  989. /* Initialize positional arg ptrs to NULL. */
  990. memset(psfs.pos_args, 0, sizeof(psfs.pos_args));
  991. #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
  992. __STDIO_AUTO_THREADLOCK(fp);
  993. __STDIO_STREAM_VALIDATE(fp);
  994. __init_scan_cookie(&sc,fp);
  995. #ifdef __UCLIBC_HAS_WCHAR__
  996. sc.sc_getc = sc_getc;
  997. sc.ungot_wchar_width = sc.fp->__ungot_width[1];
  998. #ifdef L_vfwscanf
  999. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  1000. if (*sc.grouping) {
  1001. sc.thousands_sep = fake_thousands_sep_str;
  1002. sc.tslen = 1;
  1003. }
  1004. #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
  1005. #ifdef __UCLIBC_HAS_FLOATS__
  1006. sc.fake_decpt = fake_decpt_str;
  1007. #endif /* __UCLIBC_HAS_FLOATS__ */
  1008. #else /* L_vfwscanf */
  1009. #ifdef __UCLIBC_HAS_FLOATS__
  1010. sc.fake_decpt = sc.decpt;
  1011. #endif /* __UCLIBC_HAS_FLOATS__ */
  1012. #endif /* L_vfwscanf */
  1013. #endif /* __UCLIBC_HAS_WCHAR__ */
  1014. psfs.cnt = 0;
  1015. /* Note: If we ever wanted to support non-nice codesets, we
  1016. * would really need to do a mb->wc conversion here in the
  1017. * vfscanf case. Related changes would have to be made in
  1018. * the code that follows... basicly wherever fmt appears. */
  1019. for (fmt = (const Wuchar *) format ; *fmt ; /* ++fmt */) {
  1020. psfs.store = 1;
  1021. psfs.flags = 0;
  1022. #ifndef NDEBUG
  1023. psfs.cur_ptr = NULL; /* Debugging aid. */
  1024. #endif /* NDEBUG */
  1025. sc.ungot_flag &= 1; /* Clear (possible fake) EOF. */
  1026. sc.width = psfs.max_width = INT_MAX;
  1027. /* Note: According to the standards, vfscanf does use isspace
  1028. * here. So, if we did a mb->wc conversion, we would have to do
  1029. * something like
  1030. * ((((__uwchar_t)wc) < UCHAR_MAX) && isspace(wc))
  1031. * because wc might not be in the allowed domain. */
  1032. if (ISSPACE(*fmt)) {
  1033. do {
  1034. ++fmt;
  1035. } while (ISSPACE(*fmt));
  1036. --fmt;
  1037. psfs.conv_num = CONV_whitespace;
  1038. goto DO_WHITESPACE;
  1039. }
  1040. if (*fmt == '%') { /* Conversion specification. */
  1041. if (*++fmt == '%') { /* Remember, '%' eats whitespace too. */
  1042. /* Note: The standard says no conversion occurs.
  1043. * So do not reset zero_conversions flag. */
  1044. psfs.conv_num = CONV_percent;
  1045. goto DO_CONVERSION;
  1046. }
  1047. #ifdef L_vfscanf
  1048. psfs.fmt = fmt;
  1049. #else /* L_vfscanf */
  1050. {
  1051. const __uwchar_t *wf = fmt;
  1052. psfs.fmt = b = buf;
  1053. while (*wf && __isascii(*wf) && (b < buf + sizeof(buf) - 1)) {
  1054. *b++ = *wf++;
  1055. }
  1056. #ifdef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
  1057. #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.
  1058. if ((*b == 'a') && ((*wf == '[') || ((*wf|0x20) == 's'))) {
  1059. goto DONE; /* Spec was excessively long. */
  1060. }
  1061. #endif /* __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__ */
  1062. *b = 0;
  1063. if (b == buf) { /* Bad conversion specifier! */
  1064. goto DONE;
  1065. }
  1066. }
  1067. #endif /* L_vfscanf */
  1068. if ((i = __psfs_parse_spec(&psfs)) < 0) { /* Bad conversion specifier! */
  1069. goto DONE;
  1070. }
  1071. fmt += i;
  1072. if (psfs.store) {
  1073. #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
  1074. if (psfs.num_pos_args == -2) {
  1075. psfs.cur_ptr = va_arg(arg, void *);
  1076. } else {
  1077. while (psfs.cur_pos_arg > psfs.num_pos_args) {
  1078. psfs.pos_args[++psfs.num_pos_args] = va_arg(arg, void *);
  1079. }
  1080. psfs.cur_ptr = psfs.pos_args[psfs.cur_pos_arg];
  1081. }
  1082. #else /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
  1083. psfs.cur_ptr = va_arg(arg, void *);
  1084. #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
  1085. }
  1086. DO_CONVERSION:
  1087. /* First, consume white-space if not n, c, [, C, or l[. */
  1088. if ((((1L << CONV_n)|(1L << CONV_C)|(1L << CONV_c)
  1089. |(1L << CONV_LEFTBRACKET)|(1L << CONV_leftbracket))
  1090. & (1L << psfs.conv_num)) == 0
  1091. ) {
  1092. DO_WHITESPACE:
  1093. while ((__scan_getc(&sc) >= 0)
  1094. #ifdef L_vfscanf
  1095. && isspace(sc.cc)
  1096. #else /* L_vfscanf */
  1097. && iswspace(sc.wc)
  1098. #endif /* L_vfscanf */
  1099. ) {}
  1100. __scan_ungetc(&sc);
  1101. if (psfs.conv_num == CONV_whitespace) {
  1102. goto NEXT_FMT;
  1103. }
  1104. }
  1105. sc.width = psfs.max_width; /* Now limit the max width. */
  1106. if (sc.width == 0) { /* 0 width is forbidden. */
  1107. goto DONE;
  1108. }
  1109. if (psfs.conv_num == CONV_percent) {
  1110. goto MATCH_CHAR;
  1111. }
  1112. if (psfs.conv_num == CONV_n) {
  1113. #ifdef __UCLIBC_MJN3_ONLY__
  1114. #warning CONSIDER: Should %n count as a conversion as far as EOF return value?
  1115. #endif
  1116. /* zero_conversions = 0; */
  1117. if (psfs.store) {
  1118. _store_inttype(psfs.cur_ptr, psfs.dataargtype,
  1119. (uintmax_t) sc.nread);
  1120. }
  1121. goto NEXT_FMT;
  1122. }
  1123. if (psfs.conv_num <= CONV_A) { /* pointer, integer, or float spec */
  1124. int r = __psfs_do_numeric(&psfs, &sc);
  1125. #ifndef L_vfscanf
  1126. if (sc.ungot_wflag == 1) { /* fix up '?', '.', and ',' hacks */
  1127. sc.cc = sc.ungot_char = sc.ungot_wchar;
  1128. }
  1129. #endif
  1130. if (r != -1) { /* Either success or a matching failure. */
  1131. zero_conversions = 0;
  1132. }
  1133. if (r < 0) {
  1134. goto DONE;
  1135. }
  1136. goto NEXT_FMT;
  1137. }
  1138. /* Do string conversions here since they are not common code. */
  1139. #ifdef L_vfscanf
  1140. if
  1141. #ifdef __UCLIBC_HAS_WCHAR__
  1142. (psfs.conv_num >= CONV_LEFTBRACKET)
  1143. #else /* __UCLIBC_HAS_WCHAR__ */
  1144. (psfs.conv_num >= CONV_c)
  1145. #endif /* __UCLIBC_HAS_WCHAR__ */
  1146. {
  1147. b = (psfs.store ? ((unsigned char *) psfs.cur_ptr) : buf);
  1148. fail = 1;
  1149. if (psfs.conv_num == CONV_c) {
  1150. if (sc.width == INT_MAX) {
  1151. sc.width = 1;
  1152. }
  1153. while (__scan_getc(&sc) >= 0) {
  1154. zero_conversions = 0;
  1155. *b = sc.cc;
  1156. b += psfs.store;
  1157. }
  1158. __scan_ungetc(&sc);
  1159. if (sc.width > 0) { /* Failed to read all required. */
  1160. goto DONE;
  1161. }
  1162. psfs.cnt += psfs.store;
  1163. goto NEXT_FMT;
  1164. }
  1165. if (psfs.conv_num == CONV_s) {
  1166. /* Yes, believe it or not, a %s conversion can store nuls. */
  1167. while ((__scan_getc(&sc) >= 0) && !isspace(sc.cc)) {
  1168. zero_conversions = 0;
  1169. *b = sc.cc;
  1170. b += psfs.store;
  1171. fail = 0;
  1172. }
  1173. } else {
  1174. #ifdef __UCLIBC_HAS_WCHAR__
  1175. assert((psfs.conv_num == CONV_LEFTBRACKET) || \
  1176. (psfs.conv_num == CONV_leftbracket));
  1177. #else /* __UCLIBC_HAS_WCHAR__ */
  1178. assert((psfs.conv_num == CONV_leftbracket));
  1179. #endif /* __UCLIBC_HAS_WCHAR__ */
  1180. invert = 0;
  1181. if (*++fmt == '^') {
  1182. ++fmt;
  1183. invert = 1;
  1184. }
  1185. memset(scanset, invert, sizeof(scanset));
  1186. invert = 1-invert;
  1187. if (*fmt == ']') {
  1188. scanset[(int)(']')] = invert;
  1189. ++fmt;
  1190. }
  1191. while (*fmt != ']') {
  1192. if (!*fmt) { /* No closing ']'. */
  1193. goto DONE;
  1194. }
  1195. if ((*fmt == '-') && (fmt[1] != ']')
  1196. && (fmt[-1] < fmt[1]) /* sorted? */
  1197. ) { /* range */
  1198. ++fmt;
  1199. i = fmt[-2];
  1200. /* Note: scanset[i] should already have been done
  1201. * in the previous iteration. */
  1202. do {
  1203. scanset[++i] = invert;
  1204. } while (i < *fmt);
  1205. /* Safe to fall through, and a bit smaller. */
  1206. }
  1207. /* literal char */
  1208. scanset[(int) *fmt] = invert;
  1209. ++fmt;
  1210. }
  1211. #ifdef __UCLIBC_HAS_WCHAR__
  1212. if (psfs.conv_num == CONV_LEFTBRACKET) {
  1213. goto DO_LEFTBRACKET;
  1214. }
  1215. #endif /* __UCLIBC_HAS_WCHAR__ */
  1216. while (__scan_getc(&sc) >= 0) {
  1217. zero_conversions = 0;
  1218. if (!scanset[sc.cc]) {
  1219. break;
  1220. }
  1221. *b = sc.cc;
  1222. b += psfs.store;
  1223. fail = 0;
  1224. }
  1225. }
  1226. /* Common tail for processing of %s and %[. */
  1227. __scan_ungetc(&sc);
  1228. if (fail) { /* nothing stored! */
  1229. goto DONE;
  1230. }
  1231. *b = 0; /* Nul-terminate string. */
  1232. psfs.cnt += psfs.store;
  1233. goto NEXT_FMT;
  1234. }
  1235. #ifdef __UCLIBC_HAS_WCHAR__
  1236. DO_LEFTBRACKET: /* Need to do common wide init. */
  1237. if (psfs.conv_num >= CONV_C) {
  1238. wchar_t wbuf[1];
  1239. wchar_t *wb;
  1240. sc.mbstate.__mask = 0;
  1241. wb = (psfs.store ? ((wchar_t *) psfs.cur_ptr) : wbuf);
  1242. fail = 1;
  1243. if (psfs.conv_num == CONV_C) {
  1244. if (sc.width == INT_MAX) {
  1245. sc.width = 1;
  1246. }
  1247. while (scan_getwc(&sc) >= 0) {
  1248. zero_conversions = 0;
  1249. assert(sc.width >= 0);
  1250. *wb = sc.wc;
  1251. wb += psfs.store;
  1252. }
  1253. __scan_ungetc(&sc);
  1254. if (sc.width > 0) { /* Failed to read all required. */
  1255. goto DONE;
  1256. }
  1257. psfs.cnt += psfs.store;
  1258. goto NEXT_FMT;
  1259. }
  1260. if (psfs.conv_num == CONV_S) {
  1261. /* Yes, believe it or not, a %s conversion can store nuls. */
  1262. while (scan_getwc(&sc) >= 0) {
  1263. zero_conversions = 0;
  1264. if ((((__uwchar_t)(sc.wc)) <= UCHAR_MAX) && isspace(sc.wc)) {
  1265. break;
  1266. }
  1267. *wb = sc.wc;
  1268. wb += psfs.store;
  1269. fail = 0;
  1270. }
  1271. } else {
  1272. assert(psfs.conv_num == CONV_LEFTBRACKET);
  1273. while (scan_getwc(&sc) >= 0) {
  1274. zero_conversions = 0;
  1275. if (((__uwchar_t) sc.wc) <= UCHAR_MAX) {
  1276. if (!scanset[sc.wc]) {
  1277. break;
  1278. }
  1279. } else if (invert) {
  1280. break;
  1281. }
  1282. *wb = sc.wc;
  1283. wb += psfs.store;
  1284. fail = 0;
  1285. }
  1286. }
  1287. /* Common tail for processing of %ls and %l[. */
  1288. __scan_ungetc(&sc);
  1289. if (fail || sc.mb_fail) { /* Nothing stored or mb error. */
  1290. goto DONE;
  1291. }
  1292. *wb = 0; /* Nul-terminate string. */
  1293. psfs.cnt += psfs.store;
  1294. goto NEXT_FMT;
  1295. }
  1296. #endif /* __UCLIBC_HAS_WCHAR__ */
  1297. #else /* L_vfscanf */
  1298. if (psfs.conv_num >= CONV_C) {
  1299. b = buf;
  1300. wb = wbuf;
  1301. if (psfs.conv_num >= CONV_c) {
  1302. mbstate.__mask = 0; /* Initialize the mbstate. */
  1303. if (psfs.store) {
  1304. b = (unsigned char *) psfs.cur_ptr;
  1305. }
  1306. } else {
  1307. if (psfs.store) {
  1308. wb = (wchar_t *) psfs.cur_ptr;
  1309. }
  1310. }
  1311. fail = 1;
  1312. if ((psfs.conv_num == CONV_C) || (psfs.conv_num == CONV_c)) {
  1313. if (sc.width == INT_MAX) {
  1314. sc.width = 1;
  1315. }
  1316. while (scan_getwc(&sc) >= 0) {
  1317. zero_conversions = 0;
  1318. if (psfs.conv_num == CONV_C) {
  1319. *wb = sc.wc;
  1320. wb += psfs.store;
  1321. } else {
  1322. i = wcrtomb(b, sc.wc, &mbstate);
  1323. if (i < 0) { /* Conversion failure. */
  1324. goto DONE_DO_UNGET;
  1325. }
  1326. if (psfs.store) {
  1327. b += i;
  1328. }
  1329. }
  1330. }
  1331. __scan_ungetc(&sc);
  1332. if (sc.width > 0) { /* Failed to read all required. */
  1333. goto DONE;
  1334. }
  1335. psfs.cnt += psfs.store;
  1336. goto NEXT_FMT;
  1337. }
  1338. if ((psfs.conv_num == CONV_S) || (psfs.conv_num == CONV_s)) {
  1339. /* Yes, believe it or not, a %s conversion can store nuls. */
  1340. while (scan_getwc(&sc) >= 0) {
  1341. zero_conversions = 0;
  1342. if (iswspace(sc.wc)) {
  1343. break;
  1344. }
  1345. if (psfs.conv_num == CONV_S) {
  1346. *wb = sc.wc;
  1347. wb += psfs.store;
  1348. } else {
  1349. i = wcrtomb(b, sc.wc, &mbstate);
  1350. if (i < 0) { /* Conversion failure. */
  1351. goto DONE_DO_UNGET;
  1352. }
  1353. if (psfs.store) {
  1354. b += i;
  1355. }
  1356. }
  1357. fail = 0;
  1358. }
  1359. } else {
  1360. const wchar_t *sss;
  1361. const wchar_t *ssp;
  1362. unsigned char invert = 0;
  1363. assert((psfs.conv_num == CONV_LEFTBRACKET)
  1364. || (psfs.conv_num == CONV_leftbracket));
  1365. if (*++fmt == '^') {
  1366. ++fmt;
  1367. invert = 1;
  1368. }
  1369. sss = (const wchar_t *) fmt;
  1370. if (*fmt == ']') {
  1371. ++fmt;
  1372. }
  1373. while (*fmt != ']') {
  1374. if (!*fmt) { /* No closing ']'. */
  1375. goto DONE;
  1376. }
  1377. if ((*fmt == '-') && (fmt[1] != ']')
  1378. && (fmt[-1] < fmt[1]) /* sorted? */
  1379. ) { /* range */
  1380. ++fmt;
  1381. }
  1382. ++fmt;
  1383. }
  1384. /* Ok... a valid scanset spec. */
  1385. while (scan_getwc(&sc) >= 0) {
  1386. zero_conversions = 0;
  1387. ssp = sss;
  1388. do { /* We know sss < fmt. */
  1389. if (*ssp == '-') { /* possible range... */
  1390. /* Note: We accept a-c-e (ordered) as
  1391. * equivalent to a-e. */
  1392. if (ssp > sss) {
  1393. if ((++ssp < (const wchar_t *) fmt)
  1394. && (ssp[-2] < *ssp) /* sorted? */
  1395. ) { /* yes */
  1396. if ((sc.wc >= ssp[-2])
  1397. && (sc.wc <= *ssp)) {
  1398. break;
  1399. }
  1400. continue; /* not in range */
  1401. }
  1402. --ssp; /* oops... '-' at end, so back up */
  1403. }
  1404. /* false alarm... a literal '-' */
  1405. }
  1406. if (sc.wc == *ssp) { /* Matched literal char. */
  1407. break;
  1408. }
  1409. } while (++ssp < (const wchar_t *) fmt);
  1410. if ((ssp == (const wchar_t *) fmt) ^ invert) {
  1411. /* no match and not inverting
  1412. * or match and inverting */
  1413. break;
  1414. }
  1415. if (psfs.conv_num == CONV_LEFTBRACKET) {
  1416. *wb = sc.wc;
  1417. wb += psfs.store;
  1418. } else {
  1419. i = wcrtomb(b, sc.wc, &mbstate);
  1420. if (i < 0) { /* Conversion failure. */
  1421. goto DONE_DO_UNGET;
  1422. }
  1423. if (psfs.store) {
  1424. b += i;
  1425. }
  1426. }
  1427. fail = 0;
  1428. }
  1429. }
  1430. /* Common tail for processing of %s and %[. */
  1431. __scan_ungetc(&sc);
  1432. if (fail) { /* nothing stored! */
  1433. goto DONE;
  1434. }
  1435. *wb = 0; /* Nul-terminate string. */
  1436. *b = 0;
  1437. psfs.cnt += psfs.store;
  1438. goto NEXT_FMT;
  1439. }
  1440. #endif /* L_vfscanf */
  1441. assert(0);
  1442. goto DONE;
  1443. } /* conversion specification */
  1444. MATCH_CHAR:
  1445. if (__scan_getc(&sc) != *fmt) {
  1446. #ifdef L_vfwscanf
  1447. DONE_DO_UNGET:
  1448. #endif /* L_vfwscanf */
  1449. __scan_ungetc(&sc);
  1450. goto DONE;
  1451. }
  1452. NEXT_FMT:
  1453. ++fmt;
  1454. if (__FERROR_UNLOCKED(fp)) {
  1455. break;
  1456. }
  1457. }
  1458. DONE:
  1459. if (__FERROR_UNLOCKED(fp) || (*fmt && zero_conversions && __FEOF_UNLOCKED(fp))) {
  1460. psfs.cnt = EOF; /* Yes, vfwscanf also returns EOF. */
  1461. }
  1462. kill_scan_cookie(&sc);
  1463. __STDIO_STREAM_VALIDATE(fp);
  1464. __STDIO_AUTO_THREADUNLOCK(fp);
  1465. return psfs.cnt;
  1466. }
  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) NULL);
  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 = 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(b, 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(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(b, 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(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. /**********************************************************************/