_scanf.c 54 KB

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