vfprintf.c 50 KB

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