vfprintf.c 50 KB

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