_scanf.c 54 KB

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