_scanf.c 55 KB

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