scanf.c 55 KB

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