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