vfprintf.c 50 KB

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