_vfprintf.c 51 KB

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