printf.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189
  1. /* Copyright (C) 2002, 2003 Manuel Novoa III
  2. * My stdio library for linux and (soon) elks.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the Free
  16. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /* This code needs a lot of clean up. Some of that is on hold until uClibc
  19. * gets a better configuration system (on Erik's todo list).
  20. * The other cleanup will take place during the implementation/integration of
  21. * the wide char (un)formatted i/o functions which I'm currently working on.
  22. */
  23. /* ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION!
  24. *
  25. * This code is currently under development. Also, I plan to port
  26. * it to elks which is a 16-bit environment with a fairly limited
  27. * compiler. Therefore, please refrain from modifying this code
  28. * and, instead, pass any bug-fixes, etc. to me. Thanks. Manuel
  29. *
  30. * ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION! */
  31. /* April 1, 2002
  32. * Initialize thread locks for fake files in vsnprintf and vdprintf.
  33. * reported by Erik Andersen (andersen@codepoet.com)
  34. * Fix an arg promotion handling bug in _do_one_spec for %c.
  35. * reported by Ilguiz Latypov <ilatypov@superbt.com>
  36. *
  37. * May 10, 2002
  38. * Remove __isdigit and use new ctype.h version.
  39. * Add conditional setting of QUAL_CHARS for size_t and ptrdiff_t.
  40. *
  41. * Aug 16, 2002
  42. * Fix two problems that showed up with the python 2.2.1 tests; one
  43. * involving %o and one involving %f.
  44. *
  45. * Oct 28, 2002
  46. * Fix a problem in vasprintf (reported by vodz a while back) when built
  47. * without custom stream support. In that case, it is necessary to do
  48. * a va_copy.
  49. * Make sure each va_copy has a matching va_end, as required by C99.
  50. *
  51. * Nov 4, 2002
  52. * Add locale-specific grouping support for integer decimal conversion.
  53. * Add locale-specific decimal point support for floating point conversion.
  54. * Note: grouping will have to wait for _dtostr() rewrite.
  55. * Add printf wchar support for %lc (%C) and %ls (%S).
  56. * Require printf format strings to be valid multibyte strings beginning and
  57. * ending in their initial shift state, as per the stds.
  58. *
  59. * Nov 21, 2002
  60. * Add *wprintf functions. Currently they don't support floating point
  61. * conversions. That will wait until the rewrite of _dtostr.
  62. *
  63. * Aug 1, 2003
  64. * Optional hexadecimal float notation support for %a/%A.
  65. * Floating point output now works for *wprintf.
  66. * Support for glibc locale-specific digit grouping for floats.
  67. * Misc bug fixes.
  68. *
  69. * Aug 31, 2003
  70. * Fix precision bug for %g conversion specifier when using %f style.
  71. */
  72. /* TODO:
  73. *
  74. * Should we validate that *printf format strings are valid multibyte
  75. * strings in the current locale? ANSI/ISO C99 seems to imply this
  76. * and Plauger's printf implementation in his Standard C Library book
  77. * treats this as an error.
  78. */
  79. #define _ISOC99_SOURCE /* for ULLONG primarily... */
  80. #define _GNU_SOURCE
  81. #define _STDIO_UTILITY /* We're using _uintmaxtostr. */
  82. #include <stdio.h>
  83. #include <stdlib.h>
  84. #include <string.h>
  85. #include <stddef.h>
  86. #include <ctype.h>
  87. #include <limits.h>
  88. #include <stdarg.h>
  89. #include <assert.h>
  90. #include <stdint.h>
  91. #include <errno.h>
  92. #include <locale.h>
  93. #define __PRINTF_INFO_NO_BITFIELD
  94. #include <printf.h>
  95. #ifdef __STDIO_THREADSAFE
  96. #include <stdio_ext.h>
  97. #include <pthread.h>
  98. #endif /* __STDIO_THREADSAFE */
  99. #ifdef __UCLIBC_HAS_WCHAR__
  100. #include <wchar.h>
  101. #endif /* __UCLIBC_HAS_WCHAR__ */
  102. /* Some older or broken gcc toolchains define LONG_LONG_MAX but not
  103. * LLONG_MAX. Since LLONG_MAX is part of the standard, that's what
  104. * we use. So complain if we do not have it but should.
  105. */
  106. #if !defined(LLONG_MAX) && defined(LONG_LONG_MAX)
  107. #error Apparently, LONG_LONG_MAX is defined but LLONG_MAX is not. You need to fix your toolchain headers to support the standard macros for (unsigned) long long.
  108. #endif
  109. /**********************************************************************/
  110. /* These provide some control over printf's feature set */
  111. /* This is undefined below depeding on uClibc's configuration. */
  112. #define __STDIO_PRINTF_FLOAT 1
  113. /* Now controlled by uClibc_stdio.h. */
  114. /* #define __STDIO_PRINTF_M_SUPPORT */
  115. /**********************************************************************/
  116. #if defined(__UCLIBC__) && !defined(__UCLIBC_HAS_FLOATS__)
  117. #undef __STDIO_PRINTF_FLOAT
  118. #endif
  119. #ifdef __BCC__
  120. #undef __STDIO_PRINTF_FLOAT
  121. #endif
  122. #ifdef __STDIO_PRINTF_FLOAT
  123. #include <float.h>
  124. #include <bits/uClibc_fpmax.h>
  125. #else /* __STDIO_PRINTF_FLOAT */
  126. #undef L__fpmaxtostr
  127. #endif /* __STDIO_PRINTF_FLOAT */
  128. /**********************************************************************/
  129. /* Now controlled by uClibc_stdio.h. */
  130. /* #define __STDIO_GLIBC_CUSTOM_PRINTF */
  131. /* TODO -- move these to a configuration section? */
  132. #define MAX_FIELD_WIDTH 4095
  133. #ifdef __UCLIBC_MJN3_ONLY__
  134. #ifdef L_register_printf_function
  135. /* emit only once */
  136. #warning WISHLIST: Make MAX_USER_SPEC configurable?
  137. #warning WISHLIST: Make MAX_ARGS_PER_SPEC configurable?
  138. #endif
  139. #endif /* __UCLIBC_MJN3_ONLY__ */
  140. #ifdef __STDIO_GLIBC_CUSTOM_PRINTF
  141. #define MAX_USER_SPEC 10
  142. #define MAX_ARGS_PER_SPEC 5
  143. #else /* __STDIO_GLIBC_CUSTOM_PRINTF */
  144. #undef MAX_USER_SPEC
  145. #define MAX_ARGS_PER_SPEC 1
  146. #endif /* __STDIO_GLIBC_CUSTOM_PRINTF */
  147. #if MAX_ARGS_PER_SPEC < 1
  148. #error MAX_ARGS_PER_SPEC < 1!
  149. #undef MAX_ARGS_PER_SPEC
  150. #define MAX_ARGS_PER_SPEC 1
  151. #endif
  152. #if defined(NL_ARGMAX) && (NL_ARGMAX < 9)
  153. #error NL_ARGMAX < 9!
  154. #endif
  155. #if defined(NL_ARGMAX) && (NL_ARGMAX >= (MAX_ARGS_PER_SPEC + 2))
  156. #define MAX_ARGS NL_ARGMAX
  157. #else
  158. /* N for spec itself, plus 1 each for width and precision */
  159. #define MAX_ARGS (MAX_ARGS_PER_SPEC + 2)
  160. #endif
  161. /**********************************************************************/
  162. /* Deal with pre-C99 compilers. */
  163. #ifndef va_copy
  164. #ifdef __va_copy
  165. #define va_copy(A,B) __va_copy(A,B)
  166. #else
  167. /* TODO -- maybe create a bits/vacopy.h for arch specific versions
  168. * to ensure we get the right behavior? Either that or fall back
  169. * on the portable (but costly in size) method of using a va_list *.
  170. * That means a pointer derefs in the va_arg() invocations... */
  171. #warning Neither va_copy (C99/SUSv3) or __va_copy is defined. Using a simple copy instead. But you should really check that this is appropriate...
  172. /* the glibc manual suggests that this will usually suffice when
  173. __va_copy doesn't exist. */
  174. #define va_copy(A,B) A = B
  175. #endif
  176. #endif /* va_copy */
  177. /**********************************************************************/
  178. #define __PA_FLAG_INTMASK \
  179. (__PA_FLAG_CHAR|PA_FLAG_SHORT|__PA_FLAG_INT|PA_FLAG_LONG|PA_FLAG_LONG_LONG)
  180. #ifdef __STDIO_GLIBC_CUSTOM_PRINTF
  181. extern printf_function _custom_printf_handler[MAX_USER_SPEC];
  182. extern printf_arginfo_function *_custom_printf_arginfo[MAX_USER_SPEC];
  183. extern char *_custom_printf_spec;
  184. #endif /* __STDIO_GLIBC_CUSTOM_PRINTF */
  185. /**********************************************************************/
  186. #define SPEC_FLAGS " +0-#'I"
  187. enum {
  188. FLAG_SPACE = 0x01,
  189. FLAG_PLUS = 0x02, /* must be 2 * FLAG_SPACE */
  190. FLAG_ZERO = 0x04,
  191. FLAG_MINUS = 0x08, /* must be 2 * FLAG_ZERO */
  192. FLAG_HASH = 0x10,
  193. FLAG_THOUSANDS = 0x20,
  194. FLAG_I18N = 0x40, /* only works for d, i, u */
  195. FLAG_WIDESTREAM = 0x80
  196. };
  197. /**********************************************************************/
  198. /* float layout 01234567890123456789 TODO: B?*/
  199. #define SPEC_CHARS "npxXoudifFeEgGaACScs"
  200. enum {
  201. CONV_n = 0,
  202. CONV_p,
  203. CONV_x, CONV_X, CONV_o, CONV_u, CONV_d, CONV_i,
  204. CONV_f, CONV_F, CONV_e, CONV_E, CONV_g, CONV_G, CONV_a, CONV_A,
  205. CONV_C, CONV_S, CONV_c, CONV_s,
  206. #ifdef __STDIO_PRINTF_M_SUPPORT
  207. CONV_m,
  208. #endif
  209. CONV_custom0 /* must be last */
  210. };
  211. /* p x X o u d i */
  212. #define SPEC_BASE { 16, 16, 16, 8, 10, 10, 10 }
  213. #define SPEC_RANGES { CONV_n, CONV_p, CONV_i, CONV_A, \
  214. CONV_C, CONV_S, CONV_c, CONV_s, CONV_custom0 }
  215. #define SPEC_OR_MASK { \
  216. /* n */ (PA_FLAG_PTR|PA_INT), \
  217. /* p */ PA_POINTER, \
  218. /* oxXudi */ PA_INT, \
  219. /* fFeEgGaA */ PA_DOUBLE, \
  220. /* C */ PA_WCHAR, \
  221. /* S */ PA_WSTRING, \
  222. /* c */ PA_CHAR, \
  223. /* s */ PA_STRING, \
  224. }
  225. #define SPEC_AND_MASK { \
  226. /* n */ (PA_FLAG_PTR|__PA_INTMASK), \
  227. /* p */ PA_POINTER, \
  228. /* oxXudi */ (__PA_INTMASK), \
  229. /* fFeEgGaA */ (PA_FLAG_LONG_DOUBLE|PA_DOUBLE), \
  230. /* C */ (PA_WCHAR), \
  231. /* S */ (PA_WSTRING), \
  232. /* c */ (PA_CHAR), \
  233. /* s */ (PA_STRING), \
  234. }
  235. /**********************************************************************/
  236. /*
  237. * In order to ease translation to what arginfo and _print_info._flags expect,
  238. * we map: 0:int 1:char 2:longlong 4:long 8:short
  239. * and then _flags |= (((q << 7) + q) & 0x701) and argtype |= (_flags & 0x701)
  240. */
  241. /* TODO -- Fix the table below to take into account stdint.h. */
  242. /* #ifndef LLONG_MAX */
  243. /* #error fix QUAL_CHARS for no long long! Affects 'L', 'j', 'q', 'll'. */
  244. /* #else */
  245. /* #if LLONG_MAX != INTMAX_MAX */
  246. /* #error fix QUAL_CHARS intmax_t entry 'j'! */
  247. /* #endif */
  248. /* #endif */
  249. #ifdef PDS
  250. #error PDS already defined!
  251. #endif
  252. #ifdef SS
  253. #error SS already defined!
  254. #endif
  255. #ifdef IMS
  256. #error IMS already defined!
  257. #endif
  258. #if PTRDIFF_MAX == INT_MAX
  259. #define PDS 0
  260. #elif PTRDIFF_MAX == LONG_MAX
  261. #define PDS 4
  262. #elif defined(LLONG_MAX) && (PTRDIFF_MAX == LLONG_MAX)
  263. #define PDS 8
  264. #else
  265. #error fix QUAL_CHARS ptrdiff_t entry 't'!
  266. #endif
  267. #if SIZE_MAX == UINT_MAX
  268. #define SS 0
  269. #elif SIZE_MAX == ULONG_MAX
  270. #define SS 4
  271. #elif defined(LLONG_MAX) && (SIZE_MAX == ULLONG_MAX)
  272. #define SS 8
  273. #else
  274. #error fix QUAL_CHARS size_t entries 'z', 'Z'!
  275. #endif
  276. #if INTMAX_MAX == INT_MAX
  277. #define IMS 0
  278. #elif INTMAX_MAX == LONG_MAX
  279. #define IMS 4
  280. #elif defined(LLONG_MAX) && (INTMAX_MAX == LLONG_MAX)
  281. #define IMS 8
  282. #else
  283. #error fix QUAL_CHARS intmax_t entry 'j'!
  284. #endif
  285. #define QUAL_CHARS { \
  286. /* j:(u)intmax_t z:(s)size_t t:ptrdiff_t \0:int */ \
  287. /* q:long_long Z:(s)size_t */ \
  288. 'h', 'l', 'L', 'j', 'z', 't', 'q', 'Z', 0, \
  289. 2, 4, 8, IMS, SS, PDS, 8, SS, 0, /* TODO -- fix!!! */\
  290. 1, 8 \
  291. }
  292. /**********************************************************************/
  293. #ifdef __STDIO_VA_ARG_PTR
  294. #ifdef __BCC__
  295. #define __va_arg_ptr(ap,type) (((type *)(ap += sizeof(type))) - 1)
  296. #endif
  297. #if 1
  298. #ifdef __GNUC__
  299. /* TODO -- need other than for 386 as well! */
  300. #ifndef __va_rounded_size
  301. #define __va_rounded_size(TYPE) \
  302. (((sizeof (TYPE) + sizeof (int) - 1) / sizeof (int)) * sizeof (int))
  303. #endif
  304. #define __va_arg_ptr(AP, TYPE) \
  305. (AP = (va_list) ((char *) (AP) + __va_rounded_size (TYPE)), \
  306. ((void *) ((char *) (AP) - __va_rounded_size (TYPE))))
  307. #endif
  308. #endif
  309. #endif /* __STDIO_VA_ARG_PTR */
  310. #ifdef __va_arg_ptr
  311. #define GET_VA_ARG(AP,F,TYPE,ARGS) (*(AP) = __va_arg_ptr(ARGS,TYPE))
  312. #define GET_ARG_VALUE(AP,F,TYPE) (*((TYPE *)(*(AP))))
  313. #else
  314. typedef union {
  315. wchar_t wc;
  316. unsigned int u;
  317. unsigned long ul;
  318. #ifdef ULLONG_MAX
  319. unsigned long long ull;
  320. #endif
  321. #ifdef __STDIO_PRINTF_FLOAT
  322. double d;
  323. long double ld;
  324. #endif /* __STDIO_PRINTF_FLOAT */
  325. void *p;
  326. } argvalue_t;
  327. #define GET_VA_ARG(AU,F,TYPE,ARGS) (AU->F = va_arg(ARGS,TYPE))
  328. #define GET_ARG_VALUE(AU,F,TYPE) ((TYPE)((AU)->F))
  329. #endif
  330. typedef struct {
  331. const char *fmtpos; /* TODO: move below struct?? */
  332. struct printf_info info;
  333. #ifdef NL_ARGMAX
  334. int maxposarg; /* > 0 if args are positional, 0 if not, -1 if unknown */
  335. #endif /* NL_ARGMAX */
  336. int num_data_args; /* TODO: use sentinal??? */
  337. unsigned int conv_num;
  338. unsigned char argnumber[4]; /* width | prec | 1st data | unused */
  339. int argtype[MAX_ARGS];
  340. va_list arg;
  341. #ifdef __va_arg_ptr
  342. void *argptr[MAX_ARGS];
  343. #else
  344. /* if defined(NL_ARGMAX) || defined(__STDIO_GLIBC_CUSTOM_PRINTF) */
  345. /* While this is wasteful of space in the case where pos args aren't
  346. * enabled, it is also needed to support custom printf handlers. */
  347. argvalue_t argvalue[MAX_ARGS];
  348. #endif
  349. } ppfs_t; /* parse printf format state */
  350. /**********************************************************************/
  351. /* TODO: fix printf to return 0 and set errno if format error. Standard says
  352. only returns -1 if sets error indicator for the stream. */
  353. #ifdef __STDIO_PRINTF_FLOAT
  354. typedef void (__fp_outfunc_t)(FILE *fp, intptr_t type, intptr_t len,
  355. intptr_t buf);
  356. extern size_t _fpmaxtostr(FILE * fp, __fpmax_t x, struct printf_info *info,
  357. __fp_outfunc_t fp_outfunc);
  358. #endif
  359. extern int _ppfs_init(ppfs_t *ppfs, const char *fmt0); /* validates */
  360. extern void _ppfs_prepargs(ppfs_t *ppfs, va_list arg); /* sets posargptrs */
  361. extern void _ppfs_setargs(ppfs_t *ppfs); /* sets argptrs for current spec */
  362. extern int _ppfs_parsespec(ppfs_t *ppfs); /* parses specifier */
  363. extern void _store_inttype(void *dest, int desttype, uintmax_t val);
  364. extern uintmax_t _load_inttype(int desttype, const void *src, int uflag);
  365. /**********************************************************************/
  366. #ifdef L_parse_printf_format
  367. /* NOTE: This function differs from the glibc version in that parsing stops
  368. * upon encountering an invalid conversion specifier. Since this is the way
  369. * my printf functions work, I think it makes sense to do it that way here.
  370. * Unfortunately, since glibc sets the return type as size_t, we have no way
  371. * of returning that the template is illegal, other than returning 0.
  372. */
  373. size_t parse_printf_format(register const char *template,
  374. size_t n, register int *argtypes)
  375. {
  376. ppfs_t ppfs;
  377. size_t i;
  378. size_t count = 0;
  379. if (_ppfs_init(&ppfs, template) >= 0) {
  380. #ifdef NL_ARGMAX
  381. if (ppfs.maxposarg > 0) { /* Using positional args. */
  382. count = ppfs.maxposarg;
  383. if (n > count) {
  384. n = count;
  385. }
  386. for (i = 0 ; i < n ; i++) {
  387. *argtypes++ = ppfs.argtype[i];
  388. }
  389. } else { /* Not using positional args. */
  390. #endif /* NL_ARGMAX */
  391. while (*template) {
  392. if ((*template == '%') && (*++template != '%')) {
  393. ppfs.fmtpos = template;
  394. _ppfs_parsespec(&ppfs); /* Can't fail. */
  395. template = ppfs.fmtpos; /* Update to one past spec end. */
  396. if (ppfs.info.width == INT_MIN) {
  397. ++count;
  398. if (n > 0) {
  399. *argtypes++ = PA_INT;
  400. --n;
  401. }
  402. }
  403. if (ppfs.info.prec == INT_MIN) {
  404. ++count;
  405. if (n > 0) {
  406. *argtypes++ = PA_INT;
  407. --n;
  408. }
  409. }
  410. for (i = 0 ; i < ppfs.num_data_args ; i++) {
  411. if ((ppfs.argtype[i]) != __PA_NOARG) {
  412. ++count;
  413. if (n > 0) {
  414. *argtypes++ = ppfs.argtype[i];
  415. --n;
  416. }
  417. }
  418. }
  419. } else {
  420. ++template;
  421. }
  422. }
  423. #ifdef NL_ARGMAX
  424. }
  425. #endif /* NL_ARGMAX */
  426. }
  427. return count;
  428. }
  429. #endif
  430. /**********************************************************************/
  431. #ifdef L__ppfs_init
  432. int _ppfs_init(register ppfs_t *ppfs, const char *fmt0)
  433. {
  434. int r;
  435. /* First, zero out everything... argnumber[], argtype[], argptr[] */
  436. memset(ppfs, 0, sizeof(ppfs_t)); /* TODO: nonportable???? */
  437. #ifdef NL_ARGMAX
  438. --ppfs->maxposarg; /* set to -1 */
  439. #endif /* NL_ARGMAX */
  440. ppfs->fmtpos = fmt0;
  441. #ifdef __UCLIBC_MJN3_ONLY__
  442. #warning TODO: Make checking of the format string in C locale an option.
  443. #endif
  444. #ifdef __UCLIBC_HAS_LOCALE__
  445. /* To support old programs, don't check mb validity if in C locale. */
  446. if (((__UCLIBC_CURLOCALE_DATA).encoding) != __ctype_encoding_7_bit) {
  447. /* ANSI/ISO C99 requires format string to be a valid multibyte string
  448. * beginning and ending in its initial shift state. */
  449. static const char invalid_mbs[] = "Invalid multibyte format string.";
  450. mbstate_t mbstate;
  451. const char *p;
  452. mbstate.mask = 0; /* Initialize the mbstate. */
  453. p = fmt0;
  454. if (mbsrtowcs(NULL, &p, SIZE_MAX, &mbstate) == ((size_t)(-1))) {
  455. ppfs->fmtpos = invalid_mbs;
  456. return -1;
  457. }
  458. }
  459. #endif /* __UCLIBC_HAS_LOCALE__ */
  460. /* now set all argtypes to no-arg */
  461. {
  462. #if 1
  463. /* TODO - use memset here since already "paid for"? */
  464. register int *p = ppfs->argtype;
  465. r = MAX_ARGS;
  466. do {
  467. *p++ = __PA_NOARG;
  468. } while (--r);
  469. #else
  470. /* TODO -- get rid of this?? */
  471. register char *p = (char *) ((MAX_ARGS-1) * sizeof(int));
  472. do {
  473. *((int *)(((char *)ppfs) + ((int)p) + offsetof(ppfs_t,argtype))) = __PA_NOARG;
  474. p -= sizeof(int);
  475. } while (p);
  476. #endif
  477. }
  478. /*
  479. * Run through the entire format string to validate it and initialize
  480. * the positional arg numbers (if any).
  481. */
  482. {
  483. register const char *fmt = fmt0;
  484. while (*fmt) {
  485. if ((*fmt == '%') && (*++fmt != '%')) {
  486. ppfs->fmtpos = fmt; /* back up to the '%' */
  487. if ((r = _ppfs_parsespec(ppfs)) < 0) {
  488. return -1;
  489. }
  490. fmt = ppfs->fmtpos; /* update to one past end of spec */
  491. } else {
  492. ++fmt;
  493. }
  494. }
  495. ppfs->fmtpos = fmt0; /* rewind */
  496. }
  497. #ifdef NL_MAX_ARG
  498. /* If we have positional args, make sure we know all the types. */
  499. {
  500. register int *p = ppfs->argtype;
  501. r = ppfs->maxposarg;
  502. while (--r >= 0) {
  503. if ( *p == __PA_NOARG ) { /* missing arg type!!! */
  504. return -1;
  505. }
  506. ++p;
  507. }
  508. }
  509. #endif /* NL_MAX_ARG */
  510. return 0;
  511. }
  512. #endif
  513. /**********************************************************************/
  514. #ifdef L__ppfs_prepargs
  515. void _ppfs_prepargs(register ppfs_t *ppfs, va_list arg)
  516. {
  517. int i;
  518. va_copy(ppfs->arg, arg);
  519. #ifdef NL_ARGMAX
  520. if ((i = ppfs->maxposarg) > 0) { /* init for positional args */
  521. ppfs->num_data_args = i;
  522. ppfs->info.width = ppfs->info.prec = ppfs->maxposarg = 0;
  523. _ppfs_setargs(ppfs);
  524. ppfs->maxposarg = i;
  525. }
  526. #endif /* NL_ARGMAX */
  527. }
  528. #endif
  529. /**********************************************************************/
  530. #ifdef L__ppfs_setargs
  531. void _ppfs_setargs(register ppfs_t *ppfs)
  532. {
  533. #ifdef __va_arg_ptr
  534. register void **p = ppfs->argptr;
  535. #else
  536. register argvalue_t *p = ppfs->argvalue;
  537. #endif
  538. int i;
  539. #ifdef NL_ARGMAX
  540. if (ppfs->maxposarg == 0) { /* initing for or no pos args */
  541. #endif /* NL_ARGMAX */
  542. if (ppfs->info.width == INT_MIN) {
  543. ppfs->info.width =
  544. #ifdef __va_arg_ptr
  545. *(int *)
  546. #endif
  547. GET_VA_ARG(p,u,unsigned int,ppfs->arg);
  548. }
  549. if (ppfs->info.prec == INT_MIN) {
  550. ppfs->info.prec =
  551. #ifdef __va_arg_ptr
  552. *(int *)
  553. #endif
  554. GET_VA_ARG(p,u,unsigned int,ppfs->arg);
  555. }
  556. i = 0;
  557. while (i < ppfs->num_data_args) {
  558. switch(ppfs->argtype[i++]) {
  559. case (PA_INT|PA_FLAG_LONG_LONG):
  560. #ifdef ULLONG_MAX
  561. GET_VA_ARG(p,ull,unsigned long long,ppfs->arg);
  562. break;
  563. #endif
  564. case (PA_INT|PA_FLAG_LONG):
  565. #if ULONG_MAX != UINT_MAX
  566. GET_VA_ARG(p,ul,unsigned long,ppfs->arg);
  567. break;
  568. #endif
  569. case PA_CHAR: /* TODO - be careful */
  570. /* ... users could use above and really want below!! */
  571. case (PA_INT|__PA_FLAG_CHAR):/* TODO -- translate this!!! */
  572. case (PA_INT|PA_FLAG_SHORT):
  573. case PA_INT:
  574. GET_VA_ARG(p,u,unsigned int,ppfs->arg);
  575. break;
  576. case PA_WCHAR: /* TODO -- assume int? */
  577. /* we're assuming wchar_t is at least an int */
  578. GET_VA_ARG(p,wc,wchar_t,ppfs->arg);
  579. break;
  580. #ifdef __STDIO_PRINTF_FLOAT
  581. /* PA_FLOAT */
  582. case PA_DOUBLE:
  583. GET_VA_ARG(p,d,double,ppfs->arg);
  584. break;
  585. case (PA_DOUBLE|PA_FLAG_LONG_DOUBLE):
  586. GET_VA_ARG(p,ld,long double,ppfs->arg);
  587. break;
  588. #else /* __STDIO_PRINTF_FLOAT */
  589. case PA_DOUBLE:
  590. case (PA_DOUBLE|PA_FLAG_LONG_DOUBLE):
  591. assert(0);
  592. continue;
  593. #endif /* __STDIO_PRINTF_FLOAT */
  594. default:
  595. /* TODO -- really need to ensure this can't happen */
  596. assert(ppfs->argtype[i-1] & PA_FLAG_PTR);
  597. case PA_POINTER:
  598. case PA_STRING:
  599. case PA_WSTRING:
  600. GET_VA_ARG(p,p,void *,ppfs->arg);
  601. break;
  602. case __PA_NOARG:
  603. continue;
  604. }
  605. ++p;
  606. }
  607. #ifdef NL_ARGMAX
  608. } else {
  609. if (ppfs->info.width == INT_MIN) {
  610. ppfs->info.width
  611. = (int) GET_ARG_VALUE(p + ppfs->argnumber[0] - 1,u,unsigned int);
  612. }
  613. if (ppfs->info.prec == INT_MIN) {
  614. ppfs->info.prec
  615. = (int) GET_ARG_VALUE(p + ppfs->argnumber[1] - 1,u,unsigned int);
  616. }
  617. }
  618. #endif /* NL_ARGMAX */
  619. /* Now we know the width and precision. */
  620. if (ppfs->info.width < 0) {
  621. ppfs->info.width = -ppfs->info.width;
  622. PRINT_INFO_SET_FLAG(&(ppfs->info),left);
  623. PRINT_INFO_CLR_FLAG(&(ppfs->info),space);
  624. ppfs->info.pad = ' ';
  625. }
  626. #if 0
  627. /* NOTE -- keep neg for now so float knows! */
  628. if (ppfs->info.prec < 0) { /* spec says treat as omitted. */
  629. /* so use default prec... 1 for everything but floats and strings. */
  630. ppfs->info.prec = 1;
  631. }
  632. #endif
  633. }
  634. #endif
  635. /**********************************************************************/
  636. #ifdef L__ppfs_parsespec
  637. /* Notes: argtype differs from glibc for the following:
  638. * mine glibc
  639. * lc PA_WCHAR PA_CHAR the standard says %lc means %C
  640. * ls PA_WSTRING PA_STRING the standard says %ls means %S
  641. * {*}n {*}|PA_FLAG_PTR PA_FLAG_PTR size of n can be qualified
  642. */
  643. /* TODO: store positions of positional args */
  644. /* TODO -- WARNING -- assumes aligned on integer boundaries!!! */
  645. /* TODO -- disable if not using positional args!!! */
  646. #define _OVERLAPPING_DIFFERENT_ARGS
  647. /* TODO -- rethink this -- perhaps we should set to largest type??? */
  648. #ifdef _OVERLAPPING_DIFFERENT_ARGS
  649. #define PROMOTED_SIZE_OF(X) ((sizeof(X) + sizeof(int) - 1) / sizeof(X))
  650. static const short int type_codes[] = {
  651. __PA_NOARG, /* must be first entry */
  652. PA_POINTER,
  653. PA_STRING,
  654. PA_WSTRING,
  655. PA_CHAR,
  656. PA_INT|PA_FLAG_SHORT,
  657. PA_INT,
  658. PA_INT|PA_FLAG_LONG,
  659. PA_INT|PA_FLAG_LONG_LONG,
  660. PA_WCHAR,
  661. #ifdef __STDIO_PRINTF_FLOAT
  662. /* PA_FLOAT, */
  663. PA_DOUBLE,
  664. PA_DOUBLE|PA_FLAG_LONG_DOUBLE,
  665. #endif /* __STDIO_PRINTF_FLOAT */
  666. };
  667. static const unsigned char type_sizes[] = {
  668. /* unknown type consumes no arg */
  669. 0, /* must be first entry */
  670. PROMOTED_SIZE_OF(void *),
  671. PROMOTED_SIZE_OF(char *),
  672. PROMOTED_SIZE_OF(wchar_t *),
  673. PROMOTED_SIZE_OF(char),
  674. PROMOTED_SIZE_OF(short),
  675. PROMOTED_SIZE_OF(int),
  676. PROMOTED_SIZE_OF(long),
  677. #ifdef ULLONG_MAX
  678. PROMOTED_SIZE_OF(long long),
  679. #else
  680. PROMOTED_SIZE_OF(long), /* TODO -- is this correct? (above too) */
  681. #endif
  682. PROMOTED_SIZE_OF(wchar_t),
  683. #ifdef __STDIO_PRINTF_FLOAT
  684. /* PROMOTED_SIZE_OF(float), */
  685. PROMOTED_SIZE_OF(double),
  686. PROMOTED_SIZE_OF(long double),
  687. #endif /* __STDIO_PRINTF_FLOAT */
  688. };
  689. static int _promoted_size(int argtype)
  690. {
  691. register const short int *p;
  692. /* note -- since any unrecognized type is treated as a pointer */
  693. p = type_codes + sizeof(type_codes)/sizeof(type_codes[0]);
  694. do {
  695. if (*--p == argtype) {
  696. break;
  697. }
  698. } while (p > type_codes);
  699. return type_sizes[(int)(p - type_codes)];
  700. }
  701. static int _is_equal_or_bigger_arg(int curtype, int newtype)
  702. {
  703. /* Quick test */
  704. if (newtype == __PA_NOARG) {
  705. return 0;
  706. }
  707. if ((curtype == __PA_NOARG) || (curtype == newtype)) {
  708. return 1;
  709. }
  710. /* Ok... slot is already filled and types are different in name. */
  711. /* So, compare promoted sizes of curtype and newtype args. */
  712. return _promoted_size(curtype) <= _promoted_size(newtype);
  713. }
  714. #else
  715. #define _is_equal_or_bigger_arg(C,N) (((C) == __PA_NOARG) || ((C) == (N)))
  716. #endif
  717. #ifdef __STDIO_GLIBC_CUSTOM_PRINTF
  718. /* TODO - do this differently? */
  719. static char _bss_custom_printf_spec[MAX_USER_SPEC]; /* 0-init'd for us. */
  720. char *_custom_printf_spec = _bss_custom_printf_spec;
  721. printf_arginfo_function *_custom_printf_arginfo[MAX_USER_SPEC];
  722. printf_function _custom_printf_handler[MAX_USER_SPEC];
  723. #endif /* __STDIO_GLIBC_CUSTOM_PRINTF */
  724. extern int _ppfs_parsespec(ppfs_t *ppfs)
  725. {
  726. register const char *fmt;
  727. register const char *p;
  728. int preci;
  729. int width;
  730. int flags;
  731. int dataargtype;
  732. int i;
  733. int dpoint;
  734. #ifdef NL_ARGMAX
  735. int maxposarg;
  736. #endif /* NL_ARGMAX */
  737. int p_m_spec_chars;
  738. int n;
  739. int argtype[MAX_ARGS_PER_SPEC+2];
  740. int argnumber[3]; /* width, precision, 1st data arg */
  741. static const char spec_flags[] = SPEC_FLAGS;
  742. static const char spec_chars[] = SPEC_CHARS;/* TODO: b? */
  743. static const char spec_ranges[] = SPEC_RANGES;
  744. static const short spec_or_mask[] = SPEC_OR_MASK;
  745. static const short spec_and_mask[] = SPEC_AND_MASK;
  746. static const char qual_chars[] = QUAL_CHARS;
  747. #ifdef __UCLIBC_HAS_WCHAR__
  748. char buf[32];
  749. #endif /* __UCLIBC_HAS_WCHAR__ */
  750. /* WIDE note: we can test against '%' here since we don't allow */
  751. /* WIDE note: other mappings of '%' in the wide char set. */
  752. preci = -1;
  753. argnumber[0] = 0;
  754. argnumber[1] = 0;
  755. argtype[0] = __PA_NOARG;
  756. argtype[1] = __PA_NOARG;
  757. #ifdef NL_ARGMAX
  758. maxposarg = ppfs->maxposarg;
  759. #endif /* NL_ARGMAX */
  760. #ifdef __UCLIBC_HAS_WCHAR__
  761. /* This is somewhat lame, but saves a lot of code. If we're dealing with
  762. * a wide stream, that means the format is a wchar string. So, copy it
  763. * char-by-char into a normal char buffer for processing. Make the buffer
  764. * (buf) big enough so that any reasonable format specifier will fit.
  765. * While there a legal specifiers that won't, the all involve duplicate
  766. * flags or outrageous field widths/precisions. */
  767. width = dpoint = 0;
  768. if ((flags = ppfs->info._flags & FLAG_WIDESTREAM) == 0) {
  769. fmt = ppfs->fmtpos;
  770. } else {
  771. fmt = buf + 1;
  772. i = 0;
  773. do {
  774. if ((buf[i] = (char) (((wchar_t *) ppfs->fmtpos)[i-1]))
  775. != (((wchar_t *) ppfs->fmtpos)[i-1])
  776. ) {
  777. return -1;
  778. }
  779. } while (buf[i++]);
  780. buf[sizeof(buf)-1] = 0;
  781. }
  782. #else /* __UCLIBC_HAS_WCHAR__ */
  783. width = flags = dpoint = 0;
  784. fmt = ppfs->fmtpos;
  785. #endif /* __UCLIBC_HAS_WCHAR__ */
  786. assert(fmt[-1] == '%');
  787. assert(fmt[0] != '%');
  788. /* Process arg pos and/or flags and/or width and/or precision. */
  789. width_precision:
  790. p = fmt;
  791. if (*fmt == '*') {
  792. argtype[-dpoint] = PA_INT;
  793. ++fmt;
  794. }
  795. i = 0;
  796. while (isdigit(*fmt)) {
  797. if (i < MAX_FIELD_WIDTH) { /* Avoid overflow. */
  798. i = (i * 10) + (*fmt - '0');
  799. }
  800. ++fmt;
  801. }
  802. if (p[-1] == '%') { /* Check for a position. */
  803. /* TODO: if val not in range, then error */
  804. #ifdef NL_ARGMAX
  805. if ((*fmt == '$') && (i > 0)) {/* Positional spec. */
  806. ++fmt;
  807. if (maxposarg == 0) {
  808. return -1;
  809. }
  810. if ((argnumber[2] = i) > maxposarg) {
  811. maxposarg = i;
  812. }
  813. /* Now fall through to check flags. */
  814. } else {
  815. if (maxposarg > 0) {
  816. #ifdef __STDIO_PRINTF_M_SUPPORT
  817. #ifdef __UCLIBC_MJN3_ONLY__
  818. #warning TODO: Support prec and width for %m when positional args used
  819. /* Actually, positional arg processing will fail in general
  820. * for specifiers that don't require an arg. */
  821. #endif /* __UCLIBC_MJN3_ONLY__ */
  822. if (*fmt == 'm') {
  823. goto PREC_WIDTH;
  824. }
  825. #endif /* __STDIO_PRINTF_M_SUPPORT */
  826. return -1;
  827. }
  828. maxposarg = 0; /* Possible redundant store, but cuts size. */
  829. if ((fmt > p) && (*p != '0')) {
  830. goto PREC_WIDTH;
  831. }
  832. fmt = p; /* Back up for possible '0's flag. */
  833. /* Now fall through to check flags. */
  834. }
  835. #else /* NL_ARGMAX */
  836. if (*fmt == '$') { /* Positional spec. */
  837. return -1;
  838. }
  839. if ((fmt > p) && (*p != '0')) {
  840. goto PREC_WIDTH;
  841. }
  842. fmt = p; /* Back up for possible '0's flag. */
  843. /* Now fall through to check flags. */
  844. #endif /* NL_ARGMAX */
  845. restart_flags: /* Process flags. */
  846. i = 1;
  847. p = spec_flags;
  848. do {
  849. if (*fmt == *p++) {
  850. ++fmt;
  851. flags |= i;
  852. goto restart_flags;
  853. }
  854. i += i; /* Better than i <<= 1 for bcc */
  855. } while (*p);
  856. i = 0;
  857. /* If '+' then ignore ' ', and if '-' then ignore '0'. */
  858. /* Note: Need to ignore '0' when prec is an arg with val < 0, */
  859. /* but that test needs to wait until the arg is retrieved. */
  860. flags &= ~((flags & (FLAG_PLUS|FLAG_MINUS)) >> 1);
  861. /* Note: Ignore '0' when prec is specified < 0 too (in printf). */
  862. if (fmt[-1] != '%') { /* If we've done anything, loop for width. */
  863. goto width_precision;
  864. }
  865. }
  866. PREC_WIDTH:
  867. if (*p == '*') { /* Prec or width takes an arg. */
  868. #ifdef NL_ARGMAX
  869. if (maxposarg) {
  870. if ((*fmt++ != '$') || (i <= 0)) {
  871. /* Using pos args and no $ or invalid arg number. */
  872. return -1;
  873. }
  874. argnumber[-dpoint] = i;
  875. } else
  876. #endif /* NL_ARGMAX */
  877. if (++p != fmt) {
  878. /* Not using pos args but digits followed *. */
  879. return -1;
  880. }
  881. i = INT_MIN;
  882. }
  883. if (!dpoint) {
  884. width = i;
  885. if (*fmt == '.') {
  886. ++fmt;
  887. dpoint = -1; /* To use as default precison. */
  888. goto width_precision;
  889. }
  890. } else {
  891. preci = i;
  892. }
  893. /* Process qualifier. */
  894. p = qual_chars;
  895. do {
  896. if (*fmt == *p) {
  897. ++fmt;
  898. break;
  899. }
  900. } while (*++p);
  901. if ((p - qual_chars < 2) && (*fmt == *p)) {
  902. p += ((sizeof(qual_chars)-2) / 2);
  903. ++fmt;
  904. }
  905. dataargtype = ((int)(p[(sizeof(qual_chars)-2) / 2])) << 8;
  906. /* Process conversion specifier. */
  907. if (!*fmt) {
  908. return -1;
  909. }
  910. p = spec_chars;
  911. do {
  912. if (*fmt == *p) {
  913. p_m_spec_chars = p - spec_chars;
  914. if ((p_m_spec_chars >= CONV_c)
  915. && (dataargtype & PA_FLAG_LONG)) {
  916. p_m_spec_chars -= 2; /* lc -> C and ls -> S */
  917. }
  918. ppfs->conv_num = p_m_spec_chars;
  919. p = spec_ranges-1;
  920. while (p_m_spec_chars > *++p) {}
  921. i = p - spec_ranges;
  922. argtype[2] = (dataargtype | spec_or_mask[i]) & spec_and_mask[i];
  923. p = spec_chars;
  924. break;
  925. }
  926. } while(*++p);
  927. ppfs->info.spec = *fmt;
  928. ppfs->info.prec = preci;
  929. ppfs->info.width = width;
  930. ppfs->info.pad = ((flags & FLAG_ZERO) ? '0' : ' ');
  931. ppfs->info._flags = (flags & ~FLAG_ZERO) | (dataargtype & __PA_INTMASK);
  932. ppfs->num_data_args = 1;
  933. if (!*p) {
  934. #ifdef __STDIO_PRINTF_M_SUPPORT
  935. if (*fmt == 'm') {
  936. ppfs->conv_num = CONV_m;
  937. ppfs->num_data_args = 0;
  938. goto DONE;
  939. }
  940. #endif
  941. #ifdef __STDIO_GLIBC_CUSTOM_PRINTF
  942. /* Handle custom arg -- WARNING -- overwrites p!!! */
  943. ppfs->conv_num = CONV_custom0;
  944. p = _custom_printf_spec;
  945. do {
  946. if (*p == *fmt) {
  947. if ((ppfs->num_data_args
  948. = ((*_custom_printf_arginfo[(int)(p-_custom_printf_spec)])
  949. (&(ppfs->info), MAX_ARGS_PER_SPEC, argtype+2)))
  950. > MAX_ARGS_PER_SPEC) {
  951. break; /* Error -- too many args! */
  952. }
  953. goto DONE;
  954. }
  955. } while (++p < (_custom_printf_spec + MAX_USER_SPEC));
  956. #endif /* __STDIO_GLIBC_CUSTOM_PRINTF */
  957. /* Otherwise error. */
  958. return -1;
  959. }
  960. #if defined(__STDIO_GLIBC_CUSTOM_PRINTF) || defined(__STDIO_PRINTF_M_SUPPORT)
  961. DONE:
  962. #endif
  963. #ifdef NL_ARGMAX
  964. if (maxposarg > 0) {
  965. i = 0;
  966. do {
  967. /* Update maxposarg and check that NL_ARGMAX is not exceeded. */
  968. n = ((i <= 2)
  969. ? (ppfs->argnumber[i] = argnumber[i])
  970. : argnumber[2] + (i-2));
  971. if (n > maxposarg) {
  972. if ((maxposarg = n) > NL_ARGMAX) {
  973. return -1;
  974. }
  975. }
  976. --n;
  977. /* Record argtype with largest size (current, new). */
  978. if (_is_equal_or_bigger_arg(ppfs->argtype[n], argtype[i])) {
  979. ppfs->argtype[n] = argtype[i];
  980. }
  981. } while (++i < ppfs->num_data_args + 2);
  982. } else {
  983. #endif /* NL_ARGMAX */
  984. ppfs->argnumber[2] = 1;
  985. memcpy(ppfs->argtype, argtype + 2, ppfs->num_data_args * sizeof(int));
  986. #ifdef NL_ARGMAX
  987. }
  988. ppfs->maxposarg = maxposarg;
  989. #endif /* NL_ARGMAX */
  990. #ifdef __UCLIBC_HAS_WCHAR__
  991. if ((flags = ppfs->info._flags & FLAG_WIDESTREAM) == 0) {
  992. ppfs->fmtpos = ++fmt;
  993. } else {
  994. ppfs->fmtpos = (const char *) (((const wchar_t *)(ppfs->fmtpos))
  995. + (fmt - buf) );
  996. }
  997. #else /* __UCLIBC_HAS_WCHAR__ */
  998. ppfs->fmtpos = ++fmt;
  999. #endif /* __UCLIBC_HAS_WCHAR__ */
  1000. return ppfs->num_data_args + 2;
  1001. }
  1002. #endif
  1003. /**********************************************************************/
  1004. #ifdef L_register_printf_function
  1005. #ifdef __STDIO_GLIBC_CUSTOM_PRINTF
  1006. int register_printf_function(int spec, printf_function handler,
  1007. printf_arginfo_function arginfo)
  1008. {
  1009. register char *r;
  1010. register char *p;
  1011. if (spec && (arginfo != NULL)) { /* TODO -- check if spec is valid char */
  1012. r = NULL;
  1013. p = _custom_printf_spec + MAX_USER_SPEC;
  1014. do {
  1015. --p;
  1016. if (!*p) {
  1017. r = p;
  1018. }
  1019. #ifdef __BCC__
  1020. else /* bcc generates less code with fall-through */
  1021. #endif
  1022. if (*p == spec) {
  1023. r = p;
  1024. p = _custom_printf_spec;
  1025. }
  1026. } while (p > _custom_printf_spec);
  1027. if (r) {
  1028. if (handler) {
  1029. *r = spec;
  1030. _custom_printf_handler[(int)(r - p)] = handler;
  1031. _custom_printf_arginfo[(int)(r - p)] = arginfo;
  1032. } else {
  1033. *r = 0;
  1034. }
  1035. return 0;
  1036. }
  1037. /* TODO -- if asked to unregister a non-existent spec, return what? */
  1038. }
  1039. return -1;
  1040. }
  1041. #endif
  1042. #endif
  1043. /**********************************************************************/
  1044. #ifdef L_vsnprintf
  1045. #ifdef __UCLIBC_MJN3_ONLY__
  1046. #warning WISHLIST: Implement vsnprintf for non-buffered and no custom stream case.
  1047. #endif /* __UCLIBC_MJN3_ONLY__ */
  1048. #ifdef __STDIO_BUFFERS
  1049. int vsnprintf(char *__restrict buf, size_t size,
  1050. const char * __restrict format, va_list arg)
  1051. {
  1052. FILE f;
  1053. int rv;
  1054. #ifdef __STDIO_GETC_MACRO
  1055. f.bufgetc =
  1056. #endif
  1057. f.bufpos = f.bufread = f.bufstart = buf;
  1058. if (size > SIZE_MAX - (size_t) buf) {
  1059. size = SIZE_MAX - (size_t) buf;
  1060. }
  1061. #ifdef __STDIO_PUTC_MACRO
  1062. f.bufputc =
  1063. #endif
  1064. f.bufend = buf + size;
  1065. #if 0 /* shouldn't be necessary */
  1066. /* #ifdef __STDIO_GLIBC_CUSTOM_STREAMS */
  1067. f.cookie = &(f.filedes);
  1068. f.gcs.read = 0;
  1069. f.gcs.write = 0;
  1070. f.gcs.seek = 0;
  1071. f.gcs.close = 0;
  1072. #endif
  1073. f.filedes = -2; /* for debugging */
  1074. f.modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);
  1075. #ifdef __STDIO_MBSTATE
  1076. __INIT_MBSTATE(&(f.state));
  1077. #endif /* __STDIO_MBSTATE */
  1078. #ifdef __STDIO_THREADSAFE
  1079. f.user_locking = 0;
  1080. __stdio_init_mutex(&f.lock);
  1081. #endif
  1082. rv = vfprintf(&f, format, arg);
  1083. if (size) {
  1084. if (f.bufpos == f.bufend) {
  1085. --f.bufpos;
  1086. }
  1087. *f.bufpos = 0;
  1088. }
  1089. return rv;
  1090. }
  1091. #else /* __STDIO_BUFFERS */
  1092. #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
  1093. typedef struct {
  1094. size_t pos;
  1095. size_t len;
  1096. unsigned char *buf;
  1097. FILE *fp;
  1098. } __snpf_cookie;
  1099. #define COOKIE ((__snpf_cookie *) cookie)
  1100. static ssize_t snpf_write(register void *cookie, const char *buf,
  1101. size_t bufsize)
  1102. {
  1103. size_t count;
  1104. register char *p;
  1105. /* Note: bufsize < SSIZE_MAX because of _stdio_WRITE. */
  1106. if (COOKIE->len > COOKIE->pos) {
  1107. count = COOKIE->len - COOKIE->pos - 1; /* Leave space for nul. */
  1108. if (count > bufsize) {
  1109. count = bufsize;
  1110. }
  1111. p = COOKIE->buf + COOKIE->pos;
  1112. while (count) {
  1113. *p++ = *buf++;
  1114. --count;
  1115. }
  1116. *p = 0;
  1117. }
  1118. COOKIE->pos += bufsize;
  1119. return bufsize;
  1120. }
  1121. #undef COOKIE
  1122. int vsnprintf(char *__restrict buf, size_t size,
  1123. const char * __restrict format, va_list arg)
  1124. {
  1125. FILE f;
  1126. __snpf_cookie cookie;
  1127. int rv;
  1128. cookie.buf = buf;
  1129. cookie.len = size;
  1130. cookie.pos = 0;
  1131. cookie.fp = &f;
  1132. f.cookie = &cookie;
  1133. f.gcs.write = snpf_write;
  1134. f.gcs.read = NULL;
  1135. f.gcs.seek = NULL;
  1136. f.gcs.close = NULL;
  1137. f.filedes = -1; /* For debugging. */
  1138. f.modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);
  1139. #ifdef __STDIO_MBSTATE
  1140. __INIT_MBSTATE(&(f.state));
  1141. #endif /* __STDIO_MBSTATE */
  1142. #ifdef __STDIO_THREADSAFE
  1143. f.user_locking = 0;
  1144. __stdio_init_mutex(&f.lock);
  1145. #endif
  1146. rv = vfprintf(&f, format, arg);
  1147. return rv;
  1148. }
  1149. #else /* __STDIO_GLIBC_CUSTOM_STREAMS */
  1150. #warning Skipping vsnprintf since no buffering and no custom streams!
  1151. #endif /* __STDIO_GLIBC_CUSTOM_STREAMS */
  1152. #endif /* __STDIO_BUFFERS */
  1153. #endif
  1154. /**********************************************************************/
  1155. #ifdef L_vdprintf
  1156. int vdprintf(int filedes, const char * __restrict format, va_list arg)
  1157. {
  1158. FILE f;
  1159. int rv;
  1160. #ifdef __STDIO_BUFFERS
  1161. char buf[64]; /* TODO: provide _optional_ buffering? */
  1162. #ifdef __STDIO_GETC_MACRO
  1163. f.bufgetc =
  1164. #endif
  1165. f.bufpos = f.bufread = f.bufstart = buf;
  1166. #ifdef __STDIO_PUTC_MACRO
  1167. f.bufputc =
  1168. #endif
  1169. f.bufend = buf + sizeof(buf);
  1170. #endif /* __STDIO_BUFFERS */
  1171. #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
  1172. f.cookie = &(f.filedes);
  1173. f.gcs.read = _cs_read;
  1174. f.gcs.write = _cs_write;
  1175. f.gcs.seek = 0; /* The internal seek func handles normals. */
  1176. f.gcs.close = _cs_close;
  1177. #endif
  1178. f.filedes = filedes;
  1179. f.modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);
  1180. #ifdef __STDIO_MBSTATE
  1181. __INIT_MBSTATE(&(f.state));
  1182. #endif /* __STDIO_MBSTATE */
  1183. #ifdef __STDIO_THREADSAFE
  1184. f.user_locking = 0;
  1185. __stdio_init_mutex(&f.lock);
  1186. #endif
  1187. rv = vfprintf(&f, format, arg);
  1188. return fflush(&f) ? -1 : rv;
  1189. }
  1190. #endif
  1191. /**********************************************************************/
  1192. #ifdef L_vasprintf
  1193. #if !defined(__STDIO_BUFFERS) && !defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  1194. #warning Skipping vasprintf since no buffering and no custom streams!
  1195. #else
  1196. int vasprintf(char **__restrict buf, const char * __restrict format,
  1197. va_list arg)
  1198. {
  1199. /* TODO -- change the default implementation? */
  1200. #ifndef __STDIO_GLIBC_CUSTOM_STREAMS
  1201. /* This implementation actually calls the printf machinery twice, but only
  1202. * only does one malloc. This can be a problem though when custom printf
  1203. * specs or the %m specifier are involved because the results of the
  1204. * second call might be different from the first. */
  1205. va_list arg2;
  1206. int rv;
  1207. va_copy(arg2, arg);
  1208. rv = vsnprintf(NULL, 0, format, arg2);
  1209. va_end(arg2);
  1210. return (((rv >= 0) && ((*buf = malloc(++rv)) != NULL))
  1211. ? vsnprintf(*buf, rv, format, arg)
  1212. : -1);
  1213. #else
  1214. FILE *f;
  1215. size_t size;
  1216. int rv;
  1217. /* TODO - do the memstream stuff inline here to avoid fclose and the openlist? */
  1218. if ((f = open_memstream(buf, &size)) == NULL) {
  1219. return -1;
  1220. }
  1221. rv = vfprintf(f, format, arg);
  1222. fclose(f);
  1223. if (rv < 0) {
  1224. free(*buf);
  1225. *buf = NULL;
  1226. return -1;
  1227. }
  1228. return rv;
  1229. #endif
  1230. }
  1231. #endif
  1232. #endif
  1233. /**********************************************************************/
  1234. #ifdef L_vprintf
  1235. int vprintf(const char * __restrict format, va_list arg)
  1236. {
  1237. return vfprintf(stdout, format, arg);
  1238. }
  1239. #endif
  1240. /**********************************************************************/
  1241. #ifdef L_vsprintf
  1242. #if !defined(__STDIO_BUFFERS) && !defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  1243. #warning Skipping vsprintf since no buffering and no custom streams!
  1244. #else
  1245. int vsprintf(char *__restrict buf, const char * __restrict format,
  1246. va_list arg)
  1247. {
  1248. return vsnprintf(buf, SIZE_MAX, format, arg);
  1249. }
  1250. #endif
  1251. #endif
  1252. /**********************************************************************/
  1253. #ifdef L_fprintf
  1254. int fprintf(FILE * __restrict stream, const char * __restrict format, ...)
  1255. {
  1256. va_list arg;
  1257. int rv;
  1258. va_start(arg, format);
  1259. rv = vfprintf(stream, format, arg);
  1260. va_end(arg);
  1261. return rv;
  1262. }
  1263. #endif
  1264. /**********************************************************************/
  1265. #ifdef L_snprintf
  1266. #if !defined(__STDIO_BUFFERS) && !defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  1267. #warning Skipping snprintf since no buffering and no custom streams!
  1268. #else
  1269. int snprintf(char *__restrict buf, size_t size,
  1270. const char * __restrict format, ...)
  1271. {
  1272. va_list arg;
  1273. int rv;
  1274. va_start(arg, format);
  1275. rv = vsnprintf(buf, size, format, arg);
  1276. va_end(arg);
  1277. return rv;
  1278. }
  1279. #endif
  1280. #endif
  1281. /**********************************************************************/
  1282. #ifdef L_dprintf
  1283. int dprintf(int filedes, const char * __restrict format, ...)
  1284. {
  1285. va_list arg;
  1286. int rv;
  1287. va_start(arg, format);
  1288. rv = vdprintf(filedes, format, arg);
  1289. va_end(arg);
  1290. return rv;
  1291. }
  1292. #endif
  1293. /**********************************************************************/
  1294. #ifdef L_asprintf
  1295. #if !defined(__STDIO_BUFFERS) && !defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  1296. #warning Skipping asprintf and __asprintf since no buffering and no custom streams!
  1297. #else
  1298. weak_alias(__asprintf,asprintf)
  1299. int __asprintf(char **__restrict buf, const char * __restrict format, ...)
  1300. {
  1301. va_list arg;
  1302. int rv;
  1303. va_start(arg, format);
  1304. rv = vasprintf(buf, format, arg);
  1305. va_end(arg);
  1306. return rv;
  1307. }
  1308. #endif
  1309. #endif
  1310. /**********************************************************************/
  1311. #ifdef L_printf
  1312. int printf(const char * __restrict format, ...)
  1313. {
  1314. va_list arg;
  1315. int rv;
  1316. va_start(arg, format);
  1317. rv = vfprintf(stdout, format, arg);
  1318. va_end(arg);
  1319. return rv;
  1320. }
  1321. #endif
  1322. /**********************************************************************/
  1323. #ifdef L_sprintf
  1324. #if !defined(__STDIO_BUFFERS) && !defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  1325. #warning Skipping sprintf since no buffering and no custom streams!
  1326. #else
  1327. int sprintf(char *__restrict buf, const char * __restrict format, ...)
  1328. {
  1329. va_list arg;
  1330. int rv;
  1331. va_start(arg, format);
  1332. rv = vsnprintf(buf, SIZE_MAX, format, arg);
  1333. va_end(arg);
  1334. return rv;
  1335. }
  1336. #endif
  1337. #endif
  1338. /**********************************************************************/
  1339. #ifdef L_vswprintf
  1340. #ifdef __STDIO_BUFFERS
  1341. int vswprintf(wchar_t *__restrict buf, size_t size,
  1342. const wchar_t * __restrict format, va_list arg)
  1343. {
  1344. FILE f;
  1345. int rv;
  1346. #ifdef __STDIO_GETC_MACRO
  1347. f.bufgetc =
  1348. #endif
  1349. f.bufpos = f.bufread = f.bufstart = (char *) buf;
  1350. /* if (size > SIZE_MAX - (size_t) buf) { */
  1351. /* size = SIZE_MAX - (size_t) buf; */
  1352. /* } */
  1353. #ifdef __STDIO_PUTC_MACRO
  1354. f.bufputc =
  1355. #endif
  1356. f.bufend = (char *)(buf + size);
  1357. #if 0 /* shouldn't be necessary */
  1358. /* #ifdef __STDIO_GLIBC_CUSTOM_STREAMS */
  1359. f.cookie = &(f.filedes);
  1360. f.gcs.read = 0;
  1361. f.gcs.write = 0;
  1362. f.gcs.seek = 0;
  1363. f.gcs.close = 0;
  1364. #endif
  1365. f.filedes = -3; /* for debugging */
  1366. f.modeflags = (__FLAG_WIDE|__FLAG_WRITEONLY|__FLAG_WRITING);
  1367. #ifdef __STDIO_MBSTATE
  1368. __INIT_MBSTATE(&(f.state));
  1369. #endif /* __STDIO_MBSTATE */
  1370. #ifdef __STDIO_THREADSAFE
  1371. f.user_locking = 0;
  1372. __stdio_init_mutex(&f.lock);
  1373. #endif
  1374. rv = vfwprintf(&f, format, arg);
  1375. /* NOTE: Return behaviour differs from snprintf... */
  1376. if (f.bufpos == f.bufend) {
  1377. rv = -1;
  1378. if (size) {
  1379. f.bufpos = (char *)(((wchar_t *) f.bufpos) - 1);
  1380. }
  1381. }
  1382. if (size) {
  1383. *((wchar_t *) f.bufpos) = 0;
  1384. }
  1385. return rv;
  1386. }
  1387. #else /* __STDIO_BUFFERS */
  1388. #warning Skipping vswprintf since no buffering!
  1389. #endif /* __STDIO_BUFFERS */
  1390. #endif
  1391. /**********************************************************************/
  1392. #ifdef L_swprintf
  1393. #ifdef __STDIO_BUFFERS
  1394. int swprintf(wchar_t *__restrict buf, size_t size,
  1395. const wchar_t * __restrict format, ...)
  1396. {
  1397. va_list arg;
  1398. int rv;
  1399. va_start(arg, format);
  1400. rv = vswprintf(buf, size, format, arg);
  1401. va_end(arg);
  1402. return rv;
  1403. }
  1404. #else /* __STDIO_BUFFERS */
  1405. #warning Skipping vsWprintf since no buffering!
  1406. #endif /* __STDIO_BUFFERS */
  1407. #endif
  1408. /**********************************************************************/
  1409. #ifdef L_fwprintf
  1410. int fwprintf(FILE * __restrict stream, const wchar_t * __restrict format, ...)
  1411. {
  1412. va_list arg;
  1413. int rv;
  1414. va_start(arg, format);
  1415. rv = vfwprintf(stream, format, arg);
  1416. va_end(arg);
  1417. return rv;
  1418. }
  1419. #endif
  1420. /**********************************************************************/
  1421. #ifdef L_vwprintf
  1422. int vwprintf(const wchar_t * __restrict format, va_list arg)
  1423. {
  1424. return vfwprintf(stdout, format, arg);
  1425. }
  1426. #endif
  1427. /**********************************************************************/
  1428. #ifdef L_wprintf
  1429. int wprintf(const wchar_t * __restrict format, ...)
  1430. {
  1431. va_list arg;
  1432. int rv;
  1433. va_start(arg, format);
  1434. rv = vfwprintf(stdout, format, arg);
  1435. va_end(arg);
  1436. return rv;
  1437. }
  1438. #endif
  1439. /**********************************************************************/
  1440. #ifdef L__fpmaxtostr
  1441. /* Copyright (C) 2000, 2001, 2003 Manuel Novoa III
  1442. *
  1443. * Function:
  1444. *
  1445. * size_t _fpmaxtostr(FILE * fp, __fpmax_t x, struct printf_info *info,
  1446. * __fp_outfunc_t fp_outfunc);
  1447. *
  1448. * This is derived from the old _dtostr, whic I wrote for uClibc to provide
  1449. * floating point support for the printf functions. It handles +/- infinity,
  1450. * nan, and signed 0 assuming you have ieee arithmetic. It also now handles
  1451. * digit grouping (for the uClibc supported locales) and hexadecimal float
  1452. * notation. Finally, via the fp_outfunc parameter, it now supports wide
  1453. * output.
  1454. *
  1455. * Notes:
  1456. *
  1457. * At most DECIMAL_DIG significant digits are kept. Any trailing digits
  1458. * are treated as 0 as they are really just the results of rounding noise
  1459. * anyway. If you want to do better, use an arbitary precision arithmetic
  1460. * package. ;-)
  1461. *
  1462. * It should also be fairly portable, as no assumptions are made about the
  1463. * bit-layout of doubles. Of course, that does make it less efficient than
  1464. * it could be.
  1465. *
  1466. */
  1467. /*****************************************************************************/
  1468. /* Don't change anything that follows unless you know what you're doing. */
  1469. /*****************************************************************************/
  1470. /* Fairly portable nan check. Bitwise for i386 generated larger code.
  1471. * If you have a better version, comment this out.
  1472. */
  1473. #define isnan(x) ((x) != (x))
  1474. /* Without seminumerical functions to examine the sign bit, this is
  1475. * about the best we can do to test for '-0'.
  1476. */
  1477. #define zeroisnegative(x) ((1./(x)) < 0)
  1478. /*****************************************************************************/
  1479. /* Don't change anything that follows peroid!!! ;-) */
  1480. /*****************************************************************************/
  1481. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  1482. #if FLT_RADIX != 2
  1483. #error FLT_RADIX != 2 is not currently supported
  1484. #endif
  1485. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  1486. #define NUM_HEX_DIGITS ((FPMAX_MANT_DIG + 3)/ 4)
  1487. /* WARNING: Adjust _fp_out_wide() below if this changes! */
  1488. /* With 32 bit ints, we can get 9 decimal digits per block. */
  1489. #define DIGITS_PER_BLOCK 9
  1490. #define HEX_DIGITS_PER_BLOCK 8
  1491. /* Maximum number of subcases to output double is...
  1492. * 0 - sign
  1493. * 1 - padding and initial digit
  1494. * 2 - digits left of the radix
  1495. * 3 - 0s left of the radix or radix
  1496. * 4 - radix or digits right of the radix
  1497. * 5 - 0s right of the radix
  1498. * 6 - exponent
  1499. * 7 - trailing space padding
  1500. * although not all cases may occur.
  1501. */
  1502. #define MAX_CALLS 8
  1503. /*****************************************************************************/
  1504. #define NUM_DIGIT_BLOCKS ((DECIMAL_DIG+DIGITS_PER_BLOCK-1)/DIGITS_PER_BLOCK)
  1505. #define NUM_HEX_DIGIT_BLOCKS \
  1506. ((NUM_HEX_DIGITS+HEX_DIGITS_PER_BLOCK-1)/HEX_DIGITS_PER_BLOCK)
  1507. /* WARNING: Adjust _fp_out_wide() below if this changes! */
  1508. /* extra space for '-', '.', 'e+###', and nul */
  1509. #define BUF_SIZE ( 3 + NUM_DIGIT_BLOCKS * DIGITS_PER_BLOCK )
  1510. /*****************************************************************************/
  1511. static const char fmt[] = "inf\0INF\0nan\0NAN\0.\0,";
  1512. #define INF_OFFSET 0 /* must be 1st */
  1513. #define NAN_OFFSET 8 /* must be 2nd.. see hex sign handling */
  1514. #define DECPT_OFFSET 16
  1515. #define THOUSEP_OFFSET 18
  1516. #define EMPTY_STRING_OFFSET 3
  1517. /*****************************************************************************/
  1518. #if FPMAX_MAX_10_EXP < -FPMAX_MIN_10_EXP
  1519. #error scaling code can not handle FPMAX_MAX_10_EXP < -FPMAX_MIN_10_EXP
  1520. #endif
  1521. static const __fpmax_t exp10_table[] =
  1522. {
  1523. 1e1L, 1e2L, 1e4L, 1e8L, 1e16L, 1e32L, /* floats */
  1524. #if FPMAX_MAX_10_EXP < 32
  1525. #error unsupported FPMAX_MAX_10_EXP (< 32). ANSI/ISO C requires >= 37.
  1526. #endif
  1527. #if FPMAX_MAX_10_EXP >= 64
  1528. 1e64L,
  1529. #endif
  1530. #if FPMAX_MAX_10_EXP >= 128
  1531. 1e128L,
  1532. #endif
  1533. #if FPMAX_MAX_10_EXP >= 256
  1534. 1e256L,
  1535. #endif
  1536. #if FPMAX_MAX_10_EXP >= 512
  1537. 1e512L,
  1538. #endif
  1539. #if FPMAX_MAX_10_EXP >= 1024
  1540. 1e1024L,
  1541. #endif
  1542. #if FPMAX_MAX_10_EXP >= 2048
  1543. 1e2048L,
  1544. #endif
  1545. #if FPMAX_MAX_10_EXP >= 4096
  1546. 1e4096L
  1547. #endif
  1548. #if FPMAX_MAX_10_EXP >= 8192
  1549. #error unsupported FPMAX_MAX_10_EXP. please increase table
  1550. #endif
  1551. };
  1552. #define EXP10_TABLE_SIZE (sizeof(exp10_table)/sizeof(exp10_table[0]))
  1553. #define EXP10_TABLE_MAX (1U<<(EXP10_TABLE_SIZE-1))
  1554. /*****************************************************************************/
  1555. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  1556. #if FLT_RADIX != 2
  1557. #error FLT_RADIX != 2 is not currently supported
  1558. #endif
  1559. #if FPMAX_MAX_EXP < -FPMAX_MIN_EXP
  1560. #error scaling code can not handle FPMAX_MAX_EXP < -FPMAX_MIN_EXP
  1561. #endif
  1562. static const __fpmax_t exp16_table[] = {
  1563. 0x1.0p4L, 0x1.0p8L, 0x1.0p16L, 0x1.0p32L, 0x1.0p64L,
  1564. #if FPMAX_MAX_EXP >= 128
  1565. 0x1.0p128L,
  1566. #endif
  1567. #if FPMAX_MAX_EXP >= 256
  1568. 0x1.0p256L,
  1569. #endif
  1570. #if FPMAX_MAX_EXP >= 512
  1571. 0x1.0p512L,
  1572. #endif
  1573. #if FPMAX_MAX_EXP >= 1024
  1574. 0x1.0p1024L,
  1575. #endif
  1576. #if FPMAX_MAX_EXP >= 2048
  1577. 0x1.0p2048L,
  1578. #endif
  1579. #if FPMAX_MAX_EXP >= 4096
  1580. 0x1.0p4096L,
  1581. #endif
  1582. #if FPMAX_MAX_EXP >= 8192
  1583. 0x1.0p8192L,
  1584. #endif
  1585. #if FPMAX_MAX_EXP >= 16384
  1586. 0x1.0p16384L
  1587. #endif
  1588. #if FPMAX_MAX_EXP >= 32768
  1589. #error unsupported FPMAX_MAX_EXP. please increase table
  1590. #endif
  1591. };
  1592. #define EXP16_TABLE_SIZE (sizeof(exp16_table)/sizeof(exp16_table[0]))
  1593. #define EXP16_TABLE_MAX (1U<<(EXP16_TABLE_SIZE-1))
  1594. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  1595. /*****************************************************************************/
  1596. #define FPO_ZERO_PAD (0x80 | '0')
  1597. #define FPO_STR_WIDTH (0x80 | ' ');
  1598. #define FPO_STR_PREC 'p'
  1599. size_t _fpmaxtostr(FILE * fp, __fpmax_t x, struct printf_info *info,
  1600. __fp_outfunc_t fp_outfunc)
  1601. {
  1602. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  1603. __fpmax_t lower_bnd;
  1604. __fpmax_t upper_bnd = 1e9;
  1605. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  1606. uint_fast32_t digit_block;
  1607. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  1608. uint_fast32_t base = 10;
  1609. const __fpmax_t *power_table;
  1610. int dpb = DIGITS_PER_BLOCK;
  1611. int ndb = NUM_DIGIT_BLOCKS;
  1612. int nd = DECIMAL_DIG;
  1613. int sufficient_precision = 0;
  1614. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  1615. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  1616. int num_groups = 0;
  1617. int initial_group; /* This does not need to be initialized. */
  1618. int tslen; /* This does not need to be initialized. */
  1619. int nblk2; /* This does not need to be initialized. */
  1620. const char *ts; /* This does not need to be initialized. */
  1621. #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
  1622. int i, j;
  1623. int round, o_exp;
  1624. int exp, exp_neg;
  1625. int width, preci;
  1626. int cnt;
  1627. char *s;
  1628. char *e;
  1629. intptr_t pc_fwi[3*MAX_CALLS];
  1630. intptr_t *ppc;
  1631. intptr_t *ppc_last;
  1632. #ifdef __UCLIBC_MJN3_ONLY__
  1633. #warning TODO: The size of exp_buf[] should really be determined by the float constants.
  1634. #endif /* __UCLIBC_MJN3_ONLY__ */
  1635. char exp_buf[16];
  1636. char buf[BUF_SIZE];
  1637. char sign_str[6]; /* Last 2 are for 1st digit + nul. */
  1638. char o_mode;
  1639. char mode;
  1640. width = info->width;
  1641. preci = info->prec;
  1642. mode = info->spec;
  1643. *exp_buf = 'e';
  1644. if ((mode|0x20) == 'a') {
  1645. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  1646. *exp_buf = 'p';
  1647. if (preci < 0) {
  1648. preci = NUM_HEX_DIGITS;
  1649. sufficient_precision = 1;
  1650. }
  1651. #else
  1652. mode += ('g' - 'a');
  1653. #endif
  1654. }
  1655. if (preci < 0) {
  1656. preci = 6;
  1657. }
  1658. *sign_str = '\0';
  1659. if (PRINT_INFO_FLAG_VAL(info,showsign)) {
  1660. *sign_str = '+';
  1661. } else if (PRINT_INFO_FLAG_VAL(info,space)) {
  1662. *sign_str = ' ';
  1663. }
  1664. *(sign_str+1) = 0;
  1665. pc_fwi[5] = INF_OFFSET;
  1666. if (isnan(x)) { /* First, check for nan. */
  1667. pc_fwi[5] = NAN_OFFSET;
  1668. goto INF_NAN;
  1669. }
  1670. if (x == 0) { /* Handle 0 now to avoid false positive. */
  1671. #if 1
  1672. if (zeroisnegative(x)) { /* Handle 'signed' zero. */
  1673. *sign_str = '-';
  1674. }
  1675. #endif
  1676. exp = -1;
  1677. goto GENERATE_DIGITS;
  1678. }
  1679. if (x < 0) { /* Convert negatives to positives. */
  1680. *sign_str = '-';
  1681. x = -x;
  1682. }
  1683. if (__FPMAX_ZERO_OR_INF_CHECK(x)) { /* Inf since zero handled above. */
  1684. INF_NAN:
  1685. info->pad = ' ';
  1686. ppc = pc_fwi + 6;
  1687. pc_fwi[3] = FPO_STR_PREC;
  1688. pc_fwi[4] = 3;
  1689. if (mode < 'a') {
  1690. pc_fwi[5] += 4;
  1691. }
  1692. pc_fwi[5] = (intptr_t)(fmt + pc_fwi[5]);
  1693. goto EXIT_SPECIAL;
  1694. }
  1695. #ifdef __UCLIBC_MJN3_ONLY__
  1696. #warning TODO: Clean up defines when hexadecimal float notation is unsupported.
  1697. #endif /* __UCLIBC_MJN3_ONLY__ */
  1698. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  1699. if ((mode|0x20) == 'a') {
  1700. lower_bnd = 0x1.0p31L;
  1701. upper_bnd = 0x1.0p32L;
  1702. power_table = exp16_table;
  1703. exp = HEX_DIGITS_PER_BLOCK - 1;
  1704. i = EXP16_TABLE_SIZE;
  1705. j = EXP16_TABLE_MAX;
  1706. dpb = HEX_DIGITS_PER_BLOCK;
  1707. ndb = NUM_HEX_DIGIT_BLOCKS;
  1708. nd = NUM_HEX_DIGITS;
  1709. base = 16;
  1710. } else {
  1711. lower_bnd = 1e8;
  1712. /* upper_bnd = 1e9; */
  1713. power_table = exp10_table;
  1714. exp = DIGITS_PER_BLOCK - 1;
  1715. i = EXP10_TABLE_SIZE;
  1716. j = EXP10_TABLE_MAX;
  1717. /* dpb = DIGITS_PER_BLOCK; */
  1718. /* ndb = NUM_DIGIT_BLOCKS; */
  1719. /* base = 10; */
  1720. }
  1721. #else /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  1722. #define lower_bnd 1e8
  1723. #define upper_bnd 1e9
  1724. #define power_table exp10_table
  1725. #define dpb DIGITS_PER_BLOCK
  1726. #define base 10
  1727. #define ndb NUM_DIGIT_BLOCKS
  1728. #define nd DECIMAL_DIG
  1729. exp = DIGITS_PER_BLOCK - 1;
  1730. i = EXP10_TABLE_SIZE;
  1731. j = EXP10_TABLE_MAX;
  1732. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  1733. exp_neg = 0;
  1734. if (x < lower_bnd) { /* Do we need to scale up or down? */
  1735. exp_neg = 1;
  1736. }
  1737. do {
  1738. --i;
  1739. if (exp_neg) {
  1740. if (x * power_table[i] < upper_bnd) {
  1741. x *= power_table[i];
  1742. exp -= j;
  1743. }
  1744. } else {
  1745. if (x / power_table[i] >= lower_bnd) {
  1746. x /= power_table[i];
  1747. exp += j;
  1748. }
  1749. }
  1750. j >>= 1;
  1751. } while (i);
  1752. if (x >= upper_bnd) { /* Handle bad rounding case. */
  1753. x /= power_table[0];
  1754. ++exp;
  1755. }
  1756. assert(x < upper_bnd);
  1757. GENERATE_DIGITS:
  1758. s = buf + 2; /* Leave space for '\0' and '0'. */
  1759. i = 0;
  1760. do {
  1761. digit_block = (uint_fast32_t) x;
  1762. assert(digit_block < upper_bnd);
  1763. #ifdef __UCLIBC_MJN3_ONLY__
  1764. #warning CONSIDER: Can rounding be a problem?
  1765. #endif /* __UCLIBC_MJN3_ONLY__ */
  1766. x = (x - digit_block) * upper_bnd;
  1767. s += dpb;
  1768. j = 0;
  1769. do {
  1770. s[- ++j] = '0' + (digit_block % base);
  1771. digit_block /= base;
  1772. } while (j < dpb);
  1773. } while (++i < ndb);
  1774. /*************************************************************************/
  1775. if (mode < 'a') {
  1776. *exp_buf -= ('a' - 'A'); /* e->E and p->P */
  1777. mode += ('a' - 'A');
  1778. }
  1779. o_mode = mode;
  1780. if ((mode == 'g') && (preci > 0)){
  1781. --preci;
  1782. }
  1783. round = preci;
  1784. if (mode == 'f') {
  1785. round += exp;
  1786. if (round < -1) {
  1787. memset(buf, '0', DECIMAL_DIG); /* OK, since 'f' -> decimal case. */
  1788. exp = -1;
  1789. round = -1;
  1790. }
  1791. }
  1792. s = buf;
  1793. *s++ = 0; /* Terminator for rounding and 0-triming. */
  1794. *s = '0'; /* Space to round. */
  1795. i = 0;
  1796. e = s + nd + 1;
  1797. if (round < nd) {
  1798. e = s + round + 2;
  1799. if (*e >= '0' + (base/2)) { /* NOTE: We always round away from 0! */
  1800. i = 1;
  1801. }
  1802. }
  1803. do { /* Handle rounding and trim trailing 0s. */
  1804. *--e += i; /* Add the carry. */
  1805. } while ((*e == '0') || (*e > '0' - 1 + base));
  1806. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  1807. if ((mode|0x20) == 'a') {
  1808. char *q;
  1809. for (q = e ; *q ; --q) {
  1810. if (*q > '9') {
  1811. *q += (*exp_buf - ('p' - 'a') - '9' - 1);
  1812. }
  1813. }
  1814. if (e > s) {
  1815. exp *= 4; /* Change from base 16 to base 2. */
  1816. }
  1817. }
  1818. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  1819. o_exp = exp;
  1820. if (e <= s) { /* We carried into an extra digit. */
  1821. ++o_exp;
  1822. e = s; /* Needed if all 0s. */
  1823. } else {
  1824. ++s;
  1825. }
  1826. *++e = 0; /* Terminating nul char. */
  1827. if ((mode == 'g') && ((o_exp >= -4) && (o_exp <= round))) {
  1828. mode = 'f';
  1829. preci = round - o_exp;
  1830. }
  1831. exp = o_exp;
  1832. if (mode != 'f') {
  1833. o_exp = 0;
  1834. }
  1835. if (o_exp < 0) { /* Exponent is < 0, so */
  1836. *--s = '0'; /* fake the first 0 digit. */
  1837. }
  1838. pc_fwi[3] = FPO_ZERO_PAD;
  1839. pc_fwi[4] = 1;
  1840. pc_fwi[5] = (intptr_t)(sign_str + 4);
  1841. sign_str[4] = *s++;
  1842. sign_str[5] = 0;
  1843. ppc = pc_fwi + 6;
  1844. i = e - s; /* Total digits is 'i'. */
  1845. if (o_exp >= 0) {
  1846. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  1847. const char *p;
  1848. if (PRINT_INFO_FLAG_VAL(info,group)
  1849. && *(p = __UCLIBC_CURLOCALE_DATA.grouping)
  1850. ) {
  1851. int nblk1;
  1852. nblk2 = nblk1 = *p;
  1853. if (*++p) {
  1854. nblk2 = *p;
  1855. assert(!*++p);
  1856. }
  1857. if (o_exp >= nblk1) {
  1858. num_groups = (o_exp - nblk1) / nblk2 + 1;
  1859. initial_group = (o_exp - nblk1) % nblk2;
  1860. #ifdef __UCLIBC_HAS_WCHAR__
  1861. if (PRINT_INFO_FLAG_VAL(info,wide)) {
  1862. /* _fp_out_wide() will fix this up. */
  1863. ts = fmt + THOUSEP_OFFSET;
  1864. tslen = 1;
  1865. } else {
  1866. #endif /* __UCLIBC_HAS_WCHAR__ */
  1867. ts = __UCLIBC_CURLOCALE_DATA.thousands_sep;
  1868. tslen = __UCLIBC_CURLOCALE_DATA.thousands_sep_len;
  1869. #ifdef __UCLIBC_HAS_WCHAR__
  1870. }
  1871. #endif /* __UCLIBC_HAS_WCHAR__ */
  1872. width -= num_groups * tslen;
  1873. }
  1874. }
  1875. #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
  1876. ppc[0] = FPO_STR_PREC;
  1877. ppc[2] = (intptr_t)(s);
  1878. if (o_exp >= i) { /* all digit(s) left of decimal */
  1879. ppc[1] = i;
  1880. ppc += 3;
  1881. o_exp -= i;
  1882. i = 0;
  1883. if (o_exp>0) { /* have 0s left of decimal */
  1884. ppc[0] = FPO_ZERO_PAD;
  1885. ppc[1] = o_exp;
  1886. ppc[2] = (intptr_t)(fmt + EMPTY_STRING_OFFSET);
  1887. ppc += 3;
  1888. }
  1889. } else if (o_exp > 0) { /* decimal between digits */
  1890. ppc[1] = o_exp;
  1891. ppc += 3;
  1892. s += o_exp;
  1893. i -= o_exp;
  1894. }
  1895. o_exp = -1;
  1896. }
  1897. if (PRINT_INFO_FLAG_VAL(info,alt)
  1898. || (i)
  1899. || ((o_mode != 'g')
  1900. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  1901. && (o_mode != 'a')
  1902. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  1903. && (preci > 0))
  1904. ) {
  1905. ppc[0] = FPO_STR_PREC;
  1906. #ifdef __LOCALE_C_ONLY
  1907. ppc[1] = 1;
  1908. ppc[2] = (intptr_t)(fmt + DECPT_OFFSET);
  1909. #else /* __LOCALE_C_ONLY */
  1910. #ifdef __UCLIBC_HAS_WCHAR__
  1911. if (PRINT_INFO_FLAG_VAL(info,wide)) {
  1912. /* _fp_out_wide() will fix this up. */
  1913. ppc[1] = 1;
  1914. ppc[2] = (intptr_t)(fmt + DECPT_OFFSET);
  1915. } else {
  1916. #endif /* __UCLIBC_HAS_WCHAR__ */
  1917. ppc[1] = __UCLIBC_CURLOCALE_DATA.decimal_point_len;
  1918. ppc[2] = (intptr_t)(__UCLIBC_CURLOCALE_DATA.decimal_point);
  1919. #ifdef __UCLIBC_HAS_WCHAR__
  1920. }
  1921. #endif /* __UCLIBC_HAS_WCHAR__ */
  1922. #endif /* __LOCALE_C_ONLY */
  1923. ppc += 3;
  1924. }
  1925. if (++o_exp < 0) { /* Have 0s right of decimal. */
  1926. ppc[0] = FPO_ZERO_PAD;
  1927. ppc[1] = -o_exp;
  1928. ppc[2] = (intptr_t)(fmt + EMPTY_STRING_OFFSET);
  1929. ppc += 3;
  1930. }
  1931. if (i) { /* Have digit(s) right of decimal. */
  1932. ppc[0] = FPO_STR_PREC;
  1933. ppc[1] = i;
  1934. ppc[2] = (intptr_t)(s);
  1935. ppc += 3;
  1936. }
  1937. if (((o_mode != 'g') || PRINT_INFO_FLAG_VAL(info,alt))
  1938. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  1939. && !sufficient_precision
  1940. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  1941. ) {
  1942. i -= o_exp;
  1943. if (i < preci) { /* Have 0s right of digits. */
  1944. i = preci - i;
  1945. ppc[0] = FPO_ZERO_PAD;
  1946. ppc[1] = i;
  1947. ppc[2] = (intptr_t)(fmt + EMPTY_STRING_OFFSET);
  1948. ppc += 3;
  1949. }
  1950. }
  1951. /* Build exponent string. */
  1952. if (mode != 'f') {
  1953. char *p = exp_buf + sizeof(exp_buf);
  1954. char exp_char = *exp_buf;
  1955. char exp_sign = '+';
  1956. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  1957. int min_exp_dig_plus_2 = ((o_mode != 'a') ? (2+2) : (2+1));
  1958. #else /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  1959. #define min_exp_dig_plus_2 (2+2)
  1960. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  1961. if (exp < 0) {
  1962. exp_sign = '-';
  1963. exp = -exp;
  1964. }
  1965. *--p = 0; /* nul-terminate */
  1966. j = 2; /* Count exp_char and exp_sign. */
  1967. do {
  1968. *--p = '0' + (exp % 10);
  1969. exp /= 10;
  1970. } while ((++j < min_exp_dig_plus_2) || exp); /* char+sign+mindigits */
  1971. *--p = exp_sign;
  1972. *--p = exp_char;
  1973. ppc[0] = FPO_STR_PREC;
  1974. ppc[1] = j;
  1975. ppc[2] = (intptr_t)(p);
  1976. ppc += 3;
  1977. }
  1978. EXIT_SPECIAL:
  1979. ppc_last = ppc;
  1980. ppc = pc_fwi + 4; /* Need width fields starting with second. */
  1981. do {
  1982. width -= *ppc;
  1983. ppc += 3;
  1984. } while (ppc < ppc_last);
  1985. ppc = pc_fwi;
  1986. ppc[0] = FPO_STR_WIDTH;
  1987. ppc[1] = i = ((*sign_str) != 0);
  1988. ppc[2] = (intptr_t) sign_str;
  1989. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  1990. if (((mode|0x20) == 'a') && (pc_fwi[3] >= 16)) { /* Hex sign handling. */
  1991. /* Hex and not inf or nan, so prefix with 0x. */
  1992. char *h = sign_str + i;
  1993. *h = '0';
  1994. *++h = 'x' - 'p' + *exp_buf;
  1995. *++h = 0;
  1996. ppc[1] = (i += 2);
  1997. }
  1998. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  1999. if ((width -= i) > 0) {
  2000. if (PRINT_INFO_FLAG_VAL(info,left)) { /* Left-justified. */
  2001. ppc_last[0] = FPO_STR_WIDTH;
  2002. ppc_last[1] = width;
  2003. ppc_last[2] = (intptr_t)(fmt + EMPTY_STRING_OFFSET);
  2004. ppc_last += 3;
  2005. } else if (info->pad == '0') { /* 0 padding */
  2006. ppc[4] += width; /* Pad second field. */
  2007. } else {
  2008. ppc[1] += width; /* Pad first (sign) field. */
  2009. }
  2010. }
  2011. cnt = 0;
  2012. do {
  2013. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  2014. if ((ppc == pc_fwi + 6) && num_groups) {
  2015. const char *gp = (const char *) ppc[2];
  2016. int len = ppc[1];
  2017. int blk = initial_group;
  2018. cnt += num_groups * tslen; /* Adjust count now for sep chars. */
  2019. /* printf("\n"); */
  2020. do {
  2021. if (!blk) { /* Initial group could be 0 digits long! */
  2022. blk = nblk2;
  2023. } else if (len >= blk) { /* Enough digits for a group. */
  2024. /* printf("norm: len=%d blk=%d \"%.*s\"\n", len, blk, blk, gp); */
  2025. fp_outfunc(fp, *ppc, blk, (intptr_t) gp);
  2026. assert(gp);
  2027. if (*gp) {
  2028. gp += blk;
  2029. }
  2030. len -= blk;
  2031. } else { /* Transition to 0s. */
  2032. /* printf("trans: len=%d blk=%d \"%.*s\"\n", len, blk, len, gp); */
  2033. if (len) {
  2034. /* printf("len\n"); */
  2035. fp_outfunc(fp, *ppc, len, (intptr_t) gp);
  2036. gp += len;
  2037. }
  2038. if (ppc[3] == FPO_ZERO_PAD) { /* Need to group 0s */
  2039. /* printf("zeropad\n"); */
  2040. cnt += ppc[1];
  2041. ppc += 3;
  2042. gp = (const char *) ppc[2];
  2043. blk -= len; /* blk > len, so blk still > 0. */
  2044. len = ppc[1];
  2045. continue; /* Don't decrement num_groups here. */
  2046. } else {
  2047. assert(num_groups == 0);
  2048. break;
  2049. }
  2050. }
  2051. if (num_groups <= 0) {
  2052. break;
  2053. }
  2054. --num_groups;
  2055. fp_outfunc(fp, FPO_STR_PREC, tslen, (intptr_t) ts);
  2056. blk = nblk2;
  2057. /* printf("num_groups=%d blk=%d\n", num_groups, blk); */
  2058. } while (1);
  2059. } else
  2060. #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
  2061. fp_outfunc(fp, *ppc, ppc[1], ppc[2]); /* NOTE: Remember 'else' above! */
  2062. cnt += ppc[1];
  2063. ppc += 3;
  2064. } while (ppc < ppc_last);
  2065. return cnt;
  2066. }
  2067. #endif
  2068. /**********************************************************************/
  2069. #ifdef L__store_inttype
  2070. /* Right now, we assume intmax_t is either long or long long */
  2071. #ifdef INTMAX_MAX
  2072. #ifdef LLONG_MAX
  2073. #if INTMAX_MAX > LLONG_MAX
  2074. #error INTMAX_MAX > LLONG_MAX! The printf code needs to be updated!
  2075. #endif
  2076. #elif INTMAX_MAX > LONG_MAX
  2077. #error No LLONG_MAX and INTMAX_MAX > LONG_MAX! The printf code needs to be updated!
  2078. #endif /* LLONG_MAX */
  2079. #endif /* INTMAX_MAX */
  2080. /* We assume int may be short or long, but short and long are different. */
  2081. void _store_inttype(register void *dest, int desttype, uintmax_t val)
  2082. {
  2083. if (desttype == __PA_FLAG_CHAR) { /* assume char not int */
  2084. *((unsigned char *) dest) = val;
  2085. return;
  2086. }
  2087. #if defined(LLONG_MAX) && (LONG_MAX != LLONG_MAX)
  2088. if (desttype == PA_FLAG_LONG_LONG) {
  2089. *((unsigned long long int *) dest) = val;
  2090. return;
  2091. }
  2092. #endif /* LLONG_MAX */
  2093. #if SHRT_MAX != INT_MAX
  2094. if (desttype == PA_FLAG_SHORT) {
  2095. *((unsigned short int *) dest) = val;
  2096. return;
  2097. }
  2098. #endif /* SHRT_MAX */
  2099. #if LONG_MAX != INT_MAX
  2100. if (desttype == PA_FLAG_LONG) {
  2101. *((unsigned long int *) dest) = val;
  2102. return;
  2103. }
  2104. #endif /* LONG_MAX */
  2105. *((unsigned int *) dest) = val;
  2106. }
  2107. #endif
  2108. /**********************************************************************/
  2109. #ifdef L__load_inttype
  2110. extern uintmax_t _load_inttype(int desttype, register const void *src,
  2111. int uflag)
  2112. {
  2113. if (uflag >= 0) { /* unsigned */
  2114. #if LONG_MAX != INT_MAX
  2115. if (desttype & (PA_FLAG_LONG|PA_FLAG_LONG_LONG)) {
  2116. #ifdef LLONG_MAX
  2117. if (desttype == PA_FLAG_LONG_LONG) {
  2118. return *((unsigned long long int *) src);
  2119. }
  2120. #endif
  2121. return *((unsigned long int *) src);
  2122. }
  2123. #else /* LONG_MAX != INT_MAX */
  2124. #ifdef LLONG_MAX
  2125. if (desttype & PA_FLAG_LONG_LONG) {
  2126. return *((unsigned long long int *) src);
  2127. }
  2128. #endif
  2129. #endif /* LONG_MAX != INT_MAX */
  2130. {
  2131. unsigned int x;
  2132. x = *((unsigned int *) src);
  2133. if (desttype == __PA_FLAG_CHAR) x = (unsigned char) x;
  2134. #if SHRT_MAX != INT_MAX
  2135. if (desttype == PA_FLAG_SHORT) x = (unsigned short int) x;
  2136. #endif
  2137. return x;
  2138. }
  2139. } else { /* signed */
  2140. #if LONG_MAX != INT_MAX
  2141. if (desttype & (PA_FLAG_LONG|PA_FLAG_LONG_LONG)) {
  2142. #ifdef LLONG_MAX
  2143. if (desttype == PA_FLAG_LONG_LONG) {
  2144. return *((long long int *) src);
  2145. }
  2146. #endif
  2147. return *((long int *) src);
  2148. }
  2149. #else /* LONG_MAX != INT_MAX */
  2150. #ifdef LLONG_MAX
  2151. if (desttype & PA_FLAG_LONG_LONG) {
  2152. return *((long long int *) src);
  2153. }
  2154. #endif
  2155. #endif /* LONG_MAX != INT_MAX */
  2156. {
  2157. int x;
  2158. x = *((int *) src);
  2159. if (desttype == __PA_FLAG_CHAR) x = (char) x;
  2160. #if SHRT_MAX != INT_MAX
  2161. if (desttype == PA_FLAG_SHORT) x = (short int) x;
  2162. #endif
  2163. return x;
  2164. }
  2165. }
  2166. }
  2167. #endif
  2168. /**********************************************************************/
  2169. #if defined(L_vfprintf) || defined(L_vfwprintf)
  2170. /* We only support ascii digits (or their USC equivalent codes) in
  2171. * precision and width settings in *printf (wide) format strings.
  2172. * In other words, we don't currently support glibc's 'I' flag.
  2173. * We do accept it, but it is currently ignored. */
  2174. static void _charpad(FILE * __restrict stream, int padchar, size_t numpad);
  2175. #ifdef L_vfprintf
  2176. #define VFPRINTF vfprintf
  2177. #define FMT_TYPE char
  2178. #define OUTNSTR _outnstr
  2179. #define STRLEN strlen
  2180. #define _PPFS_init _ppfs_init
  2181. #define OUTPUT(F,S) fputs(S,F)
  2182. #define _outnstr(stream, string, len) _stdio_fwrite(string, len, stream)
  2183. #define FP_OUT _fp_out_narrow
  2184. #ifdef __STDIO_PRINTF_FLOAT
  2185. static void _fp_out_narrow(FILE *fp, intptr_t type, intptr_t len, intptr_t buf)
  2186. {
  2187. if (type & 0x80) { /* Some type of padding needed. */
  2188. int buflen = strlen((const char *) buf);
  2189. if ((len -= buflen) > 0) {
  2190. _charpad(fp, (type & 0x7f), len);
  2191. }
  2192. len = buflen;
  2193. }
  2194. OUTNSTR(fp, (const char *) buf, len);
  2195. }
  2196. #endif /* __STDIO_PRINTF_FLOAT */
  2197. #else /* L_vfprintf */
  2198. #define VFPRINTF vfwprintf
  2199. #define FMT_TYPE wchar_t
  2200. #define OUTNSTR _outnwcs
  2201. #define STRLEN wcslen
  2202. #define _PPFS_init _ppwfs_init
  2203. #define OUTPUT(F,S) fputws(S,F)
  2204. #define _outnwcs(stream, wstring, len) _wstdio_fwrite(wstring, len, stream)
  2205. #define FP_OUT _fp_out_wide
  2206. static void _outnstr(FILE *stream, const char *s, size_t wclen)
  2207. {
  2208. /* NOTE!!! len here is the number of wchars we want to generate!!! */
  2209. wchar_t wbuf[64];
  2210. mbstate_t mbstate;
  2211. size_t todo, r;
  2212. mbstate.mask = 0;
  2213. todo = wclen;
  2214. while (todo) {
  2215. r = mbsrtowcs(wbuf, &s,
  2216. ((todo <= sizeof(wbuf)/sizeof(wbuf[0]))
  2217. ? todo
  2218. : sizeof(wbuf)/sizeof(wbuf[0])),
  2219. &mbstate);
  2220. assert(((ssize_t)r) > 0);
  2221. _outnwcs(stream, wbuf, r);
  2222. todo -= r;
  2223. }
  2224. }
  2225. #ifdef __STDIO_PRINTF_FLOAT
  2226. #ifdef __UCLIBC_MJN3_ONLY__
  2227. #warning TODO: Move defines from _fpmaxtostr. Put them in a common header.
  2228. #endif
  2229. /* The following defines are from _fpmaxtostr.*/
  2230. #define DIGITS_PER_BLOCK 9
  2231. #define NUM_DIGIT_BLOCKS ((DECIMAL_DIG+DIGITS_PER_BLOCK-1)/DIGITS_PER_BLOCK)
  2232. #define BUF_SIZE ( 3 + NUM_DIGIT_BLOCKS * DIGITS_PER_BLOCK )
  2233. static void _fp_out_wide(FILE *fp, intptr_t type, intptr_t len, intptr_t buf)
  2234. {
  2235. wchar_t wbuf[BUF_SIZE];
  2236. const char *s = (const char *) buf;
  2237. int i;
  2238. if (type & 0x80) { /* Some type of padding needed */
  2239. int buflen = strlen(s);
  2240. if ((len -= buflen) > 0) {
  2241. _charpad(fp, (type & 0x7f), len);
  2242. }
  2243. len = buflen;
  2244. }
  2245. if (len > 0) {
  2246. i = 0;
  2247. do {
  2248. #ifdef __LOCALE_C_ONLY
  2249. wbuf[i] = s[i];
  2250. #else /* __LOCALE_C_ONLY */
  2251. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  2252. if (s[i] == ',') {
  2253. wbuf[i] = __UCLIBC_CURLOCALE_DATA.thousands_sep_wc;
  2254. } else
  2255. #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
  2256. if (s[i] == '.') {
  2257. wbuf[i] = __UCLIBC_CURLOCALE_DATA.decimal_point_wc;
  2258. } else {
  2259. wbuf[i] = s[i];
  2260. }
  2261. #endif /* __LOCALE_C_ONLY */
  2262. } while (++i < len);
  2263. OUTNSTR(fp, wbuf, len);
  2264. }
  2265. }
  2266. #endif /* __STDIO_PRINTF_FLOAT */
  2267. static int _ppwfs_init(register ppfs_t *ppfs, const wchar_t *fmt0)
  2268. {
  2269. static const wchar_t invalid_wcs[] = L"Invalid wide format string.";
  2270. int r;
  2271. /* First, zero out everything... argnumber[], argtype[], argptr[] */
  2272. memset(ppfs, 0, sizeof(ppfs_t)); /* TODO: nonportable???? */
  2273. #ifdef NL_ARGMAX
  2274. --ppfs->maxposarg; /* set to -1 */
  2275. #endif /* NL_ARGMAX */
  2276. ppfs->fmtpos = (const char *) fmt0;
  2277. ppfs->info._flags = FLAG_WIDESTREAM;
  2278. {
  2279. mbstate_t mbstate;
  2280. const wchar_t *p;
  2281. mbstate.mask = 0; /* Initialize the mbstate. */
  2282. p = fmt0;
  2283. if (wcsrtombs(NULL, &p, SIZE_MAX, &mbstate) == ((size_t)(-1))) {
  2284. ppfs->fmtpos = (const char *) invalid_wcs;
  2285. return -1;
  2286. }
  2287. }
  2288. /* now set all argtypes to no-arg */
  2289. {
  2290. #if 1
  2291. /* TODO - use memset here since already "paid for"? */
  2292. register int *p = ppfs->argtype;
  2293. r = MAX_ARGS;
  2294. do {
  2295. *p++ = __PA_NOARG;
  2296. } while (--r);
  2297. #else
  2298. /* TODO -- get rid of this?? */
  2299. register char *p = (char *) ((MAX_ARGS-1) * sizeof(int));
  2300. do {
  2301. *((int *)(((char *)ppfs) + ((int)p) + offsetof(ppfs_t,argtype))) = __PA_NOARG;
  2302. p -= sizeof(int);
  2303. } while (p);
  2304. #endif
  2305. }
  2306. /*
  2307. * Run through the entire format string to validate it and initialize
  2308. * the positional arg numbers (if any).
  2309. */
  2310. {
  2311. register const wchar_t *fmt = fmt0;
  2312. while (*fmt) {
  2313. if ((*fmt == '%') && (*++fmt != '%')) {
  2314. ppfs->fmtpos = (const char *) fmt; /* back up to the '%' */
  2315. if ((r = _ppfs_parsespec(ppfs)) < 0) {
  2316. return -1;
  2317. }
  2318. fmt = (const wchar_t *) ppfs->fmtpos; /* update to one past end of spec */
  2319. } else {
  2320. ++fmt;
  2321. }
  2322. }
  2323. ppfs->fmtpos = (const char *) fmt0; /* rewind */
  2324. }
  2325. #ifdef NL_ARGMAX
  2326. /* If we have positional args, make sure we know all the types. */
  2327. {
  2328. register int *p = ppfs->argtype;
  2329. r = ppfs->maxposarg;
  2330. while (--r >= 0) {
  2331. if ( *p == __PA_NOARG ) { /* missing arg type!!! */
  2332. return -1;
  2333. }
  2334. ++p;
  2335. }
  2336. }
  2337. #endif /* NL_ARGMAX */
  2338. return 0;
  2339. }
  2340. #endif /* L_vfprintf */
  2341. static void _charpad(FILE * __restrict stream, int padchar, size_t numpad)
  2342. {
  2343. /* TODO -- Use a buffer to cut down on function calls... */
  2344. FMT_TYPE pad[1];
  2345. *pad = padchar;
  2346. while (numpad) {
  2347. OUTNSTR(stream, pad, 1);
  2348. --numpad;
  2349. }
  2350. }
  2351. /* TODO -- Dynamically allocate work space to accomodate stack-poor archs? */
  2352. static int _do_one_spec(FILE * __restrict stream,
  2353. register ppfs_t *ppfs, int *count)
  2354. {
  2355. static const char spec_base[] = SPEC_BASE;
  2356. #ifdef L_vfprintf
  2357. static const char prefix[] = "+\0-\0 \0000x\0000X";
  2358. /* 0 2 4 6 9 11*/
  2359. #else /* L_vfprintf */
  2360. static const wchar_t prefix[] = L"+\0-\0 \0000x\0000X";
  2361. #endif /* L_vfprintf */
  2362. enum {
  2363. PREFIX_PLUS = 0,
  2364. PREFIX_MINUS = 2,
  2365. PREFIX_SPACE = 4,
  2366. PREFIX_LWR_X = 6,
  2367. PREFIX_UPR_X = 9,
  2368. PREFIX_NONE = 11
  2369. };
  2370. #ifdef __va_arg_ptr
  2371. const void * const *argptr;
  2372. #else
  2373. const void * argptr[MAX_ARGS_PER_SPEC];
  2374. #endif
  2375. int *argtype;
  2376. #ifdef __UCLIBC_HAS_WCHAR__
  2377. const wchar_t *ws = NULL;
  2378. mbstate_t mbstate;
  2379. #endif /* __UCLIBC_HAS_WCHAR__ */
  2380. size_t slen;
  2381. #ifdef L_vfprintf
  2382. #define SLEN slen
  2383. #else
  2384. size_t SLEN;
  2385. wchar_t wbuf[2];
  2386. #endif
  2387. int base;
  2388. int numpad;
  2389. int alphacase;
  2390. int numfill = 0; /* TODO: fix */
  2391. int prefix_num = PREFIX_NONE;
  2392. char padchar = ' ';
  2393. #ifdef __UCLIBC_MJN3_ONLY__
  2394. #warning TODO: Determine appropriate buf size.
  2395. #endif /* __UCLIBC_MJN3_ONLY__ */
  2396. /* TODO: buf needs to be big enough for any possible error return strings
  2397. * and also for any locale-grouped long long integer strings generated.
  2398. * This should be large enough for any of the current archs/locales, but
  2399. * eventually this should be handled robustly. */
  2400. char buf[128];
  2401. #ifdef NDEBUG
  2402. _ppfs_parsespec(ppfs);
  2403. #else
  2404. if (_ppfs_parsespec(ppfs) < 0) { /* TODO: just for debugging */
  2405. abort();
  2406. }
  2407. #endif
  2408. _ppfs_setargs(ppfs);
  2409. argtype = ppfs->argtype + ppfs->argnumber[2] - 1;
  2410. /* Deal with the argptr vs argvalue issue. */
  2411. #ifdef __va_arg_ptr
  2412. argptr = (const void * const *) ppfs->argptr;
  2413. #ifdef NL_ARGMAX
  2414. if (ppfs->maxposarg > 0) { /* Using positional args... */
  2415. argptr += ppfs->argnumber[2] - 1;
  2416. }
  2417. #endif /* NL_ARGMAX */
  2418. #else
  2419. /* Need to build a local copy... */
  2420. {
  2421. register argvalue_t *p = ppfs->argvalue;
  2422. int i;
  2423. #ifdef NL_ARGMAX
  2424. if (ppfs->maxposarg > 0) { /* Using positional args... */
  2425. p += ppfs->argnumber[2] - 1;
  2426. }
  2427. #endif /* NL_ARGMAX */
  2428. for (i = 0 ; i < ppfs->num_data_args ; i++ ) {
  2429. argptr[i] = (void *) p++;
  2430. }
  2431. }
  2432. #endif
  2433. {
  2434. register char *s = NULL; /* TODO: Should s be unsigned char * ? */
  2435. if (ppfs->conv_num == CONV_n) {
  2436. _store_inttype(*(void **)*argptr,
  2437. ppfs->info._flags & __PA_INTMASK,
  2438. (intmax_t) (*count));
  2439. return 0;
  2440. }
  2441. if (ppfs->conv_num <= CONV_i) { /* pointer or (un)signed int */
  2442. alphacase = __UIM_LOWER;
  2443. #ifdef __UCLIBC_MJN3_ONLY__
  2444. #ifdef L_vfprintf
  2445. #warning CONSIDER: Should we ignore these flags if stub locale? What about custom specs?
  2446. #endif
  2447. #endif /* __UCLIBC_MJN3_ONLY__ */
  2448. if ((base = spec_base[(int)(ppfs->conv_num - CONV_p)]) == 10) {
  2449. if (PRINT_INFO_FLAG_VAL(&(ppfs->info),group)) {
  2450. alphacase = __UIM_GROUP;
  2451. }
  2452. if (PRINT_INFO_FLAG_VAL(&(ppfs->info),i18n)) {
  2453. alphacase |= 0x80;
  2454. }
  2455. }
  2456. if (ppfs->conv_num <= CONV_u) { /* pointer or unsigned int */
  2457. if (ppfs->conv_num == CONV_X) {
  2458. alphacase = __UIM_UPPER;
  2459. }
  2460. if (ppfs->conv_num == CONV_p) { /* pointer */
  2461. prefix_num = PREFIX_LWR_X;
  2462. } else { /* unsigned int */
  2463. }
  2464. } else { /* signed int */
  2465. base = -base;
  2466. }
  2467. if (ppfs->info.prec < 0) { /* Ignore '0' flag if prec specified. */
  2468. padchar = ppfs->info.pad;
  2469. }
  2470. #ifdef __UCLIBC_MJN3_ONLY__
  2471. #ifdef L_vfprintf
  2472. #warning CONSIDER: If using outdigits and/or grouping, how should we interpret precision?
  2473. #endif
  2474. #endif /* __UCLIBC_MJN3_ONLY__ */
  2475. s = _uintmaxtostr(buf + sizeof(buf) - 1,
  2476. (uintmax_t)
  2477. _load_inttype(*argtype & __PA_INTMASK,
  2478. *argptr, base), base, alphacase);
  2479. if (ppfs->conv_num > CONV_u) { /* signed int */
  2480. if (*s == '-') {
  2481. PRINT_INFO_SET_FLAG(&(ppfs->info),showsign);
  2482. ++s; /* handle '-' in the prefix string */
  2483. prefix_num = PREFIX_MINUS;
  2484. } else if (PRINT_INFO_FLAG_VAL(&(ppfs->info),showsign)) {
  2485. prefix_num = PREFIX_PLUS;
  2486. } else if (PRINT_INFO_FLAG_VAL(&(ppfs->info),space)) {
  2487. prefix_num = PREFIX_SPACE;
  2488. }
  2489. }
  2490. slen = (char *)(buf + sizeof(buf) - 1) - s;
  2491. #ifdef L_vfwprintf
  2492. {
  2493. const char *q = s;
  2494. mbstate.mask = 0; /* Initialize the mbstate. */
  2495. SLEN = mbsrtowcs(NULL, &q, 0, &mbstate);
  2496. }
  2497. #endif
  2498. numfill = ((ppfs->info.prec < 0) ? 1 : ppfs->info.prec);
  2499. if (PRINT_INFO_FLAG_VAL(&(ppfs->info),alt)) {
  2500. if (ppfs->conv_num <= CONV_x) { /* x or p */
  2501. prefix_num = PREFIX_LWR_X;
  2502. }
  2503. if (ppfs->conv_num == CONV_X) {
  2504. prefix_num = PREFIX_UPR_X;
  2505. }
  2506. if ((ppfs->conv_num == CONV_o) && (numfill <= SLEN)) {
  2507. numfill = ((*s == '0') ? 1 : SLEN + 1);
  2508. }
  2509. }
  2510. if (*s == '0') {
  2511. if (prefix_num >= PREFIX_LWR_X) {
  2512. prefix_num = PREFIX_NONE;
  2513. }
  2514. if (ppfs->conv_num == CONV_p) {/* null pointer */
  2515. s = "(nil)";
  2516. #ifdef L_vfwprintf
  2517. SLEN =
  2518. #endif
  2519. slen = 5;
  2520. numfill = 0;
  2521. } else if (numfill == 0) { /* if precision 0, no output */
  2522. #ifdef L_vfwprintf
  2523. SLEN =
  2524. #endif
  2525. slen = 0;
  2526. }
  2527. }
  2528. numfill = ((numfill > SLEN) ? numfill - SLEN : 0);
  2529. } else if (ppfs->conv_num <= CONV_A) { /* floating point */
  2530. #ifdef __STDIO_PRINTF_FLOAT
  2531. *count +=
  2532. _fpmaxtostr(stream,
  2533. (__fpmax_t)
  2534. (PRINT_INFO_FLAG_VAL(&(ppfs->info),is_long_double)
  2535. ? *(long double *) *argptr
  2536. : (long double) (* (double *) *argptr)),
  2537. &ppfs->info, FP_OUT );
  2538. return 0;
  2539. #else /* __STDIO_PRINTF_FLOAT */
  2540. return -1; /* TODO -- try to continue? */
  2541. #endif /* __STDIO_PRINTF_FLOAT */
  2542. } else if (ppfs->conv_num <= CONV_S) { /* wide char or string */
  2543. #ifdef L_vfprintf
  2544. #ifdef __UCLIBC_HAS_WCHAR__
  2545. mbstate.mask = 0; /* Initialize the mbstate. */
  2546. if (ppfs->conv_num == CONV_S) { /* wide string */
  2547. if (!(ws = *((const wchar_t **) *argptr))) {
  2548. goto NULL_STRING;
  2549. }
  2550. /* We use an awful uClibc-specific hack here, passing
  2551. * (char*) &ws as the conversion destination. This signals
  2552. * uClibc's wcsrtombs that we want a "restricted" length
  2553. * such that the mbs fits in a buffer of the specified
  2554. * size with no partial conversions. */
  2555. if ((slen = wcsrtombs((char *) &ws, &ws, /* Use awful hack! */
  2556. ((ppfs->info.prec >= 0)
  2557. ? ppfs->info.prec
  2558. : SIZE_MAX), &mbstate))
  2559. == ((size_t)-1)
  2560. ) {
  2561. return -1; /* EILSEQ */
  2562. }
  2563. } else { /* wide char */
  2564. s = buf;
  2565. slen = wcrtomb(s, (*((const wchar_t *) *argptr)), &mbstate);
  2566. if (slen == ((size_t)-1)) {
  2567. return -1; /* EILSEQ */
  2568. }
  2569. s[slen] = 0; /* TODO - Is this necessary? */
  2570. }
  2571. #else /* __UCLIBC_HAS_WCHAR__ */
  2572. return -1;
  2573. #endif /* __UCLIBC_HAS_WCHAR__ */
  2574. } else if (ppfs->conv_num <= CONV_s) { /* char or string */
  2575. if (ppfs->conv_num == CONV_s) { /* string */
  2576. s = *((char **) (*argptr));
  2577. if (s) {
  2578. #ifdef __STDIO_PRINTF_M_SUPPORT
  2579. SET_STRING_LEN:
  2580. #endif
  2581. slen = strnlen(s, ((ppfs->info.prec >= 0)
  2582. ? ppfs->info.prec : SIZE_MAX));
  2583. } else {
  2584. #ifdef __UCLIBC_HAS_WCHAR__
  2585. NULL_STRING:
  2586. #endif
  2587. s = "(null)";
  2588. slen = 6;
  2589. }
  2590. } else { /* char */
  2591. s = buf;
  2592. *s = (unsigned char)(*((const int *) *argptr));
  2593. s[1] = 0;
  2594. slen = 1;
  2595. }
  2596. #else /* L_vfprintf */
  2597. if (ppfs->conv_num == CONV_S) { /* wide string */
  2598. ws = *((wchar_t **) (*argptr));
  2599. if (!ws) {
  2600. goto NULL_STRING;
  2601. }
  2602. SLEN = wcsnlen(ws, ((ppfs->info.prec >= 0)
  2603. ? ppfs->info.prec : SIZE_MAX));
  2604. } else { /* wide char */
  2605. *wbuf = (wchar_t)(*((const wint_t *) *argptr));
  2606. CHAR_CASE:
  2607. ws = wbuf;
  2608. wbuf[1] = 0;
  2609. SLEN = 1;
  2610. }
  2611. } else if (ppfs->conv_num <= CONV_s) { /* char or string */
  2612. if (ppfs->conv_num == CONV_s) { /* string */
  2613. #ifdef __UCLIBC_MJN3_ONLY__
  2614. #warning TODO: Fix %s for vfwprintf... output upto illegal sequence?
  2615. #endif /* __UCLIBC_MJN3_ONLY__ */
  2616. s = *((char **) (*argptr));
  2617. if (s) {
  2618. #ifdef __STDIO_PRINTF_M_SUPPORT
  2619. SET_STRING_LEN:
  2620. #endif
  2621. /* We use an awful uClibc-specific hack here, passing
  2622. * (wchar_t*) &mbstate as the conversion destination.
  2623. * This signals uClibc's mbsrtowcs that we want a
  2624. * "restricted" length such that the mbs fits in a buffer
  2625. * of the specified size with no partial conversions. */
  2626. {
  2627. const char *q = s;
  2628. mbstate.mask = 0; /* Initialize the mbstate. */
  2629. SLEN = mbsrtowcs((wchar_t *) &mbstate, &q,
  2630. ((ppfs->info.prec >= 0)
  2631. ? ppfs->info.prec : SIZE_MAX),
  2632. &mbstate);
  2633. }
  2634. if (SLEN == ((size_t)(-1))) {
  2635. return -1; /* EILSEQ */
  2636. }
  2637. } else {
  2638. NULL_STRING:
  2639. s = "(null)";
  2640. SLEN = slen = 6;
  2641. }
  2642. } else { /* char */
  2643. *wbuf = btowc( (unsigned char)(*((const int *) *argptr)) );
  2644. goto CHAR_CASE;
  2645. }
  2646. #endif /* L_vfprintf */
  2647. #ifdef __STDIO_PRINTF_M_SUPPORT
  2648. } else if (ppfs->conv_num == CONV_m) {
  2649. s = _glibc_strerror_r(errno, buf, sizeof(buf));
  2650. goto SET_STRING_LEN;
  2651. #endif
  2652. } else {
  2653. #ifdef __STDIO_GLIBC_CUSTOM_PRINTF
  2654. assert(ppfs->conv_num == CONV_custom0);
  2655. s = _custom_printf_spec;
  2656. do {
  2657. if (*s == ppfs->info.spec) {
  2658. int rv;
  2659. /* TODO -- check return value for sanity? */
  2660. rv = (*_custom_printf_handler
  2661. [(int)(s-_custom_printf_spec)])
  2662. (stream, &ppfs->info, argptr);
  2663. if (rv < 0) {
  2664. return -1;
  2665. }
  2666. *count += rv;
  2667. return 0;
  2668. }
  2669. } while (++s < (_custom_printf_spec + MAX_USER_SPEC));
  2670. #endif /* __STDIO_GLIBC_CUSTOM_PRINTF */
  2671. assert(0);
  2672. return -1;
  2673. }
  2674. #ifdef __UCLIBC_MJN3_ONLY__
  2675. #ifdef L_vfprintf
  2676. #warning CONSIDER: If using outdigits and/or grouping, how should we pad?
  2677. #endif
  2678. #endif /* __UCLIBC_MJN3_ONLY__ */
  2679. {
  2680. size_t t;
  2681. t = SLEN + numfill;
  2682. if (prefix_num != PREFIX_NONE) {
  2683. t += ((prefix_num < PREFIX_LWR_X) ? 1 : 2);
  2684. }
  2685. numpad = ((ppfs->info.width > t) ? (ppfs->info.width - t) : 0);
  2686. *count += t + numpad;
  2687. }
  2688. if (padchar == '0') { /* TODO: check this */
  2689. numfill += numpad;
  2690. numpad = 0;
  2691. }
  2692. /* Now handle the output itself. */
  2693. if (!PRINT_INFO_FLAG_VAL(&(ppfs->info),left)) {
  2694. _charpad(stream, ' ', numpad);
  2695. numpad = 0;
  2696. }
  2697. OUTPUT(stream, prefix + prefix_num);
  2698. _charpad(stream, '0', numfill);
  2699. #ifdef L_vfprintf
  2700. #ifdef __UCLIBC_HAS_WCHAR__
  2701. if (!ws) {
  2702. assert(s);
  2703. _outnstr(stream, s, slen);
  2704. } else { /* wide string */
  2705. size_t t;
  2706. mbstate.mask = 0; /* Initialize the mbstate. */
  2707. while (slen) {
  2708. t = (slen <= sizeof(buf)) ? slen : sizeof(buf);
  2709. t = wcsrtombs(buf, &ws, t, &mbstate);
  2710. assert (t != ((size_t)(-1)));
  2711. _outnstr(stream, buf, t);
  2712. slen -= t;
  2713. }
  2714. }
  2715. #else /* __UCLIBC_HAS_WCHAR__ */
  2716. _outnstr(stream, s, slen);
  2717. #endif /* __UCLIBC_HAS_WCHAR__ */
  2718. #else /* L_vfprintf */
  2719. if (!ws) {
  2720. assert(s);
  2721. _outnstr(stream, s, SLEN);
  2722. } else {
  2723. _outnwcs(stream, ws, SLEN);
  2724. }
  2725. #endif /* L_vfprintf */
  2726. _charpad(stream, ' ', numpad);
  2727. }
  2728. return 0;
  2729. }
  2730. int VFPRINTF (FILE * __restrict stream,
  2731. register const FMT_TYPE * __restrict format,
  2732. va_list arg)
  2733. {
  2734. ppfs_t ppfs;
  2735. int count, r;
  2736. register const FMT_TYPE *s;
  2737. __STDIO_THREADLOCK(stream);
  2738. count = 0;
  2739. s = format;
  2740. if (_PPFS_init(&ppfs, format) < 0) { /* Bad format string. */
  2741. OUTNSTR(stream, (const FMT_TYPE *) ppfs.fmtpos,
  2742. STRLEN((const FMT_TYPE *)(ppfs.fmtpos)));
  2743. #if defined(L_vfprintf) && !defined(NDEBUG)
  2744. fprintf(stderr,"\nIMbS: \"%s\"\n\n", format);
  2745. #endif
  2746. count = -1;
  2747. } else {
  2748. _ppfs_prepargs(&ppfs, arg); /* This did a va_copy!!! */
  2749. do {
  2750. while (*format && (*format != '%')) {
  2751. ++format;
  2752. }
  2753. if (format-s) { /* output any literal text in format string */
  2754. if ( (r = OUTNSTR(stream, s, format-s)) < 0) {
  2755. count = -1;
  2756. break;
  2757. }
  2758. count += r;
  2759. }
  2760. if (!*format) { /* we're done */
  2761. break;
  2762. }
  2763. if (format[1] != '%') { /* if we get here, *format == '%' */
  2764. /* TODO: _do_one_spec needs to know what the output funcs are!!! */
  2765. ppfs.fmtpos = (const char *)(++format);
  2766. /* TODO: check -- should only fail on stream error */
  2767. if ( (r = _do_one_spec(stream, &ppfs, &count)) < 0) {
  2768. count = -1;
  2769. break;
  2770. }
  2771. s = format = (const FMT_TYPE *) ppfs.fmtpos;
  2772. } else { /* %% means literal %, so start new string */
  2773. s = ++format;
  2774. ++format;
  2775. }
  2776. } while (1);
  2777. va_end(ppfs.arg); /* Need to clean up after va_copy! */
  2778. }
  2779. __STDIO_THREADUNLOCK(stream);
  2780. return count;
  2781. }
  2782. #endif
  2783. /**********************************************************************/