_scanf.c 54 KB

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