_vfprintf.c 51 KB

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