vfprintf.c 50 KB

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