printf.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691
  1. /* Copyright (C) 2002 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. /* TODO:
  64. *
  65. * Should we validate that *printf format strings are valid multibyte
  66. * strings in the current locale? ANSI/ISO C99 seems to imply this
  67. * and Plauger's printf implementation in his Standard C Library book
  68. * treats this as an error.
  69. *
  70. * Implement %a, %A, and locale-specific grouping for the printf floating
  71. * point conversions. To be done in the rewrite of _dtostr().
  72. */
  73. #define _ISOC99_SOURCE /* for ULLONG primarily... */
  74. #define _GNU_SOURCE
  75. #define _STDIO_UTILITY /* We're using _uintmaxtostr. */
  76. #include <stdio.h>
  77. #include <stdlib.h>
  78. #include <string.h>
  79. #include <stddef.h>
  80. #include <ctype.h>
  81. #include <limits.h>
  82. #include <stdarg.h>
  83. #include <assert.h>
  84. #include <stdint.h>
  85. #include <errno.h>
  86. #define __PRINTF_INFO_NO_BITFIELD
  87. #include <printf.h>
  88. #ifdef __STDIO_THREADSAFE
  89. #include <stdio_ext.h>
  90. #include <pthread.h>
  91. #endif /* __STDIO_THREADSAFE */
  92. #ifdef __UCLIBC_HAS_WCHAR__
  93. #include <wchar.h>
  94. #endif /* __UCLIBC_HAS_WCHAR__ */
  95. /**********************************************************************/
  96. /* These provide some control over printf's feature set */
  97. #define __STDIO_PRINTF_FLOAT
  98. #define __STDIO_PRINTF_M_SUPPORT
  99. /**********************************************************************/
  100. #if defined(__UCLIBC__) && !defined(__UCLIBC_HAS_FLOATS__)
  101. #undef __STDIO_PRINTF_FLOAT
  102. #endif
  103. #ifdef __BCC__
  104. #undef __STDIO_PRINTF_FLOAT
  105. #endif
  106. #ifndef __STDIO_PRINTF_FLOAT
  107. #undef L__dtostr
  108. #endif
  109. /**********************************************************************/
  110. #define __STDIO_GLIBC_CUSTOM_PRINTF
  111. /* TODO -- move these to a configuration section? */
  112. #define MAX_FIELD_WIDTH 4095
  113. #define MAX_USER_SPEC 10
  114. #define MAX_POS_ARGS 10
  115. /* TODO - fix the defs below */
  116. #define MAX_ARGS_PER_SPEC (MAX_POS_ARGS-2)
  117. #if MAX_ARGS_PER_SPEC + 2 > MAX_POS_ARGS
  118. #define MAX_ARGS MAX_ARGS_PER_SPEC + 2
  119. #else
  120. #define MAX_ARGS MAX_POS_ARGS
  121. #endif
  122. /**********************************************************************/
  123. /* Deal with pre-C99 compilers. */
  124. #ifndef va_copy
  125. #ifdef __va_copy
  126. #define va_copy(A,B) __va_copy(A,B)
  127. #else
  128. /* TODO -- maybe create a bits/vacopy.h for arch specific versions
  129. * to ensure we get the right behavior? Either that or fall back
  130. * on the portable (but costly in size) method of using a va_list *.
  131. * That means a pointer derefs in the va_arg() invocations... */
  132. #warning neither va_copy or __va_copy is defined. using a simple copy instead...
  133. /* the glibc manual suggests that this will usually suffice when
  134. __va_copy doesn't exist. */
  135. #define va_copy(A,B) A = B
  136. #endif
  137. #endif /* va_copy */
  138. /**********************************************************************/
  139. #define __PA_FLAG_INTMASK \
  140. (__PA_FLAG_CHAR|PA_FLAG_SHORT|__PA_FLAG_INT|PA_FLAG_LONG|PA_FLAG_LONG_LONG)
  141. extern printf_function _custom_printf_handler[MAX_USER_SPEC];
  142. extern printf_arginfo_function *_custom_printf_arginfo[MAX_USER_SPEC];
  143. extern char *_custom_printf_spec;
  144. /**********************************************************************/
  145. #define SPEC_FLAGS " +0-#'I"
  146. enum {
  147. FLAG_SPACE = 0x01,
  148. FLAG_PLUS = 0x02, /* must be 2 * FLAG_SPACE */
  149. FLAG_ZERO = 0x04,
  150. FLAG_MINUS = 0x08, /* must be 2 * FLAG_ZERO */
  151. FLAG_HASH = 0x10,
  152. FLAG_THOUSANDS = 0x20,
  153. FLAG_I18N = 0x40, /* only works for d, i, u */
  154. FLAG_WIDESTREAM = 0x80
  155. };
  156. /**********************************************************************/
  157. /* float layout 01234567890123456789 TODO: B?*/
  158. #define SPEC_CHARS "npxXoudifFeEgGaACScs"
  159. enum {
  160. CONV_n = 0,
  161. CONV_p,
  162. CONV_x, CONV_X, CONV_o, CONV_u, CONV_d, CONV_i,
  163. CONV_f, CONV_F, CONV_e, CONV_E, CONV_g, CONV_G, CONV_a, CONV_A,
  164. CONV_C, CONV_S, CONV_c, CONV_s,
  165. #ifdef __STDIO_PRINTF_M_SUPPORT
  166. CONV_m,
  167. #endif
  168. CONV_custom0 /* must be last */
  169. };
  170. /* p x X o u d i */
  171. #define SPEC_BASE { 16, 16, 16, 8, 10, 10, 10 }
  172. #define SPEC_RANGES { CONV_n, CONV_p, CONV_i, CONV_A, \
  173. CONV_C, CONV_S, CONV_c, CONV_s, CONV_custom0 }
  174. #define SPEC_OR_MASK { \
  175. /* n */ (PA_FLAG_PTR|PA_INT), \
  176. /* p */ PA_POINTER, \
  177. /* oxXudi */ PA_INT, \
  178. /* fFeEgGaA */ PA_DOUBLE, \
  179. /* C */ PA_WCHAR, \
  180. /* S */ PA_WSTRING, \
  181. /* c */ PA_CHAR, \
  182. /* s */ PA_STRING, \
  183. }
  184. #define SPEC_AND_MASK { \
  185. /* n */ (PA_FLAG_PTR|__PA_INTMASK), \
  186. /* p */ PA_POINTER, \
  187. /* oxXudi */ (__PA_INTMASK), \
  188. /* fFeEgGaA */ (PA_FLAG_LONG_DOUBLE|PA_DOUBLE), \
  189. /* C */ (PA_WCHAR), \
  190. /* S */ (PA_WSTRING), \
  191. /* c */ (PA_CHAR), \
  192. /* s */ (PA_STRING), \
  193. }
  194. /**********************************************************************/
  195. /*
  196. * In order to ease translation to what arginfo and _print_info._flags expect,
  197. * we map: 0:int 1:char 2:longlong 4:long 8:short
  198. * and then _flags |= (((q << 7) + q) & 0x701) and argtype |= (_flags & 0x701)
  199. */
  200. /* TODO -- Fix the table below to take into account stdint.h. */
  201. /* #ifndef LLONG_MAX */
  202. /* #error fix QUAL_CHARS for no long long! Affects 'L', 'j', 'q', 'll'. */
  203. /* #else */
  204. /* #if LLONG_MAX != INTMAX_MAX */
  205. /* #error fix QUAL_CHARS intmax_t entry 'j'! */
  206. /* #endif */
  207. /* #endif */
  208. #ifdef PDS
  209. #error PDS already defined!
  210. #endif
  211. #ifdef SS
  212. #error SS already defined!
  213. #endif
  214. #ifdef IMS
  215. #error IMS already defined!
  216. #endif
  217. #if PTRDIFF_MAX == INT_MAX
  218. #define PDS 0
  219. #elif PTRDIFF_MAX == LONG_MAX
  220. #define PDS 4
  221. #elif defined(LLONG_MAX) && (PTRDIFF_MAX == LLONG_MAX)
  222. #define PDS 8
  223. #else
  224. #error fix QUAL_CHARS ptrdiff_t entry 't'!
  225. #endif
  226. #if SIZE_MAX == UINT_MAX
  227. #define SS 0
  228. #elif SIZE_MAX == ULONG_MAX
  229. #define SS 4
  230. #elif defined(LLONG_MAX) && (SIZE_MAX == ULLONG_MAX)
  231. #define SS 8
  232. #else
  233. #error fix QUAL_CHARS size_t entries 'z', 'Z'!
  234. #endif
  235. #if INTMAX_MAX == INT_MAX
  236. #define IMS 0
  237. #elif INTMAX_MAX == LONG_MAX
  238. #define IMS 4
  239. #elif defined(LLONG_MAX) && (INTMAX_MAX == LLONG_MAX)
  240. #define IMS 8
  241. #else
  242. #error fix QUAL_CHARS ptrdiff_t entry 't'!
  243. #endif
  244. #define QUAL_CHARS { \
  245. /* j:(u)intmax_t z:(s)size_t t:ptrdiff_t \0:int */ \
  246. /* q:long_long Z:(s)size_t */ \
  247. 'h', 'l', 'L', 'j', 'z', 't', 'q', 'Z', 0, \
  248. 2, 4, 8, IMS, SS, PDS, 8, SS, 0, /* TODO -- fix!!! */\
  249. 1, 8 \
  250. }
  251. /**********************************************************************/
  252. #ifdef __STDIO_VA_ARG_PTR
  253. #ifdef __BCC__
  254. #define __va_arg_ptr(ap,type) (((type *)(ap += sizeof(type))) - 1)
  255. #endif
  256. #if 1
  257. #ifdef __GNUC__
  258. /* TODO -- need other than for 386 as well! */
  259. #ifndef __va_rounded_size
  260. #define __va_rounded_size(TYPE) \
  261. (((sizeof (TYPE) + sizeof (int) - 1) / sizeof (int)) * sizeof (int))
  262. #endif
  263. #define __va_arg_ptr(AP, TYPE) \
  264. (AP = (va_list) ((char *) (AP) + __va_rounded_size (TYPE)), \
  265. ((void *) ((char *) (AP) - __va_rounded_size (TYPE))))
  266. #endif
  267. #endif
  268. #endif /* __STDIO_VA_ARG_PTR */
  269. #ifdef __va_arg_ptr
  270. #define GET_VA_ARG(AP,F,TYPE,ARGS) (*(AP) = __va_arg_ptr(ARGS,TYPE))
  271. #define GET_ARG_VALUE(AP,F,TYPE) (*((TYPE *)(*(AP))))
  272. #else
  273. typedef union {
  274. wchar_t wc;
  275. unsigned int u;
  276. unsigned long ul;
  277. #ifdef ULLONG_MAX
  278. unsigned long long ull;
  279. #endif
  280. #ifdef __STDIO_PRINTF_FLOAT
  281. double d;
  282. long double ld;
  283. #endif /* __STDIO_PRINTF_FLOAT */
  284. void *p;
  285. } argvalue_t;
  286. #define GET_VA_ARG(AU,F,TYPE,ARGS) (AU->F = va_arg(ARGS,TYPE))
  287. #define GET_ARG_VALUE(AU,F,TYPE) ((TYPE)((AU)->F))
  288. #endif
  289. typedef struct {
  290. const char *fmtpos; /* TODO: move below struct?? */
  291. struct printf_info info;
  292. int maxposarg; /* > 0 if args are positional, 0 if not, -1 if unknown */
  293. int num_data_args; /* TODO: use sentinal??? */
  294. unsigned int conv_num;
  295. unsigned char argnumber[4]; /* width | prec | 1st data | unused */
  296. int argtype[MAX_ARGS];
  297. va_list arg;
  298. #ifdef __va_arg_ptr
  299. void *argptr[MAX_ARGS];
  300. #else
  301. /* While this is wasteful of space in the case where pos args aren't
  302. * enabled, it is also needed to support custom printf handlers. */
  303. argvalue_t argvalue[MAX_ARGS];
  304. #endif
  305. } ppfs_t; /* parse printf format state */
  306. /**********************************************************************/
  307. /* TODO: fix printf to return 0 and set errno if format error. Standard says
  308. only returns -1 if sets error indicator for the stream. */
  309. #ifdef __STDIO_PRINTF_FLOAT
  310. extern size_t _dtostr(FILE * fp, long double x, struct printf_info *info);
  311. #endif
  312. extern int _ppfs_init(ppfs_t *ppfs, const char *fmt0); /* validates */
  313. extern void _ppfs_prepargs(ppfs_t *ppfs, va_list arg); /* sets posargptrs */
  314. extern void _ppfs_setargs(ppfs_t *ppfs); /* sets argptrs for current spec */
  315. extern int _ppfs_parsespec(ppfs_t *ppfs); /* parses specifier */
  316. extern void _store_inttype(void *dest, int desttype, uintmax_t val);
  317. extern uintmax_t _load_inttype(int desttype, const void *src, int uflag);
  318. /**********************************************************************/
  319. #ifdef L_parse_printf_format
  320. /* NOTE: This function differs from the glibc version in that parsing stops
  321. * upon encountering an invalid conversion specifier. Since this is the way
  322. * my printf functions work, I think it makes sense to do it that way here.
  323. * Unfortunately, since glibc sets the return type as size_t, we have no way
  324. * of returning that the template is illegal, other than returning 0.
  325. */
  326. size_t parse_printf_format(register const char *template,
  327. size_t n, register int *argtypes)
  328. {
  329. ppfs_t ppfs;
  330. size_t i;
  331. size_t count = 0;
  332. if (_ppfs_init(&ppfs, template) >= 0) {
  333. if (ppfs.maxposarg > 0) { /* Using positional args. */
  334. count = ppfs.maxposarg;
  335. if (n > count) {
  336. n = count;
  337. }
  338. for (i = 0 ; i < n ; i++) {
  339. *argtypes++ = ppfs.argtype[i];
  340. }
  341. } else { /* Not using positional args. */
  342. while (*template) {
  343. if ((*template == '%') && (*++template != '%')) {
  344. ppfs.fmtpos = template;
  345. _ppfs_parsespec(&ppfs); /* Can't fail. */
  346. template = ppfs.fmtpos; /* Update to one past spec end. */
  347. if (ppfs.info.width == INT_MIN) {
  348. ++count;
  349. if (n > 0) {
  350. *argtypes++ = PA_INT;
  351. --n;
  352. }
  353. }
  354. if (ppfs.info.prec == INT_MIN) {
  355. ++count;
  356. if (n > 0) {
  357. *argtypes++ = PA_INT;
  358. --n;
  359. }
  360. }
  361. for (i = 0 ; i < ppfs.num_data_args ; i++) {
  362. if ((ppfs.argtype[i]) != __PA_NOARG) {
  363. ++count;
  364. if (n > 0) {
  365. *argtypes++ = ppfs.argtype[i];
  366. --n;
  367. }
  368. }
  369. }
  370. } else {
  371. ++template;
  372. }
  373. }
  374. }
  375. }
  376. return count;
  377. }
  378. #endif
  379. /**********************************************************************/
  380. #ifdef L__ppfs_init
  381. int _ppfs_init(register ppfs_t *ppfs, const char *fmt0)
  382. {
  383. #ifdef __UCLIBC_HAS_WCHAR__
  384. static const char invalid_mbs[] = "Invalid multibyte format string.";
  385. #endif /* __UCLIBC_HAS_WCHAR__ */
  386. int r;
  387. /* First, zero out everything... argnumber[], argtype[], argptr[] */
  388. memset(ppfs, 0, sizeof(ppfs_t)); /* TODO: nonportable???? */
  389. --ppfs->maxposarg; /* set to -1 */
  390. ppfs->fmtpos = fmt0;
  391. #ifdef __UCLIBC_HAS_WCHAR__
  392. {
  393. mbstate_t mbstate;
  394. const char *p;
  395. mbstate.mask = 0; /* Initialize the mbstate. */
  396. p = fmt0;
  397. if (mbsrtowcs(NULL, &p, SIZE_MAX, &mbstate) == ((size_t)(-1))) {
  398. ppfs->fmtpos = invalid_mbs;
  399. return -1;
  400. }
  401. }
  402. #endif /* __UCLIBC_HAS_WCHAR__ */
  403. /* now set all argtypes to no-arg */
  404. {
  405. #if 1
  406. /* TODO - use memset here since already "paid for"? */
  407. register int *p = ppfs->argtype;
  408. r = MAX_ARGS;
  409. do {
  410. *p++ = __PA_NOARG;
  411. } while (--r);
  412. #else
  413. /* TODO -- get rid of this?? */
  414. register char *p = (char *) ((MAX_ARGS-1) * sizeof(int));
  415. do {
  416. *((int *)(((char *)ppfs) + ((int)p) + offsetof(ppfs_t,argtype))) = __PA_NOARG;
  417. p -= sizeof(int);
  418. } while (p);
  419. #endif
  420. }
  421. /*
  422. * Run through the entire format string to validate it and initialize
  423. * the positional arg numbers (if any).
  424. */
  425. {
  426. register const char *fmt = fmt0;
  427. while (*fmt) {
  428. if ((*fmt == '%') && (*++fmt != '%')) {
  429. ppfs->fmtpos = fmt; /* back up to the '%' */
  430. if ((r = _ppfs_parsespec(ppfs)) < 0) {
  431. return -1;
  432. }
  433. fmt = ppfs->fmtpos; /* update to one past end of spec */
  434. } else {
  435. ++fmt;
  436. }
  437. }
  438. ppfs->fmtpos = fmt0; /* rewind */
  439. }
  440. /* If we have positional args, make sure we know all the types. */
  441. {
  442. register int *p = ppfs->argtype;
  443. r = ppfs->maxposarg;
  444. while (--r >= 0) {
  445. if ( *p == __PA_NOARG ) { /* missing arg type!!! */
  446. return -1;
  447. }
  448. ++p;
  449. }
  450. }
  451. return 0;
  452. }
  453. #endif
  454. /**********************************************************************/
  455. #ifdef L__ppfs_prepargs
  456. void _ppfs_prepargs(register ppfs_t *ppfs, va_list arg)
  457. {
  458. int i;
  459. va_copy(ppfs->arg, arg);
  460. if ((i = ppfs->maxposarg) > 0) { /* init for positional args */
  461. ppfs->num_data_args = i;
  462. ppfs->info.width = ppfs->info.prec = ppfs->maxposarg = 0;
  463. _ppfs_setargs(ppfs);
  464. ppfs->maxposarg = i;
  465. }
  466. }
  467. #endif
  468. /**********************************************************************/
  469. #ifdef L__ppfs_setargs
  470. void _ppfs_setargs(register ppfs_t *ppfs)
  471. {
  472. #ifdef __va_arg_ptr
  473. register void **p = ppfs->argptr;
  474. #else
  475. register argvalue_t *p = ppfs->argvalue;
  476. #endif
  477. int i;
  478. if (ppfs->maxposarg == 0) { /* initing for or no pos args */
  479. if (ppfs->info.width == INT_MIN) {
  480. ppfs->info.width =
  481. #ifdef __va_arg_ptr
  482. *(int *)
  483. #endif
  484. GET_VA_ARG(p,u,unsigned int,ppfs->arg);
  485. }
  486. if (ppfs->info.prec == INT_MIN) {
  487. ppfs->info.prec =
  488. #ifdef __va_arg_ptr
  489. *(int *)
  490. #endif
  491. GET_VA_ARG(p,u,unsigned int,ppfs->arg);
  492. }
  493. i = 0;
  494. while (i < ppfs->num_data_args) {
  495. switch(ppfs->argtype[i++]) {
  496. case (PA_INT|PA_FLAG_LONG_LONG):
  497. #ifdef ULLONG_MAX
  498. GET_VA_ARG(p,ull,unsigned long long,ppfs->arg);
  499. break;
  500. #endif
  501. case (PA_INT|PA_FLAG_LONG):
  502. #if ULONG_MAX != UINT_MAX
  503. GET_VA_ARG(p,ul,unsigned long,ppfs->arg);
  504. break;
  505. #endif
  506. case PA_CHAR: /* TODO - be careful */
  507. /* ... users could use above and really want below!! */
  508. case (PA_INT|__PA_FLAG_CHAR):/* TODO -- translate this!!! */
  509. case (PA_INT|PA_FLAG_SHORT):
  510. case PA_INT:
  511. GET_VA_ARG(p,u,unsigned int,ppfs->arg);
  512. break;
  513. case PA_WCHAR: /* TODO -- assume int? */
  514. /* we're assuming wchar_t is at least an int */
  515. GET_VA_ARG(p,wc,wchar_t,ppfs->arg);
  516. break;
  517. #ifdef __STDIO_PRINTF_FLOAT
  518. /* PA_FLOAT */
  519. case PA_DOUBLE:
  520. GET_VA_ARG(p,d,double,ppfs->arg);
  521. break;
  522. case (PA_DOUBLE|PA_FLAG_LONG_DOUBLE):
  523. GET_VA_ARG(p,ld,long double,ppfs->arg);
  524. break;
  525. #else /* __STDIO_PRINTF_FLOAT */
  526. case PA_DOUBLE:
  527. case (PA_DOUBLE|PA_FLAG_LONG_DOUBLE):
  528. assert(0);
  529. continue;
  530. #endif /* __STDIO_PRINTF_FLOAT */
  531. default:
  532. /* TODO -- really need to ensure this can't happen */
  533. assert(ppfs->argtype[i-1] & PA_FLAG_PTR);
  534. case PA_POINTER:
  535. case PA_STRING:
  536. case PA_WSTRING:
  537. GET_VA_ARG(p,p,void *,ppfs->arg);
  538. break;
  539. case __PA_NOARG:
  540. continue;
  541. }
  542. ++p;
  543. }
  544. } else {
  545. if (ppfs->info.width == INT_MIN) {
  546. ppfs->info.width
  547. = (int) GET_ARG_VALUE(p + ppfs->argnumber[0] - 1,u,unsigned int);
  548. }
  549. if (ppfs->info.prec == INT_MIN) {
  550. ppfs->info.prec
  551. = (int) GET_ARG_VALUE(p + ppfs->argnumber[1] - 1,u,unsigned int);
  552. }
  553. }
  554. /* Now we know the width and precision. */
  555. if (ppfs->info.width < 0) {
  556. ppfs->info.width = -ppfs->info.width;
  557. PRINT_INFO_SET_FLAG(&(ppfs->info),left);
  558. PRINT_INFO_CLR_FLAG(&(ppfs->info),space);
  559. ppfs->info.pad = ' ';
  560. }
  561. #if 0
  562. /* NOTE -- keep neg for now so float knows! */
  563. if (ppfs->info.prec < 0) { /* spec says treat as omitted. */
  564. /* so use default prec... 1 for everything but floats and strings. */
  565. ppfs->info.prec = 1;
  566. }
  567. #endif
  568. }
  569. #endif
  570. /**********************************************************************/
  571. #ifdef L__ppfs_parsespec
  572. /* Notes: argtype differs from glibc for the following:
  573. * mine glibc
  574. * lc PA_WCHAR PA_CHAR the standard says %lc means %C
  575. * ls PA_WSTRING PA_STRING the standard says %ls means %S
  576. * {*}n {*}|PA_FLAG_PTR PA_FLAG_PTR size of n can be qualified
  577. */
  578. /* TODO: store positions of positional args */
  579. /* TODO -- WARNING -- assumes aligned on integer boundaries!!! */
  580. /* TODO -- disable if not using positional args!!! */
  581. #define _OVERLAPPING_DIFFERENT_ARGS
  582. /* TODO -- rethink this -- perhaps we should set to largest type??? */
  583. #ifdef _OVERLAPPING_DIFFERENT_ARGS
  584. #define PROMOTED_SIZE_OF(X) ((sizeof(X) + sizeof(int) - 1) / sizeof(X))
  585. static const short int type_codes[] = {
  586. __PA_NOARG, /* must be first entry */
  587. PA_POINTER,
  588. PA_STRING,
  589. PA_WSTRING,
  590. PA_CHAR,
  591. PA_INT|PA_FLAG_SHORT,
  592. PA_INT,
  593. PA_INT|PA_FLAG_LONG,
  594. PA_INT|PA_FLAG_LONG_LONG,
  595. PA_WCHAR,
  596. #ifdef __STDIO_PRINTF_FLOAT
  597. /* PA_FLOAT, */
  598. PA_DOUBLE,
  599. PA_DOUBLE|PA_FLAG_LONG_DOUBLE,
  600. #endif /* __STDIO_PRINTF_FLOAT */
  601. };
  602. static const unsigned char type_sizes[] = {
  603. /* unknown type consumes no arg */
  604. 0, /* must be first entry */
  605. PROMOTED_SIZE_OF(void *),
  606. PROMOTED_SIZE_OF(char *),
  607. PROMOTED_SIZE_OF(wchar_t *),
  608. PROMOTED_SIZE_OF(char),
  609. PROMOTED_SIZE_OF(short),
  610. PROMOTED_SIZE_OF(int),
  611. PROMOTED_SIZE_OF(long),
  612. #ifdef ULLONG_MAX
  613. PROMOTED_SIZE_OF(long long),
  614. #else
  615. PROMOTED_SIZE_OF(long), /* TODO -- is this correct? (above too) */
  616. #endif
  617. PROMOTED_SIZE_OF(wchar_t),
  618. #ifdef __STDIO_PRINTF_FLOAT
  619. /* PROMOTED_SIZE_OF(float), */
  620. PROMOTED_SIZE_OF(double),
  621. PROMOTED_SIZE_OF(long double),
  622. #endif /* __STDIO_PRINTF_FLOAT */
  623. };
  624. static int _promoted_size(int argtype)
  625. {
  626. register const short int *p;
  627. /* note -- since any unrecognized type is treated as a pointer */
  628. p = type_codes + sizeof(type_codes)/sizeof(type_codes[0]);
  629. do {
  630. if (*--p == argtype) {
  631. break;
  632. }
  633. } while (p > type_codes);
  634. return type_sizes[(int)(p - type_codes)];
  635. }
  636. static int _is_equal_or_bigger_arg(int curtype, int newtype)
  637. {
  638. /* Quick test */
  639. if (newtype == __PA_NOARG) {
  640. return 0;
  641. }
  642. if ((curtype == __PA_NOARG) || (curtype == newtype)) {
  643. return 1;
  644. }
  645. /* Ok... slot is already filled and types are different in name. */
  646. /* So, compare promoted sizes of curtype and newtype args. */
  647. return _promoted_size(curtype) <= _promoted_size(newtype);
  648. }
  649. #else
  650. #define _is_equal_or_bigger_arg(C,N) (((C) == __PA_NOARG) || ((C) == (N)))
  651. #endif
  652. /* TODO - do this differently? */
  653. static char _bss_custom_printf_spec[MAX_USER_SPEC]; /* 0-init'd for us. */
  654. char *_custom_printf_spec = _bss_custom_printf_spec;
  655. printf_arginfo_function *_custom_printf_arginfo[MAX_USER_SPEC];
  656. printf_function _custom_printf_handler[MAX_USER_SPEC];
  657. extern int _ppfs_parsespec(ppfs_t *ppfs)
  658. {
  659. register const char *fmt;
  660. register const char *p;
  661. int preci;
  662. int width;
  663. int flags;
  664. int dataargtype;
  665. int i;
  666. int dpoint;
  667. int maxposarg;
  668. int p_m_spec_chars;
  669. int n;
  670. int argtype[MAX_ARGS_PER_SPEC+2];
  671. int argnumber[3]; /* width, precision, 1st data arg */
  672. unsigned int conv_num; /* This does not need to be initialized. */
  673. static const char spec_flags[] = SPEC_FLAGS;
  674. static const char spec_chars[] = SPEC_CHARS;/* TODO: b? */
  675. static const char spec_ranges[] = SPEC_RANGES;
  676. static const short spec_or_mask[] = SPEC_OR_MASK;
  677. static const short spec_and_mask[] = SPEC_AND_MASK;
  678. static const char qual_chars[] = QUAL_CHARS;
  679. #ifdef __UCLIBC_HAS_WCHAR__
  680. char buf[32];
  681. #endif /* __UCLIBC_HAS_WCHAR__ */
  682. /* WIDE note: we can test against '%' here since we don't allow */
  683. /* WIDE note: other mappings of '%' in the wide char set. */
  684. preci = -1;
  685. argnumber[0] = 0;
  686. argnumber[1] = 0;
  687. argtype[0] = __PA_NOARG;
  688. argtype[1] = __PA_NOARG;
  689. maxposarg = ppfs->maxposarg;
  690. #ifdef __UCLIBC_HAS_WCHAR__
  691. /* This is somewhat lame, but saves a lot of code. If we're dealing with
  692. * a wide stream, that means the format is a wchar string. So, copy it
  693. * char-by-char into a normal char buffer for processing. Make the buffer
  694. * (buf) big enough so that any reasonable format specifier will fit.
  695. * While there a legal specifiers that won't, the all involve duplicate
  696. * flags or outrageous field widths/precisions. */
  697. width = dpoint = 0;
  698. if ((flags = ppfs->info._flags & FLAG_WIDESTREAM) == 0) {
  699. fmt = ppfs->fmtpos;
  700. } else {
  701. fmt = buf + 1;
  702. i = 0;
  703. do {
  704. if ((buf[i] = (char) (((wchar_t *) ppfs->fmtpos)[i-1]))
  705. != (((wchar_t *) ppfs->fmtpos)[i-1])
  706. ) {
  707. return -1;
  708. }
  709. } while (buf[i++]);
  710. buf[sizeof(buf)-1] = 0;
  711. }
  712. #else /* __UCLIBC_HAS_WCHAR__ */
  713. width = flags = dpoint = 0;
  714. fmt = ppfs->fmtpos;
  715. #endif /* __UCLIBC_HAS_WCHAR__ */
  716. assert(fmt[-1] == '%');
  717. assert(fmt[0] != '%');
  718. /* Process arg pos and/or flags and/or width and/or precision. */
  719. width_precision:
  720. p = fmt;
  721. if (*fmt == '*') {
  722. argtype[-dpoint] = PA_INT;
  723. ++fmt;
  724. }
  725. i = 0;
  726. while (__isdigit(*fmt)) {
  727. if (i < MAX_FIELD_WIDTH) { /* Avoid overflow. */
  728. i = (i * 10) + (*fmt - '0');
  729. }
  730. ++fmt;
  731. }
  732. if (p[-1] == '%') { /* Check for a position. */
  733. /* TODO: if val not in range, then error */
  734. if ((*fmt == '$') && (i > 0)) {/* Positional spec. */
  735. ++fmt;
  736. if (maxposarg == 0) {
  737. return -1;
  738. }
  739. if ((argnumber[2] = i) > maxposarg) {
  740. maxposarg = i;
  741. }
  742. /* Now fall through to check flags. */
  743. } else {
  744. if (maxposarg > 0) {
  745. #ifdef __STDIO_PRINTF_M_SUPPORT
  746. #ifdef __UCLIBC_MJN3_ONLY__
  747. #warning TODO: Support prec and width for %m when positional args used
  748. /* Actually, positional arg processing will fail in general
  749. * for specifiers that don't require an arg. */
  750. #endif
  751. if (*fmt == 'm') {
  752. goto PREC_WIDTH;
  753. }
  754. #endif /* __STDIO_PRINTF_M_SUPPORT */
  755. return -1;
  756. }
  757. maxposarg = 0; /* Possible redundant store, but cuts size. */
  758. if ((fmt > p) && (*p != '0')) {
  759. goto PREC_WIDTH;
  760. }
  761. fmt = p; /* Back up for possible '0's flag. */
  762. /* Now fall through to check flags. */
  763. }
  764. restart_flags: /* Process flags. */
  765. i = 1;
  766. p = spec_flags;
  767. do {
  768. if (*fmt == *p++) {
  769. ++fmt;
  770. flags |= i;
  771. goto restart_flags;
  772. }
  773. i += i; /* Better than i <<= 1 for bcc */
  774. } while (*p);
  775. i = 0;
  776. /* If '+' then ignore ' ', and if '-' then ignore '0'. */
  777. /* Note: Need to ignore '0' when prec is an arg with val < 0, */
  778. /* but that test needs to wait until the arg is retrieved. */
  779. flags &= ~((flags & (FLAG_PLUS|FLAG_MINUS)) >> 1);
  780. /* Note: Ignore '0' when prec is specified < 0 too (in printf). */
  781. if (fmt[-1] != '%') { /* If we've done anything, loop for width. */
  782. goto width_precision;
  783. }
  784. }
  785. PREC_WIDTH:
  786. if (*p == '*') { /* Prec or width takes an arg. */
  787. if (maxposarg) {
  788. if ((*fmt++ != '$') || (i <= 0)) {
  789. /* Using pos args and no $ or invalid arg number. */
  790. return -1;
  791. }
  792. argnumber[-dpoint] = i;
  793. } else if (++p != fmt) {
  794. /* Not using pos args but digits followed *. */
  795. return -1;
  796. }
  797. i = INT_MIN;
  798. }
  799. if (!dpoint) {
  800. width = i;
  801. if (*fmt == '.') {
  802. ++fmt;
  803. dpoint = -1; /* To use as default precison. */
  804. goto width_precision;
  805. }
  806. } else {
  807. preci = i;
  808. }
  809. /* Process qualifier. */
  810. p = qual_chars;
  811. do {
  812. if (*fmt == *p) {
  813. ++fmt;
  814. break;
  815. }
  816. } while (*++p);
  817. if ((p - qual_chars < 2) && (*fmt == *p)) {
  818. p += ((sizeof(qual_chars)-2) / 2);
  819. ++fmt;
  820. }
  821. dataargtype = ((int)(p[(sizeof(qual_chars)-2) / 2])) << 8;
  822. /* Process conversion specifier. */
  823. if (!*fmt) {
  824. return -1;
  825. }
  826. p = spec_chars;
  827. do {
  828. if (*fmt == *p) {
  829. p_m_spec_chars = p - spec_chars;
  830. if ((p_m_spec_chars >= CONV_c)
  831. && (dataargtype & PA_FLAG_LONG)) {
  832. p_m_spec_chars -= 2; /* lc -> C and ls -> S */
  833. }
  834. conv_num = p_m_spec_chars;
  835. p = spec_ranges-1;
  836. while (p_m_spec_chars > *++p) {}
  837. i = p - spec_ranges;
  838. argtype[2] = (dataargtype | spec_or_mask[i]) & spec_and_mask[i];
  839. p = spec_chars;
  840. break;
  841. }
  842. } while(*++p);
  843. ppfs->info.spec = *fmt;
  844. ppfs->info.prec = preci;
  845. ppfs->info.width = width;
  846. ppfs->info.pad = ((flags & FLAG_ZERO) ? '0' : ' ');
  847. ppfs->info._flags = (flags & ~FLAG_ZERO) | (dataargtype & __PA_INTMASK);
  848. ppfs->num_data_args = 1;
  849. if (!*p) {
  850. #ifdef __STDIO_GLIBC_CUSTOM_PRINTF
  851. /* TODO -- gnu %m support build option. */
  852. #ifdef __STDIO_PRINTF_M_SUPPORT
  853. if (*fmt == 'm') {
  854. conv_num = CONV_m;
  855. ppfs->num_data_args = 0;
  856. goto DONE;
  857. }
  858. #endif
  859. /* Handle custom arg -- WARNING -- overwrites p!!! */
  860. conv_num = CONV_custom0;
  861. p = _custom_printf_spec;
  862. do {
  863. if (*p == *fmt) {
  864. if ((ppfs->num_data_args
  865. = ((*_custom_printf_arginfo[(int)(p-_custom_printf_spec)])
  866. (&(ppfs->info), MAX_ARGS_PER_SPEC, argtype+2)))
  867. > MAX_ARGS_PER_SPEC) {
  868. break; /* Error -- too many args! */
  869. }
  870. goto DONE;
  871. }
  872. } while (++p < (_custom_printf_spec + MAX_USER_SPEC));
  873. #endif /* __STDIO_GLIBC_CUSTOM_PRINTF */
  874. /* Otherwise error. */
  875. return -1;
  876. }
  877. #ifdef __STDIO_GLIBC_CUSTOM_PRINTF
  878. DONE:
  879. #endif
  880. if (maxposarg > 0) {
  881. i = 0;
  882. do {
  883. /* Update maxposarg and check that NL_ARGMAX is not exceeded. */
  884. n = ((i <= 2)
  885. ? (ppfs->argnumber[i] = argnumber[i])
  886. : argnumber[2] + (i-2));
  887. if (n > maxposarg) {
  888. if ((maxposarg = n) > NL_ARGMAX) {
  889. return -1;
  890. }
  891. }
  892. --n;
  893. /* Record argtype with largest size (current, new). */
  894. if (_is_equal_or_bigger_arg(ppfs->argtype[n], argtype[i])) {
  895. ppfs->argtype[n] = argtype[i];
  896. }
  897. } while (++i < ppfs->num_data_args + 2);
  898. } else {
  899. ppfs->argnumber[2] = 1;
  900. memcpy(ppfs->argtype, argtype + 2, ppfs->num_data_args * sizeof(int));
  901. }
  902. ppfs->maxposarg = maxposarg;
  903. ppfs->conv_num = conv_num;
  904. #ifdef __UCLIBC_HAS_WCHAR__
  905. if ((flags = ppfs->info._flags & FLAG_WIDESTREAM) == 0) {
  906. ppfs->fmtpos = ++fmt;
  907. } else {
  908. ppfs->fmtpos = (const char *) (((const wchar_t *)(ppfs->fmtpos))
  909. + (fmt - buf) );
  910. }
  911. #else /* __UCLIBC_HAS_WCHAR__ */
  912. ppfs->fmtpos = ++fmt;
  913. #endif /* __UCLIBC_HAS_WCHAR__ */
  914. return ppfs->num_data_args + 2;
  915. }
  916. #endif
  917. /**********************************************************************/
  918. #ifdef L_register_printf_function
  919. int register_printf_function(int spec, printf_function handler,
  920. printf_arginfo_function arginfo)
  921. {
  922. register char *r;
  923. register char *p;
  924. if (spec && (arginfo != NULL)) { /* TODO -- check if spec is valid char */
  925. r = NULL;
  926. p = _custom_printf_spec + MAX_USER_SPEC;
  927. do {
  928. --p;
  929. if (!*p) {
  930. r = p;
  931. }
  932. #ifdef __BCC__
  933. else /* bcc generates less code with fall-through */
  934. #endif
  935. if (*p == spec) {
  936. r = p;
  937. p = _custom_printf_spec;
  938. }
  939. } while (p > _custom_printf_spec);
  940. if (r) {
  941. if (handler) {
  942. *r = spec;
  943. _custom_printf_handler[(int)(r - p)] = handler;
  944. _custom_printf_arginfo[(int)(r - p)] = arginfo;
  945. } else {
  946. *r = 0;
  947. }
  948. return 0;
  949. }
  950. /* TODO -- if asked to unregister a non-existent spec, return what? */
  951. }
  952. return -1;
  953. }
  954. #endif
  955. /**********************************************************************/
  956. #ifdef L_vsnprintf
  957. #ifdef __STDIO_BUFFERS
  958. int vsnprintf(char *__restrict buf, size_t size,
  959. const char * __restrict format, va_list arg)
  960. {
  961. FILE f;
  962. int rv;
  963. #ifdef __STDIO_GETC_MACRO
  964. f.bufgetc =
  965. #endif
  966. f.bufpos = f.bufread = f.bufstart = buf;
  967. if (size > SIZE_MAX - (size_t) buf) {
  968. size = SIZE_MAX - (size_t) buf;
  969. }
  970. #ifdef __STDIO_PUTC_MACRO
  971. f.bufputc =
  972. #endif
  973. f.bufend = buf + size;
  974. #if 0 /* shouldn't be necessary */
  975. /* #ifdef __STDIO_GLIBC_CUSTOM_STREAMS */
  976. f.cookie = &(f.filedes);
  977. f.gcs.read = 0;
  978. f.gcs.write = 0;
  979. f.gcs.seek = 0;
  980. f.gcs.close = 0;
  981. #endif
  982. f.filedes = -2; /* for debugging */
  983. f.modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);
  984. #ifdef __STDIO_MBSTATE
  985. __INIT_MBSTATE(&(f.state));
  986. #endif /* __STDIO_MBSTATE */
  987. #ifdef __STDIO_THREADSAFE
  988. f.user_locking = 0;
  989. __stdio_init_mutex(&f.lock);
  990. #endif
  991. rv = vfprintf(&f, format, arg);
  992. if (size) {
  993. if (f.bufpos == f.bufend) {
  994. --f.bufpos;
  995. }
  996. *f.bufpos = 0;
  997. }
  998. return rv;
  999. }
  1000. #else /* __STDIO_BUFFERS */
  1001. #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
  1002. typedef struct {
  1003. size_t pos;
  1004. size_t len;
  1005. unsigned char *buf;
  1006. FILE *fp;
  1007. } __snpf_cookie;
  1008. #define COOKIE ((__snpf_cookie *) cookie)
  1009. static ssize_t snpf_write(register void *cookie, const char *buf,
  1010. size_t bufsize)
  1011. {
  1012. size_t count;
  1013. register char *p;
  1014. /* Note: bufsize < SSIZE_MAX because of _stdio_WRITE. */
  1015. if (COOKIE->len > COOKIE->pos) {
  1016. count = COOKIE->len - COOKIE->pos - 1; /* Leave space for nul. */
  1017. if (count > bufsize) {
  1018. count = bufsize;
  1019. }
  1020. p = COOKIE->buf + COOKIE->pos;
  1021. while (count) {
  1022. *p++ = *buf++;
  1023. --count;
  1024. }
  1025. *p = 0;
  1026. }
  1027. COOKIE->pos += bufsize;
  1028. return bufsize;
  1029. }
  1030. #undef COOKIE
  1031. int vsnprintf(char *__restrict buf, size_t size,
  1032. const char * __restrict format, va_list arg)
  1033. {
  1034. FILE f;
  1035. __snpf_cookie cookie;
  1036. int rv;
  1037. cookie.buf = buf;
  1038. cookie.len = size;
  1039. cookie.pos = 0;
  1040. cookie.fp = &f;
  1041. f.cookie = &cookie;
  1042. f.gcs.write = snpf_write;
  1043. f.gcs.read = NULL;
  1044. f.gcs.seek = NULL;
  1045. f.gcs.close = NULL;
  1046. f.filedes = -1; /* For debugging. */
  1047. f.modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);
  1048. #ifdef __STDIO_MBSTATE
  1049. __INIT_MBSTATE(&(f.state));
  1050. #endif /* __STDIO_MBSTATE */
  1051. #ifdef __STDIO_THREADSAFE
  1052. f.user_locking = 0;
  1053. __stdio_init_mutex(&f.lock);
  1054. #endif
  1055. rv = vfprintf(&f, format, arg);
  1056. return rv;
  1057. }
  1058. #else /* __STDIO_GLIBC_CUSTOM_STREAMS */
  1059. #warning skipping vsnprintf since no buffering and no custom streams!
  1060. #endif /* __STDIO_GLIBC_CUSTOM_STREAMS */
  1061. #endif /* __STDIO_BUFFERS */
  1062. #endif
  1063. /**********************************************************************/
  1064. #ifdef L_vdprintf
  1065. int vdprintf(int filedes, const char * __restrict format, va_list arg)
  1066. {
  1067. FILE f;
  1068. int rv;
  1069. #ifdef __STDIO_BUFFERS
  1070. char buf[64]; /* TODO: provide _optional_ buffering? */
  1071. #ifdef __STDIO_GETC_MACRO
  1072. f.bufgetc =
  1073. #endif
  1074. f.bufpos = f.bufread = f.bufstart = buf;
  1075. #ifdef __STDIO_PUTC_MACRO
  1076. f.bufputc =
  1077. #endif
  1078. f.bufend = buf + sizeof(buf);
  1079. #endif /* __STDIO_BUFFERS */
  1080. #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
  1081. f.cookie = &(f.filedes);
  1082. f.gcs.read = _cs_read;
  1083. f.gcs.write = _cs_write;
  1084. f.gcs.seek = 0; /* The internal seek func handles normals. */
  1085. f.gcs.close = _cs_close;
  1086. #endif
  1087. f.filedes = filedes;
  1088. f.modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);
  1089. #ifdef __STDIO_MBSTATE
  1090. __INIT_MBSTATE(&(f.state));
  1091. #endif /* __STDIO_MBSTATE */
  1092. #ifdef __STDIO_THREADSAFE
  1093. f.user_locking = 0;
  1094. __stdio_init_mutex(&f.lock);
  1095. #endif
  1096. rv = vfprintf(&f, format, arg);
  1097. return fflush(&f) ? -1 : rv;
  1098. }
  1099. #endif
  1100. /**********************************************************************/
  1101. #ifdef L_vasprintf
  1102. #if !defined(__STDIO_BUFFERS) && !defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  1103. #warning skipping vasprintf since no buffering and no custom streams!
  1104. #else
  1105. int vasprintf(char **__restrict buf, const char * __restrict format,
  1106. va_list arg)
  1107. {
  1108. /* TODO -- change the default implementation? */
  1109. #ifndef __STDIO_GLIBC_CUSTOM_STREAMS
  1110. /* This implementation actually calls the printf machinery twice, but only
  1111. * only does one malloc. This can be a problem though when custom printf
  1112. * specs or the %m specifier are involved because the results of the
  1113. * second call might be different from the first. */
  1114. va_list arg2;
  1115. int rv;
  1116. va_copy(arg2, arg);
  1117. rv = vsnprintf(NULL, 0, format, arg2);
  1118. va_end(arg2);
  1119. return (((rv >= 0) && ((*buf = malloc(++rv)) != NULL))
  1120. ? vsnprintf(*buf, rv, format, arg)
  1121. : -1);
  1122. #else
  1123. FILE *f;
  1124. size_t size;
  1125. int rv;
  1126. /* TODO - do the memstream stuff inline here to avoid fclose and the openlist? */
  1127. if ((f = open_memstream(buf, &size)) == NULL) {
  1128. return -1;
  1129. }
  1130. rv = vfprintf(f, format, arg);
  1131. fclose(f);
  1132. if (rv < 0) {
  1133. free(*buf);
  1134. *buf = NULL;
  1135. return -1;
  1136. }
  1137. return rv;
  1138. #endif
  1139. }
  1140. #endif
  1141. #endif
  1142. /**********************************************************************/
  1143. #ifdef L_vprintf
  1144. int vprintf(const char * __restrict format, va_list arg)
  1145. {
  1146. return vfprintf(stdout, format, arg);
  1147. }
  1148. #endif
  1149. /**********************************************************************/
  1150. #ifdef L_vsprintf
  1151. #if !defined(__STDIO_BUFFERS) && !defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  1152. #warning skipping vsprintf since no buffering and no custom streams!
  1153. #else
  1154. int vsprintf(char *__restrict buf, const char * __restrict format,
  1155. va_list arg)
  1156. {
  1157. return vsnprintf(buf, SIZE_MAX, format, arg);
  1158. }
  1159. #endif
  1160. #endif
  1161. /**********************************************************************/
  1162. #ifdef L_fprintf
  1163. int fprintf(FILE * __restrict stream, const char * __restrict format, ...)
  1164. {
  1165. va_list arg;
  1166. int rv;
  1167. va_start(arg, format);
  1168. rv = vfprintf(stream, format, arg);
  1169. va_end(arg);
  1170. return rv;
  1171. }
  1172. #endif
  1173. /**********************************************************************/
  1174. #ifdef L_snprintf
  1175. #if !defined(__STDIO_BUFFERS) && !defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  1176. #warning skipping snprintf since no buffering and no custom streams!
  1177. #else
  1178. int snprintf(char *__restrict buf, size_t size,
  1179. const char * __restrict format, ...)
  1180. {
  1181. va_list arg;
  1182. int rv;
  1183. va_start(arg, format);
  1184. rv = vsnprintf(buf, size, format, arg);
  1185. va_end(arg);
  1186. return rv;
  1187. }
  1188. #endif
  1189. #endif
  1190. /**********************************************************************/
  1191. #ifdef L_dprintf
  1192. int dprintf(int filedes, const char * __restrict format, ...)
  1193. {
  1194. va_list arg;
  1195. int rv;
  1196. va_start(arg, format);
  1197. rv = vdprintf(filedes, format, arg);
  1198. va_end(arg);
  1199. return rv;
  1200. }
  1201. #endif
  1202. /**********************************************************************/
  1203. #ifdef L_asprintf
  1204. #if !defined(__STDIO_BUFFERS) && !defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  1205. #warning skipping asprintf and __asprintf since no buffering and no custom streams!
  1206. #else
  1207. weak_alias(__asprintf,asprintf)
  1208. int __asprintf(char **__restrict buf, const char * __restrict format, ...)
  1209. {
  1210. va_list arg;
  1211. int rv;
  1212. va_start(arg, format);
  1213. rv = vasprintf(buf, format, arg);
  1214. va_end(arg);
  1215. return rv;
  1216. }
  1217. #endif
  1218. #endif
  1219. /**********************************************************************/
  1220. #ifdef L_printf
  1221. int printf(const char * __restrict format, ...)
  1222. {
  1223. va_list arg;
  1224. int rv;
  1225. va_start(arg, format);
  1226. rv = vfprintf(stdout, format, arg);
  1227. va_end(arg);
  1228. return rv;
  1229. }
  1230. #endif
  1231. /**********************************************************************/
  1232. #ifdef L_sprintf
  1233. #if !defined(__STDIO_BUFFERS) && !defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  1234. #warning skipping sprintf since no buffering and no custom streams!
  1235. #else
  1236. int sprintf(char *__restrict buf, const char * __restrict format, ...)
  1237. {
  1238. va_list arg;
  1239. int rv;
  1240. va_start(arg, format);
  1241. rv = vsnprintf(buf, SIZE_MAX, format, arg);
  1242. va_end(arg);
  1243. return rv;
  1244. }
  1245. #endif
  1246. #endif
  1247. /**********************************************************************/
  1248. #ifdef L_vswprintf
  1249. #ifdef __STDIO_BUFFERS
  1250. int vswprintf(wchar_t *__restrict buf, size_t size,
  1251. const wchar_t * __restrict format, va_list arg)
  1252. {
  1253. FILE f;
  1254. int rv;
  1255. #ifdef __STDIO_GETC_MACRO
  1256. f.bufgetc =
  1257. #endif
  1258. f.bufpos = f.bufread = f.bufstart = (char *) buf;
  1259. /* if (size > SIZE_MAX - (size_t) buf) { */
  1260. /* size = SIZE_MAX - (size_t) buf; */
  1261. /* } */
  1262. #ifdef __STDIO_PUTC_MACRO
  1263. f.bufputc =
  1264. #endif
  1265. f.bufend = (char *)(buf + size);
  1266. #if 0 /* shouldn't be necessary */
  1267. /* #ifdef __STDIO_GLIBC_CUSTOM_STREAMS */
  1268. f.cookie = &(f.filedes);
  1269. f.gcs.read = 0;
  1270. f.gcs.write = 0;
  1271. f.gcs.seek = 0;
  1272. f.gcs.close = 0;
  1273. #endif
  1274. f.filedes = -3; /* for debugging */
  1275. f.modeflags = (__FLAG_WIDE|__FLAG_WRITEONLY|__FLAG_WRITING);
  1276. #ifdef __STDIO_MBSTATE
  1277. __INIT_MBSTATE(&(f.state));
  1278. #endif /* __STDIO_MBSTATE */
  1279. #ifdef __STDIO_THREADSAFE
  1280. f.user_locking = 0;
  1281. __stdio_init_mutex(&f.lock);
  1282. #endif
  1283. rv = vfwprintf(&f, format, arg);
  1284. /* NOTE: Return behaviour differs from snprintf... */
  1285. if (f.bufpos == f.bufend) {
  1286. rv = -1;
  1287. if (size) {
  1288. f.bufpos = (char *)(((wchar_t *) f.bufpos) - 1);
  1289. }
  1290. }
  1291. if (size) {
  1292. *((wchar_t *) f.bufpos) = 0;
  1293. }
  1294. return rv;
  1295. }
  1296. #else /* __STDIO_BUFFERS */
  1297. #warning skipping vswprintf since no buffering!
  1298. #endif /* __STDIO_BUFFERS */
  1299. #endif
  1300. /**********************************************************************/
  1301. #ifdef L_swprintf
  1302. #ifdef __STDIO_BUFFERS
  1303. int swprintf(wchar_t *__restrict buf, size_t size,
  1304. const wchar_t * __restrict format, ...)
  1305. {
  1306. va_list arg;
  1307. int rv;
  1308. va_start(arg, format);
  1309. rv = vswprintf(buf, size, format, arg);
  1310. va_end(arg);
  1311. return rv;
  1312. }
  1313. #else /* __STDIO_BUFFERS */
  1314. #warning skipping vsWprintf since no buffering!
  1315. #endif /* __STDIO_BUFFERS */
  1316. #endif
  1317. /**********************************************************************/
  1318. #ifdef L_fwprintf
  1319. int fwprintf(FILE * __restrict stream, const wchar_t * __restrict format, ...)
  1320. {
  1321. va_list arg;
  1322. int rv;
  1323. va_start(arg, format);
  1324. rv = vfwprintf(stream, format, arg);
  1325. va_end(arg);
  1326. return rv;
  1327. }
  1328. #endif
  1329. /**********************************************************************/
  1330. #ifdef L_vwprintf
  1331. int vwprintf(const wchar_t * __restrict format, va_list arg)
  1332. {
  1333. return vfwprintf(stdout, format, arg);
  1334. }
  1335. #endif
  1336. /**********************************************************************/
  1337. #ifdef L_wprintf
  1338. int wprintf(const wchar_t * __restrict format, ...)
  1339. {
  1340. va_list arg;
  1341. int rv;
  1342. va_start(arg, format);
  1343. rv = vfwprintf(stdout, format, arg);
  1344. va_end(arg);
  1345. return rv;
  1346. }
  1347. #endif
  1348. /**********************************************************************/
  1349. #ifdef L__dtostr
  1350. /*
  1351. * Copyright (C) 2000, 2001 Manuel Novoa III
  1352. *
  1353. * Function: size_t _dtostr(FILE *fp, long double x, struct printf_info *info)
  1354. *
  1355. * This was written for uClibc to provide floating point support for
  1356. * the printf functions. It handles +/- infinity and nan on i386.
  1357. *
  1358. * Notes:
  1359. *
  1360. * At most MAX_DIGITS significant digits are kept. Any trailing digits
  1361. * are treated as 0 as they are really just the results of rounding noise
  1362. * anyway. If you want to do better, use an arbitary precision arithmetic
  1363. * package. ;-)
  1364. *
  1365. * It should also be fairly portable, as not assumptions are made about the
  1366. * bit-layout of doubles.
  1367. *
  1368. * It should be too difficult to convert this to handle long doubles on i386.
  1369. * For information, see the comments below.
  1370. *
  1371. * TODO:
  1372. * long double and/or float version? (note: for float can trim code some).
  1373. *
  1374. * Decrease the size. This is really much bigger than I'd like.
  1375. */
  1376. /*****************************************************************************/
  1377. /* Don't change anything that follows unless you know what you're doing. */
  1378. /*****************************************************************************/
  1379. /*
  1380. * Configuration for the scaling power table. Ignoring denormals, you
  1381. * should have 2**EXP_TABLE_SIZE >= LDBL_MAX_EXP >= 2**(EXP_TABLE_SIZE-1).
  1382. * The minimum for standard C is 6. For IEEE 8bit doubles, 9 suffices.
  1383. * For long doubles on i386, use 13.
  1384. */
  1385. #define EXP_TABLE_SIZE 13
  1386. /*
  1387. * Set this to the maximum number of digits you want converted.
  1388. * Conversion is done in blocks of DIGITS_PER_BLOCK (9 by default) digits.
  1389. * (20) 17 digits suffices to uniquely determine a (long) double on i386.
  1390. */
  1391. #define MAX_DIGITS 20
  1392. /*
  1393. * Set this to the smallest integer type capable of storing a pointer.
  1394. */
  1395. #define INT_OR_PTR int
  1396. /*
  1397. * This is really only used to check for infinities. The macro produces
  1398. * smaller code for i386 and, since this is tested before any floating point
  1399. * calculations, it doesn't appear to suffer from the excess precision problem
  1400. * caused by the FPU that strtod had. If it causes problems, call the function
  1401. * and compile zoicheck.c with -ffloat-store.
  1402. */
  1403. #define _zero_or_inf_check(x) ( x == (x/4) )
  1404. /*
  1405. * Fairly portable nan check. Bitwise for i386 generated larger code.
  1406. * If you have a better version, comment this out.
  1407. */
  1408. #define isnan(x) (x != x)
  1409. /*****************************************************************************/
  1410. /* Don't change anything that follows peroid!!! ;-) */
  1411. /*****************************************************************************/
  1412. #include <float.h>
  1413. /*****************************************************************************/
  1414. /*
  1415. * Set things up for the scaling power table.
  1416. */
  1417. #if EXP_TABLE_SIZE < 6
  1418. #error EXP_TABLE_SIZE should be at least 6 to comply with standards
  1419. #endif
  1420. #define EXP_TABLE_MAX (1U<<(EXP_TABLE_SIZE-1))
  1421. /*
  1422. * Only bother checking if this is too small.
  1423. */
  1424. #if LDBL_MAX_10_EXP/2 > EXP_TABLE_MAX
  1425. #error larger EXP_TABLE_SIZE needed
  1426. #endif
  1427. /*
  1428. * With 32 bit ints, we can get 9 digits per block.
  1429. */
  1430. #define DIGITS_PER_BLOCK 9
  1431. #if INT_MAX >= 2147483647L
  1432. #define DIGIT_BLOCK_TYPE int
  1433. #define DB_FMT "%.*d"
  1434. #elif LONG_MAX >= 2147483647L
  1435. #define DIGIT_BLOCK_TYPE long
  1436. #define DB_FMT "%.*ld"
  1437. #else
  1438. #warning need at least 32 bit longs
  1439. #endif
  1440. /* Maximum number of calls to fnprintf to output double. */
  1441. #define MAX_CALLS 8
  1442. /*****************************************************************************/
  1443. #define NUM_DIGIT_BLOCKS ((MAX_DIGITS+DIGITS_PER_BLOCK-1)/DIGITS_PER_BLOCK)
  1444. /* extra space for '-', '.', 'e+###', and nul */
  1445. #define BUF_SIZE ( 3 + NUM_DIGIT_BLOCKS * DIGITS_PER_BLOCK )
  1446. /*****************************************************************************/
  1447. static const char *fmts[] = {
  1448. "%0*d", "%.*s", ".", "inf", "INF", "nan", "NAN", "%*s"
  1449. };
  1450. /*****************************************************************************/
  1451. #include <locale.h>
  1452. #ifdef __UCLIBC_MJN3_ONLY__
  1453. #warning REMINDER: implement grouping for floating point
  1454. #endif
  1455. #ifndef __LOCALE_C_ONLY
  1456. #define CUR_LOCALE (__global_locale)
  1457. #endif /* __LOCALE_C_ONLY */
  1458. size_t _dtostr(FILE * fp, long double x, struct printf_info *info)
  1459. {
  1460. long double exp_table[EXP_TABLE_SIZE];
  1461. long double p10;
  1462. DIGIT_BLOCK_TYPE digit_block; /* int of at least 32 bits */
  1463. int i, j;
  1464. int round, o_exp;
  1465. int exp, exp_neg;
  1466. int width, preci;
  1467. char *s;
  1468. char *e;
  1469. char buf[BUF_SIZE];
  1470. INT_OR_PTR pc_fwi[2*MAX_CALLS];
  1471. INT_OR_PTR *ppc;
  1472. char exp_buf[8];
  1473. char drvr[8];
  1474. char *pdrvr;
  1475. int npc;
  1476. int cnt;
  1477. char sign_str[2];
  1478. char o_mode;
  1479. char mode;
  1480. /* check that INT_OR_PTR is sufficiently large */
  1481. assert( sizeof(INT_OR_PTR) == sizeof(char *) );
  1482. width = info->width;
  1483. preci = info->prec;
  1484. mode = info->spec;
  1485. if (mode == 'a') {
  1486. mode = 'g'; /* TODO -- fix */
  1487. }
  1488. if (mode == 'A') {
  1489. mode = 'G'; /* TODO -- fix */
  1490. }
  1491. if (preci < 0) {
  1492. preci = 6;
  1493. }
  1494. *sign_str = '\0';
  1495. if (PRINT_INFO_FLAG_VAL(info,showsign)) {
  1496. *sign_str = '+';
  1497. } else if (PRINT_INFO_FLAG_VAL(info,space)) {
  1498. *sign_str = ' ';
  1499. }
  1500. /* *sign_str = flag[FLAG_PLUS]; */
  1501. *(sign_str+1) = 0;
  1502. if (isnan(x)) { /* nan check */
  1503. pdrvr = drvr + 1;
  1504. *pdrvr++ = 5 + (mode < 'a');
  1505. pc_fwi[2] = 3;
  1506. info->pad = ' ';
  1507. /* flag[FLAG_0_PAD] = 0; */
  1508. goto EXIT_SPECIAL;
  1509. }
  1510. if (x == 0) { /* handle 0 now to avoid false positive */
  1511. exp = -1;
  1512. goto GENERATE_DIGITS;
  1513. }
  1514. if (x < 0) { /* convert negatives to positives */
  1515. *sign_str = '-';
  1516. x = -x;
  1517. }
  1518. if (_zero_or_inf_check(x)) { /* must be inf since zero handled above */
  1519. pdrvr = drvr + 1;
  1520. *pdrvr++ = 3 + + (mode < 'a');
  1521. pc_fwi[2] = 3;
  1522. info->pad = ' ';
  1523. /* flag[FLAG_0_PAD] = 0; */
  1524. goto EXIT_SPECIAL;
  1525. }
  1526. /* need to build the scaling table */
  1527. for (i = 0, p10 = 10 ; i < EXP_TABLE_SIZE ; i++) {
  1528. exp_table[i] = p10;
  1529. p10 *= p10;
  1530. }
  1531. exp_neg = 0;
  1532. if (x < 1e8) { /* do we need to scale up or down? */
  1533. exp_neg = 1;
  1534. }
  1535. exp = DIGITS_PER_BLOCK - 1;
  1536. i = EXP_TABLE_SIZE;
  1537. j = EXP_TABLE_MAX;
  1538. while ( i-- ) { /* scale x such that 1e8 <= x < 1e9 */
  1539. if (exp_neg) {
  1540. if (x * exp_table[i] < 1e9) {
  1541. x *= exp_table[i];
  1542. exp -= j;
  1543. }
  1544. } else {
  1545. if (x / exp_table[i] >= 1e8) {
  1546. x /= exp_table[i];
  1547. exp += j;
  1548. }
  1549. }
  1550. j >>= 1;
  1551. }
  1552. if (x >= 1e9) { /* handle bad rounding case */
  1553. x /= 10;
  1554. ++exp;
  1555. }
  1556. assert(x < 1e9);
  1557. GENERATE_DIGITS:
  1558. s = buf + 2; /* leave space for '\0' and '0' */
  1559. #if 1
  1560. #define ONE_E_NINE 1000000000L
  1561. #else
  1562. #define ONE_E_NINE 1e9
  1563. #endif
  1564. for (i = 0 ; i < NUM_DIGIT_BLOCKS ; ++i ) {
  1565. digit_block = (DIGIT_BLOCK_TYPE) x;
  1566. x = (x - digit_block) * ONE_E_NINE;
  1567. s += sprintf(s, DB_FMT, DIGITS_PER_BLOCK, digit_block);
  1568. }
  1569. /*************************************************************************/
  1570. *exp_buf = 'e';
  1571. if (mode < 'a') {
  1572. *exp_buf = 'E';
  1573. mode += ('a' - 'A');
  1574. }
  1575. o_mode = mode;
  1576. round = preci;
  1577. if ((mode == 'g') && (round > 0)){
  1578. --round;
  1579. }
  1580. if (mode == 'f') {
  1581. round += exp;
  1582. if (round < -1) {
  1583. memset(buf, '0', MAX_DIGITS);
  1584. exp = -1;
  1585. round = -1;
  1586. }
  1587. }
  1588. s = buf;
  1589. *s++ = 0; /* terminator for rounding and 0-triming */
  1590. *s = '0'; /* space to round */
  1591. i = 0;
  1592. e = s + MAX_DIGITS + 1;
  1593. if (round < MAX_DIGITS) {
  1594. e = s + round + 2;
  1595. if (*e >= '5') {
  1596. i = 1;
  1597. }
  1598. }
  1599. do { /* handle rounding and trim trailing 0s */
  1600. *--e += i; /* add the carry */
  1601. } while ((*e == '0') || (*e > '9'));
  1602. o_exp = exp;
  1603. if (e <= s) { /* we carried into extra digit */
  1604. ++o_exp;
  1605. e = s; /* needed if all 0s */
  1606. } else {
  1607. ++s;
  1608. }
  1609. *++e = 0; /* ending nul char */
  1610. if ((mode == 'g') && ((o_exp >= -4) && (o_exp <= round))) {
  1611. mode = 'f';
  1612. }
  1613. exp = o_exp;
  1614. if (mode != 'f') {
  1615. o_exp = 0;
  1616. }
  1617. if (o_exp < 0) {
  1618. *--s = '0'; /* fake the first digit */
  1619. }
  1620. pdrvr = drvr+1;
  1621. ppc = pc_fwi+2;
  1622. *pdrvr++ = 0;
  1623. *ppc++ = 1;
  1624. *ppc++ = (INT_OR_PTR)(*s++ - '0');
  1625. i = e - s; /* total digits */
  1626. if (o_exp >= 0) {
  1627. if (o_exp >= i) { /* all digit(s) left of decimal */
  1628. *pdrvr++ = 1;
  1629. *ppc++ = i;
  1630. *ppc++ = (INT_OR_PTR)(s);
  1631. o_exp -= i;
  1632. i = 0;
  1633. if (o_exp>0) { /* have 0s left of decimal */
  1634. *pdrvr++ = 0;
  1635. *ppc++ = o_exp;
  1636. *ppc++ = 0;
  1637. }
  1638. } else if (o_exp > 0) { /* decimal between digits */
  1639. *pdrvr++ = 1;
  1640. *ppc++ = o_exp;
  1641. *ppc++ = (INT_OR_PTR)(s);
  1642. s += o_exp;
  1643. i -= o_exp;
  1644. }
  1645. o_exp = -1;
  1646. }
  1647. if (PRINT_INFO_FLAG_VAL(info,alt)
  1648. /* flag[FLAG_HASH] */
  1649. || (i) || ((o_mode != 'g') && (preci > 0))) {
  1650. #ifdef __LOCALE_C_ONLY
  1651. *pdrvr++ = 2; /* need decimal */
  1652. *ppc++ = 1; /* needed for width calc */
  1653. ppc++;
  1654. #else /* __LOCALE_C_ONLY */
  1655. *pdrvr++ = 1;
  1656. *ppc++ = strlen(CUR_LOCALE.decimal_point);
  1657. *ppc++ = (INT_OR_PTR)(CUR_LOCALE.decimal_point);
  1658. #endif /* __LOCALE_C_ONLY */
  1659. }
  1660. if (++o_exp < 0) { /* have 0s right of decimal */
  1661. *pdrvr++ = 0;
  1662. *ppc++ = -o_exp;
  1663. *ppc++ = 0;
  1664. }
  1665. if (i) { /* have digit(s) right of decimal */
  1666. *pdrvr++ = 1;
  1667. *ppc++ = i;
  1668. *ppc++ = (INT_OR_PTR)(s);
  1669. }
  1670. if (o_mode != 'g') {
  1671. i -= o_exp;
  1672. if (i < preci) { /* have 0s right of digits */
  1673. i = preci - i;
  1674. *pdrvr++ = 0;
  1675. *ppc++ = i;
  1676. *ppc++ = 0;
  1677. }
  1678. }
  1679. /* build exponent string */
  1680. if (mode != 'f') {
  1681. *pdrvr++ = 1;
  1682. *ppc++ = sprintf(exp_buf,"%c%+.2d", *exp_buf, exp);
  1683. *ppc++ = (INT_OR_PTR) exp_buf;
  1684. }
  1685. EXIT_SPECIAL:
  1686. npc = pdrvr - drvr;
  1687. ppc = pc_fwi + 2;
  1688. for (i=1 ; i< npc ; i++) {
  1689. width -= *(ppc++);
  1690. ppc++;
  1691. }
  1692. i = 0;
  1693. if (*sign_str) {
  1694. i = 1;
  1695. }
  1696. width -= i;
  1697. if (width <= 0) {
  1698. width = 0;
  1699. } else {
  1700. if (PRINT_INFO_FLAG_VAL(info,left)) { /* padding on right */
  1701. /* flag[FLAG_MINUS_LJUSTIFY] */
  1702. ++npc;
  1703. *pdrvr++ = 7;
  1704. *ppc = width;
  1705. *++ppc = (INT_OR_PTR)("");
  1706. width = 0;
  1707. } else if (info->pad == '0') { /* 0 padding */
  1708. /* (flag[FLAG_0_PAD] == '0') */
  1709. pc_fwi[2] += width;
  1710. width = 0;
  1711. }
  1712. }
  1713. *drvr = 7;
  1714. ppc = pc_fwi;
  1715. *ppc++ = width + i;
  1716. *ppc = (INT_OR_PTR) sign_str;
  1717. pdrvr = drvr;
  1718. ppc = pc_fwi;
  1719. cnt = 0;
  1720. for (i=0 ; i<npc ; i++) {
  1721. #if 1
  1722. fprintf(fp, fmts[(int)(*pdrvr++)], (INT_OR_PTR)(*(ppc)),
  1723. (INT_OR_PTR)(*(ppc+1)));
  1724. #else
  1725. j = fprintf(fp, fmts[(int)(*pdrvr++)], (INT_OR_PTR)(*(ppc)),
  1726. (INT_OR_PTR)(*(ppc+1)));
  1727. assert(j == *ppc);
  1728. #endif
  1729. /* if (size > *ppc) { */
  1730. /* size -= *ppc; */
  1731. /* } */
  1732. cnt += *ppc; /* to avoid problems if j == -1 */
  1733. ppc += 2;
  1734. }
  1735. return cnt;
  1736. }
  1737. #endif
  1738. /**********************************************************************/
  1739. #ifdef L__store_inttype
  1740. /* TODO -- right now, assumes intmax_t is either long or long long */
  1741. /* We assume int may be short or long, but short and long are different. */
  1742. void _store_inttype(register void *dest, int desttype, uintmax_t val)
  1743. {
  1744. if (desttype == __PA_FLAG_CHAR) { /* assume char not int */
  1745. *((unsigned char *) dest) = val;
  1746. return;
  1747. }
  1748. #if defined(LLONG_MAX) && (LONG_MAX != LLONG_MAX)
  1749. if (desttype == PA_FLAG_LONG_LONG) {
  1750. *((unsigned long long int *) dest) = val;
  1751. return;
  1752. }
  1753. #endif /* LLONG_MAX */
  1754. #if SHRT_MAX != INT_MAX
  1755. if (desttype == PA_FLAG_SHORT) {
  1756. *((unsigned short int *) dest) = val;
  1757. return;
  1758. }
  1759. #endif /* SHRT_MAX */
  1760. #if LONG_MAX != INT_MAX
  1761. if (desttype == PA_FLAG_LONG) {
  1762. *((unsigned long int *) dest) = val;
  1763. return;
  1764. }
  1765. #endif /* LONG_MAX */
  1766. *((unsigned int *) dest) = val;
  1767. }
  1768. #endif
  1769. /**********************************************************************/
  1770. #ifdef L__load_inttype
  1771. extern uintmax_t _load_inttype(int desttype, register const void *src,
  1772. int uflag)
  1773. {
  1774. if (uflag >= 0) { /* unsigned */
  1775. #if LONG_MAX != INT_MAX
  1776. if (desttype & (PA_FLAG_LONG|PA_FLAG_LONG_LONG)) {
  1777. #ifdef LLONG_MAX
  1778. if (desttype == PA_FLAG_LONG_LONG) {
  1779. return *((unsigned long long int *) src);
  1780. }
  1781. #endif
  1782. return *((unsigned long int *) src);
  1783. }
  1784. #else /* LONG_MAX != INT_MAX */
  1785. #ifdef LLONG_MAX
  1786. if (desttype & PA_FLAG_LONG_LONG) {
  1787. return *((unsigned long long int *) src);
  1788. }
  1789. #endif
  1790. #endif /* LONG_MAX != INT_MAX */
  1791. {
  1792. unsigned int x;
  1793. x = *((unsigned int *) src);
  1794. if (desttype == __PA_FLAG_CHAR) x = (unsigned char) x;
  1795. #if SHRT_MAX != INT_MAX
  1796. if (desttype == PA_FLAG_SHORT) x = (unsigned short int) x;
  1797. #endif
  1798. return x;
  1799. }
  1800. } else { /* signed */
  1801. #if LONG_MAX != INT_MAX
  1802. if (desttype & (PA_FLAG_LONG|PA_FLAG_LONG_LONG)) {
  1803. #ifdef LLONG_MAX
  1804. if (desttype == PA_FLAG_LONG_LONG) {
  1805. return *((long long int *) src);
  1806. }
  1807. #endif
  1808. return *((long int *) src);
  1809. }
  1810. #else /* LONG_MAX != INT_MAX */
  1811. #ifdef LLONG_MAX
  1812. if (desttype & PA_FLAG_LONG_LONG) {
  1813. return *((long long int *) src);
  1814. }
  1815. #endif
  1816. #endif /* LONG_MAX != INT_MAX */
  1817. {
  1818. int x;
  1819. x = *((int *) src);
  1820. if (desttype == __PA_FLAG_CHAR) x = (char) x;
  1821. #if SHRT_MAX != INT_MAX
  1822. if (desttype == PA_FLAG_SHORT) x = (short int) x;
  1823. #endif
  1824. return x;
  1825. }
  1826. }
  1827. }
  1828. #endif
  1829. /**********************************************************************/
  1830. #if defined(L_vfprintf) || defined(L_vfwprintf)
  1831. /* We only support ascii digits (or their USC equivalent codes) in
  1832. * precision and width settings in *printf (wide) format strings.
  1833. * In other words, we don't currently support glibc's 'I' flag.
  1834. * We do accept it, but it is currently ignored. */
  1835. #ifdef L_vfprintf
  1836. #define VFPRINTF vfprintf
  1837. #define FMT_TYPE char
  1838. #define OUTNSTR _outnstr
  1839. #define STRLEN strlen
  1840. #define _PPFS_init _ppfs_init
  1841. #define OUTPUT(F,S) fputs(S,F)
  1842. #define _outnstr(stream, string, len) _stdio_fwrite(string, len, stream)
  1843. #else /* L_vfprintf */
  1844. #define VFPRINTF vfwprintf
  1845. #define FMT_TYPE wchar_t
  1846. #define OUTNSTR _outnwcs
  1847. #define STRLEN wcslen
  1848. #define _PPFS_init _ppwfs_init
  1849. #define OUTPUT(F,S) fputws(S,F)
  1850. #define _outnwcs(stream, wstring, len) _wstdio_fwrite(wstring, len, stream)
  1851. static void _outnstr(FILE *stream, const char *s, size_t wclen)
  1852. {
  1853. /* NOTE!!! len here is the number of wchars we want to generate!!! */
  1854. wchar_t wbuf[64];
  1855. mbstate_t mbstate;
  1856. size_t todo, r;
  1857. mbstate.mask = 0;
  1858. todo = wclen;
  1859. while (todo) {
  1860. r = mbsrtowcs(wbuf, &s, sizeof(wbuf)/sizeof(wbuf[0]), &mbstate);
  1861. assert(((ssize_t)r) > 0);
  1862. _outnwcs(stream, wbuf, r);
  1863. todo -= r;
  1864. }
  1865. }
  1866. static int _ppwfs_init(register ppfs_t *ppfs, const wchar_t *fmt0)
  1867. {
  1868. static const wchar_t invalid_wcs[] = L"Invalid wide format string.";
  1869. int r;
  1870. /* First, zero out everything... argnumber[], argtype[], argptr[] */
  1871. memset(ppfs, 0, sizeof(ppfs_t)); /* TODO: nonportable???? */
  1872. --ppfs->maxposarg; /* set to -1 */
  1873. ppfs->fmtpos = (const char *) fmt0;
  1874. ppfs->info._flags = FLAG_WIDESTREAM;
  1875. {
  1876. mbstate_t mbstate;
  1877. const wchar_t *p;
  1878. mbstate.mask = 0; /* Initialize the mbstate. */
  1879. p = fmt0;
  1880. if (wcsrtombs(NULL, &p, SIZE_MAX, &mbstate) == ((size_t)(-1))) {
  1881. ppfs->fmtpos = (const char *) invalid_wcs;
  1882. return -1;
  1883. }
  1884. }
  1885. /* now set all argtypes to no-arg */
  1886. {
  1887. #if 1
  1888. /* TODO - use memset here since already "paid for"? */
  1889. register int *p = ppfs->argtype;
  1890. r = MAX_ARGS;
  1891. do {
  1892. *p++ = __PA_NOARG;
  1893. } while (--r);
  1894. #else
  1895. /* TODO -- get rid of this?? */
  1896. register char *p = (char *) ((MAX_ARGS-1) * sizeof(int));
  1897. do {
  1898. *((int *)(((char *)ppfs) + ((int)p) + offsetof(ppfs_t,argtype))) = __PA_NOARG;
  1899. p -= sizeof(int);
  1900. } while (p);
  1901. #endif
  1902. }
  1903. /*
  1904. * Run through the entire format string to validate it and initialize
  1905. * the positional arg numbers (if any).
  1906. */
  1907. {
  1908. register const wchar_t *fmt = fmt0;
  1909. while (*fmt) {
  1910. if ((*fmt == '%') && (*++fmt != '%')) {
  1911. ppfs->fmtpos = (const char *) fmt; /* back up to the '%' */
  1912. if ((r = _ppfs_parsespec(ppfs)) < 0) {
  1913. return -1;
  1914. }
  1915. fmt = (const wchar_t *) ppfs->fmtpos; /* update to one past end of spec */
  1916. } else {
  1917. ++fmt;
  1918. }
  1919. }
  1920. ppfs->fmtpos = (const char *) fmt0; /* rewind */
  1921. }
  1922. /* If we have positional args, make sure we know all the types. */
  1923. {
  1924. register int *p = ppfs->argtype;
  1925. r = ppfs->maxposarg;
  1926. while (--r >= 0) {
  1927. if ( *p == __PA_NOARG ) { /* missing arg type!!! */
  1928. return -1;
  1929. }
  1930. ++p;
  1931. }
  1932. }
  1933. return 0;
  1934. }
  1935. #endif /* L_vfprintf */
  1936. static void _charpad(FILE * __restrict stream, int padchar, size_t numpad)
  1937. {
  1938. /* TODO -- Use a buffer to cut down on function calls... */
  1939. FMT_TYPE pad[1];
  1940. *pad = padchar;
  1941. while (numpad) {
  1942. OUTNSTR(stream, pad, 1);
  1943. --numpad;
  1944. }
  1945. }
  1946. /* TODO -- Dynamically allocate work space to accomodate stack-poor archs? */
  1947. static int _do_one_spec(FILE * __restrict stream,
  1948. register ppfs_t *ppfs, int *count)
  1949. {
  1950. static const char spec_base[] = SPEC_BASE;
  1951. #ifdef L_vfprintf
  1952. static const char prefix[] = "+\0-\0 \0000x\0000X";
  1953. /* 0 2 4 6 9 11*/
  1954. #else /* L_vfprintf */
  1955. static const wchar_t prefix[] = L"+\0-\0 \0000x\0000X";
  1956. #endif /* L_vfprintf */
  1957. enum {
  1958. PREFIX_PLUS = 0,
  1959. PREFIX_MINUS = 2,
  1960. PREFIX_SPACE = 4,
  1961. PREFIX_LWR_X = 6,
  1962. PREFIX_UPR_X = 9,
  1963. PREFIX_NONE = 11
  1964. };
  1965. #ifdef __va_arg_ptr
  1966. const void * const *argptr;
  1967. #else
  1968. const void * argptr[MAX_ARGS_PER_SPEC];
  1969. #endif
  1970. int *argtype;
  1971. #ifdef __UCLIBC_HAS_WCHAR__
  1972. const wchar_t *ws = NULL;
  1973. mbstate_t mbstate;
  1974. #endif /* __UCLIBC_HAS_WCHAR__ */
  1975. size_t slen;
  1976. #ifdef L_vfprintf
  1977. #define SLEN slen
  1978. #else
  1979. size_t SLEN;
  1980. wchar_t wbuf[2];
  1981. #endif
  1982. int base;
  1983. int numpad;
  1984. int alphacase;
  1985. int numfill = 0; /* TODO: fix */
  1986. int prefix_num = PREFIX_NONE;
  1987. char padchar = ' ';
  1988. #ifdef __UCLIBC_MJN3_ONLY__
  1989. #warning REMINDER: buf size
  1990. #endif
  1991. /* TODO: buf needs to be big enough for any possible error return strings
  1992. * and also for any locale-grouped long long integer strings generated.
  1993. * This should be large enough for any of the current archs/locales, but
  1994. * eventually this should be handled robustly. */
  1995. char buf[128];
  1996. #ifdef NDEBUG
  1997. _ppfs_parsespec(ppfs);
  1998. #else
  1999. if (_ppfs_parsespec(ppfs) < 0) { /* TODO: just for debugging */
  2000. abort();
  2001. }
  2002. #endif
  2003. _ppfs_setargs(ppfs);
  2004. argtype = ppfs->argtype + ppfs->argnumber[2] - 1;
  2005. /* Deal with the argptr vs argvalue issue. */
  2006. #ifdef __va_arg_ptr
  2007. argptr = (const void * const *) ppfs->argptr;
  2008. if (ppfs->maxposarg > 0) { /* Using positional args... */
  2009. argptr += ppfs->argnumber[2] - 1;
  2010. }
  2011. #else
  2012. /* Need to build a local copy... */
  2013. {
  2014. register argvalue_t *p = ppfs->argvalue;
  2015. int i;
  2016. if (ppfs->maxposarg > 0) { /* Using positional args... */
  2017. p += ppfs->argnumber[2] - 1;
  2018. }
  2019. for (i = 0 ; i < ppfs->num_data_args ; i++ ) {
  2020. argptr[i] = (void *) p++;
  2021. }
  2022. }
  2023. #endif
  2024. {
  2025. register char *s; /* TODO: Should s be unsigned char * ? */
  2026. if (ppfs->conv_num == CONV_n) {
  2027. _store_inttype(*(void **)*argptr,
  2028. ppfs->info._flags & __PA_INTMASK,
  2029. (intmax_t) (*count));
  2030. return 0;
  2031. }
  2032. if (ppfs->conv_num <= CONV_i) { /* pointer or (un)signed int */
  2033. alphacase = __UIM_LOWER;
  2034. #ifndef __LOCALE_C_ONLY
  2035. if ((base = spec_base[(int)(ppfs->conv_num - CONV_p)]) == 10) {
  2036. if (PRINT_INFO_FLAG_VAL(&(ppfs->info),group)) {
  2037. alphacase = __UIM_GROUP;
  2038. }
  2039. if (PRINT_INFO_FLAG_VAL(&(ppfs->info),i18n)) {
  2040. alphacase |= 0x80;
  2041. }
  2042. }
  2043. #endif /* __LOCALE_C_ONLY */
  2044. if (ppfs->conv_num <= CONV_u) { /* pointer or unsigned int */
  2045. if (ppfs->conv_num == CONV_X) {
  2046. alphacase = __UIM_UPPER;
  2047. }
  2048. if (ppfs->conv_num == CONV_p) { /* pointer */
  2049. prefix_num = PREFIX_LWR_X;
  2050. } else { /* unsigned int */
  2051. }
  2052. } else { /* signed int */
  2053. base = -base;
  2054. }
  2055. if (ppfs->info.prec < 0) { /* Ignore '0' flag if prec specified. */
  2056. padchar = ppfs->info.pad;
  2057. }
  2058. #ifdef __UCLIBC_MJN3_ONLY__
  2059. #warning if using outdigits and/or grouping, how should we interpret precision?
  2060. #endif
  2061. s = _uintmaxtostr(buf + sizeof(buf) - 1,
  2062. (uintmax_t)
  2063. _load_inttype(*argtype & __PA_INTMASK,
  2064. *argptr, base), base, alphacase);
  2065. if (ppfs->conv_num > CONV_u) { /* signed int */
  2066. if (*s == '-') {
  2067. PRINT_INFO_SET_FLAG(&(ppfs->info),showsign);
  2068. ++s; /* handle '-' in the prefix string */
  2069. prefix_num = PREFIX_MINUS;
  2070. } else if (PRINT_INFO_FLAG_VAL(&(ppfs->info),showsign)) {
  2071. prefix_num = PREFIX_PLUS;
  2072. } else if (PRINT_INFO_FLAG_VAL(&(ppfs->info),space)) {
  2073. prefix_num = PREFIX_SPACE;
  2074. }
  2075. }
  2076. slen = (char *)(buf + sizeof(buf) - 1) - s;
  2077. #ifdef L_vfwprintf
  2078. {
  2079. const char *q = s;
  2080. mbstate.mask = 0; /* Initialize the mbstate. */
  2081. SLEN = mbsrtowcs(NULL, &q, 0, &mbstate);
  2082. }
  2083. #endif
  2084. numfill = ((ppfs->info.prec < 0) ? 1 : ppfs->info.prec);
  2085. if (PRINT_INFO_FLAG_VAL(&(ppfs->info),alt)) {
  2086. if (ppfs->conv_num <= CONV_x) { /* x or p */
  2087. prefix_num = PREFIX_LWR_X;
  2088. }
  2089. if (ppfs->conv_num == CONV_X) {
  2090. prefix_num = PREFIX_UPR_X;
  2091. }
  2092. if ((ppfs->conv_num == CONV_o) && (numfill <= SLEN)) {
  2093. numfill = ((*s == '0') ? 1 : SLEN + 1);
  2094. }
  2095. }
  2096. if (*s == '0') {
  2097. if (prefix_num >= PREFIX_LWR_X) {
  2098. prefix_num = PREFIX_NONE;
  2099. }
  2100. if (ppfs->conv_num == CONV_p) {/* null pointer */
  2101. s = "(nil)";
  2102. #ifdef L_vfwprintf
  2103. SLEN =
  2104. #endif
  2105. slen = 5;
  2106. numfill = 0;
  2107. } else if (numfill == 0) { /* if precision 0, no output */
  2108. #ifdef L_vfwprintf
  2109. SLEN =
  2110. #endif
  2111. slen = 0;
  2112. }
  2113. }
  2114. numfill = ((numfill > SLEN) ? numfill - SLEN : 0);
  2115. } else if (ppfs->conv_num <= CONV_A) { /* floating point */
  2116. #ifdef L_vfwprintf
  2117. #ifdef __UCLIBC_MJN3_ONLY__
  2118. #warning fix dtostr
  2119. #endif
  2120. return -1;
  2121. #else /* L_vfwprintf */
  2122. #ifdef __STDIO_PRINTF_FLOAT
  2123. *count += _dtostr(stream,
  2124. (PRINT_INFO_FLAG_VAL(&(ppfs->info),is_long_double)
  2125. ? *(long double *) *argptr
  2126. : (long double) (* (double *) *argptr)),
  2127. &ppfs->info);
  2128. return 0;
  2129. #else /* __STDIO_PRINTF_FLOAT */
  2130. return -1; /* TODO -- try to continue? */
  2131. #endif /* __STDIO_PRINTF_FLOAT */
  2132. #endif /* L_vfwprintf */
  2133. } else if (ppfs->conv_num <= CONV_S) { /* wide char or string */
  2134. #ifdef L_vfprintf
  2135. #ifdef __UCLIBC_HAS_WCHAR__
  2136. mbstate.mask = 0; /* Initialize the mbstate. */
  2137. if (ppfs->conv_num == CONV_S) { /* wide string */
  2138. if (!(ws = *((const wchar_t **) *argptr))) {
  2139. goto NULL_STRING;
  2140. }
  2141. /* We use an awful uClibc-specific hack here, passing
  2142. * (char*) &ws as the conversion destination. This signals
  2143. * uClibc's wcsrtombs that we want a "restricted" length
  2144. * such that the mbs fits in a buffer of the specified
  2145. * size with no partial conversions. */
  2146. if ((slen = wcsrtombs((char *) &ws, &ws, /* Use awful hack! */
  2147. ((ppfs->info.prec >= 0)
  2148. ? ppfs->info.prec
  2149. : SIZE_MAX), &mbstate))
  2150. == ((size_t)-1)
  2151. ) {
  2152. return -1; /* EILSEQ */
  2153. }
  2154. } else { /* wide char */
  2155. s = buf;
  2156. slen = wcrtomb(s, (*((const wchar_t *) *argptr)), &mbstate);
  2157. if (slen == ((size_t)-1)) {
  2158. return -1; /* EILSEQ */
  2159. }
  2160. s[slen] = 0; /* TODO - Is this necessary? */
  2161. }
  2162. #else /* __UCLIBC_HAS_WCHAR__ */
  2163. return -1;
  2164. #endif /* __UCLIBC_HAS_WCHAR__ */
  2165. } else if (ppfs->conv_num <= CONV_s) { /* char or string */
  2166. if (ppfs->conv_num == CONV_s) { /* string */
  2167. s = *((char **) (*argptr));
  2168. if (s) {
  2169. SET_STRING_LEN:
  2170. slen = strnlen(s, ((ppfs->info.prec >= 0)
  2171. ? ppfs->info.prec : SIZE_MAX));
  2172. } else {
  2173. #ifdef __UCLIBC_HAS_WCHAR__
  2174. NULL_STRING:
  2175. #endif
  2176. s = "(null)";
  2177. slen = 6;
  2178. }
  2179. } else { /* char */
  2180. s = buf;
  2181. *s = (unsigned char)(*((const int *) *argptr));
  2182. s[1] = 0;
  2183. slen = 1;
  2184. }
  2185. #else /* L_vfprintf */
  2186. if (ppfs->conv_num == CONV_S) { /* wide string */
  2187. ws = *((wchar_t **) (*argptr));
  2188. if (!ws) {
  2189. goto NULL_STRING;
  2190. }
  2191. SLEN = wcsnlen(ws, ((ppfs->info.prec >= 0)
  2192. ? ppfs->info.prec : SIZE_MAX));
  2193. } else { /* wide char */
  2194. *wbuf = (wchar_t)(*((const wint_t *) *argptr));
  2195. CHAR_CASE:
  2196. ws = wbuf;
  2197. wbuf[1] = 0;
  2198. SLEN = 1;
  2199. }
  2200. } else if (ppfs->conv_num <= CONV_s) { /* char or string */
  2201. if (ppfs->conv_num == CONV_s) { /* string */
  2202. #ifdef __UCLIBC_MJN3_ONLY__
  2203. #warning Fix %s for vfwprintf... output upto illegal sequence?
  2204. #endif
  2205. s = *((char **) (*argptr));
  2206. if (s) {
  2207. SET_STRING_LEN:
  2208. /* We use an awful uClibc-specific hack here, passing
  2209. * (wchar_t*) &mbstate as the conversion destination.
  2210. * This signals uClibc's mbsrtowcs that we want a
  2211. * "restricted" length such that the mbs fits in a buffer
  2212. * of the specified size with no partial conversions. */
  2213. {
  2214. const char *q = s;
  2215. mbstate.mask = 0; /* Initialize the mbstate. */
  2216. SLEN = mbsrtowcs((wchar_t *) &mbstate, &q,
  2217. ((ppfs->info.prec >= 0)
  2218. ? ppfs->info.prec : SIZE_MAX),
  2219. &mbstate);
  2220. }
  2221. if (SLEN == ((size_t)(-1))) {
  2222. return -1; /* EILSEQ */
  2223. }
  2224. } else {
  2225. NULL_STRING:
  2226. s = "(null)";
  2227. SLEN = slen = 6;
  2228. }
  2229. } else { /* char */
  2230. *wbuf = btowc( (unsigned char)(*((const int *) *argptr)) );
  2231. goto CHAR_CASE;
  2232. }
  2233. #endif /* L_vfprintf */
  2234. #ifdef __STDIO_PRINTF_M_SUPPORT
  2235. } else if (ppfs->conv_num == CONV_m) {
  2236. s = _glibc_strerror_r(errno, buf, sizeof(buf));
  2237. goto SET_STRING_LEN;
  2238. #endif
  2239. } else {
  2240. assert(ppfs->conv_num == CONV_custom0);
  2241. s = _custom_printf_spec;
  2242. do {
  2243. if (*s == ppfs->info.spec) {
  2244. int rv;
  2245. /* TODO -- check return value for sanity? */
  2246. rv = (*_custom_printf_handler
  2247. [(int)(s-_custom_printf_spec)])
  2248. (stream, &ppfs->info, argptr);
  2249. if (rv < 0) {
  2250. return -1;
  2251. }
  2252. *count += rv;
  2253. return 0;
  2254. }
  2255. } while (++s < (_custom_printf_spec + MAX_USER_SPEC));
  2256. assert(0);
  2257. return -1;
  2258. }
  2259. #ifdef __UCLIBC_MJN3_ONLY__
  2260. #warning if using outdigits and/or grouping, how should we pad?
  2261. #endif
  2262. {
  2263. size_t t;
  2264. t = SLEN + numfill;
  2265. if (prefix_num != PREFIX_NONE) {
  2266. t += ((prefix_num < PREFIX_LWR_X) ? 1 : 2);
  2267. }
  2268. numpad = ((ppfs->info.width > t) ? (ppfs->info.width - t) : 0);
  2269. *count += t + numpad;
  2270. }
  2271. if (padchar == '0') { /* TODO: check this */
  2272. numfill += numpad;
  2273. numpad = 0;
  2274. }
  2275. /* Now handle the output itself. */
  2276. if (!PRINT_INFO_FLAG_VAL(&(ppfs->info),left)) {
  2277. _charpad(stream, ' ', numpad);
  2278. numpad = 0;
  2279. }
  2280. OUTPUT(stream, prefix + prefix_num);
  2281. _charpad(stream, '0', numfill);
  2282. #ifdef L_vfprintf
  2283. #ifdef __UCLIBC_HAS_WCHAR__
  2284. if (!ws) {
  2285. _outnstr(stream, s, slen);
  2286. } else { /* wide string */
  2287. size_t t;
  2288. mbstate.mask = 0; /* Initialize the mbstate. */
  2289. while (slen) {
  2290. t = (slen <= sizeof(buf)) ? slen : sizeof(buf);
  2291. t = wcsrtombs(buf, &ws, t, &mbstate);
  2292. assert (t != ((size_t)(-1)));
  2293. _outnstr(stream, buf, t);
  2294. slen -= t;
  2295. }
  2296. ws = NULL; /* Reset ws. */
  2297. }
  2298. #else /* __UCLIBC_HAS_WCHAR__ */
  2299. _outnstr(stream, s, slen);
  2300. #endif /* __UCLIBC_HAS_WCHAR__ */
  2301. #else /* L_vfprintf */
  2302. if (!ws) {
  2303. _outnstr(stream, s, SLEN);
  2304. } else {
  2305. _outnwcs(stream, ws, SLEN);
  2306. ws = NULL; /* Reset ws. */
  2307. }
  2308. #endif /* L_vfprintf */
  2309. _charpad(stream, ' ', numpad);
  2310. }
  2311. return 0;
  2312. }
  2313. int VFPRINTF (FILE * __restrict stream,
  2314. register const FMT_TYPE * __restrict format,
  2315. va_list arg)
  2316. {
  2317. ppfs_t ppfs;
  2318. int count, r;
  2319. register const FMT_TYPE *s;
  2320. __STDIO_THREADLOCK(stream);
  2321. count = 0;
  2322. s = format;
  2323. if (_PPFS_init(&ppfs, format) < 0) { /* Bad format string. */
  2324. OUTNSTR(stream, (const FMT_TYPE *) ppfs.fmtpos,
  2325. STRLEN((const FMT_TYPE *)(ppfs.fmtpos)));
  2326. count = -1;
  2327. } else {
  2328. _ppfs_prepargs(&ppfs, arg); /* This did a va_copy!!! */
  2329. do {
  2330. while (*format && (*format != '%')) {
  2331. ++format;
  2332. }
  2333. if (format-s) { /* output any literal text in format string */
  2334. if ( (r = OUTNSTR(stream, s, format-s)) < 0) {
  2335. count = -1;
  2336. break;
  2337. }
  2338. count += r;
  2339. }
  2340. if (!*format) { /* we're done */
  2341. break;
  2342. }
  2343. if (format[1] != '%') { /* if we get here, *format == '%' */
  2344. /* TODO: _do_one_spec needs to know what the output funcs are!!! */
  2345. ppfs.fmtpos = (const char *)(++format);
  2346. /* TODO: check -- should only fail on stream error */
  2347. if ( (r = _do_one_spec(stream, &ppfs, &count)) < 0) {
  2348. count = -1;
  2349. break;
  2350. }
  2351. s = format = (const FMT_TYPE *) ppfs.fmtpos;
  2352. } else { /* %% means literal %, so start new string */
  2353. s = ++format;
  2354. ++format;
  2355. }
  2356. } while (1);
  2357. va_end(ppfs.arg); /* Need to clean up after va_copy! */
  2358. }
  2359. __STDIO_THREADUNLOCK(stream);
  2360. return count;
  2361. }
  2362. #endif
  2363. /**********************************************************************/