printf-args.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* Decomposed printf argument list.
  2. Copyright (C) 1999, 2002-2003, 2005-2007, 2009-2011 Free Software
  3. Foundation, Inc.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation,
  14. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  15. /* This file can be parametrized with the following macros:
  16. ENABLE_UNISTDIO Set to 1 to enable the unistdio extensions.
  17. PRINTF_FETCHARGS Name of the function to be defined.
  18. STATIC Set to 'static' to declare the function static. */
  19. #ifndef PRINTF_FETCHARGS
  20. # include <config.h>
  21. #endif
  22. /* Specification. */
  23. #ifndef PRINTF_FETCHARGS
  24. # include "printf-args.h"
  25. #endif
  26. #ifdef STATIC
  27. STATIC
  28. #endif
  29. int
  30. PRINTF_FETCHARGS (va_list args, arguments *a)
  31. {
  32. size_t i;
  33. argument *ap;
  34. for (i = 0, ap = &a->arg[0]; i < a->count; i++, ap++)
  35. switch (ap->type)
  36. {
  37. case TYPE_SCHAR:
  38. ap->a.a_schar = va_arg (args, /*signed char*/ int);
  39. break;
  40. case TYPE_UCHAR:
  41. ap->a.a_uchar = va_arg (args, /*unsigned char*/ int);
  42. break;
  43. case TYPE_SHORT:
  44. ap->a.a_short = va_arg (args, /*short*/ int);
  45. break;
  46. case TYPE_USHORT:
  47. ap->a.a_ushort = va_arg (args, /*unsigned short*/ int);
  48. break;
  49. case TYPE_INT:
  50. ap->a.a_int = va_arg (args, int);
  51. break;
  52. case TYPE_UINT:
  53. ap->a.a_uint = va_arg (args, unsigned int);
  54. break;
  55. case TYPE_LONGINT:
  56. ap->a.a_longint = va_arg (args, long int);
  57. break;
  58. case TYPE_ULONGINT:
  59. ap->a.a_ulongint = va_arg (args, unsigned long int);
  60. break;
  61. #if HAVE_LONG_LONG_INT
  62. case TYPE_LONGLONGINT:
  63. ap->a.a_longlongint = va_arg (args, long long int);
  64. break;
  65. case TYPE_ULONGLONGINT:
  66. ap->a.a_ulonglongint = va_arg (args, unsigned long long int);
  67. break;
  68. #endif
  69. case TYPE_DOUBLE:
  70. ap->a.a_double = va_arg (args, double);
  71. break;
  72. case TYPE_LONGDOUBLE:
  73. ap->a.a_longdouble = va_arg (args, long double);
  74. break;
  75. case TYPE_CHAR:
  76. ap->a.a_char = va_arg (args, int);
  77. break;
  78. #if HAVE_WINT_T
  79. case TYPE_WIDE_CHAR:
  80. /* Although ISO C 99 7.24.1.(2) says that wint_t is "unchanged by
  81. default argument promotions", this is not the case in mingw32,
  82. where wint_t is 'unsigned short'. */
  83. ap->a.a_wide_char =
  84. (sizeof (wint_t) < sizeof (int)
  85. ? (wint_t) va_arg (args, int)
  86. : va_arg (args, wint_t));
  87. break;
  88. #endif
  89. case TYPE_STRING:
  90. ap->a.a_string = va_arg (args, const char *);
  91. /* A null pointer is an invalid argument for "%s", but in practice
  92. it occurs quite frequently in printf statements that produce
  93. debug output. Use a fallback in this case. */
  94. if (ap->a.a_string == NULL)
  95. ap->a.a_string = "(NULL)";
  96. break;
  97. #if HAVE_WCHAR_T
  98. case TYPE_WIDE_STRING:
  99. ap->a.a_wide_string = va_arg (args, const wchar_t *);
  100. /* A null pointer is an invalid argument for "%ls", but in practice
  101. it occurs quite frequently in printf statements that produce
  102. debug output. Use a fallback in this case. */
  103. if (ap->a.a_wide_string == NULL)
  104. {
  105. static const wchar_t wide_null_string[] =
  106. {
  107. (wchar_t)'(',
  108. (wchar_t)'N', (wchar_t)'U', (wchar_t)'L', (wchar_t)'L',
  109. (wchar_t)')',
  110. (wchar_t)0
  111. };
  112. ap->a.a_wide_string = wide_null_string;
  113. }
  114. break;
  115. #endif
  116. case TYPE_POINTER:
  117. ap->a.a_pointer = va_arg (args, void *);
  118. break;
  119. case TYPE_COUNT_SCHAR_POINTER:
  120. ap->a.a_count_schar_pointer = va_arg (args, signed char *);
  121. break;
  122. case TYPE_COUNT_SHORT_POINTER:
  123. ap->a.a_count_short_pointer = va_arg (args, short *);
  124. break;
  125. case TYPE_COUNT_INT_POINTER:
  126. ap->a.a_count_int_pointer = va_arg (args, int *);
  127. break;
  128. case TYPE_COUNT_LONGINT_POINTER:
  129. ap->a.a_count_longint_pointer = va_arg (args, long int *);
  130. break;
  131. #if HAVE_LONG_LONG_INT
  132. case TYPE_COUNT_LONGLONGINT_POINTER:
  133. ap->a.a_count_longlongint_pointer = va_arg (args, long long int *);
  134. break;
  135. #endif
  136. #if ENABLE_UNISTDIO
  137. /* The unistdio extensions. */
  138. case TYPE_U8_STRING:
  139. ap->a.a_u8_string = va_arg (args, const uint8_t *);
  140. /* A null pointer is an invalid argument for "%U", but in practice
  141. it occurs quite frequently in printf statements that produce
  142. debug output. Use a fallback in this case. */
  143. if (ap->a.a_u8_string == NULL)
  144. {
  145. static const uint8_t u8_null_string[] =
  146. { '(', 'N', 'U', 'L', 'L', ')', 0 };
  147. ap->a.a_u8_string = u8_null_string;
  148. }
  149. break;
  150. case TYPE_U16_STRING:
  151. ap->a.a_u16_string = va_arg (args, const uint16_t *);
  152. /* A null pointer is an invalid argument for "%lU", but in practice
  153. it occurs quite frequently in printf statements that produce
  154. debug output. Use a fallback in this case. */
  155. if (ap->a.a_u16_string == NULL)
  156. {
  157. static const uint16_t u16_null_string[] =
  158. { '(', 'N', 'U', 'L', 'L', ')', 0 };
  159. ap->a.a_u16_string = u16_null_string;
  160. }
  161. break;
  162. case TYPE_U32_STRING:
  163. ap->a.a_u32_string = va_arg (args, const uint32_t *);
  164. /* A null pointer is an invalid argument for "%llU", but in practice
  165. it occurs quite frequently in printf statements that produce
  166. debug output. Use a fallback in this case. */
  167. if (ap->a.a_u32_string == NULL)
  168. {
  169. static const uint32_t u32_null_string[] =
  170. { '(', 'N', 'U', 'L', 'L', ')', 0 };
  171. ap->a.a_u32_string = u32_null_string;
  172. }
  173. break;
  174. #endif
  175. default:
  176. /* Unknown type. */
  177. return -1;
  178. }
  179. return 0;
  180. }