_vfprintf.c 52 KB

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