scanf.c 54 KB

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