_scanf.c 55 KB

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