vfprintf.c 50 KB

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