vfprintf.c 50 KB

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