printf.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* Copyright (C) 1991-1993,1995-1999,2000,2001 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. /* March 11, 2001 Manuel Novoa III
  15. *
  16. * Modified as appropriate for my new stdio lib.
  17. */
  18. #ifndef _PRINTF_H
  19. #define _PRINTF_H 1
  20. #include <features.h>
  21. __BEGIN_DECLS
  22. #define __need_FILE
  23. #include <stdio.h>
  24. #define __need_size_t
  25. #define __need_wchar_t
  26. #include <stddef.h>
  27. /* WARNING -- This is definitely nonportable... but it seems to work
  28. * with gcc, which is currently the only "supported" compiler.
  29. * The library code uses bitmasks for space-efficiency (you can't
  30. * set/test multiple bitfields in one operation). Unfortunatly, we
  31. * need to support bitfields since that's what glibc made visible to users.
  32. * So, we take
  33. * advantage of how gcc lays out bitfields to create an appropriate
  34. * mapping. Inside uclibc (i.e. if _LIBC is defined) we access the
  35. * bitfields using bitmasks in a single flag variable.
  36. *
  37. * WARNING -- This may very well fail if built with -fpack-struct!!!
  38. *
  39. * TODO -- Add a validation test.
  40. * TODO -- Add an option to build in a shim translation function if
  41. * the bitfield<->bitmask mapping fails.
  42. */
  43. #include <endian.h>
  44. struct printf_info {
  45. int prec; /* Precision. */
  46. int width; /* Width. */
  47. #ifdef __UCLIBC_HAS_WCHAR__
  48. wchar_t spec; /* Format letter. */
  49. #else
  50. int spec;
  51. #endif
  52. #ifndef _LIBC
  53. #if __BYTE_ORDER == __LITTLE_ENDIAN
  54. unsigned int space:1; /* Space flag. */
  55. unsigned int showsign:1; /* + flag. */
  56. unsigned int extra:1; /* For special use. */
  57. unsigned int left:1; /* - flag. */
  58. unsigned int alt:1; /* # flag. */
  59. unsigned int group:1; /* ' flag. */
  60. unsigned int i18n:1; /* I flag. */
  61. unsigned int wide:1; /* Nonzero for wide character streams. */
  62. unsigned int is_char:1; /* hh flag. */
  63. unsigned int is_short:1; /* h flag. */
  64. unsigned int is_long:1; /* l flag. */
  65. unsigned int is_long_double:1;/* L flag. */
  66. unsigned int __padding:20; /* non-gnu: match _flags width of 32 bits */
  67. #elif __BYTE_ORDER == __BIG_ENDIAN
  68. unsigned int __padding:20; /* non-gnu: match _flags width of 32 bits */
  69. unsigned int is_long_double:1;/* L flag. */
  70. unsigned int is_long:1; /* l flag. */
  71. unsigned int is_short:1; /* h flag. */
  72. unsigned int is_char:1; /* hh flag. */
  73. unsigned int wide:1; /* Nonzero for wide character streams. */
  74. unsigned int i18n:1; /* I flag. */
  75. unsigned int group:1; /* ' flag. */
  76. unsigned int alt:1; /* # flag. */
  77. unsigned int left:1; /* - flag. */
  78. unsigned int extra:1; /* For special use. */
  79. unsigned int showsign:1; /* + flag. */
  80. unsigned int space:1; /* Space flag. */
  81. #else
  82. #error unsupported byte order!
  83. #endif
  84. #else /* _LIBC */
  85. uint32_t _flags; /* non-gnu */
  86. #define __PRINT_INFO_FLAG_space (1<<0)
  87. #define __PRINT_INFO_FLAG_showsign (1<<1)
  88. #define __PRINT_INFO_FLAG_extra (1<<2)
  89. #define __PRINT_INFO_FLAG_left (1<<3)
  90. #define __PRINT_INFO_FLAG_alt (1<<4)
  91. #define __PRINT_INFO_FLAG_group (1<<5)
  92. #define __PRINT_INFO_FLAG_i18n (1<<6)
  93. #define __PRINT_INFO_FLAG_wide (1<<7)
  94. #define __PRINT_INFO_FLAG_is_char (1<<8)
  95. #define __PRINT_INFO_FLAG_is_short (1<<9)
  96. #define __PRINT_INFO_FLAG_is_long (1<<10)
  97. #define __PRINT_INFO_FLAG_is_long_double (1<<11)
  98. #define PRINT_INFO_FLAG_VAL(INFO_PTR,BITFIELD) \
  99. ((INFO_PTR)->_flags & __PRINT_INFO_FLAG_##BITFIELD)
  100. #define PRINT_INFO_SET_FLAG(INFO_PTR,BITFIELD) \
  101. ((INFO_PTR)->_flags |= __PRINT_INFO_FLAG_##BITFIELD)
  102. #define PRINT_INFO_CLR_FLAG(INFO_PTR,BITFIELD) \
  103. ((INFO_PTR)->_flags &= ~__PRINT_INFO_FLAG_##BITFIELD)
  104. #define PRINT_INFO_SET_extra(INFO_PTR,VAL) \
  105. ((INFO_PTR)->_flags |= (((INFO_PTR)->_flags & ~1) | ((VAL) & 1)))
  106. #endif /* _LIBC */
  107. #ifdef __UCLIBC_HAS_WCHAR__
  108. wchar_t pad; /* Padding character. */
  109. #else
  110. int pad;
  111. #endif
  112. };
  113. /* Type of a printf specifier-handler function.
  114. STREAM is the FILE on which to write output.
  115. INFO gives information about the format specification.
  116. ARGS is a vector of pointers to the argument data;
  117. the number of pointers will be the number returned
  118. by the associated arginfo function for the same INFO.
  119. The function should return the number of characters written,
  120. or -1 for errors. */
  121. #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_PRINTF__
  122. typedef int (*printf_function) (FILE *__stream,
  123. const struct printf_info *__info,
  124. const void *const *__args);
  125. /* Type of a printf specifier-arginfo function.
  126. INFO gives information about the format specification.
  127. N, ARGTYPES, and return value are as for parse_printf_format. */
  128. typedef int printf_arginfo_function (const struct printf_info *__info,
  129. size_t __n, int *__argtypes);
  130. /* Register FUNC to be called to format SPEC specifiers; ARGINFO must be
  131. specified to determine how many arguments a SPEC conversion requires and
  132. what their types are. */
  133. extern int register_printf_function (int __spec, printf_function __func,
  134. printf_arginfo_function __arginfo);
  135. #endif
  136. /* Parse FMT, and fill in N elements of ARGTYPES with the
  137. types needed for the conversions FMT specifies. Returns
  138. the number of arguments required by FMT.
  139. The ARGINFO function registered with a user-defined format is passed a
  140. `struct printf_info' describing the format spec being parsed. A width
  141. or precision of INT_MIN means a `*' was used to indicate that the
  142. width/precision will come from an arg. The function should fill in the
  143. array it is passed with the types of the arguments it wants, and return
  144. the number of arguments it wants. */
  145. extern size_t parse_printf_format (const char *__restrict __fmt, size_t __n,
  146. int *__restrict __argtypes) __THROW;
  147. /* Codes returned by `parse_printf_format' for basic types.
  148. These values cover all the standard format specifications.
  149. Users can add new values after PA_LAST for their own types. */
  150. /* WARNING -- The above is not entirely true, even for glibc.
  151. * As far as the library code is concerned, such args are treated
  152. * as 'your type' pointers if qualified by PA_FLAG_PTR. If they
  153. * aren't qualified as pointers, I _think_ glibc just ignores them
  154. * and carries on. I think it should be treated as an error. */
  155. enum { /* C type: */
  156. PA_INT, /* int */
  157. PA_CHAR, /* int, cast to char */
  158. PA_WCHAR, /* wide char */
  159. PA_STRING, /* const char *, a '\0'-terminated string */
  160. PA_WSTRING, /* const wchar_t *, wide character string */
  161. PA_POINTER, /* void * */
  162. PA_FLOAT, /* float */
  163. PA_DOUBLE, /* double */
  164. __PA_NOARG, /* non-glibc -- signals non-arg width or prec */
  165. PA_LAST
  166. };
  167. /* Flag bits that can be set in a type returned by `parse_printf_format'. */
  168. /* WARNING -- These differ in value from what glibc uses. */
  169. #define PA_FLAG_MASK (0xff00)
  170. #define __PA_FLAG_CHAR (0x0100) /* non-gnu -- to deal with hh */
  171. #define PA_FLAG_SHORT (0x0200)
  172. #define PA_FLAG_LONG (0x0400)
  173. #define PA_FLAG_LONG_LONG (0x0800)
  174. #define PA_FLAG_LONG_DOUBLE PA_FLAG_LONG_LONG
  175. #define PA_FLAG_PTR (0x1000) /* TODO -- make dynamic??? */
  176. #define __PA_INTMASK (0x0f00) /* non-gnu -- all int flags */
  177. #if 0
  178. /* Function which can be registered as `printf'-handlers. */
  179. /* Print floating point value using using abbreviations for the orders
  180. of magnitude used for numbers ('k' for kilo, 'm' for mega etc). If
  181. the format specifier is a uppercase character powers of 1000 are
  182. used. Otherwise powers of 1024. */
  183. extern int printf_size (FILE *__restrict __fp,
  184. const struct printf_info *__info,
  185. const void *const *__restrict __args) __THROW;
  186. /* This is the appropriate argument information function for `printf_size'. */
  187. extern int printf_size_info (const struct printf_info *__restrict
  188. __info, size_t __n, int *__restrict __argtypes)
  189. __THROW;
  190. #endif
  191. __END_DECLS
  192. #endif /* printf.h */