iPXE
Data Structures | Functions | Variables
vsprintf.h File Reference

printf() and friends More...

#include <stdint.h>
#include <stdarg.h>
#include <stdio.h>

Go to the source code of this file.

Data Structures

struct  printf_context
 A printf context. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
 FILE_SECBOOT (PERMITTED)
 
size_t vcprintf (struct printf_context *ctx, const char *fmt, va_list args)
 Write a formatted string to a printf context. More...
 
int vssnprintf (char *buf, ssize_t ssize, const char *fmt, va_list args)
 Version of vsnprintf() that accepts a signed buffer size. More...
 
int __attribute__ ((format(printf, 3, 4))) ssnprintf(char *buf
 

Variables

int ssize_t ssize
 
int ssize_t const char * fmt
 

Detailed Description

printf() and friends

Etherboot's printf() functions understand the following subset of the standard C printf()'s format specifiers:

 - Flag characters
    - '#'           - Alternate form (i.e. "0x" prefix)
    - '0'           - Zero-pad
 - Field widths
 - Length modifiers
    - 'hh'          - Signed / unsigned char
    - 'h'           - Signed / unsigned short
    - 'l'           - Signed / unsigned long
    - 'll'          - Signed / unsigned long long
    - 'z'           - Signed / unsigned size_t
 - Conversion specifiers
    - 'd'           - Signed decimal
    - 'x','X'       - Unsigned hexadecimal
    - 'c'           - Character
    - 's'           - String
    - 'p'           - Pointer

Hexadecimal numbers are always zero-padded to the specified field width (if any); decimal numbers are always space-padded. Decimal long longs are not supported.

Definition in file vsprintf.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED  )

◆ vcprintf()

size_t vcprintf ( struct printf_context ctx,
const char *  fmt,
va_list  args 
)

Write a formatted string to a printf context.

Parameters
ctxContext
fmtFormat string
argsArguments corresponding to the format string
Return values
lenLength of formatted string

Definition at line 188 of file vsprintf.c.

