_vfprintf.c 52 KB

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