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