vfprintf.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881
  1. /* Copyright (C) 2002-2004 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. * Sep 5, 2003
  73. * Implement *s*scanf for the non-buffered stdio case with old_vfprintf.
  74. *
  75. * Sep 23, 2003
  76. * vfprintf was not always checking for narrow stream orientation.
  77. */
  78. /* TODO:
  79. *
  80. * Should we validate that *printf format strings are valid multibyte
  81. * strings in the current locale? ANSI/ISO C99 seems to imply this
  82. * and Plauger's printf implementation in his Standard C Library book
  83. * treats this as an error.
  84. */
  85. #define _ISOC99_SOURCE /* for ULLONG primarily... */
  86. #define _GNU_SOURCE
  87. #define _STDIO_UTILITY /* We're using _uintmaxtostr. */
  88. #include <features.h>
  89. #include "_stdio.h"
  90. #include <stdlib.h>
  91. #include <string.h>
  92. #include <stddef.h>
  93. #include <ctype.h>
  94. #include <limits.h>
  95. #include <stdarg.h>
  96. #include <assert.h>
  97. #include <stdint.h>
  98. #include <errno.h>
  99. #include <locale.h>
  100. #define __PRINTF_INFO_NO_BITFIELD
  101. #include <printf.h>
  102. #ifdef __UCLIBC_HAS_THREADS__
  103. #include <stdio_ext.h>
  104. #include <pthread.h>
  105. #endif /* __UCLIBC_HAS_THREADS__ */
  106. #ifdef __UCLIBC_HAS_WCHAR__
  107. #include <wchar.h>
  108. #endif /* __UCLIBC_HAS_WCHAR__ */
  109. #include <bits/uClibc_uintmaxtostr.h>
  110. #include <bits/uClibc_va_copy.h>
  111. /* Some older or broken gcc toolchains define LONG_LONG_MAX but not
  112. * LLONG_MAX. Since LLONG_MAX is part of the standard, that's what
  113. * we use. So complain if we do not have it but should.
  114. */
  115. #if !defined(LLONG_MAX) && defined(LONG_LONG_MAX)
  116. #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.
  117. #endif
  118. /**********************************************************************/
  119. /* These provide some control over printf's feature set */
  120. /* This is undefined below depeding on uClibc's configuration. */
  121. #define __STDIO_PRINTF_FLOAT 1
  122. /* Now controlled by uClibc_stdio.h. */
  123. /* #define __UCLIBC_HAS_PRINTF_M_SPEC__ */
  124. /**********************************************************************/
  125. #if defined(__UCLIBC__) && !defined(__UCLIBC_HAS_FLOATS__)
  126. #undef __STDIO_PRINTF_FLOAT
  127. #endif
  128. #ifdef __BCC__
  129. #undef __STDIO_PRINTF_FLOAT
  130. #endif
  131. #ifdef __STDIO_PRINTF_FLOAT
  132. #include <float.h>
  133. #include <bits/uClibc_fpmax.h>
  134. #else /* __STDIO_PRINTF_FLOAT */
  135. #undef L__fpmaxtostr
  136. #endif /* __STDIO_PRINTF_FLOAT */
  137. #undef __STDIO_HAS_VSNPRINTF
  138. #if defined(__STDIO_BUFFERS) || defined(__USE_OLD_VFPRINTF__) || defined(__UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__)
  139. #define __STDIO_HAS_VSNPRINTF 1
  140. #endif
  141. /**********************************************************************/
  142. /* Now controlled by uClibc_stdio.h. */
  143. /* #define __UCLIBC_HAS_GLIBC_CUSTOM_PRINTF__ */
  144. /* TODO -- move these to a configuration section? */
  145. #define MAX_FIELD_WIDTH 4095
  146. #ifdef __UCLIBC_MJN3_ONLY__
  147. #ifdef L_register_printf_function
  148. /* emit only once */
  149. #warning WISHLIST: Make MAX_USER_SPEC configurable?
  150. #warning WISHLIST: Make MAX_ARGS_PER_SPEC configurable?
  151. #endif
  152. #endif /* __UCLIBC_MJN3_ONLY__ */
  153. #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_PRINTF__
  154. #define MAX_USER_SPEC 10
  155. #define MAX_ARGS_PER_SPEC 5
  156. #else /* __UCLIBC_HAS_GLIBC_CUSTOM_PRINTF__ */
  157. #undef MAX_USER_SPEC
  158. #define MAX_ARGS_PER_SPEC 1
  159. #endif /* __UCLIBC_HAS_GLIBC_CUSTOM_PRINTF__ */
  160. #if MAX_ARGS_PER_SPEC < 1
  161. #error MAX_ARGS_PER_SPEC < 1!
  162. #undef MAX_ARGS_PER_SPEC
  163. #define MAX_ARGS_PER_SPEC 1
  164. #endif
  165. #if defined(NL_ARGMAX) && (NL_ARGMAX < 9)
  166. #error NL_ARGMAX < 9!
  167. #endif
  168. #if defined(NL_ARGMAX) && (NL_ARGMAX >= (MAX_ARGS_PER_SPEC + 2))
  169. #define MAX_ARGS NL_ARGMAX
  170. #else
  171. /* N for spec itself, plus 1 each for width and precision */
  172. #define MAX_ARGS (MAX_ARGS_PER_SPEC + 2)
  173. #endif
  174. /**********************************************************************/
  175. #define __PA_FLAG_INTMASK \
  176. (__PA_FLAG_CHAR|PA_FLAG_SHORT|__PA_FLAG_INT|PA_FLAG_LONG|PA_FLAG_LONG_LONG)
  177. #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_PRINTF__
  178. extern printf_function _custom_printf_handler[MAX_USER_SPEC];
  179. extern printf_arginfo_function *_custom_printf_arginfo[MAX_USER_SPEC];
  180. extern char *_custom_printf_spec;
  181. #endif /* __UCLIBC_HAS_GLIBC_CUSTOM_PRINTF__ */
  182. /**********************************************************************/
  183. #define SPEC_FLAGS " +0-#'I"
  184. enum {
  185. FLAG_SPACE = 0x01,
  186. FLAG_PLUS = 0x02, /* must be 2 * FLAG_SPACE */
  187. FLAG_ZERO = 0x04,
  188. FLAG_MINUS = 0x08, /* must be 2 * FLAG_ZERO */
  189. FLAG_HASH = 0x10,
  190. FLAG_THOUSANDS = 0x20,
  191. FLAG_I18N = 0x40, /* only works for d, i, u */
  192. FLAG_WIDESTREAM = 0x80
  193. };
  194. /**********************************************************************/
  195. /* float layout 01234567890123456789 TODO: B?*/
  196. #define SPEC_CHARS "npxXoudifFeEgGaACScs"
  197. enum {
  198. CONV_n = 0,
  199. CONV_p,
  200. CONV_x, CONV_X, CONV_o, CONV_u, CONV_d, CONV_i,
  201. CONV_f, CONV_F, CONV_e, CONV_E, CONV_g, CONV_G, CONV_a, CONV_A,
  202. CONV_C, CONV_S, CONV_c, CONV_s,
  203. #ifdef __UCLIBC_HAS_PRINTF_M_SPEC__
  204. CONV_m,
  205. #endif
  206. CONV_custom0 /* must be last */
  207. };
  208. /* p x X o u d i */
  209. #define SPEC_BASE { 16, 16, 16, 8, 10, 10, 10 }
  210. #define SPEC_RANGES { CONV_n, CONV_p, CONV_i, CONV_A, \
  211. CONV_C, CONV_S, CONV_c, CONV_s, CONV_custom0 }
  212. #define SPEC_OR_MASK { \
  213. /* n */ (PA_FLAG_PTR|PA_INT), \
  214. /* p */ PA_POINTER, \
  215. /* oxXudi */ PA_INT, \
  216. /* fFeEgGaA */ PA_DOUBLE, \
  217. /* C */ PA_WCHAR, \
  218. /* S */ PA_WSTRING, \
  219. /* c */ PA_CHAR, \
  220. /* s */ PA_STRING, \
  221. }
  222. #define SPEC_AND_MASK { \
  223. /* n */ (PA_FLAG_PTR|__PA_INTMASK), \
  224. /* p */ PA_POINTER, \
  225. /* oxXudi */ (__PA_INTMASK), \
  226. /* fFeEgGaA */ (PA_FLAG_LONG_DOUBLE|PA_DOUBLE), \
  227. /* C */ (PA_WCHAR), \
  228. /* S */ (PA_WSTRING), \
  229. /* c */ (PA_CHAR), \
  230. /* s */ (PA_STRING), \
  231. }
  232. /**********************************************************************/
  233. /*
  234. * In order to ease translation to what arginfo and _print_info._flags expect,
  235. * we map: 0:int 1:char 2:longlong 4:long 8:short
  236. * and then _flags |= (((q << 7) + q) & 0x701) and argtype |= (_flags & 0x701)
  237. */
  238. /* TODO -- Fix the table below to take into account stdint.h. */
  239. /* #ifndef LLONG_MAX */
  240. /* #error fix QUAL_CHARS for no long long! Affects 'L', 'j', 'q', 'll'. */
  241. /* #else */
  242. /* #if LLONG_MAX != INTMAX_MAX */
  243. /* #error fix QUAL_CHARS intmax_t entry 'j'! */
  244. /* #endif */
  245. /* #endif */
  246. #ifdef PDS
  247. #error PDS already defined!
  248. #endif
  249. #ifdef SS
  250. #error SS already defined!
  251. #endif
  252. #ifdef IMS
  253. #error IMS already defined!
  254. #endif
  255. #if PTRDIFF_MAX == INT_MAX
  256. #define PDS 0
  257. #elif PTRDIFF_MAX == LONG_MAX
  258. #define PDS 4
  259. #elif defined(LLONG_MAX) && (PTRDIFF_MAX == LLONG_MAX)
  260. #define PDS 8
  261. #else
  262. #error fix QUAL_CHARS ptrdiff_t entry 't'!
  263. #endif
  264. #if SIZE_MAX == UINT_MAX
  265. #define SS 0
  266. #elif SIZE_MAX == ULONG_MAX
  267. #define SS 4
  268. #elif defined(LLONG_MAX) && (SIZE_MAX == ULLONG_MAX)
  269. #define SS 8
  270. #else
  271. #error fix QUAL_CHARS size_t entries 'z', 'Z'!
  272. #endif
  273. #if INTMAX_MAX == INT_MAX
  274. #define IMS 0
  275. #elif INTMAX_MAX == LONG_MAX
  276. #define IMS 4
  277. #elif defined(LLONG_MAX) && (INTMAX_MAX == LLONG_MAX)
  278. #define IMS 8
  279. #else
  280. #error fix QUAL_CHARS intmax_t entry 'j'!
  281. #endif
  282. #define QUAL_CHARS { \
  283. /* j:(u)intmax_t z:(s)size_t t:ptrdiff_t \0:int */ \
  284. /* q:long_long Z:(s)size_t */ \
  285. 'h', 'l', 'L', 'j', 'z', 't', 'q', 'Z', 0, \
  286. 2, 4, 8, IMS, SS, PDS, 8, SS, 0, /* TODO -- fix!!! */\
  287. 1, 8 \
  288. }
  289. /**********************************************************************/
  290. #ifdef __STDIO_VA_ARG_PTR
  291. #ifdef __BCC__
  292. #define __va_arg_ptr(ap,type) (((type *)(ap += sizeof(type))) - 1)
  293. #endif
  294. #if 1
  295. #ifdef __GNUC__
  296. /* TODO -- need other than for 386 as well! */
  297. #ifndef __va_rounded_size
  298. #define __va_rounded_size(TYPE) \
  299. (((sizeof (TYPE) + sizeof (int) - 1) / sizeof (int)) * sizeof (int))
  300. #endif
  301. #define __va_arg_ptr(AP, TYPE) \
  302. (AP = (va_list) ((char *) (AP) + __va_rounded_size (TYPE)), \
  303. ((void *) ((char *) (AP) - __va_rounded_size (TYPE))))
  304. #endif
  305. #endif
  306. #endif /* __STDIO_VA_ARG_PTR */
  307. #ifdef __va_arg_ptr
  308. #define GET_VA_ARG(AP,F,TYPE,ARGS) (*(AP) = __va_arg_ptr(ARGS,TYPE))
  309. #define GET_ARG_VALUE(AP,F,TYPE) (*((TYPE *)(*(AP))))
  310. #else
  311. typedef union {
  312. wchar_t wc;
  313. unsigned int u;
  314. unsigned long ul;
  315. #ifdef ULLONG_MAX
  316. unsigned long long ull;
  317. #endif
  318. #ifdef __STDIO_PRINTF_FLOAT
  319. double d;
  320. long double ld;
  321. #endif /* __STDIO_PRINTF_FLOAT */
  322. void *p;
  323. } argvalue_t;
  324. #define GET_VA_ARG(AU,F,TYPE,ARGS) (AU->F = va_arg(ARGS,TYPE))
  325. #define GET_ARG_VALUE(AU,F,TYPE) ((TYPE)((AU)->F))
  326. #endif
  327. typedef struct {
  328. const char *fmtpos; /* TODO: move below struct?? */
  329. struct printf_info info;
  330. #ifdef NL_ARGMAX
  331. int maxposarg; /* > 0 if args are positional, 0 if not, -1 if unknown */
  332. #endif /* NL_ARGMAX */
  333. int num_data_args; /* TODO: use sentinal??? */
  334. unsigned int conv_num;
  335. unsigned char argnumber[4]; /* width | prec | 1st data | unused */
  336. int argtype[MAX_ARGS];
  337. va_list arg;
  338. #ifdef __va_arg_ptr
  339. void *argptr[MAX_ARGS];
  340. #else
  341. /* if defined(NL_ARGMAX) || defined(__UCLIBC_HAS_GLIBC_CUSTOM_PRINTF__) */
  342. /* While this is wasteful of space in the case where pos args aren't
  343. * enabled, it is also needed to support custom printf handlers. */
  344. argvalue_t argvalue[MAX_ARGS];
  345. #endif
  346. } ppfs_t; /* parse printf format state */
  347. /**********************************************************************/
  348. /* TODO: fix printf to return 0 and set errno if format error. Standard says
  349. only returns -1 if sets error indicator for the stream. */
  350. #ifdef __STDIO_PRINTF_FLOAT
  351. typedef void (__fp_outfunc_t)(FILE *fp, intptr_t type, intptr_t len,
  352. intptr_t buf);
  353. extern size_t _fpmaxtostr(FILE * fp, __fpmax_t x, struct printf_info *info,
  354. __fp_outfunc_t fp_outfunc);
  355. #endif
  356. extern int _ppfs_init(ppfs_t *ppfs, const char *fmt0); /* validates */
  357. extern void _ppfs_prepargs(ppfs_t *ppfs, va_list arg); /* sets posargptrs */
  358. extern void _ppfs_setargs(ppfs_t *ppfs); /* sets argptrs for current spec */
  359. extern int _ppfs_parsespec(ppfs_t *ppfs); /* parses specifier */
  360. extern void _store_inttype(void *dest, int desttype, uintmax_t val);
  361. extern uintmax_t _load_inttype(int desttype, const void *src, int uflag);
  362. /**********************************************************************/
  363. #ifdef L_parse_printf_format
  364. /* NOTE: This function differs from the glibc version in that parsing stops
  365. * upon encountering an invalid conversion specifier. Since this is the way
  366. * my printf functions work, I think it makes sense to do it that way here.
  367. * Unfortunately, since glibc sets the return type as size_t, we have no way
  368. * of returning that the template is illegal, other than returning 0.
  369. */
  370. size_t parse_printf_format(register const char *template,
  371. size_t n, register int *argtypes)
  372. {
  373. ppfs_t ppfs;
  374. size_t i;
  375. size_t count = 0;
  376. if (_ppfs_init(&ppfs, template) >= 0) {
  377. #ifdef NL_ARGMAX
  378. if (ppfs.maxposarg > 0) { /* Using positional args. */
  379. count = ppfs.maxposarg;
  380. if (n > count) {
  381. n = count;
  382. }
  383. for (i = 0 ; i < n ; i++) {
  384. *argtypes++ = ppfs.argtype[i];
  385. }
  386. } else { /* Not using positional args. */
  387. #endif /* NL_ARGMAX */
  388. while (*template) {
  389. if ((*template == '%') && (*++template != '%')) {
  390. ppfs.fmtpos = template;
  391. _ppfs_parsespec(&ppfs); /* Can't fail. */
  392. template = ppfs.fmtpos; /* Update to one past spec end. */
  393. if (ppfs.info.width == INT_MIN) {
  394. ++count;
  395. if (n > 0) {
  396. *argtypes++ = PA_INT;
  397. --n;
  398. }
  399. }
  400. if (ppfs.info.prec == INT_MIN) {
  401. ++count;
  402. if (n > 0) {
  403. *argtypes++ = PA_INT;
  404. --n;
  405. }
  406. }
  407. for (i = 0 ; i < ppfs.num_data_args ; i++) {
  408. if ((ppfs.argtype[i]) != __PA_NOARG) {
  409. ++count;
  410. if (n > 0) {
  411. *argtypes++ = ppfs.argtype[i];
  412. --n;
  413. }
  414. }
  415. }
  416. } else {
  417. ++template;
  418. }
  419. }
  420. #ifdef NL_ARGMAX
  421. }
  422. #endif /* NL_ARGMAX */
  423. }
  424. return count;
  425. }
  426. #endif
  427. /**********************************************************************/
  428. #ifdef L__ppfs_init
  429. int _ppfs_init(register ppfs_t *ppfs, const char *fmt0)
  430. {
  431. int r;
  432. /* First, zero out everything... argnumber[], argtype[], argptr[] */
  433. memset(ppfs, 0, sizeof(ppfs_t)); /* TODO: nonportable???? */
  434. #ifdef NL_ARGMAX
  435. --ppfs->maxposarg; /* set to -1 */
  436. #endif /* NL_ARGMAX */
  437. ppfs->fmtpos = fmt0;
  438. #ifdef __UCLIBC_MJN3_ONLY__
  439. #warning TODO: Make checking of the format string in C locale an option.
  440. #endif
  441. #ifdef __UCLIBC_HAS_LOCALE__
  442. /* To support old programs, don't check mb validity if in C locale. */
  443. if (((__UCLIBC_CURLOCALE_DATA).encoding) != __ctype_encoding_7_bit) {
  444. /* ANSI/ISO C99 requires format string to be a valid multibyte string
  445. * beginning and ending in its initial shift state. */
  446. static const char invalid_mbs[] = "Invalid multibyte format string.";
  447. mbstate_t mbstate;
  448. const char *p;
  449. mbstate.__mask = 0; /* Initialize the mbstate. */
  450. p = fmt0;
  451. if (mbsrtowcs(NULL, &p, SIZE_MAX, &mbstate) == ((size_t)(-1))) {
  452. ppfs->fmtpos = invalid_mbs;
  453. return -1;
  454. }
  455. }
  456. #endif /* __UCLIBC_HAS_LOCALE__ */
  457. /* now set all argtypes to no-arg */
  458. {
  459. #if 1
  460. /* TODO - use memset here since already "paid for"? */
  461. register int *p = ppfs->argtype;
  462. r = MAX_ARGS;
  463. do {
  464. *p++ = __PA_NOARG;
  465. } while (--r);
  466. #else
  467. /* TODO -- get rid of this?? */
  468. register char *p = (char *) ((MAX_ARGS-1) * sizeof(int));
  469. do {
  470. *((int *)(((char *)ppfs) + ((int)p) + offsetof(ppfs_t,argtype))) = __PA_NOARG;
  471. p -= sizeof(int);
  472. } while (p);
  473. #endif
  474. }
  475. /*
  476. * Run through the entire format string to validate it and initialize
  477. * the positional arg numbers (if any).
  478. */
  479. {
  480. register const char *fmt = fmt0;
  481. while (*fmt) {
  482. if ((*fmt == '%') && (*++fmt != '%')) {
  483. ppfs->fmtpos = fmt; /* back up to the '%' */
  484. if ((r = _ppfs_parsespec(ppfs)) < 0) {
  485. return -1;
  486. }
  487. fmt = ppfs->fmtpos; /* update to one past end of spec */
  488. } else {
  489. ++fmt;
  490. }
  491. }
  492. ppfs->fmtpos = fmt0; /* rewind */
  493. }
  494. #ifdef NL_MAX_ARG
  495. /* If we have positional args, make sure we know all the types. */
  496. {
  497. register int *p = ppfs->argtype;
  498. r = ppfs->maxposarg;
  499. while (--r >= 0) {
  500. if ( *p == __PA_NOARG ) { /* missing arg type!!! */
  501. return -1;
  502. }
  503. ++p;
  504. }
  505. }
  506. #endif /* NL_MAX_ARG */
  507. return 0;
  508. }
  509. #endif
  510. /**********************************************************************/
  511. #ifdef L__ppfs_prepargs
  512. void _ppfs_prepargs(register ppfs_t *ppfs, va_list arg)
  513. {
  514. int i;
  515. va_copy(ppfs->arg, arg);
  516. #ifdef NL_ARGMAX
  517. if ((i = ppfs->maxposarg) > 0) { /* init for positional args */
  518. ppfs->num_data_args = i;
  519. ppfs->info.width = ppfs->info.prec = ppfs->maxposarg = 0;
  520. _ppfs_setargs(ppfs);
  521. ppfs->maxposarg = i;
  522. }
  523. #endif /* NL_ARGMAX */
  524. }
  525. #endif
  526. /**********************************************************************/
  527. #ifdef L__ppfs_setargs
  528. void _ppfs_setargs(register ppfs_t *ppfs)
  529. {
  530. #ifdef __va_arg_ptr
  531. register void **p = ppfs->argptr;
  532. #else
  533. register argvalue_t *p = ppfs->argvalue;
  534. #endif
  535. int i;
  536. #ifdef NL_ARGMAX
  537. if (ppfs->maxposarg == 0) { /* initing for or no pos args */
  538. #endif /* NL_ARGMAX */
  539. if (ppfs->info.width == INT_MIN) {
  540. ppfs->info.width =
  541. #ifdef __va_arg_ptr
  542. *(int *)
  543. #endif
  544. GET_VA_ARG(p,u,unsigned int,ppfs->arg);
  545. }
  546. if (ppfs->info.prec == INT_MIN) {
  547. ppfs->info.prec =
  548. #ifdef __va_arg_ptr
  549. *(int *)
  550. #endif
  551. GET_VA_ARG(p,u,unsigned int,ppfs->arg);
  552. }
  553. i = 0;
  554. while (i < ppfs->num_data_args) {
  555. switch(ppfs->argtype[i++]) {
  556. case (PA_INT|PA_FLAG_LONG_LONG):
  557. #ifdef ULLONG_MAX
  558. GET_VA_ARG(p,ull,unsigned long long,ppfs->arg);
  559. break;
  560. #endif
  561. case (PA_INT|PA_FLAG_LONG):
  562. #if ULONG_MAX != UINT_MAX
  563. GET_VA_ARG(p,ul,unsigned long,ppfs->arg);
  564. break;
  565. #endif
  566. case PA_CHAR: /* TODO - be careful */
  567. /* ... users could use above and really want below!! */
  568. case (PA_INT|__PA_FLAG_CHAR):/* TODO -- translate this!!! */
  569. case (PA_INT|PA_FLAG_SHORT):
  570. case PA_INT:
  571. GET_VA_ARG(p,u,unsigned int,ppfs->arg);
  572. break;
  573. case PA_WCHAR: /* TODO -- assume int? */
  574. /* we're assuming wchar_t is at least an int */
  575. GET_VA_ARG(p,wc,wchar_t,ppfs->arg);
  576. break;
  577. #ifdef __STDIO_PRINTF_FLOAT
  578. /* PA_FLOAT */
  579. case PA_DOUBLE:
  580. GET_VA_ARG(p,d,double,ppfs->arg);
  581. break;
  582. case (PA_DOUBLE|PA_FLAG_LONG_DOUBLE):
  583. GET_VA_ARG(p,ld,long double,ppfs->arg);
  584. break;
  585. #else /* __STDIO_PRINTF_FLOAT */
  586. case PA_DOUBLE:
  587. case (PA_DOUBLE|PA_FLAG_LONG_DOUBLE):
  588. assert(0);
  589. continue;
  590. #endif /* __STDIO_PRINTF_FLOAT */
  591. default:
  592. /* TODO -- really need to ensure this can't happen */
  593. assert(ppfs->argtype[i-1] & PA_FLAG_PTR);
  594. case PA_POINTER:
  595. case PA_STRING:
  596. case PA_WSTRING:
  597. GET_VA_ARG(p,p,void *,ppfs->arg);
  598. break;
  599. case __PA_NOARG:
  600. continue;
  601. }
  602. ++p;
  603. }
  604. #ifdef NL_ARGMAX
  605. } else {
  606. if (ppfs->info.width == INT_MIN) {
  607. ppfs->info.width
  608. = (int) GET_ARG_VALUE(p + ppfs->argnumber[0] - 1,u,unsigned int);
  609. }
  610. if (ppfs->info.prec == INT_MIN) {
  611. ppfs->info.prec
  612. = (int) GET_ARG_VALUE(p + ppfs->argnumber[1] - 1,u,unsigned int);
  613. }
  614. }
  615. #endif /* NL_ARGMAX */
  616. /* Now we know the width and precision. */
  617. if (ppfs->info.width < 0) {
  618. ppfs->info.width = -ppfs->info.width;
  619. PRINT_INFO_SET_FLAG(&(ppfs->info),left);
  620. PRINT_INFO_CLR_FLAG(&(ppfs->info),space);
  621. ppfs->info.pad = ' ';
  622. }
  623. #if 0
  624. /* NOTE -- keep neg for now so float knows! */
  625. if (ppfs->info.prec < 0) { /* spec says treat as omitted. */
  626. /* so use default prec... 1 for everything but floats and strings. */
  627. ppfs->info.prec = 1;
  628. }
  629. #endif
  630. }
  631. #endif
  632. /**********************************************************************/
  633. #ifdef L__ppfs_parsespec
  634. /* Notes: argtype differs from glibc for the following:
  635. * mine glibc
  636. * lc PA_WCHAR PA_CHAR the standard says %lc means %C
  637. * ls PA_WSTRING PA_STRING the standard says %ls means %S
  638. * {*}n {*}|PA_FLAG_PTR PA_FLAG_PTR size of n can be qualified
  639. */
  640. /* TODO: store positions of positional args */
  641. /* TODO -- WARNING -- assumes aligned on integer boundaries!!! */
  642. /* TODO -- disable if not using positional args!!! */
  643. #define _OVERLAPPING_DIFFERENT_ARGS
  644. /* TODO -- rethink this -- perhaps we should set to largest type??? */
  645. #ifdef _OVERLAPPING_DIFFERENT_ARGS
  646. #define PROMOTED_SIZE_OF(X) ((sizeof(X) + sizeof(int) - 1) / sizeof(X))
  647. static const short int type_codes[] = {
  648. __PA_NOARG, /* must be first entry */
  649. PA_POINTER,
  650. PA_STRING,
  651. PA_WSTRING,
  652. PA_CHAR,
  653. PA_INT|PA_FLAG_SHORT,
  654. PA_INT,
  655. PA_INT|PA_FLAG_LONG,
  656. PA_INT|PA_FLAG_LONG_LONG,
  657. PA_WCHAR,
  658. #ifdef __STDIO_PRINTF_FLOAT
  659. /* PA_FLOAT, */
  660. PA_DOUBLE,
  661. PA_DOUBLE|PA_FLAG_LONG_DOUBLE,
  662. #endif /* __STDIO_PRINTF_FLOAT */
  663. };
  664. static const unsigned char type_sizes[] = {
  665. /* unknown type consumes no arg */
  666. 0, /* must be first entry */
  667. PROMOTED_SIZE_OF(void *),
  668. PROMOTED_SIZE_OF(char *),
  669. PROMOTED_SIZE_OF(wchar_t *),
  670. PROMOTED_SIZE_OF(char),
  671. PROMOTED_SIZE_OF(short),
  672. PROMOTED_SIZE_OF(int),
  673. PROMOTED_SIZE_OF(long),
  674. #ifdef ULLONG_MAX
  675. PROMOTED_SIZE_OF(long long),
  676. #else
  677. PROMOTED_SIZE_OF(long), /* TODO -- is this correct? (above too) */
  678. #endif
  679. PROMOTED_SIZE_OF(wchar_t),
  680. #ifdef __STDIO_PRINTF_FLOAT
  681. /* PROMOTED_SIZE_OF(float), */
  682. PROMOTED_SIZE_OF(double),
  683. PROMOTED_SIZE_OF(long double),
  684. #endif /* __STDIO_PRINTF_FLOAT */
  685. };
  686. static int _promoted_size(int argtype)
  687. {
  688. register const short int *p;
  689. /* note -- since any unrecognized type is treated as a pointer */
  690. p = type_codes + sizeof(type_codes)/sizeof(type_codes[0]);
  691. do {
  692. if (*--p == argtype) {
  693. break;
  694. }
  695. } while (p > type_codes);
  696. return type_sizes[(int)(p - type_codes)];
  697. }
  698. static int _is_equal_or_bigger_arg(int curtype, int newtype)
  699. {
  700. /* Quick test */
  701. if (newtype == __PA_NOARG) {
  702. return 0;
  703. }
  704. if ((curtype == __PA_NOARG) || (curtype == newtype)) {
  705. return 1;
  706. }
  707. /* Ok... slot is already filled and types are different in name. */
  708. /* So, compare promoted sizes of curtype and newtype args. */
  709. return _promoted_size(curtype) <= _promoted_size(newtype);
  710. }
  711. #else
  712. #define _is_equal_or_bigger_arg(C,N) (((C) == __PA_NOARG) || ((C) == (N)))
  713. #endif
  714. #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_PRINTF__
  715. /* TODO - do this differently? */
  716. static char _bss_custom_printf_spec[MAX_USER_SPEC]; /* 0-init'd for us. */
  717. char *_custom_printf_spec = _bss_custom_printf_spec;
  718. printf_arginfo_function *_custom_printf_arginfo[MAX_USER_SPEC];
  719. printf_function _custom_printf_handler[MAX_USER_SPEC];
  720. #endif /* __UCLIBC_HAS_GLIBC_CUSTOM_PRINTF__ */
  721. extern int _ppfs_parsespec(ppfs_t *ppfs)
  722. {
  723. register const char *fmt;
  724. register const char *p;
  725. int preci;
  726. int width;
  727. int flags;
  728. int dataargtype;
  729. int i;
  730. int dpoint;
  731. #ifdef NL_ARGMAX
  732. int maxposarg;
  733. #endif /* NL_ARGMAX */
  734. int p_m_spec_chars;
  735. int n;
  736. int argtype[MAX_ARGS_PER_SPEC+2];
  737. int argnumber[3]; /* width, precision, 1st data arg */
  738. static const char spec_flags[] = SPEC_FLAGS;
  739. static const char spec_chars[] = SPEC_CHARS;/* TODO: b? */
  740. static const char spec_ranges[] = SPEC_RANGES;
  741. static const short spec_or_mask[] = SPEC_OR_MASK;
  742. static const short spec_and_mask[] = SPEC_AND_MASK;
  743. static const char qual_chars[] = QUAL_CHARS;
  744. #ifdef __UCLIBC_HAS_WCHAR__
  745. char buf[32];
  746. #endif /* __UCLIBC_HAS_WCHAR__ */
  747. /* WIDE note: we can test against '%' here since we don't allow */
  748. /* WIDE note: other mappings of '%' in the wide char set. */
  749. preci = -1;
  750. argnumber[0] = 0;
  751. argnumber[1] = 0;
  752. argtype[0] = __PA_NOARG;
  753. argtype[1] = __PA_NOARG;
  754. #ifdef NL_ARGMAX
  755. maxposarg = ppfs->maxposarg;
  756. #endif /* NL_ARGMAX */
  757. #ifdef __UCLIBC_HAS_WCHAR__
  758. /* This is somewhat lame, but saves a lot of code. If we're dealing with
  759. * a wide stream, that means the format is a wchar string. So, copy it
  760. * char-by-char into a normal char buffer for processing. Make the buffer
  761. * (buf) big enough so that any reasonable format specifier will fit.
  762. * While there a legal specifiers that won't, the all involve duplicate
  763. * flags or outrageous field widths/precisions. */
  764. width = dpoint = 0;
  765. if ((flags = ppfs->info._flags & FLAG_WIDESTREAM) == 0) {
  766. fmt = ppfs->fmtpos;
  767. } else {
  768. fmt = buf + 1;
  769. i = 0;
  770. do {
  771. if ((buf[i] = (char) (((wchar_t *) ppfs->fmtpos)[i-1]))
  772. != (((wchar_t *) ppfs->fmtpos)[i-1])
  773. ) {
  774. return -1;
  775. }
  776. } while (buf[i++]);
  777. buf[sizeof(buf)-1] = 0;
  778. }
  779. #else /* __UCLIBC_HAS_WCHAR__ */
  780. width = flags = dpoint = 0;
  781. fmt = ppfs->fmtpos;
  782. #endif /* __UCLIBC_HAS_WCHAR__ */
  783. assert(fmt[-1] == '%');
  784. assert(fmt[0] != '%');
  785. /* Process arg pos and/or flags and/or width and/or precision. */
  786. width_precision:
  787. p = fmt;
  788. if (*fmt == '*') {
  789. argtype[-dpoint] = PA_INT;
  790. ++fmt;
  791. }
  792. i = 0;
  793. while (isdigit(*fmt)) {
  794. if (i < MAX_FIELD_WIDTH) { /* Avoid overflow. */
  795. i = (i * 10) + (*fmt - '0');
  796. }
  797. ++fmt;
  798. }
  799. if (p[-1] == '%') { /* Check for a position. */
  800. /* TODO: if val not in range, then error */
  801. #ifdef NL_ARGMAX
  802. if ((*fmt == '$') && (i > 0)) {/* Positional spec. */
  803. ++fmt;
  804. if (maxposarg == 0) {
  805. return -1;
  806. }
  807. if ((argnumber[2] = i) > maxposarg) {
  808. maxposarg = i;
  809. }
  810. /* Now fall through to check flags. */
  811. } else {
  812. if (maxposarg > 0) {
  813. #ifdef __UCLIBC_HAS_PRINTF_M_SPEC__
  814. #ifdef __UCLIBC_MJN3_ONLY__
  815. #warning TODO: Support prec and width for %m when positional args used
  816. /* Actually, positional arg processing will fail in general
  817. * for specifiers that don't require an arg. */
  818. #endif /* __UCLIBC_MJN3_ONLY__ */
  819. if (*fmt == 'm') {
  820. goto PREC_WIDTH;
  821. }
  822. #endif /* __UCLIBC_HAS_PRINTF_M_SPEC__ */
  823. return -1;
  824. }
  825. maxposarg = 0; /* Possible redundant store, but cuts size. */
  826. if ((fmt > p) && (*p != '0')) {
  827. goto PREC_WIDTH;
  828. }
  829. fmt = p; /* Back up for possible '0's flag. */
  830. /* Now fall through to check flags. */
  831. }
  832. #else /* NL_ARGMAX */
  833. if (*fmt == '$') { /* Positional spec. */
  834. return -1;
  835. }
  836. if ((fmt > p) && (*p != '0')) {
  837. goto PREC_WIDTH;
  838. }
  839. fmt = p; /* Back up for possible '0's flag. */
  840. /* Now fall through to check flags. */
  841. #endif /* NL_ARGMAX */
  842. restart_flags: /* Process flags. */
  843. i = 1;
  844. p = spec_flags;
  845. do {
  846. if (*fmt == *p++) {
  847. ++fmt;
  848. flags |= i;
  849. goto restart_flags;
  850. }
  851. i += i; /* Better than i <<= 1 for bcc */
  852. } while (*p);
  853. i = 0;
  854. /* If '+' then ignore ' ', and if '-' then ignore '0'. */
  855. /* Note: Need to ignore '0' when prec is an arg with val < 0, */
  856. /* but that test needs to wait until the arg is retrieved. */
  857. flags &= ~((flags & (FLAG_PLUS|FLAG_MINUS)) >> 1);
  858. /* Note: Ignore '0' when prec is specified < 0 too (in printf). */
  859. if (fmt[-1] != '%') { /* If we've done anything, loop for width. */
  860. goto width_precision;
  861. }
  862. }
  863. PREC_WIDTH:
  864. if (*p == '*') { /* Prec or width takes an arg. */
  865. #ifdef NL_ARGMAX
  866. if (maxposarg) {
  867. if ((*fmt++ != '$') || (i <= 0)) {
  868. /* Using pos args and no $ or invalid arg number. */
  869. return -1;
  870. }
  871. argnumber[-dpoint] = i;
  872. } else
  873. #endif /* NL_ARGMAX */
  874. if (++p != fmt) {
  875. /* Not using pos args but digits followed *. */
  876. return -1;
  877. }
  878. i = INT_MIN;
  879. }
  880. if (!dpoint) {
  881. width = i;
  882. if (*fmt == '.') {
  883. ++fmt;
  884. dpoint = -1; /* To use as default precison. */
  885. goto width_precision;
  886. }
  887. } else {
  888. preci = i;
  889. }
  890. /* Process qualifier. */
  891. p = qual_chars;
  892. do {
  893. if (*fmt == *p) {
  894. ++fmt;
  895. break;
  896. }
  897. } while (*++p);
  898. if ((p - qual_chars < 2) && (*fmt == *p)) {
  899. p += ((sizeof(qual_chars)-2) / 2);
  900. ++fmt;
  901. }
  902. dataargtype = ((int)(p[(sizeof(qual_chars)-2) / 2])) << 8;
  903. /* Process conversion specifier. */
  904. if (!*fmt) {
  905. return -1;
  906. }
  907. p = spec_chars;
  908. do {
  909. if (*fmt == *p) {
  910. p_m_spec_chars = p - spec_chars;
  911. if ((p_m_spec_chars >= CONV_c)
  912. && (dataargtype & PA_FLAG_LONG)) {
  913. p_m_spec_chars -= 2; /* lc -> C and ls -> S */
  914. }
  915. ppfs->conv_num = p_m_spec_chars;
  916. p = spec_ranges-1;
  917. while (p_m_spec_chars > *++p) {}
  918. i = p - spec_ranges;
  919. argtype[2] = (dataargtype | spec_or_mask[i]) & spec_and_mask[i];
  920. p = spec_chars;
  921. break;
  922. }
  923. } while(*++p);
  924. ppfs->info.spec = *fmt;
  925. ppfs->info.prec = preci;
  926. ppfs->info.width = width;
  927. ppfs->info.pad = ((flags & FLAG_ZERO) ? '0' : ' ');
  928. ppfs->info._flags = (flags & ~FLAG_ZERO) | (dataargtype & __PA_INTMASK);
  929. ppfs->num_data_args = 1;
  930. if (!*p) {
  931. #ifdef __UCLIBC_HAS_PRINTF_M_SPEC__
  932. if (*fmt == 'm') {
  933. ppfs->conv_num = CONV_m;
  934. ppfs->num_data_args = 0;
  935. goto DONE;
  936. }
  937. #endif
  938. #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_PRINTF__
  939. /* Handle custom arg -- WARNING -- overwrites p!!! */
  940. ppfs->conv_num = CONV_custom0;
  941. p = _custom_printf_spec;
  942. do {
  943. if (*p == *fmt) {
  944. if ((ppfs->num_data_args
  945. = ((*_custom_printf_arginfo[(int)(p-_custom_printf_spec)])
  946. (&(ppfs->info), MAX_ARGS_PER_SPEC, argtype+2)))
  947. > MAX_ARGS_PER_SPEC) {
  948. break; /* Error -- too many args! */
  949. }
  950. goto DONE;
  951. }
  952. } while (++p < (_custom_printf_spec + MAX_USER_SPEC));
  953. #endif /* __UCLIBC_HAS_GLIBC_CUSTOM_PRINTF__ */
  954. /* Otherwise error. */
  955. return -1;
  956. }
  957. #if defined(__UCLIBC_HAS_GLIBC_CUSTOM_PRINTF__) || defined(__UCLIBC_HAS_PRINTF_M_SPEC__)
  958. DONE:
  959. #endif
  960. #ifdef NL_ARGMAX
  961. if (maxposarg > 0) {
  962. i = 0;
  963. do {
  964. /* Update maxposarg and check that NL_ARGMAX is not exceeded. */
  965. n = ((i <= 2)
  966. ? (ppfs->argnumber[i] = argnumber[i])
  967. : argnumber[2] + (i-2));
  968. if (n > maxposarg) {
  969. if ((maxposarg = n) > NL_ARGMAX) {
  970. return -1;
  971. }
  972. }
  973. --n;
  974. /* Record argtype with largest size (current, new). */
  975. if (_is_equal_or_bigger_arg(ppfs->argtype[n], argtype[i])) {
  976. ppfs->argtype[n] = argtype[i];
  977. }
  978. } while (++i < ppfs->num_data_args + 2);
  979. } else {
  980. #endif /* NL_ARGMAX */
  981. ppfs->argnumber[2] = 1;
  982. memcpy(ppfs->argtype, argtype + 2, ppfs->num_data_args * sizeof(int));
  983. #ifdef NL_ARGMAX
  984. }
  985. ppfs->maxposarg = maxposarg;
  986. #endif /* NL_ARGMAX */
  987. #ifdef __UCLIBC_HAS_WCHAR__
  988. if ((flags = ppfs->info._flags & FLAG_WIDESTREAM) == 0) {
  989. ppfs->fmtpos = ++fmt;
  990. } else {
  991. ppfs->fmtpos = (const char *) (((const wchar_t *)(ppfs->fmtpos))
  992. + (fmt - buf) );
  993. }
  994. #else /* __UCLIBC_HAS_WCHAR__ */
  995. ppfs->fmtpos = ++fmt;
  996. #endif /* __UCLIBC_HAS_WCHAR__ */
  997. return ppfs->num_data_args + 2;
  998. }
  999. #endif
  1000. /**********************************************************************/
  1001. #ifdef L_register_printf_function
  1002. #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_PRINTF__
  1003. int register_printf_function(int spec, printf_function handler,
  1004. printf_arginfo_function arginfo)
  1005. {
  1006. register char *r;
  1007. register char *p;
  1008. if (spec && (arginfo != NULL)) { /* TODO -- check if spec is valid char */
  1009. r = NULL;
  1010. p = _custom_printf_spec + MAX_USER_SPEC;
  1011. do {
  1012. --p;
  1013. if (!*p) {
  1014. r = p;
  1015. }
  1016. #ifdef __BCC__
  1017. else /* bcc generates less code with fall-through */
  1018. #endif
  1019. if (*p == spec) {
  1020. r = p;
  1021. p = _custom_printf_spec;
  1022. }
  1023. } while (p > _custom_printf_spec);
  1024. if (r) {
  1025. if (handler) {
  1026. *r = spec;
  1027. _custom_printf_handler[(int)(r - p)] = handler;
  1028. _custom_printf_arginfo[(int)(r - p)] = arginfo;
  1029. } else {
  1030. *r = 0;
  1031. }
  1032. return 0;
  1033. }
  1034. /* TODO -- if asked to unregister a non-existent spec, return what? */
  1035. }
  1036. return -1;
  1037. }
  1038. #endif
  1039. #endif
  1040. /**********************************************************************/
  1041. #if defined(L_vfprintf) || defined(L_vfwprintf)
  1042. /* We only support ascii digits (or their USC equivalent codes) in
  1043. * precision and width settings in *printf (wide) format strings.
  1044. * In other words, we don't currently support glibc's 'I' flag.
  1045. * We do accept it, but it is currently ignored. */
  1046. static void _charpad(FILE * __restrict stream, int padchar, size_t numpad);
  1047. #ifdef L_vfprintf
  1048. #define VFPRINTF vfprintf
  1049. #define FMT_TYPE char
  1050. #define OUTNSTR _outnstr
  1051. #define STRLEN strlen
  1052. #define _PPFS_init _ppfs_init
  1053. #define OUTPUT(F,S) __fputs_unlocked(S,F)
  1054. /* #define _outnstr(stream, string, len) __stdio_fwrite(string, len, stream) */
  1055. #define _outnstr(stream, string, len) ((len > 0) ? __stdio_fwrite(string, len, stream) : 0)
  1056. #define FP_OUT _fp_out_narrow
  1057. #ifdef __STDIO_PRINTF_FLOAT
  1058. static void _fp_out_narrow(FILE *fp, intptr_t type, intptr_t len, intptr_t buf)
  1059. {
  1060. if (type & 0x80) { /* Some type of padding needed. */
  1061. int buflen = strlen((const char *) buf);
  1062. if ((len -= buflen) > 0) {
  1063. _charpad(fp, (type & 0x7f), len);
  1064. }
  1065. len = buflen;
  1066. }
  1067. OUTNSTR(fp, (const char *) buf, len);
  1068. }
  1069. #endif /* __STDIO_PRINTF_FLOAT */
  1070. #else /* L_vfprintf */
  1071. #define VFPRINTF vfwprintf
  1072. #define FMT_TYPE wchar_t
  1073. #define OUTNSTR _outnwcs
  1074. #define STRLEN wcslen
  1075. #define _PPFS_init _ppwfs_init
  1076. #define OUTPUT(F,S) fputws(S,F)
  1077. #define _outnwcs(stream, wstring, len) _wstdio_fwrite(wstring, len, stream)
  1078. #define FP_OUT _fp_out_wide
  1079. static void _outnstr(FILE *stream, const char *s, size_t wclen)
  1080. {
  1081. /* NOTE!!! len here is the number of wchars we want to generate!!! */
  1082. wchar_t wbuf[64];
  1083. mbstate_t mbstate;
  1084. size_t todo, r;
  1085. mbstate.__mask = 0;
  1086. todo = wclen;
  1087. while (todo) {
  1088. r = mbsrtowcs(wbuf, &s,
  1089. ((todo <= sizeof(wbuf)/sizeof(wbuf[0]))
  1090. ? todo
  1091. : sizeof(wbuf)/sizeof(wbuf[0])),
  1092. &mbstate);
  1093. assert(((ssize_t)r) > 0);
  1094. _outnwcs(stream, wbuf, r);
  1095. todo -= r;
  1096. }
  1097. }
  1098. #ifdef __STDIO_PRINTF_FLOAT
  1099. #ifdef __UCLIBC_MJN3_ONLY__
  1100. #warning TODO: Move defines from _fpmaxtostr. Put them in a common header.
  1101. #endif
  1102. /* The following defines are from _fpmaxtostr.*/
  1103. #define DIGITS_PER_BLOCK 9
  1104. #define NUM_DIGIT_BLOCKS ((DECIMAL_DIG+DIGITS_PER_BLOCK-1)/DIGITS_PER_BLOCK)
  1105. #define BUF_SIZE ( 3 + NUM_DIGIT_BLOCKS * DIGITS_PER_BLOCK )
  1106. static void _fp_out_wide(FILE *fp, intptr_t type, intptr_t len, intptr_t buf)
  1107. {
  1108. wchar_t wbuf[BUF_SIZE];
  1109. const char *s = (const char *) buf;
  1110. int i;
  1111. if (type & 0x80) { /* Some type of padding needed */
  1112. int buflen = strlen(s);
  1113. if ((len -= buflen) > 0) {
  1114. _charpad(fp, (type & 0x7f), len);
  1115. }
  1116. len = buflen;
  1117. }
  1118. if (len > 0) {
  1119. i = 0;
  1120. do {
  1121. #ifdef __LOCALE_C_ONLY
  1122. wbuf[i] = s[i];
  1123. #else /* __LOCALE_C_ONLY */
  1124. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  1125. if (s[i] == ',') {
  1126. wbuf[i] = __UCLIBC_CURLOCALE_DATA.thousands_sep_wc;
  1127. } else
  1128. #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
  1129. if (s[i] == '.') {
  1130. wbuf[i] = __UCLIBC_CURLOCALE_DATA.decimal_point_wc;
  1131. } else {
  1132. wbuf[i] = s[i];
  1133. }
  1134. #endif /* __LOCALE_C_ONLY */
  1135. } while (++i < len);
  1136. OUTNSTR(fp, wbuf, len);
  1137. }
  1138. }
  1139. #endif /* __STDIO_PRINTF_FLOAT */
  1140. static int _ppwfs_init(register ppfs_t *ppfs, const wchar_t *fmt0)
  1141. {
  1142. static const wchar_t invalid_wcs[] = L"Invalid wide format string.";
  1143. int r;
  1144. /* First, zero out everything... argnumber[], argtype[], argptr[] */
  1145. memset(ppfs, 0, sizeof(ppfs_t)); /* TODO: nonportable???? */
  1146. #ifdef NL_ARGMAX
  1147. --ppfs->maxposarg; /* set to -1 */
  1148. #endif /* NL_ARGMAX */
  1149. ppfs->fmtpos = (const char *) fmt0;
  1150. ppfs->info._flags = FLAG_WIDESTREAM;
  1151. {
  1152. mbstate_t mbstate;
  1153. const wchar_t *p;
  1154. mbstate.__mask = 0; /* Initialize the mbstate. */
  1155. p = fmt0;
  1156. if (wcsrtombs(NULL, &p, SIZE_MAX, &mbstate) == ((size_t)(-1))) {
  1157. ppfs->fmtpos = (const char *) invalid_wcs;
  1158. return -1;
  1159. }
  1160. }
  1161. /* now set all argtypes to no-arg */
  1162. {
  1163. #if 1
  1164. /* TODO - use memset here since already "paid for"? */
  1165. register int *p = ppfs->argtype;
  1166. r = MAX_ARGS;
  1167. do {
  1168. *p++ = __PA_NOARG;
  1169. } while (--r);
  1170. #else
  1171. /* TODO -- get rid of this?? */
  1172. register char *p = (char *) ((MAX_ARGS-1) * sizeof(int));
  1173. do {
  1174. *((int *)(((char *)ppfs) + ((int)p) + offsetof(ppfs_t,argtype))) = __PA_NOARG;
  1175. p -= sizeof(int);
  1176. } while (p);
  1177. #endif
  1178. }
  1179. /*
  1180. * Run through the entire format string to validate it and initialize
  1181. * the positional arg numbers (if any).
  1182. */
  1183. {
  1184. register const wchar_t *fmt = fmt0;
  1185. while (*fmt) {
  1186. if ((*fmt == '%') && (*++fmt != '%')) {
  1187. ppfs->fmtpos = (const char *) fmt; /* back up to the '%' */
  1188. if ((r = _ppfs_parsespec(ppfs)) < 0) {
  1189. return -1;
  1190. }
  1191. fmt = (const wchar_t *) ppfs->fmtpos; /* update to one past end of spec */
  1192. } else {
  1193. ++fmt;
  1194. }
  1195. }
  1196. ppfs->fmtpos = (const char *) fmt0; /* rewind */
  1197. }
  1198. #ifdef NL_ARGMAX
  1199. /* If we have positional args, make sure we know all the types. */
  1200. {
  1201. register int *p = ppfs->argtype;
  1202. r = ppfs->maxposarg;
  1203. while (--r >= 0) {
  1204. if ( *p == __PA_NOARG ) { /* missing arg type!!! */
  1205. return -1;
  1206. }
  1207. ++p;
  1208. }
  1209. }
  1210. #endif /* NL_ARGMAX */
  1211. return 0;
  1212. }
  1213. #endif /* L_vfprintf */
  1214. static void _charpad(FILE * __restrict stream, int padchar, size_t numpad)
  1215. {
  1216. /* TODO -- Use a buffer to cut down on function calls... */
  1217. FMT_TYPE pad[1];
  1218. *pad = padchar;
  1219. while (numpad) {
  1220. OUTNSTR(stream, pad, 1);
  1221. --numpad;
  1222. }
  1223. }
  1224. /* TODO -- Dynamically allocate work space to accomodate stack-poor archs? */
  1225. static int _do_one_spec(FILE * __restrict stream,
  1226. register ppfs_t *ppfs, int *count)
  1227. {
  1228. static const char spec_base[] = SPEC_BASE;
  1229. #ifdef L_vfprintf
  1230. static const char prefix[] = "+\0-\0 \0000x\0000X";
  1231. /* 0 2 4 6 9 11*/
  1232. #else /* L_vfprintf */
  1233. static const wchar_t prefix[] = L"+\0-\0 \0000x\0000X";
  1234. #endif /* L_vfprintf */
  1235. enum {
  1236. PREFIX_PLUS = 0,
  1237. PREFIX_MINUS = 2,
  1238. PREFIX_SPACE = 4,
  1239. PREFIX_LWR_X = 6,
  1240. PREFIX_UPR_X = 9,
  1241. PREFIX_NONE = 11
  1242. };
  1243. #ifdef __va_arg_ptr
  1244. const void * const *argptr;
  1245. #else
  1246. const void * argptr[MAX_ARGS_PER_SPEC];
  1247. #endif
  1248. int *argtype;
  1249. #ifdef __UCLIBC_HAS_WCHAR__
  1250. const wchar_t *ws = NULL;
  1251. mbstate_t mbstate;
  1252. #endif /* __UCLIBC_HAS_WCHAR__ */
  1253. size_t slen;
  1254. #ifdef L_vfprintf
  1255. #define SLEN slen
  1256. #else
  1257. size_t SLEN;
  1258. wchar_t wbuf[2];
  1259. #endif
  1260. int base;
  1261. int numpad;
  1262. int alphacase;
  1263. int numfill = 0; /* TODO: fix */
  1264. int prefix_num = PREFIX_NONE;
  1265. char padchar = ' ';
  1266. #ifdef __UCLIBC_MJN3_ONLY__
  1267. #warning TODO: Determine appropriate buf size.
  1268. #endif /* __UCLIBC_MJN3_ONLY__ */
  1269. /* TODO: buf needs to be big enough for any possible error return strings
  1270. * and also for any locale-grouped long long integer strings generated.
  1271. * This should be large enough for any of the current archs/locales, but
  1272. * eventually this should be handled robustly. */
  1273. char buf[128];
  1274. #ifdef NDEBUG
  1275. _ppfs_parsespec(ppfs);
  1276. #else
  1277. if (_ppfs_parsespec(ppfs) < 0) { /* TODO: just for debugging */
  1278. abort();
  1279. }
  1280. #endif
  1281. _ppfs_setargs(ppfs);
  1282. argtype = ppfs->argtype + ppfs->argnumber[2] - 1;
  1283. /* Deal with the argptr vs argvalue issue. */
  1284. #ifdef __va_arg_ptr
  1285. argptr = (const void * const *) ppfs->argptr;
  1286. #ifdef NL_ARGMAX
  1287. if (ppfs->maxposarg > 0) { /* Using positional args... */
  1288. argptr += ppfs->argnumber[2] - 1;
  1289. }
  1290. #endif /* NL_ARGMAX */
  1291. #else
  1292. /* Need to build a local copy... */
  1293. {
  1294. register argvalue_t *p = ppfs->argvalue;
  1295. int i;
  1296. #ifdef NL_ARGMAX
  1297. if (ppfs->maxposarg > 0) { /* Using positional args... */
  1298. p += ppfs->argnumber[2] - 1;
  1299. }
  1300. #endif /* NL_ARGMAX */
  1301. for (i = 0 ; i < ppfs->num_data_args ; i++ ) {
  1302. argptr[i] = (void *) p++;
  1303. }
  1304. }
  1305. #endif
  1306. {
  1307. register char *s = NULL; /* TODO: Should s be unsigned char * ? */
  1308. if (ppfs->conv_num == CONV_n) {
  1309. _store_inttype(*(void **)*argptr,
  1310. ppfs->info._flags & __PA_INTMASK,
  1311. (intmax_t) (*count));
  1312. return 0;
  1313. }
  1314. if (ppfs->conv_num <= CONV_i) { /* pointer or (un)signed int */
  1315. alphacase = __UIM_LOWER;
  1316. #ifdef __UCLIBC_MJN3_ONLY__
  1317. #ifdef L_vfprintf
  1318. #warning CONSIDER: Should we ignore these flags if stub locale? What about custom specs?
  1319. #endif
  1320. #endif /* __UCLIBC_MJN3_ONLY__ */
  1321. if ((base = spec_base[(int)(ppfs->conv_num - CONV_p)]) == 10) {
  1322. if (PRINT_INFO_FLAG_VAL(&(ppfs->info),group)) {
  1323. alphacase = __UIM_GROUP;
  1324. }
  1325. if (PRINT_INFO_FLAG_VAL(&(ppfs->info),i18n)) {
  1326. alphacase |= 0x80;
  1327. }
  1328. }
  1329. if (ppfs->conv_num <= CONV_u) { /* pointer or unsigned int */
  1330. if (ppfs->conv_num == CONV_X) {
  1331. alphacase = __UIM_UPPER;
  1332. }
  1333. if (ppfs->conv_num == CONV_p) { /* pointer */
  1334. prefix_num = PREFIX_LWR_X;
  1335. } else { /* unsigned int */
  1336. }
  1337. } else { /* signed int */
  1338. base = -base;
  1339. }
  1340. if (ppfs->info.prec < 0) { /* Ignore '0' flag if prec specified. */
  1341. padchar = ppfs->info.pad;
  1342. }
  1343. #ifdef __UCLIBC_MJN3_ONLY__
  1344. #ifdef L_vfprintf
  1345. #warning CONSIDER: If using outdigits and/or grouping, how should we interpret precision?
  1346. #endif
  1347. #endif /* __UCLIBC_MJN3_ONLY__ */
  1348. s = _uintmaxtostr(buf + sizeof(buf) - 1,
  1349. (uintmax_t)
  1350. _load_inttype(*argtype & __PA_INTMASK,
  1351. *argptr, base), base, alphacase);
  1352. if (ppfs->conv_num > CONV_u) { /* signed int */
  1353. if (*s == '-') {
  1354. PRINT_INFO_SET_FLAG(&(ppfs->info),showsign);
  1355. ++s; /* handle '-' in the prefix string */
  1356. prefix_num = PREFIX_MINUS;
  1357. } else if (PRINT_INFO_FLAG_VAL(&(ppfs->info),showsign)) {
  1358. prefix_num = PREFIX_PLUS;
  1359. } else if (PRINT_INFO_FLAG_VAL(&(ppfs->info),space)) {
  1360. prefix_num = PREFIX_SPACE;
  1361. }
  1362. }
  1363. slen = (char *)(buf + sizeof(buf) - 1) - s;
  1364. #ifdef L_vfwprintf
  1365. {
  1366. const char *q = s;
  1367. mbstate.__mask = 0; /* Initialize the mbstate. */
  1368. SLEN = mbsrtowcs(NULL, &q, 0, &mbstate);
  1369. }
  1370. #endif
  1371. numfill = ((ppfs->info.prec < 0) ? 1 : ppfs->info.prec);
  1372. if (PRINT_INFO_FLAG_VAL(&(ppfs->info),alt)) {
  1373. if (ppfs->conv_num <= CONV_x) { /* x or p */
  1374. prefix_num = PREFIX_LWR_X;
  1375. }
  1376. if (ppfs->conv_num == CONV_X) {
  1377. prefix_num = PREFIX_UPR_X;
  1378. }
  1379. if ((ppfs->conv_num == CONV_o) && (numfill <= SLEN)) {
  1380. numfill = ((*s == '0') ? 1 : SLEN + 1);
  1381. }
  1382. }
  1383. if (*s == '0') {
  1384. if (prefix_num >= PREFIX_LWR_X) {
  1385. prefix_num = PREFIX_NONE;
  1386. }
  1387. if (ppfs->conv_num == CONV_p) {/* null pointer */
  1388. s = "(nil)";
  1389. #ifdef L_vfwprintf
  1390. SLEN =
  1391. #endif
  1392. slen = 5;
  1393. numfill = 0;
  1394. } else if (numfill == 0) { /* if precision 0, no output */
  1395. #ifdef L_vfwprintf
  1396. SLEN =
  1397. #endif
  1398. slen = 0;
  1399. }
  1400. }
  1401. numfill = ((numfill > SLEN) ? numfill - SLEN : 0);
  1402. } else if (ppfs->conv_num <= CONV_A) { /* floating point */
  1403. #ifdef __STDIO_PRINTF_FLOAT
  1404. *count +=
  1405. _fpmaxtostr(stream,
  1406. (__fpmax_t)
  1407. (PRINT_INFO_FLAG_VAL(&(ppfs->info),is_long_double)
  1408. ? *(long double *) *argptr
  1409. : (long double) (* (double *) *argptr)),
  1410. &ppfs->info, FP_OUT );
  1411. return 0;
  1412. #else /* __STDIO_PRINTF_FLOAT */
  1413. return -1; /* TODO -- try to continue? */
  1414. #endif /* __STDIO_PRINTF_FLOAT */
  1415. } else if (ppfs->conv_num <= CONV_S) { /* wide char or string */
  1416. #ifdef L_vfprintf
  1417. #ifdef __UCLIBC_HAS_WCHAR__
  1418. mbstate.__mask = 0; /* Initialize the mbstate. */
  1419. if (ppfs->conv_num == CONV_S) { /* wide string */
  1420. if (!(ws = *((const wchar_t **) *argptr))) {
  1421. goto NULL_STRING;
  1422. }
  1423. /* We use an awful uClibc-specific hack here, passing
  1424. * (char*) &ws as the conversion destination. This signals
  1425. * uClibc's wcsrtombs that we want a "restricted" length
  1426. * such that the mbs fits in a buffer of the specified
  1427. * size with no partial conversions. */
  1428. if ((slen = wcsrtombs((char *) &ws, &ws, /* Use awful hack! */
  1429. ((ppfs->info.prec >= 0)
  1430. ? ppfs->info.prec
  1431. : SIZE_MAX), &mbstate))
  1432. == ((size_t)-1)
  1433. ) {
  1434. return -1; /* EILSEQ */
  1435. }
  1436. } else { /* wide char */
  1437. s = buf;
  1438. slen = wcrtomb(s, (*((const wchar_t *) *argptr)), &mbstate);
  1439. if (slen == ((size_t)-1)) {
  1440. return -1; /* EILSEQ */
  1441. }
  1442. s[slen] = 0; /* TODO - Is this necessary? */
  1443. }
  1444. #else /* __UCLIBC_HAS_WCHAR__ */
  1445. return -1;
  1446. #endif /* __UCLIBC_HAS_WCHAR__ */
  1447. } else if (ppfs->conv_num <= CONV_s) { /* char or string */
  1448. if (ppfs->conv_num == CONV_s) { /* string */
  1449. s = *((char **) (*argptr));
  1450. if (s) {
  1451. #ifdef __UCLIBC_HAS_PRINTF_M_SPEC__
  1452. SET_STRING_LEN:
  1453. #endif
  1454. slen = strnlen(s, ((ppfs->info.prec >= 0)
  1455. ? ppfs->info.prec : SIZE_MAX));
  1456. } else {
  1457. #ifdef __UCLIBC_HAS_WCHAR__
  1458. NULL_STRING:
  1459. #endif
  1460. s = "(null)";
  1461. slen = 6;
  1462. }
  1463. } else { /* char */
  1464. s = buf;
  1465. *s = (unsigned char)(*((const int *) *argptr));
  1466. s[1] = 0;
  1467. slen = 1;
  1468. }
  1469. #else /* L_vfprintf */
  1470. if (ppfs->conv_num == CONV_S) { /* wide string */
  1471. ws = *((wchar_t **) (*argptr));
  1472. if (!ws) {
  1473. goto NULL_STRING;
  1474. }
  1475. SLEN = wcsnlen(ws, ((ppfs->info.prec >= 0)
  1476. ? ppfs->info.prec : SIZE_MAX));
  1477. } else { /* wide char */
  1478. *wbuf = (wchar_t)(*((const wint_t *) *argptr));
  1479. CHAR_CASE:
  1480. ws = wbuf;
  1481. wbuf[1] = 0;
  1482. SLEN = 1;
  1483. }
  1484. } else if (ppfs->conv_num <= CONV_s) { /* char or string */
  1485. if (ppfs->conv_num == CONV_s) { /* string */
  1486. #ifdef __UCLIBC_MJN3_ONLY__
  1487. #warning TODO: Fix %s for vfwprintf... output upto illegal sequence?
  1488. #endif /* __UCLIBC_MJN3_ONLY__ */
  1489. s = *((char **) (*argptr));
  1490. if (s) {
  1491. #ifdef __UCLIBC_HAS_PRINTF_M_SPEC__
  1492. SET_STRING_LEN:
  1493. #endif
  1494. /* We use an awful uClibc-specific hack here, passing
  1495. * (wchar_t*) &mbstate as the conversion destination.
  1496. * This signals uClibc's mbsrtowcs that we want a
  1497. * "restricted" length such that the mbs fits in a buffer
  1498. * of the specified size with no partial conversions. */
  1499. {
  1500. const char *q = s;
  1501. mbstate.__mask = 0; /* Initialize the mbstate. */
  1502. SLEN = mbsrtowcs((wchar_t *) &mbstate, &q,
  1503. ((ppfs->info.prec >= 0)
  1504. ? ppfs->info.prec : SIZE_MAX),
  1505. &mbstate);
  1506. }
  1507. if (SLEN == ((size_t)(-1))) {
  1508. return -1; /* EILSEQ */
  1509. }
  1510. } else {
  1511. NULL_STRING:
  1512. s = "(null)";
  1513. SLEN = slen = 6;
  1514. }
  1515. } else { /* char */
  1516. *wbuf = btowc( (unsigned char)(*((const int *) *argptr)) );
  1517. goto CHAR_CASE;
  1518. }
  1519. #endif /* L_vfprintf */
  1520. #ifdef __UCLIBC_HAS_PRINTF_M_SPEC__
  1521. } else if (ppfs->conv_num == CONV_m) {
  1522. s = _glibc_strerror_r(errno, buf, sizeof(buf));
  1523. goto SET_STRING_LEN;
  1524. #endif
  1525. } else {
  1526. #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_PRINTF__
  1527. assert(ppfs->conv_num == CONV_custom0);
  1528. s = _custom_printf_spec;
  1529. do {
  1530. if (*s == ppfs->info.spec) {
  1531. int rv;
  1532. /* TODO -- check return value for sanity? */
  1533. rv = (*_custom_printf_handler
  1534. [(int)(s-_custom_printf_spec)])
  1535. (stream, &ppfs->info, argptr);
  1536. if (rv < 0) {
  1537. return -1;
  1538. }
  1539. *count += rv;
  1540. return 0;
  1541. }
  1542. } while (++s < (_custom_printf_spec + MAX_USER_SPEC));
  1543. #endif /* __UCLIBC_HAS_GLIBC_CUSTOM_PRINTF__ */
  1544. assert(0);
  1545. return -1;
  1546. }
  1547. #ifdef __UCLIBC_MJN3_ONLY__
  1548. #ifdef L_vfprintf
  1549. #warning CONSIDER: If using outdigits and/or grouping, how should we pad?
  1550. #endif
  1551. #endif /* __UCLIBC_MJN3_ONLY__ */
  1552. {
  1553. size_t t;
  1554. t = SLEN + numfill;
  1555. if (prefix_num != PREFIX_NONE) {
  1556. t += ((prefix_num < PREFIX_LWR_X) ? 1 : 2);
  1557. }
  1558. numpad = ((ppfs->info.width > t) ? (ppfs->info.width - t) : 0);
  1559. *count += t + numpad;
  1560. }
  1561. if (padchar == '0') { /* TODO: check this */
  1562. numfill += numpad;
  1563. numpad = 0;
  1564. }
  1565. /* Now handle the output itself. */
  1566. if (!PRINT_INFO_FLAG_VAL(&(ppfs->info),left)) {
  1567. _charpad(stream, ' ', numpad);
  1568. numpad = 0;
  1569. }
  1570. OUTPUT(stream, prefix + prefix_num);
  1571. _charpad(stream, '0', numfill);
  1572. #ifdef L_vfprintf
  1573. #ifdef __UCLIBC_HAS_WCHAR__
  1574. if (!ws) {
  1575. assert(s);
  1576. _outnstr(stream, s, slen);
  1577. } else { /* wide string */
  1578. size_t t;
  1579. mbstate.__mask = 0; /* Initialize the mbstate. */
  1580. while (slen) {
  1581. t = (slen <= sizeof(buf)) ? slen : sizeof(buf);
  1582. t = wcsrtombs(buf, &ws, t, &mbstate);
  1583. assert (t != ((size_t)(-1)));
  1584. _outnstr(stream, buf, t);
  1585. slen -= t;
  1586. }
  1587. }
  1588. #else /* __UCLIBC_HAS_WCHAR__ */
  1589. _outnstr(stream, s, slen);
  1590. #endif /* __UCLIBC_HAS_WCHAR__ */
  1591. #else /* L_vfprintf */
  1592. if (!ws) {
  1593. assert(s);
  1594. _outnstr(stream, s, SLEN);
  1595. } else {
  1596. _outnwcs(stream, ws, SLEN);
  1597. }
  1598. #endif /* L_vfprintf */
  1599. _charpad(stream, ' ', numpad);
  1600. }
  1601. return 0;
  1602. }
  1603. int VFPRINTF (FILE * __restrict stream,
  1604. register const FMT_TYPE * __restrict format,
  1605. va_list arg)
  1606. {
  1607. ppfs_t ppfs;
  1608. int count, r;
  1609. register const FMT_TYPE *s;
  1610. __STDIO_AUTO_THREADLOCK_VAR;
  1611. __STDIO_AUTO_THREADLOCK(stream);
  1612. count = 0;
  1613. s = format;
  1614. if
  1615. #ifdef L_vfprintf
  1616. (!__STDIO_STREAM_IS_NARROW_WRITING(stream)
  1617. && __STDIO_STREAM_TRANS_TO_WRITE(stream, __FLAG_NARROW))
  1618. #else
  1619. (!__STDIO_STREAM_IS_WIDE_WRITING(stream)
  1620. && __STDIO_STREAM_TRANS_TO_WRITE(stream, __FLAG_WIDE))
  1621. #endif
  1622. {
  1623. count = -1;
  1624. } else if (_PPFS_init(&ppfs, format) < 0) { /* Bad format string. */
  1625. OUTNSTR(stream, (const FMT_TYPE *) ppfs.fmtpos,
  1626. STRLEN((const FMT_TYPE *)(ppfs.fmtpos)));
  1627. #if defined(L_vfprintf) && !defined(NDEBUG)
  1628. fprintf(stderr,"\nIMbS: \"%s\"\n\n", format);
  1629. #endif
  1630. count = -1;
  1631. } else {
  1632. _ppfs_prepargs(&ppfs, arg); /* This did a va_copy!!! */
  1633. do {
  1634. while (*format && (*format != '%')) {
  1635. ++format;
  1636. }
  1637. if (format-s) { /* output any literal text in format string */
  1638. if ( (r = OUTNSTR(stream, s, format-s)) < 0) {
  1639. count = -1;
  1640. break;
  1641. }
  1642. count += r;
  1643. }
  1644. if (!*format) { /* we're done */
  1645. break;
  1646. }
  1647. if (format[1] != '%') { /* if we get here, *format == '%' */
  1648. /* TODO: _do_one_spec needs to know what the output funcs are!!! */
  1649. ppfs.fmtpos = (const char *)(++format);
  1650. /* TODO: check -- should only fail on stream error */
  1651. if ( (r = _do_one_spec(stream, &ppfs, &count)) < 0) {
  1652. count = -1;
  1653. break;
  1654. }
  1655. s = format = (const FMT_TYPE *) ppfs.fmtpos;
  1656. } else { /* %% means literal %, so start new string */
  1657. s = ++format;
  1658. ++format;
  1659. }
  1660. } while (1);
  1661. va_end(ppfs.arg); /* Need to clean up after va_copy! */
  1662. }
  1663. /* #if defined(L_vfprintf) && defined(__UCLIBC_HAS_WCHAR__) */
  1664. /* DONE: */
  1665. /* #endif */
  1666. __STDIO_AUTO_THREADUNLOCK(stream);
  1667. return count;
  1668. }
  1669. #endif
  1670. /**********************************************************************/