vfprintf.c 50 KB

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