_vfprintf.c 48 KB

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