188  {
189  int flags;
190  int width;
191  uint8_t *length;
192  char *ptr;
193  char tmp_buf[32]; /* 32 is enough for all numerical formats.
194  * Insane width fields could overflow this buffer. */
195  wchar_t *wptr;
196 
197  /* Initialise context */
198  ctx->len = 0;
199 
200  for ( ; *fmt ; fmt++ ) {
201  /* Pass through ordinary characters */
202  if ( *fmt != '%' ) {
203  cputchar ( ctx, *fmt );
204  continue;
205  }
206  fmt++;
207  /* Process flag characters */
208  flags = 0;
209  for ( ; ; fmt++ ) {
210  if ( *fmt == '#' ) {
211  flags |= ALT_FORM;
212  } else if ( *fmt == '0' ) {
213  flags |= ZPAD;
214  } else {
215  /* End of flag characters */
216  break;
217  }
218  }
219  /* Process field width */
220  width = 0;
221  for ( ; ; fmt++ ) {
222  if ( ( ( unsigned ) ( *fmt - '0' ) ) < 10 ) {
223  width = ( width * 10 ) + ( *fmt - '0' );
224  } else {
225  break;
226  }
227  }
228  /* We don't do floating point */
229  /* Process length modifier */
231  for ( ; ; fmt++ ) {
232  if ( *fmt == 'h' ) {
233  length--;
234  } else if ( *fmt == 'l' ) {
235  length++;
236  } else if ( *fmt == 'z' ) {
238  } else {
239  break;
240  }
241  }
242  /* Process conversion specifier */
243  ptr = tmp_buf + sizeof ( tmp_buf ) - 1;
244  *ptr = '\0';
245  wptr = NULL;
246  if ( *fmt == 'c' ) {
247  if ( length < &type_sizes[LONG_LEN] ) {
248  cputchar ( ctx, va_arg ( args, unsigned int ) );
249  } else {
250  wchar_t wc;
251  size_t len;
252 
253  wc = va_arg ( args, wint_t );
254  len = wcrtomb ( tmp_buf, wc, NULL );
255  tmp_buf[len] = '\0';
256  ptr = tmp_buf;
257  }
258  } else if ( *fmt == 's' ) {
259  if ( length < &type_sizes[LONG_LEN] ) {
260  ptr = va_arg ( args, char * );
261  if ( ! ptr )
262  ptr = "<NULL>";
263  } else {
264  wptr = va_arg ( args, wchar_t * );
265  if ( ! wptr )
266  ptr = "<NULL>";
267  }
268  } else if ( *fmt == 'p' ) {
269  intptr_t ptrval;
270 
271  ptrval = ( intptr_t ) va_arg ( args, void * );
272  ptr = format_hex ( ptr, ptrval, width,
273  ( ALT_FORM | LCASE ) );
274  } else if ( ( *fmt & ~0x20 ) == 'X' ) {
275  unsigned long long hex;
276 
277  flags |= ( *fmt & 0x20 ); /* LCASE */
278  if ( *length >= sizeof ( unsigned long long ) ) {
279  hex = va_arg ( args, unsigned long long );
280  } else if ( *length >= sizeof ( unsigned long ) ) {
281  hex = va_arg ( args, unsigned long );
282  } else {
283  hex = va_arg ( args, unsigned int );
284  }
285  ptr = format_hex ( ptr, hex, width, flags );
286  } else if ( ( *fmt == 'd' ) || ( *fmt == 'i' ) ){
287  signed long decimal;
288 
289  if ( *length >= sizeof ( signed long ) ) {
290  decimal = va_arg ( args, signed long );
291  } else {
292  decimal = va_arg ( args, signed int );
293  }
294  ptr = format_decimal ( ptr, decimal, width, flags );
295  } else {
296  *(--ptr) = *fmt;
297  }
298  /* Write out conversion result */
299  if ( wptr == NULL ) {
300  for ( ; *ptr ; ptr++ ) {
301  cputchar ( ctx, *ptr );
302  }
303  } else {
304  for ( ; *wptr ; wptr++ ) {
305  size_t len = wcrtomb ( tmp_buf, *wptr, NULL );
306  for ( ptr = tmp_buf ; len-- ; ptr++ ) {
307  cputchar ( ctx, *ptr );
308  }
309  }
310  }
311  }
312 
313  return ctx->len;
314 }
u16 length
Definition: sky2.h:9
static wchar_t wc
Definition: wchar.h:23
#define ALT_FORM
Use "alternate form".
Definition: vsprintf.c:67
static void cputchar(struct printf_context *ctx, unsigned char c)
Print character via a printf context.
Definition: vsprintf.c:175
static char * format_decimal(char *end, signed long num, int width, int flags)
Format a decimal number.
Definition: vsprintf.c:134
char hex[8]
Count (as an eight-digit hex value)
Definition: pccrd.h:13
#define INT_LEN
no length modifier
Definition: vsprintf.c:38
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
unsigned long intptr_t
Definition: stdint.h:21
#define LCASE
Use lower-case for hexadecimal digits.
Definition: vsprintf.c:59
#define LONG_LEN
"l" length modifier
Definition: vsprintf.c:39
#define SIZE_T_LEN
"z" length modifier
Definition: vsprintf.c:41
#define va_arg(ap, type)
Definition: stdarg.h:9
ring len
Length.
Definition: dwmac.h:231
#define ZPAD
Use zero padding.
Definition: vsprintf.c:75
uint8_t flags
Flags.
Definition: ena.h:18
static char * format_hex(char *end, unsigned long long num, int width, int flags)
Format a hexadecimal number.
Definition: vsprintf.c:94
static uint8_t type_sizes[]
Definition: vsprintf.c:43
unsigned char uint8_t
Definition: stdint.h:10
int ssize_t const char * fmt
Definition: vsprintf.h:73
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
__WINT_TYPE__ wint_t
Definition: stddef.h:51

References ALT_FORM, cputchar(), ctx, flags, fmt, format_decimal(), format_hex(), hex, INT_LEN, LCASE, len, length, LONG_LEN, NULL, SIZE_T_LEN, type_sizes, va_arg, wc, and ZPAD.

Referenced by efi_vsnprintf(), vprintf(), vsnprintf(), and vw_printw().

◆ vssnprintf()

int vssnprintf ( char *  buf,
ssize_t  ssize,
const char *  fmt,
va_list  args 
)

Version of vsnprintf() that accepts a signed buffer size.

Parameters
bufBuffer into which to write the string
sizeSize of buffer
fmtFormat string
argsArguments corresponding to the format string
Return values
lenLength of formatted string

Definition at line 402 of file vsprintf.c.

402  {
403 
404  /* Treat negative buffer size as zero buffer size */
405  if ( ssize < 0 )
406  ssize = 0;
407 
408  /* Hand off to vsnprintf */
409  return vsnprintf ( buf, ssize, fmt, args );
410 }
int ssize_t ssize
Definition: vsprintf.h:73
int ssize_t const char * fmt
Definition: vsprintf.h:73
int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
Write a formatted string to a buffer.
Definition: vsprintf.c:352

References sputc_context::buf, fmt, ssize, and vsnprintf().

Referenced by ssnprintf().

◆ __attribute__()

int __attribute__ ( (format(printf, 3, 4))  )

Variable Documentation

◆ ssize

int ssize_t ssize

Definition at line 73 of file vsprintf.h.

Referenced by ssnprintf(), and vssnprintf().

◆ fmt

int int size_t const char int const char * fmt