_vfprintf.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986
  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. /* Experimentally off - libc_hidden_proto(memcpy) */
  110. /* Experimentally off - libc_hidden_proto(memset) */
  111. /* Experimentally off - libc_hidden_proto(strlen) */
  112. /* Experimentally off - 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++] && (i < sizeof(buf)));
  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_internal) || defined(L__vfwprintf_internal)
  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_internal
  1067. #define VFPRINTF_internal _vfprintf_internal
  1068. #define FMT_TYPE char
  1069. #define OUTNSTR _outnstr
  1070. #define STRLEN strlen
  1071. #define _PPFS_init _ppfs_init
  1072. /* Pulls in fseek: #define OUTPUT(F,S) fputs_unlocked(S,F) */
  1073. #define OUTPUT(F,S) __stdio_fwrite((const unsigned char *)(S),strlen(S),(F))
  1074. /* #define _outnstr(stream, string, len) __stdio_fwrite(string, len, stream) */
  1075. #define _outnstr(stream, string, len) ((len > 0) ? __stdio_fwrite(string, len, stream) : 0)
  1076. #define FP_OUT _fp_out_narrow
  1077. #ifdef __STDIO_PRINTF_FLOAT
  1078. static size_t _fp_out_narrow(FILE *fp, intptr_t type, intptr_t len, intptr_t buf)
  1079. {
  1080. size_t r = 0;
  1081. if (type & 0x80) { /* Some type of padding needed. */
  1082. int buflen = strlen((const char *) buf);
  1083. if ((len -= buflen) > 0) {
  1084. if ((r = _charpad(fp, (type & 0x7f), len)) != len) {
  1085. return r;
  1086. }
  1087. }
  1088. len = buflen;
  1089. }
  1090. return r + OUTNSTR(fp, (const unsigned char *) buf, len);
  1091. }
  1092. #endif /* __STDIO_PRINTF_FLOAT */
  1093. #else /* L__vfprintf_internal */
  1094. #define VFPRINTF_internal _vfwprintf_internal
  1095. #define FMT_TYPE wchar_t
  1096. #define OUTNSTR _outnwcs
  1097. #define STRLEN wcslen
  1098. #define _PPFS_init _ppwfs_init
  1099. /* Pulls in fseek: */
  1100. #define OUTPUT(F,S) fputws(S,F)
  1101. /* TODO: #define OUTPUT(F,S) _wstdio_fwrite((S),wcslen(S),(F)) */
  1102. #define _outnwcs(stream, wstring, len) _wstdio_fwrite(wstring, len, stream)
  1103. #define FP_OUT _fp_out_wide
  1104. static size_t _outnstr(FILE *stream, const char *s, size_t wclen)
  1105. {
  1106. /* NOTE!!! len here is the number of wchars we want to generate!!! */
  1107. wchar_t wbuf[64];
  1108. mbstate_t mbstate;
  1109. size_t todo, r, n;
  1110. mbstate.__mask = 0;
  1111. todo = wclen;
  1112. while (todo) {
  1113. r = mbsrtowcs(wbuf, &s,
  1114. ((todo <= sizeof(wbuf)/sizeof(wbuf[0]))
  1115. ? todo
  1116. : sizeof(wbuf)/sizeof(wbuf[0])),
  1117. &mbstate);
  1118. assert(((ssize_t)r) > 0);
  1119. n = _outnwcs(stream, wbuf, r);
  1120. todo -= n;
  1121. if (n != r) {
  1122. break;
  1123. }
  1124. }
  1125. return wclen - todo;
  1126. }
  1127. #ifdef __STDIO_PRINTF_FLOAT
  1128. #ifdef __UCLIBC_MJN3_ONLY__
  1129. #warning TODO: Move defines from _fpmaxtostr. Put them in a common header.
  1130. #endif
  1131. /* The following defines are from _fpmaxtostr.*/
  1132. #define DIGITS_PER_BLOCK 9
  1133. #define NUM_DIGIT_BLOCKS ((DECIMAL_DIG+DIGITS_PER_BLOCK-1)/DIGITS_PER_BLOCK)
  1134. #define BUF_SIZE ( 3 + NUM_DIGIT_BLOCKS * DIGITS_PER_BLOCK )
  1135. static size_t _fp_out_wide(FILE *fp, intptr_t type, intptr_t len, intptr_t buf)
  1136. {
  1137. wchar_t wbuf[BUF_SIZE];
  1138. const char *s = (const char *) buf;
  1139. size_t r = 0;
  1140. int i;
  1141. if (type & 0x80) { /* Some type of padding needed */
  1142. int buflen = strlen(s);
  1143. if ((len -= buflen) > 0) {
  1144. if ((r = _charpad(fp, (type & 0x7f), len)) != len) {
  1145. return r;
  1146. }
  1147. }
  1148. len = buflen;
  1149. }
  1150. if (len > 0) {
  1151. i = 0;
  1152. do {
  1153. #ifdef __LOCALE_C_ONLY
  1154. wbuf[i] = s[i];
  1155. #else /* __LOCALE_C_ONLY */
  1156. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  1157. if (s[i] == ',') {
  1158. wbuf[i] = __UCLIBC_CURLOCALE_DATA.thousands_sep_wc;
  1159. } else
  1160. #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
  1161. if (s[i] == '.') {
  1162. wbuf[i] = __UCLIBC_CURLOCALE_DATA.decimal_point_wc;
  1163. } else {
  1164. wbuf[i] = s[i];
  1165. }
  1166. #endif /* __LOCALE_C_ONLY */
  1167. } while (++i < len);
  1168. r += OUTNSTR(fp, wbuf, len);
  1169. }
  1170. return r;
  1171. }
  1172. #endif /* __STDIO_PRINTF_FLOAT */
  1173. static int _ppwfs_init(register ppfs_t *ppfs, const wchar_t *fmt0)
  1174. {
  1175. static const wchar_t invalid_wcs[] = L"Invalid wide format string.";
  1176. int r;
  1177. /* First, zero out everything... argnumber[], argtype[], argptr[] */
  1178. memset(ppfs, 0, sizeof(ppfs_t)); /* TODO: nonportable???? */
  1179. #ifdef NL_ARGMAX
  1180. --ppfs->maxposarg; /* set to -1 */
  1181. #endif /* NL_ARGMAX */
  1182. ppfs->fmtpos = (const char *) fmt0;
  1183. ppfs->info._flags = FLAG_WIDESTREAM;
  1184. {
  1185. mbstate_t mbstate;
  1186. const wchar_t *p;
  1187. mbstate.__mask = 0; /* Initialize the mbstate. */
  1188. p = fmt0;
  1189. if (wcsrtombs(NULL, &p, SIZE_MAX, &mbstate) == ((size_t)(-1))) {
  1190. ppfs->fmtpos = (const char *) invalid_wcs;
  1191. return -1;
  1192. }
  1193. }
  1194. /* now set all argtypes to no-arg */
  1195. {
  1196. #if 1
  1197. /* TODO - use memset here since already "paid for"? */
  1198. register int *p = ppfs->argtype;
  1199. r = MAX_ARGS;
  1200. do {
  1201. *p++ = __PA_NOARG;
  1202. } while (--r);
  1203. #else
  1204. /* TODO -- get rid of this?? */
  1205. register char *p = (char *) ((MAX_ARGS-1) * sizeof(int));
  1206. do {
  1207. *((int *)(((char *)ppfs) + ((int)p) + offsetof(ppfs_t,argtype))) = __PA_NOARG;
  1208. p -= sizeof(int);
  1209. } while (p);
  1210. #endif
  1211. }
  1212. /*
  1213. * Run through the entire format string to validate it and initialize
  1214. * the positional arg numbers (if any).
  1215. */
  1216. {
  1217. register const wchar_t *fmt = fmt0;
  1218. while (*fmt) {
  1219. if ((*fmt == '%') && (*++fmt != '%')) {
  1220. ppfs->fmtpos = (const char *) fmt; /* back up to the '%' */
  1221. if ((r = _ppfs_parsespec(ppfs)) < 0) {
  1222. return -1;
  1223. }
  1224. fmt = (const wchar_t *) ppfs->fmtpos; /* update to one past end of spec */
  1225. } else {
  1226. ++fmt;
  1227. }
  1228. }
  1229. ppfs->fmtpos = (const char *) fmt0; /* rewind */
  1230. }
  1231. #ifdef NL_ARGMAX
  1232. /* If we have positional args, make sure we know all the types. */
  1233. {
  1234. register int *p = ppfs->argtype;
  1235. r = ppfs->maxposarg;
  1236. while (--r >= 0) {
  1237. if ( *p == __PA_NOARG ) { /* missing arg type!!! */
  1238. return -1;
  1239. }
  1240. ++p;
  1241. }
  1242. }
  1243. #endif /* NL_ARGMAX */
  1244. return 0;
  1245. }
  1246. #endif /* L__vfprintf_internal */
  1247. static size_t _charpad(FILE * __restrict stream, int padchar, size_t numpad)
  1248. {
  1249. size_t todo = numpad;
  1250. /* TODO -- Use a buffer to cut down on function calls... */
  1251. FMT_TYPE pad[1];
  1252. *pad = padchar;
  1253. while (todo && (OUTNSTR(stream, (const unsigned char *) pad, 1) == 1)) {
  1254. --todo;
  1255. }
  1256. return numpad - todo;
  1257. }
  1258. /* TODO -- Dynamically allocate work space to accomodate stack-poor archs? */
  1259. static int _do_one_spec(FILE * __restrict stream,
  1260. register ppfs_t *ppfs, int *count)
  1261. {
  1262. static const char spec_base[] = SPEC_BASE;
  1263. #ifdef L__vfprintf_internal
  1264. static const char prefix[] = "+\0-\0 \0000x\0000X";
  1265. /* 0 2 4 6 9 11*/
  1266. #else /* L__vfprintf_internal */
  1267. static const wchar_t prefix[] = L"+\0-\0 \0000x\0000X";
  1268. #endif /* L__vfprintf_internal */
  1269. enum {
  1270. PREFIX_PLUS = 0,
  1271. PREFIX_MINUS = 2,
  1272. PREFIX_SPACE = 4,
  1273. PREFIX_LWR_X = 6,
  1274. PREFIX_UPR_X = 9,
  1275. PREFIX_NONE = 11
  1276. };
  1277. #ifdef __va_arg_ptr
  1278. const void * const *argptr;
  1279. #else
  1280. const void * argptr[MAX_ARGS_PER_SPEC];
  1281. #endif
  1282. int *argtype;
  1283. #ifdef __UCLIBC_HAS_WCHAR__
  1284. const wchar_t *ws = NULL;
  1285. mbstate_t mbstate;
  1286. #endif /* __UCLIBC_HAS_WCHAR__ */
  1287. size_t slen;
  1288. #ifdef L__vfprintf_internal
  1289. #define SLEN slen
  1290. #else
  1291. size_t SLEN;
  1292. wchar_t wbuf[2];
  1293. #endif
  1294. int base;
  1295. int numpad;
  1296. int alphacase;
  1297. int numfill = 0; /* TODO: fix */
  1298. int prefix_num = PREFIX_NONE;
  1299. char padchar = ' ';
  1300. #ifdef __UCLIBC_MJN3_ONLY__
  1301. #warning TODO: Determine appropriate buf size.
  1302. #endif /* __UCLIBC_MJN3_ONLY__ */
  1303. /* TODO: buf needs to be big enough for any possible error return strings
  1304. * and also for any locale-grouped long long integer strings generated.
  1305. * This should be large enough for any of the current archs/locales, but
  1306. * eventually this should be handled robustly. */
  1307. char buf[128];
  1308. #ifdef NDEBUG
  1309. _ppfs_parsespec(ppfs);
  1310. #else
  1311. if (_ppfs_parsespec(ppfs) < 0) { /* TODO: just for debugging */
  1312. abort();
  1313. }
  1314. #endif
  1315. _ppfs_setargs(ppfs);
  1316. argtype = ppfs->argtype + ppfs->argnumber[2] - 1;
  1317. /* Deal with the argptr vs argvalue issue. */
  1318. #ifdef __va_arg_ptr
  1319. argptr = (const void * const *) ppfs->argptr;
  1320. #ifdef NL_ARGMAX
  1321. if (ppfs->maxposarg > 0) { /* Using positional args... */
  1322. argptr += ppfs->argnumber[2] - 1;
  1323. }
  1324. #endif /* NL_ARGMAX */
  1325. #else
  1326. /* Need to build a local copy... */
  1327. {
  1328. register argvalue_t *p = ppfs->argvalue;
  1329. int i;
  1330. #ifdef NL_ARGMAX
  1331. if (ppfs->maxposarg > 0) { /* Using positional args... */
  1332. p += ppfs->argnumber[2] - 1;
  1333. }
  1334. #endif /* NL_ARGMAX */
  1335. for (i = 0 ; i < ppfs->num_data_args ; i++ ) {
  1336. argptr[i] = (void *) p++;
  1337. }
  1338. }
  1339. #endif
  1340. {
  1341. register char *s = NULL; /* TODO: Should s be unsigned char * ? */
  1342. if (ppfs->conv_num == CONV_n) {
  1343. _store_inttype(*(void **)*argptr,
  1344. ppfs->info._flags & __PA_INTMASK,
  1345. (intmax_t) (*count));
  1346. return 0;
  1347. }
  1348. if (ppfs->conv_num <= CONV_i) { /* pointer or (un)signed int */
  1349. alphacase = __UIM_LOWER;
  1350. #ifdef __UCLIBC_MJN3_ONLY__
  1351. #ifdef L__vfprintf_internal
  1352. #warning CONSIDER: Should we ignore these flags if stub locale? What about custom specs?
  1353. #endif
  1354. #endif /* __UCLIBC_MJN3_ONLY__ */
  1355. if ((base = spec_base[(int)(ppfs->conv_num - CONV_p)]) == 10) {
  1356. if (PRINT_INFO_FLAG_VAL(&(ppfs->info),group)) {
  1357. alphacase = __UIM_GROUP;
  1358. }
  1359. if (PRINT_INFO_FLAG_VAL(&(ppfs->info),i18n)) {
  1360. alphacase |= 0x80;
  1361. }
  1362. }
  1363. if (ppfs->conv_num <= CONV_u) { /* pointer or unsigned int */
  1364. if (ppfs->conv_num == CONV_X) {
  1365. alphacase = __UIM_UPPER;
  1366. }
  1367. if (ppfs->conv_num == CONV_p) { /* pointer */
  1368. prefix_num = PREFIX_LWR_X;
  1369. } else { /* unsigned int */
  1370. }
  1371. } else { /* signed int */
  1372. base = -base;
  1373. }
  1374. if (ppfs->info.prec < 0) { /* Ignore '0' flag if prec specified. */
  1375. padchar = ppfs->info.pad;
  1376. }
  1377. #ifdef __UCLIBC_MJN3_ONLY__
  1378. #ifdef L__vfprintf_internal
  1379. #warning CONSIDER: If using outdigits and/or grouping, how should we interpret precision?
  1380. #endif
  1381. #endif /* __UCLIBC_MJN3_ONLY__ */
  1382. s = _uintmaxtostr(buf + sizeof(buf) - 1,
  1383. (uintmax_t)
  1384. _load_inttype(ppfs->conv_num == CONV_p ? PA_FLAG_LONG : *argtype & __PA_INTMASK,
  1385. *argptr, base), base, alphacase);
  1386. if (ppfs->conv_num > CONV_u) { /* signed int */
  1387. if (*s == '-') {
  1388. PRINT_INFO_SET_FLAG(&(ppfs->info),showsign);
  1389. ++s; /* handle '-' in the prefix string */
  1390. prefix_num = PREFIX_MINUS;
  1391. } else if (PRINT_INFO_FLAG_VAL(&(ppfs->info),showsign)) {
  1392. prefix_num = PREFIX_PLUS;
  1393. } else if (PRINT_INFO_FLAG_VAL(&(ppfs->info),space)) {
  1394. prefix_num = PREFIX_SPACE;
  1395. }
  1396. }
  1397. slen = (char *)(buf + sizeof(buf) - 1) - s;
  1398. #ifdef L__vfwprintf_internal
  1399. {
  1400. const char *q = s;
  1401. mbstate.__mask = 0; /* Initialize the mbstate. */
  1402. SLEN = mbsrtowcs(NULL, &q, 0, &mbstate);
  1403. }
  1404. #endif
  1405. numfill = ((ppfs->info.prec < 0) ? 1 : ppfs->info.prec);
  1406. if (PRINT_INFO_FLAG_VAL(&(ppfs->info),alt)) {
  1407. if (ppfs->conv_num <= CONV_x) { /* x or p */
  1408. prefix_num = PREFIX_LWR_X;
  1409. }
  1410. if (ppfs->conv_num == CONV_X) {
  1411. prefix_num = PREFIX_UPR_X;
  1412. }
  1413. if ((ppfs->conv_num == CONV_o) && (numfill <= SLEN)) {
  1414. numfill = ((*s == '0') ? 1 : SLEN + 1);
  1415. }
  1416. }
  1417. if (*s == '0') {
  1418. if (prefix_num >= PREFIX_LWR_X) {
  1419. prefix_num = PREFIX_NONE;
  1420. }
  1421. if (ppfs->conv_num == CONV_p) {/* null pointer */
  1422. s = "(nil)";
  1423. #ifdef L__vfwprintf_internal
  1424. SLEN =
  1425. #endif
  1426. slen = 5;
  1427. numfill = 0;
  1428. } else if (numfill == 0) { /* if precision 0, no output */
  1429. #ifdef L__vfwprintf_internal
  1430. SLEN =
  1431. #endif
  1432. slen = 0;
  1433. }
  1434. }
  1435. numfill = ((numfill > SLEN) ? numfill - SLEN : 0);
  1436. } else if (ppfs->conv_num <= CONV_A) { /* floating point */
  1437. #ifdef __STDIO_PRINTF_FLOAT
  1438. ssize_t nf;
  1439. nf = _fpmaxtostr(stream,
  1440. (__fpmax_t)
  1441. (PRINT_INFO_FLAG_VAL(&(ppfs->info),is_long_double)
  1442. ? *(long double *) *argptr
  1443. : (long double) (* (double *) *argptr)),
  1444. &ppfs->info, FP_OUT );
  1445. if (nf < 0) {
  1446. return -1;
  1447. }
  1448. *count += nf;
  1449. return 0;
  1450. #else /* __STDIO_PRINTF_FLOAT */
  1451. return -1; /* TODO -- try to continue? */
  1452. #endif /* __STDIO_PRINTF_FLOAT */
  1453. } else if (ppfs->conv_num <= CONV_S) { /* wide char or string */
  1454. #ifdef L__vfprintf_internal
  1455. #ifdef __UCLIBC_HAS_WCHAR__
  1456. mbstate.__mask = 0; /* Initialize the mbstate. */
  1457. if (ppfs->conv_num == CONV_S) { /* wide string */
  1458. if (!(ws = *((const wchar_t **) *argptr))) {
  1459. goto NULL_STRING;
  1460. }
  1461. /* We use an awful uClibc-specific hack here, passing
  1462. * (char*) &ws as the conversion destination. This signals
  1463. * uClibc's wcsrtombs that we want a "restricted" length
  1464. * such that the mbs fits in a buffer of the specified
  1465. * size with no partial conversions. */
  1466. if ((slen = wcsrtombs((char *) &ws, &ws, /* Use awful hack! */
  1467. ((ppfs->info.prec >= 0)
  1468. ? ppfs->info.prec
  1469. : SIZE_MAX), &mbstate))
  1470. == ((size_t)-1)
  1471. ) {
  1472. return -1; /* EILSEQ */
  1473. }
  1474. } else { /* wide char */
  1475. s = buf;
  1476. slen = wcrtomb(s, (*((const wchar_t *) *argptr)), &mbstate);
  1477. if (slen == ((size_t)-1)) {
  1478. return -1; /* EILSEQ */
  1479. }
  1480. s[slen] = 0; /* TODO - Is this necessary? */
  1481. }
  1482. #else /* __UCLIBC_HAS_WCHAR__ */
  1483. return -1;
  1484. #endif /* __UCLIBC_HAS_WCHAR__ */
  1485. } else if (ppfs->conv_num <= CONV_s) { /* char or string */
  1486. if (ppfs->conv_num == CONV_s) { /* string */
  1487. s = *((char **) (*argptr));
  1488. if (s) {
  1489. #ifdef __UCLIBC_HAS_PRINTF_M_SPEC__
  1490. SET_STRING_LEN:
  1491. #endif
  1492. slen = strnlen(s, ((ppfs->info.prec >= 0)
  1493. ? ppfs->info.prec : SIZE_MAX));
  1494. } else {
  1495. #ifdef __UCLIBC_HAS_WCHAR__
  1496. NULL_STRING:
  1497. #endif
  1498. s = "(null)";
  1499. slen = 6;
  1500. }
  1501. } else { /* char */
  1502. s = buf;
  1503. *s = (unsigned char)(*((const int *) *argptr));
  1504. s[1] = 0;
  1505. slen = 1;
  1506. }
  1507. #else /* L__vfprintf_internal */
  1508. if (ppfs->conv_num == CONV_S) { /* wide string */
  1509. ws = *((wchar_t **) (*argptr));
  1510. if (!ws) {
  1511. goto NULL_STRING;
  1512. }
  1513. SLEN = wcsnlen(ws, ((ppfs->info.prec >= 0)
  1514. ? ppfs->info.prec : SIZE_MAX));
  1515. } else { /* wide char */
  1516. *wbuf = (wchar_t)(*((const wint_t *) *argptr));
  1517. CHAR_CASE:
  1518. ws = wbuf;
  1519. wbuf[1] = 0;
  1520. SLEN = 1;
  1521. }
  1522. } else if (ppfs->conv_num <= CONV_s) { /* char or string */
  1523. if (ppfs->conv_num == CONV_s) { /* string */
  1524. #ifdef __UCLIBC_MJN3_ONLY__
  1525. #warning TODO: Fix %s for _vfwprintf_internal... output upto illegal sequence?
  1526. #endif /* __UCLIBC_MJN3_ONLY__ */
  1527. s = *((char **) (*argptr));
  1528. if (s) {
  1529. #ifdef __UCLIBC_HAS_PRINTF_M_SPEC__
  1530. SET_STRING_LEN:
  1531. #endif
  1532. /* We use an awful uClibc-specific hack here, passing
  1533. * (wchar_t*) &mbstate as the conversion destination.
  1534. * This signals uClibc's mbsrtowcs that we want a
  1535. * "restricted" length such that the mbs fits in a buffer
  1536. * of the specified size with no partial conversions. */
  1537. {
  1538. const char *q = s;
  1539. mbstate.__mask = 0; /* Initialize the mbstate. */
  1540. SLEN = mbsrtowcs((wchar_t *) &mbstate, &q,
  1541. ((ppfs->info.prec >= 0)
  1542. ? ppfs->info.prec : SIZE_MAX),
  1543. &mbstate);
  1544. }
  1545. if (SLEN == ((size_t)(-1))) {
  1546. return -1; /* EILSEQ */
  1547. }
  1548. } else {
  1549. NULL_STRING:
  1550. s = "(null)";
  1551. SLEN = slen = 6;
  1552. }
  1553. } else { /* char */
  1554. *wbuf = btowc( (unsigned char)(*((const int *) *argptr)) );
  1555. goto CHAR_CASE;
  1556. }
  1557. #endif /* L__vfprintf_internal */
  1558. #ifdef __UCLIBC_HAS_PRINTF_M_SPEC__
  1559. } else if (ppfs->conv_num == CONV_m) {
  1560. s = __glibc_strerror_r(errno, buf, sizeof(buf));
  1561. goto SET_STRING_LEN;
  1562. #endif
  1563. } else {
  1564. #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_PRINTF__
  1565. assert(ppfs->conv_num == CONV_custom0);
  1566. s = _custom_printf_spec;
  1567. do {
  1568. if (*s == ppfs->info.spec) {
  1569. int rv;
  1570. /* TODO -- check return value for sanity? */
  1571. rv = (*_custom_printf_handler
  1572. [(int)(s-_custom_printf_spec)])
  1573. (stream, &ppfs->info, argptr);
  1574. if (rv < 0) {
  1575. return -1;
  1576. }
  1577. *count += rv;
  1578. return 0;
  1579. }
  1580. } while (++s < (_custom_printf_spec + MAX_USER_SPEC));
  1581. #endif /* __UCLIBC_HAS_GLIBC_CUSTOM_PRINTF__ */
  1582. assert(0);
  1583. return -1;
  1584. }
  1585. #ifdef __UCLIBC_MJN3_ONLY__
  1586. #ifdef L__vfprintf_internal
  1587. #warning CONSIDER: If using outdigits and/or grouping, how should we pad?
  1588. #endif
  1589. #endif /* __UCLIBC_MJN3_ONLY__ */
  1590. {
  1591. size_t t;
  1592. t = SLEN + numfill;
  1593. if (prefix_num != PREFIX_NONE) {
  1594. t += ((prefix_num < PREFIX_LWR_X) ? 1 : 2);
  1595. }
  1596. numpad = ((ppfs->info.width > t) ? (ppfs->info.width - t) : 0);
  1597. *count += t + numpad;
  1598. }
  1599. if (padchar == '0') { /* TODO: check this */
  1600. numfill += numpad;
  1601. numpad = 0;
  1602. }
  1603. /* Now handle the output itself. */
  1604. if (!PRINT_INFO_FLAG_VAL(&(ppfs->info),left)) {
  1605. if (_charpad(stream, ' ', numpad) != numpad) {
  1606. return -1;
  1607. }
  1608. numpad = 0;
  1609. }
  1610. OUTPUT(stream, prefix + prefix_num);
  1611. if (_charpad(stream, '0', numfill) != numfill) {
  1612. return -1;
  1613. }
  1614. #ifdef L__vfprintf_internal
  1615. #ifdef __UCLIBC_HAS_WCHAR__
  1616. if (!ws) {
  1617. assert(s);
  1618. if (_outnstr(stream, s, slen) != slen) {
  1619. return -1;
  1620. }
  1621. } else { /* wide string */
  1622. size_t t;
  1623. mbstate.__mask = 0; /* Initialize the mbstate. */
  1624. while (slen) {
  1625. t = (slen <= sizeof(buf)) ? slen : sizeof(buf);
  1626. t = wcsrtombs(buf, &ws, t, &mbstate);
  1627. assert (t != ((size_t)(-1)));
  1628. if (_outnstr(stream, buf, t) != t) {
  1629. return -1;
  1630. }
  1631. slen -= t;
  1632. }
  1633. }
  1634. #else /* __UCLIBC_HAS_WCHAR__ */
  1635. if (_outnstr(stream, (const unsigned char *) s, slen) != slen) {
  1636. return -1;
  1637. }
  1638. #endif /* __UCLIBC_HAS_WCHAR__ */
  1639. #else /* L__vfprintf_internal */
  1640. if (!ws) {
  1641. assert(s);
  1642. if (_outnstr(stream, s, SLEN) != SLEN) {
  1643. return -1;
  1644. }
  1645. } else {
  1646. if (_outnwcs(stream, ws, SLEN) != SLEN) {
  1647. return -1;
  1648. }
  1649. }
  1650. #endif /* L__vfprintf_internal */
  1651. if (_charpad(stream, ' ', numpad) != numpad) {
  1652. return -1;
  1653. }
  1654. }
  1655. return 0;
  1656. }
  1657. libc_hidden_proto(fprintf)
  1658. int VFPRINTF_internal (FILE * __restrict stream,
  1659. const FMT_TYPE * __restrict format,
  1660. va_list arg)
  1661. {
  1662. ppfs_t ppfs;
  1663. int count, r;
  1664. register const FMT_TYPE *s;
  1665. count = 0;
  1666. s = format;
  1667. if (_PPFS_init(&ppfs, format) < 0) { /* Bad format string. */
  1668. OUTNSTR(stream, (const unsigned char *) ppfs.fmtpos,
  1669. STRLEN((const FMT_TYPE *)(ppfs.fmtpos)));
  1670. #if defined(L__vfprintf_internal) && !defined(NDEBUG)
  1671. fprintf(stderr,"\nIMbS: \"%s\"\n\n", format);
  1672. #endif
  1673. count = -1;
  1674. } else {
  1675. _ppfs_prepargs(&ppfs, arg); /* This did a va_copy!!! */
  1676. do {
  1677. while (*format && (*format != '%')) {
  1678. ++format;
  1679. }
  1680. if (format-s) { /* output any literal text in format string */
  1681. if ( (r = OUTNSTR(stream, (const unsigned char *) s, format-s)) != (format-s)) {
  1682. count = -1;
  1683. break;
  1684. }
  1685. count += r;
  1686. }
  1687. if (!*format) { /* we're done */
  1688. break;
  1689. }
  1690. if (format[1] != '%') { /* if we get here, *format == '%' */
  1691. /* TODO: _do_one_spec needs to know what the output funcs are!!! */
  1692. ppfs.fmtpos = (const char *)(++format);
  1693. /* TODO: check -- should only fail on stream error */
  1694. if ( (r = _do_one_spec(stream, &ppfs, &count)) < 0) {
  1695. count = -1;
  1696. break;
  1697. }
  1698. s = format = (const FMT_TYPE *) ppfs.fmtpos;
  1699. } else { /* %% means literal %, so start new string */
  1700. s = ++format;
  1701. ++format;
  1702. }
  1703. } while (1);
  1704. va_end(ppfs.arg); /* Need to clean up after va_copy! */
  1705. }
  1706. /* #if defined(L__vfprintf_internal) && defined(__UCLIBC_HAS_WCHAR__) */
  1707. /* DONE: */
  1708. /* #endif */
  1709. return count;
  1710. }
  1711. #endif /* defined(L__vfprintf_internal) || defined(L__vfwprintf_internal) */
  1712. /**********************************************************************/
  1713. #if defined(L_vfprintf) || defined(L_vfwprintf)
  1714. /* This is just a wrapper around VFPRINTF_internal.
  1715. * Factoring out vfprintf internals allows:
  1716. * (1) vdprintf and vsnprintf don't need to setup fake locking,
  1717. * (2) __STDIO_STREAM_TRANS_TO_WRITE is not used in vfprintf internals,
  1718. * and thus fseek etc is not pulled in by vdprintf and vsnprintf.
  1719. *
  1720. * In order to not pull in fseek through fputs, OUTPUT() macro
  1721. * is using __stdio_fwrite (TODO: do the same for wide functions).
  1722. */
  1723. #ifdef L_vfprintf
  1724. #define VFPRINTF vfprintf
  1725. #define VFPRINTF_internal _vfprintf_internal
  1726. #define FMT_TYPE char
  1727. #else
  1728. #define VFPRINTF vfwprintf
  1729. #define VFPRINTF_internal _vfwprintf_internal
  1730. #define FMT_TYPE wchar_t
  1731. #endif
  1732. libc_hidden_proto(VFPRINTF)
  1733. int VFPRINTF (FILE * __restrict stream,
  1734. const FMT_TYPE * __restrict format,
  1735. va_list arg)
  1736. {
  1737. int count;
  1738. __STDIO_AUTO_THREADLOCK_VAR;
  1739. __STDIO_AUTO_THREADLOCK(stream);
  1740. if
  1741. #ifdef L_vfprintf
  1742. (!__STDIO_STREAM_IS_NARROW_WRITING(stream)
  1743. && __STDIO_STREAM_TRANS_TO_WRITE(stream, __FLAG_NARROW))
  1744. #else
  1745. (!__STDIO_STREAM_IS_WIDE_WRITING(stream)
  1746. && __STDIO_STREAM_TRANS_TO_WRITE(stream, __FLAG_WIDE))
  1747. #endif
  1748. {
  1749. count = -1;
  1750. } else {
  1751. count = VFPRINTF_internal(stream, format, arg);
  1752. }
  1753. __STDIO_AUTO_THREADUNLOCK(stream);
  1754. return count;
  1755. }
  1756. libc_hidden_def(VFPRINTF)
  1757. #endif /* defined(L_vfprintf) || defined(L_vfwprintf) */
  1758. /**********************************************************************/