vfprintf.c 50 KB

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