_scanf.c 54 KB

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