scanf.c 54 KB

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