_vfprintf.c 52 KB

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