You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vsprintf.h 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #ifndef VSPRINTF_H
  2. #define VSPRINTF_H
  3. /** @file
  4. *
  5. * printf() and friends
  6. *
  7. * Etherboot's printf() functions understand the following subset of
  8. * the standard C printf()'s format specifiers:
  9. *
  10. * - Flag characters
  11. * - '#' - Alternate form (i.e. "0x" prefix)
  12. * - '0' - Zero-pad
  13. * - Field widths
  14. * - Length modifiers
  15. * - 'hh' - Signed / unsigned char
  16. * - 'h' - Signed / unsigned short
  17. * - 'l' - Signed / unsigned long
  18. * - 'll' - Signed / unsigned long long
  19. * - 'z' - Signed / unsigned size_t
  20. * - Conversion specifiers
  21. * - 'd' - Signed decimal
  22. * - 'x','X' - Unsigned hexadecimal
  23. * - 'c' - Character
  24. * - 's' - String
  25. * - 'p' - Pointer
  26. *
  27. * Hexadecimal numbers are always zero-padded to the specified field
  28. * width (if any); decimal numbers are always space-padded. Decimal
  29. * long longs are not supported.
  30. *
  31. */
  32. #include <stdint.h>
  33. #include <stdarg.h>
  34. #define PRINTF_NO_LENGTH ( ( size_t ) -1 )
  35. /**
  36. * A printf context
  37. *
  38. * Contexts are used in order to be able to share code between
  39. * vprintf() and vsnprintf(), without requiring the allocation of a
  40. * buffer for vprintf().
  41. */
  42. struct printf_context {
  43. /**
  44. * Character handler
  45. *
  46. * @v ctx Context
  47. * @v c Character
  48. *
  49. * This method is called for each character written to the
  50. * formatted string.
  51. */
  52. void ( * handler ) ( struct printf_context *ctx, unsigned int c );
  53. /** Length of formatted string
  54. *
  55. * When handler() is called, @len will be set to the number of
  56. * characters written so far (i.e. zero for the first call to
  57. * handler()).
  58. */
  59. size_t len;
  60. };
  61. extern size_t vcprintf ( struct printf_context *ctx, const char *fmt,
  62. va_list args );
  63. extern int vsnprintf ( char *buf, size_t size, const char *fmt, va_list args );
  64. extern int vprintf ( const char *fmt, va_list args );
  65. extern int __attribute__ (( format ( printf, 3, 4 ) ))
  66. snprintf ( char *buf, size_t size, const char *fmt, ... );
  67. extern int __attribute__ (( format ( printf, 1, 2 ) ))
  68. printf ( const char *fmt, ... );
  69. /**
  70. * Write a formatted string to a buffer
  71. *
  72. * @v buf Buffer into which to write the string
  73. * @v fmt Format string
  74. * @v ... Arguments corresponding to the format string
  75. * @ret len Length of formatted string
  76. */
  77. #define sprintf( buf, fmt, ... ) \
  78. snprintf ( (buf), PRINTF_NO_LENGTH, (fmt), ## __VA_ARGS__ )
  79. /**
  80. * Write a formatted string to a buffer
  81. *
  82. * @v buf Buffer into which to write the string
  83. * @v fmt Format string
  84. * @v args Arguments corresponding to the format string
  85. * @ret len Length of formatted string
  86. */
  87. static inline int vsprintf ( char *buf, const char *fmt, va_list args ) {
  88. return vsnprintf ( buf, PRINTF_NO_LENGTH, fmt, args );
  89. }
  90. #endif /* VSPRINTF_H */