_scanf.c 54 KB

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