_scanf.c 54 KB

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