vfprintf.c 51 KB

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