_vfprintf.c 51 KB

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