_scanf.c 55 KB

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