vfprintf.c 51 KB

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