_vfprintf.c 51 KB

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