scanf.c 55 KB

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