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