_vfprintf.c 51 KB

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