scanf.c 51 KB

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