scanf.c 54 KB

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