_scanf.c 54 KB

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