scanf.c 52 KB

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