_vfprintf.c 50 KB

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