scanf.c 55 KB

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