_scanf.c 55 KB

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