printf-args.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* Decomposed printf argument list.
  2. Copyright (C) 1999, 2002-2003, 2005-2007, 2009-2023 Free Software
  3. Foundation, Inc.
  4. This file is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as
  6. published by the Free Software Foundation; either version 2.1 of the
  7. License, or (at your option) any later version.
  8. This file 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
  13. along with this program. If not, see <https://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. case TYPE_LONGLONGINT:
  61. ap->a.a_longlongint = va_arg (args, long long int);
  62. break;
  63. case TYPE_ULONGLONGINT:
  64. ap->a.a_ulonglongint = va_arg (args, unsigned long long int);
  65. break;
  66. case TYPE_DOUBLE:
  67. ap->a.a_double = va_arg (args, double);
  68. break;
  69. case TYPE_LONGDOUBLE:
  70. ap->a.a_longdouble = va_arg (args, long double);
  71. break;
  72. case TYPE_CHAR:
  73. ap->a.a_char = va_arg (args, int);
  74. break;
  75. #if HAVE_WINT_T
  76. case TYPE_WIDE_CHAR:
  77. /* Although ISO C 99 7.24.1.(2) says that wint_t is "unchanged by
  78. default argument promotions", this is not the case in mingw32,
  79. where wint_t is 'unsigned short'. */
  80. ap->a.a_wide_char =
  81. (sizeof (wint_t) < sizeof (int)
  82. ? (wint_t) va_arg (args, int)
  83. : va_arg (args, wint_t));
  84. break;
  85. #endif
  86. case TYPE_STRING:
  87. ap->a.a_string = va_arg (args, const char *);
  88. /* A null pointer is an invalid argument for "%s", but in practice
  89. it occurs quite frequently in printf statements that produce
  90. debug output. Use a fallback in this case. */
  91. if (ap->a.a_string == NULL)
  92. ap->a.a_string = "(NULL)";
  93. break;
  94. #if HAVE_WCHAR_T
  95. case TYPE_WIDE_STRING:
  96. ap->a.a_wide_string = va_arg (args, const wchar_t *);
  97. /* A null pointer is an invalid argument for "%ls", but in practice
  98. it occurs quite frequently in printf statements that produce
  99. debug output. Use a fallback in this case. */
  100. if (ap->a.a_wide_string == NULL)
  101. {
  102. static const wchar_t wide_null_string[] =
  103. {
  104. (wchar_t)'(',
  105. (wchar_t)'N', (wchar_t)'U', (wchar_t)'L', (wchar_t)'L',
  106. (wchar_t)')',
  107. (wchar_t)0
  108. };
  109. ap->a.a_wide_string = wide_null_string;
  110. }
  111. break;
  112. #endif
  113. case TYPE_POINTER:
  114. ap->a.a_pointer = va_arg (args, void *);
  115. break;
  116. case TYPE_COUNT_SCHAR_POINTER:
  117. ap->a.a_count_schar_pointer = va_arg (args, signed char *);
  118. break;
  119. case TYPE_COUNT_SHORT_POINTER:
  120. ap->a.a_count_short_pointer = va_arg (args, short *);
  121. break;
  122. case TYPE_COUNT_INT_POINTER:
  123. ap->a.a_count_int_pointer = va_arg (args, int *);
  124. break;
  125. case TYPE_COUNT_LONGINT_POINTER:
  126. ap->a.a_count_longint_pointer = va_arg (args, long int *);
  127. break;
  128. case TYPE_COUNT_LONGLONGINT_POINTER:
  129. ap->a.a_count_longlongint_pointer = va_arg (args, long long int *);
  130. break;
  131. #if ENABLE_UNISTDIO
  132. /* The unistdio extensions. */
  133. case TYPE_U8_STRING:
  134. ap->a.a_u8_string = va_arg (args, const uint8_t *);
  135. /* A null pointer is an invalid argument for "%U", but in practice
  136. it occurs quite frequently in printf statements that produce
  137. debug output. Use a fallback in this case. */
  138. if (ap->a.a_u8_string == NULL)
  139. {
  140. static const uint8_t u8_null_string[] =
  141. { '(', 'N', 'U', 'L', 'L', ')', 0 };
  142. ap->a.a_u8_string = u8_null_string;
  143. }
  144. break;
  145. case TYPE_U16_STRING:
  146. ap->a.a_u16_string = va_arg (args, const uint16_t *);
  147. /* A null pointer is an invalid argument for "%lU", but in practice
  148. it occurs quite frequently in printf statements that produce
  149. debug output. Use a fallback in this case. */
  150. if (ap->a.a_u16_string == NULL)
  151. {
  152. static const uint16_t u16_null_string[] =
  153. { '(', 'N', 'U', 'L', 'L', ')', 0 };
  154. ap->a.a_u16_string = u16_null_string;
  155. }
  156. break;
  157. case TYPE_U32_STRING:
  158. ap->a.a_u32_string = va_arg (args, const uint32_t *);
  159. /* A null pointer is an invalid argument for "%llU", but in practice
  160. it occurs quite frequently in printf statements that produce
  161. debug output. Use a fallback in this case. */
  162. if (ap->a.a_u32_string == NULL)
  163. {
  164. static const uint32_t u32_null_string[] =
  165. { '(', 'N', 'U', 'L', 'L', ')', 0 };
  166. ap->a.a_u32_string = u32_null_string;
  167. }
  168. break;
  169. #endif
  170. default:
  171. /* Unknown type. */
  172. return -1;
  173. }
  174. return 0;
  175. }