vfprintf.c 50 KB

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