printf-args.c 6.4 KB

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